9eb226fba83f25894fbe73185ee66557a8529498
[openwrt/staging/lynxis.git] / tools / firmware-utils / src / mktplinkfw2.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 <stdbool.h>
24 #include <endian.h>
25 #include <sys/stat.h>
26
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
29
30 #include "md5.h"
31 #include "mktplinkfw-lib.h"
32
33 struct fw_header {
34 uint32_t version; /* 0x00: header version */
35 char fw_version[48]; /* 0x04: fw version string */
36 uint32_t hw_id; /* 0x34: hardware id */
37 uint32_t hw_rev; /* 0x38: FIXME: hardware revision? */
38 uint32_t hw_ver_add; /* 0x3c: additional hardware version */
39 uint8_t md5sum1[MD5SUM_LEN]; /* 0x40 */
40 uint32_t unk2; /* 0x50: 0x00000000 */
41 uint8_t md5sum2[MD5SUM_LEN]; /* 0x54 */
42 uint32_t unk3; /* 0x64: 0xffffffff */
43
44 uint32_t kernel_la; /* 0x68: kernel load address */
45 uint32_t kernel_ep; /* 0x6c: kernel entry point */
46 uint32_t fw_length; /* 0x70: total length of the image */
47 uint32_t kernel_ofs; /* 0x74: kernel data offset */
48 uint32_t kernel_len; /* 0x78: kernel data length */
49 uint32_t rootfs_ofs; /* 0x7c: rootfs data offset */
50 uint32_t rootfs_len; /* 0x80: rootfs data length */
51 uint32_t boot_ofs; /* 0x84: bootloader offset */
52 uint32_t boot_len; /* 0x88: bootloader length */
53 uint16_t unk4; /* 0x8c: 0x55aa */
54 uint8_t sver_hi; /* 0x8e */
55 uint8_t sver_lo; /* 0x8f */
56 uint8_t unk5; /* 0x90: magic: 0xa5 */
57 uint8_t ver_hi; /* 0x91 */
58 uint8_t ver_mid; /* 0x92 */
59 uint8_t ver_lo; /* 0x93 */
60 uint8_t pad[364];
61 } __attribute__ ((packed));
62
63 #define FLAG_LE_KERNEL_LA_EP 0x00000001 /* Little-endian used for kernel load address & entry point */
64
65 struct board_info {
66 char *id;
67 uint32_t hw_id;
68 uint32_t hw_rev;
69 uint32_t hw_ver_add;
70 char *layout_id;
71 uint32_t hdr_ver;
72 uint32_t flags;
73 };
74
75 /*
76 * Globals
77 */
78 char *ofname;
79 char *progname;
80 static char *vendor = "TP-LINK Technologies";
81 static char *version = "ver. 1.0";
82 static char *fw_ver = "0.0.0";
83 static char *sver = "1.0";
84 static uint32_t hdr_ver = 2;
85
86 static struct board_info custom_board;
87
88 static struct board_info *board;
89 static char *layout_id;
90 struct flash_layout *layout;
91 static char *opt_hw_id;
92 static char *opt_hw_rev;
93 static char *opt_hw_ver_add;
94 static int fw_ver_lo;
95 static int fw_ver_mid;
96 static int fw_ver_hi;
97 static int sver_lo;
98 static int sver_hi;
99 struct file_info kernel_info;
100 static uint32_t kernel_la = 0;
101 static uint32_t kernel_ep = 0;
102 uint32_t kernel_len = 0;
103 struct file_info rootfs_info;
104 uint32_t rootfs_ofs = 0;
105 uint32_t rootfs_align;
106 static struct file_info boot_info = { 0 };
107 int combined;
108 int strip_padding;
109 int add_jffs2_eof;
110
111 static struct file_info inspect_info;
112 static int extract = 0;
113
114 char md5salt_normal[MD5SUM_LEN] = {
115 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
116 0xdc, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x37,
117 };
118
119 char md5salt_boot[MD5SUM_LEN] = {
120 0x8c, 0xef, 0x33, 0x5f, 0xd5, 0xc5, 0xce, 0xfa,
121 0xac, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
122 };
123
124 static struct flash_layout layouts[] = {
125 {
126 .id = "4Mmtk",
127 .fw_max_len = 0x3d0000,
128 .kernel_la = 0x80000000,
129 .kernel_ep = 0x80000000,
130 .rootfs_ofs = 0x140000,
131 }, {
132 .id = "8Mltq",
133 .fw_max_len = 0x7a0000,
134 .kernel_la = 0x80002000,
135 .kernel_ep = 0x80002000,
136 .rootfs_ofs = 0x140000,
137 }, {
138 .id = "16Mltq",
139 .fw_max_len = 0xf90000,
140 .kernel_la = 0x80002000,
141 .kernel_ep = 0x800061b0,
142 .rootfs_ofs = 0x140000,
143 }, {
144 .id = "8Mmtk",
145 .fw_max_len = 0x7a0000,
146 .kernel_la = 0x80000000,
147 .kernel_ep = 0x80000000,
148 .rootfs_ofs = 0x140000,
149 }, {
150 .id = "8MSUmtk", /* Split U-Boot OS */
151 .fw_max_len = 0x770000,
152 .kernel_la = 0x80000000,
153 .kernel_ep = 0x80000000,
154 .rootfs_ofs = 0x140000,
155 }, {
156 .id = "8MLmtk",
157 .fw_max_len = 0x7b0000,
158 .kernel_la = 0x80000000,
159 .kernel_ep = 0x80000000,
160 .rootfs_ofs = 0x140000,
161 }, {
162 .id = "8Mqca",
163 .fw_max_len = 0x7a0000,
164 .kernel_la = 0x80060000,
165 .kernel_ep = 0x80060000,
166 .rootfs_ofs = 0x140000,
167 }, {
168 .id = "16Mqca",
169 .fw_max_len = 0xf90000,
170 .kernel_la = 0x80060000,
171 .kernel_ep = 0x80060000,
172 .rootfs_ofs = 0x140000,
173 }, {
174 /* terminating entry */
175 }
176 };
177
178 static void usage(int status)
179 {
180 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
181 struct board_info *board;
182
183 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
184 fprintf(stream,
185 "\n"
186 "Options:\n"
187 " -c use combined kernel image\n"
188 " -e swap endianness in kernel load address and entry point\n"
189 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
190 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
191 " -H <hwid> use hardware id specified with <hwid>\n"
192 " -W <hwrev> use hardware revision specified with <hwrev>\n"
193 " -w <hwveradd> use additional hardware version specified with <hwveradd>\n"
194 " -F <id> use flash layout specified with <id>\n"
195 " -k <file> read kernel image from the file <file>\n"
196 " -r <file> read rootfs image from the file <file>\n"
197 " -b <file> read bootloader image from the file <file>\n"
198 " -a <align> align the rootfs start on an <align> bytes boundary\n"
199 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
200 " -o <file> write output to the file <file>\n"
201 " -s strip padding from the end of the image\n"
202 " -j add jffs2 end-of-filesystem markers\n"
203 " -N <vendor> set image vendor to <vendor>\n"
204 " -T <version> set header version to <version>\n"
205 " -V <version> set image version to <version>\n"
206 " -v <version> set firmware version to <version>\n"
207 " -y <version> set secondary version to <version>\n"
208 " -i <file> inspect given firmware file <file>\n"
209 " -x extract bootloader, kernel and rootfs while inspecting (requires -i)\n"
210 " -h show this screen\n"
211 );
212
213 exit(status);
214 }
215
216 static int check_options(void)
217 {
218 int ret;
219
220 if (inspect_info.file_name) {
221 ret = get_file_stat(&inspect_info);
222 if (ret)
223 return ret;
224
225 return 0;
226 } else if (extract) {
227 ERR("no firmware for inspection specified");
228 return -1;
229 }
230
231 if (opt_hw_id == NULL) {
232 ERR("hardware id must be specified");
233 return -1;
234 }
235
236 board = &custom_board;
237
238 if (layout_id == NULL) {
239 ERR("flash layout is not specified");
240 return -1;
241 }
242
243 board->hw_id = strtoul(opt_hw_id, NULL, 0);
244
245 board->hw_rev = 1;
246 board->hw_ver_add = 0;
247
248 if (opt_hw_rev)
249 board->hw_rev = strtoul(opt_hw_rev, NULL, 0);
250 if (opt_hw_ver_add)
251 board->hw_ver_add = strtoul(opt_hw_ver_add, NULL, 0);
252
253 layout = find_layout(layouts, layout_id);
254 if (layout == NULL) {
255 ERR("unknown flash layout \"%s\"", layout_id);
256 return -1;
257 }
258
259 if (!kernel_la)
260 kernel_la = layout->kernel_la;
261 if (!kernel_ep)
262 kernel_ep = layout->kernel_ep;
263 if (!rootfs_ofs)
264 rootfs_ofs = layout->rootfs_ofs;
265
266 /* check bootloader */
267 ret = get_file_stat(&boot_info);
268 if (ret) {
269 ERR("Can not load bootloader image.");
270 return ret;
271 }
272
273 if (boot_info.file_size > 2 * 64 * 1024) {
274 /* the offset in fill_header is hardcoded to 128k */
275 ERR("Bootloader image is bigger than 128k.");
276 return -1;
277 }
278
279 if (kernel_info.file_name == NULL) {
280 ERR("no kernel image specified");
281 return -1;
282 }
283 ret = get_file_stat(&kernel_info);
284 if (ret)
285 return ret;
286
287 kernel_len = kernel_info.file_size;
288
289 if (combined) {
290 if (kernel_info.file_size >
291 layout->fw_max_len - sizeof(struct fw_header)) {
292 ERR("kernel image is too big");
293 return -1;
294 }
295 } else {
296 if (rootfs_info.file_name == NULL) {
297 ERR("no rootfs image specified");
298 return -1;
299 }
300
301 ret = get_file_stat(&rootfs_info);
302 if (ret)
303 return ret;
304
305 if (rootfs_align) {
306 kernel_len += sizeof(struct fw_header);
307 rootfs_ofs = ALIGN(kernel_len, rootfs_align);
308 kernel_len -= sizeof(struct fw_header);
309
310 DBG("rootfs offset aligned to 0x%u", rootfs_ofs);
311
312 if (kernel_len + rootfs_info.file_size >
313 layout->fw_max_len - sizeof(struct fw_header)) {
314 ERR("images are too big");
315 return -1;
316 }
317 } else {
318 if (kernel_info.file_size >
319 rootfs_ofs - sizeof(struct fw_header)) {
320 ERR("kernel image is too big");
321 return -1;
322 }
323
324 if (rootfs_info.file_size >
325 (layout->fw_max_len - rootfs_ofs)) {
326 ERR("rootfs image is too big");
327 return -1;
328 }
329 }
330 }
331
332 if (ofname == NULL) {
333 ERR("no output file specified");
334 return -1;
335 }
336
337 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
338 if (ret != 3) {
339 ERR("invalid firmware version '%s'", fw_ver);
340 return -1;
341 }
342
343 ret = sscanf(sver, "%d.%d", &sver_hi, &sver_lo);
344 if (ret != 2) {
345 ERR("invalid secondary version '%s'", sver);
346 return -1;
347 }
348
349 return 0;
350 }
351
352 void _fill_header(char *buf, int len, int with_bootloader)
353 {
354 struct fw_header *hdr = (struct fw_header *)buf;
355 unsigned ver_len;
356 unsigned int offset = 0;
357
358 if (with_bootloader) {
359 /* ensure the bootloader is padded to 64k */
360 offset = boot_info.file_size & (64 * 1024);
361 if (offset < boot_info.file_size)
362 offset += 64 * 1024;
363
364 offset += sizeof(struct fw_header);
365 }
366
367 memset(hdr, '\xff', sizeof(struct fw_header));
368
369 hdr->version = htonl(bswap_32(hdr_ver));
370 ver_len = strlen(version);
371 if (ver_len > (sizeof(hdr->fw_version) - 1))
372 ver_len = sizeof(hdr->fw_version) - 1;
373
374 memcpy(hdr->fw_version, version, ver_len);
375 hdr->fw_version[ver_len] = 0;
376
377 hdr->hw_id = htonl(board->hw_id);
378 hdr->hw_rev = htonl(board->hw_rev);
379 hdr->hw_ver_add = htonl(board->hw_ver_add);
380
381 if (boot_info.file_size == 0 || !with_bootloader) {
382 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
383 hdr->boot_ofs = htonl(0);
384 hdr->boot_len = htonl(0);
385 } else {
386 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
387 hdr->boot_ofs = htonl(0);
388 hdr->boot_len = htonl(boot_info.file_size);
389 }
390
391 hdr->kernel_la = htonl(kernel_la);
392 hdr->kernel_ep = htonl(kernel_ep);
393 hdr->fw_length = htonl(layout->fw_max_len + offset);
394 hdr->kernel_ofs = htonl(sizeof(struct fw_header) + offset);
395 hdr->kernel_len = htonl(kernel_len);
396 if (!combined) {
397 /* even the bootloader doesnt increased the root_ofs. Unsure if this parser
398 * always ignores the rootfs or only in the 841v13
399 */
400 hdr->rootfs_ofs = htonl(rootfs_ofs);
401 hdr->rootfs_len = htonl(rootfs_info.file_size);
402 }
403
404 hdr->unk2 = htonl(0);
405 hdr->unk3 = htonl(0xffffffff);
406 hdr->unk4 = htons(0x55aa);
407 hdr->unk5 = 0xa5;
408
409 hdr->sver_hi = sver_hi;
410 hdr->sver_lo = sver_lo;
411
412 hdr->ver_hi = fw_ver_hi;
413 hdr->ver_mid = fw_ver_mid;
414 hdr->ver_lo = fw_ver_lo;
415
416 if (board->flags & FLAG_LE_KERNEL_LA_EP) {
417 hdr->kernel_la = bswap_32(hdr->kernel_la);
418 hdr->kernel_ep = bswap_32(hdr->kernel_ep);
419 }
420
421 get_md5(buf, len, hdr->md5sum1);
422 }
423
424 /* fill_header get called by mktplinkfw_lib to fill the header in front of the kernel. */
425 void fill_header(char *buf, int len) {
426 _fill_header(buf, len, 0);
427 }
428
429 static int inspect_fw(void)
430 {
431 char *buf;
432 struct fw_header *hdr;
433 uint8_t md5sum[MD5SUM_LEN];
434 struct board_info *board;
435 int ret = EXIT_FAILURE;
436
437 buf = malloc(inspect_info.file_size);
438 if (!buf) {
439 ERR("no memory for buffer!\n");
440 goto out;
441 }
442
443 ret = read_to_buf(&inspect_info, buf);
444 if (ret)
445 goto out_free_buf;
446 hdr = (struct fw_header *)buf;
447
448 board = &custom_board;
449
450 if (board->flags & FLAG_LE_KERNEL_LA_EP) {
451 hdr->kernel_la = bswap_32(hdr->kernel_la);
452 hdr->kernel_ep = bswap_32(hdr->kernel_ep);
453 }
454
455 inspect_fw_pstr("File name", inspect_info.file_name);
456 inspect_fw_phexdec("File size", inspect_info.file_size);
457
458 switch(bswap_32(ntohl(hdr->version))) {
459 case 2:
460 case 3:
461 break;
462 default:
463 ERR("file does not seem to have V2/V3 header!\n");
464 goto out_free_buf;
465 }
466
467 inspect_fw_phexdec("Version 2 Header size", sizeof(struct fw_header));
468
469 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
470 if (ntohl(hdr->boot_len) == 0)
471 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
472 else
473 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
474 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
475
476 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
477 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
478 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
479 } else {
480 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
481 }
482 if (ntohl(hdr->unk2) != 0)
483 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
484 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
485 "(purpose yet unknown, unchecked here)");
486
487 if (ntohl(hdr->unk3) != 0xffffffff)
488 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
489
490 if (ntohs(hdr->unk4) != 0x55aa)
491 inspect_fw_phexdec("Unknown value 4", hdr->unk4);
492
493 if (hdr->unk5 != 0xa5)
494 inspect_fw_phexdec("Unknown value 5", hdr->unk5);
495
496 printf("\n");
497
498 inspect_fw_pstr("Firmware version", hdr->fw_version);
499 inspect_fw_phex("Hardware ID", ntohl(hdr->hw_id));
500 inspect_fw_phex("Hardware Revision",
501 ntohl(hdr->hw_rev));
502 inspect_fw_phex("Additional HW Version",
503 ntohl(hdr->hw_ver_add));
504
505 printf("%-23s: %d.%d.%d-%d.%d\n", "Software version",
506 hdr->ver_hi, hdr->ver_mid, hdr->ver_lo,
507 hdr->sver_hi, hdr->sver_lo);
508
509 printf("\n");
510
511 inspect_fw_phexdec("Kernel data offset",
512 ntohl(hdr->kernel_ofs));
513 inspect_fw_phexdec("Kernel data length",
514 ntohl(hdr->kernel_len));
515 inspect_fw_phex("Kernel load address",
516 ntohl(hdr->kernel_la));
517 inspect_fw_phex("Kernel entry point",
518 ntohl(hdr->kernel_ep));
519 inspect_fw_phexdec("Rootfs data offset",
520 ntohl(hdr->rootfs_ofs));
521 inspect_fw_phexdec("Rootfs data length",
522 ntohl(hdr->rootfs_len));
523 inspect_fw_phexdec("Boot loader data offset",
524 ntohl(hdr->boot_ofs));
525 inspect_fw_phexdec("Boot loader data length",
526 ntohl(hdr->boot_len));
527 inspect_fw_phexdec("Total firmware length",
528 ntohl(hdr->fw_length));
529
530 if (extract) {
531 FILE *fp;
532 char *filename;
533
534 printf("\n");
535
536 if (hdr->boot_len) {
537 filename = malloc(strlen(inspect_info.file_name) + 8);
538 sprintf(filename, "%s-bootloader", inspect_info.file_name);
539 printf("Extracting bootloader to \"%s\"...\n", filename);
540 fp = fopen(filename, "w");
541 if (fp) {
542 if (!fwrite(buf + sizeof(struct fw_header) + ntohl(hdr->boot_ofs),
543 ntohl(hdr->boot_len), 1, fp)) {
544 ERR("error in fwrite(): %s", strerror(errno));
545 }
546 fclose(fp);
547 } else {
548 ERR("error in fopen(): %s", strerror(errno));
549 }
550 free(filename);
551 }
552
553 filename = malloc(strlen(inspect_info.file_name) + 8);
554 sprintf(filename, "%s-kernel", inspect_info.file_name);
555 printf("Extracting kernel to \"%s\"...\n", filename);
556 fp = fopen(filename, "w");
557 if (fp) {
558 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
559 ntohl(hdr->kernel_len), 1, fp)) {
560 ERR("error in fwrite(): %s", strerror(errno));
561 }
562 fclose(fp);
563 } else {
564 ERR("error in fopen(): %s", strerror(errno));
565 }
566 free(filename);
567
568 filename = malloc(strlen(inspect_info.file_name) + 8);
569 sprintf(filename, "%s-rootfs", inspect_info.file_name);
570 printf("Extracting rootfs to \"%s\"...\n", filename);
571 fp = fopen(filename, "w");
572 if (fp) {
573 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
574 ntohl(hdr->rootfs_len), 1, fp)) {
575 ERR("error in fwrite(): %s", strerror(errno));
576 }
577 fclose(fp);
578 } else {
579 ERR("error in fopen(): %s", strerror(errno));
580 }
581 free(filename);
582 }
583
584 out_free_buf:
585 free(buf);
586 out:
587 return ret;
588 }
589
590 /* prepend a second image header and the bootloader.
591 *
592 * |------------|
593 * |image header|
594 * |------------|
595 * |bootloader |
596 * |------------|
597 * |optional pad|
598 * |------------|
599 * |image header|
600 * |------------|
601 * |kernel image|
602 * |------------|
603 * |rootfs image|
604 * |------------|
605 *
606 * The padding is optional. The second image header should begin a 64k boundary.
607 */
608 int prepend_bootloader() {
609 unsigned int buflen = 0;
610 int ret = 0, bootloader_padded = 0;
611 char *buf = 0, *p = 0;
612 struct file_info image;
613
614 /* calculate blocks to ensure padding */
615 bootloader_padded = boot_info.file_size & (64 * 1024);
616 if (bootloader_padded < boot_info.file_size)
617 bootloader_padded += 64 * 1024;
618
619 image.file_name = ofname;
620 ret = get_file_stat(&image);
621 if (ret) {
622 ERR("Can not load the output image");
623 return ret;
624 }
625
626 buflen = image.file_size + bootloader_padded + sizeof(struct fw_header);
627 buf = malloc(buflen);
628 if (buf == NULL) {
629 ERR("Can not allocate buffer %d bytes", buflen);
630 return -1;
631 }
632 memset(buf, 0xff, buflen);
633
634 /* load old image */
635 p = buf + bootloader_padded + sizeof(struct fw_header);
636 ret = read_to_buf(&image, p);
637 if (ret) {
638 ERR("Can not read output image");
639 goto out_free_buf;
640 }
641
642 p = buf + sizeof(struct fw_header);
643 ret = read_to_buf(&boot_info, p);
644
645 _fill_header(buf, buflen, 1);
646
647 ret = write_fw(ofname, buf, buflen);
648 if (ret)
649 goto out_free_buf;
650
651 ret = EXIT_SUCCESS;
652
653 out_free_buf:
654 free(buf);
655
656 return ret;
657 }
658
659 int main(int argc, char *argv[])
660 {
661 int ret = EXIT_FAILURE;
662
663 progname = basename(argv[0]);
664
665 while ( 1 ) {
666 int c;
667
668 c = getopt(argc, argv, "a:b:H:E:F:L:V:N:W:w:ci:k:r:R:o:xhsjv:y:T:e");
669 if (c == -1)
670 break;
671
672 switch (c) {
673 case 'a':
674 sscanf(optarg, "0x%x", &rootfs_align);
675 break;
676 case 'b':
677 boot_info.file_name = optarg;
678 break;
679 case 'H':
680 opt_hw_id = optarg;
681 break;
682 case 'E':
683 sscanf(optarg, "0x%x", &kernel_ep);
684 break;
685 case 'F':
686 layout_id = optarg;
687 break;
688 case 'W':
689 opt_hw_rev = optarg;
690 break;
691 case 'w':
692 opt_hw_ver_add = optarg;
693 break;
694 case 'L':
695 sscanf(optarg, "0x%x", &kernel_la);
696 break;
697 case 'V':
698 version = optarg;
699 break;
700 case 'v':
701 fw_ver = optarg;
702 break;
703 case 'y':
704 sver = optarg;
705 break;
706 case 'N':
707 vendor = optarg;
708 break;
709 case 'c':
710 combined++;
711 break;
712 case 'k':
713 kernel_info.file_name = optarg;
714 break;
715 case 'r':
716 rootfs_info.file_name = optarg;
717 break;
718 case 'R':
719 sscanf(optarg, "0x%x", &rootfs_ofs);
720 break;
721 case 'o':
722 ofname = optarg;
723 break;
724 case 's':
725 strip_padding = 1;
726 break;
727 case 'i':
728 inspect_info.file_name = optarg;
729 break;
730 case 'j':
731 add_jffs2_eof = 1;
732 break;
733 case 'x':
734 extract = 1;
735 break;
736 case 'T':
737 hdr_ver = atoi(optarg);
738 break;
739 case 'e':
740 custom_board.flags = FLAG_LE_KERNEL_LA_EP;
741 break;
742 case 'h':
743 usage(EXIT_SUCCESS);
744 break;
745 default:
746 usage(EXIT_FAILURE);
747 break;
748 }
749 }
750
751 ret = check_options();
752 if (ret)
753 goto out;
754
755 if (!inspect_info.file_name) {
756 ret = build_fw(sizeof(struct fw_header));
757 if (ret == 0 && boot_info.file_size > 0)
758 ret = prepend_bootloader();
759 }
760 else
761 ret = inspect_fw();
762
763 out:
764 return ret;
765 }