3 Copyright (c) 1999-2008 Intel Corporation. All rights reserved
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 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.
18 Utility program to create an EFI option ROM image from binary and
23 #include "EfiUtilityMsgs.h"
27 UINT64 DebugLevel
= 0;
38 Given an EFI image filename, create a ROM-able image by creating an option
39 ROM header and PCI data structure, filling them in, and then writing the
40 option ROM header + PCI data structure + EFI image out to the output file.
44 Argc - standard C main() argument count
46 Argv - standard C main() argument list
63 SetUtilityName(UTILITY_NAME
);
65 Status
= STATUS_SUCCESS
;
69 // Parse the command line arguments
71 if (ParseCommandLine (Argc
, Argv
, &mOptions
)) {
77 } else if (mOptions
.Verbose
) {
79 } else if (mOptions
.Debug
) {
80 SetPrintLevel(DebugLevel
);
83 if (mOptions
.Verbose
) {
84 VerboseMsg("%s tool start.\n", UTILITY_NAME
);
88 // If dumping an image, then do that and quit
90 if (mOptions
.DumpOption
== 1) {
91 for (FList
= mOptions
.FileList
; FList
!= NULL
; FList
= FList
->Next
) {
92 if ((Ptr0
= strstr ((CONST CHAR8
*)FList
->FileName
, DEFAULT_OUTPUT_EXTENSION
)) != NULL
) {
93 DumpImage (mOptions
.FileList
);
96 Error (NULL
, 0, 1002, "No PciRom input file", "No *.rom input file");
102 // Determine the output filename. Either what they specified on
103 // the command line, or the first input filename with a different extension.
105 if (!mOptions
.OutFileName
[0]) {
106 strcpy (mOptions
.OutFileName
, mOptions
.FileList
->FileName
);
108 // Find the last . on the line and replace the filename extension with
111 for (Ext
= mOptions
.OutFileName
+ strlen (mOptions
.OutFileName
) - 1;
112 (Ext
>= mOptions
.OutFileName
) && (*Ext
!= '.') && (*Ext
!= '\\');
117 // If dot here, then insert extension here, otherwise append
120 Ext
= mOptions
.OutFileName
+ strlen (mOptions
.OutFileName
);
123 strcpy (Ext
, DEFAULT_OUTPUT_EXTENSION
);
126 // Make sure we don't have the same filename for input and output files
128 for (FList
= mOptions
.FileList
; FList
!= NULL
; FList
= FList
->Next
) {
129 if (stricmp (mOptions
.OutFileName
, FList
->FileName
) == 0) {
130 Status
= STATUS_ERROR
;
131 Error (NULL
, 0, 1002, "Invalid input paramter", "Input and output file names must be different - %s = %s.", FList
->FileName
, mOptions
.OutFileName
);
136 // Now open our output file
138 if ((FptrOut
= fopen (mOptions
.OutFileName
, "wb")) == NULL
) {
139 Error (NULL
, 0, 0001, "Error opening file", "Error opening file %s", mOptions
.OutFileName
);
143 // Process all our files
146 for (FList
= mOptions
.FileList
; FList
!= NULL
; FList
= FList
->Next
) {
148 if ((FList
->FileFlags
& FILE_FLAG_EFI
) != 0) {
149 if (mOptions
.Verbose
) {
150 VerboseMsg("Processing EFI file %s\n", FList
->FileName
);
153 Status
= ProcessEfiFile (FptrOut
, FList
, mOptions
.VendId
, mOptions
.DevId
, &Size
);
154 } else if ((FList
->FileFlags
& FILE_FLAG_BINARY
) !=0 ) {
155 if (mOptions
.Verbose
) {
156 VerboseMsg("Processing binary file %s\n", FList
->FileName
);
159 Status
= ProcessBinFile (FptrOut
, FList
, &Size
);
161 Error (NULL
, 0, 2000, "Invalid parameter", "File type not specified, it must be either an EFI or binary file: %s.", FList
->FileName
);
162 Status
= STATUS_ERROR
;
165 if (mOptions
.Verbose
) {
166 VerboseMsg(" Output size = 0x%X\n", Size
);
169 if (Status
!= STATUS_SUCCESS
) {
178 if (TotalSize
> MAX_OPTION_ROM_SIZE
) {
179 Error (NULL
, 0, 2000, "Invalid paramter", "Option ROM image size exceeds limit of 0x%X bytes.", MAX_OPTION_ROM_SIZE
);
180 Status
= STATUS_ERROR
;
184 if (Status
== STATUS_SUCCESS
) {
185 if (FptrOut
!= NULL
) {
189 // Clean up our file list
191 while (mOptions
.FileList
!= NULL
) {
192 FList
= mOptions
.FileList
->Next
;
193 free (mOptions
.FileList
);
194 mOptions
.FileList
= FList
;
198 if (mOptions
.Verbose
) {
199 VerboseMsg("%s tool done with return code is 0x%x.\n", UTILITY_NAME
, GetUtilityStatus ());
202 return GetUtilityStatus ();
216 Process a binary input file.
220 OutFptr - file pointer to output binary ROM image file we're creating
221 InFile - structure contains information on the binary file to process
222 Size - pointer to where to return the size added to the output file
235 PCI_EXPANSION_ROM_HEADER
*RomHdr
;
236 PCI_DATA_STRUCTURE
*PciDs23
;
237 PCI_3_0_DATA_STRUCTURE
*PciDs30
;
242 Status
= STATUS_SUCCESS
;
245 // Try to open the input file
247 if ((InFptr
= fopen (InFile
->FileName
, "rb")) == NULL
) {
248 Error (NULL
, 0, 0001, "Error opening file", InFile
->FileName
);
252 // Seek to the end of the input file and get the file size. Then allocate
253 // a buffer to read it in to.
255 fseek (InFptr
, 0, SEEK_END
);
256 FileSize
= ftell (InFptr
);
257 if (mOptions
.Verbose
) {
258 VerboseMsg(" File size = 0x%X\n", FileSize
);
261 fseek (InFptr
, 0, SEEK_SET
);
262 Buffer
= (UINT8
*) malloc (FileSize
);
263 if (Buffer
== NULL
) {
264 Error (NULL
, 0, 4003, "Resource", "memory cannot be allocated!");
265 Status
= STATUS_ERROR
;
269 if (fread (Buffer
, FileSize
, 1, InFptr
) != 1) {
270 Error (NULL
, 0, 2000, "Invalid", "Failed to read all bytes from input file.");
271 Status
= STATUS_ERROR
;
275 // Total size must be an even multiple of 512 bytes, and can't exceed
276 // the option ROM image size.
278 TotalSize
= FileSize
;
279 if (TotalSize
& 0x1FF) {
280 TotalSize
= (TotalSize
+ 0x200) &~0x1ff;
283 if (TotalSize
> MAX_OPTION_ROM_SIZE
) {
284 Error (NULL
, 0, 3001, "Invalid", "Option ROM image %s size exceeds limit of 0x%X bytes.", InFile
->FileName
, MAX_OPTION_ROM_SIZE
);
285 Status
= STATUS_ERROR
;
289 // Return the size to the caller so they can keep track of the running total.
294 // Crude check to make sure it's a legitimate ROM image
296 RomHdr
= (PCI_EXPANSION_ROM_HEADER
*) Buffer
;
297 if (RomHdr
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
298 Error (NULL
, 0, 2000, "Invalid parameter", "ROM image file has an invalid ROM signature.");
299 Status
= STATUS_ERROR
;
303 // Make sure the pointer to the PCI data structure is within the size of the image.
304 // Then check it for valid signature.
306 if ((RomHdr
->PcirOffset
> FileSize
) || (RomHdr
->PcirOffset
== 0)) {
307 Error (NULL
, 0, 2000, "Invalid parameter", "Invalid PCI data structure offset.");
308 Status
= STATUS_ERROR
;
313 // Check the header is conform to PCI2.3 or PCI3.0
315 if (mOptions
.Pci23
== 1) {
316 PciDs23
= (PCI_DATA_STRUCTURE
*) (Buffer
+ RomHdr
->PcirOffset
);
317 if (PciDs23
->Signature
!= PCI_DATA_STRUCTURE_SIGNATURE
) {
318 Error (NULL
, 0, 2000, "Invalid parameter", "PCI data structure has an invalid signature.");
319 Status
= STATUS_ERROR
;
324 // Default setting is PCI3.0 header
326 PciDs30
= (PCI_3_0_DATA_STRUCTURE
*)(Buffer
+ RomHdr
->PcirOffset
);
327 if (PciDs30
->Signature
!= PCI_DATA_STRUCTURE_SIGNATURE
) {
328 Error (NULL
, 0, 2000, "Invalid parameter", "PCI data structure has an invalid signature.");
329 Status
= STATUS_ERROR
;
335 // ReSet Option Rom size
337 if (mOptions
.Pci23
== 1) {
338 PciDs23
->ImageLength
= (UINT16
) (TotalSize
/ 512);
340 PciDs30
->ImageLength
= (UINT16
) (TotalSize
/ 512);
344 // If this is the last image, then set the LAST bit unless requested not
345 // to via the command-line -n argument. Otherwise, make sure you clear it.
347 if ((InFile
->Next
== NULL
) && (mOptions
.NoLast
== 0)) {
348 if (mOptions
.Pci23
== 1) {
349 PciDs23
->Indicator
= INDICATOR_LAST
;
351 PciDs30
->Indicator
= INDICATOR_LAST
;
354 if (mOptions
.Pci23
== 1) {
355 PciDs23
->Indicator
= 0;
357 PciDs30
->Indicator
= 0;
362 for (Index
= 0; Index
< FileSize
- 1; Index
++) {
363 ByteCheckSum
= (UINT8
) (ByteCheckSum
+ Buffer
[Index
]);
366 Buffer
[FileSize
- 1] = (UINT8
) ((~ByteCheckSum
) + 1);
367 if (mOptions
.Verbose
) {
368 VerboseMsg(" Checksum = %02x\n\n", (UINT32
) Buffer
[FileSize
- 1]);
372 // Now copy the input file contents out to the output file
374 if (fwrite (Buffer
, FileSize
, 1, OutFptr
) != 1) {
375 Error (NULL
, 0, 0005, "Failed to write all file bytes to output file.", NULL
);
376 Status
= STATUS_ERROR
;
380 TotalSize
-= FileSize
;
382 // Pad the rest of the image to make it a multiple of 512 bytes
384 while (TotalSize
> 0) {
390 if (InFptr
!= NULL
) {
394 if (Buffer
!= NULL
) {
398 // Print the file name if errors occurred
400 if (Status
!= STATUS_SUCCESS
) {
401 Error (NULL
, 0, 0003, "Error", "Error parsing file: %s", InFile
->FileName
);
420 Process a PE32 EFI file.
424 OutFptr - file pointer to output binary ROM image file we're creating
425 InFile - structure contains information on the PE32 file to process
426 VendId - vendor ID as required in the option ROM header
427 DevId - device ID as required in the option ROM header
428 Size - pointer to where to return the size added to the output file
438 EFI_PCI_EXPANSION_ROM_HEADER RomHdr
;
439 PCI_DATA_STRUCTURE PciDs23
;
440 PCI_3_0_DATA_STRUCTURE PciDs30
;
442 UINT32 CompressedFileSize
;
444 UINT8
*CompressedBuffer
;
445 UINT8
*TempBufferPtr
;
450 UINT32 HeaderPadBytes
;
453 // Try to open the input file
455 if ((InFptr
= fopen (InFile
->FileName
, "rb")) == NULL
) {
456 Error (NULL
, 0, 0001, "Open file error", "Error opening file: %s", InFile
->FileName
);
460 // Initialize our buffer pointers to null.
463 CompressedBuffer
= NULL
;
466 // Double-check the file to make sure it's what we expect it to be
468 Status
= CheckPE32File (InFptr
, &MachineType
, &SubSystem
);
469 if (Status
!= STATUS_SUCCESS
) {
473 // Seek to the end of the input file and get the file size
475 fseek (InFptr
, 0, SEEK_END
);
476 FileSize
= ftell (InFptr
);
479 // Get the size of the headers we're going to put in front of the image. The
480 // EFI header must be aligned on a 4-byte boundary, so pad accordingly.
482 if (sizeof (RomHdr
) & 0x03) {
483 HeaderPadBytes
= 4 - (sizeof (RomHdr
) & 0x03);
489 // For Pci3.0 to use the different data structure.
491 if (mOptions
.Pci23
== 1) {
492 HeaderSize
= sizeof (PCI_DATA_STRUCTURE
) + HeaderPadBytes
+ sizeof (EFI_PCI_EXPANSION_ROM_HEADER
);
494 HeaderSize
= sizeof (PCI_3_0_DATA_STRUCTURE
) + HeaderPadBytes
+ sizeof (EFI_PCI_EXPANSION_ROM_HEADER
);
497 if (mOptions
.Verbose
) {
498 VerboseMsg(" File size = 0x%X\n", FileSize
);
501 // Allocate memory for the entire file (in case we have to compress), then
502 // seek back to the beginning of the file and read it into our buffer.
504 Buffer
= (UINT8
*) malloc (FileSize
);
505 if (Buffer
== NULL
) {
506 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
507 Status
= STATUS_ERROR
;
511 fseek (InFptr
, 0, SEEK_SET
);
512 if (fread (Buffer
, FileSize
, 1, InFptr
) != 1) {
513 Error (NULL
, 0, 0004, "Error reading file", "File %s", InFile
->FileName
);
514 Status
= STATUS_ERROR
;
518 // Now determine the size of the final output file. It's either the header size
519 // plus the file's size, or the header size plus the compressed file size.
521 if ((InFile
->FileFlags
& FILE_FLAG_COMPRESS
) != 0) {
523 // Allocate a buffer into which we can compress the image, compress it,
524 // and use that size as the new size.
526 CompressedBuffer
= (UINT8
*) malloc (FileSize
);
527 if (CompressedBuffer
== NULL
) {
528 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
529 Status
= STATUS_ERROR
;
533 CompressedFileSize
= FileSize
;
534 Status
= EfiCompress (Buffer
, FileSize
, CompressedBuffer
, &CompressedFileSize
);
535 if (Status
!= STATUS_SUCCESS
) {
536 Error (NULL
, 0, 0007, "Error compressing file!", NULL
);
540 // Now compute the size, then swap buffer pointers.
542 if (mOptions
.Verbose
) {
543 VerboseMsg(" Comp size = 0x%X\n", CompressedFileSize
);
546 TotalSize
= CompressedFileSize
+ HeaderSize
;
547 FileSize
= CompressedFileSize
;
548 TempBufferPtr
= Buffer
;
549 Buffer
= CompressedBuffer
;
550 CompressedBuffer
= TempBufferPtr
;
552 TotalSize
= FileSize
+ HeaderSize
;
555 // Total size must be an even multiple of 512 bytes
557 if (TotalSize
& 0x1FF) {
558 TotalSize
= (TotalSize
+ 0x200) &~0x1ff;
563 if (TotalSize
> MAX_OPTION_ROM_SIZE
) {
564 Error (NULL
, 0, 2000, "Invalid", "Option ROM image %s size exceeds limit of 0x%X bytes.", InFile
->FileName
, MAX_OPTION_ROM_SIZE
);
565 Status
= STATUS_ERROR
;
569 // Return the size to the caller so they can keep track of the running total.
574 // Now fill in the ROM header. These values come from chapter 18 of the
575 // EFI 1.02 specification.
577 memset (&RomHdr
, 0, sizeof (RomHdr
));
578 RomHdr
.Signature
= PCI_EXPANSION_ROM_HEADER_SIGNATURE
;
579 RomHdr
.InitializationSize
= (UINT16
) (TotalSize
/ 512);
580 RomHdr
.EfiSignature
= EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
;
581 RomHdr
.EfiSubsystem
= SubSystem
;
582 RomHdr
.EfiMachineType
= MachineType
;
583 RomHdr
.EfiImageHeaderOffset
= (UINT16
) HeaderSize
;
584 RomHdr
.PcirOffset
= (UINT16
) (sizeof (RomHdr
) + HeaderPadBytes
);
586 // Set image as compressed or not
588 if (InFile
->FileFlags
& FILE_FLAG_COMPRESS
) {
589 RomHdr
.CompressionType
= EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
;
592 // Fill in the PCI data structure
594 if (mOptions
.Pci23
== 1) {
595 memset (&PciDs23
, 0, sizeof (PCI_DATA_STRUCTURE
));
597 memset (&PciDs30
, 0, sizeof (PCI_3_0_DATA_STRUCTURE
));
600 if (mOptions
.Pci23
== 1) {
601 PciDs23
.Signature
= PCI_DATA_STRUCTURE_SIGNATURE
;
602 PciDs23
.VendorId
= VendId
;
603 PciDs23
.DeviceId
= DevId
;
604 PciDs23
.Length
= (UINT16
) sizeof (PCI_DATA_STRUCTURE
);
605 PciDs23
.Revision
= 0;
607 // Class code and code revision from the command line (optional)
609 PciDs23
.ClassCode
[0] = (UINT8
) InFile
->ClassCode
;
610 PciDs23
.ClassCode
[1] = (UINT8
) (InFile
->ClassCode
>> 8);
611 PciDs23
.ClassCode
[2] = (UINT8
) (InFile
->ClassCode
>> 16);
612 PciDs23
.ImageLength
= RomHdr
.InitializationSize
;
613 PciDs23
.CodeRevision
= InFile
->CodeRevision
;
614 PciDs23
.CodeType
= PCI_CODE_TYPE_EFI_IMAGE
;
616 PciDs30
.Signature
= PCI_DATA_STRUCTURE_SIGNATURE
;
617 PciDs30
.VendorId
= VendId
;
618 PciDs30
.DeviceId
= DevId
;
619 PciDs30
.DeviceListOffset
= 0; // to be fixed
620 PciDs30
.Length
= (UINT16
) sizeof (PCI_3_0_DATA_STRUCTURE
);
621 PciDs30
.Revision
= 0x3;
623 // Class code and code revision from the command line (optional)
625 PciDs30
.ClassCode
[0] = (UINT8
) InFile
->ClassCode
;
626 PciDs30
.ClassCode
[1] = (UINT8
) (InFile
->ClassCode
>> 8);
627 PciDs30
.ClassCode
[2] = (UINT8
) (InFile
->ClassCode
>> 16);
628 PciDs30
.ImageLength
= RomHdr
.InitializationSize
;
629 PciDs30
.CodeRevision
= InFile
->CodeRevision
;
630 PciDs30
.CodeType
= PCI_CODE_TYPE_EFI_IMAGE
;
631 PciDs30
.MaxRuntimeImageLength
= 0; // to be fixed
632 PciDs30
.ConfigUtilityCodeHeaderOffset
= 0; // to be fixed
633 PciDs30
.DMTFCLPEntryPointOffset
= 0; // to be fixed
636 // If this is the last image, then set the LAST bit unless requested not
637 // to via the command-line -n argument.
639 if ((InFile
->Next
== NULL
) && (mOptions
.NoLast
== 0)) {
640 if (mOptions
.Pci23
== 1) {
641 PciDs23
.Indicator
= INDICATOR_LAST
;
643 PciDs30
.Indicator
= INDICATOR_LAST
;}
645 if (mOptions
.Pci23
== 1) {
646 PciDs23
.Indicator
= 0;
648 PciDs30
.Indicator
= 0;
652 // Write the ROM header to the output file
654 if (fwrite (&RomHdr
, sizeof (RomHdr
), 1, OutFptr
) != 1) {
655 Error (NULL
, 0, 0002, "Failed to write ROM header to output file!", NULL
);
656 Status
= STATUS_ERROR
;
661 // Write pad bytes to align the PciDs
663 while (HeaderPadBytes
> 0) {
664 if (putc (0, OutFptr
) == EOF
) {
665 Error (NULL
, 0, 0002, "Failed to write ROM header pad bytes to output file!", NULL
);
666 Status
= STATUS_ERROR
;
673 // Write the PCI data structure header to the output file
675 if (mOptions
.Pci23
== 1) {
676 if (fwrite (&PciDs23
, sizeof (PciDs23
), 1, OutFptr
) != 1) {
677 Error (NULL
, 0, 0002, "Failed to write PCI ROM header to output file!", NULL
);
678 Status
= STATUS_ERROR
;
682 if (fwrite (&PciDs30
, sizeof (PciDs30
), 1, OutFptr
) != 1) {
683 Error (NULL
, 0, 0002, "Failed to write PCI ROM header to output file!", NULL
);
684 Status
= STATUS_ERROR
;
689 // Keep track of how many bytes left to write
691 TotalSize
-= HeaderSize
;
694 // Now dump the input file's contents to the output file
696 if (fwrite (Buffer
, FileSize
, 1, OutFptr
) != 1) {
697 Error (NULL
, 0, 0002, "Failed to write all file bytes to output file!", NULL
);
698 Status
= STATUS_ERROR
;
702 TotalSize
-= FileSize
;
704 // Pad the rest of the image to make it a multiple of 512 bytes
706 while (TotalSize
> 0) {
707 if (putc (~0, OutFptr
) == EOF
) {
708 Error (NULL
, 0, 2000, "Failed to write trailing pad bytes output file!", NULL
);
709 Status
= STATUS_ERROR
;
717 if (InFptr
!= NULL
) {
721 // Free up our buffers
723 if (Buffer
!= NULL
) {
727 if (CompressedBuffer
!= NULL
) {
728 free (CompressedBuffer
);
731 // Print the file name if errors occurred
733 if (Status
!= STATUS_SUCCESS
) {
734 Error (NULL
, 0, 0003, "Error parsing", "Error parsing file: %s", InFile
->FileName
);
751 Given a file pointer to a supposed PE32 image file, verify that it is indeed a
752 PE32 image file, and then return the machine type in the supplied pointer.
756 Fptr File pointer to the already-opened PE32 file
757 MachineType Location to stuff the machine type of the PE32 file. This is needed
758 because the image may be Itanium-based, IA32, or EBC.
767 EFI_IMAGE_DOS_HEADER DosHeader
;
768 EFI_IMAGE_OPTIONAL_HEADER_UNION PeHdr
;
771 // Position to the start of the file
773 fseek (Fptr
, 0, SEEK_SET
);
776 // Read the DOS header
778 if (fread (&DosHeader
, sizeof (DosHeader
), 1, Fptr
) != 1) {
779 Error (NULL
, 0, 0004, "Failed to read the DOS stub from the input file!", NULL
);
783 // Check the magic number (0x5A4D)
785 if (DosHeader
.e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
786 Error (NULL
, 0, 2000, "Invalid parameter", "Input file does not appear to be a PE32 image (magic number)!");
790 // Position into the file and check the PE signature
792 fseek (Fptr
, (long) DosHeader
.e_lfanew
, SEEK_SET
);
797 if (fread (&PeHdr
, sizeof (PeHdr
), 1, Fptr
) != 1) {
798 Error (NULL
, 0, 0004, "Failed to read PE/COFF headers from input file!", NULL
);
804 // Check the PE signature in the header "PE\0\0"
806 if (PeHdr
.Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
807 Error (NULL
, 0, 2000, "Invalid parameter", "Input file does not appear to be a PE32 image (signature)!");
811 memcpy ((char *) MachineType
, &PeHdr
.Pe32
.FileHeader
.Machine
, 2);
813 if (PeHdr
.Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
814 *SubSystem
= PeHdr
.Pe32
.OptionalHeader
.Subsystem
;
815 } else if (PeHdr
.Pe32Plus
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
816 *SubSystem
= PeHdr
.Pe32Plus
.OptionalHeader
.Subsystem
;
818 Error (NULL
, 0, 2000, "Invalid parameter", "Unable to find subsystem type!");
822 if (mOptions
.Verbose
) {
823 VerboseMsg(" Got subsystem = 0x%X from image\n", (int) *SubSystem
);
827 // File was successfully identified as a PE32
829 return STATUS_SUCCESS
;
843 Given the Argc/Argv program arguments, and a pointer to an options structure,
844 parse the command-line options and check their validity.
849 Argc - standard C main() argument count
850 Argv[] - standard C main() argument list
851 Options - pointer to a structure to store the options in
855 STATUS_SUCCESS success
861 FILE_LIST
*PrevFileList
;
873 // Clear out the options
875 memset ((char *) Options
, 0, sizeof (OPTIONS
));
878 // To avoid compile warnings
880 FileList
= PrevFileList
= NULL
;
885 // Skip over the program name
891 // If no arguments, assume they want usage info
898 if ((stricmp(Argv
[0], "-h") == 0) || (stricmp(Argv
[0], "--help") == 0)) {
903 if ((stricmp(Argv
[0], "--version") == 0)) {
909 // Process until no more arguments
912 if (Argv
[0][0] == '-') {
914 // Vendor ID specified with -f
916 if (stricmp (Argv
[0], "-f") == 0) {
918 // Make sure there's another parameter
920 Status
= AsciiStringToUint64(Argv
[1], FALSE
, &TempValue
);
921 if (EFI_ERROR (Status
)) {
922 Error (NULL
, 0, 2000, "Invalid option value", "%s = %s", Argv
[0], Argv
[1]);
925 if (TempValue
>= 0x10000) {
926 Error (NULL
, 0, 2000, "Invalid option value", "Vendor Id %s out of range!", Argv
[1]);
929 Options
->VendId
= (UINT16
) TempValue
;
930 Options
->VendIdValid
= 1;
934 } else if (stricmp (Argv
[0], "-i") == 0) {
936 // Device ID specified with -i
937 // Make sure there's another parameter
939 Status
= AsciiStringToUint64(Argv
[1], FALSE
, &TempValue
);
940 if (EFI_ERROR (Status
)) {
941 Error (NULL
, 0, 2000, "Invalid option value", "%s = %s", Argv
[0], Argv
[1]);
944 if (TempValue
>= 0x10000) {
945 Error (NULL
, 0, 2000, "Invalid option value", "Device Id %s out of range!", Argv
[1]);
948 Options
->DevId
= (UINT16
) TempValue
;
949 Options
->DevIdValid
= 1;
953 } else if ((stricmp (Argv
[0], "-o") == 0) || (stricmp (Argv
[0], "--output") == 0)) {
955 // Output filename specified with -o
956 // Make sure there's another parameter
958 if (Argv
[1] == NULL
|| Argv
[1][0] == '-') {
959 Error (NULL
, 0, 2000, "Invalid parameter", "Missing output file name with %s option!", Argv
[0]);
962 strcpy (Options
->OutFileName
, Argv
[1]);
966 } else if ((stricmp (Argv
[0], "-h") == 0) || (stricmp (Argv
[0], "--help") == 0)) {
972 } else if (stricmp (Argv
[0], "-b") == 0) {
974 // Specify binary files with -b
976 FileFlags
= FILE_FLAG_BINARY
;
977 } else if ((stricmp (Argv
[0], "-e") == 0) || (stricmp (Argv
[0], "-ec") == 0)) {
979 // Specify EFI files with -e. Specify EFI-compressed with -c.
981 FileFlags
= FILE_FLAG_EFI
;
982 if ((Argv
[0][2] == 'c') || (Argv
[0][2] == 'C')) {
983 FileFlags
|= FILE_FLAG_COMPRESS
;
986 // Specify not to set the LAST bit in the last file with -n
988 } else if (stricmp (Argv
[0], "-n") == 0) {
990 } else if (((stricmp (Argv
[0], "-v") == 0)) || ((stricmp (Argv
[0], "--verbose") == 0))) {
994 Options
->Verbose
= 1;
995 } else if (stricmp (Argv
[0], "--debug") == 0) {
996 Status
= AsciiStringToUint64(Argv
[1], FALSE
, &DebugLevel
);
997 if (EFI_ERROR (Status
)) {
998 Error (NULL
, 0, 2000, "Invalid option value", "%s = %s", Argv
[0], Argv
[1]);
1001 if (DebugLevel
> 9) {
1002 Error (NULL
, 0, 2000, "Invalid option value", "Debug Level range is 0-9, currnt input level is %d", Argv
[1]);
1005 if (DebugLevel
>=5 && DebugLevel
<=9) {
1006 Options
->Debug
= TRUE
;
1008 Options
->Debug
= FALSE
;
1012 } else if ((stricmp (Argv
[0], "--quiet") == 0) || (stricmp (Argv
[0], "-q") == 0)) {
1013 Options
->Quiet
= TRUE
;
1014 } else if ((stricmp (Argv
[0], "--dump") == 0) || (stricmp (Argv
[0], "-d") == 0)) {
1016 // -dump for dumping a ROM image. In this case, say that the device id
1017 // and vendor id are valid so we don't have to specify bogus ones on the
1020 Options
->DumpOption
= 1;
1022 Options
->VendIdValid
= 1;
1023 Options
->DevIdValid
= 1;
1024 FileFlags
= FILE_FLAG_BINARY
;
1025 } else if ((stricmp (Argv
[0], "-l") == 0) || (stricmp (Argv
[0], "--class-code") == 0)) {
1027 // Class code value for the next file in the list.
1028 // Make sure there's another parameter
1030 Status
= AsciiStringToUint64(Argv
[1], FALSE
, &TempValue
);
1031 if (EFI_ERROR (Status
)) {
1032 Error (NULL
, 0, 2000, "Invalid option value", "%s = %s", Argv
[0], Argv
[1]);
1035 ClassCode
= (UINT32
) TempValue
;
1036 if (ClassCode
& 0xFF000000) {
1037 Error (NULL
, 0, 2000, "Invalid parameter", "Class code %s out of range!", Argv
[1]);
1038 return STATUS_ERROR
;
1040 if (FileList
!= NULL
&& FileList
->ClassCode
== 0) {
1041 FileList
->ClassCode
= ClassCode
;
1045 } else if ((stricmp (Argv
[0], "-r") == 0) || (stricmp (Argv
[0], "--Revision") == 0)) {
1047 // Code revision in the PCI data structure. The value is for the next
1048 // file in the list.
1049 // Make sure there's another parameter
1051 Status
= AsciiStringToUint64(Argv
[1], FALSE
, &TempValue
);
1052 if (EFI_ERROR (Status
)) {
1053 Error (NULL
, 0, 2000, "Invalid option value", "%s = %s", Argv
[0], Argv
[1]);
1056 CodeRevision
= (UINT32
) TempValue
;
1057 if (CodeRevision
& 0xFFFF0000) {
1058 Error (NULL
, 0, 2000, "Invalid parameter", "Code revision %s out of range!", Argv
[1]);
1059 return STATUS_ERROR
;
1061 if (FileList
!= NULL
&& FileList
->CodeRevision
== 0) {
1062 FileList
->CodeRevision
= (UINT16
) CodeRevision
;
1066 } else if ((stricmp (Argv
[0], "-p") == 0) || (stricmp (Argv
[0], "--pci23") == 0)) {
1068 // Default layout meets PCI 3.0 specifications, specifying this flag will for a PCI 2.3 layout.
1072 Error (NULL
, 0, 2000, "Invalid parameter", "Invalid option specified: %s", Argv
[0]);
1073 return STATUS_ERROR
;
1077 // Not a slash-option argument. Must be a file name. Make sure they've specified
1078 // -e or -b already.
1080 if ((FileFlags
& (FILE_FLAG_BINARY
| FILE_FLAG_EFI
)) == 0) {
1081 Error (NULL
, 0, 2000, "Invalid parameter", "Missing -e or -b with input file %s!", Argv
[0]);
1082 return STATUS_ERROR
;
1085 // Check Efi Option RomImage
1087 if ((FileFlags
& FILE_FLAG_EFI
) == FILE_FLAG_EFI
) {
1091 // Create a new file structure
1093 FileList
= (FILE_LIST
*) malloc (sizeof (FILE_LIST
));
1094 if (FileList
== NULL
) {
1095 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!", NULL
);
1096 return STATUS_ERROR
;
1100 // set flag and class code for this image.
1102 memset ((char *) FileList
, 0, sizeof (FILE_LIST
));
1103 FileList
->FileName
= Argv
[0];
1104 FileList
->FileFlags
= FileFlags
;
1105 FileList
->ClassCode
= ClassCode
;
1106 FileList
->CodeRevision
= (UINT16
) CodeRevision
;
1110 if (Options
->FileList
== NULL
) {
1111 Options
->FileList
= FileList
;
1113 if (PrevFileList
== NULL
) {
1114 PrevFileList
= FileList
;
1116 PrevFileList
->Next
= FileList
;
1120 PrevFileList
= FileList
;
1130 // Must have specified some files
1132 if (Options
->FileList
== NULL
) {
1133 Error (NULL
, 0, 2000, "Invalid parameter", "Missing input file name!");
1134 return STATUS_ERROR
;
1138 // For EFI OptionRom image, Make sure a device ID and vendor ID are both specified.
1141 if (!Options
->VendIdValid
) {
1142 Error (NULL
, 0, 2000, "Missing Vendor ID in command line", NULL
);
1143 return STATUS_ERROR
;
1146 if (!Options
->DevIdValid
) {
1147 Error (NULL
, 0, 2000, "Missing Device ID in command line", NULL
);
1148 return STATUS_ERROR
;
1162 Routine Description:
1164 Print version information for this utility.
1175 fprintf (stdout
, "%s Version %d.%d\n", UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
);
1185 Routine Description:
1187 Print usage information for this utility.
1202 fprintf (stdout
, "Usage: %s [options] [file name<s>] \n\n", UTILITY_NAME
);
1205 // Copyright declaration
1207 fprintf (stdout
, "Copyright (c) 2007 - 2009, Intel Corporation. All rights reserved.\n\n");
1212 fprintf (stdout
, "Options:\n");
1213 fprintf (stdout
, " -o FileName, --output FileName\n\
1214 File will be created to store the ouput content.\n");
1215 fprintf (stdout
, " -e EfiFileName\n\
1216 EFI PE32 image files.\n");
1217 fprintf (stdout
, " -ec EfiFileName\n\
1218 EFI PE32 image files and will be compressed.\n");
1219 fprintf (stdout
, " -b BinFileName\n\
1220 Legacy binary files.\n");
1221 fprintf (stdout
, " -l ClassCode\n\
1222 Hex ClassCode in the PCI data structure header.\n");
1223 fprintf (stdout
, " -r Rev Hex Revision in the PCI data structure header.\n");
1224 fprintf (stdout
, " -n Not to automatically set the LAST bit in the last file.\n");
1225 fprintf (stdout
, " -f VendorId\n\
1226 Hex PCI Vendor ID for the device OpROM.\n");
1227 fprintf (stdout
, " -i DeviceId\n\
1228 Hex PCI Device ID for the device OpROM.\n");
1229 fprintf (stdout
, " -p, --pci23\n\
1230 Default layout meets PCI 3.0 specifications\n\
1231 specifying this flag will for a PCI 2.3 layout.\n");
1232 fprintf (stdout
, " -d, --dump\n\
1233 Dump the headers of an existing option ROM image.\n");
1234 fprintf (stdout
, " -v, --verbose\n\
1235 Turn on verbose output with informational messages.\n");
1236 fprintf (stdout
, " --version Show program's version number and exit.\n");
1237 fprintf (stdout
, " -h, --help\n\
1238 Show this help message and exit.\n");
1239 fprintf (stdout
, " -q, --quiet\n\
1240 Disable all messages except FATAL ERRORS.\n");
1241 fprintf (stdout
, " --debug [#,0-9]\n\
1242 Enable debug messages at level #.\n");
1252 Routine Description:
1254 Dump the headers of an existing option ROM image
1258 InFile - the file name of an existing option ROM image
1266 PCI_EXPANSION_ROM_HEADER PciRomHdr
;
1270 EFI_PCI_EXPANSION_ROM_HEADER EfiRomHdr
;
1271 PCI_DATA_STRUCTURE PciDs23
;
1272 PCI_3_0_DATA_STRUCTURE PciDs30
;
1275 // Open the input file
1277 if ((InFptr
= fopen (InFile
->FileName
, "rb")) == NULL
) {
1278 Error (NULL
, 0, 0001, "Error opening file", InFile
->FileName
);
1282 // Go through the image and dump the header stuff for each
1287 // Save our postition in the file, since offsets in the headers
1288 // are relative to the particular image.
1290 ImageStart
= ftell (InFptr
);
1294 // Read the option ROM header. Have to assume a raw binary image for now.
1296 if (fread (&PciRomHdr
, sizeof (PciRomHdr
), 1, InFptr
) != 1) {
1297 Error (NULL
, 0, 3001, "Not supported", "Failed to read PCI ROM header from file!");
1302 // Dump the contents of the header
1304 fprintf (stdout
, "Image %d -- Offset 0x%X\n", ImageCount
, ImageStart
);
1305 fprintf (stdout
, " ROM header contents\n");
1306 fprintf (stdout
, " Signature 0x%04X\n", (UINT32
) PciRomHdr
.Signature
);
1307 fprintf (stdout
, " PCIR offset 0x%04X\n", (UINT32
) PciRomHdr
.PcirOffset
);
1309 // Find PCI data structure
1311 if (fseek (InFptr
, ImageStart
+ PciRomHdr
.PcirOffset
, SEEK_SET
)) {
1312 Error (NULL
, 0, 3001, "Not supported", "Failed to seek to PCI data structure!");
1316 // Read and dump the PCI data structure
1318 memset (&PciDs23
, 0, sizeof (PciDs23
));
1319 memset (&PciDs30
, 0, sizeof (PciDs30
));
1320 if (mOptions
.Pci23
== 1) {
1321 if (fread (&PciDs23
, sizeof (PciDs23
), 1, InFptr
) != 1) {
1322 Error (NULL
, 0, 3001, "Not supported", "Failed to read PCI data structure from file %s!", InFile
->FileName
);
1326 if (fread (&PciDs30
, sizeof (PciDs30
), 1, InFptr
) != 1) {
1327 Error (NULL
, 0, 3001, "Not supported", "Failed to read PCI data structure from file %s!", InFile
->FileName
);
1331 if (mOptions
.Verbose
) {
1332 VerboseMsg("Read PCI data structure from file %s", InFile
->FileName
);
1335 //fprintf (stdout, " PCI Data Structure\n");
1336 if (mOptions
.Pci23
== 1) {
1339 " Signature %c%c%c%c\n",
1340 (char) PciDs23
.Signature
,
1341 (char) (PciDs23
.Signature
>> 8),
1342 (char) (PciDs23
.Signature
>> 16),
1343 (char) (PciDs23
.Signature
>> 24)
1345 fprintf (stdout
, " Vendor ID 0x%04X\n", PciDs23
.VendorId
);
1346 fprintf (stdout
, " Device ID 0x%04X\n", PciDs23
.DeviceId
);
1347 fprintf (stdout
, " Length 0x%04X\n", PciDs23
.Length
);
1348 fprintf (stdout
, " Revision 0x%04X\n", PciDs23
.Revision
);
1351 " Class Code 0x%06X\n",
1352 (UINT32
) (PciDs23
.ClassCode
[0] | (PciDs23
.ClassCode
[1] << 8) | (PciDs23
.ClassCode
[2] << 16))
1354 fprintf (stdout
, " Image size 0x%X\n", PciDs23
.ImageLength
* 512);
1355 fprintf (stdout
, " Code revision: 0x%04X\n", PciDs23
.CodeRevision
);
1356 fprintf (stdout
, " Indicator 0x%02X", (UINT32
) PciDs23
.Indicator
);
1360 " Signature %c%c%c%c\n",
1361 (char) PciDs30
.Signature
,
1362 (char) (PciDs30
.Signature
>> 8),
1363 (char) (PciDs30
.Signature
>> 16),
1364 (char) (PciDs30
.Signature
>> 24)
1366 fprintf (stdout
, " Vendor ID 0x%04X\n", PciDs30
.VendorId
);
1367 fprintf (stdout
, " Device ID 0x%04X\n", PciDs30
.DeviceId
);
1368 fprintf (stdout
, " Length 0x%04X\n", PciDs30
.Length
);
1369 fprintf (stdout
, " Revision 0x%04X\n", PciDs30
.Revision
);
1370 fprintf (stdout
, " DeviceListOffset 0x%02X\n", (UINT32
) PciDs30
.DeviceListOffset
);
1373 " Class Code 0x%06X\n",
1374 (UINT32
) (PciDs30
.ClassCode
[0] | (PciDs30
.ClassCode
[1] << 8) | (PciDs30
.ClassCode
[2] << 16))
1376 fprintf (stdout
, " Image size 0x%X\n", PciDs30
.ImageLength
* 512);
1377 fprintf (stdout
, " Code revision: 0x%04X\n", PciDs30
.CodeRevision
);
1378 fprintf (stdout
, " MaxRuntimeImageLength 0x%02X\n", (UINT32
) PciDs30
.MaxRuntimeImageLength
);
1379 fprintf (stdout
, " ConfigUtilityCodeHeaderOffset 0x%02X\n", (UINT32
) PciDs30
.ConfigUtilityCodeHeaderOffset
);
1380 fprintf (stdout
, " DMTFCLPEntryPointOffset 0x%02X\n", (UINT32
) PciDs30
.DMTFCLPEntryPointOffset
);
1381 fprintf (stdout
, " Indicator 0x%02X", (UINT32
) PciDs30
.Indicator
);
1384 // Print the indicator, used to flag the last image
1386 if (PciDs23
.Indicator
== INDICATOR_LAST
|| PciDs30
.Indicator
== INDICATOR_LAST
) {
1387 fprintf (stdout
, " (last image)\n");
1389 fprintf (stdout
, "\n");
1392 // Print the code type. If EFI code, then we can provide more info.
1394 if (mOptions
.Pci23
== 1) {
1395 fprintf (stdout
, " Code type 0x%02X", (UINT32
) PciDs23
.CodeType
);
1397 fprintf (stdout
, " Code type 0x%02X", (UINT32
) PciDs30
.CodeType
);
1399 if (PciDs23
.CodeType
== PCI_CODE_TYPE_EFI_IMAGE
|| PciDs30
.CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) {
1400 fprintf (stdout
, " (EFI image)\n");
1402 // Re-read the header as an EFI ROM header, then dump more info
1404 fprintf (stdout
, " EFI ROM header contents\n");
1405 if (fseek (InFptr
, ImageStart
, SEEK_SET
)) {
1406 Error (NULL
, 0, 5001, "Failed to re-seek to ROM header structure!", NULL
);
1410 if (fread (&EfiRomHdr
, sizeof (EfiRomHdr
), 1, InFptr
) != 1) {
1411 Error (NULL
, 0, 5001, "Failed to read EFI PCI ROM header from file!", NULL
);
1415 // Now dump more info
1417 fprintf (stdout
, " EFI Signature 0x%04X\n", EfiRomHdr
.EfiSignature
);
1420 " Compression Type 0x%04X ",
1421 (UINT32
) EfiRomHdr
.CompressionType
1423 if (EfiRomHdr
.CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
1424 fprintf (stdout
, "(compressed)\n");
1426 fprintf (stdout
, "(not compressed)\n");
1431 " Machine type 0x%04X (%s)\n",
1432 EfiRomHdr
.EfiMachineType
,
1433 GetMachineTypeStr (EfiRomHdr
.EfiMachineType
)
1437 " Subsystem 0x%04X (%s)\n",
1438 EfiRomHdr
.EfiSubsystem
,
1439 GetSubsystemTypeStr (EfiRomHdr
.EfiSubsystem
)
1443 " EFI image offset 0x%04X (@0x%X)\n",
1444 (UINT32
) EfiRomHdr
.EfiImageHeaderOffset
,
1445 (UINT32
) (EfiRomHdr
.EfiImageHeaderOffset
+ ImageStart
)
1452 fprintf (stdout
, "\n");
1455 // If code type is EFI image, then dump it as well?
1457 // if (PciDs.CodeType == PCI_CODE_TYPE_EFI_IMAGE) {
1460 // If last image, then we're done
1462 if (PciDs23
.Indicator
== INDICATOR_LAST
|| PciDs30
.Indicator
== INDICATOR_LAST
) {
1466 // Seek to the start of the next image
1468 if (mOptions
.Pci23
== 1) {
1469 if (fseek (InFptr
, ImageStart
+ (PciDs23
.ImageLength
* 512), SEEK_SET
)) {
1470 Error (NULL
, 0, 3001, "Not supported", "Failed to seek to next image!");
1474 if (fseek (InFptr
, ImageStart
+ (PciDs30
.ImageLength
* 512), SEEK_SET
)) {
1475 Error (NULL
, 0, 3001, "Not supported", "Failed to seek to next image!");
1491 Routine Description:
1493 GC_TODO: Add function description
1497 MachineType - GC_TODO: add argument description
1501 GC_TODO: add return values
1507 for (Index
= 0; mMachineTypes
[Index
].Name
!= NULL
; Index
++) {
1508 if (mMachineTypes
[Index
].Value
== MachineType
) {
1509 return mMachineTypes
[Index
].Name
;
1518 GetSubsystemTypeStr (
1519 UINT16 SubsystemType
1523 Routine Description:
1525 GC_TODO: Add function description
1529 SubsystemType - GC_TODO: add argument description
1533 GC_TODO: add return values
1539 for (Index
= 0; mSubsystemTypes
[Index
].Name
!= NULL
; Index
++) {
1540 if (mSubsystemTypes
[Index
].Value
== SubsystemType
) {
1541 return mSubsystemTypes
[Index
].Name
;