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 6.5KB

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