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