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