forked from len0rd/rockbox
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
This commit is contained in:
parent
2b20026dd7
commit
5ff3a3a98f
6 changed files with 39 additions and 24 deletions
|
@ -333,6 +333,7 @@ void elf_simplify(struct elf_params_t *params)
|
||||||
{
|
{
|
||||||
cur_sec->size += sections[i].size;
|
cur_sec->size += sections[i].size;
|
||||||
sections[i].size = 0; // will be ignored by rebuilding (see below)
|
sections[i].size = 0; // will be ignored by rebuilding (see below)
|
||||||
|
free(sections[i].name);
|
||||||
}
|
}
|
||||||
else if(sections[i].type == EST_LOAD)
|
else if(sections[i].type == EST_LOAD)
|
||||||
{
|
{
|
||||||
|
|
|
@ -324,13 +324,13 @@ static void usage(void)
|
||||||
{
|
{
|
||||||
printf("Usage: elftosb [options | file]...\n");
|
printf("Usage: elftosb [options | file]...\n");
|
||||||
printf("Options:\n");
|
printf("Options:\n");
|
||||||
printf(" -?/--help\tDisplay this message\n");
|
printf(" -h/--help\tDisplay this message\n");
|
||||||
printf(" -o <file>\tSet output file\n");
|
printf(" -o <file>\tSet output file\n");
|
||||||
printf(" -c <file>\tSet command file\n");
|
printf(" -c <file>\tSet command file\n");
|
||||||
printf(" -d/--debug\tEnable debug output\n");
|
printf(" -d/--debug\tEnable debug output\n");
|
||||||
printf(" -k <file>\tAdd key file\n");
|
printf(" -k <file>\tAdd key file\n");
|
||||||
printf(" -z\t\tAdd zero key\n");
|
printf(" -z\t\tAdd zero key\n");
|
||||||
printf(" --add-key <key>\tAdd single key (hex or usbotp)\n");
|
printf(" --add-key <key>\tAdd single key\n");
|
||||||
printf(" --real-key <key>\tOverride real key\n");
|
printf(" --real-key <key>\tOverride real key\n");
|
||||||
printf(" --crypto-iv <iv>\tOverride crypto IV\n");
|
printf(" --crypto-iv <iv>\tOverride crypto IV\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -342,14 +342,19 @@ int main(int argc, char **argv)
|
||||||
char *output_filename = NULL;
|
char *output_filename = NULL;
|
||||||
struct crypto_key_t real_key;
|
struct crypto_key_t real_key;
|
||||||
struct crypto_key_t crypto_iv;
|
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;
|
real_key.method = CRYPTO_NONE;
|
||||||
crypto_iv.method = CRYPTO_NONE;
|
crypto_iv.method = CRYPTO_NONE;
|
||||||
|
|
||||||
|
if(argc == 1)
|
||||||
|
usage();
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
static struct option long_options[] =
|
static struct option long_options[] =
|
||||||
{
|
{
|
||||||
{"help", no_argument, 0, '?'},
|
{"help", no_argument, 0, 'h'},
|
||||||
{"debug", no_argument, 0, 'd'},
|
{"debug", no_argument, 0, 'd'},
|
||||||
{"add-key", required_argument, 0, 'a'},
|
{"add-key", required_argument, 0, 'a'},
|
||||||
{"real-key", required_argument, 0, 'r'},
|
{"real-key", required_argument, 0, 'r'},
|
||||||
|
@ -357,7 +362,7 @@ int main(int argc, char **argv)
|
||||||
{0, 0, 0, 0}
|
{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)
|
if(c == -1)
|
||||||
break;
|
break;
|
||||||
switch(c)
|
switch(c)
|
||||||
|
@ -365,7 +370,7 @@ int main(int argc, char **argv)
|
||||||
case 'd':
|
case 'd':
|
||||||
g_debug = true;
|
g_debug = true;
|
||||||
break;
|
break;
|
||||||
case '?':
|
case 'h':
|
||||||
usage();
|
usage();
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
|
|
|
@ -50,7 +50,11 @@ void generate_random_data(void *buf, size_t sz)
|
||||||
void *xmalloc(size_t s)
|
void *xmalloc(size_t s)
|
||||||
{
|
{
|
||||||
void * r = malloc(s);
|
void * r = malloc(s);
|
||||||
if(!r) bugp("malloc");
|
if(!r)
|
||||||
|
{
|
||||||
|
printf("Alloc failed\n");
|
||||||
|
abort();
|
||||||
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@
|
||||||
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
||||||
#endif
|
#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 bugp(...) do { fprintf(stderr, __VA_ARGS__); perror(" "); exit(1); } while(0)
|
||||||
|
|
||||||
#define ROUND_UP(val, round) ((((val) + (round) - 1) / (round)) * (round))
|
#define ROUND_UP(val, round) ((((val) + (round) - 1) / (round)) * (round))
|
||||||
|
|
|
@ -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)
|
if(memcmp(hdr_sha1, computed_sha1, 20) == 0)
|
||||||
printf(RED, " Ok\n");
|
printf(RED, " Ok\n");
|
||||||
else
|
else
|
||||||
|
{
|
||||||
printf(RED, " Failed\n");
|
printf(RED, " Failed\n");
|
||||||
|
if(!(flags & SB_IGNORE_SHA1))
|
||||||
|
fatal(SB_FORMAT_ERROR, "Bad header checksum\n");
|
||||||
|
}
|
||||||
printf(GREEN, " Flags: ");
|
printf(GREEN, " Flags: ");
|
||||||
printf(YELLOW, "%x\n", sb_header->flags);
|
printf(YELLOW, "%x\n", sb_header->flags);
|
||||||
printf(GREEN, " Total file size : ");
|
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);
|
printf(OFF, "%s", indent);
|
||||||
uint8_t checksum = instruction_checksum(hdr);
|
uint8_t checksum = instruction_checksum(hdr);
|
||||||
if(checksum != hdr->checksum)
|
if(checksum != hdr->checksum)
|
||||||
printf(GREY, "[Bad checksum']");
|
printf(GREY, "[Bad checksum]");
|
||||||
|
|
||||||
if(hdr->opcode == SB_INST_NOP)
|
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(RED, " (Encrypted)");
|
||||||
printf(OFF, "\n");
|
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 */
|
/* save it */
|
||||||
byte *sec = xmalloc(size);
|
byte *sec = xmalloc(size);
|
||||||
if(encrypted)
|
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)
|
else if(flags & SB_IGNORE_SHA1)
|
||||||
{
|
{
|
||||||
/* some weird images produced by some buggy tools have wrong SHA-1,
|
/* 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 */
|
* anyway */
|
||||||
printf(RED, " Failed\n");
|
printf(RED, " Failed\n");
|
||||||
cprintf(u, true, GREY, "Warning: SHA-1 mismatch ignored per flags\n");
|
cprintf(u, true, GREY, "Warning: SHA-1 mismatch ignored per flags\n");
|
||||||
|
|
|
@ -48,15 +48,6 @@
|
||||||
/* all blocks are sized as a multiple of 0x1ff */
|
/* all blocks are sized as a multiple of 0x1ff */
|
||||||
#define PAD_TO_BOUNDARY(x) (((x) + 0x1ff) & ~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 */
|
/* globals */
|
||||||
|
|
||||||
static char *g_out_prefix;
|
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++)
|
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);
|
fwrite(sec->insts[j].data, sec->insts[j].size, 1, fd);
|
||||||
}
|
}
|
||||||
fclose(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);
|
printf("Write command file to %s\n", filename);
|
||||||
db_generate_file(cmd_file, filename, NULL, generic_std_printf);
|
db_generate_file(cmd_file, filename, NULL, generic_std_printf);
|
||||||
db_free(cmd_file);
|
db_free(cmd_file);
|
||||||
|
free(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void extract_elf(struct elf_params_t *elf, int count)
|
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("Usage: sbtoelf [options] sb-file\n");
|
||||||
printf("Options:\n");
|
printf("Options:\n");
|
||||||
printf(" -?/--help Display this message\n");
|
printf(" -h/--help Display this message\n");
|
||||||
printf(" -o <prefix> Enable output and set prefix\n");
|
printf(" -o <prefix> Enable output and set prefix\n");
|
||||||
printf(" -d/--debug Enable debug output*\n");
|
printf(" -d/--debug Enable debug output*\n");
|
||||||
printf(" -k <file> Add key file\n");
|
printf(" -k <file> Add key file\n");
|
||||||
printf(" -z Add zero key\n");
|
printf(" -z Add zero key\n");
|
||||||
printf(" -r Use raw command mode\n");
|
printf(" -r Use raw command mode\n");
|
||||||
printf(" -a/--add-key <key> Add single key (hex or usbotp)\n");
|
printf(" -a/--add-key <key> Add single key\n");
|
||||||
printf(" -n/--no-color Disable output colors\n");
|
printf(" -n/--no-color Disable output colors\n");
|
||||||
printf(" -l/--loopback <file> Produce sb file out of extracted description*\n");
|
printf(" -l/--loopback <file> Produce sb file out of extracted description*\n");
|
||||||
printf(" -f/--force Force reading even without a key*\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[] =
|
static struct option long_options[] =
|
||||||
{
|
{
|
||||||
{"help", no_argument, 0, '?'},
|
{"help", no_argument, 0, 'h'},
|
||||||
{"debug", no_argument, 0, 'd'},
|
{"debug", no_argument, 0, 'd'},
|
||||||
{"add-key", required_argument, 0, 'a'},
|
{"add-key", required_argument, 0, 'a'},
|
||||||
{"no-color", no_argument, 0, 'n'},
|
{"no-color", no_argument, 0, 'n'},
|
||||||
|
@ -293,7 +286,7 @@ int main(int argc, char **argv)
|
||||||
{0, 0, 0, 0}
|
{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)
|
if(c == -1)
|
||||||
break;
|
break;
|
||||||
switch(c)
|
switch(c)
|
||||||
|
@ -311,7 +304,7 @@ int main(int argc, char **argv)
|
||||||
case 'd':
|
case 'd':
|
||||||
g_debug = true;
|
g_debug = true;
|
||||||
break;
|
break;
|
||||||
case '?':
|
case 'h':
|
||||||
usage();
|
usage();
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue