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