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