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