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 )
33 #include <ipxe/efi/Uefi.h>
34 #include <ipxe/efi/IndustryStandard/PeImage.h>
35 #include <ipxe/efi/IndustryStandard/Pci22.h>
37 #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
39 /* Round up ROM size */
40 #define ROM_SIZE( len ) ( ( (len) + 511 ) & ~511 )
42 /* Include the EDK2 compression code */
43 #include "eficompress.c"
45 /** Command-line options */
55 * @v len Length of memory to allocate
56 * @ret ptr Pointer to allocated memory
58 static void * xmalloc ( size_t len
) {
63 eprintf ( "Could not allocate %zd bytes\n", len
);
71 * Read information from PE headers
74 * @ret machine Machine type
75 * @ret subsystem EFI subsystem
77 static void read_pe_info ( void *pe
, uint16_t *machine
,
78 uint16_t *subsystem
) {
79 EFI_IMAGE_DOS_HEADER
*dos
;
81 EFI_IMAGE_NT_HEADERS32 nt32
;
82 EFI_IMAGE_NT_HEADERS64 nt64
;
85 /* Locate NT header */
87 nt
= ( pe
+ dos
->e_lfanew
);
89 /* Parse out PE information */
90 *machine
= nt
->nt32
.FileHeader
.Machine
;
92 case EFI_IMAGE_MACHINE_IA32
:
93 case EFI_IMAGE_MACHINE_ARMTHUMB_MIXED
:
94 *subsystem
= nt
->nt32
.OptionalHeader
.Subsystem
;
96 case EFI_IMAGE_MACHINE_X64
:
97 case EFI_IMAGE_MACHINE_AARCH64
:
98 *subsystem
= nt
->nt64
.OptionalHeader
.Subsystem
;
101 eprintf ( "Unrecognised machine type %04x\n", *machine
);
107 * Attempt to compress EFI data in-place
109 * @v data Data to be compressed
110 * @v max_len Length of data
111 * @ret len Length after attempted compression
113 static size_t efi_compress ( void *data
, size_t max_len
) {
117 /* Allocate temporary buffer for compressed data */
118 tmp
= xmalloc ( max_len
);
120 /* Attempt compression */
122 if ( ( EfiCompress ( data
, max_len
, tmp
, &len
) == 0 ) &&
123 ( len
< max_len
) ) {
124 memcpy ( data
, tmp
, len
);
129 /* Free temporary buffer */
136 * Convert EFI image to ROM image
141 static void make_efi_rom ( FILE *pe
, FILE *rom
, struct options
*opts
) {
143 EFI_PCI_EXPANSION_ROM_HEADER rom
;
144 PCI_DATA_STRUCTURE pci
__attribute__ (( aligned ( 4 ) ));
150 size_t compressed_size
;
159 /* Determine PE file size */
160 if ( fstat ( fileno ( pe
), &pe_stat
) != 0 ) {
161 eprintf ( "Could not stat PE file: %s\n",
162 strerror ( errno
) );
165 pe_size
= pe_stat
.st_size
;
167 /* Determine ROM file size */
168 rom_size
= ROM_SIZE ( sizeof ( *headers
) + pe_size
);
170 /* Allocate ROM buffer and read in PE file */
171 buf
= xmalloc ( rom_size
);
172 memset ( buf
, 0, rom_size
);
174 payload
= ( buf
+ sizeof ( *headers
) );
175 if ( fread ( payload
, pe_size
, 1, pe
) != 1 ) {
176 eprintf ( "Could not read PE file: %s\n",
177 strerror ( errno
) );
181 /* Parse PE headers */
182 read_pe_info ( payload
, &machine
, &subsystem
);
184 /* Compress the image, if requested */
185 if ( opts
->compress
) {
186 compressed_size
= efi_compress ( payload
, pe_size
);
187 rom_size
= ROM_SIZE ( sizeof ( *headers
) + compressed_size
);
188 compressed
= ( compressed_size
< pe_size
);
193 /* Construct ROM header */
194 headers
->rom
.Signature
= PCI_EXPANSION_ROM_HEADER_SIGNATURE
;
195 headers
->rom
.InitializationSize
= ( rom_size
/ 512 );
196 headers
->rom
.EfiSignature
= EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
;
197 headers
->rom
.EfiSubsystem
= subsystem
;
198 headers
->rom
.EfiMachineType
= machine
;
199 headers
->rom
.CompressionType
=
200 ( compressed ? EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
: 0 );
201 headers
->rom
.EfiImageHeaderOffset
= sizeof ( *headers
);
202 headers
->rom
.PcirOffset
=
203 offsetof ( typeof ( *headers
), pci
);
204 headers
->pci
.Signature
= PCI_DATA_STRUCTURE_SIGNATURE
;
205 headers
->pci
.VendorId
= opts
->vendor
;
206 headers
->pci
.DeviceId
= opts
->device
;
207 headers
->pci
.Length
= sizeof ( headers
->pci
);
208 headers
->pci
.ClassCode
[2] = PCI_CLASS_NETWORK
;
209 headers
->pci
.ImageLength
= ( rom_size
/ 512 );
210 headers
->pci
.CodeType
= 0x03; /* No constant in EFI headers? */
211 headers
->pci
.Indicator
= 0x80; /* No constant in EFI headers? */
213 /* Fix image checksum */
214 for ( i
= 0, checksum
= 0 ; i
< rom_size
; i
++ )
215 checksum
+= *( ( uint8_t * ) buf
+ i
);
216 headers
->checksum
-= checksum
;
219 if ( fwrite ( buf
, rom_size
, 1, rom
) != 1 ) {
220 eprintf ( "Could not write ROM file: %s\n",
221 strerror ( errno
) );
229 * @v program_name Program name
231 static void print_help ( const char *program_name
) {
232 eprintf ( "Syntax: %s [--vendor=VVVV] [--device=DDDD] "
233 "infile outfile\n", program_name
);
237 * Parse command-line options
239 * @v argc Argument count
240 * @v argv Argument list
241 * @v opts Options structure to populate
243 static int parse_options ( const int argc
, char **argv
,
244 struct options
*opts
) {
249 int option_index
= 0;
250 static struct option long_options
[] = {
251 { "vendor", required_argument
, NULL
, 'v' },
252 { "device", required_argument
, NULL
, 'd' },
253 { "compress", 0, NULL
, 'c' },
254 { "help", 0, NULL
, 'h' },
258 if ( ( c
= getopt_long ( argc
, argv
, "v:d:ch",
260 &option_index
) ) == -1 ) {
266 opts
->vendor
= strtoul ( optarg
, &end
, 16 );
267 if ( *end
|| ( ! *optarg
) ) {
268 eprintf ( "Invalid vendor \"%s\"\n", optarg
);
273 opts
->device
= strtoul ( optarg
, &end
, 16 );
274 if ( *end
|| ( ! *optarg
) ) {
275 eprintf ( "Invalid device \"%s\"\n", optarg
);
283 print_help ( argv
[0] );
293 int main ( int argc
, char **argv
) {
296 const char *infile_name
;
297 const char *outfile_name
;
301 /* Parse command-line arguments */
302 memset ( &opts
, 0, sizeof ( opts
) );
303 infile_index
= parse_options ( argc
, argv
, &opts
);
304 if ( argc
!= ( infile_index
+ 2 ) ) {
305 print_help ( argv
[0] );
308 infile_name
= argv
[infile_index
];
309 outfile_name
= argv
[infile_index
+ 1];
311 /* Open input and output files */
312 infile
= fopen ( infile_name
, "r" );
314 eprintf ( "Could not open %s for reading: %s\n",
315 infile_name
, strerror ( errno
) );
318 outfile
= fopen ( outfile_name
, "w" );
320 eprintf ( "Could not open %s for writing: %s\n",
321 outfile_name
, strerror ( errno
) );
326 make_efi_rom ( infile
, outfile
, &opts
);