]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/BdsLib/Arm/BdsLinuxLoader.c
ArmPkg/BdsLinuxFdt.c: Check that FDT blob is correctly loaded.
[mirror_edk2.git] / ArmPkg / Library / BdsLib / Arm / BdsLinuxLoader.c
CommitLineData
1e57a462 1/** @file\r
2*\r
3* Copyright (c) 2011-2012, ARM Limited. All rights reserved.\r
4* \r
5* This program and the accompanying materials \r
6* are licensed and made available under the terms and conditions of the BSD License \r
7* which accompanies this distribution. The full text of the license may be found at \r
8* http://opensource.org/licenses/bsd-license.php \r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12*\r
13**/\r
14\r
15#include "BdsInternal.h"\r
16#include "BdsLinuxLoader.h"\r
17\r
18#define ALIGN32_BELOW(addr) ALIGN_POINTER(addr - 32,32)\r
19\r
20STATIC\r
21EFI_STATUS\r
22PreparePlatformHardware (\r
23 VOID\r
24 )\r
25{\r
26 //Note: Interrupts will be disabled by the GIC driver when ExitBootServices() will be called.\r
27\r
28 // Clean, invalidate, disable data cache\r
29 ArmDisableDataCache();\r
30 ArmCleanInvalidateDataCache();\r
31\r
32 // Invalidate and disable the Instruction cache\r
33 ArmDisableInstructionCache ();\r
34 ArmInvalidateInstructionCache ();\r
35\r
36 // Turn off MMU\r
37 ArmDisableMmu();\r
38\r
39 return EFI_SUCCESS;\r
40}\r
41\r
42STATIC\r
43EFI_STATUS\r
44StartLinux (\r
45 IN EFI_PHYSICAL_ADDRESS LinuxImage,\r
46 IN UINTN LinuxImageSize,\r
47 IN EFI_PHYSICAL_ADDRESS KernelParamsAddress,\r
48 IN UINTN KernelParamsSize,\r
49 IN UINT32 MachineType\r
50 )\r
51{\r
52 EFI_STATUS Status;\r
53 LINUX_KERNEL LinuxKernel;\r
54\r
55 // Shut down UEFI boot services. ExitBootServices() will notify every driver that created an event on\r
56 // ExitBootServices event. Example the Interrupt DXE driver will disable the interrupts on this event.\r
57 Status = ShutdownUefiBootServices ();\r
58 if(EFI_ERROR(Status)) {\r
59 DEBUG((EFI_D_ERROR,"ERROR: Can not shutdown UEFI boot services. Status=0x%X\n", Status));\r
60 goto Exit;\r
61 }\r
62\r
63 // Move the kernel parameters to any address inside the first 1MB.\r
64 // This is necessary because the ARM Linux kernel requires\r
65 // the FTD / ATAG List to reside entirely inside the first 1MB of\r
66 // physical memory.\r
67 //Note: There is no requirement on the alignment\r
68 if (MachineType != ARM_FDT_MACHINE_TYPE) {\r
69 if (((UINTN)KernelParamsAddress > LINUX_ATAG_MAX_OFFSET) && (KernelParamsSize < PcdGet32(PcdArmLinuxAtagMaxOffset))) {\r
70 KernelParamsAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)CopyMem (ALIGN32_BELOW(LINUX_ATAG_MAX_OFFSET - KernelParamsSize), (VOID*)(UINTN)KernelParamsAddress, KernelParamsSize);\r
71 }\r
72 } else {\r
73 if (((UINTN)KernelParamsAddress > LINUX_FDT_MAX_OFFSET) && (KernelParamsSize < PcdGet32(PcdArmLinuxFdtMaxOffset))) {\r
74 KernelParamsAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)CopyMem (ALIGN32_BELOW(LINUX_FDT_MAX_OFFSET - KernelParamsSize), (VOID*)(UINTN)KernelParamsAddress, KernelParamsSize);\r
75 }\r
76 }\r
77\r
78 if ((UINTN)LinuxImage > LINUX_KERNEL_MAX_OFFSET) {\r
79 //Note: There is no requirement on the alignment\r
80 LinuxKernel = (LINUX_KERNEL)CopyMem (ALIGN32_BELOW(LINUX_KERNEL_MAX_OFFSET - LinuxImageSize), (VOID*)(UINTN)LinuxImage, LinuxImageSize);\r
81 } else {\r
82 LinuxKernel = (LINUX_KERNEL)(UINTN)LinuxImage;\r
83 }\r
84\r
85 // Check if the Linux Image is a uImage\r
86 if (*(UINT32*)LinuxKernel == LINUX_UIMAGE_SIGNATURE) {\r
87 // Assume the Image Entry Point is just after the uImage header (64-byte size)\r
88 LinuxKernel = (LINUX_KERNEL)((UINTN)LinuxKernel + 64);\r
89 LinuxImageSize -= 64;\r
90 }\r
91\r
92 //TODO: Check there is no overlapping between kernel and Atag\r
93\r
94 //\r
95 // Switch off interrupts, caches, mmu, etc\r
96 //\r
97 Status = PreparePlatformHardware ();\r
98 ASSERT_EFI_ERROR(Status);\r
99\r
100 // Register and print out performance information\r
101 PERF_END (NULL, "BDS", NULL, 0);\r
102 if (PerformanceMeasurementEnabled ()) {\r
103 PrintPerformance ();\r
104 }\r
105\r
106 //\r
107 // Start the Linux Kernel\r
108 //\r
109\r
110 // Outside BootServices, so can't use Print();\r
111 DEBUG((EFI_D_ERROR, "\nStarting the kernel:\n\n"));\r
112\r
113 // Jump to kernel with register set\r
114 LinuxKernel ((UINTN)0, MachineType, (UINTN)KernelParamsAddress);\r
115\r
116 // Kernel should never exit\r
117 // After Life services are not provided\r
118 ASSERT(FALSE);\r
119\r
120Exit:\r
121 // Only be here if we fail to start Linux\r
122 Print (L"ERROR : Can not start the kernel. Status=0x%X\n", Status);\r
123\r
124 // Free Runtimee Memory (kernel and FDT)\r
125 return Status;\r
126}\r
127\r
128/**\r
129 Start a Linux kernel from a Device Path\r
130\r
131 @param LinuxKernel Device Path to the Linux Kernel\r
132 @param Parameters Linux kernel arguments\r
133 @param Fdt Device Path to the Flat Device Tree\r
134\r
135 @retval EFI_SUCCESS All drivers have been connected\r
136 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found\r
137 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.\r
138\r
139**/\r
140EFI_STATUS\r
141BdsBootLinuxAtag (\r
142 IN EFI_DEVICE_PATH_PROTOCOL* LinuxKernelDevicePath,\r
143 IN EFI_DEVICE_PATH_PROTOCOL* InitrdDevicePath,\r
144 IN CONST CHAR8* CommandLineArguments\r
145 )\r
146{\r
147 EFI_STATUS Status;\r
148 UINT32 LinuxImageSize;\r
149 UINT32 InitrdImageSize = 0;\r
150 UINT32 AtagSize;\r
151 EFI_PHYSICAL_ADDRESS AtagBase;\r
152 EFI_PHYSICAL_ADDRESS LinuxImage;\r
153 EFI_PHYSICAL_ADDRESS InitrdImage;\r
154\r
155 PERF_START (NULL, "BDS", NULL, 0);\r
156\r
157 // Load the Linux kernel from a device path\r
158 LinuxImage = LINUX_KERNEL_MAX_OFFSET;\r
159 Status = BdsLoadImage (LinuxKernelDevicePath, AllocateMaxAddress, &LinuxImage, &LinuxImageSize);\r
160 if (EFI_ERROR(Status)) {\r
161 Print (L"ERROR: Did not find Linux kernel.\n");\r
162 return Status;\r
163 }\r
164\r
165 if (InitrdDevicePath) {\r
166 // Load the initrd near to the Linux kernel\r
167 InitrdImage = LINUX_KERNEL_MAX_OFFSET;\r
168 Status = BdsLoadImage (InitrdDevicePath, AllocateMaxAddress, &InitrdImage, &InitrdImageSize);\r
169 if (Status == EFI_OUT_OF_RESOURCES) {\r
170 Status = BdsLoadImage (InitrdDevicePath, AllocateAnyPages, &InitrdImage, &InitrdImageSize);\r
171 }\r
172 if (EFI_ERROR(Status)) {\r
173 Print (L"ERROR: Did not find initrd image.\n");\r
174 return Status;\r
175 }\r
176 \r
177 // Check if the initrd is a uInitrd\r
178 if (*(UINT32*)((UINTN)InitrdImage) == LINUX_UIMAGE_SIGNATURE) {\r
179 // Skip the 64-byte image header\r
180 InitrdImage = (EFI_PHYSICAL_ADDRESS)((UINTN)InitrdImage + 64);\r
181 InitrdImageSize -= 64;\r
182 }\r
183 }\r
184\r
185 //\r
186 // Setup the Linux Kernel Parameters\r
187 //\r
188 \r
189 // By setting address=0 we leave the memory allocation to the function\r
190 Status = PrepareAtagList (CommandLineArguments, InitrdImage, InitrdImageSize, &AtagBase, &AtagSize);\r
191 if (EFI_ERROR(Status)) {\r
192 Print(L"ERROR: Can not prepare ATAG list. Status=0x%X\n", Status);\r
193 return Status;\r
194 }\r
195\r
196 return StartLinux (LinuxImage, LinuxImageSize, AtagBase, AtagSize, PcdGet32(PcdArmMachineType));\r
197}\r
198\r
199/**\r
200 Start a Linux kernel from a Device Path\r
201\r
202 @param LinuxKernel Device Path to the Linux Kernel\r
203 @param Parameters Linux kernel arguments\r
204 @param Fdt Device Path to the Flat Device Tree\r
205\r
206 @retval EFI_SUCCESS All drivers have been connected\r
207 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found\r
208 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.\r
209\r
210**/\r
211EFI_STATUS\r
212BdsBootLinuxFdt (\r
213 IN EFI_DEVICE_PATH_PROTOCOL* LinuxKernelDevicePath,\r
214 IN EFI_DEVICE_PATH_PROTOCOL* InitrdDevicePath,\r
215 IN CONST CHAR8* CommandLineArguments,\r
216 IN EFI_DEVICE_PATH_PROTOCOL* FdtDevicePath\r
217 )\r
218{\r
219 EFI_STATUS Status;\r
220 UINT32 LinuxImageSize;\r
221 UINT32 InitrdImageSize = 0;\r
222 UINT32 FdtBlobSize;\r
223 EFI_PHYSICAL_ADDRESS FdtBlobBase;\r
224 EFI_PHYSICAL_ADDRESS LinuxImage;\r
225 EFI_PHYSICAL_ADDRESS InitrdImage;\r
226\r
227 PERF_START (NULL, "BDS", NULL, 0);\r
228\r
229 // Load the Linux kernel from a device path\r
230 LinuxImage = LINUX_KERNEL_MAX_OFFSET;\r
231 Status = BdsLoadImage (LinuxKernelDevicePath, AllocateMaxAddress, &LinuxImage, &LinuxImageSize);\r
232 if (EFI_ERROR(Status)) {\r
233 Print (L"ERROR: Did not find Linux kernel.\n");\r
234 return Status;\r
235 }\r
236\r
237 if (InitrdDevicePath) {\r
238 InitrdImage = LINUX_KERNEL_MAX_OFFSET;\r
239 Status = BdsLoadImage (InitrdDevicePath, AllocateMaxAddress, &InitrdImage, &InitrdImageSize);\r
240 if (Status == EFI_OUT_OF_RESOURCES) {\r
241 Status = BdsLoadImage (InitrdDevicePath, AllocateAnyPages, &InitrdImage, &InitrdImageSize);\r
242 }\r
243 if (EFI_ERROR(Status)) {\r
244 Print (L"ERROR: Did not find initrd image.\n");\r
245 return Status;\r
246 }\r
247\r
248 // Check if the initrd is a uInitrd\r
249 if (*(UINT32*)((UINTN)InitrdImage) == LINUX_UIMAGE_SIGNATURE) {\r
250 // Skip the 64-byte image header\r
251 InitrdImage = (EFI_PHYSICAL_ADDRESS)((UINTN)InitrdImage + 64);\r
252 InitrdImageSize -= 64;\r
253 }\r
254 }\r
255\r
256 // Load the FDT binary from a device path. The FDT will be reloaded later to a more appropriate location for the Linux kernel.\r
257 FdtBlobBase = 0;\r
258 Status = BdsLoadImage (FdtDevicePath, AllocateAnyPages, &FdtBlobBase, &FdtBlobSize);\r
259 if (EFI_ERROR(Status)) {\r
260 Print (L"ERROR: Did not find Device Tree blob.\n");\r
261 return Status;\r
262 }\r
263\r
264 // Update the Fdt with the Initrd information. The FDT will increase in size.\r
265 // By setting address=0 we leave the memory allocation to the function\r
266 Status = PrepareFdt (CommandLineArguments, InitrdImage, InitrdImageSize, &FdtBlobBase, &FdtBlobSize);\r
267 if (EFI_ERROR(Status)) {\r
268 Print(L"ERROR: Can not load kernel with FDT. Status=%r\n", Status);\r
269 return Status;\r
270 }\r
271\r
272 return StartLinux (LinuxImage, LinuxImageSize, FdtBlobBase, FdtBlobSize, ARM_FDT_MACHINE_TYPE);\r
273}\r
274\r