[efi] Use efi_open() for all ephemeral protocol opens

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-03-23 19:11:13 +00:00
parent 5a5e2a1dae
commit bac3187439
17 changed files with 130 additions and 442 deletions
+2 -6
View File
@@ -493,9 +493,7 @@ static struct net_device_operations snpnet_operations = {
* @ret rc Return status code
*/
int snpnet_supported ( EFI_HANDLE device, EFI_GUID *protocol ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
EFI_HANDLE parent;
EFI_STATUS efirc;
int rc;
/* Check that this is not a device we are providing ourselves */
@@ -506,13 +504,11 @@ int snpnet_supported ( EFI_HANDLE device, EFI_GUID *protocol ) {
}
/* Test for presence of protocol */
if ( ( efirc = bs->OpenProtocol ( device, protocol,
NULL, efi_image_handle, device,
EFI_OPEN_PROTOCOL_TEST_PROTOCOL))!=0){
if ( ( rc = efi_open ( device, protocol, NULL ) ) != 0 ) {
DBGCP ( device, "HANDLE %s is not a %s device\n",
efi_handle_name ( device ),
efi_guid_ntoa ( protocol ) );
return -EEFI ( efirc );
return rc;
}
/* Check that there are no instances of this protocol further
+7 -26
View File
@@ -86,13 +86,11 @@ static struct chained_protocol chained_mnp = {
* @v chained Chainloaded protocol
*/
static void chained_locate ( struct chained_protocol *chained ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
EFI_HANDLE device = efi_loaded_image->DeviceHandle;
EFI_HANDLE handle;
void *match = NULL;
void *interface;
unsigned int skip;
EFI_STATUS efirc;
int rc;
/* Identify target device handle */
@@ -111,11 +109,8 @@ static void chained_locate ( struct chained_protocol *chained ) {
}
/* Get protocol instance */
if ( ( efirc = bs->OpenProtocol (
handle, chained->protocol, &interface,
efi_image_handle, handle,
EFI_OPEN_PROTOCOL_GET_PROTOCOL )) != 0){
rc = -EEFI ( efirc );
if ( ( rc = efi_open ( handle, chained->protocol,
&interface ) ) != 0 ) {
DBGC ( device, "CHAINED %s could not open %s on ",
efi_handle_name ( device ),
efi_guid_ntoa ( chained->protocol ) );
@@ -123,8 +118,6 @@ static void chained_locate ( struct chained_protocol *chained ) {
efi_handle_name ( handle ), strerror ( rc ) );
break;
}
bs->CloseProtocol ( handle, chained->protocol,
efi_image_handle, handle );
/* Stop if we reach a non-matching protocol instance */
if ( match && ( match != interface ) ) {
@@ -154,20 +147,16 @@ static void chained_locate ( struct chained_protocol *chained ) {
*/
static int chained_supported ( EFI_HANDLE device,
struct chained_protocol *chained ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
void *interface;
EFI_STATUS efirc;
int rc;
/* Get protocol */
if ( ( efirc = bs->OpenProtocol ( device, chained->protocol, &interface,
efi_image_handle, device,
EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
rc = -EEFI ( efirc );
if ( ( rc = efi_open ( device, chained->protocol,
&interface ) ) != 0 ) {
DBGCP ( device, "CHAINED %s is not a %s device\n",
efi_handle_name ( device ),
efi_guid_ntoa ( chained->protocol ) );
goto err_open_protocol;
return rc;
}
/* Ignore non-matching handles */
@@ -175,21 +164,13 @@ static int chained_supported ( EFI_HANDLE device,
DBGC2 ( device, "CHAINED %s is not the chainloaded %s\n",
efi_handle_name ( device ),
efi_guid_ntoa ( chained->protocol ) );
rc = -ENOTTY;
goto err_no_match;
return -ENOTTY;
}
/* Success */
rc = 0;
DBGC ( device, "CHAINED %s is the chainloaded %s\n",
efi_handle_name ( device ),
efi_guid_ntoa ( chained->protocol ) );
err_no_match:
bs->CloseProtocol ( device, chained->protocol, efi_image_handle,
device );
err_open_protocol:
return rc;
return 0;
}
/**
+15 -53
View File
@@ -1291,7 +1291,6 @@ static struct usb_host_operations usbio_operations = {
* @ret rc Return status code
*/
static int usbio_supported ( EFI_HANDLE handle ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
EFI_USB_DEVICE_DESCRIPTOR device;
EFI_USB_INTERFACE_DESCRIPTOR interface;
struct usb_function_descriptor desc;
@@ -1305,14 +1304,11 @@ static int usbio_supported ( EFI_HANDLE handle ) {
int rc;
/* Get protocol */
if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
&usb.interface, efi_image_handle,
handle,
EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
rc = -EEFI ( efirc );
if ( ( rc = efi_open ( handle, &efi_usb_io_protocol_guid,
&usb.interface ) ) != 0 ) {
DBGCP ( handle, "USB %s is not a USB device\n",
efi_handle_name ( handle ) );
goto err_open_protocol;
return rc;
}
/* Get device descriptor */
@@ -1321,7 +1317,7 @@ static int usbio_supported ( EFI_HANDLE handle ) {
rc = -EEFI ( efirc );
DBGC ( handle, "USB %s could not get device descriptor: "
"%s\n", efi_handle_name ( handle ), strerror ( rc ) );
goto err_get_device_descriptor;
return rc;
}
memset ( &desc, 0, sizeof ( desc ) );
desc.vendor = device.IdVendor;
@@ -1333,7 +1329,7 @@ static int usbio_supported ( EFI_HANDLE handle ) {
rc = -EEFI ( efirc );
DBGC ( handle, "USB %s could not get interface descriptor: "
"%s\n", efi_handle_name ( handle ), strerror ( rc ) );
goto err_get_interface_descriptor;
return rc;
}
desc.class.class.class = interface.InterfaceClass;
desc.class.class.subclass = interface.InterfaceSubClass;
@@ -1341,21 +1337,10 @@ static int usbio_supported ( EFI_HANDLE handle ) {
/* Look for a driver for this interface */
driver = usb_find_driver ( &desc, &id );
if ( ! driver ) {
rc = -ENOTSUP;
goto err_unsupported;
}
if ( ! driver )
return -ENOTSUP;
/* Success */
rc = 0;
err_unsupported:
err_get_interface_descriptor:
err_get_device_descriptor:
bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
efi_image_handle, handle );
err_open_protocol:
return rc;
return 0;
}
/**
@@ -1471,7 +1456,6 @@ static int usbio_config ( struct usbio_device *usbio ) {
* @ret rc Return status code
*/
static int usbio_path ( struct usbio_device *usbio ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
EFI_HANDLE handle = usbio->handle;
EFI_DEVICE_PATH_PROTOCOL *path;
EFI_DEVICE_PATH_PROTOCOL *end;
@@ -1481,19 +1465,14 @@ static int usbio_path ( struct usbio_device *usbio ) {
EFI_DEVICE_PATH_PROTOCOL *path;
} u;
size_t len;
EFI_STATUS efirc;
int rc;
/* Open device path protocol */
if ( ( efirc = bs->OpenProtocol ( handle,
&efi_device_path_protocol_guid,
&u.interface, efi_image_handle,
handle,
EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
rc = -EEFI ( efirc );
if ( ( rc = efi_open ( handle, &efi_device_path_protocol_guid,
&u.interface ) ) != 0 ) {
DBGC ( usbio, "USBIO %s cannot open device path protocol: "
"%s\n", efi_handle_name ( handle ), strerror ( rc ) );
goto err_open_protocol;
return rc;
}
path = u.interface;
@@ -1502,8 +1481,7 @@ static int usbio_path ( struct usbio_device *usbio ) {
if ( len < sizeof ( *usbpath ) ) {
DBGC ( usbio, "USBIO %s underlength device path\n",
efi_handle_name ( handle ) );
rc = -EINVAL;
goto err_underlength;
return -EINVAL;
}
usbpath = ( ( ( void * ) path ) + len - sizeof ( *usbpath ) );
if ( ! ( ( usbpath->Header.Type == MESSAGING_DEVICE_PATH ) &&
@@ -1511,34 +1489,18 @@ static int usbio_path ( struct usbio_device *usbio ) {
DBGC ( usbio, "USBIO %s not a USB device path: ",
efi_handle_name ( handle ) );
DBGC ( usbio, "%s\n", efi_devpath_text ( path ) );
rc = -EINVAL;
goto err_non_usb;
return -EINVAL;
}
/* Allocate copy of device path */
usbio->path = malloc ( len + sizeof ( *end ) );
if ( ! usbio->path ) {
rc = -ENOMEM;
goto err_alloc;
}
if ( ! usbio->path )
return -ENOMEM;
memcpy ( usbio->path, path, ( len + sizeof ( *end ) ) );
usbio->usbpath = ( ( ( void * ) usbio->path ) + len -
sizeof ( *usbpath ) );
/* Close protocol */
bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
efi_image_handle, handle );
return 0;
free ( usbio->path );
err_alloc:
err_non_usb:
err_underlength:
bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
efi_image_handle, handle );
err_open_protocol:
return rc;
}
/**