Skip to content

Commit

Permalink
EmbeddedPkg/FdtLib: Added support to load FDT from Firmware Volume
Browse files Browse the repository at this point in the history
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Olivier Martin <[email protected]>



git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15908 6f19259b-4bc3-4df7-8a09-765794883524
  • Loading branch information
oliviermartin authored and oliviermartin committed Aug 26, 2014
1 parent 22044ca commit 5b38a70
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
15 changes: 15 additions & 0 deletions EmbeddedPkg/Include/libfdt_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,19 @@ InstallFdtFromSemihosting (
IN CONST CHAR16* FileName
);

/**
Load and Install FDT from Firmware Volume
@param Filename Guid of the FDT blob to load from firmware volume
@return EFI_SUCCESS Fdt Blob was successfully installed into the configuration table
from firmware volume
@return EFI_NOT_FOUND Failed to locate the file in firmware volume
@return EFI_OUT_OF_RESOURCES Failed to allocate memory to contain the blob
**/
EFI_STATUS
InstallFdtFromFv (
IN CONST EFI_GUID *FileName
);

#endif /* _LIBFDT_ENV_H */
103 changes: 102 additions & 1 deletion EmbeddedPkg/Library/FdtLib/FdtConfigurationTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
*
**/

#include <Uefi.h>
#include <PiDxe.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>

#include <Protocol/DevicePath.h>
#include <Protocol/FirmwareVolume2.h>
#include <Protocol/SimpleFileSystem.h>

#include <Guid/Fdt.h>
Expand Down Expand Up @@ -176,3 +177,103 @@ InstallFdtFromSemihosting (
Fs->Close (Fs);
return Status;
}

/**
Load and Install FDT from Firmware Volume
@param Filename Guid of the FDT blob to load from firmware volume
@return EFI_SUCCESS Fdt Blob was successfully installed into the configuration table
from firmware volume
@return EFI_NOT_FOUND Fail to locate the file in firmware volume
@return EFI_OUT_OF_RESOURCES Fail to allocate memory to contain the blob
**/
EFI_STATUS
InstallFdtFromFv (
IN CONST EFI_GUID *FileName
)
{
EFI_STATUS Status;
EFI_HANDLE *HandleBuffer;
UINTN NumberOfHandles;
UINT32 FvStatus;
UINTN Index;
EFI_FIRMWARE_VOLUME2_PROTOCOL *FvInstance;
INTN SectionInstance;
UINTN FdtSize;
VOID* FdtBlob;
EFI_PHYSICAL_ADDRESS FdtBase;

FvStatus = 0;
SectionInstance = 0;

// Locate all the Firmware Volume protocols.
Status = gBS->LocateHandleBuffer (
ByProtocol,
&gEfiFirmwareVolume2ProtocolGuid,
NULL,
&NumberOfHandles,
&HandleBuffer
);
if (EFI_ERROR (Status)) {
return Status;
}

// Looking for FV that contains the FDT blob
for (Index = 0; Index < NumberOfHandles; Index++) {
//
// Get the protocol on this handle
// This should not fail because of LocateHandleBuffer
//
Status = gBS->HandleProtocol (
HandleBuffer[Index],
&gEfiFirmwareVolume2ProtocolGuid,
(VOID**) &FvInstance
);
if (EFI_ERROR (Status)) {
goto FREE_HANDLE_BUFFER;
}

while (Status == EFI_SUCCESS) {
// FdtBlob must be allocated by ReadSection
FdtBlob = NULL;

// See if it contains the FDT file
Status = FvInstance->ReadSection (
FvInstance,
FileName,
EFI_SECTION_RAW,
SectionInstance,
&FdtBlob,
&FdtSize,
&FvStatus
);
if (!EFI_ERROR (Status)) {
// When the FDT blob is attached to the Configuration Table it is recommended to load it as Runtime Service Data
// to prevent the kernel to overwrite its data
Status = gBS->AllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (FdtSize), &FdtBase);
if (EFI_ERROR (Status)) {
goto FREE_HANDLE_BUFFER;
}

// Copy the FDT to the Runtime memory
gBS->CopyMem ((VOID*)(UINTN)FdtBase, FdtBlob, FdtSize);
// Free the buffer allocated by FvInstance->ReadSection()
gBS->FreePool (FdtBlob);

// Install the FDT as part of the UEFI Configuration Table
Status = InstallFdtIntoConfigurationTable ((VOID*)(UINTN)FdtBase, FdtSize);
if (EFI_ERROR (Status)) {
gBS->FreePages (FdtBase, EFI_SIZE_TO_PAGES (FdtSize));
}
break;
}
}
}

FREE_HANDLE_BUFFER:
// Free any allocated buffers
gBS->FreePool (HandleBuffer);

return Status;
}
1 change: 1 addition & 0 deletions EmbeddedPkg/Library/FdtLib/FdtLib.inf
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
[Protocols]
gEfiDevicePathProtocolGuid
gEfiSimpleFileSystemProtocolGuid
gEfiFirmwareVolume2ProtocolGuid

[Guids]
gEfiFileInfoGuid
Expand Down

0 comments on commit 5b38a70

Please sign in to comment.