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: src/sbin/pc98/fdisk/fdisk.c,v 1.1.2.3 2000/12/11 01:03:25 obrien Exp $
27 * $DragonFly: src/sbin/pc98/fdisk/Attic/fdisk.c,v 1.2 2003/06/17 04:27:34 dillon Exp $
30 #include <sys/disklabel.h>
45 static char lbuf[LBUF];
49 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
51 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
52 * Copyright (c) 1989 Robert. V. Baron
56 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
57 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
58 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
60 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
62 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
63 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
64 int secsize = 0; /* the sensed sector size */
69 "/dev/ad0", "/dev/wd0", "/dev/da0", "/dev/od0", 0
72 struct disklabel disklabel; /* disk parameters */
74 int cyls, sectors, heads, cylsecs, disksecs;
78 unsigned char padding[2]; /* force the longs to be long aligned */
80 unsigned char bootinst[510];
82 unsigned char bootinst[DOSPARTOFF];
83 struct dos_partition parts[4];
85 unsigned short int signature;
88 struct dos_partition parts[8];
90 /* room to read in MBRs that are bigger then DEV_BSIZE */
91 unsigned char large_sector_overflow[MAX_SEC_SIZE-MIN_SEC_SIZE];
96 #define BOOT_MAGIC 0xAA55
104 #define DOSSECT(s,c) (s)
105 #define DOSCYL(c) (c)
107 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
108 #define DOSCYL(c) (c & 0xff)
110 static int partition = -1;
115 static int current_line_number;
117 static int geom_processed = 0;
118 static int part_processed = 0;
119 static int active_processed = 0;
132 static int B_flag = 0; /* replace boot code */
133 static int I_flag = 0; /* use entire disk for FreeBSD */
134 static int a_flag = 0; /* set active partition */
135 static char *b_flag = NULL; /* path to boot code */
136 static int i_flag = 0; /* replace partition data */
137 static int u_flag = 0; /* update partition data */
138 static int s_flag = 0; /* Print a summary and exit */
139 static int t_flag = 0; /* test only, if f_flag is given */
140 static char *f_flag = NULL; /* Read config info from file */
141 static int v_flag = 0; /* Be verbose */
150 ,{0x01, "Primary DOS with 12 bit FAT"}
157 ,{0x02, "XENIX / filesystem"}
158 ,{0x03, "XENIX /usr filesystem"}
160 ,{0x05, "Extended DOS"}
161 ,{0x06, "Primary 'big' DOS (> 32MB)"}
162 ,{0x07, "OS/2 HPFS, QNX or Advanced UNIX"}
163 ,{0x08, "AIX filesystem"}
164 ,{0x09, "AIX boot partition or Coherent"}
165 ,{0x0A, "OS/2 Boot Manager or OPUS"}
167 ,{0x14, "FreeBSD/NetBSD/386BSD"}
168 ,{0x94, "FreeBSD/NetBSD/386BSD"}
172 ,{0x52, "CP/M or Microport SysV/AT"}
175 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
176 ,{0x64, "Novell Netware 2.xx"}
177 ,{0x65, "Novell Netware 3.xx"}
181 ,{0x02, "XENIX / filesystem"}
182 ,{0x03, "XENIX /usr filesystem"}
183 ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
184 ,{0x05, "Extended DOS"}
185 ,{0x06, "Primary 'big' DOS (> 32MB)"}
186 ,{0x07, "OS/2 HPFS, NTFS, QNX or Advanced UNIX"}
187 ,{0x08, "AIX filesystem"}
188 ,{0x09, "AIX boot partition or Coherent"}
189 ,{0x0A, "OS/2 Boot Manager or OPUS"}
190 ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
191 ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
192 ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
193 ,{0x0F, "Extended DOS, LBA"}
198 ,{0x52, "CP/M or Microport SysV/AT"}
201 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
202 ,{0x64, "Novell Netware 2.xx"}
203 ,{0x65, "Novell Netware 3.xx"}
205 ,{0x80, "Minix 1.1 ... 1.4a"}
206 ,{0x81, "Minix 1.4b ... 1.5.10"}
207 ,{0x82, "Linux swap or Solaris x86"}
208 ,{0x83, "Linux filesystem"}
209 ,{0x93, "Amoeba filesystem"}
210 ,{0x94, "Amoeba bad block table"}
212 ,{0xA5, "FreeBSD/NetBSD/386BSD"}
216 ,{0xB7, "BSDI BSD/386 filesystem"}
217 ,{0xB8, "BSDI BSD/386 swap"}
218 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
223 ,{0xF2, "DOS 3.3+ Secondary"}
225 ,{0xFF, "BBT (Bad Blocks Table)"}
229 static void print_s0(int which);
230 static void print_part(int i);
231 static void init_sector0(unsigned long start);
232 static void init_boot(void);
233 static void change_part(int i);
234 static void print_params();
235 static void change_active(int which);
236 static void change_code();
237 static void get_params_to_use();
239 static void dos(int sec, int size, unsigned short *c, unsigned char *s,
242 static void dos(int sec, int size, unsigned char *c, unsigned char *s,
245 static int open_disk(int u_flag);
246 static ssize_t read_disk(off_t sector, void *buf);
247 static ssize_t write_disk(off_t sector, void *buf);
248 static int get_params();
249 static int read_s0();
250 static int write_s0();
251 static int ok(char *str);
252 static int decimal(char *str, int *num, int deflt);
253 static char *get_type(int type);
254 static int read_config(char *config_file);
255 static void reset_boot(void);
256 static void usage(void);
258 static int hex(char *str, int *num, int deflt);
261 static int string(char *str, char **ans);
267 main(int argc, char *argv[])
272 while ((c = getopt(argc, argv, "Bab:f:istuv12345678")) != -1)
274 while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
322 if (f_flag || i_flag)
331 static char realname[12];
333 if(strncmp(argv[0], _PATH_DEV, sizeof _PATH_DEV - 2) == 0)
337 snprintf(realname, 12, "%s%s", _PATH_DEV, argv[0]);
341 if (open_disk(u_flag) < 0)
342 err(1, "cannot open disk %s", disk);
348 for(i = 0; disks[i]; i++)
351 rv = open_disk(u_flag);
355 err(1, "cannot open any disk");
360 struct dos_partition *partp;
364 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
367 printf("Part %11s %11s SID\n", "Start", "Size");
369 printf("Part %11s %11s Type Flags\n", "Start", "Size");
371 for (i = 0; i < NDOSPART; i++) {
372 partp = ((struct dos_partition *) &mboot.parts) + i;
374 if (partp->dp_sid == 0)
376 if (partp->dp_start == 0 && partp->dp_size == 0)
379 printf("%4d: %11lu %11lu 0x%02xn", i + 1,
381 partp->dp_scyl * cylsecs,
382 (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs,
385 (u_long) partp->dp_start,
386 (u_long) partp->dp_size, partp->dp_typ,
393 printf("******* Working on device %s *******\n",disk);
398 struct dos_partition *partp;
402 partp = (struct dos_partition *) (&mboot.parts[0]);
403 partp->dp_typ = DOSPTYP_386BSD;
404 partp->dp_flag = ACTIVE;
405 partp->dp_start = dos_sectors;
406 partp->dp_size = disksecs - dos_sectors;
408 dos(partp->dp_start, partp->dp_size,
409 &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
410 dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
411 &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
421 if (read_s0() || i_flag)
426 if (!read_config(f_flag))
454 printf("Media sector size is %d\n", secsize);
455 printf("Warning: BIOS sector numbering starts with sector 1\n");
456 printf("Information from DOS bootblock is:\n");
458 for (i = 1; i <= NDOSPART; i++)
461 change_part(partition);
463 if (u_flag || a_flag)
464 change_active(partition);
469 if (u_flag || a_flag || B_flag) {
472 printf("\nWe haven't changed the partition table yet. ");
473 printf("This is your last chance.\n");
478 if (ok("Should we write new partition table?"))
483 printf("\n-t flag specified -- partition table not written.\n");
494 fprintf(stderr, "%s%s",
495 "usage: fdisk [-Batu] [-b bootcode] [-12345678] [disk]\n",
496 " fdisk -f configfile [-itv] [disk]\n");
506 printf("Information from DOS bootblock is:\n");
508 for (i = 1; i <= NDOSPART; i++)
509 printf("%d: ", i), print_part(i);
514 static struct dos_partition mtpart = { 0 };
519 struct dos_partition *partp;
522 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
524 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
525 printf("<UNUSED>\n");
529 * Be careful not to overflow.
532 printf("sysmid %d,(%s)\n", partp->dp_mid, get_type(partp->dp_mid));
533 printf(" start %d, size %d (%d Meg), sid %d\n",
534 partp->dp_scyl * cylsecs ,
535 (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs,
536 (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs * 512 / (1024 * 1024),
539 part_mb = partp->dp_size;
541 part_mb /= (1024 * 1024);
542 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
543 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
544 (u_long)partp->dp_start,
545 (u_long)partp->dp_size,
548 partp->dp_flag == ACTIVE ? " (active)" : "");
550 printf("\tbeg: cyl %d/ sector %d/ head %d;\n\tend: cyl %d/ sector %d/ head %d\n"
551 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
552 ,DPSECT(partp->dp_ssect)
554 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
555 ,DPSECT(partp->dp_esect)
558 printf ("\tsystem Name %.16s\n",partp->dp_name);
570 fname = b_flag ? b_flag : "/boot/mbr";
571 if ((fd = open(fname, O_RDONLY)) == -1 ||
572 read(fd, mboot.bootinst, DOSPARTOFF) == -1 ||
576 mboot.signature = BOOT_MAGIC;
581 init_sector0(unsigned long start)
583 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
584 unsigned long size = disksecs - start;
589 partp->dp_mid = DOSMID_386BSD;
590 partp->dp_sid = DOSSID_386BSD;
592 dos(start, size, &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
593 partp->dp_ipl_cyl = partp->dp_scyl;
594 partp->dp_ipl_sct = partp->dp_ssect;
595 partp->dp_ipl_head = partp->dp_shd;
596 dos(start+size-cylsecs, size, &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
598 partp->dp_typ = DOSPTYP_386BSD;
599 partp->dp_flag = ACTIVE;
600 partp->dp_start = start;
601 partp->dp_size = size;
603 dos(partp->dp_start, partp->dp_size,
604 &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
605 dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
606 &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
613 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
615 printf("The data for partition %d is:\n", i);
618 if (u_flag && ok("Do you want to change it?")) {
622 bzero((char *)partp, sizeof (struct dos_partition));
625 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
632 int x_start = partp->dp_scyl * cylsecs ;
633 int x_size = (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs;
634 Decimal("sysmid", partp->dp_mid, tmp);
635 Decimal("syssid", partp->dp_sid, tmp);
636 String ("system name", partp->dp_name, 16);
637 Decimal("start", x_start, tmp);
638 Decimal("size", x_size, tmp);
640 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
641 Decimal("start", partp->dp_start, tmp);
642 Decimal("size", partp->dp_size, tmp);
644 if (ok("Explicitly specify beg/end address ?"))
647 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
649 tsec = DPSECT(partp->dp_ssect);
650 Decimal("beginning cylinder", tcyl, tmp);
651 Decimal("beginning head", thd, tmp);
652 Decimal("beginning sector", tsec, tmp);
653 partp->dp_scyl = DOSCYL(tcyl);
654 partp->dp_ssect = DOSSECT(tsec,tcyl);
657 partp->dp_ipl_cyl = partp->dp_scyl;
658 partp->dp_ipl_sct = partp->dp_ssect;
659 partp->dp_ipl_head = partp->dp_shd;
662 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
664 tsec = DPSECT(partp->dp_esect);
665 Decimal("ending cylinder", tcyl, tmp);
666 Decimal("ending head", thd, tmp);
667 Decimal("ending sector", tsec, tmp);
668 partp->dp_ecyl = DOSCYL(tcyl);
669 partp->dp_esect = DOSSECT(tsec,tcyl);
673 dos(x_start, x_size, &partp->dp_scyl,
674 &partp->dp_ssect, &partp->dp_shd);
675 partp->dp_ipl_cyl = partp->dp_scyl;
676 partp->dp_ipl_sct = partp->dp_ssect;
677 partp->dp_ipl_head = partp->dp_shd;
678 dos(x_start+x_size - cylsecs, x_size, &partp->dp_ecyl,
679 &partp->dp_esect, &partp->dp_ehd);
681 dos(partp->dp_start, partp->dp_size,
682 &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
683 dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
684 &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
689 } while (!ok("Are we happy with this entry?"));
696 printf("parameters extracted from in-core disklabel are:\n");
697 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
698 ,cyls,heads,sectors,cylsecs);
700 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
701 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
703 printf("parameters to be used for BIOS calculations are:\n");
704 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
705 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
709 change_active(int which)
718 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts);
720 if (a_flag && which != -1)
722 if (!ok("Do you want to change the active partition?"))
731 Decimal("active partition", active, tmp);
733 if (active < 1 || 8 < active) {
734 printf("Active partition number must be in range 1-8."
739 if (active < 1 || 4 < active) {
740 printf("Active partition number must be in range 1-4."
745 } while (!ok("Are you happy with this choice"));
747 partp[active].dp_sid |= ACTIVE;
749 for (i = 0; i < NDOSPART; i++)
750 partp[i].dp_flag = 0;
751 if (active > 0 && active <= NDOSPART)
752 partp[active-1].dp_flag = ACTIVE;
759 if (ok("Do you want to change the boot code?"))
769 if (ok("Do you want to change our idea of what BIOS thinks ?"))
773 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
774 Decimal("BIOS's idea of #heads", dos_heads, tmp);
775 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
776 dos_cylsecs = dos_heads * dos_sectors;
779 while(!ok("Are you happy with this choice"));
784 /***********************************************\
785 * Change real numbers into strange dos numbers *
786 \***********************************************/
788 dos(sec, size, c, s, h)
792 unsigned char *s, *h;
794 unsigned char *c, *s, *h;
800 if (sec == 0 && size == 0) {
805 cy = sec / ( dos_cylsecs );
806 sec = sec - cy * ( dos_cylsecs );
808 hd = sec / dos_sectors;
810 sec = (sec - hd * dos_sectors);
816 sec = (sec - hd * dos_sectors) + 1;
820 *s = (sec & 0x3f) | ( (cy & 0x300) >> 2);
826 /* Getting device status */
829 open_disk(int u_flag)
833 if (stat(disk, &st) == -1) {
834 warnx("can't get file status of %s", disk);
837 if ( !(st.st_mode & S_IFCHR) )
838 warnx("device %s is not character special", disk);
840 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
843 warnx("can't open device %s", disk);
846 if (get_params(0) == -1) {
847 warnx("can't get disk parameters on %s", disk);
854 read_disk(off_t sector, void *buf)
856 lseek(fd,(sector * 512), 0);
858 return read(fd, buf, secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2);
861 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
864 int size = read(fd, buf, secsize);
865 if( size == secsize )
866 /* it worked so return */
870 return read( fd, buf, secsize );
872 /* we failed to read at any of the sizes */
878 write_disk(off_t sector, void *buf)
880 lseek(fd,(sector * 512), 0);
881 /* write out in the size that the read_disk found worked */
883 return write(fd, buf, secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2);
885 return write(fd, buf, secsize);
893 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
894 warnx("can't get disk parameters on %s; supplying dummy ones", disk);
896 dos_heads = heads = 1;
897 dos_sectors = sectors = 1;
898 dos_cylsecs = cylsecs = heads * sectors;
899 disksecs = cyls * heads * sectors;
901 secsize = disklabel.d_secsize;
906 dos_cyls = cyls = disklabel.d_ncylinders;
907 dos_heads = heads = disklabel.d_ntracks;
908 dos_sectors = sectors = disklabel.d_nsectors;
909 dos_cylsecs = cylsecs = heads * sectors;
910 disksecs = cyls * heads * sectors;
912 secsize = disklabel.d_secsize;
921 if (read_disk(0, (char *) mboot.bootinst) == -1) {
922 warnx("can't read fdisk partition table");
925 if (mboot.signature != BOOT_MAGIC) {
926 warnx("invalid fdisk partition table found");
927 /* So should we initialize things */
944 * write enable label sector before write (if necessary),
945 * disable after writing.
946 * needed if the disklabel protected area also protects
947 * sector 0. (e.g. empty disk)
951 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
952 warn("ioctl DIOCWLABEL");
954 if (write_disk(0, (char *) mboot.bootinst) == -1) {
955 warn("can't write fdisk partition table");
959 (void) ioctl(fd, DIOCWLABEL, &flag);
970 printf("%s [n] ", str);
971 fgets(lbuf, LBUF, stdin);
972 lbuf[strlen(lbuf)-1] = 0;
975 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
976 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
983 decimal(char *str, int *num, int deflt)
989 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
990 fgets(lbuf, LBUF, stdin);
991 lbuf[strlen(lbuf)-1] = 0;
997 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1000 while ((c = *cp++)) {
1001 if (c <= '9' && c >= '0')
1002 acc = acc * 10 + c - '0';
1006 if (c == ' ' || c == '\t')
1007 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1012 printf("%s is an invalid decimal number. Try again.\n",
1020 hex(char *str, int *num, int deflt)
1026 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
1027 fgets(lbuf, LBUF, stdin);
1028 lbuf[strlen(lbuf)-1] = 0;
1034 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1037 while ((c = *cp++)) {
1038 if (c <= '9' && c >= '0')
1039 acc = (acc << 4) + c - '0';
1040 else if (c <= 'f' && c >= 'a')
1041 acc = (acc << 4) + c - 'a' + 10;
1042 else if (c <= 'F' && c >= 'A')
1043 acc = (acc << 4) + c - 'A' + 10;
1047 if (c == ' ' || c == '\t')
1048 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1053 printf("%s is an invalid hex number. Try again.\n",
1062 string(char *str, char **ans)
1071 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
1072 fgets(lbuf, LBUF, stdin);
1073 lbuf[strlen(lbuf)-1] = 0;
1078 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1082 while ((c = *cp) && c != '"') cp++;
1085 while ((c = *cp) && c != ' ' && c != '\t') cp++;
1089 for (i = strlen(*ans); i < 16; i++)
1104 int numentries = (sizeof(part_types)/sizeof(struct part_type));
1106 struct part_type *ptr = part_types;
1109 while(counter < numentries)
1112 if(ptr->type == (type & 0x7f))
1114 if(ptr->type == type)
1127 parse_config_line(line, command)
1134 while (1) /* dirty trick used to insure one exit point for this
1137 memset(command, 0, sizeof(*command));
1139 while (isspace(*cp)) ++cp;
1140 if (*cp == '\0' || *cp == '#')
1144 command->cmd = *cp++;
1151 while (isspace(*cp)) ++cp;
1154 break; /* found comment */
1158 command->args[command->n_args].argtype = *cp++;
1162 break; /* assume end of line */
1165 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1168 break; /* couldn't parse number */
1179 process_geometry(command)
1190 "ERROR line %d: the geometry specification line must occur before\n\
1191 all partition specifications",
1192 current_line_number);
1196 if (command->n_args != 3)
1198 warnx("ERROR line %d: incorrect number of geometry args",
1199 current_line_number);
1206 for (i = 0; i < 3; ++i)
1208 switch (command->args[i].argtype)
1211 dos_cyls = command->args[i].arg_val;
1214 dos_heads = command->args[i].arg_val;
1217 dos_sectors = command->args[i].arg_val;
1221 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1222 current_line_number, command->args[i].argtype,
1223 command->args[i].argtype);
1233 dos_cylsecs = dos_heads * dos_sectors;
1236 * Do sanity checks on parameter values
1240 warnx("ERROR line %d: number of cylinders not specified",
1241 current_line_number);
1244 if (dos_cyls == 0 || dos_cyls > 1024)
1247 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1248 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1249 is dedicated to FreeBSD)",
1250 current_line_number, dos_cyls);
1255 warnx("ERROR line %d: number of heads not specified",
1256 current_line_number);
1259 else if (dos_heads < 1 || dos_heads > 256)
1261 warnx("ERROR line %d: number of heads must be within (1-256)",
1262 current_line_number);
1266 if (dos_sectors < 0)
1268 warnx("ERROR line %d: number of sectors not specified",
1269 current_line_number);
1272 else if (dos_sectors < 1 || dos_sectors > 63)
1274 warnx("ERROR line %d: number of sectors must be within (1-63)",
1275 current_line_number);
1286 process_partition(command)
1289 int status = 0, partition;
1290 unsigned long chunks, adj_size, max_end;
1291 struct dos_partition *partp;
1296 if (command->n_args != 4)
1298 warnx("ERROR line %d: incorrect number of partition args",
1299 current_line_number);
1302 partition = command->args[0].arg_val;
1303 if (partition < 1 || partition > 4)
1305 warnx("ERROR line %d: invalid partition number %d",
1306 current_line_number, partition);
1309 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1310 bzero((char *)partp, sizeof (struct dos_partition));
1311 partp->dp_typ = command->args[1].arg_val;
1312 partp->dp_start = command->args[2].arg_val;
1313 partp->dp_size = command->args[3].arg_val;
1314 max_end = partp->dp_start + partp->dp_size;
1316 if (partp->dp_typ == 0)
1319 * Get out, the partition is marked as unused.
1322 * Insure that it's unused.
1324 bzero((char *)partp, sizeof (struct dos_partition));
1330 * Adjust start upwards, if necessary, to fall on an head boundary.
1332 if (partp->dp_start % dos_sectors != 0)
1335 (partp->dp_start / dos_sectors + 1) * dos_sectors;
1336 if (adj_size > max_end)
1339 * Can't go past end of partition
1342 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1343 a cylinder boundary",
1344 current_line_number, partition);
1348 "WARNING: adjusting start offset of partition '%d' from %lu\n\
1349 to %lu, to round to an head boundary",
1350 partition, (u_long)partp->dp_start, adj_size);
1351 partp->dp_start = adj_size;
1355 * Adjust size downwards, if necessary, to fall on a cylinder
1359 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1360 adj_size = chunks - partp->dp_start;
1361 if (adj_size != partp->dp_size)
1364 "WARNING: adjusting size of partition '%d' from %lu to %lu,\n\
1365 to round to a cylinder boundary",
1366 partition, (u_long)partp->dp_size, adj_size);
1369 partp->dp_size = adj_size;
1376 if (partp->dp_size < 1)
1378 warnx("ERROR line %d: size for partition '%d' is zero",
1379 current_line_number, partition);
1383 dos(partp->dp_start, partp->dp_size,
1384 &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
1385 dos(partp->dp_start+partp->dp_size - 1, partp->dp_size,
1386 &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
1395 process_active(command)
1398 int status = 0, partition, i;
1399 struct dos_partition *partp;
1403 active_processed = 1;
1404 if (command->n_args != 1)
1406 warnx("ERROR line %d: incorrect number of active args",
1407 current_line_number);
1411 partition = command->args[0].arg_val;
1412 if (partition < 1 || partition > 4)
1414 warnx("ERROR line %d: invalid partition number %d",
1415 current_line_number, partition);
1419 * Reset active partition
1421 partp = ((struct dos_partition *) &mboot.parts);
1422 for (i = 0; i < NDOSPART; i++)
1423 partp[i].dp_flag = 0;
1424 partp[partition-1].dp_flag = ACTIVE;
1442 parse_config_line(line, &command);
1443 switch (command.cmd)
1447 * Comment or blank line
1454 status = process_geometry(&command);
1457 status = process_partition(&command);
1460 status = process_active(&command);
1473 read_config(config_file)
1480 while (1) /* dirty trick used to insure one exit point for this
1483 if (strcmp(config_file, "-") != 0)
1486 * We're not reading from stdin
1488 if ((fp = fopen(config_file, "r")) == NULL)
1498 current_line_number = 0;
1501 if (fgets(buf, sizeof(buf), fp) == NULL)
1505 ++current_line_number;
1506 status = process_line(buf);
1517 * It doesn't matter if we're reading from stdin, as we've reached EOF
1529 struct dos_partition *partp;
1532 for (i = 0; i < 4; ++i)
1534 partp = ((struct dos_partition *) &mboot.parts) + i;
1535 bzero((char *)partp, sizeof (struct dos_partition));