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
30 #include <ipxe/efi/efi.h>
31 #include <ipxe/efi/IndustryStandard/PeImage.h>
32 #include <ipxe/efi/IndustryStandard/Pci22.h>
34 #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
36 /** Command-line options */
45 * @v len Length of memory to allocate
46 * @ret ptr Pointer to allocated memory
48 static void * xmalloc ( size_t len ) {
53 eprintf ( "Could not allocate %zd bytes\n", len );
61 * Read information from PE headers
64 * @ret machine Machine type
65 * @ret subsystem EFI subsystem
67 static void read_pe_info ( void *pe, uint16_t *machine,
68 uint16_t *subsystem ) {
69 EFI_IMAGE_DOS_HEADER *dos;
71 EFI_IMAGE_NT_HEADERS32 nt32;
72 EFI_IMAGE_NT_HEADERS64 nt64;
75 /* Locate NT header */
77 nt = ( pe + dos->e_lfanew );
79 /* Parse out PE information */
80 *machine = nt->nt32.FileHeader.Machine;
82 case EFI_IMAGE_MACHINE_IA32:
83 *subsystem = nt->nt32.OptionalHeader.Subsystem;
85 case EFI_IMAGE_MACHINE_X64:
86 *subsystem = nt->nt64.OptionalHeader.Subsystem;
89 eprintf ( "Unrecognised machine type %04x\n", *machine );
95 * Convert EFI image to ROM image
100 static void make_efi_rom ( FILE *pe, FILE *rom, struct options *opts ) {
102 EFI_PCI_EXPANSION_ROM_HEADER rom;
103 PCI_DATA_STRUCTURE pci __attribute__ (( aligned ( 4 ) ));
114 /* Determine PE file size */
115 if ( fstat ( fileno ( pe ), &pe_stat ) != 0 ) {
116 eprintf ( "Could not stat PE file: %s\n",
117 strerror ( errno ) );
120 pe_size = pe_stat.st_size;
122 /* Determine ROM file size */
123 rom_size = ( ( pe_size + sizeof ( *headers ) + 511 ) & ~511 );
125 /* Allocate ROM buffer and read in PE file */
126 buf = xmalloc ( rom_size );
127 memset ( buf, 0, rom_size );
129 payload = ( buf + sizeof ( *headers ) );
130 if ( fread ( payload, pe_size, 1, pe ) != 1 ) {
131 eprintf ( "Could not read PE file: %s\n",
132 strerror ( errno ) );
136 /* Construct ROM header */
137 headers->rom.Signature = PCI_EXPANSION_ROM_HEADER_SIGNATURE;
138 headers->rom.InitializationSize = ( rom_size / 512 );
139 headers->rom.EfiSignature = EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE;
140 read_pe_info ( payload, &headers->rom.EfiMachineType,
141 &headers->rom.EfiSubsystem );
142 headers->rom.EfiImageHeaderOffset = sizeof ( *headers );
143 headers->rom.PcirOffset =
144 offsetof ( typeof ( *headers ), pci );
145 headers->pci.Signature = PCI_DATA_STRUCTURE_SIGNATURE;
146 headers->pci.VendorId = opts->vendor;
147 headers->pci.DeviceId = opts->device;
148 headers->pci.Length = sizeof ( headers->pci );
149 headers->pci.ClassCode[0] = PCI_CLASS_NETWORK;
150 headers->pci.ImageLength = ( rom_size / 512 );
151 headers->pci.CodeType = 0x03; /* No constant in EFI headers? */
152 headers->pci.Indicator = 0x80; /* No constant in EFI headers? */
154 /* Fix image checksum */
155 for ( i = 0, checksum = 0 ; i < rom_size ; i++ )
156 checksum += *( ( uint8_t * ) buf + i );
157 headers->checksum -= checksum;
160 if ( fwrite ( buf, rom_size, 1, rom ) != 1 ) {
161 eprintf ( "Could not write ROM file: %s\n",
162 strerror ( errno ) );
170 * @v program_name Program name
172 static void print_help ( const char *program_name ) {
173 eprintf ( "Syntax: %s [--vendor=VVVV] [--device=DDDD] "
174 "infile outfile\n", program_name );
178 * Parse command-line options
180 * @v argc Argument count
181 * @v argv Argument list
182 * @v opts Options structure to populate
184 static int parse_options ( const int argc, char **argv,
185 struct options *opts ) {
190 int option_index = 0;
191 static struct option long_options[] = {
192 { "vendor", required_argument, NULL, 'v' },
193 { "device", required_argument, NULL, 'd' },
194 { "help", 0, NULL, 'h' },
198 if ( ( c = getopt_long ( argc, argv, "v:d:h",
200 &option_index ) ) == -1 ) {
206 opts->vendor = strtoul ( optarg, &end, 16 );
208 eprintf ( "Invalid vendor \"%s\"\n", optarg );
213 opts->device = strtoul ( optarg, &end, 16 );
215 eprintf ( "Invalid device \"%s\"\n", optarg );
220 print_help ( argv[0] );
230 int main ( int argc, char **argv ) {
233 const char *infile_name;
234 const char *outfile_name;
238 /* Parse command-line arguments */
239 memset ( &opts, 0, sizeof ( opts ) );
240 infile_index = parse_options ( argc, argv, &opts );
241 if ( argc != ( infile_index + 2 ) ) {
242 print_help ( argv[0] );
245 infile_name = argv[infile_index];
246 outfile_name = argv[infile_index + 1];
248 /* Open input and output files */
249 infile = fopen ( infile_name, "r" );
251 eprintf ( "Could not open %s for reading: %s\n",
252 infile_name, strerror ( errno ) );
255 outfile = fopen ( outfile_name, "w" );
257 eprintf ( "Could not open %s for writing: %s\n",
258 outfile_name, strerror ( errno ) );
263 make_efi_rom ( infile, outfile, &opts );