diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/sbtools/sb.c | 12 | ||||
-rw-r--r-- | utils/sbtools/sb.h | 1 | ||||
-rw-r--r-- | utils/sbtools/sbtoelf.c | 82 |
3 files changed, 80 insertions, 15 deletions
diff --git a/utils/sbtools/sb.c b/utils/sbtools/sb.c index 99f953600d..d620c00f42 100644 --- a/utils/sbtools/sb.c +++ b/utils/sbtools/sb.c | |||
@@ -571,7 +571,7 @@ static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, | |||
571 | #undef printf | 571 | #undef printf |
572 | } | 572 | } |
573 | 573 | ||
574 | static void fill_section_name(char name[5], uint32_t identifier) | 574 | void sb_fill_section_name(char name[5], uint32_t identifier) |
575 | { | 575 | { |
576 | name[0] = (identifier >> 24) & 0xff; | 576 | name[0] = (identifier >> 24) & 0xff; |
577 | name[1] = (identifier >> 16) & 0xff; | 577 | name[1] = (identifier >> 16) & 0xff; |
@@ -773,7 +773,7 @@ struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, | |||
773 | struct crypto_key_t k; | 773 | struct crypto_key_t k; |
774 | char *env = getenv("SB_REAL_KEY"); | 774 | char *env = getenv("SB_REAL_KEY"); |
775 | if(!parse_key(&env, &k) || *env) | 775 | if(!parse_key(&env, &k) || *env) |
776 | bug("Invalid SB_REAL_KEY"); | 776 | bug("Invalid SB_REAL_KEY\n"); |
777 | memcpy(real_key, k.u.key, 16); | 777 | memcpy(real_key, k.u.key, 16); |
778 | } | 778 | } |
779 | 779 | ||
@@ -801,7 +801,7 @@ struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, | |||
801 | struct sb_section_header_t *sec_hdr = (struct sb_section_header_t *)&buf[ofs]; | 801 | struct sb_section_header_t *sec_hdr = (struct sb_section_header_t *)&buf[ofs]; |
802 | 802 | ||
803 | char name[5]; | 803 | char name[5]; |
804 | fill_section_name(name, sec_hdr->identifier); | 804 | sb_fill_section_name(name, sec_hdr->identifier); |
805 | int pos = sec_hdr->offset * BLOCK_SIZE; | 805 | int pos = sec_hdr->offset * BLOCK_SIZE; |
806 | int size = sec_hdr->size * BLOCK_SIZE; | 806 | int size = sec_hdr->size * BLOCK_SIZE; |
807 | int data_sec = !(sec_hdr->flags & SECTION_BOOTABLE); | 807 | int data_sec = !(sec_hdr->flags & SECTION_BOOTABLE); |
@@ -889,7 +889,7 @@ struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, | |||
889 | offset += sizeof(struct sb_instruction_tag_t); | 889 | offset += sizeof(struct sb_instruction_tag_t); |
890 | 890 | ||
891 | char name[5]; | 891 | char name[5]; |
892 | fill_section_name(name, tag->identifier); | 892 | sb_fill_section_name(name, tag->identifier); |
893 | int pos = offset; | 893 | int pos = offset; |
894 | int size = tag->len * BLOCK_SIZE; | 894 | int size = tag->len * BLOCK_SIZE; |
895 | int data_sec = !(tag->flags & SECTION_BOOTABLE); | 895 | int data_sec = !(tag->flags & SECTION_BOOTABLE); |
@@ -1004,7 +1004,7 @@ void sb_dump(struct sb_file_t *file, void *u, sb_color_printf cprintf) | |||
1004 | printf(TREE, "+-"); | 1004 | printf(TREE, "+-"); |
1005 | printf(HEADER, "First Boot Section ID: "); | 1005 | printf(HEADER, "First Boot Section ID: "); |
1006 | char name[5]; | 1006 | char name[5]; |
1007 | fill_section_name(name, file->first_boot_sec_id); | 1007 | sb_fill_section_name(name, file->first_boot_sec_id); |
1008 | printf(TEXT, "%08x (%s)\n", file->first_boot_sec_id, name); | 1008 | printf(TEXT, "%08x (%s)\n", file->first_boot_sec_id, name); |
1009 | 1009 | ||
1010 | if(file->real_key) | 1010 | if(file->real_key) |
@@ -1035,7 +1035,7 @@ void sb_dump(struct sb_file_t *file, void *u, sb_color_printf cprintf) | |||
1035 | printf(HEADER, "Section\n"); | 1035 | printf(HEADER, "Section\n"); |
1036 | printf(TREE,"| +-"); | 1036 | printf(TREE,"| +-"); |
1037 | printf(HEADER, "Identifier: "); | 1037 | printf(HEADER, "Identifier: "); |
1038 | fill_section_name(name, sec->identifier); | 1038 | sb_fill_section_name(name, sec->identifier); |
1039 | printf(TEXT, "%08x (%s)\n", sec->identifier, name); | 1039 | printf(TEXT, "%08x (%s)\n", sec->identifier, name); |
1040 | printf(TREE, "| +-"); | 1040 | printf(TREE, "| +-"); |
1041 | printf(HEADER, "Type: "); | 1041 | printf(HEADER, "Type: "); |
diff --git a/utils/sbtools/sb.h b/utils/sbtools/sb.h index 548b3ef55a..39bb8ce59b 100644 --- a/utils/sbtools/sb.h +++ b/utils/sbtools/sb.h | |||
@@ -213,6 +213,7 @@ typedef void (*sb_color_printf)(void *u, bool err, color_t c, const char *f, ... | |||
213 | struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, | 213 | struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, |
214 | sb_color_printf printf); | 214 | sb_color_printf printf); |
215 | 215 | ||
216 | void sb_fill_section_name(char name[5], uint32_t identifier); | ||
216 | void sb_dump(struct sb_file_t *file, void *u, sb_color_printf printf); | 217 | void sb_dump(struct sb_file_t *file, void *u, sb_color_printf printf); |
217 | 218 | ||
218 | #endif /* __SB_H__ */ | 219 | #endif /* __SB_H__ */ |
diff --git a/utils/sbtools/sbtoelf.c b/utils/sbtools/sbtoelf.c index 0199ffb4f5..179f91262f 100644 --- a/utils/sbtools/sbtoelf.c +++ b/utils/sbtools/sbtoelf.c | |||
@@ -76,12 +76,14 @@ static void elf_write(void *user, uint32_t addr, const void *buf, size_t count) | |||
76 | fwrite(buf, count, 1, f); | 76 | fwrite(buf, count, 1, f); |
77 | } | 77 | } |
78 | 78 | ||
79 | static void extract_elf_section(struct elf_params_t *elf, int count, const char *prefix, | 79 | static void extract_elf_section(struct elf_params_t *elf, int count, uint32_t id) |
80 | const char *indent) | ||
81 | { | 80 | { |
82 | char *filename = xmalloc(strlen(prefix) + 32); | 81 | char name[5]; |
83 | sprintf(filename, "%s.%d.elf", prefix, count); | 82 | char *filename = xmalloc(strlen(g_out_prefix) + 32); |
84 | printf("%swrite %s\n", indent, filename); | 83 | sb_fill_section_name(name, id); |
84 | sprintf(filename, "%s%s.%d.elf", g_out_prefix, name, count); | ||
85 | if(g_debug) | ||
86 | printf("Write boot section %s to %s\n", name, filename); | ||
85 | 87 | ||
86 | FILE *fd = fopen(filename, "wb"); | 88 | FILE *fd = fopen(filename, "wb"); |
87 | free(filename); | 89 | free(filename); |
@@ -92,12 +94,74 @@ static void extract_elf_section(struct elf_params_t *elf, int count, const char | |||
92 | fclose(fd); | 94 | fclose(fd); |
93 | } | 95 | } |
94 | 96 | ||
97 | static void extract_sb_section(struct sb_section_t *sec) | ||
98 | { | ||
99 | if(sec->is_data) | ||
100 | { | ||
101 | char sec_name[5]; | ||
102 | char *filename = xmalloc(strlen(g_out_prefix) + 32); | ||
103 | sb_fill_section_name(sec_name, sec->identifier); | ||
104 | sprintf(filename, "%s%s.bin", g_out_prefix, sec_name); | ||
105 | FILE *fd = fopen(filename, "wb"); | ||
106 | if(fd == NULL) | ||
107 | bugp("Cannot open %s for writing\n", filename); | ||
108 | if(g_debug) | ||
109 | printf("Write data section %s to %s\n", sec_name, filename); | ||
110 | free(filename); | ||
111 | |||
112 | for(int j = 0; j < sec->nr_insts; j++) | ||
113 | { | ||
114 | assert(sec->insts[j].inst == SB_INST_DATA); | ||
115 | fwrite(sec->insts[j].data, sec->insts[j].size, 1, fd); | ||
116 | } | ||
117 | fclose(fd); | ||
118 | } | ||
119 | |||
120 | int elf_count = 0; | ||
121 | struct elf_params_t elf; | ||
122 | elf_init(&elf); | ||
123 | |||
124 | for(int i = 0; i < sec->nr_insts; i++) | ||
125 | { | ||
126 | struct sb_inst_t *inst = &sec->insts[i]; | ||
127 | switch(inst->inst) | ||
128 | { | ||
129 | case SB_INST_LOAD: | ||
130 | elf_add_load_section(&elf, inst->addr, inst->size, inst->data); | ||
131 | break; | ||
132 | case SB_INST_FILL: | ||
133 | elf_add_fill_section(&elf, inst->addr, inst->size, inst->pattern); | ||
134 | break; | ||
135 | case SB_INST_CALL: | ||
136 | case SB_INST_JUMP: | ||
137 | elf_set_start_addr(&elf, inst->addr); | ||
138 | extract_elf_section(&elf, elf_count++, sec->identifier); | ||
139 | elf_release(&elf); | ||
140 | elf_init(&elf); | ||
141 | break; | ||
142 | default: | ||
143 | /* ignore mode and nop */ | ||
144 | break; | ||
145 | } | ||
146 | } | ||
147 | |||
148 | if(!elf_is_empty(&elf)) | ||
149 | extract_elf_section(&elf, elf_count, sec->identifier); | ||
150 | elf_release(&elf); | ||
151 | } | ||
152 | |||
153 | static void extract_sb_file(struct sb_file_t *file) | ||
154 | { | ||
155 | for(int i = 0; i < file->nr_sections; i++) | ||
156 | extract_sb_section(&file->sections[i]); | ||
157 | } | ||
158 | |||
95 | static void usage(void) | 159 | static void usage(void) |
96 | { | 160 | { |
97 | printf("Usage: sbtoelf [options] sb-file\n"); | 161 | printf("Usage: sbtoelf [options] sb-file\n"); |
98 | printf("Options:\n"); | 162 | printf("Options:\n"); |
99 | printf(" -?/--help\tDisplay this message\n"); | 163 | printf(" -?/--help\tDisplay this message\n"); |
100 | printf(" -o <file>\tSet output prefix\n"); | 164 | printf(" -o <prefix>\tEnable output and set prefix\n"); |
101 | printf(" -d/--debug\tEnable debug output*\n"); | 165 | printf(" -d/--debug\tEnable debug output*\n"); |
102 | printf(" -k <file>\tAdd key file\n"); | 166 | printf(" -k <file>\tAdd key file\n"); |
103 | printf(" -z\t\tAdd zero key\n"); | 167 | printf(" -z\t\tAdd zero key\n"); |
@@ -196,9 +260,6 @@ int main(int argc, char **argv) | |||
196 | } | 260 | } |
197 | } | 261 | } |
198 | 262 | ||
199 | if(g_out_prefix == NULL) | ||
200 | g_out_prefix = ""; | ||
201 | |||
202 | if(argc - optind != 1) | 263 | if(argc - optind != 1) |
203 | { | 264 | { |
204 | usage(); | 265 | usage(); |
@@ -208,6 +269,9 @@ int main(int argc, char **argv) | |||
208 | const char *sb_filename = argv[optind]; | 269 | const char *sb_filename = argv[optind]; |
209 | 270 | ||
210 | struct sb_file_t *file = sb_read_file(sb_filename, raw_mode, NULL, sb_printf); | 271 | struct sb_file_t *file = sb_read_file(sb_filename, raw_mode, NULL, sb_printf); |
272 | color(OFF); | ||
273 | if(g_out_prefix) | ||
274 | extract_sb_file(file); | ||
211 | if(g_debug) | 275 | if(g_debug) |
212 | { | 276 | { |
213 | color(GREY); | 277 | color(GREY); |