2 * Mach Operating System
3 * Copyright (c) 1992 Carnegie Mellon University
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.
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.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/disklabel.h>
32 #include <sys/diskmbr.h>
33 #include <sys/endian.h>
34 #include <sys/param.h>
36 #include <sys/mount.h>
52 #define NO_DISK_SECTORS ((u_int32_t)-1)
53 #define NO_TRACK_CYLINDERS 1023
54 #define NO_TRACK_HEADS 255
55 #define NO_TRACK_SECTORS 63
57 static char lbuf[LBUF];
61 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
63 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
64 * Copyright (c) 1989 Robert. V. Baron
68 #define Decimal(str, ans, tmp, maxval) if (decimal(str, &tmp, ans, maxval)) ans = tmp
70 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
71 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
72 static int secsize = 0; /* the sensed sector size */
76 static int cyls, sectors, heads, cylsecs;
77 static u_int32_t disksecs;
80 unsigned char *bootinst; /* boot code */
82 struct dos_partition parts[NDOSPART];
85 static struct mboot mboot;
91 static uint dos_heads;
92 static uint dos_sectors;
93 static uint dos_cylsecs;
95 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
96 #define DOSCYL(c) (c & 0xff)
100 static int current_line_number;
102 static int geom_processed = 0;
103 static int part_processed = 0;
104 static int active_processed = 0;
111 unsigned long arg_val;
116 static int B_flag = 0; /* replace boot code */
117 static int I_flag = 0; /* use entire disk for FreeBSD */
118 static int a_flag = 0; /* set active partition */
119 static char *b_flag = NULL; /* path to boot code */
120 static int i_flag = 0; /* replace partition data */
121 static int q_flag = 0; /* Be quiet */
122 static int u_flag = 0; /* update partition data */
123 static int s_flag = 0; /* Print a summary and exit */
124 static int t_flag = 0; /* test only */
125 static char *f_flag = NULL; /* Read config info from file */
126 static int v_flag = 0; /* Be verbose */
127 static int print_config_flag = 0;
130 * A list of partition types, probably outdated.
132 static const char *const part_types[256] = {
134 [0x01] = "Primary DOS with 12 bit FAT",
135 [0x02] = "XENIX / file system",
136 [0x03] = "XENIX /usr file system",
137 [0x04] = "Primary DOS with 16 bit FAT (< 32MB)",
138 [0x05] = "Extended DOS",
139 [0x06] = "Primary DOS, 16 bit FAT (>= 32MB)",
140 [0x07] = "NTFS, OS/2 HPFS, QNX-2 (16 bit) or Advanced UNIX",
141 [0x08] = "AIX file system or SplitDrive",
142 [0x09] = "AIX boot partition or Coherent",
143 [0x0A] = "OS/2 Boot Manager, OPUS or Coherent swap",
144 [0x0B] = "DOS or Windows 95 with 32 bit FAT",
145 [0x0C] = "DOS or Windows 95 with 32 bit FAT (LBA)",
146 [0x0E] = "Primary 'big' DOS (>= 32MB, LBA)",
147 [0x0F] = "Extended DOS (LBA)",
149 [0x11] = "OS/2 BM: hidden DOS with 12-bit FAT",
150 [0x12] = "Compaq diagnostics",
151 [0x14] = "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)",
152 [0x16] = "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)",
153 [0x17] = "OS/2 BM: hidden IFS (e.g. HPFS)",
154 [0x18] = "AST Windows swapfile",
155 [0x1b] = "ASUS Recovery partition (NTFS)",
157 [0x3C] = "PartitionMagic recovery",
159 [0x40] = "VENIX 286",
160 [0x41] = "Linux/MINIX (sharing disk with DRDOS)",
161 [0x42] = "SFS or Linux swap (sharing disk with DRDOS)",
162 [0x43] = "Linux native (sharing disk with DRDOS)",
163 [0x4D] = "QNX 4.2 Primary",
164 [0x4E] = "QNX 4.2 Secondary",
165 [0x4F] = "QNX 4.2 Tertiary",
166 [0x50] = "DM (disk manager)",
167 [0x51] = "DM6 Aux1 (or Novell)",
168 [0x52] = "CP/M or Microport SysV/AT",
171 [0x55] = "EZ-Drive (disk manager)",
172 [0x56] = "Golden Bow (disk manager)",
173 [0x5c] = "Priam Edisk (disk manager)", /* according to S. Widlake */
174 [0x61] = "SpeedStor",
175 [0x63] = "System V/386 (such as ISC UNIX), GNU HURD or Mach",
176 [0x64] = "Novell Netware/286 2.xx",
177 [0x65] = "Novell Netware/386 3.xx",
178 [0x70] = "DiskSecure Multi-Boot",
181 [0x78] = "QNX4.x 2nd part",
182 [0x79] = "QNX4.x 3rd part",
183 [0x80] = "Minix until 1.4a",
184 [0x81] = "Minix since 1.4b, early Linux partition or Mitac disk manager",
185 [0x82] = "Linux swap or Solaris x86",
186 [0x83] = "Linux native",
187 [0x84] = "OS/2 hidden C: drive",
188 [0x85] = "Linux extended",
189 [0x86] = "NTFS volume set??",
190 [0x87] = "NTFS volume set??",
191 [0x93] = "Amoeba file system",
192 [0x94] = "Amoeba bad block table",
194 [0xA0] = "Suspend to Disk",
195 [0xA5] = "FreeBSD/NetBSD/386BSD",
201 [0xB7] = "BSDI BSD/386 file system",
202 [0xB8] = "BSDI BSD/386 swap",
203 [0xBE] = "Solaris x86 boot",
204 [0xBF] = "Solaris x86 (new)",
205 [0xC1] = "DRDOS/sec with 12-bit FAT",
206 [0xC4] = "DRDOS/sec with 16-bit FAT (< 32MB)",
207 [0xC6] = "DRDOS/sec with 16-bit FAT (>= 32MB)",
209 [0xDB] = "CP/M, Concurrent CP/M, Concurrent DOS or CTOS",
210 [0xDE] = "DELL Utilities - FAT filesystem",
211 [0xE1] = "DOS access or SpeedStor with 12-bit FAT extended partition",
212 [0xE3] = "DOS R/O or SpeedStor",
213 [0xE4] = "SpeedStor with 16-bit FAT extended partition < 1024 cyl.",
214 [0xEB] = "BeOS file system",
216 [0xEF] = "EFI System Partition",
217 [0xF1] = "SpeedStor",
218 [0xF2] = "DOS 3.3+ Secondary",
219 [0xF4] = "SpeedStor large partition",
220 [0xFB] = "VMware VMFS",
221 [0xFE] = "SpeedStor >1024 cyl. or LANstep",
222 [0xFF] = "Xenix bad blocks table",
230 ret = (t >= 0 && t <= 255) ? part_types[t] : NULL;
231 return ret ? ret : "unknown";
235 static int geom_class_available(const char *);
236 static void print_s0(void);
237 static void print_part(const struct dos_partition *);
238 static void init_sector0(unsigned long start);
239 static void init_boot(void);
240 static void change_part(int i);
241 static void print_params(void);
242 static void change_active(int which);
243 static void change_code(void);
244 static void get_params_to_use(void);
245 static char *get_rootdisk(void);
246 static void dos(struct dos_partition *partp);
247 static int open_disk(int flag);
248 static ssize_t read_disk(off_t sector, void *buf);
249 static int write_disk(off_t sector, void *buf);
250 static int get_params(void);
251 static int read_s0(void);
252 static int write_s0(void);
253 static int ok(const char *str);
254 static int decimal(const char *str, int *num, int deflt, uint32_t maxval);
255 static int read_config(char *config_file);
256 static void reset_boot(void);
257 static int sanitize_partition(struct dos_partition *);
258 static void usage(void);
261 main(int argc, char *argv[])
265 struct dos_partition *partp;
267 while ((c = getopt(argc, argv, "BIab:f:ipqstuv1234")) != -1)
288 print_config_flag = 1;
314 if (f_flag || i_flag)
322 disk = get_rootdisk();
324 disk = g_device_path(argv[0]);
326 err(1, "unable to get correct path for %s", argv[0]);
328 if (open_disk(u_flag) < 0)
329 err(1, "cannot open disk %s", disk);
331 /* (abu)use mboot.bootinst to probe for the sector size */
332 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
333 err(1, "cannot allocate buffer to determine disk sector size");
334 if (read_disk(0, mboot.bootinst) == -1)
335 errx(1, "could not detect sector size");
336 free(mboot.bootinst);
337 mboot.bootinst = NULL;
339 if (print_config_flag) {
343 printf("# %s\n", disk);
344 printf("g c%d h%d s%d\n", dos_cyls, dos_heads, dos_sectors);
346 for (i = 0; i < NDOSPART; i++) {
347 partp = &mboot.parts[i];
349 if (partp->dp_start == 0 && partp->dp_size == 0)
352 printf("p %d 0x%02x %lu %lu\n", i + 1, partp->dp_typ,
353 (u_long)partp->dp_start, (u_long)partp->dp_size);
355 /* Fill flags for the partition. */
356 if (partp->dp_flag & 0x80)
357 printf("a %d\n", i + 1);
364 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
366 printf("Part %11s %11s Type Flags\n", "Start", "Size");
367 for (i = 0; i < NDOSPART; i++) {
368 partp = &mboot.parts[i];
369 if (partp->dp_start == 0 && partp->dp_size == 0)
371 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
372 (u_long) partp->dp_start,
373 (u_long) partp->dp_size, partp->dp_typ,
379 printf("******* Working on device %s *******\n",disk);
384 partp = &mboot.parts[0];
385 partp->dp_typ = DOSPTYP_386BSD;
386 partp->dp_flag = ACTIVE;
387 partp->dp_start = dos_sectors;
388 partp->dp_size = rounddown(disksecs, dos_cylsecs) -
398 if (read_s0() || i_flag)
400 if (!read_config(f_flag))
413 init_sector0(dos_sectors);
415 printf("Media sector size is %d\n", secsize);
416 printf("Warning: BIOS sector numbering starts with sector 1\n");
417 printf("Information from DOS bootblock is:\n");
419 for (i = 1; i <= NDOSPART; i++)
422 change_part(partition);
424 if (u_flag || a_flag)
425 change_active(partition);
430 if (u_flag || a_flag || B_flag) {
432 printf("\nWe haven't changed the partition table yet. ");
433 printf("This is your last chance.\n");
437 if (ok("Should we write new partition table?"))
440 printf("\n-t flag specified -- partition table not written.\n");
451 fprintf(stderr, "%s%s",
452 "usage: fdisk [-BIaipqstu] [-b bootcode] [-1234] [disk]\n",
453 " fdisk -f configfile [-itv] [disk]\n");
463 printf("Information from DOS bootblock is:\n");
464 for (i = 1; i <= NDOSPART; i++) {
466 print_part(&mboot.parts[i - 1]);
470 static struct dos_partition mtpart;
473 print_part(const struct dos_partition *partp)
477 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
478 printf("<UNUSED>\n");
482 * Be careful not to overflow.
484 part_mb = partp->dp_size;
486 part_mb /= (1024 * 1024);
487 printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
488 get_type(partp->dp_typ));
489 printf(" start %lu, size %lu (%ju Meg), flag %x%s\n",
490 (u_long)partp->dp_start,
491 (u_long)partp->dp_size,
494 partp->dp_flag == ACTIVE ? " (active)" : "");
495 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
496 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
498 ,DPSECT(partp->dp_ssect)
499 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
501 ,DPSECT(partp->dp_esect));
512 fname = b_flag ? b_flag : "/boot/mbr";
513 if ((fdesc = open(fname, O_RDONLY)) == -1 ||
514 fstat(fdesc, &sb) == -1)
517 errx(1, "%s is empty, must not be.", fname);
518 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
519 errx(1, "%s: length must be a multiple of sector size", fname);
520 if (mboot.bootinst != NULL)
521 free(mboot.bootinst);
522 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
523 errx(1, "%s: unable to allocate read buffer", fname);
524 if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
527 if (n != mboot.bootinst_size)
528 errx(1, "%s: short read", fname);
533 init_sector0(unsigned long start)
535 struct dos_partition *partp = &mboot.parts[0];
539 partp->dp_typ = DOSPTYP_386BSD;
540 partp->dp_flag = ACTIVE;
541 start = roundup(start, dos_sectors);
544 partp->dp_start = start;
545 partp->dp_size = rounddown(disksecs, dos_cylsecs) - start;
553 struct dos_partition *partp = &mboot.parts[i - 1];
555 printf("The data for partition %d is:\n", i);
558 if (u_flag && ok("Do you want to change it?")) {
562 bzero(partp, sizeof (*partp));
565 printf("\nThe static data for the slice 1 has been reinitialized to:\n");
571 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp, 255);
572 Decimal("start", partp->dp_start, tmp, NO_DISK_SECTORS);
573 Decimal("size", partp->dp_size, tmp, NO_DISK_SECTORS);
574 if (!sanitize_partition(partp)) {
575 warnx("ERROR: failed to adjust; setting sysid to 0");
579 if (ok("Explicitly specify beg/end address ?"))
582 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
584 tsec = DPSECT(partp->dp_ssect);
585 Decimal("beginning cylinder", tcyl, tmp, NO_TRACK_CYLINDERS);
586 Decimal("beginning head", thd, tmp, NO_TRACK_HEADS);
587 Decimal("beginning sector", tsec, tmp, NO_TRACK_SECTORS);
588 partp->dp_scyl = DOSCYL(tcyl);
589 partp->dp_ssect = DOSSECT(tsec,tcyl);
592 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
594 tsec = DPSECT(partp->dp_esect);
595 Decimal("ending cylinder", tcyl, tmp, NO_TRACK_CYLINDERS);
596 Decimal("ending head", thd, tmp, NO_TRACK_HEADS);
597 Decimal("ending sector", tsec, tmp, NO_TRACK_SECTORS);
598 partp->dp_ecyl = DOSCYL(tcyl);
599 partp->dp_esect = DOSSECT(tsec,tcyl);
605 } while (!ok("Are we happy with this entry?"));
612 printf("parameters extracted from in-core disklabel are:\n");
613 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
614 ,cyls,heads,sectors,cylsecs);
615 if (dos_cyls > 1023 || dos_heads > 255 || dos_sectors > 63)
616 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
617 printf("parameters to be used for BIOS calculations are:\n");
618 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
619 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
623 change_active(int which)
625 struct dos_partition *partp = &mboot.parts[0];
626 int active, i, new, tmp;
629 for (i = 0; i < NDOSPART; i++) {
630 if ((partp[i].dp_flag & ACTIVE) == 0)
632 printf("Partition %d is marked active\n", i + 1);
636 if (a_flag && which != -1)
638 else if (active == -1)
641 if (!ok("Do you want to change the active partition?"))
646 Decimal("active partition", new, tmp, 0);
647 if (new < 1 || new > 4) {
648 printf("Active partition number must be in range 1-4."
653 } while (!ok("Are you happy with this choice"));
654 for (i = 0; i < NDOSPART; i++)
655 partp[i].dp_flag = 0;
656 if (active > 0 && active <= NDOSPART)
657 partp[active-1].dp_flag = ACTIVE;
663 if (ok("Do you want to change the boot code?"))
672 if (ok("Do you want to change our idea of what BIOS thinks ?"))
676 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp, 0);
677 Decimal("BIOS's idea of #heads", dos_heads, tmp, 0);
678 Decimal("BIOS's idea of #sectors", dos_sectors, tmp, 0);
679 dos_cylsecs = dos_heads * dos_sectors;
682 while(!ok("Are you happy with this choice"));
687 /***********************************************\
688 * Change real numbers into strange dos numbers *
689 \***********************************************/
691 dos(struct dos_partition *partp)
696 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
697 memcpy(partp, &mtpart, sizeof(*partp));
702 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
703 cy = partp->dp_start / dos_cylsecs;
704 sec = partp->dp_start % dos_sectors + 1;
705 partp->dp_scyl = DOSCYL(cy);
706 partp->dp_ssect = DOSSECT(sec, cy);
709 end = partp->dp_start + partp->dp_size - 1;
710 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
711 cy = end / dos_cylsecs;
712 sec = end % dos_sectors + 1;
713 partp->dp_ecyl = DOSCYL(cy);
714 partp->dp_esect = DOSSECT(sec, cy);
722 /* Write mode if one of these flags are set. */
723 rwmode = (a_flag || I_flag || B_flag || flag);
724 fd = g_open(disk, rwmode);
725 /* If the mode fails, try read-only if we didn't. */
726 if (fd == -1 && errno == EPERM && rwmode)
727 fd = g_open(disk, 0);
728 if (fd == -1 && errno == ENXIO)
731 warnx("can't open device %s", disk);
734 if (get_params() == -1) {
735 warnx("can't get disk parameters on %s", disk);
742 read_disk(off_t sector, void *buf)
745 lseek(fd, (sector * 512), 0);
747 for (secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE;
750 int size = read(fd, buf, secsize);
752 /* it worked so return */
756 return read(fd, buf, secsize);
758 /* we failed to read at any of the sizes */
763 geom_class_available(const char *name)
765 struct gclass *class;
769 error = geom_gettree(&mesh);
771 errc(1, error, "Cannot get GEOM tree");
773 LIST_FOREACH(class, &mesh.lg_class, lg_class) {
774 if (strcmp(class->lg_name, name) == 0) {
775 geom_deletetree(&mesh);
780 geom_deletetree(&mesh);
786 write_disk(off_t sector, void *buf)
788 struct gctl_req *grq;
793 /* Check that GEOM_MBR is available */
794 if (geom_class_available("MBR") != 0) {
795 grq = gctl_get_handle();
796 gctl_ro_param(grq, "verb", -1, "write MBR");
797 gctl_ro_param(grq, "class", -1, "MBR");
798 pname = g_providername(fd);
800 warn("Error getting providername for %s", disk);
803 gctl_ro_param(grq, "geom", -1, pname);
804 gctl_ro_param(grq, "data", secsize, buf);
805 errmsg = gctl_issue(grq);
807 if (errmsg == NULL) {
812 warnx("GEOM_MBR: %s", errmsg);
816 * Try to write MBR directly. This may help when disk
818 * XXX: hardcoded sectorsize
820 error = pwrite(fd, buf, secsize, (sector * 512));
821 if (error == secsize)
826 * GEOM_MBR is not available or failed to write MBR.
827 * Now check that we have GEOM_PART and recommend to use gpart (8).
829 if (geom_class_available("PART") != 0)
830 warnx("Failed to write MBR. Try to use gpart(8).");
832 warnx("Failed to write sector zero");
843 error = ioctl(fd, DIOCGFWSECTORS, &u);
845 sectors = dos_sectors = u;
847 sectors = dos_sectors = 63;
849 error = ioctl(fd, DIOCGFWHEADS, &u);
851 heads = dos_heads = u;
853 heads = dos_heads = 255;
855 dos_cylsecs = cylsecs = heads * sectors;
856 disksecs = cyls * heads * sectors;
858 u = g_sectorsize(fd);
865 if (o / u <= NO_DISK_SECTORS)
868 disksecs = NO_DISK_SECTORS;
869 cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
879 mboot.bootinst_size = secsize;
880 if (mboot.bootinst != NULL)
881 free(mboot.bootinst);
882 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
883 warnx("unable to allocate buffer to read fdisk "
887 if (read_disk(0, mboot.bootinst) == -1) {
888 warnx("can't read fdisk partition table");
891 if (le16dec(&mboot.bootinst[DOSMAGICOFFSET]) != DOSMAGIC) {
892 warnx("invalid fdisk partition table found");
893 /* So should we initialize things */
896 for (i = 0; i < NDOSPART; i++)
898 &mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
912 for(i = 0; i < NDOSPART; i++)
913 dos_partition_enc(&mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
915 le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
916 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
917 if (write_disk(sector,
918 &mboot.bootinst[sector * secsize]) == -1) {
919 warn("can't write fdisk partition table");
929 printf("%s [n] ", str);
931 if (fgets(lbuf, LBUF, stdin) == NULL)
933 lbuf[strlen(lbuf)-1] = 0;
936 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
937 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
944 decimal(const char *str, int *num, int deflt, uint32_t maxval)
952 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
954 if (fgets(lbuf, LBUF, stdin) == NULL)
956 lbuf[strlen(lbuf)-1] = 0;
962 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
965 while ((c = *cp++)) {
966 if (c <= '9' && c >= '0') {
967 if (acc <= maxval || maxval == 0)
968 acc = acc * 10 + c - '0';
972 if (c == ' ' || c == '\t')
973 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
975 if (maxval > 0 && acc > maxval) {
977 printf("%s exceeds maximum value allowed for "
978 "this field. The value has been reduced "
979 "to %lld\n", lbuf, acc);
984 printf("%s is an invalid decimal number. Try again.\n",
991 parse_config_line(char *line, CMD *command)
997 memset(command, 0, sizeof(*command));
999 while (isspace(*cp)) ++cp;
1000 if (*cp == '\0' || *cp == '#')
1002 command->cmd = *cp++;
1008 while (isspace(*cp)) ++cp;
1012 break; /* found comment */
1014 command->args[command->n_args].argtype = *cp++;
1016 command->args[command->n_args].arg_val = strtoul(cp, &end, 0);
1017 if (cp == end || (!isspace(*end) && *end != '\0')) {
1020 while (!isspace(*end) && *end != '\0') ++end;
1021 ch = *end; *end = '\0';
1022 command->args[command->n_args].arg_str = strdup(cp);
1025 command->args[command->n_args].arg_str = NULL;
1035 process_geometry(CMD *command)
1041 if (part_processed) {
1043 "ERROR line %d: the geometry specification line must occur before\n\
1044 all partition specifications",
1045 current_line_number);
1049 if (command->n_args != 3) {
1050 warnx("ERROR line %d: incorrect number of geometry args",
1051 current_line_number);
1058 for (i = 0; i < 3; ++i) {
1059 switch (command->args[i].argtype) {
1061 dos_cyls = command->args[i].arg_val;
1064 dos_heads = command->args[i].arg_val;
1067 dos_sectors = command->args[i].arg_val;
1071 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1072 current_line_number, command->args[i].argtype,
1073 command->args[i].argtype);
1081 dos_cylsecs = dos_heads * dos_sectors;
1084 * Do sanity checks on parameter values
1086 if (dos_cyls == 0) {
1087 warnx("ERROR line %d: number of cylinders not specified",
1088 current_line_number);
1091 if (dos_cyls > 1024) {
1093 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1094 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1095 is dedicated to FreeBSD)",
1096 current_line_number, dos_cyls);
1099 if (dos_heads == 0) {
1100 warnx("ERROR line %d: number of heads not specified",
1101 current_line_number);
1103 } else if (dos_heads > 256) {
1104 warnx("ERROR line %d: number of heads must be within (1-256)",
1105 current_line_number);
1109 if (dos_sectors == 0) {
1110 warnx("ERROR line %d: number of sectors not specified",
1111 current_line_number);
1113 } else if (dos_sectors > 63) {
1114 warnx("ERROR line %d: number of sectors must be within (1-63)",
1115 current_line_number);
1125 str2sectors(const char *str)
1130 val = strtoul(str, &end, 0);
1131 if (str == end || *end == '\0') {
1132 warnx("ERROR line %d: unexpected size: \'%s\'",
1133 current_line_number, str);
1134 return NO_DISK_SECTORS;
1138 val *= 1024UL / secsize;
1139 else if (*end == 'M')
1140 val *= 1024UL * 1024UL / secsize;
1141 else if (*end == 'G')
1142 val *= 1024UL * 1024UL * 1024UL / secsize;
1144 warnx("ERROR line %d: unexpected modifier: %c "
1145 "(not K/M/G)", current_line_number, *end);
1146 return NO_DISK_SECTORS;
1153 process_partition(CMD *command)
1155 int status = 0, partition;
1156 u_int32_t prev_head_boundary, prev_cyl_boundary;
1157 u_int32_t adj_size, max_end;
1158 struct dos_partition *partp;
1162 if (command->n_args != 4) {
1163 warnx("ERROR line %d: incorrect number of partition args",
1164 current_line_number);
1167 partition = command->args[0].arg_val;
1168 if (partition < 1 || partition > 4) {
1169 warnx("ERROR line %d: invalid partition number %d",
1170 current_line_number, partition);
1173 partp = &mboot.parts[partition - 1];
1174 bzero(partp, sizeof (*partp));
1175 partp->dp_typ = command->args[1].arg_val;
1176 if (command->args[2].arg_str != NULL) {
1177 if (strcmp(command->args[2].arg_str, "*") == 0) {
1179 partp->dp_start = dos_sectors;
1180 for (i = 1; i < partition; i++) {
1181 struct dos_partition *prev_partp;
1182 prev_partp = ((struct dos_partition *)
1183 &mboot.parts) + i - 1;
1184 if (prev_partp->dp_typ != 0)
1185 partp->dp_start = prev_partp->dp_start +
1186 prev_partp->dp_size;
1188 if (partp->dp_start % dos_sectors != 0) {
1189 prev_head_boundary = rounddown(partp->dp_start,
1191 partp->dp_start = prev_head_boundary +
1195 partp->dp_start = str2sectors(command->args[2].arg_str);
1196 if (partp->dp_start == NO_DISK_SECTORS)
1200 partp->dp_start = command->args[2].arg_val;
1202 if (command->args[3].arg_str != NULL) {
1203 if (strcmp(command->args[3].arg_str, "*") == 0)
1204 partp->dp_size = rounddown(disksecs, dos_cylsecs) -
1207 partp->dp_size = str2sectors(command->args[3].arg_str);
1208 if (partp->dp_size == NO_DISK_SECTORS)
1211 prev_cyl_boundary = rounddown(partp->dp_start + partp->dp_size,
1213 if (prev_cyl_boundary > partp->dp_start)
1214 partp->dp_size = prev_cyl_boundary - partp->dp_start;
1216 partp->dp_size = command->args[3].arg_val;
1218 max_end = partp->dp_start + partp->dp_size;
1220 if (partp->dp_typ == 0) {
1222 * Get out, the partition is marked as unused.
1225 * Insure that it's unused.
1227 bzero(partp, sizeof(*partp));
1233 * Adjust start upwards, if necessary, to fall on a head boundary.
1235 if (partp->dp_start % dos_sectors != 0) {
1236 prev_head_boundary = rounddown(partp->dp_start, dos_sectors);
1237 if (max_end < dos_sectors ||
1238 prev_head_boundary > max_end - dos_sectors) {
1240 * Can't go past end of partition
1243 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1245 current_line_number, partition);
1249 "WARNING: adjusting start offset of partition %d\n\
1250 from %u to %u, to fall on a head boundary",
1251 partition, (u_int)partp->dp_start,
1252 (u_int)(prev_head_boundary + dos_sectors));
1253 partp->dp_start = prev_head_boundary + dos_sectors;
1257 * Adjust size downwards, if necessary, to fall on a cylinder
1260 prev_cyl_boundary = rounddown(partp->dp_start + partp->dp_size,
1262 if (prev_cyl_boundary > partp->dp_start)
1263 adj_size = prev_cyl_boundary - partp->dp_start;
1266 "ERROR: could not adjust partition to start on a head boundary\n\
1267 and end on a cylinder boundary.");
1270 if (adj_size != partp->dp_size) {
1272 "WARNING: adjusting size of partition %d from %u to %u\n\
1273 to end on a cylinder boundary",
1274 partition, (u_int)partp->dp_size, (u_int)adj_size);
1275 partp->dp_size = adj_size;
1277 if (partp->dp_size == 0) {
1278 warnx("ERROR line %d: size of partition %d is zero",
1279 current_line_number, partition);
1292 process_active(CMD *command)
1294 int status = 0, partition, i;
1295 struct dos_partition *partp;
1298 active_processed = 1;
1299 if (command->n_args != 1) {
1300 warnx("ERROR line %d: incorrect number of active args",
1301 current_line_number);
1305 partition = command->args[0].arg_val;
1306 if (partition < 1 || partition > 4) {
1307 warnx("ERROR line %d: invalid partition number %d",
1308 current_line_number, partition);
1312 * Reset active partition
1314 partp = mboot.parts;
1315 for (i = 0; i < NDOSPART; i++)
1316 partp[i].dp_flag = 0;
1317 partp[partition-1].dp_flag = ACTIVE;
1327 process_line(char *line)
1333 parse_config_line(line, &command);
1334 switch (command.cmd) {
1337 * Comment or blank line
1344 status = process_geometry(&command);
1347 status = process_partition(&command);
1350 status = process_active(&command);
1363 read_config(char *config_file)
1370 if (strcmp(config_file, "-") != 0) {
1372 * We're not reading from stdin
1374 if ((fp = fopen(config_file, "r")) == NULL) {
1381 current_line_number = 0;
1383 if (fgets(buf, sizeof(buf), fp) == NULL)
1385 ++current_line_number;
1386 status = process_line(buf);
1394 * It doesn't matter if we're reading from stdin, as we've reached EOF
1406 struct dos_partition *partp;
1409 for (i = 0; i < 4; ++i) {
1410 partp = &mboot.parts[i];
1411 bzero(partp, sizeof(*partp));
1416 sanitize_partition(struct dos_partition *partp)
1418 u_int32_t prev_head_boundary, prev_cyl_boundary;
1419 u_int32_t max_end, size, start;
1421 start = partp->dp_start;
1422 size = partp->dp_size;
1423 max_end = start + size;
1424 /* Only allow a zero size if the partition is being marked unused. */
1426 if (start == 0 && partp->dp_typ == 0)
1428 warnx("ERROR: size of partition is zero");
1431 /* Return if no adjustment is necessary. */
1432 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1436 warnx("WARNING: partition overlaps with partition table");
1437 if (ok("Correct this automatically?"))
1438 start = dos_sectors;
1440 if (start % dos_sectors != 0)
1441 warnx("WARNING: partition does not start on a head boundary");
1442 if ((start +size) % dos_sectors != 0)
1443 warnx("WARNING: partition does not end on a cylinder boundary");
1444 warnx("WARNING: this may confuse the BIOS or some operating systems");
1445 if (!ok("Correct this automatically?"))
1449 * Adjust start upwards, if necessary, to fall on a head boundary.
1451 if (start % dos_sectors != 0) {
1452 prev_head_boundary = rounddown(start, dos_sectors);
1453 if (max_end < dos_sectors ||
1454 prev_head_boundary >= max_end - dos_sectors) {
1456 * Can't go past end of partition
1459 "ERROR: unable to adjust start of partition to fall on a head boundary");
1462 start = prev_head_boundary + dos_sectors;
1466 * Adjust size downwards, if necessary, to fall on a cylinder
1469 prev_cyl_boundary = rounddown(start + size, dos_cylsecs);
1470 if (prev_cyl_boundary > start)
1471 size = prev_cyl_boundary - start;
1473 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1474 and end on a cylinder boundary.");
1478 /* Finally, commit any changes to partp and return. */
1479 if (start != partp->dp_start) {
1480 warnx("WARNING: adjusting start offset of partition to %u",
1482 partp->dp_start = start;
1484 if (size != partp->dp_size) {
1485 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1486 partp->dp_size = size;
1493 * Try figuring out the root device's canonical disk name.
1494 * The following choices are considered:
1495 * /dev/ad0s1a => /dev/ad0
1496 * /dev/da0a => /dev/da0
1497 * /dev/vinum/root => /dev/vinum/root
1498 * A ".eli" part is removed if it exists (see geli(8)).
1499 * A ".journal" ending is removed if it exists (see gjournal(8)).
1504 struct statfs rootfs;
1507 regmatch_t rm[NMATCHES];
1508 char dev[PATH_MAX], *s;
1511 if (statfs("/", &rootfs) == -1)
1512 err(1, "statfs(\"/\")");
1514 if ((rv = regcomp(&re, "^(/dev/[a-z/]+[0-9]*)([sp][0-9]+)?[a-h]?(\\.journal)?$",
1515 REG_EXTENDED)) != 0)
1516 errx(1, "regcomp() failed (%d)", rv);
1517 strlcpy(dev, rootfs.f_mntfromname, sizeof (dev));
1518 if ((s = strstr(dev, ".eli")) != NULL)
1519 memmove(s, s+4, strlen(s + 4) + 1);
1521 if ((rv = regexec(&re, dev, NMATCHES, rm, 0)) != 0)
1523 "mounted root fs resource doesn't match expectations (regexec returned %d)",
1525 if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1526 errx(1, "out of memory");
1527 memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1528 rm[1].rm_eo - rm[1].rm_so);
1529 s[rm[1].rm_eo - rm[1].rm_so] = 0;