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",
161 CPX_BOARD_AR23XX(DEVID_COMPEX_NP25G
, 4,
162 "NP25G", "Compex NetPassage 25G",
163 "Wireless 54Mbps XR Router"),
164 CPX_BOARD_AR23XX(DEVID_COMPEX_WPE53G
, 4,
165 "WPE53G", "Compex NetPassage 25G",
166 "Wireless 54Mbps XR Access Point"),
171 errmsgv(int syserr
, const char *fmt
, va_list arg_ptr
)
176 fprintf(stderr
, "[%s] Error: ", progname
);
177 vfprintf(stderr
, fmt
, arg_ptr
);
179 fprintf(stderr
, ": %s", strerror(save
));
181 fprintf(stderr
, "\n");
185 errmsg(int syserr
, const char *fmt
, ...)
188 va_start(arg_ptr
, fmt
);
189 errmsgv(syserr
, fmt
, arg_ptr
);
194 dbgmsg(int level
, const char *fmt
, ...)
197 if (verblevel
>= level
) {
199 va_start(arg_ptr
, fmt
);
200 vfprintf(stderr
, fmt
, arg_ptr
);
201 fprintf(stderr
, "\n");
210 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
211 struct cpx_board
*board
;
213 fprintf(stream
, "Usage: %s [OPTION...] <file>\n", progname
);
216 " <file> write output to the <file>\n"
219 " -B <board> create firmware for the board specified with <board>.\n"
220 " This option set vendor id, device id, subvendor id,\n"
221 " subdevice id, and flash size options to the right value.\n"
222 " valid <board> values:\n");
223 for (board
= boards
; board
->model
!= NULL
; board
++){
226 board
->model
, board
->name
);
229 " -i <vid>:<did>[:<svid>[:<sdid>]]\n"
230 " create firmware for board with vendor id <vid>, device\n"
231 " id <did>, subvendor id <svid> and subdevice id <sdid>.\n"
232 " -r <rev> set board revision to <rev>.\n"
233 " -s <size> set flash size to <size>\n"
234 " -b <addr>:<len>[:[<flags>]:<file>]\n"
235 " define block at <addr> with length of <len>.\n"
236 " valid <flag> values:\n"
237 " h : add crc header before the file data.\n"
238 " -p <addr>:<len>[:<flags>[:<param>[:<name>[:<file>]]]]\n"
239 " add partition at <addr>, with size of <len> to the\n"
240 " partition table, set partition name to <name>, partition \n"
241 " flags to <flags> and partition parameter to <param>.\n"
242 " If the <file> is specified content of the file will be \n"
243 " added to the firmware image.\n"
244 " valid <flag> values:\n"
245 " a: this is the active partition. The bootloader loads\n"
246 " the firmware from this partition.\n"
247 " h: the partition data have a header.\n"
248 " l: the partition data uses LZMA compression.\n"
249 " p: the bootloader loads data from this partition to\n"
250 " the RAM before decompress it.\n"
251 " -h show this screen\n"
258 * Code to compute the CRC-32 table. Borrowed from
259 * gzip-1.0.3/makecrc.c.
262 static uint32_t crc_32_tab
[256];
267 /* Not copyrighted 1990 Mark Adler */
269 uint32_t c
; /* crc shift register */
270 uint32_t e
; /* polynomial exclusive-or pattern */
271 int i
; /* counter for all possible eight bit values */
272 int k
; /* byte being shifted into crc apparatus */
274 /* terms of polynomial defining this crc (except x^32): */
275 static const int p
[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
277 /* Make exclusive-or pattern from polynomial */
279 for (i
= 0; i
< sizeof(p
)/sizeof(int); i
++)
280 e
|= 1L << (31 - p
[i
]);
284 for (i
= 1; i
< 256; i
++) {
286 for (k
= i
| 256; k
!= 1; k
>>= 1) {
287 c
= c
& 1 ? (c
>> 1) ^ e
: c
>> 1;
297 update_crc(uint8_t *p
, uint32_t len
, uint32_t *crc
)
301 t
= *crc
^ 0xFFFFFFFFUL
;
303 t
= crc_32_tab
[(t
^ *p
++) & 0xff] ^ (t
>> 8);
305 *crc
= t
^ 0xFFFFFFFFUL
;
310 get_crc(uint8_t *p
, uint32_t len
)
315 update_crc(p
,len
, &crc
);
321 str2u32(char *arg
, uint32_t *val
)
327 t
= strtoul(arg
, &err
, 0);
328 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
338 str2u16(char *arg
, uint16_t *val
)
344 t
= strtoul(arg
, &err
, 0);
345 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
355 find_board(char *model
){
356 struct cpx_board
*board
;
357 struct cpx_board
*tmp
;
360 for (tmp
= boards
; tmp
->model
!= NULL
; tmp
++){
361 if (strcasecmp(model
, tmp
->model
) == 0) {
372 get_file_crc(struct fw_block
*ff
)
375 uint8_t buf
[FILE_BUF_LEN
];
376 uint32_t readlen
= sizeof(buf
);
380 if ((ff
->flags
& BLOCK_FLAG_HAVEHDR
) == 0) {
386 f
= fopen(ff
->name
,"r");
388 errmsg(1,"unable to open file %s", ff
->name
);
399 fread(buf
, readlen
, 1, f
);
401 errmsg(1,"unable to read from file %s", ff
->name
);
405 update_crc(buf
, readlen
, &ff
->crc
);
425 for (i
= 0; i
< fw_num_blocks
; i
++) {
427 if ((b
->addr
+ b
->blocklen
) > flash_size
) {
428 errmsg(0, "block at 0x%08X is too big", b
->addr
);
434 if (stat(b
->name
, &st
) < 0) {
435 errmsg(0, "stat failed on %s",b
->name
);
438 if (b
->blocklen
== 0) {
439 b
->blocklen
= flash_size
- b
->addr
;
441 if (st
.st_size
> b
->blocklen
) {
442 errmsg(0,"file %s is too big",b
->name
);
446 b
->size
= st
.st_size
;
454 process_partitions(void)
456 struct mylo_partition
*part
;
459 for (i
= 0; i
< fw_num_partitions
; i
++) {
460 part
= &fw_parts
[i
].mylo
;
462 if (part
->addr
> flash_size
) {
463 errmsg(0, "invalid partition at 0x%08X", part
->addr
);
467 if ((part
->addr
+ part
->size
) > flash_size
) {
468 errmsg(0, "partition at 0x%08X is too big", part
->addr
);
478 * routines to write data to the output file
481 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
, uint32_t *crc
)
485 fwrite(data
, len
, 1, outfile
);
487 errmsg(1,"unable to write output file");
492 update_crc(data
, len
, crc
);
500 write_out_desc(FILE *outfile
, struct mylo_fw_blockdesc
*desc
, uint32_t *crc
)
502 return write_out_data(outfile
, (uint8_t *)desc
,
508 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
, uint32_t *crc
)
511 size_t buflen
= sizeof(buff
);
513 memset(buff
, padc
, buflen
);
519 if (write_out_data(outfile
, buff
, buflen
, crc
))
530 write_out_file(FILE *outfile
, struct fw_block
*block
, uint32_t *crc
)
532 char buff
[FILE_BUF_LEN
];
533 size_t buflen
= sizeof(buff
);
539 if (block
->name
== NULL
) {
543 if ((block
->flags
& BLOCK_FLAG_HAVEHDR
) != 0) {
544 struct mylo_partition_header ph
;
546 if (get_file_crc(block
) != 0)
549 ph
.crc
= HOST_TO_LE32(block
->crc
);
550 ph
.len
= HOST_TO_LE32(block
->size
);
552 if (write_out_data(outfile
, (uint8_t *)&ph
, sizeof(ph
), crc
) != 0)
556 f
= fopen(block
->name
,"r");
558 errmsg(1,"unable to open file: %s", block
->name
);
567 /* read data from source file */
569 fread(buff
, buflen
, 1, f
);
571 errmsg(1,"unable to read from file: %s",block
->name
);
575 if (write_out_data(outfile
, buff
, buflen
, crc
) != 0)
583 /* align next block on a 4 byte boundary */
584 len
= ALIGN(len
,4) - block
->size
;
585 if (write_out_padding(outfile
, len
, 0xFF, crc
))
588 dbgmsg(1,"file %s written out", block
->name
);
594 write_out_header(FILE *outfile
, uint32_t *crc
)
596 struct mylo_fw_header hdr
;
598 memset(&hdr
, 0, sizeof(hdr
));
600 hdr
.magic
= HOST_TO_LE32(MYLO_MAGIC_FIRMWARE
);
601 hdr
.crc
= HOST_TO_LE32(fw_header
.crc
);
602 hdr
.vid
= HOST_TO_LE16(fw_header
.vid
);
603 hdr
.did
= HOST_TO_LE16(fw_header
.did
);
604 hdr
.svid
= HOST_TO_LE16(fw_header
.svid
);
605 hdr
.sdid
= HOST_TO_LE16(fw_header
.sdid
);
606 hdr
.rev
= HOST_TO_LE32(fw_header
.rev
);
607 hdr
.fwhi
= HOST_TO_LE32(fw_header
.fwhi
);
608 hdr
.fwlo
= HOST_TO_LE32(fw_header
.fwlo
);
609 hdr
.flags
= HOST_TO_LE32(fw_header
.flags
);
611 if (fseek(outfile
, 0, SEEK_SET
) != 0) {
612 errmsg(1,"fseek failed on output file");
616 return write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
), crc
);
621 write_out_partitions(FILE *outfile
, uint32_t *crc
)
623 struct mylo_partition_table p
;
624 char part_names
[MYLO_MAX_PARTITIONS
][PART_NAME_LEN
];
628 if (fw_num_partitions
== 0)
631 memset(&p
, 0, sizeof(p
));
632 memset(part_names
, 0, sizeof(part_names
));
634 p
.magic
= HOST_TO_LE32(MYLO_MAGIC_PARTITIONS
);
635 for (i
= 0; i
< fw_num_partitions
; i
++) {
636 struct mylo_partition
*mp
;
639 mp
= &p
.partitions
[i
];
641 mp
->flags
= HOST_TO_LE16(fp
->mylo
.flags
);
642 mp
->type
= HOST_TO_LE16(PARTITION_TYPE_USED
);
643 mp
->addr
= HOST_TO_LE32(fp
->mylo
.addr
);
644 mp
->size
= HOST_TO_LE32(fp
->mylo
.size
);
645 mp
->param
= HOST_TO_LE32(fp
->mylo
.param
);
647 memcpy(part_names
[i
], fp
->name
, PART_NAME_LEN
);
650 ret
= write_out_data(outfile
, (uint8_t *)&p
, sizeof(p
), crc
);
654 ret
= write_out_data(outfile
, (uint8_t *)part_names
, sizeof(part_names
),
661 write_out_blocks(FILE *outfile
, uint32_t *crc
)
663 struct mylo_fw_blockdesc desc
;
669 * if at least one partition specified, write out block descriptor
670 * for the partition table
672 if (fw_num_partitions
> 0) {
673 desc
.type
= HOST_TO_LE32(FW_DESC_TYPE_USED
);
674 desc
.addr
= HOST_TO_LE32(board
->part_offset
);
675 desc
.dlen
= HOST_TO_LE32(sizeof(struct mylo_partition_table
) +
676 (MYLO_MAX_PARTITIONS
* PART_NAME_LEN
));
677 desc
.blen
= HOST_TO_LE32(board
->part_size
);
679 if (write_out_desc(outfile
, &desc
, crc
) != 0)
684 * write out block descriptors for each files
686 for (i
= 0; i
< fw_num_blocks
; i
++) {
689 /* detect block size */
691 if ((b
->flags
& BLOCK_FLAG_HAVEHDR
) != 0) {
692 dlen
+= sizeof(struct mylo_partition_header
);
695 /* round up to 4 bytes */
696 dlen
= ALIGN(dlen
, 4);
698 /* setup the descriptor */
699 desc
.type
= HOST_TO_LE32(FW_DESC_TYPE_USED
);
700 desc
.addr
= HOST_TO_LE32(b
->addr
);
701 desc
.dlen
= HOST_TO_LE32(dlen
);
702 desc
.blen
= HOST_TO_LE32(b
->blocklen
);
704 if (write_out_desc(outfile
, &desc
, crc
) != 0)
709 * write out the null block descriptor
711 memset(&desc
, 0, sizeof(desc
));
712 if (write_out_desc(outfile
, &desc
, crc
) != 0)
715 if (write_out_partitions(outfile
, crc
) != 0)
719 * write out data for each blocks
721 for (i
= 0; i
< fw_num_blocks
; i
++) {
723 if (write_out_file(outfile
, b
, crc
) != 0)
735 parse_arg(char *arg
, char *buf
, char *argv
[])
744 /* invalid argument string */
754 if (argl
>= MAX_ARG_LEN
) {
755 /* argument is too long */
756 argl
= MAX_ARG_LEN
-1;
759 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
760 memcpy(buf
, arg
, argl
);
763 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
764 tok
= strsep(ap
, ":");
769 else if (tok
[0] == '\0') {
782 required_arg(char c
, char *arg
)
784 if ((optarg
!= NULL
) && (*arg
== '-')){
785 errmsg(0,"option %c requires an argument\n", c
);
794 is_empty_arg(char *arg
)
805 parse_opt_flags(char ch
, char *arg
)
807 if (required_arg(ch
, arg
)) {
811 if (str2u32(arg
, &fw_header
.flags
) != 0) {
812 errmsg(0,"invalid firmware flags: %s", arg
);
816 dbgmsg(1, "firmware flags set to %X bytes", fw_header
.flags
);
826 parse_opt_size(char ch
, char *arg
)
828 if (required_arg(ch
, arg
)) {
832 if (str2u32(arg
, &flash_size
) != 0) {
833 errmsg(0,"invalid flash size: %s", arg
);
837 dbgmsg(1, "flash size set to %d bytes", flash_size
);
847 parse_opt_id(char ch
, char *arg
)
849 char buf
[MAX_ARG_LEN
];
850 char *argv
[MAX_ARG_COUNT
];
854 if (required_arg(ch
, arg
)) {
858 argc
= parse_arg(arg
, buf
, argv
);
860 /* processing vendor ID*/
862 if (is_empty_arg(p
)) {
863 errmsg(0,"vendor id is missing from -%c %s",ch
, arg
);
865 } else if (str2u16(p
, &fw_header
.vid
) != 0) {
866 errmsg(0,"invalid vendor id: %s", p
);
870 dbgmsg(1, "vendor id is set to 0x%04X", fw_header
.vid
);
872 /* processing device ID*/
874 if (is_empty_arg(p
)) {
875 errmsg(0,"device id is missing from -%c %s",ch
, arg
);
877 } else if (str2u16(p
, &fw_header
.did
) != 0) {
878 errmsg(0,"invalid device id: %s", p
);
882 dbgmsg(1, "device id is set to 0x%04X", fw_header
.did
);
884 /* processing sub vendor ID*/
886 if (is_empty_arg(p
)) {
887 fw_header
.svid
= fw_header
.vid
;
888 } else if (str2u16(p
, &fw_header
.svid
) != 0) {
889 errmsg(0,"invalid sub vendor id: %s", p
);
893 dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header
.svid
);
895 /* processing device ID*/
897 if (is_empty_arg(p
)) {
898 fw_header
.sdid
= fw_header
.did
;
899 } else if (str2u16(p
, &fw_header
.sdid
) != 0) {
900 errmsg(0,"invalid sub device id: %s", p
);
904 dbgmsg(1, "sub device id is set to 0x%04X", fw_header
.sdid
);
906 /* processing revision */
908 if (is_empty_arg(p
)) {
910 } else if (str2u32(arg
, &fw_header
.rev
) != 0) {
911 errmsg(0,"invalid revision number: %s", p
);
915 dbgmsg(1, "board revision is set to 0x%08X", fw_header
.rev
);
925 parse_opt_block(char ch
, char *arg
)
927 char buf
[MAX_ARG_LEN
];
928 char *argv
[MAX_ARG_COUNT
];
933 if (required_arg(ch
, arg
)) {
937 if (fw_num_blocks
>= MAX_FW_BLOCKS
) {
938 errmsg(0,"too many blocks specified");
942 argc
= parse_arg(arg
, buf
, argv
);
943 dbgmsg(1,"processing block option %s, count %d", arg
, argc
);
945 b
= &fw_blocks
[fw_num_blocks
++];
947 /* processing block address */
949 if (is_empty_arg(p
)) {
950 errmsg(0,"no block address specified in %s", arg
);
952 } else if (str2u32(p
, &b
->addr
) != 0) {
953 errmsg(0,"invalid block address: %s", p
);
957 /* processing block length */
959 if (is_empty_arg(p
)) {
960 errmsg(0,"no block length specified in %s", arg
);
962 } else if (str2u32(p
, &b
->blocklen
) != 0) {
963 errmsg(0,"invalid block length: %s", p
);
968 dbgmsg(1,"empty block %s", arg
);
972 /* processing flags */
974 if (is_empty_arg(p
) == 0) {
975 for ( ; *p
!= '\0'; p
++) {
978 b
->flags
|= BLOCK_FLAG_HAVEHDR
;
981 errmsg(0, "invalid block flag \"%c\"", *p
);
987 /* processing file name */
989 if (is_empty_arg(p
)) {
990 errmsg(0,"file name missing in %s", arg
);
995 if (b
->name
== NULL
) {
996 errmsg(0,"not enough memory");
1010 parse_opt_partition(char ch
, char *arg
)
1012 char buf
[MAX_ARG_LEN
];
1013 char *argv
[MAX_ARG_COUNT
];
1016 struct mylo_partition
*part
;
1019 if (required_arg(ch
, arg
)) {
1023 if (fw_num_partitions
>= MYLO_MAX_PARTITIONS
) {
1024 errmsg(0, "too many partitions specified");
1028 fp
= &fw_parts
[fw_num_partitions
++];
1031 argc
= parse_arg(arg
, buf
, argv
);
1033 /* processing partition address */
1035 if (is_empty_arg(p
)) {
1036 errmsg(0,"partition address missing in -%c %s",ch
, arg
);
1038 } else if (str2u32(p
, &part
->addr
) != 0) {
1039 errmsg(0,"invalid partition address: %s", p
);
1043 /* processing partition size */
1045 if (is_empty_arg(p
)) {
1046 errmsg(0,"partition size missing in -%c %s",ch
, arg
);
1048 } else if (str2u32(p
, &part
->size
) != 0) {
1049 errmsg(0,"invalid partition size: %s", p
);
1053 /* processing partition flags */
1055 if (is_empty_arg(p
) == 0) {
1056 for ( ; *p
!= '\0'; p
++) {
1059 part
->flags
|= PARTITION_FLAG_ACTIVE
;
1062 part
->flags
|= PARTITION_FLAG_PRELOAD
;
1065 part
->flags
|= PARTITION_FLAG_LZMA
;
1068 part
->flags
|= PARTITION_FLAG_HAVEHDR
;
1071 errmsg(0, "invalid partition flag \"%c\"", *p
);
1077 /* processing partition parameter */
1079 if (is_empty_arg(p
)) {
1080 /* set default partition parameter */
1082 } else if (str2u32(p
, &part
->param
) != 0) {
1083 errmsg(0,"invalid partition parameter: %s", p
);
1088 if (is_empty_arg(p
)) {
1089 /* set default partition parameter */
1092 strncpy(fp
->name
, p
, PART_NAME_LEN
);
1096 if (part
->size
== 0) {
1097 part
->size
= flash_size
- part
->addr
;
1100 /* processing file parameter */
1102 if (is_empty_arg(p
) == 0) {
1105 if (fw_num_blocks
== MAX_FW_BLOCKS
) {
1106 errmsg(0,"too many blocks specified", p
);
1109 b
= &fw_blocks
[fw_num_blocks
++];
1110 b
->name
= strdup(p
);
1111 b
->addr
= part
->addr
;
1112 b
->blocklen
= part
->size
;
1113 if (part
->flags
& PARTITION_FLAG_HAVEHDR
) {
1114 b
->flags
|= BLOCK_FLAG_HAVEHDR
;
1127 parse_opt_board(char ch
, char *arg
)
1129 if (required_arg(ch
, arg
)) {
1133 board
= find_board(arg
);
1135 errmsg(0,"invalid/unknown board specified: %s", arg
);
1139 fw_header
.vid
= board
->vid
;
1140 fw_header
.did
= board
->did
;
1141 fw_header
.svid
= board
->svid
;
1142 fw_header
.sdid
= board
->sdid
;
1144 flash_size
= board
->flash_size
;
1154 parse_opt_rev(char ch
, char *arg
)
1156 if (required_arg(ch
, arg
)) {
1160 if (str2u32(arg
, &fw_header
.rev
) != 0) {
1161 errmsg(0,"invalid revision number: %s", arg
);
1173 main(int argc
, char *argv
[])
1175 int optinvalid
= 0; /* flag for invalid option */
1177 int res
= EXIT_FAILURE
;
1182 progname
=basename(argv
[0]);
1184 memset(&fw_header
, 0, sizeof(fw_header
));
1186 /* init header defaults */
1187 fw_header
.vid
= VENID_COMPEX
;
1188 fw_header
.did
= DEVID_COMPEX_WP54G
;
1189 fw_header
.svid
= VENID_COMPEX
;
1190 fw_header
.sdid
= DEVID_COMPEX_WP54G
;
1191 fw_header
.fwhi
= 0x20000;
1192 fw_header
.fwlo
= 0x20000;
1193 fw_header
.flags
= 0;
1195 opterr
= 0; /* could not print standard getopt error messages */
1196 while ((c
= getopt(argc
, argv
, "b:B:f:hi:p:r:s:v")) != -1) {
1200 optinvalid
= parse_opt_block(c
,optarg
);
1203 optinvalid
= parse_opt_board(c
,optarg
);
1206 optinvalid
= parse_opt_flags(c
,optarg
);
1209 usage(EXIT_SUCCESS
);
1212 optinvalid
= parse_opt_id(c
,optarg
);
1215 optinvalid
= parse_opt_partition(c
,optarg
);
1218 optinvalid
= parse_opt_rev(c
,optarg
);
1221 optinvalid
= parse_opt_size(c
,optarg
);
1230 if (optinvalid
!= 0 ){
1231 errmsg(0, "invalid option: -%c", optopt
);
1236 if (optind
== argc
) {
1237 errmsg(0, "no output file specified");
1241 ofname
= argv
[optind
++];
1243 if (optind
< argc
) {
1244 errmsg(0, "invalid option: %s", argv
[optind
]);
1249 errmsg(0, "no board specified");
1253 if (flash_size
== 0) {
1254 errmsg(0, "no flash size specified");
1258 if (process_files() != 0) {
1262 if (process_partitions() != 0) {
1266 outfile
= fopen(ofname
, "w");
1267 if (outfile
== NULL
) {
1268 errmsg(1, "could not open \"%s\" for writing", ofname
);
1275 if (write_out_header(outfile
, &crc
) != 0)
1278 if (write_out_blocks(outfile
, &crc
) != 0)
1281 fw_header
.crc
= crc
;
1282 if (write_out_header(outfile
, NULL
) != 0)
1285 dbgmsg(1,"Firmware file %s completed.", ofname
);
1292 if (res
!= EXIT_SUCCESS
) {
This page took 0.101516 seconds and 5 git commands to generate.