Home | History | Annotate | Line # | Download | only in fdisk
fdisk.c revision 1.110.2.3
      1  1.110.2.3  mjf /*	$NetBSD: fdisk.c,v 1.110.2.3 2008/02/18 21:04:17 mjf Exp $ */
      2  1.110.2.2  mjf 
      3  1.110.2.2  mjf /*
      4  1.110.2.2  mjf  * Mach Operating System
      5  1.110.2.2  mjf  * Copyright (c) 1992 Carnegie Mellon University
      6  1.110.2.2  mjf  * All Rights Reserved.
      7  1.110.2.2  mjf  *
      8  1.110.2.2  mjf  * Permission to use, copy, modify and distribute this software and its
      9  1.110.2.2  mjf  * documentation is hereby granted, provided that both the copyright
     10  1.110.2.2  mjf  * notice and this permission notice appear in all copies of the
     11  1.110.2.2  mjf  * software, derivative works or modified versions, and any portions
     12  1.110.2.2  mjf  * thereof, and that both notices appear in supporting documentation.
     13  1.110.2.2  mjf  *
     14  1.110.2.2  mjf  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  1.110.2.2  mjf  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16  1.110.2.2  mjf  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  1.110.2.2  mjf  *
     18  1.110.2.2  mjf  * Carnegie Mellon requests users of this software to return to
     19  1.110.2.2  mjf  *
     20  1.110.2.2  mjf  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  1.110.2.2  mjf  *  School of Computer Science
     22  1.110.2.2  mjf  *  Carnegie Mellon University
     23  1.110.2.2  mjf  *  Pittsburgh PA 15213-3890
     24  1.110.2.2  mjf  *
     25  1.110.2.2  mjf  * any improvements or extensions that they make and grant Carnegie Mellon
     26  1.110.2.2  mjf  * the rights to redistribute these changes.
     27  1.110.2.2  mjf  */
     28  1.110.2.2  mjf 
     29  1.110.2.2  mjf /*
     30  1.110.2.2  mjf  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
     31  1.110.2.2  mjf  *	Copyright (c) 1989	Robert. V. Baron
     32  1.110.2.2  mjf  *	Created.
     33  1.110.2.2  mjf  */
     34  1.110.2.2  mjf 
     35  1.110.2.2  mjf #if HAVE_NBTOOL_CONFIG_H
     36  1.110.2.2  mjf #include "nbtool_config.h"
     37  1.110.2.2  mjf #endif
     38  1.110.2.2  mjf 
     39  1.110.2.2  mjf #include <sys/cdefs.h>
     40  1.110.2.2  mjf 
     41  1.110.2.2  mjf #ifndef lint
     42  1.110.2.3  mjf __RCSID("$NetBSD: fdisk.c,v 1.110.2.3 2008/02/18 21:04:17 mjf Exp $");
     43  1.110.2.2  mjf #endif /* not lint */
     44  1.110.2.2  mjf 
     45  1.110.2.2  mjf #define MBRPTYPENAMES
     46  1.110.2.2  mjf #include <sys/types.h>
     47  1.110.2.2  mjf #include <sys/param.h>
     48  1.110.2.2  mjf #include <sys/stat.h>
     49  1.110.2.2  mjf #include <ctype.h>
     50  1.110.2.2  mjf #include <err.h>
     51  1.110.2.2  mjf #include <errno.h>
     52  1.110.2.2  mjf #include <fcntl.h>
     53  1.110.2.2  mjf #include <paths.h>
     54  1.110.2.2  mjf #include <stdarg.h>
     55  1.110.2.2  mjf #include <stddef.h>
     56  1.110.2.2  mjf #include <stdio.h>
     57  1.110.2.2  mjf #include <stdlib.h>
     58  1.110.2.2  mjf #include <string.h>
     59  1.110.2.2  mjf #include <unistd.h>
     60  1.110.2.2  mjf #include <vis.h>
     61  1.110.2.2  mjf 
     62  1.110.2.2  mjf #if !HAVE_NBTOOL_CONFIG_H
     63  1.110.2.2  mjf #include <sys/disklabel.h>
     64  1.110.2.2  mjf #include <sys/bootblock.h>
     65  1.110.2.2  mjf #include <sys/ioctl.h>
     66  1.110.2.2  mjf #include <sys/sysctl.h>
     67  1.110.2.2  mjf #include <disktab.h>
     68  1.110.2.2  mjf #include <util.h>
     69  1.110.2.2  mjf #else
     70  1.110.2.2  mjf #include <nbinclude/sys/disklabel.h>
     71  1.110.2.2  mjf #include <nbinclude/sys/bootblock.h>
     72  1.110.2.2  mjf #include "../../include/disktab.h"
     73  1.110.2.2  mjf /* We enforce -F, so none of these possibly undefined items can be needed */
     74  1.110.2.2  mjf #define opendisk(path, fl, buf, buflen, cooked) (-1)
     75  1.110.2.2  mjf #ifndef DIOCGDEFLABEL
     76  1.110.2.2  mjf #define DIOCGDEFLABEL 0
     77  1.110.2.2  mjf #endif
     78  1.110.2.2  mjf #ifndef DIOCGDINFO
     79  1.110.2.2  mjf #define DIOCGDINFO 0
     80  1.110.2.2  mjf #endif
     81  1.110.2.2  mjf #ifndef DIOCWLABEL
     82  1.110.2.2  mjf #define DIOCWLABEL 0
     83  1.110.2.2  mjf #endif
     84  1.110.2.2  mjf #endif /* HAVE_NBTOOL_CONFIG_H */
     85  1.110.2.2  mjf 
     86  1.110.2.2  mjf #define	DEFAULT_BOOTDIR		"/usr/mdec"
     87  1.110.2.2  mjf 
     88  1.110.2.2  mjf #define	LE_MBR_MAGIC		htole16(MBR_MAGIC)
     89  1.110.2.2  mjf #define	LE_MBR_BS_MAGIC		htole16(MBR_BS_MAGIC)
     90  1.110.2.2  mjf 
     91  1.110.2.2  mjf #if defined(__i386__) || defined(__x86_64__)
     92  1.110.2.2  mjf #if !HAVE_NBTOOL_CONFIG_H
     93  1.110.2.2  mjf #include <machine/cpu.h>
     94  1.110.2.2  mjf #endif /* !HAVE_NBTOOL_CONFIG_H */
     95  1.110.2.2  mjf #define BOOTSEL
     96  1.110.2.2  mjf #endif
     97  1.110.2.2  mjf 
     98  1.110.2.2  mjf #ifdef BOOTSEL
     99  1.110.2.2  mjf 
    100  1.110.2.2  mjf #define	DEFAULT_BOOTCODE	"mbr"
    101  1.110.2.2  mjf #define	DEFAULT_BOOTSELCODE	"mbr_bootsel"
    102  1.110.2.2  mjf #define	DEFAULT_BOOTEXTCODE	"mbr_ext"
    103  1.110.2.2  mjf 
    104  1.110.2.2  mjf /* Scan values for the various keys we use, as returned by the BIOS */
    105  1.110.2.2  mjf #define	SCAN_ENTER	0x1c
    106  1.110.2.2  mjf #define	SCAN_F1		0x3b
    107  1.110.2.2  mjf #define	SCAN_1		0x2
    108  1.110.2.2  mjf 
    109  1.110.2.3  mjf #define	MAX_BIOS_DISKS	16	/* Going beyond F12 is hard though! */
    110  1.110.2.3  mjf 
    111  1.110.2.3  mjf /* We same the dflt 'boot partition' as a disk block, with some magic values. */
    112  1.110.2.3  mjf #define DEFAULT_ACTIVE	(~(daddr_t)0)
    113  1.110.2.3  mjf #define	DEFAULT_DISK(n)	(DEFAULT_ACTIVE - MAX_BIOS_DISKS + (n))
    114  1.110.2.3  mjf 
    115  1.110.2.2  mjf #endif
    116  1.110.2.2  mjf 
    117  1.110.2.2  mjf #define LBUF 100
    118  1.110.2.2  mjf static char lbuf[LBUF];
    119  1.110.2.2  mjf 
    120  1.110.2.2  mjf #ifndef PRIdaddr
    121  1.110.2.2  mjf #define PRIdaddr PRId64
    122  1.110.2.2  mjf #endif
    123  1.110.2.2  mjf 
    124  1.110.2.2  mjf #ifndef PRId64
    125  1.110.2.2  mjf #if HAVE_LONG_LONG
    126  1.110.2.2  mjf #define PRId64 "lld"
    127  1.110.2.2  mjf #endif
    128  1.110.2.2  mjf #endif
    129  1.110.2.2  mjf 
    130  1.110.2.2  mjf #ifndef PRId32
    131  1.110.2.2  mjf #define PRId32 "ld"
    132  1.110.2.2  mjf #endif
    133  1.110.2.2  mjf 
    134  1.110.2.2  mjf #ifndef _PATH_DEFDISK
    135  1.110.2.2  mjf #define _PATH_DEFDISK	"/dev/rwd0d"
    136  1.110.2.2  mjf #endif
    137  1.110.2.2  mjf 
    138  1.110.2.2  mjf const char *disk = _PATH_DEFDISK;
    139  1.110.2.2  mjf 
    140  1.110.2.2  mjf struct disklabel disklabel;		/* disk parameters */
    141  1.110.2.2  mjf 
    142  1.110.2.2  mjf unsigned int cylinders, sectors, heads;
    143  1.110.2.2  mjf daddr_t disksectors;
    144  1.110.2.2  mjf #define cylindersectors (heads * sectors)
    145  1.110.2.2  mjf 
    146  1.110.2.2  mjf struct mbr_sector mboot;
    147  1.110.2.2  mjf 
    148  1.110.2.2  mjf 
    149  1.110.2.2  mjf struct {
    150  1.110.2.2  mjf 	struct mbr_sector *ptn;		/* array of pbrs */
    151  1.110.2.2  mjf 	daddr_t		base;		/* first sector of ext. ptn */
    152  1.110.2.2  mjf 	daddr_t		limit;		/* last sector of ext. ptn */
    153  1.110.2.2  mjf 	int		num_ptn;	/* number of contained partitions */
    154  1.110.2.2  mjf 	int		ptn_id;		/* entry in mbr */
    155  1.110.2.2  mjf 	int		is_corrupt;	/* 1 if extended chain illegal */
    156  1.110.2.2  mjf } ext;
    157  1.110.2.2  mjf 
    158  1.110.2.2  mjf const char *boot_dir = DEFAULT_BOOTDIR;
    159  1.110.2.2  mjf char *boot_path = 0;			/* name of file we actually opened */
    160  1.110.2.2  mjf 
    161  1.110.2.2  mjf #ifdef BOOTSEL
    162  1.110.2.2  mjf 
    163  1.110.2.2  mjf #define OPTIONS			"0123BFSafiluvs:b:c:E:r:w:t:T:"
    164  1.110.2.2  mjf #else
    165  1.110.2.2  mjf #define change_part(e, p, id, st, sz, bm) change__part(e, p, id, st, sz)
    166  1.110.2.2  mjf #define OPTIONS			"0123FSafiluvs:b:c:E:r:w:"
    167  1.110.2.2  mjf #endif
    168  1.110.2.2  mjf 
    169  1.110.2.2  mjf unsigned int dos_cylinders;
    170  1.110.2.2  mjf unsigned int dos_heads;
    171  1.110.2.2  mjf unsigned int dos_sectors;
    172  1.110.2.2  mjf daddr_t dos_disksectors;
    173  1.110.2.2  mjf #define dos_cylindersectors (dos_heads * dos_sectors)
    174  1.110.2.2  mjf #define dos_totalsectors (dos_heads * dos_sectors * dos_cylinders)
    175  1.110.2.2  mjf 
    176  1.110.2.2  mjf #define DOSSECT(s,c)	(((s) & 0x3f) | (((c) >> 2) & 0xc0))
    177  1.110.2.2  mjf #define DOSCYL(c)	((c) & 0xff)
    178  1.110.2.2  mjf #define SEC_IN_1M (1024 * 1024 / 512)
    179  1.110.2.2  mjf #define SEC_TO_MB(sec) ((unsigned int)(((sec) + SEC_IN_1M / 2) / SEC_IN_1M))
    180  1.110.2.2  mjf #define SEC_TO_CYL(sec) (((sec) + dos_cylindersectors/2) / dos_cylindersectors)
    181  1.110.2.2  mjf 
    182  1.110.2.2  mjf #define MAXCYL		1024	/* Usual limit is 1023 */
    183  1.110.2.2  mjf #define	MAXHEAD		256	/* Usual limit is 255 */
    184  1.110.2.2  mjf #define	MAXSECTOR	63
    185  1.110.2.2  mjf int partition = -1;
    186  1.110.2.2  mjf 
    187  1.110.2.2  mjf int fd = -1, wfd = -1, *rfd = &fd;
    188  1.110.2.2  mjf char *disk_file = NULL;
    189  1.110.2.2  mjf char *disk_type = NULL;
    190  1.110.2.2  mjf 
    191  1.110.2.2  mjf int a_flag;		/* set active partition */
    192  1.110.2.2  mjf int i_flag;		/* init bootcode */
    193  1.110.2.2  mjf int u_flag;		/* update partition data */
    194  1.110.2.2  mjf int v_flag;		/* more verbose */
    195  1.110.2.2  mjf int sh_flag;		/* Output data as shell defines */
    196  1.110.2.2  mjf int f_flag;		/* force --not interactive */
    197  1.110.2.2  mjf int s_flag;		/* set id,offset,size */
    198  1.110.2.2  mjf int b_flag;		/* Set cyl, heads, secs (as c/h/s) */
    199  1.110.2.2  mjf int B_flag;		/* Edit/install bootselect code */
    200  1.110.2.2  mjf int E_flag;		/* extended partition number */
    201  1.110.2.2  mjf int b_cyl, b_head, b_sec;  /* b_flag values. */
    202  1.110.2.2  mjf 
    203  1.110.2.2  mjf #if !HAVE_NBTOOL_CONFIG_H
    204  1.110.2.2  mjf int F_flag = 0;
    205  1.110.2.2  mjf #else
    206  1.110.2.2  mjf /* Tool - force 'file' mode to avoid unsupported functions and ioctls */
    207  1.110.2.2  mjf int F_flag = 1;
    208  1.110.2.2  mjf #endif
    209  1.110.2.2  mjf 
    210  1.110.2.2  mjf struct mbr_sector bootcode[8192 / sizeof (struct mbr_sector)];
    211  1.110.2.2  mjf int bootsize;		/* actual size of bootcode */
    212  1.110.2.2  mjf int boot_installed;	/* 1 if we've copied code into the mbr */
    213  1.110.2.2  mjf 
    214  1.110.2.2  mjf #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
    215  1.110.2.2  mjf struct disklist *dl;
    216  1.110.2.2  mjf #endif
    217  1.110.2.2  mjf 
    218  1.110.2.2  mjf 
    219  1.110.2.2  mjf #define KNOWN_SYSIDS	(sizeof(mbr_ptypes)/sizeof(mbr_ptypes[0]))
    220  1.110.2.2  mjf 
    221  1.110.2.2  mjf void	usage(void);
    222  1.110.2.2  mjf void	print_s0(int);
    223  1.110.2.2  mjf void	print_part(struct mbr_sector *, int, daddr_t);
    224  1.110.2.2  mjf void	print_mbr_partition(struct mbr_sector *, int, daddr_t, daddr_t, int);
    225  1.110.2.2  mjf void	print_pbr(daddr_t, int, uint8_t);
    226  1.110.2.2  mjf int	is_all_zero(const unsigned char *, size_t);
    227  1.110.2.2  mjf void	printvis(int, const char *, const char *, size_t);
    228  1.110.2.2  mjf int	read_boot(const char *, void *, size_t, int);
    229  1.110.2.2  mjf void	init_sector0(int);
    230  1.110.2.2  mjf void	intuit_translated_geometry(void);
    231  1.110.2.2  mjf void	get_geometry(void);
    232  1.110.2.2  mjf void	get_extended_ptn(void);
    233  1.110.2.2  mjf #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
    234  1.110.2.2  mjf void	get_diskname(const char *, char *, size_t);
    235  1.110.2.2  mjf #endif /* (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H */
    236  1.110.2.2  mjf int	change_part(int, int, int, daddr_t, daddr_t, char *);
    237  1.110.2.2  mjf void	print_params(void);
    238  1.110.2.2  mjf int	first_active(void);
    239  1.110.2.2  mjf void	change_active(int);
    240  1.110.2.2  mjf void	get_params_to_use(void);
    241  1.110.2.2  mjf void	dos(int, unsigned char *, unsigned char *, unsigned char *);
    242  1.110.2.2  mjf int	open_disk(int);
    243  1.110.2.2  mjf int	read_disk(daddr_t, void *);
    244  1.110.2.2  mjf int	write_disk(daddr_t, void *);
    245  1.110.2.2  mjf int	get_params(void);
    246  1.110.2.2  mjf int	read_s0(daddr_t, struct mbr_sector *);
    247  1.110.2.2  mjf int	write_mbr(void);
    248  1.110.2.2  mjf int	yesno(const char *, ...);
    249  1.110.2.2  mjf int	decimal(const char *, int, int, int, int);
    250  1.110.2.2  mjf #define DEC_SEC		1		/* asking for a sector number */
    251  1.110.2.2  mjf #define	DEC_RND		2		/* round to end of first track */
    252  1.110.2.2  mjf #define	DEC_RND_0	4		/* round 0 to size of a track */
    253  1.110.2.2  mjf #define DEC_RND_DOWN	8		/* subtract 1 track */
    254  1.110.2.2  mjf #define DEC_RND_DOWN_2	16		/* subtract 2 tracks */
    255  1.110.2.2  mjf void	string(const char *, int, char *);
    256  1.110.2.2  mjf int	ptn_id(const char *, int *);
    257  1.110.2.2  mjf int	type_match(const void *, const void *);
    258  1.110.2.2  mjf const char *get_type(int);
    259  1.110.2.2  mjf int	get_mapping(int, unsigned int *, unsigned int *, unsigned int *, unsigned long *);
    260  1.110.2.2  mjf #ifdef BOOTSEL
    261  1.110.2.2  mjf daddr_t	configure_bootsel(daddr_t);
    262  1.110.2.2  mjf void	install_bootsel(int);
    263  1.110.2.2  mjf daddr_t	get_default_boot(void);
    264  1.110.2.2  mjf void	set_default_boot(daddr_t);
    265  1.110.2.2  mjf #endif
    266  1.110.2.2  mjf 
    267  1.110.2.2  mjf static void
    268  1.110.2.2  mjf initvar_disk(const char **diskp)
    269  1.110.2.2  mjf {
    270  1.110.2.2  mjf #if !HAVE_NBTOOL_CONFIG_H
    271  1.110.2.2  mjf 	int mib[2];
    272  1.110.2.2  mjf 	size_t len;
    273  1.110.2.2  mjf 	char *root_device;
    274  1.110.2.2  mjf 
    275  1.110.2.2  mjf 	mib[0] = CTL_KERN;
    276  1.110.2.2  mjf 	mib[1] = KERN_ROOT_DEVICE;
    277  1.110.2.2  mjf 	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1 ||
    278  1.110.2.2  mjf 	    (root_device = malloc(len)) == NULL ||
    279  1.110.2.2  mjf 	    sysctl(mib, 2, root_device, &len, NULL, 0) == -1)
    280  1.110.2.2  mjf 		return;
    281  1.110.2.2  mjf 
    282  1.110.2.2  mjf 	*diskp = root_device;
    283  1.110.2.2  mjf #endif /* HAVE_NBTOOL_CONFIG_H */
    284  1.110.2.2  mjf }
    285  1.110.2.2  mjf 
    286  1.110.2.2  mjf int
    287  1.110.2.2  mjf main(int argc, char *argv[])
    288  1.110.2.2  mjf {
    289  1.110.2.2  mjf 	struct stat sb;
    290  1.110.2.2  mjf 	int ch;
    291  1.110.2.2  mjf 	size_t len;
    292  1.110.2.2  mjf 	char *cp;
    293  1.110.2.2  mjf 	int n;
    294  1.110.2.2  mjf #ifdef BOOTSEL
    295  1.110.2.2  mjf 	daddr_t default_ptn;		/* start sector of default ptn */
    296  1.110.2.2  mjf 	char *cbootmenu = 0;
    297  1.110.2.2  mjf #endif
    298  1.110.2.2  mjf 
    299  1.110.2.2  mjf 	int csysid, cstart, csize;	/* For the b_flag. */
    300  1.110.2.2  mjf 
    301  1.110.2.2  mjf 	a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
    302  1.110.2.2  mjf 	v_flag = 0;
    303  1.110.2.2  mjf 	E_flag = 0;
    304  1.110.2.2  mjf 	csysid = cstart = csize = 0;
    305  1.110.2.2  mjf 	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
    306  1.110.2.2  mjf 		switch (ch) {
    307  1.110.2.2  mjf 		case '0':
    308  1.110.2.2  mjf 			partition = 0;
    309  1.110.2.2  mjf 			break;
    310  1.110.2.2  mjf 		case '1':
    311  1.110.2.2  mjf 			partition = 1;
    312  1.110.2.2  mjf 			break;
    313  1.110.2.2  mjf 		case '2':
    314  1.110.2.2  mjf 			partition = 2;
    315  1.110.2.2  mjf 			break;
    316  1.110.2.2  mjf 		case '3':
    317  1.110.2.2  mjf 			partition = 3;
    318  1.110.2.2  mjf 			break;
    319  1.110.2.2  mjf 		case 'E':	/* Extended partition number */
    320  1.110.2.2  mjf 			E_flag = 1;
    321  1.110.2.2  mjf 			partition = strtoul(optarg, &cp, 0);
    322  1.110.2.2  mjf 			if (*cp || partition < 0)
    323  1.110.2.2  mjf 				errx(1, "Bad partition number -E %s.", optarg);
    324  1.110.2.2  mjf 			break;
    325  1.110.2.2  mjf #ifdef BOOTSEL
    326  1.110.2.2  mjf 		case 'B':	/* Bootselect parameters */
    327  1.110.2.2  mjf 			B_flag = 1;
    328  1.110.2.2  mjf 			break;
    329  1.110.2.2  mjf #endif
    330  1.110.2.2  mjf 		case 'F':	/* device argument is really a file */
    331  1.110.2.2  mjf 			F_flag = 1;
    332  1.110.2.2  mjf 			break;
    333  1.110.2.2  mjf 		case 'S':	/* Output as shell variables */
    334  1.110.2.2  mjf 			sh_flag = 1;
    335  1.110.2.2  mjf 			break;
    336  1.110.2.2  mjf 		case 'a':	/* Set active partition */
    337  1.110.2.2  mjf 			a_flag = 1;
    338  1.110.2.2  mjf 			break;
    339  1.110.2.2  mjf 		case 'f':	/* Non interactive */
    340  1.110.2.2  mjf 			f_flag = 1;
    341  1.110.2.2  mjf 			break;
    342  1.110.2.2  mjf 		case 'i':	/* Always update bootcode */
    343  1.110.2.2  mjf 			i_flag = 1;
    344  1.110.2.2  mjf 			break;
    345  1.110.2.2  mjf 		case 'l':	/* List known partition types */
    346  1.110.2.2  mjf 			for (len = 0; len < KNOWN_SYSIDS; len++)
    347  1.110.2.2  mjf 				printf("%03d %s\n", mbr_ptypes[len].id,
    348  1.110.2.2  mjf 				    mbr_ptypes[len].name);
    349  1.110.2.2  mjf 			return 0;
    350  1.110.2.2  mjf 		case 'u':	/* Update partition details */
    351  1.110.2.2  mjf 			u_flag = 1;
    352  1.110.2.2  mjf 			break;
    353  1.110.2.2  mjf 		case 'v':	/* Be verbose */
    354  1.110.2.2  mjf 			v_flag++;
    355  1.110.2.2  mjf 			break;
    356  1.110.2.2  mjf 		case 's':	/* Partition details */
    357  1.110.2.2  mjf 			s_flag = 1;
    358  1.110.2.2  mjf 			if (sscanf(optarg, "%d/%d/%d%n", &csysid, &cstart,
    359  1.110.2.2  mjf 			    &csize, &n) == 3) {
    360  1.110.2.2  mjf 				if (optarg[n] == 0)
    361  1.110.2.2  mjf 					break;
    362  1.110.2.2  mjf #ifdef BOOTSEL
    363  1.110.2.2  mjf 				if (optarg[n] == '/') {
    364  1.110.2.2  mjf 					cbootmenu = optarg + n + 1;
    365  1.110.2.2  mjf 					break;
    366  1.110.2.2  mjf 				}
    367  1.110.2.2  mjf #endif
    368  1.110.2.2  mjf 			}
    369  1.110.2.2  mjf 			errx(1, "Bad argument to the -s flag.");
    370  1.110.2.2  mjf 			break;
    371  1.110.2.2  mjf 		case 'b':	/* BIOS geometry */
    372  1.110.2.2  mjf 			b_flag = 1;
    373  1.110.2.2  mjf 			if (sscanf(optarg, "%d/%d/%d%n", &b_cyl, &b_head,
    374  1.110.2.2  mjf 			    &b_sec, &n) != 3 || optarg[n] != 0)
    375  1.110.2.2  mjf 				errx(1, "Bad argument to the -b flag.");
    376  1.110.2.2  mjf 			if (b_cyl > MAXCYL)
    377  1.110.2.2  mjf 				b_cyl = MAXCYL;
    378  1.110.2.2  mjf 			break;
    379  1.110.2.2  mjf 		case 'c':	/* file/directory containing boot code */
    380  1.110.2.2  mjf 			if (strchr(optarg, '/') != NULL &&
    381  1.110.2.2  mjf 			    stat(optarg, &sb) == 0 &&
    382  1.110.2.2  mjf 			    (sb.st_mode & S_IFMT) == S_IFDIR) {
    383  1.110.2.2  mjf 				boot_dir = optarg;
    384  1.110.2.2  mjf 				break;
    385  1.110.2.2  mjf 			}
    386  1.110.2.2  mjf 			bootsize = read_boot(optarg, bootcode,
    387  1.110.2.2  mjf 						sizeof bootcode, 1);
    388  1.110.2.2  mjf 			i_flag = 1;
    389  1.110.2.2  mjf 			break;
    390  1.110.2.2  mjf 		case 'r':	/* read data from disk_file (not raw disk) */
    391  1.110.2.2  mjf 			rfd = &wfd;
    392  1.110.2.2  mjf 			/* FALLTHROUGH */
    393  1.110.2.2  mjf 		case 'w':	/* write data to disk_file */
    394  1.110.2.2  mjf 			disk_file = optarg;
    395  1.110.2.2  mjf 			break;
    396  1.110.2.2  mjf 		case 't':
    397  1.110.2.2  mjf 			if (setdisktab(optarg) == -1)
    398  1.110.2.2  mjf 				errx(EXIT_FAILURE, "bad disktab");
    399  1.110.2.2  mjf 			break;
    400  1.110.2.2  mjf 		case 'T':
    401  1.110.2.2  mjf 			disk_type = optarg;
    402  1.110.2.2  mjf 			break;
    403  1.110.2.2  mjf 		default:
    404  1.110.2.2  mjf 			usage();
    405  1.110.2.2  mjf 		}
    406  1.110.2.2  mjf 	argc -= optind;
    407  1.110.2.2  mjf 	argv += optind;
    408  1.110.2.2  mjf 
    409  1.110.2.2  mjf 	if (disk_type != NULL && getdiskbyname(disk_type) == NULL)
    410  1.110.2.2  mjf 		errx(EXIT_FAILURE, "bad disktype");
    411  1.110.2.2  mjf 
    412  1.110.2.2  mjf 	if (sh_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
    413  1.110.2.2  mjf 		usage();
    414  1.110.2.2  mjf 
    415  1.110.2.2  mjf 	if (B_flag && f_flag) {
    416  1.110.2.2  mjf 		warnx("Bootselector may only be configured interactively");
    417  1.110.2.2  mjf 		usage();
    418  1.110.2.2  mjf 	}
    419  1.110.2.2  mjf 
    420  1.110.2.2  mjf 	if (f_flag && u_flag && !s_flag) {
    421  1.110.2.2  mjf 		warnx("Partition data not specified");
    422  1.110.2.2  mjf 		usage();
    423  1.110.2.2  mjf 	}
    424  1.110.2.2  mjf 
    425  1.110.2.2  mjf 	if (s_flag && partition == -1) {
    426  1.110.2.2  mjf 		warnx("-s flag requires a partition selected.");
    427  1.110.2.2  mjf 		usage();
    428  1.110.2.2  mjf 	}
    429  1.110.2.2  mjf 
    430  1.110.2.2  mjf 	if (argc > 1)
    431  1.110.2.2  mjf 		usage();
    432  1.110.2.2  mjf 
    433  1.110.2.2  mjf 	if (argc > 0)
    434  1.110.2.2  mjf 		disk = argv[0];
    435  1.110.2.2  mjf 	else if (!F_flag) {
    436  1.110.2.2  mjf 		/* Default to boot device */
    437  1.110.2.2  mjf 		initvar_disk(&disk);
    438  1.110.2.2  mjf 	}
    439  1.110.2.2  mjf 
    440  1.110.2.2  mjf 	if (open_disk(B_flag || a_flag || i_flag || u_flag) < 0)
    441  1.110.2.2  mjf 		exit(1);
    442  1.110.2.2  mjf 
    443  1.110.2.2  mjf 	if (read_s0(0, &mboot))
    444  1.110.2.2  mjf 		/* must have been a blank disk */
    445  1.110.2.2  mjf 		init_sector0(1);
    446  1.110.2.2  mjf 
    447  1.110.2.2  mjf #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
    448  1.110.2.2  mjf 	get_geometry();
    449  1.110.2.2  mjf #else
    450  1.110.2.2  mjf 	intuit_translated_geometry();
    451  1.110.2.2  mjf #endif
    452  1.110.2.2  mjf 	get_extended_ptn();
    453  1.110.2.2  mjf 
    454  1.110.2.2  mjf #ifdef BOOTSEL
    455  1.110.2.2  mjf 	default_ptn = get_default_boot();
    456  1.110.2.2  mjf #endif
    457  1.110.2.2  mjf 
    458  1.110.2.2  mjf 	if (E_flag && !u_flag && partition >= ext.num_ptn)
    459  1.110.2.2  mjf 		errx(1, "Extended partition %d is not defined.", partition);
    460  1.110.2.2  mjf 
    461  1.110.2.2  mjf 	if (u_flag && (!f_flag || b_flag))
    462  1.110.2.2  mjf 		get_params_to_use();
    463  1.110.2.2  mjf 
    464  1.110.2.2  mjf 	/* Do the update stuff! */
    465  1.110.2.2  mjf 	if (u_flag) {
    466  1.110.2.2  mjf 		if (s_flag)
    467  1.110.2.2  mjf 			change_part(E_flag, partition, csysid, cstart, csize,
    468  1.110.2.2  mjf 				cbootmenu);
    469  1.110.2.2  mjf 		else {
    470  1.110.2.2  mjf 			int part = partition, chg_ext = E_flag, prompt = 1;
    471  1.110.2.2  mjf 			do {
    472  1.110.2.2  mjf 				if (prompt) {
    473  1.110.2.2  mjf 					printf("\n");
    474  1.110.2.2  mjf 					print_s0(partition);
    475  1.110.2.2  mjf 				}
    476  1.110.2.2  mjf 				if (partition == -1)
    477  1.110.2.2  mjf 					part = ptn_id(
    478  1.110.2.2  mjf 				    "Which partition do you want to change?",
    479  1.110.2.2  mjf 							&chg_ext);
    480  1.110.2.2  mjf 				if (part < 0)
    481  1.110.2.2  mjf 					break;
    482  1.110.2.2  mjf 				prompt = change_part(chg_ext, part, 0, 0, 0, 0);
    483  1.110.2.2  mjf 			} while (partition == -1);
    484  1.110.2.2  mjf 		}
    485  1.110.2.2  mjf 	} else
    486  1.110.2.2  mjf 		if (!i_flag && !B_flag) {
    487  1.110.2.2  mjf 			print_params();
    488  1.110.2.2  mjf 			print_s0(partition);
    489  1.110.2.2  mjf 		}
    490  1.110.2.2  mjf 
    491  1.110.2.2  mjf 	if (a_flag && !E_flag)
    492  1.110.2.2  mjf 		change_active(partition);
    493  1.110.2.2  mjf 
    494  1.110.2.2  mjf #ifdef BOOTSEL
    495  1.110.2.2  mjf 	if (B_flag || u_flag || i_flag)
    496  1.110.2.2  mjf 		/* Ensure the mbr code supports this configuration */
    497  1.110.2.2  mjf 		install_bootsel(0);
    498  1.110.2.2  mjf 	if (B_flag)
    499  1.110.2.2  mjf 		default_ptn = configure_bootsel(default_ptn);
    500  1.110.2.2  mjf 	set_default_boot(default_ptn);
    501  1.110.2.2  mjf #else
    502  1.110.2.2  mjf 	if (i_flag)
    503  1.110.2.2  mjf 		init_sector0(0);
    504  1.110.2.2  mjf #endif
    505  1.110.2.2  mjf 
    506  1.110.2.2  mjf 	if (u_flag || a_flag || i_flag || B_flag) {
    507  1.110.2.2  mjf 		if (!f_flag) {
    508  1.110.2.2  mjf 			printf("\nWe haven't written the MBR back to disk "
    509  1.110.2.2  mjf 			       "yet.  This is your last chance.\n");
    510  1.110.2.2  mjf 			if (u_flag)
    511  1.110.2.2  mjf 				print_s0(-1);
    512  1.110.2.2  mjf 			if (yesno("Should we write new partition table?"))
    513  1.110.2.2  mjf 				write_mbr();
    514  1.110.2.2  mjf 		} else
    515  1.110.2.2  mjf 			write_mbr();
    516  1.110.2.2  mjf 	}
    517  1.110.2.2  mjf 
    518  1.110.2.2  mjf 	exit(0);
    519  1.110.2.2  mjf }
    520  1.110.2.2  mjf 
    521  1.110.2.2  mjf void
    522  1.110.2.2  mjf usage(void)
    523  1.110.2.2  mjf {
    524  1.110.2.2  mjf 	int indent = 7 + (int)strlen(getprogname()) + 1;
    525  1.110.2.2  mjf 
    526  1.110.2.2  mjf 	(void)fprintf(stderr, "usage: %s [-afiluvBS] "
    527  1.110.2.2  mjf 		"[-b cylinders/heads/sectors] \\\n"
    528  1.110.2.2  mjf 		"%*s[-0123 | -E num "
    529  1.110.2.2  mjf 		"[-s id/start/size[/bootmenu]]] \\\n"
    530  1.110.2.2  mjf 		"%*s[-t disktab] [-T disktype] \\\n"
    531  1.110.2.2  mjf 		"%*s[-c bootcode] "
    532  1.110.2.2  mjf 		"[-r|-w file] [device]\n"
    533  1.110.2.2  mjf 		"\t-a change active partition\n"
    534  1.110.2.2  mjf 		"\t-f force - not interactive\n"
    535  1.110.2.2  mjf 		"\t-i initialise MBR code\n"
    536  1.110.2.2  mjf 		"\t-l list partition types\n"
    537  1.110.2.2  mjf 		"\t-u update partition data\n"
    538  1.110.2.2  mjf 		"\t-v verbose output, -v -v more verbose still\n"
    539  1.110.2.2  mjf 		"\t-B update bootselect options\n"
    540  1.110.2.2  mjf 		"\t-F treat device as a regular file\n"
    541  1.110.2.2  mjf 		"\t-S output as shell defines\n"
    542  1.110.2.2  mjf 		"\t-r and -w access 'file' for non-destructive testing\n",
    543  1.110.2.2  mjf 		getprogname(), indent, "", indent, "", indent, "");
    544  1.110.2.2  mjf 	exit(1);
    545  1.110.2.2  mjf }
    546  1.110.2.2  mjf 
    547  1.110.2.2  mjf static daddr_t
    548  1.110.2.2  mjf ext_offset(int part)
    549  1.110.2.2  mjf {
    550  1.110.2.2  mjf 	daddr_t offset = ext.base;
    551  1.110.2.2  mjf 
    552  1.110.2.2  mjf 	if (part != 0)
    553  1.110.2.2  mjf 		offset += le32toh(ext.ptn[part - 1].mbr_parts[1].mbrp_start);
    554  1.110.2.2  mjf 	return offset;
    555  1.110.2.2  mjf }
    556  1.110.2.2  mjf 
    557  1.110.2.2  mjf void
    558  1.110.2.2  mjf print_s0(int which)
    559  1.110.2.2  mjf {
    560  1.110.2.2  mjf 	int part;
    561  1.110.2.2  mjf 
    562  1.110.2.2  mjf 	if (which == -1) {
    563  1.110.2.2  mjf 		if (!sh_flag)
    564  1.110.2.2  mjf 			printf("Partition table:\n");
    565  1.110.2.2  mjf 		for (part = 0; part < MBR_PART_COUNT; part++) {
    566  1.110.2.2  mjf 			if (!sh_flag)
    567  1.110.2.2  mjf 				printf("%d: ", part);
    568  1.110.2.2  mjf 			print_part(&mboot, part, 0);
    569  1.110.2.2  mjf 		}
    570  1.110.2.2  mjf 		if (!sh_flag) {
    571  1.110.2.2  mjf 			if (ext.is_corrupt)
    572  1.110.2.2  mjf 				printf("Extended partition table is corrupt\n");
    573  1.110.2.2  mjf 			else
    574  1.110.2.2  mjf 				if (ext.num_ptn != 0)
    575  1.110.2.2  mjf 					printf("Extended partition table:\n");
    576  1.110.2.2  mjf 		}
    577  1.110.2.2  mjf 		for (part = 0; part < ext.num_ptn; part++) {
    578  1.110.2.2  mjf 			if (!sh_flag)
    579  1.110.2.2  mjf 				printf("E%d: ", part);
    580  1.110.2.2  mjf 			print_part(&ext.ptn[part], 0, ext_offset(part));
    581  1.110.2.2  mjf 			if (!sh_flag && v_flag >= 2) {
    582  1.110.2.2  mjf 				printf("link: ");
    583  1.110.2.2  mjf 				print_mbr_partition(&ext.ptn[part], 1,
    584  1.110.2.2  mjf 						ext_offset(part), ext.base, 0);
    585  1.110.2.2  mjf 			}
    586  1.110.2.2  mjf 		}
    587  1.110.2.2  mjf #ifdef BOOTSEL
    588  1.110.2.2  mjf 		if (!sh_flag && mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC) {
    589  1.110.2.2  mjf 			int tmo;
    590  1.110.2.2  mjf 
    591  1.110.2.2  mjf 			printf("Bootselector ");
    592  1.110.2.2  mjf 			if (mboot.mbr_bootsel.mbrbs_flags & MBR_BS_ACTIVE) {
    593  1.110.2.2  mjf 				printf("enabled");
    594  1.110.2.2  mjf 				tmo = le16toh(mboot.mbr_bootsel.mbrbs_timeo);
    595  1.110.2.2  mjf 				if (tmo == 0xffff)
    596  1.110.2.2  mjf 					printf(", infinite timeout");
    597  1.110.2.2  mjf 				else
    598  1.110.2.2  mjf 					printf(", timeout %d seconds",
    599  1.110.2.2  mjf 						    (10 * tmo + 9) / 182);
    600  1.110.2.2  mjf 			} else
    601  1.110.2.2  mjf 				printf("disabled");
    602  1.110.2.2  mjf 			printf(".\n");
    603  1.110.2.2  mjf 		}
    604  1.110.2.2  mjf #endif
    605  1.110.2.2  mjf 		if (!sh_flag) {
    606  1.110.2.2  mjf 			int active = first_active();
    607  1.110.2.2  mjf 			if (active == MBR_PART_COUNT)
    608  1.110.2.2  mjf 				printf("No active partition.\n");
    609  1.110.2.2  mjf 			else
    610  1.110.2.2  mjf 				printf("First active partition: %d\n", active);
    611  1.110.2.2  mjf 		}
    612  1.110.2.2  mjf 		if (!sh_flag && mboot.mbr_dsn != 0)
    613  1.110.2.2  mjf 			printf("Drive serial number: %"PRId32" (0x%08x)\n",
    614  1.110.2.2  mjf 			    le32toh(mboot.mbr_dsn),
    615  1.110.2.2  mjf 			    le32toh(mboot.mbr_dsn));
    616  1.110.2.2  mjf 		return;
    617  1.110.2.2  mjf 	}
    618  1.110.2.2  mjf 
    619  1.110.2.2  mjf 	if (E_flag) {
    620  1.110.2.2  mjf 		if (!sh_flag)
    621  1.110.2.2  mjf 			printf("Extended partition E%d:\n", which);
    622  1.110.2.2  mjf 		if (which > ext.num_ptn)
    623  1.110.2.2  mjf 			printf("Undefined\n");
    624  1.110.2.2  mjf 		else
    625  1.110.2.2  mjf 			print_part(&ext.ptn[which], 0, ext_offset(which));
    626  1.110.2.2  mjf 	} else {
    627  1.110.2.2  mjf 		if (!sh_flag)
    628  1.110.2.2  mjf 			printf("Partition %d:\n", which);
    629  1.110.2.2  mjf 		print_part(&mboot, which, 0);
    630  1.110.2.2  mjf 	}
    631  1.110.2.2  mjf }
    632  1.110.2.2  mjf 
    633  1.110.2.2  mjf void
    634  1.110.2.2  mjf print_part(struct mbr_sector *boot, int part, daddr_t offset)
    635  1.110.2.2  mjf {
    636  1.110.2.2  mjf 	struct mbr_partition *partp;
    637  1.110.2.2  mjf 	const char *e;
    638  1.110.2.2  mjf 
    639  1.110.2.2  mjf 	if (!sh_flag) {
    640  1.110.2.2  mjf 		print_mbr_partition(boot, part, offset, 0, 0);
    641  1.110.2.2  mjf 		return;
    642  1.110.2.2  mjf 	}
    643  1.110.2.2  mjf 
    644  1.110.2.2  mjf 	partp = &boot->mbr_parts[part];
    645  1.110.2.2  mjf 	if (boot != &mboot) {
    646  1.110.2.2  mjf 		part = boot - ext.ptn;
    647  1.110.2.2  mjf 		e = "E";
    648  1.110.2.2  mjf 	} else
    649  1.110.2.2  mjf 		e = "";
    650  1.110.2.2  mjf 
    651  1.110.2.2  mjf 	if (partp->mbrp_type == 0) {
    652  1.110.2.2  mjf 		printf("PART%s%dSIZE=0\n", e, part);
    653  1.110.2.2  mjf 		return;
    654  1.110.2.2  mjf 	}
    655  1.110.2.2  mjf 
    656  1.110.2.2  mjf 	printf("PART%s%dID=%d\n", e, part, partp->mbrp_type);
    657  1.110.2.2  mjf 	printf("PART%s%dSIZE=%u\n", e, part, le32toh(partp->mbrp_size));
    658  1.110.2.2  mjf 	printf("PART%s%dSTART=%"PRIdaddr"\n", e, part,
    659  1.110.2.2  mjf 	    offset + le32toh(partp->mbrp_start));
    660  1.110.2.2  mjf 	printf("PART%s%dFLAG=0x%x\n", e, part, partp->mbrp_flag);
    661  1.110.2.2  mjf 	printf("PART%s%dBCYL=%d\n", e, part,
    662  1.110.2.2  mjf 	    MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect));
    663  1.110.2.2  mjf 	printf("PART%s%dBHEAD=%d\n", e, part, partp->mbrp_shd);
    664  1.110.2.2  mjf 	printf("PART%s%dBSEC=%d\n", e, part, MBR_PSECT(partp->mbrp_ssect));
    665  1.110.2.2  mjf 	printf("PART%s%dECYL=%d\n", e, part,
    666  1.110.2.2  mjf 	    MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect));
    667  1.110.2.2  mjf 	printf("PART%s%dEHEAD=%d\n", e, part, partp->mbrp_ehd);
    668  1.110.2.2  mjf 	printf("PART%s%dESEC=%d\n", e, part, MBR_PSECT(partp->mbrp_esect));
    669  1.110.2.2  mjf }
    670  1.110.2.2  mjf 
    671  1.110.2.2  mjf static void
    672  1.110.2.2  mjf pr_cyls(daddr_t sector, int is_end)
    673  1.110.2.2  mjf {
    674  1.110.2.2  mjf 	unsigned long cyl, head, sect;
    675  1.110.2.2  mjf 	cyl = sector / dos_cylindersectors;
    676  1.110.2.2  mjf 	sect = sector - cyl * dos_cylindersectors;
    677  1.110.2.2  mjf 	head = sect / dos_sectors;
    678  1.110.2.2  mjf 	sect -= head * dos_sectors;
    679  1.110.2.2  mjf 
    680  1.110.2.2  mjf 	printf("%lu", cyl);
    681  1.110.2.2  mjf 
    682  1.110.2.2  mjf 	if (is_end) {
    683  1.110.2.2  mjf 		if (head == dos_heads - 1 && sect == dos_sectors - 1)
    684  1.110.2.2  mjf 			return;
    685  1.110.2.2  mjf 	} else {
    686  1.110.2.2  mjf 		if (head == 0 && sect == 0)
    687  1.110.2.2  mjf 			return;
    688  1.110.2.2  mjf 	}
    689  1.110.2.2  mjf 
    690  1.110.2.2  mjf 	printf("/%lu/%lu", head, sect + 1);
    691  1.110.2.2  mjf }
    692  1.110.2.2  mjf 
    693  1.110.2.2  mjf void
    694  1.110.2.2  mjf print_mbr_partition(struct mbr_sector *boot, int part,
    695  1.110.2.2  mjf     daddr_t offset, daddr_t exoffset, int indent)
    696  1.110.2.2  mjf {
    697  1.110.2.2  mjf 	daddr_t	start;
    698  1.110.2.2  mjf 	daddr_t	size;
    699  1.110.2.2  mjf 	struct mbr_partition *partp = &boot->mbr_parts[part];
    700  1.110.2.2  mjf 	struct mbr_sector eboot;
    701  1.110.2.2  mjf 	int p;
    702  1.110.2.2  mjf 	static int dumped = 0;
    703  1.110.2.2  mjf 
    704  1.110.2.2  mjf 	if (partp->mbrp_type == 0 && v_flag < 2) {
    705  1.110.2.2  mjf 		printf("<UNUSED>\n");
    706  1.110.2.2  mjf 		return;
    707  1.110.2.2  mjf 	}
    708  1.110.2.2  mjf 
    709  1.110.2.2  mjf 	start = le32toh(partp->mbrp_start);
    710  1.110.2.2  mjf 	size = le32toh(partp->mbrp_size);
    711  1.110.2.2  mjf 	if (MBR_IS_EXTENDED(partp->mbrp_type))
    712  1.110.2.2  mjf 		start += exoffset;
    713  1.110.2.2  mjf 	else
    714  1.110.2.2  mjf 		start += offset;
    715  1.110.2.2  mjf 
    716  1.110.2.2  mjf 	printf("%s (sysid %d)\n", get_type(partp->mbrp_type), partp->mbrp_type);
    717  1.110.2.2  mjf #ifdef BOOTSEL
    718  1.110.2.2  mjf 	if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC &&
    719  1.110.2.2  mjf 	    boot->mbr_bootsel.mbrbs_nametab[part][0])
    720  1.110.2.2  mjf 		printf("%*s    bootmenu: %s\n", indent, "",
    721  1.110.2.2  mjf 		    boot->mbr_bootsel.mbrbs_nametab[part]);
    722  1.110.2.2  mjf #endif
    723  1.110.2.2  mjf 
    724  1.110.2.2  mjf 	printf("%*s    start %"PRIdaddr", size %"PRIdaddr,
    725  1.110.2.2  mjf 	    indent, "", start, size);
    726  1.110.2.2  mjf 	if (size != 0) {
    727  1.110.2.2  mjf 		printf(" (%u MB, Cyls ", SEC_TO_MB(size));
    728  1.110.2.2  mjf 		if (v_flag == 0 && le32toh(partp->mbrp_start) == dos_sectors)
    729  1.110.2.2  mjf 			pr_cyls(start - dos_sectors, 0);
    730  1.110.2.2  mjf 		else
    731  1.110.2.2  mjf 			pr_cyls(start, 0);
    732  1.110.2.2  mjf 		printf("-");
    733  1.110.2.2  mjf 		pr_cyls(start + size - 1, 1);
    734  1.110.2.2  mjf 		printf(")");
    735  1.110.2.2  mjf 	}
    736  1.110.2.2  mjf 
    737  1.110.2.2  mjf 	switch (partp->mbrp_flag) {
    738  1.110.2.2  mjf 	case 0:
    739  1.110.2.2  mjf 		break;
    740  1.110.2.2  mjf 	case MBR_PFLAG_ACTIVE:
    741  1.110.2.2  mjf 		printf(", Active");
    742  1.110.2.2  mjf 		break;
    743  1.110.2.2  mjf 	default:
    744  1.110.2.2  mjf 		printf(", flag 0x%x", partp->mbrp_flag);
    745  1.110.2.2  mjf 		break;
    746  1.110.2.2  mjf 	}
    747  1.110.2.2  mjf 	printf("\n");
    748  1.110.2.2  mjf 
    749  1.110.2.2  mjf 	if (v_flag) {
    750  1.110.2.2  mjf 		printf("%*s        beg: cylinder %4d, head %3d, sector %2d\n",
    751  1.110.2.2  mjf 		    indent, "",
    752  1.110.2.2  mjf 		    MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect),
    753  1.110.2.2  mjf 		    partp->mbrp_shd, MBR_PSECT(partp->mbrp_ssect));
    754  1.110.2.2  mjf 		printf("%*s        end: cylinder %4d, head %3d, sector %2d\n",
    755  1.110.2.2  mjf 		    indent, "",
    756  1.110.2.2  mjf 		    MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect),
    757  1.110.2.2  mjf 		    partp->mbrp_ehd, MBR_PSECT(partp->mbrp_esect));
    758  1.110.2.2  mjf 	}
    759  1.110.2.2  mjf 
    760  1.110.2.2  mjf 	if (partp->mbrp_type == 0 && start == 0 && v_flag < 3)
    761  1.110.2.2  mjf 		return;
    762  1.110.2.2  mjf 
    763  1.110.2.2  mjf 	if (! MBR_IS_EXTENDED(partp->mbrp_type))
    764  1.110.2.2  mjf 		print_pbr(start, indent + 8, partp->mbrp_type);
    765  1.110.2.2  mjf 
    766  1.110.2.2  mjf 	if (!MBR_IS_EXTENDED(partp->mbrp_type) ||
    767  1.110.2.2  mjf 	    (v_flag <= 2 && !ext.is_corrupt))
    768  1.110.2.2  mjf 		return;
    769  1.110.2.2  mjf 
    770  1.110.2.2  mjf 	/*
    771  1.110.2.2  mjf 	 * Recursive dump extended table,
    772  1.110.2.2  mjf 	 * This is read from the disk - so is wrong during editing.
    773  1.110.2.2  mjf 	 * Just ensure we only show it once.
    774  1.110.2.2  mjf 	 */
    775  1.110.2.2  mjf 	if (dumped)
    776  1.110.2.2  mjf 		return;
    777  1.110.2.2  mjf 
    778  1.110.2.2  mjf 	printf("%*s    Extended partition table:\n", indent, "");
    779  1.110.2.2  mjf 	indent += 4;
    780  1.110.2.2  mjf 	if (read_s0(start, &eboot) == -1)
    781  1.110.2.2  mjf 		return;
    782  1.110.2.2  mjf 	for (p = 0; p < MBR_PART_COUNT; p++) {
    783  1.110.2.2  mjf 		printf("%*s%d: ", indent, "", p);
    784  1.110.2.2  mjf 		print_mbr_partition(&eboot, p, start,
    785  1.110.2.2  mjf 				    exoffset ? exoffset : start, indent);
    786  1.110.2.2  mjf 	}
    787  1.110.2.2  mjf 
    788  1.110.2.2  mjf 	if (exoffset == 0)
    789  1.110.2.2  mjf 		dumped = 1;
    790  1.110.2.2  mjf }
    791  1.110.2.2  mjf 
    792  1.110.2.2  mjf /* Print a line with a label and a vis-encoded string */
    793  1.110.2.2  mjf void
    794  1.110.2.2  mjf printvis(int indent, const char *label, const char *buf, size_t size)
    795  1.110.2.2  mjf {
    796  1.110.2.2  mjf 	char *visbuf;
    797  1.110.2.2  mjf 
    798  1.110.2.2  mjf 	if ((visbuf = malloc(size * 4 + 1)) == NULL)
    799  1.110.2.2  mjf 		err(1, "Malloc failed");
    800  1.110.2.2  mjf 	strsvisx(visbuf, buf, size, VIS_TAB|VIS_NL|VIS_OCTAL, "\"");
    801  1.110.2.2  mjf 	printf("%*s%s: \"%s\"\n",
    802  1.110.2.2  mjf 	    indent, "",
    803  1.110.2.2  mjf 	    label, visbuf);
    804  1.110.2.2  mjf 	free(visbuf);
    805  1.110.2.2  mjf }
    806  1.110.2.2  mjf 
    807  1.110.2.2  mjf /* Check whether a buffer contains all bytes zero */
    808  1.110.2.2  mjf int
    809  1.110.2.2  mjf is_all_zero(const unsigned char *p, size_t size)
    810  1.110.2.2  mjf {
    811  1.110.2.2  mjf 
    812  1.110.2.2  mjf 	while (size-- > 0) {
    813  1.110.2.2  mjf 		if (*p++ != 0)
    814  1.110.2.2  mjf 			return 0;
    815  1.110.2.2  mjf 	}
    816  1.110.2.2  mjf 	return 1;
    817  1.110.2.2  mjf }
    818  1.110.2.2  mjf 
    819  1.110.2.2  mjf /*
    820  1.110.2.2  mjf  * Report on the contents of a PBR sector.
    821  1.110.2.2  mjf  *
    822  1.110.2.2  mjf  * We first perform several sanity checks.  If vflag >= 2, we report all
    823  1.110.2.2  mjf  * failing tests, but for smaller values of v_flag we stop after the
    824  1.110.2.2  mjf  * first failing test.  Tests are ordered in an attempt to get the most
    825  1.110.2.2  mjf  * useful error message from the first failing test.
    826  1.110.2.2  mjf  *
    827  1.110.2.2  mjf  * If v_flag >= 2, we also report some decoded values from the PBR.
    828  1.110.2.2  mjf  * These results may be meaningless, if the PBR doesn't follow common
    829  1.110.2.2  mjf  * conventions.
    830  1.110.2.2  mjf  *
    831  1.110.2.2  mjf  * Trying to decode anything more than the magic number in the last
    832  1.110.2.2  mjf  * two bytes is a layering violation, but it can be very useful in
    833  1.110.2.2  mjf  * diagnosing boot failures.
    834  1.110.2.2  mjf  */
    835  1.110.2.2  mjf void
    836  1.110.2.2  mjf print_pbr(daddr_t sector, int indent, uint8_t part_type)
    837  1.110.2.2  mjf {
    838  1.110.2.2  mjf 	struct mbr_sector pboot;
    839  1.110.2.2  mjf 	unsigned char *p, *endp;
    840  1.110.2.2  mjf 	unsigned char val;
    841  1.110.2.2  mjf 	int ok;
    842  1.110.2.2  mjf 	int errcount = 0;
    843  1.110.2.2  mjf 
    844  1.110.2.2  mjf #define PBR_ERROR(...)							\
    845  1.110.2.2  mjf 	do {								\
    846  1.110.2.2  mjf 		++errcount;						\
    847  1.110.2.2  mjf 		printf("%*s%s: ", indent, "",				\
    848  1.110.2.2  mjf 		    (v_flag < 2 ? "PBR is not bootable" : "Not bootable")); \
    849  1.110.2.2  mjf 		printf(__VA_ARGS__);					\
    850  1.110.2.2  mjf 		if (v_flag < 2)						\
    851  1.110.2.2  mjf 			return;						\
    852  1.110.2.2  mjf 	} while (/*CONSTCOND*/ 0)
    853  1.110.2.2  mjf 
    854  1.110.2.2  mjf 	if (v_flag >= 2) {
    855  1.110.2.2  mjf 		printf("%*sInformation from PBR:\n",
    856  1.110.2.2  mjf 		    indent, "");
    857  1.110.2.2  mjf 		indent += 4;
    858  1.110.2.2  mjf 	}
    859  1.110.2.2  mjf 
    860  1.110.2.2  mjf 	if (read_disk(sector, &pboot) == -1) {
    861  1.110.2.2  mjf 		PBR_ERROR("Sector %"PRIdaddr" is unreadable (%s)\n",
    862  1.110.2.2  mjf 		    sector, strerror(errno));
    863  1.110.2.2  mjf 		return;
    864  1.110.2.2  mjf 	}
    865  1.110.2.2  mjf 
    866  1.110.2.2  mjf 	/* all bytes identical? */
    867  1.110.2.2  mjf 	p = (unsigned char *)&pboot;
    868  1.110.2.2  mjf 	endp = p + sizeof(pboot);
    869  1.110.2.2  mjf 	val = *p;
    870  1.110.2.2  mjf 	ok = 0;
    871  1.110.2.2  mjf 	for (; p < endp; p++) {
    872  1.110.2.2  mjf 		if (*p != val) {
    873  1.110.2.2  mjf 			ok = 1;
    874  1.110.2.2  mjf 			break;
    875  1.110.2.2  mjf 		}
    876  1.110.2.2  mjf 	}
    877  1.110.2.2  mjf 	if (! ok)
    878  1.110.2.2  mjf 		PBR_ERROR("All bytes are identical (0x%02x)\n", val);
    879  1.110.2.2  mjf 
    880  1.110.2.2  mjf 	if (pboot.mbr_magic != LE_MBR_MAGIC)
    881  1.110.2.2  mjf 		PBR_ERROR("Bad magic number (0x%04x)\n",
    882  1.110.2.2  mjf 			le16toh(pboot.mbr_magic));
    883  1.110.2.2  mjf 
    884  1.110.2.2  mjf #if 0
    885  1.110.2.2  mjf 	/* Some i386 OS might fail this test.  All non-i386 will fail. */
    886  1.110.2.2  mjf 	if (pboot.mbr_jmpboot[0] != 0xE9
    887  1.110.2.2  mjf 	    && pboot.mbr_jmpboot[0] != 0xEB) {
    888  1.110.2.2  mjf 		PBR_ERROR("Does not begin with i386 JMP instruction"
    889  1.110.2.2  mjf 			" (0x%02x 0x%02x0 0x%02x)\n",
    890  1.110.2.2  mjf 		    pboot.mbr_jmpboot[0], pboot.mbr_jmpboot[1],
    891  1.110.2.2  mjf 		    pboot.mbr_jmpboot[2]);
    892  1.110.2.2  mjf 	}
    893  1.110.2.2  mjf #endif
    894  1.110.2.2  mjf 
    895  1.110.2.2  mjf 	if (v_flag > 0 && errcount == 0)
    896  1.110.2.2  mjf 		printf("%*sPBR appears to be bootable\n",
    897  1.110.2.2  mjf 		    indent, "");
    898  1.110.2.2  mjf 	if (v_flag < 2)
    899  1.110.2.2  mjf 		return;
    900  1.110.2.2  mjf 
    901  1.110.2.2  mjf 	if (! is_all_zero(pboot.mbr_oemname, sizeof(pboot.mbr_oemname))) {
    902  1.110.2.2  mjf 		printvis(indent, "OEM name", (char *)pboot.mbr_oemname,
    903  1.110.2.2  mjf 			sizeof(pboot.mbr_oemname));
    904  1.110.2.2  mjf 	}
    905  1.110.2.2  mjf 
    906  1.110.2.2  mjf 	if (pboot.mbr_bpb.bpb16.bsBootSig == 0x29)
    907  1.110.2.2  mjf 		printf("%*sBPB FAT16 boot signature found\n",
    908  1.110.2.2  mjf 		    indent, "");
    909  1.110.2.2  mjf 	if (pboot.mbr_bpb.bpb32.bsBootSig == 0x29)
    910  1.110.2.2  mjf 		printf("%*sBPB FAT32 boot signature found\n",
    911  1.110.2.2  mjf 		    indent, "");
    912  1.110.2.2  mjf 
    913  1.110.2.2  mjf #undef PBR_ERROR
    914  1.110.2.2  mjf }
    915  1.110.2.2  mjf 
    916  1.110.2.2  mjf int
    917  1.110.2.2  mjf read_boot(const char *name, void *buf, size_t len, int err_exit)
    918  1.110.2.2  mjf {
    919  1.110.2.2  mjf 	int bfd, ret;
    920  1.110.2.2  mjf 	struct stat st;
    921  1.110.2.2  mjf 
    922  1.110.2.2  mjf 	if (boot_path != NULL)
    923  1.110.2.2  mjf 		free(boot_path);
    924  1.110.2.2  mjf 	if (strchr(name, '/') == 0)
    925  1.110.2.2  mjf 		asprintf(&boot_path, "%s/%s", boot_dir, name);
    926  1.110.2.2  mjf 	else
    927  1.110.2.2  mjf 		boot_path = strdup(name);
    928  1.110.2.2  mjf 	if (boot_path == NULL)
    929  1.110.2.2  mjf 		err(1, "Malloc failed");
    930  1.110.2.2  mjf 
    931  1.110.2.2  mjf 	if ((bfd = open(boot_path, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
    932  1.110.2.2  mjf 		warn("%s", boot_path);
    933  1.110.2.2  mjf 		goto fail;
    934  1.110.2.2  mjf 	}
    935  1.110.2.2  mjf 
    936  1.110.2.2  mjf 	if (st.st_size > (off_t)len) {
    937  1.110.2.2  mjf 		warnx("%s: bootcode too large", boot_path);
    938  1.110.2.2  mjf 		goto fail;
    939  1.110.2.2  mjf 	}
    940  1.110.2.2  mjf 	ret = st.st_size;
    941  1.110.2.2  mjf 	if (ret < 0x200) {
    942  1.110.2.2  mjf 		warnx("%s: bootcode too small", boot_path);
    943  1.110.2.2  mjf 		goto fail;
    944  1.110.2.2  mjf 	}
    945  1.110.2.2  mjf 	if (read(bfd, buf, len) != ret) {
    946  1.110.2.2  mjf 		warn("%s", boot_path);
    947  1.110.2.2  mjf 		goto fail;
    948  1.110.2.2  mjf 	}
    949  1.110.2.2  mjf 
    950  1.110.2.2  mjf 	/*
    951  1.110.2.2  mjf 	 * Do some sanity checking here
    952  1.110.2.2  mjf 	 */
    953  1.110.2.2  mjf 	if (((struct mbr_sector *)buf)->mbr_magic != LE_MBR_MAGIC) {
    954  1.110.2.2  mjf 		warnx("%s: invalid magic", boot_path);
    955  1.110.2.2  mjf 		goto fail;
    956  1.110.2.2  mjf 	}
    957  1.110.2.2  mjf 
    958  1.110.2.2  mjf 	close(bfd);
    959  1.110.2.2  mjf 	ret = (ret + 0x1ff) & ~0x1ff;
    960  1.110.2.2  mjf 	return ret;
    961  1.110.2.2  mjf 
    962  1.110.2.2  mjf     fail:
    963  1.110.2.2  mjf 	if (bfd >= 0)
    964  1.110.2.2  mjf 		close(bfd);
    965  1.110.2.2  mjf 	if (err_exit)
    966  1.110.2.2  mjf 		exit(1);
    967  1.110.2.2  mjf 	return 0;
    968  1.110.2.2  mjf }
    969  1.110.2.2  mjf 
    970  1.110.2.2  mjf void
    971  1.110.2.2  mjf init_sector0(int zappart)
    972  1.110.2.2  mjf {
    973  1.110.2.2  mjf 	int i;
    974  1.110.2.2  mjf 	int copy_size = offsetof(struct mbr_sector, mbr_dsn);
    975  1.110.2.2  mjf 
    976  1.110.2.2  mjf #ifdef DEFAULT_BOOTCODE
    977  1.110.2.2  mjf 	if (bootsize == 0)
    978  1.110.2.2  mjf 		bootsize = read_boot(DEFAULT_BOOTCODE, bootcode,
    979  1.110.2.2  mjf 			sizeof bootcode, 1);
    980  1.110.2.2  mjf #endif
    981  1.110.2.2  mjf #ifdef BOOTSEL
    982  1.110.2.2  mjf 	if (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC
    983  1.110.2.2  mjf 	    && bootcode[0].mbr_bootsel_magic == LE_MBR_BS_MAGIC)
    984  1.110.2.2  mjf 		copy_size = MBR_BS_OFFSET;
    985  1.110.2.2  mjf #endif
    986  1.110.2.2  mjf 
    987  1.110.2.2  mjf 	if (bootsize != 0) {
    988  1.110.2.2  mjf 		boot_installed = 1;
    989  1.110.2.2  mjf 		memcpy(&mboot, bootcode, copy_size);
    990  1.110.2.2  mjf 		mboot.mbr_bootsel_magic = bootcode[0].mbr_bootsel_magic;
    991  1.110.2.2  mjf 	}
    992  1.110.2.2  mjf 	mboot.mbr_magic = LE_MBR_MAGIC;
    993  1.110.2.2  mjf 
    994  1.110.2.2  mjf 	if (!zappart)
    995  1.110.2.2  mjf 		return;
    996  1.110.2.2  mjf 	for (i = 0; i < MBR_PART_COUNT; i++)
    997  1.110.2.2  mjf 		memset(&mboot.mbr_parts[i], 0, sizeof(mboot.mbr_parts[i]));
    998  1.110.2.2  mjf }
    999  1.110.2.2  mjf 
   1000  1.110.2.2  mjf void
   1001  1.110.2.2  mjf get_extended_ptn(void)
   1002  1.110.2.2  mjf {
   1003  1.110.2.2  mjf 	struct mbr_partition *mp;
   1004  1.110.2.2  mjf 	struct mbr_sector *boot;
   1005  1.110.2.2  mjf 	daddr_t offset;
   1006  1.110.2.2  mjf 	struct mbr_sector *nptn;
   1007  1.110.2.2  mjf 
   1008  1.110.2.2  mjf 	/* find first (there should only be one) extended partition */
   1009  1.110.2.2  mjf 	for (mp = mboot.mbr_parts; !MBR_IS_EXTENDED(mp->mbrp_type); mp++)
   1010  1.110.2.2  mjf 		if (mp >= &mboot.mbr_parts[MBR_PART_COUNT])
   1011  1.110.2.2  mjf 			return;
   1012  1.110.2.2  mjf 
   1013  1.110.2.2  mjf 	/*
   1014  1.110.2.2  mjf 	 * The extended partition should be structured as a linked list
   1015  1.110.2.2  mjf 	 * (even though it appears, at first glance, to be a tree).
   1016  1.110.2.2  mjf 	 */
   1017  1.110.2.2  mjf 	ext.base = le32toh(mp->mbrp_start);
   1018  1.110.2.2  mjf 	ext.limit = ext.base + le32toh(mp->mbrp_size);
   1019  1.110.2.2  mjf 	ext.ptn_id = mp - mboot.mbr_parts;
   1020  1.110.2.2  mjf 	for (offset = 0;; offset = le32toh(boot->mbr_parts[1].mbrp_start)) {
   1021  1.110.2.2  mjf 		nptn = realloc(ext.ptn, (ext.num_ptn + 1) * sizeof *ext.ptn);
   1022  1.110.2.2  mjf 		if (nptn == NULL)
   1023  1.110.2.2  mjf 			err(1, "Malloc failed");
   1024  1.110.2.2  mjf 		ext.ptn = nptn;
   1025  1.110.2.2  mjf 		boot = ext.ptn + ext.num_ptn;
   1026  1.110.2.2  mjf 		if (read_s0(offset + ext.base, boot) == -1)
   1027  1.110.2.2  mjf 			break;
   1028  1.110.2.2  mjf 		/* expect p0 to be valid and p1 to be another extended ptn */
   1029  1.110.2.2  mjf 		if (MBR_IS_EXTENDED(boot->mbr_parts[0].mbrp_type))
   1030  1.110.2.2  mjf 			break;
   1031  1.110.2.2  mjf 		if (boot->mbr_parts[1].mbrp_type != 0 &&
   1032  1.110.2.2  mjf 		    !MBR_IS_EXTENDED(boot->mbr_parts[1].mbrp_type))
   1033  1.110.2.2  mjf 			break;
   1034  1.110.2.2  mjf 		/* p2 and p3 should be unallocated */
   1035  1.110.2.2  mjf 		if (boot->mbr_parts[2].mbrp_type != 0 ||
   1036  1.110.2.2  mjf 		    boot->mbr_parts[3].mbrp_type != 0)
   1037  1.110.2.2  mjf 			break;
   1038  1.110.2.2  mjf 		/* data ptn inside extended one */
   1039  1.110.2.2  mjf 		if (boot->mbr_parts[0].mbrp_type != 0 &&
   1040  1.110.2.2  mjf 		    offset + le32toh(boot->mbr_parts[0].mbrp_start)
   1041  1.110.2.2  mjf 		    + le32toh(boot->mbr_parts[0].mbrp_size) > ext.limit)
   1042  1.110.2.2  mjf 			break;
   1043  1.110.2.2  mjf 
   1044  1.110.2.2  mjf 		ext.num_ptn++;
   1045  1.110.2.2  mjf 
   1046  1.110.2.2  mjf 		if (boot->mbr_parts[1].mbrp_type == 0)
   1047  1.110.2.2  mjf 			/* end of extended partition chain */
   1048  1.110.2.2  mjf 			return;
   1049  1.110.2.2  mjf 		/* must be in sector order */
   1050  1.110.2.2  mjf 		if (offset >= le32toh(boot->mbr_parts[1].mbrp_start))
   1051  1.110.2.2  mjf 			break;
   1052  1.110.2.2  mjf 	}
   1053  1.110.2.2  mjf 
   1054  1.110.2.2  mjf 	warnx("Extended partition table is corrupt\n");
   1055  1.110.2.2  mjf 	ext.is_corrupt = 1;
   1056  1.110.2.2  mjf 	ext.num_ptn = 0;
   1057  1.110.2.2  mjf }
   1058  1.110.2.2  mjf 
   1059  1.110.2.2  mjf #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
   1060  1.110.2.2  mjf void
   1061  1.110.2.2  mjf get_diskname(const char *fullname, char *diskname, size_t size)
   1062  1.110.2.2  mjf {
   1063  1.110.2.2  mjf 	const char *p, *p2;
   1064  1.110.2.2  mjf 	size_t len;
   1065  1.110.2.2  mjf 
   1066  1.110.2.2  mjf 	p = strrchr(fullname, '/');
   1067  1.110.2.2  mjf 	if (p == NULL)
   1068  1.110.2.2  mjf 		p = fullname;
   1069  1.110.2.2  mjf 	else
   1070  1.110.2.2  mjf 		p++;
   1071  1.110.2.2  mjf 
   1072  1.110.2.2  mjf 	if (*p == 0) {
   1073  1.110.2.2  mjf 		strlcpy(diskname, fullname, size);
   1074  1.110.2.2  mjf 		return;
   1075  1.110.2.2  mjf 	}
   1076  1.110.2.2  mjf 
   1077  1.110.2.2  mjf 	if (*p == 'r')
   1078  1.110.2.2  mjf 		p++;
   1079  1.110.2.2  mjf 
   1080  1.110.2.2  mjf 	for (p2 = p; *p2 != 0; p2++)
   1081  1.110.2.2  mjf 		if (isdigit((unsigned char)*p2))
   1082  1.110.2.2  mjf 			break;
   1083  1.110.2.2  mjf 	if (*p2 == 0) {
   1084  1.110.2.2  mjf 		/* XXX invalid diskname? */
   1085  1.110.2.2  mjf 		strlcpy(diskname, fullname, size);
   1086  1.110.2.2  mjf 		return;
   1087  1.110.2.2  mjf 	}
   1088  1.110.2.2  mjf 	while (isdigit((unsigned char)*p2))
   1089  1.110.2.2  mjf 		p2++;
   1090  1.110.2.2  mjf 
   1091  1.110.2.2  mjf 	len = p2 - p;
   1092  1.110.2.2  mjf 	if (len > size) {
   1093  1.110.2.2  mjf 		/* XXX */
   1094  1.110.2.2  mjf 		strlcpy(diskname, fullname, size);
   1095  1.110.2.2  mjf 		return;
   1096  1.110.2.2  mjf 	}
   1097  1.110.2.2  mjf 
   1098  1.110.2.2  mjf 	memcpy(diskname, p, len);
   1099  1.110.2.2  mjf 	diskname[len] = 0;
   1100  1.110.2.2  mjf }
   1101  1.110.2.2  mjf 
   1102  1.110.2.2  mjf void
   1103  1.110.2.2  mjf get_geometry(void)
   1104  1.110.2.2  mjf {
   1105  1.110.2.2  mjf 	int mib[2], i;
   1106  1.110.2.2  mjf 	size_t len;
   1107  1.110.2.2  mjf 	struct biosdisk_info *bip;
   1108  1.110.2.2  mjf 	struct nativedisk_info *nip;
   1109  1.110.2.2  mjf 	char diskname[8];
   1110  1.110.2.2  mjf 
   1111  1.110.2.2  mjf 	mib[0] = CTL_MACHDEP;
   1112  1.110.2.2  mjf 	mib[1] = CPU_DISKINFO;
   1113  1.110.2.2  mjf 	if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) {
   1114  1.110.2.2  mjf 		goto out;
   1115  1.110.2.2  mjf 	}
   1116  1.110.2.2  mjf 	dl = (struct disklist *) malloc(len);
   1117  1.110.2.2  mjf 	if (dl == NULL)
   1118  1.110.2.2  mjf 		err(1, "Malloc failed");
   1119  1.110.2.2  mjf 	if (sysctl(mib, 2, dl, &len, NULL, 0) < 0) {
   1120  1.110.2.2  mjf 		free(dl);
   1121  1.110.2.2  mjf 		dl = 0;
   1122  1.110.2.2  mjf 		goto out;
   1123  1.110.2.2  mjf 	}
   1124  1.110.2.2  mjf 
   1125  1.110.2.2  mjf 	get_diskname(disk, diskname, sizeof diskname);
   1126  1.110.2.2  mjf 
   1127  1.110.2.2  mjf 	for (i = 0; i < dl->dl_nnativedisks; i++) {
   1128  1.110.2.2  mjf 		nip = &dl->dl_nativedisks[i];
   1129  1.110.2.2  mjf 		if (strcmp(diskname, nip->ni_devname))
   1130  1.110.2.2  mjf 			continue;
   1131  1.110.2.2  mjf 		/*
   1132  1.110.2.2  mjf 		 * XXX listing possible matches is better. This is ok for
   1133  1.110.2.2  mjf 		 * now because the user has a chance to change it later.
   1134  1.110.2.2  mjf 		 * Also, if all the disks have the same parameters then we can
   1135  1.110.2.2  mjf 		 * just use them, we don't need to know which disk is which.
   1136  1.110.2.2  mjf 		 */
   1137  1.110.2.2  mjf 		if (nip->ni_nmatches != 0) {
   1138  1.110.2.2  mjf 			bip = &dl->dl_biosdisks[nip->ni_biosmatches[0]];
   1139  1.110.2.2  mjf 			dos_cylinders = bip->bi_cyl;
   1140  1.110.2.2  mjf 			dos_heads = bip->bi_head;
   1141  1.110.2.2  mjf 			dos_sectors = bip->bi_sec;
   1142  1.110.2.2  mjf 			if (bip->bi_lbasecs)
   1143  1.110.2.2  mjf 				dos_disksectors = bip->bi_lbasecs;
   1144  1.110.2.2  mjf 			return;
   1145  1.110.2.2  mjf 		}
   1146  1.110.2.2  mjf 	}
   1147  1.110.2.2  mjf  out:
   1148  1.110.2.2  mjf 	/* Allright, allright, make a stupid guess.. */
   1149  1.110.2.2  mjf 	intuit_translated_geometry();
   1150  1.110.2.2  mjf }
   1151  1.110.2.2  mjf #endif /* (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H */
   1152  1.110.2.2  mjf 
   1153  1.110.2.2  mjf #ifdef BOOTSEL
   1154  1.110.2.2  mjf daddr_t
   1155  1.110.2.2  mjf get_default_boot(void)
   1156  1.110.2.2  mjf {
   1157  1.110.2.2  mjf 	unsigned int id;
   1158  1.110.2.2  mjf 	int p;
   1159  1.110.2.2  mjf 
   1160  1.110.2.2  mjf 	if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC)
   1161  1.110.2.2  mjf 		/* default to first active partition */
   1162  1.110.2.2  mjf 		return DEFAULT_ACTIVE;
   1163  1.110.2.2  mjf 
   1164  1.110.2.2  mjf 	id = mboot.mbr_bootsel.mbrbs_defkey;
   1165  1.110.2.2  mjf 
   1166  1.110.2.3  mjf 	if (mboot.mbr_bootsel.mbrbs_flags & MBR_BS_ASCII) {
   1167  1.110.2.3  mjf 		/* Keycode is ascii */
   1168  1.110.2.3  mjf 		if (id == '\r')
   1169  1.110.2.3  mjf 		    return DEFAULT_ACTIVE;
   1170  1.110.2.3  mjf 		/* '1'+ => allocated partition id, 'a'+ => disk 0+ */
   1171  1.110.2.3  mjf 		if (id >= 'a' && id < 'a' + MAX_BIOS_DISKS)
   1172  1.110.2.3  mjf 			return DEFAULT_DISK(id - 'a');
   1173  1.110.2.3  mjf 		id -= '1';
   1174  1.110.2.3  mjf 	} else {
   1175  1.110.2.3  mjf 		/* keycode is PS/2 keycode */
   1176  1.110.2.3  mjf 		if (id == SCAN_ENTER)
   1177  1.110.2.3  mjf 			return DEFAULT_ACTIVE;
   1178  1.110.2.3  mjf 		/* 1+ => allocated partition id, F1+ => disk 0+ */
   1179  1.110.2.3  mjf 		if (id >= SCAN_F1 && id < SCAN_F1 + MAX_BIOS_DISKS)
   1180  1.110.2.3  mjf 			return DEFAULT_DISK(id - SCAN_F1);
   1181  1.110.2.3  mjf 		id -= SCAN_1;
   1182  1.110.2.3  mjf 	}
   1183  1.110.2.3  mjf 
   1184  1.110.2.3  mjf 	/* Convert partition index to the invariant start sector number */
   1185  1.110.2.2  mjf 
   1186  1.110.2.2  mjf 	for (p = 0; p < MBR_PART_COUNT; p++) {
   1187  1.110.2.2  mjf 		if (mboot.mbr_parts[p].mbrp_type == 0)
   1188  1.110.2.2  mjf 			continue;
   1189  1.110.2.2  mjf 		if (mboot.mbr_bootsel.mbrbs_nametab[p][0] == 0)
   1190  1.110.2.2  mjf 			continue;
   1191  1.110.2.2  mjf 		if (id-- == 0)
   1192  1.110.2.2  mjf 			return le32toh(mboot.mbr_parts[p].mbrp_start);
   1193  1.110.2.2  mjf 	}
   1194  1.110.2.2  mjf 
   1195  1.110.2.2  mjf 	for (p = 0; p < ext.num_ptn; p++) {
   1196  1.110.2.2  mjf 		if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
   1197  1.110.2.2  mjf 			continue;
   1198  1.110.2.2  mjf 		if (ext.ptn[p].mbr_bootsel.mbrbs_nametab[0][0] == 0)
   1199  1.110.2.2  mjf 			continue;
   1200  1.110.2.2  mjf 		if (id-- == 0)
   1201  1.110.2.2  mjf 			return ext_offset(p)
   1202  1.110.2.2  mjf 				+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_start);
   1203  1.110.2.2  mjf 	}
   1204  1.110.2.2  mjf 
   1205  1.110.2.2  mjf 	return DEFAULT_ACTIVE;
   1206  1.110.2.2  mjf }
   1207  1.110.2.2  mjf 
   1208  1.110.2.2  mjf void
   1209  1.110.2.2  mjf set_default_boot(daddr_t default_ptn)
   1210  1.110.2.2  mjf {
   1211  1.110.2.2  mjf 	int p;
   1212  1.110.2.3  mjf 	static const unsigned char key_list[] = { SCAN_ENTER, SCAN_F1, SCAN_1,
   1213  1.110.2.3  mjf 						'\r', 'a', '1' };
   1214  1.110.2.3  mjf 	const unsigned char *key = key_list;
   1215  1.110.2.2  mjf 
   1216  1.110.2.2  mjf 	if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC)
   1217  1.110.2.2  mjf 		/* sanity */
   1218  1.110.2.2  mjf 		return;
   1219  1.110.2.2  mjf 
   1220  1.110.2.3  mjf 	if (mboot.mbr_bootsel.mbrbs_flags & MBR_BS_ASCII)
   1221  1.110.2.3  mjf 		/* Use ascii values */
   1222  1.110.2.3  mjf 		key += 3;
   1223  1.110.2.3  mjf 
   1224  1.110.2.2  mjf 	if (default_ptn == DEFAULT_ACTIVE) {
   1225  1.110.2.3  mjf 		mboot.mbr_bootsel.mbrbs_defkey = key[0];
   1226  1.110.2.2  mjf 		return;
   1227  1.110.2.2  mjf 	}
   1228  1.110.2.2  mjf 
   1229  1.110.2.3  mjf 	if (default_ptn >= DEFAULT_DISK(0)
   1230  1.110.2.3  mjf 	    && default_ptn < DEFAULT_DISK(MAX_BIOS_DISKS)) {
   1231  1.110.2.3  mjf 		mboot.mbr_bootsel.mbrbs_defkey = key[1]
   1232  1.110.2.3  mjf 		    + default_ptn - DEFAULT_DISK(0);
   1233  1.110.2.3  mjf 		return;
   1234  1.110.2.3  mjf 	}
   1235  1.110.2.3  mjf 
   1236  1.110.2.3  mjf 	mboot.mbr_bootsel.mbrbs_defkey = key[2];
   1237  1.110.2.2  mjf 	for (p = 0; p < MBR_PART_COUNT; p++) {
   1238  1.110.2.2  mjf 		if (mboot.mbr_parts[p].mbrp_type == 0)
   1239  1.110.2.2  mjf 			continue;
   1240  1.110.2.2  mjf 		if (mboot.mbr_bootsel.mbrbs_nametab[p][0] == 0)
   1241  1.110.2.2  mjf 			continue;
   1242  1.110.2.3  mjf 		if (le32toh(mboot.mbr_parts[p].mbrp_start) == default_ptn)
   1243  1.110.2.2  mjf 			return;
   1244  1.110.2.3  mjf 		mboot.mbr_bootsel.mbrbs_defkey++;
   1245  1.110.2.2  mjf 	}
   1246  1.110.2.2  mjf 
   1247  1.110.2.2  mjf 	if (mboot.mbr_bootsel.mbrbs_flags & MBR_BS_EXTLBA) {
   1248  1.110.2.2  mjf 		for (p = 0; p < ext.num_ptn; p++) {
   1249  1.110.2.2  mjf 			if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
   1250  1.110.2.2  mjf 				continue;
   1251  1.110.2.2  mjf 			if (ext.ptn[p].mbr_bootsel.mbrbs_nametab[0][0] == 0)
   1252  1.110.2.2  mjf 				continue;
   1253  1.110.2.2  mjf 			if (le32toh(ext.ptn[p].mbr_parts[0].mbrp_start) +
   1254  1.110.2.3  mjf 			    ext_offset(p) == default_ptn)
   1255  1.110.2.2  mjf 				return;
   1256  1.110.2.3  mjf 			mboot.mbr_bootsel.mbrbs_defkey++;
   1257  1.110.2.2  mjf 		}
   1258  1.110.2.2  mjf 	}
   1259  1.110.2.2  mjf 
   1260  1.110.2.2  mjf 	/* Default to first active partition */
   1261  1.110.2.3  mjf 	mboot.mbr_bootsel.mbrbs_defkey = key[0];
   1262  1.110.2.2  mjf }
   1263  1.110.2.2  mjf 
   1264  1.110.2.2  mjf void
   1265  1.110.2.2  mjf install_bootsel(int needed)
   1266  1.110.2.2  mjf {
   1267  1.110.2.2  mjf 	struct mbr_bootsel *mbs = &mboot.mbr_bootsel;
   1268  1.110.2.2  mjf 	int p;
   1269  1.110.2.2  mjf 	int ext13 = 0;
   1270  1.110.2.2  mjf 	const char *code;
   1271  1.110.2.2  mjf 
   1272  1.110.2.2  mjf 	needed |= MBR_BS_NEWMBR;	/* need new bootsel code */
   1273  1.110.2.2  mjf 
   1274  1.110.2.2  mjf 	/* Work out which boot code we need for this configuration */
   1275  1.110.2.2  mjf 	for (p = 0; p < MBR_PART_COUNT; p++) {
   1276  1.110.2.2  mjf 		if (mboot.mbr_parts[p].mbrp_type == 0)
   1277  1.110.2.2  mjf 			continue;
   1278  1.110.2.2  mjf 		if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC)
   1279  1.110.2.2  mjf 			break;
   1280  1.110.2.2  mjf 		if (mbs->mbrbs_nametab[p][0] == 0)
   1281  1.110.2.2  mjf 			continue;
   1282  1.110.2.2  mjf 		needed |= MBR_BS_ACTIVE;
   1283  1.110.2.2  mjf 		if (le32toh(mboot.mbr_parts[p].mbrp_start) >= dos_totalsectors)
   1284  1.110.2.2  mjf 			ext13 = MBR_BS_EXTINT13;
   1285  1.110.2.2  mjf 	}
   1286  1.110.2.2  mjf 
   1287  1.110.2.2  mjf 	for (p = 0; p < ext.num_ptn; p++) {
   1288  1.110.2.2  mjf 		if (ext.ptn[p].mbr_bootsel_magic != LE_MBR_BS_MAGIC)
   1289  1.110.2.2  mjf 			continue;
   1290  1.110.2.2  mjf 		if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
   1291  1.110.2.2  mjf 			continue;
   1292  1.110.2.2  mjf 		if (ext.ptn[p].mbr_bootsel.mbrbs_nametab[p][0] == 0)
   1293  1.110.2.2  mjf 			continue;
   1294  1.110.2.2  mjf 		needed |= MBR_BS_EXTLBA | MBR_BS_ACTIVE;
   1295  1.110.2.2  mjf 	}
   1296  1.110.2.2  mjf 
   1297  1.110.2.2  mjf 	if (B_flag)
   1298  1.110.2.2  mjf 		needed |= MBR_BS_ACTIVE;
   1299  1.110.2.2  mjf 
   1300  1.110.2.2  mjf 	/* Is the installed code good enough ? */
   1301  1.110.2.2  mjf 	if (!i_flag && (needed == 0 ||
   1302  1.110.2.2  mjf 	    (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC
   1303  1.110.2.2  mjf 	    && (mbs->mbrbs_flags & needed) == needed))) {
   1304  1.110.2.2  mjf 		/* yes - just set flags */
   1305  1.110.2.2  mjf 		mbs->mbrbs_flags |= ext13;
   1306  1.110.2.2  mjf 		return;
   1307  1.110.2.2  mjf 	}
   1308  1.110.2.2  mjf 
   1309  1.110.2.2  mjf 	/* ok - we need to replace the bootcode */
   1310  1.110.2.2  mjf 
   1311  1.110.2.2  mjf 	if (f_flag && !(i_flag || B_flag)) {
   1312  1.110.2.2  mjf 		warnx("Installed bootfile doesn't support required options.");
   1313  1.110.2.2  mjf 		return;
   1314  1.110.2.2  mjf 	}
   1315  1.110.2.2  mjf 
   1316  1.110.2.2  mjf 	if (!f_flag && bootsize == 0 && !i_flag)
   1317  1.110.2.2  mjf 		/* Output an explanation for the 'update bootcode' prompt. */
   1318  1.110.2.2  mjf 		printf("\n%s\n",
   1319  1.110.2.2  mjf 		    "Installed bootfile doesn't support required options.");
   1320  1.110.2.2  mjf 
   1321  1.110.2.2  mjf 	/* Were we told a specific file ? (which we have already read) */
   1322  1.110.2.2  mjf 	/* If so check that it supports what we need. */
   1323  1.110.2.2  mjf 	if (bootsize != 0 && needed != 0
   1324  1.110.2.2  mjf 	    && (bootcode[0].mbr_bootsel_magic != LE_MBR_BS_MAGIC
   1325  1.110.2.2  mjf 	    || ((bootcode[0].mbr_bootsel.mbrbs_flags & needed) != needed))) {
   1326  1.110.2.2  mjf 		/* No it doesn't... */
   1327  1.110.2.2  mjf 		if (f_flag)
   1328  1.110.2.2  mjf 			warnx("Bootfile %s doesn't support "
   1329  1.110.2.2  mjf 				    "required bootsel options", boot_path );
   1330  1.110.2.2  mjf 			/* But install it anyway */
   1331  1.110.2.2  mjf 		else
   1332  1.110.2.2  mjf 			if (yesno("Bootfile %s doesn't support the required "
   1333  1.110.2.2  mjf 			    "options,\ninstall default bootfile instead?",
   1334  1.110.2.2  mjf 			    boot_path))
   1335  1.110.2.2  mjf 				bootsize = 0;
   1336  1.110.2.2  mjf 	}
   1337  1.110.2.2  mjf 
   1338  1.110.2.2  mjf 	if (bootsize == 0) {
   1339  1.110.2.2  mjf 		/* Get name of bootfile that supports the required facilities */
   1340  1.110.2.2  mjf 		code = DEFAULT_BOOTCODE;
   1341  1.110.2.2  mjf 		if (needed & MBR_BS_ACTIVE)
   1342  1.110.2.2  mjf 			code = DEFAULT_BOOTSELCODE;
   1343  1.110.2.2  mjf #ifdef DEFAULT_BOOTEXTCODE
   1344  1.110.2.2  mjf 		if (needed & MBR_BS_EXTLBA)
   1345  1.110.2.2  mjf 			code = DEFAULT_BOOTEXTCODE;
   1346  1.110.2.2  mjf #endif
   1347  1.110.2.2  mjf 
   1348  1.110.2.2  mjf 		bootsize = read_boot(code, bootcode, sizeof bootcode, 0);
   1349  1.110.2.2  mjf 		if (bootsize == 0)
   1350  1.110.2.2  mjf 			/* The old bootcode is better than no bootcode at all */
   1351  1.110.2.2  mjf 			return;
   1352  1.110.2.2  mjf 		if ((bootcode[0].mbr_bootsel.mbrbs_flags & needed) != needed)
   1353  1.110.2.2  mjf 			warnx("Default bootfile %s doesn't support required "
   1354  1.110.2.2  mjf 				"options.  Got flags 0x%x, wanted 0x%x\n",
   1355  1.110.2.2  mjf 				boot_path, bootcode[0].mbr_bootsel.mbrbs_flags,
   1356  1.110.2.2  mjf 				needed);
   1357  1.110.2.2  mjf 	}
   1358  1.110.2.2  mjf 
   1359  1.110.2.2  mjf 	if (!f_flag && !yesno("Update the bootcode from %s?", boot_path))
   1360  1.110.2.2  mjf 		return;
   1361  1.110.2.2  mjf 
   1362  1.110.2.2  mjf 	init_sector0(0);
   1363  1.110.2.2  mjf 
   1364  1.110.2.2  mjf 	if (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC)
   1365  1.110.2.2  mjf 		mbs->mbrbs_flags = bootcode[0].mbr_bootsel.mbrbs_flags | ext13;
   1366  1.110.2.2  mjf }
   1367  1.110.2.2  mjf 
   1368  1.110.2.2  mjf daddr_t
   1369  1.110.2.2  mjf configure_bootsel(daddr_t default_ptn)
   1370  1.110.2.2  mjf {
   1371  1.110.2.2  mjf 	struct mbr_bootsel *mbs = &mboot.mbr_bootsel;
   1372  1.110.2.2  mjf 	int i, item, opt;
   1373  1.110.2.2  mjf 	int tmo;
   1374  1.110.2.2  mjf 	daddr_t *off;
   1375  1.110.2.2  mjf 	int num_bios_disks;
   1376  1.110.2.2  mjf 
   1377  1.110.2.2  mjf #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
   1378  1.110.2.2  mjf 	if (dl != NULL) {
   1379  1.110.2.2  mjf 		num_bios_disks = dl->dl_nbiosdisks;
   1380  1.110.2.3  mjf 		if (num_bios_disks > MAX_BIOS_DISKS)
   1381  1.110.2.3  mjf 			num_bios_disks = MAX_BIOS_DISKS;
   1382  1.110.2.2  mjf 	} else
   1383  1.110.2.2  mjf #endif
   1384  1.110.2.3  mjf 		num_bios_disks = MAX_BIOS_DISKS;
   1385  1.110.2.2  mjf 
   1386  1.110.2.2  mjf 	printf("\nBoot selector configuration:\n");
   1387  1.110.2.2  mjf 
   1388  1.110.2.2  mjf 	/* The timeout value is in ticks, ~18.2 Hz. Avoid using floats.
   1389  1.110.2.2  mjf 	 * Ticks are nearly 64k/3600 - so our long timers are sligtly out!
   1390  1.110.2.2  mjf 	 * Newer bootcode always waits for 1 tick, so treats 0xffff
   1391  1.110.2.2  mjf 	 * as wait forever.
   1392  1.110.2.2  mjf 	 */
   1393  1.110.2.2  mjf 	tmo = le16toh(mbs->mbrbs_timeo);
   1394  1.110.2.2  mjf 	tmo = tmo == 0xffff ? -1 : (10 * tmo + 9) / 182;
   1395  1.110.2.2  mjf 	tmo = decimal("Timeout value (0 to 3600 seconds, -1 => never)",
   1396  1.110.2.2  mjf 			tmo, 0, -1, 3600);
   1397  1.110.2.2  mjf 	mbs->mbrbs_timeo = htole16(tmo == -1 ? 0xffff : (tmo * 182) / 10);
   1398  1.110.2.2  mjf 
   1399  1.110.2.2  mjf 	off = calloc(1 + MBR_PART_COUNT + ext.num_ptn + num_bios_disks, sizeof *off);
   1400  1.110.2.2  mjf 	if (off == NULL)
   1401  1.110.2.2  mjf 		err(1, "Malloc failed");
   1402  1.110.2.2  mjf 
   1403  1.110.2.2  mjf 	printf("Select the default boot option. Options are:\n\n");
   1404  1.110.2.2  mjf 	item = 0;
   1405  1.110.2.2  mjf 	opt = 0;
   1406  1.110.2.2  mjf 	off[opt] = DEFAULT_ACTIVE;
   1407  1.110.2.2  mjf 	printf("%d: The first active partition\n", opt);
   1408  1.110.2.2  mjf 	for (i = 0; i < MBR_PART_COUNT; i++) {
   1409  1.110.2.2  mjf 		if (mboot.mbr_parts[i].mbrp_type == 0)
   1410  1.110.2.2  mjf 			continue;
   1411  1.110.2.2  mjf 		if (mbs->mbrbs_nametab[i][0] == 0)
   1412  1.110.2.2  mjf 			continue;
   1413  1.110.2.2  mjf 		printf("%d: %s\n", ++opt, &mbs->mbrbs_nametab[i][0]);
   1414  1.110.2.2  mjf 		off[opt] = le32toh(mboot.mbr_parts[i].mbrp_start);
   1415  1.110.2.2  mjf 		if (off[opt] == default_ptn)
   1416  1.110.2.2  mjf 			item = opt;
   1417  1.110.2.2  mjf 	}
   1418  1.110.2.2  mjf 	if (mbs->mbrbs_flags & MBR_BS_EXTLBA) {
   1419  1.110.2.2  mjf 		for (i = 0; i < ext.num_ptn; i++) {
   1420  1.110.2.2  mjf 			if (ext.ptn[i].mbr_parts[0].mbrp_type == 0)
   1421  1.110.2.2  mjf 				continue;
   1422  1.110.2.2  mjf 			if (ext.ptn[i].mbr_bootsel.mbrbs_nametab[0][0] == 0)
   1423  1.110.2.2  mjf 				continue;
   1424  1.110.2.2  mjf 			printf("%d: %s\n",
   1425  1.110.2.2  mjf 			    ++opt, ext.ptn[i].mbr_bootsel.mbrbs_nametab[0]);
   1426  1.110.2.2  mjf 			off[opt] = ext_offset(i) +
   1427  1.110.2.2  mjf 			    le32toh(ext.ptn[i].mbr_parts[0].mbrp_start);
   1428  1.110.2.2  mjf 			if (off[opt] == default_ptn)
   1429  1.110.2.2  mjf 				item = opt;
   1430  1.110.2.2  mjf 		}
   1431  1.110.2.2  mjf 	}
   1432  1.110.2.2  mjf 	for (i = 0; i < num_bios_disks; i++) {
   1433  1.110.2.2  mjf 		printf("%d: Harddisk %d\n", ++opt, i);
   1434  1.110.2.3  mjf 		off[opt] = DEFAULT_DISK(i);
   1435  1.110.2.3  mjf 		if (DEFAULT_DISK(i) == default_ptn)
   1436  1.110.2.2  mjf 			item = opt;
   1437  1.110.2.2  mjf 	}
   1438  1.110.2.2  mjf 
   1439  1.110.2.2  mjf 	item = decimal("Default boot option", item, 0, 0, opt);
   1440  1.110.2.2  mjf 
   1441  1.110.2.2  mjf 	default_ptn = off[item];
   1442  1.110.2.2  mjf 	free(off);
   1443  1.110.2.2  mjf 	return default_ptn;
   1444  1.110.2.2  mjf }
   1445  1.110.2.2  mjf #endif /* BOOTSEL */
   1446  1.110.2.2  mjf 
   1447  1.110.2.2  mjf 
   1448  1.110.2.2  mjf /* Prerequisite: the disklabel parameters and master boot record must
   1449  1.110.2.2  mjf  *		 have been read (i.e. dos_* and mboot are meaningful).
   1450  1.110.2.2  mjf  * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and
   1451  1.110.2.2  mjf  *		  dos_cylindersectors to be consistent with what the
   1452  1.110.2.2  mjf  *		  partition table is using, if we can find a geometry
   1453  1.110.2.2  mjf  *		  which is consistent with all partition table entries.
   1454  1.110.2.2  mjf  *		  We may get the number of cylinders slightly wrong (in
   1455  1.110.2.2  mjf  *		  the conservative direction).  The idea is to be able
   1456  1.110.2.2  mjf  *		  to create a NetBSD partition on a disk we don't know
   1457  1.110.2.2  mjf  *		  the translated geometry of.
   1458  1.110.2.2  mjf  * This routine is only used for non-x86 systems or when we fail to
   1459  1.110.2.2  mjf  * get the BIOS geometry from the kernel.
   1460  1.110.2.2  mjf  */
   1461  1.110.2.2  mjf void
   1462  1.110.2.2  mjf intuit_translated_geometry(void)
   1463  1.110.2.2  mjf {
   1464  1.110.2.2  mjf 	int xcylinders = -1, xheads = -1, xsectors = -1, i, j;
   1465  1.110.2.2  mjf 	unsigned int c1, h1, s1, c2, h2, s2;
   1466  1.110.2.2  mjf 	unsigned long a1, a2;
   1467  1.110.2.2  mjf 	uint64_t num, denom;
   1468  1.110.2.2  mjf 
   1469  1.110.2.2  mjf 	/*
   1470  1.110.2.2  mjf 	 * The physical parameters may be invalid as bios geometry.
   1471  1.110.2.2  mjf 	 * If we cannot determine the actual bios geometry, we are
   1472  1.110.2.2  mjf 	 * better off picking a likely 'faked' geometry than leaving
   1473  1.110.2.2  mjf 	 * the invalid physical one.
   1474  1.110.2.2  mjf 	 */
   1475  1.110.2.2  mjf 
   1476  1.110.2.2  mjf 	if (dos_cylinders > MAXCYL || dos_heads > MAXHEAD ||
   1477  1.110.2.2  mjf 	    dos_sectors > MAXSECTOR) {
   1478  1.110.2.2  mjf 		h1 = MAXHEAD - 1;
   1479  1.110.2.2  mjf 		c1 = MAXCYL - 1;
   1480  1.110.2.2  mjf #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
   1481  1.110.2.2  mjf 		if (dl != NULL) {
   1482  1.110.2.2  mjf 			/* BIOS may use 256 heads or 1024 cylinders */
   1483  1.110.2.2  mjf 			for (i = 0; i < dl->dl_nbiosdisks; i++) {
   1484  1.110.2.2  mjf 				if (h1 < dl->dl_biosdisks[i].bi_head)
   1485  1.110.2.2  mjf 					h1 = dl->dl_biosdisks[i].bi_head;
   1486  1.110.2.2  mjf 				if (c1 < dl->dl_biosdisks[i].bi_cyl)
   1487  1.110.2.2  mjf 					c1 = dl->dl_biosdisks[i].bi_cyl;
   1488  1.110.2.2  mjf 			}
   1489  1.110.2.2  mjf 		}
   1490  1.110.2.2  mjf #endif
   1491  1.110.2.2  mjf 		dos_sectors = MAXSECTOR;
   1492  1.110.2.2  mjf 		dos_heads = h1;
   1493  1.110.2.2  mjf 		dos_cylinders = disklabel.d_secperunit / (MAXSECTOR * h1);
   1494  1.110.2.2  mjf 		if (dos_cylinders > c1)
   1495  1.110.2.2  mjf 			dos_cylinders = c1;
   1496  1.110.2.2  mjf 	}
   1497  1.110.2.2  mjf 
   1498  1.110.2.2  mjf 	/* Try to deduce the number of heads from two different mappings. */
   1499  1.110.2.2  mjf 	for (i = 0; i < MBR_PART_COUNT * 2 - 1; i++) {
   1500  1.110.2.2  mjf 		if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
   1501  1.110.2.2  mjf 			continue;
   1502  1.110.2.2  mjf 		a1 -= s1;
   1503  1.110.2.2  mjf 		for (j = i + 1; j < MBR_PART_COUNT * 2; j++) {
   1504  1.110.2.2  mjf 			if (get_mapping(j, &c2, &h2, &s2, &a2) < 0)
   1505  1.110.2.2  mjf 				continue;
   1506  1.110.2.2  mjf 			a2 -= s2;
   1507  1.110.2.2  mjf 			num = (uint64_t)h1 * a2 - (uint64_t)h2 * a1;
   1508  1.110.2.2  mjf 			denom = (uint64_t)c2 * a1 - (uint64_t)c1 * a2;
   1509  1.110.2.3  mjf 			if (denom != 0 && num != 0 && num % denom == 0) {
   1510  1.110.2.2  mjf 				xheads = num / denom;
   1511  1.110.2.2  mjf 				xsectors = a1 / (c1 * xheads + h1);
   1512  1.110.2.2  mjf 				break;
   1513  1.110.2.2  mjf 			}
   1514  1.110.2.2  mjf 		}
   1515  1.110.2.2  mjf 		if (xheads != -1)
   1516  1.110.2.2  mjf 			break;
   1517  1.110.2.2  mjf 	}
   1518  1.110.2.2  mjf 
   1519  1.110.2.2  mjf 	if (xheads == -1) {
   1520  1.110.2.2  mjf 		warnx("Cannot determine the number of heads");
   1521  1.110.2.2  mjf 		return;
   1522  1.110.2.2  mjf 	}
   1523  1.110.2.2  mjf 
   1524  1.110.2.2  mjf 	/* Estimate the number of cylinders. */
   1525  1.110.2.2  mjf 	xcylinders = disklabel.d_secperunit / xheads / xsectors;
   1526  1.110.2.2  mjf 	if (disklabel.d_secperunit > xcylinders * xheads * xsectors)
   1527  1.110.2.2  mjf 		xcylinders++;
   1528  1.110.2.2  mjf 
   1529  1.110.2.2  mjf 	/*
   1530  1.110.2.2  mjf 	 * Now verify consistency with each of the partition table entries.
   1531  1.110.2.2  mjf 	 * Be willing to shove cylinders up a little bit to make things work,
   1532  1.110.2.2  mjf 	 * but translation mismatches are fatal.
   1533  1.110.2.2  mjf 	 */
   1534  1.110.2.2  mjf 	for (i = 0; i < MBR_PART_COUNT * 2; i++) {
   1535  1.110.2.2  mjf 		if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
   1536  1.110.2.2  mjf 			continue;
   1537  1.110.2.2  mjf 		if (c1 >= MAXCYL - 2)
   1538  1.110.2.2  mjf 			continue;
   1539  1.110.2.2  mjf 		if (xsectors * (c1 * xheads + h1) + s1 != a1)
   1540  1.110.2.2  mjf 			return;
   1541  1.110.2.2  mjf 	}
   1542  1.110.2.2  mjf 
   1543  1.110.2.2  mjf 
   1544  1.110.2.2  mjf 	/* Everything checks out.
   1545  1.110.2.2  mjf 	 * Reset the geometry to use for further calculations.
   1546  1.110.2.2  mjf 	 * But cylinders cannot be > 1024.
   1547  1.110.2.2  mjf 	 */
   1548  1.110.2.2  mjf 	if (xcylinders > MAXCYL)
   1549  1.110.2.2  mjf 		dos_cylinders = MAXCYL;
   1550  1.110.2.2  mjf 	else
   1551  1.110.2.2  mjf 		dos_cylinders = xcylinders;
   1552  1.110.2.2  mjf 	dos_heads = xheads;
   1553  1.110.2.2  mjf 	dos_sectors = xsectors;
   1554  1.110.2.2  mjf }
   1555  1.110.2.2  mjf 
   1556  1.110.2.2  mjf /*
   1557  1.110.2.2  mjf  * For the purposes of intuit_translated_geometry(), treat the partition
   1558  1.110.2.2  mjf  * table as a list of eight mapping between (cylinder, head, sector)
   1559  1.110.2.2  mjf  * triplets and absolute sectors.  Get the relevant geometry triplet and
   1560  1.110.2.2  mjf  * absolute sectors for a given entry, or return -1 if it isn't present.
   1561  1.110.2.2  mjf  * Note: for simplicity, the returned sector is 0-based.
   1562  1.110.2.2  mjf  */
   1563  1.110.2.2  mjf int
   1564  1.110.2.2  mjf get_mapping(int i, unsigned int *cylinder, unsigned int *head, unsigned int *sector,
   1565  1.110.2.2  mjf     unsigned long *absolute)
   1566  1.110.2.2  mjf {
   1567  1.110.2.2  mjf 	struct mbr_partition *part = &mboot.mbr_parts[i / 2];
   1568  1.110.2.2  mjf 
   1569  1.110.2.2  mjf 	if (part->mbrp_type == 0)
   1570  1.110.2.2  mjf 		return -1;
   1571  1.110.2.2  mjf 	if (i % 2 == 0) {
   1572  1.110.2.2  mjf 		*cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect);
   1573  1.110.2.2  mjf 		*head = part->mbrp_shd;
   1574  1.110.2.3  mjf 		*sector = MBR_PSECT(part->mbrp_ssect);
   1575  1.110.2.2  mjf 		*absolute = le32toh(part->mbrp_start);
   1576  1.110.2.2  mjf 	} else {
   1577  1.110.2.2  mjf 		*cylinder = MBR_PCYL(part->mbrp_ecyl, part->mbrp_esect);
   1578  1.110.2.2  mjf 		*head = part->mbrp_ehd;
   1579  1.110.2.3  mjf 		*sector = MBR_PSECT(part->mbrp_esect);
   1580  1.110.2.2  mjf 		*absolute = le32toh(part->mbrp_start)
   1581  1.110.2.2  mjf 		    + le32toh(part->mbrp_size) - 1;
   1582  1.110.2.2  mjf 	}
   1583  1.110.2.2  mjf 	/* Sanity check the data against all zeroes */
   1584  1.110.2.2  mjf 	if ((*cylinder == 0) && (*sector == 0) && (*head == 0))
   1585  1.110.2.2  mjf 		return -1;
   1586  1.110.2.3  mjf 	/* sector numbers in the MBR partition table start at 1 */
   1587  1.110.2.3  mjf 	*sector = *sector - 1;
   1588  1.110.2.2  mjf 	/* Sanity check the data against max values */
   1589  1.110.2.2  mjf 	if ((((*cylinder * MAXHEAD) + *head) * MAXSECTOR + *sector) < *absolute)
   1590  1.110.2.2  mjf 		/* cannot be a CHS mapping */
   1591  1.110.2.2  mjf 		return -1;
   1592  1.110.2.2  mjf 	return 0;
   1593  1.110.2.2  mjf }
   1594  1.110.2.2  mjf 
   1595  1.110.2.2  mjf static void
   1596  1.110.2.2  mjf delete_ptn(int part)
   1597  1.110.2.2  mjf {
   1598  1.110.2.2  mjf 	if (part == ext.ptn_id) {
   1599  1.110.2.2  mjf 		/* forget all about the extended partition */
   1600  1.110.2.2  mjf 		free(ext.ptn);
   1601  1.110.2.2  mjf 		memset(&ext, 0, sizeof ext);
   1602  1.110.2.2  mjf 	}
   1603  1.110.2.2  mjf 
   1604  1.110.2.2  mjf 	mboot.mbr_parts[part].mbrp_type = 0;
   1605  1.110.2.2  mjf }
   1606  1.110.2.2  mjf 
   1607  1.110.2.2  mjf static void
   1608  1.110.2.2  mjf delete_ext_ptn(int part)
   1609  1.110.2.2  mjf {
   1610  1.110.2.2  mjf 
   1611  1.110.2.2  mjf 	if (part == 0) {
   1612  1.110.2.2  mjf 		ext.ptn[0].mbr_parts[0].mbrp_type = 0;
   1613  1.110.2.2  mjf 		return;
   1614  1.110.2.2  mjf 	}
   1615  1.110.2.2  mjf 	ext.ptn[part - 1].mbr_parts[1] = ext.ptn[part].mbr_parts[1];
   1616  1.110.2.2  mjf 	memmove(&ext.ptn[part], &ext.ptn[part + 1],
   1617  1.110.2.2  mjf 		(ext.num_ptn - part - 1) * sizeof ext.ptn[0]);
   1618  1.110.2.2  mjf 	ext.num_ptn--;
   1619  1.110.2.2  mjf }
   1620  1.110.2.2  mjf 
   1621  1.110.2.2  mjf static int
   1622  1.110.2.2  mjf add_ext_ptn(daddr_t start, daddr_t size)
   1623  1.110.2.2  mjf {
   1624  1.110.2.2  mjf 	int part;
   1625  1.110.2.2  mjf 	struct mbr_partition *partp;
   1626  1.110.2.2  mjf 	struct mbr_sector *nptn;
   1627  1.110.2.2  mjf 
   1628  1.110.2.2  mjf 	nptn = realloc(ext.ptn, (ext.num_ptn + 1) * sizeof *ext.ptn);
   1629  1.110.2.2  mjf 	if (!nptn)
   1630  1.110.2.2  mjf 		err(1, "realloc");
   1631  1.110.2.2  mjf 	ext.ptn = nptn;
   1632  1.110.2.2  mjf 	for (part = 0; part < ext.num_ptn; part++)
   1633  1.110.2.2  mjf 		if (ext_offset(part) > start)
   1634  1.110.2.2  mjf 			break;
   1635  1.110.2.2  mjf 	/* insert before 'part' - make space... */
   1636  1.110.2.2  mjf 	memmove(&ext.ptn[part + 1], &ext.ptn[part],
   1637  1.110.2.2  mjf 		(ext.num_ptn - part) * sizeof ext.ptn[0]);
   1638  1.110.2.2  mjf 	memset(&ext.ptn[part], 0, sizeof ext.ptn[0]);
   1639  1.110.2.2  mjf 	ext.ptn[part].mbr_magic = LE_MBR_MAGIC;
   1640  1.110.2.2  mjf 	/* we will be 'part' */
   1641  1.110.2.2  mjf 	if (part == 0) {
   1642  1.110.2.2  mjf 		/* link us to 'next' */
   1643  1.110.2.2  mjf 		partp = &ext.ptn[0].mbr_parts[1];
   1644  1.110.2.2  mjf 		/* offset will be fixed by caller */
   1645  1.110.2.2  mjf 		partp->mbrp_size = htole32(
   1646  1.110.2.2  mjf 		    le32toh(ext.ptn[1].mbr_parts[0].mbrp_start) +
   1647  1.110.2.2  mjf 		    le32toh(ext.ptn[1].mbr_parts[0].mbrp_size));
   1648  1.110.2.2  mjf 	} else {
   1649  1.110.2.2  mjf 		/* link us to prev's next */
   1650  1.110.2.2  mjf 		partp = &ext.ptn[part - 1].mbr_parts[1];
   1651  1.110.2.2  mjf 		ext.ptn[part].mbr_parts[1] = *partp;
   1652  1.110.2.2  mjf 		/* and prev onto us */
   1653  1.110.2.2  mjf 		partp->mbrp_start = htole32(start - dos_sectors - ext.base);
   1654  1.110.2.2  mjf 		partp->mbrp_size = htole32(size + dos_sectors);
   1655  1.110.2.2  mjf 	}
   1656  1.110.2.2  mjf 	partp->mbrp_type = 5;	/* as used by win98 */
   1657  1.110.2.2  mjf 	partp->mbrp_flag = 0;
   1658  1.110.2.2  mjf 	/* wallop in some CHS values - win98 doesn't saturate them */
   1659  1.110.2.2  mjf 	dos(le32toh(partp->mbrp_start),
   1660  1.110.2.2  mjf 	    &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
   1661  1.110.2.2  mjf 	dos(le32toh(partp->mbrp_start) + le32toh(partp->mbrp_size) - 1,
   1662  1.110.2.2  mjf 	    &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
   1663  1.110.2.2  mjf 	ext.num_ptn++;
   1664  1.110.2.2  mjf 
   1665  1.110.2.2  mjf 	return part;
   1666  1.110.2.2  mjf }
   1667  1.110.2.2  mjf 
   1668  1.110.2.2  mjf static const char *
   1669  1.110.2.2  mjf check_overlap(int part, int sysid, daddr_t start, daddr_t size, int fix)
   1670  1.110.2.2  mjf {
   1671  1.110.2.2  mjf 	int p;
   1672  1.110.2.2  mjf 	unsigned int p_s, p_e;
   1673  1.110.2.2  mjf 
   1674  1.110.2.2  mjf 	if (sysid != 0) {
   1675  1.110.2.2  mjf 		if (start == 0)
   1676  1.110.2.2  mjf 			return "Sector zero is reserved for the MBR";
   1677  1.110.2.2  mjf #if 0
   1678  1.110.2.2  mjf 		if (start < dos_sectors)
   1679  1.110.2.2  mjf 			/* This is just a convention, not a requirement */
   1680  1.110.2.2  mjf 			return "Track zero is reserved for the BIOS";
   1681  1.110.2.2  mjf #endif
   1682  1.110.2.2  mjf 		if (start + size > disksectors)
   1683  1.110.2.2  mjf 			return "Partition exceeds size of disk";
   1684  1.110.2.2  mjf 		for (p = 0; p < MBR_PART_COUNT; p++) {
   1685  1.110.2.2  mjf 			if (p == part || mboot.mbr_parts[p].mbrp_type == 0)
   1686  1.110.2.2  mjf 				continue;
   1687  1.110.2.2  mjf 			p_s = le32toh(mboot.mbr_parts[p].mbrp_start);
   1688  1.110.2.2  mjf 			p_e = p_s + le32toh(mboot.mbr_parts[p].mbrp_size);
   1689  1.110.2.2  mjf 			if (start + size <= p_s || start >= p_e)
   1690  1.110.2.2  mjf 				continue;
   1691  1.110.2.2  mjf 			if (f_flag) {
   1692  1.110.2.2  mjf 				if (fix)
   1693  1.110.2.2  mjf 					delete_ptn(p);
   1694  1.110.2.2  mjf 				return 0;
   1695  1.110.2.2  mjf 			}
   1696  1.110.2.2  mjf 			return "Overlaps another partition";
   1697  1.110.2.2  mjf 		}
   1698  1.110.2.2  mjf 	}
   1699  1.110.2.2  mjf 
   1700  1.110.2.2  mjf 	/* Are we trying to create an extended partition */
   1701  1.110.2.2  mjf 	if (!MBR_IS_EXTENDED(mboot.mbr_parts[part].mbrp_type)) {
   1702  1.110.2.2  mjf 		/* this wasn't the extended partition */
   1703  1.110.2.2  mjf 		if (!MBR_IS_EXTENDED(sysid))
   1704  1.110.2.2  mjf 			return 0;
   1705  1.110.2.2  mjf 		/* making an extended partition */
   1706  1.110.2.2  mjf 		if (ext.base != 0) {
   1707  1.110.2.2  mjf 			if (!f_flag)
   1708  1.110.2.2  mjf 				return "There cannot be 2 extended partitions";
   1709  1.110.2.2  mjf 			if (fix)
   1710  1.110.2.2  mjf 				delete_ptn(ext.ptn_id);
   1711  1.110.2.2  mjf 		}
   1712  1.110.2.2  mjf 		if (fix) {
   1713  1.110.2.2  mjf 			/* allocate a new extended partition */
   1714  1.110.2.2  mjf 			ext.ptn = calloc(1, sizeof ext.ptn[0]);
   1715  1.110.2.2  mjf 			if (ext.ptn == NULL)
   1716  1.110.2.2  mjf 				err(1, "Malloc failed");
   1717  1.110.2.2  mjf 			ext.ptn[0].mbr_magic = LE_MBR_MAGIC;
   1718  1.110.2.2  mjf 			ext.ptn_id = part;
   1719  1.110.2.2  mjf 			ext.base = start;
   1720  1.110.2.2  mjf 			ext.limit = start + size;
   1721  1.110.2.2  mjf 			ext.num_ptn = 1;
   1722  1.110.2.2  mjf 		}
   1723  1.110.2.2  mjf 		return 0;
   1724  1.110.2.2  mjf 	}
   1725  1.110.2.2  mjf 
   1726  1.110.2.2  mjf 	/* Check we haven't cut space allocated to an extended ptn */
   1727  1.110.2.2  mjf 
   1728  1.110.2.2  mjf 	if (!MBR_IS_EXTENDED(sysid)) {
   1729  1.110.2.2  mjf 		/* no longer an extended partition */
   1730  1.110.2.2  mjf 		if (fix) {
   1731  1.110.2.2  mjf 			/* Kill all memory of the extended partitions */
   1732  1.110.2.2  mjf 			delete_ptn(part);
   1733  1.110.2.2  mjf 			return 0;
   1734  1.110.2.2  mjf 		}
   1735  1.110.2.2  mjf 		if (ext.num_ptn == 0 ||
   1736  1.110.2.2  mjf 		    (ext.num_ptn == 1 && ext.ptn[0].mbr_parts[0].mbrp_type == 0))
   1737  1.110.2.2  mjf 			/* nothing in extended partition */
   1738  1.110.2.2  mjf 			return 0;
   1739  1.110.2.2  mjf 		if (f_flag)
   1740  1.110.2.2  mjf 			return 0;
   1741  1.110.2.2  mjf 		if (yesno("Do you really want to delete all the extended partitions?"))
   1742  1.110.2.2  mjf 			return 0;
   1743  1.110.2.2  mjf 		return "Extended partition busy";
   1744  1.110.2.2  mjf 	}
   1745  1.110.2.2  mjf 
   1746  1.110.2.2  mjf 	if (le32toh(mboot.mbr_parts[part].mbrp_start) != ext.base)
   1747  1.110.2.2  mjf 		/* maybe impossible, but an extra sanity check */
   1748  1.110.2.2  mjf 		return 0;
   1749  1.110.2.2  mjf 
   1750  1.110.2.2  mjf 	for (p = ext.num_ptn; --p >= 0;) {
   1751  1.110.2.2  mjf 		if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
   1752  1.110.2.2  mjf 			continue;
   1753  1.110.2.2  mjf 		p_s = ext_offset(p);
   1754  1.110.2.2  mjf 		p_e = p_s + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
   1755  1.110.2.2  mjf 			  + le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
   1756  1.110.2.2  mjf 		if (p_s >= start && p_e <= start + size)
   1757  1.110.2.2  mjf 			continue;
   1758  1.110.2.2  mjf 		if (!f_flag)
   1759  1.110.2.2  mjf 			return "Extended partition outside main partition";
   1760  1.110.2.2  mjf 		if (fix)
   1761  1.110.2.2  mjf 			delete_ext_ptn(p);
   1762  1.110.2.2  mjf 	}
   1763  1.110.2.2  mjf 
   1764  1.110.2.2  mjf 	if (fix && start != ext.base) {
   1765  1.110.2.2  mjf 		/* The internal offsets need to be fixed up */
   1766  1.110.2.2  mjf 		for (p = 0; p < ext.num_ptn - 1; p++)
   1767  1.110.2.2  mjf 			ext.ptn[p].mbr_parts[1].mbrp_start = htole32(
   1768  1.110.2.2  mjf 			    le32toh(ext.ptn[p].mbr_parts[1].mbrp_start)
   1769  1.110.2.2  mjf 				    + ext.base - start);
   1770  1.110.2.2  mjf 		/* and maybe an empty partition at the start */
   1771  1.110.2.2  mjf 		if (ext.ptn[0].mbr_parts[0].mbrp_type == 0) {
   1772  1.110.2.2  mjf 			if (le32toh(ext.ptn[0].mbr_parts[1].mbrp_start) == 0) {
   1773  1.110.2.2  mjf 				/* don't need the empty slot */
   1774  1.110.2.2  mjf 				memmove(&ext.ptn[0], &ext.ptn[1],
   1775  1.110.2.2  mjf 					(ext.num_ptn - 1) * sizeof ext.ptn[0]);
   1776  1.110.2.2  mjf 				ext.num_ptn--;
   1777  1.110.2.2  mjf 			}
   1778  1.110.2.2  mjf 		} else {
   1779  1.110.2.2  mjf 			/* must create an empty slot */
   1780  1.110.2.2  mjf 			add_ext_ptn(start, dos_sectors);
   1781  1.110.2.2  mjf 			ext.ptn[0].mbr_parts[1].mbrp_start = htole32(ext.base
   1782  1.110.2.2  mjf 								- start);
   1783  1.110.2.2  mjf 		}
   1784  1.110.2.2  mjf 	}
   1785  1.110.2.2  mjf 	if (fix) {
   1786  1.110.2.2  mjf 		ext.base = start;
   1787  1.110.2.2  mjf 		ext.limit = start + size;
   1788  1.110.2.2  mjf 	}
   1789  1.110.2.2  mjf 	return 0;
   1790  1.110.2.2  mjf }
   1791  1.110.2.2  mjf 
   1792  1.110.2.2  mjf static const char *
   1793  1.110.2.2  mjf check_ext_overlap(int part, int sysid, daddr_t start, daddr_t size, int fix)
   1794  1.110.2.2  mjf {
   1795  1.110.2.2  mjf 	int p;
   1796  1.110.2.2  mjf 	unsigned int p_s, p_e;
   1797  1.110.2.2  mjf 
   1798  1.110.2.2  mjf 	if (sysid == 0)
   1799  1.110.2.2  mjf 		return 0;
   1800  1.110.2.2  mjf 
   1801  1.110.2.2  mjf 	if (MBR_IS_EXTENDED(sysid))
   1802  1.110.2.2  mjf 		return "Nested extended partitions are not allowed";
   1803  1.110.2.2  mjf 
   1804  1.110.2.2  mjf 	/* allow one track at start for extended partition header */
   1805  1.110.2.2  mjf 	start -= dos_sectors;
   1806  1.110.2.2  mjf 	size += dos_sectors;
   1807  1.110.2.2  mjf 	if (start < ext.base || start + size > ext.limit)
   1808  1.110.2.2  mjf 		return "Outside bounds of extended partition";
   1809  1.110.2.2  mjf 
   1810  1.110.2.2  mjf 	if (f_flag && !fix)
   1811  1.110.2.2  mjf 		return 0;
   1812  1.110.2.2  mjf 
   1813  1.110.2.2  mjf 	for (p = ext.num_ptn; --p >= 0;) {
   1814  1.110.2.2  mjf 		if (p == part || ext.ptn[p].mbr_parts[0].mbrp_type == 0)
   1815  1.110.2.2  mjf 			continue;
   1816  1.110.2.2  mjf 		p_s = ext_offset(p);
   1817  1.110.2.2  mjf 		p_e = p_s + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
   1818  1.110.2.2  mjf 			+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
   1819  1.110.2.2  mjf 		if (p == 0)
   1820  1.110.2.2  mjf 			p_s += le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
   1821  1.110.2.2  mjf 							- dos_sectors;
   1822  1.110.2.2  mjf 		if (start < p_e && start + size > p_s) {
   1823  1.110.2.2  mjf 			if (!f_flag)
   1824  1.110.2.2  mjf 				return "Overlaps another extended partition";
   1825  1.110.2.2  mjf 			if (fix) {
   1826  1.110.2.2  mjf 				if (part == -1)
   1827  1.110.2.2  mjf 					delete_ext_ptn(p);
   1828  1.110.2.2  mjf 				else
   1829  1.110.2.2  mjf 					/* must not change numbering yet */
   1830  1.110.2.2  mjf 					ext.ptn[p].mbr_parts[0].mbrp_type = 0;
   1831  1.110.2.2  mjf 			}
   1832  1.110.2.2  mjf 		}
   1833  1.110.2.2  mjf 	}
   1834  1.110.2.2  mjf 	return 0;
   1835  1.110.2.2  mjf }
   1836  1.110.2.2  mjf 
   1837  1.110.2.2  mjf int
   1838  1.110.2.2  mjf change_part(int extended, int part, int sysid, daddr_t start, daddr_t size,
   1839  1.110.2.2  mjf 	char *bootmenu)
   1840  1.110.2.2  mjf {
   1841  1.110.2.2  mjf 	struct mbr_partition *partp;
   1842  1.110.2.2  mjf 	struct mbr_sector *boot;
   1843  1.110.2.2  mjf 	daddr_t offset;
   1844  1.110.2.2  mjf 	const char *e;
   1845  1.110.2.2  mjf 	int upart = part;
   1846  1.110.2.2  mjf 	int p;
   1847  1.110.2.2  mjf 	int fl;
   1848  1.110.2.2  mjf 	daddr_t n_s, n_e;
   1849  1.110.2.2  mjf 	const char *errtext;
   1850  1.110.2.2  mjf #ifdef BOOTSEL
   1851  1.110.2.2  mjf 	char tmp_bootmenu[MBR_PART_COUNT * (MBR_BS_PARTNAMESIZE + 1)];
   1852  1.110.2.2  mjf 	int bootmenu_len = (extended ? MBR_PART_COUNT : 1) * (MBR_BS_PARTNAMESIZE + 1);
   1853  1.110.2.2  mjf #endif
   1854  1.110.2.2  mjf 
   1855  1.110.2.2  mjf 	if (extended) {
   1856  1.110.2.2  mjf 		if (part != -1 && part < ext.num_ptn) {
   1857  1.110.2.2  mjf 			boot = &ext.ptn[part];
   1858  1.110.2.2  mjf 			partp = &boot->mbr_parts[0];
   1859  1.110.2.2  mjf 			offset = ext_offset(part);
   1860  1.110.2.2  mjf 		} else {
   1861  1.110.2.2  mjf 			part = -1;
   1862  1.110.2.2  mjf 			boot = 0;
   1863  1.110.2.2  mjf 			partp = 0;
   1864  1.110.2.2  mjf 			offset = 0;
   1865  1.110.2.2  mjf 		}
   1866  1.110.2.2  mjf 		upart = 0;
   1867  1.110.2.2  mjf 		e = "E";
   1868  1.110.2.2  mjf 	} else {
   1869  1.110.2.2  mjf 		boot = &mboot;
   1870  1.110.2.2  mjf 		partp = &boot->mbr_parts[part];
   1871  1.110.2.2  mjf 		offset = 0;
   1872  1.110.2.2  mjf 		e = "";
   1873  1.110.2.2  mjf 	}
   1874  1.110.2.2  mjf 
   1875  1.110.2.2  mjf 	if (!f_flag && part != -1) {
   1876  1.110.2.2  mjf 		printf("The data for partition %s%d is:\n", e, part);
   1877  1.110.2.2  mjf 		print_part(boot, upart, offset);
   1878  1.110.2.2  mjf 	}
   1879  1.110.2.2  mjf 
   1880  1.110.2.2  mjf #ifdef BOOTSEL
   1881  1.110.2.2  mjf 	if (bootmenu != NULL)
   1882  1.110.2.2  mjf 		strlcpy(tmp_bootmenu, bootmenu, bootmenu_len);
   1883  1.110.2.2  mjf 	else
   1884  1.110.2.2  mjf 		if (boot != NULL && boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC)
   1885  1.110.2.2  mjf 			strlcpy(tmp_bootmenu,
   1886  1.110.2.2  mjf 				boot->mbr_bootsel.mbrbs_nametab[upart],
   1887  1.110.2.2  mjf 				bootmenu_len);
   1888  1.110.2.2  mjf 		else
   1889  1.110.2.2  mjf 			tmp_bootmenu[0] = 0;
   1890  1.110.2.2  mjf #endif
   1891  1.110.2.2  mjf 
   1892  1.110.2.2  mjf 	if (!s_flag && partp != NULL) {
   1893  1.110.2.2  mjf 		/* values not specified, default to current ones */
   1894  1.110.2.2  mjf 		sysid = partp->mbrp_type;
   1895  1.110.2.2  mjf 		start = offset + le32toh(partp->mbrp_start);
   1896  1.110.2.2  mjf 		size = le32toh(partp->mbrp_size);
   1897  1.110.2.2  mjf 	}
   1898  1.110.2.2  mjf 
   1899  1.110.2.2  mjf 	/* creating a new partition, default to free space */
   1900  1.110.2.2  mjf 	if (!s_flag && sysid == 0 && extended) {
   1901  1.110.2.2  mjf 		/* non-extended partition */
   1902  1.110.2.2  mjf 		start = ext.base;
   1903  1.110.2.2  mjf 		for (p = 0; p < ext.num_ptn; p++) {
   1904  1.110.2.2  mjf 			if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
   1905  1.110.2.2  mjf 				continue;
   1906  1.110.2.2  mjf 			n_s = ext_offset(p);
   1907  1.110.2.2  mjf 			if (n_s > start + dos_sectors)
   1908  1.110.2.2  mjf 				break;
   1909  1.110.2.2  mjf 			start = ext_offset(p)
   1910  1.110.2.2  mjf 				+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
   1911  1.110.2.2  mjf 				+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
   1912  1.110.2.2  mjf 		}
   1913  1.110.2.2  mjf 		if (ext.limit - start <= dos_sectors) {
   1914  1.110.2.2  mjf 			printf("No space in extended partition\n");
   1915  1.110.2.2  mjf 			return 0;
   1916  1.110.2.2  mjf 		}
   1917  1.110.2.2  mjf 		start += dos_sectors;
   1918  1.110.2.2  mjf 	}
   1919  1.110.2.2  mjf 
   1920  1.110.2.2  mjf 	if (!s_flag && sysid == 0 && !extended) {
   1921  1.110.2.2  mjf 		/* same for non-extended partition */
   1922  1.110.2.2  mjf 		/* first see if old start is free */
   1923  1.110.2.2  mjf 		if (start < dos_sectors)
   1924  1.110.2.2  mjf 			start = 0;
   1925  1.110.2.2  mjf 		for (p = 0; start != 0 && p < MBR_PART_COUNT; p++) {
   1926  1.110.2.2  mjf 			if (mboot.mbr_parts[p].mbrp_type == 0)
   1927  1.110.2.2  mjf 				continue;
   1928  1.110.2.2  mjf 			n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
   1929  1.110.2.2  mjf 			if (start >= n_s &&
   1930  1.110.2.2  mjf 			    start < n_s + le32toh(mboot.mbr_parts[p].mbrp_size))
   1931  1.110.2.2  mjf 				start = 0;
   1932  1.110.2.2  mjf 		}
   1933  1.110.2.2  mjf 		if (start == 0) {
   1934  1.110.2.2  mjf 			/* Look for first gap */
   1935  1.110.2.2  mjf 			start = dos_sectors;
   1936  1.110.2.2  mjf 			for (p = 0; p < MBR_PART_COUNT; p++) {
   1937  1.110.2.2  mjf 				if (mboot.mbr_parts[p].mbrp_type == 0)
   1938  1.110.2.2  mjf 					continue;
   1939  1.110.2.2  mjf 				n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
   1940  1.110.2.2  mjf 				n_e = n_s + le32toh(mboot.mbr_parts[p].mbrp_size);
   1941  1.110.2.2  mjf 				if (start >= n_s && start < n_e) {
   1942  1.110.2.2  mjf 					start = n_e;
   1943  1.110.2.2  mjf 					p = -1;
   1944  1.110.2.2  mjf 				}
   1945  1.110.2.2  mjf 			}
   1946  1.110.2.2  mjf 			if (start >= disksectors) {
   1947  1.110.2.2  mjf 				printf("No free space\n");
   1948  1.110.2.2  mjf 				return 0;
   1949  1.110.2.2  mjf 			}
   1950  1.110.2.2  mjf 		}
   1951  1.110.2.2  mjf 	}
   1952  1.110.2.2  mjf 
   1953  1.110.2.2  mjf 	if (!f_flag) {
   1954  1.110.2.2  mjf 		/* request new values from user */
   1955  1.110.2.2  mjf 		if (sysid == 0)
   1956  1.110.2.2  mjf 			sysid = 169;
   1957  1.110.2.2  mjf 		sysid = decimal("sysid", sysid, 0, 0, 255);
   1958  1.110.2.2  mjf 		if (sysid == 0 && !v_flag) {
   1959  1.110.2.2  mjf 			start = 0;
   1960  1.110.2.2  mjf 			size = 0;
   1961  1.110.2.2  mjf #ifdef BOOTSEL
   1962  1.110.2.2  mjf 			tmp_bootmenu[0] = 0;
   1963  1.110.2.2  mjf #endif
   1964  1.110.2.2  mjf 		} else {
   1965  1.110.2.2  mjf 			daddr_t old = start;
   1966  1.110.2.2  mjf 			daddr_t lim = extended ? ext.limit : disksectors;
   1967  1.110.2.2  mjf 			start = decimal("start", start,
   1968  1.110.2.2  mjf 				DEC_SEC | DEC_RND_0 | (extended ? DEC_RND : 0),
   1969  1.110.2.2  mjf 				extended ? ext.base : 0, lim);
   1970  1.110.2.2  mjf 			/* Adjust 'size' so that end doesn't move when 'start'
   1971  1.110.2.2  mjf 			 * is only changed slightly.
   1972  1.110.2.2  mjf 			 */
   1973  1.110.2.2  mjf 			if (size > start - old)
   1974  1.110.2.2  mjf 				size -= start - old;
   1975  1.110.2.2  mjf 			else
   1976  1.110.2.2  mjf 				size = 0;
   1977  1.110.2.2  mjf 			/* Find end of available space from this start point */
   1978  1.110.2.2  mjf 			if (extended) {
   1979  1.110.2.2  mjf 				for (p = 0; p < ext.num_ptn; p++) {
   1980  1.110.2.2  mjf 					if (p == part)
   1981  1.110.2.2  mjf 						continue;
   1982  1.110.2.2  mjf 					if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
   1983  1.110.2.2  mjf 						continue;
   1984  1.110.2.2  mjf 					n_s = ext_offset(p);
   1985  1.110.2.2  mjf 					if (n_s > start && n_s < lim)
   1986  1.110.2.2  mjf 						lim = n_s;
   1987  1.110.2.2  mjf 					if (start >= n_s && start < n_s
   1988  1.110.2.2  mjf 				+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
   1989  1.110.2.2  mjf 				+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_size)) {
   1990  1.110.2.2  mjf 						lim = start;
   1991  1.110.2.2  mjf 						break;
   1992  1.110.2.2  mjf 					}
   1993  1.110.2.2  mjf 				}
   1994  1.110.2.2  mjf 			} else {
   1995  1.110.2.2  mjf 				for (p = 0; p < MBR_PART_COUNT; p++) {
   1996  1.110.2.2  mjf 					if (p == part)
   1997  1.110.2.2  mjf 						continue;
   1998  1.110.2.2  mjf 					if (mboot.mbr_parts[p].mbrp_type == 0)
   1999  1.110.2.2  mjf 						continue;
   2000  1.110.2.2  mjf 					n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
   2001  1.110.2.2  mjf 					if (n_s > start && n_s < lim)
   2002  1.110.2.2  mjf 						lim = n_s;
   2003  1.110.2.2  mjf 					if (start >= n_s && start < n_s
   2004  1.110.2.2  mjf 				    + le32toh(mboot.mbr_parts[p].mbrp_size)) {
   2005  1.110.2.2  mjf 						lim = start;
   2006  1.110.2.2  mjf 						break;
   2007  1.110.2.2  mjf 					}
   2008  1.110.2.2  mjf 				}
   2009  1.110.2.2  mjf 			}
   2010  1.110.2.2  mjf 			lim -= start;
   2011  1.110.2.2  mjf 			if (lim == 0) {
   2012  1.110.2.2  mjf 				printf("Start sector already allocated\n");
   2013  1.110.2.2  mjf 				return 0;
   2014  1.110.2.2  mjf 			}
   2015  1.110.2.2  mjf 			if (size == 0 || size > lim)
   2016  1.110.2.2  mjf 				size = lim;
   2017  1.110.2.2  mjf 			fl = DEC_SEC;
   2018  1.110.2.2  mjf 			if (start % dos_cylindersectors == dos_sectors)
   2019  1.110.2.2  mjf 				fl |= DEC_RND_DOWN;
   2020  1.110.2.2  mjf 			if (start == 2 * dos_sectors)
   2021  1.110.2.2  mjf 				fl |= DEC_RND_DOWN | DEC_RND_DOWN_2;
   2022  1.110.2.2  mjf 			size = decimal("size", size, fl, 0, lim);
   2023  1.110.2.2  mjf #ifdef BOOTSEL
   2024  1.110.2.2  mjf #ifndef DEFAULT_BOOTEXTCODE
   2025  1.110.2.2  mjf 			if (!extended)
   2026  1.110.2.2  mjf #endif
   2027  1.110.2.2  mjf 				string("bootmenu", bootmenu_len, tmp_bootmenu);
   2028  1.110.2.2  mjf #endif
   2029  1.110.2.2  mjf 		}
   2030  1.110.2.2  mjf 	}
   2031  1.110.2.2  mjf 
   2032  1.110.2.2  mjf 	/*
   2033  1.110.2.2  mjf 	 * Before we write these away, we must verify that nothing
   2034  1.110.2.2  mjf 	 * untoward has been requested.
   2035  1.110.2.2  mjf 	 */
   2036  1.110.2.2  mjf 
   2037  1.110.2.2  mjf 	if (extended)
   2038  1.110.2.2  mjf 		errtext = check_ext_overlap(part, sysid, start, size, 0);
   2039  1.110.2.2  mjf 	else
   2040  1.110.2.2  mjf 		errtext = check_overlap(part, sysid, start, size, 0);
   2041  1.110.2.2  mjf 	if (errtext != NULL) {
   2042  1.110.2.2  mjf 		if (f_flag)
   2043  1.110.2.2  mjf 			errx(2, "%s\n", errtext);
   2044  1.110.2.2  mjf 		printf("%s\n", errtext);
   2045  1.110.2.2  mjf 		return 0;
   2046  1.110.2.2  mjf 	}
   2047  1.110.2.2  mjf 
   2048  1.110.2.2  mjf 	/*
   2049  1.110.2.2  mjf 	 * Before proceeding, delete any overlapped partitions.
   2050  1.110.2.2  mjf 	 * This can only happen if '-f' was supplied on the command line.
   2051  1.110.2.2  mjf 	 * Just hope the caller knows what they are doing.
   2052  1.110.2.2  mjf 	 * This also fixes the base of each extended partition if the
   2053  1.110.2.2  mjf 	 * partition itself has moved.
   2054  1.110.2.2  mjf 	 */
   2055  1.110.2.2  mjf 
   2056  1.110.2.2  mjf 	if (extended)
   2057  1.110.2.2  mjf 		errtext = check_ext_overlap(part, sysid, start, size, 1);
   2058  1.110.2.2  mjf 	else
   2059  1.110.2.2  mjf 		errtext = check_overlap(part, sysid, start, size, 1);
   2060  1.110.2.2  mjf 
   2061  1.110.2.2  mjf 	if (errtext)
   2062  1.110.2.2  mjf 		errx(1, "%s\n", errtext);
   2063  1.110.2.2  mjf 
   2064  1.110.2.2  mjf 	if (sysid == 0) {
   2065  1.110.2.2  mjf 		/* delete this partition - save info though */
   2066  1.110.2.2  mjf 		if (partp == NULL)
   2067  1.110.2.2  mjf 			/* must have been trying to create an extended ptn */
   2068  1.110.2.2  mjf 			return 0;
   2069  1.110.2.2  mjf 		if (start == 0 && size == 0)
   2070  1.110.2.2  mjf 			memset(partp, 0, sizeof *partp);
   2071  1.110.2.2  mjf #ifdef BOOTSEL
   2072  1.110.2.2  mjf 		if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC)
   2073  1.110.2.2  mjf 			memset(boot->mbr_bootsel.mbrbs_nametab[upart], 0,
   2074  1.110.2.2  mjf 				sizeof boot->mbr_bootsel.mbrbs_nametab[0]);
   2075  1.110.2.2  mjf #endif
   2076  1.110.2.2  mjf 		if (extended)
   2077  1.110.2.2  mjf 			delete_ext_ptn(part);
   2078  1.110.2.2  mjf 		else
   2079  1.110.2.2  mjf 			delete_ptn(part);
   2080  1.110.2.2  mjf 		return 1;
   2081  1.110.2.2  mjf 	}
   2082  1.110.2.2  mjf 
   2083  1.110.2.2  mjf 
   2084  1.110.2.2  mjf 	if (extended) {
   2085  1.110.2.2  mjf 		if (part != -1)
   2086  1.110.2.2  mjf 			delete_ext_ptn(part);
   2087  1.110.2.2  mjf 		if (start == ext.base + dos_sectors)
   2088  1.110.2.2  mjf 			/* First one must have been free */
   2089  1.110.2.2  mjf 			part = 0;
   2090  1.110.2.2  mjf 		else
   2091  1.110.2.2  mjf 			part = add_ext_ptn(start, size);
   2092  1.110.2.2  mjf 
   2093  1.110.2.2  mjf 		/* These must be re-calculated because of the realloc */
   2094  1.110.2.2  mjf 		boot = &ext.ptn[part];
   2095  1.110.2.2  mjf 		partp = &boot->mbr_parts[0];
   2096  1.110.2.2  mjf 		offset = ext_offset(part);
   2097  1.110.2.2  mjf 	}
   2098  1.110.2.2  mjf 
   2099  1.110.2.2  mjf 	partp->mbrp_type = sysid;
   2100  1.110.2.2  mjf 	partp->mbrp_start = htole32( start - offset);
   2101  1.110.2.2  mjf 	partp->mbrp_size = htole32( size);
   2102  1.110.2.2  mjf 	dos(start, &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
   2103  1.110.2.2  mjf 	dos(start + size - 1,
   2104  1.110.2.2  mjf 		    &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
   2105  1.110.2.2  mjf #ifdef BOOTSEL
   2106  1.110.2.2  mjf 	if (extended) {
   2107  1.110.2.2  mjf 		boot->mbr_bootsel_magic = LE_MBR_BS_MAGIC;
   2108  1.110.2.2  mjf 		strncpy(boot->mbr_bootsel.mbrbs_nametab[upart], tmp_bootmenu,
   2109  1.110.2.2  mjf 			bootmenu_len);
   2110  1.110.2.2  mjf 	} else {
   2111  1.110.2.2  mjf 		/* We need to bootselect code installed in order to have
   2112  1.110.2.2  mjf 		 * somewhere to safely write the menu tag.
   2113  1.110.2.2  mjf 		 */
   2114  1.110.2.2  mjf 		if (boot->mbr_bootsel_magic != LE_MBR_BS_MAGIC) {
   2115  1.110.2.2  mjf 			if (f_flag ||
   2116  1.110.2.2  mjf 			    yesno("The bootselect code is not installed, "
   2117  1.110.2.2  mjf 				"do you want to install it now?"))
   2118  1.110.2.2  mjf 				install_bootsel(MBR_BS_ACTIVE);
   2119  1.110.2.2  mjf 		}
   2120  1.110.2.2  mjf 		if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC) {
   2121  1.110.2.2  mjf 			strncpy(boot->mbr_bootsel.mbrbs_nametab[upart],
   2122  1.110.2.2  mjf 				tmp_bootmenu, bootmenu_len);
   2123  1.110.2.2  mjf 		}
   2124  1.110.2.2  mjf 	}
   2125  1.110.2.2  mjf #endif
   2126  1.110.2.2  mjf 
   2127  1.110.2.2  mjf 	if (v_flag && !f_flag && yesno("Explicitly specify beg/end address?")) {
   2128  1.110.2.2  mjf 		/* this really isn't a good idea.... */
   2129  1.110.2.2  mjf 		int tsector, tcylinder, thead;
   2130  1.110.2.2  mjf 
   2131  1.110.2.2  mjf 		tcylinder = MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect);
   2132  1.110.2.2  mjf 		thead = partp->mbrp_shd;
   2133  1.110.2.2  mjf 		tsector = MBR_PSECT(partp->mbrp_ssect);
   2134  1.110.2.2  mjf 		tcylinder = decimal("beginning cylinder",
   2135  1.110.2.2  mjf 				tcylinder, 0, 0, dos_cylinders - 1);
   2136  1.110.2.2  mjf 		thead = decimal("beginning head",
   2137  1.110.2.2  mjf 				thead, 0, 0, dos_heads - 1);
   2138  1.110.2.2  mjf 		tsector = decimal("beginning sector",
   2139  1.110.2.2  mjf 				tsector, 0, 1, dos_sectors);
   2140  1.110.2.2  mjf 		partp->mbrp_scyl = DOSCYL(tcylinder);
   2141  1.110.2.2  mjf 		partp->mbrp_shd = thead;
   2142  1.110.2.2  mjf 		partp->mbrp_ssect = DOSSECT(tsector, tcylinder);
   2143  1.110.2.2  mjf 
   2144  1.110.2.2  mjf 		tcylinder = MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect);
   2145  1.110.2.2  mjf 		thead = partp->mbrp_ehd;
   2146  1.110.2.2  mjf 		tsector = MBR_PSECT(partp->mbrp_esect);
   2147  1.110.2.2  mjf 		tcylinder = decimal("ending cylinder",
   2148  1.110.2.2  mjf 				tcylinder, 0, 0, dos_cylinders - 1);
   2149  1.110.2.2  mjf 		thead = decimal("ending head",
   2150  1.110.2.2  mjf 				thead, 0, 0, dos_heads - 1);
   2151  1.110.2.2  mjf 		tsector = decimal("ending sector",
   2152  1.110.2.2  mjf 				tsector, 0, 1, dos_sectors);
   2153  1.110.2.2  mjf 		partp->mbrp_ecyl = DOSCYL(tcylinder);
   2154  1.110.2.2  mjf 		partp->mbrp_ehd = thead;
   2155  1.110.2.2  mjf 		partp->mbrp_esect = DOSSECT(tsector, tcylinder);
   2156  1.110.2.2  mjf 	}
   2157  1.110.2.2  mjf 
   2158  1.110.2.2  mjf 	/* If we had to mark an extended partition as deleted because
   2159  1.110.2.2  mjf 	 * another request would have overlapped it, now is the time
   2160  1.110.2.2  mjf 	 * to do the actual delete.
   2161  1.110.2.2  mjf 	 */
   2162  1.110.2.2  mjf 	if (extended && f_flag) {
   2163  1.110.2.2  mjf 		for (p = ext.num_ptn; --p >= 0;)
   2164  1.110.2.2  mjf 			if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
   2165  1.110.2.2  mjf 				delete_ext_ptn(p);
   2166  1.110.2.2  mjf 	}
   2167  1.110.2.2  mjf 	return 1;
   2168  1.110.2.2  mjf }
   2169  1.110.2.2  mjf 
   2170  1.110.2.2  mjf void
   2171  1.110.2.2  mjf print_params(void)
   2172  1.110.2.2  mjf {
   2173  1.110.2.2  mjf 
   2174  1.110.2.2  mjf 	if (sh_flag) {
   2175  1.110.2.2  mjf 		printf("DISK=%s\n", disk);
   2176  1.110.2.2  mjf 		printf("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\nDLSIZE=%"PRIdaddr"\n",
   2177  1.110.2.2  mjf 			cylinders, heads, sectors, disksectors);
   2178  1.110.2.2  mjf 		printf("BCYL=%d\nBHEAD=%d\nBSEC=%d\nBDLSIZE=%"PRIdaddr"\n",
   2179  1.110.2.2  mjf 			dos_cylinders, dos_heads, dos_sectors, dos_disksectors);
   2180  1.110.2.2  mjf 		printf("NUMEXTPTN=%d\n", ext.num_ptn);
   2181  1.110.2.2  mjf 		return;
   2182  1.110.2.2  mjf 	}
   2183  1.110.2.2  mjf 
   2184  1.110.2.2  mjf 	/* Not sh_flag */
   2185  1.110.2.2  mjf 	printf("Disk: %s\n", disk);
   2186  1.110.2.2  mjf 	printf("NetBSD disklabel disk geometry:\n");
   2187  1.110.2.2  mjf 	printf("cylinders: %d, heads: %d, sectors/track: %d "
   2188  1.110.2.2  mjf 	    "(%d sectors/cylinder)\ntotal sectors: %"PRIdaddr"\n\n",
   2189  1.110.2.2  mjf 	    cylinders, heads, sectors, cylindersectors, disksectors);
   2190  1.110.2.2  mjf 	printf("BIOS disk geometry:\n");
   2191  1.110.2.2  mjf 	printf("cylinders: %d, heads: %d, sectors/track: %d "
   2192  1.110.2.2  mjf 	    "(%d sectors/cylinder)\ntotal sectors: %"PRIdaddr"\n\n",
   2193  1.110.2.2  mjf 	    dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors,
   2194  1.110.2.2  mjf 	    dos_disksectors);
   2195  1.110.2.2  mjf }
   2196  1.110.2.2  mjf 
   2197  1.110.2.2  mjf /* Find the first active partition, else return MBR_PART_COUNT */
   2198  1.110.2.2  mjf int
   2199  1.110.2.2  mjf first_active(void)
   2200  1.110.2.2  mjf {
   2201  1.110.2.2  mjf 	struct mbr_partition *partp = &mboot.mbr_parts[0];
   2202  1.110.2.2  mjf 	int part;
   2203  1.110.2.2  mjf 
   2204  1.110.2.2  mjf 	for (part = 0; part < MBR_PART_COUNT; part++)
   2205  1.110.2.2  mjf 		if (partp[part].mbrp_flag & MBR_PFLAG_ACTIVE)
   2206  1.110.2.2  mjf 			return part;
   2207  1.110.2.2  mjf 	return MBR_PART_COUNT;
   2208  1.110.2.2  mjf }
   2209  1.110.2.2  mjf 
   2210  1.110.2.2  mjf void
   2211  1.110.2.2  mjf change_active(int which)
   2212  1.110.2.2  mjf {
   2213  1.110.2.2  mjf 	struct mbr_partition *partp;
   2214  1.110.2.2  mjf 	int part;
   2215  1.110.2.2  mjf 	int active = MBR_PART_COUNT;
   2216  1.110.2.2  mjf 
   2217  1.110.2.2  mjf 	partp = &mboot.mbr_parts[0];
   2218  1.110.2.2  mjf 
   2219  1.110.2.2  mjf 	if (a_flag && which != -1)
   2220  1.110.2.2  mjf 		active = which;
   2221  1.110.2.2  mjf 	else
   2222  1.110.2.2  mjf 		active = first_active();
   2223  1.110.2.2  mjf 	if (!f_flag) {
   2224  1.110.2.2  mjf 		if (yesno("Do you want to change the active partition?")) {
   2225  1.110.2.2  mjf 			printf ("Choosing %d will make no partition active.\n",
   2226  1.110.2.2  mjf 			    MBR_PART_COUNT);
   2227  1.110.2.2  mjf 			do {
   2228  1.110.2.2  mjf 				active = decimal("active partition",
   2229  1.110.2.2  mjf 						active, 0, 0, MBR_PART_COUNT);
   2230  1.110.2.2  mjf 			} while (!yesno("Are you happy with this choice?"));
   2231  1.110.2.2  mjf 		} else
   2232  1.110.2.2  mjf 			return;
   2233  1.110.2.2  mjf 	} else
   2234  1.110.2.2  mjf 		if (active != MBR_PART_COUNT)
   2235  1.110.2.2  mjf 			printf ("Making partition %d active.\n", active);
   2236  1.110.2.2  mjf 
   2237  1.110.2.2  mjf 	for (part = 0; part < MBR_PART_COUNT; part++)
   2238  1.110.2.2  mjf 		partp[part].mbrp_flag &= ~MBR_PFLAG_ACTIVE;
   2239  1.110.2.2  mjf 	if (active < MBR_PART_COUNT)
   2240  1.110.2.2  mjf 		partp[active].mbrp_flag |= MBR_PFLAG_ACTIVE;
   2241  1.110.2.2  mjf }
   2242  1.110.2.2  mjf 
   2243  1.110.2.2  mjf void
   2244  1.110.2.2  mjf get_params_to_use(void)
   2245  1.110.2.2  mjf {
   2246  1.110.2.2  mjf #if defined(__i386__) || defined(__x86_64__)
   2247  1.110.2.2  mjf 	struct biosdisk_info *bip;
   2248  1.110.2.2  mjf 	int i;
   2249  1.110.2.2  mjf #endif
   2250  1.110.2.2  mjf 
   2251  1.110.2.2  mjf 	if (b_flag) {
   2252  1.110.2.2  mjf 		dos_cylinders = b_cyl;
   2253  1.110.2.2  mjf 		dos_heads = b_head;
   2254  1.110.2.2  mjf 		dos_sectors = b_sec;
   2255  1.110.2.2  mjf 		return;
   2256  1.110.2.2  mjf 	}
   2257  1.110.2.2  mjf 
   2258  1.110.2.2  mjf 	print_params();
   2259  1.110.2.2  mjf 	if (!yesno("Do you want to change our idea of what BIOS thinks?"))
   2260  1.110.2.2  mjf 		return;
   2261  1.110.2.2  mjf 
   2262  1.110.2.2  mjf #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
   2263  1.110.2.2  mjf 	if (dl != NULL) {
   2264  1.110.2.2  mjf 		for (i = 0; i < dl->dl_nbiosdisks; i++) {
   2265  1.110.2.2  mjf 			if (i == 0)
   2266  1.110.2.2  mjf 				printf("\nGeometries of known disks:\n");
   2267  1.110.2.2  mjf 			bip = &dl->dl_biosdisks[i];
   2268  1.110.2.2  mjf 			printf("Disk %d: cylinders %u, heads %u, sectors %u"
   2269  1.110.2.2  mjf 				" (%"PRIdaddr" sectors, %dMB)\n",
   2270  1.110.2.2  mjf 			    i, bip->bi_cyl, bip->bi_head, bip->bi_sec,
   2271  1.110.2.2  mjf 			    bip->bi_lbasecs, SEC_TO_MB(bip->bi_lbasecs));
   2272  1.110.2.2  mjf 
   2273  1.110.2.2  mjf 		}
   2274  1.110.2.2  mjf 		printf("\n");
   2275  1.110.2.2  mjf 	}
   2276  1.110.2.2  mjf #endif
   2277  1.110.2.2  mjf 	do {
   2278  1.110.2.2  mjf 		dos_cylinders = decimal("BIOS's idea of #cylinders",
   2279  1.110.2.2  mjf 					dos_cylinders, 0, 0, MAXCYL);
   2280  1.110.2.2  mjf 		dos_heads = decimal("BIOS's idea of #heads",
   2281  1.110.2.2  mjf 					dos_heads, 0, 0, MAXHEAD);
   2282  1.110.2.2  mjf 		dos_sectors = decimal("BIOS's idea of #sectors",
   2283  1.110.2.2  mjf 					dos_sectors, 0, 1, MAXSECTOR);
   2284  1.110.2.2  mjf 		print_params();
   2285  1.110.2.2  mjf 	} while (!yesno("Are you happy with this choice?"));
   2286  1.110.2.2  mjf }
   2287  1.110.2.2  mjf 
   2288  1.110.2.2  mjf 
   2289  1.110.2.2  mjf /***********************************************\
   2290  1.110.2.2  mjf * Change real numbers into strange dos numbers	*
   2291  1.110.2.2  mjf \***********************************************/
   2292  1.110.2.2  mjf void
   2293  1.110.2.2  mjf dos(int sector, unsigned char *cylinderp, unsigned char *headp,
   2294  1.110.2.2  mjf     unsigned char *sectorp)
   2295  1.110.2.2  mjf {
   2296  1.110.2.2  mjf 	int cylinder, head;
   2297  1.110.2.2  mjf 
   2298  1.110.2.2  mjf 	cylinder = sector / dos_cylindersectors;
   2299  1.110.2.2  mjf 	sector -= cylinder * dos_cylindersectors;
   2300  1.110.2.2  mjf 
   2301  1.110.2.2  mjf 	head = sector / dos_sectors;
   2302  1.110.2.2  mjf 	sector -= head * dos_sectors;
   2303  1.110.2.2  mjf 	if (cylinder > 1023)
   2304  1.110.2.2  mjf 		cylinder = 1023;
   2305  1.110.2.2  mjf 
   2306  1.110.2.2  mjf 	*cylinderp = DOSCYL(cylinder);
   2307  1.110.2.2  mjf 	*headp = head;
   2308  1.110.2.2  mjf 	*sectorp = DOSSECT(sector + 1, cylinder);
   2309  1.110.2.2  mjf }
   2310  1.110.2.2  mjf 
   2311  1.110.2.2  mjf int
   2312  1.110.2.2  mjf open_disk(int update)
   2313  1.110.2.2  mjf {
   2314  1.110.2.2  mjf 	static char namebuf[MAXPATHLEN + 1];
   2315  1.110.2.2  mjf 	int flags = update && disk_file == NULL ? O_RDWR : O_RDONLY;
   2316  1.110.2.2  mjf 
   2317  1.110.2.2  mjf 	if (!F_flag) {
   2318  1.110.2.2  mjf 		fd = opendisk(disk, flags, namebuf, sizeof(namebuf), 0);
   2319  1.110.2.2  mjf 		if (fd < 0) {
   2320  1.110.2.2  mjf 			if (errno == ENODEV)
   2321  1.110.2.2  mjf 				warnx("%s is not a character device", namebuf);
   2322  1.110.2.2  mjf 			else
   2323  1.110.2.2  mjf 				warn("cannot opendisk %s", namebuf);
   2324  1.110.2.2  mjf 			return (-1);
   2325  1.110.2.2  mjf 		}
   2326  1.110.2.2  mjf 		disk = namebuf;
   2327  1.110.2.2  mjf 	} else {
   2328  1.110.2.2  mjf 		fd = open(disk, flags, 0);
   2329  1.110.2.2  mjf 		if (fd == -1) {
   2330  1.110.2.2  mjf 			warn("cannot open %s", disk);
   2331  1.110.2.2  mjf 			return -1;
   2332  1.110.2.2  mjf 		}
   2333  1.110.2.2  mjf 	}
   2334  1.110.2.2  mjf 
   2335  1.110.2.2  mjf 	if (get_params() == -1) {
   2336  1.110.2.2  mjf 		close(fd);
   2337  1.110.2.2  mjf 		fd = -1;
   2338  1.110.2.2  mjf 		return (-1);
   2339  1.110.2.2  mjf 	}
   2340  1.110.2.2  mjf 	if (disk_file != NULL) {
   2341  1.110.2.2  mjf 		/* for testing: read/write data from a disk file */
   2342  1.110.2.2  mjf 		wfd = open(disk_file, update ? O_RDWR|O_CREAT : O_RDONLY, 0777);
   2343  1.110.2.2  mjf 		if (wfd == -1) {
   2344  1.110.2.2  mjf 			warn("%s", disk_file);
   2345  1.110.2.2  mjf 			close(fd);
   2346  1.110.2.2  mjf 			fd = -1;
   2347  1.110.2.2  mjf 			return -1;
   2348  1.110.2.2  mjf 		}
   2349  1.110.2.2  mjf 	} else
   2350  1.110.2.2  mjf 		wfd = fd;
   2351  1.110.2.2  mjf 	return (0);
   2352  1.110.2.2  mjf }
   2353  1.110.2.2  mjf 
   2354  1.110.2.2  mjf int
   2355  1.110.2.2  mjf read_disk(daddr_t sector, void *buf)
   2356  1.110.2.2  mjf {
   2357  1.110.2.2  mjf 
   2358  1.110.2.2  mjf 	if (*rfd == -1)
   2359  1.110.2.2  mjf 		errx(1, "read_disk(); fd == -1");
   2360  1.110.2.2  mjf 	if (lseek(*rfd, sector * (off_t)512, 0) == -1)
   2361  1.110.2.2  mjf 		return (-1);
   2362  1.110.2.2  mjf 	return (read(*rfd, buf, 512));
   2363  1.110.2.2  mjf }
   2364  1.110.2.2  mjf 
   2365  1.110.2.2  mjf int
   2366  1.110.2.2  mjf write_disk(daddr_t sector, void *buf)
   2367  1.110.2.2  mjf {
   2368  1.110.2.2  mjf 
   2369  1.110.2.2  mjf 	if (wfd == -1)
   2370  1.110.2.2  mjf 		errx(1, "write_disk(); wfd == -1");
   2371  1.110.2.2  mjf 	if (lseek(wfd, sector * (off_t)512, 0) == -1)
   2372  1.110.2.2  mjf 		return (-1);
   2373  1.110.2.2  mjf 	return (write(wfd, buf, 512));
   2374  1.110.2.2  mjf }
   2375  1.110.2.2  mjf 
   2376  1.110.2.2  mjf static void
   2377  1.110.2.2  mjf guess_geometry(daddr_t _sectors)
   2378  1.110.2.2  mjf {
   2379  1.110.2.2  mjf 	dos_sectors = MAXSECTOR;
   2380  1.110.2.2  mjf 	dos_heads = MAXHEAD - 1;	/* some BIOS might use 256 */
   2381  1.110.2.2  mjf 	dos_cylinders = _sectors / (MAXSECTOR * (MAXHEAD - 1));
   2382  1.110.2.2  mjf 	if (dos_cylinders < 1)
   2383  1.110.2.2  mjf 		dos_cylinders = 1;
   2384  1.110.2.2  mjf 	else if (dos_cylinders > MAXCYL - 1)
   2385  1.110.2.2  mjf 		dos_cylinders = MAXCYL - 1;
   2386  1.110.2.2  mjf }
   2387  1.110.2.2  mjf 
   2388  1.110.2.2  mjf int
   2389  1.110.2.2  mjf get_params(void)
   2390  1.110.2.2  mjf {
   2391  1.110.2.2  mjf 	if (disk_type != NULL) {
   2392  1.110.2.2  mjf 		struct disklabel *tmplabel;
   2393  1.110.2.2  mjf 
   2394  1.110.2.2  mjf 		if ((tmplabel = getdiskbyname(disk_type)) == NULL) {
   2395  1.110.2.2  mjf 			warn("bad disktype");
   2396  1.110.2.2  mjf 			return (-1);
   2397  1.110.2.2  mjf 		}
   2398  1.110.2.2  mjf 		disklabel = *tmplabel;
   2399  1.110.2.2  mjf 	} else if (F_flag) {
   2400  1.110.2.2  mjf 		struct stat st;
   2401  1.110.2.2  mjf 		if (fstat(fd, &st) == -1) {
   2402  1.110.2.2  mjf 			warn("fstat");
   2403  1.110.2.2  mjf 			return (-1);
   2404  1.110.2.2  mjf 		}
   2405  1.110.2.2  mjf 		if (st.st_size % 512 != 0) {
   2406  1.110.2.2  mjf 			warnx("%s size (%lld) is not divisible "
   2407  1.110.2.2  mjf 			    "by sector size (%d)", disk, (long long)st.st_size,
   2408  1.110.2.2  mjf 			    512);
   2409  1.110.2.2  mjf 		}
   2410  1.110.2.2  mjf 		disklabel.d_secperunit = st.st_size / 512;
   2411  1.110.2.2  mjf 		guess_geometry(disklabel.d_secperunit);
   2412  1.110.2.2  mjf 		disklabel.d_ncylinders = dos_cylinders;
   2413  1.110.2.2  mjf 		disklabel.d_ntracks = dos_heads;
   2414  1.110.2.2  mjf 		disklabel.d_nsectors = dos_sectors;
   2415  1.110.2.2  mjf 	} else if (ioctl(fd, DIOCGDEFLABEL, &disklabel) == -1) {
   2416  1.110.2.2  mjf 		warn("DIOCGDEFLABEL");
   2417  1.110.2.2  mjf 		if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
   2418  1.110.2.2  mjf 			warn("DIOCGDINFO");
   2419  1.110.2.2  mjf 			return (-1);
   2420  1.110.2.2  mjf 		}
   2421  1.110.2.2  mjf 	}
   2422  1.110.2.2  mjf 
   2423  1.110.2.2  mjf 	disksectors = disklabel.d_secperunit;
   2424  1.110.2.2  mjf 	cylinders = disklabel.d_ncylinders;
   2425  1.110.2.2  mjf 	heads = disklabel.d_ntracks;
   2426  1.110.2.2  mjf 	sectors = disklabel.d_nsectors;
   2427  1.110.2.2  mjf 
   2428  1.110.2.2  mjf 	/* pick up some defaults for the BIOS sizes */
   2429  1.110.2.2  mjf 	if (sectors <= MAXSECTOR) {
   2430  1.110.2.2  mjf 		dos_cylinders = cylinders;
   2431  1.110.2.2  mjf 		dos_heads = heads;
   2432  1.110.2.2  mjf 		dos_sectors = sectors;
   2433  1.110.2.2  mjf 	} else {
   2434  1.110.2.2  mjf 		/* guess - has to better than the above */
   2435  1.110.2.2  mjf 		guess_geometry(disksectors);
   2436  1.110.2.2  mjf 	}
   2437  1.110.2.2  mjf 	dos_disksectors = disksectors;
   2438  1.110.2.2  mjf 
   2439  1.110.2.2  mjf 	return (0);
   2440  1.110.2.2  mjf }
   2441  1.110.2.2  mjf 
   2442  1.110.2.2  mjf #ifdef BOOTSEL
   2443  1.110.2.2  mjf /*
   2444  1.110.2.2  mjf  * Rather unfortunately the bootsel 'magic' number is at the end of the
   2445  1.110.2.2  mjf  * the structure, and there is no checksum.  So when other operating
   2446  1.110.2.2  mjf  * systems install mbr code by only writing the length of their code they
   2447  1.110.2.2  mjf  * can overwrite part of the structure but keeping the magic number intact.
   2448  1.110.2.2  mjf  * This code attempts to empirically detect this problem.
   2449  1.110.2.2  mjf  */
   2450  1.110.2.2  mjf static int
   2451  1.110.2.2  mjf validate_bootsel(struct mbr_bootsel *mbs)
   2452  1.110.2.2  mjf {
   2453  1.110.2.2  mjf 	unsigned int key = mbs->mbrbs_defkey;
   2454  1.110.2.2  mjf 	unsigned int tmo;
   2455  1.110.2.2  mjf 	int i;
   2456  1.110.2.2  mjf 
   2457  1.110.2.2  mjf 	if (v_flag)
   2458  1.110.2.2  mjf 		return 0;
   2459  1.110.2.2  mjf 
   2460  1.110.2.2  mjf 	/*
   2461  1.110.2.2  mjf 	 * Check default key is sane
   2462  1.110.2.2  mjf 	 * - this is the most likely field to be stuffed
   2463  1.110.2.3  mjf 	 * 16 disks and 16 bootable partitions seems enough!
   2464  1.110.2.2  mjf 	 * (the keymap decode starts falling apart at that point)
   2465  1.110.2.2  mjf 	 */
   2466  1.110.2.3  mjf 	if (mbs->mbrbs_flags & MBR_BS_ASCII) {
   2467  1.110.2.3  mjf 		if (key != 0 && !(key == '\r'
   2468  1.110.2.3  mjf 		    || (key >= '1' && key < '1' + MAX_BIOS_DISKS)
   2469  1.110.2.3  mjf 		    || (key >= 'a' && key < 'a' + MAX_BIOS_DISKS)))
   2470  1.110.2.3  mjf 			return 1;
   2471  1.110.2.3  mjf 	} else {
   2472  1.110.2.3  mjf 		if (key != 0 && !(key == SCAN_ENTER
   2473  1.110.2.3  mjf 		    || (key >= SCAN_1 && key < SCAN_1 + MAX_BIOS_DISKS)
   2474  1.110.2.3  mjf 		    || (key >= SCAN_F1 && key < SCAN_F1 + MAX_BIOS_DISKS)))
   2475  1.110.2.3  mjf 			return 1;
   2476  1.110.2.3  mjf 	}
   2477  1.110.2.2  mjf 
   2478  1.110.2.2  mjf 	/* Checking the flags will lead to breakage... */
   2479  1.110.2.2  mjf 
   2480  1.110.2.3  mjf 	/* Timeout value is expected to be a multiple of a second */
   2481  1.110.2.2  mjf 	tmo = htole16(mbs->mbrbs_timeo);
   2482  1.110.2.2  mjf 	if (tmo != 0 && tmo != 0xffff && tmo != (10 * tmo + 9) / 182 * 182 / 10)
   2483  1.110.2.2  mjf 		return 2;
   2484  1.110.2.2  mjf 
   2485  1.110.2.2  mjf 	/* Check the menu strings are printable */
   2486  1.110.2.2  mjf 	/* Unfortunately they aren't zero filled... */
   2487  1.110.2.2  mjf 	for (i = 0; i < sizeof(mbs->mbrbs_nametab); i++) {
   2488  1.110.2.2  mjf 		int c = (uint8_t)mbs->mbrbs_nametab[0][i];
   2489  1.110.2.2  mjf 		if (c == 0 || isprint(c))
   2490  1.110.2.2  mjf 			continue;
   2491  1.110.2.2  mjf 		return 3;
   2492  1.110.2.2  mjf 	}
   2493  1.110.2.2  mjf 
   2494  1.110.2.2  mjf 	return 0;
   2495  1.110.2.2  mjf }
   2496  1.110.2.2  mjf #endif
   2497  1.110.2.2  mjf 
   2498  1.110.2.2  mjf int
   2499  1.110.2.2  mjf read_s0(daddr_t offset, struct mbr_sector *boot)
   2500  1.110.2.2  mjf {
   2501  1.110.2.2  mjf 	const char *tabletype = offset ? "extended" : "primary";
   2502  1.110.2.2  mjf #ifdef BOOTSEL
   2503  1.110.2.2  mjf 	static int reported;
   2504  1.110.2.2  mjf #endif
   2505  1.110.2.2  mjf 
   2506  1.110.2.2  mjf 	if (read_disk(offset, boot) == -1) {
   2507  1.110.2.2  mjf 		warn("Can't read %s partition table", tabletype);
   2508  1.110.2.2  mjf 		return -1;
   2509  1.110.2.2  mjf 	}
   2510  1.110.2.2  mjf 	if (boot->mbr_magic != LE_MBR_MAGIC) {
   2511  1.110.2.2  mjf 		warnx("%s partition table invalid, "
   2512  1.110.2.2  mjf 		    "no magic in sector %"PRIdaddr, tabletype, offset);
   2513  1.110.2.2  mjf 		return -1;
   2514  1.110.2.2  mjf 
   2515  1.110.2.2  mjf 	}
   2516  1.110.2.2  mjf #ifdef BOOTSEL
   2517  1.110.2.2  mjf 	if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC) {
   2518  1.110.2.2  mjf 		/* mbr_bootsel in new location */
   2519  1.110.2.2  mjf 		if (validate_bootsel(&boot->mbr_bootsel)) {
   2520  1.110.2.2  mjf 			warnx("removing corrupt bootsel information");
   2521  1.110.2.2  mjf 			boot->mbr_bootsel_magic = 0;
   2522  1.110.2.2  mjf 		}
   2523  1.110.2.2  mjf 		return 0;
   2524  1.110.2.2  mjf 	}
   2525  1.110.2.2  mjf 	if (boot->mbr_bootsel_magic != LE_MBR_MAGIC)
   2526  1.110.2.2  mjf 		return 0;
   2527  1.110.2.2  mjf 
   2528  1.110.2.2  mjf 	/* mbr_bootsel in old location */
   2529  1.110.2.2  mjf 	if (!reported)
   2530  1.110.2.2  mjf 		warnx("%s partition table: using old-style bootsel information",
   2531  1.110.2.2  mjf 		    tabletype);
   2532  1.110.2.2  mjf 	reported = 1;
   2533  1.110.2.2  mjf 	if (validate_bootsel((void *)((uint8_t *)boot + MBR_BS_OFFSET + 4))) {
   2534  1.110.2.2  mjf 		warnx("%s bootsel information corrupt - ignoring", tabletype);
   2535  1.110.2.2  mjf 		return 0;
   2536  1.110.2.2  mjf 	}
   2537  1.110.2.2  mjf 	memmove((uint8_t *)boot + MBR_BS_OFFSET,
   2538  1.110.2.2  mjf 		(uint8_t *)boot + MBR_BS_OFFSET + 4,
   2539  1.110.2.2  mjf 		sizeof(struct mbr_bootsel));
   2540  1.110.2.2  mjf 	if ( ! (boot->mbr_bootsel.mbrbs_flags & MBR_BS_NEWMBR)) {
   2541  1.110.2.2  mjf 			/* old style default key */
   2542  1.110.2.2  mjf 		int id;
   2543  1.110.2.2  mjf 			/* F1..F4 => ptn 0..3, F5+ => disk 0+ */
   2544  1.110.2.2  mjf 		id = boot->mbr_bootsel.mbrbs_defkey;
   2545  1.110.2.2  mjf 		id -= SCAN_F1;
   2546  1.110.2.2  mjf 		if (id >= MBR_PART_COUNT)
   2547  1.110.2.2  mjf 			id -= MBR_PART_COUNT; /* Use number of disk */
   2548  1.110.2.2  mjf 		else if (mboot.mbr_parts[id].mbrp_type != 0)
   2549  1.110.2.2  mjf 			id = le32toh(boot->mbr_parts[id].mbrp_start);
   2550  1.110.2.2  mjf 		else
   2551  1.110.2.2  mjf 			id = DEFAULT_ACTIVE;
   2552  1.110.2.2  mjf 		boot->mbr_bootsel.mbrbs_defkey = id;
   2553  1.110.2.2  mjf 	}
   2554  1.110.2.2  mjf 	boot->mbr_bootsel_magic = LE_MBR_BS_MAGIC;
   2555  1.110.2.2  mjf 		/* highlight that new bootsel code is necessary */
   2556  1.110.2.2  mjf 	boot->mbr_bootsel.mbrbs_flags &= ~MBR_BS_NEWMBR;
   2557  1.110.2.2  mjf #endif /* BOOTSEL */
   2558  1.110.2.2  mjf 	return 0;
   2559  1.110.2.2  mjf }
   2560  1.110.2.2  mjf 
   2561  1.110.2.2  mjf int
   2562  1.110.2.2  mjf write_mbr(void)
   2563  1.110.2.2  mjf {
   2564  1.110.2.2  mjf 	int flag, i;
   2565  1.110.2.2  mjf 	daddr_t offset;
   2566  1.110.2.2  mjf 	int rval = -1;
   2567  1.110.2.2  mjf 
   2568  1.110.2.2  mjf 	/*
   2569  1.110.2.2  mjf 	 * write enable label sector before write (if necessary),
   2570  1.110.2.2  mjf 	 * disable after writing.
   2571  1.110.2.2  mjf 	 * needed if the disklabel protected area also protects
   2572  1.110.2.2  mjf 	 * sector 0. (e.g. empty disk)
   2573  1.110.2.2  mjf 	 */
   2574  1.110.2.2  mjf 	flag = 1;
   2575  1.110.2.2  mjf 	if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0)
   2576  1.110.2.2  mjf 		warn("DIOCWLABEL");
   2577  1.110.2.2  mjf 	if (write_disk(0, &mboot) == -1) {
   2578  1.110.2.2  mjf 		warn("Can't write fdisk partition table");
   2579  1.110.2.2  mjf 		goto protect_label;
   2580  1.110.2.2  mjf 	}
   2581  1.110.2.2  mjf 	if (boot_installed)
   2582  1.110.2.2  mjf 		for (i = bootsize; (i -= 0x200) > 0;)
   2583  1.110.2.2  mjf 			if (write_disk(i / 0x200, &bootcode[i / 0x200]) == -1) {
   2584  1.110.2.2  mjf 				warn("Can't write bootcode");
   2585  1.110.2.2  mjf 				goto protect_label;
   2586  1.110.2.2  mjf 			}
   2587  1.110.2.2  mjf 	for (offset = 0, i = 0; i < ext.num_ptn; i++) {
   2588  1.110.2.2  mjf 		if (write_disk(ext.base + offset, ext.ptn + i) == -1) {
   2589  1.110.2.2  mjf 			warn("Can't write %dth extended partition", i);
   2590  1.110.2.2  mjf 			goto protect_label;
   2591  1.110.2.2  mjf 		}
   2592  1.110.2.2  mjf 		offset = le32toh(ext.ptn[i].mbr_parts[1].mbrp_start);
   2593  1.110.2.2  mjf 	}
   2594  1.110.2.2  mjf 	rval = 0;
   2595  1.110.2.2  mjf     protect_label:
   2596  1.110.2.2  mjf 	flag = 0;
   2597  1.110.2.2  mjf 	if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0)
   2598  1.110.2.2  mjf 		warn("DIOCWLABEL");
   2599  1.110.2.2  mjf 	return rval;
   2600  1.110.2.2  mjf }
   2601  1.110.2.2  mjf 
   2602  1.110.2.2  mjf int
   2603  1.110.2.2  mjf yesno(const char *str, ...)
   2604  1.110.2.2  mjf {
   2605  1.110.2.2  mjf 	int ch, first;
   2606  1.110.2.2  mjf 	va_list ap;
   2607  1.110.2.2  mjf 
   2608  1.110.2.2  mjf 	va_start(ap, str);
   2609  1.110.2.2  mjf 
   2610  1.110.2.2  mjf 	vprintf(str, ap);
   2611  1.110.2.2  mjf 	printf(" [n] ");
   2612  1.110.2.2  mjf 
   2613  1.110.2.2  mjf 	first = ch = getchar();
   2614  1.110.2.2  mjf 	while (ch != '\n' && ch != EOF)
   2615  1.110.2.2  mjf 		ch = getchar();
   2616  1.110.2.2  mjf 	if (ch == EOF)
   2617  1.110.2.2  mjf 		errx(1, "EOF");
   2618  1.110.2.2  mjf 	return (first == 'y' || first == 'Y');
   2619  1.110.2.2  mjf }
   2620  1.110.2.2  mjf 
   2621  1.110.2.2  mjf int
   2622  1.110.2.2  mjf decimal(const char *prompt, int dflt, int flags, int minval, int maxval)
   2623  1.110.2.2  mjf {
   2624  1.110.2.2  mjf 	int acc = 0;
   2625  1.110.2.2  mjf 	char *cp;
   2626  1.110.2.2  mjf 	char ch;
   2627  1.110.2.2  mjf 
   2628  1.110.2.2  mjf 	for (;;) {
   2629  1.110.2.2  mjf 		if (flags & DEC_SEC) {
   2630  1.110.2.2  mjf 			printf("%s: [%d..%dcyl default: %d, %dcyl, %uMB] ",
   2631  1.110.2.2  mjf 			    prompt, SEC_TO_CYL(minval), SEC_TO_CYL(maxval),
   2632  1.110.2.2  mjf 			    dflt, SEC_TO_CYL(dflt), SEC_TO_MB(dflt));
   2633  1.110.2.2  mjf 		} else
   2634  1.110.2.2  mjf 			printf("%s: [%d..%d default: %d] ",
   2635  1.110.2.2  mjf 			    prompt, minval, maxval, dflt);
   2636  1.110.2.2  mjf 
   2637  1.110.2.2  mjf 		if (!fgets(lbuf, LBUF, stdin))
   2638  1.110.2.2  mjf 			errx(1, "EOF");
   2639  1.110.2.2  mjf 		lbuf[strlen(lbuf)-1] = '\0';
   2640  1.110.2.2  mjf 		cp = lbuf;
   2641  1.110.2.2  mjf 
   2642  1.110.2.2  mjf 		cp += strspn(cp, " \t");
   2643  1.110.2.2  mjf 		if (*cp == '\0')
   2644  1.110.2.2  mjf 			return dflt;
   2645  1.110.2.2  mjf 
   2646  1.110.2.2  mjf 		if (cp[0] == '$' && cp[1] == 0)
   2647  1.110.2.2  mjf 			return maxval;
   2648  1.110.2.2  mjf 
   2649  1.110.2.2  mjf 		if (isdigit((unsigned char)*cp) || *cp == '-') {
   2650  1.110.2.2  mjf 			acc = strtol(lbuf, &cp, 10);
   2651  1.110.2.2  mjf 			if (flags & DEC_SEC) {
   2652  1.110.2.2  mjf 				ch = *cp;
   2653  1.110.2.2  mjf 				if (ch == 'g' || ch == 'G') {
   2654  1.110.2.2  mjf 					acc *= 1024;
   2655  1.110.2.2  mjf 					ch = 'm';
   2656  1.110.2.2  mjf 				}
   2657  1.110.2.2  mjf 				if (ch == 'm' || ch == 'M') {
   2658  1.110.2.2  mjf 					acc *= SEC_IN_1M;
   2659  1.110.2.2  mjf 					/* round to whole number of cylinders */
   2660  1.110.2.2  mjf 					acc += dos_cylindersectors / 2;
   2661  1.110.2.2  mjf 					acc /= dos_cylindersectors;
   2662  1.110.2.2  mjf 					ch = 'c';
   2663  1.110.2.2  mjf 				}
   2664  1.110.2.2  mjf 				if (ch == 'c' || ch == 'C') {
   2665  1.110.2.2  mjf 					cp++;
   2666  1.110.2.2  mjf 					acc *= dos_cylindersectors;
   2667  1.110.2.2  mjf 					/* adjustments for cylinder boundary */
   2668  1.110.2.2  mjf 					if (acc == 0 && flags & DEC_RND_0)
   2669  1.110.2.2  mjf 						acc += dos_sectors;
   2670  1.110.2.2  mjf 					if (flags & DEC_RND)
   2671  1.110.2.2  mjf 						acc += dos_sectors;
   2672  1.110.2.2  mjf 					if (flags & DEC_RND_DOWN)
   2673  1.110.2.2  mjf 						acc -= dos_sectors;
   2674  1.110.2.2  mjf 					if (flags & DEC_RND_DOWN_2)
   2675  1.110.2.2  mjf 						acc -= dos_sectors;
   2676  1.110.2.2  mjf 				}
   2677  1.110.2.2  mjf 			}
   2678  1.110.2.2  mjf 		}
   2679  1.110.2.2  mjf 
   2680  1.110.2.2  mjf 		cp += strspn(cp, " \t");
   2681  1.110.2.2  mjf 		if (*cp != '\0') {
   2682  1.110.2.2  mjf 			printf("%s is not a valid %s number.\n", lbuf,
   2683  1.110.2.2  mjf 			    flags & DEC_SEC ? "sector" : "decimal");
   2684  1.110.2.2  mjf 			continue;
   2685  1.110.2.2  mjf 		}
   2686  1.110.2.2  mjf 
   2687  1.110.2.2  mjf 		if (acc >= minval && acc <= maxval)
   2688  1.110.2.2  mjf 			return acc;
   2689  1.110.2.2  mjf 		printf("%d is not between %d and %d.\n", acc, minval, maxval);
   2690  1.110.2.2  mjf 	}
   2691  1.110.2.2  mjf }
   2692  1.110.2.2  mjf 
   2693  1.110.2.2  mjf int
   2694  1.110.2.2  mjf ptn_id(const char *prompt, int *extended)
   2695  1.110.2.2  mjf {
   2696  1.110.2.2  mjf 	unsigned int acc = 0;
   2697  1.110.2.2  mjf 	char *cp;
   2698  1.110.2.2  mjf 
   2699  1.110.2.2  mjf 	for (;; printf("%s is not a valid partition number.\n", lbuf)) {
   2700  1.110.2.2  mjf 		printf("%s: [none] ", prompt);
   2701  1.110.2.2  mjf 
   2702  1.110.2.2  mjf 		if (!fgets(lbuf, LBUF, stdin))
   2703  1.110.2.2  mjf 			errx(1, "EOF");
   2704  1.110.2.2  mjf 		lbuf[strlen(lbuf)-1] = '\0';
   2705  1.110.2.2  mjf 		cp = lbuf;
   2706  1.110.2.2  mjf 
   2707  1.110.2.2  mjf 		cp += strspn(cp, " \t");
   2708  1.110.2.2  mjf 		*extended = 0;
   2709  1.110.2.2  mjf 		if (*cp == 0)
   2710  1.110.2.2  mjf 			return -1;
   2711  1.110.2.2  mjf 
   2712  1.110.2.2  mjf 		if (*cp == 'E' || *cp == 'e') {
   2713  1.110.2.2  mjf 			cp++;
   2714  1.110.2.2  mjf 			*extended = 1;
   2715  1.110.2.2  mjf 		}
   2716  1.110.2.2  mjf 
   2717  1.110.2.2  mjf 		acc = strtoul(cp, &cp, 10);
   2718  1.110.2.2  mjf 
   2719  1.110.2.2  mjf 		cp += strspn(cp, " \t");
   2720  1.110.2.2  mjf 		if (*cp != '\0')
   2721  1.110.2.2  mjf 			continue;
   2722  1.110.2.2  mjf 
   2723  1.110.2.2  mjf 		if (*extended || acc < MBR_PART_COUNT)
   2724  1.110.2.2  mjf 			return acc;
   2725  1.110.2.2  mjf 	}
   2726  1.110.2.2  mjf }
   2727  1.110.2.2  mjf 
   2728  1.110.2.2  mjf #ifdef BOOTSEL
   2729  1.110.2.2  mjf void
   2730  1.110.2.2  mjf string(const char *prompt, int length, char *buf)
   2731  1.110.2.2  mjf {
   2732  1.110.2.2  mjf 	int len;
   2733  1.110.2.2  mjf 
   2734  1.110.2.2  mjf 	for (;;) {
   2735  1.110.2.2  mjf 		printf("%s: [%.*s] ", prompt, length, buf);
   2736  1.110.2.2  mjf 
   2737  1.110.2.2  mjf 		if (!fgets(lbuf, LBUF, stdin))
   2738  1.110.2.2  mjf 			errx(1, "EOF");
   2739  1.110.2.2  mjf 		len = strlen(lbuf);
   2740  1.110.2.2  mjf 		if (len <= 1)
   2741  1.110.2.2  mjf 			/* unchanged if just <enter> */
   2742  1.110.2.2  mjf 			return;
   2743  1.110.2.2  mjf 		/* now strip trailing spaces, <space><enter> deletes string */
   2744  1.110.2.2  mjf 		do
   2745  1.110.2.2  mjf 			lbuf[--len] = 0;
   2746  1.110.2.2  mjf 		while (len != 0 && lbuf[len - 1] == ' ');
   2747  1.110.2.2  mjf 		if (len < length)
   2748  1.110.2.2  mjf 			break;
   2749  1.110.2.2  mjf 		printf("'%s' is longer than %d characters.\n",
   2750  1.110.2.2  mjf 		    lbuf, length - 1);
   2751  1.110.2.2  mjf 	}
   2752  1.110.2.2  mjf 	strncpy(buf, lbuf, length);
   2753  1.110.2.2  mjf }
   2754  1.110.2.2  mjf #endif
   2755  1.110.2.2  mjf 
   2756  1.110.2.2  mjf int
   2757  1.110.2.2  mjf type_match(const void *key, const void *item)
   2758  1.110.2.2  mjf {
   2759  1.110.2.2  mjf 	const int *idp = key;
   2760  1.110.2.2  mjf 	const struct mbr_ptype *ptr = item;
   2761  1.110.2.2  mjf 
   2762  1.110.2.2  mjf 	if (*idp < ptr->id)
   2763  1.110.2.2  mjf 		return (-1);
   2764  1.110.2.2  mjf 	if (*idp > ptr->id)
   2765  1.110.2.2  mjf 		return (1);
   2766  1.110.2.2  mjf 	return (0);
   2767  1.110.2.2  mjf }
   2768  1.110.2.2  mjf 
   2769  1.110.2.2  mjf const char *
   2770  1.110.2.2  mjf get_type(int type)
   2771  1.110.2.2  mjf {
   2772  1.110.2.2  mjf 	struct mbr_ptype *ptr;
   2773  1.110.2.2  mjf 
   2774  1.110.2.2  mjf 	ptr = bsearch(&type, mbr_ptypes, KNOWN_SYSIDS,
   2775  1.110.2.2  mjf 	    sizeof(mbr_ptypes[0]), type_match);
   2776  1.110.2.2  mjf 	if (ptr == 0)
   2777  1.110.2.2  mjf 		return ("unknown");
   2778  1.110.2.2  mjf 	return (ptr->name);
   2779  1.110.2.2  mjf }
   2780