3 Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Utility program to create an EFI option ROM image from binary and
30 // Includes for EFI 1.1 build
32 // #include "Tiano.h" // required defines for Compress.h
33 // #include "EfiImage.h" // for PE32 structure definitions
34 // #include "Compress.h" // for compression function
35 // Includes for Tiano build
37 #include "TianoCommon.h"
38 #include "EfiImage.h" // for PE32 structure definitions
42 // END include differences
44 #include "Pci.h" // for option ROM header structures
46 // Version of this utility
48 #define UTILITY_NAME "EfiRom"
49 #define UTILITY_VERSION "v2.7"
52 // Define some status return values
54 #define STATUS_SUCCESS 0
55 #define STATUS_WARNING 1
56 #define STATUS_ERROR 2
59 // Define the max length of a filename
63 #define DEFAULT_OUTPUT_EXTENSION ".rom"
66 // Max size for an option ROM image
68 #define MAX_OPTION_ROM_SIZE (1024 * 1024 * 16) // 16MB
70 // Values for the indicator field in the PCI data structure
72 #define INDICATOR_LAST 0x80 // last file in series of files
74 // Masks for the FILE_LIST.FileFlags field
76 #define FILE_FLAG_BINARY 0x01
77 #define FILE_FLAG_EFI 0x02
78 #define FILE_FLAG_COMPRESS 0x04
81 // Use this linked list structure to keep track of all the filenames
82 // specified on the command line.
84 typedef struct _FILE_LIST
{
85 struct _FILE_LIST
*Next
;
93 // Use this to track our command-line options
96 INT8 OutFileName
[MAX_PATH
];
108 // Make a global structure to keep track of command-line options
110 static OPTIONS mOptions
;
113 // Use these to convert from machine type value to a named type
120 static STRING_LOOKUP mMachineTypes
[] = {
121 EFI_IMAGE_MACHINE_IA32
,
123 EFI_IMAGE_MACHINE_IA64
,
125 EFI_IMAGE_MACHINE_EBC
,
131 static STRING_LOOKUP mSubsystemTypes
[] = {
132 EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
,
134 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
,
135 "EFI boot service driver",
136 EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
,
137 "EFI runtime driver",
142 static char* mCodeTypeStr
[] = {
144 "Open Firmware Image",
151 // Function prototypes
206 GetSubsystemTypeStr (
219 Given an EFI image filename, create a ROM-able image by creating an option
220 ROM header and PCI data structure, filling them in, and then writing the
221 option ROM header + PCI data structure + EFI image out to the output file.
225 Argc - standard C main() argument count
227 Argv - standard C main() argument list
235 // GC_TODO: ] - add argument and description to function comment
244 Status
= STATUS_SUCCESS
;
248 // Parse the command line arguments
250 if (ParseCommandLine (Argc
, Argv
, &mOptions
)) {
254 // If dumping an image, then do that and quit
256 if (mOptions
.DumpOption
) {
257 DumpImage (mOptions
.FileList
);
261 // Determine the output filename. Either what they specified on
262 // the command line, or the first input filename with a different extension.
264 if (!mOptions
.OutFileName
[0]) {
265 strcpy (mOptions
.OutFileName
, mOptions
.FileList
->FileName
);
267 // Find the last . on the line and replace the filename extension with
270 for (Ext
= mOptions
.OutFileName
+ strlen (mOptions
.OutFileName
) - 1;
271 (Ext
>= mOptions
.OutFileName
) && (*Ext
!= '.') && (*Ext
!= '\\');
276 // If dot here, then insert extension here, otherwise append
279 Ext
= mOptions
.OutFileName
+ strlen (mOptions
.OutFileName
);
282 strcpy (Ext
, DEFAULT_OUTPUT_EXTENSION
);
285 // Make sure we don't have the same filename for input and output files
287 for (FList
= mOptions
.FileList
; FList
!= NULL
; FList
= FList
->Next
) {
288 if (_stricmp (mOptions
.OutFileName
, FList
->FileName
) == 0) {
289 Status
= STATUS_ERROR
;
292 "ERROR: Input and output file names must be different - %s = %s\n",
300 // Now open our output file
302 if ((FptrOut
= fopen (mOptions
.OutFileName
, "w+b")) == NULL
) {
303 fprintf (stdout
, "ERROR: Failed to open output file %s\n", mOptions
.OutFileName
);
307 // Process all our files
310 for (FList
= mOptions
.FileList
; FList
!= NULL
; FList
= FList
->Next
) {
312 if (FList
->FileFlags
& FILE_FLAG_EFI
) {
313 if (mOptions
.Verbose
) {
314 fprintf (stdout
, "Processing EFI file %s\n", FList
->FileName
);
317 Status
= ProcessEfiFile (FptrOut
, FList
, mOptions
.VendId
, mOptions
.DevId
, &Size
);
318 } else if (FList
->FileFlags
& FILE_FLAG_BINARY
) {
319 if (mOptions
.Verbose
) {
320 fprintf (stdout
, "Processing binary file %s\n", FList
->FileName
);
323 Status
= ProcessBinFile (FptrOut
, FList
, &Size
);
325 fprintf (stdout
, "ERROR: File not specified as EFI or binary: %s\n", FList
->FileName
);
326 Status
= STATUS_ERROR
;
329 if (mOptions
.Verbose
) {
330 fprintf (stdout
, " Output size = 0x%X\n", Size
);
333 if (Status
!= STATUS_SUCCESS
) {
342 if (TotalSize
> MAX_OPTION_ROM_SIZE
) {
345 "ERROR: Option ROM image size exceeds limit 0x%X bytes\n",
348 Status
= STATUS_ERROR
;
352 if (FptrOut
!= NULL
) {
356 // Clean up our file list
358 while (mOptions
.FileList
!= NULL
) {
359 FList
= mOptions
.FileList
->Next
;
360 free (mOptions
.FileList
);
361 mOptions
.FileList
= FList
;
375 Calculate checksum from DataSize of Buffer.
378 Buffer - pointer to data buffer
379 DataSize - size of data buffer in bytes
386 while (DataSize
-- != 0) {
387 Checksum
= Checksum
+ Buffer
[DataSize
];
389 while (PaddingSize
-- != 0) {
390 Checksum
= Checksum
+ 0xff;
400 if (CodeType
>= sizeof (mCodeTypeStr
) / sizeof (*mCodeTypeStr
)) {
401 CodeType
= sizeof (mCodeTypeStr
) / sizeof (*mCodeTypeStr
) - 1;
403 return mCodeTypeStr
[CodeType
];
417 Process a binary input file.
421 OutFptr - file pointer to output binary ROM image file we're creating
422 InFile - structure contains information on the binary file to process
423 Size - pointer to where to return the size added to the output file
438 PCI_EXPANSION_ROM_HEADER
*RomHdr
;
439 PCI_DATA_STRUCTURE
*PciDs
;
442 Status
= STATUS_SUCCESS
;
445 // Try to open the input file
447 if ((InFptr
= fopen (InFile
->FileName
, "rb")) == NULL
) {
448 fprintf (stdout
, "ERROR: Failed to open input file %s\n", InFile
->FileName
);
452 // Seek to the end of the input file and get the file size. Then allocate
453 // a buffer to read it in to.
455 fseek (InFptr
, 0, SEEK_END
);
456 FileSize
= ftell (InFptr
);
457 if (mOptions
.Verbose
) {
458 fprintf (stdout
, " File size = 0x%X\n", FileSize
);
461 fseek (InFptr
, 0, SEEK_SET
);
462 Buffer
= (INT8
*) malloc (FileSize
);
463 if (Buffer
== NULL
) {
464 fprintf (stdout
, "ERROR: Memory allocation failed\n");
465 Status
= STATUS_ERROR
;
469 if (fread (Buffer
, FileSize
, 1, InFptr
) != 1) {
470 fprintf (stdout
, "ERROR: Failed to read all bytes from input file\n");
471 Status
= STATUS_ERROR
;
476 RomHdr
= (PCI_EXPANSION_ROM_HEADER
*) Buffer
;
477 PciDs
= (PCI_DATA_STRUCTURE
*) (Buffer
+ RomHdr
->PcirOffset
);
480 // Crude check to make sure it's a legitimate ROM image
482 if (RomHdr
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
483 fprintf (stdout
, "ERROR: ROM image file has invalid ROM signature\n");
484 Status
= STATUS_ERROR
;
488 // Make sure the pointer to the PCI data structure is within the size of the image.
489 // Then check it for valid signature.
491 if ((RomHdr
->PcirOffset
> FileSize
) || (RomHdr
->PcirOffset
== 0)) {
492 fprintf (stdout
, "ERROR: Invalid PCI data structure offset\n");
493 Status
= STATUS_ERROR
;
497 if (PciDs
->Signature
!= PCI_DATA_STRUCTURE_SIGNATURE
) {
498 fprintf (stdout
, "ERROR: PCI data structure has invalid signature\n");
499 Status
= STATUS_ERROR
;
503 if ((UINT32
) (PciDs
->ImageLength
* 512) == FileSize
) {
505 // ImageLength reflects the actual file size correctly.
507 DataSize
= FileSize
- 1;
509 TotalSize
= FileSize
;
512 // ImageLength doesn't reflect the actual file size,
513 // 1). add additional 512 bytes if actual file size is multiple of 512
514 // 2). add additional X (X <= 512) bytes so that the result size is multiple of 512
516 fprintf (stdout
, "WARNING: ImageLength in PCI data structure != Actual File Size\n"
517 " --> add additional padding bytes\n"
518 " --> adjust ImageLength\n"
520 TotalSize
= (FileSize
+ 0x200) & ~0x1ff;
522 PaddingSize
= TotalSize
- DataSize
- 1;
523 PciDs
->ImageLength
= (UINT16
) (TotalSize
/ 512);
529 if (TotalSize
> MAX_OPTION_ROM_SIZE
) {
532 "ERROR: Option ROM image %s size exceeds limit 0x%X bytes\n",
536 Status
= STATUS_ERROR
;
541 // Return the size to the caller so they can keep track of the running total.
546 // If this is the last image, then set the LAST bit unless requested not
547 // to via the command-line -l argument. Otherwise, make sure you clear it.
549 if ((InFile
->Next
== NULL
) && (mOptions
.NoLast
== 0)) {
550 PciDs
->Indicator
|= INDICATOR_LAST
;
552 PciDs
->Indicator
&= ~INDICATOR_LAST
;
555 ByteCheckSum
= -CheckSum (Buffer
, DataSize
, PaddingSize
);
557 if (fwrite (Buffer
, DataSize
, 1, OutFptr
) != 1) {
558 fprintf (stdout
, "ERROR: Failed to write all file bytes to output file\n");
559 Status
= STATUS_ERROR
;
563 while (PaddingSize
-- != 0) {
566 putc (ByteCheckSum
, OutFptr
);
569 if (InFptr
!= NULL
) {
573 if (Buffer
!= NULL
) {
577 // Print the file name if errors occurred
579 if (Status
!= STATUS_SUCCESS
) {
580 fprintf (stdout
, "Error processing binary file %s\n", InFile
->FileName
);
599 Process a PE32 EFI file.
603 OutFptr - file pointer to output binary ROM image file we're creating
604 InFile - structure contains information on the PE32 file to process
605 VendId - vendor ID as required in the option ROM header
606 DevId - device ID as required in the option ROM header
607 Size - pointer to where to return the size added to the output file
617 EFI_PCI_EXPANSION_ROM_HEADER RomHdr
;
618 PCI_DATA_STRUCTURE PciDs
;
620 UINT32 CompressedFileSize
;
622 UINT8
*CompressedBuffer
;
623 UINT8
*TempBufferPtr
;
628 UINT32 HeaderPadBytes
;
631 // Try to open the input file
633 if ((InFptr
= fopen (InFile
->FileName
, "rb")) == NULL
) {
634 fprintf (stdout
, "ERROR: Failed to open input file %s\n", InFile
->FileName
);
638 // Initialize our buffer pointers to null.
641 CompressedBuffer
= NULL
;
644 // Double-check the file to make sure it's what we expect it to be
646 Status
= CheckPE32File (InFptr
, &MachineType
, &SubSystem
);
647 if (Status
!= STATUS_SUCCESS
) {
651 // Seek to the end of the input file and get the file size
653 fseek (InFptr
, 0, SEEK_END
);
654 FileSize
= ftell (InFptr
);
657 // Get the size of the headers we're going to put in front of the image. The
658 // EFI header must be aligned on a 4-byte boundary, so pad accordingly.
660 if (sizeof (RomHdr
) & 0x03) {
661 HeaderPadBytes
= 4 - (sizeof (RomHdr
) & 0x03);
666 HeaderSize
= sizeof (PCI_DATA_STRUCTURE
) + HeaderPadBytes
+ sizeof (EFI_PCI_EXPANSION_ROM_HEADER
);
667 if (mOptions
.Verbose
) {
668 fprintf (stdout
, " File size = 0x%X\n", FileSize
);
671 // Allocate memory for the entire file (in case we have to compress), then
672 // seek back to the beginning of the file and read it into our buffer.
674 Buffer
= (INT8
*) malloc (FileSize
);
675 if (Buffer
== NULL
) {
676 fprintf (stdout
, "ERROR: Memory allocation failed\n");
677 Status
= STATUS_ERROR
;
681 fseek (InFptr
, 0, SEEK_SET
);
682 if (fread (Buffer
, FileSize
, 1, InFptr
) != 1) {
683 fprintf (stdout
, "ERROR: Failed to read all bytes from input file\n");
684 Status
= STATUS_ERROR
;
688 // Now determine the size of the final output file. It's either the header size
689 // plus the file's size, or the header size plus the compressed file size.
691 if (InFile
->FileFlags
& FILE_FLAG_COMPRESS
) {
693 // Allocate a buffer into which we can compress the image, compress it,
694 // and use that size as the new size.
696 CompressedBuffer
= (INT8
*) malloc (FileSize
);
697 if (CompressedBuffer
== NULL
) {
698 fprintf (stdout
, "ERROR: Memory allocation failed\n");
699 Status
= STATUS_ERROR
;
703 CompressedFileSize
= FileSize
;
704 Status
= EfiCompress (Buffer
, FileSize
, CompressedBuffer
, &CompressedFileSize
);
705 if (Status
!= STATUS_SUCCESS
) {
706 fprintf (stdout
, "ERROR: Compression failed\n");
710 // Now compute the size, then swap buffer pointers.
712 if (mOptions
.Verbose
) {
713 fprintf (stdout
, " Comp size = 0x%X\n", CompressedFileSize
);
716 TotalSize
= CompressedFileSize
+ HeaderSize
;
717 FileSize
= CompressedFileSize
;
718 TempBufferPtr
= Buffer
;
719 Buffer
= CompressedBuffer
;
720 CompressedBuffer
= TempBufferPtr
;
722 TotalSize
= FileSize
+ HeaderSize
;
725 // Total size must be an even multiple of 512 bytes
727 TotalSize
= (TotalSize
+ 0x1ff) & ~0x1ff;
732 if (TotalSize
> MAX_OPTION_ROM_SIZE
) {
735 "ERROR: Option ROM image %s size exceeds limit 0x%X bytes\n",
739 Status
= STATUS_ERROR
;
743 // Return the size to the caller so they can keep track of the running total.
748 // Now fill in the ROM header. These values come from chapter 18 of the
749 // EFI 1.02 specification.
751 memset (&RomHdr
, 0, sizeof (RomHdr
));
752 RomHdr
.Signature
= PCI_EXPANSION_ROM_HEADER_SIGNATURE
;
753 RomHdr
.InitializationSize
= (UINT16
) (TotalSize
/ 512);
754 RomHdr
.EfiSignature
= EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
;
755 RomHdr
.EfiSubsystem
= SubSystem
;
756 RomHdr
.EfiMachineType
= MachineType
;
757 RomHdr
.EfiImageHeaderOffset
= (UINT16
) HeaderSize
;
758 RomHdr
.PcirOffset
= (UINT16
) (sizeof (RomHdr
) + HeaderPadBytes
);
760 // Set image as compressed or not
762 if (InFile
->FileFlags
& FILE_FLAG_COMPRESS
) {
763 RomHdr
.CompressionType
= EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
;
766 // Fill in the PCI data structure
768 memset (&PciDs
, 0, sizeof (PCI_DATA_STRUCTURE
));
770 PciDs
.Signature
= PCI_DATA_STRUCTURE_SIGNATURE
;
771 PciDs
.VendorId
= VendId
;
772 PciDs
.DeviceId
= DevId
;
773 PciDs
.Length
= (UINT16
) sizeof (PCI_DATA_STRUCTURE
);
776 // Class code and code revision from the command line (optional)
778 PciDs
.ClassCode
[0] = (UINT8
) InFile
->ClassCode
;
779 PciDs
.ClassCode
[1] = (UINT8
) (InFile
->ClassCode
>> 8);
780 PciDs
.ClassCode
[2] = (UINT8
) (InFile
->ClassCode
>> 16);
781 PciDs
.ImageLength
= RomHdr
.InitializationSize
;
782 PciDs
.CodeRevision
= InFile
->CodeRevision
;
783 PciDs
.CodeType
= PCI_CODE_TYPE_EFI_IMAGE
;
786 // If this is the last image, then set the LAST bit unless requested not
787 // to via the command-line -l argument.
789 if ((InFile
->Next
== NULL
) && (mOptions
.NoLast
== 0)) {
790 PciDs
.Indicator
|= INDICATOR_LAST
;
793 // Write the ROM header to the output file
795 if (fwrite (&RomHdr
, sizeof (RomHdr
), 1, OutFptr
) != 1) {
796 fprintf (stdout
, "ERROR: Failed to write ROM header to output file\n");
797 Status
= STATUS_ERROR
;
802 // Write pad bytes to align the PciDs
804 while (HeaderPadBytes
> 0) {
805 if (putc (0, OutFptr
) == EOF
) {
806 fprintf (stdout
, "ERROR: Failed to write ROM header pad bytes to output file\n");
807 Status
= STATUS_ERROR
;
814 // Write the PCI data structure header to the output file
816 if (fwrite (&PciDs
, sizeof (PciDs
), 1, OutFptr
) != 1) {
817 fprintf (stdout
, "ERROR: Failed to write PCI ROM header to output file\n");
818 Status
= STATUS_ERROR
;
822 // Keep track of how many bytes left to write
824 TotalSize
-= HeaderSize
;
827 // Now dump the input file's contents to the output file
829 if (fwrite (Buffer
, FileSize
, 1, OutFptr
) != 1) {
830 fprintf (stdout
, "ERROR: Failed to write all file bytes to output file\n");
831 Status
= STATUS_ERROR
;
835 TotalSize
-= FileSize
;
837 // Pad the rest of the image to make it a multiple of 512 bytes
839 while (TotalSize
> 0) {
840 if (putc (~0, OutFptr
) == EOF
) {
841 fprintf (stdout
, "ERROR: Failed to write trailing pad bytes output file\n");
842 Status
= STATUS_ERROR
;
850 if (InFptr
!= NULL
) {
855 // Free up our buffers
857 if (Buffer
!= NULL
) {
861 if (CompressedBuffer
!= NULL
) {
862 free (CompressedBuffer
);
865 // Print the file name if errors occurred
867 if (Status
!= STATUS_SUCCESS
) {
868 fprintf (stdout
, "Error processing EFI file %s\n", InFile
->FileName
);
885 GC_TODO: Add function description
889 Fptr - GC_TODO: add argument description
890 MachineType - GC_TODO: add argument description
891 SubSystem - GC_TODO: add argument description
895 GC_TODO: add return values
903 Given a file pointer to a supposed PE32 image file, verify that it is indeed a
904 PE32 image file, and then return the machine type in the supplied pointer.
908 Fptr File pointer to the already-opened PE32 file
909 MachineType Location to stuff the machine type of the PE32 file. This is needed
910 because the image may be Itanium-based, IA32, or EBC.
918 EFI_IMAGE_DOS_HEADER DosHeader
;
919 EFI_IMAGE_FILE_HEADER FileHdr
;
920 EFI_IMAGE_OPTIONAL_HEADER OptionalHdr
;
924 // Position to the start of the file
926 fseek (Fptr
, 0, SEEK_SET
);
929 // Read the DOS header
931 if (fread (&DosHeader
, sizeof (DosHeader
), 1, Fptr
) != 1) {
932 fprintf (stdout
, "ERROR: Failed to read the DOS stub from the input file\n");
936 // Check the magic number (0x5A4D)
938 if (DosHeader
.e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
939 fprintf (stdout
, "ERROR: Input file does not appear to be a PE32 image (magic number)\n");
943 // Position into the file and check the PE signature
945 fseek (Fptr
, (long) DosHeader
.e_lfanew
, SEEK_SET
);
946 if (fread (&PESig
, sizeof (PESig
), 1, Fptr
) != 1) {
947 fprintf (stdout
, "ERROR: Failed to read PE signature bytes from input file\n");
951 // Check the PE signature in the header "PE\0\0"
953 if (PESig
!= EFI_IMAGE_NT_SIGNATURE
) {
954 fprintf (stdout
, "ERROR: Input file does not appear to be a PE32 image (signature)\n");
958 // Read the file header and stuff their MachineType
960 if (fread (&FileHdr
, sizeof (FileHdr
), 1, Fptr
) != 1) {
961 fprintf (stdout
, "ERROR: Failed to read PE file header from input file\n");
965 memcpy ((char *) MachineType
, &FileHdr
.Machine
, 2);
968 // Read the optional header so we can get the subsystem
970 if (fread (&OptionalHdr
, sizeof (OptionalHdr
), 1, Fptr
) != 1) {
971 fprintf (stdout
, "ERROR: Failed to read COFF optional header from input file\n");
975 *SubSystem
= OptionalHdr
.Subsystem
;
976 if (mOptions
.Verbose
) {
977 fprintf (stdout
, " Got subsystem = 0x%X from image\n", (int) *SubSystem
);
982 return STATUS_SUCCESS
;
996 Given the Argc/Argv program arguments, and a pointer to an options structure,
997 parse the command-line options and check their validity.
1002 Argc - standard C main() argument count
1003 Argv[] - standard C main() argument list
1004 Options - pointer to a structure to store the options in
1008 STATUS_SUCCESS success
1014 FILE_LIST
*FileList
;
1016 FILE_LIST
*PrevFileList
;
1019 UINT32 CodeRevision
;
1024 // Clear out the options
1026 memset ((char *) Options
, 0, sizeof (OPTIONS
));
1029 // To avoid compile warnings
1031 FileList
= PrevFileList
= NULL
;
1036 // Skip over the program name
1042 // If no arguments, assume they want usage info
1046 return STATUS_ERROR
;
1049 // Process until no more arguments
1052 if ((Argv
[0][0] == '-') || (Argv
[0][0] == '/')) {
1054 // To simplify string comparisons, replace slashes with dashes
1059 // Vendor ID specified with -v
1061 if (_stricmp (Argv
[0], "-v") == 0) {
1063 // Make sure there's another parameter
1066 Options
->VendId
= (UINT16
) strtol (Argv
[1], NULL
, 16);
1067 Options
->VendIdValid
= 1;
1071 "ERROR: Missing Vendor ID with %s\n\n",
1075 return STATUS_ERROR
;
1080 } else if (_stricmp (Argv
[0], "-d") == 0) {
1082 // Device ID specified with -d
1083 // Make sure there's another parameter
1086 Options
->DevId
= (UINT16
) strtol (Argv
[1], NULL
, 16);
1087 Options
->DevIdValid
= 1;
1091 "ERROR: Missing Device ID with %s\n\n",
1095 return STATUS_ERROR
;
1100 } else if (_stricmp (Argv
[0], "-o") == 0) {
1102 // Output filename specified with -o
1103 // Make sure there's another parameter
1106 strcpy (Options
->OutFileName
, Argv
[1]);
1110 "ERROR: Missing output file name with %s\n\n",
1114 return STATUS_ERROR
;
1119 } else if ((_stricmp (Argv
[0], "-h") == 0) || (strcmp (Argv
[0], "-?") == 0)) {
1124 return STATUS_ERROR
;
1125 } else if (_stricmp (Argv
[0], "-b") == 0) {
1127 // Specify binary files with -b
1129 FileFlags
= (FileFlags
& ~FILE_FLAG_EFI
) | FILE_FLAG_BINARY
;
1130 } else if ((_stricmp (Argv
[0], "-e") == 0) || (_stricmp (Argv
[0], "-ec") == 0)) {
1132 // Specify EFI files with -e. Specify EFI-compressed with -ec.
1134 FileFlags
= (FileFlags
&~FILE_FLAG_BINARY
) | FILE_FLAG_EFI
;
1135 if ((Argv
[0][2] == 'c') || (Argv
[0][2] == 'C')) {
1136 FileFlags
|= FILE_FLAG_COMPRESS
;
1139 // Specify not to set the LAST bit in the last file with -l
1141 } else if (_stricmp (Argv
[0], "-l") == 0) {
1142 Options
->NoLast
= 1;
1143 } else if (_stricmp (Argv
[0], "-p") == 0) {
1145 // -v for verbose would have been nicer, but it's already used. Let's use
1146 // -p for prolix (wordy) output
1148 Options
->Verbose
= 1;
1149 } else if (_stricmp (Argv
[0], "-dump") == 0) {
1151 // -dump for dumping a ROM image. In this case, say that the device id
1152 // and vendor id are valid so we don't have to specify bogus ones on the
1155 Options
->DumpOption
= 1;
1157 Options
->VendIdValid
= 1;
1158 Options
->DevIdValid
= 1;
1159 FileFlags
= FILE_FLAG_BINARY
;
1160 } else if (_stricmp (Argv
[0], "-cc") == 0) {
1162 // Class code value for the next file in the list.
1163 // Make sure there's another parameter
1167 // No error checking on the return value. Could check for LONG_MAX,
1168 // LONG_MIN, or 0 class code value if desired. Check range (3 bytes)
1171 ClassCode
= (UINT32
) strtol (Argv
[1], NULL
, 16);
1172 if (ClassCode
& 0xFF000000) {
1173 fprintf (stdout
, "ERROR: Class code %s out of range\n", Argv
[1]);
1174 return STATUS_ERROR
;
1179 "ERROR: Missing class code value with %s\n\n",
1183 return STATUS_ERROR
;
1188 } else if (_stricmp (Argv
[0], "-rev") == 0) {
1190 // Code revision in the PCI data structure. The value is for the next
1191 // file in the list.
1192 // Make sure there's another parameter
1196 // No error checking on the return value. Could check for LONG_MAX,
1197 // LONG_MIN, or 0 value if desired. Check range (2 bytes)
1200 CodeRevision
= (UINT32
) strtol (Argv
[1], NULL
, 16);
1201 if (CodeRevision
& 0xFFFF0000) {
1202 fprintf (stdout
, "ERROR: Code revision %s out of range\n", Argv
[1]);
1203 return STATUS_ERROR
;
1208 "ERROR: Missing code revision value with %s\n\n",
1212 return STATUS_ERROR
;
1218 fprintf (stdout
, "ERROR: Invalid option specified: %s\n\n", Argv
[0]);
1220 return STATUS_ERROR
;
1224 // Not a slash-option argument. Must be a file name. Make sure they've specified
1225 // -e or -b already.
1227 if ((FileFlags
& (FILE_FLAG_BINARY
| FILE_FLAG_EFI
)) == 0) {
1228 fprintf (stdout
, "ERROR: Missing -e or -b with input file %s\n", Argv
[0]);
1229 return STATUS_ERROR
;
1232 // Create a new file structure
1234 FileList
= (FILE_LIST
*) malloc (sizeof (FILE_LIST
));
1235 if (FileList
== NULL
) {
1236 fprintf (stdout
, "ERROR: Memory allocation failure\n");
1237 return STATUS_ERROR
;
1240 memset ((char *) FileList
, 0, sizeof (FILE_LIST
));
1241 FileList
->FileName
= Argv
[0];
1242 FileList
->FileFlags
= FileFlags
;
1243 if (Options
->FileList
== NULL
) {
1244 Options
->FileList
= FileList
;
1246 if (PrevFileList
== NULL
) {
1247 PrevFileList
= FileList
;
1249 PrevFileList
->Next
= FileList
;
1253 PrevFileList
= FileList
;
1255 // Set the class code and code revision for this file, then reset the values.
1257 FileList
->ClassCode
= ClassCode
;
1258 FileList
->CodeRevision
= (UINT16
) CodeRevision
;
1269 // Make sure they specified a device ID and vendor ID
1271 if (!Options
->VendIdValid
) {
1272 fprintf (stdout
, "ERROR: Missing Vendor ID on command line\n\n");
1274 return STATUS_ERROR
;
1277 if (!Options
->DevIdValid
) {
1278 fprintf (stdout
, "ERROR: Missing Device ID on command line\n\n");
1280 return STATUS_ERROR
;
1283 // Must have specified some files
1285 if (Options
->FileList
== NULL
) {
1286 fprintf (stdout
, "ERROR: Missing input file name\n");
1288 return STATUS_ERROR
;
1301 Routine Description:
1303 Print usage information for this utility.
1316 const char *Str
[] = {
1317 UTILITY_NAME
" "UTILITY_VERSION
" - Intel EFI Make Option ROM Utility",
1318 " Copyright (C), 1999 - 2008 Intel Coproration",
1320 #if ( defined(UTILITY_BUILD) && defined(UTILITY_VENDOR) )
1321 " Built from "UTILITY_BUILD
", project of "UTILITY_VENDOR
,
1326 " "UTILITY_NAME
" [OPTION]... SOURCE ...",
1328 " Create an option ROM image from a list of input files",
1330 " -v VendorId - required hex PCI Vendor ID for the device",
1331 " -d DeviceId - required hex PCI Device ID for the device",
1332 " -o OutFileName - optional output file name. Default is the first input",
1333 " file name with a "DEFAULT_OUTPUT_EXTENSION
" file extension",
1334 " -e SOURCE ... - input are EFI PE32 image file(s)",
1335 " -b SOURCE ... - input are Legacy binary file(s)",
1336 " -ec SOURCE ... - input are EFI PE32 image file(s) and should be compressed",
1337 " -p - for verbose output",
1338 " -l - to not automatically set the LAST bit on the last file",
1339 " -cc ClassCode - to use hex ClassCode in the PCI data structure header for",
1340 " the following SOURCE(s)",
1341 " -rev Revision - to use hex Revision in the PCI data structure header for",
1342 " the following one SOURCE",
1343 " -dump - to dump the headers of an existing option ROM image",
1345 " EfiRom -v 0xABCD -d 0x1234 -b File1.bin File2.bin -e File1.efi File2.efi",
1349 for (Index
= 0; Str
[Index
] != NULL
; Index
++) {
1350 fprintf (stdout
, "%s\n", Str
[Index
]);
1361 Routine Description:
1363 GC_TODO: Add function description
1367 InFile - GC_TODO: add argument description
1371 GC_TODO: add return values
1375 PCI_EXPANSION_ROM_HEADER PciRomHdr
;
1380 EFI_PCI_EXPANSION_ROM_HEADER EfiRomHdr
;
1381 PCI_3_0_DATA_STRUCTURE PciDs
;
1384 // Open the input file
1386 if ((InFptr
= fopen (InFile
->FileName
, "rb")) == NULL
) {
1389 "ERROR: Could not open input file %s\n",
1395 // Go through the image and dump the header stuff for each
1400 // Save our postition in the file, since offsets in the headers
1401 // are relative to the particular image.
1403 ImageStart
= ftell (InFptr
);
1407 // Read the option ROM header. Have to assume a raw binary image for now.
1409 if (fread (&PciRomHdr
, sizeof (PciRomHdr
), 1, InFptr
) != 1) {
1410 fprintf (stdout
, "ERROR: Failed to read PCI ROM header from file\n");
1415 // Dump the contents of the header
1417 fprintf (stdout
, "Image %d -- Offset 0x%X\n", ImageCount
, ImageStart
);
1418 fprintf (stdout
, " ROM header contents\n");
1419 fprintf (stdout
, " Signature 0x%04X\n", (UINT32
) PciRomHdr
.Signature
);
1420 fprintf (stdout
, " PCIR offset 0x%04X\n", (UINT32
) PciRomHdr
.PcirOffset
);
1422 // Find PCI data structure
1424 if (fseek (InFptr
, ImageStart
+ PciRomHdr
.PcirOffset
, SEEK_SET
)) {
1425 fprintf (stdout
, "ERROR: Failed to seek to PCI data structure\n");
1429 // Read and dump the PCI data structure
1431 if (fread (&PciDs
, sizeof (PciDs
), 1, InFptr
) != 1) {
1432 fprintf (stdout
, "ERROR: Failed to read PCI data structure from file\n");
1436 fprintf (stdout
, " PCI Data Structure\n");
1439 " Signature %c%c%c%c\n",
1440 (char) PciDs
.Signature
,
1441 (char) (PciDs
.Signature
>> 8),
1442 (char) (PciDs
.Signature
>> 16),
1443 (char) (PciDs
.Signature
>> 24)
1445 fprintf (stdout
, " Vendor ID 0x%04X\n", PciDs
.VendorId
);
1446 fprintf (stdout
, " Device ID 0x%04X\n", PciDs
.DeviceId
);
1447 fprintf (stdout
, " Structure Length 0x%04X\n", PciDs
.Length
);
1448 fprintf (stdout
, " PCI Revision 0x%02X\n", PciDs
.Revision
);
1451 " Class Code 0x%06X\n",
1452 (UINT32
) (PciDs
.ClassCode
[0] | (PciDs
.ClassCode
[1] << 8) | (PciDs
.ClassCode
[2] << 16))
1454 fprintf (stdout
, " Image size 0x%X\n", PciDs
.ImageLength
* 512);
1455 fprintf (stdout
, " Code revision 0x%04X\n", PciDs
.CodeRevision
);
1456 fprintf (stdout
, " Indicator 0x%02X", (UINT32
) PciDs
.Indicator
);
1458 // Print the indicator, used to flag the last image
1460 if ((PciDs
.Indicator
& INDICATOR_LAST
) == INDICATOR_LAST
) {
1461 fprintf (stdout
, " (last image)\n");
1463 fprintf (stdout
, "\n");
1467 // Print the code type. If EFI code, then we can provide more info.
1469 fprintf (stdout
, " Code type 0x%02X (%s)\n", (UINT32
) PciDs
.CodeType
, GetCodeTypeStr (PciDs
.CodeType
));
1470 if (PciDs
.CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) {
1472 // Re-read the header as an EFI ROM header, then dump more info
1474 fprintf (stdout
, " EFI ROM header contents\n");
1475 memcpy (&EfiRomHdr
, &PciRomHdr
, sizeof (EfiRomHdr
));
1477 // Now dump more info
1479 fprintf (stdout
, " EFI Signature 0x%04X\n", EfiRomHdr
.EfiSignature
);
1482 " Compression Type 0x%04X ",
1483 (UINT32
) EfiRomHdr
.CompressionType
1485 if (EfiRomHdr
.CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
1486 fprintf (stdout
, "(compressed)\n");
1488 fprintf (stdout
, "(not compressed)\n");
1493 " Machine type 0x%04X (%s)\n",
1494 EfiRomHdr
.EfiMachineType
,
1495 GetMachineTypeStr (EfiRomHdr
.EfiMachineType
)
1499 " Subsystem 0x%04X (%s)\n",
1500 EfiRomHdr
.EfiSubsystem
,
1501 GetSubsystemTypeStr (EfiRomHdr
.EfiSubsystem
)
1505 " EFI image offset 0x%04X (@0x%X)\n",
1506 (UINT32
) EfiRomHdr
.EfiImageHeaderOffset
,
1507 (UINT32
) (EfiRomHdr
.EfiImageHeaderOffset
+ ImageStart
)
1512 // Dump additional information for PCI 3.0 OpROM
1514 if (PciDs
.Revision
>= 3) {
1515 fprintf (stdout
, " Extended for PCI 3.0\n");
1517 if (PciDs
.DeviceListOffset
!= 0) {
1518 if (fseek (InFptr
, ImageStart
+ PciRomHdr
.PcirOffset
+ PciDs
.DeviceListOffset
, SEEK_SET
)) {
1519 fprintf (stdout
, "ERROR: Failed to seek to supported Device List\n");
1522 fprintf (stdout
, " Device ID List ");
1524 if (fread (&DeviceId
, sizeof (DeviceId
), 1, InFptr
) != 1) {
1525 fprintf (stdout
, "ERROR: Failed to read supported DeviceId from DeviceId List\n");
1528 if (DeviceId
== 0) {
1531 fprintf (stdout
, "0x%04X ", DeviceId
);
1533 fprintf (stdout
, "\n");
1535 fprintf (stdout
, " Max Runtime Image Length 0x%08X\n", PciDs
.MaxRuntimeImageLength
* 512);
1536 if (PciDs
.Length
== sizeof (PCI_3_0_DATA_STRUCTURE
)) {
1537 fprintf (stdout
, " Config Utility Header 0x%04X\n", PciDs
.ConfigUtilityCodeHeaderOffset
);
1538 fprintf (stdout
, " DMTF CLP Entry Point 0x%04X\n", PciDs
.DMTFCLPEntryPointOffset
);
1540 fprintf (stdout
, "WARNING: Oprom declars 3.0 revision with wrong structure length 0x%04X\n", PciDs
.Length
);
1545 // If last image, then we're done
1547 if ((PciDs
.Indicator
& INDICATOR_LAST
) == INDICATOR_LAST
) {
1551 // Seek to the start of the next image
1553 if (fseek (InFptr
, ImageStart
+ (PciDs
.ImageLength
* 512), SEEK_SET
)) {
1554 fprintf (stdout
, "ERROR: Failed to seek to next image\n");
1569 Routine Description:
1571 GC_TODO: Add function description
1575 MachineType - GC_TODO: add argument description
1579 GC_TODO: add return values
1585 for (Index
= 0; mMachineTypes
[Index
].Name
!= NULL
; Index
++) {
1586 if (mMachineTypes
[Index
].Value
== MachineType
) {
1587 return mMachineTypes
[Index
].Name
;
1596 GetSubsystemTypeStr (
1597 UINT16 SubsystemType
1601 Routine Description:
1603 GC_TODO: Add function description
1607 SubsystemType - GC_TODO: add argument description
1611 GC_TODO: add return values
1617 for (Index
= 0; mSubsystemTypes
[Index
].Name
!= NULL
; Index
++) {
1618 if (mSubsystemTypes
[Index
].Value
== SubsystemType
) {
1619 return mSubsystemTypes
[Index
].Name
;