/* $NetBSD: boot1.c,v 1.22 2023/06/29 14:18:58 manu Exp $ */
/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by David Laight.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 THE FOUNDATION OR CONTRIBUTORS
* 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.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: boot1.c,v 1.22 2023/06/29 14:18:58 manu Exp $");
if (set_geometry(&d, NULL))
return "set_geometry\r\n";
/*
* We default to the filesystem at the start of the
* MBR partition
*/
fd = ob();
if (fd != -1)
goto done;
/*
* Maybe the filesystem is enclosed in a raid set.
* add in size of raidframe header and try again.
* (Maybe this should only be done if the filesystem
* magic number is absent.)
*/
bios_sector += RF_PROTECTED_SECTORS;
fd = ob();
if (fd != -1)
goto done;
#ifndef NO_GPT
/*
* Test for a GPT inside the RAID
*/
bios_sector += gpt_lookup(bios_sector);
fd = ob();
if (fd != -1)
goto done;
#endif
/*
* Nothing at the start of the MBR partition, fallback on
* partition 'a' from the disklabel in this MBR partition.
*/
if (ptn_disklabel.d_magic != DISKMAGIC ||
ptn_disklabel.d_magic2 != DISKMAGIC ||
ptn_disklabel.d_partitions[0].p_fstype == FS_UNUSED)
goto done;
bios_sector = ptn_disklabel.d_partitions[0].p_offset;
*sector = bios_sector;
if (ptn_disklabel.d_partitions[0].p_fstype == FS_RAID)
bios_sector += RF_PROTECTED_SECTORS;
fd = ob();
done:
if (fd == -1 || fstat(fd, &sb) == -1)
return "Can't open /boot\r\n";
biosdev = (uint32_t)sb.st_size;
#if 0
if (biosdev > SECONDARY_MAX_LOAD)
return "/boot too large\r\n";
#endif
static int
is_bootable(struct gpt_ent *ent)
{
/* GPT_ENT_TYPE_NETBSD_RAID omitted as we are already in a RAID */
const struct uuid bootable[] = {
GPT_ENT_TYPE_NETBSD_FFS,
GPT_ENT_TYPE_NETBSD_LFS,
GPT_ENT_TYPE_NETBSD_CCD,
GPT_ENT_TYPE_NETBSD_CGD,
};
int i;
for (i = 0; i < sizeof(bootable) / sizeof(*bootable); i++) {
if (memcmp(ent->ent_type, &bootable[i],
sizeof(struct uuid)) == 0)
return 1;
}
/*
* Read partition table
*
* According to UEFI specification section 5.3.2, entries
* are 128 * (2^n) bytes long. The most common scenario is
* 128 bytes (n = 0) where there are 4 entries per sector.
* If n > 2, then entries spans multiple sectors, but they
* remain sector-aligned.
*/
entries_per_sector = BIOSDISK_DEFAULT_SECSIZE / entsz;
if (entries_per_sector == 0)
entries_per_sector = 1;