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.3 2003/09/28 14:39:20 hmp 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?"))
765 get_params_to_use(void)
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 \***********************************************/
789 dos(int sec, int size, unsigned short *c, unsigned char *s, unsigned char *h)
791 dos(int sec, int size, unsigned char *c, unsigned char *s, unsigned char *h)
797 if (sec == 0 && size == 0) {
802 cy = sec / ( dos_cylsecs );
803 sec = sec - cy * ( dos_cylsecs );
805 hd = sec / dos_sectors;
807 sec = (sec - hd * dos_sectors);
813 sec = (sec - hd * dos_sectors) + 1;
817 *s = (sec & 0x3f) | ( (cy & 0x300) >> 2);
823 /* Getting device status */
826 open_disk(int u_flag)
830 if (stat(disk, &st) == -1) {
831 warnx("can't get file status of %s", disk);
834 if ( !(st.st_mode & S_IFCHR) )
835 warnx("device %s is not character special", disk);
837 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
840 warnx("can't open device %s", disk);
843 if (get_params(0) == -1) {
844 warnx("can't get disk parameters on %s", disk);
851 read_disk(off_t sector, void *buf)
853 lseek(fd,(sector * 512), 0);
855 return read(fd, buf, secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2);
858 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
861 int size = read(fd, buf, secsize);
862 if( size == secsize )
863 /* it worked so return */
867 return read( fd, buf, secsize );
869 /* we failed to read at any of the sizes */
875 write_disk(off_t sector, void *buf)
877 lseek(fd,(sector * 512), 0);
878 /* write out in the size that the read_disk found worked */
880 return write(fd, buf, secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2);
882 return write(fd, buf, secsize);
890 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
891 warnx("can't get disk parameters on %s; supplying dummy ones", disk);
893 dos_heads = heads = 1;
894 dos_sectors = sectors = 1;
895 dos_cylsecs = cylsecs = heads * sectors;
896 disksecs = cyls * heads * sectors;
898 secsize = disklabel.d_secsize;
903 dos_cyls = cyls = disklabel.d_ncylinders;
904 dos_heads = heads = disklabel.d_ntracks;
905 dos_sectors = sectors = disklabel.d_nsectors;
906 dos_cylsecs = cylsecs = heads * sectors;
907 disksecs = cyls * heads * sectors;
909 secsize = disklabel.d_secsize;
918 if (read_disk(0, (char *) mboot.bootinst) == -1) {
919 warnx("can't read fdisk partition table");
922 if (mboot.signature != BOOT_MAGIC) {
923 warnx("invalid fdisk partition table found");
924 /* So should we initialize things */
941 * write enable label sector before write (if necessary),
942 * disable after writing.
943 * needed if the disklabel protected area also protects
944 * sector 0. (e.g. empty disk)
948 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
949 warn("ioctl DIOCWLABEL");
951 if (write_disk(0, (char *) mboot.bootinst) == -1) {
952 warn("can't write fdisk partition table");
956 (void) ioctl(fd, DIOCWLABEL, &flag);
966 printf("%s [n] ", str);
967 fgets(lbuf, LBUF, stdin);
968 lbuf[strlen(lbuf)-1] = 0;
971 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
972 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
979 decimal(char *str, int *num, int deflt)
985 printf("Supply a decimal value for \"%s\" [%d] ", 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 * 10 + c - '0';
1002 if (c == ' ' || c == '\t')
1003 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1008 printf("%s is an invalid decimal number. Try again.\n",
1016 hex(char *str, int *num, int deflt)
1022 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
1023 fgets(lbuf, LBUF, stdin);
1024 lbuf[strlen(lbuf)-1] = 0;
1030 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1033 while ((c = *cp++)) {
1034 if (c <= '9' && c >= '0')
1035 acc = (acc << 4) + c - '0';
1036 else if (c <= 'f' && c >= 'a')
1037 acc = (acc << 4) + c - 'a' + 10;
1038 else if (c <= 'F' && c >= 'A')
1039 acc = (acc << 4) + c - 'A' + 10;
1043 if (c == ' ' || c == '\t')
1044 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1049 printf("%s is an invalid hex number. Try again.\n",
1058 string(char *str, char **ans)
1067 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
1068 fgets(lbuf, LBUF, stdin);
1069 lbuf[strlen(lbuf)-1] = 0;
1074 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1078 while ((c = *cp) && c != '"') cp++;
1081 while ((c = *cp) && c != ' ' && c != '\t') cp++;
1085 for (i = strlen(*ans); i < 16; i++)
1100 int numentries = (sizeof(part_types)/sizeof(struct part_type));
1102 struct part_type *ptr = part_types;
1105 while(counter < numentries)
1108 if(ptr->type == (type & 0x7f))
1110 if(ptr->type == type)
1123 parse_config_line(char *line, CMD *command)
1128 while (1) /* dirty trick used to insure one exit point for this
1131 memset(command, 0, sizeof(*command));
1133 while (isspace(*cp)) ++cp;
1134 if (*cp == '\0' || *cp == '#')
1138 command->cmd = *cp++;
1145 while (isspace(*cp)) ++cp;
1148 break; /* found comment */
1152 command->args[command->n_args].argtype = *cp++;
1156 break; /* assume end of line */
1159 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1162 break; /* couldn't parse number */
1173 process_geometry(CMD *command)
1183 "ERROR line %d: the geometry specification line must occur before\n\
1184 all partition specifications",
1185 current_line_number);
1189 if (command->n_args != 3)
1191 warnx("ERROR line %d: incorrect number of geometry args",
1192 current_line_number);
1199 for (i = 0; i < 3; ++i)
1201 switch (command->args[i].argtype)
1204 dos_cyls = command->args[i].arg_val;
1207 dos_heads = command->args[i].arg_val;
1210 dos_sectors = command->args[i].arg_val;
1214 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1215 current_line_number, command->args[i].argtype,
1216 command->args[i].argtype);
1226 dos_cylsecs = dos_heads * dos_sectors;
1229 * Do sanity checks on parameter values
1233 warnx("ERROR line %d: number of cylinders not specified",
1234 current_line_number);
1237 if (dos_cyls == 0 || dos_cyls > 1024)
1240 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1241 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1242 is dedicated to FreeBSD)",
1243 current_line_number, dos_cyls);
1248 warnx("ERROR line %d: number of heads not specified",
1249 current_line_number);
1252 else if (dos_heads < 1 || dos_heads > 256)
1254 warnx("ERROR line %d: number of heads must be within (1-256)",
1255 current_line_number);
1259 if (dos_sectors < 0)
1261 warnx("ERROR line %d: number of sectors not specified",
1262 current_line_number);
1265 else if (dos_sectors < 1 || dos_sectors > 63)
1267 warnx("ERROR line %d: number of sectors must be within (1-63)",
1268 current_line_number);
1279 process_partition(CMD *command)
1281 int status = 0, partition;
1282 unsigned long chunks, adj_size, max_end;
1283 struct dos_partition *partp;
1288 if (command->n_args != 4)
1290 warnx("ERROR line %d: incorrect number of partition args",
1291 current_line_number);
1294 partition = command->args[0].arg_val;
1295 if (partition < 1 || partition > 4)
1297 warnx("ERROR line %d: invalid partition number %d",
1298 current_line_number, partition);
1301 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1302 bzero((char *)partp, sizeof (struct dos_partition));
1303 partp->dp_typ = command->args[1].arg_val;
1304 partp->dp_start = command->args[2].arg_val;
1305 partp->dp_size = command->args[3].arg_val;
1306 max_end = partp->dp_start + partp->dp_size;
1308 if (partp->dp_typ == 0)
1311 * Get out, the partition is marked as unused.
1314 * Insure that it's unused.
1316 bzero((char *)partp, sizeof (struct dos_partition));
1322 * Adjust start upwards, if necessary, to fall on an head boundary.
1324 if (partp->dp_start % dos_sectors != 0)
1327 (partp->dp_start / dos_sectors + 1) * dos_sectors;
1328 if (adj_size > max_end)
1331 * Can't go past end of partition
1334 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1335 a cylinder boundary",
1336 current_line_number, partition);
1340 "WARNING: adjusting start offset of partition '%d' from %lu\n\
1341 to %lu, to round to an head boundary",
1342 partition, (u_long)partp->dp_start, adj_size);
1343 partp->dp_start = adj_size;
1347 * Adjust size downwards, if necessary, to fall on a cylinder
1351 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1352 adj_size = chunks - partp->dp_start;
1353 if (adj_size != partp->dp_size)
1356 "WARNING: adjusting size of partition '%d' from %lu to %lu,\n\
1357 to round to a cylinder boundary",
1358 partition, (u_long)partp->dp_size, adj_size);
1361 partp->dp_size = adj_size;
1368 if (partp->dp_size < 1)
1370 warnx("ERROR line %d: size for partition '%d' is zero",
1371 current_line_number, partition);
1375 dos(partp->dp_start, partp->dp_size,
1376 &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
1377 dos(partp->dp_start+partp->dp_size - 1, partp->dp_size,
1378 &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
1387 process_active(CMD *command)
1389 int status = 0, partition, i;
1390 struct dos_partition *partp;
1394 active_processed = 1;
1395 if (command->n_args != 1)
1397 warnx("ERROR line %d: incorrect number of active args",
1398 current_line_number);
1402 partition = command->args[0].arg_val;
1403 if (partition < 1 || partition > 4)
1405 warnx("ERROR line %d: invalid partition number %d",
1406 current_line_number, partition);
1410 * Reset active partition
1412 partp = ((struct dos_partition *) &mboot.parts);
1413 for (i = 0; i < NDOSPART; i++)
1414 partp[i].dp_flag = 0;
1415 partp[partition-1].dp_flag = ACTIVE;
1425 process_line(char *line)
1432 parse_config_line(line, &command);
1433 switch (command.cmd)
1437 * Comment or blank line
1444 status = process_geometry(&command);
1447 status = process_partition(&command);
1450 status = process_active(&command);
1463 read_config(char *config_file)
1469 while (1) /* dirty trick used to insure one exit point for this
1472 if (strcmp(config_file, "-") != 0)
1475 * We're not reading from stdin
1477 if ((fp = fopen(config_file, "r")) == NULL)
1487 current_line_number = 0;
1490 if (fgets(buf, sizeof(buf), fp) == NULL)
1494 ++current_line_number;
1495 status = process_line(buf);
1506 * It doesn't matter if we're reading from stdin, as we've reached EOF
1518 struct dos_partition *partp;
1521 for (i = 0; i < 4; ++i)
1523 partp = ((struct dos_partition *) &mboot.parts) + i;
1524 bzero((char *)partp, sizeof (struct dos_partition));