From 5ff3a3a98f23bb1a0dd1fb97e074ddb80337ae27 Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Sun, 1 Jan 2017 19:46:01 +0100 Subject: 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 --- utils/imxtools/sbtools/elf.c | 1 + utils/imxtools/sbtools/elftosb.c | 15 ++++++++++----- utils/imxtools/sbtools/misc.c | 6 +++++- utils/imxtools/sbtools/misc.h | 2 +- utils/imxtools/sbtools/sb.c | 16 ++++++++++++++-- utils/imxtools/sbtools/sbtoelf.c | 23 ++++++++--------------- 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) { cur_sec->size += sections[i].size; sections[i].size = 0; // will be ignored by rebuilding (see below) + free(sections[i].name); } else if(sections[i].type == EST_LOAD) { 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) { printf("Usage: elftosb [options | file]...\n"); printf("Options:\n"); - printf(" -?/--help\tDisplay this message\n"); + printf(" -h/--help\tDisplay this message\n"); printf(" -o \tSet output file\n"); printf(" -c \tSet command file\n"); printf(" -d/--debug\tEnable debug output\n"); printf(" -k \tAdd key file\n"); printf(" -z\t\tAdd zero key\n"); - printf(" --add-key \tAdd single key (hex or usbotp)\n"); + printf(" --add-key \tAdd single key\n"); printf(" --real-key \tOverride real key\n"); printf(" --crypto-iv \tOverride crypto IV\n"); exit(1); @@ -342,14 +342,19 @@ int main(int argc, char **argv) char *output_filename = NULL; struct crypto_key_t real_key; struct crypto_key_t crypto_iv; + memset(&real_key, 0, sizeof(real_key)); + memset(&crypto_iv, 0, sizeof(crypto_iv)); real_key.method = CRYPTO_NONE; crypto_iv.method = CRYPTO_NONE; + if(argc == 1) + usage(); + while(1) { static struct option long_options[] = { - {"help", no_argument, 0, '?'}, + {"help", no_argument, 0, 'h'}, {"debug", no_argument, 0, 'd'}, {"add-key", required_argument, 0, 'a'}, {"real-key", required_argument, 0, 'r'}, @@ -357,7 +362,7 @@ int main(int argc, char **argv) {0, 0, 0, 0} }; - int c = getopt_long(argc, argv, "?do:c:k:za:", long_options, NULL); + int c = getopt_long(argc, argv, "hdo:c:k:za:", long_options, NULL); if(c == -1) break; switch(c) @@ -365,7 +370,7 @@ int main(int argc, char **argv) case 'd': g_debug = true; break; - case '?': + case 'h': usage(); break; 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) void *xmalloc(size_t s) { void * r = malloc(s); - if(!r) bugp("malloc"); + if(!r) + { + printf("Alloc failed\n"); + abort(); + } return r; } 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 @@ #define MIN(a,b) ((a) < (b) ? (a) : (b)) #endif -#define bug(...) do { fprintf(stderr,"["__FILE__":"STR(__LINE__)"]ERROR: "__VA_ARGS__); exit(1); } while(0) +#define bug(...) do { fprintf(stderr, __VA_ARGS__); exit(1); } while(0) #define bugp(...) do { fprintf(stderr, __VA_ARGS__); perror(" "); exit(1); } while(0) #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 if(memcmp(hdr_sha1, computed_sha1, 20) == 0) printf(RED, " Ok\n"); else + { printf(RED, " Failed\n"); + if(!(flags & SB_IGNORE_SHA1)) + fatal(SB_FORMAT_ERROR, "Bad header checksum\n"); + } printf(GREEN, " Flags: "); printf(YELLOW, "%x\n", sb_header->flags); printf(GREEN, " Total file size : "); @@ -1045,7 +1049,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo printf(OFF, "%s", indent); uint8_t checksum = instruction_checksum(hdr); if(checksum != hdr->checksum) - printf(GREY, "[Bad checksum']"); + printf(GREY, "[Bad checksum]"); if(hdr->opcode == SB_INST_NOP) { @@ -1093,6 +1097,14 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo printf(RED, " (Encrypted)"); printf(OFF, "\n"); + /* skip it if we cannot decrypt it */ + if(encrypted && !valid_key) + { + printf(GREY, " Skipping section content (no valid key)\n"); + offset += size; + continue; + } + /* save it */ byte *sec = xmalloc(size); if(encrypted) @@ -1161,7 +1173,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo else if(flags & SB_IGNORE_SHA1) { /* some weird images produced by some buggy tools have wrong SHA-1, - * this probably gone unnoticed because the bootloader ignores the SH1-1 + * this probably gone unnoticed because the bootloader ignores the SHA-1 * anyway */ printf(RED, " Failed\n"); 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 @@ /* all blocks are sized as a multiple of 0x1ff */ #define PAD_TO_BOUNDARY(x) (((x) + 0x1ff) & ~0x1ff) -/* If you find a firmware that breaks the known format ^^ */ -#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) - -#define crypto_cbc(...) \ - do { int ret = crypto_cbc(__VA_ARGS__); \ - if(ret != CRYPTO_ERROR_SUCCESS) \ - bug("crypto_cbc error: %d\n", ret); \ - }while(0) - /* globals */ static char *g_out_prefix; @@ -116,7 +107,8 @@ static void extract_sb_section(struct sb_section_t *sec, struct cmd_file_t *cmd_ for(int j = 0; j < sec->nr_insts; j++) { - assert(sec->insts[j].inst == SB_INST_DATA); + if(sec->insts[j].inst != SB_INST_DATA) + bug("Internal errror: should be a data section\n"); fwrite(sec->insts[j].data, sec->insts[j].size, 1, fd); } fclose(fd); @@ -185,6 +177,7 @@ static void extract_sb_file(struct sb_file_t *file) printf("Write command file to %s\n", filename); db_generate_file(cmd_file, filename, NULL, generic_std_printf); db_free(cmd_file); + free(filename); } static void extract_elf(struct elf_params_t *elf, int count) @@ -248,13 +241,13 @@ static void usage(void) { printf("Usage: sbtoelf [options] sb-file\n"); printf("Options:\n"); - printf(" -?/--help Display this message\n"); + printf(" -h/--help Display this message\n"); printf(" -o Enable output and set prefix\n"); printf(" -d/--debug Enable debug output*\n"); printf(" -k Add key file\n"); printf(" -z Add zero key\n"); printf(" -r Use raw command mode\n"); - printf(" -a/--add-key Add single key (hex or usbotp)\n"); + printf(" -a/--add-key Add single key\n"); printf(" -n/--no-color Disable output colors\n"); printf(" -l/--loopback Produce sb file out of extracted description*\n"); printf(" -f/--force Force reading even without a key*\n"); @@ -280,7 +273,7 @@ int main(int argc, char **argv) { static struct option long_options[] = { - {"help", no_argument, 0, '?'}, + {"help", no_argument, 0, 'h'}, {"debug", no_argument, 0, 'd'}, {"add-key", required_argument, 0, 'a'}, {"no-color", no_argument, 0, 'n'}, @@ -293,7 +286,7 @@ int main(int argc, char **argv) {0, 0, 0, 0} }; - int c = getopt_long(argc, argv, "?do:k:zra:nl:f12xsb", long_options, NULL); + int c = getopt_long(argc, argv, "hdo:k:zra:nl:f12xsb", long_options, NULL); if(c == -1) break; switch(c) @@ -311,7 +304,7 @@ int main(int argc, char **argv) case 'd': g_debug = true; break; - case '?': + case 'h': usage(); break; case 'o': -- cgit v1.2.3