PlatformBm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*++ @file
  2. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  3. Portions copyright (c) 2011, Apple Inc. All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "PlatformBm.h"
  7. EFI_GUID mBootMenuFile = {
  8. 0xEEC25BDC, 0x67F2, 0x4D95, { 0xB1, 0xD5, 0xF8, 0x1B, 0x20, 0x39, 0xD1, 0x1D }
  9. };
  10. /**
  11. Initialize the "Setup" variable.
  12. **/
  13. VOID
  14. SetupVariableInit (
  15. VOID
  16. )
  17. {
  18. EFI_STATUS Status;
  19. UINTN Size;
  20. EMU_SYSTEM_CONFIGURATION SystemConfigData;
  21. Size = sizeof (SystemConfigData);
  22. Status = gRT->GetVariable (
  23. L"Setup",
  24. &gEmuSystemConfigGuid,
  25. NULL,
  26. &Size,
  27. (VOID *)&SystemConfigData
  28. );
  29. if (EFI_ERROR (Status)) {
  30. //
  31. // SetupVariable is corrupt
  32. //
  33. SystemConfigData.ConOutRow = PcdGet32 (PcdConOutColumn);
  34. SystemConfigData.ConOutColumn = PcdGet32 (PcdConOutRow);
  35. Status = gRT->SetVariable (
  36. L"Setup",
  37. &gEmuSystemConfigGuid,
  38. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  39. sizeof (SystemConfigData),
  40. (VOID *)&SystemConfigData
  41. );
  42. if (EFI_ERROR (Status)) {
  43. DEBUG ((DEBUG_ERROR, "Failed to save Setup Variable to non-volatile storage, Status = %r\n", Status));
  44. }
  45. }
  46. }
  47. EFI_DEVICE_PATH *
  48. FvFilePath (
  49. EFI_GUID *FileGuid
  50. )
  51. {
  52. EFI_STATUS Status;
  53. EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
  54. MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;
  55. EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);
  56. Status = gBS->HandleProtocol (
  57. gImageHandle,
  58. &gEfiLoadedImageProtocolGuid,
  59. (VOID **)&LoadedImage
  60. );
  61. ASSERT_EFI_ERROR (Status);
  62. return AppendDevicePathNode (
  63. DevicePathFromHandle (LoadedImage->DeviceHandle),
  64. (EFI_DEVICE_PATH_PROTOCOL *)&FileNode
  65. );
  66. }
  67. /**
  68. Create one boot option for BootManagerMenuApp.
  69. @param FileGuid Input file guid for the BootManagerMenuApp.
  70. @param Description Description of the BootManagerMenuApp boot option.
  71. @param Position Position of the new load option to put in the ****Order variable.
  72. @param IsBootCategory Whether this is a boot category.
  73. @retval OptionNumber Return the option number info.
  74. **/
  75. UINTN
  76. RegisterBootManagerMenuAppBootOption (
  77. EFI_GUID *FileGuid,
  78. CHAR16 *Description,
  79. UINTN Position,
  80. BOOLEAN IsBootCategory
  81. )
  82. {
  83. EFI_STATUS Status;
  84. EFI_BOOT_MANAGER_LOAD_OPTION NewOption;
  85. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  86. UINTN OptionNumber;
  87. DevicePath = FvFilePath (FileGuid);
  88. Status = EfiBootManagerInitializeLoadOption (
  89. &NewOption,
  90. LoadOptionNumberUnassigned,
  91. LoadOptionTypeBoot,
  92. IsBootCategory ? LOAD_OPTION_ACTIVE : LOAD_OPTION_CATEGORY_APP,
  93. Description,
  94. DevicePath,
  95. NULL,
  96. 0
  97. );
  98. ASSERT_EFI_ERROR (Status);
  99. FreePool (DevicePath);
  100. Status = EfiBootManagerAddLoadOptionVariable (&NewOption, Position);
  101. ASSERT_EFI_ERROR (Status);
  102. OptionNumber = NewOption.OptionNumber;
  103. EfiBootManagerFreeLoadOption (&NewOption);
  104. return OptionNumber;
  105. }
  106. /**
  107. Check if it's a Device Path pointing to BootManagerMenuApp.
  108. @param DevicePath Input device path.
  109. @retval TRUE The device path is BootManagerMenuApp File Device Path.
  110. @retval FALSE The device path is NOT BootManagerMenuApp File Device Path.
  111. **/
  112. BOOLEAN
  113. IsBootManagerMenuAppFilePath (
  114. EFI_DEVICE_PATH_PROTOCOL *DevicePath
  115. )
  116. {
  117. EFI_HANDLE FvHandle;
  118. VOID *NameGuid;
  119. EFI_STATUS Status;
  120. Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePath, &FvHandle);
  121. if (!EFI_ERROR (Status)) {
  122. NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)DevicePath);
  123. if (NameGuid != NULL) {
  124. return CompareGuid (NameGuid, &mBootMenuFile);
  125. }
  126. }
  127. return FALSE;
  128. }
  129. /**
  130. Return the boot option number to the BootManagerMenuApp.
  131. If not found it in the current boot option, create a new one.
  132. @retval OptionNumber Return the boot option number to the BootManagerMenuApp.
  133. **/
  134. UINTN
  135. GetBootManagerMenuAppOption (
  136. VOID
  137. )
  138. {
  139. UINTN BootOptionCount;
  140. EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
  141. UINTN Index;
  142. UINTN OptionNumber;
  143. OptionNumber = 0;
  144. BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);
  145. for (Index = 0; Index < BootOptionCount; Index++) {
  146. if (IsBootManagerMenuAppFilePath (BootOptions[Index].FilePath)) {
  147. OptionNumber = BootOptions[Index].OptionNumber;
  148. break;
  149. }
  150. }
  151. EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);
  152. if (Index >= BootOptionCount) {
  153. //
  154. // If not found the BootManagerMenuApp, create it.
  155. //
  156. OptionNumber = (UINT16)RegisterBootManagerMenuAppBootOption (&mBootMenuFile, L"UEFI BootManagerMenuApp", (UINTN)-1, FALSE);
  157. }
  158. return OptionNumber;
  159. }
  160. /**
  161. Platform Bds init. Include the platform firmware vendor, revision
  162. and so crc check.
  163. **/
  164. VOID
  165. EFIAPI
  166. PlatformBootManagerBeforeConsole (
  167. VOID
  168. )
  169. {
  170. UINTN Index;
  171. SetupVariableInit ();
  172. EfiEventGroupSignal (&gEfiEndOfDxeEventGroupGuid);
  173. Index = 0;
  174. while (gPlatformConsole[Index].DevicePath != NULL) {
  175. //
  176. // Update the console variable with the connect type
  177. //
  178. if ((gPlatformConsole[Index].ConnectType & CONSOLE_IN) == CONSOLE_IN) {
  179. EfiBootManagerUpdateConsoleVariable (ConIn, gPlatformConsole[Index].DevicePath, NULL);
  180. }
  181. if ((gPlatformConsole[Index].ConnectType & CONSOLE_OUT) == CONSOLE_OUT) {
  182. EfiBootManagerUpdateConsoleVariable (ConOut, gPlatformConsole[Index].DevicePath, NULL);
  183. }
  184. if ((gPlatformConsole[Index].ConnectType & STD_ERROR) == STD_ERROR) {
  185. EfiBootManagerUpdateConsoleVariable (ErrOut, gPlatformConsole[Index].DevicePath, NULL);
  186. }
  187. Index++;
  188. }
  189. }
  190. /**
  191. Connect with predefined platform connect sequence,
  192. the OEM/IBV can customize with their own connect sequence.
  193. **/
  194. VOID
  195. PlatformBdsConnectSequence (
  196. VOID
  197. )
  198. {
  199. //
  200. // Just use the simple policy to connect all devices
  201. //
  202. EfiBootManagerConnectAll ();
  203. }
  204. /**
  205. Perform the platform diagnostic, such like test memory. OEM/IBV also
  206. can customize this fuction to support specific platform diagnostic.
  207. @param MemoryTestLevel The memory test intensive level
  208. @param QuietBoot Indicate if need to enable the quiet boot
  209. **/
  210. VOID
  211. PlatformBdsDiagnostics (
  212. IN EXTENDMEM_COVERAGE_LEVEL MemoryTestLevel,
  213. IN BOOLEAN QuietBoot
  214. )
  215. {
  216. EFI_STATUS Status;
  217. //
  218. // Here we can decide if we need to show
  219. // the diagnostics screen
  220. //
  221. if (QuietBoot) {
  222. BootLogoEnableLogo ();
  223. //
  224. // Perform system diagnostic
  225. //
  226. Status = PlatformBootManagerMemoryTest (MemoryTestLevel);
  227. if (EFI_ERROR (Status)) {
  228. BootLogoDisableLogo ();
  229. }
  230. return;
  231. }
  232. //
  233. // Perform system diagnostic
  234. //
  235. PlatformBootManagerMemoryTest (MemoryTestLevel);
  236. }
  237. /**
  238. Register the static boot options.
  239. **/
  240. VOID
  241. PlatformBdsRegisterStaticBootOptions (
  242. VOID
  243. )
  244. {
  245. EFI_GRAPHICS_OUTPUT_BLT_PIXEL Black;
  246. EFI_GRAPHICS_OUTPUT_BLT_PIXEL White;
  247. EFI_INPUT_KEY Enter;
  248. EFI_INPUT_KEY F2;
  249. EFI_INPUT_KEY F7;
  250. EFI_BOOT_MANAGER_LOAD_OPTION BootOption;
  251. UINTN OptionNumber;
  252. Black.Blue = Black.Green = Black.Red = Black.Reserved = 0;
  253. White.Blue = White.Green = White.Red = White.Reserved = 0xFF;
  254. //
  255. // Register ENTER as CONTINUE key
  256. //
  257. Enter.ScanCode = SCAN_NULL;
  258. Enter.UnicodeChar = CHAR_CARRIAGE_RETURN;
  259. EfiBootManagerRegisterContinueKeyOption (0, &Enter, NULL);
  260. //
  261. // Map F2 to Boot Manager Menu
  262. //
  263. F2.ScanCode = SCAN_F2;
  264. F2.UnicodeChar = CHAR_NULL;
  265. EfiBootManagerGetBootManagerMenu (&BootOption);
  266. EfiBootManagerAddKeyOptionVariable (NULL, (UINT16)BootOption.OptionNumber, 0, &F2, NULL);
  267. //
  268. // 3. Boot Device List menu
  269. //
  270. F7.ScanCode = SCAN_F7;
  271. F7.UnicodeChar = CHAR_NULL;
  272. OptionNumber = GetBootManagerMenuAppOption ();
  273. EfiBootManagerAddKeyOptionVariable (NULL, (UINT16)OptionNumber, 0, &F7, NULL);
  274. PrintXY (10, 10, &White, &Black, L"F2 to enter Setup. ");
  275. PrintXY (10, 30, &White, &Black, L"F7 to enter Boot Manager Menu.");
  276. PrintXY (10, 50, &White, &Black, L"Enter to boot directly.");
  277. }
  278. /**
  279. Returns the priority number.
  280. @param BootOption
  281. **/
  282. UINTN
  283. BootOptionPriority (
  284. CONST EFI_BOOT_MANAGER_LOAD_OPTION *BootOption
  285. )
  286. {
  287. //
  288. // Make sure Shell is first
  289. //
  290. if (StrCmp (BootOption->Description, L"UEFI Shell") == 0) {
  291. return 0;
  292. }
  293. return 100;
  294. }
  295. INTN
  296. EFIAPI
  297. CompareBootOption (
  298. CONST EFI_BOOT_MANAGER_LOAD_OPTION *Left,
  299. CONST EFI_BOOT_MANAGER_LOAD_OPTION *Right
  300. )
  301. {
  302. return BootOptionPriority (Left) - BootOptionPriority (Right);
  303. }
  304. /**
  305. Do the platform specific action after the console is connected.
  306. Such as:
  307. Dynamically switch output mode;
  308. Signal console ready platform customized event;
  309. Run diagnostics like memory testing;
  310. Connect certain devices;
  311. Dispatch aditional option roms.
  312. **/
  313. VOID
  314. EFIAPI
  315. PlatformBootManagerAfterConsole (
  316. VOID
  317. )
  318. {
  319. //
  320. // Go the different platform policy with different boot mode
  321. // Notes: this part code can be change with the table policy
  322. //
  323. switch (GetBootModeHob ()) {
  324. case BOOT_ASSUMING_NO_CONFIGURATION_CHANGES:
  325. case BOOT_WITH_MINIMAL_CONFIGURATION:
  326. PlatformBdsDiagnostics (IGNORE, TRUE);
  327. //
  328. // Perform some platform specific connect sequence
  329. //
  330. PlatformBdsConnectSequence ();
  331. break;
  332. case BOOT_IN_RECOVERY_MODE:
  333. PlatformBdsDiagnostics (EXTENSIVE, FALSE);
  334. break;
  335. case BOOT_WITH_FULL_CONFIGURATION:
  336. case BOOT_WITH_FULL_CONFIGURATION_PLUS_DIAGNOSTICS:
  337. case BOOT_WITH_DEFAULT_SETTINGS:
  338. default:
  339. PlatformBdsDiagnostics (IGNORE, TRUE);
  340. PlatformBdsRegisterStaticBootOptions ();
  341. PlatformBdsConnectSequence ();
  342. EfiBootManagerRefreshAllBootOption ();
  343. EfiBootManagerSortLoadOptionVariable (LoadOptionTypeBoot, (SORT_COMPARE)CompareBootOption);
  344. break;
  345. }
  346. }
  347. /**
  348. This function is called each second during the boot manager waits the timeout.
  349. @param TimeoutRemain The remaining timeout.
  350. **/
  351. VOID
  352. EFIAPI
  353. PlatformBootManagerWaitCallback (
  354. UINT16 TimeoutRemain
  355. )
  356. {
  357. EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION Black;
  358. EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION White;
  359. UINT16 Timeout;
  360. Timeout = PcdGet16 (PcdPlatformBootTimeOut);
  361. Black.Raw = 0x00000000;
  362. White.Raw = 0x00FFFFFF;
  363. BootLogoUpdateProgress (
  364. White.Pixel,
  365. Black.Pixel,
  366. L"Start boot option",
  367. White.Pixel,
  368. (Timeout - TimeoutRemain) * 100 / Timeout,
  369. 0
  370. );
  371. }
  372. /**
  373. The function is called when no boot option could be launched,
  374. including platform recovery options and options pointing to applications
  375. built into firmware volumes.
  376. If this function returns, BDS attempts to enter an infinite loop.
  377. **/
  378. VOID
  379. EFIAPI
  380. PlatformBootManagerUnableToBoot (
  381. VOID
  382. )
  383. {
  384. return;
  385. }