2 This module produces Boot Manager Policy protocol.
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Protocol/BootManagerPolicy.h>
11 #include <Protocol/ManagedNetwork.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/UefiLib.h>
15 #include <Library/DevicePathLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/UefiRuntimeServicesTableLib.h>
19 #include <Library/UefiBootManagerLib.h>
21 CHAR16 mNetworkDeviceList
[] = L
"_NDL";
24 Connect all the system drivers to controllers and create the network device list in NV storage.
26 @retval EFI_SUCCESS Network devices are connected.
27 @retval EFI_DEVICE_ERROR No network device is connected.
31 ConnectAllAndCreateNetworkDeviceList (
38 EFI_DEVICE_PATH_PROTOCOL
*SingleDevice
;
39 EFI_DEVICE_PATH_PROTOCOL
*Devices
;
40 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
42 EfiBootManagerConnectAll ();
44 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiManagedNetworkServiceBindingProtocolGuid
, NULL
, &HandleCount
, &Handles
);
45 if (EFI_ERROR (Status
)) {
51 while (HandleCount
-- != 0) {
52 Status
= gBS
->HandleProtocol (Handles
[HandleCount
], &gEfiDevicePathProtocolGuid
, (VOID
**) &SingleDevice
);
53 if (EFI_ERROR (Status
) || (SingleDevice
== NULL
)) {
56 TempDevicePath
= Devices
;
57 Devices
= AppendDevicePathInstance (Devices
, SingleDevice
);
58 if (TempDevicePath
!= NULL
) {
59 FreePool (TempDevicePath
);
63 if (Devices
!= NULL
) {
64 Status
= gRT
->SetVariable (
67 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
68 GetDevicePathSize (Devices
),
72 // Fails to save the network device list to NV storage is not a fatal error.
73 // Only impact is performance.
78 return (Devices
== NULL
) ? EFI_DEVICE_ERROR
: EFI_SUCCESS
;
82 Connect the network devices.
84 @retval EFI_SUCCESS At least one network device was connected.
85 @retval EFI_DEVICE_ERROR Network devices were not connected due to an error.
94 EFI_DEVICE_PATH_PROTOCOL
*Devices
;
95 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
96 EFI_DEVICE_PATH_PROTOCOL
*SingleDevice
;
100 GetVariable2 (mNetworkDeviceList
, &gEfiCallerIdGuid
, (VOID
**) &Devices
, NULL
);
101 TempDevicePath
= Devices
;
102 while (TempDevicePath
!= NULL
) {
103 SingleDevice
= GetNextDevicePathInstance (&TempDevicePath
, &Size
);
104 Status
= EfiBootManagerConnectDevicePath (SingleDevice
, NULL
);
105 if (!EFI_ERROR (Status
)) {
108 FreePool (SingleDevice
);
110 if (Devices
!= NULL
) {
118 // Cached network devices list doesn't exist or is NOT valid.
120 return ConnectAllAndCreateNetworkDeviceList ();
125 Connect a device path following the platforms EFI Boot Manager policy.
127 The ConnectDevicePath() function allows the caller to connect a DevicePath using the
128 same policy as the EFI Boot Manger.
130 @param[in] This A pointer to the EFI_BOOT_MANAGER_POLICY_PROTOCOL instance.
131 @param[in] DevicePath Points to the start of the EFI device path to connect.
132 If DevicePath is NULL then all the controllers in the
133 system will be connected using the platforms EFI Boot
135 @param[in] Recursive If TRUE, then ConnectController() is called recursively
136 until the entire tree of controllers below the
137 controller specified by DevicePath have been created.
138 If FALSE, then the tree of controllers is only expanded
139 one level. If DevicePath is NULL then Recursive is ignored.
141 @retval EFI_SUCCESS The DevicePath was connected.
142 @retval EFI_NOT_FOUND The DevicePath was not found.
143 @retval EFI_NOT_FOUND No driver was connected to DevicePath.
144 @retval EFI_SECURITY_VIOLATION The user has no permission to start UEFI device
145 drivers on the DevicePath.
146 @retval EFI_UNSUPPORTED The current TPL is not TPL_APPLICATION.
150 BootManagerPolicyConnectDevicePath (
151 IN EFI_BOOT_MANAGER_POLICY_PROTOCOL
*This
,
152 IN EFI_DEVICE_PATH
*DevicePath
,
157 EFI_HANDLE Controller
;
159 if (EfiGetCurrentTpl () != TPL_APPLICATION
) {
160 return EFI_UNSUPPORTED
;
163 if (DevicePath
== NULL
) {
164 EfiBootManagerConnectAll ();
169 Status
= EfiBootManagerConnectDevicePath (DevicePath
, NULL
);
171 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &DevicePath
, &Controller
);
172 if (!EFI_ERROR (Status
)) {
173 Status
= gBS
->ConnectController (Controller
, NULL
, DevicePath
, FALSE
);
179 Connect a class of devices using the platform Boot Manager policy.
181 The ConnectDeviceClass() function allows the caller to request that the Boot
182 Manager connect a class of devices.
184 If Class is EFI_BOOT_MANAGER_POLICY_CONSOLE_GUID then the Boot Manager will
185 use platform policy to connect consoles. Some platforms may restrict the
186 number of consoles connected as they attempt to fast boot, and calling
187 ConnectDeviceClass() with a Class value of EFI_BOOT_MANAGER_POLICY_CONSOLE_GUID
188 must connect the set of consoles that follow the Boot Manager platform policy,
189 and the EFI_SIMPLE_TEXT_INPUT_PROTOCOL, EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL, and
190 the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL are produced on the connected handles.
191 The Boot Manager may restrict which consoles get connect due to platform policy,
192 for example a security policy may require that a given console is not connected.
194 If Class is EFI_BOOT_MANAGER_POLICY_NETWORK_GUID then the Boot Manager will
195 connect the protocols the platforms supports for UEFI general purpose network
196 applications on one or more handles. If more than one network controller is
197 available a platform will connect, one, many, or all of the networks based
198 on platform policy. Connecting UEFI networking protocols, like EFI_DHCP4_PROTOCOL,
199 does not establish connections on the network. The UEFI general purpose network
200 application that called ConnectDeviceClass() may need to use the published
201 protocols to establish the network connection. The Boot Manager can optionally
202 have a policy to establish a network connection.
204 If Class is EFI_BOOT_MANAGER_POLICY_CONNECT_ALL_GUID then the Boot Manager
205 will connect all UEFI drivers using the UEFI Boot Service
206 EFI_BOOT_SERVICES.ConnectController(). If the Boot Manager has policy
207 associated with connect all UEFI drivers this policy will be used.
209 A platform can also define platform specific Class values as a properly generated
210 EFI_GUID would never conflict with this specification.
212 @param[in] This A pointer to the EFI_BOOT_MANAGER_POLICY_PROTOCOL instance.
213 @param[in] Class A pointer to an EFI_GUID that represents a class of devices
214 that will be connected using the Boot Mangers platform policy.
216 @retval EFI_SUCCESS At least one devices of the Class was connected.
217 @retval EFI_DEVICE_ERROR Devices were not connected due to an error.
218 @retval EFI_NOT_FOUND The Class is not supported by the platform.
219 @retval EFI_UNSUPPORTED The current TPL is not TPL_APPLICATION.
223 BootManagerPolicyConnectDeviceClass (
224 IN EFI_BOOT_MANAGER_POLICY_PROTOCOL
*This
,
228 if (EfiGetCurrentTpl () != TPL_APPLICATION
) {
229 return EFI_UNSUPPORTED
;
232 if (CompareGuid (Class
, &gEfiBootManagerPolicyConnectAllGuid
)) {
233 ConnectAllAndCreateNetworkDeviceList ();
237 if (CompareGuid (Class
, &gEfiBootManagerPolicyConsoleGuid
)) {
238 return EfiBootManagerConnectAllDefaultConsoles ();
241 if (CompareGuid (Class
, &gEfiBootManagerPolicyNetworkGuid
)) {
242 return ConnectNetwork ();
245 return EFI_NOT_FOUND
;
248 EFI_BOOT_MANAGER_POLICY_PROTOCOL mBootManagerPolicy
= {
249 EFI_BOOT_MANAGER_POLICY_PROTOCOL_REVISION
,
250 BootManagerPolicyConnectDevicePath
,
251 BootManagerPolicyConnectDeviceClass
255 Install Boot Manager Policy Protocol.
257 @param ImageHandle The image handle.
258 @param SystemTable The system table.
260 @retval EFI_SUCEESS The Boot Manager Policy protocol is successfully installed.
261 @retval Other Return status from gBS->InstallMultipleProtocolInterfaces().
266 BootManagerPolicyInitialize (
267 IN EFI_HANDLE ImageHandle
,
268 IN EFI_SYSTEM_TABLE
*SystemTable
273 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiBootManagerPolicyProtocolGuid
);
276 return gBS
->InstallMultipleProtocolInterfaces (
278 &gEfiBootManagerPolicyProtocolGuid
, &mBootManagerPolicy
,