]>
Commit | Line | Data |
---|---|---|
f6755908 OM |
1 | /** @file\r |
2 | \r | |
bd9a5182 | 3 | Copyright (c) 2013-2015, ARM Ltd. All rights reserved.<BR>\r |
f6755908 | 4 | \r |
878b807a | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
f6755908 OM |
6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include "AndroidFastbootApp.h"\r | |
10 | \r | |
11 | #include <Protocol/DevicePath.h>\r | |
fcf41edf | 12 | #include <Protocol/LoadedImage.h>\r |
f6755908 | 13 | \r |
f6755908 | 14 | #include <Library/DevicePathLib.h>\r |
bd9a5182 OM |
15 | #include <Library/UefiBootServicesTableLib.h>\r |
16 | #include <Library/UefiLib.h>\r | |
f6755908 | 17 | \r |
f6755908 OM |
18 | // Device Path representing an image in memory\r |
19 | #pragma pack(1)\r | |
20 | typedef struct {\r | |
21 | MEMMAP_DEVICE_PATH Node1;\r | |
22 | EFI_DEVICE_PATH_PROTOCOL End;\r | |
23 | } MEMORY_DEVICE_PATH;\r | |
24 | #pragma pack()\r | |
25 | \r | |
26 | STATIC CONST MEMORY_DEVICE_PATH MemoryDevicePathTemplate =\r | |
27 | {\r | |
28 | {\r | |
29 | {\r | |
30 | HARDWARE_DEVICE_PATH,\r | |
31 | HW_MEMMAP_DP,\r | |
32 | {\r | |
33 | (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),\r | |
34 | (UINT8)((sizeof (MEMMAP_DEVICE_PATH)) >> 8),\r | |
35 | },\r | |
36 | }, // Header\r | |
37 | 0, // StartingAddress (set at runtime)\r | |
38 | 0 // EndingAddress (set at runtime)\r | |
39 | }, // Node1\r | |
40 | {\r | |
41 | END_DEVICE_PATH_TYPE,\r | |
42 | END_ENTIRE_DEVICE_PATH_SUBTYPE,\r | |
b0fdce95 | 43 | { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }\r |
f6755908 OM |
44 | } // End\r |
45 | };\r | |
46 | \r | |
fcf41edf AB |
47 | \r |
48 | /**\r | |
49 | Start an EFI Application from a Device Path\r | |
50 | \r | |
51 | @param ParentImageHandle Handle of the calling image\r | |
52 | @param DevicePath Location of the EFI Application\r | |
53 | \r | |
54 | @retval EFI_SUCCESS All drivers have been connected\r | |
55 | @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found\r | |
56 | @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.\r | |
57 | \r | |
58 | **/\r | |
59 | STATIC\r | |
60 | EFI_STATUS\r | |
61 | StartEfiApplication (\r | |
62 | IN EFI_HANDLE ParentImageHandle,\r | |
63 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r | |
64 | IN UINTN LoadOptionsSize,\r | |
65 | IN VOID* LoadOptions\r | |
66 | )\r | |
67 | {\r | |
68 | EFI_STATUS Status;\r | |
69 | EFI_HANDLE ImageHandle;\r | |
70 | EFI_LOADED_IMAGE_PROTOCOL* LoadedImage;\r | |
71 | \r | |
72 | // Load the image from the device path with Boot Services function\r | |
73 | Status = gBS->LoadImage (TRUE, ParentImageHandle, DevicePath, NULL, 0,\r | |
74 | &ImageHandle);\r | |
75 | if (EFI_ERROR (Status)) {\r | |
82e0c422 DB |
76 | //\r |
77 | // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created\r | |
78 | // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.\r | |
79 | // If the caller doesn't have the option to defer the execution of an image, we should\r | |
80 | // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.\r | |
81 | //\r | |
82 | if (Status == EFI_SECURITY_VIOLATION) {\r | |
83 | gBS->UnloadImage (ImageHandle);\r | |
84 | }\r | |
fcf41edf AB |
85 | return Status;\r |
86 | }\r | |
87 | \r | |
88 | // Passed LoadOptions to the EFI Application\r | |
89 | if (LoadOptionsSize != 0) {\r | |
90 | Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid,\r | |
91 | (VOID **) &LoadedImage);\r | |
92 | if (EFI_ERROR (Status)) {\r | |
93 | return Status;\r | |
94 | }\r | |
95 | \r | |
96 | LoadedImage->LoadOptionsSize = LoadOptionsSize;\r | |
97 | LoadedImage->LoadOptions = LoadOptions;\r | |
98 | }\r | |
99 | \r | |
100 | // Before calling the image, enable the Watchdog Timer for the 5 Minute period\r | |
101 | gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r | |
102 | // Start the image\r | |
103 | Status = gBS->StartImage (ImageHandle, NULL, NULL);\r | |
104 | // Clear the Watchdog Timer after the image returns\r | |
105 | gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r | |
106 | \r | |
107 | return Status;\r | |
108 | }\r | |
109 | \r | |
f6755908 OM |
110 | EFI_STATUS\r |
111 | BootAndroidBootImg (\r | |
112 | IN UINTN BufferSize,\r | |
113 | IN VOID *Buffer\r | |
114 | )\r | |
115 | {\r | |
f6755908 | 116 | EFI_STATUS Status;\r |
6e414300 | 117 | CHAR8 KernelArgs[ANDROID_BOOTIMG_KERNEL_ARGS_SIZE];\r |
f6755908 OM |
118 | VOID *Kernel;\r |
119 | UINTN KernelSize;\r | |
120 | VOID *Ramdisk;\r | |
121 | UINTN RamdiskSize;\r | |
122 | MEMORY_DEVICE_PATH KernelDevicePath;\r | |
34e0bcea | 123 | CHAR16 *LoadOptions, *NewLoadOptions;\r |
f6755908 OM |
124 | \r |
125 | Status = ParseAndroidBootImg (\r | |
126 | Buffer,\r | |
127 | &Kernel,\r | |
128 | &KernelSize,\r | |
129 | &Ramdisk,\r | |
130 | &RamdiskSize,\r | |
131 | KernelArgs\r | |
132 | );\r | |
133 | if (EFI_ERROR (Status)) {\r | |
134 | return Status;\r | |
135 | }\r | |
136 | \r | |
137 | KernelDevicePath = MemoryDevicePathTemplate;\r | |
138 | \r | |
139 | // Have to cast to UINTN before casting to EFI_PHYSICAL_ADDRESS in order to\r | |
140 | // appease GCC.\r | |
141 | KernelDevicePath.Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel;\r | |
142 | KernelDevicePath.Node1.EndingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel + KernelSize;\r | |
143 | \r | |
34e0bcea AB |
144 | // Initialize Linux command line\r |
145 | LoadOptions = CatSPrint (NULL, L"%a", KernelArgs);\r | |
146 | if (LoadOptions == NULL) {\r | |
bd9a5182 OM |
147 | return EFI_OUT_OF_RESOURCES;\r |
148 | }\r | |
149 | \r | |
34e0bcea AB |
150 | if (RamdiskSize != 0) {\r |
151 | NewLoadOptions = CatSPrint (LoadOptions, L" initrd=0x%x,0x%x",\r | |
152 | (UINTN)Ramdisk, RamdiskSize);\r | |
153 | FreePool (LoadOptions);\r | |
154 | if (NewLoadOptions == NULL) {\r | |
155 | return EFI_OUT_OF_RESOURCES;\r | |
156 | }\r | |
157 | LoadOptions = NewLoadOptions;\r | |
bd9a5182 OM |
158 | }\r |
159 | \r | |
fcf41edf | 160 | Status = StartEfiApplication (gImageHandle,\r |
34e0bcea AB |
161 | (EFI_DEVICE_PATH_PROTOCOL *) &KernelDevicePath,\r |
162 | StrSize (LoadOptions),\r | |
163 | LoadOptions);\r | |
f6755908 | 164 | if (EFI_ERROR (Status)) {\r |
a1878955 | 165 | DEBUG ((DEBUG_ERROR, "Couldn't Boot Linux: %d\n", Status));\r |
34e0bcea AB |
166 | Status = EFI_DEVICE_ERROR;\r |
167 | goto FreeLoadOptions;\r | |
f6755908 OM |
168 | }\r |
169 | \r | |
170 | // If we got here we do a confused face because BootLinuxFdt returned,\r | |
171 | // reporting success.\r | |
a1878955 | 172 | DEBUG ((DEBUG_ERROR, "WARNING: BdsBootLinuxFdt returned EFI_SUCCESS.\n"));\r |
f6755908 | 173 | return EFI_SUCCESS;\r |
34e0bcea AB |
174 | \r |
175 | FreeLoadOptions:\r | |
176 | FreePool (LoadOptions);\r | |
177 | return Status;\r | |
f6755908 | 178 | }\r |