2 * Copyright (C) 2014 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>
33 #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
35 /** Command-line options */
39 /** EFI fat binary file header */
40 struct efifatbin_file_header
{
45 } __attribute__ (( packed
));
47 /** EFI fat binary signature */
48 #define EFIFATBIN_SIGNATURE 0x0ef1fab9
50 /** EFI fat binary image header */
51 struct efifatbin_image_header
{
60 } __attribute__ (( packed
));
62 /** EFI fat binary default flags */
63 #define EFIFATBIN_FLAGS 0x0000000300000007ULL
65 /** EFI fat binary 64-bit flag */
66 #define EFIFATBIN_64BIT 0x0000000001000000ULL
71 * @v len Length of memory to allocate
72 * @ret ptr Pointer to allocated memory
74 static void * xmalloc ( size_t len
) {
79 eprintf ( "Could not allocate %zd bytes\n", len
);
87 * Generate EFI fat binary
89 * @v count Number of input files
90 * @v infile_names Input filenames
91 * @v outfile_name Output filename
93 static void make_efifatbin ( unsigned int count
, char **infile_names
,
94 const char *outfile_name
) {
97 struct stat stat
[count
];
99 struct efifatbin_file_header file_header
;
100 struct efifatbin_image_header header
[count
];
102 EFI_IMAGE_DOS_HEADER
*dos
;
104 EFI_IMAGE_NT_HEADERS32 nt32
;
105 EFI_IMAGE_NT_HEADERS64 nt64
;
109 /* Generate file header */
110 file_header
.signature
= EFIFATBIN_SIGNATURE
;
111 file_header
.count
= count
;
112 offset
= ( sizeof ( file_header
) + sizeof ( header
) );
114 /* Process input files */
115 for ( i
= 0 ; i
< count
; i
++ ) {
117 /* Open input file */
118 infile
[i
] = fopen ( infile_names
[i
], "r" );
120 eprintf ( "Could not open %s for reading: %s\n",
121 infile_names
[i
], strerror ( errno
) );
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
) );
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
) );
140 /* Close input file */
141 fclose ( infile
[i
] );
143 /* Generate image header */
144 header
[i
].flags
= EFIFATBIN_FLAGS
;
145 header
[i
].offset
= offset
;
146 header
[i
].len
= stat
[i
].st_size
;
149 /* Determine architecture */
151 nt
= ( buf
[i
] + dos
->e_lfanew
);
152 if ( nt
->nt32
.FileHeader
.Machine
== EFI_IMAGE_MACHINE_X64
)
153 header
[i
].flags
|= EFIFATBIN_64BIT
;
155 /* Allow space for this image */
156 offset
+= stat
[i
].st_size
;
159 /* Open output file */
160 outfile
= fopen ( outfile_name
, "w" );
162 eprintf ( "Could not open %s for writing: %s\n",
163 outfile_name
, strerror ( errno
) );
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
) );
173 for ( i
= 0 ; i
< count
; i
++ ) {
174 if ( fwrite ( &header
[i
], sizeof ( header
[i
] ), 1,
176 eprintf ( "Could not write %s: %s\n",
177 outfile_name
, strerror ( errno
) );
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
) );
191 /* Close output file */
198 * @v program_name Program name
200 static void print_help ( const char *program_name
) {
201 eprintf ( "Syntax: %s infile [infile...] outfile\n", program_name
);
205 * Parse command-line options
207 * @v argc Argument count
208 * @v argv Argument list
209 * @v opts Options structure to populate
211 static int parse_options ( const int argc
, char **argv
,
212 struct options
*opts
__attribute__ (( unused
)) ) {
216 int option_index
= 0;
217 static struct option long_options
[] = {
218 { "help", 0, NULL
, 'h' },
222 if ( ( c
= getopt_long ( argc
, argv
, "h",
224 &option_index
) ) == -1 ) {
230 print_help ( argv
[0] );
240 int main ( int argc
, char **argv
) {
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
);
252 print_help ( argv
[0] );
256 /* Generate fat binary */
257 make_efifatbin ( count
, &argv
[infile_index
], argv
[outfile_index
] );