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., 51 Franklin Street, Fifth Floor, Boston, MA
20 #define FILE_LICENCE(...) extern void __file_licence ( void )
31 #include <ipxe/efi/Uefi.h>
32 #include <ipxe/efi/IndustryStandard/PeImage.h>
33 #include <ipxe/efi/IndustryStandard/Pci22.h>
35 #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
37 /** Command-line options */
46 * @v len Length of memory to allocate
47 * @ret ptr Pointer to allocated memory
49 static void * xmalloc ( size_t len
) {
54 eprintf ( "Could not allocate %zd bytes\n", len
);
62 * Read information from PE headers
65 * @ret machine Machine type
66 * @ret subsystem EFI subsystem
68 static void read_pe_info ( void *pe
, uint16_t *machine
,
69 uint16_t *subsystem
) {
70 EFI_IMAGE_DOS_HEADER
*dos
;
72 EFI_IMAGE_NT_HEADERS32 nt32
;
73 EFI_IMAGE_NT_HEADERS64 nt64
;
76 /* Locate NT header */
78 nt
= ( pe
+ dos
->e_lfanew
);
80 /* Parse out PE information */
81 *machine
= nt
->nt32
.FileHeader
.Machine
;
83 case EFI_IMAGE_MACHINE_IA32
:
84 *subsystem
= nt
->nt32
.OptionalHeader
.Subsystem
;
86 case EFI_IMAGE_MACHINE_X64
:
87 *subsystem
= nt
->nt64
.OptionalHeader
.Subsystem
;
90 eprintf ( "Unrecognised machine type %04x\n", *machine
);
96 * Convert EFI image to ROM image
101 static void make_efi_rom ( FILE *pe
, FILE *rom
, struct options
*opts
) {
103 EFI_PCI_EXPANSION_ROM_HEADER rom
;
104 PCI_DATA_STRUCTURE pci
__attribute__ (( aligned ( 4 ) ));
115 /* Determine PE file size */
116 if ( fstat ( fileno ( pe
), &pe_stat
) != 0 ) {
117 eprintf ( "Could not stat PE file: %s\n",
118 strerror ( errno
) );
121 pe_size
= pe_stat
.st_size
;
123 /* Determine ROM file size */
124 rom_size
= ( ( pe_size
+ sizeof ( *headers
) + 511 ) & ~511 );
126 /* Allocate ROM buffer and read in PE file */
127 buf
= xmalloc ( rom_size
);
128 memset ( buf
, 0, rom_size
);
130 payload
= ( buf
+ sizeof ( *headers
) );
131 if ( fread ( payload
, pe_size
, 1, pe
) != 1 ) {
132 eprintf ( "Could not read PE file: %s\n",
133 strerror ( errno
) );
137 /* Construct ROM header */
138 headers
->rom
.Signature
= PCI_EXPANSION_ROM_HEADER_SIGNATURE
;
139 headers
->rom
.InitializationSize
= ( rom_size
/ 512 );
140 headers
->rom
.EfiSignature
= EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
;
141 read_pe_info ( payload
, &headers
->rom
.EfiMachineType
,
142 &headers
->rom
.EfiSubsystem
);
143 headers
->rom
.EfiImageHeaderOffset
= sizeof ( *headers
);
144 headers
->rom
.PcirOffset
=
145 offsetof ( typeof ( *headers
), pci
);
146 headers
->pci
.Signature
= PCI_DATA_STRUCTURE_SIGNATURE
;
147 headers
->pci
.VendorId
= opts
->vendor
;
148 headers
->pci
.DeviceId
= opts
->device
;
149 headers
->pci
.Length
= sizeof ( headers
->pci
);
150 headers
->pci
.ClassCode
[0] = PCI_CLASS_NETWORK
;
151 headers
->pci
.ImageLength
= ( rom_size
/ 512 );
152 headers
->pci
.CodeType
= 0x03; /* No constant in EFI headers? */
153 headers
->pci
.Indicator
= 0x80; /* No constant in EFI headers? */
155 /* Fix image checksum */
156 for ( i
= 0, checksum
= 0 ; i
< rom_size
; i
++ )
157 checksum
+= *( ( uint8_t * ) buf
+ i
);
158 headers
->checksum
-= checksum
;
161 if ( fwrite ( buf
, rom_size
, 1, rom
) != 1 ) {
162 eprintf ( "Could not write ROM file: %s\n",
163 strerror ( errno
) );
171 * @v program_name Program name
173 static void print_help ( const char *program_name
) {
174 eprintf ( "Syntax: %s [--vendor=VVVV] [--device=DDDD] "
175 "infile outfile\n", program_name
);
179 * Parse command-line options
181 * @v argc Argument count
182 * @v argv Argument list
183 * @v opts Options structure to populate
185 static int parse_options ( const int argc
, char **argv
,
186 struct options
*opts
) {
191 int option_index
= 0;
192 static struct option long_options
[] = {
193 { "vendor", required_argument
, NULL
, 'v' },
194 { "device", required_argument
, NULL
, 'd' },
195 { "help", 0, NULL
, 'h' },
199 if ( ( c
= getopt_long ( argc
, argv
, "v:d:h",
201 &option_index
) ) == -1 ) {
207 opts
->vendor
= strtoul ( optarg
, &end
, 16 );
209 eprintf ( "Invalid vendor \"%s\"\n", optarg
);
214 opts
->device
= strtoul ( optarg
, &end
, 16 );
216 eprintf ( "Invalid device \"%s\"\n", optarg
);
221 print_help ( argv
[0] );
231 int main ( int argc
, char **argv
) {
234 const char *infile_name
;
235 const char *outfile_name
;
239 /* Parse command-line arguments */
240 memset ( &opts
, 0, sizeof ( opts
) );
241 infile_index
= parse_options ( argc
, argv
, &opts
);
242 if ( argc
!= ( infile_index
+ 2 ) ) {
243 print_help ( argv
[0] );
246 infile_name
= argv
[infile_index
];
247 outfile_name
= argv
[infile_index
+ 1];
249 /* Open input and output files */
250 infile
= fopen ( infile_name
, "r" );
252 eprintf ( "Could not open %s for reading: %s\n",
253 infile_name
, strerror ( errno
) );
256 outfile
= fopen ( outfile_name
, "w" );
258 eprintf ( "Could not open %s for writing: %s\n",
259 outfile_name
, strerror ( errno
) );
264 make_efi_rom ( infile
, outfile
, &opts
);