hcsmakeimage.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <stdio.h>
  4. #include <inttypes.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #include <unistd.h>
  8. #include <sys/time.h>
  9. #include <sys/stat.h>
  10. #include <libgen.h>
  11. #include "bcmalgo.h"
  12. int flag_print_version;
  13. int flag_print_help;
  14. int flag_compress;
  15. uint16_t sa2100_magic = 0x2100;
  16. uint16_t sa3349_magic = 0x3349;
  17. uint32_t default_date = 0x00000000; //A long time ago in a galaxy far far away....
  18. uint32_t default_load_address = 0x80010000; //The default load_address for the firmware image
  19. static void print_help ( const char* ename )
  20. {
  21. printf ( "Firmware image packer and calculator for broadcom-based modems.\n" );
  22. printf ( "Part of bcm-utils package.\n" );
  23. printf ( "(c) 2009 Necromant (http://necromant.ath.cx). Thanks to Luke-jr for his initial work.\n" );
  24. printf ( "usage: %s [options]\n", ename );
  25. printf ( "Valid options are:\n" );
  26. printf ( "--magic_bytes=value \t- specify magic bytes at the beginning of the image. default - 3349\n" );
  27. printf ( "\t\t\t these can be sa2100 (for DPC2100 modem),\n\t\t\t sa3349 (haxorware guys use this one for some reason),\n\t\t\t or a custom hex value e.g. 0xFFFF\n" );
  28. printf ( "--compress \t\t - Make use of LZMA (weird!) compression (Doesn't work yet).\n" );
  29. printf ( "--rev_maj=value\t\t - major revision number. default 0\n" );
  30. printf ( "--rev_min=value\t\t - minor revision number default 0\n" );
  31. printf ( "--filename=value\t - use this filename in header instead of default (input filename)\n" );
  32. printf ( "--ldaddress=value\t - hex value of the target load address. defaults to 0x80010000\n" );
  33. printf ( "--input_file=value\t - What file are we packing?\n" );
  34. printf ( "--output_file=value\t - What file shall we write? (default: image.bin)\n" );
  35. #ifdef _HAX0RSTYLE
  36. printf ( "--credz\t - Give some credz!\n" );
  37. #endif
  38. printf ( "\n" );
  39. }
  40. int main ( int argc, char** argv )
  41. {
  42. if ( argc<2 )
  43. {
  44. print_help ( argv[0] );
  45. }
  46. static struct option long_options[] =
  47. {
  48. {"magic_bytes", required_argument, 0, 'm'},
  49. {"rev_maj", required_argument, 0, 'j'},
  50. {"rev_min", required_argument, 0, 'n'},
  51. {"ldaddress", required_argument, 0, 'l'},
  52. {"filename", required_argument, 0, 'f'},
  53. {"input_file", required_argument, 0, 'i'},
  54. {"output_file", required_argument, 0, 'o'},
  55. {"compress", no_argument, &flag_compress, 'c'},
  56. {"version", no_argument, &flag_print_version, 'v'},
  57. {"help", no_argument, &flag_print_help, 'h'},
  58. {0, 0, 0, 0}
  59. };
  60. int option_index = 0;
  61. int opt_result=0;
  62. char* filename=NULL;
  63. char* input=NULL;
  64. char* magic=NULL;
  65. char* major=NULL;
  66. char* minor=NULL;
  67. char* ldaddr=NULL;
  68. char* output=NULL;
  69. while ( opt_result>=0 )
  70. {
  71. opt_result = getopt_long ( argc, argv, "m:j:n:f:i:o:vh", long_options, &option_index );
  72. switch ( opt_result )
  73. {
  74. case 0:
  75. printf ( "o!\n" );
  76. break;
  77. case 'h':
  78. print_help ( argv[0] );
  79. break;
  80. case 'l':
  81. ldaddr=optarg;
  82. break;
  83. case 'f':
  84. filename=optarg;
  85. break;
  86. case 'i':
  87. input=optarg;
  88. break;
  89. case 'o':
  90. output=optarg;
  91. break;
  92. case 'm':
  93. magic=optarg;
  94. break;
  95. case 'j':
  96. major=optarg;
  97. break;
  98. case 'n':
  99. minor=optarg;
  100. break;
  101. }
  102. }
  103. if ( input==NULL )
  104. {
  105. printf ( "Telepaths are still on holidays. I guess you should tell me what file should I process.\n\n" );
  106. exit ( 1 );
  107. }
  108. if ( access ( input,R_OK ) !=0 )
  109. {
  110. printf ( "I cannot access the file %s. Is it there? Am I allowed?\n\n", input );
  111. exit ( 1 );
  112. }
  113. uint32_t magicnum=sa2100_magic;
  114. if ( magic )
  115. {
  116. if ( strcmp ( magic,"sa2100" ) ==0 ) magicnum=sa2100_magic; else
  117. if ( strcmp ( magic,"sa3349" ) ==0 ) magicnum=sa3349_magic; else
  118. {
  119. sscanf ( magic, "0x%04X", &magicnum );
  120. }
  121. }
  122. unsigned int majrev=0;
  123. if ( major )
  124. {
  125. sscanf ( major, "%d", &majrev );
  126. }
  127. unsigned int minrev=0;
  128. if ( minor )
  129. {
  130. sscanf ( minor, "%d", &minrev );
  131. }
  132. uint32_t ldaddress = default_load_address;
  133. if ( ldaddr )
  134. {
  135. sscanf ( ldaddr, "0x%08X", &ldaddress );
  136. }
  137. char* dupe = strdup(input);
  138. char* fname = basename ( dupe );
  139. if ( filename )
  140. {
  141. fname = filename;
  142. }
  143. struct timeval tm;
  144. gettimeofday ( &tm,NULL );
  145. struct stat buf;
  146. stat ( input,&buf );
  147. ldr_header_t* head = construct_header ( magicnum, (uint16_t) majrev, (uint16_t) minrev, ( uint32_t ) tm.tv_sec, ( uint32_t ) buf.st_size, ldaddress, fname, get_file_crc ( input ) );
  148. free(dupe);
  149. //uint32_t magic, uint16_t rev_maj,uint16_t rev_min, uint32_t build_date, uint32_t filelen, uint32_t ldaddress, const char* filename, uint32_t crc
  150. //FILE* fd = fopen ("/tftpboot/haxorware11rev32.bin","r");
  151. //fread(head,sizeof(ldr_header_t),1,fd);
  152. char* filebuffer = malloc ( buf.st_size+10 );
  153. FILE* fd = fopen ( input,"r" );
  154. fread ( filebuffer, 1, buf.st_size,fd );
  155. if (!output)
  156. {
  157. output = malloc(strlen(input+5));
  158. strcpy(output,input);
  159. strcat(output,".bin");
  160. }
  161. dump_header ( head );
  162. FILE* fd_out = fopen ( output,"w+" );
  163. if (!fd_out)
  164. {
  165. fprintf(stderr, "Failed to open output file: %s\n", output);
  166. exit(1);
  167. }
  168. fwrite ( head,1,sizeof ( ldr_header_t ),fd_out );
  169. fwrite ( filebuffer,1,buf.st_size,fd_out );
  170. printf("Firmware image %s is ready\n", output);
  171. return 0;
  172. }