2 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
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>
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.
18 #include <unistd.h> /* for unlink() */
20 #include <getopt.h> /* for getopt() */
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
30 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
32 #define HEADER_VERSION_V1 0x01000000
33 #define HWID_ANTMINER_S1 0x04440101
34 #define HWID_ANTMINER_S3 0x04440301
35 #define HWID_GL_INET_V1 0x08000001
36 #define HWID_GS_OOLITE_V1 0x3C000101
37 #define HWID_ONION_OMEGA 0x04700001
38 #define HWID_TL_MR10U_V1 0x00100101
39 #define HWID_TL_MR13U_V1 0x00130101
40 #define HWID_TL_MR3020_V1 0x30200001
41 #define HWID_TL_MR3220_V1 0x32200001
42 #define HWID_TL_MR3220_V2 0x32200002
43 #define HWID_TL_MR3420_V1 0x34200001
44 #define HWID_TL_MR3420_V2 0x34200002
45 #define HWID_TL_WA701N_V1 0x07010001
46 #define HWID_TL_WA701N_V2 0x07010002
47 #define HWID_TL_WA7210N_V2 0x72100002
48 #define HWID_TL_WA7510N_V1 0x75100001
49 #define HWID_TL_WA801ND_V1 0x08010001
50 #define HWID_TL_WA830RE_V1 0x08300010
51 #define HWID_TL_WA830RE_V2 0x08300002
52 #define HWID_TL_WA801ND_V2 0x08010002
53 #define HWID_TL_WA901ND_V1 0x09010001
54 #define HWID_TL_WA901ND_V2 0x09010002
55 #define HWID_TL_WA901ND_V4 0x09010004
56 #define HWID_TL_WDR4300_V1_IL 0x43008001
57 #define HWID_TL_WDR4900_V1 0x49000001
58 #define HWID_TL_WR703N_V1 0x07030101
59 #define HWID_TL_WR720N_V3 0x07200103
60 #define HWID_TL_WR720N_V4 0x07200104
61 #define HWID_TL_WR741ND_V1 0x07410001
62 #define HWID_TL_WR741ND_V4 0x07410004
63 #define HWID_TL_WR740N_V1 0x07400001
64 #define HWID_TL_WR740N_V3 0x07400003
65 #define HWID_TL_WR743ND_V1 0x07430001
66 #define HWID_TL_WR743ND_V2 0x07430002
67 #define HWID_TL_WR841N_V1_5 0x08410002
68 #define HWID_TL_WR841ND_V3 0x08410003
69 #define HWID_TL_WR841ND_V5 0x08410005
70 #define HWID_TL_WR841ND_V7 0x08410007
71 #define HWID_TL_WR941ND_V2 0x09410002
72 #define HWID_TL_WR941ND_V4 0x09410004
73 #define HWID_TL_WR1043ND_V1 0x10430001
74 #define HWID_TL_WR1043ND_V2 0x10430002
75 #define HWID_TL_WR1041N_V2 0x10410002
76 #define HWID_TL_WR2543N_V1 0x25430001
81 char *file_name; /* name of the file */
82 uint32_t file_size; /* length of the file */
86 uint32_t version; /* header version */
89 uint32_t hw_id; /* hardware id */
90 uint32_t hw_rev; /* hardware revision */
92 uint8_t md5sum1[MD5SUM_LEN];
94 uint8_t md5sum2[MD5SUM_LEN];
96 uint32_t kernel_la; /* kernel load address */
97 uint32_t kernel_ep; /* kernel entry point */
98 uint32_t fw_length; /* total length of the firmware */
99 uint32_t kernel_ofs; /* kernel data offset */
100 uint32_t kernel_len; /* kernel data length */
101 uint32_t rootfs_ofs; /* rootfs data offset */
102 uint32_t rootfs_len; /* rootfs data length */
103 uint32_t boot_ofs; /* bootloader data offset */
104 uint32_t boot_len; /* bootloader data length */
109 } __attribute__ ((packed));
111 struct flash_layout {
130 static char *progname;
131 static char *vendor = "TP-LINK Technologies";
132 static char *version = "ver. 1.0";
133 static char *fw_ver = "0.0.0";
135 static char *board_id;
136 static struct board_info *board;
137 static char *layout_id;
138 static struct flash_layout *layout;
139 static char *opt_hw_id;
140 static uint32_t hw_id;
141 static char *opt_hw_rev;
142 static uint32_t hw_rev;
143 static int fw_ver_lo;
144 static int fw_ver_mid;
145 static int fw_ver_hi;
146 static struct file_info kernel_info;
147 static uint32_t kernel_la = 0;
148 static uint32_t kernel_ep = 0;
149 static uint32_t kernel_len = 0;
150 static struct file_info rootfs_info;
151 static uint32_t rootfs_ofs = 0;
152 static uint32_t rootfs_align;
153 static struct file_info boot_info;
155 static int strip_padding;
156 static int ignore_size;
157 static int add_jffs2_eof;
158 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
159 static uint32_t fw_max_len;
160 static uint32_t reserved_space;
162 static struct file_info inspect_info;
163 static int extract = 0;
165 char md5salt_normal[MD5SUM_LEN] = {
166 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
167 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
170 char md5salt_boot[MD5SUM_LEN] = {
171 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
172 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
175 static struct flash_layout layouts[] = {
178 .fw_max_len = 0x3c0000,
179 .kernel_la = 0x80060000,
180 .kernel_ep = 0x80060000,
181 .rootfs_ofs = 0x140000,
184 .fw_max_len = 0x3c0000,
185 .kernel_la = 0x80060000,
186 .kernel_ep = 0x80060000,
187 .rootfs_ofs = 0x100000,
190 .fw_max_len = 0x7c0000,
191 .kernel_la = 0x80060000,
192 .kernel_ep = 0x80060000,
193 .rootfs_ofs = 0x140000,
196 .fw_max_len = 0x7c0000,
197 .kernel_la = 0x80060000,
198 .kernel_ep = 0x80060000,
199 .rootfs_ofs = 0x100000,
202 .fw_max_len = 0xf80000,
203 .kernel_la = 0x80060000,
204 .kernel_ep = 0x80060000,
205 .rootfs_ofs = 0x140000,
208 .fw_max_len = 0xf80000,
209 .kernel_la = 0x80060000,
210 .kernel_ep = 0x80060000,
211 .rootfs_ofs = 0x100000,
214 .fw_max_len = 0xf80000,
215 .kernel_la = 0x00000000,
216 .kernel_ep = 0xc0000000,
217 .rootfs_ofs = 0x2a0000,
219 /* terminating entry */
223 static struct board_info boards[] = {
226 .hw_id = HWID_TL_MR10U_V1,
228 .layout_id = "4Mlzma",
231 .hw_id = HWID_TL_MR13U_V1,
233 .layout_id = "4Mlzma",
236 .hw_id = HWID_TL_MR3020_V1,
238 .layout_id = "4Mlzma",
241 .hw_id = HWID_TL_MR3220_V1,
246 .hw_id = HWID_TL_MR3220_V2,
248 .layout_id = "4Mlzma",
251 .hw_id = HWID_TL_MR3420_V1,
256 .hw_id = HWID_TL_MR3420_V2,
258 .layout_id = "4Mlzma",
261 .hw_id = HWID_TL_WA701N_V1,
266 .hw_id = HWID_TL_WA701N_V2,
268 .layout_id = "4Mlzma",
271 .hw_id = HWID_TL_WA7210N_V2,
273 .layout_id = "4Mlzma",
276 .hw_id = HWID_TL_WA7510N_V1,
280 .id = "TL-WA801NDv1",
281 .hw_id = HWID_TL_WA801ND_V1,
285 .id = "TL-WA830REv1",
286 .hw_id = HWID_TL_WA830RE_V1,
290 .id = "TL-WA830REv2",
291 .hw_id = HWID_TL_WA830RE_V2,
295 .id = "TL-WA801NDv2",
296 .hw_id = HWID_TL_WA801ND_V2,
298 .layout_id = "4Mlzma",
300 .id = "TL-WA901NDv1",
301 .hw_id = HWID_TL_WA901ND_V1,
305 .id = "TL-WA901NDv2",
306 .hw_id = HWID_TL_WA901ND_V2,
310 .id = "TL-WA901NDv4",
311 .hw_id = HWID_TL_WA901ND_V4,
313 .layout_id = "4Mlzma",
315 .id = "TL-WDR4300v1",
316 .hw_id = HWID_TL_WDR4300_V1_IL,
318 .layout_id = "8Mlzma",
320 .id = "TL-WDR4900v1",
321 .hw_id = HWID_TL_WDR4900_V1,
323 .layout_id = "16Mppc",
325 .id = "TL-WR741NDv1",
326 .hw_id = HWID_TL_WR741ND_V1,
330 .id = "TL-WR741NDv4",
331 .hw_id = HWID_TL_WR741ND_V4,
333 .layout_id = "4Mlzma",
336 .hw_id = HWID_TL_WR740N_V1,
341 .hw_id = HWID_TL_WR740N_V3,
345 .id = "TL-WR743NDv1",
346 .hw_id = HWID_TL_WR743ND_V1,
350 .id = "TL-WR743NDv2",
351 .hw_id = HWID_TL_WR743ND_V2,
353 .layout_id = "4Mlzma",
355 .id = "TL-WR841Nv1.5",
356 .hw_id = HWID_TL_WR841N_V1_5,
360 .id = "TL-WR841NDv3",
361 .hw_id = HWID_TL_WR841ND_V3,
365 .id = "TL-WR841NDv5",
366 .hw_id = HWID_TL_WR841ND_V5,
370 .id = "TL-WR841NDv7",
371 .hw_id = HWID_TL_WR841ND_V7,
375 .id = "TL-WR941NDv2",
376 .hw_id = HWID_TL_WR941ND_V2,
380 .id = "TL-WR941NDv4",
381 .hw_id = HWID_TL_WR941ND_V4,
385 .id = "TL-WR1041Nv2",
386 .hw_id = HWID_TL_WR1041N_V2,
388 .layout_id = "4Mlzma",
390 .id = "TL-WR1043NDv1",
391 .hw_id = HWID_TL_WR1043ND_V1,
395 .id = "TL-WR1043NDv2",
396 .hw_id = HWID_TL_WR1043ND_V2,
398 .layout_id = "8Mlzma",
400 .id = "TL-WR2543Nv1",
401 .hw_id = HWID_TL_WR2543N_V1,
403 .layout_id = "8Mlzma",
406 .hw_id = HWID_TL_WR703N_V1,
408 .layout_id = "4Mlzma",
411 .hw_id = HWID_TL_WR720N_V3,
413 .layout_id = "4Mlzma",
416 .hw_id = HWID_TL_WR720N_V4,
418 .layout_id = "4Mlzma",
421 .hw_id = HWID_GL_INET_V1,
423 .layout_id = "8Mlzma",
426 .hw_id = HWID_GS_OOLITE_V1,
428 .layout_id = "16Mlzma",
431 .hw_id = HWID_ONION_OMEGA,
433 .layout_id = "16Mlzma",
436 .hw_id = HWID_ANTMINER_S1,
438 .layout_id = "8Mlzma",
441 .hw_id = HWID_ANTMINER_S3,
443 .layout_id = "8Mlzma",
445 /* terminating entry */
452 #define ERR(fmt, ...) do { \
454 fprintf(stderr, "[%s] *** error: " fmt "\n", \
455 progname, ## __VA_ARGS__ ); \
458 #define ERRS(fmt, ...) do { \
461 fprintf(stderr, "[%s] *** error: " fmt "\n", \
462 progname, ## __VA_ARGS__, strerror(save)); \
465 #define DBG(fmt, ...) do { \
466 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
469 static struct board_info *find_board(char *id)
471 struct board_info *ret;
472 struct board_info *board;
475 for (board = boards; board->id != NULL; board++){
476 if (strcasecmp(id, board->id) == 0) {
485 static struct board_info *find_board_by_hwid(uint32_t hw_id)
487 struct board_info *board;
489 for (board = boards; board->id != NULL; board++) {
490 if (hw_id == board->hw_id)
497 static struct flash_layout *find_layout(char *id)
499 struct flash_layout *ret;
500 struct flash_layout *l;
503 for (l = layouts; l->id != NULL; l++){
504 if (strcasecmp(id, l->id) == 0) {
513 static void usage(int status)
515 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
516 struct board_info *board;
518 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
522 " -B <board> create image for the board specified with <board>\n"
523 " -c use combined kernel image\n"
524 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
525 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
526 " -H <hwid> use hardware id specified with <hwid>\n"
527 " -W <hwrev> use hardware revision specified with <hwrev>\n"
528 " -F <id> use flash layout specified with <id>\n"
529 " -k <file> read kernel image from the file <file>\n"
530 " -r <file> read rootfs image from the file <file>\n"
531 " -a <align> align the rootfs start on an <align> bytes boundary\n"
532 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
533 " -o <file> write output to the file <file>\n"
534 " -s strip padding from the end of the image\n"
535 " -S ignore firmware size limit (only for combined images)\n"
536 " -j add jffs2 end-of-filesystem markers\n"
537 " -N <vendor> set image vendor to <vendor>\n"
538 " -V <version> set image version to <version>\n"
539 " -v <version> set firmware version to <version>\n"
540 " -i <file> inspect given firmware file <file>\n"
541 " -x extract kernel and rootfs while inspecting (requires -i)\n"
542 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
543 " -h show this screen\n"
549 static int get_md5(char *data, int size, char *md5)
554 MD5_Update(&ctx, data, size);
555 MD5_Final(md5, &ctx);
558 static int get_file_stat(struct file_info *fdata)
563 if (fdata->file_name == NULL)
566 res = stat(fdata->file_name, &st);
568 ERRS("stat failed on %s", fdata->file_name);
572 fdata->file_size = st.st_size;
576 static int read_to_buf(struct file_info *fdata, char *buf)
579 int ret = EXIT_FAILURE;
581 f = fopen(fdata->file_name, "r");
583 ERRS("could not open \"%s\" for reading", fdata->file_name);
588 fread(buf, fdata->file_size, 1, f);
590 ERRS("unable to read from file \"%s\"", fdata->file_name);
602 static int check_options(void)
606 if (inspect_info.file_name) {
607 ret = get_file_stat(&inspect_info);
612 } else if (extract) {
613 ERR("no firmware for inspection specified");
617 if (board_id == NULL && opt_hw_id == NULL) {
618 ERR("either board or hardware id must be specified");
623 board = find_board(board_id);
625 ERR("unknown/unsupported board id \"%s\"", board_id);
628 if (layout_id == NULL)
629 layout_id = board->layout_id;
631 hw_id = board->hw_id;
632 hw_rev = board->hw_rev;
634 if (layout_id == NULL) {
635 ERR("flash layout is not specified");
638 hw_id = strtoul(opt_hw_id, NULL, 0);
641 hw_rev = strtoul(opt_hw_rev, NULL, 0);
646 layout = find_layout(layout_id);
647 if (layout == NULL) {
648 ERR("unknown flash layout \"%s\"", layout_id);
653 kernel_la = layout->kernel_la;
655 kernel_ep = layout->kernel_ep;
657 rootfs_ofs = layout->rootfs_ofs;
659 if (reserved_space > layout->fw_max_len) {
660 ERR("reserved space is not valid");
664 fw_max_len = layout->fw_max_len - reserved_space;
666 if (kernel_info.file_name == NULL) {
667 ERR("no kernel image specified");
671 ret = get_file_stat(&kernel_info);
675 kernel_len = kernel_info.file_size;
678 if (kernel_info.file_size >
679 fw_max_len - sizeof(struct fw_header)) {
681 ERR("kernel image is too big");
684 layout->fw_max_len = sizeof(struct fw_header) +
685 kernel_info.file_size +
689 if (rootfs_info.file_name == NULL) {
690 ERR("no rootfs image specified");
694 ret = get_file_stat(&rootfs_info);
699 kernel_len += sizeof(struct fw_header);
700 kernel_len = ALIGN(kernel_len, rootfs_align);
701 kernel_len -= sizeof(struct fw_header);
703 DBG("kernel length aligned to %u", kernel_len);
705 if (kernel_len + rootfs_info.file_size >
706 fw_max_len - sizeof(struct fw_header)) {
707 ERR("images are too big");
711 if (kernel_info.file_size >
712 rootfs_ofs - sizeof(struct fw_header)) {
713 ERR("kernel image is too big");
717 if (rootfs_info.file_size >
718 (fw_max_len - rootfs_ofs)) {
719 ERR("rootfs image is too big");
725 if (ofname == NULL) {
726 ERR("no output file specified");
730 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
732 ERR("invalid firmware version '%s'", fw_ver);
739 static void fill_header(char *buf, int len)
741 struct fw_header *hdr = (struct fw_header *)buf;
743 memset(hdr, 0, sizeof(struct fw_header));
745 hdr->version = htonl(HEADER_VERSION_V1);
746 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
747 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
748 hdr->hw_id = htonl(hw_id);
749 hdr->hw_rev = htonl(hw_rev);
751 if (boot_info.file_size == 0)
752 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
754 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
756 hdr->kernel_la = htonl(kernel_la);
757 hdr->kernel_ep = htonl(kernel_ep);
758 hdr->fw_length = htonl(layout->fw_max_len);
759 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
760 hdr->kernel_len = htonl(kernel_len);
762 hdr->rootfs_ofs = htonl(rootfs_ofs);
763 hdr->rootfs_len = htonl(rootfs_info.file_size);
766 hdr->ver_hi = htons(fw_ver_hi);
767 hdr->ver_mid = htons(fw_ver_mid);
768 hdr->ver_lo = htons(fw_ver_lo);
770 get_md5(buf, len, hdr->md5sum1);
773 static int pad_jffs2(char *buf, int currlen)
779 pad_mask = (64 * 1024);
780 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
784 for (i = 10; i < 32; i++) {
790 len = ALIGN(len, mask);
792 for (i = 10; i < 32; i++) {
794 if ((len & (mask - 1)) == 0)
798 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
799 buf[len + i] = jffs2_eof_mark[i];
801 len += sizeof(jffs2_eof_mark);
807 static int write_fw(char *data, int len)
810 int ret = EXIT_FAILURE;
812 f = fopen(ofname, "w");
814 ERRS("could not open \"%s\" for writing", ofname);
819 fwrite(data, len, 1, f);
821 ERRS("unable to write output file");
825 DBG("firmware file \"%s\" completed", ofname);
832 if (ret != EXIT_SUCCESS) {
839 static int build_fw(void)
844 int ret = EXIT_FAILURE;
847 buflen = layout->fw_max_len;
849 buf = malloc(buflen);
851 ERR("no memory for buffer\n");
855 memset(buf, 0xff, buflen);
856 p = buf + sizeof(struct fw_header);
857 ret = read_to_buf(&kernel_info, p);
861 writelen = sizeof(struct fw_header) + kernel_len;
867 p = buf + rootfs_ofs;
869 ret = read_to_buf(&rootfs_info, p);
874 writelen += rootfs_info.file_size;
876 writelen = rootfs_ofs + rootfs_info.file_size;
879 writelen = pad_jffs2(buf, writelen);
885 fill_header(buf, writelen);
886 ret = write_fw(buf, writelen);
898 /* Helper functions to inspect_fw() representing different output formats */
899 static inline void inspect_fw_pstr(char *label, char *str)
901 printf("%-23s: %s\n", label, str);
904 static inline void inspect_fw_phex(char *label, uint32_t val)
906 printf("%-23s: 0x%08x\n", label, val);
909 static inline void inspect_fw_phexpost(char *label,
910 uint32_t val, char *post)
912 printf("%-23s: 0x%08x (%s)\n", label, val, post);
915 static inline void inspect_fw_phexdef(char *label,
916 uint32_t val, uint32_t defval)
918 printf("%-23s: 0x%08x ", label, val);
921 printf("(== OpenWrt default)\n");
923 printf("(OpenWrt default: 0x%08x)\n", defval);
926 static inline void inspect_fw_phexexp(char *label,
927 uint32_t val, uint32_t expval)
929 printf("%-23s: 0x%08x ", label, val);
934 printf("(expected: 0x%08x)\n", expval);
937 static inline void inspect_fw_phexdec(char *label, uint32_t val)
939 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
942 static inline void inspect_fw_phexdecdef(char *label,
943 uint32_t val, uint32_t defval)
945 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
948 printf("(== OpenWrt default)\n");
950 printf("(OpenWrt default: 0x%08x)\n", defval);
953 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
957 printf("%-23s:", label);
958 for (i=0; i<MD5SUM_LEN; i++)
959 printf(" %02x", val[i]);
960 printf(" %s\n", text);
963 static int inspect_fw(void)
966 struct fw_header *hdr;
967 uint8_t md5sum[MD5SUM_LEN];
968 struct board_info *board;
969 int ret = EXIT_FAILURE;
971 buf = malloc(inspect_info.file_size);
973 ERR("no memory for buffer!\n");
977 ret = read_to_buf(&inspect_info, buf);
980 hdr = (struct fw_header *)buf;
982 inspect_fw_pstr("File name", inspect_info.file_name);
983 inspect_fw_phexdec("File size", inspect_info.file_size);
985 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
986 ERR("file does not seem to have V1 header!\n");
990 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
992 if (ntohl(hdr->unk1) != 0)
993 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
995 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
996 if (ntohl(hdr->boot_len) == 0)
997 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
999 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
1000 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
1002 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
1003 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
1004 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
1006 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
1008 if (ntohl(hdr->unk2) != 0)
1009 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
1010 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
1011 "(purpose yet unknown, unchecked here)");
1012 if (ntohl(hdr->unk3) != 0)
1013 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
1017 inspect_fw_pstr("Vendor name", hdr->vendor_name);
1018 inspect_fw_pstr("Firmware version", hdr->fw_version);
1019 board = find_board_by_hwid(ntohl(hdr->hw_id));
1021 layout = find_layout(board->layout_id);
1022 inspect_fw_phexpost("Hardware ID",
1023 ntohl(hdr->hw_id), board->id);
1024 inspect_fw_phexexp("Hardware Revision",
1025 ntohl(hdr->hw_rev), board->hw_rev);
1027 inspect_fw_phexpost("Hardware ID",
1028 ntohl(hdr->hw_id), "unknown");
1029 inspect_fw_phex("Hardware Revision",
1030 ntohl(hdr->hw_rev));
1035 inspect_fw_phexdec("Kernel data offset",
1036 ntohl(hdr->kernel_ofs));
1037 inspect_fw_phexdec("Kernel data length",
1038 ntohl(hdr->kernel_len));
1040 inspect_fw_phexdef("Kernel load address",
1041 ntohl(hdr->kernel_la),
1042 layout ? layout->kernel_la : 0xffffffff);
1043 inspect_fw_phexdef("Kernel entry point",
1044 ntohl(hdr->kernel_ep),
1045 layout ? layout->kernel_ep : 0xffffffff);
1046 inspect_fw_phexdecdef("Rootfs data offset",
1047 ntohl(hdr->rootfs_ofs),
1048 layout ? layout->rootfs_ofs : 0xffffffff);
1050 inspect_fw_phex("Kernel load address",
1051 ntohl(hdr->kernel_la));
1052 inspect_fw_phex("Kernel entry point",
1053 ntohl(hdr->kernel_ep));
1054 inspect_fw_phexdec("Rootfs data offset",
1055 ntohl(hdr->rootfs_ofs));
1057 inspect_fw_phexdec("Rootfs data length",
1058 ntohl(hdr->rootfs_len));
1059 inspect_fw_phexdec("Boot loader data offset",
1060 ntohl(hdr->boot_ofs));
1061 inspect_fw_phexdec("Boot loader data length",
1062 ntohl(hdr->boot_len));
1063 inspect_fw_phexdec("Total firmware length",
1064 ntohl(hdr->fw_length));
1072 filename = malloc(strlen(inspect_info.file_name) + 8);
1073 sprintf(filename, "%s-kernel", inspect_info.file_name);
1074 printf("Extracting kernel to \"%s\"...\n", filename);
1075 fp = fopen(filename, "w");
1077 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
1078 ntohl(hdr->kernel_len), 1, fp)) {
1079 ERR("error in fwrite(): %s", strerror(errno));
1083 ERR("error in fopen(): %s", strerror(errno));
1087 filename = malloc(strlen(inspect_info.file_name) + 8);
1088 sprintf(filename, "%s-rootfs", inspect_info.file_name);
1089 printf("Extracting rootfs to \"%s\"...\n", filename);
1090 fp = fopen(filename, "w");
1092 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
1093 ntohl(hdr->rootfs_len), 1, fp)) {
1094 ERR("error in fwrite(): %s", strerror(errno));
1098 ERR("error in fopen(): %s", strerror(errno));
1109 int main(int argc, char *argv[])
1111 int ret = EXIT_FAILURE;
1116 progname = basename(argv[0]);
1121 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xX:hsSjv:");
1127 sscanf(optarg, "0x%x", &rootfs_align);
1136 sscanf(optarg, "0x%x", &kernel_ep);
1142 opt_hw_rev = optarg;
1145 sscanf(optarg, "0x%x", &kernel_la);
1160 kernel_info.file_name = optarg;
1163 rootfs_info.file_name = optarg;
1166 sscanf(optarg, "0x%x", &rootfs_ofs);
1178 inspect_info.file_name = optarg;
1187 usage(EXIT_SUCCESS);
1190 sscanf(optarg, "0x%x", &reserved_space);
1193 usage(EXIT_FAILURE);
1198 ret = check_options();
1202 if (!inspect_info.file_name)