]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPlatformPkg/Bds/BootMenu.c
ArmPlatformPkg/Bds: Added boot options reordering
[mirror_edk2.git] / ArmPlatformPkg / Bds / BootMenu.c
index 315fc953f5a27a8672904032e962675714529ffc..b96a1c3f923cc30a69e82974a0e44b526f7af839 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
+  Worker function that displays the list of boot options that is passed in.\r
+\r
+  The function loops over the entries of the list of boot options that is passed\r
+  in. For each entry, the boot option description is displayed on a single line\r
+  along with the position of the option in the list. In debug mode, the UEFI\r
+  device path and the arguments of the boot option are displayed as well in\r
+  subsequent lines.\r
+\r
+  @param[in]  BootOptionsList  List of the boot options\r
+\r
+**/\r
+STATIC\r
+VOID\r
+DisplayBootOptions (\r
+  IN  LIST_ENTRY*   BootOptionsList\r
+  )\r
+{\r
+  EFI_STATUS        Status;\r
+  UINTN             BootOptionCount;\r
+  LIST_ENTRY       *Entry;\r
+  BDS_LOAD_OPTION  *BdsLoadOption;\r
+  BOOLEAN           IsUnicode;\r
+\r
+  BootOptionCount = 0 ;\r
+  for (Entry = GetFirstNode (BootOptionsList);\r
+       !IsNull (BootOptionsList, Entry);\r
+       Entry = GetNextNode (BootOptionsList, Entry)\r
+      ) {\r
+\r
+    BdsLoadOption = LOAD_OPTION_FROM_LINK (Entry);\r
+    Print (L"[%d] %s\n", ++BootOptionCount, 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 (\r
+                     &gEfiDevicePathToTextProtocolGuid,\r
+                     NULL,\r
+                     (VOID **)&DevicePathToTextProtocol\r
+                     );\r
+      ASSERT_EFI_ERROR (Status);\r
+      DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (\r
+                                                  BdsLoadOption->FilePathList,\r
+                                                  TRUE,\r
+                                                  TRUE\r
+                                                  );\r
+      Print (L"\t- %s\n", DevicePathTxt);\r
+\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) ||\r
+            (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
+    DEBUG_CODE_END ();\r
+  }\r
+}\r
+\r
+/**\r
+  Worker function that asks for a boot option to be selected and returns a\r
+  pointer to the structure describing the selected boot option.\r
+\r
+  @param[in]  BootOptionsList  List of the boot options\r
+\r
+  @retval     EFI_SUCCESS      Selection succeeded\r
+  @retval     !EFI_SUCCESS     Input error or input cancelled\r
+\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+SelectBootOption (\r
+  IN  LIST_ENTRY*               BootOptionsList,\r
+  IN  CONST CHAR16*             InputStatement,\r
+  OUT BDS_LOAD_OPTION_ENTRY**   BdsLoadOptionEntry\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  UINTN                         BootOptionCount;\r
+  UINT16                       *BootOrder;\r
+  LIST_ENTRY*                   Entry;\r
+  UINTN                         BootOptionSelected;\r
+  UINTN                         Index;\r
+\r
+  // Get the number of boot options\r
+  Status = GetGlobalEnvironmentVariable (\r
+            L"BootOrder", NULL, &BootOptionCount, (VOID**)&BootOrder\r
+            );\r
+  if (EFI_ERROR (Status)) {\r
+    goto ErrorExit;\r
+  }\r
+  FreePool (BootOrder);\r
+  BootOptionCount /= sizeof (UINT16);\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"Nothing to update!\n");\r
+    } else if (StrCmp (InputStatement, MOVE_BOOT_ENTRY) == 0) {\r
+      Print (L"Nothing to move!\n");\r
+    } else {\r
+      Print (L"No supported Boot Entry.\n");\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
+    Print (InputStatement);\r
+    Status = GetHIInputInteger (&BootOptionSelected);\r
+    if (EFI_ERROR (Status)) {\r
+      Print (L"\n");\r
+      goto ErrorExit;\r
+    } else if ((BootOptionSelected == 0) || (BootOptionSelected > BootOptionCount)) {\r
+      Print (L"Invalid input (max %d)\n", BootOptionCount);\r
+      BootOptionSelected = 0;\r
+    }\r
+  }\r
+\r
+  // Get the structure of the Boot device to delete\r
+  Index = 1;\r
+  for (Entry = GetFirstNode (BootOptionsList);\r
+       !IsNull (BootOptionsList, Entry);\r
+       Entry = GetNextNode (BootOptionsList,Entry)\r
+       )\r
+  {\r
+    if (Index == BootOptionSelected) {\r
+      *BdsLoadOptionEntry = LOAD_OPTION_ENTRY_FROM_LINK (Entry);\r
+      break;\r
+    }\r
+    Index++;\r
+  }\r
+\r
+ErrorExit:\r
+  return Status;\r
+}\r
+\r
+STATIC\r
 EFI_STATUS\r
 SelectBootDevice (\r
   OUT BDS_SUPPORTED_DEVICE** SupportedBootDevice\r
@@ -102,7 +260,7 @@ SelectBootDevice (
     }\r
     Index++;\r
   }\r
-  \r
+\r
 EXIT:\r
   BootDeviceListSupportedFree (&SupportedDeviceList, *SupportedBootDevice);\r
   return Status;\r
@@ -117,17 +275,20 @@ BootMenuAddBootOption (
   BDS_SUPPORTED_DEVICE*     SupportedBootDevice;\r
   ARM_BDS_LOADER_ARGUMENTS* BootArguments;\r
   CHAR16                    BootDescription[BOOT_DEVICE_DESCRIPTION_MAX];\r
-  CHAR8                     CmdLine[BOOT_DEVICE_OPTION_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  *DevicePathNode;\r
-  EFI_DEVICE_PATH_PROTOCOL  *InitrdPathNode;\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
 \r
   Attributes                = 0;\r
   SupportedBootDevice = NULL;\r
@@ -140,13 +301,27 @@ BootMenuAddBootOption (
   }\r
 \r
   // Create the specific device path node\r
-  Status = SupportedBootDevice->Support->CreateDevicePathNode (L"EFI Application or the kernel", &DevicePathNode, &BootType, &Attributes);\r
+  Status = SupportedBootDevice->Support->CreateDevicePathNode (L"EFI Application or the kernel", &DevicePathNodes);\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
+  // 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 (SupportedBootDevice->Support->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
@@ -158,15 +333,19 @@ BootMenuAddBootOption (
 \r
     if (InitrdSupport) {\r
       // Create the specific device path node\r
-      Status = SupportedBootDevice->Support->CreateDevicePathNode (L"initrd", &InitrdPathNode, NULL, NULL);\r
+      Status = SupportedBootDevice->Support->CreateDevicePathNode (L"initrd", &InitrdPathNodes);\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 (InitrdPathNode != NULL) {\r
-        // Append the Device Path node to the select device path\r
-        InitrdPath = AppendDevicePathNode (SupportedBootDevice->DevicePathProtocol, (CONST EFI_DEVICE_PATH_PROTOCOL *)InitrdPathNode);\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
@@ -175,23 +354,34 @@ BootMenuAddBootOption (
     }\r
 \r
     Print(L"Arguments to pass to the binary: ");\r
-    Status = GetHIInputAscii (CmdLine,BOOT_DEVICE_OPTION_MAX);\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 (CmdLine);\r
+    CmdLineSize = AsciiStrSize (AsciiCmdLine);\r
     InitrdSize = GetDevicePathSize (InitrdPath);\r
 \r
-    BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool (sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize);\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), CmdLine, CmdLineSize);\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
-    BootArguments = NULL;\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
@@ -203,7 +393,7 @@ BootMenuAddBootOption (
 \r
   // Create new entry\r
   BdsLoadOptionEntry = (BDS_LOAD_OPTION_ENTRY*)AllocatePool (sizeof(BDS_LOAD_OPTION_ENTRY));\r
-  Status = BootOptionCreate (Attributes, BootDescription, DevicePath, BootType, BootArguments, &BdsLoadOptionEntry->BdsLoadOption);\r
+  Status = BootOptionCreate (Attributes, BootDescription, DevicePath, BootType, OptionalData, OptionalDataSize, &BdsLoadOptionEntry->BdsLoadOption);\r
   if (!EFI_ERROR(Status)) {\r
     InsertTailList (BootOptionsList, &BdsLoadOptionEntry->Link);\r
   }\r
@@ -211,7 +401,6 @@ BootMenuAddBootOption (
 FREE_DEVICE_PATH:\r
   FreePool (DevicePath);\r
 \r
-  \r
 EXIT:\r
   if (Status == EFI_ABORTED) {\r
     Print(L"\n");\r
@@ -220,103 +409,6 @@ EXIT:
   return Status;\r
 }\r
 \r
-STATIC\r
-EFI_STATUS\r
-BootMenuSelectBootOption (\r
-  IN  LIST_ENTRY*               BootOptionsList,\r
-  IN  CONST CHAR16*             InputStatement,\r
-  IN  BOOLEAN                   OnlyArmBdsBootEntry,\r
-  OUT BDS_LOAD_OPTION_ENTRY**   BdsLoadOptionEntry\r
-  )\r
-{\r
-  EFI_STATUS                    Status;\r
-  LIST_ENTRY*                   Entry;\r
-  BDS_LOAD_OPTION*              BdsLoadOption;\r
-  UINTN                         BootOptionSelected;\r
-  UINTN                         BootOptionCount;\r
-  UINTN                         Index;\r
-\r
-  // Display the list of supported boot devices\r
-  BootOptionCount = 0;\r
-  for (Entry = GetFirstNode (BootOptionsList);\r
-       !IsNull (BootOptionsList,Entry);\r
-       Entry = GetNextNode (BootOptionsList, Entry)\r
-       )\r
-  {\r
-    BdsLoadOption = LOAD_OPTION_FROM_LINK(Entry);\r
-\r
-    if (OnlyArmBdsBootEntry && !IS_ARM_BDS_BOOTENTRY (BdsLoadOption)) {\r
-      continue;\r
-    }\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(BdsLoadOption->FilePathList,TRUE,TRUE);\r
-\r
-      Print(L"\t- %s\n",DevicePathTxt);\r
-      OptionalData = BdsLoadOption->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
-        Print (L"\t- Arguments: %a\n",&OptionalData->Arguments.LinuxArguments + 1);\r
-      }\r
-\r
-      FreePool(DevicePathTxt);\r
-    DEBUG_CODE_END();\r
-\r
-    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
-    Print(InputStatement);\r
-    Status = GetHIInputInteger (&BootOptionSelected);\r
-    if (EFI_ERROR(Status)) {\r
-      return Status;\r
-    } else if ((BootOptionSelected == 0) || (BootOptionSelected > BootOptionCount)) {\r
-      Print(L"Invalid input (max %d)\n",BootOptionCount);\r
-      BootOptionSelected = 0;\r
-    }\r
-  }\r
-\r
-  // Get the structure of the Boot device to delete\r
-  Index = 1;\r
-  for (Entry = GetFirstNode (BootOptionsList);\r
-       !IsNull (BootOptionsList, Entry);\r
-       Entry = GetNextNode (BootOptionsList,Entry)\r
-       )\r
-  {\r
-    if (Index == BootOptionSelected) {\r
-      *BdsLoadOptionEntry = LOAD_OPTION_ENTRY_FROM_LINK(Entry);\r
-      break;\r
-    }\r
-    Index++;\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
 EFI_STATUS\r
 BootMenuRemoveBootOption (\r
   IN LIST_ENTRY *BootOptionsList\r
@@ -325,8 +417,9 @@ BootMenuRemoveBootOption (
   EFI_STATUS                    Status;\r
   BDS_LOAD_OPTION_ENTRY*        BootOptionEntry;\r
 \r
-  Status = BootMenuSelectBootOption (BootOptionsList, DELETE_BOOT_ENTRY, FALSE, &BootOptionEntry);\r
-  if (EFI_ERROR(Status)) {\r
+  DisplayBootOptions (BootOptionsList);\r
+  Status = SelectBootOption (BootOptionsList, DELETE_BOOT_ENTRY, &BootOptionEntry);\r
+  if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
@@ -354,18 +447,25 @@ BootMenuUpdateBootOption (
   ARM_BDS_LOADER_ARGUMENTS*     BootArguments;\r
   CHAR16                        BootDescription[BOOT_DEVICE_DESCRIPTION_MAX];\r
   CHAR8                         CmdLine[BOOT_DEVICE_OPTION_MAX];\r
-  EFI_DEVICE_PATH*              DevicePath;\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* OptionalData;\r
+  ARM_BDS_LOADER_OPTIONAL_DATA* LoaderOptionalData;\r
   ARM_BDS_LINUX_ARGUMENTS*      LinuxArguments;\r
-  EFI_DEVICE_PATH               *InitrdPathNode;\r
+  EFI_DEVICE_PATH               *InitrdPathNodes;\r
   EFI_DEVICE_PATH               *InitrdPath;\r
   UINTN                         InitrdSize;\r
   UINTN                         CmdLineSize;\r
   BOOLEAN                       InitrdSupport;\r
-\r
-  Status = BootMenuSelectBootOption (BootOptionsList, UPDATE_BOOT_ENTRY, TRUE, &BootOptionEntry);\r
-  if (EFI_ERROR(Status)) {\r
+  UINT8*                        OptionalData;\r
+  UINTN                         OptionalDataSize;\r
+  BOOLEAN                       IsPrintable;\r
+  BOOLEAN                       IsUnicode;\r
+\r
+  DisplayBootOptions (BootOptionsList);\r
+  Status = SelectBootOption (BootOptionsList, UPDATE_BOOT_ENTRY, &BootOptionEntry);\r
+  if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
   BootOption = BootOptionEntry->BdsLoadOption;\r
@@ -377,17 +477,29 @@ BootMenuUpdateBootOption (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  Status = DeviceSupport->UpdateDevicePathNode (BootOption->FilePathList, L"EFI Application or the kernel", &DevicePath, NULL, NULL);\r
+  Status = DeviceSupport->UpdateDevicePathNode (BootOption->FilePathList, L"EFI Application or the kernel", &DevicePath);\r
   if (EFI_ERROR(Status)) {\r
     Status = EFI_ABORTED;\r
     goto EXIT;\r
   }\r
 \r
-  OptionalData = BootOption->OptionalData;\r
-  BootType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((UINT32 *)(&OptionalData->Header.LoaderType));\r
+  if (DeviceSupport->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
+    BootType = BDS_LOADER_EFI_APPLICATION;\r
+  }\r
 \r
   if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {\r
-    LinuxArguments = &OptionalData->Arguments.LinuxArguments;\r
+    LinuxArguments = &LoaderOptionalData->Arguments.LinuxArguments;\r
 \r
     CmdLineSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->CmdLineSize);\r
 \r
@@ -406,7 +518,7 @@ BootMenuUpdateBootOption (
     if (InitrdSupport) {\r
       if (InitrdSize > 0) {\r
         // Case we update the initrd device path\r
-        Status = DeviceSupport->UpdateDevicePathNode ((EFI_DEVICE_PATH*)((LinuxArguments + 1) + CmdLineSize), L"initrd", &InitrdPath, NULL, NULL);\r
+        Status = DeviceSupport->UpdateDevicePathNode ((EFI_DEVICE_PATH*)((UINTN)(LinuxArguments + 1) + CmdLineSize), L"initrd", &InitrdPath);\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
@@ -415,19 +527,25 @@ BootMenuUpdateBootOption (
       } else {\r
         // Case we create the initrd device path\r
 \r
-        Status = DeviceSupport->CreateDevicePathNode (L"initrd", &InitrdPathNode, NULL, NULL);\r
+        Status = DeviceSupport->CreateDevicePathNode (L"initrd", &InitrdPathNodes);\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 (InitrdPathNode != NULL) {\r
+        if (InitrdPathNodes != NULL) {\r
           // Duplicate Linux kernel Device Path\r
-          DevicePath = DuplicateDevicePath (BootOption->FilePathList);\r
+          TempInitrdPath = DuplicateDevicePath (BootOption->FilePathList);\r
           // Replace Linux kernel Node by EndNode\r
-          SetDevicePathEndNode (GetLastDevicePathNode (DevicePath));\r
-          // Append the Device Path node to the select device path\r
-          InitrdPath = AppendDevicePathNode (DevicePath, (CONST EFI_DEVICE_PATH_PROTOCOL *)InitrdPathNode);\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
@@ -436,7 +554,7 @@ BootMenuUpdateBootOption (
       InitrdSize = 0;\r
     }\r
 \r
-    Print(L"Arguments to pass to the binary: "); \r
+    Print(L"Arguments to pass to the binary: ");\r
     if (CmdLineSize > 0) {\r
       AsciiStrnCpy(CmdLine, (CONST CHAR8*)(LinuxArguments + 1), CmdLineSize);\r
     } else {\r
@@ -450,13 +568,58 @@ BootMenuUpdateBootOption (
 \r
     CmdLineSize = AsciiStrSize (CmdLine);\r
 \r
-    BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool(sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize);\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 ((UINTN)(&BootArguments->LinuxArguments + 1) + CmdLine, InitrdPath, InitrdSize);\r
+    CopyMem ((VOID*)((UINTN)(&BootArguments->LinuxArguments + 1) + CmdLineSize), InitrdPath, InitrdSize);\r
+\r
+    OptionalData = (UINT8*)BootArguments;\r
   } else {\r
-    BootArguments = NULL;\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
@@ -468,7 +631,7 @@ BootMenuUpdateBootOption (
   }\r
 \r
   // Update the entry\r
-  Status = BootOptionUpdate (BootOption, BootOption->Attributes, BootDescription, DevicePath, BootType, BootArguments);\r
+  Status = BootOptionUpdate (BootOption, BootOption->Attributes, BootDescription, DevicePath, BootType, OptionalData, OptionalDataSize);\r
 \r
 FREE_DEVICE_PATH:\r
   FreePool (DevicePath);\r
@@ -480,16 +643,170 @@ EXIT:
   return Status;\r
 }\r
 \r
+/**\r
+  Reorder boot options\r
+\r
+  Ask for the boot option to move and then move it when up or down arrows\r
+  are pressed. This function is called when the user selects the "Reorder Boot\r
+  Device Entries" entry in the boot manager menu.\r
+  The order of the boot options in BootOptionList and in the UEFI BootOrder\r
+  global variable are kept coherent until the user confirm his reordering (ie:\r
+  he does not exit by pressing escape).\r
+\r
+  @param[in]  BootOptionsList  List of the boot devices constructed in\r
+                               BootMenuMain()\r
+\r
+  @retval  EFI_SUCCESS   No error encountered.\r
+  @retval  !EFI_SUCCESS  An error has occured either in the selection of the\r
+                         boot option to move or while interacting with the user.\r
+\r
+**/\r
+STATIC\r
 EFI_STATUS\r
-UpdateFdtPath (\r
+BootMenuReorderBootOptions (\r
   IN LIST_ENTRY *BootOptionsList\r
   )\r
 {\r
-  EFI_STATUS Status;\r
+  EFI_STATUS              Status;\r
+  BDS_LOAD_OPTION_ENTRY  *BootOptionEntry;\r
+  LIST_ENTRY             *SelectedEntry;\r
+  LIST_ENTRY             *PrevEntry;\r
+  BOOLEAN                 Move;\r
+  BOOLEAN                 Save;\r
+  BOOLEAN                 Cancel;\r
+  UINTN                   WaitIndex;\r
+  EFI_INPUT_KEY           Key;\r
+  LIST_ENTRY             *SecondEntry;\r
+  UINTN                   BootOrderSize;\r
+  UINT16                 *BootOrder;\r
+  LIST_ENTRY             *Entry;\r
+  UINTN                   Index;\r
+\r
+  DisplayBootOptions (BootOptionsList);\r
+\r
+  // Ask to select the boot option to move\r
+  while (TRUE) {\r
+    Status = SelectBootOption (BootOptionsList, MOVE_BOOT_ENTRY, &BootOptionEntry);\r
+    if (EFI_ERROR (Status)) {\r
+      goto ErrorExit;\r
+    }\r
 \r
-  BDS_SUPPORTED_DEVICE *SupportedBootDevice;\r
-  EFI_DEVICE_PATH_PROTOCOL *FdtDevicePathNode;\r
-  EFI_DEVICE_PATH_PROTOCOL *FdtDevicePath;\r
+    SelectedEntry = &BootOptionEntry->Link;\r
+    // Note down the previous entry in the list to be able to cancel changes\r
+    PrevEntry = GetPreviousNode (BootOptionsList, SelectedEntry);\r
+\r
+    //  Start of interaction\r
+    while (TRUE) {\r
+      Print (\r
+        L"* Use up/down arrows to move the entry '%s'",\r
+        BootOptionEntry->BdsLoadOption->Description\r
+        );\r
+\r
+      // Wait for a move, save or cancel request\r
+      Move   = FALSE;\r
+      Save   = FALSE;\r
+      Cancel = FALSE;\r
+      do {\r
+        Status = gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &WaitIndex);\r
+        if (!EFI_ERROR (Status)) {\r
+          Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
+        }\r
+        if (EFI_ERROR (Status)) {\r
+          Print (L"\n");\r
+          goto ErrorExit;\r
+        }\r
+\r
+        switch (Key.ScanCode) {\r
+        case SCAN_NULL:\r
+          Save = (Key.UnicodeChar == CHAR_LINEFEED)        ||\r
+                 (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) ||\r
+                 (Key.UnicodeChar == 0x7f);\r
+          break;\r
+\r
+        case SCAN_UP:\r
+          SecondEntry = GetPreviousNode (BootOptionsList, SelectedEntry);\r
+          Move = SecondEntry != BootOptionsList;\r
+          break;\r
+\r
+        case SCAN_DOWN:\r
+          SecondEntry = GetNextNode (BootOptionsList, SelectedEntry);\r
+          Move = SecondEntry != BootOptionsList;\r
+          break;\r
+\r
+        case SCAN_ESC:\r
+          Cancel = TRUE;\r
+          break;\r
+        }\r
+      } while ((!Move) && (!Save) && (!Cancel));\r
+\r
+      if (Move) {\r
+        SwapListEntries (SelectedEntry, SecondEntry);\r
+      } else {\r
+        if (Save) {\r
+          Status = GetGlobalEnvironmentVariable (\r
+                    L"BootOrder", NULL, &BootOrderSize, (VOID**)&BootOrder\r
+                    );\r
+          BootOrderSize /= sizeof (UINT16);\r
+\r
+          if (!EFI_ERROR (Status)) {\r
+            // The order of the boot options in the 'BootOptionsList' is the\r
+            // new order that has been just defined by the user. Save this new\r
+            // order in "BootOrder" UEFI global variable.\r
+            Entry = GetFirstNode (BootOptionsList);\r
+            for (Index = 0; Index < BootOrderSize; Index++) {\r
+              BootOrder[Index] = (LOAD_OPTION_FROM_LINK (Entry))->LoadOptionIndex;\r
+              Entry = GetNextNode (BootOptionsList, Entry);\r
+            }\r
+            Status = gRT->SetVariable (\r
+                           (CHAR16*)L"BootOrder",\r
+                           &gEfiGlobalVariableGuid,\r
+                           EFI_VARIABLE_NON_VOLATILE       |\r
+                           EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
+                           EFI_VARIABLE_RUNTIME_ACCESS,\r
+                           BootOrderSize * sizeof (UINT16),\r
+                           BootOrder\r
+                           );\r
+            FreePool (BootOrder);\r
+          }\r
+\r
+          if (EFI_ERROR (Status)) {\r
+            Print (L"\nAn error occurred, move not completed!\n");\r
+            Cancel = TRUE;\r
+          }\r
+        }\r
+\r
+        if (Cancel) {\r
+          //\r
+          // Restore initial position of the selected boot option\r
+          //\r
+          RemoveEntryList (SelectedEntry);\r
+          InsertHeadList (PrevEntry, SelectedEntry);\r
+        }\r
+      }\r
+\r
+      Print (L"\n");\r
+      DisplayBootOptions (BootOptionsList);\r
+      // Saved or cancelled, back to the choice of boot option to move\r
+      if (!Move) {\r
+        break;\r
+      }\r
+    }\r
+  }\r
+\r
+ErrorExit:\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
@@ -498,20 +815,32 @@ UpdateFdtPath (
   }\r
 \r
   // Create the specific device path node\r
-  Print(L"File path of the FDT blob: ");\r
-  Status = SupportedBootDevice->Support->CreateDevicePathNode (SupportedBootDevice, &FdtDevicePathNode, NULL, NULL);\r
+  Status = SupportedBootDevice->Support->CreateDevicePathNode (L"FDT blob", &FdtDevicePathNodes);\r
   if (EFI_ERROR(Status)) {\r
     Status = EFI_ABORTED;\r
     goto EXIT;\r
   }\r
 \r
-  if (FdtDevicePathNode != NULL) {\r
+  if (FdtDevicePathNodes != NULL) {\r
     // Append the Device Path node to the select device path\r
-    FdtDevicePath = AppendDevicePathNode (SupportedBootDevice->DevicePathProtocol, FdtDevicePathNode);\r
-    Status = gRT->SetVariable ((CHAR16*)L"FDT", &gEfiGlobalVariableGuid, (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS ), 4, &FdtDevicePath);\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 ((CHAR16*)L"FDT", &gEfiGlobalVariableGuid, (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS ), 0, NULL);\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
@@ -523,6 +852,54 @@ EXIT:
   return Status;\r
 }\r
 \r
+/**\r
+  Set boot timeout\r
+\r
+  Ask for the boot timeout in seconds and if the input succeeds assign the\r
+  input value to the UEFI global variable "Timeout". This function is called\r
+  when the user selects the "Set Boot Timeout" of the boot manager menu.\r
+\r
+  @param[in]  BootOptionsList  List of the boot devices, not used here\r
+\r
+  @retval  EFI_SUCCESS   Boot timeout in second retrieved from the standard\r
+                         input and assigned to the UEFI "Timeout" global\r
+                         variable\r
+  @retval  !EFI_SUCCESS  Either the input or the setting of the UEFI global\r
+                         variable "Timeout" has failed.\r
+**/\r
+EFI_STATUS\r
+STATIC\r
+BootMenuSetBootTimeout (\r
+  IN LIST_ENTRY *BootOptionsList\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Input;\r
+  UINT16      Timeout;\r
+\r
+  Print (L"Timeout duration (in seconds): ");\r
+  Status = GetHIInputInteger (&Input);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"\n");\r
+    goto ErrorExit;\r
+  }\r
+\r
+  Timeout = Input;\r
+  Status = gRT->SetVariable (\r
+                 (CHAR16*)L"Timeout",\r
+                 &gEfiGlobalVariableGuid,\r
+                 EFI_VARIABLE_NON_VOLATILE       |\r
+                 EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
+                 EFI_VARIABLE_RUNTIME_ACCESS,\r
+                 sizeof (UINT16),\r
+                 &Timeout\r
+                 );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+ErrorExit:\r
+  return Status;\r
+}\r
+\r
 struct BOOT_MANAGER_ENTRY {\r
   CONST CHAR16* Description;\r
   EFI_STATUS (*Callback) (IN LIST_ENTRY *BootOptionsList);\r
@@ -530,7 +907,9 @@ 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"Reorder Boot Device Entries", BootMenuReorderBootOptions },\r
     { L"Update FDT path", UpdateFdtPath },\r
+    { L"Set Boot Timeout", BootMenuSetBootTimeout },\r
 };\r
 \r
 EFI_STATUS\r
