FwVol.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*++ @file
  2. A simple FV stack so the SEC can extract the SEC Core from an
  3. FV.
  4. Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <PiPei.h>
  8. #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
  9. (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
  10. EFI_FFS_FILE_STATE
  11. GetFileState (
  12. IN UINT8 ErasePolarity,
  13. IN EFI_FFS_FILE_HEADER *FfsHeader
  14. )
  15. /*++
  16. Routine Description:
  17. Returns the highest bit set of the State field
  18. Arguments:
  19. ErasePolarity - Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
  20. in the Attributes field.
  21. FfsHeader - Pointer to FFS File Header.
  22. Returns:
  23. Returns the highest bit in the State field
  24. **/
  25. {
  26. EFI_FFS_FILE_STATE FileState;
  27. EFI_FFS_FILE_STATE HighestBit;
  28. FileState = FfsHeader->State;
  29. if (ErasePolarity != 0) {
  30. FileState = (EFI_FFS_FILE_STATE) ~FileState;
  31. }
  32. HighestBit = 0x80;
  33. while (HighestBit != 0 && (HighestBit & FileState) == 0) {
  34. HighestBit >>= 1;
  35. }
  36. return HighestBit;
  37. }
  38. UINT8
  39. CalculateHeaderChecksum (
  40. IN EFI_FFS_FILE_HEADER *FileHeader
  41. )
  42. /*++
  43. Routine Description:
  44. Calculates the checksum of the header of a file.
  45. Arguments:
  46. FileHeader - Pointer to FFS File Header.
  47. Returns:
  48. Checksum of the header.
  49. **/
  50. {
  51. UINT8 *ptr;
  52. UINTN Index;
  53. UINT8 Sum;
  54. Sum = 0;
  55. ptr = (UINT8 *)FileHeader;
  56. for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
  57. Sum = (UINT8)(Sum + ptr[Index]);
  58. Sum = (UINT8)(Sum + ptr[Index + 1]);
  59. Sum = (UINT8)(Sum + ptr[Index + 2]);
  60. Sum = (UINT8)(Sum + ptr[Index + 3]);
  61. }
  62. for ( ; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
  63. Sum = (UINT8)(Sum + ptr[Index]);
  64. }
  65. //
  66. // State field (since this indicates the different state of file).
  67. //
  68. Sum = (UINT8)(Sum - FileHeader->State);
  69. //
  70. // Checksum field of the file is not part of the header checksum.
  71. //
  72. Sum = (UINT8)(Sum - FileHeader->IntegrityCheck.Checksum.File);
  73. return Sum;
  74. }
  75. EFI_STATUS
  76. SecFfsFindNextFile (
  77. IN EFI_FV_FILETYPE SearchType,
  78. IN EFI_PEI_FV_HANDLE FvHandle,
  79. IN OUT EFI_PEI_FILE_HANDLE *FileHandle
  80. )
  81. /*++
  82. Routine Description:
  83. Given the input file pointer, search for the next matching file in the
  84. FFS volume as defined by SearchType. The search starts from FileHeader inside
  85. the Firmware Volume defined by FwVolHeader.
  86. Arguments:
  87. SearchType - Filter to find only files of this type.
  88. Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
  89. FwVolHeader - Pointer to the FV header of the volume to search.
  90. This parameter must point to a valid FFS volume.
  91. FileHeader - Pointer to the current file from which to begin searching.
  92. This pointer will be updated upon return to reflect the file
  93. found.
  94. Returns:
  95. EFI_NOT_FOUND - No files matching the search criteria were found
  96. EFI_SUCCESS
  97. **/
  98. {
  99. EFI_FFS_FILE_HEADER *FfsFileHeader;
  100. UINT32 FileLength;
  101. UINT32 FileOccupiedSize;
  102. UINT32 FileOffset;
  103. UINT64 FvLength;
  104. UINT8 ErasePolarity;
  105. UINT8 FileState;
  106. EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
  107. EFI_FFS_FILE_HEADER **FileHeader;
  108. //
  109. // Convert the handle of FV to FV header for memory-mapped firmware volume
  110. //
  111. FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FvHandle;
  112. FileHeader = (EFI_FFS_FILE_HEADER **)FileHandle;
  113. FvLength = FwVolHeader->FvLength;
  114. if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
  115. ErasePolarity = 1;
  116. } else {
  117. ErasePolarity = 0;
  118. }
  119. //
  120. // If FileHeader is not specified (NULL) start with the first file in the
  121. // firmware volume. Otherwise, start from the FileHeader.
  122. //
  123. if (*FileHeader == NULL) {
  124. FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader + FwVolHeader->HeaderLength);
  125. } else {
  126. //
  127. // Length is 24 bits wide so mask upper 8 bits
  128. // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
  129. //
  130. FileLength = *(UINT32 *)(*FileHeader)->Size & 0x00FFFFFF;
  131. FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
  132. FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);
  133. }
  134. FileOffset = (UINT32)((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
  135. while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
  136. //
  137. // Get FileState which is the highest bit of the State
  138. //
  139. FileState = GetFileState (ErasePolarity, FfsFileHeader);
  140. switch (FileState) {
  141. case EFI_FILE_HEADER_INVALID:
  142. FileOffset += sizeof (EFI_FFS_FILE_HEADER);
  143. FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
  144. break;
  145. case EFI_FILE_DATA_VALID:
  146. case EFI_FILE_MARKED_FOR_UPDATE:
  147. if (CalculateHeaderChecksum (FfsFileHeader) == 0) {
  148. FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
  149. FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
  150. if ((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) {
  151. *FileHeader = FfsFileHeader;
  152. return EFI_SUCCESS;
  153. }
  154. FileOffset += FileOccupiedSize;
  155. FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
  156. } else {
  157. return EFI_NOT_FOUND;
  158. }
  159. break;
  160. case EFI_FILE_DELETED:
  161. FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
  162. FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
  163. FileOffset += FileOccupiedSize;
  164. FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
  165. break;
  166. default:
  167. return EFI_NOT_FOUND;
  168. }
  169. }
  170. return EFI_NOT_FOUND;
  171. }
  172. EFI_STATUS
  173. SecFfsFindSectionData (
  174. IN EFI_SECTION_TYPE SectionType,
  175. IN EFI_FFS_FILE_HEADER *FfsFileHeader,
  176. IN OUT VOID **SectionData
  177. )
  178. /*++
  179. Routine Description:
  180. Given the input file pointer, search for the next matching section in the
  181. FFS volume.
  182. Arguments:
  183. SearchType - Filter to find only sections of this type.
  184. FfsFileHeader - Pointer to the current file to search.
  185. SectionData - Pointer to the Section matching SectionType in FfsFileHeader.
  186. NULL if section not found
  187. Returns:
  188. EFI_NOT_FOUND - No files matching the search criteria were found
  189. EFI_SUCCESS
  190. **/
  191. {
  192. UINT32 FileSize;
  193. EFI_COMMON_SECTION_HEADER *Section;
  194. UINT32 SectionLength;
  195. UINT32 ParsedLength;
  196. //
  197. // Size is 24 bits wide so mask upper 8 bits.
  198. // Does not include FfsFileHeader header size
  199. // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
  200. //
  201. Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
  202. FileSize = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
  203. FileSize -= sizeof (EFI_FFS_FILE_HEADER);
  204. *SectionData = NULL;
  205. ParsedLength = 0;
  206. while (ParsedLength < FileSize) {
  207. if (Section->Type == SectionType) {
  208. *SectionData = (VOID *)(Section + 1);
  209. return EFI_SUCCESS;
  210. }
  211. //
  212. // Size is 24 bits wide so mask upper 8 bits.
  213. // SectionLength is adjusted it is 4 byte aligned.
  214. // Go to the next section
  215. //
  216. SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF;
  217. SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
  218. ParsedLength += SectionLength;
  219. Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
  220. }
  221. return EFI_NOT_FOUND;
  222. }