1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2006-2008 Gabor Juhos <juhosg@openwrt.org>
10 #include <unistd.h> /* for unlink() */
12 #include <getopt.h> /* for getopt() */
16 #include <endian.h> /* for __BYTE_ORDER */
18 #if defined(__CYGWIN__)
19 # include <byteswap.h>
22 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
23 # define HOST_TO_LE16(x) (x)
24 # define HOST_TO_LE32(x) (x)
26 # define HOST_TO_LE16(x) bswap_16(x)
27 # define HOST_TO_LE32(x) bswap_32(x)
32 #define MAX_FW_BLOCKS 32
33 #define MAX_ARG_COUNT 32
34 #define MAX_ARG_LEN 1024
35 #define FILE_BUF_LEN (16*1024)
36 #define PART_NAME_LEN 32
40 uint32_t blocklen
; /* length of the block */
43 char *name
; /* name of the file */
44 uint32_t size
; /* length of the file */
45 uint32_t crc
; /* crc value of the file */
49 struct mylo_partition mylo
;
50 char name
[PART_NAME_LEN
];
53 #define BLOCK_FLAG_HAVEHDR 0x0001
56 char *model
; /* model number*/
57 char *name
; /* model name*/
58 char *desc
; /* description */
59 uint16_t vid
; /* vendor id */
60 uint16_t did
; /* device id */
61 uint16_t svid
; /* sub vendor id */
62 uint16_t sdid
; /* sub device id */
63 uint32_t flash_size
; /* size of flash */
64 uint32_t part_offset
; /* offset of the partition_table */
65 uint32_t part_size
; /* size of the partition_table */
68 #define BOARD(_vid, _did, _svid, _sdid, _flash, _mod, _name, _desc, _po, _ps) { \
69 .model = _mod, .name = _name, .desc = _desc, \
70 .vid = _vid, .did = _did, .svid = _svid, .sdid = _sdid, \
71 .flash_size = (_flash << 20), \
72 .part_offset = _po, .part_size = _ps }
74 #define CPX_BOARD(_did, _flash, _mod, _name, _desc, _po, _ps) \
75 BOARD(VENID_COMPEX, _did, VENID_COMPEX, _did, _flash, _mod, _name, _desc, _po, _ps)
77 #define CPX_BOARD_ADM(_did, _flash, _mod, _name, _desc) \
78 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
80 #define CPX_BOARD_AR71XX(_did, _flash, _mod, _name, _desc) \
81 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x20000, 0x8000)
83 #define CPX_BOARD_AR23XX(_did, _flash, _mod, _name, _desc) \
84 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
86 #define ALIGN(x,y) (((x)+((y)-1)) & ~((y)-1))
91 uint32_t flash_size
= 0;
92 int fw_num_partitions
= 0;
93 int fw_num_blocks
= 0;
96 struct mylo_fw_header fw_header
;
97 struct fw_part fw_parts
[MYLO_MAX_PARTITIONS
];
98 struct fw_block fw_blocks
[MAX_FW_BLOCKS
];
99 struct cpx_board
*board
;
101 struct cpx_board boards
[] = {
102 CPX_BOARD_ADM(DEVID_COMPEX_NP18A
, 4,
103 "NP18A", "Compex NetPassage 18A",
104 "Dualband Wireless A+G Internet Gateway"),
105 CPX_BOARD_ADM(DEVID_COMPEX_NP26G8M
, 2,
106 "NP26G8M", "Compex NetPassage 26G (8M)",
107 "Wireless-G Broadband Multimedia Gateway"),
108 CPX_BOARD_ADM(DEVID_COMPEX_NP26G16M
, 4,
109 "NP26G16M", "Compex NetPassage 26G (16M)",
110 "Wireless-G Broadband Multimedia Gateway"),
111 CPX_BOARD_ADM(DEVID_COMPEX_NP27G
, 4,
112 "NP27G", "Compex NetPassage 27G",
113 "Wireless-G 54Mbps eXtended Range Router"),
114 CPX_BOARD_ADM(DEVID_COMPEX_NP28G
, 4,
115 "NP28G", "Compex NetPassage 28G",
116 "Wireless 108Mbps Super-G XR Multimedia Router with 4 USB Ports"),
117 CPX_BOARD_ADM(DEVID_COMPEX_NP28GHS
, 4,
118 "NP28GHS", "Compex NetPassage 28G (HotSpot)",
120 CPX_BOARD_ADM(DEVID_COMPEX_WP18
, 4,
121 "WP18", "Compex NetPassage WP18",
122 "Wireless-G 54Mbps A+G Dualband Access Point"),
123 CPX_BOARD_ADM(DEVID_COMPEX_WP54G
, 4,
124 "WP54G", "Compex WP54G",
125 "Wireless-G 54Mbps XR Access Point"),
126 CPX_BOARD_ADM(DEVID_COMPEX_WP54Gv1C
, 2,
127 "WP54Gv1C", "Compex WP54G rev.1C",
128 "Wireless-G 54Mbps XR Access Point"),
129 CPX_BOARD_ADM(DEVID_COMPEX_WP54AG
, 4,
130 "WP54AG", "Compex WP54AG",
131 "Wireless-AG 54Mbps XR Access Point"),
132 CPX_BOARD_ADM(DEVID_COMPEX_WPP54G
, 4,
133 "WPP54G", "Compex WPP54G",
134 "Outdoor Access Point"),
135 CPX_BOARD_ADM(DEVID_COMPEX_WPP54AG
, 4,
136 "WPP54AG", "Compex WPP54AG",
137 "Outdoor Access Point"),
139 CPX_BOARD_AR71XX(DEVID_COMPEX_WP543
, 2,
140 "WP543", "Compex WP543",
142 CPX_BOARD_AR71XX(DEVID_COMPEX_WPE72
, 8,
143 "WPE72", "Compex WPE72",
146 CPX_BOARD_AR23XX(DEVID_COMPEX_NP25G
, 4,
147 "NP25G", "Compex NetPassage 25G",
148 "Wireless 54Mbps XR Router"),
149 CPX_BOARD_AR23XX(DEVID_COMPEX_WPE53G
, 4,
150 "WPE53G", "Compex NetPassage 25G",
151 "Wireless 54Mbps XR Access Point"),
156 errmsgv(int syserr
, const char *fmt
, va_list arg_ptr
)
161 fprintf(stderr
, "[%s] Error: ", progname
);
162 vfprintf(stderr
, fmt
, arg_ptr
);
164 fprintf(stderr
, ": %s", strerror(save
));
166 fprintf(stderr
, "\n");
170 errmsg(int syserr
, const char *fmt
, ...)
173 va_start(arg_ptr
, fmt
);
174 errmsgv(syserr
, fmt
, arg_ptr
);
179 dbgmsg(int level
, const char *fmt
, ...)
182 if (verblevel
>= level
) {
184 va_start(arg_ptr
, fmt
);
185 vfprintf(stderr
, fmt
, arg_ptr
);
186 fprintf(stderr
, "\n");
195 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
196 struct cpx_board
*board
;
198 fprintf(stream
, "Usage: %s [OPTION...] <file>\n", progname
);
201 " <file> write output to the <file>\n"
204 " -B <board> create firmware for the board specified with <board>.\n"
205 " This option set vendor id, device id, subvendor id,\n"
206 " subdevice id, and flash size options to the right value.\n"
207 " valid <board> values:\n");
208 for (board
= boards
; board
->model
!= NULL
; board
++){
211 board
->model
, board
->name
);
214 " -i <vid>:<did>[:<svid>[:<sdid>]]\n"
215 " create firmware for board with vendor id <vid>, device\n"
216 " id <did>, subvendor id <svid> and subdevice id <sdid>.\n"
217 " -r <rev> set board revision to <rev>.\n"
218 " -s <size> set flash size to <size>\n"
219 " -b <addr>:<len>[:[<flags>]:<file>]\n"
220 " define block at <addr> with length of <len>.\n"
221 " valid <flag> values:\n"
222 " h : add crc header before the file data.\n"
223 " -p <addr>:<len>[:<flags>[:<param>[:<name>[:<file>]]]]\n"
224 " add partition at <addr>, with size of <len> to the\n"
225 " partition table, set partition name to <name>, partition \n"
226 " flags to <flags> and partition parameter to <param>.\n"
227 " If the <file> is specified content of the file will be \n"
228 " added to the firmware image.\n"
229 " valid <flag> values:\n"
230 " a: this is the active partition. The bootloader loads\n"
231 " the firmware from this partition.\n"
232 " h: the partition data have a header.\n"
233 " l: the partition data uses LZMA compression.\n"
234 " p: the bootloader loads data from this partition to\n"
235 " the RAM before decompress it.\n"
236 " -h show this screen\n"
243 * Code to compute the CRC-32 table. Borrowed from
244 * gzip-1.0.3/makecrc.c.
247 static uint32_t crc_32_tab
[256];
252 /* Not copyrighted 1990 Mark Adler */
254 uint32_t c
; /* crc shift register */
255 uint32_t e
; /* polynomial exclusive-or pattern */
256 int i
; /* counter for all possible eight bit values */
257 int k
; /* byte being shifted into crc apparatus */
259 /* terms of polynomial defining this crc (except x^32): */
260 static const int p
[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
262 /* Make exclusive-or pattern from polynomial */
264 for (i
= 0; i
< sizeof(p
)/sizeof(int); i
++)
265 e
|= 1L << (31 - p
[i
]);
269 for (i
= 1; i
< 256; i
++) {
271 for (k
= i
| 256; k
!= 1; k
>>= 1) {
272 c
= c
& 1 ? (c
>> 1) ^ e
: c
>> 1;
282 update_crc(uint8_t *p
, uint32_t len
, uint32_t *crc
)
286 t
= *crc
^ 0xFFFFFFFFUL
;
288 t
= crc_32_tab
[(t
^ *p
++) & 0xff] ^ (t
>> 8);
290 *crc
= t
^ 0xFFFFFFFFUL
;
295 get_crc(uint8_t *p
, uint32_t len
)
300 update_crc(p
,len
, &crc
);
306 str2u32(char *arg
, uint32_t *val
)
312 t
= strtoul(arg
, &err
, 0);
313 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
323 str2u16(char *arg
, uint16_t *val
)
329 t
= strtoul(arg
, &err
, 0);
330 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
340 find_board(char *model
){
341 struct cpx_board
*board
;
342 struct cpx_board
*tmp
;
345 for (tmp
= boards
; tmp
->model
!= NULL
; tmp
++){
346 if (strcasecmp(model
, tmp
->model
) == 0) {
357 get_file_crc(struct fw_block
*ff
)
360 uint8_t buf
[FILE_BUF_LEN
];
361 uint32_t readlen
= sizeof(buf
);
365 if ((ff
->flags
& BLOCK_FLAG_HAVEHDR
) == 0) {
371 f
= fopen(ff
->name
,"r");
373 errmsg(1,"unable to open file %s", ff
->name
);
384 fread(buf
, readlen
, 1, f
);
386 errmsg(1,"unable to read from file %s", ff
->name
);
390 update_crc(buf
, readlen
, &ff
->crc
);
410 for (i
= 0; i
< fw_num_blocks
; i
++) {
412 if ((b
->addr
+ b
->blocklen
) > flash_size
) {
413 errmsg(0, "block at 0x%08X is too big", b
->addr
);
419 if (stat(b
->name
, &st
) < 0) {
420 errmsg(0, "stat failed on %s",b
->name
);
423 if (b
->blocklen
== 0) {
424 b
->blocklen
= flash_size
- b
->addr
;
426 if (st
.st_size
> b
->blocklen
) {
427 errmsg(0,"file %s is too big",b
->name
);
431 b
->size
= st
.st_size
;
439 process_partitions(void)
441 struct mylo_partition
*part
;
444 for (i
= 0; i
< fw_num_partitions
; i
++) {
445 part
= &fw_parts
[i
].mylo
;
447 if (part
->addr
> flash_size
) {
448 errmsg(0, "invalid partition at 0x%08X", part
->addr
);
452 if ((part
->addr
+ part
->size
) > flash_size
) {
453 errmsg(0, "partition at 0x%08X is too big", part
->addr
);
463 * routines to write data to the output file
466 write_out_data(FILE *outfile
, void *data
, size_t len
, uint32_t *crc
)
472 fwrite(ptr
, len
, 1, outfile
);
474 errmsg(1,"unable to write output file");
479 update_crc(ptr
, len
, crc
);
487 write_out_desc(FILE *outfile
, struct mylo_fw_blockdesc
*desc
, uint32_t *crc
)
489 return write_out_data(outfile
, (uint8_t *)desc
,
495 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
, uint32_t *crc
)
498 size_t buflen
= sizeof(buff
);
500 memset(buff
, padc
, buflen
);
506 if (write_out_data(outfile
, buff
, buflen
, crc
))
517 write_out_file(FILE *outfile
, struct fw_block
*block
, uint32_t *crc
)
519 char buff
[FILE_BUF_LEN
];
520 size_t buflen
= sizeof(buff
);
526 if (block
->name
== NULL
) {
530 if ((block
->flags
& BLOCK_FLAG_HAVEHDR
) != 0) {
531 struct mylo_partition_header ph
;
533 if (get_file_crc(block
) != 0)
536 ph
.crc
= HOST_TO_LE32(block
->crc
);
537 ph
.len
= HOST_TO_LE32(block
->size
);
539 if (write_out_data(outfile
, (uint8_t *)&ph
, sizeof(ph
), crc
) != 0)
543 f
= fopen(block
->name
,"r");
545 errmsg(1,"unable to open file: %s", block
->name
);
554 /* read data from source file */
556 fread(buff
, buflen
, 1, f
);
558 errmsg(1,"unable to read from file: %s",block
->name
);
562 if (write_out_data(outfile
, buff
, buflen
, crc
) != 0)
570 /* align next block on a 4 byte boundary */
571 len
= block
->size
% 4;
572 if (write_out_padding(outfile
, len
, 0xFF, crc
))
575 dbgmsg(1,"file %s written out", block
->name
);
581 write_out_header(FILE *outfile
, uint32_t *crc
)
583 struct mylo_fw_header hdr
;
585 memset(&hdr
, 0, sizeof(hdr
));
587 hdr
.magic
= HOST_TO_LE32(MYLO_MAGIC_FIRMWARE
);
588 hdr
.crc
= HOST_TO_LE32(fw_header
.crc
);
589 hdr
.vid
= HOST_TO_LE16(fw_header
.vid
);
590 hdr
.did
= HOST_TO_LE16(fw_header
.did
);
591 hdr
.svid
= HOST_TO_LE16(fw_header
.svid
);
592 hdr
.sdid
= HOST_TO_LE16(fw_header
.sdid
);
593 hdr
.rev
= HOST_TO_LE32(fw_header
.rev
);
594 hdr
.fwhi
= HOST_TO_LE32(fw_header
.fwhi
);
595 hdr
.fwlo
= HOST_TO_LE32(fw_header
.fwlo
);
596 hdr
.flags
= HOST_TO_LE32(fw_header
.flags
);
598 if (fseek(outfile
, 0, SEEK_SET
) != 0) {
599 errmsg(1,"fseek failed on output file");
603 return write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
), crc
);
608 write_out_partitions(FILE *outfile
, uint32_t *crc
)
610 struct mylo_partition_table p
;
611 char part_names
[MYLO_MAX_PARTITIONS
][PART_NAME_LEN
];
615 if (fw_num_partitions
== 0)
618 memset(&p
, 0, sizeof(p
));
619 memset(part_names
, 0, sizeof(part_names
));
621 p
.magic
= HOST_TO_LE32(MYLO_MAGIC_PARTITIONS
);
622 for (i
= 0; i
< fw_num_partitions
; i
++) {
623 struct mylo_partition
*mp
;
626 mp
= &p
.partitions
[i
];
628 mp
->flags
= HOST_TO_LE16(fp
->mylo
.flags
);
629 mp
->type
= HOST_TO_LE16(PARTITION_TYPE_USED
);
630 mp
->addr
= HOST_TO_LE32(fp
->mylo
.addr
);
631 mp
->size
= HOST_TO_LE32(fp
->mylo
.size
);
632 mp
->param
= HOST_TO_LE32(fp
->mylo
.param
);
634 memcpy(part_names
[i
], fp
->name
, PART_NAME_LEN
);
637 ret
= write_out_data(outfile
, (uint8_t *)&p
, sizeof(p
), crc
);
641 ret
= write_out_data(outfile
, (uint8_t *)part_names
, sizeof(part_names
),
648 write_out_blocks(FILE *outfile
, uint32_t *crc
)
650 struct mylo_fw_blockdesc desc
;
656 * if at least one partition specified, write out block descriptor
657 * for the partition table
659 if (fw_num_partitions
> 0) {
660 desc
.type
= HOST_TO_LE32(FW_DESC_TYPE_USED
);
661 desc
.addr
= HOST_TO_LE32(board
->part_offset
);
662 desc
.dlen
= HOST_TO_LE32(sizeof(struct mylo_partition_table
) +
663 (MYLO_MAX_PARTITIONS
* PART_NAME_LEN
));
664 desc
.blen
= HOST_TO_LE32(board
->part_size
);
666 if (write_out_desc(outfile
, &desc
, crc
) != 0)
671 * write out block descriptors for each files
673 for (i
= 0; i
< fw_num_blocks
; i
++) {
676 /* detect block size */
678 if ((b
->flags
& BLOCK_FLAG_HAVEHDR
) != 0) {
679 dlen
+= sizeof(struct mylo_partition_header
);
682 /* round up to 4 bytes */
683 dlen
= ALIGN(dlen
, 4);
685 /* setup the descriptor */
686 desc
.type
= HOST_TO_LE32(FW_DESC_TYPE_USED
);
687 desc
.addr
= HOST_TO_LE32(b
->addr
);
688 desc
.dlen
= HOST_TO_LE32(dlen
);
689 desc
.blen
= HOST_TO_LE32(b
->blocklen
);
691 if (write_out_desc(outfile
, &desc
, crc
) != 0)
696 * write out the null block descriptor
698 memset(&desc
, 0, sizeof(desc
));
699 if (write_out_desc(outfile
, &desc
, crc
) != 0)
702 if (write_out_partitions(outfile
, crc
) != 0)
706 * write out data for each blocks
708 for (i
= 0; i
< fw_num_blocks
; i
++) {
710 if (write_out_file(outfile
, b
, crc
) != 0)
722 parse_arg(char *arg
, char *buf
, char *argv
[])
731 /* invalid argument string */
741 if (argl
>= MAX_ARG_LEN
) {
742 /* argument is too long */
743 argl
= MAX_ARG_LEN
-1;
746 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
747 memcpy(buf
, arg
, argl
);
750 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
751 tok
= strsep(ap
, ":");
756 else if (tok
[0] == '\0') {
769 required_arg(char c
, char *arg
)
771 if ((optarg
!= NULL
) && (*arg
== '-')){
772 errmsg(0,"option %c requires an argument\n", c
);
781 is_empty_arg(char *arg
)
792 parse_opt_flags(char ch
, char *arg
)
794 if (required_arg(ch
, arg
)) {
798 if (str2u32(arg
, &fw_header
.flags
) != 0) {
799 errmsg(0,"invalid firmware flags: %s", arg
);
803 dbgmsg(1, "firmware flags set to %X bytes", fw_header
.flags
);
813 parse_opt_size(char ch
, char *arg
)
815 if (required_arg(ch
, arg
)) {
819 if (str2u32(arg
, &flash_size
) != 0) {
820 errmsg(0,"invalid flash size: %s", arg
);
824 dbgmsg(1, "flash size set to %d bytes", flash_size
);
834 parse_opt_id(char ch
, char *arg
)
836 char buf
[MAX_ARG_LEN
];
837 char *argv
[MAX_ARG_COUNT
];
840 if (required_arg(ch
, arg
)) {
844 parse_arg(arg
, buf
, argv
);
846 /* processing vendor ID*/
848 if (is_empty_arg(p
)) {
849 errmsg(0,"vendor id is missing from -%c %s",ch
, arg
);
851 } else if (str2u16(p
, &fw_header
.vid
) != 0) {
852 errmsg(0,"invalid vendor id: %s", p
);
856 dbgmsg(1, "vendor id is set to 0x%04X", fw_header
.vid
);
858 /* processing device ID*/
860 if (is_empty_arg(p
)) {
861 errmsg(0,"device id is missing from -%c %s",ch
, arg
);
863 } else if (str2u16(p
, &fw_header
.did
) != 0) {
864 errmsg(0,"invalid device id: %s", p
);
868 dbgmsg(1, "device id is set to 0x%04X", fw_header
.did
);
870 /* processing sub vendor ID*/
872 if (is_empty_arg(p
)) {
873 fw_header
.svid
= fw_header
.vid
;
874 } else if (str2u16(p
, &fw_header
.svid
) != 0) {
875 errmsg(0,"invalid sub vendor id: %s", p
);
879 dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header
.svid
);
881 /* processing device ID*/
883 if (is_empty_arg(p
)) {
884 fw_header
.sdid
= fw_header
.did
;
885 } else if (str2u16(p
, &fw_header
.sdid
) != 0) {
886 errmsg(0,"invalid sub device id: %s", p
);
890 dbgmsg(1, "sub device id is set to 0x%04X", fw_header
.sdid
);
892 /* processing revision */
894 if (is_empty_arg(p
)) {
896 } else if (str2u32(arg
, &fw_header
.rev
) != 0) {
897 errmsg(0,"invalid revision number: %s", p
);
901 dbgmsg(1, "board revision is set to 0x%08X", fw_header
.rev
);
911 parse_opt_block(char ch
, char *arg
)
913 char buf
[MAX_ARG_LEN
];
914 char *argv
[MAX_ARG_COUNT
];
919 if (required_arg(ch
, arg
)) {
923 if (fw_num_blocks
>= MAX_FW_BLOCKS
) {
924 errmsg(0,"too many blocks specified");
928 argc
= parse_arg(arg
, buf
, argv
);
929 dbgmsg(1,"processing block option %s, count %d", arg
, argc
);
931 b
= &fw_blocks
[fw_num_blocks
++];
933 /* processing block address */
935 if (is_empty_arg(p
)) {
936 errmsg(0,"no block address specified in %s", arg
);
938 } else if (str2u32(p
, &b
->addr
) != 0) {
939 errmsg(0,"invalid block address: %s", p
);
943 /* processing block length */
945 if (is_empty_arg(p
)) {
946 errmsg(0,"no block length specified in %s", arg
);
948 } else if (str2u32(p
, &b
->blocklen
) != 0) {
949 errmsg(0,"invalid block length: %s", p
);
954 dbgmsg(1,"empty block %s", arg
);
958 /* processing flags */
960 if (is_empty_arg(p
) == 0) {
961 for ( ; *p
!= '\0'; p
++) {
964 b
->flags
|= BLOCK_FLAG_HAVEHDR
;
967 errmsg(0, "invalid block flag \"%c\"", *p
);
973 /* processing file name */
975 if (is_empty_arg(p
)) {
976 errmsg(0,"file name missing in %s", arg
);
981 if (b
->name
== NULL
) {
982 errmsg(0,"not enough memory");
996 parse_opt_partition(char ch
, char *arg
)
998 char buf
[MAX_ARG_LEN
];
999 char *argv
[MAX_ARG_COUNT
];
1001 struct mylo_partition
*part
;
1004 if (required_arg(ch
, arg
)) {
1008 if (fw_num_partitions
>= MYLO_MAX_PARTITIONS
) {
1009 errmsg(0, "too many partitions specified");
1013 fp
= &fw_parts
[fw_num_partitions
++];
1016 parse_arg(arg
, buf
, argv
);
1018 /* processing partition address */
1020 if (is_empty_arg(p
)) {
1021 errmsg(0,"partition address missing in -%c %s",ch
, arg
);
1023 } else if (str2u32(p
, &part
->addr
) != 0) {
1024 errmsg(0,"invalid partition address: %s", p
);
1028 /* processing partition size */
1030 if (is_empty_arg(p
)) {
1031 errmsg(0,"partition size missing in -%c %s",ch
, arg
);
1033 } else if (str2u32(p
, &part
->size
) != 0) {
1034 errmsg(0,"invalid partition size: %s", p
);
1038 /* processing partition flags */
1040 if (is_empty_arg(p
) == 0) {
1041 for ( ; *p
!= '\0'; p
++) {
1044 part
->flags
|= PARTITION_FLAG_ACTIVE
;
1047 part
->flags
|= PARTITION_FLAG_PRELOAD
;
1050 part
->flags
|= PARTITION_FLAG_LZMA
;
1053 part
->flags
|= PARTITION_FLAG_HAVEHDR
;
1056 errmsg(0, "invalid partition flag \"%c\"", *p
);
1062 /* processing partition parameter */
1064 if (is_empty_arg(p
)) {
1065 /* set default partition parameter */
1067 } else if (str2u32(p
, &part
->param
) != 0) {
1068 errmsg(0,"invalid partition parameter: %s", p
);
1073 if (is_empty_arg(p
)) {
1074 /* set default partition parameter */
1077 strncpy(fp
->name
, p
, PART_NAME_LEN
);
1081 if (part
->size
== 0) {
1082 part
->size
= flash_size
- part
->addr
;
1085 /* processing file parameter */
1087 if (is_empty_arg(p
) == 0) {
1090 if (fw_num_blocks
== MAX_FW_BLOCKS
) {
1091 errmsg(0,"too many blocks specified", p
);
1094 b
= &fw_blocks
[fw_num_blocks
++];
1095 b
->name
= strdup(p
);
1096 b
->addr
= part
->addr
;
1097 b
->blocklen
= part
->size
;
1098 if (part
->flags
& PARTITION_FLAG_HAVEHDR
) {
1099 b
->flags
|= BLOCK_FLAG_HAVEHDR
;
1112 parse_opt_board(char ch
, char *arg
)
1114 if (required_arg(ch
, arg
)) {
1118 board
= find_board(arg
);
1120 errmsg(0,"invalid/unknown board specified: %s", arg
);
1124 fw_header
.vid
= board
->vid
;
1125 fw_header
.did
= board
->did
;
1126 fw_header
.svid
= board
->svid
;
1127 fw_header
.sdid
= board
->sdid
;
1129 flash_size
= board
->flash_size
;
1139 parse_opt_rev(char ch
, char *arg
)
1141 if (required_arg(ch
, arg
)) {
1145 if (str2u32(arg
, &fw_header
.rev
) != 0) {
1146 errmsg(0,"invalid revision number: %s", arg
);
1158 main(int argc
, char *argv
[])
1160 int optinvalid
= 0; /* flag for invalid option */
1162 int res
= EXIT_FAILURE
;
1167 progname
=basename(argv
[0]);
1169 memset(&fw_header
, 0, sizeof(fw_header
));
1171 /* init header defaults */
1172 fw_header
.vid
= VENID_COMPEX
;
1173 fw_header
.did
= DEVID_COMPEX_WP54G
;
1174 fw_header
.svid
= VENID_COMPEX
;
1175 fw_header
.sdid
= DEVID_COMPEX_WP54G
;
1176 fw_header
.fwhi
= 0x20000;
1177 fw_header
.fwlo
= 0x20000;
1178 fw_header
.flags
= 0;
1180 opterr
= 0; /* could not print standard getopt error messages */
1181 while ((c
= getopt(argc
, argv
, "b:B:f:hi:p:r:s:v")) != -1) {
1185 optinvalid
= parse_opt_block(c
,optarg
);
1188 optinvalid
= parse_opt_board(c
,optarg
);
1191 optinvalid
= parse_opt_flags(c
,optarg
);
1194 usage(EXIT_SUCCESS
);
1197 optinvalid
= parse_opt_id(c
,optarg
);
1200 optinvalid
= parse_opt_partition(c
,optarg
);
1203 optinvalid
= parse_opt_rev(c
,optarg
);
1206 optinvalid
= parse_opt_size(c
,optarg
);
1215 if (optinvalid
!= 0 ){
1216 errmsg(0, "invalid option: -%c", optopt
);
1221 if (optind
== argc
) {
1222 errmsg(0, "no output file specified");
1226 ofname
= argv
[optind
++];
1228 if (optind
< argc
) {
1229 errmsg(0, "invalid option: %s", argv
[optind
]);
1234 errmsg(0, "no board specified");
1238 if (flash_size
== 0) {
1239 errmsg(0, "no flash size specified");
1243 if (process_files() != 0) {
1247 if (process_partitions() != 0) {
1251 outfile
= fopen(ofname
, "w");
1252 if (outfile
== NULL
) {
1253 errmsg(1, "could not open \"%s\" for writing", ofname
);
1260 if (write_out_header(outfile
, &crc
) != 0)
1263 if (write_out_blocks(outfile
, &crc
) != 0)
1266 fw_header
.crc
= crc
;
1267 if (write_out_header(outfile
, NULL
) != 0)
1270 dbgmsg(1,"Firmware file %s completed.", ofname
);
1277 if (res
!= EXIT_SUCCESS
) {