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 "\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
);
503 * routines to write data to the output file
506 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
507 struct csum_state
*css
)
511 fwrite(data
, len
, 1, outfile
);
513 ERRS("unable to write output file");
518 csum_update(data
, len
, css
);
526 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
527 struct csum_state
*css
)
530 size_t buflen
= sizeof(buf
);
533 memset(buf
, padc
, buflen
);
538 err
= write_out_data(outfile
, buf
, buflen
, css
);
550 block_stat_file(struct csys_block
*block
)
555 if (block
->file_name
== NULL
)
558 err
= stat(block
->file_name
, &st
);
560 ERRS("stat failed on %s", block
->file_name
);
564 block
->file_size
= st
.st_size
;
570 block_writeout_hdr(FILE *outfile
, struct csys_block
*block
)
572 struct csys_header hdr
;
575 if (block
->size_hdr
== 0)
578 /* setup header fields */
579 memcpy(hdr
.sig
, block
->sig
, 4);
580 hdr
.addr
= HOST_TO_LE32(block
->addr
);
581 hdr
.size
= HOST_TO_LE32(block
->size
- block
->size_hdr
- block
->size_csum
);
583 DBG(1,"writing header for block");
584 res
= write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
),NULL
);
591 block_writeout_file(FILE *outfile
, struct csys_block
*block
)
593 char buf
[FILE_BUF_LEN
];
594 size_t buflen
= sizeof(buf
);
599 if (block
->file_name
== NULL
)
602 if (block
->file_size
== 0)
606 f
= fopen(block
->file_name
,"r");
608 ERRS("unable to open file: %s", block
->file_name
);
612 len
= block
->file_size
;
617 /* read data from source file */
619 fread(buf
, buflen
, 1, f
);
621 ERRS("unable to read from file: %s", block
->file_name
);
626 res
= write_out_data(outfile
, buf
, buflen
, block
->css
);
639 block_writeout_data(FILE *outfile
, struct csys_block
*block
)
644 res
= block_writeout_file(outfile
, block
);
648 /* write padding data if neccesary */
649 padlen
= block
->size_avail
- block
->file_size
;
650 DBG(1,"padding block, length=%d", padlen
);
651 res
= write_out_padding(outfile
, padlen
, block
->padc
, block
->css
);
658 block_writeout_csum(FILE *outfile
, struct csys_block
*block
)
663 if (block
->size_csum
== 0)
666 DBG(1,"writing checksum for block");
667 csum
= HOST_TO_LE16(csum_get(block
->css
));
668 res
= write_out_data(outfile
, (uint8_t *)&csum
, block
->size_csum
, NULL
);
675 block_writeout(FILE *outfile
, struct csys_block
*block
)
678 struct csum_state css
;
687 DBG(2, "writing block, file=%s, file_size=%d, space=%d",
688 block
->file_name
, block
->file_size
, block
->size_avail
);
689 res
= block_writeout_hdr(outfile
, block
);
693 if (block
->size_csum
!= 0) {
695 csum_init(&css
, block
->size_csum
);
698 res
= block_writeout_data(outfile
, block
);
702 res
= block_writeout_csum(outfile
, block
);
711 write_out_blocks(FILE *outfile
)
713 struct csys_block
*block
;
716 res
= block_writeout(outfile
, boot_block
);
720 res
= block_writeout(outfile
, conf_block
);
724 res
= block_writeout(outfile
, webp_block
);
728 res
= block_writeout(outfile
, code_block
);
733 for (i
=0; i
< num_blocks
; i
++) {
736 if (block
->type
!= BLOCK_TYPE_XTRA
)
739 res
= block_writeout(outfile
, block
);
749 find_board(char *model
)
751 struct board_info
*ret
;
752 struct board_info
*board
;
755 for (board
= boards
; board
->model
!= NULL
; board
++){
756 if (strcasecmp(model
, board
->model
) == 0) {
767 parse_opt_board(char ch
, char *arg
)
770 DBG(1,"parsing board option: -%c %s", ch
, arg
);
773 ERR("only one board option allowed");
777 if (required_arg(ch
, arg
))
780 board
= find_board(arg
);
782 ERR("invalid/unknown board specified: %s", arg
);
791 parse_opt_block(char ch
, char *arg
)
793 char buf
[MAX_ARG_LEN
];
794 char *argv
[MAX_ARG_COUNT
];
797 struct csys_block
*block
;
800 if ( num_blocks
> MAX_NUM_BLOCKS
) {
801 ERR("too many blocks specified");
805 block
= &blocks
[num_blocks
];
807 /* setup default field values */
808 block
->need_file
= 1;
814 WARN("only one boot block allowed");
817 block
->type
= BLOCK_TYPE_BOOT
;
822 WARN("only one config block allowed");
825 block
->type
= BLOCK_TYPE_CONF
;
830 WARN("only one web block allowed");
833 block
->type
= BLOCK_TYPE_WEBP
;
834 block
->size_hdr
= sizeof(struct csys_header
);
835 block
->size_csum
= CSUM_SIZE_8
;
836 block
->need_file
= 0;
841 WARN("only one runtime block allowed");
844 block
->type
= BLOCK_TYPE_CODE
;
845 block
->size_hdr
= sizeof(struct csys_header
);
846 block
->size_csum
= CSUM_SIZE_16
;
850 block
->type
= BLOCK_TYPE_XTRA
;
853 ERR("unknown block type \"%c\"", ch
);
857 argc
= parse_arg(arg
, buf
, argv
);
861 if (!is_empty_arg(p
)) {
862 block
->file_name
= strdup(p
);
863 if (block
->file_name
== NULL
) {
864 ERR("not enough memory");
867 } else if (block
->need_file
){
868 ERR("no file specified in %s", arg
);
872 if (block
->size_hdr
) {
874 if (!is_empty_arg(p
)) {
875 if (str2u32(p
, &block
->addr
) != 0) {
876 ERR("invalid start address in %s", arg
);
884 if (!is_empty_arg(p
)) {
885 if (str2u32(p
, &block
->align
) != 0) {
886 ERR("invalid alignment value in %s", arg
);
889 block
->align_set
= 1;
893 if (!is_empty_arg(p
) && (str2u8(p
, &block
->padc
) != 0)) {
894 ERR("invalid paddig character in %s", arg
);
907 struct csys_block
*block
;
913 /* collecting stats */
914 for (i
= 0; i
< num_blocks
; i
++) {
916 res
= block_stat_file(block
);
924 block
->size
= board
->boot_size
;
925 if (block
->file_size
> board
->boot_size
) {
926 WARN("boot block is too big");
927 res
= ERR_INVALID_IMAGE
;
930 offs
+= board
->boot_size
;
932 /* configuration data */
935 block
->size
= board
->conf_size
;
936 if (block
->file_size
> board
->conf_size
) {
937 WARN("config block is too big");
938 res
= ERR_INVALID_IMAGE
;
941 offs
+= board
->conf_size
;
947 memcpy(block
->sig
, board
->sig_webp
, 4);
949 if (block
->addr_set
== 0)
950 block
->addr
= board
->addr_webp
;
952 if (block
->align_set
== 0)
953 block
->align
= DEFAULT_BLOCK_ALIGN
;
955 block
->size
= align(offs
+ block
->file_size
+ block
->size_hdr
+
956 block
->size_csum
, block
->align
) - offs
;
958 if (block
->size
> board
->webp_size_max
) {
959 WARN("webpages block is too big");
960 res
= ERR_INVALID_IMAGE
;
963 DBG(2,"webpages start at %08x, size=%08x", offs
,
967 if (offs
> board
->flash_size
) {
968 WARN("webp block is too big");
969 res
= ERR_INVALID_IMAGE
;
976 memcpy(code_block
->sig
, SIG_CSYS
, 4);
978 if (block
->addr_set
== 0)
979 block
->addr
= board
->addr_code
;
981 if (block
->align_set
== 0)
982 block
->align
= DEFAULT_BLOCK_ALIGN
;
984 block
->size
= align(offs
+ block
->file_size
+
985 block
->size_hdr
+ block
->size_csum
,
986 block
->align
) - offs
;
988 DBG(2,"code block start at %08x, size=%08x", offs
,
992 if (offs
> board
->flash_size
) {
993 WARN("code block is too big");
994 res
= ERR_INVALID_IMAGE
;
998 for (i
= 0; i
< num_blocks
; i
++) {
1001 if (block
->type
!= BLOCK_TYPE_XTRA
)
1004 if (block
->align_set
== 0)
1005 block
->align
= DEFAULT_BLOCK_ALIGN
;
1007 block
->size
= align(offs
+ block
->file_size
,
1008 block
->align
) - offs
;
1010 DBG(2,"file %s start at %08x, size=%08x, align=%08x",
1011 block
->file_name
, offs
, block
->size
, block
->align
);
1013 offs
+= block
->size
;
1014 if (offs
> board
->flash_size
) {
1015 WARN("file %s is too big, size=%d, avail=%d",
1016 block
->file_name
, block
->file_size
,
1017 board
->flash_size
- offs
);
1018 res
= ERR_INVALID_IMAGE
;
1022 for (i
= 0; i
< num_blocks
; i
++) {
1025 block
->size_avail
= block
->size
- block
->size_hdr
-
1028 if (block
->size_avail
< block
->file_size
) {
1029 WARN("file %s is too big, size=%d, avail=%d",
1030 block
->file_name
, block
->file_size
,
1032 res
= ERR_INVALID_IMAGE
;
1041 main(int argc
, char *argv
[])
1043 int optinvalid
= 0; /* flag for invalid option */
1045 int res
= ERR_FATAL
;
1049 progname
=basename(argv
[0]);
1051 opterr
= 0; /* could not print standard getopt error messages */
1055 c
= getopt(argc
, argv
, "b:B:c:dhkr:vw:x:");
1064 optinvalid
= parse_opt_block(c
,optarg
);
1067 if (optarg
!= NULL
&& *optarg
== '-') {
1072 optinvalid
= parse_opt_block(c
,optarg
);
1075 invalid_causes_error
= 0;
1078 keep_invalid_images
= 1;
1081 optinvalid
= parse_opt_board(c
,optarg
);
1087 usage(EXIT_SUCCESS
);
1093 if (optinvalid
!= 0 ){
1094 ERR("invalid option: -%c", optopt
);
1099 if (board
== NULL
) {
1100 ERR("no board specified");
1104 if (optind
== argc
) {
1105 ERR("no output file specified");
1109 ofname
= argv
[optind
++];
1111 if (optind
< argc
) {
1112 ERR("invalid option: %s", argv
[optind
]);
1116 res
= process_blocks();
1117 if (res
== ERR_FATAL
)
1120 if (res
== ERR_INVALID_IMAGE
) {
1121 if (invalid_causes_error
)
1124 if (keep_invalid_images
== 0) {
1125 WARN("generation of invalid images disabled", ofname
);
1129 WARN("generating invalid image", ofname
);
1132 outfile
= fopen(ofname
, "w");
1133 if (outfile
== NULL
) {
1134 ERRS("could not open \"%s\" for writing", ofname
);
1139 if (write_out_blocks(outfile
) != 0) {
1144 DBG(1,"Image file %s completed.", ofname
);
1149 if (res
== ERR_FATAL
) {
1153 if (res
== ERR_FATAL
)
1154 return EXIT_FAILURE
;
1156 return EXIT_SUCCESS
;