3 * Copyright (c) 2011-2014, 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.
18 #include "BdsInternal.h"
19 #include "BdsLinuxLoader.h"
21 #define ALIGN32_BELOW(addr) ALIGN_POINTER(addr - 32,32)
23 #define IS_ADDRESS_IN_REGION(RegionStart, RegionSize, Address) \
24 (((UINTN)(RegionStart) <= (UINTN)(Address)) && ((UINTN)(Address) <= ((UINTN)(RegionStart) + (UINTN)(RegionSize))))
28 PreparePlatformHardware (
32 //Note: Interrupts will be disabled by the GIC driver when ExitBootServices() will be called.
34 // Clean before Disable else the Stack gets corrupted with old data.
36 ArmDisableDataCache ();
37 // Invalidate all the entries that might have snuck in.
38 ArmInvalidateDataCache ();
40 // Invalidate and disable the Instruction cache
41 ArmDisableInstructionCache ();
42 ArmInvalidateInstructionCache ();
53 IN EFI_PHYSICAL_ADDRESS LinuxImage
,
54 IN UINTN LinuxImageSize
,
55 IN EFI_PHYSICAL_ADDRESS KernelParamsAddress
,
56 IN UINTN KernelParamsSize
,
61 LINUX_KERNEL LinuxKernel
;
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
));
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
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
);
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
);
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
);
90 LinuxKernel
= (LINUX_KERNEL
)(UINTN
)LinuxImage
;
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);
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
));
106 // Switch off interrupts, caches, mmu, etc
108 Status
= PreparePlatformHardware ();
109 ASSERT_EFI_ERROR(Status
);
111 // Register and print out performance information
112 PERF_END (NULL
, "BDS", NULL
, 0);
113 if (PerformanceMeasurementEnabled ()) {
118 // Start the Linux Kernel
121 // Outside BootServices, so can't use Print();
122 DEBUG((EFI_D_ERROR
, "\nStarting the kernel:\n\n"));
124 // Jump to kernel with register set
125 LinuxKernel ((UINTN
)0, MachineType
, (UINTN
)KernelParamsAddress
);
127 // Kernel should never exit
128 // After Life services are not provided
132 // Only be here if we fail to start Linux
133 Print (L
"ERROR : Can not start the kernel. Status=0x%X\n", Status
);
135 // Free Runtimee Memory (kernel and FDT)
140 Start a Linux kernel from a Device Path
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
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.
153 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
154 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
155 IN CONST CHAR8
* CommandLineArguments
159 UINT32 LinuxImageSize
;
160 UINT32 InitrdImageBaseSize
= 0;
161 UINT32 InitrdImageSize
= 0;
163 EFI_PHYSICAL_ADDRESS AtagBase
;
164 EFI_PHYSICAL_ADDRESS LinuxImage
;
165 EFI_PHYSICAL_ADDRESS InitrdImageBase
= 0;
166 EFI_PHYSICAL_ADDRESS InitrdImage
= 0;
168 PERF_START (NULL
, "BDS", NULL
, 0);
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");
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
);
185 if (EFI_ERROR(Status
)) {
186 Print (L
"ERROR: Did not find initrd image.\n");
187 goto EXIT_FREE_LINUX
;
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;
196 InitrdImage
= InitrdImageBase
;
197 InitrdImageSize
= InitrdImageBaseSize
;
202 // Setup the Linux Kernel Parameters
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
;
212 return StartLinux (LinuxImage
, LinuxImageSize
, AtagBase
, AtagSize
, PcdGet32(PcdArmMachineType
));
215 if (InitrdDevicePath
) {
216 gBS
->FreePages (InitrdImageBase
, EFI_SIZE_TO_PAGES (InitrdImageBaseSize
));
220 gBS
->FreePages (LinuxImage
, EFI_SIZE_TO_PAGES (LinuxImageSize
));
226 Start a Linux kernel from a Device Path
228 @param LinuxKernelDevicePath Device Path to the Linux Kernel
229 @param InitrdDevicePath Device Path to the Initrd
230 @param CommandLineArguments Linux command line
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.
239 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
240 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
241 IN CONST CHAR8
* CommandLineArguments
245 UINT32 LinuxImageSize
;
246 UINT32 InitrdImageBaseSize
= 0;
247 UINT32 InitrdImageSize
= 0;
248 VOID
*InstalledFdtBase
;
250 EFI_PHYSICAL_ADDRESS FdtBlobBase
;
251 EFI_PHYSICAL_ADDRESS LinuxImage
;
252 EFI_PHYSICAL_ADDRESS InitrdImageBase
= 0;
253 EFI_PHYSICAL_ADDRESS InitrdImage
= 0;
255 PERF_START (NULL
, "BDS", NULL
, 0);
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");
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
);
271 if (EFI_ERROR(Status
)) {
272 Print (L
"ERROR: Did not find initrd image.\n");
273 goto EXIT_FREE_LINUX
;
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;
282 InitrdImage
= InitrdImageBase
;
283 InitrdImageSize
= InitrdImageBaseSize
;
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.
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
;
297 FdtBlobBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)InstalledFdtBase
;
298 FdtBlobSize
= fdt_totalsize (InstalledFdtBase
);
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
);
308 return StartLinux (LinuxImage
, LinuxImageSize
, FdtBlobBase
, FdtBlobSize
, ARM_FDT_MACHINE_TYPE
);
311 gBS
->FreePages (FdtBlobBase
, EFI_SIZE_TO_PAGES (FdtBlobSize
));
314 if (InitrdDevicePath
) {
315 gBS
->FreePages (InitrdImageBase
, EFI_SIZE_TO_PAGES (InitrdImageBaseSize
));
319 gBS
->FreePages (LinuxImage
, EFI_SIZE_TO_PAGES (LinuxImageSize
));