]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[efi] Use efi_open_by_driver() for all by-driver protocol opens
authorMichael Brown <mcb30@ipxe.org>
Sun, 23 Mar 2025 18:06:15 +0000 (18:06 +0000)
committerMichael Brown <mcb30@ipxe.org>
Mon, 24 Mar 2025 13:19:26 +0000 (13:19 +0000)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/drivers/net/efi/mnpnet.c
src/drivers/net/efi/nii.c
src/drivers/net/efi/snpnet.c
src/drivers/usb/usbio.c
src/interface/efi/efi_file.c
src/interface/efi/efi_pci.c
src/interface/efi/efi_snp.c

index eb4b129c7a1ec0ec5bed5b8a64c60d5a9728f392..ead8691ff6761f36462ce2a836166d4fbf69947e 100644 (file)
@@ -367,7 +367,6 @@ static struct net_device_operations mnpnet_operations = {
  * @ret rc             Return status code
  */
 int mnpnet_start ( struct efi_device *efidev ) {
-       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_HANDLE device = efidev->device;
        EFI_GUID *binding = &efi_managed_network_service_binding_protocol_guid;
        EFI_SIMPLE_NETWORK_MODE mode;
@@ -408,13 +407,9 @@ int mnpnet_start ( struct efi_device *efidev ) {
        }
 
        /* Open MNP protocol */
-       if ( ( efirc = bs->OpenProtocol ( efidev->child,
-                                         &efi_managed_network_protocol_guid,
-                                         &u.interface, efi_image_handle,
-                                         efidev->child,
-                                         ( EFI_OPEN_PROTOCOL_BY_DRIVER |
-                                           EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
-               rc = -EEFI ( efirc );
+       if ( ( rc = efi_open_by_driver ( efidev->child,
+                                        &efi_managed_network_protocol_guid,
+                                        &u.interface ) ) != 0 ) {
                DBGC ( mnp, "MNP %s could not open MNP protocol: %s\n",
                       efi_handle_name ( device ), strerror ( rc ) );
                goto err_open;
@@ -464,8 +459,8 @@ int mnpnet_start ( struct efi_device *efidev ) {
  err_ll_addr_len:
  err_hw_addr_len:
  err_mode:
-       bs->CloseProtocol ( efidev->child, &efi_managed_network_protocol_guid,
-                           efi_image_handle, efidev->child );
+       efi_close_by_driver ( efidev->child,
+                             &efi_managed_network_protocol_guid );
  err_open:
        efi_service_del ( device, binding, efidev->child );
  err_service:
@@ -482,7 +477,6 @@ int mnpnet_start ( struct efi_device *efidev ) {
  * @v efidev           EFI device
   */
 void mnpnet_stop ( struct efi_device *efidev ) {
-       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_GUID *binding = &efi_managed_network_service_binding_protocol_guid;
        struct net_device *netdev = efidev_get_drvdata ( efidev );
        struct mnp_nic *mnp = netdev->priv;
@@ -491,8 +485,8 @@ void mnpnet_stop ( struct efi_device *efidev ) {
        unregister_netdev ( netdev );
 
        /* Close MNP protocol */
-       bs->CloseProtocol ( efidev->child, &efi_managed_network_protocol_guid,
-                           efi_image_handle, efidev->child );
+       efi_close_by_driver ( efidev->child,
+                             &efi_managed_network_protocol_guid );
 
        /* Remove MNP child (unless whole system shutdown is in progress) */
        if ( ! efi_shutdown_in_progress )
index 16e9e10dfbb24a80fc4b086d3583aeb2832b1836..18b292e5659d45ceab359f1e729ca5b39cfb5afc 100644 (file)
@@ -1270,12 +1270,10 @@ static struct net_device_operations nii_operations = {
  * @ret rc             Return status code
  */
 int nii_start ( struct efi_device *efidev ) {
-       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_HANDLE device = efidev->device;
        struct net_device *netdev;
        struct nii_nic *nii;
        void *interface;
-       EFI_STATUS efirc;
        int rc;
 
        /* Allocate and initialise structure */
@@ -1300,11 +1298,8 @@ int nii_start ( struct efi_device *efidev ) {
        netdev->dev = &nii->dev;
 
        /* Open NII protocol */
-       if ( ( efirc = bs->OpenProtocol ( device, &efi_nii31_protocol_guid,
-                                         &interface, efi_image_handle, device,
-                                         ( EFI_OPEN_PROTOCOL_BY_DRIVER |
-                                           EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
-               rc = -EEFI ( efirc );
+       if ( ( rc = efi_open_by_driver ( device, &efi_nii31_protocol_guid,
+                                        &interface ) ) != 0 ) {
                DBGC ( nii, "NII %s cannot open NII protocol: %s\n",
                       nii->dev.name, strerror ( rc ) );
                DBGC_EFI_OPENERS ( device, device, &efi_nii31_protocol_guid );
@@ -1373,8 +1368,7 @@ int nii_start ( struct efi_device *efidev ) {
  err_pci_open:
  err_hw_undi:
  err_no_undi:
-       bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
-                           efi_image_handle, device );
+       efi_close_by_driver ( device, &efi_nii31_protocol_guid );
  err_open_protocol:
        list_del ( &nii->dev.siblings );
        netdev_nullify ( netdev );
@@ -1389,7 +1383,6 @@ int nii_start ( struct efi_device *efidev ) {
  * @v efidev           EFI device
  */
 void nii_stop ( struct efi_device *efidev ) {
-       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        struct net_device *netdev = efidev_get_drvdata ( efidev );
        struct nii_nic *nii = netdev->priv;
        EFI_HANDLE device = efidev->device;
@@ -1404,8 +1397,7 @@ void nii_stop ( struct efi_device *efidev ) {
        nii_pci_close ( nii );
 
        /* Close NII protocol */
-       bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
-                           efi_image_handle, device );
+       efi_close_by_driver ( device, &efi_nii31_protocol_guid );
 
        /* Free network device */
        list_del ( &nii->dev.siblings );
index 6ce731d7891f88d263c95dc54d7f34677c2b2e0c..fb071d8d333ab0ecfde145649273cb4a56448e81 100644 (file)
@@ -539,7 +539,6 @@ int snpnet_supported ( EFI_HANDLE device, EFI_GUID *protocol ) {
  * @ret rc             Return status code
  */
 int snpnet_start ( struct efi_device *efidev ) {
-       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_HANDLE device = efidev->device;
        EFI_SIMPLE_NETWORK_MODE *mode;
        struct net_device *netdev;
@@ -549,12 +548,9 @@ int snpnet_start ( struct efi_device *efidev ) {
        int rc;
 
        /* Open SNP protocol */
-       if ( ( efirc = bs->OpenProtocol ( device,
-                                         &efi_simple_network_protocol_guid,
-                                         &interface, efi_image_handle, device,
-                                         ( EFI_OPEN_PROTOCOL_BY_DRIVER |
-                                           EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
-               rc = -EEFI ( efirc );
+       if ( ( rc = efi_open_by_driver ( device,
+                                        &efi_simple_network_protocol_guid,
+                                        &interface ) ) != 0 ) {
                DBGC ( device, "SNP %s cannot open SNP protocol: %s\n",
                       efi_handle_name ( device ), strerror ( rc ) );
                DBGC_EFI_OPENERS ( device, device,
@@ -644,8 +640,7 @@ int snpnet_start ( struct efi_device *efidev ) {
        netdev_nullify ( netdev );
        netdev_put ( netdev );
  err_alloc:
-       bs->CloseProtocol ( device, &efi_simple_network_protocol_guid,
-                           efi_image_handle, device );
+       efi_close_by_driver ( device, &efi_simple_network_protocol_guid );
  err_open_protocol:
        return rc;
 }
@@ -656,7 +651,6 @@ int snpnet_start ( struct efi_device *efidev ) {
  * @v efidev           EFI device
   */
 void snpnet_stop ( struct efi_device *efidev ) {
-       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        struct net_device *netdev = efidev_get_drvdata ( efidev );
        struct snp_nic *snp = netdev->priv;
        EFI_HANDLE device = efidev->device;
@@ -681,6 +675,5 @@ void snpnet_stop ( struct efi_device *efidev ) {
        netdev_put ( netdev );
 
        /* Close SNP protocol */
-       bs->CloseProtocol ( device, &efi_simple_network_protocol_guid,
-                           efi_image_handle, device );
+       efi_close_by_driver ( device, &efi_simple_network_protocol_guid );
 }
index 278b43cd38903d3e8abb4c5f7dc7dffb6d5a3f42..c5f245fc30cfab44fe3b56ea7a68c34b916748f8 100644 (file)
@@ -229,13 +229,9 @@ static int usbio_open ( struct usbio_device *usbio, unsigned int interface ) {
        }
 
        /* Open USB I/O protocol on this handle */
-       if ( ( efirc = bs->OpenProtocol ( intf->handle,
-                                         &efi_usb_io_protocol_guid,
-                                         &u.interface, efi_image_handle,
-                                         intf->handle,
-                                         ( EFI_OPEN_PROTOCOL_BY_DRIVER |
-                                           EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
-               rc = -EEFI ( efirc );
+       if ( ( rc = efi_open_by_driver ( intf->handle,
+                                        &efi_usb_io_protocol_guid,
+                                        &u.interface ) ) != 0 ) {
                DBGC ( usbio, "USBIO %s cannot open ",
                       efi_handle_name ( handle ) );
                DBGC ( usbio, "%s: %s\n",
@@ -259,7 +255,6 @@ static int usbio_open ( struct usbio_device *usbio, unsigned int interface ) {
  * @v interface                Interface number
  */
 static void usbio_close ( struct usbio_device *usbio, unsigned int interface ) {
-       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        struct usbio_interface *intf = &usbio->interface[interface];
 
        /* Sanity checks */
@@ -274,8 +269,7 @@ static void usbio_close ( struct usbio_device *usbio, unsigned int interface ) {
                return;
 
        /* Close USB I/O protocol */
-       bs->CloseProtocol ( intf->handle, &efi_usb_io_protocol_guid,
-                           efi_image_handle, intf->handle );
+       efi_close_by_driver ( intf->handle, &efi_usb_io_protocol_guid );
 }
 
 /******************************************************************************
@@ -1608,7 +1602,6 @@ static int usbio_interfaces ( struct usbio_device *usbio ) {
  * @ret rc             Return status code
  */
 static int usbio_start ( struct efi_device *efidev ) {
-       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_HANDLE handle = efidev->device;
        struct usbio_device *usbio;
        struct usb_port *port;
@@ -1616,7 +1609,6 @@ static int usbio_start ( struct efi_device *efidev ) {
                void *interface;
                EFI_USB_IO_PROTOCOL *io;
        } u;
-       EFI_STATUS efirc;
        int rc;
 
        /* Allocate and initialise structure */
@@ -1630,12 +1622,8 @@ static int usbio_start ( struct efi_device *efidev ) {
        INIT_LIST_HEAD ( &usbio->endpoints );
 
        /* Open USB I/O protocol */
-       if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
-                                         &u.interface, efi_image_handle,
-                                         handle,
-                                         ( EFI_OPEN_PROTOCOL_BY_DRIVER |
-                                           EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
-               rc = -EEFI ( efirc );
+       if ( ( rc = efi_open_by_driver ( handle, &efi_usb_io_protocol_guid,
+                                        &u.interface ) ) != 0 ) {
                DBGC ( usbio, "USBIO %s cannot open USB I/O protocol: %s\n",
                       efi_handle_name ( handle ), strerror ( rc ) );
                DBGC_EFI_OPENERS ( usbio, handle, &efi_usb_io_protocol_guid );
@@ -1692,8 +1680,7 @@ static int usbio_start ( struct efi_device *efidev ) {
        free ( usbio->config );
  err_config:
        list_del ( &usbio->dev.siblings );
-       bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
-                           efi_image_handle, handle );
+       efi_close_by_driver ( handle, &efi_usb_io_protocol_guid );
  err_open_usbio:
        free ( usbio );
  err_alloc:
@@ -1706,7 +1693,6 @@ static int usbio_start ( struct efi_device *efidev ) {
  * @v efidev           EFI device
  */
 static void usbio_stop ( struct efi_device *efidev ) {
-       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_HANDLE handle = efidev->device;
        struct usbio_device *usbio = efidev_get_drvdata ( efidev );
 
@@ -1716,8 +1702,7 @@ static void usbio_stop ( struct efi_device *efidev ) {
        free ( usbio->path );
        free ( usbio->config );
        list_del ( &usbio->dev.siblings );
-       bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
-                           efi_image_handle, handle );
+       efi_close_by_driver ( handle, &efi_usb_io_protocol_guid );
        free ( usbio );
 }
 
index 48fccdbe13aefa8729d01ff69024564dff0d9137..f31f2fe3bab7acd30fbb3d3254e6d0a39f005003 100644 (file)
@@ -1169,11 +1169,8 @@ int efi_file_install ( EFI_HANDLE handle ) {
         * of calls to our DRIVER_STOP method when starting the EFI
         * shell.  I have no idea why this is.
         */
-       if ( ( efirc = bs->OpenProtocol ( handle, &efi_disk_io_protocol_guid,
-                                         &diskio.interface, efi_image_handle,
-                                         handle,
-                                         EFI_OPEN_PROTOCOL_BY_DRIVER ) ) != 0){
-               rc = -EEFI ( efirc );
+       if ( ( rc = efi_open_by_driver ( handle, &efi_disk_io_protocol_guid,
+                                        &diskio.interface ) ) != 0 ) {
                DBGC ( handle, "Could not open disk I/O protocol: %s\n",
                       strerror ( rc ) );
                DBGC_EFI_OPENERS ( handle, handle, &efi_disk_io_protocol_guid );
@@ -1199,8 +1196,7 @@ int efi_file_install ( EFI_HANDLE handle ) {
        efi_file_path_uninstall ( &efi_file_initrd );
  err_initrd_install:
  err_initrd_claim:
-       bs->CloseProtocol ( handle, &efi_disk_io_protocol_guid,
-                           efi_image_handle, handle );
+       efi_close_by_driver ( handle, &efi_disk_io_protocol_guid );
  err_open:
        bs->UninstallMultipleProtocolInterfaces (
                        handle,
@@ -1228,8 +1224,7 @@ void efi_file_uninstall ( EFI_HANDLE handle ) {
        efi_file_path_uninstall ( &efi_file_initrd );
 
        /* Close our own disk I/O protocol */
-       bs->CloseProtocol ( handle, &efi_disk_io_protocol_guid,
-                           efi_image_handle, handle );
+       efi_close_by_driver ( handle, &efi_disk_io_protocol_guid );
 
        /* We must install the file system protocol first, since
         * otherwise the EDK2 code will attempt to helpfully uninstall
index d55cbe86ce0d00f80ce0271ef356e2f5664eeb9d..b53a88d668e6392b87d978937a929004f98c2165 100644 (file)
@@ -902,11 +902,9 @@ static int efipci_supported ( EFI_HANDLE device ) {
  * @ret rc             Return status code
  */
 static int efipci_start ( struct efi_device *efidev ) {
-       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_HANDLE device = efidev->device;
        struct efi_pci_device *efipci;
        void *pci_io;
-       EFI_STATUS efirc;
        int rc;
 
        /* Allocate PCI device */
@@ -920,12 +918,9 @@ static int efipci_start ( struct efi_device *efidev ) {
        if ( ( rc = efipci_info ( device, efipci ) ) != 0 )
                goto err_info;
 
-       /* Open PCI device */
-       if ( ( efirc = bs->OpenProtocol ( device, &efi_pci_io_protocol_guid,
-                                         &pci_io, efi_image_handle, device,
-                                         ( EFI_OPEN_PROTOCOL_BY_DRIVER |
-                                           EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
-               rc = -EEFI_PCI ( efirc );
+       /* Open PCI I/O protocol */
+       if ( ( rc = efi_open_by_driver ( device, &efi_pci_io_protocol_guid,
+                                        &pci_io ) ) != 0 ) {
                DBGC ( device, "EFIPCI %s could not open PCI device: %s\n",
                       efi_handle_name ( device ), strerror ( rc ) );
                DBGC_EFI_OPENERS ( device, device, &efi_pci_io_protocol_guid );
@@ -960,8 +955,7 @@ static int efipci_start ( struct efi_device *efidev ) {
  err_probe:
        list_del ( &efipci->pci.dev.siblings );
  err_find_driver:
-       bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
-                           efi_image_handle, device );
+       efi_close_by_driver ( device, &efi_pci_io_protocol_guid );
  err_open:
  err_info:
        free ( efipci );
@@ -976,15 +970,13 @@ static int efipci_start ( struct efi_device *efidev ) {
   */
 static void efipci_stop ( struct efi_device *efidev ) {
        struct efi_pci_device *efipci = efidev_get_drvdata ( efidev );
-       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_HANDLE device = efidev->device;
 
        pci_remove ( &efipci->pci );
        list_del ( &efipci->pci.dev.siblings );
        assert ( efipci->pci.dma.mapped == 0 );
        assert ( efipci->pci.dma.allocated == 0 );
-       bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
-                           efi_image_handle, device );
+       efi_close_by_driver ( device, &efi_pci_io_protocol_guid );
        free ( efipci );
 }
 
index d977802fd70ad3dd2bac57634b1b43ee5e672b0c..b9706d5ab4c453c2522f0d423187dc730e9df554 100644 (file)
@@ -1916,22 +1916,16 @@ static int efi_snp_probe ( struct net_device *netdev, void *priv __unused ) {
         * instances to prevent SnpDxe from attempting to bind to
         * them.
         */
-       if ( ( efirc = bs->OpenProtocol ( snpdev->handle,
-                                         &efi_nii_protocol_guid, &interface,
-                                         efi_image_handle, snpdev->handle,
-                                         ( EFI_OPEN_PROTOCOL_BY_DRIVER |
-                                           EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
-               rc = -EEFI ( efirc );
+       if ( ( rc = efi_open_by_driver ( snpdev->handle,
+                                        &efi_nii_protocol_guid,
+                                        &interface ) ) != 0 ) {
                DBGC ( snpdev, "SNPDEV %p could not open NII protocol: %s\n",
                       snpdev, strerror ( rc ) );
                goto err_open_nii;
        }
-       if ( ( efirc = bs->OpenProtocol ( snpdev->handle,
-                                         &efi_nii31_protocol_guid, &interface,
-                                         efi_image_handle, snpdev->handle,
-                                         ( EFI_OPEN_PROTOCOL_BY_DRIVER |
-                                           EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
-               rc = -EEFI ( efirc );
+       if ( ( rc = efi_open_by_driver ( snpdev->handle,
+                                        &efi_nii31_protocol_guid,
+                                        &interface ) ) != 0 ) {
                DBGC ( snpdev, "SNPDEV %p could not open NII31 protocol: %s\n",
                       snpdev, strerror ( rc ) );
                goto err_open_nii31;
@@ -1967,11 +1961,9 @@ static int efi_snp_probe ( struct net_device *netdev, void *priv __unused ) {
                leak |= efi_snp_hii_uninstall ( snpdev );
        efi_child_del ( efidev->device, snpdev->handle );
  err_efi_child_add:
-       bs->CloseProtocol ( snpdev->handle, &efi_nii31_protocol_guid,
-                           efi_image_handle, snpdev->handle );
+       efi_close_by_driver ( snpdev->handle, &efi_nii31_protocol_guid );
  err_open_nii31:
-       bs->CloseProtocol ( snpdev->handle, &efi_nii_protocol_guid,
-                           efi_image_handle, snpdev->handle );
+       efi_close_by_driver ( snpdev->handle, &efi_nii_protocol_guid );
  err_open_nii:
        if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
                        snpdev->handle,
@@ -2060,10 +2052,8 @@ static void efi_snp_remove ( struct net_device *netdev, void *priv __unused ) {
        if ( snpdev->package_list )
                leak |= efi_snp_hii_uninstall ( snpdev );
        efi_child_del ( snpdev->efidev->device, snpdev->handle );
-       bs->CloseProtocol ( snpdev->handle, &efi_nii_protocol_guid,
-                           efi_image_handle, snpdev->handle );
-       bs->CloseProtocol ( snpdev->handle, &efi_nii31_protocol_guid,
-                           efi_image_handle, snpdev->handle );
+       efi_close_by_driver ( snpdev->handle, &efi_nii_protocol_guid );
+       efi_close_by_driver ( snpdev->handle, &efi_nii31_protocol_guid );
        if ( ( ! efi_shutdown_in_progress ) &&
             ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
                        snpdev->handle,