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.
26 * $FreeBSD: /repoman/r/ncvs/src/sbin/i386/fdisk/fdisk.c,v 1.36.2.14 2004/01/30 14:40:47 harti Exp $
29 #include <sys/param.h>
30 #include <sys/diskslice.h>
31 #include <sys/diskmbr.h>
32 #include <sys/sysctl.h>
44 #include <bus/cam/scsi/scsi_daio.h>
47 static char lbuf[LBUF];
51 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
53 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
54 * Copyright (c) 1989 Robert. V. Baron
58 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
59 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
60 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
61 #define MAX_SECTORS_PER_TRACK 0x3f /* maximum number of sectors per track */
62 #define MIN_SECTORS_PER_TRACK 0x1 /* minimum number of sectors per track */
63 #define MAX_HEADS 0xff /* maximum number of head */
64 static int secsize = 0; /* the sensed sector size */
66 static int fd; /* file descriptor of the given disk */
67 static const char *disk;
68 static const char *disks[] =
70 "/dev/ad0", "/dev/da0", "/dev/vkd0", 0
73 static int cyls, sectors, heads, cylsecs;
74 static int64_t disksecs;
78 unsigned char padding[2]; /* force the longs to be long aligned */
79 unsigned char *bootinst; /* boot code */
81 struct dos_partition parts[NDOSPART];
83 static struct mboot mboot;
92 #define DOSSECT(s,c) ((s & MAX_SECTORS_PER_TRACK) | ((c >> 2) & 0xc0))
93 #define DOSCYL(c) (c & 0xff)
95 static int partition = -1;
98 static int current_line_number;
100 static int geom_processed = 0;
101 static int part_processed = 0;
102 static int active_processed = 0;
113 static int B_flag = 0; /* replace boot code */
114 static int C_flag = 0; /* use wrapped values for CHS */
115 static int E_flag = 0; /* Erase through TRIM */
116 static int I_flag = 0; /* use entire disk for DragonFly */
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 p_flag = 0; /* operate on a disk image file */
122 static int s_flag = 0; /* Print a summary and exit */
123 static int t_flag = 0; /* test only */
124 static int x_flag = 0; /* Expand-to-fit device */
125 static char *f_flag = NULL; /* Read config info from file */
126 static int v_flag = 0; /* Be verbose */
128 static void print_s0(int which);
129 static void print_part(int i);
130 static void init_sector0(unsigned long start);
131 static void init_boot(void);
132 static void change_part(int i);
133 static void print_params(void);
134 static void change_active(int which);
135 static void change_code(void);
136 static void get_params_to_use(void);
137 static void dos(struct dos_partition *partp);
138 static int open_disk(void);
139 static void erase_partition(int i);
140 static ssize_t read_disk(off_t sector, void *buf);
141 static ssize_t write_disk(off_t sector, void *buf);
142 static int get_params(void);
143 static int read_s0(void);
144 static int write_s0(void);
145 static int ok(const char *str);
146 static int decimal(const char *str, int *num, int deflt);
147 static const char *get_type(int type);
148 static int read_config(char *config_file);
149 static void reset_boot(void);
150 static int sanitize_partition(struct dos_partition *);
151 static void usage(void);
152 static int expand_table(void);
155 main(int argc, char *argv[])
159 while ((c = getopt(argc, argv, "BCEIab:f:p:istuvx1234")) != -1)
213 if (f_flag || i_flag)
221 disk = getdevpath(argv[0], 0);
223 err(1, "cannot open disk %s", disk);
225 err(1, "cannot open disk %s", disk);
226 } else if (disk == NULL) {
229 for(i = 0; disks[i]; i++)
236 err(1, "cannot open any disk");
239 err(1, "cannot open disk %s", disk);
242 /* (abu)use mboot.bootinst to probe for the sector size */
243 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
244 err(1, "cannot allocate buffer to determine disk sector size");
245 if (read_disk(0, mboot.bootinst) == -1)
247 free(mboot.bootinst);
248 errx(1, "could not detect sector size");
250 free(mboot.bootinst);
251 mboot.bootinst = NULL;
256 struct dos_partition *partp;
260 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
262 printf("Part %11s %11s Type Flags\n", "Start", "Size");
263 for (j = 0; j < NDOSPART; j++) {
264 partp = ((struct dos_partition *) &mboot.parts) + j;
265 if (partp->dp_start == 0 && partp->dp_size == 0)
267 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", j + 1,
268 (u_long) partp->dp_start,
269 (u_long) partp->dp_size, partp->dp_typ,
275 printf("******* Working on device %s *******\n",disk);
279 struct dos_partition *partp;
283 partp = (struct dos_partition *) (&mboot.parts[0]);
284 partp->dp_typ = DOSPTYP_DFLYBSD;
285 partp->dp_flag = ACTIVE;
286 partp->dp_start = dos_sectors;
287 if (disksecs - dos_sectors > 0xFFFFFFFFU) {
288 printf("Warning: Ending logical block > 2TB, using max value\n");
289 partp->dp_size = 0xFFFFFFFFU;
292 rounddown(disksecs, dos_cylsecs) - dos_sectors;
299 /* Trim now if we're using the entire device */
304 if (write_s0() == -1)
305 err(1, "can't write fdisk partition table");
310 if (read_s0() || i_flag)
315 if (!read_config(f_flag))
320 x_flag = expand_table();
327 if (write_s0() == -1)
328 err(1, "can't write fdisk partition table");
343 init_sector0(dos_sectors);
345 printf("Media sector size is %d\n", secsize);
346 printf("Warning: BIOS sector numbering starts with sector 1\n");
347 printf("Information from DOS bootblock is:\n");
349 for (i = 1; i <= NDOSPART; i++)
352 change_part(partition);
354 if (u_flag || a_flag)
355 change_active(partition);
361 x_flag = expand_table();
363 if (x_flag || u_flag || a_flag || B_flag) {
365 printf("\nWe haven't changed the partition table yet. ");
366 printf("This is your last chance.\n");
370 if (ok("Should we write new partition table?")) {
371 if (E_flag && u_flag) {
373 * Trim now because we've committed to
374 * updating the partition.
377 for (i = 0; i < NDOSPART; i++)
380 erase_partition(partition);
382 if (write_s0() == -1)
383 err(1, "can't write fdisk partition table");
388 printf("\n-t flag specified -- partition table not written.\n");
399 fprintf(stderr, "%s%s",
400 "usage: fdisk [-BCEIaistu] [-b bootcode] [-p diskimage] [-1234] [disk]\n",
401 " fdisk -f configfile [-itv] [disk]\n");
408 struct dos_partition *part;
409 struct dos_partition *best;
415 for (i = 0; i < NDOSPART; ++i) {
416 part = ((struct dos_partition *) &mboot.parts) + i;
417 if (part->dp_start == 0 && part->dp_size == 0)
419 if (best == NULL || best->dp_start < part->dp_start)
423 if (best->dp_typ == 0xEE || best->dp_typ == 0xEF) {
424 printf("Cannot use fdisk to resize a GPT label\n");
425 printf("use 'gpt expand <device>' instead\n");
429 if (disksecs - best->dp_start > 0xFFFFFFFFU) {
430 if (best->dp_size == 0xFFFFFFFFU) {
431 printf("Last slice already using max value, "
432 "no changes required\n");
435 } else if (best->dp_size == rounddown(disksecs, dos_cylsecs) -
437 printf("Last slice already properly sized, "
438 "no changes required\n");
444 printf("Changing size of last slice %u -> ", best->dp_start);
445 if (disksecs - best->dp_start > 0xFFFFFFFFU) {
446 printf("max-value\n");
447 best->dp_size = 0xFFFFFFFFU;
449 best->dp_size = rounddown(disksecs, dos_cylsecs) -
451 printf("%u\n", best->dp_size);
465 printf("Information from DOS bootblock is:\n");
467 for (i = 1; i <= NDOSPART; i++)
468 printf("%d: ", i), print_part(i);
473 static struct dos_partition mtpart = { 0 };
478 struct dos_partition *partp;
481 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
483 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
484 printf("<UNUSED>\n");
488 * Be careful not to overflow.
490 part_mb = partp->dp_size;
492 part_mb /= (1024 * 1024);
493 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
494 printf(" start %lu, size %lu (%jd Meg), flag %x%s\n",
495 (u_long)partp->dp_start,
496 (u_long)partp->dp_size,
499 partp->dp_flag == ACTIVE ? " (active)" : "");
500 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
501 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
503 ,DPSECT(partp->dp_ssect)
504 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
506 ,DPSECT(partp->dp_esect));
517 fname = b_flag ? b_flag : "/boot/mbr";
518 if ((boot_fd = open(fname, O_RDONLY)) == -1 ||
519 fstat(boot_fd, &sb) == -1)
521 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
524 errx(1, "%s: length must be a multiple of sector size", fname);
526 if (mboot.bootinst != NULL)
527 free(mboot.bootinst);
528 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
531 errx(1, "%s: unable to allocate read buffer", fname);
533 if ((n = read(boot_fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
536 if (n != mboot.bootinst_size)
539 errx(1, "%s: short read", fname);
545 init_sector0(unsigned long start)
547 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
551 partp->dp_typ = DOSPTYP_DFLYBSD;
552 partp->dp_flag = ACTIVE;
553 start = roundup(start, dos_sectors);
556 partp->dp_start = start;
557 if (disksecs - start > 0xFFFFFFFFU) {
558 printf("Warning: Ending logical block > 2TB, using max value\n");
559 partp->dp_size = 0xFFFFFFFFU;
561 partp->dp_size = rounddown(disksecs, dos_cylsecs) - start;
570 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
572 printf("The data for partition %d is:\n", i);
575 if (u_flag && ok("Do you want to change it?")) {
579 bzero((char *)partp, sizeof (struct dos_partition));
582 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
588 Decimal("sysid (108=DragonFly)", partp->dp_typ, tmp);
589 Decimal("start", partp->dp_start, tmp);
590 Decimal("size", partp->dp_size, tmp);
591 if (!sanitize_partition(partp)) {
592 warnx("ERROR: failed to adjust; setting sysid to 0");
596 if (ok("Explicitly specify beg/end address ?"))
599 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
601 tsec = DPSECT(partp->dp_ssect);
602 Decimal("beginning cylinder", tcyl, tmp);
603 Decimal("beginning head", thd, tmp);
604 Decimal("beginning sector", tsec, tmp);
605 if (tcyl > MAXCYL && C_flag == 0) {
606 printf("Warning: starting cylinder wraps, using all 1's\n");
608 partp->dp_ssect = -1;
611 partp->dp_scyl = DOSCYL(tcyl);
612 partp->dp_ssect = DOSSECT(tsec,tcyl);
616 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
618 tsec = DPSECT(partp->dp_esect);
619 Decimal("ending cylinder", tcyl, tmp);
620 Decimal("ending head", thd, tmp);
621 Decimal("ending sector", tsec, tmp);
622 if (tcyl > MAXCYL && C_flag == 0) {
623 printf("Warning: ending cylinder wraps, using all 1's\n");
625 partp->dp_esect = -1;
628 partp->dp_ecyl = DOSCYL(tcyl);
629 partp->dp_esect = DOSSECT(tsec,tcyl);
636 } while (!ok("Are we happy with this entry?"));
643 printf("parameters extracted from device are:\n");
644 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
645 ,cyls,heads,sectors,cylsecs);
646 if ((dos_sectors > MAX_SECTORS_PER_TRACK) || (dos_cyls > MAXCYL) || (dos_heads > MAX_HEADS))
647 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
648 printf("parameters to be used for BIOS calculations are:\n");
649 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
650 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
654 change_active(int which)
656 struct dos_partition *partp = &mboot.parts[0];
657 int active, i, new, tmp;
660 for (i = 0; i < NDOSPART; i++) {
661 if ((partp[i].dp_flag & ACTIVE) == 0)
663 printf("Partition %d is marked active\n", i + 1);
667 if (a_flag && which != -1)
669 else if (active == -1)
672 if (!ok("Do you want to change the active partition?"))
677 Decimal("active partition", new, tmp);
678 if (new < 1 || new > NDOSPART) {
679 printf("Active partition number must be in range 1-4."
684 } while (!ok("Are you happy with this choice"));
685 for (i = 0; i < NDOSPART; i++)
686 partp[i].dp_flag = 0;
687 if (active > 0 && active <= NDOSPART)
688 partp[active-1].dp_flag = ACTIVE;
694 if (ok("Do you want to change the boot code?"))
699 get_params_to_use(void)
703 if (ok("Do you want to change our idea of what BIOS thinks ?"))
707 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
708 Decimal("BIOS's idea of #heads", dos_heads, tmp);
709 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
710 dos_cylsecs = dos_heads * dos_sectors;
713 while(!ok("Are you happy with this choice"));
718 /***********************************************\
719 * Change real numbers into strange dos numbers *
720 \***********************************************/
722 dos(struct dos_partition *partp)
727 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
728 memcpy(partp, &mtpart, sizeof(*partp));
733 cy = partp->dp_start / dos_cylsecs;
734 sec = partp->dp_start % dos_sectors + 1;
735 if (cy > MAXCYL && C_flag == 0) {
736 printf("Warning: starting cylinder wraps, using all 1's\n");
739 partp->dp_ssect = -1;
741 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
742 partp->dp_scyl = DOSCYL(cy);
743 partp->dp_ssect = DOSSECT(sec, cy);
747 end = partp->dp_start + partp->dp_size - 1;
748 cy = end / dos_cylsecs;
749 sec = end % dos_sectors + 1;
750 if (cy > MAXCYL && C_flag == 0) {
751 printf("Warning: ending cylinder wraps, using all 1's\n");
754 partp->dp_esect = -1;
756 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
757 partp->dp_ecyl = DOSCYL(cy);
758 partp->dp_esect = DOSSECT(sec, cy);
763 erase_partition(int i)
765 struct dos_partition *partp;
768 char *dev_name = strdup(disk);
770 dev_name = strtok(dev_name + strlen("/dev/da"),"s");
772 int trim_enabled = 0;
773 char sysctl_name[64];
774 size_t olen = sizeof(trim_enabled);
776 sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled", dev_name);
777 if (sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0) < 0) {
778 printf("Device:%s does not support the TRIM command\n", disk);
782 printf("Erase device option selected, but sysctl (%s) "
783 "is not enabled\n",sysctl_name);
787 partp = ((struct dos_partition *) &mboot.parts) + i;
788 printf("erase sectors:%u %u\n",
792 /* Trim the Device */
793 ioarg[0] = partp->dp_start;
795 ioarg[1] = partp->dp_size;
798 if (ioctl(fd, DAIOCTRIM, ioarg) < 0) {
799 printf("Device trim failed\n");
804 /* Getting device status */
811 if (stat(disk, &st) == -1) {
814 warnx("can't get file status of %s", disk);
817 if (!(st.st_mode & S_IFCHR) && p_flag == 0)
818 warnx("device %s is not character special", disk);
820 (x_flag || a_flag || I_flag ||
821 B_flag || u_flag) ? O_RDWR : O_RDONLY)) == -1) {
824 warnx("can't open device %s", disk);
827 if (get_params() == -1) {
828 warnx("can't get disk parameters on %s", disk);
835 read_disk(off_t sector, void *buf)
837 lseek(fd,(sector * 512), 0);
839 for(secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2)
842 int size = read(fd, buf, secsize);
844 /* it worked so return */
848 return read(fd, buf, secsize);
850 /* we failed to read at any of the sizes */
855 write_disk(off_t sector, void *buf)
857 lseek(fd,(sector * 512), 0);
858 /* write out in the size that the read_disk found worked */
859 return write(fd, buf, secsize);
865 struct partinfo partinfo; /* disk parameters */
869 * NOTE: When faking up the CHS for a file image (e.g. for USB),
870 * we must use max values. If we do not then an overflowed
871 * cylinder count will, by convention, set the CHS fields to
872 * all 1's. The heads and sectors in the CHS fields will then
873 * exceed the basic geometry which can cause BIOSes to brick.
875 if (ioctl(fd, DIOCGPART, &partinfo) == -1) {
876 if (p_flag && fstat(fd, &st) == 0 && st.st_size) {
877 sectors = MAX_SECTORS_PER_TRACK;
879 cylsecs = heads * sectors;
880 cyls = st.st_size / 512 / cylsecs;
882 warnx("can't get disk parameters on %s; supplying dummy ones",
885 cylsecs = heads * sectors;
888 cyls = partinfo.d_ncylinders;
889 heads = partinfo.d_nheads;
890 sectors = partinfo.d_secpertrack;
891 cylsecs = heads * sectors;
892 secsize = partinfo.media_blksize;
896 dos_sectors = sectors;
897 dos_cylsecs = cylsecs;
898 disksecs = (int64_t)cyls * heads * sectors;
906 mboot.bootinst_size = secsize;
907 if (mboot.bootinst != NULL)
908 free(mboot.bootinst);
909 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
910 warnx("unable to allocate buffer to read fdisk "
914 if (read_disk(0, mboot.bootinst) == -1) {
915 warnx("can't read fdisk partition table");
918 if (*(uint16_t *)&mboot.bootinst[DOSMAGICOFF] != DOSMAGIC) {
919 warnx("invalid fdisk partition table found");
920 /* So should we initialize things */
923 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
935 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
937 * write enable label sector before write (if necessary),
938 * disable after writing.
939 * needed if the disklabel protected area also protects
940 * sector 0. (e.g. empty disk)
943 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
944 warn("ioctl DIOCWLABEL");
946 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
947 if (write_disk(sector,
948 &mboot.bootinst[sector * secsize]) == -1) {
949 warn("can't write fdisk partition table");
952 ioctl(fd, DIOCWLABEL, &flag);
958 ioctl(fd, DIOCWLABEL, &flag);
967 printf("%s [n] ", str);
969 if (fgets(lbuf, LBUF, stdin) == NULL)
971 lbuf[strlen(lbuf)-1] = 0;
974 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
975 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
982 decimal(const char *str, int *num, int deflt)
988 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
990 if (fgets(lbuf, LBUF, stdin) == NULL)
992 lbuf[strlen(lbuf)-1] = 0;
998 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1001 while ((c = *cp++)) {
1002 if (c <= '9' && c >= '0')
1003 acc = acc * 10 + c - '0';
1007 if (c == ' ' || c == '\t')
1008 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1013 printf("%s is an invalid decimal number. Try again.\n",
1022 int numentries = NELEM(dos_ptypes);
1024 const struct dos_ptype *ptr = dos_ptypes;
1027 while(counter < numentries)
1029 if (ptr->type == type)
1041 parse_config_line(char *line, CMD *command)
1046 while (1) /* dirty trick used to insure one exit point for this
1049 memset(command, 0, sizeof(*command));
1051 while (isspace(*cp)) ++cp;
1052 if (*cp == '\0' || *cp == '#')
1056 command->cmd = *cp++;
1063 while (isspace(*cp)) ++cp;
1066 break; /* found comment */
1070 command->args[command->n_args].argtype = *cp++;
1074 break; /* assume end of line */
1077 command->args[command->n_args].arg_val = strtoll(cp, &end, 0);
1080 break; /* couldn't parse number */
1091 process_geometry(CMD *command)
1101 "ERROR line %d: the geometry specification line must occur before\n\
1102 all partition specifications",
1103 current_line_number);
1107 if (command->n_args != 3)
1109 warnx("ERROR line %d: incorrect number of geometry args",
1110 current_line_number);
1117 for (i = 0; i < 3; ++i)
1119 switch (command->args[i].argtype)
1122 dos_cyls = command->args[i].arg_val;
1125 dos_heads = command->args[i].arg_val;
1128 dos_sectors = command->args[i].arg_val;
1132 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1133 current_line_number, command->args[i].argtype,
1134 command->args[i].argtype);
1144 dos_cylsecs = dos_heads * dos_sectors;
1147 * Do sanity checks on parameter values
1151 warnx("ERROR line %d: number of cylinders not specified",
1152 current_line_number);
1155 if (dos_cyls == 0 || dos_cyls > 1024)
1158 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1159 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1160 is dedicated to DragonFly)",
1161 current_line_number, dos_cyls);
1166 warnx("ERROR line %d: number of heads not specified",
1167 current_line_number);
1170 else if (dos_heads < 1 || dos_heads > 256)
1172 warnx("ERROR line %d: number of heads must be within (1-256)",
1173 current_line_number);
1177 if (dos_sectors < 0)
1179 warnx("ERROR line %d: number of sectors not specified",
1180 current_line_number);
1183 else if (dos_sectors < MIN_SECTORS_PER_TRACK || dos_sectors > MAX_SECTORS_PER_TRACK)
1185 warnx("ERROR line %d: number of sectors must be within (1-63)",
1186 current_line_number);
1197 process_partition(CMD *command)
1199 int status = 0, part;
1200 uint32_t prev_head_boundary, prev_cyl_boundary;
1201 uint32_t adj_size, max_end;
1202 struct dos_partition *partp;
1207 if (command->n_args != 4)
1209 warnx("ERROR line %d: incorrect number of partition args",
1210 current_line_number);
1213 part = command->args[0].arg_val;
1214 if (part < 1 || part > NDOSPART)
1216 warnx("ERROR line %d: invalid partition number %d",
1217 current_line_number, part);
1220 partp = ((struct dos_partition *) &mboot.parts) + part - 1;
1221 bzero((char *)partp, sizeof (struct dos_partition));
1222 partp->dp_typ = command->args[1].arg_val;
1223 partp->dp_start = command->args[2].arg_val;
1224 partp->dp_size = command->args[3].arg_val;
1225 max_end = partp->dp_start + partp->dp_size;
1227 if (partp->dp_typ == 0)
1230 * Get out, the partition is marked as unused.
1233 * Insure that it's unused.
1235 bzero((char *)partp, sizeof (struct dos_partition));
1241 * Adjust start upwards, if necessary, to fall on an head boundary.
1243 if (partp->dp_start % dos_sectors != 0)
1245 prev_head_boundary = rounddown(partp->dp_start, dos_sectors);
1246 if (max_end < (uint32_t)dos_sectors ||
1247 prev_head_boundary > max_end - dos_sectors)
1250 * Can't go past end of partition
1253 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1255 current_line_number, part);
1259 "WARNING: adjusting start offset of partition %d\n\
1260 from %u to %u, to fall on a head boundary",
1261 part, (u_int)partp->dp_start,
1262 (u_int)(prev_head_boundary + dos_sectors));
1263 partp->dp_start = prev_head_boundary + dos_sectors;
1267 * Adjust size downwards, if necessary, to fall on a cylinder
1271 rounddown(partp->dp_start + partp->dp_size, dos_cylsecs);
1272 if (prev_cyl_boundary > partp->dp_start)
1273 adj_size = prev_cyl_boundary - partp->dp_start;
1277 "ERROR: could not adjust partition to start on a head boundary\n\
1278 and end on a cylinder boundary.");
1281 if (adj_size != partp->dp_size)
1284 "WARNING: adjusting size of partition %d from %u to %u\n\
1285 to end on a cylinder boundary",
1286 part, (u_int)partp->dp_size, (u_int)adj_size);
1287 partp->dp_size = adj_size;
1289 if (partp->dp_size == 0)
1291 warnx("ERROR line %d: size of partition %d is zero",
1292 current_line_number, part);
1305 process_active(CMD *command)
1307 int status = 0, part, i;
1308 struct dos_partition *partp;
1312 active_processed = 1;
1313 if (command->n_args != 1)
1315 warnx("ERROR line %d: incorrect number of active args",
1316 current_line_number);
1320 part = command->args[0].arg_val;
1321 if (part < 1 || part > NDOSPART)
1323 warnx("ERROR line %d: invalid partition number %d",
1324 current_line_number, part);
1328 * Reset active partition
1330 partp = ((struct dos_partition *) &mboot.parts);
1331 for (i = 0; i < NDOSPART; i++)
1332 partp[i].dp_flag = 0;
1333 partp[part-1].dp_flag = ACTIVE;
1343 process_line(char *line)
1350 parse_config_line(line, &command);
1351 switch (command.cmd)
1355 * Comment or blank line
1362 status = process_geometry(&command);
1365 status = process_partition(&command);
1368 status = process_active(&command);
1381 read_config(char *config_file)
1387 while (1) /* dirty trick used to insure one exit point for this
1390 if (strcmp(config_file, "-") != 0)
1393 * We're not reading from stdin
1395 if ((fp = fopen(config_file, "r")) == NULL)
1405 current_line_number = 0;
1408 if (fgets(buf, sizeof(buf), fp) == NULL)
1412 ++current_line_number;
1413 status = process_line(buf);
1424 * It doesn't matter if we're reading from stdin, as we've reached EOF
1436 struct dos_partition *partp;
1439 for (i = 0; i < NDOSPART; ++i)
1441 partp = ((struct dos_partition *) &mboot.parts) + i;
1442 bzero((char *)partp, sizeof (struct dos_partition));
1447 sanitize_partition(struct dos_partition *partp)
1449 uint32_t prev_head_boundary, prev_cyl_boundary;
1450 uint32_t max_end, size, start;
1452 start = partp->dp_start;
1453 size = partp->dp_size;
1454 max_end = start + size;
1455 /* Only allow a zero size if the partition is being marked unused. */
1457 if (start == 0 && partp->dp_typ == 0)
1459 warnx("ERROR: size of partition is zero");
1462 /* Return if no adjustment is necessary. */
1463 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1467 warnx("WARNING: partition overlaps with partition table");
1468 if (ok("Correct this automatically?"))
1469 start = dos_sectors;
1471 if (start % dos_sectors != 0)
1472 warnx("WARNING: partition does not start on a head boundary");
1473 if ((start +size) % dos_sectors != 0)
1474 warnx("WARNING: partition does not end on a cylinder boundary");
1475 warnx("WARNING: this may confuse the BIOS or some operating systems");
1476 if (!ok("Correct this automatically?"))
1480 * Adjust start upwards, if necessary, to fall on an head boundary.
1482 if (start % dos_sectors != 0) {
1483 prev_head_boundary = rounddown(start, dos_sectors);
1484 if (max_end < (uint32_t)dos_sectors ||
1485 prev_head_boundary >= max_end - dos_sectors) {
1487 * Can't go past end of partition
1490 "ERROR: unable to adjust start of partition to fall on a head boundary");
1493 start = prev_head_boundary + dos_sectors;
1497 * Adjust size downwards, if necessary, to fall on a cylinder
1500 prev_cyl_boundary = rounddown(start + size, dos_cylsecs);
1501 if (prev_cyl_boundary > start)
1502 size = prev_cyl_boundary - start;
1504 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1505 and end on a cylinder boundary.");
1509 /* Finally, commit any changes to partp and return. */
1510 if (start != partp->dp_start) {
1511 warnx("WARNING: adjusting start offset of partition to %u",
1513 partp->dp_start = start;
1515 if (size != partp->dp_size) {
1516 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1517 partp->dp_size = size;