]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
3053cf06873eddec83506560cd7a0b7e00d73ba1
[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 #include <Guid/ArmGlobalVariableHob.h>
25
26 #define LINUX_LOADER_COMMAND_LINE L"%s -f %s -c %s"
27
28 // This GUID is defined in the INGF file of ArmPkg/Application/LinuxLoader
29 CONST EFI_GUID mLinuxLoaderAppGuid = { 0x701f54f2, 0x0d70, 0x4b89, { 0xbc, 0x0a, 0xd9, 0xca, 0x25, 0x37, 0x90, 0x59 }};
30
31 // Device Path representing an image in memory
32 #pragma pack(1)
33 typedef struct {
34 MEMMAP_DEVICE_PATH Node1;
35 EFI_DEVICE_PATH_PROTOCOL End;
36 } MEMORY_DEVICE_PATH;
37 #pragma pack()
38
39 STATIC CONST MEMORY_DEVICE_PATH MemoryDevicePathTemplate =
40 {
41 {
42 {
43 HARDWARE_DEVICE_PATH,
44 HW_MEMMAP_DP,
45 {
46 (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
47 (UINT8)((sizeof (MEMMAP_DEVICE_PATH)) >> 8),
48 },
49 }, // Header
50 0, // StartingAddress (set at runtime)
51 0 // EndingAddress (set at runtime)
52 }, // Node1
53 {
54 END_DEVICE_PATH_TYPE,
55 END_ENTIRE_DEVICE_PATH_SUBTYPE,
56 { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }
57 } // End
58 };
59
60 EFI_STATUS
61 BootAndroidBootImg (
62 IN UINTN BufferSize,
63 IN VOID *Buffer
64 )
65 {
66 EFI_STATUS Status;
67 CHAR8 KernelArgs[BOOTIMG_KERNEL_ARGS_SIZE];
68 VOID *Kernel;
69 UINTN KernelSize;
70 VOID *Ramdisk;
71 UINTN RamdiskSize;
72 MEMORY_DEVICE_PATH KernelDevicePath;
73 MEMORY_DEVICE_PATH* RamdiskDevicePath;
74 CHAR16* KernelDevicePathTxt;
75 CHAR16* RamdiskDevicePathTxt;
76 EFI_DEVICE_PATH* LinuxLoaderDevicePath;
77 CHAR16* LoadOptions;
78
79 Status = ParseAndroidBootImg (
80 Buffer,
81 &Kernel,
82 &KernelSize,
83 &Ramdisk,
84 &RamdiskSize,
85 KernelArgs
86 );
87 if (EFI_ERROR (Status)) {
88 return Status;
89 }
90
91 KernelDevicePath = MemoryDevicePathTemplate;
92
93 // Have to cast to UINTN before casting to EFI_PHYSICAL_ADDRESS in order to
94 // appease GCC.
95 KernelDevicePath.Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel;
96 KernelDevicePath.Node1.EndingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel + KernelSize;
97
98 RamdiskDevicePath = NULL;
99 if (RamdiskSize != 0) {
100 RamdiskDevicePath = (MEMORY_DEVICE_PATH*)DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL*) &MemoryDevicePathTemplate);
101
102 RamdiskDevicePath->Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Ramdisk;
103 RamdiskDevicePath->Node1.EndingAddress = ((EFI_PHYSICAL_ADDRESS)(UINTN) Ramdisk) + RamdiskSize;
104 }
105
106 //
107 // Boot Linux using the Legacy Linux Loader
108 //
109
110 Status = LocateEfiApplicationInFvByGuid (&mLinuxLoaderAppGuid, &LinuxLoaderDevicePath);
111 if (EFI_ERROR (Status)) {
112 Print (L"Couldn't Boot Linux: %d\n", Status);
113 return EFI_DEVICE_ERROR;
114 }
115
116 KernelDevicePathTxt = ConvertDevicePathToText ((EFI_DEVICE_PATH_PROTOCOL *) &KernelDevicePath, FALSE, FALSE);
117 if (KernelDevicePathTxt == NULL) {
118 return EFI_OUT_OF_RESOURCES;
119 }
120
121 RamdiskDevicePathTxt = ConvertDevicePathToText ((EFI_DEVICE_PATH_PROTOCOL *) RamdiskDevicePath, FALSE, FALSE);
122 if (RamdiskDevicePathTxt == NULL) {
123 return EFI_OUT_OF_RESOURCES;
124 }
125
126 // Initialize Legacy Linux loader command line
127 LoadOptions = CatSPrint (NULL, LINUX_LOADER_COMMAND_LINE, KernelDevicePathTxt, RamdiskDevicePathTxt, KernelArgs);
128 if (LoadOptions == NULL) {
129 return EFI_OUT_OF_RESOURCES;
130 }
131
132 Status = BdsStartEfiApplication (gImageHandle, LinuxLoaderDevicePath, StrSize (LoadOptions), LoadOptions);
133 if (EFI_ERROR (Status)) {
134 DEBUG ((EFI_D_ERROR, "Couldn't Boot Linux: %d\n", Status));
135 return EFI_DEVICE_ERROR;
136 }
137
138 if (RamdiskDevicePath) {
139 FreePool (RamdiskDevicePathTxt);
140 FreePool (RamdiskDevicePath);
141 }
142
143 FreePool (KernelDevicePathTxt);
144
145 // If we got here we do a confused face because BootLinuxFdt returned,
146 // reporting success.
147 DEBUG ((EFI_D_ERROR, "WARNING: BdsBootLinuxFdt returned EFI_SUCCESS.\n"));
148 return EFI_SUCCESS;
149 }