Browse Source

[efi] Add efifatbin utility

Add utility for constructing EFI fat binaries (dual 32/64-bit
binaries, usable only on Apple EFI systems).

This utility is not part of the standard build process.  To use it:

  make util/efifatbin bin-i386-efi/ipxe.efi bin-x86_64-efi/ipxe.efi

and then

  ./util/efifatbin bin-*-efi/ipxe.efi fat-ipxe.efi

Requested-by: Brandon Penglase <bpenglase-ipxe@spaceservices.net>
Tested-by: Brandon Penglase <bpenglase-ipxe@spaceservices.net>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 years ago
parent
commit
5de37e124f
4 changed files with 267 additions and 0 deletions
  1. 1
    0
      src/Makefile
  2. 5
    0
      src/Makefile.housekeeping
  3. 1
    0
      src/util/.gitignore
  4. 260
    0
      src/util/efifatbin.c

+ 1
- 0
src/Makefile View File

@@ -46,6 +46,7 @@ ZBIN		:= ./util/zbin
46 46
 ELF2EFI32	:= ./util/elf2efi32
47 47
 ELF2EFI64	:= ./util/elf2efi64
48 48
 EFIROM		:= ./util/efirom
49
+EFIFATBIN	:= ./util/efifatbin
49 50
 ICCFIX		:= ./util/iccfix
50 51
 EINFO		:= ./util/einfo
51 52
 GENKEYMAP	:= ./util/genkeymap.pl

+ 5
- 0
src/Makefile.housekeeping View File

