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