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 FILE_SECBOOT ( PERMITTED );
26 
27 /** @file
28  *
29  * EFI driver connection and disconnection
30  *
31  */
32 
33 #include <errno.h>
34 #include <string.h>
35 #include <ipxe/efi/efi.h>
36 
37 /* Disambiguate the various error causes */
38 #define EINFO_EEFI_CONNECT \
39  __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
40  "Could not connect controllers" )
41 #define EINFO_EEFI_CONNECT_PROHIBITED \
42  __einfo_platformify ( EINFO_EEFI_CONNECT, \
43  EFI_SECURITY_VIOLATION, \
44  "Connecting controllers prohibited by " \
45  "security policy" )
46 #define EEFI_CONNECT_PROHIBITED \
47  __einfo_error ( EINFO_EEFI_CONNECT_PROHIBITED )
48 #define EEFI_CONNECT( efirc ) EPLATFORM ( EINFO_EEFI_CONNECT, efirc, \
49  EEFI_CONNECT_PROHIBITED )
50 
51 /**
52  * Connect UEFI driver(s)
53  *
54  * @v device EFI device handle
55  * @v driver EFI driver handle, or NULL
56  * @ret rc Return status code
57  */
60  EFI_HANDLE driverlist[2] = { driver, NULL };
61  EFI_HANDLE *drivers = ( driver ? driverlist : NULL );
62  EFI_STATUS efirc;
63  int rc;
64 
65  /* Attempt connection at external TPL */
66  DBGC ( device, "EFI %s connecting ", efi_handle_name ( device ) );
67  DBGC ( device, "%s driver at %s TPL\n",
68  ( driver ? efi_handle_name ( driver ) : "any" ),
71  efirc = bs->ConnectController ( device, drivers, NULL, TRUE );
72  bs->RaiseTPL ( efi_internal_tpl );
73  if ( efirc != 0 ) {
74  rc = -EEFI_CONNECT ( efirc );
75  DBGC ( device, "EFI %s could not connect: %s\n",
76  efi_handle_name ( device ), strerror ( rc ) );
77  return rc;
78  }
79 
80  return 0;
81 }
82 
83 /**
84  * Disconnect UEFI driver(s)
85  *
86  * @v device EFI device handle
87  * @v driver EFI driver handle, or NULL
88  * @ret rc Return status code
89  */
92  EFI_STATUS efirc;
93  int rc;
94 
95  /* Attempt disconnection at external TPL */
96  DBGC ( device, "EFI %s disconnecting ", efi_handle_name ( device ) );
97  DBGC ( device, "%s driver at %s TPL\n",
98  ( driver ? efi_handle_name ( driver ) : "any" ),
101  efirc = bs->DisconnectController ( device, driver, NULL );
102  bs->RaiseTPL ( efi_internal_tpl );
103  if ( ( efirc != 0 ) && ( efirc != EFI_NOT_FOUND ) ) {
104  rc = -EEFI ( efirc );
105  DBGC ( device, "EFI %s could not disconnect: %s\n",
106  efi_handle_name ( device ), strerror ( rc ) );
107  return rc;
108  }
109 
110  return 0;
111 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2099
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:175
EFI_RAISE_TPL RaiseTPL
Definition: UefiSpec.h:1940
int efi_connect(EFI_HANDLE device, EFI_HANDLE driver)
Connect UEFI driver(s)
Definition: efi_connect.c:58
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:54
A hardware device.
Definition: device.h:77
#define EEFI_CONNECT(efirc)
Definition: efi_connect.c:48
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:79
EFI Boot Services Table.
Definition: UefiSpec.h:1931
EFI_CONNECT_CONTROLLER ConnectController
Definition: UefiSpec.h:1994
#define TRUE
Definition: tlan.h:46
EFI API.
#define EFI_NOT_FOUND
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:129
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:32
FILE_SECBOOT(PERMITTED)
EFI_SYSTEM_TABLE * efi_systab
EFI_RESTORE_TPL RestoreTPL
Definition: UefiSpec.h:1941
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
Definition: efi.h:62
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:90
EFI_TPL efi_external_tpl
External task priority level.
Definition: efi_init.c:57
EFI_DISCONNECT_CONTROLLER DisconnectController
Definition: UefiSpec.h:1995