Home | History | Annotate | Line # | Download | only in fdisk
fdisk.c revision 1.62
      1  1.61       dsl /*	$NetBSD: fdisk.c,v 1.62 2003/04/30 19:47:10 dsl Exp $ */
      2  1.10       cgd 
      3   1.4   mycroft /*
      4   1.1       cgd  * Mach Operating System
      5   1.1       cgd  * Copyright (c) 1992 Carnegie Mellon University
      6   1.1       cgd  * All Rights Reserved.
      7   1.4   mycroft  *
      8   1.1       cgd  * Permission to use, copy, modify and distribute this software and its
      9   1.1       cgd  * documentation is hereby granted, provided that both the copyright
     10   1.1       cgd  * notice and this permission notice appear in all copies of the
     11   1.1       cgd  * software, derivative works or modified versions, and any portions
     12   1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     13   1.4   mycroft  *
     14   1.1       cgd  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15   1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16   1.1       cgd  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17   1.4   mycroft  *
     18   1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19   1.4   mycroft  *
     20   1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21   1.1       cgd  *  School of Computer Science
     22   1.1       cgd  *  Carnegie Mellon University
     23   1.1       cgd  *  Pittsburgh PA 15213-3890
     24   1.4   mycroft  *
     25   1.1       cgd  * any improvements or extensions that they make and grant Carnegie Mellon
     26   1.1       cgd  * the rights to redistribute these changes.
     27   1.1       cgd  */
     28   1.1       cgd 
     29  1.62       dsl /*
     30  1.62       dsl  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
     31  1.62       dsl  *	Copyright (c) 1989	Robert. V. Baron
     32  1.62       dsl  *	Created.
     33  1.62       dsl  */
     34  1.62       dsl 
     35  1.16      phil #include <sys/cdefs.h>
     36  1.16      phil 
     37   1.2   mycroft #ifndef lint
     38  1.61       dsl __RCSID("$NetBSD: fdisk.c,v 1.62 2003/04/30 19:47:10 dsl Exp $");
     39   1.2   mycroft #endif /* not lint */
     40   1.2   mycroft 
     41   1.1       cgd #include <sys/types.h>
     42   1.1       cgd #include <sys/disklabel.h>
     43  1.32   thorpej #include <sys/disklabel_mbr.h>
     44   1.4   mycroft #include <sys/ioctl.h>
     45  1.18     lukem #include <sys/param.h>
     46   1.1       cgd #include <sys/stat.h>
     47  1.48     lukem #include <sys/sysctl.h>
     48   1.4   mycroft 
     49   1.8       cgd #include <ctype.h>
     50   1.4   mycroft #include <err.h>
     51  1.18     lukem #include <errno.h>
     52   1.1       cgd #include <fcntl.h>
     53  1.18     lukem #include <paths.h>
     54   1.4   mycroft #include <stdio.h>
     55   1.4   mycroft #include <stdlib.h>
     56  1.62       dsl #include <stdarg.h>
     57   1.8       cgd #include <string.h>
     58   1.8       cgd #include <unistd.h>
     59  1.19     lukem #include <util.h>
     60   1.1       cgd 
     61  1.58      fvdl #if defined(__i386__) || defined(__x86_64__)
     62  1.34      fvdl #include <machine/cpu.h>
     63  1.62       dsl #define BOOTSEL
     64  1.34      fvdl #endif
     65  1.34      fvdl 
     66   1.1       cgd #define LBUF 100
     67   1.1       cgd static char lbuf[LBUF];
     68   1.1       cgd 
     69  1.39      soda #ifndef _PATH_DEFDISK
     70  1.62       dsl #define _PATH_DEFDISK	"/dev/rwd0d"
     71  1.39      soda #endif
     72  1.39      soda 
     73  1.44     lukem const char *disk = _PATH_DEFDISK;
     74   1.1       cgd 
     75   1.1       cgd struct disklabel disklabel;		/* disk parameters */
     76   1.1       cgd 
     77  1.62       dsl uint cylinders, sectors, heads;
     78  1.62       dsl daddr_t disksectors;
     79  1.62       dsl #define cylindersectors (heads * sectors)
     80  1.62       dsl 
     81  1.62       dsl mbr_sector_t mboot;
     82  1.62       dsl 
     83  1.62       dsl 
     84  1.62       dsl struct {
     85  1.62       dsl 	mbr_sector_t	*ptn;		/* array of pbrs */
     86  1.62       dsl 	daddr_t		base;		/* first sector of ext. ptn */
     87  1.62       dsl 	daddr_t		limit;		/* last sector of ext. ptn */
     88  1.62       dsl 	int		num_ptn;	/* number of contained partitions */
     89  1.62       dsl 	int		ptn_id;		/* entry in mbr */
     90  1.62       dsl 	int		is_corrupt;	/* 1 if extended chain illegal */
     91  1.62       dsl } ext;
     92   1.1       cgd 
     93  1.62       dsl char *boot_dir = DEFAULT_BOOTDIR;
     94  1.62       dsl char *boot_path = 0;			/* name of file we actually opened */
     95   1.1       cgd 
     96  1.62       dsl #ifdef BOOTSEL
     97  1.62       dsl 
     98  1.62       dsl #define DEFAULT_ACTIVE	(~(daddr_t)0)
     99  1.62       dsl 
    100  1.62       dsl #define OPTIONS			"0123BSafiluvs:b:c:E:r:w:"
    101  1.34      fvdl #else
    102  1.62       dsl #define change_part(e, p, id, st, sz, bm) change__part(e, p, id, st, sz)
    103  1.62       dsl #define OPTIONS			"0123Safiluvs:b:c:E:r:w:"
    104  1.30        ws #endif
    105  1.30        ws 
    106  1.62       dsl uint dos_cylinders;
    107  1.62       dsl uint dos_heads;
    108  1.62       dsl uint dos_sectors;
    109  1.62       dsl daddr_t dos_disksectors;
    110  1.62       dsl #define dos_cylindersectors (dos_heads * dos_sectors)
    111  1.62       dsl #define dos_totalsectors (dos_heads * dos_sectors * dos_cylinders)
    112   1.1       cgd 
    113   1.4   mycroft #define DOSSECT(s,c)	(((s) & 0x3f) | (((c) >> 2) & 0xc0))
    114   1.4   mycroft #define DOSCYL(c)	((c) & 0xff)
    115  1.62       dsl #define SEC_IN_1M (1024 * 1024 / 512)
    116  1.62       dsl #define SEC_TO_MB(sec) ((uint)(((sec) + SEC_IN_1M / 2) / SEC_IN_1M))
    117  1.62       dsl #define SEC_TO_CYL(sec) (((sec) + dos_cylindersectors/2) / dos_cylindersectors)
    118  1.62       dsl 
    119  1.62       dsl #define MAXCYL		1024	/* Usual limit is 1023 */
    120  1.62       dsl #define	MAXHEAD		256	/* Usual limit is 255 */
    121  1.62       dsl #define	MAXSECTOR	63
    122  1.62       dsl int partition = -1;
    123  1.28        ws 
    124  1.62       dsl int fd = -1, wfd = -1, *rfd = &fd;
    125  1.62       dsl char *disk_file;
    126   1.4   mycroft 
    127   1.4   mycroft int a_flag;		/* set active partition */
    128  1.28        ws int i_flag;		/* init bootcode */
    129   1.4   mycroft int u_flag;		/* update partition data */
    130  1.62       dsl int v_flag;		/* more verbose */
    131  1.15      phil int sh_flag;		/* Output data as shell defines */
    132  1.15      phil int f_flag;		/* force --not interactive */
    133  1.15      phil int s_flag;		/* set id,offset,size */
    134  1.16      phil int b_flag;		/* Set cyl, heads, secs (as c/h/s) */
    135  1.34      fvdl int B_flag;		/* Edit/install bootselect code */
    136  1.62       dsl int E_flag;		/* extended partition number */
    137  1.16      phil int b_cyl, b_head, b_sec;  /* b_flag values. */
    138   1.4   mycroft 
    139  1.62       dsl mbr_sector_t bootcode[8192 / sizeof (mbr_sector_t)];
    140  1.30        ws int bootsize;		/* actual size of bootcode */
    141  1.62       dsl int boot_installed;	/* 1 if we've copied code into the mbr */
    142  1.62       dsl 
    143  1.62       dsl #if defined(__i386__) || defined(__x86_64__)
    144  1.62       dsl struct disklist *dl;
    145  1.62       dsl #endif
    146  1.30        ws 
    147  1.43     lukem 
    148  1.25  drochner static char reserved[] = "reserved";
    149  1.25  drochner 
    150   1.4   mycroft struct part_type {
    151  1.44     lukem 	int		 type;
    152  1.44     lukem 	const char	*name;
    153   1.4   mycroft } part_types[] = {
    154  1.62       dsl 	{0x00, "<UNUSED>"},
    155   1.4   mycroft 	{0x01, "Primary DOS with 12 bit FAT"},
    156   1.4   mycroft 	{0x02, "XENIX / filesystem"},
    157   1.4   mycroft 	{0x03, "XENIX /usr filesystem"},
    158  1.13     perry 	{0x04, "Primary DOS with 16 bit FAT <32M"},
    159  1.25  drochner 	{0x05, "Extended partition"},
    160  1.13     perry 	{0x06, "Primary 'big' DOS, 16-bit FAT (> 32MB)"},
    161  1.13     perry 	{0x07, "OS/2 HPFS or NTFS or QNX2 or Advanced UNIX"},
    162  1.25  drochner 	{0x08, "AIX filesystem or OS/2 (thru v1.3) or DELL multiple drives"
    163  1.25  drochner 	       "or Commodore DOS or SplitDrive"},
    164   1.4   mycroft 	{0x09, "AIX boot partition or Coherent"},
    165  1.13     perry 	{0x0A, "OS/2 Boot Manager or Coherent swap or OPUS"},
    166  1.25  drochner 	{0x0b, "Primary DOS with 32 bit FAT"},
    167  1.25  drochner 	{0x0c, "Primary DOS with 32 bit FAT - LBA"},
    168  1.25  drochner 	{0x0d, "Type 7??? - LBA"},
    169  1.25  drochner 	{0x0E, "DOS (16-bit FAT) - LBA"},
    170  1.25  drochner 	{0x0F, "Ext. partition - LBA"},
    171   1.4   mycroft 	{0x10, "OPUS"},
    172  1.13     perry 	{0x11, "OS/2 BM: hidden DOS 12-bit FAT"},
    173  1.13     perry 	{0x12, "Compaq diagnostics"},
    174  1.25  drochner 	{0x14, "OS/2 BM: hidden DOS 16-bit FAT <32M or Novell DOS 7.0 bug"},
    175  1.13     perry 	{0x16, "OS/2 BM: hidden DOS 16-bit FAT >=32M"},
    176  1.13     perry 	{0x17, "OS/2 BM: hidden IFS"},
    177  1.13     perry 	{0x18, "AST Windows swapfile"},
    178  1.25  drochner 	{0x19, "Willowtech Photon coS"},
    179  1.25  drochner 	{0x1e, "hidden FAT95"},
    180  1.25  drochner 	{0x20, "Willowsoft OFS1"},
    181  1.25  drochner 	{0x21, reserved},
    182  1.25  drochner 	{0x23, reserved},
    183  1.13     perry 	{0x24, "NEC DOS"},
    184  1.25  drochner 	{0x26, reserved},
    185  1.25  drochner 	{0x31, reserved},
    186  1.25  drochner 	{0x33, reserved},
    187  1.25  drochner 	{0x34, reserved},
    188  1.25  drochner 	{0x36, reserved},
    189  1.25  drochner 	{0x38, "Theos"},
    190  1.13     perry 	{0x3C, "PartitionMagic recovery"},
    191  1.25  drochner 	{0x40, "VENIX 286 or LynxOS"},
    192  1.25  drochner 	{0x41, "Linux/MINIX (sharing disk with DRDOS) or Personal RISC boot"},
    193  1.13     perry 	{0x42, "SFS or Linux swap (sharing disk with DRDOS)"},
    194  1.13     perry 	{0x43, "Linux native (sharing disk with DRDOS)"},
    195  1.41       wiz 	{0x4D, "QNX4.x"},
    196  1.41       wiz 	{0x4E, "QNX4.x 2nd part"},
    197  1.41       wiz 	{0x4F, "QNX4.x 3rd part"},
    198  1.13     perry 	{0x50, "DM (disk manager)"},
    199  1.13     perry 	{0x51, "DM6 Aux1 (or Novell)"},
    200   1.4   mycroft 	{0x52, "CP/M or Microport SysV/AT"},
    201  1.13     perry 	{0x53, "DM6 Aux3"},
    202  1.25  drochner 	{0x54, "DM6 DDO"},
    203  1.13     perry 	{0x55, "EZ-Drive (disk manager)"},
    204  1.13     perry 	{0x56, "Golden Bow (disk manager)"},
    205  1.13     perry 	{0x5C, "Priam Edisk (disk manager)"},
    206  1.13     perry 	{0x61, "SpeedStor"},
    207  1.25  drochner 	{0x63, "GNU HURD or Mach or Sys V/386 (such as ISC UNIX) or MtXinu"},
    208  1.25  drochner 	{0x64, "Novell Netware 2.xx or Speedstore"},
    209   1.4   mycroft 	{0x65, "Novell Netware 3.xx"},
    210  1.25  drochner 	{0x66, "Novell 386 Netware"},
    211  1.25  drochner 	{0x67, "Novell"},
    212  1.25  drochner 	{0x68, "Novell"},
    213  1.25  drochner 	{0x69, "Novell"},
    214  1.13     perry 	{0x70, "DiskSecure Multi-Boot"},
    215  1.25  drochner 	{0x71, reserved},
    216  1.25  drochner 	{0x73, reserved},
    217  1.25  drochner 	{0x74, reserved},
    218  1.13     perry 	{0x75, "PC/IX"},
    219  1.25  drochner 	{0x76, reserved},
    220  1.13     perry 	{0x80, "MINIX until 1.4a"},
    221  1.13     perry 	{0x81, "MINIX since 1.4b, early Linux, Mitac dmgr"},
    222  1.25  drochner 	{0x82, "Linux swap or Prime or Solaris"},
    223  1.13     perry 	{0x83, "Linux native"},
    224  1.13     perry 	{0x84, "OS/2 hidden C: drive"},
    225  1.13     perry 	{0x85, "Linux extended"},
    226  1.25  drochner 	{0x86, "NT FAT volume set"},
    227  1.25  drochner 	{0x87, "NTFS volume set or HPFS mirrored"},
    228   1.4   mycroft 	{0x93, "Amoeba filesystem"},
    229   1.4   mycroft 	{0x94, "Amoeba bad block table"},
    230  1.25  drochner 	{0x99, "Mylex EISA SCSI"},
    231  1.25  drochner 	{0x9f, "BSDI?"},
    232  1.13     perry 	{0xA0, "IBM Thinkpad hibernation"},
    233  1.25  drochner 	{0xa1, reserved},
    234  1.25  drochner 	{0xa3, reserved},
    235  1.25  drochner 	{0xa4, reserved},
    236  1.25  drochner 	{0xA5, "FreeBSD or 386BSD or old NetBSD"},
    237  1.13     perry 	{0xA6, "OpenBSD"},
    238  1.13     perry 	{0xA7, "NeXTSTEP 486"},
    239  1.49       dbj 	{0xa8, "Apple UFS"},
    240  1.25  drochner 	{0xa9, "NetBSD"},
    241  1.49       dbj 	{0xab, "Apple Boot"},
    242  1.55       dbj 	{0xaf, "Apple HFS"},
    243  1.25  drochner 	{0xb1, reserved},
    244  1.25  drochner 	{0xb3, reserved},
    245  1.25  drochner 	{0xb4, reserved},
    246  1.25  drochner 	{0xb6, reserved},
    247   1.4   mycroft 	{0xB7, "BSDI BSD/386 filesystem"},
    248   1.4   mycroft 	{0xB8, "BSDI BSD/386 swap"},
    249  1.25  drochner 	{0xc0, "CTOS"},
    250  1.13     perry 	{0xC1, "DRDOS/sec (FAT-12)"},
    251  1.13     perry 	{0xC4, "DRDOS/sec (FAT-16, < 32M)"},
    252  1.13     perry 	{0xC6, "DRDOS/sec (FAT-16, >= 32M)"},
    253  1.25  drochner 	{0xC7, "Syrinx (Cyrnix?) or HPFS disabled"},
    254  1.25  drochner 	{0xd8, "CP/M 86"},
    255  1.13     perry 	{0xDB, "CP/M or Concurrent CP/M or Concurrent DOS or CTOS"},
    256  1.13     perry 	{0xE1, "DOS access or SpeedStor 12-bit FAT extended partition"},
    257  1.25  drochner 	{0xE3, "DOS R/O or SpeedStor or Storage Dimensions"},
    258  1.13     perry 	{0xE4, "SpeedStor 16-bit FAT extended partition < 1024 cyl."},
    259  1.25  drochner 	{0xe5, reserved},
    260  1.25  drochner 	{0xe6, reserved},
    261  1.33     perry 	{0xeb, "BeOS"},
    262  1.25  drochner 	{0xF1, "SpeedStor or Storage Dimensions"},
    263   1.4   mycroft 	{0xF2, "DOS 3.3+ Secondary"},
    264  1.25  drochner 	{0xf3, reserved},
    265  1.25  drochner 	{0xF4, "SpeedStor large partition or Storage Dimensions"},
    266  1.25  drochner 	{0xf6, reserved},
    267  1.25  drochner 	{0xFE, "SpeedStor >1024 cyl. or LANstep or IBM PS/2 IML"},
    268  1.13     perry 	{0xFF, "Xenix Bad Block Table"},
    269   1.1       cgd };
    270   1.1       cgd 
    271  1.50  christos #define KNOWN_SYSIDS	(sizeof(part_types)/sizeof(part_types[0]))
    272  1.50  christos 
    273  1.43     lukem void	usage(void);
    274  1.43     lukem void	print_s0(int);
    275  1.62       dsl void	print_part(mbr_sector_t *, int, daddr_t);
    276  1.62       dsl void	print_mbr_partition(mbr_sector_t *, int, daddr_t, daddr_t, int);
    277  1.62       dsl int	read_boot(const char *, void *, size_t, int);
    278  1.62       dsl void	init_sector0(int);
    279  1.43     lukem void	intuit_translated_geometry(void);
    280  1.43     lukem void	get_geometry(void);
    281  1.62       dsl void	get_extended_ptn(void);
    282  1.44     lukem void	get_diskname(const char *, char *, size_t);
    283  1.62       dsl int	change_part(int, int, int, daddr_t, daddr_t, char *);
    284  1.43     lukem void	print_params(void);
    285  1.43     lukem void	change_active(int);
    286  1.43     lukem void	get_params_to_use(void);
    287  1.43     lukem void	dos(int, unsigned char *, unsigned char *, unsigned char *);
    288  1.43     lukem int	open_disk(int);
    289  1.62       dsl int	read_disk(daddr_t, void *);
    290  1.62       dsl int	write_disk(daddr_t, void *);
    291  1.43     lukem int	get_params(void);
    292  1.62       dsl int	read_s0(daddr_t, mbr_sector_t *);
    293  1.62       dsl int	write_mbr(void);
    294  1.62       dsl int	yesno(const char *, ...);
    295  1.62       dsl int	decimal(const char *, int, int, int, int);
    296  1.62       dsl #define DEC_SEC		1		/* asking for a sector number */
    297  1.62       dsl #define	DEC_RND		2		/* round to end of first track */
    298  1.62       dsl #define	DEC_RND_0	4		/* round 0 to size of a track */
    299  1.62       dsl #define DEC_RND_DOWN	8		/* subtract 1 track */
    300  1.62       dsl #define DEC_RND_DOWN_2	16		/* subtract 2 tracks */
    301  1.62       dsl void	string(const char *, int, char *);
    302  1.62       dsl int	ptn_id(const char *, int *);
    303  1.43     lukem int	type_match(const void *, const void *);
    304  1.43     lukem const char *get_type(int);
    305  1.62       dsl int	get_mapping(int, uint *, uint *, uint *, unsigned long *);
    306  1.62       dsl #ifdef BOOTSEL
    307  1.62       dsl daddr_t	configure_bootsel(daddr_t);
    308  1.62       dsl void	install_bootsel(int);
    309  1.62       dsl daddr_t	get_default_boot(void);
    310  1.62       dsl void	set_default_boot(daddr_t);
    311  1.34      fvdl #endif
    312  1.16      phil 
    313  1.16      phil 
    314  1.43     lukem int	main(int, char *[]);
    315   1.1       cgd 
    316   1.4   mycroft int
    317  1.43     lukem main(int argc, char *argv[])
    318   1.1       cgd {
    319  1.62       dsl 	struct stat sb;
    320  1.62       dsl 	int ch, mib[2];
    321  1.52   thorpej 	size_t len;
    322  1.48     lukem 	char *root_device;
    323  1.62       dsl 	char *cp;
    324  1.62       dsl 	int n;
    325  1.62       dsl #ifdef BOOTSEL
    326  1.62       dsl 	daddr_t default_ptn;		/* start sector of default ptn */
    327  1.62       dsl 	char *cbootmenu = 0;
    328  1.62       dsl #endif
    329   1.1       cgd 
    330  1.15      phil 	int csysid, cstart, csize;	/* For the b_flag. */
    331  1.15      phil 
    332  1.48     lukem 	mib[0] = CTL_KERN;
    333  1.48     lukem 	mib[1] = KERN_ROOT_DEVICE;
    334  1.48     lukem 	if (sysctl(mib, 2, NULL, &len, NULL, 0) != -1 &&
    335  1.48     lukem 	    (root_device = malloc(len)) != NULL &&
    336  1.48     lukem 	    sysctl(mib, 2, root_device, &len, NULL, 0) != -1)
    337  1.48     lukem 		disk = root_device;
    338  1.48     lukem 
    339  1.16      phil 	a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
    340  1.62       dsl 	v_flag = 0;
    341  1.62       dsl 	E_flag = 0;
    342  1.15      phil 	csysid = cstart = csize = 0;
    343  1.34      fvdl 	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
    344   1.4   mycroft 		switch (ch) {
    345   1.4   mycroft 		case '0':
    346   1.4   mycroft 			partition = 0;
    347   1.4   mycroft 			break;
    348   1.4   mycroft 		case '1':
    349   1.4   mycroft 			partition = 1;
    350   1.4   mycroft 			break;
    351   1.4   mycroft 		case '2':
    352   1.4   mycroft 			partition = 2;
    353   1.4   mycroft 			break;
    354   1.4   mycroft 		case '3':
    355   1.4   mycroft 			partition = 3;
    356   1.4   mycroft 			break;
    357  1.62       dsl 		case 'E':	/* Extended partition number */
    358  1.62       dsl 			E_flag = 1;
    359  1.62       dsl 			partition = strtoul(optarg, &cp, 0);
    360  1.62       dsl 			if (*cp || partition < 0)
    361  1.62       dsl 				errx(1, "Bad partition number -E %s.", optarg);
    362  1.62       dsl 			break;
    363  1.62       dsl #ifdef BOOTSEL
    364  1.62       dsl 		case 'B':	/* Bootselect parameters */
    365  1.34      fvdl 			B_flag = 1;
    366  1.34      fvdl 			break;
    367  1.34      fvdl #endif
    368  1.62       dsl 		case 'S':	/* Output as shell variables */
    369  1.15      phil 			sh_flag = 1;
    370  1.15      phil 			break;
    371  1.62       dsl 		case 'a':	/* Set active partition */
    372   1.4   mycroft 			a_flag = 1;
    373   1.4   mycroft 			break;
    374  1.62       dsl 		case 'f':	/* Non interactive */
    375  1.15      phil 			f_flag = 1;
    376  1.15      phil 			break;
    377  1.62       dsl 		case 'i':	/* Always update bootcode */
    378   1.4   mycroft 			i_flag = 1;
    379  1.15      phil 			break;
    380  1.62       dsl 		case 'l':	/* List known partition types */
    381  1.50  christos 			for (len = 0; len < KNOWN_SYSIDS; len++)
    382  1.56   jdarrow 				printf("%03d %s\n", part_types[len].type,
    383  1.62       dsl 				    part_types[len].name);
    384  1.50  christos 			return 0;
    385  1.62       dsl 		case 'u':	/* Update partition details */
    386   1.4   mycroft 			u_flag = 1;
    387   1.1       cgd 			break;
    388  1.62       dsl 		case 'v':	/* Be verbose */
    389  1.62       dsl 			v_flag++;
    390  1.62       dsl 			break;
    391  1.62       dsl 		case 's':	/* Partition details */
    392  1.14      phil 			s_flag = 1;
    393  1.62       dsl 			if (sscanf(optarg, "%d/%d/%d%n", &csysid, &cstart,
    394  1.62       dsl 			    &csize, &n) == 3) {
    395  1.62       dsl 				if (optarg[n] == 0)
    396  1.62       dsl 					break;
    397  1.62       dsl #ifdef BOOTSEL
    398  1.62       dsl 				if (optarg[n] == '/') {
    399  1.62       dsl 					cbootmenu = optarg + n + 1;
    400  1.62       dsl 					break;
    401  1.62       dsl 				}
    402  1.62       dsl #endif
    403  1.62       dsl 			}
    404  1.62       dsl 			errx(1, "Bad argument to the -s flag.");
    405  1.16      phil 			break;
    406  1.62       dsl 		case 'b':	/* BIOS geometry */
    407  1.16      phil 			b_flag = 1;
    408  1.62       dsl 			if (sscanf(optarg, "%d/%d/%d%n", &b_cyl, &b_head,
    409  1.62       dsl 			    &b_sec, &n) != 3 || optarg[n] != 0)
    410  1.54     soren 				errx(1, "Bad argument to the -b flag.");
    411  1.28        ws 			if (b_cyl > MAXCYL)
    412  1.28        ws 				b_cyl = MAXCYL;
    413  1.14      phil 			break;
    414  1.62       dsl 		case 'c':	/* file/directory containing boot code */
    415  1.62       dsl 			if (strchr(optarg, '/') != NULL &&
    416  1.62       dsl 			    stat(optarg, &sb) == 0 &&
    417  1.62       dsl 			    (sb.st_mode & S_IFMT) == S_IFDIR) {
    418  1.62       dsl 				boot_dir = optarg;
    419  1.62       dsl 				break;
    420  1.62       dsl 			}
    421  1.62       dsl 			bootsize = read_boot(optarg, bootcode,
    422  1.62       dsl 						sizeof bootcode, 1);
    423  1.62       dsl 			i_flag = 1;
    424  1.62       dsl 			break;
    425  1.62       dsl 		case 'r':	/* read data from disk_file (not raw disk) */
    426  1.62       dsl 			rfd = &wfd;
    427  1.62       dsl 			/* FALLTHROUGH */
    428  1.62       dsl 		case 'w':	/* write data to disk_file */
    429  1.62       dsl 			disk_file = optarg;
    430  1.30        ws 			break;
    431  1.62       dsl 
    432   1.4   mycroft 		default:
    433   1.4   mycroft 			usage();
    434   1.1       cgd 		}
    435   1.4   mycroft 	argc -= optind;
    436   1.4   mycroft 	argv += optind;
    437   1.1       cgd 
    438  1.15      phil 	if (sh_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
    439  1.15      phil 		usage();
    440  1.15      phil 
    441  1.62       dsl 	if (B_flag && f_flag) {
    442  1.62       dsl 		warnx("Bootselector may only be configured interactively");
    443  1.34      fvdl 		usage();
    444  1.62       dsl 	}
    445  1.62       dsl 
    446  1.62       dsl 	if (f_flag && u_flag && !s_flag) {
    447  1.62       dsl 		warnx("Partition data not specified");
    448  1.62       dsl 		usage();
    449  1.62       dsl 	}
    450  1.34      fvdl 
    451  1.62       dsl 	if (s_flag && partition == -1) {
    452  1.50  christos 		warnx("-s flag requires a partition selected.");
    453  1.14      phil 		usage();
    454  1.15      phil 	}
    455  1.15      phil 
    456   1.1       cgd 	if (argc > 0)
    457   1.1       cgd 		disk = argv[0];
    458   1.4   mycroft 
    459  1.34      fvdl 	if (open_disk(B_flag || a_flag || i_flag || u_flag) < 0)
    460   1.1       cgd 		exit(1);
    461   1.1       cgd 
    462  1.44     lukem 	if (read_s0(0, &mboot))
    463  1.62       dsl 		/* must have been a blank disk */
    464  1.62       dsl 		init_sector0(1);
    465  1.11   ghudson 
    466  1.58      fvdl #if defined(__i386__) || defined(__x86_64__)
    467  1.34      fvdl 	get_geometry();
    468  1.34      fvdl #else
    469  1.11   ghudson 	intuit_translated_geometry();
    470  1.34      fvdl #endif
    471  1.62       dsl 	get_extended_ptn();
    472  1.15      phil 
    473  1.62       dsl #ifdef BOOTSEL
    474  1.62       dsl 	default_ptn = get_default_boot();
    475  1.62       dsl #endif
    476  1.62       dsl 
    477  1.62       dsl 	if (E_flag && !u_flag && partition >= ext.num_ptn)
    478  1.62       dsl 		errx(1, "Extended partition %d is not defined.", partition);
    479  1.15      phil 
    480  1.62       dsl 	if (u_flag && (!f_flag || b_flag))
    481   1.1       cgd 		get_params_to_use();
    482   1.1       cgd 
    483  1.15      phil 	/* Do the update stuff! */
    484  1.15      phil 	if (u_flag) {
    485  1.62       dsl 		if (s_flag)
    486  1.62       dsl 			change_part(E_flag, partition, csysid, cstart, csize,
    487  1.62       dsl 				cbootmenu);
    488  1.62       dsl 		else {
    489  1.62       dsl 			int part = partition, chg_ext = E_flag, prompt = 1;
    490  1.62       dsl 			do {
    491  1.62       dsl 				if (prompt) {
    492  1.62       dsl 					printf("\n");
    493  1.62       dsl 					print_s0(partition);
    494  1.62       dsl 				}
    495  1.62       dsl 				if (partition == -1)
    496  1.62       dsl 					part = ptn_id(
    497  1.62       dsl 				    "Which partition do you want to change?",
    498  1.62       dsl 							&chg_ext);
    499  1.62       dsl 				if (part < 0)
    500  1.62       dsl 					break;
    501  1.62       dsl 				prompt = change_part(chg_ext, part, 0, 0, 0, 0);
    502  1.62       dsl 			} while (partition == -1);
    503  1.62       dsl 		}
    504   1.4   mycroft 	} else
    505  1.62       dsl 		if (!i_flag && !B_flag) {
    506  1.62       dsl 			print_params();
    507  1.15      phil 			print_s0(partition);
    508  1.62       dsl 		}
    509   1.1       cgd 
    510  1.62       dsl 	if (a_flag && !E_flag)
    511   1.1       cgd 		change_active(partition);
    512   1.1       cgd 
    513  1.62       dsl #ifdef BOOTSEL
    514  1.62       dsl 	if (B_flag || u_flag || i_flag)
    515  1.62       dsl 		/* Ensure the mbr code supports this configuration */
    516  1.62       dsl 		install_bootsel(0);
    517  1.62       dsl 	if (B_flag)
    518  1.62       dsl 		default_ptn = configure_bootsel(default_ptn);
    519  1.62       dsl 	set_default_boot(default_ptn);
    520  1.62       dsl #else
    521  1.62       dsl 	if (i_flag)
    522  1.62       dsl 		init_sector0(0);
    523  1.34      fvdl #endif
    524  1.34      fvdl 
    525  1.62       dsl 	if (u_flag || a_flag || i_flag || B_flag) {
    526  1.15      phil 		if (!f_flag) {
    527  1.34      fvdl 			printf("\nWe haven't written the MBR back to disk "
    528  1.15      phil 			       "yet.  This is your last chance.\n");
    529  1.62       dsl 			if (u_flag)
    530  1.62       dsl 				print_s0(-1);
    531  1.15      phil 			if (yesno("Should we write new partition table?"))
    532  1.62       dsl 				write_mbr();
    533  1.15      phil 		} else
    534  1.62       dsl 			write_mbr();
    535   1.1       cgd 	}
    536   1.1       cgd 
    537   1.1       cgd 	exit(0);
    538   1.4   mycroft }
    539   1.1       cgd 
    540   1.4   mycroft void
    541  1.43     lukem usage(void)
    542   1.4   mycroft {
    543  1.62       dsl 	int indent = 7 + (int)strlen(getprogname()) + 1;
    544  1.43     lukem 
    545  1.62       dsl 	(void)fprintf(stderr, "Usage: %s [-afiluvBS] "
    546  1.62       dsl 		"[-b cylinders/heads/sectors] \\\n"
    547  1.62       dsl 		"%*s[-0123 | -E num "
    548  1.62       dsl 		"[-s id/start/size[/bootmenu]]] \\\n"
    549  1.62       dsl 		"%*s[-c bootcode] [-r|-w file] [device]\n"
    550  1.62       dsl 		"\t-a change active partition\n"
    551  1.62       dsl 		"\t-f force - not interactive\n"
    552  1.62       dsl 		"\t-i initialise MBR code\n"
    553  1.62       dsl 		"\t-l list partition types\n"
    554  1.62       dsl 		"\t-u update partition data\n"
    555  1.62       dsl 		"\t-v verbose output, -v -v more verbose still\n"
    556  1.62       dsl 		"\t-B update bootselect options\n"
    557  1.62       dsl 		"\t-S output as shell defines\n",
    558  1.62       dsl 		getprogname(), indent, "", indent, "");
    559   1.4   mycroft 	exit(1);
    560   1.1       cgd }
    561   1.1       cgd 
    562  1.62       dsl static daddr_t
    563  1.62       dsl ext_offset(int part)
    564  1.62       dsl {
    565  1.62       dsl 	daddr_t offset = ext.base;
    566  1.62       dsl 
    567  1.62       dsl 	if (part != 0)
    568  1.62       dsl 		offset += le32toh(ext.ptn[part - 1].mbr_parts[1].mbrp_start);
    569  1.62       dsl 	return offset;
    570  1.62       dsl }
    571  1.62       dsl 
    572   1.4   mycroft void
    573  1.43     lukem print_s0(int which)
    574   1.1       cgd {
    575   1.4   mycroft 	int part;
    576   1.1       cgd 
    577   1.4   mycroft 	if (which == -1) {
    578  1.62       dsl 		if (!sh_flag)
    579  1.62       dsl 			printf("Partition table:\n");
    580  1.32   thorpej 		for (part = 0; part < NMBRPART; part++) {
    581  1.15      phil 			if (!sh_flag)
    582  1.15      phil 				printf("%d: ", part);
    583  1.62       dsl 			print_part(&mboot, part, 0);
    584  1.15      phil 		}
    585  1.62       dsl 		if (!sh_flag) {
    586  1.62       dsl 			if (ext.is_corrupt)
    587  1.62       dsl 				printf("Extended partition table is currupt\n");
    588  1.62       dsl 			else
    589  1.62       dsl 				if (ext.num_ptn != 0)
    590  1.62       dsl 					printf("Extended partition table:\n");
    591  1.62       dsl 		}
    592  1.62       dsl 		for (part = 0; part < ext.num_ptn; part++) {
    593  1.62       dsl 			if (!sh_flag)
    594  1.62       dsl 				printf("E%d: ", part);
    595  1.62       dsl 			print_part(&ext.ptn[part], 0, ext_offset(part));
    596  1.62       dsl 			if (!sh_flag && v_flag >= 2) {
    597  1.62       dsl 				printf("link: ");
    598  1.62       dsl 				print_mbr_partition(&ext.ptn[part], 1,
    599  1.62       dsl 						ext_offset(part), ext.base, 0);
    600  1.62       dsl 			}
    601  1.62       dsl 		}
    602  1.62       dsl #ifdef BOOTSEL
    603  1.62       dsl 		if (!sh_flag &&
    604  1.62       dsl 		    le16toh(mboot.mbr_bootsel.mbrb_magic) == MBR_MAGIC){
    605  1.62       dsl 			int tmo;
    606  1.62       dsl 			printf("Bootselector ");
    607  1.62       dsl 			if (mboot.mbr_bootsel.mbrb_flags & BFL_SELACTIVE) {
    608  1.62       dsl 				printf("enabled");
    609  1.62       dsl 				tmo = le16toh(mboot.mbr_bootsel.mbrb_timeo);
    610  1.62       dsl 				if (tmo == 0xffff)
    611  1.62       dsl 					printf(", infinite timeout");
    612  1.62       dsl 				else
    613  1.62       dsl 					printf(", timeout %d seconds",
    614  1.62       dsl 						    (10 * tmo + 9) / 182);
    615  1.62       dsl 			} else
    616  1.62       dsl 				printf("disabled");
    617  1.62       dsl 			printf(".\n");
    618  1.62       dsl 		}
    619  1.62       dsl #endif
    620  1.62       dsl 		return;
    621  1.62       dsl 	}
    622  1.62       dsl 
    623  1.62       dsl 	if (E_flag) {
    624  1.62       dsl 		if (!sh_flag)
    625  1.62       dsl 			printf("Extended partition E%d:\n", which);
    626  1.62       dsl 		if (which > ext.num_ptn)
    627  1.62       dsl 			printf("Undefined\n");
    628  1.62       dsl 		else
    629  1.62       dsl 			print_part(&ext.ptn[which], 0, ext_offset(which));
    630  1.62       dsl 	} else {
    631  1.62       dsl 		if (!sh_flag)
    632  1.62       dsl 			printf("Partition %d:\n", which);
    633  1.62       dsl 		print_part(&mboot, which, 0);
    634  1.62       dsl 	}
    635   1.1       cgd }
    636   1.1       cgd 
    637  1.62       dsl void
    638  1.62       dsl print_part(mbr_sector_t *boot, int part, daddr_t offset)
    639  1.12   thorpej {
    640  1.62       dsl 	mbr_partition_t *partp;
    641  1.62       dsl 	char *e;
    642  1.62       dsl 
    643  1.62       dsl 	if (!sh_flag) {
    644  1.62       dsl 		print_mbr_partition(boot, part, offset, 0, 0);
    645  1.62       dsl 		return;
    646  1.62       dsl 	}
    647  1.12   thorpej 
    648  1.62       dsl 	partp = &boot->mbr_parts[part];
    649  1.62       dsl 	if (boot != &mboot) {
    650  1.62       dsl 		part = boot - ext.ptn;
    651  1.62       dsl 		e = "E";
    652  1.62       dsl 	} else
    653  1.62       dsl 		e = "";
    654  1.62       dsl 
    655  1.62       dsl 	if (partp->mbrp_typ == 0) {
    656  1.62       dsl 		printf("PART%s%dSIZE=0\n", e, part);
    657  1.62       dsl 		return;
    658  1.62       dsl 	}
    659  1.62       dsl 
    660  1.62       dsl 	printf("PART%s%dID=%d\n", e, part, partp->mbrp_typ);
    661  1.62       dsl 	printf("PART%s%dSIZE=%u\n", e, part, le32toh(partp->mbrp_size));
    662  1.62       dsl 	printf("PART%s%dSTART=%llu\n", e, part,
    663  1.62       dsl 	    (uintmax_t)(offset + le32toh(partp->mbrp_start)));
    664  1.62       dsl 	printf("PART%s%dFLAG=0x%x\n", e, part, partp->mbrp_flag);
    665  1.62       dsl 	printf("PART%s%dBCYL=%d\n", e, part,
    666  1.62       dsl 	    MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect));
    667  1.62       dsl 	printf("PART%s%dBHEAD=%d\n", e, part, partp->mbrp_shd);
    668  1.62       dsl 	printf("PART%s%dBSEC=%d\n", e, part, MBR_PSECT(partp->mbrp_ssect));
    669  1.62       dsl 	printf("PART%s%dECYL=%d\n", e, part,
    670  1.62       dsl 	    MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect));
    671  1.62       dsl 	printf("PART%s%dEHEAD=%d\n", e, part, partp->mbrp_ehd);
    672  1.62       dsl 	printf("PART%s%dESEC=%d\n", e, part, MBR_PSECT(partp->mbrp_esect));
    673  1.12   thorpej }
    674  1.12   thorpej 
    675  1.43     lukem static void
    676  1.62       dsl pr_cyls(daddr_t sector)
    677  1.12   thorpej {
    678  1.62       dsl 	ulong cyl, head, sect;
    679  1.62       dsl 	cyl = sector / dos_cylindersectors;
    680  1.62       dsl 	sect = sector - cyl * dos_cylindersectors;
    681  1.62       dsl 	head = sect / dos_sectors;
    682  1.62       dsl 	sect -= head * dos_sectors;
    683  1.12   thorpej 
    684  1.62       dsl 	printf("%lu", cyl);
    685  1.62       dsl 	if (head == 0 && sect == 0)
    686  1.62       dsl 		return;
    687  1.62       dsl 	printf("/%lu/%lu", head, sect + 1);
    688  1.12   thorpej }
    689  1.12   thorpej 
    690  1.62       dsl void
    691  1.62       dsl print_mbr_partition(mbr_sector_t *boot, int part,
    692  1.62       dsl     daddr_t offset, daddr_t exoffset, int indent)
    693  1.12   thorpej {
    694  1.62       dsl 	daddr_t	start;
    695  1.62       dsl 	daddr_t	size;
    696  1.62       dsl 	mbr_partition_t *partp = &boot->mbr_parts[part];
    697  1.62       dsl 	mbr_sector_t eboot;
    698  1.62       dsl 	int p;
    699  1.62       dsl 	static int dumped = 0;
    700  1.12   thorpej 
    701  1.62       dsl 	if (partp->mbrp_typ == 0 && v_flag < 2) {
    702  1.62       dsl 		printf("<UNUSED>\n");
    703  1.62       dsl 		return;
    704  1.62       dsl 	}
    705  1.12   thorpej 
    706  1.62       dsl 	start = le32toh(partp->mbrp_start);
    707  1.62       dsl 	size = le32toh(partp->mbrp_size);
    708  1.62       dsl 	if (MBR_IS_EXTENDED(partp->mbrp_typ))
    709  1.62       dsl 		start += exoffset;
    710  1.62       dsl 	else
    711  1.62       dsl 		start += offset;
    712  1.12   thorpej 
    713  1.62       dsl 	printf("%s (sysid %d)\n", get_type(partp->mbrp_typ), partp->mbrp_typ);
    714  1.62       dsl #ifdef BOOTSEL
    715  1.62       dsl 	if (le16toh(boot->mbr_bootsel.mbrb_magic) == MBR_MAGIC &&
    716  1.62       dsl 	    boot->mbr_bootsel.mbrb_nametab[part][0])
    717  1.62       dsl 		printf("%*s    bootmenu: %s\n", indent, "",
    718  1.62       dsl 		    boot->mbr_bootsel.mbrb_nametab[part]);
    719  1.62       dsl #endif
    720  1.12   thorpej 
    721  1.62       dsl 	printf("%*s    start %llu, size %llu",
    722  1.62       dsl 	    indent, "", (uintmax_t)start, (uintmax_t)size);
    723  1.62       dsl 	if (size != 0) {
    724  1.62       dsl 		printf(" (%u MB, Cyls ", SEC_TO_MB(size));
    725  1.62       dsl 		if (v_flag == 0 && le32toh(partp->mbrp_start) == dos_sectors)
    726  1.62       dsl 			pr_cyls(start - dos_sectors);
    727  1.62       dsl 		else
    728  1.62       dsl 			pr_cyls(start);
    729  1.62       dsl 		printf("-");
    730  1.62       dsl 		pr_cyls(start + size);
    731  1.62       dsl 		printf(")");
    732  1.62       dsl 	}
    733   1.1       cgd 
    734  1.62       dsl 	switch (partp->mbrp_flag) {
    735  1.62       dsl 	case 0:
    736  1.62       dsl 		break;
    737  1.62       dsl 	case MBR_FLAGS_ACTIVE:
    738  1.62       dsl 		printf(", Active");
    739  1.62       dsl 		break;
    740  1.62       dsl 	default:
    741  1.62       dsl 		printf(", flag 0x%x", partp->mbrp_flag);
    742  1.62       dsl 		break;
    743  1.62       dsl 	}
    744  1.62       dsl 	printf("\n");
    745  1.14      phil 
    746  1.62       dsl 	if (v_flag) {
    747  1.62       dsl 		printf("%*s        beg: cylinder %4d, head %3d, sector %2d\n",
    748  1.62       dsl 		    indent, "",
    749  1.62       dsl 		    MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect),
    750  1.62       dsl 		    partp->mbrp_shd, MBR_PSECT(partp->mbrp_ssect));
    751  1.62       dsl 		printf("%*s        end: cylinder %4d, head %3d, sector %2d\n",
    752  1.62       dsl 		    indent, "",
    753  1.62       dsl 		    MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect),
    754  1.62       dsl 		    partp->mbrp_ehd, MBR_PSECT(partp->mbrp_esect));
    755  1.62       dsl 	}
    756  1.14      phil 
    757  1.62       dsl 	if (!MBR_IS_EXTENDED(partp->mbrp_typ) ||
    758  1.62       dsl 	    (v_flag <= 2 && !ext.is_corrupt))
    759  1.14      phil 		return;
    760  1.44     lukem 
    761  1.62       dsl 	/*
    762  1.62       dsl 	 * Recursive dump extended table,
    763  1.62       dsl 	 * This is read from the disk - so is wrong during editing.
    764  1.62       dsl 	 * Just ensure we only show it once.
    765  1.62       dsl 	 */
    766  1.62       dsl 	if (dumped)
    767  1.62       dsl 		return;
    768  1.14      phil 
    769  1.62       dsl 	printf("%*s    Extended partition table:\n", indent, "");
    770  1.62       dsl 	indent += 4;
    771  1.62       dsl 	if (read_s0(start, &eboot) == -1)
    772   1.1       cgd 		return;
    773  1.62       dsl 	for (p = 0; p < NMBRPART; p++) {
    774  1.62       dsl 		printf("%*s%d: ", indent, "", p);
    775  1.62       dsl 		print_mbr_partition(&eboot, p, start,
    776  1.62       dsl 				    exoffset ? exoffset : start, indent);
    777   1.1       cgd 	}
    778  1.44     lukem 
    779  1.62       dsl 	if (exoffset == 0)
    780  1.62       dsl 		dumped = 1;
    781   1.1       cgd }
    782   1.1       cgd 
    783  1.34      fvdl int
    784  1.62       dsl read_boot(const char *name, void *buf, size_t len, int err_exit)
    785  1.30        ws {
    786  1.34      fvdl 	int bfd, ret;
    787  1.30        ws 	struct stat st;
    788  1.30        ws 
    789  1.62       dsl 	if (boot_path != NULL)
    790  1.62       dsl 		free(boot_path);
    791  1.62       dsl 	boot_path = malloc(strlen(boot_dir) + 1 + strlen(name) + 1);
    792  1.62       dsl 	if (boot_path == NULL)
    793  1.62       dsl 		err(1, "Malloc failed");
    794  1.62       dsl 	if (strchr(name, '/') == 0) {
    795  1.62       dsl 		strcpy(boot_path, boot_dir);
    796  1.62       dsl 		strcat(boot_path, "/");
    797  1.62       dsl 	} else
    798  1.62       dsl 		boot_path[0] = 0;
    799  1.62       dsl 	strcat(boot_path, name);
    800  1.62       dsl 
    801  1.62       dsl 	if ((bfd = open(boot_path, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
    802  1.62       dsl 		warn("%s", boot_path);
    803  1.62       dsl 		goto fail;
    804  1.62       dsl 	}
    805  1.62       dsl 
    806  1.62       dsl 	if (st.st_size > (off_t)len) {
    807  1.62       dsl 		warnx("%s: bootcode too large", boot_path);
    808  1.62       dsl 		goto fail;
    809  1.62       dsl 	}
    810  1.34      fvdl 	ret = st.st_size;
    811  1.62       dsl 	if (ret < 0x200) {
    812  1.62       dsl 		warnx("%s: bootcode too small", boot_path);
    813  1.62       dsl 		goto fail;
    814  1.62       dsl 	}
    815  1.62       dsl 	if (read(bfd, buf, len) != ret) {
    816  1.62       dsl 		warn("%s", boot_path);
    817  1.62       dsl 		goto fail;
    818  1.62       dsl 	}
    819  1.30        ws 
    820  1.30        ws 	/*
    821  1.30        ws 	 * Do some sanity checking here
    822  1.30        ws 	 */
    823  1.62       dsl 	if (le16toh(bootcode[0].mbr_signature) != MBR_MAGIC) {
    824  1.62       dsl 		warnx("%s: invalid magic", boot_path);
    825  1.62       dsl 		goto fail;
    826  1.62       dsl 	}
    827  1.62       dsl 
    828  1.62       dsl 	close(bfd);
    829  1.62       dsl 	ret = (ret + 0x1ff) & ~0x1ff;
    830  1.34      fvdl 	return ret;
    831  1.62       dsl 
    832  1.62       dsl     fail:
    833  1.62       dsl 	close(bfd);
    834  1.62       dsl 	if (err_exit)
    835  1.62       dsl 		exit(1);
    836  1.62       dsl 	return 0;
    837  1.30        ws }
    838  1.30        ws 
    839  1.30        ws void
    840  1.62       dsl init_sector0(int dopart)
    841   1.1       cgd {
    842  1.15      phil 	int i;
    843  1.62       dsl 	int copy_size =  MBR_PARTOFF;
    844  1.15      phil 
    845  1.62       dsl #ifdef DEFAULT_BOOTCODE
    846  1.62       dsl 	if (bootsize == 0)
    847  1.62       dsl 		bootsize = read_boot(DEFAULT_BOOTCODE, bootcode,
    848  1.62       dsl 			sizeof bootcode, 1);
    849  1.62       dsl #endif
    850  1.62       dsl #ifdef BOOTSEL
    851  1.62       dsl 	if (le16toh(mboot.mbr_bootsel.mbrb_magic) == MBR_MAGIC
    852  1.62       dsl 	    && le16toh(bootcode[0].mbr_bootsel.mbrb_magic) == MBR_MAGIC)
    853  1.62       dsl 		copy_size = MBR_BOOTSELOFF;
    854  1.30        ws #endif
    855  1.30        ws 
    856  1.62       dsl 	if (bootsize != 0) {
    857  1.62       dsl 		boot_installed = 1;
    858  1.62       dsl 		memcpy(&mboot.mbr_bootinst, bootcode, copy_size);
    859  1.62       dsl 	}
    860  1.62       dsl 	mboot.mbr_signature = htole16(MBR_MAGIC);
    861  1.15      phil 
    862  1.62       dsl 	if (!dopart)
    863  1.62       dsl 		return;
    864  1.62       dsl 	for (i = 0; i < 4; i++)
    865  1.62       dsl 		memset(&mboot.mbr_parts[i], 0, sizeof(mboot.mbr_parts[i]));
    866  1.62       dsl }
    867  1.34      fvdl 
    868  1.62       dsl void
    869  1.62       dsl get_extended_ptn(void)
    870  1.62       dsl {
    871  1.62       dsl 	mbr_partition_t *mp;
    872  1.62       dsl 	mbr_sector_t *boot;
    873  1.62       dsl 	daddr_t offset;
    874  1.62       dsl 
    875  1.62       dsl 	/* find first (there should only be one) extended partition */
    876  1.62       dsl 	for (mp = mboot.mbr_parts; !MBR_IS_EXTENDED(mp->mbrp_typ); mp++)
    877  1.62       dsl 		if (mp >= &mboot.mbr_parts[NMBRPART])
    878  1.62       dsl 			return;
    879  1.62       dsl 
    880  1.62       dsl 	/*
    881  1.62       dsl 	 * The extended partition should be structured as a linked list
    882  1.62       dsl 	 * (even though it appears, at first glance, to be a tree).
    883  1.62       dsl 	 */
    884  1.62       dsl 	ext.base = le32toh(mp->mbrp_start);
    885  1.62       dsl 	ext.limit = ext.base + le32toh(mp->mbrp_size);
    886  1.62       dsl 	ext.ptn_id = mp - mboot.mbr_parts;
    887  1.62       dsl 	for (offset = 0;; offset = le32toh(boot->mbr_parts[1].mbrp_start)) {
    888  1.62       dsl 		ext.ptn = realloc(ext.ptn, (ext.num_ptn + 1) * sizeof *ext.ptn);
    889  1.62       dsl 		if (ext.ptn == NULL)
    890  1.62       dsl 			err(1, "Malloc failed");
    891  1.62       dsl 		boot = ext.ptn + ext.num_ptn;
    892  1.62       dsl 		if (read_s0(offset + ext.base, boot) == -1)
    893  1.62       dsl 			break;
    894  1.62       dsl 		/* expect p0 to be valid and p1 to be another extended ptn */
    895  1.62       dsl 		if (MBR_IS_EXTENDED(boot->mbr_parts[0].mbrp_typ))
    896  1.62       dsl 			break;
    897  1.62       dsl 		if (boot->mbr_parts[1].mbrp_typ != 0 &&
    898  1.62       dsl 		    !MBR_IS_EXTENDED(boot->mbr_parts[1].mbrp_typ))
    899  1.62       dsl 			break;
    900  1.62       dsl 		/* p2 and p3 should be unallocated */
    901  1.62       dsl 		if (boot->mbr_parts[2].mbrp_typ != 0 ||
    902  1.62       dsl 		    boot->mbr_parts[3].mbrp_typ != 0)
    903  1.62       dsl 			break;
    904  1.62       dsl 		/* data ptn inside extended one */
    905  1.62       dsl 		if (boot->mbr_parts[0].mbrp_typ != 0 &&
    906  1.62       dsl 		    offset + le32toh(boot->mbr_parts[0].mbrp_start)
    907  1.62       dsl 		    + le32toh(boot->mbr_parts[0].mbrp_size) > ext.limit)
    908  1.62       dsl 			break;
    909  1.62       dsl 
    910  1.62       dsl 		ext.num_ptn++;
    911  1.62       dsl 
    912  1.62       dsl 		if (boot->mbr_parts[1].mbrp_typ == 0)
    913  1.62       dsl 			/* end of extended partition chain */
    914  1.62       dsl 			return;
    915  1.62       dsl 		/* must be in sector order */
    916  1.62       dsl 		if (offset >= le32toh(boot->mbr_parts[1].mbrp_start))
    917  1.62       dsl 			break;
    918  1.62       dsl 	}
    919  1.62       dsl 
    920  1.62       dsl 	warnx("Extended partition table is corrupt\n");
    921  1.62       dsl 	ext.is_corrupt = 1;
    922  1.62       dsl 	ext.num_ptn = 0;
    923  1.34      fvdl }
    924   1.1       cgd 
    925  1.58      fvdl #if defined(__i386__) || defined(__x86_64__)
    926  1.34      fvdl 
    927  1.34      fvdl void
    928  1.44     lukem get_diskname(const char *fullname, char *diskname, size_t size)
    929  1.34      fvdl {
    930  1.44     lukem 	const char *p, *p2;
    931  1.34      fvdl 	size_t len;
    932  1.34      fvdl 
    933  1.34      fvdl 	p = strrchr(fullname, '/');
    934  1.34      fvdl 	if (p == NULL)
    935  1.34      fvdl 		p = fullname;
    936  1.34      fvdl 	else
    937  1.34      fvdl 		p++;
    938  1.34      fvdl 
    939  1.34      fvdl 	if (*p == 0) {
    940  1.62       dsl 		strlcpy(diskname, fullname, size);
    941  1.34      fvdl 		return;
    942  1.34      fvdl 	}
    943  1.34      fvdl 
    944  1.34      fvdl 	if (*p == 'r')
    945  1.34      fvdl 		p++;
    946  1.34      fvdl 
    947  1.34      fvdl 	for (p2 = p; *p2 != 0; p2++)
    948  1.34      fvdl 		if (isdigit(*p2))
    949  1.34      fvdl 			break;
    950  1.34      fvdl 	if (*p2 == 0) {
    951  1.34      fvdl 		/* XXX invalid diskname? */
    952  1.62       dsl 		strlcpy(diskname, fullname, size);
    953  1.34      fvdl 		return;
    954  1.34      fvdl 	}
    955  1.34      fvdl 	while (isdigit(*p2))
    956  1.34      fvdl 		p2++;
    957  1.34      fvdl 
    958  1.34      fvdl 	len = p2 - p;
    959  1.34      fvdl 	if (len > size) {
    960  1.34      fvdl 		/* XXX */
    961  1.62       dsl 		strlcpy(diskname, fullname, size);
    962  1.34      fvdl 		return;
    963  1.34      fvdl 	}
    964  1.34      fvdl 
    965  1.62       dsl 	memcpy(diskname, p, len);
    966  1.34      fvdl 	diskname[len] = 0;
    967  1.34      fvdl }
    968  1.34      fvdl 
    969  1.34      fvdl void
    970  1.43     lukem get_geometry(void)
    971  1.34      fvdl {
    972  1.34      fvdl 	int mib[2], i;
    973  1.34      fvdl 	size_t len;
    974  1.34      fvdl 	struct biosdisk_info *bip;
    975  1.34      fvdl 	struct nativedisk_info *nip;
    976  1.34      fvdl 	char diskname[8];
    977  1.34      fvdl 
    978  1.34      fvdl 	mib[0] = CTL_MACHDEP;
    979  1.34      fvdl 	mib[1] = CPU_DISKINFO;
    980  1.34      fvdl 	if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) {
    981  1.34      fvdl 		intuit_translated_geometry();
    982  1.34      fvdl 		return;
    983  1.34      fvdl 	}
    984  1.34      fvdl 	dl = (struct disklist *) malloc(len);
    985  1.34      fvdl 	sysctl(mib, 2, dl, &len, NULL, 0);
    986  1.34      fvdl 
    987  1.34      fvdl 	get_diskname(disk, diskname, sizeof diskname);
    988  1.34      fvdl 
    989  1.34      fvdl 	for (i = 0; i < dl->dl_nnativedisks; i++) {
    990  1.34      fvdl 		nip = &dl->dl_nativedisks[i];
    991  1.34      fvdl 		if (strcmp(diskname, nip->ni_devname))
    992  1.34      fvdl 			continue;
    993  1.34      fvdl 		/*
    994  1.62       dsl 		 * XXX listing possible matches is better. This is ok for
    995  1.62       dsl 		 * now because the user has a chance to change it later.
    996  1.62       dsl 		 * Also, if all the disks have the same parameters then we can
    997  1.62       dsl 		 * just use them, we don't need to know which disk is which.
    998  1.34      fvdl 		 */
    999  1.34      fvdl 		if (nip->ni_nmatches != 0) {
   1000  1.34      fvdl 			bip = &dl->dl_biosdisks[nip->ni_biosmatches[0]];
   1001  1.34      fvdl 			dos_cylinders = bip->bi_cyl;
   1002  1.34      fvdl 			dos_heads = bip->bi_head;
   1003  1.34      fvdl 			dos_sectors = bip->bi_sec;
   1004  1.62       dsl 			if (bip->bi_lbasecs)
   1005  1.62       dsl 				dos_disksectors = bip->bi_lbasecs;
   1006  1.34      fvdl 			return;
   1007  1.34      fvdl 		}
   1008  1.34      fvdl 	}
   1009  1.34      fvdl 	/* Allright, allright, make a stupid guess.. */
   1010  1.34      fvdl 	intuit_translated_geometry();
   1011  1.11   ghudson }
   1012  1.62       dsl #endif
   1013  1.62       dsl 
   1014  1.62       dsl #ifdef BOOTSEL
   1015  1.62       dsl daddr_t
   1016  1.62       dsl get_default_boot(void)
   1017  1.62       dsl {
   1018  1.62       dsl 	uint id;
   1019  1.62       dsl 	int p;
   1020  1.62       dsl 
   1021  1.62       dsl 	if (le16toh(mboot.mbr_bootsel.mbrb_magic) != MBR_MAGIC)
   1022  1.62       dsl 		/* default to first active partition */
   1023  1.62       dsl 		return DEFAULT_ACTIVE;
   1024  1.62       dsl 
   1025  1.62       dsl 	if (mboot.mbr_bootsel.mbrb_defkey == SCAN_ENTER)
   1026  1.62       dsl 		return DEFAULT_ACTIVE;
   1027  1.62       dsl 
   1028  1.62       dsl 	id = mboot.mbr_bootsel.mbrb_defkey;
   1029  1.62       dsl 	if (!(mboot.mbr_bootsel.mbrb_flags & BFL_NEWMBR)) {
   1030  1.62       dsl 		/* F1..F4 => ptn 0..3, F5+ => disk 0+ */
   1031  1.62       dsl 		id -= SCAN_F1;
   1032  1.62       dsl 		if (id >= NMBRPART)
   1033  1.62       dsl 			/* Return number of disk */
   1034  1.62       dsl 			return id - NMBRPART;
   1035  1.62       dsl 		if (mboot.mbr_parts[id].mbrp_typ != 0)
   1036  1.62       dsl 			return le32toh(mboot.mbr_parts[id].mbrp_start);
   1037  1.62       dsl 		return DEFAULT_ACTIVE;
   1038  1.62       dsl 	}
   1039  1.62       dsl 
   1040  1.62       dsl 	/* 1+ => allocated partition id, F1+ => disk 0+ */
   1041  1.62       dsl 	if (id >= SCAN_F1)
   1042  1.62       dsl 		return id - SCAN_F1;
   1043  1.62       dsl 	id -= SCAN_1;
   1044  1.62       dsl 
   1045  1.62       dsl 	for (p = 0; p < NMBRPART; p++) {
   1046  1.62       dsl 		if (mboot.mbr_parts[p].mbrp_typ == 0)
   1047  1.62       dsl 			continue;
   1048  1.62       dsl 		if (mboot.mbr_bootsel.mbrb_nametab[p][0] == 0)
   1049  1.62       dsl 			continue;
   1050  1.62       dsl 		if (id-- == 0)
   1051  1.62       dsl 			return le32toh(mboot.mbr_parts[p].mbrp_start);
   1052  1.62       dsl 	}
   1053  1.62       dsl 
   1054  1.62       dsl 	for (p = 0; p < ext.num_ptn; p++) {
   1055  1.62       dsl 		if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
   1056  1.62       dsl 			continue;
   1057  1.62       dsl 		if (ext.ptn[p].mbr_bootsel.mbrb_nametab[0][0] == 0)
   1058  1.62       dsl 			continue;
   1059  1.62       dsl 		if (id-- == 0)
   1060  1.62       dsl 			return ext_offset(p)
   1061  1.62       dsl 				+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_start);
   1062  1.62       dsl 	}
   1063  1.62       dsl 
   1064  1.62       dsl 	return DEFAULT_ACTIVE;
   1065  1.62       dsl }
   1066  1.11   ghudson 
   1067  1.34      fvdl void
   1068  1.62       dsl set_default_boot(daddr_t default_ptn)
   1069  1.34      fvdl {
   1070  1.62       dsl 	int p;
   1071  1.62       dsl 	int key = SCAN_1;
   1072  1.34      fvdl 
   1073  1.62       dsl 	if (le16toh(mboot.mbr_bootsel.mbrb_magic) != MBR_MAGIC)
   1074  1.62       dsl 		/* sanity */
   1075  1.62       dsl 		return;
   1076  1.34      fvdl 
   1077  1.62       dsl 	if (default_ptn == DEFAULT_ACTIVE) {
   1078  1.62       dsl 		mboot.mbr_bootsel.mbrb_defkey = SCAN_ENTER;
   1079  1.34      fvdl 		return;
   1080  1.34      fvdl 	}
   1081  1.34      fvdl 
   1082  1.62       dsl 	for (p = 0; p < NMBRPART; p++) {
   1083  1.62       dsl 		if (mboot.mbr_parts[p].mbrp_typ == 0)
   1084  1.62       dsl 			continue;
   1085  1.62       dsl 		if (mboot.mbr_bootsel.mbrb_nametab[p][0] == 0)
   1086  1.62       dsl 			continue;
   1087  1.62       dsl 		if (le32toh(mboot.mbr_parts[p].mbrp_start) == default_ptn) {
   1088  1.62       dsl 			if (mboot.mbr_bootsel.mbrb_flags & BFL_NEWMBR)
   1089  1.62       dsl 				mboot.mbr_bootsel.mbrb_defkey = key;
   1090  1.62       dsl 			else
   1091  1.62       dsl 				mboot.mbr_bootsel.mbrb_defkey = SCAN_F1 + p;
   1092  1.34      fvdl 			return;
   1093  1.34      fvdl 		}
   1094  1.62       dsl 		key++;
   1095  1.62       dsl 	}
   1096  1.62       dsl 
   1097  1.62       dsl 	if (mboot.mbr_bootsel.mbrb_flags & BFL_EXTLBA) {
   1098  1.62       dsl 		for (p = 0; p < ext.num_ptn; p++) {
   1099  1.62       dsl 			if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
   1100  1.62       dsl 				continue;
   1101  1.62       dsl 			if (ext.ptn[p].mbr_bootsel.mbrb_nametab[0][0] == 0)
   1102  1.62       dsl 				continue;
   1103  1.62       dsl 			if (le32toh(ext.ptn[p].mbr_parts[0].mbrp_start) +
   1104  1.62       dsl 			    ext_offset(p) == default_ptn) {
   1105  1.62       dsl 				mboot.mbr_bootsel.mbrb_defkey = key;
   1106  1.34      fvdl 				return;
   1107  1.34      fvdl 			}
   1108  1.62       dsl 			key++;
   1109  1.34      fvdl 		}
   1110  1.34      fvdl 	}
   1111  1.34      fvdl 
   1112  1.62       dsl 	if (default_ptn < 8) {
   1113  1.62       dsl 		if (mboot.mbr_bootsel.mbrb_flags & BFL_NEWMBR)
   1114  1.62       dsl 			key = SCAN_F1;
   1115  1.34      fvdl 		else
   1116  1.62       dsl 			key = SCAN_F1 + 4;
   1117  1.62       dsl 		mboot.mbr_bootsel.mbrb_defkey = key + default_ptn;
   1118  1.62       dsl 		return;
   1119  1.34      fvdl 	}
   1120  1.34      fvdl 
   1121  1.62       dsl 	/* Default to first active partition */
   1122  1.62       dsl 	mboot.mbr_bootsel.mbrb_defkey = SCAN_ENTER;
   1123  1.62       dsl }
   1124  1.34      fvdl 
   1125  1.62       dsl void
   1126  1.62       dsl install_bootsel(int needed)
   1127  1.62       dsl {
   1128  1.62       dsl 	struct mbr_bootsel *mbs = &mboot.mbr_bootsel;
   1129  1.62       dsl 	int p;
   1130  1.62       dsl 	int ext13 = 0;
   1131  1.62       dsl 	char *code;
   1132  1.62       dsl 
   1133  1.62       dsl 	/* Work out which boot code we need for this configuration */
   1134  1.62       dsl 	for (p = 0; p < NMBRPART; p++) {
   1135  1.62       dsl 		if (mboot.mbr_parts[p].mbrp_typ == 0)
   1136  1.62       dsl 			continue;
   1137  1.62       dsl 		if (le16toh(mbs->mbrb_magic) != MBR_MAGIC)
   1138  1.34      fvdl 			break;
   1139  1.62       dsl 		if (mbs->mbrb_nametab[p][0] == 0)
   1140  1.62       dsl 			continue;
   1141  1.62       dsl 		needed |= BFL_SELACTIVE;
   1142  1.62       dsl 		if (le32toh(mboot.mbr_parts[p].mbrp_start) >= dos_totalsectors)
   1143  1.62       dsl 			ext13 = BFL_EXTINT13;
   1144  1.62       dsl 	}
   1145  1.62       dsl 
   1146  1.62       dsl 	for (p = 0; p < ext.num_ptn; p++) {
   1147  1.62       dsl 		if (le16toh(ext.ptn[p].mbr_bootsel.mbrb_magic) != MBR_MAGIC)
   1148  1.34      fvdl 			continue;
   1149  1.62       dsl 		if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
   1150  1.62       dsl 			continue;
   1151  1.62       dsl 		if (ext.ptn[p].mbr_bootsel.mbrb_nametab[p][0] == 0)
   1152  1.34      fvdl 			continue;
   1153  1.62       dsl 		needed |= BFL_EXTLBA | BFL_SELACTIVE;
   1154  1.62       dsl 	}
   1155  1.34      fvdl 
   1156  1.62       dsl 	if (B_flag)
   1157  1.62       dsl 		needed |= BFL_SELACTIVE;
   1158  1.38     soren 
   1159  1.62       dsl 	/* Is the installed code good enough ? */
   1160  1.62       dsl 	if (!i_flag && (needed == 0 || (le16toh(mbs->mbrb_magic) == MBR_MAGIC
   1161  1.62       dsl 	    && (mbs->mbrb_flags & needed) == needed))) {
   1162  1.62       dsl 		/* yes - just set flags */
   1163  1.62       dsl 		mbs->mbrb_flags |= ext13;
   1164  1.62       dsl 		return;
   1165  1.34      fvdl 	}
   1166  1.34      fvdl 
   1167  1.62       dsl 	/* ok - we need to replace the bootcode */
   1168  1.34      fvdl 
   1169  1.62       dsl 	if (f_flag && !(i_flag || B_flag)) {
   1170  1.62       dsl 		warnx("Installed bootfile doesn't support required options.");
   1171  1.62       dsl 		return;
   1172  1.62       dsl 	}
   1173  1.62       dsl 
   1174  1.62       dsl 	if (!f_flag && bootsize == 0)
   1175  1.62       dsl 		/* Output an explanation for the 'update bootcode' prompt. */
   1176  1.62       dsl 		printf("Installed bootfile doesn't support required options.\n");
   1177  1.62       dsl 
   1178  1.62       dsl 	/* Were we told a specific file ? (which we have already read) */
   1179  1.62       dsl 	/* If so check that it supports what we need. */
   1180  1.62       dsl 	if (bootsize != 0 && needed != 0
   1181  1.62       dsl 	    && (le16toh(bootcode[0].mbr_bootsel.mbrb_magic) != MBR_MAGIC
   1182  1.62       dsl 	    || ((bootcode[0].mbr_bootsel.mbrb_flags & needed) != needed))) {
   1183  1.62       dsl 		/* No it doesn't... */
   1184  1.62       dsl 		if (f_flag)
   1185  1.62       dsl 			warnx("Bootfile %s doesn't support "
   1186  1.62       dsl 				    "required bootsel options", boot_path );
   1187  1.62       dsl 			/* But install it anyway */
   1188  1.62       dsl 		else
   1189  1.62       dsl 			if (yesno("Bootfile %s doesn't support the required "
   1190  1.62       dsl 			    "options,\ninstall default bootfile instead?",
   1191  1.62       dsl 			    boot_path))
   1192  1.62       dsl 				bootsize = 0;
   1193  1.34      fvdl 	}
   1194  1.34      fvdl 
   1195  1.62       dsl 	if (bootsize == 0) {
   1196  1.62       dsl 		/* Get name of bootfile that supports the required facilities */
   1197  1.62       dsl 		code = DEFAULT_BOOTCODE;
   1198  1.62       dsl 		if (needed & BFL_SELACTIVE)
   1199  1.62       dsl 			code = DEFAULT_BOOTSELCODE;
   1200  1.62       dsl #ifdef DEFAULT_BOOTEXTCODE
   1201  1.62       dsl 		if (needed & BFL_EXTLBA)
   1202  1.62       dsl 			code = DEFAULT_BOOTEXTCODE;
   1203  1.62       dsl #endif
   1204  1.62       dsl 
   1205  1.62       dsl 		bootsize = read_boot(code, bootcode, sizeof bootcode, 0);
   1206  1.62       dsl 		if (bootsize == 0)
   1207  1.62       dsl 			/* The old bootcode is better than no bootcode at all */
   1208  1.62       dsl 			return;
   1209  1.62       dsl 		if ((bootcode[0].mbr_bootsel.mbrb_flags & needed) != needed)
   1210  1.62       dsl 			warnx("Default bootfile %s doesn't support required "
   1211  1.62       dsl 				"options.  Got flags 0x%x, wanted 0x%x\n",
   1212  1.62       dsl 				boot_path, bootcode[0].mbr_bootsel.mbrb_flags,
   1213  1.62       dsl 				needed);
   1214  1.34      fvdl 	}
   1215  1.34      fvdl 
   1216  1.62       dsl 	if (!f_flag && !yesno("Update the bootcode from %s?", boot_path))
   1217  1.62       dsl 		return;
   1218  1.37   thorpej 
   1219  1.62       dsl 	init_sector0(0);
   1220  1.34      fvdl 
   1221  1.62       dsl 	if (le16toh(mbs->mbrb_magic) == MBR_MAGIC)
   1222  1.62       dsl 		mbs->mbrb_flags = bootcode[0].mbr_bootsel.mbrb_flags | ext13;
   1223  1.62       dsl }
   1224  1.34      fvdl 
   1225  1.62       dsl daddr_t
   1226  1.62       dsl configure_bootsel(daddr_t default_ptn)
   1227  1.62       dsl {
   1228  1.62       dsl 	struct mbr_bootsel *mbs = &mboot.mbr_bootsel;
   1229  1.62       dsl 	int i, item, opt;
   1230  1.62       dsl 	int tmo;
   1231  1.62       dsl 	daddr_t *off;
   1232  1.62       dsl 	int num_bios_disks;
   1233  1.34      fvdl 
   1234  1.62       dsl 	if (dl != NULL) {
   1235  1.62       dsl 		num_bios_disks = dl->dl_nbiosdisks;
   1236  1.62       dsl 		if (num_bios_disks > 8)
   1237  1.62       dsl 			num_bios_disks = 8;
   1238  1.62       dsl 	} else
   1239  1.62       dsl 		num_bios_disks = 8;
   1240  1.34      fvdl 
   1241  1.62       dsl 	printf("\nBoot selector configuration:\n");
   1242  1.34      fvdl 
   1243  1.62       dsl 	/* The timeout value is in ticks, ~18.2 Hz. Avoid using floats.
   1244  1.62       dsl 	 * Ticks are nearly 64k/3600 - so our long timers are sligtly out!
   1245  1.62       dsl 	 * Newer bootcode always waits for 1 tick, so treats 0xffff
   1246  1.62       dsl 	 * as wait forever.
   1247  1.62       dsl 	 */
   1248  1.62       dsl 	tmo = le16toh(mbs->mbrb_timeo);
   1249  1.62       dsl 	tmo = tmo == 0xffff ? -1 : (10 * tmo + 9) / 182;
   1250  1.62       dsl 	tmo = decimal("Timeout value (0 to 3600 seconds, -1 => never)",
   1251  1.62       dsl 			tmo, 0, -1, 3600);
   1252  1.62       dsl 	mbs->mbrb_timeo = htole16(tmo == -1 ? 0xffff : (tmo * 182) / 10);
   1253  1.62       dsl 
   1254  1.62       dsl 	off = calloc(1 + NMBRPART + ext.num_ptn + num_bios_disks, sizeof *off);
   1255  1.62       dsl 	if (off == NULL)
   1256  1.62       dsl 		err(1, "Malloc failed");
   1257  1.34      fvdl 
   1258  1.62       dsl 	printf("Select the default boot option. Options are:\n\n");
   1259  1.62       dsl 	item = 0;
   1260  1.62       dsl 	opt = 0;
   1261  1.62       dsl 	off[opt] = DEFAULT_ACTIVE;
   1262  1.62       dsl 	printf("%d: The first active partition\n", opt);
   1263  1.35      fvdl 	for (i = 0; i < NMBRPART; i++) {
   1264  1.62       dsl 		if (mboot.mbr_parts[i].mbrp_typ == 0)
   1265  1.62       dsl 			continue;
   1266  1.62       dsl 		if (mbs->mbrb_nametab[i][0] == 0)
   1267  1.62       dsl 			continue;
   1268  1.62       dsl 		printf("%d: %s\n", ++opt, &mbs->mbrb_nametab[i][0]);
   1269  1.62       dsl 		off[opt] = le32toh(mboot.mbr_parts[i].mbrp_start);
   1270  1.62       dsl 		if (off[opt] == default_ptn)
   1271  1.62       dsl 			item = opt;
   1272  1.62       dsl 	}
   1273  1.62       dsl 	if (mbs->mbrb_flags & BFL_EXTLBA) {
   1274  1.62       dsl 		for (i = 0; i < ext.num_ptn; i++) {
   1275  1.62       dsl 			if (ext.ptn[i].mbr_parts[0].mbrp_typ == 0)
   1276  1.62       dsl 				continue;
   1277  1.62       dsl 			if (ext.ptn[i].mbr_bootsel.mbrb_nametab[0][0] == 0)
   1278  1.62       dsl 				continue;
   1279  1.62       dsl 			printf("%d: %s\n",
   1280  1.62       dsl 			    ++opt, ext.ptn[i].mbr_bootsel.mbrb_nametab[0]);
   1281  1.62       dsl 			off[opt] = ext_offset(i) +
   1282  1.62       dsl 			    le32toh(ext.ptn[i].mbr_parts[0].mbrp_start);
   1283  1.62       dsl 			if (off[opt] == default_ptn)
   1284  1.62       dsl 				item = opt;
   1285  1.35      fvdl 		}
   1286  1.35      fvdl 	}
   1287  1.62       dsl 	for (i = 0; i < num_bios_disks; i++) {
   1288  1.62       dsl 		printf("%d: Harddisk %d\n", ++opt, i);
   1289  1.62       dsl 		off[opt] = i;
   1290  1.62       dsl 		if (i == default_ptn)
   1291  1.62       dsl 			item = opt;
   1292  1.62       dsl 	}
   1293  1.35      fvdl 
   1294  1.62       dsl 	item = decimal("Default boot option", item, 0, 0, opt);
   1295  1.62       dsl 
   1296  1.62       dsl 	default_ptn = off[item];
   1297  1.62       dsl 	free(off);
   1298  1.62       dsl 	return default_ptn;
   1299  1.34      fvdl }
   1300  1.34      fvdl #endif
   1301  1.34      fvdl 
   1302  1.34      fvdl 
   1303  1.11   ghudson /* Prerequisite: the disklabel parameters and master boot record must
   1304  1.11   ghudson  *		 have been read (i.e. dos_* and mboot are meaningful).
   1305  1.11   ghudson  * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and
   1306  1.11   ghudson  *		  dos_cylindersectors to be consistent with what the
   1307  1.11   ghudson  *		  partition table is using, if we can find a geometry
   1308  1.11   ghudson  *		  which is consistent with all partition table entries.
   1309  1.11   ghudson  *		  We may get the number of cylinders slightly wrong (in
   1310  1.11   ghudson  *		  the conservative direction).  The idea is to be able
   1311  1.11   ghudson  *		  to create a NetBSD partition on a disk we don't know
   1312  1.11   ghudson  *		  the translated geometry of.
   1313  1.62       dsl  * This routine is only used for non-x86 systems or when we fail to
   1314  1.62       dsl  * get the BIOS geometry from the kernel.
   1315  1.62       dsl  */
   1316  1.11   ghudson void
   1317  1.43     lukem intuit_translated_geometry(void)
   1318  1.11   ghudson {
   1319  1.62       dsl 	int xcylinders = -1, xheads = -1, xsectors = -1, i, j;
   1320  1.62       dsl 	uint c1, h1, s1, c2, h2, s2;
   1321  1.62       dsl 	ulong a1, a2;
   1322  1.62       dsl 	uint64_t num, denom;
   1323  1.62       dsl 
   1324  1.62       dsl 	/*
   1325  1.62       dsl 	 * The physical parameters may be invalid as bios geometry.
   1326  1.62       dsl 	 * If we cannot determine the actual bios geometry, we are
   1327  1.62       dsl 	 * better off picking a likely 'faked' geometry than leaving
   1328  1.62       dsl 	 * the invalid physical one.
   1329  1.62       dsl 	 */
   1330  1.15      phil 
   1331  1.62       dsl 	if (dos_cylinders > MAXCYL || dos_heads > MAXHEAD ||
   1332  1.62       dsl 	    dos_sectors > MAXSECTOR) {
   1333  1.62       dsl 		h1 = MAXHEAD - 1;
   1334  1.62       dsl 		c1 = MAXCYL - 1;
   1335  1.62       dsl #if defined(__i386__) || defined(__x86_64__)
   1336  1.62       dsl 		if (dl != NULL) {
   1337  1.62       dsl 			/* BIOS may use 256 heads or 1024 cylinders */
   1338  1.62       dsl 			for (i = 0; i < dl->dl_nbiosdisks; i++) {
   1339  1.62       dsl 				if (h1 < dl->dl_biosdisks[i].bi_head)
   1340  1.62       dsl 					h1 = dl->dl_biosdisks[i].bi_head;
   1341  1.62       dsl 				if (c1 < dl->dl_biosdisks[i].bi_cyl)
   1342  1.62       dsl 					c1 = dl->dl_biosdisks[i].bi_cyl;
   1343  1.62       dsl 			}
   1344  1.62       dsl 		}
   1345  1.62       dsl #endif
   1346  1.62       dsl 		dos_sectors = MAXSECTOR;
   1347  1.62       dsl 		dos_heads = h1;
   1348  1.62       dsl 		dos_cylinders = disklabel.d_secperunit / (MAXSECTOR * h1);
   1349  1.62       dsl 		if (dos_cylinders > c1)
   1350  1.62       dsl 			dos_cylinders = c1;
   1351  1.62       dsl 	}
   1352  1.11   ghudson 
   1353  1.11   ghudson 	/* Try to deduce the number of heads from two different mappings. */
   1354  1.32   thorpej 	for (i = 0; i < NMBRPART * 2; i++) {
   1355  1.11   ghudson 		if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
   1356  1.11   ghudson 			continue;
   1357  1.11   ghudson 		for (j = 0; j < 8; j++) {
   1358  1.11   ghudson 			if (get_mapping(j, &c2, &h2, &s2, &a2) < 0)
   1359  1.11   ghudson 				continue;
   1360  1.62       dsl 			a1 -= s1;
   1361  1.62       dsl 			a2 -= s2;
   1362  1.62       dsl 			num = (uint64_t)h1 * a2 - (uint64_t)h2 * a1;
   1363  1.62       dsl 			denom = (uint64_t)c2 * a1 - (uint64_t)c1 * a2;
   1364  1.11   ghudson 			if (denom != 0 && num % denom == 0) {
   1365  1.44     lukem 				xheads = num / denom;
   1366  1.62       dsl 				xsectors = a1 / (c1 * xheads + h1);
   1367  1.11   ghudson 				break;
   1368  1.11   ghudson 			}
   1369  1.11   ghudson 		}
   1370  1.44     lukem 		if (xheads != -1)
   1371  1.11   ghudson 			break;
   1372  1.11   ghudson 	}
   1373  1.11   ghudson 
   1374  1.44     lukem 	if (xheads == -1)
   1375  1.11   ghudson 		return;
   1376  1.11   ghudson 
   1377  1.11   ghudson 	/* Estimate the number of cylinders. */
   1378  1.44     lukem 	xcylinders = disklabel.d_secperunit / xheads / xsectors;
   1379  1.11   ghudson 
   1380  1.62       dsl 	/*
   1381  1.62       dsl 	 * Now verify consistency with each of the partition table entries.
   1382  1.11   ghudson 	 * Be willing to shove cylinders up a little bit to make things work,
   1383  1.62       dsl 	 * but translation mismatches are fatal.
   1384  1.62       dsl 	 */
   1385  1.32   thorpej 	for (i = 0; i < NMBRPART * 2; i++) {
   1386  1.11   ghudson 		if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
   1387  1.11   ghudson 			continue;
   1388  1.44     lukem 		if (xsectors * (c1 * xheads + h1) + s1 != a1)
   1389  1.11   ghudson 			return;
   1390  1.44     lukem 		if (c1 >= xcylinders)
   1391  1.44     lukem 			xcylinders = c1 + 1;
   1392  1.11   ghudson 	}
   1393  1.11   ghudson 
   1394  1.62       dsl 
   1395  1.62       dsl 	/* Everything checks out.
   1396  1.62       dsl 	 * Reset the geometry to use for further calculations.
   1397  1.62       dsl 	 * But cylinders cannot be > 1024, we set the max at the top.
   1398  1.62       dsl 	 */
   1399  1.62       dsl 	if (dos_cylinders > xcylinders)
   1400  1.62       dsl 		dos_cylinders = xcylinders;
   1401  1.44     lukem 	dos_heads = xheads;
   1402  1.44     lukem 	dos_sectors = xsectors;
   1403  1.11   ghudson }
   1404  1.11   ghudson 
   1405  1.62       dsl /*
   1406  1.62       dsl  * For the purposes of intuit_translated_geometry(), treat the partition
   1407  1.11   ghudson  * table as a list of eight mapping between (cylinder, head, sector)
   1408  1.11   ghudson  * triplets and absolute sectors.  Get the relevant geometry triplet and
   1409  1.11   ghudson  * absolute sectors for a given entry, or return -1 if it isn't present.
   1410  1.62       dsl  * Note: for simplicity, the returned sector is 0-based.
   1411  1.62       dsl  */
   1412  1.11   ghudson int
   1413  1.62       dsl get_mapping(int i, uint *cylinder, uint *head, uint *sector,
   1414  1.43     lukem     unsigned long *absolute)
   1415  1.11   ghudson {
   1416  1.62       dsl 	mbr_partition_t *part = &mboot.mbr_parts[i / 2];
   1417  1.11   ghudson 
   1418  1.32   thorpej 	if (part->mbrp_typ == 0)
   1419  1.11   ghudson 		return -1;
   1420  1.11   ghudson 	if (i % 2 == 0) {
   1421  1.32   thorpej 		*cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect);
   1422  1.32   thorpej 		*head = part->mbrp_shd;
   1423  1.32   thorpej 		*sector = MBR_PSECT(part->mbrp_ssect) - 1;
   1424  1.62       dsl 		*absolute = le32toh(part->mbrp_start);
   1425  1.11   ghudson 	} else {
   1426  1.32   thorpej 		*cylinder = MBR_PCYL(part->mbrp_ecyl, part->mbrp_esect);
   1427  1.32   thorpej 		*head = part->mbrp_ehd;
   1428  1.32   thorpej 		*sector = MBR_PSECT(part->mbrp_esect) - 1;
   1429  1.62       dsl 		*absolute = le32toh(part->mbrp_start)
   1430  1.62       dsl 		    + le32toh(part->mbrp_size) - 1;
   1431  1.11   ghudson 	}
   1432  1.62       dsl 	/* Sanity check the data against max values */
   1433  1.62       dsl 	if ((((*cylinder * MAXHEAD) + *head) * MAXSECTOR + *sector) < *absolute)
   1434  1.62       dsl 		/* cannot be a CHS mapping */
   1435  1.62       dsl 		return -1;
   1436  1.11   ghudson 	return 0;
   1437   1.1       cgd }
   1438   1.1       cgd 
   1439  1.62       dsl static void
   1440  1.62       dsl delete_ptn(int part)
   1441  1.62       dsl {
   1442  1.62       dsl 	if (part == ext.ptn_id) {
   1443  1.62       dsl 		/* forget all about the extended partition */
   1444  1.62       dsl 		free(ext.ptn);
   1445  1.62       dsl 		memset(&ext, 0, sizeof ext);
   1446  1.62       dsl 	}
   1447  1.62       dsl 
   1448  1.62       dsl 	mboot.mbr_parts[part].mbrp_typ = 0;
   1449  1.62       dsl }
   1450  1.62       dsl 
   1451  1.62       dsl static void
   1452  1.62       dsl delete_ext_ptn(int part)
   1453   1.1       cgd {
   1454   1.1       cgd 
   1455  1.62       dsl 	if (part == 0) {
   1456  1.62       dsl 		ext.ptn[0].mbr_parts[0].mbrp_typ = 0;
   1457  1.62       dsl 		return;
   1458  1.62       dsl 	}
   1459  1.62       dsl 	ext.ptn[part - 1].mbr_parts[1] = ext.ptn[part].mbr_parts[1];
   1460  1.62       dsl 	memmove(&ext.ptn[part], &ext.ptn[part + 1],
   1461  1.62       dsl 		(ext.num_ptn - part - 1) * sizeof ext.ptn[0]);
   1462  1.62       dsl 	ext.num_ptn--;
   1463  1.62       dsl }
   1464   1.1       cgd 
   1465  1.62       dsl static int
   1466  1.62       dsl add_ext_ptn(daddr_t start, daddr_t size)
   1467  1.62       dsl {
   1468  1.62       dsl 	int part;
   1469  1.62       dsl 	mbr_partition_t *partp;
   1470  1.62       dsl 
   1471  1.62       dsl 	ext.ptn = realloc(ext.ptn, (ext.num_ptn + 1) * sizeof *ext.ptn);
   1472  1.62       dsl 	for (part = 0; part < ext.num_ptn; part++)
   1473  1.62       dsl 		if (ext_offset(part) > start)
   1474  1.62       dsl 			break;
   1475  1.62       dsl 	/* insert before 'part' - make space... */
   1476  1.62       dsl 	memmove(&ext.ptn[part + 1], &ext.ptn[part],
   1477  1.62       dsl 		(ext.num_ptn - part) * sizeof ext.ptn[0]);
   1478  1.62       dsl 	memset(&ext.ptn[part], 0, sizeof ext.ptn[0]);
   1479  1.62       dsl 	ext.ptn[part].mbr_signature = htole16(MBR_MAGIC);
   1480  1.62       dsl 	/* we will be 'part' */
   1481  1.62       dsl 	if (part == 0) {
   1482  1.62       dsl 		/* link us to 'next' */
   1483  1.62       dsl 		partp = &ext.ptn[0].mbr_parts[1];
   1484  1.62       dsl 		/* offset will be fixed by caller */
   1485  1.62       dsl 		partp->mbrp_size = htole32(
   1486  1.62       dsl 		    le32toh(ext.ptn[1].mbr_parts[0].mbrp_start) +
   1487  1.62       dsl 		    le32toh(ext.ptn[1].mbr_parts[0].mbrp_size));
   1488  1.62       dsl 	} else {
   1489  1.62       dsl 		/* link us to prev's next */
   1490  1.62       dsl 		partp = &ext.ptn[part - 1].mbr_parts[1];
   1491  1.62       dsl 		ext.ptn[part].mbr_parts[1] = *partp;
   1492  1.62       dsl 		/* and prev onto us */
   1493  1.62       dsl 		partp->mbrp_start = htole32(start - dos_sectors - ext.base);
   1494  1.62       dsl 		partp->mbrp_size = htole32(size + dos_sectors);
   1495  1.62       dsl 	}
   1496  1.62       dsl 	partp->mbrp_typ = 5;	/* as used by win98 */
   1497  1.62       dsl 	partp->mbrp_flag = 0;
   1498  1.62       dsl 	/* wallop in some CHS values - win98 doesn't saturate them */
   1499  1.62       dsl 	dos(le32toh(partp->mbrp_start),
   1500  1.62       dsl 	    &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
   1501  1.62       dsl 	dos(le32toh(partp->mbrp_start) + le32toh(partp->mbrp_size) - 1,
   1502  1.62       dsl 	    &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
   1503  1.62       dsl 	ext.num_ptn++;
   1504  1.62       dsl 
   1505  1.62       dsl 	return part;
   1506  1.62       dsl }
   1507  1.62       dsl 
   1508  1.62       dsl static const char *
   1509  1.62       dsl check_overlap(int part, int sysid, daddr_t start, daddr_t size, int fix)
   1510  1.62       dsl {
   1511  1.62       dsl 	int p;
   1512  1.62       dsl 	uint p_s, p_e;
   1513  1.62       dsl 
   1514  1.62       dsl 	if (sysid != 0) {
   1515  1.62       dsl 		if (start < dos_sectors)
   1516  1.62       dsl 			return "Track zero is reserved for the BIOS";
   1517  1.62       dsl 		if (start + size > dos_disksectors)
   1518  1.62       dsl 			return "Partition exceeds size of disk";
   1519  1.62       dsl 		for (p = 0; p < NMBRPART; p++) {
   1520  1.62       dsl 			if (p == part || mboot.mbr_parts[p].mbrp_typ == 0)
   1521  1.62       dsl 				continue;
   1522  1.62       dsl 			p_s = le32toh(mboot.mbr_parts[p].mbrp_start);
   1523  1.62       dsl 			p_e = p_s + le32toh(mboot.mbr_parts[p].mbrp_size);
   1524  1.62       dsl 			if (start + size <= p_s || start >= p_e)
   1525  1.62       dsl 				continue;
   1526  1.62       dsl 			if (f_flag) {
   1527  1.62       dsl 				if (fix)
   1528  1.62       dsl 					delete_ptn(p);
   1529  1.62       dsl 				return 0;
   1530  1.62       dsl 			}
   1531  1.62       dsl 			return "Overlaps another partition";
   1532  1.62       dsl 		}
   1533  1.62       dsl 	}
   1534  1.62       dsl 
   1535  1.62       dsl 	/* Are we trying to create an extended partition */
   1536  1.62       dsl 	if (!MBR_IS_EXTENDED(mboot.mbr_parts[part].mbrp_typ)) {
   1537  1.62       dsl 		/* this wasn't the extended partition */
   1538  1.62       dsl 		if (!MBR_IS_EXTENDED(sysid))
   1539  1.62       dsl 			return 0;
   1540  1.62       dsl 		/* making an extended partition */
   1541  1.62       dsl 		if (ext.base != 0) {
   1542  1.62       dsl 			if (!f_flag)
   1543  1.62       dsl 				return "There cannot be 2 extended partitions";
   1544  1.62       dsl 			if (fix)
   1545  1.62       dsl 				delete_ptn(ext.ptn_id);
   1546  1.62       dsl 		}
   1547  1.62       dsl 		if (fix) {
   1548  1.62       dsl 			/* allocate a new extended partition */
   1549  1.62       dsl 			ext.ptn = calloc(1, sizeof ext.ptn[0]);
   1550  1.62       dsl 			if (ext.ptn == NULL)
   1551  1.62       dsl 				err(1, "Malloc failed");
   1552  1.62       dsl 			ext.ptn[0].mbr_signature = htole16(MBR_MAGIC);
   1553  1.62       dsl 			ext.ptn_id = part;
   1554  1.62       dsl 			ext.base = start;
   1555  1.62       dsl 			ext.limit = start + size;
   1556  1.62       dsl 			ext.num_ptn = 1;
   1557  1.62       dsl 		}
   1558  1.62       dsl 		return 0;
   1559  1.62       dsl 	}
   1560  1.62       dsl 
   1561  1.62       dsl 	/* Check we haven't cut space allocated to an extended ptn */
   1562  1.62       dsl 
   1563  1.62       dsl 	if (!MBR_IS_EXTENDED(sysid)) {
   1564  1.62       dsl 		/* no longer an extended partition */
   1565  1.62       dsl 		if (fix) {
   1566  1.62       dsl 			/* Kill all memory of the extended partitions */
   1567  1.62       dsl 			delete_ptn(part);
   1568  1.62       dsl 			return 0;
   1569  1.29        ws 		}
   1570  1.62       dsl 		if (ext.num_ptn == 0 ||
   1571  1.62       dsl 		    (ext.num_ptn == 1 && ext.ptn[0].mbr_parts[0].mbrp_typ == 0))
   1572  1.62       dsl 			/* nothing in extended partition */
   1573  1.62       dsl 			return 0;
   1574  1.15      phil 		if (f_flag)
   1575  1.62       dsl 			return 0;
   1576  1.62       dsl 		if (yesno("Do you really want to delete all the extended partitions?"))
   1577  1.62       dsl 			return 0;
   1578  1.62       dsl 		return "Extended partition busy";
   1579  1.62       dsl 	}
   1580  1.62       dsl 
   1581  1.62       dsl 	if (le32toh(mboot.mbr_parts[part].mbrp_start) != ext.base)
   1582  1.62       dsl 		/* maybe impossible, but an extra sanity check */
   1583  1.62       dsl 		return 0;
   1584  1.62       dsl 
   1585  1.62       dsl 	for (p = ext.num_ptn; --p >= 0;) {
   1586  1.62       dsl 		if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
   1587  1.62       dsl 			continue;
   1588  1.62       dsl 		p_s = ext_offset(p);
   1589  1.62       dsl 		p_e = p_s + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
   1590  1.62       dsl 			  + le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
   1591  1.62       dsl 		if (p_s >= start && p_e <= start + size)
   1592  1.62       dsl 			continue;
   1593  1.62       dsl 		if (!f_flag)
   1594  1.62       dsl 			return "Extended partition outside main partition";
   1595  1.62       dsl 		if (fix)
   1596  1.62       dsl 			delete_ext_ptn(p);
   1597  1.62       dsl 	}
   1598  1.62       dsl 
   1599  1.62       dsl 	if (fix && start != ext.base) {
   1600  1.62       dsl 		/* The internal offsets need to be fixed up */
   1601  1.62       dsl 		for (p = 0; p < ext.num_ptn - 1; p++)
   1602  1.62       dsl 			ext.ptn[p].mbr_parts[1].mbrp_start = htole32(
   1603  1.62       dsl 			    le32toh(ext.ptn[p].mbr_parts[1].mbrp_start)
   1604  1.62       dsl 				    + ext.base - start);
   1605  1.62       dsl 		/* and maybe an empty partition at the start */
   1606  1.62       dsl 		if (ext.ptn[0].mbr_parts[0].mbrp_typ == 0) {
   1607  1.62       dsl 			if (le32toh(ext.ptn[0].mbr_parts[1].mbrp_start) == 0) {
   1608  1.62       dsl 				/* don't need the empty slot */
   1609  1.62       dsl 				memmove(&ext.ptn[0], &ext.ptn[1],
   1610  1.62       dsl 					(ext.num_ptn - 1) * sizeof ext.ptn[0]);
   1611  1.62       dsl 				ext.num_ptn--;
   1612  1.62       dsl 			}
   1613  1.62       dsl 		} else {
   1614  1.62       dsl 			/* must create an empty slot */
   1615  1.62       dsl 			add_ext_ptn(start, dos_sectors);
   1616  1.62       dsl 			ext.ptn[0].mbr_parts[1].mbrp_start = htole32(ext.base
   1617  1.62       dsl 								- start);
   1618  1.62       dsl 		}
   1619  1.62       dsl 	}
   1620  1.62       dsl 	if (fix) {
   1621  1.62       dsl 		ext.base = start;
   1622  1.62       dsl 		ext.limit = start + size;
   1623  1.15      phil 	}
   1624  1.62       dsl 	return 0;
   1625  1.62       dsl }
   1626  1.15      phil 
   1627  1.62       dsl static const char *
   1628  1.62       dsl check_ext_overlap(int part, int sysid, daddr_t start, daddr_t size, int fix)
   1629  1.62       dsl {
   1630  1.62       dsl 	int p;
   1631  1.62       dsl 	uint p_s, p_e;
   1632  1.62       dsl 
   1633  1.62       dsl 	if (sysid == 0)
   1634  1.62       dsl 		return 0;
   1635  1.62       dsl 
   1636  1.62       dsl 	if (MBR_IS_EXTENDED(sysid))
   1637  1.62       dsl 		return "Nested extended partitions are not allowed";
   1638  1.62       dsl 
   1639  1.62       dsl 	/* allow one track at start for extended partition header */
   1640  1.62       dsl 	start -= dos_sectors;
   1641  1.62       dsl 	size += dos_sectors;
   1642  1.62       dsl 	if (start < ext.base || start + size > ext.limit)
   1643  1.62       dsl 		return "Outside bounds of extended partition";
   1644  1.62       dsl 
   1645  1.62       dsl 	if (f_flag && !fix)
   1646  1.62       dsl 		return 0;
   1647  1.62       dsl 
   1648  1.62       dsl 	for (p = ext.num_ptn; --p >= 0;) {
   1649  1.62       dsl 		if (p == part || ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
   1650  1.62       dsl 			continue;
   1651  1.62       dsl 		p_s = ext_offset(p);
   1652  1.62       dsl 		p_e = p_s + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
   1653  1.62       dsl 			+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
   1654  1.62       dsl 		if (p == 0)
   1655  1.62       dsl 			p_s += le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
   1656  1.62       dsl 							- dos_sectors;
   1657  1.62       dsl 		if (start < p_e && start + size > p_s) {
   1658  1.62       dsl 			if (!f_flag)
   1659  1.62       dsl 				return "Overlaps another extended partition";
   1660  1.62       dsl 			if (fix) {
   1661  1.62       dsl 				if (part == -1)
   1662  1.62       dsl 					delete_ext_ptn(p);
   1663  1.62       dsl 				else
   1664  1.62       dsl 					/* must not change numbering yet */
   1665  1.62       dsl 					ext.ptn[p].mbr_parts[0].mbrp_typ = 0;
   1666  1.62       dsl 			}
   1667  1.62       dsl 		}
   1668  1.62       dsl 	}
   1669  1.62       dsl 	return 0;
   1670  1.62       dsl }
   1671   1.1       cgd 
   1672  1.62       dsl int
   1673  1.62       dsl change_part(int extended, int part, int sysid, daddr_t start, daddr_t size,
   1674  1.62       dsl 	char *bootmenu)
   1675  1.62       dsl {
   1676  1.62       dsl 	mbr_partition_t *partp;
   1677  1.62       dsl 	mbr_sector_t *boot;
   1678  1.62       dsl 	daddr_t offset;
   1679  1.62       dsl 	char *e;
   1680  1.62       dsl 	int upart = part;
   1681  1.62       dsl 	int p;
   1682  1.62       dsl 	int fl;
   1683  1.62       dsl 	daddr_t n_s, n_e;
   1684  1.62       dsl 	const char *errtext;
   1685  1.62       dsl #ifdef BOOTSEL
   1686  1.62       dsl 	char tmp_bootmenu[NMBRPART * (PARTNAMESIZE + 1)];
   1687  1.62       dsl 	int bootmenu_len = (extended ? NMBRPART : 1) * (PARTNAMESIZE + 1);
   1688  1.62       dsl #endif
   1689   1.5   mycroft 
   1690  1.62       dsl 	if (extended) {
   1691  1.62       dsl 		if (part != -1 && part < ext.num_ptn) {
   1692  1.62       dsl 			boot = &ext.ptn[part];
   1693  1.62       dsl 			partp = &boot->mbr_parts[0];
   1694  1.62       dsl 			offset = ext_offset(part);
   1695   1.1       cgd 		} else {
   1696  1.62       dsl 			part = -1;
   1697  1.62       dsl 			boot = 0;
   1698  1.62       dsl 			partp = 0;
   1699  1.62       dsl 			offset = 0;
   1700  1.62       dsl 		}
   1701  1.62       dsl 		upart = 0;
   1702  1.62       dsl 		e = "E";
   1703  1.62       dsl 	} else {
   1704  1.62       dsl 		boot = &mboot;
   1705  1.62       dsl 		partp = &boot->mbr_parts[part];
   1706  1.62       dsl 		offset = 0;
   1707  1.62       dsl 		e = "";
   1708  1.62       dsl 	}
   1709  1.62       dsl 
   1710  1.62       dsl 	if (!f_flag && part != -1) {
   1711  1.62       dsl 		printf("The data for partition %s%d is:\n", e, part);
   1712  1.62       dsl 		print_part(boot, upart, offset);
   1713  1.62       dsl 	}
   1714  1.28        ws 
   1715  1.62       dsl #ifdef BOOTSEL
   1716  1.62       dsl 	if (bootmenu != NULL)
   1717  1.62       dsl 		strlcpy(tmp_bootmenu, bootmenu, bootmenu_len);
   1718  1.62       dsl 	else
   1719  1.62       dsl 		if (boot != NULL &&
   1720  1.62       dsl 		    le16toh(boot->mbr_bootsel.mbrb_magic) == MBR_MAGIC)
   1721  1.62       dsl 			strlcpy(tmp_bootmenu,
   1722  1.62       dsl 				boot->mbr_bootsel.mbrb_nametab[upart],
   1723  1.62       dsl 				bootmenu_len);
   1724  1.62       dsl 		else
   1725  1.62       dsl 			tmp_bootmenu[0] = 0;
   1726  1.62       dsl #endif
   1727  1.62       dsl 
   1728  1.62       dsl 	if (!s_flag && partp != NULL) {
   1729  1.62       dsl 		/* values not specified, default to current ones */
   1730  1.62       dsl 		sysid = partp->mbrp_typ;
   1731  1.62       dsl 		start = offset + le32toh(partp->mbrp_start);
   1732  1.62       dsl 		size = le32toh(partp->mbrp_size);
   1733  1.62       dsl 	}
   1734  1.62       dsl 
   1735  1.62       dsl 	/* creating a new partition, default to free space */
   1736  1.62       dsl 	if (!s_flag && sysid == 0 && extended) {
   1737  1.62       dsl 		/* non-extended partition */
   1738  1.62       dsl 		start = ext.base;
   1739  1.62       dsl 		for (p = 0; p < ext.num_ptn; p++) {
   1740  1.62       dsl 			if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
   1741  1.62       dsl 				continue;
   1742  1.62       dsl 			n_s = ext_offset(p);
   1743  1.62       dsl 			if (n_s > start + dos_sectors)
   1744  1.62       dsl 				break;
   1745  1.62       dsl 			start = ext_offset(p)
   1746  1.62       dsl 				+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
   1747  1.62       dsl 				+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
   1748  1.62       dsl 		}
   1749  1.62       dsl 		if (ext.limit - start <= dos_sectors) {
   1750  1.62       dsl 			printf("No space in extended partition\n");
   1751  1.62       dsl 			return 0;
   1752  1.62       dsl 		}
   1753  1.62       dsl 		start += dos_sectors;
   1754  1.62       dsl 	}
   1755  1.62       dsl 
   1756  1.62       dsl 	if (!s_flag && sysid == 0 && !extended) {
   1757  1.62       dsl 		/* same for non-extended partition */
   1758  1.62       dsl 		/* first see if old start is free */
   1759  1.62       dsl 		if (start < dos_sectors)
   1760  1.62       dsl 			start = 0;
   1761  1.62       dsl 		for (p = 0; start != 0 && p < NMBRPART; p++) {
   1762  1.62       dsl 			if (mboot.mbr_parts[p].mbrp_typ == 0)
   1763  1.62       dsl 				continue;
   1764  1.62       dsl 			n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
   1765  1.62       dsl 			if (start >= n_s &&
   1766  1.62       dsl 			    start < n_s + le32toh(mboot.mbr_parts[p].mbrp_size))
   1767  1.62       dsl 				start = 0;
   1768  1.62       dsl 		}
   1769  1.62       dsl 		if (start == 0) {
   1770  1.62       dsl 			/* Look for first gap */
   1771  1.62       dsl 			start = dos_sectors;
   1772  1.62       dsl 			for (p = 0; p < NMBRPART; p++) {
   1773  1.62       dsl 				if (mboot.mbr_parts[p].mbrp_typ == 0)
   1774  1.62       dsl 					continue;
   1775  1.62       dsl 				n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
   1776  1.62       dsl 				n_e = n_s + le32toh(mboot.mbr_parts[p].mbrp_size);
   1777  1.62       dsl 				if (start >= n_s && start < n_e) {
   1778  1.62       dsl 					start = n_e;
   1779  1.62       dsl 					p = -1;
   1780  1.62       dsl 				}
   1781  1.62       dsl 			}
   1782  1.62       dsl 			if (start >= dos_disksectors) {
   1783  1.62       dsl 				printf("No free space\n");
   1784  1.62       dsl 				return 0;
   1785  1.29        ws 			}
   1786   1.1       cgd 		}
   1787  1.62       dsl 	}
   1788   1.4   mycroft 
   1789  1.62       dsl 	if (!f_flag) {
   1790  1.62       dsl 		/* request new values from user */
   1791  1.62       dsl 		if (sysid == 0)
   1792  1.62       dsl 			sysid = 169;
   1793  1.62       dsl 		sysid = decimal("sysid", sysid, 0, 0, 255);
   1794  1.62       dsl 		if (sysid == 0 && !v_flag) {
   1795  1.62       dsl 			start = 0;
   1796  1.62       dsl 			size = 0;
   1797  1.62       dsl #ifdef BOOTSEL
   1798  1.62       dsl 			tmp_bootmenu[0] = 0;
   1799  1.62       dsl #endif
   1800  1.62       dsl 		} else {
   1801  1.62       dsl 			daddr_t old = start;
   1802  1.62       dsl 			daddr_t lim = extended ? ext.limit : dos_disksectors;
   1803  1.62       dsl 			start = decimal("start", start,
   1804  1.62       dsl 				DEC_SEC | DEC_RND_0 | (extended ? DEC_RND : 0),
   1805  1.62       dsl 				extended ? ext.base : 0, lim);
   1806  1.62       dsl 			/* Adjust 'size' so that end doesn't move when 'start'
   1807  1.62       dsl 			 * is only changed slightly.
   1808  1.62       dsl 			 */
   1809  1.62       dsl 			if (size > start - old)
   1810  1.62       dsl 				size -= start - old;
   1811  1.62       dsl 			else
   1812  1.62       dsl 				size = 0;
   1813  1.62       dsl 			/* Find end of available space from this start point */
   1814  1.62       dsl 			if (extended) {
   1815  1.62       dsl 				for (p = 0; p < ext.num_ptn; p++) {
   1816  1.62       dsl 					if (p == part)
   1817  1.62       dsl 						continue;
   1818  1.62       dsl 					if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
   1819  1.62       dsl 						continue;
   1820  1.62       dsl 					n_s = ext_offset(p);
   1821  1.62       dsl 					if (n_s > start && n_s < lim)
   1822  1.62       dsl 						lim = n_s;
   1823  1.62       dsl 					if (start >= n_s && start < n_s
   1824  1.62       dsl 				+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
   1825  1.62       dsl 				+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_size)) {
   1826  1.62       dsl 						lim = start;
   1827  1.62       dsl 						break;
   1828  1.62       dsl 					}
   1829  1.62       dsl 				}
   1830  1.62       dsl 			} else {
   1831  1.62       dsl 				for (p = 0; p < NMBRPART; p++) {
   1832  1.62       dsl 					if (p == part)
   1833  1.62       dsl 						continue;
   1834  1.62       dsl 					if (mboot.mbr_parts[p].mbrp_typ == 0)
   1835  1.62       dsl 						continue;
   1836  1.62       dsl 					n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
   1837  1.62       dsl 					if (n_s > start && n_s < lim)
   1838  1.62       dsl 						lim = n_s;
   1839  1.62       dsl 					if (start >= n_s && start < n_s
   1840  1.62       dsl 				    + le32toh(mboot.mbr_parts[p].mbrp_size)) {
   1841  1.62       dsl 						lim = start;
   1842  1.62       dsl 						break;
   1843  1.62       dsl 					}
   1844  1.62       dsl 				}
   1845  1.62       dsl 			}
   1846  1.62       dsl 			lim -= start;
   1847  1.62       dsl 			if (lim == 0) {
   1848  1.62       dsl 				printf("Start sector already allocated\n");
   1849  1.62       dsl 				return 0;
   1850  1.62       dsl 			}
   1851  1.62       dsl 			if (size == 0 || size > lim)
   1852  1.62       dsl 				size = lim;
   1853  1.62       dsl 			fl = DEC_SEC;
   1854  1.62       dsl 			if (start % dos_cylindersectors == dos_sectors)
   1855  1.62       dsl 				fl |= DEC_RND_DOWN;
   1856  1.62       dsl 			printf("start %lld, cps %d\n",
   1857  1.62       dsl 				start, dos_cylindersectors);
   1858  1.62       dsl 			if (start == 2 * dos_sectors)
   1859  1.62       dsl 				fl |= DEC_RND_DOWN | DEC_RND_DOWN_2;
   1860  1.62       dsl 			size = decimal("size", size, fl, 0, lim);
   1861  1.62       dsl #ifdef BOOTSEL
   1862  1.62       dsl #ifndef DEFAULT_BOOTEXTCODE
   1863  1.62       dsl 			if (!extended)
   1864  1.62       dsl #endif
   1865  1.62       dsl 				string("bootmenu", bootmenu_len, tmp_bootmenu);
   1866  1.62       dsl #endif
   1867  1.62       dsl 		}
   1868  1.62       dsl 	}
   1869  1.62       dsl 
   1870  1.62       dsl 	/*
   1871  1.62       dsl 	 * Before we write these away, we must verify that nothing
   1872  1.62       dsl 	 * untoward has been requested.
   1873  1.62       dsl 	 */
   1874  1.62       dsl 
   1875  1.62       dsl 	if (extended)
   1876  1.62       dsl 		errtext = check_ext_overlap(part, sysid, start, size, 0);
   1877  1.62       dsl 	else
   1878  1.62       dsl 		errtext = check_overlap(part, sysid, start, size, 0);
   1879  1.62       dsl 	if (errtext != NULL) {
   1880  1.62       dsl 		if (f_flag)
   1881  1.62       dsl 			errx(2, "%s\n", errtext);
   1882  1.62       dsl 		printf("%s\n", errtext);
   1883  1.62       dsl 		return 0;
   1884  1.62       dsl 	}
   1885  1.62       dsl 
   1886  1.62       dsl 	/*
   1887  1.62       dsl 	 * Before proceeding, delete any overlapped partitions.
   1888  1.62       dsl 	 * This can only happen if '-f' was supplied on the command line.
   1889  1.62       dsl 	 * Just hope the caller knows what they are doing.
   1890  1.62       dsl 	 * This also fixes the base of each extended partition if the
   1891  1.62       dsl 	 * partition itself has moved.
   1892  1.62       dsl 	 */
   1893  1.62       dsl 
   1894  1.62       dsl 	if (extended)
   1895  1.62       dsl 		errtext = check_ext_overlap(part, sysid, start, size, 1);
   1896  1.62       dsl 	else
   1897  1.62       dsl 		errtext = check_overlap(part, sysid, start, size, 1);
   1898  1.62       dsl 
   1899  1.62       dsl 	if (errtext)
   1900  1.62       dsl 		errx(1, "%s\n", errtext);
   1901  1.62       dsl 
   1902  1.62       dsl 	if (sysid == 0) {
   1903  1.62       dsl 		/* delete this partition - save info though */
   1904  1.62       dsl 		if (partp == NULL)
   1905  1.62       dsl 			/* must have been trying to create an extended ptn */
   1906  1.62       dsl 			return 0;
   1907  1.62       dsl 		if (start == 0 && size == 0)
   1908  1.62       dsl 			memset(partp, 0, sizeof *partp);
   1909  1.62       dsl #ifdef BOOTSEL
   1910  1.62       dsl 		if (le16toh(boot->mbr_bootsel.mbrb_magic) == MBR_MAGIC)
   1911  1.62       dsl 			memset(boot->mbr_bootsel.mbrb_nametab[upart], 0,
   1912  1.62       dsl 				sizeof boot->mbr_bootsel.mbrb_nametab[0]);
   1913  1.62       dsl #endif
   1914  1.62       dsl 		if (extended)
   1915  1.62       dsl 			delete_ext_ptn(part);
   1916  1.62       dsl 		else
   1917  1.62       dsl 			delete_ptn(part);
   1918  1.62       dsl 		return 1;
   1919  1.62       dsl 	}
   1920  1.62       dsl 
   1921  1.62       dsl 
   1922  1.62       dsl 	if (extended) {
   1923  1.62       dsl 		if (part != -1)
   1924  1.62       dsl 			delete_ext_ptn(part);
   1925  1.62       dsl 		if (start == ext.base + dos_sectors)
   1926  1.62       dsl 			/* First one must have been free */
   1927  1.62       dsl 			part = 0;
   1928  1.62       dsl 		else
   1929  1.62       dsl 			part = add_ext_ptn(start, size);
   1930  1.62       dsl 
   1931  1.62       dsl 		/* These must be re-calculated because of the realloc */
   1932  1.62       dsl 		boot = &ext.ptn[part];
   1933  1.62       dsl 		partp = &boot->mbr_parts[0];
   1934  1.62       dsl 		offset = ext_offset(part);
   1935  1.62       dsl 	}
   1936  1.62       dsl 
   1937  1.62       dsl 	partp->mbrp_typ = sysid;
   1938  1.62       dsl 	partp->mbrp_start = htole32( start - offset);
   1939  1.62       dsl 	partp->mbrp_size = htole32( size);
   1940  1.62       dsl 	dos(start, &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
   1941  1.62       dsl 	dos(start + size - 1,
   1942  1.62       dsl 		    &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
   1943  1.62       dsl #ifdef BOOTSEL
   1944  1.62       dsl 	if (extended) {
   1945  1.62       dsl 		boot->mbr_bootsel.mbrb_magic = htole16(MBR_MAGIC);
   1946  1.62       dsl 		strncpy(boot->mbr_bootsel.mbrb_nametab[upart], tmp_bootmenu,
   1947  1.62       dsl 			bootmenu_len);
   1948  1.62       dsl 	} else {
   1949  1.62       dsl 		/* We need to bootselect code installed in order to have
   1950  1.62       dsl 		 * somewhere to safely write the menu tag.
   1951  1.62       dsl 		 */
   1952  1.62       dsl 		if (le16toh(boot->mbr_bootsel.mbrb_magic) != MBR_MAGIC) {
   1953  1.62       dsl 			if (yesno("The bootselect code is not installed, "
   1954  1.62       dsl 					    "do you want to install it now?"))
   1955  1.62       dsl 				install_bootsel(BFL_SELACTIVE);
   1956  1.62       dsl 		}
   1957  1.62       dsl 		if (le16toh(boot->mbr_bootsel.mbrb_magic) == MBR_MAGIC) {
   1958  1.62       dsl 			strncpy(boot->mbr_bootsel.mbrb_nametab[upart],
   1959  1.62       dsl 				tmp_bootmenu, bootmenu_len);
   1960  1.62       dsl 		}
   1961  1.62       dsl 	}
   1962  1.62       dsl #endif
   1963  1.62       dsl 
   1964  1.62       dsl 	if (v_flag && !f_flag && yesno("Explicitly specify beg/end address?")) {
   1965  1.62       dsl 		/* this really isn't a good idea.... */
   1966  1.62       dsl 		int tsector, tcylinder, thead;
   1967  1.62       dsl 
   1968  1.62       dsl 		tcylinder = MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect);
   1969  1.62       dsl 		thead = partp->mbrp_shd;
   1970  1.62       dsl 		tsector = MBR_PSECT(partp->mbrp_ssect);
   1971  1.62       dsl 		tcylinder = decimal("beginning cylinder",
   1972  1.62       dsl 				tcylinder, 0, 0, dos_cylinders - 1);
   1973  1.62       dsl 		thead = decimal("beginning head",
   1974  1.62       dsl 				thead, 0, 0, dos_heads - 1);
   1975  1.62       dsl 		tsector = decimal("beginning sector",
   1976  1.62       dsl 				tsector, 0, 1, dos_sectors);
   1977  1.62       dsl 		partp->mbrp_scyl = DOSCYL(tcylinder);
   1978  1.62       dsl 		partp->mbrp_shd = thead;
   1979  1.62       dsl 		partp->mbrp_ssect = DOSSECT(tsector, tcylinder);
   1980  1.62       dsl 
   1981  1.62       dsl 		tcylinder = MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect);
   1982  1.62       dsl 		thead = partp->mbrp_ehd;
   1983  1.62       dsl 		tsector = MBR_PSECT(partp->mbrp_esect);
   1984  1.62       dsl 		tcylinder = decimal("ending cylinder",
   1985  1.62       dsl 				tcylinder, 0, 0, dos_cylinders - 1);
   1986  1.62       dsl 		thead = decimal("ending head",
   1987  1.62       dsl 				thead, 0, 0, dos_heads - 1);
   1988  1.62       dsl 		tsector = decimal("ending sector",
   1989  1.62       dsl 				tsector, 0, 1, dos_sectors);
   1990  1.62       dsl 		partp->mbrp_ecyl = DOSCYL(tcylinder);
   1991  1.62       dsl 		partp->mbrp_ehd = thead;
   1992  1.62       dsl 		partp->mbrp_esect = DOSSECT(tsector, tcylinder);
   1993  1.62       dsl 	}
   1994  1.62       dsl 
   1995  1.62       dsl 	/* If we had to mark an extended partition as deleted because
   1996  1.62       dsl 	 * another request would have overlapped it, now is the time
   1997  1.62       dsl 	 * to do the actual delete.
   1998  1.62       dsl 	 */
   1999  1.62       dsl 	if (extended && f_flag) {
   2000  1.62       dsl 		for (p = ext.num_ptn; --p >= 0;)
   2001  1.62       dsl 			if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
   2002  1.62       dsl 				delete_ext_ptn(p);
   2003  1.62       dsl 	}
   2004  1.62       dsl 	return 1;
   2005   1.1       cgd }
   2006   1.1       cgd 
   2007   1.4   mycroft void
   2008  1.43     lukem print_params(void)
   2009   1.1       cgd {
   2010   1.4   mycroft 
   2011  1.15      phil 	if (sh_flag) {
   2012  1.62       dsl 		printf("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\nDLSIZE=%llu\n",
   2013  1.62       dsl 			cylinders, heads, sectors, (uintmax_t)disksectors);
   2014  1.62       dsl 		printf("BCYL=%d\nBHEAD=%d\nBSEC=%d\nBDLSIZE=%llu\n",
   2015  1.62       dsl 			dos_cylinders, dos_heads, dos_sectors,
   2016  1.62       dsl 			(uintmax_t)dos_disksectors);
   2017  1.62       dsl 		printf("NUMEXTPTN=%d\n", ext.num_ptn);
   2018  1.14      phil 		return;
   2019  1.14      phil 	}
   2020  1.14      phil 
   2021  1.15      phil 	/* Not sh_flag */
   2022  1.48     lukem 	printf("Disk: %s\n", disk);
   2023  1.34      fvdl 	printf("NetBSD disklabel disk geometry:\n");
   2024  1.62       dsl 	printf("cylinders: %d, heads: %d, sectors/track: %d "
   2025  1.62       dsl 	    "(%d sectors/cylinder)\ntotal sectors: %llu\n\n",
   2026  1.62       dsl 	    cylinders, heads, sectors, cylindersectors,
   2027  1.62       dsl 	    (uintmax_t)disksectors);
   2028  1.34      fvdl 	printf("BIOS disk geometry:\n");
   2029  1.62       dsl 	printf("cylinders: %d, heads: %d, sectors/track: %d "
   2030  1.62       dsl 	    "(%d sectors/cylinder)\ntotal sectors: %llu\n\n",
   2031  1.62       dsl 	    dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors,
   2032  1.62       dsl 	    (uintmax_t)dos_disksectors);
   2033   1.1       cgd }
   2034   1.1       cgd 
   2035   1.4   mycroft void
   2036  1.43     lukem change_active(int which)
   2037   1.1       cgd {
   2038  1.62       dsl 	mbr_partition_t *partp;
   2039   1.4   mycroft 	int part;
   2040  1.15      phil 	int active = 4;
   2041   1.4   mycroft 
   2042  1.61       dsl 	partp = &mboot.mbr_parts[0];
   2043   1.1       cgd 
   2044   1.1       cgd 	if (a_flag && which != -1)
   2045   1.1       cgd 		active = which;
   2046   1.4   mycroft 	else {
   2047  1.32   thorpej 		for (part = 0; part < NMBRPART; part++)
   2048  1.62       dsl 			if (partp[part].mbrp_flag & MBR_FLAGS_ACTIVE)
   2049   1.4   mycroft 				active = part;
   2050   1.4   mycroft 	}
   2051  1.15      phil 	if (!f_flag) {
   2052  1.15      phil 		if (yesno("Do you want to change the active partition?")) {
   2053  1.15      phil 			printf ("Choosing 4 will make no partition active.\n");
   2054  1.15      phil 			do {
   2055  1.62       dsl 				active = decimal("active partition",
   2056  1.62       dsl 						active, 0, 0, 4);
   2057  1.15      phil 			} while (!yesno("Are you happy with this choice?"));
   2058  1.15      phil 		} else
   2059  1.15      phil 			return;
   2060  1.15      phil 	} else
   2061  1.15      phil 		if (active != 4)
   2062  1.15      phil 			printf ("Making partition %d active.\n", active);
   2063  1.15      phil 
   2064  1.32   thorpej 	for (part = 0; part < NMBRPART; part++)
   2065  1.62       dsl 		partp[part].mbrp_flag &= ~MBR_FLAGS_ACTIVE;
   2066  1.15      phil 	if (active < 4)
   2067  1.62       dsl 		partp[active].mbrp_flag |= MBR_FLAGS_ACTIVE;
   2068   1.1       cgd }
   2069   1.1       cgd 
   2070   1.4   mycroft void
   2071  1.43     lukem get_params_to_use(void)
   2072   1.1       cgd {
   2073  1.62       dsl 	struct biosdisk_info *bip;
   2074  1.62       dsl 	int i;
   2075  1.43     lukem 
   2076  1.16      phil 	if (b_flag) {
   2077  1.16      phil 		dos_cylinders = b_cyl;
   2078  1.16      phil 		dos_heads = b_head;
   2079  1.16      phil 		dos_sectors = b_sec;
   2080  1.16      phil 		return;
   2081  1.16      phil 	}
   2082   1.4   mycroft 
   2083   1.1       cgd 	print_params();
   2084  1.62       dsl 	if (!yesno("Do you want to change our idea of what BIOS thinks?"))
   2085  1.62       dsl 		return;
   2086  1.62       dsl 
   2087  1.62       dsl #if defined(__i386__) || defined(__x86_64__)
   2088  1.62       dsl 	if (dl != NULL) {
   2089  1.62       dsl 		for (i = 0; i < dl->dl_nbiosdisks; i++) {
   2090  1.62       dsl 			if (i == 0)
   2091  1.62       dsl 				printf("\nGeometries of known disks:\n");
   2092  1.62       dsl 			bip = &dl->dl_biosdisks[i];
   2093  1.62       dsl 			printf("Disk %d: cylinders %u, heads %u, sectors %u"
   2094  1.62       dsl 				" (%lld sectors, %dMB)\n",
   2095  1.62       dsl 			    i, bip->bi_cyl, bip->bi_head, bip->bi_sec,
   2096  1.62       dsl 			    (uintmax_t)bip->bi_lbasecs,
   2097  1.62       dsl 			    SEC_TO_MB(bip->bi_lbasecs));
   2098  1.62       dsl 
   2099  1.62       dsl 		}
   2100  1.62       dsl 		printf("\n");
   2101   1.1       cgd 	}
   2102  1.62       dsl #endif
   2103  1.62       dsl 	do {
   2104  1.62       dsl 		dos_cylinders = decimal("BIOS's idea of #cylinders",
   2105  1.62       dsl 					dos_cylinders, 0, 0, MAXCYL);
   2106  1.62       dsl 		dos_heads = decimal("BIOS's idea of #heads",
   2107  1.62       dsl 					dos_heads, 0, 0, MAXHEAD);
   2108  1.62       dsl 		dos_sectors = decimal("BIOS's idea of #sectors",
   2109  1.62       dsl 					dos_sectors, 0, 1, MAXSECTOR);
   2110  1.62       dsl 		print_params();
   2111  1.62       dsl 	} while (!yesno("Are you happy with this choice?"));
   2112   1.1       cgd }
   2113   1.1       cgd 
   2114  1.62       dsl 
   2115   1.1       cgd /***********************************************\
   2116   1.1       cgd * Change real numbers into strange dos numbers	*
   2117   1.1       cgd \***********************************************/
   2118   1.4   mycroft void
   2119  1.43     lukem dos(int sector, unsigned char *cylinderp, unsigned char *headp,
   2120  1.43     lukem     unsigned char *sectorp)
   2121   1.4   mycroft {
   2122   1.5   mycroft 	int cylinder, head;
   2123  1.40      fvdl 	int biosmaxsec;
   2124  1.40      fvdl 
   2125  1.62       dsl 	biosmaxsec = dos_totalsectors - 1;
   2126  1.40      fvdl 	if (sector > biosmaxsec)
   2127  1.40      fvdl 		sector = biosmaxsec;
   2128   1.4   mycroft 
   2129   1.5   mycroft 	cylinder = sector / dos_cylindersectors;
   2130  1.28        ws 
   2131   1.5   mycroft 	sector -= cylinder * dos_cylindersectors;
   2132   1.4   mycroft 
   2133   1.5   mycroft 	head = sector / dos_sectors;
   2134   1.5   mycroft 	sector -= head * dos_sectors;
   2135   1.4   mycroft 
   2136   1.5   mycroft 	*cylinderp = DOSCYL(cylinder);
   2137   1.5   mycroft 	*headp = head;
   2138   1.5   mycroft 	*sectorp = DOSSECT(sector + 1, cylinder);
   2139   1.1       cgd }
   2140   1.1       cgd 
   2141   1.4   mycroft int
   2142  1.44     lukem open_disk(int update)
   2143   1.1       cgd {
   2144  1.18     lukem 	static char namebuf[MAXPATHLEN + 1];
   2145   1.1       cgd 
   2146  1.62       dsl 	fd = opendisk(disk, update && disk_file == NULL ? O_RDWR : O_RDONLY,
   2147  1.62       dsl 	    namebuf, sizeof(namebuf), 0);
   2148  1.18     lukem 	if (fd < 0) {
   2149  1.47     lukem 		if (errno == ENODEV)
   2150  1.47     lukem 			warnx("%s is not a character device", namebuf);
   2151  1.47     lukem 		else
   2152  1.47     lukem 			warn("%s", namebuf);
   2153   1.4   mycroft 		return (-1);
   2154   1.4   mycroft 	}
   2155  1.18     lukem 	disk = namebuf;
   2156   1.4   mycroft 	if (get_params() == -1) {
   2157   1.4   mycroft 		close(fd);
   2158  1.62       dsl 		fd = -1;
   2159   1.4   mycroft 		return (-1);
   2160   1.1       cgd 	}
   2161  1.62       dsl 	if (disk_file != NULL) {
   2162  1.62       dsl 		/* for testing: read/write data from a disk file */
   2163  1.62       dsl 		wfd = open(disk_file, update ? O_RDWR|O_CREAT : O_RDONLY, 0777);
   2164  1.62       dsl 		if (wfd == -1) {
   2165  1.62       dsl 			warn("%s", disk_file);
   2166  1.62       dsl 			close(fd);
   2167  1.62       dsl 			fd = -1;
   2168  1.62       dsl 			return -1;
   2169  1.62       dsl 		}
   2170  1.62       dsl 	} else
   2171  1.62       dsl 		wfd = fd;
   2172   1.4   mycroft 	return (0);
   2173   1.1       cgd }
   2174   1.1       cgd 
   2175   1.4   mycroft int
   2176  1.62       dsl read_disk(daddr_t sector, void *buf)
   2177   1.1       cgd {
   2178   1.4   mycroft 
   2179  1.62       dsl 	if (*rfd == -1)
   2180  1.44     lukem 		errx(1, "read_disk(); fd == -1");
   2181  1.62       dsl 	if (lseek(*rfd, sector * (off_t)512, 0) == -1)
   2182   1.4   mycroft 		return (-1);
   2183  1.62       dsl 	return (read(*rfd, buf, 512));
   2184   1.1       cgd }
   2185   1.1       cgd 
   2186   1.4   mycroft int
   2187  1.62       dsl write_disk(daddr_t sector, void *buf)
   2188   1.1       cgd {
   2189   1.4   mycroft 
   2190  1.62       dsl 	if (wfd == -1)
   2191  1.62       dsl 		errx(1, "write_disk(); wfd == -1");
   2192  1.62       dsl 	if (lseek(wfd, sector * (off_t)512, 0) == -1)
   2193   1.4   mycroft 		return (-1);
   2194  1.62       dsl 	return (write(wfd, buf, 512));
   2195   1.1       cgd }
   2196   1.1       cgd 
   2197   1.4   mycroft int
   2198  1.43     lukem get_params(void)
   2199   1.1       cgd {
   2200   1.1       cgd 
   2201  1.42   thorpej 	if (ioctl(fd, DIOCGDEFLABEL, &disklabel) == -1) {
   2202  1.42   thorpej 		warn("DIOCGDEFLABEL");
   2203  1.42   thorpej 		if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
   2204  1.42   thorpej 			warn("DIOCGDINFO");
   2205  1.42   thorpej 			return (-1);
   2206  1.42   thorpej 		}
   2207   1.4   mycroft 	}
   2208   1.1       cgd 
   2209  1.62       dsl 	cylinders = disklabel.d_ncylinders;
   2210  1.62       dsl 	heads = disklabel.d_ntracks;
   2211  1.62       dsl 	sectors = disklabel.d_nsectors;
   2212  1.31  jonathan 	disksectors = disklabel.d_secperunit;
   2213   1.1       cgd 
   2214  1.62       dsl 	/* pick up some defaults for the BIOS sizes */
   2215  1.62       dsl 	if (sectors <= MAXSECTOR) {
   2216  1.62       dsl 		dos_cylinders = cylinders;
   2217  1.62       dsl 		dos_heads = heads;
   2218  1.62       dsl 		dos_sectors = sectors;
   2219  1.62       dsl 	} else {
   2220  1.62       dsl 		/* guess - has to better than the above */
   2221  1.62       dsl 		dos_sectors = MAXSECTOR;
   2222  1.62       dsl 		dos_heads = MAXHEAD - 1;	/* some BIOS might use 256 */
   2223  1.62       dsl 		dos_cylinders = disksectors / (MAXSECTOR * (MAXHEAD - 1));
   2224  1.62       dsl 		if (dos_cylinders > MAXCYL - 1)
   2225  1.62       dsl 			dos_cylinders = MAXCYL - 1;
   2226  1.62       dsl 	}
   2227  1.62       dsl 	dos_disksectors = disksectors;
   2228  1.62       dsl 
   2229   1.4   mycroft 	return (0);
   2230   1.1       cgd }
   2231   1.1       cgd 
   2232   1.4   mycroft int
   2233  1.62       dsl read_s0(daddr_t offset, mbr_sector_t *boot)
   2234   1.1       cgd {
   2235   1.4   mycroft 
   2236  1.62       dsl 	if (read_disk(offset, &boot->mbr_bootinst) == -1) {
   2237  1.62       dsl 		warn("Can't read %spartition table",
   2238  1.62       dsl 		    offset ? "extended " : "");
   2239  1.62       dsl 		return -1;
   2240   1.1       cgd 	}
   2241  1.62       dsl 	if (le16toh(boot->mbr_signature) != MBR_MAGIC) {
   2242  1.62       dsl 		warnx("%spartition table invalid, no magic in sector %lld",
   2243  1.62       dsl 		    offset ? "extended " : "", (long long)offset);
   2244  1.62       dsl 		return -1;
   2245   1.1       cgd 	}
   2246   1.4   mycroft 	return (0);
   2247   1.1       cgd }
   2248   1.1       cgd 
   2249   1.4   mycroft int
   2250  1.62       dsl write_mbr(void)
   2251   1.1       cgd {
   2252  1.30        ws 	int flag, i;
   2253  1.62       dsl 	daddr_t offset;
   2254  1.62       dsl 	int rval = -1;
   2255   1.4   mycroft 
   2256   1.1       cgd 	/*
   2257   1.1       cgd 	 * write enable label sector before write (if necessary),
   2258   1.1       cgd 	 * disable after writing.
   2259   1.1       cgd 	 * needed if the disklabel protected area also protects
   2260   1.1       cgd 	 * sector 0. (e.g. empty disk)
   2261   1.1       cgd 	 */
   2262   1.1       cgd 	flag = 1;
   2263  1.62       dsl 	if (wfd == fd && ioctl(wfd, DIOCWLABEL, &flag) < 0)
   2264   1.4   mycroft 		warn("DIOCWLABEL");
   2265  1.62       dsl 	if (write_disk(0, &mboot.mbr_bootinst) == -1) {
   2266  1.62       dsl 		warn("Can't write fdisk partition table");
   2267  1.62       dsl 		goto protect_label;
   2268  1.62       dsl 	}
   2269  1.62       dsl 	if (boot_installed)
   2270  1.62       dsl 		for (i = bootsize; (i -= 0x200) > 0;)
   2271  1.62       dsl 			if (write_disk(i / 0x200, &bootcode[i / 0x200]) == -1) {
   2272  1.62       dsl 				warn("Can't write bootcode");
   2273  1.62       dsl 				goto protect_label;
   2274  1.62       dsl 			}
   2275  1.62       dsl 	for (offset = 0, i = 0; i < ext.num_ptn; i++) {
   2276  1.62       dsl 		if (write_disk(ext.base + offset, ext.ptn + i) == -1) {
   2277  1.62       dsl 			warn("Can't write %dth extended partition", i);
   2278  1.62       dsl 			goto protect_label;
   2279  1.62       dsl 		}
   2280  1.62       dsl 		offset = le32toh(ext.ptn[i].mbr_parts[1].mbrp_start);
   2281   1.4   mycroft 	}
   2282  1.62       dsl 	rval = 0;
   2283  1.62       dsl     protect_label:
   2284   1.1       cgd 	flag = 0;
   2285  1.62       dsl 	if (wfd == fd && ioctl(wfd, DIOCWLABEL, &flag) < 0)
   2286   1.4   mycroft 		warn("DIOCWLABEL");
   2287  1.62       dsl 	return rval;
   2288   1.1       cgd }
   2289   1.1       cgd 
   2290   1.4   mycroft int
   2291  1.62       dsl yesno(const char *str, ...)
   2292   1.4   mycroft {
   2293   1.4   mycroft 	int ch, first;
   2294  1.62       dsl 	va_list ap;
   2295  1.62       dsl 
   2296  1.62       dsl 	va_start(ap, str);
   2297   1.1       cgd 
   2298  1.62       dsl 	vprintf(str, ap);
   2299  1.62       dsl 	printf(" [n] ");
   2300   1.1       cgd 
   2301   1.4   mycroft 	first = ch = getchar();
   2302   1.4   mycroft 	while (ch != '\n' && ch != EOF)
   2303   1.4   mycroft 		ch = getchar();
   2304  1.53      yamt 	if (ch == EOF)
   2305  1.53      yamt 		errx(1, "EOF");
   2306   1.4   mycroft 	return (first == 'y' || first == 'Y');
   2307   1.1       cgd }
   2308   1.1       cgd 
   2309  1.62       dsl int
   2310  1.62       dsl decimal(const char *prompt, int dflt, int flags, int minval, int maxval)
   2311   1.1       cgd {
   2312   1.8       cgd 	int acc = 0;
   2313   1.4   mycroft 	char *cp;
   2314   1.1       cgd 
   2315  1.62       dsl 	for (;;) {
   2316  1.62       dsl 		if (flags & DEC_SEC) {
   2317  1.62       dsl 			printf("%s: [%d..%dcyl default: %d, %dcyl, %uMB] ",
   2318  1.62       dsl 			    prompt, SEC_TO_CYL(minval), SEC_TO_CYL(maxval),
   2319  1.62       dsl 			    dflt, SEC_TO_CYL(dflt), SEC_TO_MB(dflt));
   2320  1.62       dsl 		} else
   2321  1.62       dsl 			printf("%s: [%d..%d default: %d] ",
   2322  1.62       dsl 			    prompt, minval, maxval, dflt);
   2323   1.1       cgd 
   2324  1.53      yamt 		if (!fgets(lbuf, LBUF, stdin))
   2325  1.53      yamt 			errx(1, "EOF");
   2326   1.7   mycroft 		lbuf[strlen(lbuf)-1] = '\0';
   2327   1.7   mycroft 		cp = lbuf;
   2328   1.7   mycroft 
   2329   1.7   mycroft 		cp += strspn(cp, " \t");
   2330   1.7   mycroft 		if (*cp == '\0')
   2331  1.62       dsl 			return dflt;
   2332  1.62       dsl 
   2333  1.62       dsl 		if (cp[0] == '$' && cp[1] == 0)
   2334  1.62       dsl 			return maxval;
   2335  1.62       dsl 
   2336  1.62       dsl 		if (isdigit(*cp) || *cp == '-') {
   2337  1.62       dsl 			acc = strtol(lbuf, &cp, 10);
   2338  1.62       dsl 			if (flags & DEC_SEC) {
   2339  1.62       dsl 				if (*cp == 'm' || *cp == 'M') {
   2340  1.62       dsl 					acc *= SEC_IN_1M;
   2341  1.62       dsl 					/* round to whole number of cylinders */
   2342  1.62       dsl 					acc += dos_cylindersectors / 2;
   2343  1.62       dsl 					acc /= dos_cylindersectors;
   2344  1.62       dsl 					cp = "c";
   2345  1.62       dsl 				}
   2346  1.62       dsl 				if (*cp == 'c' || *cp == 'C') {
   2347  1.62       dsl 					cp = "";
   2348  1.62       dsl 					acc *= dos_cylindersectors;
   2349  1.62       dsl 					/* adjustments for cylinder boundary */
   2350  1.62       dsl 					if (acc == 0 && flags & DEC_RND_0)
   2351  1.62       dsl 						acc += dos_sectors;
   2352  1.62       dsl 					if (flags & DEC_RND)
   2353  1.62       dsl 						acc += dos_sectors;
   2354  1.62       dsl 					if (flags & DEC_RND_DOWN)
   2355  1.62       dsl 						acc -= dos_sectors;
   2356  1.62       dsl 					if (flags & DEC_RND_DOWN_2)
   2357  1.62       dsl 						acc -= dos_sectors;
   2358  1.62       dsl 				}
   2359  1.62       dsl 			}
   2360  1.62       dsl 		}
   2361   1.1       cgd 
   2362  1.62       dsl 		cp += strspn(cp, " \t");
   2363  1.62       dsl 		if (*cp != '\0') {
   2364  1.62       dsl 			printf("%s is not a valid %s number.\n", lbuf,
   2365  1.62       dsl 			    flags & DEC_SEC ? "sector" : "decimal");
   2366   1.7   mycroft 			continue;
   2367  1.62       dsl 		}
   2368  1.62       dsl 
   2369  1.62       dsl 		if (acc >= minval && acc <= maxval)
   2370  1.62       dsl 			return acc;
   2371  1.62       dsl 		printf("%d is not between %d and %d.\n", acc, minval, maxval);
   2372  1.62       dsl 	}
   2373  1.62       dsl }
   2374  1.62       dsl 
   2375  1.62       dsl int
   2376  1.62       dsl ptn_id(const char *prompt, int *extended)
   2377  1.62       dsl {
   2378  1.62       dsl 	uint acc = 0;
   2379  1.62       dsl 	char *cp;
   2380  1.62       dsl 
   2381  1.62       dsl 	for (;; printf("%s is not a valid partition number.\n", lbuf)) {
   2382  1.62       dsl 		printf("%s: [none] ", prompt);
   2383  1.62       dsl 
   2384  1.62       dsl 		if (!fgets(lbuf, LBUF, stdin))
   2385  1.62       dsl 			errx(1, "EOF");
   2386  1.62       dsl 		lbuf[strlen(lbuf)-1] = '\0';
   2387  1.62       dsl 		cp = lbuf;
   2388  1.62       dsl 
   2389  1.62       dsl 		cp += strspn(cp, " \t");
   2390  1.62       dsl 		*extended = 0;
   2391  1.62       dsl 		if (*cp == 0)
   2392  1.62       dsl 			return -1;
   2393  1.62       dsl 
   2394  1.62       dsl 		if (*cp == 'E' || *cp == 'e') {
   2395  1.62       dsl 			cp++;
   2396  1.62       dsl 			*extended = 1;
   2397  1.62       dsl 		}
   2398  1.62       dsl 
   2399  1.62       dsl 		acc = strtoul(cp, &cp, 10);
   2400   1.7   mycroft 
   2401   1.5   mycroft 		cp += strspn(cp, " \t");
   2402   1.5   mycroft 		if (*cp != '\0')
   2403   1.5   mycroft 			continue;
   2404   1.1       cgd 
   2405  1.62       dsl 		if (*extended || acc < NMBRPART)
   2406  1.62       dsl 			return acc;
   2407   1.1       cgd 	}
   2408  1.62       dsl }
   2409  1.62       dsl 
   2410  1.62       dsl #ifdef BOOTSEL
   2411  1.62       dsl void
   2412  1.62       dsl string(const char *prompt, int length, char *buf)
   2413  1.62       dsl {
   2414  1.62       dsl 	int len;
   2415   1.1       cgd 
   2416  1.62       dsl 	for (;;) {
   2417  1.62       dsl 		printf("%s: [%.*s] ", prompt, length, buf);
   2418  1.62       dsl 
   2419  1.62       dsl 		if (!fgets(lbuf, LBUF, stdin))
   2420  1.62       dsl 			errx(1, "EOF");
   2421  1.62       dsl 		len = strlen(lbuf);
   2422  1.62       dsl 		if (len <= 1)
   2423  1.62       dsl 			/* unchanged if just <enter> */
   2424  1.62       dsl 			return;
   2425  1.62       dsl 		/* now strip trailing spaces, <space><enter> deletes string */
   2426  1.62       dsl 		do
   2427  1.62       dsl 			lbuf[--len] = 0;
   2428  1.62       dsl 		while (len != 0 && lbuf[len - 1] == ' ');
   2429  1.62       dsl 		if (len < length)
   2430  1.62       dsl 			break;
   2431  1.62       dsl 		printf("'%s' is longer than %d character.\n", lbuf, length - 1);
   2432  1.62       dsl 	}
   2433  1.62       dsl 	strncpy(buf, lbuf, length);
   2434   1.1       cgd }
   2435  1.62       dsl #endif
   2436   1.1       cgd 
   2437   1.4   mycroft int
   2438  1.43     lukem type_match(const void *key, const void *item)
   2439   1.4   mycroft {
   2440   1.4   mycroft 	const int *typep = key;
   2441   1.4   mycroft 	const struct part_type *ptr = item;
   2442   1.4   mycroft 
   2443   1.4   mycroft 	if (*typep < ptr->type)
   2444   1.4   mycroft 		return (-1);
   2445   1.4   mycroft 	if (*typep > ptr->type)
   2446   1.4   mycroft 		return (1);
   2447   1.4   mycroft 	return (0);
   2448   1.4   mycroft }
   2449   1.4   mycroft 
   2450  1.43     lukem const char *
   2451  1.43     lukem get_type(int type)
   2452   1.4   mycroft {
   2453   1.4   mycroft 	struct part_type *ptr;
   2454   1.4   mycroft 
   2455   1.4   mycroft 	ptr = bsearch(&type, part_types,
   2456   1.4   mycroft 	    sizeof(part_types) / sizeof(struct part_type),
   2457   1.4   mycroft 	    sizeof(struct part_type), type_match);
   2458   1.4   mycroft 	if (ptr == 0)
   2459   1.4   mycroft 		return ("unknown");
   2460  1.16      phil 	return (ptr->name);
   2461   1.1       cgd }
   2462