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