]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPlatformPkg/Bds/BootMenu.c
ArmPlatformPkg/Bds: Add a signature in front of the Boot Argument propoer to this Bds
[mirror_edk2.git] / ArmPlatformPkg / Bds / BootMenu.c
index 1e25c1e72e5d477302736c377917e279f919a336..8aa09596fdcbaed2637d3044e876ea6660eb60a2 100644 (file)
@@ -18,28 +18,16 @@ extern EFI_HANDLE mImageHandle;
 extern BDS_LOAD_OPTION_SUPPORT *BdsLoadOptionSupportList;\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
-  SupportedBootDevice = NULL;\r
 \r
   //\r
   // List the Boot Devices supported\r
@@ -58,8 +46,8 @@ 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
@@ -67,7 +55,7 @@ BootMenuAddBootOption (
 \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
@@ -109,13 +97,49 @@ 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
+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                     CmdLine[BOOT_DEVICE_OPTION_MAX];\r
+  UINT32                    Attributes;\r
+  ARM_BDS_LOADER_TYPE       BootType;\r
+  BDS_LOAD_OPTION          *BdsLoadOption;\r
+  EFI_DEVICE_PATH           *DevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePathNode;\r
+  EFI_DEVICE_PATH_PROTOCOL  *InitrdPathNode;\r
+  EFI_DEVICE_PATH_PROTOCOL  *InitrdPath;\r
+  UINTN                     CmdLineSize;\r
+  UINTN                     InitrdSize;\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
 \r
   // Create the specific device path node\r
+  Print(L"File path of the EFI Application or the kernel: ");\r
   Status = SupportedBootDevice->Support->CreateDevicePathNode (SupportedBootDevice, &DevicePathNode, &BootType, &Attributes);\r
   if (EFI_ERROR(Status)) {\r
     Status = EFI_ABORTED;\r
@@ -124,37 +148,64 @@ BootMenuAddBootOption (
   // 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
-  if (EFI_ERROR(Status)) {\r
-    Status = EFI_ABORTED;\r
-    goto FREE_DEVICE_PATH;\r
+  if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {\r
+    // Create the specific device path node\r
+    Print(L"File path of the initrd: ");\r
+    Status = SupportedBootDevice->Support->CreateDevicePathNode (SupportedBootDevice, &InitrdPathNode, NULL, 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 (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
+    } else {\r
+      InitrdPath = NULL;\r
+    }\r
+\r
+    Print(L"Arguments to pass to the binary: ");\r
+    Status = GetHIInputAscii (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
+    InitrdSize = GetDevicePathSize (InitrdPath);\r
+\r
+    BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool (sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize);\r
+    \r
+    BootArguments->LinuxArguments.CmdLineSize = CmdLineSize;\r
+    BootArguments->LinuxArguments.InitrdSize = InitrdSize;\r
+    CopyMem ((VOID*)(&BootArguments->LinuxArguments + 1), CmdLine, CmdLineSize);\r
+    CopyMem ((VOID*)((UINTN)(&BootArguments->LinuxArguments + 1) + CmdLineSize), InitrdPath, InitrdSize);\r
+  } else {\r
+    BootArguments = NULL;\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
+  Status = BootOptionCreate (Attributes, BootDescription, DevicePath, BootType, &BootArguments, &BdsLoadOption);\r
   if (!EFI_ERROR(Status)) {\r
     InsertTailList (BootOptionsList,&BdsLoadOption->Link);\r
   }\r
 \r
-  FreePool (BootDescription);\r
-\r
 FREE_DEVICE_PATH:\r
   FreePool (DevicePath);\r
 \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
@@ -186,14 +237,17 @@ BootMenuSelectBootOption (
     DEBUG_CODE_BEGIN();\r
       CHAR16*                           DevicePathTxt;\r
       EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;\r
+      ARM_BDS_LOADER_TYPE               LoaderType;\r
 \r
       Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);\r
       ASSERT_EFI_ERROR(Status);\r
       DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText(BootOption->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
+      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
@@ -210,7 +264,7 @@ BootMenuSelectBootOption (
     if (EFI_ERROR(Status)) {\r
       return Status;\r
     } else if ((BootOptionSelected == 0) || (BootOptionSelected >= BootOptionCount)) {\r
-      Print(L"Invalid input (max %d)\n",BootOptionCount);\r
+      Print(L"Invalid input (max %d)\n",BootOptionCount-1);\r
       BootOptionSelected = 0;\r
     }\r
   }\r
@@ -256,15 +310,19 @@ 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
+  EFI_STATUS                    Status;\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
+  EFI_DEVICE_PATH*              DevicePath;\r
+  ARM_BDS_LOADER_TYPE           BootType;\r
+  ARM_BDS_LOADER_OPTIONAL_DATA* OptionalData;\r
+  ARM_BDS_LINUX_ARGUMENTS*      LinuxArguments;\r
+  EFI_DEVICE_PATH*              InitrdPathList;\r
+  UINTN                         InitrdSize;\r
+  UINTN                         CmdLineSize;\r
 \r
   Status = BootMenuSelectBootOption (BootOptionsList,L"Update entry: ",&BootOption);\r
   if (EFI_ERROR(Status)) {\r
@@ -278,45 +336,72 @@ BootMenuUpdateBootOption (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  Status = DeviceSupport->UpdateDevicePathNode (BootOption,&DevicePath,&BootType,&Attributes);\r
+  Print(L"File path of the EFI Application or the kernel: ");\r
+  Status = DeviceSupport->UpdateDevicePathNode (BootOption->FilePathList, &DevicePath, NULL, NULL);\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
+  OptionalData = BootOption->OptionalData;\r
+  BootType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((UINT32 *)(&OptionalData->Header.LoaderType));\r
+\r
+  // TODO: Allow adding an initrd to a boot entry without one\r
+  if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {\r
+    LinuxArguments = &OptionalData->Arguments.LinuxArguments;\r
+\r
+    CmdLineSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->CmdLineSize);\r
+    InitrdSize = GetUnalignedDevicePathSize ((EFI_DEVICE_PATH*)((LinuxArguments + 1) + CmdLineSize));\r
+\r
+    Print(L"File path of the initrd: ");\r
+    Status = DeviceSupport->UpdateDevicePathNode (\r
+      (EFI_DEVICE_PATH_PROTOCOL *)((UINTN)(LinuxArguments + 1) + CmdLineSize), &InitrdPathList, NULL, 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
+    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
+    InitrdSize = GetDevicePathSize (InitrdPathList);\r
+\r
+    BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool(sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize);\r
+    BootArguments->LinuxArguments.CmdLineSize = CmdLineSize;\r
+    BootArguments->LinuxArguments.InitrdSize = InitrdSize;\r
+    CopyMem (&BootArguments->LinuxArguments + 1, CmdLine, CmdLineSize);\r
+    CopyMem ((UINTN)(&BootArguments->LinuxArguments + 1) + CmdLine, InitrdPathList, InitrdSize);\r
   } else {\r
-    AsciiBootOption[0] = '\0';\r
-  }\r
-  Status = EditHIInputAscii (AsciiBootOption,BOOT_DEVICE_OPTION_MAX);\r
-  if (EFI_ERROR(Status)) {\r
-    Status = EFI_ABORTED;\r
-    goto FREE_DEVICE_PATH;\r
+    BootArguments = NULL;\r
   }\r
 \r
   Print(L"Description for this new Entry: ");\r
-  UnicodeStrToAsciiStr (BootOption->Description,AsciiBootDescription);\r
-  Status = EditHIInputAscii (AsciiBootDescription,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, BootArguments);\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
@@ -352,6 +437,9 @@ 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
@@ -393,19 +481,19 @@ 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
+  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
   BootOption              = NULL;\r
   BootMainEntryCount = sizeof(BootMainEntries) / sizeof(struct BOOT_MAIN_ENTRY);\r
-
+\r
   // Get Boot#### list\r
   BootOptionList (&BootOptionsList);\r
 \r
@@ -425,6 +513,9 @@ BootMenuMain (
       DEBUG_CODE_BEGIN();\r
         CHAR16*                           DevicePathTxt;\r
         EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;\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
@@ -435,13 +526,22 @@ BootMenuMain (
         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", ReadUnaligned32 (&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
+            Print(L"\t- Arguments: %a\n", (&OptionalData->Arguments.LinuxArguments + 1));\r
           }\r
+          Print(L"\t- LoaderType: %d\n", LoaderType);\r
         }\r
-\r
         FreePool(DevicePathTxt);\r
       DEBUG_CODE_END();\r
 \r