Home | History | Annotate | Line # | Download | only in i386
md.c revision 1.15
      1 /*	$NetBSD: md.c,v 1.15 2019/06/12 06:20:21 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 
     64 /* prototypes */
     65 
     66 static bool get_bios_info(const char*, struct disk_partitions*, int*, int*, int*);
     67 static int mbr_root_above_chs(void);
     68 static int md_read_bootcode(const char *, struct mbr_sector *);
     69 static unsigned int get_bootmodel(void);
     70 
     71 static int conmib[] = {CTL_MACHDEP, CPU_CONSDEV};
     72 
     73 void
     74 md_init(void)
     75 {
     76 }
     77 
     78 void
     79 md_init_set_status(int flags)
     80 {
     81 	(void)flags;
     82 
     83 	/* Default to install same type of kernel as we are running */
     84 	set_kernel_set(get_bootmodel());
     85 }
     86 
     87 bool
     88 md_get_info(struct install_partition_desc *install)
     89 {
     90 	int bcyl = 0, bhead = 0, bsec = 0;
     91 
     92 	if (pm->no_mbr || pm->no_part)
     93 		return true;
     94 
     95 	if (pm->parts == NULL) {
     96 
     97 		const struct disk_partitioning_scheme *ps =
     98 		    select_part_scheme(pm, NULL, true, NULL);
     99 
    100 		if (!ps)
    101 			return true;
    102 
    103 		struct disk_partitions *parts =
    104 		   (*ps->create_new_for_disk)(pm->diskdev,
    105 		   0, pm->dlsize, pm->dlsize, true);
    106 		if (!parts)
    107 			return false;
    108 
    109 		pm->parts = parts;
    110 		if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
    111 			pm->dlsize = ps->size_limit;
    112 	}
    113 
    114 	if (get_bios_info(pm->diskdev, pm->parts, &bcyl, &bhead, &bsec)
    115 	    && pm->parts->pscheme->change_disk_geom != NULL)
    116 		pm->parts->pscheme->change_disk_geom(pm->parts,
    117 		    bcyl, bhead, bsec);
    118 	else
    119 		set_default_sizemult(MEG/512);
    120 
    121 	/*
    122 	 * If the selected scheme does not need two-stage partitioning
    123 	 * (like GPT), do not bother to edit the outer partitions.
    124 	 */
    125 	if (pm->parts->pscheme->secondary_partitions == NULL ||
    126 	    pm->parts->pscheme->secondary_scheme == NULL)
    127 		return true;
    128 
    129 	if (pm->no_mbr || pm->no_part)
    130 		return true;
    131 
    132 	return edit_outer_parts(pm->parts);
    133 }
    134 
    135 /*
    136  * md back-end code for menu-driven BSD disklabel editor.
    137  */
    138 bool
    139 md_make_bsd_partitions(struct install_partition_desc *install)
    140 {
    141 	return make_bsd_partitions(install);
    142 }
    143 
    144 /*
    145  * any additional partition validation
    146  */
    147 bool
    148 md_check_partitions(struct install_partition_desc *install)
    149 {
    150 	int rval;
    151 	char *bootxx;
    152 
    153 	/* check we have boot code for the root partition type */
    154 	bootxx = bootxx_name(install);
    155 	rval = access(bootxx, R_OK);
    156 	free(bootxx);
    157 	if (rval == 0)
    158 		return true;
    159 	process_menu(MENU_ok, __UNCONST(MSG_No_Bootcode));
    160 	return false;
    161 }
    162 
    163 /*
    164  * hook called before writing new disklabel.
    165  */
    166 bool
    167 md_pre_disklabel(struct install_partition_desc *install,
    168     struct disk_partitions *parts)
    169 {
    170 
    171 	if (parts->parent == NULL)
    172 		return true;	/* no outer partitions */
    173 
    174 	parts = parts->parent;
    175 
    176 	msg_display_subst(MSG_dofdisk, 3, parts->disk,
    177 	    msg_string(parts->pscheme->name),
    178 	    msg_string(parts->pscheme->short_name));
    179 
    180 	/* write edited "MBR" onto disk. */
    181 	if (!parts->pscheme->write_to_disk(parts)) {
    182 		msg_display(MSG_wmbrfail);
    183 		process_menu(MENU_ok, NULL);
    184 		return false;
    185 	}
    186 	return true;
    187 }
    188 
    189 /*
    190  * hook called after writing disklabel to new target disk.
    191  */
    192 bool
    193 md_post_disklabel(struct install_partition_desc *install,
    194     struct disk_partitions *parts)
    195 {
    196 	return true;
    197 }
    198 
    199 /*
    200  * hook called after upgrade() or install() has finished setting
    201  * up the target disk but immediately before the user is given the
    202  * ``disks are now set up'' message.
    203  */
    204 int
    205 md_post_newfs(struct install_partition_desc *install)
    206 {
    207 	int ret;
    208 	size_t len;
    209 	char boot_options[1024];
    210 	char *bootxx_filename;
    211 	/*
    212 	 * XXX - this code retains a lot of cruft from when we went
    213 	 * to great pains to exclude installboot from the ramdisk
    214 	 * for size reasons and should be rewritten.
    215 	 */
    216 	static const char *consoles[]={
    217         	"pc", /* CONSDEV_PC */
    218 		"com0", /* CONSDEV_COM0 */
    219 		"com1", /* CONSDEV_COM1 */
    220 		"com2", /* CONSDEV_COM2 */
    221 		"com3", /* CONSDEV_COM3 */
    222 		"com0kbd", /* CONSDEV_COM0KBD */
    223 		"com1kbd", /* CONSDEV_COM1KBD */
    224 		"com2kbd", /* CONSDEV_COM2KBD */
    225 		"com3kbd" /* CONSDEV_COM3KBD */ };
    226 	static struct x86_boot_params boottype =
    227 		{sizeof boottype, 0, 5, 0, 9600, { '\0' }, "", 0};
    228 	struct termios t;
    229 	dev_t condev;
    230 
    231 	if (pm == NULL || !pm->no_part) {
    232 		/*
    233 		 * Get console device, should either be ttyE0 or tty0n.
    234 		 * Too hard to double check, so just 'know' the device numbers.
    235 		 */
    236 		len = sizeof condev;
    237 		if (sysctl(conmib, __arraycount(conmib), &condev, &len, NULL, 0) != -1
    238 		    && (condev & ~3) == 0x800) {
    239 			/* Motherboard serial port */
    240 			boottype.bp_consdev = (condev & 3) + 1;
    241 			/* Defaulting the baud rate to that of stdin should suffice */
    242 			if (tcgetattr(0, &t) != -1)
    243 				boottype.bp_conspeed = t.c_ispeed;
    244 		}
    245 
    246 		process_menu(MENU_getboottype, &boottype);
    247 		msg_display(MSG_dobootblks, pm->diskdev);
    248 		if (boottype.bp_consdev == ~0u)
    249 			/* Use existing bootblocks */
    250 			return 0;
    251 	}
    252 
    253 	ret = cp_to_target("/usr/mdec/boot", "/boot");
    254 	if (ret)
    255 		return ret;
    256 	if (pm && pm->no_part)
    257 		return 0;
    258 
    259         bootxx_filename = bootxx_name(install);
    260         if (bootxx_filename != NULL) {
    261 		char rdev[PATH_MAX];
    262 
    263 		install->infos[0].parts->pscheme->get_part_device(
    264 		    install->infos[0].parts, install->infos[0].cur_part_id,
    265 		    rdev, sizeof rdev, NULL, raw_dev_name, true);
    266 
    267 		snprintf(boot_options, sizeof boot_options,
    268 		    "console=%s,speed=%u", consoles[boottype.bp_consdev],
    269 		    boottype.bp_conspeed);
    270                	ret = run_program(RUN_DISPLAY,
    271                 	    "/usr/sbin/installboot -o %s %s %s",
    272 			    boot_options, rdev, bootxx_filename);
    273                 free(bootxx_filename);
    274         } else
    275                 ret = -1;
    276 
    277         if (ret != 0)
    278                 process_menu(MENU_ok,
    279                     __UNCONST("Warning: disk is probably not bootable"));
    280 
    281 	return ret;
    282 }
    283 
    284 int
    285 md_post_extract(struct install_partition_desc *install)
    286 {
    287 	return 0;
    288 }
    289 
    290 void
    291 md_cleanup_install(struct install_partition_desc *install)
    292 {
    293 	size_t len;
    294 	dev_t condev;
    295 
    296 #ifndef DEBUG
    297 	enable_rc_conf();
    298 	add_rc_conf("wscons=YES\n");
    299 
    300 # if defined(__i386__) && defined(SET_KERNEL_TINY)
    301 	/*
    302 	 * For GENERIC_TINY, do not enable any extra screens or wsmux.
    303 	 * Otherwise, run getty on 4 VTs.
    304 	 */
    305 	if (get_kernel_set() == SET_KERNEL_TINY)
    306 		run_program(RUN_CHROOT,
    307                             "sed -an -e '/^screen/s/^/#/;/^mux/s/^/#/;"
    308 			    "H;$!d;g;w /etc/wscons.conf' /etc/wscons.conf");
    309 	else
    310 # endif
    311 		run_program(RUN_CHROOT,
    312 			    "sed -an -e '/^ttyE[1-9]/s/off/on/;"
    313 			    "H;$!d;g;w /etc/ttys' /etc/ttys");
    314 
    315 #endif
    316 
    317 	/*
    318 	 * Get console device, should either be ttyE0 or tty0n.
    319 	 * Too hard to double check, so just 'know' the device numbers.
    320 	 */
    321 	len = sizeof condev;
    322 	if (sysctl(conmib, __arraycount(conmib), &condev, &len, NULL, 0) != -1
    323 	    && (condev & ~3) != 0x800) {
    324 
    325 		/*
    326 		 * Current console is not com*, assume ttyE*.
    327 		 * Modify /etc/ttys to use wsvt25 for all ports.
    328 		 */
    329 
    330 		run_program(RUN_CHROOT,
    331 			    "sed -an -e 's/vt100/wsvt25/g;"
    332 			    "H;$!d;g;w  /etc/ttys' /etc/ttys");
    333 	}
    334 }
    335 
    336 int
    337 md_pre_update(struct install_partition_desc *install)
    338 {
    339 	return 1;
    340 }
    341 
    342 /* Upgrade support */
    343 int
    344 md_update(struct install_partition_desc *install)
    345 {
    346 	md_post_newfs(install);
    347 	return 1;
    348 }
    349 
    350 int
    351 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
    352 {
    353 	mbr_info_t *ext;
    354 	struct mbr_partition *p;
    355 	const char *bootcode;
    356 	int i, names, fl, ofl;
    357 #define	ACTIVE_FOUND	0x0100
    358 #define	NETBSD_ACTIVE	0x0200
    359 #define	NETBSD_NAMED	0x0400
    360 #define	ACTIVE_NAMED	0x0800
    361 
    362 	root_limit = 0;
    363 	if (biosdisk != NULL && (biosdisk->bi_flags & BIFLAG_EXTINT13) == 0) {
    364 		if (mbr_root_above_chs()) {
    365 			if (quiet)
    366 				return 0;
    367 			msg_display(MSG_partabovechs);
    368 			if (!ask_noyes(NULL))
    369 				return 1;
    370 			/* The user is shooting themselves in the foot here...*/
    371 		} else {
    372 			if (parts->pscheme->size_limit)
    373 				root_limit = min(parts->pscheme->size_limit,
    374 				    parts->disk_size);
    375 			else
    376 				root_limit = parts->disk_size;
    377 		}
    378 	}
    379 
    380 	/*
    381 	 * Ensure the install partition (at sector pm->ptstart) and the active
    382 	 * partition are bootable.
    383 	 * Determine whether the bootselect code is needed.
    384 	 * Note that MBR_BS_NEWMBR is always set, so we ignore it!
    385 	 */
    386 	fl = 0;
    387 	names = 0;
    388 	for (ext = mbri; ext != NULL; ext = ext->extended) {
    389 		p = ext->mbr.mbr_parts;
    390 		for (i = 0; i < MBR_PART_COUNT; p++, i++) {
    391 			if (p->mbrp_flag == MBR_PFLAG_ACTIVE) {
    392 				fl |= ACTIVE_FOUND;
    393 			    if (ext->sector + p->mbrp_start == pm->ptstart)
    394 				fl |= NETBSD_ACTIVE;
    395 			}
    396 			if (ext->mbrb.mbrbs_nametab[i][0] == 0) {
    397 				/* No bootmenu label... */
    398 				if (ext->sector == 0)
    399 					continue;
    400 				if (ext->sector + p->mbrp_start == pm->ptstart)
    401 					/*
    402 					 * Have installed into an extended ptn
    403 					 * force name & bootsel...
    404 					 */
    405 					names++;
    406 				continue;
    407 			}
    408 			/* Partition has a bootmenu label... */
    409 			if (ext->sector != 0)
    410 				fl |= MBR_BS_EXTLBA;
    411 			if (ext->sector + p->mbrp_start == pm->ptstart)
    412 				fl |= NETBSD_NAMED;
    413 			else if (p->mbrp_flag == MBR_PFLAG_ACTIVE)
    414 				fl |= ACTIVE_NAMED;
    415 			else
    416 				names++;
    417 		}
    418 	}
    419 	if (!(fl & ACTIVE_FOUND))
    420 		fl |= NETBSD_ACTIVE;
    421 	if (fl & NETBSD_NAMED && fl & NETBSD_ACTIVE)
    422 		fl |= ACTIVE_NAMED;
    423 
    424 	if ((names > 0 || !(fl & NETBSD_ACTIVE)) &&
    425 	    (!(fl & NETBSD_NAMED) || !(fl & ACTIVE_NAMED))) {
    426 		/*
    427 		 * There appear to be multiple bootable partitions, but they
    428 		 * don't all have bootmenu texts.
    429 		 */
    430 		if (quiet)
    431 			return 0;
    432 
    433 		msg_display(MSG_missing_bootmenu_text);
    434 		if (ask_yesno(NULL))
    435 			return 1;
    436 	}
    437 
    438 	if ((fl & MBR_BS_EXTLBA) &&
    439 	    (biosdisk == NULL || !(biosdisk->bi_flags & BIFLAG_EXTINT13))) {
    440 		/* Need unsupported LBA reads to read boot sectors */
    441 		if (quiet)
    442 			return 0;
    443 
    444 		msg_display(MSG_no_extended_bootmenu);
    445 		if (!ask_noyes(NULL))
    446 			return 1;
    447 	}
    448 
    449 	/* Sort out the name of the mbr code we need */
    450 	if (names > 0 || fl & (NETBSD_NAMED | ACTIVE_NAMED)) {
    451 		/* Need bootselect code */
    452 		fl |= MBR_BS_ACTIVE;
    453 		bootcode = fl & MBR_BS_EXTLBA ? _PATH_BOOTEXT : _PATH_BOOTSEL;
    454 	} else
    455 		bootcode = _PATH_MBR;
    456 
    457 	fl &=  MBR_BS_ACTIVE | MBR_BS_EXTLBA;
    458 
    459 	/* Look at what is installed */
    460 	ofl = mbri->mbrb.mbrbs_flags;
    461 	if (ofl == 0) {
    462 		/* Check there is some bootcode at all... */
    463 		if (mbri->mbr.mbr_magic != htole16(MBR_MAGIC) ||
    464 		    mbri->mbr.mbr_jmpboot[0] == 0 ||
    465 		    mbr_root_above_chs())
    466 			/* Existing won't do, force update */
    467 			fl |= MBR_BS_NEWMBR;
    468 	}
    469 	ofl = mbri->oflags & (MBR_BS_ACTIVE | MBR_BS_EXTLBA);
    470 
    471 	if (!quiet) {
    472 		if (fl & ~ofl || (fl == 0 && ofl & MBR_BS_ACTIVE)) {
    473 			/* Existing boot code isn't the right one... */
    474 			if (fl & MBR_BS_ACTIVE)
    475 				msg_display(MSG_installbootsel);
    476 			else
    477 				msg_display(MSG_installmbr);
    478 		} else
    479 			/* Existing code would (probably) be ok */
    480 			msg_display(MSG_updatembr);
    481 
    482 		if (!ask_yesno(NULL))
    483 			/* User doesn't want to update mbr code */
    484 			return 2;
    485 	}
    486 
    487 	if (md_read_bootcode(bootcode, &mbri->mbr) == 0)
    488 		/* update suceeded - to memory copy */
    489 		return 2;
    490 
    491 	/* This shouldn't happen since the files are in the floppy fs... */
    492 	msg_display("Can't find %s", bootcode);
    493 	return ask_reedit(parts);
    494 }
    495 
    496 bool
    497 md_parts_use_wholedisk(struct disk_partitions *parts)
    498 {
    499 	return parts_use_wholedisk(parts, 0, NULL);
    500 }
    501 
    502 static bool
    503 get_bios_info(const char *dev, struct disk_partitions *parts, int *bcyl,
    504     int *bhead, int *bsec)
    505 {
    506 	static struct disklist *disklist = NULL;
    507 	static int mib[2] = {CTL_MACHDEP, CPU_DISKINFO};
    508 	int i;
    509 	size_t len;
    510 	struct biosdisk_info *bip;
    511 	struct nativedisk_info *nip = NULL, *nat;
    512 	int cyl, head, sec;
    513 
    514 	if (disklist == NULL) {
    515 		if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0)
    516 			goto nogeom;
    517 		disklist = malloc(len);
    518 		if (disklist == NULL) {
    519 			fprintf(stderr, "Out of memory\n");
    520 			return false;
    521 		}
    522 		sysctl(mib, 2, disklist, &len, NULL, 0);
    523 	}
    524 
    525 	for (i = 0; i < disklist->dl_nnativedisks; i++) {
    526 		nat = &disklist->dl_nativedisks[i];
    527 		if (!strcmp(dev, nat->ni_devname)) {
    528 			nip = nat;
    529 			break;
    530 		}
    531 	}
    532 	if (nip == NULL || nip->ni_nmatches == 0) {
    533 nogeom:
    534 		if (nip != NULL)
    535 			msg_display(MSG_nobiosgeom, pm->dlcyl, pm->dlhead, pm->dlsec);
    536 		if (guess_biosgeom_from_parts(parts, &cyl, &head, &sec) >= 0
    537 		    && nip != NULL)
    538 			msg_display_add(MSG_biosguess, cyl, head, sec);
    539 		biosdisk = NULL;
    540 	} else {
    541 		guess_biosgeom_from_parts(parts, &cyl, &head, &sec);
    542 		if (nip->ni_nmatches == 1) {
    543 			bip = &disklist->dl_biosdisks[nip->ni_biosmatches[0]];
    544 			msg_display(MSG_onebiosmatch);
    545 			msg_table_add(MSG_onebiosmatch_header);
    546 			msg_table_add(MSG_onebiosmatch_row, bip->bi_dev,
    547 			    bip->bi_cyl, bip->bi_head, bip->bi_sec,
    548 			    (unsigned)bip->bi_lbasecs,
    549 			    (unsigned)(bip->bi_lbasecs / (1000000000 / 512)));
    550 			msg_display_add(MSG_biosgeom_advise);
    551 			biosdisk = bip;
    552 			process_menu(MENU_biosonematch, &biosdisk);
    553 		} else {
    554 			msg_display(MSG_biosmultmatch);
    555 			msg_table_add(MSG_biosmultmatch_header);
    556 			for (i = 0; i < nip->ni_nmatches; i++) {
    557 				bip = &disklist->dl_biosdisks[
    558 							nip->ni_biosmatches[i]];
    559 				msg_table_add(MSG_biosmultmatch_row, i,
    560 				    bip->bi_dev, bip->bi_cyl, bip->bi_head,
    561 				    bip->bi_sec, (unsigned)bip->bi_lbasecs,
    562 				    (unsigned)bip->bi_lbasecs/(1000000000/512));
    563 			}
    564 			process_menu(MENU_biosmultmatch, &i);
    565 			if (i == -1)
    566 				biosdisk = NULL;
    567 			else
    568 				biosdisk = &disklist->dl_biosdisks[
    569 							nip->ni_biosmatches[i]];
    570 		}
    571 	}
    572 	if (biosdisk == NULL) {
    573 		if (nip != NULL) {
    574 			set_bios_geom(parts, cyl, head, sec);
    575 		} else {
    576 			*bcyl = cyl;
    577 			*bhead = head;
    578 			*bsec = sec;
    579 		}
    580 	} else {
    581 		*bcyl = biosdisk->bi_cyl;
    582 		*bhead = biosdisk->bi_head;
    583 		*bsec = biosdisk->bi_sec;
    584 	}
    585 	return true;
    586 }
    587 
    588 static int
    589 mbr_root_above_chs(void)
    590 {
    591 	return pm->ptstart + (daddr_t)DEFROOTSIZE * (daddr_t)(MEG / 512)
    592 	    >= pm->max_chs;
    593 }
    594 
    595 /* returns false if no write-back of parts is required */
    596 bool
    597 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
    598 {
    599 	struct mbr_partition *mbrp;
    600 	int i, netbsdpart = -1, oldbsdpart = -1, oldbsdcount = 0;
    601 
    602 	if (pm->no_mbr || pm->no_part)
    603 		return false;
    604 
    605 	mbrp = &mbri->mbr.mbr_parts[0];
    606 
    607 	for (i = 0; i < MBR_PART_COUNT; i++) {
    608 		if (mbrp[i].mbrp_type == MBR_PTYPE_386BSD) {
    609 			oldbsdpart = i;
    610 			oldbsdcount++;
    611 		} else if (mbrp[i].mbrp_type == MBR_PTYPE_NETBSD)
    612 			netbsdpart = i;
    613 	}
    614 
    615 	if (netbsdpart == -1 && oldbsdcount == 1) {
    616 		mbrp[oldbsdpart].mbrp_type = MBR_PTYPE_NETBSD;
    617 		return true;
    618 	}
    619 
    620 	return false;
    621 }
    622 
    623 /*
    624  * Read MBR code from a file.
    625  * The existing partition table and bootselect configuration is kept.
    626  */
    627 static int
    628 md_read_bootcode(const char *path, struct mbr_sector *mbrs)
    629 {
    630 	int fd;
    631 	struct stat st;
    632 	size_t len;
    633 	struct mbr_sector new_mbr;
    634 	uint32_t dsn;
    635 
    636 	fd = open(path, O_RDONLY);
    637 	if (fd < 0)
    638 		return -1;
    639 
    640 	if (fstat(fd, &st) < 0 || st.st_size != sizeof *mbrs) {
    641 		close(fd);
    642 		return -1;
    643 	}
    644 
    645 	if (read(fd, &new_mbr, sizeof new_mbr) != sizeof new_mbr) {
    646 		close(fd);
    647 		return -1;
    648 	}
    649 	close(fd);
    650 
    651 	if (new_mbr.mbr_bootsel_magic != htole16(MBR_BS_MAGIC))
    652 		return -1;
    653 
    654 	if (mbrs->mbr_bootsel_magic == htole16(MBR_BS_MAGIC)) {
    655 		len = offsetof(struct mbr_sector, mbr_bootsel);
    656 	} else
    657 		len = offsetof(struct mbr_sector, mbr_parts);
    658 
    659 	/* Preserve the 'drive serial number' - especially for Vista */
    660 	dsn = mbrs->mbr_dsn;
    661 	memcpy(mbrs, &new_mbr, len);
    662 	mbrs->mbr_dsn = dsn;
    663 
    664 	/* Keep flags from object file - indicate the properties */
    665 	mbrs->mbr_bootsel.mbrbs_flags = new_mbr.mbr_bootsel.mbrbs_flags;
    666 	mbrs->mbr_magic = htole16(MBR_MAGIC);
    667 
    668 	return 0;
    669 }
    670 
    671 static unsigned int
    672 get_bootmodel(void)
    673 {
    674 #if defined(__i386__)
    675 	struct utsname ut;
    676 #ifdef DEBUG
    677 	char *envstr;
    678 
    679 	envstr = getenv("BOOTMODEL");
    680 	if (envstr != NULL)
    681 		return atoi(envstr);
    682 #endif
    683 
    684 	if (uname(&ut) < 0)
    685 		ut.version[0] = 0;
    686 
    687 #if defined(SET_KERNEL_TINY)
    688 	if (strstr(ut.version, "TINY") != NULL)
    689 		return SET_KERNEL_TINY;
    690 #endif
    691 #if defined(SET_KERNEL_PS2)
    692 	if (strstr(ut.version, "PS2") != NULL)
    693 		return SET_KERNEL_PS2;
    694 #endif
    695 #endif
    696 	return SET_KERNEL_GENERIC;
    697 }
    698 
    699 
    700 int
    701 md_pre_mount(struct install_partition_desc *install)
    702 {
    703 	return 0;
    704 }
    705 
    706 #ifdef HAVE_GPT
    707 /*
    708  * New GPT partitions have been written, update bootloader or remember
    709  * data untill needed in md_post_newfs
    710  */
    711 bool
    712 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
    713     bool root_is_new, part_id efi_id, bool efi_is_new)
    714 {
    715 	struct disk_part_info info;
    716 
    717 	assert(efi_id == NO_PART);	/* XXX EFI support still missing */
    718 
    719 	if (root_id != NO_PART) {
    720 		/* we always update the gpt boot record for now */
    721 		if (!parts->pscheme->get_part_info(parts, root_id, &info))
    722 			return false;
    723 		if (run_program(RUN_SILENT, "gpt biosboot -b %" PRIu64 " %s",
    724 		    info.start, parts->disk) != 0)
    725 			return false;
    726 	}
    727 
    728 	return true;
    729 }
    730 #endif
    731