]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/BdsLib/Arm/BdsLinuxLoader.c
3d512486da415f5d2d19edb6cbb00e94a48e6a14
[mirror_edk2.git] / ArmPkg / Library / BdsLib / Arm / BdsLinuxLoader.c
1 /** @file
2 *
3 * Copyright (c) 2011-2012, ARM Limited. All rights reserved.
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 "BdsInternal.h"
16 #include "BdsLinuxLoader.h"
17
18 #define ALIGN32_BELOW(addr) ALIGN_POINTER(addr - 32,32)
19
20 STATIC
21 EFI_STATUS
22 PreparePlatformHardware (
23 VOID
24 )
25 {
26 //Note: Interrupts will be disabled by the GIC driver when ExitBootServices() will be called.
27
28 // Clean before Disable else the Stack gets corrupted with old data.
29 ArmCleanDataCache ();
30 ArmDisableDataCache ();
31 // Invalidate all the entries that might have snuck in.
32 ArmInvalidateDataCache ();
33
34 // Invalidate and disable the Instruction cache
35 ArmDisableInstructionCache ();
36 ArmInvalidateInstructionCache ();
37
38 // Turn off MMU
39 ArmDisableMmu();
40
41 return EFI_SUCCESS;
42 }
43
44 STATIC
45 EFI_STATUS
46 StartLinux (
47 IN EFI_PHYSICAL_ADDRESS LinuxImage,
48 IN UINTN LinuxImageSize,
49 IN EFI_PHYSICAL_ADDRESS KernelParamsAddress,
50 IN UINTN KernelParamsSize,
51 IN UINT32 MachineType
52 )
53 {
54 EFI_STATUS Status;
55 LINUX_KERNEL LinuxKernel;
56
57 // Shut down UEFI boot services. ExitBootServices() will notify every driver that created an event on
58 // ExitBootServices event. Example the Interrupt DXE driver will disable the interrupts on this event.
59 Status = ShutdownUefiBootServices ();
60 if(EFI_ERROR(Status)) {
61 DEBUG((EFI_D_ERROR,"ERROR: Can not shutdown UEFI boot services. Status=0x%X\n", Status));
62 goto Exit;
63 }
64
65 // Move the kernel parameters to any address inside the first 1MB.
66 // This is necessary because the ARM Linux kernel requires
67 // the FTD / ATAG List to reside entirely inside the first 1MB of
68 // physical memory.
69 //Note: There is no requirement on the alignment
70 if (MachineType != ARM_FDT_MACHINE_TYPE) {
71 if (((UINTN)KernelParamsAddress > LINUX_ATAG_MAX_OFFSET) && (KernelParamsSize < PcdGet32(PcdArmLinuxAtagMaxOffset))) {
72 KernelParamsAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)CopyMem (ALIGN32_BELOW(LINUX_ATAG_MAX_OFFSET - KernelParamsSize), (VOID*)(UINTN)KernelParamsAddress, KernelParamsSize);
73 }
74 } else {
75 if (((UINTN)KernelParamsAddress > LINUX_FDT_MAX_OFFSET) && (KernelParamsSize < PcdGet32(PcdArmLinuxFdtMaxOffset))) {
76 KernelParamsAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)CopyMem (ALIGN32_BELOW(LINUX_FDT_MAX_OFFSET - KernelParamsSize), (VOID*)(UINTN)KernelParamsAddress, KernelParamsSize);
77 }
78 }
79
80 if ((UINTN)LinuxImage > LINUX_KERNEL_MAX_OFFSET) {
81 //Note: There is no requirement on the alignment
82 LinuxKernel = (LINUX_KERNEL)CopyMem (ALIGN32_BELOW(LINUX_KERNEL_MAX_OFFSET - LinuxImageSize), (VOID*)(UINTN)LinuxImage, LinuxImageSize);
83 } else {
84 LinuxKernel = (LINUX_KERNEL)(UINTN)LinuxImage;
85 }
86
87 // Check if the Linux Image is a uImage
88 if (*(UINT32*)LinuxKernel == LINUX_UIMAGE_SIGNATURE) {
89 // Assume the Image Entry Point is just after the uImage header (64-byte size)
90 LinuxKernel = (LINUX_KERNEL)((UINTN)LinuxKernel + 64);
91 LinuxImageSize -= 64;
92 }
93
94 //TODO: Check there is no overlapping between kernel and Atag
95
96 //
97 // Switch off interrupts, caches, mmu, etc
98 //
99 Status = PreparePlatformHardware ();
100 ASSERT_EFI_ERROR(Status);
101
102 // Register and print out performance information
103 PERF_END (NULL, "BDS", NULL, 0);
104 if (PerformanceMeasurementEnabled ()) {
105 PrintPerformance ();
106 }
107
108 //
109 // Start the Linux Kernel
110 //
111
112 // Outside BootServices, so can't use Print();
113 DEBUG((EFI_D_ERROR, "\nStarting the kernel:\n\n"));
114
115 // Jump to kernel with register set
116 LinuxKernel ((UINTN)0, MachineType, (UINTN)KernelParamsAddress);
117
118 // Kernel should never exit
119 // After Life services are not provided
120 ASSERT(FALSE);
121
122 Exit:
123 // Only be here if we fail to start Linux
124 Print (L"ERROR : Can not start the kernel. Status=0x%X\n", Status);
125
126 // Free Runtimee Memory (kernel and FDT)
127 return Status;
128 }
129
130 /**
131 Start a Linux kernel from a Device Path
132
133 @param LinuxKernel Device Path to the Linux Kernel
134 @param Parameters Linux kernel arguments
135 @param Fdt Device Path to the Flat Device Tree
136
137 @retval EFI_SUCCESS All drivers have been connected
138 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
139 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
140
141 **/
142 EFI_STATUS
143 BdsBootLinuxAtag (
144 IN EFI_DEVICE_PATH_PROTOCOL* LinuxKernelDevicePath,
145 IN EFI_DEVICE_PATH_PROTOCOL* InitrdDevicePath,
146 IN CONST CHAR8* CommandLineArguments
147 )
148 {
149 EFI_STATUS Status;
150 UINT32 LinuxImageSize;
151 UINT32 InitrdImageBaseSize = 0;
152 UINT32 InitrdImageSize = 0;
153 UINT32 AtagSize;
154 EFI_PHYSICAL_ADDRESS AtagBase;
155 EFI_PHYSICAL_ADDRESS LinuxImage;
156 EFI_PHYSICAL_ADDRESS InitrdImageBase = 0;
157 EFI_PHYSICAL_ADDRESS InitrdImage = 0;
158
159 PERF_START (NULL, "BDS", NULL, 0);
160
161 // Load the Linux kernel from a device path
162 LinuxImage = LINUX_KERNEL_MAX_OFFSET;
163 Status = BdsLoadImage (LinuxKernelDevicePath, AllocateMaxAddress, &LinuxImage, &LinuxImageSize);
164 if (EFI_ERROR(Status)) {
165 Print (L"ERROR: Did not find Linux kernel.\n");
166 return Status;
167 }
168
169 if (InitrdDevicePath) {
170 // Load the initrd near to the Linux kernel
171 InitrdImageBase = LINUX_KERNEL_MAX_OFFSET;
172 Status = BdsLoadImage (InitrdDevicePath, AllocateMaxAddress, &InitrdImageBase, &InitrdImageBaseSize);
173 if (Status == EFI_OUT_OF_RESOURCES) {
174 Status = BdsLoadImage (InitrdDevicePath, AllocateAnyPages, &InitrdImageBase, &InitrdImageBaseSize);
175 }
176 if (EFI_ERROR(Status)) {
177 Print (L"ERROR: Did not find initrd image.\n");
178 goto EXIT_FREE_LINUX;
179 }
180
181 // Check if the initrd is a uInitrd
182 if (*(UINT32*)((UINTN)InitrdImageBase) == LINUX_UIMAGE_SIGNATURE) {
183 // Skip the 64-byte image header
184 InitrdImage = (EFI_PHYSICAL_ADDRESS)((UINTN)InitrdImageBase + 64);
185 InitrdImageSize = InitrdImageBaseSize - 64;
186 } else {
187 InitrdImage = InitrdImageBase;
188 InitrdImageSize = InitrdImageBaseSize;
189 }
190 }
191
192 //
193 // Setup the Linux Kernel Parameters
194 //
195
196 // By setting address=0 we leave the memory allocation to the function
197 Status = PrepareAtagList (CommandLineArguments, InitrdImage, InitrdImageSize, &AtagBase, &AtagSize);
198 if (EFI_ERROR(Status)) {
199 Print(L"ERROR: Can not prepare ATAG list. Status=0x%X\n", Status);
200 goto EXIT_FREE_INITRD;
201 }
202
203 return StartLinux (LinuxImage, LinuxImageSize, AtagBase, AtagSize, PcdGet32(PcdArmMachineType));
204
205 EXIT_FREE_INITRD:
206 if (InitrdDevicePath) {
207 gBS->FreePages (InitrdImageBase, EFI_SIZE_TO_PAGES (InitrdImageBaseSize));
208 }
209
210 EXIT_FREE_LINUX:
211 gBS->FreePages (LinuxImage, EFI_SIZE_TO_PAGES (LinuxImageSize));
212
213 return Status;
214 }
215
216 /**
217 Start a Linux kernel from a Device Path
218
219 @param LinuxKernel Device Path to the Linux Kernel
220 @param Parameters Linux kernel arguments
221 @param Fdt Device Path to the Flat Device Tree
222
223 @retval EFI_SUCCESS All drivers have been connected
224 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
225 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
226
227 **/
228 EFI_STATUS
229 BdsBootLinuxFdt (
230 IN EFI_DEVICE_PATH_PROTOCOL* LinuxKernelDevicePath,
231 IN EFI_DEVICE_PATH_PROTOCOL* InitrdDevicePath,
232 IN CONST CHAR8* CommandLineArguments,
233 IN EFI_DEVICE_PATH_PROTOCOL* FdtDevicePath
234 )
235 {
236 EFI_STATUS Status;
237 UINT32 LinuxImageSize;
238 UINT32 InitrdImageBaseSize = 0;
239 UINT32 InitrdImageSize = 0;
240 UINT32 FdtBlobSize;
241 EFI_PHYSICAL_ADDRESS FdtBlobBase;
242 EFI_PHYSICAL_ADDRESS LinuxImage;
243 EFI_PHYSICAL_ADDRESS InitrdImageBase = 0;
244 EFI_PHYSICAL_ADDRESS InitrdImage = 0;
245
246 PERF_START (NULL, "BDS", NULL, 0);
247
248 // Load the Linux kernel from a device path
249 LinuxImage = LINUX_KERNEL_MAX_OFFSET;
250 Status = BdsLoadImage (LinuxKernelDevicePath, AllocateMaxAddress, &LinuxImage, &LinuxImageSize);
251 if (EFI_ERROR(Status)) {
252 Print (L"ERROR: Did not find Linux kernel.\n");
253 return Status;
254 }
255
256 if (InitrdDevicePath) {
257 InitrdImageBase = LINUX_KERNEL_MAX_OFFSET;
258 Status = BdsLoadImage (InitrdDevicePath, AllocateMaxAddress, &InitrdImageBase, &InitrdImageBaseSize);
259 if (Status == EFI_OUT_OF_RESOURCES) {
260 Status = BdsLoadImage (InitrdDevicePath, AllocateAnyPages, &InitrdImageBase, &InitrdImageBaseSize);
261 }
262 if (EFI_ERROR(Status)) {
263 Print (L"ERROR: Did not find initrd image.\n");
264 goto EXIT_FREE_LINUX;
265 }
266
267 // Check if the initrd is a uInitrd
268 if (*(UINT32*)((UINTN)InitrdImageBase) == LINUX_UIMAGE_SIGNATURE) {
269 // Skip the 64-byte image header
270 InitrdImage = (EFI_PHYSICAL_ADDRESS)((UINTN)InitrdImageBase + 64);
271 InitrdImageSize = InitrdImageBaseSize - 64;
272 } else {
273 InitrdImage = InitrdImageBase;
274 InitrdImageSize = InitrdImageBaseSize;
275 }
276 }
277
278 // Load the FDT binary from a device path. The FDT will be reloaded later to a more appropriate location for the Linux kernel.
279 FdtBlobBase = 0;
280 Status = BdsLoadImage (FdtDevicePath, AllocateAnyPages, &FdtBlobBase, &FdtBlobSize);
281 if (EFI_ERROR(Status)) {
282 Print (L"ERROR: Did not find Device Tree blob.\n");
283 goto EXIT_FREE_INITRD;
284 }
285
286 // Update the Fdt with the Initrd information. The FDT will increase in size.
287 // By setting address=0 we leave the memory allocation to the function
288 Status = PrepareFdt (CommandLineArguments, InitrdImage, InitrdImageSize, &FdtBlobBase, &FdtBlobSize);
289 if (EFI_ERROR(Status)) {
290 Print(L"ERROR: Can not load kernel with FDT. Status=%r\n", Status);
291 goto EXIT_FREE_FDT;
292 }
293
294 return StartLinux (LinuxImage, LinuxImageSize, FdtBlobBase, FdtBlobSize, ARM_FDT_MACHINE_TYPE);
295
296 EXIT_FREE_FDT:
297 gBS->FreePages (FdtBlobBase, EFI_SIZE_TO_PAGES (FdtBlobSize));
298
299 EXIT_FREE_INITRD:
300 if (InitrdDevicePath) {
301 gBS->FreePages (InitrdImageBase, EFI_SIZE_TO_PAGES (InitrdImageBaseSize));
302 }
303
304 EXIT_FREE_LINUX:
305 gBS->FreePages (LinuxImage, EFI_SIZE_TO_PAGES (LinuxImageSize));
306
307 return Status;
308 }
309