Home | History | Annotate | Line # | Download | only in i386
md.c revision 1.3.4.2
      1  1.3.4.2  tls /*	$NetBSD: md.c,v 1.3.4.2 2014/08/20 00:05:16 tls Exp $ */
      2  1.3.4.2  tls 
      3  1.3.4.2  tls /*
      4  1.3.4.2  tls  * Copyright 1997 Piermont Information Systems Inc.
      5  1.3.4.2  tls  * All rights reserved.
      6  1.3.4.2  tls  *
      7  1.3.4.2  tls  * Based on code written by Philip A. Nelson for Piermont Information
      8  1.3.4.2  tls  * Systems Inc.
      9  1.3.4.2  tls  *
     10  1.3.4.2  tls  * Redistribution and use in source and binary forms, with or without
     11  1.3.4.2  tls  * modification, are permitted provided that the following conditions
     12  1.3.4.2  tls  * are met:
     13  1.3.4.2  tls  * 1. Redistributions of source code must retain the above copyright
     14  1.3.4.2  tls  *    notice, this list of conditions and the following disclaimer.
     15  1.3.4.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3.4.2  tls  *    notice, this list of conditions and the following disclaimer in the
     17  1.3.4.2  tls  *    documentation and/or other materials provided with the distribution.
     18  1.3.4.2  tls  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
     19  1.3.4.2  tls  *    or promote products derived from this software without specific prior
     20  1.3.4.2  tls  *    written permission.
     21  1.3.4.2  tls  *
     22  1.3.4.2  tls  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     23  1.3.4.2  tls  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.3.4.2  tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.3.4.2  tls  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     26  1.3.4.2  tls  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.3.4.2  tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.3.4.2  tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.3.4.2  tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.3.4.2  tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.3.4.2  tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  1.3.4.2  tls  * THE POSSIBILITY OF SUCH DAMAGE.
     33  1.3.4.2  tls  */
     34  1.3.4.2  tls 
     35  1.3.4.2  tls /* md.c -- i386 machine specific routines - also used by amd64 */
     36  1.3.4.2  tls 
     37  1.3.4.2  tls #include <sys/param.h>
     38  1.3.4.2  tls #include <sys/sysctl.h>
     39  1.3.4.2  tls #include <sys/exec.h>
     40  1.3.4.2  tls #include <sys/utsname.h>
     41  1.3.4.2  tls #include <sys/types.h>
     42  1.3.4.2  tls #include <sys/stat.h>
     43  1.3.4.2  tls #include <machine/cpu.h>
     44  1.3.4.2  tls #include <stdio.h>
     45  1.3.4.2  tls #include <stddef.h>
     46  1.3.4.2  tls #include <util.h>
     47  1.3.4.2  tls #include <dirent.h>
     48  1.3.4.2  tls #include <termios.h>
     49  1.3.4.2  tls 
     50  1.3.4.2  tls #include "defs.h"
     51  1.3.4.2  tls #include "md.h"
     52  1.3.4.2  tls #include "endian.h"
     53  1.3.4.2  tls #include "msg_defs.h"
     54  1.3.4.2  tls #include "menu_defs.h"
     55  1.3.4.2  tls 
     56  1.3.4.2  tls #ifdef NO_LBA_READS		/* for testing */
     57  1.3.4.2  tls #undef BIFLAG_EXTINT13
     58  1.3.4.2  tls #define BIFLAG_EXTINT13	0
     59  1.3.4.2  tls #endif
     60  1.3.4.2  tls 
     61  1.3.4.2  tls static struct biosdisk_info *biosdisk = NULL;
     62  1.3.4.2  tls 
     63  1.3.4.2  tls /* prototypes */
     64  1.3.4.2  tls 
     65  1.3.4.2  tls static int get_bios_info(char *);
     66  1.3.4.2  tls static int mbr_root_above_chs(void);
     67  1.3.4.2  tls static void md_upgrade_mbrtype(void);
     68  1.3.4.2  tls static int md_read_bootcode(const char *, struct mbr_sector *);
     69  1.3.4.2  tls static unsigned int get_bootmodel(void);
     70  1.3.4.2  tls 
     71  1.3.4.2  tls void
     72  1.3.4.2  tls md_init(void)
     73  1.3.4.2  tls {
     74  1.3.4.2  tls }
     75  1.3.4.2  tls 
     76  1.3.4.2  tls void
     77  1.3.4.2  tls md_init_set_status(int flags)
     78  1.3.4.2  tls {
     79  1.3.4.2  tls 	(void)flags;
     80  1.3.4.2  tls 
     81  1.3.4.2  tls 	/* Default to install same type of kernel as we are running */
     82  1.3.4.2  tls 	set_kernel_set(get_bootmodel());
     83  1.3.4.2  tls }
     84  1.3.4.2  tls 
     85  1.3.4.2  tls int
     86  1.3.4.2  tls md_get_info(void)
     87  1.3.4.2  tls {
     88  1.3.4.2  tls 	mbr_info_t *ext;
     89  1.3.4.2  tls 	struct mbr_partition *p;
     90  1.3.4.2  tls 	const char *bootcode;
     91  1.3.4.2  tls 	int i;
     92  1.3.4.2  tls 	int names, fl, ofl;
     93  1.3.4.2  tls #define	ACTIVE_FOUND	0x0100
     94  1.3.4.2  tls #define	NETBSD_ACTIVE	0x0200
     95  1.3.4.2  tls #define	NETBSD_NAMED	0x0400
     96  1.3.4.2  tls #define	ACTIVE_NAMED	0x0800
     97  1.3.4.2  tls 
     98  1.3.4.2  tls 	if (pm->no_mbr)
     99  1.3.4.2  tls 		return 1;
    100  1.3.4.2  tls 
    101  1.3.4.2  tls 	if (read_mbr(pm->diskdev, &mbr) < 0)
    102  1.3.4.2  tls 		memset(&mbr.mbr, 0, sizeof mbr.mbr - 2);
    103  1.3.4.2  tls 	get_bios_info(pm->diskdev);
    104  1.3.4.2  tls 
    105  1.3.4.2  tls edit:
    106  1.3.4.2  tls 	if (edit_mbr(&mbr) == 0)
    107  1.3.4.2  tls 		return 0;
    108  1.3.4.2  tls 
    109  1.3.4.2  tls 	root_limit = 0;
    110  1.3.4.2  tls 	if (biosdisk != NULL && (biosdisk->bi_flags & BIFLAG_EXTINT13) == 0) {
    111  1.3.4.2  tls 		if (mbr_root_above_chs()) {
    112  1.3.4.2  tls 			msg_display(MSG_partabovechs);
    113  1.3.4.2  tls 			process_menu(MENU_noyes, NULL);
    114  1.3.4.2  tls 			if (!yesno)
    115  1.3.4.2  tls 				goto edit;
    116  1.3.4.2  tls 			/* The user is shooting themselves in the foot here...*/
    117  1.3.4.2  tls 		} else
    118  1.3.4.2  tls 			root_limit = bcyl * bhead * bsec;
    119  1.3.4.2  tls 	}
    120  1.3.4.2  tls 
    121  1.3.4.2  tls 	/*
    122  1.3.4.2  tls 	 * Ensure the install partition (at sector pm->ptstart) and the active
    123  1.3.4.2  tls 	 * partition are bootable.
    124  1.3.4.2  tls 	 * Determine whether the bootselect code is needed.
    125  1.3.4.2  tls 	 * Note that MBR_BS_NEWMBR is always set, so we ignore it!
    126  1.3.4.2  tls 	 */
    127  1.3.4.2  tls 	fl = 0;
    128  1.3.4.2  tls 	names = 0;
    129  1.3.4.2  tls 	for (ext = &mbr; ext != NULL; ext = ext->extended) {
    130  1.3.4.2  tls 		p = ext->mbr.mbr_parts;
    131  1.3.4.2  tls 		for (i = 0; i < MBR_PART_COUNT; p++, i++) {
    132  1.3.4.2  tls 			if (p->mbrp_flag == MBR_PFLAG_ACTIVE) {
    133  1.3.4.2  tls 				fl |= ACTIVE_FOUND;
    134  1.3.4.2  tls 			    if (ext->sector + p->mbrp_start == pm->ptstart)
    135  1.3.4.2  tls 				fl |= NETBSD_ACTIVE;
    136  1.3.4.2  tls 			}
    137  1.3.4.2  tls 			if (ext->mbrb.mbrbs_nametab[i][0] == 0) {
    138  1.3.4.2  tls 				/* No bootmenu label... */
    139  1.3.4.2  tls 				if (ext->sector == 0)
    140  1.3.4.2  tls 					continue;
    141  1.3.4.2  tls 				if (ext->sector + p->mbrp_start == pm->ptstart)
    142  1.3.4.2  tls 					/*
    143  1.3.4.2  tls 					 * Have installed into an extended ptn
    144  1.3.4.2  tls 					 * force name & bootsel...
    145  1.3.4.2  tls 					 */
    146  1.3.4.2  tls 					names++;
    147  1.3.4.2  tls 				continue;
    148  1.3.4.2  tls 			}
    149  1.3.4.2  tls 			/* Partition has a bootmenu label... */
    150  1.3.4.2  tls 			if (ext->sector != 0)
    151  1.3.4.2  tls 				fl |= MBR_BS_EXTLBA;
    152  1.3.4.2  tls 			if (ext->sector + p->mbrp_start == pm->ptstart)
    153  1.3.4.2  tls 				fl |= NETBSD_NAMED;
    154  1.3.4.2  tls 			else if (p->mbrp_flag == MBR_PFLAG_ACTIVE)
    155  1.3.4.2  tls 				fl |= ACTIVE_NAMED;
    156  1.3.4.2  tls 			else
    157  1.3.4.2  tls 				names++;
    158  1.3.4.2  tls 		}
    159  1.3.4.2  tls 	}
    160  1.3.4.2  tls 	if (!(fl & ACTIVE_FOUND))
    161  1.3.4.2  tls 		fl |= NETBSD_ACTIVE;
    162  1.3.4.2  tls 	if (fl & NETBSD_NAMED && fl & NETBSD_ACTIVE)
    163  1.3.4.2  tls 		fl |= ACTIVE_NAMED;
    164  1.3.4.2  tls 
    165  1.3.4.2  tls 	if ((names > 0 || !(fl & NETBSD_ACTIVE)) &&
    166  1.3.4.2  tls 	    (!(fl & NETBSD_NAMED) || !(fl & ACTIVE_NAMED))) {
    167  1.3.4.2  tls 		/*
    168  1.3.4.2  tls 		 * There appear to be multiple bootable partitions, but they
    169  1.3.4.2  tls 		 * don't all have bootmenu texts.
    170  1.3.4.2  tls 		 */
    171  1.3.4.2  tls 		msg_display(MSG_missing_bootmenu_text);
    172  1.3.4.2  tls 		process_menu(MENU_yesno, NULL);
    173  1.3.4.2  tls 		if (yesno)
    174  1.3.4.2  tls 			goto edit;
    175  1.3.4.2  tls 	}
    176  1.3.4.2  tls 
    177  1.3.4.2  tls 	if ((fl & MBR_BS_EXTLBA) &&
    178  1.3.4.2  tls 	    (biosdisk == NULL || !(biosdisk->bi_flags & BIFLAG_EXTINT13))) {
    179  1.3.4.2  tls 		/* Need unsupported LBA reads to read boot sectors */
    180  1.3.4.2  tls 		msg_display(MSG_no_extended_bootmenu);
    181  1.3.4.2  tls 		process_menu(MENU_noyes, NULL);
    182  1.3.4.2  tls 		if (!yesno)
    183  1.3.4.2  tls 			goto edit;
    184  1.3.4.2  tls 	}
    185  1.3.4.2  tls 
    186  1.3.4.2  tls 	/* Sort out the name of the mbr code we need */
    187  1.3.4.2  tls 	if (names > 0 || fl & (NETBSD_NAMED | ACTIVE_NAMED)) {
    188  1.3.4.2  tls 		/* Need bootselect code */
    189  1.3.4.2  tls 		fl |= MBR_BS_ACTIVE;
    190  1.3.4.2  tls 		bootcode = fl & MBR_BS_EXTLBA ? _PATH_BOOTEXT : _PATH_BOOTSEL;
    191  1.3.4.2  tls 	} else
    192  1.3.4.2  tls 		bootcode = _PATH_MBR;
    193  1.3.4.2  tls 
    194  1.3.4.2  tls 	fl &=  MBR_BS_ACTIVE | MBR_BS_EXTLBA;
    195  1.3.4.2  tls 
    196  1.3.4.2  tls 	/* Look at what is installed */
    197  1.3.4.2  tls 	ofl = mbr.mbrb.mbrbs_flags;
    198  1.3.4.2  tls 	if (ofl == 0) {
    199  1.3.4.2  tls 		/* Check there is some bootcode at all... */
    200  1.3.4.2  tls 		if (mbr.mbr.mbr_magic != htole16(MBR_MAGIC) ||
    201  1.3.4.2  tls 		    mbr.mbr.mbr_jmpboot[0] == 0 ||
    202  1.3.4.2  tls 		    mbr_root_above_chs())
    203  1.3.4.2  tls 			/* Existing won't do, force update */
    204  1.3.4.2  tls 			fl |= MBR_BS_NEWMBR;
    205  1.3.4.2  tls 	}
    206  1.3.4.2  tls 	ofl = mbr.oflags & (MBR_BS_ACTIVE | MBR_BS_EXTLBA);
    207  1.3.4.2  tls 
    208  1.3.4.2  tls 	if (fl & ~ofl || (fl == 0 && ofl & MBR_BS_ACTIVE)) {
    209  1.3.4.2  tls 		/* Existing boot code isn't the right one... */
    210  1.3.4.2  tls 		if (fl & MBR_BS_ACTIVE)
    211  1.3.4.2  tls 			msg_display(MSG_installbootsel);
    212  1.3.4.2  tls 		else
    213  1.3.4.2  tls 			msg_display(MSG_installmbr);
    214  1.3.4.2  tls 	} else
    215  1.3.4.2  tls 		/* Existing code would (probably) be ok */
    216  1.3.4.2  tls 		msg_display(MSG_updatembr);
    217  1.3.4.2  tls 
    218  1.3.4.2  tls 	process_menu(MENU_yesno, NULL);
    219  1.3.4.2  tls 	if (!yesno)
    220  1.3.4.2  tls 		/* User doesn't want to update mbr code */
    221  1.3.4.2  tls 		return 1;
    222  1.3.4.2  tls 
    223  1.3.4.2  tls 	if (md_read_bootcode(bootcode, &mbr.mbr) == 0)
    224  1.3.4.2  tls 		/* update suceeded - to memory copy */
    225  1.3.4.2  tls 		return 1;
    226  1.3.4.2  tls 
    227  1.3.4.2  tls 	/* This shouldn't happen since the files are in the floppy fs... */
    228  1.3.4.2  tls 	msg_display("Can't find %s", bootcode);
    229  1.3.4.2  tls 	process_menu(MENU_yesno, NULL);
    230  1.3.4.2  tls 
    231  1.3.4.2  tls 	return 1;
    232  1.3.4.2  tls }
    233  1.3.4.2  tls 
    234  1.3.4.2  tls /*
    235  1.3.4.2  tls  * md back-end code for menu-driven BSD disklabel editor.
    236  1.3.4.2  tls  */
    237  1.3.4.2  tls int
    238  1.3.4.2  tls md_make_bsd_partitions(void)
    239  1.3.4.2  tls {
    240  1.3.4.2  tls 	return make_bsd_partitions();
    241  1.3.4.2  tls }
    242  1.3.4.2  tls 
    243  1.3.4.2  tls /*
    244  1.3.4.2  tls  * any additional partition validation
    245  1.3.4.2  tls  */
    246  1.3.4.2  tls int
    247  1.3.4.2  tls md_check_partitions(void)
    248  1.3.4.2  tls {
    249  1.3.4.2  tls 	int rval;
    250  1.3.4.2  tls 	char *bootxx;
    251  1.3.4.2  tls 
    252  1.3.4.2  tls 	/* check we have boot code for the root partition type */
    253  1.3.4.2  tls 	bootxx = bootxx_name();
    254  1.3.4.2  tls 	rval = access(bootxx, R_OK);
    255  1.3.4.2  tls 	free(bootxx);
    256  1.3.4.2  tls 	if (rval == 0)
    257  1.3.4.2  tls 		return 1;
    258  1.3.4.2  tls 	process_menu(MENU_ok, deconst(MSG_No_Bootcode));
    259  1.3.4.2  tls 	return 0;
    260  1.3.4.2  tls }
    261  1.3.4.2  tls 
    262  1.3.4.2  tls /*
    263  1.3.4.2  tls  * hook called before writing new disklabel.
    264  1.3.4.2  tls  */
    265  1.3.4.2  tls int
    266  1.3.4.2  tls md_pre_disklabel(void)
    267  1.3.4.2  tls {
    268  1.3.4.2  tls 	if (pm->no_mbr)
    269  1.3.4.2  tls 		return 0;
    270  1.3.4.2  tls 
    271  1.3.4.2  tls 	msg_display(MSG_dofdisk);
    272  1.3.4.2  tls 
    273  1.3.4.2  tls 	/* write edited MBR onto disk. */
    274  1.3.4.2  tls 	if (write_mbr(pm->diskdev, &mbr, 1) != 0) {
    275  1.3.4.2  tls 		msg_display(MSG_wmbrfail);
    276  1.3.4.2  tls 		process_menu(MENU_ok, NULL);
    277  1.3.4.2  tls 		return 1;
    278  1.3.4.2  tls 	}
    279  1.3.4.2  tls 	return 0;
    280  1.3.4.2  tls }
    281  1.3.4.2  tls 
    282  1.3.4.2  tls /*
    283  1.3.4.2  tls  * hook called after writing disklabel to new target disk.
    284  1.3.4.2  tls  */
    285  1.3.4.2  tls int
    286  1.3.4.2  tls md_post_disklabel(void)
    287  1.3.4.2  tls {
    288  1.3.4.2  tls 	if (get_ramsize() <= 32)
    289  1.3.4.2  tls 		set_swap(pm->diskdev, pm->bsdlabel);
    290  1.3.4.2  tls 
    291  1.3.4.2  tls 	return 0;
    292  1.3.4.2  tls }
    293  1.3.4.2  tls 
    294  1.3.4.2  tls /*
    295  1.3.4.2  tls  * hook called after upgrade() or install() has finished setting
    296  1.3.4.2  tls  * up the target disk but immediately before the user is given the
    297  1.3.4.2  tls  * ``disks are now set up'' message.
    298  1.3.4.2  tls  */
    299  1.3.4.2  tls int
    300  1.3.4.2  tls md_post_newfs(void)
    301  1.3.4.2  tls {
    302  1.3.4.2  tls 	int ret;
    303  1.3.4.2  tls 	size_t len;
    304  1.3.4.2  tls 	char bootxx[8192 + 4];
    305  1.3.4.2  tls 	char boot_options[1024];
    306  1.3.4.2  tls 	char *bootxx_filename;
    307  1.3.4.2  tls 	/*
    308  1.3.4.2  tls 	 * XXX - this code retains a lot of cruft from when we went
    309  1.3.4.2  tls 	 * to great pains to exclude installboot from the ramdisk
    310  1.3.4.2  tls 	 * for size reasons and should be rewritten.
    311  1.3.4.2  tls 	 */
    312  1.3.4.2  tls 	static const char *consoles[]={
    313  1.3.4.2  tls         	"pc", /* CONSDEV_PC */
    314  1.3.4.2  tls 		"com0", /* CONSDEV_COM0 */
    315  1.3.4.2  tls 		"com1", /* CONSDEV_COM1 */
    316  1.3.4.2  tls 		"com2", /* CONSDEV_COM2 */
    317  1.3.4.2  tls 		"com3", /* CONSDEV_COM3 */
    318  1.3.4.2  tls 		"com0kbd", /* CONSDEV_COM0KBD */
    319  1.3.4.2  tls 		"com1kbd", /* CONSDEV_COM1KBD */
    320  1.3.4.2  tls 		"com2kbd", /* CONSDEV_COM2KBD */
    321  1.3.4.2  tls 		"com3kbd" /* CONSDEV_COM3KBD */ };
    322  1.3.4.2  tls 	static struct x86_boot_params boottype =
    323  1.3.4.2  tls 		{sizeof boottype, 0, 5, 0, 9600, { '\0' }, "", 0};
    324  1.3.4.2  tls 	static int conmib[] = {CTL_MACHDEP, CPU_CONSDEV};
    325  1.3.4.2  tls 	struct termios t;
    326  1.3.4.2  tls 	dev_t condev;
    327  1.3.4.2  tls #define bp (*(struct x86_boot_params *)(bootxx + 512 * 2 + 8))
    328  1.3.4.2  tls 
    329  1.3.4.2  tls 	/*
    330  1.3.4.2  tls 	 * Get console device, should either be ttyE0 or tty0n.
    331  1.3.4.2  tls 	 * Too hard to double check, so just 'know' the device numbers.
    332  1.3.4.2  tls 	 */
    333  1.3.4.2  tls 	len = sizeof condev;
    334  1.3.4.2  tls 	if (sysctl(conmib, nelem(conmib), &condev, &len, NULL, 0) != -1
    335  1.3.4.2  tls 	    && (condev & ~3) == 0x800) {
    336  1.3.4.2  tls 		/* Motherboard serial port */
    337  1.3.4.2  tls 		boottype.bp_consdev = (condev & 3) + 1;
    338  1.3.4.2  tls 		/* Defaulting the baud rate to that of stdin should suffice */
    339  1.3.4.2  tls 		if (tcgetattr(0, &t) != -1)
    340  1.3.4.2  tls 			boottype.bp_conspeed = t.c_ispeed;
    341  1.3.4.2  tls 	}
    342  1.3.4.2  tls 
    343  1.3.4.2  tls 	process_menu(MENU_getboottype, &boottype);
    344  1.3.4.2  tls 	msg_display(MSG_dobootblks, pm->diskdev);
    345  1.3.4.2  tls 	if (bp.bp_consdev == ~0u)
    346  1.3.4.2  tls 		return 0;
    347  1.3.4.2  tls 
    348  1.3.4.2  tls 	ret = cp_to_target("/usr/mdec/boot", "/boot");
    349  1.3.4.2  tls 	if (ret)
    350  1.3.4.2  tls 		return ret;
    351  1.3.4.2  tls 
    352  1.3.4.2  tls         bootxx_filename = bootxx_name();
    353  1.3.4.2  tls         if (bootxx_filename != NULL) {
    354  1.3.4.2  tls 		snprintf(boot_options, sizeof boot_options,
    355  1.3.4.2  tls 		    "console=%s,speed=%u", consoles[boottype.bp_consdev],
    356  1.3.4.2  tls 		    boottype.bp_conspeed);
    357  1.3.4.2  tls 		if (pm->isspecial) {
    358  1.3.4.2  tls                 	ret = run_program(RUN_DISPLAY | RUN_NO_CLEAR,
    359  1.3.4.2  tls                 	    "/usr/sbin/installboot -o %s /dev/r%s %s",
    360  1.3.4.2  tls 			    boot_options, pm->diskdev, bootxx_filename);
    361  1.3.4.2  tls 		} else {
    362  1.3.4.2  tls                 	ret = run_program(RUN_DISPLAY | RUN_NO_CLEAR,
    363  1.3.4.2  tls                 	    "/usr/sbin/installboot -o %s /dev/r%s%c %s",
    364  1.3.4.2  tls 			    boot_options, pm->diskdev, 'a' + pm->rootpart,
    365  1.3.4.2  tls 			    bootxx_filename);
    366  1.3.4.2  tls 		}
    367  1.3.4.2  tls                 free(bootxx_filename);
    368  1.3.4.2  tls         } else
    369  1.3.4.2  tls                 ret = -1;
    370  1.3.4.2  tls 
    371  1.3.4.2  tls         if (ret != 0)
    372  1.3.4.2  tls                 process_menu(MENU_ok,
    373  1.3.4.2  tls                     deconst("Warning: disk is probably not bootable"));
    374  1.3.4.2  tls 
    375  1.3.4.2  tls 	return ret;
    376  1.3.4.2  tls }
    377  1.3.4.2  tls 
    378  1.3.4.2  tls int
    379  1.3.4.2  tls md_post_extract(void)
    380  1.3.4.2  tls {
    381  1.3.4.2  tls 	return 0;
    382  1.3.4.2  tls }
    383  1.3.4.2  tls 
    384  1.3.4.2  tls void
    385  1.3.4.2  tls md_cleanup_install(void)
    386  1.3.4.2  tls {
    387  1.3.4.2  tls #ifndef DEBUG
    388  1.3.4.2  tls 	enable_rc_conf();
    389  1.3.4.2  tls 	add_rc_conf("wscons=YES\n");
    390  1.3.4.2  tls 
    391  1.3.4.2  tls # if defined(__i386__) && defined(SET_KERNEL_TINY)
    392  1.3.4.2  tls 	/*
    393  1.3.4.2  tls 	 * For GENERIC_TINY, do not enable any extra screens or wsmux.
    394  1.3.4.2  tls 	 * Otherwise, run getty on 4 VTs.
    395  1.3.4.2  tls 	 */
    396  1.3.4.2  tls 	if (get_kernel_set() == SET_KERNEL_TINY)
    397  1.3.4.2  tls 		run_program(RUN_CHROOT,
    398  1.3.4.2  tls                             "sed -an -e '/^screen/s/^/#/;/^mux/s/^/#/;"
    399  1.3.4.2  tls 			    "H;$!d;g;w /etc/wscons.conf' /etc/wscons.conf");
    400  1.3.4.2  tls 	else
    401  1.3.4.2  tls # endif
    402  1.3.4.2  tls 		run_program(RUN_CHROOT,
    403  1.3.4.2  tls 			    "sed -an -e '/^ttyE[1-9]/s/off/on/;"
    404  1.3.4.2  tls 			    "H;$!d;g;w /etc/ttys' /etc/ttys");
    405  1.3.4.2  tls 
    406  1.3.4.2  tls #endif
    407  1.3.4.2  tls }
    408  1.3.4.2  tls 
    409  1.3.4.2  tls int
    410  1.3.4.2  tls md_pre_update(void)
    411  1.3.4.2  tls {
    412  1.3.4.2  tls 	if (get_ramsize() <= 8)
    413  1.3.4.2  tls 		set_swap(pm->diskdev, NULL);
    414  1.3.4.2  tls 	return 1;
    415  1.3.4.2  tls }
    416  1.3.4.2  tls 
    417  1.3.4.2  tls /* Upgrade support */
    418  1.3.4.2  tls int
    419  1.3.4.2  tls md_update(void)
    420  1.3.4.2  tls {
    421  1.3.4.2  tls 	md_post_newfs();
    422  1.3.4.2  tls 	md_upgrade_mbrtype();
    423  1.3.4.2  tls 	return 1;
    424  1.3.4.2  tls }
    425  1.3.4.2  tls 
    426  1.3.4.2  tls int
    427  1.3.4.2  tls md_check_mbr(mbr_info_t *mbri)
    428  1.3.4.2  tls {
    429  1.3.4.2  tls 	return 2;
    430  1.3.4.2  tls }
    431  1.3.4.2  tls 
    432  1.3.4.2  tls int
    433  1.3.4.2  tls md_mbr_use_wholedisk(mbr_info_t *mbri)
    434  1.3.4.2  tls {
    435  1.3.4.2  tls 	return mbr_use_wholedisk(mbri);
    436  1.3.4.2  tls }
    437  1.3.4.2  tls 
    438  1.3.4.2  tls static int
    439  1.3.4.2  tls get_bios_info(char *dev)
    440  1.3.4.2  tls {
    441  1.3.4.2  tls 	static struct disklist *disklist = NULL;
    442  1.3.4.2  tls 	static int mib[2] = {CTL_MACHDEP, CPU_DISKINFO};
    443  1.3.4.2  tls 	int i;
    444  1.3.4.2  tls 	size_t len;
    445  1.3.4.2  tls 	struct biosdisk_info *bip;
    446  1.3.4.2  tls 	struct nativedisk_info *nip = NULL, *nat;
    447  1.3.4.2  tls 	int cyl, head;
    448  1.3.4.2  tls 	daddr_t sec;
    449  1.3.4.2  tls 
    450  1.3.4.2  tls 	if (disklist == NULL) {
    451  1.3.4.2  tls 		if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0)
    452  1.3.4.2  tls 			goto nogeom;
    453  1.3.4.2  tls 		disklist = malloc(len);
    454  1.3.4.2  tls 		if (disklist == NULL) {
    455  1.3.4.2  tls 			fprintf(stderr, "Out of memory\n");
    456  1.3.4.2  tls 			return -1;
    457  1.3.4.2  tls 		}
    458  1.3.4.2  tls 		sysctl(mib, 2, disklist, &len, NULL, 0);
    459  1.3.4.2  tls 	}
    460  1.3.4.2  tls 
    461  1.3.4.2  tls 	for (i = 0; i < disklist->dl_nnativedisks; i++) {
    462  1.3.4.2  tls 		nat = &disklist->dl_nativedisks[i];
    463  1.3.4.2  tls 		if (!strcmp(dev, nat->ni_devname)) {
    464  1.3.4.2  tls 			nip = nat;
    465  1.3.4.2  tls 			break;
    466  1.3.4.2  tls 		}
    467  1.3.4.2  tls 	}
    468  1.3.4.2  tls 	if (nip == NULL || nip->ni_nmatches == 0) {
    469  1.3.4.2  tls nogeom:
    470  1.3.4.2  tls 		if (nip != NULL)
    471  1.3.4.2  tls 			msg_display(MSG_nobiosgeom, pm->dlcyl, pm->dlhead, pm->dlsec);
    472  1.3.4.2  tls 		if (guess_biosgeom_from_mbr(&mbr, &cyl, &head, &sec) >= 0
    473  1.3.4.2  tls 		    && nip != NULL)
    474  1.3.4.2  tls 			msg_display_add(MSG_biosguess, cyl, head, sec);
    475  1.3.4.2  tls 		biosdisk = NULL;
    476  1.3.4.2  tls 	} else {
    477  1.3.4.2  tls 		guess_biosgeom_from_mbr(&mbr, &cyl, &head, &sec);
    478  1.3.4.2  tls 		if (nip->ni_nmatches == 1) {
    479  1.3.4.2  tls 			bip = &disklist->dl_biosdisks[nip->ni_biosmatches[0]];
    480  1.3.4.2  tls 			msg_display(MSG_onebiosmatch);
    481  1.3.4.2  tls 			msg_table_add(MSG_onebiosmatch_header);
    482  1.3.4.2  tls 			msg_table_add(MSG_onebiosmatch_row, bip->bi_dev,
    483  1.3.4.2  tls 			    bip->bi_cyl, bip->bi_head, bip->bi_sec,
    484  1.3.4.2  tls 			    (unsigned)bip->bi_lbasecs,
    485  1.3.4.2  tls 			    (unsigned)(bip->bi_lbasecs / (1000000000 / 512)));
    486  1.3.4.2  tls 			msg_display_add(MSG_biosgeom_advise);
    487  1.3.4.2  tls 			biosdisk = bip;
    488  1.3.4.2  tls 			process_menu(MENU_biosonematch, &biosdisk);
    489  1.3.4.2  tls 		} else {
    490  1.3.4.2  tls 			msg_display(MSG_biosmultmatch);
    491  1.3.4.2  tls 			msg_table_add(MSG_biosmultmatch_header);
    492  1.3.4.2  tls 			for (i = 0; i < nip->ni_nmatches; i++) {
    493  1.3.4.2  tls 				bip = &disklist->dl_biosdisks[
    494  1.3.4.2  tls 							nip->ni_biosmatches[i]];
    495  1.3.4.2  tls 				msg_table_add(MSG_biosmultmatch_row, i,
    496  1.3.4.2  tls 				    bip->bi_dev, bip->bi_cyl, bip->bi_head,
    497  1.3.4.2  tls 				    bip->bi_sec, (unsigned)bip->bi_lbasecs,
    498  1.3.4.2  tls 				    (unsigned)bip->bi_lbasecs/(1000000000/512));
    499  1.3.4.2  tls 			}
    500  1.3.4.2  tls 			process_menu(MENU_biosmultmatch, &i);
    501  1.3.4.2  tls 			if (i == -1)
    502  1.3.4.2  tls 				biosdisk = NULL;
    503  1.3.4.2  tls 			else
    504  1.3.4.2  tls 				biosdisk = &disklist->dl_biosdisks[
    505  1.3.4.2  tls 							nip->ni_biosmatches[i]];
    506  1.3.4.2  tls 		}
    507  1.3.4.2  tls 	}
    508  1.3.4.2  tls 	if (biosdisk == NULL) {
    509  1.3.4.2  tls 		if (nip != NULL) {
    510  1.3.4.2  tls 			set_bios_geom(cyl, head, sec);
    511  1.3.4.2  tls 		} else {
    512  1.3.4.2  tls 			bcyl = cyl;
    513  1.3.4.2  tls 			bhead = head;
    514  1.3.4.2  tls 			bsec = sec;
    515  1.3.4.2  tls 		}
    516  1.3.4.2  tls 	} else {
    517  1.3.4.2  tls 		bcyl = biosdisk->bi_cyl;
    518  1.3.4.2  tls 		bhead = biosdisk->bi_head;
    519  1.3.4.2  tls 		bsec = biosdisk->bi_sec;
    520  1.3.4.2  tls 	}
    521  1.3.4.2  tls 	return 0;
    522  1.3.4.2  tls }
    523  1.3.4.2  tls 
    524  1.3.4.2  tls static int
    525  1.3.4.2  tls mbr_root_above_chs(void)
    526  1.3.4.2  tls {
    527  1.3.4.2  tls 	return pm->ptstart + DEFROOTSIZE * (MEG / 512) >= bcyl * bhead * bsec;
    528  1.3.4.2  tls }
    529  1.3.4.2  tls 
    530  1.3.4.2  tls static void
    531  1.3.4.2  tls md_upgrade_mbrtype(void)
    532  1.3.4.2  tls {
    533  1.3.4.2  tls 	struct mbr_partition *mbrp;
    534  1.3.4.2  tls 	int i, netbsdpart = -1, oldbsdpart = -1, oldbsdcount = 0;
    535  1.3.4.2  tls 
    536  1.3.4.2  tls 	if (pm->no_mbr)
    537  1.3.4.2  tls 		return;
    538  1.3.4.2  tls 
    539  1.3.4.2  tls 	if (read_mbr(pm->diskdev, &mbr) < 0)
    540  1.3.4.2  tls 		return;
    541  1.3.4.2  tls 
    542  1.3.4.2  tls 	mbrp = &mbr.mbr.mbr_parts[0];
    543  1.3.4.2  tls 
    544  1.3.4.2  tls 	for (i = 0; i < MBR_PART_COUNT; i++) {
    545  1.3.4.2  tls 		if (mbrp[i].mbrp_type == MBR_PTYPE_386BSD) {
    546  1.3.4.2  tls 			oldbsdpart = i;
    547  1.3.4.2  tls 			oldbsdcount++;
    548  1.3.4.2  tls 		} else if (mbrp[i].mbrp_type == MBR_PTYPE_NETBSD)
    549  1.3.4.2  tls 			netbsdpart = i;
    550  1.3.4.2  tls 	}
    551  1.3.4.2  tls 
    552  1.3.4.2  tls 	if (netbsdpart == -1 && oldbsdcount == 1) {
    553  1.3.4.2  tls 		mbrp[oldbsdpart].mbrp_type = MBR_PTYPE_NETBSD;
    554  1.3.4.2  tls 		write_mbr(pm->diskdev, &mbr, 0);
    555  1.3.4.2  tls 	}
    556  1.3.4.2  tls }
    557  1.3.4.2  tls 
    558  1.3.4.2  tls /*
    559  1.3.4.2  tls  * Read MBR code from a file.
    560  1.3.4.2  tls  * The existing partition table and bootselect configuration is kept.
    561  1.3.4.2  tls  */
    562  1.3.4.2  tls static int
    563  1.3.4.2  tls md_read_bootcode(const char *path, struct mbr_sector *mbrs)
    564  1.3.4.2  tls {
    565  1.3.4.2  tls 	int fd;
    566  1.3.4.2  tls 	struct stat st;
    567  1.3.4.2  tls 	size_t len;
    568  1.3.4.2  tls 	struct mbr_sector new_mbr;
    569  1.3.4.2  tls 	uint32_t dsn;
    570  1.3.4.2  tls 
    571  1.3.4.2  tls 	fd = open(path, O_RDONLY);
    572  1.3.4.2  tls 	if (fd < 0)
    573  1.3.4.2  tls 		return -1;
    574  1.3.4.2  tls 
    575  1.3.4.2  tls 	if (fstat(fd, &st) < 0 || st.st_size != sizeof *mbrs) {
    576  1.3.4.2  tls 		close(fd);
    577  1.3.4.2  tls 		return -1;
    578  1.3.4.2  tls 	}
    579  1.3.4.2  tls 
    580  1.3.4.2  tls 	if (read(fd, &new_mbr, sizeof new_mbr) != sizeof new_mbr) {
    581  1.3.4.2  tls 		close(fd);
    582  1.3.4.2  tls 		return -1;
    583  1.3.4.2  tls 	}
    584  1.3.4.2  tls 	close(fd);
    585  1.3.4.2  tls 
    586  1.3.4.2  tls 	if (new_mbr.mbr_bootsel_magic != htole16(MBR_BS_MAGIC))
    587  1.3.4.2  tls 		return -1;
    588  1.3.4.2  tls 
    589  1.3.4.2  tls 	if (mbrs->mbr_bootsel_magic == htole16(MBR_BS_MAGIC)) {
    590  1.3.4.2  tls 		len = offsetof(struct mbr_sector, mbr_bootsel);
    591  1.3.4.2  tls 	} else
    592  1.3.4.2  tls 		len = offsetof(struct mbr_sector, mbr_parts);
    593  1.3.4.2  tls 
    594  1.3.4.2  tls 	/* Preserve the 'drive serial number' - especially for Vista */
    595  1.3.4.2  tls 	dsn = mbrs->mbr_dsn;
    596  1.3.4.2  tls 	memcpy(mbrs, &new_mbr, len);
    597  1.3.4.2  tls 	mbrs->mbr_dsn = dsn;
    598  1.3.4.2  tls 
    599  1.3.4.2  tls 	/* Keep flags from object file - indicate the properties */
    600  1.3.4.2  tls 	mbrs->mbr_bootsel.mbrbs_flags = new_mbr.mbr_bootsel.mbrbs_flags;
    601  1.3.4.2  tls 	mbrs->mbr_magic = htole16(MBR_MAGIC);
    602  1.3.4.2  tls 
    603  1.3.4.2  tls 	return 0;
    604  1.3.4.2  tls }
    605  1.3.4.2  tls 
    606  1.3.4.2  tls static unsigned int
    607  1.3.4.2  tls get_bootmodel(void)
    608  1.3.4.2  tls {
    609  1.3.4.2  tls #if defined(__i386__)
    610  1.3.4.2  tls 	struct utsname ut;
    611  1.3.4.2  tls #ifdef DEBUG
    612  1.3.4.2  tls 	char *envstr;
    613  1.3.4.2  tls 
    614  1.3.4.2  tls 	envstr = getenv("BOOTMODEL");
    615  1.3.4.2  tls 	if (envstr != NULL)
    616  1.3.4.2  tls 		return atoi(envstr);
    617  1.3.4.2  tls #endif
    618  1.3.4.2  tls 
    619  1.3.4.2  tls 	if (uname(&ut) < 0)
    620  1.3.4.2  tls 		ut.version[0] = 0;
    621  1.3.4.2  tls 
    622  1.3.4.2  tls #if defined(SET_KERNEL_TINY)
    623  1.3.4.2  tls 	if (strstr(ut.version, "TINY") != NULL)
    624  1.3.4.2  tls 		return SET_KERNEL_TINY;
    625  1.3.4.2  tls #endif
    626  1.3.4.2  tls #if defined(SET_KERNEL_PS2)
    627  1.3.4.2  tls 	if (strstr(ut.version, "PS2") != NULL)
    628  1.3.4.2  tls 		return SET_KERNEL_PS2;
    629  1.3.4.2  tls #endif
    630  1.3.4.2  tls #endif
    631  1.3.4.2  tls 	return SET_KERNEL_GENERIC;
    632  1.3.4.2  tls }
    633  1.3.4.2  tls 
    634  1.3.4.2  tls 
    635  1.3.4.2  tls int
    636  1.3.4.2  tls md_pre_mount()
    637  1.3.4.2  tls {
    638  1.3.4.2  tls 	return 0;
    639  1.3.4.2  tls }
    640