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