@@ -1268,6 +1268,11 @@ $(EFIROM) : util/efirom.c $(MAKEDEPS)
1268 1268
 	$(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
1269 1269
 CLEANUP += $(EFIROM)
1270 1270
 
1271
+$(EFIFATBIN) : util/efifatbin.c $(MAKEDEPS)
1272
+	$(QM)$(ECHO) "  [HOSTCC] $@"
1273
+	$(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
1274
+CLEANUP += $(EFIFATBIN)
1275
+
1271 1276
 ###############################################################################
1272 1277
 #
1273 1278
 # The ICC fixup utility

+ 1
- 0
src/util/.gitignore View File

@@ -5,5 +5,6 @@ prototester
5 5
 elf2efi32
6 6
 elf2efi64
7 7
 efirom
8
+efifatbin
8 9
 iccfix
9 10
 einfo

+ 260
- 0
src/util/efifatbin.c View File

@@ -0,0 +1,260 @@
1
+/*
2
+ * Copyright (C) 2014 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
+#include <stdint.h>
21
+#include <stddef.h>
22
+#include <stdlib.h>
23
+#include <stdio.h>
24
+#include <string.h>
25
+#include <sys/stat.h>
26
+#include <unistd.h>
27
+#include <errno.h>
28
+#include <assert.h>
29
+#include <getopt.h>
30
+#include <ipxe/efi/efi.h>
31
+#include <ipxe/efi/IndustryStandard/PeImage.h>
32
+
33
+#define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
34
+
35
+/** Command-line options */
36
+struct options {
37
+};
38
+
39
+/** EFI fat binary file header */
40
+struct efifatbin_file_header {
41
+	/** Signature */
42
+	uint32_t signature;
43
+	/** Count */
44
+	uint32_t count;
45
+} __attribute__ (( packed ));
46
+
47
+/** EFI fat binary signature */
48
+#define EFIFATBIN_SIGNATURE 0x0ef1fab9
49
+
50
+/** EFI fat binary image header */
51
+struct efifatbin_image_header {
52
+	/** Flags */
53
+	uint64_t flags;
54
+	/** Offset */
55
+	uint32_t offset;
56
+	/** Length */
57
+	uint32_t len;
58
+	/** Padding */
59
+	uint32_t pad;
60
+} __attribute__ (( packed ));
61
+
62
+/** EFI fat binary default flags */
63
+#define EFIFATBIN_FLAGS 0x0000000300000007ULL
64
+
65
+/** EFI fat binary 64-bit flag */
66
+#define EFIFATBIN_64BIT 0x0000000001000000ULL
67
+
68
+/**
69
+ * Allocate memory
70
+ *
71
+ * @v len		Length of memory to allocate
72
+ * @ret ptr		Pointer to allocated memory
73
+ */
74
+static void * xmalloc ( size_t len ) {
75
+	void *ptr;
76
+
77
+	ptr = malloc ( len );
78
+	if ( ! ptr ) {
79
+		eprintf ( "Could not allocate %zd bytes\n", len );
80
+		exit ( 1 );
81
+	}
82
+
83
+	return ptr;
84
+}
85
+
86
+/**
87
+ * Generate EFI fat binary
88
+ *
89
+ * @v count		Number of input files
90
+ * @v infile_names	Input filenames
91
+ * @v outfile_name	Output filename
92
+ */
93
+static void make_efifatbin ( unsigned int count, char **infile_names,
94
+			     const char *outfile_name ) {
95
+	FILE *infile[count];
96
+	FILE *outfile;
97
+	struct stat stat[count];
98
+	void *buf[count];
99
+	struct efifatbin_file_header file_header;
100
+	struct efifatbin_image_header header[count];
101
+	size_t offset;
102
+	EFI_IMAGE_DOS_HEADER *dos;
103
+	union {
104
+		EFI_IMAGE_NT_HEADERS32 nt32;
105
+		EFI_IMAGE_NT_HEADERS64 nt64;
106
+	} *nt;
107
+	unsigned int i;
108
+
109
+	/* Generate file header */
110
+	file_header.signature = EFIFATBIN_SIGNATURE;
111
+	file_header.count = count;
112
+	offset = ( sizeof ( file_header ) + sizeof ( header ) );
113
+
114
+	/* Process input files */
115
+	for ( i = 0 ; i < count ; i++ ) {
116
+
117
+		/* Open input file */
118
+		infile[i] = fopen ( infile_names[i], "r" );
119
+		if ( ! infile[i] ) {
120
+			eprintf ( "Could not open %s for reading: %s\n",
121
+				  infile_names[i], strerror ( errno ) );
122
+			exit ( 1 );
123
+		}
124
+
125
+		/* Determine PE file size */
126
+		if ( fstat ( fileno ( infile[i] ), &stat[i] ) != 0 ) {
127
+			eprintf ( "Could not stat %s: %s\n",
128
+				  infile_names[i], strerror ( errno ) );
129
+			exit ( 1 );
130
+		}
131
+
132
+		/* Allocate buffer and read in PE file */
133
+		buf[i] = xmalloc ( stat[i].st_size );
134
+		if ( fread ( buf[i], stat[i].st_size, 1, infile[i] ) != 1 ) {
135
+			eprintf ( "Could not read %s: %s\n",
136
+				  infile_names[i], strerror ( errno ) );
137
+			exit ( 1 );
138
+		}
139
+
140
+		/* Close input file */
141
+		fclose ( infile[i] );
142
+
143
+		/* Generate image header */
144
+		header[i].flags = EFIFATBIN_FLAGS;
145
+		header[i].offset = offset;
146
+		header[i].len = stat[i].st_size;
147
+		header[i].pad = 0;
148
+
149
+		/* Determine architecture */
150
+		dos = buf[i];
151
+		nt = ( buf[i] + dos->e_lfanew );
152
+		if ( nt->nt32.FileHeader.Machine == EFI_IMAGE_MACHINE_X64 )
153
+			header[i].flags |= EFIFATBIN_64BIT;
154
+
155
+		/* Allow space for this image */
156
+		offset += stat[i].st_size;
157
+	}
158
+
159
+	/* Open output file */
160
+	outfile = fopen ( outfile_name, "w" );
161
+	if ( ! outfile ) {
162
+		eprintf ( "Could not open %s for writing: %s\n",
163
+			  outfile_name, strerror ( errno ) );
164
+		exit ( 1 );
165
+	}
166
+
167
+	/* Write fat binary header */
168
+	if ( fwrite ( &file_header, sizeof ( file_header ), 1, outfile ) != 1 ){
169
+		eprintf ( "Could not write %s: %s\n",
170
+			  outfile_name, strerror ( errno ) );
171
+		exit ( 1 );
172
+	}
173
+	for ( i = 0 ; i < count ; i++ ) {
174
+		if ( fwrite ( &header[i], sizeof ( header[i] ), 1,
175
+			      outfile ) != 1 ) {
176
+			eprintf ( "Could not write %s: %s\n",
177
+				  outfile_name, strerror ( errno ) );
178
+			exit ( 1 );
179
+		}
180
+	}
181
+
182
+	/* Write images */
183
+	for ( i = 0 ; i < count ; i++ ) {
184
+		if ( fwrite ( buf[i], stat[i].st_size, 1, outfile ) != 1 ) {
185
+			eprintf ( "Could not write %s: %s\n",
186
+				  outfile_name, strerror ( errno ) );
187
+			exit ( 1 );
188
+		}
189
+	}
190
+
191
+	/* Close output file */
192
+	fclose ( outfile );
193
+}
194
+
195
+/**
196
+ * Print help
197
+ *
198
+ * @v program_name	Program name
199
+ */
200
+static void print_help ( const char *program_name ) {
201
+	eprintf ( "Syntax: %s infile [infile...] outfile\n", program_name );
202
+}
203
+
204
+/**
205
+ * Parse command-line options
206
+ *
207
+ * @v argc		Argument count
208
+ * @v argv		Argument list
209
+ * @v opts		Options structure to populate
210
+ */
211
+static int parse_options ( const int argc, char **argv,
212
+			   struct options *opts __attribute__ (( unused )) ) {
213
+	int c;
214
+
215
+	while (1) {
216
+		int option_index = 0;
217
+		static struct option long_options[] = {
218
+			{ "help", 0, NULL, 'h' },
219
+			{ 0, 0, 0, 0 }
220
+		};
221
+
222
+		if ( ( c = getopt_long ( argc, argv, "h",
223
+					 long_options,
224
+					 &option_index ) ) == -1 ) {
225
+			break;
226
+		}
227
+
228
+		switch ( c ) {
229
+		case 'h':
230
+			print_help ( argv[0] );
231
+			exit ( 0 );
232
+		case '?':
233
+		default:
234
+			exit ( 2 );
235
+		}
236
+	}
237
+	return optind;
238
+}
239
+
240
+int main ( int argc, char **argv ) {
241
+	struct options opts;
242
+	int infile_index;
243
+	int outfile_index;
244
+	int count;
245
+
246
+	/* Parse command-line arguments */
247
+	memset ( &opts, 0, sizeof ( opts ) );
248
+	infile_index = parse_options ( argc, argv, &opts );
249
+	outfile_index = ( argc - 1 );
250
+	count = ( outfile_index - infile_index );
251
+	if ( count <= 0 ) {
252
+		print_help ( argv[0] );
253
+		exit ( 2 );
254
+	}
255
+
256
+	/* Generate fat binary */
257
+	make_efifatbin ( count, &argv[infile_index], argv[outfile_index] );
258
+
259
+	return 0;
260
+}

Loading…
Cancel
Save