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