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, invalidate, disable data cache
29 ArmDisableDataCache();
30 ArmCleanInvalidateDataCache();
32 // Invalidate and disable the Instruction cache
33 ArmDisableInstructionCache ();
34 ArmInvalidateInstructionCache ();
45 IN EFI_PHYSICAL_ADDRESS LinuxImage
,
46 IN UINTN LinuxImageSize
,
47 IN EFI_PHYSICAL_ADDRESS KernelParamsAddress
,
48 IN UINTN KernelParamsSize
,
53 LINUX_KERNEL LinuxKernel
;
55 // Shut down UEFI boot services. ExitBootServices() will notify every driver that created an event on
56 // ExitBootServices event. Example the Interrupt DXE driver will disable the interrupts on this event.
57 Status
= ShutdownUefiBootServices ();
58 if(EFI_ERROR(Status
)) {
59 DEBUG((EFI_D_ERROR
,"ERROR: Can not shutdown UEFI boot services. Status=0x%X\n", Status
));
63 // Move the kernel parameters to any address inside the first 1MB.
64 // This is necessary because the ARM Linux kernel requires
65 // the FTD / ATAG List to reside entirely inside the first 1MB of
67 //Note: There is no requirement on the alignment
68 if (MachineType
!= ARM_FDT_MACHINE_TYPE
) {
69 if (((UINTN
)KernelParamsAddress
> LINUX_ATAG_MAX_OFFSET
) && (KernelParamsSize
< PcdGet32(PcdArmLinuxAtagMaxOffset
))) {
70 KernelParamsAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)CopyMem (ALIGN32_BELOW(LINUX_ATAG_MAX_OFFSET
- KernelParamsSize
), (VOID
*)(UINTN
)KernelParamsAddress
, KernelParamsSize
);
73 if (((UINTN
)KernelParamsAddress
> LINUX_FDT_MAX_OFFSET
) && (KernelParamsSize
< PcdGet32(PcdArmLinuxFdtMaxOffset
))) {
74 KernelParamsAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)CopyMem (ALIGN32_BELOW(LINUX_FDT_MAX_OFFSET
- KernelParamsSize
), (VOID
*)(UINTN
)KernelParamsAddress
, KernelParamsSize
);
78 if ((UINTN
)LinuxImage
> LINUX_KERNEL_MAX_OFFSET
) {
79 //Note: There is no requirement on the alignment
80 LinuxKernel
= (LINUX_KERNEL
)CopyMem (ALIGN32_BELOW(LINUX_KERNEL_MAX_OFFSET
- LinuxImageSize
), (VOID
*)(UINTN
)LinuxImage
, LinuxImageSize
);
82 LinuxKernel
= (LINUX_KERNEL
)(UINTN
)LinuxImage
;
85 // Check if the Linux Image is a uImage
86 if (*(UINT32
*)LinuxKernel
== LINUX_UIMAGE_SIGNATURE
) {
87 // Assume the Image Entry Point is just after the uImage header (64-byte size)
88 LinuxKernel
= (LINUX_KERNEL
)((UINTN
)LinuxKernel
+ 64);
92 //TODO: Check there is no overlapping between kernel and Atag
95 // Switch off interrupts, caches, mmu, etc
97 Status
= PreparePlatformHardware ();
98 ASSERT_EFI_ERROR(Status
);
100 // Register and print out performance information
101 PERF_END (NULL
, "BDS", NULL
, 0);
102 if (PerformanceMeasurementEnabled ()) {
107 // Start the Linux Kernel
110 // Outside BootServices, so can't use Print();
111 DEBUG((EFI_D_ERROR
, "\nStarting the kernel:\n\n"));
113 // Jump to kernel with register set
114 LinuxKernel ((UINTN
)0, MachineType
, (UINTN
)KernelParamsAddress
);
116 // Kernel should never exit
117 // After Life services are not provided
121 // Only be here if we fail to start Linux
122 Print (L
"ERROR : Can not start the kernel. Status=0x%X\n", Status
);
124 // Free Runtimee Memory (kernel and FDT)
129 Start a Linux kernel from a Device Path
131 @param LinuxKernel Device Path to the Linux Kernel
132 @param Parameters Linux kernel arguments
133 @param Fdt Device Path to the Flat Device Tree
135 @retval EFI_SUCCESS All drivers have been connected
136 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
137 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
142 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
143 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
144 IN CONST CHAR8
* CommandLineArguments
148 UINT32 LinuxImageSize
;
149 UINT32 InitrdImageSize
= 0;
151 EFI_PHYSICAL_ADDRESS AtagBase
;
152 EFI_PHYSICAL_ADDRESS LinuxImage
;
153 EFI_PHYSICAL_ADDRESS InitrdImage
;
155 PERF_START (NULL
, "BDS", NULL
, 0);
157 // Load the Linux kernel from a device path
158 LinuxImage
= LINUX_KERNEL_MAX_OFFSET
;
159 Status
= BdsLoadImage (LinuxKernelDevicePath
, AllocateMaxAddress
, &LinuxImage
, &LinuxImageSize
);
160 if (EFI_ERROR(Status
)) {
161 Print (L
"ERROR: Did not find Linux kernel.\n");
165 if (InitrdDevicePath
) {
166 // Load the initrd near to the Linux kernel
167 InitrdImage
= LINUX_KERNEL_MAX_OFFSET
;
168 Status
= BdsLoadImage (InitrdDevicePath
, AllocateMaxAddress
, &InitrdImage
, &InitrdImageSize
);
169 if (Status
== EFI_OUT_OF_RESOURCES
) {
170 Status
= BdsLoadImage (InitrdDevicePath
, AllocateAnyPages
, &InitrdImage
, &InitrdImageSize
);
172 if (EFI_ERROR(Status
)) {
173 Print (L
"ERROR: Did not find initrd image.\n");
177 // Check if the initrd is a uInitrd
178 if (*(UINT32
*)((UINTN
)InitrdImage
) == LINUX_UIMAGE_SIGNATURE
) {
179 // Skip the 64-byte image header
180 InitrdImage
= (EFI_PHYSICAL_ADDRESS
)((UINTN
)InitrdImage
+ 64);
181 InitrdImageSize
-= 64;
186 // Setup the Linux Kernel Parameters
189 // By setting address=0 we leave the memory allocation to the function
190 Status
= PrepareAtagList (CommandLineArguments
, InitrdImage
, InitrdImageSize
, &AtagBase
, &AtagSize
);
191 if (EFI_ERROR(Status
)) {
192 Print(L
"ERROR: Can not prepare ATAG list. Status=0x%X\n", Status
);
196 return StartLinux (LinuxImage
, LinuxImageSize
, AtagBase
, AtagSize
, PcdGet32(PcdArmMachineType
));
200 Start a Linux kernel from a Device Path
202 @param LinuxKernel Device Path to the Linux Kernel
203 @param Parameters Linux kernel arguments
204 @param Fdt Device Path to the Flat Device Tree
206 @retval EFI_SUCCESS All drivers have been connected
207 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
208 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
213 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
214 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
215 IN CONST CHAR8
* CommandLineArguments
,
216 IN EFI_DEVICE_PATH_PROTOCOL
* FdtDevicePath
220 UINT32 LinuxImageSize
;
221 UINT32 InitrdImageSize
= 0;
223 EFI_PHYSICAL_ADDRESS FdtBlobBase
;
224 EFI_PHYSICAL_ADDRESS LinuxImage
;
225 EFI_PHYSICAL_ADDRESS InitrdImage
;
227 PERF_START (NULL
, "BDS", NULL
, 0);
229 // Load the Linux kernel from a device path
230 LinuxImage
= LINUX_KERNEL_MAX_OFFSET
;
231 Status
= BdsLoadImage (LinuxKernelDevicePath
, AllocateMaxAddress
, &LinuxImage
, &LinuxImageSize
);
232 if (EFI_ERROR(Status
)) {
233 Print (L
"ERROR: Did not find Linux kernel.\n");
237 if (InitrdDevicePath
) {
238 InitrdImage
= LINUX_KERNEL_MAX_OFFSET
;
239 Status
= BdsLoadImage (InitrdDevicePath
, AllocateMaxAddress
, &InitrdImage
, &InitrdImageSize
);
240 if (Status
== EFI_OUT_OF_RESOURCES
) {
241 Status
= BdsLoadImage (InitrdDevicePath
, AllocateAnyPages
, &InitrdImage
, &InitrdImageSize
);
243 if (EFI_ERROR(Status
)) {
244 Print (L
"ERROR: Did not find initrd image.\n");
248 // Check if the initrd is a uInitrd
249 if (*(UINT32
*)((UINTN
)InitrdImage
) == LINUX_UIMAGE_SIGNATURE
) {
250 // Skip the 64-byte image header
251 InitrdImage
= (EFI_PHYSICAL_ADDRESS
)((UINTN
)InitrdImage
+ 64);
252 InitrdImageSize
-= 64;
256 // Load the FDT binary from a device path. The FDT will be reloaded later to a more appropriate location for the Linux kernel.
258 Status
= BdsLoadImage (FdtDevicePath
, AllocateAnyPages
, &FdtBlobBase
, &FdtBlobSize
);
259 if (EFI_ERROR(Status
)) {
260 Print (L
"ERROR: Did not find Device Tree blob.\n");
264 // Update the Fdt with the Initrd information. The FDT will increase in size.
265 // By setting address=0 we leave the memory allocation to the function
266 Status
= PrepareFdt (CommandLineArguments
, InitrdImage
, InitrdImageSize
, &FdtBlobBase
, &FdtBlobSize
);
267 if (EFI_ERROR(Status
)) {
268 Print(L
"ERROR: Can not load kernel with FDT. Status=%r\n", Status
);
272 return StartLinux (LinuxImage
, LinuxImageSize
, FdtBlobBase
, FdtBlobSize
, ARM_FDT_MACHINE_TYPE
);