4 * Copyright (C) 2006,2007 Gabor Juhos <juhosg@freemail.hu>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
27 #include <unistd.h> /* for unlink() */
29 #include <getopt.h> /* for getopt() */
33 #include <endian.h> /* for __BYTE_ORDER */
35 #if defined(__CYGWIN__)
36 # include <byteswap.h>
39 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
40 # define HOST_TO_LE16(x) (x)
41 # define HOST_TO_LE32(x) (x)
43 # define HOST_TO_LE16(x) bswap_16(x)
44 # define HOST_TO_LE32(x) bswap_32(x)
49 #define MAX_FW_BLOCKS 32
50 #define MAX_ARG_COUNT 32
51 #define MAX_ARG_LEN 1024
52 #define FILE_BUF_LEN (16*1024)
56 uint32_t blocklen; /* length of the block */
59 char *name; /* name of the file */
60 uint32_t size; /* length of the file */
61 uint32_t crc; /* crc value of the file */
64 #define BLOCK_FLAG_HAVEHDR 0x0001
67 char *model; /* model number*/
68 char *name; /* model name*/
69 char *desc; /* description */
70 uint16_t vid; /* vendor id */
71 uint16_t did; /* device id */
72 uint16_t svid; /* sub vendor id */
73 uint16_t sdid; /* sub device id */
74 uint32_t flash_size; /* size of flash */
77 #define BOARD(_vid, _did, _svid, _sdid, _flash, _mod, _name, _desc) { \
78 .model = _mod, .name = _name, .desc = _desc, \
79 .vid = _vid, .did = _did, .svid = _svid, .sdid = _sdid, \
80 .flash_size = (_flash << 20) }
82 #define CPX_BOARD(_did, _flash, _mod, _name, _desc) \
83 BOARD(VENID_COMPEX, _did, VENID_COMPEX, _did, _flash, _mod, _name, _desc)
85 #define ALIGN(x,y) ((x)+((y)-1)) & ~((y)-1)
90 uint32_t flash_size = 0;
91 int fw_num_partitions = 0;
92 int fw_num_blocks = 0;
95 struct mylo_fw_header fw_header;
96 struct mylo_partition fw_partitions[MYLO_MAX_PARTITIONS];
97 struct fw_block fw_blocks[MAX_FW_BLOCKS];
99 struct cpx_board boards[] = {
100 CPX_BOARD(DEVID_COMPEX_NP18A, 4,
101 "NP18A", "Compex NetPassage 18A",
102 "Dualband Wireless A+G Internet Gateway"),
103 CPX_BOARD(DEVID_COMPEX_NP26G8M, 2,
104 "NP26G8M", "Compex NetPassage 26G (8M)",
105 "Wireless-G Broadband Multimedia Gateway"),
106 CPX_BOARD(DEVID_COMPEX_NP26G16M, 4,
107 "NP26G16M", "Compex NetPassage 26G (16M)",
108 "Wireless-G Broadband Multimedia Gateway"),
109 CPX_BOARD(DEVID_COMPEX_NP27G, 4,
110 "NP27G", "Compex NetPassage 27G",
111 "Wireless-G 54Mbps eXtended Range Router"),
112 CPX_BOARD(DEVID_COMPEX_NP28G, 4,
113 "NP28G", "Compex NetPassage 28G",
114 "Wireless 108Mbps Super-G XR Multimedia Router with 4 USB Ports"),
115 CPX_BOARD(DEVID_COMPEX_NP28GHS, 4,
116 "NP28GHS", "Compex NetPassage 28G (HotSpot)",
118 CPX_BOARD(DEVID_COMPEX_WP18, 4,
119 "WP18", "Compex NetPassage WP18",
120 "Wireless-G 54Mbps A+G Dualband Access Point"),
121 CPX_BOARD(DEVID_COMPEX_WP54G, 4,
122 "WP54G", "Compex WP54G",
123 "Wireless-G 54Mbps XR Access Point"),
124 CPX_BOARD(DEVID_COMPEX_WP54Gv1C, 2,
125 "WP54Gv1C", "Compex WP54G rev.1C",
126 "Wireless-G 54Mbps XR Access Point"),
127 CPX_BOARD(DEVID_COMPEX_WP54AG, 4,
128 "WP54AG", "Compex WP54AG",
129 "Wireless-AG 54Mbps XR Access Point"),
130 CPX_BOARD(DEVID_COMPEX_WPP54G, 4,
131 "WPP54G", "Compex WPP54G",
132 "Outdoor Access Point"),
133 CPX_BOARD(DEVID_COMPEX_WPP54AG, 4,
134 "WPP54AG", "Compex WPP54AG",
135 "Outdoor Access Point"),
140 errmsgv(int syserr, const char *fmt, va_list arg_ptr)
145 fprintf(stderr, "[%s] Error: ", progname);
146 vfprintf(stderr, fmt, arg_ptr);
148 fprintf(stderr, ": %s", strerror(save));
150 fprintf(stderr, "\n");
154 errmsg(int syserr, const char *fmt, ...)
157 va_start(arg_ptr, fmt);
158 errmsgv(syserr, fmt, arg_ptr);
163 dbgmsg(int level, const char *fmt, ...)
166 if (verblevel >= level) {
168 va_start(arg_ptr, fmt);
169 vfprintf(stderr, fmt, arg_ptr);
170 fprintf(stderr, "\n");
179 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
180 struct cpx_board *board;
182 fprintf(stream, "Usage: %s [OPTION...] <file>\n", progname);
185 " <file> write output to the <file>\n"
188 " -B <board> create firmware for the board specified with <board>.\n"
189 " This option set vendor id, device id, subvendor id,\n"
190 " subdevice id, and flash size options to the right value.\n"
191 " valid <board> values:\n");
192 for (board = boards; board->model != NULL; board++){
195 board->model, board->name);
198 " -i <vid>:<did>[:<svid>[:<sdid>]]\n"
199 " create firmware for board with vendor id <vid>, device\n"
200 " id <did>, subvendor id <svid> and subdevice id <sdid>.\n"
201 " -r <rev> set board revision to <rev>.\n"
202 " -s <size> set flash size to <size>\n"
203 " -b <addr>:<len>[:[<flags>]:<file>]\n"
204 " define block at <addr> with length of <len>.\n"
205 " valid <flag> values:\n"
206 " h : add crc header before the file data.\n"
207 " -p <addr>:<len>[:<flags>[:<param>[:<file>]]]\n"
208 " add partition at <addr>, with size of <len> to the\n"
209 " partition table, set partition flags to <flags> and\n"
210 " partition parameter to <param>. If the <file> is specified\n"
211 " content of the file is also added to the firmware image.\n"
212 " valid <flag> values:\n"
213 " a: this is the active partition. The bootloader loads\n"
214 " the firmware from this partition.\n"
215 " h: the partition data have a header.\n"
216 " p: the bootloader loads data from this partition to\n"
217 " the RAM before decompress it.\n"
218 " -h show this screen\n"
225 * Code to compute the CRC-32 table. Borrowed from
226 * gzip-1.0.3/makecrc.c.
229 static uint32_t crc_32_tab[256];
234 /* Not copyrighted 1990 Mark Adler */
236 uint32_t c; /* crc shift register */
237 uint32_t e; /* polynomial exclusive-or pattern */
238 int i; /* counter for all possible eight bit values */
239 int k; /* byte being shifted into crc apparatus */
241 /* terms of polynomial defining this crc (except x^32): */
242 static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
244 /* Make exclusive-or pattern from polynomial */
246 for (i = 0; i < sizeof(p)/sizeof(int); i++)
247 e |= 1L << (31 - p[i]);
251 for (i = 1; i < 256; i++) {
253 for (k = i | 256; k != 1; k >>= 1) {
254 c = c & 1 ? (c >> 1) ^ e : c >> 1;
264 update_crc(uint8_t *p, uint32_t len, uint32_t *crc)
268 t = *crc ^ 0xFFFFFFFFUL;
270 t = crc_32_tab[(t ^ *p++) & 0xff] ^ (t >> 8);
272 *crc = t ^ 0xFFFFFFFFUL;
277 get_crc(uint8_t *p, uint32_t len)
282 update_crc(p ,len , &crc);
288 str2u32(char *arg, uint32_t *val)
294 t = strtoul(arg, &err, 0);
295 if (errno || (err==arg) || ((err != NULL) && *err)) {
305 str2u16(char *arg, uint16_t *val)
311 t = strtoul(arg, &err, 0);
312 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
322 find_board(char *model){
323 struct cpx_board *board;
324 struct cpx_board *tmp;
327 for (tmp = boards; tmp->model != NULL; tmp++){
328 if (strcmp(model, tmp->model) == 0) {
339 get_file_crc(struct fw_block *ff)
342 uint8_t buf[FILE_BUF_LEN];
343 uint32_t readlen = sizeof(buf);
347 if ((ff->flags & BLOCK_FLAG_HAVEHDR) == 0) {
353 f = fopen(ff->name,"r");
355 errmsg(1,"unable to open file %s", ff->name);
366 fread(buf, readlen, 1, f);
368 errmsg(1,"unable to read from file %s", ff->name);
372 update_crc(buf, readlen, &ff->crc);
392 for (i = 0; i < fw_num_blocks; i++) {
394 if ((b->addr + b->blocklen) > flash_size) {
395 errmsg(0, "block at 0x%08X is too big", b->addr);
401 if (stat(b->name, &st) < 0) {
402 errmsg(0, "stat failed on %s",b->name);
405 if (b->blocklen == 0) {
406 b->blocklen = flash_size - b->addr;
408 if (st.st_size > b->blocklen) {
409 errmsg(0,"file %s is too big",b->name);
413 b->size = st.st_size;
421 process_partitions(void)
423 struct mylo_partition *part;
426 for (i = 0; i < fw_num_partitions; i++) {
427 part = &fw_partitions[i];
429 if (part->addr > flash_size) {
430 errmsg(0, "invalid partition at 0x%08X", part->addr);
434 if ((part->addr + part->size) > flash_size) {
435 errmsg(0, "partition at 0x%08X is too big", part->addr);
445 * routines to write data to the output file
448 write_out_data(FILE *outfile, uint8_t *data, size_t len, uint32_t *crc)
452 fwrite(data, len, 1, outfile);
454 errmsg(1,"unable to write output file");
459 update_crc(data, len, crc);
467 write_out_desc(FILE *outfile, struct mylo_fw_blockdesc *desc, uint32_t *crc)
469 return write_out_data(outfile, (uint8_t *)desc,
475 write_out_padding(FILE *outfile, size_t len, uint8_t padc, uint32_t *crc)
478 size_t buflen = sizeof(buff);
480 memset(buff, padc, buflen);
486 if (write_out_data(outfile, buff, buflen, crc))
497 write_out_file(FILE *outfile, struct fw_block *block, uint32_t *crc)
499 char buff[FILE_BUF_LEN];
500 size_t buflen = sizeof(buff);
506 if (block->name == NULL) {
510 if ((block->flags & BLOCK_FLAG_HAVEHDR) != 0) {
511 struct mylo_partition_header ph;
513 if (get_file_crc(block) != 0)
516 ph.crc = HOST_TO_LE32(block->crc);
517 ph.len = HOST_TO_LE32(block->size);
519 if (write_out_data(outfile, (uint8_t *)&ph, sizeof(ph), crc) != 0)
523 f = fopen(block->name,"r");
525 errmsg(1,"unable to open file: %s", block->name);
534 /* read data from source file */
536 fread(buff, buflen, 1, f);
538 errmsg(1,"unable to read from file: %s",block->name);
542 if (write_out_data(outfile, buff, buflen, crc) != 0)
550 /* align next block on a 4 byte boundary */
551 len = ALIGN(len,4) - block->size;
552 if (write_out_padding(outfile, len, 0xFF, crc))
555 dbgmsg(1,"file %s written out", block->name);
561 write_out_header(FILE *outfile, uint32_t *crc)
563 struct mylo_fw_header hdr;
565 memset(&hdr, 0, sizeof(hdr));
567 hdr.magic = HOST_TO_LE32(MYLO_MAGIC_FIRMWARE);
568 hdr.crc = HOST_TO_LE32(fw_header.crc);
569 hdr.vid = HOST_TO_LE16(fw_header.vid);
570 hdr.did = HOST_TO_LE16(fw_header.did);
571 hdr.svid = HOST_TO_LE16(fw_header.svid);
572 hdr.sdid = HOST_TO_LE16(fw_header.sdid);
573 hdr.rev = HOST_TO_LE32(fw_header.rev);
574 hdr.fwhi = HOST_TO_LE32(fw_header.fwhi);
575 hdr.fwlo = HOST_TO_LE32(fw_header.fwlo);
576 hdr.flags = HOST_TO_LE32(fw_header.flags);
578 if (fseek(outfile, 0, SEEK_SET) != 0) {
579 errmsg(1,"fseek failed on output file");
583 return write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr), crc);
588 write_out_partitions(FILE *outfile, uint32_t *crc)
590 struct mylo_partition_table p;
591 struct mylo_partition *p1, *p2;
594 if (fw_num_partitions == 0)
597 memset(&p, 0, sizeof(p));
599 p.magic = HOST_TO_LE32(MYLO_MAGIC_PARTITIONS);
600 for (i = 0; i < fw_num_partitions; i++) {
601 p1 = &p.partitions[i];
602 p2 = &fw_partitions[i];
603 p1->flags = HOST_TO_LE16(p2->flags);
604 p1->type = HOST_TO_LE16(PARTITION_TYPE_USED);
605 p1->addr = HOST_TO_LE32(p2->addr);
606 p1->size = HOST_TO_LE32(p2->size);
607 p1->param = HOST_TO_LE32(p2->param);
610 return write_out_data(outfile, (uint8_t *)&p, sizeof(p), crc);
615 write_out_blocks(FILE *outfile, uint32_t *crc)
617 struct mylo_fw_blockdesc desc;
623 * if at least one partition specified, write out block descriptor
624 * for the partition table
626 if (fw_num_partitions > 0) {
628 desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
629 desc.addr = HOST_TO_LE32(0x10000);
630 desc.dlen = HOST_TO_LE32(sizeof(struct mylo_partition_table));
631 desc.blen = HOST_TO_LE32(0x10000);
633 if (write_out_desc(outfile, &desc, crc) != 0)
638 * write out block descriptors for each files
640 for (i = 0; i < fw_num_blocks; i++) {
643 /* detect block size */
645 if ((b->flags & BLOCK_FLAG_HAVEHDR) != 0) {
646 dlen += sizeof(struct mylo_partition_header);
649 /* round up to 4 bytes */
650 dlen = ALIGN(dlen, 4);
652 /* setup the descriptor */
653 desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
654 desc.addr = HOST_TO_LE32(b->addr);
655 desc.dlen = HOST_TO_LE32(dlen);
656 desc.blen = HOST_TO_LE32(b->blocklen);
658 if (write_out_desc(outfile, &desc, crc) != 0)
663 * write out the null block descriptor
665 memset(&desc, 0, sizeof(desc));
666 if (write_out_desc(outfile, &desc, crc) != 0)
669 if (write_out_partitions(outfile, crc) != 0)
673 * write out data for each blocks
675 for (i = 0; i < fw_num_blocks; i++) {
677 if (write_out_file(outfile, b, crc) != 0)
689 parse_arg(char *arg, char *buf, char *argv[])
698 /* invalid argument string */
708 if (argl >= MAX_ARG_LEN) {
709 /* argument is too long */
710 argl = MAX_ARG_LEN-1;
713 memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
714 memcpy(buf, arg, argl);
717 for (i = 0; i < MAX_ARG_COUNT; i++) {
718 tok = strsep(ap, ":");
723 else if (tok[0] == '\0') {
736 required_arg(char c, char *arg)
738 if ((optarg != NULL) && (*arg == '-')){
739 errmsg(0,"option %c requires an argument\n", c);
748 is_empty_arg(char *arg)
759 parse_opt_flags(char ch, char *arg)
761 if (required_arg(ch, arg)) {
765 if (str2u32(arg, &fw_header.flags) != 0) {
766 errmsg(0,"invalid firmware flags: %s", arg);
770 dbgmsg(1, "firmware flags set to %X bytes", fw_header.flags);
780 parse_opt_size(char ch, char *arg)
782 if (required_arg(ch, arg)) {
786 if (str2u32(arg, &flash_size) != 0) {
787 errmsg(0,"invalid flash size: %s", arg);
791 dbgmsg(1, "flash size set to %d bytes", flash_size);
801 parse_opt_id(char ch, char *arg)
803 char buf[MAX_ARG_LEN];
804 char *argv[MAX_ARG_COUNT];
808 if (required_arg(ch, arg)) {
812 argc = parse_arg(arg, buf, argv);
814 /* processing vendor ID*/
816 if (is_empty_arg(p)) {
817 errmsg(0,"vendor id is missing from -%c %s",ch, arg);
819 } else if (str2u16(p, &fw_header.vid) != 0) {
820 errmsg(0,"invalid vendor id: %s", p);
824 dbgmsg(1, "vendor id is set to 0x%04X", fw_header.vid);
826 /* processing device ID*/
828 if (is_empty_arg(p)) {
829 errmsg(0,"device id is missing from -%c %s",ch, arg);
831 } else if (str2u16(p, &fw_header.did) != 0) {
832 errmsg(0,"invalid device id: %s", p);
836 dbgmsg(1, "device id is set to 0x%04X", fw_header.did);
838 /* processing sub vendor ID*/
840 if (is_empty_arg(p)) {
841 fw_header.svid = fw_header.vid;
842 } else if (str2u16(p, &fw_header.svid) != 0) {
843 errmsg(0,"invalid sub vendor id: %s", p);
847 dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header.svid);
849 /* processing device ID*/
851 if (is_empty_arg(p)) {
852 fw_header.sdid = fw_header.did;
853 } else if (str2u16(p, &fw_header.sdid) != 0) {
854 errmsg(0,"invalid sub device id: %s", p);
858 dbgmsg(1, "sub device id is set to 0x%04X", fw_header.sdid);
860 /* processing revision */
862 if (is_empty_arg(p)) {
864 } else if (str2u32(arg, &fw_header.rev) != 0) {
865 errmsg(0,"invalid revision number: %s", p);
869 dbgmsg(1, "board revision is set to 0x%08X", fw_header.rev);
879 parse_opt_block(char ch, char *arg)
881 char buf[MAX_ARG_LEN];
882 char *argv[MAX_ARG_COUNT];
887 if (required_arg(ch, arg)) {
891 if (fw_num_blocks >= MAX_FW_BLOCKS) {
892 errmsg(0,"too many blocks specified");
896 argc = parse_arg(arg, buf, argv);
897 dbgmsg(1,"processing block option %s, count %d", arg, argc);
899 b = &fw_blocks[fw_num_blocks++];
901 /* processing block address */
903 if (is_empty_arg(p)) {
904 errmsg(0,"no block address specified in %s", arg);
906 } else if (str2u32(p, &b->addr) != 0) {
907 errmsg(0,"invalid block address: %s", p);
911 /* processing block length */
913 if (is_empty_arg(p)) {
914 errmsg(0,"no block length specified in %s", arg);
916 } else if (str2u32(p, &b->blocklen) != 0) {
917 errmsg(0,"invalid block length: %s", p);
922 dbgmsg(1,"empty block %s", arg);
926 /* processing flags */
928 if (is_empty_arg(p) == 0) {
929 for ( ; *p != '\0'; p++) {
932 b->flags |= BLOCK_FLAG_HAVEHDR;
935 errmsg(0, "invalid block flag \"%c\"", *p);
941 /* processing file name */
943 if (is_empty_arg(p)) {
944 errmsg(0,"file name missing in %s", arg);
949 if (b->name == NULL) {
950 errmsg(0,"not enough memory");
964 parse_opt_partition(char ch, char *arg)
966 char buf[MAX_ARG_LEN];
967 char *argv[MAX_ARG_COUNT];
971 struct mylo_partition *part;
973 if (required_arg(ch, arg)) {
977 if (fw_num_partitions >= MYLO_MAX_PARTITIONS) {
978 errmsg(0, "too many partitions specified");
982 part = &fw_partitions[fw_num_partitions++];
984 argc = parse_arg(arg, buf, argv);
986 /* processing partition address */
988 if (is_empty_arg(p)) {
989 errmsg(0,"partition address missing in -%c %s",ch, arg);
991 } else if (str2u32(p, &part->addr) != 0) {
992 errmsg(0,"invalid partition address: %s", p);
996 /* processing partition size */
998 if (is_empty_arg(p)) {
999 errmsg(0,"partition size missing in -%c %s",ch, arg);
1001 } else if (str2u32(p, &part->size) != 0) {
1002 errmsg(0,"invalid partition size: %s", p);
1006 /* processing partition flags */
1008 if (is_empty_arg(p) == 0) {
1009 for ( ; *p != '\0'; p++) {
1012 part->flags |= PARTITION_FLAG_ACTIVE;
1015 part->flags |= PARTITION_FLAG_PRELOAD;
1018 part->flags |= PARTITION_FLAG_HAVEHDR;
1021 errmsg(0, "invalid partition flag \"%c\"", *p);
1027 /* processing partition parameter */
1029 if (is_empty_arg(p)) {
1030 /* set default partition parameter */
1032 } else if (str2u32(p, &part->param) != 0) {
1033 errmsg(0,"invalid partition parameter: %s", p);
1038 if (part->size == 0) {
1039 part->size = flash_size - part->addr;
1042 /* processing file parameter */
1044 if (is_empty_arg(p) == 0) {
1047 if (fw_num_blocks == MAX_FW_BLOCKS) {
1048 errmsg(0,"too many blocks specified", p);
1051 b = &fw_blocks[fw_num_blocks++];
1052 b->name = strdup(p);
1053 b->addr = part->addr;
1054 b->blocklen = part->size;
1055 if (part->flags & PARTITION_FLAG_HAVEHDR) {
1056 b->flags |= BLOCK_FLAG_HAVEHDR;
1069 parse_opt_board(char ch, char *arg)
1071 struct cpx_board *board;
1073 if (required_arg(ch, arg)) {
1077 board = find_board(arg);
1079 errmsg(0,"invalid/unknown board specified: %s", arg);
1083 fw_header.vid = board->vid;
1084 fw_header.did = board->did;
1085 fw_header.svid = board->svid;
1086 fw_header.sdid = board->sdid;
1088 flash_size = board->flash_size;
1098 parse_opt_rev(char ch, char *arg)
1100 if (required_arg(ch, arg)) {
1104 if (str2u32(arg, &fw_header.rev) != 0) {
1105 errmsg(0,"invalid revision number: %s", arg);
1117 main(int argc, char *argv[])
1119 int optinvalid = 0; /* flag for invalid option */
1121 int res = EXIT_FAILURE;
1126 progname=basename(argv[0]);
1128 memset(&fw_header, 0, sizeof(fw_header));
1130 /* init header defaults */
1131 fw_header.vid = VENID_COMPEX;
1132 fw_header.did = DEVID_COMPEX_WP54G;
1133 fw_header.svid = VENID_COMPEX;
1134 fw_header.sdid = DEVID_COMPEX_WP54G;
1135 fw_header.fwhi = 0x20000;
1136 fw_header.fwlo = 0x20000;
1137 fw_header.flags = 0;
1139 opterr = 0; /* could not print standard getopt error messages */
1140 while ((c = getopt(argc, argv, "b:B:f:hi:p:r:s:v")) != -1) {
1144 optinvalid = parse_opt_block(c,optarg);
1147 optinvalid = parse_opt_board(c,optarg);
1150 optinvalid = parse_opt_flags(c,optarg);
1153 usage(EXIT_SUCCESS);
1156 optinvalid = parse_opt_id(c,optarg);
1159 optinvalid = parse_opt_partition(c,optarg);
1162 optinvalid = parse_opt_rev(c,optarg);
1165 optinvalid = parse_opt_size(c,optarg);
1174 if (optinvalid != 0 ){
1175 errmsg(0, "invalid option: -%c", optopt);
1180 if (optind == argc) {
1181 errmsg(0, "no output file specified");
1185 ofname = argv[optind++];
1187 if (optind < argc) {
1188 errmsg(0, "invalid option: %s", argv[optind]);
1192 if (flash_size == 0) {
1193 errmsg(0, "no flash size specified");
1197 if (process_files() != 0) {
1201 if (process_partitions() != 0) {
1205 outfile = fopen(ofname, "w");
1206 if (outfile == NULL) {
1207 errmsg(1, "could not open \"%s\" for writing", ofname);
1214 if (write_out_header(outfile, &crc) != 0)
1217 if (write_out_blocks(outfile, &crc) != 0)
1220 fw_header.crc = crc;
1221 if (write_out_header(outfile, NULL) != 0)
1224 dbgmsg(1,"Firmware file %s completed.", ofname);
1231 if (res != EXIT_SUCCESS) {