summaryrefslogtreecommitdiff
path: root/utils/imxtools/sbtools/rsrctool.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/imxtools/sbtools/rsrctool.c')
-rw-r--r--utils/imxtools/sbtools/rsrctool.c198
1 files changed, 198 insertions, 0 deletions
diff --git a/utils/imxtools/sbtools/rsrctool.c b/utils/imxtools/sbtools/rsrctool.c
new file mode 100644
index 0000000000..cb0582245a
--- /dev/null
+++ b/utils/imxtools/sbtools/rsrctool.c
@@ -0,0 +1,198 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2012 Amaury Pouly
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#define _ISOC99_SOURCE /* snprintf() */
23#include <stdio.h>
24#include <errno.h>
25#include <stdlib.h>
26#include <string.h>
27#include <ctype.h>
28#include <time.h>
29#include <stdarg.h>
30#include <strings.h>
31#include <getopt.h>
32
33#include "crypto.h"
34#include "rsrc.h"
35#include "misc.h"
36
37/* all blocks are sized as a multiple of 0x1ff */
38#define PAD_TO_BOUNDARY(x) (((x) + 0x1ff) & ~0x1ff)
39
40/* If you find a firmware that breaks the known format ^^ */
41#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)
42
43#define crypto_cbc(...) \
44 do { int ret = crypto_cbc(__VA_ARGS__); \
45 if(ret != CRYPTO_ERROR_SUCCESS) \
46 bug("crypto_cbc error: %d\n", ret); \
47 }while(0)
48
49/* globals */
50
51char *g_out_prefix;
52
53static void extract_rsrc_file(struct rsrc_file_t *file)
54{
55
56}
57
58static void usage(void)
59{
60 printf("Usage: rsrctool [options] rsrc-file\n");
61 printf("Options:\n");
62 printf(" -?/--help\tDisplay this message\n");
63 printf(" -o <prefix>\tEnable output and set prefix\n");
64 printf(" -d/--debug\tEnable debug output*\n");
65 printf(" -k <file>\tAdd key file\n");
66 printf(" -z\t\tAdd zero key\n");
67 printf(" -a/--add-key <key>\tAdd single key (hex or usbotp)\n");
68 printf(" -n/--no-color\tDisable output colors\n");
69 printf(" -l/--loopback <file>\tProduce rsrc file out of extracted description*\n");
70 printf(" -f/--force\tForce reading even without a key*\n");
71 printf("Options marked with a * are for debug purpose only\n");
72 exit(1);
73}
74
75static void rsrc_printf(void *user, bool error, color_t c, const char *fmt, ...)
76{
77 (void) user;
78 (void) error;
79 va_list args;
80 va_start(args, fmt);
81 color(c);
82 vprintf(fmt, args);
83 va_end(args);
84}
85
86static struct crypto_key_t g_zero_key =
87{
88 .method = CRYPTO_KEY,
89 .u.key = {0}
90};
91
92int main(int argc, char **argv)
93{
94 const char *loopback = NULL;
95
96 while(1)
97 {
98 static struct option long_options[] =
99 {
100 {"help", no_argument, 0, '?'},
101 {"debug", no_argument, 0, 'd'},
102 {"add-key", required_argument, 0, 'a'},
103 {"no-color", no_argument, 0, 'n'},
104 {"loopback", required_argument, 0, 'l'},
105 {"force", no_argument, 0, 'f' },
106 {0, 0, 0, 0}
107 };
108
109 int c = getopt_long(argc, argv, "?do:k:za:nl:f", long_options, NULL);
110 if(c == -1)
111 break;
112 switch(c)
113 {
114 case -1:
115 break;
116 case 'l':
117 if(loopback)
118 bug("Only one loopback file can be specified !\n");
119 loopback = optarg;
120 break;
121 case 'n':
122 enable_color(false);
123 break;
124 case 'd':
125 g_debug = true;
126 break;
127 case '?':
128 usage();
129 break;
130 case 'o':
131 g_out_prefix = optarg;
132 break;
133 case 'f':
134 g_force = true;
135 break;
136 case 'k':
137 {
138 if(!add_keys_from_file(optarg))
139 bug("Cannot add keys from %s\n", optarg);
140 break;
141 }
142 case 'z':
143 {
144 add_keys(&g_zero_key, 1);
145 break;
146 }
147 case 'a':
148 {
149 struct crypto_key_t key;
150 char *s = optarg;
151 if(!parse_key(&s, &key))
152 bug("Invalid key specified as argument\n");
153 if(*s != 0)
154 bug("Trailing characters after key specified as argument\n");
155 add_keys(&key, 1);
156 break;
157 }
158 default:
159 abort();
160 }
161 }
162
163 if(argc - optind != 1)
164 {
165 usage();
166 return 1;
167 }
168
169 const char *rsrc_filename = argv[optind];
170
171 enum rsrc_error_t err;
172 struct rsrc_file_t *file = rsrc_read_file(rsrc_filename, NULL, rsrc_printf, &err);
173 if(file == NULL)
174 {
175 color(OFF);
176 printf("RSRC read failed: %d\n", err);
177 return 1;
178 }
179
180 color(OFF);
181 if(g_out_prefix)
182 extract_rsrc_file(file);
183 if(g_debug)
184 {
185 color(GREY);
186 printf("[Debug output]\n");
187 rsrc_dump(file, NULL, rsrc_printf);
188 }
189 if(loopback)
190 {
191 rsrc_write_file(file, loopback);
192 }
193 rsrc_free(file);
194 clear_keys();
195
196 return 0;
197}
198