[infiniband] Add support for performing service record lookups
[ipxe.git] / src / util / efirom.c
1 /*
2 * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA.
18 */
19
20 #define FILE_LICENCE(...) extern void __file_licence ( void )
21 #include <stdint.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <getopt.h>
31 #include <ipxe/efi/Uefi.h>
32 #include <ipxe/efi/IndustryStandard/PeImage.h>
33 #include <ipxe/efi/IndustryStandard/Pci22.h>
34
35 #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
36
37 /** Command-line options */
38 struct options {
39 uint16_t vendor;
40 uint16_t device;
41 };
42
43 /**
44 * Allocate memory
45 *
46 * @v len Length of memory to allocate
47 * @ret ptr Pointer to allocated memory
48 */
49 static void * xmalloc ( size_t len ) {
50 void *ptr;
51
52 ptr = malloc ( len );
53 if ( ! ptr ) {
54 eprintf ( "Could not allocate %zd bytes\n", len );
55 exit ( 1 );
56 }
57
58 return ptr;
59 }
60
61 /**
62 * Read information from PE headers
63 *
64 * @v pe PE file
65 * @ret machine Machine type
66 * @ret subsystem EFI subsystem
67 */
68 static void read_pe_info ( void *pe, uint16_t *machine,
69 uint16_t *subsystem ) {
70 EFI_IMAGE_DOS_HEADER *dos;
71 union {
72 EFI_IMAGE_NT_HEADERS32 nt32;
73 EFI_IMAGE_NT_HEADERS64 nt64;
74 } *nt;
75
76 /* Locate NT header */
77 dos = pe;
78 nt = ( pe + dos->e_lfanew );
79
80 /* Parse out PE information */
81 *machine = nt->nt32.FileHeader.Machine;
82 switch ( *machine ) {
83 case EFI_IMAGE_MACHINE_IA32:
84 *subsystem = nt->nt32.OptionalHeader.Subsystem;
85 break;
86 case EFI_IMAGE_MACHINE_X64:
87 *subsystem = nt->nt64.OptionalHeader.Subsystem;
88 break;
89 default:
90 eprintf ( "Unrecognised machine type %04x\n", *machine );
91 exit ( 1 );
92 }
93 }
94
95 /**
96 * Convert EFI image to ROM image
97 *
98 * @v pe EFI file
99 * @v rom ROM file
100 */
101 static void make_efi_rom ( FILE *pe, FILE *rom, struct options *opts ) {
102 struct {
103 EFI_PCI_EXPANSION_ROM_HEADER rom;
104 PCI_DATA_STRUCTURE pci __attribute__ (( aligned ( 4 ) ));
105 uint8_t checksum;
106 } *headers;
107 struct stat pe_stat;
108 size_t pe_size;
109 size_t rom_size;
110 void *buf;
111 void *payload;
112 unsigned int i;
113 uint8_t checksum;
114
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 ) );
119 exit ( 1 );
120 }
121 pe_size = pe_stat.st_size;
122
123 /* Determine ROM file size */
124 rom_size = ( ( pe_size + sizeof ( *headers ) + 511 ) & ~511 );
125
126 /* Allocate ROM buffer and read in PE file */
127 buf = xmalloc ( rom_size );
128 memset ( buf, 0, rom_size );
129 headers = buf;
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 ) );
134 exit ( 1 );
135 }
136
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? */
154
155 /* Fix image checksum */
156 for ( i = 0, checksum = 0 ; i < rom_size ; i++ )
157 checksum += *( ( uint8_t * ) buf + i );
158 headers->checksum -= checksum;
159
160 /* Write out ROM */
161 if ( fwrite ( buf, rom_size, 1, rom ) != 1 ) {
162 eprintf ( "Could not write ROM file: %s\n",
163 strerror ( errno ) );
164 exit ( 1 );
165 }
166 }
167
168 /**
169 * Print help
170 *
171 * @v program_name Program name
172 */
173 static void print_help ( const char *program_name ) {
174 eprintf ( "Syntax: %s [--vendor=VVVV] [--device=DDDD] "
175 "infile outfile\n", program_name );
176 }
177
178 /**
179 * Parse command-line options
180 *
181 * @v argc Argument count
182 * @v argv Argument list
183 * @v opts Options structure to populate
184 */
185 static int parse_options ( const int argc, char **argv,
186 struct options *opts ) {
187 char *end;
188 int c;
189
190 while (1) {
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' },
196 { 0, 0, 0, 0 }
197 };
198
199 if ( ( c = getopt_long ( argc, argv, "v:d:h",
200 long_options,
201 &option_index ) ) == -1 ) {
202 break;
203 }
204
205 switch ( c ) {
206 case 'v':
207 opts->vendor = strtoul ( optarg, &end, 16 );
208 if ( *end ) {
209 eprintf ( "Invalid vendor \"%s\"\n", optarg );
210 exit ( 2 );
211 }
212 break;
213 case 'd':
214 opts->device = strtoul ( optarg, &end, 16 );
215 if ( *end ) {
216 eprintf ( "Invalid device \"%s\"\n", optarg );
217 exit ( 2 );
218 }
219 break;
220 case 'h':
221 print_help ( argv[0] );
222 exit ( 0 );
223 case '?':
224 default:
225 exit ( 2 );
226 }
227 }
228 return optind;
229 }
230
231 int main ( int argc, char **argv ) {
232 struct options opts;
233 int infile_index;
234 const char *infile_name;
235 const char *outfile_name;
236 FILE *infile;
237 FILE *outfile;
238
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] );
244 exit ( 2 );
245 }
246 infile_name = argv[infile_index];
247 outfile_name = argv[infile_index + 1];
248
249 /* Open input and output files */
250 infile = fopen ( infile_name, "r" );
251 if ( ! infile ) {
252 eprintf ( "Could not open %s for reading: %s\n",
253 infile_name, strerror ( errno ) );
254 exit ( 1 );
255 }
256 outfile = fopen ( outfile_name, "w" );
257 if ( ! outfile ) {
258 eprintf ( "Could not open %s for writing: %s\n",
259 outfile_name, strerror ( errno ) );
260 exit ( 1 );
261 }
262
263 /* Convert file */
264 make_efi_rom ( infile, outfile, &opts );
265
266 fclose ( outfile );
267 fclose ( infile );
268
269 return 0;
270 }