]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c
ArmVirtPkg/PlatformBootManagerLib: return to "-kernel before boot devices"
[mirror_edk2.git] / ArmVirtPkg / Library / PlatformBootManagerLib / PlatformBm.c
index b114420fd020f5f2f7ce6b20f325e44ed6344c41..5d5e51d8c870cec53f733cc3d71434443dd3397e 100644 (file)
@@ -3,7 +3,7 @@
 \r
   Copyright (C) 2015-2016, Red Hat, Inc.\r
   Copyright (c) 2014, ARM Ltd. All rights reserved.<BR>\r
-  Copyright (c) 2004 - 2008, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
 \r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License which accompanies this\r
 **/\r
 \r
 #include <IndustryStandard/Pci22.h>\r
+#include <Library/BootLogoLib.h>\r
 #include <Library/DevicePathLib.h>\r
 #include <Library/PcdLib.h>\r
 #include <Library/QemuBootOrderLib.h>\r
 #include <Library/UefiBootManagerLib.h>\r
 #include <Protocol/DevicePath.h>\r
+#include <Protocol/FirmwareVolume2.h>\r
 #include <Protocol/GraphicsOutput.h>\r
+#include <Protocol/LoadedImage.h>\r
 #include <Protocol/PciIo.h>\r
 #include <Protocol/PciRootBridgeIo.h>\r
 #include <Guid/EventGroup.h>\r
@@ -322,6 +325,238 @@ AddOutput (
     ReportText));\r
 }\r
 \r
