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