/* $NetBSD: md.c,v 1.35 2022/02/10 16:11:42 martin Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
* All rights reserved.
*
* Based on code written by Philip A. Nelson for Piermont Information
* Systems Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of Piermont Information Systems Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/* md.c -- i386 machine specific routines - also used by amd64 */
if (get_bios_info(pm->diskdev, pm->parts, &bcyl, &bhead, &bsec)
&& pm->parts->pscheme->change_disk_geom != NULL)
pm->parts->pscheme->change_disk_geom(pm->parts,
bcyl, bhead, bsec);
else
set_default_sizemult(pm->diskdev, MEG, pm->sectorsize);
/*
* If the selected scheme does not need two-stage partitioning
* (like GPT), do not bother to edit the outer partitions.
*/
if (pm->parts->pscheme->secondary_partitions == NULL ||
pm->parts->pscheme->secondary_scheme == NULL)
return true;
if (pm->no_mbr || pm->no_part)
return true;
res = edit_outer_parts(pm->parts);
if (res == 0)
return false;
else if (res == 1)
return true;
/*
* hook called after writing disklabel to new target disk.
*/
bool
md_post_disklabel(struct install_partition_desc *install,
struct disk_partitions *parts)
{
return true;
}
/*
* Do all legacy bootblock update/setup here
*/
static int
update_bios_boot(struct install_partition_desc *install, bool use_target_files)
{
int ret;
size_t len;
char boot_options[1024];
char *bootxx_filename, *p;
/*
* XXX - this code retains a lot of cruft from when we went
* to great pains to exclude installboot from the ramdisk
* for size reasons and should be rewritten.
*/
static const char *consoles[]={
"pc", /* CONSDEV_PC */
"com0", /* CONSDEV_COM0 */
"com1", /* CONSDEV_COM1 */
"com2", /* CONSDEV_COM2 */
"com3", /* CONSDEV_COM3 */
"com0kbd", /* CONSDEV_COM0KBD */
"com1kbd", /* CONSDEV_COM1KBD */
"com2kbd", /* CONSDEV_COM2KBD */
"com3kbd" /* CONSDEV_COM3KBD */ };
static struct x86_boot_params boottype =
{sizeof boottype, 0, 5, 0, 9600, { '\0' }, "", 0};
struct termios t;
dev_t condev;
if (pm == NULL || !pm->no_part) {
/*
* Get console device, should either be ttyE0 or tty0n.
* Too hard to double check, so just 'know' the device numbers.
*/
len = sizeof condev;
if (sysctl(conmib, __arraycount(conmib), &condev, &len, NULL,
0) != -1 && (condev & ~3) == 0x800) {
/* Motherboard serial port */
boottype.bp_consdev = (condev & 3) + 1;
/* Defaulting the baud rate to that of stdin should
suffice */
if (tcgetattr(0, &t) != -1)
boottype.bp_conspeed = t.c_ispeed;
}
process_menu(MENU_getboottype, &boottype);
msg_fmt_display(MSG_dobootblks, "%s", pm->diskdev);
if (boottype.bp_consdev == ~0u)
/* Use existing bootblocks */
return 0;
}
if (use_target_files)
ret = cp_within_target("/usr/mdec/boot", "/", 0);
else
ret = cp_to_target("/usr/mdec/boot", "/");
if (ret)
return ret;
if (pm && pm->no_part)
return 0;
p = bootxx_name(install);
if (p && use_target_files) {
bootxx_filename = strdup(target_expand(p));
free(p);
} else {
bootxx_filename = p;
}
if (bootxx_filename != NULL) {
char rdev[PATH_MAX];
/*
* We should have a valid file system on that partition.
* Try to mount it and check if there is a /EFI in there.
*/
if (boot->mount[0])
strlcpy(path, boot->mount, sizeof(path));
else
strcpy(path, "/mnt");
if (!(boot->instflags & PUIINST_MOUNT)) {
make_target_dir(path);
err = target_mount("", dev, path);
if (err != 0)
return err;
}
for (i = 0; i < __arraycount(uefi_bootloaders); i++) {
s = uefi_bootloaders[i];
strcpy(src, target_is_populated ? target_expand(s) : s);
if (access(src, R_OK) != 0)
continue;
err = target_is_populated ?
cp_within_target(s, path, 0) :
cp_to_target(s, path);
if (err)
return err;
}
if (boot->mount[0] == 0)
target_unmount("/mnt");
return 0;
}
/*
* Find (U)EFI boot partition and install/update bootloaders
*/
static int
update_uefi_boot_code(struct install_partition_desc *install,
bool target_is_populated)
{
size_t i, boot_part;
boot_part = ~0U;
for (i = 0; i < install->num; i++) {
if (!(install->infos[i].instflags & PUIINST_BOOT))
continue;
boot_part = i;
break;
}
if (boot_part == ~0U) {
/*
* Didn't find an explicitly marked boot partition,
* check if we have any EFI System Partitions and
* use the first.
*/
for (i = 0; i < install->num; i++) {
if (install->infos[i].type != PT_EFI_SYSTEM)
continue;
boot_part = i;
break;
}
}
if (boot_part < install->num)
return copy_uefi_boot(&install->infos[boot_part],
target_is_populated);
return -1; /* no EFI boot partition found */
}
/*
* Find bootloader options and update bootloader
*/
static int
update_bios_boot_code(struct install_partition_desc *install)
{
return update_bios_boot(install, true);
}
static int
md_post_newfs_uefi(struct install_partition_desc *install)
{
return update_uefi_boot_code(install, false);
}
/*
* hook called after upgrade() or install() has finished setting
* up the target disk but immediately before the user is given the
* ``disks are now set up'' message.
*/
int
md_post_newfs(struct install_partition_desc *install)
{
# if defined(__i386__) && defined(SET_KERNEL_TINY)
/*
* For GENERIC_TINY, do not enable any extra screens or wsmux.
* Otherwise, run getty on 4 VTs.
*/
if (get_kernel_set() == SET_KERNEL_TINY)
run_program(RUN_CHROOT,
"sed -an -e '/^screen/s/^/#/;/^mux/s/^/#/;"
"H;$!d;g;w /etc/wscons.conf' /etc/wscons.conf");
else
# endif
run_program(RUN_CHROOT,
"sed -an -e '/^ttyE[1-9]/s/off/on/;"
"H;$!d;g;w /etc/ttys' /etc/ttys");
#endif
/*
* Get console device, should either be ttyE0 or tty0n.
* Too hard to double check, so just 'know' the device numbers.
*/
len = sizeof condev;
if (sysctl(conmib, __arraycount(conmib), &condev, &len, NULL, 0) != -1
&& (condev & ~3) != 0x800) {
/*
* Current console is not com*, assume ttyE*.
* Modify /etc/ttys to use wsvt25 for all ports.
*/
if (biosdisk != NULL && (biosdisk->bi_flags & BIFLAG_EXTINT13) == 0) {
if (mbr_root_above_chs(inst_start)) {
if (quiet)
return 0;
msg_display(MSG_partabovechs);
if (!ask_noyes(NULL))
return 1;
/* The user is shooting themselves in the foot here...*/
} else {
if (parts->pscheme->size_limit)
root_limit = min(parts->pscheme->size_limit,
parts->disk_size);
else
root_limit = parts->disk_size;
}
}
/*
* Ensure the install partition (at sector inst_start) and the active
* partition are bootable.
* Determine whether the bootselect code is needed.
* Note that MBR_BS_NEWMBR is always set, so we ignore it!
*/
fl = 0;
names = 0;
for (ext = mbri; ext != NULL; ext = ext->extended) {
p = ext->mbr.mbr_parts;
for (i = 0; i < MBR_PART_COUNT; p++, i++) {
if (p->mbrp_flag == MBR_PFLAG_ACTIVE) {
fl |= ACTIVE_FOUND;
if (ext->sector + p->mbrp_start == inst_start)
fl |= NETBSD_ACTIVE;
}
if (ext->mbrb.mbrbs_nametab[i][0] == 0) {
/* No bootmenu label... */
if (ext->sector == 0)
continue;
if (ext->sector + p->mbrp_start == inst_start)
/*
* Have installed into an extended ptn
* force name & bootsel...
*/
names++;
continue;
}
/* Partition has a bootmenu label... */
if (ext->sector != 0)
fl |= MBR_BS_EXTLBA;
if (ext->sector + p->mbrp_start == inst_start)
fl |= NETBSD_NAMED;
else if (p->mbrp_flag == MBR_PFLAG_ACTIVE)
fl |= ACTIVE_NAMED;
else
names++;
}
}
if (!(fl & ACTIVE_FOUND))
fl |= NETBSD_ACTIVE;
if (fl & NETBSD_NAMED && fl & NETBSD_ACTIVE)
fl |= ACTIVE_NAMED;
if ((names > 0 || !(fl & NETBSD_ACTIVE)) &&
(!(fl & NETBSD_NAMED) || !(fl & ACTIVE_NAMED))) {
/*
* There appear to be multiple bootable partitions, but they
* don't all have bootmenu texts.
*/
if (quiet)
return 0;
msg_display(MSG_missing_bootmenu_text);
if (ask_yesno(NULL))
return 1;
}
if ((fl & MBR_BS_EXTLBA) &&
(biosdisk == NULL || !(biosdisk->bi_flags & BIFLAG_EXTINT13))) {
/* Need unsupported LBA reads to read boot sectors */
if (quiet)
return 0;
msg_display(MSG_no_extended_bootmenu);
if (!ask_noyes(NULL))
return 1;
}
/* Sort out the name of the mbr code we need */
if (names > 1 ||
(parts->num_part > 1 && (fl & (NETBSD_NAMED | ACTIVE_NAMED)))) {
/* Need bootselect code */
fl |= MBR_BS_ACTIVE;
bootcode = fl & MBR_BS_EXTLBA ? _PATH_BOOTEXT : _PATH_BOOTSEL;
} else {
bootcode = _PATH_MBR;
}
fl &= MBR_BS_ACTIVE | MBR_BS_EXTLBA;
/* Look at what is installed */
ofl = mbri->mbrb.mbrbs_flags;
if (ofl == 0) {
/* Check there is some bootcode at all... */
if (mbri->mbr.mbr_magic != htole16(MBR_MAGIC) ||
mbri->mbr.mbr_jmpboot[0] == 0 ||
mbr_root_above_chs(inst_start))
/* Existing won't do, force update */
fl |= MBR_BS_NEWMBR;
}
ofl = mbri->oflags & (MBR_BS_ACTIVE | MBR_BS_EXTLBA);
if (!quiet) {
if (fl & ~ofl || (fl == 0 && ofl & MBR_BS_ACTIVE)) {
/* Existing boot code isn't the right one... */
if (fl & MBR_BS_ACTIVE)
msg_display(MSG_installbootsel);
else
msg_display(MSG_installmbr);
} else
/* Existing code would (probably) be ok */
msg_display(MSG_updatembr);
if (!ask_yesno(NULL))
/* User doesn't want to update mbr code */
return 2;
}
if (md_read_bootcode(bootcode, &mbri->mbr) == 0)
/* update suceeded - to memory copy */
return 2;
/* This shouldn't happen since the files are in the floppy fs... */
msg_fmt_display("Can't find %s", "%s", bootcode);
return ask_reedit(parts);
}
/* returns false if no write-back of parts is required */
bool
md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
{
struct mbr_partition *mbrp;
int i, netbsdpart = -1, oldbsdpart = -1, oldbsdcount = 0;
if (pm->no_mbr || pm->no_part)
return false;
mbrp = &mbri->mbr.mbr_parts[0];
for (i = 0; i < MBR_PART_COUNT; i++) {
if (mbrp[i].mbrp_type == MBR_PTYPE_386BSD) {
oldbsdpart = i;
oldbsdcount++;
} else if (mbrp[i].mbrp_type == MBR_PTYPE_NETBSD)
netbsdpart = i;
}
/*
* Read MBR code from a file.
* The existing partition table and bootselect configuration is kept.
*/
static int
md_read_bootcode(const char *path, struct mbr_sector *mbrs)
{
int fd;
struct stat st;
size_t len;
struct mbr_sector new_mbr;
uint32_t dsn;
int
md_pre_mount(struct install_partition_desc *install, size_t ndx)
{
return 0;
}
#ifdef HAVE_GPT
/*
* New GPT partitions have been written, update bootloader or remember
* data untill needed in md_post_newfs
*/
bool
md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
bool root_is_new, part_id efi_id, bool efi_is_new)
{
struct disk_part_info info;
if (root_id != NO_PART) {
/* we always update the gpt boot record for now */
if (!parts->pscheme->get_part_info(parts, root_id, &info))
return false;
if (run_program(RUN_SILENT, "gpt biosboot -b %" PRIu64 " %s",
info.start, parts->disk) != 0)
return false;
}
return true;
}
#endif
/*
* When we do an UEFI install, we have completely different default
* partitions and need to adjust the description at runtime.
*/
void
x86_md_part_defaults(struct pm_devs *cur_pm, struct part_usage_info **partsp,
size_t *num_usage_infos)
{
static const struct part_usage_info uefi_boot_part =
{
.size = BOOT_PART,
.type = BOOT_PART_TYPE,
.instflags = PUIINST_NEWFS|PUIINST_BOOT,
.fs_type = FS_MSDOS, .fs_version = MBR_PTYPE_FAT32L,
.flags = PUIFLAG_ADD_OUTER,
};