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