]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPlatformPkg/Bds/BootMenu.c
ArmPlatformPkg/Bds: Fix setting kernel command line
[mirror_edk2.git] / ArmPlatformPkg / Bds / BootMenu.c
index a2360d14f5e50c34b33d96df9afc44a04f77c975..62e7927defd6cc8eb291667f57b192520e619519 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
 *\r
-*  Copyright (c) 2011, ARM Limited. All rights reserved.\r
+*  Copyright (c) 2011 - 2014, ARM Limited. All rights reserved.\r
 *\r
 *  This program and the accompanying materials\r
 *  are licensed and made available under the terms and conditions of the BSD License\r
 \r
 #include "BdsInternal.h"\r
 \r
+#include <Guid/ArmGlobalVariableHob.h>\r
+\r
 extern EFI_HANDLE mImageHandle;\r
 extern BDS_LOAD_OPTION_SUPPORT *BdsLoadOptionSupportList;\r
 \r
+\r
 EFI_STATUS\r
-BootMenuAddBootOption (\r
-  IN LIST_ENTRY *BootOptionsList\r
+SelectBootDevice (\r
+  OUT BDS_SUPPORTED_DEVICE** SupportedBootDevice\r
   )\r
 {\r
-  EFI_STATUS    Status;\r
+  EFI_STATUS  Status;\r
   LIST_ENTRY  SupportedDeviceList;\r
   UINTN       SupportedDeviceCount;\r
-  BDS_SUPPORTED_DEVICE* SupportedBootDevice;\r
   LIST_ENTRY* Entry;\r
   UINTN       SupportedDeviceSelected;\r
-  CHAR8       AsciiBootOption[BOOT_DEVICE_OPTION_MAX];\r
-  CHAR8       AsciiBootDescription[BOOT_DEVICE_DESCRIPTION_MAX];\r
-  CHAR16      *BootDescription;\r
-  UINT32      Attributes;\r
-  BDS_LOADER_TYPE   BootType;\r
   UINTN       Index;\r
-  BDS_LOAD_OPTION *BdsLoadOption;\r
-  EFI_DEVICE_PATH*  DevicePath;\r
-  EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
-\r
-  Attributes = 0;\r
 \r
   //\r
   // List the Boot Devices supported\r
@@ -57,16 +49,16 @@ BootMenuAddBootOption (
        Entry = GetNextNode (&SupportedDeviceList,Entry)\r
        )\r
   {\r
-    SupportedBootDevice = SUPPORTED_BOOT_DEVICE_FROM_LINK(Entry);\r
-    Print(L"[%d] %s\n",SupportedDeviceCount+1,SupportedBootDevice->Description);\r
+    *SupportedBootDevice = SUPPORTED_BOOT_DEVICE_FROM_LINK(Entry);\r
+    Print(L"[%d] %s\n",SupportedDeviceCount+1,(*SupportedBootDevice)->Description);\r
 \r
     DEBUG_CODE_BEGIN();\r
       CHAR16*                           DevicePathTxt;\r
       EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;\r
 \r
-      Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);\r
+      Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);\r
       ASSERT_EFI_ERROR(Status);\r
-      DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText(SupportedBootDevice->DevicePathProtocol,TRUE,TRUE);\r
+      DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText ((*SupportedBootDevice)->DevicePathProtocol,TRUE,TRUE);\r
 \r
       Print(L"\t- %s\n",DevicePathTxt);\r
 \r
@@ -93,7 +85,7 @@ BootMenuAddBootOption (
       Status = EFI_ABORTED;\r
       goto EXIT;\r
     } else if ((SupportedDeviceSelected == 0) || (SupportedDeviceSelected > SupportedDeviceCount)) {\r
-      Print(L"Invalid input (max %d)\n",SupportedDeviceSelected);\r
+      Print(L"Invalid input (max %d)\n",SupportedDeviceCount);\r
       SupportedDeviceSelected = 0;\r
     }\r
   }\r
@@ -108,91 +100,214 @@ BootMenuAddBootOption (
        )\r
   {\r
     if (Index == SupportedDeviceSelected) {\r
-      SupportedBootDevice = SUPPORTED_BOOT_DEVICE_FROM_LINK(Entry);\r
+      *SupportedBootDevice = SUPPORTED_BOOT_DEVICE_FROM_LINK(Entry);\r
       break;\r
     }\r
     Index++;\r
   }\r
 \r
