Merge commit '1276d1e1a1b128f7093a3021d3f6bc27afa80d23' into amd64
[dragonfly.git] / sbin / i386 / fdisk / fdisk.c
1 /*
2  * Mach Operating System
3  * Copyright (c) 1992 Carnegie Mellon University
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  *
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie Mellon
24  * the rights to redistribute these changes.
25  *
26  * $FreeBSD: /repoman/r/ncvs/src/sbin/i386/fdisk/fdisk.c,v 1.36.2.14 2004/01/30 14:40:47 harti Exp $
27  * $DragonFly: src/sbin/i386/fdisk/fdisk.c,v 1.16 2008/04/23 22:09:07 thomas Exp $
28  */
29
30 #include <sys/types.h>
31 #include <sys/diskslice.h>
32 #include <sys/diskmbr.h>
33 #include <sys/stat.h>
34 #include <ctype.h>
35 #include <fcntl.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 int iotest;
44
45 #define LBUF 100
46 static char lbuf[LBUF];
47
48 #define MBRSIGOFF       510
49
50 /*
51  *
52  * Ported to 386bsd by Julian Elischer  Thu Oct 15 20:26:46 PDT 1992
53  *
54  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
55  *      Copyright (c) 1989      Robert. V. Baron
56  *      Created.
57  */
58
59 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
60 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
61 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
62
63 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
64
65 #define MAX_SEC_SIZE 2048       /* maximum section size that is supported */
66 #define MIN_SEC_SIZE 512        /* the sector size to start sensing at */
67 int secsize = 0;                /* the sensed sector size */
68
69 const char *disk;
70 const char *disks[] =
71 {
72   "/dev/ad0", "/dev/da0", "/dev/vkd0", 0
73 };
74
75 int cyls, sectors, heads, cylsecs;
76 int64_t disksecs;
77
78 struct mboot
79 {
80         unsigned char padding[2]; /* force the longs to be long aligned */
81         unsigned char *bootinst;  /* boot code */
82         off_t bootinst_size;
83         struct  dos_partition parts[4];
84 };
85 struct mboot mboot = {{0}, NULL, 0};
86
87 #define ACTIVE 0x80
88 #define BOOT_MAGIC 0xAA55
89
90 int dos_cyls;
91 int dos_heads;
92 int dos_sectors;
93 int dos_cylsecs;
94
95 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
96 #define DOSCYL(c)       (c & 0xff)
97 #define MAXCYL          1023
98 static int partition = -1;
99
100
101 #define MAX_ARGS        10
102
103 static int      current_line_number;
104
105 static int      geom_processed = 0;
106 static int      part_processed = 0;
107 static int      active_processed = 0;
108
109
110 typedef struct cmd {
111     char                cmd;
112     int                 n_args;
113     struct arg {
114         char    argtype;
115         int     arg_val;
116     }                   args[MAX_ARGS];
117 } CMD;
118
119
120 static int B_flag  = 0;         /* replace boot code */
121 static int C_flag  = 0;         /* use wrapped values for CHS */
122 static int I_flag  = 0;         /* use entire disk for DragonFly */
123 static int a_flag  = 0;         /* set active partition */
124 static char *b_flag = NULL;     /* path to boot code */
125 static int i_flag  = 0;         /* replace partition data */
126 static int u_flag  = 0;         /* update partition data */
127 static int p_flag  = 0;         /* operate on a disk image file */
128 static int s_flag  = 0;         /* Print a summary and exit */
129 static int t_flag  = 0;         /* test only */
130 static char *f_flag = NULL;     /* Read config info from file */
131 static int v_flag  = 0;         /* Be verbose */
132
133 struct part_type
134 {
135  unsigned char type;
136  char *name;
137 }part_types[] =
138 {
139          {0x00, "unused"}
140         ,{0x01, "Primary DOS with 12 bit FAT"}
141         ,{0x02, "XENIX / filesystem"}
142         ,{0x03, "XENIX /usr filesystem"}
143         ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
144         ,{0x05, "Extended DOS"}
145         ,{0x06, "Primary 'big' DOS (> 32MB)"}
146         ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
147         ,{0x08, "AIX filesystem"}
148         ,{0x09, "AIX boot partition or Coherent"}
149         ,{0x0A, "OS/2 Boot Manager or OPUS"}
150         ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
151         ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
152         ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
153         ,{0x0F, "Extended DOS, LBA"}
154         ,{0x10, "OPUS"}
155         ,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
156         ,{0x12, "Compaq diagnostics"}
157         ,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
158         ,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
159         ,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
160         ,{0x18, "AST Windows swapfile"}
161         ,{0x24, "NEC DOS"}
162         ,{0x39, "plan9"}
163         ,{0x3C, "PartitionMagic recovery"}
164         ,{0x40, "VENIX 286"}
165         ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
166         ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
167         ,{0x43, "Linux native (sharing disk with DRDOS)"}
168         ,{0x4D, "QNX 4.2 Primary"}
169         ,{0x4E, "QNX 4.2 Secondary"}
170         ,{0x4F, "QNX 4.2 Tertiary"}
171         ,{0x50, "DM"}
172         ,{0x51, "DM"}
173         ,{0x52, "CP/M or Microport SysV/AT"}
174         ,{0x53, "DM6 Aux3"}
175         ,{0x54, "DM6"}
176         ,{0x55, "EZ-Drive (disk manager)"}
177         ,{0x56, "GB"}
178         ,{0x5C, "Priam Edisk (disk manager)"} /* according to S. Widlake */
179         ,{0x61, "Speed"}
180         ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
181         ,{0x64, "Novell Netware 2.xx"}
182         ,{0x65, "Novell Netware 3.xx"}
183         ,{0x70, "DiskSecure Multi-Boot"}
184         ,{0x75, "PCIX"}
185         ,{0x77, "QNX4.x"}
186         ,{0x78, "QNX4.x 2nd part"}
187         ,{0x79, "QNX4.x 3rd part"}
188         ,{0x80, "Minix 1.1 ... 1.4a"}
189         ,{0x81, "Minix 1.4b ... 1.5.10"}
190         ,{0x82, "Linux swap or Solaris x86"}
191         ,{0x83, "Linux filesystem"}
192         ,{0x84, "OS/2 hidden C: drive"}
193         ,{0x85, "Linux extended"}
194         ,{0x86, "NTFS volume set??"}
195         ,{0x87, "NTFS volume set??"}
196         ,{0x93, "Amoeba filesystem"}
197         ,{0x94, "Amoeba bad block table"}
198         ,{0x9F, "BSD/OS"}
199         ,{0xA0, "Suspend to Disk"}
200         ,{0xA5, "DragonFly/FreeBSD/NetBSD/386BSD"}
201         ,{0xA6, "OpenBSD"}
202         ,{0xA7, "NEXTSTEP"}
203         ,{0xA9, "NetBSD"}
204         ,{0xAC, "IBM JFS"}
205         ,{0xB7, "BSDI BSD/386 filesystem"}
206         ,{0xB8, "BSDI BSD/386 swap"}
207         ,{0xBE, "Solaris x86 boot"}
208         ,{0xC1, "DRDOS/sec with 12-bit FAT"}
209         ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
210         ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
211         ,{0xC7, "Syrinx"}
212         ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
213         ,{0xE1, "Speed"}
214         ,{0xE3, "Speed"}
215         ,{0xE4, "Speed"}
216         ,{0xEB, "BeOS file system"}
217         ,{0xEE, "EFI GPT"}
218         ,{0xEF, "EFI System Partition"}
219         ,{0xF1, "Speed"}
220         ,{0xF2, "DOS 3.3+ Secondary"}
221         ,{0xF4, "Speed"}
222         ,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
223         ,{0xFF, "BBT (Bad Blocks Table)"}
224 };
225
226 static void print_s0(int which);
227 static void print_part(int i);
228 static void init_sector0(unsigned long start);
229 static void init_boot(void);
230 static void change_part(int i);
231 static void print_params();
232 static void change_active(int which);
233 static void change_code();
234 static void get_params_to_use();
235 static void dos(struct dos_partition *partp);
236 static int open_disk(int u_flag);
237 static ssize_t read_disk(off_t sector, void *buf);
238 static ssize_t write_disk(off_t sector, void *buf);
239 static int get_params();
240 static int read_s0();
241 static int write_s0();
242 static int ok(char *str);
243 static int decimal(char *str, int *num, int deflt);
244 static char *get_type(int type);
245 static int read_config(char *config_file);
246 static void reset_boot(void);
247 static int sanitize_partition(struct dos_partition *);
248 static void usage(void);
249 #if 0
250 static int hex(char *str, int *num, int deflt);
251 static int string(char *str, char **ans);
252 #endif
253
254
255 int
256 main(int argc, char *argv[])
257 {
258         int     c, i;
259
260         while ((c = getopt(argc, argv, "BCIab:f:p:istuv1234")) != -1)
261                 switch (c) {
262                 case 'B':
263                         B_flag = 1;
264                         break;
265                 case 'C':
266                         C_flag = 1;
267                         break;
268                 case 'I':
269                         I_flag = 1;
270                         break;
271                 case 'a':
272                         a_flag = 1;
273                         break;
274                 case 'b':
275                         b_flag = optarg;
276                         break;
277                 case 'f':
278                         f_flag = optarg;
279                         break;
280                 case 'p':
281                         disk = optarg;
282                         p_flag = 1;
283                         break;
284                 case 'i':
285                         i_flag = 1;
286                         break;
287                 case 's':
288                         s_flag = 1;
289                         break;
290                 case 't':
291                         t_flag = 1;
292                         break;
293                 case 'u':
294                         u_flag = 1;
295                         break;
296                 case 'v':
297                         v_flag = 1;
298                         break;
299                 case '1':
300                 case '2':
301                 case '3':
302                 case '4':
303                         partition = c - '0';
304                         break;
305                 default:
306                         usage();
307                 }
308         if (f_flag || i_flag)
309                 u_flag = 1;
310         if (t_flag)
311                 v_flag = 1;
312         argc -= optind;
313         argv += optind;
314
315         if (argc > 0) {
316                 static char realname[12];
317
318                 if(strncmp(argv[0], "/dev", 4) == 0) {
319                         disk = argv[0];
320                 } else {
321                         snprintf(realname, 12, "/dev/%s", argv[0]);
322                         disk = realname;
323                 }
324                 
325                 if (open_disk(u_flag) < 0)
326                         err(1, "cannot open disk %s", disk);
327         } else if (disk == NULL) {
328                 int rv = 0;
329
330                 for(i = 0; disks[i]; i++)
331                 {
332                         disk = disks[i];
333                         rv = open_disk(u_flag);
334                         if(rv != -2) break;
335                 }
336                 if(rv < 0)
337                         err(1, "cannot open any disk");
338         } else {
339                 if (open_disk(u_flag) < 0)
340                         err(1, "cannot open disk %s", disk);
341         }
342
343         /* (abu)use mboot.bootinst to probe for the sector size */
344         if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
345                 err(1, "cannot allocate buffer to determine disk sector size");
346         read_disk(0, mboot.bootinst);
347         free(mboot.bootinst);
348         mboot.bootinst = NULL;
349
350         if (s_flag)
351         {
352                 int i;
353                 struct dos_partition *partp;
354
355                 if (read_s0())
356                         err(1, "read_s0");
357                 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
358                     dos_sectors);
359                 printf("Part  %11s %11s Type Flags\n", "Start", "Size");
360                 for (i = 0; i < NDOSPART; i++) {
361                         partp = ((struct dos_partition *) &mboot.parts) + i;
362                         if (partp->dp_start == 0 && partp->dp_size == 0)
363                                 continue;
364                         printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
365                             (u_long) partp->dp_start,
366                             (u_long) partp->dp_size, partp->dp_typ,
367                             partp->dp_flag);
368                 }
369                 exit(0);
370         }
371
372         printf("******* Working on device %s *******\n",disk);
373
374         if (I_flag)
375         {
376                 struct dos_partition *partp;
377
378                 read_s0();
379                 reset_boot();
380                 partp = (struct dos_partition *) (&mboot.parts[0]);
381                 partp->dp_typ = DOSPTYP_386BSD;
382                 partp->dp_flag = ACTIVE;
383                 partp->dp_start = dos_sectors;
384                 if (disksecs - dos_sectors > 0xFFFFFFFFU) {
385                         printf("Warning: Ending logical block > 2TB, using max value\n");
386                         partp->dp_size = 0xFFFFFFFFU;
387                 } else {
388                         partp->dp_size = (disksecs / dos_cylsecs) *
389                                         dos_cylsecs - dos_sectors;
390                 }
391                 dos(partp);
392                 if (v_flag)
393                         print_s0(-1);
394                 if (!t_flag)
395                         write_s0();
396                 exit(0);
397         }
398         if (f_flag)
399         {
400             if (read_s0() || i_flag)
401             {
402                 reset_boot();
403             }
404
405             if (!read_config(f_flag))
406             {
407                 exit(1);
408             }
409             if (v_flag)
410             {
411                 print_s0(-1);
412             }
413             if (!t_flag)
414             {
415                 write_s0();
416             }
417         }
418         else
419         {
420             if(u_flag)
421             {
422                 get_params_to_use();
423             }
424             else
425             {
426                 print_params();
427             }
428
429             if (read_s0())
430                 init_sector0(dos_sectors);
431
432             printf("Media sector size is %d\n", secsize);
433             printf("Warning: BIOS sector numbering starts with sector 1\n");
434             printf("Information from DOS bootblock is:\n");
435             if (partition == -1)
436                 for (i = 1; i <= NDOSPART; i++)
437                     change_part(i);
438             else
439                 change_part(partition);
440
441             if (u_flag || a_flag)
442                 change_active(partition);
443
444             if (B_flag)
445                 change_code();
446
447             if (u_flag || a_flag || B_flag) {
448                 if (!t_flag)    {
449                     printf("\nWe haven't changed the partition table yet.  ");
450                     printf("This is your last chance.\n");
451                 }
452                 print_s0(-1);
453                 if (!t_flag)    {
454                    if (ok("Should we write new partition table?"))
455                         write_s0();
456                 }
457                 else
458                 {
459                     printf("\n-t flag specified -- partition table not written.\n");
460                 }
461             }
462         }
463
464         exit(0);
465 }
466
467 static void
468 usage(void)
469 {
470         fprintf(stderr, "%s%s",
471                 "usage: fdisk [-BCIaistu] [-b bootcode] [-p diskimage] [-1234] [disk]\n",
472                 "       fdisk -f configfile [-itv] [disk]\n");
473         exit(1);
474 }
475
476 static void
477 print_s0(int which)
478 {
479         int     i;
480
481         print_params();
482         printf("Information from DOS bootblock is:\n");
483         if (which == -1)
484                 for (i = 1; i <= NDOSPART; i++)
485                         printf("%d: ", i), print_part(i);
486         else
487                 print_part(which);
488 }
489
490 static struct dos_partition mtpart = { 0 };
491
492 static void
493 print_part(int i)
494 {
495         struct    dos_partition *partp;
496         u_int64_t part_mb;
497
498         partp = ((struct dos_partition *) &mboot.parts) + i - 1;
499
500         if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
501                 printf("<UNUSED>\n");
502                 return;
503         }
504         /*
505          * Be careful not to overflow.
506          */
507         part_mb = partp->dp_size;
508         part_mb *= secsize;
509         part_mb /= (1024 * 1024);
510         printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
511         printf("    start %lu, size %lu (%qd Meg), flag %x%s\n",
512                 (u_long)partp->dp_start,
513                 (u_long)partp->dp_size, 
514                 part_mb,
515                 partp->dp_flag,
516                 partp->dp_flag == ACTIVE ? " (active)" : "");
517         printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
518                 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
519                 ,partp->dp_shd
520                 ,DPSECT(partp->dp_ssect)
521                 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
522                 ,partp->dp_ehd
523                 ,DPSECT(partp->dp_esect));
524 }
525
526
527 static void
528 init_boot(void)
529 {
530         const char *fname;
531         int fd, n;
532         struct stat sb;
533
534         fname = b_flag ? b_flag : "/boot/mbr";
535         if ((fd = open(fname, O_RDONLY)) == -1 ||
536             fstat(fd, &sb) == -1)
537                 err(1, "%s", fname);
538         if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
539                 errx(1, "%s: length must be a multiple of sector size", fname);
540         if (mboot.bootinst != NULL)
541                 free(mboot.bootinst);
542         if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
543                 errx(1, "%s: unable to allocate read buffer", fname);
544         if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
545             close(fd))
546                 err(1, "%s", fname);
547         if (n != mboot.bootinst_size)
548                 errx(1, "%s: short read", fname);
549 }
550
551
552 static void
553 init_sector0(unsigned long start)
554 {
555 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
556
557         init_boot();
558
559         partp->dp_typ = DOSPTYP_386BSD;
560         partp->dp_flag = ACTIVE;
561         start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
562         if(start == 0)
563                 start = dos_sectors;
564         partp->dp_start = start;
565         if (disksecs - start > 0xFFFFFFFFU) {
566                 printf("Warning: Ending logical block > 2TB, using max value\n");
567                 partp->dp_size = 0xFFFFFFFFU;
568         } else {
569                 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
570         }
571
572         dos(partp);
573 }
574
575 static void
576 change_part(int i)
577 {
578 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
579
580     printf("The data for partition %d is:\n", i);
581     print_part(i);
582
583     if (u_flag && ok("Do you want to change it?")) {
584         int tmp;
585
586         if (i_flag) {
587                 bzero((char *)partp, sizeof (struct dos_partition));
588                 if (i == 4) {
589                         init_sector0(1);
590                         printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
591                         print_part(i);
592                 }
593         }
594
595         do {
596                 Decimal("sysid (165=DragonFly)", partp->dp_typ, tmp);
597                 Decimal("start", partp->dp_start, tmp);
598                 Decimal("size", partp->dp_size, tmp);
599                 if (!sanitize_partition(partp)) {
600                         warnx("ERROR: failed to adjust; setting sysid to 0");
601                         partp->dp_typ = 0;
602                 }
603
604                 if (ok("Explicitly specify beg/end address ?"))
605                 {
606                         int     tsec,tcyl,thd;
607                         tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
608                         thd = partp->dp_shd;
609                         tsec = DPSECT(partp->dp_ssect);
610                         Decimal("beginning cylinder", tcyl, tmp);
611                         Decimal("beginning head", thd, tmp);
612                         Decimal("beginning sector", tsec, tmp);
613                         if (tcyl > MAXCYL && C_flag == 0) {
614                                 printf("Warning: starting cylinder wraps, using all 1's\n");
615                                 partp->dp_scyl = -1;
616                                 partp->dp_ssect = -1;
617                                 partp->dp_shd = -1;
618                         } else {
619                                 partp->dp_scyl = DOSCYL(tcyl);
620                                 partp->dp_ssect = DOSSECT(tsec,tcyl);
621                                 partp->dp_shd = thd;
622                         }
623
624                         tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
625                         thd = partp->dp_ehd;
626                         tsec = DPSECT(partp->dp_esect);
627                         Decimal("ending cylinder", tcyl, tmp);
628                         Decimal("ending head", thd, tmp);
629                         Decimal("ending sector", tsec, tmp);
630                         if (tcyl > MAXCYL && C_flag == 0) {
631                                 printf("Warning: ending cylinder wraps, using all 1's\n");
632                                 partp->dp_ecyl = -1;
633                                 partp->dp_esect = -1;
634                                 partp->dp_ehd = -1;
635                         } else {
636                                 partp->dp_ecyl = DOSCYL(tcyl);
637                                 partp->dp_esect = DOSSECT(tsec,tcyl);
638                                 partp->dp_ehd = thd;
639                         }
640                 } else
641                         dos(partp);
642
643                 print_part(i);
644         } while (!ok("Are we happy with this entry?"));
645     }
646 }
647
648 static void
649 print_params(void)
650 {
651         printf("parameters extracted from device are:\n");
652         printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
653                         ,cyls,heads,sectors,cylsecs);
654         if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
655                 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
656         printf("parameters to be used for BIOS calculations are:\n");
657         printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
658                 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
659 }
660
661 static void
662 change_active(int which)
663 {
664         struct dos_partition *partp = &mboot.parts[0];
665         int active, i, new, tmp;
666
667         active = -1;
668         for (i = 0; i < NDOSPART; i++) {
669                 if ((partp[i].dp_flag & ACTIVE) == 0)
670                         continue;
671                 printf("Partition %d is marked active\n", i + 1);
672                 if (active == -1)
673                         active = i + 1;
674         }
675         if (a_flag && which != -1)
676                 active = which;
677         else if (active == -1)
678                 active = 1;
679
680         if (!ok("Do you want to change the active partition?"))
681                 return;
682 setactive:
683         do {
684                 new = active;
685                 Decimal("active partition", new, tmp);
686                 if (new < 1 || new > 4) {
687                         printf("Active partition number must be in range 1-4."
688                                         "  Try again.\n");
689                         goto setactive;
690                 }
691                 active = new;
692         } while (!ok("Are you happy with this choice"));
693         for (i = 0; i < NDOSPART; i++)
694                 partp[i].dp_flag = 0;
695         if (active > 0 && active <= NDOSPART)
696                 partp[active-1].dp_flag = ACTIVE;
697 }
698
699 static void
700 change_code(void)
701 {
702         if (ok("Do you want to change the boot code?"))
703                 init_boot();
704 }
705
706 void
707 get_params_to_use(void)
708 {
709         int     tmp;
710         print_params();
711         if (ok("Do you want to change our idea of what BIOS thinks ?"))
712         {
713                 do
714                 {
715                         Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
716                         Decimal("BIOS's idea of #heads", dos_heads, tmp);
717                         Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
718                         dos_cylsecs = dos_heads * dos_sectors;
719                         print_params();
720                 }
721                 while(!ok("Are you happy with this choice"));
722         }
723 }
724
725
726 /***********************************************\
727 * Change real numbers into strange dos numbers  *
728 \***********************************************/
729 static void
730 dos(struct dos_partition *partp)
731 {
732         int cy, sec;
733         u_int32_t end;
734
735         if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
736                 memcpy(partp, &mtpart, sizeof(*partp));
737                 return;
738         }
739
740         /* Start c/h/s. */
741         cy = partp->dp_start / dos_cylsecs;
742         sec = partp->dp_start % dos_sectors + 1;
743         if (cy > MAXCYL && C_flag == 0) {
744             printf("Warning: starting cylinder wraps, using all 1's\n");
745             partp->dp_shd = -1;
746             partp->dp_scyl = -1;
747             partp->dp_ssect = -1;
748         } else {
749             partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
750             partp->dp_scyl = DOSCYL(cy);
751             partp->dp_ssect = DOSSECT(sec, cy);
752         }
753
754         /* End c/h/s. */
755         end = partp->dp_start + partp->dp_size - 1;
756         cy = end / dos_cylsecs;
757         sec = end % dos_sectors + 1;
758         if (cy > MAXCYL && C_flag == 0) {
759             printf("Warning: ending cylinder wraps, using all 1's\n");
760             partp->dp_ehd = -1;
761             partp->dp_ecyl = -1;
762             partp->dp_esect = -1;
763         } else {
764             partp->dp_ehd = end % dos_cylsecs / dos_sectors;
765             partp->dp_ecyl = DOSCYL(cy);
766             partp->dp_esect = DOSSECT(sec, cy);
767         }
768 }
769
770 int fd;
771
772         /* Getting device status */
773
774 static int
775 open_disk(int u_flag)
776 {
777         struct stat     st;
778
779         if (stat(disk, &st) == -1) {
780                 if (errno == ENOENT)
781                         return -2;
782                 warnx("can't get file status of %s", disk);
783                 return -1;
784         }
785         if ( !(st.st_mode & S_IFCHR) && p_flag == 0 )
786                 warnx("device %s is not character special", disk);
787         if ((fd = open(disk,
788             a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
789                 if(errno == ENXIO)
790                         return -2;
791                 warnx("can't open device %s", disk);
792                 return -1;
793         }
794         if (get_params() == -1) {
795                 warnx("can't get disk parameters on %s", disk);
796                 return -1;
797         }
798         return fd;
799 }
800
801 static ssize_t
802 read_disk(off_t sector, void *buf)
803 {
804         lseek(fd,(sector * 512), 0);
805         if( secsize == 0 )
806                 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
807                         {
808                         /* try the read */
809                         int size = read(fd, buf, secsize);
810                         if( size == secsize )
811                                 /* it worked so return */
812                                 return secsize;
813                         }
814         else
815                 return read( fd, buf, secsize );
816
817         /* we failed to read at any of the sizes */
818         return -1;
819 }
820
821 static ssize_t
822 write_disk(off_t sector, void *buf)
823 {
824         lseek(fd,(sector * 512), 0);
825         /* write out in the size that the read_disk found worked */
826         return write(fd, buf, secsize);
827 }
828
829 static int
830 get_params(void)
831 {
832     struct partinfo partinfo;   /* disk parameters */
833     struct stat st;
834
835     if (ioctl(fd, DIOCGPART, &partinfo) == -1) {
836         if (p_flag && fstat(fd, &st) == 0 && st.st_size) {
837             sectors = 63;
838             heads = 16;
839             cylsecs = heads * sectors;
840             cyls = st.st_size / 512 / cylsecs;
841         } else {
842             warnx("can't get disk parameters on %s; supplying dummy ones",
843                   disk);
844             heads = 1;
845             cylsecs = heads * sectors;
846         }
847     } else {
848         cyls = partinfo.d_ncylinders;
849         heads = partinfo.d_nheads;
850         sectors = partinfo.d_secpertrack;
851         cylsecs = heads * sectors;
852         secsize = partinfo.media_blksize;
853     }
854     dos_cyls = cyls;
855     dos_heads = heads;
856     dos_sectors = sectors;
857     dos_cylsecs = cylsecs;
858     disksecs = (int64_t)cyls * heads * sectors;
859     return (disksecs);
860 }
861 \f
862
863 static int
864 read_s0(void)
865 {
866         mboot.bootinst_size = secsize;
867         if (mboot.bootinst != NULL)
868                 free(mboot.bootinst);
869         if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
870                 warnx("unable to allocate buffer to read fdisk "
871                       "partition table");
872                 return -1;
873         }
874         if (read_disk(0, mboot.bootinst) == -1) {
875                 warnx("can't read fdisk partition table");
876                 return -1;
877         }
878         if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
879                 warnx("invalid fdisk partition table found");
880                 /* So should we initialize things */
881                 return -1;
882         }
883         memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
884         return 0;
885 }
886
887 static int
888 write_s0(void)
889 {
890 #ifdef NOT_NOW
891         int     flag;
892 #endif
893         int     sector;
894
895         if (iotest) {
896                 print_s0(-1);
897                 return 0;
898         }
899         memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
900         /*
901          * write enable label sector before write (if necessary),
902          * disable after writing.
903          * needed if the disklabel protected area also protects
904          * sector 0. (e.g. empty disk)
905          */
906 #ifdef NOT_NOW
907         flag = 1;
908         if (ioctl(fd, DIOCWLABEL, &flag) < 0)
909                 warn("ioctl DIOCWLABEL");
910 #endif
911         for(sector = 0; sector < mboot.bootinst_size / secsize; sector++) 
912                 if (write_disk(sector,
913                                &mboot.bootinst[sector * secsize]) == -1) {
914                         warn("can't write fdisk partition table");
915                         return -1;
916 #ifdef NOT_NOW
917                         flag = 0;
918                         ioctl(fd, DIOCWLABEL, &flag);
919 #endif
920                 }
921 #ifdef NOT_NOW
922         flag = 0;
923         ioctl(fd, DIOCWLABEL, &flag);
924 #endif
925         return(0);
926 }
927
928
929 static int
930 ok(char *str)
931 {
932         printf("%s [n] ", str);
933         fflush(stdout);
934         if (fgets(lbuf, LBUF, stdin) == NULL)
935                 exit(1);
936         lbuf[strlen(lbuf)-1] = 0;
937
938         if (*lbuf &&
939                 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
940                  !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
941                 return 1;
942         else
943                 return 0;
944 }
945
946 static int
947 decimal(char *str, int *num, int deflt)
948 {
949 int acc = 0, c;
950 char *cp;
951
952         while (1) {
953                 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
954                 fflush(stdout);
955                 if (fgets(lbuf, LBUF, stdin) == NULL)
956                         exit(1);
957                 lbuf[strlen(lbuf)-1] = 0;
958
959                 if (!*lbuf)
960                         return 0;
961
962                 cp = lbuf;
963                 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
964                 if (!c)
965                         return 0;
966                 while ((c = *cp++)) {
967                         if (c <= '9' && c >= '0')
968                                 acc = acc * 10 + c - '0';
969                         else
970                                 break;
971                 }
972                 if (c == ' ' || c == '\t')
973                         while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
974                 if (!c) {
975                         *num = acc;
976                         return 1;
977                 } else
978                         printf("%s is an invalid decimal number.  Try again.\n",
979                                 lbuf);
980         }
981
982 }
983
984 #if 0
985 static int
986 hex(char *str, int *num, int deflt)
987 {
988 int acc = 0, c;
989 char *cp;
990
991         while (1) {
992                 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
993                 fgets(lbuf, LBUF, stdin);
994                 lbuf[strlen(lbuf)-1] = 0;
995
996                 if (!*lbuf)
997                         return 0;
998
999                 cp = lbuf;
1000                 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1001                 if (!c)
1002                         return 0;
1003                 while ((c = *cp++)) {
1004                         if (c <= '9' && c >= '0')
1005                                 acc = (acc << 4) + c - '0';
1006                         else if (c <= 'f' && c >= 'a')
1007                                 acc = (acc << 4) + c - 'a' + 10;
1008                         else if (c <= 'F' && c >= 'A')
1009                                 acc = (acc << 4) + c - 'A' + 10;
1010                         else
1011                                 break;
1012                 }
1013                 if (c == ' ' || c == '\t')
1014                         while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1015                 if (!c) {
1016                         *num = acc;
1017                         return 1;
1018                 } else
1019                         printf("%s is an invalid hex number.  Try again.\n",
1020                                 lbuf);
1021         }
1022
1023 }
1024
1025 static int
1026 string(char *str, char **ans)
1027 {
1028 int c;
1029 char *cp = lbuf;
1030
1031         while (1) {
1032                 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
1033                 fgets(lbuf, LBUF, stdin);
1034                 lbuf[strlen(lbuf)-1] = 0;
1035
1036                 if (!*lbuf)
1037                         return 0;
1038
1039                 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1040                 if (c == '"') {
1041                         c = *++cp;
1042                         *ans = cp;
1043                         while ((c = *cp) && c != '"') cp++;
1044                 } else {
1045                         *ans = cp;
1046                         while ((c = *cp) && c != ' ' && c != '\t') cp++;
1047                 }
1048
1049                 if (c)
1050                         *cp = 0;
1051                 return 1;
1052         }
1053 }
1054 #endif
1055
1056 static char *
1057 get_type(int type)
1058 {
1059         int     numentries = (sizeof(part_types)/sizeof(struct part_type));
1060         int     counter = 0;
1061         struct  part_type *ptr = part_types;
1062
1063
1064         while(counter < numentries)
1065         {
1066                 if(ptr->type == type)
1067                 {
1068                         return(ptr->name);
1069                 }
1070                 ptr++;
1071                 counter++;
1072         }
1073         return("unknown");
1074 }
1075
1076
1077 static void
1078 parse_config_line(char *line, CMD *command)
1079 {
1080     char        *cp, *end;
1081
1082     cp = line;
1083     while (1)   /* dirty trick used to insure one exit point for this
1084                    function */
1085     {
1086         memset(command, 0, sizeof(*command));
1087
1088         while (isspace(*cp)) ++cp;
1089         if (*cp == '\0' || *cp == '#')
1090         {
1091             break;
1092         }
1093         command->cmd = *cp++;
1094
1095         /*
1096          * Parse args
1097          */
1098         while (1)
1099         {
1100             while (isspace(*cp)) ++cp;
1101             if (*cp == '#')
1102             {
1103                 break;          /* found comment */
1104             }
1105             if (isalpha(*cp))
1106             {
1107                 command->args[command->n_args].argtype = *cp++;
1108             }
1109             if (!isdigit(*cp))
1110             {
1111                 break;          /* assume end of line */
1112             }
1113             end = NULL;
1114             command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1115             if (cp == end)
1116             {
1117                 break;          /* couldn't parse number */
1118             }
1119             cp = end;
1120             command->n_args++;
1121         }
1122         break;
1123     }
1124 }
1125
1126
1127 static int
1128 process_geometry(CMD *command)
1129 {
1130     int         status = 1, i;
1131
1132     while (1)
1133     {
1134         geom_processed = 1;
1135         if (part_processed)
1136         {
1137             warnx(
1138         "ERROR line %d: the geometry specification line must occur before\n\
1139     all partition specifications",
1140                     current_line_number);
1141             status = 0;
1142             break;
1143         }
1144         if (command->n_args != 3)
1145         {
1146             warnx("ERROR line %d: incorrect number of geometry args",
1147                     current_line_number);
1148             status = 0;
1149             break;
1150         }
1151         dos_cyls = -1;
1152         dos_heads = -1;
1153         dos_sectors = -1;
1154         for (i = 0; i < 3; ++i)
1155         {
1156             switch (command->args[i].argtype)
1157             {
1158             case 'c':
1159                 dos_cyls = command->args[i].arg_val;
1160                 break;
1161             case 'h':
1162                 dos_heads = command->args[i].arg_val;
1163                 break;
1164             case 's':
1165                 dos_sectors = command->args[i].arg_val;
1166                 break;
1167             default:
1168                 warnx(
1169                 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1170                         current_line_number, command->args[i].argtype,
1171                         command->args[i].argtype);
1172                 status = 0;
1173                 break;
1174             }
1175         }
1176         if (status == 0)
1177         {
1178             break;
1179         }
1180
1181         dos_cylsecs = dos_heads * dos_sectors;
1182
1183         /*
1184          * Do sanity checks on parameter values
1185          */
1186         if (dos_cyls < 0)
1187         {
1188             warnx("ERROR line %d: number of cylinders not specified",
1189                     current_line_number);
1190             status = 0;
1191         }
1192         if (dos_cyls == 0 || dos_cyls > 1024)
1193         {
1194             warnx(
1195         "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1196     (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1197     is dedicated to DragonFly)",
1198                     current_line_number, dos_cyls);
1199         }
1200
1201         if (dos_heads < 0)
1202         {
1203             warnx("ERROR line %d: number of heads not specified",
1204                     current_line_number);
1205             status = 0;
1206         }
1207         else if (dos_heads < 1 || dos_heads > 256)
1208         {
1209             warnx("ERROR line %d: number of heads must be within (1-256)",
1210                     current_line_number);
1211             status = 0;
1212         }
1213
1214         if (dos_sectors < 0)
1215         {
1216             warnx("ERROR line %d: number of sectors not specified",
1217                     current_line_number);
1218             status = 0;
1219         }
1220         else if (dos_sectors < 1 || dos_sectors > 63)
1221         {
1222             warnx("ERROR line %d: number of sectors must be within (1-63)",
1223                     current_line_number);
1224             status = 0;
1225         }
1226
1227         break;
1228     }
1229     return (status);
1230 }
1231
1232
1233 static int
1234 process_partition(CMD *command)
1235 {
1236     int                         status = 0, partition;
1237     u_int32_t                   prev_head_boundary, prev_cyl_boundary;
1238     u_int32_t                   adj_size, max_end;
1239     struct dos_partition        *partp;
1240
1241     while (1)
1242     {
1243         part_processed = 1;
1244         if (command->n_args != 4)
1245         {
1246             warnx("ERROR line %d: incorrect number of partition args",
1247                     current_line_number);
1248             break;
1249         }
1250         partition = command->args[0].arg_val;
1251         if (partition < 1 || partition > 4)
1252         {
1253             warnx("ERROR line %d: invalid partition number %d",
1254                     current_line_number, partition);
1255             break;
1256         }
1257         partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1258         bzero((char *)partp, sizeof (struct dos_partition));
1259         partp->dp_typ = command->args[1].arg_val;
1260         partp->dp_start = command->args[2].arg_val;
1261         partp->dp_size = command->args[3].arg_val;
1262         max_end = partp->dp_start + partp->dp_size;
1263
1264         if (partp->dp_typ == 0)
1265         {
1266             /*
1267              * Get out, the partition is marked as unused.
1268              */
1269             /*
1270              * Insure that it's unused.
1271              */
1272             bzero((char *)partp, sizeof (struct dos_partition));
1273             status = 1;
1274             break;
1275         }
1276
1277         /*
1278          * Adjust start upwards, if necessary, to fall on an head boundary.
1279          */
1280         if (partp->dp_start % dos_sectors != 0)
1281         {
1282             prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1283             if (max_end < dos_sectors ||
1284                 prev_head_boundary > max_end - dos_sectors)
1285             {
1286                 /*
1287                  * Can't go past end of partition
1288                  */
1289                 warnx(
1290         "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1291     a head boundary",
1292                         current_line_number, partition);
1293                 break;
1294             }
1295             warnx(
1296         "WARNING: adjusting start offset of partition %d\n\
1297     from %u to %u, to fall on a head boundary",
1298                     partition, (u_int)partp->dp_start,
1299                     (u_int)(prev_head_boundary + dos_sectors));
1300             partp->dp_start = prev_head_boundary + dos_sectors;
1301         }
1302
1303         /*
1304          * Adjust size downwards, if necessary, to fall on a cylinder
1305          * boundary.
1306          */
1307         prev_cyl_boundary =
1308             ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1309         if (prev_cyl_boundary > partp->dp_start)
1310             adj_size = prev_cyl_boundary - partp->dp_start;
1311         else
1312         {
1313             warnx(
1314         "ERROR: could not adjust partition to start on a head boundary\n\
1315     and end on a cylinder boundary.");
1316             return (0);
1317         }
1318         if (adj_size != partp->dp_size)
1319         {
1320             warnx(
1321         "WARNING: adjusting size of partition %d from %u to %u\n\
1322     to end on a cylinder boundary",
1323                     partition, (u_int)partp->dp_size, (u_int)adj_size);
1324             partp->dp_size = adj_size;
1325         }
1326         if (partp->dp_size == 0)
1327         {
1328             warnx("ERROR line %d: size of partition %d is zero",
1329                     current_line_number, partition);
1330             break;
1331         }
1332
1333         dos(partp);
1334         status = 1;
1335         break;
1336     }
1337     return (status);
1338 }
1339
1340
1341 static int
1342 process_active(CMD *command)
1343 {
1344     int                         status = 0, partition, i;
1345     struct dos_partition        *partp;
1346
1347     while (1)
1348     {
1349         active_processed = 1;
1350         if (command->n_args != 1)
1351         {
1352             warnx("ERROR line %d: incorrect number of active args",
1353                     current_line_number);
1354             status = 0;
1355             break;
1356         }
1357         partition = command->args[0].arg_val;
1358         if (partition < 1 || partition > 4)
1359         {
1360             warnx("ERROR line %d: invalid partition number %d",
1361                     current_line_number, partition);
1362             break;
1363         }
1364         /*
1365          * Reset active partition
1366          */
1367         partp = ((struct dos_partition *) &mboot.parts);
1368         for (i = 0; i < NDOSPART; i++)
1369             partp[i].dp_flag = 0;
1370         partp[partition-1].dp_flag = ACTIVE;
1371
1372         status = 1;
1373         break;
1374     }
1375     return (status);
1376 }
1377
1378
1379 static int
1380 process_line(char *line)
1381 {
1382     CMD         command;
1383     int         status = 1;
1384
1385     while (1)
1386     {
1387         parse_config_line(line, &command);
1388         switch (command.cmd)
1389         {
1390         case 0:
1391             /*
1392              * Comment or blank line
1393              */
1394             break;
1395         case 'g':
1396             /*
1397              * Set geometry
1398              */
1399             status = process_geometry(&command);
1400             break;
1401         case 'p':
1402             status = process_partition(&command);
1403             break;
1404         case 'a':
1405             status = process_active(&command);
1406             break;
1407         default:
1408             status = 0;
1409             break;
1410         }
1411         break;
1412     }
1413     return (status);
1414 }
1415
1416
1417 static int
1418 read_config(char *config_file)
1419 {
1420     FILE        *fp = NULL;
1421     int         status = 1;
1422     char        buf[1010];
1423
1424     while (1)   /* dirty trick used to insure one exit point for this
1425                    function */
1426     {
1427         if (strcmp(config_file, "-") != 0)
1428         {
1429             /*
1430              * We're not reading from stdin
1431              */
1432             if ((fp = fopen(config_file, "r")) == NULL)
1433             {
1434                 status = 0;
1435                 break;
1436             }
1437         }
1438         else
1439         {
1440             fp = stdin;
1441         }
1442         current_line_number = 0;
1443         while (!feof(fp))
1444         {
1445             if (fgets(buf, sizeof(buf), fp) == NULL)
1446             {
1447                 break;
1448             }
1449             ++current_line_number;
1450             status = process_line(buf);
1451             if (status == 0)
1452             {
1453                 break;
1454             }
1455         }
1456         break;
1457     }
1458     if (fp)
1459     {
1460         /*
1461          * It doesn't matter if we're reading from stdin, as we've reached EOF
1462          */
1463         fclose(fp);
1464     }
1465     return (status);
1466 }
1467
1468
1469 static void
1470 reset_boot(void)
1471 {
1472     int                         i;
1473     struct dos_partition        *partp;
1474
1475     init_boot();
1476     for (i = 0; i < 4; ++i)
1477     {
1478         partp = ((struct dos_partition *) &mboot.parts) + i;
1479         bzero((char *)partp, sizeof (struct dos_partition));
1480     }
1481 }
1482
1483 static int
1484 sanitize_partition(struct dos_partition *partp)
1485 {
1486     u_int32_t                   prev_head_boundary, prev_cyl_boundary;
1487     u_int32_t                   max_end, size, start;
1488
1489     start = partp->dp_start;
1490     size = partp->dp_size;
1491     max_end = start + size;
1492     /* Only allow a zero size if the partition is being marked unused. */
1493     if (size == 0) {
1494         if (start == 0 && partp->dp_typ == 0)
1495             return (1);
1496         warnx("ERROR: size of partition is zero");
1497         return (0);
1498     }
1499     /* Return if no adjustment is necessary. */
1500     if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1501         return (1);
1502
1503     if (start == 0) {
1504             warnx("WARNING: partition overlaps with partition table");
1505             if (ok("Correct this automatically?"))
1506                     start = dos_sectors;
1507     }
1508     if (start % dos_sectors != 0)
1509         warnx("WARNING: partition does not start on a head boundary");
1510     if ((start  +size) % dos_sectors != 0)
1511         warnx("WARNING: partition does not end on a cylinder boundary");
1512     warnx("WARNING: this may confuse the BIOS or some operating systems");
1513     if (!ok("Correct this automatically?"))
1514         return (1);
1515
1516     /*
1517      * Adjust start upwards, if necessary, to fall on an head boundary.
1518      */
1519     if (start % dos_sectors != 0) {
1520         prev_head_boundary = start / dos_sectors * dos_sectors;
1521         if (max_end < dos_sectors ||
1522             prev_head_boundary >= max_end - dos_sectors) {
1523             /*
1524              * Can't go past end of partition
1525              */
1526             warnx(
1527     "ERROR: unable to adjust start of partition to fall on a head boundary");
1528             return (0);
1529         }
1530         start = prev_head_boundary + dos_sectors;
1531     }
1532
1533     /*
1534      * Adjust size downwards, if necessary, to fall on a cylinder
1535      * boundary.
1536      */
1537     prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1538     if (prev_cyl_boundary > start)
1539         size = prev_cyl_boundary - start;
1540     else {
1541         warnx("ERROR: could not adjust partition to start on a head boundary\n\
1542     and end on a cylinder boundary.");
1543         return (0);
1544     }
1545
1546     /* Finally, commit any changes to partp and return. */
1547     if (start != partp->dp_start) {
1548         warnx("WARNING: adjusting start offset of partition to %u",
1549             (u_int)start);
1550         partp->dp_start = start;
1551     }
1552     if (size != partp->dp_size) {
1553         warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1554         partp->dp_size = size;
1555     }
1556
1557     return (1);
1558 }