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