2 * Copyright (C) 2006-2008 Gabor Juhos <juhosg@openwrt.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
25 #include <unistd.h> /* for unlink() */
27 #include <getopt.h> /* for getopt() */
31 #include <endian.h> /* for __BYTE_ORDER */
33 #if defined(__CYGWIN__)
34 # include <byteswap.h>
37 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
38 # define HOST_TO_LE16(x) (x)
39 # define HOST_TO_LE32(x) (x)
41 # define HOST_TO_LE16(x) bswap_16(x)
42 # define HOST_TO_LE32(x) bswap_32(x)
47 #define MAX_FW_BLOCKS 32
48 #define MAX_ARG_COUNT 32
49 #define MAX_ARG_LEN 1024
50 #define FILE_BUF_LEN (16*1024)
54 uint32_t blocklen; /* length of the block */
57 char *name; /* name of the file */
58 uint32_t size; /* length of the file */
59 uint32_t crc; /* crc value of the file */
62 #define BLOCK_FLAG_HAVEHDR 0x0001
65 char *model; /* model number*/
66 char *name; /* model name*/
67 char *desc; /* description */
68 uint16_t vid; /* vendor id */
69 uint16_t did; /* device id */
70 uint16_t svid; /* sub vendor id */
71 uint16_t sdid; /* sub device id */
72 uint32_t flash_size; /* size of flash */
73 uint32_t part_offset; /* offset of the partition_table */
74 uint32_t part_size; /* size of the partition_table */
77 #define BOARD(_vid, _did, _svid, _sdid, _flash, _mod, _name, _desc, _po, _ps) { \
78 .model = _mod, .name = _name, .desc = _desc, \
79 .vid = _vid, .did = _did, .svid = _svid, .sdid = _sdid, \
80 .flash_size = (_flash << 20), \
81 .part_offset = _po, .part_size = _ps }
83 #define CPX_BOARD(_did, _flash, _mod, _name, _desc, _po, _ps) \
84 BOARD(VENID_COMPEX, _did, VENID_COMPEX, _did, _flash, _mod, _name, _desc, _po, _ps)
86 #define CPX_BOARD_ADM(_did, _flash, _mod, _name, _desc) \
87 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
89 #define CPX_BOARD_AR71XX(_did, _flash, _mod, _name, _desc) \
90 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x20000, 0x8000)
92 #define ALIGN(x,y) ((x)+((y)-1)) & ~((y)-1)
97 uint32_t flash_size = 0;
98 int fw_num_partitions = 0;
99 int fw_num_blocks = 0;
102 struct mylo_fw_header fw_header;
103 struct mylo_partition fw_partitions[MYLO_MAX_PARTITIONS];
104 struct fw_block fw_blocks[MAX_FW_BLOCKS];
105 struct cpx_board *board;
107 struct cpx_board boards[] = {
108 CPX_BOARD_ADM(DEVID_COMPEX_NP18A, 4,
109 "NP18A", "Compex NetPassage 18A",
110 "Dualband Wireless A+G Internet Gateway"),
111 CPX_BOARD_ADM(DEVID_COMPEX_NP26G8M, 2,
112 "NP26G8M", "Compex NetPassage 26G (8M)",
113 "Wireless-G Broadband Multimedia Gateway"),
114 CPX_BOARD_ADM(DEVID_COMPEX_NP26G16M, 4,
115 "NP26G16M", "Compex NetPassage 26G (16M)",
116 "Wireless-G Broadband Multimedia Gateway"),
117 CPX_BOARD_ADM(DEVID_COMPEX_NP27G, 4,
118 "NP27G", "Compex NetPassage 27G",
119 "Wireless-G 54Mbps eXtended Range Router"),
120 CPX_BOARD_ADM(DEVID_COMPEX_NP28G, 4,
121 "NP28G", "Compex NetPassage 28G",
122 "Wireless 108Mbps Super-G XR Multimedia Router with 4 USB Ports"),
123 CPX_BOARD_ADM(DEVID_COMPEX_NP28GHS, 4,
124 "NP28GHS", "Compex NetPassage 28G (HotSpot)",
126 CPX_BOARD_ADM(DEVID_COMPEX_WP18, 4,
127 "WP18", "Compex NetPassage WP18",
128 "Wireless-G 54Mbps A+G Dualband Access Point"),
129 CPX_BOARD_ADM(DEVID_COMPEX_WP54G, 4,
130 "WP54G", "Compex WP54G",
131 "Wireless-G 54Mbps XR Access Point"),
132 CPX_BOARD_ADM(DEVID_COMPEX_WP54Gv1C, 2,
133 "WP54Gv1C", "Compex WP54G rev.1C",
134 "Wireless-G 54Mbps XR Access Point"),
135 CPX_BOARD_ADM(DEVID_COMPEX_WP54AG, 4,
136 "WP54AG", "Compex WP54AG",
137 "Wireless-AG 54Mbps XR Access Point"),
138 CPX_BOARD_ADM(DEVID_COMPEX_WPP54G, 4,
139 "WPP54G", "Compex WPP54G",
140 "Outdoor Access Point"),
141 CPX_BOARD_ADM(DEVID_COMPEX_WPP54AG, 4,
142 "WPP54AG", "Compex WPP54AG",
143 "Outdoor Access Point"),
144 CPX_BOARD_AR71XX(DEVID_COMPEX_WP543, 2,
145 "WP543", "Compex WP543",
151 errmsgv(int syserr, const char *fmt, va_list arg_ptr)
156 fprintf(stderr, "[%s] Error: ", progname);
157 vfprintf(stderr, fmt, arg_ptr);
159 fprintf(stderr, ": %s", strerror(save));
161 fprintf(stderr, "\n");
165 errmsg(int syserr, const char *fmt, ...)
168 va_start(arg_ptr, fmt);
169 errmsgv(syserr, fmt, arg_ptr);
174 dbgmsg(int level, const char *fmt, ...)
177 if (verblevel >= level) {
179 va_start(arg_ptr, fmt);
180 vfprintf(stderr, fmt, arg_ptr);
181 fprintf(stderr, "\n");
190 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
191 struct cpx_board *board;
193 fprintf(stream, "Usage: %s [OPTION...] <file>\n", progname);
196 " <file> write output to the <file>\n"
199 " -B <board> create firmware for the board specified with <board>.\n"
200 " This option set vendor id, device id, subvendor id,\n"
201 " subdevice id, and flash size options to the right value.\n"
202 " valid <board> values:\n");
203 for (board = boards; board->model != NULL; board++){
206 board->model, board->name);
209 " -i <vid>:<did>[:<svid>[:<sdid>]]\n"
210 " create firmware for board with vendor id <vid>, device\n"
211 " id <did>, subvendor id <svid> and subdevice id <sdid>.\n"
212 " -r <rev> set board revision to <rev>.\n"
213 " -s <size> set flash size to <size>\n"
214 " -b <addr>:<len>[:[<flags>]:<file>]\n"
215 " define block at <addr> with length of <len>.\n"
216 " valid <flag> values:\n"
217 " h : add crc header before the file data.\n"
218 " -p <addr>:<len>[:<flags>[:<param>[:<file>]]]\n"
219 " add partition at <addr>, with size of <len> to the\n"
220 " partition table, set partition flags to <flags> and\n"
221 " partition parameter to <param>. If the <file> is specified\n"
222 " content of the file is also added to the firmware image.\n"
223 " valid <flag> values:\n"
224 " a: this is the active partition. The bootloader loads\n"
225 " the firmware from this partition.\n"
226 " h: the partition data have a header.\n"
227 " l: the partition data uses LZMA compression.\n"
228 " p: the bootloader loads data from this partition to\n"
229 " the RAM before decompress it.\n"
230 " -h show this screen\n"
237 * Code to compute the CRC-32 table. Borrowed from
238 * gzip-1.0.3/makecrc.c.
241 static uint32_t crc_32_tab[256];
246 /* Not copyrighted 1990 Mark Adler */
248 uint32_t c; /* crc shift register */
249 uint32_t e; /* polynomial exclusive-or pattern */
250 int i; /* counter for all possible eight bit values */
251 int k; /* byte being shifted into crc apparatus */
253 /* terms of polynomial defining this crc (except x^32): */
254 static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
256 /* Make exclusive-or pattern from polynomial */
258 for (i = 0; i < sizeof(p)/sizeof(int); i++)
259 e |= 1L << (31 - p[i]);
263 for (i = 1; i < 256; i++) {
265 for (k = i | 256; k != 1; k >>= 1) {
266 c = c & 1 ? (c >> 1) ^ e : c >> 1;
276 update_crc(uint8_t *p, uint32_t len, uint32_t *crc)
280 t = *crc ^ 0xFFFFFFFFUL;
282 t = crc_32_tab[(t ^ *p++) & 0xff] ^ (t >> 8);
284 *crc = t ^ 0xFFFFFFFFUL;
289 get_crc(uint8_t *p, uint32_t len)
294 update_crc(p ,len , &crc);
300 str2u32(char *arg, uint32_t *val)
306 t = strtoul(arg, &err, 0);
307 if (errno || (err==arg) || ((err != NULL) && *err)) {
317 str2u16(char *arg, uint16_t *val)
323 t = strtoul(arg, &err, 0);
324 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
334 find_board(char *model){
335 struct cpx_board *board;
336 struct cpx_board *tmp;
339 for (tmp = boards; tmp->model != NULL; tmp++){
340 if (strcasecmp(model, tmp->model) == 0) {
351 get_file_crc(struct fw_block *ff)
354 uint8_t buf[FILE_BUF_LEN];
355 uint32_t readlen = sizeof(buf);
359 if ((ff->flags & BLOCK_FLAG_HAVEHDR) == 0) {
365 f = fopen(ff->name,"r");
367 errmsg(1,"unable to open file %s", ff->name);
378 fread(buf, readlen, 1, f);
380 errmsg(1,"unable to read from file %s", ff->name);
384 update_crc(buf, readlen, &ff->crc);
404 for (i = 0; i < fw_num_blocks; i++) {
406 if ((b->addr + b->blocklen) > flash_size) {
407 errmsg(0, "block at 0x%08X is too big", b->addr);
413 if (stat(b->name, &st) < 0) {
414 errmsg(0, "stat failed on %s",b->name);
417 if (b->blocklen == 0) {
418 b->blocklen = flash_size - b->addr;
420 if (st.st_size > b->blocklen) {
421 errmsg(0,"file %s is too big",b->name);
425 b->size = st.st_size;
433 process_partitions(void)
435 struct mylo_partition *part;
438 for (i = 0; i < fw_num_partitions; i++) {
439 part = &fw_partitions[i];
441 if (part->addr > flash_size) {
442 errmsg(0, "invalid partition at 0x%08X", part->addr);
446 if ((part->addr + part->size) > flash_size) {
447 errmsg(0, "partition at 0x%08X is too big", part->addr);
457 * routines to write data to the output file
460 write_out_data(FILE *outfile, uint8_t *data, size_t len, uint32_t *crc)
464 fwrite(data, len, 1, outfile);
466 errmsg(1,"unable to write output file");
471 update_crc(data, len, crc);
479 write_out_desc(FILE *outfile, struct mylo_fw_blockdesc *desc, uint32_t *crc)
481 return write_out_data(outfile, (uint8_t *)desc,
487 write_out_padding(FILE *outfile, size_t len, uint8_t padc, uint32_t *crc)
490 size_t buflen = sizeof(buff);
492 memset(buff, padc, buflen);
498 if (write_out_data(outfile, buff, buflen, crc))
509 write_out_file(FILE *outfile, struct fw_block *block, uint32_t *crc)
511 char buff[FILE_BUF_LEN];
512 size_t buflen = sizeof(buff);
518 if (block->name == NULL) {
522 if ((block->flags & BLOCK_FLAG_HAVEHDR) != 0) {
523 struct mylo_partition_header ph;
525 if (get_file_crc(block) != 0)
528 ph.crc = HOST_TO_LE32(block->crc);
529 ph.len = HOST_TO_LE32(block->size);
531 if (write_out_data(outfile, (uint8_t *)&ph, sizeof(ph), crc) != 0)
535 f = fopen(block->name,"r");
537 errmsg(1,"unable to open file: %s", block->name);
546 /* read data from source file */
548 fread(buff, buflen, 1, f);
550 errmsg(1,"unable to read from file: %s",block->name);
554 if (write_out_data(outfile, buff, buflen, crc) != 0)
562 /* align next block on a 4 byte boundary */
563 len = ALIGN(len,4) - block->size;
564 if (write_out_padding(outfile, len, 0xFF, crc))
567 dbgmsg(1,"file %s written out", block->name);
573 write_out_header(FILE *outfile, uint32_t *crc)
575 struct mylo_fw_header hdr;
577 memset(&hdr, 0, sizeof(hdr));
579 hdr.magic = HOST_TO_LE32(MYLO_MAGIC_FIRMWARE);
580 hdr.crc = HOST_TO_LE32(fw_header.crc);
581 hdr.vid = HOST_TO_LE16(fw_header.vid);
582 hdr.did = HOST_TO_LE16(fw_header.did);
583 hdr.svid = HOST_TO_LE16(fw_header.svid);
584 hdr.sdid = HOST_TO_LE16(fw_header.sdid);
585 hdr.rev = HOST_TO_LE32(fw_header.rev);
586 hdr.fwhi = HOST_TO_LE32(fw_header.fwhi);
587 hdr.fwlo = HOST_TO_LE32(fw_header.fwlo);
588 hdr.flags = HOST_TO_LE32(fw_header.flags);
590 if (fseek(outfile, 0, SEEK_SET) != 0) {
591 errmsg(1,"fseek failed on output file");
595 return write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr), crc);
600 write_out_partitions(FILE *outfile, uint32_t *crc)
602 struct mylo_partition_table p;
603 struct mylo_partition *p1, *p2;
606 if (fw_num_partitions == 0)
609 memset(&p, 0, sizeof(p));
611 p.magic = HOST_TO_LE32(MYLO_MAGIC_PARTITIONS);
612 for (i = 0; i < fw_num_partitions; i++) {
613 p1 = &p.partitions[i];
614 p2 = &fw_partitions[i];
615 p1->flags = HOST_TO_LE16(p2->flags);
616 p1->type = HOST_TO_LE16(PARTITION_TYPE_USED);
617 p1->addr = HOST_TO_LE32(p2->addr);
618 p1->size = HOST_TO_LE32(p2->size);
619 p1->param = HOST_TO_LE32(p2->param);
622 return write_out_data(outfile, (uint8_t *)&p, sizeof(p), crc);
627 write_out_blocks(FILE *outfile, uint32_t *crc)
629 struct mylo_fw_blockdesc desc;
635 * if at least one partition specified, write out block descriptor
636 * for the partition table
638 if (fw_num_partitions > 0) {
639 desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
640 desc.addr = HOST_TO_LE32(board->part_offset);
641 desc.dlen = HOST_TO_LE32(sizeof(struct mylo_partition_table));
642 desc.blen = HOST_TO_LE32(board->part_size);
644 if (write_out_desc(outfile, &desc, crc) != 0)
649 * write out block descriptors for each files
651 for (i = 0; i < fw_num_blocks; i++) {
654 /* detect block size */
656 if ((b->flags & BLOCK_FLAG_HAVEHDR) != 0) {
657 dlen += sizeof(struct mylo_partition_header);
660 /* round up to 4 bytes */
661 dlen = ALIGN(dlen, 4);
663 /* setup the descriptor */
664 desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
665 desc.addr = HOST_TO_LE32(b->addr);
666 desc.dlen = HOST_TO_LE32(dlen);
667 desc.blen = HOST_TO_LE32(b->blocklen);
669 if (write_out_desc(outfile, &desc, crc) != 0)
674 * write out the null block descriptor
676 memset(&desc, 0, sizeof(desc));
677 if (write_out_desc(outfile, &desc, crc) != 0)
680 if (write_out_partitions(outfile, crc) != 0)
684 * write out data for each blocks
686 for (i = 0; i < fw_num_blocks; i++) {
688 if (write_out_file(outfile, b, crc) != 0)
700 parse_arg(char *arg, char *buf, char *argv[])
709 /* invalid argument string */
719 if (argl >= MAX_ARG_LEN) {
720 /* argument is too long */
721 argl = MAX_ARG_LEN-1;
724 memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
725 memcpy(buf, arg, argl);
728 for (i = 0; i < MAX_ARG_COUNT; i++) {
729 tok = strsep(ap, ":");
734 else if (tok[0] == '\0') {
747 required_arg(char c, char *arg)
749 if ((optarg != NULL) && (*arg == '-')){
750 errmsg(0,"option %c requires an argument\n", c);
759 is_empty_arg(char *arg)
770 parse_opt_flags(char ch, char *arg)
772 if (required_arg(ch, arg)) {
776 if (str2u32(arg, &fw_header.flags) != 0) {
777 errmsg(0,"invalid firmware flags: %s", arg);
781 dbgmsg(1, "firmware flags set to %X bytes", fw_header.flags);
791 parse_opt_size(char ch, char *arg)
793 if (required_arg(ch, arg)) {
797 if (str2u32(arg, &flash_size) != 0) {
798 errmsg(0,"invalid flash size: %s", arg);
802 dbgmsg(1, "flash size set to %d bytes", flash_size);
812 parse_opt_id(char ch, char *arg)
814 char buf[MAX_ARG_LEN];
815 char *argv[MAX_ARG_COUNT];
819 if (required_arg(ch, arg)) {
823 argc = parse_arg(arg, buf, argv);
825 /* processing vendor ID*/
827 if (is_empty_arg(p)) {
828 errmsg(0,"vendor id is missing from -%c %s",ch, arg);
830 } else if (str2u16(p, &fw_header.vid) != 0) {
831 errmsg(0,"invalid vendor id: %s", p);
835 dbgmsg(1, "vendor id is set to 0x%04X", fw_header.vid);
837 /* processing device ID*/
839 if (is_empty_arg(p)) {
840 errmsg(0,"device id is missing from -%c %s",ch, arg);
842 } else if (str2u16(p, &fw_header.did) != 0) {
843 errmsg(0,"invalid device id: %s", p);
847 dbgmsg(1, "device id is set to 0x%04X", fw_header.did);
849 /* processing sub vendor ID*/
851 if (is_empty_arg(p)) {
852 fw_header.svid = fw_header.vid;
853 } else if (str2u16(p, &fw_header.svid) != 0) {
854 errmsg(0,"invalid sub vendor id: %s", p);
858 dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header.svid);
860 /* processing device ID*/
862 if (is_empty_arg(p)) {
863 fw_header.sdid = fw_header.did;
864 } else if (str2u16(p, &fw_header.sdid) != 0) {
865 errmsg(0,"invalid sub device id: %s", p);
869 dbgmsg(1, "sub device id is set to 0x%04X", fw_header.sdid);
871 /* processing revision */
873 if (is_empty_arg(p)) {
875 } else if (str2u32(arg, &fw_header.rev) != 0) {
876 errmsg(0,"invalid revision number: %s", p);
880 dbgmsg(1, "board revision is set to 0x%08X", fw_header.rev);
890 parse_opt_block(char ch, char *arg)
892 char buf[MAX_ARG_LEN];
893 char *argv[MAX_ARG_COUNT];
898 if (required_arg(ch, arg)) {
902 if (fw_num_blocks >= MAX_FW_BLOCKS) {
903 errmsg(0,"too many blocks specified");
907 argc = parse_arg(arg, buf, argv);
908 dbgmsg(1,"processing block option %s, count %d", arg, argc);
910 b = &fw_blocks[fw_num_blocks++];
912 /* processing block address */
914 if (is_empty_arg(p)) {
915 errmsg(0,"no block address specified in %s", arg);
917 } else if (str2u32(p, &b->addr) != 0) {
918 errmsg(0,"invalid block address: %s", p);
922 /* processing block length */
924 if (is_empty_arg(p)) {
925 errmsg(0,"no block length specified in %s", arg);
927 } else if (str2u32(p, &b->blocklen) != 0) {
928 errmsg(0,"invalid block length: %s", p);
933 dbgmsg(1,"empty block %s", arg);
937 /* processing flags */
939 if (is_empty_arg(p) == 0) {
940 for ( ; *p != '\0'; p++) {
943 b->flags |= BLOCK_FLAG_HAVEHDR;
946 errmsg(0, "invalid block flag \"%c\"", *p);
952 /* processing file name */
954 if (is_empty_arg(p)) {
955 errmsg(0,"file name missing in %s", arg);
960 if (b->name == NULL) {
961 errmsg(0,"not enough memory");
975 parse_opt_partition(char ch, char *arg)
977 char buf[MAX_ARG_LEN];
978 char *argv[MAX_ARG_COUNT];
982 struct mylo_partition *part;
984 if (required_arg(ch, arg)) {
988 if (fw_num_partitions >= MYLO_MAX_PARTITIONS) {
989 errmsg(0, "too many partitions specified");
993 part = &fw_partitions[fw_num_partitions++];
995 argc = parse_arg(arg, buf, argv);
997 /* processing partition address */
999 if (is_empty_arg(p)) {
1000 errmsg(0,"partition address missing in -%c %s",ch, arg);
1002 } else if (str2u32(p, &part->addr) != 0) {
1003 errmsg(0,"invalid partition address: %s", p);
1007 /* processing partition size */
1009 if (is_empty_arg(p)) {
1010 errmsg(0,"partition size missing in -%c %s",ch, arg);
1012 } else if (str2u32(p, &part->size) != 0) {
1013 errmsg(0,"invalid partition size: %s", p);
1017 /* processing partition flags */
1019 if (is_empty_arg(p) == 0) {
1020 for ( ; *p != '\0'; p++) {
1023 part->flags |= PARTITION_FLAG_ACTIVE;
1026 part->flags |= PARTITION_FLAG_PRELOAD;
1029 part->flags |= PARTITION_FLAG_LZMA;
1032 part->flags |= PARTITION_FLAG_HAVEHDR;
1035 errmsg(0, "invalid partition flag \"%c\"", *p);
1041 /* processing partition parameter */
1043 if (is_empty_arg(p)) {
1044 /* set default partition parameter */
1046 } else if (str2u32(p, &part->param) != 0) {
1047 errmsg(0,"invalid partition parameter: %s", p);
1052 if (part->size == 0) {
1053 part->size = flash_size - part->addr;
1056 /* processing file parameter */
1058 if (is_empty_arg(p) == 0) {
1061 if (fw_num_blocks == MAX_FW_BLOCKS) {
1062 errmsg(0,"too many blocks specified", p);
1065 b = &fw_blocks[fw_num_blocks++];
1066 b->name = strdup(p);
1067 b->addr = part->addr;
1068 b->blocklen = part->size;
1069 if (part->flags & PARTITION_FLAG_HAVEHDR) {
1070 b->flags |= BLOCK_FLAG_HAVEHDR;
1083 parse_opt_board(char ch, char *arg)
1085 if (required_arg(ch, arg)) {
1089 board = find_board(arg);
1091 errmsg(0,"invalid/unknown board specified: %s", arg);
1095 fw_header.vid = board->vid;
1096 fw_header.did = board->did;
1097 fw_header.svid = board->svid;
1098 fw_header.sdid = board->sdid;
1100 flash_size = board->flash_size;
1110 parse_opt_rev(char ch, char *arg)
1112 if (required_arg(ch, arg)) {
1116 if (str2u32(arg, &fw_header.rev) != 0) {
1117 errmsg(0,"invalid revision number: %s", arg);
1129 main(int argc, char *argv[])
1131 int optinvalid = 0; /* flag for invalid option */
1133 int res = EXIT_FAILURE;
1138 progname=basename(argv[0]);
1140 memset(&fw_header, 0, sizeof(fw_header));
1142 /* init header defaults */
1143 fw_header.vid = VENID_COMPEX;
1144 fw_header.did = DEVID_COMPEX_WP54G;
1145 fw_header.svid = VENID_COMPEX;
1146 fw_header.sdid = DEVID_COMPEX_WP54G;
1147 fw_header.fwhi = 0x20000;
1148 fw_header.fwlo = 0x20000;
1149 fw_header.flags = 0;
1151 opterr = 0; /* could not print standard getopt error messages */
1152 while ((c = getopt(argc, argv, "b:B:f:hi:p:r:s:v")) != -1) {
1156 optinvalid = parse_opt_block(c,optarg);
1159 optinvalid = parse_opt_board(c,optarg);
1162 optinvalid = parse_opt_flags(c,optarg);
1165 usage(EXIT_SUCCESS);
1168 optinvalid = parse_opt_id(c,optarg);
1171 optinvalid = parse_opt_partition(c,optarg);
1174 optinvalid = parse_opt_rev(c,optarg);
1177 optinvalid = parse_opt_size(c,optarg);
1186 if (optinvalid != 0 ){
1187 errmsg(0, "invalid option: -%c", optopt);
1192 if (optind == argc) {
1193 errmsg(0, "no output file specified");
1197 ofname = argv[optind++];
1199 if (optind < argc) {
1200 errmsg(0, "invalid option: %s", argv[optind]);
1205 errmsg(0, "no board specified");
1209 if (flash_size == 0) {
1210 errmsg(0, "no flash size specified");
1214 if (process_files() != 0) {
1218 if (process_partitions() != 0) {
1222 outfile = fopen(ofname, "w");
1223 if (outfile == NULL) {
1224 errmsg(1, "could not open \"%s\" for writing", ofname);
1231 if (write_out_header(outfile, &crc) != 0)
1234 if (write_out_blocks(outfile, &crc) != 0)
1237 fw_header.crc = crc;
1238 if (write_out_header(outfile, NULL) != 0)
1241 dbgmsg(1,"Firmware file %s completed.", ofname);
1248 if (res != EXIT_SUCCESS) {