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 DEFAULT_TFFS_SIZE (256 * 1024)
41 #define TFFS_ID_END 0xffff
42 #define TFFS_ID_TABLE_NAME 0x01ff
44 static char *progname
;
45 static char *input_file
;
46 static unsigned long tffs_size
= DEFAULT_TFFS_SIZE
;
47 static char *name_filter
= NULL
;
48 static bool show_all
= false;
49 static bool print_all_key_names
= false;
50 static bool swap_bytes
= false;
52 struct tffs_entry_header
{
58 const struct tffs_entry_header
*header
;
63 struct tffs_name_table_entry
{
68 struct tffs_key_name_table
{
70 struct tffs_name_table_entry
*entries
;
73 static inline uint16_t get_header_len(const struct tffs_entry_header
*header
)
76 return ntohs(header
->len
);
81 static inline uint16_t get_header_id(const struct tffs_entry_header
*header
)
84 return ntohs(header
->id
);
89 static inline uint16_t to_entry_header_id(uint32_t name_id
)
92 return ntohl(name_id
) & 0xffff;
94 return name_id
& 0xffff;
97 static inline uint32_t get_walk_size(uint32_t entry_len
)
99 return (entry_len
+ 3) & ~0x03;
102 static void print_entry_value(const struct tffs_entry
*entry
)
106 /* These are NOT NULL terminated. */
107 for (i
= 0; i
< get_header_len(entry
->header
); i
++)
108 fprintf(stdout
, "%c", entry
->val
[i
]);
111 static void parse_entry(uint8_t *buffer
, uint32_t pos
,
112 struct tffs_entry
*entry
)
114 entry
->header
= (struct tffs_entry_header
*) &buffer
[pos
];
115 entry
->val
= &buffer
[pos
+ sizeof(struct tffs_entry_header
)];
118 static int find_entry(uint8_t *buffer
, uint16_t id
, struct tffs_entry
*entry
)
123 parse_entry(buffer
, pos
, entry
);
125 if (get_header_id(entry
->header
) == id
)
128 pos
+= sizeof(struct tffs_entry_header
);
129 pos
+= get_walk_size(get_header_len(entry
->header
));
130 } while (pos
< tffs_size
&& entry
->header
->id
!= TFFS_ID_END
);
135 static void parse_key_names(struct tffs_entry
*names_entry
,
136 struct tffs_key_name_table
*key_names
)
138 uint32_t pos
= 0, i
= 0;
139 struct tffs_name_table_entry
*name_item
;
141 key_names
->entries
= calloc(sizeof(*name_item
), 1);
144 name_item
= &key_names
->entries
[i
];
146 name_item
->id
= (uint32_t *) &names_entry
->val
[pos
];
147 pos
+= sizeof(*name_item
->id
);
148 name_item
->val
= (const char *) &names_entry
->val
[pos
];
151 * There is no "length" field because the string values are
152 * simply NULL-terminated -> strlen() gives us the size.
154 pos
+= get_walk_size(strlen(name_item
->val
) + 1);
157 key_names
->entries
= realloc(key_names
->entries
,
158 sizeof(*name_item
) * (i
+ 1));
159 } while (pos
< get_header_len(names_entry
->header
));
164 static void show_all_key_names(struct tffs_key_name_table
*key_names
)
168 for (i
= 0; i
< key_names
->size
; i
++)
169 printf("%s\n", key_names
->entries
[i
].val
);
172 static int show_all_key_value_pairs(uint8_t *buffer
,
173 struct tffs_key_name_table
*key_names
)
175 int i
, has_value
= 0;
177 struct tffs_entry tmp
;
179 for (i
= 0; i
< key_names
->size
; i
++) {
180 id
= to_entry_header_id(*key_names
->entries
[i
].id
);
182 if (find_entry(buffer
, id
, &tmp
)) {
183 printf("%s=", key_names
->entries
[i
].val
);
184 print_entry_value(&tmp
);
191 fprintf(stderr
, "ERROR: no values found!\n");
198 static int show_matching_key_value(uint8_t *buffer
,
199 struct tffs_key_name_table
*key_names
)
203 struct tffs_entry tmp
;
206 for (i
= 0; i
< key_names
->size
; i
++) {
207 name
= key_names
->entries
[i
].val
;
209 if (strncmp(name
, name_filter
, strlen(name
)) == 0) {
210 id
= to_entry_header_id(*key_names
->entries
[i
].id
);
212 if (find_entry(buffer
, id
, &tmp
)) {
213 print_entry_value(&tmp
);
218 "ERROR: no value found for name %s!\n",
225 fprintf(stderr
, "ERROR: Unknown key name %s!\n", name_filter
);
229 static void usage(int status
)
231 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
233 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
237 " -a list all key value pairs found in the TFFS file/device\n"
238 " -b swap bytes while parsing the TFFS file/device\n"
239 " -h show this screen\n"
240 " -i <file> inspect the given TFFS file/device <file>\n"
241 " -l list all supported keys\n"
242 " -n <key name> display the value of the given key\n"
243 " -s <size> the (max) size of the TFFS file/device <size>\n"
249 static int file_exist(char *filename
)
253 return stat(filename
, &buffer
) == 0;
256 static void parse_options(int argc
, char *argv
[])
262 c
= getopt(argc
, argv
, "abhi:ln:s:");
270 print_all_key_names
= false;
282 print_all_key_names
= true;
287 name_filter
= optarg
;
289 print_all_key_names
= false;
292 tffs_size
= strtoul(optarg
, NULL
, 0);
301 fprintf(stderr
, "ERROR: No input file (-i <file>) given!\n");
305 if (!file_exist(input_file
)) {
306 fprintf(stderr
, "ERROR: %s does not exist\n", input_file
);
310 if (!show_all
&& !name_filter
&& !print_all_key_names
) {
312 "ERROR: either -l, -a or -n <key name> is required!\n");
317 int main(int argc
, char *argv
[])
319 int ret
= EXIT_FAILURE
;
322 struct tffs_entry name_table
;
323 struct tffs_key_name_table key_names
;
325 progname
= basename(argv
[0]);
327 parse_options(argc
, argv
);
329 fp
= fopen(input_file
, "r");
332 fprintf(stderr
, "ERROR: Failed to open tffs input file %s\n",
337 buffer
= malloc(tffs_size
);
339 if (fread(buffer
, 1, tffs_size
, fp
) != tffs_size
) {
340 fprintf(stderr
, "ERROR: Failed read tffs file %s\n",
345 if (!find_entry(buffer
, TFFS_ID_TABLE_NAME
, &name_table
)) {
346 fprintf(stderr
,"ERROR: No name table found in tffs file %s\n",
348 fprintf(stderr
," Is byte-swapping (-b) required?\n");
352 parse_key_names(&name_table
, &key_names
);
353 if (key_names
.size
< 1) {
354 fprintf(stderr
, "ERROR: No name table found in tffs file %s\n",
359 if (print_all_key_names
) {
360 show_all_key_names(&key_names
);
362 } else if (show_all
) {
363 ret
= show_all_key_value_pairs(buffer
, &key_names
);
365 ret
= show_matching_key_value(buffer
, &key_names
);
369 free(key_names
.entries
);