-  // Create the specific device path node\r
-  Status = SupportedBootDevice->Support->CreateDevicePathNode (SupportedBootDevice, &DevicePathNode, &BootType, &Attributes);\r
+EXIT:\r
+  BootDeviceListSupportedFree (&SupportedDeviceList, *SupportedBootDevice);\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+BootMenuAddBootOption (\r
+  IN LIST_ENTRY *BootOptionsList\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  BDS_SUPPORTED_DEVICE*     SupportedBootDevice;\r
+  ARM_BDS_LOADER_ARGUMENTS* BootArguments;\r
+  CHAR16                    BootDescription[BOOT_DEVICE_DESCRIPTION_MAX];\r
+  CHAR8                     AsciiCmdLine[BOOT_DEVICE_OPTION_MAX];\r
+  CHAR16                    CmdLine[BOOT_DEVICE_OPTION_MAX];\r
+  UINT32                    Attributes;\r
+  ARM_BDS_LOADER_TYPE       BootType;\r
+  BDS_LOAD_OPTION_ENTRY     *BdsLoadOptionEntry;\r
+  EFI_DEVICE_PATH           *DevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePathNodes;\r
+  EFI_DEVICE_PATH_PROTOCOL  *InitrdPathNodes;\r
+  EFI_DEVICE_PATH_PROTOCOL  *InitrdPath;\r
+  UINTN                     CmdLineSize;\r
+  BOOLEAN                   InitrdSupport;\r
+  UINTN                     InitrdSize;\r
+  UINT8*                    OptionalData;\r
+  UINTN                     OptionalDataSize;\r
+  BOOLEAN                   RequestBootType;\r
+\r
+  Attributes                = 0;\r
+  SupportedBootDevice = NULL;\r
+\r
+  // List the Boot Devices supported\r
+  Status = SelectBootDevice (&SupportedBootDevice);\r
   if (EFI_ERROR(Status)) {\r
     Status = EFI_ABORTED;\r
     goto EXIT;\r
   }\r
-  // Append the Device Path node to the select device path\r
-  DevicePath = AppendDevicePathNode (SupportedBootDevice->DevicePathProtocol, (CONST EFI_DEVICE_PATH_PROTOCOL *)DevicePathNode);\r
 \r
-  Print(L"Arguments to pass to the binary: ");\r
-  Status = GetHIInputAscii (AsciiBootOption,BOOT_DEVICE_OPTION_MAX);\r
+  // Create the specific device path node\r
+  RequestBootType = TRUE;\r
+  Status = SupportedBootDevice->Support->CreateDevicePathNode (L"EFI Application or the kernel", &DevicePathNodes, &RequestBootType);\r
   if (EFI_ERROR(Status)) {\r
     Status = EFI_ABORTED;\r
-    goto FREE_DEVICE_PATH;\r
+    goto EXIT;\r
+  }\r
+  // Append the Device Path to the selected device path\r
+  DevicePath = AppendDevicePath (SupportedBootDevice->DevicePathProtocol, (CONST EFI_DEVICE_PATH_PROTOCOL *)DevicePathNodes);\r
+  if (DevicePath == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto EXIT;\r
+  }\r
+\r
+  if (RequestBootType) {\r
+    Status = BootDeviceGetType (DevicePath, &BootType, &Attributes);\r
+    if (EFI_ERROR(Status)) {\r
+      Status = EFI_ABORTED;\r
+      goto EXIT;\r
+    }\r
+  } else {\r
+    BootType = BDS_LOADER_EFI_APPLICATION;\r
+  }\r
+\r
+  if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {\r
+    Print(L"Add an initrd: ");\r
+    Status = GetHIInputBoolean (&InitrdSupport);\r
+    if (EFI_ERROR(Status)) {\r
+      Status = EFI_ABORTED;\r
+      goto EXIT;\r
+    }\r
+\r
+    if (InitrdSupport) {\r
+      // Create the specific device path node\r
+      Status = SupportedBootDevice->Support->CreateDevicePathNode (L"initrd", &InitrdPathNodes, NULL);\r
+      if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) { // EFI_NOT_FOUND is returned on empty input string, but we can boot without an initrd\r
+        Status = EFI_ABORTED;\r
+        goto EXIT;\r
+      }\r
+\r
+      if (InitrdPathNodes != NULL) {\r
+        // Append the Device Path to the selected device path\r
+        InitrdPath = AppendDevicePath (SupportedBootDevice->DevicePathProtocol, (CONST EFI_DEVICE_PATH_PROTOCOL *)InitrdPathNodes);\r
+        if (InitrdPath == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          goto EXIT;\r
+        }\r
+      } else {\r
+        InitrdPath = NULL;\r
+      }\r
+    } else {\r
+      InitrdPath = NULL;\r
+    }\r
+\r
+    Print(L"Arguments to pass to the binary: ");\r
+    Status = GetHIInputAscii (AsciiCmdLine, BOOT_DEVICE_OPTION_MAX);\r
+    if (EFI_ERROR(Status)) {\r
+      Status = EFI_ABORTED;\r
+      goto FREE_DEVICE_PATH;\r
+    }\r
+\r
+    CmdLineSize = AsciiStrSize (AsciiCmdLine);\r
+    InitrdSize = GetDevicePathSize (InitrdPath);\r
+\r
+    OptionalDataSize = sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize;\r
+    BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool (OptionalDataSize);\r
+\r
+    BootArguments->LinuxArguments.CmdLineSize = CmdLineSize;\r
+    BootArguments->LinuxArguments.InitrdSize = InitrdSize;\r
+    CopyMem ((VOID*)(&BootArguments->LinuxArguments + 1), AsciiCmdLine, CmdLineSize);\r
+    CopyMem ((VOID*)((UINTN)(&BootArguments->LinuxArguments + 1) + CmdLineSize), InitrdPath, InitrdSize);\r
+\r
+    OptionalData = (UINT8*)BootArguments;\r
+  } else {\r
+    Print (L"Arguments to pass to the EFI Application: ");\r
+    Status = GetHIInputStr (CmdLine, BOOT_DEVICE_OPTION_MAX);\r
+    if (EFI_ERROR (Status)) {\r
+      Status = EFI_ABORTED;\r
+      goto EXIT;\r
+    }\r
+\r
+    OptionalData = (UINT8*)CmdLine;\r
+    OptionalDataSize = StrSize (CmdLine);\r
   }\r
 \r
   Print(L"Description for this new Entry: ");\r
