#include <Library/DebugLib.h>\r
#include <Library/MemoryAllocationLib.h>\r
#include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/PiLib.h>\r
+#include <Library/DxePiLib.h>\r
#include <Protocol/FirmwareVolume2.h>\r
#include <Protocol/LoadedImage.h>\r
\r
\r
/**\r
- Internal function which read the image specified by Firmware File GUID name and \r
- the Firmware Section tyep from a specified Firmware Volume \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
- @param Fv The Firmware Volume Protocol instance.\r
- @param NameGuid The GUID name of a Firmware File.\r
- @param SectionType The Firmware Section type.\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_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
+ @retval EFI_HANDLE The device handle from which the Image is loaded from.\r
\r
**/\r
-STATIC\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 Instance. \r
+ \r
+ The search order for the section specified by SectionType within a Firmware File is defined by\r
+ EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection (). Please check Section 2.4 of Volume 3: Platform Initialization \r
+ Shared Architectural Elements for detailes.\r
+ \r
+ If SectionType is EFI_SECTION_TE, EFI_SECTION_TE will be used as Firmware Section type to read Firmware Section \r
+ data from the Firmware File. If no such section exists, EFI_SECTION_PE32 will be used as Firmware Section type to \r
+ read Firmware Section data from the Firmware File. If no such section specified is found to match , \r
+ EFI_NOT_FOUND 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 Instance 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
+\r
+\r
EFI_STATUS\r
-InternalGetImageFromFv (\r
- IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
- IN CONST EFI_GUID *NameGuid,\r
- IN EFI_SECTION_TYPE SectionType,\r
- OUT VOID **Buffer,\r
- OUT UINTN *Size\r
+GetSectionFromFv (\r
+ IN EFI_HANDLE FvHandle,\r
+ IN CONST EFI_GUID *NameGuid,\r
+ IN EFI_SECTION_TYPE SectionType,\r
+ IN UINTN Instance,\r
+ OUT VOID **Buffer,\r
+ OUT UINTN *Size\r
)\r
{\r
- EFI_STATUS Status;\r
- EFI_FV_FILETYPE FileType;\r
- EFI_FV_FILE_ATTRIBUTES Attributes;\r
- UINT32 AuthenticationStatus;\r
+ EFI_STATUS Status;\r
+ EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
+ UINT32 AuthenticationStatus;\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
if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r
//\r
- // Try reading PE32 section, since the TE section does not exist\r
+ // Try reading PE32 section, if the required section is TE type \r
//\r
*Buffer = NULL;\r
*Size = 0;\r
);\r
}\r
\r
- if (EFI_ERROR (Status) && \r
- ((SectionType == EFI_SECTION_TE) || (SectionType == EFI_SECTION_PE32))) {\r
- //\r
- // Try reading raw file, since the desired section does not exist\r
- //\r
- *Buffer = NULL;\r
- *Size = 0;\r
- Status = Fv->ReadFile (\r
- Fv,\r
- NameGuid,\r
- Buffer,\r
- Size,\r
- &FileType,\r
- &Attributes,\r
- &AuthenticationStatus\r
- );\r
- }\r
-\r
- return Status;\r
-}\r
-\r
-/**\r
- Allocate and fill a buffer with an image identified by a Firmware File GUID name and a Firmware Section type. \r
- The Firmware Volumes to search for the Firmware File can be specified to be either all Firmware Volumes \r
- in the system, or the Firmware Volume which contains the Firmware File specified by an image handle.\r
-\r
- If ImageHandle is NULL, all Firmware Volumes in the system will be searched. If ImageHandle is not NULL, \r
- ImageHandle is interpreted as EFI_PEI_FILE_HANDLE for the implementation of this function for PEI phase. \r
- The input parameter ImageHandle is interpreted as EFI_HANDLE, on which an EFI_LOADED_IMAGE_PROTOCOL \r
- is installed, for the implementation of this function for DXE phase. The search always starts from the FV \r
- identified by ImageHandle. If WithinImageFv is TRUE, search will only be performed on the first FV. If WithinImageFv \r
- is FALSE, search will continue on other FVs if it fails on the first FV. The search order of Firmware Volumes is \r
- deterministic but arbitrary if no new firmware volume is added into the system between each search. \r
- \r
- The search order for the section type specified by SectionType in the Firmware File is using a depth-first \r
- and left-to-right algorithm through all sections. The first section found to match SectionType will be returned. \r
- \r
- If SectionType is EFI_SECTION_PE32, EFI_SECTION_PE32 will be used as Firmware Section type \r
- to read Firmware Section data from the Firmware File. If no such section exists, the function will try \r
- to read a Firmware File named with NameGuid. If no such file exists, EFI_NOT_FOUND is returned.\r
- \r
- If SectionType is EFI_SECTION_TE, EFI_SECTION_TE will be used as Firmware Section type to read Firmware Section \r
- data from the Firmware File. If no such section exists, EFI_SECTION_PE32 will be used as Firmware Section type to \r
- read Firmware Section data from the Firmware File. If no such section exists, the function will try to read a Firmware \r
- File named with NameGuid. If no such file exists, EFI_NOT_FOUND 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 ImageHandle is NULL and WithinImage is TRUE, 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 NameGuid The GUID name of a Firmware File.\r
- @param SectionType The Firmware Section type.\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_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
-\r
-EFI_STATUS\r
-EFIAPI\r
-GetSectionFromFvFile (\r
- IN CONST VOID *ImageHandle,\r
- IN CONST EFI_GUID *NameGuid,\r
- IN EFI_SECTION_TYPE SectionType,\r
- OUT VOID **Buffer,\r
- OUT UINTN *Size,\r
- IN BOOLEAN WithinImageFv\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_HANDLE *HandleBuffer;\r
- UINTN HandleCount;\r
- UINTN Index;\r
- EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
- EFI_FIRMWARE_VOLUME2_PROTOCOL *ImageFv;\r
- EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
-\r
- ASSERT (NameGuid != NULL);\r
- ASSERT (Buffer != NULL);\r
- ASSERT (Size != NULL);\r
- ASSERT (!(ImageHandle == NULL && WithinImageFv));\r
-\r
- Status = EFI_NOT_FOUND;\r
- ImageFv = NULL;\r
- if (ImageHandle != NULL) {\r
- Status = gBS->HandleProtocol (\r
- (EFI_HANDLE *) ImageHandle,\r
- &gEfiLoadedImageProtocolGuid,\r
- (VOID **) &LoadedImage\r
- );\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
- Status = gBS->HandleProtocol (\r
- LoadedImage->DeviceHandle,\r
- &gEfiFirmwareVolume2ProtocolGuid,\r
- (VOID **) &ImageFv\r
- );\r
- if (!EFI_ERROR (Status)) {\r
- Status = InternalGetImageFromFv (ImageFv, NameGuid, SectionType, Buffer, Size);\r
- }\r
- }\r
-\r
- if (Status == EFI_SUCCESS || WithinImageFv) {\r
- return Status;\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
- //\r
- // Find desired image in all Fvs\r
- //\r
- for (Index = 0; Index < HandleCount; ++Index) {\r
- Status = gBS->HandleProtocol (\r
- HandleBuffer[Index],\r
- &gEfiFirmwareVolume2ProtocolGuid,\r
- (VOID**)&Fv\r
- );\r
-\r
- if (EFI_ERROR (Status)) {\r
- goto Done;\r
- }\r
-\r
- if (ImageFv != NULL && Fv == ImageFv) {\r
- continue;\r
- }\r
-\r
- Status = InternalGetImageFromFv (Fv, NameGuid, SectionType, Buffer, Size);\r
-\r
- if (!EFI_ERROR (Status)) {\r
- goto Done;\r
- }\r
- }\r
-\r
- //\r
- // Not found image\r
- //\r
- if (Index == HandleCount) {\r
- Status = EFI_NOT_FOUND;\r
- }\r
-\r
-Done:\r
-\r
- if (HandleBuffer != NULL) { \r
- FreePool(HandleBuffer);\r
- }\r
-\r
return Status;\r
}\r
\r
-EFI_HANDLE\r
-EFIAPI\r
-ImageHandleToFvHandle (\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
EFI_STATUS\r
EFIAPI\r
-GetSectionFromAnyFv (\r
+PiLibGetSectionFromAnyFv (\r
IN CONST EFI_GUID *NameGuid,\r
IN EFI_SECTION_TYPE SectionType,\r
IN UINTN Instance,\r
// so that this implementation of GetSectionFromAnyFv\r
// will locate the FFS faster.\r
//\r
- FvHandle = ImageHandleToFvHandle (gImageHandle);\r
+ FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r
Status = GetSectionFromFv (\r
FvHandle,\r
NameGuid,\r
\r
}\r
\r
-EFI_STATUS\r
-EFIAPI\r
-GetSectionFromFv (\r
- IN EFI_HANDLE FvHandle,\r
- IN CONST EFI_GUID *NameGuid,\r
- IN EFI_SECTION_TYPE SectionType,\r
- IN UINTN Instance,\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 (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
- 0,\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
- 0,\r
- Buffer,\r
- Size,\r
- &AuthenticationStatus\r
- );\r
- }\r
-\r
- return Status;\r
-}\r
-\r
\r
EFI_STATUS\r
EFIAPI\r
-GetSectionFromCurrentFv (\r
+PiLibGetSectionFromCurrentFv (\r
IN CONST EFI_GUID *NameGuid,\r
IN EFI_SECTION_TYPE SectionType,\r
IN UINTN Instance,\r
)\r
{\r
return GetSectionFromFv(\r
- ImageHandleToFvHandle(gImageHandle),\r
+ InternalImageHandleToFvHandle(gImageHandle),\r
NameGuid,\r
SectionType,\r
Instance,\r
\r
EFI_STATUS\r
EFIAPI\r
-GetSectionFromCurrentFfs (\r
+PiLibGetSectionFromCurrentFfs (\r
IN EFI_SECTION_TYPE SectionType,\r
IN UINTN Instance,\r
OUT VOID **Buffer,\r
)\r
{\r
return GetSectionFromFv(\r
- ImageHandleToFvHandle(gImageHandle),\r
+ InternalImageHandleToFvHandle(gImageHandle),\r
&gEfiCallerIdGuid,\r
SectionType,\r
Instance,\r