+STATIC\r
+VOID\r
+PlatformRegisterFvBootOption (\r
+  EFI_GUID                         *FileGuid,\r
+  CHAR16                           *Description,\r
+  UINT32                           Attributes\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  INTN                              OptionIndex;\r
+  EFI_BOOT_MANAGER_LOAD_OPTION      NewOption;\r
+  EFI_BOOT_MANAGER_LOAD_OPTION      *BootOptions;\r
+  UINTN                             BootOptionCount;\r
+  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;\r
+  EFI_LOADED_IMAGE_PROTOCOL         *LoadedImage;\r
+  EFI_DEVICE_PATH_PROTOCOL          *DevicePath;\r
+\r
+  Status = gBS->HandleProtocol (\r
+                  gImageHandle,\r
+                  &gEfiLoadedImageProtocolGuid,\r
+                  (VOID **) &LoadedImage\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);\r
+  DevicePath = DevicePathFromHandle (LoadedImage->DeviceHandle);\r
+  ASSERT (DevicePath != NULL);\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *) &FileNode\r
+                 );\r
+  ASSERT (DevicePath != NULL);\r
+\r
+  Status = EfiBootManagerInitializeLoadOption (\r
+             &NewOption,\r
+             LoadOptionNumberUnassigned,\r
+             LoadOptionTypeBoot,\r
+             Attributes,\r
+             Description,\r
+             DevicePath,\r
+             NULL,\r
+             0\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+  FreePool (DevicePath);\r
+\r
+  BootOptions = EfiBootManagerGetLoadOptions (\r
+                  &BootOptionCount, LoadOptionTypeBoot\r
+                  );\r
+\r
+  OptionIndex = EfiBootManagerFindLoadOption (\r
+                  &NewOption, BootOptions, BootOptionCount\r
+                  );\r
+\r
+  if (OptionIndex == -1) {\r
+    Status = EfiBootManagerAddLoadOptionVariable (&NewOption, MAX_UINTN);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+  EfiBootManagerFreeLoadOption (&NewOption);\r
+  EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r
+}\r
+\r
+\r
+/**\r
+  Remove all MemoryMapped(...)/FvFile(...) and Fv(...)/FvFile(...) boot options\r
+  whose device paths do not resolve exactly to an FvFile in the system.\r
+\r
+  This removes any boot options that point to binaries built into the firmware\r
+  and have become stale due to any of the following:\r
+  - FvMain's base address or size changed (historical),\r
+  - FvMain's FvNameGuid changed,\r
+  - the FILE_GUID of the pointed-to binary changed,\r
+  - the referenced binary is no longer built into the firmware.\r
+\r
+  EfiBootManagerFindLoadOption() used in PlatformRegisterFvBootOption() only\r
+  avoids exact duplicates.\r
+**/\r
+STATIC\r
+VOID\r
+RemoveStaleFvFileOptions (\r
+  VOID\r
+  )\r
+{\r
+  EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;\r
+  UINTN                        BootOptionCount;\r
+  UINTN                        Index;\r
+\r
+  BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount,\r
+                  LoadOptionTypeBoot);\r
+\r
+  for (Index = 0; Index < BootOptionCount; ++Index) {\r
+    EFI_DEVICE_PATH_PROTOCOL *Node1, *Node2, *SearchNode;\r
+    EFI_STATUS               Status;\r
+    EFI_HANDLE               FvHandle;\r
+\r
+    //\r
+    // If the device path starts with neither MemoryMapped(...) nor Fv(...),\r
+    // then keep the boot option.\r
+    //\r
+    Node1 = BootOptions[Index].FilePath;\r
+    if (!(DevicePathType (Node1) == HARDWARE_DEVICE_PATH &&\r
+          DevicePathSubType (Node1) == HW_MEMMAP_DP) &&\r
+        !(DevicePathType (Node1) == MEDIA_DEVICE_PATH &&\r
+          DevicePathSubType (Node1) == MEDIA_PIWG_FW_VOL_DP)) {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // If the second device path node is not FvFile(...), then keep the boot\r
+    // option.\r
+    //\r
+    Node2 = NextDevicePathNode (Node1);\r
+    if (DevicePathType (Node2) != MEDIA_DEVICE_PATH ||\r
+        DevicePathSubType (Node2) != MEDIA_PIWG_FW_FILE_DP) {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // Locate the Firmware Volume2 protocol instance that is denoted by the\r
+    // boot option. If this lookup fails (i.e., the boot option references a\r
+    // firmware volume that doesn't exist), then we'll proceed to delete the\r
+    // boot option.\r
+    //\r
+    SearchNode = Node1;\r
+    Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid,\r
+                    &SearchNode, &FvHandle);\r
+\r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // The firmware volume was found; now let's see if it contains the FvFile\r
+      // identified by GUID.\r
+      //\r
+      EFI_FIRMWARE_VOLUME2_PROTOCOL     *FvProtocol;\r
+      MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvFileNode;\r
+      UINTN                             BufferSize;\r
+      EFI_FV_FILETYPE                   FoundType;\r
+      EFI_FV_FILE_ATTRIBUTES            FileAttributes;\r
+      UINT32                            AuthenticationStatus;\r
+\r
+      Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid,\r
+                      (VOID **)&FvProtocol);\r
+      ASSERT_EFI_ERROR (Status);\r
+\r
+      FvFileNode = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)Node2;\r
+      //\r
+      // Buffer==NULL means we request metadata only: BufferSize, FoundType,\r
+      // FileAttributes.\r
+      //\r
+      Status = FvProtocol->ReadFile (\r
+                             FvProtocol,\r
+                             &FvFileNode->FvFileName, // NameGuid\r
+                             NULL,                    // Buffer\r
+                             &BufferSize,\r
+                             &FoundType,\r
+                             &FileAttributes,\r
+                             &AuthenticationStatus\r
+                             );\r
+      if (!EFI_ERROR (Status)) {\r
+        //\r
+        // The FvFile was found. Keep the boot option.\r
+        //\r
+        continue;\r
+      }\r
+    }\r
+\r
+    //\r
+    // Delete the boot option.\r
+    //\r
+    Status = EfiBootManagerDeleteLoadOptionVariable (\r
+               BootOptions[Index].OptionNumber, LoadOptionTypeBoot);\r
+    DEBUG_CODE (\r
+      CHAR16 *DevicePathString;\r
+\r
+      DevicePathString = ConvertDevicePathToText(BootOptions[Index].FilePath,\r
+                           FALSE, FALSE);\r
+      DEBUG ((\r
+        EFI_ERROR (Status) ? EFI_D_WARN : EFI_D_VERBOSE,\r
+        "%a: removing stale Boot#%04x %s: %r\n",\r
+        __FUNCTION__,\r
+        (UINT32)BootOptions[Index].OptionNumber,\r
+        DevicePathString == NULL ? L"<unavailable>" : DevicePathString,\r
+        Status\r
+        ));\r
+      if (DevicePathString != NULL) {\r
+        FreePool (DevicePathString);\r
+      }\r
+      );\r
+  }\r
+\r
+  EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r
+}\r
+\r
+\r
+STATIC\r
+VOID\r
+PlatformRegisterOptionsAndKeys (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                   Status;\r
+  EFI_INPUT_KEY                Enter;\r
+  EFI_INPUT_KEY                F2;\r
+  EFI_INPUT_KEY                Esc;\r
+  EFI_BOOT_MANAGER_LOAD_OPTION BootOption;\r
+\r
+  //\r
+  // Register ENTER as CONTINUE key\r
+  //\r
+  Enter.ScanCode    = SCAN_NULL;\r
+  Enter.UnicodeChar = CHAR_CARRIAGE_RETURN;\r
+  Status = EfiBootManagerRegisterContinueKeyOption (0, &Enter, NULL);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Map F2 and ESC to Boot Manager Menu\r
+  //\r
+  F2.ScanCode     = SCAN_F2;\r
+  F2.UnicodeChar  = CHAR_NULL;\r
+  Esc.ScanCode    = SCAN_ESC;\r
+  Esc.UnicodeChar = CHAR_NULL;\r
+  Status = EfiBootManagerGetBootManagerMenu (&BootOption);\r
+  ASSERT_EFI_ERROR (Status);\r
+  Status = EfiBootManagerAddKeyOptionVariable (\r
+             NULL, (UINT16) BootOption.OptionNumber, 0, &F2, NULL\r
+             );\r
+  ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);\r
+  Status = EfiBootManagerAddKeyOptionVariable (\r
+             NULL, (UINT16) BootOption.OptionNumber, 0, &Esc, NULL\r
+             );\r
+  ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);\r
+}\r
+\r
 \r
 //\r
 // BDS Platform Functions\r
