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