Home | History | Annotate | Line # | Download | only in fdisk
fdisk.c revision 1.61
      1  1.61       dsl /*	$NetBSD: fdisk.c,v 1.61 2003/04/30 10:29:52 dsl 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.61       dsl __RCSID("$NetBSD: fdisk.c,v 1.61 2003/04/30 10:29:52 dsl 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.32   thorpej #include <sys/disklabel_mbr.h>
     38   1.4   mycroft #include <sys/ioctl.h>
     39  1.18     lukem #include <sys/param.h>
     40   1.1       cgd #include <sys/stat.h>
     41  1.48     lukem #include <sys/sysctl.h>
     42   1.4   mycroft 
     43   1.8       cgd #include <ctype.h>
     44   1.4   mycroft #include <err.h>
     45  1.18     lukem #include <errno.h>
     46   1.1       cgd #include <fcntl.h>
     47  1.18     lukem #include <paths.h>
     48   1.4   mycroft #include <stdio.h>
     49   1.4   mycroft #include <stdlib.h>
     50   1.8       cgd #include <string.h>
     51   1.8       cgd #include <unistd.h>
     52  1.19     lukem #include <util.h>
     53   1.1       cgd 
     54  1.58      fvdl #if defined(__i386__) || defined(__x86_64__)
     55  1.34      fvdl #include <machine/cpu.h>
     56  1.34      fvdl #endif
     57  1.34      fvdl 
     58   1.1       cgd #define LBUF 100
     59   1.1       cgd static char lbuf[LBUF];
     60   1.1       cgd 
     61   1.1       cgd /*
     62   1.1       cgd  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
     63   1.1       cgd  *	Copyright (c) 1989	Robert. V. Baron
     64   1.1       cgd  *	Created.
     65   1.1       cgd  */
     66   1.1       cgd 
     67  1.39      soda #ifndef _PATH_DEFDISK
     68  1.39      soda #define	_PATH_DEFDISK	"/dev/rwd0d"
     69  1.39      soda #endif
     70  1.39      soda 
     71  1.44     lukem const char *disk = _PATH_DEFDISK;
     72   1.1       cgd 
     73   1.1       cgd struct disklabel disklabel;		/* disk parameters */
     74   1.1       cgd 
     75   1.5   mycroft int cylinders, sectors, heads, cylindersectors, disksectors;
     76   1.1       cgd 
     77  1.61       dsl struct mbr_sector mboot;
     78   1.1       cgd 
     79  1.58      fvdl #if defined(__i386__) || defined(__x86_64__)
     80  1.50  christos #define OPTIONS			"0123BSafilus:b:c:"
     81  1.34      fvdl #else
     82  1.50  christos #define OPTIONS			"0123Safilus:b:c:"
     83  1.30        ws #endif
     84  1.30        ws 
     85   1.1       cgd #define ACTIVE 0x80
     86   1.1       cgd 
     87   1.5   mycroft int dos_cylinders;
     88   1.1       cgd int dos_heads;
     89   1.1       cgd int dos_sectors;
     90   1.5   mycroft int dos_cylindersectors;
     91   1.1       cgd 
     92   1.4   mycroft #define DOSSECT(s,c)	(((s) & 0x3f) | (((c) >> 2) & 0xc0))
     93   1.4   mycroft #define DOSCYL(c)	((c) & 0xff)
     94  1.28        ws 
     95  1.28        ws #define	MAXCYL	1024
     96   1.4   mycroft int partition = -1;
     97   1.4   mycroft 
     98   1.4   mycroft int a_flag;		/* set active partition */
     99  1.28        ws int i_flag;		/* init bootcode */
    100   1.4   mycroft int u_flag;		/* update partition data */
    101  1.15      phil int sh_flag;		/* Output data as shell defines */
    102  1.15      phil int f_flag;		/* force --not interactive */
    103  1.15      phil int s_flag;		/* set id,offset,size */
    104  1.16      phil int b_flag;		/* Set cyl, heads, secs (as c/h/s) */
    105  1.34      fvdl int B_flag;		/* Edit/install bootselect code */
    106  1.16      phil int b_cyl, b_head, b_sec;  /* b_flag values. */
    107  1.34      fvdl int bootsel_modified;
    108   1.4   mycroft 
    109  1.30        ws unsigned char bootcode[8192];	/* maximum size of bootcode */
    110  1.34      fvdl unsigned char tempcode[8192];
    111  1.30        ws int bootsize;		/* actual size of bootcode */
    112  1.30        ws 
    113  1.43     lukem 
    114  1.25  drochner static char reserved[] = "reserved";
    115  1.25  drochner 
    116   1.4   mycroft struct part_type {
    117  1.44     lukem 	int		 type;
    118  1.44     lukem 	const char	*name;
    119   1.4   mycroft } part_types[] = {
    120   1.4   mycroft 	{0x00, "unused"},
    121   1.4   mycroft 	{0x01, "Primary DOS with 12 bit FAT"},
    122   1.4   mycroft 	{0x02, "XENIX / filesystem"},
    123   1.4   mycroft 	{0x03, "XENIX /usr filesystem"},
    124  1.13     perry 	{0x04, "Primary DOS with 16 bit FAT <32M"},
    125  1.25  drochner 	{0x05, "Extended partition"},
    126  1.13     perry 	{0x06, "Primary 'big' DOS, 16-bit FAT (> 32MB)"},
    127  1.13     perry 	{0x07, "OS/2 HPFS or NTFS or QNX2 or Advanced UNIX"},
    128  1.25  drochner 	{0x08, "AIX filesystem or OS/2 (thru v1.3) or DELL multiple drives"
    129  1.25  drochner 	       "or Commodore DOS or SplitDrive"},
    130   1.4   mycroft 	{0x09, "AIX boot partition or Coherent"},
    131  1.13     perry 	{0x0A, "OS/2 Boot Manager or Coherent swap or OPUS"},
    132  1.25  drochner 	{0x0b, "Primary DOS with 32 bit FAT"},
    133  1.25  drochner 	{0x0c, "Primary DOS with 32 bit FAT - LBA"},
    134  1.25  drochner 	{0x0d, "Type 7??? - LBA"},
    135  1.25  drochner 	{0x0E, "DOS (16-bit FAT) - LBA"},
    136  1.25  drochner 	{0x0F, "Ext. partition - LBA"},
    137   1.4   mycroft 	{0x10, "OPUS"},
    138  1.13     perry 	{0x11, "OS/2 BM: hidden DOS 12-bit FAT"},
    139  1.13     perry 	{0x12, "Compaq diagnostics"},
    140  1.25  drochner 	{0x14, "OS/2 BM: hidden DOS 16-bit FAT <32M or Novell DOS 7.0 bug"},
    141  1.13     perry 	{0x16, "OS/2 BM: hidden DOS 16-bit FAT >=32M"},
    142  1.13     perry 	{0x17, "OS/2 BM: hidden IFS"},
    143  1.13     perry 	{0x18, "AST Windows swapfile"},
    144  1.25  drochner 	{0x19, "Willowtech Photon coS"},
    145  1.25  drochner 	{0x1e, "hidden FAT95"},
    146  1.25  drochner 	{0x20, "Willowsoft OFS1"},
    147  1.25  drochner 	{0x21, reserved},
    148  1.25  drochner 	{0x23, reserved},
    149  1.13     perry 	{0x24, "NEC DOS"},
    150  1.25  drochner 	{0x26, reserved},
    151  1.25  drochner 	{0x31, reserved},
    152  1.25  drochner 	{0x33, reserved},
    153  1.25  drochner 	{0x34, reserved},
    154  1.25  drochner 	{0x36, reserved},
    155  1.25  drochner 	{0x38, "Theos"},
    156  1.13     perry 	{0x3C, "PartitionMagic recovery"},
    157  1.25  drochner 	{0x40, "VENIX 286 or LynxOS"},
    158  1.25  drochner 	{0x41, "Linux/MINIX (sharing disk with DRDOS) or Personal RISC boot"},
    159  1.13     perry 	{0x42, "SFS or Linux swap (sharing disk with DRDOS)"},
    160  1.13     perry 	{0x43, "Linux native (sharing disk with DRDOS)"},
    161  1.41       wiz 	{0x4D, "QNX4.x"},
    162  1.41       wiz 	{0x4E, "QNX4.x 2nd part"},
    163  1.41       wiz 	{0x4F, "QNX4.x 3rd part"},
    164  1.13     perry 	{0x50, "DM (disk manager)"},
    165  1.13     perry 	{0x51, "DM6 Aux1 (or Novell)"},
    166   1.4   mycroft 	{0x52, "CP/M or Microport SysV/AT"},
    167  1.13     perry 	{0x53, "DM6 Aux3"},
    168  1.25  drochner 	{0x54, "DM6 DDO"},
    169  1.13     perry 	{0x55, "EZ-Drive (disk manager)"},
    170  1.13     perry 	{0x56, "Golden Bow (disk manager)"},
    171  1.13     perry 	{0x5C, "Priam Edisk (disk manager)"},
    172  1.13     perry 	{0x61, "SpeedStor"},
    173  1.25  drochner 	{0x63, "GNU HURD or Mach or Sys V/386 (such as ISC UNIX) or MtXinu"},
    174  1.25  drochner 	{0x64, "Novell Netware 2.xx or Speedstore"},
    175   1.4   mycroft 	{0x65, "Novell Netware 3.xx"},
    176  1.25  drochner 	{0x66, "Novell 386 Netware"},
    177  1.25  drochner 	{0x67, "Novell"},
    178  1.25  drochner 	{0x68, "Novell"},
    179  1.25  drochner 	{0x69, "Novell"},
    180  1.13     perry 	{0x70, "DiskSecure Multi-Boot"},
    181  1.25  drochner 	{0x71, reserved},
    182  1.25  drochner 	{0x73, reserved},
    183  1.25  drochner 	{0x74, reserved},
    184  1.13     perry 	{0x75, "PC/IX"},
    185  1.25  drochner 	{0x76, reserved},
    186  1.13     perry 	{0x80, "MINIX until 1.4a"},
    187  1.13     perry 	{0x81, "MINIX since 1.4b, early Linux, Mitac dmgr"},
    188  1.25  drochner 	{0x82, "Linux swap or Prime or Solaris"},
    189  1.13     perry 	{0x83, "Linux native"},
    190  1.13     perry 	{0x84, "OS/2 hidden C: drive"},
    191  1.13     perry 	{0x85, "Linux extended"},
    192  1.25  drochner 	{0x86, "NT FAT volume set"},
    193  1.25  drochner 	{0x87, "NTFS volume set or HPFS mirrored"},
    194   1.4   mycroft 	{0x93, "Amoeba filesystem"},
    195   1.4   mycroft 	{0x94, "Amoeba bad block table"},
    196  1.25  drochner 	{0x99, "Mylex EISA SCSI"},
    197  1.25  drochner 	{0x9f, "BSDI?"},
    198  1.13     perry 	{0xA0, "IBM Thinkpad hibernation"},
    199  1.25  drochner 	{0xa1, reserved},
    200  1.25  drochner 	{0xa3, reserved},
    201  1.25  drochner 	{0xa4, reserved},
    202  1.25  drochner 	{0xA5, "FreeBSD or 386BSD or old NetBSD"},
    203  1.13     perry 	{0xA6, "OpenBSD"},
    204  1.13     perry 	{0xA7, "NeXTSTEP 486"},
    205  1.49       dbj 	{0xa8, "Apple UFS"},
    206  1.25  drochner 	{0xa9, "NetBSD"},
    207  1.49       dbj 	{0xab, "Apple Boot"},
    208  1.55       dbj 	{0xaf, "Apple HFS"},
    209  1.25  drochner 	{0xb1, reserved},
    210  1.25  drochner 	{0xb3, reserved},
    211  1.25  drochner 	{0xb4, reserved},
    212  1.25  drochner 	{0xb6, reserved},
    213   1.4   mycroft 	{0xB7, "BSDI BSD/386 filesystem"},
    214   1.4   mycroft 	{0xB8, "BSDI BSD/386 swap"},
    215  1.25  drochner 	{0xc0, "CTOS"},
    216  1.13     perry 	{0xC1, "DRDOS/sec (FAT-12)"},
    217  1.13     perry 	{0xC4, "DRDOS/sec (FAT-16, < 32M)"},
    218  1.13     perry 	{0xC6, "DRDOS/sec (FAT-16, >= 32M)"},
    219  1.25  drochner 	{0xC7, "Syrinx (Cyrnix?) or HPFS disabled"},
    220  1.25  drochner 	{0xd8, "CP/M 86"},
    221  1.13     perry 	{0xDB, "CP/M or Concurrent CP/M or Concurrent DOS or CTOS"},
    222  1.13     perry 	{0xE1, "DOS access or SpeedStor 12-bit FAT extended partition"},
    223  1.25  drochner 	{0xE3, "DOS R/O or SpeedStor or Storage Dimensions"},
    224  1.13     perry 	{0xE4, "SpeedStor 16-bit FAT extended partition < 1024 cyl."},
    225  1.25  drochner 	{0xe5, reserved},
    226  1.25  drochner 	{0xe6, reserved},
    227  1.33     perry 	{0xeb, "BeOS"},
    228  1.25  drochner 	{0xF1, "SpeedStor or Storage Dimensions"},
    229   1.4   mycroft 	{0xF2, "DOS 3.3+ Secondary"},
    230  1.25  drochner 	{0xf3, reserved},
    231  1.25  drochner 	{0xF4, "SpeedStor large partition or Storage Dimensions"},
    232  1.25  drochner 	{0xf6, reserved},
    233  1.25  drochner 	{0xFE, "SpeedStor >1024 cyl. or LANstep or IBM PS/2 IML"},
    234  1.13     perry 	{0xFF, "Xenix Bad Block Table"},
    235   1.1       cgd };
    236   1.1       cgd 
    237  1.50  christos #define KNOWN_SYSIDS	(sizeof(part_types)/sizeof(part_types[0]))
    238  1.50  christos 
    239  1.43     lukem void	usage(void);
    240  1.43     lukem void	print_s0(int);
    241  1.43     lukem void	print_part(int);
    242  1.46     lukem void	print_mbr_partition(struct mbr_partition *, off_t, off_t, int);
    243  1.43     lukem int	read_boot(const char *, void *, size_t);
    244  1.43     lukem void	init_sector0(int, int);
    245  1.43     lukem void	intuit_translated_geometry(void);
    246  1.43     lukem void	get_geometry(void);
    247  1.44     lukem void	get_diskname(const char *, char *, size_t);
    248  1.43     lukem int	try_heads(quad_t, quad_t, quad_t, quad_t, quad_t, quad_t, quad_t,
    249  1.50  christos     quad_t);
    250  1.43     lukem int	try_sectors(quad_t, quad_t, quad_t, quad_t, quad_t);
    251  1.43     lukem void	change_part(int, int, int, int);
    252  1.43     lukem void	print_params(void);
    253  1.43     lukem void	change_active(int);
    254  1.43     lukem void	get_params_to_use(void);
    255  1.43     lukem void	dos(int, unsigned char *, unsigned char *, unsigned char *);
    256  1.43     lukem int	open_disk(int);
    257  1.44     lukem int	read_disk(off_t, void *);
    258  1.44     lukem int	write_disk(off_t, void *);
    259  1.43     lukem int	get_params(void);
    260  1.61       dsl int	read_s0(off_t, struct mbr_sector *);
    261  1.43     lukem int	write_s0(void);
    262  1.43     lukem int	yesno(const char *);
    263  1.43     lukem void	decimal(const char *, int *);
    264  1.43     lukem int	type_match(const void *, const void *);
    265  1.43     lukem const char *get_type(int);
    266  1.43     lukem int	get_mapping(int, int *, int *, int *, unsigned long *);
    267  1.58      fvdl #if defined(__i386__) || defined(__x86_64__)
    268  1.43     lukem void	configure_bootsel(void);
    269  1.34      fvdl #endif
    270  1.16      phil 
    271  1.43     lukem static unsigned short getshort(void *);
    272  1.43     lukem static void putshort(void *p, unsigned short);
    273  1.43     lukem static unsigned long getlong(void *);
    274  1.43     lukem static void putlong(void *, unsigned long);
    275  1.16      phil 
    276  1.16      phil 
    277  1.43     lukem int	main(int, char *[]);
    278   1.1       cgd 
    279   1.4   mycroft int
    280  1.43     lukem main(int argc, char *argv[])
    281   1.1       cgd {
    282  1.52   thorpej 	int ch, part, mib[2];
    283  1.52   thorpej 	size_t len;
    284  1.48     lukem 	char *root_device;
    285   1.1       cgd 
    286  1.15      phil 	int csysid, cstart, csize;	/* For the b_flag. */
    287  1.15      phil 
    288  1.48     lukem 	mib[0] = CTL_KERN;
    289  1.48     lukem 	mib[1] = KERN_ROOT_DEVICE;
    290  1.48     lukem 	if (sysctl(mib, 2, NULL, &len, NULL, 0) != -1 &&
    291  1.48     lukem 	    (root_device = malloc(len)) != NULL &&
    292  1.48     lukem 	    sysctl(mib, 2, root_device, &len, NULL, 0) != -1)
    293  1.48     lukem 		disk = root_device;
    294  1.48     lukem 
    295  1.16      phil 	a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
    296  1.15      phil 	csysid = cstart = csize = 0;
    297  1.34      fvdl 	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
    298   1.4   mycroft 		switch (ch) {
    299   1.4   mycroft 		case '0':
    300   1.4   mycroft 			partition = 0;
    301   1.4   mycroft 			break;
    302   1.4   mycroft 		case '1':
    303   1.4   mycroft 			partition = 1;
    304   1.4   mycroft 			break;
    305   1.4   mycroft 		case '2':
    306   1.4   mycroft 			partition = 2;
    307   1.4   mycroft 			break;
    308   1.4   mycroft 		case '3':
    309   1.4   mycroft 			partition = 3;
    310   1.4   mycroft 			break;
    311  1.58      fvdl #if defined(__i386__) || defined(__x86_64__)
    312  1.34      fvdl 		case 'B':
    313  1.34      fvdl 			B_flag = 1;
    314  1.34      fvdl 			break;
    315  1.34      fvdl #endif
    316  1.15      phil 		case 'S':
    317  1.15      phil 			sh_flag = 1;
    318  1.15      phil 			break;
    319   1.4   mycroft 		case 'a':
    320   1.4   mycroft 			a_flag = 1;
    321   1.4   mycroft 			break;
    322  1.15      phil 		case 'f':
    323  1.15      phil 			f_flag = 1;
    324  1.15      phil 			break;
    325   1.4   mycroft 		case 'i':
    326   1.4   mycroft 			i_flag = 1;
    327  1.15      phil 			break;
    328  1.50  christos 		case 'l':
    329  1.50  christos 			for (len = 0; len < KNOWN_SYSIDS; len++)
    330  1.56   jdarrow 				printf("%03d %s\n", part_types[len].type,
    331  1.52   thorpej 				part_types[len].name);
    332  1.50  christos 			return 0;
    333   1.4   mycroft 		case 'u':
    334   1.4   mycroft 			u_flag = 1;
    335   1.1       cgd 			break;
    336  1.14      phil 		case 's':
    337  1.14      phil 			s_flag = 1;
    338  1.50  christos 			if (sscanf(optarg, "%d/%d/%d", &csysid, &cstart,
    339  1.50  christos 			    &csize) != 3)
    340  1.54     soren 				errx(1, "Bad argument to the -s flag.");
    341  1.16      phil 			break;
    342  1.16      phil 		case 'b':
    343  1.16      phil 			b_flag = 1;
    344  1.50  christos 			if (sscanf(optarg, "%d/%d/%d", &b_cyl, &b_head,
    345  1.50  christos 			    &b_sec) != 3)
    346  1.54     soren 				errx(1, "Bad argument to the -b flag.");
    347  1.28        ws 			if (b_cyl > MAXCYL)
    348  1.28        ws 				b_cyl = MAXCYL;
    349  1.14      phil 			break;
    350  1.30        ws 		case 'c':
    351  1.34      fvdl 			bootsize = read_boot(optarg, bootcode, sizeof bootcode);
    352  1.30        ws 			break;
    353   1.4   mycroft 		default:
    354   1.4   mycroft 			usage();
    355   1.1       cgd 		}
    356   1.4   mycroft 	argc -= optind;
    357   1.4   mycroft 	argv += optind;
    358   1.1       cgd 
    359  1.15      phil 	if (sh_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
    360  1.15      phil 		usage();
    361  1.15      phil 
    362  1.34      fvdl 	if (B_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
    363  1.34      fvdl 		usage();
    364  1.34      fvdl 
    365  1.15      phil 	if (partition == -1 && s_flag) {
    366  1.50  christos 		warnx("-s flag requires a partition selected.");
    367  1.14      phil 		usage();
    368  1.15      phil 	}
    369  1.15      phil 
    370   1.1       cgd 	if (argc > 0)
    371   1.1       cgd 		disk = argv[0];
    372   1.4   mycroft 
    373  1.34      fvdl 	if (open_disk(B_flag || a_flag || i_flag || u_flag) < 0)
    374   1.1       cgd 		exit(1);
    375   1.1       cgd 
    376  1.44     lukem 	if (read_s0(0, &mboot))
    377  1.28        ws 		init_sector0(sectors > 63 ? 63 : sectors, 1);
    378  1.11   ghudson 
    379  1.58      fvdl #if defined(__i386__) || defined(__x86_64__)
    380  1.34      fvdl 	get_geometry();
    381  1.34      fvdl #else
    382  1.11   ghudson 	intuit_translated_geometry();
    383  1.34      fvdl #endif
    384  1.15      phil 
    385  1.15      phil 
    386  1.16      phil 	if ((i_flag || u_flag) && (!f_flag || b_flag))
    387   1.1       cgd 		get_params_to_use();
    388   1.1       cgd 
    389  1.15      phil 	if (i_flag)
    390  1.28        ws 		init_sector0(dos_sectors > 63 ? 63 : dos_sectors, 0);
    391  1.15      phil 
    392  1.15      phil 	/* Do the update stuff! */
    393  1.15      phil 	if (u_flag) {
    394  1.15      phil 		if (!f_flag)
    395  1.34      fvdl 			printf("Partition table:\n");
    396  1.34      fvdl 		if (partition == -1)
    397  1.32   thorpej 			for (part = 0; part < NMBRPART; part++)
    398  1.15      phil 				change_part(part,-1, -1, -1);
    399  1.15      phil 		else
    400  1.15      phil 			change_part(partition, csysid, cstart, csize);
    401   1.4   mycroft 	} else
    402  1.15      phil 		if (!i_flag)
    403  1.15      phil 			print_s0(partition);
    404   1.1       cgd 
    405  1.15      phil 	if (a_flag)
    406   1.1       cgd 		change_active(partition);
    407   1.1       cgd 
    408  1.58      fvdl #if defined(__i386__) || defined(__x86_64__)
    409  1.34      fvdl 	if (B_flag) {
    410  1.34      fvdl 		configure_bootsel();
    411  1.34      fvdl 		if (B_flag && bootsel_modified)
    412  1.34      fvdl 			write_s0();
    413  1.34      fvdl 	}
    414  1.34      fvdl #endif
    415  1.34      fvdl 
    416  1.15      phil 	if (u_flag || a_flag || i_flag) {
    417  1.15      phil 		if (!f_flag) {
    418  1.34      fvdl 			printf("\nWe haven't written the MBR back to disk "
    419  1.15      phil 			       "yet.  This is your last chance.\n");
    420  1.15      phil 			print_s0(-1);
    421  1.15      phil 			if (yesno("Should we write new partition table?"))
    422  1.15      phil 				write_s0();
    423  1.15      phil 		} else
    424   1.1       cgd 			write_s0();
    425   1.1       cgd 	}
    426   1.1       cgd 
    427   1.1       cgd 	exit(0);
    428   1.4   mycroft }
    429   1.1       cgd 
    430   1.4   mycroft void
    431  1.43     lukem usage(void)
    432   1.4   mycroft {
    433  1.43     lukem 
    434  1.50  christos 	(void)fprintf(stderr, "Usage: %s [-aiufBS] [-0|-1|-2|-3] "
    435  1.50  christos 	      "[-b cylinders/heads/sectors]\n"
    436  1.51  christos 	      "\t%s [-s id/start/size] [-c bootcode] [device]\n",
    437  1.50  christos 	      getprogname(), getprogname());
    438   1.4   mycroft 	exit(1);
    439   1.1       cgd }
    440   1.1       cgd 
    441   1.4   mycroft void
    442  1.43     lukem print_s0(int which)
    443   1.1       cgd {
    444   1.4   mycroft 	int part;
    445   1.1       cgd 
    446   1.1       cgd 	print_params();
    447  1.15      phil 	if (!sh_flag)
    448  1.34      fvdl 		printf("Partition table:\n");
    449   1.4   mycroft 	if (which == -1) {
    450  1.32   thorpej 		for (part = 0; part < NMBRPART; part++) {
    451  1.15      phil 			if (!sh_flag)
    452  1.15      phil 				printf("%d: ", part);
    453  1.15      phil 			print_part(part);
    454  1.15      phil 		}
    455   1.4   mycroft 	} else
    456   1.1       cgd 		print_part(which);
    457   1.1       cgd }
    458   1.1       cgd 
    459  1.43     lukem static unsigned short
    460  1.43     lukem getshort(void *p)
    461  1.12   thorpej {
    462  1.12   thorpej 	unsigned char *cp = p;
    463  1.12   thorpej 
    464  1.12   thorpej 	return cp[0] | (cp[1] << 8);
    465  1.12   thorpej }
    466  1.12   thorpej 
    467  1.43     lukem static void
    468  1.43     lukem putshort(void *p, unsigned short l)
    469  1.12   thorpej {
    470  1.12   thorpej 	unsigned char *cp = p;
    471  1.12   thorpej 
    472  1.12   thorpej 	*cp++ = l;
    473  1.12   thorpej 	*cp++ = l >> 8;
    474  1.12   thorpej }
    475  1.12   thorpej 
    476  1.43     lukem static unsigned long
    477  1.43     lukem getlong(void *p)
    478  1.12   thorpej {
    479  1.12   thorpej 	unsigned char *cp = p;
    480  1.12   thorpej 
    481  1.12   thorpej 	return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
    482  1.12   thorpej }
    483  1.12   thorpej 
    484  1.43     lukem static void
    485  1.43     lukem putlong(void *p, unsigned long l)
    486  1.12   thorpej {
    487  1.12   thorpej 	unsigned char *cp = p;
    488  1.12   thorpej 
    489  1.12   thorpej 	*cp++ = l;
    490  1.12   thorpej 	*cp++ = l >> 8;
    491  1.12   thorpej 	*cp++ = l >> 16;
    492  1.12   thorpej 	*cp++ = l >> 24;
    493  1.12   thorpej }
    494  1.12   thorpej 
    495   1.4   mycroft void
    496  1.43     lukem print_part(int part)
    497   1.1       cgd {
    498  1.32   thorpej 	struct mbr_partition *partp;
    499  1.14      phil 	int empty;
    500   1.1       cgd 
    501  1.61       dsl 	partp = &mboot.mbr_parts[part];
    502  1.34      fvdl 	empty = (partp->mbrp_typ == 0);
    503  1.14      phil 
    504  1.15      phil 	if (sh_flag) {
    505  1.14      phil 		if (empty) {
    506  1.14      phil 			printf("PART%dSIZE=0\n", part);
    507  1.14      phil 			return;
    508  1.14      phil 		}
    509  1.14      phil 
    510  1.32   thorpej 		printf("PART%dID=%d\n", part, partp->mbrp_typ);
    511  1.32   thorpej 		printf("PART%dSIZE=%ld\n", part, getlong(&partp->mbrp_size));
    512  1.32   thorpej 		printf("PART%dSTART=%ld\n", part, getlong(&partp->mbrp_start));
    513  1.32   thorpej 		printf("PART%dFLAG=0x%x\n", part, partp->mbrp_flag);
    514  1.32   thorpej 		printf("PART%dBCYL=%d\n", part, MBR_PCYL(partp->mbrp_scyl,
    515  1.50  christos 		    partp->mbrp_ssect));
    516  1.32   thorpej 		printf("PART%dBHEAD=%d\n", part, partp->mbrp_shd);
    517  1.32   thorpej 		printf("PART%dBSEC=%d\n", part, MBR_PSECT(partp->mbrp_ssect));
    518  1.32   thorpej 		printf("PART%dECYL=%d\n", part, MBR_PCYL(partp->mbrp_ecyl,
    519  1.50  christos 		    partp->mbrp_esect));
    520  1.32   thorpej 		printf("PART%dEHEAD=%d\n", part, partp->mbrp_ehd);
    521  1.32   thorpej 		printf("PART%dESEC=%d\n", part, MBR_PSECT(partp->mbrp_esect));
    522  1.14      phil 		return;
    523  1.14      phil 	}
    524  1.46     lukem 	print_mbr_partition(partp, 0, 0, 0);
    525  1.44     lukem }
    526  1.44     lukem 
    527  1.44     lukem void
    528  1.46     lukem print_mbr_partition(struct mbr_partition *partp,
    529  1.50  christos     off_t offset, off_t exoffset, int indent)
    530  1.44     lukem {
    531  1.44     lukem 	int	empty;
    532  1.44     lukem 	off_t	start;
    533  1.14      phil 
    534  1.44     lukem 	empty = (partp->mbrp_typ == 0);
    535  1.46     lukem 	if (MBR_IS_EXTENDED(partp->mbrp_typ))
    536  1.46     lukem 		start = (off_t)getlong(&partp->mbrp_start) + exoffset;
    537  1.46     lukem 	else
    538  1.46     lukem 		start = (off_t)getlong(&partp->mbrp_start) + offset;
    539  1.14      phil 	if (empty) {
    540   1.1       cgd 		printf("<UNUSED>\n");
    541   1.1       cgd 		return;
    542   1.1       cgd 	}
    543  1.44     lukem 	printf("sysid %d (%s)\n",
    544  1.44     lukem 	    partp->mbrp_typ, get_type(partp->mbrp_typ));
    545  1.44     lukem 	printf("%*s    start %lld, size %ld (%ld MB), flag 0x%x\n",
    546  1.44     lukem 	    indent, "",
    547  1.57      fvdl 	    (long long)start, getlong(&partp->mbrp_size),
    548  1.32   thorpej 	    getlong(&partp->mbrp_size) * 512 / (1024 * 1024), partp->mbrp_flag);
    549  1.44     lukem 	printf("%*s        beg: cylinder %4d, head %3d, sector %2d\n",
    550  1.44     lukem 	    indent, "",
    551  1.32   thorpej 	    MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect),
    552  1.32   thorpej 	    partp->mbrp_shd, MBR_PSECT(partp->mbrp_ssect));
    553  1.44     lukem 	printf("%*s        end: cylinder %4d, head %3d, sector %2d\n",
    554  1.44     lukem 	    indent, "",
    555  1.32   thorpej 	    MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect),
    556  1.32   thorpej 	    partp->mbrp_ehd, MBR_PSECT(partp->mbrp_esect));
    557  1.44     lukem 
    558  1.45     lukem 	if (MBR_IS_EXTENDED(partp->mbrp_typ)) {
    559  1.61       dsl 		struct mbr_sector	eboot;
    560  1.44     lukem 		int		part;
    561  1.44     lukem 
    562  1.44     lukem 		printf("%*s    Extended partition table:\n", indent, "");
    563  1.44     lukem 		if (read_s0(start, &eboot) == -1)
    564  1.44     lukem 			return;
    565  1.44     lukem 		indent += 8;
    566  1.46     lukem 		if (exoffset == 0)
    567  1.46     lukem 			exoffset = start;
    568  1.44     lukem 		for (part = 0; part < NMBRPART; part++) {
    569  1.44     lukem 			printf("%*s%d: ", indent, "", part);
    570  1.61       dsl 			print_mbr_partition(&eboot.mbr_parts[part],
    571  1.50  christos 			    start, exoffset, indent);
    572  1.44     lukem 		}
    573  1.44     lukem 	}
    574   1.1       cgd }
    575   1.1       cgd 
    576  1.34      fvdl int
    577  1.43     lukem read_boot(const char *name, void *buf, size_t len)
    578  1.30        ws {
    579  1.34      fvdl 	int bfd, ret;
    580  1.30        ws 	struct stat st;
    581  1.30        ws 
    582  1.34      fvdl 	if ((bfd = open(name, O_RDONLY)) < 0)
    583  1.30        ws 		err(1, "%s", name);
    584  1.34      fvdl 	if (fstat(bfd, &st) == -1)
    585  1.30        ws 		err(1, "%s", name);
    586  1.58      fvdl 	if (st.st_size > (off_t)len)
    587  1.30        ws 		errx(1, "%s: bootcode too large", name);
    588  1.34      fvdl 	ret = st.st_size;
    589  1.34      fvdl 	if (ret < 0x200)
    590  1.30        ws 		errx(1, "%s: bootcode too small", name);
    591  1.34      fvdl 	if (read(bfd, buf, len) != ret)
    592  1.30        ws 		err(1, "%s", name);
    593  1.34      fvdl 	close(bfd);
    594  1.30        ws 
    595  1.30        ws 	/*
    596  1.30        ws 	 * Do some sanity checking here
    597  1.30        ws 	 */
    598  1.32   thorpej 	if (getshort(bootcode + MBR_MAGICOFF) != MBR_MAGIC)
    599  1.30        ws 		errx(1, "%s: invalid magic", name);
    600  1.34      fvdl 	ret = (ret + 0x1ff) / 0x200;
    601  1.34      fvdl 	ret *= 0x200;
    602  1.34      fvdl 	return ret;
    603  1.30        ws }
    604  1.30        ws 
    605  1.30        ws void
    606  1.43     lukem init_sector0(int start, int dopart)
    607   1.1       cgd {
    608  1.15      phil 	int i;
    609  1.15      phil 
    610  1.30        ws #ifdef	DEFAULT_BOOTCODE
    611  1.30        ws 	if (!bootsize)
    612  1.61       dsl 		bootsize = read_boot(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE,
    613  1.61       dsl 		    bootcode, sizeof bootcode);
    614  1.30        ws #endif
    615  1.30        ws 
    616  1.61       dsl 	memcpy(mboot.mbr_bootinst, bootcode, sizeof(mboot.mbr_bootinst));
    617  1.61       dsl 	putshort(&mboot.mbr_signature, MBR_MAGIC);
    618  1.15      phil 
    619  1.28        ws 	if (dopart)
    620  1.50  christos 		for (i = 0; i < 4; i++)
    621  1.61       dsl 			memset(&mboot.mbr_parts[i], 0, sizeof(mboot.mbr_parts[i]));
    622  1.34      fvdl 
    623  1.34      fvdl }
    624   1.1       cgd 
    625  1.58      fvdl #if defined(__i386__) || defined(__x86_64__)
    626  1.34      fvdl 
    627  1.34      fvdl void
    628  1.44     lukem get_diskname(const char *fullname, char *diskname, size_t size)
    629  1.34      fvdl {
    630  1.44     lukem 	const char *p, *p2;
    631  1.34      fvdl 	size_t len;
    632  1.34      fvdl 
    633  1.34      fvdl 	p = strrchr(fullname, '/');
    634  1.34      fvdl 	if (p == NULL)
    635  1.34      fvdl 		p = fullname;
    636  1.34      fvdl 	else
    637  1.34      fvdl 		p++;
    638  1.34      fvdl 
    639  1.34      fvdl 	if (*p == 0) {
    640  1.34      fvdl 		strncpy(diskname, fullname, size - 1);
    641  1.34      fvdl 		diskname[size - 1] = '\0';
    642  1.34      fvdl 		return;
    643  1.34      fvdl 	}
    644  1.34      fvdl 
    645  1.34      fvdl 	if (*p == 'r')
    646  1.34      fvdl 		p++;
    647  1.34      fvdl 
    648  1.34      fvdl 	for (p2 = p; *p2 != 0; p2++)
    649  1.34      fvdl 		if (isdigit(*p2))
    650  1.34      fvdl 			break;
    651  1.34      fvdl 	if (*p2 == 0) {
    652  1.34      fvdl 		/* XXX invalid diskname? */
    653  1.34      fvdl 		strncpy(diskname, fullname, size - 1);
    654  1.34      fvdl 		diskname[size - 1] = '\0';
    655  1.34      fvdl 		return;
    656  1.34      fvdl 	}
    657  1.34      fvdl 	while (isdigit(*p2))
    658  1.34      fvdl 		p2++;
    659  1.34      fvdl 
    660  1.34      fvdl 	len = p2 - p;
    661  1.34      fvdl 	if (len > size) {
    662  1.34      fvdl 		/* XXX */
    663  1.34      fvdl 		strncpy(diskname, fullname, size - 1);
    664  1.34      fvdl 		diskname[size - 1] = '\0';
    665  1.34      fvdl 		return;
    666  1.34      fvdl 	}
    667  1.34      fvdl 
    668  1.34      fvdl 	strncpy(diskname, p, len);
    669  1.34      fvdl 	diskname[len] = 0;
    670  1.34      fvdl }
    671  1.34      fvdl 
    672  1.34      fvdl void
    673  1.43     lukem get_geometry(void)
    674  1.34      fvdl {
    675  1.34      fvdl 	int mib[2], i;
    676  1.34      fvdl 	size_t len;
    677  1.34      fvdl 	struct disklist *dl;
    678  1.34      fvdl 	struct biosdisk_info *bip;
    679  1.34      fvdl 	struct nativedisk_info *nip;
    680  1.34      fvdl 	char diskname[8];
    681  1.34      fvdl 
    682  1.34      fvdl 	mib[0] = CTL_MACHDEP;
    683  1.34      fvdl 	mib[1] = CPU_DISKINFO;
    684  1.34      fvdl 	if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) {
    685  1.34      fvdl 		intuit_translated_geometry();
    686  1.34      fvdl 		return;
    687  1.34      fvdl 	}
    688  1.34      fvdl 	dl = (struct disklist *) malloc(len);
    689  1.34      fvdl 	sysctl(mib, 2, dl, &len, NULL, 0);
    690  1.34      fvdl 
    691  1.34      fvdl 	get_diskname(disk, diskname, sizeof diskname);
    692  1.34      fvdl 
    693  1.34      fvdl 	for (i = 0; i < dl->dl_nnativedisks; i++) {
    694  1.34      fvdl 		nip = &dl->dl_nativedisks[i];
    695  1.34      fvdl 		if (strcmp(diskname, nip->ni_devname))
    696  1.34      fvdl 			continue;
    697  1.34      fvdl 		/*
    698  1.34      fvdl 		 * XXX listing possible matches is better. This is ok
    699  1.34      fvdl 		 * for now because the user has a chance to change
    700  1.34      fvdl 		 * it later.
    701  1.34      fvdl 		 */
    702  1.34      fvdl 		if (nip->ni_nmatches != 0) {
    703  1.34      fvdl 			bip = &dl->dl_biosdisks[nip->ni_biosmatches[0]];
    704  1.34      fvdl 			dos_cylinders = bip->bi_cyl;
    705  1.34      fvdl 			dos_heads = bip->bi_head;
    706  1.34      fvdl 			dos_sectors = bip->bi_sec;
    707  1.34      fvdl 			dos_cylindersectors = bip->bi_head * bip->bi_sec;
    708  1.34      fvdl 			return;
    709  1.34      fvdl 		}
    710  1.34      fvdl 	}
    711  1.34      fvdl 	/* Allright, allright, make a stupid guess.. */
    712  1.34      fvdl 	intuit_translated_geometry();
    713  1.11   ghudson }
    714  1.11   ghudson 
    715  1.34      fvdl void
    716  1.43     lukem configure_bootsel(void)
    717  1.34      fvdl {
    718  1.34      fvdl 	struct mbr_bootsel *mbs =
    719  1.61       dsl 	    (struct mbr_bootsel *)&mboot.mbr_bootinst[MBR_BOOTSELOFF];
    720  1.34      fvdl 	int i, nused, firstpart = -1, item;
    721  1.38     soren 	char desc[PARTNAMESIZE + 2], *p;
    722  1.34      fvdl 	int timo, entry_changed = 0;
    723  1.34      fvdl 
    724  1.38     soren 	for (i = nused = 0; i < NMBRPART; ++i) {
    725  1.61       dsl 		if (mboot.mbr_parts[i].mbrp_typ != 0) {
    726  1.34      fvdl 			if (firstpart == -1)
    727  1.34      fvdl 				firstpart = i;
    728  1.34      fvdl 			nused++;
    729  1.34      fvdl 		}
    730  1.34      fvdl 	}
    731  1.34      fvdl 
    732  1.34      fvdl 	if (nused == 0) {
    733  1.50  christos 		warnx("No used partitions found. Partition the disk first.");
    734  1.34      fvdl 		return;
    735  1.34      fvdl 	}
    736  1.34      fvdl 
    737  1.61       dsl 	if (mbs->mbrb_magic != MBR_MAGIC) {
    738  1.34      fvdl 		if (!yesno("Bootselector not yet installed. Install it now?")) {
    739  1.50  christos 			warnx("Bootselector not installed.");
    740  1.34      fvdl 			return;
    741  1.34      fvdl 		}
    742  1.61       dsl 		bootsize = read_boot(DEFAULT_BOOTDIR "/" DEFAULT_BOOTSELCODE,
    743  1.61       dsl 		    bootcode, sizeof bootcode);
    744  1.61       dsl 		memcpy(mboot.mbr_bootinst, bootcode, sizeof(mboot.mbr_bootinst));
    745  1.34      fvdl 		bootsel_modified = 1;
    746  1.61       dsl 		mbs->mbrb_flags |= BFL_SELACTIVE;
    747  1.34      fvdl 	} else {
    748  1.61       dsl 		if (mbs->mbrb_flags & BFL_SELACTIVE) {
    749  1.59      jmmv 			printf("The bootselector is installed and active.\n");
    750  1.34      fvdl 			if (!yesno("Do you want to change its settings?")) {
    751  1.34      fvdl 				if (yesno("Do you want to deactivate it?")) {
    752  1.61       dsl 					mbs->mbrb_flags &= ~BFL_SELACTIVE;
    753  1.34      fvdl 					bootsel_modified = 1;
    754  1.34      fvdl 					goto done;
    755  1.34      fvdl 				}
    756  1.34      fvdl 				return;
    757  1.34      fvdl 			}
    758  1.34      fvdl 		} else {
    759  1.59      jmmv 			printf("The bootselector is installed but not active.\n");
    760  1.34      fvdl 			if (yesno("Do you want to activate it?")) {
    761  1.61       dsl 				mbs->mbrb_flags |= BFL_SELACTIVE;
    762  1.34      fvdl 				bootsel_modified = 1;
    763  1.34      fvdl 			}
    764  1.34      fvdl 			if (!yesno("Do you want to change its settings?"))
    765  1.34      fvdl 				goto done;
    766  1.34      fvdl 		}
    767  1.34      fvdl 	}
    768  1.34      fvdl 
    769  1.34      fvdl 	printf("\n\nPartition table:\n");
    770  1.34      fvdl 	for (i = 0; i < NMBRPART; i++) {
    771  1.34      fvdl 		printf("%d: ", i);
    772  1.34      fvdl 		print_part(i);
    773  1.34      fvdl 	}
    774  1.34      fvdl 
    775  1.34      fvdl 	printf("\n\nCurrent boot selection menu option names:\n");
    776  1.34      fvdl 	for (i = 0; i < NMBRPART; i++) {
    777  1.61       dsl 		if (mbs->mbrb_nametab[i][0] != 0)
    778  1.61       dsl 			printf("%d: %s\n", i, &mbs->mbrb_nametab[i][0]);
    779  1.34      fvdl 		else
    780  1.38     soren 			printf("%d: <UNUSED>\n", i);
    781  1.34      fvdl 	}
    782  1.34      fvdl 	printf("\n");
    783  1.34      fvdl 
    784  1.34      fvdl 	item = firstpart;
    785  1.34      fvdl 
    786  1.34      fvdl editentries:
    787  1.34      fvdl 	while (1) {
    788  1.34      fvdl 		decimal("Change which entry (-1 quits)?", &item);
    789  1.34      fvdl 		if (item == -1)
    790  1.34      fvdl 			break;
    791  1.34      fvdl 		if (item < 0 || item >= NMBRPART) {
    792  1.34      fvdl 			printf("Invalid entry number\n");
    793  1.38     soren 			item = -1;
    794  1.34      fvdl 			continue;
    795  1.34      fvdl 		}
    796  1.61       dsl 		if (mboot.mbr_parts[item].mbrp_typ == 0) {
    797  1.38     soren 			printf("The partition entry is unused\n");
    798  1.38     soren 			item = -1;
    799  1.34      fvdl 			continue;
    800  1.34      fvdl 		}
    801  1.34      fvdl 
    802  1.34      fvdl 		printf("Enter descriptions (max. 8 characters): ");
    803  1.34      fvdl 		rewind(stdin);
    804  1.53      yamt 		if (!fgets(desc, PARTNAMESIZE + 1, stdin))
    805  1.53      yamt 			errx(1, "EOF");
    806  1.38     soren 		fpurge(stdin);
    807  1.34      fvdl 		p = strchr(desc, '\n');
    808  1.34      fvdl 		if (p != NULL)
    809  1.34      fvdl 			*p = 0;
    810  1.61       dsl 		strcpy(&mbs->mbrb_nametab[item][0], desc);
    811  1.34      fvdl 		entry_changed = bootsel_modified = 1;
    812  1.38     soren 
    813  1.60      fvdl 		if (item < NMBRPART -1)
    814  1.60      fvdl 			item++;
    815  1.60      fvdl 		else
    816  1.60      fvdl 			item = -1;
    817  1.34      fvdl 	}
    818  1.34      fvdl 
    819  1.34      fvdl 	if (entry_changed)
    820  1.34      fvdl 		printf("Boot selection menu option names are now:\n");
    821  1.34      fvdl 
    822  1.34      fvdl 	firstpart = -1;
    823  1.34      fvdl 	for (i = 0; i < NMBRPART; i++) {
    824  1.61       dsl 		if (mbs->mbrb_nametab[i][0] != 0) {
    825  1.34      fvdl 			firstpart = i;
    826  1.34      fvdl 			if (entry_changed)
    827  1.61       dsl 				printf("%d: %s\n", i, &mbs->mbrb_nametab[i][0]);
    828  1.34      fvdl 		} else {
    829  1.34      fvdl 			if (entry_changed)
    830  1.38     soren 				printf("%d: <UNUSED>\n", i);
    831  1.34      fvdl 		}
    832  1.34      fvdl 	}
    833  1.34      fvdl 	if (entry_changed)
    834  1.34      fvdl 		printf("\n");
    835  1.34      fvdl 
    836  1.34      fvdl 	if (firstpart == -1) {
    837  1.34      fvdl 		printf("All menu entries are now inactive.\n");
    838  1.34      fvdl 		if (!yesno("Are you sure about this?"))
    839  1.34      fvdl 			goto editentries;
    840  1.34      fvdl 	} else {
    841  1.61       dsl 		if (!(mbs->mbrb_flags & BFL_SELACTIVE)) {
    842  1.34      fvdl 			printf("The bootselector is not yet active.\n");
    843  1.34      fvdl 			if (yesno("Activate it now?"))
    844  1.61       dsl 				mbs->mbrb_flags |= BFL_SELACTIVE;
    845  1.34      fvdl 		}
    846  1.34      fvdl 	}
    847  1.34      fvdl 
    848  1.37   thorpej 	/* bootsel is dirty from here on out. */
    849  1.37   thorpej 	bootsel_modified = 1;
    850  1.37   thorpej 
    851  1.34      fvdl 	/* The timeout value is in ticks, 18.2 Hz. Avoid using floats. */
    852  1.61       dsl 	timo = ((1000 * mbs->mbrb_timeo) / 18200);
    853  1.34      fvdl 	do {
    854  1.34      fvdl 		decimal("Timeout value", &timo);
    855  1.34      fvdl 	} while (timo < 0 || timo > 3600);
    856  1.61       dsl 	mbs->mbrb_timeo = (u_int16_t)((timo * 18200) / 1000);
    857  1.34      fvdl 
    858  1.34      fvdl 	printf("Select the default boot option. Options are:\n\n");
    859  1.34      fvdl 	for (i = 0; i < NMBRPART; i++) {
    860  1.61       dsl 		if (mbs->mbrb_nametab[i][0] != 0)
    861  1.61       dsl 			printf("%d: %s\n", i, &mbs->mbrb_nametab[i][0]);
    862  1.34      fvdl 	}
    863  1.34      fvdl 	for (i = 4; i < 10; i++)
    864  1.34      fvdl 		printf("%d: Harddisk %d\n", i, i - 4);
    865  1.34      fvdl 	printf("10: The first active partition\n");
    866  1.34      fvdl 
    867  1.61       dsl 	if (mbs->mbrb_defkey == SCAN_ENTER)
    868  1.34      fvdl 		item = 10;
    869  1.34      fvdl 	else
    870  1.61       dsl 		item = mbs->mbrb_defkey - SCAN_F1;
    871  1.34      fvdl 
    872  1.60      fvdl 	if (item < 0 || item > 10 ||
    873  1.61       dsl 	    (item < NMBRPART && mbs->mbrb_nametab[item][0] == 0))
    874  1.34      fvdl 		item = 10;
    875  1.34      fvdl 
    876  1.34      fvdl 	do {
    877  1.34      fvdl 		decimal("Default boot option", &item);
    878  1.34      fvdl 	} while (item < 0 || item > 10 ||
    879  1.61       dsl 		    (item < NMBRPART && mbs->mbrb_nametab[item][0] == 0));
    880  1.34      fvdl 
    881  1.34      fvdl 	if (item == 10)
    882  1.61       dsl 		mbs->mbrb_defkey = SCAN_ENTER;
    883  1.34      fvdl 	else
    884  1.61       dsl 		mbs->mbrb_defkey = SCAN_F1 + item;
    885  1.34      fvdl 
    886  1.34      fvdl done:
    887  1.35      fvdl 	for (i = 0; i < NMBRPART; i++) {
    888  1.61       dsl 		if (mboot.mbr_parts[i].mbrp_typ != 0 &&
    889  1.61       dsl 		   mboot.mbr_parts[i].mbrp_start >=
    890  1.58      fvdl 		     (unsigned)(dos_cylinders * dos_heads * dos_sectors)) {
    891  1.61       dsl 			mbs->mbrb_flags |= BFL_EXTINT13;
    892  1.35      fvdl 			break;
    893  1.35      fvdl 		}
    894  1.35      fvdl 	}
    895  1.35      fvdl 
    896  1.37   thorpej 	if (bootsel_modified != 0 && !yesno("Update the bootselector?"))
    897  1.34      fvdl 		bootsel_modified = 0;
    898  1.34      fvdl }
    899  1.34      fvdl #endif
    900  1.34      fvdl 
    901  1.34      fvdl 
    902  1.11   ghudson /* Prerequisite: the disklabel parameters and master boot record must
    903  1.11   ghudson  *		 have been read (i.e. dos_* and mboot are meaningful).
    904  1.11   ghudson  * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and
    905  1.11   ghudson  *		  dos_cylindersectors to be consistent with what the
    906  1.11   ghudson  *		  partition table is using, if we can find a geometry
    907  1.11   ghudson  *		  which is consistent with all partition table entries.
    908  1.11   ghudson  *		  We may get the number of cylinders slightly wrong (in
    909  1.11   ghudson  *		  the conservative direction).  The idea is to be able
    910  1.11   ghudson  *		  to create a NetBSD partition on a disk we don't know
    911  1.11   ghudson  *		  the translated geometry of.
    912  1.11   ghudson  * This whole routine should be replaced with a kernel interface to get
    913  1.11   ghudson  * the BIOS geometry (which in turn requires modifications to the i386
    914  1.11   ghudson  * boot loader to pass in the BIOS geometry for each disk). */
    915  1.11   ghudson void
    916  1.43     lukem intuit_translated_geometry(void)
    917  1.11   ghudson {
    918  1.15      phil 
    919  1.44     lukem 	int xcylinders = -1, xheads = -1, xsectors = -1, i, j;
    920  1.11   ghudson 	int c1, h1, s1, c2, h2, s2;
    921  1.11   ghudson 	long a1, a2;
    922  1.11   ghudson 	quad_t num, denom;
    923  1.11   ghudson 
    924  1.11   ghudson 	/* Try to deduce the number of heads from two different mappings. */
    925  1.32   thorpej 	for (i = 0; i < NMBRPART * 2; i++) {
    926  1.11   ghudson 		if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
    927  1.11   ghudson 			continue;
    928  1.11   ghudson 		for (j = 0; j < 8; j++) {
    929  1.11   ghudson 			if (get_mapping(j, &c2, &h2, &s2, &a2) < 0)
    930  1.11   ghudson 				continue;
    931  1.22     enami 			num = (quad_t)h1*(a2-s2) - (quad_t)h2*(a1-s1);
    932  1.22     enami 			denom = (quad_t)c2*(a1-s1) - (quad_t)c1*(a2-s2);
    933  1.11   ghudson 			if (denom != 0 && num % denom == 0) {
    934  1.44     lukem 				xheads = num / denom;
    935  1.11   ghudson 				break;
    936  1.11   ghudson 			}
    937  1.11   ghudson 		}
    938  1.44     lukem 		if (xheads != -1)
    939  1.11   ghudson 			break;
    940  1.11   ghudson 	}
    941  1.11   ghudson 
    942  1.44     lukem 	if (xheads == -1)
    943  1.11   ghudson 		return;
    944  1.11   ghudson 
    945  1.11   ghudson 	/* Now figure out the number of sectors from a single mapping. */
    946  1.32   thorpej 	for (i = 0; i < NMBRPART * 2; i++) {
    947  1.11   ghudson 		if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
    948  1.11   ghudson 			continue;
    949  1.11   ghudson 		num = a1 - s1;
    950  1.44     lukem 		denom = c1 * xheads + h1;
    951  1.11   ghudson 		if (denom != 0 && num % denom == 0) {
    952  1.44     lukem 			xsectors = num / denom;
    953  1.11   ghudson 			break;
    954  1.11   ghudson 		}
    955  1.11   ghudson 	}
    956  1.11   ghudson 
    957  1.44     lukem 	if (xsectors == -1)
    958  1.11   ghudson 		return;
    959  1.11   ghudson 
    960  1.11   ghudson 	/* Estimate the number of cylinders. */
    961  1.44     lukem 	xcylinders = disklabel.d_secperunit / xheads / xsectors;
    962  1.11   ghudson 
    963  1.11   ghudson 	/* Now verify consistency with each of the partition table entries.
    964  1.11   ghudson 	 * Be willing to shove cylinders up a little bit to make things work,
    965  1.11   ghudson 	 * but translation mismatches are fatal. */
    966  1.32   thorpej 	for (i = 0; i < NMBRPART * 2; i++) {
    967  1.11   ghudson 		if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
    968  1.11   ghudson 			continue;
    969  1.44     lukem 		if (xsectors * (c1 * xheads + h1) + s1 != a1)
    970  1.11   ghudson 			return;
    971  1.44     lukem 		if (c1 >= xcylinders)
    972  1.44     lukem 			xcylinders = c1 + 1;
    973  1.11   ghudson 	}
    974  1.11   ghudson 
    975  1.11   ghudson 	/* Everything checks out.  Reset the geometry to use for further
    976  1.11   ghudson 	 * calculations. */
    977  1.44     lukem 	dos_cylinders = xcylinders;
    978  1.44     lukem 	dos_heads = xheads;
    979  1.44     lukem 	dos_sectors = xsectors;
    980  1.44     lukem 	dos_cylindersectors = xheads * xsectors;
    981  1.11   ghudson }
    982  1.11   ghudson 
    983  1.11   ghudson /* For the purposes of intuit_translated_geometry(), treat the partition
    984  1.11   ghudson  * table as a list of eight mapping between (cylinder, head, sector)
    985  1.11   ghudson  * triplets and absolute sectors.  Get the relevant geometry triplet and
    986  1.11   ghudson  * absolute sectors for a given entry, or return -1 if it isn't present.
    987  1.11   ghudson  * Note: for simplicity, the returned sector is 0-based. */
    988  1.11   ghudson int
    989  1.43     lukem get_mapping(int i, int *cylinder, int *head, int *sector,
    990  1.43     lukem     unsigned long *absolute)
    991  1.11   ghudson {
    992  1.61       dsl 	struct mbr_partition *part = &mboot.mbr_parts[i / 2];
    993  1.11   ghudson 
    994  1.32   thorpej 	if (part->mbrp_typ == 0)
    995  1.11   ghudson 		return -1;
    996  1.11   ghudson 	if (i % 2 == 0) {
    997  1.32   thorpej 		*cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect);
    998  1.32   thorpej 		*head = part->mbrp_shd;
    999  1.32   thorpej 		*sector = MBR_PSECT(part->mbrp_ssect) - 1;
   1000  1.32   thorpej 		*absolute = getlong(&part->mbrp_start);
   1001  1.11   ghudson 	} else {
   1002  1.32   thorpej 		*cylinder = MBR_PCYL(part->mbrp_ecyl, part->mbrp_esect);
   1003  1.32   thorpej 		*head = part->mbrp_ehd;
   1004  1.32   thorpej 		*sector = MBR_PSECT(part->mbrp_esect) - 1;
   1005  1.32   thorpej 		*absolute = getlong(&part->mbrp_start)
   1006  1.32   thorpej 		    + getlong(&part->mbrp_size) - 1;
   1007  1.11   ghudson 	}
   1008  1.11   ghudson 	return 0;
   1009   1.1       cgd }
   1010   1.1       cgd 
   1011   1.4   mycroft void
   1012  1.43     lukem change_part(int part, int csysid, int cstart, int csize)
   1013   1.1       cgd {
   1014  1.32   thorpej 	struct mbr_partition *partp;
   1015   1.1       cgd 
   1016  1.61       dsl 	partp = &mboot.mbr_parts[part];
   1017   1.1       cgd 
   1018  1.15      phil 	if (s_flag) {
   1019  1.29        ws 		if (csysid == 0 && cstart == 0 && csize == 0)
   1020  1.29        ws 			memset(partp, 0, sizeof *partp);
   1021  1.29        ws 		else {
   1022  1.32   thorpej 			partp->mbrp_typ = csysid;
   1023  1.28        ws #if 0
   1024  1.29        ws 			checkcyl(cstart / dos_cylindersectors);
   1025  1.28        ws #endif
   1026  1.32   thorpej 			putlong(&partp->mbrp_start, cstart);
   1027  1.32   thorpej 			putlong(&partp->mbrp_size, csize);
   1028  1.32   thorpej 			dos(getlong(&partp->mbrp_start),
   1029  1.32   thorpej 			    &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
   1030  1.32   thorpej 			dos(getlong(&partp->mbrp_start)
   1031  1.32   thorpej 			    + getlong(&partp->mbrp_size) - 1,
   1032  1.32   thorpej 			    &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
   1033  1.29        ws 		}
   1034  1.15      phil 		if (f_flag)
   1035  1.15      phil 			return;
   1036  1.15      phil 	}
   1037  1.15      phil 
   1038  1.15      phil 	printf("The data for partition %d is:\n", part);
   1039   1.4   mycroft 	print_part(part);
   1040   1.4   mycroft 	if (!u_flag || !yesno("Do you want to change it?"))
   1041   1.4   mycroft 		return;
   1042   1.1       cgd 
   1043   1.1       cgd 	do {
   1044   1.5   mycroft 		{
   1045   1.5   mycroft 			int sysid, start, size;
   1046   1.5   mycroft 
   1047  1.32   thorpej 			sysid = partp->mbrp_typ,
   1048  1.32   thorpej 			start = getlong(&partp->mbrp_start),
   1049  1.32   thorpej 			size = getlong(&partp->mbrp_size);
   1050   1.5   mycroft 			decimal("sysid", &sysid);
   1051   1.5   mycroft 			decimal("start", &start);
   1052   1.5   mycroft 			decimal("size", &size);
   1053  1.32   thorpej 			partp->mbrp_typ = sysid;
   1054  1.32   thorpej 			putlong(&partp->mbrp_start, start);
   1055  1.32   thorpej 			putlong(&partp->mbrp_size, size);
   1056   1.5   mycroft 		}
   1057   1.1       cgd 
   1058   1.7   mycroft 		if (yesno("Explicitly specify beg/end address?")) {
   1059   1.5   mycroft 			int tsector, tcylinder, thead;
   1060   1.4   mycroft 
   1061  1.50  christos 			tcylinder = MBR_PCYL(partp->mbrp_scyl,
   1062  1.50  christos 			    partp->mbrp_ssect);
   1063  1.32   thorpej 			thead = partp->mbrp_shd;
   1064  1.32   thorpej 			tsector = MBR_PSECT(partp->mbrp_ssect);
   1065   1.5   mycroft 			decimal("beginning cylinder", &tcylinder);
   1066  1.28        ws #if 0
   1067  1.28        ws 			checkcyl(tcylinder);
   1068  1.28        ws #endif
   1069   1.5   mycroft 			decimal("beginning head", &thead);
   1070   1.5   mycroft 			decimal("beginning sector", &tsector);
   1071  1.32   thorpej 			partp->mbrp_scyl = DOSCYL(tcylinder);
   1072  1.32   thorpej 			partp->mbrp_shd = thead;
   1073  1.32   thorpej 			partp->mbrp_ssect = DOSSECT(tsector, tcylinder);
   1074  1.32   thorpej 
   1075  1.50  christos 			tcylinder = MBR_PCYL(partp->mbrp_ecyl,
   1076  1.50  christos 			    partp->mbrp_esect);
   1077  1.32   thorpej 			thead = partp->mbrp_ehd;
   1078  1.32   thorpej 			tsector = MBR_PSECT(partp->mbrp_esect);
   1079   1.5   mycroft 			decimal("ending cylinder", &tcylinder);
   1080   1.5   mycroft 			decimal("ending head", &thead);
   1081   1.5   mycroft 			decimal("ending sector", &tsector);
   1082  1.32   thorpej 			partp->mbrp_ecyl = DOSCYL(tcylinder);
   1083  1.32   thorpej 			partp->mbrp_ehd = thead;
   1084  1.32   thorpej 			partp->mbrp_esect = DOSSECT(tsector, tcylinder);
   1085   1.1       cgd 		} else {
   1086  1.28        ws 
   1087  1.32   thorpej 			if (partp->mbrp_typ == 0
   1088  1.32   thorpej 			    && getlong(&partp->mbrp_start) == 0
   1089  1.32   thorpej 			    && getlong(&partp->mbrp_size) == 0)
   1090  1.29        ws 				memset(partp, 0, sizeof *partp);
   1091  1.29        ws 			else {
   1092  1.28        ws #if 0
   1093  1.32   thorpej 				checkcyl(getlong(&partp->mbrp_start)
   1094  1.29        ws 					 / dos_cylindersectors);
   1095  1.28        ws #endif
   1096  1.50  christos 				dos(getlong(&partp->mbrp_start),
   1097  1.50  christos 				    &partp->mbrp_scyl,
   1098  1.32   thorpej 				    &partp->mbrp_shd, &partp->mbrp_ssect);
   1099  1.32   thorpej 				dos(getlong(&partp->mbrp_start)
   1100  1.32   thorpej 				    + getlong(&partp->mbrp_size) - 1,
   1101  1.32   thorpej 				    &partp->mbrp_ecyl, &partp->mbrp_ehd,
   1102  1.32   thorpej 				    &partp->mbrp_esect);
   1103  1.29        ws 			}
   1104   1.1       cgd 		}
   1105   1.4   mycroft 
   1106   1.4   mycroft 		print_part(part);
   1107   1.7   mycroft 	} while (!yesno("Is this entry okay?"));
   1108   1.1       cgd }
   1109   1.1       cgd 
   1110   1.4   mycroft void
   1111  1.43     lukem print_params(void)
   1112   1.1       cgd {
   1113   1.4   mycroft 
   1114  1.15      phil 	if (sh_flag) {
   1115  1.31  jonathan 		printf ("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\nDLSIZE=%d\n",
   1116  1.31  jonathan 			cylinders, heads, sectors, disksectors);
   1117  1.14      phil 		printf ("BCYL=%d\nBHEAD=%d\nBSEC=%d\n",
   1118  1.14      phil 			dos_cylinders, dos_heads, dos_sectors);
   1119  1.14      phil 		return;
   1120  1.14      phil 	}
   1121  1.14      phil 
   1122  1.15      phil 	/* Not sh_flag */
   1123  1.48     lukem 	printf("Disk: %s\n", disk);
   1124  1.34      fvdl 	printf("NetBSD disklabel disk geometry:\n");
   1125  1.34      fvdl 	printf("cylinders: %d heads: %d sectors/track: %d (%d sectors/cylinder)\n\n",
   1126   1.5   mycroft 	    cylinders, heads, sectors, cylindersectors);
   1127  1.34      fvdl 	printf("BIOS disk geometry:\n");
   1128  1.34      fvdl 	printf("cylinders: %d heads: %d sectors/track: %d (%d sectors/cylinder)\n\n",
   1129   1.5   mycroft 	    dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors);
   1130   1.1       cgd }
   1131   1.1       cgd 
   1132   1.4   mycroft void
   1133  1.43     lukem change_active(int which)
   1134   1.1       cgd {
   1135  1.32   thorpej 	struct mbr_partition *partp;
   1136   1.4   mycroft 	int part;
   1137  1.15      phil 	int active = 4;
   1138   1.4   mycroft 
   1139  1.61       dsl 	partp = &mboot.mbr_parts[0];
   1140   1.1       cgd 
   1141   1.1       cgd 	if (a_flag && which != -1)
   1142   1.1       cgd 		active = which;
   1143   1.4   mycroft 	else {
   1144  1.32   thorpej 		for (part = 0; part < NMBRPART; part++)
   1145  1.32   thorpej 			if (partp[part].mbrp_flag & ACTIVE)
   1146   1.4   mycroft 				active = part;
   1147   1.4   mycroft 	}
   1148  1.15      phil 	if (!f_flag) {
   1149  1.15      phil 		if (yesno("Do you want to change the active partition?")) {
   1150  1.15      phil 			printf ("Choosing 4 will make no partition active.\n");
   1151  1.15      phil 			do {
   1152  1.15      phil 				decimal("active partition", &active);
   1153  1.15      phil 			} while (!yesno("Are you happy with this choice?"));
   1154  1.15      phil 		} else
   1155  1.15      phil 			return;
   1156  1.15      phil 	} else
   1157  1.15      phil 		if (active != 4)
   1158  1.15      phil 			printf ("Making partition %d active.\n", active);
   1159  1.15      phil 
   1160  1.32   thorpej 	for (part = 0; part < NMBRPART; part++)
   1161  1.32   thorpej 		partp[part].mbrp_flag &= ~ACTIVE;
   1162  1.15      phil 	if (active < 4)
   1163  1.32   thorpej 		partp[active].mbrp_flag |= ACTIVE;
   1164   1.1       cgd }
   1165   1.1       cgd 
   1166   1.4   mycroft void
   1167  1.43     lukem get_params_to_use(void)
   1168   1.1       cgd {
   1169  1.43     lukem 
   1170  1.16      phil 	if (b_flag) {
   1171  1.16      phil 		dos_cylinders = b_cyl;
   1172  1.16      phil 		dos_heads = b_head;
   1173  1.16      phil 		dos_sectors = b_sec;
   1174  1.16      phil 		dos_cylindersectors = dos_heads * dos_sectors;
   1175  1.16      phil 		return;
   1176  1.16      phil 	}
   1177   1.4   mycroft 
   1178   1.1       cgd 	print_params();
   1179   1.4   mycroft 	if (yesno("Do you want to change our idea of what BIOS thinks?")) {
   1180   1.4   mycroft 		do {
   1181   1.5   mycroft 			decimal("BIOS's idea of #cylinders", &dos_cylinders);
   1182   1.5   mycroft 			decimal("BIOS's idea of #heads", &dos_heads);
   1183   1.5   mycroft 			decimal("BIOS's idea of #sectors", &dos_sectors);
   1184   1.5   mycroft 			dos_cylindersectors = dos_heads * dos_sectors;
   1185   1.1       cgd 			print_params();
   1186   1.4   mycroft 		} while (!yesno("Are you happy with this choice?"));
   1187   1.1       cgd 	}
   1188   1.1       cgd }
   1189   1.1       cgd 
   1190   1.1       cgd /***********************************************\
   1191   1.1       cgd * Change real numbers into strange dos numbers	*
   1192   1.1       cgd \***********************************************/
   1193   1.4   mycroft void
   1194  1.43     lukem dos(int sector, unsigned char *cylinderp, unsigned char *headp,
   1195  1.43     lukem     unsigned char *sectorp)
   1196   1.4   mycroft {
   1197   1.5   mycroft 	int cylinder, head;
   1198  1.40      fvdl 	int biosmaxsec;
   1199  1.40      fvdl 
   1200  1.40      fvdl 	biosmaxsec = dos_cylinders * dos_heads * dos_sectors - 1;
   1201  1.40      fvdl 	if (sector > biosmaxsec)
   1202  1.40      fvdl 		sector = biosmaxsec;
   1203   1.4   mycroft 
   1204   1.5   mycroft 	cylinder = sector / dos_cylindersectors;
   1205  1.28        ws 
   1206   1.5   mycroft 	sector -= cylinder * dos_cylindersectors;
   1207   1.4   mycroft 
   1208   1.5   mycroft 	head = sector / dos_sectors;
   1209   1.5   mycroft 	sector -= head * dos_sectors;
   1210   1.4   mycroft 
   1211   1.5   mycroft 	*cylinderp = DOSCYL(cylinder);
   1212   1.5   mycroft 	*headp = head;
   1213   1.5   mycroft 	*sectorp = DOSSECT(sector + 1, cylinder);
   1214   1.1       cgd }
   1215   1.1       cgd 
   1216  1.28        ws #if 0
   1217  1.28        ws void
   1218  1.43     lukem checkcyl(int cyl)
   1219  1.28        ws {
   1220  1.43     lukem 
   1221  1.28        ws 	if (cyl >= MAXCYL)
   1222  1.28        ws 		warnx("partition start beyond BIOS limit");
   1223  1.28        ws }
   1224  1.28        ws #endif
   1225  1.28        ws 
   1226  1.44     lukem int fd = -1;
   1227   1.1       cgd 
   1228   1.4   mycroft int
   1229  1.44     lukem open_disk(int update)
   1230   1.1       cgd {
   1231  1.18     lukem 	static char namebuf[MAXPATHLEN + 1];
   1232   1.1       cgd 
   1233  1.44     lukem 	fd = opendisk(disk, update ? O_RDWR : O_RDONLY, namebuf,
   1234  1.19     lukem 	    sizeof(namebuf), 0);
   1235  1.18     lukem 	if (fd < 0) {
   1236  1.47     lukem 		if (errno == ENODEV)
   1237  1.47     lukem 			warnx("%s is not a character device", namebuf);
   1238  1.47     lukem 		else
   1239  1.47     lukem 			warn("%s", namebuf);
   1240   1.4   mycroft 		return (-1);
   1241   1.4   mycroft 	}
   1242  1.18     lukem 	disk = namebuf;
   1243   1.4   mycroft 	if (get_params() == -1) {
   1244   1.4   mycroft 		close(fd);
   1245   1.4   mycroft 		return (-1);
   1246   1.1       cgd 	}
   1247   1.4   mycroft 	return (0);
   1248   1.1       cgd }
   1249   1.1       cgd 
   1250   1.4   mycroft int
   1251  1.44     lukem read_disk(off_t sector, void *buf)
   1252   1.1       cgd {
   1253   1.4   mycroft 
   1254  1.44     lukem 	if (fd == -1)
   1255  1.44     lukem 		errx(1, "read_disk(); fd == -1");
   1256  1.44     lukem 	if (lseek(fd, sector * 512, 0) == -1)
   1257   1.4   mycroft 		return (-1);
   1258   1.4   mycroft 	return (read(fd, buf, 512));
   1259   1.1       cgd }
   1260   1.1       cgd 
   1261   1.4   mycroft int
   1262  1.44     lukem write_disk(off_t sector, void *buf)
   1263   1.1       cgd {
   1264   1.4   mycroft 
   1265  1.44     lukem 	if (fd == -1)
   1266  1.44     lukem 		errx(1, "write_disk(); fd == -1");
   1267  1.44     lukem 	if (lseek(fd, sector * 512, 0) == -1)
   1268   1.4   mycroft 		return (-1);
   1269   1.4   mycroft 	return (write(fd, buf, 512));
   1270   1.1       cgd }
   1271   1.1       cgd 
   1272   1.4   mycroft int
   1273  1.43     lukem get_params(void)
   1274   1.1       cgd {
   1275   1.1       cgd 
   1276  1.42   thorpej 	if (ioctl(fd, DIOCGDEFLABEL, &disklabel) == -1) {
   1277  1.42   thorpej 		warn("DIOCGDEFLABEL");
   1278  1.42   thorpej 		if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
   1279  1.42   thorpej 			warn("DIOCGDINFO");
   1280  1.42   thorpej 			return (-1);
   1281  1.42   thorpej 		}
   1282   1.4   mycroft 	}
   1283   1.1       cgd 
   1284   1.5   mycroft 	dos_cylinders = cylinders = disklabel.d_ncylinders;
   1285   1.4   mycroft 	dos_heads = heads = disklabel.d_ntracks;
   1286   1.4   mycroft 	dos_sectors = sectors = disklabel.d_nsectors;
   1287   1.5   mycroft 	dos_cylindersectors = cylindersectors = heads * sectors;
   1288  1.31  jonathan 	disksectors = disklabel.d_secperunit;
   1289   1.1       cgd 
   1290   1.4   mycroft 	return (0);
   1291   1.1       cgd }
   1292   1.1       cgd 
   1293   1.4   mycroft int
   1294  1.61       dsl read_s0(off_t offset, struct mbr_sector *boot)
   1295   1.1       cgd {
   1296   1.4   mycroft 
   1297  1.61       dsl 	if (read_disk(offset, boot->mbr_bootinst) == -1) {
   1298  1.44     lukem 		warn("can't read %s partition table",
   1299  1.44     lukem 		    offset ? "extended" : "fdisk");
   1300   1.4   mycroft 		return (-1);
   1301   1.1       cgd 	}
   1302  1.61       dsl 	if (getshort(&boot->mbr_signature) != MBR_MAGIC) {
   1303  1.44     lukem 		warnx("invalid %s partition table found",
   1304  1.44     lukem 		    offset ? "extended" : "fdisk");
   1305   1.4   mycroft 		return (-1);
   1306   1.1       cgd 	}
   1307   1.4   mycroft 	return (0);
   1308   1.1       cgd }
   1309   1.1       cgd 
   1310   1.4   mycroft int
   1311  1.43     lukem write_s0(void)
   1312   1.1       cgd {
   1313  1.30        ws 	int flag, i;
   1314   1.4   mycroft 
   1315   1.1       cgd 	/*
   1316   1.1       cgd 	 * write enable label sector before write (if necessary),
   1317   1.1       cgd 	 * disable after writing.
   1318   1.1       cgd 	 * needed if the disklabel protected area also protects
   1319   1.1       cgd 	 * sector 0. (e.g. empty disk)
   1320   1.1       cgd 	 */
   1321   1.1       cgd 	flag = 1;
   1322   1.1       cgd 	if (ioctl(fd, DIOCWLABEL, &flag) < 0)
   1323   1.4   mycroft 		warn("DIOCWLABEL");
   1324  1.61       dsl 	if (write_disk(0, mboot.mbr_bootinst) == -1) {
   1325   1.4   mycroft 		warn("can't write fdisk partition table");
   1326   1.1       cgd 		return -1;
   1327   1.4   mycroft 	}
   1328  1.30        ws 	for (i = bootsize; (i -= 0x200) > 0;)
   1329  1.30        ws 		if (write_disk(i / 0x200, bootcode + i) == -1) {
   1330  1.30        ws 			warn("can't write bootcode");
   1331  1.30        ws 			return -1;
   1332  1.30        ws 		}
   1333   1.1       cgd 	flag = 0;
   1334   1.4   mycroft 	if (ioctl(fd, DIOCWLABEL, &flag) < 0)
   1335   1.4   mycroft 		warn("DIOCWLABEL");
   1336  1.16      phil 	return 0;
   1337   1.1       cgd }
   1338   1.1       cgd 
   1339   1.4   mycroft int
   1340  1.43     lukem yesno(const char *str)
   1341   1.4   mycroft {
   1342   1.4   mycroft 	int ch, first;
   1343   1.1       cgd 
   1344   1.1       cgd 	printf("%s [n] ", str);
   1345   1.1       cgd 
   1346   1.4   mycroft 	first = ch = getchar();
   1347   1.4   mycroft 	while (ch != '\n' && ch != EOF)
   1348   1.4   mycroft 		ch = getchar();
   1349  1.53      yamt 	if (ch == EOF)
   1350  1.53      yamt 		errx(1, "EOF");
   1351   1.4   mycroft 	return (first == 'y' || first == 'Y');
   1352   1.1       cgd }
   1353   1.1       cgd 
   1354   1.5   mycroft void
   1355  1.43     lukem decimal(const char *str, int *num)
   1356   1.1       cgd {
   1357   1.8       cgd 	int acc = 0;
   1358   1.4   mycroft 	char *cp;
   1359   1.1       cgd 
   1360   1.5   mycroft 	for (;; printf("%s is not a valid decimal number.\n", lbuf)) {
   1361  1.34      fvdl 		printf("%s: [%d] ", str, *num);
   1362   1.1       cgd 
   1363  1.53      yamt 		if (!fgets(lbuf, LBUF, stdin))
   1364  1.53      yamt 			errx(1, "EOF");
   1365   1.7   mycroft 		lbuf[strlen(lbuf)-1] = '\0';
   1366   1.7   mycroft 		cp = lbuf;
   1367   1.7   mycroft 
   1368   1.7   mycroft 		cp += strspn(cp, " \t");
   1369   1.7   mycroft 		if (*cp == '\0')
   1370   1.5   mycroft 			return;
   1371   1.1       cgd 
   1372  1.34      fvdl 		if (!isdigit(*cp) && *cp != '-')
   1373   1.7   mycroft 			continue;
   1374   1.5   mycroft 		acc = strtol(lbuf, &cp, 10);
   1375   1.7   mycroft 
   1376   1.5   mycroft 		cp += strspn(cp, " \t");
   1377   1.5   mycroft 		if (*cp != '\0')
   1378   1.5   mycroft 			continue;
   1379   1.1       cgd 
   1380   1.5   mycroft 		*num = acc;
   1381   1.5   mycroft 		return;
   1382   1.1       cgd 	}
   1383   1.1       cgd 
   1384   1.1       cgd }
   1385   1.1       cgd 
   1386   1.4   mycroft int
   1387  1.43     lukem type_match(const void *key, const void *item)
   1388   1.4   mycroft {
   1389   1.4   mycroft 	const int *typep = key;
   1390   1.4   mycroft 	const struct part_type *ptr = item;
   1391   1.4   mycroft 
   1392   1.4   mycroft 	if (*typep < ptr->type)
   1393   1.4   mycroft 		return (-1);
   1394   1.4   mycroft 	if (*typep > ptr->type)
   1395   1.4   mycroft 		return (1);
   1396   1.4   mycroft 	return (0);
   1397   1.4   mycroft }
   1398   1.4   mycroft 
   1399  1.43     lukem const char *
   1400  1.43     lukem get_type(int type)
   1401   1.4   mycroft {
   1402   1.4   mycroft 	struct part_type *ptr;
   1403   1.4   mycroft 
   1404   1.4   mycroft 	ptr = bsearch(&type, part_types,
   1405   1.4   mycroft 	    sizeof(part_types) / sizeof(struct part_type),
   1406   1.4   mycroft 	    sizeof(struct part_type), type_match);
   1407   1.4   mycroft 	if (ptr == 0)
   1408   1.4   mycroft 		return ("unknown");
   1409  1.16      phil 	return (ptr->name);
   1410   1.1       cgd }
   1411