Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

efirom.c 6.5KB

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