diff mbox series

[edk2,v8,edk-platforms,2/2] Platform/HiKey: create 4 boot options

Message ID 1528091078-20889-3-git-send-email-haojian.zhuang@linaro.org
State New
Headers show
Series add platform boot options | expand

Commit Message

Haojian Zhuang June 4, 2018, 5:44 a.m. UTC
Create 4 predefined boot options for HiKey. They're
"Boot from SD", "Grub", "Android Boot" and "Android Fastboot".

Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Leif Lindholm <leif.lindholm@linaro.org>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Acked-by: Laszlo Ersek <lersek@redhat.com>

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Haojian Zhuang <haojian.zhuang@linaro.org>

---
 Platform/Hisilicon/HiKey/HiKey.dec             |  10 +-
 Platform/Hisilicon/HiKey/HiKey.dsc             |   7 +
 Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.inf |  11 ++
 Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.h   |   2 +
 Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.c   | 164 ++++++++++++++++++++
 5 files changed, 190 insertions(+), 4 deletions(-)

-- 
2.7.4

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
diff mbox series

Patch

diff --git a/Platform/Hisilicon/HiKey/HiKey.dec b/Platform/Hisilicon/HiKey/HiKey.dec
index 537138eb45a1..d5de1a165910 100644
--- a/Platform/Hisilicon/HiKey/HiKey.dec
+++ b/Platform/Hisilicon/HiKey/HiKey.dec
@@ -1,5 +1,5 @@ 
 #
-#  Copyright (c) 2014-2017, Linaro Limited. All rights reserved.
+#  Copyright (c) 2014-2018, Linaro Limited. All rights reserved.
 #
 #  This program and the accompanying materials
 #  are licensed and made available under the terms and conditions of the BSD License
@@ -11,7 +11,7 @@ 
 #
 
 [Defines]
-  DEC_SPECIFICATION              = 0x00010019
+  DEC_SPECIFICATION              = 0x0001001a
   PACKAGE_NAME                   = HiKey
   PACKAGE_GUID                   = d6db414d-ea67-4312-94d5-9c9e5b224d25
   PACKAGE_VERSION                = 0.1
@@ -32,5 +32,7 @@  [Guids.common]
   gHiKeyTokenSpaceGuid          =  { 0x91148425, 0xcdd2, 0x4830, { 0x8b, 0xd0, 0xc6, 0x1c, 0x6d, 0xea, 0x36, 0x21 } }
 
 [PcdsFixedAtBuild.common]
-  gHiKeyTokenSpaceGuid.PcdAndroidFastbootNvmDevicePath|L""|VOID*|0x00000001
-  gHiKeyTokenSpaceGuid.PcdArmFastbootFlashLimit|L""|VOID*|0x00000002
+  gHiKeyTokenSpaceGuid.PcdAndroidBootDevicePath|L""|VOID*|0x00000001
+  gHiKeyTokenSpaceGuid.PcdAndroidBootFile|{ 0x36, 0x8b, 0x73, 0x3a, 0xc5, 0xb9, 0x63, 0x47, 0xab, 0xbd, 0x6c, 0xbd, 0x4b, 0x25, 0xf9, 0xff }|VOID*|0x00000002
+  gHiKeyTokenSpaceGuid.PcdAndroidFastbootFile|{ 0x2a, 0x50, 0x88, 0x95, 0x70, 0x53, 0xe3, 0x11, 0x86, 0x31, 0xd7, 0xc5, 0x95, 0x13, 0x64, 0xc8 }|VOID*|0x00000003
+  gHiKeyTokenSpaceGuid.PcdSdBootDevicePath|L""|VOID*|0x00000004
diff --git a/Platform/Hisilicon/HiKey/HiKey.dsc b/Platform/Hisilicon/HiKey/HiKey.dsc
index 83dd68a820b1..9c1fc2e1b40d 100644
--- a/Platform/Hisilicon/HiKey/HiKey.dsc
+++ b/Platform/Hisilicon/HiKey/HiKey.dsc
@@ -138,6 +138,13 @@  [PcdsFixedAtBuild.common]
   gEmbeddedTokenSpaceGuid.PcdAndroidFastbootUsbVendorId|0x18d1
   gEmbeddedTokenSpaceGuid.PcdAndroidFastbootUsbProductId|0xd00d
 
