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"
17 #define ALIGN32_BELOW(addr) ALIGN_POINTER(addr - 32,32)
21 PreparePlatformHardware (
25 //Note: Interrupts will be disabled by the GIC driver when ExitBootServices() will be called.
27 // Clean, invalidate, disable data cache
28 ArmDisableDataCache();
29 ArmCleanInvalidateDataCache();
31 // Invalidate and disable the Instruction cache
32 ArmDisableInstructionCache ();
33 ArmInvalidateInstructionCache ();
44 IN EFI_PHYSICAL_ADDRESS LinuxImage
,
45 IN UINTN LinuxImageSize
,
46 IN EFI_PHYSICAL_ADDRESS KernelParamsAddress
,
47 IN UINTN KernelParamsSize
,
52 LINUX_KERNEL LinuxKernel
;
54 // Shut down UEFI boot services. ExitBootServices() will notify every driver that created an event on
55 // ExitBootServices event. Example the Interrupt DXE driver will disable the interrupts on this event.
56 Status
= ShutdownUefiBootServices ();
57 if(EFI_ERROR(Status
)) {
58 DEBUG((EFI_D_ERROR
,"ERROR: Can not shutdown UEFI boot services. Status=0x%X\n", Status
));
62 // Move the kernel parameters to any address inside the first 1MB.
63 // This is necessary because the ARM Linux kernel requires
64 // the FTD / ATAG List to reside entirely inside the first 1MB of
66 if ((UINTN
)KernelParamsAddress
> LINUX_ATAG_MAX_OFFSET
) {
67 //Note: There is no requirement on the alignment
68 KernelParamsAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)CopyMem (ALIGN32_BELOW(LINUX_ATAG_MAX_OFFSET
- KernelParamsSize
), (VOID
*)(UINTN
)KernelParamsAddress
, KernelParamsSize
);
71 if ((UINTN
)LinuxImage
> LINUX_KERNEL_MAX_OFFSET
) {
72 //Note: There is no requirement on the alignment
73 LinuxKernel
= (LINUX_KERNEL
)CopyMem (ALIGN32_BELOW(LINUX_KERNEL_MAX_OFFSET
- LinuxImageSize
), (VOID
*)(UINTN
)LinuxImage
, LinuxImageSize
);
75 LinuxKernel
= (LINUX_KERNEL
)(UINTN
)LinuxImage
;
78 // Check if the Linux Image is a uImage
79 if (*(UINT32
*)LinuxKernel
== LINUX_UIMAGE_SIGNATURE
) {
80 // Assume the Image Entry Point is just after the uImage header (64-byte size)
81 LinuxKernel
= (LINUX_KERNEL
)((UINTN
)LinuxKernel
+ 64);
85 //TODO: Check there is no overlapping between kernel and Atag
88 // Switch off interrupts, caches, mmu, etc
90 Status
= PreparePlatformHardware ();
91 ASSERT_EFI_ERROR(Status
);
93 // Register and print out performance information
94 PERF_END (NULL
, "BDS", NULL
, 0);
95 if (PerformanceMeasurementEnabled ()) {
100 // Start the Linux Kernel
103 // Outside BootServices, so can't use Print();
104 DEBUG((EFI_D_ERROR
, "\nStarting the kernel:\n\n"));
106 // Jump to kernel with register set
107 LinuxKernel ((UINTN
)0, MachineType
, (UINTN
)KernelParamsAddress
);
109 // Kernel should never exit
110 // After Life services are not provided
114 // Only be here if we fail to start Linux
115 Print (L
"ERROR : Can not start the kernel. Status=0x%X\n", Status
);
117 // Free Runtimee Memory (kernel and FDT)
122 Start a Linux kernel from a Device Path
124 @param LinuxKernel Device Path to the Linux Kernel
125 @param Parameters Linux kernel agruments
126 @param Fdt Device Path to the Flat Device Tree
128 @retval EFI_SUCCESS All drivers have been connected
129 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
130 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
135 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
136 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
137 IN CONST CHAR8
* Arguments
141 UINT32 LinuxImageSize
;
142 UINT32 InitrdImageSize
= 0;
143 UINT32 KernelParamsSize
;
144 EFI_PHYSICAL_ADDRESS KernelParamsAddress
;
145 EFI_PHYSICAL_ADDRESS LinuxImage
;
146 EFI_PHYSICAL_ADDRESS InitrdImage
;
148 PERF_START (NULL
, "BDS", NULL
, 0);
150 // Load the Linux kernel from a device path
151 LinuxImage
= LINUX_KERNEL_MAX_OFFSET
;
152 Status
= BdsLoadImage (LinuxKernelDevicePath
, AllocateMaxAddress
, &LinuxImage
, &LinuxImageSize
);
153 if (EFI_ERROR(Status
)) {
154 Print (L
"ERROR: Did not find Linux kernel.\n");
158 if (InitrdDevicePath
) {
159 // Load the initrd near to the Linux kernel
160 InitrdImage
= LINUX_KERNEL_MAX_OFFSET
;
161 Status
= BdsLoadImage (InitrdDevicePath
, AllocateMaxAddress
, &InitrdImage
, &InitrdImageSize
);
162 if (Status
== EFI_OUT_OF_RESOURCES
) {
163 Status
= BdsLoadImage (InitrdDevicePath
, AllocateAnyPages
, &InitrdImage
, &InitrdImageSize
);
165 if (EFI_ERROR(Status
)) {
166 Print (L
"ERROR: Did not find initrd image.\n");
170 // Check if the initrd is a uInitrd
171 if (*(UINT32
*)((UINTN
)InitrdImage
) == LINUX_UIMAGE_SIGNATURE
) {
172 // Skip the 64-byte image header
173 InitrdImage
= (EFI_PHYSICAL_ADDRESS
)((UINTN
)InitrdImage
+ 64);
174 InitrdImageSize
-= 64;
179 // Setup the Linux Kernel Parameters
182 // By setting address=0 we leave the memory allocation to the function
183 Status
= PrepareAtagList (Arguments
, InitrdImage
, InitrdImageSize
, &KernelParamsAddress
, &KernelParamsSize
);
184 if (EFI_ERROR(Status
)) {
185 Print(L
"ERROR: Can not prepare ATAG list. Status=0x%X\n", Status
);
189 return StartLinux (LinuxImage
, LinuxImageSize
, KernelParamsAddress
, KernelParamsSize
, PcdGet32(PcdArmMachineType
));
193 Start a Linux kernel from a Device Path
195 @param LinuxKernel Device Path to the Linux Kernel
196 @param Parameters Linux kernel agruments
197 @param Fdt Device Path to the Flat Device Tree
199 @retval EFI_SUCCESS All drivers have been connected
200 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
201 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
206 IN EFI_DEVICE_PATH_PROTOCOL
* LinuxKernelDevicePath
,
207 IN EFI_DEVICE_PATH_PROTOCOL
* InitrdDevicePath
,
208 IN CONST CHAR8
* Arguments
,
209 IN EFI_DEVICE_PATH_PROTOCOL
* FdtDevicePath
213 UINT32 LinuxImageSize
;
214 UINT32 InitrdImageSize
= 0;
215 UINT32 KernelParamsSize
;
216 EFI_PHYSICAL_ADDRESS KernelParamsAddress
;
217 UINT32 FdtMachineType
;
218 EFI_PHYSICAL_ADDRESS LinuxImage
;
219 EFI_PHYSICAL_ADDRESS InitrdImage
;
221 FdtMachineType
= 0xFFFFFFFF;
223 PERF_START (NULL
, "BDS", NULL
, 0);
225 // Load the Linux kernel from a device path
226 LinuxImage
= LINUX_KERNEL_MAX_OFFSET
;
227 Status
= BdsLoadImage (LinuxKernelDevicePath
, AllocateMaxAddress
, &LinuxImage
, &LinuxImageSize
);
228 if (EFI_ERROR(Status
)) {
229 Print (L
"ERROR: Did not find Linux kernel.\n");
233 if (InitrdDevicePath
) {
234 InitrdImage
= LINUX_KERNEL_MAX_OFFSET
;
235 Status
= BdsLoadImage (InitrdDevicePath
, AllocateMaxAddress
, &InitrdImage
, &InitrdImageSize
);
236 if (Status
== EFI_OUT_OF_RESOURCES
) {
237 Status
= BdsLoadImage (InitrdDevicePath
, AllocateAnyPages
, &InitrdImage
, &InitrdImageSize
);
239 if (EFI_ERROR(Status
)) {
240 Print (L
"ERROR: Did not find initrd image.\n");
244 // Check if the initrd is a uInitrd
245 if (*(UINT32
*)((UINTN
)InitrdImage
) == LINUX_UIMAGE_SIGNATURE
) {
246 // Skip the 64-byte image header
247 InitrdImage
= (EFI_PHYSICAL_ADDRESS
)((UINTN
)InitrdImage
+ 64);
248 InitrdImageSize
-= 64;
252 // Load the FDT binary from a device path
253 KernelParamsAddress
= LINUX_ATAG_MAX_OFFSET
;
254 Status
= BdsLoadImage (FdtDevicePath
, AllocateMaxAddress
, &KernelParamsAddress
, &KernelParamsSize
);
255 if (EFI_ERROR(Status
)) {
256 Print (L
"ERROR: Did not find Device Tree blob.\n");
259 return StartLinux (LinuxImage
, LinuxImageSize
, KernelParamsAddress
, KernelParamsSize
, FdtMachineType
);