forked from len0rd/rockbox
sbtools: add option to force sb dump
In the case of encrypted SB files without any key match, it is still possible to dump the section headers. The force option allows one to do so. It also allows to dump unencrypted sections of encrypted files if there are some. Change-Id: I36280230679ac5903f9c451c68c276f5c6959536
This commit is contained in:
parent
fa17cb904c
commit
7c9e7ec707
4 changed files with 37 additions and 7 deletions
|
@ -25,6 +25,7 @@
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
bool g_debug = false;
|
bool g_debug = false;
|
||||||
|
bool g_force = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Misc
|
* Misc
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
#define ROUND_UP(val, round) ((((val) + (round) - 1) / (round)) * (round))
|
#define ROUND_UP(val, round) ((((val) + (round) - 1) / (round)) * (round))
|
||||||
|
|
||||||
extern bool g_debug;
|
extern bool g_debug;
|
||||||
|
extern bool g_force;
|
||||||
|
|
||||||
typedef struct crypto_key_t *key_array_t;
|
typedef struct crypto_key_t *key_array_t;
|
||||||
int g_nr_keys;
|
int g_nr_keys;
|
||||||
|
|
|
@ -503,7 +503,7 @@ static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf,
|
||||||
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)
|
||||||
fatal(SB_CHECKSUM_ERROR, "Bad instruction checksum");
|
fatal(SB_CHECKSUM_ERROR, "Bad instruction checksum\n");
|
||||||
if(hdr->flags != 0)
|
if(hdr->flags != 0)
|
||||||
{
|
{
|
||||||
printf(GREY, "[");
|
printf(GREY, "[");
|
||||||
|
@ -788,7 +788,8 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, voi
|
||||||
printf(BLUE, "Encryption keys\n");
|
printf(BLUE, "Encryption keys\n");
|
||||||
for(int i = 0; i < g_nr_keys; i++)
|
for(int i = 0; i < g_nr_keys; i++)
|
||||||
{
|
{
|
||||||
printf(RED, " Key %d: ", i);
|
printf(RED, " Key %d\n", i),
|
||||||
|
printf(GREEN, " Key: ");
|
||||||
printf(YELLOW, "");
|
printf(YELLOW, "");
|
||||||
print_key(&g_key_array[i], true);
|
print_key(&g_key_array[i], true);
|
||||||
printf(GREEN, " CBC-MAC: ");
|
printf(GREEN, " CBC-MAC: ");
|
||||||
|
@ -859,7 +860,12 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, voi
|
||||||
free(cbcmacs);
|
free(cbcmacs);
|
||||||
|
|
||||||
if(!valid_key)
|
if(!valid_key)
|
||||||
fatal(SB_NO_VALID_KEY, "No valid key found\n");
|
{
|
||||||
|
if(g_force)
|
||||||
|
printf(GREY, " No valid key found\n");
|
||||||
|
else
|
||||||
|
fatal(SB_NO_VALID_KEY, "No valid key found\n");
|
||||||
|
}
|
||||||
|
|
||||||
if(getenv("SB_REAL_KEY") != 0)
|
if(getenv("SB_REAL_KEY") != 0)
|
||||||
{
|
{
|
||||||
|
@ -868,6 +874,12 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, voi
|
||||||
if(!parse_key(&env, &k) || *env)
|
if(!parse_key(&env, &k) || *env)
|
||||||
fatal(SB_ERROR, "Invalid SB_REAL_KEY\n");
|
fatal(SB_ERROR, "Invalid SB_REAL_KEY\n");
|
||||||
memcpy(real_key, k.u.key, 16);
|
memcpy(real_key, k.u.key, 16);
|
||||||
|
/* assume the key is valid */
|
||||||
|
if(valid_key)
|
||||||
|
printf(GREY, " Overriding real key\n");
|
||||||
|
else
|
||||||
|
printf(GREY, " Assuming real key is ok\n");
|
||||||
|
valid_key = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf(RED, " Summary:\n");
|
printf(RED, " Summary:\n");
|
||||||
|
@ -916,6 +928,13 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, voi
|
||||||
if(encrypted)
|
if(encrypted)
|
||||||
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");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
/* save it */
|
/* save it */
|
||||||
byte *sec = xmalloc(size);
|
byte *sec = xmalloc(size);
|
||||||
|
@ -939,7 +958,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, voi
|
||||||
free(sec);
|
free(sec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else if(valid_key)
|
||||||
{
|
{
|
||||||
/* advanced raw mode */
|
/* advanced raw mode */
|
||||||
printf(BLUE, "Commands\n");
|
printf(BLUE, "Commands\n");
|
||||||
|
@ -1041,6 +1060,10 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, voi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf(GREY, "Cannot read content in raw mode without a valid key\n");
|
||||||
|
}
|
||||||
|
|
||||||
/* final signature */
|
/* final signature */
|
||||||
printf(BLUE, "Final signature:\n");
|
printf(BLUE, "Final signature:\n");
|
||||||
|
|
|
@ -169,6 +169,7 @@ static void usage(void)
|
||||||
printf(" -a/--add-key <key>\tAdd single key (hex or usbotp)\n");
|
printf(" -a/--add-key <key>\tAdd single key (hex or usbotp)\n");
|
||||||
printf(" -n/--no-color\tDisable output colors\n");
|
printf(" -n/--no-color\tDisable output colors\n");
|
||||||
printf(" -l/--loopback <file>\tProduce sb file out of extracted description*\n");
|
printf(" -l/--loopback <file>\tProduce sb file out of extracted description*\n");
|
||||||
|
printf(" -f/--force\tForce reading even without a key*\n");
|
||||||
printf("Options marked with a * are for debug purpose only\n");
|
printf("Options marked with a * are for debug purpose only\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -204,10 +205,11 @@ int main(int argc, char **argv)
|
||||||
{"add-key", required_argument, 0, 'a'},
|
{"add-key", required_argument, 0, 'a'},
|
||||||
{"no-color", no_argument, 0, 'n'},
|
{"no-color", no_argument, 0, 'n'},
|
||||||
{"loopback", required_argument, 0, 'l'},
|
{"loopback", required_argument, 0, 'l'},
|
||||||
|
{"force", no_argument, 0, 'f' },
|
||||||
{0, 0, 0, 0}
|
{0, 0, 0, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
int c = getopt_long(argc, argv, "?do:k:zra:nl:", long_options, NULL);
|
int c = getopt_long(argc, argv, "?do:k:zra:nl:f", long_options, NULL);
|
||||||
if(c == -1)
|
if(c == -1)
|
||||||
break;
|
break;
|
||||||
switch(c)
|
switch(c)
|
||||||
|
@ -231,6 +233,9 @@ int main(int argc, char **argv)
|
||||||
case 'o':
|
case 'o':
|
||||||
g_out_prefix = optarg;
|
g_out_prefix = optarg;
|
||||||
break;
|
break;
|
||||||
|
case 'f':
|
||||||
|
g_force = true;
|
||||||
|
break;
|
||||||
case 'k':
|
case 'k':
|
||||||
{
|
{
|
||||||
if(!add_keys_from_file(optarg))
|
if(!add_keys_from_file(optarg))
|
||||||
|
@ -250,9 +255,9 @@ int main(int argc, char **argv)
|
||||||
struct crypto_key_t key;
|
struct crypto_key_t key;
|
||||||
char *s = optarg;
|
char *s = optarg;
|
||||||
if(!parse_key(&s, &key))
|
if(!parse_key(&s, &key))
|
||||||
bug("Invalid key specified as argument");
|
bug("Invalid key specified as argument\n");
|
||||||
if(*s != 0)
|
if(*s != 0)
|
||||||
bug("Trailing characters after key specified as argument");
|
bug("Trailing characters after key specified as argument\n");
|
||||||
add_keys(&key, 1);
|
add_keys(&key, 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue