FwVol.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /** @file
  2. Implementation of the 6 PEI Ffs (FV) APIs in library form.
  3. This code only knows about a FV if it has a EFI_HOB_TYPE_FV entry in the HOB list
  4. Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <PrePi.h>
  8. #include <Library/ExtractGuidedSectionLib.h>
  9. #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
  10. (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
  11. /**
  12. Returns the highest bit set of the State field
  13. @param ErasePolarity Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
  14. in the Attributes field.
  15. @param FfsHeader Pointer to FFS File Header
  16. @retval the highest bit in the State field
  17. **/
  18. STATIC
  19. EFI_FFS_FILE_STATE
  20. GetFileState (
  21. IN UINT8 ErasePolarity,
  22. IN EFI_FFS_FILE_HEADER *FfsHeader
  23. )
  24. {
  25. EFI_FFS_FILE_STATE FileState;
  26. EFI_FFS_FILE_STATE HighestBit;
  27. FileState = FfsHeader->State;
  28. if (ErasePolarity != 0) {
  29. FileState = (EFI_FFS_FILE_STATE) ~FileState;
  30. }
  31. HighestBit = 0x80;
  32. while (HighestBit != 0 && (HighestBit & FileState) == 0) {
  33. HighestBit >>= 1;
  34. }
  35. return HighestBit;
  36. }
  37. /**
  38. Calculates the checksum of the header of a file.
  39. The header is a zero byte checksum, so zero means header is good
  40. @param FfsHeader Pointer to FFS File Header
  41. @retval Checksum of the header
  42. **/
  43. STATIC
  44. UINT8
  45. CalculateHeaderChecksum (
  46. IN EFI_FFS_FILE_HEADER *FileHeader
  47. )
  48. {
  49. UINT8 *Ptr;
  50. UINTN Index;
  51. UINT8 Sum;
  52. Sum = 0;
  53. Ptr = (UINT8 *)FileHeader;
  54. for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
  55. Sum = (UINT8)(Sum + Ptr[Index]);
  56. Sum = (UINT8)(Sum + Ptr[Index+1]);
  57. Sum = (UINT8)(Sum + Ptr[Index+2]);
  58. Sum = (UINT8)(Sum + Ptr[Index+3]);
  59. }
  60. for ( ; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
  61. Sum = (UINT8)(Sum + Ptr[Index]);
  62. }
  63. //
  64. // State field (since this indicates the different state of file).
  65. //
  66. Sum = (UINT8)(Sum - FileHeader->State);
  67. //
  68. // Checksum field of the file is not part of the header checksum.
  69. //
  70. Sum = (UINT8)(Sum - FileHeader->IntegrityCheck.Checksum.File);
  71. return Sum;
  72. }
  73. /**
  74. Given a FileHandle return the VolumeHandle
  75. @param FileHandle File handle to look up
  76. @param VolumeHandle Match for FileHandle
  77. @retval TRUE VolumeHandle is valid
  78. **/
  79. STATIC
  80. BOOLEAN
  81. EFIAPI
  82. FileHandleToVolume (
  83. IN EFI_PEI_FILE_HANDLE FileHandle,
  84. OUT EFI_PEI_FV_HANDLE *VolumeHandle
  85. )
  86. {
  87. EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
  88. EFI_PEI_HOB_POINTERS Hob;
  89. Hob.Raw = GetHobList ();
  90. if (Hob.Raw == NULL) {
  91. return FALSE;
  92. }
  93. do {
  94. Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw);
  95. if (Hob.Raw != NULL) {
  96. FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(Hob.FirmwareVolume->BaseAddress);
  97. if (((UINT64)(UINTN)FileHandle > (UINT64)(UINTN)FwVolHeader) && \
  98. ((UINT64)(UINTN)FileHandle <= ((UINT64)(UINTN)FwVolHeader + FwVolHeader->FvLength - 1)))
  99. {
  100. *VolumeHandle = (EFI_PEI_FV_HANDLE)FwVolHeader;
  101. return TRUE;
  102. }
  103. Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, GET_NEXT_HOB (Hob));
  104. }
  105. } while (Hob.Raw != NULL);
  106. return FALSE;
  107. }
  108. /**
  109. Given the input file pointer, search for the next matching file in the
  110. FFS volume as defined by SearchType. The search starts from FileHeader inside
  111. the Firmware Volume defined by FwVolHeader.
  112. @param FileHandle File handle to look up
  113. @param VolumeHandle Match for FileHandle
  114. **/
  115. EFI_STATUS
  116. FindFileEx (
  117. IN CONST EFI_PEI_FV_HANDLE FvHandle,
  118. IN CONST EFI_GUID *FileName OPTIONAL,
  119. IN EFI_FV_FILETYPE SearchType,
  120. IN OUT EFI_PEI_FILE_HANDLE *FileHandle
  121. )
  122. {
  123. EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
  124. EFI_FFS_FILE_HEADER **FileHeader;
  125. EFI_FFS_FILE_HEADER *FfsFileHeader;
  126. EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo;
  127. UINT32 FileLength;
  128. UINT32 FileOccupiedSize;
  129. UINT32 FileOffset;
  130. UINT64 FvLength;
  131. UINT8 ErasePolarity;
  132. UINT8 FileState;
  133. FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FvHandle;
  134. FileHeader = (EFI_FFS_FILE_HEADER **)FileHandle;
  135. FvLength = FwVolHeader->FvLength;
  136. if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
  137. ErasePolarity = 1;
  138. } else {
  139. ErasePolarity = 0;
  140. }
  141. //
  142. // If FileHeader is not specified (NULL) or FileName is not NULL,
  143. // start with the first file in the firmware volume. Otherwise,
  144. // start from the FileHeader.
  145. //
  146. if ((*FileHeader == NULL) || (FileName != NULL)) {
  147. FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader + FwVolHeader->HeaderLength);
  148. if (FwVolHeader->ExtHeaderOffset != 0) {
  149. FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)(((UINT8 *)FwVolHeader) + FwVolHeader->ExtHeaderOffset);
  150. FfsFileHeader = (EFI_FFS_FILE_HEADER *)(((UINT8 *)FwVolExHeaderInfo) + FwVolExHeaderInfo->ExtHeaderSize);
  151. }
  152. } else {
  153. //
  154. // Length is 24 bits wide so mask upper 8 bits
  155. // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
  156. //
  157. FileLength = *(UINT32 *)(*FileHeader)->Size & 0x00FFFFFF;
  158. FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
  159. FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);
  160. }
  161. // FFS files begin with a header that is aligned on an 8-byte boundary
  162. FfsFileHeader = ALIGN_POINTER (FfsFileHeader, 8);
  163. FileOffset = (UINT32)((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
  164. ASSERT (FileOffset <= 0xFFFFFFFF);
  165. while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
  166. //
  167. // Get FileState which is the highest bit of the State
  168. //
  169. FileState = GetFileState (ErasePolarity, FfsFileHeader);
  170. switch (FileState) {
  171. case EFI_FILE_HEADER_INVALID:
  172. FileOffset += sizeof (EFI_FFS_FILE_HEADER);
  173. FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
  174. break;
  175. case EFI_FILE_DATA_VALID:
  176. case EFI_FILE_MARKED_FOR_UPDATE:
  177. if (CalculateHeaderChecksum (FfsFileHeader) != 0) {
  178. ASSERT (FALSE);
  179. *FileHeader = NULL;
  180. return EFI_NOT_FOUND;
  181. }
  182. FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
  183. FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
  184. if (FileName != NULL) {
  185. if (CompareGuid (&FfsFileHeader->Name, (EFI_GUID *)FileName)) {
  186. *FileHeader = FfsFileHeader;
  187. return EFI_SUCCESS;
  188. }
  189. } else if (((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) &&
  190. (FfsFileHeader->Type != EFI_FV_FILETYPE_FFS_PAD))
  191. {
  192. *FileHeader = FfsFileHeader;
  193. return EFI_SUCCESS;
  194. }
  195. FileOffset += FileOccupiedSize;
  196. FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
  197. break;
  198. case EFI_FILE_DELETED:
  199. FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
  200. FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
  201. FileOffset += FileOccupiedSize;
  202. FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
  203. break;
  204. default:
  205. *FileHeader = NULL;
  206. return EFI_NOT_FOUND;
  207. }
  208. }
  209. *FileHeader = NULL;
  210. return EFI_NOT_FOUND;
  211. }
  212. /**
  213. Go through the file to search SectionType section,
  214. when meeting an encapsuled section.
  215. @param SectionType - Filter to find only section of this type.
  216. @param Section - From where to search.
  217. @param SectionSize - The file size to search.
  218. @param OutputBuffer - Pointer to the section to search.
  219. @retval EFI_SUCCESS
  220. **/
  221. EFI_STATUS
  222. FfsProcessSection (
  223. IN EFI_SECTION_TYPE SectionType,
  224. IN EFI_COMMON_SECTION_HEADER *Section,
  225. IN UINTN SectionSize,
  226. OUT VOID **OutputBuffer
  227. )
  228. {
  229. EFI_STATUS Status;
  230. UINT32 SectionLength;
  231. UINT32 ParsedLength;
  232. EFI_COMPRESSION_SECTION *CompressionSection;
  233. EFI_COMPRESSION_SECTION2 *CompressionSection2;
  234. UINT32 DstBufferSize;
  235. VOID *ScratchBuffer;
  236. UINT32 ScratchBufferSize;
  237. VOID *DstBuffer;
  238. UINT16 SectionAttribute;
  239. UINT32 AuthenticationStatus;
  240. CHAR8 *CompressedData;
  241. UINTN CompressedDataLength;
  242. *OutputBuffer = NULL;
  243. ParsedLength = 0;
  244. Status = EFI_NOT_FOUND;
  245. while (ParsedLength < SectionSize) {
  246. if (IS_SECTION2 (Section)) {
  247. ASSERT (SECTION2_SIZE (Section) > 0x00FFFFFF);
  248. }
  249. if (Section->Type == SectionType) {
  250. if (IS_SECTION2 (Section)) {
  251. *OutputBuffer = (VOID *)((UINT8 *)Section + sizeof (EFI_COMMON_SECTION_HEADER2));
  252. } else {
  253. *OutputBuffer = (VOID *)((UINT8 *)Section + sizeof (EFI_COMMON_SECTION_HEADER));
  254. }
  255. return EFI_SUCCESS;
  256. } else if ((Section->Type == EFI_SECTION_COMPRESSION) || (Section->Type == EFI_SECTION_GUID_DEFINED)) {
  257. if (Section->Type == EFI_SECTION_COMPRESSION) {
  258. if (IS_SECTION2 (Section)) {
  259. CompressionSection2 = (EFI_COMPRESSION_SECTION2 *)Section;
  260. SectionLength = SECTION2_SIZE (Section);
  261. if (CompressionSection2->CompressionType != EFI_STANDARD_COMPRESSION) {
  262. return EFI_UNSUPPORTED;
  263. }
  264. CompressedData = (CHAR8 *)((EFI_COMPRESSION_SECTION2 *)Section + 1);
  265. CompressedDataLength = (UINT32)SectionLength - sizeof (EFI_COMPRESSION_SECTION2);
  266. } else {
  267. CompressionSection = (EFI_COMPRESSION_SECTION *)Section;
  268. SectionLength = SECTION_SIZE (Section);
  269. if (CompressionSection->CompressionType != EFI_STANDARD_COMPRESSION) {
  270. return EFI_UNSUPPORTED;
  271. }
  272. CompressedData = (CHAR8 *)((EFI_COMPRESSION_SECTION *)Section + 1);
  273. CompressedDataLength = (UINT32)SectionLength - sizeof (EFI_COMPRESSION_SECTION);
  274. }
  275. Status = UefiDecompressGetInfo (
  276. CompressedData,
  277. CompressedDataLength,
  278. &DstBufferSize,
  279. &ScratchBufferSize
  280. );
  281. } else if (Section->Type == EFI_SECTION_GUID_DEFINED) {
  282. Status = ExtractGuidedSectionGetInfo (
  283. Section,
  284. &DstBufferSize,
  285. &ScratchBufferSize,
  286. &SectionAttribute
  287. );
  288. }
  289. if (EFI_ERROR (Status)) {
  290. //
  291. // GetInfo failed
  292. //
  293. DEBUG ((DEBUG_ERROR, "Decompress GetInfo Failed - %r\n", Status));
  294. return EFI_NOT_FOUND;
  295. }
  296. //
  297. // Allocate scratch buffer
  298. //
  299. ScratchBuffer = (VOID *)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));
  300. if (ScratchBuffer == NULL) {
  301. return EFI_OUT_OF_RESOURCES;
  302. }
  303. //
  304. // Allocate destination buffer, extra one page for adjustment
  305. //
  306. DstBuffer = (VOID *)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize) + 1);
  307. if (DstBuffer == NULL) {
  308. return EFI_OUT_OF_RESOURCES;
  309. }
  310. //
  311. // DstBuffer still is one section. Adjust DstBuffer offset, skip EFI section header
  312. // to make section data at page alignment.
  313. //
  314. if (IS_SECTION2 (Section)) {
  315. DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER2);
  316. } else {
  317. DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER);
  318. }
  319. //
  320. // Call decompress function
  321. //
  322. if (Section->Type == EFI_SECTION_COMPRESSION) {
  323. if (IS_SECTION2 (Section)) {
  324. CompressedData = (CHAR8 *)((EFI_COMPRESSION_SECTION2 *)Section + 1);
  325. } else {
  326. CompressedData = (CHAR8 *)((EFI_COMPRESSION_SECTION *)Section + 1);
  327. }
  328. Status = UefiDecompress (
  329. CompressedData,
  330. DstBuffer,
  331. ScratchBuffer
  332. );
  333. } else if (Section->Type == EFI_SECTION_GUID_DEFINED) {
  334. Status = ExtractGuidedSectionDecode (
  335. Section,
  336. &DstBuffer,
  337. ScratchBuffer,
  338. &AuthenticationStatus
  339. );
  340. }
  341. if (EFI_ERROR (Status)) {
  342. //
  343. // Decompress failed
  344. //
  345. DEBUG ((DEBUG_ERROR, "Decompress Failed - %r\n", Status));
  346. return EFI_NOT_FOUND;
  347. } else {
  348. return FfsProcessSection (
  349. SectionType,
  350. DstBuffer,
  351. DstBufferSize,
  352. OutputBuffer
  353. );
  354. }
  355. }
  356. if (IS_SECTION2 (Section)) {
  357. SectionLength = SECTION2_SIZE (Section);
  358. } else {
  359. SectionLength = SECTION_SIZE (Section);
  360. }
  361. //
  362. // SectionLength is adjusted it is 4 byte aligned.
  363. // Go to the next section
  364. //
  365. SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
  366. ASSERT (SectionLength != 0);
  367. ParsedLength += SectionLength;
  368. Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
  369. }
  370. return EFI_NOT_FOUND;
  371. }
  372. /**
  373. This service enables discovery sections of a given type within a valid FFS file.
  374. @param SearchType The value of the section type to find.
  375. @param FfsFileHeader A pointer to the file header that contains the set of sections to
  376. be searched.
  377. @param SectionData A pointer to the discovered section, if successful.
  378. @retval EFI_SUCCESS The section was found.
  379. @retval EFI_NOT_FOUND The section was not found.
  380. **/
  381. EFI_STATUS
  382. EFIAPI
  383. FfsFindSectionData (
  384. IN EFI_SECTION_TYPE SectionType,
  385. IN EFI_PEI_FILE_HANDLE FileHandle,
  386. OUT VOID **SectionData
  387. )
  388. {
  389. EFI_FFS_FILE_HEADER *FfsFileHeader;
  390. UINT32 FileSize;
  391. EFI_COMMON_SECTION_HEADER *Section;
  392. FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);
  393. //
  394. // Size is 24 bits wide so mask upper 8 bits.
  395. // Does not include FfsFileHeader header size
  396. // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
  397. //
  398. Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
  399. FileSize = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
  400. FileSize -= sizeof (EFI_FFS_FILE_HEADER);
  401. return FfsProcessSection (
  402. SectionType,
  403. Section,
  404. FileSize,
  405. SectionData
  406. );
  407. }
  408. /**
  409. This service enables discovery of additional firmware files.
  410. @param SearchType A filter to find files only of this type.
  411. @param FwVolHeader Pointer to the firmware volume header of the volume to search.
  412. This parameter must point to a valid FFS volume.
  413. @param FileHeader Pointer to the current file from which to begin searching.
  414. @retval EFI_SUCCESS The file was found.
  415. @retval EFI_NOT_FOUND The file was not found.
  416. @retval EFI_NOT_FOUND The header checksum was not zero.
  417. **/
  418. EFI_STATUS
  419. EFIAPI
  420. FfsFindNextFile (
  421. IN UINT8 SearchType,
  422. IN EFI_PEI_FV_HANDLE VolumeHandle,
  423. IN OUT EFI_PEI_FILE_HANDLE *FileHandle
  424. )
  425. {
  426. return FindFileEx (VolumeHandle, NULL, SearchType, FileHandle);
  427. }
  428. /**
  429. This service enables discovery of additional firmware volumes.
  430. @param Instance This instance of the firmware volume to find. The value 0 is the
  431. Boot Firmware Volume (BFV).
  432. @param FwVolHeader Pointer to the firmware volume header of the volume to return.
  433. @retval EFI_SUCCESS The volume was found.
  434. @retval EFI_NOT_FOUND The volume was not found.
  435. **/
  436. EFI_STATUS
  437. EFIAPI
  438. FfsFindNextVolume (
  439. IN UINTN Instance,
  440. IN OUT EFI_PEI_FV_HANDLE *VolumeHandle
  441. )
  442. {
  443. EFI_PEI_HOB_POINTERS Hob;
  444. Hob.Raw = GetHobList ();
  445. if (Hob.Raw == NULL) {
  446. return EFI_NOT_FOUND;
  447. }
  448. do {
  449. Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw);
  450. if (Hob.Raw != NULL) {
  451. if (Instance-- == 0) {
  452. *VolumeHandle = (EFI_PEI_FV_HANDLE)(UINTN)(Hob.FirmwareVolume->BaseAddress);
  453. return EFI_SUCCESS;
  454. }
  455. Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, GET_NEXT_HOB (Hob));
  456. }
  457. } while (Hob.Raw != NULL);
  458. return EFI_NOT_FOUND;
  459. }
  460. /**
  461. Find a file in the volume by name
  462. @param FileName A pointer to the name of the file to
  463. find within the firmware volume.
  464. @param VolumeHandle The firmware volume to search FileHandle
  465. Upon exit, points to the found file's
  466. handle or NULL if it could not be found.
  467. @retval EFI_SUCCESS File was found.
  468. @retval EFI_NOT_FOUND File was not found.
  469. @retval EFI_INVALID_PARAMETER VolumeHandle or FileHandle or
  470. FileName was NULL.
  471. **/
  472. EFI_STATUS
  473. EFIAPI
  474. FfsFindFileByName (
  475. IN CONST EFI_GUID *FileName,
  476. IN EFI_PEI_FV_HANDLE VolumeHandle,
  477. OUT EFI_PEI_FILE_HANDLE *FileHandle
  478. )
  479. {
  480. EFI_STATUS Status;
  481. if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {
  482. return EFI_INVALID_PARAMETER;
  483. }
  484. Status = FindFileEx (VolumeHandle, FileName, 0, FileHandle);
  485. if (Status == EFI_NOT_FOUND) {
  486. *FileHandle = NULL;
  487. }
  488. return Status;
  489. }
  490. /**
  491. Get information about the file by name.
  492. @param FileHandle Handle of the file.
  493. @param FileInfo Upon exit, points to the file's
  494. information.
  495. @retval EFI_SUCCESS File information returned.
  496. @retval EFI_INVALID_PARAMETER If FileHandle does not
  497. represent a valid file.
  498. @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
  499. **/
  500. EFI_STATUS
  501. EFIAPI
  502. FfsGetFileInfo (
  503. IN EFI_PEI_FILE_HANDLE FileHandle,
  504. OUT EFI_FV_FILE_INFO *FileInfo
  505. )
  506. {
  507. UINT8 FileState;
  508. UINT8 ErasePolarity;
  509. EFI_FFS_FILE_HEADER *FileHeader;
  510. EFI_PEI_FV_HANDLE VolumeHandle;
  511. if ((FileHandle == NULL) || (FileInfo == NULL)) {
  512. return EFI_INVALID_PARAMETER;
  513. }
  514. VolumeHandle = 0;
  515. //
  516. // Retrieve the FirmwareVolume which the file resides in.
  517. //
  518. if (!FileHandleToVolume (FileHandle, &VolumeHandle)) {
  519. return EFI_INVALID_PARAMETER;
  520. }
  521. if (((EFI_FIRMWARE_VOLUME_HEADER *)VolumeHandle)->Attributes & EFI_FVB2_ERASE_POLARITY) {
  522. ErasePolarity = 1;
  523. } else {
  524. ErasePolarity = 0;
  525. }
  526. //
  527. // Get FileState which is the highest bit of the State
  528. //
  529. FileState = GetFileState (ErasePolarity, (EFI_FFS_FILE_HEADER *)FileHandle);
  530. switch (FileState) {
  531. case EFI_FILE_DATA_VALID:
  532. case EFI_FILE_MARKED_FOR_UPDATE:
  533. break;
  534. default:
  535. return EFI_INVALID_PARAMETER;
  536. }
  537. FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle;
  538. CopyMem (&FileInfo->FileName, &FileHeader->Name, sizeof (EFI_GUID));
  539. FileInfo->FileType = FileHeader->Type;
  540. FileInfo->FileAttributes = FileHeader->Attributes;
  541. FileInfo->BufferSize = ((*(UINT32 *)FileHeader->Size) & 0x00FFFFFF) - sizeof (EFI_FFS_FILE_HEADER);
  542. FileInfo->Buffer = (FileHeader + 1);
  543. return EFI_SUCCESS;
  544. }
  545. /**
  546. Get Information about the volume by name
  547. @param VolumeHandle Handle of the volume.
  548. @param VolumeInfo Upon exit, points to the volume's
  549. information.
  550. @retval EFI_SUCCESS File information returned.
  551. @retval EFI_INVALID_PARAMETER If FileHandle does not
  552. represent a valid file.
  553. @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
  554. **/
  555. EFI_STATUS
  556. EFIAPI
  557. FfsGetVolumeInfo (
  558. IN EFI_PEI_FV_HANDLE VolumeHandle,
  559. OUT EFI_FV_INFO *VolumeInfo
  560. )
  561. {
  562. EFI_FIRMWARE_VOLUME_HEADER FwVolHeader;
  563. EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo;
  564. if (VolumeInfo == NULL) {
  565. return EFI_INVALID_PARAMETER;
  566. }
  567. //
  568. // VolumeHandle may not align at 8 byte,
  569. // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte.
  570. // So, Copy FvHeader into the local FvHeader structure.
  571. //
  572. CopyMem (&FwVolHeader, VolumeHandle, sizeof (EFI_FIRMWARE_VOLUME_HEADER));
  573. //
  574. // Check Fv Image Signature
  575. //
  576. if (FwVolHeader.Signature != EFI_FVH_SIGNATURE) {
  577. return EFI_INVALID_PARAMETER;
  578. }
  579. VolumeInfo->FvAttributes = FwVolHeader.Attributes;
  580. VolumeInfo->FvStart = (VOID *)VolumeHandle;
  581. VolumeInfo->FvSize = FwVolHeader.FvLength;
  582. CopyMem (&VolumeInfo->FvFormat, &FwVolHeader.FileSystemGuid, sizeof (EFI_GUID));
  583. if (FwVolHeader.ExtHeaderOffset != 0) {
  584. FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)(((UINT8 *)VolumeHandle) + FwVolHeader.ExtHeaderOffset);
  585. CopyMem (&VolumeInfo->FvName, &FwVolExHeaderInfo->FvName, sizeof (EFI_GUID));
  586. }
  587. return EFI_SUCCESS;
  588. }
  589. /**
  590. Search through every FV until you find a file of type FileType
  591. @param FileType File handle of a Fv type file.
  592. @param Volumehandle On success Volume Handle of the match
  593. @param FileHandle On success File Handle of the match
  594. @retval EFI_NOT_FOUND FV image can't be found.
  595. @retval EFI_SUCCESS Successfully found FileType
  596. **/
  597. EFI_STATUS
  598. EFIAPI
  599. FfsAnyFvFindFirstFile (
  600. IN EFI_FV_FILETYPE FileType,
  601. OUT EFI_PEI_FV_HANDLE *VolumeHandle,
  602. OUT EFI_PEI_FILE_HANDLE *FileHandle
  603. )
  604. {
  605. EFI_STATUS Status;
  606. UINTN Instance;
  607. //
  608. // Search every FV for the DXE Core
  609. //
  610. Instance = 0;
  611. *FileHandle = NULL;
  612. while (1) {
  613. Status = FfsFindNextVolume (Instance++, VolumeHandle);
  614. if (EFI_ERROR (Status)) {
  615. break;
  616. }
  617. Status = FfsFindNextFile (FileType, *VolumeHandle, FileHandle);
  618. if (!EFI_ERROR (Status)) {
  619. break;
  620. }
  621. }
  622. return Status;
  623. }
  624. /**
  625. Get Fv image from the FV type file, then add FV & FV2 Hob.
  626. @param FileHandle File handle of a Fv type file.
  627. @retval EFI_NOT_FOUND FV image can't be found.
  628. @retval EFI_SUCCESS Successfully to process it.
  629. **/
  630. EFI_STATUS
  631. EFIAPI
  632. FfsProcessFvFile (
  633. IN EFI_PEI_FILE_HANDLE FvFileHandle
  634. )
  635. {
  636. EFI_STATUS Status;
  637. EFI_PEI_FV_HANDLE FvImageHandle;
  638. EFI_FV_INFO FvImageInfo;
  639. UINT32 FvAlignment;
  640. VOID *FvBuffer;
  641. EFI_PEI_HOB_POINTERS HobFv2;
  642. FvBuffer = NULL;
  643. //
  644. // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already
  645. // been extracted.
  646. //
  647. HobFv2.Raw = GetHobList ();
  648. while ((HobFv2.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobFv2.Raw)) != NULL) {
  649. if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name), &HobFv2.FirmwareVolume2->FileName)) {
  650. //
  651. // this FILE has been dispatched, it will not be dispatched again.
  652. //
  653. return EFI_SUCCESS;
  654. }
  655. HobFv2.Raw = GET_NEXT_HOB (HobFv2);
  656. }
  657. //
  658. // Find FvImage in FvFile
  659. //
  660. Status = FfsFindSectionData (EFI_SECTION_FIRMWARE_VOLUME_IMAGE, FvFileHandle, (VOID **)&FvImageHandle);
  661. if (EFI_ERROR (Status)) {
  662. return Status;
  663. }
  664. //
  665. // Collect FvImage Info.
  666. //
  667. ZeroMem (&FvImageInfo, sizeof (FvImageInfo));
  668. Status = FfsGetVolumeInfo (FvImageHandle, &FvImageInfo);
  669. ASSERT_EFI_ERROR (Status);
  670. //
  671. // FvAlignment must be more than 8 bytes required by FvHeader structure.
  672. //
  673. FvAlignment = 1 << ((FvImageInfo.FvAttributes & EFI_FVB2_ALIGNMENT) >> 16);
  674. if (FvAlignment < 8) {
  675. FvAlignment = 8;
  676. }
  677. //
  678. // Check FvImage
  679. //
  680. if ((UINTN)FvImageInfo.FvStart % FvAlignment != 0) {
  681. FvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32)FvImageInfo.FvSize), FvAlignment);
  682. if (FvBuffer == NULL) {
  683. return EFI_OUT_OF_RESOURCES;
  684. }
  685. CopyMem (FvBuffer, FvImageInfo.FvStart, (UINTN)FvImageInfo.FvSize);
  686. //
  687. // Update FvImageInfo after reload FvImage to new aligned memory
  688. //
  689. FfsGetVolumeInfo ((EFI_PEI_FV_HANDLE)FvBuffer, &FvImageInfo);
  690. }
  691. //
  692. // Inform HOB consumer phase, i.e. DXE core, the existence of this FV
  693. //
  694. BuildFvHob ((EFI_PHYSICAL_ADDRESS)(UINTN)FvImageInfo.FvStart, FvImageInfo.FvSize);
  695. //
  696. // Makes the encapsulated volume show up in DXE phase to skip processing of
  697. // encapsulated file again.
  698. //
  699. BuildFv2Hob (
  700. (EFI_PHYSICAL_ADDRESS)(UINTN)FvImageInfo.FvStart,
  701. FvImageInfo.FvSize,
  702. &FvImageInfo.FvName,
  703. &(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name)
  704. );
  705. return EFI_SUCCESS;
  706. }