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 $
27 * $DragonFly: src/sbin/i386/fdisk/fdisk.c,v 1.11 2005/11/06 12:19:22 swildner 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"}
153 ,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
154 ,{0x12, "Compaq diagnostics"}
155 ,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
156 ,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
157 ,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
158 ,{0x18, "AST Windows swapfile"}
161 ,{0x3C, "PartitionMagic recovery"}
163 ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
164 ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
165 ,{0x43, "Linux native (sharing disk with DRDOS)"}
166 ,{0x4D, "QNX 4.2 Primary"}
167 ,{0x4E, "QNX 4.2 Secondary"}
168 ,{0x4F, "QNX 4.2 Tertiary"}
171 ,{0x52, "CP/M or Microport SysV/AT"}
174 ,{0x55, "EZ-Drive (disk manager)"}
176 ,{0x5C, "Priam Edisk (disk manager)"} /* according to S. Widlake */
178 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
179 ,{0x64, "Novell Netware 2.xx"}
180 ,{0x65, "Novell Netware 3.xx"}
181 ,{0x70, "DiskSecure Multi-Boot"}
184 ,{0x78, "QNX4.x 2nd part"}
185 ,{0x79, "QNX4.x 3rd part"}
186 ,{0x80, "Minix 1.1 ... 1.4a"}
187 ,{0x81, "Minix 1.4b ... 1.5.10"}
188 ,{0x82, "Linux swap or Solaris x86"}
189 ,{0x83, "Linux filesystem"}
190 ,{0x84, "OS/2 hidden C: drive"}
191 ,{0x85, "Linux extended"}
192 ,{0x86, "NTFS volume set??"}
193 ,{0x87, "NTFS volume set??"}
194 ,{0x93, "Amoeba filesystem"}
195 ,{0x94, "Amoeba bad block table"}
197 ,{0xA0, "Suspend to Disk"}
198 ,{0xA5, "DragonFly/FreeBSD/NetBSD/386BSD"}
203 ,{0xB7, "BSDI BSD/386 filesystem"}
204 ,{0xB8, "BSDI BSD/386 swap"}
205 ,{0xBE, "Solaris x86 boot"}
206 ,{0xC1, "DRDOS/sec with 12-bit FAT"}
207 ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
208 ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
210 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
214 ,{0xEB, "BeOS file system"}
216 ,{0xEF, "EFI System Partition"}
218 ,{0xF2, "DOS 3.3+ Secondary"}
220 ,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
221 ,{0xFF, "BBT (Bad Blocks Table)"}
224 static void print_s0(int which);
225 static void print_part(int i);
226 static void init_sector0(unsigned long start);
227 static void init_boot(void);
228 static void change_part(int i);
229 static void print_params();
230 static void change_active(int which);
231 static void change_code();
232 static void get_params_to_use();
233 static void dos(struct dos_partition *partp);
234 static int open_disk(int u_flag);
235 static ssize_t read_disk(off_t sector, void *buf);
236 static ssize_t write_disk(off_t sector, void *buf);
237 static int get_params();
238 static int read_s0();
239 static int write_s0();
240 static int ok(char *str);
241 static int decimal(char *str, int *num, int deflt);
242 static char *get_type(int type);
243 static int read_config(char *config_file);
244 static void reset_boot(void);
245 static int sanitize_partition(struct dos_partition *);
246 static void usage(void);
248 static int hex(char *str, int *num, int deflt);
249 static int string(char *str, char **ans);
254 main(int argc, char *argv[])
258 while ((c = getopt(argc, argv, "BIab:f:p:istuv1234")) != -1)
303 if (f_flag || i_flag)
311 static char realname[12];
313 if(strncmp(argv[0], "/dev", 4) == 0) {
316 snprintf(realname, 12, "/dev/%s", argv[0]);
320 if (open_disk(u_flag) < 0)
321 err(1, "cannot open disk %s", disk);
322 } else if (disk == NULL) {
325 for(i = 0; disks[i]; i++)
328 rv = open_disk(u_flag);
332 err(1, "cannot open any disk");
334 if (open_disk(u_flag) < 0)
335 err(1, "cannot open disk %s", disk);
338 /* (abu)use mboot.bootinst to probe for the sector size */
339 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
340 err(1, "cannot allocate buffer to determine disk sector size");
341 read_disk(0, mboot.bootinst);
342 free(mboot.bootinst);
343 mboot.bootinst = NULL;
348 struct dos_partition *partp;
352 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
354 printf("Part %11s %11s Type Flags\n", "Start", "Size");
355 for (i = 0; i < NDOSPART; i++) {
356 partp = ((struct dos_partition *) &mboot.parts) + i;
357 if (partp->dp_start == 0 && partp->dp_size == 0)
359 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
360 (u_long) partp->dp_start,
361 (u_long) partp->dp_size, partp->dp_typ,
367 printf("******* Working on device %s *******\n",disk);
371 struct dos_partition *partp;
375 partp = (struct dos_partition *) (&mboot.parts[0]);
376 partp->dp_typ = DOSPTYP_386BSD;
377 partp->dp_flag = ACTIVE;
378 partp->dp_start = dos_sectors;
379 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
390 if (read_s0() || i_flag)
395 if (!read_config(f_flag))
420 init_sector0(dos_sectors);
422 printf("Media sector size is %d\n", secsize);
423 printf("Warning: BIOS sector numbering starts with sector 1\n");
424 printf("Information from DOS bootblock is:\n");
426 for (i = 1; i <= NDOSPART; i++)
429 change_part(partition);
431 if (u_flag || a_flag)
432 change_active(partition);
437 if (u_flag || a_flag || B_flag) {
439 printf("\nWe haven't changed the partition table yet. ");
440 printf("This is your last chance.\n");
444 if (ok("Should we write new partition table?"))
449 printf("\n-t flag specified -- partition table not written.\n");
460 fprintf(stderr, "%s%s",
461 "usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
462 " fdisk -f configfile [-itv] [disk]\n");
472 printf("Information from DOS bootblock is:\n");
474 for (i = 1; i <= NDOSPART; i++)
475 printf("%d: ", i), print_part(i);
480 static struct dos_partition mtpart = { 0 };
485 struct dos_partition *partp;
488 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
490 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
491 printf("<UNUSED>\n");
495 * Be careful not to overflow.
497 part_mb = partp->dp_size;
499 part_mb /= (1024 * 1024);
500 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
501 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
502 (u_long)partp->dp_start,
503 (u_long)partp->dp_size,
506 partp->dp_flag == ACTIVE ? " (active)" : "");
507 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
508 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
510 ,DPSECT(partp->dp_ssect)
511 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
513 ,DPSECT(partp->dp_esect));
524 fname = b_flag ? b_flag : "/boot/mbr";
525 if ((fd = open(fname, O_RDONLY)) == -1 ||
526 fstat(fd, &sb) == -1)
528 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
529 errx(1, "%s: length must be a multiple of sector size", fname);
530 if (mboot.bootinst != NULL)
531 free(mboot.bootinst);
532 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
533 errx(1, "%s: unable to allocate read buffer", fname);
534 if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
537 if (n != mboot.bootinst_size)
538 errx(1, "%s: short read", fname);
543 init_sector0(unsigned long start)
545 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
549 partp->dp_typ = DOSPTYP_386BSD;
550 partp->dp_flag = ACTIVE;
551 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
554 partp->dp_start = start;
555 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
563 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
565 printf("The data for partition %d is:\n", i);
568 if (u_flag && ok("Do you want to change it?")) {
572 bzero((char *)partp, sizeof (struct dos_partition));
575 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
581 Decimal("sysid (165=DragonFly)", partp->dp_typ, tmp);
582 Decimal("start", partp->dp_start, tmp);
583 Decimal("size", partp->dp_size, tmp);
584 if (!sanitize_partition(partp)) {
585 warnx("ERROR: failed to adjust; setting sysid to 0");
589 if (ok("Explicitly specify beg/end address ?"))
592 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
594 tsec = DPSECT(partp->dp_ssect);
595 Decimal("beginning cylinder", tcyl, tmp);
596 Decimal("beginning head", thd, tmp);
597 Decimal("beginning sector", tsec, tmp);
598 partp->dp_scyl = DOSCYL(tcyl);
599 partp->dp_ssect = DOSSECT(tsec,tcyl);
602 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
604 tsec = DPSECT(partp->dp_esect);
605 Decimal("ending cylinder", tcyl, tmp);
606 Decimal("ending head", thd, tmp);
607 Decimal("ending sector", tsec, tmp);
608 partp->dp_ecyl = DOSCYL(tcyl);
609 partp->dp_esect = DOSSECT(tsec,tcyl);
615 } while (!ok("Are we happy with this entry?"));
622 printf("parameters extracted from in-core disklabel are:\n");
623 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
624 ,cyls,heads,sectors,cylsecs);
625 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
626 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
627 printf("parameters to be used for BIOS calculations are:\n");
628 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
629 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
633 change_active(int which)
635 struct dos_partition *partp = &mboot.parts[0];
636 int active, i, new, tmp;
639 for (i = 0; i < NDOSPART; i++) {
640 if ((partp[i].dp_flag & ACTIVE) == 0)
642 printf("Partition %d is marked active\n", i + 1);
646 if (a_flag && which != -1)
648 else if (active == -1)
651 if (!ok("Do you want to change the active partition?"))
656 Decimal("active partition", new, tmp);
657 if (new < 1 || new > 4) {
658 printf("Active partition number must be in range 1-4."
663 } while (!ok("Are you happy with this choice"));
664 for (i = 0; i < NDOSPART; i++)
665 partp[i].dp_flag = 0;
666 if (active > 0 && active <= NDOSPART)
667 partp[active-1].dp_flag = ACTIVE;
673 if (ok("Do you want to change the boot code?"))
678 get_params_to_use(void)
682 if (ok("Do you want to change our idea of what BIOS thinks ?"))
686 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
687 Decimal("BIOS's idea of #heads", dos_heads, tmp);
688 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
689 dos_cylsecs = dos_heads * dos_sectors;
692 while(!ok("Are you happy with this choice"));
697 /***********************************************\
698 * Change real numbers into strange dos numbers *
699 \***********************************************/
701 dos(struct dos_partition *partp)
706 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
707 memcpy(partp, &mtpart, sizeof(*partp));
712 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
713 cy = partp->dp_start / dos_cylsecs;
714 sec = partp->dp_start % dos_sectors + 1;
715 partp->dp_scyl = DOSCYL(cy);
716 partp->dp_ssect = DOSSECT(sec, cy);
719 end = partp->dp_start + partp->dp_size - 1;
720 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
721 cy = end / dos_cylsecs;
722 sec = end % dos_sectors + 1;
723 partp->dp_ecyl = DOSCYL(cy);
724 partp->dp_esect = DOSSECT(sec, cy);
729 /* Getting device status */
732 open_disk(int u_flag)
736 if (stat(disk, &st) == -1) {
739 warnx("can't get file status of %s", disk);
742 if ( !(st.st_mode & S_IFCHR) && p_flag == 0 )
743 warnx("device %s is not character special", disk);
745 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
748 warnx("can't open device %s", disk);
751 if (get_params() == -1) {
752 warnx("can't get disk parameters on %s", disk);
759 read_disk(off_t sector, void *buf)
761 lseek(fd,(sector * 512), 0);
763 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
766 int size = read(fd, buf, secsize);
767 if( size == secsize )
768 /* it worked so return */
772 return read( fd, buf, secsize );
774 /* we failed to read at any of the sizes */
779 write_disk(off_t sector, void *buf)
781 lseek(fd,(sector * 512), 0);
782 /* write out in the size that the read_disk found worked */
783 return write(fd, buf, secsize);
791 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
792 if (p_flag && fstat(fd, &st) == 0 && st.st_size) {
795 cylsecs = heads * sectors;
796 cyls = st.st_size / 512 / cylsecs;
798 warnx("can't get disk parameters on %s; supplying dummy ones", disk);
802 cylsecs = heads * sectors;
805 cyls = disklabel.d_ncylinders;
806 heads = disklabel.d_ntracks;
807 sectors = disklabel.d_nsectors;
808 cylsecs = heads * sectors;
812 dos_sectors = sectors;
813 dos_cylsecs = cylsecs;
814 disksecs = cyls * heads * sectors;
822 mboot.bootinst_size = secsize;
823 if (mboot.bootinst != NULL)
824 free(mboot.bootinst);
825 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
826 warnx("unable to allocate buffer to read fdisk "
830 if (read_disk(0, mboot.bootinst) == -1) {
831 warnx("can't read fdisk partition table");
834 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
835 warnx("invalid fdisk partition table found");
836 /* So should we initialize things */
839 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
855 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
857 * write enable label sector before write (if necessary),
858 * disable after writing.
859 * needed if the disklabel protected area also protects
860 * sector 0. (e.g. empty disk)
864 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
865 warn("ioctl DIOCWLABEL");
867 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
868 if (write_disk(sector,
869 &mboot.bootinst[sector * secsize]) == -1) {
870 warn("can't write fdisk partition table");
874 ioctl(fd, DIOCWLABEL, &flag);
879 ioctl(fd, DIOCWLABEL, &flag);
888 printf("%s [n] ", str);
890 if (fgets(lbuf, LBUF, stdin) == NULL)
892 lbuf[strlen(lbuf)-1] = 0;
895 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
896 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
903 decimal(char *str, int *num, int deflt)
909 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
911 if (fgets(lbuf, LBUF, stdin) == NULL)
913 lbuf[strlen(lbuf)-1] = 0;
919 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
922 while ((c = *cp++)) {
923 if (c <= '9' && c >= '0')
924 acc = acc * 10 + c - '0';
928 if (c == ' ' || c == '\t')
929 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
934 printf("%s is an invalid decimal number. Try again.\n",
942 hex(char *str, int *num, int deflt)
948 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
949 fgets(lbuf, LBUF, stdin);
950 lbuf[strlen(lbuf)-1] = 0;
956 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
959 while ((c = *cp++)) {
960 if (c <= '9' && c >= '0')
961 acc = (acc << 4) + c - '0';
962 else if (c <= 'f' && c >= 'a')
963 acc = (acc << 4) + c - 'a' + 10;
964 else if (c <= 'F' && c >= 'A')
965 acc = (acc << 4) + c - 'A' + 10;
969 if (c == ' ' || c == '\t')
970 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
975 printf("%s is an invalid hex number. Try again.\n",
982 string(char *str, char **ans)
988 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
989 fgets(lbuf, LBUF, stdin);
990 lbuf[strlen(lbuf)-1] = 0;
995 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
999 while ((c = *cp) && c != '"') cp++;
1002 while ((c = *cp) && c != ' ' && c != '\t') cp++;
1015 int numentries = (sizeof(part_types)/sizeof(struct part_type));
1017 struct part_type *ptr = part_types;
1020 while(counter < numentries)
1022 if(ptr->type == type)
1034 parse_config_line(char *line, CMD *command)
1039 while (1) /* dirty trick used to insure one exit point for this
1042 memset(command, 0, sizeof(*command));
1044 while (isspace(*cp)) ++cp;
1045 if (*cp == '\0' || *cp == '#')
1049 command->cmd = *cp++;
1056 while (isspace(*cp)) ++cp;
1059 break; /* found comment */
1063 command->args[command->n_args].argtype = *cp++;
1067 break; /* assume end of line */
1070 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1073 break; /* couldn't parse number */
1084 process_geometry(CMD *command)
1094 "ERROR line %d: the geometry specification line must occur before\n\
1095 all partition specifications",
1096 current_line_number);
1100 if (command->n_args != 3)
1102 warnx("ERROR line %d: incorrect number of geometry args",
1103 current_line_number);
1110 for (i = 0; i < 3; ++i)
1112 switch (command->args[i].argtype)
1115 dos_cyls = command->args[i].arg_val;
1118 dos_heads = command->args[i].arg_val;
1121 dos_sectors = command->args[i].arg_val;
1125 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1126 current_line_number, command->args[i].argtype,
1127 command->args[i].argtype);
1137 dos_cylsecs = dos_heads * dos_sectors;
1140 * Do sanity checks on parameter values
1144 warnx("ERROR line %d: number of cylinders not specified",
1145 current_line_number);
1148 if (dos_cyls == 0 || dos_cyls > 1024)
1151 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1152 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1153 is dedicated to DragonFly)",
1154 current_line_number, dos_cyls);
1159 warnx("ERROR line %d: number of heads not specified",
1160 current_line_number);
1163 else if (dos_heads < 1 || dos_heads > 256)
1165 warnx("ERROR line %d: number of heads must be within (1-256)",
1166 current_line_number);
1170 if (dos_sectors < 0)
1172 warnx("ERROR line %d: number of sectors not specified",
1173 current_line_number);
1176 else if (dos_sectors < 1 || dos_sectors > 63)
1178 warnx("ERROR line %d: number of sectors must be within (1-63)",
1179 current_line_number);
1190 process_partition(CMD *command)
1192 int status = 0, partition;
1193 u_int32_t prev_head_boundary, prev_cyl_boundary;
1194 u_int32_t adj_size, max_end;
1195 struct dos_partition *partp;
1200 if (command->n_args != 4)
1202 warnx("ERROR line %d: incorrect number of partition args",
1203 current_line_number);
1206 partition = command->args[0].arg_val;
1207 if (partition < 1 || partition > 4)
1209 warnx("ERROR line %d: invalid partition number %d",
1210 current_line_number, partition);
1213 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1214 bzero((char *)partp, sizeof (struct dos_partition));
1215 partp->dp_typ = command->args[1].arg_val;
1216 partp->dp_start = command->args[2].arg_val;
1217 partp->dp_size = command->args[3].arg_val;
1218 max_end = partp->dp_start + partp->dp_size;
1220 if (partp->dp_typ == 0)
1223 * Get out, the partition is marked as unused.
1226 * Insure that it's unused.
1228 bzero((char *)partp, sizeof (struct dos_partition));
1234 * Adjust start upwards, if necessary, to fall on an head boundary.
1236 if (partp->dp_start % dos_sectors != 0)
1238 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1239 if (max_end < dos_sectors ||
1240 prev_head_boundary > max_end - dos_sectors)
1243 * Can't go past end of partition
1246 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1248 current_line_number, partition);
1252 "WARNING: adjusting start offset of partition %d\n\
1253 from %u to %u, to fall on a head boundary",
1254 partition, (u_int)partp->dp_start,
1255 (u_int)(prev_head_boundary + dos_sectors));
1256 partp->dp_start = prev_head_boundary + dos_sectors;
1260 * Adjust size downwards, if necessary, to fall on a cylinder
1264 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1265 if (prev_cyl_boundary > partp->dp_start)
1266 adj_size = prev_cyl_boundary - partp->dp_start;
1270 "ERROR: could not adjust partition to start on a head boundary\n\
1271 and end on a cylinder boundary.");
1274 if (adj_size != partp->dp_size)
1277 "WARNING: adjusting size of partition %d from %u to %u\n\
1278 to end on a cylinder boundary",
1279 partition, (u_int)partp->dp_size, (u_int)adj_size);
1280 partp->dp_size = adj_size;
1282 if (partp->dp_size == 0)
1284 warnx("ERROR line %d: size of partition %d is zero",
1285 current_line_number, partition);
1298 process_active(CMD *command)
1300 int status = 0, partition, i;
1301 struct dos_partition *partp;
1305 active_processed = 1;
1306 if (command->n_args != 1)
1308 warnx("ERROR line %d: incorrect number of active args",
1309 current_line_number);
1313 partition = command->args[0].arg_val;
1314 if (partition < 1 || partition > 4)
1316 warnx("ERROR line %d: invalid partition number %d",
1317 current_line_number, partition);
1321 * Reset active partition
1323 partp = ((struct dos_partition *) &mboot.parts);
1324 for (i = 0; i < NDOSPART; i++)
1325 partp[i].dp_flag = 0;
1326 partp[partition-1].dp_flag = ACTIVE;
1336 process_line(char *line)
1343 parse_config_line(line, &command);
1344 switch (command.cmd)
1348 * Comment or blank line
1355 status = process_geometry(&command);
1358 status = process_partition(&command);
1361 status = process_active(&command);
1374 read_config(char *config_file)
1380 while (1) /* dirty trick used to insure one exit point for this
1383 if (strcmp(config_file, "-") != 0)
1386 * We're not reading from stdin
1388 if ((fp = fopen(config_file, "r")) == NULL)
1398 current_line_number = 0;
1401 if (fgets(buf, sizeof(buf), fp) == NULL)
1405 ++current_line_number;
1406 status = process_line(buf);
1417 * It doesn't matter if we're reading from stdin, as we've reached EOF
1429 struct dos_partition *partp;
1432 for (i = 0; i < 4; ++i)
1434 partp = ((struct dos_partition *) &mboot.parts) + i;
1435 bzero((char *)partp, sizeof (struct dos_partition));
1440 sanitize_partition(struct dos_partition *partp)
1442 u_int32_t prev_head_boundary, prev_cyl_boundary;
1443 u_int32_t max_end, size, start;
1445 start = partp->dp_start;
1446 size = partp->dp_size;
1447 max_end = start + size;
1448 /* Only allow a zero size if the partition is being marked unused. */
1450 if (start == 0 && partp->dp_typ == 0)
1452 warnx("ERROR: size of partition is zero");
1455 /* Return if no adjustment is necessary. */
1456 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1460 warnx("WARNING: partition overlaps with partition table");
1461 if (ok("Correct this automatically?"))
1462 start = dos_sectors;
1464 if (start % dos_sectors != 0)
1465 warnx("WARNING: partition does not start on a head boundary");
1466 if ((start +size) % dos_sectors != 0)
1467 warnx("WARNING: partition does not end on a cylinder boundary");
1468 warnx("WARNING: this may confuse the BIOS or some operating systems");
1469 if (!ok("Correct this automatically?"))
1473 * Adjust start upwards, if necessary, to fall on an head boundary.
1475 if (start % dos_sectors != 0) {
1476 prev_head_boundary = start / dos_sectors * dos_sectors;
1477 if (max_end < dos_sectors ||
1478 prev_head_boundary >= max_end - dos_sectors) {
1480 * Can't go past end of partition
1483 "ERROR: unable to adjust start of partition to fall on a head boundary");
1486 start = prev_head_boundary + dos_sectors;
1490 * Adjust size downwards, if necessary, to fall on a cylinder
1493 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1494 if (prev_cyl_boundary > start)
1495 size = prev_cyl_boundary - start;
1497 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1498 and end on a cylinder boundary.");
1502 /* Finally, commit any changes to partp and return. */
1503 if (start != partp->dp_start) {
1504 warnx("WARNING: adjusting start offset of partition to %u",
1506 partp->dp_start = start;
1508 if (size != partp->dp_size) {
1509 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1510 partp->dp_size = size;