]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/C/VolInfo/VolInfo.c
BaseTools/VfrCompile: Avoid possible NULL pointer dereference
[mirror_edk2.git] / BaseTools / Source / C / VolInfo / VolInfo.c
CommitLineData
f51461c8 1/** @file\r
97fa0ee9 2The tool dumps the contents of a firmware volume\r
f51461c8 3\r
45258285 4Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.<BR>\r
f51461c8
LG
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
f51461c8
LG
13**/\r
14\r
15#include <stdio.h>\r
16#include <stdlib.h>\r
17#include <string.h>\r
18#include <ctype.h>\r
19#include <assert.h>\r
868c9c35
YZ
20#ifdef __GNUC__\r
21#include <unistd.h>\r
9947f576
YZ
22#else\r
23#include <direct.h>\r
868c9c35 24#endif\r
f51461c8
LG
25\r
26#include <FvLib.h>\r
27#include <Common/UefiBaseTypes.h>\r
28#include <Common/UefiCapsule.h>\r
29#include <Common/PiFirmwareFile.h>\r
30#include <Common/PiFirmwareVolume.h>\r
31#include <Guid/PiFirmwareFileSystem.h>\r
32#include <IndustryStandard/PeImage.h>\r
33#include <Protocol/GuidedSectionExtraction.h>\r
34\r
35#include "Compress.h"\r
36#include "Decompress.h"\r
37#include "VolInfo.h"\r
38#include "CommonLib.h"\r
39#include "EfiUtilityMsgs.h"\r
40#include "FirmwareVolumeBufferLib.h"\r
41#include "OsPath.h"\r
42#include "ParseGuidedSectionTools.h"\r
43#include "StringFuncs.h"\r
730ffca1 44#include "ParseInf.h"\r
9947f576 45#include "PeCoffLib.h"\r
f51461c8
LG
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
45258285
LG
53#define UTILITY_MAJOR_VERSION 1\r
54#define UTILITY_MINOR_VERSION 0\r
f51461c8
LG
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
9947f576
YZ
80BOOLEAN EnableHash = FALSE;\r
81CHAR8 *OpenSslPath = NULL;\r
82\r
f51461c8
LG
83EFI_STATUS\r
84ParseGuidBaseNameFile (\r
85 CHAR8 *FileName\r
86 );\r
87\r
88EFI_STATUS\r
89FreeGuidBaseNameList (\r
90 VOID\r
91 );\r
92\r
93EFI_STATUS\r
94PrintGuidName (\r
95 IN UINT8 *GuidStr\r
96 );\r
97\r
98EFI_STATUS\r
99ParseSection (\r
100 IN UINT8 *SectionBuffer,\r
101 IN UINT32 BufferLength\r
102 );\r
103\r
104EFI_STATUS\r
105DumpDepexSection (\r
106 IN UINT8 *Ptr,\r
107 IN UINT32 SectionLength\r
108 );\r
109\r
110STATIC\r
111EFI_STATUS\r
112ReadHeader (\r
113 IN FILE *InputFile,\r
114 OUT UINT32 *FvSize,\r
115 OUT BOOLEAN *ErasePolarity\r
116 );\r
117\r
118STATIC\r
119EFI_STATUS\r
120PrintFileInfo (\r
121 EFI_FIRMWARE_VOLUME_HEADER *FvImage,\r
122 EFI_FFS_FILE_HEADER *FileHeader,\r
123 BOOLEAN ErasePolarity\r
124 );\r
125\r
126static\r
127EFI_STATUS\r
128PrintFvInfo (\r
129 IN VOID *Fv,\r
130 IN BOOLEAN IsChildFv\r
131 );\r
132\r
133static\r
134VOID\r
135LoadGuidedSectionToolsTxt (\r
136 IN CHAR8* FirmwareVolumeFilename\r
137 );\r
138\r
9947f576
YZ
139EFI_STATUS\r
140CombinePath (\r
141 IN CHAR8* DefaultPath,\r
142 IN CHAR8* AppendPath,\r
143 OUT CHAR8* NewPath\r
144);\r
145\r
f51461c8
LG
146void\r
147Usage (\r
148 VOID\r
149 );\r
150\r
151int\r
152main (\r
153 int argc,\r
154 char *argv[]\r
155 )\r
156/*++\r
157\r
158Routine Description:\r
159\r
160 GC_TODO: Add function description\r
161\r
162Arguments:\r
163\r
164 argc - GC_TODO: add argument description\r
165 ] - GC_TODO: add argument description\r
166\r
167Returns:\r
168\r
169 GC_TODO: add return values\r
170\r
171--*/\r
172{\r
173 FILE *InputFile;\r
174 int BytesRead;\r
175 EFI_FIRMWARE_VOLUME_HEADER *FvImage;\r
176 UINT32 FvSize;\r
177 EFI_STATUS Status;\r
178 int Offset;\r
179 BOOLEAN ErasePolarity;\r
730ffca1 180 UINT64 LogLevel;\r
9947f576
YZ
181 CHAR8 *OpenSslEnv;\r
182 CHAR8 *OpenSslCommand;\r
f51461c8
LG
183\r
184 SetUtilityName (UTILITY_NAME);\r
185 //\r
186 // Print utility header\r
187 //\r
45258285 188 printf ("%s Version %d.%d Build %s\n",\r
f51461c8
LG
189 UTILITY_NAME,\r
190 UTILITY_MAJOR_VERSION,\r
191 UTILITY_MINOR_VERSION,\r
45258285 192 __BUILD_VERSION\r
f51461c8
LG
193 );\r
194\r
730ffca1
YZ
195 if (argc == 1) {\r
196 Usage ();\r
197 return -1;\r
198 }\r
199\r
f51461c8
LG
200 argc--;\r
201 argv++;\r
730ffca1 202 LogLevel = 0;\r
f51461c8
LG
203 Offset = 0;\r
204\r
730ffca1
YZ
205 //\r
206 // Look for help options\r
207 //\r
208 if ((strcmp(argv[0], "-h") == 0) || (strcmp(argv[0], "--help") == 0) ||\r
209 (strcmp(argv[0], "-?") == 0) || (strcmp(argv[0], "/?") == 0)) {\r
210 Usage();\r
211 return STATUS_SUCCESS;\r
212 }\r
213 //\r
214 // Version has already be printed, so just return success\r
215 //\r
216 if (strcmp(argv[0], "--version") == 0) {\r
217 return STATUS_SUCCESS;\r
218 }\r
219\r
f51461c8
LG
220 //\r
221 // If they specified -x xref guid/basename cross-reference files, process it.\r
222 // This will print the basename beside each file guid. To use it, specify\r
223 // -x xref_filename to processdsc, then use xref_filename as a parameter\r
224 // here.\r
225 //\r
730ffca1 226 while (argc > 0) {\r
f51461c8
LG
227 if ((strcmp(argv[0], "-x") == 0) || (strcmp(argv[0], "--xref") == 0)) {\r
228 ParseGuidBaseNameFile (argv[1]);\r
229 printf("ParseGuidBaseNameFile: %s\n", argv[1]);\r
230 argc -= 2;\r
231 argv += 2;\r
730ffca1
YZ
232 continue;\r
233 }\r
234 if (strcmp(argv[0], "--offset") == 0) {\r
f51461c8
LG
235 //\r
236 // Hex or decimal?\r
237 //\r
238 if ((argv[1][0] == '0') && (tolower ((int)argv[1][1]) == 'x')) {\r
239 if (sscanf (argv[1], "%x", &Offset) != 1) {\r
240 Error (NULL, 0, 1003, "Invalid option value", "Offset = %s", argv[1]);\r
241 return GetUtilityStatus ();\r
242 }\r
243 } else {\r
244 if (sscanf (argv[1], "%d", &Offset) != 1) {\r
245 Error (NULL, 0, 1003, "Invalid option value", "Offset = %s", argv[1]);\r
246 return GetUtilityStatus ();\r
247 }\r
248 //\r
249 // See if they said something like "64K"\r
250 //\r
251 if (tolower ((int)argv[1][strlen (argv[1]) - 1]) == 'k') {\r
252 Offset *= 1024;\r
253 }\r
254 }\r
255\r
256 argc -= 2;\r
257 argv += 2;\r
730ffca1 258 continue;\r
f51461c8 259 }\r
9947f576
YZ
260 if ((stricmp (argv[0], "--hash") == 0)) {\r
261 EnableHash = TRUE;\r
262 OpenSslCommand = "openssl";\r
263 OpenSslEnv = getenv("OPENSSL_PATH");\r
264 if (OpenSslEnv == NULL) {\r
265 OpenSslPath = OpenSslCommand;\r
266 } else {\r
267 OpenSslPath = malloc(strlen(OpenSslEnv)+strlen(OpenSslCommand)+1);\r
268 CombinePath(OpenSslEnv, OpenSslCommand, OpenSslPath);\r
269 }\r
270 if (OpenSslPath == NULL){\r
271 Error (NULL, 0, 3000, "Open SSL command not available. Please verify PATH or set OPENSSL_PATH.", NULL);\r
272 return GetUtilityStatus ();\r
273 }\r
274 argc --;\r
275 argv ++;\r
276 continue;\r
277 }\r
730ffca1
YZ
278\r
279 if ((stricmp (argv[0], "-v") == 0) || (stricmp (argv[0], "--verbose") == 0)) {\r
280 SetPrintLevel (VERBOSE_LOG_LEVEL);\r
281 argc --;\r
282 argv ++;\r
283 continue;\r
284 }\r
285\r
286 if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) {\r
287 SetPrintLevel (KEY_LOG_LEVEL);\r
288 argc --;\r
289 argv ++;\r
290 continue;\r
291 }\r
292\r
293 if ((stricmp (argv[0], "-d") == 0) || (stricmp (argv[0], "--debug") == 0)) {\r
294 Status = AsciiStringToUint64 (argv[1], FALSE, &LogLevel);\r
295 if (EFI_ERROR (Status)) {\r
296 Error (NULL, 0, 1003, "Invalid option value", "%s = %s", argv[0], argv[1]);\r
297 return -1;\r
298 }\r
299 if (LogLevel > 9) {\r
300 Error (NULL, 0, 1003, "Invalid option value", "Debug Level range is 0-9, current input level is %d", (int) LogLevel);\r
301 return -1;\r
302 }\r
303 SetPrintLevel (LogLevel);\r
304 DebugMsg (NULL, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", argv[1]);\r
305 argc -= 2;\r
306 argv += 2;\r
307 continue;\r
308 }\r
309\r
310 mUtilityFilename = argv[0];\r
311 argc --;\r
312 argv ++;\r
f51461c8 313 }\r
730ffca1 314\r
f51461c8
LG
315 //\r
316 // Open the file containing the FV\r
317 //\r
730ffca1
YZ
318 if (mUtilityFilename == NULL) {\r
319 Error (NULL, 0, 1001, "Missing option", "Input files are not specified");\r
320 return GetUtilityStatus ();\r
321 }\r
322 InputFile = fopen (LongFilePath (mUtilityFilename), "rb");\r
f51461c8 323 if (InputFile == NULL) {\r
730ffca1 324 Error (NULL, 0, 0001, "Error opening the input file", mUtilityFilename);\r
f51461c8
LG
325 return GetUtilityStatus ();\r
326 }\r
327 //\r
328 // Skip over pad bytes if specified. This is used if they prepend 0xff\r
329 // data to the FV image binary.\r
330 //\r
331 if (Offset != 0) {\r
332 fseek (InputFile, Offset, SEEK_SET);\r
333 }\r
334 //\r
335 // Determine size of FV\r
336 //\r
337 Status = ReadHeader (InputFile, &FvSize, &ErasePolarity);\r
338 if (EFI_ERROR (Status)) {\r
730ffca1 339 Error (NULL, 0, 0003, "error parsing FV image", "%s Header is invalid", mUtilityFilename);\r
f51461c8
LG
340 fclose (InputFile);\r
341 return GetUtilityStatus ();\r
342 }\r
343 //\r
344 // Allocate a buffer for the FV image\r
345 //\r
346 FvImage = malloc (FvSize);\r
347 if (FvImage == NULL) {\r
348 Error (NULL, 0, 4001, "Resource: Memory can't be allocated", NULL);\r
349 fclose (InputFile);\r
350 return GetUtilityStatus ();\r
351 }\r
352 //\r
353 // Seek to the start of the image, then read the entire FV to the buffer\r
354 //\r
355 fseek (InputFile, Offset, SEEK_SET);\r
356 BytesRead = fread (FvImage, 1, FvSize, InputFile);\r
357 fclose (InputFile);\r
358 if ((unsigned int) BytesRead != FvSize) {\r
730ffca1 359 Error (NULL, 0, 0004, "error reading FvImage from", mUtilityFilename);\r
f51461c8
LG
360 free (FvImage);\r
361 return GetUtilityStatus ();\r
362 }\r
363\r
730ffca1 364 LoadGuidedSectionToolsTxt (mUtilityFilename);\r
f51461c8
LG
365\r
366 PrintFvInfo (FvImage, FALSE);\r
367\r
368 //\r
369 // Clean up\r
370 //\r
371 free (FvImage);\r
372 FreeGuidBaseNameList ();\r
373 return GetUtilityStatus ();\r
374}\r
375\r
376\r
377static\r
378EFI_STATUS\r
379PrintFvInfo (\r
380 IN VOID *Fv,\r
381 IN BOOLEAN IsChildFv\r
382 )\r
383/*++\r
384\r
385Routine Description:\r
386\r
387 GC_TODO: Add function description\r
388\r
389Arguments:\r
390\r
391 Fv - Firmware Volume to print information about\r
392 IsChildFv - Flag specifies whether the input FV is a child FV.\r
393\r
394Returns:\r
395\r
396 EFI_STATUS\r
397\r
398--*/\r
399{\r
400 EFI_STATUS Status;\r
401 UINTN NumberOfFiles;\r
402 BOOLEAN ErasePolarity;\r
403 UINTN FvSize;\r
404 EFI_FFS_FILE_HEADER *CurrentFile;\r
405 UINTN Key;\r
406\r
407 Status = FvBufGetSize (Fv, &FvSize);\r
408\r
409 NumberOfFiles = 0;\r
410 ErasePolarity =\r
411 (((EFI_FIRMWARE_VOLUME_HEADER*)Fv)->Attributes & EFI_FVB2_ERASE_POLARITY) ?\r
412 TRUE : FALSE;\r
413\r
414 //\r
415 // Get the first file\r
416 //\r
417 Key = 0;\r
418 Status = FvBufFindNextFile (Fv, &Key, (VOID **) &CurrentFile);\r
419 if (EFI_ERROR (Status)) {\r
420 Error (NULL, 0, 0003, "error parsing FV image", "cannot find the first file in the FV image");\r
421 return GetUtilityStatus ();\r
422 }\r
423 //\r
424 // Display information about files found\r
425 //\r
426 while (CurrentFile != NULL) {\r
427 //\r
428 // Increment the number of files counter\r
429 //\r
430 NumberOfFiles++;\r
431\r
432 //\r
433 // Display info about this file\r
434 //\r
435 Status = PrintFileInfo (Fv, CurrentFile, ErasePolarity);\r
436 if (EFI_ERROR (Status)) {\r
437 Error (NULL, 0, 0003, "error parsing FV image", "failed to parse a file in the FV");\r
438 return GetUtilityStatus ();\r
439 }\r
440 //\r
441 // Get the next file\r
442 //\r
443 Status = FvBufFindNextFile (Fv, &Key, (VOID **) &CurrentFile);\r
444 if (Status == EFI_NOT_FOUND) {\r
445 CurrentFile = NULL;\r
446 } else if (EFI_ERROR (Status)) {\r
447 Error (NULL, 0, 0003, "error parsing FV image", "cannot find the next file in the FV image");\r
448 return GetUtilityStatus ();\r
449 }\r
450 }\r
451\r
452 if (IsChildFv) {\r
453 printf ("There are a total of %d files in the child FV\n", (int) NumberOfFiles);\r
454 } else {\r
455 printf ("There are a total of %d files in this FV\n", (int) NumberOfFiles);\r
456 }\r
457\r
458 return EFI_SUCCESS;\r
459}\r
460\r
461UINT32\r
462GetOccupiedSize (\r
463 IN UINT32 ActualSize,\r
464 IN UINT32 Alignment\r
465 )\r
466/*++\r
467\r
468Routine Description:\r
469\r
470 This function returns the next larger size that meets the alignment \r
471 requirement specified.\r
472\r
473Arguments:\r
474\r
475 ActualSize The size.\r
476 Alignment The desired alignment.\r
477 \r
478Returns:\r
479 \r
480 EFI_SUCCESS Function completed successfully.\r
481 EFI_ABORTED The function encountered an error.\r
482\r
483--*/\r
484{\r
485 UINT32 OccupiedSize;\r
486\r
487 OccupiedSize = ActualSize;\r
488 while ((OccupiedSize & (Alignment - 1)) != 0) {\r
489 OccupiedSize++;\r
490 }\r
491\r
492 return OccupiedSize;\r
493}\r
494\r
495static\r
496CHAR8 *\r
497SectionNameToStr (\r
498 IN EFI_SECTION_TYPE Type\r
499 )\r
500/*++\r
501\r
502Routine Description:\r
503\r
504 Converts EFI Section names to Strings\r
505\r
506Arguments:\r
507\r
508 Type - The EFI Section type\r
509\r
510Returns:\r
511\r
512 CHAR8* - Pointer to the String containing the section name.\r
513\r
514--*/\r
515{\r
516 CHAR8 *SectionStr;\r
517 CHAR8 *SectionTypeStringTable[] = {\r
518 //\r
519 // 0X00\r
520 //\r
521 "EFI_SECTION_ALL",\r
522 //\r
523 // 0x01\r
524 //\r
525 "EFI_SECTION_COMPRESSION",\r
526 //\r
527 // 0x02\r
528 //\r
529 "EFI_SECTION_GUID_DEFINED", \r
530 //\r
531 // 0x03\r
532 //\r
533 "Unknown section type - Reserved 0x03",\r
534 //\r
535 // 0x04\r
536 //\r
537 "Unknown section type - Reserved 0x04",\r
538 //\r
539 // 0x05\r
540 //\r
541 "Unknown section type - Reserved 0x05",\r
542 //\r
543 // 0x06\r
544 //\r
545 "Unknown section type - Reserved 0x06",\r
546 //\r
547 // 0x07\r
548 //\r
549 "Unknown section type - Reserved 0x07",\r
550 //\r
551 // 0x08\r
552 //\r
553 "Unknown section type - Reserved 0x08",\r
554 //\r
555 // 0x09\r
556 //\r
557 "Unknown section type - Reserved 0x09",\r
558 //\r
559 // 0x0A\r
560 //\r
561 "Unknown section type - Reserved 0x0A",\r
562 //\r
563 // 0x0B\r
564 //\r
565 "Unknown section type - Reserved 0x0B",\r
566 //\r
567 // 0x0C\r
568 //\r
569 "Unknown section type - Reserved 0x0C",\r
570 //\r
571 // 0x0D\r
572 //\r
573 "Unknown section type - Reserved 0x0D",\r
574 //\r
575 // 0x0E\r
576 //\r
577 "Unknown section type - Reserved 0x0E",\r
578 //\r
579 // 0x0F\r
580 //\r
581 "Unknown section type - Reserved 0x0E",\r
582 //\r
583 // 0x10\r
584 //\r
585 "EFI_SECTION_PE32",\r
586 //\r
587 // 0x11\r
588 //\r
589 "EFI_SECTION_PIC",\r
590 //\r
591 // 0x12\r
592 //\r
593 "EFI_SECTION_TE", \r
594 //\r
595 // 0x13\r
596 //\r
597 "EFI_SECTION_DXE_DEPEX", \r
598 //\r
599 // 0x14\r
600 //\r
601 "EFI_SECTION_VERSION",\r
602 //\r
603 // 0x15\r
604 //\r
605 "EFI_SECTION_USER_INTERFACE",\r
606 //\r
607 // 0x16\r
608 //\r
609 "EFI_SECTION_COMPATIBILITY16",\r
610 //\r
611 // 0x17\r
612 //\r
613 "EFI_SECTION_FIRMWARE_VOLUME_IMAGE ",\r
614 //\r
615 // 0x18\r
616 //\r
617 "EFI_SECTION_FREEFORM_SUBTYPE_GUID ",\r
618 //\r
619 // 0x19\r
620 //\r
621 "EFI_SECTION_RAW",\r
622 //\r
623 // 0x1A\r
624 //\r
625 "Unknown section type - 0x1A",\r
626 //\r
627 // 0x1B\r
628 //\r
629 "EFI_SECTION_PEI_DEPEX",\r
630 //\r
631 // 0x1C\r
632 //\r
633 "EFI_SECTION_SMM_DEPEX",\r
634 //\r
635 // 0x1C+\r
636 //\r
637 "Unknown section type - Reserved - beyond last defined section"\r
638 };\r
639\r
640 if (Type > EFI_SECTION_LAST_SECTION_TYPE) {\r
641 Type = EFI_SECTION_LAST_SECTION_TYPE + 1;\r
642 }\r
643\r
644 SectionStr = malloc (100);\r
645 if (SectionStr == NULL) {\r
646 printf ("Error: Out of memory resources.\n");\r
647 return SectionStr;\r
648 }\r
649 strcpy (SectionStr, SectionTypeStringTable[Type]);\r
650 return SectionStr;\r
651}\r
652\r
653STATIC\r
654EFI_STATUS\r
655ReadHeader (\r
656 IN FILE *InputFile,\r
657 OUT UINT32 *FvSize,\r
658 OUT BOOLEAN *ErasePolarity\r
659 )\r
660/*++\r
661\r
662Routine Description:\r
663\r
664 This function determines the size of the FV and the erase polarity. The \r
665 erase polarity is the FALSE value for file state.\r
666\r
667Arguments:\r
668\r
669 InputFile The file that contains the FV image.\r
670 FvSize The size of the FV.\r
671 ErasePolarity The FV erase polarity.\r
672 \r
673Returns:\r
674 \r
675 EFI_SUCCESS Function completed successfully.\r
676 EFI_INVALID_PARAMETER A required parameter was NULL or is out of range.\r
677 EFI_ABORTED The function encountered an error.\r
678\r
679--*/\r
680{\r
681 EFI_FIRMWARE_VOLUME_HEADER VolumeHeader;\r
682 EFI_FV_BLOCK_MAP_ENTRY BlockMap;\r
683 UINTN Signature[2];\r
684 UINTN BytesRead;\r
685 UINT32 Size;\r
686\r
687 BytesRead = 0;\r
688 Size = 0;\r
689 //\r
690 // Check input parameters\r
691 //\r
692 if (InputFile == NULL || FvSize == NULL || ErasePolarity == NULL) {\r
693 Error (__FILE__, __LINE__, 0, "application error", "invalid parameter to function");\r
694 return EFI_INVALID_PARAMETER;\r
695 }\r
696 //\r
697 // Read the header\r
698 //\r
699 fread (&VolumeHeader, sizeof (EFI_FIRMWARE_VOLUME_HEADER) - sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, InputFile);\r
700 BytesRead = sizeof (EFI_FIRMWARE_VOLUME_HEADER) - sizeof (EFI_FV_BLOCK_MAP_ENTRY);\r
701 Signature[0] = VolumeHeader.Signature;\r
702 Signature[1] = 0;\r
703\r
704 //\r
705 // Print FV header information\r
706 //\r
707 printf ("Signature: %s (%X)\n", (char *) Signature, (unsigned) VolumeHeader.Signature);\r
708 printf ("Attributes: %X\n", (unsigned) VolumeHeader.Attributes);\r
709\r
710 if (VolumeHeader.Attributes & EFI_FVB2_READ_DISABLED_CAP) {\r
711 printf (" EFI_FVB2_READ_DISABLED_CAP\n");\r
712 }\r
713\r
714 if (VolumeHeader.Attributes & EFI_FVB2_READ_ENABLED_CAP) {\r
715 printf (" EFI_FVB2_READ_ENABLED_CAP\n");\r
716 }\r
717\r
718 if (VolumeHeader.Attributes & EFI_FVB2_READ_STATUS) {\r
719 printf (" EFI_FVB2_READ_STATUS\n");\r
720 }\r
721\r
722 if (VolumeHeader.Attributes & EFI_FVB2_WRITE_DISABLED_CAP) {\r
723 printf (" EFI_FVB2_WRITE_DISABLED_CAP\n");\r
724 }\r
725\r
726 if (VolumeHeader.Attributes & EFI_FVB2_WRITE_ENABLED_CAP) {\r
727 printf (" EFI_FVB2_WRITE_ENABLED_CAP\n");\r
728 }\r
729\r
730 if (VolumeHeader.Attributes & EFI_FVB2_WRITE_STATUS) {\r
731 printf (" EFI_FVB2_WRITE_STATUS\n");\r
732 }\r
733\r
734 if (VolumeHeader.Attributes & EFI_FVB2_LOCK_CAP) {\r
735 printf (" EFI_FVB2_LOCK_CAP\n");\r
736 }\r
737\r
738 if (VolumeHeader.Attributes & EFI_FVB2_LOCK_STATUS) {\r
739 printf (" EFI_FVB2_LOCK_STATUS\n");\r
740 }\r
741\r
742 if (VolumeHeader.Attributes & EFI_FVB2_STICKY_WRITE) {\r
743 printf (" EFI_FVB2_STICKY_WRITE\n");\r
744 }\r
745\r
746 if (VolumeHeader.Attributes & EFI_FVB2_MEMORY_MAPPED) {\r
747 printf (" EFI_FVB2_MEMORY_MAPPED\n");\r
748 }\r
749\r
750 if (VolumeHeader.Attributes & EFI_FVB2_ERASE_POLARITY) {\r
751 printf (" EFI_FVB2_ERASE_POLARITY\n");\r
752 *ErasePolarity = TRUE;\r
753 }\r
754\r
755#if (PI_SPECIFICATION_VERSION < 0x00010000)\r
756 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT) {\r
757 printf (" EFI_FVB2_ALIGNMENT\n");\r
758 }\r
759\r
760 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2) {\r
761 printf (" EFI_FVB2_ALIGNMENT_2\n");\r
762 }\r
763\r
764 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_4) {\r
765 printf (" EFI_FVB2_ALIGNMENT_4\n");\r
766 }\r
767\r
768 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_8) {\r
769 printf (" EFI_FVB2_ALIGNMENT_8\n");\r
770 }\r
771\r
772 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_16) {\r
773 printf (" EFI_FVB2_ALIGNMENT_16\n");\r
774 }\r
775\r
776 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_32) {\r
777 printf (" EFI_FVB2_ALIGNMENT_32\n");\r
778 }\r
779\r
780 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64) {\r
781 printf (" EFI_FVB2_ALIGNMENT_64\n");\r
782 }\r
783\r
784 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_128) {\r
785 printf (" EFI_FVB2_ALIGNMENT_128\n");\r
786 }\r
787\r
788 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_256) {\r
789 printf (" EFI_FVB2_ALIGNMENT_256\n");\r
790 }\r
791\r
792 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_512) {\r
793 printf (" EFI_FVB2_ALIGNMENT_512\n");\r
794 }\r
795\r
796 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_1K) {\r
797 printf (" EFI_FVB2_ALIGNMENT_1K\n");\r
798 }\r
799\r
800 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2K) {\r
801 printf (" EFI_FVB2_ALIGNMENT_2K\n");\r
802 }\r
803\r
804 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_4K) {\r
805 printf (" EFI_FVB2_ALIGNMENT_4K\n");\r
806 }\r
807\r
808 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_8K) {\r
809 printf (" EFI_FVB2_ALIGNMENT_8K\n");\r
810 }\r
811\r
812 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_16K) {\r
813 printf (" EFI_FVB2_ALIGNMENT_16K\n");\r
814 }\r
815\r
816 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_32K) {\r
817 printf (" EFI_FVB2_ALIGNMENT_32K\n");\r
818 }\r
819\r
820 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64K) {\r
821 printf (" EFI_FVB2_ALIGNMENT_64K\n");\r
822 }\r
823 \r
824#else\r
825\r
826 if (VolumeHeader.Attributes & EFI_FVB2_READ_LOCK_CAP) {\r
827 printf (" EFI_FVB2_READ_LOCK_CAP\n");\r
828 }\r
829\r
830 if (VolumeHeader.Attributes & EFI_FVB2_READ_LOCK_STATUS) {\r
831 printf (" EFI_FVB2_READ_LOCK_STATUS\n");\r
832 }\r
833\r
834 if (VolumeHeader.Attributes & EFI_FVB2_WRITE_LOCK_CAP) {\r
835 printf (" EFI_FVB2_WRITE_LOCK_CAP\n");\r
836 }\r
837\r
838 if (VolumeHeader.Attributes & EFI_FVB2_WRITE_LOCK_STATUS) {\r
839 printf (" EFI_FVB2_WRITE_LOCK_STATUS\n");\r
840 }\r
841\r
842 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_1) {\r
843 printf (" EFI_FVB2_ALIGNMENT_1\n");\r
844 }\r
845\r
846 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2) {\r
847 printf (" EFI_FVB2_ALIGNMENT_2\n");\r
848 }\r
849\r
850 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_4) {\r
851 printf (" EFI_FVB2_ALIGNMENT_4\n");\r
852 }\r
853\r
854 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_8) {\r
855 printf (" EFI_FVB2_ALIGNMENT_8\n");\r
856 }\r
857\r
858 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_16) {\r
859 printf (" EFI_FVB2_ALIGNMENT_16\n");\r
860 }\r
861\r
862 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_32) {\r
863 printf (" EFI_FVB2_ALIGNMENT_32\n");\r
864 }\r
865\r
866 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64) {\r
867 printf (" EFI_FVB2_ALIGNMENT_64\n");\r
868 }\r
869\r
870 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_128) {\r
871 printf (" EFI_FVB2_ALIGNMENT_128\n");\r
872 }\r
873\r
874 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_256) {\r
875 printf (" EFI_FVB2_ALIGNMENT_256\n");\r
876 }\r
877\r
878 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_512) {\r
879 printf (" EFI_FVB2_ALIGNMENT_512\n");\r
880 }\r
881\r
882 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_1K) {\r
883 printf (" EFI_FVB2_ALIGNMENT_1K\n");\r
884 }\r
885\r
886 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2K) {\r
887 printf (" EFI_FVB2_ALIGNMENT_2K\n");\r
888 }\r
889\r
890 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_4K) {\r
891 printf (" EFI_FVB2_ALIGNMENT_4K\n");\r
892 }\r
893\r
894 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_8K) {\r
895 printf (" EFI_FVB2_ALIGNMENT_8K\n");\r
896 }\r
897\r
898 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_16K) {\r
899 printf (" EFI_FVB2_ALIGNMENT_16K\n");\r
900 }\r
901\r
902 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_32K) {\r
903 printf (" EFI_FVB2_ALIGNMENT_32K\n");\r
904 }\r
905\r
906 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64K) {\r
907 printf (" EFI_FVB2_ALIGNMENT_64K\n");\r
908 }\r
909\r
910 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_128K) {\r
911 printf (" EFI_FVB2_ALIGNMENT_128K\n");\r
912 }\r
913\r
914 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_256K) {\r
915 printf (" EFI_FVB2_ALIGNMENT_256K\n");\r
916 }\r
917\r
918 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_512K) {\r
919 printf (" EFI_FVB2_ALIGNMENT_512K\n");\r
920 }\r
921\r
922 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_1M) {\r
923 printf (" EFI_FVB2_ALIGNMENT_1M\n");\r
924 }\r
925\r
926 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2M) {\r
927 printf (" EFI_FVB2_ALIGNMENT_2M\n");\r
928 }\r
929\r
930 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_4M) {\r
931 printf (" EFI_FVB2_ALIGNMENT_4M\n");\r
932 }\r
933\r
934 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_8M) {\r
935 printf (" EFI_FVB2_ALIGNMENT_8M\n");\r
936 }\r
937\r
938 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_16M) {\r
939 printf (" EFI_FVB2_ALIGNMENT_16M\n");\r
940 }\r
941\r
942 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_32M) {\r
943 printf (" EFI_FVB2_ALIGNMENT_32M\n");\r
944 }\r
945\r
946 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64M) {\r
947 printf (" EFI_FVB2_ALIGNMENT_64M\n");\r
948 }\r
949\r
950 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_128M) {\r
951 printf (" EFI_FVB2_ALIGNMENT_128M\n");\r
952 }\r
953\r
954 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_64M) {\r
955 printf (" EFI_FVB2_ALIGNMENT_64M\n");\r
956 }\r
957\r
958 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_128M) {\r
959 printf (" EFI_FVB2_ALIGNMENT_128M\n");\r
960 }\r
961\r
962 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_256M) {\r
963 printf (" EFI_FVB2_ALIGNMENT_256M\n");\r
964 }\r
965\r
966 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_512M) {\r
967 printf (" EFI_FVB2_ALIGNMENT_512M\n");\r
968 }\r
969\r
970 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_1G) {\r
971 printf (" EFI_FVB2_ALIGNMENT_1G\n");\r
972 }\r
973\r
974 if (VolumeHeader.Attributes & EFI_FVB2_ALIGNMENT_2G) {\r
975 printf (" EFI_FVB2_ALIGNMENT_2G\n");\r
976 }\r
977\r
978#endif\r
979 printf ("Header Length: 0x%08X\n", VolumeHeader.HeaderLength);\r
980 printf ("File System ID: ");\r
981 PrintGuid (&VolumeHeader.FileSystemGuid);\r
982 //\r
983 // printf ("\n");\r
984 //\r
985 printf ("Revision: 0x%04X\n", VolumeHeader.Revision);\r
986\r
987 do {\r
988 fread (&BlockMap, sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, InputFile);\r
989 BytesRead += sizeof (EFI_FV_BLOCK_MAP_ENTRY);\r
990\r
991 if (BlockMap.NumBlocks != 0) {\r
992 printf ("Number of Blocks: 0x%08X\n", (unsigned) BlockMap.NumBlocks);\r
993 printf ("Block Length: 0x%08X\n", (unsigned) BlockMap.Length);\r
994 Size += BlockMap.NumBlocks * BlockMap.Length;\r
995 }\r
996\r
997 } while (!(BlockMap.NumBlocks == 0 && BlockMap.Length == 0));\r
998\r
999 if (BytesRead != VolumeHeader.HeaderLength) {\r
1000 printf ("ERROR: Header length not consistent with Block Maps!\n");\r
1001 return EFI_ABORTED;\r
1002 }\r
1003\r
1004 if (VolumeHeader.FvLength != Size) {\r
1005 printf ("ERROR: Volume Size not consistant with Block Maps!\n");\r
1006 return EFI_ABORTED;\r
1007 }\r
1008\r
1009 printf ("Total Volume Size: 0x%08X\n", (unsigned) Size);\r
1010\r
1011 *FvSize = Size;\r
1012\r
1013 //\r
1014 // rewind (InputFile);\r
1015 //\r
1016 return EFI_SUCCESS;\r
1017}\r
1018\r
1019STATIC\r
1020EFI_STATUS\r
1021PrintFileInfo (\r
1022 EFI_FIRMWARE_VOLUME_HEADER *FvImage,\r
1023 EFI_FFS_FILE_HEADER *FileHeader,\r
1024 BOOLEAN ErasePolarity\r
1025 )\r
1026/*++\r
1027\r
1028Routine Description:\r
1029\r
1030 GC_TODO: Add function description\r
1031\r
1032Arguments:\r
1033\r
1034 FvImage - GC_TODO: add argument description\r
1035 FileHeader - GC_TODO: add argument description\r
1036 ErasePolarity - GC_TODO: add argument description\r
1037\r
1038Returns:\r
1039\r
1040 EFI_SUCCESS - GC_TODO: Add description for return value\r
1041 EFI_ABORTED - GC_TODO: Add description for return value\r
1042\r
1043--*/\r
1044{\r
1045 UINT32 FileLength;\r
1046 UINT8 FileState;\r
1047 UINT8 Checksum;\r
e4ac870f 1048 EFI_FFS_FILE_HEADER2 BlankHeader;\r
f51461c8
LG
1049 EFI_STATUS Status;\r
1050 UINT8 GuidBuffer[PRINTED_GUID_BUFFER_SIZE];\r
e4ac870f 1051 UINT32 HeaderSize;\r
f51461c8
LG
1052#if (PI_SPECIFICATION_VERSION < 0x00010000) \r
1053 UINT16 *Tail;\r
1054#endif\r
1055 //\r
1056 // Check if we have free space\r
1057 //\r
e4ac870f 1058 HeaderSize = FvBufGetFfsHeaderSize(FileHeader);\r
f51461c8 1059 if (ErasePolarity) {\r
e4ac870f 1060 memset (&BlankHeader, -1, HeaderSize);\r
f51461c8 1061 } else {\r
e4ac870f 1062 memset (&BlankHeader, 0, HeaderSize);\r
f51461c8
LG
1063 }\r
1064\r
e4ac870f 1065 if (memcmp (&BlankHeader, FileHeader, HeaderSize) == 0) {\r
f51461c8
LG
1066 return EFI_SUCCESS;\r
1067 }\r
1068 //\r
1069 // Print file information.\r
1070 //\r
1071 printf ("============================================================\n");\r
1072\r
1073 printf ("File Name: ");\r
1074 PrintGuidToBuffer (&FileHeader->Name, GuidBuffer, sizeof (GuidBuffer), TRUE);\r
1075 printf ("%s ", GuidBuffer);\r
1076 PrintGuidName (GuidBuffer);\r
1077 printf ("\n");\r
1078\r
1079 //\r
1080 // PrintGuid (&FileHeader->Name);\r
1081 // printf ("\n");\r
1082 //\r
e4ac870f 1083 FileLength = FvBufGetFfsFileSize (FileHeader);\r
f51461c8
LG
1084 printf ("File Offset: 0x%08X\n", (unsigned) ((UINTN) FileHeader - (UINTN) FvImage));\r
1085 printf ("File Length: 0x%08X\n", (unsigned) FileLength);\r
1086 printf ("File Attributes: 0x%02X\n", FileHeader->Attributes);\r
1087 printf ("File State: 0x%02X\n", FileHeader->State);\r
1088\r
1089 //\r
1090 // Print file state\r
1091 //\r
1092 FileState = GetFileState (ErasePolarity, FileHeader);\r
1093\r
1094 switch (FileState) {\r
1095\r
1096 case EFI_FILE_HEADER_CONSTRUCTION:\r
1097 printf (" EFI_FILE_HEADER_CONSTRUCTION\n");\r
1098 return EFI_SUCCESS;\r
1099\r
1100 case EFI_FILE_HEADER_INVALID:\r
1101 printf (" EFI_FILE_HEADER_INVALID\n");\r
1102 return EFI_SUCCESS;\r
1103\r
1104 case EFI_FILE_HEADER_VALID:\r
1105 printf (" EFI_FILE_HEADER_VALID\n");\r
e4ac870f 1106 Checksum = CalculateSum8 ((UINT8 *) FileHeader, HeaderSize);\r
f51461c8
LG
1107 Checksum = (UINT8) (Checksum - FileHeader->IntegrityCheck.Checksum.File);\r
1108 Checksum = (UINT8) (Checksum - FileHeader->State);\r
1109 if (Checksum != 0) {\r
1110 printf ("ERROR: Header checksum invalid.\n");\r
1111 return EFI_ABORTED;\r
1112 }\r
1113\r
1114 return EFI_SUCCESS;\r
1115\r
1116 case EFI_FILE_DELETED:\r
1117 printf (" EFI_FILE_DELETED\n");\r
1118\r
1119 case EFI_FILE_MARKED_FOR_UPDATE:\r
1120 printf (" EFI_FILE_MARKED_FOR_UPDATE\n");\r
1121\r
1122 case EFI_FILE_DATA_VALID:\r
1123 printf (" EFI_FILE_DATA_VALID\n");\r
1124\r
1125 //\r
1126 // Calculate header checksum\r
1127 //\r
e4ac870f 1128 Checksum = CalculateSum8 ((UINT8 *) FileHeader, HeaderSize);\r
f51461c8
LG
1129 Checksum = (UINT8) (Checksum - FileHeader->IntegrityCheck.Checksum.File);\r
1130 Checksum = (UINT8) (Checksum - FileHeader->State);\r
1131 if (Checksum != 0) {\r
1132 Error (NULL, 0, 0003, "error parsing FFS file", "FFS file with Guid %s has invalid header checksum", GuidBuffer);\r
1133 return EFI_ABORTED;\r
1134 }\r
1135\r
e4ac870f 1136 FileLength = FvBufGetFfsFileSize (FileHeader);\r
f51461c8
LG
1137\r
1138 if (FileHeader->Attributes & FFS_ATTRIB_CHECKSUM) {\r
1139 //\r
1140 // Calculate file checksum\r
1141 //\r
e4ac870f 1142 Checksum = CalculateSum8 ((UINT8 *)FileHeader + HeaderSize, FileLength - HeaderSize);\r
f51461c8
LG
1143 Checksum = Checksum + FileHeader->IntegrityCheck.Checksum.File;\r
1144 if (Checksum != 0) {\r
1145 Error (NULL, 0, 0003, "error parsing FFS file", "FFS file with Guid %s has invalid file checksum", GuidBuffer);\r
1146 return EFI_ABORTED;\r
1147 }\r
1148 } else {\r
1149 if (FileHeader->IntegrityCheck.Checksum.File != FFS_FIXED_CHECKSUM) {\r
1150 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
1151 return EFI_ABORTED;\r
1152 }\r
1153 }\r
1154#if (PI_SPECIFICATION_VERSION < 0x00010000) \r
1155 //\r
1156 // Verify tail if present\r
1157 //\r
1158 if (FileHeader->Attributes & FFS_ATTRIB_TAIL_PRESENT) {\r
1159 //\r
1160 // Verify tail is complement of integrity check field in the header.\r
1161 //\r
1162 Tail = (UINT16 *) ((UINTN) FileHeader + GetLength (FileHeader->Size) - sizeof (EFI_FFS_INTEGRITY_CHECK));\r
1163 if (FileHeader->IntegrityCheck.TailReference != (UINT16)~(*Tail)) {\r
1164 Error (NULL, 0, 0003, "error parsing FFS file", \\r
1165 "FFS file with Guid %s failed in the integrity check, tail is not the complement of the header field", GuidBuffer);\r
1166 return EFI_ABORTED;\r
1167 }\r
1168 }\r
1169 #endif \r
1170 break;\r
1171\r
1172 default:\r
1173 Error (NULL, 0, 0003, "error parsing FFS file", "FFS file with Guid %s has the invalid/unrecognized file state bits", GuidBuffer);\r
1174 return EFI_ABORTED;\r
1175 }\r
1176\r
1177 printf ("File Type: 0x%02X ", FileHeader->Type);\r
1178\r
1179 switch (FileHeader->Type) {\r
1180\r
1181 case EFI_FV_FILETYPE_RAW:\r
1182 printf ("EFI_FV_FILETYPE_RAW\n");\r
1183 break;\r
1184\r
1185 case EFI_FV_FILETYPE_FREEFORM:\r
1186 printf ("EFI_FV_FILETYPE_FREEFORM\n");\r
1187 break;\r
1188\r
1189 case EFI_FV_FILETYPE_SECURITY_CORE:\r
1190 printf ("EFI_FV_FILETYPE_SECURITY_CORE\n");\r
1191 break;\r
1192\r
1193 case EFI_FV_FILETYPE_PEI_CORE:\r
1194 printf ("EFI_FV_FILETYPE_PEI_CORE\n");\r
1195 break;\r
1196\r
1197 case EFI_FV_FILETYPE_DXE_CORE:\r
1198 printf ("EFI_FV_FILETYPE_DXE_CORE\n");\r
1199 break;\r
1200\r
1201 case EFI_FV_FILETYPE_PEIM:\r
1202 printf ("EFI_FV_FILETYPE_PEIM\n");\r
1203 break;\r
1204\r
1205 case EFI_FV_FILETYPE_DRIVER:\r
1206 printf ("EFI_FV_FILETYPE_DRIVER\n");\r
1207 break;\r
1208\r
1209 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:\r
1210 printf ("EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER\n");\r
1211 break;\r
1212\r
1213 case EFI_FV_FILETYPE_APPLICATION:\r
1214 printf ("EFI_FV_FILETYPE_APPLICATION\n");\r
1215 break;\r
1216\r
1217 case EFI_FV_FILETYPE_SMM:\r
1218 printf ("EFI_FV_FILETYPE_SMM\n");\r
1219 break;\r
1220\r
1221 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE:\r
1222 printf ("EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE\n");\r
1223 break;\r
1224\r
1225 case EFI_FV_FILETYPE_COMBINED_SMM_DXE:\r
1226 printf ("EFI_FV_FILETYPE_COMBINED_SMM_DXE\n");\r
1227 break;\r
1228\r
1229 case EFI_FV_FILETYPE_SMM_CORE:\r
1230 printf ("EFI_FV_FILETYPE_SMM_CORE\n");\r
1231 break;\r
1232\r
1233 case EFI_FV_FILETYPE_FFS_PAD:\r
1234 printf ("EFI_FV_FILETYPE_FFS_PAD\n");\r
1235 break;\r
1236\r
1237 default:\r
1238 printf ("\nERROR: Unrecognized file type %X.\n", FileHeader->Type);\r
1239 return EFI_ABORTED;\r
1240 break;\r
1241 }\r
1242\r
1243 switch (FileHeader->Type) {\r
1244\r
1245 case EFI_FV_FILETYPE_ALL:\r
1246 case EFI_FV_FILETYPE_RAW:\r
1247 case EFI_FV_FILETYPE_FFS_PAD:\r
1248 break;\r
1249\r
1250 default:\r
1251 //\r
1252 // All other files have sections\r
1253 //\r
1254 Status = ParseSection (\r
e4ac870f
LG
1255 (UINT8 *) ((UINTN) FileHeader + HeaderSize),\r
1256 FvBufGetFfsFileSize (FileHeader) - HeaderSize\r
f51461c8
LG
1257 );\r
1258 if (EFI_ERROR (Status)) {\r
1259 //\r
1260 // printf ("ERROR: Parsing the FFS file.\n");\r
1261 //\r
1262 return EFI_ABORTED;\r
1263 }\r
1264 break;\r
1265 }\r
1266\r
1267 return EFI_SUCCESS;\r
1268}\r
1269\r
9947f576
YZ
1270EFI_STATUS\r
1271RebaseImageRead (\r
1272 IN VOID *FileHandle,\r
1273 IN UINTN FileOffset,\r
1274 IN OUT UINT32 *ReadSize,\r
1275 OUT VOID *Buffer\r
1276 )\r
1277/*++\r
1278\r
1279Routine Description:\r
1280\r
1281 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
1282\r
1283Arguments:\r
1284\r
1285 FileHandle - The handle to the PE/COFF file\r
1286\r
1287 FileOffset - The offset, in bytes, into the file to read\r
1288\r
1289 ReadSize - The number of bytes to read from the file starting at FileOffset\r
1290\r
1291 Buffer - A pointer to the buffer to read the data into.\r
1292\r
1293Returns:\r
1294\r
1295 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
1296\r
1297--*/\r
1298{\r
1299 CHAR8 *Destination8;\r
1300 CHAR8 *Source8;\r
1301 UINT32 Length;\r
1302\r
1303 Destination8 = Buffer;\r
1304 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
1305 Length = *ReadSize;\r
1306 while (Length--) {\r
1307 *(Destination8++) = *(Source8++);\r
1308 }\r
1309\r
1310 return EFI_SUCCESS;\r
1311}\r
1312\r
1313EFI_STATUS\r
1314SetAddressToSectionHeader (\r
1315 IN CHAR8 *FileName,\r
1316 IN OUT UINT8 *FileBuffer,\r
1317 IN UINT64 NewPe32BaseAddress\r
1318 )\r
1319/*++\r
1320\r
1321Routine Description:\r
1322\r
1323 Set new base address into the section header of PeImage\r
1324\r
1325Arguments:\r
1326\r
1327 FileName - Name of file\r
1328 FileBuffer - Pointer to PeImage.\r
1329 NewPe32BaseAddress - New Base Address for PE image.\r
1330\r
1331Returns:\r
1332\r
1333 EFI_SUCCESS Set new base address into this image successfully.\r
1334\r
1335--*/\r
1336{\r
1337 EFI_STATUS Status;\r
1338 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
1339 UINTN Index;\r
1340 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
1341 EFI_IMAGE_SECTION_HEADER *SectionHeader;\r
1342\r
1343 //\r
1344 // Initialize context\r
1345 //\r
1346 memset (&ImageContext, 0, sizeof (ImageContext));\r
1347 ImageContext.Handle = (VOID *) FileBuffer;\r
1348 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) RebaseImageRead;\r
1349 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
1350 if (EFI_ERROR (Status)) {\r
1351 Error (NULL, 0, 3000, "Invalid", "The input PeImage %s is not valid", FileName);\r
1352 return Status;\r
1353 }\r
1354\r
1355 if (ImageContext.RelocationsStripped) {\r
1356 Error (NULL, 0, 3000, "Invalid", "The input PeImage %s has no relocation to be fixed up", FileName);\r
1357 return Status;\r
1358 }\r
1359\r
1360 //\r
1361 // Get PeHeader pointer\r
1362 //\r
1363 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(FileBuffer + ImageContext.PeCoffHeaderOffset);\r
1364\r
1365 //\r
1366 // Get section header list\r
1367 //\r
1368 SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (\r
1369 (UINTN) ImgHdr +\r
1370 sizeof (UINT32) +\r
1371 sizeof (EFI_IMAGE_FILE_HEADER) +\r
1372 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
1373 );\r
1374\r
1375 //\r
1376 // Set base address into the first section header that doesn't point to code section.\r
1377 //\r
1378 for (Index = 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; Index ++, SectionHeader ++) {\r
1379 if ((SectionHeader->Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
1380 *(UINT64 *) &SectionHeader->PointerToRelocations = NewPe32BaseAddress;\r
1381 break;\r
1382 }\r
1383 }\r
1384\r
9947f576
YZ
1385 //\r
1386 // BaseAddress is set to section header.\r
1387 //\r
1388 return EFI_SUCCESS;\r
1389}\r
1390\r
1391EFI_STATUS\r
1392RebaseImage (\r
1393 IN CHAR8 *FileName,\r
1394 IN OUT UINT8 *FileBuffer,\r
1395 IN UINT64 NewPe32BaseAddress\r
1396 )\r
1397/*++\r
1398\r
1399Routine Description:\r
1400\r
1401 Set new base address into PeImage, and fix up PeImage based on new address.\r
1402\r
1403Arguments:\r
1404\r
1405 FileName - Name of file\r
1406 FileBuffer - Pointer to PeImage.\r
1407 NewPe32BaseAddress - New Base Address for PE image.\r
1408\r
1409Returns:\r
1410\r
1411 EFI_INVALID_PARAMETER - BaseAddress is not valid.\r
1412 EFI_SUCCESS - Update PeImage is correctly.\r
1413\r
1414--*/\r
1415{\r
1416 EFI_STATUS Status;\r
1417 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
1418 UINTN Index;\r
1419 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
1420 UINT8 *MemoryImagePointer;\r
1421 EFI_IMAGE_SECTION_HEADER *SectionHeader;\r
1422\r
1423 //\r
1424 // Initialize context\r
1425 //\r
1426 memset (&ImageContext, 0, sizeof (ImageContext));\r
1427 ImageContext.Handle = (VOID *) FileBuffer;\r
1428 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) RebaseImageRead;\r
1429 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
1430 if (EFI_ERROR (Status)) {\r
1431 Error (NULL, 0, 3000, "Invalid", "The input PeImage %s is not valid", FileName);\r
1432 return Status;\r
1433 }\r
1434\r
1435 if (ImageContext.RelocationsStripped) {\r
1436 Error (NULL, 0, 3000, "Invalid", "The input PeImage %s has no relocation to be fixed up", FileName);\r
1437 return Status;\r
1438 }\r
1439\r
1440 //\r
1441 // Get PeHeader pointer\r
1442 //\r
1443 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(FileBuffer + ImageContext.PeCoffHeaderOffset);\r
1444\r
1445 //\r
1446 // Load and Relocate Image Data\r
1447 //\r
1448 MemoryImagePointer = (UINT8 *) malloc ((UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);\r
1449 if (MemoryImagePointer == NULL) {\r
1450 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);\r
1451 return EFI_OUT_OF_RESOURCES;\r
1452 }\r
1453 memset ((VOID *) MemoryImagePointer, 0, (UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);\r
1454 ImageContext.ImageAddress = ((UINTN) MemoryImagePointer + ImageContext.SectionAlignment - 1) & (~((INT64)ImageContext.SectionAlignment - 1));\r
1455\r
1456 Status = PeCoffLoaderLoadImage (&ImageContext);\r
1457 if (EFI_ERROR (Status)) {\r
1458 Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName);\r
1459 free ((VOID *) MemoryImagePointer);\r
1460 return Status;\r
1461 }\r
1462\r
1463 ImageContext.DestinationAddress = NewPe32BaseAddress;\r
1464 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
1465 if (EFI_ERROR (Status)) {\r
1466 Error (NULL, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName);\r
1467 free ((VOID *) MemoryImagePointer);\r
1468 return Status;\r
1469 }\r
1470\r
1471 //\r
1472 // Copy Relocated data to raw image file.\r
1473 //\r
1474 SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (\r
1475 (UINTN) ImgHdr +\r
1476 sizeof (UINT32) +\r
1477 sizeof (EFI_IMAGE_FILE_HEADER) +\r
1478 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
1479 );\r
1480\r
1481 for (Index = 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; Index ++, SectionHeader ++) {\r
1482 CopyMem (\r
1483 FileBuffer + SectionHeader->PointerToRawData,\r
1484 (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader->VirtualAddress),\r
1485 SectionHeader->SizeOfRawData\r
1486 );\r
1487 }\r
1488\r
1489 free ((VOID *) MemoryImagePointer);\r
1490\r
1491 //\r
1492 // Update Image Base Address\r
1493 //\r
1494 if ((ImgHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) && (ImgHdr->Pe32.FileHeader.Machine != IMAGE_FILE_MACHINE_IA64)) {\r
1495 ImgHdr->Pe32.OptionalHeader.ImageBase = (UINT32) NewPe32BaseAddress;\r
1496 } else if (ImgHdr->Pe32Plus.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
1497 ImgHdr->Pe32Plus.OptionalHeader.ImageBase = NewPe32BaseAddress;\r
1498 } else {\r
1499 Error (NULL, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",\r
1500 ImgHdr->Pe32.OptionalHeader.Magic,\r
1501 FileName\r
1502 );\r
1503 return EFI_ABORTED;\r
1504 }\r
1505\r
1506 //\r
1507 // Set new base address into section header\r
1508 //\r
1509 Status = SetAddressToSectionHeader (FileName, FileBuffer, NewPe32BaseAddress);\r
1510\r
1511 return Status;\r
1512}\r
1513\r
1514EFI_STATUS\r
1515CombinePath (\r
1516 IN CHAR8* DefaultPath,\r
1517 IN CHAR8* AppendPath,\r
1518 OUT CHAR8* NewPath\r
1519)\r
1520{\r
1521 UINT32 DefaultPathLen;\r
1522 DefaultPathLen = strlen(DefaultPath);\r
1523 strcpy(NewPath, DefaultPath);\r
1524 UINT64 Index = 0;\r
1525 for (; Index < DefaultPathLen; Index ++) {\r
1526 if (NewPath[Index] == '\\' || NewPath[Index] == '/') {\r
1527 if (NewPath[Index + 1] != '\0') {\r
1528 NewPath[Index] = '/';\r
1529 }\r
1530 }\r
1531 }\r
1532 if (NewPath[Index -1] != '/') {\r
1533 NewPath[Index] = '/';\r
1534 NewPath[Index + 1] = '\0';\r
1535 }\r
1536 strcat(NewPath, AppendPath);\r
1537 return EFI_SUCCESS;\r
1538}\r
1539\r
f51461c8
LG
1540EFI_STATUS\r
1541ParseSection (\r
1542 IN UINT8 *SectionBuffer,\r
1543 IN UINT32 BufferLength\r
1544 )\r
1545/*++\r
1546\r
1547Routine Description:\r
1548\r
1549 Parses EFI Sections\r
1550\r
1551Arguments:\r
1552\r
1553 SectionBuffer - Buffer containing the section to parse.\r
1554 BufferLength - Length of SectionBuffer\r
1555\r
1556Returns:\r
1557\r
1558 EFI_SECTION_ERROR - Problem with section parsing.\r
1559 (a) compression errors\r
1560 (b) unrecognized section \r
1561 EFI_UNSUPPORTED - Do not know how to parse the section.\r
1562 EFI_SUCCESS - Section successfully parsed.\r
1563 EFI_OUT_OF_RESOURCES - Memory allocation failed.\r
1564\r
1565--*/\r
1566{\r
1567 EFI_SECTION_TYPE Type;\r
1568 UINT8 *Ptr;\r
1569 UINT32 SectionLength;\r
e4ac870f 1570 UINT32 SectionHeaderLen;\r
f51461c8
LG
1571 CHAR8 *SectionName;\r
1572 EFI_STATUS Status;\r
1573 UINT32 ParsedLength;\r
1574 UINT8 *CompressedBuffer;\r
1575 UINT32 CompressedLength;\r
1576 UINT8 *UncompressedBuffer;\r
1577 UINT32 UncompressedLength;\r
1578 UINT8 *ToolOutputBuffer;\r
1579 UINT32 ToolOutputLength;\r
1580 UINT8 CompressionType;\r
1581 UINT32 DstSize;\r
1582 UINT32 ScratchSize;\r
1583 UINT8 *ScratchBuffer;\r
1584 DECOMPRESS_FUNCTION DecompressFunction;\r
1585 GETINFO_FUNCTION GetInfoFunction;\r
1586 // CHAR16 *name;\r
1587 CHAR8 *ExtractionTool;\r
1588 CHAR8 *ToolInputFile;\r
1589 CHAR8 *ToolOutputFile;\r
1590 CHAR8 *SystemCommandFormatString;\r
1591 CHAR8 *SystemCommand;\r
e4ac870f
LG
1592 EFI_GUID *EfiGuid;\r
1593 UINT16 DataOffset;\r
1594 UINT16 Attributes;\r
1595 UINT32 RealHdrLen;\r
9947f576
YZ
1596 CHAR8 *ToolInputFileName;\r
1597 CHAR8 *ToolOutputFileName;\r
f51461c8
LG
1598\r
1599 ParsedLength = 0;\r
9947f576
YZ
1600 ToolInputFileName = NULL;\r
1601 ToolOutputFileName = NULL;\r
1602\r
f51461c8
LG
1603 while (ParsedLength < BufferLength) {\r
1604 Ptr = SectionBuffer + ParsedLength;\r
1605\r
1606 SectionLength = GetLength (((EFI_COMMON_SECTION_HEADER *) Ptr)->Size);\r
1607 Type = ((EFI_COMMON_SECTION_HEADER *) Ptr)->Type;\r
1608\r
1609 //\r
1610 // This is sort of an odd check, but is necessary because FFS files are\r
1611 // padded to a QWORD boundary, meaning there is potentially a whole section\r
1612 // header worth of 0xFF bytes.\r
1613 //\r
1614 if (SectionLength == 0xffffff && Type == 0xff) {\r
1615 ParsedLength += 4;\r
1616 continue;\r
1617 }\r
1618\r
e4ac870f
LG
1619 //\r
1620 // Get real section file size\r
1621 //\r
1622 SectionLength = GetSectionFileLength ((EFI_COMMON_SECTION_HEADER *) Ptr);\r
1623 SectionHeaderLen = GetSectionHeaderLength((EFI_COMMON_SECTION_HEADER *)Ptr);\r
1624\r
f51461c8
LG
1625 SectionName = SectionNameToStr (Type);\r
1626 printf ("------------------------------------------------------------\n");\r
1627 printf (" Type: %s\n Size: 0x%08X\n", SectionName, (unsigned) SectionLength);\r
1628 free (SectionName);\r
1629\r
1630 switch (Type) {\r
1631 case EFI_SECTION_RAW:\r
f51461c8
LG
1632 case EFI_SECTION_PIC:\r
1633 case EFI_SECTION_TE:\r
1634 // default is no more information\r
1635 break;\r
1636\r
9947f576
YZ
1637 case EFI_SECTION_PE32:\r
1638 if (EnableHash) {\r
1639 ToolInputFileName = "edk2Temp_InputEfi.tmp";\r
1640 ToolOutputFileName = "edk2Temp_OutputHash.tmp";\r
1641 RebaseImage(ToolInputFileName, (UINT8*)Ptr + SectionHeaderLen, 0);\r
1642 PutFileImage (\r
1643 ToolInputFileName,\r
1644 (CHAR8*)Ptr + SectionHeaderLen,\r
1645 SectionLength - SectionHeaderLen\r
1646 );\r
1647\r
1648 SystemCommandFormatString = "%s sha1 -out %s %s";\r
1649 SystemCommand = malloc (\r
1650 strlen (SystemCommandFormatString) +\r
1651 strlen (OpenSslPath) +\r
1652 strlen (ToolInputFileName) +\r
1653 strlen (ToolOutputFileName) +\r
1654 1\r
1655 );\r
1656 sprintf (\r
1657 SystemCommand,\r
1658 SystemCommandFormatString,\r
1659 OpenSslPath,\r
1660 ToolOutputFileName,\r
1661 ToolInputFileName\r
1662 );\r
1663\r
1664 if (system (SystemCommand) != EFI_SUCCESS) {\r
1665 Error (NULL, 0, 3000, "Open SSL command not available. Please verify PATH or set OPENSSL_PATH.", NULL);\r
1666 }\r
1667 else {\r
1668 FILE *fp;\r
1669 CHAR8 *StrLine;\r
1670 CHAR8 *NewStr;\r
1671 UINT32 nFileLen;\r
1672 if((fp = fopen(ToolOutputFileName,"r")) == NULL) {\r
1673 Error (NULL, 0, 0004, "Hash the PE32 image failed.", NULL);\r
1674 }\r
1675 else {\r
1676 fseek(fp,0,SEEK_SET);\r
1677 fseek(fp,0,SEEK_END);\r
1678 nFileLen = ftell(fp);\r
1679 fseek(fp,0,SEEK_SET);\r
1680 StrLine = malloc(nFileLen);\r
1681 fgets(StrLine, nFileLen, fp);\r
1682 NewStr = strrchr (StrLine, '=');\r
1683 printf (" SHA1: %s\n", NewStr + 1);\r
1684 free (StrLine);\r
1685 }\r
1686 fclose(fp);\r
1687 }\r
1688 remove(ToolInputFileName);\r
1689 remove(ToolOutputFileName);\r
1690 free (SystemCommand);\r
1691 }\r
1692 break;\r
1693\r
f51461c8 1694 case EFI_SECTION_USER_INTERFACE:\r
730ffca1 1695 printf (" String: %ls\n", (CHAR16 *) &((EFI_USER_INTERFACE_SECTION *) Ptr)->FileNameString);\r
f51461c8
LG
1696 break;\r
1697\r
1698 case EFI_SECTION_FIRMWARE_VOLUME_IMAGE:\r
e4ac870f 1699 Status = PrintFvInfo (Ptr + SectionHeaderLen, TRUE);\r
f51461c8
LG
1700 if (EFI_ERROR (Status)) {\r
1701 Error (NULL, 0, 0003, "printing of FV section contents failed", NULL);\r
1702 return EFI_SECTION_ERROR;\r
1703 }\r
1704 break;\r
1705\r
1706 case EFI_SECTION_COMPATIBILITY16:\r
1707 case EFI_SECTION_FREEFORM_SUBTYPE_GUID:\r
1708 //\r
1709 // Section does not contain any further header information.\r
1710 //\r
1711 break;\r
1712\r
1713 case EFI_SECTION_PEI_DEPEX:\r
1714 case EFI_SECTION_DXE_DEPEX:\r
1715 case EFI_SECTION_SMM_DEPEX:\r
1716 DumpDepexSection (Ptr, SectionLength);\r
1717 break;\r
1718\r
1719 case EFI_SECTION_VERSION:\r
e4ac870f
LG
1720 printf (" Build Number: 0x%02X\n", *(UINT16 *)(Ptr + SectionHeaderLen));\r
1721 printf (" Version Strg: %s\n", (char*) (Ptr + SectionHeaderLen + sizeof (UINT16)));\r
f51461c8
LG
1722 break;\r
1723\r
1724 case EFI_SECTION_COMPRESSION:\r
1725 UncompressedBuffer = NULL;\r
e4ac870f
LG
1726 if (SectionHeaderLen == sizeof (EFI_COMMON_SECTION_HEADER)) {\r
1727 RealHdrLen = sizeof(EFI_COMPRESSION_SECTION);\r
1728 UncompressedLength = ((EFI_COMPRESSION_SECTION *)Ptr)->UncompressedLength;\r
1729 CompressionType = ((EFI_COMPRESSION_SECTION *)Ptr)->CompressionType;\r
1730 } else {\r
1731 RealHdrLen = sizeof(EFI_COMPRESSION_SECTION2);\r
1732 UncompressedLength = ((EFI_COMPRESSION_SECTION2 *)Ptr)->UncompressedLength;\r
1733 CompressionType = ((EFI_COMPRESSION_SECTION2 *)Ptr)->CompressionType;\r
1734 }\r
1735 CompressedLength = SectionLength - RealHdrLen;\r
f51461c8
LG
1736 printf (" Uncompressed Length: 0x%08X\n", (unsigned) UncompressedLength);\r
1737\r
1738 if (CompressionType == EFI_NOT_COMPRESSED) {\r
1739 printf (" Compression Type: EFI_NOT_COMPRESSED\n");\r
1740 if (CompressedLength != UncompressedLength) {\r
1741 Error (\r
1742 NULL,\r
1743 0,\r
1744 0,\r
1745 "file is not compressed, but the compressed length does not match the uncompressed length",\r
1746 NULL\r
1747 );\r
1748 return EFI_SECTION_ERROR;\r
1749 }\r
1750\r
e4ac870f 1751 UncompressedBuffer = Ptr + RealHdrLen;\r
f51461c8
LG
1752 } else if (CompressionType == EFI_STANDARD_COMPRESSION) {\r
1753 GetInfoFunction = EfiGetInfo;\r
1754 DecompressFunction = EfiDecompress;\r
1755 printf (" Compression Type: EFI_STANDARD_COMPRESSION\n");\r
1756\r
e4ac870f 1757 CompressedBuffer = Ptr + RealHdrLen;\r
f51461c8
LG
1758\r
1759 Status = GetInfoFunction (CompressedBuffer, CompressedLength, &DstSize, &ScratchSize);\r
1760 if (EFI_ERROR (Status)) {\r
1761 Error (NULL, 0, 0003, "error getting compression info from compression section", NULL);\r
1762 return EFI_SECTION_ERROR;\r
1763 }\r
1764\r
1765 if (DstSize != UncompressedLength) {\r
1766 Error (NULL, 0, 0003, "compression error in the compression section", NULL);\r
1767 return EFI_SECTION_ERROR;\r
1768 }\r
1769\r
1770 ScratchBuffer = malloc (ScratchSize);\r
1771 UncompressedBuffer = malloc (UncompressedLength);\r
1772 if ((ScratchBuffer == NULL) || (UncompressedBuffer == NULL)) {\r
1773 return EFI_OUT_OF_RESOURCES;\r
1774 }\r
1775 Status = DecompressFunction (\r
1776 CompressedBuffer,\r
1777 CompressedLength,\r
1778 UncompressedBuffer,\r
1779 UncompressedLength,\r
1780 ScratchBuffer,\r
1781 ScratchSize\r
1782 );\r
1783 free (ScratchBuffer);\r
1784 if (EFI_ERROR (Status)) {\r
1785 Error (NULL, 0, 0003, "decompress failed", NULL);\r
1786 free (UncompressedBuffer);\r
1787 return EFI_SECTION_ERROR;\r
1788 }\r
1789 } else {\r
1790 Error (NULL, 0, 0003, "unrecognized compression type", "type 0x%X", CompressionType);\r
1791 return EFI_SECTION_ERROR;\r
1792 }\r
1793\r
1794 Status = ParseSection (UncompressedBuffer, UncompressedLength);\r
1795\r
1796 if (CompressionType == EFI_STANDARD_COMPRESSION) {\r
1797 //\r
1798 // We need to deallocate Buffer\r
1799 //\r
1800 free (UncompressedBuffer);\r
1801 }\r
1802\r
1803 if (EFI_ERROR (Status)) {\r
1804 Error (NULL, 0, 0003, "failed to parse section", NULL);\r
1805 return EFI_SECTION_ERROR;\r
1806 }\r
1807 break;\r
1808\r
1809 case EFI_SECTION_GUID_DEFINED:\r
e4ac870f
LG
1810 if (SectionHeaderLen == sizeof(EFI_COMMON_SECTION_HEADER)) {\r
1811 EfiGuid = &((EFI_GUID_DEFINED_SECTION *) Ptr)->SectionDefinitionGuid;\r
1812 DataOffset = ((EFI_GUID_DEFINED_SECTION *) Ptr)->DataOffset;\r
1813 Attributes = ((EFI_GUID_DEFINED_SECTION *) Ptr)->Attributes;\r
1814 } else {\r
1815 EfiGuid = &((EFI_GUID_DEFINED_SECTION2 *) Ptr)->SectionDefinitionGuid;\r
1816 DataOffset = ((EFI_GUID_DEFINED_SECTION2 *) Ptr)->DataOffset;\r
1817 Attributes = ((EFI_GUID_DEFINED_SECTION2 *) Ptr)->Attributes;\r
1818 }\r
f51461c8 1819 printf (" SectionDefinitionGuid: ");\r
e4ac870f 1820 PrintGuid (EfiGuid);\r
f51461c8 1821 printf ("\n");\r
e4ac870f
LG
1822 printf (" DataOffset: 0x%04X\n", (unsigned) DataOffset);\r
1823 printf (" Attributes: 0x%04X\n", (unsigned) Attributes);\r
f51461c8
LG
1824\r
1825 ExtractionTool =\r
1826 LookupGuidedSectionToolPath (\r
1827 mParsedGuidedSectionTools,\r
e4ac870f 1828 EfiGuid\r
f51461c8
LG
1829 );\r
1830\r
1831 if (ExtractionTool != NULL) {\r
868c9c35 1832 #ifndef __GNUC__\r
f51461c8
LG
1833 ToolInputFile = CloneString (tmpnam (NULL));\r
1834 ToolOutputFile = CloneString (tmpnam (NULL));\r
868c9c35
YZ
1835 #else\r
1836 char tmp1[] = "/tmp/fileXXXXXX";\r
1837 char tmp2[] = "/tmp/fileXXXXXX";\r
1838 int fd1;\r
1839 int fd2;\r
1840 fd1 = mkstemp(tmp1);\r
1841 fd2 = mkstemp(tmp2);\r
1842 ToolInputFile = CloneString(tmp1);\r
1843 ToolOutputFile = CloneString(tmp2);\r
1844 close(fd1);\r
1845 close(fd2);\r
1846 #endif\r
f51461c8
LG
1847\r
1848 //\r
1849 // Construction 'system' command string\r
1850 //\r
1851 SystemCommandFormatString = "%s -d -o %s %s";\r
1852 SystemCommand = malloc (\r
1853 strlen (SystemCommandFormatString) +\r
1854 strlen (ExtractionTool) +\r
1855 strlen (ToolInputFile) +\r
1856 strlen (ToolOutputFile) +\r
1857 1\r
1858 );\r
1859 sprintf (\r
1860 SystemCommand,\r
1861 SystemCommandFormatString,\r
1862 ExtractionTool,\r
1863 ToolOutputFile,\r
1864 ToolInputFile\r
1865 );\r
1866 free (ExtractionTool);\r
1867\r
1868 Status =\r
1869 PutFileImage (\r
1870 ToolInputFile,\r
e4ac870f
LG
1871 (CHAR8*) SectionBuffer + DataOffset,\r
1872 BufferLength - DataOffset\r
f51461c8
LG
1873 );\r
1874\r
1875 system (SystemCommand);\r
1876 remove (ToolInputFile);\r
1877 free (ToolInputFile);\r
1878\r
1879 Status =\r
1880 GetFileImage (\r
1881 ToolOutputFile,\r
1882 (CHAR8 **)&ToolOutputBuffer,\r
1883 &ToolOutputLength\r
1884 );\r
1885 remove (ToolOutputFile);\r
1886 free (ToolOutputFile);\r
1887 if (EFI_ERROR (Status)) {\r
1888 Error (NULL, 0, 0004, "unable to read decoded GUIDED section", NULL);\r
1889 return EFI_SECTION_ERROR;\r
1890 }\r
1891\r
1892 Status = ParseSection (\r
1893 ToolOutputBuffer,\r
1894 ToolOutputLength\r
1895 );\r
1896 if (EFI_ERROR (Status)) {\r
1897 Error (NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL);\r
1898 return EFI_SECTION_ERROR;\r
1899 }\r
1900\r
1901 //\r
1902 // Check for CRC32 sections which we can handle internally if needed.\r
1903 //\r
1904 } else if (!CompareGuid (\r
e4ac870f 1905 EfiGuid,\r
f51461c8
LG
1906 &gEfiCrc32GuidedSectionExtractionProtocolGuid\r
1907 )\r
1908 ) {\r
1909 //\r
1910 // CRC32 guided section\r
1911 //\r
1912 Status = ParseSection (\r
e4ac870f
LG
1913 SectionBuffer + DataOffset,\r
1914 BufferLength - DataOffset\r
f51461c8
LG
1915 );\r
1916 if (EFI_ERROR (Status)) {\r
1917 Error (NULL, 0, 0003, "parse of CRC32 GUIDED section failed", NULL);\r
1918 return EFI_SECTION_ERROR;\r
1919 }\r
1920 } else {\r
1921 //\r
1922 // We don't know how to parse it now.\r
1923 //\r
1924 Error (NULL, 0, 0003, "Error parsing section", \\r
1925 "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
1926 return EFI_UNSUPPORTED;\r
1927 }\r
1928 break;\r
1929\r
1930 default:\r
1931 //\r
1932 // Unknown section, return error\r
1933 //\r
1934 Error (NULL, 0, 0003, "unrecognized section type found", "section type = 0x%X", Type);\r
1935 return EFI_SECTION_ERROR;\r
1936 }\r
1937\r
1938 ParsedLength += SectionLength;\r
1939 //\r
1940 // We make then next section begin on a 4-byte boundary\r
1941 //\r
1942 ParsedLength = GetOccupiedSize (ParsedLength, 4);\r
1943 }\r
1944\r
1945 if (ParsedLength < BufferLength) {\r
1946 Error (NULL, 0, 0003, "sections do not completely fill the sectioned buffer being parsed", NULL);\r
1947 return EFI_SECTION_ERROR;\r
1948 }\r
1949\r
1950 return EFI_SUCCESS;\r
1951}\r
1952\r
1953EFI_STATUS\r
1954DumpDepexSection (\r
1955 IN UINT8 *Ptr,\r
1956 IN UINT32 SectionLength\r
1957 )\r
1958/*++\r
1959\r
1960Routine Description:\r
1961\r
1962 GC_TODO: Add function description\r
1963\r
1964Arguments:\r
1965\r
1966 Ptr - GC_TODO: add argument description\r
1967 SectionLength - GC_TODO: add argument description\r
1968\r
1969Returns:\r
1970\r
1971 EFI_SUCCESS - GC_TODO: Add description for return value\r
1972\r
1973--*/\r
1974{\r
1975 UINT8 GuidBuffer[PRINTED_GUID_BUFFER_SIZE];\r
1976\r
1977 //\r
1978 // Need at least a section header + data\r
1979 //\r
1980 if (SectionLength <= sizeof (EFI_COMMON_SECTION_HEADER)) {\r
1981 return EFI_SUCCESS;\r
1982 }\r
1983\r
e4ac870f
LG
1984 Ptr += GetSectionHeaderLength((EFI_COMMON_SECTION_HEADER *)Ptr);\r
1985 SectionLength -= GetSectionHeaderLength((EFI_COMMON_SECTION_HEADER *)Ptr);\r
f51461c8
LG
1986 while (SectionLength > 0) {\r
1987 printf (" ");\r
1988 switch (*Ptr) {\r
1989 case EFI_DEP_BEFORE:\r
1990 printf ("BEFORE\n");\r
1991 Ptr++;\r
1992 SectionLength--;\r
1993 break;\r
1994\r
1995 case EFI_DEP_AFTER:\r
1996 printf ("AFTER\n");\r
1997 Ptr++;\r
1998 SectionLength--;\r
1999 break;\r
2000\r
2001 case EFI_DEP_PUSH:\r
2002 printf ("PUSH\n ");\r
2003 PrintGuidToBuffer ((EFI_GUID *) (Ptr + 1), GuidBuffer, sizeof (GuidBuffer), TRUE);\r
2004 printf ("%s ", GuidBuffer);\r
2005 PrintGuidName (GuidBuffer);\r
2006 printf ("\n");\r
2007 //\r
2008 // PrintGuid ((EFI_GUID *)(Ptr + 1));\r
2009 //\r
2010 Ptr += 17;\r
2011 SectionLength -= 17;\r
2012 break;\r
2013\r
2014 case EFI_DEP_AND:\r
2015 printf ("AND\n");\r
2016 Ptr++;\r
2017 SectionLength--;\r
2018 break;\r
2019\r
2020 case EFI_DEP_OR:\r
2021 printf ("OR\n");\r
2022 Ptr++;\r
2023 SectionLength--;\r
2024 break;\r
2025\r
2026 case EFI_DEP_NOT:\r
2027 printf ("NOT\n");\r
2028 Ptr++;\r
2029 SectionLength--;\r
2030 break;\r
2031\r
2032 case EFI_DEP_TRUE:\r
2033 printf ("TRUE\n");\r
2034 Ptr++;\r
2035 SectionLength--;\r
2036 break;\r
2037\r
2038 case EFI_DEP_FALSE:\r
2039 printf ("FALSE\n");\r
2040 Ptr++;\r
2041 SectionLength--;\r
2042 break;\r
2043\r
2044 case EFI_DEP_END:\r
2045 printf ("END DEPEX\n");\r
2046 Ptr++;\r
2047 SectionLength--;\r
2048 break;\r
2049\r
2050 case EFI_DEP_SOR:\r
2051 printf ("SOR\n");\r
2052 Ptr++;\r
2053 SectionLength--;\r
2054 break;\r
2055\r
2056 default:\r
2057 printf ("Unrecognized byte in depex: 0x%X\n", *Ptr);\r
2058 return EFI_SUCCESS;\r
2059 }\r
2060 }\r
2061\r
2062 return EFI_SUCCESS;\r
2063}\r
2064\r
2065EFI_STATUS\r
2066PrintGuidName (\r
2067 IN UINT8 *GuidStr\r
2068 )\r
2069/*++\r
2070\r
2071Routine Description:\r
2072\r
2073 GC_TODO: Add function description\r
2074\r
2075Arguments:\r
2076\r
2077 GuidStr - GC_TODO: add argument description\r
2078\r
2079Returns:\r
2080\r
2081 EFI_SUCCESS - GC_TODO: Add description for return value\r
2082 EFI_INVALID_PARAMETER - GC_TODO: Add description for return value\r
2083\r
2084--*/\r
2085{\r
2086 GUID_TO_BASENAME *GPtr;\r
2087 //\r
2088 // If we have a list of guid-to-basenames, then go through the list to\r
2089 // look for a guid string match. If found, print the basename to stdout,\r
2090 // otherwise return a failure.\r
2091 //\r
2092 GPtr = mGuidBaseNameList;\r
2093 while (GPtr != NULL) {\r
2094 if (_stricmp ((CHAR8*) GuidStr, (CHAR8*) GPtr->Guid) == 0) {\r
2095 printf ("%s", GPtr->BaseName);\r
2096 return EFI_SUCCESS;\r
2097 }\r
2098\r
2099 GPtr = GPtr->Next;\r
2100 }\r
2101\r
2102 return EFI_INVALID_PARAMETER;\r
2103}\r
2104\r
2105EFI_STATUS\r
2106ParseGuidBaseNameFile (\r
2107 CHAR8 *FileName\r
2108 )\r
2109/*++\r
2110\r
2111Routine Description:\r
2112\r
2113 GC_TODO: Add function description\r
2114\r
2115Arguments:\r
2116\r
2117 FileName - GC_TODO: add argument description\r
2118\r
2119Returns:\r
2120\r
2121 EFI_DEVICE_ERROR - GC_TODO: Add description for return value\r
2122 EFI_OUT_OF_RESOURCES - GC_TODO: Add description for return value\r
2123 EFI_SUCCESS - GC_TODO: Add description for return value\r
2124\r
2125--*/\r
2126{\r
2127 FILE *Fptr;\r
2128 CHAR8 Line[MAX_LINE_LEN];\r
2129 GUID_TO_BASENAME *GPtr;\r
2130\r
97fa0ee9 2131 if ((Fptr = fopen (LongFilePath (FileName), "r")) == NULL) {\r
f51461c8
LG
2132 printf ("ERROR: Failed to open input cross-reference file '%s'\n", FileName);\r
2133 return EFI_DEVICE_ERROR;\r
2134 }\r
2135\r
2136 while (fgets (Line, sizeof (Line), Fptr) != NULL) {\r
2137 //\r
2138 // Allocate space for another guid/basename element\r
2139 //\r
2140 GPtr = malloc (sizeof (GUID_TO_BASENAME));\r
2141 if (GPtr == NULL) {\r
2142 return EFI_OUT_OF_RESOURCES;\r
2143 }\r
2144\r
2145 memset ((char *) GPtr, 0, sizeof (GUID_TO_BASENAME));\r
2146 if (sscanf (Line, "%s %s", GPtr->Guid, GPtr->BaseName) == 2) {\r
2147 GPtr->Next = mGuidBaseNameList;\r
2148 mGuidBaseNameList = GPtr;\r
2149 } else {\r
2150 //\r
2151 // Some sort of error. Just continue.\r
2152 //\r
2153 free (GPtr);\r
2154 }\r
2155 }\r
2156\r
2157 fclose (Fptr);\r
2158 return EFI_SUCCESS;\r
2159}\r
2160\r
2161EFI_STATUS\r
2162FreeGuidBaseNameList (\r
2163 VOID\r
2164 )\r
2165/*++\r
2166\r
2167Routine Description:\r
2168\r
2169 GC_TODO: Add function description\r
2170\r
2171Arguments:\r
2172\r
2173 None\r
2174\r
2175Returns:\r
2176\r
2177 EFI_SUCCESS - GC_TODO: Add description for return value\r
2178\r
2179--*/\r
2180{\r
2181 GUID_TO_BASENAME *Next;\r
2182\r
2183 while (mGuidBaseNameList != NULL) {\r
2184 Next = mGuidBaseNameList->Next;\r
2185 free (mGuidBaseNameList);\r
2186 mGuidBaseNameList = Next;\r
2187 }\r
2188\r
2189 return EFI_SUCCESS;\r
2190}\r
2191\r
2192\r
2193static\r
2194VOID\r
2195LoadGuidedSectionToolsTxt (\r
2196 IN CHAR8* FirmwareVolumeFilename\r
2197 )\r
2198{\r
2199 CHAR8* PeerFilename;\r
2200 CHAR8* Places[] = {\r
2201 NULL,\r
2202 //NULL,\r
2203 };\r
2204 UINTN Index;\r
2205\r
2206 Places[0] = FirmwareVolumeFilename;\r
2207 //Places[1] = mUtilityFilename;\r
2208\r
2209 mParsedGuidedSectionTools = NULL;\r
2210\r
2211 for (Index = 0; Index < (sizeof(Places)/sizeof(Places[0])); Index++) {\r
2212 PeerFilename = OsPathPeerFilePath (Places[Index], "GuidedSectionTools.txt");\r
2213 //printf("Loading %s...\n", PeerFilename);\r
2214 if (OsPathExists (PeerFilename)) {\r
2215 mParsedGuidedSectionTools = ParseGuidedSectionToolsFile (PeerFilename);\r
2216 }\r
2217 free (PeerFilename);\r
2218 if (mParsedGuidedSectionTools != NULL) {\r
2219 return;\r
2220 }\r
2221 }\r
2222}\r
2223\r
2224\r
2225void\r
2226Usage (\r
2227 VOID\r
2228 )\r
2229/*++\r
2230\r
2231Routine Description:\r
2232\r
2233 GC_TODO: Add function description\r
2234\r
2235Arguments:\r
2236\r
2237 None\r
2238\r
2239Returns:\r
2240\r
2241 GC_TODO: add return values\r
2242\r
2243--*/\r
2244{\r
2245 //\r
2246 // Summary usage\r
2247 //\r
2248 fprintf (stdout, "Usage: %s [options] <input_file>\n\n", UTILITY_NAME);\r
2249\r
2250 //\r
2251 // Copyright declaration\r
2252 // \r
45258285 2253 fprintf (stdout, "Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.\n\n");\r
f4260465 2254 fprintf (stdout, " Display Tiano Firmware Volume FFS image information\n\n");\r
f51461c8
LG
2255\r
2256 //\r
2257 // Details Option\r
2258 //\r
730ffca1 2259 fprintf (stdout, "optional arguments:\n");\r
f51461c8 2260 fprintf (stdout, " -h, --help\n\\r
730ffca1
YZ
2261 Show this help message and exit\n");\r
2262 fprintf (stdout, " --version\n\\r
2263 Show program's version number and exit\n");\r
2264 fprintf (stdout, " -d [DEBUG], --debug [DEBUG]\n\\r
2265 Output DEBUG statements, where DEBUG_LEVEL is 0 (min) - 9 (max)\n");\r
2266 fprintf (stdout, " -v, --verbose\n\\r
2267 Print informational statements\n");\r
2268 fprintf (stdout, " -q, --quiet\n\\r
2269 Returns the exit code, error messages will be displayed\n");\r
2270 fprintf (stdout, " -s, --silent\n\\r
2271 Returns only the exit code; informational and error\n\\r
2272 messages are not displayed\n");\r
2273 fprintf (stdout, " -x XREF_FILENAME, --xref XREF_FILENAME\n\\r
2274 Parse the basename to file-guid cross reference file(s)\n");\r
2275 fprintf (stdout, " -f OFFSET, --offset OFFSET\n\\r
2276 The offset from the start of the input file to start \n\\r
2277 processing an FV\n");\r
9947f576
YZ
2278 fprintf (stdout, " --hash\n\\r
2279 Generate HASH value of the entire PE image\n");\r
730ffca1
YZ
2280 fprintf (stdout, " --sfo\n\\r
2281 Reserved for future use\n");\r
f51461c8
LG
2282}\r
2283\r