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