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