]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/FwVol/FwVol.c
MdeModulePkg PeiCore: Fix typos
[mirror_edk2.git] / MdeModulePkg / Core / Pei / FwVol / FwVol.c
... / ...
CommitLineData
1/** @file\r
2 Pei Core Firmware File System service routines.\r
3\r
4Copyright (c) 2015 HP Development Company, L.P.\r
5Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
6SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#include "FwVol.h"\r
11\r
12EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList[] = {\r
13 {\r
14 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,\r
15 &gEfiPeiFirmwareVolumeInfoPpiGuid,\r
16 FirmwareVolumeInfoPpiNotifyCallback\r
17 },\r
18 {\r
19 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
20 &gEfiPeiFirmwareVolumeInfo2PpiGuid,\r
21 FirmwareVolumeInfoPpiNotifyCallback\r
22 }\r
23};\r
24\r
25PEI_FW_VOL_INSTANCE mPeiFfs2FwVol = {\r
26 PEI_FW_VOL_SIGNATURE,\r
27 FALSE,\r
28 {\r
29 PeiFfsFvPpiProcessVolume,\r
30 PeiFfsFvPpiFindFileByType,\r
31 PeiFfsFvPpiFindFileByName,\r
32 PeiFfsFvPpiGetFileInfo,\r
33 PeiFfsFvPpiGetVolumeInfo,\r
34 PeiFfsFvPpiFindSectionByType,\r
35 PeiFfsFvPpiGetFileInfo2,\r
36 PeiFfsFvPpiFindSectionByType2,\r
37 EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE,\r
38 EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION\r
39 }\r
40};\r
41\r
42PEI_FW_VOL_INSTANCE mPeiFfs3FwVol = {\r
43 PEI_FW_VOL_SIGNATURE,\r
44 TRUE,\r
45 {\r
46 PeiFfsFvPpiProcessVolume,\r
47 PeiFfsFvPpiFindFileByType,\r
48 PeiFfsFvPpiFindFileByName,\r
49 PeiFfsFvPpiGetFileInfo,\r
50 PeiFfsFvPpiGetVolumeInfo,\r
51 PeiFfsFvPpiFindSectionByType,\r
52 PeiFfsFvPpiGetFileInfo2,\r
53 PeiFfsFvPpiFindSectionByType2,\r
54 EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE,\r
55 EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION\r
56 }\r
57};\r
58\r
59EFI_PEI_PPI_DESCRIPTOR mPeiFfs2FvPpiList = {\r
60 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
61 &gEfiFirmwareFileSystem2Guid,\r
62 &mPeiFfs2FwVol.Fv\r
63};\r
64\r
65EFI_PEI_PPI_DESCRIPTOR mPeiFfs3FvPpiList = {\r
66 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
67 &gEfiFirmwareFileSystem3Guid,\r
68 &mPeiFfs3FwVol.Fv\r
69};\r
70\r
71/**\r
72Required Alignment Alignment Value in FFS FFS_ATTRIB_DATA_ALIGNMENT2 Alignment Value in\r
73(bytes) Attributes Field in FFS Attributes Field Firmware Volume Interfaces\r
741 0 0 0\r
7516 1 0 4\r
76128 2 0 7\r
77512 3 0 9\r
781 KB 4 0 10\r
794 KB 5 0 12\r
8032 KB 6 0 15\r
8164 KB 7 0 16\r
82128 KB 0 1 17\r
83256 KB 1 1 18\r
84512 KB 2 1 19\r
851 MB 3 1 20\r
862 MB 4 1 21\r
874 MB 5 1 22\r
888 MB 6 1 23\r
8916 MB 7 1 24\r
90**/\r
91UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16};\r
92UINT8 mFvAttributes2[] = {17, 18, 19, 20, 21, 22, 23, 24};\r
93\r
94/**\r
95 Convert the FFS File Attributes to FV File Attributes\r
96\r
97 @param FfsAttributes The attributes of UINT8 type.\r
98\r
99 @return The attributes of EFI_FV_FILE_ATTRIBUTES\r
100\r
101**/\r
102EFI_FV_FILE_ATTRIBUTES\r
103FfsAttributes2FvFileAttributes (\r
104 IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes\r
105 )\r
106{\r
107 UINT8 DataAlignment;\r
108 EFI_FV_FILE_ATTRIBUTES FileAttribute;\r
109\r
110 DataAlignment = (UINT8) ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);\r
111 ASSERT (DataAlignment < 8);\r
112\r
113 if ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT_2) != 0) {\r
114 FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes2[DataAlignment];\r
115 } else {\r
116 FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[DataAlignment];\r
117 }\r
118\r
119 if ((FfsAttributes & FFS_ATTRIB_FIXED) == FFS_ATTRIB_FIXED) {\r
120 FileAttribute |= EFI_FV_FILE_ATTRIB_FIXED;\r
121 }\r
122\r
123 return FileAttribute;\r
124}\r
125\r
126/**\r
127 Returns the file state set by the highest zero bit in the State field\r
128\r
129 @param ErasePolarity Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY\r
130 in the Attributes field.\r
131 @param FfsHeader Pointer to FFS File Header.\r
132\r
133 @retval EFI_FFS_FILE_STATE File state is set by the highest none zero bit\r
134 in the header State field.\r
135**/\r
136EFI_FFS_FILE_STATE\r
137GetFileState(\r
138 IN UINT8 ErasePolarity,\r
139 IN EFI_FFS_FILE_HEADER *FfsHeader\r
140 )\r
141{\r
142 EFI_FFS_FILE_STATE FileState;\r
143 EFI_FFS_FILE_STATE HighestBit;\r
144\r
145 FileState = FfsHeader->State;\r
146\r
147 if (ErasePolarity != 0) {\r
148 FileState = (EFI_FFS_FILE_STATE)~FileState;\r
149 }\r
150\r
151 //\r
152 // Get file state set by its highest none zero bit.\r
153 //\r
154 HighestBit = 0x80;\r
155 while (HighestBit != 0 && (HighestBit & FileState) == 0) {\r
156 HighestBit >>= 1;\r
157 }\r
158\r
159 return HighestBit;\r
160}\r
161\r
162/**\r
163 Calculates the checksum of the header of a file.\r
164\r
165 @param FileHeader Pointer to FFS File Header.\r
166\r
167 @return Checksum of the header.\r
168 Zero means the header is good.\r
169 Non-zero means the header is bad.\r
170**/\r
171UINT8\r
172CalculateHeaderChecksum (\r
173 IN EFI_FFS_FILE_HEADER *FileHeader\r
174 )\r
175{\r
176 EFI_FFS_FILE_HEADER2 TestFileHeader;\r
177\r
178 if (IS_FFS_FILE2 (FileHeader)) {\r
179 CopyMem (&TestFileHeader, FileHeader, sizeof (EFI_FFS_FILE_HEADER2));\r
180 //\r
181 // Ingore State and File field in FFS header.\r
182 //\r
183 TestFileHeader.State = 0;\r
184 TestFileHeader.IntegrityCheck.Checksum.File = 0;\r
185\r
186 return CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER2));\r
187 } else {\r
188 CopyMem (&TestFileHeader, FileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
189 //\r
190 // Ingore State and File field in FFS header.\r
191 //\r
192 TestFileHeader.State = 0;\r
193 TestFileHeader.IntegrityCheck.Checksum.File = 0;\r
194\r
195 return CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
196 }\r
197}\r
198\r
199/**\r
200 Find FV handler according to FileHandle in that FV.\r
201\r
202 @param FileHandle Handle of file image\r
203\r
204 @return Pointer to instance of PEI_CORE_FV_HANDLE.\r
205**/\r
206PEI_CORE_FV_HANDLE*\r
207FileHandleToVolume (\r
208 IN EFI_PEI_FILE_HANDLE FileHandle\r
209 )\r
210{\r
211 UINTN Index;\r
212 PEI_CORE_INSTANCE *PrivateData;\r
213 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
214 UINTN BestIndex;\r
215\r
216 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
217 BestIndex = PrivateData->FvCount;\r
218\r
219 //\r
220 // Find the best matched FV image that includes this FileHandle.\r
221 // FV may include the child FV, and they are in the same continuous space.\r
222 // If FileHandle is from the child FV, the updated logic can find its matched FV.\r
223 //\r
224 for (Index = 0; Index < PrivateData->FvCount; Index++) {\r
225 FwVolHeader = PrivateData->Fv[Index].FvHeader;\r
226 if (((UINT64) (UINTN) FileHandle > (UINT64) (UINTN) FwVolHeader ) && \\r
227 ((UINT64) (UINTN) FileHandle <= ((UINT64) (UINTN) FwVolHeader + FwVolHeader->FvLength - 1))) {\r
228 if (BestIndex == PrivateData->FvCount) {\r
229 BestIndex = Index;\r
230 } else {\r
231 if ((UINT64) (UINTN) PrivateData->Fv[BestIndex].FvHeader < (UINT64) (UINTN) FwVolHeader) {\r
232 BestIndex = Index;\r
233 }\r
234 }\r
235 }\r
236 }\r
237\r
238 if (BestIndex < PrivateData->FvCount) {\r
239 return &PrivateData->Fv[BestIndex];\r
240 }\r
241\r
242 return NULL;\r
243}\r
244\r
245/**\r
246 Given the input file pointer, search for the first matching file in the\r
247 FFS volume as defined by SearchType. The search starts from FileHeader inside\r
248 the Firmware Volume defined by FwVolHeader.\r
249 If SearchType is EFI_FV_FILETYPE_ALL, the first FFS file will return without check its file type.\r
250 If SearchType is PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE,\r
251 the first PEIM, or COMBINED PEIM or FV file type FFS file will return.\r
252\r
253 @param FvHandle Pointer to the FV header of the volume to search\r
254 @param FileName File name\r
255 @param SearchType Filter to find only files of this type.\r
256 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.\r
257 @param FileHandle This parameter must point to a valid FFS volume.\r
258 @param AprioriFile Pointer to AprioriFile image in this FV if has\r
259\r
260 @return EFI_NOT_FOUND No files matching the search criteria were found\r
261 @retval EFI_SUCCESS Success to search given file\r
262\r
263**/\r
264EFI_STATUS\r
265FindFileEx (\r
266 IN CONST EFI_PEI_FV_HANDLE FvHandle,\r
267 IN CONST EFI_GUID *FileName, OPTIONAL\r
268 IN EFI_FV_FILETYPE SearchType,\r
269 IN OUT EFI_PEI_FILE_HANDLE *FileHandle,\r
270 IN OUT EFI_PEI_FILE_HANDLE *AprioriFile OPTIONAL\r
271 )\r
272{\r
273 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
274 EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExtHeader;\r
275 EFI_FFS_FILE_HEADER **FileHeader;\r
276 EFI_FFS_FILE_HEADER *FfsFileHeader;\r
277 UINT32 FileLength;\r
278 UINT32 FileOccupiedSize;\r
279 UINT32 FileOffset;\r
280 UINT64 FvLength;\r
281 UINT8 ErasePolarity;\r
282 UINT8 FileState;\r
283 UINT8 DataCheckSum;\r
284 BOOLEAN IsFfs3Fv;\r
285\r
286 //\r
287 // Convert the handle of FV to FV header for memory-mapped firmware volume\r
288 //\r
289 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvHandle;\r
290 FileHeader = (EFI_FFS_FILE_HEADER **)FileHandle;\r
291\r
292 IsFfs3Fv = CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem3Guid);\r
293\r
294 FvLength = FwVolHeader->FvLength;\r
295 if ((FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {\r
296 ErasePolarity = 1;\r
297 } else {\r
298 ErasePolarity = 0;\r
299 }\r
300\r
301 //\r
302 // If FileHeader is not specified (NULL) or FileName is not NULL,\r
303 // start with the first file in the firmware volume. Otherwise,\r
304 // start from the FileHeader.\r
305 //\r
306 if ((*FileHeader == NULL) || (FileName != NULL)) {\r
307 if (FwVolHeader->ExtHeaderOffset != 0) {\r
308 //\r
309 // Searching for files starts on an 8 byte aligned boundary after the end of the Extended Header if it exists.\r
310 //\r
311 FwVolExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) ((UINT8 *) FwVolHeader + FwVolHeader->ExtHeaderOffset);\r
312 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FwVolExtHeader + FwVolExtHeader->ExtHeaderSize);\r
313 } else {\r
314 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *) FwVolHeader + FwVolHeader->HeaderLength);\r
315 }\r
316 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ALIGN_POINTER (FfsFileHeader, 8);\r
317 } else {\r
318 if (IS_FFS_FILE2 (*FileHeader)) {\r
319 if (!IsFfs3Fv) {\r
320 DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &(*FileHeader)->Name));\r
321 }\r
322 FileLength = FFS_FILE2_SIZE (*FileHeader);\r
323 ASSERT (FileLength > 0x00FFFFFF);\r
324 } else {\r
325 FileLength = FFS_FILE_SIZE (*FileHeader);\r
326 }\r
327 //\r
328 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.\r
329 //\r
330 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
331 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);\r
332 }\r
333\r
334 FileOffset = (UINT32) ((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);\r
335 ASSERT (FileOffset <= 0xFFFFFFFF);\r
336\r
337 while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {\r
338 //\r
339 // Get FileState which is the highest bit of the State\r
340 //\r
341 FileState = GetFileState (ErasePolarity, FfsFileHeader);\r
342 switch (FileState) {\r
343\r
344 case EFI_FILE_HEADER_CONSTRUCTION:\r
345 case EFI_FILE_HEADER_INVALID:\r
346 if (IS_FFS_FILE2 (FfsFileHeader)) {\r
347 if (!IsFfs3Fv) {\r
348 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));\r
349 }\r
350 FileOffset += sizeof (EFI_FFS_FILE_HEADER2);\r
351 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));\r
352 } else {\r
353 FileOffset += sizeof (EFI_FFS_FILE_HEADER);\r
354 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));\r
355 }\r
356 break;\r
357\r
358 case EFI_FILE_DATA_VALID:\r
359 case EFI_FILE_MARKED_FOR_UPDATE:\r
360 if (CalculateHeaderChecksum (FfsFileHeader) != 0) {\r
361 ASSERT (FALSE);\r
362 *FileHeader = NULL;\r
363 return EFI_NOT_FOUND;\r
364 }\r
365\r
366 if (IS_FFS_FILE2 (FfsFileHeader)) {\r
367 FileLength = FFS_FILE2_SIZE (FfsFileHeader);\r
368 ASSERT (FileLength > 0x00FFFFFF);\r
369 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
370 if (!IsFfs3Fv) {\r
371 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));\r
372 FileOffset += FileOccupiedSize;\r
373 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);\r
374 break;\r
375 }\r
376 } else {\r
377 FileLength = FFS_FILE_SIZE (FfsFileHeader);\r
378 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
379 }\r
380\r
381 DataCheckSum = FFS_FIXED_CHECKSUM;\r
382 if ((FfsFileHeader->Attributes & FFS_ATTRIB_CHECKSUM) == FFS_ATTRIB_CHECKSUM) {\r
383 if (IS_FFS_FILE2 (FfsFileHeader)) {\r
384 DataCheckSum = CalculateCheckSum8 ((CONST UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2), FileLength - sizeof(EFI_FFS_FILE_HEADER2));\r
385 } else {\r
386 DataCheckSum = CalculateCheckSum8 ((CONST UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER), FileLength - sizeof(EFI_FFS_FILE_HEADER));\r
387 }\r
388 }\r
389 if (FfsFileHeader->IntegrityCheck.Checksum.File != DataCheckSum) {\r
390 ASSERT (FALSE);\r
391 *FileHeader = NULL;\r
392 return EFI_NOT_FOUND;\r
393 }\r
394\r
395 if (FileName != NULL) {\r
396 if (CompareGuid (&FfsFileHeader->Name, (EFI_GUID*)FileName)) {\r
397 *FileHeader = FfsFileHeader;\r
398 return EFI_SUCCESS;\r
399 }\r
400 } else if (SearchType == PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE) {\r
401 if ((FfsFileHeader->Type == EFI_FV_FILETYPE_PEIM) ||\r
402 (FfsFileHeader->Type == EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER) ||\r
403 (FfsFileHeader->Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE)) {\r
404\r
405 *FileHeader = FfsFileHeader;\r
406 return EFI_SUCCESS;\r
407 } else if (AprioriFile != NULL) {\r
408 if (FfsFileHeader->Type == EFI_FV_FILETYPE_FREEFORM) {\r
409 if (CompareGuid (&FfsFileHeader->Name, &gPeiAprioriFileNameGuid)) {\r
410 *AprioriFile = (EFI_PEI_FILE_HANDLE)FfsFileHeader;\r
411 }\r
412 }\r
413 }\r
414 } else if (((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) &&\r
415 (FfsFileHeader->Type != EFI_FV_FILETYPE_FFS_PAD)) {\r
416 *FileHeader = FfsFileHeader;\r
417 return EFI_SUCCESS;\r
418 }\r
419\r
420 FileOffset += FileOccupiedSize;\r
421 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);\r
422 break;\r
423\r
424 case EFI_FILE_DELETED:\r
425 if (IS_FFS_FILE2 (FfsFileHeader)) {\r
426 if (!IsFfs3Fv) {\r
427 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));\r
428 }\r
429 FileLength = FFS_FILE2_SIZE (FfsFileHeader);\r
430 ASSERT (FileLength > 0x00FFFFFF);\r
431 } else {\r
432 FileLength = FFS_FILE_SIZE (FfsFileHeader);\r
433 }\r
434 FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);\r
435 FileOffset += FileOccupiedSize;\r
436 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);\r
437 break;\r
438\r
439 default:\r
440 *FileHeader = NULL;\r
441 return EFI_NOT_FOUND;\r
442 }\r
443 }\r
444\r
445 *FileHeader = NULL;\r
446 return EFI_NOT_FOUND;\r
447}\r
448\r
449/**\r
450 Initialize PeiCore FV List.\r
451\r
452 @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r
453 @param SecCoreData - Pointer to EFI_SEC_PEI_HAND_OFF.\r
454**/\r
455VOID\r
456PeiInitializeFv (\r
457 IN PEI_CORE_INSTANCE *PrivateData,\r
458 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData\r
459 )\r
460{\r
461 EFI_STATUS Status;\r
462 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
463 EFI_PEI_FV_HANDLE FvHandle;\r
464 EFI_FIRMWARE_VOLUME_HEADER *BfvHeader;\r
465\r
466 //\r
467 // Install FV_PPI for FFS2 file system.\r
468 //\r
469 PeiServicesInstallPpi (&mPeiFfs2FvPpiList);\r
470\r
471 //\r
472 // Install FV_PPI for FFS3 file system.\r
473 //\r
474 PeiServicesInstallPpi (&mPeiFfs3FvPpiList);\r
475\r
476 BfvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase;\r
477\r
478 //\r
479 // The FV_PPI in BFV's format should be installed.\r
480 //\r
481 Status = PeiServicesLocatePpi (\r
482 &BfvHeader->FileSystemGuid,\r
483 0,\r
484 NULL,\r
485 (VOID**)&FvPpi\r
486 );\r
487 ASSERT_EFI_ERROR (Status);\r
488\r
489 //\r
490 // Get handle of BFV\r
491 //\r
492 Status = FvPpi->ProcessVolume (\r
493 FvPpi,\r
494 SecCoreData->BootFirmwareVolumeBase,\r
495 (UINTN)BfvHeader->FvLength,\r
496 &FvHandle\r
497 );\r
498 ASSERT_EFI_ERROR (Status);\r
499\r
500 PrivateData->Fv = AllocateZeroPool (sizeof (PEI_CORE_FV_HANDLE) * FV_GROWTH_STEP);\r
501 ASSERT (PrivateData->Fv != NULL);\r
502 PrivateData->MaxFvCount = FV_GROWTH_STEP;\r
503\r
504 //\r
505 // Update internal PEI_CORE_FV array.\r
506 //\r
507 PrivateData->Fv[PrivateData->FvCount].FvHeader = BfvHeader;\r
508 PrivateData->Fv[PrivateData->FvCount].FvPpi = FvPpi;\r
509 PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;\r
510 PrivateData->Fv[PrivateData->FvCount].AuthenticationStatus = 0;\r
511 DEBUG ((\r
512 EFI_D_INFO,\r
513 "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n",\r
514 (UINT32) PrivateData->FvCount,\r
515 (VOID *) BfvHeader,\r
516 (UINT32) BfvHeader->FvLength,\r
517 FvHandle\r
518 ));\r
519 PrivateData->FvCount ++;\r
520\r
521 //\r
522 // Post a call-back for the FvInfoPPI and FvInfo2PPI services to expose\r
523 // additional FVs to PeiCore.\r
524 //\r
525 Status = PeiServicesNotifyPpi (mNotifyOnFvInfoList);\r
526 ASSERT_EFI_ERROR (Status);\r
527\r
528}\r
529\r
530/**\r
531 Process Firmware Volume Information once FvInfoPPI or FvInfo2PPI install.\r
532 The FV Info will be registered into PeiCore private data structure.\r
533 And search the inside FV image, if found, the new FV INFO(2) PPI will be installed.\r
534\r
535 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
536 @param NotifyDescriptor Address of the notification descriptor data structure.\r
537 @param Ppi Address of the PPI that was installed.\r
538\r
539 @retval EFI_SUCCESS The FV Info is registered into PeiCore private data structure.\r
540 @return if not EFI_SUCCESS, fail to verify FV.\r
541\r
542**/\r
543EFI_STATUS\r
544EFIAPI\r
545FirmwareVolumeInfoPpiNotifyCallback (\r
546 IN EFI_PEI_SERVICES **PeiServices,\r
547 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
548 IN VOID *Ppi\r
549 )\r
550{\r
551 EFI_PEI_FIRMWARE_VOLUME_INFO2_PPI FvInfo2Ppi;\r
552 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
553 PEI_CORE_INSTANCE *PrivateData;\r
554 EFI_STATUS Status;\r
555 EFI_PEI_FV_HANDLE FvHandle;\r
556 UINTN FvIndex;\r
557 EFI_PEI_FILE_HANDLE FileHandle;\r
558 VOID *DepexData;\r
559 BOOLEAN IsFvInfo2;\r
560 UINTN CurFvCount;\r
561 VOID *TempPtr;\r
562\r
563 Status = EFI_SUCCESS;\r
564 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
565\r
566 if (CompareGuid (NotifyDescriptor->Guid, &gEfiPeiFirmwareVolumeInfo2PpiGuid)) {\r
567 //\r
568 // It is FvInfo2PPI.\r
569 //\r
570 CopyMem (&FvInfo2Ppi, Ppi, sizeof (EFI_PEI_FIRMWARE_VOLUME_INFO2_PPI));\r
571 IsFvInfo2 = TRUE;\r
572 } else {\r
573 //\r
574 // It is FvInfoPPI.\r
575 //\r
576 CopyMem (&FvInfo2Ppi, Ppi, sizeof (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI));\r
577 FvInfo2Ppi.AuthenticationStatus = 0;\r
578 IsFvInfo2 = FALSE;\r
579 }\r
580\r
581 if (CompareGuid (&FvInfo2Ppi.FvFormat, &gEfiFirmwareFileSystem2Guid)) {\r
582 //\r
583 // gEfiFirmwareFileSystem2Guid is specified for FvFormat, then here to check the\r
584 // FileSystemGuid pointed by FvInfo against gEfiFirmwareFileSystem2Guid to make sure\r
585 // FvInfo has the firmware file system 2 format.\r
586 //\r
587 // If the ASSERT really appears, FvFormat needs to be specified correctly, for example,\r
588 // gEfiFirmwareFileSystem3Guid can be used for firmware file system 3 format, or\r
589 // ((EFI_FIRMWARE_VOLUME_HEADER *) FvInfo)->FileSystemGuid can be just used for both\r
590 // firmware file system 2 and 3 format.\r
591 //\r
592 ASSERT (CompareGuid (&(((EFI_FIRMWARE_VOLUME_HEADER *) FvInfo2Ppi.FvInfo)->FileSystemGuid), &gEfiFirmwareFileSystem2Guid));\r
593 }\r
594\r
595 //\r
596 // Locate the corresponding FV_PPI according to founded FV's format GUID\r
597 //\r
598 Status = PeiServicesLocatePpi (\r
599 &FvInfo2Ppi.FvFormat,\r
600 0,\r
601 NULL,\r
602 (VOID**)&FvPpi\r
603 );\r
604 if (!EFI_ERROR (Status)) {\r
605 //\r
606 // Process new found FV and get FV handle.\r
607 //\r
608 Status = FvPpi->ProcessVolume (FvPpi, FvInfo2Ppi.FvInfo, FvInfo2Ppi.FvInfoSize, &FvHandle);\r
609 if (EFI_ERROR (Status)) {\r
610 DEBUG ((EFI_D_ERROR, "Fail to process new found FV, FV may be corrupted!\n"));\r
611 return Status;\r
612 }\r
613\r
614 //\r
615 // Check whether the FV has already been processed.\r
616 //\r
617 for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {\r
618 if (PrivateData->Fv[FvIndex].FvHandle == FvHandle) {\r
619 if (IsFvInfo2 && (FvInfo2Ppi.AuthenticationStatus != PrivateData->Fv[FvIndex].AuthenticationStatus)) {\r
620 PrivateData->Fv[FvIndex].AuthenticationStatus = FvInfo2Ppi.AuthenticationStatus;\r
621 DEBUG ((EFI_D_INFO, "Update AuthenticationStatus of the %dth FV to 0x%x!\n", FvIndex, FvInfo2Ppi.AuthenticationStatus));\r
622 }\r
623 DEBUG ((DEBUG_INFO, "The FV %p has already been processed!\n", FvInfo2Ppi.FvInfo));\r
624 return EFI_SUCCESS;\r
625 }\r
626 }\r
627\r
628 if (PrivateData->FvCount >= PrivateData->MaxFvCount) {\r
629 //\r
630 // Run out of room, grow the buffer.\r
631 //\r
632 TempPtr = AllocateZeroPool (\r
633 sizeof (PEI_CORE_FV_HANDLE) * (PrivateData->MaxFvCount + FV_GROWTH_STEP)\r
634 );\r
635 ASSERT (TempPtr != NULL);\r
636 CopyMem (\r
637 TempPtr,\r
638 PrivateData->Fv,\r
639 sizeof (PEI_CORE_FV_HANDLE) * PrivateData->MaxFvCount\r
640 );\r
641 PrivateData->Fv = TempPtr;\r
642 PrivateData->MaxFvCount = PrivateData->MaxFvCount + FV_GROWTH_STEP;\r
643 }\r
644\r
645 //\r
646 // Update internal PEI_CORE_FV array.\r
647 //\r
648 PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfo2Ppi.FvInfo;\r
649 PrivateData->Fv[PrivateData->FvCount].FvPpi = FvPpi;\r
650 PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;\r
651 PrivateData->Fv[PrivateData->FvCount].AuthenticationStatus = FvInfo2Ppi.AuthenticationStatus;\r
652 CurFvCount = PrivateData->FvCount;\r
653 DEBUG ((\r
654 EFI_D_INFO,\r
655 "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n",\r
656 (UINT32) CurFvCount,\r
657 (VOID *) FvInfo2Ppi.FvInfo,\r
658 FvInfo2Ppi.FvInfoSize,\r
659 FvHandle\r
660 ));\r
661 PrivateData->FvCount ++;\r
662\r
663 //\r
664 // Scan and process the new discovered FV for EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE\r
665 //\r
666 FileHandle = NULL;\r
667 do {\r
668 Status = FvPpi->FindFileByType (\r
669 FvPpi,\r
670 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,\r
671 FvHandle,\r
672 &FileHandle\r
673 );\r
674 if (!EFI_ERROR (Status)) {\r
675 Status = FvPpi->FindSectionByType (\r
676 FvPpi,\r
677 EFI_SECTION_PEI_DEPEX,\r
678 FileHandle,\r
679 (VOID**)&DepexData\r
680 );\r
681 if (!EFI_ERROR (Status)) {\r
682 if (!PeimDispatchReadiness (PeiServices, DepexData)) {\r
683 //\r
684 // Dependency is not satisfied.\r
685 //\r
686 continue;\r
687 }\r
688 }\r
689\r
690 DEBUG ((EFI_D_INFO, "Found firmware volume Image File %p in FV[%d] %p\n", FileHandle, CurFvCount, FvHandle));\r
691 ProcessFvFile (PrivateData, &PrivateData->Fv[CurFvCount], FileHandle);\r
692 }\r
693 } while (FileHandle != NULL);\r
694 } else {\r
695 DEBUG ((EFI_D_ERROR, "Fail to process FV %p because no corresponding EFI_FIRMWARE_VOLUME_PPI is found!\n", FvInfo2Ppi.FvInfo));\r
696\r
697 AddUnknownFormatFvInfo (PrivateData, &FvInfo2Ppi);\r
698 }\r
699\r
700 return EFI_SUCCESS;\r
701}\r
702\r
703/**\r
704 Verify the Guided Section GUID by checking if there is the Guided Section GUID HOB recorded the GUID itself.\r
705\r
706 @param GuidedSectionGuid The Guided Section GUID.\r
707 @param GuidedSectionExtraction A pointer to the pointer to the supported Guided Section Extraction Ppi\r
708 for the Guided Section.\r
709\r
710 @return TRUE The GuidedSectionGuid could be identified, and the pointer to\r
711 the Guided Section Extraction Ppi will be returned to *GuidedSectionExtraction.\r
712 @return FALSE The GuidedSectionGuid could not be identified, or\r
713 the Guided Section Extraction Ppi has not been installed yet.\r
714\r
715**/\r
716BOOLEAN\r
717VerifyGuidedSectionGuid (\r
718 IN EFI_GUID *GuidedSectionGuid,\r
719 OUT EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI **GuidedSectionExtraction\r
720 )\r
721{\r
722 EFI_PEI_HOB_POINTERS Hob;\r
723 EFI_GUID *GuidRecorded;\r
724 VOID *Interface;\r
725 EFI_STATUS Status;\r
726\r
727 //\r
728 // Check if there is the Guided Section GUID HOB recorded the GUID itself.\r
729 //\r
730 Hob.Raw = GetFirstGuidHob (GuidedSectionGuid);\r
731 if (Hob.Raw != NULL) {\r
732 GuidRecorded = (EFI_GUID *) GET_GUID_HOB_DATA (Hob);\r
733 if (CompareGuid (GuidRecorded, GuidedSectionGuid)) {\r
734 //\r
735 // Found the recorded GuidedSectionGuid.\r
736 //\r
737 Status = PeiServicesLocatePpi (GuidedSectionGuid, 0, NULL, (VOID **) &Interface);\r
738 if (!EFI_ERROR (Status) && Interface != NULL) {\r
739 //\r
740 // Found the supported Guided Section Extraction Ppi for the Guided Section.\r
741 //\r
742 *GuidedSectionExtraction = (EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *) Interface;\r
743 return TRUE;\r
744 }\r
745 return FALSE;\r
746 }\r
747 }\r
748\r
749 return FALSE;\r
750}\r
751\r
752/**\r
753 Go through the file to search SectionType section.\r
754 Search within encapsulation sections (compression and GUIDed) recursively,\r
755 until the match section is found.\r
756\r
757 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
758 @param SectionType Filter to find only section of this type.\r
759 @param SectionInstance Pointer to the filter to find the specific instance of section.\r
760 @param Section From where to search.\r
761 @param SectionSize The file size to search.\r
762 @param OutputBuffer A pointer to the discovered section, if successful.\r
763 NULL if section not found\r
764 @param AuthenticationStatus Updated upon return to point to the authentication status for this section.\r
765 @param IsFfs3Fv Indicates the FV format.\r
766\r
767 @return EFI_NOT_FOUND The match section is not found.\r
768 @return EFI_SUCCESS The match section is found.\r
769\r
770**/\r
771EFI_STATUS\r
772ProcessSection (\r
773 IN CONST EFI_PEI_SERVICES **PeiServices,\r
774 IN EFI_SECTION_TYPE SectionType,\r
775 IN OUT UINTN *SectionInstance,\r
776 IN EFI_COMMON_SECTION_HEADER *Section,\r
777 IN UINTN SectionSize,\r
778 OUT VOID **OutputBuffer,\r
779 OUT UINT32 *AuthenticationStatus,\r
780 IN BOOLEAN IsFfs3Fv\r
781 )\r
782{\r
783 EFI_STATUS Status;\r
784 UINT32 SectionLength;\r
785 UINT32 ParsedLength;\r
786 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *GuidSectionPpi;\r
787 EFI_PEI_DECOMPRESS_PPI *DecompressPpi;\r
788 VOID *PpiOutput;\r
789 UINTN PpiOutputSize;\r
790 UINTN Index;\r
791 UINT32 Authentication;\r
792 PEI_CORE_INSTANCE *PrivateData;\r
793 EFI_GUID *SectionDefinitionGuid;\r
794 BOOLEAN SectionCached;\r
795 VOID *TempOutputBuffer;\r
796 UINT32 TempAuthenticationStatus;\r
797 UINT16 GuidedSectionAttributes;\r
798\r
799 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
800 *OutputBuffer = NULL;\r
801 ParsedLength = 0;\r
802 Index = 0;\r
803 Status = EFI_NOT_FOUND;\r
804 PpiOutput = NULL;\r
805 PpiOutputSize = 0;\r
806 while (ParsedLength < SectionSize) {\r
807\r
808 if (IS_SECTION2 (Section)) {\r
809 ASSERT (SECTION2_SIZE (Section) > 0x00FFFFFF);\r
810 if (!IsFfs3Fv) {\r
811 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted section in a non-FFS3 formatted FV.\n"));\r
812 SectionLength = SECTION2_SIZE (Section);\r
813 //\r
814 // SectionLength is adjusted it is 4 byte aligned.\r
815 // Go to the next section\r
816 //\r
817 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
818 ASSERT (SectionLength != 0);\r
819 ParsedLength += SectionLength;\r
820 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + SectionLength);\r
821 continue;\r
822 }\r
823 }\r
824\r
825 if (Section->Type == SectionType) {\r
826 //\r
827 // The type matches, so check the instance count to see if it's the one we want.\r
828 //\r
829 (*SectionInstance)--;\r
830 if (*SectionInstance == 0) {\r
831 //\r
832 // Got it!\r
833 //\r
834 if (IS_SECTION2 (Section)) {\r
835 *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER2));\r
836 } else {\r
837 *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER));\r
838 }\r
839 return EFI_SUCCESS;\r
840 } else {\r
841 if (IS_SECTION2 (Section)) {\r
842 SectionLength = SECTION2_SIZE (Section);\r
843 } else {\r
844 SectionLength = SECTION_SIZE (Section);\r
845 }\r
846 //\r
847 // SectionLength is adjusted it is 4 byte aligned.\r
848 // Go to the next section\r
849 //\r
850 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
851 ASSERT (SectionLength != 0);\r
852 ParsedLength += SectionLength;\r
853 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);\r
854 continue;\r
855 }\r
856 } else if ((Section->Type == EFI_SECTION_GUID_DEFINED) || (Section->Type == EFI_SECTION_COMPRESSION)) {\r
857 //\r
858 // Check the encapsulated section is extracted into the cache data.\r
859 //\r
860 SectionCached = FALSE;\r
861 for (Index = 0; Index < PrivateData->CacheSection.AllSectionCount; Index ++) {\r
862 if (Section == PrivateData->CacheSection.Section[Index]) {\r
863 SectionCached = TRUE;\r
864 PpiOutput = PrivateData->CacheSection.SectionData[Index];\r
865 PpiOutputSize = PrivateData->CacheSection.SectionSize[Index];\r
866 Authentication = PrivateData->CacheSection.AuthenticationStatus[Index];\r
867 //\r
868 // Search section directly from the cache data.\r
869 //\r
870 TempAuthenticationStatus = 0;\r
871 Status = ProcessSection (\r
872 PeiServices,\r
873 SectionType,\r
874 SectionInstance,\r
875 PpiOutput,\r
876 PpiOutputSize,\r
877 &TempOutputBuffer,\r
878 &TempAuthenticationStatus,\r
879 IsFfs3Fv\r
880 );\r
881 if (!EFI_ERROR (Status)) {\r
882 *OutputBuffer = TempOutputBuffer;\r
883 *AuthenticationStatus = TempAuthenticationStatus | Authentication;\r
884 return EFI_SUCCESS;\r
885 }\r
886 }\r
887 }\r
888\r
889 //\r
890 // If SectionCached is TRUE, the section data has been cached and scanned.\r
891 //\r
892 if (!SectionCached) {\r
893 Status = EFI_NOT_FOUND;\r
894 Authentication = 0;\r
895 if (Section->Type == EFI_SECTION_GUID_DEFINED) {\r
896 if (IS_SECTION2 (Section)) {\r
897 SectionDefinitionGuid = &((EFI_GUID_DEFINED_SECTION2 *)Section)->SectionDefinitionGuid;\r
898 GuidedSectionAttributes = ((EFI_GUID_DEFINED_SECTION2 *)Section)->Attributes;\r
899 } else {\r
900 SectionDefinitionGuid = &((EFI_GUID_DEFINED_SECTION *)Section)->SectionDefinitionGuid;\r
901 GuidedSectionAttributes = ((EFI_GUID_DEFINED_SECTION *)Section)->Attributes;\r
902 }\r
903 if (VerifyGuidedSectionGuid (SectionDefinitionGuid, &GuidSectionPpi)) {\r
904 Status = GuidSectionPpi->ExtractSection (\r
905 GuidSectionPpi,\r
906 Section,\r
907 &PpiOutput,\r
908 &PpiOutputSize,\r
909 &Authentication\r
910 );\r
911 } else if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) == 0) {\r
912 //\r
913 // Figure out the proper authentication status for GUIDED section without processing required\r
914 //\r
915 Status = EFI_SUCCESS;\r
916 if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {\r
917 Authentication |= EFI_AUTH_STATUS_IMAGE_SIGNED | EFI_AUTH_STATUS_NOT_TESTED;\r
918 }\r
919 if (IS_SECTION2 (Section)) {\r
920 PpiOutputSize = SECTION2_SIZE (Section) - ((EFI_GUID_DEFINED_SECTION2 *) Section)->DataOffset;\r
921 PpiOutput = (UINT8 *) Section + ((EFI_GUID_DEFINED_SECTION2 *) Section)->DataOffset;\r
922 } else {\r
923 PpiOutputSize = SECTION_SIZE (Section) - ((EFI_GUID_DEFINED_SECTION *) Section)->DataOffset;\r
924 PpiOutput = (UINT8 *) Section + ((EFI_GUID_DEFINED_SECTION *) Section)->DataOffset;\r
925 }\r
926 }\r
927 } else if (Section->Type == EFI_SECTION_COMPRESSION) {\r
928 Status = PeiServicesLocatePpi (&gEfiPeiDecompressPpiGuid, 0, NULL, (VOID **) &DecompressPpi);\r
929 if (!EFI_ERROR (Status)) {\r
930 Status = DecompressPpi->Decompress (\r
931 DecompressPpi,\r
932 (CONST EFI_COMPRESSION_SECTION*) Section,\r
933 &PpiOutput,\r
934 &PpiOutputSize\r
935 );\r
936 }\r
937 }\r
938\r
939 if (!EFI_ERROR (Status)) {\r
940 if ((Authentication & EFI_AUTH_STATUS_NOT_TESTED) == 0) {\r
941 //\r
942 // Update cache section data.\r
943 //\r
944 if (PrivateData->CacheSection.AllSectionCount < CACHE_SETION_MAX_NUMBER) {\r
945 PrivateData->CacheSection.AllSectionCount ++;\r
946 }\r
947 PrivateData->CacheSection.Section [PrivateData->CacheSection.SectionIndex] = Section;\r
948 PrivateData->CacheSection.SectionData [PrivateData->CacheSection.SectionIndex] = PpiOutput;\r
949 PrivateData->CacheSection.SectionSize [PrivateData->CacheSection.SectionIndex] = PpiOutputSize;\r
950 PrivateData->CacheSection.AuthenticationStatus [PrivateData->CacheSection.SectionIndex] = Authentication;\r
951 PrivateData->CacheSection.SectionIndex = (PrivateData->CacheSection.SectionIndex + 1)%CACHE_SETION_MAX_NUMBER;\r
952 }\r
953\r
954 TempAuthenticationStatus = 0;\r
955 Status = ProcessSection (\r
956 PeiServices,\r
957 SectionType,\r
958 SectionInstance,\r
959 PpiOutput,\r
960 PpiOutputSize,\r
961 &TempOutputBuffer,\r
962 &TempAuthenticationStatus,\r
963 IsFfs3Fv\r
964 );\r
965 if (!EFI_ERROR (Status)) {\r
966 *OutputBuffer = TempOutputBuffer;\r
967 *AuthenticationStatus = TempAuthenticationStatus | Authentication;\r
968 return EFI_SUCCESS;\r
969 }\r
970 }\r
971 }\r
972 }\r
973\r
974 if (IS_SECTION2 (Section)) {\r
975 SectionLength = SECTION2_SIZE (Section);\r
976 } else {\r
977 SectionLength = SECTION_SIZE (Section);\r
978 }\r
979 //\r
980 // SectionLength is adjusted it is 4 byte aligned.\r
981 // Go to the next section\r
982 //\r
983 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
984 ASSERT (SectionLength != 0);\r
985 ParsedLength += SectionLength;\r
986 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);\r
987 }\r
988\r
989 return EFI_NOT_FOUND;\r
990}\r
991\r
992\r
993/**\r
994 Searches for the next matching section within the specified file.\r
995\r
996 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
997 @param SectionType Filter to find only sections of this type.\r
998 @param FileHandle Pointer to the current file to search.\r
999 @param SectionData A pointer to the discovered section, if successful.\r
1000 NULL if section not found\r
1001\r
1002 @retval EFI_NOT_FOUND The section was not found.\r
1003 @retval EFI_SUCCESS The section was found.\r
1004\r
1005**/\r
1006EFI_STATUS\r
1007EFIAPI\r
1008PeiFfsFindSectionData (\r
1009 IN CONST EFI_PEI_SERVICES **PeiServices,\r
1010 IN EFI_SECTION_TYPE SectionType,\r
1011 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1012 OUT VOID **SectionData\r
1013 )\r
1014{\r
1015 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
1016\r
1017 CoreFvHandle = FileHandleToVolume (FileHandle);\r
1018 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
1019 return EFI_NOT_FOUND;\r
1020 }\r
1021\r
1022 return CoreFvHandle->FvPpi->FindSectionByType (CoreFvHandle->FvPpi, SectionType, FileHandle, SectionData);\r
1023}\r
1024\r
1025/**\r
1026 Searches for the next matching section within the specified file.\r
1027\r
1028 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
1029 @param SectionType The value of the section type to find.\r
1030 @param SectionInstance Section instance to find.\r
1031 @param FileHandle Handle of the firmware file to search.\r
1032 @param SectionData A pointer to the discovered section, if successful.\r
1033 @param AuthenticationStatus A pointer to the authentication status for this section.\r
1034\r
1035 @retval EFI_SUCCESS The section was found.\r
1036 @retval EFI_NOT_FOUND The section was not found.\r
1037\r
1038**/\r
1039EFI_STATUS\r
1040EFIAPI\r
1041PeiFfsFindSectionData3 (\r
1042 IN CONST EFI_PEI_SERVICES **PeiServices,\r
1043 IN EFI_SECTION_TYPE SectionType,\r
1044 IN UINTN SectionInstance,\r
1045 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1046 OUT VOID **SectionData,\r
1047 OUT UINT32 *AuthenticationStatus\r
1048 )\r
1049{\r
1050 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
1051\r
1052 CoreFvHandle = FileHandleToVolume (FileHandle);\r
1053 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
1054 return EFI_NOT_FOUND;\r
1055 }\r
1056\r
1057 if ((CoreFvHandle->FvPpi->Signature == EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE) &&\r
1058 (CoreFvHandle->FvPpi->Revision == EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION)) {\r
1059 return CoreFvHandle->FvPpi->FindSectionByType2 (CoreFvHandle->FvPpi, SectionType, SectionInstance, FileHandle, SectionData, AuthenticationStatus);\r
1060 }\r
1061 //\r
1062 // The old FvPpi doesn't support to find section by section instance\r
1063 // and return authentication status, so return EFI_UNSUPPORTED.\r
1064 //\r
1065 return EFI_UNSUPPORTED;\r
1066}\r
1067\r
1068/**\r
1069 Searches for the next matching file in the firmware volume.\r
1070\r
1071 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
1072 @param SearchType Filter to find only files of this type.\r
1073 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.\r
1074 @param FvHandle Handle of firmware volume in which to search.\r
1075 @param FileHandle On entry, points to the current handle from which to begin searching or NULL to start\r
1076 at the beginning of the firmware volume. On exit, points the file handle of the next file\r
1077 in the volume or NULL if there are no more files.\r
1078\r
1079 @retval EFI_NOT_FOUND The file was not found.\r
1080 @retval EFI_NOT_FOUND The header checksum was not zero.\r
1081 @retval EFI_SUCCESS The file was found.\r
1082\r
1083**/\r
1084EFI_STATUS\r
1085EFIAPI\r
1086PeiFfsFindNextFile (\r
1087 IN CONST EFI_PEI_SERVICES **PeiServices,\r
1088 IN UINT8 SearchType,\r
1089 IN EFI_PEI_FV_HANDLE FvHandle,\r
1090 IN OUT EFI_PEI_FILE_HANDLE *FileHandle\r
1091 )\r
1092{\r
1093 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
1094\r
1095 CoreFvHandle = FvHandleToCoreHandle (FvHandle);\r
1096\r
1097 if ((CoreFvHandle == NULL) || CoreFvHandle->FvPpi == NULL) {\r
1098 return EFI_NOT_FOUND;\r
1099 }\r
1100\r
1101 return CoreFvHandle->FvPpi->FindFileByType (CoreFvHandle->FvPpi, SearchType, FvHandle, FileHandle);\r
1102}\r
1103\r
1104\r
1105/**\r
1106 Search the firmware volumes by index\r
1107\r
1108 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
1109 @param Instance This instance of the firmware volume to find. The value 0 is the Boot Firmware\r
1110 Volume (BFV).\r
1111 @param VolumeHandle On exit, points to the next volume handle or NULL if it does not exist.\r
1112\r
1113 @retval EFI_INVALID_PARAMETER VolumeHandle is NULL\r
1114 @retval EFI_NOT_FOUND The volume was not found.\r
1115 @retval EFI_SUCCESS The volume was found.\r
1116\r
1117**/\r
1118EFI_STATUS\r
1119EFIAPI\r
1120PeiFfsFindNextVolume (\r
1121 IN CONST EFI_PEI_SERVICES **PeiServices,\r
1122 IN UINTN Instance,\r
1123 IN OUT EFI_PEI_FV_HANDLE *VolumeHandle\r
1124 )\r
1125{\r
1126 PEI_CORE_INSTANCE *Private;\r
1127 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
1128\r
1129 if (VolumeHandle == NULL) {\r
1130 return EFI_INVALID_PARAMETER;\r
1131 }\r
1132\r
1133 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
1134\r
1135 CoreFvHandle = FindNextCoreFvHandle (Private, Instance);\r
1136 if (CoreFvHandle == NULL) {\r
1137 *VolumeHandle = NULL;\r
1138 return EFI_NOT_FOUND;\r
1139 }\r
1140\r
1141 *VolumeHandle = CoreFvHandle->FvHandle;\r
1142\r
1143 return EFI_SUCCESS;\r
1144}\r
1145\r
1146\r
1147/**\r
1148 Find a file within a volume by its name.\r
1149\r
1150 @param FileName A pointer to the name of the file to find within the firmware volume.\r
1151 @param VolumeHandle The firmware volume to search\r
1152 @param FileHandle Upon exit, points to the found file's handle\r
1153 or NULL if it could not be found.\r
1154\r
1155 @retval EFI_SUCCESS File was found.\r
1156 @retval EFI_NOT_FOUND File was not found.\r
1157 @retval EFI_INVALID_PARAMETER VolumeHandle or FileHandle or FileName was NULL.\r
1158\r
1159**/\r
1160EFI_STATUS\r
1161EFIAPI\r
1162PeiFfsFindFileByName (\r
1163 IN CONST EFI_GUID *FileName,\r
1164 IN EFI_PEI_FV_HANDLE VolumeHandle,\r
1165 OUT EFI_PEI_FILE_HANDLE *FileHandle\r
1166 )\r
1167{\r
1168 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
1169\r
1170 if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {\r
1171 return EFI_INVALID_PARAMETER;\r
1172 }\r
1173\r
1174 CoreFvHandle = FvHandleToCoreHandle (VolumeHandle);\r
1175 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
1176 return EFI_NOT_FOUND;\r
1177 }\r
1178\r
1179 return CoreFvHandle->FvPpi->FindFileByName (CoreFvHandle->FvPpi, FileName, &VolumeHandle, FileHandle);\r
1180}\r
1181\r
1182/**\r
1183 Returns information about a specific file.\r
1184\r
1185 @param FileHandle Handle of the file.\r
1186 @param FileInfo Upon exit, points to the file's information.\r
1187\r
1188 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.\r
1189 @retval EFI_INVALID_PARAMETER If FileHandle does not represent a valid file.\r
1190 @retval EFI_SUCCESS File information returned.\r
1191\r
1192**/\r
1193EFI_STATUS\r
1194EFIAPI\r
1195PeiFfsGetFileInfo (\r
1196 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1197 OUT EFI_FV_FILE_INFO *FileInfo\r
1198 )\r
1199{\r
1200 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
1201\r
1202 if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
1203 return EFI_INVALID_PARAMETER;\r
1204 }\r
1205\r
1206 //\r
1207 // Retrieve the FirmwareVolume which the file resides in.\r
1208 //\r
1209 CoreFvHandle = FileHandleToVolume (FileHandle);\r
1210 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
1211 return EFI_INVALID_PARAMETER;\r
1212 }\r
1213\r
1214 return CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, FileHandle, FileInfo);\r
1215}\r
1216\r
1217/**\r
1218 Returns information about a specific file.\r
1219\r
1220 @param FileHandle Handle of the file.\r
1221 @param FileInfo Upon exit, points to the file's information.\r
1222\r
1223 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.\r
1224 @retval EFI_INVALID_PARAMETER If FileHandle does not represent a valid file.\r
1225 @retval EFI_SUCCESS File information returned.\r
1226\r
1227**/\r
1228EFI_STATUS\r
1229EFIAPI\r
1230PeiFfsGetFileInfo2 (\r
1231 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1232 OUT EFI_FV_FILE_INFO2 *FileInfo\r
1233 )\r
1234{\r
1235 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
1236\r
1237 if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
1238 return EFI_INVALID_PARAMETER;\r
1239 }\r
1240\r
1241 //\r
1242 // Retrieve the FirmwareVolume which the file resides in.\r
1243 //\r
1244 CoreFvHandle = FileHandleToVolume (FileHandle);\r
1245 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
1246 return EFI_INVALID_PARAMETER;\r
1247 }\r
1248\r
1249 if ((CoreFvHandle->FvPpi->Signature == EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE) &&\r
1250 (CoreFvHandle->FvPpi->Revision == EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION)) {\r
1251 return CoreFvHandle->FvPpi->GetFileInfo2 (CoreFvHandle->FvPpi, FileHandle, FileInfo);\r
1252 }\r
1253 //\r
1254 // The old FvPpi doesn't support to return file info with authentication status,\r
1255 // so return EFI_UNSUPPORTED.\r
1256 //\r
1257 return EFI_UNSUPPORTED;\r
1258}\r
1259\r
1260/**\r
1261 Returns information about the specified volume.\r
1262\r
1263 This function returns information about a specific firmware\r
1264 volume, including its name, type, attributes, starting address\r
1265 and size.\r
1266\r
1267 @param VolumeHandle Handle of the volume.\r
1268 @param VolumeInfo Upon exit, points to the volume's information.\r
1269\r
1270 @retval EFI_SUCCESS Volume information returned.\r
1271 @retval EFI_INVALID_PARAMETER If VolumeHandle does not represent a valid volume.\r
1272 @retval EFI_INVALID_PARAMETER If VolumeHandle is NULL.\r
1273 @retval EFI_SUCCESS Information successfully returned.\r
1274 @retval EFI_INVALID_PARAMETER The volume designated by the VolumeHandle is not available.\r
1275\r
1276**/\r
1277EFI_STATUS\r
1278EFIAPI\r
1279PeiFfsGetVolumeInfo (\r
1280 IN EFI_PEI_FV_HANDLE VolumeHandle,\r
1281 OUT EFI_FV_INFO *VolumeInfo\r
1282 )\r
1283{\r
1284 PEI_CORE_FV_HANDLE *CoreHandle;\r
1285\r
1286 if ((VolumeInfo == NULL) || (VolumeHandle == NULL)) {\r
1287 return EFI_INVALID_PARAMETER;\r
1288 }\r
1289\r
1290 CoreHandle = FvHandleToCoreHandle (VolumeHandle);\r
1291\r
1292 if ((CoreHandle == NULL) || (CoreHandle->FvPpi == NULL)) {\r
1293 return EFI_INVALID_PARAMETER;\r
1294 }\r
1295\r
1296 return CoreHandle->FvPpi->GetVolumeInfo (CoreHandle->FvPpi, VolumeHandle, VolumeInfo);\r
1297}\r
1298\r
1299/**\r
1300 Find USED_SIZE FV_EXT_TYPE entry in FV extension header and get the FV used size.\r
1301\r
1302 @param[in] FvHeader Pointer to FV header.\r
1303 @param[out] FvUsedSize Pointer to FV used size returned,\r
1304 only valid if USED_SIZE FV_EXT_TYPE entry is found.\r
1305 @param[out] EraseByte Pointer to erase byte returned,\r
1306 only valid if USED_SIZE FV_EXT_TYPE entry is found.\r
1307\r
1308 @retval TRUE USED_SIZE FV_EXT_TYPE entry is found,\r
1309 FV used size and erase byte are returned.\r
1310 @retval FALSE No USED_SIZE FV_EXT_TYPE entry found.\r
1311\r
1312**/\r
1313BOOLEAN\r
1314GetFvUsedSize (\r
1315 IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader,\r
1316 OUT UINT32 *FvUsedSize,\r
1317 OUT UINT8 *EraseByte\r
1318 )\r
1319{\r
1320 UINT16 ExtHeaderOffset;\r
1321 EFI_FIRMWARE_VOLUME_EXT_HEADER *ExtHeader;\r
1322 EFI_FIRMWARE_VOLUME_EXT_ENTRY *ExtEntryList;\r
1323 EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *ExtEntryUsedSize;\r
1324\r
1325 ExtHeaderOffset = ReadUnaligned16 (&FvHeader->ExtHeaderOffset);\r
1326 if (ExtHeaderOffset != 0) {\r
1327 ExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) ((UINT8 *) FvHeader + ExtHeaderOffset);\r
1328 ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *) (ExtHeader + 1);\r
1329 while ((UINTN) ExtEntryList < ((UINTN) ExtHeader + ReadUnaligned32 (&ExtHeader->ExtHeaderSize))) {\r
1330 if (ReadUnaligned16 (&ExtEntryList->ExtEntryType) == EFI_FV_EXT_TYPE_USED_SIZE_TYPE) {\r
1331 //\r
1332 // USED_SIZE FV_EXT_TYPE entry is found.\r
1333 //\r
1334 ExtEntryUsedSize = (EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *) ExtEntryList;\r
1335 *FvUsedSize = ReadUnaligned32 (&ExtEntryUsedSize->UsedSize);\r
1336 if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ERASE_POLARITY) != 0) {\r
1337 *EraseByte = 0xFF;\r
1338 } else {\r
1339 *EraseByte = 0;\r
1340 }\r
1341 DEBUG ((\r
1342 DEBUG_INFO,\r
1343 "FV at 0x%x has 0x%x used size, and erase byte is 0x%02x\n",\r
1344 FvHeader,\r
1345 *FvUsedSize,\r
1346 *EraseByte\r
1347 ));\r
1348 return TRUE;\r
1349 }\r
1350 ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *)\r
1351 ((UINT8 *) ExtEntryList + ReadUnaligned16 (&ExtEntryList->ExtEntrySize));\r
1352 }\r
1353 }\r
1354\r
1355 //\r
1356 // No USED_SIZE FV_EXT_TYPE entry found.\r
1357 //\r
1358 return FALSE;\r
1359}\r
1360\r
1361/**\r
1362 Get FV image(s) from the FV type file, then install FV INFO(2) PPI, Build FV(2, 3) HOB.\r
1363\r
1364 @param PrivateData PeiCore's private data structure\r
1365 @param ParentFvCoreHandle Pointer of EFI_CORE_FV_HANDLE to parent FV image that contain this FV image.\r
1366 @param ParentFvFileHandle File handle of a FV type file that contain this FV image.\r
1367\r
1368 @retval EFI_NOT_FOUND FV image can't be found.\r
1369 @retval EFI_SUCCESS Successfully to process it.\r
1370 @retval EFI_OUT_OF_RESOURCES Can not allocate page when aligning FV image\r
1371 @retval EFI_SECURITY_VIOLATION Image is illegal\r
1372 @retval Others Can not find EFI_SECTION_FIRMWARE_VOLUME_IMAGE section\r
1373\r
1374**/\r
1375EFI_STATUS\r
1376ProcessFvFile (\r
1377 IN PEI_CORE_INSTANCE *PrivateData,\r
1378 IN PEI_CORE_FV_HANDLE *ParentFvCoreHandle,\r
1379 IN EFI_PEI_FILE_HANDLE ParentFvFileHandle\r
1380 )\r
1381{\r
1382 EFI_STATUS Status;\r
1383 EFI_FV_INFO ParentFvImageInfo;\r
1384 UINT32 FvAlignment;\r
1385 VOID *NewFvBuffer;\r
1386 EFI_PEI_HOB_POINTERS HobPtr;\r
1387 EFI_PEI_FIRMWARE_VOLUME_PPI *ParentFvPpi;\r
1388 EFI_PEI_FV_HANDLE ParentFvHandle;\r
1389 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
1390 EFI_FV_FILE_INFO FileInfo;\r
1391 UINT64 FvLength;\r
1392 UINT32 AuthenticationStatus;\r
1393 UINT32 FvUsedSize;\r
1394 UINT8 EraseByte;\r
1395 UINTN Index;\r
1396\r
1397 //\r
1398 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already\r
1399 // been extracted.\r
1400 //\r
1401 HobPtr.Raw = GetHobList ();\r
1402 while ((HobPtr.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobPtr.Raw)) != NULL) {\r
1403 if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)ParentFvFileHandle)->Name), &HobPtr.FirmwareVolume2->FileName)) {\r
1404 //\r
1405 // this FILE has been dispatched, it will not be dispatched again.\r
1406 //\r
1407 DEBUG ((EFI_D_INFO, "FV file %p has been dispatched!\r\n", ParentFvFileHandle));\r
1408 return EFI_SUCCESS;\r
1409 }\r
1410 HobPtr.Raw = GET_NEXT_HOB (HobPtr);\r
1411 }\r
1412\r
1413 ParentFvHandle = ParentFvCoreHandle->FvHandle;\r
1414 ParentFvPpi = ParentFvCoreHandle->FvPpi;\r
1415\r
1416 Status = EFI_SUCCESS;\r
1417\r
1418 //\r
1419 // Find FvImage(s) in FvFile\r
1420 //\r
1421 Index = 0;\r
1422 do {\r
1423 AuthenticationStatus = 0;\r
1424 if ((ParentFvPpi->Signature == EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE) &&\r
1425 (ParentFvPpi->Revision == EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION)) {\r
1426 Status = ParentFvPpi->FindSectionByType2 (\r
1427 ParentFvPpi,\r
1428 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
1429 Index,\r
1430 ParentFvFileHandle,\r
1431 (VOID **)&FvHeader,\r
1432 &AuthenticationStatus\r
1433 );\r
1434 } else {\r
1435 //\r
1436 // Old FvPpi has no parameter to input SearchInstance,\r
1437 // only one instance is supported.\r
1438 //\r
1439 if (Index > 0) {\r
1440 break;\r
1441 }\r
1442 Status = ParentFvPpi->FindSectionByType (\r
1443 ParentFvPpi,\r
1444 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
1445 ParentFvFileHandle,\r
1446 (VOID **)&FvHeader\r
1447 );\r
1448 }\r
1449 if (EFI_ERROR (Status)) {\r
1450 break;\r
1451 }\r
1452\r
1453 Status = VerifyPeim (PrivateData, ParentFvHandle, ParentFvFileHandle, AuthenticationStatus);\r
1454 if (Status == EFI_SECURITY_VIOLATION) {\r
1455 break;\r
1456 }\r
1457\r
1458 //\r
1459 // If EFI_FVB2_WEAK_ALIGNMENT is set in the volume header then the first byte of the volume\r
1460 // can be aligned on any power-of-two boundary. A weakly aligned volume can not be moved from\r
1461 // its initial linked location and maintain its alignment.\r
1462 //\r
1463 if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) {\r
1464 //\r
1465 // FvAlignment must be greater than or equal to 8 bytes of the minimum FFS alignment value.\r
1466 //\r
1467 FvAlignment = 1 << ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ALIGNMENT) >> 16);\r
1468 if (FvAlignment < 8) {\r
1469 FvAlignment = 8;\r
1470 }\r
1471\r
1472 DEBUG ((\r
1473 DEBUG_INFO,\r
1474 "%a() FV at 0x%x, FvAlignment required is 0x%x\n",\r
1475 __FUNCTION__,\r
1476 FvHeader,\r
1477 FvAlignment\r
1478 ));\r
1479\r
1480 //\r
1481 // Check FvImage alignment.\r
1482 //\r
1483 if ((UINTN) FvHeader % FvAlignment != 0) {\r
1484 FvLength = ReadUnaligned64 (&FvHeader->FvLength);\r
1485 NewFvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvLength), FvAlignment);\r
1486 if (NewFvBuffer == NULL) {\r
1487 Status = EFI_OUT_OF_RESOURCES;\r
1488 break;\r
1489 }\r
1490 if (GetFvUsedSize (FvHeader, &FvUsedSize, &EraseByte)) {\r
1491 //\r
1492 // Copy the used bytes and fill the rest with the erase value.\r
1493 //\r
1494 CopyMem (NewFvBuffer, FvHeader, (UINTN) FvUsedSize);\r
1495 SetMem (\r
1496 (UINT8 *) NewFvBuffer + FvUsedSize,\r
1497 (UINTN) (FvLength - FvUsedSize),\r
1498 EraseByte\r
1499 );\r
1500 } else {\r
1501 CopyMem (NewFvBuffer, FvHeader, (UINTN) FvLength);\r
1502 }\r
1503 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) NewFvBuffer;\r
1504 }\r
1505 }\r
1506\r
1507 Status = ParentFvPpi->GetVolumeInfo (ParentFvPpi, ParentFvHandle, &ParentFvImageInfo);\r
1508 ASSERT_EFI_ERROR (Status);\r
1509\r
1510 Status = ParentFvPpi->GetFileInfo (ParentFvPpi, ParentFvFileHandle, &FileInfo);\r
1511 ASSERT_EFI_ERROR (Status);\r
1512\r
1513 //\r
1514 // Install FvInfo(2) Ppi\r
1515 // NOTE: FvInfo2 must be installed before FvInfo so that recursive processing of encapsulated\r
1516 // FVs inherit the proper AuthenticationStatus.\r
1517 //\r
1518 PeiServicesInstallFvInfo2Ppi(\r
1519 &FvHeader->FileSystemGuid,\r
1520 (VOID**)FvHeader,\r
1521 (UINT32)FvHeader->FvLength,\r
1522 &ParentFvImageInfo.FvName,\r
1523 &FileInfo.FileName,\r
1524 AuthenticationStatus\r
1525 );\r
1526\r
1527 PeiServicesInstallFvInfoPpi (\r
1528 &FvHeader->FileSystemGuid,\r
1529 (VOID**) FvHeader,\r
1530 (UINT32) FvHeader->FvLength,\r
1531 &ParentFvImageInfo.FvName,\r
1532 &FileInfo.FileName\r
1533 );\r
1534\r
1535 //\r
1536 // Inform the extracted FvImage to FV HOB consumer phase, i.e. DXE phase\r
1537 //\r
1538 BuildFvHob (\r
1539 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r
1540 FvHeader->FvLength\r
1541 );\r
1542\r
1543 //\r
1544 // Makes the encapsulated volume show up in DXE phase to skip processing of\r
1545 // encapsulated file again.\r
1546 //\r
1547 BuildFv2Hob (\r
1548 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r
1549 FvHeader->FvLength,\r
1550 &ParentFvImageInfo.FvName,\r
1551 &FileInfo.FileName\r
1552 );\r
1553\r
1554 //\r
1555 // Build FV3 HOB with authentication status to be propagated to DXE.\r
1556 //\r
1557 BuildFv3Hob (\r
1558 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r
1559 FvHeader->FvLength,\r
1560 AuthenticationStatus,\r
1561 TRUE,\r
1562 &ParentFvImageInfo.FvName,\r
1563 &FileInfo.FileName\r
1564 );\r
1565\r
1566 Index++;\r
1567 } while (TRUE);\r
1568\r
1569 if (Index > 0) {\r
1570 //\r
1571 // At least one FvImage has been processed successfully.\r
1572 //\r
1573 return EFI_SUCCESS;\r
1574 } else {\r
1575 return Status;\r
1576 }\r
1577}\r
1578\r
1579/**\r
1580 Process a firmware volume and create a volume handle.\r
1581\r
1582 Create a volume handle from the information in the buffer. For\r
1583 memory-mapped firmware volumes, Buffer and BufferSize refer to\r
1584 the start of the firmware volume and the firmware volume size.\r
1585 For non memory-mapped firmware volumes, this points to a\r
1586 buffer which contains the necessary information for creating\r
1587 the firmware volume handle. Normally, these values are derived\r
1588 from the EFI_FIRMWARE_VOLUME_INFO_PPI.\r
1589\r
1590\r
1591 @param This Points to this instance of the\r
1592 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1593 @param Buffer Points to the start of the buffer.\r
1594 @param BufferSize Size of the buffer.\r
1595 @param FvHandle Points to the returned firmware volume\r
1596 handle. The firmware volume handle must\r
1597 be unique within the system.\r
1598\r
1599 @retval EFI_SUCCESS Firmware volume handle created.\r
1600 @retval EFI_VOLUME_CORRUPTED Volume was corrupt.\r
1601\r
1602**/\r
1603EFI_STATUS\r
1604EFIAPI\r
1605PeiFfsFvPpiProcessVolume (\r
1606 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
1607 IN VOID *Buffer,\r
1608 IN UINTN BufferSize,\r
1609 OUT EFI_PEI_FV_HANDLE *FvHandle\r
1610 )\r
1611{\r
1612 EFI_STATUS Status;\r
1613\r
1614 ASSERT (FvHandle != NULL);\r
1615\r
1616 if (Buffer == NULL) {\r
1617 return EFI_VOLUME_CORRUPTED;\r
1618 }\r
1619\r
1620 //\r
1621 // The build-in EFI_PEI_FIRMWARE_VOLUME_PPI for FFS2/FFS3 support memory-mapped\r
1622 // FV image and the handle is pointed to FV image's buffer.\r
1623 //\r
1624 *FvHandle = (EFI_PEI_FV_HANDLE) Buffer;\r
1625\r
1626 //\r
1627 // Do verify for given FV buffer.\r
1628 //\r
1629 Status = VerifyFv ((EFI_FIRMWARE_VOLUME_HEADER*) Buffer);\r
1630 if (EFI_ERROR(Status)) {\r
1631 DEBUG ((EFI_D_ERROR, "Fail to verify FV which address is 0x%11p", Buffer));\r
1632 return EFI_VOLUME_CORRUPTED;\r
1633 }\r
1634\r
1635 return EFI_SUCCESS;\r
1636}\r
1637\r
1638/**\r
1639 Finds the next file of the specified type.\r
1640\r
1641 This service enables PEI modules to discover additional firmware files.\r
1642 The FileHandle must be unique within the system.\r
1643\r
1644 @param This Points to this instance of the\r
1645 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1646 @param SearchType A filter to find only files of this type. Type\r
1647 EFI_FV_FILETYPE_ALL causes no filtering to be\r
1648 done.\r
1649 @param FvHandle Handle of firmware volume in which to\r
1650 search.\r
1651 @param FileHandle Points to the current handle from which to\r
1652 begin searching or NULL to start at the\r
1653 beginning of the firmware volume. Updated\r
1654 upon return to reflect the file found.\r
1655\r
1656 @retval EFI_SUCCESS The file was found.\r
1657 @retval EFI_NOT_FOUND The file was not found. FileHandle contains NULL.\r
1658\r
1659**/\r
1660EFI_STATUS\r
1661EFIAPI\r
1662PeiFfsFvPpiFindFileByType (\r
1663 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
1664 IN EFI_FV_FILETYPE SearchType,\r
1665 IN EFI_PEI_FV_HANDLE FvHandle,\r
1666 IN OUT EFI_PEI_FILE_HANDLE *FileHandle\r
1667 )\r
1668{\r
1669 return FindFileEx (FvHandle, NULL, SearchType, FileHandle, NULL);\r
1670}\r
1671\r
1672/**\r
1673 Find a file within a volume by its name.\r
1674\r
1675 This service searches for files with a specific name, within\r
1676 either the specified firmware volume or all firmware volumes.\r
1677\r
1678 @param This Points to this instance of the\r
1679 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1680 @param FileName A pointer to the name of the file to find\r
1681 within the firmware volume.\r
1682 @param FvHandle Upon entry, the pointer to the firmware\r
1683 volume to search or NULL if all firmware\r
1684 volumes should be searched. Upon exit, the\r
1685 actual firmware volume in which the file was\r
1686 found.\r
1687 @param FileHandle Upon exit, points to the found file's\r
1688 handle or NULL if it could not be found.\r
1689\r
1690 @retval EFI_SUCCESS File was found.\r
1691 @retval EFI_NOT_FOUND File was not found.\r
1692 @retval EFI_INVALID_PARAMETER FvHandle or FileHandle or\r
1693 FileName was NULL.\r
1694\r
1695\r
1696**/\r
1697EFI_STATUS\r
1698EFIAPI\r
1699PeiFfsFvPpiFindFileByName (\r
1700 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
1701 IN CONST EFI_GUID *FileName,\r
1702 IN EFI_PEI_FV_HANDLE *FvHandle,\r
1703 OUT EFI_PEI_FILE_HANDLE *FileHandle\r
1704 )\r
1705{\r
1706 EFI_STATUS Status;\r
1707 PEI_CORE_INSTANCE *PrivateData;\r
1708 UINTN Index;\r
1709\r
1710 if ((FvHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {\r
1711 return EFI_INVALID_PARAMETER;\r
1712 }\r
1713\r
1714 if (*FvHandle != NULL) {\r
1715 Status = FindFileEx (*FvHandle, FileName, 0, FileHandle, NULL);\r
1716 if (Status == EFI_NOT_FOUND) {\r
1717 *FileHandle = NULL;\r
1718 }\r
1719 } else {\r
1720 //\r
1721 // If *FvHandle = NULL, so search all FV for given filename\r
1722 //\r
1723 Status = EFI_NOT_FOUND;\r
1724\r
1725 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer());\r
1726 for (Index = 0; Index < PrivateData->FvCount; Index ++) {\r
1727 //\r
1728 // Only search the FV which is associated with a EFI_PEI_FIRMWARE_VOLUME_PPI instance.\r
1729 //\r
1730 if (PrivateData->Fv[Index].FvPpi != NULL) {\r
1731 Status = FindFileEx (PrivateData->Fv[Index].FvHandle, FileName, 0, FileHandle, NULL);\r
1732 if (!EFI_ERROR (Status)) {\r
1733 *FvHandle = PrivateData->Fv[Index].FvHandle;\r
1734 break;\r
1735 }\r
1736 }\r
1737 }\r
1738 }\r
1739\r
1740 return Status;\r
1741}\r
1742\r
1743/**\r
1744 Returns information about a specific file.\r
1745\r
1746 This function returns information about a specific\r
1747 file, including its file name, type, attributes, starting\r
1748 address and size.\r
1749\r
1750 @param This Points to this instance of the\r
1751 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1752 @param FileHandle Handle of the file.\r
1753 @param FileInfo Upon exit, points to the file's\r
1754 information.\r
1755\r
1756 @retval EFI_SUCCESS File information returned.\r
1757 @retval EFI_INVALID_PARAMETER If FileHandle does not\r
1758 represent a valid file.\r
1759 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.\r
1760\r
1761**/\r
1762EFI_STATUS\r
1763EFIAPI\r
1764PeiFfsFvPpiGetFileInfo (\r
1765 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
1766 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1767 OUT EFI_FV_FILE_INFO *FileInfo\r
1768 )\r
1769{\r
1770 UINT8 FileState;\r
1771 UINT8 ErasePolarity;\r
1772 EFI_FFS_FILE_HEADER *FileHeader;\r
1773 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
1774 PEI_FW_VOL_INSTANCE *FwVolInstance;\r
1775\r
1776 if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
1777 return EFI_INVALID_PARAMETER;\r
1778 }\r
1779\r
1780 //\r
1781 // Retrieve the FirmwareVolume which the file resides in.\r
1782 //\r
1783 CoreFvHandle = FileHandleToVolume (FileHandle);\r
1784 if (CoreFvHandle == NULL) {\r
1785 return EFI_INVALID_PARAMETER;\r
1786 }\r
1787\r
1788 FwVolInstance = PEI_FW_VOL_INSTANCE_FROM_FV_THIS (This);\r
1789\r
1790 if ((CoreFvHandle->FvHeader->Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {\r
1791 ErasePolarity = 1;\r
1792 } else {\r
1793 ErasePolarity = 0;\r
1794 }\r
1795\r
1796 //\r
1797 // Get FileState which is the highest bit of the State\r
1798 //\r
1799 FileState = GetFileState (ErasePolarity, (EFI_FFS_FILE_HEADER*)FileHandle);\r
1800\r
1801 switch (FileState) {\r
1802 case EFI_FILE_DATA_VALID:\r
1803 case EFI_FILE_MARKED_FOR_UPDATE:\r
1804 break;\r
1805 default:\r
1806 return EFI_INVALID_PARAMETER;\r
1807 }\r
1808\r
1809 FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle;\r
1810 if (IS_FFS_FILE2 (FileHeader)) {\r
1811 ASSERT (FFS_FILE2_SIZE (FileHeader) > 0x00FFFFFF);\r
1812 if (!FwVolInstance->IsFfs3Fv) {\r
1813 DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FileHeader->Name));\r
1814 return EFI_INVALID_PARAMETER;\r
1815 }\r
1816 FileInfo->BufferSize = FFS_FILE2_SIZE (FileHeader) - sizeof (EFI_FFS_FILE_HEADER2);\r
1817 FileInfo->Buffer = (UINT8 *) FileHeader + sizeof (EFI_FFS_FILE_HEADER2);\r
1818 } else {\r
1819 FileInfo->BufferSize = FFS_FILE_SIZE (FileHeader) - sizeof (EFI_FFS_FILE_HEADER);\r
1820 FileInfo->Buffer = (UINT8 *) FileHeader + sizeof (EFI_FFS_FILE_HEADER);\r
1821 }\r
1822 CopyMem (&FileInfo->FileName, &FileHeader->Name, sizeof(EFI_GUID));\r
1823 FileInfo->FileType = FileHeader->Type;\r
1824 FileInfo->FileAttributes = FfsAttributes2FvFileAttributes (FileHeader->Attributes);\r
1825 if ((CoreFvHandle->FvHeader->Attributes & EFI_FVB2_MEMORY_MAPPED) == EFI_FVB2_MEMORY_MAPPED) {\r
1826 FileInfo->FileAttributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;\r
1827 }\r
1828 return EFI_SUCCESS;\r
1829}\r
1830\r
1831/**\r
1832 Returns information about a specific file.\r
1833\r
1834 This function returns information about a specific\r
1835 file, including its file name, type, attributes, starting\r
1836 address, size and authentication status.\r
1837\r
1838 @param This Points to this instance of the\r
1839 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1840 @param FileHandle Handle of the file.\r
1841 @param FileInfo Upon exit, points to the file's\r
1842 information.\r
1843\r
1844 @retval EFI_SUCCESS File information returned.\r
1845 @retval EFI_INVALID_PARAMETER If FileHandle does not\r
1846 represent a valid file.\r
1847 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.\r
1848\r
1849**/\r
1850EFI_STATUS\r
1851EFIAPI\r
1852PeiFfsFvPpiGetFileInfo2 (\r
1853 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
1854 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1855 OUT EFI_FV_FILE_INFO2 *FileInfo\r
1856 )\r
1857{\r
1858 EFI_STATUS Status;\r
1859 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
1860\r
1861 if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
1862 return EFI_INVALID_PARAMETER;\r
1863 }\r
1864\r
1865 //\r
1866 // Retrieve the FirmwareVolume which the file resides in.\r
1867 //\r
1868 CoreFvHandle = FileHandleToVolume (FileHandle);\r
1869 if (CoreFvHandle == NULL) {\r
1870 return EFI_INVALID_PARAMETER;\r
1871 }\r
1872\r
1873 Status = PeiFfsFvPpiGetFileInfo (This, FileHandle, (EFI_FV_FILE_INFO *) FileInfo);\r
1874 if (!EFI_ERROR (Status)) {\r
1875 FileInfo->AuthenticationStatus = CoreFvHandle->AuthenticationStatus;\r
1876 }\r
1877\r
1878 return Status;\r
1879}\r
1880\r
1881/**\r
1882 This function returns information about the firmware volume.\r
1883\r
1884 @param This Points to this instance of the\r
1885 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1886 @param FvHandle Handle to the firmware handle.\r
1887 @param VolumeInfo Points to the returned firmware volume\r
1888 information.\r
1889\r
1890 @retval EFI_SUCCESS Information returned successfully.\r
1891 @retval EFI_INVALID_PARAMETER FvHandle does not indicate a valid\r
1892 firmware volume or VolumeInfo is NULL.\r
1893\r
1894**/\r
1895EFI_STATUS\r
1896EFIAPI\r
1897PeiFfsFvPpiGetVolumeInfo (\r
1898 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
1899 IN EFI_PEI_FV_HANDLE FvHandle,\r
1900 OUT EFI_FV_INFO *VolumeInfo\r
1901 )\r
1902{\r
1903 EFI_FIRMWARE_VOLUME_HEADER FwVolHeader;\r
1904 EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo;\r
1905\r
1906 if ((VolumeInfo == NULL) || (FvHandle == NULL)) {\r
1907 return EFI_INVALID_PARAMETER;\r
1908 }\r
1909\r
1910 //\r
1911 // VolumeHandle may not align at 8 byte,\r
1912 // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte.\r
1913 // So, Copy FvHeader into the local FvHeader structure.\r
1914 //\r
1915 CopyMem (&FwVolHeader, FvHandle, sizeof (EFI_FIRMWARE_VOLUME_HEADER));\r
1916\r
1917 //\r
1918 // Check FV Image Signature\r
1919 //\r
1920 if (FwVolHeader.Signature != EFI_FVH_SIGNATURE) {\r
1921 return EFI_INVALID_PARAMETER;\r
1922 }\r
1923\r
1924 ZeroMem (VolumeInfo, sizeof (EFI_FV_INFO));\r
1925 VolumeInfo->FvAttributes = FwVolHeader.Attributes;\r
1926 VolumeInfo->FvStart = (VOID *) FvHandle;\r
1927 VolumeInfo->FvSize = FwVolHeader.FvLength;\r
1928 CopyMem (&VolumeInfo->FvFormat, &FwVolHeader.FileSystemGuid, sizeof(EFI_GUID));\r
1929\r
1930 if (FwVolHeader.ExtHeaderOffset != 0) {\r
1931 FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER*)(((UINT8 *)FvHandle) + FwVolHeader.ExtHeaderOffset);\r
1932 CopyMem (&VolumeInfo->FvName, &FwVolExHeaderInfo->FvName, sizeof(EFI_GUID));\r
1933 }\r
1934\r
1935 return EFI_SUCCESS;\r
1936}\r
1937\r
1938/**\r
1939 Find the next matching section in the firmware file.\r
1940\r
1941 This service enables PEI modules to discover sections\r
1942 of a given type within a valid file.\r
1943\r
1944 @param This Points to this instance of the\r
1945 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1946 @param SearchType A filter to find only sections of this\r
1947 type.\r
1948 @param FileHandle Handle of firmware file in which to\r
1949 search.\r
1950 @param SectionData Updated upon return to point to the\r
1951 section found.\r
1952\r
1953 @retval EFI_SUCCESS Section was found.\r
1954 @retval EFI_NOT_FOUND Section of the specified type was not\r
1955 found. SectionData contains NULL.\r
1956**/\r
1957EFI_STATUS\r
1958EFIAPI\r
1959PeiFfsFvPpiFindSectionByType (\r
1960 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
1961 IN EFI_SECTION_TYPE SearchType,\r
1962 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1963 OUT VOID **SectionData\r
1964 )\r
1965{\r
1966 UINT32 AuthenticationStatus;\r
1967 return PeiFfsFvPpiFindSectionByType2 (This, SearchType, 0, FileHandle, SectionData, &AuthenticationStatus);\r
1968}\r
1969\r
1970/**\r
1971 Find the next matching section in the firmware file.\r
1972\r
1973 This service enables PEI modules to discover sections\r
1974 of a given instance and type within a valid file.\r
1975\r
1976 @param This Points to this instance of the\r
1977 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1978 @param SearchType A filter to find only sections of this\r
1979 type.\r
1980 @param SearchInstance A filter to find the specific instance\r
1981 of sections.\r
1982 @param FileHandle Handle of firmware file in which to\r
1983 search.\r
1984 @param SectionData Updated upon return to point to the\r
1985 section found.\r
1986 @param AuthenticationStatus Updated upon return to point to the\r
1987 authentication status for this section.\r
1988\r
1989 @retval EFI_SUCCESS Section was found.\r
1990 @retval EFI_NOT_FOUND Section of the specified type was not\r
1991 found. SectionData contains NULL.\r
1992**/\r
1993EFI_STATUS\r
1994EFIAPI\r
1995PeiFfsFvPpiFindSectionByType2 (\r
1996 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
1997 IN EFI_SECTION_TYPE SearchType,\r
1998 IN UINTN SearchInstance,\r
1999 IN EFI_PEI_FILE_HANDLE FileHandle,\r
2000 OUT VOID **SectionData,\r
2001 OUT UINT32 *AuthenticationStatus\r
2002 )\r
2003{\r
2004 EFI_STATUS Status;\r
2005 EFI_FFS_FILE_HEADER *FfsFileHeader;\r
2006 UINT32 FileSize;\r
2007 EFI_COMMON_SECTION_HEADER *Section;\r
2008 PEI_FW_VOL_INSTANCE *FwVolInstance;\r
2009 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
2010 UINTN Instance;\r
2011 UINT32 ExtractedAuthenticationStatus;\r
2012\r
2013 if (SectionData == NULL) {\r
2014 return EFI_NOT_FOUND;\r
2015 }\r
2016\r
2017 FwVolInstance = PEI_FW_VOL_INSTANCE_FROM_FV_THIS (This);\r
2018\r
2019 //\r
2020 // Retrieve the FirmwareVolume which the file resides in.\r
2021 //\r
2022 CoreFvHandle = FileHandleToVolume (FileHandle);\r
2023 if (CoreFvHandle == NULL) {\r
2024 return EFI_NOT_FOUND;\r
2025 }\r
2026\r
2027 FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);\r
2028\r
2029 if (IS_FFS_FILE2 (FfsFileHeader)) {\r
2030 ASSERT (FFS_FILE2_SIZE (FfsFileHeader) > 0x00FFFFFF);\r
2031 if (!FwVolInstance->IsFfs3Fv) {\r
2032 DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));\r
2033 return EFI_NOT_FOUND;\r
2034 }\r
2035 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));\r
2036 FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);\r
2037 } else {\r
2038 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));\r
2039 FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);\r
2040 }\r
2041\r
2042 Instance = SearchInstance + 1;\r
2043 ExtractedAuthenticationStatus = 0;\r
2044 Status = ProcessSection (\r
2045 GetPeiServicesTablePointer (),\r
2046 SearchType,\r
2047 &Instance,\r
2048 Section,\r
2049 FileSize,\r
2050 SectionData,\r
2051 &ExtractedAuthenticationStatus,\r
2052 FwVolInstance->IsFfs3Fv\r
2053 );\r
2054 if (!EFI_ERROR (Status)) {\r
2055 //\r
2056 // Inherit the authentication status.\r
2057 //\r
2058 *AuthenticationStatus = ExtractedAuthenticationStatus | CoreFvHandle->AuthenticationStatus;\r
2059 }\r
2060 return Status;\r
2061}\r
2062\r
2063/**\r
2064 Convert the handle of FV to pointer of corresponding PEI_CORE_FV_HANDLE.\r
2065\r
2066 @param FvHandle The handle of a FV.\r
2067\r
2068 @retval NULL if can not find.\r
2069 @return Pointer of corresponding PEI_CORE_FV_HANDLE.\r
2070**/\r
2071PEI_CORE_FV_HANDLE *\r
2072FvHandleToCoreHandle (\r
2073 IN EFI_PEI_FV_HANDLE FvHandle\r
2074 )\r
2075{\r
2076 UINTN Index;\r
2077 PEI_CORE_INSTANCE *PrivateData;\r
2078\r
2079 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer());\r
2080 for (Index = 0; Index < PrivateData->FvCount; Index ++) {\r
2081 if (FvHandle == PrivateData->Fv[Index].FvHandle) {\r
2082 return &PrivateData->Fv[Index];\r
2083 }\r
2084 }\r
2085\r
2086 return NULL;\r
2087}\r
2088\r
2089/**\r
2090 Get instance of PEI_CORE_FV_HANDLE for next volume according to given index.\r
2091\r
2092 This routine also will install FvInfo PPI for FV HOB in PI ways.\r
2093\r
2094 @param Private Pointer of PEI_CORE_INSTANCE\r
2095 @param Instance The index of FV want to be searched.\r
2096\r
2097 @return Instance of PEI_CORE_FV_HANDLE.\r
2098**/\r
2099PEI_CORE_FV_HANDLE *\r
2100FindNextCoreFvHandle (\r
2101 IN PEI_CORE_INSTANCE *Private,\r
2102 IN UINTN Instance\r
2103 )\r
2104{\r
2105 if (Instance >= Private->FvCount) {\r
2106 return NULL;\r
2107 }\r
2108\r
2109 return &Private->Fv[Instance];\r
2110}\r
2111\r
2112/**\r
2113 After PeiCore image is shadowed into permanent memory, all build-in FvPpi should\r
2114 be re-installed with the instance in permanent memory and all cached FvPpi pointers in\r
2115 PrivateData->Fv[] array should be fixed up to be pointed to the one in permanent\r
2116 memory.\r
2117\r
2118 @param PrivateData Pointer to PEI_CORE_INSTANCE.\r
2119**/\r
2120VOID\r
2121PeiReinitializeFv (\r
2122 IN PEI_CORE_INSTANCE *PrivateData\r
2123 )\r
2124{\r
2125 VOID *OldFfsFvPpi;\r
2126 EFI_PEI_PPI_DESCRIPTOR *OldDescriptor;\r
2127 UINTN Index;\r
2128 EFI_STATUS Status;\r
2129\r
2130 //\r
2131 // Locate old build-in Ffs2 EFI_PEI_FIRMWARE_VOLUME_PPI which\r
2132 // in flash.\r
2133 //\r
2134 Status = PeiServicesLocatePpi (\r
2135 &gEfiFirmwareFileSystem2Guid,\r
2136 0,\r
2137 &OldDescriptor,\r
2138 &OldFfsFvPpi\r
2139 );\r
2140 ASSERT_EFI_ERROR (Status);\r
2141\r
2142 //\r
2143 // Re-install the EFI_PEI_FIRMWARE_VOLUME_PPI for build-in Ffs2\r
2144 // which is shadowed from flash to permanent memory within PeiCore image.\r
2145 //\r
2146 Status = PeiServicesReInstallPpi (OldDescriptor, &mPeiFfs2FvPpiList);\r
2147 ASSERT_EFI_ERROR (Status);\r
2148\r
2149 //\r
2150 // Fixup all FvPpi pointers for the implementation in flash to permanent memory.\r
2151 //\r
2152 for (Index = 0; Index < PrivateData->FvCount; Index ++) {\r
2153 if (PrivateData->Fv[Index].FvPpi == OldFfsFvPpi) {\r
2154 PrivateData->Fv[Index].FvPpi = &mPeiFfs2FwVol.Fv;\r
2155 }\r
2156 }\r
2157\r
2158 //\r
2159 // Locate old build-in Ffs3 EFI_PEI_FIRMWARE_VOLUME_PPI which\r
2160 // in flash.\r
2161 //\r
2162 Status = PeiServicesLocatePpi (\r
2163 &gEfiFirmwareFileSystem3Guid,\r
2164 0,\r
2165 &OldDescriptor,\r
2166 &OldFfsFvPpi\r
2167 );\r
2168 ASSERT_EFI_ERROR (Status);\r
2169\r
2170 //\r
2171 // Re-install the EFI_PEI_FIRMWARE_VOLUME_PPI for build-in Ffs3\r
2172 // which is shadowed from flash to permanent memory within PeiCore image.\r
2173 //\r
2174 Status = PeiServicesReInstallPpi (OldDescriptor, &mPeiFfs3FvPpiList);\r
2175 ASSERT_EFI_ERROR (Status);\r
2176\r
2177 //\r
2178 // Fixup all FvPpi pointers for the implementation in flash to permanent memory.\r
2179 //\r
2180 for (Index = 0; Index < PrivateData->FvCount; Index ++) {\r
2181 if (PrivateData->Fv[Index].FvPpi == OldFfsFvPpi) {\r
2182 PrivateData->Fv[Index].FvPpi = &mPeiFfs3FwVol.Fv;\r
2183 }\r
2184 }\r
2185}\r
2186\r
2187/**\r
2188 Report the information for a new discovered FV in unknown third-party format.\r
2189\r
2190 If the EFI_PEI_FIRMWARE_VOLUME_PPI has not been installed for third-party FV format, but\r
2191 the FV in this format has been discovered, then this FV's information will be cached into\r
2192 PEI_CORE_INSTANCE's UnknownFvInfo array.\r
2193 Also a notification would be installed for unknown third-party FV format guid, if EFI_PEI_FIRMWARE_VOLUME_PPI\r
2194 is installed later by platform's PEIM, the original unknown third-party FV will be processed by\r
2195 using new installed EFI_PEI_FIRMWARE_VOLUME_PPI.\r
2196\r
2197 @param PrivateData Point to instance of PEI_CORE_INSTANCE\r
2198 @param FvInfo2Ppi Point to FvInfo2 PPI.\r
2199\r
2200 @retval EFI_OUT_OF_RESOURCES The FV info array in PEI_CORE_INSTANCE has no more spaces.\r
2201 @retval EFI_SUCCESS Success to add the information for unknown FV.\r
2202**/\r
2203EFI_STATUS\r
2204AddUnknownFormatFvInfo (\r
2205 IN PEI_CORE_INSTANCE *PrivateData,\r
2206 IN EFI_PEI_FIRMWARE_VOLUME_INFO2_PPI *FvInfo2Ppi\r
2207 )\r
2208{\r
2209 PEI_CORE_UNKNOW_FORMAT_FV_INFO *NewUnknownFv;\r
2210 VOID *TempPtr;\r
2211\r
2212 if (PrivateData->UnknownFvInfoCount >= PrivateData->MaxUnknownFvInfoCount) {\r
2213 //\r
2214 // Run out of room, grow the buffer.\r
2215 //\r
2216 TempPtr = AllocateZeroPool (\r
2217 sizeof (PEI_CORE_UNKNOW_FORMAT_FV_INFO) * (PrivateData->MaxUnknownFvInfoCount + FV_GROWTH_STEP)\r
2218 );\r
2219 ASSERT (TempPtr != NULL);\r
2220 CopyMem (\r
2221 TempPtr,\r
2222 PrivateData->UnknownFvInfo,\r
2223 sizeof (PEI_CORE_UNKNOW_FORMAT_FV_INFO) * PrivateData->MaxUnknownFvInfoCount\r
2224 );\r
2225 PrivateData->UnknownFvInfo = TempPtr;\r
2226 PrivateData->MaxUnknownFvInfoCount = PrivateData->MaxUnknownFvInfoCount + FV_GROWTH_STEP;\r
2227 }\r
2228\r
2229 NewUnknownFv = &PrivateData->UnknownFvInfo[PrivateData->UnknownFvInfoCount];\r
2230 PrivateData->UnknownFvInfoCount ++;\r
2231\r
2232 CopyGuid (&NewUnknownFv->FvFormat, &FvInfo2Ppi->FvFormat);\r
2233 NewUnknownFv->FvInfo = FvInfo2Ppi->FvInfo;\r
2234 NewUnknownFv->FvInfoSize = FvInfo2Ppi->FvInfoSize;\r
2235 NewUnknownFv->AuthenticationStatus = FvInfo2Ppi->AuthenticationStatus;\r
2236 NewUnknownFv->NotifyDescriptor.Flags = (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);\r
2237 NewUnknownFv->NotifyDescriptor.Guid = &NewUnknownFv->FvFormat;\r
2238 NewUnknownFv->NotifyDescriptor.Notify = ThirdPartyFvPpiNotifyCallback;\r
2239\r
2240 PeiServicesNotifyPpi (&NewUnknownFv->NotifyDescriptor);\r
2241 return EFI_SUCCESS;\r
2242}\r
2243\r
2244/**\r
2245 Find the FV information according to third-party FV format GUID.\r
2246\r
2247 This routine also will remove the FV information found by given FV format GUID from\r
2248 PrivateData->UnknownFvInfo[].\r
2249\r
2250 @param PrivateData Point to instance of PEI_CORE_INSTANCE\r
2251 @param Format Point to given FV format GUID\r
2252 @param FvInfo On return, the pointer of FV information buffer\r
2253 @param FvInfoSize On return, the size of FV information buffer.\r
2254 @param AuthenticationStatus On return, the authentication status of FV information buffer.\r
2255\r
2256 @retval EFI_NOT_FOUND The FV is not found for new installed EFI_PEI_FIRMWARE_VOLUME_PPI\r
2257 @retval EFI_SUCCESS Success to find a FV which could be processed by new installed EFI_PEI_FIRMWARE_VOLUME_PPI.\r
2258**/\r
2259EFI_STATUS\r
2260FindUnknownFormatFvInfo (\r
2261 IN PEI_CORE_INSTANCE *PrivateData,\r
2262 IN EFI_GUID *Format,\r
2263 OUT VOID **FvInfo,\r
2264 OUT UINT32 *FvInfoSize,\r
2265 OUT UINT32 *AuthenticationStatus\r
2266 )\r
2267{\r
2268 UINTN Index;\r
2269 UINTN Index2;\r
2270\r
2271 Index = 0;\r
2272 for (; Index < PrivateData->UnknownFvInfoCount; Index ++) {\r
2273 if (CompareGuid (Format, &PrivateData->UnknownFvInfo[Index].FvFormat)) {\r
2274 break;\r
2275 }\r
2276 }\r
2277\r
2278 if (Index == PrivateData->UnknownFvInfoCount) {\r
2279 return EFI_NOT_FOUND;\r
2280 }\r
2281\r
2282 *FvInfo = PrivateData->UnknownFvInfo[Index].FvInfo;\r
2283 *FvInfoSize = PrivateData->UnknownFvInfo[Index].FvInfoSize;\r
2284 *AuthenticationStatus = PrivateData->UnknownFvInfo[Index].AuthenticationStatus;\r
2285\r
2286 //\r
2287 // Remove an entry from UnknownFvInfo array.\r
2288 //\r
2289 Index2 = Index + 1;\r
2290 for (;Index2 < PrivateData->UnknownFvInfoCount; Index2 ++, Index ++) {\r
2291 CopyMem (&PrivateData->UnknownFvInfo[Index], &PrivateData->UnknownFvInfo[Index2], sizeof (PEI_CORE_UNKNOW_FORMAT_FV_INFO));\r
2292 }\r
2293 PrivateData->UnknownFvInfoCount --;\r
2294 return EFI_SUCCESS;\r
2295}\r
2296\r
2297/**\r
2298 Notification callback function for EFI_PEI_FIRMWARE_VOLUME_PPI.\r
2299\r
2300 When a EFI_PEI_FIRMWARE_VOLUME_PPI is installed to support new FV format, this\r
2301 routine is called to process all discovered FVs in this format.\r
2302\r
2303 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
2304 @param NotifyDescriptor Address of the notification descriptor data structure.\r
2305 @param Ppi Address of the PPI that was installed.\r
2306\r
2307 @retval EFI_SUCCESS The notification callback is processed correctly.\r
2308**/\r
2309EFI_STATUS\r
2310EFIAPI\r
2311ThirdPartyFvPpiNotifyCallback (\r
2312 IN EFI_PEI_SERVICES **PeiServices,\r
2313 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
2314 IN VOID *Ppi\r
2315 )\r
2316{\r
2317 PEI_CORE_INSTANCE *PrivateData;\r
2318 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
2319 VOID *FvInfo;\r
2320 UINT32 FvInfoSize;\r
2321 UINT32 AuthenticationStatus;\r
2322 EFI_STATUS Status;\r
2323 EFI_PEI_FV_HANDLE FvHandle;\r
2324 BOOLEAN IsProcessed;\r
2325 UINTN FvIndex;\r
2326 EFI_PEI_FILE_HANDLE FileHandle;\r
2327 VOID *DepexData;\r
2328 UINTN CurFvCount;\r
2329 VOID *TempPtr;\r
2330\r
2331 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
2332 FvPpi = (EFI_PEI_FIRMWARE_VOLUME_PPI*) Ppi;\r
2333\r
2334 do {\r
2335 Status = FindUnknownFormatFvInfo (PrivateData, NotifyDescriptor->Guid, &FvInfo, &FvInfoSize, &AuthenticationStatus);\r
2336 if (EFI_ERROR (Status)) {\r
2337 return EFI_SUCCESS;\r
2338 }\r
2339\r
2340 //\r
2341 // Process new found FV and get FV handle.\r
2342 //\r
2343 Status = FvPpi->ProcessVolume (FvPpi, FvInfo, FvInfoSize, &FvHandle);\r
2344 if (EFI_ERROR (Status)) {\r
2345 DEBUG ((EFI_D_ERROR, "Fail to process the FV 0x%p, FV may be corrupted!\n", FvInfo));\r
2346 continue;\r
2347 }\r
2348\r
2349 //\r
2350 // Check whether the FV has already been processed.\r
2351 //\r
2352 IsProcessed = FALSE;\r
2353 for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {\r
2354 if (PrivateData->Fv[FvIndex].FvHandle == FvHandle) {\r
2355 DEBUG ((DEBUG_INFO, "The FV %p has already been processed!\n", FvInfo));\r
2356 IsProcessed = TRUE;\r
2357 break;\r
2358 }\r
2359 }\r
2360\r
2361 if (IsProcessed) {\r
2362 continue;\r
2363 }\r
2364\r
2365 if (PrivateData->FvCount >= PrivateData->MaxFvCount) {\r
2366 //\r
2367 // Run out of room, grow the buffer.\r
2368 //\r
2369 TempPtr = AllocateZeroPool (\r
2370 sizeof (PEI_CORE_FV_HANDLE) * (PrivateData->MaxFvCount + FV_GROWTH_STEP)\r
2371 );\r
2372 ASSERT (TempPtr != NULL);\r
2373 CopyMem (\r
2374 TempPtr,\r
2375 PrivateData->Fv,\r
2376 sizeof (PEI_CORE_FV_HANDLE) * PrivateData->MaxFvCount\r
2377 );\r
2378 PrivateData->Fv = TempPtr;\r
2379 PrivateData->MaxFvCount = PrivateData->MaxFvCount + FV_GROWTH_STEP;\r
2380 }\r
2381\r
2382 //\r
2383 // Update internal PEI_CORE_FV array.\r
2384 //\r
2385 PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfo;\r
2386 PrivateData->Fv[PrivateData->FvCount].FvPpi = FvPpi;\r
2387 PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;\r
2388 PrivateData->Fv[PrivateData->FvCount].AuthenticationStatus = AuthenticationStatus;\r
2389 CurFvCount = PrivateData->FvCount;\r
2390 DEBUG ((\r
2391 EFI_D_INFO,\r
2392 "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n",\r
2393 (UINT32) CurFvCount,\r
2394 (VOID *) FvInfo,\r
2395 FvInfoSize,\r
2396 FvHandle\r
2397 ));\r
2398 PrivateData->FvCount ++;\r
2399\r
2400 //\r
2401 // Scan and process the new discovered FV for EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE\r
2402 //\r
2403 FileHandle = NULL;\r
2404 do {\r
2405 Status = FvPpi->FindFileByType (\r
2406 FvPpi,\r
2407 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,\r
2408 FvHandle,\r
2409 &FileHandle\r
2410 );\r
2411 if (!EFI_ERROR (Status)) {\r
2412 Status = FvPpi->FindSectionByType (\r
2413 FvPpi,\r
2414 EFI_SECTION_PEI_DEPEX,\r
2415 FileHandle,\r
2416 (VOID**)&DepexData\r
2417 );\r
2418 if (!EFI_ERROR (Status)) {\r
2419 if (!PeimDispatchReadiness (PeiServices, DepexData)) {\r
2420 //\r
2421 // Dependency is not satisfied.\r
2422 //\r
2423 continue;\r
2424 }\r
2425 }\r
2426\r
2427 DEBUG ((EFI_D_INFO, "Found firmware volume Image File %p in FV[%d] %p\n", FileHandle, CurFvCount, FvHandle));\r
2428 ProcessFvFile (PrivateData, &PrivateData->Fv[CurFvCount], FileHandle);\r
2429 }\r
2430 } while (FileHandle != NULL);\r
2431 } while (TRUE);\r
2432}\r