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