]> git.proxmox.com Git - mirror_edk2.git/blob - UefiPayloadPkg/Library/PlatformBootManagerLib/PlatformBootManager.c
UefiPayloadPkg: Fix ECC reported issues
[mirror_edk2.git] / UefiPayloadPkg / Library / PlatformBootManagerLib / PlatformBootManager.c
1 /** @file
2 This file include all platform action which can be customized
3 by IBV/OEM.
4
5 Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "PlatformBootManager.h"
11 #include "PlatformConsole.h"
12 #include <Protocol/PlatformBootManagerOverride.h>
13 #include <Guid/BootManagerMenu.h>
14 #include <Library/HobLib.h>
15
16 UNIVERSAL_PAYLOAD_PLATFORM_BOOT_MANAGER_OVERRIDE_PROTOCOL *mUniversalPayloadPlatformBootManagerOverrideInstance = NULL;
17
18 /**
19 Signal EndOfDxe event and install SMM Ready to lock protocol.
20
21 **/
22 VOID
23 InstallReadyToLock (
24 VOID
25 )
26 {
27 EFI_STATUS Status;
28 EFI_HANDLE Handle;
29 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;
30
31 DEBUG((DEBUG_INFO,"InstallReadyToLock entering......\n"));
32 //
33 // Inform the SMM infrastructure that we're entering BDS and may run 3rd party code hereafter
34 // Since PI1.2.1, we need signal EndOfDxe as ExitPmAuth
35 //
36 EfiEventGroupSignal (&gEfiEndOfDxeEventGroupGuid);
37 DEBUG((DEBUG_INFO,"All EndOfDxe callbacks have returned successfully\n"));
38
39 //
40 // Install DxeSmmReadyToLock protocol in order to lock SMM
41 //
42 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **) &SmmAccess);
43 if (!EFI_ERROR (Status)) {
44 Handle = NULL;
45 Status = gBS->InstallProtocolInterface (
46 &Handle,
47 &gEfiDxeSmmReadyToLockProtocolGuid,
48 EFI_NATIVE_INTERFACE,
49 NULL
50 );
51 ASSERT_EFI_ERROR (Status);
52 }
53
54 DEBUG((DEBUG_INFO,"InstallReadyToLock end\n"));
55 return;
56 }
57
58 /**
59 Return the index of the load option in the load option array.
60
61 The function consider two load options are equal when the
62 OptionType, Attributes, Description, FilePath and OptionalData are equal.
63
64 @param Key Pointer to the load option to be found.
65 @param Array Pointer to the array of load options to be found.
66 @param Count Number of entries in the Array.
67
68 @retval -1 Key wasn't found in the Array.
69 @retval 0 ~ Count-1 The index of the Key in the Array.
70 **/
71 INTN
72 PlatformFindLoadOption (
73 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Key,
74 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Array,
75 IN UINTN Count
76 )
77 {
78 UINTN Index;
79
80 for (Index = 0; Index < Count; Index++) {
81 if ((Key->OptionType == Array[Index].OptionType) &&
82 (Key->Attributes == Array[Index].Attributes) &&
83 (StrCmp (Key->Description, Array[Index].Description) == 0) &&
84 (CompareMem (Key->FilePath, Array[Index].FilePath, GetDevicePathSize (Key->FilePath)) == 0) &&
85 (Key->OptionalDataSize == Array[Index].OptionalDataSize) &&
86 (CompareMem (Key->OptionalData, Array[Index].OptionalData, Key->OptionalDataSize) == 0)) {
87 return (INTN) Index;
88 }
89 }
90
91 return -1;
92 }
93
94 /**
95 Register a boot option using a file GUID in the FV.
96
97 @param FileGuid The file GUID name in FV.
98 @param Description The boot option description.
99 @param Attributes The attributes used for the boot option loading.
100 **/
101 VOID
102 PlatformRegisterFvBootOption (
103 EFI_GUID *FileGuid,
104 CHAR16 *Description,
105 UINT32 Attributes
106 )
107 {
108 EFI_STATUS Status;
109 UINTN OptionIndex;
110 EFI_BOOT_MANAGER_LOAD_OPTION NewOption;
111 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
112 UINTN BootOptionCount;
113 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;
114 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
115 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
116
117 Status = gBS->HandleProtocol (gImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &LoadedImage);
118 ASSERT_EFI_ERROR (Status);
119
120 EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);
121 DevicePath = AppendDevicePathNode (
122 DevicePathFromHandle (LoadedImage->DeviceHandle),
123 (EFI_DEVICE_PATH_PROTOCOL *) &FileNode
124 );
125
126 Status = EfiBootManagerInitializeLoadOption (
127 &NewOption,
128 LoadOptionNumberUnassigned,
129 LoadOptionTypeBoot,
130 Attributes,
131 Description,
132 DevicePath,
133 NULL,
134 0
135 );
136 if (!EFI_ERROR (Status)) {
137 BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);
138
139 OptionIndex = PlatformFindLoadOption (&NewOption, BootOptions, BootOptionCount);
140
141 if (OptionIndex == -1) {
142 Status = EfiBootManagerAddLoadOptionVariable (&NewOption, (UINTN) -1);
143 ASSERT_EFI_ERROR (Status);
144 }
145 EfiBootManagerFreeLoadOption (&NewOption);
146 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);
147 }
148 }
149
150 /**
151 Do the platform specific action before the console is connected.
152
153 Such as:
154 Update console variable;
155 Register new Driver#### or Boot####;
156 Signal ReadyToLock event.
157 **/
158 VOID
159 EFIAPI
160 PlatformBootManagerBeforeConsole (
161 VOID
162 )
163 {
164 EFI_INPUT_KEY Enter;
165 EFI_INPUT_KEY F2;
166 EFI_INPUT_KEY Down;
167 EFI_BOOT_MANAGER_LOAD_OPTION BootOption;
168 EFI_STATUS Status;
169
170 Status = gBS->LocateProtocol (&gUniversalPayloadPlatformBootManagerOverrideProtocolGuid, NULL, (VOID **) &mUniversalPayloadPlatformBootManagerOverrideInstance);
171 if (EFI_ERROR (Status)) {
172 mUniversalPayloadPlatformBootManagerOverrideInstance = NULL;
173 }
174 if (mUniversalPayloadPlatformBootManagerOverrideInstance != NULL){
175 mUniversalPayloadPlatformBootManagerOverrideInstance->BeforeConsole();
176 return;
177 }
178
179 //
180 // Register ENTER as CONTINUE key
181 //
182 Enter.ScanCode = SCAN_NULL;
183 Enter.UnicodeChar = CHAR_CARRIAGE_RETURN;
184 EfiBootManagerRegisterContinueKeyOption (0, &Enter, NULL);
185
186 //
187 // Map F2 to Boot Manager Menu
188 //
189 F2.ScanCode = SCAN_F2;
190 F2.UnicodeChar = CHAR_NULL;
191 EfiBootManagerGetBootManagerMenu (&BootOption);
192 EfiBootManagerAddKeyOptionVariable (NULL, (UINT16) BootOption.OptionNumber, 0, &F2, NULL);
193
194 //
195 // Also add Down key to Boot Manager Menu since some serial terminals don't support F2 key.
196 //
197 Down.ScanCode = SCAN_DOWN;
198 Down.UnicodeChar = CHAR_NULL;
199 EfiBootManagerGetBootManagerMenu (&BootOption);
200 EfiBootManagerAddKeyOptionVariable (NULL, (UINT16) BootOption.OptionNumber, 0, &Down, NULL);
201
202 //
203 // Install ready to lock.
204 // This needs to be done before option rom dispatched.
205 //
206 InstallReadyToLock ();
207
208 //
209 // Dispatch deferred images after EndOfDxe event and ReadyToLock installation.
210 //
211 EfiBootManagerDispatchDeferredImages ();
212
213 PlatformConsoleInit ();
214 }
215
216 /**
217 Do the platform specific action after the console is connected.
218
219 Such as:
220 Dynamically switch output mode;
221 Signal console ready platform customized event;
222 Run diagnostics like memory testing;
223 Connect certain devices;
224 Dispatch additional option roms.
225 **/
226 VOID
227 EFIAPI
228 PlatformBootManagerAfterConsole (
229 VOID
230 )
231 {
232 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Black;
233 EFI_GRAPHICS_OUTPUT_BLT_PIXEL White;
234
235 if (mUniversalPayloadPlatformBootManagerOverrideInstance != NULL){
236 mUniversalPayloadPlatformBootManagerOverrideInstance->AfterConsole();
237 return;
238 }
239 Black.Blue = Black.Green = Black.Red = Black.Reserved = 0;
240 White.Blue = White.Green = White.Red = White.Reserved = 0xFF;
241
242 EfiBootManagerConnectAll ();
243 EfiBootManagerRefreshAllBootOption ();
244
245 //
246 // Register UEFI Shell
247 //
248 PlatformRegisterFvBootOption (PcdGetPtr (PcdShellFile), L"UEFI Shell", LOAD_OPTION_ACTIVE);
249
250 Print (
251 L"\n"
252 L"F2 or Down to enter Boot Manager Menu.\n"
253 L"ENTER to boot directly.\n"
254 L"\n"
255 );
256
257 }
258
259 /**
260 This function is called each second during the boot manager waits the timeout.
261
262 @param TimeoutRemain The remaining timeout.
263 **/
264 VOID
265 EFIAPI
266 PlatformBootManagerWaitCallback (
267 UINT16 TimeoutRemain
268 )
269 {
270 if (mUniversalPayloadPlatformBootManagerOverrideInstance != NULL){
271 mUniversalPayloadPlatformBootManagerOverrideInstance->WaitCallback (TimeoutRemain);
272 }
273 return;
274 }
275
276 /**
277 The function is called when no boot option could be launched,
278 including platform recovery options and options pointing to applications
279 built into firmware volumes.
280
281 If this function returns, BDS attempts to enter an infinite loop.
282 **/
283 VOID
284 EFIAPI
285 PlatformBootManagerUnableToBoot (
286 VOID
287 )
288 {
289 if (mUniversalPayloadPlatformBootManagerOverrideInstance != NULL){
290 mUniversalPayloadPlatformBootManagerOverrideInstance->UnableToBoot();
291 }
292 return;
293 }
294
295 /**
296 Get/update PcdBootManagerMenuFile from GUID HOB which will be assigned in bootloader.
297
298 @param ImageHandle The firmware allocated handle for the EFI image.
299 @param SystemTable A pointer to the EFI System Table.
300
301 @retval EFI_SUCCESS The entry point is executed successfully.
302 @retval other Some error occurs.
303
304 **/
305 EFI_STATUS
306 EFIAPI
307 PlatformBootManagerLibConstructor (
308 IN EFI_HANDLE ImageHandle,
309 IN EFI_SYSTEM_TABLE *SystemTable
310 )
311 {
312 EFI_STATUS Status;
313 UINTN Size;
314 VOID *GuidHob;
315 UNIVERSAL_PAYLOAD_GENERIC_HEADER *GenericHeader;
316 UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU *BootManagerMenuFile;
317
318 GuidHob = GetFirstGuidHob (&gEdkiiBootManagerMenuFileGuid);
319
320 if (GuidHob == NULL) {
321 //
322 // If the HOB is not create, the default value of PcdBootManagerMenuFile will be used.
323 //
324 return EFI_SUCCESS;
325 }
326
327 GenericHeader = (UNIVERSAL_PAYLOAD_GENERIC_HEADER *) GET_GUID_HOB_DATA (GuidHob);
328 if ((sizeof (UNIVERSAL_PAYLOAD_GENERIC_HEADER) > GET_GUID_HOB_DATA_SIZE (GuidHob)) || (GenericHeader->Length > GET_GUID_HOB_DATA_SIZE (GuidHob))) {
329 return EFI_NOT_FOUND;
330 }
331 if (GenericHeader->Revision == UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU_REVISION) {
332 BootManagerMenuFile = (UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU *) GET_GUID_HOB_DATA (GuidHob);
333 if (BootManagerMenuFile->Header.Length < UNIVERSAL_PAYLOAD_SIZEOF_THROUGH_FIELD (UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU, FileName)) {
334 return EFI_NOT_FOUND;
335 }
336 Size = sizeof (BootManagerMenuFile->FileName);
337 Status = PcdSetPtrS (PcdBootManagerMenuFile, &Size, &BootManagerMenuFile->FileName);
338 ASSERT_EFI_ERROR (Status);
339 } else {
340 return EFI_NOT_FOUND;
341 }
342
343 return EFI_SUCCESS;
344 }