4 Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <UniversalPayload/UniversalPayload.h>
11 #include <UniversalPayload/ExtraData.h>
13 #include <Ppi/LoadFile.h>
15 #include <Library/DebugLib.h>
16 #include <Library/HobLib.h>
17 #include <Library/PeiServicesLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/BaseMemoryLib.h>
24 The wrapper function of PeiLoadImageLoadImage().
26 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.
27 @param FileHandle - Pointer to the FFS file header of the image.
28 @param ImageAddressArg - Pointer to PE/TE image.
29 @param ImageSizeArg - Size of PE/TE image.
30 @param EntryPoint - Pointer to entry point of specified image file for output.
31 @param AuthenticationState - Pointer to attestation authentication state of image.
33 @return Status of PeiLoadImageLoadImage().
38 PeiLoadFileLoadPayload (
39 IN CONST EFI_PEI_LOAD_FILE_PPI
*This
,
40 IN EFI_PEI_FILE_HANDLE FileHandle
,
41 OUT EFI_PHYSICAL_ADDRESS
*ImageAddressArg OPTIONAL
,
42 OUT UINT64
*ImageSizeArg OPTIONAL
,
43 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
,
44 OUT UINT32
*AuthenticationState
49 UNIVERSAL_PAYLOAD_EXTRA_DATA
*ExtraData
;
50 ELF_IMAGE_CONTEXT Context
;
51 UNIVERSAL_PAYLOAD_INFO_HEADER
*PldInfo
;
53 UINT16 ExtraDataIndex
;
57 UINT32 ExtraDataCount
;
62 // ELF is added to file as RAW section for EDKII bootloader.
63 // But RAW section might be added by build tool before the ELF RAW section when alignment is specified for ELF RAW section.
64 // Below loop skips the RAW section that doesn't contain valid ELF image.
68 Status
= PeiServicesFfsFindSectionData3 (EFI_SECTION_RAW
, Instance
++, FileHandle
, &Elf
, AuthenticationState
);
69 if (EFI_ERROR (Status
)) {
73 ZeroMem (&Context
, sizeof (Context
));
74 Status
= ParseElfImage (Elf
, &Context
);
75 } while (EFI_ERROR (Status
));
79 "Payload File Size: 0x%08X, Mem Size: 0x%08x, Reload: %d\n",
82 Context
.ReloadRequired
86 // Get UNIVERSAL_PAYLOAD_INFO_HEADER and number of additional PLD sections.
90 for (Index
= 0; Index
< Context
.ShNum
; Index
++) {
91 Status
= GetElfSectionName (&Context
, Index
, &SectionName
);
92 if (EFI_ERROR (Status
)) {
96 DEBUG ((DEBUG_INFO
, "Payload Section[%d]: %a\n", Index
, SectionName
));
97 if (AsciiStrCmp (SectionName
, UNIVERSAL_PAYLOAD_INFO_SEC_NAME
) == 0) {
98 Status
= GetElfSectionPos (&Context
, Index
, &Offset
, &Size
);
99 if (!EFI_ERROR (Status
)) {
100 PldInfo
= (UNIVERSAL_PAYLOAD_INFO_HEADER
*)(Context
.FileBase
+ Offset
);
102 } else if (AsciiStrnCmp (SectionName
, UNIVERSAL_PAYLOAD_EXTRA_SEC_NAME_PREFIX
, UNIVERSAL_PAYLOAD_EXTRA_SEC_NAME_PREFIX_LENGTH
) == 0) {
103 Status
= GetElfSectionPos (&Context
, Index
, &Offset
, &Size
);
104 if (!EFI_ERROR (Status
)) {
111 // Report the additional PLD sections through HOB.
113 Length
= sizeof (UNIVERSAL_PAYLOAD_EXTRA_DATA
) + ExtraDataCount
* sizeof (UNIVERSAL_PAYLOAD_EXTRA_DATA_ENTRY
);
114 ExtraData
= BuildGuidHob (
115 &gUniversalPayloadExtraDataGuid
,
118 ExtraData
->Count
= ExtraDataCount
;
119 ExtraData
->Header
.Revision
= UNIVERSAL_PAYLOAD_EXTRA_DATA_REVISION
;
120 ExtraData
->Header
.Length
= (UINT16
)Length
;
121 if (ExtraDataCount
!= 0) {
122 for (ExtraDataIndex
= 0, Index
= 0; Index
< Context
.ShNum
; Index
++) {
123 Status
= GetElfSectionName (&Context
, Index
, &SectionName
);
124 if (EFI_ERROR (Status
)) {
128 if (AsciiStrnCmp (SectionName
, UNIVERSAL_PAYLOAD_EXTRA_SEC_NAME_PREFIX
, UNIVERSAL_PAYLOAD_EXTRA_SEC_NAME_PREFIX_LENGTH
) == 0) {
129 Status
= GetElfSectionPos (&Context
, Index
, &Offset
, &Size
);
130 if (!EFI_ERROR (Status
)) {
131 ASSERT (ExtraDataIndex
< ExtraDataCount
);
133 ExtraData
->Entry
[ExtraDataIndex
].Identifier
,
134 sizeof (ExtraData
->Entry
[ExtraDataIndex
].Identifier
),
135 SectionName
+ UNIVERSAL_PAYLOAD_EXTRA_SEC_NAME_PREFIX_LENGTH
137 ExtraData
->Entry
[ExtraDataIndex
].Base
= (UINTN
)(Context
.FileBase
+ Offset
);
138 ExtraData
->Entry
[ExtraDataIndex
].Size
= Size
;
145 if (Context
.ReloadRequired
|| (Context
.PreferredImageAddress
!= Context
.FileBase
)) {
146 Context
.ImageAddress
= AllocatePages (EFI_SIZE_TO_PAGES (Context
.ImageSize
));
148 Context
.ImageAddress
= Context
.FileBase
;
152 // Load ELF into the required base
154 Status
= LoadElfImage (&Context
);
155 if (!EFI_ERROR (Status
)) {
156 *ImageAddressArg
= (UINTN
)Context
.ImageAddress
;
157 *EntryPoint
= Context
.EntryPoint
;
158 *ImageSizeArg
= Context
.ImageSize
;
164 EFI_PEI_LOAD_FILE_PPI mPeiLoadFilePpi
= {
165 PeiLoadFileLoadPayload
168 EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList
= {
169 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
170 &gEfiPeiLoadFilePpiGuid
,
176 Install Pei Load File PPI.
178 @param FileHandle Handle of the file being invoked.
179 @param PeiServices Describes the list of possible PEI Services.
181 @retval EFI_SUCESS The entry point executes successfully.
182 @retval Others Some error occurs during the execution of this function.
187 InitializePayloadLoaderPeim (
188 IN EFI_PEI_FILE_HANDLE FileHandle
,
189 IN CONST EFI_PEI_SERVICES
**PeiServices
194 Status
= PeiServicesInstallPpi (&gPpiLoadFilePpiList
);