firmware: add IPQ fritz-tools FW extraction tool
[oweals/openwrt.git] / package / firmware / fritz-tools / src / fritz_tffs_read.c
1 /*
2  * A tool for reading the TFFS partitions (a name-value storage usually
3  * found in AVM Fritz!Box based devices).
4  *
5  * Copyright (c) 2015-2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
6  *
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>
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <libgen.h>
33 #include <getopt.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <arpa/inet.h>
38
39 #define DEFAULT_TFFS_SIZE       (256 * 1024)
40
41 #define TFFS_ID_END             0xffff
42 #define TFFS_ID_TABLE_NAME      0x01ff
43
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;
51
52 struct tffs_entry_header {
53         uint16_t id;
54         uint16_t len;
55 };
56
57 struct tffs_entry {
58         const struct tffs_entry_header *header;
59         char *name;
60         uint8_t *val;
61 };
62
63 struct tffs_name_table_entry {
64         const uint32_t *id;
65         const char *val;
66 };
67
68 struct tffs_key_name_table {
69         uint32_t size;
70         struct tffs_name_table_entry *entries;
71 };
72
73 static inline uint16_t get_header_len(const struct tffs_entry_header *header)
74 {
75         if (swap_bytes)
76                 return ntohs(header->len);
77
78         return header->len;
79 }
80
81 static inline uint16_t get_header_id(const struct tffs_entry_header *header)
82 {
83         if (swap_bytes)
84                 return ntohs(header->id);
85
86         return header->id;
87 }
88
89 static inline uint16_t to_entry_header_id(uint32_t name_id)
90 {
91         if (swap_bytes)
92                 return ntohl(name_id) & 0xffff;
93
94         return name_id & 0xffff;
95 }
96
97 static inline uint32_t get_walk_size(uint32_t entry_len)
98 {
99         return (entry_len + 3) & ~0x03;
100 }
101
102 static void print_entry_value(const struct tffs_entry *entry)
103 {
104         int i;
105
106         /* These are NOT NULL terminated. */
107         for (i = 0; i < get_header_len(entry->header); i++)
108                 fprintf(stdout, "%c", entry->val[i]);
109 }
110
111 static void parse_entry(uint8_t *buffer, uint32_t pos,
112                         struct tffs_entry *entry)
113 {
114         entry->header = (struct tffs_entry_header *) &buffer[pos];
115         entry->val = &buffer[pos + sizeof(struct tffs_entry_header)];
116 }
117
118 static int find_entry(uint8_t *buffer, uint16_t id, struct tffs_entry *entry)
119 {
120         uint32_t pos = 0;
121
122         do {
123                 parse_entry(buffer, pos, entry);
124
125                 if (get_header_id(entry->header) == id)
126                         return 1;
127
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);
131
132         return 0;
133 }
134
135 static void parse_key_names(struct tffs_entry *names_entry,
136                             struct tffs_key_name_table *key_names)
137 {
138         uint32_t pos = 0, i = 0;
139         struct tffs_name_table_entry *name_item;
140
141         key_names->entries = calloc(sizeof(*name_item), 1);
142
143         do {
144                 name_item = &key_names->entries[i];
145
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];
149
150                 /*
151                  * There is no "length" field because the string values are
152                  * simply NULL-terminated -> strlen() gives us the size.
153                  */
154                 pos += get_walk_size(strlen(name_item->val) + 1);
155
156                 ++i;
157                 key_names->entries = realloc(key_names->entries,
158                                                 sizeof(*name_item) * (i + 1));
159         } while (pos < get_header_len(names_entry->header));
160
161         key_names->size = i;
162 }
163
164 static void show_all_key_names(struct tffs_key_name_table *key_names)
165 {
166         int i;
167
168         for (i = 0; i < key_names->size; i++)
169                 printf("%s\n", key_names->entries[i].val);
170 }
171
172 static int show_all_key_value_pairs(uint8_t *buffer,
173                                     struct tffs_key_name_table *key_names)
174 {
175         int i, has_value = 0;
176         uint16_t id;
177         struct tffs_entry tmp;
178
179         for (i = 0; i < key_names->size; i++) {
180                 id = to_entry_header_id(*key_names->entries[i].id);
181
182                 if (find_entry(buffer, id, &tmp)) {
183                         printf("%s=", key_names->entries[i].val);
184                         print_entry_value(&tmp);
185                         printf("\n");
186                         has_value++;
187                 }
188         }
189
190         if (!has_value) {
191                 fprintf(stderr, "ERROR: no values found!\n");
192                 return EXIT_FAILURE;
193         }
194
195         return EXIT_SUCCESS;
196 }
197
198 static int show_matching_key_value(uint8_t *buffer,
199                                    struct tffs_key_name_table *key_names)
200 {
201         int i;
202         uint16_t id;
203         struct tffs_entry tmp;
204         const char *name;
205
206         for (i = 0; i < key_names->size; i++) {
207                 name = key_names->entries[i].val;
208
209                 if (strncmp(name, name_filter, strlen(name)) == 0) {
210                         id = to_entry_header_id(*key_names->entries[i].id);
211
212                         if (find_entry(buffer, id, &tmp)) {
213                                 print_entry_value(&tmp);
214                                 printf("\n");
215                                 return EXIT_SUCCESS;
216                         } else {
217                                 fprintf(stderr,
218                                         "ERROR: no value found for name %s!\n",
219                                         name);
220                                 return EXIT_FAILURE;
221                         }
222                 }
223         }
224
225         fprintf(stderr, "ERROR: Unknown key name %s!\n", name_filter);
226         return EXIT_FAILURE;
227 }
228
229 static void usage(int status)
230 {
231         FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
232
233         fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
234         fprintf(stream,
235         "\n"
236         "Options:\n"
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"
244         );
245
246         exit(status);
247 }
248
249 static int file_exist(char *filename)
250 {
251         struct stat buffer;
252
253         return stat(filename, &buffer) == 0;
254 }
255
256 static void parse_options(int argc, char *argv[])
257 {
258         while (1)
259         {
260                 int c;
261
262                 c = getopt(argc, argv, "abhi:ln:s");
263                 if (c == -1)
264                         break;
265
266                 switch (c) {
267                         case 'a':
268                                 show_all = true;
269                                 name_filter = NULL;
270                                 print_all_key_names = false;
271                                 break;
272                         case 'b':
273                                 swap_bytes = 1;
274                                 break;
275                         case 'h':
276                                 usage(EXIT_SUCCESS);
277                                 break;
278                         case 'i':
279                                 input_file = optarg;
280                                 break;
281                         case 'l':
282                                 print_all_key_names = true;
283                                 show_all = false;
284                                 name_filter = NULL;
285                                 break;
286                         case 'n':
287                                 name_filter = optarg;
288                                 show_all = false;
289                                 print_all_key_names = false;
290                                 break;
291                         case 's':
292                                 tffs_size = strtoul(optarg, NULL, 0);
293                                 break;
294                         default:
295                                 usage(EXIT_FAILURE);
296                                 break;
297                 }
298         }
299
300         if (!input_file) {
301                 fprintf(stderr, "ERROR: No input file (-i <file>) given!\n");
302                 exit(EXIT_FAILURE);
303         }
304
305         if (!file_exist(input_file)) {
306                 fprintf(stderr, "ERROR: %s does not exist\n", input_file);
307                 exit(EXIT_FAILURE);
308         }
309
310         if (!show_all && !name_filter && !print_all_key_names) {
311                 fprintf(stderr,
312                         "ERROR: either -l, -a or -n <key name> is required!\n");
313                 exit(EXIT_FAILURE);
314         }
315 }
316
317 int main(int argc, char *argv[])
318 {
319         int ret = EXIT_FAILURE;
320         uint8_t *buffer;
321         FILE *fp;
322         struct tffs_entry name_table;
323         struct tffs_key_name_table key_names;
324
325         progname = basename(argv[0]);
326
327         parse_options(argc, argv);
328
329         fp = fopen(input_file, "r");
330
331         if (!fp) {
332                 fprintf(stderr, "ERROR: Failed to open tffs input file %s\n",
333                         input_file);
334                 goto out;
335         }
336
337         buffer = malloc(tffs_size);
338
339         if (fread(buffer, 1, tffs_size, fp) != tffs_size) {
340                 fprintf(stderr, "ERROR: Failed read tffs file %s\n",
341                         input_file);
342                 goto out_free;
343         }
344
345         if (!find_entry(buffer, TFFS_ID_TABLE_NAME, &name_table)) {
346                 fprintf(stderr,"ERROR: No name table found in tffs file %s\n",
347                         input_file);
348                 fprintf(stderr,"       Is byte-swapping (-b) required?\n");
349                 goto out_free;
350         }
351
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",
355                         input_file);
356                 goto out_free_names;
357         }
358
359         if (print_all_key_names) {
360                 show_all_key_names(&key_names);
361                 ret = EXIT_SUCCESS;
362         } else if (show_all) {
363                 ret = show_all_key_value_pairs(buffer, &key_names);
364         } else {
365                 ret = show_matching_key_value(buffer, &key_names);
366         }
367
368 out_free_names:
369         free(key_names.entries);
370 out_free:
371         fclose(fp);
372         free(buffer);
373 out:
374         return ret;
375 }