Home | History | Annotate | Line # | Download | only in i386
md.c revision 1.21
      1 /*	$NetBSD: md.c,v 1.21 2019/08/14 12:55:36 martin Exp $ */
      2 
      3 /*
      4  * Copyright 1997 Piermont Information Systems Inc.
      5  * All rights reserved.
      6  *
      7  * Based on code written by Philip A. Nelson for Piermont Information
      8  * Systems Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
     19  *    or promote products derived from this software without specific prior
     20  *    written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /* md.c -- i386 machine specific routines - also used by amd64 */
     36 
     37 #include <sys/param.h>
     38 #include <sys/sysctl.h>
     39 #include <sys/exec.h>
     40 #include <sys/utsname.h>
     41 #include <sys/types.h>
     42 #include <sys/stat.h>
     43 #include <machine/cpu.h>
     44 #include <assert.h>
     45 #include <stdio.h>
     46 #include <stddef.h>
     47 #include <util.h>
     48 #include <dirent.h>
     49 #include <termios.h>
     50 
     51 #include "defs.h"
     52 #include "md.h"
     53 #include "endian.h"
     54 #include "msg_defs.h"
     55 #include "menu_defs.h"
     56 
     57 #ifdef NO_LBA_READS		/* for testing */
     58 #undef BIFLAG_EXTINT13
     59 #define BIFLAG_EXTINT13	0
     60 #endif
     61 
     62 static struct biosdisk_info *biosdisk = NULL;
     63 static bool uefi_boot;
     64 
     65 /* prototypes */
     66 
     67 static bool get_bios_info(const char*, struct disk_partitions*, int*, int*, int*);
     68 static int mbr_root_above_chs(void);
     69 static int md_read_bootcode(const char *, struct mbr_sector *);
     70 static unsigned int get_bootmodel(void);
     71 
     72 static int conmib[] = { CTL_MACHDEP, CPU_CONSDEV };
     73 
     74 #define	BOOT_PART	(128*(MEG/512))
     75 #define	BOOT_PART_TYPE	PT_EFI_SYSTEM
     76 
     77 static const char * uefi_bootloaders[] = {
     78 	"/usr/mdec/bootia32.efi",
     79 	"/usr/mdec/bootx64.efi",
     80 };
     81 
     82 void
     83 md_init(void)
     84 {
     85 	char boot_method[100];
     86 	size_t len;
     87 
     88 	len = sizeof(boot_method);
     89 	if (sysctlbyname("machdep.bootmethod", boot_method, &len, NULL, 0)
     90 	    != -1) {
     91 		if (strcmp(boot_method, "BIOS") == 0)
     92 			uefi_boot = false;
     93 		else if (strcmp(boot_method, "UEFI") == 0)
     94 			uefi_boot = true;
     95 	}
     96 }
     97 
     98 void
     99 md_init_set_status(int flags)
    100 {
    101 	(void)flags;
    102 
    103 	/* Default to install same type of kernel as we are running */
    104 	set_kernel_set(get_bootmodel());
    105 }
    106 
    107 bool
    108 md_get_info(struct install_partition_desc *install)
    109 {
    110 	int bcyl = 0, bhead = 0, bsec = 0;
    111 
    112 	if (pm->no_mbr || pm->no_part)
    113 		return true;
    114 
    115 	if (pm->parts == NULL) {
    116 
    117 		const struct disk_partitioning_scheme *ps =
    118 		    select_part_scheme(pm, NULL, true, NULL);
    119 
    120 		if (!ps)
    121 			return false;
    122 
    123 		struct disk_partitions *parts =
    124 		   (*ps->create_new_for_disk)(pm->diskdev,
    125 		   0, pm->dlsize, pm->dlsize, true);
    126 		if (!parts)
    127 			return false;
    128 
    129 		pm->parts = parts;
    130 		if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
    131 			pm->dlsize = ps->size_limit;
    132 	}
    133 
    134 	if (get_bios_info(pm->diskdev, pm->parts, &bcyl, &bhead, &bsec)
    135 	    && pm->parts->pscheme->change_disk_geom != NULL)
    136 		pm->parts->pscheme->change_disk_geom(pm->parts,
    137 		    bcyl, bhead, bsec);
    138 	else
    139 		set_default_sizemult(MEG/512);
    140 
    141 	/*
    142 	 * If the selected scheme does not need two-stage partitioning
    143 	 * (like GPT), do not bother to edit the outer partitions.
    144 	 */
    145 	if (pm->parts->pscheme->secondary_partitions == NULL ||
    146 	    pm->parts->pscheme->secondary_scheme == NULL)
    147 		return true;
    148 
    149 	if (pm->no_mbr || pm->no_part)
    150 		return true;
    151 
    152 	return edit_outer_parts(pm->parts);
    153 }
    154 
    155 /*
    156  * md back-end code for menu-driven BSD disklabel editor.
    157  */
    158 bool
    159 md_make_bsd_partitions(struct install_partition_desc *install)
    160 {
    161 	return make_bsd_partitions(install);
    162 }
    163 
    164 /*
    165  * any additional partition validation
    166  */
    167 bool
    168 md_check_partitions(struct install_partition_desc *install)
    169 {
    170 	int rval;
    171 	char *bootxx;
    172 
    173 	/* if booting via UEFI no boot blocks are needed */
    174 	if (uefi_boot)
    175 		return true;
    176 
    177 	/* check we have boot code for the root partition type */
    178 	bootxx = bootxx_name(install);
    179 	rval = access(bootxx, R_OK);
    180 	free(bootxx);
    181 	if (rval == 0)
    182 		return true;
    183 	process_menu(MENU_ok, __UNCONST(MSG_No_Bootcode));
    184 	return false;
    185 }
    186 
    187 /*
    188  * hook called before writing new disklabel.
    189  */
    190 bool
    191 md_pre_disklabel(struct install_partition_desc *install,
    192     struct disk_partitions *parts)
    193 {
    194 
    195 	if (parts->parent == NULL)
    196 		return true;	/* no outer partitions */
    197 
    198 	parts = parts->parent;
    199 
    200 	msg_display_subst(MSG_dofdisk, 3, parts->disk,
    201 	    msg_string(parts->pscheme->name),
    202 	    msg_string(parts->pscheme->short_name));
    203 
    204 	/* write edited "MBR" onto disk. */
    205 	if (!parts->pscheme->write_to_disk(parts)) {
    206 		msg_display(MSG_wmbrfail);
    207 		process_menu(MENU_ok, NULL);
    208 		return false;
    209 	}
    210 	return true;
    211 }
    212 
    213 /*
    214  * hook called after writing disklabel to new target disk.
    215  */
    216 bool
    217 md_post_disklabel(struct install_partition_desc *install,
    218     struct disk_partitions *parts)
    219 {
    220 	return true;
    221 }
    222 
    223 /*
    224  * Do all legacy bootblock update/setup here
    225  */
    226 static int
    227 md_post_newfs_bios(struct install_partition_desc *install)
    228 {
    229 	int ret;
    230 	size_t len;
    231 	char boot_options[1024];
    232 	char *bootxx_filename;
    233 	/*
    234 	 * XXX - this code retains a lot of cruft from when we went
    235 	 * to great pains to exclude installboot from the ramdisk
    236 	 * for size reasons and should be rewritten.
    237 	 */
    238 	static const char *consoles[]={
    239         	"pc", /* CONSDEV_PC */
    240 		"com0", /* CONSDEV_COM0 */
    241 		"com1", /* CONSDEV_COM1 */
    242 		"com2", /* CONSDEV_COM2 */
    243 		"com3", /* CONSDEV_COM3 */
    244 		"com0kbd", /* CONSDEV_COM0KBD */
    245 		"com1kbd", /* CONSDEV_COM1KBD */
    246 		"com2kbd", /* CONSDEV_COM2KBD */
    247 		"com3kbd" /* CONSDEV_COM3KBD */ };
    248 	static struct x86_boot_params boottype =
    249 		{sizeof boottype, 0, 5, 0, 9600, { '\0' }, "", 0};
    250 	struct termios t;
    251 	dev_t condev;
    252 
    253 	if (pm == NULL || !pm->no_part) {
    254 		/*
    255 		 * Get console device, should either be ttyE0 or tty0n.
    256 		 * Too hard to double check, so just 'know' the device numbers.
    257 		 */
    258 		len = sizeof condev;
    259 		if (sysctl(conmib, __arraycount(conmib), &condev, &len, NULL, 0) != -1
    260 		    && (condev & ~3) == 0x800) {
    261 			/* Motherboard serial port */
    262 			boottype.bp_consdev = (condev & 3) + 1;
    263 			/* Defaulting the baud rate to that of stdin should suffice */
    264 			if (tcgetattr(0, &t) != -1)
    265 				boottype.bp_conspeed = t.c_ispeed;
    266 		}
    267 
    268 		process_menu(MENU_getboottype, &boottype);
    269 		msg_fmt_display(MSG_dobootblks, "%s", pm->diskdev);
    270 		if (boottype.bp_consdev == ~0u)
    271 			/* Use existing bootblocks */
    272 			return 0;
    273 	}
    274 
    275 	ret = cp_to_target("/usr/mdec/boot", "/boot");
    276 	if (ret)
    277 		return ret;
    278 	if (pm && pm->no_part)
    279 		return 0;
    280 
    281         bootxx_filename = bootxx_name(install);
    282         if (bootxx_filename != NULL) {
    283 		char rdev[PATH_MAX];
    284 
    285 		install->infos[0].parts->pscheme->get_part_device(
    286 		    install->infos[0].parts, install->infos[0].cur_part_id,
    287 		    rdev, sizeof rdev, NULL, raw_dev_name, true);
    288 
    289 		snprintf(boot_options, sizeof boot_options,
    290 		    "console=%s,speed=%u", consoles[boottype.bp_consdev],
    291 		    boottype.bp_conspeed);
    292                	ret = run_program(RUN_DISPLAY,
    293                 	    "/usr/sbin/installboot -o %s %s %s",
    294 			    boot_options, rdev, bootxx_filename);
    295                 free(bootxx_filename);
    296         } else {
    297                 ret = -1;
    298 	}
    299 
    300         if (ret != 0)
    301                 process_menu(MENU_ok,
    302                     __UNCONST("Warning: disk is probably not bootable"));
    303 
    304 	return ret;
    305 }
    306 
    307 /*
    308  * Make sure our bootloader(s) are in the proper directory in the boot
    309  * boot partition (or update them).
    310  */
    311 static int
    312 copy_uefi_boot(const struct part_usage_info *boot)
    313 {
    314 	char dev[MAXPATHLEN], path[MAXPATHLEN];
    315 	size_t i;
    316 	int err;
    317 
    318 	if (!boot->parts->pscheme->get_part_device(boot->parts,
    319 	    boot->cur_part_id, dev, sizeof(dev), NULL, plain_name, true))
    320 		return -1;
    321 
    322 	/*
    323 	 * We should have a valid file system on that partition.
    324 	 * Try to mount it and check if there is a /EFI in there.
    325 	 */
    326 	if (boot->mount[0])
    327 		strlcpy(path, boot->mount, sizeof(path));
    328 	else
    329 		strcpy(path, "/mnt");
    330 
    331 	if (!(boot->instflags & PUIINST_MOUNT)) {
    332 		make_target_dir(path);
    333 		err = target_mount("", dev, path);
    334 		if (err != 0)
    335 			return err;
    336 	}
    337 
    338 	strlcat(path, "/EFI/boot", sizeof(path));
    339 	make_target_dir(path);
    340 
    341 	for (i = 0; i < __arraycount(uefi_bootloaders); i++) {
    342 		if (access(uefi_bootloaders[i], R_OK) != 0)
    343 			continue;
    344 		err = cp_to_target(uefi_bootloaders[i], path);
    345 		if (err)
    346 			return err;
    347 	}
    348 
    349 	return 0;
    350 }
    351 
    352 /*
    353  * Find (U)EFI boot partition and install/update bootloaders
    354  */
    355 static int
    356 md_post_newfs_uefi(struct install_partition_desc *install)
    357 {
    358 	size_t i;
    359 
    360 	for (i = 0; i < install->num; i++) {
    361 		if (!(install->infos[i].instflags & PUIINST_BOOT))
    362 			continue;
    363 
    364 		return copy_uefi_boot(&install->infos[i]);
    365 	}
    366 
    367 	return -1;	/* no EFI boot partition found */
    368 }
    369 
    370 /*
    371  * hook called after upgrade() or install() has finished setting
    372  * up the target disk but immediately before the user is given the
    373  * ``disks are now set up'' message.
    374  */
    375 int
    376 md_post_newfs(struct install_partition_desc *install)
    377 {
    378 
    379 	return uefi_boot ? md_post_newfs_uefi(install)
    380 	    : md_post_newfs_bios(install);
    381 }
    382 
    383 int
    384 md_post_extract(struct install_partition_desc *install)
    385 {
    386 	return 0;
    387 }
    388 
    389 void
    390 md_cleanup_install(struct install_partition_desc *install)
    391 {
    392 	size_t len;
    393 	dev_t condev;
    394 
    395 #ifndef DEBUG
    396 	enable_rc_conf();
    397 	add_rc_conf("wscons=YES\n");
    398 
    399 # if defined(__i386__) && defined(SET_KERNEL_TINY)
    400 	/*
    401 	 * For GENERIC_TINY, do not enable any extra screens or wsmux.
    402 	 * Otherwise, run getty on 4 VTs.
    403 	 */
    404 	if (get_kernel_set() == SET_KERNEL_TINY)
    405 		run_program(RUN_CHROOT,
    406                             "sed -an -e '/^screen/s/^/#/;/^mux/s/^/#/;"
    407 			    "H;$!d;g;w /etc/wscons.conf' /etc/wscons.conf");
    408 	else
    409 # endif
    410 		run_program(RUN_CHROOT,
    411 			    "sed -an -e '/^ttyE[1-9]/s/off/on/;"
    412 			    "H;$!d;g;w /etc/ttys' /etc/ttys");
    413 
    414 #endif
    415 
    416 	/*
    417 	 * Get console device, should either be ttyE0 or tty0n.
    418 	 * Too hard to double check, so just 'know' the device numbers.
    419 	 */
    420 	len = sizeof condev;
    421 	if (sysctl(conmib, __arraycount(conmib), &condev, &len, NULL, 0) != -1
    422 	    && (condev & ~3) != 0x800) {
    423 
    424 		/*
    425 		 * Current console is not com*, assume ttyE*.
    426 		 * Modify /etc/ttys to use wsvt25 for all ports.
    427 		 */
    428 
    429 		run_program(RUN_CHROOT,
    430 			    "sed -an -e 's/vt100/wsvt25/g;"
    431 			    "H;$!d;g;w  /etc/ttys' /etc/ttys");
    432 	}
    433 }
    434 
    435 int
    436 md_pre_update(struct install_partition_desc *install)
    437 {
    438 	return 1;
    439 }
    440 
    441 /* Upgrade support */
    442 int
    443 md_update(struct install_partition_desc *install)
    444 {
    445 	md_post_newfs(install);
    446 	return 1;
    447 }
    448 
    449 int
    450 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
    451 {
    452 	mbr_info_t *ext;
    453 	struct mbr_partition *p;
    454 	const char *bootcode;
    455 	int i, names, fl, ofl;
    456 #define	ACTIVE_FOUND	0x0100
    457 #define	NETBSD_ACTIVE	0x0200
    458 #define	NETBSD_NAMED	0x0400
    459 #define	ACTIVE_NAMED	0x0800
    460 
    461 	root_limit = 0;
    462 	if (biosdisk != NULL && (biosdisk->bi_flags & BIFLAG_EXTINT13) == 0) {
    463 		if (mbr_root_above_chs()) {
    464 			if (quiet)
    465 				return 0;
    466 			msg_display(MSG_partabovechs);
    467 			if (!ask_noyes(NULL))
    468 				return 1;
    469 			/* The user is shooting themselves in the foot here...*/
    470 		} else {
    471 			if (parts->pscheme->size_limit)
    472 				root_limit = min(parts->pscheme->size_limit,
    473 				    parts->disk_size);
    474 			else
    475 				root_limit = parts->disk_size;
    476 		}
    477 	}
    478 
    479 	/*
    480 	 * Ensure the install partition (at sector pm->ptstart) and the active
    481 	 * partition are bootable.
    482 	 * Determine whether the bootselect code is needed.
    483 	 * Note that MBR_BS_NEWMBR is always set, so we ignore it!
    484 	 */
    485 	fl = 0;
    486 	names = 0;
    487 	for (ext = mbri; ext != NULL; ext = ext->extended) {
    488 		p = ext->mbr.mbr_parts;
    489 		for (i = 0; i < MBR_PART_COUNT; p++, i++) {
    490 			if (p->mbrp_flag == MBR_PFLAG_ACTIVE) {
    491 				fl |= ACTIVE_FOUND;
    492 			    if (ext->sector + p->mbrp_start == pm->ptstart)
    493 				fl |= NETBSD_ACTIVE;
    494 			}
    495 			if (ext->mbrb.mbrbs_nametab[i][0] == 0) {
    496 				/* No bootmenu label... */
    497 				if (ext->sector == 0)
    498 					continue;
    499 				if (ext->sector + p->mbrp_start == pm->ptstart)
    500 					/*
    501 					 * Have installed into an extended ptn
    502 					 * force name & bootsel...
    503 					 */
    504 					names++;
    505 				continue;
    506 			}
    507 			/* Partition has a bootmenu label... */
    508 			if (ext->sector != 0)
    509 				fl |= MBR_BS_EXTLBA;
    510 			if (ext->sector + p->mbrp_start == pm->ptstart)
    511 				fl |= NETBSD_NAMED;
    512 			else if (p->mbrp_flag == MBR_PFLAG_ACTIVE)
    513 				fl |= ACTIVE_NAMED;
    514 			else
    515 				names++;
    516 		}
    517 	}
    518 	if (!(fl & ACTIVE_FOUND))
    519 		fl |= NETBSD_ACTIVE;
    520 	if (fl & NETBSD_NAMED && fl & NETBSD_ACTIVE)
    521 		fl |= ACTIVE_NAMED;
    522 
    523 	if ((names > 0 || !(fl & NETBSD_ACTIVE)) &&
    524 	    (!(fl & NETBSD_NAMED) || !(fl & ACTIVE_NAMED))) {
    525 		/*
    526 		 * There appear to be multiple bootable partitions, but they
    527 		 * don't all have bootmenu texts.
    528 		 */
    529 		if (quiet)
    530 			return 0;
    531 
    532 		msg_display(MSG_missing_bootmenu_text);
    533 		if (ask_yesno(NULL))
    534 			return 1;
    535 	}
    536 
    537 	if ((fl & MBR_BS_EXTLBA) &&
    538 	    (biosdisk == NULL || !(biosdisk->bi_flags & BIFLAG_EXTINT13))) {
    539 		/* Need unsupported LBA reads to read boot sectors */
    540 		if (quiet)
    541 			return 0;
    542 
    543 		msg_display(MSG_no_extended_bootmenu);
    544 		if (!ask_noyes(NULL))
    545 			return 1;
    546 	}
    547 
    548 	/* Sort out the name of the mbr code we need */
    549 	if (names > 0 || fl & (NETBSD_NAMED | ACTIVE_NAMED)) {
    550 		/* Need bootselect code */
    551 		fl |= MBR_BS_ACTIVE;
    552 		bootcode = fl & MBR_BS_EXTLBA ? _PATH_BOOTEXT : _PATH_BOOTSEL;
    553 	} else
    554 		bootcode = _PATH_MBR;
    555 
    556 	fl &=  MBR_BS_ACTIVE | MBR_BS_EXTLBA;
    557 
    558 	/* Look at what is installed */
    559 	ofl = mbri->mbrb.mbrbs_flags;
    560 	if (ofl == 0) {
    561 		/* Check there is some bootcode at all... */
    562 		if (mbri->mbr.mbr_magic != htole16(MBR_MAGIC) ||
    563 		    mbri->mbr.mbr_jmpboot[0] == 0 ||
    564 		    mbr_root_above_chs())
    565 			/* Existing won't do, force update */
    566 			fl |= MBR_BS_NEWMBR;
    567 	}
    568 	ofl = mbri->oflags & (MBR_BS_ACTIVE | MBR_BS_EXTLBA);
    569 
    570 	if (!quiet) {
    571 		if (fl & ~ofl || (fl == 0 && ofl & MBR_BS_ACTIVE)) {
    572 			/* Existing boot code isn't the right one... */
    573 			if (fl & MBR_BS_ACTIVE)
    574 				msg_display(MSG_installbootsel);
    575 			else
    576 				msg_display(MSG_installmbr);
    577 		} else
    578 			/* Existing code would (probably) be ok */
    579 			msg_display(MSG_updatembr);
    580 
    581 		if (!ask_yesno(NULL))
    582 			/* User doesn't want to update mbr code */
    583 			return 2;
    584 	}
    585 
    586 	if (md_read_bootcode(bootcode, &mbri->mbr) == 0)
    587 		/* update suceeded - to memory copy */
    588 		return 2;
    589 
    590 	/* This shouldn't happen since the files are in the floppy fs... */
    591 	msg_fmt_display("Can't find %s", "%s", bootcode);
    592 	return ask_reedit(parts);
    593 }
    594 
    595 bool
    596 md_parts_use_wholedisk(struct disk_partitions *parts)
    597 {
    598 	struct disk_part_info boot_part = {
    599 		.size = BOOT_PART,
    600 		.fs_type = FS_MSDOS, .fs_sub_type = MBR_PTYPE_FAT32L,
    601 	};
    602 
    603 	if (!uefi_boot)
    604 		return parts_use_wholedisk(parts, 0, NULL);
    605 
    606 	boot_part.nat_type = parts->pscheme->get_generic_part_type(
    607 	    PT_EFI_SYSTEM);
    608 
    609 	return parts_use_wholedisk(parts, 1, &boot_part);
    610 }
    611 
    612 static bool
    613 get_bios_info(const char *dev, struct disk_partitions *parts, int *bcyl,
    614     int *bhead, int *bsec)
    615 {
    616 	static struct disklist *disklist = NULL;
    617 	static int mib[2] = {CTL_MACHDEP, CPU_DISKINFO};
    618 	int i;
    619 	size_t len;
    620 	struct biosdisk_info *bip;
    621 	struct nativedisk_info *nip = NULL, *nat;
    622 	int cyl, head, sec;
    623 
    624 	if (disklist == NULL) {
    625 		if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0)
    626 			goto nogeom;
    627 		disklist = malloc(len);
    628 		if (disklist == NULL) {
    629 			fprintf(stderr, "Out of memory\n");
    630 			return false;
    631 		}
    632 		sysctl(mib, 2, disklist, &len, NULL, 0);
    633 	}
    634 
    635 	for (i = 0; i < disklist->dl_nnativedisks; i++) {
    636 		nat = &disklist->dl_nativedisks[i];
    637 		if (!strcmp(dev, nat->ni_devname)) {
    638 			nip = nat;
    639 			break;
    640 		}
    641 	}
    642 	if (nip == NULL || nip->ni_nmatches == 0) {
    643 nogeom:
    644 		if (nip != NULL)
    645 			msg_fmt_display(MSG_nobiosgeom, "%d%d%d",
    646 			    pm->dlcyl, pm->dlhead, pm->dlsec);
    647 		if (guess_biosgeom_from_parts(parts, &cyl, &head, &sec) >= 0
    648 		    && nip != NULL)
    649 		{
    650 			msg_fmt_display_add(MSG_biosguess, "%d%d%d",
    651 			    cyl, head, sec);
    652 		}
    653 		biosdisk = NULL;
    654 	} else {
    655 		guess_biosgeom_from_parts(parts, &cyl, &head, &sec);
    656 		if (nip->ni_nmatches == 1) {
    657 			bip = &disklist->dl_biosdisks[nip->ni_biosmatches[0]];
    658 			msg_display(MSG_onebiosmatch);
    659 			msg_table_add(MSG_onebiosmatch_header);
    660 			msg_fmt_table_add(MSG_onebiosmatch_row, "%d%d%d%d%u%u",
    661 			    bip->bi_dev, bip->bi_cyl, bip->bi_head, bip->bi_sec,
    662 			    (unsigned)bip->bi_lbasecs,
    663 			    (unsigned)(bip->bi_lbasecs / (1000000000 / 512)));
    664 			msg_display_add(MSG_biosgeom_advise);
    665 			biosdisk = bip;
    666 			process_menu(MENU_biosonematch, &biosdisk);
    667 		} else {
    668 			msg_display(MSG_biosmultmatch);
    669 			msg_table_add(MSG_biosmultmatch_header);
    670 			for (i = 0; i < nip->ni_nmatches; i++) {
    671 				bip = &disklist->dl_biosdisks[
    672 							nip->ni_biosmatches[i]];
    673 				msg_fmt_table_add(MSG_biosmultmatch_row,
    674 				    "%d%d%d%d%d%u%u", i,
    675 				    bip->bi_dev, bip->bi_cyl, bip->bi_head,
    676 				    bip->bi_sec, (unsigned)bip->bi_lbasecs,
    677 				    (unsigned)bip->bi_lbasecs/(1000000000/512));
    678 			}
    679 			process_menu(MENU_biosmultmatch, &i);
    680 			if (i == -1)
    681 				biosdisk = NULL;
    682 			else
    683 				biosdisk = &disklist->dl_biosdisks[
    684 							nip->ni_biosmatches[i]];
    685 		}
    686 	}
    687 	if (biosdisk == NULL) {
    688 		*bcyl = cyl;
    689 		*bhead = head;
    690 		*bsec = sec;
    691 		if (nip != NULL)
    692 			set_bios_geom(parts, bcyl, bhead, bsec);
    693 	} else {
    694 		*bcyl = biosdisk->bi_cyl;
    695 		*bhead = biosdisk->bi_head;
    696 		*bsec = biosdisk->bi_sec;
    697 	}
    698 	return true;
    699 }
    700 
    701 static int
    702 mbr_root_above_chs(void)
    703 {
    704 	return pm->ptstart + (daddr_t)DEFROOTSIZE * (daddr_t)(MEG / 512)
    705 	    >= pm->max_chs;
    706 }
    707 
    708 /* returns false if no write-back of parts is required */
    709 bool
    710 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
    711 {
    712 	struct mbr_partition *mbrp;
    713 	int i, netbsdpart = -1, oldbsdpart = -1, oldbsdcount = 0;
    714 
    715 	if (pm->no_mbr || pm->no_part)
    716 		return false;
    717 
    718 	mbrp = &mbri->mbr.mbr_parts[0];
    719 
    720 	for (i = 0; i < MBR_PART_COUNT; i++) {
    721 		if (mbrp[i].mbrp_type == MBR_PTYPE_386BSD) {
    722 			oldbsdpart = i;
    723 			oldbsdcount++;
    724 		} else if (mbrp[i].mbrp_type == MBR_PTYPE_NETBSD)
    725 			netbsdpart = i;
    726 	}
    727 
    728 	if (netbsdpart == -1 && oldbsdcount == 1) {
    729 		mbrp[oldbsdpart].mbrp_type = MBR_PTYPE_NETBSD;
    730 		return true;
    731 	}
    732 
    733 	return false;
    734 }
    735 
    736 /*
    737  * Read MBR code from a file.
    738  * The existing partition table and bootselect configuration is kept.
    739  */
    740 static int
    741 md_read_bootcode(const char *path, struct mbr_sector *mbrs)
    742 {
    743 	int fd;
    744 	struct stat st;
    745 	size_t len;
    746 	struct mbr_sector new_mbr;
    747 	uint32_t dsn;
    748 
    749 	fd = open(path, O_RDONLY);
    750 	if (fd < 0)
    751 		return -1;
    752 
    753 	if (fstat(fd, &st) < 0 || st.st_size != sizeof *mbrs) {
    754 		close(fd);
    755 		return -1;
    756 	}
    757 
    758 	if (read(fd, &new_mbr, sizeof new_mbr) != sizeof new_mbr) {
    759 		close(fd);
    760 		return -1;
    761 	}
    762 	close(fd);
    763 
    764 	if (new_mbr.mbr_bootsel_magic != htole16(MBR_BS_MAGIC))
    765 		return -1;
    766 
    767 	if (mbrs->mbr_bootsel_magic == htole16(MBR_BS_MAGIC)) {
    768 		len = offsetof(struct mbr_sector, mbr_bootsel);
    769 	} else
    770 		len = offsetof(struct mbr_sector, mbr_parts);
    771 
    772 	/* Preserve the 'drive serial number' - especially for Vista */
    773 	dsn = mbrs->mbr_dsn;
    774 	memcpy(mbrs, &new_mbr, len);
    775 	mbrs->mbr_dsn = dsn;
    776 
    777 	/* Keep flags from object file - indicate the properties */
    778 	mbrs->mbr_bootsel.mbrbs_flags = new_mbr.mbr_bootsel.mbrbs_flags;
    779 	mbrs->mbr_magic = htole16(MBR_MAGIC);
    780 
    781 	return 0;
    782 }
    783 
    784 static unsigned int
    785 get_bootmodel(void)
    786 {
    787 #if defined(__i386__)
    788 	struct utsname ut;
    789 #ifdef DEBUG
    790 	char *envstr;
    791 
    792 	envstr = getenv("BOOTMODEL");
    793 	if (envstr != NULL)
    794 		return atoi(envstr);
    795 #endif
    796 
    797 	if (uname(&ut) < 0)
    798 		ut.version[0] = 0;
    799 
    800 #if defined(SET_KERNEL_TINY)
    801 	if (strstr(ut.version, "TINY") != NULL)
    802 		return SET_KERNEL_TINY;
    803 #endif
    804 #if defined(SET_KERNEL_PS2)
    805 	if (strstr(ut.version, "PS2") != NULL)
    806 		return SET_KERNEL_PS2;
    807 #endif
    808 #endif
    809 	return SET_KERNEL_GENERIC;
    810 }
    811 
    812 
    813 int
    814 md_pre_mount(struct install_partition_desc *install, size_t ndx)
    815 {
    816 	return 0;
    817 }
    818 
    819 #ifdef HAVE_GPT
    820 /*
    821  * New GPT partitions have been written, update bootloader or remember
    822  * data untill needed in md_post_newfs
    823  */
    824 bool
    825 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
    826     bool root_is_new, part_id efi_id, bool efi_is_new)
    827 {
    828 	struct disk_part_info info;
    829 
    830 	assert(efi_id == NO_PART);	/* XXX EFI support still missing */
    831 
    832 	if (root_id != NO_PART) {
    833 		/* we always update the gpt boot record for now */
    834 		if (!parts->pscheme->get_part_info(parts, root_id, &info))
    835 			return false;
    836 		if (run_program(RUN_SILENT, "gpt biosboot -b %" PRIu64 " %s",
    837 		    info.start, parts->disk) != 0)
    838 			return false;
    839 	}
    840 
    841 	return true;
    842 }
    843 #endif
    844 
    845 /*
    846  * When we do an UEFI install, we have completely different default
    847  * partitions and need to adjust the description at runtime.
    848  */
    849 void
    850 x86_md_part_defaults(struct pm_devs *cur_pm, struct part_usage_info **partsp,
    851     size_t *num_usage_infos)
    852 {
    853 	static const struct part_usage_info uefi_boot_part =
    854 	{
    855 		.size = BOOT_PART,
    856 		.type = BOOT_PART_TYPE,
    857 		.instflags = PUIINST_NEWFS|PUIINST_BOOT,
    858 		.fs_type = FS_MSDOS, .fs_version = MBR_PTYPE_FAT32L,
    859 		.flags = PUIFLAG_ADD_OUTER,
    860 	};
    861 
    862 	struct disk_partitions *parts;
    863 	struct part_usage_info *new_usage, *boot;
    864 	struct disk_part_info info;
    865 	size_t num;
    866 	part_id pno;
    867 
    868 	if (!uefi_boot)
    869 		return;		/* legacy defaults apply */
    870 
    871 	/*
    872 	 * Insert a UEFI boot partition at the beginning of the array
    873 	 */
    874 
    875 	/* create space for new description */
    876 	num = *num_usage_infos + 1;
    877 	new_usage = realloc(*partsp, sizeof(*new_usage)*num);
    878 	if (new_usage == NULL)
    879 		return;
    880 	*partsp = new_usage;
    881 	*num_usage_infos = num;
    882 	boot = new_usage;
    883 	memmove(boot+1, boot, sizeof(*boot)*(num-1));
    884 	*boot = uefi_boot_part;
    885 
    886 	/*
    887 	 * Check if the UEFI partition already exists
    888 	 */
    889 	parts = pm->parts;
    890 	if (parts->parent != NULL)
    891 		parts = parts->parent;
    892 	for (pno = 0; pno < parts->num_part; pno++) {
    893 		if (!parts->pscheme->get_part_info(parts, pno, &info))
    894 			continue;
    895 		if (info.nat_type->generic_ptype != boot->type)
    896 			continue;
    897 		boot->flags &= ~PUIFLAG_ADD_OUTER;
    898 		boot->flags |= PUIFLG_IS_OUTER|PUIFLAG_ADD_INNER;
    899 		boot->size = info.size;
    900 		boot->cur_start = info.start;
    901 		boot->cur_flags = info.flags;
    902 		break;
    903 	}
    904 }
    905 
    906 /* no need to install bootblock if installing for UEFI */
    907 bool
    908 x86_md_need_bootblock(struct install_partition_desc *install)
    909 {
    910 
    911 	return !uefi_boot;
    912 }
    913 
    914 
    915