summaryrefslogtreecommitdiff
path: root/EmbeddedPkg
diff options
context:
space:
mode:
authorHaojian Zhuang <haojian.zhuang@linaro.org>2017-03-01 14:20:06 +0800
committerHaojian Zhuang <haojian.zhuang@linaro.org>2017-03-01 14:50:02 +0800
commit45733e6395d002a430b30f06232fa5d5c764e6f3 (patch)
tree551dc2a9449ed3decd212e5c2ece6637410c1e7e /EmbeddedPkg
parent2320bfdff23e38759afb1cc1f504bfda642c41cb (diff)
downloadedk2-45733e6395d002a430b30f06232fa5d5c764e6f3.tar.gz
EmbeddedPkg/AndroidFastboot: fix to boot android kernel
Fix the issue of booting android kernel since LinuxLoader is broken. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Haojian Zhuang <haojian.zhuang@linaro.org>
Diffstat (limited to 'EmbeddedPkg')
-rw-r--r--EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.c2
-rw-r--r--EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.h21
-rw-r--r--EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.inf4
-rw-r--r--EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c213
4 files changed, 198 insertions, 42 deletions
diff --git a/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.c b/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.c
index c5e8a7e34..204754f71 100644
--- a/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.c
+++ b/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.c
@@ -216,7 +216,7 @@ HandleBoot (
// boot we lose control of the system.
SEND_LITERAL ("OKAY");
- Status = BootAndroidBootImg (mNumDataBytes, mDataBuffer);
+ Status = BootAndroidBootImg (mPlatform, mNumDataBytes, mDataBuffer);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Failed to boot downloaded image: %r\n", Status));
}
diff --git a/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.h b/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.h
index f62660feb..24d31de58 100644
--- a/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.h
+++ b/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.h
@@ -19,24 +19,27 @@
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
-#define BOOTIMG_KERNEL_ARGS_SIZE 512
+#include <Protocol/AndroidFastbootPlatform.h>
+
+#define BOOTIMG_KERNEL_ARGS_SIZE 1024
#define ANDROID_FASTBOOT_VERSION "0.4"
EFI_STATUS
BootAndroidBootImg (
- IN UINTN BufferSize,
- IN VOID *Buffer
+ IN FASTBOOT_PLATFORM_PROTOCOL *Platform,
+ IN UINTN BufferSize,
+ IN VOID *Buffer
);
EFI_STATUS
ParseAndroidBootImg (
- IN VOID *BootImg,
- OUT VOID **Kernel,
- OUT UINTN *KernelSize,
- OUT VOID **Ramdisk,
- OUT UINTN *RamdiskSize,
- OUT CHAR8 *KernelArgs
+ IN VOID *BootImg,
+ OUT VOID **Kernel,
+ OUT UINTN *KernelSize,
+ OUT VOID **Ramdisk,
+ OUT UINTN *RamdiskSize,
+ OUT CHAR8 *KernelArgs
);
#endif //ifdef __ANDROID_FASTBOOT_APP_H__
diff --git a/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.inf b/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.inf
index 3e115171c..daf4008e7 100644
--- a/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.inf
+++ b/EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.inf
@@ -34,6 +34,7 @@
DebugLib
DevicePathLib
DxeServicesTableLib
+ FdtLib
MemoryAllocationLib
PcdLib
PrintLib
@@ -56,3 +57,6 @@
[Packages.ARM, Packages.AARCH64]
ArmPkg/ArmPkg.dec
ArmPlatformPkg/ArmPlatformPkg.dec
+
+[Guids]
+ gFdtTableGuid
diff --git a/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c b/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
index 46a7ceb3a..65f843784 100644
--- a/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
+++ b/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
@@ -15,12 +15,22 @@
#include "AndroidFastbootApp.h"
#include <Protocol/DevicePath.h>
+#include <Protocol/LoadedImage.h>
#include <Library/BdsLib.h>
#include <Library/DevicePathLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/PrintLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
+#include <libfdt.h>
+
+#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
+
+// Additional size that could be used for FDT entries added by the UEFI OS Loader
+#define FDT_ADDITIONAL_ENTRIES_SIZE 0x400
+
// Device Path representing an image in memory
#pragma pack(1)
typedef struct {
@@ -29,6 +39,12 @@ typedef struct {
} MEMORY_DEVICE_PATH;
#pragma pack()
+/* It's the value of arm64 efi stub kernel */
+#define KERNEL_IMAGE_STEXT_OFFSET 0x12C
+#define KERNEL_IMAGE_RAW_SIZE_OFFSET 0x130
+
+#define FDT_SIZE_OFFSET 0x4
+
STATIC CONST MEMORY_DEVICE_PATH MemoryDevicePathTemplate =
{
{
@@ -51,9 +67,114 @@ STATIC CONST MEMORY_DEVICE_PATH MemoryDevicePathTemplate =
};
EFI_STATUS
+PrepareFdt (
+ IN EFI_PHYSICAL_ADDRESS FdtBlobBase,
+ IN UINTN *FdtBlobSize,
+ IN OUT CHAR16 *KernelArgs
+ )
+{
+ VOID *fdt;
+ INTN err;
+ INTN node;
+ INT32 lenp;
+ CONST VOID *BootArg;
+ UINTN OriginalFdtSize;
+ EFI_STATUS Status;
+ EFI_PHYSICAL_ADDRESS NewFdtBlobBase;
+ UINTN NewFdtBlobSize;
+ CHAR16 Arg[BOOTIMG_KERNEL_ARGS_SIZE];
+ UINTN Size;
+
+ //
+ // Sanity checks on the original FDT blob.
+ //
+ err = fdt_check_header ((VOID*)(UINTN)FdtBlobBase);
+ if (err != 0) {
+ Print (L"ERROR: Device Tree header not valid (err:%d)\n", err);
+ return EFI_INVALID_PARAMETER;
+ }
+
+ // The original FDT blob might have been loaded partially.
+ // Check that it is not the case.
+ OriginalFdtSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)FdtBlobBase);
+ if (OriginalFdtSize > *FdtBlobSize) {
+ Print (L"ERROR: Incomplete FDT. Only %d/%d bytes have been loaded.\n",
+ FdtBlobSize, OriginalFdtSize);
+ return EFI_INVALID_PARAMETER;
+ }
+
+ //
+ // Relocate the FDT to its final location since some platform may update FDT.
+ //
+ Size = OriginalFdtSize + FDT_ADDITIONAL_ENTRIES_SIZE;
+ NewFdtBlobSize = ALIGN (Size, EFI_PAGE_SIZE);
+
+ // Try anywhere there is available space.
+ Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData,
+ EFI_SIZE_TO_PAGES (NewFdtBlobSize), &NewFdtBlobBase);
+ if (EFI_ERROR (Status)) {
+ ASSERT_EFI_ERROR (Status);
+ return EFI_OUT_OF_RESOURCES;
+ } else {
+ DEBUG ((EFI_D_WARN, "WARNING: Loaded FDT at random address 0x%lX.\nWARNING: There is a risk of accidental overwriting by other code/data.\n", NewFdtBlobBase));
+ }
+
+ // Load the Original FDT tree into the new region
+ err = fdt_open_into ((VOID*)(UINTN) FdtBlobBase,
+ (VOID*)(UINTN)(NewFdtBlobBase), NewFdtBlobSize);
+ if (err) {
+ DEBUG ((EFI_D_ERROR, "fdt_open_into(): %a\n", fdt_strerror (err)));
+ gBS->FreePages (NewFdtBlobBase, EFI_SIZE_TO_PAGES (NewFdtBlobSize));
+ return EFI_INVALID_PARAMETER;
+ }
+
+ // If we succeeded to generate the new Device Tree then free the old Device Tree
+ gBS->FreePages (FdtBlobBase, EFI_SIZE_TO_PAGES (OriginalFdtSize));
+
+ fdt = (VOID*)(UINTN)NewFdtBlobBase;
+
+ node = fdt_subnode_offset (fdt, 0, "chosen");
+ if (node < 0) {
+ // The 'chosen' node does not exist, create it
+ node = fdt_add_subnode (fdt, 0, "chosen");
+ if (node < 0) {
+ DEBUG ((EFI_D_ERROR, "Error on finding 'chosen' node\n"));
+ Status = EFI_INVALID_PARAMETER;
+ goto FAIL_COMPLETE_FDT;
+ }
+ }
+
+ // Merge bootargs into command line arguments
+ BootArg = fdt_getprop (fdt, node, "bootargs", &lenp);
+ if (BootArg != NULL) {
+ AsciiStrToUnicodeStrS (BootArg, Arg, BOOTIMG_KERNEL_ARGS_SIZE);
+ // StrCatS() is using the size of CHAR16
+ StrCatS (KernelArgs, BOOTIMG_KERNEL_ARGS_SIZE >> 1, L" ");
+ StrCatS (KernelArgs, BOOTIMG_KERNEL_ARGS_SIZE >> 1, Arg);
+ }
+
+ // Update the real size of the Device Tree
+ fdt_pack ((VOID*)(UINTN)(NewFdtBlobBase));
+
+ *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(NewFdtBlobBase));
+
+ Status = gBS->InstallConfigurationTable (
+ &gFdtTableGuid,
+ (VOID *)(UINTN)NewFdtBlobBase
+ );
+ return Status;
+
+FAIL_COMPLETE_FDT:
+ gBS->FreePages (NewFdtBlobBase, EFI_SIZE_TO_PAGES (NewFdtBlobSize));
+
+ return EFI_SUCCESS;
+}
+
+EFI_STATUS
BootAndroidBootImg (
- IN UINTN BufferSize,
- IN VOID *Buffer
+ IN FASTBOOT_PLATFORM_PROTOCOL *Platform,
+ IN UINTN BufferSize,
+ IN VOID *Buffer
)
{
EFI_STATUS Status;
@@ -63,7 +184,13 @@ BootAndroidBootImg (
VOID *Ramdisk;
UINTN RamdiskSize;
MEMORY_DEVICE_PATH KernelDevicePath;
- CHAR16 *LoadOptions, *NewLoadOptions;
+ EFI_HANDLE ImageHandle;
+ EFI_PHYSICAL_ADDRESS FdtBase;
+ UINTN FdtSize, Index;
+ UINT8 *FdtPtr;
+ VOID *NewKernelArg;
+ EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;
+ CHAR16 *PlatformKernelArgs;
Status = ParseAndroidBootImg (
Buffer,
@@ -77,45 +204,67 @@ BootAndroidBootImg (
return Status;
}
- KernelDevicePath = MemoryDevicePathTemplate;
+ /* Install Fdt that is attached at the end of kernel */
+ KernelSize = *(UINT32 *)((EFI_PHYSICAL_ADDRESS)(UINTN)Kernel + KERNEL_IMAGE_STEXT_OFFSET) +
+ *(UINT32 *)((EFI_PHYSICAL_ADDRESS)(UINTN)Kernel + KERNEL_IMAGE_RAW_SIZE_OFFSET);
- // Have to cast to UINTN before casting to EFI_PHYSICAL_ADDRESS in order to
- // appease GCC.
- KernelDevicePath.Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel;
- KernelDevicePath.Node1.EndingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel + KernelSize;
+ /* FDT is at the end of kernel image */
+ FdtBase = (EFI_PHYSICAL_ADDRESS)(UINTN)Kernel + KernelSize;
+ FdtPtr = (UINT8 *)(FdtBase + FDT_SIZE_OFFSET);
+ for (Index = 0, FdtSize = 0; Index < sizeof (UINT32); Index++) {
+ FdtSize |= *FdtPtr << ((sizeof (UINT32) - 1 - Index) * 8);
+ FdtPtr++;
+ }
- // Initialize Linux command line
- LoadOptions = CatSPrint (NULL, L"%a", KernelArgs);
- if (LoadOptions == NULL) {
+ NewKernelArg = AllocateZeroPool (BOOTIMG_KERNEL_ARGS_SIZE);
+ if (NewKernelArg == NULL) {
+ DEBUG ((DEBUG_ERROR, "Fail to allocate memory\n"));
return EFI_OUT_OF_RESOURCES;
}
- if (RamdiskSize != 0) {
- NewLoadOptions = CatSPrint (LoadOptions, L" initrd=0x%x,0x%x",
- (UINTN)Ramdisk, RamdiskSize);
- FreePool (LoadOptions);
- if (NewLoadOptions == NULL) {
- return EFI_OUT_OF_RESOURCES;
- }
- LoadOptions = NewLoadOptions;
+ // Set the ramdisk in command line arguments
+ UnicodeSPrint (
+ (CHAR16 *)NewKernelArg, BOOTIMG_KERNEL_ARGS_SIZE,
+ L"initrd=0x%x,0x%x ",
+ (UINTN)Ramdisk, (UINTN)RamdiskSize
+ );
+ // Merge kernel arguments from Android boot image into command line arguments
+ AsciiStrToUnicodeStrS (KernelArgs, NewKernelArg + StrLen (NewKernelArg) * sizeof (CHAR16), BOOTIMG_KERNEL_ARGS_SIZE >> 1);
+ // StrCatS() is using the size of CHAR16
+ StrCatS ((CHAR16 *)NewKernelArg, BOOTIMG_KERNEL_ARGS_SIZE >> 1, L" ");
+ // Merge platform arguemnts into command line arguments
+ PlatformKernelArgs = Platform->GetKernelArgs ();
+ if (PlatformKernelArgs) {
+ StrCatS ((CHAR16 *)NewKernelArg, BOOTIMG_KERNEL_ARGS_SIZE >> 1, PlatformKernelArgs);
}
- Status = BdsStartEfiApplication (gImageHandle,
- (EFI_DEVICE_PATH_PROTOCOL *) &KernelDevicePath,
- StrSize (LoadOptions),
- LoadOptions);
+ Status = PrepareFdt (FdtBase, &FdtSize, NewKernelArg);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Couldn't Boot Linux: %d\n", Status));
- Status = EFI_DEVICE_ERROR;
- goto FreeLoadOptions;
+ return EFI_DEVICE_ERROR;
+ FreePool (NewKernelArg);
+ return EFI_INVALID_PARAMETER;
}
- // If we got here we do a confused face because BootLinuxFdt returned,
- // reporting success.
- DEBUG ((EFI_D_ERROR, "WARNING: BdsBootLinuxFdt returned EFI_SUCCESS.\n"));
- return EFI_SUCCESS;
+ KernelDevicePath = MemoryDevicePathTemplate;
-FreeLoadOptions:
- FreePool (LoadOptions);
- return Status;
+ // Have to cast to UINTN before casting to EFI_PHYSICAL_ADDRESS in order to
+ // appease GCC.
+ KernelDevicePath.Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel;
+ KernelDevicePath.Node1.EndingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel + KernelSize;
+
+ Status = gBS->LoadImage (TRUE, gImageHandle, (EFI_DEVICE_PATH *)&KernelDevicePath, (VOID*)(UINTN)Kernel, KernelSize, &ImageHandle);
+
+ // Set kernel arguments
+ Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);
+ ImageInfo->LoadOptions = NewKernelArg;
+ ImageInfo->LoadOptionsSize = StrLen (NewKernelArg) * sizeof (CHAR16);
+
+ // Before calling the image, enable the Watchdog Timer for the 5 Minute period
+ gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);
+ // Start the image
+ Status = gBS->StartImage (ImageHandle, NULL, NULL);
+ // Clear the Watchdog Timer after the image returns
+ gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);
+ return EFI_SUCCESS;
}