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 if ((UINTN
)KernelParamsAddress
> LINUX_ATAG_MAX_OFFSET
) {
68 //Note: There is no requirement on the alignment
69 KernelParamsAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)CopyMem (ALIGN32_BELOW(LINUX_ATAG_MAX_OFFSET
- KernelParamsSize
), (VOID
*)(UINTN
)KernelParamsAddress
, KernelParamsSize
);
72 if ((UINTN
)LinuxImage
> LINUX_KERNEL_MAX_OFFSET
) {
73 //Note: There is no requirement on the alignment
74 LinuxKernel
= (LINUX_KERNEL
)CopyMem (ALIGN32_BELOW(LINUX_KERNEL_MAX_OFFSET
- LinuxImageSize
), (VOID
*)(UINTN
)LinuxImage
, LinuxImageSize
);
76 LinuxKernel
= (LINUX_KERNEL
)(UINTN
)LinuxImage
;
79 // Check if the Linux Image is a uImage
80 if (*(UINT32
*)LinuxKernel
== LINUX_UIMAGE_SIGNATURE
) {
81 // Assume the Image Entry Point is just after the uImage header (64-byte size)
82 LinuxKernel
= (LINUX_KERNEL
)((UINTN
)LinuxKernel
+ 64);
86 //TODO: Check there is no overlapping between kernel and Atag
89 // Switch off interrupts, caches, mmu, etc
91 Status
= PreparePlatformHardware ();
92 ASSERT_EFI_ERROR(Status
);
94 // Register and print out performance information
95 PERF_END (NULL
, "BDS", NULL
, 0);
96 if (PerformanceMeasurementEnabled ()) {
101 // Start the Linux Kernel
104 // Outside BootServices, so can't use Print();
105 DEBUG((EFI_D_ERROR
, "\nStarting the kernel:\n\n"));
107 // Jump to kernel with register set
108 LinuxKernel ((UINTN
)0, MachineType
, (UINTN
)KernelParamsAddress
);
110 // Kernel should never exit
111 // After Life services are not provided
115 // Only be here if we fail to start Linux
116 Print (L
"ERROR : Can not start the kernel. Status=0x%X\n", Status
);
118 // Free Runtimee Memory (kernel and FDT)
123 Start a Linux kernel from a Device Path
125 @param LinuxKernel Device Path to the Linux Kernel
126 @param Parameters Linux kernel arguments
127 @param Fdt Device Path to the Flat Device Tree
129 @retval EFI_SUCCESS All drivers have been connected
130 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
131 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
136 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
137 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
138 IN CONST CHAR8
* Arguments
142 UINT32 LinuxImageSize
;
143 UINT32 InitrdImageSize
= 0;
144 UINT32 KernelParamsSize
;
145 EFI_PHYSICAL_ADDRESS KernelParamsAddress
;
146 EFI_PHYSICAL_ADDRESS LinuxImage
;
147 EFI_PHYSICAL_ADDRESS InitrdImage
;
149 PERF_START (NULL
, "BDS", NULL
, 0);
151 // Load the Linux kernel from a device path
152 LinuxImage
= LINUX_KERNEL_MAX_OFFSET
;
153 Status
= BdsLoadImage (LinuxKernelDevicePath
, AllocateMaxAddress
, &LinuxImage
, &LinuxImageSize
);
154 if (EFI_ERROR(Status
)) {
155 Print (L
"ERROR: Did not find Linux kernel.\n");
159 if (InitrdDevicePath
) {
160 // Load the initrd near to the Linux kernel
161 InitrdImage
= LINUX_KERNEL_MAX_OFFSET
;
162 Status
= BdsLoadImage (InitrdDevicePath
, AllocateMaxAddress
, &InitrdImage
, &InitrdImageSize
);
163 if (Status
== EFI_OUT_OF_RESOURCES
) {
164 Status
= BdsLoadImage (InitrdDevicePath
, AllocateAnyPages
, &InitrdImage
, &InitrdImageSize
);
166 if (EFI_ERROR(Status
)) {
167 Print (L
"ERROR: Did not find initrd image.\n");
171 // Check if the initrd is a uInitrd
172 if (*(UINT32
*)((UINTN
)InitrdImage
) == LINUX_UIMAGE_SIGNATURE
) {
173 // Skip the 64-byte image header
174 InitrdImage
= (EFI_PHYSICAL_ADDRESS
)((UINTN
)InitrdImage
+ 64);
175 InitrdImageSize
-= 64;
180 // Setup the Linux Kernel Parameters
183 // By setting address=0 we leave the memory allocation to the function
184 Status
= PrepareAtagList (Arguments
, InitrdImage
, InitrdImageSize
, &KernelParamsAddress
, &KernelParamsSize
);
185 if (EFI_ERROR(Status
)) {
186 Print(L
"ERROR: Can not prepare ATAG list. Status=0x%X\n", Status
);
190 return StartLinux (LinuxImage
, LinuxImageSize
, KernelParamsAddress
, KernelParamsSize
, PcdGet32(PcdArmMachineType
));
194 Start a Linux kernel from a Device Path
196 @param LinuxKernel Device Path to the Linux Kernel
197 @param Parameters Linux kernel arguments
198 @param Fdt Device Path to the Flat Device Tree
200 @retval EFI_SUCCESS All drivers have been connected
201 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
202 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
207 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
208 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
209 IN CONST CHAR8
* Arguments
,
210 IN EFI_DEVICE_PATH_PROTOCOL
* FdtDevicePath
214 UINT32 LinuxImageSize
;
215 UINT32 InitrdImageSize
= 0;
216 UINT32 KernelParamsSize
;
217 EFI_PHYSICAL_ADDRESS KernelParamsAddress
;
218 UINT32 FdtMachineType
;
219 EFI_PHYSICAL_ADDRESS LinuxImage
;
220 EFI_PHYSICAL_ADDRESS InitrdImage
;
222 FdtMachineType
= 0xFFFFFFFF;
224 PERF_START (NULL
, "BDS", NULL
, 0);
226 // Load the Linux kernel from a device path
227 LinuxImage
= LINUX_KERNEL_MAX_OFFSET
;
228 Status
= BdsLoadImage (LinuxKernelDevicePath
, AllocateMaxAddress
, &LinuxImage
, &LinuxImageSize
);
229 if (EFI_ERROR(Status
)) {
230 Print (L
"ERROR: Did not find Linux kernel.\n");
234 if (InitrdDevicePath
) {
235 InitrdImage
= LINUX_KERNEL_MAX_OFFSET
;
236 Status
= BdsLoadImage (InitrdDevicePath
, AllocateMaxAddress
, &InitrdImage
, &InitrdImageSize
);
237 if (Status
== EFI_OUT_OF_RESOURCES
) {
238 Status
= BdsLoadImage (InitrdDevicePath
, AllocateAnyPages
, &InitrdImage
, &InitrdImageSize
);
240 if (EFI_ERROR(Status
)) {
241 Print (L
"ERROR: Did not find initrd image.\n");
245 // Check if the initrd is a uInitrd
246 if (*(UINT32
*)((UINTN
)InitrdImage
) == LINUX_UIMAGE_SIGNATURE
) {
247 // Skip the 64-byte image header
248 InitrdImage
= (EFI_PHYSICAL_ADDRESS
)((UINTN
)InitrdImage
+ 64);
249 InitrdImageSize
-= 64;
253 // Load the FDT binary from a device path
254 KernelParamsAddress
= LINUX_ATAG_MAX_OFFSET
;
255 Status
= BdsLoadImage (FdtDevicePath
, AllocateMaxAddress
, &KernelParamsAddress
, &KernelParamsSize
);
256 if (EFI_ERROR(Status
)) {
257 Print (L
"ERROR: Did not find Device Tree blob.\n");
260 return StartLinux (LinuxImage
, LinuxImageSize
, KernelParamsAddress
, KernelParamsSize
, FdtMachineType
);