-/** @file\r
- Mde PI library functions.\r
-\r
- Copyright (c) 2007 - 2008, Intel Corporation<BR>\r
- All rights reserved. This program and the accompanying materials\r
- are licensed and made available under the terms and conditions of the BSD License\r
- which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php\r
-\r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include <PiDxe.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/DxePiLib.h>\r
-#include <Protocol/FirmwareVolume2.h>\r
-#include <Protocol/LoadedImage.h>\r
-\r
-\r
-/**\r
- Identify the device handle from which the Image is loaded from. As this device handle is passed to\r
- GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL \r
- protocol instance should be located succesfully by calling gBS->HandleProtocol ().\r
-\r
- This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed\r
- on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.\r
- \r
- If ImageHandle is NULL, then ASSERT ();\r
- If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();\r
- \r
- @param ImageHandle The firmware allocated handle for UEFI image.\r
-\r
- @retval EFI_HANDLE The device handle from which the Image is loaded from.\r
-\r
-**/\r
-EFI_HANDLE\r
-InternalImageHandleToFvHandle (\r
- EFI_HANDLE ImageHandle\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
- \r
- ASSERT (ImageHandle != NULL);\r
-\r
- Status = gBS->HandleProtocol (\r
- (EFI_HANDLE *) ImageHandle,\r
- &gEfiLoadedImageProtocolGuid,\r
- (VOID **) &LoadedImage\r
- );\r
-\r
- ASSERT_EFI_ERROR (Status);\r
-\r
- return LoadedImage->DeviceHandle;\r
-\r
-}\r
-\r
-/**\r
- Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware \r
- Section type and instance number from the specified Firmware Volume.\r
- \r
- This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to \r
- carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed \r
- by NameGuid, SectionType and SectionInstance. \r
- \r
- The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
- found in PI Specification.\r
- \r
- If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
- is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND \r
- is returned.\r
- \r
- The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
- by this function. This function can be only called at TPL_NOTIFY and below.\r
- \r
- If FvHandle is NULL, then ASSERT ();\r
- If NameGuid is NULL, then ASSERT();\r
- If Buffer is NULL, then ASSERT();\r
- If Size is NULL, then ASSERT().\r
-\r
- @param FvHandle The device handle that contains a instance of EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r
- @param NameGuid The GUID name of a Firmware File.\r
- @param SectionType The Firmware Section type.\r
- @param SectionInstance The instance number of Firmware Section to read from starting from 0.\r
- @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.\r
- @param Size On output, the size of Buffer.\r
- \r
- @retval EFI_SUCCESS The image is found and data and size is returned.\r
- @retval EFI_UNSUPPORTED FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.\r
- @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r
- @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
- @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
- @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r
- \r
-**/\r
-EFI_STATUS\r
-GetSectionFromFv (\r
- IN EFI_HANDLE FvHandle,\r
- IN CONST EFI_GUID *NameGuid,\r
- IN EFI_SECTION_TYPE SectionType,\r
- IN UINTN SectionInstance,\r
- OUT VOID **Buffer,\r
- OUT UINTN *Size\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
- UINT32 AuthenticationStatus;\r
-\r
- ASSERT (NameGuid != NULL);\r
- ASSERT (Buffer != NULL);\r
- ASSERT (Size != NULL);\r
- \r
- ASSERT (FvHandle != NULL);\r
-\r
- Status = gBS->HandleProtocol (\r
- FvHandle,\r
- &gEfiFirmwareVolume2ProtocolGuid,\r
- (VOID **) &Fv\r
- );\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
-\r
- //\r
- // Read desired section content in NameGuid file\r
- //\r
- *Buffer = NULL;\r
- *Size = 0;\r
- Status = Fv->ReadSection (\r
- Fv,\r
- NameGuid,\r
- SectionType,\r
- SectionInstance,\r
- Buffer,\r
- Size,\r
- &AuthenticationStatus\r
- );\r
-\r
- if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r
- //\r
- // Try reading PE32 section, if the required section is TE type \r
- //\r
- *Buffer = NULL;\r
- *Size = 0;\r
- Status = Fv->ReadSection (\r
- Fv,\r
- NameGuid,\r
- EFI_SECTION_PE32,\r
- SectionInstance,\r
- Buffer,\r
- Size,\r
- &AuthenticationStatus\r
- );\r
- }\r
-\r
- return Status;\r
-}\r
-\r
-\r
-\r
-/**\r
- Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r
-\r
- PiLibGetSectionFromAnyFv () is used to read a specific section from a file within a firmware volume. The function\r
- will search the first file with the specified name in all firmware volumes in the system. The search order for firmware \r
- volumes in the system is determistic but abitrary if no new firmware volume is added into the system between \r
- each calls of this function. \r
-\r
- After the specific file is located, the function searches the specifc firmware section with type SectionType in this file. \r
- The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
- found in PI Specification.\r
-\r
- If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
- is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND \r
- is returned.\r
-\r
- The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
- by this function. This function can only be called at TPL_NOTIFY and below.\r
-\r
- If NameGuid is NULL, then ASSERT();\r
- If Buffer is NULL, then ASSERT();\r
- If Size is NULL, then ASSERT().\r
-\r
- @param NameGuid Pointer to an EFI_GUID, which indicates the file name from which the requested \r
- section will be read. Type EFI_GUID is defined in \r
- InstallProtocolInterface() in the UEFI 2.0 specification. \r
- @param SectionType Indicates the section type to return. SectionType in conjunction with \r
- SectionInstance indicates which section to return. Type \r
- EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r
- @param SectionInstance Indicates which instance of sections with a type of SectionType to return. \r
- SectionType in conjunction with SectionInstance indicates which section to \r
- return. SectionInstance is zero based.\r
- @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not \r
- including the section header. Caller is responsible to free this memory.\r
- @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r
- *Buffer.\r
-\r
- @retval EFI_SUCCESS The image is found and data and size is returned.\r
- @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r
- @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
- @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
- @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PiLibGetSectionFromAnyFv (\r
- IN CONST EFI_GUID *NameGuid,\r
- IN EFI_SECTION_TYPE SectionType,\r
- IN UINTN SectionInstance,\r
- OUT VOID **Buffer,\r
- OUT UINTN *Size\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_HANDLE *HandleBuffer;\r
- UINTN HandleCount;\r
- UINTN Index;\r
- EFI_HANDLE FvHandle;\r
-\r
- //\r
- // Search the FV that contain the caller's FFS first.\r
- // FV builder can choose to build FFS into the this FV\r
- // so that this implementation of GetSectionFromAnyFv\r
- // will locate the FFS faster.\r
- //\r
- FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r
- Status = GetSectionFromFv (\r
- FvHandle,\r
- NameGuid,\r
- SectionType,\r
- SectionInstance,\r
- Buffer,\r
- Size\r
- );\r
- if (!EFI_ERROR (Status)) {\r
- return EFI_SUCCESS;\r
- }\r
-\r
- HandleBuffer = NULL;\r
- Status = gBS->LocateHandleBuffer (\r
- ByProtocol,\r
- &gEfiFirmwareVolume2ProtocolGuid,\r
- NULL,\r
- &HandleCount,\r
- &HandleBuffer\r
- );\r
- if (EFI_ERROR (Status)) {\r
- goto Done;\r
- }\r
-\r
- for (Index = 0; Index < HandleCount; Index++) {\r
- //\r
- // Skip the FV that contain the caller's FFS\r
- //\r
- if (HandleBuffer[Index] != FvHandle) {\r
- Status = GetSectionFromFv (\r
- HandleBuffer[Index], \r
- NameGuid, \r
- SectionType, \r
- SectionInstance,\r
- Buffer, \r
- Size\r
- );\r
-\r
- if (!EFI_ERROR (Status)) {\r
- goto Done;\r
- }\r
- }\r
-\r
- }\r
-\r
- if (Index == HandleCount) {\r
- Status = EFI_NOT_FOUND;\r
- }\r
-\r
-Done:\r
- \r
- if (HandleBuffer != NULL) { \r
- FreePool(HandleBuffer);\r
- }\r
- return Status;\r
- \r
-}\r
-\r
-/**\r
- Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r
-\r
- PiLibGetSectionFromCurrentFv () is used to read a specific section from a file within the same firmware volume from which\r
- the running image is loaded. If the specific file is found, the function searches the specifc firmware section with type SectionType. \r
- The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
- found in PI Specification.\r
-\r
- If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
- is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND \r
- is returned.\r
-\r
- The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
- by this function. This function can be only called at TPL_NOTIFY and below.\r
-\r
- If NameGuid is NULL, then ASSERT();\r
- If Buffer is NULL, then ASSERT();\r
- If Size is NULL, then ASSERT().\r
-\r
- @param NameGuid Pointer to an EFI_GUID, which indicates the file name from which the requested \r
- section will be read. Type EFI_GUID is defined in \r
- InstallProtocolInterface() in the UEFI 2.0 specification. \r
- @param SectionType Indicates the section type to return. SectionType in conjunction with \r
- SectionInstance indicates which section to return. Type \r
- EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r
- @param SectionInstance Indicates which instance of sections with a type of SectionType to return. \r
- SectionType in conjunction with SectionInstance indicates which section to \r
- return. SectionInstance is zero based.\r
- @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not \r
- including the section header. Caller is responsible to free this memory.\r
- @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r
- *Buffer.\r
-\r
-\r
- @retval EFI_SUCCESS The image is found and data and size is returned.\r
- @retval EFI_UNSUPPORTED FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.\r
- @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r
- @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
- @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
- @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r
- \r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PiLibGetSectionFromCurrentFv (\r
- IN CONST EFI_GUID *NameGuid,\r
- IN EFI_SECTION_TYPE SectionType,\r
- IN UINTN SectionInstance,\r
- OUT VOID **Buffer,\r
- OUT UINTN *Size\r
- )\r
-{\r
- return GetSectionFromFv(\r
- InternalImageHandleToFvHandle(gImageHandle),\r
- NameGuid,\r
- SectionType,\r
- SectionInstance,\r
- Buffer,\r
- Size\r
- );\r
-}\r
-\r
-\r
-/**\r
- Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r
-\r
- PiLibGetSectionFromCurrentFfs () searches the specifc firmware section with type SectionType in the same firmware file from\r
- which the running image is loaded. The details of this search order is defined in description of \r
- EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () found in PI Specification.\r
-\r
- If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
- is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND \r
- is returned.\r
-\r
-\r
- The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
- by this function. This function can only be called at TPL_NOTIFY and below.\r
-\r
- If Buffer is NULL, then ASSERT();\r
- If Size is NULL, then ASSERT().\r
-\r
- @param SectionType Indicates the section type to return. SectionType in conjunction with \r
- SectionInstance indicates which section to return. Type \r
- EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r
- @param SectionInstance Indicates which instance of sections with a type of SectionType to return. \r
- SectionType in conjunction with SectionInstance indicates which section to \r
- return. SectionInstance is zero based.\r
- @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not \r
- including the section header. Caller is responsible to free this memory.\r
- @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r
- *Buffer.\r
-\r
- @retval EFI_SUCCESS The image is found and data and size is returned.\r
- @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r
- @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
- @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
- @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r
- \r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PiLibGetSectionFromCurrentFfs (\r
- IN EFI_SECTION_TYPE SectionType,\r
- IN UINTN SectionInstance,\r
- OUT VOID **Buffer,\r
- OUT UINTN *Size\r
- )\r
-{\r
- return GetSectionFromFv(\r
- InternalImageHandleToFvHandle(gImageHandle),\r
- &gEfiCallerIdGuid,\r
- SectionType,\r
- SectionInstance,\r
- Buffer,\r
- Size\r
- );\r
-}\r
-\r