efi_loader: simplify efi_disk_remove

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@linaro.org>

If a handle is not found, return 0 to let the device be removed.

Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
diff --git a/lib/efi_loader/efi_disk.c b/lib/efi_loader/efi_disk.c
index d225671..28c8cdf 100644
--- a/lib/efi_loader/efi_disk.c
+++ b/lib/efi_loader/efi_disk.c
@@ -690,89 +690,50 @@
 	return 0;
 }
 
-/*
- * Delete an efi_disk object for a whole raw disk
+/**
+ * efi_disk_remove - delete an efi_disk object for a block device or partition
  *
- * @dev		uclass device (UCLASS_BLK)
+ * @ctx:	event context: driver binding protocol
+ * @event:	EV_PM_PRE_REMOVE event
  *
- * Delete an efi_disk object which is associated with @dev.
- * The type of @dev must be UCLASS_BLK.
+ * Delete an efi_disk object which is associated with the UCLASS_BLK or
+ * UCLASS_PARTITION device for which the EV_PM_PRE_REMOVE event is raised.
  *
- * @return	0 on success, -1 otherwise
- */
-static int efi_disk_delete_raw(struct udevice *dev)
-{
-	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->uclass_id != UCLASS_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
+ * Return:	0 on success, -1 otherwise
  */
 int efi_disk_remove(void *ctx, struct event *event)
 {
 	enum uclass_id id;
-	struct udevice *dev;
+	struct udevice *dev = event->data.dm.dev;
+	efi_handle_t handle;
+	struct blk_desc *desc;
+	struct efi_disk_obj *diskobj = NULL;
 
-	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
+	if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
 		return 0;
+
+	id = device_get_uclass_id(dev);
+	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);
+		break;
+	case UCLASS_PARTITION:
+		diskobj = container_of(handle, struct efi_disk_obj, header);
+		break;
+	default:
+		return 0;
+	}
+
+	if (diskobj)
+		efi_free_pool(diskobj->dp);
+
+	efi_delete_handle(handle);
+	dev_tag_del(dev, DM_TAG_EFI);
+
+	return 0;
 }
 
 /**