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