-  Status = GetHIInputAscii (AsciiBootDescription,BOOT_DEVICE_DESCRIPTION_MAX);\r
+  Status = GetHIInputStr (BootDescription, BOOT_DEVICE_DESCRIPTION_MAX);\r
   if (EFI_ERROR(Status)) {\r
     Status = EFI_ABORTED;\r
     goto FREE_DEVICE_PATH;\r
   }\r
 \r
-  // Convert Ascii into Unicode\r
-  BootDescription = (CHAR16*)AllocatePool(AsciiStrSize(AsciiBootDescription) * sizeof(CHAR16));\r
-  AsciiStrToUnicodeStr (AsciiBootDescription, BootDescription);\r
-\r
   // Create new entry\r
-  Status = BootOptionCreate (Attributes, BootDescription, DevicePath, BootType, AsciiBootOption, &BdsLoadOption);\r
+  BdsLoadOptionEntry = (BDS_LOAD_OPTION_ENTRY*)AllocatePool (sizeof(BDS_LOAD_OPTION_ENTRY));\r
+  Status = BootOptionCreate (Attributes, BootDescription, DevicePath, BootType, OptionalData, OptionalDataSize, &BdsLoadOptionEntry->BdsLoadOption);\r
   if (!EFI_ERROR(Status)) {\r
-    InsertTailList (BootOptionsList,&BdsLoadOption->Link);\r
+    InsertTailList (BootOptionsList, &BdsLoadOptionEntry->Link);\r
   }\r
 \r
-  FreePool (BootDescription);\r
-\r
 FREE_DEVICE_PATH:\r
   FreePool (DevicePath);\r
 \r
 EXIT:\r
