sbtools: add option to force sb dump
[maemo-rb.git] / utils / imxtools / sbtools / sbtoelf.c
blob0170ea1836a0d6c0eb961fd72115a347d5e2752f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Bertrik Sikken
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 * .sb file parser and chunk extractor
25 * Based on amsinfo, which is
26 * Copyright © 2008 Rafaël Carré <rafael.carre@gmail.com>
29 #define _ISOC99_SOURCE /* snprintf() */
30 #include <stdio.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <time.h>
36 #include <stdarg.h>
37 #include <strings.h>
38 #include <getopt.h>
40 #include "crypto.h"
41 #include "elf.h"
42 #include "sb.h"
43 #include "misc.h"
45 /* all blocks are sized as a multiple of 0x1ff */
46 #define PAD_TO_BOUNDARY(x) (((x) + 0x1ff) & ~0x1ff)
48 /* If you find a firmware that breaks the known format ^^ */
49 #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)
51 #define crypto_cbc(...) \
52 do { int ret = crypto_cbc(__VA_ARGS__); \
53 if(ret != CRYPTO_ERROR_SUCCESS) \
54 bug("crypto_cbc error: %d\n", ret); \
55 }while(0)
57 /* globals */
59 char *g_out_prefix;
61 static void elf_printf(void *user, bool error, const char *fmt, ...)
63 if(!g_debug && !error)
64 return;
65 (void) user;
66 va_list args;
67 va_start(args, fmt);
68 vprintf(fmt, args);
69 va_end(args);
72 static void elf_write(void *user, uint32_t addr, const void *buf, size_t count)
74 FILE *f = user;
75 fseek(f, addr, SEEK_SET);
76 fwrite(buf, count, 1, f);
79 static void extract_elf_section(struct elf_params_t *elf, int count, uint32_t id)
81 char name[5];
82 char *filename = xmalloc(strlen(g_out_prefix) + 32);
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);
88 FILE *fd = fopen(filename, "wb");
89 free(filename);
91 if(fd == NULL)
92 return ;
93 elf_write_file(elf, elf_write, elf_printf, fd);
94 fclose(fd);
97 static void extract_sb_section(struct sb_section_t *sec)
99 if(sec->is_data)
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);
112 for(int j = 0; j < sec->nr_insts; j++)
114 assert(sec->insts[j].inst == SB_INST_DATA);
115 fwrite(sec->insts[j].data, sec->insts[j].size, 1, fd);
117 fclose(fd);
120 int elf_count = 0;
121 struct elf_params_t elf;
122 elf_init(&elf);
124 for(int i = 0; i < sec->nr_insts; i++)
126 struct sb_inst_t *inst = &sec->insts[i];
127 switch(inst->inst)
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;
148 if(!elf_is_empty(&elf))
149 extract_elf_section(&elf, elf_count, sec->identifier);
150 elf_release(&elf);
153 static void extract_sb_file(struct sb_file_t *file)
155 for(int i = 0; i < file->nr_sections; i++)
156 extract_sb_section(&file->sections[i]);
159 static void usage(void)
161 printf("Usage: sbtoelf [options] sb-file\n");
162 printf("Options:\n");
163 printf(" -?/--help\tDisplay this message\n");
164 printf(" -o <prefix>\tEnable output and set prefix\n");
165 printf(" -d/--debug\tEnable debug output*\n");
166 printf(" -k <file>\tAdd key file\n");
167 printf(" -z\t\tAdd zero key\n");
168 printf(" -r\t\tUse raw command mode\n");
169 printf(" -a/--add-key <key>\tAdd single key (hex or usbotp)\n");
170 printf(" -n/--no-color\tDisable output colors\n");
171 printf(" -l/--loopback <file>\tProduce sb file out of extracted description*\n");
172 printf(" -f/--force\tForce reading even without a key*\n");
173 printf("Options marked with a * are for debug purpose only\n");
174 exit(1);
177 static void sb_printf(void *user, bool error, color_t c, const char *fmt, ...)
179 (void) user;
180 (void) error;
181 va_list args;
182 va_start(args, fmt);
183 color(c);
184 vprintf(fmt, args);
185 va_end(args);
188 static struct crypto_key_t g_zero_key =
190 .method = CRYPTO_KEY,
191 .u.key = {0}
194 int main(int argc, char **argv)
196 bool raw_mode = false;
197 const char *loopback = NULL;
199 while(1)
201 static struct option long_options[] =
203 {"help", no_argument, 0, '?'},
204 {"debug", no_argument, 0, 'd'},
205 {"add-key", required_argument, 0, 'a'},
206 {"no-color", no_argument, 0, 'n'},
207 {"loopback", required_argument, 0, 'l'},
208 {"force", no_argument, 0, 'f' },
209 {0, 0, 0, 0}
212 int c = getopt_long(argc, argv, "?do:k:zra:nl:f", long_options, NULL);
213 if(c == -1)
214 break;
215 switch(c)
217 case -1:
218 break;
219 case 'l':
220 if(loopback)
221 bug("Only one loopback file can be specified !\n");
222 loopback = optarg;
223 break;
224 case 'n':
225 enable_color(false);
226 break;
227 case 'd':
228 g_debug = true;
229 break;
230 case '?':
231 usage();
232 break;
233 case 'o':
234 g_out_prefix = optarg;
235 break;
236 case 'f':
237 g_force = true;
238 break;
239 case 'k':
241 if(!add_keys_from_file(optarg))
242 bug("Cannot add keys from %s\n", optarg);
243 break;
245 case 'z':
247 add_keys(&g_zero_key, 1);
248 break;
250 case 'r':
251 raw_mode = true;
252 break;
253 case 'a':
255 struct crypto_key_t key;
256 char *s = optarg;
257 if(!parse_key(&s, &key))
258 bug("Invalid key specified as argument\n");
259 if(*s != 0)
260 bug("Trailing characters after key specified as argument\n");
261 add_keys(&key, 1);
262 break;
264 default:
265 abort();
269 if(argc - optind != 1)
271 usage();
272 return 1;
275 const char *sb_filename = argv[optind];
277 enum sb_error_t err;
278 struct sb_file_t *file = sb_read_file(sb_filename, raw_mode, NULL, sb_printf, &err);
279 if(file == NULL)
281 color(OFF);
282 printf("SB read failed: %d\n", err);
283 return 1;
286 color(OFF);
287 if(g_out_prefix)
288 extract_sb_file(file);
289 if(g_debug)
291 color(GREY);
292 printf("[Debug output]\n");
293 sb_dump(file, NULL, sb_printf);
295 if(loopback)
297 /* sb_read_file will fill real key and IV but we don't want to override
298 * them when looping back otherwise the output will be inconsistent and
299 * garbage */
300 file->override_real_key = false;
301 file->override_crypto_iv = false;
302 sb_write_file(file, loopback);
304 sb_free(file);
305 clear_keys();
307 return 0;