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 */
19 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
20 # define HOST_TO_LE16(x) (x)
21 # define HOST_TO_LE32(x) (x)
23 # define HOST_TO_LE16(x) bswap_16(x)
24 # define HOST_TO_LE32(x) bswap_32(x)
29 #define MAX_FW_BLOCKS 32
30 #define MAX_ARG_COUNT 32
31 #define MAX_ARG_LEN 1024
32 #define FILE_BUF_LEN (16*1024)
33 #define PART_NAME_LEN 32
37 uint32_t blocklen
; /* length of the block */
40 char *name
; /* name of the file */
41 uint32_t size
; /* length of the file */
42 uint32_t crc
; /* crc value of the file */
46 struct mylo_partition mylo
;
47 char name
[PART_NAME_LEN
];
50 #define BLOCK_FLAG_HAVEHDR 0x0001
53 char *model
; /* model number*/
54 char *name
; /* model name*/
55 char *desc
; /* description */
56 uint16_t vid
; /* vendor id */
57 uint16_t did
; /* device id */
58 uint16_t svid
; /* sub vendor id */
59 uint16_t sdid
; /* sub device id */
60 uint32_t flash_size
; /* size of flash */
61 uint32_t part_offset
; /* offset of the partition_table */
62 uint32_t part_size
; /* size of the partition_table */
65 #define BOARD(_vid, _did, _svid, _sdid, _flash, _mod, _name, _desc, _po, _ps) { \
66 .model = _mod, .name = _name, .desc = _desc, \
67 .vid = _vid, .did = _did, .svid = _svid, .sdid = _sdid, \
68 .flash_size = (_flash << 20), \
69 .part_offset = _po, .part_size = _ps }
71 #define CPX_BOARD(_did, _flash, _mod, _name, _desc, _po, _ps) \
72 BOARD(VENID_COMPEX, _did, VENID_COMPEX, _did, _flash, _mod, _name, _desc, _po, _ps)
74 #define CPX_BOARD_ADM(_did, _flash, _mod, _name, _desc) \
75 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
77 #define CPX_BOARD_AR71XX(_did, _flash, _mod, _name, _desc) \
78 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x20000, 0x8000)
80 #define CPX_BOARD_AR23XX(_did, _flash, _mod, _name, _desc) \
81 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
83 #define ALIGN(x,y) (((x)+((y)-1)) & ~((y)-1))
88 uint32_t flash_size
= 0;
89 int fw_num_partitions
= 0;
90 int fw_num_blocks
= 0;
93 struct mylo_fw_header fw_header
;
94 struct fw_part fw_parts
[MYLO_MAX_PARTITIONS
];
95 struct fw_block fw_blocks
[MAX_FW_BLOCKS
];
96 struct cpx_board
*board
;
98 struct cpx_board boards
[] = {
99 CPX_BOARD_ADM(DEVID_COMPEX_NP18A
, 4,
100 "NP18A", "Compex NetPassage 18A",
101 "Dualband Wireless A+G Internet Gateway"),
102 CPX_BOARD_ADM(DEVID_COMPEX_NP26G8M
, 2,
103 "NP26G8M", "Compex NetPassage 26G (8M)",
104 "Wireless-G Broadband Multimedia Gateway"),
105 CPX_BOARD_ADM(DEVID_COMPEX_NP26G16M
, 4,
106 "NP26G16M", "Compex NetPassage 26G (16M)",
107 "Wireless-G Broadband Multimedia Gateway"),
108 CPX_BOARD_ADM(DEVID_COMPEX_NP27G
, 4,
109 "NP27G", "Compex NetPassage 27G",
110 "Wireless-G 54Mbps eXtended Range Router"),
111 CPX_BOARD_ADM(DEVID_COMPEX_NP28G
, 4,
112 "NP28G", "Compex NetPassage 28G",
113 "Wireless 108Mbps Super-G XR Multimedia Router with 4 USB Ports"),
114 CPX_BOARD_ADM(DEVID_COMPEX_NP28GHS
, 4,
115 "NP28GHS", "Compex NetPassage 28G (HotSpot)",
117 CPX_BOARD_ADM(DEVID_COMPEX_WP18
, 4,
118 "WP18", "Compex NetPassage WP18",
119 "Wireless-G 54Mbps A+G Dualband Access Point"),
120 CPX_BOARD_ADM(DEVID_COMPEX_WP54G
, 4,
121 "WP54G", "Compex WP54G",
122 "Wireless-G 54Mbps XR Access Point"),
123 CPX_BOARD_ADM(DEVID_COMPEX_WP54Gv1C
, 2,
124 "WP54Gv1C", "Compex WP54G rev.1C",
125 "Wireless-G 54Mbps XR Access Point"),
126 CPX_BOARD_ADM(DEVID_COMPEX_WP54AG
, 4,
127 "WP54AG", "Compex WP54AG",
128 "Wireless-AG 54Mbps XR Access Point"),
129 CPX_BOARD_ADM(DEVID_COMPEX_WPP54G
, 4,
130 "WPP54G", "Compex WPP54G",
131 "Outdoor Access Point"),
132 CPX_BOARD_ADM(DEVID_COMPEX_WPP54AG
, 4,
133 "WPP54AG", "Compex WPP54AG",
134 "Outdoor Access Point"),
136 CPX_BOARD_AR71XX(DEVID_COMPEX_WP543
, 2,
137 "WP543", "Compex WP543",
139 CPX_BOARD_AR71XX(DEVID_COMPEX_WPE72
, 8,
140 "WPE72", "Compex WPE72",
143 CPX_BOARD_AR23XX(DEVID_COMPEX_NP25G
, 4,
144 "NP25G", "Compex NetPassage 25G",
145 "Wireless 54Mbps XR Router"),
146 CPX_BOARD_AR23XX(DEVID_COMPEX_WPE53G
, 4,
147 "WPE53G", "Compex NetPassage 25G",
148 "Wireless 54Mbps XR Access Point"),
153 errmsgv(int syserr
, const char *fmt
, va_list arg_ptr
)
158 fprintf(stderr
, "[%s] Error: ", progname
);
159 vfprintf(stderr
, fmt
, arg_ptr
);
161 fprintf(stderr
, ": %s", strerror(save
));
163 fprintf(stderr
, "\n");
167 errmsg(int syserr
, const char *fmt
, ...)
170 va_start(arg_ptr
, fmt
);
171 errmsgv(syserr
, fmt
, arg_ptr
);
176 dbgmsg(int level
, const char *fmt
, ...)
179 if (verblevel
>= level
) {
181 va_start(arg_ptr
, fmt
);
182 vfprintf(stderr
, fmt
, arg_ptr
);
183 fprintf(stderr
, "\n");
192 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
193 struct cpx_board
*board
;
195 fprintf(stream
, "Usage: %s [OPTION...] <file>\n", progname
);
198 " <file> write output to the <file>\n"
201 " -B <board> create firmware for the board specified with <board>.\n"
202 " This option set vendor id, device id, subvendor id,\n"
203 " subdevice id, and flash size options to the right value.\n"
204 " valid <board> values:\n");
205 for (board
= boards
; board
->model
!= NULL
; board
++){
208 board
->model
, board
->name
);
211 " -i <vid>:<did>[:<svid>[:<sdid>]]\n"
212 " create firmware for board with vendor id <vid>, device\n"
213 " id <did>, subvendor id <svid> and subdevice id <sdid>.\n"
214 " -r <rev> set board revision to <rev>.\n"
215 " -s <size> set flash size to <size>\n"
216 " -b <addr>:<len>[:[<flags>]:<file>]\n"
217 " define block at <addr> with length of <len>.\n"
218 " valid <flag> values:\n"
219 " h : add crc header before the file data.\n"
220 " -p <addr>:<len>[:<flags>[:<param>[:<name>[:<file>]]]]\n"
221 " add partition at <addr>, with size of <len> to the\n"
222 " partition table, set partition name to <name>, partition \n"
223 " flags to <flags> and partition parameter to <param>.\n"
224 " If the <file> is specified content of the file will be \n"
225 " added to the firmware image.\n"
226 " valid <flag> values:\n"
227 " a: this is the active partition. The bootloader loads\n"
228 " the firmware from this partition.\n"
229 " h: the partition data have a header.\n"
230 " l: the partition data uses LZMA compression.\n"
231 " p: the bootloader loads data from this partition to\n"
232 " the RAM before decompress it.\n"
233 " -h show this screen\n"
240 * Code to compute the CRC-32 table. Borrowed from
241 * gzip-1.0.3/makecrc.c.
244 static uint32_t crc_32_tab
[256];
249 /* Not copyrighted 1990 Mark Adler */
251 uint32_t c
; /* crc shift register */
252 uint32_t e
; /* polynomial exclusive-or pattern */
253 int i
; /* counter for all possible eight bit values */
254 int k
; /* byte being shifted into crc apparatus */
256 /* terms of polynomial defining this crc (except x^32): */
257 static const int p
[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
259 /* Make exclusive-or pattern from polynomial */
261 for (i
= 0; i
< sizeof(p
)/sizeof(int); i
++)
262 e
|= 1L << (31 - p
[i
]);
266 for (i
= 1; i
< 256; i
++) {
268 for (k
= i
| 256; k
!= 1; k
>>= 1) {
269 c
= c
& 1 ? (c
>> 1) ^ e
: c
>> 1;
279 update_crc(uint8_t *p
, uint32_t len
, uint32_t *crc
)
283 t
= *crc
^ 0xFFFFFFFFUL
;
285 t
= crc_32_tab
[(t
^ *p
++) & 0xff] ^ (t
>> 8);
287 *crc
= t
^ 0xFFFFFFFFUL
;
292 get_crc(uint8_t *p
, uint32_t len
)
297 update_crc(p
,len
, &crc
);
303 str2u32(char *arg
, uint32_t *val
)
309 t
= strtoul(arg
, &err
, 0);
310 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
320 str2u16(char *arg
, uint16_t *val
)
326 t
= strtoul(arg
, &err
, 0);
327 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
337 find_board(char *model
){
338 struct cpx_board
*board
;
339 struct cpx_board
*tmp
;
342 for (tmp
= boards
; tmp
->model
!= NULL
; tmp
++){
343 if (strcasecmp(model
, tmp
->model
) == 0) {
354 get_file_crc(struct fw_block
*ff
)
357 uint8_t buf
[FILE_BUF_LEN
];
358 uint32_t readlen
= sizeof(buf
);
362 if ((ff
->flags
& BLOCK_FLAG_HAVEHDR
) == 0) {
368 f
= fopen(ff
->name
,"r");
370 errmsg(1,"unable to open file %s", ff
->name
);
381 fread(buf
, readlen
, 1, f
);
383 errmsg(1,"unable to read from file %s", ff
->name
);
387 update_crc(buf
, readlen
, &ff
->crc
);
407 for (i
= 0; i
< fw_num_blocks
; i
++) {
409 if ((b
->addr
+ b
->blocklen
) > flash_size
) {
410 errmsg(0, "block at 0x%08X is too big", b
->addr
);
416 if (stat(b
->name
, &st
) < 0) {
417 errmsg(0, "stat failed on %s",b
->name
);
420 if (b
->blocklen
== 0) {
421 b
->blocklen
= flash_size
- b
->addr
;
423 if (st
.st_size
> b
->blocklen
) {
424 errmsg(0,"file %s is too big",b
->name
);
428 b
->size
= st
.st_size
;
436 process_partitions(void)
438 struct mylo_partition
*part
;
441 for (i
= 0; i
< fw_num_partitions
; i
++) {
442 part
= &fw_parts
[i
].mylo
;
444 if (part
->addr
> flash_size
) {
445 errmsg(0, "invalid partition at 0x%08X", part
->addr
);
449 if ((part
->addr
+ part
->size
) > flash_size
) {
450 errmsg(0, "partition at 0x%08X is too big", part
->addr
);
460 * routines to write data to the output file
463 write_out_data(FILE *outfile
, void *data
, size_t len
, uint32_t *crc
)
469 fwrite(ptr
, len
, 1, outfile
);
471 errmsg(1,"unable to write output file");
476 update_crc(ptr
, len
, crc
);
484 write_out_desc(FILE *outfile
, struct mylo_fw_blockdesc
*desc
, uint32_t *crc
)
486 return write_out_data(outfile
, (uint8_t *)desc
,
492 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
, uint32_t *crc
)
495 size_t buflen
= sizeof(buff
);
497 memset(buff
, padc
, buflen
);
503 if (write_out_data(outfile
, buff
, buflen
, crc
))
514 write_out_file(FILE *outfile
, struct fw_block
*block
, uint32_t *crc
)
516 char buff
[FILE_BUF_LEN
];
517 size_t buflen
= sizeof(buff
);
523 if (block
->name
== NULL
) {
527 if ((block
->flags
& BLOCK_FLAG_HAVEHDR
) != 0) {
528 struct mylo_partition_header ph
;
530 if (get_file_crc(block
) != 0)
533 ph
.crc
= HOST_TO_LE32(block
->crc
);
534 ph
.len
= HOST_TO_LE32(block
->size
);
536 if (write_out_data(outfile
, (uint8_t *)&ph
, sizeof(ph
), crc
) != 0)
540 f
= fopen(block
->name
,"r");
542 errmsg(1,"unable to open file: %s", block
->name
);
551 /* read data from source file */
553 fread(buff
, buflen
, 1, f
);
555 errmsg(1,"unable to read from file: %s",block
->name
);
559 if (write_out_data(outfile
, buff
, buflen
, crc
) != 0)
567 /* align next block on a 4 byte boundary */
568 len
= block
->size
% 4;
569 if (write_out_padding(outfile
, len
, 0xFF, crc
))
572 dbgmsg(1,"file %s written out", block
->name
);
578 write_out_header(FILE *outfile
, uint32_t *crc
)
580 struct mylo_fw_header hdr
;
582 memset(&hdr
, 0, sizeof(hdr
));
584 hdr
.magic
= HOST_TO_LE32(MYLO_MAGIC_FIRMWARE
);
585 hdr
.crc
= HOST_TO_LE32(fw_header
.crc
);
586 hdr
.vid
= HOST_TO_LE16(fw_header
.vid
);
587 hdr
.did
= HOST_TO_LE16(fw_header
.did
);
588 hdr
.svid
= HOST_TO_LE16(fw_header
.svid
);
589 hdr
.sdid
= HOST_TO_LE16(fw_header
.sdid
);
590 hdr
.rev
= HOST_TO_LE32(fw_header
.rev
);
591 hdr
.fwhi
= HOST_TO_LE32(fw_header
.fwhi
);
592 hdr
.fwlo
= HOST_TO_LE32(fw_header
.fwlo
);
593 hdr
.flags
= HOST_TO_LE32(fw_header
.flags
);
595 if (fseek(outfile
, 0, SEEK_SET
) != 0) {
596 errmsg(1,"fseek failed on output file");
600 return write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
), crc
);
605 write_out_partitions(FILE *outfile
, uint32_t *crc
)
607 struct mylo_partition_table p
;
608 char part_names
[MYLO_MAX_PARTITIONS
][PART_NAME_LEN
];
612 if (fw_num_partitions
== 0)
615 memset(&p
, 0, sizeof(p
));
616 memset(part_names
, 0, sizeof(part_names
));
618 p
.magic
= HOST_TO_LE32(MYLO_MAGIC_PARTITIONS
);
619 for (i
= 0; i
< fw_num_partitions
; i
++) {
620 struct mylo_partition
*mp
;
623 mp
= &p
.partitions
[i
];
625 mp
->flags
= HOST_TO_LE16(fp
->mylo
.flags
);
626 mp
->type
= HOST_TO_LE16(PARTITION_TYPE_USED
);
627 mp
->addr
= HOST_TO_LE32(fp
->mylo
.addr
);
628 mp
->size
= HOST_TO_LE32(fp
->mylo
.size
);
629 mp
->param
= HOST_TO_LE32(fp
->mylo
.param
);
631 memcpy(part_names
[i
], fp
->name
, PART_NAME_LEN
);
634 ret
= write_out_data(outfile
, (uint8_t *)&p
, sizeof(p
), crc
);
638 ret
= write_out_data(outfile
, (uint8_t *)part_names
, sizeof(part_names
),
645 write_out_blocks(FILE *outfile
, uint32_t *crc
)
647 struct mylo_fw_blockdesc desc
;
653 * if at least one partition specified, write out block descriptor
654 * for the partition table
656 if (fw_num_partitions
> 0) {
657 desc
.type
= HOST_TO_LE32(FW_DESC_TYPE_USED
);
658 desc
.addr
= HOST_TO_LE32(board
->part_offset
);
659 desc
.dlen
= HOST_TO_LE32(sizeof(struct mylo_partition_table
) +
660 (MYLO_MAX_PARTITIONS
* PART_NAME_LEN
));
661 desc
.blen
= HOST_TO_LE32(board
->part_size
);
663 if (write_out_desc(outfile
, &desc
, crc
) != 0)
668 * write out block descriptors for each files
670 for (i
= 0; i
< fw_num_blocks
; i
++) {
673 /* detect block size */
675 if ((b
->flags
& BLOCK_FLAG_HAVEHDR
) != 0) {
676 dlen
+= sizeof(struct mylo_partition_header
);
679 /* round up to 4 bytes */
680 dlen
= ALIGN(dlen
, 4);
682 /* setup the descriptor */
683 desc
.type
= HOST_TO_LE32(FW_DESC_TYPE_USED
);
684 desc
.addr
= HOST_TO_LE32(b
->addr
);
685 desc
.dlen
= HOST_TO_LE32(dlen
);
686 desc
.blen
= HOST_TO_LE32(b
->blocklen
);
688 if (write_out_desc(outfile
, &desc
, crc
) != 0)
693 * write out the null block descriptor
695 memset(&desc
, 0, sizeof(desc
));
696 if (write_out_desc(outfile
, &desc
, crc
) != 0)
699 if (write_out_partitions(outfile
, crc
) != 0)
703 * write out data for each blocks
705 for (i
= 0; i
< fw_num_blocks
; i
++) {
707 if (write_out_file(outfile
, b
, crc
) != 0)
719 parse_arg(char *arg
, char *buf
, char *argv
[])
728 /* invalid argument string */
738 if (argl
>= MAX_ARG_LEN
) {
739 /* argument is too long */
740 argl
= MAX_ARG_LEN
-1;
743 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
744 memcpy(buf
, arg
, argl
);
747 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
748 tok
= strsep(ap
, ":");
753 else if (tok
[0] == '\0') {
766 required_arg(char c
, char *arg
)
768 if ((optarg
!= NULL
) && (*arg
== '-')){
769 errmsg(0,"option %c requires an argument\n", c
);
778 is_empty_arg(char *arg
)
789 parse_opt_flags(char ch
, char *arg
)
791 if (required_arg(ch
, arg
)) {
795 if (str2u32(arg
, &fw_header
.flags
) != 0) {
796 errmsg(0,"invalid firmware flags: %s", arg
);
800 dbgmsg(1, "firmware flags set to %X bytes", fw_header
.flags
);
810 parse_opt_size(char ch
, char *arg
)
812 if (required_arg(ch
, arg
)) {
816 if (str2u32(arg
, &flash_size
) != 0) {
817 errmsg(0,"invalid flash size: %s", arg
);
821 dbgmsg(1, "flash size set to %d bytes", flash_size
);
831 parse_opt_id(char ch
, char *arg
)
833 char buf
[MAX_ARG_LEN
];
834 char *argv
[MAX_ARG_COUNT
];
837 if (required_arg(ch
, arg
)) {
841 parse_arg(arg
, buf
, argv
);
843 /* processing vendor ID*/
845 if (is_empty_arg(p
)) {
846 errmsg(0,"vendor id is missing from -%c %s",ch
, arg
);
848 } else if (str2u16(p
, &fw_header
.vid
) != 0) {
849 errmsg(0,"invalid vendor id: %s", p
);
853 dbgmsg(1, "vendor id is set to 0x%04X", fw_header
.vid
);
855 /* processing device ID*/
857 if (is_empty_arg(p
)) {
858 errmsg(0,"device id is missing from -%c %s",ch
, arg
);
860 } else if (str2u16(p
, &fw_header
.did
) != 0) {
861 errmsg(0,"invalid device id: %s", p
);
865 dbgmsg(1, "device id is set to 0x%04X", fw_header
.did
);
867 /* processing sub vendor ID*/
869 if (is_empty_arg(p
)) {
870 fw_header
.svid
= fw_header
.vid
;
871 } else if (str2u16(p
, &fw_header
.svid
) != 0) {
872 errmsg(0,"invalid sub vendor id: %s", p
);
876 dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header
.svid
);
878 /* processing device ID*/
880 if (is_empty_arg(p
)) {
881 fw_header
.sdid
= fw_header
.did
;
882 } else if (str2u16(p
, &fw_header
.sdid
) != 0) {
883 errmsg(0,"invalid sub device id: %s", p
);
887 dbgmsg(1, "sub device id is set to 0x%04X", fw_header
.sdid
);
889 /* processing revision */
891 if (is_empty_arg(p
)) {
893 } else if (str2u32(arg
, &fw_header
.rev
) != 0) {
894 errmsg(0,"invalid revision number: %s", p
);
898 dbgmsg(1, "board revision is set to 0x%08X", fw_header
.rev
);
908 parse_opt_block(char ch
, char *arg
)
910 char buf
[MAX_ARG_LEN
];
911 char *argv
[MAX_ARG_COUNT
];
916 if (required_arg(ch
, arg
)) {
920 if (fw_num_blocks
>= MAX_FW_BLOCKS
) {
921 errmsg(0,"too many blocks specified");
925 argc
= parse_arg(arg
, buf
, argv
);
926 dbgmsg(1,"processing block option %s, count %d", arg
, argc
);
928 b
= &fw_blocks
[fw_num_blocks
++];
930 /* processing block address */
932 if (is_empty_arg(p
)) {
933 errmsg(0,"no block address specified in %s", arg
);
935 } else if (str2u32(p
, &b
->addr
) != 0) {
936 errmsg(0,"invalid block address: %s", p
);
940 /* processing block length */
942 if (is_empty_arg(p
)) {
943 errmsg(0,"no block length specified in %s", arg
);
945 } else if (str2u32(p
, &b
->blocklen
) != 0) {
946 errmsg(0,"invalid block length: %s", p
);
951 dbgmsg(1,"empty block %s", arg
);
955 /* processing flags */
957 if (is_empty_arg(p
) == 0) {
958 for ( ; *p
!= '\0'; p
++) {
961 b
->flags
|= BLOCK_FLAG_HAVEHDR
;
964 errmsg(0, "invalid block flag \"%c\"", *p
);
970 /* processing file name */
972 if (is_empty_arg(p
)) {
973 errmsg(0,"file name missing in %s", arg
);
978 if (b
->name
== NULL
) {
979 errmsg(0,"not enough memory");
993 parse_opt_partition(char ch
, char *arg
)
995 char buf
[MAX_ARG_LEN
];
996 char *argv
[MAX_ARG_COUNT
];
998 struct mylo_partition
*part
;
1001 if (required_arg(ch
, arg
)) {
1005 if (fw_num_partitions
>= MYLO_MAX_PARTITIONS
) {
1006 errmsg(0, "too many partitions specified");
1010 fp
= &fw_parts
[fw_num_partitions
++];
1013 parse_arg(arg
, buf
, argv
);
1015 /* processing partition address */
1017 if (is_empty_arg(p
)) {
1018 errmsg(0,"partition address missing in -%c %s",ch
, arg
);
1020 } else if (str2u32(p
, &part
->addr
) != 0) {
1021 errmsg(0,"invalid partition address: %s", p
);
1025 /* processing partition size */
1027 if (is_empty_arg(p
)) {
1028 errmsg(0,"partition size missing in -%c %s",ch
, arg
);
1030 } else if (str2u32(p
, &part
->size
) != 0) {
1031 errmsg(0,"invalid partition size: %s", p
);
1035 /* processing partition flags */
1037 if (is_empty_arg(p
) == 0) {
1038 for ( ; *p
!= '\0'; p
++) {
1041 part
->flags
|= PARTITION_FLAG_ACTIVE
;
1044 part
->flags
|= PARTITION_FLAG_PRELOAD
;
1047 part
->flags
|= PARTITION_FLAG_LZMA
;
1050 part
->flags
|= PARTITION_FLAG_HAVEHDR
;
1053 errmsg(0, "invalid partition flag \"%c\"", *p
);
1059 /* processing partition parameter */
1061 if (is_empty_arg(p
)) {
1062 /* set default partition parameter */
1064 } else if (str2u32(p
, &part
->param
) != 0) {
1065 errmsg(0,"invalid partition parameter: %s", p
);
1070 if (is_empty_arg(p
)) {
1071 /* set default partition parameter */
1074 strncpy(fp
->name
, p
, PART_NAME_LEN
);
1078 if (part
->size
== 0) {
1079 part
->size
= flash_size
- part
->addr
;
1082 /* processing file parameter */
1084 if (is_empty_arg(p
) == 0) {
1087 if (fw_num_blocks
== MAX_FW_BLOCKS
) {
1088 errmsg(0,"too many blocks specified", p
);
1091 b
= &fw_blocks
[fw_num_blocks
++];
1092 b
->name
= strdup(p
);
1093 b
->addr
= part
->addr
;
1094 b
->blocklen
= part
->size
;
1095 if (part
->flags
& PARTITION_FLAG_HAVEHDR
) {
1096 b
->flags
|= BLOCK_FLAG_HAVEHDR
;
1109 parse_opt_board(char ch
, char *arg
)
1111 if (required_arg(ch
, arg
)) {
1115 board
= find_board(arg
);
1117 errmsg(0,"invalid/unknown board specified: %s", arg
);
1121 fw_header
.vid
= board
->vid
;
1122 fw_header
.did
= board
->did
;
1123 fw_header
.svid
= board
->svid
;
1124 fw_header
.sdid
= board
->sdid
;
1126 flash_size
= board
->flash_size
;
1136 parse_opt_rev(char ch
, char *arg
)
1138 if (required_arg(ch
, arg
)) {
1142 if (str2u32(arg
, &fw_header
.rev
) != 0) {
1143 errmsg(0,"invalid revision number: %s", arg
);
1155 main(int argc
, char *argv
[])
1157 int optinvalid
= 0; /* flag for invalid option */
1159 int res
= EXIT_FAILURE
;
1164 progname
=basename(argv
[0]);
1166 memset(&fw_header
, 0, sizeof(fw_header
));
1168 /* init header defaults */
1169 fw_header
.vid
= VENID_COMPEX
;
1170 fw_header
.did
= DEVID_COMPEX_WP54G
;
1171 fw_header
.svid
= VENID_COMPEX
;
1172 fw_header
.sdid
= DEVID_COMPEX_WP54G
;
1173 fw_header
.fwhi
= 0x20000;
1174 fw_header
.fwlo
= 0x20000;
1175 fw_header
.flags
= 0;
1177 opterr
= 0; /* could not print standard getopt error messages */
1178 while ((c
= getopt(argc
, argv
, "b:B:f:hi:p:r:s:v")) != -1) {
1182 optinvalid
= parse_opt_block(c
,optarg
);
1185 optinvalid
= parse_opt_board(c
,optarg
);
1188 optinvalid
= parse_opt_flags(c
,optarg
);
1191 usage(EXIT_SUCCESS
);
1194 optinvalid
= parse_opt_id(c
,optarg
);
1197 optinvalid
= parse_opt_partition(c
,optarg
);
1200 optinvalid
= parse_opt_rev(c
,optarg
);
1203 optinvalid
= parse_opt_size(c
,optarg
);
1212 if (optinvalid
!= 0 ){
1213 errmsg(0, "invalid option: -%c", optopt
);
1218 if (optind
== argc
) {
1219 errmsg(0, "no output file specified");
1223 ofname
= argv
[optind
++];
1225 if (optind
< argc
) {
1226 errmsg(0, "invalid option: %s", argv
[optind
]);
1231 errmsg(0, "no board specified");
1235 if (flash_size
== 0) {
1236 errmsg(0, "no flash size specified");
1240 if (process_files() != 0) {
1244 if (process_partitions() != 0) {
1248 outfile
= fopen(ofname
, "w");
1249 if (outfile
== NULL
) {
1250 errmsg(1, "could not open \"%s\" for writing", ofname
);
1257 if (write_out_header(outfile
, &crc
) != 0)
1260 if (write_out_blocks(outfile
, &crc
) != 0)
1263 fw_header
.crc
= crc
;
1264 if (write_out_header(outfile
, NULL
) != 0)
1267 dbgmsg(1,"Firmware file %s completed.", ofname
);
1274 if (res
!= EXIT_SUCCESS
) {