firmware-utils/mktplinkfw: add support for TL-WA701N/ND
[openwrt.git] / tools / firmware-utils / src / mktplinkfw.c
1 /*
2  * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
3  *
4  * This tool was based on:
5  *   TP-Link WR941 V2 firmware checksum fixing tool.
6  *   Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License version 2 as published
10  * by the Free Software Foundation.
11  *
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <string.h>
18 #include <unistd.h>     /* for unlink() */
19 #include <libgen.h>
20 #include <getopt.h>     /* for getopt() */
21 #include <stdarg.h>
22 #include <errno.h>
23 #include <sys/stat.h>
24
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
27
28 #include "md5.h"
29
30 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
31
32 #define HEADER_VERSION_V1       0x01000000
33 #define HWID_TL_MR3020_V1       0x30200001
34 #define HWID_TL_MR3220_V1       0x32200001
35 #define HWID_TL_MR3420_V1       0x34200001
36 #define HWID_TL_WA701N_V1       0x07010001
37 #define HWID_TL_WA901ND_V1      0x09010001
38 #define HWID_TL_WA901ND_V2      0x09010002
39 #define HWID_TL_WR703N_V1       0x07030101
40 #define HWID_TL_WR741ND_V1      0x07410001
41 #define HWID_TL_WR741ND_V4      0x07410004
42 #define HWID_TL_WR740N_V1       0x07400001
43 #define HWID_TL_WR740N_V3       0x07400003
44 #define HWID_TL_WR743ND_V1      0x07430001
45 #define HWID_TL_WR841N_V1_5     0x08410002
46 #define HWID_TL_WR841ND_V3      0x08410003
47 #define HWID_TL_WR841ND_V5      0x08410005
48 #define HWID_TL_WR841ND_V7      0x08410007
49 #define HWID_TL_WR941ND_V2      0x09410002
50 #define HWID_TL_WR941ND_V4      0x09410004
51 #define HWID_TL_WR1043ND_V1     0x10430001
52
53 #define MD5SUM_LEN      16
54
55 struct file_info {
56         char            *file_name;     /* name of the file */
57         uint32_t        file_size;      /* length of the file */
58 };
59
60 struct fw_header {
61         uint32_t        version;        /* header version */
62         char            vendor_name[24];
63         char            fw_version[36];
64         uint32_t        hw_id;          /* hardware id */
65         uint32_t        hw_rev;         /* hardware revision */
66         uint32_t        unk1;
67         uint8_t         md5sum1[MD5SUM_LEN];
68         uint32_t        unk2;
69         uint8_t         md5sum2[MD5SUM_LEN];
70         uint32_t        unk3;
71         uint32_t        kernel_la;      /* kernel load address */
72         uint32_t        kernel_ep;      /* kernel entry point */
73         uint32_t        fw_length;      /* total length of the firmware */
74         uint32_t        kernel_ofs;     /* kernel data offset */
75         uint32_t        kernel_len;     /* kernel data length */
76         uint32_t        rootfs_ofs;     /* rootfs data offset */
77         uint32_t        rootfs_len;     /* rootfs data length */
78         uint32_t        boot_ofs;       /* bootloader data offset */
79         uint32_t        boot_len;       /* bootloader data length */
80         uint8_t         pad[360];
81 } __attribute__ ((packed));
82
83 struct flash_layout {
84         char            *id;
85         uint32_t        fw_max_len;
86         uint32_t        kernel_la;
87         uint32_t        kernel_ep;
88         uint32_t        rootfs_ofs;
89 };
90
91 struct board_info {
92         char            *id;
93         uint32_t        hw_id;
94         uint32_t        hw_rev;
95         char            *layout_id;
96 };
97
98 /*
99  * Globals
100  */
101 static char *ofname;
102 static char *progname;
103 static char *vendor = "TP-LINK Technologies";
104 static char *version = "ver. 1.0";
105
106 static char *board_id;
107 static struct board_info *board;
108 static char *layout_id;
109 static struct flash_layout *layout;
110 static char *opt_hw_id;
111 static uint32_t hw_id;
112 static char *opt_hw_rev;
113 static uint32_t hw_rev;
114 static struct file_info kernel_info;
115 static uint32_t kernel_la = 0;
116 static uint32_t kernel_ep = 0;
117 static uint32_t kernel_len = 0;
118 static struct file_info rootfs_info;
119 static uint32_t rootfs_ofs = 0;
120 static uint32_t rootfs_align;
121 static struct file_info boot_info;
122 static int combined;
123 static int strip_padding;
124 static int add_jffs2_eof;
125 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
126
127 static struct file_info inspect_info;
128 static int extract = 0;
129
130 char md5salt_normal[MD5SUM_LEN] = {
131         0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
132         0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
133 };
134
135 char md5salt_boot[MD5SUM_LEN] = {
136         0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
137         0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
138 };
139
140 static struct flash_layout layouts[] = {
141         {
142                 .id             = "4M",
143                 .fw_max_len     = 0x3c0000,
144                 .kernel_la      = 0x80060000,
145                 .kernel_ep      = 0x80060000,
146                 .rootfs_ofs     = 0x140000,
147         }, {
148                 .id             = "4Mlzma",
149                 .fw_max_len     = 0x3c0000,
150                 .kernel_la      = 0x80060000,
151                 .kernel_ep      = 0x80060000,
152                 .rootfs_ofs     = 0x100000,
153         }, {
154                 .id             = "8M",
155                 .fw_max_len     = 0x7c0000,
156                 .kernel_la      = 0x80060000,
157                 .kernel_ep      = 0x80060000,
158                 .rootfs_ofs     = 0x140000,
159         }, {
160                 /* terminating entry */
161         }
162 };
163
164 static struct board_info boards[] = {
165         {
166                 .id             = "TL-MR3020v1",
167                 .hw_id          = HWID_TL_MR3020_V1,
168                 .hw_rev         = 1,
169                 .layout_id      = "4Mlzma",
170         }, {
171                 .id             = "TL-MR3220v1",
172                 .hw_id          = HWID_TL_MR3220_V1,
173                 .hw_rev         = 1,
174                 .layout_id      = "4M",
175         }, {
176                 .id             = "TL-MR3420v1",
177                 .hw_id          = HWID_TL_MR3420_V1,
178                 .hw_rev         = 1,
179                 .layout_id      = "4M",
180         }, {
181                 .id             = "TL-WA701Nv1",
182                 .hw_id          = HWID_TL_WA701N_V1,
183                 .hw_rev         = 1,
184                 .layout_id      = "4M",
185         }, {
186                 .id             = "TL-WA901NDv1",
187                 .hw_id          = HWID_TL_WA901ND_V1,
188                 .hw_rev         = 1,
189                 .layout_id      = "4M",
190         }, {
191                 .id             = "TL-WA901NDv2",
192                 .hw_id          = HWID_TL_WA901ND_V2,
193                 .hw_rev         = 1,
194                 .layout_id      = "4M",
195         }, {
196                 .id             = "TL-WR741NDv1",
197                 .hw_id          = HWID_TL_WR741ND_V1,
198                 .hw_rev         = 1,
199                 .layout_id      = "4M",
200         }, {
201                 .id             = "TL-WR741NDv4",
202                 .hw_id          = HWID_TL_WR741ND_V4,
203                 .hw_rev         = 1,
204                 .layout_id      = "4Mlzma",
205         }, {
206                 .id             = "TL-WR740Nv1",
207                 .hw_id          = HWID_TL_WR740N_V1,
208                 .hw_rev         = 1,
209                 .layout_id      = "4M",
210         }, {
211                 .id             = "TL-WR740Nv3",
212                 .hw_id          = HWID_TL_WR740N_V3,
213                 .hw_rev         = 1,
214                 .layout_id      = "4M",
215         }, {
216                 .id             = "TL-WR743NDv1",
217                 .hw_id          = HWID_TL_WR743ND_V1,
218                 .hw_rev         = 1,
219                 .layout_id      = "4M",
220         }, {
221                 .id             = "TL-WR841Nv1.5",
222                 .hw_id          = HWID_TL_WR841N_V1_5,
223                 .hw_rev         = 2,
224                 .layout_id      = "4M",
225         }, {
226                 .id             = "TL-WR841NDv3",
227                 .hw_id          = HWID_TL_WR841ND_V3,
228                 .hw_rev         = 3,
229                 .layout_id      = "4M",
230         }, {
231                 .id             = "TL-WR841NDv5",
232                 .hw_id          = HWID_TL_WR841ND_V5,
233                 .hw_rev         = 1,
234                 .layout_id      = "4M",
235         }, {
236                 .id             = "TL-WR841NDv7",
237                 .hw_id          = HWID_TL_WR841ND_V7,
238                 .hw_rev         = 1,
239                 .layout_id      = "4M",
240         }, {
241                 .id             = "TL-WR941NDv2",
242                 .hw_id          = HWID_TL_WR941ND_V2,
243                 .hw_rev         = 2,
244                 .layout_id      = "4M",
245         }, {
246                 .id             = "TL-WR941NDv4",
247                 .hw_id          = HWID_TL_WR941ND_V4,
248                 .hw_rev         = 1,
249                 .layout_id      = "4M",
250         }, {
251                 .id             = "TL-WR1043NDv1",
252                 .hw_id          = HWID_TL_WR1043ND_V1,
253                 .hw_rev         = 1,
254                 .layout_id      = "8M",
255         }, {
256                 .id             = "TL-WR703Nv1",
257                 .hw_id          = HWID_TL_WR703N_V1,
258                 .hw_rev         = 1,
259                 .layout_id      = "4Mlzma",
260         }, {
261                 /* terminating entry */
262         }
263 };
264
265 /*
266  * Message macros
267  */
268 #define ERR(fmt, ...) do { \
269         fflush(0); \
270         fprintf(stderr, "[%s] *** error: " fmt "\n", \
271                         progname, ## __VA_ARGS__ ); \
272 } while (0)
273
274 #define ERRS(fmt, ...) do { \
275         int save = errno; \
276         fflush(0); \
277         fprintf(stderr, "[%s] *** error: " fmt "\n", \
278                         progname, ## __VA_ARGS__, strerror(save)); \
279 } while (0)
280
281 #define DBG(fmt, ...) do { \
282         fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
283 } while (0)
284
285 static struct board_info *find_board(char *id)
286 {
287         struct board_info *ret;
288         struct board_info *board;
289
290         ret = NULL;
291         for (board = boards; board->id != NULL; board++){
292                 if (strcasecmp(id, board->id) == 0) {
293                         ret = board;
294                         break;
295                 }
296         };
297
298         return ret;
299 }
300
301 static struct board_info *find_board_by_hwid(uint32_t hw_id)
302 {
303         struct board_info *board;
304
305         for (board = boards; board->id != NULL; board++) {
306                 if (hw_id == board->hw_id)
307                         return board;
308         };
309
310         return NULL;
311 }
312
313 static struct flash_layout *find_layout(char *id)
314 {
315         struct flash_layout *ret;
316         struct flash_layout *l;
317
318         ret = NULL;
319         for (l = layouts; l->id != NULL; l++){
320                 if (strcasecmp(id, l->id) == 0) {
321                         ret = l;
322                         break;
323                 }
324         };
325
326         return ret;
327 }
328
329 static void usage(int status)
330 {
331         FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
332         struct board_info *board;
333
334         fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
335         fprintf(stream,
336 "\n"
337 "Options:\n"
338 "  -B <board>      create image for the board specified with <board>\n"
339 "  -c              use combined kernel image\n"
340 "  -E <ep>         overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
341 "  -L <la>         overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
342 "  -H <hwid>       use hardware id specified with <hwid>\n"
343 "  -F <id>         use flash layout specified with <id>\n"
344 "  -k <file>       read kernel image from the file <file>\n"
345 "  -r <file>       read rootfs image from the file <file>\n"
346 "  -a <align>      align the rootfs start on an <align> bytes boundary\n"
347 "  -R <offset>     overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
348 "  -o <file>       write output to the file <file>\n"
349 "  -s              strip padding from the end of the image\n"
350 "  -j              add jffs2 end-of-filesystem markers\n"
351 "  -N <vendor>     set image vendor to <vendor>\n"
352 "  -V <version>    set image version to <version>\n"
353 "  -i <file>       inspect given firmware file <file>\n"
354 "  -x              extract kernel and rootfs while inspecting (requires -i)\n"
355 "  -h              show this screen\n"
356         );
357
358         exit(status);
359 }
360
361 static int get_md5(char *data, int size, char *md5)
362 {
363         MD5_CTX ctx;
364
365         MD5_Init(&ctx);
366         MD5_Update(&ctx, data, size);
367         MD5_Final(md5, &ctx);
368 }
369
370 static int get_file_stat(struct file_info *fdata)
371 {
372         struct stat st;
373         int res;
374
375         if (fdata->file_name == NULL)
376                 return 0;
377
378         res = stat(fdata->file_name, &st);
379         if (res){
380                 ERRS("stat failed on %s", fdata->file_name);
381                 return res;
382         }
383
384         fdata->file_size = st.st_size;
385         return 0;
386 }
387
388 static int read_to_buf(struct file_info *fdata, char *buf)
389 {
390         FILE *f;
391         int ret = EXIT_FAILURE;
392
393         f = fopen(fdata->file_name, "r");
394         if (f == NULL) {
395                 ERRS("could not open \"%s\" for reading", fdata->file_name);
396                 goto out;
397         }
398
399         errno = 0;
400         fread(buf, fdata->file_size, 1, f);
401         if (errno != 0) {
402                 ERRS("unable to read from file \"%s\"", fdata->file_name);
403                 goto out_close;
404         }
405
406         ret = EXIT_SUCCESS;
407
408  out_close:
409         fclose(f);
410  out:
411         return ret;
412 }
413
414 static int check_options(void)
415 {
416         int ret;
417
418         if (inspect_info.file_name) {
419                 ret = get_file_stat(&inspect_info);
420                 if (ret)
421                         return ret;
422
423                 return 0;
424         } else if (extract) {
425                 ERR("no firmware for inspection specified");
426                 return -1;
427         }
428
429         if (board_id == NULL && opt_hw_id == NULL) {
430                 ERR("either board or hardware id must be specified");
431                 return -1;
432         }
433
434         if (board_id) {
435                 board = find_board(board_id);
436                 if (board == NULL) {
437                         ERR("unknown/unsupported board id \"%s\"", board_id);
438                         return -1;
439                 }
440                 if (layout_id == NULL)
441                         layout_id = board->layout_id;
442
443                 hw_id = board->hw_id;
444                 hw_rev = board->hw_rev;
445         } else {
446                 if (layout_id == NULL) {
447                         ERR("flash layout is not specified");
448                         return -1;
449                 }
450                 hw_id = strtoul(opt_hw_id, NULL, 0);
451
452                 if (opt_hw_rev)
453                         hw_rev = strtoul(opt_hw_rev, NULL, 0);
454                 else
455                         hw_rev = 1;
456         }
457
458         layout = find_layout(layout_id);
459         if (layout == NULL) {
460                 ERR("unknown flash layout \"%s\"", layout_id);
461                 return -1;
462         }
463
464         if (!kernel_la)
465                 kernel_la = layout->kernel_la;
466         if (!kernel_ep)
467                 kernel_ep = layout->kernel_ep;
468         if (!rootfs_ofs)
469                 rootfs_ofs = layout->rootfs_ofs;
470
471         if (kernel_info.file_name == NULL) {
472                 ERR("no kernel image specified");
473                 return -1;
474         }
475
476         ret = get_file_stat(&kernel_info);
477         if (ret)
478                 return ret;
479
480         kernel_len = kernel_info.file_size;
481
482         if (combined) {
483                 if (kernel_info.file_size >
484                     layout->fw_max_len - sizeof(struct fw_header)) {
485                         ERR("kernel image is too big");
486                         return -1;
487                 }
488         } else {
489                 if (rootfs_info.file_name == NULL) {
490                         ERR("no rootfs image specified");
491                         return -1;
492                 }
493
494                 ret = get_file_stat(&rootfs_info);
495                 if (ret)
496                         return ret;
497
498                 if (rootfs_align) {
499                         kernel_len += sizeof(struct fw_header);
500                         kernel_len = ALIGN(kernel_len, rootfs_align);
501                         kernel_len -= sizeof(struct fw_header);
502
503                         DBG("kernel length aligned to %u", kernel_len);
504
505                         if (kernel_len + rootfs_info.file_size >
506                             layout->fw_max_len - sizeof(struct fw_header)) {
507                                 ERR("images are too big");
508                                 return -1;
509                         }
510                 } else {
511                         if (kernel_info.file_size >
512                             rootfs_ofs - sizeof(struct fw_header)) {
513                                 ERR("kernel image is too big");
514                                 return -1;
515                         }
516
517                         if (rootfs_info.file_size >
518                             (layout->fw_max_len - rootfs_ofs)) {
519                                 ERR("rootfs image is too big");
520                                 return -1;
521                         }
522                 }
523         }
524
525         if (ofname == NULL) {
526                 ERR("no output file specified");
527                 return -1;
528         }
529
530         return 0;
531 }
532
533 static void fill_header(char *buf, int len)
534 {
535         struct fw_header *hdr = (struct fw_header *)buf;
536
537         memset(hdr, 0, sizeof(struct fw_header));
538
539         hdr->version = htonl(HEADER_VERSION_V1);
540         strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
541         strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
542         hdr->hw_id = htonl(hw_id);
543         hdr->hw_rev = htonl(hw_rev);
544
545         if (boot_info.file_size == 0)
546                 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
547         else
548                 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
549
550         hdr->kernel_la = htonl(kernel_la);
551         hdr->kernel_ep = htonl(kernel_ep);
552         hdr->fw_length = htonl(layout->fw_max_len);
553         hdr->kernel_ofs = htonl(sizeof(struct fw_header));
554         hdr->kernel_len = htonl(kernel_len);
555         if (!combined) {
556                 hdr->rootfs_ofs = htonl(rootfs_ofs);
557                 hdr->rootfs_len = htonl(rootfs_info.file_size);
558         }
559
560         get_md5(buf, len, hdr->md5sum1);
561 }
562
563 static int pad_jffs2(char *buf, int currlen)
564 {
565         int len;
566         uint32_t pad_mask;
567
568         len = currlen;
569         pad_mask = (64 * 1024);
570         while ((len < layout->fw_max_len) && (pad_mask != 0)) {
571                 uint32_t mask;
572                 int i;
573
574                 for (i = 10; i < 32; i++) {
575                         mask = 1 << i;
576                         if (pad_mask & mask)
577                                 break;
578                 }
579
580                 len = ALIGN(len, mask);
581
582                 for (i = 10; i < 32; i++) {
583                         mask = 1 << i;
584                         if ((len & (mask - 1)) == 0)
585                                 pad_mask &= ~mask;
586                 }
587
588                 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
589                         buf[len + i] = jffs2_eof_mark[i];
590
591                 len += sizeof(jffs2_eof_mark);
592         }
593
594         return len;
595 }
596
597 static int write_fw(char *data, int len)
598 {
599         FILE *f;
600         int ret = EXIT_FAILURE;
601
602         f = fopen(ofname, "w");
603         if (f == NULL) {
604                 ERRS("could not open \"%s\" for writing", ofname);
605                 goto out;
606         }
607
608         errno = 0;
609         fwrite(data, len, 1, f);
610         if (errno) {
611                 ERRS("unable to write output file");
612                 goto out_flush;
613         }
614
615         DBG("firmware file \"%s\" completed", ofname);
616
617         ret = EXIT_SUCCESS;
618
619  out_flush:
620         fflush(f);
621         fclose(f);
622         if (ret != EXIT_SUCCESS) {
623                 unlink(ofname);
624         }
625  out:
626         return ret;
627 }
628
629 static int build_fw(void)
630 {
631         int buflen;
632         char *buf;
633         char *p;
634         int ret = EXIT_FAILURE;
635         int writelen = 0;
636
637         buflen = layout->fw_max_len;
638
639         buf = malloc(buflen);
640         if (!buf) {
641                 ERR("no memory for buffer\n");
642                 goto out;
643         }
644
645         memset(buf, 0xff, buflen);
646         p = buf + sizeof(struct fw_header);
647         ret = read_to_buf(&kernel_info, p);
648         if (ret)
649                 goto out_free_buf;
650
651         writelen = sizeof(struct fw_header) + kernel_len;
652
653         if (!combined) {
654                 if (rootfs_align)
655                         p = buf + writelen;
656                 else
657                         p = buf + rootfs_ofs;
658
659                 ret = read_to_buf(&rootfs_info, p);
660                 if (ret)
661                         goto out_free_buf;
662
663                 if (rootfs_align)
664                         writelen += rootfs_info.file_size;
665                 else
666                         writelen = rootfs_ofs + rootfs_info.file_size;
667
668                 if (add_jffs2_eof)
669                         writelen = pad_jffs2(buf, writelen);
670         }
671
672         if (!strip_padding)
673                 writelen = buflen;
674
675         fill_header(buf, writelen);
676         ret = write_fw(buf, writelen);
677         if (ret)
678                 goto out_free_buf;
679
680         ret = EXIT_SUCCESS;
681
682  out_free_buf:
683         free(buf);
684  out:
685         return ret;
686 }
687
688 /* Helper functions to inspect_fw() representing different output formats */
689 static inline void inspect_fw_pstr(char *label, char *str)
690 {
691         printf("%-23s: %s\n", label, str);
692 }
693
694 static inline void inspect_fw_phex(char *label, uint32_t val)
695 {
696         printf("%-23s: 0x%08x\n", label, val);
697 }
698
699 static inline void inspect_fw_phexpost(char *label,
700                                        uint32_t val, char *post)
701 {
702         printf("%-23s: 0x%08x (%s)\n", label, val, post);
703 }
704
705 static inline void inspect_fw_phexdef(char *label,
706                                       uint32_t val, uint32_t defval)
707 {
708         printf("%-23s: 0x%08x                  ", label, val);
709
710         if (val == defval)
711                 printf("(== OpenWrt default)\n");
712         else
713                 printf("(OpenWrt default: 0x%08x)\n", defval);
714 }
715
716 static inline void inspect_fw_phexexp(char *label,
717                                       uint32_t val, uint32_t expval)
718 {
719         printf("%-23s: 0x%08x ", label, val);
720
721         if (val == expval)
722                 printf("(ok)\n");
723         else
724                 printf("(expected: 0x%08x)\n", expval);
725 }
726
727 static inline void inspect_fw_phexdec(char *label, uint32_t val)
728 {
729         printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
730 }
731
732 static inline void inspect_fw_phexdecdef(char *label,
733                                          uint32_t val, uint32_t defval)
734 {
735         printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
736
737         if (val == defval)
738                 printf("(== OpenWrt default)\n");
739         else
740                 printf("(OpenWrt default: 0x%08x)\n", defval);
741 }
742
743 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
744 {
745         int i;
746
747         printf("%-23s:", label);
748         for (i=0; i<MD5SUM_LEN; i++)
749                 printf(" %02x", val[i]);
750         printf(" %s\n", text);
751 }
752
753 static int inspect_fw(void)
754 {
755         char *buf;
756         struct fw_header *hdr;
757         uint8_t md5sum[MD5SUM_LEN];
758         struct board_info *board;
759         int ret = EXIT_FAILURE;
760
761         buf = malloc(inspect_info.file_size);
762         if (!buf) {
763                 ERR("no memory for buffer!\n");
764                 goto out;
765         }
766
767         ret = read_to_buf(&inspect_info, buf);
768         if (ret)
769                 goto out_free_buf;
770         hdr = (struct fw_header *)buf;
771
772         inspect_fw_pstr("File name", inspect_info.file_name);
773         inspect_fw_phexdec("File size", inspect_info.file_size);
774
775         if (ntohl(hdr->version) != HEADER_VERSION_V1) {
776                 ERR("file does not seem to have V1 header!\n");
777                 goto out_free_buf;
778         }
779
780         inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
781
782         if (ntohl(hdr->unk1) != 0)
783                 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
784
785         memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
786         if (ntohl(hdr->boot_len) == 0)
787                 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
788         else
789                 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
790         get_md5(buf, inspect_info.file_size, hdr->md5sum1);
791
792         if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
793                 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
794                 inspect_fw_pmd5sum("          --> expected", hdr->md5sum1, "");
795         } else {
796                 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
797         }
798         if (ntohl(hdr->unk2) != 0)
799                 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
800         inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
801                            "(purpose yet unknown, unchecked here)");
802         if (ntohl(hdr->unk3) != 0)
803                 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
804
805         printf("\n");
806
807         inspect_fw_pstr("Vendor name", hdr->vendor_name);
808         inspect_fw_pstr("Firmware version", hdr->fw_version);
809         board = find_board_by_hwid(ntohl(hdr->hw_id));
810         if (board) {
811                 layout = find_layout(board->layout_id);
812                 inspect_fw_phexpost("Hardware ID",
813                                     ntohl(hdr->hw_id), board->id);
814                 inspect_fw_phexexp("Hardware Revision",
815                                    ntohl(hdr->hw_rev), board->hw_rev);
816         } else {
817                 inspect_fw_phexpost("Hardware ID",
818                                     ntohl(hdr->hw_id), "unknown");
819                 inspect_fw_phex("Hardware Revision",
820                                 ntohl(hdr->hw_rev));
821         }
822
823         printf("\n");
824
825         inspect_fw_phexdec("Kernel data offset",
826                            ntohl(hdr->kernel_ofs));
827         inspect_fw_phexdec("Kernel data length",
828                            ntohl(hdr->kernel_len));
829         if (board) {
830                 inspect_fw_phexdef("Kernel load address",
831                                    ntohl(hdr->kernel_la),
832                                    layout ? layout->kernel_la : 0xffffffff);
833                 inspect_fw_phexdef("Kernel entry point",
834                                    ntohl(hdr->kernel_ep),
835                                    layout ? layout->kernel_ep : 0xffffffff);
836                 inspect_fw_phexdecdef("Rootfs data offset",
837                                       ntohl(hdr->rootfs_ofs),
838                                       layout ? layout->rootfs_ofs : 0xffffffff);
839         } else {
840                 inspect_fw_phex("Kernel load address",
841                                 ntohl(hdr->kernel_la));
842                 inspect_fw_phex("Kernel entry point",
843                                 ntohl(hdr->kernel_ep));
844                 inspect_fw_phexdec("Rootfs data offset",
845                                    ntohl(hdr->rootfs_ofs));
846         }
847         inspect_fw_phexdec("Rootfs data length",
848                            ntohl(hdr->rootfs_len));
849         inspect_fw_phexdec("Boot loader data offset",
850                            ntohl(hdr->boot_ofs));
851         inspect_fw_phexdec("Boot loader data length",
852                            ntohl(hdr->boot_len));
853         inspect_fw_phexdec("Total firmware length",
854                            ntohl(hdr->fw_length));
855
856         if (extract) {
857                 FILE *fp;
858                 char *filename;
859
860                 printf("\n");
861
862                 filename = malloc(strlen(inspect_info.file_name) + 8);
863                 sprintf(filename, "%s-kernel", inspect_info.file_name);
864                 printf("Extracting kernel to \"%s\"...\n", filename);
865                 fp = fopen(filename, "w");
866                 if (fp) {
867                         if (!fwrite(buf + ntohl(hdr->kernel_ofs),
868                                     ntohl(hdr->kernel_len), 1, fp)) {
869                                 ERR("error in fwrite(): %s", strerror(errno));
870                         }
871                         fclose(fp);
872                 } else {
873                         ERR("error in fopen(): %s", strerror(errno));
874                 }
875                 free(filename);
876
877                 filename = malloc(strlen(inspect_info.file_name) + 8);
878                 sprintf(filename, "%s-rootfs", inspect_info.file_name);
879                 printf("Extracting rootfs to \"%s\"...\n", filename);
880                 fp = fopen(filename, "w");
881                 if (fp) {
882                         if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
883                                     ntohl(hdr->rootfs_len), 1, fp)) {
884                                 ERR("error in fwrite(): %s", strerror(errno));
885                         }
886                         fclose(fp);
887                 } else {
888                         ERR("error in fopen(): %s", strerror(errno));
889                 }
890                 free(filename);
891         }
892
893  out_free_buf:
894         free(buf);
895  out:
896         return ret;
897 }
898
899 int main(int argc, char *argv[])
900 {
901         int ret = EXIT_FAILURE;
902         int err;
903
904         FILE *outfile;
905
906         progname = basename(argv[0]);
907
908         while ( 1 ) {
909                 int c;
910
911                 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsj");
912                 if (c == -1)
913                         break;
914
915                 switch (c) {
916                 case 'a':
917                         sscanf(optarg, "0x%x", &rootfs_align);
918                         break;
919                 case 'B':
920                         board_id = optarg;
921                         break;
922                 case 'H':
923                         opt_hw_id = optarg;
924                         break;
925                 case 'E':
926                         sscanf(optarg, "0x%x", &kernel_ep);
927                         break;
928                 case 'F':
929                         layout_id = optarg;
930                         break;
931                 case 'W':
932                         opt_hw_rev = optarg;
933                         break;
934                 case 'L':
935                         sscanf(optarg, "0x%x", &kernel_la);
936                         break;
937                 case 'V':
938                         version = optarg;
939                         break;
940                 case 'N':
941                         vendor = optarg;
942                         break;
943                 case 'c':
944                         combined++;
945                         break;
946                 case 'k':
947                         kernel_info.file_name = optarg;
948                         break;
949                 case 'r':
950                         rootfs_info.file_name = optarg;
951                         break;
952                 case 'R':
953                         sscanf(optarg, "0x%x", &rootfs_ofs);
954                         break;
955                 case 'o':
956                         ofname = optarg;
957                         break;
958                 case 's':
959                         strip_padding = 1;
960                         break;
961                 case 'i':
962                         inspect_info.file_name = optarg;
963                         break;
964                 case 'j':
965                         add_jffs2_eof = 1;
966                         break;
967                 case 'x':
968                         extract = 1;
969                         break;
970                 case 'h':
971                         usage(EXIT_SUCCESS);
972                         break;
973                 default:
974                         usage(EXIT_FAILURE);
975                         break;
976                 }
977         }
978
979         ret = check_options();
980         if (ret)
981                 goto out;
982
983         if (!inspect_info.file_name)
984                 ret = build_fw();
985         else
986                 ret = inspect_fw();
987
988  out:
989         return ret;
990 }
991