]> git.proxmox.com Git - mirror_edk2.git/blame - StandaloneMmPkg/Library/FvLib/FvLib.c
StandaloneMmPkg: Replace BSD License with BSD+Patent License
[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
40 FileState = (EFI_FFS_FILE_STATE)~FileState;\r
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
63 UINT8 *ptr;\r
64 UINTN Index;\r
65 UINT8 Sum;\r
66\r
67 Sum = 0;\r
68 ptr = (UINT8 *) FileHeader;\r
69\r
70 for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {\r
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
75 }\r
76\r
77 for (; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {\r
78 Sum = (UINT8) (Sum + ptr[Index]);\r
79 }\r
80 //\r
81 // State field (since this indicates the different state of file).\r
82 //\r
83 Sum = (UINT8) (Sum - FileHeader->State);\r
84 //\r
85 // Checksum field of the file is not part of the header checksum.\r
86 //\r
87 Sum = (UINT8) (Sum - FileHeader->IntegrityCheck.Checksum.File);\r
88\r
89 return Sum;\r
90}\r
91\r
92/**\r
93 Given the input file pointer, search for the next matching file in the\r
94 FFS volume as defined by SearchType. The search starts from FileHeader inside\r
95 the Firmware Volume defined by FwVolHeader.\r
96\r
97 @param SearchType Filter to find only files of this type.\r
98 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.\r
99 @param FwVolHeader Pointer to the FV header of the volume to search.\r
100 This parameter must point to a valid FFS volume.\r
101 @param FileHeader Pointer to the current file from which to begin searching.\r
102 This pointer will be updated upon return to reflect the file found.\r
103\r
104 @retval EFI_NOT_FOUND No files matching the search criteria were found\r
105 @retval EFI_SUCCESS\r
106**/\r
107EFI_STATUS\r
108EFIAPI\r
109FfsFindNextFile (\r
110 IN EFI_FV_FILETYPE SearchType,\r
111 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,\r
112 IN OUT EFI_FFS_FILE_HEADER **FileHeader\r
113 )\r
114{\r
115 EFI_FIRMWARE_VOLUME_EXT_HEADER *FvExtHeader;\r
116\r
117 EFI_FFS_FILE_HEADER *FfsFileHeader;\r
118 UINT32 FileLength;\r
119 UINT32 FileOccupiedSize;\r
120 UINT32 FileOffset;\r
121 UINT64 FvLength;\r
122 UINT8 ErasePolarity;\r
123 UINT8 FileState;\r
124\r
125 FvLength = FwVolHeader->FvLength;\r
126 if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {\r
127 ErasePolarity = 1;\r
128 } else {\r
129 ErasePolarity = 0;\r
130 }\r
131 //\r
132 // If FileHeader is not specified (NULL) start with the first file in the\r
133 // firmware volume. Otherwise, start from the FileHeader.\r
134 //\r
135 if (*FileHeader == NULL) {\r
136\r
137 if (FwVolHeader->ExtHeaderOffset != 0) {\r
138\r
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
144\r
145 } else {\r
146\r
147 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader +\r
148 FwVolHeader->HeaderLength);\r
149\r
150 }\r
151\r
152 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINTN)FwVolHeader +\r
153 ALIGN_VALUE((UINTN)FfsFileHeader -\r
154 (UINTN)FwVolHeader, 8));\r
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
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
163 }\r
164\r
165 FileOffset = (UINT32) ((UINT8 *) FfsFileHeader - (UINT8 *) FwVolHeader);\r
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
174\r
175 case EFI_FILE_HEADER_INVALID:\r
176 FileOffset += sizeof (EFI_FFS_FILE_HEADER);\r
177 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));\r
178 break;\r
179\r
180 case EFI_FILE_DATA_VALID:\r
181 case EFI_FILE_MARKED_FOR_UPDATE:\r
182 if (CalculateHeaderChecksum (FfsFileHeader) == 0) {\r
183 FileLength = FFS_FILE_SIZE(FfsFileHeader);\r
184 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
185\r
186 if ((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) {\r
187\r
188 *FileHeader = FfsFileHeader;\r
189\r
190 return EFI_SUCCESS;\r
191 }\r
192\r
193 FileOffset += FileOccupiedSize;\r
194 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);\r
195 } else {\r
196 return EFI_NOT_FOUND;\r
197 }\r
198 break;\r
199\r
200 case EFI_FILE_DELETED:\r
201 FileLength = FFS_FILE_SIZE(FfsFileHeader);\r
202 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
203 FileOffset += FileOccupiedSize;\r
204 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);\r
205 break;\r
206\r
207 default:\r
208 return EFI_NOT_FOUND;\r
209\r
210 }\r
211 }\r
212\r
213 return EFI_NOT_FOUND;\r
214}\r
215\r
216/**\r
217 Locates a section within a series of sections\r
218 with the specified section type.\r
219\r
220 @param[in] Sections The sections to search\r
221 @param[in] SizeOfSections Total size of all sections\r
222 @param[in] SectionType The section type to locate\r
223 @param[out] FoundSection The FFS section if found\r
224\r
225 @retval EFI_SUCCESS The file and section was found\r
226 @retval EFI_NOT_FOUND The file and section was not found\r
227 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted\r
228**/\r
229EFI_STATUS\r
230EFIAPI\r
231FindFfsSectionInSections (\r
232 IN VOID *Sections,\r
233 IN UINTN SizeOfSections,\r
234 IN EFI_SECTION_TYPE SectionType,\r
235 OUT EFI_COMMON_SECTION_HEADER **FoundSection\r
236 )\r
237{\r
238 EFI_PHYSICAL_ADDRESS CurrentAddress;\r
239 UINT32 Size;\r
240 EFI_PHYSICAL_ADDRESS EndOfSections;\r
241 EFI_COMMON_SECTION_HEADER *Section;\r
242 EFI_PHYSICAL_ADDRESS EndOfSection;\r
243\r
244 //\r
245 // Loop through the FFS file sections\r
246 //\r
247 EndOfSection = (EFI_PHYSICAL_ADDRESS)(UINTN) Sections;\r
248 EndOfSections = EndOfSection + SizeOfSections;\r
249 for (;;) {\r
250 if (EndOfSection == EndOfSections) {\r
251 break;\r
252 }\r
253 CurrentAddress = EndOfSection;\r
254\r
255 Section = (EFI_COMMON_SECTION_HEADER*)(UINTN) CurrentAddress;\r
256\r
257 Size = SECTION_SIZE (Section);\r
258 if (Size < sizeof (*Section)) {\r
259 return EFI_VOLUME_CORRUPTED;\r
260 }\r
261\r
262 EndOfSection = CurrentAddress + Size;\r
263 if (EndOfSection > EndOfSections) {\r
264 return EFI_VOLUME_CORRUPTED;\r
265 }\r
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
295 IN EFI_SECTION_TYPE SectionType,\r
296 IN EFI_FFS_FILE_HEADER *FfsFileHeader,\r
297 IN OUT EFI_COMMON_SECTION_HEADER **SectionHeader\r
298 )\r
299{\r
300 UINT32 FileSize;\r
301 EFI_COMMON_SECTION_HEADER *Section;\r
302 EFI_STATUS Status;\r
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
309 Section = (EFI_COMMON_SECTION_HEADER *) (FfsFileHeader + 1);\r
310 FileSize = FFS_FILE_SIZE(FfsFileHeader);\r
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
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
342 )\r
343{\r
344 UINT32 FileSize;\r
345 EFI_COMMON_SECTION_HEADER *Section;\r
346 UINT32 SectionLength;\r
347 UINT32 ParsedLength;\r
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
354 Section = (EFI_COMMON_SECTION_HEADER *) (FfsFileHeader + 1);\r
355 FileSize = FFS_FILE_SIZE(FfsFileHeader);\r
356 FileSize -= sizeof (EFI_FFS_FILE_HEADER);\r
357\r
358 *SectionData = NULL;\r
359 ParsedLength = 0;\r
360 while (ParsedLength < FileSize) {\r
361 if (Section->Type == SectionType) {\r
362 *SectionData = (VOID *) (Section + 1);\r
363 *SectionDataSize = SECTION_SIZE(Section);\r
364 return EFI_SUCCESS;\r
365 }\r
366 //\r
367 // Size is 24 bits wide so mask upper 8 bits.\r
368 // SectionLength is adjusted it is 4 byte aligned.\r
369 // Go to the next section\r
370 //\r
371 SectionLength = SECTION_SIZE(Section);\r
372 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
373\r
374 ParsedLength += SectionLength;\r
375 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + SectionLength);\r
376 }\r
377\r
378 return EFI_NOT_FOUND;\r
379}\r