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 NOSECTORS ((u_int32_t)-1)
54 static char lbuf[LBUF];
58 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
60 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
61 * Copyright (c) 1989 Robert. V. Baron
65 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
67 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
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 */
75 static int cyls, sectors, heads, cylsecs, disksecs;
78 unsigned char *bootinst; /* boot code */
80 struct dos_partition parts[NDOSPART];
83 static struct mboot mboot;
89 static uint dos_heads;
90 static uint dos_sectors;
91 static uint dos_cylsecs;
93 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
94 #define DOSCYL(c) (c & 0xff)
98 static int current_line_number;
100 static int geom_processed = 0;
101 static int part_processed = 0;
102 static int active_processed = 0;
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;
128 * A list of partition types, probably outdated.
130 static const char *const part_types[256] = {
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)",
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)",
155 [0x3C] = "PartitionMagic recovery",
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",
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",
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",
192 [0xA0] = "Suspend to Disk",
193 [0xA5] = "FreeBSD/NetBSD/386BSD",
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)",
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",
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",
228 ret = (t >= 0 && t <= 255) ? part_types[t] : NULL;
229 return ret ? ret : "unknown";
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);
259 main(int argc, char *argv[])
263 struct dos_partition *partp;
265 while ((c = getopt(argc, argv, "BIab:f:ipqstuv1234")) != -1)
286 print_config_flag = 1;
312 if (f_flag || i_flag)
320 disk = get_rootdisk();
322 disk = g_device_path(argv[0]);
324 err(1, "unable to get correct path for %s", argv[0]);
326 if (open_disk(u_flag) < 0)
327 err(1, "cannot open disk %s", disk);
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;
337 if (print_config_flag) {
341 printf("# %s\n", disk);
342 printf("g c%d h%d s%d\n", dos_cyls, dos_heads, dos_sectors);
344 for (i = 0; i < NDOSPART; i++) {
345 partp = &mboot.parts[i];
347 if (partp->dp_start == 0 && partp->dp_size == 0)
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);
353 /* Fill flags for the partition. */
354 if (partp->dp_flag & 0x80)
355 printf("a %d\n", i + 1);
362 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
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)
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,
377 printf("******* Working on device %s *******\n",disk);
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 -
396 if (read_s0() || i_flag)
398 if (!read_config(f_flag))
411 init_sector0(dos_sectors);
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");
417 for (i = 1; i <= NDOSPART; i++)
420 change_part(partition);
422 if (u_flag || a_flag)
423 change_active(partition);
428 if (u_flag || a_flag || B_flag) {
430 printf("\nWe haven't changed the partition table yet. ");
431 printf("This is your last chance.\n");
435 if (ok("Should we write new partition table?"))
438 printf("\n-t flag specified -- partition table not written.\n");
449 fprintf(stderr, "%s%s",
450 "usage: fdisk [-BIaipqstu] [-b bootcode] [-1234] [disk]\n",
451 " fdisk -f configfile [-itv] [disk]\n");
461 printf("Information from DOS bootblock is:\n");
462 for (i = 1; i <= NDOSPART; i++) {
464 print_part(&mboot.parts[i - 1]);
468 static struct dos_partition mtpart;
471 print_part(const struct dos_partition *partp)
475 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
476 printf("<UNUSED>\n");
480 * Be careful not to overflow.
482 part_mb = partp->dp_size;
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,
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)
496 ,DPSECT(partp->dp_ssect)
497 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
499 ,DPSECT(partp->dp_esect));
511 fname = b_flag ? b_flag : "/boot/mbr";
512 if ((fdesc = open(fname, O_RDONLY)) == -1 ||
513 fstat(fdesc, &sb) == -1)
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 ||
524 if (n != mboot.bootinst_size)
525 errx(1, "%s: short read", fname);
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);
539 init_sector0(unsigned long start)
541 struct dos_partition *partp = &mboot.parts[0];
545 partp->dp_typ = DOSPTYP_386BSD;
546 partp->dp_flag = ACTIVE;
547 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
550 partp->dp_start = start;
551 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
559 struct dos_partition *partp = &mboot.parts[i - 1];
561 printf("The data for partition %d is:\n", i);
564 if (u_flag && ok("Do you want to change it?")) {
568 bzero(partp, sizeof (*partp));
571 printf("\nThe static data for the slice 1 has been reinitialized to:\n");
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");
585 if (ok("Explicitly specify beg/end address ?"))
588 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
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);
598 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
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);
611 } while (!ok("Are we happy with this entry?"));
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);
629 change_active(int which)
631 struct dos_partition *partp = &mboot.parts[0];
632 int active, i, new, tmp;
635 for (i = 0; i < NDOSPART; i++) {
636 if ((partp[i].dp_flag & ACTIVE) == 0)
638 printf("Partition %d is marked active\n", i + 1);
642 if (a_flag && which != -1)
644 else if (active == -1)
647 if (!ok("Do you want to change the active partition?"))
652 Decimal("active partition", new, tmp);
653 if (new < 1 || new > 4) {
654 printf("Active partition number must be in range 1-4."
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;
669 if (ok("Do you want to change the boot code?"))
678 if (ok("Do you want to change our idea of what BIOS thinks ?"))
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;
688 while(!ok("Are you happy with this choice"));
693 /***********************************************\
694 * Change real numbers into strange dos numbers *
695 \***********************************************/
697 dos(struct dos_partition *partp)
702 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
703 memcpy(partp, &mtpart, sizeof(*partp));
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);
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);
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)
737 warnx("can't open device %s", disk);
740 if (get_params() == -1) {
741 warnx("can't get disk parameters on %s", disk);
748 read_disk(off_t sector, void *buf)
751 lseek(fd, (sector * 512), 0);
753 for (secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE;
756 int size = read(fd, buf, secsize);
758 /* it worked so return */
762 return read(fd, buf, secsize);
764 /* we failed to read at any of the sizes */
769 geom_class_available(const char *name)
771 struct gclass *class;
775 error = geom_gettree(&mesh);
777 errc(1, error, "Cannot get GEOM tree");
779 LIST_FOREACH(class, &mesh.lg_class, lg_class) {
780 if (strcmp(class->lg_name, name) == 0) {
781 geom_deletetree(&mesh);
786 geom_deletetree(&mesh);
792 write_disk(off_t sector, void *buf)
794 struct gctl_req *grq;
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);
806 warn("Error getting providername for %s", disk);
809 gctl_ro_param(grq, "geom", -1, pname);
810 gctl_ro_param(grq, "data", secsize, buf);
811 errmsg = gctl_issue(grq);
813 if (errmsg == NULL) {
818 warnx("GEOM_MBR: %s", errmsg);
822 * Try to write MBR directly. This may help when disk
824 * XXX: hardcoded sectorsize
826 error = pwrite(fd, buf, secsize, (sector * 512));
827 if (error == secsize)
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).
835 if (geom_class_available("PART") != 0)
836 warnx("Failed to write MBR. Try to use gpart(8).");
838 warnx("Failed to write sector zero");
849 error = ioctl(fd, DIOCGFWSECTORS, &u);
851 sectors = dos_sectors = u;
853 sectors = dos_sectors = 63;
855 error = ioctl(fd, DIOCGFWHEADS, &u);
857 heads = dos_heads = u;
859 heads = dos_heads = 255;
861 dos_cylsecs = cylsecs = heads * sectors;
862 disksecs = cyls * heads * sectors;
864 u = g_sectorsize(fd);
872 cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
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 "
890 if (read_disk(0, mboot.bootinst) == -1) {
891 warnx("can't read fdisk partition table");
894 if (le16dec(&mboot.bootinst[DOSMAGICOFFSET]) != DOSMAGIC) {
895 warnx("invalid fdisk partition table found");
896 /* So should we initialize things */
899 for (i = 0; i < NDOSPART; i++)
901 &mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
915 for(i = 0; i < NDOSPART; i++)
916 dos_partition_enc(&mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
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");
932 printf("%s [n] ", str);
934 if (fgets(lbuf, LBUF, stdin) == NULL)
936 lbuf[strlen(lbuf)-1] = 0;
939 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
940 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
947 decimal(const char *str, int *num, int deflt)
953 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
955 if (fgets(lbuf, LBUF, stdin) == NULL)
957 lbuf[strlen(lbuf)-1] = 0;
963 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
966 while ((c = *cp++)) {
967 if (c <= '9' && c >= '0')
968 acc = acc * 10 + c - '0';
972 if (c == ' ' || c == '\t')
973 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
978 printf("%s is an invalid decimal number. Try again.\n",
986 parse_config_line(char *line, CMD *command)
992 memset(command, 0, sizeof(*command));
994 while (isspace(*cp)) ++cp;
995 if (*cp == '\0' || *cp == '#')
997 command->cmd = *cp++;
1003 while (isspace(*cp)) ++cp;
1007 break; /* found comment */
1009 command->args[command->n_args].argtype = *cp++;
1011 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1012 if (cp == end || (!isspace(*end) && *end != '\0')) {
1015 while (!isspace(*end) && *end != '\0') ++end;
1016 ch = *end; *end = '\0';
1017 command->args[command->n_args].arg_str = strdup(cp);
1020 command->args[command->n_args].arg_str = NULL;
1030 process_geometry(CMD *command)
1036 if (part_processed) {
1038 "ERROR line %d: the geometry specification line must occur before\n\
1039 all partition specifications",
1040 current_line_number);
1044 if (command->n_args != 3) {
1045 warnx("ERROR line %d: incorrect number of geometry args",
1046 current_line_number);
1053 for (i = 0; i < 3; ++i) {
1054 switch (command->args[i].argtype) {
1056 dos_cyls = command->args[i].arg_val;
1059 dos_heads = command->args[i].arg_val;
1062 dos_sectors = command->args[i].arg_val;
1066 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1067 current_line_number, command->args[i].argtype,
1068 command->args[i].argtype);
1076 dos_cylsecs = dos_heads * dos_sectors;
1079 * Do sanity checks on parameter values
1081 if (dos_cyls == 0) {
1082 warnx("ERROR line %d: number of cylinders not specified",
1083 current_line_number);
1086 if (dos_cyls > 1024) {
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);
1094 if (dos_heads == 0) {
1095 warnx("ERROR line %d: number of heads not specified",
1096 current_line_number);
1098 } else if (dos_heads > 256) {
1099 warnx("ERROR line %d: number of heads must be within (1-256)",
1100 current_line_number);
1104 if (dos_sectors == 0) {
1105 warnx("ERROR line %d: number of sectors not specified",
1106 current_line_number);
1108 } else if (dos_sectors > 63) {
1109 warnx("ERROR line %d: number of sectors must be within (1-63)",
1110 current_line_number);
1120 str2sectors(const char *str)
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);
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;
1139 warnx("ERROR line %d: unexpected modifier: %c "
1140 "(not K/M/G)", current_line_number, *end);
1148 process_partition(CMD *command)
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;
1157 if (command->n_args != 4) {
1158 warnx("ERROR line %d: incorrect number of partition args",
1159 current_line_number);
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);
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) {
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;
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 +
1190 partp->dp_start = str2sectors(command->args[2].arg_str);
1191 if (partp->dp_start == NOSECTORS)
1195 partp->dp_start = command->args[2].arg_val;
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;
1202 partp->dp_size = str2sectors(command->args[3].arg_str);
1203 if (partp->dp_size == NOSECTORS)
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;
1211 partp->dp_size = command->args[3].arg_val;
1213 max_end = partp->dp_start + partp->dp_size;
1215 if (partp->dp_typ == 0) {
1217 * Get out, the partition is marked as unused.
1220 * Insure that it's unused.
1222 bzero(partp, sizeof(*partp));
1228 * Adjust start upwards, if necessary, to fall on a head boundary.
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) {
1235 * Can't go past end of partition
1238 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1240 current_line_number, partition);
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;
1252 * Adjust size downwards, if necessary, to fall on a cylinder
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;
1261 "ERROR: could not adjust partition to start on a head boundary\n\
1262 and end on a cylinder boundary.");
1265 if (adj_size != partp->dp_size) {
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;
1272 if (partp->dp_size == 0) {
1273 warnx("ERROR line %d: size of partition %d is zero",
1274 current_line_number, partition);
1287 process_active(CMD *command)
1289 int status = 0, partition, i;
1290 struct dos_partition *partp;
1293 active_processed = 1;
1294 if (command->n_args != 1) {
1295 warnx("ERROR line %d: incorrect number of active args",
1296 current_line_number);
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);
1307 * Reset active partition
1309 partp = mboot.parts;
1310 for (i = 0; i < NDOSPART; i++)
1311 partp[i].dp_flag = 0;
1312 partp[partition-1].dp_flag = ACTIVE;
1322 process_line(char *line)
1328 parse_config_line(line, &command);
1329 switch (command.cmd) {
1332 * Comment or blank line
1339 status = process_geometry(&command);
1342 status = process_partition(&command);
1345 status = process_active(&command);
1358 read_config(char *config_file)
1365 if (strcmp(config_file, "-") != 0) {
1367 * We're not reading from stdin
1369 if ((fp = fopen(config_file, "r")) == NULL) {
1376 current_line_number = 0;
1378 if (fgets(buf, sizeof(buf), fp) == NULL)
1380 ++current_line_number;
1381 status = process_line(buf);
1389 * It doesn't matter if we're reading from stdin, as we've reached EOF
1401 struct dos_partition *partp;
1404 for (i = 0; i < 4; ++i) {
1405 partp = &mboot.parts[i];
1406 bzero(partp, sizeof(*partp));
1411 sanitize_partition(struct dos_partition *partp)
1413 u_int32_t prev_head_boundary, prev_cyl_boundary;
1414 u_int32_t max_end, size, start;
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. */
1421 if (start == 0 && partp->dp_typ == 0)
1423 warnx("ERROR: size of partition is zero");
1426 /* Return if no adjustment is necessary. */
1427 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1431 warnx("WARNING: partition overlaps with partition table");
1432 if (ok("Correct this automatically?"))
1433 start = dos_sectors;
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?"))
1444 * Adjust start upwards, if necessary, to fall on a head boundary.
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) {
1451 * Can't go past end of partition
1454 "ERROR: unable to adjust start of partition to fall on a head boundary");
1457 start = prev_head_boundary + dos_sectors;
1461 * Adjust size downwards, if necessary, to fall on a cylinder
1464 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1465 if (prev_cyl_boundary > start)
1466 size = prev_cyl_boundary - start;
1468 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1469 and end on a cylinder boundary.");
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",
1477 partp->dp_start = start;
1479 if (size != partp->dp_size) {
1480 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1481 partp->dp_size = size;
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)).
1499 struct statfs rootfs;
1502 regmatch_t rm[NMATCHES];
1503 char dev[PATH_MAX], *s;
1506 if (statfs("/", &rootfs) == -1)
1507 err(1, "statfs(\"/\")");
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);
1516 if ((rv = regexec(&re, dev, NMATCHES, rm, 0)) != 0)
1518 "mounted root fs resource doesn't match expectations (regexec returned %d)",
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;