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.

efirom.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #include <stdint.h>
  19. #include <stddef.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <errno.h>
  25. #include <assert.h>
  26. #include <getopt.h>
  27. #include <gpxe/efi/efi.h>
  28. #include <gpxe/efi/IndustryStandard/PeImage.h>
  29. #include <gpxe/efi/IndustryStandard/Pci22.h>
  30. #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
  31. /** Command-line options */
  32. struct options {
  33. uint16_t vendor;
  34. uint16_t device;
  35. };
  36. /**
  37. * Allocate memory
  38. *
  39. * @v len Length of memory to allocate
  40. * @ret ptr Pointer to allocated memory
  41. */
  42. static void * xmalloc ( size_t len ) {
  43. void *ptr;
  44. ptr = malloc ( len );
  45. if ( ! ptr ) {
  46. eprintf ( "Could not allocate %zd bytes\n", len );
  47. exit ( 1 );
  48. }
  49. return ptr;
  50. }
  51. /**
  52. * Get file size
  53. *
  54. * @v file File
  55. * @v len File size
  56. */
  57. static size_t file_size ( FILE *file ) {
  58. ssize_t len;
  59. if ( fseek ( file, 0, SEEK_END ) != 0 ) {
  60. eprintf ( "Could not seek: %s\n", strerror ( errno ) );
  61. exit ( 1 );
  62. }
  63. len = ftell ( file );
  64. if ( len < 0 ) {
  65. eprintf ( "Could not determine file size: %s\n",
  66. strerror ( errno ) );
  67. exit ( 1 );
  68. }
  69. return len;
  70. }
  71. /**
  72. * Copy file
  73. *
  74. * @v in Input file
  75. * @v out Output file
  76. * @v len Length to copy
  77. */
  78. static void file_copy ( FILE *in, FILE *out, size_t len ) {
  79. char buf[4096];
  80. size_t frag_len;
  81. while ( len ) {
  82. frag_len = len;
  83. if ( frag_len > sizeof ( buf ) )
  84. frag_len = sizeof ( buf );
  85. if ( fread ( buf, frag_len, 1, in ) != 1 ) {
  86. eprintf ( "Could not read: %s\n",
  87. strerror ( errno ) );
  88. exit ( 1 );
  89. }
  90. if ( fwrite ( buf, frag_len, 1, out ) != 1 ) {
  91. eprintf ( "Could not write: %s\n",
  92. strerror ( errno ) );
  93. exit ( 1 );
  94. }
  95. len -= frag_len;
  96. }
  97. }
  98. /**
  99. * Read information from PE headers
  100. *
  101. * @v pe PE file
  102. * @ret machine Machine type
  103. * @ret subsystem EFI subsystem
  104. */
  105. static void read_pe_info ( FILE *pe, uint16_t *machine,
  106. uint16_t *subsystem ) {
  107. EFI_IMAGE_DOS_HEADER dos;
  108. union {
  109. EFI_IMAGE_NT_HEADERS32 nt32;
  110. EFI_IMAGE_NT_HEADERS64 nt64;
  111. } nt;
  112. /* Read DOS header */
  113. if ( fseek ( pe, 0, SEEK_SET ) != 0 ) {
  114. eprintf ( "Could not seek: %s\n", strerror ( errno ) );
  115. exit ( 1 );
  116. }
  117. if ( fread ( &dos, sizeof ( dos ), 1, pe ) != 1 ) {
  118. eprintf ( "Could not read: %s\n", strerror ( errno ) );
  119. exit ( 1 );
  120. }
  121. /* Read NT header */
  122. if ( fseek ( pe, dos.e_lfanew, SEEK_SET ) != 0 ) {
  123. eprintf ( "Could not seek: %s\n", strerror ( errno ) );
  124. exit ( 1 );
  125. }
  126. if ( fread ( &nt, sizeof ( nt ), 1, pe ) != 1 ) {
  127. eprintf ( "Could not read: %s\n", strerror ( errno ) );
  128. exit ( 1 );
  129. }
  130. /* Locate NT header */
  131. *machine = nt.nt32.FileHeader.Machine;
  132. switch ( *machine ) {
  133. case EFI_IMAGE_MACHINE_IA32:
  134. *subsystem = nt.nt32.OptionalHeader.Subsystem;
  135. break;
  136. case EFI_IMAGE_MACHINE_X64:
  137. *subsystem = nt.nt64.OptionalHeader.Subsystem;
  138. break;
  139. default:
  140. eprintf ( "Unrecognised machine type %04x\n", *machine );
  141. exit ( 1 );
  142. }
  143. }
  144. /**
  145. * Convert EFI image to ROM image
  146. *
  147. * @v pe EFI file
  148. * @v rom ROM file
  149. */
  150. static void make_efi_rom ( FILE *pe, FILE *rom, struct options *opts ) {
  151. struct {
  152. EFI_PCI_EXPANSION_ROM_HEADER rom;
  153. PCI_DATA_STRUCTURE pci __attribute__ (( aligned ( 4 ) ));
  154. } headers;
  155. size_t pe_size;
  156. size_t rom_size;
  157. unsigned int rom_size_sectors;
  158. /* Determine output file size */
  159. pe_size = file_size ( pe );
  160. rom_size = ( pe_size + sizeof ( headers ) );
  161. rom_size_sectors = ( ( rom_size + 511 ) / 512 );
  162. /* Construct ROM header */
  163. memset ( &headers, 0, sizeof ( headers ) );
  164. headers.rom.Signature = PCI_EXPANSION_ROM_HEADER_SIGNATURE;
  165. headers.rom.InitializationSize = rom_size_sectors;
  166. headers.rom.EfiSignature = EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE;
  167. read_pe_info ( pe, &headers.rom.EfiMachineType,
  168. &headers.rom.EfiSubsystem );
  169. headers.rom.EfiImageHeaderOffset = sizeof ( headers );
  170. headers.rom.PcirOffset =
  171. offsetof ( typeof ( headers ), pci );
  172. headers.pci.Signature = PCI_DATA_STRUCTURE_SIGNATURE;
  173. headers.pci.VendorId = opts->vendor;
  174. headers.pci.DeviceId = opts->device;
  175. headers.pci.Length = sizeof ( headers.pci );
  176. headers.pci.ClassCode[0] = PCI_CLASS_NETWORK;
  177. headers.pci.ImageLength = rom_size_sectors;
  178. headers.pci.CodeType = 0x03; /* No constant in EFI headers? */
  179. headers.pci.Indicator = 0x80; /* No constant in EFI headers? */
  180. /* Write out ROM header */
  181. if ( fwrite ( &headers, sizeof ( headers ), 1, rom ) != 1 ) {
  182. eprintf ( "Could not write headers: %s\n",
  183. strerror ( errno ) );
  184. exit ( 1 );
  185. }
  186. /* Write out payload */
  187. if ( fseek ( pe, 0, SEEK_SET ) != 0 ) {
  188. eprintf ( "Could not seek: %s\n", strerror ( errno ) );
  189. exit ( 1 );
  190. }
  191. file_copy ( pe, rom, pe_size );
  192. /* Round up to 512-byte boundary */
  193. if ( ftruncate ( fileno ( rom ), ( rom_size_sectors * 512 ) ) != 0 ) {
  194. eprintf ( "Could not set length: %s\n", strerror ( errno ) );
  195. exit ( 1 );
  196. }
  197. }
  198. /**
  199. * Print help
  200. *
  201. * @v program_name Program name
  202. */
  203. static void print_help ( const char *program_name ) {
  204. eprintf ( "Syntax: %s [--vendor=VVVV] [--device=DDDD] "
  205. "infile outfile\n", program_name );
  206. }
  207. /**
  208. * Parse command-line options
  209. *
  210. * @v argc Argument count
  211. * @v argv Argument list
  212. * @v opts Options structure to populate
  213. */
  214. static int parse_options ( const int argc, char **argv,
  215. struct options *opts ) {
  216. char *end;
  217. int c;
  218. while (1) {
  219. int option_index = 0;
  220. static struct option long_options[] = {
  221. { "vendor", required_argument, NULL, 'v' },
  222. { "device", required_argument, NULL, 'd' },
  223. { "help", 0, NULL, 'h' },
  224. { 0, 0, 0, 0 }
  225. };
  226. if ( ( c = getopt_long ( argc, argv, "v:d:h",
  227. long_options,
  228. &option_index ) ) == -1 ) {
  229. break;
  230. }
  231. switch ( c ) {
  232. case 'v':
  233. opts->vendor = strtoul ( optarg, &end, 16 );
  234. if ( *end ) {
  235. eprintf ( "Invalid vendor \"%s\"\n", optarg );
  236. exit ( 2 );
  237. }
  238. break;
  239. case 'd':
  240. opts->device = strtoul ( optarg, &end, 16 );
  241. if ( *end ) {
  242. eprintf ( "Invalid device \"%s\"\n", optarg );
  243. exit ( 2 );
  244. }
  245. break;
  246. case 'h':
  247. print_help ( argv[0] );
  248. exit ( 0 );
  249. case '?':
  250. default:
  251. exit ( 2 );
  252. }
  253. }
  254. return optind;
  255. }
  256. int main ( int argc, char **argv ) {
  257. struct options opts = {
  258. };
  259. unsigned int infile_index;
  260. const char *infile_name;
  261. const char *outfile_name;
  262. FILE *infile;
  263. FILE *outfile;
  264. /* Parse command-line arguments */
  265. infile_index = parse_options ( argc, argv, &opts );
  266. if ( argc != ( infile_index + 2 ) ) {
  267. print_help ( argv[0] );
  268. exit ( 2 );
  269. }
  270. infile_name = argv[infile_index];
  271. outfile_name = argv[infile_index + 1];
  272. /* Open input and output files */
  273. infile = fopen ( infile_name, "r" );
  274. if ( ! infile ) {
  275. eprintf ( "Could not open %s for reading: %s\n",
  276. infile_name, strerror ( errno ) );
  277. exit ( 1 );
  278. }
  279. outfile = fopen ( outfile_name, "w" );
  280. if ( ! outfile ) {
  281. eprintf ( "Could not open %s for writing: %s\n",
  282. outfile_name, strerror ( errno ) );
  283. exit ( 1 );
  284. }
  285. /* Convert file */
  286. make_efi_rom ( infile, outfile, &opts );
  287. fclose ( outfile );
  288. fclose ( infile );
  289. return 0;
  290. }