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

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