Home | History | Annotate | Line # | Download | only in fdisk
fdisk.c revision 1.17
      1  1.17     phil /*	$NetBSD: fdisk.c,v 1.17 1997/09/11 22:53:00 phil Exp $	*/
      2  1.10      cgd 
      3   1.4  mycroft /*
      4   1.1      cgd  * Mach Operating System
      5   1.1      cgd  * Copyright (c) 1992 Carnegie Mellon University
      6   1.1      cgd  * All Rights Reserved.
      7   1.4  mycroft  *
      8   1.1      cgd  * Permission to use, copy, modify and distribute this software and its
      9   1.1      cgd  * documentation is hereby granted, provided that both the copyright
     10   1.1      cgd  * notice and this permission notice appear in all copies of the
     11   1.1      cgd  * software, derivative works or modified versions, and any portions
     12   1.1      cgd  * thereof, and that both notices appear in supporting documentation.
     13   1.4  mycroft  *
     14   1.1      cgd  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15   1.1      cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16   1.1      cgd  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17   1.4  mycroft  *
     18   1.1      cgd  * Carnegie Mellon requests users of this software to return to
     19   1.4  mycroft  *
     20   1.1      cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21   1.1      cgd  *  School of Computer Science
     22   1.1      cgd  *  Carnegie Mellon University
     23   1.1      cgd  *  Pittsburgh PA 15213-3890
     24   1.4  mycroft  *
     25   1.1      cgd  * any improvements or extensions that they make and grant Carnegie Mellon
     26   1.1      cgd  * the rights to redistribute these changes.
     27   1.1      cgd  */
     28   1.1      cgd 
     29  1.16     phil #include <sys/cdefs.h>
     30  1.16     phil 
     31   1.2  mycroft #ifndef lint
     32  1.17     phil __RCSID("$NetBSD: fdisk.c,v 1.17 1997/09/11 22:53:00 phil Exp $");
     33   1.2  mycroft #endif /* not lint */
     34   1.2  mycroft 
     35   1.1      cgd #include <sys/types.h>
     36   1.1      cgd #include <sys/disklabel.h>
     37   1.4  mycroft #include <sys/ioctl.h>
     38   1.1      cgd #include <sys/stat.h>
     39   1.4  mycroft 
     40   1.8      cgd #include <ctype.h>
     41   1.4  mycroft #include <err.h>
     42   1.1      cgd #include <fcntl.h>
     43   1.4  mycroft #include <stdio.h>
     44   1.4  mycroft #include <stdlib.h>
     45   1.8      cgd #include <string.h>
     46   1.8      cgd #include <unistd.h>
     47   1.1      cgd 
     48   1.1      cgd #define LBUF 100
     49   1.1      cgd static char lbuf[LBUF];
     50   1.1      cgd 
     51   1.1      cgd /*
     52   1.1      cgd  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
     53   1.1      cgd  *	Copyright (c) 1989	Robert. V. Baron
     54   1.1      cgd  *	Created.
     55   1.1      cgd  */
     56   1.1      cgd 
     57   1.1      cgd char *disk = "/dev/rwd0d";
     58   1.1      cgd 
     59   1.1      cgd struct disklabel disklabel;		/* disk parameters */
     60   1.1      cgd 
     61   1.5  mycroft int cylinders, sectors, heads, cylindersectors, disksectors;
     62   1.1      cgd 
     63   1.4  mycroft struct mboot {
     64   1.1      cgd 	unsigned char padding[2]; /* force the longs to be long alligned */
     65   1.1      cgd 	unsigned char bootinst[DOSPARTOFF];
     66   1.1      cgd 	struct	dos_partition parts[4];
     67   1.1      cgd 	unsigned short int	signature;
     68   1.1      cgd };
     69   1.1      cgd struct mboot mboot;
     70   1.1      cgd 
     71   1.1      cgd #define ACTIVE 0x80
     72   1.1      cgd #define BOOT_MAGIC 0xAA55
     73   1.1      cgd 
     74   1.5  mycroft int dos_cylinders;
     75   1.1      cgd int dos_heads;
     76   1.1      cgd int dos_sectors;
     77   1.5  mycroft int dos_cylindersectors;
     78   1.1      cgd 
     79   1.4  mycroft #define DOSSECT(s,c)	(((s) & 0x3f) | (((c) >> 2) & 0xc0))
     80   1.4  mycroft #define DOSCYL(c)	((c) & 0xff)
     81   1.4  mycroft int partition = -1;
     82   1.4  mycroft 
     83   1.4  mycroft int a_flag;		/* set active partition */
     84   1.4  mycroft int i_flag;		/* replace partition data */
     85   1.4  mycroft int u_flag;		/* update partition data */
     86  1.15     phil int sh_flag;		/* Output data as shell defines */
     87  1.15     phil int f_flag;		/* force --not interactive */
     88  1.15     phil int s_flag;		/* set id,offset,size */
     89  1.16     phil int b_flag;		/* Set cyl, heads, secs (as c/h/s) */
     90  1.16     phil int b_cyl, b_head, b_sec;  /* b_flag values. */
     91   1.4  mycroft 
     92   1.4  mycroft unsigned char bootcode[] = {
     93   1.4  mycroft 0x33, 0xc0, 0xfa, 0x8e, 0xd0, 0xbc, 0x00, 0x7c, 0x8e, 0xc0, 0x8e, 0xd8, 0xfb, 0x8b, 0xf4, 0xbf,
     94   1.4  mycroft 0x00, 0x06, 0xb9, 0x00, 0x02, 0xfc, 0xf3, 0xa4, 0xea, 0x1d, 0x06, 0x00, 0x00, 0xb0, 0x04, 0xbe,
     95   1.4  mycroft 0xbe, 0x07, 0x80, 0x3c, 0x80, 0x74, 0x0c, 0x83, 0xc6, 0x10, 0xfe, 0xc8, 0x75, 0xf4, 0xbe, 0xbd,
     96   1.4  mycroft 0x06, 0xeb, 0x43, 0x8b, 0xfe, 0x8b, 0x14, 0x8b, 0x4c, 0x02, 0x83, 0xc6, 0x10, 0xfe, 0xc8, 0x74,
     97   1.4  mycroft 0x0a, 0x80, 0x3c, 0x80, 0x75, 0xf4, 0xbe, 0xbd, 0x06, 0xeb, 0x2b, 0xbd, 0x05, 0x00, 0xbb, 0x00,
     98   1.4  mycroft 0x7c, 0xb8, 0x01, 0x02, 0xcd, 0x13, 0x73, 0x0c, 0x33, 0xc0, 0xcd, 0x13, 0x4d, 0x75, 0xef, 0xbe,
     99   1.4  mycroft 0x9e, 0x06, 0xeb, 0x12, 0x81, 0x3e, 0xfe, 0x7d, 0x55, 0xaa, 0x75, 0x07, 0x8b, 0xf7, 0xea, 0x00,
    100   1.4  mycroft 0x7c, 0x00, 0x00, 0xbe, 0x85, 0x06, 0x2e, 0xac, 0x0a, 0xc0, 0x74, 0x06, 0xb4, 0x0e, 0xcd, 0x10,
    101   1.1      cgd 0xeb, 0xf4, 0xfb, 0xeb, 0xfe,
    102   1.1      cgd 'M', 'i', 's', 's', 'i', 'n', 'g', ' ',
    103   1.1      cgd 	'o', 'p', 'e', 'r', 'a', 't', 'i', 'n', 'g', ' ', 's', 'y', 's', 't', 'e', 'm', 0,
    104   1.4  mycroft 'E', 'r', 'r', 'o', 'r', ' ', 'l', 'o', 'a', 'd', 'i', 'n', 'g', ' ',
    105   1.1      cgd 	'o', 'p', 'e', 'r', 'a', 't', 'i', 'n', 'g', ' ', 's', 'y', 's', 't', 'e', 'm', 0,
    106   1.1      cgd 'I', 'n', 'v', 'a', 'l', 'i', 'd', ' ',
    107   1.1      cgd 	'p', 'a', 'r', 't', 'i', 't', 'i', 'o', 'n', ' ', 't', 'a', 'b', 'l', 'e', 0,
    108   1.1      cgd 'A', 'u', 't', 'h', 'o', 'r', ' ', '-', ' ',
    109   1.4  mycroft 	'S', 'i', 'e', 'g', 'm', 'a', 'r', ' ', 'S', 'c', 'h', 'm', 'i', 'd', 't', 0,0,0,
    110   1.1      cgd 
    111   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    112   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    113   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    114   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    115   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    116   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    117   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    118   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    119   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    120   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    121   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    122   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    123   1.4  mycroft   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0
    124   1.1      cgd };
    125   1.1      cgd 
    126   1.4  mycroft struct part_type {
    128   1.4  mycroft 	int type;
    129   1.4  mycroft 	char *name;
    130   1.4  mycroft } part_types[] = {
    131   1.4  mycroft 	{0x00, "unused"},
    132   1.4  mycroft 	{0x01, "Primary DOS with 12 bit FAT"},
    133   1.4  mycroft 	{0x02, "XENIX / filesystem"},
    134  1.13    perry 	{0x03, "XENIX /usr filesystem"},
    135   1.4  mycroft 	{0x04, "Primary DOS with 16 bit FAT <32M"},
    136  1.13    perry 	{0x05, "Extended DOS"},
    137  1.13    perry 	{0x06, "Primary 'big' DOS, 16-bit FAT (> 32MB)"},
    138   1.4  mycroft 	{0x07, "OS/2 HPFS or NTFS or QNX2 or Advanced UNIX"},
    139   1.4  mycroft 	{0x08, "AIX filesystem"},
    140  1.13    perry 	{0x09, "AIX boot partition or Coherent"},
    141  1.13    perry 	{0x0A, "OS/2 Boot Manager or Coherent swap or OPUS"},
    142  1.13    perry 	{0x0E, "DOS (16-bit FAT), CHS-mapped"},
    143   1.4  mycroft 	{0x0F, "Ext. partition, CHS-mapped"},
    144  1.13    perry 	{0x10, "OPUS"},
    145  1.13    perry 	{0x11, "OS/2 BM: hidden DOS 12-bit FAT"},
    146  1.13    perry 	{0x12, "Compaq diagnostics"},
    147  1.13    perry 	{0x14, "OS/2 BM: hidden DOS 16-bit FAT <32M"},
    148  1.13    perry 	{0x16, "OS/2 BM: hidden DOS 16-bit FAT >=32M"},
    149  1.13    perry 	{0x17, "OS/2 BM: hidden IFS"},
    150  1.13    perry 	{0x18, "AST Windows swapfile"},
    151  1.13    perry 	{0x24, "NEC DOS"},
    152   1.4  mycroft 	{0x3C, "PartitionMagic recovery"},
    153  1.13    perry 	{0x40, "VENIX 286"},
    154  1.13    perry 	{0x41, "Linux/MINIX (sharing disk with DRDOS)"},
    155  1.13    perry 	{0x42, "SFS or Linux swap (sharing disk with DRDOS)"},
    156  1.13    perry 	{0x43, "Linux native (sharing disk with DRDOS)"},
    157  1.13    perry 	{0x50, "DM (disk manager)"},
    158   1.4  mycroft 	{0x51, "DM6 Aux1 (or Novell)"},
    159  1.13    perry 	{0x52, "CP/M or Microport SysV/AT"},
    160  1.13    perry 	{0x53, "DM6 Aux3"},
    161  1.13    perry 	{0x54, "DM6"},
    162  1.13    perry 	{0x55, "EZ-Drive (disk manager)"},
    163  1.13    perry 	{0x56, "Golden Bow (disk manager)"},
    164  1.13    perry 	{0x5C, "Priam Edisk (disk manager)"},
    165  1.13    perry 	{0x61, "SpeedStor"},
    166   1.4  mycroft 	{0x63, "GNU HURD or Mach or Sys V/386 (such as ISC UNIX)"},
    167   1.4  mycroft 	{0x64, "Novell Netware 2.xx"},
    168  1.13    perry 	{0x65, "Novell Netware 3.xx"},
    169  1.13    perry 	{0x70, "DiskSecure Multi-Boot"},
    170  1.13    perry 	{0x75, "PC/IX"},
    171  1.13    perry 	{0x77, "QNX4.x"},
    172  1.13    perry 	{0x78, "QNX4.x 2nd part"},
    173  1.13    perry 	{0x79, "QNX4.x 3rd part"},
    174  1.13    perry 	{0x80, "MINIX until 1.4a"},
    175   1.9  mycroft 	{0x81, "MINIX since 1.4b, early Linux, Mitac dmgr"},
    176  1.13    perry 	{0x82, "Linux swap"},
    177  1.13    perry 	{0x83, "Linux native"},
    178  1.13    perry 	{0x84, "OS/2 hidden C: drive"},
    179  1.13    perry 	{0x85, "Linux extended"},
    180  1.13    perry 	{0x86, "NTFS volume set??"},
    181   1.4  mycroft 	{0x87, "NTFS volume set??"},
    182   1.4  mycroft 	{0x93, "Amoeba filesystem"},
    183  1.13    perry 	{0x94, "Amoeba bad block table"},
    184  1.13    perry 	{0xA0, "IBM Thinkpad hibernation"},
    185  1.13    perry 	{0xA5, "NetBSD or FreeBSD or 386BSD"},
    186  1.13    perry 	{0xA6, "OpenBSD"},
    187   1.4  mycroft 	{0xA7, "NeXTSTEP 486"},
    188   1.4  mycroft 	{0xB7, "BSDI BSD/386 filesystem"},
    189  1.13    perry 	{0xB8, "BSDI BSD/386 swap"},
    190  1.13    perry 	{0xC1, "DRDOS/sec (FAT-12)"},
    191  1.13    perry 	{0xC4, "DRDOS/sec (FAT-16, < 32M)"},
    192  1.13    perry 	{0xC6, "DRDOS/sec (FAT-16, >= 32M)"},
    193  1.13    perry 	{0xC7, "Syrinx"},
    194  1.13    perry 	{0xDB, "CP/M or Concurrent CP/M or Concurrent DOS or CTOS"},
    195  1.13    perry 	{0xE1, "DOS access or SpeedStor 12-bit FAT extended partition"},
    196  1.13    perry 	{0xE3, "DOS R/O or SpeedStor"},
    197  1.13    perry 	{0xE4, "SpeedStor 16-bit FAT extended partition < 1024 cyl."},
    198   1.4  mycroft 	{0xF1, "SpeedStor"},
    199  1.13    perry 	{0xF2, "DOS 3.3+ Secondary"},
    200  1.13    perry 	{0xF4, "SpeedStor large partition"},
    201  1.13    perry 	{0xFE, "SpeedStor >1024 cyl. or LANstep"},
    202   1.1      cgd 	{0xFF, "Xenix Bad Block Table"},
    203   1.1      cgd };
    204   1.4  mycroft 
    205   1.4  mycroft void	usage __P((void));
    206   1.4  mycroft void	print_s0 __P((int));
    207   1.4  mycroft void	print_part __P((int));
    208  1.11  ghudson void	init_sector0 __P((int));
    209  1.11  ghudson void	intuit_translated_geometry __P((void));
    210  1.11  ghudson int	try_heads __P((quad_t, quad_t, quad_t, quad_t, quad_t, quad_t, quad_t,
    211  1.11  ghudson 		       quad_t));
    212  1.15     phil int	try_sectors __P((quad_t, quad_t, quad_t, quad_t, quad_t));
    213   1.4  mycroft void	change_part __P((int, int, int, int));
    214   1.4  mycroft void	print_params __P((void));
    215   1.4  mycroft void	change_active __P((int));
    216   1.4  mycroft void	get_params_to_use __P((void));
    217   1.4  mycroft void	dos __P((int, unsigned char *, unsigned char *, unsigned char *));
    218   1.4  mycroft int	open_disk __P((int));
    219   1.4  mycroft int	read_disk __P((int, void *));
    220   1.4  mycroft int	write_disk __P((int, void *));
    221   1.4  mycroft int	get_params __P((void));
    222   1.4  mycroft int	read_s0 __P((void));
    223   1.4  mycroft int	write_s0 __P((void));
    224   1.5  mycroft int	yesno __P((char *));
    225   1.4  mycroft void	decimal __P((char *, int *));
    226   1.4  mycroft int	type_match __P((const void *, const void *));
    227  1.16     phil char	*get_type __P((int));
    228  1.16     phil int	get_mapping __P((int, int *, int *, int *, long *));
    229  1.16     phil 
    230  1.16     phil static inline unsigned short getshort __P((void *));
    231  1.16     phil static inline void putshort __P((void *p, unsigned short));
    232  1.16     phil static inline unsigned long getlong __P((void *));
    233  1.16     phil static inline void putlong __P((void *,	unsigned long));
    234  1.16     phil 
    235  1.16     phil 
    236   1.1      cgd int	main __P((int, char **));
    237   1.4  mycroft 
    238   1.1      cgd int
    239   1.4  mycroft main(argc, argv)
    240   1.4  mycroft 	int argc;
    241   1.1      cgd 	char *argv[];
    242   1.4  mycroft {
    243   1.4  mycroft 	int ch;
    244   1.1      cgd 	int part;
    245  1.15     phil 
    246  1.15     phil 	int csysid, cstart, csize;	/* For the b_flag. */
    247  1.16     phil 
    248  1.15     phil 	a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
    249  1.16     phil 	csysid = cstart = csize = 0;
    250   1.4  mycroft 	while ((ch = getopt(argc, argv, "0123Safius:b:")) != -1)
    251   1.4  mycroft 		switch (ch) {
    252   1.4  mycroft 		case '0':
    253   1.4  mycroft 			partition = 0;
    254   1.4  mycroft 			break;
    255   1.4  mycroft 		case '1':
    256   1.4  mycroft 			partition = 1;
    257   1.4  mycroft 			break;
    258   1.4  mycroft 		case '2':
    259   1.4  mycroft 			partition = 2;
    260   1.4  mycroft 			break;
    261   1.4  mycroft 		case '3':
    262   1.4  mycroft 			partition = 3;
    263  1.15     phil 			break;
    264  1.15     phil 		case 'S':
    265  1.15     phil 			sh_flag = 1;
    266   1.4  mycroft 			break;
    267   1.4  mycroft 		case 'a':
    268   1.4  mycroft 			a_flag = 1;
    269  1.15     phil 			break;
    270  1.15     phil 		case 'f':
    271  1.15     phil 			f_flag = 1;
    272   1.4  mycroft 			break;
    273   1.4  mycroft 		case 'i':
    274  1.15     phil 			i_flag = 1;
    275   1.4  mycroft 			break;
    276   1.4  mycroft 		case 'u':
    277   1.1      cgd 			u_flag = 1;
    278  1.14     phil 			break;
    279  1.14     phil 		case 's':
    280  1.16     phil 			s_flag = 1;
    281  1.16     phil 			if (sscanf (optarg, "%d/%d/%d",
    282  1.16     phil 				    &csysid, &cstart, &csize) != 3) {
    283  1.16     phil 				(void)fprintf (stderr, "%s: Bad argument "
    284  1.16     phil 					       "to the -s flag.\n",
    285  1.16     phil 					       argv[0]);
    286  1.16     phil 				exit (1);
    287  1.16     phil 			}
    288  1.16     phil 			break;
    289  1.16     phil 		case 'b':
    290  1.16     phil 			b_flag = 1;
    291  1.16     phil 			if (sscanf (optarg, "%d/%d/%d",
    292  1.16     phil 				    &b_cyl, &b_head, &b_sec) != 3) {
    293  1.16     phil 				(void)fprintf (stderr, "%s: Bad argument "
    294  1.16     phil 					       "to the -b flag.\n",
    295  1.16     phil 					       argv[0]);
    296  1.16     phil 				exit (1);
    297  1.14     phil 			}
    298   1.4  mycroft 			break;
    299   1.4  mycroft 		default:
    300   1.1      cgd 			usage();
    301   1.4  mycroft 		}
    302   1.4  mycroft 	argc -= optind;
    303   1.1      cgd 	argv += optind;
    304  1.15     phil 
    305  1.15     phil 	if (sh_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
    306  1.15     phil 		usage();
    307  1.15     phil 
    308  1.15     phil 	if (partition == -1 && s_flag) {
    309  1.15     phil 		(void) fprintf (stderr,
    310  1.14     phil 				"-s flag requires a partition selected.\n");
    311  1.15     phil 		usage();
    312  1.15     phil 	}
    313   1.1      cgd 
    314   1.1      cgd 	if (argc > 0)
    315   1.4  mycroft 		disk = argv[0];
    316   1.4  mycroft 
    317   1.1      cgd 	if (open_disk(a_flag || i_flag || u_flag) < 0)
    318   1.1      cgd 		exit(1);
    319  1.11  ghudson 
    320  1.15     phil 	if (read_s0())
    321  1.11  ghudson 		init_sector0(sectors > 63 ? 63 : sectors);
    322  1.11  ghudson 
    323  1.11  ghudson 	intuit_translated_geometry();
    324  1.17     phil 
    325  1.14     phil 	if (!sh_flag && !f_flag)
    326  1.15     phil 		printf("******* Working on device %s *******\n", disk);
    327  1.15     phil 
    328  1.16     phil 
    329   1.1      cgd 	if ((i_flag || u_flag) && (!f_flag || b_flag))
    330   1.1      cgd 		get_params_to_use();
    331  1.15     phil 
    332  1.15     phil 	if (i_flag)
    333  1.15     phil 		init_sector0(dos_sectors > 63 ? 63 : dos_sectors);
    334  1.17     phil 
    335  1.14     phil 	if (!sh_flag && !f_flag)
    336  1.14     phil 		printf("Warning: BIOS sector numbering starts with sector 1\n");
    337  1.15     phil 
    338  1.15     phil 	/* Do the update stuff! */
    339  1.15     phil 	if (u_flag) {
    340  1.15     phil 		if (!f_flag)
    341  1.15     phil 			printf("Information from DOS bootblock is:\n");
    342  1.15     phil 		if (partition == -1)
    343  1.15     phil 			for (part = 0; part < NDOSPART; part++)
    344  1.15     phil 				change_part(part,-1, -1, -1);
    345  1.15     phil 		else
    346   1.4  mycroft 			change_part(partition, csysid, cstart, csize);
    347  1.15     phil 	} else
    348  1.15     phil 		if (!i_flag)
    349   1.1      cgd 			print_s0(partition);
    350  1.15     phil 
    351   1.1      cgd 	if (a_flag)
    352   1.1      cgd 		change_active(partition);
    353  1.15     phil 
    354  1.15     phil 	if (u_flag || a_flag || i_flag) {
    355  1.15     phil 		if (!f_flag) {
    356  1.15     phil 			printf("\nWe haven't changed the partition table "
    357  1.15     phil 			       "yet.  This is your last chance.\n");
    358  1.15     phil 			print_s0(-1);
    359  1.15     phil 			if (yesno("Should we write new partition table?"))
    360  1.15     phil 				write_s0();
    361   1.1      cgd 		} else
    362   1.1      cgd 			write_s0();
    363   1.1      cgd 	}
    364   1.1      cgd 
    365   1.4  mycroft 	exit(0);
    366   1.1      cgd }
    367   1.4  mycroft 
    368   1.4  mycroft void
    369   1.4  mycroft usage()
    370  1.15     phil {
    371  1.16     phil 	(void)fprintf(stderr, "usage: fdisk [-aiufS] [-0|-1|-2|-3] "
    372   1.4  mycroft 		      "[device]\n");
    373   1.1      cgd 	exit(1);
    374   1.1      cgd }
    375   1.4  mycroft 
    376   1.1      cgd void
    377   1.4  mycroft print_s0(which)
    378   1.1      cgd 	int which;
    379   1.4  mycroft {
    380   1.1      cgd 	int part;
    381   1.1      cgd 
    382  1.15     phil 	print_params();
    383  1.15     phil 	if (!sh_flag)
    384   1.4  mycroft 		printf("Information from DOS bootblock is:\n");
    385  1.15     phil 	if (which == -1) {
    386  1.15     phil 		for (part = 0; part < NDOSPART; part++) {
    387  1.15     phil 			if (!sh_flag)
    388  1.15     phil 				printf("%d: ", part);
    389  1.15     phil 			print_part(part);
    390   1.4  mycroft 		}
    391   1.1      cgd 	} else
    392   1.1      cgd 		print_part(which);
    393   1.1      cgd }
    394   1.1      cgd 
    395   1.1      cgd static struct dos_partition mtpart = { 0 };
    396  1.12  thorpej 
    397  1.12  thorpej static inline unsigned short
    398  1.12  thorpej getshort(p)
    399  1.12  thorpej 	void *p;
    400  1.12  thorpej {
    401  1.12  thorpej 	unsigned char *cp = p;
    402  1.12  thorpej 
    403  1.12  thorpej 	return cp[0] | (cp[1] << 8);
    404  1.12  thorpej }
    405  1.12  thorpej 
    406  1.12  thorpej static inline void
    407  1.12  thorpej putshort(p, l)
    408  1.12  thorpej 	void *p;
    409  1.12  thorpej 	unsigned short l;
    410  1.12  thorpej {
    411  1.12  thorpej 	unsigned char *cp = p;
    412  1.12  thorpej 
    413  1.12  thorpej 	*cp++ = l;
    414  1.12  thorpej 	*cp++ = l >> 8;
    415  1.12  thorpej }
    416  1.12  thorpej 
    417  1.12  thorpej static inline unsigned long
    418  1.12  thorpej getlong(p)
    419  1.12  thorpej 	void *p;
    420  1.12  thorpej {
    421  1.12  thorpej 	unsigned char *cp = p;
    422  1.12  thorpej 
    423  1.12  thorpej 	return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
    424  1.12  thorpej }
    425  1.12  thorpej 
    426  1.12  thorpej static inline void
    427  1.12  thorpej putlong(p, l)
    428  1.12  thorpej 	void *p;
    429  1.12  thorpej 	unsigned long l;
    430  1.12  thorpej {
    431  1.12  thorpej 	unsigned char *cp = p;
    432  1.12  thorpej 
    433  1.12  thorpej 	*cp++ = l;
    434  1.12  thorpej 	*cp++ = l >> 8;
    435  1.12  thorpej 	*cp++ = l >> 16;
    436  1.12  thorpej 	*cp++ = l >> 24;
    437  1.12  thorpej }
    438   1.4  mycroft 
    439   1.4  mycroft void
    440   1.4  mycroft print_part(part)
    441   1.1      cgd 	int part;
    442   1.4  mycroft {
    443  1.14     phil 	struct dos_partition *partp;
    444   1.1      cgd 	int empty;
    445   1.4  mycroft 
    446  1.14     phil 	partp = &mboot.parts[part];
    447  1.14     phil 	empty = !memcmp(partp, &mtpart, sizeof(struct dos_partition));
    448  1.15     phil 
    449  1.14     phil 	if (sh_flag) {
    450  1.14     phil 		if (empty) {
    451  1.14     phil 			printf("PART%dSIZE=0\n", part);
    452  1.14     phil 			return;
    453  1.14     phil 		}
    454  1.14     phil 
    455  1.16     phil 		printf("PART%dID=%d\n", part, partp->dp_typ);
    456  1.16     phil 		printf("PART%dSIZE=%ld\n", part, getlong(&partp->dp_size));
    457  1.14     phil 		printf("PART%dSTART=%ld\n", part, getlong(&partp->dp_start));
    458  1.14     phil 		printf("PART%dFLAG=%x\n", part, partp->dp_flag);
    459  1.14     phil 		printf("PART%dBCYL=%d\n", part, DPCYL(partp->dp_scyl,
    460  1.14     phil 						      partp->dp_ssect));
    461  1.14     phil 		printf("PART%dBHEAD=%d\n", part, partp->dp_shd);
    462  1.14     phil 		printf("PART%dBSEC=%d\n", part, DPSECT(partp->dp_ssect));
    463  1.14     phil 		printf("PART%dECYL=%d\n", part, DPCYL(partp->dp_ecyl,
    464  1.14     phil 						      partp->dp_esect));
    465  1.14     phil 		printf("PART%dEHEAD=%d\n", part, partp->dp_ehd);
    466  1.14     phil 		printf("PART%dESEC=%d\n", part, DPSECT(partp->dp_esect));
    467  1.14     phil 		return;
    468  1.14     phil 	}
    469  1.15     phil 
    470  1.14     phil 	/* Not sh_flag. */
    471   1.1      cgd 	if (empty) {
    472   1.1      cgd 		printf("<UNUSED>\n");
    473   1.1      cgd 		return;
    474   1.5  mycroft 	}
    475  1.16     phil 	printf("sysid %d (%s)\n", partp->dp_typ, get_type(partp->dp_typ));
    476  1.12  thorpej 	printf("    start %ld, size %ld (%ld MB), flag %x\n",
    477  1.12  thorpej 	    getlong(&partp->dp_start), getlong(&partp->dp_size),
    478   1.5  mycroft 	    getlong(&partp->dp_size) * 512 / (1024 * 1024), partp->dp_flag);
    479   1.5  mycroft 	printf("\tbeg: cylinder %4d, head %3d, sector %2d\n",
    480   1.5  mycroft 	    DPCYL(partp->dp_scyl, partp->dp_ssect),
    481   1.5  mycroft 	    partp->dp_shd, DPSECT(partp->dp_ssect));
    482   1.5  mycroft 	printf("\tend: cylinder %4d, head %3d, sector %2d\n",
    483   1.5  mycroft 	    DPCYL(partp->dp_ecyl, partp->dp_esect),
    484   1.1      cgd 	    partp->dp_ehd, DPSECT(partp->dp_esect));
    485   1.1      cgd }
    486   1.4  mycroft 
    487   1.1      cgd void
    488   1.4  mycroft init_sector0(start)
    489   1.1      cgd 	int start;
    490  1.15     phil {
    491   1.4  mycroft 	int i;
    492   1.1      cgd 	struct dos_partition *partp;
    493  1.15     phil 
    494  1.15     phil 	int dos_disksectors = dos_cylinders * dos_heads * dos_sectors;
    495   1.4  mycroft 
    496  1.12  thorpej 	memcpy(mboot.bootinst, bootcode, sizeof(bootcode));
    497  1.15     phil 	putshort(&mboot.signature, BOOT_MAGIC);
    498  1.15     phil 
    499  1.15     phil 	for (i=0; i<3; i++)
    500   1.1      cgd 		memset (&mboot.parts[i], 0, sizeof(struct dos_partition));
    501   1.4  mycroft 
    502   1.1      cgd 	partp = &mboot.parts[3];
    503   1.1      cgd 	partp->dp_typ = DOSPTYP_386BSD;
    504  1.12  thorpej 	partp->dp_flag = ACTIVE;
    505  1.15     phil 	putlong(&partp->dp_start, start);
    506   1.1      cgd 	putlong(&partp->dp_size, dos_disksectors - start);
    507  1.12  thorpej 
    508   1.4  mycroft 	dos(getlong(&partp->dp_start),
    509  1.12  thorpej 	    &partp->dp_scyl, &partp->dp_shd, &partp->dp_ssect);
    510   1.4  mycroft 	dos(getlong(&partp->dp_start) + getlong(&partp->dp_size) - 1,
    511  1.15     phil 	    &partp->dp_ecyl, &partp->dp_ehd, &partp->dp_esect);
    512  1.15     phil 
    513  1.11  ghudson 	printf ("DOS partition table initialized.\n");
    514  1.11  ghudson }
    515  1.11  ghudson 
    516  1.11  ghudson /* Prerequisite: the disklabel parameters and master boot record must
    517  1.11  ghudson  *		 have been read (i.e. dos_* and mboot are meaningful).
    518  1.11  ghudson  * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and
    519  1.11  ghudson  *		  dos_cylindersectors to be consistent with what the
    520  1.11  ghudson  *		  partition table is using, if we can find a geometry
    521  1.11  ghudson  *		  which is consistent with all partition table entries.
    522  1.11  ghudson  *		  We may get the number of cylinders slightly wrong (in
    523  1.11  ghudson  *		  the conservative direction).  The idea is to be able
    524  1.11  ghudson  *		  to create a NetBSD partition on a disk we don't know
    525  1.11  ghudson  *		  the translated geometry of.
    526  1.11  ghudson  * This whole routine should be replaced with a kernel interface to get
    527  1.11  ghudson  * the BIOS geometry (which in turn requires modifications to the i386
    528  1.11  ghudson  * boot loader to pass in the BIOS geometry for each disk). */
    529  1.11  ghudson void
    530  1.11  ghudson intuit_translated_geometry()
    531  1.15     phil {
    532  1.11  ghudson 
    533  1.11  ghudson 	int cylinders = -1, heads = -1, sectors = -1, i, j;
    534  1.11  ghudson 	int c1, h1, s1, c2, h2, s2;
    535  1.11  ghudson 	long a1, a2;
    536  1.11  ghudson 	quad_t num, denom;
    537  1.11  ghudson 
    538  1.11  ghudson 	/* Try to deduce the number of heads from two different mappings. */
    539  1.11  ghudson 	for (i = 0; i < NDOSPART * 2; i++) {
    540  1.11  ghudson 		if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
    541  1.11  ghudson 			continue;
    542  1.11  ghudson 		for (j = 0; j < 8; j++) {
    543  1.11  ghudson 			if (get_mapping(j, &c2, &h2, &s2, &a2) < 0)
    544  1.11  ghudson 				continue;
    545  1.11  ghudson 			num = (quad_t)h1*(a2-s2) - h2*(a1-s1);
    546  1.11  ghudson 			denom = (quad_t)c2*(a1-s1) - c1*(a2-s2);
    547  1.11  ghudson 			if (denom != 0 && num % denom == 0) {
    548  1.11  ghudson 				heads = num / denom;
    549  1.11  ghudson 				break;
    550  1.11  ghudson 			}
    551  1.11  ghudson 		}
    552  1.11  ghudson 		if (heads != -1)
    553  1.11  ghudson 			break;
    554  1.11  ghudson 	}
    555  1.11  ghudson 
    556  1.11  ghudson 	if (heads == -1)
    557  1.11  ghudson 		return;
    558  1.11  ghudson 
    559  1.11  ghudson 	/* Now figure out the number of sectors from a single mapping. */
    560  1.11  ghudson 	for (i = 0; i < NDOSPART * 2; i++) {
    561  1.11  ghudson 		if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
    562  1.11  ghudson 			continue;
    563  1.11  ghudson 		num = a1 - s1;
    564  1.11  ghudson 		denom = c1 * heads + h1;
    565  1.11  ghudson 		if (denom != 0 && num % denom == 0) {
    566  1.11  ghudson 			sectors = num / denom;
    567  1.11  ghudson 			break;
    568  1.11  ghudson 		}
    569  1.11  ghudson 	}
    570  1.11  ghudson 
    571  1.11  ghudson 	if (sectors == -1)
    572  1.11  ghudson 		return;
    573  1.11  ghudson 
    574  1.11  ghudson 	/* Estimate the number of cylinders. */
    575  1.11  ghudson 	cylinders = dos_cylinders * dos_cylindersectors / heads / sectors;
    576  1.11  ghudson 
    577  1.11  ghudson 	/* Now verify consistency with each of the partition table entries.
    578  1.11  ghudson 	 * Be willing to shove cylinders up a little bit to make things work,
    579  1.11  ghudson 	 * but translation mismatches are fatal. */
    580  1.11  ghudson 	for (i = 0; i < NDOSPART * 2; i++) {
    581  1.11  ghudson 		if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
    582  1.11  ghudson 			continue;
    583  1.11  ghudson 		if (sectors * (c1 * heads + h1) + s1 != a1)
    584  1.11  ghudson 			return;
    585  1.11  ghudson 		if (c1 >= cylinders)
    586  1.11  ghudson 			cylinders = c1 + 1;
    587  1.11  ghudson 	}
    588  1.11  ghudson 
    589  1.11  ghudson 	/* Everything checks out.  Reset the geometry to use for further
    590  1.11  ghudson 	 * calculations. */
    591  1.11  ghudson 	dos_cylinders = cylinders;
    592  1.11  ghudson 	dos_heads = heads;
    593  1.11  ghudson 	dos_sectors = sectors;
    594  1.11  ghudson 	dos_cylindersectors = heads * sectors;
    595  1.11  ghudson }
    596  1.11  ghudson 
    597  1.11  ghudson /* For the purposes of intuit_translated_geometry(), treat the partition
    598  1.11  ghudson  * table as a list of eight mapping between (cylinder, head, sector)
    599  1.11  ghudson  * triplets and absolute sectors.  Get the relevant geometry triplet and
    600  1.11  ghudson  * absolute sectors for a given entry, or return -1 if it isn't present.
    601  1.11  ghudson  * Note: for simplicity, the returned sector is 0-based. */
    602  1.11  ghudson int
    603  1.11  ghudson get_mapping(i, cylinder, head, sector, absolute)
    604  1.11  ghudson 	int i, *cylinder, *head, *sector;
    605  1.11  ghudson 	long *absolute;
    606  1.11  ghudson {
    607  1.11  ghudson 	struct dos_partition *part = &mboot.parts[i / 2];
    608  1.11  ghudson 
    609  1.11  ghudson 	if (part->dp_typ == 0)
    610  1.11  ghudson 		return -1;
    611  1.11  ghudson 	if (i % 2 == 0) {
    612  1.11  ghudson 		*cylinder = DPCYL(part->dp_scyl, part->dp_ssect);
    613  1.11  ghudson 		*head = part->dp_shd;
    614  1.12  thorpej 		*sector = DPSECT(part->dp_ssect) - 1;
    615  1.11  ghudson 		*absolute = getlong(&part->dp_start);
    616  1.11  ghudson 	} else {
    617  1.11  ghudson 		*cylinder = DPCYL(part->dp_ecyl, part->dp_esect);
    618  1.11  ghudson 		*head = part->dp_ehd;
    619  1.12  thorpej 		*sector = DPSECT(part->dp_esect) - 1;
    620  1.12  thorpej 		*absolute = getlong(&part->dp_start)
    621  1.11  ghudson 		    + getlong(&part->dp_size) - 1;
    622  1.11  ghudson 	}
    623   1.1      cgd 	return 0;
    624   1.1      cgd }
    625   1.4  mycroft 
    626  1.15     phil void
    627  1.15     phil change_part(part, csysid, cstart, csize)
    628   1.1      cgd 	int part, csysid, cstart, csize;
    629   1.4  mycroft {
    630   1.1      cgd 	struct dos_partition *partp;
    631   1.4  mycroft 
    632   1.1      cgd 	partp = &mboot.parts[part];
    633  1.15     phil 
    634  1.15     phil 	if (s_flag) {
    635  1.15     phil 		partp->dp_typ = csysid;
    636  1.15     phil 		putlong(&partp->dp_start, cstart);
    637  1.15     phil 		putlong(&partp->dp_size, csize);
    638  1.15     phil 		dos(getlong(&partp->dp_start),
    639  1.15     phil 		    &partp->dp_scyl, &partp->dp_shd, &partp->dp_ssect);
    640  1.15     phil 		dos(getlong(&partp->dp_start)
    641  1.15     phil 		    + getlong(&partp->dp_size) - 1,
    642  1.15     phil 		    &partp->dp_ecyl, &partp->dp_ehd, &partp->dp_esect);
    643  1.15     phil 		if (f_flag)
    644  1.15     phil 			return;
    645  1.15     phil 	}
    646  1.15     phil 
    647   1.4  mycroft 	printf("The data for partition %d is:\n", part);
    648   1.4  mycroft 	print_part(part);
    649   1.4  mycroft 	if (!u_flag || !yesno("Do you want to change it?"))
    650   1.1      cgd 		return;
    651   1.1      cgd 
    652   1.5  mycroft 	do {
    653   1.5  mycroft 		{
    654   1.5  mycroft 			int sysid, start, size;
    655   1.5  mycroft 
    656  1.12  thorpej 			sysid = partp->dp_typ,
    657  1.12  thorpej 			start = getlong(&partp->dp_start),
    658   1.5  mycroft 			size = getlong(&partp->dp_size);
    659   1.5  mycroft 			decimal("sysid", &sysid);
    660   1.5  mycroft 			decimal("start", &start);
    661   1.5  mycroft 			decimal("size", &size);
    662  1.12  thorpej 			partp->dp_typ = sysid;
    663  1.12  thorpej 			putlong(&partp->dp_start, start);
    664   1.5  mycroft 			putlong(&partp->dp_size, size);
    665   1.1      cgd 		}
    666   1.7  mycroft 
    667   1.5  mycroft 		if (yesno("Explicitly specify beg/end address?")) {
    668   1.4  mycroft 			int tsector, tcylinder, thead;
    669   1.5  mycroft 
    670   1.5  mycroft 			tcylinder = DPCYL(partp->dp_scyl, partp->dp_ssect);
    671   1.5  mycroft 			thead = partp->dp_shd;
    672   1.5  mycroft 			tsector = DPSECT(partp->dp_ssect);
    673   1.5  mycroft 			decimal("beginning cylinder", &tcylinder);
    674   1.5  mycroft 			decimal("beginning head", &thead);
    675   1.5  mycroft 			decimal("beginning sector", &tsector);
    676   1.5  mycroft 			partp->dp_scyl = DOSCYL(tcylinder);
    677   1.5  mycroft 			partp->dp_shd = thead;
    678   1.5  mycroft 			partp->dp_ssect = DOSSECT(tsector, tcylinder);
    679   1.5  mycroft 
    680   1.5  mycroft 			tcylinder = DPCYL(partp->dp_ecyl, partp->dp_esect);
    681   1.5  mycroft 			thead = partp->dp_ehd;
    682   1.5  mycroft 			tsector = DPSECT(partp->dp_esect);
    683   1.5  mycroft 			decimal("ending cylinder", &tcylinder);
    684   1.5  mycroft 			decimal("ending head", &thead);
    685   1.5  mycroft 			decimal("ending sector", &tsector);
    686   1.5  mycroft 			partp->dp_ecyl = DOSCYL(tcylinder);
    687   1.5  mycroft 			partp->dp_ehd = thead;
    688   1.1      cgd 			partp->dp_esect = DOSSECT(tsector, tcylinder);
    689  1.12  thorpej 		} else {
    690   1.4  mycroft 			dos(getlong(&partp->dp_start),
    691  1.12  thorpej 			    &partp->dp_scyl, &partp->dp_shd, &partp->dp_ssect);
    692  1.12  thorpej 			dos(getlong(&partp->dp_start)
    693   1.4  mycroft 			    + getlong(&partp->dp_size) - 1,
    694   1.1      cgd 			    &partp->dp_ecyl, &partp->dp_ehd, &partp->dp_esect);
    695   1.4  mycroft 		}
    696   1.4  mycroft 
    697   1.7  mycroft 		print_part(part);
    698   1.1      cgd 	} while (!yesno("Is this entry okay?"));
    699   1.1      cgd }
    700   1.4  mycroft 
    701   1.1      cgd void
    702   1.1      cgd print_params()
    703   1.4  mycroft {
    704  1.15     phil 
    705  1.14     phil 	if (sh_flag) {
    706  1.14     phil 		printf ("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\n",
    707  1.14     phil 			cylinders, heads, sectors);
    708  1.14     phil 		printf ("BCYL=%d\nBHEAD=%d\nBSEC=%d\n",
    709  1.14     phil 			dos_cylinders, dos_heads, dos_sectors);
    710  1.14     phil 		return;
    711  1.14     phil 	}
    712  1.15     phil 
    713   1.1      cgd 	/* Not sh_flag */
    714   1.5  mycroft 	printf("parameters extracted from in-core disklabel are:\n");
    715   1.5  mycroft 	printf("cylinders=%d heads=%d sectors/track=%d (%d sectors/cylinder)\n\n",
    716   1.5  mycroft 	    cylinders, heads, sectors, cylindersectors);
    717   1.5  mycroft 	if (dos_sectors > 63 || dos_cylinders > 1023 || dos_heads > 255)
    718   1.1      cgd 		printf("Figures below won't work with BIOS for partitions not in cylinder 1\n");
    719   1.5  mycroft 	printf("parameters to be used for BIOS calculations are:\n");
    720   1.5  mycroft 	printf("cylinders=%d heads=%d sectors/track=%d (%d sectors/cylinder)\n\n",
    721   1.1      cgd 	    dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors);
    722   1.1      cgd }
    723   1.4  mycroft 
    724   1.1      cgd void
    725   1.4  mycroft change_active(which)
    726   1.1      cgd 	int which;
    727   1.4  mycroft {
    728   1.4  mycroft 	struct dos_partition *partp;
    729  1.15     phil 	int part;
    730   1.4  mycroft 	int active = 4;
    731   1.4  mycroft 
    732   1.1      cgd 	partp = &mboot.parts[0];
    733   1.1      cgd 
    734   1.1      cgd 	if (a_flag && which != -1)
    735   1.4  mycroft 		active = which;
    736   1.4  mycroft 	else {
    737   1.4  mycroft 		for (part = 0; part < NDOSPART; part++)
    738   1.4  mycroft 			if (partp[part].dp_flag & ACTIVE)
    739   1.4  mycroft 				active = part;
    740  1.15     phil 	}
    741  1.15     phil 	if (!f_flag) {
    742  1.15     phil 		if (yesno("Do you want to change the active partition?")) {
    743  1.15     phil 			printf ("Choosing 4 will make no partition active.\n");
    744  1.15     phil 			do {
    745  1.15     phil 				decimal("active partition", &active);
    746  1.15     phil 			} while (!yesno("Are you happy with this choice?"));
    747  1.15     phil 		} else
    748  1.15     phil 			return;
    749  1.15     phil 	} else
    750  1.15     phil 		if (active != 4)
    751  1.15     phil 			printf ("Making partition %d active.\n", active);
    752   1.4  mycroft 
    753   1.4  mycroft 	for (part = 0; part < NDOSPART; part++)
    754  1.15     phil 		partp[part].dp_flag &= ~ACTIVE;
    755  1.15     phil 	if (active < 4)
    756   1.1      cgd 		partp[active].dp_flag |= ACTIVE;
    757   1.1      cgd }
    758   1.4  mycroft 
    759   1.1      cgd void
    760   1.1      cgd get_params_to_use()
    761  1.16     phil {
    762  1.16     phil 	if (b_flag) {
    763  1.16     phil 		dos_cylinders = b_cyl;
    764  1.16     phil 		dos_heads = b_head;
    765  1.16     phil 		dos_sectors = b_sec;
    766  1.16     phil 		dos_cylindersectors = dos_heads * dos_sectors;
    767  1.16     phil 		return;
    768   1.4  mycroft 	}
    769   1.1      cgd 
    770   1.4  mycroft 	print_params();
    771   1.4  mycroft 	if (yesno("Do you want to change our idea of what BIOS thinks?")) {
    772   1.5  mycroft 		do {
    773   1.5  mycroft 			decimal("BIOS's idea of #cylinders", &dos_cylinders);
    774   1.5  mycroft 			decimal("BIOS's idea of #heads", &dos_heads);
    775   1.5  mycroft 			decimal("BIOS's idea of #sectors", &dos_sectors);
    776   1.1      cgd 			dos_cylindersectors = dos_heads * dos_sectors;
    777   1.4  mycroft 			print_params();
    778   1.1      cgd 		} while (!yesno("Are you happy with this choice?"));
    779   1.1      cgd 	}
    780   1.1      cgd }
    781   1.1      cgd 
    782   1.1      cgd /***********************************************\
    783   1.1      cgd * Change real numbers into strange dos numbers	*
    784   1.4  mycroft \***********************************************/
    785   1.5  mycroft void
    786   1.5  mycroft dos(sector, cylinderp, headp, sectorp)
    787   1.5  mycroft 	int sector;
    788   1.4  mycroft 	unsigned char *cylinderp, *headp, *sectorp;
    789   1.5  mycroft {
    790   1.4  mycroft 	int cylinder, head;
    791   1.5  mycroft 
    792   1.5  mycroft 	cylinder = sector / dos_cylindersectors;
    793   1.4  mycroft 	sector -= cylinder * dos_cylindersectors;
    794   1.5  mycroft 
    795   1.5  mycroft 	head = sector / dos_sectors;
    796   1.4  mycroft 	sector -= head * dos_sectors;
    797   1.5  mycroft 
    798   1.5  mycroft 	*cylinderp = DOSCYL(cylinder);
    799   1.5  mycroft 	*headp = head;
    800   1.1      cgd 	*sectorp = DOSSECT(sector + 1, cylinder);
    801   1.1      cgd }
    802   1.1      cgd 
    803   1.1      cgd int fd;
    804   1.4  mycroft 
    805   1.1      cgd int
    806   1.4  mycroft open_disk(u_flag)
    807   1.1      cgd 	int u_flag;
    808   1.4  mycroft {
    809   1.1      cgd 	struct stat st;
    810   1.4  mycroft 
    811   1.4  mycroft 	if ((fd = open(disk, u_flag ? O_RDWR : O_RDONLY)) == -1) {
    812   1.4  mycroft 		warn("%s", disk);
    813   1.4  mycroft 		return (-1);
    814   1.4  mycroft 	}
    815   1.4  mycroft 	if (fstat(fd, &st) == -1) {
    816   1.4  mycroft 		close(fd);
    817   1.4  mycroft 		warn("%s", disk);
    818   1.1      cgd 		return (-1);
    819   1.7  mycroft 	}
    820   1.4  mycroft 	if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode)) {
    821   1.7  mycroft 		close(fd);
    822   1.4  mycroft 		warnx("%s is not a character device or regular file", disk);
    823   1.1      cgd 		return (-1);
    824   1.4  mycroft 	}
    825   1.4  mycroft 	if (get_params() == -1) {
    826   1.4  mycroft 		close(fd);
    827   1.1      cgd 		return (-1);
    828   1.4  mycroft 	}
    829   1.1      cgd 	return (0);
    830   1.1      cgd }
    831   1.4  mycroft 
    832   1.1      cgd int
    833   1.4  mycroft read_disk(sector, buf)
    834   1.4  mycroft 	int sector;
    835   1.1      cgd 	void *buf;
    836   1.4  mycroft {
    837   1.4  mycroft 
    838   1.4  mycroft 	if (lseek(fd, (off_t)(sector * 512), 0) == -1)
    839   1.4  mycroft 		return (-1);
    840   1.1      cgd 	return (read(fd, buf, 512));
    841   1.1      cgd }
    842   1.4  mycroft 
    843   1.1      cgd int
    844   1.4  mycroft write_disk(sector, buf)
    845   1.4  mycroft 	int sector;
    846   1.1      cgd 	void *buf;
    847   1.4  mycroft {
    848   1.4  mycroft 
    849   1.4  mycroft 	if (lseek(fd, (off_t)(sector * 512), 0) == -1)
    850   1.4  mycroft 		return (-1);
    851   1.1      cgd 	return (write(fd, buf, 512));
    852   1.1      cgd }
    853   1.4  mycroft 
    854   1.4  mycroft int
    855   1.1      cgd get_params()
    856   1.1      cgd {
    857   1.4  mycroft 
    858   1.4  mycroft 	if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
    859   1.4  mycroft 		warn("DIOCGDINFO");
    860   1.4  mycroft 		return (-1);
    861   1.1      cgd 	}
    862   1.5  mycroft 
    863   1.4  mycroft 	dos_cylinders = cylinders = disklabel.d_ncylinders;
    864   1.4  mycroft 	dos_heads = heads = disklabel.d_ntracks;
    865   1.5  mycroft 	dos_sectors = sectors = disklabel.d_nsectors;
    866   1.5  mycroft 	dos_cylindersectors = cylindersectors = heads * sectors;
    867   1.1      cgd 	disksectors = cylinders * heads * sectors;
    868   1.4  mycroft 
    869   1.1      cgd 	return (0);
    870   1.1      cgd }
    871   1.4  mycroft 
    872   1.1      cgd int
    873   1.1      cgd read_s0()
    874   1.4  mycroft {
    875   1.4  mycroft 
    876   1.4  mycroft 	if (read_disk(0, mboot.bootinst) == -1) {
    877   1.4  mycroft 		warn("can't read fdisk partition table");
    878   1.1      cgd 		return (-1);
    879  1.12  thorpej 	}
    880  1.12  thorpej 	if (getshort(&mboot.signature) != BOOT_MAGIC) {
    881   1.4  mycroft 		warnx("invalid fdisk partition table found");
    882   1.4  mycroft 		/* So should we initialize things? */
    883   1.1      cgd 		return (-1);
    884   1.4  mycroft 	}
    885   1.1      cgd 	return (0);
    886   1.1      cgd }
    887   1.4  mycroft 
    888   1.1      cgd int
    889   1.1      cgd write_s0()
    890   1.4  mycroft {
    891   1.4  mycroft 	int flag;
    892   1.1      cgd 
    893   1.1      cgd 	/*
    894   1.1      cgd 	 * write enable label sector before write (if necessary),
    895   1.1      cgd 	 * disable after writing.
    896   1.1      cgd 	 * needed if the disklabel protected area also protects
    897   1.1      cgd 	 * sector 0. (e.g. empty disk)
    898   1.1      cgd 	 */
    899   1.1      cgd 	flag = 1;
    900   1.4  mycroft 	if (ioctl(fd, DIOCWLABEL, &flag) < 0)
    901   1.4  mycroft 		warn("DIOCWLABEL");
    902   1.4  mycroft 	if (write_disk(0, mboot.bootinst) == -1) {
    903   1.1      cgd 		warn("can't write fdisk partition table");
    904   1.4  mycroft 		return -1;
    905   1.1      cgd 	}
    906   1.4  mycroft 	flag = 0;
    907   1.4  mycroft 	if (ioctl(fd, DIOCWLABEL, &flag) < 0)
    908  1.16     phil 		warn("DIOCWLABEL");
    909   1.1      cgd 	return 0;
    910   1.1      cgd }
    911   1.4  mycroft 
    912   1.4  mycroft int
    913   1.4  mycroft yesno(str)
    914   1.4  mycroft 	char *str;
    915   1.4  mycroft {
    916   1.1      cgd 	int ch, first;
    917   1.1      cgd 
    918   1.1      cgd 	printf("%s [n] ", str);
    919   1.4  mycroft 
    920   1.4  mycroft 	first = ch = getchar();
    921   1.4  mycroft 	while (ch != '\n' && ch != EOF)
    922   1.4  mycroft 		ch = getchar();
    923   1.1      cgd 	return (first == 'y' || first == 'Y');
    924   1.1      cgd }
    925   1.5  mycroft 
    926   1.5  mycroft void
    927   1.4  mycroft decimal(str, num)
    928   1.5  mycroft 	char *str;
    929   1.1      cgd 	int *num;
    930   1.8      cgd {
    931   1.4  mycroft 	int acc = 0;
    932   1.1      cgd 	char *cp;
    933   1.5  mycroft 
    934   1.5  mycroft 	for (;; printf("%s is not a valid decimal number.\n", lbuf)) {
    935   1.1      cgd 		printf("Supply a decimal value for \"%s\" [%d] ", str, *num);
    936   1.1      cgd 
    937   1.7  mycroft 		fgets(lbuf, LBUF, stdin);
    938   1.7  mycroft 		lbuf[strlen(lbuf)-1] = '\0';
    939   1.7  mycroft 		cp = lbuf;
    940   1.7  mycroft 
    941   1.7  mycroft 		cp += strspn(cp, " \t");
    942   1.5  mycroft 		if (*cp == '\0')
    943   1.1      cgd 			return;
    944   1.7  mycroft 
    945   1.7  mycroft 		if (!isdigit(*cp))
    946   1.5  mycroft 			continue;
    947   1.7  mycroft 		acc = strtol(lbuf, &cp, 10);
    948   1.5  mycroft 
    949   1.5  mycroft 		cp += strspn(cp, " \t");
    950   1.5  mycroft 		if (*cp != '\0')
    951   1.1      cgd 			continue;
    952   1.5  mycroft 
    953   1.5  mycroft 		*num = acc;
    954   1.1      cgd 		return;
    955   1.1      cgd 	}
    956   1.1      cgd 
    957   1.1      cgd }
    958   1.4  mycroft 
    959   1.4  mycroft int
    960   1.4  mycroft type_match(key, item)
    961   1.4  mycroft 	const void *key, *item;
    962   1.4  mycroft {
    963   1.4  mycroft 	const int *typep = key;
    964   1.4  mycroft 	const struct part_type *ptr = item;
    965   1.4  mycroft 
    966   1.4  mycroft 	if (*typep < ptr->type)
    967   1.4  mycroft 		return (-1);
    968   1.4  mycroft 	if (*typep > ptr->type)
    969   1.4  mycroft 		return (1);
    970   1.4  mycroft 	return (0);
    971   1.4  mycroft }
    972   1.4  mycroft 
    973   1.4  mycroft char *
    974   1.4  mycroft get_type(type)
    975   1.4  mycroft 	int type;
    976   1.4  mycroft {
    977   1.4  mycroft 	struct part_type *ptr;
    978   1.4  mycroft 
    979   1.4  mycroft 	ptr = bsearch(&type, part_types,
    980   1.4  mycroft 	    sizeof(part_types) / sizeof(struct part_type),
    981   1.4  mycroft 	    sizeof(struct part_type), type_match);
    982   1.4  mycroft 	if (ptr == 0)
    983  1.16     phil 		return ("unknown");
    984   1.1      cgd 	return (ptr->name);
    985                }
    986