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",
158 CPX_BOARD_AR23XX(DEVID_COMPEX_NP25G
, 4,
159 "NP25G", "Compex NetPassage 25G",
160 "Wireless 54Mbps XR Router"),
161 CPX_BOARD_AR23XX(DEVID_COMPEX_WPE53G
, 4,
162 "WPE53G", "Compex NetPassage 25G",
163 "Wireless 54Mbps XR Access Point"),
168 errmsgv(int syserr
, const char *fmt
, va_list arg_ptr
)
173 fprintf(stderr
, "[%s] Error: ", progname
);
174 vfprintf(stderr
, fmt
, arg_ptr
);
176 fprintf(stderr
, ": %s", strerror(save
));
178 fprintf(stderr
, "\n");
182 errmsg(int syserr
, const char *fmt
, ...)
185 va_start(arg_ptr
, fmt
);
186 errmsgv(syserr
, fmt
, arg_ptr
);
191 dbgmsg(int level
, const char *fmt
, ...)
194 if (verblevel
>= level
) {
196 va_start(arg_ptr
, fmt
);
197 vfprintf(stderr
, fmt
, arg_ptr
);
198 fprintf(stderr
, "\n");
207 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
208 struct cpx_board
*board
;
210 fprintf(stream
, "Usage: %s [OPTION...] <file>\n", progname
);
213 " <file> write output to the <file>\n"
216 " -B <board> create firmware for the board specified with <board>.\n"
217 " This option set vendor id, device id, subvendor id,\n"
218 " subdevice id, and flash size options to the right value.\n"
219 " valid <board> values:\n");
220 for (board
= boards
; board
->model
!= NULL
; board
++){
223 board
->model
, board
->name
);
226 " -i <vid>:<did>[:<svid>[:<sdid>]]\n"
227 " create firmware for board with vendor id <vid>, device\n"
228 " id <did>, subvendor id <svid> and subdevice id <sdid>.\n"
229 " -r <rev> set board revision to <rev>.\n"
230 " -s <size> set flash size to <size>\n"
231 " -b <addr>:<len>[:[<flags>]:<file>]\n"
232 " define block at <addr> with length of <len>.\n"
233 " valid <flag> values:\n"
234 " h : add crc header before the file data.\n"
235 " -p <addr>:<len>[:<flags>[:<param>[:<name>[:<file>]]]]\n"
236 " add partition at <addr>, with size of <len> to the\n"
237 " partition table, set partition name to <name>, partition \n"
238 " flags to <flags> and partition parameter to <param>.\n"
239 " If the <file> is specified content of the file will be \n"
240 " added to the firmware image.\n"
241 " valid <flag> values:\n"
242 " a: this is the active partition. The bootloader loads\n"
243 " the firmware from this partition.\n"
244 " h: the partition data have a header.\n"
245 " l: the partition data uses LZMA compression.\n"
246 " p: the bootloader loads data from this partition to\n"
247 " the RAM before decompress it.\n"
248 " -h show this screen\n"
255 * Code to compute the CRC-32 table. Borrowed from
256 * gzip-1.0.3/makecrc.c.
259 static uint32_t crc_32_tab
[256];
264 /* Not copyrighted 1990 Mark Adler */
266 uint32_t c
; /* crc shift register */
267 uint32_t e
; /* polynomial exclusive-or pattern */
268 int i
; /* counter for all possible eight bit values */
269 int k
; /* byte being shifted into crc apparatus */
271 /* terms of polynomial defining this crc (except x^32): */
272 static const int p
[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
274 /* Make exclusive-or pattern from polynomial */
276 for (i
= 0; i
< sizeof(p
)/sizeof(int); i
++)
277 e
|= 1L << (31 - p
[i
]);
281 for (i
= 1; i
< 256; i
++) {
283 for (k
= i
| 256; k
!= 1; k
>>= 1) {
284 c
= c
& 1 ? (c
>> 1) ^ e
: c
>> 1;
294 update_crc(uint8_t *p
, uint32_t len
, uint32_t *crc
)
298 t
= *crc
^ 0xFFFFFFFFUL
;
300 t
= crc_32_tab
[(t
^ *p
++) & 0xff] ^ (t
>> 8);
302 *crc
= t
^ 0xFFFFFFFFUL
;
307 get_crc(uint8_t *p
, uint32_t len
)
312 update_crc(p
,len
, &crc
);
318 str2u32(char *arg
, uint32_t *val
)
324 t
= strtoul(arg
, &err
, 0);
325 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
335 str2u16(char *arg
, uint16_t *val
)
341 t
= strtoul(arg
, &err
, 0);
342 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
352 find_board(char *model
){
353 struct cpx_board
*board
;
354 struct cpx_board
*tmp
;
357 for (tmp
= boards
; tmp
->model
!= NULL
; tmp
++){
358 if (strcasecmp(model
, tmp
->model
) == 0) {
369 get_file_crc(struct fw_block
*ff
)
372 uint8_t buf
[FILE_BUF_LEN
];
373 uint32_t readlen
= sizeof(buf
);
377 if ((ff
->flags
& BLOCK_FLAG_HAVEHDR
) == 0) {
383 f
= fopen(ff
->name
,"r");
385 errmsg(1,"unable to open file %s", ff
->name
);
396 fread(buf
, readlen
, 1, f
);
398 errmsg(1,"unable to read from file %s", ff
->name
);
402 update_crc(buf
, readlen
, &ff
->crc
);
422 for (i
= 0; i
< fw_num_blocks
; i
++) {
424 if ((b
->addr
+ b
->blocklen
) > flash_size
) {
425 errmsg(0, "block at 0x%08X is too big", b
->addr
);
431 if (stat(b
->name
, &st
) < 0) {
432 errmsg(0, "stat failed on %s",b
->name
);
435 if (b
->blocklen
== 0) {
436 b
->blocklen
= flash_size
- b
->addr
;
438 if (st
.st_size
> b
->blocklen
) {
439 errmsg(0,"file %s is too big",b
->name
);
443 b
->size
= st
.st_size
;
451 process_partitions(void)
453 struct mylo_partition
*part
;
456 for (i
= 0; i
< fw_num_partitions
; i
++) {
457 part
= &fw_parts
[i
].mylo
;
459 if (part
->addr
> flash_size
) {
460 errmsg(0, "invalid partition at 0x%08X", part
->addr
);
464 if ((part
->addr
+ part
->size
) > flash_size
) {
465 errmsg(0, "partition at 0x%08X is too big", part
->addr
);
475 * routines to write data to the output file
478 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
, uint32_t *crc
)
482 fwrite(data
, len
, 1, outfile
);
484 errmsg(1,"unable to write output file");
489 update_crc(data
, len
, crc
);
497 write_out_desc(FILE *outfile
, struct mylo_fw_blockdesc
*desc
, uint32_t *crc
)
499 return write_out_data(outfile
, (uint8_t *)desc
,
505 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
, uint32_t *crc
)
508 size_t buflen
= sizeof(buff
);
510 memset(buff
, padc
, buflen
);
516 if (write_out_data(outfile
, buff
, buflen
, crc
))
527 write_out_file(FILE *outfile
, struct fw_block
*block
, uint32_t *crc
)
529 char buff
[FILE_BUF_LEN
];
530 size_t buflen
= sizeof(buff
);
536 if (block
->name
== NULL
) {
540 if ((block
->flags
& BLOCK_FLAG_HAVEHDR
) != 0) {
541 struct mylo_partition_header ph
;
543 if (get_file_crc(block
) != 0)
546 ph
.crc
= HOST_TO_LE32(block
->crc
);
547 ph
.len
= HOST_TO_LE32(block
->size
);
549 if (write_out_data(outfile
, (uint8_t *)&ph
, sizeof(ph
), crc
) != 0)
553 f
= fopen(block
->name
,"r");
555 errmsg(1,"unable to open file: %s", block
->name
);
564 /* read data from source file */
566 fread(buff
, buflen
, 1, f
);
568 errmsg(1,"unable to read from file: %s",block
->name
);
572 if (write_out_data(outfile
, buff
, buflen
, crc
) != 0)
580 /* align next block on a 4 byte boundary */
581 len
= ALIGN(len
,4) - block
->size
;
582 if (write_out_padding(outfile
, len
, 0xFF, crc
))
585 dbgmsg(1,"file %s written out", block
->name
);
591 write_out_header(FILE *outfile
, uint32_t *crc
)
593 struct mylo_fw_header hdr
;
595 memset(&hdr
, 0, sizeof(hdr
));
597 hdr
.magic
= HOST_TO_LE32(MYLO_MAGIC_FIRMWARE
);
598 hdr
.crc
= HOST_TO_LE32(fw_header
.crc
);
599 hdr
.vid
= HOST_TO_LE16(fw_header
.vid
);
600 hdr
.did
= HOST_TO_LE16(fw_header
.did
);
601 hdr
.svid
= HOST_TO_LE16(fw_header
.svid
);
602 hdr
.sdid
= HOST_TO_LE16(fw_header
.sdid
);
603 hdr
.rev
= HOST_TO_LE32(fw_header
.rev
);
604 hdr
.fwhi
= HOST_TO_LE32(fw_header
.fwhi
);
605 hdr
.fwlo
= HOST_TO_LE32(fw_header
.fwlo
);
606 hdr
.flags
= HOST_TO_LE32(fw_header
.flags
);
608 if (fseek(outfile
, 0, SEEK_SET
) != 0) {
609 errmsg(1,"fseek failed on output file");
613 return write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
), crc
);
618 write_out_partitions(FILE *outfile
, uint32_t *crc
)
620 struct mylo_partition_table p
;
621 char part_names
[MYLO_MAX_PARTITIONS
][PART_NAME_LEN
];
625 if (fw_num_partitions
== 0)
628 memset(&p
, 0, sizeof(p
));
629 memset(part_names
, 0, sizeof(part_names
));
631 p
.magic
= HOST_TO_LE32(MYLO_MAGIC_PARTITIONS
);
632 for (i
= 0; i
< fw_num_partitions
; i
++) {
633 struct mylo_partition
*mp
;
636 mp
= &p
.partitions
[i
];
638 mp
->flags
= HOST_TO_LE16(fp
->mylo
.flags
);
639 mp
->type
= HOST_TO_LE16(PARTITION_TYPE_USED
);
640 mp
->addr
= HOST_TO_LE32(fp
->mylo
.addr
);
641 mp
->size
= HOST_TO_LE32(fp
->mylo
.size
);
642 mp
->param
= HOST_TO_LE32(fp
->mylo
.param
);
644 memcpy(part_names
[i
], fp
->name
, PART_NAME_LEN
);
647 ret
= write_out_data(outfile
, (uint8_t *)&p
, sizeof(p
), crc
);
651 ret
= write_out_data(outfile
, (uint8_t *)part_names
, sizeof(part_names
),
658 write_out_blocks(FILE *outfile
, uint32_t *crc
)
660 struct mylo_fw_blockdesc desc
;
666 * if at least one partition specified, write out block descriptor
667 * for the partition table
669 if (fw_num_partitions
> 0) {
670 desc
.type
= HOST_TO_LE32(FW_DESC_TYPE_USED
);
671 desc
.addr
= HOST_TO_LE32(board
->part_offset
);
672 desc
.dlen
= HOST_TO_LE32(sizeof(struct mylo_partition_table
) +
673 (MYLO_MAX_PARTITIONS
* PART_NAME_LEN
));
674 desc
.blen
= HOST_TO_LE32(board
->part_size
);
676 if (write_out_desc(outfile
, &desc
, crc
) != 0)
681 * write out block descriptors for each files
683 for (i
= 0; i
< fw_num_blocks
; i
++) {
686 /* detect block size */
688 if ((b
->flags
& BLOCK_FLAG_HAVEHDR
) != 0) {
689 dlen
+= sizeof(struct mylo_partition_header
);
692 /* round up to 4 bytes */
693 dlen
= ALIGN(dlen
, 4);
695 /* setup the descriptor */
696 desc
.type
= HOST_TO_LE32(FW_DESC_TYPE_USED
);
697 desc
.addr
= HOST_TO_LE32(b
->addr
);
698 desc
.dlen
= HOST_TO_LE32(dlen
);
699 desc
.blen
= HOST_TO_LE32(b
->blocklen
);
701 if (write_out_desc(outfile
, &desc
, crc
) != 0)
706 * write out the null block descriptor
708 memset(&desc
, 0, sizeof(desc
));
709 if (write_out_desc(outfile
, &desc
, crc
) != 0)
712 if (write_out_partitions(outfile
, crc
) != 0)
716 * write out data for each blocks
718 for (i
= 0; i
< fw_num_blocks
; i
++) {
720 if (write_out_file(outfile
, b
, crc
) != 0)
732 parse_arg(char *arg
, char *buf
, char *argv
[])
741 /* invalid argument string */
751 if (argl
>= MAX_ARG_LEN
) {
752 /* argument is too long */
753 argl
= MAX_ARG_LEN
-1;
756 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
757 memcpy(buf
, arg
, argl
);
760 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
761 tok
= strsep(ap
, ":");
766 else if (tok
[0] == '\0') {
779 required_arg(char c
, char *arg
)
781 if ((optarg
!= NULL
) && (*arg
== '-')){
782 errmsg(0,"option %c requires an argument\n", c
);
791 is_empty_arg(char *arg
)
802 parse_opt_flags(char ch
, char *arg
)
804 if (required_arg(ch
, arg
)) {
808 if (str2u32(arg
, &fw_header
.flags
) != 0) {
809 errmsg(0,"invalid firmware flags: %s", arg
);
813 dbgmsg(1, "firmware flags set to %X bytes", fw_header
.flags
);
823 parse_opt_size(char ch
, char *arg
)
825 if (required_arg(ch
, arg
)) {
829 if (str2u32(arg
, &flash_size
) != 0) {
830 errmsg(0,"invalid flash size: %s", arg
);
834 dbgmsg(1, "flash size set to %d bytes", flash_size
);
844 parse_opt_id(char ch
, char *arg
)
846 char buf
[MAX_ARG_LEN
];
847 char *argv
[MAX_ARG_COUNT
];
851 if (required_arg(ch
, arg
)) {
855 argc
= parse_arg(arg
, buf
, argv
);
857 /* processing vendor ID*/
859 if (is_empty_arg(p
)) {
860 errmsg(0,"vendor id is missing from -%c %s",ch
, arg
);
862 } else if (str2u16(p
, &fw_header
.vid
) != 0) {
863 errmsg(0,"invalid vendor id: %s", p
);
867 dbgmsg(1, "vendor id is set to 0x%04X", fw_header
.vid
);
869 /* processing device ID*/
871 if (is_empty_arg(p
)) {
872 errmsg(0,"device id is missing from -%c %s",ch
, arg
);
874 } else if (str2u16(p
, &fw_header
.did
) != 0) {
875 errmsg(0,"invalid device id: %s", p
);
879 dbgmsg(1, "device id is set to 0x%04X", fw_header
.did
);
881 /* processing sub vendor ID*/
883 if (is_empty_arg(p
)) {
884 fw_header
.svid
= fw_header
.vid
;
885 } else if (str2u16(p
, &fw_header
.svid
) != 0) {
886 errmsg(0,"invalid sub vendor id: %s", p
);
890 dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header
.svid
);
892 /* processing device ID*/
894 if (is_empty_arg(p
)) {
895 fw_header
.sdid
= fw_header
.did
;
896 } else if (str2u16(p
, &fw_header
.sdid
) != 0) {
897 errmsg(0,"invalid sub device id: %s", p
);
901 dbgmsg(1, "sub device id is set to 0x%04X", fw_header
.sdid
);
903 /* processing revision */
905 if (is_empty_arg(p
)) {
907 } else if (str2u32(arg
, &fw_header
.rev
) != 0) {
908 errmsg(0,"invalid revision number: %s", p
);
912 dbgmsg(1, "board revision is set to 0x%08X", fw_header
.rev
);
922 parse_opt_block(char ch
, char *arg
)
924 char buf
[MAX_ARG_LEN
];
925 char *argv
[MAX_ARG_COUNT
];
930 if (required_arg(ch
, arg
)) {
934 if (fw_num_blocks
>= MAX_FW_BLOCKS
) {
935 errmsg(0,"too many blocks specified");
939 argc
= parse_arg(arg
, buf
, argv
);
940 dbgmsg(1,"processing block option %s, count %d", arg
, argc
);
942 b
= &fw_blocks
[fw_num_blocks
++];
944 /* processing block address */
946 if (is_empty_arg(p
)) {
947 errmsg(0,"no block address specified in %s", arg
);
949 } else if (str2u32(p
, &b
->addr
) != 0) {
950 errmsg(0,"invalid block address: %s", p
);
954 /* processing block length */
956 if (is_empty_arg(p
)) {
957 errmsg(0,"no block length specified in %s", arg
);
959 } else if (str2u32(p
, &b
->blocklen
) != 0) {
960 errmsg(0,"invalid block length: %s", p
);
965 dbgmsg(1,"empty block %s", arg
);
969 /* processing flags */
971 if (is_empty_arg(p
) == 0) {
972 for ( ; *p
!= '\0'; p
++) {
975 b
->flags
|= BLOCK_FLAG_HAVEHDR
;
978 errmsg(0, "invalid block flag \"%c\"", *p
);
984 /* processing file name */
986 if (is_empty_arg(p
)) {
987 errmsg(0,"file name missing in %s", arg
);
992 if (b
->name
== NULL
) {
993 errmsg(0,"not enough memory");
1007 parse_opt_partition(char ch
, char *arg
)
1009 char buf
[MAX_ARG_LEN
];
1010 char *argv
[MAX_ARG_COUNT
];
1013 struct mylo_partition
*part
;
1016 if (required_arg(ch
, arg
)) {
1020 if (fw_num_partitions
>= MYLO_MAX_PARTITIONS
) {
1021 errmsg(0, "too many partitions specified");
1025 fp
= &fw_parts
[fw_num_partitions
++];
1028 argc
= parse_arg(arg
, buf
, argv
);
1030 /* processing partition address */
1032 if (is_empty_arg(p
)) {
1033 errmsg(0,"partition address missing in -%c %s",ch
, arg
);
1035 } else if (str2u32(p
, &part
->addr
) != 0) {
1036 errmsg(0,"invalid partition address: %s", p
);
1040 /* processing partition size */
1042 if (is_empty_arg(p
)) {
1043 errmsg(0,"partition size missing in -%c %s",ch
, arg
);
1045 } else if (str2u32(p
, &part
->size
) != 0) {
1046 errmsg(0,"invalid partition size: %s", p
);
1050 /* processing partition flags */
1052 if (is_empty_arg(p
) == 0) {
1053 for ( ; *p
!= '\0'; p
++) {
1056 part
->flags
|= PARTITION_FLAG_ACTIVE
;
1059 part
->flags
|= PARTITION_FLAG_PRELOAD
;
1062 part
->flags
|= PARTITION_FLAG_LZMA
;
1065 part
->flags
|= PARTITION_FLAG_HAVEHDR
;
1068 errmsg(0, "invalid partition flag \"%c\"", *p
);
1074 /* processing partition parameter */
1076 if (is_empty_arg(p
)) {
1077 /* set default partition parameter */
1079 } else if (str2u32(p
, &part
->param
) != 0) {
1080 errmsg(0,"invalid partition parameter: %s", p
);
1085 if (is_empty_arg(p
)) {
1086 /* set default partition parameter */
1089 strncpy(fp
->name
, p
, PART_NAME_LEN
);
1093 if (part
->size
== 0) {
1094 part
->size
= flash_size
- part
->addr
;
1097 /* processing file parameter */
1099 if (is_empty_arg(p
) == 0) {
1102 if (fw_num_blocks
== MAX_FW_BLOCKS
) {
1103 errmsg(0,"too many blocks specified", p
);
1106 b
= &fw_blocks
[fw_num_blocks
++];
1107 b
->name
= strdup(p
);
1108 b
->addr
= part
->addr
;
1109 b
->blocklen
= part
->size
;
1110 if (part
->flags
& PARTITION_FLAG_HAVEHDR
) {
1111 b
->flags
|= BLOCK_FLAG_HAVEHDR
;
1124 parse_opt_board(char ch
, char *arg
)
1126 if (required_arg(ch
, arg
)) {
1130 board
= find_board(arg
);
1132 errmsg(0,"invalid/unknown board specified: %s", arg
);
1136 fw_header
.vid
= board
->vid
;
1137 fw_header
.did
= board
->did
;
1138 fw_header
.svid
= board
->svid
;
1139 fw_header
.sdid
= board
->sdid
;
1141 flash_size
= board
->flash_size
;
1151 parse_opt_rev(char ch
, char *arg
)
1153 if (required_arg(ch
, arg
)) {
1157 if (str2u32(arg
, &fw_header
.rev
) != 0) {
1158 errmsg(0,"invalid revision number: %s", arg
);
1170 main(int argc
, char *argv
[])
1172 int optinvalid
= 0; /* flag for invalid option */
1174 int res
= EXIT_FAILURE
;
1179 progname
=basename(argv
[0]);
1181 memset(&fw_header
, 0, sizeof(fw_header
));
1183 /* init header defaults */
1184 fw_header
.vid
= VENID_COMPEX
;
1185 fw_header
.did
= DEVID_COMPEX_WP54G
;
1186 fw_header
.svid
= VENID_COMPEX
;
1187 fw_header
.sdid
= DEVID_COMPEX_WP54G
;
1188 fw_header
.fwhi
= 0x20000;
1189 fw_header
.fwlo
= 0x20000;
1190 fw_header
.flags
= 0;
1192 opterr
= 0; /* could not print standard getopt error messages */
1193 while ((c
= getopt(argc
, argv
, "b:B:f:hi:p:r:s:v")) != -1) {
1197 optinvalid
= parse_opt_block(c
,optarg
);
1200 optinvalid
= parse_opt_board(c
,optarg
);
1203 optinvalid
= parse_opt_flags(c
,optarg
);
1206 usage(EXIT_SUCCESS
);
1209 optinvalid
= parse_opt_id(c
,optarg
);
1212 optinvalid
= parse_opt_partition(c
,optarg
);
1215 optinvalid
= parse_opt_rev(c
,optarg
);
1218 optinvalid
= parse_opt_size(c
,optarg
);
1227 if (optinvalid
!= 0 ){
1228 errmsg(0, "invalid option: -%c", optopt
);
1233 if (optind
== argc
) {
1234 errmsg(0, "no output file specified");
1238 ofname
= argv
[optind
++];
1240 if (optind
< argc
) {
1241 errmsg(0, "invalid option: %s", argv
[optind
]);
1246 errmsg(0, "no board specified");
1250 if (flash_size
== 0) {
1251 errmsg(0, "no flash size specified");
1255 if (process_files() != 0) {
1259 if (process_partitions() != 0) {
1263 outfile
= fopen(ofname
, "w");
1264 if (outfile
== NULL
) {
1265 errmsg(1, "could not open \"%s\" for writing", ofname
);
1272 if (write_out_header(outfile
, &crc
) != 0)
1275 if (write_out_blocks(outfile
, &crc
) != 0)
1278 fw_header
.crc
= crc
;
1279 if (write_out_header(outfile
, NULL
) != 0)
1282 dbgmsg(1,"Firmware file %s completed.", ofname
);
1289 if (res
!= EXIT_SUCCESS
) {
This page took 0.12766 seconds and 5 git commands to generate.