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.
28 static const char rcsid[] =
32 #include <sys/disklabel.h>
33 #include <sys/param.h>
35 #include <sys/mount.h>
50 static char lbuf[LBUF];
56 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
58 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
59 * Copyright (c) 1989 Robert. V. Baron
63 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
64 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
65 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
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 int secsize = 0; /* the sensed sector size */
75 struct disklabel disklabel; /* disk parameters */
77 int cyls, sectors, heads, cylsecs, disksecs;
81 unsigned char padding[2]; /* force the longs to be long aligned */
82 unsigned char *bootinst; /* boot code */
84 struct dos_partition parts[4];
86 struct mboot mboot = {{0}, NULL, 0};
89 #define BOOT_MAGIC 0xAA55
96 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
97 #define DOSCYL(c) (c & 0xff)
98 static int partition = -1;
103 static int current_line_number;
105 static int geom_processed = 0;
106 static int part_processed = 0;
107 static int active_processed = 0;
120 static int B_flag = 0; /* replace boot code */
121 static int I_flag = 0; /* use entire disk for FreeBSD */
122 static int a_flag = 0; /* set active partition */
123 static char *b_flag = NULL; /* path to boot code */
124 static int i_flag = 0; /* replace partition data */
125 static int u_flag = 0; /* update partition data */
126 static int s_flag = 0; /* Print a summary and exit */
127 static int t_flag = 0; /* test only, if f_flag is given */
128 static char *f_flag = NULL; /* Read config info from file */
129 static int v_flag = 0; /* Be verbose */
138 ,{0x01, "Primary DOS with 12 bit FAT"}
139 ,{0x02, "XENIX / filesystem"}
140 ,{0x03, "XENIX /usr filesystem"}
141 ,{0x04, "Primary DOS with 16 bit FAT (< 32MB)"}
142 ,{0x05, "Extended DOS"}
143 ,{0x06, "Primary 'big' DOS (>= 32MB)"}
144 ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
145 ,{0x08, "AIX filesystem or SplitDrive"}
146 ,{0x09, "AIX boot partition or Coherent"}
147 ,{0x0A, "OS/2 Boot Manager, OPUS or Coherent swap"}
148 ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
149 ,{0x0C, "DOS or Windows 95 with 32 bit FAT (LBA)"}
150 ,{0x0E, "Primary 'big' DOS (>= 32MB, LBA)"}
151 ,{0x0F, "Extended DOS (LBA)"}
153 ,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
154 ,{0x12, "Compaq diagnostics"}
155 ,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
156 ,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
157 ,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
158 ,{0x18, "AST Windows swapfile"}
160 ,{0x3C, "PartitionMagic recovery"}
163 ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
164 ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
165 ,{0x43, "Linux native (sharing disk with DRDOS)"}
166 ,{0x4D, "QNX 4.2 Primary"}
167 ,{0x4E, "QNX 4.2 Secondary"}
168 ,{0x4F, "QNX 4.2 Tertiary"}
169 ,{0x50, "DM (disk manager)"}
170 ,{0x51, "DM6 Aux1 (or Novell)"}
171 ,{0x52, "CP/M or Microport SysV/AT"}
174 ,{0x55, "EZ-Drive (disk manager)"}
175 ,{0x56, "Golden Bow (disk manager)"}
176 ,{0x5c, "Priam Edisk (disk manager)"} /* according to S. Widlake */
178 ,{0x63, "System V/386 (such as ISC UNIX), GNU HURD or Mach"}
179 ,{0x64, "Novell Netware/286 2.xx"}
180 ,{0x65, "Novell Netware/386 3.xx"}
181 ,{0x70, "DiskSecure Multi-Boot"}
184 ,{0x78, "QNX4.x 2nd part"}
185 ,{0x79, "QNX4.x 3rd part"}
186 ,{0x80, "Minix until 1.4a"}
187 ,{0x81, "Minix since 1.4b, early Linux partition or Mitac (disk manager)"}
188 ,{0x82, "Linux swap or Solaris x86"}
189 ,{0x83, "Linux native"}
190 ,{0x84, "OS/2 hidden C: drive"}
191 ,{0x85, "Linux extended"}
192 ,{0x86, "NTFS volume set??"}
193 ,{0x87, "NTFS volume set??"}
194 ,{0x93, "Amoeba filesystem"}
195 ,{0x94, "Amoeba bad block table"}
197 ,{0xA0, "Suspend to Disk"}
198 ,{0xA5, "FreeBSD/NetBSD/386BSD"}
202 ,{0xB7, "BSDI BSD/386 filesystem"}
203 ,{0xB8, "BSDI BSD/386 swap"}
204 ,{0xC1, "DRDOS/sec with 12-bit FAT"}
205 ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
206 ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
208 ,{0xDB, "CP/M, Concurrent CP/M, Concurrent DOS or CTOS"}
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 filesystem"}
214 ,{0xEF, "EFI System Partition"}
216 ,{0xF2, "DOS 3.3+ Secondary"}
217 ,{0xF4, "SpeedStor large partition"}
218 ,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
219 ,{0xFF, "Xenix bad blocks table"}
222 static void print_s0(int which);
223 static void print_part(int i);
224 static void init_sector0(unsigned long start);
225 static void init_boot(void);
226 static void change_part(int i);
227 static void print_params();
228 static void change_active(int which);
229 static void change_code();
230 static void get_params_to_use();
231 static char *get_rootdisk(void);
232 static void dos(struct dos_partition *partp);
233 static int open_disk(int u_flag);
234 static ssize_t read_disk(off_t sector, void *buf);
235 static ssize_t write_disk(off_t sector, void *buf);
236 static int get_params();
237 static int read_s0();
238 static int write_s0();
239 static int ok(char *str);
240 static int decimal(char *str, int *num, int deflt);
241 static char *get_type(int type);
242 static int read_config(char *config_file);
243 static void reset_boot(void);
244 static int sanitize_partition(struct dos_partition *);
245 static void usage(void);
247 static int hex(char *str, int *num, int deflt);
248 static int string(char *str, char **ans);
253 main(int argc, char *argv[])
258 while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
299 if (f_flag || i_flag)
307 disk = get_rootdisk();
309 if (stat(argv[0], &sb) == 0) {
310 /* OK, full pathname given */
312 } else if (errno == ENOENT) {
313 /* Try prepending "/dev" */
314 if ((disk = malloc(strlen(argv[0]) + strlen(_PATH_DEV) +
316 errx(1, "out of memory");
317 strcpy(disk, _PATH_DEV);
318 strcat(disk, argv[0]);
320 /* other stat error, let it fail below */
324 if (open_disk(u_flag) < 0)
325 err(1, "cannot open disk %s", disk);
327 /* (abu)use mboot.bootinst to probe for the sector size */
328 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
329 err(1, "cannot allocate buffer to determine disk sector size");
330 read_disk(0, mboot.bootinst);
331 free(mboot.bootinst);
332 mboot.bootinst = NULL;
337 struct dos_partition *partp;
341 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
343 printf("Part %11s %11s Type Flags\n", "Start", "Size");
344 for (i = 0; i < NDOSPART; i++) {
345 partp = ((struct dos_partition *) &mboot.parts) + i;
346 if (partp->dp_start == 0 && partp->dp_size == 0)
348 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
349 (u_long) partp->dp_start,
350 (u_long) partp->dp_size, partp->dp_typ,
356 printf("******* Working on device %s *******\n",disk);
360 struct dos_partition *partp;
364 partp = (struct dos_partition *) (&mboot.parts[0]);
365 partp->dp_typ = DOSPTYP_386BSD;
366 partp->dp_flag = ACTIVE;
367 partp->dp_start = dos_sectors;
368 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
379 if (read_s0() || i_flag)
384 if (!read_config(f_flag))
409 init_sector0(dos_sectors);
411 printf("Media sector size is %d\n", secsize);
412 printf("Warning: BIOS sector numbering starts with sector 1\n");
413 printf("Information from DOS bootblock is:\n");
415 for (i = 1; i <= NDOSPART; i++)
418 change_part(partition);
420 if (u_flag || a_flag)
421 change_active(partition);
426 if (u_flag || a_flag || B_flag) {
429 printf("\nWe haven't changed the partition table yet. ");
430 printf("This is your last chance.\n");
435 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 [-BIaistu] [-b bootcode] [-1234] [disk]\n",
453 " fdisk -f configfile [-itv] [disk]\n");
463 printf("Information from DOS bootblock is:\n");
465 for (i = 1; i <= NDOSPART; i++)
466 printf("%d: ", i), print_part(i);
471 static struct dos_partition mtpart = { 0 };
476 struct dos_partition *partp;
479 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
481 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
482 printf("<UNUSED>\n");
486 * Be careful not to overflow.
488 part_mb = partp->dp_size;
490 part_mb /= (1024 * 1024);
491 printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
492 get_type(partp->dp_typ));
493 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
494 (u_long)partp->dp_start,
495 (u_long)partp->dp_size,
498 partp->dp_flag == ACTIVE ? " (active)" : "");
499 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
500 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
502 ,DPSECT(partp->dp_ssect)
503 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
505 ,DPSECT(partp->dp_esect));
516 fname = b_flag ? b_flag : "/boot/mbr";
517 if ((fd = open(fname, O_RDONLY)) == -1 ||
518 fstat(fd, &sb) == -1)
520 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
521 errx(1, "%s: length must be a multiple of sector size", fname);
522 if (mboot.bootinst != NULL)
523 free(mboot.bootinst);
524 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
525 errx(1, "%s: unable to allocate read buffer", fname);
526 if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
529 if (n != mboot.bootinst_size)
530 errx(1, "%s: short read", fname);
535 init_sector0(unsigned long start)
537 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
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 = ((struct dos_partition *) &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((char *)partp, sizeof (struct dos_partition));
567 printf("\nThe static data for the DOS partition 4 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_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
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 \***********************************************/
694 struct dos_partition *partp;
699 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
700 memcpy(partp, &mtpart, sizeof(*partp));
705 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
706 cy = partp->dp_start / dos_cylsecs;
707 sec = partp->dp_start % dos_sectors + 1;
708 partp->dp_scyl = DOSCYL(cy);
709 partp->dp_ssect = DOSSECT(sec, cy);
712 end = partp->dp_start + partp->dp_size - 1;
713 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
714 cy = end / dos_cylsecs;
715 sec = end % dos_sectors + 1;
716 partp->dp_ecyl = DOSCYL(cy);
717 partp->dp_esect = DOSSECT(sec, cy);
722 /* Getting device status */
725 open_disk(int u_flag)
729 if (stat(disk, &st) == -1) {
732 warnx("can't get file status of %s", disk);
735 if ( !(st.st_mode & S_IFCHR) )
736 warnx("device %s is not character special", disk);
738 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
741 warnx("can't open device %s", disk);
744 if (get_params(0) == -1) {
745 warnx("can't get disk parameters on %s", disk);
752 read_disk(off_t sector, void *buf)
754 lseek(fd,(sector * 512), 0);
756 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
759 int size = read(fd, buf, secsize);
760 if( size == secsize )
761 /* it worked so return */
765 return read( fd, buf, secsize );
767 /* we failed to read at any of the sizes */
772 write_disk(off_t sector, void *buf)
774 lseek(fd,(sector * 512), 0);
775 /* write out in the size that the read_disk found worked */
776 return write(fd, buf, secsize);
783 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
784 warnx("can't get disk parameters on %s; supplying dummy ones", disk);
786 dos_heads = heads = 1;
787 dos_sectors = sectors = 1;
788 dos_cylsecs = cylsecs = heads * sectors;
789 disksecs = cyls * heads * sectors;
793 dos_cyls = cyls = disklabel.d_ncylinders;
794 dos_heads = heads = disklabel.d_ntracks;
795 dos_sectors = sectors = disklabel.d_nsectors;
796 dos_cylsecs = cylsecs = heads * sectors;
797 disksecs = cyls * heads * sectors;
806 mboot.bootinst_size = secsize;
807 if (mboot.bootinst != NULL)
808 free(mboot.bootinst);
809 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
810 warnx("unable to allocate buffer to read fdisk "
814 if (read_disk(0, mboot.bootinst) == -1) {
815 warnx("can't read fdisk partition table");
818 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
819 warnx("invalid fdisk partition table found");
820 /* So should we initialize things */
823 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
839 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
841 * write enable label sector before write (if necessary),
842 * disable after writing.
843 * needed if the disklabel protected area also protects
844 * sector 0. (e.g. empty disk)
848 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
849 warn("ioctl DIOCWLABEL");
851 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
852 if (write_disk(sector,
853 &mboot.bootinst[sector * secsize]) == -1) {
854 warn("can't write fdisk partition table");
858 (void) ioctl(fd, DIOCWLABEL, &flag);
863 (void) ioctl(fd, DIOCWLABEL, &flag);
873 printf("%s [n] ", str);
875 if (fgets(lbuf, LBUF, stdin) == NULL)
877 lbuf[strlen(lbuf)-1] = 0;
880 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
881 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
888 decimal(char *str, int *num, int deflt)
894 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
896 if (fgets(lbuf, LBUF, stdin) == NULL)
898 lbuf[strlen(lbuf)-1] = 0;
904 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
907 while ((c = *cp++)) {
908 if (c <= '9' && c >= '0')
909 acc = acc * 10 + c - '0';
913 if (c == ' ' || c == '\t')
914 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
919 printf("%s is an invalid decimal number. Try again.\n",
927 hex(char *str, int *num, int deflt)
933 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
934 fgets(lbuf, LBUF, stdin);
935 lbuf[strlen(lbuf)-1] = 0;
941 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
944 while ((c = *cp++)) {
945 if (c <= '9' && c >= '0')
946 acc = (acc << 4) + c - '0';
947 else if (c <= 'f' && c >= 'a')
948 acc = (acc << 4) + c - 'a' + 10;
949 else if (c <= 'F' && c >= 'A')
950 acc = (acc << 4) + c - 'A' + 10;
954 if (c == ' ' || c == '\t')
955 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
960 printf("%s is an invalid hex number. Try again.\n",
967 string(char *str, char **ans)
973 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
974 fgets(lbuf, LBUF, stdin);
975 lbuf[strlen(lbuf)-1] = 0;
980 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
984 while ((c = *cp) && c != '"') cp++;
987 while ((c = *cp) && c != ' ' && c != '\t') cp++;
1000 int numentries = (sizeof(part_types)/sizeof(struct part_type));
1002 struct part_type *ptr = part_types;
1005 while(counter < numentries)
1007 if(ptr->type == type)
1019 parse_config_line(line, command)
1026 while (1) /* dirty trick used to insure one exit point for this
1029 memset(command, 0, sizeof(*command));
1031 while (isspace(*cp)) ++cp;
1032 if (*cp == '\0' || *cp == '#')
1036 command->cmd = *cp++;
1043 while (isspace(*cp)) ++cp;
1046 break; /* found comment */
1050 command->args[command->n_args].argtype = *cp++;
1054 break; /* assume end of line */
1057 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1060 break; /* couldn't parse number */
1071 process_geometry(command)
1082 "ERROR line %d: the geometry specification line must occur before\n\
1083 all partition specifications",
1084 current_line_number);
1088 if (command->n_args != 3)
1090 warnx("ERROR line %d: incorrect number of geometry args",
1091 current_line_number);
1098 for (i = 0; i < 3; ++i)
1100 switch (command->args[i].argtype)
1103 dos_cyls = command->args[i].arg_val;
1106 dos_heads = command->args[i].arg_val;
1109 dos_sectors = command->args[i].arg_val;
1113 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1114 current_line_number, command->args[i].argtype,
1115 command->args[i].argtype);
1125 dos_cylsecs = dos_heads * dos_sectors;
1128 * Do sanity checks on parameter values
1132 warnx("ERROR line %d: number of cylinders not specified",
1133 current_line_number);
1136 if (dos_cyls == 0 || dos_cyls > 1024)
1139 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1140 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1141 is dedicated to FreeBSD)",
1142 current_line_number, dos_cyls);
1147 warnx("ERROR line %d: number of heads not specified",
1148 current_line_number);
1151 else if (dos_heads < 1 || dos_heads > 256)
1153 warnx("ERROR line %d: number of heads must be within (1-256)",
1154 current_line_number);
1158 if (dos_sectors < 0)
1160 warnx("ERROR line %d: number of sectors not specified",
1161 current_line_number);
1164 else if (dos_sectors < 1 || dos_sectors > 63)
1166 warnx("ERROR line %d: number of sectors must be within (1-63)",
1167 current_line_number);
1178 process_partition(command)
1181 int status = 0, partition;
1182 u_int32_t prev_head_boundary, prev_cyl_boundary;
1183 u_int32_t adj_size, max_end;
1184 struct dos_partition *partp;
1189 if (command->n_args != 4)
1191 warnx("ERROR line %d: incorrect number of partition args",
1192 current_line_number);
1195 partition = command->args[0].arg_val;
1196 if (partition < 1 || partition > 4)
1198 warnx("ERROR line %d: invalid partition number %d",
1199 current_line_number, partition);
1202 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1203 bzero((char *)partp, sizeof (struct dos_partition));
1204 partp->dp_typ = command->args[1].arg_val;
1205 partp->dp_start = command->args[2].arg_val;
1206 partp->dp_size = command->args[3].arg_val;
1207 max_end = partp->dp_start + partp->dp_size;
1209 if (partp->dp_typ == 0)
1212 * Get out, the partition is marked as unused.
1215 * Insure that it's unused.
1217 bzero((char *)partp, sizeof (struct dos_partition));
1223 * Adjust start upwards, if necessary, to fall on an head boundary.
1225 if (partp->dp_start % dos_sectors != 0)
1227 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1228 if (max_end < dos_sectors ||
1229 prev_head_boundary > max_end - dos_sectors)
1232 * Can't go past end of partition
1235 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1237 current_line_number, partition);
1241 "WARNING: adjusting start offset of partition %d\n\
1242 from %u to %u, to fall on a head boundary",
1243 partition, (u_int)partp->dp_start,
1244 (u_int)(prev_head_boundary + dos_sectors));
1245 partp->dp_start = prev_head_boundary + dos_sectors;
1249 * Adjust size downwards, if necessary, to fall on a cylinder
1253 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1254 if (prev_cyl_boundary > partp->dp_start)
1255 adj_size = prev_cyl_boundary - partp->dp_start;
1259 "ERROR: could not adjust partition to start on a head boundary\n\
1260 and end on a cylinder boundary.");
1263 if (adj_size != partp->dp_size)
1266 "WARNING: adjusting size of partition %d from %u to %u\n\
1267 to end on a cylinder boundary",
1268 partition, (u_int)partp->dp_size, (u_int)adj_size);
1269 partp->dp_size = adj_size;
1271 if (partp->dp_size == 0)
1273 warnx("ERROR line %d: size of partition %d is zero",
1274 current_line_number, partition);
1287 process_active(command)
1290 int status = 0, partition, i;
1291 struct dos_partition *partp;
1295 active_processed = 1;
1296 if (command->n_args != 1)
1298 warnx("ERROR line %d: incorrect number of active args",
1299 current_line_number);
1303 partition = command->args[0].arg_val;
1304 if (partition < 1 || partition > 4)
1306 warnx("ERROR line %d: invalid partition number %d",
1307 current_line_number, partition);
1311 * Reset active partition
1313 partp = ((struct dos_partition *) &mboot.parts);
1314 for (i = 0; i < NDOSPART; i++)
1315 partp[i].dp_flag = 0;
1316 partp[partition-1].dp_flag = ACTIVE;
1334 parse_config_line(line, &command);
1335 switch (command.cmd)
1339 * Comment or blank line
1346 status = process_geometry(&command);
1349 status = process_partition(&command);
1352 status = process_active(&command);
1365 read_config(config_file)
1372 while (1) /* dirty trick used to insure one exit point for this
1375 if (strcmp(config_file, "-") != 0)
1378 * We're not reading from stdin
1380 if ((fp = fopen(config_file, "r")) == NULL)
1390 current_line_number = 0;
1393 if (fgets(buf, sizeof(buf), fp) == NULL)
1397 ++current_line_number;
1398 status = process_line(buf);
1409 * It doesn't matter if we're reading from stdin, as we've reached EOF
1421 struct dos_partition *partp;
1424 for (i = 0; i < 4; ++i)
1426 partp = ((struct dos_partition *) &mboot.parts) + i;
1427 bzero((char *)partp, sizeof (struct dos_partition));
1432 sanitize_partition(partp)
1433 struct dos_partition *partp;
1435 u_int32_t prev_head_boundary, prev_cyl_boundary;
1436 u_int32_t max_end, size, start;
1438 start = partp->dp_start;
1439 size = partp->dp_size;
1440 max_end = start + size;
1441 /* Only allow a zero size if the partition is being marked unused. */
1443 if (start == 0 && partp->dp_typ == 0)
1445 warnx("ERROR: size of partition is zero");
1448 /* Return if no adjustment is necessary. */
1449 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1452 if (start % dos_sectors != 0)
1453 warnx("WARNING: partition does not start on a head boundary");
1454 if ((start +size) % dos_sectors != 0)
1455 warnx("WARNING: partition does not end on a cylinder boundary");
1456 warnx("WARNING: this may confuse the BIOS or some operating systems");
1457 if (!ok("Correct this automatically?"))
1461 * Adjust start upwards, if necessary, to fall on an head boundary.
1463 if (start % dos_sectors != 0) {
1464 prev_head_boundary = start / dos_sectors * dos_sectors;
1465 if (max_end < dos_sectors ||
1466 prev_head_boundary >= max_end - dos_sectors) {
1468 * Can't go past end of partition
1471 "ERROR: unable to adjust start of partition to fall on a head boundary");
1474 start = prev_head_boundary + dos_sectors;
1478 * Adjust size downwards, if necessary, to fall on a cylinder
1481 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1482 if (prev_cyl_boundary > start)
1483 size = prev_cyl_boundary - start;
1485 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1486 and end on a cylinder boundary.");
1490 /* Finally, commit any changes to partp and return. */
1491 if (start != partp->dp_start) {
1492 warnx("WARNING: adjusting start offset of partition to %u",
1494 partp->dp_start = start;
1496 if (size != partp->dp_size) {
1497 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1498 partp->dp_size = size;
1505 * Try figuring out the root device's canonical disk name.
1506 * The following choices are considered:
1507 * /dev/ad0s1a => /dev/ad0
1508 * /dev/da0a => /dev/da0
1509 * /dev/vinum/root => /dev/vinum/root
1514 struct statfs rootfs;
1517 regmatch_t rm[NMATCHES];
1521 if (statfs("/", &rootfs) == -1)
1522 err(1, "statfs(\"/\")");
1524 if ((rv = regcomp(&re, "^(/dev/.*)(\\d+(s\\d+)?[a-h])?$",
1525 REG_EXTENDED)) != 0)
1526 errx(1, "regcomp() failed (%d)", rv);
1527 if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
1529 "mounted root fs resource doesn't match expectations (regexec returned %d)",
1531 if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1532 errx(1, "out of memory");
1533 memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1534 rm[1].rm_eo - rm[1].rm_so);
1535 s[rm[1].rm_eo - rm[1].rm_so] = 0;