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
64 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
65 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
67 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
69 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
70 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
71 int secsize = 0; /* the sensed sector size */
75 struct disklabel disklabel; /* disk parameters */
77 int cyls, sectors, heads, cylsecs, disksecs;
81 unsigned char padding[2]; /* force the longs to be long aligned */
82 unsigned char *bootinst; /* boot code */
84 struct dos_partition parts[4];
86 struct mboot mboot = {{0}, NULL, 0};
89 #define BOOT_MAGIC 0xAA55
96 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
97 #define DOSCYL(c) (c & 0xff)
98 static int partition = -1;
103 static int current_line_number;
105 static int geom_processed = 0;
106 static int part_processed = 0;
107 static int active_processed = 0;
120 static int B_flag = 0; /* replace boot code */
121 static int I_flag = 0; /* use entire disk for FreeBSD */
122 static int a_flag = 0; /* set active partition */
123 static char *b_flag = NULL; /* path to boot code */
124 static int i_flag = 0; /* replace partition data */
125 static int u_flag = 0; /* update partition data */
126 static int s_flag = 0; /* Print a summary and exit */
127 static int t_flag = 0; /* test only, if f_flag is given */
128 static char *f_flag = NULL; /* Read config info from file */
129 static int v_flag = 0; /* Be verbose */
138 ,{0x01, "Primary DOS with 12 bit FAT"}
139 ,{0x02, "XENIX / filesystem"}
140 ,{0x03, "XENIX /usr filesystem"}
141 ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
142 ,{0x05, "Extended DOS"}
143 ,{0x06, "Primary 'big' DOS (> 32MB)"}
144 ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
145 ,{0x08, "AIX filesystem"}
146 ,{0x09, "AIX boot partition or Coherent"}
147 ,{0x0A, "OS/2 Boot Manager or OPUS"}
148 ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
149 ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
150 ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
151 ,{0x0F, "Extended DOS, LBA"}
155 ,{0x4D, "QNX 4.2 Primary"}
156 ,{0x4E, "QNX 4.2 Secondary"}
157 ,{0x4F, "QNX 4.2 Tertiary"}
160 ,{0x52, "CP/M or Microport SysV/AT"}
163 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
164 ,{0x64, "Novell Netware 2.xx"}
165 ,{0x65, "Novell Netware 3.xx"}
167 ,{0x80, "Minix 1.1 ... 1.4a"}
168 ,{0x81, "Minix 1.4b ... 1.5.10"}
169 ,{0x82, "Linux swap or Solaris x86"}
170 ,{0x83, "Linux filesystem"}
171 ,{0x93, "Amoeba filesystem"}
172 ,{0x94, "Amoeba bad block table"}
174 ,{0xA0, "Suspend to Disk"}
175 ,{0xA5, "FreeBSD/NetBSD/386BSD"}
179 ,{0xB7, "BSDI BSD/386 filesystem"}
180 ,{0xB8, "BSDI BSD/386 swap"}
181 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
186 ,{0xEF, "EFI System Partition"}
188 ,{0xF2, "DOS 3.3+ Secondary"}
190 ,{0xFF, "BBT (Bad Blocks Table)"}
193 static void print_s0(int which);
194 static void print_part(int i);
195 static void init_sector0(unsigned long start);
196 static void init_boot(void);
197 static void change_part(int i);
198 static void print_params();
199 static void change_active(int which);
200 static void change_code();
201 static void get_params_to_use();
202 static char *get_rootdisk(void);
203 static void dos(struct dos_partition *partp);
204 static int open_disk(int u_flag);
205 static ssize_t read_disk(off_t sector, void *buf);
206 static ssize_t write_disk(off_t sector, void *buf);
207 static int get_params();
208 static int read_s0();
209 static int write_s0();
210 static int ok(char *str);
211 static int decimal(char *str, int *num, int deflt);
212 static char *get_type(int type);
213 static int read_config(char *config_file);
214 static void reset_boot(void);
215 static int sanitize_partition(struct dos_partition *);
216 static void usage(void);
218 static int hex(char *str, int *num, int deflt);
219 static int string(char *str, char **ans);
224 main(int argc, char *argv[])
229 while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
270 if (f_flag || i_flag)
278 disk = get_rootdisk();
280 if (stat(argv[0], &sb) == 0) {
281 /* OK, full pathname given */
283 } else if (errno == ENOENT) {
284 /* Try prepending "/dev" */
285 if ((disk = malloc(strlen(argv[0]) + strlen(_PATH_DEV) +
287 errx(1, "out of memory");
288 strcpy(disk, _PATH_DEV);
289 strcat(disk, argv[0]);
291 /* other stat error, let it fail below */
295 if (open_disk(u_flag) < 0)
296 err(1, "cannot open disk %s", disk);
298 /* (abu)use mboot.bootinst to probe for the sector size */
299 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
300 err(1, "cannot allocate buffer to determine disk sector size");
301 read_disk(0, mboot.bootinst);
302 free(mboot.bootinst);
303 mboot.bootinst = NULL;
308 struct dos_partition *partp;
312 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
314 printf("Part %11s %11s Type Flags\n", "Start", "Size");
315 for (i = 0; i < NDOSPART; i++) {
316 partp = ((struct dos_partition *) &mboot.parts) + i;
317 if (partp->dp_start == 0 && partp->dp_size == 0)
319 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
320 (u_long) partp->dp_start,
321 (u_long) partp->dp_size, partp->dp_typ,
327 printf("******* Working on device %s *******\n",disk);
331 struct dos_partition *partp;
335 partp = (struct dos_partition *) (&mboot.parts[0]);
336 partp->dp_typ = DOSPTYP_386BSD;
337 partp->dp_flag = ACTIVE;
338 partp->dp_start = dos_sectors;
339 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
350 if (read_s0() || i_flag)
355 if (!read_config(f_flag))
380 init_sector0(dos_sectors);
382 printf("Media sector size is %d\n", secsize);
383 printf("Warning: BIOS sector numbering starts with sector 1\n");
384 printf("Information from DOS bootblock is:\n");
386 for (i = 1; i <= NDOSPART; i++)
389 change_part(partition);
391 if (u_flag || a_flag)
392 change_active(partition);
397 if (u_flag || a_flag || B_flag) {
400 printf("\nWe haven't changed the partition table yet. ");
401 printf("This is your last chance.\n");
406 if (ok("Should we write new partition table?"))
411 printf("\n-t flag specified -- partition table not written.\n");
422 fprintf(stderr, "%s%s",
423 "usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
424 " fdisk -f configfile [-itv] [disk]\n");
434 printf("Information from DOS bootblock is:\n");
436 for (i = 1; i <= NDOSPART; i++)
437 printf("%d: ", i), print_part(i);
442 static struct dos_partition mtpart = { 0 };
447 struct dos_partition *partp;
450 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
452 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
453 printf("<UNUSED>\n");
457 * Be careful not to overflow.
459 part_mb = partp->dp_size;
461 part_mb /= (1024 * 1024);
462 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
463 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
464 (u_long)partp->dp_start,
465 (u_long)partp->dp_size,
468 partp->dp_flag == ACTIVE ? " (active)" : "");
469 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
470 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
472 ,DPSECT(partp->dp_ssect)
473 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
475 ,DPSECT(partp->dp_esect));
486 fname = b_flag ? b_flag : "/boot/mbr";
487 if ((fd = open(fname, O_RDONLY)) == -1 ||
488 fstat(fd, &sb) == -1)
490 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
491 errx(1, "%s: length must be a multiple of sector size", fname);
492 if (mboot.bootinst != NULL)
493 free(mboot.bootinst);
494 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
495 errx(1, "%s: unable to allocate read buffer", fname);
496 if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
499 if (n != mboot.bootinst_size)
500 errx(1, "%s: short read", fname);
505 init_sector0(unsigned long start)
507 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
511 partp->dp_typ = DOSPTYP_386BSD;
512 partp->dp_flag = ACTIVE;
513 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
516 partp->dp_start = start;
517 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
525 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
527 printf("The data for partition %d is:\n", i);
530 if (u_flag && ok("Do you want to change it?")) {
534 bzero((char *)partp, sizeof (struct dos_partition));
537 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
543 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
544 Decimal("start", partp->dp_start, tmp);
545 Decimal("size", partp->dp_size, tmp);
546 if (!sanitize_partition(partp)) {
547 warnx("ERROR: failed to adjust; setting sysid to 0");
551 if (ok("Explicitly specify beg/end address ?"))
554 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
556 tsec = DPSECT(partp->dp_ssect);
557 Decimal("beginning cylinder", tcyl, tmp);
558 Decimal("beginning head", thd, tmp);
559 Decimal("beginning sector", tsec, tmp);
560 partp->dp_scyl = DOSCYL(tcyl);
561 partp->dp_ssect = DOSSECT(tsec,tcyl);
564 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
566 tsec = DPSECT(partp->dp_esect);
567 Decimal("ending cylinder", tcyl, tmp);
568 Decimal("ending head", thd, tmp);
569 Decimal("ending sector", tsec, tmp);
570 partp->dp_ecyl = DOSCYL(tcyl);
571 partp->dp_esect = DOSSECT(tsec,tcyl);
577 } while (!ok("Are we happy with this entry?"));
584 printf("parameters extracted from in-core disklabel are:\n");
585 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
586 ,cyls,heads,sectors,cylsecs);
587 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
588 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
589 printf("parameters to be used for BIOS calculations are:\n");
590 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
591 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
595 change_active(int which)
597 struct dos_partition *partp = &mboot.parts[0];
598 int active, i, new, tmp;
601 for (i = 0; i < NDOSPART; i++) {
602 if ((partp[i].dp_flag & ACTIVE) == 0)
604 printf("Partition %d is marked active\n", i + 1);
608 if (a_flag && which != -1)
610 else if (active == -1)
613 if (!ok("Do you want to change the active partition?"))
618 Decimal("active partition", new, tmp);
619 if (new < 1 || new > 4) {
620 printf("Active partition number must be in range 1-4."
625 } while (!ok("Are you happy with this choice"));
626 for (i = 0; i < NDOSPART; i++)
627 partp[i].dp_flag = 0;
628 if (active > 0 && active <= NDOSPART)
629 partp[active-1].dp_flag = ACTIVE;
635 if (ok("Do you want to change the boot code?"))
644 if (ok("Do you want to change our idea of what BIOS thinks ?"))
648 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
649 Decimal("BIOS's idea of #heads", dos_heads, tmp);
650 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
651 dos_cylsecs = dos_heads * dos_sectors;
654 while(!ok("Are you happy with this choice"));
659 /***********************************************\
660 * Change real numbers into strange dos numbers *
661 \***********************************************/
664 struct dos_partition *partp;
669 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
670 memcpy(partp, &mtpart, sizeof(*partp));
675 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
676 cy = partp->dp_start / dos_cylsecs;
677 sec = partp->dp_start % dos_sectors + 1;
678 partp->dp_scyl = DOSCYL(cy);
679 partp->dp_ssect = DOSSECT(sec, cy);
682 end = partp->dp_start + partp->dp_size - 1;
683 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
684 cy = end / dos_cylsecs;
685 sec = end % dos_sectors + 1;
686 partp->dp_ecyl = DOSCYL(cy);
687 partp->dp_esect = DOSSECT(sec, cy);
692 /* Getting device status */
695 open_disk(int u_flag)
699 if (stat(disk, &st) == -1) {
702 warnx("can't get file status of %s", disk);
705 if ( !(st.st_mode & S_IFCHR) )
706 warnx("device %s is not character special", disk);
708 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
711 warnx("can't open device %s", disk);
714 if (get_params(0) == -1) {
715 warnx("can't get disk parameters on %s", disk);
722 read_disk(off_t sector, void *buf)
724 lseek(fd,(sector * 512), 0);
726 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
729 int size = read(fd, buf, secsize);
730 if( size == secsize )
731 /* it worked so return */
735 return read( fd, buf, secsize );
737 /* we failed to read at any of the sizes */
742 write_disk(off_t sector, void *buf)
744 lseek(fd,(sector * 512), 0);
745 /* write out in the size that the read_disk found worked */
746 return write(fd, buf, secsize);
753 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
754 warnx("can't get disk parameters on %s; supplying dummy ones", disk);
756 dos_heads = heads = 1;
757 dos_sectors = sectors = 1;
758 dos_cylsecs = cylsecs = heads * sectors;
759 disksecs = cyls * heads * sectors;
763 dos_cyls = cyls = disklabel.d_ncylinders;
764 dos_heads = heads = disklabel.d_ntracks;
765 dos_sectors = sectors = disklabel.d_nsectors;
766 dos_cylsecs = cylsecs = heads * sectors;
767 disksecs = cyls * heads * sectors;
776 mboot.bootinst_size = secsize;
777 if (mboot.bootinst != NULL)
778 free(mboot.bootinst);
779 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
780 warnx("unable to allocate buffer to read fdisk "
784 if (read_disk(0, mboot.bootinst) == -1) {
785 warnx("can't read fdisk partition table");
788 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
789 warnx("invalid fdisk partition table found");
790 /* So should we initialize things */
793 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
809 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
811 * write enable label sector before write (if necessary),
812 * disable after writing.
813 * needed if the disklabel protected area also protects
814 * sector 0. (e.g. empty disk)
818 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
819 warn("ioctl DIOCWLABEL");
821 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
822 if (write_disk(sector,
823 &mboot.bootinst[sector * secsize]) == -1) {
824 warn("can't write fdisk partition table");
828 (void) ioctl(fd, DIOCWLABEL, &flag);
833 (void) ioctl(fd, DIOCWLABEL, &flag);
843 printf("%s [n] ", str);
845 if (fgets(lbuf, LBUF, stdin) == NULL)
847 lbuf[strlen(lbuf)-1] = 0;
850 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
851 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
858 decimal(char *str, int *num, int deflt)
864 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
866 if (fgets(lbuf, LBUF, stdin) == NULL)
868 lbuf[strlen(lbuf)-1] = 0;
874 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
877 while ((c = *cp++)) {
878 if (c <= '9' && c >= '0')
879 acc = acc * 10 + c - '0';
883 if (c == ' ' || c == '\t')
884 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
889 printf("%s is an invalid decimal number. Try again.\n",
897 hex(char *str, int *num, int deflt)
903 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
904 fgets(lbuf, LBUF, stdin);
905 lbuf[strlen(lbuf)-1] = 0;
911 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
914 while ((c = *cp++)) {
915 if (c <= '9' && c >= '0')
916 acc = (acc << 4) + c - '0';
917 else if (c <= 'f' && c >= 'a')
918 acc = (acc << 4) + c - 'a' + 10;
919 else if (c <= 'F' && c >= 'A')
920 acc = (acc << 4) + c - 'A' + 10;
924 if (c == ' ' || c == '\t')
925 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
930 printf("%s is an invalid hex number. Try again.\n",
937 string(char *str, char **ans)
943 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
944 fgets(lbuf, LBUF, stdin);
945 lbuf[strlen(lbuf)-1] = 0;
950 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
954 while ((c = *cp) && c != '"') cp++;
957 while ((c = *cp) && c != ' ' && c != '\t') cp++;
970 int numentries = (sizeof(part_types)/sizeof(struct part_type));
972 struct part_type *ptr = part_types;
975 while(counter < numentries)
977 if(ptr->type == type)
989 parse_config_line(line, command)
996 while (1) /* dirty trick used to insure one exit point for this
999 memset(command, 0, sizeof(*command));
1001 while (isspace(*cp)) ++cp;
1002 if (*cp == '\0' || *cp == '#')
1006 command->cmd = *cp++;
1013 while (isspace(*cp)) ++cp;
1016 break; /* found comment */
1020 command->args[command->n_args].argtype = *cp++;
1024 break; /* assume end of line */
1027 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1030 break; /* couldn't parse number */
1041 process_geometry(command)
1052 "ERROR line %d: the geometry specification line must occur before\n\
1053 all partition specifications",
1054 current_line_number);
1058 if (command->n_args != 3)
1060 warnx("ERROR line %d: incorrect number of geometry args",
1061 current_line_number);
1068 for (i = 0; i < 3; ++i)
1070 switch (command->args[i].argtype)
1073 dos_cyls = command->args[i].arg_val;
1076 dos_heads = command->args[i].arg_val;
1079 dos_sectors = command->args[i].arg_val;
1083 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1084 current_line_number, command->args[i].argtype,
1085 command->args[i].argtype);
1095 dos_cylsecs = dos_heads * dos_sectors;
1098 * Do sanity checks on parameter values
1102 warnx("ERROR line %d: number of cylinders not specified",
1103 current_line_number);
1106 if (dos_cyls == 0 || dos_cyls > 1024)
1109 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1110 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1111 is dedicated to FreeBSD)",
1112 current_line_number, dos_cyls);
1117 warnx("ERROR line %d: number of heads not specified",
1118 current_line_number);
1121 else if (dos_heads < 1 || dos_heads > 256)
1123 warnx("ERROR line %d: number of heads must be within (1-256)",
1124 current_line_number);
1128 if (dos_sectors < 0)
1130 warnx("ERROR line %d: number of sectors not specified",
1131 current_line_number);
1134 else if (dos_sectors < 1 || dos_sectors > 63)
1136 warnx("ERROR line %d: number of sectors must be within (1-63)",
1137 current_line_number);
1148 process_partition(command)
1151 int status = 0, partition;
1152 u_int32_t prev_head_boundary, prev_cyl_boundary;
1153 u_int32_t adj_size, max_end;
1154 struct dos_partition *partp;
1159 if (command->n_args != 4)
1161 warnx("ERROR line %d: incorrect number of partition args",
1162 current_line_number);
1165 partition = command->args[0].arg_val;
1166 if (partition < 1 || partition > 4)
1168 warnx("ERROR line %d: invalid partition number %d",
1169 current_line_number, partition);
1172 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1173 bzero((char *)partp, sizeof (struct dos_partition));
1174 partp->dp_typ = command->args[1].arg_val;
1175 partp->dp_start = command->args[2].arg_val;
1176 partp->dp_size = command->args[3].arg_val;
1177 max_end = partp->dp_start + partp->dp_size;
1179 if (partp->dp_typ == 0)
1182 * Get out, the partition is marked as unused.
1185 * Insure that it's unused.
1187 bzero((char *)partp, sizeof (struct dos_partition));
1193 * Adjust start upwards, if necessary, to fall on an head boundary.
1195 if (partp->dp_start % dos_sectors != 0)
1197 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1198 if (max_end < dos_sectors ||
1199 prev_head_boundary > max_end - dos_sectors)
1202 * Can't go past end of partition
1205 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1207 current_line_number, partition);
1211 "WARNING: adjusting start offset of partition %d\n\
1212 from %u to %u, to fall on a head boundary",
1213 partition, (u_int)partp->dp_start,
1214 (u_int)(prev_head_boundary + dos_sectors));
1215 partp->dp_start = prev_head_boundary + dos_sectors;
1219 * Adjust size downwards, if necessary, to fall on a cylinder
1223 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1224 if (prev_cyl_boundary > partp->dp_start)
1225 adj_size = prev_cyl_boundary - partp->dp_start;
1229 "ERROR: could not adjust partition to start on a head boundary\n\
1230 and end on a cylinder boundary.");
1233 if (adj_size != partp->dp_size)
1236 "WARNING: adjusting size of partition %d from %u to %u\n\
1237 to end on a cylinder boundary",
1238 partition, (u_int)partp->dp_size, (u_int)adj_size);
1239 partp->dp_size = adj_size;
1241 if (partp->dp_size == 0)
1243 warnx("ERROR line %d: size of partition %d is zero",
1244 current_line_number, partition);
1257 process_active(command)
1260 int status = 0, partition, i;
1261 struct dos_partition *partp;
1265 active_processed = 1;
1266 if (command->n_args != 1)
1268 warnx("ERROR line %d: incorrect number of active args",
1269 current_line_number);
1273 partition = command->args[0].arg_val;
1274 if (partition < 1 || partition > 4)
1276 warnx("ERROR line %d: invalid partition number %d",
1277 current_line_number, partition);
1281 * Reset active partition
1283 partp = ((struct dos_partition *) &mboot.parts);
1284 for (i = 0; i < NDOSPART; i++)
1285 partp[i].dp_flag = 0;
1286 partp[partition-1].dp_flag = ACTIVE;
1304 parse_config_line(line, &command);
1305 switch (command.cmd)
1309 * Comment or blank line
1316 status = process_geometry(&command);
1319 status = process_partition(&command);
1322 status = process_active(&command);
1335 read_config(config_file)
1342 while (1) /* dirty trick used to insure one exit point for this
1345 if (strcmp(config_file, "-") != 0)
1348 * We're not reading from stdin
1350 if ((fp = fopen(config_file, "r")) == NULL)
1360 current_line_number = 0;
1363 if (fgets(buf, sizeof(buf), fp) == NULL)
1367 ++current_line_number;
1368 status = process_line(buf);
1379 * It doesn't matter if we're reading from stdin, as we've reached EOF
1391 struct dos_partition *partp;
1394 for (i = 0; i < 4; ++i)
1396 partp = ((struct dos_partition *) &mboot.parts) + i;
1397 bzero((char *)partp, sizeof (struct dos_partition));
1402 sanitize_partition(partp)
1403 struct dos_partition *partp;
1405 u_int32_t prev_head_boundary, prev_cyl_boundary;
1406 u_int32_t max_end, size, start;
1408 start = partp->dp_start;
1409 size = partp->dp_size;
1410 max_end = start + size;
1411 /* Only allow a zero size if the partition is being marked unused. */
1413 if (start == 0 && partp->dp_typ == 0)
1415 warnx("ERROR: size of partition is zero");
1418 /* Return if no adjustment is necessary. */
1419 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1422 if (start % dos_sectors != 0)
1423 warnx("WARNING: partition does not start on a head boundary");
1424 if ((start +size) % dos_sectors != 0)
1425 warnx("WARNING: partition does not end on a cylinder boundary");
1426 warnx("WARNING: this may confuse the BIOS or some operating systems");
1427 if (!ok("Correct this automatically?"))
1431 * Adjust start upwards, if necessary, to fall on an head boundary.
1433 if (start % dos_sectors != 0) {
1434 prev_head_boundary = start / dos_sectors * dos_sectors;
1435 if (max_end < dos_sectors ||
1436 prev_head_boundary >= max_end - dos_sectors) {
1438 * Can't go past end of partition
1441 "ERROR: unable to adjust start of partition to fall on a head boundary");
1444 start = prev_head_boundary + dos_sectors;
1448 * Adjust size downwards, if necessary, to fall on a cylinder
1451 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1452 if (prev_cyl_boundary > start)
1453 size = prev_cyl_boundary - start;
1455 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1456 and end on a cylinder boundary.");
1460 /* Finally, commit any changes to partp and return. */
1461 if (start != partp->dp_start) {
1462 warnx("WARNING: adjusting start offset of partition to %u",
1464 partp->dp_start = start;
1466 if (size != partp->dp_size) {
1467 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1468 partp->dp_size = size;
1475 * Try figuring out the root device's canonical disk name.
1476 * The following choices are considered:
1477 * /dev/ad0s1a => /dev/ad0
1478 * /dev/da0a => /dev/da0
1479 * /dev/vinum/root => /dev/vinum/root
1484 struct statfs rootfs;
1487 regmatch_t rm[NMATCHES];
1491 if (statfs("/", &rootfs) == -1)
1492 err(1, "statfs(\"/\")");
1494 if ((rv = regcomp(&re, "^(/dev/.*)(\\d+(s\\d+)?[a-h])?$",
1495 REG_EXTENDED)) != 0)
1496 errx(1, "regcomp() failed (%d)", rv);
1497 if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
1499 "mounted root fs resource doesn't match expectations (regexec returned %d)",
1501 if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1502 errx(1, "out of memory");
1503 memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1504 rm[1].rm_eo - rm[1].rm_so);
1505 s[rm[1].rm_eo - rm[1].rm_so] = 0;