--- /dev/null
+/** @file\r
+\r
+ Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>\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 "AndroidFastbootApp.h"\r
+\r
+#define BOOT_MAGIC "ANDROID!"\r
+#define BOOT_MAGIC_LENGTH sizeof (BOOT_MAGIC) - 1\r
+\r
+// Check Val (unsigned) is a power of 2 (has only one bit set)\r
+#define IS_POWER_OF_2(Val) (Val != 0 && ((Val & (Val - 1)) == 0))\r
+\r
+// No documentation for this really - sizes of fields has been determined\r
+// empirically.\r
+#pragma pack(1)\r
+typedef struct {\r
+ CHAR8 BootMagic[BOOT_MAGIC_LENGTH];\r
+ UINT32 KernelSize;\r
+ UINT32 KernelAddress;\r
+ UINT32 RamdiskSize;\r
+ UINT32 RamdiskAddress;\r
+ UINT32 SecondStageBootloaderSize;\r
+ UINT32 SecondStageBootloaderAddress;\r
+ UINT32 KernelTaggsAddress;\r
+ UINT32 PageSize;\r
+ UINT32 Reserved[2];\r
+ CHAR8 ProductName[16];\r
+ CHAR8 KernelArgs[BOOTIMG_KERNEL_ARGS_SIZE];\r
+ UINT32 Id[32];\r
+} ANDROID_BOOTIMG_HEADER;\r
+#pragma pack()\r
+\r
+// Find the kernel and ramdisk in an Android boot.img.\r
+// return EFI_INVALID_PARAMTER if the boot.img is invalid (i.e. doesn't have the\r
+// right magic value),\r
+// return EFI_NOT_FOUND if there was no kernel in the boot.img.\r
+// Note that the Ramdisk is optional - *Ramdisk won't be touched if it isn't\r
+// present, but RamdiskSize will be set to 0.\r
+EFI_STATUS\r
+ParseAndroidBootImg (\r
+ IN VOID *BootImg,\r
+ OUT VOID **Kernel,\r
+ OUT UINTN *KernelSize,\r
+ OUT VOID **Ramdisk,\r
+ OUT UINTN *RamdiskSize,\r
+ OUT CHAR8 *KernelArgs\r
+ )\r
+{\r
+ ANDROID_BOOTIMG_HEADER *Header;\r
+ UINT8 *BootImgBytePtr;\r
+\r
+ // Cast to UINT8 so we can do pointer arithmetic\r
+ BootImgBytePtr = (UINT8 *) BootImg;\r
+\r
+ Header = (ANDROID_BOOTIMG_HEADER *) BootImg;\r
+\r
+ if (AsciiStrnCmp (Header->BootMagic, BOOT_MAGIC, BOOT_MAGIC_LENGTH) != 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (Header->KernelSize == 0) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ ASSERT (IS_POWER_OF_2 (Header->PageSize));\r
+\r
+ *KernelSize = Header->KernelSize;\r
+ *Kernel = BootImgBytePtr + Header->PageSize;\r
+ *RamdiskSize = Header->RamdiskSize;\r
+\r
+ if (Header->RamdiskSize != 0) {\r
+ *Ramdisk = (VOID *) (BootImgBytePtr\r
+ + Header->PageSize\r
+ + ALIGN_VALUE (Header->KernelSize, Header->PageSize));\r
+ }\r
+\r
+ AsciiStrnCpy (KernelArgs, Header->KernelArgs, BOOTIMG_KERNEL_ARGS_SIZE);\r
+\r
+ return EFI_SUCCESS;\r
+}\r