From nobody Sat Nov 2 14:24:44 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1488350190746862.7178290971761; Tue, 28 Feb 2017 22:36:30 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 991A2821D4; Tue, 28 Feb 2017 22:36:29 -0800 (PST) Received: from mail-pf0-x232.google.com (mail-pf0-x232.google.com [IPv6:2607:f8b0:400e:c00::232]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 67A0781F5C for ; Tue, 28 Feb 2017 22:36:28 -0800 (PST) Received: by mail-pf0-x232.google.com with SMTP id x66so8062030pfb.3 for ; Tue, 28 Feb 2017 22:36:28 -0800 (PST) Received: from localhost.localdomain ([45.56.159.97]) by smtp.gmail.com with ESMTPSA id r17sm7910668pgg.19.2017.02.28.22.36.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 28 Feb 2017 22:36:27 -0800 (PST) X-Original-To: edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=mswTDVYLvup9MWw4lPnRZ5QzqPmq7KFrUkxIKXmjmiI=; b=ZZ251n155c/thwNBAfle710VoYPLm2Xy5B9eiMp8iV6waCbbZvPdtfN8/1qvkQ5drE WJnaVgaaVsEqsw7lvAZ2qGX6xFvCbdUIZfFunvgtz+c03hR+oCrNM/usCUsNvOw+t9yJ W//6Z8fRx0vILpnBdtJ6buq8zN8wplh7bc/yA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=mswTDVYLvup9MWw4lPnRZ5QzqPmq7KFrUkxIKXmjmiI=; b=q7ejaelKyY0CoEjuFA4GRTQDsmP3TCtihIDLSUg93rVkQbqlh8gPGsm4DS/R54eq5N NahMUQ+ujjTqz/dh1jS87kuBnj9KBYVpJ+F1gwxrRW1zpBwM6kRvSSWwkbuKZah0jH+q SokvA8oxJxklKpLT5tTvkT1JeymGns73e8IWopIH067mSAPn77ahHoeaoWaEB4zHe02N 46Vh+1JcKo6KYxigCIGItzmLoes9pBGzQ+srr1ICU8HPHfYXruyct/9ELHax73QqjFdP Oh83FMIVz5DXSMYL2wRF8mYOOvGZwI8oQT3jkAmBKkY0Eg9Ijm2NVRYo5VGlkUsd/g9O Ek5A== X-Gm-Message-State: AMke39l/h+jl60Y+TcjRv94c15kA0rOq2OlEAiWM0RXtOhJuFSIKXdpOQTrlEPsL1n3aDcoz X-Received: by 10.84.196.164 with SMTP id l33mr8302761pld.0.1488350187817; Tue, 28 Feb 2017 22:36:27 -0800 (PST) From: Haojian Zhuang To: leif.lindholm@linaro.org, ard.biesheuvel@linaro.org, edk2-devel@lists.01.org, linaro-uefi@lists.linaro.org Date: Wed, 1 Mar 2017 14:36:07 +0800 Message-Id: <1488350167-30752-5-git-send-email-haojian.zhuang@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488350167-30752-1-git-send-email-haojian.zhuang@linaro.org> References: <1488350167-30752-1-git-send-email-haojian.zhuang@linaro.org> Subject: [edk2] [PATCH 4/4] EmbeddedPkg/AndroidBoot: boot android kernel from storage X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haojian Zhuang MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add an android kernel loader that could load kernel from storage device. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Haojian Zhuang --- .../Application/AndroidBoot/AndroidBootApp.c | 118 +++++++++ .../Application/AndroidBoot/AndroidBootApp.h | 44 ++++ .../Application/AndroidBoot/AndroidBootApp.inf | 67 ++++++ .../Application/AndroidBoot/AndroidBootImg.c | 91 +++++++ .../AndroidBoot/Arm/BootAndroidBootImg.c | 268 +++++++++++++++++= ++++ 5 files changed, 588 insertions(+) create mode 100644 EmbeddedPkg/Application/AndroidBoot/AndroidBootApp.c create mode 100644 EmbeddedPkg/Application/AndroidBoot/AndroidBootApp.h create mode 100644 EmbeddedPkg/Application/AndroidBoot/AndroidBootApp.inf create mode 100644 EmbeddedPkg/Application/AndroidBoot/AndroidBootImg.c create mode 100644 EmbeddedPkg/Application/AndroidBoot/Arm/BootAndroidBoot= Img.c diff --git a/EmbeddedPkg/Application/AndroidBoot/AndroidBootApp.c b/Embedde= dPkg/Application/AndroidBoot/AndroidBootApp.c new file mode 100644 index 0000000..312bfd7 --- /dev/null +++ b/EmbeddedPkg/Application/AndroidBoot/AndroidBootApp.c @@ -0,0 +1,118 @@ +/** @file + + Copyright (c) 2013-2014, ARM Ltd. All rights reserved.
+ Copyright (c) 2017, Linaro. All rights reserved. + + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. + +**/ + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "AndroidBootApp.h" + +#define IS_DEVICE_PATH_NODE(node,type,subtype) (((node)->Type =3D=3D (type= )) && ((node)->SubType =3D=3D (subtype))) + +STATIC FASTBOOT_PLATFORM_PROTOCOL *mPlatform; + +EFI_STATUS +EFIAPI +AndroidBootAppEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + CHAR16 *BootPathStr; + EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *EfiDevicePathFromTextProtocol; + EFI_DEVICE_PATH *DevicePath; + EFI_DEVICE_PATH_PROTOCOL *Node, *NextNode; + EFI_BLOCK_IO_PROTOCOL *BlockIo; + HARDDRIVE_DEVICE_PATH *PartitionPath; + UINT32 MediaId, BlockSize; + VOID *Buffer; + EFI_HANDLE Handle; + + BootPathStr =3D (CHAR16 *)PcdGetPtr (PcdAndroidBootDevicePath); + ASSERT (BootPathStr !=3D NULL); + Status =3D gBS->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid, NUL= L, (VOID **)&EfiDevicePathFromTextProtocol); + ASSERT_EFI_ERROR(Status); + DevicePath =3D (EFI_DEVICE_PATH *)EfiDevicePathFromTextProtocol->Convert= TextToDevicePath (BootPathStr); + ASSERT (DevicePath !=3D NULL); + + /* Find DevicePath node of Partition */ + NextNode =3D DevicePath; + while (1) { + Node =3D NextNode; + if (IS_DEVICE_PATH_NODE (Node, MEDIA_DEVICE_PATH, MEDIA_HARDDRIVE_DP))= { + PartitionPath =3D (HARDDRIVE_DEVICE_PATH *)Node; + break; + } + NextNode =3D NextDevicePathNode (Node); + } + + Status =3D gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &DevicePa= th, &Handle); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D gBS->OpenProtocol ( + Handle, + &gEfiBlockIoProtocolGuid, + (VOID **) &BlockIo, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Failed to get BlockIo: %r\n", Status)); + return Status; + } + + MediaId =3D BlockIo->Media->MediaId; + BlockSize =3D BlockIo->Media->BlockSize; + /* Both PartitionStart and PartitionSize are counted as block size. */ + Buffer =3D AllocatePages (EFI_SIZE_TO_PAGES (PartitionPath->PartitionSiz= e)); + if (Buffer =3D=3D NULL) { + return EFI_BUFFER_TOO_SMALL; + } + + /* Load header of boot.img */ + Status =3D BlockIo->ReadBlocks ( + BlockIo, + MediaId, + PartitionPath->PartitionStart / BlockSize, + PartitionPath->PartitionSize, + Buffer + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Failed to read blocks: %r\n", Status)); + goto EXIT; + } + + Status =3D gBS->LocateProtocol (&gAndroidFastbootPlatformProtocolGuid, N= ULL, (VOID **) &mPlatform); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Fastboot: Couldn't open Fastboot Platform Protoc= ol: %r\n", Status)); + goto EXIT; + } + + Status =3D BootAndroidBootImg (mPlatform, PartitionPath->PartitionSize, = Buffer); + +EXIT: + FreePages (Buffer, EFI_SIZE_TO_PAGES (PartitionPath->PartitionSize)); + return Status; +} diff --git a/EmbeddedPkg/Application/AndroidBoot/AndroidBootApp.h b/Embedde= dPkg/Application/AndroidBoot/AndroidBootApp.h new file mode 100644 index 0000000..ed7ea6e --- /dev/null +++ b/EmbeddedPkg/Application/AndroidBoot/AndroidBootApp.h @@ -0,0 +1,44 @@ +/** @file + + Copyright (c) 2013-2014, ARM Ltd. All rights reserved.
+ Copyright (c) 2017, Linaro. + + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. + +**/ + +#ifndef __ANDROID_BOOT_APP_H__ +#define __ANDROID_BOOT_APP_H__ + +#include +#include +#include + +#include + +#define BOOTIMG_KERNEL_ARGS_SIZE 1024 + +EFI_STATUS +BootAndroidBootImg ( + 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 + ); + +#endif //ifdef __ANDROID_BOOT_APP_H__ diff --git a/EmbeddedPkg/Application/AndroidBoot/AndroidBootApp.inf b/Embed= dedPkg/Application/AndroidBoot/AndroidBootApp.inf new file mode 100644 index 0000000..176f778 --- /dev/null +++ b/EmbeddedPkg/Application/AndroidBoot/AndroidBootApp.inf @@ -0,0 +1,67 @@ +#/** @file +# +# Copyright (c) 2013-2015, ARM Ltd. All rights reserved.
+# Copyright (c) 2017, Linaro. All rights reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD License +# which accompanies this distribution. The full text of the license may b= e found at +# http://opensource.org/licenses/bsd-license.php +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IM= PLIED. +# +# +#**/ + +[Defines] + INF_VERSION =3D 0x00010019 + BASE_NAME =3D AndroidBootApp + FILE_GUID =3D 3a738b36-b9c5-4763-abbd-6cbd4b25f9ff + MODULE_TYPE =3D UEFI_APPLICATION + VERSION_STRING =3D 1.0 + ENTRY_POINT =3D AndroidBootAppEntryPoint + +[Sources.common] + AndroidBootApp.c + AndroidBootImg.c + +[Sources.ARM, Sources.AARCH64] + Arm/BootAndroidBootImg.c + +[LibraryClasses] + BaseLib + BaseMemoryLib + BdsLib + DebugLib + DevicePathLib + DxeServicesTableLib + FdtLib + MemoryAllocationLib + PcdLib + PrintLib + UefiApplicationEntryPoint + UefiBootServicesTableLib + UefiLib + UefiRuntimeServicesTableLib + +[Protocols] + gAndroidFastbootPlatformProtocolGuid + gEfiBlockIoProtocolGuid + gEfiDevicePathFromTextProtocolGuid + gEfiSimpleTextOutProtocolGuid + gEfiSimpleTextInProtocolGuid + +[Packages] + EmbeddedPkg/EmbeddedPkg.dec + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + +[Packages.ARM, Packages.AARCH64] + ArmPkg/ArmPkg.dec + ArmPlatformPkg/ArmPlatformPkg.dec + +[Guids] + gFdtTableGuid + +[Pcd] + gEmbeddedTokenSpaceGuid.PcdAndroidBootDevicePath diff --git a/EmbeddedPkg/Application/AndroidBoot/AndroidBootImg.c b/Embedde= dPkg/Application/AndroidBoot/AndroidBootImg.c new file mode 100644 index 0000000..abfd8c6 --- /dev/null +++ b/EmbeddedPkg/Application/AndroidBoot/AndroidBootImg.c @@ -0,0 +1,91 @@ +/** @file + + Copyright (c) 2013-2014, ARM Ltd. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. + +**/ + +#include "AndroidBootApp.h" + +#define BOOT_MAGIC "ANDROID!" +#define BOOT_MAGIC_LENGTH sizeof (BOOT_MAGIC) - 1 + +// Check Val (unsigned) is a power of 2 (has only one bit set) +#define IS_POWER_OF_2(Val) (Val !=3D 0 && ((Val & (Val - 1)) =3D=3D 0)) + +// No documentation for this really - sizes of fields has been determined +// empirically. +#pragma pack(1) +typedef struct { + CHAR8 BootMagic[BOOT_MAGIC_LENGTH]; + UINT32 KernelSize; + UINT32 KernelAddress; + UINT32 RamdiskSize; + UINT32 RamdiskAddress; + UINT32 SecondStageBootloaderSize; + UINT32 SecondStageBootloaderAddress; + UINT32 KernelTaggsAddress; + UINT32 PageSize; + UINT32 Reserved[2]; + CHAR8 ProductName[16]; + CHAR8 KernelArgs[BOOTIMG_KERNEL_ARGS_SIZE]; + UINT32 Id[32]; +} ANDROID_BOOTIMG_HEADER; +#pragma pack() + +// Find the kernel and ramdisk in an Android boot.img. +// return EFI_INVALID_PARAMTER if the boot.img is invalid (i.e. doesn't ha= ve the +// right magic value), +// return EFI_NOT_FOUND if there was no kernel in the boot.img. +// Note that the Ramdisk is optional - *Ramdisk won't be touched if it isn= 't +// present, but RamdiskSize will be set to 0. +EFI_STATUS +ParseAndroidBootImg ( + IN VOID *BootImg, + OUT VOID **Kernel, + OUT UINTN *KernelSize, + OUT VOID **Ramdisk, + OUT UINTN *RamdiskSize, + OUT CHAR8 *KernelArgs + ) +{ + ANDROID_BOOTIMG_HEADER *Header; + UINT8 *BootImgBytePtr; + + // Cast to UINT8 so we can do pointer arithmetic + BootImgBytePtr =3D (UINT8 *) BootImg; + + Header =3D (ANDROID_BOOTIMG_HEADER *) BootImg; + + if (AsciiStrnCmp (Header->BootMagic, BOOT_MAGIC, BOOT_MAGIC_LENGTH) !=3D= 0) { + return EFI_INVALID_PARAMETER; + } + + if (Header->KernelSize =3D=3D 0) { + return EFI_NOT_FOUND; + } + + ASSERT (IS_POWER_OF_2 (Header->PageSize)); + + *KernelSize =3D Header->KernelSize; + *Kernel =3D BootImgBytePtr + Header->PageSize; + *RamdiskSize =3D Header->RamdiskSize; + + if (Header->RamdiskSize !=3D 0) { + *Ramdisk =3D (VOID *) (BootImgBytePtr + + Header->PageSize + + ALIGN_VALUE (Header->KernelSize, Header->PageSize)); + } + + AsciiStrnCpyS (KernelArgs, BOOTIMG_KERNEL_ARGS_SIZE, Header->KernelArgs, + BOOTIMG_KERNEL_ARGS_SIZE); + + return EFI_SUCCESS; +} diff --git a/EmbeddedPkg/Application/AndroidBoot/Arm/BootAndroidBootImg.c b= /EmbeddedPkg/Application/AndroidBoot/Arm/BootAndroidBootImg.c new file mode 100644 index 0000000..87b657d --- /dev/null +++ b/EmbeddedPkg/Application/AndroidBoot/Arm/BootAndroidBootImg.c @@ -0,0 +1,268 @@ +/** @file + + Copyright (c) 2013-2015, ARM Ltd. All rights reserved.
+ Copyright (c) 2017, Linaro. All rights reserved. + + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. + +**/ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "AndroidBootApp.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 { + MEMMAP_DEVICE_PATH Node1; + EFI_DEVICE_PATH_PROTOCOL End; +} 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 =3D +{ + { + { + HARDWARE_DEVICE_PATH, + HW_MEMMAP_DP, + { + (UINT8)(sizeof (MEMMAP_DEVICE_PATH)), + (UINT8)((sizeof (MEMMAP_DEVICE_PATH)) >> 8), + }, + }, // Header + 0, // StartingAddress (set at runtime) + 0 // EndingAddress (set at runtime) + }, // Node1 + { + END_DEVICE_PATH_TYPE, + END_ENTIRE_DEVICE_PATH_SUBTYPE, + { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 } + } // End +}; + +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 =3D fdt_check_header ((VOID*)(UINTN)FdtBlobBase); + if (err !=3D 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 =3D (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 =3D OriginalFdtSize + FDT_ADDITIONAL_ENTRIES_SIZE; + NewFdtBlobSize =3D ALIGN (Size, EFI_PAGE_SIZE); + + // Try anywhere there is available space. + Status =3D 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.\nWAR= NING: There is a risk of accidental overwriting by other code/data.\n", New= FdtBlobBase)); + } + + // Load the Original FDT tree into the new region + err =3D 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 Dev= ice Tree + gBS->FreePages (FdtBlobBase, EFI_SIZE_TO_PAGES (OriginalFdtSize)); + + fdt =3D (VOID*)(UINTN)NewFdtBlobBase; + + node =3D fdt_subnode_offset (fdt, 0, "chosen"); + if (node < 0) { + // The 'chosen' node does not exist, create it + node =3D fdt_add_subnode (fdt, 0, "chosen"); + if (node < 0) { + DEBUG ((EFI_D_ERROR, "Error on finding 'chosen' node\n")); + Status =3D EFI_INVALID_PARAMETER; + goto FAIL_COMPLETE_FDT; + } + } + + // Merge bootargs into command line arguments + BootArg =3D fdt_getprop (fdt, node, "bootargs", &lenp); + if (BootArg !=3D 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 =3D (UINTN)fdt_totalsize ((VOID*)(UINTN)(NewFdtBlobBase)); + + Status =3D 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 FASTBOOT_PLATFORM_PROTOCOL *Platform, + IN UINTN BufferSize, + IN VOID *Buffer + ) +{ + EFI_STATUS Status; + CHAR8 KernelArgs[BOOTIMG_KERNEL_ARGS_SIZE]; + VOID *Kernel; + UINTN KernelSize; + VOID *Ramdisk; + UINTN RamdiskSize; + MEMORY_DEVICE_PATH KernelDevicePath; + EFI_HANDLE ImageHandle; + EFI_PHYSICAL_ADDRESS FdtBase; + UINTN FdtSize, Index; + UINT8 *FdtPtr; + VOID *NewKernelArg; + EFI_LOADED_IMAGE_PROTOCOL *ImageInfo; + CHAR16 *PlatformKernelArgs; + + Status =3D ParseAndroidBootImg ( + Buffer, + &Kernel, + &KernelSize, + &Ramdisk, + &RamdiskSize, + KernelArgs + ); + if (EFI_ERROR (Status)) { + return Status; + } + + /* Install Fdt that is attached at the end of kernel */ + KernelSize =3D *(UINT32 *)((EFI_PHYSICAL_ADDRESS)(UINTN)Kernel + KERNEL_= IMAGE_STEXT_OFFSET) + + *(UINT32 *)((EFI_PHYSICAL_ADDRESS)(UINTN)Kernel + KERNEL_IM= AGE_RAW_SIZE_OFFSET); + + /* FDT is at the end of kernel image */ + FdtBase =3D (EFI_PHYSICAL_ADDRESS)(UINTN)Kernel + KernelSize; + FdtPtr =3D (UINT8 *)(FdtBase + FDT_SIZE_OFFSET); + for (Index =3D 0, FdtSize =3D 0; Index < sizeof (UINT32); Index++) { + FdtSize |=3D *FdtPtr << ((sizeof (UINT32) - 1 - Index) * 8); + FdtPtr++; + } + + NewKernelArg =3D AllocateZeroPool (BOOTIMG_KERNEL_ARGS_SIZE); + if (NewKernelArg =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "Fail to allocate memory\n")); + return EFI_OUT_OF_RESOURCES; + } + + // Set the ramdisk in command line arguments + UnicodeSPrint ( + (CHAR16 *)NewKernelArg, BOOTIMG_KERNEL_ARGS_SIZE, + L"initrd=3D0x%x,0x%x ", + (UINTN)Ramdisk, (UINTN)RamdiskSize + ); + // Merge kernel arguments from Android boot image into command line argu= ments + 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 =3D Platform->GetKernelArgs (); + if (PlatformKernelArgs) { + StrCatS ((CHAR16 *)NewKernelArg, BOOTIMG_KERNEL_ARGS_SIZE >> 1, Platfo= rmKernelArgs); + } + Status =3D PrepareFdt (FdtBase, &FdtSize, NewKernelArg); + if (EFI_ERROR (Status)) { + FreePool (NewKernelArg); + return EFI_INVALID_PARAMETER; + } + + KernelDevicePath =3D MemoryDevicePathTemplate; + + // Have to cast to UINTN before casting to EFI_PHYSICAL_ADDRESS in order= to + // appease GCC. + KernelDevicePath.Node1.StartingAddress =3D (EFI_PHYSICAL_ADDRESS)(UINTN)= Kernel; + KernelDevicePath.Node1.EndingAddress =3D (EFI_PHYSICAL_ADDRESS)(UINTN)= Kernel + KernelSize; + + Status =3D gBS->LoadImage (TRUE, gImageHandle, (EFI_DEVICE_PATH *)&Kerne= lDevicePath, (VOID*)(UINTN)Kernel, KernelSize, &ImageHandle); + + // Set kernel arguments + Status =3D gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGui= d, (VOID **) &ImageInfo); + ImageInfo->LoadOptions =3D NewKernelArg; + ImageInfo->LoadOptionsSize =3D 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 =3D gBS->StartImage (ImageHandle, NULL, NULL); + // Clear the Watchdog Timer after the image returns + gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL); + return EFI_SUCCESS; +} --=20 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel