You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

efifatbin.c 6.1KB

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