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[] =
29 "$FreeBSD: src/sbin/i386/fdisk/fdisk.c,v 1.36.2.11 2002/04/25 21:02:21 trhodes Exp $";
32 #include <sys/disklabel.h>
46 static char lbuf[LBUF];
52 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
54 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
55 * Copyright (c) 1989 Robert. V. Baron
59 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
60 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
61 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
63 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
65 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
66 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
67 int secsize = 0; /* the sensed sector size */
72 "/dev/ad0", "/dev/da0", 0
75 struct disklabel disklabel; /* disk parameters */
77 int cyls, sectors, heads, cylsecs, disksecs;
81 unsigned char padding[2]; /* force the longs to be long aligned */
82 unsigned char *bootinst; /* boot code */
84 struct dos_partition parts[4];
86 struct mboot mboot = {{0}, NULL, 0};
89 #define BOOT_MAGIC 0xAA55
96 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
97 #define DOSCYL(c) (c & 0xff)
98 static int partition = -1;
103 static int current_line_number;
105 static int geom_processed = 0;
106 static int part_processed = 0;
107 static int active_processed = 0;
120 static int B_flag = 0; /* replace boot code */
121 static int I_flag = 0; /* use entire disk for FreeBSD */
122 static int a_flag = 0; /* set active partition */
123 static char *b_flag = NULL; /* path to boot code */
124 static int i_flag = 0; /* replace partition data */
125 static int u_flag = 0; /* update partition data */
126 static int s_flag = 0; /* Print a summary and exit */
127 static int t_flag = 0; /* test only */
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, "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:istuv1234")) != -1)
266 if (f_flag || i_flag)
275 static char realname[12];
277 if(strncmp(argv[0], "/dev", 4) == 0)
281 snprintf(realname, 12, "/dev/%s", argv[0]);
285 if (open_disk(u_flag) < 0)
286 err(1, "cannot open disk %s", disk);
292 for(i = 0; disks[i]; i++)
295 rv = open_disk(u_flag);
299 err(1, "cannot open any disk");
302 /* (abu)use mboot.bootinst to probe for the sector size */
303 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
304 err(1, "cannot allocate buffer to determine disk sector size");
305 read_disk(0, mboot.bootinst);
306 free(mboot.bootinst);
307 mboot.bootinst = NULL;
312 struct dos_partition *partp;
316 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
318 printf("Part %11s %11s Type Flags\n", "Start", "Size");
319 for (i = 0; i < NDOSPART; i++) {
320 partp = ((struct dos_partition *) &mboot.parts) + i;
321 if (partp->dp_start == 0 && partp->dp_size == 0)
323 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
324 (u_long) partp->dp_start,
325 (u_long) partp->dp_size, partp->dp_typ,
331 printf("******* Working on device %s *******\n",disk);
335 struct dos_partition *partp;
339 partp = (struct dos_partition *) (&mboot.parts[0]);
340 partp->dp_typ = DOSPTYP_386BSD;
341 partp->dp_flag = ACTIVE;
342 partp->dp_start = dos_sectors;
343 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
354 if (read_s0() || i_flag)
359 if (!read_config(f_flag))
384 init_sector0(dos_sectors);
386 printf("Media sector size is %d\n", secsize);
387 printf("Warning: BIOS sector numbering starts with sector 1\n");
388 printf("Information from DOS bootblock is:\n");
390 for (i = 1; i <= NDOSPART; i++)
393 change_part(partition);
395 if (u_flag || a_flag)
396 change_active(partition);
401 if (u_flag || a_flag || B_flag) {
403 printf("\nWe haven't changed the partition table yet. ");
404 printf("This is your last chance.\n");
408 if (ok("Should we write new partition table?"))
413 printf("\n-t flag specified -- partition table not written.\n");
424 fprintf(stderr, "%s%s",
425 "usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
426 " fdisk -f configfile [-itv] [disk]\n");
436 printf("Information from DOS bootblock is:\n");
438 for (i = 1; i <= NDOSPART; i++)
439 printf("%d: ", i), print_part(i);
444 static struct dos_partition mtpart = { 0 };
449 struct dos_partition *partp;
452 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
454 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
455 printf("<UNUSED>\n");
459 * Be careful not to overflow.
461 part_mb = partp->dp_size;
463 part_mb /= (1024 * 1024);
464 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
465 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
466 (u_long)partp->dp_start,
467 (u_long)partp->dp_size,
470 partp->dp_flag == ACTIVE ? " (active)" : "");
471 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
472 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
474 ,DPSECT(partp->dp_ssect)
475 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
477 ,DPSECT(partp->dp_esect));
488 fname = b_flag ? b_flag : "/boot/mbr";
489 if ((fd = open(fname, O_RDONLY)) == -1 ||
490 fstat(fd, &sb) == -1)
492 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
493 errx(1, "%s: length must be a multiple of sector size", fname);
494 if (mboot.bootinst != NULL)
495 free(mboot.bootinst);
496 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
497 errx(1, "%s: unable to allocate read buffer", fname);
498 if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
501 if (n != mboot.bootinst_size)
502 errx(1, "%s: short read", fname);
507 init_sector0(unsigned long start)
509 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
513 partp->dp_typ = DOSPTYP_386BSD;
514 partp->dp_flag = ACTIVE;
515 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
518 partp->dp_start = start;
519 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
527 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
529 printf("The data for partition %d is:\n", i);
532 if (u_flag && ok("Do you want to change it?")) {
536 bzero((char *)partp, sizeof (struct dos_partition));
539 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
545 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
546 Decimal("start", partp->dp_start, tmp);
547 Decimal("size", partp->dp_size, tmp);
548 if (!sanitize_partition(partp)) {
549 warnx("ERROR: failed to adjust; setting sysid to 0");
553 if (ok("Explicitly specify beg/end address ?"))
556 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
558 tsec = DPSECT(partp->dp_ssect);
559 Decimal("beginning cylinder", tcyl, tmp);
560 Decimal("beginning head", thd, tmp);
561 Decimal("beginning sector", tsec, tmp);
562 partp->dp_scyl = DOSCYL(tcyl);
563 partp->dp_ssect = DOSSECT(tsec,tcyl);
566 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
568 tsec = DPSECT(partp->dp_esect);
569 Decimal("ending cylinder", tcyl, tmp);
570 Decimal("ending head", thd, tmp);
571 Decimal("ending sector", tsec, tmp);
572 partp->dp_ecyl = DOSCYL(tcyl);
573 partp->dp_esect = DOSSECT(tsec,tcyl);
579 } while (!ok("Are we happy with this entry?"));
586 printf("parameters extracted from in-core disklabel are:\n");
587 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
588 ,cyls,heads,sectors,cylsecs);
589 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
590 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
591 printf("parameters to be used for BIOS calculations are:\n");
592 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
593 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
597 change_active(int which)
599 struct dos_partition *partp = &mboot.parts[0];
600 int active, i, new, tmp;
603 for (i = 0; i < NDOSPART; i++) {
604 if ((partp[i].dp_flag & ACTIVE) == 0)
606 printf("Partition %d is marked active\n", i + 1);
610 if (a_flag && which != -1)
612 else if (active == -1)
615 if (!ok("Do you want to change the active partition?"))
620 Decimal("active partition", new, tmp);
621 if (new < 1 || new > 4) {
622 printf("Active partition number must be in range 1-4."
627 } while (!ok("Are you happy with this choice"));
628 for (i = 0; i < NDOSPART; i++)
629 partp[i].dp_flag = 0;
630 if (active > 0 && active <= NDOSPART)
631 partp[active-1].dp_flag = ACTIVE;
637 if (ok("Do you want to change the boot code?"))
646 if (ok("Do you want to change our idea of what BIOS thinks ?"))
650 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
651 Decimal("BIOS's idea of #heads", dos_heads, tmp);
652 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
653 dos_cylsecs = dos_heads * dos_sectors;
656 while(!ok("Are you happy with this choice"));
661 /***********************************************\
662 * Change real numbers into strange dos numbers *
663 \***********************************************/
666 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);
694 /* Getting device status */
697 open_disk(int u_flag)
701 if (stat(disk, &st) == -1) {
704 warnx("can't get file status of %s", disk);
707 if ( !(st.st_mode & S_IFCHR) )
708 warnx("device %s is not character special", disk);
710 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
713 warnx("can't open device %s", disk);
716 if (get_params(0) == -1) {
717 warnx("can't get disk parameters on %s", disk);
724 read_disk(off_t sector, void *buf)
726 lseek(fd,(sector * 512), 0);
728 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
731 int size = read(fd, buf, secsize);
732 if( size == secsize )
733 /* it worked so return */
737 return read( fd, buf, secsize );
739 /* we failed to read at any of the sizes */
744 write_disk(off_t sector, void *buf)
746 lseek(fd,(sector * 512), 0);
747 /* write out in the size that the read_disk found worked */
748 return write(fd, buf, secsize);
755 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
756 warnx("can't get disk parameters on %s; supplying dummy ones", disk);
758 dos_heads = heads = 1;
759 dos_sectors = sectors = 1;
760 dos_cylsecs = cylsecs = heads * sectors;
761 disksecs = cyls * heads * sectors;
765 dos_cyls = cyls = disklabel.d_ncylinders;
766 dos_heads = heads = disklabel.d_ntracks;
767 dos_sectors = sectors = disklabel.d_nsectors;
768 dos_cylsecs = cylsecs = heads * sectors;
769 disksecs = cyls * heads * sectors;
778 mboot.bootinst_size = secsize;
779 if (mboot.bootinst != NULL)
780 free(mboot.bootinst);
781 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
782 warnx("unable to allocate buffer to read fdisk "
786 if (read_disk(0, mboot.bootinst) == -1) {
787 warnx("can't read fdisk partition table");
790 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
791 warnx("invalid fdisk partition table found");
792 /* So should we initialize things */
795 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
811 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
813 * write enable label sector before write (if necessary),
814 * disable after writing.
815 * needed if the disklabel protected area also protects
816 * sector 0. (e.g. empty disk)
820 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
821 warn("ioctl DIOCWLABEL");
823 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
824 if (write_disk(sector,
825 &mboot.bootinst[sector * secsize]) == -1) {
826 warn("can't write fdisk partition table");
830 (void) ioctl(fd, DIOCWLABEL, &flag);
835 (void) ioctl(fd, DIOCWLABEL, &flag);
845 printf("%s [n] ", str);
847 if (fgets(lbuf, LBUF, stdin) == NULL)
849 lbuf[strlen(lbuf)-1] = 0;
852 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
853 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
860 decimal(char *str, int *num, int deflt)
866 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
868 if (fgets(lbuf, LBUF, stdin) == NULL)
870 lbuf[strlen(lbuf)-1] = 0;
876 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
879 while ((c = *cp++)) {
880 if (c <= '9' && c >= '0')
881 acc = acc * 10 + c - '0';
885 if (c == ' ' || c == '\t')
886 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
891 printf("%s is an invalid decimal number. Try again.\n",
899 hex(char *str, int *num, int deflt)
905 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
906 fgets(lbuf, LBUF, stdin);
907 lbuf[strlen(lbuf)-1] = 0;
913 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
916 while ((c = *cp++)) {
917 if (c <= '9' && c >= '0')
918 acc = (acc << 4) + c - '0';
919 else if (c <= 'f' && c >= 'a')
920 acc = (acc << 4) + c - 'a' + 10;
921 else if (c <= 'F' && c >= 'A')
922 acc = (acc << 4) + c - 'A' + 10;
926 if (c == ' ' || c == '\t')
927 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
932 printf("%s is an invalid hex number. Try again.\n",
939 string(char *str, char **ans)
945 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
946 fgets(lbuf, LBUF, stdin);
947 lbuf[strlen(lbuf)-1] = 0;
952 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
956 while ((c = *cp) && c != '"') cp++;
959 while ((c = *cp) && c != ' ' && c != '\t') cp++;
972 int numentries = (sizeof(part_types)/sizeof(struct part_type));
974 struct part_type *ptr = part_types;
977 while(counter < numentries)
979 if(ptr->type == type)
991 parse_config_line(line, command)
998 while (1) /* dirty trick used to insure one exit point for this
1001 memset(command, 0, sizeof(*command));
1003 while (isspace(*cp)) ++cp;
1004 if (*cp == '\0' || *cp == '#')
1008 command->cmd = *cp++;
1015 while (isspace(*cp)) ++cp;
1018 break; /* found comment */
1022 command->args[command->n_args].argtype = *cp++;
1026 break; /* assume end of line */
1029 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1032 break; /* couldn't parse number */
1043 process_geometry(command)
1054 "ERROR line %d: the geometry specification line must occur before\n\
1055 all partition specifications",
1056 current_line_number);
1060 if (command->n_args != 3)
1062 warnx("ERROR line %d: incorrect number of geometry args",
1063 current_line_number);
1070 for (i = 0; i < 3; ++i)
1072 switch (command->args[i].argtype)
1075 dos_cyls = command->args[i].arg_val;
1078 dos_heads = command->args[i].arg_val;
1081 dos_sectors = command->args[i].arg_val;
1085 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1086 current_line_number, command->args[i].argtype,
1087 command->args[i].argtype);
1097 dos_cylsecs = dos_heads * dos_sectors;
1100 * Do sanity checks on parameter values
1104 warnx("ERROR line %d: number of cylinders not specified",
1105 current_line_number);
1108 if (dos_cyls == 0 || dos_cyls > 1024)
1111 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1112 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1113 is dedicated to FreeBSD)",
1114 current_line_number, dos_cyls);
1119 warnx("ERROR line %d: number of heads not specified",
1120 current_line_number);
1123 else if (dos_heads < 1 || dos_heads > 256)
1125 warnx("ERROR line %d: number of heads must be within (1-256)",
1126 current_line_number);
1130 if (dos_sectors < 0)
1132 warnx("ERROR line %d: number of sectors not specified",
1133 current_line_number);
1136 else if (dos_sectors < 1 || dos_sectors > 63)
1138 warnx("ERROR line %d: number of sectors must be within (1-63)",
1139 current_line_number);
1150 process_partition(command)
1153 int status = 0, partition;
1154 u_int32_t prev_head_boundary, prev_cyl_boundary;
1155 u_int32_t adj_size, max_end;
1156 struct dos_partition *partp;
1161 if (command->n_args != 4)
1163 warnx("ERROR line %d: incorrect number of partition args",
1164 current_line_number);
1167 partition = command->args[0].arg_val;
1168 if (partition < 1 || partition > 4)
1170 warnx("ERROR line %d: invalid partition number %d",
1171 current_line_number, partition);
1174 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1175 bzero((char *)partp, sizeof (struct dos_partition));
1176 partp->dp_typ = command->args[1].arg_val;
1177 partp->dp_start = command->args[2].arg_val;
1178 partp->dp_size = command->args[3].arg_val;
1179 max_end = partp->dp_start + partp->dp_size;
1181 if (partp->dp_typ == 0)
1184 * Get out, the partition is marked as unused.
1187 * Insure that it's unused.
1189 bzero((char *)partp, sizeof (struct dos_partition));
1195 * Adjust start upwards, if necessary, to fall on an head boundary.
1197 if (partp->dp_start % dos_sectors != 0)
1199 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1200 if (max_end < dos_sectors ||
1201 prev_head_boundary > max_end - dos_sectors)
1204 * Can't go past end of partition
1207 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1209 current_line_number, partition);
1213 "WARNING: adjusting start offset of partition %d\n\
1214 from %u to %u, to fall on a head boundary",
1215 partition, (u_int)partp->dp_start,
1216 (u_int)(prev_head_boundary + dos_sectors));
1217 partp->dp_start = prev_head_boundary + dos_sectors;
1221 * Adjust size downwards, if necessary, to fall on a cylinder
1225 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1226 if (prev_cyl_boundary > partp->dp_start)
1227 adj_size = prev_cyl_boundary - partp->dp_start;
1231 "ERROR: could not adjust partition to start on a head boundary\n\
1232 and end on a cylinder boundary.");
1235 if (adj_size != partp->dp_size)
1238 "WARNING: adjusting size of partition %d from %u to %u\n\
1239 to end on a cylinder boundary",
1240 partition, (u_int)partp->dp_size, (u_int)adj_size);
1241 partp->dp_size = adj_size;
1243 if (partp->dp_size == 0)
1245 warnx("ERROR line %d: size of partition %d is zero",
1246 current_line_number, partition);
1259 process_active(command)
1262 int status = 0, partition, i;
1263 struct dos_partition *partp;
1267 active_processed = 1;
1268 if (command->n_args != 1)
1270 warnx("ERROR line %d: incorrect number of active args",
1271 current_line_number);
1275 partition = command->args[0].arg_val;
1276 if (partition < 1 || partition > 4)
1278 warnx("ERROR line %d: invalid partition number %d",
1279 current_line_number, partition);
1283 * Reset active partition
1285 partp = ((struct dos_partition *) &mboot.parts);
1286 for (i = 0; i < NDOSPART; i++)
1287 partp[i].dp_flag = 0;
1288 partp[partition-1].dp_flag = ACTIVE;
1306 parse_config_line(line, &command);
1307 switch (command.cmd)
1311 * Comment or blank line
1318 status = process_geometry(&command);
1321 status = process_partition(&command);
1324 status = process_active(&command);
1337 read_config(config_file)
1344 while (1) /* dirty trick used to insure one exit point for this
1347 if (strcmp(config_file, "-") != 0)
1350 * We're not reading from stdin
1352 if ((fp = fopen(config_file, "r")) == NULL)
1362 current_line_number = 0;
1365 if (fgets(buf, sizeof(buf), fp) == NULL)
1369 ++current_line_number;
1370 status = process_line(buf);
1381 * It doesn't matter if we're reading from stdin, as we've reached EOF
1393 struct dos_partition *partp;
1396 for (i = 0; i < 4; ++i)
1398 partp = ((struct dos_partition *) &mboot.parts) + i;
1399 bzero((char *)partp, sizeof (struct dos_partition));
1404 sanitize_partition(partp)
1405 struct dos_partition *partp;
1407 u_int32_t prev_head_boundary, prev_cyl_boundary;
1408 u_int32_t max_end, size, start;
1410 start = partp->dp_start;
1411 size = partp->dp_size;
1412 max_end = start + size;
1413 /* Only allow a zero size if the partition is being marked unused. */
1415 if (start == 0 && partp->dp_typ == 0)
1417 warnx("ERROR: size of partition is zero");
1420 /* Return if no adjustment is necessary. */
1421 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1424 if (start % dos_sectors != 0)
1425 warnx("WARNING: partition does not start on a head boundary");
1426 if ((start +size) % dos_sectors != 0)
1427 warnx("WARNING: partition does not end on a cylinder boundary");
1428 warnx("WARNING: this may confuse the BIOS or some operating systems");
1429 if (!ok("Correct this automatically?"))
1433 * Adjust start upwards, if necessary, to fall on an head boundary.
1435 if (start % dos_sectors != 0) {
1436 prev_head_boundary = start / dos_sectors * dos_sectors;
1437 if (max_end < dos_sectors ||
1438 prev_head_boundary >= max_end - dos_sectors) {
1440 * Can't go past end of partition
1443 "ERROR: unable to adjust start of partition to fall on a head boundary");
1446 start = prev_head_boundary + dos_sectors;
1450 * Adjust size downwards, if necessary, to fall on a cylinder
1453 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1454 if (prev_cyl_boundary > start)
1455 size = prev_cyl_boundary - start;
1457 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1458 and end on a cylinder boundary.");
1462 /* Finally, commit any changes to partp and return. */
1463 if (start != partp->dp_start) {
1464 warnx("WARNING: adjusting start offset of partition to %u",
1466 partp->dp_start = start;
1468 if (size != partp->dp_size) {
1469 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1470 partp->dp_size = size;