-  BootDeviceListSupportedFree (&SupportedDeviceList);\r
+  if (Status == EFI_ABORTED) {\r
+    Print(L"\n");\r
+  }\r
+  FreePool(SupportedBootDevice);\r
   return Status;\r
 }\r
 \r
 STATIC\r
 EFI_STATUS\r
 BootMenuSelectBootOption (\r
-  IN  LIST_ENTRY *BootOptionsList,\r
-  IN  CONST CHAR16* InputStatement,\r
-  OUT BDS_LOAD_OPTION **BdsLoadOption\r
+  IN  LIST_ENTRY*               BootOptionsList,\r
+  IN  CONST CHAR16*             InputStatement,\r
+  OUT BDS_LOAD_OPTION_ENTRY**   BdsLoadOptionEntry\r
   )\r
 {\r
-  EFI_STATUS    Status;\r
-  LIST_ENTRY*   Entry;\r
-  BDS_LOAD_OPTION *BootOption;\r
-  UINTN         BootOptionSelected;\r
-  UINTN         BootOptionCount;\r
-  UINTN         Index;\r
+  EFI_STATUS                    Status;\r
+  LIST_ENTRY*                   Entry;\r
+  BDS_LOAD_OPTION*              BdsLoadOption;\r
+  UINTN                         BootOptionSelected;\r
+  UINTN                         BootOptionCount;\r
+  UINTN                         Index;\r
+  BOOLEAN                       IsUnicode;\r
 \r
   // Display the list of supported boot devices\r
-  BootOptionCount = 1;\r
+  BootOptionCount = 0;\r
   for (Entry = GetFirstNode (BootOptionsList);\r
        !IsNull (BootOptionsList,Entry);\r
-       Entry = GetNextNode (BootOptionsList,Entry)\r
+       Entry = GetNextNode (BootOptionsList, Entry)\r
        )\r
   {\r
-    BootOption = LOAD_OPTION_FROM_LINK(Entry);\r
-    Print(L"[%d] %s\n",BootOptionCount,BootOption->Description);\r
+    BdsLoadOption = LOAD_OPTION_FROM_LINK(Entry);\r
+\r
+    Print (L"[%d] %s\n", (BootOptionCount + 1), BdsLoadOption->Description);\r
 \r
     DEBUG_CODE_BEGIN();\r
       CHAR16*                           DevicePathTxt;\r
       EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;\r
+      ARM_BDS_LOADER_TYPE               LoaderType;\r
+      ARM_BDS_LOADER_OPTIONAL_DATA*     OptionalData;\r
 \r
       Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);\r
       ASSERT_EFI_ERROR(Status);\r
-      DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText(BootOption->FilePathList,TRUE,TRUE);\r
+      DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText(BdsLoadOption->FilePathList,TRUE,TRUE);\r
 \r
       Print(L"\t- %s\n",DevicePathTxt);\r
-      if ((BootOption->OptionalData != NULL) && (BootOption->OptionalData->Arguments != NULL)) {\r
-        Print(L"\t- Arguments: %a\n",BootOption->OptionalData->Arguments);\r
+      OptionalData = BdsLoadOption->OptionalData;\r
+      if (IS_ARM_BDS_BOOTENTRY (BdsLoadOption)) {\r
+        LoaderType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((CONST UINT32*)&OptionalData->Header.LoaderType);\r
+        if ((LoaderType == BDS_LOADER_KERNEL_LINUX_ATAG) || (LoaderType == BDS_LOADER_KERNEL_LINUX_FDT)) {\r
+          Print (L"\t- Arguments: %a\n",&OptionalData->Arguments.LinuxArguments + 1);\r
+        }\r
+      } else if (OptionalData != NULL) {\r
+        if (IsPrintableString (OptionalData, &IsUnicode)) {\r
+          if (IsUnicode) {\r
+            Print (L"\t- Arguments: %s\n", OptionalData);\r
+          } else {\r
+            AsciiPrint ("\t- Arguments: %a\n", OptionalData);\r
+          }\r
+        }\r
       }\r
 \r
       FreePool(DevicePathTxt);\r
@@ -201,6 +316,19 @@ BootMenuSelectBootOption (
     BootOptionCount++;\r
   }\r
 \r
+  // Check if a valid boot option(s) is found\r
+  if (BootOptionCount == 0) {\r
+    if (StrCmp (InputStatement, DELETE_BOOT_ENTRY) == 0) {\r
+      Print (L"Nothing to remove!\n");\r
+    } else if (StrCmp (InputStatement, UPDATE_BOOT_ENTRY) == 0) {\r
+      Print (L"Couldn't find valid boot entries\n");\r
+    } else{\r
+      Print (L"No supported Boot Entry.\n");\r
+    }\r
+\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
   // Get the index of the boot device to delete\r
   BootOptionSelected = 0;\r
   while (BootOptionSelected == 0) {\r
@@ -208,7 +336,7 @@ BootMenuSelectBootOption (
     Status = GetHIInputInteger (&BootOptionSelected);\r
     if (EFI_ERROR(Status)) {\r
       return Status;\r
-    } else if ((BootOptionSelected == 0) || (BootOptionSelected >= BootOptionCount)) {\r
+    } else if ((BootOptionSelected == 0) || (BootOptionSelected > BootOptionCount)) {\r
       Print(L"Invalid input (max %d)\n",BootOptionCount);\r
       BootOptionSelected = 0;\r
     }\r
@@ -217,12 +345,12 @@ BootMenuSelectBootOption (
   // Get the structure of the Boot device to delete\r
   Index = 1;\r
   for (Entry = GetFirstNode (BootOptionsList);\r
-       !IsNull (BootOptionsList,Entry);\r
+       !IsNull (BootOptionsList, Entry);\r
        Entry = GetNextNode (BootOptionsList,Entry)\r
        )\r
   {\r
     if (Index == BootOptionSelected) {\r
-      *BdsLoadOption = LOAD_OPTION_FROM_LINK(Entry);\r
+      *BdsLoadOptionEntry = LOAD_OPTION_ENTRY_FROM_LINK(Entry);\r
       break;\r
     }\r
     Index++;\r
@@ -236,16 +364,22 @@ BootMenuRemoveBootOption (
   IN LIST_ENTRY *BootOptionsList\r
   )\r
 {\r
-  EFI_STATUS    Status;\r
-  BDS_LOAD_OPTION *BootOption;\r
+  EFI_STATUS                    Status;\r
+  BDS_LOAD_OPTION_ENTRY*        BootOptionEntry;\r
 \r
-  Status = BootMenuSelectBootOption (BootOptionsList,L"Delete entry: ",&BootOption);\r
+  Status = BootMenuSelectBootOption (BootOptionsList, DELETE_BOOT_ENTRY, &BootOptionEntry);\r
   if (EFI_ERROR(Status)) {\r
     return Status;\r
   }\r
 \r
+  // If the Boot Option was attached to a list remove it\r
+  if (!IsListEmpty (&BootOptionEntry->Link)) {\r
+    // Remove the entry from the list\r
+    RemoveEntryList (&BootOptionEntry->Link);\r
+  }\r
+\r
   // Delete the BDS Load option structures\r
-  BootOptionDelete (BootOption);\r
+  BootOptionDelete (BootOptionEntry->BdsLoadOption);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -255,67 +389,262 @@ BootMenuUpdateBootOption (
   IN LIST_ENTRY *BootOptionsList\r
   )\r
 {\r
-  EFI_STATUS    Status;\r
-  BDS_LOAD_OPTION *BootOption;\r
-  BDS_LOAD_OPTION_SUPPORT*  DeviceSupport;\r
-  CHAR8       AsciiBootOption[BOOT_DEVICE_OPTION_MAX];\r
-  CHAR8       AsciiBootDescription[BOOT_DEVICE_DESCRIPTION_MAX];\r
-  CHAR16      *BootDescription;\r
-  EFI_DEVICE_PATH* DevicePath;\r
-  UINT32      Attributes;\r
-  BDS_LOADER_TYPE   BootType;\r
-\r
-  Status = BootMenuSelectBootOption (BootOptionsList,L"Update entry: ",&BootOption);\r
+  EFI_STATUS                    Status;\r
+  BDS_LOAD_OPTION_ENTRY         *BootOptionEntry;\r
+  BDS_LOAD_OPTION               *BootOption;\r
+  BDS_LOAD_OPTION_SUPPORT*      DeviceSupport;\r
+  ARM_BDS_LOADER_ARGUMENTS*     BootArguments;\r
+  CHAR16                        BootDescription[BOOT_DEVICE_DESCRIPTION_MAX];\r
+  CHAR8                         CmdLine[BOOT_DEVICE_OPTION_MAX];\r
+  CHAR16                        UnicodeCmdLine[BOOT_DEVICE_OPTION_MAX];\r
+  EFI_DEVICE_PATH               *DevicePath;\r
+  EFI_DEVICE_PATH               *TempInitrdPath;\r
+  ARM_BDS_LOADER_TYPE           BootType;\r
+  ARM_BDS_LOADER_OPTIONAL_DATA* LoaderOptionalData;\r
+  ARM_BDS_LINUX_ARGUMENTS*      LinuxArguments;\r
+  EFI_DEVICE_PATH               *InitrdPathNodes;\r
+  EFI_DEVICE_PATH               *InitrdPath;\r
+  UINTN                         InitrdSize;\r
+  UINTN                         CmdLineSize;\r
+  BOOLEAN                       InitrdSupport;\r
+  UINT8*                        OptionalData;\r
+  UINTN                         OptionalDataSize;\r
+  BOOLEAN                       RequestBootType;\r
+  BOOLEAN                       IsPrintable;\r
+  BOOLEAN                       IsUnicode;\r
+\r
+  Status = BootMenuSelectBootOption (BootOptionsList, UPDATE_BOOT_ENTRY, &BootOptionEntry);\r
   if (EFI_ERROR(Status)) {\r
     return Status;\r
   }\r
+  BootOption = BootOptionEntry->BdsLoadOption;\r
 \r
   // Get the device support for this Boot Option\r
-  Status = BootDeviceGetDeviceSupport (BootOption,&DeviceSupport);\r
+  Status = BootDeviceGetDeviceSupport (BootOption->FilePathList, &DeviceSupport);\r
   if (EFI_ERROR(Status)) {\r
-    Print(L"Impossible to retrieve the supported device for the update\n");\r
+    Print(L"Not possible to retrieve the supported device for the update\n");\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  Status = DeviceSupport->UpdateDevicePathNode (BootOption,&DevicePath,&BootType,&Attributes);\r
+  RequestBootType = TRUE;\r
+  Status = DeviceSupport->UpdateDevicePathNode (BootOption->FilePathList, L"EFI Application or the kernel", &DevicePath, &RequestBootType);\r
   if (EFI_ERROR(Status)) {\r
     Status = EFI_ABORTED;\r
     goto EXIT;\r
   }\r
 \r
-  Print(L"Arguments to pass to the binary: ");\r
-  if (BootOption->OptionalData) {\r
-    AsciiStrnCpy(AsciiBootOption,BootOption->OptionalData->Arguments,BOOT_DEVICE_FILEPATH_MAX);\r
+  if (RequestBootType) {\r
+    Status = BootDeviceGetType (DevicePath, &BootType, &BootOption->Attributes);\r
+    if (EFI_ERROR(Status)) {\r
+      Status = EFI_ABORTED;\r
+      goto EXIT;\r
+    }\r
+  }\r
+\r
+  LoaderOptionalData = BootOption->OptionalData;\r
+  if (LoaderOptionalData != NULL) {\r
+    BootType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((UINT32 *)(&LoaderOptionalData->Header.LoaderType));\r
   } else {\r
-    AsciiBootOption[0] = '\0';\r
+    BootType = BDS_LOADER_EFI_APPLICATION;\r
   }\r
-  Status = EditHIInputAscii (AsciiBootOption,BOOT_DEVICE_OPTION_MAX);\r
-  if (EFI_ERROR(Status)) {\r
-    Status = EFI_ABORTED;\r
-    goto FREE_DEVICE_PATH;\r
+\r
+  if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {\r
+    LinuxArguments = &LoaderOptionalData->Arguments.LinuxArguments;\r
+\r
+    CmdLineSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->CmdLineSize);\r
+\r
+    InitrdSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->InitrdSize);\r
+    if (InitrdSize > 0) {\r
+      Print(L"Keep the initrd: ");\r
+    } else {\r
+      Print(L"Add an initrd: ");\r
+    }\r
+    Status = GetHIInputBoolean (&InitrdSupport);\r
+    if (EFI_ERROR(Status)) {\r
+      Status = EFI_ABORTED;\r
+      goto EXIT;\r
+    }\r
+\r
+    if (InitrdSupport) {\r
+      if (InitrdSize > 0) {\r
+        // Case we update the initrd device path\r
+        Status = DeviceSupport->UpdateDevicePathNode ((EFI_DEVICE_PATH*)((UINTN)(LinuxArguments + 1) + CmdLineSize), L"initrd", &InitrdPath, NULL);\r
+        if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) {// EFI_NOT_FOUND is returned on empty input string, but we can boot without an initrd\r
+          Status = EFI_ABORTED;\r
+          goto EXIT;\r
+        }\r
+        InitrdSize = GetDevicePathSize (InitrdPath);\r
+      } else {\r
+        // Case we create the initrd device path\r
+\r
+        Status = DeviceSupport->CreateDevicePathNode (L"initrd", &InitrdPathNodes, NULL);\r
+        if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) { // EFI_NOT_FOUND is returned on empty input string, but we can boot without an initrd\r
+          Status = EFI_ABORTED;\r
+          goto EXIT;\r
+        }\r
+\r
+        if (InitrdPathNodes != NULL) {\r
+          // Duplicate Linux kernel Device Path\r
+          TempInitrdPath = DuplicateDevicePath (BootOption->FilePathList);\r
+          // Replace Linux kernel Node by EndNode\r
+          SetDevicePathEndNode (GetLastDevicePathNode (TempInitrdPath));\r
+          // Append the Device Path to the selected device path\r
+          InitrdPath = AppendDevicePath (TempInitrdPath, (CONST EFI_DEVICE_PATH_PROTOCOL *)InitrdPathNodes);\r
+          FreePool (TempInitrdPath);\r
+          if (InitrdPath == NULL) {\r
+            Status = EFI_OUT_OF_RESOURCES;\r
+            goto EXIT;\r
+          }\r
+          InitrdSize = GetDevicePathSize (InitrdPath);\r
+        } else {\r
+          InitrdPath = NULL;\r
+        }\r
+      }\r
+    } else {\r
+      InitrdSize = 0;\r
+    }\r
+\r
+    Print(L"Arguments to pass to the binary: ");\r
+    if (CmdLineSize > 0) {\r
+      AsciiStrnCpy(CmdLine, (CONST CHAR8*)(LinuxArguments + 1), CmdLineSize);\r
+    } else {\r
+      CmdLine[0] = '\0';\r
+    }\r
+    Status = EditHIInputAscii (CmdLine, BOOT_DEVICE_OPTION_MAX);\r
+    if (EFI_ERROR(Status)) {\r
+      Status = EFI_ABORTED;\r
+      goto FREE_DEVICE_PATH;\r
+    }\r
+\r
+    CmdLineSize = AsciiStrSize (CmdLine);\r
+\r
+    OptionalDataSize = sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize;\r
+    BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool (OptionalDataSize);\r
+    BootArguments->LinuxArguments.CmdLineSize = CmdLineSize;\r
+    BootArguments->LinuxArguments.InitrdSize = InitrdSize;\r
+    CopyMem (&BootArguments->LinuxArguments + 1, CmdLine, CmdLineSize);\r
+    CopyMem ((VOID*)((UINTN)(&BootArguments->LinuxArguments + 1) + CmdLineSize), InitrdPath, InitrdSize);\r
+\r
+    OptionalData = (UINT8*)BootArguments;\r
+  } else {\r
+    Print (L"Arguments to pass to the EFI Application: ");\r
+\r
+    if (BootOption->OptionalDataSize > 0) {\r
+      IsPrintable = IsPrintableString (BootOption->OptionalData, &IsUnicode);\r
+      if (IsPrintable) {\r
+        if (IsUnicode) {\r
+          StrnCpy (UnicodeCmdLine, BootOption->OptionalData, BootOption->OptionalDataSize / 2);\r
+        } else {\r
+          AsciiStrnCpy (CmdLine, BootOption->OptionalData, BootOption->OptionalDataSize);\r
+        }\r
+      }\r
+    } else {\r
+      UnicodeCmdLine[0] = L'\0';\r
+      IsPrintable = TRUE;\r
+      IsUnicode = TRUE;\r
+    }\r
+\r
+    // We do not request arguments for OptionalData that cannot be printed\r
+    if (IsPrintable) {\r
+      if (IsUnicode) {\r
+        Status = EditHIInputStr (UnicodeCmdLine, BOOT_DEVICE_OPTION_MAX);\r
+        if (EFI_ERROR (Status)) {\r
+          Status = EFI_ABORTED;\r
+          goto FREE_DEVICE_PATH;\r
+        }\r
+\r
+        OptionalData = (UINT8*)UnicodeCmdLine;\r
+        OptionalDataSize = StrSize (UnicodeCmdLine);\r
+      } else {\r
+        Status = EditHIInputAscii (CmdLine, BOOT_DEVICE_OPTION_MAX);\r
+        if (EFI_ERROR (Status)) {\r
+          Status = EFI_ABORTED;\r
+          goto FREE_DEVICE_PATH;\r
+        }\r
+\r
+        OptionalData = (UINT8*)CmdLine;\r
+        OptionalDataSize = AsciiStrSize (CmdLine);\r
+      }\r
+    } else {\r
+      // We keep the former OptionalData\r
+      OptionalData = BootOption->OptionalData;\r
+      OptionalDataSize = BootOption->OptionalDataSize;\r
+    }\r
   }\r
 \r
   Print(L"Description for this new Entry: ");\r
-  UnicodeStrToAsciiStr (BootOption->Description,AsciiBootDescription);\r
-  Status = EditHIInputAscii (AsciiBootDescription,BOOT_DEVICE_DESCRIPTION_MAX);\r
+  StrnCpy (BootDescription, BootOption->Description, BOOT_DEVICE_DESCRIPTION_MAX);\r
+  Status = EditHIInputStr (BootDescription, BOOT_DEVICE_DESCRIPTION_MAX);\r
   if (EFI_ERROR(Status)) {\r
     Status = EFI_ABORTED;\r
     goto FREE_DEVICE_PATH;\r
   }\r
 \r
-  // Convert Ascii into Unicode\r
-  BootDescription = (CHAR16*)AllocatePool(AsciiStrSize(AsciiBootDescription) * sizeof(CHAR16));\r
-  AsciiStrToUnicodeStr (AsciiBootDescription, BootDescription);\r
-\r
   // Update the entry\r
-  Status = BootOptionUpdate (BootOption, Attributes, BootDescription, DevicePath, BootType, AsciiBootOption);\r
-\r
-  FreePool (BootDescription);\r
+  Status = BootOptionUpdate (BootOption, BootOption->Attributes, BootDescription, DevicePath, BootType, OptionalData, OptionalDataSize);\r
 \r
 FREE_DEVICE_PATH:\r
   FreePool (DevicePath);\r
 \r
 EXIT:\r
+  if (Status == EFI_ABORTED) {\r
+    Print(L"\n");\r
+  }\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+UpdateFdtPath (\r
+  IN LIST_ENTRY *BootOptionsList\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  UINTN                     FdtDevicePathSize;\r
+  BDS_SUPPORTED_DEVICE      *SupportedBootDevice;\r
+  EFI_DEVICE_PATH_PROTOCOL  *FdtDevicePathNodes;\r
+  EFI_DEVICE_PATH_PROTOCOL  *FdtDevicePath;\r
+\r
+  Status = SelectBootDevice (&SupportedBootDevice);\r
+  if (EFI_ERROR(Status)) {\r
+    Status = EFI_ABORTED;\r
+    goto EXIT;\r
+  }\r
+\r
+  // Create the specific device path node\r
+  Status = SupportedBootDevice->Support->CreateDevicePathNode (L"FDT blob", &FdtDevicePathNodes, NULL);\r
+  if (EFI_ERROR(Status)) {\r
+    Status = EFI_ABORTED;\r
+    goto EXIT;\r
+  }\r
+\r
+  if (FdtDevicePathNodes != NULL) {\r
+    // Append the Device Path node to the select device path\r
+    FdtDevicePath = AppendDevicePath (SupportedBootDevice->DevicePathProtocol, FdtDevicePathNodes);\r
+    FdtDevicePathSize = GetDevicePathSize (FdtDevicePath);\r
+    Status = gRT->SetVariable (\r
+                    (CHAR16*)L"Fdt",\r
+                    &gArmGlobalVariableGuid,\r
+                    EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+                    FdtDevicePathSize,\r
+                    FdtDevicePath\r
+                    );\r
+    ASSERT_EFI_ERROR(Status);\r
+  } else {\r
+    gRT->SetVariable (\r
+           (CHAR16*)L"Fdt",\r
+           &gArmGlobalVariableGuid,\r
+           EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+           0,\r
+           NULL\r
+           );\r
+    ASSERT_EFI_ERROR(Status);\r
+  }\r
+\r
+EXIT:\r
+  if (Status == EFI_ABORTED) {\r
+    Print(L"\n");\r
+  }\r
+  FreePool(SupportedBootDevice);\r
   return Status;\r
 }\r
 \r
@@ -326,6 +655,7 @@ struct BOOT_MANAGER_ENTRY {
     { L"Add Boot Device Entry", BootMenuAddBootOption },\r
     { L"Update Boot Device Entry", BootMenuUpdateBootOption },\r
     { L"Remove Boot Device Entry", BootMenuRemoveBootOption },\r
+    { L"Update FDT path", UpdateFdtPath },\r
 };\r
 \r
 EFI_STATUS\r
@@ -351,24 +681,26 @@ BootMenuManager (
     Print(L"Choice: ");\r
     Status = GetHIInputInteger (&OptionSelected);\r
     if (EFI_ERROR(Status) || (OptionSelected == (BootManagerEntryCount+1))) {\r
+      if (EFI_ERROR(Status)) {\r
+        Print(L"\n");\r
+      }\r
       return EFI_SUCCESS;\r
     } else if ((OptionSelected > 0) && (OptionSelected <= BootManagerEntryCount))  {\r
-      Status = BootManagerEntries[OptionSelected-1].Callback (BootOptionsList);\r
+      BootManagerEntries[OptionSelected-1].Callback (BootOptionsList);\r
     }\r
   }\r
-\r
-  return EFI_SUCCESS;\r
+  // Should never go here\r
 }\r
 \r
 EFI_STATUS\r
-BootEBL (\r
+BootShell (\r
   IN LIST_ENTRY *BootOptionsList\r
   )\r
 {\r
   EFI_STATUS Status;\r
 \r
   // Start EFI Shell\r
-  Status = BdsLoadApplication(mImageHandle, L"Ebl");\r
+  Status = BdsLoadApplication (mImageHandle, L"Shell", 0, NULL);\r
   if (Status == EFI_NOT_FOUND) {\r
     Print (L"Error: EFI Application not found.\n");\r
   } else if (EFI_ERROR(Status)) {\r
@@ -382,7 +714,7 @@ struct BOOT_MAIN_ENTRY {
   CONST CHAR16* Description;\r
   EFI_STATUS (*Callback) (IN LIST_ENTRY *BootOptionsList);\r
 } BootMainEntries[] = {\r
-    { L"EBL", BootEBL },\r
+    { L"Shell", BootShell },\r
     { L"Boot Manager", BootMenuManager },\r
 };\r
 \r
@@ -392,22 +724,24 @@ BootMenuMain (
   VOID\r
   )\r
 {\r
-  LIST_ENTRY BootOptionsList;\r
-  UINTN       OptionCount;\r
-  UINTN       BootOptionCount;\r
-  EFI_STATUS  Status;\r
-  LIST_ENTRY  *Entry;\r
-  BDS_LOAD_OPTION *BootOption;\r
-  UINTN   BootOptionSelected;\r
-  UINTN   Index;\r
-  UINTN   BootMainEntryCount;\r
-\r
+  LIST_ENTRY                    BootOptionsList;\r
+  UINTN                         OptionCount;\r
+  UINTN                         BootOptionCount;\r
+  EFI_STATUS                    Status;\r
+  LIST_ENTRY*                   Entry;\r
+  BDS_LOAD_OPTION*              BootOption;\r
+  UINTN                         BootOptionSelected;\r
+  UINTN                         Index;\r
+  UINTN                         BootMainEntryCount;\r
+  BOOLEAN                       IsUnicode;\r
+\r
+  BootOption         = NULL;\r
   BootMainEntryCount = sizeof(BootMainEntries) / sizeof(struct BOOT_MAIN_ENTRY);\r
-
-  // Get Boot#### list\r
-  BootOptionList (&BootOptionsList);\r
 \r
   while (TRUE) {\r
+    // Get Boot#### list\r
+    BootOptionList (&BootOptionsList);\r
+\r
     OptionCount = 1;\r
 \r
     // Display the Boot options\r
@@ -418,24 +752,66 @@ BootMenuMain (
     {\r
       BootOption = LOAD_OPTION_FROM_LINK(Entry);\r
 \r
-      Print(L"[%d] %s\n",OptionCount,BootOption->Description);\r
+      Print(L"[%d] %s\n", OptionCount, BootOption->Description);\r
 \r
       DEBUG_CODE_BEGIN();\r
         CHAR16*                           DevicePathTxt;\r
         EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;\r
-\r
-        Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);\r
-        ASSERT_EFI_ERROR(Status);\r
-        DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText(BootOption->FilePathList,TRUE,TRUE);\r
+        ARM_BDS_LOADER_OPTIONAL_DATA*     OptionalData;\r
+        UINTN                             CmdLineSize;\r
+        ARM_BDS_LOADER_TYPE               LoaderType;\r
+\r
+        Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);\r
+        if (EFI_ERROR(Status)) {\r
+          // You must provide an implementation of DevicePathToTextProtocol in your firmware (eg: DevicePathDxe)\r
+          DEBUG((EFI_D_ERROR,"Error: Bds requires DevicePathToTextProtocol\n"));\r
+          return Status;\r
+        }\r
+        DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (BootOption->FilePathList, TRUE, TRUE);\r
 \r
         Print(L"\t- %s\n",DevicePathTxt);\r
-        if (BootOption->OptionalData != NULL) {\r
-          Print(L"\t- LoaderType: %d\n",BootOption->OptionalData->LoaderType);\r
-          if (BootOption->OptionalData->Arguments != NULL) {\r
-            Print(L"\t- Arguments: %a\n",BootOption->OptionalData->Arguments);\r
+\r
+        // If it is a supported BootEntry then print its details\r
+        if (IS_ARM_BDS_BOOTENTRY (BootOption)) {\r
+          OptionalData = BootOption->OptionalData;\r
+          LoaderType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((CONST UINT32*)&OptionalData->Header.LoaderType);\r
+          if ((LoaderType == BDS_LOADER_KERNEL_LINUX_ATAG) || (LoaderType == BDS_LOADER_KERNEL_LINUX_FDT)) {\r
+            if (ReadUnaligned16 (&OptionalData->Arguments.LinuxArguments.InitrdSize) > 0) {\r
+              CmdLineSize = ReadUnaligned16 (&OptionalData->Arguments.LinuxArguments.CmdLineSize);\r
+              DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (\r
+                  GetAlignedDevicePath ((EFI_DEVICE_PATH*)((UINTN)(&OptionalData->Arguments.LinuxArguments + 1) + CmdLineSize)), TRUE, TRUE);\r
+              Print(L"\t- Initrd: %s\n", DevicePathTxt);\r
+            }\r
+            if (ReadUnaligned16 (&OptionalData->Arguments.LinuxArguments.CmdLineSize) > 0) {\r
+              Print(L"\t- Arguments: %a\n", (&OptionalData->Arguments.LinuxArguments + 1));\r
+            }\r
           }\r
-        }\r
 \r
+          switch (LoaderType) {\r
+            case BDS_LOADER_EFI_APPLICATION:\r
+              Print(L"\t- LoaderType: EFI Application\n");\r
+              break;\r
+\r
+            case BDS_LOADER_KERNEL_LINUX_ATAG:\r
+              Print(L"\t- LoaderType: Linux kernel with ATAG support\n");\r
+              break;\r
+\r
+            case BDS_LOADER_KERNEL_LINUX_FDT:\r
+              Print(L"\t- LoaderType: Linux kernel with FDT support\n");\r
+              break;\r
+\r
+            default:\r
+              Print(L"\t- LoaderType: Not recognized (%d)\n", LoaderType);\r
+          }\r
+        } else if (BootOption->OptionalData != NULL) {\r
+          if (IsPrintableString (BootOption->OptionalData, &IsUnicode)) {\r
+            if (IsUnicode) {\r
+              Print (L"\t- Arguments: %s\n", BootOption->OptionalData);\r
+            } else {\r
+              AsciiPrint ("\t- Arguments: %a\n", BootOption->OptionalData);\r
+            }\r
+          }\r
+        }\r
         FreePool(DevicePathTxt);\r
       DEBUG_CODE_END();\r
 \r
@@ -482,6 +858,5 @@ BootMenuMain (
       Status = BootOptionStart (BootOption);\r
     }\r
   }\r
-\r
-  return Status;\r
+  // Should never go here\r
 }\r