1 From 9c16a23fba659cdf0ce798aa085a4fb8c3bd47d1 Mon Sep 17 00:00:00 2001
2 From: Olivier Martin <olivier.martin@arm.com>
3 Date: Thu, 16 Feb 2012 15:50:59 +0000
4 Subject: [PATCH 2/3] ArmPkg/BdsLib: Added support for modifying the passed FDT blob
6 - Add Linux CmdLine if not defined
7 - Add initrd if not defined
8 - Add CPU parking address if not defined
9 - Add System Memory info if not defined
11 ArmPkg/ArmPkg.dsc | 1 +
12 ArmPkg/Library/BdsLib/BdsInternal.h | 9 +
13 ArmPkg/Library/BdsLib/BdsLib.inf | 3 +
14 ArmPkg/Library/BdsLib/BdsLinuxFdt.c | 353 ++++++++++++++++++++++++++++++++
15 ArmPkg/Library/BdsLib/BdsLinuxLoader.c | 8 +
16 ArmPkg/Library/BdsLib/BdsLinuxLoader.h | 10 +-
17 6 files changed, 383 insertions(+), 1 deletions(-)
18 mode change 100644 => 100755 ArmPkg/ArmPkg.dsc
19 mode change 100644 => 100755 ArmPkg/Library/BdsLib/BdsInternal.h
20 mode change 100644 => 100755 ArmPkg/Library/BdsLib/BdsLib.inf
21 create mode 100755 ArmPkg/Library/BdsLib/BdsLinuxFdt.c
23 diff --git a/ArmPkg/ArmPkg.dsc b/ArmPkg/ArmPkg.dsc
26 index f4989a6..07c825d
27 --- a/ArmPkg/ArmPkg.dsc
28 +++ b/ArmPkg/ArmPkg.dsc
30 SerialPortLib|MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf
32 BdsLib|ArmPkg/Library/BdsLib/BdsLib.inf
33 + FdtLib|EmbeddedPkg/Library/FdtLib/FdtLib.inf
35 IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
37 diff --git a/ArmPkg/Library/BdsLib/BdsInternal.h b/ArmPkg/Library/BdsLib/BdsInternal.h
40 index 880d780..80d21b2
41 --- a/ArmPkg/Library/BdsLib/BdsInternal.h
42 +++ b/ArmPkg/Library/BdsLib/BdsInternal.h
43 @@ -103,4 +103,13 @@ PrepareAtagList (
49 + IN CONST CHAR8* CommandLineString,
50 + IN EFI_PHYSICAL_ADDRESS InitrdImage,
51 + IN UINTN InitrdImageSize,
52 + IN OUT EFI_PHYSICAL_ADDRESS *FdtBlobBase,
53 + IN OUT UINT32 *FdtBlobSize
57 diff --git a/ArmPkg/Library/BdsLib/BdsLib.inf b/ArmPkg/Library/BdsLib/BdsLib.inf
60 index 20644f1..b3cab21
61 --- a/ArmPkg/Library/BdsLib/BdsLib.inf
62 +++ b/ArmPkg/Library/BdsLib/BdsLib.inf
82 gEfiBdsArchProtocolGuid
83 diff --git a/ArmPkg/Library/BdsLib/BdsLinuxFdt.c b/ArmPkg/Library/BdsLib/BdsLinuxFdt.c
85 index 0000000..5c14b65
87 +++ b/ArmPkg/Library/BdsLib/BdsLinuxFdt.c
91 +* Copyright (c) 2011-2012, ARM Limited. All rights reserved.
93 +* This program and the accompanying materials
94 +* are licensed and made available under the terms and conditions of the BSD License
95 +* which accompanies this distribution. The full text of the license may be found at
96 +* http://opensource.org/licenses/bsd-license.php
98 +* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
99 +* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
103 +#include <Library/PcdLib.h>
106 +#include "BdsInternal.h"
108 +#define LINUX_FDT_MAX_OFFSET (PcdGet32(PcdSystemMemoryBase) + PcdGet32(PcdArmLinuxAtagMaxOffset))
110 +#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
111 +#define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a))))
112 +#define GET_CELL(p) (p += 4, *((const UINT32 *)(p-4)))
117 + IN CONST VOID* data,
121 + CONST CHAR8 *s = data;
124 + // zero length is not
129 + // must terminate with zero
130 + if (s[len - 1] != '\0') {
135 + while (*s/* && isprint(*s)*/) {
139 + // not zero, or not done yet
140 + if (*s != '\0' || (s + 1 - ss) < len) {
150 + IN CONST CHAR8* data,
155 + CONST CHAR8 *p = data;
157 + // no data, don't print
161 + if (IsPrintableString (data, len)) {
162 + Print(L" = \"%a\"", (const char *)data);
163 + } else if ((len % 4) == 0) {
165 + for (i = 0; i < len; i += 4) {
166 + Print(L"0x%08x%a", fdt32_to_cpu(GET_CELL(p)),i < (len - 4) ? " " : "");
171 + for (i = 0; i < len; i++)
172 + Print(L"%02x%a", *p++, i < len - 1 ? " " : "");
182 + struct fdt_header *bph;
185 + CONST CHAR8* p_struct;
186 + CONST CHAR8* p_strings;
200 + off_dt = fdt32_to_cpu(bph->off_dt_struct);
201 + off_str = fdt32_to_cpu(bph->off_dt_strings);
202 + p_struct = (CONST CHAR8*)FdtBlob + off_dt;
203 + p_strings = (CONST CHAR8*)FdtBlob + off_str;
204 + version = fdt32_to_cpu(bph->version);
207 + while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {
209 + //printf("tag: 0x%08x (%d)\n", tag, p - p_struct);
211 + if (tag == FDT_BEGIN_NODE) {
213 + p = PALIGN(p + AsciiStrLen (s) + 1, 4);
218 + Print(L"%*s%a {\n", depth * shift, L" ", s);
224 + if (tag == FDT_END_NODE) {
227 + Print(L"%*s};\n", depth * shift, L" ");
231 + if (tag == FDT_NOP) {
232 + Print(L"%*s// [NOP]\n", depth * shift, L" ");
236 + if (tag != FDT_PROP) {
237 + Print(L"%*s ** Unknown tag 0x%08x\n", depth * shift, L" ", tag);
240 + sz = fdt32_to_cpu(GET_CELL(p));
241 + s = p_strings + fdt32_to_cpu(GET_CELL(p));
242 + if (version < 16 && sz >= 8)
246 + p = PALIGN(p + sz, 4);
248 + Print(L"%*s%a", depth * shift, L" ", s);
261 + IN CONST CHAR8* CommandLineString,
262 + IN EFI_PHYSICAL_ADDRESS InitrdImage,
263 + IN UINTN InitrdImageSize,
264 + IN OUT EFI_PHYSICAL_ADDRESS *FdtBlobBase,
265 + IN OUT UINT32 *FdtBlobSize
269 + EFI_PHYSICAL_ADDRESS NewFdtBlobBase;
270 + UINTN NewFdtBlobSize;
276 + CONST VOID* BootArg;
277 + EFI_PHYSICAL_ADDRESS InitrdImageStart;
278 + EFI_PHYSICAL_ADDRESS InitrdImageEnd;
282 + LIST_ENTRY ResourceList;
283 + BDS_SYSTEM_MEMORY_RESOURCE *Resource;
284 + ARM_PROCESSOR_TABLE *ArmProcessorTable;
285 + ARM_CORE_INFO *ArmCoreInfoTable;
289 + UINT64 CpuReleaseAddr;
291 + err = fdt_check_header ((VOID*)(UINTN)(*FdtBlobBase));
293 + Print (L"ERROR: Device Tree header not valid (err:%d)\n", err);
294 + return EFI_INVALID_PARAMETER;
297 + // Allocate memory for the new FDT
298 + NewFdtBlobBase = LINUX_FDT_MAX_OFFSET;
299 + NewFdtBlobSize = *FdtBlobSize + FDT_ADDITIONAL_ENTRIES_SIZE;
300 + Status = gBS->AllocatePages (AllocateMaxAddress, EfiBootServicesData, EFI_SIZE_TO_PAGES(NewFdtBlobSize), &NewFdtBlobBase);
301 + if (EFI_ERROR(Status)) {
302 + DEBUG ((EFI_D_WARN, "Warning: Failed to allocate Fdt below 0x%lX (%r). The Fdt will be allocated somewhere else in System Memory.\n",NewFdtBlobBase,Status));
303 + Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, EFI_SIZE_TO_PAGES(NewFdtBlobSize), &NewFdtBlobBase);
304 + ASSERT_EFI_ERROR(Status);
308 + // Load the Original FDT tree into the new region
309 + fdt = (VOID*)(UINTN)NewFdtBlobBase;
310 + err = fdt_open_into((VOID*)(UINTN)(*FdtBlobBase), fdt, NewFdtBlobSize);
312 + DEBUG((EFI_D_ERROR, "fdt_open_into(): %a\n", fdt_strerror(err)));
313 + Status = EFI_INVALID_PARAMETER;
317 + DEBUG_CODE_BEGIN();
318 + DebugDumpFdt (fdt);
321 + node = fdt_subnode_offset(fdt, 0, "chosen");
323 + // The 'chosen' node does not exist, create it
324 + node = fdt_add_subnode(fdt, 0, "chosen");
326 + DEBUG((EFI_D_ERROR,"Error on finding 'chosen' node\n"));
327 + Status = EFI_INVALID_PARAMETER;
332 + DEBUG_CODE_BEGIN();
333 + BootArg = fdt_getprop(fdt, node, "bootargs", &lenp);
334 + if (BootArg != NULL) {
335 + DEBUG((EFI_D_ERROR,"BootArg: %a\n",BootArg));
339 + // Set Linux CmdLine
340 + if ((CommandLineString != NULL) && (AsciiStrLen (CommandLineString) > 0)) {
341 + err = fdt_setprop(fdt, node, "bootargs", CommandLineString, AsciiStrSize(CommandLineString));
343 + DEBUG((EFI_D_ERROR,"Fail to set new 'bootarg' (err:%d)\n",err));
347 + // Set Linux Initrd
348 + if (InitrdImageSize != 0) {
349 + InitrdImageStart = cpu_to_fdt64 (InitrdImage);
350 + err = fdt_setprop(fdt, node, "linux,initrd-start", &InitrdImageStart, sizeof(EFI_PHYSICAL_ADDRESS));
352 + DEBUG((EFI_D_ERROR,"Fail to set new 'linux,initrd-start' (err:%d)\n",err));
354 + InitrdImageEnd = cpu_to_fdt64 (InitrdImage + InitrdImageSize);
355 + err = fdt_setprop(fdt, node, "linux,initrd-end", &InitrdImageEnd, sizeof(EFI_PHYSICAL_ADDRESS));
357 + DEBUG((EFI_D_ERROR,"Fail to set new 'linux,initrd-start' (err:%d)\n",err));
361 + // Set Physical memory setup if does not exist
362 + node = fdt_subnode_offset(fdt, 0, "memory");
364 + // The 'chosen' node does not exist, create it
365 + node = fdt_add_subnode(fdt, 0, "memory");
367 + fdt_setprop_string(fdt, node, "name", "memory");
368 + fdt_setprop_string(fdt, node, "device_type", "memory");
370 + GetSystemMemoryResources (&ResourceList);
371 + Resource = (BDS_SYSTEM_MEMORY_RESOURCE*)ResourceList.ForwardLink;
373 + if (sizeof(UINTN) == sizeof(UINT32)) {
374 + Region.Base = cpu_to_fdt32((UINTN)Resource->PhysicalStart);
375 + Region.Size = cpu_to_fdt32((UINTN)Resource->ResourceLength);
377 + Region.Base = cpu_to_fdt64((UINTN)Resource->PhysicalStart);
378 + Region.Size = cpu_to_fdt64((UINTN)Resource->ResourceLength);
381 + err = fdt_setprop(fdt, node, "reg", &Region, sizeof(Region));
383 + DEBUG((EFI_D_ERROR,"Fail to set new 'memory region' (err:%d)\n",err));
388 + // Setup Arm Mpcore Info if it is a multi-core or multi-cluster platforms
389 + for (Index=0; Index < gST->NumberOfTableEntries; Index++) {
390 + // Check for correct GUID type
391 + if (CompareGuid (&gArmMpCoreInfoGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {
392 + MpId = ArmReadMpidr ();
393 + ClusterId = GET_CLUSTER_ID(MpId);
394 + CoreId = GET_CORE_ID(MpId);
396 + node = fdt_subnode_offset(fdt, 0, "cpus");
398 + // Create the /cpus node
399 + node = fdt_add_subnode(fdt, 0, "cpus");
400 + fdt_setprop_string(fdt, node, "name", "cpus");
401 + fdt_setprop_cell(fdt, node, "#address-cells", 1);
402 + fdt_setprop_cell(fdt, node, "#size-cells", 0);
405 + // Get pointer to ARM processor table
406 + ArmProcessorTable = (ARM_PROCESSOR_TABLE *)gST->ConfigurationTable[Index].VendorTable;
407 + ArmCoreInfoTable = ArmProcessorTable->ArmCpus;
409 + for (Index = 0; Index < ArmProcessorTable->NumberOfEntries; Index++) {
410 + if (((ArmCoreInfoTable[Index].ClusterId != ClusterId) || (ArmCoreInfoTable[Index].CoreId != CoreId))) {
411 + AsciiSPrint (Name, 10, "cpu@%d", Index);
412 + cpu_node = fdt_subnode_offset(fdt, node, Name);
413 + if (cpu_node < 0) {
414 + cpu_node = fdt_add_subnode(fdt, node, Name);
416 + fdt_setprop_string(fdt, cpu_node, "device-type", "cpu");
417 + fdt_setprop_string(fdt, cpu_node, "enable-method", "spin-table");
418 + fdt_setprop_string(fdt, cpu_node, "status", "disabled");
419 + CpuReleaseAddr = cpu_to_fdt64(ArmCoreInfoTable[Index].MailboxSetAddress);
420 + fdt_setprop(fdt, cpu_node, "cpu-release-addr", &CpuReleaseAddr, sizeof(CpuReleaseAddr));
427 + DEBUG_CODE_BEGIN();
428 + DebugDumpFdt (fdt);
431 + *FdtBlobBase = NewFdtBlobBase;
432 + *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(NewFdtBlobBase));;
433 + return EFI_SUCCESS;
436 + *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(*FdtBlobBase));
437 + // Return success even if we failed to update the FDT blob. The original one is still valid.
438 + return EFI_SUCCESS;
442 diff --git a/ArmPkg/Library/BdsLib/BdsLinuxLoader.c b/ArmPkg/Library/BdsLib/BdsLinuxLoader.c
443 index 12a8862..82fa811 100755
444 --- a/ArmPkg/Library/BdsLib/BdsLinuxLoader.c
445 +++ b/ArmPkg/Library/BdsLib/BdsLinuxLoader.c
446 @@ -241,6 +241,14 @@ BdsBootLinuxFdt (
447 Print (L"ERROR: Did not find Device Tree blob.\n");
451 + // By setting address=0 we leave the memory allocation to the function
452 + Status = PrepareFdt (Arguments, InitrdImage, InitrdImageSize, &KernelParamsAddress, &KernelParamsSize);
453 + if (EFI_ERROR(Status)) {
454 + Print(L"ERROR: Can not load Linux kernel with Device Tree. Status=0x%X\n", Status);
458 return StartLinux (LinuxImage, LinuxImageSize, KernelParamsAddress, KernelParamsSize, FdtMachineType);
461 diff --git a/ArmPkg/Library/BdsLib/BdsLinuxLoader.h b/ArmPkg/Library/BdsLib/BdsLinuxLoader.h
462 index 8d58ce1..9e45e03 100755
463 --- a/ArmPkg/Library/BdsLib/BdsLinuxLoader.h
464 +++ b/ArmPkg/Library/BdsLib/BdsLinuxLoader.h
466 #ifndef __BDSLINUXLOADER_H
467 #define __BDSLINUXLOADER_H
469 +#include <Guid/ArmMpCoreInfo.h>
471 #define LINUX_UIMAGE_SIGNATURE 0x56190527
473 #define LINUX_ATAG_MAX_OFFSET (PcdGet32(PcdSystemMemoryBase) + PcdGet32(PcdArmLinuxAtagMaxOffset))
474 #define LINUX_KERNEL_MAX_OFFSET (PcdGet32(PcdSystemMemoryBase) + PcdGet32(PcdArmLinuxKernelMaxOffset))
476 -#define ATAG_MAX_SIZE 0x3000
477 +// Size allocated for the Atag list
478 +#define ATAG_MAX_SIZE 0x3000
480 +// Additional size that could be used for FDT entries added by the UEFI OS Loader
481 +// Estimation based on: EDID (300bytes) + bootargs (200bytes) + initrd region (20bytes)
482 +// + system memory region (20bytes) + mp_core entries (200 bytes)
483 +#define FDT_ADDITIONAL_ENTRIES_SIZE 0x300
485 /* ATAG : list of possible tags */
486 #define ATAG_NONE 0x00000000