From 43a5891c66c8fe961999415b051c827ce1b543c4 Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Mon, 3 Oct 2022 10:35:35 +0200 Subject: efi_driver: fix error handling If creating the block device fails, * delete all created objects and references * close the protocol interface on the controller Signed-off-by: Heinrich Schuchardt --- lib/efi_driver/efi_block_device.c | 60 +++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 27 deletions(-) (limited to 'lib/efi_driver/efi_block_device.c') diff --git a/lib/efi_driver/efi_block_device.c b/lib/efi_driver/efi_block_device.c index 3177ab67b81..9ccc1485907 100644 --- a/lib/efi_driver/efi_block_device.c +++ b/lib/efi_driver/efi_block_device.c @@ -112,12 +112,13 @@ static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, * * @handle: handle * @interface: block io protocol - * Return: 0 = success + * Return: status code */ -static int efi_bl_bind(efi_handle_t handle, void *interface) +static efi_status_t efi_bl_bind(efi_handle_t handle, void *interface) { - struct udevice *bdev, *parent = dm_root(); - int ret, devnum; + struct udevice *bdev = NULL, *parent = dm_root(); + efi_status_t ret; + int devnum; char *name; struct efi_object *obj = efi_search_obj(handle); struct efi_block_io *io = interface; @@ -125,28 +126,28 @@ static int efi_bl_bind(efi_handle_t handle, void *interface) EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, io); - if (!obj) - return -ENOENT; + if (!obj || !interface) + return EFI_INVALID_PARAMETER; devnum = blk_find_max_devnum(UCLASS_EFI_LOADER); if (devnum == -ENODEV) devnum = 0; else if (devnum < 0) - return devnum; + return EFI_OUT_OF_RESOURCES; name = calloc(1, 18); /* strlen("efiblk#2147483648") + 1 */ if (!name) - return -ENOMEM; + return EFI_OUT_OF_RESOURCES; sprintf(name, "efiblk#%d", devnum); /* Create driver model udevice for the EFI block io device */ - ret = blk_create_device(parent, "efi_blk", name, UCLASS_EFI_LOADER, - devnum, io->media->block_size, - (lbaint_t)io->media->last_block, &bdev); - if (ret) - return ret; - if (!bdev) - return -ENOENT; + if (blk_create_device(parent, "efi_blk", name, UCLASS_EFI_LOADER, + devnum, io->media->block_size, + (lbaint_t)io->media->last_block, &bdev)) { + ret = EFI_OUT_OF_RESOURCES; + free(name); + goto err; + } /* Set the DM_FLAG_NAME_ALLOCED flag to avoid a memory leak */ device_set_name_alloced(bdev); @@ -154,20 +155,25 @@ 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(). - */ - if (efi_link_dev(handle, bdev)) - /* FIXME: cleanup for bdev */ - return ret; - - ret = device_probe(bdev); - if (ret) - return ret; + if (efi_link_dev(handle, bdev)) { + ret = EFI_OUT_OF_RESOURCES; + goto err; + } + + if (device_probe(bdev)) { + ret = EFI_DEVICE_ERROR; + goto err; + } EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name); - return 0; + return EFI_SUCCESS; + +err: + efi_unlink_dev(handle); + if (bdev) + device_unbind(bdev); + + return ret; } /* Block device driver operators */ -- cgit v1.2.3 From a6d4f704ad4ae91f66d1901877f072f762812c39 Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Tue, 4 Oct 2022 16:19:30 +0200 Subject: efi_driver: carve out function to create block device * Carve out function efi_bl_create_block_device() from efi_bl_bind(). * Add a check for U-Boot devices to efi_bl_bind(). Signed-off-by: Heinrich Schuchardt --- lib/efi_driver/efi_block_device.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'lib/efi_driver/efi_block_device.c') diff --git a/lib/efi_driver/efi_block_device.c b/lib/efi_driver/efi_block_device.c index 9ccc1485907..10f3cb90c43 100644 --- a/lib/efi_driver/efi_block_device.c +++ b/lib/efi_driver/efi_block_device.c @@ -114,21 +114,16 @@ static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, * @interface: block io protocol * Return: status code */ -static efi_status_t efi_bl_bind(efi_handle_t handle, void *interface) +static efi_status_t +efi_bl_create_block_device(efi_handle_t handle, void *interface) { struct udevice *bdev = NULL, *parent = dm_root(); efi_status_t ret; int devnum; char *name; - struct efi_object *obj = efi_search_obj(handle); struct efi_block_io *io = interface; struct efi_blk_plat *plat; - EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, io); - - if (!obj || !interface) - return EFI_INVALID_PARAMETER; - devnum = blk_find_max_devnum(UCLASS_EFI_LOADER); if (devnum == -ENODEV) devnum = 0; @@ -176,6 +171,29 @@ err: return ret; } +/** + * efi_bl_bind() - bind to a block io protocol + * + * @handle: handle + * @interface: block io protocol + * Return: status code + */ +static efi_status_t efi_bl_bind(efi_handle_t handle, void *interface) +{ + efi_status_t ret = EFI_SUCCESS; + struct efi_object *obj = efi_search_obj(handle); + + EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, interface); + + if (!obj || !interface) + return EFI_INVALID_PARAMETER; + + if (!handle->dev) + ret = efi_bl_create_block_device(handle, interface); + + return ret; +} + /* Block device driver operators */ static const struct blk_ops efi_blk_ops = { .read = efi_bl_read, -- cgit v1.2.3 From 939f204c5a37e87052b1967cbd6971109b7176e7 Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Tue, 4 Oct 2022 18:53:34 +0200 Subject: efi_driver: reformat efi_block_device.c * use Sphinx documentation style * correct indentation Signed-off-by: Heinrich Schuchardt --- lib/efi_driver/efi_block_device.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'lib/efi_driver/efi_block_device.c') diff --git a/lib/efi_driver/efi_block_device.c b/lib/efi_driver/efi_block_device.c index 10f3cb90c43..e9eabbde58d 100644 --- a/lib/efi_driver/efi_block_device.c +++ b/lib/efi_driver/efi_block_device.c @@ -37,11 +37,11 @@ #include #include -/* - * EFI attributes of the udevice handled by this driver. +/** + * struct efi_blk_plat - attributes of a block device * - * handle handle of the controller on which this driver is installed - * io block io protocol proxied by this driver + * @handle: handle of the controller on which this driver is installed + * @io: block io protocol proxied by this driver */ struct efi_blk_plat { efi_handle_t handle; @@ -49,7 +49,7 @@ struct efi_blk_plat { }; /** - * Read from block device + * efi_bl_read() - read from block device * * @dev: device * @blknr: first block to be read @@ -78,7 +78,7 @@ static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, } /** - * Write to block device + * efi_bl_write() - write to block device * * @dev: device * @blknr: first block to be write @@ -108,7 +108,7 @@ static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, } /** - * Create a block device for a handle + * efi_bl_create_block_device() - create a block device for a handle * * @handle: handle * @interface: block io protocol @@ -202,9 +202,9 @@ static const struct blk_ops efi_blk_ops = { /* Identify as block device driver */ U_BOOT_DRIVER(efi_blk) = { - .name = "efi_blk", - .id = UCLASS_BLK, - .ops = &efi_blk_ops, + .name = "efi_blk", + .id = UCLASS_BLK, + .ops = &efi_blk_ops, .plat_auto = sizeof(struct efi_blk_plat), }; -- cgit v1.2.3 From ec4f675f9ebec2535f2cd050aed7f9c106a5bee9 Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Tue, 4 Oct 2022 19:12:59 +0200 Subject: efi_driver: provide driver binding protocol to bind function DisconnectController() is based on the open protocol information created when the driver opens a protocol with BY_CHILD_CONTROLLER or BY_DRIVER. To create an open protocol information it is required to supply the handle of the driver as agent handle. This information is available as field DriverBindingHandle in the driver binding protocol. Signed-off-by: Heinrich Schuchardt --- lib/efi_driver/efi_block_device.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/efi_driver/efi_block_device.c') diff --git a/lib/efi_driver/efi_block_device.c b/lib/efi_driver/efi_block_device.c index e9eabbde58d..f440067f702 100644 --- a/lib/efi_driver/efi_block_device.c +++ b/lib/efi_driver/efi_block_device.c @@ -174,11 +174,14 @@ err: /** * efi_bl_bind() - bind to a block io protocol * + * @this: driver binding protocol * @handle: handle * @interface: block io protocol * Return: status code */ -static efi_status_t efi_bl_bind(efi_handle_t handle, void *interface) +static efi_status_t efi_bl_bind( + struct efi_driver_binding_extended_protocol *this, + efi_handle_t handle, void *interface) { efi_status_t ret = EFI_SUCCESS; struct efi_object *obj = efi_search_obj(handle); -- cgit v1.2.3 From 8f8fe1d458664aaa15fa82de78dfdb0eca74b2ca Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Wed, 5 Oct 2022 11:28:47 +0200 Subject: efi_driver: add init function to EFI block driver For handling added and removed block devices we need to register events which has to be done when the driver is installed. This patch only creates an empty init function that will be filled with code later on. The function needs to be called before any EFI block devices are used. Move the efi_driver_init() call to early init. Signed-off-by: Heinrich Schuchardt --- lib/efi_driver/efi_block_device.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'lib/efi_driver/efi_block_device.c') diff --git a/lib/efi_driver/efi_block_device.c b/lib/efi_driver/efi_block_device.c index f440067f702..bd3688848ba 100644 --- a/lib/efi_driver/efi_block_device.c +++ b/lib/efi_driver/efi_block_device.c @@ -197,6 +197,17 @@ static efi_status_t efi_bl_bind( return ret; } +/** + * efi_bl_init() - initialize block device driver + * + * @this: extended driver binding protocol + */ +static efi_status_t +efi_bl_init(struct efi_driver_binding_extended_protocol *this) +{ + return EFI_SUCCESS; +} + /* Block device driver operators */ static const struct blk_ops efi_blk_ops = { .read = efi_bl_read, @@ -215,6 +226,7 @@ U_BOOT_DRIVER(efi_blk) = { static const struct efi_driver_ops driver_ops = { .protocol = &efi_block_io_guid, .child_protocol = &efi_block_io_guid, + .init = efi_bl_init, .bind = efi_bl_bind, }; -- cgit v1.2.3 From f05911a126bd6b8536c8d43fd6c1d837008fcda1 Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Thu, 6 Oct 2022 07:29:41 +0200 Subject: efi_driver: move event registration to driver Move the registration of events for the addition and removal of block devices to the block device driver. Here we can add a reference to the EFI Driver Binding protocol as context. Signed-off-by: Heinrich Schuchardt --- lib/efi_driver/efi_block_device.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/efi_driver/efi_block_device.c') diff --git a/lib/efi_driver/efi_block_device.c b/lib/efi_driver/efi_block_device.c index bd3688848ba..add00eeebbe 100644 --- a/lib/efi_driver/efi_block_device.c +++ b/lib/efi_driver/efi_block_device.c @@ -205,6 +205,22 @@ static efi_status_t efi_bl_bind( static efi_status_t efi_bl_init(struct efi_driver_binding_extended_protocol *this) { + int ret; + + ret = event_register("efi_disk add", EVT_DM_POST_PROBE, + efi_disk_probe, this); + if (ret) { + log_err("Event registration for efi_disk add failed\n"); + return EFI_OUT_OF_RESOURCES; + } + + ret = event_register("efi_disk del", EVT_DM_PRE_REMOVE, + efi_disk_remove, this); + if (ret) { + log_err("Event registration for efi_disk del failed\n"); + return EFI_OUT_OF_RESOURCES; + } + return EFI_SUCCESS; } -- cgit v1.2.3