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.6KB

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