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