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