diff options
author | Amaury Pouly <amaury.pouly@gmail.com> | 2013-07-18 00:27:12 +0200 |
---|---|---|
committer | Amaury Pouly <amaury.pouly@gmail.com> | 2013-07-18 00:27:12 +0200 |
commit | 1c8c24183ed9d89519de2924997593cc81f6f33d (patch) | |
tree | ba4a7babace41a249c44467982d5e6a70b8d7796 /utils/imxtools/sbtools/sbtoelf.c | |
parent | 3c5ee9ac57cbac4050df1729a28ec39c60d303ff (diff) | |
download | rockbox-1c8c24183ed9d89519de2924997593cc81f6f33d.tar.gz rockbox-1c8c24183ed9d89519de2924997593cc81f6f33d.zip |
sbtools: refactor and cleanup
Factor common elf/sb read/write/printf routines. Factor sb zero
key, move sb version guess.
Change-Id: I5ae1885587456736e782e623610281bbee545039
Diffstat (limited to 'utils/imxtools/sbtools/sbtoelf.c')
-rw-r--r-- | utils/imxtools/sbtools/sbtoelf.c | 110 |
1 files changed, 17 insertions, 93 deletions
diff --git a/utils/imxtools/sbtools/sbtoelf.c b/utils/imxtools/sbtools/sbtoelf.c index 69ab59e44d..a13e18b81d 100644 --- a/utils/imxtools/sbtools/sbtoelf.c +++ b/utils/imxtools/sbtools/sbtoelf.c | |||
@@ -60,24 +60,6 @@ | |||
60 | static char *g_out_prefix; | 60 | static char *g_out_prefix; |
61 | static bool g_elf_simplify = true; | 61 | static bool g_elf_simplify = true; |
62 | 62 | ||
63 | static void elf_printf(void *user, bool error, const char *fmt, ...) | ||
64 | { | ||
65 | if(!g_debug && !error) | ||
66 | return; | ||
67 | (void) user; | ||
68 | va_list args; | ||
69 | va_start(args, fmt); | ||
70 | vprintf(fmt, args); | ||
71 | va_end(args); | ||
72 | } | ||
73 | |||
74 | static void elf_write(void *user, uint32_t addr, const void *buf, size_t count) | ||
75 | { | ||
76 | FILE *f = user; | ||
77 | fseek(f, addr, SEEK_SET); | ||
78 | fwrite(buf, count, 1, f); | ||
79 | } | ||
80 | |||
81 | static void extract_elf_section(struct elf_params_t *elf, int count, uint32_t id) | 63 | static void extract_elf_section(struct elf_params_t *elf, int count, uint32_t id) |
82 | { | 64 | { |
83 | char name[5]; | 65 | char name[5]; |
@@ -94,7 +76,7 @@ static void extract_elf_section(struct elf_params_t *elf, int count, uint32_t id | |||
94 | return; | 76 | return; |
95 | if(g_elf_simplify) | 77 | if(g_elf_simplify) |
96 | elf_simplify(elf); | 78 | elf_simplify(elf); |
97 | elf_write_file(elf, elf_write, elf_printf, fd); | 79 | elf_write_file(elf, elf_std_write, elf_std_printf, fd); |
98 | fclose(fd); | 80 | fclose(fd); |
99 | } | 81 | } |
100 | 82 | ||
@@ -174,7 +156,7 @@ static void extract_elf(struct elf_params_t *elf, int count) | |||
174 | return; | 156 | return; |
175 | if(g_elf_simplify) | 157 | if(g_elf_simplify) |
176 | elf_simplify(elf); | 158 | elf_simplify(elf); |
177 | elf_write_file(elf, elf_write, elf_printf, fd); | 159 | elf_write_file(elf, elf_std_write, elf_std_printf, fd); |
178 | fclose(fd); | 160 | fclose(fd); |
179 | } | 161 | } |
180 | 162 | ||
@@ -236,73 +218,6 @@ static void usage(void) | |||
236 | exit(1); | 218 | exit(1); |
237 | } | 219 | } |
238 | 220 | ||
239 | static void sb_printf(void *user, bool error, color_t c, const char *fmt, ...) | ||
240 | { | ||
241 | (void) user; | ||
242 | (void) error; | ||
243 | va_list args; | ||
244 | va_start(args, fmt); | ||
245 | color(c); | ||
246 | vprintf(fmt, args); | ||
247 | va_end(args); | ||
248 | } | ||
249 | |||
250 | static struct crypto_key_t g_zero_key = | ||
251 | { | ||
252 | .method = CRYPTO_KEY, | ||
253 | .u.key = {0} | ||
254 | }; | ||
255 | |||
256 | |||
257 | |||
258 | enum sb_version_guess_t | ||
259 | { | ||
260 | SB_VERSION_1, | ||
261 | SB_VERSION_2, | ||
262 | SB_VERSION_UNK, | ||
263 | }; | ||
264 | |||
265 | enum sb_version_guess_t guess_sb_version(const char *filename) | ||
266 | { | ||
267 | #define ret(x) do { fclose(f); return x; } while(0) | ||
268 | FILE *f = fopen(filename, "rb"); | ||
269 | if(f == NULL) | ||
270 | bugp("Cannot open file for reading\n"); | ||
271 | // check signature | ||
272 | uint8_t sig[4]; | ||
273 | if(fseek(f, 20, SEEK_SET)) | ||
274 | ret(SB_VERSION_UNK); | ||
275 | if(fread(sig, 4, 1, f) != 1) | ||
276 | ret(SB_VERSION_UNK); | ||
277 | if(memcmp(sig, "STMP", 4) != 0) | ||
278 | ret(SB_VERSION_UNK); | ||
279 | // check header size (v1) | ||
280 | uint32_t hdr_size; | ||
281 | if(fseek(f, 8, SEEK_SET)) | ||
282 | ret(SB_VERSION_UNK); | ||
283 | if(fread(&hdr_size, 4, 1, f) != 1) | ||
284 | ret(SB_VERSION_UNK); | ||
285 | if(hdr_size == 0x34) | ||
286 | ret(SB_VERSION_1); | ||
287 | // check header params relationship | ||
288 | struct | ||
289 | { | ||
290 | uint16_t nr_keys; /* Number of encryption keys */ | ||
291 | uint16_t key_dict_off; /* Offset to key dictionary (in blocks) */ | ||
292 | uint16_t header_size; /* In blocks */ | ||
293 | uint16_t nr_sections; /* Number of sections */ | ||
294 | uint16_t sec_hdr_size; /* Section header size (in blocks) */ | ||
295 | } __attribute__((packed)) u; | ||
296 | if(fseek(f, 0x28, SEEK_SET)) | ||
297 | ret(SB_VERSION_UNK); | ||
298 | if(fread(&u, sizeof(u), 1, f) != 1) | ||
299 | ret(SB_VERSION_UNK); | ||
300 | if(u.sec_hdr_size == 1 && u.header_size == 6 && u.key_dict_off == u.header_size + u.nr_sections) | ||
301 | ret(SB_VERSION_2); | ||
302 | ret(SB_VERSION_UNK); | ||
303 | #undef ret | ||
304 | } | ||
305 | |||
306 | int main(int argc, char **argv) | 221 | int main(int argc, char **argv) |
307 | { | 222 | { |
308 | bool raw_mode = false; | 223 | bool raw_mode = false; |
@@ -361,8 +276,12 @@ int main(int argc, char **argv) | |||
361 | break; | 276 | break; |
362 | } | 277 | } |
363 | case 'z': | 278 | case 'z': |
279 | { | ||
280 | struct crypto_key_t g_zero_key; | ||
281 | sb_get_zero_key(&g_zero_key); | ||
364 | add_keys(&g_zero_key, 1); | 282 | add_keys(&g_zero_key, 1); |
365 | break; | 283 | break; |
284 | } | ||
366 | case 'x': | 285 | case 'x': |
367 | { | 286 | { |
368 | struct crypto_key_t key; | 287 | struct crypto_key_t key; |
@@ -397,7 +316,7 @@ int main(int argc, char **argv) | |||
397 | brute_force = true; | 316 | brute_force = true; |
398 | break; | 317 | break; |
399 | default: | 318 | default: |
400 | abort(); | 319 | bug("Internal error: unknown option '%c'\n", c); |
401 | } | 320 | } |
402 | } | 321 | } |
403 | 322 | ||
@@ -413,11 +332,16 @@ int main(int argc, char **argv) | |||
413 | const char *sb_filename = argv[optind]; | 332 | const char *sb_filename = argv[optind]; |
414 | 333 | ||
415 | enum sb_version_guess_t ver = guess_sb_version(sb_filename); | 334 | enum sb_version_guess_t ver = guess_sb_version(sb_filename); |
335 | if(ver == SB_VERSION_ERR) | ||
336 | { | ||
337 | printf("Cannot open/read SB file: %m\n"); | ||
338 | return 1; | ||
339 | } | ||
416 | 340 | ||
417 | if(force_sb2 || ver == SB_VERSION_2) | 341 | if(force_sb2 || ver == SB_VERSION_2) |
418 | { | 342 | { |
419 | enum sb_error_t err; | 343 | enum sb_error_t err; |
420 | struct sb_file_t *file = sb_read_file(sb_filename, raw_mode, NULL, sb_printf, &err); | 344 | struct sb_file_t *file = sb_read_file(sb_filename, raw_mode, NULL, sb_std_printf, &err); |
421 | if(file == NULL) | 345 | if(file == NULL) |
422 | { | 346 | { |
423 | color(OFF); | 347 | color(OFF); |
@@ -432,7 +356,7 @@ int main(int argc, char **argv) | |||
432 | { | 356 | { |
433 | color(GREY); | 357 | color(GREY); |
434 | printf("[Debug output]\n"); | 358 | printf("[Debug output]\n"); |
435 | sb_dump(file, NULL, sb_printf); | 359 | sb_dump(file, NULL, sb_std_printf); |
436 | } | 360 | } |
437 | if(loopback) | 361 | if(loopback) |
438 | { | 362 | { |
@@ -451,7 +375,7 @@ int main(int argc, char **argv) | |||
451 | { | 375 | { |
452 | struct crypto_key_t key; | 376 | struct crypto_key_t key; |
453 | enum sb1_error_t err; | 377 | enum sb1_error_t err; |
454 | if(!sb1_brute_force(sb_filename, NULL, sb_printf, &err, &key)) | 378 | if(!sb1_brute_force(sb_filename, NULL, sb_std_printf, &err, &key)) |
455 | { | 379 | { |
456 | color(OFF); | 380 | color(OFF); |
457 | printf("Brute force failed: %d\n", err); | 381 | printf("Brute force failed: %d\n", err); |
@@ -475,7 +399,7 @@ int main(int argc, char **argv) | |||
475 | } | 399 | } |
476 | 400 | ||
477 | enum sb1_error_t err; | 401 | enum sb1_error_t err; |
478 | struct sb1_file_t *file = sb1_read_file(sb_filename, NULL, sb_printf, &err); | 402 | struct sb1_file_t *file = sb1_read_file(sb_filename, NULL, sb_std_printf, &err); |
479 | if(file == NULL) | 403 | if(file == NULL) |
480 | { | 404 | { |
481 | color(OFF); | 405 | color(OFF); |
@@ -490,7 +414,7 @@ int main(int argc, char **argv) | |||
490 | { | 414 | { |
491 | color(GREY); | 415 | color(GREY); |
492 | printf("[Debug output]\n"); | 416 | printf("[Debug output]\n"); |
493 | sb1_dump(file, NULL, sb_printf); | 417 | sb1_dump(file, NULL, sb_std_printf); |
494 | } | 418 | } |
495 | if(loopback) | 419 | if(loopback) |
496 | sb1_write_file(file, loopback); | 420 | sb1_write_file(file, loopback); |