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