@@ -343,11 +578,18 @@ PlatformBootManagerBeforeConsole (
   VOID\r
   )\r
 {\r
+  RETURN_STATUS PcdStatus;\r
+\r
   //\r
   // Signal EndOfDxe PI Event\r
   //\r
   EfiEventGroupSignal (&gEfiEndOfDxeEventGroupGuid);\r
 \r
+  //\r
+  // Dispatch deferred images after EndOfDxe event.\r
+  //\r
+  EfiBootManagerDispatchDeferredImages ();\r
+\r
   //\r
   // Locate the PCI root bridges and make the PCI bus driver connect each,\r
   // non-recursively. This will produce a number of child handles with PciIo on\r
@@ -394,7 +636,14 @@ PlatformBootManagerBeforeConsole (
   //\r
   // Set the front page timeout from the QEMU configuration.\r
   //\r
-  PcdSet16 (PcdPlatformBootTimeOut, GetFrontPageTimeoutFromQemu ());\r
+  PcdStatus = PcdSet16S (PcdPlatformBootTimeOut,\r
+                GetFrontPageTimeoutFromQemu ());\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
+\r
+  //\r
+  // Register platform-specific boot options and keyboard shortcuts.\r
+  //\r
+  PlatformRegisterOptionsAndKeys ();\r
 }\r
 \r
 /**\r
@@ -414,81 +663,47 @@ PlatformBootManagerAfterConsole (
   VOID\r
   )\r
 {\r
+  RETURN_STATUS Status;\r
+\r
   //\r
   // Show the splash screen.\r
   //\r
-  EnableQuietBoot (PcdGetPtr (PcdLogoFile));\r
+  BootLogoEnableLogo ();\r
 \r
   //\r
-  // Connect the rest of the devices.\r
+  // Process QEMU's -kernel command line option. The kernel booted this way\r
+  // will receive ACPI tables: in PlatformBootManagerBeforeConsole(), we\r
+  // connected any and all PCI root bridges, and then signaled the ACPI\r
+  // platform driver.\r
   //\r
-  BdsLibConnectAll ();\r
+  TryRunningQemuKernel ();\r
 \r
   //\r
-  // Process QEMU's -kernel command line option. Note that the kernel booted\r
-  // this way should receive ACPI tables, which is why we connect all devices\r
-  // first (see above) -- PCI enumeration blocks ACPI table installation, if\r
-  // there is a PCI host.\r
+  // Connect the purported boot devices.\r
   //\r
-  TryRunningQemuKernel ();\r
+  Status = ConnectDevicesFromQemu ();\r
+  if (RETURN_ERROR (Status)) {\r
+    //\r
+    // Connect the rest of the devices.\r
+    //\r
+    EfiBootManagerConnectAll ();\r
+  }\r
 \r
-  BdsLibEnumerateAllBootOption (BootOptionList);\r
-  SetBootOrderFromQemu (BootOptionList);\r
   //\r
-  // The BootOrder variable may have changed, reload the in-memory list with\r
-  // it.\r
+  // Enumerate all possible boot options, then filter and reorder them based on\r
+  // the QEMU configuration.\r
   //\r
-  BdsLibBuildOptionFromVar (BootOptionList, L"BootOrder");\r
-}\r
+  EfiBootManagerRefreshAllBootOption ();\r
 \r
-/**\r
-  Hook point after a boot attempt succeeds. We don't expect a boot option to\r
-  return, so the UEFI 2.0 specification defines that you will default to an\r
-  interactive mode and stop processing the BootOrder list in this case. This\r
-  is also a platform implementation and can be customized by IBV/OEM.\r
-\r
-  @param  Option                  Pointer to Boot Option that succeeded to boot.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-PlatformBdsBootSuccess (\r
-  IN  BDS_COMMON_OPTION *Option\r
-  )\r
-{\r
-}\r
-\r
-/**\r
-  Hook point after a boot attempt fails.\r
-\r
-  @param  Option                  Pointer to Boot Option that failed to boot.\r
-  @param  Status                  Status returned from failed boot.\r
-  @param  ExitData                Exit data returned from failed boot.\r
-  @param  ExitDataSize            Exit data size returned from failed boot.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-PlatformBdsBootFail (\r
-  IN  BDS_COMMON_OPTION  *Option,\r
-  IN  EFI_STATUS         Status,\r
-  IN  CHAR16             *ExitData,\r
-  IN  UINTN              ExitDataSize\r
-  )\r
-{\r
-}\r
+  //\r
+  // Register UEFI Shell\r
+  //\r
+  PlatformRegisterFvBootOption (\r
+    &gUefiShellFileGuid, L"EFI Internal Shell", LOAD_OPTION_ACTIVE\r
+    );\r
 \r
-/**\r
-  This function locks platform flash that is not allowed to be updated during normal boot path.\r
-  The flash layout is platform specific.\r
-**/\r
-VOID\r
-EFIAPI\r
-PlatformBdsLockNonUpdatableFlash (\r
-  VOID\r
-  )\r
-{\r
-  return;\r
+  RemoveStaleFvFileOptions ();\r
+  SetBootOrderFromQemu ();\r
 }\r
 \r
 /**\r
@@ -503,4 +718,21 @@ PlatformBootManagerWaitCallback (
   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