]> git.proxmox.com Git - mirror_edk2.git/blob - UefiPayloadPkg/PayloadLoaderPeim/PayloadLoaderPeim.c
c619470dbbf03b1eb9f473ac9cd1f5b340e3b55f
[mirror_edk2.git] / UefiPayloadPkg / PayloadLoaderPeim / PayloadLoaderPeim.c
1 /** @file
2 ELF Load Image Support
3
4 Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiPei.h>
10 #include <UniversalPayload/UniversalPayload.h>
11 #include <UniversalPayload/ExtraData.h>
12
13 #include <Ppi/LoadFile.h>
14
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>
20
21 #include "ElfLib.h"
22
23 /**
24 The wrapper function of PeiLoadImageLoadImage().
25
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.
32
33 @return Status of PeiLoadImageLoadImage().
34
35 **/
36 EFI_STATUS
37 EFIAPI
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
45 )
46 {
47 EFI_STATUS Status;
48 VOID *Elf;
49 UNIVERSAL_PAYLOAD_EXTRA_DATA *ExtraData;
50 ELF_IMAGE_CONTEXT Context;
51 UNIVERSAL_PAYLOAD_INFO_HEADER *PldInfo;
52 UINT32 Index;
53 UINT16 ExtraDataIndex;
54 CHAR8 *SectionName;
55 UINTN Offset;
56 UINTN Size;
57 UINT32 ExtraDataCount;
58 UINTN Instance;
59
60 //
61 // ELF is added to file as RAW section for EDKII bootloader.
62 // But RAW section might be added by build tool before the ELF RAW section when alignment is specified for ELF RAW section.
63 // Below loop skips the RAW section that doesn't contain valid ELF image.
64 //
65 Instance = 0;
66 do {
67 Status = PeiServicesFfsFindSectionData3 (EFI_SECTION_RAW, Instance++, FileHandle, &Elf, AuthenticationState);
68 if (EFI_ERROR (Status)) {
69 return Status;
70 }
71
72 ZeroMem (&Context, sizeof (Context));
73 Status = ParseElfImage (Elf, &Context);
74 } while (EFI_ERROR (Status));
75
76 DEBUG ((
77 DEBUG_INFO, "Payload File Size: 0x%08X, Mem Size: 0x%08x, Reload: %d\n",
78 Context.FileSize, Context.ImageSize, Context.ReloadRequired
79 ));
80
81 //
82 // Get UNIVERSAL_PAYLOAD_INFO_HEADER and number of additional PLD sections.
83 //
84 PldInfo = NULL;
85 ExtraDataCount = 0;
86 for (Index = 0; Index < Context.ShNum; Index++) {
87 Status = GetElfSectionName (&Context, Index, &SectionName);
88 if (EFI_ERROR(Status)) {
89 continue;
90 }
91 DEBUG ((DEBUG_INFO, "Payload Section[%d]: %a\n", Index, SectionName));
92 if (AsciiStrCmp(SectionName, UNIVERSAL_PAYLOAD_INFO_SEC_NAME) == 0) {
93 Status = GetElfSectionPos (&Context, Index, &Offset, &Size);
94 if (!EFI_ERROR(Status)) {
95 PldInfo = (UNIVERSAL_PAYLOAD_INFO_HEADER *)(Context.FileBase + Offset);
96 }
97 } else if (AsciiStrnCmp(SectionName, UNIVERSAL_PAYLOAD_EXTRA_SEC_NAME_PREFIX, UNIVERSAL_PAYLOAD_EXTRA_SEC_NAME_PREFIX_LENGTH) == 0) {
98 Status = GetElfSectionPos (&Context, Index, &Offset, &Size);
99 if (!EFI_ERROR (Status)) {
100 ExtraDataCount++;
101 }
102 }
103 }
104
105 //
106 // Report the additional PLD sections through HOB.
107 //
108 ExtraData = BuildGuidHob (
109 &gUniversalPayloadExtraDataGuid,
110 sizeof (UNIVERSAL_PAYLOAD_EXTRA_DATA) + ExtraDataCount * sizeof (UNIVERSAL_PAYLOAD_EXTRA_DATA_ENTRY)
111 );
112 ExtraData->Count = ExtraDataCount;
113 if (ExtraDataCount != 0) {
114 for (ExtraDataIndex = 0, Index = 0; Index < Context.ShNum; Index++) {
115 Status = GetElfSectionName (&Context, Index, &SectionName);
116 if (EFI_ERROR(Status)) {
117 continue;
118 }
119 if (AsciiStrnCmp(SectionName, UNIVERSAL_PAYLOAD_EXTRA_SEC_NAME_PREFIX, UNIVERSAL_PAYLOAD_EXTRA_SEC_NAME_PREFIX_LENGTH) == 0) {
120 Status = GetElfSectionPos (&Context, Index, &Offset, &Size);
121 if (!EFI_ERROR (Status)) {
122 ASSERT (ExtraDataIndex < ExtraDataCount);
123 AsciiStrCpyS (
124 ExtraData->Entry[ExtraDataIndex].Identifier,
125 sizeof(ExtraData->Entry[ExtraDataIndex].Identifier),
126 SectionName + UNIVERSAL_PAYLOAD_EXTRA_SEC_NAME_PREFIX_LENGTH
127 );
128 ExtraData->Entry[ExtraDataIndex].Base = (UINTN)(Context.FileBase + Offset);
129 ExtraData->Entry[ExtraDataIndex].Size = Size;
130 ExtraDataIndex++;
131 }
132 }
133 }
134 }
135
136 if (Context.ReloadRequired || Context.PreferredImageAddress != Context.FileBase) {
137 Context.ImageAddress = AllocatePages (EFI_SIZE_TO_PAGES (Context.ImageSize));
138 } else {
139 Context.ImageAddress = Context.FileBase;
140 }
141
142 //
143 // Load ELF into the required base
144 //
145 Status = LoadElfImage (&Context);
146 if (!EFI_ERROR(Status)) {
147 *ImageAddressArg = (UINTN) Context.ImageAddress;
148 *EntryPoint = Context.EntryPoint;
149 *ImageSizeArg = Context.ImageSize;
150 }
151 return Status;
152 }
153
154
155 EFI_PEI_LOAD_FILE_PPI mPeiLoadFilePpi = {
156 PeiLoadFileLoadPayload
157 };
158
159
160 EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList = {
161 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
162 &gEfiPeiLoadFilePpiGuid,
163 &mPeiLoadFilePpi
164 };
165 /**
166
167 Install Pei Load File PPI.
168
169 @param FileHandle Handle of the file being invoked.
170 @param PeiServices Describes the list of possible PEI Services.
171
172 @retval EFI_SUCESS The entry point executes successfully.
173 @retval Others Some error occurs during the execution of this function.
174
175 **/
176 EFI_STATUS
177 EFIAPI
178 InitializePayloadLoaderPeim (
179 IN EFI_PEI_FILE_HANDLE FileHandle,
180 IN CONST EFI_PEI_SERVICES **PeiServices
181 )
182 {
183 EFI_STATUS Status;
184 Status = PeiServicesInstallPpi (&gPpiLoadFilePpiList);
185
186 return Status;
187 }