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