FvLib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /** @file
  2. These functions assist in parsing and manipulating a Firmware Volume.
  3. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. //
  7. // Include files
  8. //
  9. #include "FvLib.h"
  10. #include "CommonLib.h"
  11. #include "EfiUtilityMsgs.h"
  12. //
  13. // Module global variables
  14. //
  15. EFI_FIRMWARE_VOLUME_HEADER *mFvHeader = NULL;
  16. UINT32 mFvLength = 0;
  17. //
  18. // External function implementations
  19. //
  20. EFI_STATUS
  21. InitializeFvLib (
  22. IN VOID *Fv,
  23. IN UINT32 FvLength
  24. )
  25. /*++
  26. Routine Description:
  27. This initializes the FV lib with a pointer to the FV and length. It does not
  28. verify the FV in any way.
  29. Arguments:
  30. Fv Buffer containing the FV.
  31. FvLength Length of the FV
  32. Returns:
  33. EFI_SUCCESS Function Completed successfully.
  34. EFI_INVALID_PARAMETER A required parameter was NULL.
  35. --*/
  36. {
  37. //
  38. // Verify input arguments
  39. //
  40. if (Fv == NULL) {
  41. return EFI_INVALID_PARAMETER;
  42. }
  43. mFvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) Fv;
  44. mFvLength = FvLength;
  45. return EFI_SUCCESS;
  46. }
  47. EFI_STATUS
  48. GetFvHeader (
  49. OUT EFI_FIRMWARE_VOLUME_HEADER **FvHeader,
  50. OUT UINT32 *FvLength
  51. )
  52. /*++
  53. Routine Description:
  54. This function returns a pointer to the current FV and the size.
  55. Arguments:
  56. FvHeader Pointer to the FV buffer.
  57. FvLength Length of the FV
  58. Returns:
  59. EFI_SUCCESS Function Completed successfully.
  60. EFI_INVALID_PARAMETER A required parameter was NULL.
  61. EFI_ABORTED The library needs to be initialized.
  62. --*/
  63. {
  64. //
  65. // Verify library has been initialized.
  66. //
  67. if (mFvHeader == NULL || mFvLength == 0) {
  68. return EFI_ABORTED;
  69. }
  70. //
  71. // Verify input arguments
  72. //
  73. if (FvHeader == NULL) {
  74. return EFI_INVALID_PARAMETER;
  75. }
  76. *FvHeader = mFvHeader;
  77. *FvLength = mFvLength;
  78. return EFI_SUCCESS;
  79. }
  80. EFI_STATUS
  81. GetNextFile (
  82. IN EFI_FFS_FILE_HEADER *CurrentFile,
  83. OUT EFI_FFS_FILE_HEADER **NextFile
  84. )
  85. /*++
  86. Routine Description:
  87. This function returns the next file. If the current file is NULL, it returns
  88. the first file in the FV. If the function returns EFI_SUCCESS and the file
  89. pointer is NULL, then there are no more files in the FV.
  90. Arguments:
  91. CurrentFile Pointer to the current file, must be within the current FV.
  92. NextFile Pointer to the next file in the FV.
  93. Returns:
  94. EFI_SUCCESS Function completed successfully.
  95. EFI_INVALID_PARAMETER A required parameter was NULL or is out of range.
  96. EFI_ABORTED The library needs to be initialized.
  97. --*/
  98. {
  99. EFI_STATUS Status;
  100. //
  101. // Verify library has been initialized.
  102. //
  103. if (mFvHeader == NULL || mFvLength == 0) {
  104. return EFI_ABORTED;
  105. }
  106. //
  107. // Verify input arguments
  108. //
  109. if (NextFile == NULL) {
  110. return EFI_INVALID_PARAMETER;
  111. }
  112. //
  113. // Verify FV header
  114. //
  115. Status = VerifyFv (mFvHeader);
  116. if (EFI_ERROR (Status)) {
  117. return EFI_ABORTED;
  118. }
  119. //
  120. // Get first file
  121. //
  122. if (CurrentFile == NULL) {
  123. CurrentFile = (EFI_FFS_FILE_HEADER *) ((UINTN) mFvHeader + mFvHeader->HeaderLength);
  124. //
  125. // Verify file is valid
  126. //
  127. Status = VerifyFfsFile (CurrentFile);
  128. if (EFI_ERROR (Status)) {
  129. //
  130. // no files in this FV
  131. //
  132. *NextFile = NULL;
  133. return EFI_SUCCESS;
  134. } else {
  135. //
  136. // Verify file is in this FV.
  137. //
  138. if ((UINTN) CurrentFile + GetFfsFileLength(CurrentFile) > (UINTN) mFvHeader + mFvLength) {
  139. *NextFile = NULL;
  140. return EFI_SUCCESS;
  141. }
  142. *NextFile = CurrentFile;
  143. return EFI_SUCCESS;
  144. }
  145. }
  146. //
  147. // Verify current file is in range
  148. //
  149. if (((UINTN) CurrentFile < (UINTN) mFvHeader + mFvHeader->HeaderLength) ||
  150. ((UINTN) CurrentFile + GetFfsFileLength(CurrentFile) > (UINTN) mFvHeader + mFvLength)
  151. ) {
  152. return EFI_INVALID_PARAMETER;
  153. }
  154. //
  155. // Get next file, compensate for 8 byte alignment if necessary.
  156. //
  157. *NextFile = (EFI_FFS_FILE_HEADER *) ((((UINTN) CurrentFile - (UINTN) mFvHeader + GetFfsFileLength(CurrentFile) + 0x07) & (~(UINTN) 7)) + (UINT8 *) mFvHeader);
  158. //
  159. // Verify file is in this FV.
  160. //
  161. if (((UINTN) *NextFile + GetFfsHeaderLength(*NextFile) >= (UINTN) mFvHeader + mFvLength) ||
  162. ((UINTN) *NextFile + GetFfsFileLength (*NextFile) > (UINTN) mFvHeader + mFvLength)
  163. ) {
  164. *NextFile = NULL;
  165. return EFI_SUCCESS;
  166. }
  167. //
  168. // Verify file is valid
  169. //
  170. Status = VerifyFfsFile (*NextFile);
  171. if (EFI_ERROR (Status)) {
  172. //
  173. // no more files in this FV
  174. //
  175. *NextFile = NULL;
  176. return EFI_SUCCESS;
  177. }
  178. return EFI_SUCCESS;
  179. }
  180. EFI_STATUS
  181. GetFileByName (
  182. IN EFI_GUID *FileName,
  183. OUT EFI_FFS_FILE_HEADER **File
  184. )
  185. /*++
  186. Routine Description:
  187. Find a file by name. The function will return NULL if the file is not found.
  188. Arguments:
  189. FileName The GUID file name of the file to search for.
  190. File Return pointer. In the case of an error, contents are undefined.
  191. Returns:
  192. EFI_SUCCESS The function completed successfully.
  193. EFI_ABORTED An error was encountered.
  194. EFI_INVALID_PARAMETER One of the parameters was NULL.
  195. --*/
  196. {
  197. EFI_FFS_FILE_HEADER *CurrentFile;
  198. EFI_STATUS Status;
  199. CHAR8 FileGuidString[80];
  200. //
  201. // Verify library has been initialized.
  202. //
  203. if (mFvHeader == NULL || mFvLength == 0) {
  204. return EFI_ABORTED;
  205. }
  206. //
  207. // Verify input parameters
  208. //
  209. if (FileName == NULL || File == NULL) {
  210. return EFI_INVALID_PARAMETER;
  211. }
  212. //
  213. // File Guid String Name
  214. //
  215. PrintGuidToBuffer (FileName, (UINT8 *)FileGuidString, sizeof (FileGuidString), TRUE);
  216. //
  217. // Verify FV header
  218. //
  219. Status = VerifyFv (mFvHeader);
  220. if (EFI_ERROR (Status)) {
  221. return EFI_ABORTED;
  222. }
  223. //
  224. // Get the first file
  225. //
  226. Status = GetNextFile (NULL, &CurrentFile);
  227. if (EFI_ERROR (Status)) {
  228. Error (NULL, 0, 0003, "error parsing FV image", "FFS file with Guid %s can't be found", FileGuidString);
  229. return EFI_ABORTED;
  230. }
  231. //
  232. // Loop as long as we have a valid file
  233. //
  234. while (CurrentFile) {
  235. if (!CompareGuid (&CurrentFile->Name, FileName)) {
  236. *File = CurrentFile;
  237. return EFI_SUCCESS;
  238. }
  239. Status = GetNextFile (CurrentFile, &CurrentFile);
  240. if (EFI_ERROR (Status)) {
  241. Error (NULL, 0, 0003, "error parsing FV image", "FFS file with Guid %s can't be found", FileGuidString);
  242. return EFI_ABORTED;
  243. }
  244. }
  245. //
  246. // File not found in this FV.
  247. //
  248. *File = NULL;
  249. return EFI_SUCCESS;
  250. }
  251. EFI_STATUS
  252. GetFileByType (
  253. IN EFI_FV_FILETYPE FileType,
  254. IN UINTN Instance,
  255. OUT EFI_FFS_FILE_HEADER **File
  256. )
  257. /*++
  258. Routine Description:
  259. Find a file by type and instance. An instance of 1 is the first instance.
  260. The function will return NULL if a matching file cannot be found.
  261. File type EFI_FV_FILETYPE_ALL means any file type is valid.
  262. Arguments:
  263. FileType Type of file to search for.
  264. Instance Instance of the file type to return.
  265. File Return pointer. In the case of an error, contents are undefined.
  266. Returns:
  267. EFI_SUCCESS The function completed successfully.
  268. EFI_ABORTED An error was encountered.
  269. EFI_INVALID_PARAMETER One of the parameters was NULL.
  270. --*/
  271. {
  272. EFI_FFS_FILE_HEADER *CurrentFile;
  273. EFI_STATUS Status;
  274. UINTN FileCount;
  275. //
  276. // Verify library has been initialized.
  277. //
  278. if (mFvHeader == NULL || mFvLength == 0) {
  279. return EFI_ABORTED;
  280. }
  281. //
  282. // Verify input parameters
  283. //
  284. if (File == NULL) {
  285. return EFI_INVALID_PARAMETER;
  286. }
  287. //
  288. // Verify FV header
  289. //
  290. Status = VerifyFv (mFvHeader);
  291. if (EFI_ERROR (Status)) {
  292. return EFI_ABORTED;
  293. }
  294. //
  295. // Initialize the number of matching files found.
  296. //
  297. FileCount = 0;
  298. //
  299. // Get the first file
  300. //
  301. Status = GetNextFile (NULL, &CurrentFile);
  302. if (EFI_ERROR (Status)) {
  303. Error (NULL, 0, 0003, "error parsing FV image", "FFS file with FileType 0x%x can't be found", FileType);
  304. return EFI_ABORTED;
  305. }
  306. //
  307. // Loop as long as we have a valid file
  308. //
  309. while (CurrentFile) {
  310. if (FileType == EFI_FV_FILETYPE_ALL || CurrentFile->Type == FileType) {
  311. FileCount++;
  312. }
  313. if (FileCount == Instance) {
  314. *File = CurrentFile;
  315. return EFI_SUCCESS;
  316. }
  317. Status = GetNextFile (CurrentFile, &CurrentFile);
  318. if (EFI_ERROR (Status)) {
  319. Error (NULL, 0, 0003, "error parsing FV image", "FFS file with FileType 0x%x can't be found", FileType);
  320. return EFI_ABORTED;
  321. }
  322. }
  323. *File = NULL;
  324. return EFI_SUCCESS;
  325. }
  326. EFI_STATUS
  327. SearchSectionByType (
  328. IN EFI_FILE_SECTION_POINTER FirstSection,
  329. IN UINT8 *SearchEnd,
  330. IN EFI_SECTION_TYPE SectionType,
  331. IN OUT UINTN *StartIndex,
  332. IN UINTN Instance,
  333. OUT EFI_FILE_SECTION_POINTER *Section
  334. )
  335. /*++
  336. Routine Description:
  337. Helper function to search a sequence of sections from the section pointed
  338. by FirstSection to SearchEnd for the Instance-th section of type SectionType.
  339. The current counter is saved in StartIndex and when the section is found, it's
  340. saved in Section. GUID-defined sections, if special processing is not required,
  341. are searched recursively in a depth-first manner.
  342. Arguments:
  343. FirstSection The first section to start searching from.
  344. SearchEnd The end address to stop search.
  345. SectionType The type of section to search.
  346. StartIndex The current counter is saved.
  347. Instance The requested n-th section number.
  348. Section The found section returned.
  349. Returns:
  350. EFI_SUCCESS The function completed successfully.
  351. EFI_NOT_FOUND The section is not found.
  352. --*/
  353. {
  354. EFI_FILE_SECTION_POINTER CurrentSection;
  355. EFI_FILE_SECTION_POINTER InnerSection;
  356. EFI_STATUS Status;
  357. UINTN SectionSize;
  358. UINT16 GuidSecAttr;
  359. UINT16 GuidDataOffset;
  360. GuidSecAttr = 0;
  361. GuidDataOffset = 0;
  362. CurrentSection = FirstSection;
  363. while ((UINTN) CurrentSection.CommonHeader < (UINTN) SearchEnd) {
  364. if (CurrentSection.CommonHeader->Type == SectionType) {
  365. (*StartIndex)++;
  366. }
  367. if (*StartIndex == Instance) {
  368. *Section = CurrentSection;
  369. return EFI_SUCCESS;
  370. }
  371. //
  372. // If the requesting section is not GUID-defined and
  373. // we find a GUID-defined section that doesn't need
  374. // special processing, go ahead to search the requesting
  375. // section inside the GUID-defined section.
  376. //
  377. if (CurrentSection.CommonHeader->Type == EFI_SECTION_GUID_DEFINED) {
  378. if (GetLength(CurrentSection.CommonHeader->Size) == 0xffffff) {
  379. GuidSecAttr = CurrentSection.GuidDefinedSection2->Attributes;
  380. GuidDataOffset = CurrentSection.GuidDefinedSection2->DataOffset;
  381. } else {
  382. GuidSecAttr = CurrentSection.GuidDefinedSection->Attributes;
  383. GuidDataOffset = CurrentSection.GuidDefinedSection->DataOffset;
  384. }
  385. }
  386. if (SectionType != EFI_SECTION_GUID_DEFINED &&
  387. CurrentSection.CommonHeader->Type == EFI_SECTION_GUID_DEFINED &&
  388. !(GuidSecAttr & EFI_GUIDED_SECTION_PROCESSING_REQUIRED)) {
  389. InnerSection.CommonHeader = (EFI_COMMON_SECTION_HEADER *)
  390. ((UINTN) CurrentSection.CommonHeader + GuidDataOffset);
  391. SectionSize = GetSectionFileLength(CurrentSection.CommonHeader);
  392. Status = SearchSectionByType (
  393. InnerSection,
  394. (UINT8 *) ((UINTN) CurrentSection.CommonHeader + SectionSize),
  395. SectionType,
  396. StartIndex,
  397. Instance,
  398. Section
  399. );
  400. if (!EFI_ERROR (Status)) {
  401. return EFI_SUCCESS;
  402. }
  403. }
  404. //
  405. // Find next section (including compensating for alignment issues.
  406. //
  407. CurrentSection.CommonHeader = (EFI_COMMON_SECTION_HEADER *) ((((UINTN) CurrentSection.CommonHeader) + GetSectionFileLength(CurrentSection.CommonHeader) + 0x03) & (~(UINTN) 3));
  408. }
  409. return EFI_NOT_FOUND;
  410. }
  411. EFI_STATUS
  412. GetSectionByType (
  413. IN EFI_FFS_FILE_HEADER *File,
  414. IN EFI_SECTION_TYPE SectionType,
  415. IN UINTN Instance,
  416. OUT EFI_FILE_SECTION_POINTER *Section
  417. )
  418. /*++
  419. Routine Description:
  420. Find a section in a file by type and instance. An instance of 1 is the first
  421. instance. The function will return NULL if a matching section cannot be found.
  422. GUID-defined sections, if special processing is not needed, are handled in a
  423. depth-first manner.
  424. Arguments:
  425. File The file to search.
  426. SectionType Type of file to search for.
  427. Instance Instance of the section to return.
  428. Section Return pointer. In the case of an error, contents are undefined.
  429. Returns:
  430. EFI_SUCCESS The function completed successfully.
  431. EFI_ABORTED An error was encountered.
  432. EFI_INVALID_PARAMETER One of the parameters was NULL.
  433. EFI_NOT_FOUND No found.
  434. --*/
  435. {
  436. EFI_FILE_SECTION_POINTER CurrentSection;
  437. EFI_STATUS Status;
  438. UINTN SectionCount;
  439. //
  440. // Verify input parameters
  441. //
  442. if (File == NULL || Instance == 0) {
  443. return EFI_INVALID_PARAMETER;
  444. }
  445. //
  446. // Verify FFS header
  447. //
  448. Status = VerifyFfsFile (File);
  449. if (EFI_ERROR (Status)) {
  450. Error (NULL, 0, 0006, "invalid FFS file", NULL);
  451. return EFI_ABORTED;
  452. }
  453. //
  454. // Initialize the number of matching sections found.
  455. //
  456. SectionCount = 0;
  457. //
  458. // Get the first section
  459. //
  460. CurrentSection.CommonHeader = (EFI_COMMON_SECTION_HEADER *) ((UINTN) File + GetFfsHeaderLength(File));
  461. //
  462. // Depth-first manner to find section file.
  463. //
  464. Status = SearchSectionByType (
  465. CurrentSection,
  466. (UINT8 *) ((UINTN) File + GetFfsFileLength (File)),
  467. SectionType,
  468. &SectionCount,
  469. Instance,
  470. Section
  471. );
  472. if (!EFI_ERROR (Status)) {
  473. return EFI_SUCCESS;
  474. } else {
  475. //
  476. // Section not found
  477. //
  478. (*Section).Code16Section = NULL;
  479. return EFI_NOT_FOUND;
  480. }
  481. }
  482. //
  483. // will not parse compressed sections
  484. //
  485. EFI_STATUS
  486. VerifyFv (
  487. IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader
  488. )
  489. /*++
  490. Routine Description:
  491. Verify the current pointer points to a valid FV header.
  492. Arguments:
  493. FvHeader Pointer to an alleged FV file.
  494. Returns:
  495. EFI_SUCCESS The FV header is valid.
  496. EFI_VOLUME_CORRUPTED The FV header is not valid.
  497. EFI_INVALID_PARAMETER A required parameter was NULL.
  498. EFI_ABORTED Operation aborted.
  499. --*/
  500. {
  501. UINT16 Checksum;
  502. //
  503. // Verify input parameters
  504. //
  505. if (FvHeader == NULL) {
  506. return EFI_INVALID_PARAMETER;
  507. }
  508. if (FvHeader->Signature != EFI_FVH_SIGNATURE) {
  509. Error (NULL, 0, 0006, "invalid FV header signature", NULL);
  510. return EFI_VOLUME_CORRUPTED;
  511. }
  512. //
  513. // Verify header checksum
  514. //
  515. Checksum = CalculateSum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
  516. if (Checksum != 0) {
  517. Error (NULL, 0, 0006, "invalid FV header checksum", NULL);
  518. return EFI_ABORTED;
  519. }
  520. return EFI_SUCCESS;
  521. }
  522. EFI_STATUS
  523. VerifyFfsFile (
  524. IN EFI_FFS_FILE_HEADER *FfsHeader
  525. )
  526. /*++
  527. Routine Description:
  528. Verify the current pointer points to a FFS file header.
  529. Arguments:
  530. FfsHeader Pointer to an alleged FFS file.
  531. Returns:
  532. EFI_SUCCESS The Ffs header is valid.
  533. EFI_NOT_FOUND This "file" is the beginning of free space.
  534. EFI_VOLUME_CORRUPTED The Ffs header is not valid.
  535. EFI_ABORTED The erase polarity is not known.
  536. --*/
  537. {
  538. BOOLEAN ErasePolarity;
  539. EFI_STATUS Status;
  540. EFI_FFS_FILE_HEADER2 BlankHeader;
  541. UINT8 Checksum;
  542. UINT32 FileLength;
  543. UINT8 SavedChecksum;
  544. UINT8 SavedState;
  545. UINT8 FileGuidString[80];
  546. UINT32 FfsHeaderSize;
  547. //
  548. // Verify library has been initialized.
  549. //
  550. if (mFvHeader == NULL || mFvLength == 0) {
  551. return EFI_ABORTED;
  552. }
  553. //
  554. // Verify FV header
  555. //
  556. Status = VerifyFv (mFvHeader);
  557. if (EFI_ERROR (Status)) {
  558. return EFI_ABORTED;
  559. }
  560. //
  561. // Get the erase polarity.
  562. //
  563. Status = GetErasePolarity (&ErasePolarity);
  564. if (EFI_ERROR (Status)) {
  565. return EFI_ABORTED;
  566. }
  567. FfsHeaderSize = GetFfsHeaderLength(FfsHeader);
  568. //
  569. // Check if we have free space
  570. //
  571. if (ErasePolarity) {
  572. memset (&BlankHeader, -1, FfsHeaderSize);
  573. } else {
  574. memset (&BlankHeader, 0, FfsHeaderSize);
  575. }
  576. if (memcmp (&BlankHeader, FfsHeader, FfsHeaderSize) == 0) {
  577. return EFI_NOT_FOUND;
  578. }
  579. //
  580. // Convert the GUID to a string so we can at least report which file
  581. // if we find an error.
  582. //
  583. PrintGuidToBuffer (&FfsHeader->Name, FileGuidString, sizeof (FileGuidString), TRUE);
  584. //
  585. // Verify file header checksum
  586. //
  587. SavedState = FfsHeader->State;
  588. FfsHeader->State = 0;
  589. SavedChecksum = FfsHeader->IntegrityCheck.Checksum.File;
  590. FfsHeader->IntegrityCheck.Checksum.File = 0;
  591. Checksum = CalculateSum8 ((UINT8 *) FfsHeader, FfsHeaderSize);
  592. FfsHeader->State = SavedState;
  593. FfsHeader->IntegrityCheck.Checksum.File = SavedChecksum;
  594. if (Checksum != 0) {
  595. Error (NULL, 0, 0006, "invalid FFS file header checksum", "Ffs file with Guid %s", FileGuidString);
  596. return EFI_ABORTED;
  597. }
  598. //
  599. // Verify file checksum
  600. //
  601. if (FfsHeader->Attributes & FFS_ATTRIB_CHECKSUM) {
  602. //
  603. // Verify file data checksum
  604. //
  605. FileLength = GetFfsFileLength (FfsHeader);
  606. Checksum = CalculateSum8 ((UINT8 *) ((UINT8 *)FfsHeader + FfsHeaderSize), FileLength - FfsHeaderSize);
  607. Checksum = Checksum + FfsHeader->IntegrityCheck.Checksum.File;
  608. if (Checksum != 0) {
  609. Error (NULL, 0, 0006, "invalid FFS file checksum", "Ffs file with Guid %s", FileGuidString);
  610. return EFI_ABORTED;
  611. }
  612. } else {
  613. //
  614. // File does not have a checksum
  615. // Verify contents are 0xAA as spec'd
  616. //
  617. if (FfsHeader->IntegrityCheck.Checksum.File != FFS_FIXED_CHECKSUM) {
  618. Error (NULL, 0, 0006, "invalid fixed FFS file header checksum", "Ffs file with Guid %s", FileGuidString);
  619. return EFI_ABORTED;
  620. }
  621. }
  622. return EFI_SUCCESS;
  623. }
  624. UINT32
  625. GetFfsHeaderLength(
  626. IN EFI_FFS_FILE_HEADER *FfsHeader
  627. )
  628. {
  629. if (FfsHeader == NULL) {
  630. return 0;
  631. }
  632. if (FfsHeader->Attributes & FFS_ATTRIB_LARGE_FILE) {
  633. return sizeof(EFI_FFS_FILE_HEADER2);
  634. }
  635. return sizeof(EFI_FFS_FILE_HEADER);
  636. }
  637. UINT32
  638. GetSectionHeaderLength(
  639. IN EFI_COMMON_SECTION_HEADER *SectionHeader
  640. )
  641. {
  642. if (SectionHeader == NULL) {
  643. return 0;
  644. }
  645. if (GetLength(SectionHeader->Size) == 0xffffff) {
  646. return sizeof(EFI_COMMON_SECTION_HEADER2);
  647. }
  648. return sizeof(EFI_COMMON_SECTION_HEADER);
  649. }
  650. UINT32
  651. GetFfsFileLength (
  652. EFI_FFS_FILE_HEADER *FfsHeader
  653. )
  654. /*++
  655. Routine Description:
  656. Get FFS file length including FFS header.
  657. Arguments:
  658. FfsHeader Pointer to EFI_FFS_FILE_HEADER.
  659. Returns:
  660. UINT32 Length of FFS file header.
  661. --*/
  662. {
  663. if (FfsHeader == NULL) {
  664. return 0;
  665. }
  666. if (FfsHeader->Attributes & FFS_ATTRIB_LARGE_FILE) {
  667. return (UINT32) ((EFI_FFS_FILE_HEADER2 *)FfsHeader)->ExtendedSize;
  668. } else {
  669. return GetLength(FfsHeader->Size);
  670. }
  671. }
  672. UINT32
  673. GetSectionFileLength (
  674. EFI_COMMON_SECTION_HEADER *SectionHeader
  675. )
  676. {
  677. UINT32 Length;
  678. if (SectionHeader == NULL) {
  679. return 0;
  680. }
  681. Length = GetLength(SectionHeader->Size);
  682. if (Length == 0xffffff) {
  683. Length = ((EFI_COMMON_SECTION_HEADER2 *)SectionHeader)->ExtendedSize;
  684. }
  685. return Length;
  686. }
  687. UINT32
  688. GetLength (
  689. UINT8 *ThreeByteLength
  690. )
  691. /*++
  692. Routine Description:
  693. Converts a three byte length value into a UINT32.
  694. Arguments:
  695. ThreeByteLength Pointer to the first of the 3 byte length.
  696. Returns:
  697. UINT32 Size of the section
  698. --*/
  699. {
  700. UINT32 Length;
  701. if (ThreeByteLength == NULL) {
  702. return 0;
  703. }
  704. Length = *((UINT32 *) ThreeByteLength);
  705. Length = Length & 0x00FFFFFF;
  706. return Length;
  707. }
  708. EFI_STATUS
  709. GetErasePolarity (
  710. OUT BOOLEAN *ErasePolarity
  711. )
  712. /*++
  713. Routine Description:
  714. This function returns with the FV erase polarity. If the erase polarity
  715. for a bit is 1, the function return TRUE.
  716. Arguments:
  717. ErasePolarity A pointer to the erase polarity.
  718. Returns:
  719. EFI_SUCCESS The function completed successfully.
  720. EFI_INVALID_PARAMETER One of the input parameters was invalid.
  721. EFI_ABORTED Operation aborted.
  722. --*/
  723. {
  724. EFI_STATUS Status;
  725. //
  726. // Verify library has been initialized.
  727. //
  728. if (mFvHeader == NULL || mFvLength == 0) {
  729. return EFI_ABORTED;
  730. }
  731. //
  732. // Verify FV header
  733. //
  734. Status = VerifyFv (mFvHeader);
  735. if (EFI_ERROR (Status)) {
  736. return EFI_ABORTED;
  737. }
  738. //
  739. // Verify input parameters.
  740. //
  741. if (ErasePolarity == NULL) {
  742. return EFI_INVALID_PARAMETER;
  743. }
  744. if (mFvHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
  745. *ErasePolarity = TRUE;
  746. } else {
  747. *ErasePolarity = FALSE;
  748. }
  749. return EFI_SUCCESS;
  750. }
  751. UINT8
  752. GetFileState (
  753. IN BOOLEAN ErasePolarity,
  754. IN EFI_FFS_FILE_HEADER *FfsHeader
  755. )
  756. /*++
  757. Routine Description:
  758. This function returns a the highest state bit in the FFS that is set.
  759. It in no way validate the FFS file.
  760. Arguments:
  761. ErasePolarity The erase polarity for the file state bits.
  762. FfsHeader Pointer to a FFS file.
  763. Returns:
  764. UINT8 The hightest set state of the file.
  765. --*/
  766. {
  767. UINT8 FileState;
  768. UINT8 HighestBit;
  769. FileState = FfsHeader->State;
  770. if (ErasePolarity) {
  771. FileState = (UINT8)~FileState;
  772. }
  773. HighestBit = 0x80;
  774. while (HighestBit != 0 && (HighestBit & FileState) == 0) {
  775. HighestBit >>= 1;
  776. }
  777. return HighestBit;
  778. }