]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPlatformPkg/Bds/BootMenu.c
ArmPlatformPkg/Bds: Added support to detect if the binary is a EFI image
[mirror_edk2.git] / ArmPlatformPkg / Bds / BootMenu.c
index 12463194d0a6112476f960a2190ace9edf9a4a57..e00d06e39442a84d252516c832b0f104276c8b12 100644 (file)
@@ -120,17 +120,21 @@ 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
+  BOOLEAN                   RequestBootType;\r
 \r
   Attributes                = 0;\r
   SupportedBootDevice = NULL;\r
@@ -143,13 +147,28 @@ BootMenuAddBootOption (
   }\r
 \r
   // Create the specific device path node\r
-  Status = SupportedBootDevice->Support->CreateDevicePathNode (L"EFI Application or the kernel", &DevicePathNode, &BootType, &Attributes);\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 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 (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
@@ -161,15 +180,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, 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
+      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
@@ -178,23 +201,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
+    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*)((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
@@ -206,7 +240,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
@@ -214,7 +248,6 @@ BootMenuAddBootOption (
 FREE_DEVICE_PATH:\r
   FreePool (DevicePath);\r
 \r
-\r
 EXIT:\r
   if (Status == EFI_ABORTED) {\r
     Print(L"\n");\r
@@ -281,7 +314,7 @@ BootMenuSelectBootOption (
   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
+    } 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
@@ -360,13 +393,16 @@ BootMenuUpdateBootOption (
   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
+  UINT8*                        OptionalData;\r
+  UINTN                         OptionalDataSize;\r
+  BOOLEAN                       RequestBootType;\r
 \r
   Status = BootMenuSelectBootOption (BootOptionsList, UPDATE_BOOT_ENTRY, TRUE, &BootOptionEntry);\r
   if (EFI_ERROR(Status)) {\r
@@ -381,17 +417,26 @@ BootMenuUpdateBootOption (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  Status = DeviceSupport->UpdateDevicePathNode (BootOption->FilePathList, L"EFI Application or the kernel", &DevicePath, NULL, NULL);\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
-  OptionalData = BootOption->OptionalData;\r
-  BootType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((UINT32 *)(&OptionalData->Header.LoaderType));\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
+  BootType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((UINT32 *)(&LoaderOptionalData->Header.LoaderType));\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
@@ -410,7 +455,7 @@ BootMenuUpdateBootOption (
     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, NULL);\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
@@ -419,20 +464,24 @@ 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, 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
+        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 node to the select device path\r
-          InitrdPath = AppendDevicePathNode (TempInitrdPath, (CONST EFI_DEVICE_PATH_PROTOCOL *)InitrdPathNode);\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
@@ -456,13 +505,17 @@ 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 ((VOID*)((UINTN)(&BootArguments->LinuxArguments + 1) + CmdLineSize), InitrdPath, InitrdSize);\r
+\r
+    OptionalData = (UINT8*)BootArguments;\r
   } else {\r
-    BootArguments = NULL;\r
+    OptionalData     = NULL;\r
+    OptionalDataSize = 0;\r
   }\r
 \r
   Print(L"Description for this new Entry: ");\r
@@ -474,7 +527,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
@@ -494,7 +547,7 @@ UpdateFdtPath (
   EFI_STATUS                Status;\r
   UINTN                     FdtDevicePathSize;\r
   BDS_SUPPORTED_DEVICE      *SupportedBootDevice;\r
-  EFI_DEVICE_PATH_PROTOCOL  *FdtDevicePathNode;\r
+  EFI_DEVICE_PATH_PROTOCOL  *FdtDevicePathNodes;\r
   EFI_DEVICE_PATH_PROTOCOL  *FdtDevicePath;\r
 \r
   Status = SelectBootDevice (&SupportedBootDevice);\r
@@ -504,15 +557,15 @@ UpdateFdtPath (
   }\r
 \r
   // Create the specific device path node\r
-  Status = SupportedBootDevice->Support->CreateDevicePathNode (L"FDT blob", &FdtDevicePathNode, NULL, NULL);\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 (FdtDevicePathNode != NULL) {\r
+  if (FdtDevicePathNodes != NULL) {\r
     // Append the Device Path node to the select device path\r
-    FdtDevicePath = AppendDevicePathNode (SupportedBootDevice->DevicePathProtocol, FdtDevicePathNode);\r
+    FdtDevicePath = AppendDevicePath (SupportedBootDevice->DevicePathProtocol, FdtDevicePathNodes);\r
     FdtDevicePathSize = GetDevicePathSize (FdtDevicePath);\r
     Status = gRT->SetVariable (\r
                     (CHAR16*)L"Fdt",\r
@@ -676,7 +729,23 @@ BootMenuMain (
             }\r
             Print(L"\t- Arguments: %a\n", (&OptionalData->Arguments.LinuxArguments + 1));\r
           }\r
-          Print(L"\t- LoaderType: %d\n", LoaderType);\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
         }\r
         FreePool(DevicePathTxt);\r
       DEBUG_CODE_END();\r