]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/C/VolInfo/VolInfo.c
Sync BaseTool trunk (version r2649) into EDKII BaseTools.
[mirror_edk2.git] / BaseTools / Source / C / VolInfo / VolInfo.c
CommitLineData
f51461c8
LG
1/** @file\r
2\r
3Copyright (c) 1999 - 2011, Intel Corporation. All rights reserved.<BR>\r
4This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 VolInfo.c\r
15\r
16Abstract:\r
17\r
18 The tool dumps the contents of a firmware volume\r
19\r
20**/\r
21\r
22#include <stdio.h>\r
23#include <stdlib.h>\r
24#include <string.h>\r
25#include <ctype.h>\r
26#include <assert.h>\r
27\r
28#include <FvLib.h>\r
29#include <Common/UefiBaseTypes.h>\r
30#include <Common/UefiCapsule.h>\r
31#include <Common/PiFirmwareFile.h>\r
32#include <Common/PiFirmwareVolume.h>\r
33#include <Guid/PiFirmwareFileSystem.h>\r
34#include <IndustryStandard/PeImage.h>\r
35#include <Protocol/GuidedSectionExtraction.h>\r
36\r
37#include "Compress.h"\r
38#include "Decompress.h"\r
39#include "VolInfo.h"\r
40#include "CommonLib.h"\r
41#include "EfiUtilityMsgs.h"\r
42#include "FirmwareVolumeBufferLib.h"\r
43#include "OsPath.h"\r
44#include "ParseGuidedSectionTools.h"\r
45#include "StringFuncs.h"\r
46\r
47//\r
48// Utility global variables\r
49//\r
50\r
51EFI_GUID gEfiCrc32GuidedSectionExtractionProtocolGuid = EFI_CRC32_GUIDED_SECTION_EXTRACTION_PROTOCOL_GUID;\r
52\r
53#define UTILITY_MAJOR_VERSION 0\r
54#define UTILITY_MINOR_VERSION 82\r
55\r
56#define UTILITY_NAME "VolInfo"\r
57\r
58#define EFI_SECTION_ERROR EFIERR (100)\r
59\r
60#define MAX_BASENAME_LEN 60 // not good to hardcode, but let's be reasonable\r
61\r
62//\r
63// Structure to keep a list of guid-to-basenames\r
64//\r
65typedef struct _GUID_TO_BASENAME {\r
66 struct _GUID_TO_BASENAME *Next;\r
67 INT8 Guid[PRINTED_GUID_BUFFER_SIZE];\r
68 INT8 BaseName[MAX_BASENAME_LEN];\r
69} GUID_TO_BASENAME;\r
70\r
71static GUID_TO_BASENAME *mGuidBaseNameList = NULL;\r
72\r
73//\r
74// Store GUIDed Section guid->tool mapping\r
75//\r
76EFI_HANDLE mParsedGuidedSectionTools = NULL;\r
77\r
78CHAR8* mUtilityFilename = NULL;\r
79\r
80EFI_STATUS\r
81ParseGuidBaseNameFile (\r
82 CHAR8 *FileName\r
83 );\r
84\r
85EFI_STATUS\r
86FreeGuidBaseNameList (\r
87 VOID\r
88 );\r
89\r
90EFI_STATUS\r
91PrintGuidName (\r
92 IN UINT8 *GuidStr\r
93 );\r
94\r
95EFI_STATUS\r
96ParseSection (\r
97 IN UINT8 *SectionBuffer,\r
98 IN UINT32 BufferLength\r
99 );\r
100\r
101EFI_STATUS\r
102DumpDepexSection (\r
103 IN UINT8 *Ptr,\r
104 IN UINT32 SectionLength\r
105 );\r
106\r
107STATIC\r
108EFI_STATUS\r
109ReadHeader (\r
110 IN FILE *InputFile,\r
111 OUT UINT32 *FvSize,\r
112 OUT BOOLEAN *ErasePolarity\r
113 );\r
114\r
115STATIC\r
116EFI_STATUS\r
117PrintFileInfo (\r
118 EFI_FIRMWARE_VOLUME_HEADER *FvImage,\r
119 EFI_FFS_FILE_HEADER *FileHeader,\r
120 BOOLEAN ErasePolarity\r
121 );\r
122\r
123static\r
124EFI_STATUS\r
125PrintFvInfo (\r
126 IN VOID *Fv,\r
127 IN BOOLEAN IsChildFv\r
128 );\r
129\r
130static\r
131VOID\r
132LoadGuidedSectionToolsTxt (\r
133 IN CHAR8* FirmwareVolumeFilename\r
134 );\r
135\r
136void\r
137Usage (\r
138 VOID\r
139 );\r
140\r
141int\r
142main (\r
143 int argc,\r
144 char *argv[]\r
145 )\r
146/*++\r
147\r
148Routine Description:\r
149\r
150 GC_TODO: Add function description\r
151\r
152Arguments:\r
153\r
154 argc - GC_TODO: add argument description\r
155 ] - GC_TODO: add argument description\r
156\r
157Returns:\r
158\r
159 GC_TODO: add return values\r
160\r
161--*/\r
162{\r
163 FILE *InputFile;\r
164 int BytesRead;\r
165 EFI_FIRMWARE_VOLUME_HEADER *FvImage;\r
166 UINT32 FvSize;\r
167 EFI_STATUS Status;\r
168 int Offset;\r
169 BOOLEAN ErasePolarity;\r
170\r
171 SetUtilityName (UTILITY_NAME);\r
172 //\r
173 // Print utility header\r
174 //\r
175 printf ("%s Tiano Firmware Volume FFS image info. Version %d.%d %s, %s\n",\r
176 UTILITY_NAME,\r
177 UTILITY_MAJOR_VERSION,\r
178 UTILITY_MINOR_VERSION,\r
179 __BUILD_VERSION,\r
180 __DATE__\r
181 );\r
182\r
183 //\r
184 // Save, and then skip filename arg\r
185 //\r
186 mUtilityFilename = argv[0];\r
187 argc--;\r
188 argv++;\r
189\r
190 Offset = 0;\r
191\r
192 //\r
193 // If they specified -x xref guid/basename cross-reference files, process it.\r
194 // This will print the basename beside each file guid. To use it, specify\r
195 // -x xref_filename to processdsc, then use xref_filename as a parameter\r
196 // here.\r
197 //\r
198 while (argc > 2) {\r
199 if ((strcmp(argv[0], "-x") == 0) || (strcmp(argv[0], "--xref") == 0)) {\r
200 ParseGuidBaseNameFile (argv[1]);\r
201 printf("ParseGuidBaseNameFile: %s\n", argv[1]);\r
202 argc -= 2;\r
203 argv += 2;\r
204 } else if (strcmp(argv[0], "--offset") == 0) {\r
205 //\r
206 // Hex or decimal?\r
207 //\r
208 if ((argv[1][0] == '0') && (tolower ((int)argv[1][1]) == 'x')) {\r
209 if (sscanf (argv[1], "%x", &Offset) != 1) {\r
210 Error (NULL, 0, 1003, "Invalid option value", "Offset = %s", argv[1]);\r
211 return GetUtilityStatus ();\r
212 }\r
213 } else {\r
214 if (sscanf (argv[1], "%d", &Offset) != 1) {\r
215 Error (NULL, 0, 1003, "Invalid option value", "Offset = %s", argv[1]);\r
216 return GetUtilityStatus ();\r
217 }\r
218 //\r
219 // See if they said something like "64K"\r
220 //\r
221 if (tolower ((int)argv[1][strlen (argv[1]) - 1]) == 'k') {\r
222 Offset *= 1024;\r
223 }\r
224 }\r
225\r
226 argc -= 2;\r
227 argv += 2;\r
228 } else {\r
229 Usage ();\r
230 return -1;\r
231 }\r
232 }\r
233 //\r
234 // Check for proper number of arguments\r
235 //\r
236 if (argc != 1) {\r
237 Usage ();\r
238 return -1;\r
239 }\r
240 //\r
241 // Look for help options\r
242 //\r
243 if ((strcmp(argv[0], "-h") == 0) || (strcmp(argv[0], "--help") == 0) || \r
244 (strcmp(argv[0], "-?") == 0) || (strcmp(argv[0], "/?") == 0)) {\r
245 Usage();\r
246 return STATUS_ERROR;\r
247 }\r
248\r
249 //\r
250 // Open the file containing the FV\r
251 //\r
252 InputFile = fopen (argv[0], "rb");\r
253 if (InputFile == NULL) {\r
254 Error (NULL, 0, 0001, "Error opening the input file", argv[0]);\r
255 return GetUtilityStatus ();\r
256 }\r
257 //\r
258 // Skip over pad bytes if specified. This is used if they prepend 0xff\r
259 // data to the FV image binary.\r
260 //\r
261 if (Offset != 0) {\r
262 fseek (InputFile, Offset, SEEK_SET);\r
263 }\r
264 //\r
265 // Determine size of FV\r
266 //\r
267 Status = ReadHeader (InputFile, &FvSize, &ErasePolarity);\r
268 if (EFI_ERROR (Status)) {\r
269 Error (NULL, 0, 0003, "error parsing FV image", "%s Header is invalid", argv[0]);\r
270 fclose (InputFile);\r
271 return GetUtilityStatus ();\r
272 }\r
273 //\r
274 // Allocate a buffer for the FV image\r
275 //\r
276 FvImage = malloc (FvSize);\r
277 if (FvImage == NULL) {\r
278 Error (NULL, 0, 4001, "Resource: Memory can't be allocated", NULL);\r
279 fclose (InputFile);\r
280 return GetUtilityStatus ();\r
281 }\r
282 //\r
283 // Seek to the start of the image, then read the entire FV to the buffer\r
284 //\r
285 fseek (InputFile, Offset, SEEK_SET);\r
286 BytesRead = fread (FvImage, 1, FvSize, InputFile);\r
287 fclose (InputFile);\r
288 if ((unsigned int) BytesRead != FvSize) {\r
289 Error (NULL, 0, 0004, "error reading FvImage from", argv[0]);\r
290 free (FvImage);\r
291 return GetUtilityStatus ();\r
292 }\r
293\r
294 LoadGuidedSectionToolsTxt (argv[0]);\r
295\r
296 PrintFvInfo (FvImage, FALSE);\r
297\r
298 //\r
299 // Clean up\r
300 //\r
301 free (FvImage);\r
302 FreeGuidBaseNameList ();\r
303 return GetUtilityStatus ();\r
304}\r
305\r
306\r
307static\r
308EFI_STATUS\r
309PrintFvInfo (\r
310 IN VOID *Fv,\r
311 IN BOOLEAN IsChildFv\r
312 )\r
313/*++\r
314\r
315Routine Description:\r
316\r
317 GC_TODO: Add function description\r
318\r
319Arguments:\r
320\r
321 Fv - Firmware Volume to print information about\r
322 IsChildFv - Flag specifies whether the input FV is a child FV.\r
323\r
324Returns:\r
325\r
326 EFI_STATUS\r
327\r
328--*/\r
329{\r
330 EFI_STATUS Status;\r
331 UINTN NumberOfFiles;\r
332 BOOLEAN ErasePolarity;\r
333 UINTN FvSize;\r
334 EFI_FFS_FILE_HEADER *CurrentFile;\r
335 UINTN Key;\r
336\r
337 Status = FvBufGetSize (Fv, &FvSize);\r
338\r
339 NumberOfFiles = 0;\r
340 ErasePolarity =\r
341 (((EFI_FIRMWARE_VOLUME_HEADER*)Fv)->Attributes & EFI_FVB2_ERASE_POLARITY) ?\r
342 TRUE : FALSE;\r
343\r
344 //\r
345 // Get the first file\r
346 //\r
347 Key = 0;\r
348 Status = FvBufFindNextFile (Fv, &Key, (VOID **) &CurrentFile);\r
349 if (EFI_ERROR (Status)) {\r
350 Error (NULL, 0, 0003, "error parsing FV image", "cannot find the first file in the FV image");\r
351 return GetUtilityStatus ();\r
352 }\r
353 //\r
354 // Display information about files found\r
355 //\r
356 while (CurrentFile != NULL) {\r
357 //\r
358 // Increment the number of files counter\r
359 //\r
360 NumberOfFiles++;\r
361\r
362 //\r
363 // Display info about this file\r
364 //\r
365 Status = PrintFileInfo (Fv, CurrentFile, ErasePolarity);\r
366 if (EFI_ERROR (Status)) {\r
367 Error (NULL, 0, 0003, "error parsing FV image", "failed to parse a file in the FV");\r
368 return GetUtilityStatus ();\r
369 }\r
370 //\r
371 // Get the next file\r
372 //\r
373 Status = FvBufFindNextFile (Fv, &Key, (VOID **) &CurrentFile);\r
374 if (Status == EFI_NOT_FOUND) {\r
375 CurrentFile = NULL;\r
376 } else if (EFI_ERROR (Status)) {\r
377 Error (NULL, 0, 0003, "error parsing FV image", "cannot find the next file in the FV image");\r
378 return GetUtilityStatus ();\r
379 }\r
380 }\r
381\r
382 if (IsChildFv) {\r
383 printf ("There are a total of %d files in the child FV\n", (int) NumberOfFiles);\r
384 } else {\r
385 printf ("There are a total of %d files in this FV\n", (int) NumberOfFiles);\r
386 }\r
387\r
388 return EFI_SUCCESS;\r
389}\r
390\r
391UINT32\r
392GetOccupiedSize (\r
393 IN UINT32 ActualSize,\r
394 IN UINT32 Alignment\r
395 )\r
396/*++\r
397\r
398Routine Description:\r
399\r
400 This function returns the next larger size that meets the alignment \r
401 requirement specified.\r
402\r
403Arguments:\r
404\r
405 ActualSize The size.\r
406 Alignment The desired alignment.\r
407 \r
408Returns:\r
409 \r
410 EFI_SUCCESS Function completed successfully.\r
411 EFI_ABORTED The function encountered an error.\r
412\r
413--*/\r
414{\r
415 UINT32 OccupiedSize;\r
416\r
417 OccupiedSize = ActualSize;\r
418 while ((OccupiedSize & (Alignment - 1)) != 0) {\r
419 OccupiedSize++;\r
420 }\r
421\r
422 return OccupiedSize;\r
423}\r
424\r
425static\r
426CHAR8 *\r
427SectionNameToStr (\r
428 IN EFI_SECTION_TYPE Type\r
429 )\r
430/*++\r
431\r
432Routine Description:\r
433\r
434 Converts EFI Section names to Strings\r
435\r
436Arguments:\r
437\r
438 Type - The EFI Section type\r
439\r
440Returns:\r
441\r
442 CHAR8* - Pointer to the String containing the section name.\r
443\r
444--*/\r
445{\r
446 CHAR8 *SectionStr;\r
447 CHAR8 *SectionTypeStringTable[] = {\r
448 //\r
449 // 0X00\r
450 //\r
451 "EFI_SECTION_ALL",\r
452 //\r
453 // 0x01\r
454 //\r
455 "EFI_SECTION_COMPRESSION",\r
456 //\r
457 // 0x02\r
458 //\r
459 "EFI_SECTION_GUID_DEFINED", \r
460 //\r
461 // 0x03\r
462 //\r
463 "Unknown section type - Reserved 0x03",\r
464 //\r
465 // 0x04\r
466 //\r
467 "Unknown section type - Reserved 0x04",\r
468 //\r
469 // 0x05\r
470 //\r
471 "Unknown section type - Reserved 0x05",\r
472 //\r
473 // 0x06\r
474 //\r
475 "Unknown section type - Reserved 0x06",\r
476 //\r
477 // 0x07\r
478 //\r
479 "Unknown section type - Reserved 0x07",\r
480 //\r
481 // 0x08\r
482 //\r
483 "Unknown section type - Reserved 0x08",\r
484 //\r
485 // 0x09\r
486 //\r
487 "Unknown section type - Reserved 0x09",\r
488 //\r
489 // 0x0A\r
490 //\r
491 "Unknown section type - Reserved 0x0A",\r
492 //\r
493 // 0x0B\r
494 //\r
495 "Unknown section type - Reserved 0x0B",\r
496 //\r
497 // 0x0C\r
498 //\r
499 "Unknown section type - Reserved 0x0C",\r
500 //\r
501 // 0x0D\r
502 //\r
503 "Unknown section type - Reserved 0x0D",\r
504 //\r
505 // 0x0E\r
506 //\r
507 "Unknown section type - Reserved 0x0E",\r
508 //\r
509 // 0x0F\r
510 //\r
511 "Unknown section type - Reserved 0x0E",\r
512 //\r
513 // 0x10\r
514 //\r
515 "EFI_SECTION_PE32",\r
516 //\r
517 // 0x11\r
518 //\r
519 "EFI_SECTION_PIC",\r
520 //\r
521 // 0x12\r
522 //\r
523 "EFI_SECTION_TE", \r
524 //\r
525 // 0x13\r
526 //\r
527 "EFI_SECTION_DXE_DEPEX", \r
528 //\r
529 // 0x14\r
530 //\r
531 "EFI_SECTION_VERSION",\r
532 //\r
533 // 0x15\r
534 //\r
535 "EFI_SECTION_USER_INTERFACE",\r
536 //\r
537 // 0x16\r
538 //\r
539 "EFI_SECTION_COMPATIBILITY16",\r
540 //\r
541 // 0x17\r
542 //\r
543 "EFI_SECTION_FIRMWARE_VOLUME_IMAGE ",\r
544 //\r
545 // 0x18\r
546 //\r
547 "EFI_SECTION_FREEFORM_SUBTYPE_GUID ",\r
548 //\r
549 // 0x19\r
550 //\r
551 "EFI_SECTION_RAW",\r
552 //\r
553 // 0x1A\r
554 //\r
555 "Unknown section type - 0x1A",\r
556 //\r
557 // 0x1B\r
558 //\r
559 "EFI_SECTION_PEI_DEPEX",\r
560 //\r
561 // 0x1C\r
562 //\r
563 "EFI_SECTION_SMM_DEPEX",\r
564 //\r
565 // 0x1C+\r
566 //\r
567 "Unknown section type - Reserved - beyond last defined section"\r
568 };\r
569\r
570 if (Type > EFI_SECTION_LAST_SECTION_TYPE) {\r
571 Type = EFI_SECTION_LAST_SECTION_TYPE + 1;\r
572 }\r
573\r
574 SectionStr = malloc (100);\r
575 if (SectionStr == NULL) {\r
576 printf ("Error: Out of memory resources.\n");\r
577 return SectionStr;\r
578 }\r
579 strcpy (SectionStr, SectionTypeStringTable[Type]);\r
580 return SectionStr;\r
581}\r
582\r
583STATIC\r
584EFI_STATUS\r
585ReadHeader (\r
586 IN FILE *InputFile,\r
587 OUT UINT32 *FvSize,\r
588 OUT BOOLEAN *ErasePolarity\r
589 )\r
590/*++\r
591\r
592Routine Description:\r
593\r
594 This function determines the size of the FV and the erase polarity. The \r
595 erase polarity is the FALSE value for file state.\r
596\r
597Arguments:\r
598\r
599 InputFile The file that contains the FV image.\r
600 FvSize The size of the FV.\r
601 ErasePolarity The FV erase polarity.\r
602 \r
603Returns:\r
604 \r
605 EFI_SUCCESS Function completed successfully.\r
606 EFI_INVALID_PARAMETER A required parameter was NULL or is out of range.\r
607 EFI_ABORTED The function encountered an error.\r
608\r
609--*/\r
610{\r
611 EFI_FIRMWARE_VOLUME_HEADER VolumeHeader;\r
612 EFI_FV_BLOCK_MAP_ENTRY BlockMap;\r
613 UINTN Signature[2];\r
614 UINTN BytesRead;\r
615 UINT32 Size;\r
616\r
617 BytesRead = 0;\r
618 Size = 0;\r
619 //\r
620 // Check input parameters\r
621 //\r
622 if (InputFile == NULL || FvSize == NULL || ErasePolarity == NULL) {\r
623 Error (__FILE__, __LINE__, 0, "application error", "invalid parameter to function");\r
624 return EFI_INVALID_PARAMETER;\r
625 }\r
626 //\r
627 // Read the header\r
628 //\r
629 fread (&VolumeHeader, sizeof (EFI_FIRMWARE_VOLUME_HEADER) - sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, InputFile);\r
630 BytesRead = sizeof (EFI_FIRMWARE_VOLUME_HEADER) - sizeof (EFI_FV_BLOCK_MAP_ENTRY);\r
631 Signature[0] = VolumeHeader.Signature;\r
632 Signature[1] = 0;\r
633\r
634 //\r
635 // Print FV header information\r
636 //\r
637 printf ("Signature: %s (%X)\n", (char *) Signature, (unsigned) VolumeHeader.Signature);\r
638 printf ("Attributes: %X\n", (unsigned) VolumeHeader.Attributes);\r
639\r
640 if (VolumeHeader.Attributes & EFI_FVB2_READ_DISABLED_CAP) {\r
641 printf (" EFI_FVB2_READ_DISABLED_CAP\n");\r
642 }\r
643\r
644 if (VolumeHeader.Attributes & EFI_FVB2_READ_ENABLED_CAP) {\r
645 printf (" EFI_FVB2_READ_ENABLED_CAP\n");\r
646 }\r
647\r
648 if (VolumeHeader.Attributes & EFI_FVB2_READ_STATUS) {\r
649 printf (" EFI_FVB2_READ_STATUS\n");\r
650 }\r
651\r
652 if (VolumeHeader.Attributes & EFI_FVB2_WRITE_DISABLED_CAP) {\r
653 printf (" EFI_FVB2_WRITE_DISABLED_CAP\n");\r
654 }\r
655\r
656 if (VolumeHeader.Attributes & EFI_FVB2_WRITE_ENABLED_CAP) {\r
657 printf (" EFI_FVB2_WRITE_ENABLED_CAP\n");\r
658 }\r
659\r
660 if (VolumeHeader.Attributes & EFI_FVB2_WRITE_STATUS) {\r
661 printf (" EFI_FVB2_WRITE_STATUS\n");\r
662 }\r
663\r
664 if (VolumeHeader.Attributes & EFI_FVB2_LOCK_CAP) {\r
665 printf (" EFI_FVB2_LOCK_CAP\n");\r
666 }\r
667\r
668 if (VolumeHeader.Attributes & EFI_FVB2_LOCK_STATUS) {\r
669 printf (" EFI_FVB2_LOCK_STATUS\n");\r
670 }\r
671\r
672 if (VolumeHeader.Attributes & EFI_FVB2_STICKY_WRITE) {\r
673 printf (" EFI_FVB2_STICKY_WRITE\n");\r
674 }\r
675\r
676 if (VolumeHeader.Attributes & EFI_FVB2_MEMORY_MAPPED) {\r
677 printf (" EFI_FVB2_MEMORY_MAPPED\n");\r
678 }\r
679\r
680 if (VolumeHeader.Attributes & EFI_FVB2_ERASE_POLARITY) {\r
681 printf (" EFI_FVB2_ERASE_POLARITY\n");\r
682 *ErasePolarity = TRUE;\r
683 }\r
684\r
685#if (PI_SPECIFICATION_VERSION < 0x00010000)\r
686 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT) {\r
687 printf (" EFI_FVB2_ALIGNMENT\n");\r
688 }\r
689\r
690 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2) {\r
691 printf (" EFI_FVB2_ALIGNMENT_2\n");\r
692 }\r
693\r
694 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_4) {\r
695 printf (" EFI_FVB2_ALIGNMENT_4\n");\r
696 }\r
697\r
698 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_8) {\r
699 printf (" EFI_FVB2_ALIGNMENT_8\n");\r
700 }\r
701\r
702 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_16) {\r
703 printf (" EFI_FVB2_ALIGNMENT_16\n");\r
704 }\r
705\r
706 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_32) {\r
707 printf (" EFI_FVB2_ALIGNMENT_32\n");\r
708 }\r
709\r
710 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64) {\r
711 printf (" EFI_FVB2_ALIGNMENT_64\n");\r
712 }\r
713\r
714 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_128) {\r
715 printf (" EFI_FVB2_ALIGNMENT_128\n");\r
716 }\r
717\r
718 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_256) {\r
719 printf (" EFI_FVB2_ALIGNMENT_256\n");\r
720 }\r
721\r
722 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_512) {\r
723 printf (" EFI_FVB2_ALIGNMENT_512\n");\r
724 }\r
725\r
726 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_1K) {\r
727 printf (" EFI_FVB2_ALIGNMENT_1K\n");\r
728 }\r
729\r
730 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2K) {\r
731 printf (" EFI_FVB2_ALIGNMENT_2K\n");\r
732 }\r
733\r
734 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_4K) {\r
735 printf (" EFI_FVB2_ALIGNMENT_4K\n");\r
736 }\r
737\r
738 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_8K) {\r
739 printf (" EFI_FVB2_ALIGNMENT_8K\n");\r
740 }\r
741\r
742 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_16K) {\r
743 printf (" EFI_FVB2_ALIGNMENT_16K\n");\r
744 }\r
745\r
746 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_32K) {\r
747 printf (" EFI_FVB2_ALIGNMENT_32K\n");\r
748 }\r
749\r
750 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64K) {\r
751 printf (" EFI_FVB2_ALIGNMENT_64K\n");\r
752 }\r
753 \r
754#else\r
755\r
756 if (VolumeHeader.Attributes & EFI_FVB2_READ_LOCK_CAP) {\r
757 printf (" EFI_FVB2_READ_LOCK_CAP\n");\r
758 }\r
759\r
760 if (VolumeHeader.Attributes & EFI_FVB2_READ_LOCK_STATUS) {\r
761 printf (" EFI_FVB2_READ_LOCK_STATUS\n");\r
762 }\r
763\r
764 if (VolumeHeader.Attributes & EFI_FVB2_WRITE_LOCK_CAP) {\r
765 printf (" EFI_FVB2_WRITE_LOCK_CAP\n");\r
766 }\r
767\r
768 if (VolumeHeader.Attributes & EFI_FVB2_WRITE_LOCK_STATUS) {\r
769 printf (" EFI_FVB2_WRITE_LOCK_STATUS\n");\r
770 }\r
771\r
772 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_1) {\r
773 printf (" EFI_FVB2_ALIGNMENT_1\n");\r
774 }\r
775\r
776 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2) {\r
777 printf (" EFI_FVB2_ALIGNMENT_2\n");\r
778 }\r
779\r
780 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_4) {\r
781 printf (" EFI_FVB2_ALIGNMENT_4\n");\r
782 }\r
783\r
784 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_8) {\r
785 printf (" EFI_FVB2_ALIGNMENT_8\n");\r
786 }\r
787\r
788 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_16) {\r
789 printf (" EFI_FVB2_ALIGNMENT_16\n");\r
790 }\r
791\r
792 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_32) {\r
793 printf (" EFI_FVB2_ALIGNMENT_32\n");\r
794 }\r
795\r
796 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64) {\r
797 printf (" EFI_FVB2_ALIGNMENT_64\n");\r
798 }\r
799\r
800 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_128) {\r
801 printf (" EFI_FVB2_ALIGNMENT_128\n");\r
802 }\r
803\r
804 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_256) {\r
805 printf (" EFI_FVB2_ALIGNMENT_256\n");\r
806 }\r
807\r
808 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_512) {\r
809 printf (" EFI_FVB2_ALIGNMENT_512\n");\r
810 }\r
811\r
812 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_1K) {\r
813 printf (" EFI_FVB2_ALIGNMENT_1K\n");\r
814 }\r
815\r
816 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2K) {\r
817 printf (" EFI_FVB2_ALIGNMENT_2K\n");\r
818 }\r
819\r
820 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_4K) {\r
821 printf (" EFI_FVB2_ALIGNMENT_4K\n");\r
822 }\r
823\r
824 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_8K) {\r
825 printf (" EFI_FVB2_ALIGNMENT_8K\n");\r
826 }\r
827\r
828 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_16K) {\r
829 printf (" EFI_FVB2_ALIGNMENT_16K\n");\r
830 }\r
831\r
832 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_32K) {\r
833 printf (" EFI_FVB2_ALIGNMENT_32K\n");\r
834 }\r
835\r
836 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64K) {\r
837 printf (" EFI_FVB2_ALIGNMENT_64K\n");\r
838 }\r
839\r
840 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_128K) {\r
841 printf (" EFI_FVB2_ALIGNMENT_128K\n");\r
842 }\r
843\r
844 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_256K) {\r
845 printf (" EFI_FVB2_ALIGNMENT_256K\n");\r
846 }\r
847\r
848 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_512K) {\r
849 printf (" EFI_FVB2_ALIGNMENT_512K\n");\r
850 }\r
851\r
852 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_1M) {\r
853 printf (" EFI_FVB2_ALIGNMENT_1M\n");\r
854 }\r
855\r
856 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2M) {\r
857 printf (" EFI_FVB2_ALIGNMENT_2M\n");\r
858 }\r
859\r
860 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_4M) {\r
861 printf (" EFI_FVB2_ALIGNMENT_4M\n");\r
862 }\r
863\r
864 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_8M) {\r
865 printf (" EFI_FVB2_ALIGNMENT_8M\n");\r
866 }\r
867\r
868 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_16M) {\r
869 printf (" EFI_FVB2_ALIGNMENT_16M\n");\r
870 }\r
871\r
872 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_32M) {\r
873 printf (" EFI_FVB2_ALIGNMENT_32M\n");\r
874 }\r
875\r
876 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64M) {\r
877 printf (" EFI_FVB2_ALIGNMENT_64M\n");\r
878 }\r
879\r
880 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_128M) {\r
881 printf (" EFI_FVB2_ALIGNMENT_128M\n");\r
882 }\r
883\r
884 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64M) {\r
885 printf (" EFI_FVB2_ALIGNMENT_64M\n");\r
886 }\r
887\r
888 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_128M) {\r
889 printf (" EFI_FVB2_ALIGNMENT_128M\n");\r
890 }\r
891\r
892 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_256M) {\r
893 printf (" EFI_FVB2_ALIGNMENT_256M\n");\r
894 }\r
895\r
896 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_512M) {\r
897 printf (" EFI_FVB2_ALIGNMENT_512M\n");\r
898 }\r
899\r
900 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_1G) {\r
901 printf (" EFI_FVB2_ALIGNMENT_1G\n");\r
902 }\r
903\r
904 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2G) {\r
905 printf (" EFI_FVB2_ALIGNMENT_2G\n");\r
906 }\r
907\r
908#endif\r
909 printf ("Header Length: 0x%08X\n", VolumeHeader.HeaderLength);\r
910 printf ("File System ID: ");\r
911 PrintGuid (&VolumeHeader.FileSystemGuid);\r
912 //\r
913 // printf ("\n");\r
914 //\r
915 printf ("Revision: 0x%04X\n", VolumeHeader.Revision);\r
916\r
917 do {\r
918 fread (&BlockMap, sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, InputFile);\r
919 BytesRead += sizeof (EFI_FV_BLOCK_MAP_ENTRY);\r
920\r
921 if (BlockMap.NumBlocks != 0) {\r
922 printf ("Number of Blocks: 0x%08X\n", (unsigned) BlockMap.NumBlocks);\r
923 printf ("Block Length: 0x%08X\n", (unsigned) BlockMap.Length);\r
924 Size += BlockMap.NumBlocks * BlockMap.Length;\r
925 }\r
926\r
927 } while (!(BlockMap.NumBlocks == 0 && BlockMap.Length == 0));\r
928\r
929 if (BytesRead != VolumeHeader.HeaderLength) {\r
930 printf ("ERROR: Header length not consistent with Block Maps!\n");\r
931 return EFI_ABORTED;\r
932 }\r
933\r
934 if (VolumeHeader.FvLength != Size) {\r
935 printf ("ERROR: Volume Size not consistant with Block Maps!\n");\r
936 return EFI_ABORTED;\r
937 }\r
938\r
939 printf ("Total Volume Size: 0x%08X\n", (unsigned) Size);\r
940\r
941 *FvSize = Size;\r
942\r
943 //\r
944 // rewind (InputFile);\r
945 //\r
946 return EFI_SUCCESS;\r
947}\r
948\r
949STATIC\r
950EFI_STATUS\r
951PrintFileInfo (\r
952 EFI_FIRMWARE_VOLUME_HEADER *FvImage,\r
953 EFI_FFS_FILE_HEADER *FileHeader,\r
954 BOOLEAN ErasePolarity\r
955 )\r
956/*++\r
957\r
958Routine Description:\r
959\r
960 GC_TODO: Add function description\r
961\r
962Arguments:\r
963\r
964 FvImage - GC_TODO: add argument description\r
965 FileHeader - GC_TODO: add argument description\r
966 ErasePolarity - GC_TODO: add argument description\r
967\r
968Returns:\r
969\r
970 EFI_SUCCESS - GC_TODO: Add description for return value\r
971 EFI_ABORTED - GC_TODO: Add description for return value\r
972\r
973--*/\r
974{\r
975 UINT32 FileLength;\r
976 UINT8 FileState;\r
977 UINT8 Checksum;\r
978 EFI_FFS_FILE_HEADER BlankHeader;\r
979 EFI_STATUS Status;\r
980 UINT8 GuidBuffer[PRINTED_GUID_BUFFER_SIZE];\r
981#if (PI_SPECIFICATION_VERSION < 0x00010000) \r
982 UINT16 *Tail;\r
983#endif\r
984 //\r
985 // Check if we have free space\r
986 //\r
987 if (ErasePolarity) {\r
988 memset (&BlankHeader, -1, sizeof (EFI_FFS_FILE_HEADER));\r
989 } else {\r
990 memset (&BlankHeader, 0, sizeof (EFI_FFS_FILE_HEADER));\r
991 }\r
992\r
993 if (memcmp (&BlankHeader, FileHeader, sizeof (EFI_FFS_FILE_HEADER)) == 0) {\r
994 return EFI_SUCCESS;\r
995 }\r
996 //\r
997 // Print file information.\r
998 //\r
999 printf ("============================================================\n");\r
1000\r
1001 printf ("File Name: ");\r
1002 PrintGuidToBuffer (&FileHeader->Name, GuidBuffer, sizeof (GuidBuffer), TRUE);\r
1003 printf ("%s ", GuidBuffer);\r
1004 PrintGuidName (GuidBuffer);\r
1005 printf ("\n");\r
1006\r
1007 //\r
1008 // PrintGuid (&FileHeader->Name);\r
1009 // printf ("\n");\r
1010 //\r
1011 FileLength = GetLength (FileHeader->Size);\r
1012 printf ("File Offset: 0x%08X\n", (unsigned) ((UINTN) FileHeader - (UINTN) FvImage));\r
1013 printf ("File Length: 0x%08X\n", (unsigned) FileLength);\r
1014 printf ("File Attributes: 0x%02X\n", FileHeader->Attributes);\r
1015 printf ("File State: 0x%02X\n", FileHeader->State);\r
1016\r
1017 //\r
1018 // Print file state\r
1019 //\r
1020 FileState = GetFileState (ErasePolarity, FileHeader);\r
1021\r
1022 switch (FileState) {\r
1023\r
1024 case EFI_FILE_HEADER_CONSTRUCTION:\r
1025 printf (" EFI_FILE_HEADER_CONSTRUCTION\n");\r
1026 return EFI_SUCCESS;\r
1027\r
1028 case EFI_FILE_HEADER_INVALID:\r
1029 printf (" EFI_FILE_HEADER_INVALID\n");\r
1030 return EFI_SUCCESS;\r
1031\r
1032 case EFI_FILE_HEADER_VALID:\r
1033 printf (" EFI_FILE_HEADER_VALID\n");\r
1034 Checksum = CalculateSum8 ((UINT8 *) FileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
1035 Checksum = (UINT8) (Checksum - FileHeader->IntegrityCheck.Checksum.File);\r
1036 Checksum = (UINT8) (Checksum - FileHeader->State);\r
1037 if (Checksum != 0) {\r
1038 printf ("ERROR: Header checksum invalid.\n");\r
1039 return EFI_ABORTED;\r
1040 }\r
1041\r
1042 return EFI_SUCCESS;\r
1043\r
1044 case EFI_FILE_DELETED:\r
1045 printf (" EFI_FILE_DELETED\n");\r
1046\r
1047 case EFI_FILE_MARKED_FOR_UPDATE:\r
1048 printf (" EFI_FILE_MARKED_FOR_UPDATE\n");\r
1049\r
1050 case EFI_FILE_DATA_VALID:\r
1051 printf (" EFI_FILE_DATA_VALID\n");\r
1052\r
1053 //\r
1054 // Calculate header checksum\r
1055 //\r
1056 Checksum = CalculateSum8 ((UINT8 *) FileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
1057 Checksum = (UINT8) (Checksum - FileHeader->IntegrityCheck.Checksum.File);\r
1058 Checksum = (UINT8) (Checksum - FileHeader->State);\r
1059 if (Checksum != 0) {\r
1060 Error (NULL, 0, 0003, "error parsing FFS file", "FFS file with Guid %s has invalid header checksum", GuidBuffer);\r
1061 return EFI_ABORTED;\r
1062 }\r
1063\r
1064 FileLength = GetLength (FileHeader->Size);\r
1065\r
1066 if (FileHeader->Attributes & FFS_ATTRIB_CHECKSUM) {\r
1067 //\r
1068 // Calculate file checksum\r
1069 //\r
1070 Checksum = CalculateSum8 ((UINT8 *) (FileHeader + 1), FileLength - sizeof (EFI_FFS_FILE_HEADER));\r
1071 Checksum = Checksum + FileHeader->IntegrityCheck.Checksum.File;\r
1072 if (Checksum != 0) {\r
1073 Error (NULL, 0, 0003, "error parsing FFS file", "FFS file with Guid %s has invalid file checksum", GuidBuffer);\r
1074 return EFI_ABORTED;\r
1075 }\r
1076 } else {\r
1077 if (FileHeader->IntegrityCheck.Checksum.File != FFS_FIXED_CHECKSUM) {\r
1078 Error (NULL, 0, 0003, "error parsing FFS file", "FFS file with Guid %s has invalid header checksum -- not set to fixed value of 0xAA", GuidBuffer);\r
1079 return EFI_ABORTED;\r
1080 }\r
1081 }\r
1082#if (PI_SPECIFICATION_VERSION < 0x00010000) \r
1083 //\r
1084 // Verify tail if present\r
1085 //\r
1086 if (FileHeader->Attributes & FFS_ATTRIB_TAIL_PRESENT) {\r
1087 //\r
1088 // Verify tail is complement of integrity check field in the header.\r
1089 //\r
1090 Tail = (UINT16 *) ((UINTN) FileHeader + GetLength (FileHeader->Size) - sizeof (EFI_FFS_INTEGRITY_CHECK));\r
1091 if (FileHeader->IntegrityCheck.TailReference != (UINT16)~(*Tail)) {\r
1092 Error (NULL, 0, 0003, "error parsing FFS file", \\r
1093 "FFS file with Guid %s failed in the integrity check, tail is not the complement of the header field", GuidBuffer);\r
1094 return EFI_ABORTED;\r
1095 }\r
1096 }\r
1097 #endif \r
1098 break;\r
1099\r
1100 default:\r
1101 Error (NULL, 0, 0003, "error parsing FFS file", "FFS file with Guid %s has the invalid/unrecognized file state bits", GuidBuffer);\r
1102 return EFI_ABORTED;\r
1103 }\r
1104\r
1105 printf ("File Type: 0x%02X ", FileHeader->Type);\r
1106\r
1107 switch (FileHeader->Type) {\r
1108\r
1109 case EFI_FV_FILETYPE_RAW:\r
1110 printf ("EFI_FV_FILETYPE_RAW\n");\r
1111 break;\r
1112\r
1113 case EFI_FV_FILETYPE_FREEFORM:\r
1114 printf ("EFI_FV_FILETYPE_FREEFORM\n");\r
1115 break;\r
1116\r
1117 case EFI_FV_FILETYPE_SECURITY_CORE:\r
1118 printf ("EFI_FV_FILETYPE_SECURITY_CORE\n");\r
1119 break;\r
1120\r
1121 case EFI_FV_FILETYPE_PEI_CORE:\r
1122 printf ("EFI_FV_FILETYPE_PEI_CORE\n");\r
1123 break;\r
1124\r
1125 case EFI_FV_FILETYPE_DXE_CORE:\r
1126 printf ("EFI_FV_FILETYPE_DXE_CORE\n");\r
1127 break;\r
1128\r
1129 case EFI_FV_FILETYPE_PEIM:\r
1130 printf ("EFI_FV_FILETYPE_PEIM\n");\r
1131 break;\r
1132\r
1133 case EFI_FV_FILETYPE_DRIVER:\r
1134 printf ("EFI_FV_FILETYPE_DRIVER\n");\r
1135 break;\r
1136\r
1137 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:\r
1138 printf ("EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER\n");\r
1139 break;\r
1140\r
1141 case EFI_FV_FILETYPE_APPLICATION:\r
1142 printf ("EFI_FV_FILETYPE_APPLICATION\n");\r
1143 break;\r
1144\r
1145 case EFI_FV_FILETYPE_SMM:\r
1146 printf ("EFI_FV_FILETYPE_SMM\n");\r
1147 break;\r
1148\r
1149 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE:\r
1150 printf ("EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE\n");\r
1151 break;\r
1152\r
1153 case EFI_FV_FILETYPE_COMBINED_SMM_DXE:\r
1154 printf ("EFI_FV_FILETYPE_COMBINED_SMM_DXE\n");\r
1155 break;\r
1156\r
1157 case EFI_FV_FILETYPE_SMM_CORE:\r
1158 printf ("EFI_FV_FILETYPE_SMM_CORE\n");\r
1159 break;\r
1160\r
1161 case EFI_FV_FILETYPE_FFS_PAD:\r
1162 printf ("EFI_FV_FILETYPE_FFS_PAD\n");\r
1163 break;\r
1164\r
1165 default:\r
1166 printf ("\nERROR: Unrecognized file type %X.\n", FileHeader->Type);\r
1167 return EFI_ABORTED;\r
1168 break;\r
1169 }\r
1170\r
1171 switch (FileHeader->Type) {\r
1172\r
1173 case EFI_FV_FILETYPE_ALL:\r
1174 case EFI_FV_FILETYPE_RAW:\r
1175 case EFI_FV_FILETYPE_FFS_PAD:\r
1176 break;\r
1177\r
1178 default:\r
1179 //\r
1180 // All other files have sections\r
1181 //\r
1182 Status = ParseSection (\r
1183 (UINT8 *) ((UINTN) FileHeader + sizeof (EFI_FFS_FILE_HEADER)),\r
1184 GetLength (FileHeader->Size) - sizeof (EFI_FFS_FILE_HEADER)\r
1185 );\r
1186 if (EFI_ERROR (Status)) {\r
1187 //\r
1188 // printf ("ERROR: Parsing the FFS file.\n");\r
1189 //\r
1190 return EFI_ABORTED;\r
1191 }\r
1192 break;\r
1193 }\r
1194\r
1195 return EFI_SUCCESS;\r
1196}\r
1197\r
1198EFI_STATUS\r
1199ParseSection (\r
1200 IN UINT8 *SectionBuffer,\r
1201 IN UINT32 BufferLength\r
1202 )\r
1203/*++\r
1204\r
1205Routine Description:\r
1206\r
1207 Parses EFI Sections\r
1208\r
1209Arguments:\r
1210\r
1211 SectionBuffer - Buffer containing the section to parse.\r
1212 BufferLength - Length of SectionBuffer\r
1213\r
1214Returns:\r
1215\r
1216 EFI_SECTION_ERROR - Problem with section parsing.\r
1217 (a) compression errors\r
1218 (b) unrecognized section \r
1219 EFI_UNSUPPORTED - Do not know how to parse the section.\r
1220 EFI_SUCCESS - Section successfully parsed.\r
1221 EFI_OUT_OF_RESOURCES - Memory allocation failed.\r
1222\r
1223--*/\r
1224{\r
1225 EFI_SECTION_TYPE Type;\r
1226 UINT8 *Ptr;\r
1227 UINT32 SectionLength;\r
1228 CHAR8 *SectionName;\r
1229 EFI_STATUS Status;\r
1230 UINT32 ParsedLength;\r
1231 UINT8 *CompressedBuffer;\r
1232 UINT32 CompressedLength;\r
1233 UINT8 *UncompressedBuffer;\r
1234 UINT32 UncompressedLength;\r
1235 UINT8 *ToolOutputBuffer;\r
1236 UINT32 ToolOutputLength;\r
1237 UINT8 CompressionType;\r
1238 UINT32 DstSize;\r
1239 UINT32 ScratchSize;\r
1240 UINT8 *ScratchBuffer;\r
1241 DECOMPRESS_FUNCTION DecompressFunction;\r
1242 GETINFO_FUNCTION GetInfoFunction;\r
1243 // CHAR16 *name;\r
1244 CHAR8 *ExtractionTool;\r
1245 CHAR8 *ToolInputFile;\r
1246 CHAR8 *ToolOutputFile;\r
1247 CHAR8 *SystemCommandFormatString;\r
1248 CHAR8 *SystemCommand;\r
1249\r
1250 ParsedLength = 0;\r
1251 while (ParsedLength < BufferLength) {\r
1252 Ptr = SectionBuffer + ParsedLength;\r
1253\r
1254 SectionLength = GetLength (((EFI_COMMON_SECTION_HEADER *) Ptr)->Size);\r
1255 Type = ((EFI_COMMON_SECTION_HEADER *) Ptr)->Type;\r
1256\r
1257 //\r
1258 // This is sort of an odd check, but is necessary because FFS files are\r
1259 // padded to a QWORD boundary, meaning there is potentially a whole section\r
1260 // header worth of 0xFF bytes.\r
1261 //\r
1262 if (SectionLength == 0xffffff && Type == 0xff) {\r
1263 ParsedLength += 4;\r
1264 continue;\r
1265 }\r
1266\r
1267 SectionName = SectionNameToStr (Type);\r
1268 printf ("------------------------------------------------------------\n");\r
1269 printf (" Type: %s\n Size: 0x%08X\n", SectionName, (unsigned) SectionLength);\r
1270 free (SectionName);\r
1271\r
1272 switch (Type) {\r
1273 case EFI_SECTION_RAW:\r
1274 case EFI_SECTION_PE32:\r
1275 case EFI_SECTION_PIC:\r
1276 case EFI_SECTION_TE:\r
1277 // default is no more information\r
1278 break;\r
1279\r
1280 case EFI_SECTION_USER_INTERFACE:\r
1281 // name = &((EFI_USER_INTERFACE_SECTION *) Ptr)->FileNameString;\r
1282 // printf (" String: %s\n", &name);\r
1283 break;\r
1284\r
1285 case EFI_SECTION_FIRMWARE_VOLUME_IMAGE:\r
1286 Status = PrintFvInfo (((EFI_FIRMWARE_VOLUME_IMAGE_SECTION*)Ptr) + 1, TRUE);\r
1287 if (EFI_ERROR (Status)) {\r
1288 Error (NULL, 0, 0003, "printing of FV section contents failed", NULL);\r
1289 return EFI_SECTION_ERROR;\r
1290 }\r
1291 break;\r
1292\r
1293 case EFI_SECTION_COMPATIBILITY16:\r
1294 case EFI_SECTION_FREEFORM_SUBTYPE_GUID:\r
1295 //\r
1296 // Section does not contain any further header information.\r
1297 //\r
1298 break;\r
1299\r
1300 case EFI_SECTION_PEI_DEPEX:\r
1301 case EFI_SECTION_DXE_DEPEX:\r
1302 case EFI_SECTION_SMM_DEPEX:\r
1303 DumpDepexSection (Ptr, SectionLength);\r
1304 break;\r
1305\r
1306 case EFI_SECTION_VERSION:\r
1307 printf (" Build Number: 0x%02X\n", ((EFI_VERSION_SECTION *) Ptr)->BuildNumber);\r
1308 printf (" Version Strg: %s\n", (char*) ((EFI_VERSION_SECTION *) Ptr)->VersionString);\r
1309 break;\r
1310\r
1311 case EFI_SECTION_COMPRESSION:\r
1312 UncompressedBuffer = NULL;\r
1313 CompressedLength = SectionLength - sizeof (EFI_COMPRESSION_SECTION);\r
1314 UncompressedLength = ((EFI_COMPRESSION_SECTION *) Ptr)->UncompressedLength;\r
1315 CompressionType = ((EFI_COMPRESSION_SECTION *) Ptr)->CompressionType;\r
1316 printf (" Uncompressed Length: 0x%08X\n", (unsigned) UncompressedLength);\r
1317\r
1318 if (CompressionType == EFI_NOT_COMPRESSED) {\r
1319 printf (" Compression Type: EFI_NOT_COMPRESSED\n");\r
1320 if (CompressedLength != UncompressedLength) {\r
1321 Error (\r
1322 NULL,\r
1323 0,\r
1324 0,\r
1325 "file is not compressed, but the compressed length does not match the uncompressed length",\r
1326 NULL\r
1327 );\r
1328 return EFI_SECTION_ERROR;\r
1329 }\r
1330\r
1331 UncompressedBuffer = Ptr + sizeof (EFI_COMPRESSION_SECTION);\r
1332 } else if (CompressionType == EFI_STANDARD_COMPRESSION) {\r
1333 GetInfoFunction = EfiGetInfo;\r
1334 DecompressFunction = EfiDecompress;\r
1335 printf (" Compression Type: EFI_STANDARD_COMPRESSION\n");\r
1336\r
1337 CompressedBuffer = Ptr + sizeof (EFI_COMPRESSION_SECTION);\r
1338\r
1339 Status = GetInfoFunction (CompressedBuffer, CompressedLength, &DstSize, &ScratchSize);\r
1340 if (EFI_ERROR (Status)) {\r
1341 Error (NULL, 0, 0003, "error getting compression info from compression section", NULL);\r
1342 return EFI_SECTION_ERROR;\r
1343 }\r
1344\r
1345 if (DstSize != UncompressedLength) {\r
1346 Error (NULL, 0, 0003, "compression error in the compression section", NULL);\r
1347 return EFI_SECTION_ERROR;\r
1348 }\r
1349\r
1350 ScratchBuffer = malloc (ScratchSize);\r
1351 UncompressedBuffer = malloc (UncompressedLength);\r
1352 if ((ScratchBuffer == NULL) || (UncompressedBuffer == NULL)) {\r
1353 return EFI_OUT_OF_RESOURCES;\r
1354 }\r
1355 Status = DecompressFunction (\r
1356 CompressedBuffer,\r
1357 CompressedLength,\r
1358 UncompressedBuffer,\r
1359 UncompressedLength,\r
1360 ScratchBuffer,\r
1361 ScratchSize\r
1362 );\r
1363 free (ScratchBuffer);\r
1364 if (EFI_ERROR (Status)) {\r
1365 Error (NULL, 0, 0003, "decompress failed", NULL);\r
1366 free (UncompressedBuffer);\r
1367 return EFI_SECTION_ERROR;\r
1368 }\r
1369 } else {\r
1370 Error (NULL, 0, 0003, "unrecognized compression type", "type 0x%X", CompressionType);\r
1371 return EFI_SECTION_ERROR;\r
1372 }\r
1373\r
1374 Status = ParseSection (UncompressedBuffer, UncompressedLength);\r
1375\r
1376 if (CompressionType == EFI_STANDARD_COMPRESSION) {\r
1377 //\r
1378 // We need to deallocate Buffer\r
1379 //\r
1380 free (UncompressedBuffer);\r
1381 }\r
1382\r
1383 if (EFI_ERROR (Status)) {\r
1384 Error (NULL, 0, 0003, "failed to parse section", NULL);\r
1385 return EFI_SECTION_ERROR;\r
1386 }\r
1387 break;\r
1388\r
1389 case EFI_SECTION_GUID_DEFINED:\r
1390 printf (" SectionDefinitionGuid: ");\r
1391 PrintGuid (&((EFI_GUID_DEFINED_SECTION *) Ptr)->SectionDefinitionGuid);\r
1392 printf ("\n");\r
1393 printf (" DataOffset: 0x%04X\n", (unsigned) ((EFI_GUID_DEFINED_SECTION *) Ptr)->DataOffset);\r
1394 printf (" Attributes: 0x%04X\n", (unsigned) ((EFI_GUID_DEFINED_SECTION *) Ptr)->Attributes);\r
1395\r
1396 ExtractionTool =\r
1397 LookupGuidedSectionToolPath (\r
1398 mParsedGuidedSectionTools,\r
1399 &((EFI_GUID_DEFINED_SECTION *) Ptr)->SectionDefinitionGuid\r
1400 );\r
1401\r
1402 if (ExtractionTool != NULL) {\r
1403\r
1404 ToolInputFile = CloneString (tmpnam (NULL));\r
1405 ToolOutputFile = CloneString (tmpnam (NULL));\r
1406\r
1407 //\r
1408 // Construction 'system' command string\r
1409 //\r
1410 SystemCommandFormatString = "%s -d -o %s %s";\r
1411 SystemCommand = malloc (\r
1412 strlen (SystemCommandFormatString) +\r
1413 strlen (ExtractionTool) +\r
1414 strlen (ToolInputFile) +\r
1415 strlen (ToolOutputFile) +\r
1416 1\r
1417 );\r
1418 sprintf (\r
1419 SystemCommand,\r
1420 SystemCommandFormatString,\r
1421 ExtractionTool,\r
1422 ToolOutputFile,\r
1423 ToolInputFile\r
1424 );\r
1425 free (ExtractionTool);\r
1426\r
1427 Status =\r
1428 PutFileImage (\r
1429 ToolInputFile,\r
1430 (CHAR8*) SectionBuffer + ((EFI_GUID_DEFINED_SECTION *) Ptr)->DataOffset,\r
1431 BufferLength - ((EFI_GUID_DEFINED_SECTION *) Ptr)->DataOffset\r
1432 );\r
1433\r
1434 system (SystemCommand);\r
1435 remove (ToolInputFile);\r
1436 free (ToolInputFile);\r
1437\r
1438 Status =\r
1439 GetFileImage (\r
1440 ToolOutputFile,\r
1441 (CHAR8 **)&ToolOutputBuffer,\r
1442 &ToolOutputLength\r
1443 );\r
1444 remove (ToolOutputFile);\r
1445 free (ToolOutputFile);\r
1446 if (EFI_ERROR (Status)) {\r
1447 Error (NULL, 0, 0004, "unable to read decoded GUIDED section", NULL);\r
1448 return EFI_SECTION_ERROR;\r
1449 }\r
1450\r
1451 Status = ParseSection (\r
1452 ToolOutputBuffer,\r
1453 ToolOutputLength\r
1454 );\r
1455 if (EFI_ERROR (Status)) {\r
1456 Error (NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL);\r
1457 return EFI_SECTION_ERROR;\r
1458 }\r
1459\r
1460 //\r
1461 // Check for CRC32 sections which we can handle internally if needed.\r
1462 //\r
1463 } else if (!CompareGuid (\r
1464 &((EFI_GUID_DEFINED_SECTION *) Ptr)->SectionDefinitionGuid,\r
1465 &gEfiCrc32GuidedSectionExtractionProtocolGuid\r
1466 )\r
1467 ) {\r
1468 //\r
1469 // CRC32 guided section\r
1470 //\r
1471 Status = ParseSection (\r
1472 SectionBuffer + ((EFI_GUID_DEFINED_SECTION *) Ptr)->DataOffset,\r
1473 BufferLength - ((EFI_GUID_DEFINED_SECTION *) Ptr)->DataOffset\r
1474 );\r
1475 if (EFI_ERROR (Status)) {\r
1476 Error (NULL, 0, 0003, "parse of CRC32 GUIDED section failed", NULL);\r
1477 return EFI_SECTION_ERROR;\r
1478 }\r
1479 } else {\r
1480 //\r
1481 // We don't know how to parse it now.\r
1482 //\r
1483 Error (NULL, 0, 0003, "Error parsing section", \\r
1484 "EFI_SECTION_GUID_DEFINED cannot be parsed at this time. Tool to decode this section should have been defined in GuidedSectionTools.txt (built in the FV directory).");\r
1485 return EFI_UNSUPPORTED;\r
1486 }\r
1487 break;\r
1488\r
1489 default:\r
1490 //\r
1491 // Unknown section, return error\r
1492 //\r
1493 Error (NULL, 0, 0003, "unrecognized section type found", "section type = 0x%X", Type);\r
1494 return EFI_SECTION_ERROR;\r
1495 }\r
1496\r
1497 ParsedLength += SectionLength;\r
1498 //\r
1499 // We make then next section begin on a 4-byte boundary\r
1500 //\r
1501 ParsedLength = GetOccupiedSize (ParsedLength, 4);\r
1502 }\r
1503\r
1504 if (ParsedLength < BufferLength) {\r
1505 Error (NULL, 0, 0003, "sections do not completely fill the sectioned buffer being parsed", NULL);\r
1506 return EFI_SECTION_ERROR;\r
1507 }\r
1508\r
1509 return EFI_SUCCESS;\r
1510}\r
1511\r
1512EFI_STATUS\r
1513DumpDepexSection (\r
1514 IN UINT8 *Ptr,\r
1515 IN UINT32 SectionLength\r
1516 )\r
1517/*++\r
1518\r
1519Routine Description:\r
1520\r
1521 GC_TODO: Add function description\r
1522\r
1523Arguments:\r
1524\r
1525 Ptr - GC_TODO: add argument description\r
1526 SectionLength - GC_TODO: add argument description\r
1527\r
1528Returns:\r
1529\r
1530 EFI_SUCCESS - GC_TODO: Add description for return value\r
1531\r
1532--*/\r
1533{\r
1534 UINT8 GuidBuffer[PRINTED_GUID_BUFFER_SIZE];\r
1535\r
1536 //\r
1537 // Need at least a section header + data\r
1538 //\r
1539 if (SectionLength <= sizeof (EFI_COMMON_SECTION_HEADER)) {\r
1540 return EFI_SUCCESS;\r
1541 }\r
1542\r
1543 Ptr += sizeof (EFI_COMMON_SECTION_HEADER);\r
1544 SectionLength -= sizeof (EFI_COMMON_SECTION_HEADER);\r
1545 while (SectionLength > 0) {\r
1546 printf (" ");\r
1547 switch (*Ptr) {\r
1548 case EFI_DEP_BEFORE:\r
1549 printf ("BEFORE\n");\r
1550 Ptr++;\r
1551 SectionLength--;\r
1552 break;\r
1553\r
1554 case EFI_DEP_AFTER:\r
1555 printf ("AFTER\n");\r
1556 Ptr++;\r
1557 SectionLength--;\r
1558 break;\r
1559\r
1560 case EFI_DEP_PUSH:\r
1561 printf ("PUSH\n ");\r
1562 PrintGuidToBuffer ((EFI_GUID *) (Ptr + 1), GuidBuffer, sizeof (GuidBuffer), TRUE);\r
1563 printf ("%s ", GuidBuffer);\r
1564 PrintGuidName (GuidBuffer);\r
1565 printf ("\n");\r
1566 //\r
1567 // PrintGuid ((EFI_GUID *)(Ptr + 1));\r
1568 //\r
1569 Ptr += 17;\r
1570 SectionLength -= 17;\r
1571 break;\r
1572\r
1573 case EFI_DEP_AND:\r
1574 printf ("AND\n");\r
1575 Ptr++;\r
1576 SectionLength--;\r
1577 break;\r
1578\r
1579 case EFI_DEP_OR:\r
1580 printf ("OR\n");\r
1581 Ptr++;\r
1582 SectionLength--;\r
1583 break;\r
1584\r
1585 case EFI_DEP_NOT:\r
1586 printf ("NOT\n");\r
1587 Ptr++;\r
1588 SectionLength--;\r
1589 break;\r
1590\r
1591 case EFI_DEP_TRUE:\r
1592 printf ("TRUE\n");\r
1593 Ptr++;\r
1594 SectionLength--;\r
1595 break;\r
1596\r
1597 case EFI_DEP_FALSE:\r
1598 printf ("FALSE\n");\r
1599 Ptr++;\r
1600 SectionLength--;\r
1601 break;\r
1602\r
1603 case EFI_DEP_END:\r
1604 printf ("END DEPEX\n");\r
1605 Ptr++;\r
1606 SectionLength--;\r
1607 break;\r
1608\r
1609 case EFI_DEP_SOR:\r
1610 printf ("SOR\n");\r
1611 Ptr++;\r
1612 SectionLength--;\r
1613 break;\r
1614\r
1615 default:\r
1616 printf ("Unrecognized byte in depex: 0x%X\n", *Ptr);\r
1617 return EFI_SUCCESS;\r
1618 }\r
1619 }\r
1620\r
1621 return EFI_SUCCESS;\r
1622}\r
1623\r
1624EFI_STATUS\r
1625PrintGuidName (\r
1626 IN UINT8 *GuidStr\r
1627 )\r
1628/*++\r
1629\r
1630Routine Description:\r
1631\r
1632 GC_TODO: Add function description\r
1633\r
1634Arguments:\r
1635\r
1636 GuidStr - GC_TODO: add argument description\r
1637\r
1638Returns:\r
1639\r
1640 EFI_SUCCESS - GC_TODO: Add description for return value\r
1641 EFI_INVALID_PARAMETER - GC_TODO: Add description for return value\r
1642\r
1643--*/\r
1644{\r
1645 GUID_TO_BASENAME *GPtr;\r
1646 //\r
1647 // If we have a list of guid-to-basenames, then go through the list to\r
1648 // look for a guid string match. If found, print the basename to stdout,\r
1649 // otherwise return a failure.\r
1650 //\r
1651 GPtr = mGuidBaseNameList;\r
1652 while (GPtr != NULL) {\r
1653 if (_stricmp ((CHAR8*) GuidStr, (CHAR8*) GPtr->Guid) == 0) {\r
1654 printf ("%s", GPtr->BaseName);\r
1655 return EFI_SUCCESS;\r
1656 }\r
1657\r
1658 GPtr = GPtr->Next;\r
1659 }\r
1660\r
1661 return EFI_INVALID_PARAMETER;\r
1662}\r
1663\r
1664EFI_STATUS\r
1665ParseGuidBaseNameFile (\r
1666 CHAR8 *FileName\r
1667 )\r
1668/*++\r
1669\r
1670Routine Description:\r
1671\r
1672 GC_TODO: Add function description\r
1673\r
1674Arguments:\r
1675\r
1676 FileName - GC_TODO: add argument description\r
1677\r
1678Returns:\r
1679\r
1680 EFI_DEVICE_ERROR - GC_TODO: Add description for return value\r
1681 EFI_OUT_OF_RESOURCES - GC_TODO: Add description for return value\r
1682 EFI_SUCCESS - GC_TODO: Add description for return value\r
1683\r
1684--*/\r
1685{\r
1686 FILE *Fptr;\r
1687 CHAR8 Line[MAX_LINE_LEN];\r
1688 GUID_TO_BASENAME *GPtr;\r
1689\r
1690 if ((Fptr = fopen (FileName, "r")) == NULL) {\r
1691 printf ("ERROR: Failed to open input cross-reference file '%s'\n", FileName);\r
1692 return EFI_DEVICE_ERROR;\r
1693 }\r
1694\r
1695 while (fgets (Line, sizeof (Line), Fptr) != NULL) {\r
1696 //\r
1697 // Allocate space for another guid/basename element\r
1698 //\r
1699 GPtr = malloc (sizeof (GUID_TO_BASENAME));\r
1700 if (GPtr == NULL) {\r
1701 return EFI_OUT_OF_RESOURCES;\r
1702 }\r
1703\r
1704 memset ((char *) GPtr, 0, sizeof (GUID_TO_BASENAME));\r
1705 if (sscanf (Line, "%s %s", GPtr->Guid, GPtr->BaseName) == 2) {\r
1706 GPtr->Next = mGuidBaseNameList;\r
1707 mGuidBaseNameList = GPtr;\r
1708 } else {\r
1709 //\r
1710 // Some sort of error. Just continue.\r
1711 //\r
1712 free (GPtr);\r
1713 }\r
1714 }\r
1715\r
1716 fclose (Fptr);\r
1717 return EFI_SUCCESS;\r
1718}\r
1719\r
1720EFI_STATUS\r
1721FreeGuidBaseNameList (\r
1722 VOID\r
1723 )\r
1724/*++\r
1725\r
1726Routine Description:\r
1727\r
1728 GC_TODO: Add function description\r
1729\r
1730Arguments:\r
1731\r
1732 None\r
1733\r
1734Returns:\r
1735\r
1736 EFI_SUCCESS - GC_TODO: Add description for return value\r
1737\r
1738--*/\r
1739{\r
1740 GUID_TO_BASENAME *Next;\r
1741\r
1742 while (mGuidBaseNameList != NULL) {\r
1743 Next = mGuidBaseNameList->Next;\r
1744 free (mGuidBaseNameList);\r
1745 mGuidBaseNameList = Next;\r
1746 }\r
1747\r
1748 return EFI_SUCCESS;\r
1749}\r
1750\r
1751\r
1752static\r
1753VOID\r
1754LoadGuidedSectionToolsTxt (\r
1755 IN CHAR8* FirmwareVolumeFilename\r
1756 )\r
1757{\r
1758 CHAR8* PeerFilename;\r
1759 CHAR8* Places[] = {\r
1760 NULL,\r
1761 //NULL,\r
1762 };\r
1763 UINTN Index;\r
1764\r
1765 Places[0] = FirmwareVolumeFilename;\r
1766 //Places[1] = mUtilityFilename;\r
1767\r
1768 mParsedGuidedSectionTools = NULL;\r
1769\r
1770 for (Index = 0; Index < (sizeof(Places)/sizeof(Places[0])); Index++) {\r
1771 PeerFilename = OsPathPeerFilePath (Places[Index], "GuidedSectionTools.txt");\r
1772 //printf("Loading %s...\n", PeerFilename);\r
1773 if (OsPathExists (PeerFilename)) {\r
1774 mParsedGuidedSectionTools = ParseGuidedSectionToolsFile (PeerFilename);\r
1775 }\r
1776 free (PeerFilename);\r
1777 if (mParsedGuidedSectionTools != NULL) {\r
1778 return;\r
1779 }\r
1780 }\r
1781}\r
1782\r
1783\r
1784void\r
1785Usage (\r
1786 VOID\r
1787 )\r
1788/*++\r
1789\r
1790Routine Description:\r
1791\r
1792 GC_TODO: Add function description\r
1793\r
1794Arguments:\r
1795\r
1796 None\r
1797\r
1798Returns:\r
1799\r
1800 GC_TODO: add return values\r
1801\r
1802--*/\r
1803{\r
1804 //\r
1805 // Summary usage\r
1806 //\r
1807 fprintf (stdout, "Usage: %s [options] <input_file>\n\n", UTILITY_NAME);\r
1808\r
1809 //\r
1810 // Copyright declaration\r
1811 // \r
1812 fprintf (stdout, "Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.\n\n");\r
1813\r
1814 //\r
1815 // Details Option\r
1816 //\r
1817 fprintf (stdout, "Options:\n");\r
1818 fprintf (stdout, " -x xref, --xref xref\n\\r
1819 Parse basename to file-guid cross reference file(s).\n");\r
1820 fprintf (stdout, " --offset offset\n\\r
1821 Offset of file to start processing FV at.\n");\r
1822 fprintf (stdout, " -h, --help\n\\r
1823 Show this help message and exit.\n");\r
1824\r
1825}\r
1826\r