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: src/sbin/i386/fdisk/fdisk.c,v 1.36.2.11 2002/04/25 21:02:21 trhodes Exp $
27 * $DragonFly: src/sbin/i386/fdisk/fdisk.c,v 1.4 2003/10/12 00:43:17 dillon Exp $
30 #include <sys/disklabel.h>
44 static char lbuf[LBUF];
50 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
52 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
53 * Copyright (c) 1989 Robert. V. Baron
57 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
58 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
59 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
61 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
63 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
64 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
65 int secsize = 0; /* the sensed sector size */
70 "/dev/ad0", "/dev/da0", 0
73 struct disklabel disklabel; /* disk parameters */
75 int cyls, sectors, heads, cylsecs, disksecs;
79 unsigned char padding[2]; /* force the longs to be long aligned */
80 unsigned char *bootinst; /* boot code */
82 struct dos_partition parts[4];
84 struct mboot mboot = {{0}, NULL, 0};
87 #define BOOT_MAGIC 0xAA55
94 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
95 #define DOSCYL(c) (c & 0xff)
96 static int partition = -1;
101 static int current_line_number;
103 static int geom_processed = 0;
104 static int part_processed = 0;
105 static int active_processed = 0;
118 static int B_flag = 0; /* replace boot code */
119 static int I_flag = 0; /* use entire disk for FreeBSD */
120 static int a_flag = 0; /* set active partition */
121 static char *b_flag = NULL; /* path to boot code */
122 static int i_flag = 0; /* replace partition data */
123 static int u_flag = 0; /* update partition data */
124 static int p_flag = 0; /* operate on a disk image file */
125 static int s_flag = 0; /* Print a summary and exit */
126 static int t_flag = 0; /* test only */
127 static char *f_flag = NULL; /* Read config info from file */
128 static int v_flag = 0; /* Be verbose */
137 ,{0x01, "Primary DOS with 12 bit FAT"}
138 ,{0x02, "XENIX / filesystem"}
139 ,{0x03, "XENIX /usr filesystem"}
140 ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
141 ,{0x05, "Extended DOS"}
142 ,{0x06, "Primary 'big' DOS (> 32MB)"}
143 ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
144 ,{0x08, "AIX filesystem"}
145 ,{0x09, "AIX boot partition or Coherent"}
146 ,{0x0A, "OS/2 Boot Manager or OPUS"}
147 ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
148 ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
149 ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
150 ,{0x0F, "Extended DOS, LBA"}
154 ,{0x4D, "QNX 4.2 Primary"}
155 ,{0x4E, "QNX 4.2 Secondary"}
156 ,{0x4F, "QNX 4.2 Tertiary"}
159 ,{0x52, "CP/M or Microport SysV/AT"}
162 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
163 ,{0x64, "Novell Netware 2.xx"}
164 ,{0x65, "Novell Netware 3.xx"}
166 ,{0x80, "Minix 1.1 ... 1.4a"}
167 ,{0x81, "Minix 1.4b ... 1.5.10"}
168 ,{0x82, "Linux swap or Solaris x86"}
169 ,{0x83, "Linux filesystem"}
170 ,{0x93, "Amoeba filesystem"}
171 ,{0x94, "Amoeba bad block table"}
173 ,{0xA0, "Suspend to Disk"}
174 ,{0xA5, "FreeBSD/NetBSD/386BSD"}
178 ,{0xB7, "BSDI BSD/386 filesystem"}
179 ,{0xB8, "BSDI BSD/386 swap"}
180 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
185 ,{0xF2, "DOS 3.3+ Secondary"}
187 ,{0xFF, "BBT (Bad Blocks Table)"}
190 static void print_s0(int which);
191 static void print_part(int i);
192 static void init_sector0(unsigned long start);
193 static void init_boot(void);
194 static void change_part(int i);
195 static void print_params();
196 static void change_active(int which);
197 static void change_code();
198 static void get_params_to_use();
199 static void dos(struct dos_partition *partp);
200 static int open_disk(int u_flag);
201 static ssize_t read_disk(off_t sector, void *buf);
202 static ssize_t write_disk(off_t sector, void *buf);
203 static int get_params();
204 static int read_s0();
205 static int write_s0();
206 static int ok(char *str);
207 static int decimal(char *str, int *num, int deflt);
208 static char *get_type(int type);
209 static int read_config(char *config_file);
210 static void reset_boot(void);
211 static int sanitize_partition(struct dos_partition *);
212 static void usage(void);
214 static int hex(char *str, int *num, int deflt);
215 static int string(char *str, char **ans);
220 main(int argc, char *argv[])
224 while ((c = getopt(argc, argv, "BIab:f:p:istuv1234")) != -1)
269 if (f_flag || i_flag)
277 static char realname[12];
279 if(strncmp(argv[0], "/dev", 4) == 0) {
282 snprintf(realname, 12, "/dev/%s", argv[0]);
286 if (open_disk(u_flag) < 0)
287 err(1, "cannot open disk %s", disk);
288 } else if (disk == NULL) {
291 for(i = 0; disks[i]; i++)
294 rv = open_disk(u_flag);
298 err(1, "cannot open any disk");
300 if (open_disk(u_flag) < 0)
301 err(1, "cannot open disk %s", disk);
304 /* (abu)use mboot.bootinst to probe for the sector size */
305 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
306 err(1, "cannot allocate buffer to determine disk sector size");
307 read_disk(0, mboot.bootinst);
308 free(mboot.bootinst);
309 mboot.bootinst = NULL;
314 struct dos_partition *partp;
318 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
320 printf("Part %11s %11s Type Flags\n", "Start", "Size");
321 for (i = 0; i < NDOSPART; i++) {
322 partp = ((struct dos_partition *) &mboot.parts) + i;
323 if (partp->dp_start == 0 && partp->dp_size == 0)
325 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
326 (u_long) partp->dp_start,
327 (u_long) partp->dp_size, partp->dp_typ,
333 printf("******* Working on device %s *******\n",disk);
337 struct dos_partition *partp;
341 partp = (struct dos_partition *) (&mboot.parts[0]);
342 partp->dp_typ = DOSPTYP_386BSD;
343 partp->dp_flag = ACTIVE;
344 partp->dp_start = dos_sectors;
345 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
356 if (read_s0() || i_flag)
361 if (!read_config(f_flag))
386 init_sector0(dos_sectors);
388 printf("Media sector size is %d\n", secsize);
389 printf("Warning: BIOS sector numbering starts with sector 1\n");
390 printf("Information from DOS bootblock is:\n");
392 for (i = 1; i <= NDOSPART; i++)
395 change_part(partition);
397 if (u_flag || a_flag)
398 change_active(partition);
403 if (u_flag || a_flag || B_flag) {
405 printf("\nWe haven't changed the partition table yet. ");
406 printf("This is your last chance.\n");
410 if (ok("Should we write new partition table?"))
415 printf("\n-t flag specified -- partition table not written.\n");
426 fprintf(stderr, "%s%s",
427 "usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
428 " fdisk -f configfile [-itv] [disk]\n");
438 printf("Information from DOS bootblock is:\n");
440 for (i = 1; i <= NDOSPART; i++)
441 printf("%d: ", i), print_part(i);
446 static struct dos_partition mtpart = { 0 };
451 struct dos_partition *partp;
454 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
456 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
457 printf("<UNUSED>\n");
461 * Be careful not to overflow.
463 part_mb = partp->dp_size;
465 part_mb /= (1024 * 1024);
466 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
467 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
468 (u_long)partp->dp_start,
469 (u_long)partp->dp_size,
472 partp->dp_flag == ACTIVE ? " (active)" : "");
473 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
474 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
476 ,DPSECT(partp->dp_ssect)
477 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
479 ,DPSECT(partp->dp_esect));
490 fname = b_flag ? b_flag : "/boot/mbr";
491 if ((fd = open(fname, O_RDONLY)) == -1 ||
492 fstat(fd, &sb) == -1)
494 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
495 errx(1, "%s: length must be a multiple of sector size", fname);
496 if (mboot.bootinst != NULL)
497 free(mboot.bootinst);
498 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
499 errx(1, "%s: unable to allocate read buffer", fname);
500 if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
503 if (n != mboot.bootinst_size)
504 errx(1, "%s: short read", fname);
509 init_sector0(unsigned long start)
511 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
515 partp->dp_typ = DOSPTYP_386BSD;
516 partp->dp_flag = ACTIVE;
517 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
520 partp->dp_start = start;
521 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
529 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
531 printf("The data for partition %d is:\n", i);
534 if (u_flag && ok("Do you want to change it?")) {
538 bzero((char *)partp, sizeof (struct dos_partition));
541 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
547 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
548 Decimal("start", partp->dp_start, tmp);
549 Decimal("size", partp->dp_size, tmp);
550 if (!sanitize_partition(partp)) {
551 warnx("ERROR: failed to adjust; setting sysid to 0");
555 if (ok("Explicitly specify beg/end address ?"))
558 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
560 tsec = DPSECT(partp->dp_ssect);
561 Decimal("beginning cylinder", tcyl, tmp);
562 Decimal("beginning head", thd, tmp);
563 Decimal("beginning sector", tsec, tmp);
564 partp->dp_scyl = DOSCYL(tcyl);
565 partp->dp_ssect = DOSSECT(tsec,tcyl);
568 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
570 tsec = DPSECT(partp->dp_esect);
571 Decimal("ending cylinder", tcyl, tmp);
572 Decimal("ending head", thd, tmp);
573 Decimal("ending sector", tsec, tmp);
574 partp->dp_ecyl = DOSCYL(tcyl);
575 partp->dp_esect = DOSSECT(tsec,tcyl);
581 } while (!ok("Are we happy with this entry?"));
588 printf("parameters extracted from in-core disklabel are:\n");
589 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
590 ,cyls,heads,sectors,cylsecs);
591 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
592 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
593 printf("parameters to be used for BIOS calculations are:\n");
594 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
595 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
599 change_active(int which)
601 struct dos_partition *partp = &mboot.parts[0];
602 int active, i, new, tmp;
605 for (i = 0; i < NDOSPART; i++) {
606 if ((partp[i].dp_flag & ACTIVE) == 0)
608 printf("Partition %d is marked active\n", i + 1);
612 if (a_flag && which != -1)
614 else if (active == -1)
617 if (!ok("Do you want to change the active partition?"))
622 Decimal("active partition", new, tmp);
623 if (new < 1 || new > 4) {
624 printf("Active partition number must be in range 1-4."
629 } while (!ok("Are you happy with this choice"));
630 for (i = 0; i < NDOSPART; i++)
631 partp[i].dp_flag = 0;
632 if (active > 0 && active <= NDOSPART)
633 partp[active-1].dp_flag = ACTIVE;
639 if (ok("Do you want to change the boot code?"))
644 get_params_to_use(void)
648 if (ok("Do you want to change our idea of what BIOS thinks ?"))
652 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
653 Decimal("BIOS's idea of #heads", dos_heads, tmp);
654 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
655 dos_cylsecs = dos_heads * dos_sectors;
658 while(!ok("Are you happy with this choice"));
663 /***********************************************\
664 * Change real numbers into strange dos numbers *
665 \***********************************************/
668 struct dos_partition *partp;
673 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
674 memcpy(partp, &mtpart, sizeof(*partp));
679 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
680 cy = partp->dp_start / dos_cylsecs;
681 sec = partp->dp_start % dos_sectors + 1;
682 partp->dp_scyl = DOSCYL(cy);
683 partp->dp_ssect = DOSSECT(sec, cy);
686 end = partp->dp_start + partp->dp_size - 1;
687 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
688 cy = end / dos_cylsecs;
689 sec = end % dos_sectors + 1;
690 partp->dp_ecyl = DOSCYL(cy);
691 partp->dp_esect = DOSSECT(sec, cy);
696 /* Getting device status */
699 open_disk(int u_flag)
703 if (stat(disk, &st) == -1) {
706 warnx("can't get file status of %s", disk);
709 if ( !(st.st_mode & S_IFCHR) && p_flag == 0 )
710 warnx("device %s is not character special", disk);
712 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
715 warnx("can't open device %s", disk);
718 if (get_params() == -1) {
719 warnx("can't get disk parameters on %s", disk);
726 read_disk(off_t sector, void *buf)
728 lseek(fd,(sector * 512), 0);
730 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
733 int size = read(fd, buf, secsize);
734 if( size == secsize )
735 /* it worked so return */
739 return read( fd, buf, secsize );
741 /* we failed to read at any of the sizes */
746 write_disk(off_t sector, void *buf)
748 lseek(fd,(sector * 512), 0);
749 /* write out in the size that the read_disk found worked */
750 return write(fd, buf, secsize);
758 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
759 if (p_flag && fstat(fd, &st) == 0 && st.st_size) {
762 cylsecs = heads * sectors;
763 cyls = st.st_size / 512 / cylsecs;
765 warnx("can't get disk parameters on %s; supplying dummy ones", disk);
769 cylsecs = heads * sectors;
772 cyls = disklabel.d_ncylinders;
773 heads = disklabel.d_ntracks;
774 sectors = disklabel.d_nsectors;
775 cylsecs = heads * sectors;
779 dos_sectors = sectors;
780 dos_cylsecs = cylsecs;
781 disksecs = cyls * heads * sectors;
789 mboot.bootinst_size = secsize;
790 if (mboot.bootinst != NULL)
791 free(mboot.bootinst);
792 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
793 warnx("unable to allocate buffer to read fdisk "
797 if (read_disk(0, mboot.bootinst) == -1) {
798 warnx("can't read fdisk partition table");
801 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
802 warnx("invalid fdisk partition table found");
803 /* So should we initialize things */
806 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
822 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
824 * write enable label sector before write (if necessary),
825 * disable after writing.
826 * needed if the disklabel protected area also protects
827 * sector 0. (e.g. empty disk)
831 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
832 warn("ioctl DIOCWLABEL");
834 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
835 if (write_disk(sector,
836 &mboot.bootinst[sector * secsize]) == -1) {
837 warn("can't write fdisk partition table");
841 (void) ioctl(fd, DIOCWLABEL, &flag);
846 (void) ioctl(fd, DIOCWLABEL, &flag);
856 printf("%s [n] ", str);
858 if (fgets(lbuf, LBUF, stdin) == NULL)
860 lbuf[strlen(lbuf)-1] = 0;
863 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
864 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
871 decimal(char *str, int *num, int deflt)
877 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
879 if (fgets(lbuf, LBUF, stdin) == NULL)
881 lbuf[strlen(lbuf)-1] = 0;
887 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
890 while ((c = *cp++)) {
891 if (c <= '9' && c >= '0')
892 acc = acc * 10 + c - '0';
896 if (c == ' ' || c == '\t')
897 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
902 printf("%s is an invalid decimal number. Try again.\n",
910 hex(char *str, int *num, int deflt)
916 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
917 fgets(lbuf, LBUF, stdin);
918 lbuf[strlen(lbuf)-1] = 0;
924 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
927 while ((c = *cp++)) {
928 if (c <= '9' && c >= '0')
929 acc = (acc << 4) + c - '0';
930 else if (c <= 'f' && c >= 'a')
931 acc = (acc << 4) + c - 'a' + 10;
932 else if (c <= 'F' && c >= 'A')
933 acc = (acc << 4) + c - 'A' + 10;
937 if (c == ' ' || c == '\t')
938 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
943 printf("%s is an invalid hex number. Try again.\n",
950 string(char *str, char **ans)
956 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
957 fgets(lbuf, LBUF, stdin);
958 lbuf[strlen(lbuf)-1] = 0;
963 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
967 while ((c = *cp) && c != '"') cp++;
970 while ((c = *cp) && c != ' ' && c != '\t') cp++;
983 int numentries = (sizeof(part_types)/sizeof(struct part_type));
985 struct part_type *ptr = part_types;
988 while(counter < numentries)
990 if(ptr->type == type)
1002 parse_config_line(line, command)
1009 while (1) /* dirty trick used to insure one exit point for this
1012 memset(command, 0, sizeof(*command));
1014 while (isspace(*cp)) ++cp;
1015 if (*cp == '\0' || *cp == '#')
1019 command->cmd = *cp++;
1026 while (isspace(*cp)) ++cp;
1029 break; /* found comment */
1033 command->args[command->n_args].argtype = *cp++;
1037 break; /* assume end of line */
1040 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1043 break; /* couldn't parse number */
1054 process_geometry(command)
1065 "ERROR line %d: the geometry specification line must occur before\n\
1066 all partition specifications",
1067 current_line_number);
1071 if (command->n_args != 3)
1073 warnx("ERROR line %d: incorrect number of geometry args",
1074 current_line_number);
1081 for (i = 0; i < 3; ++i)
1083 switch (command->args[i].argtype)
1086 dos_cyls = command->args[i].arg_val;
1089 dos_heads = command->args[i].arg_val;
1092 dos_sectors = command->args[i].arg_val;
1096 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1097 current_line_number, command->args[i].argtype,
1098 command->args[i].argtype);
1108 dos_cylsecs = dos_heads * dos_sectors;
1111 * Do sanity checks on parameter values
1115 warnx("ERROR line %d: number of cylinders not specified",
1116 current_line_number);
1119 if (dos_cyls == 0 || dos_cyls > 1024)
1122 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1123 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1124 is dedicated to FreeBSD)",
1125 current_line_number, dos_cyls);
1130 warnx("ERROR line %d: number of heads not specified",
1131 current_line_number);
1134 else if (dos_heads < 1 || dos_heads > 256)
1136 warnx("ERROR line %d: number of heads must be within (1-256)",
1137 current_line_number);
1141 if (dos_sectors < 0)
1143 warnx("ERROR line %d: number of sectors not specified",
1144 current_line_number);
1147 else if (dos_sectors < 1 || dos_sectors > 63)
1149 warnx("ERROR line %d: number of sectors must be within (1-63)",
1150 current_line_number);
1161 process_partition(command)
1164 int status = 0, partition;
1165 u_int32_t prev_head_boundary, prev_cyl_boundary;
1166 u_int32_t adj_size, max_end;
1167 struct dos_partition *partp;
1172 if (command->n_args != 4)
1174 warnx("ERROR line %d: incorrect number of partition args",
1175 current_line_number);
1178 partition = command->args[0].arg_val;
1179 if (partition < 1 || partition > 4)
1181 warnx("ERROR line %d: invalid partition number %d",
1182 current_line_number, partition);
1185 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1186 bzero((char *)partp, sizeof (struct dos_partition));
1187 partp->dp_typ = command->args[1].arg_val;
1188 partp->dp_start = command->args[2].arg_val;
1189 partp->dp_size = command->args[3].arg_val;
1190 max_end = partp->dp_start + partp->dp_size;
1192 if (partp->dp_typ == 0)
1195 * Get out, the partition is marked as unused.
1198 * Insure that it's unused.
1200 bzero((char *)partp, sizeof (struct dos_partition));
1206 * Adjust start upwards, if necessary, to fall on an head boundary.
1208 if (partp->dp_start % dos_sectors != 0)
1210 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1211 if (max_end < dos_sectors ||
1212 prev_head_boundary > max_end - dos_sectors)
1215 * Can't go past end of partition
1218 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1220 current_line_number, partition);
1224 "WARNING: adjusting start offset of partition %d\n\
1225 from %u to %u, to fall on a head boundary",
1226 partition, (u_int)partp->dp_start,
1227 (u_int)(prev_head_boundary + dos_sectors));
1228 partp->dp_start = prev_head_boundary + dos_sectors;
1232 * Adjust size downwards, if necessary, to fall on a cylinder
1236 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1237 if (prev_cyl_boundary > partp->dp_start)
1238 adj_size = prev_cyl_boundary - partp->dp_start;
1242 "ERROR: could not adjust partition to start on a head boundary\n\
1243 and end on a cylinder boundary.");
1246 if (adj_size != partp->dp_size)
1249 "WARNING: adjusting size of partition %d from %u to %u\n\
1250 to end on a cylinder boundary",
1251 partition, (u_int)partp->dp_size, (u_int)adj_size);
1252 partp->dp_size = adj_size;
1254 if (partp->dp_size == 0)
1256 warnx("ERROR line %d: size of partition %d is zero",
1257 current_line_number, partition);
1270 process_active(command)
1273 int status = 0, partition, i;
1274 struct dos_partition *partp;
1278 active_processed = 1;
1279 if (command->n_args != 1)
1281 warnx("ERROR line %d: incorrect number of active args",
1282 current_line_number);
1286 partition = command->args[0].arg_val;
1287 if (partition < 1 || partition > 4)
1289 warnx("ERROR line %d: invalid partition number %d",
1290 current_line_number, partition);
1294 * Reset active partition
1296 partp = ((struct dos_partition *) &mboot.parts);
1297 for (i = 0; i < NDOSPART; i++)
1298 partp[i].dp_flag = 0;
1299 partp[partition-1].dp_flag = ACTIVE;
1317 parse_config_line(line, &command);
1318 switch (command.cmd)
1322 * Comment or blank line
1329 status = process_geometry(&command);
1332 status = process_partition(&command);
1335 status = process_active(&command);
1348 read_config(config_file)
1355 while (1) /* dirty trick used to insure one exit point for this
1358 if (strcmp(config_file, "-") != 0)
1361 * We're not reading from stdin
1363 if ((fp = fopen(config_file, "r")) == NULL)
1373 current_line_number = 0;
1376 if (fgets(buf, sizeof(buf), fp) == NULL)
1380 ++current_line_number;
1381 status = process_line(buf);
1392 * It doesn't matter if we're reading from stdin, as we've reached EOF
1404 struct dos_partition *partp;
1407 for (i = 0; i < 4; ++i)
1409 partp = ((struct dos_partition *) &mboot.parts) + i;
1410 bzero((char *)partp, sizeof (struct dos_partition));
1415 sanitize_partition(partp)
1416 struct dos_partition *partp;
1418 u_int32_t prev_head_boundary, prev_cyl_boundary;
1419 u_int32_t max_end, size, start;
1421 start = partp->dp_start;
1422 size = partp->dp_size;
1423 max_end = start + size;
1424 /* Only allow a zero size if the partition is being marked unused. */
1426 if (start == 0 && partp->dp_typ == 0)
1428 warnx("ERROR: size of partition is zero");
1431 /* Return if no adjustment is necessary. */
1432 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1435 if (start % dos_sectors != 0)
1436 warnx("WARNING: partition does not start on a head boundary");
1437 if ((start +size) % dos_sectors != 0)
1438 warnx("WARNING: partition does not end on a cylinder boundary");
1439 warnx("WARNING: this may confuse the BIOS or some operating systems");
1440 if (!ok("Correct this automatically?"))
1444 * Adjust start upwards, if necessary, to fall on an head boundary.
1446 if (start % dos_sectors != 0) {
1447 prev_head_boundary = start / dos_sectors * dos_sectors;
1448 if (max_end < dos_sectors ||
1449 prev_head_boundary >= max_end - dos_sectors) {
1451 * Can't go past end of partition
1454 "ERROR: unable to adjust start of partition to fall on a head boundary");
1457 start = prev_head_boundary + dos_sectors;
1461 * Adjust size downwards, if necessary, to fall on a cylinder
1464 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1465 if (prev_cyl_boundary > start)
1466 size = prev_cyl_boundary - start;
1468 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1469 and end on a cylinder boundary.");
1473 /* Finally, commit any changes to partp and return. */
1474 if (start != partp->dp_start) {
1475 warnx("WARNING: adjusting start offset of partition to %u",
1477 partp->dp_start = start;
1479 if (size != partp->dp_size) {
1480 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1481 partp->dp_size = size;