summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2017-01-01 19:46:01 +0100
committerAmaury Pouly <amaury.pouly@gmail.com>2017-01-16 19:58:31 +0100
commit5ff3a3a98f23bb1a0dd1fb97e074ddb80337ae27 (patch)
tree481f63dc08db67d2355fc70a2463089d8e2654d2
parent2b20026dd755706934f8f8e1a192bffdfc3d717c (diff)
downloadrockbox-5ff3a3a98f23bb1a0dd1fb97e074ddb80337ae27.tar.gz
rockbox-5ff3a3a98f23bb1a0dd1fb97e074ddb80337ae27.zip
imxtools/sbtools: various fixes
Change bug() macro, fix memory leaks, always use -h for help, fix usage(), fix comment, remove useless macro Change-Id: I30554b5e07e6f2845560a570808603cf8c4da5ad
-rw-r--r--utils/imxtools/sbtools/elf.c1
-rw-r--r--utils/imxtools/sbtools/elftosb.c15
-rw-r--r--utils/imxtools/sbtools/misc.c6
-rw-r--r--utils/imxtools/sbtools/misc.h2
-rw-r--r--utils/imxtools/sbtools/sb.c16
-rw-r--r--utils/imxtools/sbtools/sbtoelf.c23
6 files changed, 39 insertions, 24 deletions
diff --git a/utils/imxtools/sbtools/elf.c b/utils/imxtools/sbtools/elf.c
index d15c27b206..68f66107a3 100644
--- a/utils/imxtools/sbtools/elf.c
+++ b/utils/imxtools/sbtools/elf.c
@@ -333,6 +333,7 @@ void elf_simplify(struct elf_params_t *params)
333 { 333 {
334 cur_sec->size += sections[i].size; 334 cur_sec->size += sections[i].size;
335 sections[i].size = 0; // will be ignored by rebuilding (see below) 335 sections[i].size = 0; // will be ignored by rebuilding (see below)
336 free(sections[i].name);
336 } 337 }
337 else if(sections[i].type == EST_LOAD) 338 else if(sections[i].type == EST_LOAD)
338 { 339 {
diff --git a/utils/imxtools/sbtools/elftosb.c b/utils/imxtools/sbtools/elftosb.c
index 2a96aecad7..ebfa033fcb 100644
--- a/utils/imxtools/sbtools/elftosb.c
+++ b/utils/imxtools/sbtools/elftosb.c
@@ -324,13 +324,13 @@ static void usage(void)
324{ 324{
325 printf("Usage: elftosb [options | file]...\n"); 325 printf("Usage: elftosb [options | file]...\n");
326 printf("Options:\n"); 326 printf("Options:\n");
327 printf(" -?/--help\tDisplay this message\n"); 327 printf(" -h/--help\tDisplay this message\n");
328 printf(" -o <file>\tSet output file\n"); 328 printf(" -o <file>\tSet output file\n");
329 printf(" -c <file>\tSet command file\n"); 329 printf(" -c <file>\tSet command file\n");
330 printf(" -d/--debug\tEnable debug output\n"); 330 printf(" -d/--debug\tEnable debug output\n");
331 printf(" -k <file>\tAdd key file\n"); 331 printf(" -k <file>\tAdd key file\n");
332 printf(" -z\t\tAdd zero key\n"); 332 printf(" -z\t\tAdd zero key\n");
333 printf(" --add-key <key>\tAdd single key (hex or usbotp)\n"); 333 printf(" --add-key <key>\tAdd single key\n");
334 printf(" --real-key <key>\tOverride real key\n"); 334 printf(" --real-key <key>\tOverride real key\n");
335 printf(" --crypto-iv <iv>\tOverride crypto IV\n"); 335 printf(" --crypto-iv <iv>\tOverride crypto IV\n");
336 exit(1); 336 exit(1);
@@ -342,14 +342,19 @@ int main(int argc, char **argv)
342 char *output_filename = NULL; 342 char *output_filename = NULL;
343 struct crypto_key_t real_key; 343 struct crypto_key_t real_key;
344 struct crypto_key_t crypto_iv; 344 struct crypto_key_t crypto_iv;
345 memset(&real_key, 0, sizeof(real_key));
346 memset(&crypto_iv, 0, sizeof(crypto_iv));
345 real_key.method = CRYPTO_NONE; 347 real_key.method = CRYPTO_NONE;
346 crypto_iv.method = CRYPTO_NONE; 348 crypto_iv.method = CRYPTO_NONE;
347 349
350 if(argc == 1)
351 usage();
352
348 while(1) 353 while(1)
349 { 354 {
350 static struct option long_options[] = 355 static struct option long_options[] =
351 { 356 {
352 {"help", no_argument, 0, '?'}, 357 {"help", no_argument, 0, 'h'},
353 {"debug", no_argument, 0, 'd'}, 358 {"debug", no_argument, 0, 'd'},
354 {"add-key", required_argument, 0, 'a'}, 359 {"add-key", required_argument, 0, 'a'},
355 {"real-key", required_argument, 0, 'r'}, 360 {"real-key", required_argument, 0, 'r'},
@@ -357,7 +362,7 @@ int main(int argc, char **argv)
357 {0, 0, 0, 0} 362 {0, 0, 0, 0}
358 }; 363 };
359 364
360 int c = getopt_long(argc, argv, "?do:c:k:za:", long_options, NULL); 365 int c = getopt_long(argc, argv, "hdo:c:k:za:", long_options, NULL);
361 if(c == -1) 366 if(c == -1)
362 break; 367 break;
363 switch(c) 368 switch(c)
@@ -365,7 +370,7 @@ int main(int argc, char **argv)
365 case 'd': 370 case 'd':
366 g_debug = true; 371 g_debug = true;
367 break; 372 break;
368 case '?': 373 case 'h':
369 usage(); 374 usage();
370 break; 375 break;
371 case 'o': 376 case 'o':
diff --git a/utils/imxtools/sbtools/misc.c b/utils/imxtools/sbtools/misc.c
index b3ca23cf77..6216ae612b 100644
--- a/utils/imxtools/sbtools/misc.c
+++ b/utils/imxtools/sbtools/misc.c
@@ -50,7 +50,11 @@ void generate_random_data(void *buf, size_t sz)
50void *xmalloc(size_t s) 50void *xmalloc(size_t s)
51{ 51{
52 void * r = malloc(s); 52 void * r = malloc(s);
53 if(!r) bugp("malloc"); 53 if(!r)
54 {
55 printf("Alloc failed\n");
56 abort();
57 }
54 return r; 58 return r;
55} 59}
56 60
diff --git a/utils/imxtools/sbtools/misc.h b/utils/imxtools/sbtools/misc.h
index 01e77c80c1..f803fbb06f 100644
--- a/utils/imxtools/sbtools/misc.h
+++ b/utils/imxtools/sbtools/misc.h
@@ -31,7 +31,7 @@
31#define MIN(a,b) ((a) < (b) ? (a) : (b)) 31#define MIN(a,b) ((a) < (b) ? (a) : (b))
32#endif 32#endif
33 33
34#define bug(...) do { fprintf(stderr,"["__FILE__":"STR(__LINE__)"]ERROR: "__VA_ARGS__); exit(1); } while(0) 34#define bug(...) do { fprintf(stderr, __VA_ARGS__); exit(1); } while(0)
35#define bugp(...) do { fprintf(stderr, __VA_ARGS__); perror(" "); exit(1); } while(0) 35#define bugp(...) do { fprintf(stderr, __VA_ARGS__); perror(" "); exit(1); } while(0)
36 36
37#define ROUND_UP(val, round) ((((val) + (round) - 1) / (round)) * (round)) 37#define ROUND_UP(val, round) ((((val) + (round) - 1) / (round)) * (round))
diff --git a/utils/imxtools/sbtools/sb.c b/utils/imxtools/sbtools/sb.c
index ff8e0da3ee..cbeacf9c3f 100644
--- a/utils/imxtools/sbtools/sb.c
+++ b/utils/imxtools/sbtools/sb.c
@@ -802,7 +802,11 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
802 if(memcmp(hdr_sha1, computed_sha1, 20) == 0) 802 if(memcmp(hdr_sha1, computed_sha1, 20) == 0)
803 printf(RED, " Ok\n"); 803 printf(RED, " Ok\n");
804 else 804 else
805 {
805 printf(RED, " Failed\n"); 806 printf(RED, " Failed\n");
807 if(!(flags & SB_IGNORE_SHA1))
808 fatal(SB_FORMAT_ERROR, "Bad header checksum\n");
809 }
806 printf(GREEN, " Flags: "); 810 printf(GREEN, " Flags: ");
807 printf(YELLOW, "%x\n", sb_header->flags); 811 printf(YELLOW, "%x\n", sb_header->flags);
808 printf(GREEN, " Total file size : "); 812 printf(GREEN, " Total file size : ");
@@ -1045,7 +1049,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
1045 printf(OFF, "%s", indent); 1049 printf(OFF, "%s", indent);
1046 uint8_t checksum = instruction_checksum(hdr); 1050 uint8_t checksum = instruction_checksum(hdr);
1047 if(checksum != hdr->checksum) 1051 if(checksum != hdr->checksum)
1048 printf(GREY, "[Bad checksum']"); 1052 printf(GREY, "[Bad checksum]");
1049 1053
1050 if(hdr->opcode == SB_INST_NOP) 1054 if(hdr->opcode == SB_INST_NOP)
1051 { 1055 {
@@ -1093,6 +1097,14 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
1093 printf(RED, " (Encrypted)"); 1097 printf(RED, " (Encrypted)");
1094 printf(OFF, "\n"); 1098 printf(OFF, "\n");
1095 1099
1100 /* skip it if we cannot decrypt it */
1101 if(encrypted && !valid_key)
1102 {
1103 printf(GREY, " Skipping section content (no valid key)\n");
1104 offset += size;
1105 continue;
1106 }
1107
1096 /* save it */ 1108 /* save it */
1097 byte *sec = xmalloc(size); 1109 byte *sec = xmalloc(size);
1098 if(encrypted) 1110 if(encrypted)
@@ -1161,7 +1173,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
1161 else if(flags & SB_IGNORE_SHA1) 1173 else if(flags & SB_IGNORE_SHA1)
1162 { 1174 {
1163 /* some weird images produced by some buggy tools have wrong SHA-1, 1175 /* some weird images produced by some buggy tools have wrong SHA-1,
1164 * this probably gone unnoticed because the bootloader ignores the SH1-1 1176 * this probably gone unnoticed because the bootloader ignores the SHA-1
1165 * anyway */ 1177 * anyway */
1166 printf(RED, " Failed\n"); 1178 printf(RED, " Failed\n");
1167 cprintf(u, true, GREY, "Warning: SHA-1 mismatch ignored per flags\n"); 1179 cprintf(u, true, GREY, "Warning: SHA-1 mismatch ignored per flags\n");
diff --git a/utils/imxtools/sbtools/sbtoelf.c b/utils/imxtools/sbtools/sbtoelf.c
index ac8db068aa..11a46968dd 100644
--- a/utils/imxtools/sbtools/sbtoelf.c
+++ b/utils/imxtools/sbtools/sbtoelf.c
@@ -48,15 +48,6 @@
48/* all blocks are sized as a multiple of 0x1ff */ 48/* all blocks are sized as a multiple of 0x1ff */
49#define PAD_TO_BOUNDARY(x) (((x) + 0x1ff) & ~0x1ff) 49#define PAD_TO_BOUNDARY(x) (((x) + 0x1ff) & ~0x1ff)
50 50
51/* If you find a firmware that breaks the known format ^^ */
52#define assert(a) do { if(!(a)) { fprintf(stderr,"Assertion \"%s\" failed in %s() line %d!\n\nPlease send us your firmware!\n",#a,__func__,__LINE__); exit(1); } } while(0)
53
54#define crypto_cbc(...) \
55 do { int ret = crypto_cbc(__VA_ARGS__); \
56 if(ret != CRYPTO_ERROR_SUCCESS) \
57 bug("crypto_cbc error: %d\n", ret); \
58 }while(0)
59
60/* globals */ 51/* globals */
61 52
62static char *g_out_prefix; 53static char *g_out_prefix;
@@ -116,7 +107,8 @@ static void extract_sb_section(struct sb_section_t *sec, struct cmd_file_t *cmd_
116 107
117 for(int j = 0; j < sec->nr_insts; j++) 108 for(int j = 0; j < sec->nr_insts; j++)
118 { 109 {
119 assert(sec->insts[j].inst == SB_INST_DATA); 110 if(sec->insts[j].inst != SB_INST_DATA)
111 bug("Internal errror: should be a data section\n");
120 fwrite(sec->insts[j].data, sec->insts[j].size, 1, fd); 112 fwrite(sec->insts[j].data, sec->insts[j].size, 1, fd);
121 } 113 }
122 fclose(fd); 114 fclose(fd);
@@ -185,6 +177,7 @@ static void extract_sb_file(struct sb_file_t *file)
185 printf("Write command file to %s\n", filename); 177 printf("Write command file to %s\n", filename);
186 db_generate_file(cmd_file, filename, NULL, generic_std_printf); 178 db_generate_file(cmd_file, filename, NULL, generic_std_printf);
187 db_free(cmd_file); 179 db_free(cmd_file);
180 free(filename);
188} 181}
189 182
190static void extract_elf(struct elf_params_t *elf, int count) 183static void extract_elf(struct elf_params_t *elf, int count)
@@ -248,13 +241,13 @@ static void usage(void)
248{ 241{
249 printf("Usage: sbtoelf [options] sb-file\n"); 242 printf("Usage: sbtoelf [options] sb-file\n");
250 printf("Options:\n"); 243 printf("Options:\n");
251 printf(" -?/--help Display this message\n"); 244 printf(" -h/--help Display this message\n");
252 printf(" -o <prefix> Enable output and set prefix\n"); 245 printf(" -o <prefix> Enable output and set prefix\n");
253 printf(" -d/--debug Enable debug output*\n"); 246 printf(" -d/--debug Enable debug output*\n");
254 printf(" -k <file> Add key file\n"); 247 printf(" -k <file> Add key file\n");
255 printf(" -z Add zero key\n"); 248 printf(" -z Add zero key\n");
256 printf(" -r Use raw command mode\n"); 249 printf(" -r Use raw command mode\n");
257 printf(" -a/--add-key <key> Add single key (hex or usbotp)\n"); 250 printf(" -a/--add-key <key> Add single key\n");
258 printf(" -n/--no-color Disable output colors\n"); 251 printf(" -n/--no-color Disable output colors\n");
259 printf(" -l/--loopback <file> Produce sb file out of extracted description*\n"); 252 printf(" -l/--loopback <file> Produce sb file out of extracted description*\n");
260 printf(" -f/--force Force reading even without a key*\n"); 253 printf(" -f/--force Force reading even without a key*\n");
@@ -280,7 +273,7 @@ int main(int argc, char **argv)
280 { 273 {
281 static struct option long_options[] = 274 static struct option long_options[] =
282 { 275 {
283 {"help", no_argument, 0, '?'}, 276 {"help", no_argument, 0, 'h'},
284 {"debug", no_argument, 0, 'd'}, 277 {"debug", no_argument, 0, 'd'},
285 {"add-key", required_argument, 0, 'a'}, 278 {"add-key", required_argument, 0, 'a'},
286 {"no-color", no_argument, 0, 'n'}, 279 {"no-color", no_argument, 0, 'n'},
@@ -293,7 +286,7 @@ int main(int argc, char **argv)
293 {0, 0, 0, 0} 286 {0, 0, 0, 0}
294 }; 287 };
295 288
296 int c = getopt_long(argc, argv, "?do:k:zra:nl:f12xsb", long_options, NULL); 289 int c = getopt_long(argc, argv, "hdo:k:zra:nl:f12xsb", long_options, NULL);
297 if(c == -1) 290 if(c == -1)
298 break; 291 break;
299 switch(c) 292 switch(c)
@@ -311,7 +304,7 @@ int main(int argc, char **argv)
311 case 'd': 304 case 'd':
312 g_debug = true; 305 g_debug = true;
313 break; 306 break;
314 case '?': 307 case 'h':
315 usage(); 308 usage();
316 break; 309 break;
317 case 'o': 310 case 'o':