@@ -568,14 +947,14 @@ BootMenuManager (
 }\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", 0, NULL);\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
@@ -589,7 +968,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
@@ -608,8 +987,9 @@ BootMenuMain (
   UINTN                         BootOptionSelected;\r
   UINTN                         Index;\r
   UINTN                         BootMainEntryCount;\r
+  BOOLEAN                       IsUnicode;\r
 \r
-  BootOption              = NULL;\r
+  BootOption         = NULL;\r
   BootMainEntryCount = sizeof(BootMainEntries) / sizeof(struct BOOT_MAIN_ENTRY);\r
 \r
   while (TRUE) {\r
@@ -656,9 +1036,35 @@ BootMenuMain (
                   GetAlignedDevicePath ((EFI_DEVICE_PATH*)((UINTN)(&OptionalData->Arguments.LinuxArguments + 1) + CmdLineSize)), TRUE, TRUE);\r
               Print(L"\t- Initrd: %s\n", DevicePathTxt);\r
             }\r
-            Print(L"\t- Arguments: %a\n", (&OptionalData->Arguments.LinuxArguments + 1));\r
+            if (ReadUnaligned16 (&OptionalData->Arguments.LinuxArguments.CmdLineSize) > 0) {\r
+              Print(L"\t- Arguments: %a\n", (&OptionalData->Arguments.LinuxArguments + 1));\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
-          Print(L"\t- LoaderType: %d\n", LoaderType);\r
         }\r
         FreePool(DevicePathTxt);\r
       DEBUG_CODE_END();\r