+  #
+  # Android Loader
+  #
+  gHiKeyTokenSpaceGuid.PcdAndroidBootDevicePath|L"VenHw(0D51905B-B77E-452A-A2C0-ECA0CC8D514A,00D023F70000000000)/eMMC(0x0)/Ctrl(0x0)/HD(6,GPT,5C0F213C-17E1-4149-88C8-8B50FB4EC70E,0x7000,0x20000)/\\EFI\\BOOT\\GRUBAA64.EFI"
+  gHiKeyTokenSpaceGuid.PcdSdBootDevicePath|L"VenHw(0D51905B-B77E-452A-A2C0-ECA0CC8D514A,00E023F70000000000)/SD(0x0)"
+
+
 ################################################################################
 #
 # Components Section - list of all EDK II Modules needed by this Platform
diff --git a/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.inf b/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.inf
index 41aa7f8081ed..4c4644a0fe51 100644
--- a/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.inf
+++ b/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.inf
@@ -26,18 +26,29 @@  [Packages]
   EmbeddedPkg/EmbeddedPkg.dec
   MdeModulePkg/MdeModulePkg.dec
   MdePkg/MdePkg.dec
+  Platform/Hisilicon/HiKey/HiKey.dec
 
 [LibraryClasses]
   CacheMaintenanceLib
   DebugLib
   IoLib
+  UefiBootManagerLib
   UefiLib
   UefiDriverEntryPoint
 
 [Protocols]
+  gEfiDevicePathFromTextProtocolGuid
+  gEfiLoadedImageProtocolGuid
   gEmbeddedGpioProtocolGuid
+  gPlatformBootManagerProtocolGuid
   gPlatformVirtualKeyboardProtocolGuid
 
+[Pcd]
+  gHiKeyTokenSpaceGuid.PcdAndroidBootDevicePath
+  gHiKeyTokenSpaceGuid.PcdAndroidBootFile
+  gHiKeyTokenSpaceGuid.PcdAndroidFastbootFile
+  gHiKeyTokenSpaceGuid.PcdSdBootDevicePath
+
 [Guids]
   gEfiEndOfDxeEventGroupGuid
 
diff --git a/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.h b/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.h
index 104048135f27..0ababfa32eab 100644
--- a/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.h
+++ b/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.h
@@ -21,4 +21,6 @@ 
 #define ADB_REBOOT_BOOTLOADER            0x77665500
 #define ADB_REBOOT_NONE                  0x77665501
 
+#define HIKEY_BOOT_OPTION_NUM            4
+
 #endif /* __HIKEYDXE_H__ */
diff --git a/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.c b/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.c
index 832615f9cc80..2162b68529bd 100644
--- a/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.c
+++ b/Platform/Hisilicon/HiKey/HiKeyDxe/HiKeyDxe.c
@@ -14,11 +14,19 @@ 
 
 #include <Library/CacheMaintenanceLib.h>
 #include <Library/DebugLib.h>
+#include <Library/DevicePathLib.h>
 #include <Library/IoLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/PcdLib.h>
+#include <Library/UefiBootManagerLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/UefiLib.h>
 
+#include <Protocol/DevicePath.h>
+#include <Protocol/DevicePathFromText.h>
 #include <Protocol/EmbeddedGpio.h>
+#include <Protocol/LoadedImage.h>
+#include <Protocol/PlatformBootManager.h>
 #include <Protocol/PlatformVirtualKeyboard.h>
 
 #include <Hi6220.h>
@@ -99,6 +107,152 @@  HiKeyInitPeripherals (
   return EFI_SUCCESS;
 }
 
