]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/BdsLib/BdsLinuxLoader.c
2b42b28ff4094e46182413a73116db4ecab59220
[mirror_edk2.git] / ArmPkg / Library / BdsLib / BdsLinuxLoader.c
1 /** @file
2 *
3 * Copyright (c) 2011, 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 #include <Library/PcdLib.h>
19 #include <Library/ArmLib.h>
20 #include <Library/HobLib.h>
21
22 #define ALIGN32_BELOW(addr) ALIGN_POINTER(addr - 32,32)
23
24 #define LINUX_ATAG_MAX_OFFSET (PcdGet32(PcdSystemMemoryBase) + PcdGet32(PcdArmLinuxAtagMaxOffset))
25 #define LINUX_KERNEL_MAX_OFFSET (PcdGet32(PcdSystemMemoryBase) + PcdGet32(PcdArmLinuxKernelMaxOffset))
26
27 // Point to the current ATAG
28 STATIC LINUX_ATAG *mLinuxKernelCurrentAtag;
29
30 STATIC
31 VOID
32 SetupCoreTag (
33 IN UINT32 PageSize
34 )
35 {
36 mLinuxKernelCurrentAtag->header.size = tag_size(LINUX_ATAG_CORE);
37 mLinuxKernelCurrentAtag->header.type = ATAG_CORE;
38
39 mLinuxKernelCurrentAtag->body.core_tag.flags = 1; /* ensure read-only */
40 mLinuxKernelCurrentAtag->body.core_tag.pagesize = PageSize; /* systems PageSize (4k) */
41 mLinuxKernelCurrentAtag->body.core_tag.rootdev = 0; /* zero root device (typically overridden from kernel command line )*/
42
43 // move pointer to next tag
44 mLinuxKernelCurrentAtag = next_tag_address(mLinuxKernelCurrentAtag);
45 }
46
47 STATIC
48 VOID
49 SetupMemTag (
50 IN UINTN StartAddress,
51 IN UINT32 Size
52 )
53 {
54 mLinuxKernelCurrentAtag->header.size = tag_size(LINUX_ATAG_MEM);
55 mLinuxKernelCurrentAtag->header.type = ATAG_MEM;
56
57 mLinuxKernelCurrentAtag->body.mem_tag.start = StartAddress; /* Start of memory chunk for AtagMem */
58 mLinuxKernelCurrentAtag->body.mem_tag.size = Size; /* Size of memory chunk for AtagMem */
59
60 // move pointer to next tag
61 mLinuxKernelCurrentAtag = next_tag_address(mLinuxKernelCurrentAtag);
62 }
63
64 STATIC
65 VOID
66 SetupCmdlineTag (
67 IN CONST CHAR8 *CmdLine
68 )
69 {
70 UINT32 LineLength;
71
72 // Increment the line length by 1 to account for the null string terminator character
73 LineLength = AsciiStrLen(CmdLine) + 1;
74
75 /* Check for NULL strings.
76 * Do not insert a tag for an empty CommandLine, don't even modify the tag address pointer.
77 * Remember, you have at least one null string terminator character.
78 */
79 if(LineLength > 1) {
80 mLinuxKernelCurrentAtag->header.size = ((UINT32)sizeof(LINUX_ATAG_HEADER) + LineLength + (UINT32)3) >> 2;
81 mLinuxKernelCurrentAtag->header.type = ATAG_CMDLINE;
82
83 /* place CommandLine into tag */
84 AsciiStrCpy(mLinuxKernelCurrentAtag->body.cmdline_tag.cmdline, CmdLine);
85
86 // move pointer to next tag
87 mLinuxKernelCurrentAtag = next_tag_address(mLinuxKernelCurrentAtag);
88 }
89 }
90
91 STATIC
92 VOID
93 SetupEndTag (
94 VOID
95 )
96 {
97 // Empty tag ends list; this has zero length and no body
98 mLinuxKernelCurrentAtag->header.type = ATAG_NONE;
99 mLinuxKernelCurrentAtag->header.size = 0;
100
101 /* We can not calculate the next address by using the standard macro:
102 * Params = next_tag_address(Params);
103 * because it relies on the header.size, which here it is 0 (zero).
104 * The easiest way is to add the sizeof(mLinuxKernelCurrentAtag->header).
105 */
106 mLinuxKernelCurrentAtag = (LINUX_ATAG*)((UINT32)mLinuxKernelCurrentAtag + sizeof(mLinuxKernelCurrentAtag->header));
107 }
108
109 STATIC
110 EFI_STATUS
111 PrepareAtagList (
112 IN CONST CHAR8* CommandLineString,
113 IN EFI_PHYSICAL_ADDRESS InitrdImage,
114 IN UINTN InitrdImageSize,
115 OUT LINUX_ATAG **AtagBase,
116 OUT UINT32 *AtagSize
117 )
118 {
119 EFI_STATUS Status;
120 LIST_ENTRY *ResourceLink;
121 LIST_ENTRY ResourceList;
122 EFI_PHYSICAL_ADDRESS AtagStartAddress;
123 BDS_SYSTEM_MEMORY_RESOURCE *Resource;
124
125 AtagStartAddress = LINUX_ATAG_MAX_OFFSET;
126 Status = gBS->AllocatePages (AllocateMaxAddress, EfiBootServicesData, EFI_SIZE_TO_PAGES(ATAG_MAX_SIZE), &AtagStartAddress);
127 if (EFI_ERROR(Status)) {
128 DEBUG ((EFI_D_ERROR,"Failed to allocate Atag at 0x%lX (%r)\n",AtagStartAddress,Status));
129 Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, EFI_SIZE_TO_PAGES(ATAG_MAX_SIZE), &AtagStartAddress);
130 ASSERT_EFI_ERROR(Status);
131 }
132
133 // Ready to setup the atag list
134 mLinuxKernelCurrentAtag = (LINUX_ATAG*)(UINTN)AtagStartAddress;
135
136 // Standard core tag 4k PageSize
137 SetupCoreTag( (UINT32)SIZE_4KB );
138
139 // Physical memory setup
140 GetSystemMemoryResources (&ResourceList);
141 ResourceLink = ResourceList.ForwardLink;
142 while (ResourceLink != NULL && ResourceLink != &ResourceList) {
143 Resource = (BDS_SYSTEM_MEMORY_RESOURCE*)ResourceLink;
144 DEBUG((EFI_D_INFO,"- [0x%08X,0x%08X]\n",(UINT32)Resource->PhysicalStart,(UINT32)Resource->PhysicalStart+(UINT32)Resource->ResourceLength));
145 SetupMemTag( (UINT32)Resource->PhysicalStart, (UINT32)Resource->ResourceLength );
146 ResourceLink = ResourceLink->ForwardLink;
147 }
148
149 // CommandLine setting root device
150 SetupCmdlineTag (CommandLineString);
151
152 if (InitrdImageSize > 0 && InitrdImage != 0) {
153 mLinuxKernelCurrentAtag->header.size = tag_size(LINUX_ATAG_INITRD2);
154 mLinuxKernelCurrentAtag->header.type = ATAG_INITRD2;
155
156 mLinuxKernelCurrentAtag->body.initrd2_tag.start = (UINT32)InitrdImage;
157 mLinuxKernelCurrentAtag->body.initrd2_tag.size = (UINT32)InitrdImageSize;
158
159 // Move pointer to next tag
160 mLinuxKernelCurrentAtag = next_tag_address(mLinuxKernelCurrentAtag);
161 }
162
163 // end of tags
164 SetupEndTag();
165
166 // Calculate atag list size
167 *AtagBase = (LINUX_ATAG*)(UINTN)AtagStartAddress;
168 *AtagSize = (UINT32)mLinuxKernelCurrentAtag - (UINT32)AtagStartAddress + 1;
169
170 return EFI_SUCCESS;
171 }
172
173 STATIC
174 EFI_STATUS
175 PreparePlatformHardware (
176 VOID
177 )
178 {
179 //Note: Interrupts will be disabled by the GIC driver when ExitBootServices() will be called.
180
181 // clean, invalidate, disable data cache
182 ArmCleanInvalidateDataCache();
183 ArmDisableDataCache();
184
185 // Invalidate and disable the Instruction cache
186 ArmInvalidateInstructionCache ();
187 ArmDisableInstructionCache ();
188
189 // turn off MMU
190 ArmDisableMmu();
191
192 return EFI_SUCCESS;
193 }
194
195 /**
196 Start a Linux kernel from a Device Path
197
198 @param LinuxKernel Device Path to the Linux Kernel
199 @param Parameters Linux kernel agruments
200 @param Fdt Device Path to the Flat Device Tree
201
202 @retval EFI_SUCCESS All drivers have been connected
203 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
204 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
205
206 **/
207 EFI_STATUS
208 BdsBootLinux (
209 IN EFI_DEVICE_PATH_PROTOCOL* LinuxKernelDevicePath,
210 IN EFI_DEVICE_PATH_PROTOCOL* InitrdDevicePath,
211 IN CONST CHAR8* Arguments,
212 IN EFI_DEVICE_PATH_PROTOCOL* FdtDevicePath
213 )
214 {
215 EFI_STATUS Status;
216 UINT32 LinuxImageSize;
217 UINT32 InitrdImageSize;
218 UINT32 KernelParamsSize;
219 EFI_PHYSICAL_ADDRESS KernelParamsAddress;
220 UINT32 MachineType;
221 BOOLEAN FdtSupported = FALSE;
222 LINUX_KERNEL LinuxKernel;
223 EFI_PHYSICAL_ADDRESS LinuxImage;
224 EFI_PHYSICAL_ADDRESS InitrdImage;
225
226 // Ensure the System Memory PCDs have been initialized (PcdSystemMemoryBase and PcdSystemMemorySize)
227 ASSERT (PcdGet32(PcdSystemMemorySize) != 0);
228
229 PERF_START (NULL, "BDS", NULL, 0);
230
231 // Load the Linux kernel from a device path
232 LinuxImage = LINUX_KERNEL_MAX_OFFSET;
233 Status = BdsLoadImage (LinuxKernelDevicePath, AllocateMaxAddress, &LinuxImage, &LinuxImageSize);
234 if (EFI_ERROR(Status)) {
235 Print (L"ERROR: Did not find Linux kernel.\n");
236 return Status;
237 }
238 LinuxKernel = (LINUX_KERNEL)(UINTN)LinuxImage;
239
240 if (InitrdDevicePath) {
241 InitrdImageSize = 0;
242 Status = BdsLoadImage (InitrdDevicePath, AllocateAnyPages, &InitrdImage, &InitrdImageSize);
243 if (EFI_ERROR(Status)) {
244 Print (L"ERROR: Did not find initrd image.\n");
245 return Status;
246 }
247 }
248
249 if (FdtDevicePath) {
250 // Load the FDT binary from a device path
251 KernelParamsAddress = LINUX_ATAG_MAX_OFFSET;
252 Status = BdsLoadImage (FdtDevicePath, AllocateMaxAddress, &KernelParamsAddress, &KernelParamsSize);
253 if (EFI_ERROR(Status)) {
254 Print (L"ERROR: Did not find Device Tree blob.\n");
255 return Status;
256 }
257 FdtSupported = TRUE;
258 }
259
260 //
261 // Setup the Linux Kernel Parameters
262 //
263 if (!FdtSupported) {
264 // Non-FDT requires a specific machine type.
265 // This OS Boot loader supports just one machine type,
266 // but that could change in the future.
267 MachineType = PcdGet32(PcdArmMachineType);
268
269 // By setting address=0 we leave the memory allocation to the function
270 Status = PrepareAtagList (Arguments, InitrdImage, InitrdImageSize, (LINUX_ATAG**)&KernelParamsAddress, &KernelParamsSize);
271 if(EFI_ERROR(Status)) {
272 Print(L"ERROR: Can not prepare ATAG list. Status=0x%X\n", Status);
273 goto Exit;
274 }
275 } else {
276 MachineType = 0xFFFFFFFF;
277 }
278
279 // Shut down UEFI boot services. ExitBootServices() will notify every driver that created an event on
280 // ExitBootServices event. Example the Interrupt DXE driver will disable the interrupts on this event.
281 Status = ShutdownUefiBootServices ();
282 if(EFI_ERROR(Status)) {
283 DEBUG((EFI_D_ERROR,"ERROR: Can not shutdown UEFI boot services. Status=0x%X\n", Status));
284 goto Exit;
285 }
286
287 // Move the kernel parameters to any address inside the first 1MB.
288 // This is necessary because the ARM Linux kernel requires
289 // the FTD / ATAG List to reside entirely inside the first 1MB of
290 // physical memory.
291 if ((UINTN)KernelParamsAddress > LINUX_ATAG_MAX_OFFSET) {
292 //Note: There is no requirement on the alignment
293 KernelParamsAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)CopyMem (ALIGN32_BELOW(LINUX_ATAG_MAX_OFFSET - KernelParamsSize), (VOID*)(UINTN)KernelParamsAddress, KernelParamsSize);
294 }
295
296 if ((UINTN)LinuxImage > LINUX_KERNEL_MAX_OFFSET) {
297 //Note: There is no requirement on the alignment
298 LinuxKernel = (LINUX_KERNEL)CopyMem (ALIGN32_BELOW(LINUX_KERNEL_MAX_OFFSET - LinuxImageSize), (VOID*)(UINTN)LinuxImage, LinuxImageSize);
299 }
300
301 //TODO: Check there is no overlapping between kernel and Atag
302
303 //
304 // Switch off interrupts, caches, mmu, etc
305 //
306 Status = PreparePlatformHardware ();
307 ASSERT_EFI_ERROR(Status);
308
309 // Register and print out performance information
310 PERF_END (NULL, "BDS", NULL, 0);
311 if (PerformanceMeasurementEnabled ()) {
312 PrintPerformance ();
313 }
314
315 //
316 // Start the Linux Kernel
317 //
318
319 // Outside BootServices, so can't use Print();
320 DEBUG((EFI_D_ERROR, "\nStarting the kernel:\n\n"));
321
322 // jump to kernel with register set
323 LinuxKernel ((UINTN)0, (UINTN)MachineType, (UINTN)KernelParamsAddress);
324
325 // Kernel should never exit
326 // After Life services are not provided
327 ASSERT(FALSE);
328
329 Exit:
330 // Only be here if we fail to start Linux
331 Print (L"ERROR : Can not start the kernel. Status=0x%X\n", Status);
332
333 // Free Runtimee Memory (kernel and FDT)
334 return Status;
335 }