3 Copyright (c) 2007, 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_VERSION "v2.6"
51 // Define some status return values
53 #define STATUS_SUCCESS 0
54 #define STATUS_WARNING 1
55 #define STATUS_ERROR 2
58 // Define the max length of a filename
62 #define DEFAULT_OUTPUT_EXTENSION ".rom"
65 // Max size for an option ROM image
67 #define MAX_OPTION_ROM_SIZE (1024 * 1024 * 16) // 16MB
69 // Values for the indicator field in the PCI data structure
71 #define INDICATOR_LAST 0x80 // last file in series of files
73 // Masks for the FILE_LIST.FileFlags field
75 #define FILE_FLAG_BINARY 0x01
76 #define FILE_FLAG_EFI 0x02
77 #define FILE_FLAG_COMPRESS 0x04
80 // Use this linked list structure to keep track of all the filenames
81 // specified on the command line.
83 typedef struct _FILE_LIST
{
84 struct _FILE_LIST
*Next
;
92 // Use this to track our command-line options
95 INT8 OutFileName
[MAX_PATH
];
107 // Make a global structure to keep track of command-line options
109 static OPTIONS mOptions
;
112 // Use these to convert from machine type value to a named type
119 static STRING_LOOKUP mMachineTypes
[] = {
120 EFI_IMAGE_MACHINE_IA32
,
122 EFI_IMAGE_MACHINE_IA64
,
124 EFI_IMAGE_MACHINE_EBC
,
130 static STRING_LOOKUP mSubsystemTypes
[] = {
131 EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
,
133 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
,
134 "EFI boot service driver",
135 EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
,
136 "EFI runtime driver",
141 static char* mCodeTypeStr
[] = {
143 "Open Firmware Image",
150 // Function prototypes
205 GetSubsystemTypeStr (
218 Given an EFI image filename, create a ROM-able image by creating an option
219 ROM header and PCI data structure, filling them in, and then writing the
220 option ROM header + PCI data structure + EFI image out to the output file.
224 Argc - standard C main() argument count
226 Argv - standard C main() argument list
234 // GC_TODO: ] - add argument and description to function comment
243 Status
= STATUS_SUCCESS
;
247 // Parse the command line arguments
249 if (ParseCommandLine (Argc
, Argv
, &mOptions
)) {
253 // If dumping an image, then do that and quit
255 if (mOptions
.DumpOption
) {
256 DumpImage (mOptions
.FileList
);
260 // Determine the output filename. Either what they specified on
261 // the command line, or the first input filename with a different extension.
263 if (!mOptions
.OutFileName
[0]) {
264 strcpy (mOptions
.OutFileName
, mOptions
.FileList
->FileName
);
266 // Find the last . on the line and replace the filename extension with
269 for (Ext
= mOptions
.OutFileName
+ strlen (mOptions
.OutFileName
) - 1;
270 (Ext
>= mOptions
.OutFileName
) && (*Ext
!= '.') && (*Ext
!= '\\');
275 // If dot here, then insert extension here, otherwise append
278 Ext
= mOptions
.OutFileName
+ strlen (mOptions
.OutFileName
);
281 strcpy (Ext
, DEFAULT_OUTPUT_EXTENSION
);
284 // Make sure we don't have the same filename for input and output files
286 for (FList
= mOptions
.FileList
; FList
!= NULL
; FList
= FList
->Next
) {
287 if (_stricmp (mOptions
.OutFileName
, FList
->FileName
) == 0) {
288 Status
= STATUS_ERROR
;
291 "ERROR: Input and output file names must be different - %s = %s\n",
299 // Now open our output file
301 if ((FptrOut
= fopen (mOptions
.OutFileName
, "w+b")) == NULL
) {
302 fprintf (stdout
, "ERROR: Failed to open output file %s\n", mOptions
.OutFileName
);
306 // Process all our files
309 for (FList
= mOptions
.FileList
; FList
!= NULL
; FList
= FList
->Next
) {
311 if (FList
->FileFlags
& FILE_FLAG_EFI
) {
312 if (mOptions
.Verbose
) {
313 fprintf (stdout
, "Processing EFI file %s\n", FList
->FileName
);
316 Status
= ProcessEfiFile (FptrOut
, FList
, mOptions
.VendId
, mOptions
.DevId
, &Size
);
317 } else if (FList
->FileFlags
& FILE_FLAG_BINARY
) {
318 if (mOptions
.Verbose
) {
319 fprintf (stdout
, "Processing binary file %s\n", FList
->FileName
);
322 Status
= ProcessBinFile (FptrOut
, FList
, &Size
);
324 fprintf (stdout
, "ERROR: File not specified as EFI or binary: %s\n", FList
->FileName
);
325 Status
= STATUS_ERROR
;
328 if (mOptions
.Verbose
) {
329 fprintf (stdout
, " Output size = 0x%X\n", Size
);
332 if (Status
!= STATUS_SUCCESS
) {
341 if (TotalSize
> MAX_OPTION_ROM_SIZE
) {
344 "ERROR: Option ROM image size exceeds limit 0x%X bytes\n",
347 Status
= STATUS_ERROR
;
351 if (FptrOut
!= NULL
) {
355 // Clean up our file list
357 while (mOptions
.FileList
!= NULL
) {
358 FList
= mOptions
.FileList
->Next
;
359 free (mOptions
.FileList
);
360 mOptions
.FileList
= FList
;
374 Calculate checksum from DataSize of Buffer.
377 Buffer - pointer to data buffer
378 DataSize - size of data buffer in bytes
385 while (DataSize
-- != 0) {
386 Checksum
= Checksum
+ Buffer
[DataSize
];
388 while (PaddingSize
-- != 0) {
389 Checksum
= Checksum
+ 0xff;
399 if (CodeType
>= sizeof (mCodeTypeStr
) / sizeof (*mCodeTypeStr
)) {
400 CodeType
= sizeof (mCodeTypeStr
) / sizeof (*mCodeTypeStr
) - 1;
402 return mCodeTypeStr
[CodeType
];
416 Process a binary input file.
420 OutFptr - file pointer to output binary ROM image file we're creating
421 InFile - structure contains information on the binary file to process
422 Size - pointer to where to return the size added to the output file
437 PCI_EXPANSION_ROM_HEADER
*RomHdr
;
438 PCI_DATA_STRUCTURE
*PciDs
;
441 Status
= STATUS_SUCCESS
;
444 // Try to open the input file
446 if ((InFptr
= fopen (InFile
->FileName
, "rb")) == NULL
) {
447 fprintf (stdout
, "ERROR: Failed to open input file %s\n", InFile
->FileName
);
451 // Seek to the end of the input file and get the file size. Then allocate
452 // a buffer to read it in to.
454 fseek (InFptr
, 0, SEEK_END
);
455 FileSize
= ftell (InFptr
);
456 if (mOptions
.Verbose
) {
457 fprintf (stdout
, " File size = 0x%X\n", FileSize
);
460 fseek (InFptr
, 0, SEEK_SET
);
461 Buffer
= (INT8
*) malloc (FileSize
);
462 if (Buffer
== NULL
) {
463 fprintf (stdout
, "ERROR: Memory allocation failed\n");
464 Status
= STATUS_ERROR
;
468 if (fread (Buffer
, FileSize
, 1, InFptr
) != 1) {
469 fprintf (stdout
, "ERROR: Failed to read all bytes from input file\n");
470 Status
= STATUS_ERROR
;
475 RomHdr
= (PCI_EXPANSION_ROM_HEADER
*) Buffer
;
476 PciDs
= (PCI_DATA_STRUCTURE
*) (Buffer
+ RomHdr
->PcirOffset
);
479 // Crude check to make sure it's a legitimate ROM image
481 if (RomHdr
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
482 fprintf (stdout
, "ERROR: ROM image file has invalid ROM signature\n");
483 Status
= STATUS_ERROR
;
487 // Make sure the pointer to the PCI data structure is within the size of the image.
488 // Then check it for valid signature.
490 if ((RomHdr
->PcirOffset
> FileSize
) || (RomHdr
->PcirOffset
== 0)) {
491 fprintf (stdout
, "ERROR: Invalid PCI data structure offset\n");
492 Status
= STATUS_ERROR
;
496 if (PciDs
->Signature
!= PCI_DATA_STRUCTURE_SIGNATURE
) {
497 fprintf (stdout
, "ERROR: PCI data structure has invalid signature\n");
498 Status
= STATUS_ERROR
;
502 if ((UINT32
) (PciDs
->ImageLength
* 512) == FileSize
) {
504 // ImageLength reflects the actual file size correctly.
506 DataSize
= FileSize
- 1;
508 TotalSize
= FileSize
;
511 // ImageLength doesn't reflect the actual file size,
512 // 1). add additional 512 bytes if actual file size is multiple of 512
513 // 2). add additional X (X <= 512) bytes so that the result size is multiple of 512
515 fprintf (stdout
, "WARNING: ImageLength in PCI data structure != Actual File Size\n"
516 " --> add additional padding bytes\n"
517 " --> adjust ImageLength\n"
519 TotalSize
= (FileSize
+ 0x200) & ~0x1ff;
521 PaddingSize
= TotalSize
- DataSize
- 1;
522 PciDs
->ImageLength
= (UINT16
) (TotalSize
/ 512);
528 if (TotalSize
> MAX_OPTION_ROM_SIZE
) {
531 "ERROR: Option ROM image %s size exceeds limit 0x%X bytes\n",
535 Status
= STATUS_ERROR
;
540 // Return the size to the caller so they can keep track of the running total.
545 // If this is the last image, then set the LAST bit unless requested not
546 // to via the command-line -l argument. Otherwise, make sure you clear it.
548 if ((InFile
->Next
== NULL
) && (mOptions
.NoLast
== 0)) {
549 PciDs
->Indicator
|= INDICATOR_LAST
;
551 PciDs
->Indicator
&= ~INDICATOR_LAST
;
554 ByteCheckSum
= -CheckSum (Buffer
, DataSize
, PaddingSize
);
556 if (fwrite (Buffer
, DataSize
, 1, OutFptr
) != 1) {
557 fprintf (stdout
, "ERROR: Failed to write all file bytes to output file\n");
558 Status
= STATUS_ERROR
;
562 while (PaddingSize
-- != 0) {
565 putc (ByteCheckSum
, OutFptr
);
568 if (InFptr
!= NULL
) {
572 if (Buffer
!= NULL
) {
576 // Print the file name if errors occurred
578 if (Status
!= STATUS_SUCCESS
) {
579 fprintf (stdout
, "Error processing binary file %s\n", InFile
->FileName
);
598 Process a PE32 EFI file.
602 OutFptr - file pointer to output binary ROM image file we're creating
603 InFile - structure contains information on the PE32 file to process
604 VendId - vendor ID as required in the option ROM header
605 DevId - device ID as required in the option ROM header
606 Size - pointer to where to return the size added to the output file
616 EFI_PCI_EXPANSION_ROM_HEADER RomHdr
;
617 PCI_DATA_STRUCTURE PciDs
;
619 UINT32 CompressedFileSize
;
621 UINT8
*CompressedBuffer
;
622 UINT8
*TempBufferPtr
;
627 UINT32 HeaderPadBytes
;
630 // Try to open the input file
632 if ((InFptr
= fopen (InFile
->FileName
, "rb")) == NULL
) {
633 fprintf (stdout
, "ERROR: Failed to open input file %s\n", InFile
->FileName
);
637 // Initialize our buffer pointers to null.
640 CompressedBuffer
= NULL
;
643 // Double-check the file to make sure it's what we expect it to be
645 Status
= CheckPE32File (InFptr
, &MachineType
, &SubSystem
);
646 if (Status
!= STATUS_SUCCESS
) {
650 // Seek to the end of the input file and get the file size
652 fseek (InFptr
, 0, SEEK_END
);
653 FileSize
= ftell (InFptr
);
656 // Get the size of the headers we're going to put in front of the image. The
657 // EFI header must be aligned on a 4-byte boundary, so pad accordingly.
659 if (sizeof (RomHdr
) & 0x03) {
660 HeaderPadBytes
= 4 - (sizeof (RomHdr
) & 0x03);
665 HeaderSize
= sizeof (PCI_DATA_STRUCTURE
) + HeaderPadBytes
+ sizeof (EFI_PCI_EXPANSION_ROM_HEADER
);
666 if (mOptions
.Verbose
) {
667 fprintf (stdout
, " File size = 0x%X\n", FileSize
);
670 // Allocate memory for the entire file (in case we have to compress), then
671 // seek back to the beginning of the file and read it into our buffer.
673 Buffer
= (INT8
*) malloc (FileSize
);
674 if (Buffer
== NULL
) {
675 fprintf (stdout
, "ERROR: Memory allocation failed\n");
676 Status
= STATUS_ERROR
;
680 fseek (InFptr
, 0, SEEK_SET
);
681 if (fread (Buffer
, FileSize
, 1, InFptr
) != 1) {
682 fprintf (stdout
, "ERROR: Failed to read all bytes from input file\n");
683 Status
= STATUS_ERROR
;
687 // Now determine the size of the final output file. It's either the header size
688 // plus the file's size, or the header size plus the compressed file size.
690 if (InFile
->FileFlags
& FILE_FLAG_COMPRESS
) {
692 // Allocate a buffer into which we can compress the image, compress it,
693 // and use that size as the new size.
695 CompressedBuffer
= (INT8
*) malloc (FileSize
);
696 if (CompressedBuffer
== NULL
) {
697 fprintf (stdout
, "ERROR: Memory allocation failed\n");
698 Status
= STATUS_ERROR
;
702 CompressedFileSize
= FileSize
;
703 Status
= EfiCompress (Buffer
, FileSize
, CompressedBuffer
, &CompressedFileSize
);
704 if (Status
!= STATUS_SUCCESS
) {
705 fprintf (stdout
, "ERROR: Compression failed\n");
709 // Now compute the size, then swap buffer pointers.
711 if (mOptions
.Verbose
) {
712 fprintf (stdout
, " Comp size = 0x%X\n", CompressedFileSize
);
715 TotalSize
= CompressedFileSize
+ HeaderSize
;
716 FileSize
= CompressedFileSize
;
717 TempBufferPtr
= Buffer
;
718 Buffer
= CompressedBuffer
;
719 CompressedBuffer
= TempBufferPtr
;
721 TotalSize
= FileSize
+ HeaderSize
;
724 // Total size must be an even multiple of 512 bytes
726 TotalSize
= (TotalSize
+ 0x1ff) & ~0x1ff;
731 if (TotalSize
> MAX_OPTION_ROM_SIZE
) {
734 "ERROR: Option ROM image %s size exceeds limit 0x%X bytes\n",
738 Status
= STATUS_ERROR
;
742 // Return the size to the caller so they can keep track of the running total.
747 // Now fill in the ROM header. These values come from chapter 18 of the
748 // EFI 1.02 specification.
750 memset (&RomHdr
, 0, sizeof (RomHdr
));
751 RomHdr
.Signature
= PCI_EXPANSION_ROM_HEADER_SIGNATURE
;
752 RomHdr
.InitializationSize
= (UINT16
) (TotalSize
/ 512);
753 RomHdr
.EfiSignature
= EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
;
754 RomHdr
.EfiSubsystem
= SubSystem
;
755 RomHdr
.EfiMachineType
= MachineType
;
756 RomHdr
.EfiImageHeaderOffset
= (UINT16
) HeaderSize
;
757 RomHdr
.PcirOffset
= (UINT16
) (sizeof (RomHdr
) + HeaderPadBytes
);
759 // Set image as compressed or not
761 if (InFile
->FileFlags
& FILE_FLAG_COMPRESS
) {
762 RomHdr
.CompressionType
= EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
;
765 // Fill in the PCI data structure
767 memset (&PciDs
, 0, sizeof (PCI_DATA_STRUCTURE
));
769 PciDs
.Signature
= PCI_DATA_STRUCTURE_SIGNATURE
;
770 PciDs
.VendorId
= VendId
;
771 PciDs
.DeviceId
= DevId
;
772 PciDs
.Length
= (UINT16
) sizeof (PCI_DATA_STRUCTURE
);
775 // Class code and code revision from the command line (optional)
777 PciDs
.ClassCode
[0] = (UINT8
) InFile
->ClassCode
;
778 PciDs
.ClassCode
[1] = (UINT8
) (InFile
->ClassCode
>> 8);
779 PciDs
.ClassCode
[2] = (UINT8
) (InFile
->ClassCode
>> 16);
780 PciDs
.ImageLength
= RomHdr
.InitializationSize
;
781 PciDs
.CodeRevision
= InFile
->CodeRevision
;
782 PciDs
.CodeType
= PCI_CODE_TYPE_EFI_IMAGE
;
785 // If this is the last image, then set the LAST bit unless requested not
786 // to via the command-line -l argument.
788 if ((InFile
->Next
== NULL
) && (mOptions
.NoLast
== 0)) {
789 PciDs
.Indicator
|= INDICATOR_LAST
;
792 // Write the ROM header to the output file
794 if (fwrite (&RomHdr
, sizeof (RomHdr
), 1, OutFptr
) != 1) {
795 fprintf (stdout
, "ERROR: Failed to write ROM header to output file\n");
796 Status
= STATUS_ERROR
;
801 // Write pad bytes to align the PciDs
803 while (HeaderPadBytes
> 0) {
804 if (putc (0, OutFptr
) == EOF
) {
805 fprintf (stdout
, "ERROR: Failed to write ROM header pad bytes to output file\n");
806 Status
= STATUS_ERROR
;
813 // Write the PCI data structure header to the output file
815 if (fwrite (&PciDs
, sizeof (PciDs
), 1, OutFptr
) != 1) {
816 fprintf (stdout
, "ERROR: Failed to write PCI ROM header to output file\n");
817 Status
= STATUS_ERROR
;
821 // Keep track of how many bytes left to write
823 TotalSize
-= HeaderSize
;
826 // Now dump the input file's contents to the output file
828 if (fwrite (Buffer
, FileSize
, 1, OutFptr
) != 1) {
829 fprintf (stdout
, "ERROR: Failed to write all file bytes to output file\n");
830 Status
= STATUS_ERROR
;
834 TotalSize
-= FileSize
;
836 // Pad the rest of the image to make it a multiple of 512 bytes
838 while (TotalSize
> 0) {
839 if (putc (~0, OutFptr
) == EOF
) {
840 fprintf (stdout
, "ERROR: Failed to write trailing pad bytes output file\n");
841 Status
= STATUS_ERROR
;
849 if (InFptr
!= NULL
) {
854 // Free up our buffers
856 if (Buffer
!= NULL
) {
860 if (CompressedBuffer
!= NULL
) {
861 free (CompressedBuffer
);
864 // Print the file name if errors occurred
866 if (Status
!= STATUS_SUCCESS
) {
867 fprintf (stdout
, "Error processing EFI file %s\n", InFile
->FileName
);
884 GC_TODO: Add function description
888 Fptr - GC_TODO: add argument description
889 MachineType - GC_TODO: add argument description
890 SubSystem - GC_TODO: add argument description
894 GC_TODO: add return values
902 Given a file pointer to a supposed PE32 image file, verify that it is indeed a
903 PE32 image file, and then return the machine type in the supplied pointer.
907 Fptr File pointer to the already-opened PE32 file
908 MachineType Location to stuff the machine type of the PE32 file. This is needed
909 because the image may be Itanium-based, IA32, or EBC.
917 EFI_IMAGE_DOS_HEADER DosHeader
;
918 EFI_IMAGE_FILE_HEADER FileHdr
;
919 EFI_IMAGE_OPTIONAL_HEADER OptionalHdr
;
923 // Position to the start of the file
925 fseek (Fptr
, 0, SEEK_SET
);
928 // Read the DOS header
930 if (fread (&DosHeader
, sizeof (DosHeader
), 1, Fptr
) != 1) {
931 fprintf (stdout
, "ERROR: Failed to read the DOS stub from the input file\n");
935 // Check the magic number (0x5A4D)
937 if (DosHeader
.e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
938 fprintf (stdout
, "ERROR: Input file does not appear to be a PE32 image (magic number)\n");
942 // Position into the file and check the PE signature
944 fseek (Fptr
, (long) DosHeader
.e_lfanew
, SEEK_SET
);
945 if (fread (&PESig
, sizeof (PESig
), 1, Fptr
) != 1) {
946 fprintf (stdout
, "ERROR: Failed to read PE signature bytes from input file\n");
950 // Check the PE signature in the header "PE\0\0"
952 if (PESig
!= EFI_IMAGE_NT_SIGNATURE
) {
953 fprintf (stdout
, "ERROR: Input file does not appear to be a PE32 image (signature)\n");
957 // Read the file header and stuff their MachineType
959 if (fread (&FileHdr
, sizeof (FileHdr
), 1, Fptr
) != 1) {
960 fprintf (stdout
, "ERROR: Failed to read PE file header from input file\n");
964 memcpy ((char *) MachineType
, &FileHdr
.Machine
, 2);
967 // Read the optional header so we can get the subsystem
969 if (fread (&OptionalHdr
, sizeof (OptionalHdr
), 1, Fptr
) != 1) {
970 fprintf (stdout
, "ERROR: Failed to read COFF optional header from input file\n");
974 *SubSystem
= OptionalHdr
.Subsystem
;
975 if (mOptions
.Verbose
) {
976 fprintf (stdout
, " Got subsystem = 0x%X from image\n", (int) *SubSystem
);
981 return STATUS_SUCCESS
;
995 Given the Argc/Argv program arguments, and a pointer to an options structure,
996 parse the command-line options and check their validity.
1001 Argc - standard C main() argument count
1002 Argv[] - standard C main() argument list
1003 Options - pointer to a structure to store the options in
1007 STATUS_SUCCESS success
1013 FILE_LIST
*FileList
;
1015 FILE_LIST
*PrevFileList
;
1018 UINT32 CodeRevision
;
1023 // Clear out the options
1025 memset ((char *) Options
, 0, sizeof (OPTIONS
));
1028 // To avoid compile warnings
1030 FileList
= PrevFileList
= NULL
;
1035 // Skip over the program name
1041 // If no arguments, assume they want usage info
1045 return STATUS_ERROR
;
1048 // Process until no more arguments
1051 if ((Argv
[0][0] == '-') || (Argv
[0][0] == '/')) {
1053 // To simplify string comparisons, replace slashes with dashes
1058 // Vendor ID specified with -v
1060 if (_stricmp (Argv
[0], "-v") == 0) {
1062 // Make sure there's another parameter
1065 Options
->VendId
= (UINT16
) strtol (Argv
[1], NULL
, 16);
1066 Options
->VendIdValid
= 1;
1070 "ERROR: Missing Vendor ID with %s\n\n",
1074 return STATUS_ERROR
;
1079 } else if (_stricmp (Argv
[0], "-d") == 0) {
1081 // Device ID specified with -d
1082 // Make sure there's another parameter
1085 Options
->DevId
= (UINT16
) strtol (Argv
[1], NULL
, 16);
1086 Options
->DevIdValid
= 1;
1090 "ERROR: Missing Device ID with %s\n\n",
1094 return STATUS_ERROR
;
1099 } else if (_stricmp (Argv
[0], "-o") == 0) {
1101 // Output filename specified with -o
1102 // Make sure there's another parameter
1105 strcpy (Options
->OutFileName
, Argv
[1]);
1109 "ERROR: Missing output file name with %s\n\n",
1113 return STATUS_ERROR
;
1118 } else if ((_stricmp (Argv
[0], "-h") == 0) || (strcmp (Argv
[0], "-?") == 0)) {
1123 return STATUS_ERROR
;
1124 } else if (_stricmp (Argv
[0], "-b") == 0) {
1126 // Specify binary files with -b
1128 FileFlags
= (FileFlags
& ~FILE_FLAG_EFI
) | FILE_FLAG_BINARY
;
1129 } else if ((_stricmp (Argv
[0], "-e") == 0) || (_stricmp (Argv
[0], "-ec") == 0)) {
1131 // Specify EFI files with -e. Specify EFI-compressed with -ec.
1133 FileFlags
= (FileFlags
&~FILE_FLAG_BINARY
) | FILE_FLAG_EFI
;
1134 if ((Argv
[0][2] == 'c') || (Argv
[0][2] == 'C')) {
1135 FileFlags
|= FILE_FLAG_COMPRESS
;
1138 // Specify not to set the LAST bit in the last file with -l
1140 } else if (_stricmp (Argv
[0], "-l") == 0) {
1141 Options
->NoLast
= 1;
1142 } else if (_stricmp (Argv
[0], "-p") == 0) {
1144 // -v for verbose would have been nicer, but it's already used. Let's use
1145 // -p for prolix (wordy) output
1147 Options
->Verbose
= 1;
1148 } else if (_stricmp (Argv
[0], "-dump") == 0) {
1150 // -dump for dumping a ROM image. In this case, say that the device id
1151 // and vendor id are valid so we don't have to specify bogus ones on the
1154 Options
->DumpOption
= 1;
1156 Options
->VendIdValid
= 1;
1157 Options
->DevIdValid
= 1;
1158 FileFlags
= FILE_FLAG_BINARY
;
1159 } else if (_stricmp (Argv
[0], "-cc") == 0) {
1161 // Class code value for the next file in the list.
1162 // Make sure there's another parameter
1166 // No error checking on the return value. Could check for LONG_MAX,
1167 // LONG_MIN, or 0 class code value if desired. Check range (3 bytes)
1170 ClassCode
= (UINT32
) strtol (Argv
[1], NULL
, 16);
1171 if (ClassCode
& 0xFF000000) {
1172 fprintf (stdout
, "ERROR: Class code %s out of range\n", Argv
[1]);
1173 return STATUS_ERROR
;
1178 "ERROR: Missing class code value with %s\n\n",
1182 return STATUS_ERROR
;
1187 } else if (_stricmp (Argv
[0], "-rev") == 0) {
1189 // Code revision in the PCI data structure. The value is for the next
1190 // file in the list.
1191 // Make sure there's another parameter
1195 // No error checking on the return value. Could check for LONG_MAX,
1196 // LONG_MIN, or 0 value if desired. Check range (2 bytes)
1199 CodeRevision
= (UINT32
) strtol (Argv
[1], NULL
, 16);
1200 if (CodeRevision
& 0xFFFF0000) {
1201 fprintf (stdout
, "ERROR: Code revision %s out of range\n", Argv
[1]);
1202 return STATUS_ERROR
;
1207 "ERROR: Missing code revision value with %s\n\n",
1211 return STATUS_ERROR
;
1217 fprintf (stdout
, "ERROR: Invalid option specified: %s\n\n", Argv
[0]);
1219 return STATUS_ERROR
;
1223 // Not a slash-option argument. Must be a file name. Make sure they've specified
1224 // -e or -b already.
1226 if ((FileFlags
& (FILE_FLAG_BINARY
| FILE_FLAG_EFI
)) == 0) {
1227 fprintf (stdout
, "ERROR: Missing -e or -b with input file %s\n", Argv
[0]);
1228 return STATUS_ERROR
;
1231 // Create a new file structure
1233 FileList
= (FILE_LIST
*) malloc (sizeof (FILE_LIST
));
1234 if (FileList
== NULL
) {
1235 fprintf (stdout
, "ERROR: Memory allocation failure\n");
1236 return STATUS_ERROR
;
1239 memset ((char *) FileList
, 0, sizeof (FILE_LIST
));
1240 FileList
->FileName
= Argv
[0];
1241 FileList
->FileFlags
= FileFlags
;
1242 if (Options
->FileList
== NULL
) {
1243 Options
->FileList
= FileList
;
1245 if (PrevFileList
== NULL
) {
1246 PrevFileList
= FileList
;
1248 PrevFileList
->Next
= FileList
;
1252 PrevFileList
= FileList
;
1254 // Set the class code and code revision for this file, then reset the values.
1256 FileList
->ClassCode
= ClassCode
;
1257 FileList
->CodeRevision
= (UINT16
) CodeRevision
;
1268 // Make sure they specified a device ID and vendor ID
1270 if (!Options
->VendIdValid
) {
1271 fprintf (stdout
, "ERROR: Missing Vendor ID on command line\n\n");
1273 return STATUS_ERROR
;
1276 if (!Options
->DevIdValid
) {
1277 fprintf (stdout
, "ERROR: Missing Device ID on command line\n\n");
1279 return STATUS_ERROR
;
1282 // Must have specified some files
1284 if (Options
->FileList
== NULL
) {
1285 fprintf (stdout
, "ERROR: Missing input file name\n");
1287 return STATUS_ERROR
;
1300 Routine Description:
1302 Print usage information for this utility.
1315 static const char *Msg
[] = {
1316 "EfiRom "UTILITY_VERSION
" - Intel EFI Make Option ROM utility",
1317 " Copyright (C), 1999 - 2002 Intel Coproration\n",
1318 " Create an option ROM image from a list of input files",
1319 " Usage: efirom {-p} [-v VendorId] [-d DeviceId] {-o OutFileName} ",
1320 " [-e|-b] [FileName(s)]",
1322 " VendorId - required hex PCI Vendor ID for the device",
1323 " DeviceId - required hex PCI Device ID for the device",
1324 " OutFileName - optional output file name. Default is the first input",
1325 " file name with a "DEFAULT_OUTPUT_EXTENSION
" file extension",
1326 " FileNames - input PE32 or binary file name(s)",
1327 " BinFileName - input binary file name(s)",
1328 " -p - for verbose output",
1329 " -l - to not automatically set the LAST bit on the last file",
1330 " -b - following FileNames are binary files",
1331 " -e - following FileNames are EFI PE32 image files",
1332 " -ec - following FileNames are EFI PE32 image files, and should",
1333 " be compressed by this utility",
1334 " -cc ClassCode - to use hex ClassCode in the PCI data structure header for",
1335 " the following FileName",
1336 " -rev Revision - to use hex Revision in the PCI data structure header for",
1337 " the following FileName",
1338 " -dump - to dump the headers of an existing option ROM image",
1340 "Example usage: EfiRom -v 0xABCD -d 0x1234 -b File1.bin File2.bin -e File1.efi File2.efi",
1345 for (Index
= 0; Msg
[Index
] != NULL
; Index
++) {
1346 fprintf (stdout
, "%s\n", Msg
[Index
]);
1357 Routine Description:
1359 GC_TODO: Add function description
1363 InFile - GC_TODO: add argument description
1367 GC_TODO: add return values
1371 PCI_EXPANSION_ROM_HEADER PciRomHdr
;
1376 EFI_PCI_EXPANSION_ROM_HEADER EfiRomHdr
;
1377 PCI_3_0_DATA_STRUCTURE PciDs
;
1380 // Open the input file
1382 if ((InFptr
= fopen (InFile
->FileName
, "rb")) == NULL
) {
1385 "ERROR: Could not open input file %s\n",
1391 // Go through the image and dump the header stuff for each
1396 // Save our postition in the file, since offsets in the headers
1397 // are relative to the particular image.
1399 ImageStart
= ftell (InFptr
);
1403 // Read the option ROM header. Have to assume a raw binary image for now.
1405 if (fread (&PciRomHdr
, sizeof (PciRomHdr
), 1, InFptr
) != 1) {
1406 fprintf (stdout
, "ERROR: Failed to read PCI ROM header from file\n");
1411 // Dump the contents of the header
1413 fprintf (stdout
, "Image %d -- Offset 0x%X\n", ImageCount
, ImageStart
);
1414 fprintf (stdout
, " ROM header contents\n");
1415 fprintf (stdout
, " Signature 0x%04X\n", (UINT32
) PciRomHdr
.Signature
);
1416 fprintf (stdout
, " PCIR offset 0x%04X\n", (UINT32
) PciRomHdr
.PcirOffset
);
1418 // Find PCI data structure
1420 if (fseek (InFptr
, ImageStart
+ PciRomHdr
.PcirOffset
, SEEK_SET
)) {
1421 fprintf (stdout
, "ERROR: Failed to seek to PCI data structure\n");
1425 // Read and dump the PCI data structure
1427 if (fread (&PciDs
, sizeof (PciDs
), 1, InFptr
) != 1) {
1428 fprintf (stdout
, "ERROR: Failed to read PCI data structure from file\n");
1432 fprintf (stdout
, " PCI Data Structure\n");
1435 " Signature %c%c%c%c\n",
1436 (char) PciDs
.Signature
,
1437 (char) (PciDs
.Signature
>> 8),
1438 (char) (PciDs
.Signature
>> 16),
1439 (char) (PciDs
.Signature
>> 24)
1441 fprintf (stdout
, " Vendor ID 0x%04X\n", PciDs
.VendorId
);
1442 fprintf (stdout
, " Device ID 0x%04X\n", PciDs
.DeviceId
);
1443 fprintf (stdout
, " Structure Length 0x%04X\n", PciDs
.Length
);
1444 fprintf (stdout
, " PCI Revision 0x%02X\n", PciDs
.Revision
);
1447 " Class Code 0x%06X\n",
1448 (UINT32
) (PciDs
.ClassCode
[0] | (PciDs
.ClassCode
[1] << 8) | (PciDs
.ClassCode
[2] << 16))
1450 fprintf (stdout
, " Image size 0x%X\n", PciDs
.ImageLength
* 512);
1451 fprintf (stdout
, " Code revision 0x%04X\n", PciDs
.CodeRevision
);
1452 fprintf (stdout
, " Indicator 0x%02X", (UINT32
) PciDs
.Indicator
);
1454 // Print the indicator, used to flag the last image
1456 if ((PciDs
.Indicator
& INDICATOR_LAST
) == INDICATOR_LAST
) {
1457 fprintf (stdout
, " (last image)\n");
1459 fprintf (stdout
, "\n");
1463 // Print the code type. If EFI code, then we can provide more info.
1465 fprintf (stdout
, " Code type 0x%02X (%s)\n", (UINT32
) PciDs
.CodeType
, GetCodeTypeStr (PciDs
.CodeType
));
1466 if (PciDs
.CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) {
1468 // Re-read the header as an EFI ROM header, then dump more info
1470 fprintf (stdout
, " EFI ROM header contents\n");
1471 memcpy (&EfiRomHdr
, &PciRomHdr
, sizeof (EfiRomHdr
));
1473 // Now dump more info
1475 fprintf (stdout
, " EFI Signature 0x%04X\n", EfiRomHdr
.EfiSignature
);
1478 " Compression Type 0x%04X ",
1479 (UINT32
) EfiRomHdr
.CompressionType
1481 if (EfiRomHdr
.CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
1482 fprintf (stdout
, "(compressed)\n");
1484 fprintf (stdout
, "(not compressed)\n");
1489 " Machine type 0x%04X (%s)\n",
1490 EfiRomHdr
.EfiMachineType
,
1491 GetMachineTypeStr (EfiRomHdr
.EfiMachineType
)
1495 " Subsystem 0x%04X (%s)\n",
1496 EfiRomHdr
.EfiSubsystem
,
1497 GetSubsystemTypeStr (EfiRomHdr
.EfiSubsystem
)
1501 " EFI image offset 0x%04X (@0x%X)\n",
1502 (UINT32
) EfiRomHdr
.EfiImageHeaderOffset
,
1503 (UINT32
) (EfiRomHdr
.EfiImageHeaderOffset
+ ImageStart
)
1508 // Dump additional information for PCI 3.0 OpROM
1510 if (PciDs
.Revision
>= 3) {
1511 fprintf (stdout
, " Extended for PCI 3.0\n");
1513 if (PciDs
.DeviceListOffset
!= 0) {
1514 if (fseek (InFptr
, ImageStart
+ PciRomHdr
.PcirOffset
+ PciDs
.DeviceListOffset
, SEEK_SET
)) {
1515 fprintf (stdout
, "ERROR: Failed to seek to supported Device List\n");
1518 fprintf (stdout
, " Device ID List ");
1520 if (fread (&DeviceId
, sizeof (DeviceId
), 1, InFptr
) != 1) {
1521 fprintf (stdout
, "ERROR: Failed to read supported DeviceId from DeviceId List\n");
1524 if (DeviceId
== 0) {
1527 fprintf (stdout
, "0x%04X ", DeviceId
);
1529 fprintf (stdout
, "\n");
1531 fprintf (stdout
, " Max Runtime Image Length 0x%08X\n", PciDs
.MaxRuntimeImageLength
* 512);
1532 if (PciDs
.Length
== sizeof (PCI_3_0_DATA_STRUCTURE
)) {
1533 fprintf (stdout
, " Config Utility Header 0x%04X\n", PciDs
.ConfigUtilityCodeHeaderOffset
);
1534 fprintf (stdout
, " DMTF CLP Entry Point 0x%04X\n", PciDs
.DMTFCLPEntryPointOffset
);
1536 fprintf (stdout
, "WARNING: Oprom declars 3.0 revision with wrong structure length 0x%04X\n", PciDs
.Length
);
1541 // If last image, then we're done
1543 if ((PciDs
.Indicator
& INDICATOR_LAST
) == INDICATOR_LAST
) {
1547 // Seek to the start of the next image
1549 if (fseek (InFptr
, ImageStart
+ (PciDs
.ImageLength
* 512), SEEK_SET
)) {
1550 fprintf (stdout
, "ERROR: Failed to seek to next image\n");
1565 Routine Description:
1567 GC_TODO: Add function description
1571 MachineType - GC_TODO: add argument description
1575 GC_TODO: add return values
1581 for (Index
= 0; mMachineTypes
[Index
].Name
!= NULL
; Index
++) {
1582 if (mMachineTypes
[Index
].Value
== MachineType
) {
1583 return mMachineTypes
[Index
].Name
;
1592 GetSubsystemTypeStr (
1593 UINT16 SubsystemType
1597 Routine Description:
1599 GC_TODO: Add function description
1603 SubsystemType - GC_TODO: add argument description
1607 GC_TODO: add return values
1613 for (Index
= 0; mSubsystemTypes
[Index
].Name
!= NULL
; Index
++) {
1614 if (mSubsystemTypes
[Index
].Value
== SubsystemType
) {
1615 return mSubsystemTypes
[Index
].Name
;