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