e156f79153ae6625c93ad6e227b3fc847ec19047
3 * Copyright (C) 2007-2009 Gabor Juhos <juhosg@openwrt.org>
5 * This program was based on the code found in various Linux
6 * source tarballs released by Edimax for it's devices.
7 * Original author: David Hsu <davidhsu@realtek.com.tw>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
29 #include <unistd.h> /* for unlink() */
31 #include <getopt.h> /* for getopt() */
35 #include <endian.h> /* for __BYTE_ORDER */
36 #if defined(__CYGWIN__)
37 # include <byteswap.h>
42 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
43 # define HOST_TO_LE16(x) (x)
44 # define HOST_TO_LE32(x) (x)
45 # define LE16_TO_HOST(x) (x)
46 # define LE32_TO_HOST(x) (x)
48 # define HOST_TO_LE16(x) bswap_16(x)
49 # define HOST_TO_LE32(x) bswap_32(x)
50 # define LE16_TO_HOST(x) bswap_16(x)
51 # define LE32_TO_HOST(x) bswap_32(x)
54 #define MAX_NUM_BLOCKS 8
55 #define MAX_ARG_COUNT 32
56 #define MAX_ARG_LEN 1024
57 #define FILE_BUF_LEN (16*1024)
58 #define CSYS_PADC 0xFF
60 #define BLOCK_TYPE_BOOT 0
61 #define BLOCK_TYPE_CONF 1
62 #define BLOCK_TYPE_WEBP 2
63 #define BLOCK_TYPE_CODE 3
64 #define BLOCK_TYPE_XTRA 4
66 #define DEFAULT_BLOCK_ALIGN 0x10000U
68 #define CSUM_SIZE_NONE 0
70 #define CSUM_SIZE_16 2
82 int type
; /* type of the block */
85 char *file_name
; /* name of the file */
86 uint32_t file_size
; /* length of the file */
88 unsigned char sig
[SIG_LEN
];
100 struct csum_state
*css
;
109 char sig_boot
[SIG_LEN
];
110 char sig_conf
[SIG_LEN
];
111 char sig_webp
[SIG_LEN
];
116 uint32_t webp_size_max
;
123 #define BOARD(m, n, f, sigb, sigw, bs, cs, ws, ac, aw) {\
124 .model = m, .name = n, .flash_size = f<<20, \
125 .sig_boot = sigb, .sig_conf = SIG_CONF, .sig_webp = sigw, \
126 .boot_size = bs, .conf_size = cs, \
127 .webp_size = ws, .webp_size_max = 3*0x10000, \
128 .addr_code = ac, .addr_webp = aw \
131 #define BOARD_ADM(m,n,f, sigw) BOARD(m,n,f, ADM_BOOT_SIG, sigw, \
132 ADM_BOOT_SIZE, ADM_CONF_SIZE, ADM_WEBP_SIZE, \
133 ADM_CODE_ADDR, ADM_WEBP_ADDR)
142 int invalid_causes_error
= 1;
143 int keep_invalid_images
= 0;
145 struct board_info
*board
= NULL
;
147 struct csys_block
*boot_block
= NULL
;
148 struct csys_block
*conf_block
= NULL
;
149 struct csys_block
*webp_block
= NULL
;
150 struct csys_block
*code_block
= NULL
;
152 struct csys_block blocks
[MAX_NUM_BLOCKS
];
155 static struct board_info boards
[] = {
156 /* The original Edimax products */
157 BOARD_ADM("BR-6104K", "Edimax BR-6104K", 2, SIG_BR6104K
),
158 BOARD_ADM("BR-6104KP", "Edimax BR-6104KP", 2, SIG_BR6104KP
),
159 BOARD_ADM("BR-6104Wg", "Edimax BR-6104Wg", 2, SIG_BR6104Wg
),
160 BOARD_ADM("BR-6114WG", "Edimax BR-6114WG", 2, SIG_BR6114WG
),
161 BOARD_ADM("BR-6524K", "Edimax BR-6524K", 2, SIG_BR6524K
),
162 BOARD_ADM("BR-6524KP", "Edimax BR-6524KP", 2, SIG_BR6524KP
),
163 BOARD_ADM("BR-6524N", "Edimax BR-6524N", 2, SIG_BR6524N
),
164 BOARD_ADM("BR-6524WG", "Edimax BR-6524WG", 4, SIG_BR6524WG
),
165 BOARD_ADM("BR-6524WP", "Edimax BR-6524WP", 4, SIG_BR6524WP
),
166 BOARD_ADM("BR-6541K", "Edimax BR-6541K", 2, SIG_BR6541K
),
167 BOARD_ADM("BR-6541KP", "Edimax BR-6541K", 2, SIG_BR6541KP
),
168 BOARD_ADM("BR-6541WP", "Edimax BR-6541WP", 4, SIG_BR6541WP
),
169 BOARD_ADM("EW-7207APg", "Edimax EW-7207APg", 2, SIG_EW7207APg
),
170 BOARD_ADM("PS-1205UWg", "Edimax PS-1205UWg", 2, SIG_PS1205UWg
),
171 BOARD_ADM("PS-3205U", "Edimax PS-3205U", 2, SIG_PS3205U
),
172 BOARD_ADM("PS-3205UWg", "Edimax PS-3205UWg", 2, SIG_PS3205UWg
),
174 /* Hawking products */
175 BOARD_ADM("H2BR4", "Hawking H2BR4", 2, SIG_H2BR4
),
176 BOARD_ADM("H2WR54G", "Hawking H2WR54G", 4, SIG_H2WR54G
),
178 /* Planet products */
179 BOARD_ADM("XRT-401D", "Planet XRT-401D", 2, SIG_XRT401D
),
180 BOARD_ADM("XRT-402D", "Planet XRT-402D", 2, SIG_XRT402D
),
182 /* Conceptronic products */
183 BOARD_ADM("C54BSR4", "Conceptronic C54BSR4", 2, SIG_C54BSR4
),
185 /* OSBRiDGE products */
186 BOARD_ADM("5GXi", "OSBDRiDGE 5GXi", 2, SIG_5GXI
),
194 #define ERR(fmt, ...) do { \
196 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
199 #define ERRS(fmt, ...) do { \
202 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", progname, ## __VA_ARGS__ \
206 #define WARN(fmt, ...) do { \
207 fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
210 #define DBG(lev, fmt, ...) do { \
211 if (verblevel < lev) \
213 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
217 #define ERR_INVALID_IMAGE -2
222 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
223 struct board_info
*board
;
225 fprintf(stream
, "Usage: %s [OPTIONS...] <file>\n", progname
);
229 " -B <board> create image for the board specified with <board>.\n"
230 " valid <board> values:\n"
232 for (board
= boards
; board
->model
!= NULL
; board
++){
235 board
->model
, board
->name
);
238 " -d don't throw error on invalid images\n"
239 " -k keep invalid images\n"
240 " -b <file>[:<align>[:<padc>]]\n"
241 " add boot code to the image\n"
242 " -c <file>[:<align>[:<padc>]]\n"
243 " add configuration settings to the image\n"
244 " -r <file>:[<addr>][:<align>[:<padc>]]\n"
245 " add runtime code to the image\n"
246 " -w [<file>:[<addr>][:<align>[:<padc>]]]\n"
247 " add webpages to the image\n"
248 " -x <file>[:<align>[:<padc>]]\n"
249 " add extra data at the end of the image\n"
250 " -h show this screen\n"
252 " <file> write output to the file <file>\n"
258 static inline uint32_t align(uint32_t base
, uint32_t alignment
)
263 ret
= (base
+ alignment
- 1);
264 ret
&= ~(alignment
-1);
276 str2u32(char *arg
, uint32_t *val
)
282 t
= strtoul(arg
, &err
, 0);
283 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
293 str2u16(char *arg
, uint16_t *val
)
299 t
= strtoul(arg
, &err
, 0);
300 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
309 str2u8(char *arg
, uint8_t *val
)
315 t
= strtoul(arg
, &err
, 0);
316 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
325 str2sig(char *arg
, uint32_t *sig
)
327 if (strlen(arg
) != 4)
330 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
337 parse_arg(char *arg
, char *buf
, char *argv
[])
345 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
358 if (argl
>= MAX_ARG_LEN
) {
359 /* argument is too long */
360 argl
= MAX_ARG_LEN
-1;
363 memcpy(buf
, arg
, argl
);
366 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
367 tok
= strsep(ap
, ":");
372 else if (tok
[0] == '\0') {
385 required_arg(char c
, char *arg
)
387 if (arg
== NULL
|| *arg
!= '-')
390 ERR("option -%c requires an argument\n", c
);
396 is_empty_arg(char *arg
)
407 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
409 for ( ; len
> 0; len
--) {
416 csum8_get(struct csum_state
*css
)
426 csum16_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
431 t
= css
->tmp
+ (p
[0]<<8);
432 css
->val
+= LE16_TO_HOST(t
);
438 for ( ; len
> 1; len
-= 2, p
+=2 ) {
439 t
= p
[0] + (p
[1] << 8);
440 css
->val
+= LE16_TO_HOST(t
);
451 csum16_get(struct csum_state
*css
)
455 csum16_update(&pad
, 1, css
);
456 return ~css
->val
+ 1;
461 csum_init(struct csum_state
*css
, int size
)
471 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
475 csum8_update(p
,len
,css
);
478 csum16_update(p
,len
,css
);
485 csum_get(struct csum_state
*css
)
491 ret
= csum8_get(css
);
494 ret
= csum16_get(css
);
497 ERR("invalid checksum size\n");
506 * routines to write data to the output file
509 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
510 struct csum_state
*css
)
514 fwrite(data
, len
, 1, outfile
);
516 ERRS("unable to write output file");
521 csum_update(data
, len
, css
);
529 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
530 struct csum_state
*css
)
533 size_t buflen
= sizeof(buf
);
536 memset(buf
, padc
, buflen
);
541 err
= write_out_data(outfile
, buf
, buflen
, css
);
553 block_stat_file(struct csys_block
*block
)
558 if (block
->file_name
== NULL
)
561 err
= stat(block
->file_name
, &st
);
563 ERRS("stat failed on %s", block
->file_name
);
567 block
->file_size
= st
.st_size
;
573 block_writeout_hdr(FILE *outfile
, struct csys_block
*block
)
575 struct csys_header hdr
;
578 if (block
->size_hdr
== 0)
581 /* setup header fields */
582 memcpy(hdr
.sig
, block
->sig
, 4);
583 hdr
.addr
= HOST_TO_LE32(block
->addr
);
584 hdr
.size
= HOST_TO_LE32(block
->size
- block
->size_hdr
- block
->size_csum
);
586 DBG(1,"writing header for block");
587 res
= write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
),NULL
);
594 block_writeout_file(FILE *outfile
, struct csys_block
*block
)
596 char buf
[FILE_BUF_LEN
];
597 size_t buflen
= sizeof(buf
);
602 if (block
->file_name
== NULL
)
605 if (block
->file_size
== 0)
609 f
= fopen(block
->file_name
,"r");
611 ERRS("unable to open file: %s", block
->file_name
);
615 len
= block
->file_size
;
620 /* read data from source file */
622 fread(buf
, buflen
, 1, f
);
624 ERRS("unable to read from file: %s", block
->file_name
);
629 res
= write_out_data(outfile
, buf
, buflen
, block
->css
);
642 block_writeout_data(FILE *outfile
, struct csys_block
*block
)
647 res
= block_writeout_file(outfile
, block
);
651 /* write padding data if neccesary */
652 padlen
= block
->size_avail
- block
->file_size
;
653 DBG(1,"padding block, length=%zu", padlen
);
654 res
= write_out_padding(outfile
, padlen
, block
->padc
, block
->css
);
661 block_writeout_csum(FILE *outfile
, struct csys_block
*block
)
666 if (block
->size_csum
== 0)
669 DBG(1,"writing checksum for block");
670 csum
= HOST_TO_LE16(csum_get(block
->css
));
671 res
= write_out_data(outfile
, (uint8_t *)&csum
, block
->size_csum
, NULL
);
678 block_writeout(FILE *outfile
, struct csys_block
*block
)
681 struct csum_state css
;
690 DBG(2, "writing block, file=%s, file_size=%d, space=%d",
691 block
->file_name
, block
->file_size
, block
->size_avail
);
692 res
= block_writeout_hdr(outfile
, block
);
696 if (block
->size_csum
!= 0) {
698 csum_init(&css
, block
->size_csum
);
701 res
= block_writeout_data(outfile
, block
);
705 res
= block_writeout_csum(outfile
, block
);
714 write_out_blocks(FILE *outfile
)
716 struct csys_block
*block
;
719 res
= block_writeout(outfile
, boot_block
);
723 res
= block_writeout(outfile
, conf_block
);
727 res
= block_writeout(outfile
, webp_block
);
731 res
= block_writeout(outfile
, code_block
);
736 for (i
=0; i
< num_blocks
; i
++) {
739 if (block
->type
!= BLOCK_TYPE_XTRA
)
742 res
= block_writeout(outfile
, block
);
752 find_board(char *model
)
754 struct board_info
*ret
;
755 struct board_info
*board
;
758 for (board
= boards
; board
->model
!= NULL
; board
++){
759 if (strcasecmp(model
, board
->model
) == 0) {
770 parse_opt_board(char ch
, char *arg
)
773 DBG(1,"parsing board option: -%c %s", ch
, arg
);
776 ERR("only one board option allowed");
780 if (required_arg(ch
, arg
))
783 board
= find_board(arg
);
785 ERR("invalid/unknown board specified: %s", arg
);
794 parse_opt_block(char ch
, char *arg
)
796 char buf
[MAX_ARG_LEN
];
797 char *argv
[MAX_ARG_COUNT
];
799 struct csys_block
*block
;
802 if ( num_blocks
> MAX_NUM_BLOCKS
) {
803 ERR("too many blocks specified");
807 block
= &blocks
[num_blocks
];
809 /* setup default field values */
810 block
->need_file
= 1;
816 WARN("only one boot block allowed");
819 block
->type
= BLOCK_TYPE_BOOT
;
824 WARN("only one config block allowed");
827 block
->type
= BLOCK_TYPE_CONF
;
832 WARN("only one web block allowed");
835 block
->type
= BLOCK_TYPE_WEBP
;
836 block
->size_hdr
= sizeof(struct csys_header
);
837 block
->size_csum
= CSUM_SIZE_8
;
838 block
->need_file
= 0;
843 WARN("only one runtime block allowed");
846 block
->type
= BLOCK_TYPE_CODE
;
847 block
->size_hdr
= sizeof(struct csys_header
);
848 block
->size_csum
= CSUM_SIZE_16
;
852 block
->type
= BLOCK_TYPE_XTRA
;
855 ERR("unknown block type \"%c\"", ch
);
859 parse_arg(arg
, buf
, argv
);
863 if (!is_empty_arg(p
)) {
864 block
->file_name
= strdup(p
);
865 if (block
->file_name
== NULL
) {
866 ERR("not enough memory");
869 } else if (block
->need_file
){
870 ERR("no file specified in %s", arg
);
874 if (block
->size_hdr
) {
876 if (!is_empty_arg(p
)) {
877 if (str2u32(p
, &block
->addr
) != 0) {
878 ERR("invalid start address in %s", arg
);
886 if (!is_empty_arg(p
)) {
887 if (str2u32(p
, &block
->align
) != 0) {
888 ERR("invalid alignment value in %s", arg
);
891 block
->align_set
= 1;
895 if (!is_empty_arg(p
) && (str2u8(p
, &block
->padc
) != 0)) {
896 ERR("invalid paddig character in %s", arg
);
909 struct csys_block
*block
;
915 /* collecting stats */
916 for (i
= 0; i
< num_blocks
; i
++) {
918 res
= block_stat_file(block
);
926 block
->size
= board
->boot_size
;
927 if (block
->file_size
> board
->boot_size
) {
928 WARN("boot block is too big");
929 res
= ERR_INVALID_IMAGE
;
932 offs
+= board
->boot_size
;
934 /* configuration data */
937 block
->size
= board
->conf_size
;
938 if (block
->file_size
> board
->conf_size
) {
939 WARN("config block is too big");
940 res
= ERR_INVALID_IMAGE
;
943 offs
+= board
->conf_size
;
949 memcpy(block
->sig
, board
->sig_webp
, 4);
951 if (block
->addr_set
== 0)
952 block
->addr
= board
->addr_webp
;
954 if (block
->align_set
== 0)
955 block
->align
= DEFAULT_BLOCK_ALIGN
;
957 block
->size
= align(offs
+ block
->file_size
+ block
->size_hdr
+
958 block
->size_csum
, block
->align
) - offs
;
960 if (block
->size
> board
->webp_size_max
) {
961 WARN("webpages block is too big");
962 res
= ERR_INVALID_IMAGE
;
965 DBG(2,"webpages start at %08x, size=%08x", offs
,
969 if (offs
> board
->flash_size
) {
970 WARN("webp block is too big");
971 res
= ERR_INVALID_IMAGE
;
978 memcpy(code_block
->sig
, SIG_CSYS
, 4);
980 if (block
->addr_set
== 0)
981 block
->addr
= board
->addr_code
;
983 if (block
->align_set
== 0)
984 block
->align
= DEFAULT_BLOCK_ALIGN
;
986 block
->size
= align(offs
+ block
->file_size
+
987 block
->size_hdr
+ block
->size_csum
,
988 block
->align
) - offs
;
990 DBG(2,"code block start at %08x, size=%08x", offs
,
994 if (offs
> board
->flash_size
) {
995 WARN("code block is too big");
996 res
= ERR_INVALID_IMAGE
;
1000 for (i
= 0; i
< num_blocks
; i
++) {
1003 if (block
->type
!= BLOCK_TYPE_XTRA
)
1006 if (block
->align_set
== 0)
1007 block
->align
= DEFAULT_BLOCK_ALIGN
;
1009 block
->size
= align(offs
+ block
->file_size
,
1010 block
->align
) - offs
;
1012 DBG(2,"file %s start at %08x, size=%08x, align=%08x",
1013 block
->file_name
, offs
, block
->size
, block
->align
);
1015 offs
+= block
->size
;
1016 if (offs
> board
->flash_size
) {
1017 WARN("file %s is too big, size=%d, avail=%d",
1018 block
->file_name
, block
->file_size
,
1019 board
->flash_size
- offs
);
1020 res
= ERR_INVALID_IMAGE
;
1024 for (i
= 0; i
< num_blocks
; i
++) {
1027 block
->size_avail
= block
->size
- block
->size_hdr
-
1030 if (block
->size_avail
< block
->file_size
) {
1031 WARN("file %s is too big, size=%d, avail=%d",
1032 block
->file_name
, block
->file_size
,
1034 res
= ERR_INVALID_IMAGE
;
1043 main(int argc
, char *argv
[])
1045 int optinvalid
= 0; /* flag for invalid option */
1047 int res
= ERR_FATAL
;
1051 progname
=basename(argv
[0]);
1053 opterr
= 0; /* could not print standard getopt error messages */
1057 c
= getopt(argc
, argv
, "b:B:c:dhkr:vw:x:");
1066 optinvalid
= parse_opt_block(c
,optarg
);
1069 if (optarg
!= NULL
&& *optarg
== '-') {
1074 optinvalid
= parse_opt_block(c
,optarg
);
1077 invalid_causes_error
= 0;
1080 keep_invalid_images
= 1;
1083 optinvalid
= parse_opt_board(c
,optarg
);
1089 usage(EXIT_SUCCESS
);
1095 if (optinvalid
!= 0 ){
1096 ERR("invalid option: -%c", optopt
);
1101 if (board
== NULL
) {
1102 ERR("no board specified");
1106 if (optind
== argc
) {
1107 ERR("no output file specified");
1111 ofname
= argv
[optind
++];
1113 if (optind
< argc
) {
1114 ERR("invalid option: %s", argv
[optind
]);
1118 res
= process_blocks();
1119 if (res
== ERR_FATAL
)
1122 if (res
== ERR_INVALID_IMAGE
) {
1123 if (invalid_causes_error
)
1126 if (keep_invalid_images
== 0) {
1127 WARN("generation of invalid images \"%s\" disabled", ofname
);
1131 WARN("generating invalid image: \"%s\"", ofname
);
1134 outfile
= fopen(ofname
, "w");
1135 if (outfile
== NULL
) {
1136 ERRS("could not open \"%s\" for writing", ofname
);
1141 if (write_out_blocks(outfile
) != 0) {
1146 DBG(1,"Image file %s completed.", ofname
);
1151 if (res
== ERR_FATAL
) {
1155 if (res
== ERR_FATAL
)
1156 return EXIT_FAILURE
;
1158 return EXIT_SUCCESS
;