]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/BdsLib/Arm/BdsLinuxLoader.c
ARM Packages: Removed trailing spaces
[mirror_edk2.git] / ArmPkg / Library / BdsLib / Arm / BdsLinuxLoader.c
1 /** @file
2 *
3 * Copyright (c) 2011-2012, ARM Limited. All rights reserved.
4 *
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
9 *
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.
12 *
13 **/
14
15 #include "BdsInternal.h"
16 #include "BdsLinuxLoader.h"
17
18 #define ALIGN32_BELOW(addr) ALIGN_POINTER(addr - 32,32)
19
20 STATIC
21 EFI_STATUS
22 PreparePlatformHardware (
23 VOID
24 )
25 {
26 //Note: Interrupts will be disabled by the GIC driver when ExitBootServices() will be called.
27
28 // Clean, invalidate, disable data cache
29 ArmDisableDataCache();
30 ArmCleanInvalidateDataCache();
31
32 // Invalidate and disable the Instruction cache
33 ArmDisableInstructionCache ();
34 ArmInvalidateInstructionCache ();
35
36 // Turn off MMU
37 ArmDisableMmu();
38
39 return EFI_SUCCESS;
40 }
41
42 STATIC
43 EFI_STATUS
44 StartLinux (
45 IN EFI_PHYSICAL_ADDRESS LinuxImage,
46 IN UINTN LinuxImageSize,
47 IN EFI_PHYSICAL_ADDRESS KernelParamsAddress,
48 IN UINTN KernelParamsSize,
49 IN UINT32 MachineType
50 )
51 {
52 EFI_STATUS Status;
53 LINUX_KERNEL LinuxKernel;
54
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));
60 goto Exit;
61 }
62
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
66 // physical memory.
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);
71 }
72 } else {
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);
75 }
76 }
77
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);
81 } else {
82 LinuxKernel = (LINUX_KERNEL)(UINTN)LinuxImage;
83 }
84
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);
89 LinuxImageSize -= 64;
90 }
91
92 //TODO: Check there is no overlapping between kernel and Atag
93
94 //
95 // Switch off interrupts, caches, mmu, etc
96 //
97 Status = PreparePlatformHardware ();
98 ASSERT_EFI_ERROR(Status);
99
100 // Register and print out performance information
101 PERF_END (NULL, "BDS", NULL, 0);
102 if (PerformanceMeasurementEnabled ()) {
103 PrintPerformance ();
104 }
105
106 //
107 // Start the Linux Kernel
108 //
109
110 // Outside BootServices, so can't use Print();
111 DEBUG((EFI_D_ERROR, "\nStarting the kernel:\n\n"));
112
113 // Jump to kernel with register set
114 LinuxKernel ((UINTN)0, MachineType, (UINTN)KernelParamsAddress);
115
116 // Kernel should never exit
117 // After Life services are not provided
118 ASSERT(FALSE);
119
120 Exit:
121 // Only be here if we fail to start Linux
122 Print (L"ERROR : Can not start the kernel. Status=0x%X\n", Status);
123
124 // Free Runtimee Memory (kernel and FDT)
125 return Status;
126 }
127
128 /**
129 Start a Linux kernel from a Device Path
130
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
134
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.
138
139 **/
140 EFI_STATUS
141 BdsBootLinuxAtag (
142 IN EFI_DEVICE_PATH_PROTOCOL* LinuxKernelDevicePath,
143 IN EFI_DEVICE_PATH_PROTOCOL* InitrdDevicePath,
144 IN CONST CHAR8* CommandLineArguments
145 )
146 {
147 EFI_STATUS Status;
148 UINT32 LinuxImageSize;
149 UINT32 InitrdImageBaseSize = 0;
150 UINT32 InitrdImageSize = 0;
151 UINT32 AtagSize;
152 EFI_PHYSICAL_ADDRESS AtagBase;
153 EFI_PHYSICAL_ADDRESS LinuxImage;
154 EFI_PHYSICAL_ADDRESS InitrdImageBase = 0;
155 EFI_PHYSICAL_ADDRESS InitrdImage = 0;
156
157 PERF_START (NULL, "BDS", NULL, 0);
158
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");
164 return Status;
165 }
166
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);
173 }
174 if (EFI_ERROR(Status)) {
175 Print (L"ERROR: Did not find initrd image.\n");
176 goto EXIT_FREE_LINUX;
177 }
178
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;
184 } else {
185 InitrdImage = InitrdImageBase;
186 InitrdImageSize = InitrdImageBaseSize;
187 }
188 }
189
190 //
191 // Setup the Linux Kernel Parameters
192 //
193
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;
199 }
200
201 return StartLinux (LinuxImage, LinuxImageSize, AtagBase, AtagSize, PcdGet32(PcdArmMachineType));
202
203 EXIT_FREE_INITRD:
204 if (InitrdDevicePath) {
205 gBS->FreePages (InitrdImageBase, EFI_SIZE_TO_PAGES (InitrdImageBaseSize));
206 }
207
208 EXIT_FREE_LINUX:
209 gBS->FreePages (LinuxImage, EFI_SIZE_TO_PAGES (LinuxImageSize));
210
211 return Status;
212 }
213
214 /**
215 Start a Linux kernel from a Device Path
216
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
220
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.
224
225 **/
226 EFI_STATUS
227 BdsBootLinuxFdt (
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
232 )
233 {
234 EFI_STATUS Status;
235 UINT32 LinuxImageSize;
236 UINT32 InitrdImageBaseSize = 0;
237 UINT32 InitrdImageSize = 0;
238 UINT32 FdtBlobSize;
239 EFI_PHYSICAL_ADDRESS FdtBlobBase;
240 EFI_PHYSICAL_ADDRESS LinuxImage;
241 EFI_PHYSICAL_ADDRESS InitrdImageBase = 0;
242 EFI_PHYSICAL_ADDRESS InitrdImage = 0;
243
244 PERF_START (NULL, "BDS", NULL, 0);
245
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");
251 return Status;
252 }
253
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);
259 }
260 if (EFI_ERROR(Status)) {
261 Print (L"ERROR: Did not find initrd image.\n");
262 goto EXIT_FREE_LINUX;
263 }
264
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;
270 } else {
271 InitrdImage = InitrdImageBase;
272 InitrdImageSize = InitrdImageBaseSize;
273 }
274 }
275
276 // Load the FDT binary from a device path. The FDT will be reloaded later to a more appropriate location for the Linux kernel.
277 FdtBlobBase = 0;
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;
282 }
283
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);
289 goto EXIT_FREE_FDT;
290 }
291
292 return StartLinux (LinuxImage, LinuxImageSize, FdtBlobBase, FdtBlobSize, ARM_FDT_MACHINE_TYPE);
293
294 EXIT_FREE_FDT:
295 gBS->FreePages (FdtBlobBase, EFI_SIZE_TO_PAGES (FdtBlobSize));
296
297 EXIT_FREE_INITRD:
298 if (InitrdDevicePath) {
299 gBS->FreePages (InitrdImageBase, EFI_SIZE_TO_PAGES (InitrdImageBaseSize));
300 }
301
302 EXIT_FREE_LINUX:
303 gBS->FreePages (LinuxImage, EFI_SIZE_TO_PAGES (LinuxImageSize));
304
305 return Status;
306 }
307