Platforms/PlatformBm: add boot from grub

Add the configuration of booting from grub.

Signed-off-by: Haojian Zhuang <haojian.zhuang@linaro.org>
diff --git a/Platforms/Hisilicon/Library/PlatformBootManagerLib/PlatformBm.c b/Platforms/Hisilicon/Library/PlatformBootManagerLib/PlatformBm.c
index 1239c56..3fbc77f 100644
--- a/Platforms/Hisilicon/Library/PlatformBootManagerLib/PlatformBm.c
+++ b/Platforms/Hisilicon/Library/PlatformBootManagerLib/PlatformBm.c
@@ -20,7 +20,10 @@
 #include <Library/PcdLib.h>

 #include <Library/UefiBootManagerLib.h>

 #include <Library/UefiLib.h>

+#include <Protocol/BlockIo.h>

 #include <Protocol/DevicePath.h>

+#include <Protocol/DevicePathFromText.h>

+#include <Protocol/DevicePathToText.h>

 #include <Protocol/GraphicsOutput.h>

 #include <Protocol/LoadedImage.h>

 #include <Guid/EventGroup.h>

@@ -30,6 +33,8 @@
 

 #define DP_NODE_LEN(Type) { (UINT8)sizeof (Type), (UINT8)(sizeof (Type) >> 8) }

 

+#define GRUB_FILE_NAME       L"\\EFI\\BOOT\\GRUBAA64.EFI"

+

 

 #pragma pack (1)

 typedef struct {

@@ -327,25 +332,110 @@
 

 STATIC

 VOID

+PlatformRegisterBootGrub (

+  VOID

+  )

+{

+  EFI_STATUS                           Status;

+  EFI_HANDLE                           Handle;

+  CHAR16                              *BootPathStr;

+  EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL  *EfiDevicePathFromTextProtocol;

+  EFI_DEVICE_PATH                     *DevicePath;

+  EFI_DEVICE_PATH                     *TmpDevicePath;

+  EFI_DEVICE_PATH                     *FileDevicePath;

+  FILEPATH_DEVICE_PATH                *FilePath;

+  UINTN                                Size;

+  EFI_BOOT_MANAGER_LOAD_OPTION         NewOption;

+  EFI_BOOT_MANAGER_LOAD_OPTION        *BootOptions;

+  UINTN                                BootOptionCount;

+  INTN                                 OptionIndex;

+

+  //

+  // Get PcdAndroidBootDevicePath

+  //

+  BootPathStr = (CHAR16 *)PcdGetPtr (PcdAndroidBootDevicePath);

+  ASSERT (BootPathStr != NULL);

+  Status = gBS->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID **)&EfiDevicePathFromTextProtocol);

+  ASSERT_EFI_ERROR(Status);

+  DevicePath = (EFI_DEVICE_PATH *)EfiDevicePathFromTextProtocol->ConvertTextToDevicePath (BootPathStr);

+  ASSERT (DevicePath != NULL);

+  Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &DevicePath, &Handle);

+  if (EFI_ERROR (Status)) {

+    DEBUG ((DEBUG_ERROR, "Failed to locate device path:%s\n", BootPathStr));

+    return;

+  }

+  TmpDevicePath = DevicePathFromHandle (Handle);

+  DevicePath = AppendDevicePathNode (TmpDevicePath, DevicePath);

+  Size = StrSize (GRUB_FILE_NAME);

+  FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + END_DEVICE_PATH_LENGTH);

+  if (FileDevicePath != NULL) {

+    FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;

+    FilePath->Header.Type    = MEDIA_DEVICE_PATH;

+    FilePath->Header.SubType = MEDIA_FILEPATH_DP;

+    CopyMem (&FilePath->PathName, GRUB_FILE_NAME, Size);

+    SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);

+    SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));

+

+    DevicePath = AppendDevicePath (DevicePath, FileDevicePath);

+    FreePool (FileDevicePath);

