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.
28 static const char rcsid[] =
32 #include <sys/disklabel.h>
47 static char lbuf[LBUF];
51 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
53 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
54 * Copyright (c) 1989 Robert. V. Baron
58 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
59 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
60 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
62 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
64 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
65 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
66 int secsize = 0; /* the sensed sector size */
71 "/dev/ad0", "/dev/wd0", "/dev/da0", "/dev/od0", 0
74 struct disklabel disklabel; /* disk parameters */
76 int cyls, sectors, heads, cylsecs, disksecs;
80 unsigned char padding[2]; /* force the longs to be long aligned */
82 unsigned char bootinst[510];
84 unsigned char bootinst[DOSPARTOFF];
85 struct dos_partition parts[4];
87 unsigned short int signature;
90 struct dos_partition parts[8];
92 /* room to read in MBRs that are bigger then DEV_BSIZE */
93 unsigned char large_sector_overflow[MAX_SEC_SIZE-MIN_SEC_SIZE];
98 #define BOOT_MAGIC 0xAA55
106 #define DOSSECT(s,c) (s)
107 #define DOSCYL(c) (c)
109 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
110 #define DOSCYL(c) (c & 0xff)
112 static int partition = -1;
117 static int current_line_number;
119 static int geom_processed = 0;
120 static int part_processed = 0;
121 static int active_processed = 0;
134 static int B_flag = 0; /* replace boot code */
135 static int I_flag = 0; /* use entire disk for FreeBSD */
136 static int a_flag = 0; /* set active partition */
137 static char *b_flag = NULL; /* path to boot code */
138 static int i_flag = 0; /* replace partition data */
139 static int u_flag = 0; /* update partition data */
140 static int s_flag = 0; /* Print a summary and exit */
141 static int t_flag = 0; /* test only, if f_flag is given */
142 static char *f_flag = NULL; /* Read config info from file */
143 static int v_flag = 0; /* Be verbose */
152 ,{0x01, "Primary DOS with 12 bit FAT"}
159 ,{0x02, "XENIX / filesystem"}
160 ,{0x03, "XENIX /usr filesystem"}
162 ,{0x05, "Extended DOS"}
163 ,{0x06, "Primary 'big' DOS (> 32MB)"}
164 ,{0x07, "OS/2 HPFS, QNX or Advanced UNIX"}
165 ,{0x08, "AIX filesystem"}
166 ,{0x09, "AIX boot partition or Coherent"}
167 ,{0x0A, "OS/2 Boot Manager or OPUS"}
169 ,{0x14, "FreeBSD/NetBSD/386BSD"}
170 ,{0x94, "FreeBSD/NetBSD/386BSD"}
174 ,{0x52, "CP/M or Microport SysV/AT"}
177 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
178 ,{0x64, "Novell Netware 2.xx"}
179 ,{0x65, "Novell Netware 3.xx"}
183 ,{0x02, "XENIX / filesystem"}
184 ,{0x03, "XENIX /usr filesystem"}
185 ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
186 ,{0x05, "Extended DOS"}
187 ,{0x06, "Primary 'big' DOS (> 32MB)"}
188 ,{0x07, "OS/2 HPFS, NTFS, QNX or Advanced UNIX"}
189 ,{0x08, "AIX filesystem"}
190 ,{0x09, "AIX boot partition or Coherent"}
191 ,{0x0A, "OS/2 Boot Manager or OPUS"}
192 ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
193 ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
194 ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
195 ,{0x0F, "Extended DOS, LBA"}
200 ,{0x52, "CP/M or Microport SysV/AT"}
203 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
204 ,{0x64, "Novell Netware 2.xx"}
205 ,{0x65, "Novell Netware 3.xx"}
207 ,{0x80, "Minix 1.1 ... 1.4a"}
208 ,{0x81, "Minix 1.4b ... 1.5.10"}
209 ,{0x82, "Linux swap or Solaris x86"}
210 ,{0x83, "Linux filesystem"}
211 ,{0x93, "Amoeba filesystem"}
212 ,{0x94, "Amoeba bad block table"}
214 ,{0xA5, "FreeBSD/NetBSD/386BSD"}
218 ,{0xB7, "BSDI BSD/386 filesystem"}
219 ,{0xB8, "BSDI BSD/386 swap"}
220 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
225 ,{0xF2, "DOS 3.3+ Secondary"}
227 ,{0xFF, "BBT (Bad Blocks Table)"}
231 static void print_s0(int which);
232 static void print_part(int i);
233 static void init_sector0(unsigned long start);
234 static void init_boot(void);
235 static void change_part(int i);
236 static void print_params();
237 static void change_active(int which);
238 static void change_code();
239 static void get_params_to_use();
241 static void dos(int sec, int size, unsigned short *c, unsigned char *s,
244 static void dos(int sec, int size, unsigned char *c, unsigned char *s,
247 static int open_disk(int u_flag);
248 static ssize_t read_disk(off_t sector, void *buf);
249 static ssize_t write_disk(off_t sector, void *buf);
250 static int get_params();
251 static int read_s0();
252 static int write_s0();
253 static int ok(char *str);
254 static int decimal(char *str, int *num, int deflt);
255 static char *get_type(int type);
256 static int read_config(char *config_file);
257 static void reset_boot(void);
258 static void usage(void);
260 static int hex(char *str, int *num, int deflt);
263 static int string(char *str, char **ans);
269 main(int argc, char *argv[])
274 while ((c = getopt(argc, argv, "Bab:f:istuv12345678")) != -1)
276 while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
324 if (f_flag || i_flag)
333 static char realname[12];
335 if(strncmp(argv[0], _PATH_DEV, sizeof _PATH_DEV - 2) == 0)
339 snprintf(realname, 12, "%s%s", _PATH_DEV, argv[0]);
343 if (open_disk(u_flag) < 0)
344 err(1, "cannot open disk %s", disk);
350 for(i = 0; disks[i]; i++)
353 rv = open_disk(u_flag);
357 err(1, "cannot open any disk");
362 struct dos_partition *partp;
366 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
369 printf("Part %11s %11s SID\n", "Start", "Size");
371 printf("Part %11s %11s Type Flags\n", "Start", "Size");
373 for (i = 0; i < NDOSPART; i++) {
374 partp = ((struct dos_partition *) &mboot.parts) + i;
376 if (partp->dp_sid == 0)
378 if (partp->dp_start == 0 && partp->dp_size == 0)
381 printf("%4d: %11lu %11lu 0x%02x\n", i + 1,
383 partp->dp_scyl * cylsecs,
384 (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs,
387 (u_long) partp->dp_start,
388 (u_long) partp->dp_size, partp->dp_typ,
395 printf("******* Working on device %s *******\n",disk);
400 struct dos_partition *partp;
404 partp = (struct dos_partition *) (&mboot.parts[0]);
405 partp->dp_typ = DOSPTYP_386BSD;
406 partp->dp_flag = ACTIVE;
407 partp->dp_start = dos_sectors;
408 partp->dp_size = disksecs - dos_sectors;
410 dos(partp->dp_start, partp->dp_size,
411 &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
412 dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
413 &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
423 if (read_s0() || i_flag)
428 if (!read_config(f_flag))
456 printf("Media sector size is %d\n", secsize);
457 printf("Warning: BIOS sector numbering starts with sector 1\n");
458 printf("Information from DOS bootblock is:\n");
460 for (i = 1; i <= NDOSPART; i++)
463 change_part(partition);
465 if (u_flag || a_flag)
466 change_active(partition);
471 if (u_flag || a_flag || B_flag) {
474 printf("\nWe haven't changed the partition table yet. ");
475 printf("This is your last chance.\n");
480 if (ok("Should we write new partition table?"))
485 printf("\n-t flag specified -- partition table not written.\n");
496 fprintf(stderr, "%s%s",
497 "usage: fdisk [-Batu] [-b bootcode] [-12345678] [disk]\n",
498 " fdisk -f configfile [-itv] [disk]\n");
508 printf("Information from DOS bootblock is:\n");
510 for (i = 1; i <= NDOSPART; i++)
511 printf("%d: ", i), print_part(i);
516 static struct dos_partition mtpart = { 0 };
521 struct dos_partition *partp;
524 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
526 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
527 printf("<UNUSED>\n");
531 * Be careful not to overflow.
534 printf("sysmid %d,(%s)\n", partp->dp_mid, get_type(partp->dp_mid));
535 printf(" start %d, size %d (%d Meg), sid %d\n",
536 partp->dp_scyl * cylsecs ,
537 (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs,
538 (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs * 512 / (1024 * 1024),
541 part_mb = partp->dp_size;
543 part_mb /= (1024 * 1024);
544 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
545 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
546 (u_long)partp->dp_start,
547 (u_long)partp->dp_size,
550 partp->dp_flag == ACTIVE ? " (active)" : "");
552 printf("\tbeg: cyl %d/ sector %d/ head %d;\n\tend: cyl %d/ sector %d/ head %d\n"
553 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
554 ,DPSECT(partp->dp_ssect)
556 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
557 ,DPSECT(partp->dp_esect)
560 printf ("\tsystem Name %.16s\n",partp->dp_name);
572 fname = b_flag ? b_flag : "/boot/mbr";
573 if ((fd = open(fname, O_RDONLY)) == -1 ||
574 read(fd, mboot.bootinst, DOSPARTOFF) == -1 ||
578 mboot.signature = BOOT_MAGIC;
583 init_sector0(unsigned long start)
585 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
586 unsigned long size = disksecs - start;
591 partp->dp_mid = DOSMID_386BSD;
592 partp->dp_sid = DOSSID_386BSD;
594 dos(start, size, &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
595 partp->dp_ipl_cyl = partp->dp_scyl;
596 partp->dp_ipl_sct = partp->dp_ssect;
597 partp->dp_ipl_head = partp->dp_shd;
598 dos(start+size-cylsecs, size, &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
600 partp->dp_typ = DOSPTYP_386BSD;
601 partp->dp_flag = ACTIVE;
602 partp->dp_start = start;
603 partp->dp_size = size;
605 dos(partp->dp_start, partp->dp_size,
606 &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
607 dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
608 &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
615 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
617 printf("The data for partition %d is:\n", i);
620 if (u_flag && ok("Do you want to change it?")) {
624 bzero((char *)partp, sizeof (struct dos_partition));
627 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
634 int x_start = partp->dp_scyl * cylsecs ;
635 int x_size = (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs;
636 Decimal("sysmid", partp->dp_mid, tmp);
637 Decimal("syssid", partp->dp_sid, tmp);
638 String ("system name", partp->dp_name, 16);
639 Decimal("start", x_start, tmp);
640 Decimal("size", x_size, tmp);
642 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
643 Decimal("start", partp->dp_start, tmp);
644 Decimal("size", partp->dp_size, tmp);
646 if (ok("Explicitly specify beg/end address ?"))
649 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
651 tsec = DPSECT(partp->dp_ssect);
652 Decimal("beginning cylinder", tcyl, tmp);
653 Decimal("beginning head", thd, tmp);
654 Decimal("beginning sector", tsec, tmp);
655 partp->dp_scyl = DOSCYL(tcyl);
656 partp->dp_ssect = DOSSECT(tsec,tcyl);
659 partp->dp_ipl_cyl = partp->dp_scyl;
660 partp->dp_ipl_sct = partp->dp_ssect;
661 partp->dp_ipl_head = partp->dp_shd;
664 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
666 tsec = DPSECT(partp->dp_esect);
667 Decimal("ending cylinder", tcyl, tmp);
668 Decimal("ending head", thd, tmp);
669 Decimal("ending sector", tsec, tmp);
670 partp->dp_ecyl = DOSCYL(tcyl);
671 partp->dp_esect = DOSSECT(tsec,tcyl);
675 dos(x_start, x_size, &partp->dp_scyl,
676 &partp->dp_ssect, &partp->dp_shd);
677 partp->dp_ipl_cyl = partp->dp_scyl;
678 partp->dp_ipl_sct = partp->dp_ssect;
679 partp->dp_ipl_head = partp->dp_shd;
680 dos(x_start+x_size - cylsecs, x_size, &partp->dp_ecyl,
681 &partp->dp_esect, &partp->dp_ehd);
683 dos(partp->dp_start, partp->dp_size,
684 &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
685 dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
686 &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
691 } while (!ok("Are we happy with this entry?"));
698 printf("parameters extracted from in-core disklabel are:\n");
699 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
700 ,cyls,heads,sectors,cylsecs);
702 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
703 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
705 printf("parameters to be used for BIOS calculations are:\n");
706 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
707 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
711 change_active(int which)
720 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts);
722 if (a_flag && which != -1)
724 if (!ok("Do you want to change the active partition?"))
733 Decimal("active partition", active, tmp);
735 if (active < 1 || 8 < active) {
736 printf("Active partition number must be in range 1-8."
741 if (active < 1 || 4 < active) {
742 printf("Active partition number must be in range 1-4."
747 } while (!ok("Are you happy with this choice"));
749 partp[active].dp_sid |= ACTIVE;
751 for (i = 0; i < NDOSPART; i++)
752 partp[i].dp_flag = 0;
753 if (active > 0 && active <= NDOSPART)
754 partp[active-1].dp_flag = ACTIVE;
761 if (ok("Do you want to change the boot code?"))
771 if (ok("Do you want to change our idea of what BIOS thinks ?"))
775 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
776 Decimal("BIOS's idea of #heads", dos_heads, tmp);
777 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
778 dos_cylsecs = dos_heads * dos_sectors;
781 while(!ok("Are you happy with this choice"));
786 /***********************************************\
787 * Change real numbers into strange dos numbers *
788 \***********************************************/
790 dos(sec, size, c, s, h)
794 unsigned char *s, *h;
796 unsigned char *c, *s, *h;
802 if (sec == 0 && size == 0) {
807 cy = sec / ( dos_cylsecs );
808 sec = sec - cy * ( dos_cylsecs );
810 hd = sec / dos_sectors;
812 sec = (sec - hd * dos_sectors);
818 sec = (sec - hd * dos_sectors) + 1;
822 *s = (sec & 0x3f) | ( (cy & 0x300) >> 2);
828 /* Getting device status */
831 open_disk(int u_flag)
835 if (stat(disk, &st) == -1) {
836 warnx("can't get file status of %s", disk);
839 if ( !(st.st_mode & S_IFCHR) )
840 warnx("device %s is not character special", disk);
842 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
845 warnx("can't open device %s", disk);
848 if (get_params(0) == -1) {
849 warnx("can't get disk parameters on %s", disk);
856 read_disk(off_t sector, void *buf)
858 lseek(fd,(sector * 512), 0);
860 return read(fd, buf, secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2);
863 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
866 int size = read(fd, buf, secsize);
867 if( size == secsize )
868 /* it worked so return */
872 return read( fd, buf, secsize );
874 /* 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 */
885 return write(fd, buf, secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2);
887 return write(fd, buf, secsize);
895 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
896 warnx("can't get disk parameters on %s; supplying dummy ones", disk);
898 dos_heads = heads = 1;
899 dos_sectors = sectors = 1;
900 dos_cylsecs = cylsecs = heads * sectors;
901 disksecs = cyls * heads * sectors;
903 secsize = disklabel.d_secsize;
908 dos_cyls = cyls = disklabel.d_ncylinders;
909 dos_heads = heads = disklabel.d_ntracks;
910 dos_sectors = sectors = disklabel.d_nsectors;
911 dos_cylsecs = cylsecs = heads * sectors;
912 disksecs = cyls * heads * sectors;
914 secsize = disklabel.d_secsize;
923 if (read_disk(0, (char *) mboot.bootinst) == -1) {
924 warnx("can't read fdisk partition table");
927 if (mboot.signature != BOOT_MAGIC) {
928 warnx("invalid fdisk partition table found");
929 /* So should we initialize things */
946 * write enable label sector before write (if necessary),
947 * disable after writing.
948 * needed if the disklabel protected area also protects
949 * sector 0. (e.g. empty disk)
953 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
954 warn("ioctl DIOCWLABEL");
956 if (write_disk(0, (char *) mboot.bootinst) == -1) {
957 warn("can't write fdisk partition table");
961 (void) ioctl(fd, DIOCWLABEL, &flag);
972 printf("%s [n] ", str);
973 fgets(lbuf, LBUF, stdin);
974 lbuf[strlen(lbuf)-1] = 0;
977 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
978 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
985 decimal(char *str, int *num, int deflt)
991 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
992 fgets(lbuf, LBUF, stdin);
993 lbuf[strlen(lbuf)-1] = 0;
999 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1002 while ((c = *cp++)) {
1003 if (c <= '9' && c >= '0')
1004 acc = acc * 10 + c - '0';
1008 if (c == ' ' || c == '\t')
1009 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1014 printf("%s is an invalid decimal number. Try again.\n",
1022 hex(char *str, int *num, int deflt)
1028 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
1029 fgets(lbuf, LBUF, stdin);
1030 lbuf[strlen(lbuf)-1] = 0;
1036 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1039 while ((c = *cp++)) {
1040 if (c <= '9' && c >= '0')
1041 acc = (acc << 4) + c - '0';
1042 else if (c <= 'f' && c >= 'a')
1043 acc = (acc << 4) + c - 'a' + 10;
1044 else if (c <= 'F' && c >= 'A')
1045 acc = (acc << 4) + c - 'A' + 10;
1049 if (c == ' ' || c == '\t')
1050 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1055 printf("%s is an invalid hex number. Try again.\n",
1064 string(char *str, char **ans)
1073 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
1074 fgets(lbuf, LBUF, stdin);
1075 lbuf[strlen(lbuf)-1] = 0;
1080 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1084 while ((c = *cp) && c != '"') cp++;
1087 while ((c = *cp) && c != ' ' && c != '\t') cp++;
1091 for (i = strlen(*ans); i < 16; i++)
1106 int numentries = (sizeof(part_types)/sizeof(struct part_type));
1108 struct part_type *ptr = part_types;
1111 while(counter < numentries)
1114 if(ptr->type == (type & 0x7f))
1116 if(ptr->type == type)
1129 parse_config_line(line, command)
1136 while (1) /* dirty trick used to insure one exit point for this
1139 memset(command, 0, sizeof(*command));
1141 while (isspace(*cp)) ++cp;
1142 if (*cp == '\0' || *cp == '#')
1146 command->cmd = *cp++;
1153 while (isspace(*cp)) ++cp;
1156 break; /* found comment */
1160 command->args[command->n_args].argtype = *cp++;
1164 break; /* assume end of line */
1167 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1170 break; /* couldn't parse number */
1181 process_geometry(command)
1192 "ERROR line %d: the geometry specification line must occur before\n\
1193 all partition specifications",
1194 current_line_number);
1198 if (command->n_args != 3)
1200 warnx("ERROR line %d: incorrect number of geometry args",
1201 current_line_number);
1208 for (i = 0; i < 3; ++i)
1210 switch (command->args[i].argtype)
1213 dos_cyls = command->args[i].arg_val;
1216 dos_heads = command->args[i].arg_val;
1219 dos_sectors = command->args[i].arg_val;
1223 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1224 current_line_number, command->args[i].argtype,
1225 command->args[i].argtype);
1235 dos_cylsecs = dos_heads * dos_sectors;
1238 * Do sanity checks on parameter values
1242 warnx("ERROR line %d: number of cylinders not specified",
1243 current_line_number);
1246 if (dos_cyls == 0 || dos_cyls > 1024)
1249 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1250 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1251 is dedicated to FreeBSD)",
1252 current_line_number, dos_cyls);
1257 warnx("ERROR line %d: number of heads not specified",
1258 current_line_number);
1261 else if (dos_heads < 1 || dos_heads > 256)
1263 warnx("ERROR line %d: number of heads must be within (1-256)",
1264 current_line_number);
1268 if (dos_sectors < 0)
1270 warnx("ERROR line %d: number of sectors not specified",
1271 current_line_number);
1274 else if (dos_sectors < 1 || dos_sectors > 63)
1276 warnx("ERROR line %d: number of sectors must be within (1-63)",
1277 current_line_number);
1288 process_partition(command)
1291 int status = 0, partition;
1292 unsigned long chunks, adj_size, max_end;
1293 struct dos_partition *partp;
1298 if (command->n_args != 4)
1300 warnx("ERROR line %d: incorrect number of partition args",
1301 current_line_number);
1304 partition = command->args[0].arg_val;
1305 if (partition < 1 || partition > 4)
1307 warnx("ERROR line %d: invalid partition number %d",
1308 current_line_number, partition);
1311 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1312 bzero((char *)partp, sizeof (struct dos_partition));
1313 partp->dp_typ = command->args[1].arg_val;
1314 partp->dp_start = command->args[2].arg_val;
1315 partp->dp_size = command->args[3].arg_val;
1316 max_end = partp->dp_start + partp->dp_size;
1318 if (partp->dp_typ == 0)
1321 * Get out, the partition is marked as unused.
1324 * Insure that it's unused.
1326 bzero((char *)partp, sizeof (struct dos_partition));
1332 * Adjust start upwards, if necessary, to fall on an head boundary.
1334 if (partp->dp_start % dos_sectors != 0)
1337 (partp->dp_start / dos_sectors + 1) * dos_sectors;
1338 if (adj_size > max_end)
1341 * Can't go past end of partition
1344 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1345 a cylinder boundary",
1346 current_line_number, partition);
1350 "WARNING: adjusting start offset of partition '%d' from %lu\n\
1351 to %lu, to round to an head boundary",
1352 partition, (u_long)partp->dp_start, adj_size);
1353 partp->dp_start = adj_size;
1357 * Adjust size downwards, if necessary, to fall on a cylinder
1361 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1362 adj_size = chunks - partp->dp_start;
1363 if (adj_size != partp->dp_size)
1366 "WARNING: adjusting size of partition '%d' from %lu to %lu,\n\
1367 to round to a cylinder boundary",
1368 partition, (u_long)partp->dp_size, adj_size);
1371 partp->dp_size = adj_size;
1378 if (partp->dp_size < 1)
1380 warnx("ERROR line %d: size for partition '%d' is zero",
1381 current_line_number, partition);
1385 dos(partp->dp_start, partp->dp_size,
1386 &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
1387 dos(partp->dp_start+partp->dp_size - 1, partp->dp_size,
1388 &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
1397 process_active(command)
1400 int status = 0, partition, i;
1401 struct dos_partition *partp;
1405 active_processed = 1;
1406 if (command->n_args != 1)
1408 warnx("ERROR line %d: incorrect number of active args",
1409 current_line_number);
1413 partition = command->args[0].arg_val;
1414 if (partition < 1 || partition > 4)
1416 warnx("ERROR line %d: invalid partition number %d",
1417 current_line_number, partition);
1421 * Reset active partition
1423 partp = ((struct dos_partition *) &mboot.parts);
1424 for (i = 0; i < NDOSPART; i++)
1425 partp[i].dp_flag = 0;
1426 partp[partition-1].dp_flag = ACTIVE;
1444 parse_config_line(line, &command);
1445 switch (command.cmd)
1449 * Comment or blank line
1456 status = process_geometry(&command);
1459 status = process_partition(&command);
1462 status = process_active(&command);
1475 read_config(config_file)
1482 while (1) /* dirty trick used to insure one exit point for this
1485 if (strcmp(config_file, "-") != 0)
1488 * We're not reading from stdin
1490 if ((fp = fopen(config_file, "r")) == NULL)
1500 current_line_number = 0;
1503 if (fgets(buf, sizeof(buf), fp) == NULL)
1507 ++current_line_number;
1508 status = process_line(buf);
1519 * It doesn't matter if we're reading from stdin, as we've reached EOF
1531 struct dos_partition *partp;
1534 for (i = 0; i < 4; ++i)
1536 partp = ((struct dos_partition *) &mboot.parts) + i;
1537 bzero((char *)partp, sizeof (struct dos_partition));