[RFC 18/22] efi_loader: efi_disk: a helper function to delete efi_disk objects

AKASHI Takahiro
2021-10-01T05:09:39Z

This function is expected to be called, in particular from dm's pre_remove
hook, when associated block devices no longer exist.

Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
---
include/efi_loader.h | 2 ++
lib/efi_loader/efi_disk.c | 54 +++++++++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index 50f4119dcdfb..7079549bea70 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -519,6 +519,8 @@ void efi_carve_out_dt_rsv(void *fdt);
efi_status_t efi_console_register(void);
/* Called when a block devices has been probed */
int efi_disk_create(struct udevice *dev);
+/* Called when a block devices is to be removed */
+int efi_disk_delete(struct udevice *dev);
/* Called by efi_init_obj_list() to install EFI_RNG_PROTOCOL */
efi_status_t efi_rng_register(void);
/* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
diff --git a/lib/efi_loader/efi_disk.c b/lib/efi_loader/efi_disk.c
index 74ef923d1d67..dfd06dd31e4a 100644
--- a/lib/efi_loader/efi_disk.c
+++ b/lib/efi_loader/efi_disk.c
@@ -574,6 +574,60 @@ int efi_disk_create(struct udevice *dev)
return -1;
}

+static int efi_disk_delete_raw(struct udevice *dev)
+{
+ efi_handle_t handle = dev->efi_obj;
+ struct blk_desc *desc;
+ struct efi_disk_obj *diskobj;
+
+ desc = dev_get_uclass_plat(dev);
+ if (desc->if_type != IF_TYPE_EFI) {
+ diskobj = container_of(handle, struct efi_disk_obj, header);
+ efi_free_pool(diskobj->dp);
+ }
+
+ /*
+ * TODO: Can we use efi_delete_handle() here?
+ */
+ efi_remove_all_protocols(handle);
+
+ efi_remove_handle(handle);
+ free(diskobj);
+
+ return 0;
+}
+
+static int efi_disk_delete_part(struct udevice *dev)
+{
+ efi_handle_t handle = dev->efi_obj;
+ struct efi_disk_obj *diskobj;
+
+ diskobj = container_of(handle, struct efi_disk_obj, header);
+
+ efi_free_pool(diskobj->dp);
+
+ efi_remove_all_protocols(handle);
+
+ efi_remove_handle(handle);
+ free(diskobj);
+
+ return 0;
+}
+
+int efi_disk_delete(struct udevice *dev)
+{
+ enum uclass_id id;
+
+ id = device_get_uclass_id(dev);
+
+ if (id == UCLASS_BLK)
+ return efi_disk_delete_raw(dev);
+ else if (id == UCLASS_PARTITION)
+ return efi_disk_delete_part(dev);
+ else
+ return -1;
+}
+
/**
* efi_disk_is_system_part() - check if handle refers to an EFI system partition
*
--
2.33.0