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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/disklabel.h>
32 #include <sys/diskpc98.h>
33 #include <sys/param.h>
35 #include <sys/mount.h>
52 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 String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
66 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
68 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
69 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
70 static int secsize = 0; /* the sensed sector size */
74 static int cyls, sectors, heads, cylsecs, disksecs;
77 unsigned char padding[2]; /* force the longs to be long aligned */
78 unsigned char bootinst[510];
79 unsigned short int signature;
80 struct pc98_partition parts[8];
81 unsigned char large_sector_overflow[MAX_SEC_SIZE-MIN_SEC_SIZE];
84 static struct mboot mboot;
90 static uint dos_heads;
91 static uint dos_sectors;
92 static uint dos_cylsecs;
105 static int B_flag = 0; /* replace boot code */
106 static int a_flag = 0; /* set active partition */
107 static int i_flag = 0; /* replace partition data */
108 static int u_flag = 0; /* update partition data */
109 static int s_flag = 0; /* Print a summary and exit */
110 static int t_flag = 0; /* test only */
111 static char *f_flag = NULL; /* Read config info from file */
112 static int v_flag = 0; /* Be verbose */
114 static struct part_type
120 ,{0x01, "Primary DOS with 12 bit FAT"}
126 ,{0x02, "XENIX / file system"}
127 ,{0x03, "XENIX /usr file system"}
129 ,{0x05, "Extended DOS"}
130 ,{0x06, "Primary 'big' DOS (> 32MB)"}
131 ,{0x07, "OS/2 HPFS, QNX or Advanced UNIX"}
132 ,{0x08, "AIX file system"}
133 ,{0x09, "AIX boot partition or Coherent"}
134 ,{0x0A, "OS/2 Boot Manager or OPUS"}
136 ,{0x14, "FreeBSD/NetBSD/386BSD"}
137 ,{0x94, "FreeBSD/NetBSD/386BSD"}
141 ,{0x52, "CP/M or Microport SysV/AT"}
144 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
145 ,{0x64, "Novell Netware 2.xx"}
146 ,{0x65, "Novell Netware 3.xx"}
151 static void print_s0(int which);
152 static void print_part(int i);
153 static void init_sector0(unsigned long start);
154 static void init_boot(void);
155 static void change_part(int i);
156 static void print_params(void);
157 static void change_active(int which);
158 static void change_code(void);
159 static void get_params_to_use(void);
160 static char *get_rootdisk(void);
161 static void dos(u_int32_t start, u_int32_t size, struct pc98_partition *partp);
162 static int open_disk(int flag);
163 static ssize_t read_disk(off_t sector, void *buf);
164 static int write_disk(off_t sector, void *buf);
165 static int get_params(void);
166 static int read_s0(void);
167 static int write_s0(void);
168 static int ok(const char *str);
169 static int decimal(const char *str, int *num, int deflt);
170 static const char *get_type(int type);
171 static void usage(void);
172 static int string(const char *str, char **ans);
175 main(int argc, char *argv[])
180 struct pc98_partition *partp;
182 while ((c = getopt(argc, argv, "Ba:f:istuv12345678")) != -1)
221 if (f_flag || i_flag)
229 disk = get_rootdisk();
231 if (stat(argv[0], &sb) == 0) {
232 /* OK, full pathname given */
234 } else if (errno == ENOENT && argv[0][0] != '/') {
235 /* Try prepending "/dev" */
236 asprintf(&disk, "%s%s", _PATH_DEV, argv[0]);
238 errx(1, "out of memory");
240 /* other stat error, let it fail below */
244 if (open_disk(u_flag) < 0)
245 err(1, "cannot open disk %s", disk);
250 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
252 printf("Part %11s %11s SID\n", "Start", "Size");
253 for (i = 0; i < NDOSPART; i++) {
254 partp = ((struct pc98_partition *) &mboot.parts) + i;
255 if (partp->dp_sid == 0)
257 printf("%4d: %11u %11u 0x%02x\n", i + 1,
258 partp->dp_scyl * cylsecs,
259 (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs,
265 printf("******* Working on device %s *******\n",disk);
279 init_sector0(dos_sectors);
281 printf("Media sector size is %d\n", secsize);
282 printf("Warning: BIOS sector numbering starts with sector 1\n");
283 printf("Information from DOS bootblock is:\n");
285 for (i = 1; i <= NDOSPART; i++)
288 change_part(partition);
290 if (u_flag || a_flag)
291 change_active(partition);
296 if (u_flag || a_flag || B_flag) {
298 printf("\nWe haven't changed the partition table yet. ");
299 printf("This is your last chance.\n");
303 if (ok("Should we write new partition table?"))
306 printf("\n-t flag specified -- partition table not written.\n");
317 fprintf(stderr, "%s%s",
318 "usage: fdisk [-Baistu] [-12345678] [disk]\n",
319 " fdisk -f configfile [-itv] [disk]\n");
329 printf("Information from DOS bootblock is:\n");
331 for (i = 1; i <= NDOSPART; i++)
332 printf("%d: ", i), print_part(i);
337 static struct pc98_partition mtpart;
342 struct pc98_partition *partp;
343 u_int64_t part_sz, part_mb;
345 partp = ((struct pc98_partition *) &mboot.parts) + i - 1;
347 if (!bcmp(partp, &mtpart, sizeof (struct pc98_partition))) {
348 printf("<UNUSED>\n");
352 * Be careful not to overflow.
354 part_sz = (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs;
355 part_mb = part_sz * secsize;
356 part_mb /= (1024 * 1024);
357 printf("sysmid %d (%#04x),(%s)\n", partp->dp_mid, partp->dp_mid,
358 get_type(partp->dp_mid));
359 printf(" start %lu, size %lu (%ju Meg), sid %d\n",
360 (u_long)(partp->dp_scyl * cylsecs), (u_long)part_sz,
361 (uintmax_t)part_mb, partp->dp_sid);
362 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
369 printf ("\tsystem Name %.16s\n", partp->dp_name);
377 mboot.signature = DOSMAGIC;
382 init_sector0(unsigned long start)
384 struct pc98_partition *partp =
385 (struct pc98_partition *)(&mboot.parts[0]);
389 partp->dp_mid = DOSMID_386BSD;
390 partp->dp_sid = DOSSID_386BSD;
392 dos(start, disksecs - start, partp);
398 struct pc98_partition *partp =
399 ((struct pc98_partition *) &mboot.parts) + i - 1;
401 printf("The data for partition %d is:\n", i);
404 if (u_flag && ok("Do you want to change it?")) {
408 bzero((char *)partp, sizeof (struct pc98_partition));
411 printf("\nThe static data for the slice 1 has been reinitialized to:\n");
417 int x_start = partp->dp_scyl * cylsecs ;
418 int x_size = (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs;
419 Decimal("sysmid", partp->dp_mid, tmp);
420 Decimal("syssid", partp->dp_sid, tmp);
421 String ("system name", partp->dp_name, 16);
422 Decimal("start", x_start, tmp);
423 Decimal("size", x_size, tmp);
425 if (ok("Explicitly specify beg/end address ?"))
428 tcyl = partp->dp_scyl;
430 tsec = partp->dp_ssect;
431 Decimal("beginning cylinder", tcyl, tmp);
432 Decimal("beginning head", thd, tmp);
433 Decimal("beginning sector", tsec, tmp);
434 partp->dp_scyl = tcyl;
435 partp->dp_ssect = tsec;
437 partp->dp_ipl_cyl = partp->dp_scyl;
438 partp->dp_ipl_sct = partp->dp_ssect;
439 partp->dp_ipl_head = partp->dp_shd;
441 tcyl = partp->dp_ecyl;
443 tsec = partp->dp_esect;
444 Decimal("ending cylinder", tcyl, tmp);
445 Decimal("ending head", thd, tmp);
446 Decimal("ending sector", tsec, tmp);
447 partp->dp_ecyl = tcyl;
448 partp->dp_esect = tsec;
451 dos(x_start, x_size, partp);
454 } while (!ok("Are we happy with this entry?"));
461 printf("parameters extracted from in-core disklabel are:\n");
462 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
463 ,cyls,heads,sectors,cylsecs);
464 if (dos_cyls > 65535 || dos_heads > 255 || dos_sectors > 255)
465 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
466 printf("parameters to be used for BIOS calculations are:\n");
467 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
468 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
472 change_active(int which)
474 struct pc98_partition *partp = &mboot.parts[0];
475 int active, i, new, tmp;
478 for (i = 0; i < NDOSPART; i++) {
479 if ((partp[i].dp_sid & ACTIVE) == 0)
481 printf("Partition %d is marked active\n", i + 1);
485 if (a_flag && which != -1)
487 else if (active == -1)
490 if (!ok("Do you want to change the active partition?"))
495 Decimal("active partition", new, tmp);
496 if (new < 1 || new > 8) {
497 printf("Active partition number must be in range 1-8."
502 } while (!ok("Are you happy with this choice"));
503 if (active > 0 && active <= 8)
504 partp[active-1].dp_sid |= ACTIVE;
510 if (ok("Do you want to change the boot code?"))
519 if (ok("Do you want to change our idea of what BIOS thinks ?"))
523 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
524 Decimal("BIOS's idea of #heads", dos_heads, tmp);
525 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
526 dos_cylsecs = dos_heads * dos_sectors;
529 while(!ok("Are you happy with this choice"));
534 /***********************************************\
535 * Change real numbers into strange dos numbers *
536 \***********************************************/
538 dos(u_int32_t start, u_int32_t size, struct pc98_partition *partp)
542 if (partp->dp_mid == 0 && partp->dp_sid == 0 &&
543 start == 0 && size == 0) {
544 memcpy(partp, &mtpart, sizeof(*partp));
549 partp->dp_scyl = partp->dp_ipl_cyl = start / dos_cylsecs;
550 partp->dp_shd = partp->dp_ipl_head = start % dos_cylsecs / dos_sectors;
551 partp->dp_ssect = partp->dp_ipl_sct = start % dos_sectors;
554 end = start + size - cylsecs;
555 partp->dp_ecyl = end / dos_cylsecs;
556 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
557 partp->dp_esect = end % dos_sectors;
566 if (stat(disk, &st) == -1) {
569 warnx("can't get file status of %s", disk);
572 if ( !(st.st_mode & S_IFCHR) )
573 warnx("device %s is not character special", disk);
574 rwmode = a_flag || B_flag || flag ? O_RDWR : O_RDONLY;
575 fd = open(disk, rwmode);
576 if (fd == -1 && errno == EPERM && rwmode == O_RDWR)
577 fd = open(disk, O_RDONLY);
578 if (fd == -1 && errno == ENXIO)
581 warnx("can't open device %s", disk);
584 if (get_params() == -1) {
585 warnx("can't get disk parameters on %s", disk);
592 read_disk(off_t sector, void *buf)
595 lseek(fd, (sector * 512), 0);
597 secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2);
601 write_disk(off_t sector, void *buf)
604 struct gctl_req *grq;
609 grq = gctl_get_handle();
610 gctl_ro_param(grq, "verb", -1, "write PC98");
611 gctl_ro_param(grq, "class", -1, "PC98");
612 q = strrchr(disk, '/');
617 gctl_ro_param(grq, "geom", -1, q);
618 gctl_ro_param(grq, "data", secsize, buf);
627 error = pwrite(fd, buf, secsize, (sector * 512));
628 if (error == secsize)
631 for (i = 0; i < NDOSPART; i++) {
632 sprintf(fbuf, "%ss%d", disk, i + 1);
633 fdw = open(fbuf, O_RDWR, 0);
636 error = ioctl(fdw, DIOCSPC98, buf);
641 warnx("Failed to write sector zero");
652 error = ioctl(fd, DIOCGFWSECTORS, &u);
654 sectors = dos_sectors = u;
656 sectors = dos_sectors = 17;
658 error = ioctl(fd, DIOCGFWHEADS, &u);
660 heads = dos_heads = u;
662 heads = dos_heads = 8;
664 dos_cylsecs = cylsecs = heads * sectors;
665 disksecs = cyls * heads * sectors;
667 error = ioctl(fd, DIOCGSECTORSIZE, &u);
668 if (error != 0 || u == 0)
672 error = ioctl(fd, DIOCGMEDIASIZE, &o);
675 cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
686 if (read_disk(0, (char *) mboot.bootinst) == -1) {
687 warnx("can't read fdisk partition table");
690 if (mboot.signature != DOSMAGIC) {
691 warnx("invalid fdisk partition table found");
692 /* So should we initialize things */
709 * write enable label sector before write (if necessary),
710 * disable after writing.
711 * needed if the disklabel protected area also protects
712 * sector 0. (e.g. empty disk)
714 if (write_disk(0, (char *) mboot.bootinst) == -1) {
715 warn("can't write fdisk partition table");
726 printf("%s [n] ", str);
728 if (fgets(lbuf, LBUF, stdin) == NULL)
730 lbuf[strlen(lbuf)-1] = 0;
733 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
734 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
741 decimal(const char *str, int *num, int deflt)
747 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
749 if (fgets(lbuf, LBUF, stdin) == NULL)
751 lbuf[strlen(lbuf)-1] = 0;
757 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
760 while ((c = *cp++)) {
761 if (c <= '9' && c >= '0')
762 acc = acc * 10 + c - '0';
766 if (c == ' ' || c == '\t')
767 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
772 printf("%s is an invalid decimal number. Try again.\n",
779 string(const char *str, char **ans)
785 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
786 fgets(lbuf, LBUF, stdin);
787 lbuf[strlen(lbuf)-1] = 0;
792 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
796 while ((c = *cp) && c != '"') cp++;
799 while ((c = *cp) && c != ' ' && c != '\t') cp++;
802 for (i = strlen(*ans); i < 16; i++)
813 int numentries = (sizeof(part_types)/sizeof(struct part_type));
815 struct part_type *ptr = part_types;
818 while(counter < numentries) {
819 if(ptr->type == (type & 0x7f))
828 * Try figuring out the root device's canonical disk name.
829 * The following choices are considered:
830 * /dev/ad0s1a => /dev/ad0
831 * /dev/da0a => /dev/da0
832 * /dev/vinum/root => /dev/vinum/root
837 struct statfs rootfs;
840 regmatch_t rm[NMATCHES];
844 if (statfs("/", &rootfs) == -1)
845 err(1, "statfs(\"/\")");
847 if ((rv = regcomp(&re, "^(/dev/[a-z]+[0-9]+)([sp][0-9]+)?[a-h]?$",
849 errx(1, "regcomp() failed (%d)", rv);
850 if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
852 "mounted root fs resource doesn't match expectations (regexec returned %d)",
854 if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
855 errx(1, "out of memory");
856 memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
857 rm[1].rm_eo - rm[1].rm_so);
858 s[rm[1].rm_eo - rm[1].rm_so] = 0;