summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Brown2024-03-05 13:54:44 +0100
committerMichael Brown2024-03-05 14:25:35 +0100
commitb1c13cc43ece3008f7012cf736fc943d5bb89131 (patch)
tree0162bded757d1c359a23dc7a70f0341e63c9f4ec
parent[block] Allow for iteration over SAN device list in drive number order (diff)
downloadipxe-b1c13cc43ece3008f7012cf736fc943d5bb89131.tar.gz
ipxe-b1c13cc43ece3008f7012cf736fc943d5bb89131.tar.xz
ipxe-b1c13cc43ece3008f7012cf736fc943d5bb89131.zip
[efi] Allow booting from local disks via the "sanboot" command
Extend the EFI SAN boot code to allow for booting from a local disk, as is already possible with the BIOS SAN boot code. There is unfortunately no direct UEFI equivalent of the BIOS drive number. The UEFI shell does provide numbered mappings fs0:, blk0:, etc, but these numberings exist only while the UEFI shell is running and are not necessarily stable between shell invocations or across reboots. A substantial amount of existing third-party documentation for iPXE will suggest using "sanboot --drive 0x80" to boot from a local disk (when no SAN drives are present), since this suggestion has been present in the official documentation for the "sanboot" command for almost thirteen years. We therefore aim to ensure that this instruction will also work for UEFI, i.e. that in a situation where there are local disks but no SAN disks, then the first local disk will be treated as being drive 0x80. We therefore assign local disks the virtual drive numbers 0x80, 0x81, etc, matching the numbering typically used in a BIOS environment. Where a SAN disk is already occupying one of these drive numbers, the local disks' virtual drive numbers will be incremented as necessary. This provides a rough approximation of the equivalent functionality under BIOS, where existing local disks' drive numbers are remapped to make way for SAN disks. We do not make any attempt to sort the list of local disks: the order used for allocating virtual drive numbers will be whatever order is returned by LocateHandle(). This will typically match the creation order of the EFI handles, which will typically match the hardware enumeration order of the devices, which will typically match user expectations as to which local disk is first, second, etc. We explicitly do not attempt to match the numbering used by the UEFI shell (which initially sorts in increasing order of device path, but does not renumber when new devices are added or removed). We can never guarantee matching this partly transient UEFI shell numbering, so it is best not to set any expectation that it will be matched. (Using local drive numbers starting at 0x80 helps to avoid setting up this impossible expectation, since the UEFI shell uses local drive numbers starting at zero.) Since floppy disks are essentially non-existent in any plausible UEFI system, overload "--drive 0" to mean "boot from any drive containing the specified (or default) boot filename". Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r--src/core/sanboot.c10
-rw-r--r--src/include/ipxe/sanboot.h10
-rw-r--r--src/interface/efi/efi_block.c177
3 files changed, 166 insertions, 31 deletions
diff --git a/src/core/sanboot.c b/src/core/sanboot.c
index 5b7a2165..e49a3f92 100644
--- a/src/core/sanboot.c
+++ b/src/core/sanboot.c
@@ -45,16 +45,6 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ipxe/sanboot.h>
/**
- * Default SAN drive number
- *
- * The drive number is a meaningful concept only in a BIOS
- * environment, where it represents the INT13 drive number (0x80 for
- * the first hard disk). We retain it in other environments to allow
- * for a simple way for iPXE commands to refer to SAN drives.
- */
-#define SAN_DEFAULT_DRIVE 0x80
-
-/**
* Timeout for block device commands (in ticks)
*
* Underlying devices should ideally never become totally stuck.
diff --git a/src/include/ipxe/sanboot.h b/src/include/ipxe/sanboot.h
index a1b6d7f3..b11197f9 100644
--- a/src/include/ipxe/sanboot.h
+++ b/src/include/ipxe/sanboot.h
@@ -21,6 +21,16 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ipxe/acpi.h>
#include <config/sanboot.h>
+/**
+ * Default SAN drive number
+ *
+ * The drive number is an externally defined concept only in a BIOS
+ * environment, where it represents the INT13 drive number (0x80 for
+ * the first hard disk). We retain it in other environments to allow
+ * for a simple way for iPXE commands to refer to SAN drives.
+ */
+#define SAN_DEFAULT_DRIVE 0x80
+
/** A SAN path */
struct san_path {
/** Containing SAN device */
diff --git a/src/interface/efi/efi_block.c b/src/interface/efi/efi_block.c
index 79ef7455..11d11156 100644
--- a/src/interface/efi/efi_block.c
+++ b/src/interface/efi/efi_block.c
@@ -299,8 +299,8 @@ static int efi_block_hook ( unsigned int drive, struct uri **uris,
DBGC ( drive, "EFIBLK %#02x has no device path\n", drive );
goto err_describe;
}
- DBGC ( drive, "EFIBLK %#02x has device path %s\n",
- drive, efi_devpath_text ( block->path ) );
+ DBGC2 ( drive, "EFIBLK %#02x has device path %s\n",
+ drive, efi_devpath_text ( block->path ) );
/* Install protocols */
if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
@@ -313,6 +313,8 @@ static int efi_block_hook ( unsigned int drive, struct uri **uris,
drive, strerror ( rc ) );
goto err_install;
}
+ DBGC ( drive, "EFIBLK %#02x installed as SAN drive %s\n",
+ drive, efi_handle_name ( block->handle ) );
/* Connect all possible protocols */
efi_block_connect ( drive, block->handle );
@@ -783,6 +785,65 @@ static int efi_block_exec ( unsigned int drive,
}
/**
+ * Check that EFI block device is eligible for a local virtual drive number
+ *
+ * @v handle Block device handle
+ * @ret rc Return status code
+ *
+ * We assign virtual drive numbers for local (non-SAN) EFI block
+ * devices that represent complete disks, to provide roughly
+ * equivalent functionality to BIOS drive numbers.
+ */
+static int efi_block_local ( EFI_HANDLE handle ) {
+ EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
+ EFI_GUID *protocol = &efi_block_io_protocol_guid;
+ struct san_device *sandev;
+ struct efi_block_data *block;
+ union {
+ EFI_BLOCK_IO_PROTOCOL *blockio;
+ void *interface;
+ } u;
+ EFI_STATUS efirc;
+ int rc;
+
+ /* Check if handle belongs to a SAN device */
+ for_each_sandev ( sandev ) {
+ block = sandev->priv;
+ if ( handle == block->handle ) {
+ rc = -ENOTTY;
+ goto err_sandev;
+ }
+ }
+
+ /* Open block I/O protocol */
+ if ( ( efirc = bs->OpenProtocol ( handle, protocol, &u.interface,
+ efi_image_handle, handle,
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
+ rc = -EEFI ( efirc );
+ DBGC ( handle, "EFIBLK %s could not open block I/O: %s\n",
+ efi_handle_name ( handle ), strerror ( rc ) );
+ goto err_open;
+ }
+
+ /* Do not assign drive numbers for partitions */
+ if ( u.blockio->Media->LogicalPartition ) {
+ rc = -ENOTTY;
+ DBGC2 ( handle, "EFLBLK %s is a partition\n",
+ efi_handle_name ( handle ) );
+ goto err_partition;
+ }
+
+ /* Success */
+ rc = 0;
+
+ err_partition:
+ bs->CloseProtocol ( handle, protocol, efi_image_handle, handle );
+ err_open:
+ err_sandev:
+ return rc;
+}
+
+/**
* Boot from EFI block device
*
* @v drive Drive number
@@ -790,37 +851,111 @@ static int efi_block_exec ( unsigned int drive,
* @ret rc Return status code
*/
static int efi_block_boot ( unsigned int drive, const char *filename ) {
+ EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
EFI_DEVICE_PATH_PROTOCOL *fspath = NULL;
+ EFI_HANDLE *handles;
+ EFI_HANDLE handle;
+ UINTN count;
struct san_device *sandev;
struct efi_block_data *block;
+ unsigned int vdrive;
+ unsigned int index;
+ EFI_STATUS efirc;
int rc;
- /* Find SAN device */
- sandev = sandev_find ( drive );
- if ( ! sandev ) {
- DBGC ( drive, "EFIBLK %#02x is not a SAN drive\n", drive );
- rc = -ENODEV;
- goto err_sandev_find;
- }
- block = sandev->priv;
-
/* Release SNP devices */
efi_snp_release();
- /* Scan for a matching filesystem within this block device */
- if ( ( rc = efi_block_scan ( drive, block->handle, filename,
- &fspath ) ) != 0 ) {
- goto err_scan;
+ /* Locate all block I/O protocol handles */
+ if ( ( efirc = bs->LocateHandleBuffer ( ByProtocol,
+ &efi_block_io_protocol_guid,
+ NULL, &count,
+ &handles ) ) != 0 ) {
+ rc = -EEFI ( efirc );
+ DBGC ( drive, "EFIBLK %#02x cannot locate block I/O: %s\n",
+ drive, strerror ( rc ) );
+ goto err_locate_block_io;
}
- /* Attempt to boot from this filesystem */
- if ( ( rc = efi_block_exec ( drive, fspath, filename ) ) != 0 )
- goto err_exec;
+ /* Try booting from the first matching block device, if any */
+ rc = -ENOENT;
+ for ( vdrive = 0, index = 0 ; ; vdrive++ ) {
+
+ /* Identify next drive number and block I/O handle */
+ if ( ( sandev = sandev_next ( vdrive ) ) &&
+ ( ( sandev->drive == vdrive ) ||
+ ( sandev->drive <= SAN_DEFAULT_DRIVE ) ||
+ ( index >= count ) ) ) {
+
+ /* There is a SAN drive that either:
+ *
+ * a) has the current virtual drive number, or
+ * b) has a drive number below SAN_DEFAULT_DRIVE, or
+ * c) has a drive number higher than any local drive
+ *
+ * Use this SAN drive, since the explicit SAN
+ * drive numbering takes precedence over the
+ * implicit local drive numbering.
+ */
+ block = sandev->priv;
+ handle = block->handle;
+
+ /* Use SAN drive's explicit drive number */
+ vdrive = sandev->drive;
+ DBGC ( vdrive, "EFIBLK %#02x is SAN drive %s\n",
+ vdrive, efi_handle_name ( handle ) );
+
+ } else if ( index < count ) {
+
+ /* There is no SAN drive meeting any of the
+ * above criteria. Try the next block I/O
+ * handle.
+ */
+ handle = handles[index++];
+
+ /* Check if this handle is eligible to be
+ * given a local virtual drive number.
+ */
+ if ( ( rc = efi_block_local ( handle ) ) != 0 ) {
+ /* Do not consume virtual drive number */
+ vdrive--;
+ continue;
+ }
+
+ /* Use the current virtual drive number, with
+ * a minimum of SAN_DEFAULT_DRIVE to match
+ * typical BIOS drive numbering.
+ */
+ if ( vdrive < SAN_DEFAULT_DRIVE )
+ vdrive = SAN_DEFAULT_DRIVE;
+ DBGC ( vdrive, "EFIBLK %#02x is local drive %s\n",
+ vdrive, efi_handle_name ( handle ) );
+
+ } else {
+
+ /* No more SAN or local drives */
+ break;
+ }
+
+ /* Skip non-matching drives */
+ if ( drive && ( drive != vdrive ) )
+ continue;
+ DBGC ( vdrive, "EFIBLK %#02x attempting to boot\n", vdrive );
+
+ /* Scan for a matching filesystem within this drive */
+ if ( ( rc = efi_block_scan ( vdrive, handle, filename,
+ &fspath ) ) != 0 ) {
+ continue;
+ }
- err_exec:
- err_scan:
+ /* Attempt to boot from the matched filesystem */
+ rc = efi_block_exec ( vdrive, fspath, filename );
+ break;
+ }
+
+ bs->FreePool ( handles );
+ err_locate_block_io:
efi_snp_claim();
- err_sandev_find:
return rc;
}