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