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