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>
47 static char lbuf[LBUF];
53 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
55 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
56 * Copyright (c) 1989 Robert. V. Baron
60 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
61 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
62 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
64 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
66 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
67 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
68 int secsize = 0; /* the sensed sector size */
73 "/dev/ad0", "/dev/da0", "/dev/vkd0", 0
76 int cyls, sectors, heads, cylsecs;
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)
99 static int partition = -1;
104 static int current_line_number;
106 static int geom_processed = 0;
107 static int part_processed = 0;
108 static int active_processed = 0;
121 static int B_flag = 0; /* replace boot code */
122 static int C_flag = 0; /* use wrapped values for CHS */
123 static int I_flag = 0; /* use entire disk for DragonFly */
124 static int a_flag = 0; /* set active partition */
125 static char *b_flag = NULL; /* path to boot code */
126 static int i_flag = 0; /* replace partition data */
127 static int u_flag = 0; /* update partition data */
128 static int p_flag = 0; /* operate on a disk image file */
129 static int s_flag = 0; /* Print a summary and exit */
130 static int t_flag = 0; /* test only */
131 static char *f_flag = NULL; /* Read config info from file */
132 static int v_flag = 0; /* Be verbose */
141 ,{0x01, "Primary DOS with 12 bit FAT"}
142 ,{0x02, "XENIX / filesystem"}
143 ,{0x03, "XENIX /usr filesystem"}
144 ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
145 ,{0x05, "Extended DOS"}
146 ,{0x06, "Primary 'big' DOS (> 32MB)"}
147 ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
148 ,{0x08, "AIX filesystem"}
149 ,{0x09, "AIX boot partition or Coherent"}
150 ,{0x0A, "OS/2 Boot Manager or OPUS"}
151 ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
152 ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
153 ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
154 ,{0x0F, "Extended DOS, LBA"}
156 ,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
157 ,{0x12, "Compaq diagnostics"}
158 ,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
159 ,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
160 ,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
161 ,{0x18, "AST Windows swapfile"}
164 ,{0x3C, "PartitionMagic recovery"}
166 ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
167 ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
168 ,{0x43, "Linux native (sharing disk with DRDOS)"}
169 ,{0x4D, "QNX 4.2 Primary"}
170 ,{0x4E, "QNX 4.2 Secondary"}
171 ,{0x4F, "QNX 4.2 Tertiary"}
174 ,{0x52, "CP/M or Microport SysV/AT"}
177 ,{0x55, "EZ-Drive (disk manager)"}
179 ,{0x5C, "Priam Edisk (disk manager)"} /* according to S. Widlake */
181 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
182 ,{0x64, "Novell Netware 2.xx"}
183 ,{0x65, "Novell Netware 3.xx"}
184 ,{0x70, "DiskSecure Multi-Boot"}
187 ,{0x78, "QNX4.x 2nd part"}
188 ,{0x79, "QNX4.x 3rd part"}
189 ,{0x80, "Minix 1.1 ... 1.4a"}
190 ,{0x81, "Minix 1.4b ... 1.5.10"}
191 ,{0x82, "Linux swap or Solaris x86"}
192 ,{0x83, "Linux filesystem"}
193 ,{0x84, "OS/2 hidden C: drive"}
194 ,{0x85, "Linux extended"}
195 ,{0x86, "NTFS volume set??"}
196 ,{0x87, "NTFS volume set??"}
197 ,{0x93, "Amoeba filesystem"}
198 ,{0x94, "Amoeba bad block table"}
200 ,{0xA0, "Suspend to Disk"}
201 ,{0xA5, "DragonFly/FreeBSD/NetBSD/386BSD"}
206 ,{0xB7, "BSDI BSD/386 filesystem"}
207 ,{0xB8, "BSDI BSD/386 swap"}
208 ,{0xBE, "Solaris x86 boot"}
209 ,{0xC1, "DRDOS/sec with 12-bit FAT"}
210 ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
211 ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
213 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
217 ,{0xEB, "BeOS file system"}
219 ,{0xEF, "EFI System Partition"}
221 ,{0xF2, "DOS 3.3+ Secondary"}
223 ,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
224 ,{0xFF, "BBT (Bad Blocks Table)"}
227 static void print_s0(int which);
228 static void print_part(int i);
229 static void init_sector0(unsigned long start);
230 static void init_boot(void);
231 static void change_part(int i);
232 static void print_params();
233 static void change_active(int which);
234 static void change_code();
235 static void get_params_to_use();
236 static void dos(struct dos_partition *partp);
237 static int open_disk(int u_flag);
238 static ssize_t read_disk(off_t sector, void *buf);
239 static ssize_t write_disk(off_t sector, void *buf);
240 static int get_params();
241 static int read_s0();
242 static int write_s0();
243 static int ok(char *str);
244 static int decimal(char *str, int *num, int deflt);
245 static char *get_type(int type);
246 static int read_config(char *config_file);
247 static void reset_boot(void);
248 static int sanitize_partition(struct dos_partition *);
249 static void usage(void);
251 static int hex(char *str, int *num, int deflt);
252 static int string(char *str, char **ans);
257 main(int argc, char *argv[])
261 while ((c = getopt(argc, argv, "BCIab:f:p:istuv1234")) != -1)
309 if (f_flag || i_flag)
317 disk = getdevpath(argv[0], 0);
318 if (open_disk(u_flag) < 0)
319 err(1, "cannot open disk %s", disk);
320 } else if (disk == NULL) {
323 for(i = 0; disks[i]; i++)
326 rv = open_disk(u_flag);
330 err(1, "cannot open any disk");
332 if (open_disk(u_flag) < 0)
333 err(1, "cannot open disk %s", disk);
336 /* (abu)use mboot.bootinst to probe for the sector size */
337 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
338 err(1, "cannot allocate buffer to determine disk sector size");
339 read_disk(0, mboot.bootinst);
340 free(mboot.bootinst);
341 mboot.bootinst = NULL;
346 struct dos_partition *partp;
350 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
352 printf("Part %11s %11s Type Flags\n", "Start", "Size");
353 for (i = 0; i < NDOSPART; i++) {
354 partp = ((struct dos_partition *) &mboot.parts) + i;
355 if (partp->dp_start == 0 && partp->dp_size == 0)
357 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
358 (u_long) partp->dp_start,
359 (u_long) partp->dp_size, partp->dp_typ,
365 printf("******* Working on device %s *******\n",disk);
369 struct dos_partition *partp;
373 partp = (struct dos_partition *) (&mboot.parts[0]);
374 partp->dp_typ = DOSPTYP_386BSD;
375 partp->dp_flag = ACTIVE;
376 partp->dp_start = dos_sectors;
377 if (disksecs - dos_sectors > 0xFFFFFFFFU) {
378 printf("Warning: Ending logical block > 2TB, using max value\n");
379 partp->dp_size = 0xFFFFFFFFU;
381 partp->dp_size = (disksecs / dos_cylsecs) *
382 dos_cylsecs - dos_sectors;
393 if (read_s0() || i_flag)
398 if (!read_config(f_flag))
423 init_sector0(dos_sectors);
425 printf("Media sector size is %d\n", secsize);
426 printf("Warning: BIOS sector numbering starts with sector 1\n");
427 printf("Information from DOS bootblock is:\n");
429 for (i = 1; i <= NDOSPART; i++)
432 change_part(partition);
434 if (u_flag || a_flag)
435 change_active(partition);
440 if (u_flag || a_flag || B_flag) {
442 printf("\nWe haven't changed the partition table yet. ");
443 printf("This is your last chance.\n");
447 if (ok("Should we write new partition table?"))
452 printf("\n-t flag specified -- partition table not written.\n");
463 fprintf(stderr, "%s%s",
464 "usage: fdisk [-BCIaistu] [-b bootcode] [-p diskimage] [-1234] [disk]\n",
465 " fdisk -f configfile [-itv] [disk]\n");
475 printf("Information from DOS bootblock is:\n");
477 for (i = 1; i <= NDOSPART; i++)
478 printf("%d: ", i), print_part(i);
483 static struct dos_partition mtpart = { 0 };
488 struct dos_partition *partp;
491 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
493 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
494 printf("<UNUSED>\n");
498 * Be careful not to overflow.
500 part_mb = partp->dp_size;
502 part_mb /= (1024 * 1024);
503 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
504 printf(" start %lu, size %lu (%jd Meg), flag %x%s\n",
505 (u_long)partp->dp_start,
506 (u_long)partp->dp_size,
509 partp->dp_flag == ACTIVE ? " (active)" : "");
510 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
511 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
513 ,DPSECT(partp->dp_ssect)
514 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
516 ,DPSECT(partp->dp_esect));
527 fname = b_flag ? b_flag : "/boot/mbr";
528 if ((fd = open(fname, O_RDONLY)) == -1 ||
529 fstat(fd, &sb) == -1)
531 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
532 errx(1, "%s: length must be a multiple of sector size", fname);
533 if (mboot.bootinst != NULL)
534 free(mboot.bootinst);
535 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
536 errx(1, "%s: unable to allocate read buffer", fname);
537 if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
540 if (n != mboot.bootinst_size)
541 errx(1, "%s: short read", fname);
546 init_sector0(unsigned long start)
548 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
552 partp->dp_typ = DOSPTYP_386BSD;
553 partp->dp_flag = ACTIVE;
554 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
557 partp->dp_start = start;
558 if (disksecs - start > 0xFFFFFFFFU) {
559 printf("Warning: Ending logical block > 2TB, using max value\n");
560 partp->dp_size = 0xFFFFFFFFU;
562 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
571 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
573 printf("The data for partition %d is:\n", i);
576 if (u_flag && ok("Do you want to change it?")) {
580 bzero((char *)partp, sizeof (struct dos_partition));
583 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
589 Decimal("sysid (165=DragonFly)", partp->dp_typ, tmp);
590 Decimal("start", partp->dp_start, tmp);
591 Decimal("size", partp->dp_size, tmp);
592 if (!sanitize_partition(partp)) {
593 warnx("ERROR: failed to adjust; setting sysid to 0");
597 if (ok("Explicitly specify beg/end address ?"))
600 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
602 tsec = DPSECT(partp->dp_ssect);
603 Decimal("beginning cylinder", tcyl, tmp);
604 Decimal("beginning head", thd, tmp);
605 Decimal("beginning sector", tsec, tmp);
606 if (tcyl > MAXCYL && C_flag == 0) {
607 printf("Warning: starting cylinder wraps, using all 1's\n");
609 partp->dp_ssect = -1;
612 partp->dp_scyl = DOSCYL(tcyl);
613 partp->dp_ssect = DOSSECT(tsec,tcyl);
617 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
619 tsec = DPSECT(partp->dp_esect);
620 Decimal("ending cylinder", tcyl, tmp);
621 Decimal("ending head", thd, tmp);
622 Decimal("ending sector", tsec, tmp);
623 if (tcyl > MAXCYL && C_flag == 0) {
624 printf("Warning: ending cylinder wraps, using all 1's\n");
626 partp->dp_esect = -1;
629 partp->dp_ecyl = DOSCYL(tcyl);
630 partp->dp_esect = DOSSECT(tsec,tcyl);
637 } while (!ok("Are we happy with this entry?"));
644 printf("parameters extracted from device are:\n");
645 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
646 ,cyls,heads,sectors,cylsecs);
647 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
648 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
649 printf("parameters to be used for BIOS calculations are:\n");
650 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
651 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
655 change_active(int which)
657 struct dos_partition *partp = &mboot.parts[0];
658 int active, i, new, tmp;
661 for (i = 0; i < NDOSPART; i++) {
662 if ((partp[i].dp_flag & ACTIVE) == 0)
664 printf("Partition %d is marked active\n", i + 1);
668 if (a_flag && which != -1)
670 else if (active == -1)
673 if (!ok("Do you want to change the active partition?"))
678 Decimal("active partition", new, tmp);
679 if (new < 1 || new > 4) {
680 printf("Active partition number must be in range 1-4."
685 } while (!ok("Are you happy with this choice"));
686 for (i = 0; i < NDOSPART; i++)
687 partp[i].dp_flag = 0;
688 if (active > 0 && active <= NDOSPART)
689 partp[active-1].dp_flag = ACTIVE;
695 if (ok("Do you want to change the boot code?"))
700 get_params_to_use(void)
704 if (ok("Do you want to change our idea of what BIOS thinks ?"))
708 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
709 Decimal("BIOS's idea of #heads", dos_heads, tmp);
710 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
711 dos_cylsecs = dos_heads * dos_sectors;
714 while(!ok("Are you happy with this choice"));
719 /***********************************************\
720 * Change real numbers into strange dos numbers *
721 \***********************************************/
723 dos(struct dos_partition *partp)
728 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
729 memcpy(partp, &mtpart, sizeof(*partp));
734 cy = partp->dp_start / dos_cylsecs;
735 sec = partp->dp_start % dos_sectors + 1;
736 if (cy > MAXCYL && C_flag == 0) {
737 printf("Warning: starting cylinder wraps, using all 1's\n");
740 partp->dp_ssect = -1;
742 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
743 partp->dp_scyl = DOSCYL(cy);
744 partp->dp_ssect = DOSSECT(sec, cy);
748 end = partp->dp_start + partp->dp_size - 1;
749 cy = end / dos_cylsecs;
750 sec = end % dos_sectors + 1;
751 if (cy > MAXCYL && C_flag == 0) {
752 printf("Warning: ending cylinder wraps, using all 1's\n");
755 partp->dp_esect = -1;
757 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
758 partp->dp_ecyl = DOSCYL(cy);
759 partp->dp_esect = DOSSECT(sec, cy);
765 /* Getting device status */
768 open_disk(int u_flag)
772 if (stat(disk, &st) == -1) {
775 warnx("can't get file status of %s", disk);
778 if ( !(st.st_mode & S_IFCHR) && p_flag == 0 )
779 warnx("device %s is not character special", disk);
781 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
784 warnx("can't open device %s", disk);
787 if (get_params() == -1) {
788 warnx("can't get disk parameters on %s", disk);
795 read_disk(off_t sector, void *buf)
797 lseek(fd,(sector * 512), 0);
799 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
802 int size = read(fd, buf, secsize);
803 if( size == secsize )
804 /* it worked so return */
808 return read( fd, buf, secsize );
810 /* we failed to read at any of the sizes */
815 write_disk(off_t sector, void *buf)
817 lseek(fd,(sector * 512), 0);
818 /* write out in the size that the read_disk found worked */
819 return write(fd, buf, secsize);
825 struct partinfo partinfo; /* disk parameters */
828 if (ioctl(fd, DIOCGPART, &partinfo) == -1) {
829 if (p_flag && fstat(fd, &st) == 0 && st.st_size) {
832 cylsecs = heads * sectors;
833 cyls = st.st_size / 512 / cylsecs;
835 warnx("can't get disk parameters on %s; supplying dummy ones",
838 cylsecs = heads * sectors;
841 cyls = partinfo.d_ncylinders;
842 heads = partinfo.d_nheads;
843 sectors = partinfo.d_secpertrack;
844 cylsecs = heads * sectors;
845 secsize = partinfo.media_blksize;
849 dos_sectors = sectors;
850 dos_cylsecs = cylsecs;
851 disksecs = (int64_t)cyls * heads * sectors;
859 mboot.bootinst_size = secsize;
860 if (mboot.bootinst != NULL)
861 free(mboot.bootinst);
862 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
863 warnx("unable to allocate buffer to read fdisk "
867 if (read_disk(0, mboot.bootinst) == -1) {
868 warnx("can't read fdisk partition table");
871 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
872 warnx("invalid fdisk partition table found");
873 /* So should we initialize things */
876 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
892 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
894 * write enable label sector before write (if necessary),
895 * disable after writing.
896 * needed if the disklabel protected area also protects
897 * sector 0. (e.g. empty disk)
901 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
902 warn("ioctl DIOCWLABEL");
904 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
905 if (write_disk(sector,
906 &mboot.bootinst[sector * secsize]) == -1) {
907 warn("can't write fdisk partition table");
911 ioctl(fd, DIOCWLABEL, &flag);
916 ioctl(fd, DIOCWLABEL, &flag);
925 printf("%s [n] ", str);
927 if (fgets(lbuf, LBUF, stdin) == NULL)
929 lbuf[strlen(lbuf)-1] = 0;
932 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
933 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
940 decimal(char *str, int *num, int deflt)
946 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
948 if (fgets(lbuf, LBUF, stdin) == NULL)
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 * 10 + c - '0';
965 if (c == ' ' || c == '\t')
966 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
971 printf("%s is an invalid decimal number. Try again.\n",
979 hex(char *str, int *num, int deflt)
985 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
986 fgets(lbuf, LBUF, stdin);
987 lbuf[strlen(lbuf)-1] = 0;
993 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
996 while ((c = *cp++)) {
997 if (c <= '9' && c >= '0')
998 acc = (acc << 4) + c - '0';
999 else if (c <= 'f' && c >= 'a')
1000 acc = (acc << 4) + c - 'a' + 10;
1001 else if (c <= 'F' && c >= 'A')
1002 acc = (acc << 4) + c - 'A' + 10;
1006 if (c == ' ' || c == '\t')
1007 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1012 printf("%s is an invalid hex number. Try again.\n",
1019 string(char *str, char **ans)
1025 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
1026 fgets(lbuf, LBUF, stdin);
1027 lbuf[strlen(lbuf)-1] = 0;
1032 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1036 while ((c = *cp) && c != '"') cp++;
1039 while ((c = *cp) && c != ' ' && c != '\t') cp++;
1052 int numentries = (sizeof(part_types)/sizeof(struct part_type));
1054 struct part_type *ptr = part_types;
1057 while(counter < numentries)
1059 if(ptr->type == type)
1071 parse_config_line(char *line, CMD *command)
1076 while (1) /* dirty trick used to insure one exit point for this
1079 memset(command, 0, sizeof(*command));
1081 while (isspace(*cp)) ++cp;
1082 if (*cp == '\0' || *cp == '#')
1086 command->cmd = *cp++;
1093 while (isspace(*cp)) ++cp;
1096 break; /* found comment */
1100 command->args[command->n_args].argtype = *cp++;
1104 break; /* assume end of line */
1107 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1110 break; /* couldn't parse number */
1121 process_geometry(CMD *command)
1131 "ERROR line %d: the geometry specification line must occur before\n\
1132 all partition specifications",
1133 current_line_number);
1137 if (command->n_args != 3)
1139 warnx("ERROR line %d: incorrect number of geometry args",
1140 current_line_number);
1147 for (i = 0; i < 3; ++i)
1149 switch (command->args[i].argtype)
1152 dos_cyls = command->args[i].arg_val;
1155 dos_heads = command->args[i].arg_val;
1158 dos_sectors = command->args[i].arg_val;
1162 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1163 current_line_number, command->args[i].argtype,
1164 command->args[i].argtype);
1174 dos_cylsecs = dos_heads * dos_sectors;
1177 * Do sanity checks on parameter values
1181 warnx("ERROR line %d: number of cylinders not specified",
1182 current_line_number);
1185 if (dos_cyls == 0 || dos_cyls > 1024)
1188 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1189 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1190 is dedicated to DragonFly)",
1191 current_line_number, dos_cyls);
1196 warnx("ERROR line %d: number of heads not specified",
1197 current_line_number);
1200 else if (dos_heads < 1 || dos_heads > 256)
1202 warnx("ERROR line %d: number of heads must be within (1-256)",
1203 current_line_number);
1207 if (dos_sectors < 0)
1209 warnx("ERROR line %d: number of sectors not specified",
1210 current_line_number);
1213 else if (dos_sectors < 1 || dos_sectors > 63)
1215 warnx("ERROR line %d: number of sectors must be within (1-63)",
1216 current_line_number);
1227 process_partition(CMD *command)
1229 int status = 0, partition;
1230 u_int32_t prev_head_boundary, prev_cyl_boundary;
1231 u_int32_t adj_size, max_end;
1232 struct dos_partition *partp;
1237 if (command->n_args != 4)
1239 warnx("ERROR line %d: incorrect number of partition args",
1240 current_line_number);
1243 partition = command->args[0].arg_val;
1244 if (partition < 1 || partition > 4)
1246 warnx("ERROR line %d: invalid partition number %d",
1247 current_line_number, partition);
1250 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1251 bzero((char *)partp, sizeof (struct dos_partition));
1252 partp->dp_typ = command->args[1].arg_val;
1253 partp->dp_start = command->args[2].arg_val;
1254 partp->dp_size = command->args[3].arg_val;
1255 max_end = partp->dp_start + partp->dp_size;
1257 if (partp->dp_typ == 0)
1260 * Get out, the partition is marked as unused.
1263 * Insure that it's unused.
1265 bzero((char *)partp, sizeof (struct dos_partition));
1271 * Adjust start upwards, if necessary, to fall on an head boundary.
1273 if (partp->dp_start % dos_sectors != 0)
1275 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1276 if (max_end < dos_sectors ||
1277 prev_head_boundary > max_end - dos_sectors)
1280 * Can't go past end of partition
1283 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1285 current_line_number, partition);
1289 "WARNING: adjusting start offset of partition %d\n\
1290 from %u to %u, to fall on a head boundary",
1291 partition, (u_int)partp->dp_start,
1292 (u_int)(prev_head_boundary + dos_sectors));
1293 partp->dp_start = prev_head_boundary + dos_sectors;
1297 * Adjust size downwards, if necessary, to fall on a cylinder
1301 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1302 if (prev_cyl_boundary > partp->dp_start)
1303 adj_size = prev_cyl_boundary - partp->dp_start;
1307 "ERROR: could not adjust partition to start on a head boundary\n\
1308 and end on a cylinder boundary.");
1311 if (adj_size != partp->dp_size)
1314 "WARNING: adjusting size of partition %d from %u to %u\n\
1315 to end on a cylinder boundary",
1316 partition, (u_int)partp->dp_size, (u_int)adj_size);
1317 partp->dp_size = adj_size;
1319 if (partp->dp_size == 0)
1321 warnx("ERROR line %d: size of partition %d is zero",
1322 current_line_number, partition);
1335 process_active(CMD *command)
1337 int status = 0, partition, i;
1338 struct dos_partition *partp;
1342 active_processed = 1;
1343 if (command->n_args != 1)
1345 warnx("ERROR line %d: incorrect number of active args",
1346 current_line_number);
1350 partition = command->args[0].arg_val;
1351 if (partition < 1 || partition > 4)
1353 warnx("ERROR line %d: invalid partition number %d",
1354 current_line_number, partition);
1358 * Reset active partition
1360 partp = ((struct dos_partition *) &mboot.parts);
1361 for (i = 0; i < NDOSPART; i++)
1362 partp[i].dp_flag = 0;
1363 partp[partition-1].dp_flag = ACTIVE;
1373 process_line(char *line)
1380 parse_config_line(line, &command);
1381 switch (command.cmd)
1385 * Comment or blank line
1392 status = process_geometry(&command);
1395 status = process_partition(&command);
1398 status = process_active(&command);
1411 read_config(char *config_file)
1417 while (1) /* dirty trick used to insure one exit point for this
1420 if (strcmp(config_file, "-") != 0)
1423 * We're not reading from stdin
1425 if ((fp = fopen(config_file, "r")) == NULL)
1435 current_line_number = 0;
1438 if (fgets(buf, sizeof(buf), fp) == NULL)
1442 ++current_line_number;
1443 status = process_line(buf);
1454 * It doesn't matter if we're reading from stdin, as we've reached EOF
1466 struct dos_partition *partp;
1469 for (i = 0; i < 4; ++i)
1471 partp = ((struct dos_partition *) &mboot.parts) + i;
1472 bzero((char *)partp, sizeof (struct dos_partition));
1477 sanitize_partition(struct dos_partition *partp)
1479 u_int32_t prev_head_boundary, prev_cyl_boundary;
1480 u_int32_t max_end, size, start;
1482 start = partp->dp_start;
1483 size = partp->dp_size;
1484 max_end = start + size;
1485 /* Only allow a zero size if the partition is being marked unused. */
1487 if (start == 0 && partp->dp_typ == 0)
1489 warnx("ERROR: size of partition is zero");
1492 /* Return if no adjustment is necessary. */
1493 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1497 warnx("WARNING: partition overlaps with partition table");
1498 if (ok("Correct this automatically?"))
1499 start = dos_sectors;
1501 if (start % dos_sectors != 0)
1502 warnx("WARNING: partition does not start on a head boundary");
1503 if ((start +size) % dos_sectors != 0)
1504 warnx("WARNING: partition does not end on a cylinder boundary");
1505 warnx("WARNING: this may confuse the BIOS or some operating systems");
1506 if (!ok("Correct this automatically?"))
1510 * Adjust start upwards, if necessary, to fall on an head boundary.
1512 if (start % dos_sectors != 0) {
1513 prev_head_boundary = start / dos_sectors * dos_sectors;
1514 if (max_end < dos_sectors ||
1515 prev_head_boundary >= max_end - dos_sectors) {
1517 * Can't go past end of partition
1520 "ERROR: unable to adjust start of partition to fall on a head boundary");
1523 start = prev_head_boundary + dos_sectors;
1527 * Adjust size downwards, if necessary, to fall on a cylinder
1530 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1531 if (prev_cyl_boundary > start)
1532 size = prev_cyl_boundary - start;
1534 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1535 and end on a cylinder boundary.");
1539 /* Finally, commit any changes to partp and return. */
1540 if (start != partp->dp_start) {
1541 warnx("WARNING: adjusting start offset of partition to %u",
1543 partp->dp_start = start;
1545 if (size != partp->dp_size) {
1546 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1547 partp->dp_size = size;