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