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>
53 static char lbuf[LBUF];
57 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
59 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
60 * Copyright (c) 1989 Robert. V. Baron
64 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
66 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
68 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
69 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
70 static int secsize = 0; /* the sensed sector size */
74 static int cyls, sectors, heads, cylsecs, disksecs;
77 unsigned char *bootinst; /* boot code */
79 struct dos_partition parts[NDOSPART];
82 static struct mboot mboot;
88 static uint dos_heads;
89 static uint dos_sectors;
90 static uint dos_cylsecs;
92 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
93 #define DOSCYL(c) (c & 0xff)
97 static int current_line_number;
99 static int geom_processed = 0;
100 static int part_processed = 0;
101 static int active_processed = 0;
112 static int B_flag = 0; /* replace boot code */
113 static int I_flag = 0; /* use entire disk for FreeBSD */
114 static int a_flag = 0; /* set active partition */
115 static char *b_flag = NULL; /* path to boot code */
116 static int i_flag = 0; /* replace partition data */
117 static int q_flag = 0; /* Be quiet */
118 static int u_flag = 0; /* update partition data */
119 static int s_flag = 0; /* Print a summary and exit */
120 static int t_flag = 0; /* test only */
121 static char *f_flag = NULL; /* Read config info from file */
122 static int v_flag = 0; /* Be verbose */
123 static int print_config_flag = 0;
126 * A list of partition types, probably outdated.
128 static const char *const part_types[256] = {
130 [0x01] = "Primary DOS with 12 bit FAT",
131 [0x02] = "XENIX / file system",
132 [0x03] = "XENIX /usr file system",
133 [0x04] = "Primary DOS with 16 bit FAT (< 32MB)",
134 [0x05] = "Extended DOS",
135 [0x06] = "Primary DOS, 16 bit FAT (>= 32MB)",
136 [0x07] = "NTFS, OS/2 HPFS, QNX-2 (16 bit) or Advanced UNIX",
137 [0x08] = "AIX file system or SplitDrive",
138 [0x09] = "AIX boot partition or Coherent",
139 [0x0A] = "OS/2 Boot Manager, OPUS or Coherent swap",
140 [0x0B] = "DOS or Windows 95 with 32 bit FAT",
141 [0x0C] = "DOS or Windows 95 with 32 bit FAT (LBA)",
142 [0x0E] = "Primary 'big' DOS (>= 32MB, LBA)",
143 [0x0F] = "Extended DOS (LBA)",
145 [0x11] = "OS/2 BM: hidden DOS with 12-bit FAT",
146 [0x12] = "Compaq diagnostics",
147 [0x14] = "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)",
148 [0x16] = "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)",
149 [0x17] = "OS/2 BM: hidden IFS (e.g. HPFS)",
150 [0x18] = "AST Windows swapfile",
151 [0x1b] = "ASUS Recovery partition (NTFS)",
153 [0x3C] = "PartitionMagic recovery",
155 [0x40] = "VENIX 286",
156 [0x41] = "Linux/MINIX (sharing disk with DRDOS)",
157 [0x42] = "SFS or Linux swap (sharing disk with DRDOS)",
158 [0x43] = "Linux native (sharing disk with DRDOS)",
159 [0x4D] = "QNX 4.2 Primary",
160 [0x4E] = "QNX 4.2 Secondary",
161 [0x4F] = "QNX 4.2 Tertiary",
162 [0x50] = "DM (disk manager)",
163 [0x51] = "DM6 Aux1 (or Novell)",
164 [0x52] = "CP/M or Microport SysV/AT",
167 [0x55] = "EZ-Drive (disk manager)",
168 [0x56] = "Golden Bow (disk manager)",
169 [0x5c] = "Priam Edisk (disk manager)", /* according to S. Widlake */
170 [0x61] = "SpeedStor",
171 [0x63] = "System V/386 (such as ISC UNIX), GNU HURD or Mach",
172 [0x64] = "Novell Netware/286 2.xx",
173 [0x65] = "Novell Netware/386 3.xx",
174 [0x70] = "DiskSecure Multi-Boot",
177 [0x78] = "QNX4.x 2nd part",
178 [0x79] = "QNX4.x 3rd part",
179 [0x80] = "Minix until 1.4a",
180 [0x81] = "Minix since 1.4b, early Linux partition or Mitac disk manager",
181 [0x82] = "Linux swap or Solaris x86",
182 [0x83] = "Linux native",
183 [0x84] = "OS/2 hidden C: drive",
184 [0x85] = "Linux extended",
185 [0x86] = "NTFS volume set??",
186 [0x87] = "NTFS volume set??",
187 [0x93] = "Amoeba file system",
188 [0x94] = "Amoeba bad block table",
190 [0xA0] = "Suspend to Disk",
191 [0xA5] = "FreeBSD/NetBSD/386BSD",
197 [0xB7] = "BSDI BSD/386 file system",
198 [0xB8] = "BSDI BSD/386 swap",
199 [0xBE] = "Solaris x86 boot",
200 [0xBF] = "Solaris x86 (new)",
201 [0xC1] = "DRDOS/sec with 12-bit FAT",
202 [0xC4] = "DRDOS/sec with 16-bit FAT (< 32MB)",
203 [0xC6] = "DRDOS/sec with 16-bit FAT (>= 32MB)",
205 [0xDB] = "CP/M, Concurrent CP/M, Concurrent DOS or CTOS",
206 [0xDE] = "DELL Utilities - FAT filesystem",
207 [0xE1] = "DOS access or SpeedStor with 12-bit FAT extended partition",
208 [0xE3] = "DOS R/O or SpeedStor",
209 [0xE4] = "SpeedStor with 16-bit FAT extended partition < 1024 cyl.",
210 [0xEB] = "BeOS file system",
212 [0xEF] = "EFI System Partition",
213 [0xF1] = "SpeedStor",
214 [0xF2] = "DOS 3.3+ Secondary",
215 [0xF4] = "SpeedStor large partition",
216 [0xFE] = "SpeedStor >1024 cyl. or LANstep",
217 [0xFF] = "Xenix bad blocks table",
225 ret = (t >= 0 && t <= 255) ? part_types[t] : NULL;
226 return ret ? ret : "unknown";
230 static void print_s0(void);
231 static void print_part(const struct dos_partition *);
232 static void init_sector0(unsigned long start);
233 static void init_boot(void);
234 static void change_part(int i);
235 static void print_params(void);
236 static void change_active(int which);
237 static void change_code(void);
238 static void get_params_to_use(void);
239 static char *get_rootdisk(void);
240 static void dos(struct dos_partition *partp);
241 static int open_disk(int flag);
242 static ssize_t read_disk(off_t sector, void *buf);
243 static int write_disk(off_t sector, void *buf);
244 static int get_params(void);
245 static int read_s0(void);
246 static int write_s0(void);
247 static int ok(const char *str);
248 static int decimal(const char *str, int *num, int deflt);
249 static int read_config(char *config_file);
250 static void reset_boot(void);
251 static int sanitize_partition(struct dos_partition *);
252 static void usage(void);
255 main(int argc, char *argv[])
259 struct dos_partition *partp;
261 while ((c = getopt(argc, argv, "BIab:f:ipqstuv1234")) != -1)
282 print_config_flag = 1;
308 if (f_flag || i_flag)
316 disk = get_rootdisk();
318 disk = g_device_path(argv[0]);
320 err(1, "unable to get correct path for %s", argv[0]);
322 if (open_disk(u_flag) < 0)
323 err(1, "cannot open disk %s", disk);
325 /* (abu)use mboot.bootinst to probe for the sector size */
326 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
327 err(1, "cannot allocate buffer to determine disk sector size");
328 if (read_disk(0, mboot.bootinst) == -1)
329 errx(1, "could not detect sector size");
330 free(mboot.bootinst);
331 mboot.bootinst = NULL;
333 if (print_config_flag) {
337 printf("# %s\n", disk);
338 printf("g c%d h%d s%d\n", dos_cyls, dos_heads, dos_sectors);
340 for (i = 0; i < NDOSPART; i++) {
341 partp = &mboot.parts[i];
343 if (partp->dp_start == 0 && partp->dp_size == 0)
346 printf("p %d 0x%02x %lu %lu\n", i + 1, partp->dp_typ,
347 (u_long)partp->dp_start, (u_long)partp->dp_size);
349 /* Fill flags for the partition. */
350 if (partp->dp_flag & 0x80)
351 printf("a %d\n", i + 1);
358 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
360 printf("Part %11s %11s Type Flags\n", "Start", "Size");
361 for (i = 0; i < NDOSPART; i++) {
362 partp = &mboot.parts[i];
363 if (partp->dp_start == 0 && partp->dp_size == 0)
365 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
366 (u_long) partp->dp_start,
367 (u_long) partp->dp_size, partp->dp_typ,
373 printf("******* Working on device %s *******\n",disk);
378 partp = &mboot.parts[0];
379 partp->dp_typ = DOSPTYP_386BSD;
380 partp->dp_flag = ACTIVE;
381 partp->dp_start = dos_sectors;
382 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
392 if (read_s0() || i_flag)
394 if (!read_config(f_flag))
407 init_sector0(dos_sectors);
409 printf("Media sector size is %d\n", secsize);
410 printf("Warning: BIOS sector numbering starts with sector 1\n");
411 printf("Information from DOS bootblock is:\n");
413 for (i = 1; i <= NDOSPART; i++)
416 change_part(partition);
418 if (u_flag || a_flag)
419 change_active(partition);
424 if (u_flag || a_flag || B_flag) {
426 printf("\nWe haven't changed the partition table yet. ");
427 printf("This is your last chance.\n");
431 if (ok("Should we write new partition table?"))
434 printf("\n-t flag specified -- partition table not written.\n");
445 fprintf(stderr, "%s%s",
446 "usage: fdisk [-BIaipqstu] [-b bootcode] [-1234] [disk]\n",
447 " fdisk -f configfile [-itv] [disk]\n");
457 printf("Information from DOS bootblock is:\n");
458 for (i = 1; i <= NDOSPART; i++) {
460 print_part(&mboot.parts[i - 1]);
464 static struct dos_partition mtpart;
467 print_part(const struct dos_partition *partp)
471 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
472 printf("<UNUSED>\n");
476 * Be careful not to overflow.
478 part_mb = partp->dp_size;
480 part_mb /= (1024 * 1024);
481 printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
482 get_type(partp->dp_typ));
483 printf(" start %lu, size %lu (%ju Meg), flag %x%s\n",
484 (u_long)partp->dp_start,
485 (u_long)partp->dp_size,
488 partp->dp_flag == ACTIVE ? " (active)" : "");
489 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
490 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
492 ,DPSECT(partp->dp_ssect)
493 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
495 ,DPSECT(partp->dp_esect));
507 fname = b_flag ? b_flag : "/boot/mbr";
508 if ((fdesc = open(fname, O_RDONLY)) == -1 ||
509 fstat(fdesc, &sb) == -1)
511 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
512 errx(1, "%s: length must be a multiple of sector size", fname);
513 if (mboot.bootinst != NULL)
514 free(mboot.bootinst);
515 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
516 errx(1, "%s: unable to allocate read buffer", fname);
517 if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
520 if (n != mboot.bootinst_size)
521 errx(1, "%s: short read", fname);
523 if (mboot.bootinst != NULL)
524 free(mboot.bootinst);
525 mboot.bootinst_size = secsize;
526 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL)
527 errx(1, "unable to allocate boot block buffer");
528 memset(mboot.bootinst, 0, mboot.bootinst_size);
529 le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
535 init_sector0(unsigned long start)
537 struct dos_partition *partp = &mboot.parts[0];
541 partp->dp_typ = DOSPTYP_386BSD;
542 partp->dp_flag = ACTIVE;
543 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
546 partp->dp_start = start;
547 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
555 struct dos_partition *partp = &mboot.parts[i - 1];
557 printf("The data for partition %d is:\n", i);
560 if (u_flag && ok("Do you want to change it?")) {
564 bzero(partp, sizeof (*partp));
567 printf("\nThe static data for the slice 1 has been reinitialized to:\n");
573 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
574 Decimal("start", partp->dp_start, tmp);
575 Decimal("size", partp->dp_size, tmp);
576 if (!sanitize_partition(partp)) {
577 warnx("ERROR: failed to adjust; setting sysid to 0");
581 if (ok("Explicitly specify beg/end address ?"))
584 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
586 tsec = DPSECT(partp->dp_ssect);
587 Decimal("beginning cylinder", tcyl, tmp);
588 Decimal("beginning head", thd, tmp);
589 Decimal("beginning sector", tsec, tmp);
590 partp->dp_scyl = DOSCYL(tcyl);
591 partp->dp_ssect = DOSSECT(tsec,tcyl);
594 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
596 tsec = DPSECT(partp->dp_esect);
597 Decimal("ending cylinder", tcyl, tmp);
598 Decimal("ending head", thd, tmp);
599 Decimal("ending sector", tsec, tmp);
600 partp->dp_ecyl = DOSCYL(tcyl);
601 partp->dp_esect = DOSSECT(tsec,tcyl);
607 } while (!ok("Are we happy with this entry?"));
614 printf("parameters extracted from in-core disklabel are:\n");
615 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
616 ,cyls,heads,sectors,cylsecs);
617 if (dos_cyls > 1023 || dos_heads > 255 || dos_sectors > 63)
618 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
619 printf("parameters to be used for BIOS calculations are:\n");
620 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
621 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
625 change_active(int which)
627 struct dos_partition *partp = &mboot.parts[0];
628 int active, i, new, tmp;
631 for (i = 0; i < NDOSPART; i++) {
632 if ((partp[i].dp_flag & ACTIVE) == 0)
634 printf("Partition %d is marked active\n", i + 1);
638 if (a_flag && which != -1)
640 else if (active == -1)
643 if (!ok("Do you want to change the active partition?"))
648 Decimal("active partition", new, tmp);
649 if (new < 1 || new > 4) {
650 printf("Active partition number must be in range 1-4."
655 } while (!ok("Are you happy with this choice"));
656 for (i = 0; i < NDOSPART; i++)
657 partp[i].dp_flag = 0;
658 if (active > 0 && active <= NDOSPART)
659 partp[active-1].dp_flag = ACTIVE;
665 if (ok("Do you want to change the boot code?"))
674 if (ok("Do you want to change our idea of what BIOS thinks ?"))
678 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
679 Decimal("BIOS's idea of #heads", dos_heads, tmp);
680 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
681 dos_cylsecs = dos_heads * dos_sectors;
684 while(!ok("Are you happy with this choice"));
689 /***********************************************\
690 * Change real numbers into strange dos numbers *
691 \***********************************************/
693 dos(struct dos_partition *partp)
698 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
699 memcpy(partp, &mtpart, sizeof(*partp));
704 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
705 cy = partp->dp_start / dos_cylsecs;
706 sec = partp->dp_start % dos_sectors + 1;
707 partp->dp_scyl = DOSCYL(cy);
708 partp->dp_ssect = DOSSECT(sec, cy);
711 end = partp->dp_start + partp->dp_size - 1;
712 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
713 cy = end / dos_cylsecs;
714 sec = end % dos_sectors + 1;
715 partp->dp_ecyl = DOSCYL(cy);
716 partp->dp_esect = DOSSECT(sec, cy);
724 /* Write mode if one of these flags are set. */
725 rwmode = (a_flag || I_flag || B_flag || flag);
726 fd = g_open(disk, rwmode);
727 /* If the mode fails, try read-only if we didn't. */
728 if (fd == -1 && errno == EPERM && rwmode)
729 fd = g_open(disk, 0);
730 if (fd == -1 && errno == ENXIO)
733 warnx("can't open device %s", disk);
736 if (get_params() == -1) {
737 warnx("can't get disk parameters on %s", disk);
744 read_disk(off_t sector, void *buf)
747 lseek(fd, (sector * 512), 0);
749 for (secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE;
752 int size = read(fd, buf, secsize);
754 /* it worked so return */
758 return read(fd, buf, secsize);
760 /* we failed to read at any of the sizes */
765 write_disk(off_t sector, void *buf)
768 struct gctl_req *grq;
770 char fbuf[BUFSIZ], *pname;
773 grq = gctl_get_handle();
774 gctl_ro_param(grq, "verb", -1, "write MBR");
775 gctl_ro_param(grq, "class", -1, "MBR");
776 pname = g_providername(fd);
778 warn("Error getting providername for %s", disk);
781 gctl_ro_param(grq, "geom", -1, pname);
782 gctl_ro_param(grq, "data", secsize, buf);
783 errmsg = gctl_issue(grq);
785 if (errmsg == NULL) {
789 if (!q_flag) /* GEOM errors are benign, not all devices supported */
793 error = pwrite(fd, buf, secsize, (sector * 512));
794 if (error == secsize)
797 for (i = 1; i < 5; i++) {
798 sprintf(fbuf, "%ss%d", disk, i);
799 fdw = open(fbuf, O_RDWR, 0);
802 error = ioctl(fdw, DIOCSMBR, buf);
807 warnx("Failed to write sector zero");
818 error = ioctl(fd, DIOCGFWSECTORS, &u);
820 sectors = dos_sectors = u;
822 sectors = dos_sectors = 63;
824 error = ioctl(fd, DIOCGFWHEADS, &u);
826 heads = dos_heads = u;
828 heads = dos_heads = 255;
830 dos_cylsecs = cylsecs = heads * sectors;
831 disksecs = cyls * heads * sectors;
833 u = g_sectorsize(fd);
841 cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
851 mboot.bootinst_size = secsize;
852 if (mboot.bootinst != NULL)
853 free(mboot.bootinst);
854 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
855 warnx("unable to allocate buffer to read fdisk "
859 if (read_disk(0, mboot.bootinst) == -1) {
860 warnx("can't read fdisk partition table");
863 if (le16dec(&mboot.bootinst[DOSMAGICOFFSET]) != DOSMAGIC) {
864 warnx("invalid fdisk partition table found");
865 /* So should we initialize things */
868 for (i = 0; i < NDOSPART; i++)
870 &mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
884 for(i = 0; i < NDOSPART; i++)
885 dos_partition_enc(&mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
887 le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
888 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
889 if (write_disk(sector,
890 &mboot.bootinst[sector * secsize]) == -1) {
891 warn("can't write fdisk partition table");
901 printf("%s [n] ", str);
903 if (fgets(lbuf, LBUF, stdin) == NULL)
905 lbuf[strlen(lbuf)-1] = 0;
908 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
909 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
916 decimal(const char *str, int *num, int deflt)
922 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
924 if (fgets(lbuf, LBUF, stdin) == NULL)
926 lbuf[strlen(lbuf)-1] = 0;
932 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
935 while ((c = *cp++)) {
936 if (c <= '9' && c >= '0')
937 acc = acc * 10 + c - '0';
941 if (c == ' ' || c == '\t')
942 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
947 printf("%s is an invalid decimal number. Try again.\n",
955 parse_config_line(char *line, CMD *command)
961 memset(command, 0, sizeof(*command));
963 while (isspace(*cp)) ++cp;
964 if (*cp == '\0' || *cp == '#')
966 command->cmd = *cp++;
972 while (isspace(*cp)) ++cp;
974 break; /* found comment */
976 command->args[command->n_args].argtype = *cp++;
978 break; /* assume end of line */
980 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
982 break; /* couldn't parse number */
992 process_geometry(CMD *command)
998 if (part_processed) {
1000 "ERROR line %d: the geometry specification line must occur before\n\
1001 all partition specifications",
1002 current_line_number);
1006 if (command->n_args != 3) {
1007 warnx("ERROR line %d: incorrect number of geometry args",
1008 current_line_number);
1015 for (i = 0; i < 3; ++i) {
1016 switch (command->args[i].argtype) {
1018 dos_cyls = command->args[i].arg_val;
1021 dos_heads = command->args[i].arg_val;
1024 dos_sectors = command->args[i].arg_val;
1028 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1029 current_line_number, command->args[i].argtype,
1030 command->args[i].argtype);
1038 dos_cylsecs = dos_heads * dos_sectors;
1041 * Do sanity checks on parameter values
1043 if (dos_cyls == 0) {
1044 warnx("ERROR line %d: number of cylinders not specified",
1045 current_line_number);
1048 if (dos_cyls > 1024) {
1050 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1051 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1052 is dedicated to FreeBSD)",
1053 current_line_number, dos_cyls);
1056 if (dos_heads == 0) {
1057 warnx("ERROR line %d: number of heads not specified",
1058 current_line_number);
1060 } else if (dos_heads > 256) {
1061 warnx("ERROR line %d: number of heads must be within (1-256)",
1062 current_line_number);
1066 if (dos_sectors == 0) {
1067 warnx("ERROR line %d: number of sectors not specified",
1068 current_line_number);
1070 } else if (dos_sectors > 63) {
1071 warnx("ERROR line %d: number of sectors must be within (1-63)",
1072 current_line_number);
1083 process_partition(CMD *command)
1085 int status = 0, partition;
1086 u_int32_t prev_head_boundary, prev_cyl_boundary;
1087 u_int32_t adj_size, max_end;
1088 struct dos_partition *partp;
1092 if (command->n_args != 4) {
1093 warnx("ERROR line %d: incorrect number of partition args",
1094 current_line_number);
1097 partition = command->args[0].arg_val;
1098 if (partition < 1 || partition > 4) {
1099 warnx("ERROR line %d: invalid partition number %d",
1100 current_line_number, partition);
1103 partp = &mboot.parts[partition - 1];
1104 bzero(partp, sizeof (*partp));
1105 partp->dp_typ = command->args[1].arg_val;
1106 partp->dp_start = command->args[2].arg_val;
1107 partp->dp_size = command->args[3].arg_val;
1108 max_end = partp->dp_start + partp->dp_size;
1110 if (partp->dp_typ == 0) {
1112 * Get out, the partition is marked as unused.
1115 * Insure that it's unused.
1117 bzero(partp, sizeof(*partp));
1123 * Adjust start upwards, if necessary, to fall on a head boundary.
1125 if (partp->dp_start % dos_sectors != 0) {
1126 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1127 if (max_end < dos_sectors ||
1128 prev_head_boundary > max_end - dos_sectors) {
1130 * Can't go past end of partition
1133 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1135 current_line_number, partition);
1139 "WARNING: adjusting start offset of partition %d\n\
1140 from %u to %u, to fall on a head boundary",
1141 partition, (u_int)partp->dp_start,
1142 (u_int)(prev_head_boundary + dos_sectors));
1143 partp->dp_start = prev_head_boundary + dos_sectors;
1147 * Adjust size downwards, if necessary, to fall on a cylinder
1151 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1152 if (prev_cyl_boundary > partp->dp_start)
1153 adj_size = prev_cyl_boundary - partp->dp_start;
1156 "ERROR: could not adjust partition to start on a head boundary\n\
1157 and end on a cylinder boundary.");
1160 if (adj_size != partp->dp_size) {
1162 "WARNING: adjusting size of partition %d from %u to %u\n\
1163 to end on a cylinder boundary",
1164 partition, (u_int)partp->dp_size, (u_int)adj_size);
1165 partp->dp_size = adj_size;
1167 if (partp->dp_size == 0) {
1168 warnx("ERROR line %d: size of partition %d is zero",
1169 current_line_number, partition);
1182 process_active(CMD *command)
1184 int status = 0, partition, i;
1185 struct dos_partition *partp;
1188 active_processed = 1;
1189 if (command->n_args != 1) {
1190 warnx("ERROR line %d: incorrect number of active args",
1191 current_line_number);
1195 partition = command->args[0].arg_val;
1196 if (partition < 1 || partition > 4) {
1197 warnx("ERROR line %d: invalid partition number %d",
1198 current_line_number, partition);
1202 * Reset active partition
1204 partp = mboot.parts;
1205 for (i = 0; i < NDOSPART; i++)
1206 partp[i].dp_flag = 0;
1207 partp[partition-1].dp_flag = ACTIVE;
1217 process_line(char *line)
1223 parse_config_line(line, &command);
1224 switch (command.cmd) {
1227 * Comment or blank line
1234 status = process_geometry(&command);
1237 status = process_partition(&command);
1240 status = process_active(&command);
1253 read_config(char *config_file)
1260 if (strcmp(config_file, "-") != 0) {
1262 * We're not reading from stdin
1264 if ((fp = fopen(config_file, "r")) == NULL) {
1271 current_line_number = 0;
1273 if (fgets(buf, sizeof(buf), fp) == NULL)
1275 ++current_line_number;
1276 status = process_line(buf);
1284 * It doesn't matter if we're reading from stdin, as we've reached EOF
1296 struct dos_partition *partp;
1299 for (i = 0; i < 4; ++i) {
1300 partp = &mboot.parts[i];
1301 bzero(partp, sizeof(*partp));
1306 sanitize_partition(struct dos_partition *partp)
1308 u_int32_t prev_head_boundary, prev_cyl_boundary;
1309 u_int32_t max_end, size, start;
1311 start = partp->dp_start;
1312 size = partp->dp_size;
1313 max_end = start + size;
1314 /* Only allow a zero size if the partition is being marked unused. */
1316 if (start == 0 && partp->dp_typ == 0)
1318 warnx("ERROR: size of partition is zero");
1321 /* Return if no adjustment is necessary. */
1322 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1326 warnx("WARNING: partition overlaps with partition table");
1327 if (ok("Correct this automatically?"))
1328 start = dos_sectors;
1330 if (start % dos_sectors != 0)
1331 warnx("WARNING: partition does not start on a head boundary");
1332 if ((start +size) % dos_sectors != 0)
1333 warnx("WARNING: partition does not end on a cylinder boundary");
1334 warnx("WARNING: this may confuse the BIOS or some operating systems");
1335 if (!ok("Correct this automatically?"))
1339 * Adjust start upwards, if necessary, to fall on a head boundary.
1341 if (start % dos_sectors != 0) {
1342 prev_head_boundary = start / dos_sectors * dos_sectors;
1343 if (max_end < dos_sectors ||
1344 prev_head_boundary >= max_end - dos_sectors) {
1346 * Can't go past end of partition
1349 "ERROR: unable to adjust start of partition to fall on a head boundary");
1352 start = prev_head_boundary + dos_sectors;
1356 * Adjust size downwards, if necessary, to fall on a cylinder
1359 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1360 if (prev_cyl_boundary > start)
1361 size = prev_cyl_boundary - start;
1363 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1364 and end on a cylinder boundary.");
1368 /* Finally, commit any changes to partp and return. */
1369 if (start != partp->dp_start) {
1370 warnx("WARNING: adjusting start offset of partition to %u",
1372 partp->dp_start = start;
1374 if (size != partp->dp_size) {
1375 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1376 partp->dp_size = size;
1383 * Try figuring out the root device's canonical disk name.
1384 * The following choices are considered:
1385 * /dev/ad0s1a => /dev/ad0
1386 * /dev/da0a => /dev/da0
1387 * /dev/vinum/root => /dev/vinum/root
1392 struct statfs rootfs;
1395 regmatch_t rm[NMATCHES];
1399 if (statfs("/", &rootfs) == -1)
1400 err(1, "statfs(\"/\")");
1402 if ((rv = regcomp(&re, "^(/dev/[a-z/]+[0-9]+)([sp][0-9]+)?[a-h]?$",
1403 REG_EXTENDED)) != 0)
1404 errx(1, "regcomp() failed (%d)", rv);
1405 if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
1407 "mounted root fs resource doesn't match expectations (regexec returned %d)",
1409 if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1410 errx(1, "out of memory");
1411 memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1412 rm[1].rm_eo - rm[1].rm_so);
1413 s[rm[1].rm_eo - rm[1].rm_so] = 0;