2 This file include all platform action which can be customized
5 Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "PlatformBootManager.h"
11 #include "PlatformConsole.h"
12 #include <Protocol/PlatformBootManagerOverride.h>
13 #include <Guid/BootManagerMenu.h>
14 #include <Library/HobLib.h>
16 UNIVERSAL_PAYLOAD_PLATFORM_BOOT_MANAGER_OVERRIDE_PROTOCOL
*mUniversalPayloadPlatformBootManagerOverrideInstance
= NULL
;
19 Signal EndOfDxe event and install SMM Ready to lock protocol.
29 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
31 DEBUG ((DEBUG_INFO
, "InstallReadyToLock entering......\n"));
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
36 EfiEventGroupSignal (&gEfiEndOfDxeEventGroupGuid
);
37 DEBUG ((DEBUG_INFO
, "All EndOfDxe callbacks have returned successfully\n"));
40 // Install DxeSmmReadyToLock protocol in order to lock SMM
42 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
43 if (!EFI_ERROR (Status
)) {
45 Status
= gBS
->InstallProtocolInterface (
47 &gEfiDxeSmmReadyToLockProtocolGuid
,
51 ASSERT_EFI_ERROR (Status
);
54 DEBUG ((DEBUG_INFO
, "InstallReadyToLock end\n"));
59 Return the index of the load option in the load option array.
61 The function consider two load options are equal when the
62 OptionType, Attributes, Description, FilePath and OptionalData are equal.
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.
68 @retval -1 Key wasn't found in the Array.
69 @retval 0 ~ Count-1 The index of the Key in the Array.
72 PlatformFindLoadOption (
73 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION
*Key
,
74 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION
*Array
,
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))
96 Register a boot option using a file GUID in the FV.
98 @param FileGuid The file GUID name in FV.
99 @param Description The boot option description.
100 @param Attributes The attributes used for the boot option loading.
103 PlatformRegisterFvBootOption (
111 EFI_BOOT_MANAGER_LOAD_OPTION NewOption
;
112 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
113 UINTN BootOptionCount
;
114 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode
;
115 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
116 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
118 Status
= gBS
->HandleProtocol (gImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&LoadedImage
);
119 ASSERT_EFI_ERROR (Status
);
121 EfiInitializeFwVolDevicepathNode (&FileNode
, FileGuid
);
122 DevicePath
= AppendDevicePathNode (
123 DevicePathFromHandle (LoadedImage
->DeviceHandle
),
124 (EFI_DEVICE_PATH_PROTOCOL
*)&FileNode
127 Status
= EfiBootManagerInitializeLoadOption (
129 LoadOptionNumberUnassigned
,
137 if (!EFI_ERROR (Status
)) {
138 BootOptions
= EfiBootManagerGetLoadOptions (&BootOptionCount
, LoadOptionTypeBoot
);
140 OptionIndex
= PlatformFindLoadOption (&NewOption
, BootOptions
, BootOptionCount
);
142 if (OptionIndex
== -1) {
143 Status
= EfiBootManagerAddLoadOptionVariable (&NewOption
, (UINTN
)-1);
144 ASSERT_EFI_ERROR (Status
);
147 EfiBootManagerFreeLoadOption (&NewOption
);
148 EfiBootManagerFreeLoadOptions (BootOptions
, BootOptionCount
);
153 Do the platform specific action before the console is connected.
156 Update console variable;
157 Register new Driver#### or Boot####;
158 Signal ReadyToLock event.
162 PlatformBootManagerBeforeConsole (
167 EFI_INPUT_KEY CustomKey
;
169 EFI_BOOT_MANAGER_LOAD_OPTION BootOption
;
172 Status
= gBS
->LocateProtocol (&gUniversalPayloadPlatformBootManagerOverrideProtocolGuid
, NULL
, (VOID
**)&mUniversalPayloadPlatformBootManagerOverrideInstance
);
173 if (EFI_ERROR (Status
)) {
174 mUniversalPayloadPlatformBootManagerOverrideInstance
= NULL
;
177 if (mUniversalPayloadPlatformBootManagerOverrideInstance
!= NULL
) {
178 mUniversalPayloadPlatformBootManagerOverrideInstance
->BeforeConsole ();
183 // Register ENTER as CONTINUE key
185 Enter
.ScanCode
= SCAN_NULL
;
186 Enter
.UnicodeChar
= CHAR_CARRIAGE_RETURN
;
187 EfiBootManagerRegisterContinueKeyOption (0, &Enter
, NULL
);
189 if (FixedPcdGetBool (PcdBootManagerEscape
)) {
191 // Map Esc to Boot Manager Menu
193 CustomKey
.ScanCode
= SCAN_ESC
;
194 CustomKey
.UnicodeChar
= CHAR_NULL
;
197 // Map Esc to Boot Manager Menu
199 CustomKey
.ScanCode
= SCAN_F2
;
200 CustomKey
.UnicodeChar
= CHAR_NULL
;
203 EfiBootManagerGetBootManagerMenu (&BootOption
);
204 EfiBootManagerAddKeyOptionVariable (NULL
, (UINT16
)BootOption
.OptionNumber
, 0, &CustomKey
, NULL
);
207 // Also add Down key to Boot Manager Menu since some serial terminals don't support F2 key.
209 Down
.ScanCode
= SCAN_DOWN
;
210 Down
.UnicodeChar
= CHAR_NULL
;
211 EfiBootManagerGetBootManagerMenu (&BootOption
);
212 EfiBootManagerAddKeyOptionVariable (NULL
, (UINT16
)BootOption
.OptionNumber
, 0, &Down
, NULL
);
215 // Install ready to lock.
216 // This needs to be done before option rom dispatched.
218 InstallReadyToLock ();
221 // Dispatch deferred images after EndOfDxe event and ReadyToLock installation.
223 EfiBootManagerDispatchDeferredImages ();
225 PlatformConsoleInit ();
229 Do the platform specific action after the console is connected.
232 Dynamically switch output mode;
233 Signal console ready platform customized event;
234 Run diagnostics like memory testing;
235 Connect certain devices;
236 Dispatch additional option roms.
240 PlatformBootManagerAfterConsole (
244 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Black
;
245 EFI_GRAPHICS_OUTPUT_BLT_PIXEL White
;
247 if (mUniversalPayloadPlatformBootManagerOverrideInstance
!= NULL
) {
248 mUniversalPayloadPlatformBootManagerOverrideInstance
->AfterConsole ();
252 Black
.Blue
= Black
.Green
= Black
.Red
= Black
.Reserved
= 0;
253 White
.Blue
= White
.Green
= White
.Red
= White
.Reserved
= 0xFF;
255 EfiBootManagerConnectAll ();
256 EfiBootManagerRefreshAllBootOption ();
259 // Register UEFI Shell
261 PlatformRegisterFvBootOption (PcdGetPtr (PcdShellFile
), L
"UEFI Shell", LOAD_OPTION_ACTIVE
);
263 if (FixedPcdGetBool (PcdBootManagerEscape
)) {
266 L
" Esc or Down to enter Boot Manager Menu.\n"
267 L
" ENTER to boot directly.\n"
273 L
" F2 or Down to enter Boot Manager Menu.\n"
274 L
" ENTER to boot directly.\n"
281 This function is called each second during the boot manager waits the timeout.
283 @param TimeoutRemain The remaining timeout.
287 PlatformBootManagerWaitCallback (
291 if (mUniversalPayloadPlatformBootManagerOverrideInstance
!= NULL
) {
292 mUniversalPayloadPlatformBootManagerOverrideInstance
->WaitCallback (TimeoutRemain
);
299 The function is called when no boot option could be launched,
300 including platform recovery options and options pointing to applications
301 built into firmware volumes.
303 If this function returns, BDS attempts to enter an infinite loop.
307 PlatformBootManagerUnableToBoot (
311 if (mUniversalPayloadPlatformBootManagerOverrideInstance
!= NULL
) {
312 mUniversalPayloadPlatformBootManagerOverrideInstance
->UnableToBoot ();
319 Get/update PcdBootManagerMenuFile from GUID HOB which will be assigned in bootloader.
321 @param ImageHandle The firmware allocated handle for the EFI image.
322 @param SystemTable A pointer to the EFI System Table.
324 @retval EFI_SUCCESS The entry point is executed successfully.
325 @retval other Some error occurs.
330 PlatformBootManagerLibConstructor (
331 IN EFI_HANDLE ImageHandle
,
332 IN EFI_SYSTEM_TABLE
*SystemTable
338 UNIVERSAL_PAYLOAD_GENERIC_HEADER
*GenericHeader
;
339 UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU
*BootManagerMenuFile
;
341 GuidHob
= GetFirstGuidHob (&gEdkiiBootManagerMenuFileGuid
);
343 if (GuidHob
== NULL
) {
345 // If the HOB is not create, the default value of PcdBootManagerMenuFile will be used.
350 GenericHeader
= (UNIVERSAL_PAYLOAD_GENERIC_HEADER
*)GET_GUID_HOB_DATA (GuidHob
);
351 if ((sizeof (UNIVERSAL_PAYLOAD_GENERIC_HEADER
) > GET_GUID_HOB_DATA_SIZE (GuidHob
)) || (GenericHeader
->Length
> GET_GUID_HOB_DATA_SIZE (GuidHob
))) {
352 return EFI_NOT_FOUND
;
355 if (GenericHeader
->Revision
== UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU_REVISION
) {
356 BootManagerMenuFile
= (UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU
*)GET_GUID_HOB_DATA (GuidHob
);
357 if (BootManagerMenuFile
->Header
.Length
< UNIVERSAL_PAYLOAD_SIZEOF_THROUGH_FIELD (UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU
, FileName
)) {
358 return EFI_NOT_FOUND
;
361 Size
= sizeof (BootManagerMenuFile
->FileName
);
362 Status
= PcdSetPtrS (PcdBootManagerMenuFile
, &Size
, &BootManagerMenuFile
->FileName
);
363 ASSERT_EFI_ERROR (Status
);
365 return EFI_NOT_FOUND
;