+/*++ @file\r
+\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+Portions copyright (c) 2011, Apple Inc. All rights reserved.\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "PlatformBm.h"\r
+\r
+EFI_GUID mBootMenuFile = {\r
+ 0xEEC25BDC, 0x67F2, 0x4D95, { 0xB1, 0xD5, 0xF8, 0x1B, 0x20, 0x39, 0xD1, 0x1D }\r
+};\r
+\r
+/**\r
+ Initialize the "Setup" variable.\r
+**/\r
+VOID\r
+SetupVariableInit (\r
+ VOID\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN Size;\r
+ EMU_SYSTEM_CONFIGURATION SystemConfigData;\r
+\r
+ Size = sizeof (SystemConfigData);\r
+ Status = gRT->GetVariable (\r
+ L"Setup",\r
+ &gEmuSystemConfigGuid,\r
+ NULL,\r
+ &Size,\r
+ (VOID *) &SystemConfigData\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // SetupVariable is corrupt\r
+ //\r
+ SystemConfigData.ConOutRow = PcdGet32 (PcdConOutColumn);\r
+ SystemConfigData.ConOutColumn = PcdGet32 (PcdConOutRow);\r
+\r
+ Status = gRT->SetVariable (\r
+ L"Setup",\r
+ &gEmuSystemConfigGuid,\r
+ EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+ sizeof (SystemConfigData),\r
+ (VOID *) &SystemConfigData\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR, "Failed to save Setup Variable to non-volatile storage, Status = %r\n", Status));\r
+ }\r
+ }\r
+}\r
+\r
+EFI_DEVICE_PATH *\r
+FvFilePath (\r
+ EFI_GUID *FileGuid\r
+ )\r
+{\r
+\r
+ EFI_STATUS Status;\r
+ EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
+ MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;\r
+\r
+ EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);\r
+\r
+ Status = gBS->HandleProtocol (\r
+ gImageHandle,\r
+ &gEfiLoadedImageProtocolGuid,\r
+ (VOID **) &LoadedImage\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+ return AppendDevicePathNode (\r
+ DevicePathFromHandle (LoadedImage->DeviceHandle),\r
+ (EFI_DEVICE_PATH_PROTOCOL *) &FileNode\r
+ );\r
+}\r
+\r
+/**\r
+ Create one boot option for BootManagerMenuApp.\r
+\r
+ @param FileGuid Input file guid for the BootManagerMenuApp.\r
+ @param Description Description of the BootManagerMenuApp boot option.\r
+ @param Position Position of the new load option to put in the ****Order variable.\r
+ @param IsBootCategory Whether this is a boot category.\r
+\r
+\r
+ @retval OptionNumber Return the option number info.\r
+\r
+**/\r
+UINTN\r
+RegisterBootManagerMenuAppBootOption (\r
+ EFI_GUID *FileGuid,\r
+ CHAR16 *Description,\r
+ UINTN Position,\r
+ BOOLEAN IsBootCategory\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_BOOT_MANAGER_LOAD_OPTION NewOption;\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ UINTN OptionNumber;\r
+\r
+ DevicePath = FvFilePath (FileGuid);\r
+ Status = EfiBootManagerInitializeLoadOption (\r
+ &NewOption,\r
+ LoadOptionNumberUnassigned,\r
+ LoadOptionTypeBoot,\r
+ IsBootCategory ? LOAD_OPTION_ACTIVE : LOAD_OPTION_CATEGORY_APP,\r
+ Description,\r
+ DevicePath,\r
+ NULL,\r
+ 0\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+ FreePool (DevicePath);\r
+\r
+ Status = EfiBootManagerAddLoadOptionVariable (&NewOption, Position);\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ OptionNumber = NewOption.OptionNumber;\r
+\r
+ EfiBootManagerFreeLoadOption (&NewOption);\r
+\r
+ return OptionNumber;\r
+}\r
+\r
+/**\r
+ Check if it's a Device Path pointing to BootManagerMenuApp.\r
+\r
+ @param DevicePath Input device path.\r
+\r
+ @retval TRUE The device path is BootManagerMenuApp File Device Path.\r
+ @retval FALSE The device path is NOT BootManagerMenuApp File Device Path.\r
+**/\r
+BOOLEAN\r
+IsBootManagerMenuAppFilePath (\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
+)\r
+{\r
+ EFI_HANDLE FvHandle;\r
+ VOID *NameGuid;\r
+ EFI_STATUS Status;\r
+\r
+ Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePath, &FvHandle);\r
+ if (!EFI_ERROR (Status)) {\r
+ NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath);\r
+ if (NameGuid != NULL) {\r
+ return CompareGuid (NameGuid, &mBootMenuFile);\r
+ }\r
+ }\r
+\r
+ return FALSE;\r
+}\r
+\r
+/**\r
+ Return the boot option number to the BootManagerMenuApp.\r
+\r
+ If not found it in the current boot option, create a new one.\r
+\r
+ @retval OptionNumber Return the boot option number to the BootManagerMenuApp.\r
+\r
+**/\r
+UINTN\r
+GetBootManagerMenuAppOption (\r
+ VOID\r
+ )\r
+{\r
+ UINTN BootOptionCount;\r
+ EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;\r
+ UINTN Index;\r
+ UINTN OptionNumber;\r
+\r
+ OptionNumber = 0;\r
+\r
+ BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);\r
+\r
+ for (Index = 0; Index < BootOptionCount; Index++) {\r
+ if (IsBootManagerMenuAppFilePath (BootOptions[Index].FilePath)) {\r
+ OptionNumber = BootOptions[Index].OptionNumber;\r
+ break;\r
+ }\r
+ }\r
+\r
+ EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r
+\r
+ if (Index >= BootOptionCount) {\r
+ //\r
+ // If not found the BootManagerMenuApp, create it.\r
+ //\r
+ OptionNumber = (UINT16) RegisterBootManagerMenuAppBootOption (&mBootMenuFile, L"UEFI BootManagerMenuApp", (UINTN) -1, FALSE);\r
+ }\r
+\r
+ return OptionNumber;\r
+}\r
+\r
+/**\r
+ Platform Bds init. Include the platform firmware vendor, revision\r
+ and so crc check.\r
+**/\r
+VOID\r
+EFIAPI\r
+PlatformBootManagerBeforeConsole (\r
+ VOID\r
+ )\r
+{\r
+ UINTN Index;\r
+\r
+ SetupVariableInit ();\r
+\r
+ EfiEventGroupSignal (&gEfiEndOfDxeEventGroupGuid);\r
+\r
+ Index = 0;\r
+ while (gPlatformConsole[Index].DevicePath != NULL) {\r
+ //\r
+ // Update the console variable with the connect type\r
+ //\r
+ if ((gPlatformConsole[Index].ConnectType & CONSOLE_IN) == CONSOLE_IN) {\r
+ EfiBootManagerUpdateConsoleVariable (ConIn, gPlatformConsole[Index].DevicePath, NULL);\r
+ }\r
+\r
+ if ((gPlatformConsole[Index].ConnectType & CONSOLE_OUT) == CONSOLE_OUT) {\r
+ EfiBootManagerUpdateConsoleVariable (ConOut, gPlatformConsole[Index].DevicePath, NULL);\r
+ }\r
+\r
+ if ((gPlatformConsole[Index].ConnectType & STD_ERROR) == STD_ERROR) {\r
+ EfiBootManagerUpdateConsoleVariable (ErrOut, gPlatformConsole[Index].DevicePath, NULL);\r
+ }\r
+\r
+ Index++;\r
+ }\r
+}\r
+\r
+/**\r
+ Connect with predefined platform connect sequence,\r
+ the OEM/IBV can customize with their own connect sequence.\r
+**/\r
+VOID\r
+PlatformBdsConnectSequence (\r
+ VOID\r
+ )\r
+{\r
+ //\r
+ // Just use the simple policy to connect all devices\r
+ //\r
+ EfiBootManagerConnectAll ();\r
+}\r
+\r
+/**\r
+ Perform the platform diagnostic, such like test memory. OEM/IBV also\r
+ can customize this fuction to support specific platform diagnostic.\r
+\r
+ @param MemoryTestLevel The memory test intensive level\r
+ @param QuietBoot Indicate if need to enable the quiet boot\r
+**/\r
+VOID\r
+PlatformBdsDiagnostics (\r
+ IN EXTENDMEM_COVERAGE_LEVEL MemoryTestLevel,\r
+ IN BOOLEAN QuietBoot\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Here we can decide if we need to show\r
+ // the diagnostics screen\r
+ //\r
+ if (QuietBoot) {\r
+ BootLogoEnableLogo ();\r
+\r
+ //\r
+ // Perform system diagnostic\r
+ //\r
+ Status = PlatformBootManagerMemoryTest (MemoryTestLevel);\r
+ if (EFI_ERROR (Status)) {\r
+ BootLogoDisableLogo ();\r
+ }\r
+\r
+ return;\r
+ }\r
+\r
+ //\r
+ // Perform system diagnostic\r
+ //\r
+ PlatformBootManagerMemoryTest (MemoryTestLevel);\r
+}\r
+\r
+/**\r
+ Register the static boot options.\r
+**/\r
+VOID\r
+PlatformBdsRegisterStaticBootOptions (\r
+ VOID\r
+ )\r
+{\r
+ EFI_GRAPHICS_OUTPUT_BLT_PIXEL Black;\r
+ EFI_GRAPHICS_OUTPUT_BLT_PIXEL White;\r
+ EFI_INPUT_KEY Enter;\r
+ EFI_INPUT_KEY F2;\r
+ EFI_INPUT_KEY F7;\r
+ EFI_BOOT_MANAGER_LOAD_OPTION BootOption;\r
+ UINTN OptionNumber;\r
+\r
+ Black.Blue = Black.Green = Black.Red = Black.Reserved = 0;\r
+ White.Blue = White.Green = White.Red = White.Reserved = 0xFF;\r
+\r
+ //\r
+ // Register ENTER as CONTINUE key\r
+ //\r
+ Enter.ScanCode = SCAN_NULL;\r
+ Enter.UnicodeChar = CHAR_CARRIAGE_RETURN;\r
+ EfiBootManagerRegisterContinueKeyOption (0, &Enter, NULL);\r
+ //\r
+ // Map F2 to Boot Manager Menu\r
+ //\r
+ F2.ScanCode = SCAN_F2;\r
+ F2.UnicodeChar = CHAR_NULL;\r
+ EfiBootManagerGetBootManagerMenu (&BootOption);\r
+ EfiBootManagerAddKeyOptionVariable (NULL, (UINT16) BootOption.OptionNumber, 0, &F2, NULL);\r
+\r
+ //\r
+ // 3. Boot Device List menu\r
+ //\r
+ F7.ScanCode = SCAN_F7;\r
+ F7.UnicodeChar = CHAR_NULL;\r
+ OptionNumber = GetBootManagerMenuAppOption ();\r
+ EfiBootManagerAddKeyOptionVariable (NULL, (UINT16)OptionNumber, 0, &F7, NULL);\r
+\r
+ PrintXY (10, 10, &White, &Black, L"F2 to enter Setup. ");\r
+ PrintXY (10, 30, &White, &Black, L"F7 to enter Boot Manager Menu.");\r
+ PrintXY (10, 50, &White, &Black, L"Enter to boot directly.");\r
+}\r
+\r
+/**\r
+ Do the platform specific action after the console is connected.\r
+\r
+ Such as:\r
+ Dynamically switch output mode;\r
+ Signal console ready platform customized event;\r
+ Run diagnostics like memory testing;\r
+ Connect certain devices;\r
+ Dispatch aditional option roms.\r
+**/\r
+VOID\r
+EFIAPI\r
+PlatformBootManagerAfterConsole (\r
+ VOID\r
+ )\r
+{\r
+\r
+ //\r
+ // Go the different platform policy with different boot mode\r
+ // Notes: this part code can be change with the table policy\r
+ //\r
+ switch (GetBootModeHob ()) {\r
+\r
+ case BOOT_ASSUMING_NO_CONFIGURATION_CHANGES:\r
+ case BOOT_WITH_MINIMAL_CONFIGURATION:\r
+ PlatformBdsDiagnostics (IGNORE, TRUE);\r
+\r
+ //\r
+ // Perform some platform specific connect sequence\r
+ //\r
+ PlatformBdsConnectSequence ();\r
+ break;\r
+\r
+ case BOOT_IN_RECOVERY_MODE:\r
+ PlatformBdsDiagnostics (EXTENSIVE, FALSE);\r
+ break;\r
+\r
+ case BOOT_WITH_FULL_CONFIGURATION:\r
+ case BOOT_WITH_FULL_CONFIGURATION_PLUS_DIAGNOSTICS:\r
+ case BOOT_WITH_DEFAULT_SETTINGS:\r
+ default:\r
+ PlatformBdsDiagnostics (IGNORE, TRUE);\r
+ PlatformBdsRegisterStaticBootOptions ();\r
+ PlatformBdsConnectSequence ();\r
+ EfiBootManagerRefreshAllBootOption ();\r
+ break;\r
+ }\r
+}\r
+\r
+/**\r
+ This function is called each second during the boot manager waits the timeout.\r
+\r
+ @param TimeoutRemain The remaining timeout.\r
+**/\r
+VOID\r
+EFIAPI\r
+PlatformBootManagerWaitCallback (\r
+ UINT16 TimeoutRemain\r
+ )\r
+{\r
+ EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION Black;\r
+ EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION White;\r
+ UINT16 Timeout;\r
+\r
+ Timeout = PcdGet16 (PcdPlatformBootTimeOut);\r
+\r
+ Black.Raw = 0x00000000;\r
+ White.Raw = 0x00FFFFFF;\r
+\r
+ BootLogoUpdateProgress (\r
+ White.Pixel,\r
+ Black.Pixel,\r
+ L"Start boot option",\r
+ White.Pixel,\r
+ (Timeout - TimeoutRemain) * 100 / Timeout,\r
+ 0\r
+ );\r
+}\r
+\r
+/**\r
+ The function is called when no boot option could be launched,\r
+ including platform recovery options and options pointing to applications\r
+ built into firmware volumes.\r
+\r
+ If this function returns, BDS attempts to enter an infinite loop.\r
+**/\r
+VOID\r
+EFIAPI\r
+PlatformBootManagerUnableToBoot (\r
+ VOID\r
+ )\r
+{\r
+ return;\r
+}\r
+\r