+STATIC
+EFI_STATUS
+CreatePlatformBootOptionFromPath (
+  IN     CHAR16                          *PathStr,
+  IN     CHAR16                          *Description,
+  IN OUT EFI_BOOT_MANAGER_LOAD_OPTION    *BootOption
+  )
+{
+  EFI_STATUS                   Status;
+  EFI_DEVICE_PATH              *DevicePath;
+
+  DevicePath = (EFI_DEVICE_PATH *)ConvertTextToDevicePath (PathStr);
+  ASSERT (DevicePath != NULL);
+  Status = EfiBootManagerInitializeLoadOption (
+             BootOption,
+             LoadOptionNumberUnassigned,
+             LoadOptionTypeBoot,
+             LOAD_OPTION_ACTIVE,
+             Description,
+             DevicePath,
+             NULL,
+             0
+             );
+  FreePool (DevicePath);
+  return Status;
+}
+
+STATIC
+EFI_STATUS
+CreatePlatformBootOptionFromGuid (
+  IN     EFI_GUID                        *FileGuid,
+  IN     CHAR16                          *Description,
+  IN OUT EFI_BOOT_MANAGER_LOAD_OPTION    *BootOption
+  )
+{
+  EFI_STATUS                             Status;
+  EFI_DEVICE_PATH                        *DevicePath;
+  EFI_DEVICE_PATH                        *TempDevicePath;
+  EFI_LOADED_IMAGE_PROTOCOL              *LoadedImage;
+  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH      FileNode;
+
+  Status = gBS->HandleProtocol (
+                  gImageHandle,
+                  &gEfiLoadedImageProtocolGuid,
+                  (VOID **) &LoadedImage
+                  );
+  ASSERT_EFI_ERROR (Status);
+  EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);
+  TempDevicePath = DevicePathFromHandle (LoadedImage->DeviceHandle);
+  ASSERT (TempDevicePath != NULL);
+  DevicePath = AppendDevicePathNode (
+                 TempDevicePath,
+                 (EFI_DEVICE_PATH_PROTOCOL *) &FileNode
+                 );
+  ASSERT (DevicePath != NULL);
+  Status = EfiBootManagerInitializeLoadOption (
+             BootOption,
+             LoadOptionNumberUnassigned,
+             LoadOptionTypeBoot,
+             LOAD_OPTION_ACTIVE,
+             Description,
+             DevicePath,
+             NULL,
+             0
+             );
+  FreePool (DevicePath);
+  return Status;
+}
+
+STATIC
+EFI_STATUS
+GetPlatformBootOptionsAndKeys (
+  OUT UINTN                              *BootCount,
+  OUT EFI_BOOT_MANAGER_LOAD_OPTION       **BootOptions,
+  OUT EFI_INPUT_KEY                      **BootKeys
+  )
+{
+  EFI_GUID                               *FileGuid;
+  CHAR16                                 *PathStr;
+  EFI_STATUS                             Status;
+  UINTN                                  Size;
+
+  Size = sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * HIKEY_BOOT_OPTION_NUM;
+  *BootOptions = (EFI_BOOT_MANAGER_LOAD_OPTION *)AllocateZeroPool (Size);
+  if (*BootOptions == NULL) {
+    DEBUG ((DEBUG_ERROR, "Failed to allocate memory for BootOptions\n"));
+    return EFI_OUT_OF_RESOURCES;
+  }
+  Size = sizeof (EFI_INPUT_KEY) * HIKEY_BOOT_OPTION_NUM;
+  *BootKeys = (EFI_INPUT_KEY *)AllocateZeroPool (Size);
+  if (*BootKeys == NULL) {
+    DEBUG ((DEBUG_ERROR, "Failed to allocate memory for BootKeys\n"));
+    Status = EFI_OUT_OF_RESOURCES;
+    goto Error;
+  }
+
+  PathStr = (CHAR16 *)PcdGetPtr (PcdSdBootDevicePath);
+  ASSERT (PathStr != NULL);
+  Status = CreatePlatformBootOptionFromPath (
+             PathStr,
+             L"Boot from SD",
+             &(*BootOptions)[0]
+             );
+  ASSERT_EFI_ERROR (Status);
+
+  PathStr = (CHAR16 *)PcdGetPtr (PcdAndroidBootDevicePath);
+  ASSERT (PathStr != NULL);
+  Status = CreatePlatformBootOptionFromPath (
+             PathStr,
+             L"Grub",
+             &(*BootOptions)[1]
+             );
+  ASSERT_EFI_ERROR (Status);
+
+  FileGuid = PcdGetPtr (PcdAndroidBootFile);
+  ASSERT (FileGuid != NULL);
+  Status = CreatePlatformBootOptionFromGuid (
+             FileGuid,
+             L"Android Boot",
+             &(*BootOptions)[2]
+             );
+  ASSERT_EFI_ERROR (Status);
+
+  FileGuid = PcdGetPtr (PcdAndroidFastbootFile);
+  ASSERT (FileGuid != NULL);
+  Status = CreatePlatformBootOptionFromGuid (
+             FileGuid,
+             L"Android Fastboot",
+             &(*BootOptions)[3]
+             );
+  ASSERT_EFI_ERROR (Status);
+  (*BootKeys)[3].ScanCode = SCAN_NULL;
+  (*BootKeys)[3].UnicodeChar = 'f';
+
+  *BootCount = 4;
+
+  return EFI_SUCCESS;
+Error:
+  FreePool (*BootOptions);
+  return Status;
+}
+
+PLATFORM_BOOT_MANAGER_PROTOCOL mPlatformBootManager = {
+  GetPlatformBootOptionsAndKeys
+};
+
 EFI_STATUS
 EFIAPI
 VirtualKeyboardRegister (
@@ -203,5 +357,15 @@  HiKeyEntryPoint (
                   EFI_NATIVE_INTERFACE,
                   &mVirtualKeyboard
                   );
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  Status = gBS->InstallProtocolInterface (
+                  &ImageHandle,
+                  &gPlatformBootManagerProtocolGuid,
+                  EFI_NATIVE_INTERFACE,
+                  &mPlatformBootManager
+                  );
   return Status;
 }