diff options
author | Amaury Pouly <amaury.pouly@gmail.com> | 2013-08-21 22:20:53 +0200 |
---|---|---|
committer | Amaury Pouly <amaury.pouly@gmail.com> | 2013-08-21 22:20:53 +0200 |
commit | 99f3ca174e3e6a3e6a55931b012208be64d8be39 (patch) | |
tree | 2a8a1a6ab55f44be85d287c06f9f35e0a5bd92fc | |
parent | 1357e0a4f1567e4bf5f3ecb27c28457d1dfc1ab4 (diff) | |
download | rockbox-99f3ca174e3e6a3e6a55931b012208be64d8be39.tar.gz rockbox-99f3ca174e3e6a3e6a55931b012208be64d8be39.zip |
sbtools: more code refactoring
Factor all printf method with a unique one defined in misc.h
Change-Id: I58fbf8916b76e873a2e6678506d2c8aece7834ec
-rw-r--r-- | rbutil/mkimxboot/mkimxboot.c | 28 | ||||
-rw-r--r-- | utils/imxtools/sbtools/elf.c | 35 | ||||
-rw-r--r-- | utils/imxtools/sbtools/elf.h | 8 | ||||
-rw-r--r-- | utils/imxtools/sbtools/elftosb.c | 4 | ||||
-rw-r--r-- | utils/imxtools/sbtools/elftosb1.c | 20 | ||||
-rw-r--r-- | utils/imxtools/sbtools/misc.c | 12 | ||||
-rw-r--r-- | utils/imxtools/sbtools/misc.h | 4 | ||||
-rw-r--r-- | utils/imxtools/sbtools/sb.c | 30 | ||||
-rw-r--r-- | utils/imxtools/sbtools/sb.h | 15 | ||||
-rw-r--r-- | utils/imxtools/sbtools/sb1.c | 10 | ||||
-rw-r--r-- | utils/imxtools/sbtools/sb1.h | 11 | ||||
-rw-r--r-- | utils/imxtools/sbtools/sbtoelf.c | 15 |
12 files changed, 69 insertions, 123 deletions
diff --git a/rbutil/mkimxboot/mkimxboot.c b/rbutil/mkimxboot/mkimxboot.c index 47035bbc4d..511b3a1b7e 100644 --- a/rbutil/mkimxboot/mkimxboot.c +++ b/rbutil/mkimxboot/mkimxboot.c | |||
@@ -540,7 +540,7 @@ static enum imx_error_t load_sb_file(const char *file, int md5_idx, | |||
540 | clear_keys(); | 540 | clear_keys(); |
541 | add_keys(imx_models[model].keys, imx_models[model].nr_keys); | 541 | add_keys(imx_models[model].keys, imx_models[model].nr_keys); |
542 | *sb_file = sb_read_file_ex(file, imx_sums[md5_idx].fw_variants[opt.fw_variant].offset, | 542 | *sb_file = sb_read_file_ex(file, imx_sums[md5_idx].fw_variants[opt.fw_variant].offset, |
543 | imx_sums[md5_idx].fw_variants[opt.fw_variant].size, false, NULL, &sb_std_printf, &err); | 543 | imx_sums[md5_idx].fw_variants[opt.fw_variant].size, false, NULL, generic_std_printf, &err); |
544 | if(*sb_file == NULL) | 544 | if(*sb_file == NULL) |
545 | { | 545 | { |
546 | clear_keys(); | 546 | clear_keys(); |
@@ -595,28 +595,6 @@ struct elf_user_t | |||
595 | size_t sz; | 595 | size_t sz; |
596 | }; | 596 | }; |
597 | 597 | ||
598 | static bool elf_read(void *user, uint32_t addr, void *buf, size_t count) | ||
599 | { | ||
600 | struct elf_user_t *u = user; | ||
601 | if(addr + count <= u->sz) | ||
602 | { | ||
603 | memcpy(buf, u->buf + addr, count); | ||
604 | return true; | ||
605 | } | ||
606 | else | ||
607 | return false; | ||
608 | } | ||
609 | |||
610 | static void elf_printf(void *user, bool error, const char *fmt, ...) | ||
611 | { | ||
612 | if(!g_debug && !error) | ||
613 | return; | ||
614 | (void) user; | ||
615 | va_list args; | ||
616 | va_start(args, fmt); | ||
617 | vprintf(fmt, args); | ||
618 | va_end(args); | ||
619 | } | ||
620 | /* Load a rockbox firwmare from a buffer. Data is copied. Assume firmware is | 598 | /* Load a rockbox firwmare from a buffer. Data is copied. Assume firmware is |
621 | * using ELF format. */ | 599 | * using ELF format. */ |
622 | static enum imx_error_t rb_fw_load_buf_elf(struct rb_fw_t *fw, uint8_t *buf, | 600 | static enum imx_error_t rb_fw_load_buf_elf(struct rb_fw_t *fw, uint8_t *buf, |
@@ -627,7 +605,7 @@ static enum imx_error_t rb_fw_load_buf_elf(struct rb_fw_t *fw, uint8_t *buf, | |||
627 | user.buf = buf; | 605 | user.buf = buf; |
628 | user.sz = sz; | 606 | user.sz = sz; |
629 | elf_init(&elf); | 607 | elf_init(&elf); |
630 | if(!elf_read_file(&elf, &elf_read, &elf_printf, &user)) | 608 | if(!elf_read_file(&elf, elf_std_read, generic_std_printf, &user)) |
631 | { | 609 | { |
632 | elf_release(&elf); | 610 | elf_release(&elf); |
633 | printf("[ERR] Error parsing ELF file\n"); | 611 | printf("[ERR] Error parsing ELF file\n"); |
@@ -744,7 +722,7 @@ enum imx_error_t mkimxboot(const char *infile, const char *bootfile, | |||
744 | ret = patch_firmware(model, opt.fw_variant, opt.output, | 722 | ret = patch_firmware(model, opt.fw_variant, opt.output, |
745 | sb_file, boot_fw, opt.force_version); | 723 | sb_file, boot_fw, opt.force_version); |
746 | if(ret == IMX_SUCCESS) | 724 | if(ret == IMX_SUCCESS) |
747 | ret = sb_write_file(sb_file, outfile, NULL, sb_std_printf); | 725 | ret = sb_write_file(sb_file, outfile, NULL, generic_std_printf); |
748 | 726 | ||
749 | clear_keys(); | 727 | clear_keys(); |
750 | rb_fw_free(&boot_fw); | 728 | rb_fw_free(&boot_fw); |
diff --git a/utils/imxtools/sbtools/elf.c b/utils/imxtools/sbtools/elf.c index 5017251da3..c875cc7963 100644 --- a/utils/imxtools/sbtools/elf.c +++ b/utils/imxtools/sbtools/elf.c | |||
@@ -430,7 +430,7 @@ void elf_sort_by_address(struct elf_params_t *params) | |||
430 | } | 430 | } |
431 | 431 | ||
432 | void elf_write_file(struct elf_params_t *params, elf_write_fn_t write, | 432 | void elf_write_file(struct elf_params_t *params, elf_write_fn_t write, |
433 | elf_printf_fn_t printf, void *user) | 433 | generic_printf_t printf, void *user) |
434 | { | 434 | { |
435 | (void) printf; | 435 | (void) printf; |
436 | 436 | ||
@@ -607,13 +607,13 @@ void elf_write_file(struct elf_params_t *params, elf_write_fn_t write, | |||
607 | free(strtbl_content); | 607 | free(strtbl_content); |
608 | } | 608 | } |
609 | 609 | ||
610 | static void *elf_load_section(Elf32_Shdr *sh, elf_read_fn_t read, elf_printf_fn_t printf, void *user) | 610 | static void *elf_load_section(Elf32_Shdr *sh, elf_read_fn_t read, generic_printf_t printf, void *user) |
611 | { | 611 | { |
612 | void *data = xmalloc(sh->sh_size); | 612 | void *data = xmalloc(sh->sh_size); |
613 | if(!read(user, sh->sh_offset, data, sh->sh_size)) | 613 | if(!read(user, sh->sh_offset, data, sh->sh_size)) |
614 | { | 614 | { |
615 | free(data); | 615 | free(data); |
616 | printf(user, true, "error reading elf section data\n"); | 616 | printf(user, true, OFF, "error reading elf section data\n"); |
617 | return NULL; | 617 | return NULL; |
618 | } | 618 | } |
619 | return data; | 619 | return data; |
@@ -633,15 +633,15 @@ bool elf_guess(elf_read_fn_t read, void *user) | |||
633 | } | 633 | } |
634 | 634 | ||
635 | bool elf_read_file(struct elf_params_t *params, elf_read_fn_t read, | 635 | bool elf_read_file(struct elf_params_t *params, elf_read_fn_t read, |
636 | elf_printf_fn_t printf, void *user) | 636 | generic_printf_t printf, void *user) |
637 | { | 637 | { |
638 | #define error_printf(...) ({printf(user, true, __VA_ARGS__); return false;}) | 638 | #define error_printf(...) ({printf(user, true, GREY, __VA_ARGS__); return false;}) |
639 | 639 | ||
640 | /* read header */ | 640 | /* read header */ |
641 | Elf32_Ehdr ehdr; | 641 | Elf32_Ehdr ehdr; |
642 | if(!read(user, 0, &ehdr, sizeof(ehdr))) | 642 | if(!read(user, 0, &ehdr, sizeof(ehdr))) |
643 | { | 643 | { |
644 | printf(user, true, "error reading elf header\n"); | 644 | printf(user, true, GREY, "error reading elf header\n"); |
645 | return false; | 645 | return false; |
646 | } | 646 | } |
647 | /* basic checks */ | 647 | /* basic checks */ |
@@ -667,16 +667,16 @@ bool elf_read_file(struct elf_params_t *params, elf_read_fn_t read, | |||
667 | elf_set_start_addr(params, ehdr.e_entry); | 667 | elf_set_start_addr(params, ehdr.e_entry); |
668 | 668 | ||
669 | /* run through sections */ | 669 | /* run through sections */ |
670 | printf(user, false, "ELF file:\n"); | 670 | printf(user, false, OFF, "ELF file:\n"); |
671 | Elf32_Shdr *shdr = xmalloc(sizeof(Elf32_Shdr) * ehdr.e_shnum); | 671 | Elf32_Shdr *shdr = xmalloc(sizeof(Elf32_Shdr) * ehdr.e_shnum); |
672 | if(!read(user, ehdr.e_shoff, shdr, sizeof(Elf32_Shdr) * ehdr.e_shnum)) | 672 | if(!read(user, ehdr.e_shoff, shdr, sizeof(Elf32_Shdr) * ehdr.e_shnum)) |
673 | { | 673 | { |
674 | printf(user, true, "cannot read elf section headers\n"); | 674 | printf(user, true, GREY, "cannot read elf section headers\n"); |
675 | return false; | 675 | return false; |
676 | } | 676 | } |
677 | char *strtab = elf_load_section(&shdr[ehdr.e_shstrndx], read, printf, user); | 677 | char *strtab = elf_load_section(&shdr[ehdr.e_shstrndx], read, printf, user); |
678 | if(!strtab) | 678 | if(!strtab) |
679 | printf(user, false, "elf file has no valid section string table\n"); | 679 | printf(user, false, OFF, "elf file has no valid section string table\n"); |
680 | for(int i = 1; i < ehdr.e_shnum; i++) | 680 | for(int i = 1; i < ehdr.e_shnum; i++) |
681 | { | 681 | { |
682 | const char *sec_name = &strtab[shdr[i].sh_name]; | 682 | const char *sec_name = &strtab[shdr[i].sh_name]; |
@@ -688,7 +688,7 @@ bool elf_read_file(struct elf_params_t *params, elf_read_fn_t read, | |||
688 | void *data = elf_load_section(&shdr[i], read, printf, user); | 688 | void *data = elf_load_section(&shdr[i], read, printf, user); |
689 | if(!data) | 689 | if(!data) |
690 | { | 690 | { |
691 | printf(user, true, "cannot read elf section %s\n", sec_name); | 691 | printf(user, true, GREY, "cannot read elf section %s\n", sec_name); |
692 | goto Lerr; | 692 | goto Lerr; |
693 | } | 693 | } |
694 | elf_add_load_section(params, shdr[i].sh_addr, shdr[i].sh_size, data, sec_name); | 694 | elf_add_load_section(params, shdr[i].sh_addr, shdr[i].sh_size, data, sec_name); |
@@ -707,7 +707,7 @@ bool elf_read_file(struct elf_params_t *params, elf_read_fn_t read, | |||
707 | char *symstrtab = elf_load_section(&shdr[shdr[i].sh_link], read, printf, user); | 707 | char *symstrtab = elf_load_section(&shdr[shdr[i].sh_link], read, printf, user); |
708 | if(!symstrtab) | 708 | if(!symstrtab) |
709 | { | 709 | { |
710 | printf(user, true, "cannot load string table for symbol table %s\n", sec_name); | 710 | printf(user, true, GREY, "cannot load string table for symbol table %s\n", sec_name); |
711 | goto Lerr; | 711 | goto Lerr; |
712 | } | 712 | } |
713 | // load symbol table data | 713 | // load symbol table data |
@@ -772,7 +772,7 @@ bool elf_read_file(struct elf_params_t *params, elf_read_fn_t read, | |||
772 | seg->paddr = phdr.p_paddr; | 772 | seg->paddr = phdr.p_paddr; |
773 | seg->vsize = phdr.p_memsz; | 773 | seg->vsize = phdr.p_memsz; |
774 | seg->psize = phdr.p_filesz; | 774 | seg->psize = phdr.p_filesz; |
775 | printf(user, false, "create segment [%#x,+%#x[ -> [%#x,+%#x[\n", | 775 | printf(user, false, OFF, "create segment [%#x,+%#x[ -> [%#x,+%#x[\n", |
776 | seg->vaddr, seg->vsize, seg->paddr, seg->psize); | 776 | seg->vaddr, seg->vsize, seg->paddr, seg->psize); |
777 | } | 777 | } |
778 | 778 | ||
@@ -855,17 +855,6 @@ void elf_release(struct elf_params_t *params) | |||
855 | } | 855 | } |
856 | } | 856 | } |
857 | 857 | ||
858 | void elf_std_printf(void *user, bool error, const char *fmt, ...) | ||
859 | { | ||
860 | if(!g_debug && !error) | ||
861 | return; | ||
862 | (void) user; | ||
863 | va_list args; | ||
864 | va_start(args, fmt); | ||
865 | vprintf(fmt, args); | ||
866 | va_end(args); | ||
867 | } | ||
868 | |||
869 | void elf_std_write(void *user, uint32_t addr, const void *buf, size_t count) | 858 | void elf_std_write(void *user, uint32_t addr, const void *buf, size_t count) |
870 | { | 859 | { |
871 | FILE *f = user; | 860 | FILE *f = user; |
diff --git a/utils/imxtools/sbtools/elf.h b/utils/imxtools/sbtools/elf.h index 4200850e74..bc2971c4f1 100644 --- a/utils/imxtools/sbtools/elf.h +++ b/utils/imxtools/sbtools/elf.h | |||
@@ -27,6 +27,7 @@ | |||
27 | #include <stdbool.h> | 27 | #include <stdbool.h> |
28 | #include <stdlib.h> | 28 | #include <stdlib.h> |
29 | #include <unistd.h> | 29 | #include <unistd.h> |
30 | #include "misc.h" | ||
30 | 31 | ||
31 | /** | 32 | /** |
32 | * API | 33 | * API |
@@ -93,7 +94,6 @@ struct elf_params_t | |||
93 | typedef bool (*elf_read_fn_t)(void *user, uint32_t addr, void *buf, size_t count); | 94 | typedef bool (*elf_read_fn_t)(void *user, uint32_t addr, void *buf, size_t count); |
94 | /* write function manages it's own error state */ | 95 | /* write function manages it's own error state */ |
95 | typedef void (*elf_write_fn_t)(void *user, uint32_t addr, const void *buf, size_t count); | 96 | typedef void (*elf_write_fn_t)(void *user, uint32_t addr, const void *buf, size_t count); |
96 | typedef void (*elf_printf_fn_t)(void *user, bool error, const char *fmt, ...); | ||
97 | 97 | ||
98 | void elf_init(struct elf_params_t *params); | 98 | void elf_init(struct elf_params_t *params); |
99 | void elf_add_load_section(struct elf_params_t *params, | 99 | void elf_add_load_section(struct elf_params_t *params, |
@@ -103,9 +103,8 @@ void elf_add_fill_section(struct elf_params_t *params, | |||
103 | uint32_t elf_translate_virtual_address(struct elf_params_t *params, uint32_t addr); | 103 | uint32_t elf_translate_virtual_address(struct elf_params_t *params, uint32_t addr); |
104 | void elf_simplify(struct elf_params_t *params); | 104 | void elf_simplify(struct elf_params_t *params); |
105 | void elf_sort_by_address(struct elf_params_t *params); | 105 | void elf_sort_by_address(struct elf_params_t *params); |
106 | void elf_write_file(struct elf_params_t *params, elf_write_fn_t write, elf_printf_fn_t printf, void *user); | 106 | void elf_write_file(struct elf_params_t *params, elf_write_fn_t write, generic_printf_t printf, void *user); |
107 | bool elf_read_file(struct elf_params_t *params, elf_read_fn_t read, elf_printf_fn_t printf, | 107 | bool elf_read_file(struct elf_params_t *params, elf_read_fn_t read, generic_printf_t printf, void *user); |
108 | void *user); | ||
109 | bool elf_is_empty(struct elf_params_t *params); | 108 | bool elf_is_empty(struct elf_params_t *params); |
110 | void elf_set_start_addr(struct elf_params_t *params, uint32_t addr); | 109 | void elf_set_start_addr(struct elf_params_t *params, uint32_t addr); |
111 | bool elf_get_start_addr(struct elf_params_t *params, uint32_t *addr); | 110 | bool elf_get_start_addr(struct elf_params_t *params, uint32_t *addr); |
@@ -116,7 +115,6 @@ bool elf_guess(elf_read_fn_t read, void *user); | |||
116 | 115 | ||
117 | /* standard implementation of read/write/printf functions | 116 | /* standard implementation of read/write/printf functions |
118 | * with user being a FILE* pointer */ | 117 | * with user being a FILE* pointer */ |
119 | void elf_std_printf(void *user, bool error, const char *fmt, ...); | ||
120 | void elf_std_write(void *user, uint32_t addr, const void *buf, size_t count); | 118 | void elf_std_write(void *user, uint32_t addr, const void *buf, size_t count); |
121 | bool elf_std_read(void *user, uint32_t addr, void *buf, size_t count); | 119 | bool elf_std_read(void *user, uint32_t addr, void *buf, size_t count); |
122 | 120 | ||
diff --git a/utils/imxtools/sbtools/elftosb.c b/utils/imxtools/sbtools/elftosb.c index 4df35ae6e6..c904e42f79 100644 --- a/utils/imxtools/sbtools/elftosb.c +++ b/utils/imxtools/sbtools/elftosb.c | |||
@@ -84,7 +84,7 @@ static void load_elf_by_id(struct cmd_file_t *cmd_file, const char *id) | |||
84 | if(g_debug) | 84 | if(g_debug) |
85 | printf("Loading ELF file '%s'...\n", src->filename); | 85 | printf("Loading ELF file '%s'...\n", src->filename); |
86 | elf_init(&src->elf); | 86 | elf_init(&src->elf); |
87 | src->loaded = elf_read_file(&src->elf, elf_std_read, elf_std_printf, fd); | 87 | src->loaded = elf_read_file(&src->elf, elf_std_read, generic_std_printf, fd); |
88 | fclose(fd); | 88 | fclose(fd); |
89 | if(!src->loaded) | 89 | if(!src->loaded) |
90 | bug("error loading elf file '%s' (id '%s')\n", src->filename, id); | 90 | bug("error loading elf file '%s' (id '%s')\n", src->filename, id); |
@@ -430,7 +430,7 @@ int main(int argc, char **argv) | |||
430 | sb_file->flags = 0; | 430 | sb_file->flags = 0; |
431 | sb_file->minor_version = 1; | 431 | sb_file->minor_version = 1; |
432 | 432 | ||
433 | sb_write_file(sb_file, output_filename); | 433 | sb_write_file(sb_file, output_filename, 0, generic_std_printf); |
434 | sb_free(sb_file); | 434 | sb_free(sb_file); |
435 | clear_keys(); | 435 | clear_keys(); |
436 | 436 | ||
diff --git a/utils/imxtools/sbtools/elftosb1.c b/utils/imxtools/sbtools/elftosb1.c index 43f9b1e9be..83e1c175bc 100644 --- a/utils/imxtools/sbtools/elftosb1.c +++ b/utils/imxtools/sbtools/elftosb1.c | |||
@@ -68,24 +68,6 @@ static char *cmd_line_next_arg(void *user) | |||
68 | return *(uu->argv - 1); | 68 | return *(uu->argv - 1); |
69 | } | 69 | } |
70 | 70 | ||
71 | static bool elf_read(void *user, uint32_t addr, void *buf, size_t count) | ||
72 | { | ||
73 | if(fseek((FILE *)user, addr, SEEK_SET) == -1) | ||
74 | return false; | ||
75 | return fread(buf, 1, count, (FILE *)user) == count; | ||
76 | } | ||
77 | |||
78 | static void elf_printf(void *user, bool error, const char *fmt, ...) | ||
79 | { | ||
80 | if(!g_debug && !error) | ||
81 | return; | ||
82 | (void) user; | ||
83 | va_list args; | ||
84 | va_start(args, fmt); | ||
85 | vprintf(fmt, args); | ||
86 | va_end(args); | ||
87 | } | ||
88 | |||
89 | static int sb1_add_inst(struct sb1_file_t *sb, struct sb1_inst_t *insts, int nr_insts) | 71 | static int sb1_add_inst(struct sb1_file_t *sb, struct sb1_inst_t *insts, int nr_insts) |
90 | { | 72 | { |
91 | sb->insts = augment_array(sb->insts, sizeof(struct sb1_inst_t), sb->nr_insts, | 73 | sb->insts = augment_array(sb->insts, sizeof(struct sb1_inst_t), sb->nr_insts, |
@@ -402,7 +384,7 @@ static int load_elf(struct sb1_file_t *sb, const char *filename, int act) | |||
402 | if(g_debug) | 384 | if(g_debug) |
403 | printf("Loading elf file '%s'...\n", filename); | 385 | printf("Loading elf file '%s'...\n", filename); |
404 | elf_init(&elf); | 386 | elf_init(&elf); |
405 | bool loaded = elf_read_file(&elf, elf_read, elf_printf, fd); | 387 | bool loaded = elf_read_file(&elf, elf_std_read, generic_std_printf, fd); |
406 | fclose(fd); | 388 | fclose(fd); |
407 | if(!loaded) | 389 | if(!loaded) |
408 | bug("error loading elf file '%s'\n", filename); | 390 | bug("error loading elf file '%s'\n", filename); |
diff --git a/utils/imxtools/sbtools/misc.c b/utils/imxtools/sbtools/misc.c index 58e9609909..b36ab7902f 100644 --- a/utils/imxtools/sbtools/misc.c +++ b/utils/imxtools/sbtools/misc.c | |||
@@ -316,6 +316,18 @@ void color(color_t c) | |||
316 | printf("%s", (char *)c); | 316 | printf("%s", (char *)c); |
317 | } | 317 | } |
318 | 318 | ||
319 | void generic_std_printf(void *u, bool err, color_t c, const char *f, ...) | ||
320 | { | ||
321 | (void)u; | ||
322 | if(!g_debug && !err) | ||
323 | return; | ||
324 | va_list args; | ||
325 | va_start(args, f); | ||
326 | color(c); | ||
327 | vprintf(f, args); | ||
328 | va_end(args); | ||
329 | } | ||
330 | |||
319 | enum sb_version_guess_t guess_sb_version(const char *filename) | 331 | enum sb_version_guess_t guess_sb_version(const char *filename) |
320 | { | 332 | { |
321 | #define ret(x) do { if(f) fclose(f); return x; } while(0) | 333 | #define ret(x) do { if(f) fclose(f); return x; } while(0) |
diff --git a/utils/imxtools/sbtools/misc.h b/utils/imxtools/sbtools/misc.h index 624cd5a9c0..01e77c80c1 100644 --- a/utils/imxtools/sbtools/misc.h +++ b/utils/imxtools/sbtools/misc.h | |||
@@ -67,6 +67,10 @@ extern color_t OFF, GREY, RED, GREEN, YELLOW, BLUE; | |||
67 | void color(color_t c); | 67 | void color(color_t c); |
68 | void enable_color(bool enable); | 68 | void enable_color(bool enable); |
69 | 69 | ||
70 | typedef void (*generic_printf_t)(void *u, bool err, color_t c, const char *f, ...); | ||
71 | |||
72 | void generic_std_printf(void *u, bool err, color_t c, const char *f, ...); | ||
73 | |||
70 | enum sb_version_guess_t | 74 | enum sb_version_guess_t |
71 | { | 75 | { |
72 | SB_VERSION_1, | 76 | SB_VERSION_1, |
diff --git a/utils/imxtools/sbtools/sb.c b/utils/imxtools/sbtools/sb.c index 33961d41bc..a642af5945 100644 --- a/utils/imxtools/sbtools/sb.c +++ b/utils/imxtools/sbtools/sb.c | |||
@@ -45,7 +45,7 @@ static void fill_gaps(struct sb_file_t *sb) | |||
45 | } | 45 | } |
46 | } | 46 | } |
47 | 47 | ||
48 | static void compute_sb_offsets(struct sb_file_t *sb, void *u, sb_color_printf cprintf) | 48 | static void compute_sb_offsets(struct sb_file_t *sb, void *u, generic_printf_t cprintf) |
49 | { | 49 | { |
50 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) | 50 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) |
51 | sb->image_size = 0; | 51 | sb->image_size = 0; |
@@ -283,7 +283,7 @@ static void produce_section_tag_cmd(struct sb_section_t *sec, | |||
283 | } | 283 | } |
284 | 284 | ||
285 | void produce_sb_instruction(struct sb_inst_t *inst, | 285 | void produce_sb_instruction(struct sb_inst_t *inst, |
286 | struct sb_instruction_common_t *cmd, void *u, sb_color_printf cprintf) | 286 | struct sb_instruction_common_t *cmd, void *u, generic_printf_t cprintf) |
287 | { | 287 | { |
288 | memset(cmd, 0, sizeof(struct sb_instruction_common_t)); | 288 | memset(cmd, 0, sizeof(struct sb_instruction_common_t)); |
289 | cmd->hdr.opcode = inst->inst; | 289 | cmd->hdr.opcode = inst->inst; |
@@ -318,7 +318,7 @@ void produce_sb_instruction(struct sb_inst_t *inst, | |||
318 | } | 318 | } |
319 | 319 | ||
320 | enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *u, | 320 | enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *u, |
321 | sb_color_printf cprintf) | 321 | generic_printf_t cprintf) |
322 | { | 322 | { |
323 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) | 323 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) |
324 | struct crypto_key_t real_key; | 324 | struct crypto_key_t real_key; |
@@ -475,7 +475,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void * | |||
475 | } | 475 | } |
476 | 476 | ||
477 | static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, | 477 | static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, |
478 | int size, const char *indent, void *u, sb_color_printf cprintf, enum sb_error_t *err) | 478 | int size, const char *indent, void *u, generic_printf_t cprintf, enum sb_error_t *err) |
479 | { | 479 | { |
480 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) | 480 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) |
481 | #define fatal(e, ...) \ | 481 | #define fatal(e, ...) \ |
@@ -640,13 +640,13 @@ static uint32_t guess_alignment(uint32_t off) | |||
640 | } | 640 | } |
641 | 641 | ||
642 | struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, | 642 | struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, |
643 | sb_color_printf cprintf, enum sb_error_t *err) | 643 | generic_printf_t cprintf, enum sb_error_t *err) |
644 | { | 644 | { |
645 | return sb_read_file_ex(filename, 0, -1, raw_mode, u, cprintf, err); | 645 | return sb_read_file_ex(filename, 0, -1, raw_mode, u, cprintf, err); |
646 | } | 646 | } |
647 | 647 | ||
648 | struct sb_file_t *sb_read_file_ex(const char *filename, size_t offset, size_t size, bool raw_mode, void *u, | 648 | struct sb_file_t *sb_read_file_ex(const char *filename, size_t offset, size_t size, bool raw_mode, void *u, |
649 | sb_color_printf cprintf, enum sb_error_t *err) | 649 | generic_printf_t cprintf, enum sb_error_t *err) |
650 | { | 650 | { |
651 | #define fatal(e, ...) \ | 651 | #define fatal(e, ...) \ |
652 | do { if(err) *err = e; \ | 652 | do { if(err) *err = e; \ |
@@ -681,7 +681,7 @@ struct sb_file_t *sb_read_file_ex(const char *filename, size_t offset, size_t si | |||
681 | struct printer_t | 681 | struct printer_t |
682 | { | 682 | { |
683 | void *user; | 683 | void *user; |
684 | sb_color_printf cprintf; | 684 | generic_printf_t cprintf; |
685 | const char *color; | 685 | const char *color; |
686 | bool error; | 686 | bool error; |
687 | }; | 687 | }; |
@@ -698,7 +698,7 @@ static void sb_printer(void *user, const char *fmt, ...) | |||
698 | } | 698 | } |
699 | 699 | ||
700 | struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, void *u, | 700 | struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, void *u, |
701 | sb_color_printf cprintf, enum sb_error_t *err) | 701 | generic_printf_t cprintf, enum sb_error_t *err) |
702 | { | 702 | { |
703 | struct sb_file_t *sb_file = NULL; | 703 | struct sb_file_t *sb_file = NULL; |
704 | uint8_t *buf = _buf; | 704 | uint8_t *buf = _buf; |
@@ -1158,7 +1158,7 @@ void sb_free(struct sb_file_t *file) | |||
1158 | free(file); | 1158 | free(file); |
1159 | } | 1159 | } |
1160 | 1160 | ||
1161 | void sb_dump(struct sb_file_t *file, void *u, sb_color_printf cprintf) | 1161 | void sb_dump(struct sb_file_t *file, void *u, generic_printf_t cprintf) |
1162 | { | 1162 | { |
1163 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) | 1163 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) |
1164 | struct printer_t printer = {.user = u, .cprintf = cprintf, .color = OFF, .error = false }; | 1164 | struct printer_t printer = {.user = u, .cprintf = cprintf, .color = OFF, .error = false }; |
@@ -1284,15 +1284,3 @@ void sb_get_zero_key(struct crypto_key_t *key) | |||
1284 | key->method = CRYPTO_KEY; | 1284 | key->method = CRYPTO_KEY; |
1285 | memset(key->u.key, 0, sizeof(key->u.key)); | 1285 | memset(key->u.key, 0, sizeof(key->u.key)); |
1286 | } | 1286 | } |
1287 | |||
1288 | void sb_std_printf(void *user, bool error, color_t c, const char *fmt, ...) | ||
1289 | { | ||
1290 | (void)user; | ||
1291 | if(!g_debug && !error) | ||
1292 | return; | ||
1293 | va_list args; | ||
1294 | va_start(args, fmt); | ||
1295 | color(c); | ||
1296 | vprintf(fmt, args); | ||
1297 | va_end(args); | ||
1298 | } | ||
diff --git a/utils/imxtools/sbtools/sb.h b/utils/imxtools/sbtools/sb.h index 259ac7369b..4f725da750 100644 --- a/utils/imxtools/sbtools/sb.h +++ b/utils/imxtools/sbtools/sb.h | |||
@@ -231,26 +231,21 @@ enum sb_error_t | |||
231 | SB_LAST_CRYPTO_ERROR = SB_FIRST_CRYPTO_ERROR - CRYPTO_NUM_ERRORS, | 231 | SB_LAST_CRYPTO_ERROR = SB_FIRST_CRYPTO_ERROR - CRYPTO_NUM_ERRORS, |
232 | }; | 232 | }; |
233 | 233 | ||
234 | typedef void (*sb_color_printf)(void *u, bool err, color_t c, const char *f, ...); | ||
235 | |||
236 | enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *u, | 234 | enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *u, |
237 | sb_color_printf printf); | 235 | generic_printf_t printf); |
238 | struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, | 236 | struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, |
239 | sb_color_printf printf, enum sb_error_t *err); | 237 | generic_printf_t printf, enum sb_error_t *err); |
240 | /* use size_t(-1) to use maximum size */ | 238 | /* use size_t(-1) to use maximum size */ |
241 | struct sb_file_t *sb_read_file_ex(const char *filename, size_t offset, size_t size, bool raw_mode, void *u, | 239 | struct sb_file_t *sb_read_file_ex(const char *filename, size_t offset, size_t size, bool raw_mode, void *u, |
242 | sb_color_printf printf, enum sb_error_t *err); | 240 | generic_printf_t printf, enum sb_error_t *err); |
243 | struct sb_file_t *sb_read_memory(void *buffer, size_t size, bool raw_mode, void *u, | 241 | struct sb_file_t *sb_read_memory(void *buffer, size_t size, bool raw_mode, void *u, |
244 | sb_color_printf printf, enum sb_error_t *err); | 242 | generic_printf_t printf, enum sb_error_t *err); |
245 | 243 | ||
246 | void sb_fill_section_name(char name[5], uint32_t identifier); | 244 | void sb_fill_section_name(char name[5], uint32_t identifier); |
247 | void sb_dump(struct sb_file_t *file, void *u, sb_color_printf printf); | 245 | void sb_dump(struct sb_file_t *file, void *u, generic_printf_t printf); |
248 | void sb_free_instruction(struct sb_inst_t inst); | 246 | void sb_free_instruction(struct sb_inst_t inst); |
249 | void sb_free_section(struct sb_section_t file); | 247 | void sb_free_section(struct sb_section_t file); |
250 | void sb_free(struct sb_file_t *file); | 248 | void sb_free(struct sb_file_t *file); |
251 | void sb_get_zero_key(struct crypto_key_t *key); | 249 | void sb_get_zero_key(struct crypto_key_t *key); |
252 | 250 | ||
253 | /* standard implementation: user is unused*/ | ||
254 | void sb_std_printf(void *user, bool error, color_t c, const char *fmt, ...); | ||
255 | |||
256 | #endif /* __SB_H__ */ | 251 | #endif /* __SB_H__ */ |
diff --git a/utils/imxtools/sbtools/sb1.c b/utils/imxtools/sbtools/sb1.c index ca7b7fdc8c..ed5893f454 100644 --- a/utils/imxtools/sbtools/sb1.c +++ b/utils/imxtools/sbtools/sb1.c | |||
@@ -189,13 +189,13 @@ enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename) | |||
189 | } | 189 | } |
190 | 190 | ||
191 | struct sb1_file_t *sb1_read_file(const char *filename, void *u, | 191 | struct sb1_file_t *sb1_read_file(const char *filename, void *u, |
192 | sb1_color_printf cprintf, enum sb1_error_t *err) | 192 | generic_printf_t cprintf, enum sb1_error_t *err) |
193 | { | 193 | { |
194 | return sb1_read_file_ex(filename, 0, -1, u, cprintf, err); | 194 | return sb1_read_file_ex(filename, 0, -1, u, cprintf, err); |
195 | } | 195 | } |
196 | 196 | ||
197 | struct sb1_file_t *sb1_read_file_ex(const char *filename, size_t offset, size_t size, void *u, | 197 | struct sb1_file_t *sb1_read_file_ex(const char *filename, size_t offset, size_t size, void *u, |
198 | sb1_color_printf cprintf, enum sb1_error_t *err) | 198 | generic_printf_t cprintf, enum sb1_error_t *err) |
199 | { | 199 | { |
200 | #define fatal(e, ...) \ | 200 | #define fatal(e, ...) \ |
201 | do { if(err) *err = e; \ | 201 | do { if(err) *err = e; \ |
@@ -270,7 +270,7 @@ bool sb1_is_key_valid_fast(void *buffer, union xorcrypt_key_t _key[2]) | |||
270 | return mark == *(uint32_t *)§or[SECTOR_SIZE - 4 - header->header_size]; | 270 | return mark == *(uint32_t *)§or[SECTOR_SIZE - 4 - header->header_size]; |
271 | } | 271 | } |
272 | 272 | ||
273 | bool sb1_brute_force(const char *filename, void *u, sb1_color_printf cprintf, | 273 | bool sb1_brute_force(const char *filename, void *u, generic_printf_t cprintf, |
274 | enum sb1_error_t *err, struct crypto_key_t *key) | 274 | enum sb1_error_t *err, struct crypto_key_t *key) |
275 | { | 275 | { |
276 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) | 276 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) |
@@ -358,7 +358,7 @@ bool sb1_brute_force(const char *filename, void *u, sb1_color_printf cprintf, | |||
358 | } | 358 | } |
359 | 359 | ||
360 | struct sb1_file_t *sb1_read_memory(void *_buf, size_t filesize, void *u, | 360 | struct sb1_file_t *sb1_read_memory(void *_buf, size_t filesize, void *u, |
361 | sb1_color_printf cprintf, enum sb1_error_t *err) | 361 | generic_printf_t cprintf, enum sb1_error_t *err) |
362 | { | 362 | { |
363 | struct sb1_file_t *file = NULL; | 363 | struct sb1_file_t *file = NULL; |
364 | uint8_t *buf = _buf; | 364 | uint8_t *buf = _buf; |
@@ -606,7 +606,7 @@ void sb1_free(struct sb1_file_t *file) | |||
606 | free(file); | 606 | free(file); |
607 | } | 607 | } |
608 | 608 | ||
609 | void sb1_dump(struct sb1_file_t *file, void *u, sb1_color_printf cprintf) | 609 | void sb1_dump(struct sb1_file_t *file, void *u, generic_printf_t cprintf) |
610 | { | 610 | { |
611 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) | 611 | #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) |
612 | #define print_hex(c, p, len, nl) \ | 612 | #define print_hex(c, p, len, nl) \ |
diff --git a/utils/imxtools/sbtools/sb1.h b/utils/imxtools/sbtools/sb1.h index d5ab042c95..f2dec509b7 100644 --- a/utils/imxtools/sbtools/sb1.h +++ b/utils/imxtools/sbtools/sb1.h | |||
@@ -145,24 +145,23 @@ enum sb1_error_t | |||
145 | 145 | ||
146 | enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename); | 146 | enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename); |
147 | 147 | ||
148 | typedef void (*sb1_color_printf)(void *u, bool err, color_t c, const char *f, ...); | ||
149 | struct sb1_file_t *sb1_read_file(const char *filename, void *u, | 148 | struct sb1_file_t *sb1_read_file(const char *filename, void *u, |
150 | sb1_color_printf printf, enum sb1_error_t *err); | 149 | generic_printf_t printf, enum sb1_error_t *err); |
151 | /* use size_t(-1) to use maximum size */ | 150 | /* use size_t(-1) to use maximum size */ |
152 | struct sb1_file_t *sb1_read_file_ex(const char *filename, size_t offset, size_t size, | 151 | struct sb1_file_t *sb1_read_file_ex(const char *filename, size_t offset, size_t size, |
153 | void *u, sb1_color_printf printf, enum sb1_error_t *err); | 152 | void *u, generic_printf_t printf, enum sb1_error_t *err); |
154 | struct sb1_file_t *sb1_read_memory(void *buffer, size_t size, void *u, | 153 | struct sb1_file_t *sb1_read_memory(void *buffer, size_t size, void *u, |
155 | sb1_color_printf printf, enum sb1_error_t *err); | 154 | generic_printf_t printf, enum sb1_error_t *err); |
156 | 155 | ||
157 | /* do as little checks as possible, make sure the image is valid (advance use only). | 156 | /* do as little checks as possible, make sure the image is valid (advance use only). |
158 | * Buffer should be of size SECTOR_SIZE at least. */ | 157 | * Buffer should be of size SECTOR_SIZE at least. */ |
159 | bool sb1_is_key_valid_fast(void *buffer, union xorcrypt_key_t key[2]); | 158 | bool sb1_is_key_valid_fast(void *buffer, union xorcrypt_key_t key[2]); |
160 | bool sb1_brute_force(const char *filename, void *u, sb1_color_printf printf, | 159 | bool sb1_brute_force(const char *filename, void *u, generic_printf_t printf, |
161 | enum sb1_error_t *err, struct crypto_key_t *key); | 160 | enum sb1_error_t *err, struct crypto_key_t *key); |
162 | 161 | ||
163 | void sb1_get_default_key(struct crypto_key_t *key); | 162 | void sb1_get_default_key(struct crypto_key_t *key); |
164 | 163 | ||
165 | void sb1_dump(struct sb1_file_t *file, void *u, sb1_color_printf printf); | 164 | void sb1_dump(struct sb1_file_t *file, void *u, generic_printf_t printf); |
166 | void sb1_free(struct sb1_file_t *file); | 165 | void sb1_free(struct sb1_file_t *file); |
167 | 166 | ||
168 | #endif /* __SB1_H__ */ | 167 | #endif /* __SB1_H__ */ |
diff --git a/utils/imxtools/sbtools/sbtoelf.c b/utils/imxtools/sbtools/sbtoelf.c index 95fc7381a9..cf1744b086 100644 --- a/utils/imxtools/sbtools/sbtoelf.c +++ b/utils/imxtools/sbtools/sbtoelf.c | |||
@@ -27,6 +27,7 @@ | |||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #define _ISOC99_SOURCE /* snprintf() */ | 29 | #define _ISOC99_SOURCE /* snprintf() */ |
30 | #define _POSIX_C_SOURCE 200809L /* for strdup */ | ||
30 | #include <stdio.h> | 31 | #include <stdio.h> |
31 | #include <errno.h> | 32 | #include <errno.h> |
32 | #include <stdlib.h> | 33 | #include <stdlib.h> |
@@ -161,7 +162,7 @@ static void extract_elf(struct elf_params_t *elf, int count) | |||
161 | return; | 162 | return; |
162 | if(g_elf_simplify) | 163 | if(g_elf_simplify) |
163 | elf_simplify(elf); | 164 | elf_simplify(elf); |
164 | elf_write_file(elf, elf_std_write, elf_std_printf, fd); | 165 | elf_write_file(elf, elf_std_write, generic_std_printf, fd); |
165 | fclose(fd); | 166 | fclose(fd); |
166 | } | 167 | } |
167 | 168 | ||
@@ -350,7 +351,7 @@ int main(int argc, char **argv) | |||
350 | if(force_sb2 || ver == SB_VERSION_2) | 351 | if(force_sb2 || ver == SB_VERSION_2) |
351 | { | 352 | { |
352 | enum sb_error_t err; | 353 | enum sb_error_t err; |
353 | struct sb_file_t *file = sb_read_file(sb_filename, raw_mode, NULL, sb_std_printf, &err); | 354 | struct sb_file_t *file = sb_read_file(sb_filename, raw_mode, NULL, generic_std_printf, &err); |
354 | if(file == NULL) | 355 | if(file == NULL) |
355 | { | 356 | { |
356 | color(OFF); | 357 | color(OFF); |
@@ -365,7 +366,7 @@ int main(int argc, char **argv) | |||
365 | { | 366 | { |
366 | color(GREY); | 367 | color(GREY); |
367 | printf("[Debug output]\n"); | 368 | printf("[Debug output]\n"); |
368 | sb_dump(file, NULL, sb_std_printf); | 369 | sb_dump(file, NULL, generic_std_printf); |
369 | } | 370 | } |
370 | if(loopback) | 371 | if(loopback) |
371 | { | 372 | { |
@@ -374,7 +375,7 @@ int main(int argc, char **argv) | |||
374 | * garbage */ | 375 | * garbage */ |
375 | file->override_real_key = false; | 376 | file->override_real_key = false; |
376 | file->override_crypto_iv = false; | 377 | file->override_crypto_iv = false; |
377 | sb_write_file(file, loopback); | 378 | sb_write_file(file, loopback, 0, generic_std_printf); |
378 | } | 379 | } |
379 | sb_free(file); | 380 | sb_free(file); |
380 | } | 381 | } |
@@ -384,7 +385,7 @@ int main(int argc, char **argv) | |||
384 | { | 385 | { |
385 | struct crypto_key_t key; | 386 | struct crypto_key_t key; |
386 | enum sb1_error_t err; | 387 | enum sb1_error_t err; |
387 | if(!sb1_brute_force(sb_filename, NULL, sb_std_printf, &err, &key)) | 388 | if(!sb1_brute_force(sb_filename, NULL, generic_std_printf, &err, &key)) |
388 | { | 389 | { |
389 | color(OFF); | 390 | color(OFF); |
390 | printf("Brute force failed: %d\n", err); | 391 | printf("Brute force failed: %d\n", err); |
@@ -408,7 +409,7 @@ int main(int argc, char **argv) | |||
408 | } | 409 | } |
409 | 410 | ||
410 | enum sb1_error_t err; | 411 | enum sb1_error_t err; |
411 | struct sb1_file_t *file = sb1_read_file(sb_filename, NULL, sb_std_printf, &err); | 412 | struct sb1_file_t *file = sb1_read_file(sb_filename, NULL, generic_std_printf, &err); |
412 | if(file == NULL) | 413 | if(file == NULL) |
413 | { | 414 | { |
414 | color(OFF); | 415 | color(OFF); |
@@ -423,7 +424,7 @@ int main(int argc, char **argv) | |||
423 | { | 424 | { |
424 | color(GREY); | 425 | color(GREY); |
425 | printf("[Debug output]\n"); | 426 | printf("[Debug output]\n"); |
426 | sb1_dump(file, NULL, sb_std_printf); | 427 | sb1_dump(file, NULL, generic_std_printf); |
427 | } | 428 | } |
428 | if(loopback) | 429 | if(loopback) |
429 | sb1_write_file(file, loopback); | 430 | sb1_write_file(file, loopback); |