2011-02-02 23:35:30 +01:00
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011, 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 "BdsInternal.h"
|
|
|
|
#include "BdsLinuxLoader.h"
|
|
|
|
|
|
|
|
#include <Library/PcdLib.h>
|
|
|
|
#include <Library/ArmLib.h>
|
|
|
|
#include <Library/HobLib.h>
|
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
#define ALIGN32_BELOW(addr) ALIGN_POINTER(addr - 32,32)
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
#define LINUX_ATAG_MAX_OFFSET (PcdGet32(PcdSystemMemoryBase) + PcdGet32(PcdArmLinuxAtagMaxOffset))
|
|
|
|
#define LINUX_KERNEL_MAX_OFFSET (PcdGet32(PcdSystemMemoryBase) + PcdGet32(PcdArmLinuxKernelMaxOffset))
|
|
|
|
|
|
|
|
// Point to the current ATAG
|
|
|
|
STATIC LINUX_ATAG *mLinuxKernelCurrentAtag;
|
2011-02-02 23:35:30 +01:00
|
|
|
|
|
|
|
STATIC
|
|
|
|
VOID
|
2011-06-11 13:56:30 +02:00
|
|
|
SetupCoreTag (
|
|
|
|
IN UINT32 PageSize
|
|
|
|
)
|
2011-02-02 23:35:30 +01:00
|
|
|
{
|
2011-06-11 13:56:30 +02:00
|
|
|
mLinuxKernelCurrentAtag->header.size = tag_size(LINUX_ATAG_CORE);
|
|
|
|
mLinuxKernelCurrentAtag->header.type = ATAG_CORE;
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
mLinuxKernelCurrentAtag->body.core_tag.flags = 1; /* ensure read-only */
|
|
|
|
mLinuxKernelCurrentAtag->body.core_tag.pagesize = PageSize; /* systems PageSize (4k) */
|
|
|
|
mLinuxKernelCurrentAtag->body.core_tag.rootdev = 0; /* zero root device (typically overridden from kernel command line )*/
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
// move pointer to next tag
|
|
|
|
mLinuxKernelCurrentAtag = next_tag_address(mLinuxKernelCurrentAtag);
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC
|
|
|
|
VOID
|
2011-06-11 13:56:30 +02:00
|
|
|
SetupMemTag (
|
|
|
|
IN UINTN StartAddress,
|
|
|
|
IN UINT32 Size
|
|
|
|
)
|
2011-02-02 23:35:30 +01:00
|
|
|
{
|
2011-06-11 13:56:30 +02:00
|
|
|
mLinuxKernelCurrentAtag->header.size = tag_size(LINUX_ATAG_MEM);
|
|
|
|
mLinuxKernelCurrentAtag->header.type = ATAG_MEM;
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
mLinuxKernelCurrentAtag->body.mem_tag.start = StartAddress; /* Start of memory chunk for AtagMem */
|
|
|
|
mLinuxKernelCurrentAtag->body.mem_tag.size = Size; /* Size of memory chunk for AtagMem */
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
// move pointer to next tag
|
|
|
|
mLinuxKernelCurrentAtag = next_tag_address(mLinuxKernelCurrentAtag);
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC
|
|
|
|
VOID
|
2011-06-11 13:56:30 +02:00
|
|
|
SetupCmdlineTag (
|
|
|
|
IN CONST CHAR8 *CmdLine
|
|
|
|
)
|
2011-02-02 23:35:30 +01:00
|
|
|
{
|
|
|
|
UINT32 LineLength;
|
|
|
|
|
|
|
|
// Increment the line length by 1 to account for the null string terminator character
|
|
|
|
LineLength = AsciiStrLen(CmdLine) + 1;
|
|
|
|
|
|
|
|
/* Check for NULL strings.
|
|
|
|
* Do not insert a tag for an empty CommandLine, don't even modify the tag address pointer.
|
|
|
|
* Remember, you have at least one null string terminator character.
|
|
|
|
*/
|
2011-06-11 13:56:30 +02:00
|
|
|
if(LineLength > 1) {
|
|
|
|
mLinuxKernelCurrentAtag->header.size = ((UINT32)sizeof(LINUX_ATAG_HEADER) + LineLength + (UINT32)3) >> 2;
|
|
|
|
mLinuxKernelCurrentAtag->header.type = ATAG_CMDLINE;
|
2011-02-02 23:35:30 +01:00
|
|
|
|
|
|
|
/* place CommandLine into tag */
|
2011-06-11 13:56:30 +02:00
|
|
|
AsciiStrCpy(mLinuxKernelCurrentAtag->body.cmdline_tag.cmdline, CmdLine);
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
// move pointer to next tag
|
|
|
|
mLinuxKernelCurrentAtag = next_tag_address(mLinuxKernelCurrentAtag);
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC
|
|
|
|
VOID
|
2011-06-11 13:56:30 +02:00
|
|
|
SetupEndTag (
|
|
|
|
VOID
|
|
|
|
)
|
2011-02-02 23:35:30 +01:00
|
|
|
{
|
|
|
|
// Empty tag ends list; this has zero length and no body
|
2011-06-11 13:56:30 +02:00
|
|
|
mLinuxKernelCurrentAtag->header.type = ATAG_NONE;
|
|
|
|
mLinuxKernelCurrentAtag->header.size = 0;
|
2011-02-02 23:35:30 +01:00
|
|
|
|
|
|
|
/* We can not calculate the next address by using the standard macro:
|
|
|
|
* Params = next_tag_address(Params);
|
|
|
|
* because it relies on the header.size, which here it is 0 (zero).
|
2011-06-11 13:56:30 +02:00
|
|
|
* The easiest way is to add the sizeof(mLinuxKernelCurrentAtag->header).
|
2011-02-02 23:35:30 +01:00
|
|
|
*/
|
2011-06-11 13:56:30 +02:00
|
|
|
mLinuxKernelCurrentAtag = (LINUX_ATAG*)((UINT32)mLinuxKernelCurrentAtag + sizeof(mLinuxKernelCurrentAtag->header));
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC
|
|
|
|
EFI_STATUS
|
2011-06-11 13:56:30 +02:00
|
|
|
PrepareAtagList (
|
|
|
|
IN CONST CHAR8* CommandLineString,
|
|
|
|
OUT LINUX_ATAG **AtagBase,
|
|
|
|
OUT UINT32 *AtagSize
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
LIST_ENTRY *ResourceLink;
|
|
|
|
LIST_ENTRY ResourceList;
|
|
|
|
EFI_PHYSICAL_ADDRESS AtagStartAddress;
|
2011-02-02 23:35:30 +01:00
|
|
|
BDS_SYSTEM_MEMORY_RESOURCE *Resource;
|
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
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));
|
|
|
|
Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, EFI_SIZE_TO_PAGES(ATAG_MAX_SIZE), &AtagStartAddress);
|
|
|
|
ASSERT_EFI_ERROR(Status);
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ready to setup the atag list
|
2011-06-11 13:56:30 +02:00
|
|
|
mLinuxKernelCurrentAtag = (LINUX_ATAG*)(UINTN)AtagStartAddress;
|
2011-02-02 23:35:30 +01:00
|
|
|
|
|
|
|
// Standard core tag 4k PageSize
|
|
|
|
SetupCoreTag( (UINT32)SIZE_4KB );
|
|
|
|
|
|
|
|
// Physical memory setup
|
2011-06-11 13:56:30 +02:00
|
|
|
GetSystemMemoryResources (&ResourceList);
|
2011-02-02 23:35:30 +01:00
|
|
|
ResourceLink = ResourceList.ForwardLink;
|
|
|
|
while (ResourceLink != NULL && ResourceLink != &ResourceList) {
|
2011-06-11 13:56:30 +02:00
|
|
|
Resource = (BDS_SYSTEM_MEMORY_RESOURCE*)ResourceLink;
|
|
|
|
DEBUG((EFI_D_INFO,"- [0x%08X,0x%08X]\n",(UINT32)Resource->PhysicalStart,(UINT32)Resource->PhysicalStart+(UINT32)Resource->ResourceLength));
|
2011-02-02 23:35:30 +01:00
|
|
|
SetupMemTag( (UINT32)Resource->PhysicalStart, (UINT32)Resource->ResourceLength );
|
|
|
|
ResourceLink = ResourceLink->ForwardLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommandLine setting root device
|
2011-06-11 13:56:30 +02:00
|
|
|
SetupCmdlineTag (CommandLineString);
|
2011-02-02 23:35:30 +01:00
|
|
|
|
|
|
|
// end of tags
|
|
|
|
SetupEndTag();
|
|
|
|
|
|
|
|
// Calculate atag list size
|
2011-06-11 13:56:30 +02:00
|
|
|
*AtagBase = (LINUX_ATAG*)(UINTN)AtagStartAddress;
|
|
|
|
*AtagSize = (UINT32)mLinuxKernelCurrentAtag - (UINT32)AtagStartAddress + 1;
|
2011-02-02 23:35:30 +01:00
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC
|
|
|
|
EFI_STATUS
|
2011-06-11 13:56:30 +02:00
|
|
|
PreparePlatformHardware (
|
|
|
|
VOID
|
|
|
|
)
|
2011-02-02 23:35:30 +01:00
|
|
|
{
|
|
|
|
//Note: Interrupts will be disabled by the GIC driver when ExitBootServices() will be called.
|
|
|
|
|
|
|
|
// clean, invalidate, disable data cache
|
|
|
|
ArmCleanInvalidateDataCache();
|
|
|
|
ArmDisableDataCache();
|
|
|
|
|
|
|
|
// Invalidate and disable the Instruction cache
|
|
|
|
ArmInvalidateInstructionCache ();
|
|
|
|
ArmDisableInstructionCache ();
|
|
|
|
|
|
|
|
// turn off MMU
|
|
|
|
ArmDisableMmu();
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
/**
|
|
|
|
Start a Linux kernel from a Device Path
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
@param LinuxKernel Device Path to the Linux Kernel
|
|
|
|
@param Parameters Linux kernel agruments
|
|
|
|
@param Fdt Device Path to the Flat Device Tree
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
@retval EFI_SUCCESS All drivers have been connected
|
|
|
|
@retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
|
|
|
|
@retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
BdsBootLinux (
|
|
|
|
IN EFI_DEVICE_PATH_PROTOCOL* LinuxKernelDevicePath,
|
|
|
|
IN CONST CHAR8* Arguments,
|
|
|
|
IN EFI_DEVICE_PATH_PROTOCOL* FdtDevicePath
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 LinuxImageSize;
|
|
|
|
UINT32 KernelParamsSize;
|
|
|
|
VOID* KernelParamsAddress = NULL;
|
|
|
|
UINT32 MachineType;
|
|
|
|
BOOLEAN FdtSupported = FALSE;
|
|
|
|
LINUX_KERNEL LinuxKernel;
|
|
|
|
EFI_PHYSICAL_ADDRESS LinuxImage;;
|
|
|
|
|
|
|
|
|
|
|
|
PERF_START (NULL, "BDS", NULL, 0);
|
|
|
|
|
|
|
|
// Load the Linux kernel from a device path
|
|
|
|
LinuxImage = LINUX_KERNEL_MAX_OFFSET;
|
|
|
|
Status = BdsLoadImage (LinuxKernelDevicePath, AllocateMaxAddress, &LinuxImage, &LinuxImageSize);
|
|
|
|
if (EFI_ERROR(Status)) {
|
|
|
|
DEBUG ((EFI_D_ERROR, "ERROR: Do not find Linux kernel.\n"));
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
LinuxKernel = (LINUX_KERNEL)(UINTN)LinuxImage;
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
// Load the FDT binary from a device path
|
|
|
|
KernelParamsAddress = (VOID*)LINUX_ATAG_MAX_OFFSET;
|
|
|
|
Status = BdsLoadImage (FdtDevicePath, AllocateMaxAddress, (EFI_PHYSICAL_ADDRESS*)&KernelParamsAddress, &KernelParamsSize);
|
|
|
|
if (!EFI_ERROR(Status)) {
|
|
|
|
FdtSupported = TRUE;
|
|
|
|
}
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
//
|
|
|
|
// Setup the Linux Kernel Parameters
|
|
|
|
//
|
|
|
|
if (!FdtSupported) {
|
|
|
|
// Non-FDT requires a specific machine type.
|
|
|
|
// This OS Boot loader supports just one machine type,
|
|
|
|
// but that could change in the future.
|
|
|
|
MachineType = PcdGet32(PcdArmMachineType);
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
// By setting address=0 we leave the memory allocation to the function
|
|
|
|
Status = PrepareAtagList (Arguments, (LINUX_ATAG**)&KernelParamsAddress, &KernelParamsSize);
|
|
|
|
if(EFI_ERROR(Status)) {
|
|
|
|
Print(L"ERROR: Can not prepare ATAG list. Status=0x%X\n", Status);
|
|
|
|
goto Exit;
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
2011-06-11 13:56:30 +02:00
|
|
|
} else {
|
|
|
|
MachineType = 0xFFFFFFFF;
|
|
|
|
}
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
// Shut down UEFI boot services. ExitBootServices() will notify every driver that created an event on
|
|
|
|
// ExitBootServices event. Example the Interrupt DXE driver will disable the interrupts on this event.
|
|
|
|
Status = ShutdownUefiBootServices ();
|
|
|
|
if(EFI_ERROR(Status)) {
|
|
|
|
DEBUG((EFI_D_ERROR,"ERROR: Can not shutdown UEFI boot services. Status=0x%X\n", Status));
|
|
|
|
goto Exit;
|
|
|
|
}
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
// Move the kernel parameters to any address inside the first 1MB.
|
|
|
|
// 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 = CopyMem (ALIGN32_BELOW(LINUX_ATAG_MAX_OFFSET - KernelParamsSize), KernelParamsAddress, KernelParamsSize);
|
|
|
|
}
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
if ((UINTN)LinuxImage > LINUX_KERNEL_MAX_OFFSET) {
|
|
|
|
//Note: There is no requirement on the alignment
|
|
|
|
LinuxKernel = (LINUX_KERNEL)CopyMem (ALIGN32_BELOW(LINUX_KERNEL_MAX_OFFSET - LinuxImageSize), (VOID*)(UINTN)LinuxImage, LinuxImageSize);
|
|
|
|
}
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
//TODO: Check there is no overlapping between kernel and Atag
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
//
|
|
|
|
// Switch off interrupts, caches, mmu, etc
|
|
|
|
//
|
|
|
|
Status = PreparePlatformHardware ();
|
|
|
|
ASSERT_EFI_ERROR(Status);
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
// Register and print out performance information
|
|
|
|
PERF_END (NULL, "BDS", NULL, 0);
|
|
|
|
if (PerformanceMeasurementEnabled ()) {
|
|
|
|
PrintPerformance ();
|
|
|
|
}
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
//
|
|
|
|
// Start the Linux Kernel
|
|
|
|
//
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
// Outside BootServices, so can't use Print();
|
|
|
|
DEBUG((EFI_D_ERROR, "\nStarting the kernel:\n\n"));
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
// jump to kernel with register set
|
|
|
|
LinuxKernel ((UINTN)0, (UINTN)MachineType, (UINTN)KernelParamsAddress);
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2011-06-11 13:56:30 +02:00
|
|
|
// Kernel should never exit
|
|
|
|
// After Life services are not provided
|
|
|
|
ASSERT(FALSE);
|
2011-02-02 23:35:30 +01:00
|
|
|
|
|
|
|
Exit:
|
2011-06-11 13:56:30 +02:00
|
|
|
// Only be here if we fail to start Linux
|
|
|
|
Print (L"ERROR : Can not start the kernel. Status=0x%X\n", Status);
|
|
|
|
|
|
|
|
// Free Runtimee Memory (kernel and FDT)
|
|
|
|
return Status;
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|