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