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