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