imxtools: introduce rsrctool to manipulate rsrc sections
[maemo-rb.git] / utils / imxtools / sbtools / rsrctool.c
blobcb0582245ad66bf08e075f10efb73b3dfce03a83
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)
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 va_list args;
80 va_start(args, fmt);
81 color(c);
82 vprintf(fmt, args);
83 va_end(args);
86 static struct crypto_key_t g_zero_key =
88 .method = CRYPTO_KEY,
89 .u.key = {0}
92 int main(int argc, char **argv)
94 const char *loopback = NULL;
96 while(1)
98 static struct option long_options[] =
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}
109 int c = getopt_long(argc, argv, "?do:k:za:nl:f", long_options, NULL);
110 if(c == -1)
111 break;
112 switch(c)
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':
138 if(!add_keys_from_file(optarg))
139 bug("Cannot add keys from %s\n", optarg);
140 break;
142 case 'z':
144 add_keys(&g_zero_key, 1);
145 break;
147 case 'a':
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;
158 default:
159 abort();
163 if(argc - optind != 1)
165 usage();
166 return 1;
169 const char *rsrc_filename = argv[optind];
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)
175 color(OFF);
176 printf("RSRC read failed: %d\n", err);
177 return 1;
180 color(OFF);
181 if(g_out_prefix)
182 extract_rsrc_file(file);
183 if(g_debug)
185 color(GREY);
186 printf("[Debug output]\n");
187 rsrc_dump(file, NULL, rsrc_printf);
189 if(loopback)
191 rsrc_write_file(file, loopback);
193 rsrc_free(file);
194 clear_keys();
196 return 0;