iPXE
efi_connect.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2025 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * EFI driver connection and disconnection
29  *
30  */
31 
32 #include <errno.h>
33 #include <string.h>
34 #include <ipxe/efi/efi.h>
35 
36 /* Disambiguate the various error causes */
37 #define EINFO_EEFI_CONNECT \
38  __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
39  "Could not connect controllers" )
40 #define EINFO_EEFI_CONNECT_PROHIBITED \
41  __einfo_platformify ( EINFO_EEFI_CONNECT, \
42  EFI_SECURITY_VIOLATION, \
43  "Connecting controllers prohibited by " \
44  "security policy" )
45 #define EEFI_CONNECT_PROHIBITED \
46  __einfo_error ( EINFO_EEFI_CONNECT_PROHIBITED )
47 #define EEFI_CONNECT( efirc ) EPLATFORM ( EINFO_EEFI_CONNECT, efirc, \
48  EEFI_CONNECT_PROHIBITED )
49 
50 /**
51  * Connect UEFI driver(s)
52  *
53  * @v device EFI device handle
54  * @v driver EFI driver handle, or NULL
55  * @ret rc Return status code
56  */
59  EFI_HANDLE driverlist[2] = { driver, NULL };
60  EFI_HANDLE *drivers = ( driver ? driverlist : NULL );
61  EFI_STATUS efirc;
62  int rc;
63 
64  /* Attempt connection at external TPL */
65  DBGC ( device, "EFI %s connecting ", efi_handle_name ( device ) );
66  DBGC ( device, "%s driver at %s TPL\n",
67  ( driver ? efi_handle_name ( driver ) : "any" ),
70  efirc = bs->ConnectController ( device, drivers, NULL, TRUE );
71  bs->RaiseTPL ( efi_internal_tpl );
72  if ( efirc != 0 ) {
73  rc = -EEFI_CONNECT ( efirc );
74  DBGC ( device, "EFI %s could not connect: %s\n",
75  efi_handle_name ( device ), strerror ( rc ) );
76  return rc;
77  }
78 
79  return 0;
80 }
81 
82 /**
83  * Disconnect UEFI driver(s)
84  *
85  * @v device EFI device handle
86  * @v driver EFI driver handle, or NULL
87  * @ret rc Return status code
88  */
91  EFI_STATUS efirc;
92  int rc;
93 
94  /* Attempt disconnection at external TPL */
95  DBGC ( device, "EFI %s disconnecting ", efi_handle_name ( device ) );
96  DBGC ( device, "%s driver at %s TPL\n",
97  ( driver ? efi_handle_name ( driver ) : "any" ),
100  efirc = bs->DisconnectController ( device, driver, NULL );
101  bs->RaiseTPL ( efi_internal_tpl );
102  if ( ( efirc != 0 ) && ( efirc != EFI_NOT_FOUND ) ) {
103  rc = -EEFI ( efirc );
104  DBGC ( device, "EFI %s could not disconnect: %s\n",
105  efi_handle_name ( device ), strerror ( rc ) );
106  return rc;
107  }
108 
109  return 0;
110 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2098
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:174
EFI_RAISE_TPL RaiseTPL
Definition: UefiSpec.h:1939
int efi_connect(EFI_HANDLE device, EFI_HANDLE driver)
Connect UEFI driver(s)
Definition: efi_connect.c:57
Error codes.
#define DBGC(...)
Definition: compiler.h:505
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
EFI_TPL efi_internal_tpl
Internal task priority level.
Definition: efi_init.c:53
A hardware device.
Definition: device.h:76
#define EEFI_CONNECT(efirc)
Definition: efi_connect.c:47
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:652
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1930
EFI_CONNECT_CONTROLLER ConnectController
Definition: UefiSpec.h:1993
#define TRUE
Definition: tlan.h:46
EFI API.
#define EFI_NOT_FOUND
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:128
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_SYSTEM_TABLE * efi_systab
EFI_RESTORE_TPL RestoreTPL
Definition: UefiSpec.h:1940
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
Definition: efi.h:61
const char * efi_tpl_name(EFI_TPL tpl)
Name EFI TPL.
Definition: efi_debug.c:55
int efi_disconnect(EFI_HANDLE device, EFI_HANDLE driver)
Disconnect UEFI driver(s)
Definition: efi_connect.c:89
EFI_TPL efi_external_tpl
External task priority level.
Definition: efi_init.c:56
EFI_DISCONNECT_CONTROLLER DisconnectController
Definition: UefiSpec.h:1994