1 /** @file
2 
3   Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
4 
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include "AndroidFastbootApp.h"
10 
11 // Find the kernel and ramdisk in an Android boot.img.
12 // return EFI_INVALID_PARAMETER if the boot.img is invalid (i.e. doesn't have the
13 //  right magic value),
14 // return EFI_NOT_FOUND if there was no kernel in the boot.img.
15 // Note that the Ramdisk is optional - *Ramdisk won't be touched if it isn't
16 // present, but RamdiskSize will be set to 0.
17 EFI_STATUS
ParseAndroidBootImg(IN VOID * BootImg,OUT VOID ** Kernel,OUT UINTN * KernelSize,OUT VOID ** Ramdisk,OUT UINTN * RamdiskSize,OUT CHAR8 * KernelArgs)18 ParseAndroidBootImg (
19   IN  VOID    *BootImg,
20   OUT VOID   **Kernel,
21   OUT UINTN   *KernelSize,
22   OUT VOID   **Ramdisk,
23   OUT UINTN   *RamdiskSize,
24   OUT CHAR8   *KernelArgs
25   )
26 {
27   ANDROID_BOOTIMG_HEADER   *Header;
28   UINT8                    *BootImgBytePtr;
29 
30   // Cast to UINT8 so we can do pointer arithmetic
31   BootImgBytePtr = (UINT8 *) BootImg;
32 
33   Header = (ANDROID_BOOTIMG_HEADER *) BootImg;
34 
35   if (AsciiStrnCmp ((CONST CHAR8 *)Header->BootMagic, ANDROID_BOOT_MAGIC,
36                     ANDROID_BOOT_MAGIC_LENGTH) != 0) {
37     return EFI_INVALID_PARAMETER;
38   }
39 
40   if (Header->KernelSize == 0) {
41     return EFI_NOT_FOUND;
42   }
43 
44   ASSERT (IS_VALID_ANDROID_PAGE_SIZE (Header->PageSize));
45 
46   *KernelSize = Header->KernelSize;
47   *Kernel = BootImgBytePtr + Header->PageSize;
48   *RamdiskSize = Header->RamdiskSize;
49 
50   if (Header->RamdiskSize != 0) {
51     *Ramdisk = (VOID *) (BootImgBytePtr
52                  + Header->PageSize
53                  + ALIGN_VALUE (Header->KernelSize, Header->PageSize));
54   }
55 
56   AsciiStrnCpyS (KernelArgs, ANDROID_BOOTIMG_KERNEL_ARGS_SIZE, Header->KernelArgs,
57     ANDROID_BOOTIMG_KERNEL_ARGS_SIZE);
58 
59   return EFI_SUCCESS;
60 }
61