2 * A tool for reading the TFFS partitions (a name-value storage usually
3 * found in AVM Fritz!Box based devices).
5 * Copyright (c) 2015-2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
7 * Based on the TFFS 2.0 kernel driver from AVM:
8 * Copyright (c) 2004-2007 AVM GmbH <fritzbox_info@avm.de>
9 * and the OpenWrt TFFS kernel driver:
10 * Copyright (c) 2013 John Crispin <blogic@openwrt.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 #include <sys/types.h>
37 #include <arpa/inet.h>
39 #define TFFS_ID_END 0xffff
40 #define TFFS_ID_TABLE_NAME 0x01ff
42 static char *progname;
43 static char *input_file;
44 static unsigned long tffs_size;
45 static char *name_filter = NULL;
46 static bool show_all = false;
47 static bool print_all_key_names = false;
48 static bool swap_bytes = false;
50 struct tffs_entry_header {
56 const struct tffs_entry_header *header;
61 struct tffs_name_table_entry {
66 struct tffs_key_name_table {
68 struct tffs_name_table_entry *entries;
71 static inline uint16_t get_header_len(const struct tffs_entry_header *header)
74 return ntohs(header->len);
79 static inline uint16_t get_header_id(const struct tffs_entry_header *header)
82 return ntohs(header->id);
87 static inline uint16_t to_entry_header_id(uint32_t name_id)
90 return ntohl(name_id) & 0xffff;
92 return name_id & 0xffff;
95 static inline uint32_t get_walk_size(uint32_t entry_len)
97 return (entry_len + 3) & ~0x03;
100 static void print_entry_value(const struct tffs_entry *entry)
104 /* These are NOT NULL terminated. */
105 for (i = 0; i < get_header_len(entry->header); i++)
106 fprintf(stdout, "%c", entry->val[i]);
109 static void parse_entry(uint8_t *buffer, uint32_t pos,
110 struct tffs_entry *entry)
112 entry->header = (struct tffs_entry_header *) &buffer[pos];
113 entry->val = &buffer[pos + sizeof(struct tffs_entry_header)];
116 static int find_entry(uint8_t *buffer, uint16_t id, struct tffs_entry *entry)
121 parse_entry(buffer, pos, entry);
123 if (get_header_id(entry->header) == id)
126 pos += sizeof(struct tffs_entry_header);
127 pos += get_walk_size(get_header_len(entry->header));
128 } while (pos < tffs_size && entry->header->id != TFFS_ID_END);
133 static void parse_key_names(struct tffs_entry *names_entry,
134 struct tffs_key_name_table *key_names)
136 uint32_t pos = 0, i = 0;
137 struct tffs_name_table_entry *name_item;
139 key_names->entries = calloc(sizeof(*name_item), 1);
142 name_item = &key_names->entries[i];
144 name_item->id = (uint32_t *) &names_entry->val[pos];
145 pos += sizeof(*name_item->id);
146 name_item->val = (const char *) &names_entry->val[pos];
149 * There is no "length" field because the string values are
150 * simply NULL-terminated -> strlen() gives us the size.
152 pos += get_walk_size(strlen(name_item->val) + 1);
155 key_names->entries = realloc(key_names->entries,
156 sizeof(*name_item) * (i + 1));
157 } while (pos < get_header_len(names_entry->header));
162 static void show_all_key_names(struct tffs_key_name_table *key_names)
166 for (i = 0; i < key_names->size; i++)
167 printf("%s\n", key_names->entries[i].val);
170 static int show_all_key_value_pairs(uint8_t *buffer,
171 struct tffs_key_name_table *key_names)
173 int i, has_value = 0;
175 struct tffs_entry tmp;
177 for (i = 0; i < key_names->size; i++) {
178 id = to_entry_header_id(*key_names->entries[i].id);
180 if (find_entry(buffer, id, &tmp)) {
181 printf("%s=", key_names->entries[i].val);
182 print_entry_value(&tmp);
189 fprintf(stderr, "ERROR: no values found!\n");
196 static int show_matching_key_value(uint8_t *buffer,
197 struct tffs_key_name_table *key_names)
201 struct tffs_entry tmp;
204 for (i = 0; i < key_names->size; i++) {
205 name = key_names->entries[i].val;
207 if (strncmp(name, name_filter, strlen(name)) == 0) {
208 id = to_entry_header_id(*key_names->entries[i].id);
210 if (find_entry(buffer, id, &tmp)) {
211 print_entry_value(&tmp);
216 "ERROR: no value found for name %s!\n",
223 fprintf(stderr, "ERROR: Unknown key name %s!\n", name_filter);
227 static void usage(int status)
229 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
231 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
235 " -a list all key value pairs found in the TFFS file/device\n"
236 " -b swap bytes while parsing the TFFS file/device\n"
237 " -h show this screen\n"
238 " -i <file> inspect the given TFFS file/device <file>\n"
239 " -l list all supported keys\n"
240 " -n <key name> display the value of the given key\n"
241 " -s <size> the (max) size of the TFFS file/device <size>\n"
247 static int file_exist(char *filename)
251 return stat(filename, &buffer) == 0;
254 static void parse_options(int argc, char *argv[])
260 c = getopt(argc, argv, "abhi:ln:s:");
268 print_all_key_names = false;
280 print_all_key_names = true;
285 name_filter = optarg;
287 print_all_key_names = false;
290 tffs_size = strtoul(optarg, NULL, 0);
299 fprintf(stderr, "ERROR: No input file (-i <file>) given!\n");
303 if (!file_exist(input_file)) {
304 fprintf(stderr, "ERROR: %s does not exist\n", input_file);
308 if (!show_all && !name_filter && !print_all_key_names) {
310 "ERROR: either -l, -a or -n <key name> is required!\n");
315 int main(int argc, char *argv[])
317 int ret = EXIT_FAILURE;
320 struct tffs_entry name_table;
321 struct tffs_key_name_table key_names;
323 progname = basename(argv[0]);
325 parse_options(argc, argv);
327 fp = fopen(input_file, "r");
330 fprintf(stderr, "ERROR: Failed to open tffs input file %s\n",
335 if (tffs_size == 0) {
336 fseek(fp, 0L, SEEK_END);
337 tffs_size = ftell(fp);
338 fseek(fp, 0L, SEEK_SET);
341 buffer = malloc(tffs_size);
343 if (fread(buffer, 1, tffs_size, fp) != tffs_size) {
344 fprintf(stderr, "ERROR: Failed read tffs file %s\n",
349 if (!find_entry(buffer, TFFS_ID_TABLE_NAME, &name_table)) {
350 fprintf(stderr,"ERROR: No name table found in tffs file %s\n",
352 fprintf(stderr," Is byte-swapping (-b) required?\n");
356 parse_key_names(&name_table, &key_names);
357 if (key_names.size < 1) {
358 fprintf(stderr, "ERROR: No name table found in tffs file %s\n",
363 if (print_all_key_names) {
364 show_all_key_names(&key_names);
366 } else if (show_all) {
367 ret = show_all_key_value_pairs(buffer, &key_names);
369 ret = show_matching_key_value(buffer, &key_names);
373 free(key_names.entries);