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 InitrdImageBaseSize
= 0;
150 UINT32 InitrdImageSize
= 0;
152 EFI_PHYSICAL_ADDRESS AtagBase
;
153 EFI_PHYSICAL_ADDRESS LinuxImage
;
154 EFI_PHYSICAL_ADDRESS InitrdImageBase
= 0;
155 EFI_PHYSICAL_ADDRESS InitrdImage
= 0;
157 PERF_START (NULL
, "BDS", NULL
, 0);
159 // Load the Linux kernel from a device path
160 LinuxImage
= LINUX_KERNEL_MAX_OFFSET
;
161 Status
= BdsLoadImage (LinuxKernelDevicePath
, AllocateMaxAddress
, &LinuxImage
, &LinuxImageSize
);
162 if (EFI_ERROR(Status
)) {
163 Print (L
"ERROR: Did not find Linux kernel.\n");
167 if (InitrdDevicePath
) {
168 // Load the initrd near to the Linux kernel
169 InitrdImageBase
= LINUX_KERNEL_MAX_OFFSET
;
170 Status
= BdsLoadImage (InitrdDevicePath
, AllocateMaxAddress
, &InitrdImageBase
, &InitrdImageBaseSize
);
171 if (Status
== EFI_OUT_OF_RESOURCES
) {
172 Status
= BdsLoadImage (InitrdDevicePath
, AllocateAnyPages
, &InitrdImageBase
, &InitrdImageBaseSize
);
174 if (EFI_ERROR(Status
)) {
175 Print (L
"ERROR: Did not find initrd image.\n");
176 goto EXIT_FREE_LINUX
;
179 // Check if the initrd is a uInitrd
180 if (*(UINT32
*)((UINTN
)InitrdImageBase
) == LINUX_UIMAGE_SIGNATURE
) {
181 // Skip the 64-byte image header
182 InitrdImage
= (EFI_PHYSICAL_ADDRESS
)((UINTN
)InitrdImageBase
+ 64);
183 InitrdImageSize
= InitrdImageBaseSize
- 64;
185 InitrdImage
= InitrdImageBase
;
186 InitrdImageSize
= InitrdImageBaseSize
;
191 // Setup the Linux Kernel Parameters
194 // By setting address=0 we leave the memory allocation to the function
195 Status
= PrepareAtagList (CommandLineArguments
, InitrdImage
, InitrdImageSize
, &AtagBase
, &AtagSize
);
196 if (EFI_ERROR(Status
)) {
197 Print(L
"ERROR: Can not prepare ATAG list. Status=0x%X\n", Status
);
198 goto EXIT_FREE_INITRD
;
201 return StartLinux (LinuxImage
, LinuxImageSize
, AtagBase
, AtagSize
, PcdGet32(PcdArmMachineType
));
204 if (InitrdDevicePath
) {
205 gBS
->FreePages (InitrdImageBase
, EFI_SIZE_TO_PAGES (InitrdImageBaseSize
));
209 gBS
->FreePages (LinuxImage
, EFI_SIZE_TO_PAGES (LinuxImageSize
));
215 Start a Linux kernel from a Device Path
217 @param LinuxKernel Device Path to the Linux Kernel
218 @param Parameters Linux kernel arguments
219 @param Fdt Device Path to the Flat Device Tree
221 @retval EFI_SUCCESS All drivers have been connected
222 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
223 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
228 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
229 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
230 IN CONST CHAR8
* CommandLineArguments
,
231 IN EFI_DEVICE_PATH_PROTOCOL
* FdtDevicePath
235 UINT32 LinuxImageSize
;
236 UINT32 InitrdImageBaseSize
= 0;
237 UINT32 InitrdImageSize
= 0;
239 EFI_PHYSICAL_ADDRESS FdtBlobBase
;
240 EFI_PHYSICAL_ADDRESS LinuxImage
;
241 EFI_PHYSICAL_ADDRESS InitrdImageBase
= 0;
242 EFI_PHYSICAL_ADDRESS InitrdImage
= 0;
244 PERF_START (NULL
, "BDS", NULL
, 0);
246 // Load the Linux kernel from a device path
247 LinuxImage
= LINUX_KERNEL_MAX_OFFSET
;
248 Status
= BdsLoadImage (LinuxKernelDevicePath
, AllocateMaxAddress
, &LinuxImage
, &LinuxImageSize
);
249 if (EFI_ERROR(Status
)) {
250 Print (L
"ERROR: Did not find Linux kernel.\n");
254 if (InitrdDevicePath
) {
255 InitrdImageBase
= LINUX_KERNEL_MAX_OFFSET
;
256 Status
= BdsLoadImage (InitrdDevicePath
, AllocateMaxAddress
, &InitrdImageBase
, &InitrdImageBaseSize
);
257 if (Status
== EFI_OUT_OF_RESOURCES
) {
258 Status
= BdsLoadImage (InitrdDevicePath
, AllocateAnyPages
, &InitrdImageBase
, &InitrdImageBaseSize
);
260 if (EFI_ERROR(Status
)) {
261 Print (L
"ERROR: Did not find initrd image.\n");
262 goto EXIT_FREE_LINUX
;
265 // Check if the initrd is a uInitrd
266 if (*(UINT32
*)((UINTN
)InitrdImageBase
) == LINUX_UIMAGE_SIGNATURE
) {
267 // Skip the 64-byte image header
268 InitrdImage
= (EFI_PHYSICAL_ADDRESS
)((UINTN
)InitrdImageBase
+ 64);
269 InitrdImageSize
= InitrdImageBaseSize
- 64;
271 InitrdImage
= InitrdImageBase
;
272 InitrdImageSize
= InitrdImageBaseSize
;
276 // Load the FDT binary from a device path. The FDT will be reloaded later to a more appropriate location for the Linux kernel.
278 Status
= BdsLoadImage (FdtDevicePath
, AllocateAnyPages
, &FdtBlobBase
, &FdtBlobSize
);
279 if (EFI_ERROR(Status
)) {
280 Print (L
"ERROR: Did not find Device Tree blob.\n");
281 goto EXIT_FREE_INITRD
;
284 // Update the Fdt with the Initrd information. The FDT will increase in size.
285 // By setting address=0 we leave the memory allocation to the function
286 Status
= PrepareFdt (CommandLineArguments
, InitrdImage
, InitrdImageSize
, &FdtBlobBase
, &FdtBlobSize
);
287 if (EFI_ERROR(Status
)) {
288 Print(L
"ERROR: Can not load kernel with FDT. Status=%r\n", Status
);
292 return StartLinux (LinuxImage
, LinuxImageSize
, FdtBlobBase
, FdtBlobSize
, ARM_FDT_MACHINE_TYPE
);
295 gBS
->FreePages (FdtBlobBase
, EFI_SIZE_TO_PAGES (FdtBlobSize
));
298 if (InitrdDevicePath
) {
299 gBS
->FreePages (InitrdImageBase
, EFI_SIZE_TO_PAGES (InitrdImageBaseSize
));
303 gBS
->FreePages (LinuxImage
, EFI_SIZE_TO_PAGES (LinuxImageSize
));