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)
51 #define PART_NAME_LEN 32
55 uint32_t blocklen; /* length of the block */
58 char *name; /* name of the file */
59 uint32_t size; /* length of the file */
60 uint32_t crc; /* crc value of the file */
64 struct mylo_partition mylo;
65 char name[PART_NAME_LEN];
68 #define BLOCK_FLAG_HAVEHDR 0x0001
71 char *model; /* model number*/
72 char *name; /* model name*/
73 char *desc; /* description */
74 uint16_t vid; /* vendor id */
75 uint16_t did; /* device id */
76 uint16_t svid; /* sub vendor id */
77 uint16_t sdid; /* sub device id */
78 uint32_t flash_size; /* size of flash */
79 uint32_t part_offset; /* offset of the partition_table */
80 uint32_t part_size; /* size of the partition_table */
83 #define BOARD(_vid, _did, _svid, _sdid, _flash, _mod, _name, _desc, _po, _ps) { \
84 .model = _mod, .name = _name, .desc = _desc, \
85 .vid = _vid, .did = _did, .svid = _svid, .sdid = _sdid, \
86 .flash_size = (_flash << 20), \
87 .part_offset = _po, .part_size = _ps }
89 #define CPX_BOARD(_did, _flash, _mod, _name, _desc, _po, _ps) \
90 BOARD(VENID_COMPEX, _did, VENID_COMPEX, _did, _flash, _mod, _name, _desc, _po, _ps)
92 #define CPX_BOARD_ADM(_did, _flash, _mod, _name, _desc) \
93 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
95 #define CPX_BOARD_AR71XX(_did, _flash, _mod, _name, _desc) \
96 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x20000, 0x8000)
98 #define CPX_BOARD_AR23XX(_did, _flash, _mod, _name, _desc) \
99 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
101 #define ALIGN(x,y) ((x)+((y)-1)) & ~((y)-1)
106 uint32_t flash_size = 0;
107 int fw_num_partitions = 0;
108 int fw_num_blocks = 0;
111 struct mylo_fw_header fw_header;
112 struct fw_part fw_parts[MYLO_MAX_PARTITIONS];
113 struct fw_block fw_blocks[MAX_FW_BLOCKS];
114 struct cpx_board *board;
116 struct cpx_board boards[] = {
117 CPX_BOARD_ADM(DEVID_COMPEX_NP18A, 4,
118 "NP18A", "Compex NetPassage 18A",
119 "Dualband Wireless A+G Internet Gateway"),
120 CPX_BOARD_ADM(DEVID_COMPEX_NP26G8M, 2,
121 "NP26G8M", "Compex NetPassage 26G (8M)",
122 "Wireless-G Broadband Multimedia Gateway"),
123 CPX_BOARD_ADM(DEVID_COMPEX_NP26G16M, 4,
124 "NP26G16M", "Compex NetPassage 26G (16M)",
125 "Wireless-G Broadband Multimedia Gateway"),
126 CPX_BOARD_ADM(DEVID_COMPEX_NP27G, 4,
127 "NP27G", "Compex NetPassage 27G",
128 "Wireless-G 54Mbps eXtended Range Router"),
129 CPX_BOARD_ADM(DEVID_COMPEX_NP28G, 4,
130 "NP28G", "Compex NetPassage 28G",
131 "Wireless 108Mbps Super-G XR Multimedia Router with 4 USB Ports"),
132 CPX_BOARD_ADM(DEVID_COMPEX_NP28GHS, 4,
133 "NP28GHS", "Compex NetPassage 28G (HotSpot)",
135 CPX_BOARD_ADM(DEVID_COMPEX_WP18, 4,
136 "WP18", "Compex NetPassage WP18",
137 "Wireless-G 54Mbps A+G Dualband Access Point"),
138 CPX_BOARD_ADM(DEVID_COMPEX_WP54G, 4,
139 "WP54G", "Compex WP54G",
140 "Wireless-G 54Mbps XR Access Point"),
141 CPX_BOARD_ADM(DEVID_COMPEX_WP54Gv1C, 2,
142 "WP54Gv1C", "Compex WP54G rev.1C",
143 "Wireless-G 54Mbps XR Access Point"),
144 CPX_BOARD_ADM(DEVID_COMPEX_WP54AG, 4,
145 "WP54AG", "Compex WP54AG",
146 "Wireless-AG 54Mbps XR Access Point"),
147 CPX_BOARD_ADM(DEVID_COMPEX_WPP54G, 4,
148 "WPP54G", "Compex WPP54G",
149 "Outdoor Access Point"),
150 CPX_BOARD_ADM(DEVID_COMPEX_WPP54AG, 4,
151 "WPP54AG", "Compex WPP54AG",
152 "Outdoor Access Point"),
154 CPX_BOARD_AR71XX(DEVID_COMPEX_WP543, 2,
155 "WP543", "Compex WP543",
157 CPX_BOARD_AR71XX(DEVID_COMPEX_WPE72, 8,
158 "WPE72", "Compex WPE72",
160 CPX_BOARD_AR71XX(DEVID_COMPEX_WPJ558, 8,
161 "WPJ558", "Compex WPJ558",
164 CPX_BOARD_AR23XX(DEVID_COMPEX_NP25G, 4,
165 "NP25G", "Compex NetPassage 25G",
166 "Wireless 54Mbps XR Router"),
167 CPX_BOARD_AR23XX(DEVID_COMPEX_WPE53G, 4,
168 "WPE53G", "Compex NetPassage 25G",
169 "Wireless 54Mbps XR Access Point"),
174 errmsgv(int syserr, const char *fmt, va_list arg_ptr)
179 fprintf(stderr, "[%s] Error: ", progname);
180 vfprintf(stderr, fmt, arg_ptr);
182 fprintf(stderr, ": %s", strerror(save));
184 fprintf(stderr, "\n");
188 errmsg(int syserr, const char *fmt, ...)
191 va_start(arg_ptr, fmt);
192 errmsgv(syserr, fmt, arg_ptr);
197 dbgmsg(int level, const char *fmt, ...)
200 if (verblevel >= level) {
202 va_start(arg_ptr, fmt);
203 vfprintf(stderr, fmt, arg_ptr);
204 fprintf(stderr, "\n");
213 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
214 struct cpx_board *board;
216 fprintf(stream, "Usage: %s [OPTION...] <file>\n", progname);
219 " <file> write output to the <file>\n"
222 " -B <board> create firmware for the board specified with <board>.\n"
223 " This option set vendor id, device id, subvendor id,\n"
224 " subdevice id, and flash size options to the right value.\n"
225 " valid <board> values:\n");
226 for (board = boards; board->model != NULL; board++){
229 board->model, board->name);
232 " -i <vid>:<did>[:<svid>[:<sdid>]]\n"
233 " create firmware for board with vendor id <vid>, device\n"
234 " id <did>, subvendor id <svid> and subdevice id <sdid>.\n"
235 " -r <rev> set board revision to <rev>.\n"
236 " -s <size> set flash size to <size>\n"
237 " -b <addr>:<len>[:[<flags>]:<file>]\n"
238 " define block at <addr> with length of <len>.\n"
239 " valid <flag> values:\n"
240 " h : add crc header before the file data.\n"
241 " -p <addr>:<len>[:<flags>[:<param>[:<name>[:<file>]]]]\n"
242 " add partition at <addr>, with size of <len> to the\n"
243 " partition table, set partition name to <name>, partition \n"
244 " flags to <flags> and partition parameter to <param>.\n"
245 " If the <file> is specified content of the file will be \n"
246 " added to the firmware image.\n"
247 " valid <flag> values:\n"
248 " a: this is the active partition. The bootloader loads\n"
249 " the firmware from this partition.\n"
250 " h: the partition data have a header.\n"
251 " l: the partition data uses LZMA compression.\n"
252 " p: the bootloader loads data from this partition to\n"
253 " the RAM before decompress it.\n"
254 " -h show this screen\n"
261 * Code to compute the CRC-32 table. Borrowed from
262 * gzip-1.0.3/makecrc.c.
265 static uint32_t crc_32_tab[256];
270 /* Not copyrighted 1990 Mark Adler */
272 uint32_t c; /* crc shift register */
273 uint32_t e; /* polynomial exclusive-or pattern */
274 int i; /* counter for all possible eight bit values */
275 int k; /* byte being shifted into crc apparatus */
277 /* terms of polynomial defining this crc (except x^32): */
278 static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
280 /* Make exclusive-or pattern from polynomial */
282 for (i = 0; i < sizeof(p)/sizeof(int); i++)
283 e |= 1L << (31 - p[i]);
287 for (i = 1; i < 256; i++) {
289 for (k = i | 256; k != 1; k >>= 1) {
290 c = c & 1 ? (c >> 1) ^ e : c >> 1;
300 update_crc(uint8_t *p, uint32_t len, uint32_t *crc)
304 t = *crc ^ 0xFFFFFFFFUL;
306 t = crc_32_tab[(t ^ *p++) & 0xff] ^ (t >> 8);
308 *crc = t ^ 0xFFFFFFFFUL;
313 get_crc(uint8_t *p, uint32_t len)
318 update_crc(p ,len , &crc);
324 str2u32(char *arg, uint32_t *val)
330 t = strtoul(arg, &err, 0);
331 if (errno || (err==arg) || ((err != NULL) && *err)) {
341 str2u16(char *arg, uint16_t *val)
347 t = strtoul(arg, &err, 0);
348 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
358 find_board(char *model){
359 struct cpx_board *board;
360 struct cpx_board *tmp;
363 for (tmp = boards; tmp->model != NULL; tmp++){
364 if (strcasecmp(model, tmp->model) == 0) {
375 get_file_crc(struct fw_block *ff)
378 uint8_t buf[FILE_BUF_LEN];
379 uint32_t readlen = sizeof(buf);
383 if ((ff->flags & BLOCK_FLAG_HAVEHDR) == 0) {
389 f = fopen(ff->name,"r");
391 errmsg(1,"unable to open file %s", ff->name);
402 fread(buf, readlen, 1, f);
404 errmsg(1,"unable to read from file %s", ff->name);
408 update_crc(buf, readlen, &ff->crc);
428 for (i = 0; i < fw_num_blocks; i++) {
430 if ((b->addr + b->blocklen) > flash_size) {
431 errmsg(0, "block at 0x%08X is too big", b->addr);
437 if (stat(b->name, &st) < 0) {
438 errmsg(0, "stat failed on %s",b->name);
441 if (b->blocklen == 0) {
442 b->blocklen = flash_size - b->addr;
444 if (st.st_size > b->blocklen) {
445 errmsg(0,"file %s is too big",b->name);
449 b->size = st.st_size;
457 process_partitions(void)
459 struct mylo_partition *part;
462 for (i = 0; i < fw_num_partitions; i++) {
463 part = &fw_parts[i].mylo;
465 if (part->addr > flash_size) {
466 errmsg(0, "invalid partition at 0x%08X", part->addr);
470 if ((part->addr + part->size) > flash_size) {
471 errmsg(0, "partition at 0x%08X is too big", part->addr);
481 * routines to write data to the output file
484 write_out_data(FILE *outfile, uint8_t *data, size_t len, uint32_t *crc)
488 fwrite(data, len, 1, outfile);
490 errmsg(1,"unable to write output file");
495 update_crc(data, len, crc);
503 write_out_desc(FILE *outfile, struct mylo_fw_blockdesc *desc, uint32_t *crc)
505 return write_out_data(outfile, (uint8_t *)desc,
511 write_out_padding(FILE *outfile, size_t len, uint8_t padc, uint32_t *crc)
514 size_t buflen = sizeof(buff);
516 memset(buff, padc, buflen);
522 if (write_out_data(outfile, buff, buflen, crc))
533 write_out_file(FILE *outfile, struct fw_block *block, uint32_t *crc)
535 char buff[FILE_BUF_LEN];
536 size_t buflen = sizeof(buff);
542 if (block->name == NULL) {
546 if ((block->flags & BLOCK_FLAG_HAVEHDR) != 0) {
547 struct mylo_partition_header ph;
549 if (get_file_crc(block) != 0)
552 ph.crc = HOST_TO_LE32(block->crc);
553 ph.len = HOST_TO_LE32(block->size);
555 if (write_out_data(outfile, (uint8_t *)&ph, sizeof(ph), crc) != 0)
559 f = fopen(block->name,"r");
561 errmsg(1,"unable to open file: %s", block->name);
570 /* read data from source file */
572 fread(buff, buflen, 1, f);
574 errmsg(1,"unable to read from file: %s",block->name);
578 if (write_out_data(outfile, buff, buflen, crc) != 0)
586 /* align next block on a 4 byte boundary */
587 len = ALIGN(len,4) - block->size;
588 if (write_out_padding(outfile, len, 0xFF, crc))
591 dbgmsg(1,"file %s written out", block->name);
597 write_out_header(FILE *outfile, uint32_t *crc)
599 struct mylo_fw_header hdr;
601 memset(&hdr, 0, sizeof(hdr));
603 hdr.magic = HOST_TO_LE32(MYLO_MAGIC_FIRMWARE);
604 hdr.crc = HOST_TO_LE32(fw_header.crc);
605 hdr.vid = HOST_TO_LE16(fw_header.vid);
606 hdr.did = HOST_TO_LE16(fw_header.did);
607 hdr.svid = HOST_TO_LE16(fw_header.svid);
608 hdr.sdid = HOST_TO_LE16(fw_header.sdid);
609 hdr.rev = HOST_TO_LE32(fw_header.rev);
610 hdr.fwhi = HOST_TO_LE32(fw_header.fwhi);
611 hdr.fwlo = HOST_TO_LE32(fw_header.fwlo);
612 hdr.flags = HOST_TO_LE32(fw_header.flags);
614 if (fseek(outfile, 0, SEEK_SET) != 0) {
615 errmsg(1,"fseek failed on output file");
619 return write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr), crc);
624 write_out_partitions(FILE *outfile, uint32_t *crc)
626 struct mylo_partition_table p;
627 char part_names[MYLO_MAX_PARTITIONS][PART_NAME_LEN];
631 if (fw_num_partitions == 0)
634 memset(&p, 0, sizeof(p));
635 memset(part_names, 0, sizeof(part_names));
637 p.magic = HOST_TO_LE32(MYLO_MAGIC_PARTITIONS);
638 for (i = 0; i < fw_num_partitions; i++) {
639 struct mylo_partition *mp;
642 mp = &p.partitions[i];
644 mp->flags = HOST_TO_LE16(fp->mylo.flags);
645 mp->type = HOST_TO_LE16(PARTITION_TYPE_USED);
646 mp->addr = HOST_TO_LE32(fp->mylo.addr);
647 mp->size = HOST_TO_LE32(fp->mylo.size);
648 mp->param = HOST_TO_LE32(fp->mylo.param);
650 memcpy(part_names[i], fp->name, PART_NAME_LEN);
653 ret = write_out_data(outfile, (uint8_t *)&p, sizeof(p), crc);
657 ret = write_out_data(outfile, (uint8_t *)part_names, sizeof(part_names),
664 write_out_blocks(FILE *outfile, uint32_t *crc)
666 struct mylo_fw_blockdesc desc;
672 * if at least one partition specified, write out block descriptor
673 * for the partition table
675 if (fw_num_partitions > 0) {
676 desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
677 desc.addr = HOST_TO_LE32(board->part_offset);
678 desc.dlen = HOST_TO_LE32(sizeof(struct mylo_partition_table) +
679 (MYLO_MAX_PARTITIONS * PART_NAME_LEN));
680 desc.blen = HOST_TO_LE32(board->part_size);
682 if (write_out_desc(outfile, &desc, crc) != 0)
687 * write out block descriptors for each files
689 for (i = 0; i < fw_num_blocks; i++) {
692 /* detect block size */
694 if ((b->flags & BLOCK_FLAG_HAVEHDR) != 0) {
695 dlen += sizeof(struct mylo_partition_header);
698 /* round up to 4 bytes */
699 dlen = ALIGN(dlen, 4);
701 /* setup the descriptor */
702 desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
703 desc.addr = HOST_TO_LE32(b->addr);
704 desc.dlen = HOST_TO_LE32(dlen);
705 desc.blen = HOST_TO_LE32(b->blocklen);
707 if (write_out_desc(outfile, &desc, crc) != 0)
712 * write out the null block descriptor
714 memset(&desc, 0, sizeof(desc));
715 if (write_out_desc(outfile, &desc, crc) != 0)
718 if (write_out_partitions(outfile, crc) != 0)
722 * write out data for each blocks
724 for (i = 0; i < fw_num_blocks; i++) {
726 if (write_out_file(outfile, b, crc) != 0)
738 parse_arg(char *arg, char *buf, char *argv[])
747 /* invalid argument string */
757 if (argl >= MAX_ARG_LEN) {
758 /* argument is too long */
759 argl = MAX_ARG_LEN-1;
762 memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
763 memcpy(buf, arg, argl);
766 for (i = 0; i < MAX_ARG_COUNT; i++) {
767 tok = strsep(ap, ":");
772 else if (tok[0] == '\0') {
785 required_arg(char c, char *arg)
787 if ((optarg != NULL) && (*arg == '-')){
788 errmsg(0,"option %c requires an argument\n", c);
797 is_empty_arg(char *arg)
808 parse_opt_flags(char ch, char *arg)
810 if (required_arg(ch, arg)) {
814 if (str2u32(arg, &fw_header.flags) != 0) {
815 errmsg(0,"invalid firmware flags: %s", arg);
819 dbgmsg(1, "firmware flags set to %X bytes", fw_header.flags);
829 parse_opt_size(char ch, char *arg)
831 if (required_arg(ch, arg)) {
835 if (str2u32(arg, &flash_size) != 0) {
836 errmsg(0,"invalid flash size: %s", arg);
840 dbgmsg(1, "flash size set to %d bytes", flash_size);
850 parse_opt_id(char ch, char *arg)
852 char buf[MAX_ARG_LEN];
853 char *argv[MAX_ARG_COUNT];
857 if (required_arg(ch, arg)) {
861 argc = parse_arg(arg, buf, argv);
863 /* processing vendor ID*/
865 if (is_empty_arg(p)) {
866 errmsg(0,"vendor id is missing from -%c %s",ch, arg);
868 } else if (str2u16(p, &fw_header.vid) != 0) {
869 errmsg(0,"invalid vendor id: %s", p);
873 dbgmsg(1, "vendor id is set to 0x%04X", fw_header.vid);
875 /* processing device ID*/
877 if (is_empty_arg(p)) {
878 errmsg(0,"device id is missing from -%c %s",ch, arg);
880 } else if (str2u16(p, &fw_header.did) != 0) {
881 errmsg(0,"invalid device id: %s", p);
885 dbgmsg(1, "device id is set to 0x%04X", fw_header.did);
887 /* processing sub vendor ID*/
889 if (is_empty_arg(p)) {
890 fw_header.svid = fw_header.vid;
891 } else if (str2u16(p, &fw_header.svid) != 0) {
892 errmsg(0,"invalid sub vendor id: %s", p);
896 dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header.svid);
898 /* processing device ID*/
900 if (is_empty_arg(p)) {
901 fw_header.sdid = fw_header.did;
902 } else if (str2u16(p, &fw_header.sdid) != 0) {
903 errmsg(0,"invalid sub device id: %s", p);
907 dbgmsg(1, "sub device id is set to 0x%04X", fw_header.sdid);
909 /* processing revision */
911 if (is_empty_arg(p)) {
913 } else if (str2u32(arg, &fw_header.rev) != 0) {
914 errmsg(0,"invalid revision number: %s", p);
918 dbgmsg(1, "board revision is set to 0x%08X", fw_header.rev);
928 parse_opt_block(char ch, char *arg)
930 char buf[MAX_ARG_LEN];
931 char *argv[MAX_ARG_COUNT];
936 if (required_arg(ch, arg)) {
940 if (fw_num_blocks >= MAX_FW_BLOCKS) {
941 errmsg(0,"too many blocks specified");
945 argc = parse_arg(arg, buf, argv);
946 dbgmsg(1,"processing block option %s, count %d", arg, argc);
948 b = &fw_blocks[fw_num_blocks++];
950 /* processing block address */
952 if (is_empty_arg(p)) {
953 errmsg(0,"no block address specified in %s", arg);
955 } else if (str2u32(p, &b->addr) != 0) {
956 errmsg(0,"invalid block address: %s", p);
960 /* processing block length */
962 if (is_empty_arg(p)) {
963 errmsg(0,"no block length specified in %s", arg);
965 } else if (str2u32(p, &b->blocklen) != 0) {
966 errmsg(0,"invalid block length: %s", p);
971 dbgmsg(1,"empty block %s", arg);
975 /* processing flags */
977 if (is_empty_arg(p) == 0) {
978 for ( ; *p != '\0'; p++) {
981 b->flags |= BLOCK_FLAG_HAVEHDR;
984 errmsg(0, "invalid block flag \"%c\"", *p);
990 /* processing file name */
992 if (is_empty_arg(p)) {
993 errmsg(0,"file name missing in %s", arg);
998 if (b->name == NULL) {
999 errmsg(0,"not enough memory");
1013 parse_opt_partition(char ch, char *arg)
1015 char buf[MAX_ARG_LEN];
1016 char *argv[MAX_ARG_COUNT];
1019 struct mylo_partition *part;
1022 if (required_arg(ch, arg)) {
1026 if (fw_num_partitions >= MYLO_MAX_PARTITIONS) {
1027 errmsg(0, "too many partitions specified");
1031 fp = &fw_parts[fw_num_partitions++];
1034 argc = parse_arg(arg, buf, argv);
1036 /* processing partition address */
1038 if (is_empty_arg(p)) {
1039 errmsg(0,"partition address missing in -%c %s",ch, arg);
1041 } else if (str2u32(p, &part->addr) != 0) {
1042 errmsg(0,"invalid partition address: %s", p);
1046 /* processing partition size */
1048 if (is_empty_arg(p)) {
1049 errmsg(0,"partition size missing in -%c %s",ch, arg);
1051 } else if (str2u32(p, &part->size) != 0) {
1052 errmsg(0,"invalid partition size: %s", p);
1056 /* processing partition flags */
1058 if (is_empty_arg(p) == 0) {
1059 for ( ; *p != '\0'; p++) {
1062 part->flags |= PARTITION_FLAG_ACTIVE;
1065 part->flags |= PARTITION_FLAG_PRELOAD;
1068 part->flags |= PARTITION_FLAG_LZMA;
1071 part->flags |= PARTITION_FLAG_HAVEHDR;
1074 errmsg(0, "invalid partition flag \"%c\"", *p);
1080 /* processing partition parameter */
1082 if (is_empty_arg(p)) {
1083 /* set default partition parameter */
1085 } else if (str2u32(p, &part->param) != 0) {
1086 errmsg(0,"invalid partition parameter: %s", p);
1091 if (is_empty_arg(p)) {
1092 /* set default partition parameter */
1095 strncpy(fp->name, p, PART_NAME_LEN);
1099 if (part->size == 0) {
1100 part->size = flash_size - part->addr;
1103 /* processing file parameter */
1105 if (is_empty_arg(p) == 0) {
1108 if (fw_num_blocks == MAX_FW_BLOCKS) {
1109 errmsg(0,"too many blocks specified", p);
1112 b = &fw_blocks[fw_num_blocks++];
1113 b->name = strdup(p);
1114 b->addr = part->addr;
1115 b->blocklen = part->size;
1116 if (part->flags & PARTITION_FLAG_HAVEHDR) {
1117 b->flags |= BLOCK_FLAG_HAVEHDR;
1130 parse_opt_board(char ch, char *arg)
1132 if (required_arg(ch, arg)) {
1136 board = find_board(arg);
1138 errmsg(0,"invalid/unknown board specified: %s", arg);
1142 fw_header.vid = board->vid;
1143 fw_header.did = board->did;
1144 fw_header.svid = board->svid;
1145 fw_header.sdid = board->sdid;
1147 flash_size = board->flash_size;
1157 parse_opt_rev(char ch, char *arg)
1159 if (required_arg(ch, arg)) {
1163 if (str2u32(arg, &fw_header.rev) != 0) {
1164 errmsg(0,"invalid revision number: %s", arg);
1176 main(int argc, char *argv[])
1178 int optinvalid = 0; /* flag for invalid option */
1180 int res = EXIT_FAILURE;
1185 progname=basename(argv[0]);
1187 memset(&fw_header, 0, sizeof(fw_header));
1189 /* init header defaults */
1190 fw_header.vid = VENID_COMPEX;
1191 fw_header.did = DEVID_COMPEX_WP54G;
1192 fw_header.svid = VENID_COMPEX;
1193 fw_header.sdid = DEVID_COMPEX_WP54G;
1194 fw_header.fwhi = 0x20000;
1195 fw_header.fwlo = 0x20000;
1196 fw_header.flags = 0;
1198 opterr = 0; /* could not print standard getopt error messages */
1199 while ((c = getopt(argc, argv, "b:B:f:hi:p:r:s:v")) != -1) {
1203 optinvalid = parse_opt_block(c,optarg);
1206 optinvalid = parse_opt_board(c,optarg);
1209 optinvalid = parse_opt_flags(c,optarg);
1212 usage(EXIT_SUCCESS);
1215 optinvalid = parse_opt_id(c,optarg);
1218 optinvalid = parse_opt_partition(c,optarg);
1221 optinvalid = parse_opt_rev(c,optarg);
1224 optinvalid = parse_opt_size(c,optarg);
1233 if (optinvalid != 0 ){
1234 errmsg(0, "invalid option: -%c", optopt);
1239 if (optind == argc) {
1240 errmsg(0, "no output file specified");
1244 ofname = argv[optind++];
1246 if (optind < argc) {
1247 errmsg(0, "invalid option: %s", argv[optind]);
1252 errmsg(0, "no board specified");
1256 if (flash_size == 0) {
1257 errmsg(0, "no flash size specified");
1261 if (process_files() != 0) {
1265 if (process_partitions() != 0) {
1269 outfile = fopen(ofname, "w");
1270 if (outfile == NULL) {
1271 errmsg(1, "could not open \"%s\" for writing", ofname);
1278 if (write_out_header(outfile, &crc) != 0)
1281 if (write_out_blocks(outfile, &crc) != 0)
1284 fw_header.crc = crc;
1285 if (write_out_header(outfile, NULL) != 0)
1288 dbgmsg(1,"Firmware file %s completed.", ofname);
1295 if (res != EXIT_SUCCESS) {