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>
33 #include <sys/param.h>
35 #include <sys/mount.h>
50 static char lbuf[LBUF];
56 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
58 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
59 * Copyright (c) 1989 Robert. V. Baron
63 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
65 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
67 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
68 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
69 static int secsize = 0; /* the sensed sector size */
73 static struct disklabel disklabel; /* disk parameters */
75 static int cyls, sectors, heads, cylsecs, disksecs;
78 unsigned char padding[2]; /* force the longs to be long aligned */
79 unsigned char *bootinst; /* boot code */
81 struct dos_partition parts[4];
84 static struct mboot mboot;
88 #define BOOT_MAGIC 0xAA55
91 static uint dos_heads;
92 static uint dos_sectors;
93 static uint dos_cylsecs;
95 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
96 #define DOSCYL(c) (c & 0xff)
100 static int current_line_number;
102 static int geom_processed = 0;
103 static int part_processed = 0;
104 static int active_processed = 0;
115 static int B_flag = 0; /* replace boot code */
116 static int I_flag = 0; /* use entire disk for FreeBSD */
117 static int a_flag = 0; /* set active partition */
118 static char *b_flag = NULL; /* path to boot code */
119 static int i_flag = 0; /* replace partition data */
120 static int u_flag = 0; /* update partition data */
121 static int s_flag = 0; /* Print a summary and exit */
122 static int t_flag = 0; /* test only, if f_flag is given */
123 static char *f_flag = NULL; /* Read config info from file */
124 static int v_flag = 0; /* Be verbose */
126 static struct part_type
132 ,{0x01, "Primary DOS with 12 bit FAT"}
133 ,{0x02, "XENIX / filesystem"}
134 ,{0x03, "XENIX /usr filesystem"}
135 ,{0x04, "Primary DOS with 16 bit FAT (< 32MB)"}
136 ,{0x05, "Extended DOS"}
137 ,{0x06, "Primary 'big' DOS (>= 32MB)"}
138 ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
139 ,{0x08, "AIX filesystem or SplitDrive"}
140 ,{0x09, "AIX boot partition or Coherent"}
141 ,{0x0A, "OS/2 Boot Manager, OPUS or Coherent swap"}
142 ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
143 ,{0x0C, "DOS or Windows 95 with 32 bit FAT (LBA)"}
144 ,{0x0E, "Primary 'big' DOS (>= 32MB, LBA)"}
145 ,{0x0F, "Extended DOS (LBA)"}
147 ,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
148 ,{0x12, "Compaq diagnostics"}
149 ,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
150 ,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
151 ,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
152 ,{0x18, "AST Windows swapfile"}
154 ,{0x3C, "PartitionMagic recovery"}
157 ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
158 ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
159 ,{0x43, "Linux native (sharing disk with DRDOS)"}
160 ,{0x4D, "QNX 4.2 Primary"}
161 ,{0x4E, "QNX 4.2 Secondary"}
162 ,{0x4F, "QNX 4.2 Tertiary"}
163 ,{0x50, "DM (disk manager)"}
164 ,{0x51, "DM6 Aux1 (or Novell)"}
165 ,{0x52, "CP/M or Microport SysV/AT"}
168 ,{0x55, "EZ-Drive (disk manager)"}
169 ,{0x56, "Golden Bow (disk manager)"}
170 ,{0x5c, "Priam Edisk (disk manager)"} /* according to S. Widlake */
172 ,{0x63, "System V/386 (such as ISC UNIX), GNU HURD or Mach"}
173 ,{0x64, "Novell Netware/286 2.xx"}
174 ,{0x65, "Novell Netware/386 3.xx"}
175 ,{0x70, "DiskSecure Multi-Boot"}
178 ,{0x78, "QNX4.x 2nd part"}
179 ,{0x79, "QNX4.x 3rd part"}
180 ,{0x80, "Minix until 1.4a"}
181 ,{0x81, "Minix since 1.4b, early Linux partition or Mitac disk manager"}
182 ,{0x82, "Linux swap or Solaris x86"}
183 ,{0x83, "Linux native"}
184 ,{0x84, "OS/2 hidden C: drive"}
185 ,{0x85, "Linux extended"}
186 ,{0x86, "NTFS volume set??"}
187 ,{0x87, "NTFS volume set??"}
188 ,{0x93, "Amoeba filesystem"}
189 ,{0x94, "Amoeba bad block table"}
191 ,{0xA0, "Suspend to Disk"}
192 ,{0xA5, "FreeBSD/NetBSD/386BSD"}
196 ,{0xB7, "BSDI BSD/386 filesystem"}
197 ,{0xB8, "BSDI BSD/386 swap"}
198 ,{0xC1, "DRDOS/sec with 12-bit FAT"}
199 ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
200 ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
202 ,{0xDB, "CP/M, Concurrent CP/M, Concurrent DOS or CTOS"}
203 ,{0xE1, "DOS access or SpeedStor with 12-bit FAT extended partition"}
204 ,{0xE3, "DOS R/O or SpeedStor"}
205 ,{0xE4, "SpeedStor with 16-bit FAT extended partition < 1024 cyl."}
206 ,{0xEB, "BeOS filesystem"}
208 ,{0xEF, "EFI System Partition"}
210 ,{0xF2, "DOS 3.3+ Secondary"}
211 ,{0xF4, "SpeedStor large partition"}
212 ,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
213 ,{0xFF, "Xenix bad blocks table"}
216 static void print_s0(int which);
217 static void print_part(int i);
218 static void init_sector0(unsigned long start);
219 static void init_boot(void);
220 static void change_part(int i);
221 static void print_params(void);
222 static void change_active(int which);
223 static void change_code(void);
224 static void get_params_to_use(void);
225 static char *get_rootdisk(void);
226 static void dos(struct dos_partition *partp);
227 static int open_disk(int flag);
228 static ssize_t read_disk(off_t sector, void *buf);
229 static ssize_t write_disk(off_t sector, void *buf);
230 static int get_params(void);
231 static int read_s0(void);
232 static int write_s0(void);
233 static int ok(const char *str);
234 static int decimal(const char *str, int *num, int deflt);
235 static const char *get_type(int type);
236 static int read_config(char *config_file);
237 static void reset_boot(void);
238 static int sanitize_partition(struct dos_partition *);
239 static void usage(void);
242 main(int argc, char *argv[])
247 struct dos_partition *partp;
249 while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
290 if (f_flag || i_flag)
298 disk = get_rootdisk();
300 if (stat(argv[0], &sb) == 0) {
301 /* OK, full pathname given */
303 } else if (errno == ENOENT) {
304 /* Try prepending "/dev" */
305 asprintf(&disk, "%s%s", _PATH_DEV, argv[0]);
307 errx(1, "out of memory");
309 /* other stat error, let it fail below */
313 if (open_disk(u_flag) < 0)
314 err(1, "cannot open disk %s", disk);
316 /* (abu)use mboot.bootinst to probe for the sector size */
317 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
318 err(1, "cannot allocate buffer to determine disk sector size");
319 read_disk(0, mboot.bootinst);
320 free(mboot.bootinst);
321 mboot.bootinst = NULL;
326 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
328 printf("Part %11s %11s Type Flags\n", "Start", "Size");
329 for (i = 0; i < NDOSPART; i++) {
330 partp = ((struct dos_partition *) &mboot.parts) + i;
331 if (partp->dp_start == 0 && partp->dp_size == 0)
333 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
334 (u_long) partp->dp_start,
335 (u_long) partp->dp_size, partp->dp_typ,
341 printf("******* Working on device %s *******\n",disk);
346 partp = (struct dos_partition *) (&mboot.parts[0]);
347 partp->dp_typ = DOSPTYP_386BSD;
348 partp->dp_flag = ACTIVE;
349 partp->dp_start = dos_sectors;
350 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
360 if (read_s0() || i_flag)
362 if (!read_config(f_flag))
375 init_sector0(dos_sectors);
377 printf("Media sector size is %d\n", secsize);
378 printf("Warning: BIOS sector numbering starts with sector 1\n");
379 printf("Information from DOS bootblock is:\n");
381 for (i = 1; i <= NDOSPART; i++)
384 change_part(partition);
386 if (u_flag || a_flag)
387 change_active(partition);
392 if (u_flag || a_flag || B_flag) {
394 printf("\nWe haven't changed the partition table yet. ");
395 printf("This is your last chance.\n");
399 if (ok("Should we write new partition table?"))
402 printf("\n-t flag specified -- partition table not written.\n");
413 fprintf(stderr, "%s%s",
414 "usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
415 " fdisk -f configfile [-itv] [disk]\n");
425 printf("Information from DOS bootblock is:\n");
427 for (i = 1; i <= NDOSPART; i++)
428 printf("%d: ", i), print_part(i);
433 static struct dos_partition mtpart;
438 struct dos_partition *partp;
441 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
443 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
444 printf("<UNUSED>\n");
448 * Be careful not to overflow.
450 part_mb = partp->dp_size;
452 part_mb /= (1024 * 1024);
453 printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
454 get_type(partp->dp_typ));
455 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
456 (u_long)partp->dp_start,
457 (u_long)partp->dp_size,
460 partp->dp_flag == ACTIVE ? " (active)" : "");
461 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
462 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
464 ,DPSECT(partp->dp_ssect)
465 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
467 ,DPSECT(partp->dp_esect));
478 fname = b_flag ? b_flag : "/boot/mbr";
479 if ((fdesc = open(fname, O_RDONLY)) == -1 ||
480 fstat(fdesc, &sb) == -1)
482 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
483 errx(1, "%s: length must be a multiple of sector size", fname);
484 if (mboot.bootinst != NULL)
485 free(mboot.bootinst);
486 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
487 errx(1, "%s: unable to allocate read buffer", fname);
488 if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
491 if (n != mboot.bootinst_size)
492 errx(1, "%s: short read", fname);
497 init_sector0(unsigned long start)
499 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
503 partp->dp_typ = DOSPTYP_386BSD;
504 partp->dp_flag = ACTIVE;
505 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
508 partp->dp_start = start;
509 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
517 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
519 printf("The data for partition %d is:\n", i);
522 if (u_flag && ok("Do you want to change it?")) {
526 bzero((char *)partp, sizeof (struct dos_partition));
529 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
535 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
536 Decimal("start", partp->dp_start, tmp);
537 Decimal("size", partp->dp_size, tmp);
538 if (!sanitize_partition(partp)) {
539 warnx("ERROR: failed to adjust; setting sysid to 0");
543 if (ok("Explicitly specify beg/end address ?"))
546 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
548 tsec = DPSECT(partp->dp_ssect);
549 Decimal("beginning cylinder", tcyl, tmp);
550 Decimal("beginning head", thd, tmp);
551 Decimal("beginning sector", tsec, tmp);
552 partp->dp_scyl = DOSCYL(tcyl);
553 partp->dp_ssect = DOSSECT(tsec,tcyl);
556 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
558 tsec = DPSECT(partp->dp_esect);
559 Decimal("ending cylinder", tcyl, tmp);
560 Decimal("ending head", thd, tmp);
561 Decimal("ending sector", tsec, tmp);
562 partp->dp_ecyl = DOSCYL(tcyl);
563 partp->dp_esect = DOSSECT(tsec,tcyl);
569 } while (!ok("Are we happy with this entry?"));
576 printf("parameters extracted from in-core disklabel are:\n");
577 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
578 ,cyls,heads,sectors,cylsecs);
579 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
580 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
581 printf("parameters to be used for BIOS calculations are:\n");
582 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
583 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
587 change_active(int which)
589 struct dos_partition *partp = &mboot.parts[0];
590 int active, i, new, tmp;
593 for (i = 0; i < NDOSPART; i++) {
594 if ((partp[i].dp_flag & ACTIVE) == 0)
596 printf("Partition %d is marked active\n", i + 1);
600 if (a_flag && which != -1)
602 else if (active == -1)
605 if (!ok("Do you want to change the active partition?"))
610 Decimal("active partition", new, tmp);
611 if (new < 1 || new > 4) {
612 printf("Active partition number must be in range 1-4."
617 } while (!ok("Are you happy with this choice"));
618 for (i = 0; i < NDOSPART; i++)
619 partp[i].dp_flag = 0;
620 if (active > 0 && active <= NDOSPART)
621 partp[active-1].dp_flag = ACTIVE;
627 if (ok("Do you want to change the boot code?"))
636 if (ok("Do you want to change our idea of what BIOS thinks ?"))
640 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
641 Decimal("BIOS's idea of #heads", dos_heads, tmp);
642 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
643 dos_cylsecs = dos_heads * dos_sectors;
646 while(!ok("Are you happy with this choice"));
651 /***********************************************\
652 * Change real numbers into strange dos numbers *
653 \***********************************************/
655 dos(struct dos_partition *partp)
660 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
661 memcpy(partp, &mtpart, sizeof(*partp));
666 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
667 cy = partp->dp_start / dos_cylsecs;
668 sec = partp->dp_start % dos_sectors + 1;
669 partp->dp_scyl = DOSCYL(cy);
670 partp->dp_ssect = DOSSECT(sec, cy);
673 end = partp->dp_start + partp->dp_size - 1;
674 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
675 cy = end / dos_cylsecs;
676 sec = end % dos_sectors + 1;
677 partp->dp_ecyl = DOSCYL(cy);
678 partp->dp_esect = DOSSECT(sec, cy);
686 if (stat(disk, &st) == -1) {
689 warnx("can't get file status of %s", disk);
692 if ( !(st.st_mode & S_IFCHR) )
693 warnx("device %s is not character special", disk);
695 a_flag || I_flag || B_flag || flag ? O_RDWR : O_RDONLY)) == -1) {
698 warnx("can't open device %s", disk);
701 if (get_params() == -1) {
702 warnx("can't get disk parameters on %s", disk);
709 read_disk(off_t sector, void *buf)
711 lseek(fd,(sector * 512), 0);
713 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
716 int size = read(fd, buf, secsize);
717 if( size == secsize )
718 /* it worked so return */
722 return read( fd, buf, secsize );
724 /* we failed to read at any of the sizes */
729 write_disk(off_t sector, void *buf)
731 lseek(fd,(sector * 512), 0);
732 /* write out in the size that the read_disk found worked */
733 return write(fd, buf, secsize);
740 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
741 warnx("can't get disk parameters on %s; supplying dummy ones", disk);
743 dos_heads = heads = 1;
744 dos_sectors = sectors = 1;
745 dos_cylsecs = cylsecs = heads * sectors;
746 disksecs = cyls * heads * sectors;
750 dos_cyls = cyls = disklabel.d_ncylinders;
751 dos_heads = heads = disklabel.d_ntracks;
752 dos_sectors = sectors = disklabel.d_nsectors;
753 dos_cylsecs = cylsecs = heads * sectors;
754 disksecs = cyls * heads * sectors;
763 mboot.bootinst_size = secsize;
764 if (mboot.bootinst != NULL)
765 free(mboot.bootinst);
766 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
767 warnx("unable to allocate buffer to read fdisk "
771 if (read_disk(0, mboot.bootinst) == -1) {
772 warnx("can't read fdisk partition table");
775 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
776 warnx("invalid fdisk partition table found");
777 /* So should we initialize things */
780 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
793 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
795 * write enable label sector before write (if necessary),
796 * disable after writing.
797 * needed if the disklabel protected area also protects
798 * sector 0. (e.g. empty disk)
800 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
801 if (write_disk(sector,
802 &mboot.bootinst[sector * secsize]) == -1) {
803 warn("can't write fdisk partition table");
813 printf("%s [n] ", str);
815 if (fgets(lbuf, LBUF, stdin) == NULL)
817 lbuf[strlen(lbuf)-1] = 0;
820 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
821 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
828 decimal(const char *str, int *num, int deflt)
834 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
836 if (fgets(lbuf, LBUF, stdin) == NULL)
838 lbuf[strlen(lbuf)-1] = 0;
844 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
847 while ((c = *cp++)) {
848 if (c <= '9' && c >= '0')
849 acc = acc * 10 + c - '0';
853 if (c == ' ' || c == '\t')
854 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
859 printf("%s is an invalid decimal number. Try again.\n",
868 int numentries = (sizeof(part_types)/sizeof(struct part_type));
870 struct part_type *ptr = part_types;
873 while(counter < numentries) {
874 if(ptr->type == type)
884 parse_config_line(char *line, CMD *command)
890 memset(command, 0, sizeof(*command));
892 while (isspace(*cp)) ++cp;
893 if (*cp == '\0' || *cp == '#')
895 command->cmd = *cp++;
901 while (isspace(*cp)) ++cp;
903 break; /* found comment */
905 command->args[command->n_args].argtype = *cp++;
907 break; /* assume end of line */
909 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
911 break; /* couldn't parse number */
921 process_geometry(CMD *command)
927 if (part_processed) {
929 "ERROR line %d: the geometry specification line must occur before\n\
930 all partition specifications",
931 current_line_number);
935 if (command->n_args != 3) {
936 warnx("ERROR line %d: incorrect number of geometry args",
937 current_line_number);
944 for (i = 0; i < 3; ++i) {
945 switch (command->args[i].argtype) {
947 dos_cyls = command->args[i].arg_val;
950 dos_heads = command->args[i].arg_val;
953 dos_sectors = command->args[i].arg_val;
957 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
958 current_line_number, command->args[i].argtype,
959 command->args[i].argtype);
967 dos_cylsecs = dos_heads * dos_sectors;
970 * Do sanity checks on parameter values
973 warnx("ERROR line %d: number of cylinders not specified",
974 current_line_number);
977 if (dos_cyls > 1024) {
979 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
980 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
981 is dedicated to FreeBSD)",
982 current_line_number, dos_cyls);
985 if (dos_heads == 0) {
986 warnx("ERROR line %d: number of heads not specified",
987 current_line_number);
989 } else if (dos_heads > 256) {
990 warnx("ERROR line %d: number of heads must be within (1-256)",
991 current_line_number);
995 if (dos_sectors == 0) {
996 warnx("ERROR line %d: number of sectors not specified",
997 current_line_number);
999 } else if (dos_sectors > 63) {
1000 warnx("ERROR line %d: number of sectors must be within (1-63)",
1001 current_line_number);
1012 process_partition(CMD *command)
1014 int status = 0, partition;
1015 u_int32_t prev_head_boundary, prev_cyl_boundary;
1016 u_int32_t adj_size, max_end;
1017 struct dos_partition *partp;
1021 if (command->n_args != 4) {
1022 warnx("ERROR line %d: incorrect number of partition args",
1023 current_line_number);
1026 partition = command->args[0].arg_val;
1027 if (partition < 1 || partition > 4) {
1028 warnx("ERROR line %d: invalid partition number %d",
1029 current_line_number, partition);
1032 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1033 bzero((char *)partp, sizeof (struct dos_partition));
1034 partp->dp_typ = command->args[1].arg_val;
1035 partp->dp_start = command->args[2].arg_val;
1036 partp->dp_size = command->args[3].arg_val;
1037 max_end = partp->dp_start + partp->dp_size;
1039 if (partp->dp_typ == 0) {
1041 * Get out, the partition is marked as unused.
1044 * Insure that it's unused.
1046 bzero((char *)partp, sizeof (struct dos_partition));
1052 * Adjust start upwards, if necessary, to fall on an head boundary.
1054 if (partp->dp_start % dos_sectors != 0) {
1055 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1056 if (max_end < dos_sectors ||
1057 prev_head_boundary > max_end - dos_sectors) {
1059 * Can't go past end of partition
1062 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1064 current_line_number, partition);
1068 "WARNING: adjusting start offset of partition %d\n\
1069 from %u to %u, to fall on a head boundary",
1070 partition, (u_int)partp->dp_start,
1071 (u_int)(prev_head_boundary + dos_sectors));
1072 partp->dp_start = prev_head_boundary + dos_sectors;
1076 * Adjust size downwards, if necessary, to fall on a cylinder
1080 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1081 if (prev_cyl_boundary > partp->dp_start)
1082 adj_size = prev_cyl_boundary - partp->dp_start;
1085 "ERROR: could not adjust partition to start on a head boundary\n\
1086 and end on a cylinder boundary.");
1089 if (adj_size != partp->dp_size) {
1091 "WARNING: adjusting size of partition %d from %u to %u\n\
1092 to end on a cylinder boundary",
1093 partition, (u_int)partp->dp_size, (u_int)adj_size);
1094 partp->dp_size = adj_size;
1096 if (partp->dp_size == 0) {
1097 warnx("ERROR line %d: size of partition %d is zero",
1098 current_line_number, partition);
1111 process_active(CMD *command)
1113 int status = 0, partition, i;
1114 struct dos_partition *partp;
1117 active_processed = 1;
1118 if (command->n_args != 1) {
1119 warnx("ERROR line %d: incorrect number of active args",
1120 current_line_number);
1124 partition = command->args[0].arg_val;
1125 if (partition < 1 || partition > 4) {
1126 warnx("ERROR line %d: invalid partition number %d",
1127 current_line_number, partition);
1131 * Reset active partition
1133 partp = ((struct dos_partition *) &mboot.parts);
1134 for (i = 0; i < NDOSPART; i++)
1135 partp[i].dp_flag = 0;
1136 partp[partition-1].dp_flag = ACTIVE;
1146 process_line(char *line)
1152 parse_config_line(line, &command);
1153 switch (command.cmd) {
1156 * Comment or blank line
1163 status = process_geometry(&command);
1166 status = process_partition(&command);
1169 status = process_active(&command);
1182 read_config(char *config_file)
1189 if (strcmp(config_file, "-") != 0) {
1191 * We're not reading from stdin
1193 if ((fp = fopen(config_file, "r")) == NULL) {
1200 current_line_number = 0;
1202 if (fgets(buf, sizeof(buf), fp) == NULL)
1204 ++current_line_number;
1205 status = process_line(buf);
1213 * It doesn't matter if we're reading from stdin, as we've reached EOF
1225 struct dos_partition *partp;
1228 for (i = 0; i < 4; ++i) {
1229 partp = ((struct dos_partition *) &mboot.parts) + i;
1230 bzero((char *)partp, sizeof (struct dos_partition));
1235 sanitize_partition(struct dos_partition *partp)
1237 u_int32_t prev_head_boundary, prev_cyl_boundary;
1238 u_int32_t max_end, size, start;
1240 start = partp->dp_start;
1241 size = partp->dp_size;
1242 max_end = start + size;
1243 /* Only allow a zero size if the partition is being marked unused. */
1245 if (start == 0 && partp->dp_typ == 0)
1247 warnx("ERROR: size of partition is zero");
1250 /* Return if no adjustment is necessary. */
1251 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1254 if (start % dos_sectors != 0)
1255 warnx("WARNING: partition does not start on a head boundary");
1256 if ((start +size) % dos_sectors != 0)
1257 warnx("WARNING: partition does not end on a cylinder boundary");
1258 warnx("WARNING: this may confuse the BIOS or some operating systems");
1259 if (!ok("Correct this automatically?"))
1263 * Adjust start upwards, if necessary, to fall on an head boundary.
1265 if (start % dos_sectors != 0) {
1266 prev_head_boundary = start / dos_sectors * dos_sectors;
1267 if (max_end < dos_sectors ||
1268 prev_head_boundary >= max_end - dos_sectors) {
1270 * Can't go past end of partition
1273 "ERROR: unable to adjust start of partition to fall on a head boundary");
1276 start = prev_head_boundary + dos_sectors;
1280 * Adjust size downwards, if necessary, to fall on a cylinder
1283 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1284 if (prev_cyl_boundary > start)
1285 size = prev_cyl_boundary - start;
1287 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1288 and end on a cylinder boundary.");
1292 /* Finally, commit any changes to partp and return. */
1293 if (start != partp->dp_start) {
1294 warnx("WARNING: adjusting start offset of partition to %u",
1296 partp->dp_start = start;
1298 if (size != partp->dp_size) {
1299 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1300 partp->dp_size = size;
1307 * Try figuring out the root device's canonical disk name.
1308 * The following choices are considered:
1309 * /dev/ad0s1a => /dev/ad0
1310 * /dev/da0a => /dev/da0
1311 * /dev/vinum/root => /dev/vinum/root
1316 struct statfs rootfs;
1319 regmatch_t rm[NMATCHES];
1323 if (statfs("/", &rootfs) == -1)
1324 err(1, "statfs(\"/\")");
1326 if ((rv = regcomp(&re, "^(/dev/.*)(\\d+(s\\d+)?[a-h])?$",
1327 REG_EXTENDED)) != 0)
1328 errx(1, "regcomp() failed (%d)", rv);
1329 if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
1331 "mounted root fs resource doesn't match expectations (regexec returned %d)",
1333 if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1334 errx(1, "out of memory");
1335 memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1336 rm[1].rm_eo - rm[1].rm_so);
1337 s[rm[1].rm_eo - rm[1].rm_so] = 0;