update Chinese(Simplified) translation
[maemo-rb.git] / utils / imxtools / sbtools / rsrctool.c
blob53235365ae9418d520727e0d4afacc5e45156487
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2012 Amaury Pouly
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 ****************************************************************************/
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>
33 #include "crypto.h"
34 #include "rsrc.h"
35 #include "misc.h"
37 /* all blocks are sized as a multiple of 0x1ff */
38 #define PAD_TO_BOUNDARY(x) (((x) + 0x1ff) & ~0x1ff)
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)
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)
49 /* globals */
51 char *g_out_prefix;
53 static void extract_rsrc_file(struct rsrc_file_t *file)
55 (void) file;
58 static void usage(void)
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);
75 static void rsrc_printf(void *user, bool error, color_t c, const char *fmt, ...)
77 (void) user;
78 (void) error;
79 if(!g_debug)
80 return;
81 va_list args;
82 va_start(args, fmt);
83 color(c);
84 vprintf(fmt, args);
85 va_end(args);
88 static struct crypto_key_t g_zero_key =
90 .method = CRYPTO_KEY,
91 .u.key = {0}
94 int main(int argc, char **argv)
96 const char *loopback = NULL;
98 while(1)
100 static struct option long_options[] =
102 {"help", no_argument, 0, '?'},
103 {"debug", no_argument, 0, 'd'},
104 {"add-key", required_argument, 0, 'a'},
105 {"no-color", no_argument, 0, 'n'},
106 {"loopback", required_argument, 0, 'l'},
107 {"force", no_argument, 0, 'f' },
108 {0, 0, 0, 0}
111 int c = getopt_long(argc, argv, "?do:k:za:nl:f", long_options, NULL);
112 if(c == -1)
113 break;
114 switch(c)
116 case -1:
117 break;
118 case 'l':
119 if(loopback)
120 bug("Only one loopback file can be specified !\n");
121 loopback = optarg;
122 break;
123 case 'n':
124 enable_color(false);
125 break;
126 case 'd':
127 g_debug = true;
128 break;
129 case '?':
130 usage();
131 break;
132 case 'o':
133 g_out_prefix = optarg;
134 break;
135 case 'f':
136 g_force = true;
137 break;
138 case 'k':
140 if(!add_keys_from_file(optarg))
141 bug("Cannot add keys from %s\n", optarg);
142 break;
144 case 'z':
146 add_keys(&g_zero_key, 1);
147 break;
149 case 'a':
151 struct crypto_key_t key;
152 char *s = optarg;
153 if(!parse_key(&s, &key))
154 bug("Invalid key specified as argument\n");
155 if(*s != 0)
156 bug("Trailing characters after key specified as argument\n");
157 add_keys(&key, 1);
158 break;
160 default:
161 abort();
165 if(argc - optind != 1)
167 usage();
168 return 1;
171 const char *rsrc_filename = argv[optind];
173 enum rsrc_error_t err;
174 struct rsrc_file_t *file = rsrc_read_file(rsrc_filename, NULL, rsrc_printf, &err);
175 if(file == NULL)
177 color(OFF);
178 printf("RSRC read failed: %d\n", err);
179 return 1;
181 if(g_debug)
182 printf("%d entries read from file\n", file->nr_entries);
184 color(OFF);
185 if(g_out_prefix)
186 extract_rsrc_file(file);
187 if(g_debug)
189 color(GREY);
190 printf("[Debug output]\n");
191 rsrc_dump(file, NULL, rsrc_printf);
193 if(loopback)
195 rsrc_write_file(file, loopback);
197 rsrc_free(file);
198 clear_keys();
200 return 0;