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