hcsmakeimage.c 5.7 KB

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