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.14 2007/05/19 21:43:32 dillon Exp $
30 #include <sys/types.h>
31 #include <sys/diskslice.h>
32 #include <sys/diskmbr.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", "/dev/vkd0", 0
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)
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 C_flag = 0; /* use wrapped values for CHS */
121 static int I_flag = 0; /* use entire disk for DragonFly */
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 p_flag = 0; /* operate on a disk image file */
127 static int s_flag = 0; /* Print a summary and exit */
128 static int t_flag = 0; /* test only */
129 static char *f_flag = NULL; /* Read config info from file */
130 static int v_flag = 0; /* Be verbose */
139 ,{0x01, "Primary DOS with 12 bit FAT"}
140 ,{0x02, "XENIX / filesystem"}
141 ,{0x03, "XENIX /usr filesystem"}
142 ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
143 ,{0x05, "Extended DOS"}
144 ,{0x06, "Primary 'big' DOS (> 32MB)"}
145 ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
146 ,{0x08, "AIX filesystem"}
147 ,{0x09, "AIX boot partition or Coherent"}
148 ,{0x0A, "OS/2 Boot Manager or OPUS"}
149 ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
150 ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
151 ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
152 ,{0x0F, "Extended DOS, LBA"}
154 ,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
155 ,{0x12, "Compaq diagnostics"}
156 ,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
157 ,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
158 ,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
159 ,{0x18, "AST Windows swapfile"}
162 ,{0x3C, "PartitionMagic recovery"}
164 ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
165 ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
166 ,{0x43, "Linux native (sharing disk with DRDOS)"}
167 ,{0x4D, "QNX 4.2 Primary"}
168 ,{0x4E, "QNX 4.2 Secondary"}
169 ,{0x4F, "QNX 4.2 Tertiary"}
172 ,{0x52, "CP/M or Microport SysV/AT"}
175 ,{0x55, "EZ-Drive (disk manager)"}
177 ,{0x5C, "Priam Edisk (disk manager)"} /* according to S. Widlake */
179 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
180 ,{0x64, "Novell Netware 2.xx"}
181 ,{0x65, "Novell Netware 3.xx"}
182 ,{0x70, "DiskSecure Multi-Boot"}
185 ,{0x78, "QNX4.x 2nd part"}
186 ,{0x79, "QNX4.x 3rd part"}
187 ,{0x80, "Minix 1.1 ... 1.4a"}
188 ,{0x81, "Minix 1.4b ... 1.5.10"}
189 ,{0x82, "Linux swap or Solaris x86"}
190 ,{0x83, "Linux filesystem"}
191 ,{0x84, "OS/2 hidden C: drive"}
192 ,{0x85, "Linux extended"}
193 ,{0x86, "NTFS volume set??"}
194 ,{0x87, "NTFS volume set??"}
195 ,{0x93, "Amoeba filesystem"}
196 ,{0x94, "Amoeba bad block table"}
198 ,{0xA0, "Suspend to Disk"}
199 ,{0xA5, "DragonFly/FreeBSD/NetBSD/386BSD"}
204 ,{0xB7, "BSDI BSD/386 filesystem"}
205 ,{0xB8, "BSDI BSD/386 swap"}
206 ,{0xBE, "Solaris x86 boot"}
207 ,{0xC1, "DRDOS/sec with 12-bit FAT"}
208 ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
209 ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
211 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
215 ,{0xEB, "BeOS file system"}
217 ,{0xEF, "EFI System Partition"}
219 ,{0xF2, "DOS 3.3+ Secondary"}
221 ,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
222 ,{0xFF, "BBT (Bad Blocks Table)"}
225 static void print_s0(int which);
226 static void print_part(int i);
227 static void init_sector0(unsigned long start);
228 static void init_boot(void);
229 static void change_part(int i);
230 static void print_params();
231 static void change_active(int which);
232 static void change_code();
233 static void get_params_to_use();
234 static void dos(struct dos_partition *partp);
235 static int open_disk(int u_flag);
236 static ssize_t read_disk(off_t sector, void *buf);
237 static ssize_t write_disk(off_t sector, void *buf);
238 static int get_params();
239 static int read_s0();
240 static int write_s0();
241 static int ok(char *str);
242 static int decimal(char *str, int *num, int deflt);
243 static char *get_type(int type);
244 static int read_config(char *config_file);
245 static void reset_boot(void);
246 static int sanitize_partition(struct dos_partition *);
247 static void usage(void);
249 static int hex(char *str, int *num, int deflt);
250 static int string(char *str, char **ans);
255 main(int argc, char *argv[])
259 while ((c = getopt(argc, argv, "BCIab:f:p:istuv1234")) != -1)
307 if (f_flag || i_flag)
315 static char realname[12];
317 if(strncmp(argv[0], "/dev", 4) == 0) {
320 snprintf(realname, 12, "/dev/%s", argv[0]);
324 if (open_disk(u_flag) < 0)
325 err(1, "cannot open disk %s", disk);
326 } else if (disk == NULL) {
329 for(i = 0; disks[i]; i++)
332 rv = open_disk(u_flag);
336 err(1, "cannot open any disk");
338 if (open_disk(u_flag) < 0)
339 err(1, "cannot open disk %s", disk);
342 /* (abu)use mboot.bootinst to probe for the sector size */
343 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
344 err(1, "cannot allocate buffer to determine disk sector size");
345 read_disk(0, mboot.bootinst);
346 free(mboot.bootinst);
347 mboot.bootinst = NULL;
352 struct dos_partition *partp;
356 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
358 printf("Part %11s %11s Type Flags\n", "Start", "Size");
359 for (i = 0; i < NDOSPART; i++) {
360 partp = ((struct dos_partition *) &mboot.parts) + i;
361 if (partp->dp_start == 0 && partp->dp_size == 0)
363 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
364 (u_long) partp->dp_start,
365 (u_long) partp->dp_size, partp->dp_typ,
371 printf("******* Working on device %s *******\n",disk);
375 struct dos_partition *partp;
379 partp = (struct dos_partition *) (&mboot.parts[0]);
380 partp->dp_typ = DOSPTYP_386BSD;
381 partp->dp_flag = ACTIVE;
382 partp->dp_start = dos_sectors;
383 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
394 if (read_s0() || i_flag)
399 if (!read_config(f_flag))
424 init_sector0(dos_sectors);
426 printf("Media sector size is %d\n", secsize);
427 printf("Warning: BIOS sector numbering starts with sector 1\n");
428 printf("Information from DOS bootblock is:\n");
430 for (i = 1; i <= NDOSPART; i++)
433 change_part(partition);
435 if (u_flag || a_flag)
436 change_active(partition);
441 if (u_flag || a_flag || B_flag) {
443 printf("\nWe haven't changed the partition table yet. ");
444 printf("This is your last chance.\n");
448 if (ok("Should we write new partition table?"))
453 printf("\n-t flag specified -- partition table not written.\n");
464 fprintf(stderr, "%s%s",
465 "usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
466 " fdisk -f configfile [-itv] [disk]\n");
476 printf("Information from DOS bootblock is:\n");
478 for (i = 1; i <= NDOSPART; i++)
479 printf("%d: ", i), print_part(i);
484 static struct dos_partition mtpart = { 0 };
489 struct dos_partition *partp;
492 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
494 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
495 printf("<UNUSED>\n");
499 * Be careful not to overflow.
501 part_mb = partp->dp_size;
503 part_mb /= (1024 * 1024);
504 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
505 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
506 (u_long)partp->dp_start,
507 (u_long)partp->dp_size,
510 partp->dp_flag == ACTIVE ? " (active)" : "");
511 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
512 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
514 ,DPSECT(partp->dp_ssect)
515 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
517 ,DPSECT(partp->dp_esect));
528 fname = b_flag ? b_flag : "/boot/mbr";
529 if ((fd = open(fname, O_RDONLY)) == -1 ||
530 fstat(fd, &sb) == -1)
532 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
533 errx(1, "%s: length must be a multiple of sector size", fname);
534 if (mboot.bootinst != NULL)
535 free(mboot.bootinst);
536 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
537 errx(1, "%s: unable to allocate read buffer", fname);
538 if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
541 if (n != mboot.bootinst_size)
542 errx(1, "%s: short read", fname);
547 init_sector0(unsigned long start)
549 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
553 partp->dp_typ = DOSPTYP_386BSD;
554 partp->dp_flag = ACTIVE;
555 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
558 partp->dp_start = start;
559 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
567 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
569 printf("The data for partition %d is:\n", i);
572 if (u_flag && ok("Do you want to change it?")) {
576 bzero((char *)partp, sizeof (struct dos_partition));
579 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
585 Decimal("sysid (165=DragonFly)", partp->dp_typ, tmp);
586 Decimal("start", partp->dp_start, tmp);
587 Decimal("size", partp->dp_size, tmp);
588 if (!sanitize_partition(partp)) {
589 warnx("ERROR: failed to adjust; setting sysid to 0");
593 if (ok("Explicitly specify beg/end address ?"))
596 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
598 tsec = DPSECT(partp->dp_ssect);
599 Decimal("beginning cylinder", tcyl, tmp);
600 Decimal("beginning head", thd, tmp);
601 Decimal("beginning sector", tsec, tmp);
602 if (tcyl > MAXCYL && C_flag == 0) {
603 printf("Warning: starting cylinder wraps, using all 1's\n");
605 partp->dp_ssect = -1;
608 partp->dp_scyl = DOSCYL(tcyl);
609 partp->dp_ssect = DOSSECT(tsec,tcyl);
613 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
615 tsec = DPSECT(partp->dp_esect);
616 Decimal("ending cylinder", tcyl, tmp);
617 Decimal("ending head", thd, tmp);
618 Decimal("ending sector", tsec, tmp);
619 if (tcyl > MAXCYL && C_flag == 0) {
620 printf("Warning: ending cylinder wraps, using all 1's\n");
622 partp->dp_esect = -1;
625 partp->dp_ecyl = DOSCYL(tcyl);
626 partp->dp_esect = DOSSECT(tsec,tcyl);
633 } while (!ok("Are we happy with this entry?"));
640 printf("parameters extracted from device are:\n");
641 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
642 ,cyls,heads,sectors,cylsecs);
643 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
644 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
645 printf("parameters to be used for BIOS calculations are:\n");
646 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
647 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
651 change_active(int which)
653 struct dos_partition *partp = &mboot.parts[0];
654 int active, i, new, tmp;
657 for (i = 0; i < NDOSPART; i++) {
658 if ((partp[i].dp_flag & ACTIVE) == 0)
660 printf("Partition %d is marked active\n", i + 1);
664 if (a_flag && which != -1)
666 else if (active == -1)
669 if (!ok("Do you want to change the active partition?"))
674 Decimal("active partition", new, tmp);
675 if (new < 1 || new > 4) {
676 printf("Active partition number must be in range 1-4."
681 } while (!ok("Are you happy with this choice"));
682 for (i = 0; i < NDOSPART; i++)
683 partp[i].dp_flag = 0;
684 if (active > 0 && active <= NDOSPART)
685 partp[active-1].dp_flag = ACTIVE;
691 if (ok("Do you want to change the boot code?"))
696 get_params_to_use(void)
700 if (ok("Do you want to change our idea of what BIOS thinks ?"))
704 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
705 Decimal("BIOS's idea of #heads", dos_heads, tmp);
706 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
707 dos_cylsecs = dos_heads * dos_sectors;
710 while(!ok("Are you happy with this choice"));
715 /***********************************************\
716 * Change real numbers into strange dos numbers *
717 \***********************************************/
719 dos(struct dos_partition *partp)
724 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
725 memcpy(partp, &mtpart, sizeof(*partp));
730 cy = partp->dp_start / dos_cylsecs;
731 sec = partp->dp_start % dos_sectors + 1;
732 if (cy > MAXCYL && C_flag == 0) {
733 printf("Warning: starting cylinder wraps, using all 1's\n");
736 partp->dp_ssect = -1;
738 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
739 partp->dp_scyl = DOSCYL(cy);
740 partp->dp_ssect = DOSSECT(sec, cy);
744 end = partp->dp_start + partp->dp_size - 1;
745 cy = end / dos_cylsecs;
746 sec = end % dos_sectors + 1;
747 if (cy > MAXCYL && C_flag == 0) {
748 printf("Warning: ending cylinder wraps, using all 1's\n");
751 partp->dp_esect = -1;
753 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
754 partp->dp_ecyl = DOSCYL(cy);
755 partp->dp_esect = DOSSECT(sec, cy);
761 /* Getting device status */
764 open_disk(int u_flag)
768 if (stat(disk, &st) == -1) {
771 warnx("can't get file status of %s", disk);
774 if ( !(st.st_mode & S_IFCHR) && p_flag == 0 )
775 warnx("device %s is not character special", disk);
777 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
780 warnx("can't open device %s", disk);
783 if (get_params() == -1) {
784 warnx("can't get disk parameters on %s", disk);
791 read_disk(off_t sector, void *buf)
793 lseek(fd,(sector * 512), 0);
795 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
798 int size = read(fd, buf, secsize);
799 if( size == secsize )
800 /* it worked so return */
804 return read( fd, buf, secsize );
806 /* we failed to read at any of the sizes */
811 write_disk(off_t sector, void *buf)
813 lseek(fd,(sector * 512), 0);
814 /* write out in the size that the read_disk found worked */
815 return write(fd, buf, secsize);
821 struct partinfo partinfo; /* disk parameters */
824 if (ioctl(fd, DIOCGPART, &partinfo) == -1) {
825 if (p_flag && fstat(fd, &st) == 0 && st.st_size) {
828 cylsecs = heads * sectors;
829 cyls = st.st_size / 512 / cylsecs;
831 warnx("can't get disk parameters on %s; supplying dummy ones",
834 cylsecs = heads * sectors;
837 cyls = partinfo.d_ncylinders;
838 heads = partinfo.d_nheads;
839 sectors = partinfo.d_secpertrack;
840 cylsecs = heads * sectors;
844 dos_sectors = sectors;
845 dos_cylsecs = cylsecs;
846 disksecs = cyls * heads * sectors;
854 mboot.bootinst_size = secsize;
855 if (mboot.bootinst != NULL)
856 free(mboot.bootinst);
857 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
858 warnx("unable to allocate buffer to read fdisk "
862 if (read_disk(0, mboot.bootinst) == -1) {
863 warnx("can't read fdisk partition table");
866 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
867 warnx("invalid fdisk partition table found");
868 /* So should we initialize things */
871 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
887 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
889 * write enable label sector before write (if necessary),
890 * disable after writing.
891 * needed if the disklabel protected area also protects
892 * sector 0. (e.g. empty disk)
896 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
897 warn("ioctl DIOCWLABEL");
899 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
900 if (write_disk(sector,
901 &mboot.bootinst[sector * secsize]) == -1) {
902 warn("can't write fdisk partition table");
906 ioctl(fd, DIOCWLABEL, &flag);
911 ioctl(fd, DIOCWLABEL, &flag);
920 printf("%s [n] ", str);
922 if (fgets(lbuf, LBUF, stdin) == NULL)
924 lbuf[strlen(lbuf)-1] = 0;
927 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
928 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
935 decimal(char *str, int *num, int deflt)
941 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
943 if (fgets(lbuf, LBUF, stdin) == NULL)
945 lbuf[strlen(lbuf)-1] = 0;
951 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
954 while ((c = *cp++)) {
955 if (c <= '9' && c >= '0')
956 acc = acc * 10 + c - '0';
960 if (c == ' ' || c == '\t')
961 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
966 printf("%s is an invalid decimal number. Try again.\n",
974 hex(char *str, int *num, int deflt)
980 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
981 fgets(lbuf, LBUF, stdin);
982 lbuf[strlen(lbuf)-1] = 0;
988 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
991 while ((c = *cp++)) {
992 if (c <= '9' && c >= '0')
993 acc = (acc << 4) + c - '0';
994 else if (c <= 'f' && c >= 'a')
995 acc = (acc << 4) + c - 'a' + 10;
996 else if (c <= 'F' && c >= 'A')
997 acc = (acc << 4) + c - 'A' + 10;
1001 if (c == ' ' || c == '\t')
1002 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1007 printf("%s is an invalid hex number. Try again.\n",
1014 string(char *str, char **ans)
1020 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
1021 fgets(lbuf, LBUF, stdin);
1022 lbuf[strlen(lbuf)-1] = 0;
1027 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1031 while ((c = *cp) && c != '"') cp++;
1034 while ((c = *cp) && c != ' ' && c != '\t') cp++;
1047 int numentries = (sizeof(part_types)/sizeof(struct part_type));
1049 struct part_type *ptr = part_types;
1052 while(counter < numentries)
1054 if(ptr->type == type)
1066 parse_config_line(char *line, CMD *command)
1071 while (1) /* dirty trick used to insure one exit point for this
1074 memset(command, 0, sizeof(*command));
1076 while (isspace(*cp)) ++cp;
1077 if (*cp == '\0' || *cp == '#')
1081 command->cmd = *cp++;
1088 while (isspace(*cp)) ++cp;
1091 break; /* found comment */
1095 command->args[command->n_args].argtype = *cp++;
1099 break; /* assume end of line */
1102 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1105 break; /* couldn't parse number */
1116 process_geometry(CMD *command)
1126 "ERROR line %d: the geometry specification line must occur before\n\
1127 all partition specifications",
1128 current_line_number);
1132 if (command->n_args != 3)
1134 warnx("ERROR line %d: incorrect number of geometry args",
1135 current_line_number);
1142 for (i = 0; i < 3; ++i)
1144 switch (command->args[i].argtype)
1147 dos_cyls = command->args[i].arg_val;
1150 dos_heads = command->args[i].arg_val;
1153 dos_sectors = command->args[i].arg_val;
1157 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1158 current_line_number, command->args[i].argtype,
1159 command->args[i].argtype);
1169 dos_cylsecs = dos_heads * dos_sectors;
1172 * Do sanity checks on parameter values
1176 warnx("ERROR line %d: number of cylinders not specified",
1177 current_line_number);
1180 if (dos_cyls == 0 || dos_cyls > 1024)
1183 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1184 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1185 is dedicated to DragonFly)",
1186 current_line_number, dos_cyls);
1191 warnx("ERROR line %d: number of heads not specified",
1192 current_line_number);
1195 else if (dos_heads < 1 || dos_heads > 256)
1197 warnx("ERROR line %d: number of heads must be within (1-256)",
1198 current_line_number);
1202 if (dos_sectors < 0)
1204 warnx("ERROR line %d: number of sectors not specified",
1205 current_line_number);
1208 else if (dos_sectors < 1 || dos_sectors > 63)
1210 warnx("ERROR line %d: number of sectors must be within (1-63)",
1211 current_line_number);
1222 process_partition(CMD *command)
1224 int status = 0, partition;
1225 u_int32_t prev_head_boundary, prev_cyl_boundary;
1226 u_int32_t adj_size, max_end;
1227 struct dos_partition *partp;
1232 if (command->n_args != 4)
1234 warnx("ERROR line %d: incorrect number of partition args",
1235 current_line_number);
1238 partition = command->args[0].arg_val;
1239 if (partition < 1 || partition > 4)
1241 warnx("ERROR line %d: invalid partition number %d",
1242 current_line_number, partition);
1245 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1246 bzero((char *)partp, sizeof (struct dos_partition));
1247 partp->dp_typ = command->args[1].arg_val;
1248 partp->dp_start = command->args[2].arg_val;
1249 partp->dp_size = command->args[3].arg_val;
1250 max_end = partp->dp_start + partp->dp_size;
1252 if (partp->dp_typ == 0)
1255 * Get out, the partition is marked as unused.
1258 * Insure that it's unused.
1260 bzero((char *)partp, sizeof (struct dos_partition));
1266 * Adjust start upwards, if necessary, to fall on an head boundary.
1268 if (partp->dp_start % dos_sectors != 0)
1270 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1271 if (max_end < dos_sectors ||
1272 prev_head_boundary > max_end - dos_sectors)
1275 * Can't go past end of partition
1278 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1280 current_line_number, partition);
1284 "WARNING: adjusting start offset of partition %d\n\
1285 from %u to %u, to fall on a head boundary",
1286 partition, (u_int)partp->dp_start,
1287 (u_int)(prev_head_boundary + dos_sectors));
1288 partp->dp_start = prev_head_boundary + dos_sectors;
1292 * Adjust size downwards, if necessary, to fall on a cylinder
1296 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1297 if (prev_cyl_boundary > partp->dp_start)
1298 adj_size = prev_cyl_boundary - partp->dp_start;
1302 "ERROR: could not adjust partition to start on a head boundary\n\
1303 and end on a cylinder boundary.");
1306 if (adj_size != partp->dp_size)
1309 "WARNING: adjusting size of partition %d from %u to %u\n\
1310 to end on a cylinder boundary",
1311 partition, (u_int)partp->dp_size, (u_int)adj_size);
1312 partp->dp_size = adj_size;
1314 if (partp->dp_size == 0)
1316 warnx("ERROR line %d: size of partition %d is zero",
1317 current_line_number, partition);
1330 process_active(CMD *command)
1332 int status = 0, partition, i;
1333 struct dos_partition *partp;
1337 active_processed = 1;
1338 if (command->n_args != 1)
1340 warnx("ERROR line %d: incorrect number of active args",
1341 current_line_number);
1345 partition = command->args[0].arg_val;
1346 if (partition < 1 || partition > 4)
1348 warnx("ERROR line %d: invalid partition number %d",
1349 current_line_number, partition);
1353 * Reset active partition
1355 partp = ((struct dos_partition *) &mboot.parts);
1356 for (i = 0; i < NDOSPART; i++)
1357 partp[i].dp_flag = 0;
1358 partp[partition-1].dp_flag = ACTIVE;
1368 process_line(char *line)
1375 parse_config_line(line, &command);
1376 switch (command.cmd)
1380 * Comment or blank line
1387 status = process_geometry(&command);
1390 status = process_partition(&command);
1393 status = process_active(&command);
1406 read_config(char *config_file)
1412 while (1) /* dirty trick used to insure one exit point for this
1415 if (strcmp(config_file, "-") != 0)
1418 * We're not reading from stdin
1420 if ((fp = fopen(config_file, "r")) == NULL)
1430 current_line_number = 0;
1433 if (fgets(buf, sizeof(buf), fp) == NULL)
1437 ++current_line_number;
1438 status = process_line(buf);
1449 * It doesn't matter if we're reading from stdin, as we've reached EOF
1461 struct dos_partition *partp;
1464 for (i = 0; i < 4; ++i)
1466 partp = ((struct dos_partition *) &mboot.parts) + i;
1467 bzero((char *)partp, sizeof (struct dos_partition));
1472 sanitize_partition(struct dos_partition *partp)
1474 u_int32_t prev_head_boundary, prev_cyl_boundary;
1475 u_int32_t max_end, size, start;
1477 start = partp->dp_start;
1478 size = partp->dp_size;
1479 max_end = start + size;
1480 /* Only allow a zero size if the partition is being marked unused. */
1482 if (start == 0 && partp->dp_typ == 0)
1484 warnx("ERROR: size of partition is zero");
1487 /* Return if no adjustment is necessary. */
1488 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1492 warnx("WARNING: partition overlaps with partition table");
1493 if (ok("Correct this automatically?"))
1494 start = dos_sectors;
1496 if (start % dos_sectors != 0)
1497 warnx("WARNING: partition does not start on a head boundary");
1498 if ((start +size) % dos_sectors != 0)
1499 warnx("WARNING: partition does not end on a cylinder boundary");
1500 warnx("WARNING: this may confuse the BIOS or some operating systems");
1501 if (!ok("Correct this automatically?"))
1505 * Adjust start upwards, if necessary, to fall on an head boundary.
1507 if (start % dos_sectors != 0) {
1508 prev_head_boundary = start / dos_sectors * dos_sectors;
1509 if (max_end < dos_sectors ||
1510 prev_head_boundary >= max_end - dos_sectors) {
1512 * Can't go past end of partition
1515 "ERROR: unable to adjust start of partition to fall on a head boundary");
1518 start = prev_head_boundary + dos_sectors;
1522 * Adjust size downwards, if necessary, to fall on a cylinder
1525 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1526 if (prev_cyl_boundary > start)
1527 size = prev_cyl_boundary - start;
1529 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1530 and end on a cylinder boundary.");
1534 /* Finally, commit any changes to partp and return. */
1535 if (start != partp->dp_start) {
1536 warnx("WARNING: adjusting start offset of partition to %u",
1538 partp->dp_start = start;
1540 if (size != partp->dp_size) {
1541 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1542 partp->dp_size = size;