]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Sec/FindPeiCore.c
OVMF: Update OVMF FD/FV build to minimize ROM size
[mirror_edk2.git] / OvmfPkg / Sec / FindPeiCore.c
1 /** @file
2 Locate the entry point for the PEI Core
3
4 Copyright (c) 2008 - 2010, Intel Corporation
5
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <PiPei.h>
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/ExtractGuidedSectionLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/PeCoffGetEntryPointLib.h>
23
24 #include "SecMain.h"
25
26
27 /**
28 Locates the main boot firmware volume.
29
30 @param[in,out] BootFv On input, the base of the BootFv
31 On output, the decompressed main firmware volume
32
33 @retval EFI_SUCCESS The main firmware volume was located and decompressed
34 @retval EFI_NOT_FOUND The main firmware volume was not found
35
36 **/
37 EFI_STATUS
38 FindMainFv (
39 IN OUT EFI_FIRMWARE_VOLUME_HEADER **BootFv
40 )
41 {
42 EFI_FIRMWARE_VOLUME_HEADER *Fv;
43 UINTN Distance;
44 BOOLEAN Found;
45
46 ASSERT (((UINTN) *BootFv & EFI_PAGE_MASK) == 0);
47
48 Found = FALSE;
49 Fv = *BootFv;
50 Distance = (UINTN) (*BootFv)->FvLength;
51 do {
52 Fv = (EFI_FIRMWARE_VOLUME_HEADER*) ((UINT8*) Fv - EFI_PAGE_SIZE);
53 Distance += EFI_PAGE_SIZE;
54 if (Distance > SIZE_32MB) {
55 return EFI_NOT_FOUND;
56 }
57
58 if (Fv->Signature != EFI_FVH_SIGNATURE) {
59 continue;
60 }
61
62 if ((UINTN) Fv->FvLength > Distance) {
63 continue;
64 }
65
66 *BootFv = Fv;
67 return EFI_SUCCESS;
68
69 } while (TRUE);
70 }
71
72
73 /**
74 Locates a section within a series of sections
75 with the specified section type.
76
77 @param[in] Sections The sections to search
78 @param[in] SizeOfSections Total size of all sections
79 @param[in] SectionType The section type to locate
80 @param[out] FoundSection The FFS section if found
81
82 @retval EFI_SUCCESS The file and section was found
83 @retval EFI_NOT_FOUND The file and section was not found
84 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
85
86 **/
87 EFI_STATUS
88 FindFfsSectionInSections (
89 IN VOID *Sections,
90 IN UINTN SizeOfSections,
91 IN EFI_SECTION_TYPE SectionType,
92 OUT EFI_COMMON_SECTION_HEADER **FoundSection
93 )
94 {
95 EFI_PHYSICAL_ADDRESS CurrentAddress;
96 UINT32 Size;
97 EFI_PHYSICAL_ADDRESS EndOfSections;
98 EFI_COMMON_SECTION_HEADER *Section;
99 EFI_PHYSICAL_ADDRESS EndOfSection;
100
101 //
102 // Loop through the FFS file sections within the PEI Core FFS file
103 //
104 EndOfSection = (EFI_PHYSICAL_ADDRESS)(UINTN) Sections;
105 EndOfSections = EndOfSection + SizeOfSections;
106 for (;;) {
107 if (EndOfSection == EndOfSections) {
108 break;
109 }
110 CurrentAddress = (EndOfSection + 3) & ~(3ULL);
111 if (CurrentAddress >= EndOfSections) {
112 return EFI_VOLUME_CORRUPTED;
113 }
114
115 Section = (EFI_COMMON_SECTION_HEADER*)(UINTN) CurrentAddress;
116 DEBUG ((EFI_D_INFO, "Section->Type: 0x%x\n", Section->Type));
117
118 Size = SECTION_SIZE (Section);
119 if (Size < sizeof (*Section)) {
120 return EFI_VOLUME_CORRUPTED;
121 }
122
123 EndOfSection = CurrentAddress + Size;
124 if (EndOfSection > EndOfSections) {
125 return EFI_VOLUME_CORRUPTED;
126 }
127
128 //
129 // Look for the requested section type
130 //
131 if (Section->Type == SectionType) {
132 *FoundSection = Section;
133 return EFI_SUCCESS;
134 }
135 DEBUG ((EFI_D_INFO, "Section->Type (0x%x) != SectionType (0x%x)\n", Section->Type, SectionType));
136 }
137
138 return EFI_NOT_FOUND;
139 }
140
141
142 /**
143 Locates a FFS file with the specified file type and a section
144 within that file with the specified section type.
145
146 @param[in] Fv The firmware volume to search
147 @param[in] FileType The file type to locate
148 @param[in] SectionType The section type to locate
149 @param[out] FoundSection The FFS section if found
150
151 @retval EFI_SUCCESS The file and section was found
152 @retval EFI_NOT_FOUND The file and section was not found
153 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
154
155 **/
156 EFI_STATUS
157 EFIAPI
158 FindFfsFileAndSection (
159 IN EFI_FIRMWARE_VOLUME_HEADER *Fv,
160 IN EFI_FV_FILETYPE FileType,
161 IN EFI_SECTION_TYPE SectionType,
162 OUT EFI_COMMON_SECTION_HEADER **FoundSection
163 )
164 {
165 EFI_STATUS Status;
166 EFI_PHYSICAL_ADDRESS CurrentAddress;
167 EFI_PHYSICAL_ADDRESS EndOfFirmwareVolume;
168 EFI_FFS_FILE_HEADER *File;
169 UINT32 Size;
170 EFI_PHYSICAL_ADDRESS EndOfFile;
171
172 if (Fv->Signature != EFI_FVH_SIGNATURE) {
173 DEBUG ((EFI_D_INFO, "FV at %p does not have FV header signature\n", Fv));
174 return EFI_VOLUME_CORRUPTED;
175 }
176
177 CurrentAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Fv;
178 EndOfFirmwareVolume = CurrentAddress + Fv->FvLength;
179
180 //
181 // Loop through the FFS files in the Boot Firmware Volume
182 //
183 for (EndOfFile = CurrentAddress + Fv->HeaderLength; ; ) {
184
185 CurrentAddress = (EndOfFile + 7) & ~(7ULL);
186 if (CurrentAddress > EndOfFirmwareVolume) {
187 return EFI_VOLUME_CORRUPTED;
188 }
189
190 File = (EFI_FFS_FILE_HEADER*)(UINTN) CurrentAddress;
191 Size = *(UINT32*) File->Size & 0xffffff;
192 if (Size < (sizeof (*File) + sizeof (EFI_COMMON_SECTION_HEADER))) {
193 return EFI_VOLUME_CORRUPTED;
194 }
195 DEBUG ((EFI_D_INFO, "File->Type: 0x%x\n", File->Type));
196
197 EndOfFile = CurrentAddress + Size;
198 if (EndOfFile > EndOfFirmwareVolume) {
199 return EFI_VOLUME_CORRUPTED;
200 }
201
202 //
203 // Look for the request file type
204 //
205 if (File->Type != FileType) {
206 DEBUG ((EFI_D_INFO, "File->Type (0x%x) != FileType (0x%x)\n", File->Type, FileType));
207 continue;
208 }
209
210 Status = FindFfsSectionInSections (
211 (VOID*) (File + 1),
212 (UINTN) EndOfFile - (UINTN) (File + 1),
213 SectionType,
214 FoundSection
215 );
216 if (!EFI_ERROR (Status) || (Status == EFI_VOLUME_CORRUPTED)) {
217 return Status;
218 }
219 }
220
221 return EFI_NOT_FOUND;
222 }
223
224
225 /**
226 Locates the compressed main firmware volume and decompresses it.
227
228 @param[in,out] Fv On input, the firmware volume to search
229 On output, the decompressed main FV
230
231 @retval EFI_SUCCESS The file and section was found
232 @retval EFI_NOT_FOUND The file and section was not found
233 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
234
235 **/
236 EFI_STATUS
237 EFIAPI
238 DecompressGuidedFv (
239 IN OUT EFI_FIRMWARE_VOLUME_HEADER **Fv
240 )
241 {
242 EFI_STATUS Status;
243 EFI_GUID_DEFINED_SECTION *Section;
244 UINT32 OutputBufferSize;
245 UINT32 ScratchBufferSize;
246 UINT16 SectionAttribute;
247 UINT32 AuthenticationStatus;
248 VOID *OutputBuffer;
249 VOID *ScratchBuffer;
250 EFI_FIRMWARE_VOLUME_IMAGE_SECTION *NewFvSection;
251 EFI_FIRMWARE_VOLUME_HEADER *NewFv;
252
253 NewFvSection = (EFI_FIRMWARE_VOLUME_IMAGE_SECTION*) NULL;
254
255 Status = FindFfsFileAndSection (
256 *Fv,
257 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,
258 EFI_SECTION_GUID_DEFINED,
259 (EFI_COMMON_SECTION_HEADER**) &Section
260 );
261 if (EFI_ERROR (Status)) {
262 DEBUG ((EFI_D_ERROR, "Unable to find GUID defined section\n"));
263 return Status;
264 }
265
266 Status = ExtractGuidedSectionGetInfo (
267 Section,
268 &OutputBufferSize,
269 &ScratchBufferSize,
270 &SectionAttribute
271 );
272 if (EFI_ERROR (Status)) {
273 DEBUG ((EFI_D_ERROR, "Unable to GetInfo for GUIDed section\n"));
274 return Status;
275 }
276
277 //PcdGet32 (PcdOvmfMemFvBase), PcdGet32 (PcdOvmfMemFvSize)
278 OutputBuffer = (VOID*) ((UINT8*)(UINTN) PcdGet32 (PcdOvmfMemFvBase) + SIZE_1MB);
279 ScratchBuffer = ALIGN_POINTER ((UINT8*) OutputBuffer + OutputBufferSize, SIZE_1MB);
280 Status = ExtractGuidedSectionDecode (
281 Section,
282 &OutputBuffer,
283 ScratchBuffer,
284 &AuthenticationStatus
285 );
286 if (EFI_ERROR (Status)) {
287 DEBUG ((EFI_D_ERROR, "Error during GUID section decode\n"));
288 return Status;
289 }
290
291 Status = FindFfsSectionInSections (
292 OutputBuffer,
293 OutputBufferSize,
294 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,
295 (EFI_COMMON_SECTION_HEADER**) &NewFvSection
296 );
297 if (EFI_ERROR (Status)) {
298 DEBUG ((EFI_D_ERROR, "Unable to find FV image in extracted data\n"));
299 return Status;
300 }
301
302 NewFv = (EFI_FIRMWARE_VOLUME_HEADER*)(UINTN) PcdGet32 (PcdOvmfMemFvBase);
303 CopyMem (NewFv, (VOID*) (NewFvSection + 1), PcdGet32 (PcdOvmfMemFvSize));
304
305 if (NewFv->Signature != EFI_FVH_SIGNATURE) {
306 DEBUG ((EFI_D_ERROR, "Extracted FV at %p does not have FV header signature\n", NewFv));
307 CpuDeadLoop ();
308 return EFI_VOLUME_CORRUPTED;
309 }
310
311 *Fv = NewFv;
312 return EFI_SUCCESS;
313 }
314
315
316 /**
317 Locates the PEI Core entry point address
318
319 @param[in] Fv The firmware volume to search
320 @param[out] PeiCoreEntryPoint The entry point of the PEI Core image
321
322 @retval EFI_SUCCESS The file and section was found
323 @retval EFI_NOT_FOUND The file and section was not found
324 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
325
326 **/
327 EFI_STATUS
328 EFIAPI
329 FindPeiCoreEntryPointInFv (
330 IN EFI_FIRMWARE_VOLUME_HEADER *Fv,
331 OUT VOID **PeiCoreEntryPoint
332 )
333 {
334 EFI_STATUS Status;
335 EFI_COMMON_SECTION_HEADER *Section;
336
337 Status = FindFfsFileAndSection (
338 Fv,
339 EFI_FV_FILETYPE_PEI_CORE,
340 EFI_SECTION_PE32,
341 &Section
342 );
343 if (EFI_ERROR (Status)) {
344 Status = FindFfsFileAndSection (
345 Fv,
346 EFI_FV_FILETYPE_PEI_CORE,
347 EFI_SECTION_TE,
348 &Section
349 );
350 if (EFI_ERROR (Status)) {
351 DEBUG ((EFI_D_ERROR, "Unable to find PEI Core image\n"));
352 return Status;
353 }
354 }
355
356 return PeCoffLoaderGetEntryPoint ((VOID*) (Section + 1), PeiCoreEntryPoint);
357 }
358
359
360 /**
361 Locates the PEI Core entry point address
362
363 @param[in,out] Fv The firmware volume to search
364 @param[out] PeiCoreEntryPoint The entry point of the PEI Core image
365
366 @retval EFI_SUCCESS The file and section was found
367 @retval EFI_NOT_FOUND The file and section was not found
368 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted
369
370 **/
371 VOID
372 EFIAPI
373 FindPeiCoreEntryPoint (
374 IN OUT EFI_FIRMWARE_VOLUME_HEADER **BootFv,
375 OUT VOID **PeiCoreEntryPoint
376 )
377 {
378 *PeiCoreEntryPoint = NULL;
379
380 FindMainFv (BootFv);
381
382 DecompressGuidedFv (BootFv);
383
384 FindPeiCoreEntryPointInFv (*BootFv, PeiCoreEntryPoint);
385 }
386