efirom.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * modifications for gnuefi by bzt (bztsrc@gitlab)
  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 <efi.h>
  31. #include <efilink.h>
  32. #include <pe.h>
  33. #include <pci22.h>
  34. #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
  35. /** Command-line options */
  36. struct options {
  37. uint16_t vendor;
  38. uint16_t device;
  39. };
  40. /**
  41. * Allocate memory
  42. *
  43. * @v len Length of memory to allocate
  44. * @ret ptr Pointer to allocated memory
  45. */
  46. static void * xmalloc ( size_t len ) {
  47. void *ptr;
  48. ptr = malloc ( len );
  49. if ( ! ptr ) {
  50. eprintf ( "Could not allocate %zd bytes\n", len );
  51. exit ( 1 );
  52. }
  53. return ptr;
  54. }
  55. /**
  56. * Get file size
  57. *
  58. * @v file File
  59. * @v len File size
  60. */
  61. /*
  62. static size_t file_size ( FILE *file ) {
  63. ssize_t len;
  64. return len;
  65. }
  66. */
  67. /**
  68. * Read information from PE headers
  69. *
  70. * @v pe PE file
  71. * @ret machine Machine type
  72. * @ret subsystem EFI subsystem
  73. */
  74. static void read_pe_info ( void *pe, uint16_t *machine,
  75. uint16_t *subsystem ) {
  76. IMAGE_DOS_HEADER *dos;
  77. union {
  78. IMAGE_NT_HEADERS nt64;
  79. } *nt;
  80. /* Locate NT header */
  81. dos = pe;
  82. nt = ( pe + dos->e_lfanew );
  83. /* issue 4: TianoCore demands subsystem 10, so we must use EFI_APPLICATION
  84. * in the PE header. Therefore we force EFI_ROM subsystem in this code here. */
  85. if(nt->nt64.OptionalHeader.Subsystem == 10)
  86. nt->nt64.OptionalHeader.Subsystem = 13;
  87. /* Parse out PE information */
  88. *machine = nt->nt64.FileHeader.Machine;
  89. *subsystem = nt->nt64.OptionalHeader.Subsystem;
  90. }
  91. /**
  92. * Convert EFI image to ROM image
  93. *
  94. * @v pe EFI file
  95. * @v rom ROM file
  96. */
  97. static void make_efi_rom ( FILE *pe, FILE *rom, struct options *opts ) {
  98. struct {
  99. EFI_PCI_EXPANSION_ROM_HEADER rom;
  100. PCI_DATA_STRUCTURE pci __attribute__ (( aligned ( 4 ) ));
  101. uint8_t checksum;
  102. } *headers;
  103. struct stat pe_stat;
  104. size_t pe_size;
  105. size_t rom_size;
  106. void *buf;
  107. void *payload;
  108. unsigned int i;
  109. uint8_t checksum;
  110. /* Determine PE file size */
  111. if ( fstat ( fileno ( pe ), &pe_stat ) != 0 ) {
  112. eprintf ( "Could not stat PE file: %s\n",
  113. strerror ( errno ) );
  114. exit ( 1 );
  115. }
  116. pe_size = pe_stat.st_size;
  117. /* Determine ROM file size */
  118. rom_size = ( ( pe_size + sizeof ( *headers ) + 511 ) & ~511 );
  119. /* Allocate ROM buffer and read in PE file */
  120. buf = xmalloc ( rom_size );
  121. memset ( buf, 0, rom_size );
  122. headers = buf;
  123. payload = ( buf + sizeof ( *headers ) );
  124. if ( fread ( payload, pe_size, 1, pe ) != 1 ) {
  125. eprintf ( "Could not read PE file: %s\n",
  126. strerror ( errno ) );
  127. exit ( 1 );
  128. }
  129. /* Construct ROM header */
  130. headers->rom.Signature = PCI_EXPANSION_ROM_HEADER_SIGNATURE;
  131. headers->rom.InitializationSize = ( rom_size / 512 );
  132. headers->rom.EfiSignature = EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE;
  133. read_pe_info ( payload, &headers->rom.EfiMachineType,
  134. &headers->rom.EfiSubsystem );
  135. headers->rom.EfiImageHeaderOffset = sizeof ( *headers );
  136. headers->rom.PcirOffset =
  137. offsetof ( typeof ( *headers ), pci );
  138. headers->pci.Signature = PCI_DATA_STRUCTURE_SIGNATURE;
  139. headers->pci.VendorId = opts->vendor ? opts->vendor : 0x8086;
  140. headers->pci.DeviceId = opts->device ? opts->device : 0x100E;
  141. headers->pci.Length = sizeof ( headers->pci );
  142. headers->pci.ClassCode[0] = PCI_CLASS_NETWORK;
  143. headers->pci.ImageLength = ( rom_size / 512 );
  144. headers->pci.CodeType = 0x03; /* No constant in EFI headers? */
  145. headers->pci.Indicator = 0x80; /* No constant in EFI headers? */
  146. /* Fix image checksum */
  147. for ( i = 0, checksum = 0 ; i < rom_size ; i++ )
  148. checksum += *( ( uint8_t * ) buf + i );
  149. headers->checksum -= checksum;
  150. /* Write out ROM */
  151. if ( fwrite ( buf, rom_size, 1, rom ) != 1 ) {
  152. eprintf ( "Could not write ROM file: %s\n",
  153. strerror ( errno ) );
  154. exit ( 1 );
  155. }
  156. }
  157. /**
  158. * Print help
  159. *
  160. * @v program_name Program name
  161. */
  162. static void print_help ( const char *program_name ) {
  163. eprintf ( "Syntax: %s [--vendor=VVVV] [--device=DDDD] "
  164. "infile outfile\n", program_name );
  165. }
  166. /**
  167. * Parse command-line options
  168. *
  169. * @v argc Argument count
  170. * @v argv Argument list
  171. * @v opts Options structure to populate
  172. */
  173. static int parse_options ( const int argc, char **argv,
  174. struct options *opts ) {
  175. char *end;
  176. int c;
  177. while (1) {
  178. int option_index = 0;
  179. static struct option long_options[] = {
  180. { "vendor", required_argument, NULL, 'v' },
  181. { "device", required_argument, NULL, 'd' },
  182. { "help", 0, NULL, 'h' },
  183. { 0, 0, 0, 0 }
  184. };
  185. if ( ( c = getopt_long ( argc, argv, "v:d:h",
  186. long_options,
  187. &option_index ) ) == -1 ) {
  188. break;
  189. }
  190. switch ( c ) {
  191. case 'v':
  192. opts->vendor = strtoul ( optarg, &end, 16 );
  193. if ( *end ) {
  194. eprintf ( "Invalid vendor \"%s\"\n", optarg );
  195. exit ( 2 );
  196. }
  197. break;
  198. case 'd':
  199. opts->device = strtoul ( optarg, &end, 16 );
  200. if ( *end ) {
  201. eprintf ( "Invalid device \"%s\"\n", optarg );
  202. exit ( 2 );
  203. }
  204. break;
  205. case 'h':
  206. print_help ( argv[0] );
  207. exit ( 0 );
  208. case '?':
  209. default:
  210. exit ( 2 );
  211. }
  212. }
  213. return optind;
  214. }
  215. int main ( int argc, char **argv ) {
  216. struct options opts = {
  217. };
  218. unsigned int infile_index;
  219. const char *infile_name;
  220. const char *outfile_name;
  221. FILE *infile;
  222. FILE *outfile;
  223. /* Parse command-line arguments */
  224. infile_index = parse_options ( argc, argv, &opts );
  225. if ( argc != ( infile_index + 2 ) ) {
  226. print_help ( argv[0] );
  227. exit ( 2 );
  228. }
  229. infile_name = argv[infile_index];
  230. outfile_name = argv[infile_index + 1];
  231. /* Open input and output files */
  232. infile = fopen ( infile_name, "r" );
  233. if ( ! infile ) {
  234. eprintf ( "Could not open %s for reading: %s\n",
  235. infile_name, strerror ( errno ) );
  236. exit ( 1 );
  237. }
  238. outfile = fopen ( outfile_name, "w" );
  239. if ( ! outfile ) {
  240. eprintf ( "Could not open %s for writing: %s\n",
  241. outfile_name, strerror ( errno ) );
  242. exit ( 1 );
  243. }
  244. /* Convert file */
  245. make_efi_rom ( infile, outfile, &opts );
  246. fclose ( outfile );
  247. fclose ( infile );
  248. return 0;
  249. }