FvLib.c 23 KB

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