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_GL_INET_V1 0x08000001
34 #define HWID_GS_OOLITE_V1 0x3C000101
35 #define HWID_TL_MR10U_V1 0x00100101
36 #define HWID_TL_MR13U_V1 0x00130101
37 #define HWID_TL_MR3020_V1 0x30200001
38 #define HWID_TL_MR3220_V1 0x32200001
39 #define HWID_TL_MR3220_V2 0x32200002
40 #define HWID_TL_MR3420_V1 0x34200001
41 #define HWID_TL_MR3420_V2 0x34200002
42 #define HWID_TL_WA701N_V1 0x07010001
43 #define HWID_TL_WA701N_V2 0x07010002
44 #define HWID_TL_WA7210N_V2 0x72100002
45 #define HWID_TL_WA7510N_V1 0x75100001
46 #define HWID_TL_WA801ND_V1 0x08010001
47 #define HWID_TL_WA830RE_V1 0x08300010
48 #define HWID_TL_WA830RE_V2 0x08300002
49 #define HWID_TL_WA801ND_V2 0x08010002
50 #define HWID_TL_WA901ND_V1 0x09010001
51 #define HWID_TL_WA901ND_V2 0x09010002
52 #define HWID_TL_WDR4300_V1_IL 0x43008001
53 #define HWID_TL_WDR4900_V1 0x49000001
54 #define HWID_TL_WR703N_V1 0x07030101
55 #define HWID_TL_WR720N_V3 0x07200103
56 #define HWID_TL_WR741ND_V1 0x07410001
57 #define HWID_TL_WR741ND_V4 0x07410004
58 #define HWID_TL_WR740N_V1 0x07400001
59 #define HWID_TL_WR740N_V3 0x07400003
60 #define HWID_TL_WR743ND_V1 0x07430001
61 #define HWID_TL_WR743ND_V2 0x07430002
62 #define HWID_TL_WR841N_V1_5 0x08410002
63 #define HWID_TL_WR841ND_V3 0x08410003
64 #define HWID_TL_WR841ND_V5 0x08410005
65 #define HWID_TL_WR841ND_V7 0x08410007
66 #define HWID_TL_WR941ND_V2 0x09410002
67 #define HWID_TL_WR941ND_V4 0x09410004
68 #define HWID_TL_WR1043ND_V1 0x10430001
69 #define HWID_TL_WR1043ND_V2 0x10430002
70 #define HWID_TL_WR1041N_V2 0x10410002
71 #define HWID_TL_WR2543N_V1 0x25430001
76 char *file_name; /* name of the file */
77 uint32_t file_size; /* length of the file */
81 uint32_t version; /* header version */
84 uint32_t hw_id; /* hardware id */
85 uint32_t hw_rev; /* hardware revision */
87 uint8_t md5sum1[MD5SUM_LEN];
89 uint8_t md5sum2[MD5SUM_LEN];
91 uint32_t kernel_la; /* kernel load address */
92 uint32_t kernel_ep; /* kernel entry point */
93 uint32_t fw_length; /* total length of the firmware */
94 uint32_t kernel_ofs; /* kernel data offset */
95 uint32_t kernel_len; /* kernel data length */
96 uint32_t rootfs_ofs; /* rootfs data offset */
97 uint32_t rootfs_len; /* rootfs data length */
98 uint32_t boot_ofs; /* bootloader data offset */
99 uint32_t boot_len; /* bootloader data length */
104 } __attribute__ ((packed));
106 struct flash_layout {
125 static char *progname;
126 static char *vendor = "TP-LINK Technologies";
127 static char *version = "ver. 1.0";
128 static char *fw_ver = "0.0.0";
130 static char *board_id;
131 static struct board_info *board;
132 static char *layout_id;
133 static struct flash_layout *layout;
134 static char *opt_hw_id;
135 static uint32_t hw_id;
136 static char *opt_hw_rev;
137 static uint32_t hw_rev;
138 static int fw_ver_lo;
139 static int fw_ver_mid;
140 static int fw_ver_hi;
141 static struct file_info kernel_info;
142 static uint32_t kernel_la = 0;
143 static uint32_t kernel_ep = 0;
144 static uint32_t kernel_len = 0;
145 static struct file_info rootfs_info;
146 static uint32_t rootfs_ofs = 0;
147 static uint32_t rootfs_align;
148 static struct file_info boot_info;
150 static int strip_padding;
151 static int add_jffs2_eof;
152 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
153 static uint32_t fw_max_len;
154 static uint32_t reserved_space;
156 static struct file_info inspect_info;
157 static int extract = 0;
159 char md5salt_normal[MD5SUM_LEN] = {
160 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
161 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
164 char md5salt_boot[MD5SUM_LEN] = {
165 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
166 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
169 static struct flash_layout layouts[] = {
172 .fw_max_len = 0x3c0000,
173 .kernel_la = 0x80060000,
174 .kernel_ep = 0x80060000,
175 .rootfs_ofs = 0x140000,
178 .fw_max_len = 0x3c0000,
179 .kernel_la = 0x80060000,
180 .kernel_ep = 0x80060000,
181 .rootfs_ofs = 0x100000,
184 .fw_max_len = 0x7c0000,
185 .kernel_la = 0x80060000,
186 .kernel_ep = 0x80060000,
187 .rootfs_ofs = 0x140000,
190 .fw_max_len = 0x7c0000,
191 .kernel_la = 0x80060000,
192 .kernel_ep = 0x80060000,
193 .rootfs_ofs = 0x100000,
196 .fw_max_len = 0xf80000,
197 .kernel_la = 0x80060000,
198 .kernel_ep = 0x80060000,
199 .rootfs_ofs = 0x140000,
202 .fw_max_len = 0xf80000,
203 .kernel_la = 0x80060000,
204 .kernel_ep = 0x80060000,
205 .rootfs_ofs = 0x100000,
208 .fw_max_len = 0xf80000,
209 .kernel_la = 0x00000000,
210 .kernel_ep = 0xc0000000,
211 .rootfs_ofs = 0x2a0000,
213 /* terminating entry */
217 static struct board_info boards[] = {
220 .hw_id = HWID_TL_MR10U_V1,
222 .layout_id = "4Mlzma",
225 .hw_id = HWID_TL_MR13U_V1,
227 .layout_id = "4Mlzma",
230 .hw_id = HWID_TL_MR3020_V1,
232 .layout_id = "4Mlzma",
235 .hw_id = HWID_TL_MR3220_V1,
240 .hw_id = HWID_TL_MR3220_V2,
242 .layout_id = "4Mlzma",
245 .hw_id = HWID_TL_MR3420_V1,
250 .hw_id = HWID_TL_MR3420_V2,
252 .layout_id = "4Mlzma",
255 .hw_id = HWID_TL_WA701N_V1,
260 .hw_id = HWID_TL_WA701N_V2,
262 .layout_id = "4Mlzma",
265 .hw_id = HWID_TL_WA7210N_V2,
267 .layout_id = "4Mlzma",
270 .hw_id = HWID_TL_WA7510N_V1,
274 .id = "TL-WA801NDv1",
275 .hw_id = HWID_TL_WA801ND_V1,
279 .id = "TL-WA830REv1",
280 .hw_id = HWID_TL_WA830RE_V1,
284 .id = "TL-WA830REv2",
285 .hw_id = HWID_TL_WA830RE_V2,
289 .id = "TL-WA801NDv2",
290 .hw_id = HWID_TL_WA801ND_V2,
292 .layout_id = "4Mlzma",
294 .id = "TL-WA901NDv1",
295 .hw_id = HWID_TL_WA901ND_V1,
299 .id = "TL-WA901NDv2",
300 .hw_id = HWID_TL_WA901ND_V2,
304 .id = "TL-WDR4300v1",
305 .hw_id = HWID_TL_WDR4300_V1_IL,
307 .layout_id = "8Mlzma",
309 .id = "TL-WDR4900v1",
310 .hw_id = HWID_TL_WDR4900_V1,
312 .layout_id = "16Mppc",
314 .id = "TL-WR741NDv1",
315 .hw_id = HWID_TL_WR741ND_V1,
319 .id = "TL-WR741NDv4",
320 .hw_id = HWID_TL_WR741ND_V4,
322 .layout_id = "4Mlzma",
325 .hw_id = HWID_TL_WR740N_V1,
330 .hw_id = HWID_TL_WR740N_V3,
334 .id = "TL-WR743NDv1",
335 .hw_id = HWID_TL_WR743ND_V1,
339 .id = "TL-WR743NDv2",
340 .hw_id = HWID_TL_WR743ND_V2,
342 .layout_id = "4Mlzma",
344 .id = "TL-WR841Nv1.5",
345 .hw_id = HWID_TL_WR841N_V1_5,
349 .id = "TL-WR841NDv3",
350 .hw_id = HWID_TL_WR841ND_V3,
354 .id = "TL-WR841NDv5",
355 .hw_id = HWID_TL_WR841ND_V5,
359 .id = "TL-WR841NDv7",
360 .hw_id = HWID_TL_WR841ND_V7,
364 .id = "TL-WR941NDv2",
365 .hw_id = HWID_TL_WR941ND_V2,
369 .id = "TL-WR941NDv4",
370 .hw_id = HWID_TL_WR941ND_V4,
374 .id = "TL-WR1041Nv2",
375 .hw_id = HWID_TL_WR1041N_V2,
377 .layout_id = "4Mlzma",
379 .id = "TL-WR1043NDv1",
380 .hw_id = HWID_TL_WR1043ND_V1,
384 .id = "TL-WR1043NDv2",
385 .hw_id = HWID_TL_WR1043ND_V2,
387 .layout_id = "8Mlzma",
389 .id = "TL-WR2543Nv1",
390 .hw_id = HWID_TL_WR2543N_V1,
392 .layout_id = "8Mlzma",
395 .hw_id = HWID_TL_WR703N_V1,
397 .layout_id = "4Mlzma",
400 .hw_id = HWID_TL_WR720N_V3,
402 .layout_id = "4Mlzma",
405 .hw_id = HWID_GL_INET_V1,
407 .layout_id = "8Mlzma",
410 .hw_id = HWID_GS_OOLITE_V1,
412 .layout_id = "16Mlzma",
414 /* terminating entry */
421 #define ERR(fmt, ...) do { \
423 fprintf(stderr, "[%s] *** error: " fmt "\n", \
424 progname, ## __VA_ARGS__ ); \
427 #define ERRS(fmt, ...) do { \
430 fprintf(stderr, "[%s] *** error: " fmt "\n", \
431 progname, ## __VA_ARGS__, strerror(save)); \
434 #define DBG(fmt, ...) do { \
435 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
438 static struct board_info *find_board(char *id)
440 struct board_info *ret;
441 struct board_info *board;
444 for (board = boards; board->id != NULL; board++){
445 if (strcasecmp(id, board->id) == 0) {
454 static struct board_info *find_board_by_hwid(uint32_t hw_id)
456 struct board_info *board;
458 for (board = boards; board->id != NULL; board++) {
459 if (hw_id == board->hw_id)
466 static struct flash_layout *find_layout(char *id)
468 struct flash_layout *ret;
469 struct flash_layout *l;
472 for (l = layouts; l->id != NULL; l++){
473 if (strcasecmp(id, l->id) == 0) {
482 static void usage(int status)
484 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
485 struct board_info *board;
487 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
491 " -B <board> create image for the board specified with <board>\n"
492 " -c use combined kernel image\n"
493 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
494 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
495 " -H <hwid> use hardware id specified with <hwid>\n"
496 " -W <hwrev> use hardware revision specified with <hwrev>\n"
497 " -F <id> use flash layout specified with <id>\n"
498 " -k <file> read kernel image from the file <file>\n"
499 " -r <file> read rootfs image from the file <file>\n"
500 " -a <align> align the rootfs start on an <align> bytes boundary\n"
501 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
502 " -o <file> write output to the file <file>\n"
503 " -s strip padding from the end of the image\n"
504 " -j add jffs2 end-of-filesystem markers\n"
505 " -N <vendor> set image vendor to <vendor>\n"
506 " -V <version> set image version to <version>\n"
507 " -v <version> set firmware version to <version>\n"
508 " -i <file> inspect given firmware file <file>\n"
509 " -x extract kernel and rootfs while inspecting (requires -i)\n"
510 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
511 " -h show this screen\n"
517 static int get_md5(char *data, int size, char *md5)
522 MD5_Update(&ctx, data, size);
523 MD5_Final(md5, &ctx);
526 static int get_file_stat(struct file_info *fdata)
531 if (fdata->file_name == NULL)
534 res = stat(fdata->file_name, &st);
536 ERRS("stat failed on %s", fdata->file_name);
540 fdata->file_size = st.st_size;
544 static int read_to_buf(struct file_info *fdata, char *buf)
547 int ret = EXIT_FAILURE;
549 f = fopen(fdata->file_name, "r");
551 ERRS("could not open \"%s\" for reading", fdata->file_name);
556 fread(buf, fdata->file_size, 1, f);
558 ERRS("unable to read from file \"%s\"", fdata->file_name);
570 static int check_options(void)
574 if (inspect_info.file_name) {
575 ret = get_file_stat(&inspect_info);
580 } else if (extract) {
581 ERR("no firmware for inspection specified");
585 if (board_id == NULL && opt_hw_id == NULL) {
586 ERR("either board or hardware id must be specified");
591 board = find_board(board_id);
593 ERR("unknown/unsupported board id \"%s\"", board_id);
596 if (layout_id == NULL)
597 layout_id = board->layout_id;
599 hw_id = board->hw_id;
600 hw_rev = board->hw_rev;
602 if (layout_id == NULL) {
603 ERR("flash layout is not specified");
606 hw_id = strtoul(opt_hw_id, NULL, 0);
609 hw_rev = strtoul(opt_hw_rev, NULL, 0);
614 layout = find_layout(layout_id);
615 if (layout == NULL) {
616 ERR("unknown flash layout \"%s\"", layout_id);
621 kernel_la = layout->kernel_la;
623 kernel_ep = layout->kernel_ep;
625 rootfs_ofs = layout->rootfs_ofs;
627 if (reserved_space > layout->fw_max_len) {
628 ERR("reserved space is not valid");
632 fw_max_len = layout->fw_max_len - reserved_space;
634 if (kernel_info.file_name == NULL) {
635 ERR("no kernel image specified");
639 ret = get_file_stat(&kernel_info);
643 kernel_len = kernel_info.file_size;
646 if (kernel_info.file_size >
647 fw_max_len - sizeof(struct fw_header)) {
648 ERR("kernel image is too big");
652 if (rootfs_info.file_name == NULL) {
653 ERR("no rootfs image specified");
657 ret = get_file_stat(&rootfs_info);
662 kernel_len += sizeof(struct fw_header);
663 kernel_len = ALIGN(kernel_len, rootfs_align);
664 kernel_len -= sizeof(struct fw_header);
666 DBG("kernel length aligned to %u", kernel_len);
668 if (kernel_len + rootfs_info.file_size >
669 fw_max_len - sizeof(struct fw_header)) {
670 ERR("images are too big");
674 if (kernel_info.file_size >
675 rootfs_ofs - sizeof(struct fw_header)) {
676 ERR("kernel image is too big");
680 if (rootfs_info.file_size >
681 (fw_max_len - rootfs_ofs)) {
682 ERR("rootfs image is too big");
688 if (ofname == NULL) {
689 ERR("no output file specified");
693 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
695 ERR("invalid firmware version '%s'", fw_ver);
702 static void fill_header(char *buf, int len)
704 struct fw_header *hdr = (struct fw_header *)buf;
706 memset(hdr, 0, sizeof(struct fw_header));
708 hdr->version = htonl(HEADER_VERSION_V1);
709 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
710 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
711 hdr->hw_id = htonl(hw_id);
712 hdr->hw_rev = htonl(hw_rev);
714 if (boot_info.file_size == 0)
715 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
717 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
719 hdr->kernel_la = htonl(kernel_la);
720 hdr->kernel_ep = htonl(kernel_ep);
721 hdr->fw_length = htonl(layout->fw_max_len);
722 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
723 hdr->kernel_len = htonl(kernel_len);
725 hdr->rootfs_ofs = htonl(rootfs_ofs);
726 hdr->rootfs_len = htonl(rootfs_info.file_size);
729 hdr->ver_hi = htons(fw_ver_hi);
730 hdr->ver_mid = htons(fw_ver_mid);
731 hdr->ver_lo = htons(fw_ver_lo);
733 get_md5(buf, len, hdr->md5sum1);
736 static int pad_jffs2(char *buf, int currlen)
742 pad_mask = (64 * 1024);
743 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
747 for (i = 10; i < 32; i++) {
753 len = ALIGN(len, mask);
755 for (i = 10; i < 32; i++) {
757 if ((len & (mask - 1)) == 0)
761 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
762 buf[len + i] = jffs2_eof_mark[i];
764 len += sizeof(jffs2_eof_mark);
770 static int write_fw(char *data, int len)
773 int ret = EXIT_FAILURE;
775 f = fopen(ofname, "w");
777 ERRS("could not open \"%s\" for writing", ofname);
782 fwrite(data, len, 1, f);
784 ERRS("unable to write output file");
788 DBG("firmware file \"%s\" completed", ofname);
795 if (ret != EXIT_SUCCESS) {
802 static int build_fw(void)
807 int ret = EXIT_FAILURE;
810 buflen = layout->fw_max_len;
812 buf = malloc(buflen);
814 ERR("no memory for buffer\n");
818 memset(buf, 0xff, buflen);
819 p = buf + sizeof(struct fw_header);
820 ret = read_to_buf(&kernel_info, p);
824 writelen = sizeof(struct fw_header) + kernel_len;
830 p = buf + rootfs_ofs;
832 ret = read_to_buf(&rootfs_info, p);
837 writelen += rootfs_info.file_size;
839 writelen = rootfs_ofs + rootfs_info.file_size;
842 writelen = pad_jffs2(buf, writelen);
848 fill_header(buf, writelen);
849 ret = write_fw(buf, writelen);
861 /* Helper functions to inspect_fw() representing different output formats */
862 static inline void inspect_fw_pstr(char *label, char *str)
864 printf("%-23s: %s\n", label, str);
867 static inline void inspect_fw_phex(char *label, uint32_t val)
869 printf("%-23s: 0x%08x\n", label, val);
872 static inline void inspect_fw_phexpost(char *label,
873 uint32_t val, char *post)
875 printf("%-23s: 0x%08x (%s)\n", label, val, post);
878 static inline void inspect_fw_phexdef(char *label,
879 uint32_t val, uint32_t defval)
881 printf("%-23s: 0x%08x ", label, val);
884 printf("(== OpenWrt default)\n");
886 printf("(OpenWrt default: 0x%08x)\n", defval);
889 static inline void inspect_fw_phexexp(char *label,
890 uint32_t val, uint32_t expval)
892 printf("%-23s: 0x%08x ", label, val);
897 printf("(expected: 0x%08x)\n", expval);
900 static inline void inspect_fw_phexdec(char *label, uint32_t val)
902 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
905 static inline void inspect_fw_phexdecdef(char *label,
906 uint32_t val, uint32_t defval)
908 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
911 printf("(== OpenWrt default)\n");
913 printf("(OpenWrt default: 0x%08x)\n", defval);
916 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
920 printf("%-23s:", label);
921 for (i=0; i<MD5SUM_LEN; i++)
922 printf(" %02x", val[i]);
923 printf(" %s\n", text);
926 static int inspect_fw(void)
929 struct fw_header *hdr;
930 uint8_t md5sum[MD5SUM_LEN];
931 struct board_info *board;
932 int ret = EXIT_FAILURE;
934 buf = malloc(inspect_info.file_size);
936 ERR("no memory for buffer!\n");
940 ret = read_to_buf(&inspect_info, buf);
943 hdr = (struct fw_header *)buf;
945 inspect_fw_pstr("File name", inspect_info.file_name);
946 inspect_fw_phexdec("File size", inspect_info.file_size);
948 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
949 ERR("file does not seem to have V1 header!\n");
953 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
955 if (ntohl(hdr->unk1) != 0)
956 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
958 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
959 if (ntohl(hdr->boot_len) == 0)
960 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
962 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
963 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
965 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
966 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
967 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
969 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
971 if (ntohl(hdr->unk2) != 0)
972 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
973 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
974 "(purpose yet unknown, unchecked here)");
975 if (ntohl(hdr->unk3) != 0)
976 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
980 inspect_fw_pstr("Vendor name", hdr->vendor_name);
981 inspect_fw_pstr("Firmware version", hdr->fw_version);
982 board = find_board_by_hwid(ntohl(hdr->hw_id));
984 layout = find_layout(board->layout_id);
985 inspect_fw_phexpost("Hardware ID",
986 ntohl(hdr->hw_id), board->id);
987 inspect_fw_phexexp("Hardware Revision",
988 ntohl(hdr->hw_rev), board->hw_rev);
990 inspect_fw_phexpost("Hardware ID",
991 ntohl(hdr->hw_id), "unknown");
992 inspect_fw_phex("Hardware Revision",
998 inspect_fw_phexdec("Kernel data offset",
999 ntohl(hdr->kernel_ofs));
1000 inspect_fw_phexdec("Kernel data length",
1001 ntohl(hdr->kernel_len));
1003 inspect_fw_phexdef("Kernel load address",
1004 ntohl(hdr->kernel_la),
1005 layout ? layout->kernel_la : 0xffffffff);
1006 inspect_fw_phexdef("Kernel entry point",
1007 ntohl(hdr->kernel_ep),
1008 layout ? layout->kernel_ep : 0xffffffff);
1009 inspect_fw_phexdecdef("Rootfs data offset",
1010 ntohl(hdr->rootfs_ofs),
1011 layout ? layout->rootfs_ofs : 0xffffffff);
1013 inspect_fw_phex("Kernel load address",
1014 ntohl(hdr->kernel_la));
1015 inspect_fw_phex("Kernel entry point",
1016 ntohl(hdr->kernel_ep));
1017 inspect_fw_phexdec("Rootfs data offset",
1018 ntohl(hdr->rootfs_ofs));
1020 inspect_fw_phexdec("Rootfs data length",
1021 ntohl(hdr->rootfs_len));
1022 inspect_fw_phexdec("Boot loader data offset",
1023 ntohl(hdr->boot_ofs));
1024 inspect_fw_phexdec("Boot loader data length",
1025 ntohl(hdr->boot_len));
1026 inspect_fw_phexdec("Total firmware length",
1027 ntohl(hdr->fw_length));
1035 filename = malloc(strlen(inspect_info.file_name) + 8);
1036 sprintf(filename, "%s-kernel", inspect_info.file_name);
1037 printf("Extracting kernel to \"%s\"...\n", filename);
1038 fp = fopen(filename, "w");
1040 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
1041 ntohl(hdr->kernel_len), 1, fp)) {
1042 ERR("error in fwrite(): %s", strerror(errno));
1046 ERR("error in fopen(): %s", strerror(errno));
1050 filename = malloc(strlen(inspect_info.file_name) + 8);
1051 sprintf(filename, "%s-rootfs", inspect_info.file_name);
1052 printf("Extracting rootfs to \"%s\"...\n", filename);
1053 fp = fopen(filename, "w");
1055 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
1056 ntohl(hdr->rootfs_len), 1, fp)) {
1057 ERR("error in fwrite(): %s", strerror(errno));
1061 ERR("error in fopen(): %s", strerror(errno));
1072 int main(int argc, char *argv[])
1074 int ret = EXIT_FAILURE;
1079 progname = basename(argv[0]);
1084 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xX:hsjv:");
1090 sscanf(optarg, "0x%x", &rootfs_align);
1099 sscanf(optarg, "0x%x", &kernel_ep);
1105 opt_hw_rev = optarg;
1108 sscanf(optarg, "0x%x", &kernel_la);
1123 kernel_info.file_name = optarg;
1126 rootfs_info.file_name = optarg;
1129 sscanf(optarg, "0x%x", &rootfs_ofs);
1138 inspect_info.file_name = optarg;
1147 usage(EXIT_SUCCESS);
1150 sscanf(optarg, "0x%x", &reserved_space);
1153 usage(EXIT_FAILURE);
1158 ret = check_options();
1162 if (!inspect_info.file_name)