acedd3e0e3cdd674a4794ca91f4c6a86abf7bbce
[mirror_edk2.git] / EmbeddedPkg / Application / AndroidFastboot / Arm / BootAndroidBootImg.c
1 /** @file
2
3 Copyright (c) 2013-2015, ARM Ltd. All rights reserved.<BR>
4
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "AndroidFastbootApp.h"
16
17 #include <Protocol/DevicePath.h>
18
19 #include <Library/BdsLib.h>
20 #include <Library/DevicePathLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/UefiLib.h>
23
24 #define LINUX_LOADER_COMMAND_LINE L"%s -f %s -c %s"
25
26 // This GUID is defined in the INGF file of ArmPkg/Application/LinuxLoader
27 CONST EFI_GUID mLinuxLoaderAppGuid = { 0x701f54f2, 0x0d70, 0x4b89, { 0xbc, 0x0a, 0xd9, 0xca, 0x25, 0x37, 0x90, 0x59 }};
28
29 // Device Path representing an image in memory
30 #pragma pack(1)
31 typedef struct {
32 MEMMAP_DEVICE_PATH Node1;
33 EFI_DEVICE_PATH_PROTOCOL End;
34 } MEMORY_DEVICE_PATH;
35 #pragma pack()
36
37 STATIC CONST MEMORY_DEVICE_PATH MemoryDevicePathTemplate =
38 {
39 {
40 {
41 HARDWARE_DEVICE_PATH,
42 HW_MEMMAP_DP,
43 {
44 (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
45 (UINT8)((sizeof (MEMMAP_DEVICE_PATH)) >> 8),
46 },
47 }, // Header
48 0, // StartingAddress (set at runtime)
49 0 // EndingAddress (set at runtime)
50 }, // Node1
51 {
52 END_DEVICE_PATH_TYPE,
53 END_ENTIRE_DEVICE_PATH_SUBTYPE,
54 { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }
55 } // End
56 };
57
58 EFI_STATUS
59 BootAndroidBootImg (
60 IN UINTN BufferSize,
61 IN VOID *Buffer
62 )
63 {
64 EFI_STATUS Status;
65 CHAR8 KernelArgs[BOOTIMG_KERNEL_ARGS_SIZE];
66 VOID *Kernel;
67 UINTN KernelSize;
68 VOID *Ramdisk;
69 UINTN RamdiskSize;
70 MEMORY_DEVICE_PATH KernelDevicePath;
71 MEMORY_DEVICE_PATH* RamdiskDevicePath;
72 CHAR16* KernelDevicePathTxt;
73 CHAR16* RamdiskDevicePathTxt;
74 EFI_DEVICE_PATH* LinuxLoaderDevicePath;
75 CHAR16* LoadOptions;
76
77 Status = ParseAndroidBootImg (
78 Buffer,
79 &Kernel,
80 &KernelSize,
81 &Ramdisk,
82 &RamdiskSize,
83 KernelArgs
84 );
85 if (EFI_ERROR (Status)) {
86 return Status;
87 }
88
89 KernelDevicePath = MemoryDevicePathTemplate;
90
91 // Have to cast to UINTN before casting to EFI_PHYSICAL_ADDRESS in order to
92 // appease GCC.
93 KernelDevicePath.Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel;
94 KernelDevicePath.Node1.EndingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel + KernelSize;
95
96 RamdiskDevicePath = NULL;
97 if (RamdiskSize != 0) {
98 RamdiskDevicePath = (MEMORY_DEVICE_PATH*)DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL*) &MemoryDevicePathTemplate);
99
100 RamdiskDevicePath->Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Ramdisk;
101 RamdiskDevicePath->Node1.EndingAddress = ((EFI_PHYSICAL_ADDRESS)(UINTN) Ramdisk) + RamdiskSize;
102 }
103
104 //
105 // Boot Linux using the Legacy Linux Loader
106 //
107
108 Status = LocateEfiApplicationInFvByGuid (&mLinuxLoaderAppGuid, &LinuxLoaderDevicePath);
109 if (EFI_ERROR (Status)) {
110 Print (L"Couldn't Boot Linux: %d\n", Status);
111 return EFI_DEVICE_ERROR;
112 }
113
114 KernelDevicePathTxt = ConvertDevicePathToText ((EFI_DEVICE_PATH_PROTOCOL *) &KernelDevicePath, FALSE, FALSE);
115 if (KernelDevicePathTxt == NULL) {
116 return EFI_OUT_OF_RESOURCES;
117 }
118
119 RamdiskDevicePathTxt = ConvertDevicePathToText ((EFI_DEVICE_PATH_PROTOCOL *) RamdiskDevicePath, FALSE, FALSE);
120 if (RamdiskDevicePathTxt == NULL) {
121 return EFI_OUT_OF_RESOURCES;
122 }
123
124 // Initialize Legacy Linux loader command line
125 LoadOptions = CatSPrint (NULL, LINUX_LOADER_COMMAND_LINE, KernelDevicePathTxt, RamdiskDevicePathTxt, KernelArgs);
126 if (LoadOptions == NULL) {
127 return EFI_OUT_OF_RESOURCES;
128 }
129
130 Status = BdsStartEfiApplication (gImageHandle, LinuxLoaderDevicePath, StrSize (LoadOptions), LoadOptions);
131 if (EFI_ERROR (Status)) {
132 DEBUG ((EFI_D_ERROR, "Couldn't Boot Linux: %d\n", Status));
133 return EFI_DEVICE_ERROR;
134 }
135
136 if (RamdiskDevicePath) {
137 FreePool (RamdiskDevicePathTxt);
138 FreePool (RamdiskDevicePath);
139 }
140
141 FreePool (KernelDevicePathTxt);
142
143 // If we got here we do a confused face because BootLinuxFdt returned,
144 // reporting success.
145 DEBUG ((EFI_D_ERROR, "WARNING: BdsBootLinuxFdt returned EFI_SUCCESS.\n"));
146 return EFI_SUCCESS;
147 }