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.16 2008/04/23 22:09:07 thomas Exp $
30 #include <sys/types.h>
31 #include <sys/diskslice.h>
32 #include <sys/diskmbr.h>
33 #include <sys/ioctl_compat.h>
34 #include <sys/sysctl.h>
49 static char lbuf[LBUF];
55 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
57 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
58 * Copyright (c) 1989 Robert. V. Baron
62 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
63 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
64 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
66 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
68 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
69 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
70 int secsize = 0; /* the sensed sector size */
75 "/dev/ad0", "/dev/da0", "/dev/vkd0", 0
78 int cyls, sectors, heads, cylsecs;
83 unsigned char padding[2]; /* force the longs to be long aligned */
84 unsigned char *bootinst; /* boot code */
86 struct dos_partition parts[4];
88 struct mboot mboot = {{0}, NULL, 0};
91 #define BOOT_MAGIC 0xAA55
98 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
99 #define DOSCYL(c) (c & 0xff)
101 static int partition = -1;
106 static int current_line_number;
108 static int geom_processed = 0;
109 static int part_processed = 0;
110 static int active_processed = 0;
123 static int B_flag = 0; /* replace boot code */
124 static int C_flag = 0; /* use wrapped values for CHS */
125 static int E_flag = 0; /* Erase through TRIM */
126 static int I_flag = 0; /* use entire disk for DragonFly */
127 static int a_flag = 0; /* set active partition */
128 static char *b_flag = NULL; /* path to boot code */
129 static int i_flag = 0; /* replace partition data */
130 static int u_flag = 0; /* update partition data */
131 static int p_flag = 0; /* operate on a disk image file */
132 static int s_flag = 0; /* Print a summary and exit */
133 static int t_flag = 0; /* test only */
134 static char *f_flag = NULL; /* Read config info from file */
135 static int v_flag = 0; /* Be verbose */
144 ,{0x01, "Primary DOS with 12 bit FAT"}
145 ,{0x02, "XENIX / filesystem"}
146 ,{0x03, "XENIX /usr filesystem"}
147 ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
148 ,{0x05, "Extended DOS"}
149 ,{0x06, "Primary 'big' DOS (> 32MB)"}
150 ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
151 ,{0x08, "AIX filesystem"}
152 ,{0x09, "AIX boot partition or Coherent"}
153 ,{0x0A, "OS/2 Boot Manager or OPUS"}
154 ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
155 ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
156 ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
157 ,{0x0F, "Extended DOS, LBA"}
159 ,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
160 ,{0x12, "Compaq diagnostics"}
161 ,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
162 ,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
163 ,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
164 ,{0x18, "AST Windows swapfile"}
167 ,{0x3C, "PartitionMagic recovery"}
169 ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
170 ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
171 ,{0x43, "Linux native (sharing disk with DRDOS)"}
172 ,{0x4D, "QNX 4.2 Primary"}
173 ,{0x4E, "QNX 4.2 Secondary"}
174 ,{0x4F, "QNX 4.2 Tertiary"}
177 ,{0x52, "CP/M or Microport SysV/AT"}
180 ,{0x55, "EZ-Drive (disk manager)"}
182 ,{0x5C, "Priam Edisk (disk manager)"} /* according to S. Widlake */
184 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
185 ,{0x64, "Novell Netware 2.xx"}
186 ,{0x65, "Novell Netware 3.xx"}
187 ,{0x70, "DiskSecure Multi-Boot"}
190 ,{0x78, "QNX4.x 2nd part"}
191 ,{0x79, "QNX4.x 3rd part"}
192 ,{0x80, "Minix 1.1 ... 1.4a"}
193 ,{0x81, "Minix 1.4b ... 1.5.10"}
194 ,{0x82, "Linux swap or Solaris x86"}
195 ,{0x83, "Linux filesystem"}
196 ,{0x84, "OS/2 hidden C: drive"}
197 ,{0x85, "Linux extended"}
198 ,{0x86, "NTFS volume set??"}
199 ,{0x87, "NTFS volume set??"}
200 ,{0x93, "Amoeba filesystem"}
201 ,{0x94, "Amoeba bad block table"}
203 ,{0xA0, "Suspend to Disk"}
204 ,{0xA5, "DragonFly/FreeBSD/NetBSD/386BSD"}
209 ,{0xB7, "BSDI BSD/386 filesystem"}
210 ,{0xB8, "BSDI BSD/386 swap"}
211 ,{0xBE, "Solaris x86 boot"}
212 ,{0xC1, "DRDOS/sec with 12-bit FAT"}
213 ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
214 ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
216 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
220 ,{0xEB, "BeOS file system"}
222 ,{0xEF, "EFI System Partition"}
224 ,{0xF2, "DOS 3.3+ Secondary"}
226 ,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
227 ,{0xFF, "BBT (Bad Blocks Table)"}
230 static void print_s0(int which);
231 static void print_part(int i);
232 static void init_sector0(unsigned long start);
233 static void init_boot(void);
234 static void change_part(int i);
235 static void print_params();
236 static void change_active(int which);
237 static void change_code();
238 static void get_params_to_use();
239 static void dos(struct dos_partition *partp);
240 static int open_disk(int u_flag);
241 static void erase_partition(int i);
242 static ssize_t read_disk(off_t sector, void *buf);
243 static ssize_t write_disk(off_t sector, void *buf);
244 static int get_params();
245 static int read_s0();
246 static int write_s0();
247 static int ok(char *str);
248 static int decimal(char *str, int *num, int deflt);
249 static char *get_type(int type);
250 static int read_config(char *config_file);
251 static void reset_boot(void);
252 static int sanitize_partition(struct dos_partition *);
253 static void usage(void);
255 static int hex(char *str, int *num, int deflt);
256 static int string(char *str, char **ans);
261 main(int argc, char *argv[])
265 while ((c = getopt(argc, argv, "BCEIab:f:p:istuv1234")) != -1)
316 if (f_flag || i_flag)
324 disk = getdevpath(argv[0], 0);
325 if (open_disk(u_flag) < 0)
326 err(1, "cannot open disk %s", disk);
327 } else if (disk == NULL) {
330 for(i = 0; disks[i]; i++)
333 rv = open_disk(u_flag);
337 err(1, "cannot open any disk");
339 if (open_disk(u_flag) < 0)
340 err(1, "cannot open disk %s", disk);
343 /* (abu)use mboot.bootinst to probe for the sector size */
344 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
345 err(1, "cannot allocate buffer to determine disk sector size");
346 read_disk(0, mboot.bootinst);
347 free(mboot.bootinst);
348 mboot.bootinst = NULL;
353 struct dos_partition *partp;
357 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
359 printf("Part %11s %11s Type Flags\n", "Start", "Size");
360 for (i = 0; i < NDOSPART; i++) {
361 partp = ((struct dos_partition *) &mboot.parts) + i;
362 if (partp->dp_start == 0 && partp->dp_size == 0)
364 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
365 (u_long) partp->dp_start,
366 (u_long) partp->dp_size, partp->dp_typ,
372 printf("******* Working on device %s *******\n",disk);
376 struct dos_partition *partp;
380 partp = (struct dos_partition *) (&mboot.parts[0]);
381 partp->dp_typ = DOSPTYP_386BSD;
382 partp->dp_flag = ACTIVE;
383 partp->dp_start = dos_sectors;
384 if (disksecs - dos_sectors > 0xFFFFFFFFU) {
385 printf("Warning: Ending logical block > 2TB, using max value\n");
386 partp->dp_size = 0xFFFFFFFFU;
388 partp->dp_size = (disksecs / dos_cylsecs) *
389 dos_cylsecs - dos_sectors;
396 /* Trim now if we're using the entire device */
406 if (read_s0() || i_flag)
411 if (!read_config(f_flag))
436 init_sector0(dos_sectors);
438 printf("Media sector size is %d\n", secsize);
439 printf("Warning: BIOS sector numbering starts with sector 1\n");
440 printf("Information from DOS bootblock is:\n");
442 for (i = 1; i <= NDOSPART; i++)
445 change_part(partition);
447 if (u_flag || a_flag)
448 change_active(partition);
453 if (u_flag || a_flag || B_flag) {
455 printf("\nWe haven't changed the partition table yet. ");
456 printf("This is your last chance.\n");
460 if (ok("Should we write new partition table?")) {
461 if (E_flag && u_flag) {
463 * Trim now because we've committed to
464 * updating the partition.
467 for (i = 0; i < NDOSPART; i++)
470 erase_partition(partition);
477 printf("\n-t flag specified -- partition table not written.\n");
488 fprintf(stderr, "%s%s",
489 "usage: fdisk [-BCIaistu] [-b bootcode] [-p diskimage] [-1234] [disk]\n",
490 " fdisk -f configfile [-itv] [disk]\n");
500 printf("Information from DOS bootblock is:\n");
502 for (i = 1; i <= NDOSPART; i++)
503 printf("%d: ", i), print_part(i);
508 static struct dos_partition mtpart = { 0 };
513 struct dos_partition *partp;
516 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
518 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
519 printf("<UNUSED>\n");
523 * Be careful not to overflow.
525 part_mb = partp->dp_size;
527 part_mb /= (1024 * 1024);
528 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
529 printf(" start %lu, size %lu (%jd Meg), flag %x%s\n",
530 (u_long)partp->dp_start,
531 (u_long)partp->dp_size,
534 partp->dp_flag == ACTIVE ? " (active)" : "");
535 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
536 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
538 ,DPSECT(partp->dp_ssect)
539 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
541 ,DPSECT(partp->dp_esect));
552 fname = b_flag ? b_flag : "/boot/mbr";
553 if ((fd = open(fname, O_RDONLY)) == -1 ||
554 fstat(fd, &sb) == -1)
556 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
557 errx(1, "%s: length must be a multiple of sector size", fname);
558 if (mboot.bootinst != NULL)
559 free(mboot.bootinst);
560 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
561 errx(1, "%s: unable to allocate read buffer", fname);
562 if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
565 if (n != mboot.bootinst_size)
566 errx(1, "%s: short read", fname);
571 init_sector0(unsigned long start)
573 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
577 partp->dp_typ = DOSPTYP_386BSD;
578 partp->dp_flag = ACTIVE;
579 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
582 partp->dp_start = start;
583 if (disksecs - start > 0xFFFFFFFFU) {
584 printf("Warning: Ending logical block > 2TB, using max value\n");
585 partp->dp_size = 0xFFFFFFFFU;
587 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
596 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
598 printf("The data for partition %d is:\n", i);
601 if (u_flag && ok("Do you want to change it?")) {
605 bzero((char *)partp, sizeof (struct dos_partition));
608 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
614 Decimal("sysid (165=DragonFly)", partp->dp_typ, tmp);
615 Decimal("start", partp->dp_start, tmp);
616 Decimal("size", partp->dp_size, tmp);
617 if (!sanitize_partition(partp)) {
618 warnx("ERROR: failed to adjust; setting sysid to 0");
622 if (ok("Explicitly specify beg/end address ?"))
625 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
627 tsec = DPSECT(partp->dp_ssect);
628 Decimal("beginning cylinder", tcyl, tmp);
629 Decimal("beginning head", thd, tmp);
630 Decimal("beginning sector", tsec, tmp);
631 if (tcyl > MAXCYL && C_flag == 0) {
632 printf("Warning: starting cylinder wraps, using all 1's\n");
634 partp->dp_ssect = -1;
637 partp->dp_scyl = DOSCYL(tcyl);
638 partp->dp_ssect = DOSSECT(tsec,tcyl);
642 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
644 tsec = DPSECT(partp->dp_esect);
645 Decimal("ending cylinder", tcyl, tmp);
646 Decimal("ending head", thd, tmp);
647 Decimal("ending sector", tsec, tmp);
648 if (tcyl > MAXCYL && C_flag == 0) {
649 printf("Warning: ending cylinder wraps, using all 1's\n");
651 partp->dp_esect = -1;
654 partp->dp_ecyl = DOSCYL(tcyl);
655 partp->dp_esect = DOSSECT(tsec,tcyl);
662 } while (!ok("Are we happy with this entry?"));
669 printf("parameters extracted from device are:\n");
670 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
671 ,cyls,heads,sectors,cylsecs);
672 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
673 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
674 printf("parameters to be used for BIOS calculations are:\n");
675 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
676 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
680 change_active(int which)
682 struct dos_partition *partp = &mboot.parts[0];
683 int active, i, new, tmp;
686 for (i = 0; i < NDOSPART; i++) {
687 if ((partp[i].dp_flag & ACTIVE) == 0)
689 printf("Partition %d is marked active\n", i + 1);
693 if (a_flag && which != -1)
695 else if (active == -1)
698 if (!ok("Do you want to change the active partition?"))
703 Decimal("active partition", new, tmp);
704 if (new < 1 || new > 4) {
705 printf("Active partition number must be in range 1-4."
710 } while (!ok("Are you happy with this choice"));
711 for (i = 0; i < NDOSPART; i++)
712 partp[i].dp_flag = 0;
713 if (active > 0 && active <= NDOSPART)
714 partp[active-1].dp_flag = ACTIVE;
720 if (ok("Do you want to change the boot code?"))
725 get_params_to_use(void)
729 if (ok("Do you want to change our idea of what BIOS thinks ?"))
733 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
734 Decimal("BIOS's idea of #heads", dos_heads, tmp);
735 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
736 dos_cylsecs = dos_heads * dos_sectors;
739 while(!ok("Are you happy with this choice"));
744 /***********************************************\
745 * Change real numbers into strange dos numbers *
746 \***********************************************/
748 dos(struct dos_partition *partp)
753 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
754 memcpy(partp, &mtpart, sizeof(*partp));
759 cy = partp->dp_start / dos_cylsecs;
760 sec = partp->dp_start % dos_sectors + 1;
761 if (cy > MAXCYL && C_flag == 0) {
762 printf("Warning: starting cylinder wraps, using all 1's\n");
765 partp->dp_ssect = -1;
767 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
768 partp->dp_scyl = DOSCYL(cy);
769 partp->dp_ssect = DOSSECT(sec, cy);
773 end = partp->dp_start + partp->dp_size - 1;
774 cy = end / dos_cylsecs;
775 sec = end % dos_sectors + 1;
776 if (cy > MAXCYL && C_flag == 0) {
777 printf("Warning: ending cylinder wraps, using all 1's\n");
780 partp->dp_esect = -1;
782 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
783 partp->dp_ecyl = DOSCYL(cy);
784 partp->dp_esect = DOSSECT(sec, cy);
791 erase_partition(int i)
793 struct dos_partition *partp;
796 char sysctl_name[64];
797 int trim_enabled = 0;
798 size_t olen = sizeof(trim_enabled);
799 char *dev_name = strdup(disk);
801 dev_name = strtok(dev_name + strlen("/dev/da"),"s");
802 sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled", dev_name);
803 sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0);
804 if(errno == ENOENT) {
805 printf("Device:%s does not support the TRIM command\n", disk);
809 printf("Erase device option selected, but sysctl (%s) "
810 "is not enabled\n",sysctl_name);
813 partp = ((struct dos_partition *) &mboot.parts) + i;
814 printf("erase sectors:%u %u\n",
818 /* Trim the Device */
819 ioarg[0] = partp->dp_start;
821 ioarg[1] = partp->dp_size;
824 if (ioctl(fd, IOCTLTRIM, ioarg) < 0) {
825 printf("Device trim failed\n");
830 /* Getting device status */
833 open_disk(int u_flag)
837 if (stat(disk, &st) == -1) {
840 warnx("can't get file status of %s", disk);
843 if ( !(st.st_mode & S_IFCHR) && p_flag == 0 )
844 warnx("device %s is not character special", disk);
846 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
849 warnx("can't open device %s", disk);
852 if (get_params() == -1) {
853 warnx("can't get disk parameters on %s", disk);
860 read_disk(off_t sector, void *buf)
862 lseek(fd,(sector * 512), 0);
864 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
867 int size = read(fd, buf, secsize);
868 if( size == secsize )
869 /* it worked so return */
873 return read( fd, buf, secsize );
875 /* we failed to read at any of the sizes */
880 write_disk(off_t sector, void *buf)
882 lseek(fd,(sector * 512), 0);
883 /* write out in the size that the read_disk found worked */
884 return write(fd, buf, secsize);
890 struct partinfo partinfo; /* disk parameters */
894 * NOTE: When faking up the CHS for a file image (e.g. for USB),
895 * we must use max values. If we do not then an overflowed
896 * cylinder count will, by convention, set the CHS fields to
897 * all 1's. The heads and sectors in the CHS fields will then
898 * exceed the basic geometry which can cause BIOSes to brick.
900 if (ioctl(fd, DIOCGPART, &partinfo) == -1) {
901 if (p_flag && fstat(fd, &st) == 0 && st.st_size) {
904 cylsecs = heads * sectors;
905 cyls = st.st_size / 512 / cylsecs;
907 warnx("can't get disk parameters on %s; supplying dummy ones",
910 cylsecs = heads * sectors;
913 cyls = partinfo.d_ncylinders;
914 heads = partinfo.d_nheads;
915 sectors = partinfo.d_secpertrack;
916 cylsecs = heads * sectors;
917 secsize = partinfo.media_blksize;
921 dos_sectors = sectors;
922 dos_cylsecs = cylsecs;
923 disksecs = (int64_t)cyls * heads * sectors;
931 mboot.bootinst_size = secsize;
932 if (mboot.bootinst != NULL)
933 free(mboot.bootinst);
934 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
935 warnx("unable to allocate buffer to read fdisk "
939 if (read_disk(0, mboot.bootinst) == -1) {
940 warnx("can't read fdisk partition table");
943 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
944 warnx("invalid fdisk partition table found");
945 /* So should we initialize things */
948 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
964 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
966 * write enable label sector before write (if necessary),
967 * disable after writing.
968 * needed if the disklabel protected area also protects
969 * sector 0. (e.g. empty disk)
973 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
974 warn("ioctl DIOCWLABEL");
976 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
977 if (write_disk(sector,
978 &mboot.bootinst[sector * secsize]) == -1) {
979 warn("can't write fdisk partition table");
983 ioctl(fd, DIOCWLABEL, &flag);
988 ioctl(fd, DIOCWLABEL, &flag);
997 printf("%s [n] ", str);
999 if (fgets(lbuf, LBUF, stdin) == NULL)
1001 lbuf[strlen(lbuf)-1] = 0;
1004 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
1005 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
1012 decimal(char *str, int *num, int deflt)
1018 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
1020 if (fgets(lbuf, LBUF, stdin) == NULL)
1022 lbuf[strlen(lbuf)-1] = 0;
1028 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1031 while ((c = *cp++)) {
1032 if (c <= '9' && c >= '0')
1033 acc = acc * 10 + c - '0';
1037 if (c == ' ' || c == '\t')
1038 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1043 printf("%s is an invalid decimal number. Try again.\n",
1051 hex(char *str, int *num, int deflt)
1057 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
1058 fgets(lbuf, LBUF, stdin);
1059 lbuf[strlen(lbuf)-1] = 0;
1065 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1068 while ((c = *cp++)) {
1069 if (c <= '9' && c >= '0')
1070 acc = (acc << 4) + c - '0';
1071 else if (c <= 'f' && c >= 'a')
1072 acc = (acc << 4) + c - 'a' + 10;
1073 else if (c <= 'F' && c >= 'A')
1074 acc = (acc << 4) + c - 'A' + 10;
1078 if (c == ' ' || c == '\t')
1079 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1084 printf("%s is an invalid hex number. Try again.\n",
1091 string(char *str, char **ans)
1097 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
1098 fgets(lbuf, LBUF, stdin);
1099 lbuf[strlen(lbuf)-1] = 0;
1104 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1108 while ((c = *cp) && c != '"') cp++;
1111 while ((c = *cp) && c != ' ' && c != '\t') cp++;
1124 int numentries = (sizeof(part_types)/sizeof(struct part_type));
1126 struct part_type *ptr = part_types;
1129 while(counter < numentries)
1131 if(ptr->type == type)
1143 parse_config_line(char *line, CMD *command)
1148 while (1) /* dirty trick used to insure one exit point for this
1151 memset(command, 0, sizeof(*command));
1153 while (isspace(*cp)) ++cp;
1154 if (*cp == '\0' || *cp == '#')
1158 command->cmd = *cp++;
1165 while (isspace(*cp)) ++cp;
1168 break; /* found comment */
1172 command->args[command->n_args].argtype = *cp++;
1176 break; /* assume end of line */
1179 command->args[command->n_args].arg_val = strtoll(cp, &end, 0);
1182 break; /* couldn't parse number */
1193 process_geometry(CMD *command)
1203 "ERROR line %d: the geometry specification line must occur before\n\
1204 all partition specifications",
1205 current_line_number);
1209 if (command->n_args != 3)
1211 warnx("ERROR line %d: incorrect number of geometry args",
1212 current_line_number);
1219 for (i = 0; i < 3; ++i)
1221 switch (command->args[i].argtype)
1224 dos_cyls = command->args[i].arg_val;
1227 dos_heads = command->args[i].arg_val;
1230 dos_sectors = command->args[i].arg_val;
1234 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1235 current_line_number, command->args[i].argtype,
1236 command->args[i].argtype);
1246 dos_cylsecs = dos_heads * dos_sectors;
1249 * Do sanity checks on parameter values
1253 warnx("ERROR line %d: number of cylinders not specified",
1254 current_line_number);
1257 if (dos_cyls == 0 || dos_cyls > 1024)
1260 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1261 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1262 is dedicated to DragonFly)",
1263 current_line_number, dos_cyls);
1268 warnx("ERROR line %d: number of heads not specified",
1269 current_line_number);
1272 else if (dos_heads < 1 || dos_heads > 256)
1274 warnx("ERROR line %d: number of heads must be within (1-256)",
1275 current_line_number);
1279 if (dos_sectors < 0)
1281 warnx("ERROR line %d: number of sectors not specified",
1282 current_line_number);
1285 else if (dos_sectors < 1 || dos_sectors > 63)
1287 warnx("ERROR line %d: number of sectors must be within (1-63)",
1288 current_line_number);
1299 process_partition(CMD *command)
1301 int status = 0, partition;
1302 u_int32_t prev_head_boundary, prev_cyl_boundary;
1303 u_int32_t adj_size, max_end;
1304 struct dos_partition *partp;
1309 if (command->n_args != 4)
1311 warnx("ERROR line %d: incorrect number of partition args",
1312 current_line_number);
1315 partition = command->args[0].arg_val;
1316 if (partition < 1 || partition > 4)
1318 warnx("ERROR line %d: invalid partition number %d",
1319 current_line_number, partition);
1322 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1323 bzero((char *)partp, sizeof (struct dos_partition));
1324 partp->dp_typ = command->args[1].arg_val;
1325 partp->dp_start = command->args[2].arg_val;
1326 partp->dp_size = command->args[3].arg_val;
1327 max_end = partp->dp_start + partp->dp_size;
1329 if (partp->dp_typ == 0)
1332 * Get out, the partition is marked as unused.
1335 * Insure that it's unused.
1337 bzero((char *)partp, sizeof (struct dos_partition));
1343 * Adjust start upwards, if necessary, to fall on an head boundary.
1345 if (partp->dp_start % dos_sectors != 0)
1347 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1348 if (max_end < dos_sectors ||
1349 prev_head_boundary > max_end - dos_sectors)
1352 * Can't go past end of partition
1355 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1357 current_line_number, partition);
1361 "WARNING: adjusting start offset of partition %d\n\
1362 from %u to %u, to fall on a head boundary",
1363 partition, (u_int)partp->dp_start,
1364 (u_int)(prev_head_boundary + dos_sectors));
1365 partp->dp_start = prev_head_boundary + dos_sectors;
1369 * Adjust size downwards, if necessary, to fall on a cylinder
1373 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1374 if (prev_cyl_boundary > partp->dp_start)
1375 adj_size = prev_cyl_boundary - partp->dp_start;
1379 "ERROR: could not adjust partition to start on a head boundary\n\
1380 and end on a cylinder boundary.");
1383 if (adj_size != partp->dp_size)
1386 "WARNING: adjusting size of partition %d from %u to %u\n\
1387 to end on a cylinder boundary",
1388 partition, (u_int)partp->dp_size, (u_int)adj_size);
1389 partp->dp_size = adj_size;
1391 if (partp->dp_size == 0)
1393 warnx("ERROR line %d: size of partition %d is zero",
1394 current_line_number, partition);
1407 process_active(CMD *command)
1409 int status = 0, partition, i;
1410 struct dos_partition *partp;
1414 active_processed = 1;
1415 if (command->n_args != 1)
1417 warnx("ERROR line %d: incorrect number of active args",
1418 current_line_number);
1422 partition = command->args[0].arg_val;
1423 if (partition < 1 || partition > 4)
1425 warnx("ERROR line %d: invalid partition number %d",
1426 current_line_number, partition);
1430 * Reset active partition
1432 partp = ((struct dos_partition *) &mboot.parts);
1433 for (i = 0; i < NDOSPART; i++)
1434 partp[i].dp_flag = 0;
1435 partp[partition-1].dp_flag = ACTIVE;
1445 process_line(char *line)
1452 parse_config_line(line, &command);
1453 switch (command.cmd)
1457 * Comment or blank line
1464 status = process_geometry(&command);
1467 status = process_partition(&command);
1470 status = process_active(&command);
1483 read_config(char *config_file)
1489 while (1) /* dirty trick used to insure one exit point for this
1492 if (strcmp(config_file, "-") != 0)
1495 * We're not reading from stdin
1497 if ((fp = fopen(config_file, "r")) == NULL)
1507 current_line_number = 0;
1510 if (fgets(buf, sizeof(buf), fp) == NULL)
1514 ++current_line_number;
1515 status = process_line(buf);
1526 * It doesn't matter if we're reading from stdin, as we've reached EOF
1538 struct dos_partition *partp;
1541 for (i = 0; i < 4; ++i)
1543 partp = ((struct dos_partition *) &mboot.parts) + i;
1544 bzero((char *)partp, sizeof (struct dos_partition));
1549 sanitize_partition(struct dos_partition *partp)
1551 u_int32_t prev_head_boundary, prev_cyl_boundary;
1552 u_int32_t max_end, size, start;
1554 start = partp->dp_start;
1555 size = partp->dp_size;
1556 max_end = start + size;
1557 /* Only allow a zero size if the partition is being marked unused. */
1559 if (start == 0 && partp->dp_typ == 0)
1561 warnx("ERROR: size of partition is zero");
1564 /* Return if no adjustment is necessary. */
1565 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1569 warnx("WARNING: partition overlaps with partition table");
1570 if (ok("Correct this automatically?"))
1571 start = dos_sectors;
1573 if (start % dos_sectors != 0)
1574 warnx("WARNING: partition does not start on a head boundary");
1575 if ((start +size) % dos_sectors != 0)
1576 warnx("WARNING: partition does not end on a cylinder boundary");
1577 warnx("WARNING: this may confuse the BIOS or some operating systems");
1578 if (!ok("Correct this automatically?"))
1582 * Adjust start upwards, if necessary, to fall on an head boundary.
1584 if (start % dos_sectors != 0) {
1585 prev_head_boundary = start / dos_sectors * dos_sectors;
1586 if (max_end < dos_sectors ||
1587 prev_head_boundary >= max_end - dos_sectors) {
1589 * Can't go past end of partition
1592 "ERROR: unable to adjust start of partition to fall on a head boundary");
1595 start = prev_head_boundary + dos_sectors;
1599 * Adjust size downwards, if necessary, to fall on a cylinder
1602 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1603 if (prev_cyl_boundary > start)
1604 size = prev_cyl_boundary - start;
1606 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1607 and end on a cylinder boundary.");
1611 /* Finally, commit any changes to partp and return. */
1612 if (start != partp->dp_start) {
1613 warnx("WARNING: adjusting start offset of partition to %u",
1615 partp->dp_start = start;
1617 if (size != partp->dp_size) {
1618 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1619 partp->dp_size = size;