From 0a6653bc2aecaa621eb42c7137b6f13815a34a92 Mon Sep 17 00:00:00 2001 From: oliviermartin Date: Fri, 28 Sep 2012 09:58:42 +0000 Subject: [PATCH] ArmPkg/BdsLib: Added FDT support for BdsLib Signed-off-by: Olivier Martin git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13768 6f19259b-4bc3-4df7-8a09-765794883524 --- ArmPkg/ArmPkg.dec | 5 + ArmPkg/Library/BdsLib/BdsAppLoader.c | 2 +- ArmPkg/Library/BdsLib/BdsFilePath.c | 2 +- ArmPkg/Library/BdsLib/BdsHelper.c | 3 +- ArmPkg/Library/BdsLib/BdsInternal.h | 14 +- ArmPkg/Library/BdsLib/BdsLib.inf | 10 +- ArmPkg/Library/BdsLib/BdsLinuxAtag.c | 30 +- ArmPkg/Library/BdsLib/BdsLinuxFdt.c | 368 ++++++++++++++++++ ArmPkg/Library/BdsLib/BdsLinuxLoader.c | 48 ++- ArmPkg/Library/BdsLib/BdsLinuxLoader.h | 44 ++- .../ArmRealViewEbPkg/ArmRealViewEb.dsc.inc | 1 + .../ArmVExpressPkg/ArmVExpress.dsc.inc | 1 + BeagleBoardPkg/BeagleBoardPkg.dsc | 1 + 13 files changed, 476 insertions(+), 53 deletions(-) create mode 100644 ArmPkg/Library/BdsLib/BdsLinuxFdt.c diff --git a/ArmPkg/ArmPkg.dec b/ArmPkg/ArmPkg.dec index cd2578f05e..7a4aa79933 100644 --- a/ArmPkg/ArmPkg.dec +++ b/ArmPkg/ArmPkg.dec @@ -160,10 +160,15 @@ # BdsLib # gArmTokenSpaceGuid.PcdArmMachineType|0|UINT32|0x0000001E + # The compressed Linux kernel is expected to load at MemStart + 0x8000 (e.g. 0x8000_8000) + gArmTokenSpaceGuid.PcdArmLinuxKernelFixedOffset|0x00008000|UINT32|0x00000027 # The compressed Linux kernel is expected to be under 128MB from the beginning of the System Memory gArmTokenSpaceGuid.PcdArmLinuxKernelMaxOffset|0x08000000|UINT32|0x0000001F # The Linux ATAGs are expected to be under 0x4000 (16KB) from the beginning of the System Memory gArmTokenSpaceGuid.PcdArmLinuxAtagMaxOffset|0x4000|UINT32|0x00000020 + # If the fixed FDT address is not available, then it should be loaded the below the kernel + # The recommandation from the Linux kernel is to have the FDT below 16KB + gArmTokenSpaceGuid.PcdArmLinuxFdtMaxOffset|0x4000|UINT32|0x00000023 # # ARM Architectural Timer diff --git a/ArmPkg/Library/BdsLib/BdsAppLoader.c b/ArmPkg/Library/BdsLib/BdsAppLoader.c index 4f359cda10..2d7f96e28b 100644 --- a/ArmPkg/Library/BdsLib/BdsAppLoader.c +++ b/ArmPkg/Library/BdsLib/BdsAppLoader.c @@ -1,6 +1,6 @@ /** @file * -* Copyright (c) 2011, ARM Limited. All rights reserved. +* Copyright (c) 2011-2012, ARM Limited. All rights reserved. * * This program and the accompanying materials * are licensed and made available under the terms and conditions of the BSD License diff --git a/ArmPkg/Library/BdsLib/BdsFilePath.c b/ArmPkg/Library/BdsLib/BdsFilePath.c index 88596c7c6a..a8b77a3d22 100644 --- a/ArmPkg/Library/BdsLib/BdsFilePath.c +++ b/ArmPkg/Library/BdsLib/BdsFilePath.c @@ -1,6 +1,6 @@ /** @file * -* Copyright (c) 2011, ARM Limited. All rights reserved. +* Copyright (c) 2011-2012, ARM Limited. All rights reserved. * * This program and the accompanying materials * are licensed and made available under the terms and conditions of the BSD License diff --git a/ArmPkg/Library/BdsLib/BdsHelper.c b/ArmPkg/Library/BdsLib/BdsHelper.c index bd1fa005e8..29cc12bd33 100644 --- a/ArmPkg/Library/BdsLib/BdsHelper.c +++ b/ArmPkg/Library/BdsLib/BdsHelper.c @@ -1,6 +1,6 @@ /** @file * -* Copyright (c) 2011, ARM Limited. All rights reserved. +* Copyright (c) 2011-2012, ARM Limited. All rights reserved. * * This program and the accompanying materials * are licensed and made available under the terms and conditions of the BSD License @@ -44,6 +44,7 @@ ShutdownUefiBootServices ( MemoryMap = NULL; MemoryMapSize = 0; Pages = 0; + do { Status = gBS->GetMemoryMap ( &MemoryMapSize, diff --git a/ArmPkg/Library/BdsLib/BdsInternal.h b/ArmPkg/Library/BdsLib/BdsInternal.h index 880d780994..85f9f41598 100644 --- a/ArmPkg/Library/BdsLib/BdsInternal.h +++ b/ArmPkg/Library/BdsLib/BdsInternal.h @@ -1,6 +1,6 @@ /** @file * -* Copyright (c) 2011, ARM Limited. All rights reserved. +* Copyright (c) 2011-2012, ARM Limited. All rights reserved. * * This program and the accompanying materials * are licensed and made available under the terms and conditions of the BSD License @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -41,7 +42,7 @@ #include #include -#include "BdsLinuxLoader.h" +#include typedef BOOLEAN (*BDS_FILE_LOADER_SUPPORT) ( IN EFI_DEVICE_PATH *DevicePath, @@ -94,13 +95,4 @@ BdsLoadImage ( OUT UINTN *FileSize ); -EFI_STATUS -PrepareAtagList ( - IN CONST CHAR8* CommandLineString, - IN EFI_PHYSICAL_ADDRESS InitrdImage, - IN UINTN InitrdImageSize, - OUT EFI_PHYSICAL_ADDRESS *AtagBase, - OUT UINT32 *AtagSize - ); - #endif diff --git a/ArmPkg/Library/BdsLib/BdsLib.inf b/ArmPkg/Library/BdsLib/BdsLib.inf index 20644f1f7c..9dee03a795 100644 --- a/ArmPkg/Library/BdsLib/BdsLib.inf +++ b/ArmPkg/Library/BdsLib/BdsLib.inf @@ -1,5 +1,6 @@ #/* @file -# Copyright (c) 2011, ARM Limited. All rights reserved. +# +# Copyright (c) 2011-2012, ARM Limited. All rights reserved. # # This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License @@ -27,6 +28,7 @@ BdsLinuxLoader.c BdsLinuxAtag.c + BdsLinuxFdt.c [Packages] MdePkg/MdePkg.dec @@ -41,9 +43,11 @@ HobLib PerformanceLib SerialPortLib + FdtLib [Guids] gEfiFileInfoGuid + gArmMpCoreInfoGuid [Protocols] gEfiBdsArchProtocolGuid @@ -64,10 +68,10 @@ gArmTokenSpaceGuid.PcdSystemMemorySize gArmTokenSpaceGuid.PcdArmMachineType + gArmTokenSpaceGuid.PcdArmLinuxFdtMaxOffset + gArmTokenSpaceGuid.PcdArmLinuxKernelFixedOffset gArmTokenSpaceGuid.PcdArmLinuxKernelMaxOffset gArmTokenSpaceGuid.PcdArmLinuxAtagMaxOffset -[Pcd] - [Depex] TRUE diff --git a/ArmPkg/Library/BdsLib/BdsLinuxAtag.c b/ArmPkg/Library/BdsLib/BdsLinuxAtag.c index 8c16bc0ec3..b59df07622 100644 --- a/ArmPkg/Library/BdsLib/BdsLinuxAtag.c +++ b/ArmPkg/Library/BdsLib/BdsLinuxAtag.c @@ -1,6 +1,6 @@ /** @file * -* Copyright (c) 2011, ARM Limited. All rights reserved. +* Copyright (c) 2011-2012, ARM Limited. All rights reserved. * * This program and the accompanying materials * are licensed and made available under the terms and conditions of the BSD License @@ -13,6 +13,7 @@ **/ #include "BdsInternal.h" +#include "BdsLinuxLoader.h" // Point to the current ATAG STATIC LINUX_ATAG *mLinuxKernelCurrentAtag; @@ -78,6 +79,22 @@ SetupCmdlineTag ( } } +STATIC +VOID +SetupInitrdTag ( + IN UINT32 InitrdImage, + IN UINT32 InitrdImageSize + ) +{ + mLinuxKernelCurrentAtag->header.size = tag_size(LINUX_ATAG_INITRD2); + mLinuxKernelCurrentAtag->header.type = ATAG_INITRD2; + + mLinuxKernelCurrentAtag->body.initrd2_tag.start = InitrdImage; + mLinuxKernelCurrentAtag->body.initrd2_tag.size = InitrdImageSize; + + // Move pointer to next tag + mLinuxKernelCurrentAtag = next_tag_address(mLinuxKernelCurrentAtag); +} STATIC VOID SetupEndTag ( @@ -114,7 +131,7 @@ PrepareAtagList ( AtagStartAddress = LINUX_ATAG_MAX_OFFSET; Status = gBS->AllocatePages (AllocateMaxAddress, EfiBootServicesData, EFI_SIZE_TO_PAGES(ATAG_MAX_SIZE), &AtagStartAddress); if (EFI_ERROR(Status)) { - DEBUG ((EFI_D_ERROR,"Failed to allocate Atag at 0x%lX (%r)\n",AtagStartAddress,Status)); + DEBUG ((EFI_D_WARN, "Warning: Failed to allocate Atag at 0x%lX (%r). The Atag will be allocated somewhere else in System Memory.\n", AtagStartAddress, Status)); Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, EFI_SIZE_TO_PAGES(ATAG_MAX_SIZE), &AtagStartAddress); ASSERT_EFI_ERROR(Status); } @@ -141,14 +158,7 @@ PrepareAtagList ( } if (InitrdImageSize > 0 && InitrdImage != 0) { - mLinuxKernelCurrentAtag->header.size = tag_size(LINUX_ATAG_INITRD2); - mLinuxKernelCurrentAtag->header.type = ATAG_INITRD2; - - mLinuxKernelCurrentAtag->body.initrd2_tag.start = (UINT32)InitrdImage; - mLinuxKernelCurrentAtag->body.initrd2_tag.size = (UINT32)InitrdImageSize; - - // Move pointer to next tag - mLinuxKernelCurrentAtag = next_tag_address(mLinuxKernelCurrentAtag); + SetupInitrdTag ((UINT32)InitrdImage, (UINT32)InitrdImageSize); } // End of tags diff --git a/ArmPkg/Library/BdsLib/BdsLinuxFdt.c b/ArmPkg/Library/BdsLib/BdsLinuxFdt.c new file mode 100644 index 0000000000..fa06287b19 --- /dev/null +++ b/ArmPkg/Library/BdsLib/BdsLinuxFdt.c @@ -0,0 +1,368 @@ +/** @file +* +* Copyright (c) 2011-2012, ARM Limited. All rights reserved. +* +* This program and the accompanying materials +* are licensed and made available under the terms and conditions of the BSD License +* which accompanies this distribution. The full text of the license may be found at +* http://opensource.org/licenses/bsd-license.php +* +* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +* +**/ + +#include +#include + +#include "BdsInternal.h" +#include "BdsLinuxLoader.h" + +#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1)) +#define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a)))) +#define GET_CELL(p) (p += 4, *((const UINT32 *)(p-4))) + +STATIC +UINTN +IsPrintableString ( + IN CONST VOID* data, + IN UINTN len + ) +{ + CONST CHAR8 *s = data; + CONST CHAR8 *ss; + + // Zero length is not + if (len == 0) { + return 0; + } + + // Must terminate with zero + if (s[len - 1] != '\0') { + return 0; + } + + ss = s; + while (*s/* && isprint(*s)*/) { + s++; + } + + // Not zero, or not done yet + if (*s != '\0' || (s + 1 - ss) < len) { + return 0; + } + + return 1; +} + +STATIC +VOID +PrintData ( + IN CONST CHAR8* data, + IN UINTN len + ) +{ + UINTN i; + CONST CHAR8 *p = data; + + // No data, don't print + if (len == 0) + return; + + if (IsPrintableString (data, len)) { + Print(L" = \"%a\"", (const char *)data); + } else if ((len % 4) == 0) { + Print(L" = <"); + for (i = 0; i < len; i += 4) { + Print(L"0x%08x%a", fdt32_to_cpu(GET_CELL(p)),i < (len - 4) ? " " : ""); + } + Print(L">"); + } else { + Print(L" = ["); + for (i = 0; i < len; i++) + Print(L"%02x%a", *p++, i < len - 1 ? " " : ""); + Print(L"]"); + } +} + +VOID +DebugDumpFdt ( + IN VOID* FdtBlob + ) +{ + struct fdt_header *bph; + UINT32 off_dt; + UINT32 off_str; + CONST CHAR8* p_struct; + CONST CHAR8* p_strings; + CONST CHAR8* p; + CONST CHAR8* s; + CONST CHAR8* t; + UINT32 tag; + UINTN sz; + UINTN depth; + UINTN shift; + UINT32 version; + + depth = 0; + shift = 4; + + bph = FdtBlob; + off_dt = fdt32_to_cpu(bph->off_dt_struct); + off_str = fdt32_to_cpu(bph->off_dt_strings); + p_struct = (CONST CHAR8*)FdtBlob + off_dt; + p_strings = (CONST CHAR8*)FdtBlob + off_str; + version = fdt32_to_cpu(bph->version); + + p = p_struct; + while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) { + if (tag == FDT_BEGIN_NODE) { + s = p; + p = PALIGN(p + AsciiStrLen (s) + 1, 4); + + if (*s == '\0') + s = "/"; + + Print(L"%*s%a {\n", depth * shift, L" ", s); + + depth++; + continue; + } + + if (tag == FDT_END_NODE) { + depth--; + + Print(L"%*s};\n", depth * shift, L" "); + continue; + } + + if (tag == FDT_NOP) { + Print(L"%*s// [NOP]\n", depth * shift, L" "); + continue; + } + + if (tag != FDT_PROP) { + Print(L"%*s ** Unknown tag 0x%08x\n", depth * shift, L" ", tag); + break; + } + sz = fdt32_to_cpu(GET_CELL(p)); + s = p_strings + fdt32_to_cpu(GET_CELL(p)); + if (version < 16 && sz >= 8) + p = PALIGN(p, 8); + t = p; + + p = PALIGN(p + sz, 4); + + Print(L"%*s%a", depth * shift, L" ", s); + PrintData(t, sz); + Print(L";\n"); + } +} + +typedef struct { + UINTN Base; + UINTN Size; +} FdtRegion; + +EFI_STATUS +PrepareFdt ( + IN CONST CHAR8* CommandLineArguments, + IN EFI_PHYSICAL_ADDRESS InitrdImage, + IN UINTN InitrdImageSize, + IN OUT EFI_PHYSICAL_ADDRESS *FdtBlobBase, + IN OUT UINT32 *FdtBlobSize + ) +{ + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS NewFdtBlobBase; + UINTN NewFdtBlobSize; + VOID* fdt; + INTN err; + INTN node; + INTN cpu_node; + INTN lenp; + CONST VOID* BootArg; + EFI_PHYSICAL_ADDRESS InitrdImageStart; + EFI_PHYSICAL_ADDRESS InitrdImageEnd; + FdtRegion Region; + UINTN Index; + CHAR8 Name[10]; + LIST_ENTRY ResourceList; + BDS_SYSTEM_MEMORY_RESOURCE *Resource; + ARM_PROCESSOR_TABLE *ArmProcessorTable; + ARM_CORE_INFO *ArmCoreInfoTable; + UINT32 MpId; + UINT32 ClusterId; + UINT32 CoreId; + UINT64 CpuReleaseAddr; + + err = fdt_check_header ((VOID*)(UINTN)(*FdtBlobBase)); + if (err != 0) { + Print (L"ERROR: Device Tree header not valid (err:%d)\n", err); + return EFI_INVALID_PARAMETER; + } + + // + // Allocate memory for the new FDT + // + NewFdtBlobSize = fdt_totalsize((VOID*)(UINTN)(*FdtBlobBase)) + FDT_ADDITIONAL_ENTRIES_SIZE; + + // Try below a watermark address + Status = EFI_NOT_FOUND; + if (PcdGet32(PcdArmLinuxFdtMaxOffset) != 0) { + NewFdtBlobBase = LINUX_FDT_MAX_OFFSET; + Status = gBS->AllocatePages (AllocateMaxAddress, EfiBootServicesData, EFI_SIZE_TO_PAGES(NewFdtBlobSize), &NewFdtBlobBase); + if (EFI_ERROR(Status)) { + DEBUG ((EFI_D_WARN, "Warning: Failed to load FDT below address 0x%lX (%r). Will try again at a random address anywhere.\n", NewFdtBlobBase, Status)); + } + } + + // Try anywhere there is available space + if (EFI_ERROR(Status)) { + Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, EFI_SIZE_TO_PAGES(NewFdtBlobSize), &NewFdtBlobBase); + if (EFI_ERROR(Status)) { + ASSERT_EFI_ERROR(Status); + goto FAIL_NEW_FDT; + } else { + DEBUG ((EFI_D_WARN, "WARNING: Loaded FDT at random address 0x%lX.\nWARNING: There is a risk of accidental overwriting by other code/data.\n", NewFdtBlobBase)); + } + } + + // Load the Original FDT tree into the new region + fdt = (VOID*)(UINTN)NewFdtBlobBase; + err = fdt_open_into((VOID*)(UINTN)(*FdtBlobBase), fdt, NewFdtBlobSize); + if (err) { + DEBUG((EFI_D_ERROR, "fdt_open_into(): %a\n", fdt_strerror(err))); + Status = EFI_INVALID_PARAMETER; + goto FAIL_NEW_FDT; + } + + DEBUG_CODE_BEGIN(); + //DebugDumpFdt (fdt); + DEBUG_CODE_END(); + + node = fdt_subnode_offset(fdt, 0, "chosen"); + if (node < 0) { + // The 'chosen' node does not exist, create it + node = fdt_add_subnode(fdt, 0, "chosen"); + if (node < 0) { + DEBUG((EFI_D_ERROR,"Error on finding 'chosen' node\n")); + Status = EFI_INVALID_PARAMETER; + goto FAIL_NEW_FDT; + } + } + + DEBUG_CODE_BEGIN(); + BootArg = fdt_getprop(fdt, node, "bootargs", &lenp); + if (BootArg != NULL) { + DEBUG((EFI_D_ERROR,"BootArg: %a\n",BootArg)); + } + DEBUG_CODE_END(); + + // Set Linux CmdLine + if ((CommandLineArguments != NULL) && (AsciiStrLen (CommandLineArguments) > 0)) { + err = fdt_setprop(fdt, node, "bootargs", CommandLineArguments, AsciiStrSize(CommandLineArguments)); + if (err) { + DEBUG((EFI_D_ERROR,"Fail to set new 'bootarg' (err:%d)\n",err)); + } + } + + // Set Linux Initrd + if (InitrdImageSize != 0) { + InitrdImageStart = cpu_to_fdt64 (InitrdImage); + err = fdt_setprop(fdt, node, "linux,initrd-start", &InitrdImageStart, sizeof(EFI_PHYSICAL_ADDRESS)); + if (err) { + DEBUG((EFI_D_ERROR,"Fail to set new 'linux,initrd-start' (err:%d)\n",err)); + } + InitrdImageEnd = cpu_to_fdt64 (InitrdImage + InitrdImageSize); + err = fdt_setprop(fdt, node, "linux,initrd-end", &InitrdImageEnd, sizeof(EFI_PHYSICAL_ADDRESS)); + if (err) { + DEBUG((EFI_D_ERROR,"Fail to set new 'linux,initrd-start' (err:%d)\n",err)); + } + } + + // Set Physical memory setup if does not exist + node = fdt_subnode_offset(fdt, 0, "memory"); + if (node < 0) { + // The 'memory' node does not exist, create it + node = fdt_add_subnode(fdt, 0, "memory"); + if (node >= 0) { + fdt_setprop_string(fdt, node, "name", "memory"); + fdt_setprop_string(fdt, node, "device_type", "memory"); + + GetSystemMemoryResources (&ResourceList); + Resource = (BDS_SYSTEM_MEMORY_RESOURCE*)ResourceList.ForwardLink; + + if (sizeof(UINTN) == sizeof(UINT32)) { + Region.Base = cpu_to_fdt32((UINTN)Resource->PhysicalStart); + Region.Size = cpu_to_fdt32((UINTN)Resource->ResourceLength); + } else { + Region.Base = cpu_to_fdt64((UINTN)Resource->PhysicalStart); + Region.Size = cpu_to_fdt64((UINTN)Resource->ResourceLength); + } + + err = fdt_setprop(fdt, node, "reg", &Region, sizeof(Region)); + if (err) { + DEBUG((EFI_D_ERROR,"Fail to set new 'memory region' (err:%d)\n",err)); + } + } + } + + // Setup Arm Mpcore Info if it is a multi-core or multi-cluster platforms + for (Index=0; Index < gST->NumberOfTableEntries; Index++) { + // Check for correct GUID type + if (CompareGuid (&gArmMpCoreInfoGuid, &(gST->ConfigurationTable[Index].VendorGuid))) { + MpId = ArmReadMpidr (); + ClusterId = GET_CLUSTER_ID(MpId); + CoreId = GET_CORE_ID(MpId); + + node = fdt_subnode_offset(fdt, 0, "cpus"); + if (node < 0) { + // Create the /cpus node + node = fdt_add_subnode(fdt, 0, "cpus"); + fdt_setprop_string(fdt, node, "name", "cpus"); + fdt_setprop_cell(fdt, node, "#address-cells", 1); + fdt_setprop_cell(fdt, node, "#size-cells", 0); + } + + // Get pointer to ARM processor table + ArmProcessorTable = (ARM_PROCESSOR_TABLE *)gST->ConfigurationTable[Index].VendorTable; + ArmCoreInfoTable = ArmProcessorTable->ArmCpus; + + for (Index = 0; Index < ArmProcessorTable->NumberOfEntries; Index++) { + AsciiSPrint (Name, 10, "cpu@%d", Index); + cpu_node = fdt_subnode_offset(fdt, node, Name); + if (cpu_node < 0) { + cpu_node = fdt_add_subnode(fdt, node, Name); + fdt_setprop_string(fdt, cpu_node, "device-type", "cpu"); + fdt_setprop(fdt, cpu_node, "reg", &Index, sizeof(Index)); + } + + fdt_setprop_string(fdt, cpu_node, "enable-method", "spin-table"); + CpuReleaseAddr = cpu_to_fdt64(ArmCoreInfoTable[Index].MailboxSetAddress); + fdt_setprop(fdt, cpu_node, "cpu-release-addr", &CpuReleaseAddr, sizeof(CpuReleaseAddr)); + + // If it is not the primary core than the cpu should be disabled + if (((ArmCoreInfoTable[Index].ClusterId != ClusterId) || (ArmCoreInfoTable[Index].CoreId != CoreId))) { + fdt_setprop_string(fdt, cpu_node, "status", "disabled"); + } + } + break; + } + } + + DEBUG_CODE_BEGIN(); + //DebugDumpFdt (fdt); + DEBUG_CODE_END(); + + *FdtBlobBase = NewFdtBlobBase; + *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(NewFdtBlobBase)); + return EFI_SUCCESS; + +FAIL_NEW_FDT: + *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(*FdtBlobBase)); + // Return success even if we failed to update the FDT blob. The original one is still valid. + return EFI_SUCCESS; +} + + diff --git a/ArmPkg/Library/BdsLib/BdsLinuxLoader.c b/ArmPkg/Library/BdsLib/BdsLinuxLoader.c index a3ba58023a..901781f2f8 100644 --- a/ArmPkg/Library/BdsLib/BdsLinuxLoader.c +++ b/ArmPkg/Library/BdsLib/BdsLinuxLoader.c @@ -64,9 +64,15 @@ StartLinux ( // This is necessary because the ARM Linux kernel requires // the FTD / ATAG List to reside entirely inside the first 1MB of // physical memory. - if ((UINTN)KernelParamsAddress > LINUX_ATAG_MAX_OFFSET) { - //Note: There is no requirement on the alignment - KernelParamsAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)CopyMem (ALIGN32_BELOW(LINUX_ATAG_MAX_OFFSET - KernelParamsSize), (VOID*)(UINTN)KernelParamsAddress, KernelParamsSize); + //Note: There is no requirement on the alignment + if (MachineType != ARM_FDT_MACHINE_TYPE) { + if (((UINTN)KernelParamsAddress > LINUX_ATAG_MAX_OFFSET) && (KernelParamsSize < PcdGet32(PcdArmLinuxAtagMaxOffset))) { + KernelParamsAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)CopyMem (ALIGN32_BELOW(LINUX_ATAG_MAX_OFFSET - KernelParamsSize), (VOID*)(UINTN)KernelParamsAddress, KernelParamsSize); + } + } else { + if (((UINTN)KernelParamsAddress > LINUX_FDT_MAX_OFFSET) && (KernelParamsSize < PcdGet32(PcdArmLinuxFdtMaxOffset))) { + KernelParamsAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)CopyMem (ALIGN32_BELOW(LINUX_FDT_MAX_OFFSET - KernelParamsSize), (VOID*)(UINTN)KernelParamsAddress, KernelParamsSize); + } } if ((UINTN)LinuxImage > LINUX_KERNEL_MAX_OFFSET) { @@ -135,14 +141,14 @@ EFI_STATUS BdsBootLinuxAtag ( IN EFI_DEVICE_PATH_PROTOCOL* LinuxKernelDevicePath, IN EFI_DEVICE_PATH_PROTOCOL* InitrdDevicePath, - IN CONST CHAR8* Arguments + IN CONST CHAR8* CommandLineArguments ) { EFI_STATUS Status; UINT32 LinuxImageSize; UINT32 InitrdImageSize = 0; - UINT32 KernelParamsSize; - EFI_PHYSICAL_ADDRESS KernelParamsAddress; + UINT32 AtagSize; + EFI_PHYSICAL_ADDRESS AtagBase; EFI_PHYSICAL_ADDRESS LinuxImage; EFI_PHYSICAL_ADDRESS InitrdImage; @@ -181,13 +187,13 @@ BdsBootLinuxAtag ( // // By setting address=0 we leave the memory allocation to the function - Status = PrepareAtagList (Arguments, InitrdImage, InitrdImageSize, &KernelParamsAddress, &KernelParamsSize); + Status = PrepareAtagList (CommandLineArguments, InitrdImage, InitrdImageSize, &AtagBase, &AtagSize); if (EFI_ERROR(Status)) { Print(L"ERROR: Can not prepare ATAG list. Status=0x%X\n", Status); return Status; } - return StartLinux (LinuxImage, LinuxImageSize, KernelParamsAddress, KernelParamsSize, PcdGet32(PcdArmMachineType)); + return StartLinux (LinuxImage, LinuxImageSize, AtagBase, AtagSize, PcdGet32(PcdArmMachineType)); } /** @@ -206,21 +212,18 @@ EFI_STATUS BdsBootLinuxFdt ( IN EFI_DEVICE_PATH_PROTOCOL* LinuxKernelDevicePath, IN EFI_DEVICE_PATH_PROTOCOL* InitrdDevicePath, - IN CONST CHAR8* Arguments, + IN CONST CHAR8* CommandLineArguments, IN EFI_DEVICE_PATH_PROTOCOL* FdtDevicePath ) { EFI_STATUS Status; UINT32 LinuxImageSize; UINT32 InitrdImageSize = 0; - UINT32 KernelParamsSize; - EFI_PHYSICAL_ADDRESS KernelParamsAddress; - UINT32 FdtMachineType; + UINT32 FdtBlobSize; + EFI_PHYSICAL_ADDRESS FdtBlobBase; EFI_PHYSICAL_ADDRESS LinuxImage; EFI_PHYSICAL_ADDRESS InitrdImage; - FdtMachineType = 0xFFFFFFFF; - PERF_START (NULL, "BDS", NULL, 0); // Load the Linux kernel from a device path @@ -250,13 +253,22 @@ BdsBootLinuxFdt ( } } - // Load the FDT binary from a device path - KernelParamsAddress = LINUX_ATAG_MAX_OFFSET; - Status = BdsLoadImage (FdtDevicePath, AllocateMaxAddress, &KernelParamsAddress, &KernelParamsSize); + // Load the FDT binary from a device path. The FDT will be reloaded later to a more appropriate location for the Linux kernel. + FdtBlobBase = 0; + Status = BdsLoadImage (FdtDevicePath, AllocateAnyPages, &FdtBlobBase, &FdtBlobSize); if (EFI_ERROR(Status)) { Print (L"ERROR: Did not find Device Tree blob.\n"); return Status; } - return StartLinux (LinuxImage, LinuxImageSize, KernelParamsAddress, KernelParamsSize, FdtMachineType); + + // Update the Fdt with the Initrd information. The FDT will increase in size. + // By setting address=0 we leave the memory allocation to the function + Status = PrepareFdt (CommandLineArguments, InitrdImage, InitrdImageSize, &FdtBlobBase, &FdtBlobSize); + if (EFI_ERROR(Status)) { + Print(L"ERROR: Can not load kernel with FDT. Status=%r\n", Status); + return Status; + } + + return StartLinux (LinuxImage, LinuxImageSize, FdtBlobBase, FdtBlobSize, ARM_FDT_MACHINE_TYPE); } diff --git a/ArmPkg/Library/BdsLib/BdsLinuxLoader.h b/ArmPkg/Library/BdsLib/BdsLinuxLoader.h index 8d58ce103a..a9b7037d19 100644 --- a/ArmPkg/Library/BdsLib/BdsLinuxLoader.h +++ b/ArmPkg/Library/BdsLib/BdsLinuxLoader.h @@ -16,11 +16,24 @@ #define __BDSLINUXLOADER_H #define LINUX_UIMAGE_SIGNATURE 0x56190527 - -#define LINUX_ATAG_MAX_OFFSET (PcdGet32(PcdSystemMemoryBase) + PcdGet32(PcdArmLinuxAtagMaxOffset)) #define LINUX_KERNEL_MAX_OFFSET (PcdGet32(PcdSystemMemoryBase) + PcdGet32(PcdArmLinuxKernelMaxOffset)) +#define LINUX_ATAG_MAX_OFFSET (PcdGet32(PcdSystemMemoryBase) + PcdGet32(PcdArmLinuxAtagMaxOffset)) +#define LINUX_FDT_MAX_OFFSET (PcdGet32(PcdSystemMemoryBase) + PcdGet32(PcdArmLinuxFdtMaxOffset)) -#define ATAG_MAX_SIZE 0x3000 +// Additional size that could be used for FDT entries added by the UEFI OS Loader +// Estimation based on: EDID (300bytes) + bootargs (200bytes) + initrd region (20bytes) +// + system memory region (20bytes) + mp_core entries (200 bytes) +#define FDT_ADDITIONAL_ENTRIES_SIZE 0x300 + +#define ARM_FDT_MACHINE_TYPE 0xFFFFFFFF + +typedef VOID (*LINUX_KERNEL)(UINT32 Zero, UINT32 Arch, UINTN ParametersBase); + +// +// ATAG Definitions +// + +#define ATAG_MAX_SIZE 0x3000 /* ATAG : list of possible tags */ #define ATAG_NONE 0x00000000 @@ -35,7 +48,9 @@ #define ATAG_CMDLINE 0x54410009 #define ATAG_ARM_MP_CORE 0x5441000A -/* structures for each atag */ +#define next_tag_address(t) ((LINUX_ATAG*)((UINT32)(t) + (((t)->header.size) << 2) )) +#define tag_size(type) ((UINT32)((sizeof(LINUX_ATAG_HEADER) + sizeof(type)) >> 2)) + typedef struct { UINT32 size; /* length of tag in words including this header */ UINT32 type; /* tag type */ @@ -120,9 +135,22 @@ typedef struct { } body; } LINUX_ATAG; -typedef VOID (*LINUX_KERNEL)(UINT32 Zero, UINT32 Arch, UINTN ParametersBase); - -#define next_tag_address(t) ((LINUX_ATAG*)((UINT32)(t) + (((t)->header.size) << 2) )) -#define tag_size(type) ((UINT32)((sizeof(LINUX_ATAG_HEADER) + sizeof(type)) >> 2)) +EFI_STATUS +PrepareAtagList ( + IN CONST CHAR8* CommandLineString, + IN EFI_PHYSICAL_ADDRESS InitrdImage, + IN UINTN InitrdImageSize, + OUT EFI_PHYSICAL_ADDRESS *AtagBase, + OUT UINT32 *AtagSize + ); + +EFI_STATUS +PrepareFdt ( + IN CONST CHAR8* CommandLineArguments, + IN EFI_PHYSICAL_ADDRESS InitrdImage, + IN UINTN InitrdImageSize, + IN OUT EFI_PHYSICAL_ADDRESS *FdtBlobBase, + IN OUT UINT32 *FdtBlobSize + ); #endif diff --git a/ArmPlatformPkg/ArmRealViewEbPkg/ArmRealViewEb.dsc.inc b/ArmPlatformPkg/ArmRealViewEbPkg/ArmRealViewEb.dsc.inc index 27413fe5a3..385d296dbb 100644 --- a/ArmPlatformPkg/ArmRealViewEbPkg/ArmRealViewEb.dsc.inc +++ b/ArmPlatformPkg/ArmRealViewEbPkg/ArmRealViewEb.dsc.inc @@ -99,6 +99,7 @@ # BDS Libraries BdsLib|ArmPkg/Library/BdsLib/BdsLib.inf + FdtLib|EmbeddedPkg/Library/FdtLib/FdtLib.inf [LibraryClasses.common.SEC] ArmPlatformSecExtraActionLib|ArmPlatformPkg/Library/DebugSecExtraActionLib/DebugSecExtraActionLib.inf diff --git a/ArmPlatformPkg/ArmVExpressPkg/ArmVExpress.dsc.inc b/ArmPlatformPkg/ArmVExpressPkg/ArmVExpress.dsc.inc index fdff40da63..1af5490b91 100644 --- a/ArmPlatformPkg/ArmVExpressPkg/ArmVExpress.dsc.inc +++ b/ArmPlatformPkg/ArmVExpressPkg/ArmVExpress.dsc.inc @@ -101,6 +101,7 @@ # BDS Libraries BdsLib|ArmPkg/Library/BdsLib/BdsLib.inf + FdtLib|EmbeddedPkg/Library/FdtLib/FdtLib.inf [LibraryClasses.common.SEC] ArmPlatformSecExtraActionLib|ArmPlatformPkg/Library/DebugSecExtraActionLib/DebugSecExtraActionLib.inf diff --git a/BeagleBoardPkg/BeagleBoardPkg.dsc b/BeagleBoardPkg/BeagleBoardPkg.dsc index db6ba895ca..4e00c03452 100644 --- a/BeagleBoardPkg/BeagleBoardPkg.dsc +++ b/BeagleBoardPkg/BeagleBoardPkg.dsc @@ -120,6 +120,7 @@ DmaLib|ArmPkg/Library/ArmDmaLib/ArmDmaLib.inf BdsLib|ArmPkg/Library/BdsLib/BdsLib.inf + FdtLib|EmbeddedPkg/Library/FdtLib/FdtLib.inf [LibraryClasses.common.SEC] ArmLib|ArmPkg/Library/ArmLib/ArmV7/ArmV7LibPrePi.inf -- 2.39.2