PlatformBootManager.c 10 KB

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