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

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