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