]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/BootManagerPolicyDxe/BootManagerPolicyDxe.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Universal / BootManagerPolicyDxe / BootManagerPolicyDxe.c
1 /** @file
2 This module produces Boot Manager Policy protocol.
3
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Uefi.h>
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>
20
21 CHAR16 mNetworkDeviceList[] = L"_NDL";
22
23 /**
24 Connect all the system drivers to controllers and create the network device list in NV storage.
25
26 @retval EFI_SUCCESS Network devices are connected.
27 @retval EFI_DEVICE_ERROR No network device is connected.
28
29 **/
30 EFI_STATUS
31 ConnectAllAndCreateNetworkDeviceList (
32 VOID
33 )
34 {
35 EFI_STATUS Status;
36 EFI_HANDLE *Handles;
37 UINTN HandleCount;
38 EFI_DEVICE_PATH_PROTOCOL *SingleDevice;
39 EFI_DEVICE_PATH_PROTOCOL *Devices;
40 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
41
42 EfiBootManagerConnectAll ();
43
44 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiManagedNetworkServiceBindingProtocolGuid, NULL, &HandleCount, &Handles);
45 if (EFI_ERROR (Status)) {
46 Handles = NULL;
47 HandleCount = 0;
48 }
49
50 Devices = NULL;
51 while (HandleCount-- != 0) {
52 Status = gBS->HandleProtocol (Handles[HandleCount], &gEfiDevicePathProtocolGuid, (VOID **)&SingleDevice);
53 if (EFI_ERROR (Status) || (SingleDevice == NULL)) {
54 continue;
55 }
56
57 TempDevicePath = Devices;
58 Devices = AppendDevicePathInstance (Devices, SingleDevice);
59 if (TempDevicePath != NULL) {
60 FreePool (TempDevicePath);
61 }
62 }
63
64 if (Devices != NULL) {
65 Status = gRT->SetVariable (
66 mNetworkDeviceList,
67 &gEfiCallerIdGuid,
68 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
69 GetDevicePathSize (Devices),
70 Devices
71 );
72 //
73 // Fails to save the network device list to NV storage is not a fatal error.
74 // Only impact is performance.
75 //
76 FreePool (Devices);
77 }
78
79 return (Devices == NULL) ? EFI_DEVICE_ERROR : EFI_SUCCESS;
80 }
81
82 /**
83 Connect the network devices.
84
85 @retval EFI_SUCCESS At least one network device was connected.
86 @retval EFI_DEVICE_ERROR Network devices were not connected due to an error.
87 **/
88 EFI_STATUS
89 ConnectNetwork (
90 VOID
91 )
92 {
93 EFI_STATUS Status;
94 BOOLEAN OneConnected;
95 EFI_DEVICE_PATH_PROTOCOL *Devices;
96 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
97 EFI_DEVICE_PATH_PROTOCOL *SingleDevice;
98 UINTN Size;
99
100 OneConnected = FALSE;
101 GetVariable2 (mNetworkDeviceList, &gEfiCallerIdGuid, (VOID **)&Devices, NULL);
102 TempDevicePath = Devices;
103 while (TempDevicePath != NULL) {
104 SingleDevice = GetNextDevicePathInstance (&TempDevicePath, &Size);
105 Status = EfiBootManagerConnectDevicePath (SingleDevice, NULL);
106 if (!EFI_ERROR (Status)) {
107 OneConnected = TRUE;
108 }
109
110 FreePool (SingleDevice);
111 }
112
113 if (Devices != NULL) {
114 FreePool (Devices);
115 }
116
117 if (OneConnected) {
118 return EFI_SUCCESS;
119 } else {
120 //
121 // Cached network devices list doesn't exist or is NOT valid.
122 //
123 return ConnectAllAndCreateNetworkDeviceList ();
124 }
125 }
126
127 /**
128 Connect a device path following the platforms EFI Boot Manager policy.
129
130 The ConnectDevicePath() function allows the caller to connect a DevicePath using the
131 same policy as the EFI Boot Manger.
132
133 @param[in] This A pointer to the EFI_BOOT_MANAGER_POLICY_PROTOCOL instance.
134 @param[in] DevicePath Points to the start of the EFI device path to connect.
135 If DevicePath is NULL then all the controllers in the
136 system will be connected using the platforms EFI Boot
137 Manager policy.
138 @param[in] Recursive If TRUE, then ConnectController() is called recursively
139 until the entire tree of controllers below the
140 controller specified by DevicePath have been created.
141 If FALSE, then the tree of controllers is only expanded
142 one level. If DevicePath is NULL then Recursive is ignored.
143
144 @retval EFI_SUCCESS The DevicePath was connected.
145 @retval EFI_NOT_FOUND The DevicePath was not found.
146 @retval EFI_NOT_FOUND No driver was connected to DevicePath.
147 @retval EFI_SECURITY_VIOLATION The user has no permission to start UEFI device
148 drivers on the DevicePath.
149 @retval EFI_UNSUPPORTED The current TPL is not TPL_APPLICATION.
150 **/
151 EFI_STATUS
152 EFIAPI
153 BootManagerPolicyConnectDevicePath (
154 IN EFI_BOOT_MANAGER_POLICY_PROTOCOL *This,
155 IN EFI_DEVICE_PATH *DevicePath,
156 IN BOOLEAN Recursive
157 )
158 {
159 EFI_STATUS Status;
160 EFI_HANDLE Controller;
161
162 if (EfiGetCurrentTpl () != TPL_APPLICATION) {
163 return EFI_UNSUPPORTED;
164 }
165
166 if (DevicePath == NULL) {
167 EfiBootManagerConnectAll ();
168 return EFI_SUCCESS;
169 }
170
171 if (Recursive) {
172 Status = EfiBootManagerConnectDevicePath (DevicePath, NULL);
173 } else {
174 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &DevicePath, &Controller);
175 if (!EFI_ERROR (Status)) {
176 Status = gBS->ConnectController (Controller, NULL, DevicePath, FALSE);
177 }
178 }
179
180 return Status;
181 }
182
183 /**
184 Connect a class of devices using the platform Boot Manager policy.
185
186 The ConnectDeviceClass() function allows the caller to request that the Boot
187 Manager connect a class of devices.
188
189 If Class is EFI_BOOT_MANAGER_POLICY_CONSOLE_GUID then the Boot Manager will
190 use platform policy to connect consoles. Some platforms may restrict the
191 number of consoles connected as they attempt to fast boot, and calling
192 ConnectDeviceClass() with a Class value of EFI_BOOT_MANAGER_POLICY_CONSOLE_GUID
193 must connect the set of consoles that follow the Boot Manager platform policy,
194 and the EFI_SIMPLE_TEXT_INPUT_PROTOCOL, EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL, and
195 the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL are produced on the connected handles.
196 The Boot Manager may restrict which consoles get connect due to platform policy,
197 for example a security policy may require that a given console is not connected.
198
199 If Class is EFI_BOOT_MANAGER_POLICY_NETWORK_GUID then the Boot Manager will
200 connect the protocols the platforms supports for UEFI general purpose network
201 applications on one or more handles. If more than one network controller is
202 available a platform will connect, one, many, or all of the networks based
203 on platform policy. Connecting UEFI networking protocols, like EFI_DHCP4_PROTOCOL,
204 does not establish connections on the network. The UEFI general purpose network
205 application that called ConnectDeviceClass() may need to use the published
206 protocols to establish the network connection. The Boot Manager can optionally
207 have a policy to establish a network connection.
208
209 If Class is EFI_BOOT_MANAGER_POLICY_CONNECT_ALL_GUID then the Boot Manager
210 will connect all UEFI drivers using the UEFI Boot Service
211 EFI_BOOT_SERVICES.ConnectController(). If the Boot Manager has policy
212 associated with connect all UEFI drivers this policy will be used.
213
214 A platform can also define platform specific Class values as a properly generated
215 EFI_GUID would never conflict with this specification.
216
217 @param[in] This A pointer to the EFI_BOOT_MANAGER_POLICY_PROTOCOL instance.
218 @param[in] Class A pointer to an EFI_GUID that represents a class of devices
219 that will be connected using the Boot Mangers platform policy.
220
221 @retval EFI_SUCCESS At least one devices of the Class was connected.
222 @retval EFI_DEVICE_ERROR Devices were not connected due to an error.
223 @retval EFI_NOT_FOUND The Class is not supported by the platform.
224 @retval EFI_UNSUPPORTED The current TPL is not TPL_APPLICATION.
225 **/
226 EFI_STATUS
227 EFIAPI
228 BootManagerPolicyConnectDeviceClass (
229 IN EFI_BOOT_MANAGER_POLICY_PROTOCOL *This,
230 IN EFI_GUID *Class
231 )
232 {
233 if (EfiGetCurrentTpl () != TPL_APPLICATION) {
234 return EFI_UNSUPPORTED;
235 }
236
237 if (CompareGuid (Class, &gEfiBootManagerPolicyConnectAllGuid)) {
238 ConnectAllAndCreateNetworkDeviceList ();
239 return EFI_SUCCESS;
240 }
241
242 if (CompareGuid (Class, &gEfiBootManagerPolicyConsoleGuid)) {
243 return EfiBootManagerConnectAllDefaultConsoles ();
244 }
245
246 if (CompareGuid (Class, &gEfiBootManagerPolicyNetworkGuid)) {
247 return ConnectNetwork ();
248 }
249
250 return EFI_NOT_FOUND;
251 }
252
253 EFI_BOOT_MANAGER_POLICY_PROTOCOL mBootManagerPolicy = {
254 EFI_BOOT_MANAGER_POLICY_PROTOCOL_REVISION,
255 BootManagerPolicyConnectDevicePath,
256 BootManagerPolicyConnectDeviceClass
257 };
258
259 /**
260 Install Boot Manager Policy Protocol.
261
262 @param ImageHandle The image handle.
263 @param SystemTable The system table.
264
265 @retval EFI_SUCEESS The Boot Manager Policy protocol is successfully installed.
266 @retval Other Return status from gBS->InstallMultipleProtocolInterfaces().
267
268 **/
269 EFI_STATUS
270 EFIAPI
271 BootManagerPolicyInitialize (
272 IN EFI_HANDLE ImageHandle,
273 IN EFI_SYSTEM_TABLE *SystemTable
274 )
275 {
276 EFI_HANDLE Handle;
277
278 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiBootManagerPolicyProtocolGuid);
279
280 Handle = NULL;
281 return gBS->InstallMultipleProtocolInterfaces (
282 &Handle,
283 &gEfiBootManagerPolicyProtocolGuid,
284 &mBootManagerPolicy,
285 NULL
286 );
287 }