]> git.proxmox.com Git - mirror_edk2.git/blame - StandaloneMmPkg/Library/FvLib/FvLib.c
StandaloneMmPkg: Apply uncrustify changes
[mirror_edk2.git] / StandaloneMmPkg / Library / FvLib / FvLib.c
CommitLineData
e85162ac
SV
1/** @file\r
2\r
3Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
4Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>\r
5\r
86094561 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
e85162ac
SV
7\r
8**/\r
9\r
10#include <Library/FvLib.h>\r
11\r
12#include <Library/BaseLib.h>\r
13#include <Library/BaseMemoryLib.h>\r
14#include <Library/DebugLib.h>\r
15\r
16#define GET_OCCUPIED_SIZE(ActualSize, Alignment) \\r
17 (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))\r
18\r
19/**\r
20 Returns the highest bit set of the State field\r
21\r
22 @param ErasePolarity Erase Polarity as defined by EFI_FVB_ERASE_POLARITY\r
23 in the Attributes field.\r
24 @param FfsHeader Pointer to FFS File Header.\r
25\r
26 @return the highest bit in the State field\r
27**/\r
28EFI_FFS_FILE_STATE\r
29GetFileState (\r
30 IN UINT8 ErasePolarity,\r
31 IN EFI_FFS_FILE_HEADER *FfsHeader\r
32 )\r
33{\r
34 EFI_FFS_FILE_STATE FileState;\r
35 EFI_FFS_FILE_STATE HighestBit;\r
36\r
37 FileState = FfsHeader->State;\r
38\r
39 if (ErasePolarity != 0) {\r
91415a36 40 FileState = (EFI_FFS_FILE_STATE) ~FileState;\r
e85162ac
SV
41 }\r
42\r
43 HighestBit = 0x80;\r
44 while (HighestBit != 0 && (HighestBit & FileState) == 0) {\r
45 HighestBit >>= 1;\r
46 }\r
47\r
48 return HighestBit;\r
49}\r
50\r
51/**\r
52 Calculates the checksum of the header of a file.\r
53\r
54 @param FileHeader Pointer to FFS File Header.\r
55\r
56 @return Checksum of the header.\r
57**/\r
58UINT8\r
59CalculateHeaderChecksum (\r
60 IN EFI_FFS_FILE_HEADER *FileHeader\r
61 )\r
62{\r
91415a36
MK
63 UINT8 *ptr;\r
64 UINTN Index;\r
65 UINT8 Sum;\r
e85162ac
SV
66\r
67 Sum = 0;\r
91415a36 68 ptr = (UINT8 *)FileHeader;\r
e85162ac
SV
69\r
70 for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {\r
91415a36
MK
71 Sum = (UINT8)(Sum + ptr[Index]);\r
72 Sum = (UINT8)(Sum + ptr[Index + 1]);\r
73 Sum = (UINT8)(Sum + ptr[Index + 2]);\r
74 Sum = (UINT8)(Sum + ptr[Index + 3]);\r
e85162ac
SV
75 }\r
76\r
91415a36
MK
77 for ( ; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {\r
78 Sum = (UINT8)(Sum + ptr[Index]);\r
e85162ac 79 }\r
91415a36 80\r
e85162ac
SV
81 //\r
82 // State field (since this indicates the different state of file).\r
83 //\r
91415a36 84 Sum = (UINT8)(Sum - FileHeader->State);\r
e85162ac
SV
85 //\r
86 // Checksum field of the file is not part of the header checksum.\r
87 //\r
91415a36 88 Sum = (UINT8)(Sum - FileHeader->IntegrityCheck.Checksum.File);\r
e85162ac
SV
89\r
90 return Sum;\r
91}\r
92\r
93/**\r
94 Given the input file pointer, search for the next matching file in the\r
95 FFS volume as defined by SearchType. The search starts from FileHeader inside\r
96 the Firmware Volume defined by FwVolHeader.\r
97\r
98 @param SearchType Filter to find only files of this type.\r
99 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.\r
100 @param FwVolHeader Pointer to the FV header of the volume to search.\r
101 This parameter must point to a valid FFS volume.\r
102 @param FileHeader Pointer to the current file from which to begin searching.\r
103 This pointer will be updated upon return to reflect the file found.\r
104\r
105 @retval EFI_NOT_FOUND No files matching the search criteria were found\r
106 @retval EFI_SUCCESS\r
107**/\r
108EFI_STATUS\r
109EFIAPI\r
110FfsFindNextFile (\r
111 IN EFI_FV_FILETYPE SearchType,\r
112 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,\r
113 IN OUT EFI_FFS_FILE_HEADER **FileHeader\r
114 )\r
115{\r
91415a36 116 EFI_FIRMWARE_VOLUME_EXT_HEADER *FvExtHeader;\r
e85162ac 117\r
91415a36
MK
118 EFI_FFS_FILE_HEADER *FfsFileHeader;\r
119 UINT32 FileLength;\r
120 UINT32 FileOccupiedSize;\r
121 UINT32 FileOffset;\r
122 UINT64 FvLength;\r
123 UINT8 ErasePolarity;\r
124 UINT8 FileState;\r
e85162ac
SV
125\r
126 FvLength = FwVolHeader->FvLength;\r
127 if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {\r
128 ErasePolarity = 1;\r
129 } else {\r
130 ErasePolarity = 0;\r
131 }\r
91415a36 132\r
e85162ac
SV
133 //\r
134 // If FileHeader is not specified (NULL) start with the first file in the\r
135 // firmware volume. Otherwise, start from the FileHeader.\r
136 //\r
137 if (*FileHeader == NULL) {\r
e85162ac 138 if (FwVolHeader->ExtHeaderOffset != 0) {\r
e85162ac
SV
139 FvExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)((UINT8 *)FwVolHeader +\r
140 FwVolHeader->ExtHeaderOffset);\r
141\r
142 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FvExtHeader +\r
143 FvExtHeader->ExtHeaderSize);\r
e85162ac 144 } else {\r
e85162ac
SV
145 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader +\r
146 FwVolHeader->HeaderLength);\r
e85162ac
SV
147 }\r
148\r
149 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINTN)FwVolHeader +\r
91415a36
MK
150 ALIGN_VALUE (\r
151 (UINTN)FfsFileHeader -\r
152 (UINTN)FwVolHeader,\r
153 8\r
154 ));\r
e85162ac
SV
155 } else {\r
156 //\r
157 // Length is 24 bits wide so mask upper 8 bits\r
158 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.\r
159 //\r
91415a36
MK
160 FileLength = FFS_FILE_SIZE (*FileHeader);\r
161 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
162 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);\r
e85162ac
SV
163 }\r
164\r
91415a36 165 FileOffset = (UINT32)((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);\r
e85162ac
SV
166\r
167 while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {\r
168 //\r
169 // Get FileState which is the highest bit of the State\r
170 //\r
171 FileState = GetFileState (ErasePolarity, FfsFileHeader);\r
172\r
173 switch (FileState) {\r
91415a36
MK
174 case EFI_FILE_HEADER_INVALID:\r
175 FileOffset += sizeof (EFI_FFS_FILE_HEADER);\r
176 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));\r
177 break;\r
178\r
179 case EFI_FILE_DATA_VALID:\r
180 case EFI_FILE_MARKED_FOR_UPDATE:\r
181 if (CalculateHeaderChecksum (FfsFileHeader) == 0) {\r
182 FileLength = FFS_FILE_SIZE (FfsFileHeader);\r
183 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
184\r
185 if ((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) {\r
186 *FileHeader = FfsFileHeader;\r
187\r
188 return EFI_SUCCESS;\r
189 }\r
190\r
191 FileOffset += FileOccupiedSize;\r
192 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);\r
193 } else {\r
194 return EFI_NOT_FOUND;\r
e85162ac
SV
195 }\r
196\r
91415a36 197 break;\r
e85162ac 198\r
91415a36
MK
199 case EFI_FILE_DELETED:\r
200 FileLength = FFS_FILE_SIZE (FfsFileHeader);\r
201 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
202 FileOffset += FileOccupiedSize;\r
203 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);\r
204 break;\r
e85162ac 205\r
91415a36
MK
206 default:\r
207 return EFI_NOT_FOUND;\r
e85162ac
SV
208 }\r
209 }\r
210\r
211 return EFI_NOT_FOUND;\r
212}\r
213\r
214/**\r
215 Locates a section within a series of sections\r
216 with the specified section type.\r
217\r
218 @param[in] Sections The sections to search\r
219 @param[in] SizeOfSections Total size of all sections\r
220 @param[in] SectionType The section type to locate\r
221 @param[out] FoundSection The FFS section if found\r
222\r
223 @retval EFI_SUCCESS The file and section was found\r
224 @retval EFI_NOT_FOUND The file and section was not found\r
225 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted\r
226**/\r
227EFI_STATUS\r
228EFIAPI\r
229FindFfsSectionInSections (\r
91415a36
MK
230 IN VOID *Sections,\r
231 IN UINTN SizeOfSections,\r
232 IN EFI_SECTION_TYPE SectionType,\r
233 OUT EFI_COMMON_SECTION_HEADER **FoundSection\r
e85162ac
SV
234 )\r
235{\r
91415a36
MK
236 EFI_PHYSICAL_ADDRESS CurrentAddress;\r
237 UINT32 Size;\r
238 EFI_PHYSICAL_ADDRESS EndOfSections;\r
239 EFI_COMMON_SECTION_HEADER *Section;\r
240 EFI_PHYSICAL_ADDRESS EndOfSection;\r
e85162ac
SV
241\r
242 //\r
243 // Loop through the FFS file sections\r
244 //\r
91415a36 245 EndOfSection = (EFI_PHYSICAL_ADDRESS)(UINTN)Sections;\r
e85162ac 246 EndOfSections = EndOfSection + SizeOfSections;\r
91415a36 247 for ( ; ;) {\r
e85162ac
SV
248 if (EndOfSection == EndOfSections) {\r
249 break;\r
250 }\r
91415a36 251\r
e85162ac
SV
252 CurrentAddress = EndOfSection;\r
253\r
91415a36 254 Section = (EFI_COMMON_SECTION_HEADER *)(UINTN)CurrentAddress;\r
e85162ac
SV
255\r
256 Size = SECTION_SIZE (Section);\r
257 if (Size < sizeof (*Section)) {\r
258 return EFI_VOLUME_CORRUPTED;\r
259 }\r
260\r
261 EndOfSection = CurrentAddress + Size;\r
262 if (EndOfSection > EndOfSections) {\r
263 return EFI_VOLUME_CORRUPTED;\r
264 }\r
91415a36 265\r
e85162ac
SV
266 Size = GET_OCCUPIED_SIZE (Size, 4);\r
267\r
268 //\r
269 // Look for the requested section type\r
270 //\r
271 if (Section->Type == SectionType) {\r
272 *FoundSection = Section;\r
273 return EFI_SUCCESS;\r
274 }\r
275 }\r
276\r
277 return EFI_NOT_FOUND;\r
278}\r
279\r
280/**\r
281 Given the input file pointer, search for the next matching section in the\r
282 FFS volume.\r
283\r
284 @param SearchType Filter to find only sections of this type.\r
285 @param FfsFileHeader Pointer to the current file to search.\r
286 @param SectionHeader Pointer to the Section matching SectionType in FfsFileHeader.\r
287 NULL if section not found\r
288\r
289 @retval EFI_NOT_FOUND No files matching the search criteria were found\r
290 @retval EFI_SUCCESS\r
291**/\r
292EFI_STATUS\r
293EFIAPI\r
294FfsFindSection (\r
91415a36
MK
295 IN EFI_SECTION_TYPE SectionType,\r
296 IN EFI_FFS_FILE_HEADER *FfsFileHeader,\r
297 IN OUT EFI_COMMON_SECTION_HEADER **SectionHeader\r
e85162ac
SV
298 )\r
299{\r
91415a36
MK
300 UINT32 FileSize;\r
301 EFI_COMMON_SECTION_HEADER *Section;\r
302 EFI_STATUS Status;\r
e85162ac
SV
303\r
304 //\r
305 // Size is 24 bits wide so mask upper 8 bits.\r
306 // Does not include FfsFileHeader header size\r
307 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.\r
308 //\r
91415a36
MK
309 Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);\r
310 FileSize = FFS_FILE_SIZE (FfsFileHeader);\r
e85162ac
SV
311 FileSize -= sizeof (EFI_FFS_FILE_HEADER);\r
312\r
313 Status = FindFfsSectionInSections (\r
314 Section,\r
315 FileSize,\r
316 SectionType,\r
317 SectionHeader\r
318 );\r
319 return Status;\r
320}\r
321\r
322/**\r
323 Given the input file pointer, search for the next matching section in the\r
324 FFS volume.\r
325\r
326 @param SearchType Filter to find only sections of this type.\r
327 @param FfsFileHeader Pointer to the current file to search.\r
328 @param SectionData Pointer to the Section matching SectionType in FfsFileHeader.\r
329 NULL if section not found\r
330 @param SectionDataSize The size of SectionData\r
331\r
332 @retval EFI_NOT_FOUND No files matching the search criteria were found\r
333 @retval EFI_SUCCESS\r
334**/\r
335EFI_STATUS\r
336EFIAPI\r
337FfsFindSectionData (\r
91415a36
MK
338 IN EFI_SECTION_TYPE SectionType,\r
339 IN EFI_FFS_FILE_HEADER *FfsFileHeader,\r
340 IN OUT VOID **SectionData,\r
341 IN OUT UINTN *SectionDataSize\r
e85162ac
SV
342 )\r
343{\r
91415a36
MK
344 UINT32 FileSize;\r
345 EFI_COMMON_SECTION_HEADER *Section;\r
346 UINT32 SectionLength;\r
347 UINT32 ParsedLength;\r
e85162ac
SV
348\r
349 //\r
350 // Size is 24 bits wide so mask upper 8 bits.\r
351 // Does not include FfsFileHeader header size\r
352 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.\r
353 //\r
91415a36
MK
354 Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);\r
355 FileSize = FFS_FILE_SIZE (FfsFileHeader);\r
e85162ac
SV
356 FileSize -= sizeof (EFI_FFS_FILE_HEADER);\r
357\r
91415a36
MK
358 *SectionData = NULL;\r
359 ParsedLength = 0;\r
e85162ac
SV
360 while (ParsedLength < FileSize) {\r
361 if (Section->Type == SectionType) {\r
91415a36
MK
362 *SectionData = (VOID *)(Section + 1);\r
363 *SectionDataSize = SECTION_SIZE (Section);\r
e85162ac
SV
364 return EFI_SUCCESS;\r
365 }\r
91415a36 366\r
e85162ac
SV
367 //\r
368 // Size is 24 bits wide so mask upper 8 bits.\r
369 // SectionLength is adjusted it is 4 byte aligned.\r
370 // Go to the next section\r
371 //\r
91415a36 372 SectionLength = SECTION_SIZE (Section);\r
e85162ac
SV
373 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
374\r
375 ParsedLength += SectionLength;\r
91415a36 376 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);\r
e85162ac
SV
377 }\r
378\r
379 return EFI_NOT_FOUND;\r
380}\r