+  }

+  Status = EfiBootManagerInitializeLoadOption (

+             &NewOption,

+             LoadOptionNumberUnassigned,

+             LoadOptionTypeBoot,

+             LOAD_OPTION_ACTIVE,

+             L"Grub",

+             DevicePath,

+             NULL,

+             0

+             );

+  ASSERT_EFI_ERROR (Status);

+  FreePool (DevicePath);

+

+  BootOptions = EfiBootManagerGetLoadOptions (

+                  &BootOptionCount, LoadOptionTypeBoot

+                  );

+

+  OptionIndex = EfiBootManagerFindLoadOption (

+                  &NewOption, BootOptions, BootOptionCount

+                  );

+

+  if (OptionIndex == -1) {

+    Status = EfiBootManagerAddLoadOptionVariable (&NewOption, MAX_UINTN);

+    ASSERT_EFI_ERROR (Status);

+  }

+

+  EfiBootManagerFreeLoadOption (&NewOption);

+  EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);

+

+}

+

+STATIC

+VOID

 PlatformRegisterOptionsAndKeys (

   VOID

   )

 {

-  EFI_STATUS                   Status;

-  EFI_INPUT_KEY                Enter;

-  EFI_INPUT_KEY                Esc;

-  EFI_INPUT_KEY                KeyF;

-  EFI_BOOT_MANAGER_LOAD_OPTION BootOption;

+  EFI_STATUS                           Status;

+  EFI_INPUT_KEY                        Enter;

+  EFI_INPUT_KEY                        Esc;

+  EFI_INPUT_KEY                        KeyF;

+  EFI_BOOT_MANAGER_LOAD_OPTION         BootOption;

 

   //

-  // Register Android Boot. OptionNumber is 1.

+  // Register Boot. OptionNumber is 1.

+  //

+  PlatformRegisterBootGrub ();

+

+  //

+  // Register Android Boot. OptionNumber is 2.

   //

   PlatformRegisterFvBootOption (

     PcdGetPtr (PcdAndroidBootFile), L"Android Boot", LOAD_OPTION_ACTIVE

     );

 

   //

-  // Register Android Fastboot. OptionNumber is 2.

+  // Register Android Fastboot. OptionNumber is 3.

   //

   PlatformRegisterFvBootOption (

     PcdGetPtr (PcdAndroidFastbootFile), L"Android Fastboot", LOAD_OPTION_ACTIVE

@@ -377,7 +467,7 @@
   KeyF.ScanCode    = SCAN_NULL;

   KeyF.UnicodeChar = 'f';

   Status = EfiBootManagerAddKeyOptionVariable (

-             NULL, 2, 0, &KeyF, NULL

+             NULL, 3, 0, &KeyF, NULL

              );

   ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);

 }

diff --git a/Platforms/Hisilicon/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf b/Platforms/Hisilicon/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
index c015610..ff37bea 100644
--- a/Platforms/Hisilicon/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
+++ b/Platforms/Hisilicon/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
@@ -36,6 +36,7 @@
   QuietBoot.c

 

 [Packages]

+  EmbeddedPkg/EmbeddedPkg.dec

   IntelFrameworkModulePkg/IntelFrameworkModulePkg.dec

   MdeModulePkg/MdeModulePkg.dec

   MdePkg/MdePkg.dec

@@ -80,6 +81,7 @@
   gEfiTtyTermGuid

 

 [Protocols]

+  gEfiDevicePathFromTextProtocolGuid

   gEfiDevicePathProtocolGuid

   gEfiGraphicsOutputProtocolGuid

   gEfiLoadedImageProtocolGuid

@@ -87,3 +89,6 @@
   gEfiPciRootBridgeIoProtocolGuid

   gEfiSimpleFileSystemProtocolGuid

   gEfiDevicePathToTextProtocolGuid

+

+[Pcd]

+  gEmbeddedTokenSpaceGuid.PcdAndroidBootDevicePath