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_WDR4300_V1_IL 0x43008001
50 #define HWID_TL_WDR4900_V1 0x49000001
51 #define HWID_TL_WR703N_V1 0x07030101
52 #define HWID_TL_WR720N_V3 0x07200103
53 #define HWID_TL_WR741ND_V1 0x07410001
54 #define HWID_TL_WR741ND_V4 0x07410004
55 #define HWID_TL_WR740N_V1 0x07400001
56 #define HWID_TL_WR740N_V3 0x07400003
57 #define HWID_TL_WR743ND_V1 0x07430001
58 #define HWID_TL_WR743ND_V2 0x07430002
59 #define HWID_TL_WR841N_V1_5 0x08410002
60 #define HWID_TL_WR841ND_V3 0x08410003
61 #define HWID_TL_WR841ND_V5 0x08410005
62 #define HWID_TL_WR841ND_V7 0x08410007
63 #define HWID_TL_WR941ND_V2 0x09410002
64 #define HWID_TL_WR941ND_V4 0x09410004
65 #define HWID_TL_WR1043ND_V1 0x10430001
66 #define HWID_TL_WR1043ND_V2 0x10430002
67 #define HWID_TL_WR1041N_V2 0x10410002
68 #define HWID_TL_WR2543N_V1 0x25430001
73 char *file_name; /* name of the file */
74 uint32_t file_size; /* length of the file */
78 uint32_t version; /* header version */
81 uint32_t hw_id; /* hardware id */
82 uint32_t hw_rev; /* hardware revision */
84 uint8_t md5sum1[MD5SUM_LEN];
86 uint8_t md5sum2[MD5SUM_LEN];
88 uint32_t kernel_la; /* kernel load address */
89 uint32_t kernel_ep; /* kernel entry point */
90 uint32_t fw_length; /* total length of the firmware */
91 uint32_t kernel_ofs; /* kernel data offset */
92 uint32_t kernel_len; /* kernel data length */
93 uint32_t rootfs_ofs; /* rootfs data offset */
94 uint32_t rootfs_len; /* rootfs data length */
95 uint32_t boot_ofs; /* bootloader data offset */
96 uint32_t boot_len; /* bootloader data length */
101 } __attribute__ ((packed));
103 struct flash_layout {
122 static char *progname;
123 static char *vendor = "TP-LINK Technologies";
124 static char *version = "ver. 1.0";
125 static char *fw_ver = "0.0.0";
127 static char *board_id;
128 static struct board_info *board;
129 static char *layout_id;
130 static struct flash_layout *layout;
131 static char *opt_hw_id;
132 static uint32_t hw_id;
133 static char *opt_hw_rev;
134 static uint32_t hw_rev;
135 static int fw_ver_lo;
136 static int fw_ver_mid;
137 static int fw_ver_hi;
138 static struct file_info kernel_info;
139 static uint32_t kernel_la = 0;
140 static uint32_t kernel_ep = 0;
141 static uint32_t kernel_len = 0;
142 static struct file_info rootfs_info;
143 static uint32_t rootfs_ofs = 0;
144 static uint32_t rootfs_align;
145 static struct file_info boot_info;
147 static int strip_padding;
148 static int add_jffs2_eof;
149 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
150 static uint32_t fw_max_len;
151 static uint32_t reserved_space;
153 static struct file_info inspect_info;
154 static int extract = 0;
156 char md5salt_normal[MD5SUM_LEN] = {
157 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
158 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
161 char md5salt_boot[MD5SUM_LEN] = {
162 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
163 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
166 static struct flash_layout layouts[] = {
169 .fw_max_len = 0x3c0000,
170 .kernel_la = 0x80060000,
171 .kernel_ep = 0x80060000,
172 .rootfs_ofs = 0x140000,
175 .fw_max_len = 0x3c0000,
176 .kernel_la = 0x80060000,
177 .kernel_ep = 0x80060000,
178 .rootfs_ofs = 0x100000,
181 .fw_max_len = 0x7c0000,
182 .kernel_la = 0x80060000,
183 .kernel_ep = 0x80060000,
184 .rootfs_ofs = 0x140000,
187 .fw_max_len = 0x7c0000,
188 .kernel_la = 0x80060000,
189 .kernel_ep = 0x80060000,
190 .rootfs_ofs = 0x100000,
193 .fw_max_len = 0xf80000,
194 .kernel_la = 0x80060000,
195 .kernel_ep = 0x80060000,
196 .rootfs_ofs = 0x140000,
199 .fw_max_len = 0xf80000,
200 .kernel_la = 0x80060000,
201 .kernel_ep = 0x80060000,
202 .rootfs_ofs = 0x100000,
205 .fw_max_len = 0xf80000,
206 .kernel_la = 0x00000000,
207 .kernel_ep = 0xc0000000,
208 .rootfs_ofs = 0x2a0000,
210 /* terminating entry */
214 static struct board_info boards[] = {
217 .hw_id = HWID_TL_MR10U_V1,
219 .layout_id = "4Mlzma",
222 .hw_id = HWID_TL_MR13U_V1,
224 .layout_id = "4Mlzma",
227 .hw_id = HWID_TL_MR3020_V1,
229 .layout_id = "4Mlzma",
232 .hw_id = HWID_TL_MR3220_V1,
237 .hw_id = HWID_TL_MR3220_V2,
239 .layout_id = "4Mlzma",
242 .hw_id = HWID_TL_MR3420_V1,
247 .hw_id = HWID_TL_MR3420_V2,
249 .layout_id = "4Mlzma",
252 .hw_id = HWID_TL_WA701N_V1,
257 .hw_id = HWID_TL_WA7510N_V1,
261 .id = "TL-WA801NDv1",
262 .hw_id = HWID_TL_WA801ND_V1,
266 .id = "TL-WA830REv1",
267 .hw_id = HWID_TL_WA830RE_V1,
271 .id = "TL-WA830REv2",
272 .hw_id = HWID_TL_WA830RE_V2,
276 .id = "TL-WA801NDv2",
277 .hw_id = HWID_TL_WA801ND_V2,
279 .layout_id = "4Mlzma",
281 .id = "TL-WA901NDv1",
282 .hw_id = HWID_TL_WA901ND_V1,
286 .id = "TL-WA901NDv2",
287 .hw_id = HWID_TL_WA901ND_V2,
291 .id = "TL-WDR4300v1",
292 .hw_id = HWID_TL_WDR4300_V1_IL,
294 .layout_id = "8Mlzma",
296 .id = "TL-WDR4900v1",
297 .hw_id = HWID_TL_WDR4900_V1,
299 .layout_id = "16Mppc",
301 .id = "TL-WR741NDv1",
302 .hw_id = HWID_TL_WR741ND_V1,
306 .id = "TL-WR741NDv4",
307 .hw_id = HWID_TL_WR741ND_V4,
309 .layout_id = "4Mlzma",
312 .hw_id = HWID_TL_WR740N_V1,
317 .hw_id = HWID_TL_WR740N_V3,
321 .id = "TL-WR743NDv1",
322 .hw_id = HWID_TL_WR743ND_V1,
326 .id = "TL-WR743NDv2",
327 .hw_id = HWID_TL_WR743ND_V2,
329 .layout_id = "4Mlzma",
331 .id = "TL-WR841Nv1.5",
332 .hw_id = HWID_TL_WR841N_V1_5,
336 .id = "TL-WR841NDv3",
337 .hw_id = HWID_TL_WR841ND_V3,
341 .id = "TL-WR841NDv5",
342 .hw_id = HWID_TL_WR841ND_V5,
346 .id = "TL-WR841NDv7",
347 .hw_id = HWID_TL_WR841ND_V7,
351 .id = "TL-WR941NDv2",
352 .hw_id = HWID_TL_WR941ND_V2,
356 .id = "TL-WR941NDv4",
357 .hw_id = HWID_TL_WR941ND_V4,
361 .id = "TL-WR1041Nv2",
362 .hw_id = HWID_TL_WR1041N_V2,
364 .layout_id = "4Mlzma",
366 .id = "TL-WR1043NDv1",
367 .hw_id = HWID_TL_WR1043ND_V1,
371 .id = "TL-WR1043NDv2",
372 .hw_id = HWID_TL_WR1043ND_V2,
374 .layout_id = "8Mlzma",
376 .id = "TL-WR2543Nv1",
377 .hw_id = HWID_TL_WR2543N_V1,
379 .layout_id = "8Mlzma",
382 .hw_id = HWID_TL_WR703N_V1,
384 .layout_id = "4Mlzma",
387 .hw_id = HWID_TL_WR720N_V3,
389 .layout_id = "4Mlzma",
392 .hw_id = HWID_GS_OOLITE_V1,
394 .layout_id = "16Mlzma",
396 /* terminating entry */
403 #define ERR(fmt, ...) do { \
405 fprintf(stderr, "[%s] *** error: " fmt "\n", \
406 progname, ## __VA_ARGS__ ); \
409 #define ERRS(fmt, ...) do { \
412 fprintf(stderr, "[%s] *** error: " fmt "\n", \
413 progname, ## __VA_ARGS__, strerror(save)); \
416 #define DBG(fmt, ...) do { \
417 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
420 static struct board_info *find_board(char *id)
422 struct board_info *ret;
423 struct board_info *board;
426 for (board = boards; board->id != NULL; board++){
427 if (strcasecmp(id, board->id) == 0) {
436 static struct board_info *find_board_by_hwid(uint32_t hw_id)
438 struct board_info *board;
440 for (board = boards; board->id != NULL; board++) {
441 if (hw_id == board->hw_id)
448 static struct flash_layout *find_layout(char *id)
450 struct flash_layout *ret;
451 struct flash_layout *l;
454 for (l = layouts; l->id != NULL; l++){
455 if (strcasecmp(id, l->id) == 0) {
464 static void usage(int status)
466 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
467 struct board_info *board;
469 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
473 " -B <board> create image for the board specified with <board>\n"
474 " -c use combined kernel image\n"
475 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
476 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
477 " -H <hwid> use hardware id specified with <hwid>\n"
478 " -W <hwrev> use hardware revision specified with <hwrev>\n"
479 " -F <id> use flash layout specified with <id>\n"
480 " -k <file> read kernel image from the file <file>\n"
481 " -r <file> read rootfs image from the file <file>\n"
482 " -a <align> align the rootfs start on an <align> bytes boundary\n"
483 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
484 " -o <file> write output to the file <file>\n"
485 " -s strip padding from the end of the image\n"
486 " -j add jffs2 end-of-filesystem markers\n"
487 " -N <vendor> set image vendor to <vendor>\n"
488 " -V <version> set image version to <version>\n"
489 " -v <version> set firmware version to <version>\n"
490 " -i <file> inspect given firmware file <file>\n"
491 " -x extract kernel and rootfs while inspecting (requires -i)\n"
492 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
493 " -h show this screen\n"
499 static int get_md5(char *data, int size, char *md5)
504 MD5_Update(&ctx, data, size);
505 MD5_Final(md5, &ctx);
508 static int get_file_stat(struct file_info *fdata)
513 if (fdata->file_name == NULL)
516 res = stat(fdata->file_name, &st);
518 ERRS("stat failed on %s", fdata->file_name);
522 fdata->file_size = st.st_size;
526 static int read_to_buf(struct file_info *fdata, char *buf)
529 int ret = EXIT_FAILURE;
531 f = fopen(fdata->file_name, "r");
533 ERRS("could not open \"%s\" for reading", fdata->file_name);
538 fread(buf, fdata->file_size, 1, f);
540 ERRS("unable to read from file \"%s\"", fdata->file_name);
552 static int check_options(void)
556 if (inspect_info.file_name) {
557 ret = get_file_stat(&inspect_info);
562 } else if (extract) {
563 ERR("no firmware for inspection specified");
567 if (board_id == NULL && opt_hw_id == NULL) {
568 ERR("either board or hardware id must be specified");
573 board = find_board(board_id);
575 ERR("unknown/unsupported board id \"%s\"", board_id);
578 if (layout_id == NULL)
579 layout_id = board->layout_id;
581 hw_id = board->hw_id;
582 hw_rev = board->hw_rev;
584 if (layout_id == NULL) {
585 ERR("flash layout is not specified");
588 hw_id = strtoul(opt_hw_id, NULL, 0);
591 hw_rev = strtoul(opt_hw_rev, NULL, 0);
596 layout = find_layout(layout_id);
597 if (layout == NULL) {
598 ERR("unknown flash layout \"%s\"", layout_id);
603 kernel_la = layout->kernel_la;
605 kernel_ep = layout->kernel_ep;
607 rootfs_ofs = layout->rootfs_ofs;
609 if (reserved_space > layout->fw_max_len) {
610 ERR("reserved space is not valid");
614 fw_max_len = layout->fw_max_len - reserved_space;
616 if (kernel_info.file_name == NULL) {
617 ERR("no kernel image specified");
621 ret = get_file_stat(&kernel_info);
625 kernel_len = kernel_info.file_size;
628 if (kernel_info.file_size >
629 fw_max_len - sizeof(struct fw_header)) {
630 ERR("kernel image is too big");
634 if (rootfs_info.file_name == NULL) {
635 ERR("no rootfs image specified");
639 ret = get_file_stat(&rootfs_info);
644 kernel_len += sizeof(struct fw_header);
645 kernel_len = ALIGN(kernel_len, rootfs_align);
646 kernel_len -= sizeof(struct fw_header);
648 DBG("kernel length aligned to %u", kernel_len);
650 if (kernel_len + rootfs_info.file_size >
651 fw_max_len - sizeof(struct fw_header)) {
652 ERR("images are too big");
656 if (kernel_info.file_size >
657 rootfs_ofs - sizeof(struct fw_header)) {
658 ERR("kernel image is too big");
662 if (rootfs_info.file_size >
663 (fw_max_len - rootfs_ofs)) {
664 ERR("rootfs image is too big");
670 if (ofname == NULL) {
671 ERR("no output file specified");
675 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
677 ERR("invalid firmware version '%s'", fw_ver);
684 static void fill_header(char *buf, int len)
686 struct fw_header *hdr = (struct fw_header *)buf;
688 memset(hdr, 0, sizeof(struct fw_header));
690 hdr->version = htonl(HEADER_VERSION_V1);
691 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
692 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
693 hdr->hw_id = htonl(hw_id);
694 hdr->hw_rev = htonl(hw_rev);
696 if (boot_info.file_size == 0)
697 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
699 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
701 hdr->kernel_la = htonl(kernel_la);
702 hdr->kernel_ep = htonl(kernel_ep);
703 hdr->fw_length = htonl(layout->fw_max_len);
704 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
705 hdr->kernel_len = htonl(kernel_len);
707 hdr->rootfs_ofs = htonl(rootfs_ofs);
708 hdr->rootfs_len = htonl(rootfs_info.file_size);
711 hdr->ver_hi = htons(fw_ver_hi);
712 hdr->ver_mid = htons(fw_ver_mid);
713 hdr->ver_lo = htons(fw_ver_lo);
715 get_md5(buf, len, hdr->md5sum1);
718 static int pad_jffs2(char *buf, int currlen)
724 pad_mask = (64 * 1024);
725 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
729 for (i = 10; i < 32; i++) {
735 len = ALIGN(len, mask);
737 for (i = 10; i < 32; i++) {
739 if ((len & (mask - 1)) == 0)
743 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
744 buf[len + i] = jffs2_eof_mark[i];
746 len += sizeof(jffs2_eof_mark);
752 static int write_fw(char *data, int len)
755 int ret = EXIT_FAILURE;
757 f = fopen(ofname, "w");
759 ERRS("could not open \"%s\" for writing", ofname);
764 fwrite(data, len, 1, f);
766 ERRS("unable to write output file");
770 DBG("firmware file \"%s\" completed", ofname);
777 if (ret != EXIT_SUCCESS) {
784 static int build_fw(void)
789 int ret = EXIT_FAILURE;
792 buflen = layout->fw_max_len;
794 buf = malloc(buflen);
796 ERR("no memory for buffer\n");
800 memset(buf, 0xff, buflen);
801 p = buf + sizeof(struct fw_header);
802 ret = read_to_buf(&kernel_info, p);
806 writelen = sizeof(struct fw_header) + kernel_len;
812 p = buf + rootfs_ofs;
814 ret = read_to_buf(&rootfs_info, p);
819 writelen += rootfs_info.file_size;
821 writelen = rootfs_ofs + rootfs_info.file_size;
824 writelen = pad_jffs2(buf, writelen);
830 fill_header(buf, writelen);
831 ret = write_fw(buf, writelen);
843 /* Helper functions to inspect_fw() representing different output formats */
844 static inline void inspect_fw_pstr(char *label, char *str)
846 printf("%-23s: %s\n", label, str);
849 static inline void inspect_fw_phex(char *label, uint32_t val)
851 printf("%-23s: 0x%08x\n", label, val);
854 static inline void inspect_fw_phexpost(char *label,
855 uint32_t val, char *post)
857 printf("%-23s: 0x%08x (%s)\n", label, val, post);
860 static inline void inspect_fw_phexdef(char *label,
861 uint32_t val, uint32_t defval)
863 printf("%-23s: 0x%08x ", label, val);
866 printf("(== OpenWrt default)\n");
868 printf("(OpenWrt default: 0x%08x)\n", defval);
871 static inline void inspect_fw_phexexp(char *label,
872 uint32_t val, uint32_t expval)
874 printf("%-23s: 0x%08x ", label, val);
879 printf("(expected: 0x%08x)\n", expval);
882 static inline void inspect_fw_phexdec(char *label, uint32_t val)
884 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
887 static inline void inspect_fw_phexdecdef(char *label,
888 uint32_t val, uint32_t defval)
890 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
893 printf("(== OpenWrt default)\n");
895 printf("(OpenWrt default: 0x%08x)\n", defval);
898 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
902 printf("%-23s:", label);
903 for (i=0; i<MD5SUM_LEN; i++)
904 printf(" %02x", val[i]);
905 printf(" %s\n", text);
908 static int inspect_fw(void)
911 struct fw_header *hdr;
912 uint8_t md5sum[MD5SUM_LEN];
913 struct board_info *board;
914 int ret = EXIT_FAILURE;
916 buf = malloc(inspect_info.file_size);
918 ERR("no memory for buffer!\n");
922 ret = read_to_buf(&inspect_info, buf);
925 hdr = (struct fw_header *)buf;
927 inspect_fw_pstr("File name", inspect_info.file_name);
928 inspect_fw_phexdec("File size", inspect_info.file_size);
930 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
931 ERR("file does not seem to have V1 header!\n");
935 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
937 if (ntohl(hdr->unk1) != 0)
938 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
940 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
941 if (ntohl(hdr->boot_len) == 0)
942 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
944 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
945 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
947 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
948 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
949 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
951 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
953 if (ntohl(hdr->unk2) != 0)
954 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
955 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
956 "(purpose yet unknown, unchecked here)");
957 if (ntohl(hdr->unk3) != 0)
958 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
962 inspect_fw_pstr("Vendor name", hdr->vendor_name);
963 inspect_fw_pstr("Firmware version", hdr->fw_version);
964 board = find_board_by_hwid(ntohl(hdr->hw_id));
966 layout = find_layout(board->layout_id);
967 inspect_fw_phexpost("Hardware ID",
968 ntohl(hdr->hw_id), board->id);
969 inspect_fw_phexexp("Hardware Revision",
970 ntohl(hdr->hw_rev), board->hw_rev);
972 inspect_fw_phexpost("Hardware ID",
973 ntohl(hdr->hw_id), "unknown");
974 inspect_fw_phex("Hardware Revision",
980 inspect_fw_phexdec("Kernel data offset",
981 ntohl(hdr->kernel_ofs));
982 inspect_fw_phexdec("Kernel data length",
983 ntohl(hdr->kernel_len));
985 inspect_fw_phexdef("Kernel load address",
986 ntohl(hdr->kernel_la),
987 layout ? layout->kernel_la : 0xffffffff);
988 inspect_fw_phexdef("Kernel entry point",
989 ntohl(hdr->kernel_ep),
990 layout ? layout->kernel_ep : 0xffffffff);
991 inspect_fw_phexdecdef("Rootfs data offset",
992 ntohl(hdr->rootfs_ofs),
993 layout ? layout->rootfs_ofs : 0xffffffff);
995 inspect_fw_phex("Kernel load address",
996 ntohl(hdr->kernel_la));
997 inspect_fw_phex("Kernel entry point",
998 ntohl(hdr->kernel_ep));
999 inspect_fw_phexdec("Rootfs data offset",
1000 ntohl(hdr->rootfs_ofs));
1002 inspect_fw_phexdec("Rootfs data length",
1003 ntohl(hdr->rootfs_len));
1004 inspect_fw_phexdec("Boot loader data offset",
1005 ntohl(hdr->boot_ofs));
1006 inspect_fw_phexdec("Boot loader data length",
1007 ntohl(hdr->boot_len));
1008 inspect_fw_phexdec("Total firmware length",
1009 ntohl(hdr->fw_length));
1017 filename = malloc(strlen(inspect_info.file_name) + 8);
1018 sprintf(filename, "%s-kernel", inspect_info.file_name);
1019 printf("Extracting kernel to \"%s\"...\n", filename);
1020 fp = fopen(filename, "w");
1022 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
1023 ntohl(hdr->kernel_len), 1, fp)) {
1024 ERR("error in fwrite(): %s", strerror(errno));
1028 ERR("error in fopen(): %s", strerror(errno));
1032 filename = malloc(strlen(inspect_info.file_name) + 8);
1033 sprintf(filename, "%s-rootfs", inspect_info.file_name);
1034 printf("Extracting rootfs to \"%s\"...\n", filename);
1035 fp = fopen(filename, "w");
1037 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
1038 ntohl(hdr->rootfs_len), 1, fp)) {
1039 ERR("error in fwrite(): %s", strerror(errno));
1043 ERR("error in fopen(): %s", strerror(errno));
1054 int main(int argc, char *argv[])
1056 int ret = EXIT_FAILURE;
1061 progname = basename(argv[0]);
1066 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xX:hsjv:");
1072 sscanf(optarg, "0x%x", &rootfs_align);
1081 sscanf(optarg, "0x%x", &kernel_ep);
1087 opt_hw_rev = optarg;
1090 sscanf(optarg, "0x%x", &kernel_la);
1105 kernel_info.file_name = optarg;
1108 rootfs_info.file_name = optarg;
1111 sscanf(optarg, "0x%x", &rootfs_ofs);
1120 inspect_info.file_name = optarg;
1129 usage(EXIT_SUCCESS);
1132 sscanf(optarg, "0x%x", &reserved_space);
1135 usage(EXIT_FAILURE);
1140 ret = check_options();
1144 if (!inspect_info.file_name)