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
65 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
67 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
68 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
69 static int secsize = 0; /* the sensed sector size */
73 static struct disklabel disklabel; /* disk parameters */
75 static int cyls, sectors, heads, cylsecs, disksecs;
78 unsigned char padding[2]; /* force the longs to be long aligned */
79 unsigned char *bootinst; /* boot code */
81 struct dos_partition parts[4];
84 static struct mboot mboot;
88 #define BOOT_MAGIC 0xAA55
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;
115 static int B_flag = 0; /* replace boot code */
116 static int I_flag = 0; /* use entire disk for FreeBSD */
117 static int a_flag = 0; /* set active partition */
118 static char *b_flag = NULL; /* path to boot code */
119 static int i_flag = 0; /* replace partition data */
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 */
126 static struct part_type
132 ,{0x01, "Primary DOS with 12 bit FAT"}
133 ,{0x02, "XENIX / filesystem"}
134 ,{0x03, "XENIX /usr filesystem"}
135 ,{0x04, "Primary DOS with 16 bit FAT (< 32MB)"}
136 ,{0x05, "Extended DOS"}
137 ,{0x06, "Primary 'big' DOS (>= 32MB)"}
138 ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
139 ,{0x08, "AIX filesystem 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"}
154 ,{0x3C, "PartitionMagic recovery"}
157 ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
158 ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
159 ,{0x43, "Linux native (sharing disk with DRDOS)"}
160 ,{0x4D, "QNX 4.2 Primary"}
161 ,{0x4E, "QNX 4.2 Secondary"}
162 ,{0x4F, "QNX 4.2 Tertiary"}
163 ,{0x50, "DM (disk manager)"}
164 ,{0x51, "DM6 Aux1 (or Novell)"}
165 ,{0x52, "CP/M or Microport SysV/AT"}
168 ,{0x55, "EZ-Drive (disk manager)"}
169 ,{0x56, "Golden Bow (disk manager)"}
170 ,{0x5c, "Priam Edisk (disk manager)"} /* according to S. Widlake */
172 ,{0x63, "System V/386 (such as ISC UNIX), GNU HURD or Mach"}
173 ,{0x64, "Novell Netware/286 2.xx"}
174 ,{0x65, "Novell Netware/386 3.xx"}
175 ,{0x70, "DiskSecure Multi-Boot"}
178 ,{0x78, "QNX4.x 2nd part"}
179 ,{0x79, "QNX4.x 3rd part"}
180 ,{0x80, "Minix until 1.4a"}
181 ,{0x81, "Minix since 1.4b, early Linux partition or Mitac disk manager"}
182 ,{0x82, "Linux swap or Solaris x86"}
183 ,{0x83, "Linux native"}
184 ,{0x84, "OS/2 hidden C: drive"}
185 ,{0x85, "Linux extended"}
186 ,{0x86, "NTFS volume set??"}
187 ,{0x87, "NTFS volume set??"}
188 ,{0x93, "Amoeba filesystem"}
189 ,{0x94, "Amoeba bad block table"}
191 ,{0xA0, "Suspend to Disk"}
192 ,{0xA5, "FreeBSD/NetBSD/386BSD"}
197 ,{0xB7, "BSDI BSD/386 filesystem"}
198 ,{0xB8, "BSDI BSD/386 swap"}
199 ,{0xC1, "DRDOS/sec with 12-bit FAT"}
200 ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
201 ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
203 ,{0xDB, "CP/M, Concurrent CP/M, Concurrent DOS or CTOS"}
204 ,{0xE1, "DOS access or SpeedStor with 12-bit FAT extended partition"}
205 ,{0xE3, "DOS R/O or SpeedStor"}
206 ,{0xE4, "SpeedStor with 16-bit FAT extended partition < 1024 cyl."}
207 ,{0xEB, "BeOS filesystem"}
209 ,{0xEF, "EFI System Partition"}
211 ,{0xF2, "DOS 3.3+ Secondary"}
212 ,{0xF4, "SpeedStor large partition"}
213 ,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
214 ,{0xFF, "Xenix bad blocks table"}
217 static void print_s0(int which);
218 static void print_part(int i);
219 static void init_sector0(unsigned long start);
220 static void init_boot(void);
221 static void change_part(int i);
222 static void print_params(void);
223 static void change_active(int which);
224 static void change_code(void);
225 static void get_params_to_use(void);
226 static char *get_rootdisk(void);
227 static void dos(struct dos_partition *partp);
228 static int open_disk(int flag);
229 static ssize_t read_disk(off_t sector, void *buf);
230 static ssize_t write_disk(off_t sector, void *buf);
231 static int get_params(void);
232 static int read_s0(void);
233 static int write_s0(void);
234 static int ok(const char *str);
235 static int decimal(const char *str, int *num, int deflt);
236 static const char *get_type(int type);
237 static int read_config(char *config_file);
238 static void reset_boot(void);
239 static int sanitize_partition(struct dos_partition *);
240 static void usage(void);
243 main(int argc, char *argv[])
248 struct dos_partition *partp;
250 while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
291 if (f_flag || i_flag)
299 disk = get_rootdisk();
301 if (stat(argv[0], &sb) == 0) {
302 /* OK, full pathname given */
304 } else if (errno == ENOENT) {
305 /* Try prepending "/dev" */
306 asprintf(&disk, "%s%s", _PATH_DEV, argv[0]);
308 errx(1, "out of memory");
310 /* other stat error, let it fail below */
314 if (open_disk(u_flag) < 0)
315 err(1, "cannot open disk %s", disk);
317 /* (abu)use mboot.bootinst to probe for the sector size */
318 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
319 err(1, "cannot allocate buffer to determine disk sector size");
320 read_disk(0, mboot.bootinst);
321 free(mboot.bootinst);
322 mboot.bootinst = NULL;
327 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
329 printf("Part %11s %11s Type Flags\n", "Start", "Size");
330 for (i = 0; i < NDOSPART; i++) {
331 partp = ((struct dos_partition *) &mboot.parts) + i;
332 if (partp->dp_start == 0 && partp->dp_size == 0)
334 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
335 (u_long) partp->dp_start,
336 (u_long) partp->dp_size, partp->dp_typ,
342 printf("******* Working on device %s *******\n",disk);
347 partp = (struct dos_partition *) (&mboot.parts[0]);
348 partp->dp_typ = DOSPTYP_386BSD;
349 partp->dp_flag = ACTIVE;
350 partp->dp_start = dos_sectors;
351 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
361 if (read_s0() || i_flag)
363 if (!read_config(f_flag))
376 init_sector0(dos_sectors);
378 printf("Media sector size is %d\n", secsize);
379 printf("Warning: BIOS sector numbering starts with sector 1\n");
380 printf("Information from DOS bootblock is:\n");
382 for (i = 1; i <= NDOSPART; i++)
385 change_part(partition);
387 if (u_flag || a_flag)
388 change_active(partition);
393 if (u_flag || a_flag || B_flag) {
395 printf("\nWe haven't changed the partition table yet. ");
396 printf("This is your last chance.\n");
400 if (ok("Should we write new partition table?"))
403 printf("\n-t flag specified -- partition table not written.\n");
414 fprintf(stderr, "%s%s",
415 "usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
416 " fdisk -f configfile [-itv] [disk]\n");
426 printf("Information from DOS bootblock is:\n");
428 for (i = 1; i <= NDOSPART; i++)
429 printf("%d: ", i), print_part(i);
434 static struct dos_partition mtpart;
439 struct dos_partition *partp;
442 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
444 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
445 printf("<UNUSED>\n");
449 * Be careful not to overflow.
451 part_mb = partp->dp_size;
453 part_mb /= (1024 * 1024);
454 printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
455 get_type(partp->dp_typ));
456 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
457 (u_long)partp->dp_start,
458 (u_long)partp->dp_size,
461 partp->dp_flag == ACTIVE ? " (active)" : "");
462 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
463 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
465 ,DPSECT(partp->dp_ssect)
466 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
468 ,DPSECT(partp->dp_esect));
480 fname = b_flag ? b_flag : "/boot/mbr";
481 if ((fdesc = open(fname, O_RDONLY)) == -1 ||
482 fstat(fdesc, &sb) == -1)
484 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
485 errx(1, "%s: length must be a multiple of sector size", fname);
486 if (mboot.bootinst != NULL)
487 free(mboot.bootinst);
488 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
489 errx(1, "%s: unable to allocate read buffer", fname);
490 if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
493 if (n != mboot.bootinst_size)
494 errx(1, "%s: short read", fname);
496 if (mboot.bootinst != NULL)
497 free(mboot.bootinst);
498 mboot.bootinst_size = secsize;
499 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL)
500 errx(1, "unable to allocate boot block buffer");
501 memset(mboot.bootinst, 0, mboot.bootinst_size);
502 *(uint16_t *)&mboot.bootinst[MBRSIGOFF] = BOOT_MAGIC;
508 init_sector0(unsigned long start)
510 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
514 partp->dp_typ = DOSPTYP_386BSD;
515 partp->dp_flag = ACTIVE;
516 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
519 partp->dp_start = start;
520 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
528 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
530 printf("The data for partition %d is:\n", i);
533 if (u_flag && ok("Do you want to change it?")) {
537 bzero((char *)partp, sizeof (struct dos_partition));
540 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
546 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
547 Decimal("start", partp->dp_start, tmp);
548 Decimal("size", partp->dp_size, tmp);
549 if (!sanitize_partition(partp)) {
550 warnx("ERROR: failed to adjust; setting sysid to 0");
554 if (ok("Explicitly specify beg/end address ?"))
557 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
559 tsec = DPSECT(partp->dp_ssect);
560 Decimal("beginning cylinder", tcyl, tmp);
561 Decimal("beginning head", thd, tmp);
562 Decimal("beginning sector", tsec, tmp);
563 partp->dp_scyl = DOSCYL(tcyl);
564 partp->dp_ssect = DOSSECT(tsec,tcyl);
567 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
569 tsec = DPSECT(partp->dp_esect);
570 Decimal("ending cylinder", tcyl, tmp);
571 Decimal("ending head", thd, tmp);
572 Decimal("ending sector", tsec, tmp);
573 partp->dp_ecyl = DOSCYL(tcyl);
574 partp->dp_esect = DOSSECT(tsec,tcyl);
580 } while (!ok("Are we happy with this entry?"));
587 printf("parameters extracted from in-core disklabel are:\n");
588 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
589 ,cyls,heads,sectors,cylsecs);
590 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
591 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
592 printf("parameters to be used for BIOS calculations are:\n");
593 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
594 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
598 change_active(int which)
600 struct dos_partition *partp = &mboot.parts[0];
601 int active, i, new, tmp;
604 for (i = 0; i < NDOSPART; i++) {
605 if ((partp[i].dp_flag & ACTIVE) == 0)
607 printf("Partition %d is marked active\n", i + 1);
611 if (a_flag && which != -1)
613 else if (active == -1)
616 if (!ok("Do you want to change the active partition?"))
621 Decimal("active partition", new, tmp);
622 if (new < 1 || new > 4) {
623 printf("Active partition number must be in range 1-4."
628 } while (!ok("Are you happy with this choice"));
629 for (i = 0; i < NDOSPART; i++)
630 partp[i].dp_flag = 0;
631 if (active > 0 && active <= NDOSPART)
632 partp[active-1].dp_flag = ACTIVE;
638 if (ok("Do you want to change the boot code?"))
647 if (ok("Do you want to change our idea of what BIOS thinks ?"))
651 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
652 Decimal("BIOS's idea of #heads", dos_heads, tmp);
653 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
654 dos_cylsecs = dos_heads * dos_sectors;
657 while(!ok("Are you happy with this choice"));
662 /***********************************************\
663 * Change real numbers into strange dos numbers *
664 \***********************************************/
666 dos(struct dos_partition *partp)
671 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
672 memcpy(partp, &mtpart, sizeof(*partp));
677 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
678 cy = partp->dp_start / dos_cylsecs;
679 sec = partp->dp_start % dos_sectors + 1;
680 partp->dp_scyl = DOSCYL(cy);
681 partp->dp_ssect = DOSSECT(sec, cy);
684 end = partp->dp_start + partp->dp_size - 1;
685 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
686 cy = end / dos_cylsecs;
687 sec = end % dos_sectors + 1;
688 partp->dp_ecyl = DOSCYL(cy);
689 partp->dp_esect = DOSSECT(sec, cy);
697 if (stat(disk, &st) == -1) {
700 warnx("can't get file status of %s", disk);
703 if ( !(st.st_mode & S_IFCHR) )
704 warnx("device %s is not character special", disk);
706 a_flag || I_flag || B_flag || flag ? O_RDWR : O_RDONLY)) == -1) {
709 warnx("can't open device %s", disk);
712 if (get_params() == -1) {
713 warnx("can't get disk parameters on %s", disk);
720 read_disk(off_t sector, void *buf)
722 lseek(fd,(sector * 512), 0);
724 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
727 int size = read(fd, buf, secsize);
728 if( size == secsize )
729 /* it worked so return */
733 return read( fd, buf, secsize );
735 /* we failed to read at any of the sizes */
740 write_disk(off_t sector, void *buf)
742 lseek(fd,(sector * 512), 0);
743 /* write out in the size that the read_disk found worked */
744 return write(fd, buf, secsize);
751 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
752 warnx("can't get disk parameters on %s; supplying dummy ones", disk);
754 dos_heads = heads = 1;
755 dos_sectors = sectors = 1;
756 dos_cylsecs = cylsecs = heads * sectors;
757 disksecs = cyls * heads * sectors;
761 dos_cyls = cyls = disklabel.d_ncylinders;
762 dos_heads = heads = disklabel.d_ntracks;
763 dos_sectors = sectors = disklabel.d_nsectors;
764 dos_cylsecs = cylsecs = heads * sectors;
765 disksecs = cyls * heads * sectors;
774 mboot.bootinst_size = secsize;
775 if (mboot.bootinst != NULL)
776 free(mboot.bootinst);
777 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
778 warnx("unable to allocate buffer to read fdisk "
782 if (read_disk(0, mboot.bootinst) == -1) {
783 warnx("can't read fdisk partition table");
786 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
787 warnx("invalid fdisk partition table found");
788 /* So should we initialize things */
791 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
804 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
806 * write enable label sector before write (if necessary),
807 * disable after writing.
808 * needed if the disklabel protected area also protects
809 * sector 0. (e.g. empty disk)
811 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
812 if (write_disk(sector,
813 &mboot.bootinst[sector * secsize]) == -1) {
814 warn("can't write fdisk partition table");
824 printf("%s [n] ", str);
826 if (fgets(lbuf, LBUF, stdin) == NULL)
828 lbuf[strlen(lbuf)-1] = 0;
831 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
832 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
839 decimal(const char *str, int *num, int deflt)
845 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
847 if (fgets(lbuf, LBUF, stdin) == NULL)
849 lbuf[strlen(lbuf)-1] = 0;
855 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
858 while ((c = *cp++)) {
859 if (c <= '9' && c >= '0')
860 acc = acc * 10 + c - '0';
864 if (c == ' ' || c == '\t')
865 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
870 printf("%s is an invalid decimal number. Try again.\n",
879 int numentries = (sizeof(part_types)/sizeof(struct part_type));
881 struct part_type *ptr = part_types;
884 while(counter < numentries) {
885 if(ptr->type == type)
895 parse_config_line(char *line, CMD *command)
901 memset(command, 0, sizeof(*command));
903 while (isspace(*cp)) ++cp;
904 if (*cp == '\0' || *cp == '#')
906 command->cmd = *cp++;
912 while (isspace(*cp)) ++cp;
914 break; /* found comment */
916 command->args[command->n_args].argtype = *cp++;
918 break; /* assume end of line */
920 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
922 break; /* couldn't parse number */
932 process_geometry(CMD *command)
938 if (part_processed) {
940 "ERROR line %d: the geometry specification line must occur before\n\
941 all partition specifications",
942 current_line_number);
946 if (command->n_args != 3) {
947 warnx("ERROR line %d: incorrect number of geometry args",
948 current_line_number);
955 for (i = 0; i < 3; ++i) {
956 switch (command->args[i].argtype) {
958 dos_cyls = command->args[i].arg_val;
961 dos_heads = command->args[i].arg_val;
964 dos_sectors = command->args[i].arg_val;
968 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
969 current_line_number, command->args[i].argtype,
970 command->args[i].argtype);
978 dos_cylsecs = dos_heads * dos_sectors;
981 * Do sanity checks on parameter values
984 warnx("ERROR line %d: number of cylinders not specified",
985 current_line_number);
988 if (dos_cyls > 1024) {
990 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
991 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
992 is dedicated to FreeBSD)",
993 current_line_number, dos_cyls);
996 if (dos_heads == 0) {
997 warnx("ERROR line %d: number of heads not specified",
998 current_line_number);
1000 } else if (dos_heads > 256) {
1001 warnx("ERROR line %d: number of heads must be within (1-256)",
1002 current_line_number);
1006 if (dos_sectors == 0) {
1007 warnx("ERROR line %d: number of sectors not specified",
1008 current_line_number);
1010 } else if (dos_sectors > 63) {
1011 warnx("ERROR line %d: number of sectors must be within (1-63)",
1012 current_line_number);
1023 process_partition(CMD *command)
1025 int status = 0, partition;
1026 u_int32_t prev_head_boundary, prev_cyl_boundary;
1027 u_int32_t adj_size, max_end;
1028 struct dos_partition *partp;
1032 if (command->n_args != 4) {
1033 warnx("ERROR line %d: incorrect number of partition args",
1034 current_line_number);
1037 partition = command->args[0].arg_val;
1038 if (partition < 1 || partition > 4) {
1039 warnx("ERROR line %d: invalid partition number %d",
1040 current_line_number, partition);
1043 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1044 bzero((char *)partp, sizeof (struct dos_partition));
1045 partp->dp_typ = command->args[1].arg_val;
1046 partp->dp_start = command->args[2].arg_val;
1047 partp->dp_size = command->args[3].arg_val;
1048 max_end = partp->dp_start + partp->dp_size;
1050 if (partp->dp_typ == 0) {
1052 * Get out, the partition is marked as unused.
1055 * Insure that it's unused.
1057 bzero((char *)partp, sizeof (struct dos_partition));
1063 * Adjust start upwards, if necessary, to fall on an head boundary.
1065 if (partp->dp_start % dos_sectors != 0) {
1066 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1067 if (max_end < dos_sectors ||
1068 prev_head_boundary > max_end - dos_sectors) {
1070 * Can't go past end of partition
1073 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1075 current_line_number, partition);
1079 "WARNING: adjusting start offset of partition %d\n\
1080 from %u to %u, to fall on a head boundary",
1081 partition, (u_int)partp->dp_start,
1082 (u_int)(prev_head_boundary + dos_sectors));
1083 partp->dp_start = prev_head_boundary + dos_sectors;
1087 * Adjust size downwards, if necessary, to fall on a cylinder
1091 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1092 if (prev_cyl_boundary > partp->dp_start)
1093 adj_size = prev_cyl_boundary - partp->dp_start;
1096 "ERROR: could not adjust partition to start on a head boundary\n\
1097 and end on a cylinder boundary.");
1100 if (adj_size != partp->dp_size) {
1102 "WARNING: adjusting size of partition %d from %u to %u\n\
1103 to end on a cylinder boundary",
1104 partition, (u_int)partp->dp_size, (u_int)adj_size);
1105 partp->dp_size = adj_size;
1107 if (partp->dp_size == 0) {
1108 warnx("ERROR line %d: size of partition %d is zero",
1109 current_line_number, partition);
1122 process_active(CMD *command)
1124 int status = 0, partition, i;
1125 struct dos_partition *partp;
1128 active_processed = 1;
1129 if (command->n_args != 1) {
1130 warnx("ERROR line %d: incorrect number of active args",
1131 current_line_number);
1135 partition = command->args[0].arg_val;
1136 if (partition < 1 || partition > 4) {
1137 warnx("ERROR line %d: invalid partition number %d",
1138 current_line_number, partition);
1142 * Reset active partition
1144 partp = ((struct dos_partition *) &mboot.parts);
1145 for (i = 0; i < NDOSPART; i++)
1146 partp[i].dp_flag = 0;
1147 partp[partition-1].dp_flag = ACTIVE;
1157 process_line(char *line)
1163 parse_config_line(line, &command);
1164 switch (command.cmd) {
1167 * Comment or blank line
1174 status = process_geometry(&command);
1177 status = process_partition(&command);
1180 status = process_active(&command);
1193 read_config(char *config_file)
1200 if (strcmp(config_file, "-") != 0) {
1202 * We're not reading from stdin
1204 if ((fp = fopen(config_file, "r")) == NULL) {
1211 current_line_number = 0;
1213 if (fgets(buf, sizeof(buf), fp) == NULL)
1215 ++current_line_number;
1216 status = process_line(buf);
1224 * It doesn't matter if we're reading from stdin, as we've reached EOF
1236 struct dos_partition *partp;
1239 for (i = 0; i < 4; ++i) {
1240 partp = ((struct dos_partition *) &mboot.parts) + i;
1241 bzero((char *)partp, sizeof (struct dos_partition));
1246 sanitize_partition(struct dos_partition *partp)
1248 u_int32_t prev_head_boundary, prev_cyl_boundary;
1249 u_int32_t max_end, size, start;
1251 start = partp->dp_start;
1252 size = partp->dp_size;
1253 max_end = start + size;
1254 /* Only allow a zero size if the partition is being marked unused. */
1256 if (start == 0 && partp->dp_typ == 0)
1258 warnx("ERROR: size of partition is zero");
1261 /* Return if no adjustment is necessary. */
1262 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1265 if (start % dos_sectors != 0)
1266 warnx("WARNING: partition does not start on a head boundary");
1267 if ((start +size) % dos_sectors != 0)
1268 warnx("WARNING: partition does not end on a cylinder boundary");
1269 warnx("WARNING: this may confuse the BIOS or some operating systems");
1270 if (!ok("Correct this automatically?"))
1274 * Adjust start upwards, if necessary, to fall on an head boundary.
1276 if (start % dos_sectors != 0) {
1277 prev_head_boundary = start / dos_sectors * dos_sectors;
1278 if (max_end < dos_sectors ||
1279 prev_head_boundary >= max_end - dos_sectors) {
1281 * Can't go past end of partition
1284 "ERROR: unable to adjust start of partition to fall on a head boundary");
1287 start = prev_head_boundary + dos_sectors;
1291 * Adjust size downwards, if necessary, to fall on a cylinder
1294 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1295 if (prev_cyl_boundary > start)
1296 size = prev_cyl_boundary - start;
1298 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1299 and end on a cylinder boundary.");
1303 /* Finally, commit any changes to partp and return. */
1304 if (start != partp->dp_start) {
1305 warnx("WARNING: adjusting start offset of partition to %u",
1307 partp->dp_start = start;
1309 if (size != partp->dp_size) {
1310 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1311 partp->dp_size = size;
1318 * Try figuring out the root device's canonical disk name.
1319 * The following choices are considered:
1320 * /dev/ad0s1a => /dev/ad0
1321 * /dev/da0a => /dev/da0
1322 * /dev/vinum/root => /dev/vinum/root
1327 struct statfs rootfs;
1330 regmatch_t rm[NMATCHES];
1334 if (statfs("/", &rootfs) == -1)
1335 err(1, "statfs(\"/\")");
1337 if ((rv = regcomp(&re, "^(/dev/.*)(\\d+(s\\d+)?[a-h])?$",
1338 REG_EXTENDED)) != 0)
1339 errx(1, "regcomp() failed (%d)", rv);
1340 if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
1342 "mounted root fs resource doesn't match expectations (regexec returned %d)",
1344 if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1345 errx(1, "out of memory");
1346 memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1347 rm[1].rm_eo - rm[1].rm_so);
1348 s[rm[1].rm_eo - rm[1].rm_so] = 0;