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