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_GS_OOLITE_V1 0x3C000101
34 #define HWID_TL_MR10U_V1 0x00100101
35 #define HWID_TL_MR13U_V1 0x00130101
36 #define HWID_TL_MR3020_V1 0x30200001
37 #define HWID_TL_MR3220_V1 0x32200001
38 #define HWID_TL_MR3220_V2 0x32200002
39 #define HWID_TL_MR3420_V1 0x34200001
40 #define HWID_TL_MR3420_V2 0x34200002
41 #define HWID_TL_WA701N_V1 0x07010001
42 #define HWID_TL_WA7510N_V1 0x75100001
43 #define HWID_TL_WA801ND_V1 0x08010001
44 #define HWID_TL_WA830RE_V1 0x08300010
45 #define HWID_TL_WA830RE_V2 0x08300002
46 #define HWID_TL_WA801ND_V2 0x08010002
47 #define HWID_TL_WA901ND_V1 0x09010001
48 #define HWID_TL_WA901ND_V2 0x09010002
49 #define HWID_TL_WDR4900_V1 0x49000001
50 #define HWID_TL_WR703N_V1 0x07030101
51 #define HWID_TL_WR720N_V3 0x07200103
52 #define HWID_TL_WR741ND_V1 0x07410001
53 #define HWID_TL_WR741ND_V4 0x07410004
54 #define HWID_TL_WR740N_V1 0x07400001
55 #define HWID_TL_WR740N_V3 0x07400003
56 #define HWID_TL_WR743ND_V1 0x07430001
57 #define HWID_TL_WR743ND_V2 0x07430002
58 #define HWID_TL_WR841N_V1_5 0x08410002
59 #define HWID_TL_WR841ND_V3 0x08410003
60 #define HWID_TL_WR841ND_V5 0x08410005
61 #define HWID_TL_WR841ND_V7 0x08410007
62 #define HWID_TL_WR941ND_V2 0x09410002
63 #define HWID_TL_WR941ND_V4 0x09410004
64 #define HWID_TL_WR1043ND_V1 0x10430001
65 #define HWID_TL_WR1043ND_V2 0x10430002
66 #define HWID_TL_WR1041N_V2 0x10410002
67 #define HWID_TL_WR2543N_V1 0x25430001
72 char *file_name; /* name of the file */
73 uint32_t file_size; /* length of the file */
77 uint32_t version; /* header version */
80 uint32_t hw_id; /* hardware id */
81 uint32_t hw_rev; /* hardware revision */
83 uint8_t md5sum1[MD5SUM_LEN];
85 uint8_t md5sum2[MD5SUM_LEN];
87 uint32_t kernel_la; /* kernel load address */
88 uint32_t kernel_ep; /* kernel entry point */
89 uint32_t fw_length; /* total length of the firmware */
90 uint32_t kernel_ofs; /* kernel data offset */
91 uint32_t kernel_len; /* kernel data length */
92 uint32_t rootfs_ofs; /* rootfs data offset */
93 uint32_t rootfs_len; /* rootfs data length */
94 uint32_t boot_ofs; /* bootloader data offset */
95 uint32_t boot_len; /* bootloader data length */
100 } __attribute__ ((packed));
102 struct flash_layout {
121 static char *progname;
122 static char *vendor = "TP-LINK Technologies";
123 static char *version = "ver. 1.0";
124 static char *fw_ver = "0.0.0";
126 static char *board_id;
127 static struct board_info *board;
128 static char *layout_id;
129 static struct flash_layout *layout;
130 static char *opt_hw_id;
131 static uint32_t hw_id;
132 static char *opt_hw_rev;
133 static uint32_t hw_rev;
134 static int fw_ver_lo;
135 static int fw_ver_mid;
136 static int fw_ver_hi;
137 static struct file_info kernel_info;
138 static uint32_t kernel_la = 0;
139 static uint32_t kernel_ep = 0;
140 static uint32_t kernel_len = 0;
141 static struct file_info rootfs_info;
142 static uint32_t rootfs_ofs = 0;
143 static uint32_t rootfs_align;
144 static struct file_info boot_info;
146 static int strip_padding;
147 static int add_jffs2_eof;
148 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
149 static uint32_t fw_max_len;
150 static uint32_t reserved_space;
152 static struct file_info inspect_info;
153 static int extract = 0;
155 char md5salt_normal[MD5SUM_LEN] = {
156 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
157 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
160 char md5salt_boot[MD5SUM_LEN] = {
161 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
162 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
165 static struct flash_layout layouts[] = {
168 .fw_max_len = 0x3c0000,
169 .kernel_la = 0x80060000,
170 .kernel_ep = 0x80060000,
171 .rootfs_ofs = 0x140000,
174 .fw_max_len = 0x3c0000,
175 .kernel_la = 0x80060000,
176 .kernel_ep = 0x80060000,
177 .rootfs_ofs = 0x100000,
180 .fw_max_len = 0x7c0000,
181 .kernel_la = 0x80060000,
182 .kernel_ep = 0x80060000,
183 .rootfs_ofs = 0x140000,
186 .fw_max_len = 0x7c0000,
187 .kernel_la = 0x80060000,
188 .kernel_ep = 0x80060000,
189 .rootfs_ofs = 0x100000,
192 .fw_max_len = 0xf80000,
193 .kernel_la = 0x80060000,
194 .kernel_ep = 0x80060000,
195 .rootfs_ofs = 0x140000,
198 .fw_max_len = 0xf80000,
199 .kernel_la = 0x80060000,
200 .kernel_ep = 0x80060000,
201 .rootfs_ofs = 0x100000,
204 .fw_max_len = 0xf80000,
205 .kernel_la = 0x00000000,
206 .kernel_ep = 0xc0000000,
207 .rootfs_ofs = 0x2a0000,
209 /* terminating entry */
213 static struct board_info boards[] = {
216 .hw_id = HWID_TL_MR10U_V1,
218 .layout_id = "4Mlzma",
221 .hw_id = HWID_TL_MR13U_V1,
223 .layout_id = "4Mlzma",
226 .hw_id = HWID_TL_MR3020_V1,
228 .layout_id = "4Mlzma",
231 .hw_id = HWID_TL_MR3220_V1,
236 .hw_id = HWID_TL_MR3220_V2,
238 .layout_id = "4Mlzma",
241 .hw_id = HWID_TL_MR3420_V1,
246 .hw_id = HWID_TL_MR3420_V2,
248 .layout_id = "4Mlzma",
251 .hw_id = HWID_TL_WA701N_V1,
256 .hw_id = HWID_TL_WA7510N_V1,
260 .id = "TL-WA801NDv1",
261 .hw_id = HWID_TL_WA801ND_V1,
265 .id = "TL-WA830REv1",
266 .hw_id = HWID_TL_WA830RE_V1,
270 .id = "TL-WA830REv2",
271 .hw_id = HWID_TL_WA830RE_V2,
275 .id = "TL-WA801NDv2",
276 .hw_id = HWID_TL_WA801ND_V2,
278 .layout_id = "4Mlzma",
280 .id = "TL-WA901NDv1",
281 .hw_id = HWID_TL_WA901ND_V1,
285 .id = "TL-WA901NDv2",
286 .hw_id = HWID_TL_WA901ND_V2,
290 .id = "TL-WDR4900v1",
291 .hw_id = HWID_TL_WDR4900_V1,
293 .layout_id = "16Mppc",
295 .id = "TL-WR741NDv1",
296 .hw_id = HWID_TL_WR741ND_V1,
300 .id = "TL-WR741NDv4",
301 .hw_id = HWID_TL_WR741ND_V4,
303 .layout_id = "4Mlzma",
306 .hw_id = HWID_TL_WR740N_V1,
311 .hw_id = HWID_TL_WR740N_V3,
315 .id = "TL-WR743NDv1",
316 .hw_id = HWID_TL_WR743ND_V1,
320 .id = "TL-WR743NDv2",
321 .hw_id = HWID_TL_WR743ND_V2,
323 .layout_id = "4Mlzma",
325 .id = "TL-WR841Nv1.5",
326 .hw_id = HWID_TL_WR841N_V1_5,
330 .id = "TL-WR841NDv3",
331 .hw_id = HWID_TL_WR841ND_V3,
335 .id = "TL-WR841NDv5",
336 .hw_id = HWID_TL_WR841ND_V5,
340 .id = "TL-WR841NDv7",
341 .hw_id = HWID_TL_WR841ND_V7,
345 .id = "TL-WR941NDv2",
346 .hw_id = HWID_TL_WR941ND_V2,
350 .id = "TL-WR941NDv4",
351 .hw_id = HWID_TL_WR941ND_V4,
355 .id = "TL-WR1041Nv2",
356 .hw_id = HWID_TL_WR1041N_V2,
358 .layout_id = "4Mlzma",
360 .id = "TL-WR1043NDv1",
361 .hw_id = HWID_TL_WR1043ND_V1,
365 .id = "TL-WR1043NDv2",
366 .hw_id = HWID_TL_WR1043ND_V2,
368 .layout_id = "8Mlzma",
370 .id = "TL-WR2543Nv1",
371 .hw_id = HWID_TL_WR2543N_V1,
373 .layout_id = "8Mlzma",
376 .hw_id = HWID_TL_WR703N_V1,
378 .layout_id = "4Mlzma",
381 .hw_id = HWID_TL_WR720N_V3,
383 .layout_id = "4Mlzma",
386 .hw_id = HWID_GS_OOLITE_V1,
388 .layout_id = "16Mlzma",
390 /* terminating entry */
397 #define ERR(fmt, ...) do { \
399 fprintf(stderr, "[%s] *** error: " fmt "\n", \
400 progname, ## __VA_ARGS__ ); \
403 #define ERRS(fmt, ...) do { \
406 fprintf(stderr, "[%s] *** error: " fmt "\n", \
407 progname, ## __VA_ARGS__, strerror(save)); \
410 #define DBG(fmt, ...) do { \
411 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
414 static struct board_info *find_board(char *id)
416 struct board_info *ret;
417 struct board_info *board;
420 for (board = boards; board->id != NULL; board++){
421 if (strcasecmp(id, board->id) == 0) {
430 static struct board_info *find_board_by_hwid(uint32_t hw_id)
432 struct board_info *board;
434 for (board = boards; board->id != NULL; board++) {
435 if (hw_id == board->hw_id)
442 static struct flash_layout *find_layout(char *id)
444 struct flash_layout *ret;
445 struct flash_layout *l;
448 for (l = layouts; l->id != NULL; l++){
449 if (strcasecmp(id, l->id) == 0) {
458 static void usage(int status)
460 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
461 struct board_info *board;
463 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
467 " -B <board> create image for the board specified with <board>\n"
468 " -c use combined kernel image\n"
469 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
470 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
471 " -H <hwid> use hardware id specified with <hwid>\n"
472 " -W <hwrev> use hardware revision specified with <hwrev>\n"
473 " -F <id> use flash layout specified with <id>\n"
474 " -k <file> read kernel image from the file <file>\n"
475 " -r <file> read rootfs image from the file <file>\n"
476 " -a <align> align the rootfs start on an <align> bytes boundary\n"
477 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
478 " -o <file> write output to the file <file>\n"
479 " -s strip padding from the end of the image\n"
480 " -j add jffs2 end-of-filesystem markers\n"
481 " -N <vendor> set image vendor to <vendor>\n"
482 " -V <version> set image version to <version>\n"
483 " -v <version> set firmware version to <version>\n"
484 " -i <file> inspect given firmware file <file>\n"
485 " -x extract kernel and rootfs while inspecting (requires -i)\n"
486 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
487 " -h show this screen\n"
493 static int get_md5(char *data, int size, char *md5)
498 MD5_Update(&ctx, data, size);
499 MD5_Final(md5, &ctx);
502 static int get_file_stat(struct file_info *fdata)
507 if (fdata->file_name == NULL)
510 res = stat(fdata->file_name, &st);
512 ERRS("stat failed on %s", fdata->file_name);
516 fdata->file_size = st.st_size;
520 static int read_to_buf(struct file_info *fdata, char *buf)
523 int ret = EXIT_FAILURE;
525 f = fopen(fdata->file_name, "r");
527 ERRS("could not open \"%s\" for reading", fdata->file_name);
532 fread(buf, fdata->file_size, 1, f);
534 ERRS("unable to read from file \"%s\"", fdata->file_name);
546 static int check_options(void)
550 if (inspect_info.file_name) {
551 ret = get_file_stat(&inspect_info);
556 } else if (extract) {
557 ERR("no firmware for inspection specified");
561 if (board_id == NULL && opt_hw_id == NULL) {
562 ERR("either board or hardware id must be specified");
567 board = find_board(board_id);
569 ERR("unknown/unsupported board id \"%s\"", board_id);
572 if (layout_id == NULL)
573 layout_id = board->layout_id;
575 hw_id = board->hw_id;
576 hw_rev = board->hw_rev;
578 if (layout_id == NULL) {
579 ERR("flash layout is not specified");
582 hw_id = strtoul(opt_hw_id, NULL, 0);
585 hw_rev = strtoul(opt_hw_rev, NULL, 0);
590 layout = find_layout(layout_id);
591 if (layout == NULL) {
592 ERR("unknown flash layout \"%s\"", layout_id);
597 kernel_la = layout->kernel_la;
599 kernel_ep = layout->kernel_ep;
601 rootfs_ofs = layout->rootfs_ofs;
603 if (reserved_space > layout->fw_max_len) {
604 ERR("reserved space is not valid");
608 fw_max_len = layout->fw_max_len - reserved_space;
610 if (kernel_info.file_name == NULL) {
611 ERR("no kernel image specified");
615 ret = get_file_stat(&kernel_info);
619 kernel_len = kernel_info.file_size;
622 if (kernel_info.file_size >
623 fw_max_len - sizeof(struct fw_header)) {
624 ERR("kernel image is too big");
628 if (rootfs_info.file_name == NULL) {
629 ERR("no rootfs image specified");
633 ret = get_file_stat(&rootfs_info);
638 kernel_len += sizeof(struct fw_header);
639 kernel_len = ALIGN(kernel_len, rootfs_align);
640 kernel_len -= sizeof(struct fw_header);
642 DBG("kernel length aligned to %u", kernel_len);
644 if (kernel_len + rootfs_info.file_size >
645 fw_max_len - sizeof(struct fw_header)) {
646 ERR("images are too big");
650 if (kernel_info.file_size >
651 rootfs_ofs - sizeof(struct fw_header)) {
652 ERR("kernel image is too big");
656 if (rootfs_info.file_size >
657 (fw_max_len - rootfs_ofs)) {
658 ERR("rootfs image is too big");
664 if (ofname == NULL) {
665 ERR("no output file specified");
669 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
671 ERR("invalid firmware version '%s'", fw_ver);
678 static void fill_header(char *buf, int len)
680 struct fw_header *hdr = (struct fw_header *)buf;
682 memset(hdr, 0, sizeof(struct fw_header));
684 hdr->version = htonl(HEADER_VERSION_V1);
685 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
686 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
687 hdr->hw_id = htonl(hw_id);
688 hdr->hw_rev = htonl(hw_rev);
690 if (boot_info.file_size == 0)
691 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
693 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
695 hdr->kernel_la = htonl(kernel_la);
696 hdr->kernel_ep = htonl(kernel_ep);
697 hdr->fw_length = htonl(layout->fw_max_len);
698 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
699 hdr->kernel_len = htonl(kernel_len);
701 hdr->rootfs_ofs = htonl(rootfs_ofs);
702 hdr->rootfs_len = htonl(rootfs_info.file_size);
705 hdr->ver_hi = htons(fw_ver_hi);
706 hdr->ver_mid = htons(fw_ver_mid);
707 hdr->ver_lo = htons(fw_ver_lo);
709 get_md5(buf, len, hdr->md5sum1);
712 static int pad_jffs2(char *buf, int currlen)
718 pad_mask = (64 * 1024);
719 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
723 for (i = 10; i < 32; i++) {
729 len = ALIGN(len, mask);
731 for (i = 10; i < 32; i++) {
733 if ((len & (mask - 1)) == 0)
737 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
738 buf[len + i] = jffs2_eof_mark[i];
740 len += sizeof(jffs2_eof_mark);
746 static int write_fw(char *data, int len)
749 int ret = EXIT_FAILURE;
751 f = fopen(ofname, "w");
753 ERRS("could not open \"%s\" for writing", ofname);
758 fwrite(data, len, 1, f);
760 ERRS("unable to write output file");
764 DBG("firmware file \"%s\" completed", ofname);
771 if (ret != EXIT_SUCCESS) {
778 static int build_fw(void)
783 int ret = EXIT_FAILURE;
786 buflen = layout->fw_max_len;
788 buf = malloc(buflen);
790 ERR("no memory for buffer\n");
794 memset(buf, 0xff, buflen);
795 p = buf + sizeof(struct fw_header);
796 ret = read_to_buf(&kernel_info, p);
800 writelen = sizeof(struct fw_header) + kernel_len;
806 p = buf + rootfs_ofs;
808 ret = read_to_buf(&rootfs_info, p);
813 writelen += rootfs_info.file_size;
815 writelen = rootfs_ofs + rootfs_info.file_size;
818 writelen = pad_jffs2(buf, writelen);
824 fill_header(buf, writelen);
825 ret = write_fw(buf, writelen);
837 /* Helper functions to inspect_fw() representing different output formats */
838 static inline void inspect_fw_pstr(char *label, char *str)
840 printf("%-23s: %s\n", label, str);
843 static inline void inspect_fw_phex(char *label, uint32_t val)
845 printf("%-23s: 0x%08x\n", label, val);
848 static inline void inspect_fw_phexpost(char *label,
849 uint32_t val, char *post)
851 printf("%-23s: 0x%08x (%s)\n", label, val, post);
854 static inline void inspect_fw_phexdef(char *label,
855 uint32_t val, uint32_t defval)
857 printf("%-23s: 0x%08x ", label, val);
860 printf("(== OpenWrt default)\n");
862 printf("(OpenWrt default: 0x%08x)\n", defval);
865 static inline void inspect_fw_phexexp(char *label,
866 uint32_t val, uint32_t expval)
868 printf("%-23s: 0x%08x ", label, val);
873 printf("(expected: 0x%08x)\n", expval);
876 static inline void inspect_fw_phexdec(char *label, uint32_t val)
878 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
881 static inline void inspect_fw_phexdecdef(char *label,
882 uint32_t val, uint32_t defval)
884 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
887 printf("(== OpenWrt default)\n");
889 printf("(OpenWrt default: 0x%08x)\n", defval);
892 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
896 printf("%-23s:", label);
897 for (i=0; i<MD5SUM_LEN; i++)
898 printf(" %02x", val[i]);
899 printf(" %s\n", text);
902 static int inspect_fw(void)
905 struct fw_header *hdr;
906 uint8_t md5sum[MD5SUM_LEN];
907 struct board_info *board;
908 int ret = EXIT_FAILURE;
910 buf = malloc(inspect_info.file_size);
912 ERR("no memory for buffer!\n");
916 ret = read_to_buf(&inspect_info, buf);
919 hdr = (struct fw_header *)buf;
921 inspect_fw_pstr("File name", inspect_info.file_name);
922 inspect_fw_phexdec("File size", inspect_info.file_size);
924 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
925 ERR("file does not seem to have V1 header!\n");
929 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
931 if (ntohl(hdr->unk1) != 0)
932 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
934 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
935 if (ntohl(hdr->boot_len) == 0)
936 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
938 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
939 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
941 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
942 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
943 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
945 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
947 if (ntohl(hdr->unk2) != 0)
948 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
949 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
950 "(purpose yet unknown, unchecked here)");
951 if (ntohl(hdr->unk3) != 0)
952 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
956 inspect_fw_pstr("Vendor name", hdr->vendor_name);
957 inspect_fw_pstr("Firmware version", hdr->fw_version);
958 board = find_board_by_hwid(ntohl(hdr->hw_id));
960 layout = find_layout(board->layout_id);
961 inspect_fw_phexpost("Hardware ID",
962 ntohl(hdr->hw_id), board->id);
963 inspect_fw_phexexp("Hardware Revision",
964 ntohl(hdr->hw_rev), board->hw_rev);
966 inspect_fw_phexpost("Hardware ID",
967 ntohl(hdr->hw_id), "unknown");
968 inspect_fw_phex("Hardware Revision",
974 inspect_fw_phexdec("Kernel data offset",
975 ntohl(hdr->kernel_ofs));
976 inspect_fw_phexdec("Kernel data length",
977 ntohl(hdr->kernel_len));
979 inspect_fw_phexdef("Kernel load address",
980 ntohl(hdr->kernel_la),
981 layout ? layout->kernel_la : 0xffffffff);
982 inspect_fw_phexdef("Kernel entry point",
983 ntohl(hdr->kernel_ep),
984 layout ? layout->kernel_ep : 0xffffffff);
985 inspect_fw_phexdecdef("Rootfs data offset",
986 ntohl(hdr->rootfs_ofs),
987 layout ? layout->rootfs_ofs : 0xffffffff);
989 inspect_fw_phex("Kernel load address",
990 ntohl(hdr->kernel_la));
991 inspect_fw_phex("Kernel entry point",
992 ntohl(hdr->kernel_ep));
993 inspect_fw_phexdec("Rootfs data offset",
994 ntohl(hdr->rootfs_ofs));
996 inspect_fw_phexdec("Rootfs data length",
997 ntohl(hdr->rootfs_len));
998 inspect_fw_phexdec("Boot loader data offset",
999 ntohl(hdr->boot_ofs));
1000 inspect_fw_phexdec("Boot loader data length",
1001 ntohl(hdr->boot_len));
1002 inspect_fw_phexdec("Total firmware length",
1003 ntohl(hdr->fw_length));
1011 filename = malloc(strlen(inspect_info.file_name) + 8);
1012 sprintf(filename, "%s-kernel", inspect_info.file_name);
1013 printf("Extracting kernel to \"%s\"...\n", filename);
1014 fp = fopen(filename, "w");
1016 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
1017 ntohl(hdr->kernel_len), 1, fp)) {
1018 ERR("error in fwrite(): %s", strerror(errno));
1022 ERR("error in fopen(): %s", strerror(errno));
1026 filename = malloc(strlen(inspect_info.file_name) + 8);
1027 sprintf(filename, "%s-rootfs", inspect_info.file_name);
1028 printf("Extracting rootfs to \"%s\"...\n", filename);
1029 fp = fopen(filename, "w");
1031 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
1032 ntohl(hdr->rootfs_len), 1, fp)) {
1033 ERR("error in fwrite(): %s", strerror(errno));
1037 ERR("error in fopen(): %s", strerror(errno));
1048 int main(int argc, char *argv[])
1050 int ret = EXIT_FAILURE;
1055 progname = basename(argv[0]);
1060 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xX:hsjv:");
1066 sscanf(optarg, "0x%x", &rootfs_align);
1075 sscanf(optarg, "0x%x", &kernel_ep);
1081 opt_hw_rev = optarg;
1084 sscanf(optarg, "0x%x", &kernel_la);
1099 kernel_info.file_name = optarg;
1102 rootfs_info.file_name = optarg;
1105 sscanf(optarg, "0x%x", &rootfs_ofs);
1114 inspect_info.file_name = optarg;
1123 usage(EXIT_SUCCESS);
1126 sscanf(optarg, "0x%x", &reserved_space);
1129 usage(EXIT_FAILURE);
1134 ret = check_options();
1138 if (!inspect_info.file_name)