1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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() */
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); \
53 static void extract_rsrc_file(struct rsrc_file_t
*file
)
58 static void usage(void)
60 printf("Usage: rsrctool [options] rsrc-file\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");
75 static void rsrc_printf(void *user
, bool error
, color_t c
, const char *fmt
, ...)
88 static struct crypto_key_t g_zero_key
=
94 int main(int argc
, char **argv
)
96 const char *loopback
= NULL
;
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' },
111 int c
= getopt_long(argc
, argv
, "?do:k:za:nl:f", long_options
, NULL
);
120 bug("Only one loopback file can be specified !\n");
133 g_out_prefix
= optarg
;
140 if(!add_keys_from_file(optarg
))
141 bug("Cannot add keys from %s\n", optarg
);
146 add_keys(&g_zero_key
, 1);
151 struct crypto_key_t key
;
153 if(!parse_key(&s
, &key
))
154 bug("Invalid key specified as argument\n");
156 bug("Trailing characters after key specified as argument\n");
165 if(argc
- optind
!= 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
);
178 printf("RSRC read failed: %d\n", err
);
182 printf("%d entries read from file\n", file
->nr_entries
);
186 extract_rsrc_file(file
);
190 printf("[Debug output]\n");
191 rsrc_dump(file
, NULL
, rsrc_printf
);
195 rsrc_write_file(file
, loopback
);