3 * Copyright (c) 2011-2012, 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 "BdsInternal.h"
16 #include "BdsLinuxLoader.h"
18 #define ALIGN32_BELOW(addr) ALIGN_POINTER(addr - 32,32)
22 PreparePlatformHardware (
26 //Note: Interrupts will be disabled by the GIC driver when ExitBootServices() will be called.
28 // Clean before Disable else the Stack gets corrupted with old data.
30 ArmDisableDataCache ();
31 // Invalidate all the entries that might have snuck in.
32 ArmInvalidateDataCache ();
34 // Invalidate and disable the Instruction cache
35 ArmDisableInstructionCache ();
36 ArmInvalidateInstructionCache ();
47 IN EFI_PHYSICAL_ADDRESS LinuxImage
,
48 IN UINTN LinuxImageSize
,
49 IN EFI_PHYSICAL_ADDRESS KernelParamsAddress
,
50 IN UINTN KernelParamsSize
,
55 LINUX_KERNEL LinuxKernel
;
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
));
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
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
);
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
);
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
);
84 LinuxKernel
= (LINUX_KERNEL
)(UINTN
)LinuxImage
;
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);
94 //TODO: Check there is no overlapping between kernel and Atag
97 // Switch off interrupts, caches, mmu, etc
99 Status
= PreparePlatformHardware ();
100 ASSERT_EFI_ERROR(Status
);
102 // Register and print out performance information
103 PERF_END (NULL
, "BDS", NULL
, 0);
104 if (PerformanceMeasurementEnabled ()) {
109 // Start the Linux Kernel
112 // Outside BootServices, so can't use Print();
113 DEBUG((EFI_D_ERROR
, "\nStarting the kernel:\n\n"));
115 // Jump to kernel with register set
116 LinuxKernel ((UINTN
)0, MachineType
, (UINTN
)KernelParamsAddress
);
118 // Kernel should never exit
119 // After Life services are not provided
123 // Only be here if we fail to start Linux
124 Print (L
"ERROR : Can not start the kernel. Status=0x%X\n", Status
);
126 // Free Runtimee Memory (kernel and FDT)
131 Start a Linux kernel from a Device Path
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
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.
144 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
145 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
146 IN CONST CHAR8
* CommandLineArguments
150 UINT32 LinuxImageSize
;
151 UINT32 InitrdImageBaseSize
= 0;
152 UINT32 InitrdImageSize
= 0;
154 EFI_PHYSICAL_ADDRESS AtagBase
;
155 EFI_PHYSICAL_ADDRESS LinuxImage
;
156 EFI_PHYSICAL_ADDRESS InitrdImageBase
= 0;
157 EFI_PHYSICAL_ADDRESS InitrdImage
= 0;
159 PERF_START (NULL
, "BDS", NULL
, 0);
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");
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
);
176 if (EFI_ERROR(Status
)) {
177 Print (L
"ERROR: Did not find initrd image.\n");
178 goto EXIT_FREE_LINUX
;
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;
187 InitrdImage
= InitrdImageBase
;
188 InitrdImageSize
= InitrdImageBaseSize
;
193 // Setup the Linux Kernel Parameters
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
;
203 return StartLinux (LinuxImage
, LinuxImageSize
, AtagBase
, AtagSize
, PcdGet32(PcdArmMachineType
));
206 if (InitrdDevicePath
) {
207 gBS
->FreePages (InitrdImageBase
, EFI_SIZE_TO_PAGES (InitrdImageBaseSize
));
211 gBS
->FreePages (LinuxImage
, EFI_SIZE_TO_PAGES (LinuxImageSize
));
217 Start a Linux kernel from a Device Path
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
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.
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
237 UINT32 LinuxImageSize
;
238 UINT32 InitrdImageBaseSize
= 0;
239 UINT32 InitrdImageSize
= 0;
241 EFI_PHYSICAL_ADDRESS FdtBlobBase
;
242 EFI_PHYSICAL_ADDRESS LinuxImage
;
243 EFI_PHYSICAL_ADDRESS InitrdImageBase
= 0;
244 EFI_PHYSICAL_ADDRESS InitrdImage
= 0;
246 PERF_START (NULL
, "BDS", NULL
, 0);
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");
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
);
262 if (EFI_ERROR(Status
)) {
263 Print (L
"ERROR: Did not find initrd image.\n");
264 goto EXIT_FREE_LINUX
;
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;
273 InitrdImage
= InitrdImageBase
;
274 InitrdImageSize
= InitrdImageBaseSize
;
278 // Load the FDT binary from a device path. The FDT will be reloaded later to a more appropriate location for the Linux kernel.
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
;
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
);
294 return StartLinux (LinuxImage
, LinuxImageSize
, FdtBlobBase
, FdtBlobSize
, ARM_FDT_MACHINE_TYPE
);
297 gBS
->FreePages (FdtBlobBase
, EFI_SIZE_TO_PAGES (FdtBlobSize
));
300 if (InitrdDevicePath
) {
301 gBS
->FreePages (InitrdImageBase
, EFI_SIZE_TO_PAGES (InitrdImageBaseSize
));
305 gBS
->FreePages (LinuxImage
, EFI_SIZE_TO_PAGES (LinuxImageSize
));