aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2024-01-21 09:11:33 -0500
committerTom Rini <trini@konsulko.com>2024-01-21 09:11:33 -0500
commit22aeab2d969b0d745104311cd0f968be85c38eb5 (patch)
tree28b304123b9836b17e7e3d22fe0f921d8dcb5963
parent3c04fcf3137d5f694d52b8f355373e4baabe5f78 (diff)
parent2c98f7435cc5edb2a0b96e9f398c0b7f084e83cd (diff)
downloadu-boot-WIP/21Jan2024.zip
u-boot-WIP/21Jan2024.tar.gz
u-boot-WIP/21Jan2024.tar.bz2
Merge tag 'efi-2024-04-rc1-3' of https://source.denx.de/u-boot/custodians/u-boot-efiWIP/21Jan2024
Pull request efi-2024-04-rc1-3 Documentation: * correct documentation of part_get_bootable() * remove duplicate word "has" in UEFI documentation UEFI: * rename check_disk_has_default_file function * auto-generate boot option for each blkio device * auto-generate removable media boot option first * avoid pointer access after calling efi_delete_handle * create common function to free struct efi_disk_obj * return immediately in UCLASS_EFI_LOADER removal
-rw-r--r--doc/develop/uefi/uefi.rst2
-rw-r--r--include/part.h4
-rw-r--r--lib/efi_loader/efi_bootmgr.c141
-rw-r--r--lib/efi_loader/efi_disk.c52
4 files changed, 147 insertions, 52 deletions
diff --git a/doc/develop/uefi/uefi.rst b/doc/develop/uefi/uefi.rst
index 6bc9d92..c739242 100644
--- a/doc/develop/uefi/uefi.rst
+++ b/doc/develop/uefi/uefi.rst
@@ -830,7 +830,7 @@ driver on a device the ConnectController service is called. In this context
controller refers to the device for which the driver is installed.
The relevant drivers are identified using the EFI_DRIVER_BINDING_PROTOCOL. This
-protocol has has three functions:
+protocol has three functions:
* supported - determines if the driver is compatible with the device
* start - installs the driver by opening the relevant protocol with
diff --git a/include/part.h b/include/part.h
index db34bc6..32ee404 100644
--- a/include/part.h
+++ b/include/part.h
@@ -685,8 +685,8 @@ int part_get_type_by_name(const char *name);
/**
* part_get_bootable() - Find the first bootable partition
*
- * @desc: Block-device descriptor
- * @return first bootable partition, or 0 if there is none
+ * @desc: Block-device descriptor
+ * Return: first bootable partition, or 0 if there is none
*/
int part_get_bootable(struct blk_desc *desc);
diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
index 68d7db5..4ac5192 100644
--- a/lib/efi_loader/efi_bootmgr.c
+++ b/lib/efi_loader/efi_bootmgr.c
@@ -294,14 +294,17 @@ err:
}
/**
- * check_disk_has_default_file() - load the default file
+ * fill_default_file_path() - get fallback boot device path for block device
+ *
+ * Provide the device path to the fallback UEFI boot file, e.g.
+ * EFI/BOOT/BOOTAA64.EFI if that file exists on the block device @blk.
*
* @blk: pointer to the UCLASS_BLK udevice
- * @dp: pointer to default file device path
+ * @dp: pointer to store the fallback boot device path
* Return: status code
*/
-static efi_status_t check_disk_has_default_file(struct udevice *blk,
- struct efi_device_path **dp)
+static efi_status_t fill_default_file_path(struct udevice *blk,
+ struct efi_device_path **dp)
{
efi_status_t ret;
struct udevice *partition;
@@ -348,7 +351,7 @@ static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size,
if (!ramdisk_blk)
return EFI_LOAD_ERROR;
- ret = check_disk_has_default_file(ramdisk_blk, dp);
+ ret = fill_default_file_path(ramdisk_blk, dp);
if (ret != EFI_SUCCESS) {
log_info("Cannot boot from downloaded image\n");
goto err;
@@ -547,6 +550,50 @@ err:
}
/**
+ * try_load_from_media() - load file from media
+ *
+ * @file_path: file path
+ * @handle_img: on return handle for the newly installed image
+ *
+ * If @file_path contains a file name, load the file.
+ * If @file_path does not have a file name, search the architecture-specific
+ * fallback boot file and load it.
+ * TODO: If the FilePathList[0] device does not support
+ * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL but supports EFI_BLOCK_IO_PROTOCOL,
+ * call EFI_BOOT_SERVICES.ConnectController()
+ * TODO: FilePathList[0] device supports the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
+ * not based on EFI_BLOCK_IO_PROTOCOL
+ *
+ * Return: status code
+ */
+static efi_status_t try_load_from_media(struct efi_device_path *file_path,
+ efi_handle_t *handle_img)
+{
+ efi_handle_t handle_blkdev;
+ efi_status_t ret = EFI_SUCCESS;
+ struct efi_device_path *rem, *dp = NULL;
+ struct efi_device_path *final_dp = file_path;
+
+ handle_blkdev = efi_dp_find_obj(file_path, &efi_block_io_guid, &rem);
+ if (handle_blkdev) {
+ if (rem->type == DEVICE_PATH_TYPE_END) {
+ /* no file name present, try default file */
+ ret = fill_default_file_path(handle_blkdev->dev, &dp);
+ if (ret != EFI_SUCCESS)
+ return ret;
+
+ final_dp = dp;
+ }
+ }
+
+ ret = EFI_CALL(efi_load_image(true, efi_root, final_dp, NULL, 0, handle_img));
+
+ efi_free_pool(dp);
+
+ return ret;
+}
+
+/**
* try_load_entry() - try to load image for boot option
*
* Attempt to load load-option number 'n', returning device_path and file_path
@@ -580,7 +627,6 @@ static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
}
if (lo.attributes & LOAD_OPTION_ACTIVE) {
- struct efi_device_path *file_path;
u32 attributes;
log_debug("trying to load \"%ls\" from %pD\n", lo.label,
@@ -597,10 +643,7 @@ static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
else
ret = EFI_LOAD_ERROR;
} else {
- file_path = expand_media_path(lo.file_path);
- ret = EFI_CALL(efi_load_image(true, efi_root, file_path,
- NULL, 0, handle));
- efi_free_pool(file_path);
+ ret = try_load_from_media(lo.file_path, handle);
}
if (ret != EFI_SUCCESS) {
log_warning("Loading %ls '%ls' failed\n",
@@ -731,22 +774,26 @@ error:
}
/**
- * efi_bootmgr_enumerate_boot_option() - enumerate the possible bootable media
+ * efi_bootmgr_enumerate_boot_options() - enumerate the possible bootable media
*
* @opt: pointer to the media boot option structure
- * @volume_handles: pointer to the efi handles
- * @count: number of efi handle
+ * @index: index of the opt array to store the boot option
+ * @handles: pointer to block device handles
+ * @count: On entry number of handles to block devices.
+ * On exit number of boot options.
+ * @removable: flag to parse removable only
* Return: status code
*/
-static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
- efi_handle_t *volume_handles,
- efi_status_t count)
+static efi_status_t
+efi_bootmgr_enumerate_boot_options(struct eficonfig_media_boot_option *opt,
+ efi_uintn_t index, efi_handle_t *handles,
+ efi_uintn_t *count, bool removable)
{
- u32 i;
+ u32 i, num = index;
struct efi_handler *handler;
efi_status_t ret = EFI_SUCCESS;
- for (i = 0; i < count; i++) {
+ for (i = 0; i < *count; i++) {
u16 *p;
u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
char *optional_data;
@@ -754,8 +801,18 @@ static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo
char buf[BOOTMENU_DEVICE_NAME_MAX];
struct efi_device_path *device_path;
struct efi_device_path *short_dp;
+ struct efi_block_io *blkio;
+
+ ret = efi_search_protocol(handles[i], &efi_block_io_guid, &handler);
+ blkio = handler->protocol_interface;
+
+ if (blkio->media->logical_partition)
+ continue;
- ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
+ if (removable != (blkio->media->removable_media != 0))
+ continue;
+
+ ret = efi_search_protocol(handles[i], &efi_guid_device_path, &handler);
if (ret != EFI_SUCCESS)
continue;
ret = efi_protocol_open(handler, (void **)&device_path,
@@ -763,7 +820,7 @@ static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo
if (ret != EFI_SUCCESS)
continue;
- ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
+ ret = efi_disk_get_device_name(handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
if (ret != EFI_SUCCESS)
continue;
@@ -787,17 +844,23 @@ static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo
* to store guid, instead of realloc the load_option.
*/
lo.optional_data = "1234567";
- opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
- if (!opt[i].size) {
+ opt[num].size = efi_serialize_load_option(&lo, (u8 **)&opt[num].lo);
+ if (!opt[num].size) {
ret = EFI_OUT_OF_RESOURCES;
goto out;
}
/* set the guid */
- optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));
+ optional_data = (char *)opt[num].lo + (opt[num].size - u16_strsize(u"1234567"));
memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
+ num++;
+
+ if (num >= *count)
+ break;
}
out:
+ *count = num;
+
return ret;
}
@@ -1026,8 +1089,7 @@ efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index)
/**
* efi_bootmgr_update_media_device_boot_option() - generate the media device boot option
*
- * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
- * and generate the bootmenu entries.
+ * This function enumerates all BlockIo devices and add the boot option for it.
* This function also provide the BOOT#### variable maintenance for
* the media device entries.
* - Automatically create the BOOT#### variable for the newly detected device,
@@ -1042,14 +1104,14 @@ efi_status_t efi_bootmgr_update_media_device_boot_option(void)
{
u32 i;
efi_status_t ret;
- efi_uintn_t count;
- efi_handle_t *volume_handles = NULL;
+ efi_uintn_t count, num, total;
+ efi_handle_t *handles = NULL;
struct eficonfig_media_boot_option *opt = NULL;
ret = efi_locate_handle_buffer_int(BY_PROTOCOL,
- &efi_simple_file_system_protocol_guid,
+ &efi_block_io_guid,
NULL, &count,
- (efi_handle_t **)&volume_handles);
+ (efi_handle_t **)&handles);
if (ret != EFI_SUCCESS)
goto out;
@@ -1059,23 +1121,32 @@ efi_status_t efi_bootmgr_update_media_device_boot_option(void)
goto out;
}
- /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
- ret = efi_bootmgr_enumerate_boot_option(opt, volume_handles, count);
+ /* parse removable block io followed by fixed block io */
+ num = count;
+ ret = efi_bootmgr_enumerate_boot_options(opt, 0, handles, &num, true);
if (ret != EFI_SUCCESS)
goto out;
+ total = num;
+ num = count;
+ ret = efi_bootmgr_enumerate_boot_options(opt, total, handles, &num, false);
+ if (ret != EFI_SUCCESS)
+ goto out;
+
+ total = num;
+
/*
* System hardware configuration may vary depending on the user setup.
* The boot option is automatically added by the bootmenu.
* If the device is not attached to the system, the boot option needs
* to be deleted.
*/
- ret = efi_bootmgr_delete_invalid_boot_option(opt, count);
+ ret = efi_bootmgr_delete_invalid_boot_option(opt, total);
if (ret != EFI_SUCCESS)
goto out;
/* add non-existent boot option */
- for (i = 0; i < count; i++) {
+ for (i = 0; i < total; i++) {
u32 boot_index;
u16 var_name[9];
@@ -1104,11 +1175,11 @@ efi_status_t efi_bootmgr_update_media_device_boot_option(void)
out:
if (opt) {
- for (i = 0; i < count; i++)
+ for (i = 0; i < total; i++)
free(opt[i].lo);
}
free(opt);
- efi_free_pool(volume_handles);
+ efi_free_pool(handles);
if (ret == EFI_NOT_FOUND)
return EFI_SUCCESS;
diff --git a/lib/efi_loader/efi_disk.c b/lib/efi_loader/efi_disk.c
index 013842f..b1739d9 100644
--- a/lib/efi_loader/efi_disk.c
+++ b/lib/efi_loader/efi_disk.c
@@ -371,6 +371,20 @@ static int efi_fs_exists(struct blk_desc *desc, int part)
return 1;
}
+static void efi_disk_free_diskobj(struct efi_disk_obj *diskobj)
+{
+ struct efi_device_path *dp = diskobj->dp;
+ struct efi_simple_file_system_protocol *volume = diskobj->volume;
+
+ /*
+ * ignore error of efi_delete_handle() since this function
+ * is expected to be called in error path.
+ */
+ efi_delete_handle(&diskobj->header);
+ efi_free_pool(dp);
+ free(volume);
+}
+
/**
* efi_disk_add_dev() - create a handle for a partition or disk
*
@@ -528,9 +542,7 @@ static efi_status_t efi_disk_add_dev(
}
return EFI_SUCCESS;
error:
- efi_delete_handle(&diskobj->header);
- free(diskobj->volume);
- free(diskobj);
+ efi_disk_free_diskobj(diskobj);
return ret;
}
@@ -569,8 +581,7 @@ static int efi_disk_create_raw(struct udevice *dev, efi_handle_t agent_handle)
return ret;
}
if (efi_link_dev(&disk->header, dev)) {
- efi_free_pool(disk->dp);
- efi_delete_handle(&disk->header);
+ efi_disk_free_diskobj(disk);
return -EINVAL;
}
@@ -624,8 +635,9 @@ static int efi_disk_create_part(struct udevice *dev, efi_handle_t agent_handle)
return -1;
}
if (efi_link_dev(&disk->header, dev)) {
- efi_free_pool(disk->dp);
- efi_delete_handle(&disk->header);
+ efi_disk_free_diskobj(disk);
+
+ /* TODO: closing the parent EFI_BLOCK_IO_PROTOCOL is missing. */
return -1;
}
@@ -707,7 +719,9 @@ int efi_disk_remove(void *ctx, struct event *event)
struct udevice *dev = event->data.dm.dev;
efi_handle_t handle;
struct blk_desc *desc;
+ struct efi_device_path *dp = NULL;
struct efi_disk_obj *diskobj = NULL;
+ struct efi_simple_file_system_protocol *volume = NULL;
efi_status_t ret;
if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
@@ -717,25 +731,35 @@ int efi_disk_remove(void *ctx, struct event *event)
switch (id) {
case UCLASS_BLK:
desc = dev_get_uclass_plat(dev);
- if (desc && desc->uclass_id != UCLASS_EFI_LOADER)
- diskobj = container_of(handle, struct efi_disk_obj,
- header);
+ if (desc && desc->uclass_id == UCLASS_EFI_LOADER)
+ /*
+ * EFI application/driver manages the EFI handle,
+ * no need to delete EFI handle.
+ */
+ return 0;
+
+ diskobj = (struct efi_disk_obj *)handle;
break;
case UCLASS_PARTITION:
- diskobj = container_of(handle, struct efi_disk_obj, header);
+ diskobj = (struct efi_disk_obj *)handle;
+
+ /* TODO: closing the parent EFI_BLOCK_IO_PROTOCOL is missing. */
+
break;
default:
return 0;
}
+ dp = diskobj->dp;
+ volume = diskobj->volume;
+
ret = efi_delete_handle(handle);
/* Do not delete DM device if there are still EFI drivers attached. */
if (ret != EFI_SUCCESS)
return -1;
- if (diskobj)
- efi_free_pool(diskobj->dp);
-
+ efi_free_pool(dp);
+ free(volume);
dev_tag_del(dev, DM_TAG_EFI);
return 0;