2 * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <gpxe/efi/efi.h>
30 #include <gpxe/efi/IndustryStandard/PeImage.h>
31 #include <gpxe/efi/IndustryStandard/Pci22.h>
33 #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
35 /** Command-line options */
44 * @v len Length of memory to allocate
45 * @ret ptr Pointer to allocated memory
47 static void * xmalloc ( size_t len
) {
52 eprintf ( "Could not allocate %zd bytes\n", len
);
65 static size_t file_size ( FILE *file
) {
72 * Read information from PE headers
75 * @ret machine Machine type
76 * @ret subsystem EFI subsystem
78 static void read_pe_info ( void *pe
, uint16_t *machine
,
79 uint16_t *subsystem
) {
80 EFI_IMAGE_DOS_HEADER
*dos
;
82 EFI_IMAGE_NT_HEADERS32 nt32
;
83 EFI_IMAGE_NT_HEADERS64 nt64
;
86 /* Locate NT header */
88 nt
= ( pe
+ dos
->e_lfanew
);
90 /* Parse out PE information */
91 *machine
= nt
->nt32
.FileHeader
.Machine
;
93 case EFI_IMAGE_MACHINE_IA32
:
94 *subsystem
= nt
->nt32
.OptionalHeader
.Subsystem
;
96 case EFI_IMAGE_MACHINE_X64
:
97 *subsystem
= nt
->nt64
.OptionalHeader
.Subsystem
;
100 eprintf ( "Unrecognised machine type %04x\n", *machine
);
106 * Convert EFI image to ROM image
111 static void make_efi_rom ( FILE *pe
, FILE *rom
, struct options
*opts
) {
113 EFI_PCI_EXPANSION_ROM_HEADER rom
;
114 PCI_DATA_STRUCTURE pci
__attribute__ (( aligned ( 4 ) ));
125 /* Determine PE file size */
126 if ( fstat ( fileno ( pe
), &pe_stat
) != 0 ) {
127 eprintf ( "Could not stat PE file: %s\n",
128 strerror ( errno
) );
131 pe_size
= pe_stat
.st_size
;
133 /* Determine ROM file size */
134 rom_size
= ( ( pe_size
+ sizeof ( *headers
) + 511 ) & ~511 );
136 /* Allocate ROM buffer and read in PE file */
137 buf
= xmalloc ( rom_size
);
138 memset ( buf
, 0, rom_size
);
140 payload
= ( buf
+ sizeof ( *headers
) );
141 if ( fread ( payload
, pe_size
, 1, pe
) != 1 ) {
142 eprintf ( "Could not read PE file: %s\n",
143 strerror ( errno
) );
147 /* Construct ROM header */
148 headers
->rom
.Signature
= PCI_EXPANSION_ROM_HEADER_SIGNATURE
;
149 headers
->rom
.InitializationSize
= ( rom_size
/ 512 );
150 headers
->rom
.EfiSignature
= EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
;
151 read_pe_info ( payload
, &headers
->rom
.EfiMachineType
,
152 &headers
->rom
.EfiSubsystem
);
153 headers
->rom
.EfiImageHeaderOffset
= sizeof ( *headers
);
154 headers
->rom
.PcirOffset
=
155 offsetof ( typeof ( *headers
), pci
);
156 headers
->pci
.Signature
= PCI_DATA_STRUCTURE_SIGNATURE
;
157 headers
->pci
.VendorId
= opts
->vendor
;
158 headers
->pci
.DeviceId
= opts
->device
;
159 headers
->pci
.Length
= sizeof ( headers
->pci
);
160 headers
->pci
.ClassCode
[0] = PCI_CLASS_NETWORK
;
161 headers
->pci
.ImageLength
= ( rom_size
/ 512 );
162 headers
->pci
.CodeType
= 0x03; /* No constant in EFI headers? */
163 headers
->pci
.Indicator
= 0x80; /* No constant in EFI headers? */
165 /* Fix image checksum */
166 for ( i
= 0, checksum
= 0 ; i
< rom_size
; i
++ )
167 checksum
+= *( ( uint8_t * ) buf
+ i
);
168 headers
->checksum
-= checksum
;
171 if ( fwrite ( buf
, rom_size
, 1, rom
) != 1 ) {
172 eprintf ( "Could not write ROM file: %s\n",
173 strerror ( errno
) );
181 * @v program_name Program name
183 static void print_help ( const char *program_name
) {
184 eprintf ( "Syntax: %s [--vendor=VVVV] [--device=DDDD] "
185 "infile outfile\n", program_name
);
189 * Parse command-line options
191 * @v argc Argument count
192 * @v argv Argument list
193 * @v opts Options structure to populate
195 static int parse_options ( const int argc
, char **argv
,
196 struct options
*opts
) {
201 int option_index
= 0;
202 static struct option long_options
[] = {
203 { "vendor", required_argument
, NULL
, 'v' },
204 { "device", required_argument
, NULL
, 'd' },
205 { "help", 0, NULL
, 'h' },
209 if ( ( c
= getopt_long ( argc
, argv
, "v:d:h",
211 &option_index
) ) == -1 ) {
217 opts
->vendor
= strtoul ( optarg
, &end
, 16 );
219 eprintf ( "Invalid vendor \"%s\"\n", optarg
);
224 opts
->device
= strtoul ( optarg
, &end
, 16 );
226 eprintf ( "Invalid device \"%s\"\n", optarg
);
231 print_help ( argv
[0] );
241 int main ( int argc
, char **argv
) {
242 struct options opts
= {
244 unsigned int infile_index
;
245 const char *infile_name
;
246 const char *outfile_name
;
250 /* Parse command-line arguments */
251 infile_index
= parse_options ( argc
, argv
, &opts
);
252 if ( argc
!= ( infile_index
+ 2 ) ) {
253 print_help ( argv
[0] );
256 infile_name
= argv
[infile_index
];
257 outfile_name
= argv
[infile_index
+ 1];
259 /* Open input and output files */
260 infile
= fopen ( infile_name
, "r" );
262 eprintf ( "Could not open %s for reading: %s\n",
263 infile_name
, strerror ( errno
) );
266 outfile
= fopen ( outfile_name
, "w" );
268 eprintf ( "Could not open %s for writing: %s\n",
269 outfile_name
, strerror ( errno
) );
274 make_efi_rom ( infile
, outfile
, &opts
);