diff options
author | oliviermartin <oliviermartin@6f19259b-4bc3-4df7-8a09-765794883524> | 2013-01-25 11:28:06 +0000 |
---|---|---|
committer | oliviermartin <oliviermartin@6f19259b-4bc3-4df7-8a09-765794883524> | 2013-01-25 11:28:06 +0000 |
commit | 1e57a46299244793beb27e74be171d1540606999 (patch) | |
tree | 8644a24d6e4b4cfd080d4c40ccf2d3d9f13760f9 /EmbeddedPkg/Library | |
parent | 5767f22fca7c337cdc113e14b411c1fd0ea7bd53 (diff) | |
download | edk2-1e57a46299244793beb27e74be171d1540606999.tar.gz edk2-1e57a46299244793beb27e74be171d1540606999.tar.bz2 edk2-1e57a46299244793beb27e74be171d1540606999.zip |
ARM Packages: Fixed line endings
This large code change only modifies the line endings to be CRLF to be
compliant with the EDK2 coding convention document.
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@14088 6f19259b-4bc3-4df7-8a09-765794883524
Diffstat (limited to 'EmbeddedPkg/Library')
29 files changed, 8378 insertions, 8378 deletions
diff --git a/EmbeddedPkg/Library/DebugAgentTimerLibNull/DebugAgentTimerLib.c b/EmbeddedPkg/Library/DebugAgentTimerLibNull/DebugAgentTimerLib.c index 7ee5609c5b..7fab09bad6 100755 --- a/EmbeddedPkg/Library/DebugAgentTimerLibNull/DebugAgentTimerLib.c +++ b/EmbeddedPkg/Library/DebugAgentTimerLibNull/DebugAgentTimerLib.c @@ -61,4 +61,4 @@ DebugAgentTimerEndOfInterrupt ( {
}
-
\ No newline at end of file +
\ No newline at end of file diff --git a/EmbeddedPkg/Library/EblAddExternalCommandLib/EblAddExternalCommandLib.inf b/EmbeddedPkg/Library/EblAddExternalCommandLib/EblAddExternalCommandLib.inf index 1429c37f29..03258751c0 100644 --- a/EmbeddedPkg/Library/EblAddExternalCommandLib/EblAddExternalCommandLib.inf +++ b/EmbeddedPkg/Library/EblAddExternalCommandLib/EblAddExternalCommandLib.inf @@ -45,4 +45,4 @@ gEfiEblAddCommandProtocolGuid
[Guids]
-
\ No newline at end of file +
\ No newline at end of file diff --git a/EmbeddedPkg/Library/EblCmdLibNull/EblCmdLibNull.inf b/EmbeddedPkg/Library/EblCmdLibNull/EblCmdLibNull.inf index 3fd8e92dcd..52e343997d 100644 --- a/EmbeddedPkg/Library/EblCmdLibNull/EblCmdLibNull.inf +++ b/EmbeddedPkg/Library/EblCmdLibNull/EblCmdLibNull.inf @@ -41,4 +41,4 @@ [Protocols]
[Guids]
-
\ No newline at end of file +
\ No newline at end of file diff --git a/EmbeddedPkg/Library/EblNetworkLib/EblNetworkLib.c b/EmbeddedPkg/Library/EblNetworkLib/EblNetworkLib.c index 0edd5d018d..3b0f6a1c71 100644 --- a/EmbeddedPkg/Library/EblNetworkLib/EblNetworkLib.c +++ b/EmbeddedPkg/Library/EblNetworkLib/EblNetworkLib.c @@ -1,173 +1,173 @@ -/** @file - - Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR> - - 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 <Uefi.h> -#include <Library/BaseMemoryLib.h> -#include <Library/DebugLib.h> -#include <Library/UefiRuntimeServicesTableLib.h> -#include <Library/UefiBootServicesTableLib.h> - -#include <Protocol/SimpleNetwork.h> -#include <Protocol/PxeBaseCode.h> - - -BOOLEAN gUseIpv6 = FALSE; - -EFI_STATUS -EFIAPI -EblGetCurrentIpAddress ( - IN OUT EFI_IP_ADDRESS *Ip - ) -{ - EFI_STATUS Status; - EFI_PXE_BASE_CODE_PROTOCOL *Pxe; - - Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe); - if (EFI_ERROR(Status)) { - return Status; - } - - Status = Pxe->Start (Pxe, gUseIpv6); - if (EFI_ERROR(Status) && (Status != EFI_ALREADY_STARTED)) { - return Status; - } - - CopyMem (Ip, &Pxe->Mode->StationIp, sizeof (EFI_IP_ADDRESS)); - - return EFI_SUCCESS; -} - - -EFI_STATUS -EFIAPI -EblGetCurrentMacAddress ( - IN OUT EFI_MAC_ADDRESS *Mac - ) -{ - EFI_STATUS Status; - EFI_SIMPLE_NETWORK_PROTOCOL *SimpleNet; - - Status = gBS->LocateProtocol (&gEfiSimpleNetworkProtocolGuid, NULL, (VOID **)&SimpleNet); - if (EFI_ERROR(Status)) { - return Status; - } - - CopyMem (Mac, SimpleNet->Mode->CurrentAddress.Addr, sizeof (EFI_MAC_ADDRESS)); - return Status; -} - - -CHAR8 * -EFIAPI -EblLoadFileBootTypeString ( - IN EFI_HANDLE Handle - ) -{ - EFI_STATUS Status; - VOID *NullPtr; - - Status = gBS->HandleProtocol (Handle, &gEfiPxeBaseCodeProtocolGuid, &NullPtr); - if (!EFI_ERROR (Status)) { - return "EFI PXE Network Boot"; - } - - return ""; -} - -EFI_STATUS -EFIAPI -EblPerformDHCP ( - IN BOOLEAN SortOffers - ) -{ - EFI_STATUS Status; - EFI_PXE_BASE_CODE_PROTOCOL *Pxe; - - Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe); - if (EFI_ERROR(Status)) { - return Status; - } - - Status = Pxe->Start (Pxe, gUseIpv6); - if (EFI_ERROR(Status) && (Status != EFI_ALREADY_STARTED)) { - return Status; - } - - Status = Pxe->Dhcp(Pxe, TRUE); - return Status; -} - - -EFI_STATUS -EFIAPI -EblSetStationIp ( - IN EFI_IP_ADDRESS *NewStationIp, OPTIONAL - IN EFI_IP_ADDRESS *NewSubnetMask OPTIONAL - ) -{ - EFI_STATUS Status; - EFI_PXE_BASE_CODE_PROTOCOL *Pxe; - - Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe); - if (EFI_ERROR(Status)) { - return Status; - } - - Status = Pxe->Start (Pxe, gUseIpv6); - if (EFI_ERROR(Status) && (Status != EFI_ALREADY_STARTED)) { - return Status; - } - - Status = Pxe->SetStationIp (Pxe, NewStationIp, NewSubnetMask); - return Status; -} - - -EFI_STATUS -EFIAPI -EblMtftp ( - IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation, - IN OUT VOID *BufferPtr OPTIONAL, - IN BOOLEAN Overwrite, - IN OUT UINT64 *BufferSize, - IN UINTN *BlockSize OPTIONAL, - IN EFI_IP_ADDRESS *ServerIp, - IN UINT8 *Filename OPTIONAL, - IN EFI_PXE_BASE_CODE_MTFTP_INFO *Info OPTIONAL, - IN BOOLEAN DontUseBuffer - ) -{ - EFI_STATUS Status; - EFI_PXE_BASE_CODE_PROTOCOL *Pxe; - - Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe); - if (EFI_ERROR(Status)) { - return Status; - } - - Status = Pxe->Mtftp ( - Pxe, - Operation, - BufferPtr, - Overwrite, - BufferSize, - BlockSize, - ServerIp, - Filename, - Info, - DontUseBuffer - ); - return Status; -} - +/** @file
+
+ Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
+
+ 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 <Uefi.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/UefiRuntimeServicesTableLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+
+#include <Protocol/SimpleNetwork.h>
+#include <Protocol/PxeBaseCode.h>
+
+
+BOOLEAN gUseIpv6 = FALSE;
+
+EFI_STATUS
+EFIAPI
+EblGetCurrentIpAddress (
+ IN OUT EFI_IP_ADDRESS *Ip
+ )
+{
+ EFI_STATUS Status;
+ EFI_PXE_BASE_CODE_PROTOCOL *Pxe;
+
+ Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ Status = Pxe->Start (Pxe, gUseIpv6);
+ if (EFI_ERROR(Status) && (Status != EFI_ALREADY_STARTED)) {
+ return Status;
+ }
+
+ CopyMem (Ip, &Pxe->Mode->StationIp, sizeof (EFI_IP_ADDRESS));
+
+ return EFI_SUCCESS;
+}
+
+
+EFI_STATUS
+EFIAPI
+EblGetCurrentMacAddress (
+ IN OUT EFI_MAC_ADDRESS *Mac
+ )
+{
+ EFI_STATUS Status;
+ EFI_SIMPLE_NETWORK_PROTOCOL *SimpleNet;
+
+ Status = gBS->LocateProtocol (&gEfiSimpleNetworkProtocolGuid, NULL, (VOID **)&SimpleNet);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ CopyMem (Mac, SimpleNet->Mode->CurrentAddress.Addr, sizeof (EFI_MAC_ADDRESS));
+ return Status;
+}
+
+
+CHAR8 *
+EFIAPI
+EblLoadFileBootTypeString (
+ IN EFI_HANDLE Handle
+ )
+{
+ EFI_STATUS Status;
+ VOID *NullPtr;
+
+ Status = gBS->HandleProtocol (Handle, &gEfiPxeBaseCodeProtocolGuid, &NullPtr);
+ if (!EFI_ERROR (Status)) {
+ return "EFI PXE Network Boot";
+ }
+
+ return "";
+}
+
+EFI_STATUS
+EFIAPI
+EblPerformDHCP (
+ IN BOOLEAN SortOffers
+ )
+{
+ EFI_STATUS Status;
+ EFI_PXE_BASE_CODE_PROTOCOL *Pxe;
+
+ Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ Status = Pxe->Start (Pxe, gUseIpv6);
+ if (EFI_ERROR(Status) && (Status != EFI_ALREADY_STARTED)) {
+ return Status;
+ }
+
+ Status = Pxe->Dhcp(Pxe, TRUE);
+ return Status;
+}
+
+
+EFI_STATUS
+EFIAPI
+EblSetStationIp (
+ IN EFI_IP_ADDRESS *NewStationIp, OPTIONAL
+ IN EFI_IP_ADDRESS *NewSubnetMask OPTIONAL
+ )
+{
+ EFI_STATUS Status;
+ EFI_PXE_BASE_CODE_PROTOCOL *Pxe;
+
+ Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ Status = Pxe->Start (Pxe, gUseIpv6);
+ if (EFI_ERROR(Status) && (Status != EFI_ALREADY_STARTED)) {
+ return Status;
+ }
+
+ Status = Pxe->SetStationIp (Pxe, NewStationIp, NewSubnetMask);
+ return Status;
+}
+
+
+EFI_STATUS
+EFIAPI
+EblMtftp (
+ IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation,
+ IN OUT VOID *BufferPtr OPTIONAL,
+ IN BOOLEAN Overwrite,
+ IN OUT UINT64 *BufferSize,
+ IN UINTN *BlockSize OPTIONAL,
+ IN EFI_IP_ADDRESS *ServerIp,
+ IN UINT8 *Filename OPTIONAL,
+ IN EFI_PXE_BASE_CODE_MTFTP_INFO *Info OPTIONAL,
+ IN BOOLEAN DontUseBuffer
+ )
+{
+ EFI_STATUS Status;
+ EFI_PXE_BASE_CODE_PROTOCOL *Pxe;
+
+ Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ Status = Pxe->Mtftp (
+ Pxe,
+ Operation,
+ BufferPtr,
+ Overwrite,
+ BufferSize,
+ BlockSize,
+ ServerIp,
+ Filename,
+ Info,
+ DontUseBuffer
+ );
+ return Status;
+}
+
diff --git a/EmbeddedPkg/Library/EblNetworkLib/EblNetworkLib.inf b/EmbeddedPkg/Library/EblNetworkLib/EblNetworkLib.inf index b0d6e80aea..4d66bacb81 100644 --- a/EmbeddedPkg/Library/EblNetworkLib/EblNetworkLib.inf +++ b/EmbeddedPkg/Library/EblNetworkLib/EblNetworkLib.inf @@ -31,4 +31,4 @@ gEfiPxeBaseCodeProtocolGuid
[Depex]
- TRUE
\ No newline at end of file + TRUE
\ No newline at end of file diff --git a/EmbeddedPkg/Library/EfiFileLib/EfiFileLib.c b/EmbeddedPkg/Library/EfiFileLib/EfiFileLib.c index 5e0f9ca117..83a3b7841f 100644 --- a/EmbeddedPkg/Library/EfiFileLib/EfiFileLib.c +++ b/EmbeddedPkg/Library/EfiFileLib/EfiFileLib.c @@ -1,1778 +1,1778 @@ -/** @file -File IO routines inspired by Streams with an EFI flavor - -Copyright (c) 2007, Intel Corporation. All rights reserved.<BR> -Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR> - -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. - -Basic support for opening files on different device types. The device string -is in the form of DevType:Path. Current DevType is required as there is no -current mounted device concept of current working directory concept implement -by this library. - -Device names are case insensitive and only check the leading characters for -unique matches. Thus the following are all the same: -LoadFile0: -l0: -L0: -Lo0: - -Supported Device Names: -A0x1234:0x12 - A memory buffer starting at address 0x1234 for 0x12 bytes -l1: - EFI LoadFile device one. -B0: - EFI BlockIo zero. -fs3: - EFI Simple File System device 3 -Fv2: - EFI Firmware VOlume device 2 -10.0.1.102: - TFTP service IP followed by the file name -**/ - -#include <PiDxe.h> -#include <Protocol/BlockIo.h> -#include <Protocol/DiskIo.h> -#include <Protocol/SimpleFileSystem.h> -#include <Protocol/FirmwareVolume2.h> -#include <Protocol/LoadFile.h> -#include <Protocol/FirmwareVolumeBlock.h> -#include <Guid/FileInfo.h> -#include <Library/BaseLib.h> -#include <Library/MemoryAllocationLib.h> -#include <Library/DevicePathLib.h> -#include <Library/PrintLib.h> -#include <Library/BaseMemoryLib.h> -#include <Library/UefiLib.h> -#include <Library/UefiBootServicesTableLib.h> -#include <Library/UefiRuntimeServicesTableLib.h> -#include <Library/DebugLib.h> -#include <Library/EfiFileLib.h> -#include <Library/PcdLib.h> -#include <Library/EblNetworkLib.h> - - -CHAR8 *gCwd = NULL; - -CONST EFI_GUID gZeroGuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } }; - -#define EFI_OPEN_FILE_GUARD_HEADER 0x4B4D4641 -#define EFI_OPEN_FILE_GUARD_FOOTER 0x444D5A56 - -// Need to defend against this overflowing -#define MAX_CMD_LINE 0x200 - -typedef struct { - UINT32 Header; - EFI_OPEN_FILE File; - UINT32 Footer; -} EFI_OPEN_FILE_GUARD; - - -// globals to store current open device info -EFI_HANDLE *mBlkIo = NULL; -UINTN mBlkIoCount = 0; - -EFI_HANDLE *mFs = NULL; -UINTN mFsCount = 0; -// mFsInfo[] array entries must match mFs[] handles -EFI_FILE_SYSTEM_INFO **mFsInfo = NULL; - -EFI_HANDLE *mFv = NULL; -UINTN mFvCount = 0; -EFI_HANDLE *mLoadFile = NULL; -UINTN mLoadFileCount = 0; - - - -/** -Internal worker function to validate a File handle. - -@param File Open File Handle - -@return TRUE File is valid -@return FALSE File is not valid - - -**/ -BOOLEAN -FileHandleValid ( - IN EFI_OPEN_FILE *File - ) -{ - EFI_OPEN_FILE_GUARD *GuardFile; - - // Look right before and after file structure for the correct signatures - GuardFile = BASE_CR (File, EFI_OPEN_FILE_GUARD, File); - if ((GuardFile->Header != EFI_OPEN_FILE_GUARD_HEADER) || - (GuardFile->Footer != EFI_OPEN_FILE_GUARD_FOOTER) ) { - return FALSE; - } - - return TRUE; -} - -/** -Internal worker function. If Buffer is not NULL free it. - -@param Buffer Buffer to FreePool() - -**/ -VOID -EblFreePool ( - IN VOID *Buffer - ) -{ - if (Buffer != NULL) { - FreePool (Buffer); - } -} - -/** -Update Device List Global Variables - -**/ -VOID -EblUpdateDeviceLists ( - VOID - ) -{ - EFI_STATUS Status; - UINTN Size; - EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs; - EFI_FILE_HANDLE Root; - UINTN Index; - - if (mBlkIo != NULL) { - FreePool (mBlkIo); - } - gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &mBlkIoCount, &mBlkIo); - - - - if (mFv != NULL) { - FreePool (mFv); - } - gBS->LocateHandleBuffer (ByProtocol, &gEfiFirmwareVolume2ProtocolGuid, NULL, &mFvCount, &mFv); - - if (mLoadFile != NULL) { - FreePool (mLoadFile); - } - gBS->LocateHandleBuffer (ByProtocol, &gEfiLoadFileProtocolGuid, NULL, &mLoadFileCount, &mLoadFile); - - if (mFs != NULL) { - FreePool (mFs); - } - - if (&mFsInfo[0] != NULL) { - // Need to Free the mFsInfo prior to recalculating mFsCount so don't move this code - for (Index = 0; Index < mFsCount; Index++) { - if (mFsInfo[Index] != NULL) { - FreePool (mFsInfo[Index]); - } - } - FreePool (mFsInfo); - } - - gBS->LocateHandleBuffer (ByProtocol, &gEfiSimpleFileSystemProtocolGuid, NULL, &mFsCount, &mFs); - - - mFsInfo = AllocateZeroPool (mFsCount * sizeof (EFI_FILE_SYSTEM_INFO *)); - if (mFsInfo == NULL) { - // If we can't do this then we can't support file system entries - mFsCount = 0; - } else { - // Loop through all the file system structures and cache the file system info data - for (Index =0; Index < mFsCount; Index++) { - Status = gBS->HandleProtocol (mFs[Index], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&Fs); - if (!EFI_ERROR (Status)) { - Status = Fs->OpenVolume (Fs, &Root); - if (!EFI_ERROR (Status)) { - // Get information about the volume - Size = 0; - Status = Root->GetInfo (Root, &gEfiFileSystemInfoGuid, &Size, mFsInfo[Index]); - if (Status == EFI_BUFFER_TOO_SMALL) { - mFsInfo[Index] = AllocatePool (Size); - Status = Root->GetInfo (Root, &gEfiFileSystemInfoGuid, &Size, mFsInfo[Index]); - } - - Root->Close (Root); - } - } - } - } -} - - -/** -PathName is in the form <device name>:<path> for example fs1:\ or ROOT:\. -Return TRUE if the <devce name> prefix of PathName matches a file system -Volume Name. MatchIndex is the array index in mFsInfo[] of the match, -and it can be used with mFs[] to find the handle that needs to be opened - -@param PathName PathName to check -@param FileStart Index of the first character of the <path> -@param MatchIndex Index in mFsInfo[] that matches - -@return TRUE PathName matches a Volume Label and MatchIndex is valid -@return FALSE PathName does not match a Volume Label MatchIndex undefined - -**/ -BOOLEAN -EblMatchVolumeName ( - IN CHAR8 *PathName, - IN UINTN FileStart, - OUT UINTN *MatchIndex - ) -{ - UINTN Index; - UINTN Compare; - UINTN VolStrLen; - BOOLEAN Match; - - for (Index =0; Index < mFsCount; Index++) { - if (mFsInfo[Index] == NULL) { - // FsInfo is not valid so skip it - continue; - } - VolStrLen = StrLen (mFsInfo[Index]->VolumeLabel); - for (Compare = 0, Match = TRUE; Compare < (FileStart - 1); Compare++) { - if (Compare > VolStrLen) { - Match = FALSE; - break; - } - if (PathName[Compare] != (CHAR8)mFsInfo[Index]->VolumeLabel[Compare]) { - // If the VolumeLabel has a space allow a _ to match with it in addition to ' ' - if (!((PathName[Compare] == '_') && (mFsInfo[Index]->VolumeLabel[Compare] == L' '))) { - Match = FALSE; - break; - } - } - } - if (Match) { - *MatchIndex = Index; - return TRUE; - } - } - - return FALSE; -} - - -/** -Return the number of devices of the current type active in the system - -@param Type Device type to check - -@return 0 Invalid type - -**/ -UINTN -EfiGetDeviceCounts ( - IN EFI_OPEN_FILE_TYPE DeviceType - ) -{ - switch (DeviceType) { - case EfiOpenLoadFile: - return mLoadFileCount; - case EfiOpenFirmwareVolume: - return mFvCount; - case EfiOpenFileSystem: - return mFsCount; - case EfiOpenBlockIo: - return mBlkIoCount; - default: - return 0; - } -} - -EFI_STATUS -ConvertIpStringToEfiIp ( - IN CHAR8 *PathName, - OUT EFI_IP_ADDRESS *ServerIp - ) -{ - CHAR8 *Str; - - Str = PathName; - ServerIp->v4.Addr[0] = (UINT8)AsciiStrDecimalToUintn (Str); - - Str = AsciiStrStr (Str, "."); - if (Str == NULL) { - return EFI_DEVICE_ERROR; - } - - ServerIp->v4.Addr[1] = (UINT8)AsciiStrDecimalToUintn (++Str); - - Str = AsciiStrStr (Str, "."); - if (Str == NULL) { - return EFI_DEVICE_ERROR; - } - - ServerIp->v4.Addr[2] = (UINT8)AsciiStrDecimalToUintn (++Str); - - Str = AsciiStrStr (Str, "."); - if (Str == NULL) { - return EFI_DEVICE_ERROR; - } - - ServerIp->v4.Addr[3] = (UINT8)AsciiStrDecimalToUintn (++Str); - - return EFI_SUCCESS; -} - - -/** -Internal work function to extract a device number from a string skipping -text. Easy way to extract numbers from strings like blk7:. - -@param Str String to extract device number form - -@return -1 Device string is not valid -@return Device # - -**/ -UINTN -EblConvertDevStringToNumber ( - IN CHAR8 *Str - ) -{ - UINTN Max; - UINTN Index; - - - // Find the first digit - Max = AsciiStrLen (Str); - for (Index = 0; !((*Str >= '0') && (*Str <= '9')) && (Index < Max); Index++) { - Str++; - } - if (Index == Max) { - return (UINTN)-1; - } - - return AsciiStrDecimalToUintn (Str); -} - - -/** -Internal work function to fill in EFI_OPEN_FILE information for the Fs and BlkIo - -@param File Open file handle -@param FileName Name of file after device stripped off - - -**/ -EFI_STATUS -EblFileDevicePath ( - IN OUT EFI_OPEN_FILE *File, - IN CHAR8 *FileName, - IN CONST UINT64 OpenMode - ) -{ - EFI_STATUS Status; - UINTN Size; - FILEPATH_DEVICE_PATH *FilePath; - EFI_DEVICE_PATH_PROTOCOL *FileDevicePath; - CHAR16 UnicodeFileName[MAX_PATHNAME]; - EFI_BLOCK_IO_PROTOCOL *BlkIo; - EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs; - EFI_FILE_HANDLE Root; - - - if ( *FileName != 0 ) { - AsciiStrToUnicodeStr (FileName, UnicodeFileName); - } else { - AsciiStrToUnicodeStr ("\\", UnicodeFileName); - } - - Size = StrSize (UnicodeFileName); - FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + sizeof (EFI_DEVICE_PATH_PROTOCOL)); - if (FileDevicePath != NULL) { - FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath; - FilePath->Header.Type = MEDIA_DEVICE_PATH; - FilePath->Header.SubType = MEDIA_FILEPATH_DP; - CopyMem (&FilePath->PathName, UnicodeFileName, Size); - SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH); - SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header)); - - if (File->EfiHandle != NULL) { - File->DevicePath = DevicePathFromHandle (File->EfiHandle); - } - - File->DevicePath = AppendDevicePath (File->DevicePath, FileDevicePath); - FreePool (FileDevicePath); - } - - Status = gBS->HandleProtocol (File->EfiHandle, &gEfiBlockIoProtocolGuid, (VOID **)&BlkIo); - if (!EFI_ERROR (Status)) { - File->FsBlockIoMedia = BlkIo->Media; - File->FsBlockIo = BlkIo; - - // If we are not opening the device this will get over written with file info - File->MaxPosition = MultU64x32 (BlkIo->Media->LastBlock + 1, BlkIo->Media->BlockSize); - } - - if (File->Type == EfiOpenFileSystem) { - Status = gBS->HandleProtocol (File->EfiHandle, &gEfiSimpleFileSystemProtocolGuid, (VOID **)&Fs); - if (!EFI_ERROR (Status)) { - Status = Fs->OpenVolume (Fs, &Root); - if (!EFI_ERROR (Status)) { - // Get information about the volume - Size = 0; - Status = Root->GetInfo (Root, &gEfiFileSystemInfoGuid, &Size, File->FsInfo); - if (Status == EFI_BUFFER_TOO_SMALL) { - File->FsInfo = AllocatePool (Size); - Status = Root->GetInfo (Root, &gEfiFileSystemInfoGuid, &Size, File->FsInfo); - } - - // Get information about the file - Status = Root->Open (Root, &File->FsFileHandle, UnicodeFileName, OpenMode, 0); - if (!EFI_ERROR (Status)) { - Size = 0; - Status = File->FsFileHandle->GetInfo (File->FsFileHandle, &gEfiFileInfoGuid, &Size, NULL); - if (Status == EFI_BUFFER_TOO_SMALL) { - File->FsFileInfo = AllocatePool (Size); - Status = File->FsFileHandle->GetInfo (File->FsFileHandle, &gEfiFileInfoGuid, &Size, File->FsFileInfo); - if (!EFI_ERROR (Status)) { - File->Size = (UINTN)File->FsFileInfo->FileSize; - File->MaxPosition = (UINT64)File->Size; - } - } - } - - Root->Close (Root); - } - } - } else if (File->Type == EfiOpenBlockIo) { - File->Size = (UINTN)File->MaxPosition; - } - - return Status; -} - -#define ToUpper(a) ((((a) >= 'a') && ((a) <= 'z')) ? ((a) - 'a' + 'A') : (a)) - -EFI_STATUS -CompareGuidToString ( - IN EFI_GUID *Guid, - IN CHAR8 *String - ) -{ - CHAR8 AsciiGuid[64]; - CHAR8 *StringPtr; - CHAR8 *GuidPtr; - - AsciiSPrint (AsciiGuid, sizeof(AsciiGuid), "%g", Guid); - - StringPtr = String; - GuidPtr = AsciiGuid; - - while ((*StringPtr != '\0') && (*GuidPtr != '\0')) { - // Skip dashes - if (*StringPtr == '-') { - StringPtr++; - continue; - } - - if (*GuidPtr == '-') { - GuidPtr++; - continue; - } - - if (ToUpper(*StringPtr) != ToUpper(*GuidPtr)) { - return EFI_NOT_FOUND; - } - - StringPtr++; - GuidPtr++; - } - - return EFI_SUCCESS; -} - - -/** -Internal work function to fill in EFI_OPEN_FILE information for the FV - -@param File Open file handle -@param FileName Name of file after device stripped off - - -**/ -EFI_STATUS -EblFvFileDevicePath ( - IN OUT EFI_OPEN_FILE *File, - IN CHAR8 *FileName, - IN CONST UINT64 OpenMode - ) -{ - EFI_STATUS Status; - EFI_STATUS GetNextFileStatus; - MEDIA_FW_VOL_FILEPATH_DEVICE_PATH DevicePathNode; - EFI_DEVICE_PATH_PROTOCOL *DevicePath; - UINTN Key; - UINT32 AuthenticationStatus; - CHAR8 AsciiSection[MAX_PATHNAME]; - VOID *Section; - UINTN SectionSize; - EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb; - EFI_LBA Lba; - UINTN BlockSize; - UINTN NumberOfBlocks; - EFI_FIRMWARE_VOLUME_HEADER *FvHeader = NULL; - UINTN Index; - - - Status = gBS->HandleProtocol (File->EfiHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&File->Fv); - if (EFI_ERROR (Status)) { - return Status; - } - - // Get FVB Info about the handle - Status = gBS->HandleProtocol (File->EfiHandle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **)&Fvb); - if (!EFI_ERROR (Status)) { - Status = Fvb->GetPhysicalAddress (Fvb, &File->FvStart); - if (!EFI_ERROR (Status)) { - FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)File->FvStart; - File->FvHeaderSize = sizeof (EFI_FIRMWARE_VOLUME_HEADER); - for (Index = 0; FvHeader->BlockMap[Index].Length !=0; Index++) { - File->FvHeaderSize += sizeof (EFI_FV_BLOCK_MAP_ENTRY); - } - - for (Lba = 0, File->FvSize = 0, NumberOfBlocks = 0; ; File->FvSize += (BlockSize * NumberOfBlocks), Lba += NumberOfBlocks) { - Status = Fvb->GetBlockSize (Fvb, Lba, &BlockSize, &NumberOfBlocks); - if (EFI_ERROR (Status)) { - break; - } - } - } - } - - - DevicePath = DevicePathFromHandle (File->EfiHandle); - - if (*FileName == '\0') { - File->DevicePath = DuplicateDevicePath (DevicePath); - File->Size = File->FvSize; - File->MaxPosition = File->Size; - } else { - Key = 0; - do { - File->FvType = EFI_FV_FILETYPE_ALL; - GetNextFileStatus = File->Fv->GetNextFile ( - File->Fv, - &Key, - &File->FvType, - &File->FvNameGuid, - &File->FvAttributes, - &File->Size - ); - if (!EFI_ERROR (GetNextFileStatus)) { - // Compare GUID first - Status = CompareGuidToString (&File->FvNameGuid, FileName); - if (!EFI_ERROR(Status)) { - break; - } - - Section = NULL; - Status = File->Fv->ReadSection ( - File->Fv, - &File->FvNameGuid, - EFI_SECTION_USER_INTERFACE, - 0, - &Section, - &SectionSize, - &AuthenticationStatus - ); - if (!EFI_ERROR (Status)) { - UnicodeStrToAsciiStr (Section, AsciiSection); - if (AsciiStriCmp (FileName, AsciiSection) == 0) { - FreePool (Section); - break; - } - FreePool (Section); - } - } - } while (!EFI_ERROR (GetNextFileStatus)); - - if (EFI_ERROR (GetNextFileStatus)) { - return GetNextFileStatus; - } - - if (OpenMode != EFI_SECTION_ALL) { - // Calculate the size of the section we are targeting - Section = NULL; - File->Size = 0; - Status = File->Fv->ReadSection ( - File->Fv, - &File->FvNameGuid, - (EFI_SECTION_TYPE)OpenMode, - 0, - &Section, - &File->Size, - &AuthenticationStatus - ); - if (EFI_ERROR (Status)) { - return Status; - } - } - - File->MaxPosition = File->Size; - EfiInitializeFwVolDevicepathNode (&DevicePathNode, &File->FvNameGuid); - File->DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&DevicePathNode); - } - - - // FVB not required if FV was soft loaded... - return EFI_SUCCESS; -} - - - - -/** -Open a device named by PathName. The PathName includes a device name and -path separated by a :. See file header for more details on the PathName -syntax. There is no checking to prevent a file from being opened more than -one type. - -SectionType is only used to open an FV. Each file in an FV contains multiple -sections and only the SectionType section is opened. - -For any file that is opened with EfiOpen() must be closed with EfiClose(). - -@param PathName Path to parse to open -@param OpenMode Same as EFI_FILE.Open() -@param SectionType Section in FV to open. - -@return NULL Open failed -@return Valid EFI_OPEN_FILE handle - -**/ -EFI_OPEN_FILE * -EfiOpen ( - IN CHAR8 *PathName, - IN CONST UINT64 OpenMode, - IN CONST EFI_SECTION_TYPE SectionType - ) -{ - EFI_STATUS Status; - EFI_OPEN_FILE *File; - EFI_OPEN_FILE FileData; - UINTN StrLen; - UINTN FileStart; - UINTN DevNumber = 0; - EFI_OPEN_FILE_GUARD *GuardFile; - BOOLEAN VolumeNameMatch; - EFI_DEVICE_PATH_PROTOCOL *DevicePath; - UINTN Size; - EFI_IP_ADDRESS Ip; - CHAR8 *CwdPlusPathName; - UINTN Index; - EFI_SECTION_TYPE ModifiedSectionType; - - EblUpdateDeviceLists (); - - File = &FileData; - ZeroMem (File, sizeof (EFI_OPEN_FILE)); - - StrLen = AsciiStrSize (PathName); - if (StrLen <= 1) { - // Smallest valid path is 1 char and a null - return NULL; - } - - for (FileStart = 0; FileStart < StrLen; FileStart++) { - if (PathName[FileStart] == ':') { - FileStart++; - break; - } - } - - // - // Matching volume name has precedence over handle based names - // - VolumeNameMatch = EblMatchVolumeName (PathName, FileStart, &DevNumber); - if (!VolumeNameMatch) { - if (FileStart == StrLen) { - // No Volume name or device name, so try Current Working Directory - if (gCwd == NULL) { - // No CWD - return NULL; - } - - // We could add a current working directory concept - CwdPlusPathName = AllocatePool (AsciiStrSize (gCwd) + AsciiStrSize (PathName)); - if (CwdPlusPathName == NULL) { - return NULL; - } - - if ((PathName[0] == '/') || (PathName[0] == '\\')) { - // PathName starts in / so this means we go to the root of the device in the CWD. - CwdPlusPathName[0] = '\0'; - for (FileStart = 0; gCwd[FileStart] != '\0'; FileStart++) { - CwdPlusPathName[FileStart] = gCwd[FileStart]; - if (gCwd[FileStart] == ':') { - FileStart++; - CwdPlusPathName[FileStart] = '\0'; - break; - } - } - } else { - AsciiStrCpy (CwdPlusPathName, gCwd); - StrLen = AsciiStrLen (gCwd); - if ((*PathName != '/') && (*PathName != '\\') && (gCwd[StrLen-1] != '/') && (gCwd[StrLen-1] != '\\')) { - AsciiStrCat (CwdPlusPathName, "\\"); - } - } - - AsciiStrCat (CwdPlusPathName, PathName); - if (AsciiStrStr (CwdPlusPathName, ":") == NULL) { - // Extra error check to make sure we don't recurse and blow stack - return NULL; - } - - File = EfiOpen (CwdPlusPathName, OpenMode, SectionType); - FreePool (CwdPlusPathName); - return File; - } - - DevNumber = EblConvertDevStringToNumber ((CHAR8 *)PathName); - } - - File->DeviceName = AllocatePool (StrLen); - AsciiStrCpy (File->DeviceName, PathName); - File->DeviceName[FileStart - 1] = '\0'; - File->FileName = &File->DeviceName[FileStart]; - if (File->FileName[0] == '\0') { - // if it is just a file name use / as root - File->FileName = "\\"; - } - - // - // Use best match algorithm on the dev names so we only need to look at the - // first few charters to match the full device name. Short name forms are - // legal from the caller. - // - Status = EFI_SUCCESS; - if (*PathName == 'f' || *PathName == 'F' || VolumeNameMatch) { - if (PathName[1] == 's' || PathName[1] == 'S' || VolumeNameMatch) { - if (DevNumber >= mFsCount) { - goto ErrorExit; - } - File->Type = EfiOpenFileSystem; - File->EfiHandle = mFs[DevNumber]; - Status = EblFileDevicePath (File, &PathName[FileStart], OpenMode); - - } else if (PathName[1] == 'v' || PathName[1] == 'V') { - if (DevNumber >= mFvCount) { - goto ErrorExit; - } - File->Type = EfiOpenFirmwareVolume; - File->EfiHandle = mFv[DevNumber]; - - if ((PathName[FileStart] == '/') || (PathName[FileStart] == '\\')) { - // Skip leading / as its not really needed for the FV since no directories are supported - FileStart++; - } - - // Check for 2nd : - ModifiedSectionType = SectionType; - for (Index = FileStart; PathName[Index] != '\0'; Index++) { - if (PathName[Index] == ':') { - // Support fv0:\DxeCore:0x10 - // This means open the PE32 Section of the file - ModifiedSectionType = (EFI_SECTION_TYPE)AsciiStrHexToUintn (&PathName[Index + 1]); - PathName[Index] = '\0'; - } - } - File->FvSectionType = ModifiedSectionType; - Status = EblFvFileDevicePath (File, &PathName[FileStart], ModifiedSectionType); - } - } else if ((*PathName == 'A') || (*PathName == 'a')) { - // Handle a:0x10000000:0x1234 address form a:ADDRESS:SIZE - File->Type = EfiOpenMemoryBuffer; - // 1st colon is at PathName[FileStart - 1] - File->Buffer = (VOID *)AsciiStrHexToUintn (&PathName[FileStart]); - - // Find 2nd colon - while ((PathName[FileStart] != ':') && (PathName[FileStart] != '\0')) { - FileStart++; - } - - // If we ran out of string, there's no extra data - if (PathName[FileStart] == '\0') { - File->Size = 0; - } else { - File->Size = AsciiStrHexToUintn (&PathName[FileStart + 1]); - } - - // if there's no number after the second colon, default - // the end of memory - if (File->Size == 0) { - File->Size = (UINTN)(0 - (UINTN)File->Buffer); - } - - File->MaxPosition = File->Size; - File->BaseOffset = (UINTN)File->Buffer; - - } else if (*PathName== 'l' || *PathName == 'L') { - if (DevNumber >= mLoadFileCount) { - goto ErrorExit; - } - File->Type = EfiOpenLoadFile; - File->EfiHandle = mLoadFile[DevNumber]; - - Status = gBS->HandleProtocol (File->EfiHandle, &gEfiLoadFileProtocolGuid, (VOID **)&File->LoadFile); - if (EFI_ERROR (Status)) { - goto ErrorExit; - } - - Status = gBS->HandleProtocol (File->EfiHandle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePath); - if (EFI_ERROR (Status)) { - goto ErrorExit; - } - File->DevicePath = DuplicateDevicePath (DevicePath); - - } else if (*PathName == 'b' || *PathName == 'B') { - // Handle b#:0x10000000:0x1234 address form b#:ADDRESS:SIZE - if (DevNumber >= mBlkIoCount) { - goto ErrorExit; - } - File->Type = EfiOpenBlockIo; - File->EfiHandle = mBlkIo[DevNumber]; - EblFileDevicePath (File, "", OpenMode); - - // 1st colon is at PathName[FileStart - 1] - File->DiskOffset = AsciiStrHexToUintn (&PathName[FileStart]); - - // Find 2nd colon - while ((PathName[FileStart] != ':') && (PathName[FileStart] != '\0')) { - FileStart++; - } - - // If we ran out of string, there's no extra data - if (PathName[FileStart] == '\0') { - Size = 0; - } else { - Size = AsciiStrHexToUintn (&PathName[FileStart + 1]); - } - - // if a zero size is passed in (or the size is left out entirely), - // go to the end of the device. - if (Size == 0) { - File->Size = File->Size - File->DiskOffset; - } else { - File->Size = Size; - } - - File->MaxPosition = File->Size; - File->BaseOffset = File->DiskOffset; - } else if ((*PathName) >= '0' && (*PathName <= '9')) { - - // Get current IP address - Status = EblGetCurrentIpAddress (&Ip); - if (EFI_ERROR(Status)) { - AsciiPrint("Device IP Address is not configured.\n"); - goto ErrorExit; - } - - - // Parse X.X.X.X:Filename, only support IPv4 TFTP for now... - File->Type = EfiOpenTftp; - File->IsDirty = FALSE; - File->IsBufferValid = FALSE; - - Status = ConvertIpStringToEfiIp (PathName, &File->ServerIp); - } - - if (EFI_ERROR (Status)) { - goto ErrorExit; - } - - GuardFile = (EFI_OPEN_FILE_GUARD *)AllocateZeroPool (sizeof (EFI_OPEN_FILE_GUARD)); - if (GuardFile == NULL) { - goto ErrorExit; - } - - GuardFile->Header = EFI_OPEN_FILE_GUARD_HEADER; - CopyMem (&(GuardFile->File), &FileData, sizeof (EFI_OPEN_FILE)); - GuardFile->Footer = EFI_OPEN_FILE_GUARD_FOOTER; - - return &(GuardFile->File); - -ErrorExit: - FreePool (File->DeviceName); - return NULL; -} - -#define FILE_COPY_CHUNK 0x01000000 - -EFI_STATUS -EfiCopyFile ( - IN CHAR8 *DestinationFile, - IN CHAR8 *SourceFile - ) -{ - EFI_OPEN_FILE *Source = NULL; - EFI_OPEN_FILE *Destination = NULL; - EFI_STATUS Status = EFI_SUCCESS; - VOID *Buffer = NULL; - UINTN Size; - UINTN Offset; - UINTN Chunk = FILE_COPY_CHUNK; - - Source = EfiOpen (SourceFile, EFI_FILE_MODE_READ, 0); - if (Source == NULL) { - AsciiPrint("Source file open error.\n"); - Status = EFI_NOT_FOUND; - goto Exit; - } - - Destination = EfiOpen (DestinationFile, EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0); - if (Destination == NULL) { - AsciiPrint("Destination file open error.\n"); - Status = EFI_NOT_FOUND; - goto Exit; - } - - Buffer = AllocatePool(FILE_COPY_CHUNK); - if (Buffer == NULL) { - Status = EFI_OUT_OF_RESOURCES; - goto Exit; - } - - Size = EfiTell(Source, NULL); - - for (Offset = 0; Offset + FILE_COPY_CHUNK <= Size; Offset += Chunk) { - Chunk = FILE_COPY_CHUNK; - - Status = EfiRead(Source, Buffer, &Chunk); - if (EFI_ERROR(Status)) { - AsciiPrint("Read file error %r\n", Status); - goto Exit; - } - - Status = EfiWrite(Destination, Buffer, &Chunk); - if (EFI_ERROR(Status)) { - AsciiPrint("Write file error %r\n", Status); - goto Exit; - } - } - - // Any left over? - if (Offset < Size) { - Chunk = Size - Offset; - - Status = EfiRead(Source, Buffer, &Chunk); - if (EFI_ERROR(Status)) { - AsciiPrint("Read file error\n"); - goto Exit; - } - - Status = EfiWrite(Destination, Buffer, &Chunk); - if (EFI_ERROR(Status)) { - AsciiPrint("Write file error\n"); - goto Exit; - } - } - -Exit: - if (Source != NULL) { - Status = EfiClose(Source); - if (EFI_ERROR(Status)) { - AsciiPrint("Source close error"); - } - } - - if (Destination != NULL) { - Status = EfiClose(Destination); - if (EFI_ERROR(Status)) { - AsciiPrint("Destination close error"); - } - } - - if (Buffer != NULL) { - FreePool(Buffer); - } - - return Status; -} - -/** -Use DeviceType and Index to form a valid PathName and try and open it. - -@param DeviceType Device type to open -@param Index Device Index to use. Zero relative. - -@return NULL Open failed -@return Valid EFI_OPEN_FILE handle - -**/ -EFI_OPEN_FILE * -EfiDeviceOpenByType ( - IN EFI_OPEN_FILE_TYPE DeviceType, - IN UINTN Index - ) -{ - CHAR8 *DevStr; - CHAR8 Path[MAX_CMD_LINE]; - - switch (DeviceType) { - case EfiOpenLoadFile: - DevStr = "loadfile%d:"; - break; - case EfiOpenFirmwareVolume: - DevStr = "fv%d:"; - break; - case EfiOpenFileSystem: - DevStr = "fs%d:"; - break; - case EfiOpenBlockIo: - DevStr = "blk%d:"; - break; - case EfiOpenMemoryBuffer: - DevStr = "a%d:"; - break; - default: - return NULL; - } - - AsciiSPrint (Path, MAX_PATHNAME, DevStr, Index); - - return EfiOpen (Path, EFI_FILE_MODE_READ, 0); -} - - -/** -Close a file handle opened by EfiOpen() and free all resources allocated by -EfiOpen(). - -@param Stream Open File Handle - -@return EFI_INVALID_PARAMETER Stream is not an Open File -@return EFI_SUCCESS Steam closed - -**/ -EFI_STATUS -EfiClose ( - IN EFI_OPEN_FILE *File - ) -{ - EFI_STATUS Status; - UINT64 TftpBufferSize; - - if (!FileHandleValid (File)) { - return EFI_INVALID_PARAMETER; - } - - //Write the buffer contents to TFTP file. - if ((File->Type == EfiOpenTftp) && (File->IsDirty)) { - - TftpBufferSize = File->Size; - Status = EblMtftp ( - EFI_PXE_BASE_CODE_TFTP_WRITE_FILE, - File->Buffer, - TRUE, - &TftpBufferSize, - NULL, - &File->ServerIp, - (UINT8 *)File->FileName, - NULL, - FALSE - ); - if (EFI_ERROR(Status)) { - AsciiPrint("TFTP error during APPLE_NSP_TFTP_WRITE_FILE: %r\n", Status); - return Status; - } - } - - if ((File->Type == EfiOpenLoadFile) || - ((File->Type == EfiOpenTftp) && (File->IsBufferValid == TRUE)) || - ((File->Type == EfiOpenFirmwareVolume) && (File->IsBufferValid == TRUE))) { - EblFreePool(File->Buffer); - } - - EblFreePool (File->DevicePath); - EblFreePool (File->DeviceName); - EblFreePool (File->FsFileInfo); - EblFreePool (File->FsInfo); - - if (File->FsFileHandle != NULL) { - File->FsFileHandle->Close (File->FsFileHandle); - } - - // Need to free File and it's Guard structures - EblFreePool (BASE_CR (File, EFI_OPEN_FILE_GUARD, File)); - return EFI_SUCCESS; -} - - -/** -Return the size of the file represented by Stream. Also return the current -Seek position. Opening a file will enable a valid file size to be returned. -LoadFile is an exception as a load file size is set to zero. - -@param Stream Open File Handle - -@return 0 Stream is not an Open File or a valid LoadFile handle - -**/ -UINTN -EfiTell ( - IN EFI_OPEN_FILE *File, - OUT EFI_LBA *CurrentPosition OPTIONAL - ) -{ - EFI_STATUS Status; - UINT64 BufferSize = 0; - - if (!FileHandleValid (File)) { - return 0; - } - - if (CurrentPosition != NULL) { - *CurrentPosition = File->CurrentPosition; - } - - if (File->Type == EfiOpenLoadFile) { - // Figure out the File->Size - File->Buffer = NULL; - File->Size = 0; - Status = File->LoadFile->LoadFile (File->LoadFile, File->DevicePath, FALSE, &File->Size, File->Buffer); - if (Status != EFI_BUFFER_TOO_SMALL) { - return 0; - } - - File->MaxPosition = (UINT64)File->Size; - } else if (File->Type == EfiOpenTftp) { - - Status = EblMtftp ( - EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE, - NULL, - FALSE, - &BufferSize, - NULL, - &File->ServerIp, - (UINT8 *)File->FileName, - NULL, - TRUE - ); - if (EFI_ERROR(Status)) { - AsciiPrint("TFTP error during APPLE_NSP_TFTP_GET_FILE_SIZE: %r\n", Status); - return 0; - } - - File->Size = (UINTN)BufferSize; - File->MaxPosition = File->Size; - } - - return File->Size; -} - - -/** -Seek to the Offset location in the file. LoadFile and FV device types do -not support EfiSeek(). It is not possible to grow the file size using -EfiSeek(). - -SeekType defines how use Offset to calculate the new file position: -EfiSeekStart : Position = Offset -EfiSeekCurrent: Position is Offset bytes from the current position -EfiSeekEnd : Only supported if Offset is zero to seek to end of file. - -@param Stream Open File Handle -@param Offset Offset to seek too. -@param SeekType Type of seek to perform - - -@return EFI_INVALID_PARAMETER Stream is not an Open File -@return EFI_UNSUPPORTED LoadFile and FV do not support Seek -@return EFI_NOT_FOUND Seek past the end of the file. -@return EFI_SUCCESS Steam closed - -**/ -EFI_STATUS -EfiSeek ( - IN EFI_OPEN_FILE *File, - IN EFI_LBA Offset, - IN EFI_SEEK_TYPE SeekType - ) -{ - EFI_STATUS Status; - UINT64 CurrentPosition; - - if (!FileHandleValid (File)) { - return EFI_INVALID_PARAMETER; - } - - if (File->Type == EfiOpenLoadFile) { - // LoadFile does not support Seek - return EFI_UNSUPPORTED; - } - - CurrentPosition = File->CurrentPosition; - switch (SeekType) { - case EfiSeekStart: - if (Offset > File->MaxPosition) { - return EFI_NOT_FOUND; - } - CurrentPosition = Offset; - break; - - case EfiSeekCurrent: - if ((File->CurrentPosition + Offset) > File->MaxPosition) { - return EFI_NOT_FOUND; - } - CurrentPosition += Offset; - break; - - case EfiSeekEnd: - if (Offset != 0) { - // We don't support growing file size via seeking past end of file - return EFI_UNSUPPORTED; - } - CurrentPosition = File->MaxPosition; - break; - - default: - return EFI_NOT_FOUND; - } - - Status = EFI_SUCCESS; - if (File->FsFileHandle != NULL) { - Status = File->FsFileHandle->SetPosition (File->FsFileHandle, CurrentPosition); - } - - if (!EFI_ERROR (Status)) { - File->CurrentPosition = CurrentPosition; - } - - return Status; -} - -EFI_STATUS -CacheTftpFile ( - IN OUT EFI_OPEN_FILE *File - ) -{ - EFI_STATUS Status; - UINT64 TftpBufferSize; - - if (File->IsBufferValid) { - return EFI_SUCCESS; - } - - // Make sure the file size is set. - EfiTell (File, NULL); - - //Allocate a buffer to hold the whole file. - File->Buffer = AllocatePool(File->Size); - if (File->Buffer == NULL) { - return EFI_OUT_OF_RESOURCES; - } - - TftpBufferSize = File->Size; - - Status = EblMtftp ( - EFI_PXE_BASE_CODE_TFTP_READ_FILE, - File->Buffer, - FALSE, - &TftpBufferSize, - NULL, - &File->ServerIp, - (UINT8 *)File->FileName, - NULL, - FALSE); - if (EFI_ERROR(Status)) { - AsciiPrint("TFTP error during APPLE_NSP_TFTP_READ_FILE: %r\n", Status); - FreePool(File->Buffer); - return Status; - } - - // Set the buffer valid flag. - File->IsBufferValid = TRUE; - - return Status; -} - -/** -Read BufferSize bytes from the current location in the file. For load file, -FV, and TFTP case you must read the entire file. - -@param Stream Open File Handle -@param Buffer Caller allocated buffer. -@param BufferSize Size of buffer in bytes. - - -@return EFI_SUCCESS Stream is not an Open File -@return EFI_END_OF_FILE Tried to read past the end of the file -@return EFI_INVALID_PARAMETER Stream is not an open file handle -@return EFI_BUFFER_TOO_SMALL Buffer is not big enough to do the read -@return "other" Error returned from device read - -**/ -EFI_STATUS -EfiRead ( - IN EFI_OPEN_FILE *File, - OUT VOID *Buffer, - OUT UINTN *BufferSize - ) -{ - EFI_STATUS Status; - UINT32 AuthenticationStatus; - EFI_DISK_IO_PROTOCOL *DiskIo; - - if (!FileHandleValid (File)) { - return EFI_INVALID_PARAMETER; - } - - // Don't read past the end of the file. - if ((File->CurrentPosition + *BufferSize) > File->MaxPosition) { - return EFI_END_OF_FILE; - } - - switch (File->Type) { - case EfiOpenLoadFile: - // Figure out the File->Size - EfiTell (File, NULL); - - Status = File->LoadFile->LoadFile (File->LoadFile, File->DevicePath, FALSE, BufferSize, Buffer); - break; - - case EfiOpenFirmwareVolume: - if (CompareGuid (&File->FvNameGuid, &gZeroGuid)) { - // This is the entire FV device, so treat like a memory buffer - CopyMem (Buffer, (VOID *)(UINTN)(File->FvStart + File->CurrentPosition), *BufferSize); - File->CurrentPosition += *BufferSize; - Status = EFI_SUCCESS; - } else { - if (File->Buffer == NULL) { - if (File->FvSectionType == EFI_SECTION_ALL) { - Status = File->Fv->ReadFile ( - File->Fv, - &File->FvNameGuid, - (VOID **)&File->Buffer, - &File->Size, - &File->FvType, - &File->FvAttributes, - &AuthenticationStatus - ); - } else { - Status = File->Fv->ReadSection ( - File->Fv, - &File->FvNameGuid, - File->FvSectionType, - 0, - (VOID **)&File->Buffer, - &File->Size, - &AuthenticationStatus - ); - } - if (EFI_ERROR (Status)) { - return Status; - } - File->IsBufferValid = TRUE; - } - // Operate on the cached buffer so Seek will work - CopyMem (Buffer, File->Buffer + File->CurrentPosition, *BufferSize); - File->CurrentPosition += *BufferSize; - Status = EFI_SUCCESS; - } - break; - - case EfiOpenMemoryBuffer: - CopyMem (Buffer, File->Buffer + File->CurrentPosition, *BufferSize); - File->CurrentPosition += *BufferSize; - Status = EFI_SUCCESS; - break; - - case EfiOpenFileSystem: - Status = File->FsFileHandle->Read (File->FsFileHandle, BufferSize, Buffer); - File->CurrentPosition += *BufferSize; - break; - - case EfiOpenBlockIo: - Status = gBS->HandleProtocol(File->EfiHandle, &gEfiDiskIoProtocolGuid, (VOID **)&DiskIo); - if (!EFI_ERROR(Status)) { - Status = DiskIo->ReadDisk(DiskIo, File->FsBlockIoMedia->MediaId, File->DiskOffset + File->CurrentPosition, *BufferSize, Buffer); - } - File->CurrentPosition += *BufferSize; - break; - - case EfiOpenTftp: - // Cache the file if it hasn't been cached yet. - if (File->IsBufferValid == FALSE) { - Status = CacheTftpFile (File); - if (EFI_ERROR (Status)) { - return Status; - } - } - - // Copy out the requested data - CopyMem (Buffer, File->Buffer + File->CurrentPosition, *BufferSize); - File->CurrentPosition += *BufferSize; - - Status = EFI_SUCCESS; - break; - - default: - return EFI_INVALID_PARAMETER; - }; - - return Status; -} - - -/** -Read the entire file into a buffer. This routine allocates the buffer and -returns it to the user full of the read data. - -This is very useful for load file where it's hard to know how big the buffer -must be. - -@param Stream Open File Handle -@param Buffer Pointer to buffer to return. -@param BufferSize Pointer to Size of buffer return.. - - -@return EFI_SUCCESS Stream is not an Open File -@return EFI_END_OF_FILE Tried to read past the end of the file -@return EFI_INVALID_PARAMETER Stream is not an open file handle -@return EFI_BUFFER_TOO_SMALL Buffer is not big enough to do the read -@return "other" Error returned from device read - -**/ -EFI_STATUS -EfiReadAllocatePool ( - IN EFI_OPEN_FILE *File, - OUT VOID **Buffer, - OUT UINTN *BufferSize - ) -{ - if (!FileHandleValid (File)) { - return EFI_INVALID_PARAMETER; - } - - // Loadfile defers file size determination on Open so use tell to find it - EfiTell (File, NULL); - - *BufferSize = File->Size; - *Buffer = AllocatePool (*BufferSize); - if (*Buffer == NULL) { - return EFI_NOT_FOUND; - } - - return EfiRead (File, *Buffer, BufferSize); -} - - -/** -Write data back to the file. For TFTP case you must write the entire file. - -@param Stream Open File Handle -@param Buffer Pointer to buffer to return. -@param BufferSize Pointer to Size of buffer return.. - - -@return EFI_SUCCESS Stream is not an Open File -@return EFI_END_OF_FILE Tried to read past the end of the file -@return EFI_INVALID_PARAMETER Stream is not an open file handle -@return EFI_BUFFER_TOO_SMALL Buffer is not big enough to do the read -@return "other" Error returned from device write - -**/ -EFI_STATUS -EfiWrite ( - IN EFI_OPEN_FILE *File, - OUT VOID *Buffer, - OUT UINTN *BufferSize - ) -{ - EFI_STATUS Status; - EFI_FV_WRITE_FILE_DATA FileData; - EFI_DISK_IO_PROTOCOL *DiskIo; - - if (!FileHandleValid (File)) { - return EFI_INVALID_PARAMETER; - } - - switch (File->Type) { - case EfiOpenMemoryBuffer: - if ((File->CurrentPosition + *BufferSize) > File->MaxPosition) { - return EFI_END_OF_FILE; - } - - CopyMem (File->Buffer + File->CurrentPosition, Buffer, *BufferSize); - File->CurrentPosition += *BufferSize; - Status = EFI_SUCCESS; - - case EfiOpenLoadFile: - // LoadFile device is read only be definition - Status = EFI_UNSUPPORTED; - - case EfiOpenFirmwareVolume: - if (File->FvSectionType != EFI_SECTION_ALL) { - // Writes not support to a specific section. You have to update entire file - return EFI_UNSUPPORTED; - } - - FileData.NameGuid = &(File->FvNameGuid); - FileData.Type = File->FvType; - FileData.FileAttributes = File->FvAttributes; - FileData.Buffer = Buffer; - FileData.BufferSize = (UINT32)*BufferSize; - Status = File->Fv->WriteFile (File->Fv, 1, EFI_FV_UNRELIABLE_WRITE, &FileData); - break; - - case EfiOpenFileSystem: - Status = File->FsFileHandle->Write (File->FsFileHandle, BufferSize, Buffer); - File->CurrentPosition += *BufferSize; - break; - - case EfiOpenBlockIo: - if ((File->CurrentPosition + *BufferSize) > File->MaxPosition) { - return EFI_END_OF_FILE; - } - - Status = gBS->HandleProtocol (File->EfiHandle, &gEfiDiskIoProtocolGuid, (VOID **)&DiskIo); - if (!EFI_ERROR(Status)) { - Status = DiskIo->WriteDisk (DiskIo, File->FsBlockIoMedia->MediaId, File->DiskOffset + File->CurrentPosition, *BufferSize, Buffer); - } - File->CurrentPosition += *BufferSize; - break; - - case EfiOpenTftp: - // Cache the file if it hasn't been cached yet. - if (File->IsBufferValid == FALSE) { - Status = CacheTftpFile(File); - if (EFI_ERROR(Status)) { - return Status; - } - } - - // Don't overwrite the buffer - if ((File->CurrentPosition + *BufferSize) > File->MaxPosition) { - UINT8 *TempBuffer; - - TempBuffer = File->Buffer; - - File->Buffer = AllocatePool ((UINTN)(File->CurrentPosition + *BufferSize)); - if (File->Buffer == NULL) { - return EFI_OUT_OF_RESOURCES; - } - - CopyMem (File->Buffer, TempBuffer, File->Size); - - FreePool (TempBuffer); - - File->Size = (UINTN)(File->CurrentPosition + *BufferSize); - File->MaxPosition = (UINT64)File->Size; - } - - // Copy in the requested data - CopyMem (File->Buffer + File->CurrentPosition, Buffer, *BufferSize); - File->CurrentPosition += *BufferSize; - - // Mark the file dirty - File->IsDirty = TRUE; - - Status = EFI_SUCCESS; - break; - - default: - Status = EFI_INVALID_PARAMETER; - }; - - return Status; -} - - -/** -Given Cwd expand Path to remove .. and replace them with real -directory names. - -@param Cwd Current Working Directory -@param Path Path to expand - -@return NULL Cwd or Path are not valid -@return 'other' Path with .. expanded - -**/ -CHAR8 * -ExpandPath ( - IN CHAR8 *Cwd, - IN CHAR8 *Path - ) -{ - CHAR8 *NewPath; - CHAR8 *Work, *Start, *End; - UINTN StrLen; - INTN i; - - if (Cwd == NULL || Path == NULL) { - return NULL; - } - - StrLen = AsciiStrSize (Cwd); - if (StrLen <= 2) { - // Smallest valid path is 1 char and a null - return NULL; - } - - StrLen = AsciiStrSize (Path); - NewPath = AllocatePool (AsciiStrSize (Cwd) + StrLen + 1); - if (NewPath == NULL) { - return NULL; - } - AsciiStrCpy (NewPath, Cwd); - - End = Path + StrLen; - for (Start = Path ;;) { - Work = AsciiStrStr (Start, "..") ; - if (Work == NULL) { - // Remaining part of Path contains no more .. - break; - } - - // append path prior to .. - AsciiStrnCat (NewPath, Start, Work - Start); - StrLen = AsciiStrLen (NewPath); - for (i = StrLen; i >= 0; i--) { - if (NewPath[i] == ':') { - // too many .. - return NULL; - } - if (NewPath[i] == '/' || NewPath[i] == '\\') { - if ((i > 0) && (NewPath[i-1] == ':')) { - // leave the / before a : - NewPath[i+1] = '\0'; - } else { - // replace / will Null to remove trailing file/dir reference - NewPath[i] = '\0'; - } - break; - } - } - - Start = Work + 3; - } - - // Handle the path that remains after the .. - AsciiStrnCat (NewPath, Start, End - Start); - - return NewPath; -} - - -/** -Set the Current Working Directory (CWD). If a call is made to EfiOpen () and -the path does not contain a device name, The CWD is prepended to the path. - -@param Cwd Current Working Directory to set - - -@return EFI_SUCCESS CWD is set -@return EFI_INVALID_PARAMETER Cwd is not a valid device:path - -**/ -EFI_STATUS -EfiSetCwd ( - IN CHAR8 *Cwd - ) -{ - EFI_OPEN_FILE *File; - UINTN Len; - CHAR8 *Path; - - if (Cwd == NULL) { - return EFI_INVALID_PARAMETER; - } - - if (AsciiStrCmp (Cwd, ".") == 0) { - // cd . is a no-op - return EFI_SUCCESS; - } - - Path = Cwd; - if (AsciiStrStr (Cwd, "..") != NULL) { - if (gCwd == NULL) { - // no parent - return EFI_SUCCESS; - } - - Len = AsciiStrLen (gCwd); - if ((gCwd[Len-2] == ':') && ((gCwd[Len-1] == '/') || (gCwd[Len-1] == '\\'))) { - // parent is device so nothing to do - return EFI_SUCCESS; - } - - // Expand .. in Cwd, given we know current working directory - Path = ExpandPath (gCwd, Cwd); - if (Path == NULL) { - return EFI_NOT_FOUND; - } - } - - File = EfiOpen (Path, EFI_FILE_MODE_READ, 0); - if (File == NULL) { - return EFI_INVALID_PARAMETER; - } - - if (gCwd != NULL) { - FreePool (gCwd); - } - - // Use the info returned from EfiOpen as it can add in CWD if needed. So Cwd could be - // relative to the current gCwd or not. - gCwd = AllocatePool (AsciiStrSize (File->DeviceName) + AsciiStrSize (File->FileName) + 10); - if (gCwd == NULL) { - return EFI_INVALID_PARAMETER; - } - - AsciiStrCpy (gCwd, File->DeviceName); - if (File->FileName == NULL) { - AsciiStrCat (gCwd, ":\\"); - } else { - AsciiStrCat (gCwd, ":"); - AsciiStrCat (gCwd, File->FileName); - } - - - EfiClose (File); - if (Path != Cwd) { - FreePool (Path); - } - return EFI_SUCCESS; -} - - -/** -Set the Current Working Directory (CWD). If a call is made to EfiOpen () and -the path does not contain a device name, The CWD is prepended to the path. -The CWD buffer is only valid until a new call is made to EfiSetCwd(). After -a call to EfiSetCwd() it is not legal to use the pointer returned by -this function. - -@param Cwd Current Working Directory - - -@return "" No CWD set -@return 'other' Returns buffer that contains CWD. - -**/ -CHAR8 * -EfiGetCwd ( - VOID - ) -{ - if (gCwd == NULL) { - return ""; - } - return gCwd; -} - - +/** @file
+File IO routines inspired by Streams with an EFI flavor
+
+Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
+Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
+
+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.
+
+Basic support for opening files on different device types. The device string
+is in the form of DevType:Path. Current DevType is required as there is no
+current mounted device concept of current working directory concept implement
+by this library.
+
+Device names are case insensitive and only check the leading characters for
+unique matches. Thus the following are all the same:
+LoadFile0:
+l0:
+L0:
+Lo0:
+
+Supported Device Names:
+A0x1234:0x12 - A memory buffer starting at address 0x1234 for 0x12 bytes
+l1: - EFI LoadFile device one.
+B0: - EFI BlockIo zero.
+fs3: - EFI Simple File System device 3
+Fv2: - EFI Firmware VOlume device 2
+10.0.1.102: - TFTP service IP followed by the file name
+**/
+
+#include <PiDxe.h>
+#include <Protocol/BlockIo.h>
+#include <Protocol/DiskIo.h>
+#include <Protocol/SimpleFileSystem.h>
+#include <Protocol/FirmwareVolume2.h>
+#include <Protocol/LoadFile.h>
+#include <Protocol/FirmwareVolumeBlock.h>
+#include <Guid/FileInfo.h>
+#include <Library/BaseLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/DevicePathLib.h>
+#include <Library/PrintLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/UefiLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiRuntimeServicesTableLib.h>
+#include <Library/DebugLib.h>
+#include <Library/EfiFileLib.h>
+#include <Library/PcdLib.h>
+#include <Library/EblNetworkLib.h>
+
+
+CHAR8 *gCwd = NULL;
+
+CONST EFI_GUID gZeroGuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
+
+#define EFI_OPEN_FILE_GUARD_HEADER 0x4B4D4641
+#define EFI_OPEN_FILE_GUARD_FOOTER 0x444D5A56
+
+// Need to defend against this overflowing
+#define MAX_CMD_LINE 0x200
+
+typedef struct {
+ UINT32 Header;
+ EFI_OPEN_FILE File;
+ UINT32 Footer;
+} EFI_OPEN_FILE_GUARD;
+
+
+// globals to store current open device info
+EFI_HANDLE *mBlkIo = NULL;
+UINTN mBlkIoCount = 0;
+
+EFI_HANDLE *mFs = NULL;
+UINTN mFsCount = 0;
+// mFsInfo[] array entries must match mFs[] handles
+EFI_FILE_SYSTEM_INFO **mFsInfo = NULL;
+
+EFI_HANDLE *mFv = NULL;
+UINTN mFvCount = 0;
+EFI_HANDLE *mLoadFile = NULL;
+UINTN mLoadFileCount = 0;
+
+
+
+/**
+Internal worker function to validate a File handle.
+
+@param File Open File Handle
+
+@return TRUE File is valid
+@return FALSE File is not valid
+
+
+**/
+BOOLEAN
+FileHandleValid (
+ IN EFI_OPEN_FILE *File
+ )
+{
+ EFI_OPEN_FILE_GUARD *GuardFile;
+
+ // Look right before and after file structure for the correct signatures
+ GuardFile = BASE_CR (File, EFI_OPEN_FILE_GUARD, File);
+ if ((GuardFile->Header != EFI_OPEN_FILE_GUARD_HEADER) ||
+ (GuardFile->Footer != EFI_OPEN_FILE_GUARD_FOOTER) ) {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/**
+Internal worker function. If Buffer is not NULL free it.
+
+@param Buffer Buffer to FreePool()
+
+**/
+VOID
+EblFreePool (
+ IN VOID *Buffer
+ )
+{
+ if (Buffer != NULL) {
+ FreePool (Buffer);
+ }
+}
+
+/**
+Update Device List Global Variables
+
+**/
+VOID
+EblUpdateDeviceLists (
+ VOID
+ )
+{
+ EFI_STATUS Status;
+ UINTN Size;
+ EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs;
+ EFI_FILE_HANDLE Root;
+ UINTN Index;
+
+ if (mBlkIo != NULL) {
+ FreePool (mBlkIo);
+ }
+ gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &mBlkIoCount, &mBlkIo);
+
+
+
+ if (mFv != NULL) {
+ FreePool (mFv);
+ }
+ gBS->LocateHandleBuffer (ByProtocol, &gEfiFirmwareVolume2ProtocolGuid, NULL, &mFvCount, &mFv);
+
+ if (mLoadFile != NULL) {
+ FreePool (mLoadFile);
+ }
+ gBS->LocateHandleBuffer (ByProtocol, &gEfiLoadFileProtocolGuid, NULL, &mLoadFileCount, &mLoadFile);
+
+ if (mFs != NULL) {
+ FreePool (mFs);
+ }
+
+ if (&mFsInfo[0] != NULL) {
+ // Need to Free the mFsInfo prior to recalculating mFsCount so don't move this code
+ for (Index = 0; Index < mFsCount; Index++) {
+ if (mFsInfo[Index] != NULL) {
+ FreePool (mFsInfo[Index]);
+ }
+ }
+ FreePool (mFsInfo);
+ }
+
+ gBS->LocateHandleBuffer (ByProtocol, &gEfiSimpleFileSystemProtocolGuid, NULL, &mFsCount, &mFs);
+
+
+ mFsInfo = AllocateZeroPool (mFsCount * sizeof (EFI_FILE_SYSTEM_INFO *));
+ if (mFsInfo == NULL) {
+ // If we can't do this then we can't support file system entries
+ mFsCount = 0;
+ } else {
+ // Loop through all the file system structures and cache the file system info data
+ for (Index =0; Index < mFsCount; Index++) {
+ Status = gBS->HandleProtocol (mFs[Index], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&Fs);
+ if (!EFI_ERROR (Status)) {
+ Status = Fs->OpenVolume (Fs, &Root);
+ if (!EFI_ERROR (Status)) {
+ // Get information about the volume
+ Size = 0;
+ Status = Root->GetInfo (Root, &gEfiFileSystemInfoGuid, &Size, mFsInfo[Index]);
+ if (Status == EFI_BUFFER_TOO_SMALL) {
+ mFsInfo[Index] = AllocatePool (Size);
+ Status = Root->GetInfo (Root, &gEfiFileSystemInfoGuid, &Size, mFsInfo[Index]);
+ }
+
+ Root->Close (Root);
+ }
+ }
+ }
+ }
+}
+
+
+/**
+PathName is in the form <device name>:<path> for example fs1:\ or ROOT:\.
+Return TRUE if the <devce name> prefix of PathName matches a file system
+Volume Name. MatchIndex is the array index in mFsInfo[] of the match,
+and it can be used with mFs[] to find the handle that needs to be opened
+
+@param PathName PathName to check
+@param FileStart Index of the first character of the <path>
+@param MatchIndex Index in mFsInfo[] that matches
+
+@return TRUE PathName matches a Volume Label and MatchIndex is valid
+@return FALSE PathName does not match a Volume Label MatchIndex undefined
+
+**/
+BOOLEAN
+EblMatchVolumeName (
+ IN CHAR8 *PathName,
+ IN UINTN FileStart,
+ OUT UINTN *MatchIndex
+ )
+{
+ UINTN Index;
+ UINTN Compare;
+ UINTN VolStrLen;
+ BOOLEAN Match;
+
+ for (Index =0; Index < mFsCount; Index++) {
+ if (mFsInfo[Index] == NULL) {
+ // FsInfo is not valid so skip it
+ continue;
+ }
+ VolStrLen = StrLen (mFsInfo[Index]->VolumeLabel);
+ for (Compare = 0, Match = TRUE; Compare < (FileStart - 1); Compare++) {
+ if (Compare > VolStrLen) {
+ Match = FALSE;
+ break;
+ }
+ if (PathName[Compare] != (CHAR8)mFsInfo[Index]->VolumeLabel[Compare]) {
+ // If the VolumeLabel has a space allow a _ to match with it in addition to ' '
+ if (!((PathName[Compare] == '_') && (mFsInfo[Index]->VolumeLabel[Compare] == L' '))) {
+ Match = FALSE;
+ break;
+ }
+ }
+ }
+ if (Match) {
+ *MatchIndex = Index;
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+
+/**
+Return the number of devices of the current type active in the system
+
+@param Type Device type to check
+
+@return 0 Invalid type
+
+**/
+UINTN
+EfiGetDeviceCounts (
+ IN EFI_OPEN_FILE_TYPE DeviceType
+ )
+{
+ switch (DeviceType) {
+ case EfiOpenLoadFile:
+ return mLoadFileCount;
+ case EfiOpenFirmwareVolume:
+ return mFvCount;
+ case EfiOpenFileSystem:
+ return mFsCount;
+ case EfiOpenBlockIo:
+ return mBlkIoCount;
+ default:
+ return 0;
+ }
+}
+
+EFI_STATUS
+ConvertIpStringToEfiIp (
+ IN CHAR8 *PathName,
+ OUT EFI_IP_ADDRESS *ServerIp
+ )
+{
+ CHAR8 *Str;
+
+ Str = PathName;
+ ServerIp->v4.Addr[0] = (UINT8)AsciiStrDecimalToUintn (Str);
+
+ Str = AsciiStrStr (Str, ".");
+ if (Str == NULL) {
+ return EFI_DEVICE_ERROR;
+ }
+
+ ServerIp->v4.Addr[1] = (UINT8)AsciiStrDecimalToUintn (++Str);
+
+ Str = AsciiStrStr (Str, ".");
+ if (Str == NULL) {
+ return EFI_DEVICE_ERROR;
+ }
+
+ ServerIp->v4.Addr[2] = (UINT8)AsciiStrDecimalToUintn (++Str);
+
+ Str = AsciiStrStr (Str, ".");
+ if (Str == NULL) {
+ return EFI_DEVICE_ERROR;
+ }
+
+ ServerIp->v4.Addr[3] = (UINT8)AsciiStrDecimalToUintn (++Str);
+
+ return EFI_SUCCESS;
+}
+
+
+/**
+Internal work function to extract a device number from a string skipping
+text. Easy way to extract numbers from strings like blk7:.
+
+@param Str String to extract device number form
+
+@return -1 Device string is not valid
+@return Device #
+
+**/
+UINTN
+EblConvertDevStringToNumber (
+ IN CHAR8 *Str
+ )
+{
+ UINTN Max;
+ UINTN Index;
+
+
+ // Find the first digit
+ Max = AsciiStrLen (Str);
+ for (Index = 0; !((*Str >= '0') && (*Str <= '9')) && (Index < Max); Index++) {
+ Str++;
+ }
+ if (Index == Max) {
+ return (UINTN)-1;
+ }
+
+ return AsciiStrDecimalToUintn (Str);
+}
+
+
+/**
+Internal work function to fill in EFI_OPEN_FILE information for the Fs and BlkIo
+
+@param File Open file handle
+@param FileName Name of file after device stripped off
+
+
+**/
+EFI_STATUS
+EblFileDevicePath (
+ IN OUT EFI_OPEN_FILE *File,
+ IN CHAR8 *FileName,
+ IN CONST UINT64 OpenMode
+ )
+{
+ EFI_STATUS Status;
+ UINTN Size;
+ FILEPATH_DEVICE_PATH *FilePath;
+ EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;
+ CHAR16 UnicodeFileName[MAX_PATHNAME];
+ EFI_BLOCK_IO_PROTOCOL *BlkIo;
+ EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs;
+ EFI_FILE_HANDLE Root;
+
+
+ if ( *FileName != 0 ) {
+ AsciiStrToUnicodeStr (FileName, UnicodeFileName);
+ } else {
+ AsciiStrToUnicodeStr ("\\", UnicodeFileName);
+ }
+
+ Size = StrSize (UnicodeFileName);
+ FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + sizeof (EFI_DEVICE_PATH_PROTOCOL));
+ if (FileDevicePath != NULL) {
+ FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;
+ FilePath->Header.Type = MEDIA_DEVICE_PATH;
+ FilePath->Header.SubType = MEDIA_FILEPATH_DP;
+ CopyMem (&FilePath->PathName, UnicodeFileName, Size);
+ SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);
+ SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));
+
+ if (File->EfiHandle != NULL) {
+ File->DevicePath = DevicePathFromHandle (File->EfiHandle);
+ }
+
+ File->DevicePath = AppendDevicePath (File->DevicePath, FileDevicePath);
+ FreePool (FileDevicePath);
+ }
+
+ Status = gBS->HandleProtocol (File->EfiHandle, &gEfiBlockIoProtocolGuid, (VOID **)&BlkIo);
+ if (!EFI_ERROR (Status)) {
+ File->FsBlockIoMedia = BlkIo->Media;
+ File->FsBlockIo = BlkIo;
+
+ // If we are not opening the device this will get over written with file info
+ File->MaxPosition = MultU64x32 (BlkIo->Media->LastBlock + 1, BlkIo->Media->BlockSize);
+ }
+
+ if (File->Type == EfiOpenFileSystem) {
+ Status = gBS->HandleProtocol (File->EfiHandle, &gEfiSimpleFileSystemProtocolGuid, (VOID **)&Fs);
+ if (!EFI_ERROR (Status)) {
+ Status = Fs->OpenVolume (Fs, &Root);
+ if (!EFI_ERROR (Status)) {
+ // Get information about the volume
+ Size = 0;
+ Status = Root->GetInfo (Root, &gEfiFileSystemInfoGuid, &Size, File->FsInfo);
+ if (Status == EFI_BUFFER_TOO_SMALL) {
+ File->FsInfo = AllocatePool (Size);
+ Status = Root->GetInfo (Root, &gEfiFileSystemInfoGuid, &Size, File->FsInfo);
+ }
+
+ // Get information about the file
+ Status = Root->Open (Root, &File->FsFileHandle, UnicodeFileName, OpenMode, 0);
+ if (!EFI_ERROR (Status)) {
+ Size = 0;
+ Status = File->FsFileHandle->GetInfo (File->FsFileHandle, &gEfiFileInfoGuid, &Size, NULL);
+ if (Status == EFI_BUFFER_TOO_SMALL) {
+ File->FsFileInfo = AllocatePool (Size);
+ Status = File->FsFileHandle->GetInfo (File->FsFileHandle, &gEfiFileInfoGuid, &Size, File->FsFileInfo);
+ if (!EFI_ERROR (Status)) {
+ File->Size = (UINTN)File->FsFileInfo->FileSize;
+ File->MaxPosition = (UINT64)File->Size;
+ }
+ }
+ }
+
+ Root->Close (Root);
+ }
+ }
+ } else if (File->Type == EfiOpenBlockIo) {
+ File->Size = (UINTN)File->MaxPosition;
+ }
+
+ return Status;
+}
+
+#define ToUpper(a) ((((a) >= 'a') && ((a) <= 'z')) ? ((a) - 'a' + 'A') : (a))
+
+EFI_STATUS
+CompareGuidToString (
+ IN EFI_GUID *Guid,
+ IN CHAR8 *String
+ )
+{
+ CHAR8 AsciiGuid[64];
+ CHAR8 *StringPtr;
+ CHAR8 *GuidPtr;
+
+ AsciiSPrint (AsciiGuid, sizeof(AsciiGuid), "%g", Guid);
+
+ StringPtr = String;
+ GuidPtr = AsciiGuid;
+
+ while ((*StringPtr != '\0') && (*GuidPtr != '\0')) {
+ // Skip dashes
+ if (*StringPtr == '-') {
+ StringPtr++;
+ continue;
+ }
+
+ if (*GuidPtr == '-') {
+ GuidPtr++;
+ continue;
+ }
+
+ if (ToUpper(*StringPtr) != ToUpper(*GuidPtr)) {
+ return EFI_NOT_FOUND;
+ }
+
+ StringPtr++;
+ GuidPtr++;
+ }
+
+ return EFI_SUCCESS;
+}
+
+
+/**
+Internal work function to fill in EFI_OPEN_FILE information for the FV
+
+@param File Open file handle
+@param FileName Name of file after device stripped off
+
+
+**/
+EFI_STATUS
+EblFvFileDevicePath (
+ IN OUT EFI_OPEN_FILE *File,
+ IN CHAR8 *FileName,
+ IN CONST UINT64 OpenMode
+ )
+{
+ EFI_STATUS Status;
+ EFI_STATUS GetNextFileStatus;
+ MEDIA_FW_VOL_FILEPATH_DEVICE_PATH DevicePathNode;
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;
+ UINTN Key;
+ UINT32 AuthenticationStatus;
+ CHAR8 AsciiSection[MAX_PATHNAME];
+ VOID *Section;
+ UINTN SectionSize;
+ EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
+ EFI_LBA Lba;
+ UINTN BlockSize;
+ UINTN NumberOfBlocks;
+ EFI_FIRMWARE_VOLUME_HEADER *FvHeader = NULL;
+ UINTN Index;
+
+
+ Status = gBS->HandleProtocol (File->EfiHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&File->Fv);
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+
+ // Get FVB Info about the handle
+ Status = gBS->HandleProtocol (File->EfiHandle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **)&Fvb);
+ if (!EFI_ERROR (Status)) {
+ Status = Fvb->GetPhysicalAddress (Fvb, &File->FvStart);
+ if (!EFI_ERROR (Status)) {
+ FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)File->FvStart;
+ File->FvHeaderSize = sizeof (EFI_FIRMWARE_VOLUME_HEADER);
+ for (Index = 0; FvHeader->BlockMap[Index].Length !=0; Index++) {
+ File->FvHeaderSize += sizeof (EFI_FV_BLOCK_MAP_ENTRY);
+ }
+
+ for (Lba = 0, File->FvSize = 0, NumberOfBlocks = 0; ; File->FvSize += (BlockSize * NumberOfBlocks), Lba += NumberOfBlocks) {
+ Status = Fvb->GetBlockSize (Fvb, Lba, &BlockSize, &NumberOfBlocks);
+ if (EFI_ERROR (Status)) {
+ break;
+ }
+ }
+ }
+ }
+
+
+ DevicePath = DevicePathFromHandle (File->EfiHandle);
+
+ if (*FileName == '\0') {
+ File->DevicePath = DuplicateDevicePath (DevicePath);
+ File->Size = File->FvSize;
+ File->MaxPosition = File->Size;
+ } else {
+ Key = 0;
+ do {
+ File->FvType = EFI_FV_FILETYPE_ALL;
+ GetNextFileStatus = File->Fv->GetNextFile (
+ File->Fv,
+ &Key,
+ &File->FvType,
+ &File->FvNameGuid,
+ &File->FvAttributes,
+ &File->Size
+ );
+ if (!EFI_ERROR (GetNextFileStatus)) {
+ // Compare GUID first
+ Status = CompareGuidToString (&File->FvNameGuid, FileName);
+ if (!EFI_ERROR(Status)) {
+ break;
+ }
+
+ Section = NULL;
+ Status = File->Fv->ReadSection (
+ File->Fv,
+ &File->FvNameGuid,
+ EFI_SECTION_USER_INTERFACE,
+ 0,
+ &Section,
+ &SectionSize,
+ &AuthenticationStatus
+ );
+ if (!EFI_ERROR (Status)) {
+ UnicodeStrToAsciiStr (Section, AsciiSection);
+ if (AsciiStriCmp (FileName, AsciiSection) == 0) {
+ FreePool (Section);
+ break;
+ }
+ FreePool (Section);
+ }
+ }
+ } while (!EFI_ERROR (GetNextFileStatus));
+
+ if (EFI_ERROR (GetNextFileStatus)) {
+ return GetNextFileStatus;
+ }
+
+ if (OpenMode != EFI_SECTION_ALL) {
+ // Calculate the size of the section we are targeting
+ Section = NULL;
+ File->Size = 0;
+ Status = File->Fv->ReadSection (
+ File->Fv,
+ &File->FvNameGuid,
+ (EFI_SECTION_TYPE)OpenMode,
+ 0,
+ &Section,
+ &File->Size,
+ &AuthenticationStatus
+ );
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+ }
+
+ File->MaxPosition = File->Size;
+ EfiInitializeFwVolDevicepathNode (&DevicePathNode, &File->FvNameGuid);
+ File->DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&DevicePathNode);
+ }
+
+
+ // FVB not required if FV was soft loaded...
+ return EFI_SUCCESS;
+}
+
+
+
+
+/**
+Open a device named by PathName. The PathName includes a device name and
+path separated by a :. See file header for more details on the PathName
+syntax. There is no checking to prevent a file from being opened more than
+one type.
+
+SectionType is only used to open an FV. Each file in an FV contains multiple
+sections and only the SectionType section is opened.
+
+For any file that is opened with EfiOpen() must be closed with EfiClose().
+
+@param PathName Path to parse to open
+@param OpenMode Same as EFI_FILE.Open()
+@param SectionType Section in FV to open.
+
+@return NULL Open failed
+@return Valid EFI_OPEN_FILE handle
+
+**/
+EFI_OPEN_FILE *
+EfiOpen (
+ IN CHAR8 *PathName,
+ IN CONST UINT64 OpenMode,
+ IN CONST EFI_SECTION_TYPE SectionType
+ )
+{
+ EFI_STATUS Status;
+ EFI_OPEN_FILE *File;
+ EFI_OPEN_FILE FileData;
+ UINTN StrLen;
+ UINTN FileStart;
+ UINTN DevNumber = 0;
+ EFI_OPEN_FILE_GUARD *GuardFile;
+ BOOLEAN VolumeNameMatch;
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;
+ UINTN Size;
+ EFI_IP_ADDRESS Ip;
+ CHAR8 *CwdPlusPathName;
+ UINTN Index;
+ EFI_SECTION_TYPE ModifiedSectionType;
+
+ EblUpdateDeviceLists ();
+
+ File = &FileData;
+ ZeroMem (File, sizeof (EFI_OPEN_FILE));
+
+ StrLen = AsciiStrSize (PathName);
+ if (StrLen <= 1) {
+ // Smallest valid path is 1 char and a null
+ return NULL;
+ }
+
+ for (FileStart = 0; FileStart < StrLen; FileStart++) {
+ if (PathName[FileStart] == ':') {
+ FileStart++;
+ break;
+ }
+ }
+
+ //
+ // Matching volume name has precedence over handle based names
+ //
+ VolumeNameMatch = EblMatchVolumeName (PathName, FileStart, &DevNumber);
+ if (!VolumeNameMatch) {
+ if (FileStart == StrLen) {
+ // No Volume name or device name, so try Current Working Directory
+ if (gCwd == NULL) {
+ // No CWD
+ return NULL;
+ }
+
+ // We could add a current working directory concept
+ CwdPlusPathName = AllocatePool (AsciiStrSize (gCwd) + AsciiStrSize (PathName));
+ if (CwdPlusPathName == NULL) {
+ return NULL;
+ }
+
+ if ((PathName[0] == '/') || (PathName[0] == '\\')) {
+ // PathName starts in / so this means we go to the root of the device in the CWD.
+ CwdPlusPathName[0] = '\0';
+ for (FileStart = 0; gCwd[FileStart] != '\0'; FileStart++) {
+ CwdPlusPathName[FileStart] = gCwd[FileStart];
+ if (gCwd[FileStart] == ':') {
+ FileStart++;
+ CwdPlusPathName[FileStart] = '\0';
+ break;
+ }
+ }
+ } else {
+ AsciiStrCpy (CwdPlusPathName, gCwd);
+ StrLen = AsciiStrLen (gCwd);
+ if ((*PathName != '/') && (*PathName != '\\') && (gCwd[StrLen-1] != '/') && (gCwd[StrLen-1] != '\\')) {
+ AsciiStrCat (CwdPlusPathName, "\\");
+ }
+ }
+
+ AsciiStrCat (CwdPlusPathName, PathName);
+ if (AsciiStrStr (CwdPlusPathName, ":") == NULL) {
+ // Extra error check to make sure we don't recurse and blow stack
+ return NULL;
+ }
+
+ File = EfiOpen (CwdPlusPathName, OpenMode, SectionType);
+ FreePool (CwdPlusPathName);
+ return File;
+ }
+
+ DevNumber = EblConvertDevStringToNumber ((CHAR8 *)PathName);
+ }
+
+ File->DeviceName = AllocatePool (StrLen);
+ AsciiStrCpy (File->DeviceName, PathName);
+ File->DeviceName[FileStart - 1] = '\0';
+ File->FileName = &File->DeviceName[FileStart];
+ if (File->FileName[0] == '\0') {
+ // if it is just a file name use / as root
+ File->FileName = "\\";
+ }
+
+ //
+ // Use best match algorithm on the dev names so we only need to look at the
+ // first few charters to match the full device name. Short name forms are
+ // legal from the caller.
+ //
+ Status = EFI_SUCCESS;
+ if (*PathName == 'f' || *PathName == 'F' || VolumeNameMatch) {
+ if (PathName[1] == 's' || PathName[1] == 'S' || VolumeNameMatch) {
+ if (DevNumber >= mFsCount) {
+ goto ErrorExit;
+ }
+ File->Type = EfiOpenFileSystem;
+ File->EfiHandle = mFs[DevNumber];
+ Status = EblFileDevicePath (File, &PathName[FileStart], OpenMode);
+
+ } else if (PathName[1] == 'v' || PathName[1] == 'V') {
+ if (DevNumber >= mFvCount) {
+ goto ErrorExit;
+ }
+ File->Type = EfiOpenFirmwareVolume;
+ File->EfiHandle = mFv[DevNumber];
+
+ if ((PathName[FileStart] == '/') || (PathName[FileStart] == '\\')) {
+ // Skip leading / as its not really needed for the FV since no directories are supported
+ FileStart++;
+ }
+
+ // Check for 2nd :
+ ModifiedSectionType = SectionType;
+ for (Index = FileStart; PathName[Index] != '\0'; Index++) {
+ if (PathName[Index] == ':') {
+ // Support fv0:\DxeCore:0x10
+ // This means open the PE32 Section of the file
+ ModifiedSectionType = (EFI_SECTION_TYPE)AsciiStrHexToUintn (&PathName[Index + 1]);
+ PathName[Index] = '\0';
+ }
+ }
+ File->FvSectionType = ModifiedSectionType;
+ Status = EblFvFileDevicePath (File, &PathName[FileStart], ModifiedSectionType);
+ }
+ } else if ((*PathName == 'A') || (*PathName == 'a')) {
+ // Handle a:0x10000000:0x1234 address form a:ADDRESS:SIZE
+ File->Type = EfiOpenMemoryBuffer;
+ // 1st colon is at PathName[FileStart - 1]
+ File->Buffer = (VOID *)AsciiStrHexToUintn (&PathName[FileStart]);
+
+ // Find 2nd colon
+ while ((PathName[FileStart] != ':') && (PathName[FileStart] != '\0')) {
+ FileStart++;
+ }
+
+ // If we ran out of string, there's no extra data
+ if (PathName[FileStart] == '\0') {
+ File->Size = 0;
+ } else {
+ File->Size = AsciiStrHexToUintn (&PathName[FileStart + 1]);
+ }
+
+ // if there's no number after the second colon, default
+ // the end of memory
+ if (File->Size == 0) {
+ File->Size = (UINTN)(0 - (UINTN)File->Buffer);
+ }
+
+ File->MaxPosition = File->Size;
+ File->BaseOffset = (UINTN)File->Buffer;
+
+ } else if (*PathName== 'l' || *PathName == 'L') {
+ if (DevNumber >= mLoadFileCount) {
+ goto ErrorExit;
+ }
+ File->Type = EfiOpenLoadFile;
+ File->EfiHandle = mLoadFile[DevNumber];
+
+ Status = gBS->HandleProtocol (File->EfiHandle, &gEfiLoadFileProtocolGuid, (VOID **)&File->LoadFile);
+ if (EFI_ERROR (Status)) {
+ goto ErrorExit;
+ }
+
+ Status = gBS->HandleProtocol (File->EfiHandle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePath);
+ if (EFI_ERROR (Status)) {
+ goto ErrorExit;
+ }
+ File->DevicePath = DuplicateDevicePath (DevicePath);
+
+ } else if (*PathName == 'b' || *PathName == 'B') {
+ // Handle b#:0x10000000:0x1234 address form b#:ADDRESS:SIZE
+ if (DevNumber >= mBlkIoCount) {
+ goto ErrorExit;
+ }
+ File->Type = EfiOpenBlockIo;
+ File->EfiHandle = mBlkIo[DevNumber];
+ EblFileDevicePath (File, "", OpenMode);
+
+ // 1st colon is at PathName[FileStart - 1]
+ File->DiskOffset = AsciiStrHexToUintn (&PathName[FileStart]);
+
+ // Find 2nd colon
+ while ((PathName[FileStart] != ':') && (PathName[FileStart] != '\0')) {
+ FileStart++;
+ }
+
+ // If we ran out of string, there's no extra data
+ if (PathName[FileStart] == '\0') {
+ Size = 0;
+ } else {
+ Size = AsciiStrHexToUintn (&PathName[FileStart + 1]);
+ }
+
+ // if a zero size is passed in (or the size is left out entirely),
+ // go to the end of the device.
+ if (Size == 0) {
+ File->Size = File->Size - File->DiskOffset;
+ } else {
+ File->Size = Size;
+ }
+
+ File->MaxPosition = File->Size;
+ File->BaseOffset = File->DiskOffset;
+ } else if ((*PathName) >= '0' && (*PathName <= '9')) {
+
+ // Get current IP address
+ Status = EblGetCurrentIpAddress (&Ip);
+ if (EFI_ERROR(Status)) {
+ AsciiPrint("Device IP Address is not configured.\n");
+ goto ErrorExit;
+ }
+
+
+ // Parse X.X.X.X:Filename, only support IPv4 TFTP for now...
+ File->Type = EfiOpenTftp;
+ File->IsDirty = FALSE;
+ File->IsBufferValid = FALSE;
+
+ Status = ConvertIpStringToEfiIp (PathName, &File->ServerIp);
+ }
+
+ if (EFI_ERROR (Status)) {
+ goto ErrorExit;
+ }
+
+ GuardFile = (EFI_OPEN_FILE_GUARD *)AllocateZeroPool (sizeof (EFI_OPEN_FILE_GUARD));
+ if (GuardFile == NULL) {
+ goto ErrorExit;
+ }
+
+ GuardFile->Header = EFI_OPEN_FILE_GUARD_HEADER;
+ CopyMem (&(GuardFile->File), &FileData, sizeof (EFI_OPEN_FILE));
+ GuardFile->Footer = EFI_OPEN_FILE_GUARD_FOOTER;
+
+ return &(GuardFile->File);
+
+ErrorExit:
+ FreePool (File->DeviceName);
+ return NULL;
+}
+
+#define FILE_COPY_CHUNK 0x01000000
+
+EFI_STATUS
+EfiCopyFile (
+ IN CHAR8 *DestinationFile,
+ IN CHAR8 *SourceFile
+ )
+{
+ EFI_OPEN_FILE *Source = NULL;
+ EFI_OPEN_FILE *Destination = NULL;
+ EFI_STATUS Status = EFI_SUCCESS;
+ VOID *Buffer = NULL;
+ UINTN Size;
+ UINTN Offset;
+ UINTN Chunk = FILE_COPY_CHUNK;
+
+ Source = EfiOpen (SourceFile, EFI_FILE_MODE_READ, 0);
+ if (Source == NULL) {
+ AsciiPrint("Source file open error.\n");
+ Status = EFI_NOT_FOUND;
+ goto Exit;
+ }
+
+ Destination = EfiOpen (DestinationFile, EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);
+ if (Destination == NULL) {
+ AsciiPrint("Destination file open error.\n");
+ Status = EFI_NOT_FOUND;
+ goto Exit;
+ }
+
+ Buffer = AllocatePool(FILE_COPY_CHUNK);
+ if (Buffer == NULL) {
+ Status = EFI_OUT_OF_RESOURCES;
+ goto Exit;
+ }
+
+ Size = EfiTell(Source, NULL);
+
+ for (Offset = 0; Offset + FILE_COPY_CHUNK <= Size; Offset += Chunk) {
+ Chunk = FILE_COPY_CHUNK;
+
+ Status = EfiRead(Source, Buffer, &Chunk);
+ if (EFI_ERROR(Status)) {
+ AsciiPrint("Read file error %r\n", Status);
+ goto Exit;
+ }
+
+ Status = EfiWrite(Destination, Buffer, &Chunk);
+ if (EFI_ERROR(Status)) {
+ AsciiPrint("Write file error %r\n", Status);
+ goto Exit;
+ }
+ }
+
+ // Any left over?
+ if (Offset < Size) {
+ Chunk = Size - Offset;
+
+ Status = EfiRead(Source, Buffer, &Chunk);
+ if (EFI_ERROR(Status)) {
+ AsciiPrint("Read file error\n");
+ goto Exit;
+ }
+
+ Status = EfiWrite(Destination, Buffer, &Chunk);
+ if (EFI_ERROR(Status)) {
+ AsciiPrint("Write file error\n");
+ goto Exit;
+ }
+ }
+
+Exit:
+ if (Source != NULL) {
+ Status = EfiClose(Source);
+ if (EFI_ERROR(Status)) {
+ AsciiPrint("Source close error");
+ }
+ }
+
+ if (Destination != NULL) {
+ Status = EfiClose(Destination);
+ if (EFI_ERROR(Status)) {
+ AsciiPrint("Destination close error");
+ }
+ }
+
+ if (Buffer != NULL) {
+ FreePool(Buffer);
+ }
+
+ return Status;
+}
+
+/**
+Use DeviceType and Index to form a valid PathName and try and open it.
+
+@param DeviceType Device type to open
+@param Index Device Index to use. Zero relative.
+
+@return NULL Open failed
+@return Valid EFI_OPEN_FILE handle
+
+**/
+EFI_OPEN_FILE *
+EfiDeviceOpenByType (
+ IN EFI_OPEN_FILE_TYPE DeviceType,
+ IN UINTN Index
+ )
+{
+ CHAR8 *DevStr;
+ CHAR8 Path[MAX_CMD_LINE];
+
+ switch (DeviceType) {
+ case EfiOpenLoadFile:
+ DevStr = "loadfile%d:";
+ break;
+ case EfiOpenFirmwareVolume:
+ DevStr = "fv%d:";
+ break;
+ case EfiOpenFileSystem:
+ DevStr = "fs%d:";
+ break;
+ case EfiOpenBlockIo:
+ DevStr = "blk%d:";
+ break;
+ case EfiOpenMemoryBuffer:
+ DevStr = "a%d:";
+ break;
+ default:
+ return NULL;
+ }
+
+ AsciiSPrint (Path, MAX_PATHNAME, DevStr, Index);
+
+ return EfiOpen (Path, EFI_FILE_MODE_READ, 0);
+}
+
+
+/**
+Close a file handle opened by EfiOpen() and free all resources allocated by
+EfiOpen().
+
+@param Stream Open File Handle
+
+@return EFI_INVALID_PARAMETER Stream is not an Open File
+@return EFI_SUCCESS Steam closed
+
+**/
+EFI_STATUS
+EfiClose (
+ IN EFI_OPEN_FILE *File
+ )
+{
+ EFI_STATUS Status;
+ UINT64 TftpBufferSize;
+
+ if (!FileHandleValid (File)) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ //Write the buffer contents to TFTP file.
+ if ((File->Type == EfiOpenTftp) && (File->IsDirty)) {
+
+ TftpBufferSize = File->Size;
+ Status = EblMtftp (
+ EFI_PXE_BASE_CODE_TFTP_WRITE_FILE,
+ File->Buffer,
+ TRUE,
+ &TftpBufferSize,
+ NULL,
+ &File->ServerIp,
+ (UINT8 *)File->FileName,
+ NULL,
+ FALSE
+ );
+ if (EFI_ERROR(Status)) {
+ AsciiPrint("TFTP error during APPLE_NSP_TFTP_WRITE_FILE: %r\n", Status);
+ return Status;
+ }
+ }
+
+ if ((File->Type == EfiOpenLoadFile) ||
+ ((File->Type == EfiOpenTftp) && (File->IsBufferValid == TRUE)) ||
+ ((File->Type == EfiOpenFirmwareVolume) && (File->IsBufferValid == TRUE))) {
+ EblFreePool(File->Buffer);
+ }
+
+ EblFreePool (File->DevicePath);
+ EblFreePool (File->DeviceName);
+ EblFreePool (File->FsFileInfo);
+ EblFreePool (File->FsInfo);
+
+ if (File->FsFileHandle != NULL) {
+ File->FsFileHandle->Close (File->FsFileHandle);
+ }
+
+ // Need to free File and it's Guard structures
+ EblFreePool (BASE_CR (File, EFI_OPEN_FILE_GUARD, File));
+ return EFI_SUCCESS;
+}
+
+
+/**
+Return the size of the file represented by Stream. Also return the current
+Seek position. Opening a file will enable a valid file size to be returned.
+LoadFile is an exception as a load file size is set to zero.
+
+@param Stream Open File Handle
+
+@return 0 Stream is not an Open File or a valid LoadFile handle
+
+**/
+UINTN
+EfiTell (
+ IN EFI_OPEN_FILE *File,
+ OUT EFI_LBA *CurrentPosition OPTIONAL
+ )
+{
+ EFI_STATUS Status;
+ UINT64 BufferSize = 0;
+
+ if (!FileHandleValid (File)) {
+ return 0;
+ }
+
+ if (CurrentPosition != NULL) {
+ *CurrentPosition = File->CurrentPosition;
+ }
+
+ if (File->Type == EfiOpenLoadFile) {
+ // Figure out the File->Size
+ File->Buffer = NULL;
+ File->Size = 0;
+ Status = File->LoadFile->LoadFile (File->LoadFile, File->DevicePath, FALSE, &File->Size, File->Buffer);
+ if (Status != EFI_BUFFER_TOO_SMALL) {
+ return 0;
+ }
+
+ File->MaxPosition = (UINT64)File->Size;
+ } else if (File->Type == EfiOpenTftp) {
+
+ Status = EblMtftp (
+ EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE,
+ NULL,
+ FALSE,
+ &BufferSize,
+ NULL,
+ &File->ServerIp,
+ (UINT8 *)File->FileName,
+ NULL,
+ TRUE
+ );
+ if (EFI_ERROR(Status)) {
+ AsciiPrint("TFTP error during APPLE_NSP_TFTP_GET_FILE_SIZE: %r\n", Status);
+ return 0;
+ }
+
+ File->Size = (UINTN)BufferSize;
+ File->MaxPosition = File->Size;
+ }
+
+ return File->Size;
+}
+
+
+/**
+Seek to the Offset location in the file. LoadFile and FV device types do
+not support EfiSeek(). It is not possible to grow the file size using
+EfiSeek().
+
+SeekType defines how use Offset to calculate the new file position:
+EfiSeekStart : Position = Offset
+EfiSeekCurrent: Position is Offset bytes from the current position
+EfiSeekEnd : Only supported if Offset is zero to seek to end of file.
+
+@param Stream Open File Handle
+@param Offset Offset to seek too.
+@param SeekType Type of seek to perform
+
+
+@return EFI_INVALID_PARAMETER Stream is not an Open File
+@return EFI_UNSUPPORTED LoadFile and FV do not support Seek
+@return EFI_NOT_FOUND Seek past the end of the file.
+@return EFI_SUCCESS Steam closed
+
+**/
+EFI_STATUS
+EfiSeek (
+ IN EFI_OPEN_FILE *File,
+ IN EFI_LBA Offset,
+ IN EFI_SEEK_TYPE SeekType
+ )
+{
+ EFI_STATUS Status;
+ UINT64 CurrentPosition;
+
+ if (!FileHandleValid (File)) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ if (File->Type == EfiOpenLoadFile) {
+ // LoadFile does not support Seek
+ return EFI_UNSUPPORTED;
+ }
+
+ CurrentPosition = File->CurrentPosition;
+ switch (SeekType) {
+ case EfiSeekStart:
+ if (Offset > File->MaxPosition) {
+ return EFI_NOT_FOUND;
+ }
+ CurrentPosition = Offset;
+ break;
+
+ case EfiSeekCurrent:
+ if ((File->CurrentPosition + Offset) > File->MaxPosition) {
+ return EFI_NOT_FOUND;
+ }
+ CurrentPosition += Offset;
+ break;
+
+ case EfiSeekEnd:
+ if (Offset != 0) {
+ // We don't support growing file size via seeking past end of file
+ return EFI_UNSUPPORTED;
+ }
+ CurrentPosition = File->MaxPosition;
+ break;
+
+ default:
+ return EFI_NOT_FOUND;
+ }
+
+ Status = EFI_SUCCESS;
+ if (File->FsFileHandle != NULL) {
+ Status = File->FsFileHandle->SetPosition (File->FsFileHandle, CurrentPosition);
+ }
+
+ if (!EFI_ERROR (Status)) {
+ File->CurrentPosition = CurrentPosition;
+ }
+
+ return Status;
+}
+
+EFI_STATUS
+CacheTftpFile (
+ IN OUT EFI_OPEN_FILE *File
+ )
+{
+ EFI_STATUS Status;
+ UINT64 TftpBufferSize;
+
+ if (File->IsBufferValid) {
+ return EFI_SUCCESS;
+ }
+
+ // Make sure the file size is set.
+ EfiTell (File, NULL);
+
+ //Allocate a buffer to hold the whole file.
+ File->Buffer = AllocatePool(File->Size);
+ if (File->Buffer == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ TftpBufferSize = File->Size;
+
+ Status = EblMtftp (
+ EFI_PXE_BASE_CODE_TFTP_READ_FILE,
+ File->Buffer,
+ FALSE,
+ &TftpBufferSize,
+ NULL,
+ &File->ServerIp,
+ (UINT8 *)File->FileName,
+ NULL,
+ FALSE);
+ if (EFI_ERROR(Status)) {
+ AsciiPrint("TFTP error during APPLE_NSP_TFTP_READ_FILE: %r\n", Status);
+ FreePool(File->Buffer);
+ return Status;
+ }
+
+ // Set the buffer valid flag.
+ File->IsBufferValid = TRUE;
+
+ return Status;
+}
+
+/**
+Read BufferSize bytes from the current location in the file. For load file,
+FV, and TFTP case you must read the entire file.
+
+@param Stream Open File Handle
+@param Buffer Caller allocated buffer.
+@param BufferSize Size of buffer in bytes.
+
+
+@return EFI_SUCCESS Stream is not an Open File
+@return EFI_END_OF_FILE Tried to read past the end of the file
+@return EFI_INVALID_PARAMETER Stream is not an open file handle
+@return EFI_BUFFER_TOO_SMALL Buffer is not big enough to do the read
+@return "other" Error returned from device read
+
+**/
+EFI_STATUS
+EfiRead (
+ IN EFI_OPEN_FILE *File,
+ OUT VOID *Buffer,
+ OUT UINTN *BufferSize
+ )
+{
+ EFI_STATUS Status;
+ UINT32 AuthenticationStatus;
+ EFI_DISK_IO_PROTOCOL *DiskIo;
+
+ if (!FileHandleValid (File)) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ // Don't read past the end of the file.
+ if ((File->CurrentPosition + *BufferSize) > File->MaxPosition) {
+ return EFI_END_OF_FILE;
+ }
+
+ switch (File->Type) {
+ case EfiOpenLoadFile:
+ // Figure out the File->Size
+ EfiTell (File, NULL);
+
+ Status = File->LoadFile->LoadFile (File->LoadFile, File->DevicePath, FALSE, BufferSize, Buffer);
+ break;
+
+ case EfiOpenFirmwareVolume:
+ if (CompareGuid (&File->FvNameGuid, &gZeroGuid)) {
+ // This is the entire FV device, so treat like a memory buffer
+ CopyMem (Buffer, (VOID *)(UINTN)(File->FvStart + File->CurrentPosition), *BufferSize);
+ File->CurrentPosition += *BufferSize;
+ Status = EFI_SUCCESS;
+ } else {
+ if (File->Buffer == NULL) {
+ if (File->FvSectionType == EFI_SECTION_ALL) {
+ Status = File->Fv->ReadFile (
+ File->Fv,
+ &File->FvNameGuid,
+ (VOID **)&File->Buffer,
+ &File->Size,
+ &File->FvType,
+ &File->FvAttributes,
+ &AuthenticationStatus
+ );
+ } else {
+ Status = File->Fv->ReadSection (
+ File->Fv,
+ &File->FvNameGuid,
+ File->FvSectionType,
+ 0,
+ (VOID **)&File->Buffer,
+ &File->Size,
+ &AuthenticationStatus
+ );
+ }
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+ File->IsBufferValid = TRUE;
+ }
+ // Operate on the cached buffer so Seek will work
+ CopyMem (Buffer, File->Buffer + File->CurrentPosition, *BufferSize);
+ File->CurrentPosition += *BufferSize;
+ Status = EFI_SUCCESS;
+ }
+ break;
+
+ case EfiOpenMemoryBuffer:
+ CopyMem (Buffer, File->Buffer + File->CurrentPosition, *BufferSize);
+ File->CurrentPosition += *BufferSize;
+ Status = EFI_SUCCESS;
+ break;
+
+ case EfiOpenFileSystem:
+ Status = File->FsFileHandle->Read (File->FsFileHandle, BufferSize, Buffer);
+ File->CurrentPosition += *BufferSize;
+ break;
+
+ case EfiOpenBlockIo:
+ Status = gBS->HandleProtocol(File->EfiHandle, &gEfiDiskIoProtocolGuid, (VOID **)&DiskIo);
+ if (!EFI_ERROR(Status)) {
+ Status = DiskIo->ReadDisk(DiskIo, File->FsBlockIoMedia->MediaId, File->DiskOffset + File->CurrentPosition, *BufferSize, Buffer);
+ }
+ File->CurrentPosition += *BufferSize;
+ break;
+
+ case EfiOpenTftp:
+ // Cache the file if it hasn't been cached yet.
+ if (File->IsBufferValid == FALSE) {
+ Status = CacheTftpFile (File);
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+ }
+
+ // Copy out the requested data
+ CopyMem (Buffer, File->Buffer + File->CurrentPosition, *BufferSize);
+ File->CurrentPosition += *BufferSize;
+
+ Status = EFI_SUCCESS;
+ break;
+
+ default:
+ return EFI_INVALID_PARAMETER;
+ };
+
+ return Status;
+}
+
+
+/**
+Read the entire file into a buffer. This routine allocates the buffer and
+returns it to the user full of the read data.
+
+This is very useful for load file where it's hard to know how big the buffer
+must be.
+
+@param Stream Open File Handle
+@param Buffer Pointer to buffer to return.
+@param BufferSize Pointer to Size of buffer return..
+
+
+@return EFI_SUCCESS Stream is not an Open File
+@return EFI_END_OF_FILE Tried to read past the end of the file
+@return EFI_INVALID_PARAMETER Stream is not an open file handle
+@return EFI_BUFFER_TOO_SMALL Buffer is not big enough to do the read
+@return "other" Error returned from device read
+
+**/
+EFI_STATUS
+EfiReadAllocatePool (
+ IN EFI_OPEN_FILE *File,
+ OUT VOID **Buffer,
+ OUT UINTN *BufferSize
+ )
+{
+ if (!FileHandleValid (File)) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ // Loadfile defers file size determination on Open so use tell to find it
+ EfiTell (File, NULL);
+
+ *BufferSize = File->Size;
+ *Buffer = AllocatePool (*BufferSize);
+ if (*Buffer == NULL) {
+ return EFI_NOT_FOUND;
+ }
+
+ return EfiRead (File, *Buffer, BufferSize);
+}
+
+
+/**
+Write data back to the file. For TFTP case you must write the entire file.
+
+@param Stream Open File Handle
+@param Buffer Pointer to buffer to return.
+@param BufferSize Pointer to Size of buffer return..
+
+
+@return EFI_SUCCESS Stream is not an Open File
+@return EFI_END_OF_FILE Tried to read past the end of the file
+@return EFI_INVALID_PARAMETER Stream is not an open file handle
+@return EFI_BUFFER_TOO_SMALL Buffer is not big enough to do the read
+@return "other" Error returned from device write
+
+**/
+EFI_STATUS
+EfiWrite (
+ IN EFI_OPEN_FILE *File,
+ OUT VOID *Buffer,
+ OUT UINTN *BufferSize
+ )
+{
+ EFI_STATUS Status;
+ EFI_FV_WRITE_FILE_DATA FileData;
+ EFI_DISK_IO_PROTOCOL *DiskIo;
+
+ if (!FileHandleValid (File)) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ switch (File->Type) {
+ case EfiOpenMemoryBuffer:
+ if ((File->CurrentPosition + *BufferSize) > File->MaxPosition) {
+ return EFI_END_OF_FILE;
+ }
+
+ CopyMem (File->Buffer + File->CurrentPosition, Buffer, *BufferSize);
+ File->CurrentPosition += *BufferSize;
+ Status = EFI_SUCCESS;
+
+ case EfiOpenLoadFile:
+ // LoadFile device is read only be definition
+ Status = EFI_UNSUPPORTED;
+
+ case EfiOpenFirmwareVolume:
+ if (File->FvSectionType != EFI_SECTION_ALL) {
+ // Writes not support to a specific section. You have to update entire file
+ return EFI_UNSUPPORTED;
+ }
+
+ FileData.NameGuid = &(File->FvNameGuid);
+ FileData.Type = File->FvType;
+ FileData.FileAttributes = File->FvAttributes;
+ FileData.Buffer = Buffer;
+ FileData.BufferSize = (UINT32)*BufferSize;
+ Status = File->Fv->WriteFile (File->Fv, 1, EFI_FV_UNRELIABLE_WRITE, &FileData);
+ break;
+
+ case EfiOpenFileSystem:
+ Status = File->FsFileHandle->Write (File->FsFileHandle, BufferSize, Buffer);
+ File->CurrentPosition += *BufferSize;
+ break;
+
+ case EfiOpenBlockIo:
+ if ((File->CurrentPosition + *BufferSize) > File->MaxPosition) {
+ return EFI_END_OF_FILE;
+ }
+
+ Status = gBS->HandleProtocol (File->EfiHandle, &gEfiDiskIoProtocolGuid, (VOID **)&DiskIo);
+ if (!EFI_ERROR(Status)) {
+ Status = DiskIo->WriteDisk (DiskIo, File->FsBlockIoMedia->MediaId, File->DiskOffset + File->CurrentPosition, *BufferSize, Buffer);
+ }
+ File->CurrentPosition += *BufferSize;
+ break;
+
+ case EfiOpenTftp:
+ // Cache the file if it hasn't been cached yet.
+ if (File->IsBufferValid == FALSE) {
+ Status = CacheTftpFile(File);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+ }
+
+ // Don't overwrite the buffer
+ if ((File->CurrentPosition + *BufferSize) > File->MaxPosition) {
+ UINT8 *TempBuffer;
+
+ TempBuffer = File->Buffer;
+
+ File->Buffer = AllocatePool ((UINTN)(File->CurrentPosition + *BufferSize));
+ if (File->Buffer == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ CopyMem (File->Buffer, TempBuffer, File->Size);
+
+ FreePool (TempBuffer);
+
+ File->Size = (UINTN)(File->CurrentPosition + *BufferSize);
+ File->MaxPosition = (UINT64)File->Size;
+ }
+
+ // Copy in the requested data
+ CopyMem (File->Buffer + File->CurrentPosition, Buffer, *BufferSize);
+ File->CurrentPosition += *BufferSize;
+
+ // Mark the file dirty
+ File->IsDirty = TRUE;
+
+ Status = EFI_SUCCESS;
+ break;
+
+ default:
+ Status = EFI_INVALID_PARAMETER;
+ };
+
+ return Status;
+}
+
+
+/**
+Given Cwd expand Path to remove .. and replace them with real
+directory names.
+
+@param Cwd Current Working Directory
+@param Path Path to expand
+
+@return NULL Cwd or Path are not valid
+@return 'other' Path with .. expanded
+
+**/
+CHAR8 *
+ExpandPath (
+ IN CHAR8 *Cwd,
+ IN CHAR8 *Path
+ )
+{
+ CHAR8 *NewPath;
+ CHAR8 *Work, *Start, *End;
+ UINTN StrLen;
+ INTN i;
+
+ if (Cwd == NULL || Path == NULL) {
+ return NULL;
+ }
+
+ StrLen = AsciiStrSize (Cwd);
+ if (StrLen <= 2) {
+ // Smallest valid path is 1 char and a null
+ return NULL;
+ }
+
+ StrLen = AsciiStrSize (Path);
+ NewPath = AllocatePool (AsciiStrSize (Cwd) + StrLen + 1);
+ if (NewPath == NULL) {
+ return NULL;
+ }
+ AsciiStrCpy (NewPath, Cwd);
+
+ End = Path + StrLen;
+ for (Start = Path ;;) {
+ Work = AsciiStrStr (Start, "..") ;
+ if (Work == NULL) {
+ // Remaining part of Path contains no more ..
+ break;
+ }
+
+ // append path prior to ..
+ AsciiStrnCat (NewPath, Start, Work - Start);
+ StrLen = AsciiStrLen (NewPath);
+ for (i = StrLen; i >= 0; i--) {
+ if (NewPath[i] == ':') {
+ // too many ..
+ return NULL;
+ }
+ if (NewPath[i] == '/' || NewPath[i] == '\\') {
+ if ((i > 0) && (NewPath[i-1] == ':')) {
+ // leave the / before a :
+ NewPath[i+1] = '\0';
+ } else {
+ // replace / will Null to remove trailing file/dir reference
+ NewPath[i] = '\0';
+ }
+ break;
+ }
+ }
+
+ Start = Work + 3;
+ }
+
+ // Handle the path that remains after the ..
+ AsciiStrnCat (NewPath, Start, End - Start);
+
+ return NewPath;
+}
+
+
+/**
+Set the Current Working Directory (CWD). If a call is made to EfiOpen () and
+the path does not contain a device name, The CWD is prepended to the path.
+
+@param Cwd Current Working Directory to set
+
+
+@return EFI_SUCCESS CWD is set
+@return EFI_INVALID_PARAMETER Cwd is not a valid device:path
+
+**/
+EFI_STATUS
+EfiSetCwd (
+ IN CHAR8 *Cwd
+ )
+{
+ EFI_OPEN_FILE *File;
+ UINTN Len;
+ CHAR8 *Path;
+
+ if (Cwd == NULL) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ if (AsciiStrCmp (Cwd, ".") == 0) {
+ // cd . is a no-op
+ return EFI_SUCCESS;
+ }
+
+ Path = Cwd;
+ if (AsciiStrStr (Cwd, "..") != NULL) {
+ if (gCwd == NULL) {
+ // no parent
+ return EFI_SUCCESS;
+ }
+
+ Len = AsciiStrLen (gCwd);
+ if ((gCwd[Len-2] == ':') && ((gCwd[Len-1] == '/') || (gCwd[Len-1] == '\\'))) {
+ // parent is device so nothing to do
+ return EFI_SUCCESS;
+ }
+
+ // Expand .. in Cwd, given we know current working directory
+ Path = ExpandPath (gCwd, Cwd);
+ if (Path == NULL) {
+ return EFI_NOT_FOUND;
+ }
+ }
+
+ File = EfiOpen (Path, EFI_FILE_MODE_READ, 0);
+ if (File == NULL) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ if (gCwd != NULL) {
+ FreePool (gCwd);
+ }
+
+ // Use the info returned from EfiOpen as it can add in CWD if needed. So Cwd could be
+ // relative to the current gCwd or not.
+ gCwd = AllocatePool (AsciiStrSize (File->DeviceName) + AsciiStrSize (File->FileName) + 10);
+ if (gCwd == NULL) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ AsciiStrCpy (gCwd, File->DeviceName);
+ if (File->FileName == NULL) {
+ AsciiStrCat (gCwd, ":\\");
+ } else {
+ AsciiStrCat (gCwd, ":");
+ AsciiStrCat (gCwd, File->FileName);
+ }
+
+
+ EfiClose (File);
+ if (Path != Cwd) {
+ FreePool (Path);
+ }
+ return EFI_SUCCESS;
+}
+
+
+/**
+Set the Current Working Directory (CWD). If a call is made to EfiOpen () and
+the path does not contain a device name, The CWD is prepended to the path.
+The CWD buffer is only valid until a new call is made to EfiSetCwd(). After
+a call to EfiSetCwd() it is not legal to use the pointer returned by
+this function.
+
+@param Cwd Current Working Directory
+
+
+@return "" No CWD set
+@return 'other' Returns buffer that contains CWD.
+
+**/
+CHAR8 *
+EfiGetCwd (
+ VOID
+ )
+{
+ if (gCwd == NULL) {
+ return "";
+ }
+ return gCwd;
+}
+
+
diff --git a/EmbeddedPkg/Library/FdtLib/fdt.c b/EmbeddedPkg/Library/FdtLib/fdt.c index e56833ae9b..151fcb2ab0 100644 --- a/EmbeddedPkg/Library/FdtLib/fdt.c +++ b/EmbeddedPkg/Library/FdtLib/fdt.c @@ -1,222 +1,222 @@ -/* - * libfdt - Flat Device Tree manipulation - * Copyright (C) 2006 David Gibson, IBM Corporation. - * - * libfdt is dual licensed: you can use it either under the terms of - * the GPL, or the BSD license, at your option. - * - * a) This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, - * MA 02110-1301 USA - * - * Alternatively, - * - * b) Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "libfdt_env.h" - -#include <fdt.h> -#include <libfdt.h> - -#include "libfdt_internal.h" - -int fdt_check_header(const void *fdt) -{ - if (fdt_magic(fdt) == FDT_MAGIC) { - /* Complete tree */ - if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) - return -FDT_ERR_BADVERSION; - if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) - return -FDT_ERR_BADVERSION; - } else if (fdt_magic(fdt) == FDT_SW_MAGIC) { - /* Unfinished sequential-write blob */ - if (fdt_size_dt_struct(fdt) == 0) - return -FDT_ERR_BADSTATE; - } else { - return -FDT_ERR_BADMAGIC; - } - - return 0; -} - -const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len) -{ - const char *p; - - if (fdt_version(fdt) >= 0x11) - if (((offset + len) < offset) - || ((offset + len) > fdt_size_dt_struct(fdt))) - return NULL; - - p = _fdt_offset_ptr(fdt, offset); - - if (p + len < p) - return NULL; - return p; -} - -uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) -{ - const uint32_t *tagp, *lenp; - uint32_t tag; - int offset = startoffset; - const char *p; - - *nextoffset = -FDT_ERR_TRUNCATED; - tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE); - if (!tagp) - return FDT_END; /* premature end */ - tag = fdt32_to_cpu(*tagp); - offset += FDT_TAGSIZE; - - *nextoffset = -FDT_ERR_BADSTRUCTURE; - switch (tag) { - case FDT_BEGIN_NODE: - /* skip name */ - do { - p = fdt_offset_ptr(fdt, offset++, 1); - } while (p && (*p != '\0')); - if (!p) - return FDT_END; /* premature end */ - break; - - case FDT_PROP: - lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp)); - if (!lenp) - return FDT_END; /* premature end */ - /* skip-name offset, length and value */ - offset += sizeof(struct fdt_property) - FDT_TAGSIZE - + fdt32_to_cpu(*lenp); - break; - - case FDT_END: - case FDT_END_NODE: - case FDT_NOP: - break; - - default: - return FDT_END; - } - - if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset)) - return FDT_END; /* premature end */ - - *nextoffset = FDT_TAGALIGN(offset); - return tag; -} - -int _fdt_check_node_offset(const void *fdt, int offset) -{ - if ((offset < 0) || (offset % FDT_TAGSIZE) - || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)) - return -FDT_ERR_BADOFFSET; - - return offset; -} - -int _fdt_check_prop_offset(const void *fdt, int offset) -{ - if ((offset < 0) || (offset % FDT_TAGSIZE) - || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)) - return -FDT_ERR_BADOFFSET; - - return offset; -} - -int fdt_next_node(const void *fdt, int offset, int *depth) -{ - int nextoffset = 0; - uint32_t tag; - - if (offset >= 0) - if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0) - return nextoffset; - - do { - offset = nextoffset; - tag = fdt_next_tag(fdt, offset, &nextoffset); - - switch (tag) { - case FDT_PROP: - case FDT_NOP: - break; - - case FDT_BEGIN_NODE: - if (depth) - (*depth)++; - break; - - case FDT_END_NODE: - if (depth && ((--(*depth)) < 0)) - return nextoffset; - break; - - case FDT_END: - if ((nextoffset >= 0) - || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth)) - return -FDT_ERR_NOTFOUND; - else - return nextoffset; - } - } while (tag != FDT_BEGIN_NODE); - - return offset; -} - -const char *_fdt_find_string(const char *strtab, int tabsize, const char *s) -{ - int len = strlen(s) + 1; - const char *last = strtab + tabsize - len; - const char *p; - - for (p = strtab; p <= last; p++) - if (memcmp(p, s, len) == 0) - return p; - return NULL; -} - -int fdt_move(const void *fdt, void *buf, int bufsize) -{ - FDT_CHECK_HEADER(fdt); - - if (fdt_totalsize(fdt) > bufsize) - return -FDT_ERR_NOSPACE; - - memmove(buf, fdt, fdt_totalsize(fdt)); - return 0; -} +/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+int fdt_check_header(const void *fdt)
+{
+ if (fdt_magic(fdt) == FDT_MAGIC) {
+ /* Complete tree */
+ if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
+ return -FDT_ERR_BADVERSION;
+ if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
+ return -FDT_ERR_BADVERSION;
+ } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
+ /* Unfinished sequential-write blob */
+ if (fdt_size_dt_struct(fdt) == 0)
+ return -FDT_ERR_BADSTATE;
+ } else {
+ return -FDT_ERR_BADMAGIC;
+ }
+
+ return 0;
+}
+
+const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
+{
+ const char *p;
+
+ if (fdt_version(fdt) >= 0x11)
+ if (((offset + len) < offset)
+ || ((offset + len) > fdt_size_dt_struct(fdt)))
+ return NULL;
+
+ p = _fdt_offset_ptr(fdt, offset);
+
+ if (p + len < p)
+ return NULL;
+ return p;
+}
+
+uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
+{
+ const uint32_t *tagp, *lenp;
+ uint32_t tag;
+ int offset = startoffset;
+ const char *p;
+
+ *nextoffset = -FDT_ERR_TRUNCATED;
+ tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
+ if (!tagp)
+ return FDT_END; /* premature end */
+ tag = fdt32_to_cpu(*tagp);
+ offset += FDT_TAGSIZE;
+
+ *nextoffset = -FDT_ERR_BADSTRUCTURE;
+ switch (tag) {
+ case FDT_BEGIN_NODE:
+ /* skip name */
+ do {
+ p = fdt_offset_ptr(fdt, offset++, 1);
+ } while (p && (*p != '\0'));
+ if (!p)
+ return FDT_END; /* premature end */
+ break;
+
+ case FDT_PROP:
+ lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
+ if (!lenp)
+ return FDT_END; /* premature end */
+ /* skip-name offset, length and value */
+ offset += sizeof(struct fdt_property) - FDT_TAGSIZE
+ + fdt32_to_cpu(*lenp);
+ break;
+
+ case FDT_END:
+ case FDT_END_NODE:
+ case FDT_NOP:
+ break;
+
+ default:
+ return FDT_END;
+ }
+
+ if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
+ return FDT_END; /* premature end */
+
+ *nextoffset = FDT_TAGALIGN(offset);
+ return tag;
+}
+
+int _fdt_check_node_offset(const void *fdt, int offset)
+{
+ if ((offset < 0) || (offset % FDT_TAGSIZE)
+ || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
+ return -FDT_ERR_BADOFFSET;
+
+ return offset;
+}
+
+int _fdt_check_prop_offset(const void *fdt, int offset)
+{
+ if ((offset < 0) || (offset % FDT_TAGSIZE)
+ || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
+ return -FDT_ERR_BADOFFSET;
+
+ return offset;
+}
+
+int fdt_next_node(const void *fdt, int offset, int *depth)
+{
+ int nextoffset = 0;
+ uint32_t tag;
+
+ if (offset >= 0)
+ if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
+ return nextoffset;
+
+ do {
+ offset = nextoffset;
+ tag = fdt_next_tag(fdt, offset, &nextoffset);
+
+ switch (tag) {
+ case FDT_PROP:
+ case FDT_NOP:
+ break;
+
+ case FDT_BEGIN_NODE:
+ if (depth)
+ (*depth)++;
+ break;
+
+ case FDT_END_NODE:
+ if (depth && ((--(*depth)) < 0))
+ return nextoffset;
+ break;
+
+ case FDT_END:
+ if ((nextoffset >= 0)
+ || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
+ return -FDT_ERR_NOTFOUND;
+ else
+ return nextoffset;
+ }
+ } while (tag != FDT_BEGIN_NODE);
+
+ return offset;
+}
+
+const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
+{
+ int len = strlen(s) + 1;
+ const char *last = strtab + tabsize - len;
+ const char *p;
+
+ for (p = strtab; p <= last; p++)
+ if (memcmp(p, s, len) == 0)
+ return p;
+ return NULL;
+}
+
+int fdt_move(const void *fdt, void *buf, int bufsize)
+{
+ FDT_CHECK_HEADER(fdt);
+
+ if (fdt_totalsize(fdt) > bufsize)
+ return -FDT_ERR_NOSPACE;
+
+ memmove(buf, fdt, fdt_totalsize(fdt));
+ return 0;
+}
diff --git a/EmbeddedPkg/Library/FdtLib/fdt_empty_tree.c b/EmbeddedPkg/Library/FdtLib/fdt_empty_tree.c index f72d13b1d1..f00a8991d4 100644 --- a/EmbeddedPkg/Library/FdtLib/fdt_empty_tree.c +++ b/EmbeddedPkg/Library/FdtLib/fdt_empty_tree.c @@ -1,84 +1,84 @@ -/* - * libfdt - Flat Device Tree manipulation - * Copyright (C) 2012 David Gibson, IBM Corporation. - * - * libfdt is dual licensed: you can use it either under the terms of - * the GPL, or the BSD license, at your option. - * - * a) This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, - * MA 02110-1301 USA - * - * Alternatively, - * - * b) Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "libfdt_env.h" - -#include <fdt.h> -#include <libfdt.h> - -#include "libfdt_internal.h" - -int fdt_create_empty_tree(void *buf, int bufsize) -{ - int err; - - err = fdt_create(buf, bufsize); - if (err) - return err; - - err = fdt_finish_reservemap(buf); - if (err) - return err; - - err = fdt_begin_node(buf, ""); - if (err) - return err; - - err = fdt_end_node(buf); - if (err) - return err; - - err = fdt_finish(buf); - if (err) - return err; - - return fdt_open_into(buf, buf, bufsize); -} - +/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2012 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+int fdt_create_empty_tree(void *buf, int bufsize)
+{
+ int err;
+
+ err = fdt_create(buf, bufsize);
+ if (err)
+ return err;
+
+ err = fdt_finish_reservemap(buf);
+ if (err)
+ return err;
+
+ err = fdt_begin_node(buf, "");
+ if (err)
+ return err;
+
+ err = fdt_end_node(buf);
+ if (err)
+ return err;
+
+ err = fdt_finish(buf);
+ if (err)
+ return err;
+
+ return fdt_open_into(buf, buf, bufsize);
+}
+
diff --git a/EmbeddedPkg/Library/FdtLib/fdt_ro.c b/EmbeddedPkg/Library/FdtLib/fdt_ro.c index 95fdbeadd4..9ae6b5ecc5 100644 --- a/EmbeddedPkg/Library/FdtLib/fdt_ro.c +++ b/EmbeddedPkg/Library/FdtLib/fdt_ro.c @@ -1,575 +1,575 @@ -/* - * libfdt - Flat Device Tree manipulation - * Copyright (C) 2006 David Gibson, IBM Corporation. - * - * libfdt is dual licensed: you can use it either under the terms of - * the GPL, or the BSD license, at your option. - * - * a) This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, - * MA 02110-1301 USA - * - * Alternatively, - * - * b) Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "libfdt_env.h" - -#include <fdt.h> -#include <libfdt.h> - -#include "libfdt_internal.h" - -static int _fdt_nodename_eq(const void *fdt, int offset, - const char *s, int len) -{ - const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1); - - if (! p) - /* short match */ - return 0; - - if (memcmp(p, s, len) != 0) - return 0; - - if (p[len] == '\0') - return 1; - else if (!memchr(s, '@', len) && (p[len] == '@')) - return 1; - else - return 0; -} - -const char *fdt_string(const void *fdt, int stroffset) -{ - return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset; -} - -static int _fdt_string_eq(const void *fdt, int stroffset, - const char *s, int len) -{ - const char *p = fdt_string(fdt, stroffset); - - return (strlen(p) == len) && (memcmp(p, s, len) == 0); -} - -int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size) -{ - FDT_CHECK_HEADER(fdt); - *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address); - *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size); - return 0; -} - -int fdt_num_mem_rsv(const void *fdt) -{ - int i = 0; - - while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0) - i++; - return i; -} - -static int _nextprop(const void *fdt, int offset) -{ - uint32_t tag; - int nextoffset; - - do { - tag = fdt_next_tag(fdt, offset, &nextoffset); - - switch (tag) { - case FDT_END: - if (nextoffset >= 0) - return -FDT_ERR_BADSTRUCTURE; - else - return nextoffset; - - case FDT_PROP: - return offset; - } - offset = nextoffset; - } while (tag == FDT_NOP); - - return -FDT_ERR_NOTFOUND; -} - -int fdt_subnode_offset_namelen(const void *fdt, int offset, - const char *name, int namelen) -{ - int depth; - - FDT_CHECK_HEADER(fdt); - - for (depth = 0; - (offset >= 0) && (depth >= 0); - offset = fdt_next_node(fdt, offset, &depth)) - if ((depth == 1) - && _fdt_nodename_eq(fdt, offset, name, namelen)) - return offset; - - if (depth < 0) - return -FDT_ERR_NOTFOUND; - return offset; /* error */ -} - -int fdt_subnode_offset(const void *fdt, int parentoffset, - const char *name) -{ - return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name)); -} - -int fdt_path_offset(const void *fdt, const char *path) -{ - const char *end = path + strlen(path); - const char *p = path; - int offset = 0; - - FDT_CHECK_HEADER(fdt); - - /* see if we have an alias */ - if (*path != '/') { - const char *q = strchr(path, '/'); - - if (!q) - q = end; - - p = fdt_get_alias_namelen(fdt, p, q - p); - if (!p) - return -FDT_ERR_BADPATH; - offset = fdt_path_offset(fdt, p); - - p = q; - } - - while (*p) { - const char *q; - - while (*p == '/') - p++; - if (! *p) - return offset; - q = strchr(p, '/'); - if (! q) - q = end; - - offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p); - if (offset < 0) - return offset; - - p = q; - } - - return offset; -} - -const char *fdt_get_name(const void *fdt, int nodeoffset, int *len) -{ - const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset); - int err; - - if (((err = fdt_check_header(fdt)) != 0) - || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0)) - goto fail; - - if (len) - *len = strlen(nh->name); - - return nh->name; - - fail: - if (len) - *len = err; - return NULL; -} - -int fdt_first_property_offset(const void *fdt, int nodeoffset) -{ - int offset; - - if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0) - return offset; - - return _nextprop(fdt, offset); -} - -int fdt_next_property_offset(const void *fdt, int offset) -{ - if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0) - return offset; - - return _nextprop(fdt, offset); -} - -const struct fdt_property *fdt_get_property_by_offset(const void *fdt, - int offset, - int *lenp) -{ - int err; - const struct fdt_property *prop; - - if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) { - if (lenp) - *lenp = err; - return NULL; - } - - prop = _fdt_offset_ptr(fdt, offset); - - if (lenp) - *lenp = fdt32_to_cpu(prop->len); - - return prop; -} - -const struct fdt_property *fdt_get_property_namelen(const void *fdt, - int offset, - const char *name, - int namelen, int *lenp) -{ - for (offset = fdt_first_property_offset(fdt, offset); - (offset >= 0); - (offset = fdt_next_property_offset(fdt, offset))) { - const struct fdt_property *prop; - - prop = fdt_get_property_by_offset(fdt, offset, lenp); - if (!prop) { - offset = -FDT_ERR_INTERNAL; - break; - } - if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff), - name, namelen)) - return prop; - } - - if (lenp) - *lenp = offset; - return NULL; -} - -const struct fdt_property *fdt_get_property(const void *fdt, - int nodeoffset, - const char *name, int *lenp) -{ - return fdt_get_property_namelen(fdt, nodeoffset, name, - strlen(name), lenp); -} - -const void *fdt_getprop_namelen(const void *fdt, int nodeoffset, - const char *name, int namelen, int *lenp) -{ - const struct fdt_property *prop; - - prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp); - if (! prop) - return NULL; - - return prop->data; -} - -const void *fdt_getprop_by_offset(const void *fdt, int offset, - const char **namep, int *lenp) -{ - const struct fdt_property *prop; - - prop = fdt_get_property_by_offset(fdt, offset, lenp); - if (!prop) - return NULL; - if (namep) - *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff)); - return prop->data; -} - -const void *fdt_getprop(const void *fdt, int nodeoffset, - const char *name, int *lenp) -{ - return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp); -} - -uint32_t fdt_get_phandle(const void *fdt, int nodeoffset) -{ - const uint32_t *php; - int len; - - /* FIXME: This is a bit sub-optimal, since we potentially scan - * over all the properties twice. */ - php = fdt_getprop(fdt, nodeoffset, "phandle", &len); - if (!php || (len != sizeof(*php))) { - php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len); - if (!php || (len != sizeof(*php))) - return 0; - } - - return fdt32_to_cpu(*php); -} - -const char *fdt_get_alias_namelen(const void *fdt, - const char *name, int namelen) -{ - int aliasoffset; - - aliasoffset = fdt_path_offset(fdt, "/aliases"); - if (aliasoffset < 0) - return NULL; - - return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL); -} - -const char *fdt_get_alias(const void *fdt, const char *name) -{ - return fdt_get_alias_namelen(fdt, name, strlen(name)); -} - -int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen) -{ - int pdepth = 0, p = 0; - int offset, depth, namelen; - const char *name; - - FDT_CHECK_HEADER(fdt); - - if (buflen < 2) - return -FDT_ERR_NOSPACE; - - for (offset = 0, depth = 0; - (offset >= 0) && (offset <= nodeoffset); - offset = fdt_next_node(fdt, offset, &depth)) { - while (pdepth > depth) { - do { - p--; - } while (buf[p-1] != '/'); - pdepth--; - } - - if (pdepth >= depth) { - name = fdt_get_name(fdt, offset, &namelen); - if (!name) - return namelen; - if ((p + namelen + 1) <= buflen) { - memcpy(buf + p, name, namelen); - p += namelen; - buf[p++] = '/'; - pdepth++; - } - } - - if (offset == nodeoffset) { - if (pdepth < (depth + 1)) - return -FDT_ERR_NOSPACE; - - if (p > 1) /* special case so that root path is "/", not "" */ - p--; - buf[p] = '\0'; - return 0; - } - } - - if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) - return -FDT_ERR_BADOFFSET; - else if (offset == -FDT_ERR_BADOFFSET) - return -FDT_ERR_BADSTRUCTURE; - - return offset; /* error from fdt_next_node() */ -} - -int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, - int supernodedepth, int *nodedepth) -{ - int offset, depth; - int supernodeoffset = -FDT_ERR_INTERNAL; - - FDT_CHECK_HEADER(fdt); - - if (supernodedepth < 0) - return -FDT_ERR_NOTFOUND; - - for (offset = 0, depth = 0; - (offset >= 0) && (offset <= nodeoffset); - offset = fdt_next_node(fdt, offset, &depth)) { - if (depth == supernodedepth) - supernodeoffset = offset; - - if (offset == nodeoffset) { - if (nodedepth) - *nodedepth = depth; - - if (supernodedepth > depth) - return -FDT_ERR_NOTFOUND; - else - return supernodeoffset; - } - } - - if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) - return -FDT_ERR_BADOFFSET; - else if (offset == -FDT_ERR_BADOFFSET) - return -FDT_ERR_BADSTRUCTURE; - - return offset; /* error from fdt_next_node() */ -} - -int fdt_node_depth(const void *fdt, int nodeoffset) -{ - int nodedepth; - int err; - - err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth); - if (err) - return (err < 0) ? err : -FDT_ERR_INTERNAL; - return nodedepth; -} - -int fdt_parent_offset(const void *fdt, int nodeoffset) -{ - int nodedepth = fdt_node_depth(fdt, nodeoffset); - - if (nodedepth < 0) - return nodedepth; - return fdt_supernode_atdepth_offset(fdt, nodeoffset, - nodedepth - 1, NULL); -} - -int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, - const char *propname, - const void *propval, int proplen) -{ - int offset; - const void *val; - int len; - - FDT_CHECK_HEADER(fdt); - - /* FIXME: The algorithm here is pretty horrible: we scan each - * property of a node in fdt_getprop(), then if that didn't - * find what we want, we scan over them again making our way - * to the next node. Still it's the easiest to implement - * approach; performance can come later. */ - for (offset = fdt_next_node(fdt, startoffset, NULL); - offset >= 0; - offset = fdt_next_node(fdt, offset, NULL)) { - val = fdt_getprop(fdt, offset, propname, &len); - if (val && (len == proplen) - && (memcmp(val, propval, len) == 0)) - return offset; - } - - return offset; /* error from fdt_next_node() */ -} - -int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) -{ - int offset; - - if ((phandle == 0) || (phandle == (uint32_t)-1)) - return -FDT_ERR_BADPHANDLE; - - FDT_CHECK_HEADER(fdt); - - /* FIXME: The algorithm here is pretty horrible: we - * potentially scan each property of a node in - * fdt_get_phandle(), then if that didn't find what - * we want, we scan over them again making our way to the next - * node. Still it's the easiest to implement approach; - * performance can come later. */ - for (offset = fdt_next_node(fdt, -1, NULL); - offset >= 0; - offset = fdt_next_node(fdt, offset, NULL)) { - if (fdt_get_phandle(fdt, offset) == phandle) - return offset; - } - - return offset; /* error from fdt_next_node() */ -} - -static int _fdt_stringlist_contains(const char *strlist, int listlen, - const char *str) -{ - int len = strlen(str); - const char *p; - - while (listlen >= len) { - if (memcmp(str, strlist, len+1) == 0) - return 1; - p = memchr(strlist, '\0', listlen); - if (!p) - return 0; /* malformed strlist.. */ - listlen -= (p-strlist) + 1; - strlist = p + 1; - } - return 0; -} - -int fdt_node_check_compatible(const void *fdt, int nodeoffset, - const char *compatible) -{ - const void *prop; - int len; - - prop = fdt_getprop(fdt, nodeoffset, "compatible", &len); - if (!prop) - return len; - if (_fdt_stringlist_contains(prop, len, compatible)) - return 0; - else - return 1; -} - -int fdt_node_offset_by_compatible(const void *fdt, int startoffset, - const char *compatible) -{ - int offset, err; - - FDT_CHECK_HEADER(fdt); - - /* FIXME: The algorithm here is pretty horrible: we scan each - * property of a node in fdt_node_check_compatible(), then if - * that didn't find what we want, we scan over them again - * making our way to the next node. Still it's the easiest to - * implement approach; performance can come later. */ - for (offset = fdt_next_node(fdt, startoffset, NULL); - offset >= 0; - offset = fdt_next_node(fdt, offset, NULL)) { - err = fdt_node_check_compatible(fdt, offset, compatible); - if ((err < 0) && (err != -FDT_ERR_NOTFOUND)) - return err; - else if (err == 0) - return offset; - } - - return offset; /* error from fdt_next_node() */ -} +/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+static int _fdt_nodename_eq(const void *fdt, int offset,
+ const char *s, int len)
+{
+ const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
+
+ if (! p)
+ /* short match */
+ return 0;
+
+ if (memcmp(p, s, len) != 0)
+ return 0;
+
+ if (p[len] == '\0')
+ return 1;
+ else if (!memchr(s, '@', len) && (p[len] == '@'))
+ return 1;
+ else
+ return 0;
+}
+
+const char *fdt_string(const void *fdt, int stroffset)
+{
+ return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
+}
+
+static int _fdt_string_eq(const void *fdt, int stroffset,
+ const char *s, int len)
+{
+ const char *p = fdt_string(fdt, stroffset);
+
+ return (strlen(p) == len) && (memcmp(p, s, len) == 0);
+}
+
+int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
+{
+ FDT_CHECK_HEADER(fdt);
+ *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
+ *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
+ return 0;
+}
+
+int fdt_num_mem_rsv(const void *fdt)
+{
+ int i = 0;
+
+ while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
+ i++;
+ return i;
+}
+
+static int _nextprop(const void *fdt, int offset)
+{
+ uint32_t tag;
+ int nextoffset;
+
+ do {
+ tag = fdt_next_tag(fdt, offset, &nextoffset);
+
+ switch (tag) {
+ case FDT_END:
+ if (nextoffset >= 0)
+ return -FDT_ERR_BADSTRUCTURE;
+ else
+ return nextoffset;
+
+ case FDT_PROP:
+ return offset;
+ }
+ offset = nextoffset;
+ } while (tag == FDT_NOP);
+
+ return -FDT_ERR_NOTFOUND;
+}
+
+int fdt_subnode_offset_namelen(const void *fdt, int offset,
+ const char *name, int namelen)
+{
+ int depth;
+
+ FDT_CHECK_HEADER(fdt);
+
+ for (depth = 0;
+ (offset >= 0) && (depth >= 0);
+ offset = fdt_next_node(fdt, offset, &depth))
+ if ((depth == 1)
+ && _fdt_nodename_eq(fdt, offset, name, namelen))
+ return offset;
+
+ if (depth < 0)
+ return -FDT_ERR_NOTFOUND;
+ return offset; /* error */
+}
+
+int fdt_subnode_offset(const void *fdt, int parentoffset,
+ const char *name)
+{
+ return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
+}
+
+int fdt_path_offset(const void *fdt, const char *path)
+{
+ const char *end = path + strlen(path);
+ const char *p = path;
+ int offset = 0;
+
+ FDT_CHECK_HEADER(fdt);
+
+ /* see if we have an alias */
+ if (*path != '/') {
+ const char *q = strchr(path, '/');
+
+ if (!q)
+ q = end;
+
+ p = fdt_get_alias_namelen(fdt, p, q - p);
+ if (!p)
+ return -FDT_ERR_BADPATH;
+ offset = fdt_path_offset(fdt, p);
+
+ p = q;
+ }
+
+ while (*p) {
+ const char *q;
+
+ while (*p == '/')
+ p++;
+ if (! *p)
+ return offset;
+ q = strchr(p, '/');
+ if (! q)
+ q = end;
+
+ offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
+ if (offset < 0)
+ return offset;
+
+ p = q;
+ }
+
+ return offset;
+}
+
+const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
+{
+ const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
+ int err;
+
+ if (((err = fdt_check_header(fdt)) != 0)
+ || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
+ goto fail;
+
+ if (len)
+ *len = strlen(nh->name);
+
+ return nh->name;
+
+ fail:
+ if (len)
+ *len = err;
+ return NULL;
+}
+
+int fdt_first_property_offset(const void *fdt, int nodeoffset)
+{
+ int offset;
+
+ if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
+ return offset;
+
+ return _nextprop(fdt, offset);
+}
+
+int fdt_next_property_offset(const void *fdt, int offset)
+{
+ if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
+ return offset;
+
+ return _nextprop(fdt, offset);
+}
+
+const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
+ int offset,
+ int *lenp)
+{
+ int err;
+ const struct fdt_property *prop;
+
+ if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
+ if (lenp)
+ *lenp = err;
+ return NULL;
+ }
+
+ prop = _fdt_offset_ptr(fdt, offset);
+
+ if (lenp)
+ *lenp = fdt32_to_cpu(prop->len);
+
+ return prop;
+}
+
+const struct fdt_property *fdt_get_property_namelen(const void *fdt,
+ int offset,
+ const char *name,
+ int namelen, int *lenp)
+{
+ for (offset = fdt_first_property_offset(fdt, offset);
+ (offset >= 0);
+ (offset = fdt_next_property_offset(fdt, offset))) {
+ const struct fdt_property *prop;
+
+ prop = fdt_get_property_by_offset(fdt, offset, lenp);
+ if (!prop) {
+ offset = -FDT_ERR_INTERNAL;
+ break;
+ }
+ if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
+ name, namelen))
+ return prop;
+ }
+
+ if (lenp)
+ *lenp = offset;
+ return NULL;
+}
+
+const struct fdt_property *fdt_get_property(const void *fdt,
+ int nodeoffset,
+ const char *name, int *lenp)
+{
+ return fdt_get_property_namelen(fdt, nodeoffset, name,
+ strlen(name), lenp);
+}
+
+const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
+ const char *name, int namelen, int *lenp)
+{
+ const struct fdt_property *prop;
+
+ prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
+ if (! prop)
+ return NULL;
+
+ return prop->data;
+}
+
+const void *fdt_getprop_by_offset(const void *fdt, int offset,
+ const char **namep, int *lenp)
+{
+ const struct fdt_property *prop;
+
+ prop = fdt_get_property_by_offset(fdt, offset, lenp);
+ if (!prop)
+ return NULL;
+ if (namep)
+ *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
+ return prop->data;
+}
+
+const void *fdt_getprop(const void *fdt, int nodeoffset,
+ const char *name, int *lenp)
+{
+ return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
+}
+
+uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
+{
+ const uint32_t *php;
+ int len;
+
+ /* FIXME: This is a bit sub-optimal, since we potentially scan
+ * over all the properties twice. */
+ php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
+ if (!php || (len != sizeof(*php))) {
+ php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
+ if (!php || (len != sizeof(*php)))
+ return 0;
+ }
+
+ return fdt32_to_cpu(*php);
+}
+
+const char *fdt_get_alias_namelen(const void *fdt,
+ const char *name, int namelen)
+{
+ int aliasoffset;
+
+ aliasoffset = fdt_path_offset(fdt, "/aliases");
+ if (aliasoffset < 0)
+ return NULL;
+
+ return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
+}
+
+const char *fdt_get_alias(const void *fdt, const char *name)
+{
+ return fdt_get_alias_namelen(fdt, name, strlen(name));
+}
+
+int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
+{
+ int pdepth = 0, p = 0;
+ int offset, depth, namelen;
+ const char *name;
+
+ FDT_CHECK_HEADER(fdt);
+
+ if (buflen < 2)
+ return -FDT_ERR_NOSPACE;
+
+ for (offset = 0, depth = 0;
+ (offset >= 0) && (offset <= nodeoffset);
+ offset = fdt_next_node(fdt, offset, &depth)) {
+ while (pdepth > depth) {
+ do {
+ p--;
+ } while (buf[p-1] != '/');
+ pdepth--;
+ }
+
+ if (pdepth >= depth) {
+ name = fdt_get_name(fdt, offset, &namelen);
+ if (!name)
+ return namelen;
+ if ((p + namelen + 1) <= buflen) {
+ memcpy(buf + p, name, namelen);
+ p += namelen;
+ buf[p++] = '/';
+ pdepth++;
+ }
+ }
+
+ if (offset == nodeoffset) {
+ if (pdepth < (depth + 1))
+ return -FDT_ERR_NOSPACE;
+
+ if (p > 1) /* special case so that root path is "/", not "" */
+ p--;
+ buf[p] = '\0';
+ return 0;
+ }
+ }
+
+ if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
+ return -FDT_ERR_BADOFFSET;
+ else if (offset == -FDT_ERR_BADOFFSET)
+ return -FDT_ERR_BADSTRUCTURE;
+
+ return offset; /* error from fdt_next_node() */
+}
+
+int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
+ int supernodedepth, int *nodedepth)
+{
+ int offset, depth;
+ int supernodeoffset = -FDT_ERR_INTERNAL;
+
+ FDT_CHECK_HEADER(fdt);
+
+ if (supernodedepth < 0)
+ return -FDT_ERR_NOTFOUND;
+
+ for (offset = 0, depth = 0;
+ (offset >= 0) && (offset <= nodeoffset);
+ offset = fdt_next_node(fdt, offset, &depth)) {
+ if (depth == supernodedepth)
+ supernodeoffset = offset;
+
+ if (offset == nodeoffset) {
+ if (nodedepth)
+ *nodedepth = depth;
+
+ if (supernodedepth > depth)
+ return -FDT_ERR_NOTFOUND;
+ else
+ return supernodeoffset;
+ }
+ }
+
+ if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
+ return -FDT_ERR_BADOFFSET;
+ else if (offset == -FDT_ERR_BADOFFSET)
+ return -FDT_ERR_BADSTRUCTURE;
+
+ return offset; /* error from fdt_next_node() */
+}
+
+int fdt_node_depth(const void *fdt, int nodeoffset)
+{
+ int nodedepth;
+ int err;
+
+ err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
+ if (err)
+ return (err < 0) ? err : -FDT_ERR_INTERNAL;
+ return nodedepth;
+}
+
+int fdt_parent_offset(const void *fdt, int nodeoffset)
+{
+ int nodedepth = fdt_node_depth(fdt, nodeoffset);
+
+ if (nodedepth < 0)
+ return nodedepth;
+ return fdt_supernode_atdepth_offset(fdt, nodeoffset,
+ nodedepth - 1, NULL);
+}
+
+int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
+ const char *propname,
+ const void *propval, int proplen)
+{
+ int offset;
+ const void *val;
+ int len;
+
+ FDT_CHECK_HEADER(fdt);
+
+ /* FIXME: The algorithm here is pretty horrible: we scan each
+ * property of a node in fdt_getprop(), then if that didn't
+ * find what we want, we scan over them again making our way
+ * to the next node. Still it's the easiest to implement
+ * approach; performance can come later. */
+ for (offset = fdt_next_node(fdt, startoffset, NULL);
+ offset >= 0;
+ offset = fdt_next_node(fdt, offset, NULL)) {
+ val = fdt_getprop(fdt, offset, propname, &len);
+ if (val && (len == proplen)
+ && (memcmp(val, propval, len) == 0))
+ return offset;
+ }
+
+ return offset; /* error from fdt_next_node() */
+}
+
+int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
+{
+ int offset;
+
+ if ((phandle == 0) || (phandle == (uint32_t)-1))
+ return -FDT_ERR_BADPHANDLE;
+
+ FDT_CHECK_HEADER(fdt);
+
+ /* FIXME: The algorithm here is pretty horrible: we
+ * potentially scan each property of a node in
+ * fdt_get_phandle(), then if that didn't find what
+ * we want, we scan over them again making our way to the next
+ * node. Still it's the easiest to implement approach;
+ * performance can come later. */
+ for (offset = fdt_next_node(fdt, -1, NULL);
+ offset >= 0;
+ offset = fdt_next_node(fdt, offset, NULL)) {
+ if (fdt_get_phandle(fdt, offset) == phandle)
+ return offset;
+ }
+
+ return offset; /* error from fdt_next_node() */
+}
+
+static int _fdt_stringlist_contains(const char *strlist, int listlen,
+ const char *str)
+{
+ int len = strlen(str);
+ const char *p;
+
+ while (listlen >= len) {
+ if (memcmp(str, strlist, len+1) == 0)
+ return 1;
+ p = memchr(strlist, '\0', listlen);
+ if (!p)
+ return 0; /* malformed strlist.. */
+ listlen -= (p-strlist) + 1;
+ strlist = p + 1;
+ }
+ return 0;
+}
+
+int fdt_node_check_compatible(const void *fdt, int nodeoffset,
+ const char *compatible)
+{
+ const void *prop;
+ int len;
+
+ prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
+ if (!prop)
+ return len;
+ if (_fdt_stringlist_contains(prop, len, compatible))
+ return 0;
+ else
+ return 1;
+}
+
+int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
+ const char *compatible)
+{
+ int offset, err;
+
+ FDT_CHECK_HEADER(fdt);
+
+ /* FIXME: The algorithm here is pretty horrible: we scan each
+ * property of a node in fdt_node_check_compatible(), then if
+ * that didn't find what we want, we scan over them again
+ * making our way to the next node. Still it's the easiest to
+ * implement approach; performance can come later. */
+ for (offset = fdt_next_node(fdt, startoffset, NULL);
+ offset >= 0;
+ offset = fdt_next_node(fdt, offset, NULL)) {
+ err = fdt_node_check_compatible(fdt, offset, compatible);
+ if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
+ return err;
+ else if (err == 0)
+ return offset;
+ }
+
+ return offset; /* error from fdt_next_node() */
+}
diff --git a/EmbeddedPkg/Library/FdtLib/fdt_rw.c b/EmbeddedPkg/Library/FdtLib/fdt_rw.c index 878a775a7b..1d95a73ec1 100644 --- a/EmbeddedPkg/Library/FdtLib/fdt_rw.c +++ b/EmbeddedPkg/Library/FdtLib/fdt_rw.c @@ -1,495 +1,495 @@ -/* - * libfdt - Flat Device Tree manipulation - * Copyright (C) 2006 David Gibson, IBM Corporation. - * - * libfdt is dual licensed: you can use it either under the terms of - * the GPL, or the BSD license, at your option. - * - * a) This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, - * MA 02110-1301 USA - * - * Alternatively, - * - * b) Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "libfdt_env.h" - -#include <fdt.h> -#include <libfdt.h> - -#include "libfdt_internal.h" - -static int _fdt_blocks_misordered(const void *fdt, - int mem_rsv_size, int struct_size) -{ - return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8)) - || (fdt_off_dt_struct(fdt) < - (fdt_off_mem_rsvmap(fdt) + mem_rsv_size)) - || (fdt_off_dt_strings(fdt) < - (fdt_off_dt_struct(fdt) + struct_size)) - || (fdt_totalsize(fdt) < - (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt))); -} - -static int _fdt_rw_check_header(void *fdt) -{ - FDT_CHECK_HEADER(fdt); - - if (fdt_version(fdt) < 17) - return -FDT_ERR_BADVERSION; - if (_fdt_blocks_misordered(fdt, sizeof(struct fdt_reserve_entry), - fdt_size_dt_struct(fdt))) - return -FDT_ERR_BADLAYOUT; - if (fdt_version(fdt) > 17) - fdt_set_version(fdt, 17); - - return 0; -} - -#define FDT_RW_CHECK_HEADER(fdt) \ - { \ - int err; \ - if ((err = _fdt_rw_check_header(fdt)) != 0) \ - return err; \ - } - -static inline int _fdt_data_size(void *fdt) -{ - return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt); -} - -static int _fdt_splice(void *fdt, void *splicepoint, int oldlen, int newlen) -{ - char *p = splicepoint; - char *end = (char *)fdt + _fdt_data_size(fdt); - - if (((p + oldlen) < p) || ((p + oldlen) > end)) - return -FDT_ERR_BADOFFSET; - if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt))) - return -FDT_ERR_NOSPACE; - memmove(p + newlen, p + oldlen, end - p - oldlen); - return 0; -} - -static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p, - int oldn, int newn) -{ - int delta = (newn - oldn) * sizeof(*p); - int err; - err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p)); - if (err) - return err; - fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta); - fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta); - return 0; -} - -static int _fdt_splice_struct(void *fdt, void *p, - int oldlen, int newlen) -{ - int delta = newlen - oldlen; - int err; - - err = _fdt_splice(fdt, p, oldlen, newlen); - if (err) - return err; - - fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta); - fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta); - return 0; -} - -static int _fdt_splice_string(void *fdt, int newlen) -{ - void *p = (char *)fdt - + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt); - int err; - - err = _fdt_splice(fdt, p, 0, newlen); - if (err) - return err; - - fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen); - return 0; -} - -static int _fdt_find_add_string(void *fdt, const char *s) -{ - char *strtab = (char *)fdt + fdt_off_dt_strings(fdt); - const char *p; - char *new; - int len = strlen(s) + 1; - int err; - - p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s); - if (p) - /* found it */ - return (p - strtab); - - new = strtab + fdt_size_dt_strings(fdt); - err = _fdt_splice_string(fdt, len); - if (err) - return err; - - memcpy(new, s, len); - return (new - strtab); -} - -int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size) -{ - struct fdt_reserve_entry *re; - int err; - - FDT_RW_CHECK_HEADER(fdt); - - re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt)); - err = _fdt_splice_mem_rsv(fdt, re, 0, 1); - if (err) - return err; - - re->address = cpu_to_fdt64(address); - re->size = cpu_to_fdt64(size); - return 0; -} - -int fdt_del_mem_rsv(void *fdt, int n) -{ - struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n); - int err; - - FDT_RW_CHECK_HEADER(fdt); - - if (n >= fdt_num_mem_rsv(fdt)) - return -FDT_ERR_NOTFOUND; - - err = _fdt_splice_mem_rsv(fdt, re, 1, 0); - if (err) - return err; - return 0; -} - -static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name, - int len, struct fdt_property **prop) -{ - int oldlen; - int err; - - *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen); - if (! (*prop)) - return oldlen; - - err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen), - FDT_TAGALIGN(len)); - if (err) - return err; - - (*prop)->len = cpu_to_fdt32(len); - return 0; -} - -static int _fdt_add_property(void *fdt, int nodeoffset, const char *name, - int len, struct fdt_property **prop) -{ - int proplen; - int nextoffset; - int namestroff; - int err; - - if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0) - return nextoffset; - - namestroff = _fdt_find_add_string(fdt, name); - if (namestroff < 0) - return namestroff; - - *prop = _fdt_offset_ptr_w(fdt, nextoffset); - proplen = sizeof(**prop) + FDT_TAGALIGN(len); - - err = _fdt_splice_struct(fdt, *prop, 0, proplen); - if (err) - return err; - - (*prop)->tag = cpu_to_fdt32(FDT_PROP); - (*prop)->nameoff = cpu_to_fdt32(namestroff); - (*prop)->len = cpu_to_fdt32(len); - return 0; -} - -int fdt_set_name(void *fdt, int nodeoffset, const char *name) -{ - char *namep; - int oldlen, newlen; - int err; - - FDT_RW_CHECK_HEADER(fdt); - - namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen); - if (!namep) - return oldlen; - - newlen = strlen(name); - - err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1), - FDT_TAGALIGN(newlen+1)); - if (err) - return err; - - memcpy(namep, name, newlen+1); - return 0; -} - -int fdt_setprop(void *fdt, int nodeoffset, const char *name, - const void *val, int len) -{ - struct fdt_property *prop; - int err; - - FDT_RW_CHECK_HEADER(fdt); - - err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop); - if (err == -FDT_ERR_NOTFOUND) - err = _fdt_add_property(fdt, nodeoffset, name, len, &prop); - if (err) - return err; - - memcpy(prop->data, val, len); - return 0; -} - -int fdt_appendprop(void *fdt, int nodeoffset, const char *name, - const void *val, int len) -{ - struct fdt_property *prop; - int err, oldlen, newlen; - - FDT_RW_CHECK_HEADER(fdt); - - prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen); - if (prop) { - newlen = len + oldlen; - err = _fdt_splice_struct(fdt, prop->data, - FDT_TAGALIGN(oldlen), - FDT_TAGALIGN(newlen)); - if (err) - return err; - prop->len = cpu_to_fdt32(newlen); - memcpy(prop->data + oldlen, val, len); - } else { - err = _fdt_add_property(fdt, nodeoffset, name, len, &prop); - if (err) - return err; - memcpy(prop->data, val, len); - } - return 0; -} - -int fdt_delprop(void *fdt, int nodeoffset, const char *name) -{ - struct fdt_property *prop; - int len, proplen; - - FDT_RW_CHECK_HEADER(fdt); - - prop = fdt_get_property_w(fdt, nodeoffset, name, &len); - if (! prop) - return len; - - proplen = sizeof(*prop) + FDT_TAGALIGN(len); - return _fdt_splice_struct(fdt, prop, proplen, 0); -} - -int fdt_add_subnode_namelen(void *fdt, int parentoffset, - const char *name, int namelen) -{ - struct fdt_node_header *nh; - int offset, nextoffset; - int nodelen; - int err; - uint32_t tag; - uint32_t *endtag; - - FDT_RW_CHECK_HEADER(fdt); - - offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen); - if (offset >= 0) - return -FDT_ERR_EXISTS; - else if (offset != -FDT_ERR_NOTFOUND) - return offset; - - /* Try to place the new node after the parent's properties */ - fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */ - do { - offset = nextoffset; - tag = fdt_next_tag(fdt, offset, &nextoffset); - } while ((tag == FDT_PROP) || (tag == FDT_NOP)); - - nh = _fdt_offset_ptr_w(fdt, offset); - nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE; - - err = _fdt_splice_struct(fdt, nh, 0, nodelen); - if (err) - return err; - - nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE); - memset(nh->name, 0, FDT_TAGALIGN(namelen+1)); - memcpy(nh->name, name, namelen); - endtag = (uint32_t *)((char *)nh + nodelen - FDT_TAGSIZE); - *endtag = cpu_to_fdt32(FDT_END_NODE); - - return offset; -} - -int fdt_add_subnode(void *fdt, int parentoffset, const char *name) -{ - return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name)); -} - -int fdt_del_node(void *fdt, int nodeoffset) -{ - int endoffset; - - FDT_RW_CHECK_HEADER(fdt); - - endoffset = _fdt_node_end_offset(fdt, nodeoffset); - if (endoffset < 0) - return endoffset; - - return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset), - endoffset - nodeoffset, 0); -} - -static void _fdt_packblocks(const char *old, char *new, - int mem_rsv_size, int struct_size) -{ - int mem_rsv_off, struct_off, strings_off; - - mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8); - struct_off = mem_rsv_off + mem_rsv_size; - strings_off = struct_off + struct_size; - - memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size); - fdt_set_off_mem_rsvmap(new, mem_rsv_off); - - memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size); - fdt_set_off_dt_struct(new, struct_off); - fdt_set_size_dt_struct(new, struct_size); - - memmove(new + strings_off, old + fdt_off_dt_strings(old), - fdt_size_dt_strings(old)); - fdt_set_off_dt_strings(new, strings_off); - fdt_set_size_dt_strings(new, fdt_size_dt_strings(old)); -} - -int fdt_open_into(const void *fdt, void *buf, int bufsize) -{ - int err; - int mem_rsv_size, struct_size; - int newsize; - const char *fdtstart = fdt; - const char *fdtend = fdtstart + fdt_totalsize(fdt); - char *tmp; - - FDT_CHECK_HEADER(fdt); - - mem_rsv_size = (fdt_num_mem_rsv(fdt)+1) - * sizeof(struct fdt_reserve_entry); - - if (fdt_version(fdt) >= 17) { - struct_size = fdt_size_dt_struct(fdt); - } else { - struct_size = 0; - while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END) - ; - if (struct_size < 0) - return struct_size; - } - - if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) { - /* no further work necessary */ - err = fdt_move(fdt, buf, bufsize); - if (err) - return err; - fdt_set_version(buf, 17); - fdt_set_size_dt_struct(buf, struct_size); - fdt_set_totalsize(buf, bufsize); - return 0; - } - - /* Need to reorder */ - newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size - + struct_size + fdt_size_dt_strings(fdt); - - if (bufsize < newsize) - return -FDT_ERR_NOSPACE; - - /* First attempt to build converted tree at beginning of buffer */ - tmp = buf; - /* But if that overlaps with the old tree... */ - if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) { - /* Try right after the old tree instead */ - tmp = (char *)(uintptr_t)fdtend; - if ((tmp + newsize) > ((char *)buf + bufsize)) - return -FDT_ERR_NOSPACE; - } - - _fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size); - memmove(buf, tmp, newsize); - - fdt_set_magic(buf, FDT_MAGIC); - fdt_set_totalsize(buf, bufsize); - fdt_set_version(buf, 17); - fdt_set_last_comp_version(buf, 16); - fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt)); - - return 0; -} - -int fdt_pack(void *fdt) -{ - int mem_rsv_size; - - FDT_RW_CHECK_HEADER(fdt); - - mem_rsv_size = (fdt_num_mem_rsv(fdt)+1) - * sizeof(struct fdt_reserve_entry); - _fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt)); - fdt_set_totalsize(fdt, _fdt_data_size(fdt)); - - return 0; -} +/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+static int _fdt_blocks_misordered(const void *fdt,
+ int mem_rsv_size, int struct_size)
+{
+ return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
+ || (fdt_off_dt_struct(fdt) <
+ (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
+ || (fdt_off_dt_strings(fdt) <
+ (fdt_off_dt_struct(fdt) + struct_size))
+ || (fdt_totalsize(fdt) <
+ (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
+}
+
+static int _fdt_rw_check_header(void *fdt)
+{
+ FDT_CHECK_HEADER(fdt);
+
+ if (fdt_version(fdt) < 17)
+ return -FDT_ERR_BADVERSION;
+ if (_fdt_blocks_misordered(fdt, sizeof(struct fdt_reserve_entry),
+ fdt_size_dt_struct(fdt)))
+ return -FDT_ERR_BADLAYOUT;
+ if (fdt_version(fdt) > 17)
+ fdt_set_version(fdt, 17);
+
+ return 0;
+}
+
+#define FDT_RW_CHECK_HEADER(fdt) \
+ { \
+ int err; \
+ if ((err = _fdt_rw_check_header(fdt)) != 0) \
+ return err; \
+ }
+
+static inline int _fdt_data_size(void *fdt)
+{
+ return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
+}
+
+static int _fdt_splice(void *fdt, void *splicepoint, int oldlen, int newlen)
+{
+ char *p = splicepoint;
+ char *end = (char *)fdt + _fdt_data_size(fdt);
+
+ if (((p + oldlen) < p) || ((p + oldlen) > end))
+ return -FDT_ERR_BADOFFSET;
+ if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
+ return -FDT_ERR_NOSPACE;
+ memmove(p + newlen, p + oldlen, end - p - oldlen);
+ return 0;
+}
+
+static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p,
+ int oldn, int newn)
+{
+ int delta = (newn - oldn) * sizeof(*p);
+ int err;
+ err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
+ if (err)
+ return err;
+ fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
+ fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
+ return 0;
+}
+
+static int _fdt_splice_struct(void *fdt, void *p,
+ int oldlen, int newlen)
+{
+ int delta = newlen - oldlen;
+ int err;
+
+ err = _fdt_splice(fdt, p, oldlen, newlen);
+ if (err)
+ return err;
+
+ fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
+ fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
+ return 0;
+}
+
+static int _fdt_splice_string(void *fdt, int newlen)
+{
+ void *p = (char *)fdt
+ + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
+ int err;
+
+ err = _fdt_splice(fdt, p, 0, newlen);
+ if (err)
+ return err;
+
+ fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
+ return 0;
+}
+
+static int _fdt_find_add_string(void *fdt, const char *s)
+{
+ char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
+ const char *p;
+ char *new;
+ int len = strlen(s) + 1;
+ int err;
+
+ p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
+ if (p)
+ /* found it */
+ return (p - strtab);
+
+ new = strtab + fdt_size_dt_strings(fdt);
+ err = _fdt_splice_string(fdt, len);
+ if (err)
+ return err;
+
+ memcpy(new, s, len);
+ return (new - strtab);
+}
+
+int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
+{
+ struct fdt_reserve_entry *re;
+ int err;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt));
+ err = _fdt_splice_mem_rsv(fdt, re, 0, 1);
+ if (err)
+ return err;
+
+ re->address = cpu_to_fdt64(address);
+ re->size = cpu_to_fdt64(size);
+ return 0;
+}
+
+int fdt_del_mem_rsv(void *fdt, int n)
+{
+ struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n);
+ int err;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ if (n >= fdt_num_mem_rsv(fdt))
+ return -FDT_ERR_NOTFOUND;
+
+ err = _fdt_splice_mem_rsv(fdt, re, 1, 0);
+ if (err)
+ return err;
+ return 0;
+}
+
+static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name,
+ int len, struct fdt_property **prop)
+{
+ int oldlen;
+ int err;
+
+ *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
+ if (! (*prop))
+ return oldlen;
+
+ err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
+ FDT_TAGALIGN(len));
+ if (err)
+ return err;
+
+ (*prop)->len = cpu_to_fdt32(len);
+ return 0;
+}
+
+static int _fdt_add_property(void *fdt, int nodeoffset, const char *name,
+ int len, struct fdt_property **prop)
+{
+ int proplen;
+ int nextoffset;
+ int namestroff;
+ int err;
+
+ if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
+ return nextoffset;
+
+ namestroff = _fdt_find_add_string(fdt, name);
+ if (namestroff < 0)
+ return namestroff;
+
+ *prop = _fdt_offset_ptr_w(fdt, nextoffset);
+ proplen = sizeof(**prop) + FDT_TAGALIGN(len);
+
+ err = _fdt_splice_struct(fdt, *prop, 0, proplen);
+ if (err)
+ return err;
+
+ (*prop)->tag = cpu_to_fdt32(FDT_PROP);
+ (*prop)->nameoff = cpu_to_fdt32(namestroff);
+ (*prop)->len = cpu_to_fdt32(len);
+ return 0;
+}
+
+int fdt_set_name(void *fdt, int nodeoffset, const char *name)
+{
+ char *namep;
+ int oldlen, newlen;
+ int err;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
+ if (!namep)
+ return oldlen;
+
+ newlen = strlen(name);
+
+ err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1),
+ FDT_TAGALIGN(newlen+1));
+ if (err)
+ return err;
+
+ memcpy(namep, name, newlen+1);
+ return 0;
+}
+
+int fdt_setprop(void *fdt, int nodeoffset, const char *name,
+ const void *val, int len)
+{
+ struct fdt_property *prop;
+ int err;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop);
+ if (err == -FDT_ERR_NOTFOUND)
+ err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
+ if (err)
+ return err;
+
+ memcpy(prop->data, val, len);
+ return 0;
+}
+
+int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
+ const void *val, int len)
+{
+ struct fdt_property *prop;
+ int err, oldlen, newlen;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
+ if (prop) {
+ newlen = len + oldlen;
+ err = _fdt_splice_struct(fdt, prop->data,
+ FDT_TAGALIGN(oldlen),
+ FDT_TAGALIGN(newlen));
+ if (err)
+ return err;
+ prop->len = cpu_to_fdt32(newlen);
+ memcpy(prop->data + oldlen, val, len);
+ } else {
+ err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
+ if (err)
+ return err;
+ memcpy(prop->data, val, len);
+ }
+ return 0;
+}
+
+int fdt_delprop(void *fdt, int nodeoffset, const char *name)
+{
+ struct fdt_property *prop;
+ int len, proplen;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
+ if (! prop)
+ return len;
+
+ proplen = sizeof(*prop) + FDT_TAGALIGN(len);
+ return _fdt_splice_struct(fdt, prop, proplen, 0);
+}
+
+int fdt_add_subnode_namelen(void *fdt, int parentoffset,
+ const char *name, int namelen)
+{
+ struct fdt_node_header *nh;
+ int offset, nextoffset;
+ int nodelen;
+ int err;
+ uint32_t tag;
+ uint32_t *endtag;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
+ if (offset >= 0)
+ return -FDT_ERR_EXISTS;
+ else if (offset != -FDT_ERR_NOTFOUND)
+ return offset;
+
+ /* Try to place the new node after the parent's properties */
+ fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
+ do {
+ offset = nextoffset;
+ tag = fdt_next_tag(fdt, offset, &nextoffset);
+ } while ((tag == FDT_PROP) || (tag == FDT_NOP));
+
+ nh = _fdt_offset_ptr_w(fdt, offset);
+ nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
+
+ err = _fdt_splice_struct(fdt, nh, 0, nodelen);
+ if (err)
+ return err;
+
+ nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
+ memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
+ memcpy(nh->name, name, namelen);
+ endtag = (uint32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
+ *endtag = cpu_to_fdt32(FDT_END_NODE);
+
+ return offset;
+}
+
+int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
+{
+ return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
+}
+
+int fdt_del_node(void *fdt, int nodeoffset)
+{
+ int endoffset;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ endoffset = _fdt_node_end_offset(fdt, nodeoffset);
+ if (endoffset < 0)
+ return endoffset;
+
+ return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
+ endoffset - nodeoffset, 0);
+}
+
+static void _fdt_packblocks(const char *old, char *new,
+ int mem_rsv_size, int struct_size)
+{
+ int mem_rsv_off, struct_off, strings_off;
+
+ mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
+ struct_off = mem_rsv_off + mem_rsv_size;
+ strings_off = struct_off + struct_size;
+
+ memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
+ fdt_set_off_mem_rsvmap(new, mem_rsv_off);
+
+ memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
+ fdt_set_off_dt_struct(new, struct_off);
+ fdt_set_size_dt_struct(new, struct_size);
+
+ memmove(new + strings_off, old + fdt_off_dt_strings(old),
+ fdt_size_dt_strings(old));
+ fdt_set_off_dt_strings(new, strings_off);
+ fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
+}
+
+int fdt_open_into(const void *fdt, void *buf, int bufsize)
+{
+ int err;
+ int mem_rsv_size, struct_size;
+ int newsize;
+ const char *fdtstart = fdt;
+ const char *fdtend = fdtstart + fdt_totalsize(fdt);
+ char *tmp;
+
+ FDT_CHECK_HEADER(fdt);
+
+ mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
+ * sizeof(struct fdt_reserve_entry);
+
+ if (fdt_version(fdt) >= 17) {
+ struct_size = fdt_size_dt_struct(fdt);
+ } else {
+ struct_size = 0;
+ while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
+ ;
+ if (struct_size < 0)
+ return struct_size;
+ }
+
+ if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
+ /* no further work necessary */
+ err = fdt_move(fdt, buf, bufsize);
+ if (err)
+ return err;
+ fdt_set_version(buf, 17);
+ fdt_set_size_dt_struct(buf, struct_size);
+ fdt_set_totalsize(buf, bufsize);
+ return 0;
+ }
+
+ /* Need to reorder */
+ newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
+ + struct_size + fdt_size_dt_strings(fdt);
+
+ if (bufsize < newsize)
+ return -FDT_ERR_NOSPACE;
+
+ /* First attempt to build converted tree at beginning of buffer */
+ tmp = buf;
+ /* But if that overlaps with the old tree... */
+ if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
+ /* Try right after the old tree instead */
+ tmp = (char *)(uintptr_t)fdtend;
+ if ((tmp + newsize) > ((char *)buf + bufsize))
+ return -FDT_ERR_NOSPACE;
+ }
+
+ _fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
+ memmove(buf, tmp, newsize);
+
+ fdt_set_magic(buf, FDT_MAGIC);
+ fdt_set_totalsize(buf, bufsize);
+ fdt_set_version(buf, 17);
+ fdt_set_last_comp_version(buf, 16);
+ fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
+
+ return 0;
+}
+
+int fdt_pack(void *fdt)
+{
+ int mem_rsv_size;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
+ * sizeof(struct fdt_reserve_entry);
+ _fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
+ fdt_set_totalsize(fdt, _fdt_data_size(fdt));
+
+ return 0;
+}
diff --git a/EmbeddedPkg/Library/FdtLib/fdt_strerror.c b/EmbeddedPkg/Library/FdtLib/fdt_strerror.c index e6c3ceee8c..a49793d936 100644 --- a/EmbeddedPkg/Library/FdtLib/fdt_strerror.c +++ b/EmbeddedPkg/Library/FdtLib/fdt_strerror.c @@ -1,96 +1,96 @@ -/* - * libfdt - Flat Device Tree manipulation - * Copyright (C) 2006 David Gibson, IBM Corporation. - * - * libfdt is dual licensed: you can use it either under the terms of - * the GPL, or the BSD license, at your option. - * - * a) This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, - * MA 02110-1301 USA - * - * Alternatively, - * - * b) Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "libfdt_env.h" - -#include <fdt.h> -#include <libfdt.h> - -#include "libfdt_internal.h" - -struct fdt_errtabent { - const char *str; -}; - -#define FDT_ERRTABENT(val) \ - [(val)] = { .str = #val, } - -static struct fdt_errtabent fdt_errtable[] = { - FDT_ERRTABENT(FDT_ERR_NOTFOUND), - FDT_ERRTABENT(FDT_ERR_EXISTS), - FDT_ERRTABENT(FDT_ERR_NOSPACE), - - FDT_ERRTABENT(FDT_ERR_BADOFFSET), - FDT_ERRTABENT(FDT_ERR_BADPATH), - FDT_ERRTABENT(FDT_ERR_BADSTATE), - - FDT_ERRTABENT(FDT_ERR_TRUNCATED), - FDT_ERRTABENT(FDT_ERR_BADMAGIC), - FDT_ERRTABENT(FDT_ERR_BADVERSION), - FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE), - FDT_ERRTABENT(FDT_ERR_BADLAYOUT), -}; -#define FDT_ERRTABSIZE (sizeof(fdt_errtable) / sizeof(fdt_errtable[0])) - -const char *fdt_strerror(int errval) -{ - if (errval > 0) - return "<valid offset/length>"; - else if (errval == 0) - return "<no error>"; - else if (errval > -FDT_ERRTABSIZE) { - const char *s = fdt_errtable[-errval].str; - - if (s) - return s; - } - - return "<unknown error>"; -} +/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+struct fdt_errtabent {
+ const char *str;
+};
+
+#define FDT_ERRTABENT(val) \
+ [(val)] = { .str = #val, }
+
+static struct fdt_errtabent fdt_errtable[] = {
+ FDT_ERRTABENT(FDT_ERR_NOTFOUND),
+ FDT_ERRTABENT(FDT_ERR_EXISTS),
+ FDT_ERRTABENT(FDT_ERR_NOSPACE),
+
+ FDT_ERRTABENT(FDT_ERR_BADOFFSET),
+ FDT_ERRTABENT(FDT_ERR_BADPATH),
+ FDT_ERRTABENT(FDT_ERR_BADSTATE),
+
+ FDT_ERRTABENT(FDT_ERR_TRUNCATED),
+ FDT_ERRTABENT(FDT_ERR_BADMAGIC),
+ FDT_ERRTABENT(FDT_ERR_BADVERSION),
+ FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE),
+ FDT_ERRTABENT(FDT_ERR_BADLAYOUT),
+};
+#define FDT_ERRTABSIZE (sizeof(fdt_errtable) / sizeof(fdt_errtable[0]))
+
+const char *fdt_strerror(int errval)
+{
+ if (errval > 0)
+ return "<valid offset/length>";
+ else if (errval == 0)
+ return "<no error>";
+ else if (errval > -FDT_ERRTABSIZE) {
+ const char *s = fdt_errtable[-errval].str;
+
+ if (s)
+ return s;
+ }
+
+ return "<unknown error>";
+}
diff --git a/EmbeddedPkg/Library/FdtLib/fdt_sw.c b/EmbeddedPkg/Library/FdtLib/fdt_sw.c index 55ebebf1eb..240fabc554 100644 --- a/EmbeddedPkg/Library/FdtLib/fdt_sw.c +++ b/EmbeddedPkg/Library/FdtLib/fdt_sw.c @@ -1,256 +1,256 @@ -/* - * libfdt - Flat Device Tree manipulation - * Copyright (C) 2006 David Gibson, IBM Corporation. - * - * libfdt is dual licensed: you can use it either under the terms of - * the GPL, or the BSD license, at your option. - * - * a) This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, - * MA 02110-1301 USA - * - * Alternatively, - * - * b) Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "libfdt_env.h" - -#include <fdt.h> -#include <libfdt.h> - -#include "libfdt_internal.h" - -static int _fdt_sw_check_header(void *fdt) -{ - if (fdt_magic(fdt) != FDT_SW_MAGIC) - return -FDT_ERR_BADMAGIC; - /* FIXME: should check more details about the header state */ - return 0; -} - -#define FDT_SW_CHECK_HEADER(fdt) \ - { \ - int err; \ - if ((err = _fdt_sw_check_header(fdt)) != 0) \ - return err; \ - } - -static void *_fdt_grab_space(void *fdt, size_t len) -{ - int offset = fdt_size_dt_struct(fdt); - int spaceleft; - - spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt) - - fdt_size_dt_strings(fdt); - - if ((offset + len < offset) || (offset + len > spaceleft)) - return NULL; - - fdt_set_size_dt_struct(fdt, offset + len); - return _fdt_offset_ptr_w(fdt, offset); -} - -int fdt_create(void *buf, int bufsize) -{ - void *fdt = buf; - - if (bufsize < sizeof(struct fdt_header)) - return -FDT_ERR_NOSPACE; - - memset(buf, 0, bufsize); - - fdt_set_magic(fdt, FDT_SW_MAGIC); - fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION); - fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION); - fdt_set_totalsize(fdt, bufsize); - - fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header), - sizeof(struct fdt_reserve_entry))); - fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt)); - fdt_set_off_dt_strings(fdt, bufsize); - - return 0; -} - -int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size) -{ - struct fdt_reserve_entry *re; - int offset; - - FDT_SW_CHECK_HEADER(fdt); - - if (fdt_size_dt_struct(fdt)) - return -FDT_ERR_BADSTATE; - - offset = fdt_off_dt_struct(fdt); - if ((offset + sizeof(*re)) > fdt_totalsize(fdt)) - return -FDT_ERR_NOSPACE; - - re = (struct fdt_reserve_entry *)((char *)fdt + offset); - re->address = cpu_to_fdt64(addr); - re->size = cpu_to_fdt64(size); - - fdt_set_off_dt_struct(fdt, offset + sizeof(*re)); - - return 0; -} - -int fdt_finish_reservemap(void *fdt) -{ - return fdt_add_reservemap_entry(fdt, 0, 0); -} - -int fdt_begin_node(void *fdt, const char *name) -{ - struct fdt_node_header *nh; - int namelen = strlen(name) + 1; - - FDT_SW_CHECK_HEADER(fdt); - - nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen)); - if (! nh) - return -FDT_ERR_NOSPACE; - - nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE); - memcpy(nh->name, name, namelen); - return 0; -} - -int fdt_end_node(void *fdt) -{ - uint32_t *en; - - FDT_SW_CHECK_HEADER(fdt); - - en = _fdt_grab_space(fdt, FDT_TAGSIZE); - if (! en) - return -FDT_ERR_NOSPACE; - - *en = cpu_to_fdt32(FDT_END_NODE); - return 0; -} - -static int _fdt_find_add_string(void *fdt, const char *s) -{ - char *strtab = (char *)fdt + fdt_totalsize(fdt); - const char *p; - int strtabsize = fdt_size_dt_strings(fdt); - int len = strlen(s) + 1; - int struct_top, offset; - - p = _fdt_find_string(strtab - strtabsize, strtabsize, s); - if (p) - return p - strtab; - - /* Add it */ - offset = -strtabsize - len; - struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); - if (fdt_totalsize(fdt) + offset < struct_top) - return 0; /* no more room :( */ - - memcpy(strtab + offset, s, len); - fdt_set_size_dt_strings(fdt, strtabsize + len); - return offset; -} - -int fdt_property(void *fdt, const char *name, const void *val, int len) -{ - struct fdt_property *prop; - int nameoff; - - FDT_SW_CHECK_HEADER(fdt); - - nameoff = _fdt_find_add_string(fdt, name); - if (nameoff == 0) - return -FDT_ERR_NOSPACE; - - prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len)); - if (! prop) - return -FDT_ERR_NOSPACE; - - prop->tag = cpu_to_fdt32(FDT_PROP); - prop->nameoff = cpu_to_fdt32(nameoff); - prop->len = cpu_to_fdt32(len); - memcpy(prop->data, val, len); - return 0; -} - -int fdt_finish(void *fdt) -{ - char *p = (char *)fdt; - uint32_t *end; - int oldstroffset, newstroffset; - uint32_t tag; - int offset, nextoffset; - - FDT_SW_CHECK_HEADER(fdt); - - /* Add terminator */ - end = _fdt_grab_space(fdt, sizeof(*end)); - if (! end) - return -FDT_ERR_NOSPACE; - *end = cpu_to_fdt32(FDT_END); - - /* Relocate the string table */ - oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt); - newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); - memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt)); - fdt_set_off_dt_strings(fdt, newstroffset); - - /* Walk the structure, correcting string offsets */ - offset = 0; - while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) { - if (tag == FDT_PROP) { - struct fdt_property *prop = - _fdt_offset_ptr_w(fdt, offset); - int nameoff; - - nameoff = fdt32_to_cpu(prop->nameoff); - nameoff += fdt_size_dt_strings(fdt); - prop->nameoff = cpu_to_fdt32(nameoff); - } - offset = nextoffset; - } - if (nextoffset < 0) - return nextoffset; - - /* Finally, adjust the header */ - fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt)); - fdt_set_magic(fdt, FDT_MAGIC); - return 0; -} +/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+static int _fdt_sw_check_header(void *fdt)
+{
+ if (fdt_magic(fdt) != FDT_SW_MAGIC)
+ return -FDT_ERR_BADMAGIC;
+ /* FIXME: should check more details about the header state */
+ return 0;
+}
+
+#define FDT_SW_CHECK_HEADER(fdt) \
+ { \
+ int err; \
+ if ((err = _fdt_sw_check_header(fdt)) != 0) \
+ return err; \
+ }
+
+static void *_fdt_grab_space(void *fdt, size_t len)
+{
+ int offset = fdt_size_dt_struct(fdt);
+ int spaceleft;
+
+ spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
+ - fdt_size_dt_strings(fdt);
+
+ if ((offset + len < offset) || (offset + len > spaceleft))
+ return NULL;
+
+ fdt_set_size_dt_struct(fdt, offset + len);
+ return _fdt_offset_ptr_w(fdt, offset);
+}
+
+int fdt_create(void *buf, int bufsize)
+{
+ void *fdt = buf;
+
+ if (bufsize < sizeof(struct fdt_header))
+ return -FDT_ERR_NOSPACE;
+
+ memset(buf, 0, bufsize);
+
+ fdt_set_magic(fdt, FDT_SW_MAGIC);
+ fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
+ fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
+ fdt_set_totalsize(fdt, bufsize);
+
+ fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header),
+ sizeof(struct fdt_reserve_entry)));
+ fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
+ fdt_set_off_dt_strings(fdt, bufsize);
+
+ return 0;
+}
+
+int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
+{
+ struct fdt_reserve_entry *re;
+ int offset;
+
+ FDT_SW_CHECK_HEADER(fdt);
+
+ if (fdt_size_dt_struct(fdt))
+ return -FDT_ERR_BADSTATE;
+
+ offset = fdt_off_dt_struct(fdt);
+ if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
+ return -FDT_ERR_NOSPACE;
+
+ re = (struct fdt_reserve_entry *)((char *)fdt + offset);
+ re->address = cpu_to_fdt64(addr);
+ re->size = cpu_to_fdt64(size);
+
+ fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
+
+ return 0;
+}
+
+int fdt_finish_reservemap(void *fdt)
+{
+ return fdt_add_reservemap_entry(fdt, 0, 0);
+}
+
+int fdt_begin_node(void *fdt, const char *name)
+{
+ struct fdt_node_header *nh;
+ int namelen = strlen(name) + 1;
+
+ FDT_SW_CHECK_HEADER(fdt);
+
+ nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
+ if (! nh)
+ return -FDT_ERR_NOSPACE;
+
+ nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
+ memcpy(nh->name, name, namelen);
+ return 0;
+}
+
+int fdt_end_node(void *fdt)
+{
+ uint32_t *en;
+
+ FDT_SW_CHECK_HEADER(fdt);
+
+ en = _fdt_grab_space(fdt, FDT_TAGSIZE);
+ if (! en)
+ return -FDT_ERR_NOSPACE;
+
+ *en = cpu_to_fdt32(FDT_END_NODE);
+ return 0;
+}
+
+static int _fdt_find_add_string(void *fdt, const char *s)
+{
+ char *strtab = (char *)fdt + fdt_totalsize(fdt);
+ const char *p;
+ int strtabsize = fdt_size_dt_strings(fdt);
+ int len = strlen(s) + 1;
+ int struct_top, offset;
+
+ p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
+ if (p)
+ return p - strtab;
+
+ /* Add it */
+ offset = -strtabsize - len;
+ struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
+ if (fdt_totalsize(fdt) + offset < struct_top)
+ return 0; /* no more room :( */
+
+ memcpy(strtab + offset, s, len);
+ fdt_set_size_dt_strings(fdt, strtabsize + len);
+ return offset;
+}
+
+int fdt_property(void *fdt, const char *name, const void *val, int len)
+{
+ struct fdt_property *prop;
+ int nameoff;
+
+ FDT_SW_CHECK_HEADER(fdt);
+
+ nameoff = _fdt_find_add_string(fdt, name);
+ if (nameoff == 0)
+ return -FDT_ERR_NOSPACE;
+
+ prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
+ if (! prop)
+ return -FDT_ERR_NOSPACE;
+
+ prop->tag = cpu_to_fdt32(FDT_PROP);
+ prop->nameoff = cpu_to_fdt32(nameoff);
+ prop->len = cpu_to_fdt32(len);
+ memcpy(prop->data, val, len);
+ return 0;
+}
+
+int fdt_finish(void *fdt)
+{
+ char *p = (char *)fdt;
+ uint32_t *end;
+ int oldstroffset, newstroffset;
+ uint32_t tag;
+ int offset, nextoffset;
+
+ FDT_SW_CHECK_HEADER(fdt);
+
+ /* Add terminator */
+ end = _fdt_grab_space(fdt, sizeof(*end));
+ if (! end)
+ return -FDT_ERR_NOSPACE;
+ *end = cpu_to_fdt32(FDT_END);
+
+ /* Relocate the string table */
+ oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
+ newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
+ memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
+ fdt_set_off_dt_strings(fdt, newstroffset);
+
+ /* Walk the structure, correcting string offsets */
+ offset = 0;
+ while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
+ if (tag == FDT_PROP) {
+ struct fdt_property *prop =
+ _fdt_offset_ptr_w(fdt, offset);
+ int nameoff;
+
+ nameoff = fdt32_to_cpu(prop->nameoff);
+ nameoff += fdt_size_dt_strings(fdt);
+ prop->nameoff = cpu_to_fdt32(nameoff);
+ }
+ offset = nextoffset;
+ }
+ if (nextoffset < 0)
+ return nextoffset;
+
+ /* Finally, adjust the header */
+ fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
+ fdt_set_magic(fdt, FDT_MAGIC);
+ return 0;
+}
diff --git a/EmbeddedPkg/Library/FdtLib/fdt_wip.c b/EmbeddedPkg/Library/FdtLib/fdt_wip.c index 6025fa1fe8..e009a88f51 100644 --- a/EmbeddedPkg/Library/FdtLib/fdt_wip.c +++ b/EmbeddedPkg/Library/FdtLib/fdt_wip.c @@ -1,118 +1,118 @@ -/* - * libfdt - Flat Device Tree manipulation - * Copyright (C) 2006 David Gibson, IBM Corporation. - * - * libfdt is dual licensed: you can use it either under the terms of - * the GPL, or the BSD license, at your option. - * - * a) This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, - * MA 02110-1301 USA - * - * Alternatively, - * - * b) Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "libfdt_env.h" - -#include <fdt.h> -#include <libfdt.h> - -#include "libfdt_internal.h" - -int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, - const void *val, int len) -{ - void *propval; - int proplen; - - propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen); - if (! propval) - return proplen; - - if (proplen != len) - return -FDT_ERR_NOSPACE; - - memcpy(propval, val, len); - return 0; -} - -static void _fdt_nop_region(void *start, int len) -{ - uint32_t *p; - - for (p = start; (char *)p < ((char *)start + len); p++) - *p = cpu_to_fdt32(FDT_NOP); -} - -int fdt_nop_property(void *fdt, int nodeoffset, const char *name) -{ - struct fdt_property *prop; - int len; - - prop = fdt_get_property_w(fdt, nodeoffset, name, &len); - if (! prop) - return len; - - _fdt_nop_region(prop, len + sizeof(*prop)); - - return 0; -} - -int _fdt_node_end_offset(void *fdt, int offset) -{ - int depth = 0; - - while ((offset >= 0) && (depth >= 0)) - offset = fdt_next_node(fdt, offset, &depth); - - return offset; -} - -int fdt_nop_node(void *fdt, int nodeoffset) -{ - int endoffset; - - endoffset = _fdt_node_end_offset(fdt, nodeoffset); - if (endoffset < 0) - return endoffset; - - _fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0), - endoffset - nodeoffset); - return 0; -} +/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
+ const void *val, int len)
+{
+ void *propval;
+ int proplen;
+
+ propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen);
+ if (! propval)
+ return proplen;
+
+ if (proplen != len)
+ return -FDT_ERR_NOSPACE;
+
+ memcpy(propval, val, len);
+ return 0;
+}
+
+static void _fdt_nop_region(void *start, int len)
+{
+ uint32_t *p;
+
+ for (p = start; (char *)p < ((char *)start + len); p++)
+ *p = cpu_to_fdt32(FDT_NOP);
+}
+
+int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
+{
+ struct fdt_property *prop;
+ int len;
+
+ prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
+ if (! prop)
+ return len;
+
+ _fdt_nop_region(prop, len + sizeof(*prop));
+
+ return 0;
+}
+
+int _fdt_node_end_offset(void *fdt, int offset)
+{
+ int depth = 0;
+
+ while ((offset >= 0) && (depth >= 0))
+ offset = fdt_next_node(fdt, offset, &depth);
+
+ return offset;
+}
+
+int fdt_nop_node(void *fdt, int nodeoffset)
+{
+ int endoffset;
+
+ endoffset = _fdt_node_end_offset(fdt, nodeoffset);
+ if (endoffset < 0)
+ return endoffset;
+
+ _fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0),
+ endoffset - nodeoffset);
+ return 0;
+}
diff --git a/EmbeddedPkg/Library/FdtLib/libfdt_internal.h b/EmbeddedPkg/Library/FdtLib/libfdt_internal.h index 381133ba81..15456110a3 100644 --- a/EmbeddedPkg/Library/FdtLib/libfdt_internal.h +++ b/EmbeddedPkg/Library/FdtLib/libfdt_internal.h @@ -1,95 +1,95 @@ -#ifndef _LIBFDT_INTERNAL_H -#define _LIBFDT_INTERNAL_H -/* - * libfdt - Flat Device Tree manipulation - * Copyright (C) 2006 David Gibson, IBM Corporation. - * - * libfdt is dual licensed: you can use it either under the terms of - * the GPL, or the BSD license, at your option. - * - * a) This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, - * MA 02110-1301 USA - * - * Alternatively, - * - * b) Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include <fdt.h> - -#define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) -#define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE)) - -#define FDT_CHECK_HEADER(fdt) \ - { \ - int err; \ - if ((err = fdt_check_header(fdt)) != 0) \ - return err; \ - } - -int _fdt_check_node_offset(const void *fdt, int offset); -int _fdt_check_prop_offset(const void *fdt, int offset); -const char *_fdt_find_string(const char *strtab, int tabsize, const char *s); -int _fdt_node_end_offset(void *fdt, int nodeoffset); - -static inline const void *_fdt_offset_ptr(const void *fdt, int offset) -{ - return (const char *)fdt + fdt_off_dt_struct(fdt) + offset; -} - -static inline void *_fdt_offset_ptr_w(void *fdt, int offset) -{ - return (void *)(uintptr_t)_fdt_offset_ptr(fdt, offset); -} - -static inline const struct fdt_reserve_entry *_fdt_mem_rsv(const void *fdt, int n) -{ - const struct fdt_reserve_entry *rsv_table = - (const struct fdt_reserve_entry *) - ((const char *)fdt + fdt_off_mem_rsvmap(fdt)); - - return rsv_table + n; -} -static inline struct fdt_reserve_entry *_fdt_mem_rsv_w(void *fdt, int n) -{ - return (void *)(uintptr_t)_fdt_mem_rsv(fdt, n); -} - -#define FDT_SW_MAGIC (~FDT_MAGIC) - -#endif /* _LIBFDT_INTERNAL_H */ +#ifndef _LIBFDT_INTERNAL_H
+#define _LIBFDT_INTERNAL_H
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <fdt.h>
+
+#define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
+#define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE))
+
+#define FDT_CHECK_HEADER(fdt) \
+ { \
+ int err; \
+ if ((err = fdt_check_header(fdt)) != 0) \
+ return err; \
+ }
+
+int _fdt_check_node_offset(const void *fdt, int offset);
+int _fdt_check_prop_offset(const void *fdt, int offset);
+const char *_fdt_find_string(const char *strtab, int tabsize, const char *s);
+int _fdt_node_end_offset(void *fdt, int nodeoffset);
+
+static inline const void *_fdt_offset_ptr(const void *fdt, int offset)
+{
+ return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
+}
+
+static inline void *_fdt_offset_ptr_w(void *fdt, int offset)
+{
+ return (void *)(uintptr_t)_fdt_offset_ptr(fdt, offset);
+}
+
+static inline const struct fdt_reserve_entry *_fdt_mem_rsv(const void *fdt, int n)
+{
+ const struct fdt_reserve_entry *rsv_table =
+ (const struct fdt_reserve_entry *)
+ ((const char *)fdt + fdt_off_mem_rsvmap(fdt));
+
+ return rsv_table + n;
+}
+static inline struct fdt_reserve_entry *_fdt_mem_rsv_w(void *fdt, int n)
+{
+ return (void *)(uintptr_t)_fdt_mem_rsv(fdt, n);
+}
+
+#define FDT_SW_MAGIC (~FDT_MAGIC)
+
+#endif /* _LIBFDT_INTERNAL_H */
diff --git a/EmbeddedPkg/Library/GdbDebugAgent/Arm/ExceptionSupport.ARMv6.S b/EmbeddedPkg/Library/GdbDebugAgent/Arm/ExceptionSupport.ARMv6.S index f8c4e296ca..9c3cc3ce3c 100755 --- a/EmbeddedPkg/Library/GdbDebugAgent/Arm/ExceptionSupport.ARMv6.S +++ b/EmbeddedPkg/Library/GdbDebugAgent/Arm/ExceptionSupport.ARMv6.S @@ -1,258 +1,258 @@ -#------------------------------------------------------------------------------ -# -# Use ARMv6 instruction to operate on a single stack -# -# Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR> -# -# 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. -# -#------------------------------------------------------------------------------ - -/* - -This is the stack constructed by the exception handler (low address to high address) - # R0 - IFAR is EFI_SYSTEM_CONTEXT for ARM - Reg Offset - === ====== - R0 0x00 # stmfd SP!,{R0-R12} - R1 0x04 - R2 0x08 - R3 0x0c - R4 0x10 - R5 0x14 - R6 0x18 - R7 0x1c - R8 0x20 - R9 0x24 - R10 0x28 - R11 0x2c - R12 0x30 - SP 0x34 # reserved via adding 0x20 (32) to the SP - LR 0x38 - PC 0x3c - CPSR 0x40 - DFSR 0x44 - DFAR 0x48 - IFSR 0x4c - IFAR 0x50 - - LR 0x54 # SVC Link register (we need to restore it) - - LR 0x58 # pushed by srsfd - CPSR 0x5c - - */ - - -GCC_ASM_EXPORT(ExceptionHandlersStart) -GCC_ASM_EXPORT(ExceptionHandlersEnd) -GCC_ASM_EXPORT(CommonExceptionEntry) -GCC_ASM_EXPORT(AsmCommonExceptionEntry) -GCC_ASM_EXPORT(GdbExceptionHandler) - -.text -.align 3 - - -// -// This code gets copied to the ARM vector table -// ExceptionHandlersStart - ExceptionHandlersEnd gets copied -// -ASM_PFX(ExceptionHandlersStart): - -ASM_PFX(Reset): - b ASM_PFX(Reset) - -ASM_PFX(UndefinedInstruction): - b ASM_PFX(UndefinedInstructionEntry) - -ASM_PFX(SoftwareInterrupt): - b ASM_PFX(SoftwareInterruptEntry) - -ASM_PFX(PrefetchAbort): - b ASM_PFX(PrefetchAbortEntry) - -ASM_PFX(DataAbort): - b ASM_PFX(DataAbortEntry) - -ASM_PFX(ReservedException): - b ASM_PFX(ReservedExceptionEntry) - -ASM_PFX(Irq): - b ASM_PFX(Irq) - -ASM_PFX(Fiq): - b ASM_PFX(FiqEntry) - - -ASM_PFX(UndefinedInstructionEntry): - sub LR, LR, #4 @ Only -2 for Thumb, adjust in CommonExceptionEntry - srsdb #0x13! @ Store return state on SVC stack - cpsid f,#0x13 @ Switch to SVC for common stack - stmfd SP!,{LR} @ Store the link register for the current mode - sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} @ Store the register state - - mov R0,#1 @ ExceptionType - ldr R1,ASM_PFX(CommonExceptionEntry) - bx R1 - -ASM_PFX(SoftwareInterruptEntry): - sub LR, LR, #4 @ Only -2 for Thumb, adjust in CommonExceptionEntry - srsdb #0x13! @ Store return state on SVC stack - cpsid f @ We are already in SVC mode - stmfd SP!,{LR} @ Store the link register for the current mode - sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} @ Store the register state - - mov R0,#2 @ ExceptionType - ldr R1,ASM_PFX(CommonExceptionEntry) - bx R1 - -ASM_PFX(PrefetchAbortEntry): - sub LR,LR,#4 - srsdb #0x13! @ Store return state on SVC stack - cpsid f,#0x13 @ Switch to SVC for common stack - stmfd SP!,{LR} @ Store the link register for the current mode - sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} @ Store the register state - - mov R0,#3 @ ExceptionType - ldr R1,ASM_PFX(CommonExceptionEntry) - bx R1 - -ASM_PFX(DataAbortEntry): - sub LR,LR,#8 - srsdb #0x13! @ Store return state on SVC stack - cpsid f,#0x13 @ Switch to SVC for common stack - stmfd SP!,{LR} @ Store the link register for the current mode - sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} @ Store the register state - - mov R0,#4 - ldr R1,ASM_PFX(CommonExceptionEntry) - bx R1 - -ASM_PFX(ReservedExceptionEntry): - srsdb #0x13! @ Store return state on SVC stack - cpsid f,#0x13 @ Switch to SVC for common stack - stmfd SP!,{LR} @ Store the link register for the current mode - sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} @ Store the register state - - mov R0,#5 - ldr R1,ASM_PFX(CommonExceptionEntry) - bx R1 - -ASM_PFX(FiqEntry): - sub LR,LR,#4 - srsdb #0x13! @ Store return state on SVC stack - cps #0x13 @ Switch to SVC for common stack - stmfd SP!,{LR} @ Store the link register for the current mode - sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} @ Store the register state - @ Since we have already switch to SVC R8_fiq - R12_fiq - @ never get used or saved - mov R0,#7 @ ExceptionType - ldr R1,ASM_PFX(CommonExceptionEntry) - bx R1 - -// -// This gets patched by the C code that patches in the vector table -// -ASM_PFX(CommonExceptionEntry): - .byte 0x12 - .byte 0x34 - .byte 0x56 - .byte 0x78 - -ASM_PFX(ExceptionHandlersEnd): - -// -// This code runs from CpuDxe driver loaded address. It is patched into -// CommonExceptionEntry. -// -ASM_PFX(AsmCommonExceptionEntry): - mrc p15, 0, R1, c6, c0, 2 @ Read IFAR - str R1, [SP, #0x50] @ Store it in EFI_SYSTEM_CONTEXT_ARM.IFAR - - mrc p15, 0, R1, c5, c0, 1 @ Read IFSR - str R1, [SP, #0x4c] @ Store it in EFI_SYSTEM_CONTEXT_ARM.IFSR - - mrc p15, 0, R1, c6, c0, 0 @ Read DFAR - str R1, [SP, #0x48] @ Store it in EFI_SYSTEM_CONTEXT_ARM.DFAR - - mrc p15, 0, R1, c5, c0, 0 @ Read DFSR - str R1, [SP, #0x44] @ Store it in EFI_SYSTEM_CONTEXT_ARM.DFSR - - ldr R1, [SP, #0x5c] @ srsdb saved pre-exception CPSR on the stack - str R1, [SP, #0x40] @ Store it in EFI_SYSTEM_CONTEXT_ARM.CPSR - - add R2, SP, #0x38 @ Make R2 point to EFI_SYSTEM_CONTEXT_ARM.LR - and R3, R1, #0x1f @ Check CPSR to see if User or System Mode - cmp R3, #0x1f @ if ((CPSR == 0x10) || (CPSR == 0x1df)) - cmpne R3, #0x10 @ - stmeqed R2, {lr}^ @ save unbanked lr - @ else - stmneed R2, {lr} @ save SVC lr - - - ldr R5, [SP, #0x58] @ PC is the LR pushed by srsfd - @ Check to see if we have to adjust for Thumb entry - sub r4, r0, #1 @ if (ExceptionType == 1 || ExceptionType ==2)) { - cmp r4, #1 @ // UND & SVC have differnt LR adjust for Thumb - bhi NoAdjustNeeded - - tst r1, #0x20 @ if ((CPSR & T)) == T) { // Thumb Mode on entry - addne R5, R5, #2 @ PC += 2@ - str R5,[SP,#0x58] @ Update LR value pused by srsfd - -NoAdjustNeeded: - - str R5, [SP, #0x3c] @ Store it in EFI_SYSTEM_CONTEXT_ARM.PC - - sub R1, SP, #0x60 @ We pused 0x60 bytes on the stack - str R1, [SP, #0x34] @ Store it in EFI_SYSTEM_CONTEXT_ARM.SP - - @ R0 is ExceptionType - mov R1,SP @ R1 is SystemContext - -/* -VOID -EFIAPI -GdbExceptionHandler ( - IN EFI_EXCEPTION_TYPE ExceptionType, R0 - IN OUT EFI_SYSTEM_CONTEXT SystemContext R1 - ) - -*/ - blx ASM_PFX(GdbExceptionHandler) @ Call exception handler - - ldr R1,[SP,#0x3c] @ EFI_SYSTEM_CONTEXT_ARM.PC - str R1,[SP,#0x58] @ Store it back to srsfd stack slot so it can be restored - - ldr R1,[SP,#0x40] @ EFI_SYSTEM_CONTEXT_ARM.CPSR - str R1,[SP,#0x5c] @ Store it back to srsfd stack slot so it can be restored - - add R3, SP, #0x54 @ Make R3 point to SVC LR saved on entry - add R2, SP, #0x38 @ Make R2 point to EFI_SYSTEM_CONTEXT_ARM.LR - and R1, R1, #0x1f @ Check to see if User or System Mode - cmp R1, #0x1f @ if ((CPSR == 0x10) || (CPSR == 0x1f)) - cmpne R1, #0x10 @ - ldmeqed R2, {lr}^ @ restore unbanked lr - @ else - ldmneed R3, {lr} @ restore SVC lr, via ldmfd SP!, {LR} - - ldmfd SP!,{R0-R12} @ Restore general purpose registers - @ Exception handler can not change SP - - add SP,SP,#0x20 @ Clear out the remaining stack space - ldmfd SP!,{LR} @ restore the link register for this context - rfefd SP! @ return from exception via srsfd stack slot - +#------------------------------------------------------------------------------
+#
+# Use ARMv6 instruction to operate on a single stack
+#
+# Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
+#
+# 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.
+#
+#------------------------------------------------------------------------------
+
+/*
+
+This is the stack constructed by the exception handler (low address to high address)
+ # R0 - IFAR is EFI_SYSTEM_CONTEXT for ARM
+ Reg Offset
+ === ======
+ R0 0x00 # stmfd SP!,{R0-R12}
+ R1 0x04
+ R2 0x08
+ R3 0x0c
+ R4 0x10
+ R5 0x14
+ R6 0x18
+ R7 0x1c
+ R8 0x20
+ R9 0x24
+ R10 0x28
+ R11 0x2c
+ R12 0x30
+ SP 0x34 # reserved via adding 0x20 (32) to the SP
+ LR 0x38
+ PC 0x3c
+ CPSR 0x40
+ DFSR 0x44
+ DFAR 0x48
+ IFSR 0x4c
+ IFAR 0x50
+
+ LR 0x54 # SVC Link register (we need to restore it)
+
+ LR 0x58 # pushed by srsfd
+ CPSR 0x5c
+
+ */
+
+
+GCC_ASM_EXPORT(ExceptionHandlersStart)
+GCC_ASM_EXPORT(ExceptionHandlersEnd)
+GCC_ASM_EXPORT(CommonExceptionEntry)
+GCC_ASM_EXPORT(AsmCommonExceptionEntry)
+GCC_ASM_EXPORT(GdbExceptionHandler)
+
+.text
+.align 3
+
+
+//
+// This code gets copied to the ARM vector table
+// ExceptionHandlersStart - ExceptionHandlersEnd gets copied
+//
+ASM_PFX(ExceptionHandlersStart):
+
+ASM_PFX(Reset):
+ b ASM_PFX(Reset)
+
+ASM_PFX(UndefinedInstruction):
+ b ASM_PFX(UndefinedInstructionEntry)
+
+ASM_PFX(SoftwareInterrupt):
+ b ASM_PFX(SoftwareInterruptEntry)
+
+ASM_PFX(PrefetchAbort):
+ b ASM_PFX(PrefetchAbortEntry)
+
+ASM_PFX(DataAbort):
+ b ASM_PFX(DataAbortEntry)
+
+ASM_PFX(ReservedException):
+ b ASM_PFX(ReservedExceptionEntry)
+
+ASM_PFX(Irq):
+ b ASM_PFX(Irq)
+
+ASM_PFX(Fiq):
+ b ASM_PFX(FiqEntry)
+
+
+ASM_PFX(UndefinedInstructionEntry):
+ sub LR, LR, #4 @ Only -2 for Thumb, adjust in CommonExceptionEntry
+ srsdb #0x13! @ Store return state on SVC stack
+ cpsid f,#0x13 @ Switch to SVC for common stack
+ stmfd SP!,{LR} @ Store the link register for the current mode
+ sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} @ Store the register state
+
+ mov R0,#1 @ ExceptionType
+ ldr R1,ASM_PFX(CommonExceptionEntry)
+ bx R1
+
+ASM_PFX(SoftwareInterruptEntry):
+ sub LR, LR, #4 @ Only -2 for Thumb, adjust in CommonExceptionEntry
+ srsdb #0x13! @ Store return state on SVC stack
+ cpsid f @ We are already in SVC mode
+ stmfd SP!,{LR} @ Store the link register for the current mode
+ sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} @ Store the register state
+
+ mov R0,#2 @ ExceptionType
+ ldr R1,ASM_PFX(CommonExceptionEntry)
+ bx R1
+
+ASM_PFX(PrefetchAbortEntry):
+ sub LR,LR,#4
+ srsdb #0x13! @ Store return state on SVC stack
+ cpsid f,#0x13 @ Switch to SVC for common stack
+ stmfd SP!,{LR} @ Store the link register for the current mode
+ sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} @ Store the register state
+
+ mov R0,#3 @ ExceptionType
+ ldr R1,ASM_PFX(CommonExceptionEntry)
+ bx R1
+
+ASM_PFX(DataAbortEntry):
+ sub LR,LR,#8
+ srsdb #0x13! @ Store return state on SVC stack
+ cpsid f,#0x13 @ Switch to SVC for common stack
+ stmfd SP!,{LR} @ Store the link register for the current mode
+ sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} @ Store the register state
+
+ mov R0,#4
+ ldr R1,ASM_PFX(CommonExceptionEntry)
+ bx R1
+
+ASM_PFX(ReservedExceptionEntry):
+ srsdb #0x13! @ Store return state on SVC stack
+ cpsid f,#0x13 @ Switch to SVC for common stack
+ stmfd SP!,{LR} @ Store the link register for the current mode
+ sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} @ Store the register state
+
+ mov R0,#5
+ ldr R1,ASM_PFX(CommonExceptionEntry)
+ bx R1
+
+ASM_PFX(FiqEntry):
+ sub LR,LR,#4
+ srsdb #0x13! @ Store return state on SVC stack
+ cps #0x13 @ Switch to SVC for common stack
+ stmfd SP!,{LR} @ Store the link register for the current mode
+ sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} @ Store the register state
+ @ Since we have already switch to SVC R8_fiq - R12_fiq
+ @ never get used or saved
+ mov R0,#7 @ ExceptionType
+ ldr R1,ASM_PFX(CommonExceptionEntry)
+ bx R1
+
+//
+// This gets patched by the C code that patches in the vector table
+//
+ASM_PFX(CommonExceptionEntry):
+ .byte 0x12
+ .byte 0x34
+ .byte 0x56
+ .byte 0x78
+
+ASM_PFX(ExceptionHandlersEnd):
+
+//
+// This code runs from CpuDxe driver loaded address. It is patched into
+// CommonExceptionEntry.
+//
+ASM_PFX(AsmCommonExceptionEntry):
+ mrc p15, 0, R1, c6, c0, 2 @ Read IFAR
+ str R1, [SP, #0x50] @ Store it in EFI_SYSTEM_CONTEXT_ARM.IFAR
+
+ mrc p15, 0, R1, c5, c0, 1 @ Read IFSR
+ str R1, [SP, #0x4c] @ Store it in EFI_SYSTEM_CONTEXT_ARM.IFSR
+
+ mrc p15, 0, R1, c6, c0, 0 @ Read DFAR
+ str R1, [SP, #0x48] @ Store it in EFI_SYSTEM_CONTEXT_ARM.DFAR
+
+ mrc p15, 0, R1, c5, c0, 0 @ Read DFSR
+ str R1, [SP, #0x44] @ Store it in EFI_SYSTEM_CONTEXT_ARM.DFSR
+
+ ldr R1, [SP, #0x5c] @ srsdb saved pre-exception CPSR on the stack
+ str R1, [SP, #0x40] @ Store it in EFI_SYSTEM_CONTEXT_ARM.CPSR
+
+ add R2, SP, #0x38 @ Make R2 point to EFI_SYSTEM_CONTEXT_ARM.LR
+ and R3, R1, #0x1f @ Check CPSR to see if User or System Mode
+ cmp R3, #0x1f @ if ((CPSR == 0x10) || (CPSR == 0x1df))
+ cmpne R3, #0x10 @
+ stmeqed R2, {lr}^ @ save unbanked lr
+ @ else
+ stmneed R2, {lr} @ save SVC lr
+
+
+ ldr R5, [SP, #0x58] @ PC is the LR pushed by srsfd
+ @ Check to see if we have to adjust for Thumb entry
+ sub r4, r0, #1 @ if (ExceptionType == 1 || ExceptionType ==2)) {
+ cmp r4, #1 @ // UND & SVC have differnt LR adjust for Thumb
+ bhi NoAdjustNeeded
+
+ tst r1, #0x20 @ if ((CPSR & T)) == T) { // Thumb Mode on entry
+ addne R5, R5, #2 @ PC += 2@
+ str R5,[SP,#0x58] @ Update LR value pused by srsfd
+
+NoAdjustNeeded:
+
+ str R5, [SP, #0x3c] @ Store it in EFI_SYSTEM_CONTEXT_ARM.PC
+
+ sub R1, SP, #0x60 @ We pused 0x60 bytes on the stack
+ str R1, [SP, #0x34] @ Store it in EFI_SYSTEM_CONTEXT_ARM.SP
+
+ @ R0 is ExceptionType
+ mov R1,SP @ R1 is SystemContext
+
+/*
+VOID
+EFIAPI
+GdbExceptionHandler (
+ IN EFI_EXCEPTION_TYPE ExceptionType, R0
+ IN OUT EFI_SYSTEM_CONTEXT SystemContext R1
+ )
+
+*/
+ blx ASM_PFX(GdbExceptionHandler) @ Call exception handler
+
+ ldr R1,[SP,#0x3c] @ EFI_SYSTEM_CONTEXT_ARM.PC
+ str R1,[SP,#0x58] @ Store it back to srsfd stack slot so it can be restored
+
+ ldr R1,[SP,#0x40] @ EFI_SYSTEM_CONTEXT_ARM.CPSR
+ str R1,[SP,#0x5c] @ Store it back to srsfd stack slot so it can be restored
+
+ add R3, SP, #0x54 @ Make R3 point to SVC LR saved on entry
+ add R2, SP, #0x38 @ Make R2 point to EFI_SYSTEM_CONTEXT_ARM.LR
+ and R1, R1, #0x1f @ Check to see if User or System Mode
+ cmp R1, #0x1f @ if ((CPSR == 0x10) || (CPSR == 0x1f))
+ cmpne R1, #0x10 @
+ ldmeqed R2, {lr}^ @ restore unbanked lr
+ @ else
+ ldmneed R3, {lr} @ restore SVC lr, via ldmfd SP!, {LR}
+
+ ldmfd SP!,{R0-R12} @ Restore general purpose registers
+ @ Exception handler can not change SP
+
+ add SP,SP,#0x20 @ Clear out the remaining stack space
+ ldmfd SP!,{LR} @ restore the link register for this context
+ rfefd SP! @ return from exception via srsfd stack slot
+
diff --git a/EmbeddedPkg/Library/GdbDebugAgent/Arm/ExceptionSupport.ARMv6.asm b/EmbeddedPkg/Library/GdbDebugAgent/Arm/ExceptionSupport.ARMv6.asm index c133073f4b..c70fb92482 100755 --- a/EmbeddedPkg/Library/GdbDebugAgent/Arm/ExceptionSupport.ARMv6.asm +++ b/EmbeddedPkg/Library/GdbDebugAgent/Arm/ExceptionSupport.ARMv6.asm @@ -1,259 +1,259 @@ -//------------------------------------------------------------------------------ -// -// Use ARMv6 instruction to operate on a single stack -// -// Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR> -// -// 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. -// -//------------------------------------------------------------------------------ - - - -/* - -This is the stack constructed by the exception handler (low address to high address) - # R0 - IFAR is EFI_SYSTEM_CONTEXT for ARM - Reg Offset - === ====== - R0 0x00 # stmfd SP!,{R0-R12} - R1 0x04 - R2 0x08 - R3 0x0c - R4 0x10 - R5 0x14 - R6 0x18 - R7 0x1c - R8 0x20 - R9 0x24 - R10 0x28 - R11 0x2c - R12 0x30 - SP 0x34 # reserved via adding 0x20 (32) to the SP - LR 0x38 - PC 0x3c - CPSR 0x40 - DFSR 0x44 - DFAR 0x48 - IFSR 0x4c - IFAR 0x50 - - LR 0x54 # SVC Link register (we need to restore it) - - LR 0x58 # pushed by srsfd - CPSR 0x5c - - */ - - - EXPORT ExceptionHandlersStart - EXPORT ExceptionHandlersEnd - EXPORT CommonExceptionEntry - EXPORT AsmCommonExceptionEntry - IMPORT GdbExceptionHandler - - PRESERVE8 - AREA DxeExceptionHandlers, CODE, READONLY - -// -// This code gets copied to the ARM vector table -// ExceptionHandlersStart - ExceptionHandlersEnd gets copied -// -ExceptionHandlersStart - -Reset - b Reset - -UndefinedInstruction - b UndefinedInstructionEntry - -SoftwareInterrupt - b SoftwareInterruptEntry - -PrefetchAbort - b PrefetchAbortEntry - -DataAbort - b DataAbortEntry - -ReservedException - b ReservedExceptionEntry - -Irq - b Irq - -Fiq - b FiqEntry - - -UndefinedInstructionEntry - sub LR, LR, #4 ; Only -2 for Thumb, adjust in CommonExceptionEntry - srsfd #0x13! ; Store return state on SVC stack - cpsid f, #0x13 ; Switch to SVC for common stack - stmfd SP!,{LR} ; Store the link register for the current mode - sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} ; Store the register state - - mov R0,#1 ; ExceptionType - ldr R1,CommonExceptionEntry; - bx R1 - -SoftwareInterruptEntry - sub LR, LR, #4 ; Only -2 for Thumb, adjust in CommonExceptionEntry - srsfd #0x13! ; Store return state on SVC stack - cpsid f ; We are already in SVC mode - stmfd SP!,{LR} ; Store the link register for the current mode - sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} ; Store the register state - - mov R0,#2 ; ExceptionType - ldr R1,CommonExceptionEntry - bx R1 - -PrefetchAbortEntry - sub LR,LR,#4 - srsfd #0x13! ; Store return state on SVC stack - cpsid f, #0x13 ; Switch to SVC for common stack - stmfd SP!,{LR} ; Store the link register for the current mode - sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} ; Store the register state - - mov R0,#3 ; ExceptionType - ldr R1,CommonExceptionEntry - bx R1 - -DataAbortEntry - sub LR,LR,#8 - srsfd #0x13! ; Store return state on SVC stack - cpsid f, #0x13 ; Switch to SVC for common stack - stmfd SP!,{LR} ; Store the link register for the current mode - sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} ; Store the register state - - mov R0,#4 ; ExceptionType - ldr R1,CommonExceptionEntry - bx R1 - -ReservedExceptionEntry - srsfd #0x13! ; Store return state on SVC stack - cpsid f, #0x13 ; Switch to SVC for common stack - stmfd SP!,{LR} ; Store the link register for the current mode - sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} ; Store the register state - - mov R0,#5 ; ExceptionType - ldr R1,CommonExceptionEntry - bx R1 - -FiqEntry - sub LR,LR,#4 - srsfd #0x13! ; Store return state on SVC stack - cps #0x13 ; Switch to SVC for common stack - stmfd SP!,{LR} ; Store the link register for the current mode - sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR - stmfd SP!,{R0-R12} ; Store the register state - ; Since we have already switch to SVC R8_fiq - R12_fiq - ; never get used or saved - mov R0,#7 ; ExceptionType - ldr R1,CommonExceptionEntry - bx R1 - -// -// This gets patched by the C code that patches in the vector table -// -CommonExceptionEntry - dcd 0x12345678 - -ExceptionHandlersEnd - -// -// This code runs from CpuDxe driver loaded address. It is patched into -// CommonExceptionEntry. -// -AsmCommonExceptionEntry - mrc p15, 0, R1, c6, c0, 2 ; Read IFAR - str R1, [SP, #0x50] ; Store it in EFI_SYSTEM_CONTEXT_ARM.IFAR - - mrc p15, 0, R1, c5, c0, 1 ; Read IFSR - str R1, [SP, #0x4c] ; Store it in EFI_SYSTEM_CONTEXT_ARM.IFSR - - mrc p15, 0, R1, c6, c0, 0 ; Read DFAR - str R1, [SP, #0x48] ; Store it in EFI_SYSTEM_CONTEXT_ARM.DFAR - - mrc p15, 0, R1, c5, c0, 0 ; Read DFSR - str R1, [SP, #0x44] ; Store it in EFI_SYSTEM_CONTEXT_ARM.DFSR - - ldr R1, [SP, #0x5c] ; srsfd saved pre-exception CPSR on the stack - str R1, [SP, #0x40] ; Store it in EFI_SYSTEM_CONTEXT_ARM.CPSR - - add R2, SP, #0x38 ; Make R2 point to EFI_SYSTEM_CONTEXT_ARM.LR - and R3, R1, #0x1f ; Check CPSR to see if User or System Mode - cmp R3, #0x1f ; if ((CPSR == 0x10) || (CPSR == 0x1df)) - cmpne R3, #0x10 ; - stmeqed R2, {lr}^ ; save unbanked lr - ; else - stmneed R2, {lr} ; save SVC lr - - - ldr R5, [SP, #0x58] ; PC is the LR pushed by srsfd - ; Check to see if we have to adjust for Thumb entry - sub r4, r0, #1 ; if (ExceptionType == 1 || ExceptionType ==2)) { - cmp r4, #1 ; // UND & SVC have differnt LR adjust for Thumb - bhi NoAdjustNeeded - - tst r1, #0x20 ; if ((CPSR & T)) == T) { // Thumb Mode on entry - addne R5, R5, #2 ; PC += 2; - str R5,[SP,#0x58] ; Update LR value pused by srsfd - -NoAdjustNeeded - - str R5, [SP, #0x3c] ; Store it in EFI_SYSTEM_CONTEXT_ARM.PC - - sub R1, SP, #0x60 ; We pused 0x60 bytes on the stack - str R1, [SP, #0x34] ; Store it in EFI_SYSTEM_CONTEXT_ARM.SP - - ; R0 is ExceptionType - mov R1,SP ; R1 is SystemContext - -/* -VOID -EFIAPI -GdbExceptionHandler ( - IN EFI_EXCEPTION_TYPE ExceptionType, R0 - IN OUT EFI_SYSTEM_CONTEXT SystemContext R1 - ) - -*/ - blx GdbExceptionHandler ; Call exception handler - - ldr R1,[SP,#0x3c] ; EFI_SYSTEM_CONTEXT_ARM.PC - str R1,[SP,#0x58] ; Store it back to srsfd stack slot so it can be restored - - ldr R1,[SP,#0x40] ; EFI_SYSTEM_CONTEXT_ARM.CPSR - str R1,[SP,#0x5c] ; Store it back to srsfd stack slot so it can be restored - - add R3, SP, #0x54 ; Make R3 point to SVC LR saved on entry - add R2, SP, #0x38 ; Make R2 point to EFI_SYSTEM_CONTEXT_ARM.LR - and R1, R1, #0x1f ; Check to see if User or System Mode - cmp R1, #0x1f ; if ((CPSR == 0x10) || (CPSR == 0x1f)) - cmpne R1, #0x10 ; - ldmeqed R2, {lr}^ ; restore unbanked lr - ; else - ldmneed R3, {lr} ; restore SVC lr, via ldmfd SP!, {LR} - - ldmfd SP!,{R0-R12} ; Restore general purpose registers - ; Exception handler can not change SP - - add SP,SP,#0x20 ; Clear out the remaining stack space - ldmfd SP!,{LR} ; restore the link register for this context - rfefd SP! ; return from exception via srsfd stack slot - - END - - +//------------------------------------------------------------------------------
+//
+// Use ARMv6 instruction to operate on a single stack
+//
+// Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
+//
+// 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.
+//
+//------------------------------------------------------------------------------
+
+
+
+/*
+
+This is the stack constructed by the exception handler (low address to high address)
+ # R0 - IFAR is EFI_SYSTEM_CONTEXT for ARM
+ Reg Offset
+ === ======
+ R0 0x00 # stmfd SP!,{R0-R12}
+ R1 0x04
+ R2 0x08
+ R3 0x0c
+ R4 0x10
+ R5 0x14
+ R6 0x18
+ R7 0x1c
+ R8 0x20
+ R9 0x24
+ R10 0x28
+ R11 0x2c
+ R12 0x30
+ SP 0x34 # reserved via adding 0x20 (32) to the SP
+ LR 0x38
+ PC 0x3c
+ CPSR 0x40
+ DFSR 0x44
+ DFAR 0x48
+ IFSR 0x4c
+ IFAR 0x50
+
+ LR 0x54 # SVC Link register (we need to restore it)
+
+ LR 0x58 # pushed by srsfd
+ CPSR 0x5c
+
+ */
+
+
+ EXPORT ExceptionHandlersStart
+ EXPORT ExceptionHandlersEnd
+ EXPORT CommonExceptionEntry
+ EXPORT AsmCommonExceptionEntry
+ IMPORT GdbExceptionHandler
+
+ PRESERVE8
+ AREA DxeExceptionHandlers, CODE, READONLY
+
+//
+// This code gets copied to the ARM vector table
+// ExceptionHandlersStart - ExceptionHandlersEnd gets copied
+//
+ExceptionHandlersStart
+
+Reset
+ b Reset
+
+UndefinedInstruction
+ b UndefinedInstructionEntry
+
+SoftwareInterrupt
+ b SoftwareInterruptEntry
+
+PrefetchAbort
+ b PrefetchAbortEntry
+
+DataAbort
+ b DataAbortEntry
+
+ReservedException
+ b ReservedExceptionEntry
+
+Irq
+ b Irq
+
+Fiq
+ b FiqEntry
+
+
+UndefinedInstructionEntry
+ sub LR, LR, #4 ; Only -2 for Thumb, adjust in CommonExceptionEntry
+ srsfd #0x13! ; Store return state on SVC stack
+ cpsid f, #0x13 ; Switch to SVC for common stack
+ stmfd SP!,{LR} ; Store the link register for the current mode
+ sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} ; Store the register state
+
+ mov R0,#1 ; ExceptionType
+ ldr R1,CommonExceptionEntry;
+ bx R1
+
+SoftwareInterruptEntry
+ sub LR, LR, #4 ; Only -2 for Thumb, adjust in CommonExceptionEntry
+ srsfd #0x13! ; Store return state on SVC stack
+ cpsid f ; We are already in SVC mode
+ stmfd SP!,{LR} ; Store the link register for the current mode
+ sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} ; Store the register state
+
+ mov R0,#2 ; ExceptionType
+ ldr R1,CommonExceptionEntry
+ bx R1
+
+PrefetchAbortEntry
+ sub LR,LR,#4
+ srsfd #0x13! ; Store return state on SVC stack
+ cpsid f, #0x13 ; Switch to SVC for common stack
+ stmfd SP!,{LR} ; Store the link register for the current mode
+ sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} ; Store the register state
+
+ mov R0,#3 ; ExceptionType
+ ldr R1,CommonExceptionEntry
+ bx R1
+
+DataAbortEntry
+ sub LR,LR,#8
+ srsfd #0x13! ; Store return state on SVC stack
+ cpsid f, #0x13 ; Switch to SVC for common stack
+ stmfd SP!,{LR} ; Store the link register for the current mode
+ sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} ; Store the register state
+
+ mov R0,#4 ; ExceptionType
+ ldr R1,CommonExceptionEntry
+ bx R1
+
+ReservedExceptionEntry
+ srsfd #0x13! ; Store return state on SVC stack
+ cpsid f, #0x13 ; Switch to SVC for common stack
+ stmfd SP!,{LR} ; Store the link register for the current mode
+ sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} ; Store the register state
+
+ mov R0,#5 ; ExceptionType
+ ldr R1,CommonExceptionEntry
+ bx R1
+
+FiqEntry
+ sub LR,LR,#4
+ srsfd #0x13! ; Store return state on SVC stack
+ cps #0x13 ; Switch to SVC for common stack
+ stmfd SP!,{LR} ; Store the link register for the current mode
+ sub SP,SP,#0x20 ; Save space for SP, LR, PC, IFAR - CPSR
+ stmfd SP!,{R0-R12} ; Store the register state
+ ; Since we have already switch to SVC R8_fiq - R12_fiq
+ ; never get used or saved
+ mov R0,#7 ; ExceptionType
+ ldr R1,CommonExceptionEntry
+ bx R1
+
+//
+// This gets patched by the C code that patches in the vector table
+//
+CommonExceptionEntry
+ dcd 0x12345678
+
+ExceptionHandlersEnd
+
+//
+// This code runs from CpuDxe driver loaded address. It is patched into
+// CommonExceptionEntry.
+//
+AsmCommonExceptionEntry
+ mrc p15, 0, R1, c6, c0, 2 ; Read IFAR
+ str R1, [SP, #0x50] ; Store it in EFI_SYSTEM_CONTEXT_ARM.IFAR
+
+ mrc p15, 0, R1, c5, c0, 1 ; Read IFSR
+ str R1, [SP, #0x4c] ; Store it in EFI_SYSTEM_CONTEXT_ARM.IFSR
+
+ mrc p15, 0, R1, c6, c0, 0 ; Read DFAR
+ str R1, [SP, #0x48] ; Store it in EFI_SYSTEM_CONTEXT_ARM.DFAR
+
+ mrc p15, 0, R1, c5, c0, 0 ; Read DFSR
+ str R1, [SP, #0x44] ; Store it in EFI_SYSTEM_CONTEXT_ARM.DFSR
+
+ ldr R1, [SP, #0x5c] ; srsfd saved pre-exception CPSR on the stack
+ str R1, [SP, #0x40] ; Store it in EFI_SYSTEM_CONTEXT_ARM.CPSR
+
+ add R2, SP, #0x38 ; Make R2 point to EFI_SYSTEM_CONTEXT_ARM.LR
+ and R3, R1, #0x1f ; Check CPSR to see if User or System Mode
+ cmp R3, #0x1f ; if ((CPSR == 0x10) || (CPSR == 0x1df))
+ cmpne R3, #0x10 ;
+ stmeqed R2, {lr}^ ; save unbanked lr
+ ; else
+ stmneed R2, {lr} ; save SVC lr
+
+
+ ldr R5, [SP, #0x58] ; PC is the LR pushed by srsfd
+ ; Check to see if we have to adjust for Thumb entry
+ sub r4, r0, #1 ; if (ExceptionType == 1 || ExceptionType ==2)) {
+ cmp r4, #1 ; // UND & SVC have differnt LR adjust for Thumb
+ bhi NoAdjustNeeded
+
+ tst r1, #0x20 ; if ((CPSR & T)) == T) { // Thumb Mode on entry
+ addne R5, R5, #2 ; PC += 2;
+ str R5,[SP,#0x58] ; Update LR value pused by srsfd
+
+NoAdjustNeeded
+
+ str R5, [SP, #0x3c] ; Store it in EFI_SYSTEM_CONTEXT_ARM.PC
+
+ sub R1, SP, #0x60 ; We pused 0x60 bytes on the stack
+ str R1, [SP, #0x34] ; Store it in EFI_SYSTEM_CONTEXT_ARM.SP
+
+ ; R0 is ExceptionType
+ mov R1,SP ; R1 is SystemContext
+
+/*
+VOID
+EFIAPI
+GdbExceptionHandler (
+ IN EFI_EXCEPTION_TYPE ExceptionType, R0
+ IN OUT EFI_SYSTEM_CONTEXT SystemContext R1
+ )
+
+*/
+ blx GdbExceptionHandler ; Call exception handler
+
+ ldr R1,[SP,#0x3c] ; EFI_SYSTEM_CONTEXT_ARM.PC
+ str R1,[SP,#0x58] ; Store it back to srsfd stack slot so it can be restored
+
+ ldr R1,[SP,#0x40] ; EFI_SYSTEM_CONTEXT_ARM.CPSR
+ str R1,[SP,#0x5c] ; Store it back to srsfd stack slot so it can be restored
+
+ add R3, SP, #0x54 ; Make R3 point to SVC LR saved on entry
+ add R2, SP, #0x38 ; Make R2 point to EFI_SYSTEM_CONTEXT_ARM.LR
+ and R1, R1, #0x1f ; Check to see if User or System Mode
+ cmp R1, #0x1f ; if ((CPSR == 0x10) || (CPSR == 0x1f))
+ cmpne R1, #0x10 ;
+ ldmeqed R2, {lr}^ ; restore unbanked lr
+ ; else
+ ldmneed R3, {lr} ; restore SVC lr, via ldmfd SP!, {LR}
+
+ ldmfd SP!,{R0-R12} ; Restore general purpose registers
+ ; Exception handler can not change SP
+
+ add SP,SP,#0x20 ; Clear out the remaining stack space
+ ldmfd SP!,{LR} ; restore the link register for this context
+ rfefd SP! ; return from exception via srsfd stack slot
+
+ END
+
+
diff --git a/EmbeddedPkg/Library/GdbDebugAgent/Arm/Processor.c b/EmbeddedPkg/Library/GdbDebugAgent/Arm/Processor.c index e6aabf940a..f22a19dded 100755 --- a/EmbeddedPkg/Library/GdbDebugAgent/Arm/Processor.c +++ b/EmbeddedPkg/Library/GdbDebugAgent/Arm/Processor.c @@ -447,23 +447,23 @@ RemoveBreakPoint ( }
-/** - Send the T signal with the given exception type (in gdb order) and possibly - with n:r pairs related to the watchpoints - - @param SystemContext Register content at time of the exception - @param GdbExceptionType GDB exception type - **/ -VOID -ProcessorSendTSignal ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINT8 GdbExceptionType, - IN OUT CHAR8 *TSignalPtr, - IN UINTN SizeOfBuffer - ) -{ - *TSignalPtr = '\0'; -} +/**
+ Send the T signal with the given exception type (in gdb order) and possibly
+ with n:r pairs related to the watchpoints
+
+ @param SystemContext Register content at time of the exception
+ @param GdbExceptionType GDB exception type
+ **/
+VOID
+ProcessorSendTSignal (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINT8 GdbExceptionType,
+ IN OUT CHAR8 *TSignalPtr,
+ IN UINTN SizeOfBuffer
+ )
+{
+ *TSignalPtr = '\0';
+}
/**
FIQ state is only changed by FIQ exception. We don't want to take FIQ
@@ -475,21 +475,21 @@ ProcessorSendTSignal ( @return TRUE We are in the GDB stub exception preamble
@return FALSE We are not in GDB stub code
- **/ -BOOLEAN -InFiqCrack ( - IN UINT32 PC - ) -{ - UINT32 VectorBase = PcdGet32 (PcdCpuVectorBaseAddress); - UINT32 Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart; - - if ((PC >= VectorBase) && (PC <= (VectorBase + Length))) { - return TRUE; - } - - return FALSE; -} + **/
+BOOLEAN
+InFiqCrack (
+ IN UINT32 PC
+ )
+{
+ UINT32 VectorBase = PcdGet32 (PcdCpuVectorBaseAddress);
+ UINT32 Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;
+
+ if ((PC >= VectorBase) && (PC <= (VectorBase + Length))) {
+ return TRUE;
+ }
+
+ return FALSE;
+}
/**
@@ -526,24 +526,24 @@ ProcessorControlC ( return FALSE;
}
- while (TRUE) { - if (!GdbIsCharAvailable ()) { - // - // No characters are pending so exit the loop - // - Return = TRUE; - break; - } - - Char = GdbGetChar (); - if (Char == 0x03) { - // - // We have a ctrl-c so exit and process exception for ctrl-c - // - Return = FALSE; - break; - } - } + while (TRUE) {
+ if (!GdbIsCharAvailable ()) {
+ //
+ // No characters are pending so exit the loop
+ //
+ Return = TRUE;
+ break;
+ }
+
+ Char = GdbGetChar ();
+ if (Char == 0x03) {
+ //
+ // We have a ctrl-c so exit and process exception for ctrl-c
+ //
+ Return = FALSE;
+ break;
+ }
+ }
DebugAgentTimerEndOfInterrupt ();
@@ -622,13 +622,13 @@ InitializeDebugAgent ( IN VOID *Context, OPTIONAL
IN DEBUG_AGENT_CONTINUE Function OPTIONAL
)
-{ +{
UINTN Offset;
UINTN Length;
BOOLEAN IrqEnabled;
UINT32 *VectorBase;
- +
//
// Disable interrupts
//
diff --git a/EmbeddedPkg/Library/GdbDebugAgent/GdbDebugAgent.h b/EmbeddedPkg/Library/GdbDebugAgent/GdbDebugAgent.h index 2d0570e3dc..44c82f3472 100755 --- a/EmbeddedPkg/Library/GdbDebugAgent/GdbDebugAgent.h +++ b/EmbeddedPkg/Library/GdbDebugAgent/GdbDebugAgent.h @@ -1,697 +1,697 @@ -/** @file - Private include file for GDB stub - - Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR> - - 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. - -**/ - -#ifndef __GCC_DEBUG_AGENT_INTERNAL__ -#define __GCC_DEBUG_AGENT_INTERNAL__ - -#include <Uefi.h> -#include <Library/BaseLib.h> -#include <Library/BaseMemoryLib.h> -#include <Library/MemoryAllocationLib.h> -#include <Library/DebugLib.h> -#include <Library/PcdLib.h> -#include <Library/GdbSerialLib.h> -#include <Library/PrintLib.h> -#include <Library/CacheMaintenanceLib.h> -#include <Library/DebugAgentTimerLib.h> +/** @file
+ Private include file for GDB stub
+
+ Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
+
+ 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.
+
+**/
+
+#ifndef __GCC_DEBUG_AGENT_INTERNAL__
+#define __GCC_DEBUG_AGENT_INTERNAL__
+
+#include <Uefi.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/DebugLib.h>
+#include <Library/PcdLib.h>
+#include <Library/GdbSerialLib.h>
+#include <Library/PrintLib.h>
+#include <Library/CacheMaintenanceLib.h>
+#include <Library/DebugAgentTimerLib.h>
#include <Library/DebugAgentLib.h>
- -#include <IndustryStandard/PeImage.h> -#include <Protocol/DebugSupport.h> - -extern CONST CHAR8 mHexToStr[]; - -// maximum size of input and output buffers -// This value came from the show remote command of the gdb we tested against -#define MAX_BUF_SIZE 2000 - -// maximum size of address buffer -#define MAX_ADDR_SIZE 32 - -// maximum size of register number buffer -#define MAX_REG_NUM_BUF_SIZE 32 - -// maximum size of length buffer -#define MAX_LENGTH_SIZE 32 - -// maximum size of T signal members -#define MAX_T_SIGNAL_SIZE 64 - -// the mask used to clear all the cache -#define TF_BIT 0x00000100 - - -// -// GDB Signal definitions - generic names for interrupts -// -#define GDB_SIGINT 2 // Interrupt process via ctrl-c -#define GDB_SIGILL 4 // Illegal instruction -#define GDB_SIGTRAP 5 // Trace Trap (Breakpoint and SingleStep) -#define GDB_SIGEMT 7 // Emulator Trap -#define GDB_SIGFPE 8 // Floating point exception -#define GDB_SIGSEGV 11 // Setgment violation, page fault - - -// -// GDB File I/O Error values, zero means no error -// Includes all general GDB Unix like error values -// -#define GDB_EBADMEMADDRBUFSIZE 11 // the buffer that stores memory Address to be read from/written to is not the right size -#define GDB_EBADMEMLENGBUFSIZE 12 // the buffer that stores Length is not the right size -#define GDB_EBADMEMLENGTH 13 // Length, the given number of bytes to read or write, is not the right size -#define GDB_EBADMEMDATA 14 // one of the bytes or nibbles of the memory is leess than 0 -#define GDB_EBADMEMDATASIZE 15 // the memory data, 'XX..', is too short or too long -#define GDB_EBADBUFSIZE 21 // the buffer created is not the correct size -#define GDB_EINVALIDARG 31 // argument is invalid -#define GDB_ENOSPACE 41 // -#define GDB_EINVALIDBRKPOINTTYPE 51 // the breakpoint type is not recognized -#define GDB_EINVALIDREGNUM 61 // given register number is not valid: either <0 or >=Number of Registers -#define GDB_EUNKNOWN 255 // unknown - - -// -// These devices are open by GDB so we can just read and write to them -// -#define GDB_STDIN 0x00 -#define GDB_STDOUT 0x01 -#define GDB_STDERR 0x02 - -// -//Define Register size for different architectures -// -#if defined (MDE_CPU_IA32) -#define REG_SIZE 32 -#elif defined (MDE_CPU_X64) -#define REG_SIZE 64 -#elif defined (MDE_CPU_ARM) -#define REG_SIZE 32 -#endif - - -typedef struct { - EFI_EXCEPTION_TYPE Exception; - UINT8 SignalNo; -} EFI_EXCEPTION_TYPE_ENTRY; - - -#if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64) - -// -// Byte packed structure for DR6 -// 32-bits on IA-32 -// 64-bits on X64. The upper 32-bits on X64 are reserved -// -typedef union { - struct { - UINT32 B0:1; // Breakpoint condition detected - UINT32 B1:1; // Breakpoint condition detected - UINT32 B2:1; // Breakpoint condition detected - UINT32 B3:1; // Breakpoint condition detected - UINT32 Reserved_1:9; // Reserved - UINT32 BD:1; // Debug register access detected - UINT32 BS:1; // Single step - UINT32 BT:1; // Task switch - UINT32 Reserved_2:16; // Reserved - } Bits; - UINTN UintN; -} IA32_DR6; - -// -// Byte packed structure for DR7 -// 32-bits on IA-32 -// 64-bits on X64. The upper 32-bits on X64 are reserved -// -typedef union { - struct { - UINT32 L0:1; // Local breakpoint enable - UINT32 G0:1; // Global breakpoint enable - UINT32 L1:1; // Local breakpoint enable - UINT32 G1:1; // Global breakpoint enable - UINT32 L2:1; // Local breakpoint enable - UINT32 G2:1; // Global breakpoint enable - UINT32 L3:1; // Local breakpoint enable - UINT32 G3:1; // Global breakpoint enable - UINT32 LE:1; // Local exact breakpoint enable - UINT32 GE:1; // Global exact breakpoint enable - UINT32 Reserved_1:3; // Reserved - UINT32 GD:1; // Global detect enable - UINT32 Reserved_2:2; // Reserved - UINT32 RW0:2; // Read/Write field - UINT32 LEN0:2; // Length field - UINT32 RW1:2; // Read/Write field - UINT32 LEN1:2; // Length field - UINT32 RW2:2; // Read/Write field - UINT32 LEN2:2; // Length field - UINT32 RW3:2; // Read/Write field - UINT32 LEN3:2; // Length field - } Bits; - UINTN UintN; -} IA32_DR7; - -#endif /* if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64) */ - -typedef enum { - InstructionExecution, //Hardware breakpoint - DataWrite, //watch - DataRead, //rwatch - DataReadWrite, //awatch - SoftwareBreakpoint, //Software breakpoint - NotSupported -} BREAK_TYPE; - -// -// Array of exception types that need to be hooked by the debugger -// -extern EFI_EXCEPTION_TYPE_ENTRY gExceptionType[]; - -// -// If the periodic callback is called while we are processing an F packet we need -// to let the callback know to not read from the serail stream as it could steal -// characters from the F reponse packet -// -extern BOOLEAN gProcessingFPacket; - - -/** - Return the number of entries in the gExceptionType[] - - @retval UINTN, the number of entries in the gExceptionType[] array. - **/ -UINTN -MaxEfiException ( - VOID - ); - - -/** - Check to see if the ISA is supported. - ISA = Instruction Set Architecture - - @retval TRUE if Isa is supported, - FALSE otherwise. - **/ -BOOLEAN -CheckIsa ( - IN EFI_INSTRUCTION_SET_ARCHITECTURE Isa - ); - - -/** - Send the T signal with the given exception type (in gdb order) and possibly with n:r pairs related to the watchpoints - - @param SystemContext Register content at time of the exception - @param GdbExceptionType GDB exception type - **/ - -VOID -GdbSendTSignal ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINT8 GdbExceptionType - ); - - -/** - Translates the EFI mapping to GDB mapping - - @param EFIExceptionType EFI Exception that is being processed - @retval UINTN that corresponds to EFIExceptionType's GDB exception type number - **/ -UINT8 -ConvertEFItoGDBtype ( - IN EFI_EXCEPTION_TYPE EFIExceptionType - ); - - -/** - Empties the given buffer - @param *Buf pointer to the first element in buffer to be emptied - **/ -VOID -EmptyBuffer ( - IN CHAR8 *Buf - ); - - -/** - Converts an 8-bit Hex Char into a INTN. - - @param Char - the hex character to be converted into UINTN - @retval a INTN, from 0 to 15, that corressponds to Char - -1 if Char is not a hex character - **/ -INTN -HexCharToInt ( - IN CHAR8 Char - ); - - -/** 'E NN' - Send an error with the given error number after converting to hex. - The error number is put into the buffer in hex. '255' is the biggest errno we can send. - ex: 162 will be sent as A2. - - @param errno the error number that will be sent - **/ -VOID -EFIAPI -SendError ( - IN UINT8 ErrorNum - ); - - -/** - Send 'OK' when the function is done executing successfully. - **/ -VOID -SendSuccess ( - VOID - ); - - -/** - Send empty packet to specify that particular command/functionality is not supported. - **/ -VOID -SendNotSupported ( - VOID - ); - -/** ‘p n’ - Reads the n-th register's value into an output buffer and sends it as a packet - @param SystemContext Register content at time of the exception - @param InBuffer This is the input buffer received from gdb server - **/ -VOID -ReadNthRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *InBuffer - ); - - -/** ‘g’ - Reads the general registers into an output buffer and sends it as a packet - @param SystemContext Register content at time of the exception - **/ -VOID -ReadGeneralRegisters ( - IN EFI_SYSTEM_CONTEXT SystemContext - ); - - -/** ‘P n...=r...’ - Writes the new value of n-th register received into the input buffer to the n-th register - @param SystemContext Register content at time of the exception - @param InBuffer This is the input buffer received from gdb server - **/ -VOID -WriteNthRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *InBuffer - ); - - -/** ‘G XX...’ - Writes the new values received into the input buffer to the general registers - @param SystemContext Register content at time of the exception - @param InBuffer Pointer to the input buffer received from gdb server - **/ - -VOID -WriteGeneralRegisters ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *InBuffer - ); - - -/** ‘m addr,length ’ - Find the Length of the area to read and the start addres. Finally, pass them to - another function, TransferFromMemToOutBufAndSend, that will read from that memory space and - send it as a packet. - - @param *PacketData Pointer to Payload data for the packet - **/ -VOID -ReadFromMemory ( - IN CHAR8 *PacketData - ); - - -/** ‘M addr,length :XX...’ - Find the Length of the area in bytes to write and the start addres. Finally, pass them to - another function, TransferFromInBufToMem, that will write to that memory space the info in - the input buffer. - - @param PacketData Pointer to Payload data for the packet - **/ -VOID -WriteToMemory ( - IN CHAR8 *PacketData - ); - - -/** ‘c [addr ]’ - Continue. addr is Address to resume. If addr is omitted, resume at current - Address. - - @param SystemContext Register content at time of the exception - @param *PacketData Pointer to PacketData - **/ - -VOID -ContinueAtAddress ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *PacketData - ); - - -/** ‘s [addr ]’ - Single step. addr is the Address at which to resume. If addr is omitted, resume - at same Address. - - @param SystemContext Register content at time of the exception - @param PacketData Pointer to Payload data for the packet - **/ -VOID -SingleStep ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *PacketData - ); - -/** - Insert Single Step in the SystemContext - - @param SystemContext Register content at time of the exception - **/ -VOID -AddSingleStep ( - IN EFI_SYSTEM_CONTEXT SystemContext - ); - -/** - Remove Single Step in the SystemContext - - @param SystemContext Register content at time of the exception - **/ -VOID -RemoveSingleStep ( - IN EFI_SYSTEM_CONTEXT SystemContext - ); - - -/** - ‘Z1, [addr], [length]’ - ‘Z2, [addr], [length]’ - ‘Z3, [addr], [length]’ - ‘Z4, [addr], [length]’ - - Insert hardware breakpoint/watchpoint at address addr of size length - - @param SystemContext Register content at time of the exception - @param *PacketData Pointer to the Payload data for the packet - -**/ -VOID -EFIAPI -InsertBreakPoint( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *PacketData - ); - - -/** - ‘z1, [addr], [length]’ - ‘z2, [addr], [length]’ - ‘z3, [addr], [length]’ - ‘z4, [addr], [length]’ - - Remove hardware breakpoint/watchpoint at address addr of size length - - @param SystemContext Register content at time of the exception - @param *PacketData Pointer to the Payload data for the packet - -**/ -VOID -EFIAPI -RemoveBreakPoint( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *PacketData - ); - - -/** - Exception Hanldler for GDB. It will be called for all exceptions - registered via the gExceptionType[] array. - - @param ExceptionType Exception that is being processed - @param SystemContext Register content at time of the exception - - **/ -VOID -EFIAPI -GdbExceptionHandler ( - IN EFI_EXCEPTION_TYPE ExceptionType, - IN OUT EFI_SYSTEM_CONTEXT SystemContext - ); - - -/** - Periodic callback for GDB. This function is used to catch a ctrl-c or other - break in type command from GDB. - - @param SystemContext Register content at time of the call - - **/ -VOID -EFIAPI -GdbPeriodicCallBack ( - IN OUT EFI_SYSTEM_CONTEXT SystemContext - ); - - -/** - Make two serail consoles: 1) StdIn and StdOut via GDB. 2) StdErr via GDB. - - These console show up on the remote system running GDB - -**/ - -VOID -GdbInitializeSerialConsole ( - VOID - ); - - -/** - Send a GDB Remote Serial Protocol Packet - - $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$', - the packet teminating character '#' and the two digit checksum. - - If an ack '+' is not sent resend the packet, but timeout eventually so we don't end up - in an infinit loop. This is so if you unplug the debugger code just keeps running - - @param PacketData Payload data for the packet - - @retval Number of bytes of packet data sent. - -**/ -UINTN -SendPacket ( - IN CHAR8 *PacketData - ); - - -/** - Receive a GDB Remote Serial Protocol Packet - - $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$', - the packet teminating character '#' and the two digit checksum. - - If host re-starts sending a packet without ending the previous packet, only the last valid packet is proccessed. - (In other words, if received packet is '$12345$12345$123456#checksum', only '$123456#checksum' will be processed.) - - If an ack '+' is not sent resend the packet - - @param PacketData Payload data for the packet - - @retval Number of bytes of packet data received. - - **/ -UINTN -ReceivePacket ( - OUT CHAR8 *PacketData, - IN UINTN PacketDataSize - ); - - -/** - Read data from a FileDescriptor. On success number of bytes read is returned. Zero indicates - the end of a file. On error -1 is returned. If count is zero, GdbRead returns zero. - - @param FileDescriptor Device to talk to. - @param Buffer Buffer to hold Count bytes that were read - @param Count Number of bytes to transfer. - - @retval -1 Error - @retval {other} Number of bytes read. - -**/ -INTN -GdbRead ( - IN INTN FileDescriptor, - OUT VOID *Buffer, - IN UINTN Count - ); - - -/** - Write data to a FileDescriptor. On success number of bytes written is returned. Zero indicates - nothing was written. On error -1 is returned. - - @param FileDescriptor Device to talk to. - @param Buffer Buffer to hold Count bytes that are to be written - @param Count Number of bytes to transfer. - - @retval -1 Error - @retval {other} Number of bytes written. - -**/ -INTN -GdbWrite ( - IN INTN FileDescriptor, - OUT CONST VOID *Buffer, - IN UINTN Count - ); - -UINTN * -FindPointerToRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN RegNumber - ); - -CHAR8 * -BasicReadRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN RegNumber, - IN CHAR8 *OutBufPtr - ); - -VOID -TransferFromInBufToMem ( - IN UINTN Length, - IN UINT8 *Address, - IN CHAR8 *NewData - ); - -VOID -TransferFromMemToOutBufAndSend ( - IN UINTN Length, - IN UINT8 *Address - ); - -CHAR8 * -BasicWriteRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN RegNumber, - IN CHAR8 *InBufPtr - ); - -VOID -PrintReg ( - EFI_SYSTEM_CONTEXT SystemContext - ); - -UINTN -ParseBreakpointPacket ( - IN CHAR8 *PacketData, - OUT UINTN *Type, - OUT UINTN *Address, - OUT UINTN *Length - ); - -UINTN -GetBreakpointDataAddress ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN BreakpointNumber - ); - -UINTN -GetBreakpointDetected ( - IN EFI_SYSTEM_CONTEXT SystemContext - ); - -BREAK_TYPE -GetBreakpointType ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN BreakpointNumber - ); - -UINTN -ConvertLengthData ( - IN UINTN Length - ); - -EFI_STATUS -FindNextFreeDebugRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - OUT UINTN *Register - ); - -EFI_STATUS -EnableDebugRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN Register, - IN UINTN Address, - IN UINTN Length, - IN UINTN Type - ); - -EFI_STATUS -FindMatchingDebugRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN Address, - IN UINTN Length, - IN UINTN Type, - OUT UINTN *Register - ); - -EFI_STATUS -DisableDebugRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN Register - ); - -VOID -InitializeProcessor ( - VOID - ); - -/** - Send the T signal with the given exception type (in gdb order) and possibly with n:r pairs related to the watchpoints - - @param SystemContext Register content at time of the exception - @param GdbExceptionType GDB exception type - **/ -VOID -ProcessorSendTSignal ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINT8 GdbExceptionType, - IN OUT CHAR8 *TSignalPtr, - IN UINTN SizeOfBuffer - ); - +
+#include <IndustryStandard/PeImage.h>
+#include <Protocol/DebugSupport.h>
+
+extern CONST CHAR8 mHexToStr[];
+
+// maximum size of input and output buffers
+// This value came from the show remote command of the gdb we tested against
+#define MAX_BUF_SIZE 2000
+
+// maximum size of address buffer
+#define MAX_ADDR_SIZE 32
+
+// maximum size of register number buffer
+#define MAX_REG_NUM_BUF_SIZE 32
+
+// maximum size of length buffer
+#define MAX_LENGTH_SIZE 32
+
+// maximum size of T signal members
+#define MAX_T_SIGNAL_SIZE 64
+
+// the mask used to clear all the cache
+#define TF_BIT 0x00000100
+
+
+//
+// GDB Signal definitions - generic names for interrupts
+//
+#define GDB_SIGINT 2 // Interrupt process via ctrl-c
+#define GDB_SIGILL 4 // Illegal instruction
+#define GDB_SIGTRAP 5 // Trace Trap (Breakpoint and SingleStep)
+#define GDB_SIGEMT 7 // Emulator Trap
+#define GDB_SIGFPE 8 // Floating point exception
+#define GDB_SIGSEGV 11 // Setgment violation, page fault
+
+
+//
+// GDB File I/O Error values, zero means no error
+// Includes all general GDB Unix like error values
+//
+#define GDB_EBADMEMADDRBUFSIZE 11 // the buffer that stores memory Address to be read from/written to is not the right size
+#define GDB_EBADMEMLENGBUFSIZE 12 // the buffer that stores Length is not the right size
+#define GDB_EBADMEMLENGTH 13 // Length, the given number of bytes to read or write, is not the right size
+#define GDB_EBADMEMDATA 14 // one of the bytes or nibbles of the memory is leess than 0
+#define GDB_EBADMEMDATASIZE 15 // the memory data, 'XX..', is too short or too long
+#define GDB_EBADBUFSIZE 21 // the buffer created is not the correct size
+#define GDB_EINVALIDARG 31 // argument is invalid
+#define GDB_ENOSPACE 41 //
+#define GDB_EINVALIDBRKPOINTTYPE 51 // the breakpoint type is not recognized
+#define GDB_EINVALIDREGNUM 61 // given register number is not valid: either <0 or >=Number of Registers
+#define GDB_EUNKNOWN 255 // unknown
+
+
+//
+// These devices are open by GDB so we can just read and write to them
+//
+#define GDB_STDIN 0x00
+#define GDB_STDOUT 0x01
+#define GDB_STDERR 0x02
+
+//
+//Define Register size for different architectures
+//
+#if defined (MDE_CPU_IA32)
+#define REG_SIZE 32
+#elif defined (MDE_CPU_X64)
+#define REG_SIZE 64
+#elif defined (MDE_CPU_ARM)
+#define REG_SIZE 32
+#endif
+
+
+typedef struct {
+ EFI_EXCEPTION_TYPE Exception;
+ UINT8 SignalNo;
+} EFI_EXCEPTION_TYPE_ENTRY;
+
+
+#if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
+
+//
+// Byte packed structure for DR6
+// 32-bits on IA-32
+// 64-bits on X64. The upper 32-bits on X64 are reserved
+//
+typedef union {
+ struct {
+ UINT32 B0:1; // Breakpoint condition detected
+ UINT32 B1:1; // Breakpoint condition detected
+ UINT32 B2:1; // Breakpoint condition detected
+ UINT32 B3:1; // Breakpoint condition detected
+ UINT32 Reserved_1:9; // Reserved
+ UINT32 BD:1; // Debug register access detected
+ UINT32 BS:1; // Single step
+ UINT32 BT:1; // Task switch
+ UINT32 Reserved_2:16; // Reserved
+ } Bits;
+ UINTN UintN;
+} IA32_DR6;
+
+//
+// Byte packed structure for DR7
+// 32-bits on IA-32
+// 64-bits on X64. The upper 32-bits on X64 are reserved
+//
+typedef union {
+ struct {
+ UINT32 L0:1; // Local breakpoint enable
+ UINT32 G0:1; // Global breakpoint enable
+ UINT32 L1:1; // Local breakpoint enable
+ UINT32 G1:1; // Global breakpoint enable
+ UINT32 L2:1; // Local breakpoint enable
+ UINT32 G2:1; // Global breakpoint enable
+ UINT32 L3:1; // Local breakpoint enable
+ UINT32 G3:1; // Global breakpoint enable
+ UINT32 LE:1; // Local exact breakpoint enable
+ UINT32 GE:1; // Global exact breakpoint enable
+ UINT32 Reserved_1:3; // Reserved
+ UINT32 GD:1; // Global detect enable
+ UINT32 Reserved_2:2; // Reserved
+ UINT32 RW0:2; // Read/Write field
+ UINT32 LEN0:2; // Length field
+ UINT32 RW1:2; // Read/Write field
+ UINT32 LEN1:2; // Length field
+ UINT32 RW2:2; // Read/Write field
+ UINT32 LEN2:2; // Length field
+ UINT32 RW3:2; // Read/Write field
+ UINT32 LEN3:2; // Length field
+ } Bits;
+ UINTN UintN;
+} IA32_DR7;
+
+#endif /* if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64) */
+
+typedef enum {
+ InstructionExecution, //Hardware breakpoint
+ DataWrite, //watch
+ DataRead, //rwatch
+ DataReadWrite, //awatch
+ SoftwareBreakpoint, //Software breakpoint
+ NotSupported
+} BREAK_TYPE;
+
+//
+// Array of exception types that need to be hooked by the debugger
+//
+extern EFI_EXCEPTION_TYPE_ENTRY gExceptionType[];
+
+//
+// If the periodic callback is called while we are processing an F packet we need
+// to let the callback know to not read from the serail stream as it could steal
+// characters from the F reponse packet
+//
+extern BOOLEAN gProcessingFPacket;
+
+
+/**
+ Return the number of entries in the gExceptionType[]
+
+ @retval UINTN, the number of entries in the gExceptionType[] array.
+ **/
+UINTN
+MaxEfiException (
+ VOID
+ );
+
+
+/**
+ Check to see if the ISA is supported.
+ ISA = Instruction Set Architecture
+
+ @retval TRUE if Isa is supported,
+ FALSE otherwise.
+ **/
+BOOLEAN
+CheckIsa (
+ IN EFI_INSTRUCTION_SET_ARCHITECTURE Isa
+ );
+
+
+/**
+ Send the T signal with the given exception type (in gdb order) and possibly with n:r pairs related to the watchpoints
+
+ @param SystemContext Register content at time of the exception
+ @param GdbExceptionType GDB exception type
+ **/
+
+VOID
+GdbSendTSignal (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINT8 GdbExceptionType
+ );
+
+
+/**
+ Translates the EFI mapping to GDB mapping
+
+ @param EFIExceptionType EFI Exception that is being processed
+ @retval UINTN that corresponds to EFIExceptionType's GDB exception type number
+ **/
+UINT8
+ConvertEFItoGDBtype (
+ IN EFI_EXCEPTION_TYPE EFIExceptionType
+ );
+
+
+/**
+ Empties the given buffer
+ @param *Buf pointer to the first element in buffer to be emptied
+ **/
+VOID
+EmptyBuffer (
+ IN CHAR8 *Buf
+ );
+
+
+/**
+ Converts an 8-bit Hex Char into a INTN.
+
+ @param Char - the hex character to be converted into UINTN
+ @retval a INTN, from 0 to 15, that corressponds to Char
+ -1 if Char is not a hex character
+ **/
+INTN
+HexCharToInt (
+ IN CHAR8 Char
+ );
+
+
+/** 'E NN'
+ Send an error with the given error number after converting to hex.
+ The error number is put into the buffer in hex. '255' is the biggest errno we can send.
+ ex: 162 will be sent as A2.
+
+ @param errno the error number that will be sent
+ **/
+VOID
+EFIAPI
+SendError (
+ IN UINT8 ErrorNum
+ );
+
+
+/**
+ Send 'OK' when the function is done executing successfully.
+ **/
+VOID
+SendSuccess (
+ VOID
+ );
+
+
+/**
+ Send empty packet to specify that particular command/functionality is not supported.
+ **/
+VOID
+SendNotSupported (
+ VOID
+ );
+
+/** ‘p n’
+ Reads the n-th register's value into an output buffer and sends it as a packet
+ @param SystemContext Register content at time of the exception
+ @param InBuffer This is the input buffer received from gdb server
+ **/
+VOID
+ReadNthRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *InBuffer
+ );
+
+
+/** ‘g’
+ Reads the general registers into an output buffer and sends it as a packet
+ @param SystemContext Register content at time of the exception
+ **/
+VOID
+ReadGeneralRegisters (
+ IN EFI_SYSTEM_CONTEXT SystemContext
+ );
+
+
+/** ‘P n...=r...’
+ Writes the new value of n-th register received into the input buffer to the n-th register
+ @param SystemContext Register content at time of the exception
+ @param InBuffer This is the input buffer received from gdb server
+ **/
+VOID
+WriteNthRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *InBuffer
+ );
+
+
+/** ‘G XX...’
+ Writes the new values received into the input buffer to the general registers
+ @param SystemContext Register content at time of the exception
+ @param InBuffer Pointer to the input buffer received from gdb server
+ **/
+
+VOID
+WriteGeneralRegisters (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *InBuffer
+ );
+
+
+/** ‘m addr,length ’
+ Find the Length of the area to read and the start addres. Finally, pass them to
+ another function, TransferFromMemToOutBufAndSend, that will read from that memory space and
+ send it as a packet.
+
+ @param *PacketData Pointer to Payload data for the packet
+ **/
+VOID
+ReadFromMemory (
+ IN CHAR8 *PacketData
+ );
+
+
+/** ‘M addr,length :XX...’
+ Find the Length of the area in bytes to write and the start addres. Finally, pass them to
+ another function, TransferFromInBufToMem, that will write to that memory space the info in
+ the input buffer.
+
+ @param PacketData Pointer to Payload data for the packet
+ **/
+VOID
+WriteToMemory (
+ IN CHAR8 *PacketData
+ );
+
+
+/** ‘c [addr ]’
+ Continue. addr is Address to resume. If addr is omitted, resume at current
+ Address.
+
+ @param SystemContext Register content at time of the exception
+ @param *PacketData Pointer to PacketData
+ **/
+
+VOID
+ContinueAtAddress (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *PacketData
+ );
+
+
+/** ‘s [addr ]’
+ Single step. addr is the Address at which to resume. If addr is omitted, resume
+ at same Address.
+
+ @param SystemContext Register content at time of the exception
+ @param PacketData Pointer to Payload data for the packet
+ **/
+VOID
+SingleStep (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *PacketData
+ );
+
+/**
+ Insert Single Step in the SystemContext
+
+ @param SystemContext Register content at time of the exception
+ **/
+VOID
+AddSingleStep (
+ IN EFI_SYSTEM_CONTEXT SystemContext
+ );
+
+/**
+ Remove Single Step in the SystemContext
+
+ @param SystemContext Register content at time of the exception
+ **/
+VOID
+RemoveSingleStep (
+ IN EFI_SYSTEM_CONTEXT SystemContext
+ );
+
+
+/**
+ ‘Z1, [addr], [length]’
+ ‘Z2, [addr], [length]’
+ ‘Z3, [addr], [length]’
+ ‘Z4, [addr], [length]’
+
+ Insert hardware breakpoint/watchpoint at address addr of size length
+
+ @param SystemContext Register content at time of the exception
+ @param *PacketData Pointer to the Payload data for the packet
+
+**/
+VOID
+EFIAPI
+InsertBreakPoint(
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *PacketData
+ );
+
+
+/**
+ ‘z1, [addr], [length]’
+ ‘z2, [addr], [length]’
+ ‘z3, [addr], [length]’
+ ‘z4, [addr], [length]’
+
+ Remove hardware breakpoint/watchpoint at address addr of size length
+
+ @param SystemContext Register content at time of the exception
+ @param *PacketData Pointer to the Payload data for the packet
+
+**/
+VOID
+EFIAPI
+RemoveBreakPoint(
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *PacketData
+ );
+
+
+/**
+ Exception Hanldler for GDB. It will be called for all exceptions
+ registered via the gExceptionType[] array.
+
+ @param ExceptionType Exception that is being processed
+ @param SystemContext Register content at time of the exception
+
+ **/
+VOID
+EFIAPI
+GdbExceptionHandler (
+ IN EFI_EXCEPTION_TYPE ExceptionType,
+ IN OUT EFI_SYSTEM_CONTEXT SystemContext
+ );
+
+
+/**
+ Periodic callback for GDB. This function is used to catch a ctrl-c or other
+ break in type command from GDB.
+
+ @param SystemContext Register content at time of the call
+
+ **/
+VOID
+EFIAPI
+GdbPeriodicCallBack (
+ IN OUT EFI_SYSTEM_CONTEXT SystemContext
+ );
+
+
+/**
+ Make two serail consoles: 1) StdIn and StdOut via GDB. 2) StdErr via GDB.
+
+ These console show up on the remote system running GDB
+
+**/
+
+VOID
+GdbInitializeSerialConsole (
+ VOID
+ );
+
+
+/**
+ Send a GDB Remote Serial Protocol Packet
+
+ $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
+ the packet teminating character '#' and the two digit checksum.
+
+ If an ack '+' is not sent resend the packet, but timeout eventually so we don't end up
+ in an infinit loop. This is so if you unplug the debugger code just keeps running
+
+ @param PacketData Payload data for the packet
+
+ @retval Number of bytes of packet data sent.
+
+**/
+UINTN
+SendPacket (
+ IN CHAR8 *PacketData
+ );
+
+
+/**
+ Receive a GDB Remote Serial Protocol Packet
+
+ $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
+ the packet teminating character '#' and the two digit checksum.
+
+ If host re-starts sending a packet without ending the previous packet, only the last valid packet is proccessed.
+ (In other words, if received packet is '$12345$12345$123456#checksum', only '$123456#checksum' will be processed.)
+
+ If an ack '+' is not sent resend the packet
+
+ @param PacketData Payload data for the packet
+
+ @retval Number of bytes of packet data received.
+
+ **/
+UINTN
+ReceivePacket (
+ OUT CHAR8 *PacketData,
+ IN UINTN PacketDataSize
+ );
+
+
+/**
+ Read data from a FileDescriptor. On success number of bytes read is returned. Zero indicates
+ the end of a file. On error -1 is returned. If count is zero, GdbRead returns zero.
+
+ @param FileDescriptor Device to talk to.
+ @param Buffer Buffer to hold Count bytes that were read
+ @param Count Number of bytes to transfer.
+
+ @retval -1 Error
+ @retval {other} Number of bytes read.
+
+**/
+INTN
+GdbRead (
+ IN INTN FileDescriptor,
+ OUT VOID *Buffer,
+ IN UINTN Count
+ );
+
+
+/**
+ Write data to a FileDescriptor. On success number of bytes written is returned. Zero indicates
+ nothing was written. On error -1 is returned.
+
+ @param FileDescriptor Device to talk to.
+ @param Buffer Buffer to hold Count bytes that are to be written
+ @param Count Number of bytes to transfer.
+
+ @retval -1 Error
+ @retval {other} Number of bytes written.
+
+**/
+INTN
+GdbWrite (
+ IN INTN FileDescriptor,
+ OUT CONST VOID *Buffer,
+ IN UINTN Count
+ );
+
+UINTN *
+FindPointerToRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN RegNumber
+ );
+
+CHAR8 *
+BasicReadRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN RegNumber,
+ IN CHAR8 *OutBufPtr
+ );
+
+VOID
+TransferFromInBufToMem (
+ IN UINTN Length,
+ IN UINT8 *Address,
+ IN CHAR8 *NewData
+ );
+
+VOID
+TransferFromMemToOutBufAndSend (
+ IN UINTN Length,
+ IN UINT8 *Address
+ );
+
+CHAR8 *
+BasicWriteRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN RegNumber,
+ IN CHAR8 *InBufPtr
+ );
+
+VOID
+PrintReg (
+ EFI_SYSTEM_CONTEXT SystemContext
+ );
+
+UINTN
+ParseBreakpointPacket (
+ IN CHAR8 *PacketData,
+ OUT UINTN *Type,
+ OUT UINTN *Address,
+ OUT UINTN *Length
+ );
+
+UINTN
+GetBreakpointDataAddress (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN BreakpointNumber
+ );
+
+UINTN
+GetBreakpointDetected (
+ IN EFI_SYSTEM_CONTEXT SystemContext
+ );
+
+BREAK_TYPE
+GetBreakpointType (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN BreakpointNumber
+ );
+
+UINTN
+ConvertLengthData (
+ IN UINTN Length
+ );
+
+EFI_STATUS
+FindNextFreeDebugRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ OUT UINTN *Register
+ );
+
+EFI_STATUS
+EnableDebugRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN Register,
+ IN UINTN Address,
+ IN UINTN Length,
+ IN UINTN Type
+ );
+
+EFI_STATUS
+FindMatchingDebugRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN Address,
+ IN UINTN Length,
+ IN UINTN Type,
+ OUT UINTN *Register
+ );
+
+EFI_STATUS
+DisableDebugRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN Register
+ );
+
+VOID
+InitializeProcessor (
+ VOID
+ );
+
+/**
+ Send the T signal with the given exception type (in gdb order) and possibly with n:r pairs related to the watchpoints
+
+ @param SystemContext Register content at time of the exception
+ @param GdbExceptionType GDB exception type
+ **/
+VOID
+ProcessorSendTSignal (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINT8 GdbExceptionType,
+ IN OUT CHAR8 *TSignalPtr,
+ IN UINTN SizeOfBuffer
+ );
+
/**
Check to see if this exception is related to ctrl-c handling.
@@ -705,9 +705,9 @@ BOOLEAN ProcessorControlC (
IN EFI_EXCEPTION_TYPE ExceptionType,
IN OUT EFI_SYSTEM_CONTEXT SystemContext
- ); - - + );
+
+
/**
Initialize debug agent.
@@ -723,6 +723,6 @@ DebugAgentHookExceptions ( IN UINT32 InitFlag,
IN VOID *Context OPTIONAL
);
- - -#endif +
+
+#endif
diff --git a/EmbeddedPkg/Library/GdbDebugAgent/Ia32/Processor.c b/EmbeddedPkg/Library/GdbDebugAgent/Ia32/Processor.c index 96a1a596f3..4e4350a0f2 100755 --- a/EmbeddedPkg/Library/GdbDebugAgent/Ia32/Processor.c +++ b/EmbeddedPkg/Library/GdbDebugAgent/Ia32/Processor.c @@ -1,917 +1,917 @@ -/** @file - Processor specific parts of the GDB stub - - Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR> - - 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. - -**/ - +/** @file
+ Processor specific parts of the GDB stub
+
+ Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
+
+ 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 <GdbDebugAgent.h>
- -// -// Array of exception types that need to be hooked by the debugger -// {EFI mapping, GDB mapping} -// -EFI_EXCEPTION_TYPE_ENTRY gExceptionType[] = { - { EXCEPT_IA32_DIVIDE_ERROR, GDB_SIGFPE }, - { EXCEPT_IA32_DEBUG, GDB_SIGTRAP }, - { EXCEPT_IA32_NMI, GDB_SIGEMT }, - { EXCEPT_IA32_BREAKPOINT, GDB_SIGTRAP }, - { EXCEPT_IA32_OVERFLOW, GDB_SIGSEGV }, - { EXCEPT_IA32_BOUND, GDB_SIGSEGV }, - { EXCEPT_IA32_INVALID_OPCODE, GDB_SIGILL }, - { EXCEPT_IA32_DOUBLE_FAULT, GDB_SIGEMT }, - { EXCEPT_IA32_STACK_FAULT, GDB_SIGSEGV }, - { EXCEPT_IA32_GP_FAULT, GDB_SIGSEGV }, - { EXCEPT_IA32_PAGE_FAULT, GDB_SIGSEGV }, - { EXCEPT_IA32_FP_ERROR, GDB_SIGEMT }, - { EXCEPT_IA32_ALIGNMENT_CHECK, GDB_SIGEMT }, - { EXCEPT_IA32_MACHINE_CHECK, GDB_SIGEMT } -}; - - -// The offsets of registers SystemContext. -// The fields in the array are in the gdb ordering. -// -//16 regs -UINTN gRegisterOffsets[] = { - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Eax), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Ecx), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Edx), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Ebx), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Esp), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Ebp), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Esi), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Edi), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Eip), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Eflags), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Cs), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Ss), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Ds), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Es), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Fs), - OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Gs) -}; - - -//Debug only.. -VOID -PrintReg ( - IN EFI_SYSTEM_CONTEXT SystemContext - ) -{ - Print ((CHAR16 *)L"EAX: %x ", SystemContext.SystemContextIa32->Eax); - Print ((CHAR16 *)L"ECX: %x ", SystemContext.SystemContextIa32->Ecx); - Print ((CHAR16 *)L"EDX: %x ", SystemContext.SystemContextIa32->Edx); - Print ((CHAR16 *)L"EBX: %x ", SystemContext.SystemContextIa32->Ebx); - Print ((CHAR16 *)L"ESP: %x ", SystemContext.SystemContextIa32->Esp); - Print ((CHAR16 *)L"EBP: %x ", SystemContext.SystemContextIa32->Ebp); - Print ((CHAR16 *)L"ESI: %x ", SystemContext.SystemContextIa32->Esi); - Print ((CHAR16 *)L"EDI: %x ", SystemContext.SystemContextIa32->Edi); - Print ((CHAR16 *)L"EIP: %x\n", SystemContext.SystemContextIa32->Eip); - Print ((CHAR16 *)L"EFlags: %x\n", SystemContext.SystemContextIa32->Eflags); -} - -//Debug only.. -VOID -PrintDRreg ( - IN EFI_SYSTEM_CONTEXT SystemContext - ) -{ - Print ((CHAR16 *)L"DR0: %x ", SystemContext.SystemContextIa32->Dr0); - Print ((CHAR16 *)L"DR1: %x ", SystemContext.SystemContextIa32->Dr1); - Print ((CHAR16 *)L"DR2: %x ", SystemContext.SystemContextIa32->Dr2); - Print ((CHAR16 *)L"DR3: %x ", SystemContext.SystemContextIa32->Dr3); - Print ((CHAR16 *)L"DR6: %x ", SystemContext.SystemContextIa32->Dr6); - Print ((CHAR16 *)L"DR7: %x\n", SystemContext.SystemContextIa32->Dr7); -} - - -/** - Return the number of entries in the gExceptionType[] - - @retval UINTN, the number of entries in the gExceptionType[] array. - **/ -UINTN -MaxEfiException ( - VOID - ) -{ - return sizeof (gExceptionType)/sizeof (EFI_EXCEPTION_TYPE_ENTRY); -} - - -/** - Check to see if the ISA is supported. - ISA = Instruction Set Architecture - - @retval TRUE if Isa is supported, - FALSE otherwise. -**/ -BOOLEAN -CheckIsa ( - IN EFI_INSTRUCTION_SET_ARCHITECTURE Isa - ) -{ - return (BOOLEAN)(Isa == IsaIa32); -} - - -/** - This takes in the register number and the System Context, and returns a pointer to the RegNumber-th register in gdb ordering - It is, by default, set to find the register pointer of the IA32 member - - @param SystemContext Register content at time of the exception - @param RegNumber The register to which we want to find a pointer - @retval the pointer to the RegNumber-th pointer - **/ -UINTN * -FindPointerToRegister( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN RegNumber - ) -{ - UINT8 *TempPtr; - TempPtr = ((UINT8 *)SystemContext.SystemContextIa32) + gRegisterOffsets[RegNumber]; - return (UINTN *)TempPtr; -} - - -/** - Adds the RegNumber-th register's value to the output buffer, starting at the given OutBufPtr - - @param SystemContext Register content at time of the exception - @param RegNumber the number of the register that we want to read - @param OutBufPtr pointer to the output buffer's end. the new data will be added from this point on. - @retval the pointer to the next character of the output buffer that is available to be written on. - **/ -CHAR8 * -BasicReadRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN RegNumber, - IN CHAR8 *OutBufPtr - ) -{ - UINTN RegSize; - - RegSize = 0; - while (RegSize < REG_SIZE) { - *OutBufPtr++ = mHexToStr[((*FindPointerToRegister(SystemContext, RegNumber) >> (RegSize+4)) & 0xf)]; - *OutBufPtr++ = mHexToStr[((*FindPointerToRegister(SystemContext, RegNumber) >> RegSize) & 0xf)]; - RegSize = RegSize + 8; - } - return OutBufPtr; -} - - -/** ‘p n’ - Reads the n-th register's value into an output buffer and sends it as a packet - - @param SystemContext Register content at time of the exception - @param InBuffer Pointer to the input buffer received from gdb server - **/ -VOID -EFIAPI -ReadNthRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *InBuffer - ) -{ - UINTN RegNumber; - CHAR8 OutBuffer[9]; // 1 reg=8 hex chars, and the end '\0' (escape seq) - CHAR8 *OutBufPtr; // pointer to the output buffer - - RegNumber = AsciiStrHexToUintn (&InBuffer[1]); - - if ((RegNumber < 0) || (RegNumber >= sizeof (gRegisterOffsets)/sizeof (UINTN))) { - SendError (GDB_EINVALIDREGNUM); - return; - } - - OutBufPtr = OutBuffer; - OutBufPtr = BasicReadRegister(SystemContext, RegNumber, OutBufPtr); - - *OutBufPtr = '\0'; // the end of the buffer - SendPacket(OutBuffer); -} - - -/** ‘g’ - Reads the general registers into an output buffer and sends it as a packet - - @param SystemContext Register content at time of the exception - **/ -VOID -EFIAPI -ReadGeneralRegisters ( - IN EFI_SYSTEM_CONTEXT SystemContext - ) -{ - UINTN i; - CHAR8 OutBuffer[129]; // 16 regs, 8 hex chars each, and the end '\0' (escape seq) - CHAR8 *OutBufPtr; // pointer to the output buffer - - OutBufPtr = OutBuffer; - for(i = 0 ; i < sizeof (gRegisterOffsets)/sizeof (UINTN) ; i++) { // there are only 16 registers to read - OutBufPtr = BasicReadRegister(SystemContext, i, OutBufPtr); - } - - *OutBufPtr = '\0'; // the end of the buffer - SendPacket(OutBuffer); -} - - -/** - Adds the RegNumber-th register's value to the output buffer, starting at the given OutBufPtr - - @param SystemContext Register content at time of the exception - @param RegNumber the number of the register that we want to write - @param InBufPtr pointer to the output buffer. the new data will be extracted from the input buffer from this point on. - @retval the pointer to the next character of the input buffer that can be used - **/ -CHAR8 * -BasicWriteRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN RegNumber, - IN CHAR8 *InBufPtr - ) -{ - UINTN RegSize; - UINTN TempValue; // the value transferred from a hex char - UINT32 NewValue; // the new value of the RegNumber-th Register - - NewValue = 0; - RegSize = 0; - while (RegSize < REG_SIZE) { - TempValue = HexCharToInt(*InBufPtr++); - - if (TempValue < 0) { - SendError (GDB_EBADMEMDATA); - return NULL; - } - - NewValue += (TempValue << (RegSize+4)); - TempValue = HexCharToInt(*InBufPtr++); - - if (TempValue < 0) { - SendError (GDB_EBADMEMDATA); - return NULL; - } - - NewValue += (TempValue << RegSize); - RegSize = RegSize + 8; - } - *(FindPointerToRegister(SystemContext, RegNumber)) = NewValue; - return InBufPtr; -} - - -/** ‘P n...=r...’ - Writes the new value of n-th register received into the input buffer to the n-th register - - @param SystemContext Register content at time of the exception - @param InBuffer Ponter to the input buffer received from gdb server - **/ -VOID -EFIAPI -WriteNthRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *InBuffer - ) -{ - UINTN RegNumber; - CHAR8 RegNumBuffer[MAX_REG_NUM_BUF_SIZE]; // put the 'n..' part of the message into this array - CHAR8 *RegNumBufPtr; - CHAR8 *InBufPtr; // pointer to the input buffer - - // find the register number to write - InBufPtr = &InBuffer[1]; - RegNumBufPtr = RegNumBuffer; - while (*InBufPtr != '=') { - *RegNumBufPtr++ = *InBufPtr++; - } - *RegNumBufPtr = '\0'; - RegNumber = AsciiStrHexToUintn (RegNumBuffer); - - // check if this is a valid Register Number - if ((RegNumber < 0) || (RegNumber >= sizeof (gRegisterOffsets)/sizeof (UINTN))) { - SendError (GDB_EINVALIDREGNUM); - return; - } - InBufPtr++; // skips the '=' character - BasicWriteRegister (SystemContext, RegNumber, InBufPtr); - SendSuccess(); -} - - -/** ‘G XX...’ - Writes the new values received into the input buffer to the general registers - - @param SystemContext Register content at time of the exception - @param InBuffer Pointer to the input buffer received from gdb server - **/ -VOID -EFIAPI -WriteGeneralRegisters ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *InBuffer - ) -{ - UINTN i; - CHAR8 *InBufPtr; /// pointer to the input buffer - - // check to see if the buffer is the right size which is - // 1 (for 'G') + 16 (for 16 registers) * 8 ( for 8 hex chars each) = 129 - if (AsciiStrLen(InBuffer) != 129) { // 16 regs, 8 hex chars each, and the end '\0' (escape seq) - //Bad message. Message is not the right length - SendError (GDB_EBADBUFSIZE); - return; - } - - InBufPtr = &InBuffer[1]; - - // Read the new values for the registers from the input buffer to an array, NewValueArray. - // The values in the array are in the gdb ordering - for(i=0; i < sizeof (gRegisterOffsets)/sizeof (UINTN); i++) { // there are only 16 registers to write - InBufPtr = BasicWriteRegister(SystemContext, i, InBufPtr); - } - - SendSuccess(); -} - -/** ‘c [addr ]’ - Continue. addr is Address to resume. If addr is omitted, resume at current - Address. - - @param SystemContext Register content at time of the exception - **/ -VOID -EFIAPI -ContinueAtAddress ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *PacketData - ) -{ - if (PacketData[1] != '\0') { - SystemContext.SystemContextIa32->Eip = AsciiStrHexToUintn (&PacketData[1]); - } -} - - -/** ‘s [addr ]’ - Single step. addr is the Address at which to resume. If addr is omitted, resume - at same Address. - - @param SystemContext Register content at time of the exception - **/ -VOID -EFIAPI -SingleStep ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *PacketData - ) -{ +
+//
+// Array of exception types that need to be hooked by the debugger
+// {EFI mapping, GDB mapping}
+//
+EFI_EXCEPTION_TYPE_ENTRY gExceptionType[] = {
+ { EXCEPT_IA32_DIVIDE_ERROR, GDB_SIGFPE },
+ { EXCEPT_IA32_DEBUG, GDB_SIGTRAP },
+ { EXCEPT_IA32_NMI, GDB_SIGEMT },
+ { EXCEPT_IA32_BREAKPOINT, GDB_SIGTRAP },
+ { EXCEPT_IA32_OVERFLOW, GDB_SIGSEGV },
+ { EXCEPT_IA32_BOUND, GDB_SIGSEGV },
+ { EXCEPT_IA32_INVALID_OPCODE, GDB_SIGILL },
+ { EXCEPT_IA32_DOUBLE_FAULT, GDB_SIGEMT },
+ { EXCEPT_IA32_STACK_FAULT, GDB_SIGSEGV },
+ { EXCEPT_IA32_GP_FAULT, GDB_SIGSEGV },
+ { EXCEPT_IA32_PAGE_FAULT, GDB_SIGSEGV },
+ { EXCEPT_IA32_FP_ERROR, GDB_SIGEMT },
+ { EXCEPT_IA32_ALIGNMENT_CHECK, GDB_SIGEMT },
+ { EXCEPT_IA32_MACHINE_CHECK, GDB_SIGEMT }
+};
+
+
+// The offsets of registers SystemContext.
+// The fields in the array are in the gdb ordering.
+//
+//16 regs
+UINTN gRegisterOffsets[] = {
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Eax),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Ecx),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Edx),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Ebx),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Esp),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Ebp),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Esi),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Edi),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Eip),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Eflags),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Cs),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Ss),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Ds),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Es),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Fs),
+ OFFSET_OF(EFI_SYSTEM_CONTEXT_IA32, Gs)
+};
+
+
+//Debug only..
+VOID
+PrintReg (
+ IN EFI_SYSTEM_CONTEXT SystemContext
+ )
+{
+ Print ((CHAR16 *)L"EAX: %x ", SystemContext.SystemContextIa32->Eax);
+ Print ((CHAR16 *)L"ECX: %x ", SystemContext.SystemContextIa32->Ecx);
+ Print ((CHAR16 *)L"EDX: %x ", SystemContext.SystemContextIa32->Edx);
+ Print ((CHAR16 *)L"EBX: %x ", SystemContext.SystemContextIa32->Ebx);
+ Print ((CHAR16 *)L"ESP: %x ", SystemContext.SystemContextIa32->Esp);
+ Print ((CHAR16 *)L"EBP: %x ", SystemContext.SystemContextIa32->Ebp);
+ Print ((CHAR16 *)L"ESI: %x ", SystemContext.SystemContextIa32->Esi);
+ Print ((CHAR16 *)L"EDI: %x ", SystemContext.SystemContextIa32->Edi);
+ Print ((CHAR16 *)L"EIP: %x\n", SystemContext.SystemContextIa32->Eip);
+ Print ((CHAR16 *)L"EFlags: %x\n", SystemContext.SystemContextIa32->Eflags);
+}
+
+//Debug only..
+VOID
+PrintDRreg (
+ IN EFI_SYSTEM_CONTEXT SystemContext
+ )
+{
+ Print ((CHAR16 *)L"DR0: %x ", SystemContext.SystemContextIa32->Dr0);
+ Print ((CHAR16 *)L"DR1: %x ", SystemContext.SystemContextIa32->Dr1);
+ Print ((CHAR16 *)L"DR2: %x ", SystemContext.SystemContextIa32->Dr2);
+ Print ((CHAR16 *)L"DR3: %x ", SystemContext.SystemContextIa32->Dr3);
+ Print ((CHAR16 *)L"DR6: %x ", SystemContext.SystemContextIa32->Dr6);
+ Print ((CHAR16 *)L"DR7: %x\n", SystemContext.SystemContextIa32->Dr7);
+}
+
+
+/**
+ Return the number of entries in the gExceptionType[]
+
+ @retval UINTN, the number of entries in the gExceptionType[] array.
+ **/
+UINTN
+MaxEfiException (
+ VOID
+ )
+{
+ return sizeof (gExceptionType)/sizeof (EFI_EXCEPTION_TYPE_ENTRY);
+}
+
+
+/**
+ Check to see if the ISA is supported.
+ ISA = Instruction Set Architecture
+
+ @retval TRUE if Isa is supported,
+ FALSE otherwise.
+**/
+BOOLEAN
+CheckIsa (
+ IN EFI_INSTRUCTION_SET_ARCHITECTURE Isa
+ )
+{
+ return (BOOLEAN)(Isa == IsaIa32);
+}
+
+
+/**
+ This takes in the register number and the System Context, and returns a pointer to the RegNumber-th register in gdb ordering
+ It is, by default, set to find the register pointer of the IA32 member
+
+ @param SystemContext Register content at time of the exception
+ @param RegNumber The register to which we want to find a pointer
+ @retval the pointer to the RegNumber-th pointer
+ **/
+UINTN *
+FindPointerToRegister(
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN RegNumber
+ )
+{
+ UINT8 *TempPtr;
+ TempPtr = ((UINT8 *)SystemContext.SystemContextIa32) + gRegisterOffsets[RegNumber];
+ return (UINTN *)TempPtr;
+}
+
+
+/**
+ Adds the RegNumber-th register's value to the output buffer, starting at the given OutBufPtr
+
+ @param SystemContext Register content at time of the exception
+ @param RegNumber the number of the register that we want to read
+ @param OutBufPtr pointer to the output buffer's end. the new data will be added from this point on.
+ @retval the pointer to the next character of the output buffer that is available to be written on.
+ **/
+CHAR8 *
+BasicReadRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN RegNumber,
+ IN CHAR8 *OutBufPtr
+ )
+{
+ UINTN RegSize;
+
+ RegSize = 0;
+ while (RegSize < REG_SIZE) {
+ *OutBufPtr++ = mHexToStr[((*FindPointerToRegister(SystemContext, RegNumber) >> (RegSize+4)) & 0xf)];
+ *OutBufPtr++ = mHexToStr[((*FindPointerToRegister(SystemContext, RegNumber) >> RegSize) & 0xf)];
+ RegSize = RegSize + 8;
+ }
+ return OutBufPtr;
+}
+
+
+/** ‘p n’
+ Reads the n-th register's value into an output buffer and sends it as a packet
+
+ @param SystemContext Register content at time of the exception
+ @param InBuffer Pointer to the input buffer received from gdb server
+ **/
+VOID
+EFIAPI
+ReadNthRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *InBuffer
+ )
+{
+ UINTN RegNumber;
+ CHAR8 OutBuffer[9]; // 1 reg=8 hex chars, and the end '\0' (escape seq)
+ CHAR8 *OutBufPtr; // pointer to the output buffer
+
+ RegNumber = AsciiStrHexToUintn (&InBuffer[1]);
+
+ if ((RegNumber < 0) || (RegNumber >= sizeof (gRegisterOffsets)/sizeof (UINTN))) {
+ SendError (GDB_EINVALIDREGNUM);
+ return;
+ }
+
+ OutBufPtr = OutBuffer;
+ OutBufPtr = BasicReadRegister(SystemContext, RegNumber, OutBufPtr);
+
+ *OutBufPtr = '\0'; // the end of the buffer
+ SendPacket(OutBuffer);
+}
+
+
+/** ‘g’
+ Reads the general registers into an output buffer and sends it as a packet
+
+ @param SystemContext Register content at time of the exception
+ **/
+VOID
+EFIAPI
+ReadGeneralRegisters (
+ IN EFI_SYSTEM_CONTEXT SystemContext
+ )
+{
+ UINTN i;
+ CHAR8 OutBuffer[129]; // 16 regs, 8 hex chars each, and the end '\0' (escape seq)
+ CHAR8 *OutBufPtr; // pointer to the output buffer
+
+ OutBufPtr = OutBuffer;
+ for(i = 0 ; i < sizeof (gRegisterOffsets)/sizeof (UINTN) ; i++) { // there are only 16 registers to read
+ OutBufPtr = BasicReadRegister(SystemContext, i, OutBufPtr);
+ }
+
+ *OutBufPtr = '\0'; // the end of the buffer
+ SendPacket(OutBuffer);
+}
+
+
+/**
+ Adds the RegNumber-th register's value to the output buffer, starting at the given OutBufPtr
+
+ @param SystemContext Register content at time of the exception
+ @param RegNumber the number of the register that we want to write
+ @param InBufPtr pointer to the output buffer. the new data will be extracted from the input buffer from this point on.
+ @retval the pointer to the next character of the input buffer that can be used
+ **/
+CHAR8 *
+BasicWriteRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN RegNumber,
+ IN CHAR8 *InBufPtr
+ )
+{
+ UINTN RegSize;
+ UINTN TempValue; // the value transferred from a hex char
+ UINT32 NewValue; // the new value of the RegNumber-th Register
+
+ NewValue = 0;
+ RegSize = 0;
+ while (RegSize < REG_SIZE) {
+ TempValue = HexCharToInt(*InBufPtr++);
+
+ if (TempValue < 0) {
+ SendError (GDB_EBADMEMDATA);
+ return NULL;
+ }
+
+ NewValue += (TempValue << (RegSize+4));
+ TempValue = HexCharToInt(*InBufPtr++);
+
+ if (TempValue < 0) {
+ SendError (GDB_EBADMEMDATA);
+ return NULL;
+ }
+
+ NewValue += (TempValue << RegSize);
+ RegSize = RegSize + 8;
+ }
+ *(FindPointerToRegister(SystemContext, RegNumber)) = NewValue;
+ return InBufPtr;
+}
+
+
+/** ‘P n...=r...’
+ Writes the new value of n-th register received into the input buffer to the n-th register
+
+ @param SystemContext Register content at time of the exception
+ @param InBuffer Ponter to the input buffer received from gdb server
+ **/
+VOID
+EFIAPI
+WriteNthRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *InBuffer
+ )
+{
+ UINTN RegNumber;
+ CHAR8 RegNumBuffer[MAX_REG_NUM_BUF_SIZE]; // put the 'n..' part of the message into this array
+ CHAR8 *RegNumBufPtr;
+ CHAR8 *InBufPtr; // pointer to the input buffer
+
+ // find the register number to write
+ InBufPtr = &InBuffer[1];
+ RegNumBufPtr = RegNumBuffer;
+ while (*InBufPtr != '=') {
+ *RegNumBufPtr++ = *InBufPtr++;
+ }
+ *RegNumBufPtr = '\0';
+ RegNumber = AsciiStrHexToUintn (RegNumBuffer);
+
+ // check if this is a valid Register Number
+ if ((RegNumber < 0) || (RegNumber >= sizeof (gRegisterOffsets)/sizeof (UINTN))) {
+ SendError (GDB_EINVALIDREGNUM);
+ return;
+ }
+ InBufPtr++; // skips the '=' character
+ BasicWriteRegister (SystemContext, RegNumber, InBufPtr);
+ SendSuccess();
+}
+
+
+/** ‘G XX...’
+ Writes the new values received into the input buffer to the general registers
+
+ @param SystemContext Register content at time of the exception
+ @param InBuffer Pointer to the input buffer received from gdb server
+ **/
+VOID
+EFIAPI
+WriteGeneralRegisters (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *InBuffer
+ )
+{
+ UINTN i;
+ CHAR8 *InBufPtr; /// pointer to the input buffer
+
+ // check to see if the buffer is the right size which is
+ // 1 (for 'G') + 16 (for 16 registers) * 8 ( for 8 hex chars each) = 129
+ if (AsciiStrLen(InBuffer) != 129) { // 16 regs, 8 hex chars each, and the end '\0' (escape seq)
+ //Bad message. Message is not the right length
+ SendError (GDB_EBADBUFSIZE);
+ return;
+ }
+
+ InBufPtr = &InBuffer[1];
+
+ // Read the new values for the registers from the input buffer to an array, NewValueArray.
+ // The values in the array are in the gdb ordering
+ for(i=0; i < sizeof (gRegisterOffsets)/sizeof (UINTN); i++) { // there are only 16 registers to write
+ InBufPtr = BasicWriteRegister(SystemContext, i, InBufPtr);
+ }
+
+ SendSuccess();
+}
+
+/** ‘c [addr ]’
+ Continue. addr is Address to resume. If addr is omitted, resume at current
+ Address.
+
+ @param SystemContext Register content at time of the exception
+ **/
+VOID
+EFIAPI
+ContinueAtAddress (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *PacketData
+ )
+{
+ if (PacketData[1] != '\0') {
+ SystemContext.SystemContextIa32->Eip = AsciiStrHexToUintn (&PacketData[1]);
+ }
+}
+
+
+/** ‘s [addr ]’
+ Single step. addr is the Address at which to resume. If addr is omitted, resume
+ at same Address.
+
+ @param SystemContext Register content at time of the exception
+ **/
+VOID
+EFIAPI
+SingleStep (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *PacketData
+ )
+{
SendNotSupported();
-} - - -/** - Returns breakpoint data address from DR0-DR3 based on the input breakpoint number - - @param SystemContext Register content at time of the exception - @param BreakpointNumber Breakpoint number - - @retval Address Data address from DR0-DR3 based on the breakpoint number. - -**/ -UINTN -GetBreakpointDataAddress ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN BreakpointNumber - ) -{ - UINTN Address; - - if (BreakpointNumber == 1) { - Address = SystemContext.SystemContextIa32->Dr0; - } else if (BreakpointNumber == 2) { - Address = SystemContext.SystemContextIa32->Dr1; - } else if (BreakpointNumber == 3) { - Address = SystemContext.SystemContextIa32->Dr2; - } else if (BreakpointNumber == 4) { - Address = SystemContext.SystemContextIa32->Dr3; - } else { - Address = 0; - } - - return Address; -} - - -/** - Returns currently detected breakpoint value based on the register DR6 B0-B3 field. - If no breakpoint is detected then it returns 0. - - @param SystemContext Register content at time of the exception - - @retval {1-4} Currently detected breakpoint value - @retval 0 No breakpoint detected. - -**/ -UINTN -GetBreakpointDetected ( - IN EFI_SYSTEM_CONTEXT SystemContext - ) -{ - IA32_DR6 Dr6; - UINTN BreakpointNumber; - - Dr6.UintN = SystemContext.SystemContextIa32->Dr6; - - if (Dr6.Bits.B0 == 1) { - BreakpointNumber = 1; - } else if (Dr6.Bits.B1 == 1) { - BreakpointNumber = 2; - } else if (Dr6.Bits.B2 == 1) { - BreakpointNumber = 3; - } else if (Dr6.Bits.B3 == 1) { - BreakpointNumber = 4; - } else { - BreakpointNumber = 0; //No breakpoint detected - } - - return BreakpointNumber; -} - - -/** - Returns Breakpoint type (InstructionExecution, DataWrite, DataRead or DataReadWrite) - based on the Breakpoint number - - @param SystemContext Register content at time of the exception - @param BreakpointNumber Breakpoint number - - @retval BREAK_TYPE Breakpoint type value read from register DR7 RWn field - For unknown value, it returns NotSupported. - -**/ -BREAK_TYPE -GetBreakpointType ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN BreakpointNumber - ) -{ - IA32_DR7 Dr7; - BREAK_TYPE Type = NotSupported; //Default is NotSupported type - - Dr7.UintN = SystemContext.SystemContextIa32->Dr7; - - if (BreakpointNumber == 1) { - Type = (BREAK_TYPE) Dr7.Bits.RW0; - } else if (BreakpointNumber == 2) { - Type = (BREAK_TYPE) Dr7.Bits.RW1; - } else if (BreakpointNumber == 3) { - Type = (BREAK_TYPE) Dr7.Bits.RW2; - } else if (BreakpointNumber == 4) { - Type = (BREAK_TYPE) Dr7.Bits.RW3; - } - - return Type; -} - - -/** - Parses Length and returns the length which DR7 LENn field accepts. - For example: If we receive 1-Byte length then we should return 0. - Zero gets written to DR7 LENn field. - - @param Length Breakpoint length in Bytes (1 byte, 2 byte, 4 byte) - - @retval Length Appropriate converted values which DR7 LENn field accepts. - -**/ -UINTN -ConvertLengthData ( - IN UINTN Length - ) -{ - if (Length == 1) { //1-Byte length - return 0; - } else if (Length == 2) { //2-Byte length - return 1; - } else if (Length == 4) { //4-Byte length - return 3; - } else { //Undefined or 8-byte length - return 2; - } -} - - -/** - Finds the next free debug register. If all the registers are occupied then - EFI_OUT_OF_RESOURCES is returned. - - @param SystemContext Register content at time of the exception - @param Register Register value (0 - 3 for the first free debug register) - - @retval EFI_STATUS Appropriate status value. - -**/ -EFI_STATUS -FindNextFreeDebugRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - OUT UINTN *Register - ) -{ - IA32_DR7 Dr7; - - Dr7.UintN = SystemContext.SystemContextIa32->Dr7; - - if (Dr7.Bits.G0 == 0) { - *Register = 0; - } else if (Dr7.Bits.G1 == 0) { - *Register = 1; - } else if (Dr7.Bits.G2 == 0) { - *Register = 2; - } else if (Dr7.Bits.G3 == 0) { - *Register = 3; - } else { - return EFI_OUT_OF_RESOURCES; - } - - return EFI_SUCCESS; -} - - -/** - Enables the debug register. Writes Address value to appropriate DR0-3 register. - Sets LENn, Gn, RWn bits in DR7 register. - - @param SystemContext Register content at time of the exception - @param Register Register value (0 - 3) - @param Address Breakpoint address value - @param Type Breakpoint type (Instruction, Data write, Data read - or write etc.) - - @retval EFI_STATUS Appropriate status value. - -**/ -EFI_STATUS -EnableDebugRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN Register, - IN UINTN Address, - IN UINTN Length, - IN UINTN Type - ) -{ - IA32_DR7 Dr7; - - //Convert length data - Length = ConvertLengthData (Length); - - //For Instruction execution, length should be 0 - //(Ref. Intel reference manual 18.2.4) - if ((Type == 0) && (Length != 0)) { - return EFI_INVALID_PARAMETER; - } - - //Hardware doesn't support ReadWatch (z3 packet) type. GDB can handle - //software breakpoint. We should send empty packet in both these cases. - if ((Type == (BREAK_TYPE)DataRead) || - (Type == (BREAK_TYPE)SoftwareBreakpoint)) { - return EFI_UNSUPPORTED; - } - - //Read DR7 so appropriate Gn, RWn and LENn bits can be modified. - Dr7.UintN = SystemContext.SystemContextIa32->Dr7; - - if (Register == 0) { - SystemContext.SystemContextIa32->Dr0 = Address; - Dr7.Bits.G0 = 1; - Dr7.Bits.RW0 = Type; - Dr7.Bits.LEN0 = Length; - } else if (Register == 1) { - SystemContext.SystemContextIa32->Dr1 = Address; - Dr7.Bits.G1 = 1; - Dr7.Bits.RW1 = Type; - Dr7.Bits.LEN1 = Length; - } else if (Register == 2) { - SystemContext.SystemContextIa32->Dr2 = Address; - Dr7.Bits.G2 = 1; - Dr7.Bits.RW2 = Type; - Dr7.Bits.LEN2 = Length; - } else if (Register == 3) { - SystemContext.SystemContextIa32->Dr3 = Address; - Dr7.Bits.G3 = 1; - Dr7.Bits.RW3 = Type; - Dr7.Bits.LEN3 = Length; - } else { - return EFI_INVALID_PARAMETER; - } - - //Update Dr7 with appropriate Gn, RWn and LENn bits - SystemContext.SystemContextIa32->Dr7 = Dr7.UintN; - - return EFI_SUCCESS; -} - - -/** - Returns register number 0 - 3 for the maching debug register. - This function compares incoming Address, Type, Length and - if there is a match then it returns the appropriate register number. - In case of mismatch, function returns EFI_NOT_FOUND message. - - @param SystemContext Register content at time of the exception - @param Address Breakpoint address value - @param Length Breakpoint length value - @param Type Breakpoint type (Instruction, Data write, - Data read or write etc.) - @param Register Register value to be returned - - @retval EFI_STATUS Appropriate status value. - -**/ -EFI_STATUS -FindMatchingDebugRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN Address, - IN UINTN Length, - IN UINTN Type, - OUT UINTN *Register - ) -{ - IA32_DR7 Dr7; - - //Hardware doesn't support ReadWatch (z3 packet) type. GDB can handle - //software breakpoint. We should send empty packet in both these cases. - if ((Type == (BREAK_TYPE)DataRead) || - (Type == (BREAK_TYPE)SoftwareBreakpoint)) { - return EFI_UNSUPPORTED; - } - - //Convert length data - Length = ConvertLengthData(Length); - - Dr7.UintN = SystemContext.SystemContextIa32->Dr7; - - if ((Dr7.Bits.G0 == 1) && - (Dr7.Bits.LEN0 == Length) && - (Dr7.Bits.RW0 == Type) && - (Address == SystemContext.SystemContextIa32->Dr0)) { - *Register = 0; - } else if ((Dr7.Bits.G1 == 1) && - (Dr7.Bits.LEN1 == Length) && - (Dr7.Bits.RW1 == Type) && - (Address == SystemContext.SystemContextIa32->Dr1)) { - *Register = 1; - } else if ((Dr7.Bits.G2 == 1) && - (Dr7.Bits.LEN2 == Length) && - (Dr7.Bits.RW2 == Type) && - (Address == SystemContext.SystemContextIa32->Dr2)) { - *Register = 2; - } else if ((Dr7.Bits.G3 == 1) && - (Dr7.Bits.LEN3 == Length) && - (Dr7.Bits.RW3 == Type) && - (Address == SystemContext.SystemContextIa32->Dr3)) { - *Register = 3; - } else { - Print ((CHAR16 *)L"No match found..\n"); - return EFI_NOT_FOUND; - } - - return EFI_SUCCESS; -} - - -/** - Disables the particular debug register. - - @param SystemContext Register content at time of the exception - @param Register Register to be disabled - - @retval EFI_STATUS Appropriate status value. - -**/ -EFI_STATUS -DisableDebugRegister ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN UINTN Register - ) -{ - IA32_DR7 Dr7; - UINTN Address = 0; - - //Read DR7 register so appropriate Gn, RWn and LENn bits can be turned off. - Dr7.UintN = SystemContext.SystemContextIa32->Dr7; - - if (Register == 0) { - SystemContext.SystemContextIa32->Dr0 = Address; - Dr7.Bits.G0 = 0; - Dr7.Bits.RW0 = 0; - Dr7.Bits.LEN0 = 0; - } else if (Register == 1) { - SystemContext.SystemContextIa32->Dr1 = Address; - Dr7.Bits.G1 = 0; - Dr7.Bits.RW1 = 0; - Dr7.Bits.LEN1 = 0; - } else if (Register == 2) { - SystemContext.SystemContextIa32->Dr2 = Address; - Dr7.Bits.G2 = 0; - Dr7.Bits.RW2 = 0; - Dr7.Bits.LEN2 = 0; - } else if (Register == 3) { - SystemContext.SystemContextIa32->Dr3 = Address; - Dr7.Bits.G3 = 0; - Dr7.Bits.RW3 = 0; - Dr7.Bits.LEN3 = 0; - } else { - return EFI_INVALID_PARAMETER; - } - - //Update DR7 register so appropriate Gn, RWn and LENn bits can be turned off. - SystemContext.SystemContextIa32->Dr7 = Dr7.UintN; - - return EFI_SUCCESS; -} - - -/** - ‘Z1, [addr], [length]’ - ‘Z2, [addr], [length]’ - ‘Z3, [addr], [length]’ - ‘Z4, [addr], [length]’ - - Insert hardware breakpoint/watchpoint at address addr of size length - - @param SystemContext Register content at time of the exception - @param *PacketData Pointer to the Payload data for the packet - -**/ -VOID -EFIAPI -InsertBreakPoint ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *PacketData - ) -{ - UINTN Type; - UINTN Address; - UINTN Length; - UINTN Register; - EFI_STATUS Status; - BREAK_TYPE BreakType = NotSupported; - UINTN ErrorCode; - - ErrorCode = ParseBreakpointPacket (PacketData, &Type, &Address, &Length); - if (ErrorCode > 0) { - SendError ((UINT8)ErrorCode); - return; - } - - switch (Type) { - - case 0: //Software breakpoint - BreakType = SoftwareBreakpoint; - break; - - case 1: //Hardware breakpoint - BreakType = InstructionExecution; - break; - - case 2: //Write watchpoint - BreakType = DataWrite; - break; - - case 3: //Read watchpoint - BreakType = DataRead; - break; - - case 4: //Access watchpoint - BreakType = DataReadWrite; - break; - - default : - Print ((CHAR16 *)L"Insert breakpoint default: %x\n", Type); - SendError (GDB_EINVALIDBRKPOINTTYPE); - return; - } - - // Find next free debug register - Status = FindNextFreeDebugRegister (SystemContext, &Register); - if (EFI_ERROR(Status)) { - Print ((CHAR16 *)L"No space left on device\n"); - SendError (GDB_ENOSPACE); - return; - } - - // Write Address, length data at particular DR register - Status = EnableDebugRegister (SystemContext, Register, Address, Length, (UINTN)BreakType); - if (EFI_ERROR(Status)) { - if (Status == EFI_UNSUPPORTED) { - SendNotSupported(); - return; - } - - SendError (GDB_EINVALIDARG); - return; - } - - SendSuccess (); -} - - -/** - ‘z1, [addr], [length]’ - ‘z2, [addr], [length]’ - ‘z3, [addr], [length]’ - ‘z4, [addr], [length]’ - - Remove hardware breakpoint/watchpoint at address addr of size length - - @param *PacketData Pointer to the Payload data for the packet - -**/ -VOID -EFIAPI -RemoveBreakPoint ( - IN EFI_SYSTEM_CONTEXT SystemContext, - IN CHAR8 *PacketData - ) -{ - UINTN Type; - UINTN Address; - UINTN Length; - UINTN Register; - BREAK_TYPE BreakType = NotSupported; - EFI_STATUS Status; - UINTN ErrorCode; - - //Parse breakpoint packet data - ErrorCode = ParseBreakpointPacket (PacketData, &Type, &Address, &Length); - if (ErrorCode > 0) { - SendError ((UINT8)ErrorCode); - return; - } - - switch (Type) { - - case 0: //Software breakpoint - BreakType = SoftwareBreakpoint; - break; - - case 1: //Hardware breakpoint - BreakType = InstructionExecution; - break; - - case 2: //Write watchpoint - BreakType = DataWrite; - break; - - case 3: //Read watchpoint - BreakType = DataRead; - break; - - case 4: //Access watchpoint - BreakType = DataReadWrite; - break; - - default : - SendError (GDB_EINVALIDBRKPOINTTYPE); - return; - } - - //Find matching debug register - Status = FindMatchingDebugRegister (SystemContext, Address, Length, (UINTN)BreakType, &Register); - if (EFI_ERROR(Status)) { - if (Status == EFI_UNSUPPORTED) { - SendNotSupported(); - return; - } - - SendError (GDB_ENOSPACE); - return; - } - - //Remove breakpoint - Status = DisableDebugRegister(SystemContext, Register); - if (EFI_ERROR(Status)) { - SendError (GDB_EINVALIDARG); - return; - } - - SendSuccess (); -} - - +}
+
+
+/**
+ Returns breakpoint data address from DR0-DR3 based on the input breakpoint number
+
+ @param SystemContext Register content at time of the exception
+ @param BreakpointNumber Breakpoint number
+
+ @retval Address Data address from DR0-DR3 based on the breakpoint number.
+
+**/
+UINTN
+GetBreakpointDataAddress (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN BreakpointNumber
+ )
+{
+ UINTN Address;
+
+ if (BreakpointNumber == 1) {
+ Address = SystemContext.SystemContextIa32->Dr0;
+ } else if (BreakpointNumber == 2) {
+ Address = SystemContext.SystemContextIa32->Dr1;
+ } else if (BreakpointNumber == 3) {
+ Address = SystemContext.SystemContextIa32->Dr2;
+ } else if (BreakpointNumber == 4) {
+ Address = SystemContext.SystemContextIa32->Dr3;
+ } else {
+ Address = 0;
+ }
+
+ return Address;
+}
+
+
+/**
+ Returns currently detected breakpoint value based on the register DR6 B0-B3 field.
+ If no breakpoint is detected then it returns 0.
+
+ @param SystemContext Register content at time of the exception
+
+ @retval {1-4} Currently detected breakpoint value
+ @retval 0 No breakpoint detected.
+
+**/
+UINTN
+GetBreakpointDetected (
+ IN EFI_SYSTEM_CONTEXT SystemContext
+ )
+{
+ IA32_DR6 Dr6;
+ UINTN BreakpointNumber;
+
+ Dr6.UintN = SystemContext.SystemContextIa32->Dr6;
+
+ if (Dr6.Bits.B0 == 1) {
+ BreakpointNumber = 1;
+ } else if (Dr6.Bits.B1 == 1) {
+ BreakpointNumber = 2;
+ } else if (Dr6.Bits.B2 == 1) {
+ BreakpointNumber = 3;
+ } else if (Dr6.Bits.B3 == 1) {
+ BreakpointNumber = 4;
+ } else {
+ BreakpointNumber = 0; //No breakpoint detected
+ }
+
+ return BreakpointNumber;
+}
+
+
+/**
+ Returns Breakpoint type (InstructionExecution, DataWrite, DataRead or DataReadWrite)
+ based on the Breakpoint number
+
+ @param SystemContext Register content at time of the exception
+ @param BreakpointNumber Breakpoint number
+
+ @retval BREAK_TYPE Breakpoint type value read from register DR7 RWn field
+ For unknown value, it returns NotSupported.
+
+**/
+BREAK_TYPE
+GetBreakpointType (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN BreakpointNumber
+ )
+{
+ IA32_DR7 Dr7;
+ BREAK_TYPE Type = NotSupported; //Default is NotSupported type
+
+ Dr7.UintN = SystemContext.SystemContextIa32->Dr7;
+
+ if (BreakpointNumber == 1) {
+ Type = (BREAK_TYPE) Dr7.Bits.RW0;
+ } else if (BreakpointNumber == 2) {
+ Type = (BREAK_TYPE) Dr7.Bits.RW1;
+ } else if (BreakpointNumber == 3) {
+ Type = (BREAK_TYPE) Dr7.Bits.RW2;
+ } else if (BreakpointNumber == 4) {
+ Type = (BREAK_TYPE) Dr7.Bits.RW3;
+ }
+
+ return Type;
+}
+
+
+/**
+ Parses Length and returns the length which DR7 LENn field accepts.
+ For example: If we receive 1-Byte length then we should return 0.
+ Zero gets written to DR7 LENn field.
+
+ @param Length Breakpoint length in Bytes (1 byte, 2 byte, 4 byte)
+
+ @retval Length Appropriate converted values which DR7 LENn field accepts.
+
+**/
+UINTN
+ConvertLengthData (
+ IN UINTN Length
+ )
+{
+ if (Length == 1) { //1-Byte length
+ return 0;
+ } else if (Length == 2) { //2-Byte length
+ return 1;
+ } else if (Length == 4) { //4-Byte length
+ return 3;
+ } else { //Undefined or 8-byte length
+ return 2;
+ }
+}
+
+
+/**
+ Finds the next free debug register. If all the registers are occupied then
+ EFI_OUT_OF_RESOURCES is returned.
+
+ @param SystemContext Register content at time of the exception
+ @param Register Register value (0 - 3 for the first free debug register)
+
+ @retval EFI_STATUS Appropriate status value.
+
+**/
+EFI_STATUS
+FindNextFreeDebugRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ OUT UINTN *Register
+ )
+{
+ IA32_DR7 Dr7;
+
+ Dr7.UintN = SystemContext.SystemContextIa32->Dr7;
+
+ if (Dr7.Bits.G0 == 0) {
+ *Register = 0;
+ } else if (Dr7.Bits.G1 == 0) {
+ *Register = 1;
+ } else if (Dr7.Bits.G2 == 0) {
+ *Register = 2;
+ } else if (Dr7.Bits.G3 == 0) {
+ *Register = 3;
+ } else {
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ return EFI_SUCCESS;
+}
+
+
+/**
+ Enables the debug register. Writes Address value to appropriate DR0-3 register.
+ Sets LENn, Gn, RWn bits in DR7 register.
+
+ @param SystemContext Register content at time of the exception
+ @param Register Register value (0 - 3)
+ @param Address Breakpoint address value
+ @param Type Breakpoint type (Instruction, Data write, Data read
+ or write etc.)
+
+ @retval EFI_STATUS Appropriate status value.
+
+**/
+EFI_STATUS
+EnableDebugRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN Register,
+ IN UINTN Address,
+ IN UINTN Length,
+ IN UINTN Type
+ )
+{
+ IA32_DR7 Dr7;
+
+ //Convert length data
+ Length = ConvertLengthData (Length);
+
+ //For Instruction execution, length should be 0
+ //(Ref. Intel reference manual 18.2.4)
+ if ((Type == 0) && (Length != 0)) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ //Hardware doesn't support ReadWatch (z3 packet) type. GDB can handle
+ //software breakpoint. We should send empty packet in both these cases.
+ if ((Type == (BREAK_TYPE)DataRead) ||
+ (Type == (BREAK_TYPE)SoftwareBreakpoint)) {
+ return EFI_UNSUPPORTED;
+ }
+
+ //Read DR7 so appropriate Gn, RWn and LENn bits can be modified.
+ Dr7.UintN = SystemContext.SystemContextIa32->Dr7;
+
+ if (Register == 0) {
+ SystemContext.SystemContextIa32->Dr0 = Address;
+ Dr7.Bits.G0 = 1;
+ Dr7.Bits.RW0 = Type;
+ Dr7.Bits.LEN0 = Length;
+ } else if (Register == 1) {
+ SystemContext.SystemContextIa32->Dr1 = Address;
+ Dr7.Bits.G1 = 1;
+ Dr7.Bits.RW1 = Type;
+ Dr7.Bits.LEN1 = Length;
+ } else if (Register == 2) {
+ SystemContext.SystemContextIa32->Dr2 = Address;
+ Dr7.Bits.G2 = 1;
+ Dr7.Bits.RW2 = Type;
+ Dr7.Bits.LEN2 = Length;
+ } else if (Register == 3) {
+ SystemContext.SystemContextIa32->Dr3 = Address;
+ Dr7.Bits.G3 = 1;
+ Dr7.Bits.RW3 = Type;
+ Dr7.Bits.LEN3 = Length;
+ } else {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ //Update Dr7 with appropriate Gn, RWn and LENn bits
+ SystemContext.SystemContextIa32->Dr7 = Dr7.UintN;
+
+ return EFI_SUCCESS;
+}
+
+
+/**
+ Returns register number 0 - 3 for the maching debug register.
+ This function compares incoming Address, Type, Length and
+ if there is a match then it returns the appropriate register number.
+ In case of mismatch, function returns EFI_NOT_FOUND message.
+
+ @param SystemContext Register content at time of the exception
+ @param Address Breakpoint address value
+ @param Length Breakpoint length value
+ @param Type Breakpoint type (Instruction, Data write,
+ Data read or write etc.)
+ @param Register Register value to be returned
+
+ @retval EFI_STATUS Appropriate status value.
+
+**/
+EFI_STATUS
+FindMatchingDebugRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN Address,
+ IN UINTN Length,
+ IN UINTN Type,
+ OUT UINTN *Register
+ )
+{
+ IA32_DR7 Dr7;
+
+ //Hardware doesn't support ReadWatch (z3 packet) type. GDB can handle
+ //software breakpoint. We should send empty packet in both these cases.
+ if ((Type == (BREAK_TYPE)DataRead) ||
+ (Type == (BREAK_TYPE)SoftwareBreakpoint)) {
+ return EFI_UNSUPPORTED;
+ }
+
+ //Convert length data
+ Length = ConvertLengthData(Length);
+
+ Dr7.UintN = SystemContext.SystemContextIa32->Dr7;
+
+ if ((Dr7.Bits.G0 == 1) &&
+ (Dr7.Bits.LEN0 == Length) &&
+ (Dr7.Bits.RW0 == Type) &&
+ (Address == SystemContext.SystemContextIa32->Dr0)) {
+ *Register = 0;
+ } else if ((Dr7.Bits.G1 == 1) &&
+ (Dr7.Bits.LEN1 == Length) &&
+ (Dr7.Bits.RW1 == Type) &&
+ (Address == SystemContext.SystemContextIa32->Dr1)) {
+ *Register = 1;
+ } else if ((Dr7.Bits.G2 == 1) &&
+ (Dr7.Bits.LEN2 == Length) &&
+ (Dr7.Bits.RW2 == Type) &&
+ (Address == SystemContext.SystemContextIa32->Dr2)) {
+ *Register = 2;
+ } else if ((Dr7.Bits.G3 == 1) &&
+ (Dr7.Bits.LEN3 == Length) &&
+ (Dr7.Bits.RW3 == Type) &&
+ (Address == SystemContext.SystemContextIa32->Dr3)) {
+ *Register = 3;
+ } else {
+ Print ((CHAR16 *)L"No match found..\n");
+ return EFI_NOT_FOUND;
+ }
+
+ return EFI_SUCCESS;
+}
+
+
+/**
+ Disables the particular debug register.
+
+ @param SystemContext Register content at time of the exception
+ @param Register Register to be disabled
+
+ @retval EFI_STATUS Appropriate status value.
+
+**/
+EFI_STATUS
+DisableDebugRegister (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN UINTN Register
+ )
+{
+ IA32_DR7 Dr7;
+ UINTN Address = 0;
+
+ //Read DR7 register so appropriate Gn, RWn and LENn bits can be turned off.
+ Dr7.UintN = SystemContext.SystemContextIa32->Dr7;
+
+ if (Register == 0) {
+ SystemContext.SystemContextIa32->Dr0 = Address;
+ Dr7.Bits.G0 = 0;
+ Dr7.Bits.RW0 = 0;
+ Dr7.Bits.LEN0 = 0;
+ } else if (Register == 1) {
+ SystemContext.SystemContextIa32->Dr1 = Address;
+ Dr7.Bits.G1 = 0;
+ Dr7.Bits.RW1 = 0;
+ Dr7.Bits.LEN1 = 0;
+ } else if (Register == 2) {
+ SystemContext.SystemContextIa32->Dr2 = Address;
+ Dr7.Bits.G2 = 0;
+ Dr7.Bits.RW2 = 0;
+ Dr7.Bits.LEN2 = 0;
+ } else if (Register == 3) {
+ SystemContext.SystemContextIa32->Dr3 = Address;
+ Dr7.Bits.G3 = 0;
+ Dr7.Bits.RW3 = 0;
+ Dr7.Bits.LEN3 = 0;
+ } else {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ //Update DR7 register so appropriate Gn, RWn and LENn bits can be turned off.
+ SystemContext.SystemContextIa32->Dr7 = Dr7.UintN;
+
+ return EFI_SUCCESS;
+}
+
+
+/**
+ ‘Z1, [addr], [length]’
+ ‘Z2, [addr], [length]’
+ ‘Z3, [addr], [length]’
+ ‘Z4, [addr], [length]’
+
+ Insert hardware breakpoint/watchpoint at address addr of size length
+
+ @param SystemContext Register content at time of the exception
+ @param *PacketData Pointer to the Payload data for the packet
+
+**/
+VOID
+EFIAPI
+InsertBreakPoint (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *PacketData
+ )
+{
+ UINTN Type;
+ UINTN Address;
+ UINTN Length;
+ UINTN Register;
+ EFI_STATUS Status;
+ BREAK_TYPE BreakType = NotSupported;
+ UINTN ErrorCode;
+
+ ErrorCode = ParseBreakpointPacket (PacketData, &Type, &Address, &Length);
+ if (ErrorCode > 0) {
+ SendError ((UINT8)ErrorCode);
+ return;
+ }
+
+ switch (Type) {
+
+ case 0: //Software breakpoint
+ BreakType = SoftwareBreakpoint;
+ break;
+
+ case 1: //Hardware breakpoint
+ BreakType = InstructionExecution;
+ break;
+
+ case 2: //Write watchpoint
+ BreakType = DataWrite;
+ break;
+
+ case 3: //Read watchpoint
+ BreakType = DataRead;
+ break;
+
+ case 4: //Access watchpoint
+ BreakType = DataReadWrite;
+ break;
+
+ default :
+ Print ((CHAR16 *)L"Insert breakpoint default: %x\n", Type);
+ SendError (GDB_EINVALIDBRKPOINTTYPE);
+ return;
+ }
+
+ // Find next free debug register
+ Status = FindNextFreeDebugRegister (SystemContext, &Register);
+ if (EFI_ERROR(Status)) {
+ Print ((CHAR16 *)L"No space left on device\n");
+ SendError (GDB_ENOSPACE);
+ return;
+ }
+
+ // Write Address, length data at particular DR register
+ Status = EnableDebugRegister (SystemContext, Register, Address, Length, (UINTN)BreakType);
+ if (EFI_ERROR(Status)) {
+ if (Status == EFI_UNSUPPORTED) {
+ SendNotSupported();
+ return;
+ }
+
+ SendError (GDB_EINVALIDARG);
+ return;
+ }
+
+ SendSuccess ();
+}
+
+
+/**
+ ‘z1, [addr], [length]’
+ ‘z2, [addr], [length]’
+ ‘z3, [addr], [length]’
+ ‘z4, [addr], [length]’
+
+ Remove hardware breakpoint/watchpoint at address addr of size length
+
+ @param *PacketData Pointer to the Payload data for the packet
+
+**/
+VOID
+EFIAPI
+RemoveBreakPoint (
+ IN EFI_SYSTEM_CONTEXT SystemContext,
+ IN CHAR8 *PacketData
+ )
+{
+ UINTN Type;
+ UINTN Address;
+ UINTN Length;
+ UINTN Register;
+ BREAK_TYPE BreakType = NotSupported;
+ EFI_STATUS Status;
+ UINTN ErrorCode;
+
+ //Parse breakpoint packet data
+ ErrorCode = ParseBreakpointPacket (PacketData, &Type, &Address, &Length);
+ if (ErrorCode > 0) {
+ SendError ((UINT8)ErrorCode);
+ return;
+ }
+
+ switch (Type) {
+
+ case 0: //Software breakpoint
+ BreakType = SoftwareBreakpoint;
+ break;
+
+ case 1: //Hardware breakpoint
+ BreakType = InstructionExecution;
+ break;
+
+ case 2: //Write watchpoint
+ BreakType = DataWrite;
+ break;
+
+ case 3: //Read watchpoint
+ BreakType = DataRead;
+ break;
+
+ case 4: //Access watchpoint
+ BreakType = DataReadWrite;
+ break;
+
+ default :
+ SendError (GDB_EINVALIDBRKPOINTTYPE);
+ return;
+ }
+
+ //Find matching debug register
+ Status = FindMatchingDebugRegister (SystemContext, Address, Length, (UINTN)BreakType, &Register);
+ if (EFI_ERROR(Status)) {
+ if (Status == EFI_UNSUPPORTED) {
+ SendNotSupported();
+ return;
+ }
+
+ SendError (GDB_ENOSPACE);
+ return;
+ }
+
+ //Remove breakpoint
+ Status = DisableDebugRegister(SystemContext, Register);
+ if (EFI_ERROR(Status)) {
+ SendError (GDB_EINVALIDARG);
+ return;
+ }
+
+ SendSuccess ();
+}
+
+
/**
Initialize debug agent.
diff --git a/EmbeddedPkg/Library/NullDmaLib/NullDmaLib.inf b/EmbeddedPkg/Library/NullDmaLib/NullDmaLib.inf index 4897a8a969..8e8ccaee38 100755 --- a/EmbeddedPkg/Library/NullDmaLib/NullDmaLib.inf +++ b/EmbeddedPkg/Library/NullDmaLib/NullDmaLib.inf @@ -38,4 +38,4 @@ [Pcd]
[Depex]
- TRUE
\ No newline at end of file + TRUE
\ No newline at end of file diff --git a/EmbeddedPkg/Library/PrePiHobLib/Hob.c b/EmbeddedPkg/Library/PrePiHobLib/Hob.c index a92a1de52e..320dddfc25 100644 --- a/EmbeddedPkg/Library/PrePiHobLib/Hob.c +++ b/EmbeddedPkg/Library/PrePiHobLib/Hob.c @@ -1,852 +1,852 @@ -/** @file - - Copyright (c) 2010, Apple Inc. All rights reserved.<BR> - - 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 <PiPei.h> - -#include <Library/BaseLib.h> -#include <Library/BaseMemoryLib.h> -#include <Library/DebugLib.h> -#include <Library/PeCoffLib.h> -#include <Library/HobLib.h> -#include <Library/PcdLib.h> -#include <Library/PrePiHobListPointerLib.h> - -#include <Protocol/PeCoffLoader.h> -#include <Guid/ExtractSection.h> -#include <Guid/MemoryTypeInformation.h> -#include <Guid/MemoryAllocationHob.h> - -VOID -BuildMemoryTypeInformationHob ( - VOID - ); - -/** - Returns the pointer to the HOB list. - - This function returns the pointer to first HOB in the list. - - @return The pointer to the HOB list. - -**/ -VOID * -EFIAPI -GetHobList ( - VOID - ) -{ - return PrePeiGetHobList (); -} - - - -/** - Updates the pointer to the HOB list. - - @param HobList Hob list pointer to store - -**/ -EFI_STATUS -EFIAPI -SetHobList ( - IN VOID *HobList - ) -{ - return PrePeiSetHobList (HobList); -} - -/** - - -**/ -EFI_HOB_HANDOFF_INFO_TABLE* -HobConstructor ( - IN VOID *EfiMemoryBegin, - IN UINTN EfiMemoryLength, - IN VOID *EfiFreeMemoryBottom, - IN VOID *EfiFreeMemoryTop - ) -{ - EFI_HOB_HANDOFF_INFO_TABLE *Hob; - EFI_HOB_GENERIC_HEADER *HobEnd; - - Hob = EfiFreeMemoryBottom; - HobEnd = (EFI_HOB_GENERIC_HEADER *)(Hob+1); - - Hob->Header.HobType = EFI_HOB_TYPE_HANDOFF; - Hob->Header.HobLength = sizeof(EFI_HOB_HANDOFF_INFO_TABLE); - Hob->Header.Reserved = 0; - - HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST; - HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER); - HobEnd->Reserved = 0; - - Hob->Version = EFI_HOB_HANDOFF_TABLE_VERSION; - Hob->BootMode = BOOT_WITH_FULL_CONFIGURATION; - - Hob->EfiMemoryTop = (UINTN)EfiMemoryBegin + EfiMemoryLength; - Hob->EfiMemoryBottom = (UINTN)EfiMemoryBegin; - Hob->EfiFreeMemoryTop = (UINTN)EfiFreeMemoryTop; - Hob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS)(UINTN)(HobEnd+1); - Hob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd; - - return Hob; -} - -VOID * -CreateHob ( - IN UINT16 HobType, - IN UINT16 HobLength - ) -{ - EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob; - EFI_HOB_GENERIC_HEADER *HobEnd; - EFI_PHYSICAL_ADDRESS FreeMemory; - VOID *Hob; - - HandOffHob = GetHobList (); - - HobLength = (UINT16)((HobLength + 0x7) & (~0x7)); - - FreeMemory = HandOffHob->EfiFreeMemoryTop - HandOffHob->EfiFreeMemoryBottom; - - if (FreeMemory < HobLength) { - return NULL; - } - - Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList; - ((EFI_HOB_GENERIC_HEADER*) Hob)->HobType = HobType; - ((EFI_HOB_GENERIC_HEADER*) Hob)->HobLength = HobLength; - ((EFI_HOB_GENERIC_HEADER*) Hob)->Reserved = 0; - - HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN)Hob + HobLength); - HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd; - - HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST; - HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER); - HobEnd->Reserved = 0; - HobEnd++; - HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd; - - return Hob; -} - -/** - Builds a HOB that describes a chunk of system memory. - - This function builds a HOB that describes a chunk of system memory. - If there is no additional space for HOB creation, then ASSERT(). - - @param ResourceType The type of resource described by this HOB. - @param ResourceAttribute The resource attributes of the memory described by this HOB. - @param PhysicalStart The 64 bit physical address of memory described by this HOB. - @param NumberOfBytes The length of the memory described by this HOB in bytes. - -**/ -VOID -EFIAPI -BuildResourceDescriptorHob ( - IN EFI_RESOURCE_TYPE ResourceType, - IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute, - IN EFI_PHYSICAL_ADDRESS PhysicalStart, - IN UINT64 NumberOfBytes - ) -{ - EFI_HOB_RESOURCE_DESCRIPTOR *Hob; - - Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR)); - ASSERT(Hob != NULL); - - Hob->ResourceType = ResourceType; - Hob->ResourceAttribute = ResourceAttribute; - Hob->PhysicalStart = PhysicalStart; - Hob->ResourceLength = NumberOfBytes; -} - -/** - - -**/ -VOID -CreateHobList ( - IN VOID *MemoryBegin, - IN UINTN MemoryLength, - IN VOID *HobBase, - IN VOID *StackBase - ) -{ - EFI_HOB_HANDOFF_INFO_TABLE *Hob; - EFI_RESOURCE_ATTRIBUTE_TYPE Attributes; - - Hob = HobConstructor (MemoryBegin,MemoryLength,HobBase,StackBase); - SetHobList (Hob); - - BuildCpuHob (PcdGet8 (PcdPrePiCpuMemorySize), PcdGet8 (PcdPrePiCpuIoSize)); - - Attributes =( - EFI_RESOURCE_ATTRIBUTE_PRESENT | - EFI_RESOURCE_ATTRIBUTE_INITIALIZED | - EFI_RESOURCE_ATTRIBUTE_TESTED | - EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE | - EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE | - EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE | - EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE - ); - - BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY, Attributes, (UINTN)MemoryBegin, MemoryLength); - - BuildStackHob ((EFI_PHYSICAL_ADDRESS)(UINTN)StackBase, ((UINTN)MemoryBegin + MemoryLength) - (UINTN)StackBase); - - if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) { - // Optional feature that helps prevent EFI memory map fragmentation. - BuildMemoryTypeInformationHob (); - } -} - - -VOID -EFIAPI -BuildFvHobs ( - IN EFI_PHYSICAL_ADDRESS PhysicalStart, - IN UINT64 NumberOfBytes, - IN EFI_RESOURCE_ATTRIBUTE_TYPE *ResourceAttribute - ) -{ - - EFI_RESOURCE_ATTRIBUTE_TYPE Resource; - - BuildFvHob (PhysicalStart, NumberOfBytes); - - if (ResourceAttribute == NULL) { - Resource = (EFI_RESOURCE_ATTRIBUTE_PRESENT | - EFI_RESOURCE_ATTRIBUTE_INITIALIZED | - EFI_RESOURCE_ATTRIBUTE_TESTED | - EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE); - } else { - Resource = *ResourceAttribute; - } - - BuildResourceDescriptorHob (EFI_RESOURCE_FIRMWARE_DEVICE, Resource, PhysicalStart, NumberOfBytes); -} - -/** - Returns the next instance of a HOB type from the starting HOB. - - This function searches the first instance of a HOB type from the starting HOB pointer. - If there does not exist such HOB type from the starting HOB pointer, it will return NULL. - In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer - unconditionally: it returns HobStart back if HobStart itself meets the requirement; - caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart. - If HobStart is NULL, then ASSERT(). - - @param Type The HOB type to return. - @param HobStart The starting HOB pointer to search from. - - @return The next instance of a HOB type from the starting HOB. - -**/ -VOID * -EFIAPI -GetNextHob ( - IN UINT16 Type, - IN CONST VOID *HobStart - ) -{ - EFI_PEI_HOB_POINTERS Hob; - - ASSERT (HobStart != NULL); - - Hob.Raw = (UINT8 *) HobStart; - // - // Parse the HOB list until end of list or matching type is found. - // - while (!END_OF_HOB_LIST (Hob)) { - if (Hob.Header->HobType == Type) { - return Hob.Raw; - } - Hob.Raw = GET_NEXT_HOB (Hob); - } - return NULL; -} - - - -/** - Returns the first instance of a HOB type among the whole HOB list. - - This function searches the first instance of a HOB type among the whole HOB list. - If there does not exist such HOB type in the HOB list, it will return NULL. - - @param Type The HOB type to return. - - @return The next instance of a HOB type from the starting HOB. - -**/ -VOID * -EFIAPI -GetFirstHob ( - IN UINT16 Type - ) -{ - VOID *HobList; - - HobList = GetHobList (); - return GetNextHob (Type, HobList); -} - - -/** - This function searches the first instance of a HOB from the starting HOB pointer. - Such HOB should satisfy two conditions: - its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid. - If there does not exist such HOB from the starting HOB pointer, it will return NULL. - Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE () - to extract the data section and its size info respectively. - In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer - unconditionally: it returns HobStart back if HobStart itself meets the requirement; - caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart. - If Guid is NULL, then ASSERT(). - If HobStart is NULL, then ASSERT(). - - @param Guid The GUID to match with in the HOB list. - @param HobStart A pointer to a Guid. - - @return The next instance of the matched GUID HOB from the starting HOB. - -**/ -VOID * -EFIAPI -GetNextGuidHob ( - IN CONST EFI_GUID *Guid, - IN CONST VOID *HobStart - ){ - EFI_PEI_HOB_POINTERS GuidHob; - - GuidHob.Raw = (UINT8 *) HobStart; - while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) { - if (CompareGuid (Guid, &GuidHob.Guid->Name)) { - break; - } - GuidHob.Raw = GET_NEXT_HOB (GuidHob); - } - return GuidHob.Raw; -} - - -/** - This function searches the first instance of a HOB among the whole HOB list. - Such HOB should satisfy two conditions: - its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid. - If there does not exist such HOB from the starting HOB pointer, it will return NULL. - Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE () - to extract the data section and its size info respectively. - If Guid is NULL, then ASSERT(). - - @param Guid The GUID to match with in the HOB list. - - @return The first instance of the matched GUID HOB among the whole HOB list. - -**/ -VOID * -EFIAPI -GetFirstGuidHob ( - IN CONST EFI_GUID *Guid - ) -{ - VOID *HobList; - - HobList = GetHobList (); - return GetNextGuidHob (Guid, HobList); -} - - -/** - Get the Boot Mode from the HOB list. - - This function returns the system boot mode information from the - PHIT HOB in HOB list. - - @param VOID - - @return The Boot Mode. - -**/ -EFI_BOOT_MODE -EFIAPI -GetBootMode ( - VOID - ) -{ - EFI_PEI_HOB_POINTERS Hob; - - Hob.Raw = GetHobList (); - return Hob.HandoffInformationTable->BootMode; -} - - -/** - Get the Boot Mode from the HOB list. - - This function returns the system boot mode information from the - PHIT HOB in HOB list. - - @param VOID - - @return The Boot Mode. - -**/ -EFI_STATUS -EFIAPI -SetBootMode ( - IN EFI_BOOT_MODE BootMode - ) -{ - EFI_PEI_HOB_POINTERS Hob; - - Hob.Raw = GetHobList (); - Hob.HandoffInformationTable->BootMode = BootMode; - return BootMode; -} - -/** - Builds a HOB for a loaded PE32 module. - - This function builds a HOB for a loaded PE32 module. - It can only be invoked during PEI phase; - for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase. - If ModuleName is NULL, then ASSERT(). - If there is no additional space for HOB creation, then ASSERT(). - - @param ModuleName The GUID File Name of the module. - @param MemoryAllocationModule The 64 bit physical address of the module. - @param ModuleLength The length of the module in bytes. - @param EntryPoint The 64 bit physical address of the module entry point. - -**/ -VOID -EFIAPI -BuildModuleHob ( - IN CONST EFI_GUID *ModuleName, - IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule, - IN UINT64 ModuleLength, - IN EFI_PHYSICAL_ADDRESS EntryPoint - ) -{ - EFI_HOB_MEMORY_ALLOCATION_MODULE *Hob; - - ASSERT (((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) && - ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0)); - - Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE)); - - CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid); - Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule; - Hob->MemoryAllocationHeader.MemoryLength = ModuleLength; - Hob->MemoryAllocationHeader.MemoryType = EfiBootServicesCode; - - // - // Zero the reserved space to match HOB spec - // - ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof (Hob->MemoryAllocationHeader.Reserved)); - - CopyGuid (&Hob->ModuleName, ModuleName); - Hob->EntryPoint = EntryPoint; -} - -/** - Builds a GUID HOB with a certain data length. - - This function builds a customized HOB tagged with a GUID for identification - and returns the start address of GUID HOB data so that caller can fill the customized data. - The HOB Header and Name field is already stripped. - It can only be invoked during PEI phase; - for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase. - If Guid is NULL, then ASSERT(). - If there is no additional space for HOB creation, then ASSERT(). - If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT(). - - @param Guid The GUID to tag the customized HOB. - @param DataLength The size of the data payload for the GUID HOB. - - @return The start address of GUID HOB data. - -**/ -VOID * -EFIAPI -BuildGuidHob ( - IN CONST EFI_GUID *Guid, - IN UINTN DataLength - ) -{ - EFI_HOB_GUID_TYPE *Hob; - - // - // Make sure that data length is not too long. - // - ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE))); - - Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength)); - CopyGuid (&Hob->Name, Guid); - return Hob + 1; -} - - -/** - Copies a data buffer to a newly-built HOB. - - This function builds a customized HOB tagged with a GUID for identification, - copies the input data to the HOB data field and returns the start address of the GUID HOB data. - The HOB Header and Name field is already stripped. - It can only be invoked during PEI phase; - for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase. - If Guid is NULL, then ASSERT(). - If Data is NULL and DataLength > 0, then ASSERT(). - If there is no additional space for HOB creation, then ASSERT(). - If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT(). - - @param Guid The GUID to tag the customized HOB. - @param Data The data to be copied into the data field of the GUID HOB. - @param DataLength The size of the data payload for the GUID HOB. - - @return The start address of GUID HOB data. - -**/ -VOID * -EFIAPI -BuildGuidDataHob ( - IN CONST EFI_GUID *Guid, - IN VOID *Data, - IN UINTN DataLength - ) -{ - VOID *HobData; - - ASSERT (Data != NULL || DataLength == 0); - - HobData = BuildGuidHob (Guid, DataLength); - - return CopyMem (HobData, Data, DataLength); -} - - -/** - Builds a Firmware Volume HOB. - - This function builds a Firmware Volume HOB. - It can only be invoked during PEI phase; - for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase. - If there is no additional space for HOB creation, then ASSERT(). - - @param BaseAddress The base address of the Firmware Volume. - @param Length The size of the Firmware Volume in bytes. - -**/ -VOID -EFIAPI -BuildFvHob ( - IN EFI_PHYSICAL_ADDRESS BaseAddress, - IN UINT64 Length - ) -{ - EFI_HOB_FIRMWARE_VOLUME *Hob; - - Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME)); - - Hob->BaseAddress = BaseAddress; - Hob->Length = Length; -} - - -/** - Builds a EFI_HOB_TYPE_FV2 HOB. - - This function builds a EFI_HOB_TYPE_FV2 HOB. - It can only be invoked during PEI phase; - for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase. - If there is no additional space for HOB creation, then ASSERT(). - - @param BaseAddress The base address of the Firmware Volume. - @param Length The size of the Firmware Volume in bytes. - @param FvName The name of the Firmware Volume. - @param FileName The name of the file. - -**/ -VOID -EFIAPI -BuildFv2Hob ( - IN EFI_PHYSICAL_ADDRESS BaseAddress, - IN UINT64 Length, - IN CONST EFI_GUID *FvName, - IN CONST EFI_GUID *FileName - ) -{ - EFI_HOB_FIRMWARE_VOLUME2 *Hob; - - Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof (EFI_HOB_FIRMWARE_VOLUME2)); - - Hob->BaseAddress = BaseAddress; - Hob->Length = Length; - CopyGuid (&Hob->FvName, FvName); - CopyGuid (&Hob->FileName, FileName); -} - - - -/** - Builds a Capsule Volume HOB. - - This function builds a Capsule Volume HOB. - It can only be invoked during PEI phase; - for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase. - If there is no additional space for HOB creation, then ASSERT(). - - @param BaseAddress The base address of the Capsule Volume. - @param Length The size of the Capsule Volume in bytes. - -**/ -VOID -EFIAPI -BuildCvHob ( - IN EFI_PHYSICAL_ADDRESS BaseAddress, - IN UINT64 Length - ) -{ - ASSERT (FALSE); -} - - -/** - Builds a HOB for the CPU. - - This function builds a HOB for the CPU. - It can only be invoked during PEI phase; - for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase. - If there is no additional space for HOB creation, then ASSERT(). - - @param SizeOfMemorySpace The maximum physical memory addressability of the processor. - @param SizeOfIoSpace The maximum physical I/O addressability of the processor. - -**/ -VOID -EFIAPI -BuildCpuHob ( - IN UINT8 SizeOfMemorySpace, - IN UINT8 SizeOfIoSpace - ) -{ - EFI_HOB_CPU *Hob; - - Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU)); - - Hob->SizeOfMemorySpace = SizeOfMemorySpace; - Hob->SizeOfIoSpace = SizeOfIoSpace; - - // - // Zero the reserved space to match HOB spec - // - ZeroMem (Hob->Reserved, sizeof (Hob->Reserved)); -} - - -/** - Builds a HOB for the Stack. - - This function builds a HOB for the stack. - It can only be invoked during PEI phase; - for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase. - If there is no additional space for HOB creation, then ASSERT(). - - @param BaseAddress The 64 bit physical address of the Stack. - @param Length The length of the stack in bytes. - -**/ -VOID -EFIAPI -BuildStackHob ( - IN EFI_PHYSICAL_ADDRESS BaseAddress, - IN UINT64 Length - ) -{ - EFI_HOB_MEMORY_ALLOCATION_STACK *Hob; - - ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) && - ((Length & (EFI_PAGE_SIZE - 1)) == 0)); - - Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK)); - - CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocStackGuid); - Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress; - Hob->AllocDescriptor.MemoryLength = Length; - Hob->AllocDescriptor.MemoryType = EfiBootServicesData; - - // - // Zero the reserved space to match HOB spec - // - ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved)); -} - - -/** - Update the Stack Hob if the stack has been moved - - @param BaseAddress The 64 bit physical address of the Stack. - @param Length The length of the stack in bytes. - -**/ -VOID -UpdateStackHob ( - IN EFI_PHYSICAL_ADDRESS BaseAddress, - IN UINT64 Length - ) -{ - EFI_PEI_HOB_POINTERS Hob; - - Hob.Raw = GetHobList (); - while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw)) != NULL) { - if (CompareGuid (&gEfiHobMemoryAllocStackGuid, &(Hob.MemoryAllocationStack->AllocDescriptor.Name))) { - // - // Build a new memory allocation HOB with old stack info with EfiConventionalMemory type - // to be reclaimed by DXE core. - // - BuildMemoryAllocationHob ( - Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress, - Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength, - EfiConventionalMemory - ); - // - // Update the BSP Stack Hob to reflect the new stack info. - // - Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress = BaseAddress; - Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength = Length; - break; - } - Hob.Raw = GET_NEXT_HOB (Hob); - } -} - - - -/** - Builds a HOB for the memory allocation. - - This function builds a HOB for the memory allocation. - It can only be invoked during PEI phase; - for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase. - If there is no additional space for HOB creation, then ASSERT(). - - @param BaseAddress The 64 bit physical address of the memory. - @param Length The length of the memory allocation in bytes. - @param MemoryType Type of memory allocated by this HOB. - -**/ -VOID -EFIAPI -BuildMemoryAllocationHob ( - IN EFI_PHYSICAL_ADDRESS BaseAddress, - IN UINT64 Length, - IN EFI_MEMORY_TYPE MemoryType - ) -{ - EFI_HOB_MEMORY_ALLOCATION *Hob; - - ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) && - ((Length & (EFI_PAGE_SIZE - 1)) == 0)); - - Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION)); - - ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID)); - Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress; - Hob->AllocDescriptor.MemoryLength = Length; - Hob->AllocDescriptor.MemoryType = MemoryType; - // - // Zero the reserved space to match HOB spec - // - ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved)); -} - - - -VOID -EFIAPI -BuildExtractSectionHob ( - IN EFI_GUID *Guid, - IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER SectionGetInfo, - IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER SectionExtraction - ) -{ - EXTRACT_SECTION_DATA Data; - - Data.SectionGetInfo = SectionGetInfo; - Data.SectionExtraction = SectionExtraction; - BuildGuidDataHob (Guid, &Data, sizeof (Data)); -} - -PE_COFF_LOADER_PROTOCOL gPeCoffProtocol = { - PeCoffLoaderGetImageInfo, - PeCoffLoaderLoadImage, - PeCoffLoaderRelocateImage, - PeCoffLoaderImageReadFromMemory, - PeCoffLoaderRelocateImageForRuntime, - PeCoffLoaderUnloadImage -}; - - - -VOID -EFIAPI -BuildPeCoffLoaderHob ( - VOID - ) -{ - VOID *Ptr; - - Ptr = &gPeCoffProtocol; - BuildGuidDataHob (&gPeCoffLoaderProtocolGuid, &Ptr, sizeof (VOID *)); -} - -// May want to put this into a library so you only need the PCD setings if you are using the feature? -VOID -BuildMemoryTypeInformationHob ( - VOID - ) -{ - EFI_MEMORY_TYPE_INFORMATION Info[10]; - - Info[0].Type = EfiACPIReclaimMemory; - Info[0].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIReclaimMemory); - Info[1].Type = EfiACPIMemoryNVS; - Info[1].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIMemoryNVS); - Info[2].Type = EfiReservedMemoryType; - Info[2].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiReservedMemoryType); - Info[3].Type = EfiRuntimeServicesData; - Info[3].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesData); - Info[4].Type = EfiRuntimeServicesCode; - Info[4].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesCode); - Info[5].Type = EfiBootServicesCode; - Info[5].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesCode); - Info[6].Type = EfiBootServicesData; - Info[6].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesData); - Info[7].Type = EfiLoaderCode; - Info[7].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderCode); - Info[8].Type = EfiLoaderData; - Info[8].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderData); - - // Terminator for the list - Info[9].Type = EfiMaxMemoryType; - Info[9].NumberOfPages = 0; - - - BuildGuidDataHob (&gEfiMemoryTypeInformationGuid, &Info, sizeof (Info)); -} - +/** @file
+
+ Copyright (c) 2010, Apple Inc. All rights reserved.<BR>
+
+ 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 <PiPei.h>
+
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/PeCoffLib.h>
+#include <Library/HobLib.h>
+#include <Library/PcdLib.h>
+#include <Library/PrePiHobListPointerLib.h>
+
+#include <Protocol/PeCoffLoader.h>
+#include <Guid/ExtractSection.h>
+#include <Guid/MemoryTypeInformation.h>
+#include <Guid/MemoryAllocationHob.h>
+
+VOID
+BuildMemoryTypeInformationHob (
+ VOID
+ );
+
+/**
+ Returns the pointer to the HOB list.
+
+ This function returns the pointer to first HOB in the list.
+
+ @return The pointer to the HOB list.
+
+**/
+VOID *
+EFIAPI
+GetHobList (
+ VOID
+ )
+{
+ return PrePeiGetHobList ();
+}
+
+
+
+/**
+ Updates the pointer to the HOB list.
+
+ @param HobList Hob list pointer to store
+
+**/
+EFI_STATUS
+EFIAPI
+SetHobList (
+ IN VOID *HobList
+ )
+{
+ return PrePeiSetHobList (HobList);
+}
+
+/**
+
+
+**/
+EFI_HOB_HANDOFF_INFO_TABLE*
+HobConstructor (
+ IN VOID *EfiMemoryBegin,
+ IN UINTN EfiMemoryLength,
+ IN VOID *EfiFreeMemoryBottom,
+ IN VOID *EfiFreeMemoryTop
+ )
+{
+ EFI_HOB_HANDOFF_INFO_TABLE *Hob;
+ EFI_HOB_GENERIC_HEADER *HobEnd;
+
+ Hob = EfiFreeMemoryBottom;
+ HobEnd = (EFI_HOB_GENERIC_HEADER *)(Hob+1);
+
+ Hob->Header.HobType = EFI_HOB_TYPE_HANDOFF;
+ Hob->Header.HobLength = sizeof(EFI_HOB_HANDOFF_INFO_TABLE);
+ Hob->Header.Reserved = 0;
+
+ HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;
+ HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER);
+ HobEnd->Reserved = 0;
+
+ Hob->Version = EFI_HOB_HANDOFF_TABLE_VERSION;
+ Hob->BootMode = BOOT_WITH_FULL_CONFIGURATION;
+
+ Hob->EfiMemoryTop = (UINTN)EfiMemoryBegin + EfiMemoryLength;
+ Hob->EfiMemoryBottom = (UINTN)EfiMemoryBegin;
+ Hob->EfiFreeMemoryTop = (UINTN)EfiFreeMemoryTop;
+ Hob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS)(UINTN)(HobEnd+1);
+ Hob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;
+
+ return Hob;
+}
+
+VOID *
+CreateHob (
+ IN UINT16 HobType,
+ IN UINT16 HobLength
+ )
+{
+ EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;
+ EFI_HOB_GENERIC_HEADER *HobEnd;
+ EFI_PHYSICAL_ADDRESS FreeMemory;
+ VOID *Hob;
+
+ HandOffHob = GetHobList ();
+
+ HobLength = (UINT16)((HobLength + 0x7) & (~0x7));
+
+ FreeMemory = HandOffHob->EfiFreeMemoryTop - HandOffHob->EfiFreeMemoryBottom;
+
+ if (FreeMemory < HobLength) {
+ return NULL;
+ }
+
+ Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList;
+ ((EFI_HOB_GENERIC_HEADER*) Hob)->HobType = HobType;
+ ((EFI_HOB_GENERIC_HEADER*) Hob)->HobLength = HobLength;
+ ((EFI_HOB_GENERIC_HEADER*) Hob)->Reserved = 0;
+
+ HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN)Hob + HobLength);
+ HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
+
+ HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;
+ HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER);
+ HobEnd->Reserved = 0;
+ HobEnd++;
+ HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
+
+ return Hob;
+}
+
+/**
+ Builds a HOB that describes a chunk of system memory.
+
+ This function builds a HOB that describes a chunk of system memory.
+ If there is no additional space for HOB creation, then ASSERT().
+
+ @param ResourceType The type of resource described by this HOB.
+ @param ResourceAttribute The resource attributes of the memory described by this HOB.
+ @param PhysicalStart The 64 bit physical address of memory described by this HOB.
+ @param NumberOfBytes The length of the memory described by this HOB in bytes.
+
+**/
+VOID
+EFIAPI
+BuildResourceDescriptorHob (
+ IN EFI_RESOURCE_TYPE ResourceType,
+ IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,
+ IN EFI_PHYSICAL_ADDRESS PhysicalStart,
+ IN UINT64 NumberOfBytes
+ )
+{
+ EFI_HOB_RESOURCE_DESCRIPTOR *Hob;
+
+ Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR));
+ ASSERT(Hob != NULL);
+
+ Hob->ResourceType = ResourceType;
+ Hob->ResourceAttribute = ResourceAttribute;
+ Hob->PhysicalStart = PhysicalStart;
+ Hob->ResourceLength = NumberOfBytes;
+}
+
+/**
+
+
+**/
+VOID
+CreateHobList (
+ IN VOID *MemoryBegin,
+ IN UINTN MemoryLength,
+ IN VOID *HobBase,
+ IN VOID *StackBase
+ )
+{
+ EFI_HOB_HANDOFF_INFO_TABLE *Hob;
+ EFI_RESOURCE_ATTRIBUTE_TYPE Attributes;
+
+ Hob = HobConstructor (MemoryBegin,MemoryLength,HobBase,StackBase);
+ SetHobList (Hob);
+
+ BuildCpuHob (PcdGet8 (PcdPrePiCpuMemorySize), PcdGet8 (PcdPrePiCpuIoSize));
+
+ Attributes =(
+ EFI_RESOURCE_ATTRIBUTE_PRESENT |
+ EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+ EFI_RESOURCE_ATTRIBUTE_TESTED |
+ EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
+ EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+ EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+ EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
+ );
+
+ BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY, Attributes, (UINTN)MemoryBegin, MemoryLength);
+
+ BuildStackHob ((EFI_PHYSICAL_ADDRESS)(UINTN)StackBase, ((UINTN)MemoryBegin + MemoryLength) - (UINTN)StackBase);
+
+ if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {
+ // Optional feature that helps prevent EFI memory map fragmentation.
+ BuildMemoryTypeInformationHob ();
+ }
+}
+
+
+VOID
+EFIAPI
+BuildFvHobs (
+ IN EFI_PHYSICAL_ADDRESS PhysicalStart,
+ IN UINT64 NumberOfBytes,
+ IN EFI_RESOURCE_ATTRIBUTE_TYPE *ResourceAttribute
+ )
+{
+
+ EFI_RESOURCE_ATTRIBUTE_TYPE Resource;
+
+ BuildFvHob (PhysicalStart, NumberOfBytes);
+
+ if (ResourceAttribute == NULL) {
+ Resource = (EFI_RESOURCE_ATTRIBUTE_PRESENT |
+ EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+ EFI_RESOURCE_ATTRIBUTE_TESTED |
+ EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE);
+ } else {
+ Resource = *ResourceAttribute;
+ }
+
+ BuildResourceDescriptorHob (EFI_RESOURCE_FIRMWARE_DEVICE, Resource, PhysicalStart, NumberOfBytes);
+}
+
+/**
+ Returns the next instance of a HOB type from the starting HOB.
+
+ This function searches the first instance of a HOB type from the starting HOB pointer.
+ If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
+ In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
+ unconditionally: it returns HobStart back if HobStart itself meets the requirement;
+ caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
+ If HobStart is NULL, then ASSERT().
+
+ @param Type The HOB type to return.
+ @param HobStart The starting HOB pointer to search from.
+
+ @return The next instance of a HOB type from the starting HOB.
+
+**/
+VOID *
+EFIAPI
+GetNextHob (
+ IN UINT16 Type,
+ IN CONST VOID *HobStart
+ )
+{
+ EFI_PEI_HOB_POINTERS Hob;
+
+ ASSERT (HobStart != NULL);
+
+ Hob.Raw = (UINT8 *) HobStart;
+ //
+ // Parse the HOB list until end of list or matching type is found.
+ //
+ while (!END_OF_HOB_LIST (Hob)) {
+ if (Hob.Header->HobType == Type) {
+ return Hob.Raw;
+ }
+ Hob.Raw = GET_NEXT_HOB (Hob);
+ }
+ return NULL;
+}
+
+
+
+/**
+ Returns the first instance of a HOB type among the whole HOB list.
+
+ This function searches the first instance of a HOB type among the whole HOB list.
+ If there does not exist such HOB type in the HOB list, it will return NULL.
+
+ @param Type The HOB type to return.
+
+ @return The next instance of a HOB type from the starting HOB.
+
+**/
+VOID *
+EFIAPI
+GetFirstHob (
+ IN UINT16 Type
+ )
+{
+ VOID *HobList;
+
+ HobList = GetHobList ();
+ return GetNextHob (Type, HobList);
+}
+
+
+/**
+ This function searches the first instance of a HOB from the starting HOB pointer.
+ Such HOB should satisfy two conditions:
+ its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
+ If there does not exist such HOB from the starting HOB pointer, it will return NULL.
+ Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
+ to extract the data section and its size info respectively.
+ In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
+ unconditionally: it returns HobStart back if HobStart itself meets the requirement;
+ caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
+ If Guid is NULL, then ASSERT().
+ If HobStart is NULL, then ASSERT().
+
+ @param Guid The GUID to match with in the HOB list.
+ @param HobStart A pointer to a Guid.
+
+ @return The next instance of the matched GUID HOB from the starting HOB.
+
+**/
+VOID *
+EFIAPI
+GetNextGuidHob (
+ IN CONST EFI_GUID *Guid,
+ IN CONST VOID *HobStart
+ ){
+ EFI_PEI_HOB_POINTERS GuidHob;
+
+ GuidHob.Raw = (UINT8 *) HobStart;
+ while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {
+ if (CompareGuid (Guid, &GuidHob.Guid->Name)) {
+ break;
+ }
+ GuidHob.Raw = GET_NEXT_HOB (GuidHob);
+ }
+ return GuidHob.Raw;
+}
+
+
+/**
+ This function searches the first instance of a HOB among the whole HOB list.
+ Such HOB should satisfy two conditions:
+ its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
+ If there does not exist such HOB from the starting HOB pointer, it will return NULL.
+ Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
+ to extract the data section and its size info respectively.
+ If Guid is NULL, then ASSERT().
+
+ @param Guid The GUID to match with in the HOB list.
+
+ @return The first instance of the matched GUID HOB among the whole HOB list.
+
+**/
+VOID *
+EFIAPI
+GetFirstGuidHob (
+ IN CONST EFI_GUID *Guid
+ )
+{
+ VOID *HobList;
+
+ HobList = GetHobList ();
+ return GetNextGuidHob (Guid, HobList);
+}
+
+
+/**
+ Get the Boot Mode from the HOB list.
+
+ This function returns the system boot mode information from the
+ PHIT HOB in HOB list.
+
+ @param VOID
+
+ @return The Boot Mode.
+
+**/
+EFI_BOOT_MODE
+EFIAPI
+GetBootMode (
+ VOID
+ )
+{
+ EFI_PEI_HOB_POINTERS Hob;
+
+ Hob.Raw = GetHobList ();
+ return Hob.HandoffInformationTable->BootMode;
+}
+
+
+/**
+ Get the Boot Mode from the HOB list.
+
+ This function returns the system boot mode information from the
+ PHIT HOB in HOB list.
+
+ @param VOID
+
+ @return The Boot Mode.
+
+**/
+EFI_STATUS
+EFIAPI
+SetBootMode (
+ IN EFI_BOOT_MODE BootMode
+ )
+{
+ EFI_PEI_HOB_POINTERS Hob;
+
+ Hob.Raw = GetHobList ();
+ Hob.HandoffInformationTable->BootMode = BootMode;
+ return BootMode;
+}
+
+/**
+ Builds a HOB for a loaded PE32 module.
+
+ This function builds a HOB for a loaded PE32 module.
+ It can only be invoked during PEI phase;
+ for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+ If ModuleName is NULL, then ASSERT().
+ If there is no additional space for HOB creation, then ASSERT().
+
+ @param ModuleName The GUID File Name of the module.
+ @param MemoryAllocationModule The 64 bit physical address of the module.
+ @param ModuleLength The length of the module in bytes.
+ @param EntryPoint The 64 bit physical address of the module entry point.
+
+**/
+VOID
+EFIAPI
+BuildModuleHob (
+ IN CONST EFI_GUID *ModuleName,
+ IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,
+ IN UINT64 ModuleLength,
+ IN EFI_PHYSICAL_ADDRESS EntryPoint
+ )
+{
+ EFI_HOB_MEMORY_ALLOCATION_MODULE *Hob;
+
+ ASSERT (((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) &&
+ ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0));
+
+ Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE));
+
+ CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid);
+ Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;
+ Hob->MemoryAllocationHeader.MemoryLength = ModuleLength;
+ Hob->MemoryAllocationHeader.MemoryType = EfiBootServicesCode;
+
+ //
+ // Zero the reserved space to match HOB spec
+ //
+ ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof (Hob->MemoryAllocationHeader.Reserved));
+
+ CopyGuid (&Hob->ModuleName, ModuleName);
+ Hob->EntryPoint = EntryPoint;
+}
+
+/**
+ Builds a GUID HOB with a certain data length.
+
+ This function builds a customized HOB tagged with a GUID for identification
+ and returns the start address of GUID HOB data so that caller can fill the customized data.
+ The HOB Header and Name field is already stripped.
+ It can only be invoked during PEI phase;
+ for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+ If Guid is NULL, then ASSERT().
+ If there is no additional space for HOB creation, then ASSERT().
+ If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
+
+ @param Guid The GUID to tag the customized HOB.
+ @param DataLength The size of the data payload for the GUID HOB.
+
+ @return The start address of GUID HOB data.
+
+**/
+VOID *
+EFIAPI
+BuildGuidHob (
+ IN CONST EFI_GUID *Guid,
+ IN UINTN DataLength
+ )
+{
+ EFI_HOB_GUID_TYPE *Hob;
+
+ //
+ // Make sure that data length is not too long.
+ //
+ ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));
+
+ Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength));
+ CopyGuid (&Hob->Name, Guid);
+ return Hob + 1;
+}
+
+
+/**
+ Copies a data buffer to a newly-built HOB.
+
+ This function builds a customized HOB tagged with a GUID for identification,
+ copies the input data to the HOB data field and returns the start address of the GUID HOB data.
+ The HOB Header and Name field is already stripped.
+ It can only be invoked during PEI phase;
+ for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+ If Guid is NULL, then ASSERT().
+ If Data is NULL and DataLength > 0, then ASSERT().
+ If there is no additional space for HOB creation, then ASSERT().
+ If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
+
+ @param Guid The GUID to tag the customized HOB.
+ @param Data The data to be copied into the data field of the GUID HOB.
+ @param DataLength The size of the data payload for the GUID HOB.
+
+ @return The start address of GUID HOB data.
+
+**/
+VOID *
+EFIAPI
+BuildGuidDataHob (
+ IN CONST EFI_GUID *Guid,
+ IN VOID *Data,
+ IN UINTN DataLength
+ )
+{
+ VOID *HobData;
+
+ ASSERT (Data != NULL || DataLength == 0);
+
+ HobData = BuildGuidHob (Guid, DataLength);
+
+ return CopyMem (HobData, Data, DataLength);
+}
+
+
+/**
+ Builds a Firmware Volume HOB.
+
+ This function builds a Firmware Volume HOB.
+ It can only be invoked during PEI phase;
+ for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+ If there is no additional space for HOB creation, then ASSERT().
+
+ @param BaseAddress The base address of the Firmware Volume.
+ @param Length The size of the Firmware Volume in bytes.
+
+**/
+VOID
+EFIAPI
+BuildFvHob (
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,
+ IN UINT64 Length
+ )
+{
+ EFI_HOB_FIRMWARE_VOLUME *Hob;
+
+ Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));
+
+ Hob->BaseAddress = BaseAddress;
+ Hob->Length = Length;
+}
+
+
+/**
+ Builds a EFI_HOB_TYPE_FV2 HOB.
+
+ This function builds a EFI_HOB_TYPE_FV2 HOB.
+ It can only be invoked during PEI phase;
+ for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+ If there is no additional space for HOB creation, then ASSERT().
+
+ @param BaseAddress The base address of the Firmware Volume.
+ @param Length The size of the Firmware Volume in bytes.
+ @param FvName The name of the Firmware Volume.
+ @param FileName The name of the file.
+
+**/
+VOID
+EFIAPI
+BuildFv2Hob (
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,
+ IN UINT64 Length,
+ IN CONST EFI_GUID *FvName,
+ IN CONST EFI_GUID *FileName
+ )
+{
+ EFI_HOB_FIRMWARE_VOLUME2 *Hob;
+
+ Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof (EFI_HOB_FIRMWARE_VOLUME2));
+
+ Hob->BaseAddress = BaseAddress;
+ Hob->Length = Length;
+ CopyGuid (&Hob->FvName, FvName);
+ CopyGuid (&Hob->FileName, FileName);
+}
+
+
+
+/**
+ Builds a Capsule Volume HOB.
+
+ This function builds a Capsule Volume HOB.
+ It can only be invoked during PEI phase;
+ for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+ If there is no additional space for HOB creation, then ASSERT().
+
+ @param BaseAddress The base address of the Capsule Volume.
+ @param Length The size of the Capsule Volume in bytes.
+
+**/
+VOID
+EFIAPI
+BuildCvHob (
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,
+ IN UINT64 Length
+ )
+{
+ ASSERT (FALSE);
+}
+
+
+/**
+ Builds a HOB for the CPU.
+
+ This function builds a HOB for the CPU.
+ It can only be invoked during PEI phase;
+ for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+ If there is no additional space for HOB creation, then ASSERT().
+
+ @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
+ @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
+
+**/
+VOID
+EFIAPI
+BuildCpuHob (
+ IN UINT8 SizeOfMemorySpace,
+ IN UINT8 SizeOfIoSpace
+ )
+{
+ EFI_HOB_CPU *Hob;
+
+ Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
+
+ Hob->SizeOfMemorySpace = SizeOfMemorySpace;
+ Hob->SizeOfIoSpace = SizeOfIoSpace;
+
+ //
+ // Zero the reserved space to match HOB spec
+ //
+ ZeroMem (Hob->Reserved, sizeof (Hob->Reserved));
+}
+
+
+/**
+ Builds a HOB for the Stack.
+
+ This function builds a HOB for the stack.
+ It can only be invoked during PEI phase;
+ for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+ If there is no additional space for HOB creation, then ASSERT().
+
+ @param BaseAddress The 64 bit physical address of the Stack.
+ @param Length The length of the stack in bytes.
+
+**/
+VOID
+EFIAPI
+BuildStackHob (
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,
+ IN UINT64 Length
+ )
+{
+ EFI_HOB_MEMORY_ALLOCATION_STACK *Hob;
+
+ ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&
+ ((Length & (EFI_PAGE_SIZE - 1)) == 0));
+
+ Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK));
+
+ CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocStackGuid);
+ Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
+ Hob->AllocDescriptor.MemoryLength = Length;
+ Hob->AllocDescriptor.MemoryType = EfiBootServicesData;
+
+ //
+ // Zero the reserved space to match HOB spec
+ //
+ ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
+}
+
+
+/**
+ Update the Stack Hob if the stack has been moved
+
+ @param BaseAddress The 64 bit physical address of the Stack.
+ @param Length The length of the stack in bytes.
+
+**/
+VOID
+UpdateStackHob (
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,
+ IN UINT64 Length
+ )
+{
+ EFI_PEI_HOB_POINTERS Hob;
+
+ Hob.Raw = GetHobList ();
+ while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw)) != NULL) {
+ if (CompareGuid (&gEfiHobMemoryAllocStackGuid, &(Hob.MemoryAllocationStack->AllocDescriptor.Name))) {
+ //
+ // Build a new memory allocation HOB with old stack info with EfiConventionalMemory type
+ // to be reclaimed by DXE core.
+ //
+ BuildMemoryAllocationHob (
+ Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress,
+ Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength,
+ EfiConventionalMemory
+ );
+ //
+ // Update the BSP Stack Hob to reflect the new stack info.
+ //
+ Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress = BaseAddress;
+ Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength = Length;
+ break;
+ }
+ Hob.Raw = GET_NEXT_HOB (Hob);
+ }
+}
+
+
+
+/**
+ Builds a HOB for the memory allocation.
+
+ This function builds a HOB for the memory allocation.
+ It can only be invoked during PEI phase;
+ for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+ If there is no additional space for HOB creation, then ASSERT().
+
+ @param BaseAddress The 64 bit physical address of the memory.
+ @param Length The length of the memory allocation in bytes.
+ @param MemoryType Type of memory allocated by this HOB.
+
+**/
+VOID
+EFIAPI
+BuildMemoryAllocationHob (
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,
+ IN UINT64 Length,
+ IN EFI_MEMORY_TYPE MemoryType
+ )
+{
+ EFI_HOB_MEMORY_ALLOCATION *Hob;
+
+ ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&
+ ((Length & (EFI_PAGE_SIZE - 1)) == 0));
+
+ Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));
+
+ ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
+ Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
+ Hob->AllocDescriptor.MemoryLength = Length;
+ Hob->AllocDescriptor.MemoryType = MemoryType;
+ //
+ // Zero the reserved space to match HOB spec
+ //
+ ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
+}
+
+
+
+VOID
+EFIAPI
+BuildExtractSectionHob (
+ IN EFI_GUID *Guid,
+ IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER SectionGetInfo,
+ IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER SectionExtraction
+ )
+{
+ EXTRACT_SECTION_DATA Data;
+
+ Data.SectionGetInfo = SectionGetInfo;
+ Data.SectionExtraction = SectionExtraction;
+ BuildGuidDataHob (Guid, &Data, sizeof (Data));
+}
+
+PE_COFF_LOADER_PROTOCOL gPeCoffProtocol = {
+ PeCoffLoaderGetImageInfo,
+ PeCoffLoaderLoadImage,
+ PeCoffLoaderRelocateImage,
+ PeCoffLoaderImageReadFromMemory,
+ PeCoffLoaderRelocateImageForRuntime,
+ PeCoffLoaderUnloadImage
+};
+
+
+
+VOID
+EFIAPI
+BuildPeCoffLoaderHob (
+ VOID
+ )
+{
+ VOID *Ptr;
+
+ Ptr = &gPeCoffProtocol;
+ BuildGuidDataHob (&gPeCoffLoaderProtocolGuid, &Ptr, sizeof (VOID *));
+}
+
+// May want to put this into a library so you only need the PCD setings if you are using the feature?
+VOID
+BuildMemoryTypeInformationHob (
+ VOID
+ )
+{
+ EFI_MEMORY_TYPE_INFORMATION Info[10];
+
+ Info[0].Type = EfiACPIReclaimMemory;
+ Info[0].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIReclaimMemory);
+ Info[1].Type = EfiACPIMemoryNVS;
+ Info[1].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIMemoryNVS);
+ Info[2].Type = EfiReservedMemoryType;
+ Info[2].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiReservedMemoryType);
+ Info[3].Type = EfiRuntimeServicesData;
+ Info[3].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesData);
+ Info[4].Type = EfiRuntimeServicesCode;
+ Info[4].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesCode);
+ Info[5].Type = EfiBootServicesCode;
+ Info[5].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesCode);
+ Info[6].Type = EfiBootServicesData;
+ Info[6].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesData);
+ Info[7].Type = EfiLoaderCode;
+ Info[7].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderCode);
+ Info[8].Type = EfiLoaderData;
+ Info[8].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderData);
+
+ // Terminator for the list
+ Info[9].Type = EfiMaxMemoryType;
+ Info[9].NumberOfPages = 0;
+
+
+ BuildGuidDataHob (&gEfiMemoryTypeInformationGuid, &Info, sizeof (Info));
+}
+
diff --git a/EmbeddedPkg/Library/PrePiHobListPointerLib/PrePiHobListPointer.c b/EmbeddedPkg/Library/PrePiHobListPointerLib/PrePiHobListPointer.c index 9f9e9c7d44..bf4f80f4e4 100644 --- a/EmbeddedPkg/Library/PrePiHobListPointerLib/PrePiHobListPointer.c +++ b/EmbeddedPkg/Library/PrePiHobListPointerLib/PrePiHobListPointer.c @@ -17,11 +17,11 @@ #include <Library/DebugLib.h>
#include <Library/PcdLib.h>
-// -// Have to use build system to set the original value in case we are running -// from FLASH and globals don't work. So if you do a GetHobList() and gHobList -// and gHobList is NULL the PCD default values are used. -// +//
+// Have to use build system to set the original value in case we are running
+// from FLASH and globals don't work. So if you do a GetHobList() and gHobList
+// and gHobList is NULL the PCD default values are used.
+//
VOID *gHobList = NULL;
diff --git a/EmbeddedPkg/Library/PrePiHobListPointerLib/PrePiHobListPointerLib.inf b/EmbeddedPkg/Library/PrePiHobListPointerLib/PrePiHobListPointerLib.inf index 1ba976780b..fea8f43687 100644 --- a/EmbeddedPkg/Library/PrePiHobListPointerLib/PrePiHobListPointerLib.inf +++ b/EmbeddedPkg/Library/PrePiHobListPointerLib/PrePiHobListPointerLib.inf @@ -36,4 +36,4 @@ [FixedPcd.common]
gEmbeddedTokenSpaceGuid.PcdPrePiHobBase
-
\ No newline at end of file +
\ No newline at end of file diff --git a/EmbeddedPkg/Library/PrePiLib/FwVol.c b/EmbeddedPkg/Library/PrePiLib/FwVol.c index 75d28a7257..9e4cdd002e 100644 --- a/EmbeddedPkg/Library/PrePiLib/FwVol.c +++ b/EmbeddedPkg/Library/PrePiLib/FwVol.c @@ -1,842 +1,842 @@ -/** @file - Implementation of the 6 PEI Ffs (FV) APIs in library form. - - This code only knows about a FV if it has a EFI_HOB_TYPE_FV entry in the HOB list - - Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR> - - 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 <PrePi.h> -#include <Library/ExtractGuidedSectionLib.h> - - -#define GET_OCCUPIED_SIZE(ActualSize, Alignment) \ - (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1)) - - -/** - Returns the highest bit set of the State field - - @param ErasePolarity Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY - in the Attributes field. - @param FfsHeader Pointer to FFS File Header - - - @retval the highest bit in the State field - -**/ -STATIC -EFI_FFS_FILE_STATE -GetFileState( - IN UINT8 ErasePolarity, - IN EFI_FFS_FILE_HEADER *FfsHeader - ) -{ - EFI_FFS_FILE_STATE FileState; - EFI_FFS_FILE_STATE HighestBit; - - FileState = FfsHeader->State; - - if (ErasePolarity != 0) { - FileState = (EFI_FFS_FILE_STATE)~FileState; - } - - HighestBit = 0x80; - while (HighestBit != 0 && (HighestBit & FileState) == 0) { - HighestBit >>= 1; - } - - return HighestBit; -} - - -/** - Calculates the checksum of the header of a file. - The header is a zero byte checksum, so zero means header is good - - @param FfsHeader Pointer to FFS File Header - - @retval Checksum of the header - -**/ -STATIC -UINT8 -CalculateHeaderChecksum ( - IN EFI_FFS_FILE_HEADER *FileHeader - ) -{ - UINT8 *Ptr; - UINTN Index; - UINT8 Sum; - - Sum = 0; - Ptr = (UINT8 *)FileHeader; - - for (Index = 0; Index < sizeof(EFI_FFS_FILE_HEADER) - 3; Index += 4) { - Sum = (UINT8)(Sum + Ptr[Index]); - Sum = (UINT8)(Sum + Ptr[Index+1]); - Sum = (UINT8)(Sum + Ptr[Index+2]); - Sum = (UINT8)(Sum + Ptr[Index+3]); - } - - for (; Index < sizeof(EFI_FFS_FILE_HEADER); Index++) { - Sum = (UINT8)(Sum + Ptr[Index]); - } - - // - // State field (since this indicates the different state of file). - // - Sum = (UINT8)(Sum - FileHeader->State); - // - // Checksum field of the file is not part of the header checksum. - // - Sum = (UINT8)(Sum - FileHeader->IntegrityCheck.Checksum.File); - - return Sum; -} - - -/** - Given a FileHandle return the VolumeHandle - - @param FileHandle File handle to look up - @param VolumeHandle Match for FileHandle - - @retval TRUE VolumeHandle is valid - -**/ -STATIC -BOOLEAN -EFIAPI -FileHandleToVolume ( - IN EFI_PEI_FILE_HANDLE FileHandle, - OUT EFI_PEI_FV_HANDLE *VolumeHandle - ) -{ - EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader; - EFI_PEI_HOB_POINTERS Hob; - - Hob.Raw = GetHobList (); - if (Hob.Raw == NULL) { - return FALSE; - } - - do { - Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw); - if (Hob.Raw != NULL) { - FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(Hob.FirmwareVolume->BaseAddress); - if (((UINT64) (UINTN) FileHandle > (UINT64) (UINTN) FwVolHeader ) && \ - ((UINT64) (UINTN) FileHandle <= ((UINT64) (UINTN) FwVolHeader + FwVolHeader->FvLength - 1))) { - *VolumeHandle = (EFI_PEI_FV_HANDLE)FwVolHeader; - return TRUE; - } - - Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, GET_NEXT_HOB (Hob)); - } - } while (Hob.Raw != NULL); - - return FALSE; -} - - - -/** - Given the input file pointer, search for the next matching file in the - FFS volume as defined by SearchType. The search starts from FileHeader inside - the Firmware Volume defined by FwVolHeader. - - @param FileHandle File handle to look up - @param VolumeHandle Match for FileHandle - - -**/ -EFI_STATUS -FindFileEx ( - IN CONST EFI_PEI_FV_HANDLE FvHandle, - IN CONST EFI_GUID *FileName, OPTIONAL - IN EFI_FV_FILETYPE SearchType, - IN OUT EFI_PEI_FILE_HANDLE *FileHandle - ) -{ - EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader; - EFI_FFS_FILE_HEADER **FileHeader; - EFI_FFS_FILE_HEADER *FfsFileHeader; - EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo; - UINT32 FileLength; - UINT32 FileOccupiedSize; - UINT32 FileOffset; - UINT64 FvLength; - UINT8 ErasePolarity; - UINT8 FileState; - - FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FvHandle; - FileHeader = (EFI_FFS_FILE_HEADER **)FileHandle; - - FvLength = FwVolHeader->FvLength; - if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) { - ErasePolarity = 1; - } else { - ErasePolarity = 0; - } - - // - // If FileHeader is not specified (NULL) or FileName is not NULL, - // start with the first file in the firmware volume. Otherwise, - // start from the FileHeader. - // - if ((*FileHeader == NULL) || (FileName != NULL)) { - FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader + FwVolHeader->HeaderLength); - if (FwVolHeader->ExtHeaderOffset != 0) { - FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)(((UINT8 *)FwVolHeader) + FwVolHeader->ExtHeaderOffset); - FfsFileHeader = (EFI_FFS_FILE_HEADER *)(((UINT8 *)FwVolExHeaderInfo) + FwVolExHeaderInfo->ExtHeaderSize); - } - } else { - // - // Length is 24 bits wide so mask upper 8 bits - // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned. - // - FileLength = *(UINT32 *)(*FileHeader)->Size & 0x00FFFFFF; - FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8); - FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize); - } - - FileOffset = (UINT32) ((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader); - ASSERT (FileOffset <= 0xFFFFFFFF); - - while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) { - // - // Get FileState which is the highest bit of the State - // - FileState = GetFileState (ErasePolarity, FfsFileHeader); - - switch (FileState) { - - case EFI_FILE_HEADER_INVALID: - FileOffset += sizeof(EFI_FFS_FILE_HEADER); - FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof(EFI_FFS_FILE_HEADER)); - break; - - case EFI_FILE_DATA_VALID: - case EFI_FILE_MARKED_FOR_UPDATE: - if (CalculateHeaderChecksum (FfsFileHeader) != 0) { - ASSERT (FALSE); - *FileHeader = NULL; - return EFI_NOT_FOUND; - } - - FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF; - FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8); - - if (FileName != NULL) { - if (CompareGuid (&FfsFileHeader->Name, (EFI_GUID*)FileName)) { - *FileHeader = FfsFileHeader; - return EFI_SUCCESS; - } - } else if (((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) && - (FfsFileHeader->Type != EFI_FV_FILETYPE_FFS_PAD)) { - *FileHeader = FfsFileHeader; - return EFI_SUCCESS; - } - - FileOffset += FileOccupiedSize; - FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize); - break; - - case EFI_FILE_DELETED: - FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF; - FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8); - FileOffset += FileOccupiedSize; - FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize); - break; - - default: - *FileHeader = NULL; - return EFI_NOT_FOUND; - } - } - - - *FileHeader = NULL; - return EFI_NOT_FOUND; -} - - -/** - Go through the file to search SectionType section, - when meeting an encapsuled section. - - @param SectionType - Filter to find only section of this type. - @param Section - From where to search. - @param SectionSize - The file size to search. - @param OutputBuffer - Pointer to the section to search. - - @retval EFI_SUCCESS -**/ -EFI_STATUS -FfsProcessSection ( - IN EFI_SECTION_TYPE SectionType, - IN EFI_COMMON_SECTION_HEADER *Section, - IN UINTN SectionSize, - OUT VOID **OutputBuffer - ) -{ - EFI_STATUS Status; - UINT32 SectionLength; - UINT32 ParsedLength; - EFI_COMPRESSION_SECTION *CompressionSection; - UINTN DstBufferSize; - VOID *ScratchBuffer; - UINT32 ScratchBufferSize; - VOID *DstBuffer; - UINT16 SectionAttribute; - UINT32 AuthenticationStatus; - - - *OutputBuffer = NULL; - ParsedLength = 0; - Status = EFI_NOT_FOUND; - while (ParsedLength < SectionSize) { - if (Section->Type == SectionType) { - *OutputBuffer = (VOID *)(Section + 1); - - return EFI_SUCCESS; - } else if ((Section->Type == EFI_SECTION_COMPRESSION) || (Section->Type == EFI_SECTION_GUID_DEFINED)) { - - if (Section->Type == EFI_SECTION_COMPRESSION) { - CompressionSection = (EFI_COMPRESSION_SECTION *) Section; - SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF; - - if (CompressionSection->CompressionType != EFI_STANDARD_COMPRESSION) { - return EFI_UNSUPPORTED; - } - - Status = UefiDecompressGetInfo ( - (UINT8 *) ((EFI_COMPRESSION_SECTION *) Section + 1), - (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION), - (UINT32 *) &DstBufferSize, - &ScratchBufferSize - ); - } else if (Section->Type == EFI_SECTION_GUID_DEFINED) { - Status = ExtractGuidedSectionGetInfo ( - Section, - (UINT32 *) &DstBufferSize, - &ScratchBufferSize, - &SectionAttribute - ); - } - - if (EFI_ERROR (Status)) { - // - // GetInfo failed - // - DEBUG ((EFI_D_ERROR, "Decompress GetInfo Failed - %r\n", Status)); - return EFI_NOT_FOUND; - } - // - // Allocate scratch buffer - // - ScratchBuffer = (VOID *)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize)); - if (ScratchBuffer == NULL) { - return EFI_OUT_OF_RESOURCES; - } - // - // Allocate destination buffer, extra one page for adjustment - // - DstBuffer = (VOID *)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize) + 1); - if (DstBuffer == NULL) { - return EFI_OUT_OF_RESOURCES; - } - // - // DstBuffer still is one section. Adjust DstBuffer offset, skip EFI section header - // to make section data at page alignment. - // - DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER); - // - // Call decompress function - // - if (Section->Type == EFI_SECTION_COMPRESSION) { - Status = UefiDecompress ( - (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1), - DstBuffer, - ScratchBuffer - ); - } else if (Section->Type == EFI_SECTION_GUID_DEFINED) { - Status = ExtractGuidedSectionDecode ( - Section, - &DstBuffer, - ScratchBuffer, - &AuthenticationStatus - ); - } - - if (EFI_ERROR (Status)) { - // - // Decompress failed - // - DEBUG ((EFI_D_ERROR, "Decompress Failed - %r\n", Status)); - return EFI_NOT_FOUND; - } else { - return FfsProcessSection ( - SectionType, - DstBuffer, - DstBufferSize, - OutputBuffer - ); - } - } - - // - // Size is 24 bits wide so mask upper 8 bits. - // SectionLength is adjusted it is 4 byte aligned. - // Go to the next section - // - SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF; - SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4); - ASSERT (SectionLength != 0); - ParsedLength += SectionLength; - Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength); - } - - return EFI_NOT_FOUND; -} - - - -/** - This service enables discovery sections of a given type within a valid FFS file. - - @param SearchType The value of the section type to find. - @param FfsFileHeader A pointer to the file header that contains the set of sections to - be searched. - @param SectionData A pointer to the discovered section, if successful. - - @retval EFI_SUCCESS The section was found. - @retval EFI_NOT_FOUND The section was not found. - -**/ -EFI_STATUS -EFIAPI -FfsFindSectionData ( - IN EFI_SECTION_TYPE SectionType, - IN EFI_PEI_FILE_HANDLE FileHandle, - OUT VOID **SectionData - ) -{ - EFI_FFS_FILE_HEADER *FfsFileHeader; - UINT32 FileSize; - EFI_COMMON_SECTION_HEADER *Section; - - FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle); - - // - // Size is 24 bits wide so mask upper 8 bits. - // Does not include FfsFileHeader header size - // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned. - // - Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1); - FileSize = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF; - FileSize -= sizeof (EFI_FFS_FILE_HEADER); - - return FfsProcessSection ( - SectionType, - Section, - FileSize, - SectionData - ); -} - - - - - - -/** - This service enables discovery of additional firmware files. - - @param SearchType A filter to find files only of this type. - @param FwVolHeader Pointer to the firmware volume header of the volume to search. - This parameter must point to a valid FFS volume. - @param FileHeader Pointer to the current file from which to begin searching. - - @retval EFI_SUCCESS The file was found. - @retval EFI_NOT_FOUND The file was not found. - @retval EFI_NOT_FOUND The header checksum was not zero. - -**/ -EFI_STATUS -EFIAPI -FfsFindNextFile ( - IN UINT8 SearchType, - IN EFI_PEI_FV_HANDLE VolumeHandle, - IN OUT EFI_PEI_FILE_HANDLE *FileHandle - ) -{ - return FindFileEx (VolumeHandle, NULL, SearchType, FileHandle); -} - - -/** - This service enables discovery of additional firmware volumes. - - @param Instance This instance of the firmware volume to find. The value 0 is the - Boot Firmware Volume (BFV). - @param FwVolHeader Pointer to the firmware volume header of the volume to return. - - @retval EFI_SUCCESS The volume was found. - @retval EFI_NOT_FOUND The volume was not found. - -**/ -EFI_STATUS -EFIAPI -FfsFindNextVolume ( - IN UINTN Instance, - IN OUT EFI_PEI_FV_HANDLE *VolumeHandle - ) -{ - EFI_PEI_HOB_POINTERS Hob; - - - Hob.Raw = GetHobList (); - if (Hob.Raw == NULL) { - return EFI_NOT_FOUND; - } - - do { - Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw); - if (Hob.Raw != NULL) { - if (Instance-- == 0) { - *VolumeHandle = (EFI_PEI_FV_HANDLE)(UINTN)(Hob.FirmwareVolume->BaseAddress); - return EFI_SUCCESS; - } - - Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, GET_NEXT_HOB (Hob)); - } - } while (Hob.Raw != NULL); - - return EFI_NOT_FOUND; - -} - - -/** - Find a file in the volume by name - - @param FileName A pointer to the name of the file to - find within the firmware volume. - - @param VolumeHandle The firmware volume to search FileHandle - Upon exit, points to the found file's - handle or NULL if it could not be found. - - @retval EFI_SUCCESS File was found. - - @retval EFI_NOT_FOUND File was not found. - - @retval EFI_INVALID_PARAMETER VolumeHandle or FileHandle or - FileName was NULL. - -**/ -EFI_STATUS -EFIAPI -FfsFindFileByName ( - IN CONST EFI_GUID *FileName, - IN EFI_PEI_FV_HANDLE VolumeHandle, - OUT EFI_PEI_FILE_HANDLE *FileHandle - ) -{ - EFI_STATUS Status; - if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) { - return EFI_INVALID_PARAMETER; - } - Status = FindFileEx (VolumeHandle, FileName, 0, FileHandle); - if (Status == EFI_NOT_FOUND) { - *FileHandle = NULL; - } - return Status; -} - - - - -/** - Get information about the file by name. - - @param FileHandle Handle of the file. - - @param FileInfo Upon exit, points to the file's - information. - - @retval EFI_SUCCESS File information returned. - - @retval EFI_INVALID_PARAMETER If FileHandle does not - represent a valid file. - - @retval EFI_INVALID_PARAMETER If FileInfo is NULL. - -**/ -EFI_STATUS -EFIAPI -FfsGetFileInfo ( - IN EFI_PEI_FILE_HANDLE FileHandle, - OUT EFI_FV_FILE_INFO *FileInfo - ) -{ - UINT8 FileState; - UINT8 ErasePolarity; - EFI_FFS_FILE_HEADER *FileHeader; - EFI_PEI_FV_HANDLE VolumeHandle; - - if ((FileHandle == NULL) || (FileInfo == NULL)) { - return EFI_INVALID_PARAMETER; - } - - VolumeHandle = 0; - // - // Retrieve the FirmwareVolume which the file resides in. - // - if (!FileHandleToVolume(FileHandle, &VolumeHandle)) { - return EFI_INVALID_PARAMETER; - } - - if (((EFI_FIRMWARE_VOLUME_HEADER*)VolumeHandle)->Attributes & EFI_FVB2_ERASE_POLARITY) { - ErasePolarity = 1; - } else { - ErasePolarity = 0; - } - - // - // Get FileState which is the highest bit of the State - // - FileState = GetFileState (ErasePolarity, (EFI_FFS_FILE_HEADER*)FileHandle); - - switch (FileState) { - case EFI_FILE_DATA_VALID: - case EFI_FILE_MARKED_FOR_UPDATE: - break; - default: - return EFI_INVALID_PARAMETER; - } - - FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle; - CopyMem (&FileInfo->FileName, &FileHeader->Name, sizeof(EFI_GUID)); - FileInfo->FileType = FileHeader->Type; - FileInfo->FileAttributes = FileHeader->Attributes; - FileInfo->BufferSize = ((*(UINT32 *)FileHeader->Size) & 0x00FFFFFF) - sizeof (EFI_FFS_FILE_HEADER); - FileInfo->Buffer = (FileHeader + 1); - return EFI_SUCCESS; -} - - -/** - Get Information about the volume by name - - @param VolumeHandle Handle of the volume. - - @param VolumeInfo Upon exit, points to the volume's - information. - - @retval EFI_SUCCESS File information returned. - - @retval EFI_INVALID_PARAMETER If FileHandle does not - represent a valid file. - - @retval EFI_INVALID_PARAMETER If FileInfo is NULL. - -**/ -EFI_STATUS -EFIAPI -FfsGetVolumeInfo ( - IN EFI_PEI_FV_HANDLE VolumeHandle, - OUT EFI_FV_INFO *VolumeInfo - ) -{ - EFI_FIRMWARE_VOLUME_HEADER FwVolHeader; - EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo; - - if (VolumeInfo == NULL) { - return EFI_INVALID_PARAMETER; - } - - // - // VolumeHandle may not align at 8 byte, - // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte. - // So, Copy FvHeader into the local FvHeader structure. - // - CopyMem (&FwVolHeader, VolumeHandle, sizeof (EFI_FIRMWARE_VOLUME_HEADER)); - // - // Check Fv Image Signature - // - if (FwVolHeader.Signature != EFI_FVH_SIGNATURE) { - return EFI_INVALID_PARAMETER; - } - VolumeInfo->FvAttributes = FwVolHeader.Attributes; - VolumeInfo->FvStart = (VOID *) VolumeHandle; - VolumeInfo->FvSize = FwVolHeader.FvLength; - CopyMem (&VolumeInfo->FvFormat, &FwVolHeader.FileSystemGuid, sizeof(EFI_GUID)); - - if (FwVolHeader.ExtHeaderOffset != 0) { - FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER*)(((UINT8 *)VolumeHandle) + FwVolHeader.ExtHeaderOffset); - CopyMem (&VolumeInfo->FvName, &FwVolExHeaderInfo->FvName, sizeof(EFI_GUID)); - } - return EFI_SUCCESS; -} - - - -/** - Search through every FV until you find a file of type FileType - - @param FileType File handle of a Fv type file. - @param Volumehandle On succes Volume Handle of the match - @param FileHandle On success File Handle of the match - - @retval EFI_NOT_FOUND FV image can't be found. - @retval EFI_SUCCESS Successfully found FileType - -**/ -EFI_STATUS -EFIAPI -FfsAnyFvFindFirstFile ( - IN EFI_FV_FILETYPE FileType, - OUT EFI_PEI_FV_HANDLE *VolumeHandle, - OUT EFI_PEI_FILE_HANDLE *FileHandle - ) -{ - EFI_STATUS Status; - UINTN Instance; - - // - // Search every FV for the DXE Core - // - Instance = 0; - *FileHandle = NULL; - - while (1) - { - Status = FfsFindNextVolume (Instance++, VolumeHandle); - if (EFI_ERROR (Status)) - { - break; - } - - Status = FfsFindNextFile (FileType, *VolumeHandle, FileHandle); - if (!EFI_ERROR (Status)) - { - break; - } - } - - return Status; -} - - - -/** - Get Fv image from the FV type file, then add FV & FV2 Hob. - - @param FileHandle File handle of a Fv type file. - - - @retval EFI_NOT_FOUND FV image can't be found. - @retval EFI_SUCCESS Successfully to process it. - -**/ -EFI_STATUS -EFIAPI -FfsProcessFvFile ( - IN EFI_PEI_FILE_HANDLE FvFileHandle - ) -{ - EFI_STATUS Status; - EFI_PEI_FV_HANDLE FvImageHandle; - EFI_FV_INFO FvImageInfo; - UINT32 FvAlignment; - VOID *FvBuffer; - EFI_PEI_HOB_POINTERS HobFv2; - - FvBuffer = NULL; - - - // - // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already - // been extracted. - // - HobFv2.Raw = GetHobList (); - while ((HobFv2.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobFv2.Raw)) != NULL) { - if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name), &HobFv2.FirmwareVolume2->FileName)) { - // - // this FILE has been dispatched, it will not be dispatched again. - // - return EFI_SUCCESS; - } - HobFv2.Raw = GET_NEXT_HOB (HobFv2); - } - - // - // Find FvImage in FvFile - // - Status = FfsFindSectionData (EFI_SECTION_FIRMWARE_VOLUME_IMAGE, FvFileHandle, (VOID **)&FvImageHandle); - if (EFI_ERROR (Status)) { - return Status; - } - - // - // Collect FvImage Info. - // - ZeroMem (&FvImageInfo, sizeof (FvImageInfo)); - Status = FfsGetVolumeInfo (FvImageHandle, &FvImageInfo); - ASSERT_EFI_ERROR (Status); - - // - // FvAlignment must be more than 8 bytes required by FvHeader structure. - // - FvAlignment = 1 << ((FvImageInfo.FvAttributes & EFI_FVB2_ALIGNMENT) >> 16); - if (FvAlignment < 8) { - FvAlignment = 8; - } - - // - // Check FvImage - // - if ((UINTN) FvImageInfo.FvStart % FvAlignment != 0) { - FvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvImageInfo.FvSize), FvAlignment); - if (FvBuffer == NULL) { - return EFI_OUT_OF_RESOURCES; - } - CopyMem (FvBuffer, FvImageInfo.FvStart, (UINTN) FvImageInfo.FvSize); - // - // Update FvImageInfo after reload FvImage to new aligned memory - // - FfsGetVolumeInfo ((EFI_PEI_FV_HANDLE) FvBuffer, &FvImageInfo); - } - - - // - // Inform HOB consumer phase, i.e. DXE core, the existance of this FV - // - BuildFvHob ((EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart, FvImageInfo.FvSize); - - // - // Makes the encapsulated volume show up in DXE phase to skip processing of - // encapsulated file again. - // - BuildFv2Hob ( - (EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart, - FvImageInfo.FvSize, - &FvImageInfo.FvName, - &(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name) - ); - - return EFI_SUCCESS; -} - - +/** @file
+ Implementation of the 6 PEI Ffs (FV) APIs in library form.
+
+ This code only knows about a FV if it has a EFI_HOB_TYPE_FV entry in the HOB list
+
+ Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
+
+ 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 <PrePi.h>
+#include <Library/ExtractGuidedSectionLib.h>
+
+
+#define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
+ (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
+
+
+/**
+ Returns the highest bit set of the State field
+
+ @param ErasePolarity Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
+ in the Attributes field.
+ @param FfsHeader Pointer to FFS File Header
+
+
+ @retval the highest bit in the State field
+
+**/
+STATIC
+EFI_FFS_FILE_STATE
+GetFileState(
+ IN UINT8 ErasePolarity,
+ IN EFI_FFS_FILE_HEADER *FfsHeader
+ )
+{
+ EFI_FFS_FILE_STATE FileState;
+ EFI_FFS_FILE_STATE HighestBit;
+
+ FileState = FfsHeader->State;
+
+ if (ErasePolarity != 0) {
+ FileState = (EFI_FFS_FILE_STATE)~FileState;
+ }
+
+ HighestBit = 0x80;
+ while (HighestBit != 0 && (HighestBit & FileState) == 0) {
+ HighestBit >>= 1;
+ }
+
+ return HighestBit;
+}
+
+
+/**
+ Calculates the checksum of the header of a file.
+ The header is a zero byte checksum, so zero means header is good
+
+ @param FfsHeader Pointer to FFS File Header
+
+ @retval Checksum of the header
+
+**/
+STATIC
+UINT8
+CalculateHeaderChecksum (
+ IN EFI_FFS_FILE_HEADER *FileHeader
+ )
+{
+ UINT8 *Ptr;
+ UINTN Index;
+ UINT8 Sum;
+
+ Sum = 0;
+ Ptr = (UINT8 *)FileHeader;
+
+ for (Index = 0; Index < sizeof(EFI_FFS_FILE_HEADER) - 3; Index += 4) {
+ Sum = (UINT8)(Sum + Ptr[Index]);
+ Sum = (UINT8)(Sum + Ptr[Index+1]);
+ Sum = (UINT8)(Sum + Ptr[Index+2]);
+ Sum = (UINT8)(Sum + Ptr[Index+3]);
+ }
+
+ for (; Index < sizeof(EFI_FFS_FILE_HEADER); Index++) {
+ Sum = (UINT8)(Sum + Ptr[Index]);
+ }
+
+ //
+ // State field (since this indicates the different state of file).
+ //
+ Sum = (UINT8)(Sum - FileHeader->State);
+ //
+ // Checksum field of the file is not part of the header checksum.
+ //
+ Sum = (UINT8)(Sum - FileHeader->IntegrityCheck.Checksum.File);
+
+ return Sum;
+}
+
+
+/**
+ Given a FileHandle return the VolumeHandle
+
+ @param FileHandle File handle to look up
+ @param VolumeHandle Match for FileHandle
+
+ @retval TRUE VolumeHandle is valid
+
+**/
+STATIC
+BOOLEAN
+EFIAPI
+FileHandleToVolume (
+ IN EFI_PEI_FILE_HANDLE FileHandle,
+ OUT EFI_PEI_FV_HANDLE *VolumeHandle
+ )
+{
+ EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
+ EFI_PEI_HOB_POINTERS Hob;
+
+ Hob.Raw = GetHobList ();
+ if (Hob.Raw == NULL) {
+ return FALSE;
+ }
+
+ do {
+ Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw);
+ if (Hob.Raw != NULL) {
+ FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(Hob.FirmwareVolume->BaseAddress);
+ if (((UINT64) (UINTN) FileHandle > (UINT64) (UINTN) FwVolHeader ) && \
+ ((UINT64) (UINTN) FileHandle <= ((UINT64) (UINTN) FwVolHeader + FwVolHeader->FvLength - 1))) {
+ *VolumeHandle = (EFI_PEI_FV_HANDLE)FwVolHeader;
+ return TRUE;
+ }
+
+ Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, GET_NEXT_HOB (Hob));
+ }
+ } while (Hob.Raw != NULL);
+
+ return FALSE;
+}
+
+
+
+/**
+ Given the input file pointer, search for the next matching file in the
+ FFS volume as defined by SearchType. The search starts from FileHeader inside
+ the Firmware Volume defined by FwVolHeader.
+
+ @param FileHandle File handle to look up
+ @param VolumeHandle Match for FileHandle
+
+
+**/
+EFI_STATUS
+FindFileEx (
+ IN CONST EFI_PEI_FV_HANDLE FvHandle,
+ IN CONST EFI_GUID *FileName, OPTIONAL
+ IN EFI_FV_FILETYPE SearchType,
+ IN OUT EFI_PEI_FILE_HANDLE *FileHandle
+ )
+{
+ EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
+ EFI_FFS_FILE_HEADER **FileHeader;
+ EFI_FFS_FILE_HEADER *FfsFileHeader;
+ EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo;
+ UINT32 FileLength;
+ UINT32 FileOccupiedSize;
+ UINT32 FileOffset;
+ UINT64 FvLength;
+ UINT8 ErasePolarity;
+ UINT8 FileState;
+
+ FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FvHandle;
+ FileHeader = (EFI_FFS_FILE_HEADER **)FileHandle;
+
+ FvLength = FwVolHeader->FvLength;
+ if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
+ ErasePolarity = 1;
+ } else {
+ ErasePolarity = 0;
+ }
+
+ //
+ // If FileHeader is not specified (NULL) or FileName is not NULL,
+ // start with the first file in the firmware volume. Otherwise,
+ // start from the FileHeader.
+ //
+ if ((*FileHeader == NULL) || (FileName != NULL)) {
+ FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader + FwVolHeader->HeaderLength);
+ if (FwVolHeader->ExtHeaderOffset != 0) {
+ FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)(((UINT8 *)FwVolHeader) + FwVolHeader->ExtHeaderOffset);
+ FfsFileHeader = (EFI_FFS_FILE_HEADER *)(((UINT8 *)FwVolExHeaderInfo) + FwVolExHeaderInfo->ExtHeaderSize);
+ }
+ } else {
+ //
+ // Length is 24 bits wide so mask upper 8 bits
+ // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
+ //
+ FileLength = *(UINT32 *)(*FileHeader)->Size & 0x00FFFFFF;
+ FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
+ FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);
+ }
+
+ FileOffset = (UINT32) ((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
+ ASSERT (FileOffset <= 0xFFFFFFFF);
+
+ while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
+ //
+ // Get FileState which is the highest bit of the State
+ //
+ FileState = GetFileState (ErasePolarity, FfsFileHeader);
+
+ switch (FileState) {
+
+ case EFI_FILE_HEADER_INVALID:
+ FileOffset += sizeof(EFI_FFS_FILE_HEADER);
+ FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof(EFI_FFS_FILE_HEADER));
+ break;
+
+ case EFI_FILE_DATA_VALID:
+ case EFI_FILE_MARKED_FOR_UPDATE:
+ if (CalculateHeaderChecksum (FfsFileHeader) != 0) {
+ ASSERT (FALSE);
+ *FileHeader = NULL;
+ return EFI_NOT_FOUND;
+ }
+
+ FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
+ FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);
+
+ if (FileName != NULL) {
+ if (CompareGuid (&FfsFileHeader->Name, (EFI_GUID*)FileName)) {
+ *FileHeader = FfsFileHeader;
+ return EFI_SUCCESS;
+ }
+ } else if (((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) &&
+ (FfsFileHeader->Type != EFI_FV_FILETYPE_FFS_PAD)) {
+ *FileHeader = FfsFileHeader;
+ return EFI_SUCCESS;
+ }
+
+ FileOffset += FileOccupiedSize;
+ FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
+ break;
+
+ case EFI_FILE_DELETED:
+ FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
+ FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);
+ FileOffset += FileOccupiedSize;
+ FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
+ break;
+
+ default:
+ *FileHeader = NULL;
+ return EFI_NOT_FOUND;
+ }
+ }
+
+
+ *FileHeader = NULL;
+ return EFI_NOT_FOUND;
+}
+
+
+/**
+ Go through the file to search SectionType section,
+ when meeting an encapsuled section.
+
+ @param SectionType - Filter to find only section of this type.
+ @param Section - From where to search.
+ @param SectionSize - The file size to search.
+ @param OutputBuffer - Pointer to the section to search.
+
+ @retval EFI_SUCCESS
+**/
+EFI_STATUS
+FfsProcessSection (
+ IN EFI_SECTION_TYPE SectionType,
+ IN EFI_COMMON_SECTION_HEADER *Section,
+ IN UINTN SectionSize,
+ OUT VOID **OutputBuffer
+ )
+{
+ EFI_STATUS Status;
+ UINT32 SectionLength;
+ UINT32 ParsedLength;
+ EFI_COMPRESSION_SECTION *CompressionSection;
+ UINTN DstBufferSize;
+ VOID *ScratchBuffer;
+ UINT32 ScratchBufferSize;
+ VOID *DstBuffer;
+ UINT16 SectionAttribute;
+ UINT32 AuthenticationStatus;
+
+
+ *OutputBuffer = NULL;
+ ParsedLength = 0;
+ Status = EFI_NOT_FOUND;
+ while (ParsedLength < SectionSize) {
+ if (Section->Type == SectionType) {
+ *OutputBuffer = (VOID *)(Section + 1);
+
+ return EFI_SUCCESS;
+ } else if ((Section->Type == EFI_SECTION_COMPRESSION) || (Section->Type == EFI_SECTION_GUID_DEFINED)) {
+
+ if (Section->Type == EFI_SECTION_COMPRESSION) {
+ CompressionSection = (EFI_COMPRESSION_SECTION *) Section;
+ SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF;
+
+ if (CompressionSection->CompressionType != EFI_STANDARD_COMPRESSION) {
+ return EFI_UNSUPPORTED;
+ }
+
+ Status = UefiDecompressGetInfo (
+ (UINT8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),
+ (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION),
+ (UINT32 *) &DstBufferSize,
+ &ScratchBufferSize
+ );
+ } else if (Section->Type == EFI_SECTION_GUID_DEFINED) {
+ Status = ExtractGuidedSectionGetInfo (
+ Section,
+ (UINT32 *) &DstBufferSize,
+ &ScratchBufferSize,
+ &SectionAttribute
+ );
+ }
+
+ if (EFI_ERROR (Status)) {
+ //
+ // GetInfo failed
+ //
+ DEBUG ((EFI_D_ERROR, "Decompress GetInfo Failed - %r\n", Status));
+ return EFI_NOT_FOUND;
+ }
+ //
+ // Allocate scratch buffer
+ //
+ ScratchBuffer = (VOID *)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));
+ if (ScratchBuffer == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+ //
+ // Allocate destination buffer, extra one page for adjustment
+ //
+ DstBuffer = (VOID *)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize) + 1);
+ if (DstBuffer == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+ //
+ // DstBuffer still is one section. Adjust DstBuffer offset, skip EFI section header
+ // to make section data at page alignment.
+ //
+ DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER);
+ //
+ // Call decompress function
+ //
+ if (Section->Type == EFI_SECTION_COMPRESSION) {
+ Status = UefiDecompress (
+ (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),
+ DstBuffer,
+ ScratchBuffer
+ );
+ } else if (Section->Type == EFI_SECTION_GUID_DEFINED) {
+ Status = ExtractGuidedSectionDecode (
+ Section,
+ &DstBuffer,
+ ScratchBuffer,
+ &AuthenticationStatus
+ );
+ }
+
+ if (EFI_ERROR (Status)) {
+ //
+ // Decompress failed
+ //
+ DEBUG ((EFI_D_ERROR, "Decompress Failed - %r\n", Status));
+ return EFI_NOT_FOUND;
+ } else {
+ return FfsProcessSection (
+ SectionType,
+ DstBuffer,
+ DstBufferSize,
+ OutputBuffer
+ );
+ }
+ }
+
+ //
+ // Size is 24 bits wide so mask upper 8 bits.
+ // SectionLength is adjusted it is 4 byte aligned.
+ // Go to the next section
+ //
+ SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF;
+ SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
+ ASSERT (SectionLength != 0);
+ ParsedLength += SectionLength;
+ Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
+ }
+
+ return EFI_NOT_FOUND;
+}
+
+
+
+/**
+ This service enables discovery sections of a given type within a valid FFS file.
+
+ @param SearchType The value of the section type to find.
+ @param FfsFileHeader A pointer to the file header that contains the set of sections to
+ be searched.
+ @param SectionData A pointer to the discovered section, if successful.
+
+ @retval EFI_SUCCESS The section was found.
+ @retval EFI_NOT_FOUND The section was not found.
+
+**/
+EFI_STATUS
+EFIAPI
+FfsFindSectionData (
+ IN EFI_SECTION_TYPE SectionType,
+ IN EFI_PEI_FILE_HANDLE FileHandle,
+ OUT VOID **SectionData
+ )
+{
+ EFI_FFS_FILE_HEADER *FfsFileHeader;
+ UINT32 FileSize;
+ EFI_COMMON_SECTION_HEADER *Section;
+
+ FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);
+
+ //
+ // Size is 24 bits wide so mask upper 8 bits.
+ // Does not include FfsFileHeader header size
+ // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
+ //
+ Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
+ FileSize = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
+ FileSize -= sizeof (EFI_FFS_FILE_HEADER);
+
+ return FfsProcessSection (
+ SectionType,
+ Section,
+ FileSize,
+ SectionData
+ );
+}
+
+
+
+
+
+
+/**
+ This service enables discovery of additional firmware files.
+
+ @param SearchType A filter to find files only of this type.
+ @param FwVolHeader Pointer to the firmware volume header of the volume to search.
+ This parameter must point to a valid FFS volume.
+ @param FileHeader Pointer to the current file from which to begin searching.
+
+ @retval EFI_SUCCESS The file was found.
+ @retval EFI_NOT_FOUND The file was not found.
+ @retval EFI_NOT_FOUND The header checksum was not zero.
+
+**/
+EFI_STATUS
+EFIAPI
+FfsFindNextFile (
+ IN UINT8 SearchType,
+ IN EFI_PEI_FV_HANDLE VolumeHandle,
+ IN OUT EFI_PEI_FILE_HANDLE *FileHandle
+ )
+{
+ return FindFileEx (VolumeHandle, NULL, SearchType, FileHandle);
+}
+
+
+/**
+ This service enables discovery of additional firmware volumes.
+
+ @param Instance This instance of the firmware volume to find. The value 0 is the
+ Boot Firmware Volume (BFV).
+ @param FwVolHeader Pointer to the firmware volume header of the volume to return.
+
+ @retval EFI_SUCCESS The volume was found.
+ @retval EFI_NOT_FOUND The volume was not found.
+
+**/
+EFI_STATUS
+EFIAPI
+FfsFindNextVolume (
+ IN UINTN Instance,
+ IN OUT EFI_PEI_FV_HANDLE *VolumeHandle
+ )
+{
+ EFI_PEI_HOB_POINTERS Hob;
+
+
+ Hob.Raw = GetHobList ();
+ if (Hob.Raw == NULL) {
+ return EFI_NOT_FOUND;
+ }
+
+ do {
+ Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw);
+ if (Hob.Raw != NULL) {
+ if (Instance-- == 0) {
+ *VolumeHandle = (EFI_PEI_FV_HANDLE)(UINTN)(Hob.FirmwareVolume->BaseAddress);
+ return EFI_SUCCESS;
+ }
+
+ Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, GET_NEXT_HOB (Hob));
+ }
+ } while (Hob.Raw != NULL);
+
+ return EFI_NOT_FOUND;
+
+}
+
+
+/**
+ Find a file in the volume by name
+
+ @param FileName A pointer to the name of the file to
+ find within the firmware volume.
+
+ @param VolumeHandle The firmware volume to search FileHandle
+ Upon exit, points to the found file's
+ handle or NULL if it could not be found.
+
+ @retval EFI_SUCCESS File was found.
+
+ @retval EFI_NOT_FOUND File was not found.
+
+ @retval EFI_INVALID_PARAMETER VolumeHandle or FileHandle or
+ FileName was NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+FfsFindFileByName (
+ IN CONST EFI_GUID *FileName,
+ IN EFI_PEI_FV_HANDLE VolumeHandle,
+ OUT EFI_PEI_FILE_HANDLE *FileHandle
+ )
+{
+ EFI_STATUS Status;
+ if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {
+ return EFI_INVALID_PARAMETER;
+ }
+ Status = FindFileEx (VolumeHandle, FileName, 0, FileHandle);
+ if (Status == EFI_NOT_FOUND) {
+ *FileHandle = NULL;
+ }
+ return Status;
+}
+
+
+
+
+/**
+ Get information about the file by name.
+
+ @param FileHandle Handle of the file.
+
+ @param FileInfo Upon exit, points to the file's
+ information.
+
+ @retval EFI_SUCCESS File information returned.
+
+ @retval EFI_INVALID_PARAMETER If FileHandle does not
+ represent a valid file.
+
+ @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+FfsGetFileInfo (
+ IN EFI_PEI_FILE_HANDLE FileHandle,
+ OUT EFI_FV_FILE_INFO *FileInfo
+ )
+{
+ UINT8 FileState;
+ UINT8 ErasePolarity;
+ EFI_FFS_FILE_HEADER *FileHeader;
+ EFI_PEI_FV_HANDLE VolumeHandle;
+
+ if ((FileHandle == NULL) || (FileInfo == NULL)) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ VolumeHandle = 0;
+ //
+ // Retrieve the FirmwareVolume which the file resides in.
+ //
+ if (!FileHandleToVolume(FileHandle, &VolumeHandle)) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ if (((EFI_FIRMWARE_VOLUME_HEADER*)VolumeHandle)->Attributes & EFI_FVB2_ERASE_POLARITY) {
+ ErasePolarity = 1;
+ } else {
+ ErasePolarity = 0;
+ }
+
+ //
+ // Get FileState which is the highest bit of the State
+ //
+ FileState = GetFileState (ErasePolarity, (EFI_FFS_FILE_HEADER*)FileHandle);
+
+ switch (FileState) {
+ case EFI_FILE_DATA_VALID:
+ case EFI_FILE_MARKED_FOR_UPDATE:
+ break;
+ default:
+ return EFI_INVALID_PARAMETER;
+ }
+
+ FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle;
+ CopyMem (&FileInfo->FileName, &FileHeader->Name, sizeof(EFI_GUID));
+ FileInfo->FileType = FileHeader->Type;
+ FileInfo->FileAttributes = FileHeader->Attributes;
+ FileInfo->BufferSize = ((*(UINT32 *)FileHeader->Size) & 0x00FFFFFF) - sizeof (EFI_FFS_FILE_HEADER);
+ FileInfo->Buffer = (FileHeader + 1);
+ return EFI_SUCCESS;
+}
+
+
+/**
+ Get Information about the volume by name
+
+ @param VolumeHandle Handle of the volume.
+
+ @param VolumeInfo Upon exit, points to the volume's
+ information.
+
+ @retval EFI_SUCCESS File information returned.
+
+ @retval EFI_INVALID_PARAMETER If FileHandle does not
+ represent a valid file.
+
+ @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+FfsGetVolumeInfo (
+ IN EFI_PEI_FV_HANDLE VolumeHandle,
+ OUT EFI_FV_INFO *VolumeInfo
+ )
+{
+ EFI_FIRMWARE_VOLUME_HEADER FwVolHeader;
+ EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo;
+
+ if (VolumeInfo == NULL) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ //
+ // VolumeHandle may not align at 8 byte,
+ // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte.
+ // So, Copy FvHeader into the local FvHeader structure.
+ //
+ CopyMem (&FwVolHeader, VolumeHandle, sizeof (EFI_FIRMWARE_VOLUME_HEADER));
+ //
+ // Check Fv Image Signature
+ //
+ if (FwVolHeader.Signature != EFI_FVH_SIGNATURE) {
+ return EFI_INVALID_PARAMETER;
+ }
+ VolumeInfo->FvAttributes = FwVolHeader.Attributes;
+ VolumeInfo->FvStart = (VOID *) VolumeHandle;
+ VolumeInfo->FvSize = FwVolHeader.FvLength;
+ CopyMem (&VolumeInfo->FvFormat, &FwVolHeader.FileSystemGuid, sizeof(EFI_GUID));
+
+ if (FwVolHeader.ExtHeaderOffset != 0) {
+ FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER*)(((UINT8 *)VolumeHandle) + FwVolHeader.ExtHeaderOffset);
+ CopyMem (&VolumeInfo->FvName, &FwVolExHeaderInfo->FvName, sizeof(EFI_GUID));
+ }
+ return EFI_SUCCESS;
+}
+
+
+
+/**
+ Search through every FV until you find a file of type FileType
+
+ @param FileType File handle of a Fv type file.
+ @param Volumehandle On succes Volume Handle of the match
+ @param FileHandle On success File Handle of the match
+
+ @retval EFI_NOT_FOUND FV image can't be found.
+ @retval EFI_SUCCESS Successfully found FileType
+
+**/
+EFI_STATUS
+EFIAPI
+FfsAnyFvFindFirstFile (
+ IN EFI_FV_FILETYPE FileType,
+ OUT EFI_PEI_FV_HANDLE *VolumeHandle,
+ OUT EFI_PEI_FILE_HANDLE *FileHandle
+ )
+{
+ EFI_STATUS Status;
+ UINTN Instance;
+
+ //
+ // Search every FV for the DXE Core
+ //
+ Instance = 0;
+ *FileHandle = NULL;
+
+ while (1)
+ {
+ Status = FfsFindNextVolume (Instance++, VolumeHandle);
+ if (EFI_ERROR (Status))
+ {
+ break;
+ }
+
+ Status = FfsFindNextFile (FileType, *VolumeHandle, FileHandle);
+ if (!EFI_ERROR (Status))
+ {
+ break;
+ }
+ }
+
+ return Status;
+}
+
+
+
+/**
+ Get Fv image from the FV type file, then add FV & FV2 Hob.
+
+ @param FileHandle File handle of a Fv type file.
+
+
+ @retval EFI_NOT_FOUND FV image can't be found.
+ @retval EFI_SUCCESS Successfully to process it.
+
+**/
+EFI_STATUS
+EFIAPI
+FfsProcessFvFile (
+ IN EFI_PEI_FILE_HANDLE FvFileHandle
+ )
+{
+ EFI_STATUS Status;
+ EFI_PEI_FV_HANDLE FvImageHandle;
+ EFI_FV_INFO FvImageInfo;
+ UINT32 FvAlignment;
+ VOID *FvBuffer;
+ EFI_PEI_HOB_POINTERS HobFv2;
+
+ FvBuffer = NULL;
+
+
+ //
+ // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already
+ // been extracted.
+ //
+ HobFv2.Raw = GetHobList ();
+ while ((HobFv2.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobFv2.Raw)) != NULL) {
+ if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name), &HobFv2.FirmwareVolume2->FileName)) {
+ //
+ // this FILE has been dispatched, it will not be dispatched again.
+ //
+ return EFI_SUCCESS;
+ }
+ HobFv2.Raw = GET_NEXT_HOB (HobFv2);
+ }
+
+ //
+ // Find FvImage in FvFile
+ //
+ Status = FfsFindSectionData (EFI_SECTION_FIRMWARE_VOLUME_IMAGE, FvFileHandle, (VOID **)&FvImageHandle);
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+
+ //
+ // Collect FvImage Info.
+ //
+ ZeroMem (&FvImageInfo, sizeof (FvImageInfo));
+ Status = FfsGetVolumeInfo (FvImageHandle, &FvImageInfo);
+ ASSERT_EFI_ERROR (Status);
+
+ //
+ // FvAlignment must be more than 8 bytes required by FvHeader structure.
+ //
+ FvAlignment = 1 << ((FvImageInfo.FvAttributes & EFI_FVB2_ALIGNMENT) >> 16);
+ if (FvAlignment < 8) {
+ FvAlignment = 8;
+ }
+
+ //
+ // Check FvImage
+ //
+ if ((UINTN) FvImageInfo.FvStart % FvAlignment != 0) {
+ FvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvImageInfo.FvSize), FvAlignment);
+ if (FvBuffer == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+ CopyMem (FvBuffer, FvImageInfo.FvStart, (UINTN) FvImageInfo.FvSize);
+ //
+ // Update FvImageInfo after reload FvImage to new aligned memory
+ //
+ FfsGetVolumeInfo ((EFI_PEI_FV_HANDLE) FvBuffer, &FvImageInfo);
+ }
+
+
+ //
+ // Inform HOB consumer phase, i.e. DXE core, the existance of this FV
+ //
+ BuildFvHob ((EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart, FvImageInfo.FvSize);
+
+ //
+ // Makes the encapsulated volume show up in DXE phase to skip processing of
+ // encapsulated file again.
+ //
+ BuildFv2Hob (
+ (EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart,
+ FvImageInfo.FvSize,
+ &FvImageInfo.FvName,
+ &(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name)
+ );
+
+ return EFI_SUCCESS;
+}
+
+
diff --git a/EmbeddedPkg/Library/PrePiLib/PrePi.h b/EmbeddedPkg/Library/PrePiLib/PrePi.h index 77fe32898b..d637277d2a 100644 --- a/EmbeddedPkg/Library/PrePiLib/PrePi.h +++ b/EmbeddedPkg/Library/PrePiLib/PrePi.h @@ -1,46 +1,46 @@ -/** @file - Library that helps implement monolithic PEI (i.e. PEI part of SEC) - - Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR> - - 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. - -**/ - -#ifndef _PI_PEI_H_ -#define _PI_PEI_H_ - -#include <PiPei.h> - -#include <Library/BaseLib.h> -#include <Library/PrePiLib.h> -#include <Library/PcdLib.h> -#include <Library/DebugLib.h> -#include <Library/BaseMemoryLib.h> -#include <Library/UefiDecompressLib.h> -#include <Library/PeCoffLib.h> -#include <Library/CacheMaintenanceLib.h> -#include <Library/TimerLib.h> -#include <Library/PerformanceLib.h> - -#include <Guid/MemoryAllocationHob.h> - - -#define GET_HOB_TYPE(Hob) ((Hob).Header->HobType) -#define GET_HOB_LENGTH(Hob) ((Hob).Header->HobLength) -#define GET_NEXT_HOB(Hob) ((Hob).Raw + GET_HOB_LENGTH (Hob)) -#define END_OF_HOB_LIST(Hob) (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_END_OF_HOB_LIST) - -// -// Get the data and data size field of GUID -// -#define GET_GUID_HOB_DATA(GuidHob) ((VOID *) (((UINT8 *) &((GuidHob)->Name)) + sizeof (EFI_GUID))) -#define GET_GUID_HOB_DATA_SIZE(GuidHob) (((GuidHob)->Header).HobLength - sizeof (EFI_HOB_GUID_TYPE)) - -#endif +/** @file
+ Library that helps implement monolithic PEI (i.e. PEI part of SEC)
+
+ Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
+
+ 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.
+
+**/
+
+#ifndef _PI_PEI_H_
+#define _PI_PEI_H_
+
+#include <PiPei.h>
+
+#include <Library/BaseLib.h>
+#include <Library/PrePiLib.h>
+#include <Library/PcdLib.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/UefiDecompressLib.h>
+#include <Library/PeCoffLib.h>
+#include <Library/CacheMaintenanceLib.h>
+#include <Library/TimerLib.h>
+#include <Library/PerformanceLib.h>
+
+#include <Guid/MemoryAllocationHob.h>
+
+
+#define GET_HOB_TYPE(Hob) ((Hob).Header->HobType)
+#define GET_HOB_LENGTH(Hob) ((Hob).Header->HobLength)
+#define GET_NEXT_HOB(Hob) ((Hob).Raw + GET_HOB_LENGTH (Hob))
+#define END_OF_HOB_LIST(Hob) (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_END_OF_HOB_LIST)
+
+//
+// Get the data and data size field of GUID
+//
+#define GET_GUID_HOB_DATA(GuidHob) ((VOID *) (((UINT8 *) &((GuidHob)->Name)) + sizeof (EFI_GUID)))
+#define GET_GUID_HOB_DATA_SIZE(GuidHob) (((GuidHob)->Header).HobLength - sizeof (EFI_HOB_GUID_TYPE))
+
+#endif
diff --git a/EmbeddedPkg/Library/PrePiLib/PrePiLib.c b/EmbeddedPkg/Library/PrePiLib/PrePiLib.c index e46cafec3b..4cc974f1a9 100644 --- a/EmbeddedPkg/Library/PrePiLib/PrePiLib.c +++ b/EmbeddedPkg/Library/PrePiLib/PrePiLib.c @@ -1,226 +1,226 @@ -/** @file - - Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR> - - 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 <PrePi.h> - -// -// Hack to work in NT32 -// -EFI_STATUS - -EFIAPI - -SecWinNtPeiLoadFile ( - IN VOID *Pe32Data, - IN EFI_PHYSICAL_ADDRESS *ImageAddress, - IN UINT64 *ImageSize, - IN EFI_PHYSICAL_ADDRESS *EntryPoint - ); - - -EFI_STATUS -EFIAPI -LoadPeCoffImage ( - IN VOID *PeCoffImage, - OUT EFI_PHYSICAL_ADDRESS *ImageAddress, - OUT UINT64 *ImageSize, - OUT EFI_PHYSICAL_ADDRESS *EntryPoint - ) -{ - RETURN_STATUS Status; - PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; - VOID *Buffer; - - ZeroMem (&ImageContext, sizeof (ImageContext)); - - ImageContext.Handle = PeCoffImage; - ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory; - - Status = PeCoffLoaderGetImageInfo (&ImageContext); - ASSERT_EFI_ERROR (Status); - - - // - // Allocate Memory for the image - // - Buffer = AllocatePages (EFI_SIZE_TO_PAGES((UINT32)ImageContext.ImageSize)); - ASSERT (Buffer != 0); - - - ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer; - - // - // Load the image to our new buffer - // - Status = PeCoffLoaderLoadImage (&ImageContext); - ASSERT_EFI_ERROR (Status); - - // - // Relocate the image in our new buffer - // - Status = PeCoffLoaderRelocateImage (&ImageContext); - ASSERT_EFI_ERROR (Status); - - - *ImageAddress = ImageContext.ImageAddress; - *ImageSize = ImageContext.ImageSize; - *EntryPoint = ImageContext.EntryPoint; - - // - // Flush not needed for all architectures. We could have a processor specific - // function in this library that does the no-op if needed. - // - InvalidateInstructionCacheRange ((VOID *)(UINTN)*ImageAddress, (UINTN)*ImageSize); - - return Status; -} - - - -typedef -VOID -(EFIAPI *DXE_CORE_ENTRY_POINT) ( - IN VOID *HobStart - ); - -EFI_STATUS -EFIAPI -LoadDxeCoreFromFfsFile ( - IN EFI_PEI_FILE_HANDLE FileHandle, - IN UINTN StackSize - ) -{ - EFI_STATUS Status; - VOID *PeCoffImage; - EFI_PHYSICAL_ADDRESS ImageAddress; - UINT64 ImageSize; - EFI_PHYSICAL_ADDRESS EntryPoint; - VOID *BaseOfStack; - VOID *TopOfStack; - VOID *Hob; - EFI_FV_FILE_INFO FvFileInfo; - - Status = FfsFindSectionData (EFI_SECTION_PE32, FileHandle, &PeCoffImage); - if (EFI_ERROR (Status)) { - return Status; - } - - - Status = LoadPeCoffImage (PeCoffImage, &ImageAddress, &ImageSize, &EntryPoint); -// For NT32 Debug Status = SecWinNtPeiLoadFile (PeCoffImage, &ImageAddress, &ImageSize, &EntryPoint); - ASSERT_EFI_ERROR (Status); - - // - // Extract the DxeCore GUID file name. - // - Status = FfsGetFileInfo (FileHandle, &FvFileInfo); - ASSERT_EFI_ERROR (Status); - - BuildModuleHob (&FvFileInfo.FileName, (EFI_PHYSICAL_ADDRESS)(UINTN)ImageAddress, EFI_SIZE_TO_PAGES ((UINT32) ImageSize) * EFI_PAGE_SIZE, EntryPoint); - - DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading DxeCore at 0x%10p EntryPoint=0x%10p\n", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)EntryPoint)); - - Hob = GetHobList (); - if (StackSize == 0) { - // User the current stack - - ((DXE_CORE_ENTRY_POINT)(UINTN)EntryPoint) (Hob); - } else { - - // - // Allocate 128KB for the Stack - // - BaseOfStack = AllocatePages (EFI_SIZE_TO_PAGES (StackSize)); - ASSERT (BaseOfStack != NULL); - - // - // Compute the top of the stack we were allocated. Pre-allocate a UINTN - // for safety. - // - TopOfStack = (VOID *) ((UINTN) BaseOfStack + EFI_SIZE_TO_PAGES (StackSize) * EFI_PAGE_SIZE - CPU_STACK_ALIGNMENT); - TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT); - - // - // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore. - // - UpdateStackHob ((EFI_PHYSICAL_ADDRESS)(UINTN) BaseOfStack, StackSize); - - SwitchStack ( - (SWITCH_STACK_ENTRY_POINT)(UINTN)EntryPoint, - Hob, - NULL, - TopOfStack - ); - - } - - // Should never get here as DXE Core does not return - DEBUG ((EFI_D_ERROR, "DxeCore returned\n")); - ASSERT (FALSE); - - return EFI_DEVICE_ERROR; -} - - - -EFI_STATUS -EFIAPI -LoadDxeCoreFromFv ( - IN UINTN *FvInstance, OPTIONAL - IN UINTN StackSize - ) -{ - EFI_STATUS Status; - EFI_PEI_FV_HANDLE VolumeHandle; - EFI_PEI_FILE_HANDLE FileHandle = NULL; - - if (FvInstance != NULL) { - // - // Caller passed in a specific FV to try, so only try that one - // - Status = FfsFindNextVolume (*FvInstance, &VolumeHandle); - if (!EFI_ERROR (Status)) { - Status = FfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE, VolumeHandle, &FileHandle); - } - } else { - Status = FfsAnyFvFindFirstFile (EFI_FV_FILETYPE_DXE_CORE, &VolumeHandle, &FileHandle); - } - - if (!EFI_ERROR (Status)) { - return LoadDxeCoreFromFfsFile (FileHandle, StackSize); - } - - return Status; -} - - -EFI_STATUS -EFIAPI -DecompressFirstFv ( - VOID - ) -{ - EFI_STATUS Status; - EFI_PEI_FV_HANDLE VolumeHandle; - EFI_PEI_FILE_HANDLE FileHandle; - - Status = FfsAnyFvFindFirstFile (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, &VolumeHandle, &FileHandle); - if (!EFI_ERROR (Status)) { - Status = FfsProcessFvFile (FileHandle); - } - - return Status; -} - - +/** @file
+
+ Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
+
+ 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 <PrePi.h>
+
+//
+// Hack to work in NT32
+//
+EFI_STATUS
+
+EFIAPI
+
+SecWinNtPeiLoadFile (
+ IN VOID *Pe32Data,
+ IN EFI_PHYSICAL_ADDRESS *ImageAddress,
+ IN UINT64 *ImageSize,
+ IN EFI_PHYSICAL_ADDRESS *EntryPoint
+ );
+
+
+EFI_STATUS
+EFIAPI
+LoadPeCoffImage (
+ IN VOID *PeCoffImage,
+ OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
+ OUT UINT64 *ImageSize,
+ OUT EFI_PHYSICAL_ADDRESS *EntryPoint
+ )
+{
+ RETURN_STATUS Status;
+ PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
+ VOID *Buffer;
+
+ ZeroMem (&ImageContext, sizeof (ImageContext));
+
+ ImageContext.Handle = PeCoffImage;
+ ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
+
+ Status = PeCoffLoaderGetImageInfo (&ImageContext);
+ ASSERT_EFI_ERROR (Status);
+
+
+ //
+ // Allocate Memory for the image
+ //
+ Buffer = AllocatePages (EFI_SIZE_TO_PAGES((UINT32)ImageContext.ImageSize));
+ ASSERT (Buffer != 0);
+
+
+ ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer;
+
+ //
+ // Load the image to our new buffer
+ //
+ Status = PeCoffLoaderLoadImage (&ImageContext);
+ ASSERT_EFI_ERROR (Status);
+
+ //
+ // Relocate the image in our new buffer
+ //
+ Status = PeCoffLoaderRelocateImage (&ImageContext);
+ ASSERT_EFI_ERROR (Status);
+
+
+ *ImageAddress = ImageContext.ImageAddress;
+ *ImageSize = ImageContext.ImageSize;
+ *EntryPoint = ImageContext.EntryPoint;
+
+ //
+ // Flush not needed for all architectures. We could have a processor specific
+ // function in this library that does the no-op if needed.
+ //
+ InvalidateInstructionCacheRange ((VOID *)(UINTN)*ImageAddress, (UINTN)*ImageSize);
+
+ return Status;
+}
+
+
+
+typedef
+VOID
+(EFIAPI *DXE_CORE_ENTRY_POINT) (
+ IN VOID *HobStart
+ );
+
+EFI_STATUS
+EFIAPI
+LoadDxeCoreFromFfsFile (
+ IN EFI_PEI_FILE_HANDLE FileHandle,
+ IN UINTN StackSize
+ )
+{
+ EFI_STATUS Status;
+ VOID *PeCoffImage;
+ EFI_PHYSICAL_ADDRESS ImageAddress;
+ UINT64 ImageSize;
+ EFI_PHYSICAL_ADDRESS EntryPoint;
+ VOID *BaseOfStack;
+ VOID *TopOfStack;
+ VOID *Hob;
+ EFI_FV_FILE_INFO FvFileInfo;
+
+ Status = FfsFindSectionData (EFI_SECTION_PE32, FileHandle, &PeCoffImage);
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+
+
+ Status = LoadPeCoffImage (PeCoffImage, &ImageAddress, &ImageSize, &EntryPoint);
+// For NT32 Debug Status = SecWinNtPeiLoadFile (PeCoffImage, &ImageAddress, &ImageSize, &EntryPoint);
+ ASSERT_EFI_ERROR (Status);
+
+ //
+ // Extract the DxeCore GUID file name.
+ //
+ Status = FfsGetFileInfo (FileHandle, &FvFileInfo);
+ ASSERT_EFI_ERROR (Status);
+
+ BuildModuleHob (&FvFileInfo.FileName, (EFI_PHYSICAL_ADDRESS)(UINTN)ImageAddress, EFI_SIZE_TO_PAGES ((UINT32) ImageSize) * EFI_PAGE_SIZE, EntryPoint);
+
+ DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading DxeCore at 0x%10p EntryPoint=0x%10p\n", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)EntryPoint));
+
+ Hob = GetHobList ();
+ if (StackSize == 0) {
+ // User the current stack
+
+ ((DXE_CORE_ENTRY_POINT)(UINTN)EntryPoint) (Hob);
+ } else {
+
+ //
+ // Allocate 128KB for the Stack
+ //
+ BaseOfStack = AllocatePages (EFI_SIZE_TO_PAGES (StackSize));
+ ASSERT (BaseOfStack != NULL);
+
+ //
+ // Compute the top of the stack we were allocated. Pre-allocate a UINTN
+ // for safety.
+ //
+ TopOfStack = (VOID *) ((UINTN) BaseOfStack + EFI_SIZE_TO_PAGES (StackSize) * EFI_PAGE_SIZE - CPU_STACK_ALIGNMENT);
+ TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);
+
+ //
+ // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.
+ //
+ UpdateStackHob ((EFI_PHYSICAL_ADDRESS)(UINTN) BaseOfStack, StackSize);
+
+ SwitchStack (
+ (SWITCH_STACK_ENTRY_POINT)(UINTN)EntryPoint,
+ Hob,
+ NULL,
+ TopOfStack
+ );
+
+ }
+
+ // Should never get here as DXE Core does not return
+ DEBUG ((EFI_D_ERROR, "DxeCore returned\n"));
+ ASSERT (FALSE);
+
+ return EFI_DEVICE_ERROR;
+}
+
+
+
+EFI_STATUS
+EFIAPI
+LoadDxeCoreFromFv (
+ IN UINTN *FvInstance, OPTIONAL
+ IN UINTN StackSize
+ )
+{
+ EFI_STATUS Status;
+ EFI_PEI_FV_HANDLE VolumeHandle;
+ EFI_PEI_FILE_HANDLE FileHandle = NULL;
+
+ if (FvInstance != NULL) {
+ //
+ // Caller passed in a specific FV to try, so only try that one
+ //
+ Status = FfsFindNextVolume (*FvInstance, &VolumeHandle);
+ if (!EFI_ERROR (Status)) {
+ Status = FfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE, VolumeHandle, &FileHandle);
+ }
+ } else {
+ Status = FfsAnyFvFindFirstFile (EFI_FV_FILETYPE_DXE_CORE, &VolumeHandle, &FileHandle);
+ }
+
+ if (!EFI_ERROR (Status)) {
+ return LoadDxeCoreFromFfsFile (FileHandle, StackSize);
+ }
+
+ return Status;
+}
+
+
+EFI_STATUS
+EFIAPI
+DecompressFirstFv (
+ VOID
+ )
+{
+ EFI_STATUS Status;
+ EFI_PEI_FV_HANDLE VolumeHandle;
+ EFI_PEI_FILE_HANDLE FileHandle;
+
+ Status = FfsAnyFvFindFirstFile (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, &VolumeHandle, &FileHandle);
+ if (!EFI_ERROR (Status)) {
+ Status = FfsProcessFvFile (FileHandle);
+ }
+
+ return Status;
+}
+
+
diff --git a/EmbeddedPkg/Library/PrePiLib/ReportStatusCode.c b/EmbeddedPkg/Library/PrePiLib/ReportStatusCode.c index f898808deb..8d60d5ab5d 100644 --- a/EmbeddedPkg/Library/PrePiLib/ReportStatusCode.c +++ b/EmbeddedPkg/Library/PrePiLib/ReportStatusCode.c @@ -1,327 +1,327 @@ -/** @file - Library that helps implement monolithic PEI - - Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR> - - 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 <PrePi.h> -#include <Library/ReportStatusCodeLib.h> -#include <Library/SerialPortLib.h> -#include <Library/PrintLib.h> - -#include <Protocol/StatusCode.h> -#include <Guid/StatusCodeDataTypeId.h> -#include <Guid/StatusCodeDataTypeDebug.h> -#include <FrameworkPei.h> - -#define EFI_STATUS_CODE_DATA_MAX_SIZE 200 - -EFI_STATUS -EFIAPI -SerialReportStatusCode ( - IN EFI_STATUS_CODE_TYPE CodeType, - IN EFI_STATUS_CODE_VALUE Value, - IN UINT32 Instance, - IN CONST EFI_GUID *CallerId, - IN CONST EFI_STATUS_CODE_DATA *Data OPTIONAL - ); - - -EFI_STATUS_CODE_PROTOCOL gStatusCode = { - (EFI_REPORT_STATUS_CODE)SerialReportStatusCode -}; - -/** - Extracts ASSERT() information from a status code structure. - - Converts the status code specified by CodeType, Value, and Data to the ASSERT() - arguments specified by Filename, Description, and LineNumber. If CodeType is - an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and - Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract - Filename, Description, and LineNumber from the optional data area of the - status code buffer specified by Data. The optional data area of Data contains - a Null-terminated ASCII string for the FileName, followed by a Null-terminated - ASCII string for the Description, followed by a 32-bit LineNumber. If the - ASSERT() information could be extracted from Data, then return TRUE. - Otherwise, FALSE is returned. - - If Data is NULL, then ASSERT(). - If Filename is NULL, then ASSERT(). - If Description is NULL, then ASSERT(). - If LineNumber is NULL, then ASSERT(). - - @param CodeType The type of status code being converted. - @param Value The status code value being converted. - @param Data Pointer to status code data buffer. - @param Filename Pointer to the source file name that generated the ASSERT(). - @param Description Pointer to the description of the ASSERT(). - @param LineNumber Pointer to source line number that generated the ASSERT(). - - @retval TRUE The status code specified by CodeType, Value, and Data was - converted ASSERT() arguments specified by Filename, Description, - and LineNumber. - @retval FALSE The status code specified by CodeType, Value, and Data could - not be converted to ASSERT() arguments. - -**/ -BOOLEAN -EFIAPI -ReportStatusCodeExtractAssertInfo ( - IN EFI_STATUS_CODE_TYPE CodeType, - IN EFI_STATUS_CODE_VALUE Value, - IN CONST EFI_STATUS_CODE_DATA *Data, - OUT CHAR8 **Filename, - OUT CHAR8 **Description, - OUT UINT32 *LineNumber - ) -{ - EFI_DEBUG_ASSERT_DATA *AssertData; - - ASSERT (Data != NULL); - ASSERT (Filename != NULL); - ASSERT (Description != NULL); - ASSERT (LineNumber != NULL); - - if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) && - ((CodeType & EFI_STATUS_CODE_SEVERITY_MASK) == EFI_ERROR_UNRECOVERED) && - ((Value & EFI_STATUS_CODE_OPERATION_MASK) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)) { - AssertData = (EFI_DEBUG_ASSERT_DATA *)(Data + 1); - *Filename = (CHAR8 *)(AssertData + 1); - *Description = *Filename + AsciiStrLen (*Filename) + 1; - *LineNumber = AssertData->LineNumber; - return TRUE; - } - return FALSE; -} - - -/** - Extracts DEBUG() information from a status code structure. - - Converts the status code specified by Data to the DEBUG() arguments specified - by ErrorLevel, Marker, and Format. If type GUID in Data is - EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and - Format from the optional data area of the status code buffer specified by Data. - The optional data area of Data contains a 32-bit ErrorLevel followed by Marker - which is 12 UINTN parameters, followed by a Null-terminated ASCII string for - the Format. If the DEBUG() information could be extracted from Data, then - return TRUE. Otherwise, FALSE is returned. - - If Data is NULL, then ASSERT(). - If ErrorLevel is NULL, then ASSERT(). - If Marker is NULL, then ASSERT(). - If Format is NULL, then ASSERT(). - - @param Data Pointer to status code data buffer. - @param ErrorLevel Pointer to error level mask for a debug message. - @param Marker Pointer to the variable argument list associated with Format. - @param Format Pointer to a Null-terminated ASCII format string of a - debug message. - - @retval TRUE The status code specified by Data was converted DEBUG() arguments - specified by ErrorLevel, Marker, and Format. - @retval FALSE The status code specified by Data could not be converted to - DEBUG() arguments. - -**/ -BOOLEAN -EFIAPI -ReportStatusCodeExtractDebugInfo ( - IN CONST EFI_STATUS_CODE_DATA *Data, - OUT UINT32 *ErrorLevel, - OUT BASE_LIST *Marker, - OUT CHAR8 **Format - ) -{ - EFI_DEBUG_INFO *DebugInfo; - - ASSERT (Data != NULL); - ASSERT (ErrorLevel != NULL); - ASSERT (Marker != NULL); - ASSERT (Format != NULL); - - // - // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE - // - if (!CompareGuid (&Data->Type, &gEfiStatusCodeDataTypeDebugGuid)) { - return FALSE; - } - - // - // Retrieve the debug information from the status code record - // - DebugInfo = (EFI_DEBUG_INFO *)(Data + 1); - - *ErrorLevel = DebugInfo->ErrorLevel; - - // - // The first 12 * UINTN bytes of the string are really an - // argument stack to support varargs on the Format string. - // - *Marker = (BASE_LIST) (DebugInfo + 1); - *Format = (CHAR8 *)(((UINT64 *)*Marker) + 12); - - return TRUE; -} - - - - -EFI_STATUS -EFIAPI -SerialReportStatusCode ( - IN EFI_STATUS_CODE_TYPE CodeType, - IN EFI_STATUS_CODE_VALUE Value, - IN UINT32 Instance, - IN CONST EFI_GUID *CallerId, - IN CONST EFI_STATUS_CODE_DATA *Data OPTIONAL - ) -{ - CHAR8 *Filename; - CHAR8 *Description; - CHAR8 *Format; - CHAR8 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE]; - UINT32 ErrorLevel; - UINT32 LineNumber; - UINTN CharCount; - BASE_LIST Marker; - EFI_DEBUG_INFO *DebugInfo; - - Buffer[0] = '\0'; - - - if (Data != NULL && - ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) { - - // - // Print ASSERT() information into output buffer. - // - CharCount = AsciiSPrint ( - Buffer, - EFI_STATUS_CODE_DATA_MAX_SIZE, - "\n\rASSERT!: %a (%d): %a\n\r", - Filename, - LineNumber, - Description - ); - - - // - // Callout to standard output. - // - SerialPortWrite ((UINT8 *)Buffer, CharCount); - return EFI_SUCCESS; - - } else if (Data != NULL && - ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) { - - // - // Print DEBUG() information into output buffer. - // - CharCount = AsciiBSPrint ( - Buffer, - EFI_STATUS_CODE_DATA_MAX_SIZE, - Format, - Marker - ); - - } else if (Data != NULL && - CompareGuid (&Data->Type, &gEfiStatusCodeSpecificDataGuid) && - (CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE) { - - // - // Print specific data into output buffer. - // - DebugInfo = (EFI_DEBUG_INFO *) (Data + 1); - Marker = (BASE_LIST) (DebugInfo + 1); - Format = (CHAR8 *) (((UINT64 *) (DebugInfo + 1)) + 12); - - CharCount = AsciiBSPrint (Buffer, EFI_STATUS_CODE_DATA_MAX_SIZE, Format, Marker); - - } else if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) { - // - // Print ERROR information into output buffer. - // - - CharCount = AsciiSPrint ( - Buffer, - EFI_STATUS_CODE_DATA_MAX_SIZE, - "ERROR: C%x:V%x I%x", - CodeType, - Value, - Instance - ); - - // - // Make sure we don't try to print values that weren't intended to be printed, especially NULL GUID pointers. - // - if (CallerId != NULL) { - CharCount += AsciiSPrint ( - &Buffer[CharCount - 1], - (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)), - " %g", - CallerId - ); - } - - if (Data != NULL) { - CharCount += AsciiSPrint ( - &Buffer[CharCount - 1], - (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)), - " %x", - Data - ); - - } - - - CharCount += AsciiSPrint ( - &Buffer[CharCount - 1], - (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)), - "\n\r" - ); - - } else if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) { - CharCount = AsciiSPrint ( - Buffer, - EFI_STATUS_CODE_DATA_MAX_SIZE, - "PROGRESS CODE: V%x I%x\n\r", - Value, - Instance - ); - } else { - CharCount = AsciiSPrint ( - Buffer, - EFI_STATUS_CODE_DATA_MAX_SIZE, - "Undefined: C%x:V%x I%x\n\r", - CodeType, - Value, - Instance - ); - - } - - SerialPortWrite ((UINT8 *)Buffer, CharCount); - return EFI_SUCCESS; - -} - - -VOID -EFIAPI -AddDxeCoreReportStatusCodeCallback ( - VOID - ) -{ - BuildGuidDataHob (&gEfiStatusCodeRuntimeProtocolGuid, &gStatusCode, sizeof(VOID *)); -} - +/** @file
+ Library that helps implement monolithic PEI
+
+ Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
+
+ 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 <PrePi.h>
+#include <Library/ReportStatusCodeLib.h>
+#include <Library/SerialPortLib.h>
+#include <Library/PrintLib.h>
+
+#include <Protocol/StatusCode.h>
+#include <Guid/StatusCodeDataTypeId.h>
+#include <Guid/StatusCodeDataTypeDebug.h>
+#include <FrameworkPei.h>
+
+#define EFI_STATUS_CODE_DATA_MAX_SIZE 200
+
+EFI_STATUS
+EFIAPI
+SerialReportStatusCode (
+ IN EFI_STATUS_CODE_TYPE CodeType,
+ IN EFI_STATUS_CODE_VALUE Value,
+ IN UINT32 Instance,
+ IN CONST EFI_GUID *CallerId,
+ IN CONST EFI_STATUS_CODE_DATA *Data OPTIONAL
+ );
+
+
+EFI_STATUS_CODE_PROTOCOL gStatusCode = {
+ (EFI_REPORT_STATUS_CODE)SerialReportStatusCode
+};
+
+/**
+ Extracts ASSERT() information from a status code structure.
+
+ Converts the status code specified by CodeType, Value, and Data to the ASSERT()
+ arguments specified by Filename, Description, and LineNumber. If CodeType is
+ an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
+ Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
+ Filename, Description, and LineNumber from the optional data area of the
+ status code buffer specified by Data. The optional data area of Data contains
+ a Null-terminated ASCII string for the FileName, followed by a Null-terminated
+ ASCII string for the Description, followed by a 32-bit LineNumber. If the
+ ASSERT() information could be extracted from Data, then return TRUE.
+ Otherwise, FALSE is returned.
+
+ If Data is NULL, then ASSERT().
+ If Filename is NULL, then ASSERT().
+ If Description is NULL, then ASSERT().
+ If LineNumber is NULL, then ASSERT().
+
+ @param CodeType The type of status code being converted.
+ @param Value The status code value being converted.
+ @param Data Pointer to status code data buffer.
+ @param Filename Pointer to the source file name that generated the ASSERT().
+ @param Description Pointer to the description of the ASSERT().
+ @param LineNumber Pointer to source line number that generated the ASSERT().
+
+ @retval TRUE The status code specified by CodeType, Value, and Data was
+ converted ASSERT() arguments specified by Filename, Description,
+ and LineNumber.
+ @retval FALSE The status code specified by CodeType, Value, and Data could
+ not be converted to ASSERT() arguments.
+
+**/
+BOOLEAN
+EFIAPI
+ReportStatusCodeExtractAssertInfo (
+ IN EFI_STATUS_CODE_TYPE CodeType,
+ IN EFI_STATUS_CODE_VALUE Value,
+ IN CONST EFI_STATUS_CODE_DATA *Data,
+ OUT CHAR8 **Filename,
+ OUT CHAR8 **Description,
+ OUT UINT32 *LineNumber
+ )
+{
+ EFI_DEBUG_ASSERT_DATA *AssertData;
+
+ ASSERT (Data != NULL);
+ ASSERT (Filename != NULL);
+ ASSERT (Description != NULL);
+ ASSERT (LineNumber != NULL);
+
+ if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) &&
+ ((CodeType & EFI_STATUS_CODE_SEVERITY_MASK) == EFI_ERROR_UNRECOVERED) &&
+ ((Value & EFI_STATUS_CODE_OPERATION_MASK) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)) {
+ AssertData = (EFI_DEBUG_ASSERT_DATA *)(Data + 1);
+ *Filename = (CHAR8 *)(AssertData + 1);
+ *Description = *Filename + AsciiStrLen (*Filename) + 1;
+ *LineNumber = AssertData->LineNumber;
+ return TRUE;
+ }
+ return FALSE;
+}
+
+
+/**
+ Extracts DEBUG() information from a status code structure.
+
+ Converts the status code specified by Data to the DEBUG() arguments specified
+ by ErrorLevel, Marker, and Format. If type GUID in Data is
+ EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
+ Format from the optional data area of the status code buffer specified by Data.
+ The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
+ which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
+ the Format. If the DEBUG() information could be extracted from Data, then
+ return TRUE. Otherwise, FALSE is returned.
+
+ If Data is NULL, then ASSERT().
+ If ErrorLevel is NULL, then ASSERT().
+ If Marker is NULL, then ASSERT().
+ If Format is NULL, then ASSERT().
+
+ @param Data Pointer to status code data buffer.
+ @param ErrorLevel Pointer to error level mask for a debug message.
+ @param Marker Pointer to the variable argument list associated with Format.
+ @param Format Pointer to a Null-terminated ASCII format string of a
+ debug message.
+
+ @retval TRUE The status code specified by Data was converted DEBUG() arguments
+ specified by ErrorLevel, Marker, and Format.
+ @retval FALSE The status code specified by Data could not be converted to
+ DEBUG() arguments.
+
+**/
+BOOLEAN
+EFIAPI
+ReportStatusCodeExtractDebugInfo (
+ IN CONST EFI_STATUS_CODE_DATA *Data,
+ OUT UINT32 *ErrorLevel,
+ OUT BASE_LIST *Marker,
+ OUT CHAR8 **Format
+ )
+{
+ EFI_DEBUG_INFO *DebugInfo;
+
+ ASSERT (Data != NULL);
+ ASSERT (ErrorLevel != NULL);
+ ASSERT (Marker != NULL);
+ ASSERT (Format != NULL);
+
+ //
+ // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
+ //
+ if (!CompareGuid (&Data->Type, &gEfiStatusCodeDataTypeDebugGuid)) {
+ return FALSE;
+ }
+
+ //
+ // Retrieve the debug information from the status code record
+ //
+ DebugInfo = (EFI_DEBUG_INFO *)(Data + 1);
+
+ *ErrorLevel = DebugInfo->ErrorLevel;
+
+ //
+ // The first 12 * UINTN bytes of the string are really an
+ // argument stack to support varargs on the Format string.
+ //
+ *Marker = (BASE_LIST) (DebugInfo + 1);
+ *Format = (CHAR8 *)(((UINT64 *)*Marker) + 12);
+
+ return TRUE;
+}
+
+
+
+
+EFI_STATUS
+EFIAPI
+SerialReportStatusCode (
+ IN EFI_STATUS_CODE_TYPE CodeType,
+ IN EFI_STATUS_CODE_VALUE Value,
+ IN UINT32 Instance,
+ IN CONST EFI_GUID *CallerId,
+ IN CONST EFI_STATUS_CODE_DATA *Data OPTIONAL
+ )
+{
+ CHAR8 *Filename;
+ CHAR8 *Description;
+ CHAR8 *Format;
+ CHAR8 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE];
+ UINT32 ErrorLevel;
+ UINT32 LineNumber;
+ UINTN CharCount;
+ BASE_LIST Marker;
+ EFI_DEBUG_INFO *DebugInfo;
+
+ Buffer[0] = '\0';
+
+
+ if (Data != NULL &&
+ ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) {
+
+ //
+ // Print ASSERT() information into output buffer.
+ //
+ CharCount = AsciiSPrint (
+ Buffer,
+ EFI_STATUS_CODE_DATA_MAX_SIZE,
+ "\n\rASSERT!: %a (%d): %a\n\r",
+ Filename,
+ LineNumber,
+ Description
+ );
+
+
+ //
+ // Callout to standard output.
+ //
+ SerialPortWrite ((UINT8 *)Buffer, CharCount);
+ return EFI_SUCCESS;
+
+ } else if (Data != NULL &&
+ ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) {
+
+ //
+ // Print DEBUG() information into output buffer.
+ //
+ CharCount = AsciiBSPrint (
+ Buffer,
+ EFI_STATUS_CODE_DATA_MAX_SIZE,
+ Format,
+ Marker
+ );
+
+ } else if (Data != NULL &&
+ CompareGuid (&Data->Type, &gEfiStatusCodeSpecificDataGuid) &&
+ (CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE) {
+
+ //
+ // Print specific data into output buffer.
+ //
+ DebugInfo = (EFI_DEBUG_INFO *) (Data + 1);
+ Marker = (BASE_LIST) (DebugInfo + 1);
+ Format = (CHAR8 *) (((UINT64 *) (DebugInfo + 1)) + 12);
+
+ CharCount = AsciiBSPrint (Buffer, EFI_STATUS_CODE_DATA_MAX_SIZE, Format, Marker);
+
+ } else if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) {
+ //
+ // Print ERROR information into output buffer.
+ //
+
+ CharCount = AsciiSPrint (
+ Buffer,
+ EFI_STATUS_CODE_DATA_MAX_SIZE,
+ "ERROR: C%x:V%x I%x",
+ CodeType,
+ Value,
+ Instance
+ );
+
+ //
+ // Make sure we don't try to print values that weren't intended to be printed, especially NULL GUID pointers.
+ //
+ if (CallerId != NULL) {
+ CharCount += AsciiSPrint (
+ &Buffer[CharCount - 1],
+ (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)),
+ " %g",
+ CallerId
+ );
+ }
+
+ if (Data != NULL) {
+ CharCount += AsciiSPrint (
+ &Buffer[CharCount - 1],
+ (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)),
+ " %x",
+ Data
+ );
+
+ }
+
+
+ CharCount += AsciiSPrint (
+ &Buffer[CharCount - 1],
+ (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)),
+ "\n\r"
+ );
+
+ } else if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) {
+ CharCount = AsciiSPrint (
+ Buffer,
+ EFI_STATUS_CODE_DATA_MAX_SIZE,
+ "PROGRESS CODE: V%x I%x\n\r",
+ Value,
+ Instance
+ );
+ } else {
+ CharCount = AsciiSPrint (
+ Buffer,
+ EFI_STATUS_CODE_DATA_MAX_SIZE,
+ "Undefined: C%x:V%x I%x\n\r",
+ CodeType,
+ Value,
+ Instance
+ );
+
+ }
+
+ SerialPortWrite ((UINT8 *)Buffer, CharCount);
+ return EFI_SUCCESS;
+
+}
+
+
+VOID
+EFIAPI
+AddDxeCoreReportStatusCodeCallback (
+ VOID
+ )
+{
+ BuildGuidDataHob (&gEfiStatusCodeRuntimeProtocolGuid, &gStatusCode, sizeof(VOID *));
+}
+
diff --git a/EmbeddedPkg/Library/TemplateRealTimeClockLib/TemplateRealTimeClockLib.inf b/EmbeddedPkg/Library/TemplateRealTimeClockLib/TemplateRealTimeClockLib.inf index b88d9abf5c..ce0d13257c 100644 --- a/EmbeddedPkg/Library/TemplateRealTimeClockLib/TemplateRealTimeClockLib.inf +++ b/EmbeddedPkg/Library/TemplateRealTimeClockLib/TemplateRealTimeClockLib.inf @@ -34,4 +34,4 @@ [LibraryClasses]
IoLib
DebugLib
-
\ No newline at end of file +
\ No newline at end of file diff --git a/EmbeddedPkg/Library/TemplateResetSystemLib/TemplateResetSystemLib.inf b/EmbeddedPkg/Library/TemplateResetSystemLib/TemplateResetSystemLib.inf index 446c56b39d..375cb49421 100644 --- a/EmbeddedPkg/Library/TemplateResetSystemLib/TemplateResetSystemLib.inf +++ b/EmbeddedPkg/Library/TemplateResetSystemLib/TemplateResetSystemLib.inf @@ -32,4 +32,4 @@ [LibraryClasses]
IoLib
- DebugLib
\ No newline at end of file + DebugLib
\ No newline at end of file |