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