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.6 2004/02/03 07:13:10 rob Exp $
30 #include <sys/disklabel.h>
31 #include <sys/diskmbr.h>
45 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 Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
60 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
62 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
64 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
65 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
66 int secsize = 0; /* the sensed sector size */
71 "/dev/ad0", "/dev/da0", 0
74 struct disklabel disklabel; /* disk parameters */
76 int cyls, sectors, heads, cylsecs, disksecs;
80 unsigned char padding[2]; /* force the longs to be long aligned */
81 unsigned char *bootinst; /* boot code */
83 struct dos_partition parts[4];
85 struct mboot mboot = {{0}, NULL, 0};
88 #define BOOT_MAGIC 0xAA55
95 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
96 #define DOSCYL(c) (c & 0xff)
97 static int partition = -1;
102 static int current_line_number;
104 static int geom_processed = 0;
105 static int part_processed = 0;
106 static int active_processed = 0;
119 static int B_flag = 0; /* replace boot code */
120 static int I_flag = 0; /* use entire disk for DragonFly */
121 static int a_flag = 0; /* set active partition */
122 static char *b_flag = NULL; /* path to boot code */
123 static int i_flag = 0; /* replace partition data */
124 static int u_flag = 0; /* update partition data */
125 static int p_flag = 0; /* operate on a disk image file */
126 static int s_flag = 0; /* Print a summary and exit */
127 static int t_flag = 0; /* test only */
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"}
146 ,{0x09, "AIX boot partition or Coherent"}
147 ,{0x0A, "OS/2 Boot Manager or OPUS"}
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"}
155 ,{0x4D, "QNX 4.2 Primary"}
156 ,{0x4E, "QNX 4.2 Secondary"}
157 ,{0x4F, "QNX 4.2 Tertiary"}
160 ,{0x52, "CP/M or Microport SysV/AT"}
163 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
164 ,{0x64, "Novell Netware 2.xx"}
165 ,{0x65, "Novell Netware 3.xx"}
167 ,{0x80, "Minix 1.1 ... 1.4a"}
168 ,{0x81, "Minix 1.4b ... 1.5.10"}
169 ,{0x82, "Linux swap or Solaris x86"}
170 ,{0x83, "Linux filesystem"}
171 ,{0x93, "Amoeba filesystem"}
172 ,{0x94, "Amoeba bad block table"}
174 ,{0xA0, "Suspend to Disk"}
175 ,{0xA5, "DragonFly/FreeBSD/NetBSD/386BSD"}
179 ,{0xB7, "BSDI BSD/386 filesystem"}
180 ,{0xB8, "BSDI BSD/386 swap"}
181 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
186 ,{0xF2, "DOS 3.3+ Secondary"}
188 ,{0xFF, "BBT (Bad Blocks Table)"}
191 static void print_s0(int which);
192 static void print_part(int i);
193 static void init_sector0(unsigned long start);
194 static void init_boot(void);
195 static void change_part(int i);
196 static void print_params();
197 static void change_active(int which);
198 static void change_code();
199 static void get_params_to_use();
200 static void dos(struct dos_partition *partp);
201 static int open_disk(int u_flag);
202 static ssize_t read_disk(off_t sector, void *buf);
203 static ssize_t write_disk(off_t sector, void *buf);
204 static int get_params();
205 static int read_s0();
206 static int write_s0();
207 static int ok(char *str);
208 static int decimal(char *str, int *num, int deflt);
209 static char *get_type(int type);
210 static int read_config(char *config_file);
211 static void reset_boot(void);
212 static int sanitize_partition(struct dos_partition *);
213 static void usage(void);
215 static int hex(char *str, int *num, int deflt);
216 static int string(char *str, char **ans);
221 main(int argc, char *argv[])
225 while ((c = getopt(argc, argv, "BIab:f:p:istuv1234")) != -1)
270 if (f_flag || i_flag)
278 static char realname[12];
280 if(strncmp(argv[0], "/dev", 4) == 0) {
283 snprintf(realname, 12, "/dev/%s", argv[0]);
287 if (open_disk(u_flag) < 0)
288 err(1, "cannot open disk %s", disk);
289 } else if (disk == NULL) {
292 for(i = 0; disks[i]; i++)
295 rv = open_disk(u_flag);
299 err(1, "cannot open any disk");
301 if (open_disk(u_flag) < 0)
302 err(1, "cannot open disk %s", disk);
305 /* (abu)use mboot.bootinst to probe for the sector size */
306 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
307 err(1, "cannot allocate buffer to determine disk sector size");
308 read_disk(0, mboot.bootinst);
309 free(mboot.bootinst);
310 mboot.bootinst = NULL;
315 struct dos_partition *partp;
319 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
321 printf("Part %11s %11s Type Flags\n", "Start", "Size");
322 for (i = 0; i < NDOSPART; i++) {
323 partp = ((struct dos_partition *) &mboot.parts) + i;
324 if (partp->dp_start == 0 && partp->dp_size == 0)
326 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
327 (u_long) partp->dp_start,
328 (u_long) partp->dp_size, partp->dp_typ,
334 printf("******* Working on device %s *******\n",disk);
338 struct dos_partition *partp;
342 partp = (struct dos_partition *) (&mboot.parts[0]);
343 partp->dp_typ = DOSPTYP_386BSD;
344 partp->dp_flag = ACTIVE;
345 partp->dp_start = dos_sectors;
346 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
357 if (read_s0() || i_flag)
362 if (!read_config(f_flag))
387 init_sector0(dos_sectors);
389 printf("Media sector size is %d\n", secsize);
390 printf("Warning: BIOS sector numbering starts with sector 1\n");
391 printf("Information from DOS bootblock is:\n");
393 for (i = 1; i <= NDOSPART; i++)
396 change_part(partition);
398 if (u_flag || a_flag)
399 change_active(partition);
404 if (u_flag || a_flag || B_flag) {
406 printf("\nWe haven't changed the partition table yet. ");
407 printf("This is your last chance.\n");
411 if (ok("Should we write new partition table?"))
416 printf("\n-t flag specified -- partition table not written.\n");
427 fprintf(stderr, "%s%s",
428 "usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
429 " fdisk -f configfile [-itv] [disk]\n");
439 printf("Information from DOS bootblock is:\n");
441 for (i = 1; i <= NDOSPART; i++)
442 printf("%d: ", i), print_part(i);
447 static struct dos_partition mtpart = { 0 };
452 struct dos_partition *partp;
455 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
457 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
458 printf("<UNUSED>\n");
462 * Be careful not to overflow.
464 part_mb = partp->dp_size;
466 part_mb /= (1024 * 1024);
467 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
468 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
469 (u_long)partp->dp_start,
470 (u_long)partp->dp_size,
473 partp->dp_flag == ACTIVE ? " (active)" : "");
474 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
475 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
477 ,DPSECT(partp->dp_ssect)
478 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
480 ,DPSECT(partp->dp_esect));
491 fname = b_flag ? b_flag : "/boot/mbr";
492 if ((fd = open(fname, O_RDONLY)) == -1 ||
493 fstat(fd, &sb) == -1)
495 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
496 errx(1, "%s: length must be a multiple of sector size", fname);
497 if (mboot.bootinst != NULL)
498 free(mboot.bootinst);
499 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
500 errx(1, "%s: unable to allocate read buffer", fname);
501 if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
504 if (n != mboot.bootinst_size)
505 errx(1, "%s: short read", fname);
510 init_sector0(unsigned long start)
512 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
516 partp->dp_typ = DOSPTYP_386BSD;
517 partp->dp_flag = ACTIVE;
518 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
521 partp->dp_start = start;
522 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
530 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
532 printf("The data for partition %d is:\n", i);
535 if (u_flag && ok("Do you want to change it?")) {
539 bzero((char *)partp, sizeof (struct dos_partition));
542 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
548 Decimal("sysid (165=DragonFly)", partp->dp_typ, tmp);
549 Decimal("start", partp->dp_start, tmp);
550 Decimal("size", partp->dp_size, tmp);
551 if (!sanitize_partition(partp)) {
552 warnx("ERROR: failed to adjust; setting sysid to 0");
556 if (ok("Explicitly specify beg/end address ?"))
559 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
561 tsec = DPSECT(partp->dp_ssect);
562 Decimal("beginning cylinder", tcyl, tmp);
563 Decimal("beginning head", thd, tmp);
564 Decimal("beginning sector", tsec, tmp);
565 partp->dp_scyl = DOSCYL(tcyl);
566 partp->dp_ssect = DOSSECT(tsec,tcyl);
569 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
571 tsec = DPSECT(partp->dp_esect);
572 Decimal("ending cylinder", tcyl, tmp);
573 Decimal("ending head", thd, tmp);
574 Decimal("ending sector", tsec, tmp);
575 partp->dp_ecyl = DOSCYL(tcyl);
576 partp->dp_esect = DOSSECT(tsec,tcyl);
582 } while (!ok("Are we happy with this entry?"));
589 printf("parameters extracted from in-core disklabel are:\n");
590 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
591 ,cyls,heads,sectors,cylsecs);
592 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
593 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
594 printf("parameters to be used for BIOS calculations are:\n");
595 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
596 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
600 change_active(int which)
602 struct dos_partition *partp = &mboot.parts[0];
603 int active, i, new, tmp;
606 for (i = 0; i < NDOSPART; i++) {
607 if ((partp[i].dp_flag & ACTIVE) == 0)
609 printf("Partition %d is marked active\n", i + 1);
613 if (a_flag && which != -1)
615 else if (active == -1)
618 if (!ok("Do you want to change the active partition?"))
623 Decimal("active partition", new, tmp);
624 if (new < 1 || new > 4) {
625 printf("Active partition number must be in range 1-4."
630 } while (!ok("Are you happy with this choice"));
631 for (i = 0; i < NDOSPART; i++)
632 partp[i].dp_flag = 0;
633 if (active > 0 && active <= NDOSPART)
634 partp[active-1].dp_flag = ACTIVE;
640 if (ok("Do you want to change the boot code?"))
645 get_params_to_use(void)
649 if (ok("Do you want to change our idea of what BIOS thinks ?"))
653 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
654 Decimal("BIOS's idea of #heads", dos_heads, tmp);
655 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
656 dos_cylsecs = dos_heads * dos_sectors;
659 while(!ok("Are you happy with this choice"));
664 /***********************************************\
665 * Change real numbers into strange dos numbers *
666 \***********************************************/
669 struct dos_partition *partp;
674 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
675 memcpy(partp, &mtpart, sizeof(*partp));
680 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
681 cy = partp->dp_start / dos_cylsecs;
682 sec = partp->dp_start % dos_sectors + 1;
683 partp->dp_scyl = DOSCYL(cy);
684 partp->dp_ssect = DOSSECT(sec, cy);
687 end = partp->dp_start + partp->dp_size - 1;
688 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
689 cy = end / dos_cylsecs;
690 sec = end % dos_sectors + 1;
691 partp->dp_ecyl = DOSCYL(cy);
692 partp->dp_esect = DOSSECT(sec, cy);
697 /* Getting device status */
700 open_disk(int u_flag)
704 if (stat(disk, &st) == -1) {
707 warnx("can't get file status of %s", disk);
710 if ( !(st.st_mode & S_IFCHR) && p_flag == 0 )
711 warnx("device %s is not character special", disk);
713 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
716 warnx("can't open device %s", disk);
719 if (get_params() == -1) {
720 warnx("can't get disk parameters on %s", disk);
727 read_disk(off_t sector, void *buf)
729 lseek(fd,(sector * 512), 0);
731 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
734 int size = read(fd, buf, secsize);
735 if( size == secsize )
736 /* it worked so return */
740 return read( fd, buf, secsize );
742 /* we failed to read at any of the sizes */
747 write_disk(off_t sector, void *buf)
749 lseek(fd,(sector * 512), 0);
750 /* write out in the size that the read_disk found worked */
751 return write(fd, buf, secsize);
759 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
760 if (p_flag && fstat(fd, &st) == 0 && st.st_size) {
763 cylsecs = heads * sectors;
764 cyls = st.st_size / 512 / cylsecs;
766 warnx("can't get disk parameters on %s; supplying dummy ones", disk);
770 cylsecs = heads * sectors;
773 cyls = disklabel.d_ncylinders;
774 heads = disklabel.d_ntracks;
775 sectors = disklabel.d_nsectors;
776 cylsecs = heads * sectors;
780 dos_sectors = sectors;
781 dos_cylsecs = cylsecs;
782 disksecs = cyls * heads * sectors;
790 mboot.bootinst_size = secsize;
791 if (mboot.bootinst != NULL)
792 free(mboot.bootinst);
793 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
794 warnx("unable to allocate buffer to read fdisk "
798 if (read_disk(0, mboot.bootinst) == -1) {
799 warnx("can't read fdisk partition table");
802 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
803 warnx("invalid fdisk partition table found");
804 /* So should we initialize things */
807 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
823 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
825 * write enable label sector before write (if necessary),
826 * disable after writing.
827 * needed if the disklabel protected area also protects
828 * sector 0. (e.g. empty disk)
832 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
833 warn("ioctl DIOCWLABEL");
835 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
836 if (write_disk(sector,
837 &mboot.bootinst[sector * secsize]) == -1) {
838 warn("can't write fdisk partition table");
842 (void) ioctl(fd, DIOCWLABEL, &flag);
847 (void) ioctl(fd, DIOCWLABEL, &flag);
857 printf("%s [n] ", str);
859 if (fgets(lbuf, LBUF, stdin) == NULL)
861 lbuf[strlen(lbuf)-1] = 0;
864 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
865 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
872 decimal(char *str, int *num, int deflt)
878 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
880 if (fgets(lbuf, LBUF, stdin) == NULL)
882 lbuf[strlen(lbuf)-1] = 0;
888 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
891 while ((c = *cp++)) {
892 if (c <= '9' && c >= '0')
893 acc = acc * 10 + c - '0';
897 if (c == ' ' || c == '\t')
898 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
903 printf("%s is an invalid decimal number. Try again.\n",
911 hex(char *str, int *num, int deflt)
917 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
918 fgets(lbuf, LBUF, stdin);
919 lbuf[strlen(lbuf)-1] = 0;
925 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
928 while ((c = *cp++)) {
929 if (c <= '9' && c >= '0')
930 acc = (acc << 4) + c - '0';
931 else if (c <= 'f' && c >= 'a')
932 acc = (acc << 4) + c - 'a' + 10;
933 else if (c <= 'F' && c >= 'A')
934 acc = (acc << 4) + c - 'A' + 10;
938 if (c == ' ' || c == '\t')
939 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
944 printf("%s is an invalid hex number. Try again.\n",
951 string(char *str, char **ans)
957 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
958 fgets(lbuf, LBUF, stdin);
959 lbuf[strlen(lbuf)-1] = 0;
964 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
968 while ((c = *cp) && c != '"') cp++;
971 while ((c = *cp) && c != ' ' && c != '\t') cp++;
984 int numentries = (sizeof(part_types)/sizeof(struct part_type));
986 struct part_type *ptr = part_types;
989 while(counter < numentries)
991 if(ptr->type == type)
1003 parse_config_line(line, command)
1010 while (1) /* dirty trick used to insure one exit point for this
1013 memset(command, 0, sizeof(*command));
1015 while (isspace(*cp)) ++cp;
1016 if (*cp == '\0' || *cp == '#')
1020 command->cmd = *cp++;
1027 while (isspace(*cp)) ++cp;
1030 break; /* found comment */
1034 command->args[command->n_args].argtype = *cp++;
1038 break; /* assume end of line */
1041 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1044 break; /* couldn't parse number */
1055 process_geometry(command)
1066 "ERROR line %d: the geometry specification line must occur before\n\
1067 all partition specifications",
1068 current_line_number);
1072 if (command->n_args != 3)
1074 warnx("ERROR line %d: incorrect number of geometry args",
1075 current_line_number);
1082 for (i = 0; i < 3; ++i)
1084 switch (command->args[i].argtype)
1087 dos_cyls = command->args[i].arg_val;
1090 dos_heads = command->args[i].arg_val;
1093 dos_sectors = command->args[i].arg_val;
1097 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1098 current_line_number, command->args[i].argtype,
1099 command->args[i].argtype);
1109 dos_cylsecs = dos_heads * dos_sectors;
1112 * Do sanity checks on parameter values
1116 warnx("ERROR line %d: number of cylinders not specified",
1117 current_line_number);
1120 if (dos_cyls == 0 || dos_cyls > 1024)
1123 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1124 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1125 is dedicated to DragonFly)",
1126 current_line_number, dos_cyls);
1131 warnx("ERROR line %d: number of heads not specified",
1132 current_line_number);
1135 else if (dos_heads < 1 || dos_heads > 256)
1137 warnx("ERROR line %d: number of heads must be within (1-256)",
1138 current_line_number);
1142 if (dos_sectors < 0)
1144 warnx("ERROR line %d: number of sectors not specified",
1145 current_line_number);
1148 else if (dos_sectors < 1 || dos_sectors > 63)
1150 warnx("ERROR line %d: number of sectors must be within (1-63)",
1151 current_line_number);
1162 process_partition(command)
1165 int status = 0, partition;
1166 u_int32_t prev_head_boundary, prev_cyl_boundary;
1167 u_int32_t adj_size, max_end;
1168 struct dos_partition *partp;
1173 if (command->n_args != 4)
1175 warnx("ERROR line %d: incorrect number of partition args",
1176 current_line_number);
1179 partition = command->args[0].arg_val;
1180 if (partition < 1 || partition > 4)
1182 warnx("ERROR line %d: invalid partition number %d",
1183 current_line_number, partition);
1186 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1187 bzero((char *)partp, sizeof (struct dos_partition));
1188 partp->dp_typ = command->args[1].arg_val;
1189 partp->dp_start = command->args[2].arg_val;
1190 partp->dp_size = command->args[3].arg_val;
1191 max_end = partp->dp_start + partp->dp_size;
1193 if (partp->dp_typ == 0)
1196 * Get out, the partition is marked as unused.
1199 * Insure that it's unused.
1201 bzero((char *)partp, sizeof (struct dos_partition));
1207 * Adjust start upwards, if necessary, to fall on an head boundary.
1209 if (partp->dp_start % dos_sectors != 0)
1211 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1212 if (max_end < dos_sectors ||
1213 prev_head_boundary > max_end - dos_sectors)
1216 * Can't go past end of partition
1219 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1221 current_line_number, partition);
1225 "WARNING: adjusting start offset of partition %d\n\
1226 from %u to %u, to fall on a head boundary",
1227 partition, (u_int)partp->dp_start,
1228 (u_int)(prev_head_boundary + dos_sectors));
1229 partp->dp_start = prev_head_boundary + dos_sectors;
1233 * Adjust size downwards, if necessary, to fall on a cylinder
1237 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1238 if (prev_cyl_boundary > partp->dp_start)
1239 adj_size = prev_cyl_boundary - partp->dp_start;
1243 "ERROR: could not adjust partition to start on a head boundary\n\
1244 and end on a cylinder boundary.");
1247 if (adj_size != partp->dp_size)
1250 "WARNING: adjusting size of partition %d from %u to %u\n\
1251 to end on a cylinder boundary",
1252 partition, (u_int)partp->dp_size, (u_int)adj_size);
1253 partp->dp_size = adj_size;
1255 if (partp->dp_size == 0)
1257 warnx("ERROR line %d: size of partition %d is zero",
1258 current_line_number, partition);
1271 process_active(command)
1274 int status = 0, partition, i;
1275 struct dos_partition *partp;
1279 active_processed = 1;
1280 if (command->n_args != 1)
1282 warnx("ERROR line %d: incorrect number of active args",
1283 current_line_number);
1287 partition = command->args[0].arg_val;
1288 if (partition < 1 || partition > 4)
1290 warnx("ERROR line %d: invalid partition number %d",
1291 current_line_number, partition);
1295 * Reset active partition
1297 partp = ((struct dos_partition *) &mboot.parts);
1298 for (i = 0; i < NDOSPART; i++)
1299 partp[i].dp_flag = 0;
1300 partp[partition-1].dp_flag = ACTIVE;
1318 parse_config_line(line, &command);
1319 switch (command.cmd)
1323 * Comment or blank line
1330 status = process_geometry(&command);
1333 status = process_partition(&command);
1336 status = process_active(&command);
1349 read_config(config_file)
1356 while (1) /* dirty trick used to insure one exit point for this
1359 if (strcmp(config_file, "-") != 0)
1362 * We're not reading from stdin
1364 if ((fp = fopen(config_file, "r")) == NULL)
1374 current_line_number = 0;
1377 if (fgets(buf, sizeof(buf), fp) == NULL)
1381 ++current_line_number;
1382 status = process_line(buf);
1393 * It doesn't matter if we're reading from stdin, as we've reached EOF
1405 struct dos_partition *partp;
1408 for (i = 0; i < 4; ++i)
1410 partp = ((struct dos_partition *) &mboot.parts) + i;
1411 bzero((char *)partp, sizeof (struct dos_partition));
1416 sanitize_partition(partp)
1417 struct dos_partition *partp;
1419 u_int32_t prev_head_boundary, prev_cyl_boundary;
1420 u_int32_t max_end, size, start;
1422 start = partp->dp_start;
1423 size = partp->dp_size;
1424 max_end = start + size;
1425 /* Only allow a zero size if the partition is being marked unused. */
1427 if (start == 0 && partp->dp_typ == 0)
1429 warnx("ERROR: size of partition is zero");
1432 /* Return if no adjustment is necessary. */
1433 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1436 if (start % dos_sectors != 0)
1437 warnx("WARNING: partition does not start on a head boundary");
1438 if ((start +size) % dos_sectors != 0)
1439 warnx("WARNING: partition does not end on a cylinder boundary");
1440 warnx("WARNING: this may confuse the BIOS or some operating systems");
1441 if (!ok("Correct this automatically?"))
1445 * Adjust start upwards, if necessary, to fall on an head boundary.
1447 if (start % dos_sectors != 0) {
1448 prev_head_boundary = start / dos_sectors * dos_sectors;
1449 if (max_end < dos_sectors ||
1450 prev_head_boundary >= max_end - dos_sectors) {
1452 * Can't go past end of partition
1455 "ERROR: unable to adjust start of partition to fall on a head boundary");
1458 start = prev_head_boundary + dos_sectors;
1462 * Adjust size downwards, if necessary, to fall on a cylinder
1465 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1466 if (prev_cyl_boundary > start)
1467 size = prev_cyl_boundary - start;
1469 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1470 and end on a cylinder boundary.");
1474 /* Finally, commit any changes to partp and return. */
1475 if (start != partp->dp_start) {
1476 warnx("WARNING: adjusting start offset of partition to %u",
1478 partp->dp_start = start;
1480 if (size != partp->dp_size) {
1481 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1482 partp->dp_size = size;