summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2022-04-23 18:42:00 -0400
committerTom Rini <trini@konsulko.com>2022-04-23 18:42:00 -0400
commit46a06ed82a81dfcb451fe82381c59c1d0a6667a1 (patch)
treea4b23b20380a7850521338c5dfe5a5ab6ae47d09 /lib
parent9bb99fa95826d1a608737ca821977b4136a1a278 (diff)
parentd97e98c887ed8fa4a339350c02f093f03cd1cf4d (diff)
downloadu-boot-46a06ed82a81dfcb451fe82381c59c1d0a6667a1.tar.gz
Merge tag 'efi-2022-07-rc1-3' of https://source.denx.de/u-boot/custodians/u-boot-efi
Pull request for efi-2022-07-rc1-3 Documentation: * Document image size parameter of bootefi command UEFI: * avoid building partition support in SPL/TPL where not required * improve integration of EFI subsystem and driver model * restore ability to boot arbitrary blob
Diffstat (limited to 'lib')
-rw-r--r--lib/efi_driver/efi_block_device.c34
-rw-r--r--lib/efi_loader/Kconfig5
-rw-r--r--lib/efi_loader/Makefile2
-rw-r--r--lib/efi_loader/efi_device_path.c9
-rw-r--r--lib/efi_loader/efi_disk.c321
-rw-r--r--lib/efi_loader/efi_setup.c62
6 files changed, 314 insertions, 119 deletions
diff --git a/lib/efi_driver/efi_block_device.c b/lib/efi_driver/efi_block_device.c
index 04cb3ef0d4..5baa6f87a3 100644
--- a/lib/efi_driver/efi_block_device.c
+++ b/lib/efi_driver/efi_block_device.c
@@ -35,6 +35,7 @@
#include <malloc.h>
#include <dm/device-internal.h>
#include <dm/root.h>
+#include <dm/tag.h>
/*
* EFI attributes of the udevice handled by this driver.
@@ -107,25 +108,6 @@ static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
}
/**
- * Create partions for the block device.
- *
- * @handle: EFI handle of the block device
- * @dev: udevice of the block device
- * Return: number of partitions created
- */
-static int efi_bl_bind_partitions(efi_handle_t handle, struct udevice *dev)
-{
- struct blk_desc *desc;
- const char *if_typename;
-
- desc = dev_get_uclass_plat(dev);
- if_typename = blk_get_if_type_name(desc->if_type);
-
- return efi_disk_create_partitions(handle, desc, if_typename,
- desc->devnum, dev->name);
-}
-
-/**
* Create a block device for a handle
*
* @handle: handle
@@ -139,7 +121,6 @@ static int efi_bl_bind(efi_handle_t handle, void *interface)
char *name;
struct efi_object *obj = efi_search_obj(handle);
struct efi_block_io *io = interface;
- int disks;
struct efi_blk_plat *plat;
EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, io);
@@ -173,15 +154,20 @@ static int efi_bl_bind(efi_handle_t handle, void *interface)
plat->handle = handle;
plat->io = interface;
+ /*
+ * FIXME: necessary because we won't do almost nothing in
+ * efi_disk_create() when called from device_probe().
+ */
+ ret = dev_tag_set_ptr(bdev, DM_TAG_EFI, handle);
+ if (ret)
+ /* FIXME: cleanup for bdev */
+ return ret;
+
ret = device_probe(bdev);
if (ret)
return ret;
EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name);
- /* Create handles for the partions of the block device */
- disks = efi_bl_bind_partitions(handle, bdev);
- EFI_PRINT("Found %d partitions\n", disks);
-
return 0;
}
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
index d50cd2563d..6b245f50a7 100644
--- a/lib/efi_loader/Kconfig
+++ b/lib/efi_loader/Kconfig
@@ -14,8 +14,10 @@ config EFI_LOADER
depends on DM_ETH || !NET
depends on !EFI_APP
default y if !ARM || SYS_CPU = armv7 || SYS_CPU = armv8
+ select DM_EVENT
+ select EVENT_DYNAMIC
select LIB_UUID
- select PARTITION_UUIDS
+ imply PARTITION_UUIDS
select HAVE_BLOCK_DEVICE
select REGEX
imply FAT
@@ -40,6 +42,7 @@ config CMD_BOOTEFI_BOOTMGR
config EFI_SETUP_EARLY
bool
+ default y
choice
prompt "Store for non-volatile UEFI variables"
diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile
index 034d26cf01..aaaa25cefe 100644
--- a/lib/efi_loader/Makefile
+++ b/lib/efi_loader/Makefile
@@ -68,7 +68,7 @@ obj-y += efi_watchdog.o
obj-$(CONFIG_EFI_ESRT) += efi_esrt.o
obj-$(CONFIG_LCD) += efi_gop.o
obj-$(CONFIG_DM_VIDEO) += efi_gop.o
-obj-$(CONFIG_PARTITIONS) += efi_disk.o
+obj-$(CONFIG_BLK) += efi_disk.o
obj-$(CONFIG_NET) += efi_net.o
obj-$(CONFIG_GENERATE_ACPI_TABLE) += efi_acpi.o
obj-$(CONFIG_GENERATE_SMBIOS_TABLE) += efi_smbios.o
diff --git a/lib/efi_loader/efi_device_path.c b/lib/efi_loader/efi_device_path.c
index 0542aaae16..50a988c561 100644
--- a/lib/efi_loader/efi_device_path.c
+++ b/lib/efi_loader/efi_device_path.c
@@ -864,11 +864,16 @@ static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
break;
case SIG_TYPE_GUID:
hddp->signature_type = 2;
+#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
+ /* info.uuid exists only with PARTITION_UUIDS */
if (uuid_str_to_bin(info.uuid,
- hddp->partition_signature, 1))
+ hddp->partition_signature,
+ UUID_STR_FORMAT_GUID)) {
log_warning(
- "Partition no. %d: invalid guid: %s\n",
+ "Partition %d: invalid GUID %s\n",
part, info.uuid);
+ }
+#endif
break;
}
diff --git a/lib/efi_loader/efi_disk.c b/lib/efi_loader/efi_disk.c
index c905c12abc..8fb5b2363c 100644
--- a/lib/efi_loader/efi_disk.c
+++ b/lib/efi_loader/efi_disk.c
@@ -10,6 +10,9 @@
#include <common.h>
#include <blk.h>
#include <dm.h>
+#include <dm/device-internal.h>
+#include <dm/tag.h>
+#include <event.h>
#include <efi_loader.h>
#include <fs.h>
#include <log.h>
@@ -33,7 +36,7 @@ const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
* @part: partition
* @volume: simple file system protocol of the partition
* @offset: offset into disk for simple partition
- * @desc: internal block device descriptor
+ * @dev: associated DM device
*/
struct efi_disk_obj {
struct efi_object header;
@@ -45,7 +48,7 @@ struct efi_disk_obj {
unsigned int part;
struct efi_simple_file_system_protocol *volume;
lbaint_t offset;
- struct blk_desc *desc;
+ struct udevice *dev; /* TODO: move it to efi_object */
};
/**
@@ -80,14 +83,12 @@ static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
void *buffer, enum efi_disk_direction direction)
{
struct efi_disk_obj *diskobj;
- struct blk_desc *desc;
int blksz;
int blocks;
unsigned long n;
diskobj = container_of(this, struct efi_disk_obj, ops);
- desc = (struct blk_desc *) diskobj->desc;
- blksz = desc->blksz;
+ blksz = diskobj->media.block_size;
blocks = buffer_size / blksz;
lba += diskobj->offset;
@@ -98,10 +99,21 @@ static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
if (buffer_size & (blksz - 1))
return EFI_BAD_BUFFER_SIZE;
+#if CONFIG_IS_ENABLED(PARTITIONS)
+ if (direction == EFI_DISK_READ)
+ n = dev_read(diskobj->dev, lba, blocks, buffer);
+ else
+ n = dev_write(diskobj->dev, lba, blocks, buffer);
+#else
+ /* dev is always a block device (UCLASS_BLK) */
+ struct blk_desc *desc;
+
+ desc = dev_get_uclass_plat(diskobj->dev);
if (direction == EFI_DISK_READ)
n = blk_dread(desc, lba, blocks, buffer);
else
n = blk_dwrite(desc, lba, blocks, buffer);
+#endif
/* We don't do interrupts, so check for timers cooperatively */
efi_timer_check();
@@ -443,7 +455,6 @@ static efi_status_t efi_disk_add_dev(
diskobj->ops = block_io_disk_template;
diskobj->ifname = if_typename;
diskobj->dev_index = dev_index;
- diskobj->desc = desc;
/* Fill in EFI IO Media info (for read/write callbacks) */
diskobj->media.removable_media = desc->removable;
@@ -487,103 +498,255 @@ error:
return ret;
}
-/**
- * efi_disk_create_partitions() - create handles and protocols for partitions
+/*
+ * Create a handle for a whole raw disk
+ *
+ * @dev uclass device (UCLASS_BLK)
*
- * Create handles and protocols for the partitions of a block device.
+ * Create an efi_disk object which is associated with @dev.
+ * The type of @dev must be UCLASS_BLK.
*
- * @parent: handle of the parent disk
- * @desc: block device
- * @if_typename: interface type
- * @diskid: device number
- * @pdevname: device name
- * Return: number of partitions created
+ * @return 0 on success, -1 otherwise
*/
-int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
- const char *if_typename, int diskid,
- const char *pdevname)
+static int efi_disk_create_raw(struct udevice *dev)
{
- int disks = 0;
- char devname[32] = { 0 }; /* dp->str is u16[32] long */
- int part;
- struct efi_device_path *dp = NULL;
+ struct efi_disk_obj *disk;
+ struct blk_desc *desc;
+ const char *if_typename;
+ int diskid;
efi_status_t ret;
+
+ desc = dev_get_uclass_plat(dev);
+ if_typename = blk_get_if_type_name(desc->if_type);
+ diskid = desc->devnum;
+
+ ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
+ diskid, NULL, 0, &disk);
+ if (ret != EFI_SUCCESS) {
+ if (ret == EFI_NOT_READY)
+ log_notice("Disk %s not ready\n", dev->name);
+ else
+ log_err("Adding disk for %s failed\n", dev->name);
+
+ return -1;
+ }
+ disk->dev = dev;
+ if (dev_tag_set_ptr(dev, DM_TAG_EFI, &disk->header)) {
+ efi_free_pool(disk->dp);
+ efi_delete_handle(&disk->header);
+
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ * Create a handle for a disk partition
+ *
+ * @dev uclass device (UCLASS_PARTITION)
+ *
+ * Create an efi_disk object which is associated with @dev.
+ * The type of @dev must be UCLASS_PARTITION.
+ *
+ * @return 0 on success, -1 otherwise
+ */
+static int efi_disk_create_part(struct udevice *dev)
+{
+ efi_handle_t parent;
+ struct blk_desc *desc;
+ const char *if_typename;
+ struct disk_part *part_data;
+ struct disk_partition *info;
+ unsigned int part;
+ int diskid;
struct efi_handler *handler;
+ struct efi_device_path *dp_parent;
+ struct efi_disk_obj *disk;
+ efi_status_t ret;
+
+ if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
+ return -1;
+
+ desc = dev_get_uclass_plat(dev_get_parent(dev));
+ if_typename = blk_get_if_type_name(desc->if_type);
+ diskid = desc->devnum;
+
+ part_data = dev_get_uclass_plat(dev);
+ part = part_data->partnum;
+ info = &part_data->gpt_part_info;
- /* Get the device path of the parent */
ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
- if (ret == EFI_SUCCESS)
- dp = handler->protocol_interface;
-
- /* Add devices for each partition */
- for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
- struct disk_partition info;
-
- if (part_get_info(desc, part, &info))
- continue;
- snprintf(devname, sizeof(devname), "%s:%x", pdevname,
- part);
- ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
- &info, part, NULL);
- if (ret != EFI_SUCCESS) {
- log_err("Adding partition %s failed\n", pdevname);
- continue;
- }
- disks++;
+ if (ret != EFI_SUCCESS)
+ return -1;
+ dp_parent = (struct efi_device_path *)handler->protocol_interface;
+
+ ret = efi_disk_add_dev(parent, dp_parent, if_typename, desc, diskid,
+ info, part, &disk);
+ if (ret != EFI_SUCCESS) {
+ log_err("Adding partition for %s failed\n", dev->name);
+ return -1;
+ }
+ disk->dev = dev;
+ if (dev_tag_set_ptr(dev, DM_TAG_EFI, &disk->header)) {
+ efi_free_pool(disk->dp);
+ efi_delete_handle(&disk->header);
+
+ return -1;
}
- return disks;
+ return 0;
}
-/**
- * efi_disk_register() - register block devices
+/*
+ * Create efi_disk objects for a block device
*
- * U-Boot doesn't have a list of all online disk devices. So when running our
- * EFI payload, we scan through all of the potentially available ones and
- * store them in our object pool.
+ * @dev uclass device (UCLASS_BLK)
*
- * This function is called in efi_init_obj_list().
+ * Create efi_disk objects for partitions as well as a raw disk
+ * which is associated with @dev.
+ * The type of @dev must be UCLASS_BLK.
+ * This function is expected to be called at EV_PM_POST_PROBE.
*
- * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
- * Consider converting the code to look up devices as needed. The EFI device
- * could be a child of the UCLASS_BLK block device, perhaps.
+ * @return 0 on success, -1 otherwise
+ */
+static int efi_disk_probe(void *ctx, struct event *event)
+{
+ struct udevice *dev;
+ enum uclass_id id;
+ struct blk_desc *desc;
+ struct udevice *child;
+ int ret;
+
+ dev = event->data.dm.dev;
+ id = device_get_uclass_id(dev);
+
+ /* TODO: We won't support partitions in a partition */
+ if (id != UCLASS_BLK)
+ return 0;
+
+ /*
+ * avoid creating duplicated objects now that efi_driver
+ * has already created an efi_disk at this moment.
+ */
+ desc = dev_get_uclass_plat(dev);
+ if (desc->if_type != IF_TYPE_EFI_LOADER) {
+ ret = efi_disk_create_raw(dev);
+ if (ret)
+ return -1;
+ }
+
+ device_foreach_child(child, dev) {
+ ret = efi_disk_create_part(child);
+ if (ret)
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ * Delete an efi_disk object for a whole raw disk
*
- * Return: status code
+ * @dev uclass device (UCLASS_BLK)
+ *
+ * Delete an efi_disk object which is associated with @dev.
+ * The type of @dev must be UCLASS_BLK.
+ *
+ * @return 0 on success, -1 otherwise
*/
-efi_status_t efi_disk_register(void)
+static int efi_disk_delete_raw(struct udevice *dev)
{
- struct efi_disk_obj *disk;
- int disks = 0;
- efi_status_t ret;
+ efi_handle_t handle;
+ struct blk_desc *desc;
+ struct efi_disk_obj *diskobj;
+
+ if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
+ return -1;
+
+ desc = dev_get_uclass_plat(dev);
+ if (desc->if_type != IF_TYPE_EFI_LOADER) {
+ diskobj = container_of(handle, struct efi_disk_obj, header);
+ efi_free_pool(diskobj->dp);
+ }
+
+ efi_delete_handle(handle);
+ dev_tag_del(dev, DM_TAG_EFI);
+
+ return 0;
+}
+
+/*
+ * Delete an efi_disk object for a disk partition
+ *
+ * @dev uclass device (UCLASS_PARTITION)
+ *
+ * Delete an efi_disk object which is associated with @dev.
+ * The type of @dev must be UCLASS_PARTITION.
+ *
+ * @return 0 on success, -1 otherwise
+ */
+static int efi_disk_delete_part(struct udevice *dev)
+{
+ efi_handle_t handle;
+ struct efi_disk_obj *diskobj;
+
+ if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
+ return -1;
+
+ diskobj = container_of(handle, struct efi_disk_obj, header);
+
+ efi_free_pool(diskobj->dp);
+ efi_delete_handle(handle);
+ dev_tag_del(dev, DM_TAG_EFI);
+
+ return 0;
+}
+
+/*
+ * Delete an efi_disk object for a block device
+ *
+ * @dev uclass device (UCLASS_BLK or UCLASS_PARTITION)
+ *
+ * Delete an efi_disk object which is associated with @dev.
+ * The type of @dev must be either UCLASS_BLK or UCLASS_PARTITION.
+ * This function is expected to be called at EV_PM_PRE_REMOVE.
+ *
+ * @return 0 on success, -1 otherwise
+ */
+static int efi_disk_remove(void *ctx, struct event *event)
+{
+ enum uclass_id id;
struct udevice *dev;
- for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
- uclass_next_device_check(&dev)) {
- struct blk_desc *desc = dev_get_uclass_plat(dev);
- const char *if_typename = blk_get_if_type_name(desc->if_type);
+ dev = event->data.dm.dev;
+ id = device_get_uclass_id(dev);
- /* Add block device for the full device */
- log_info("Scanning disk %s...\n", dev->name);
- ret = efi_disk_add_dev(NULL, NULL, if_typename,
- desc, desc->devnum, NULL, 0, &disk);
- if (ret == EFI_NOT_READY) {
- log_notice("Disk %s not ready\n", dev->name);
- continue;
- }
- if (ret) {
- log_err("ERROR: failure to add disk device %s, r = %lu\n",
- dev->name, ret & ~EFI_ERROR_MASK);
- continue;
- }
- disks++;
+ if (id == UCLASS_BLK)
+ return efi_disk_delete_raw(dev);
+ else if (id == UCLASS_PARTITION)
+ return efi_disk_delete_part(dev);
+ else
+ return 0;
+}
+
+efi_status_t efi_disk_init(void)
+{
+ int ret;
- /* Partitions show up as block devices in EFI */
- disks += efi_disk_create_partitions(
- &disk->header, desc, if_typename,
- desc->devnum, dev->name);
+ ret = event_register("efi_disk add", EVT_DM_POST_PROBE,
+ efi_disk_probe, NULL);
+ if (ret) {
+ log_err("Event registration for efi_disk add failed\n");
+ return EFI_OUT_OF_RESOURCES;
}
- log_info("Found %d disks\n", disks);
+ ret = event_register("efi_disk del", EVT_DM_PRE_REMOVE,
+ efi_disk_remove, NULL);
+ if (ret) {
+ log_err("Event registration for efi_disk del failed\n");
+ return EFI_OUT_OF_RESOURCES;
+ }
return EFI_SUCCESS;
}
diff --git a/lib/efi_loader/efi_setup.c b/lib/efi_loader/efi_setup.c
index eee54e4878..250eeb2fcd 100644
--- a/lib/efi_loader/efi_setup.c
+++ b/lib/efi_loader/efi_setup.c
@@ -174,20 +174,18 @@ static efi_status_t efi_init_os_indications(void)
&os_indications_supported, false);
}
-
/**
- * efi_init_obj_list() - Initialize and populate EFI object list
+ * __efi_init_early() - handle initialization at early stage
+ *
+ * This function is called in efi_init_obj_list() only if
+ * !CONFIG_EFI_SETUP_EARLY.
*
* Return: status code
*/
-efi_status_t efi_init_obj_list(void)
+static efi_status_t __efi_init_early(void)
{
efi_status_t ret = EFI_SUCCESS;
- /* Initialize once only */
- if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
- return efi_obj_list_initialized;
-
/* Allow unaligned memory access */
allow_unaligned();
@@ -200,11 +198,51 @@ efi_status_t efi_init_obj_list(void)
if (ret != EFI_SUCCESS)
goto out;
-#ifdef CONFIG_PARTITIONS
- ret = efi_disk_register();
- if (ret != EFI_SUCCESS)
- goto out;
-#endif
+ ret = efi_disk_init();
+out:
+ return ret;
+}
+
+/**
+ * efi_init_early() - handle initialization at early stage
+ *
+ * external version of __efi_init_early(); expected to be called in
+ * board_init_r().
+ *
+ * Return: status code
+ */
+int efi_init_early(void)
+{
+ efi_status_t ret;
+
+ ret = __efi_init_early();
+ if (ret != EFI_SUCCESS) {
+ /* never re-init UEFI subsystem */
+ efi_obj_list_initialized = ret;
+ return -1;
+ }
+ return 0;
+}
+
+/**
+ * efi_init_obj_list() - Initialize and populate EFI object list
+ *
+ * Return: status code
+ */
+efi_status_t efi_init_obj_list(void)
+{
+ efi_status_t ret = EFI_SUCCESS;
+
+ /* Initialize once only */
+ if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
+ return efi_obj_list_initialized;
+
+ if (!IS_ENABLED(CONFIG_EFI_SETUP_EARLY)) {
+ ret = __efi_init_early();
+ if (ret != EFI_SUCCESS)
+ goto out;
+ }
+
if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) {
ret = efi_rng_register();
if (ret != EFI_SUCCESS)