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