--- /dev/null
+/** @file\r
+*\r
+* Copyright (c) 2011 - 2015, 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
+* which accompanies this distribution. The full text of the license may be found at\r
+* http://opensource.org/licenses/bsd-license.php\r
+*\r
+* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+*\r
+**/\r
+\r
+#include "BdsInternal.h"\r
+\r
+// This GUID is defined in the INGF file of ArmPkg/Application/LinuxLoader\r
+CONST EFI_GUID mLinuxLoaderAppGuid = { 0x701f54f2, 0x0d70, 0x4b89, { 0xbc, 0x0a, 0xd9, 0xca, 0x25, 0x37, 0x90, 0x59 }};\r
+\r
+// Device path of the EFI Linux Loader in the Firmware Volume\r
+EFI_DEVICE_PATH* mLinuxLoaderDevicePath = NULL;\r
+\r
+STATIC\r
+BOOLEAN\r
+HasFilePathEfiExtension (\r
+ IN CHAR16* FilePath\r
+ )\r
+{\r
+ return (StrCmp (FilePath + (StrSize (FilePath) / sizeof (CHAR16)) - 5, L".EFI") == 0) ||\r
+ (StrCmp (FilePath + (StrSize (FilePath) / sizeof (CHAR16)) - 5, L".efi") == 0);\r
+}\r
+\r
+/**\r
+ * This function check if the DevicePath defines an EFI binary\r
+ *\r
+ * This function is used when the BDS support Linux loader to\r
+ * detect if the binary is an EFI application or potentially a\r
+ * Linux kernel.\r
+ */\r
+EFI_STATUS\r
+IsEfiBinary (\r
+ IN EFI_DEVICE_PATH* DevicePath,\r
+ OUT BOOLEAN *EfiBinary\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ CHAR16* FileName;\r
+ EFI_DEVICE_PATH* PrevDevicePathNode;\r
+ EFI_DEVICE_PATH* DevicePathNode;\r
+ EFI_PHYSICAL_ADDRESS Image;\r
+ UINTN FileSize;\r
+ EFI_IMAGE_DOS_HEADER* DosHeader;\r
+ UINTN PeCoffHeaderOffset;\r
+ EFI_IMAGE_NT_HEADERS32* NtHeader;\r
+\r
+ ASSERT (EfiBinary != NULL);\r
+\r
+ //\r
+ // Check if the last node of the device path is a FilePath node\r
+ //\r
+ PrevDevicePathNode = NULL;\r
+ DevicePathNode = DevicePath;\r
+ while ((DevicePathNode != NULL) && !IsDevicePathEnd (DevicePathNode)) {\r
+ PrevDevicePathNode = DevicePathNode;\r
+ DevicePathNode = NextDevicePathNode (DevicePathNode);\r
+ }\r
+\r
+ if ((PrevDevicePathNode != NULL) &&\r
+ (PrevDevicePathNode->Type == MEDIA_DEVICE_PATH) &&\r
+ (PrevDevicePathNode->SubType == MEDIA_FILEPATH_DP))\r
+ {\r
+ FileName = ((FILEPATH_DEVICE_PATH*)PrevDevicePathNode)->PathName;\r
+ } else {\r
+ FileName = NULL;\r
+ }\r
+\r
+ if (FileName == NULL) {\r
+ Print (L"Is an EFI Application? ");\r
+ Status = GetHIInputBoolean (EfiBinary);\r
+ if (EFI_ERROR (Status)) {\r
+ return EFI_ABORTED;\r
+ }\r
+ } else if (HasFilePathEfiExtension (FileName)) {\r
+ *EfiBinary = TRUE;\r
+ } else {\r
+ // Check if the file exist\r
+ Status = BdsLoadImage (DevicePath, AllocateAnyPages, &Image, &FileSize);\r
+ if (!EFI_ERROR (Status)) {\r
+\r
+ DosHeader = (EFI_IMAGE_DOS_HEADER *)(UINTN) Image;\r
+ if (DosHeader->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
+ //\r
+ // DOS image header is present,\r
+ // so read the PE header after the DOS image header.\r
+ //\r
+ PeCoffHeaderOffset = DosHeader->e_lfanew;\r
+ } else {\r
+ PeCoffHeaderOffset = 0;\r
+ }\r
+\r
+ //\r
+ // Check PE/COFF image.\r
+ //\r
+ NtHeader = (EFI_IMAGE_NT_HEADERS32 *)(UINTN) (Image + PeCoffHeaderOffset);\r
+ if (NtHeader->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
+ *EfiBinary = FALSE;\r
+ } else {\r
+ *EfiBinary = TRUE;\r
+ }\r
+\r
+ // Free memory\r
+ gBS->FreePages (Image, EFI_SIZE_TO_PAGES (FileSize));\r
+ } else {\r
+ // If we did not manage to open it then ask for the type\r
+ Print (L"Is an EFI Application? ");\r
+ Status = GetHIInputBoolean (EfiBinary);\r
+ if (EFI_ERROR (Status)) {\r
+ return EFI_ABORTED;\r
+ }\r
+ }\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r