您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

efifatbin.c 6.0KB

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