[PATCH v2] efi_loader: simplify efi_disk_remove

Ilias Apalodimas ilias.apalodimas at linaro.org
Mon Jun 12 17:35:58 CEST 2023


Instead of discovering the ID of the device and call two different
functions for a block device or a partition, we can rewrite
efi_disk_remove() and handle the minor differences between the two
variants internally.  As a results we can simplify efi_disk_remove()
a lot and get rid of the extra efi_disk_delete_raw/blk calls.

Signed-off-by: Ilias Apalodimas <ilias.apalodimas at linaro.org>
---
- Fix CI issues. dev_tag_get_ptr() is called for UCLASS_BLK or UCLASS_PARTITION
  only

 lib/efi_loader/efi_disk.c | 80 ++++++++++-----------------------------
 1 file changed, 19 insertions(+), 61 deletions(-)

diff --git a/lib/efi_loader/efi_disk.c b/lib/efi_loader/efi_disk.c
index d2256713a8e7..62ca8d7fa516 100644
--- a/lib/efi_loader/efi_disk.c
+++ b/lib/efi_loader/efi_disk.c
@@ -691,90 +691,48 @@ int efi_disk_probe(void *ctx, struct event *event)
 }

 /*
- * Delete an efi_disk object for a whole raw disk
+ * Delete an efi_disk object for a block device
  *
- * @dev		uclass device (UCLASS_BLK)
+ * @dev		uclass device (UCLASS_BLK or UCLASS_PARTITION)
  *
  * Delete an efi_disk object which is associated with @dev.
- * The type of @dev must be UCLASS_BLK.
+ * 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_delete_raw(struct udevice *dev)
+int efi_disk_remove(void *ctx, struct event *event)
 {
+	enum uclass_id id;
+	struct udevice *dev = event->data.dm.dev;
 	efi_handle_t handle;
 	struct blk_desc *desc;
-	struct efi_disk_obj *diskobj;
+	struct efi_disk_obj *diskobj = NULL;
+
+	id = device_get_uclass_id(dev);
+	if (id != UCLASS_BLK && id != UCLASS_PARTITION)
+		return 0;

 	if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
 		return -1;

-	desc = dev_get_uclass_plat(dev);
-	if (desc->uclass_id != UCLASS_EFI_LOADER) {
+	if (id == 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);
+	} else if (id == UCLASS_PARTITION) {
 		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);
+	if (diskobj)
+		efi_free_pool(diskobj->dp);

-	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
- */
-int efi_disk_remove(void *ctx, struct event *event)
-{
-	enum uclass_id id;
-	struct udevice *dev;
-
-	dev = event->data.dm.dev;
-	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 0;
-}
-
 /**
  * efi_disk_get_device_name() - get U-Boot device name associated with EFI handle
  *
--
2.39.2



More information about the U-Boot mailing list