3 * Copyright (c) 2011-2015, ARM Limited. All rights reserved.
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
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.
15 #include <Library/ArmLib.h>
16 #include <Library/PcdLib.h>
20 #include "LinuxLoader.h"
22 #define ALIGN32_BELOW(addr) ALIGN_POINTER(addr - 32,32)
24 #define IS_ADDRESS_IN_REGION(RegionStart, RegionSize, Address) \
25 (((UINTN)(RegionStart) <= (UINTN)(Address)) && ((UINTN)(Address) <= ((UINTN)(RegionStart) + (UINTN)(RegionSize))))
29 IN EFI_PHYSICAL_ADDRESS SystemMemoryBase
,
30 IN CONST CHAR8
* CommandLineString
,
31 IN EFI_PHYSICAL_ADDRESS InitrdImage
,
32 IN UINTN InitrdImageSize
,
33 OUT EFI_PHYSICAL_ADDRESS
*AtagBase
,
39 PreparePlatformHardware (
43 //Note: Interrupts will be disabled by the GIC driver when ExitBootServices() will be called.
45 // Clean before Disable else the Stack gets corrupted with old data.
47 ArmDisableDataCache ();
48 // Invalidate all the entries that might have snuck in.
49 ArmInvalidateDataCache ();
51 // Invalidate and disable the Instruction cache
52 ArmDisableInstructionCache ();
53 ArmInvalidateInstructionCache ();
62 IN EFI_PHYSICAL_ADDRESS SystemMemoryBase
,
63 IN EFI_PHYSICAL_ADDRESS LinuxImage
,
64 IN UINTN LinuxImageSize
,
65 IN EFI_PHYSICAL_ADDRESS KernelParamsAddress
,
66 IN UINTN KernelParamsSize
,
71 LINUX_KERNEL LinuxKernel
;
73 // Shut down UEFI boot services. ExitBootServices() will notify every driver that created an event on
74 // ExitBootServices event. Example the Interrupt DXE driver will disable the interrupts on this event.
75 Status
= ShutdownUefiBootServices ();
76 if (EFI_ERROR (Status
)) {
77 DEBUG ((EFI_D_ERROR
, "ERROR: Can not shutdown UEFI boot services. Status=0x%X\n", Status
));
81 // Move the kernel parameters to any address inside the first 1MB.
82 // This is necessary because the ARM Linux kernel requires
83 // the FTD / ATAG List to reside entirely inside the first 1MB of
85 //Note: There is no requirement on the alignment
86 if (MachineType
!= ARM_FDT_MACHINE_TYPE
) {
87 if (((UINTN
)KernelParamsAddress
> LINUX_ATAG_MAX_OFFSET
) && (KernelParamsSize
< PcdGet32 (PcdArmLinuxAtagMaxOffset
))) {
88 KernelParamsAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)CopyMem (ALIGN32_BELOW (LINUX_ATAG_MAX_OFFSET
- KernelParamsSize
), (VOID
*)(UINTN
)KernelParamsAddress
, KernelParamsSize
);
91 if (((UINTN
)KernelParamsAddress
> LINUX_FDT_MAX_OFFSET
) && (KernelParamsSize
< PcdGet32 (PcdArmLinuxFdtMaxOffset
))) {
92 KernelParamsAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)CopyMem (ALIGN32_BELOW (LINUX_FDT_MAX_OFFSET
- KernelParamsSize
), (VOID
*)(UINTN
)KernelParamsAddress
, KernelParamsSize
);
96 if ((UINTN
)LinuxImage
> LINUX_KERNEL_MAX_OFFSET
) {
97 //Note: There is no requirement on the alignment
98 LinuxKernel
= (LINUX_KERNEL
)CopyMem (ALIGN32_BELOW (LINUX_KERNEL_MAX_OFFSET
- LinuxImageSize
), (VOID
*)(UINTN
)LinuxImage
, LinuxImageSize
);
100 LinuxKernel
= (LINUX_KERNEL
)(UINTN
)LinuxImage
;
103 // Check if the Linux Image is a uImage
104 if (*(UINT32
*)LinuxKernel
== LINUX_UIMAGE_SIGNATURE
) {
105 // Assume the Image Entry Point is just after the uImage header (64-byte size)
106 LinuxKernel
= (LINUX_KERNEL
)((UINTN
)LinuxKernel
+ 64);
107 LinuxImageSize
-= 64;
110 // Check there is no overlapping between kernel and its parameters
111 // We can only assert because it is too late to fallback to UEFI (ExitBootServices has been called).
112 ASSERT (!IS_ADDRESS_IN_REGION (LinuxKernel
, LinuxImageSize
, KernelParamsAddress
) &&
113 !IS_ADDRESS_IN_REGION (LinuxKernel
, LinuxImageSize
, KernelParamsAddress
+ KernelParamsSize
));
116 // Switch off interrupts, caches, mmu, etc
118 PreparePlatformHardware ();
120 // Register and print out performance information
121 PERF_END (NULL
, "BDS", NULL
, 0);
122 if (PerformanceMeasurementEnabled ()) {
127 // Start the Linux Kernel
130 // Outside BootServices, so can't use Print();
131 DEBUG ((EFI_D_ERROR
, "\nStarting the kernel:\n\n"));
133 // Jump to kernel with register set
134 LinuxKernel ((UINTN
)0, MachineType
, (UINTN
)KernelParamsAddress
);
136 // Kernel should never exit
137 // After Life services are not provided
139 // We cannot recover the execution at this stage
144 Start a Linux kernel from a Device Path
146 @param SystemMemoryBase Base of the system memory
147 @param LinuxKernel Device Path to the Linux Kernel
148 @param Parameters Linux kernel arguments
149 @param Fdt Device Path to the Flat Device Tree
150 @param MachineType ARM machine type value
152 @retval EFI_SUCCESS All drivers have been connected
153 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
154 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
155 @retval RETURN_UNSUPPORTED ATAG is not support by this architecture
160 IN EFI_PHYSICAL_ADDRESS SystemMemoryBase
,
161 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
162 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
163 IN CONST CHAR8
* CommandLineArguments
,
168 UINT32 LinuxImageSize
;
169 UINT32 InitrdImageBaseSize
= 0;
170 UINT32 InitrdImageSize
= 0;
172 EFI_PHYSICAL_ADDRESS AtagBase
;
173 EFI_PHYSICAL_ADDRESS LinuxImage
;
174 EFI_PHYSICAL_ADDRESS InitrdImageBase
= 0;
175 EFI_PHYSICAL_ADDRESS InitrdImage
= 0;
177 PERF_START (NULL
, "BDS", NULL
, 0);
179 // Load the Linux kernel from a device path
180 LinuxImage
= LINUX_KERNEL_MAX_OFFSET
;
181 Status
= BdsLoadImage (LinuxKernelDevicePath
, AllocateMaxAddress
, &LinuxImage
, &LinuxImageSize
);
182 if (EFI_ERROR (Status
)) {
183 Print (L
"ERROR: Did not find Linux kernel.\n");
187 if (InitrdDevicePath
) {
188 // Load the initrd near to the Linux kernel
189 InitrdImageBase
= LINUX_KERNEL_MAX_OFFSET
;
190 Status
= BdsLoadImage (InitrdDevicePath
, AllocateMaxAddress
, &InitrdImageBase
, &InitrdImageBaseSize
);
191 if (Status
== EFI_OUT_OF_RESOURCES
) {
192 Status
= BdsLoadImage (InitrdDevicePath
, AllocateAnyPages
, &InitrdImageBase
, &InitrdImageBaseSize
);
194 if (EFI_ERROR (Status
)) {
195 Print (L
"ERROR: Did not find initrd image.\n");
196 goto EXIT_FREE_LINUX
;
199 // Check if the initrd is a uInitrd
200 if (*(UINT32
*)((UINTN
)InitrdImageBase
) == LINUX_UIMAGE_SIGNATURE
) {
201 // Skip the 64-byte image header
202 InitrdImage
= (EFI_PHYSICAL_ADDRESS
)((UINTN
)InitrdImageBase
+ 64);
203 InitrdImageSize
= InitrdImageBaseSize
- 64;
205 InitrdImage
= InitrdImageBase
;
206 InitrdImageSize
= InitrdImageBaseSize
;
211 // Setup the Linux Kernel Parameters
214 // By setting address=0 we leave the memory allocation to the function
215 Status
= PrepareAtagList (SystemMemoryBase
, CommandLineArguments
, InitrdImage
, InitrdImageSize
, &AtagBase
, &AtagSize
);
216 if (EFI_ERROR (Status
)) {
217 Print (L
"ERROR: Can not prepare ATAG list. Status=0x%X\n", Status
);
218 goto EXIT_FREE_INITRD
;
221 return StartLinux (SystemMemoryBase
, LinuxImage
, LinuxImageSize
, AtagBase
, AtagSize
, MachineType
);
224 if (InitrdDevicePath
) {
225 gBS
->FreePages (InitrdImageBase
, EFI_SIZE_TO_PAGES (InitrdImageBaseSize
));
229 gBS
->FreePages (LinuxImage
, EFI_SIZE_TO_PAGES (LinuxImageSize
));
235 Start a Linux kernel from a Device Path
237 @param LinuxKernelDevicePath Device Path to the Linux Kernel
238 @param InitrdDevicePath Device Path to the Initrd
239 @param CommandLineArguments Linux command line
241 @retval EFI_SUCCESS All drivers have been connected
242 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
243 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
248 IN EFI_PHYSICAL_ADDRESS SystemMemoryBase
,
249 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
250 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
251 IN EFI_DEVICE_PATH_PROTOCOL
* FdtDevicePath
,
252 IN CONST CHAR8
* CommandLineArguments
256 UINT32 LinuxImageSize
;
257 UINT32 InitrdImageBaseSize
= 0;
258 UINT32 InitrdImageSize
= 0;
259 VOID
*InstalledFdtBase
;
261 EFI_PHYSICAL_ADDRESS FdtBlobBase
;
262 EFI_PHYSICAL_ADDRESS LinuxImage
;
263 EFI_PHYSICAL_ADDRESS InitrdImageBase
= 0;
264 EFI_PHYSICAL_ADDRESS InitrdImage
= 0;
266 PERF_START (NULL
, "BDS", NULL
, 0);
268 // Load the Linux kernel from a device path
269 LinuxImage
= LINUX_KERNEL_MAX_OFFSET
;
270 Status
= BdsLoadImage (LinuxKernelDevicePath
, AllocateMaxAddress
, &LinuxImage
, &LinuxImageSize
);
271 if (EFI_ERROR (Status
)) {
272 Print (L
"ERROR: Did not find Linux kernel.\n");
276 if (InitrdDevicePath
) {
277 InitrdImageBase
= LINUX_KERNEL_MAX_OFFSET
;
278 Status
= BdsLoadImage (InitrdDevicePath
, AllocateMaxAddress
, &InitrdImageBase
, &InitrdImageBaseSize
);
279 if (Status
== EFI_OUT_OF_RESOURCES
) {
280 Status
= BdsLoadImage (InitrdDevicePath
, AllocateAnyPages
, &InitrdImageBase
, &InitrdImageBaseSize
);
282 if (EFI_ERROR (Status
)) {
283 Print (L
"ERROR: Did not find initrd image.\n");
284 goto EXIT_FREE_LINUX
;
287 // Check if the initrd is a uInitrd
288 if (*(UINT32
*)((UINTN
)InitrdImageBase
) == LINUX_UIMAGE_SIGNATURE
) {
289 // Skip the 64-byte image header
290 InitrdImage
= (EFI_PHYSICAL_ADDRESS
)((UINTN
)InitrdImageBase
+ 64);
291 InitrdImageSize
= InitrdImageBaseSize
- 64;
293 InitrdImage
= InitrdImageBase
;
294 InitrdImageSize
= InitrdImageBaseSize
;
298 if (FdtDevicePath
== NULL
) {
300 // Get the FDT from the Configuration Table.
301 // The FDT will be reloaded in PrepareFdt() to a more appropriate
302 // location for the Linux Kernel.
304 Status
= EfiGetSystemConfigurationTable (&gFdtTableGuid
, &InstalledFdtBase
);
305 if (EFI_ERROR (Status
)) {
306 Print (L
"ERROR: Did not get the Device Tree blob (%r).\n", Status
);
307 goto EXIT_FREE_INITRD
;
309 FdtBlobBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)InstalledFdtBase
;
310 FdtBlobSize
= fdt_totalsize (InstalledFdtBase
);
313 // FDT device path explicitly defined. The FDT is relocated later to a
314 // more appropriate location for the Linux kernel.
316 FdtBlobBase
= LINUX_KERNEL_MAX_OFFSET
;
317 Status
= BdsLoadImage (FdtDevicePath
, AllocateMaxAddress
, &FdtBlobBase
, &FdtBlobSize
);
318 if (EFI_ERROR (Status
)) {
319 Print (L
"ERROR: Did not find Device Tree blob (%r).\n", Status
);
320 goto EXIT_FREE_INITRD
;
324 // Update the Fdt with the Initrd information. The FDT will increase in size.
325 // By setting address=0 we leave the memory allocation to the function
326 Status
= PrepareFdt (SystemMemoryBase
, CommandLineArguments
, InitrdImage
, InitrdImageSize
, &FdtBlobBase
, &FdtBlobSize
);
327 if (EFI_ERROR (Status
)) {
328 Print (L
"ERROR: Can not load kernel with FDT. Status=%r\n", Status
);
332 return StartLinux (SystemMemoryBase
, LinuxImage
, LinuxImageSize
, FdtBlobBase
, FdtBlobSize
, ARM_FDT_MACHINE_TYPE
);
335 gBS
->FreePages (FdtBlobBase
, EFI_SIZE_TO_PAGES (FdtBlobSize
));
338 if (InitrdDevicePath
) {
339 gBS
->FreePages (InitrdImageBase
, EFI_SIZE_TO_PAGES (InitrdImageBaseSize
));
343 gBS
->FreePages (LinuxImage
, EFI_SIZE_TO_PAGES (LinuxImageSize
));