]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/BootManagerPolicyDxe/BootManagerPolicyDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 TempDevicePath = Devices;
57 Devices = AppendDevicePathInstance (Devices, SingleDevice);
58 if (TempDevicePath != NULL) {
59 FreePool (TempDevicePath);
60 }
61 }
62
63 if (Devices != NULL) {
64 Status = gRT->SetVariable (
65 mNetworkDeviceList,
66 &gEfiCallerIdGuid,
67 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
68 GetDevicePathSize (Devices),
69 Devices
70 );
71 //
72 // Fails to save the network device list to NV storage is not a fatal error.
73 // Only impact is performance.
74 //
75 FreePool (Devices);
76 }
77
78 return (Devices == NULL) ? EFI_DEVICE_ERROR : EFI_SUCCESS;
79 }
80
81 /**
82 Connect the network devices.
83
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.
86 **/
87 EFI_STATUS
88 ConnectNetwork (
89 VOID
90 )
91 {
92 EFI_STATUS Status;
93 BOOLEAN OneConnected;
94 EFI_DEVICE_PATH_PROTOCOL *Devices;
95 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
96 EFI_DEVICE_PATH_PROTOCOL *SingleDevice;
97 UINTN Size;
98
99 OneConnected = FALSE;
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)) {
106 OneConnected = TRUE;
107 }
108 FreePool (SingleDevice);
109 }
110 if (Devices != NULL) {
111 FreePool (Devices);
112 }
113
114 if (OneConnected) {
115 return EFI_SUCCESS;
116 } else {
117 //
118 // Cached network devices list doesn't exist or is NOT valid.
119 //
120 return ConnectAllAndCreateNetworkDeviceList ();
121 }
122 }
123
124 /**
125 Connect a device path following the platforms EFI Boot Manager policy.
126
127 The ConnectDevicePath() function allows the caller to connect a DevicePath using the
128 same policy as the EFI Boot Manger.
129
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
134 Manager policy.
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.
140
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.
147 **/
148 EFI_STATUS
149 EFIAPI
150 BootManagerPolicyConnectDevicePath (
151 IN EFI_BOOT_MANAGER_POLICY_PROTOCOL *This,
152 IN EFI_DEVICE_PATH *DevicePath,
153 IN BOOLEAN Recursive
154 )
155 {
156 EFI_STATUS Status;
157 EFI_HANDLE Controller;
158
159 if (EfiGetCurrentTpl () != TPL_APPLICATION) {
160 return EFI_UNSUPPORTED;
161 }
162
163 if (DevicePath == NULL) {
164 EfiBootManagerConnectAll ();
165 return EFI_SUCCESS;
166 }
167
168 if (Recursive) {
169 Status = EfiBootManagerConnectDevicePath (DevicePath, NULL);
170 } else {
171 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &DevicePath, &Controller);
172 if (!EFI_ERROR (Status)) {
173 Status = gBS->ConnectController (Controller, NULL, DevicePath, FALSE);
174 }
175 }
176 return Status;
177 }
178 /**
179 Connect a class of devices using the platform Boot Manager policy.
180
181 The ConnectDeviceClass() function allows the caller to request that the Boot
182 Manager connect a class of devices.
183
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.
193
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.
203
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.
208
209 A platform can also define platform specific Class values as a properly generated
210 EFI_GUID would never conflict with this specification.
211
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.
215
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.
220 **/
221 EFI_STATUS
222 EFIAPI
223 BootManagerPolicyConnectDeviceClass (
224 IN EFI_BOOT_MANAGER_POLICY_PROTOCOL *This,
225 IN EFI_GUID *Class
226 )
227 {
228 if (EfiGetCurrentTpl () != TPL_APPLICATION) {
229 return EFI_UNSUPPORTED;
230 }
231
232 if (CompareGuid (Class, &gEfiBootManagerPolicyConnectAllGuid)) {
233 ConnectAllAndCreateNetworkDeviceList ();
234 return EFI_SUCCESS;
235 }
236
237 if (CompareGuid (Class, &gEfiBootManagerPolicyConsoleGuid)) {
238 return EfiBootManagerConnectAllDefaultConsoles ();
239 }
240
241 if (CompareGuid (Class, &gEfiBootManagerPolicyNetworkGuid)) {
242 return ConnectNetwork ();
243 }
244
245 return EFI_NOT_FOUND;
246 }
247
248 EFI_BOOT_MANAGER_POLICY_PROTOCOL mBootManagerPolicy = {
249 EFI_BOOT_MANAGER_POLICY_PROTOCOL_REVISION,
250 BootManagerPolicyConnectDevicePath,
251 BootManagerPolicyConnectDeviceClass
252 };
253
254 /**
255 Install Boot Manager Policy Protocol.
256
257 @param ImageHandle The image handle.
258 @param SystemTable The system table.
259
260 @retval EFI_SUCEESS The Boot Manager Policy protocol is successfully installed.
261 @retval Other Return status from gBS->InstallMultipleProtocolInterfaces().
262
263 **/
264 EFI_STATUS
265 EFIAPI
266 BootManagerPolicyInitialize (
267 IN EFI_HANDLE ImageHandle,
268 IN EFI_SYSTEM_TABLE *SystemTable
269 )
270 {
271 EFI_HANDLE Handle;
272
273 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiBootManagerPolicyProtocolGuid);
274
275 Handle = NULL;
276 return gBS->InstallMultipleProtocolInterfaces (
277 &Handle,
278 &gEfiBootManagerPolicyProtocolGuid, &mBootManagerPolicy,
279 NULL
280 );
281 }