4 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
6 * This program was based on the code found in various Linux
7 * source tarballs released by Edimax for it's devices.
8 * Original author: David Hsu <davidhsu@realtek.com.tw>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
30 #include <unistd.h> /* for unlink() */
32 #include <getopt.h> /* for getopt() */
36 #include <endian.h> /* for __BYTE_ORDER */
37 #if defined(__CYGWIN__)
38 # include <byteswap.h>
43 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
44 # define HOST_TO_LE16(x) (x)
45 # define HOST_TO_LE32(x) (x)
46 # define LE16_TO_HOST(x) (x)
47 # define LE32_TO_HOST(x) (x)
49 # define HOST_TO_LE16(x) bswap_16(x)
50 # define HOST_TO_LE32(x) bswap_32(x)
51 # define LE16_TO_HOST(x) bswap_16(x)
52 # define LE32_TO_HOST(x) bswap_32(x)
55 #define ALIGN(x,y) ((x)+((y)-1)) & ~((y)-1)
57 #define MAX_NUM_BLOCKS 8
58 #define MAX_ARG_COUNT 32
59 #define MAX_ARG_LEN 1024
60 #define FILE_BUF_LEN (16*1024)
61 #define CSYS_PADC 0xFF
63 #define BLOCK_TYPE_BOOT 0
64 #define BLOCK_TYPE_CONF 1
65 #define BLOCK_TYPE_WEBP 2
66 #define BLOCK_TYPE_CODE 3
67 #define BLOCK_TYPE_XTRA 4
79 int type
; /* type of the block */
82 char *file_name
; /* name of the file */
83 uint32_t file_size
; /* length of the file */
93 unsigned char sig
[SIG_LEN
];
96 struct csum_state
*css
;
105 char sig_boot
[SIG_LEN
];
106 char sig_conf
[SIG_LEN
];
107 char sig_webp
[SIG_LEN
];
112 uint32_t webp_size_max
;
119 #define BOARD(m, n, f, sigb, sigw, bs, cs, ws, ac, aw) {\
120 .model = m, .name = n, .flash_size = f<<20, \
121 .sig_boot = sigb, .sig_conf = SIG_CONF, .sig_webp = sigw, \
122 .boot_size = bs, .conf_size = cs, \
123 .webp_size = ws, .webp_size_max = 3*0x10000, \
124 .addr_code = ac, .addr_webp = aw \
127 #define BOARD_ADM(m,n,f, sigw) BOARD(m,n,f, ADM_BOOT_SIG, sigw, \
128 ADM_BOOT_SIZE, ADM_CONF_SIZE, ADM_WEBP_SIZE, \
129 ADM_CODE_ADDR, ADM_WEBP_ADDR)
138 int invalid_causes_error
= 1;
139 int keep_invalid_images
= 0;
141 struct board_info
*board
= NULL
;
143 struct csys_block
*boot_block
= NULL
;
144 struct csys_block
*conf_block
= NULL
;
145 struct csys_block
*webp_block
= NULL
;
146 struct csys_block
*code_block
= NULL
;
148 struct csys_block blocks
[MAX_NUM_BLOCKS
];
151 static struct board_info boards
[] = {
152 /* The original Edimax products */
153 BOARD_ADM("BR-6104K", "Edimax BR-6104K", 2, SIG_BR6104K
),
154 BOARD_ADM("BR-6104KP", "Edimax BR-6104KP", 2, SIG_BR6104KP
),
155 BOARD_ADM("BR-6104Wg", "Edimax BR-6104Wg", 2, SIG_BR6104Wg
),
156 BOARD_ADM("BR-6114WG", "Edimax BR-6114WG", 2, SIG_BR6114WG
),
157 BOARD_ADM("BR-6524K", "Edimax BR-6524K", 2, SIG_BR6524K
),
158 BOARD_ADM("BR-6524KP", "Edimax BR-6524KP", 2, SIG_BR6524KP
),
159 BOARD_ADM("BR-6524WG", "Edimax BR-6524WG", 4, SIG_BR6524WG
),
160 BOARD_ADM("BR-6524WP", "Edimax BR-6524WP", 4, SIG_BR6524WP
),
161 BOARD_ADM("BR-6541K", "Edimax BR-6541K", 2, SIG_BR6541K
),
162 BOARD_ADM("BR-6541KP", "Edimax BR-6541K", 2, SIG_BR6541KP
),
163 BOARD_ADM("BR-6541WP", "Edimax BR-6541WP", 4, SIG_BR6541WP
),
164 BOARD_ADM("EW-7207APg", "Edimax EW-7207APg", 2, SIG_EW7207APg
),
165 BOARD_ADM("PS-1205UWg", "Edimax PS-1205UWg", 2, SIG_PS1205UWg
),
166 BOARD_ADM("PS-3205U", "Edimax PS-3205U", 2, SIG_PS3205U
),
167 BOARD_ADM("PS-3205UWg", "Edimax PS-3205UWg", 2, SIG_PS3205UWg
),
169 /* Hawking products */
170 BOARD_ADM("H2BR4", "Hawking H2BR4", 2, SIG_H2BR4
),
171 BOARD_ADM("H2WR54G", "Hawking H2WR54G", 4, SIG_H2WR54G
),
173 /* Planet products */
174 BOARD_ADM("XRT-401D", "Planet XRT-401D", 2, SIG_XRT401D
),
175 BOARD_ADM("XRT-402D", "Planet XRT-402D", 2, SIG_XRT402D
),
183 #define ERR(fmt, ...) do { \
185 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
188 #define ERRS(fmt, ...) do { \
191 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ \
195 #define WARN(fmt, ...) do { \
196 fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
199 #define DBG(lev, fmt, ...) do { \
200 if (verblevel < lev) \
202 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
206 #define ERR_INVALID_IMAGE -2
211 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
212 struct board_info
*board
;
214 fprintf(stream
, "Usage: %s [OPTIONS...] <file>\n", progname
);
218 " -B <board> create image for the board specified with <board>.\n"
219 " valid <board> values:\n"
221 for (board
= boards
; board
->model
!= NULL
; board
++){
224 board
->model
, board
->name
);
227 " -d don't throw error on invalid images\n"
228 " -k keep invalid images\n"
229 " -b <file>[:<len>[:<padc>]]\n"
230 " add boot code to the image\n"
231 " -c <file>[:<len>[:<padc>]]\n"
232 " add configuration settings to the image\n"
233 " -r <file>:[<addr>][:<len>[:<padc>]]\n"
234 " add runtime code to the image\n"
235 " -w [<file>:[<addr>][:<len>[:<padc>]]]\n"
236 " add webpages to the image\n"
237 " -x <file>[:<len>[:<padc>]]\n"
238 " add extra data at the end of the image\n"
239 " -h show this screen\n"
241 " <file> write output to the file <file>\n"
252 str2u32(char *arg
, uint32_t *val
)
258 t
= strtoul(arg
, &err
, 0);
259 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
269 str2u16(char *arg
, uint16_t *val
)
275 t
= strtoul(arg
, &err
, 0);
276 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
285 str2u8(char *arg
, uint8_t *val
)
291 t
= strtoul(arg
, &err
, 0);
292 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
301 str2sig(char *arg
, uint32_t *sig
)
303 if (strlen(arg
) != 4)
306 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
313 parse_arg(char *arg
, char *buf
, char *argv
[])
321 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
334 if (argl
>= MAX_ARG_LEN
) {
335 /* argument is too long */
336 argl
= MAX_ARG_LEN
-1;
339 memcpy(buf
, arg
, argl
);
342 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
343 tok
= strsep(ap
, ":");
348 else if (tok
[0] == '\0') {
361 required_arg(char c
, char *arg
)
363 if (arg
== NULL
|| *arg
!= '-')
366 ERR("option -%c requires an argument\n", c
);
372 is_empty_arg(char *arg
)
383 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
385 for ( ; len
> 0; len
--) {
392 csum8_get(struct csum_state
*css
)
402 csum16_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
407 t
= css
->tmp
+ (p
[0]<<8);
408 css
->val
+= LE16_TO_HOST(t
);
414 for ( ; len
> 1; len
-= 2, p
+=2 ) {
415 t
= p
[0] + (p
[1] << 8);
416 css
->val
+= LE16_TO_HOST(t
);
427 csum16_get(struct csum_state
*css
)
431 csum16_update(&pad
, 1, css
);
432 return ~css
->val
+ 1;
437 csum_init(struct csum_state
*css
, int size
)
447 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
451 csum8_update(p
,len
,css
);
454 csum16_update(p
,len
,css
);
461 csum_get(struct csum_state
*css
)
467 ret
= csum8_get(css
);
470 ret
= csum16_get(css
);
479 * routines to write data to the output file
482 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
483 struct csum_state
*css
)
487 fwrite(data
, len
, 1, outfile
);
489 ERRS("unable to write output file");
494 csum_update(data
, len
, css
);
502 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
503 struct csum_state
*css
)
506 size_t buflen
= sizeof(buf
);
509 memset(buf
, padc
, buflen
);
514 err
= write_out_data(outfile
, buf
, buflen
, css
);
526 block_stat_file(struct csys_block
*block
)
531 if (block
->file_name
== NULL
)
534 err
= stat(block
->file_name
, &st
);
536 ERRS("stat failed on %s", block
->file_name
);
540 block
->file_size
= st
.st_size
;
546 block_writeout_hdr(FILE *outfile
, struct csys_block
*block
)
548 struct csys_header hdr
;
551 if (block
->size_hdr
== 0)
554 /* setup header fields */
555 memcpy(hdr
.sig
, block
->sig
, 4);
556 hdr
.addr
= HOST_TO_LE32(block
->addr
);
557 hdr
.size
= HOST_TO_LE32(block
->size
-block
->size_hdr
);
559 DBG(1,"writing header for block");
560 res
= write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
),NULL
);
567 block_writeout_file(FILE *outfile
, struct csys_block
*block
)
569 char buf
[FILE_BUF_LEN
];
570 size_t buflen
= sizeof(buf
);
575 if (block
->file_name
== NULL
)
578 if (block
->file_size
== 0)
582 f
= fopen(block
->file_name
,"r");
584 ERRS("unable to open file: %s", block
->file_name
);
588 len
= block
->file_size
;
593 /* read data from source file */
595 fread(buf
, buflen
, 1, f
);
597 ERRS("unable to read from file: %s", block
->file_name
);
602 res
= write_out_data(outfile
, buf
, buflen
, block
->css
);
615 block_writeout_data(FILE *outfile
, struct csys_block
*block
)
620 res
= block_writeout_file(outfile
, block
);
624 /* write padding data if neccesary */
625 padlen
= block
->size_avail
- block
->file_size
;
626 DBG(1,"padding block, length=%d", padlen
);
627 res
= write_out_padding(outfile
, padlen
, block
->padc
, block
->css
);
634 block_writeout_csum(FILE *outfile
, struct csys_block
*block
)
639 if (block
->size_csum
== 0)
642 DBG(1,"writing checksum for block");
643 csum
= HOST_TO_LE16(csum_get(block
->css
));
644 res
= write_out_data(outfile
, (uint8_t *)&csum
, block
->size_csum
, NULL
);
651 block_writeout(FILE *outfile
, struct csys_block
*block
)
654 struct csum_state css
;
663 DBG(2, "writing block, file=%s, file_size=%d, space=%d",
664 block
->file_name
, block
->file_size
, block
->size_avail
);
665 res
= block_writeout_hdr(outfile
, block
);
669 if (block
->size_csum
!= 0) {
671 csum_init(&css
, block
->size_csum
);
674 res
= block_writeout_data(outfile
, block
);
678 res
= block_writeout_csum(outfile
, block
);
687 write_out_blocks(FILE *outfile
)
689 struct csys_block
*block
;
692 res
= block_writeout(outfile
, boot_block
);
696 res
= block_writeout(outfile
, conf_block
);
700 res
= block_writeout(outfile
, webp_block
);
704 res
= block_writeout(outfile
, code_block
);
709 for (i
=0; i
< num_blocks
; i
++) {
712 if (block
->type
!= BLOCK_TYPE_XTRA
)
715 res
= block_writeout(outfile
, block
);
725 find_board(char *model
)
727 struct board_info
*ret
;
728 struct board_info
*board
;
731 for (board
= boards
; board
->model
!= NULL
; board
++){
732 if (strcasecmp(model
, board
->model
) == 0) {
743 parse_opt_board(char ch
, char *arg
)
746 DBG(1,"parsing board option: -%c %s", ch
, arg
);
749 ERR("only one board option allowed");
753 if (required_arg(ch
, arg
))
756 board
= find_board(arg
);
758 ERR("invalid/unknown board specified: %s", arg
);
767 parse_opt_block(char ch
, char *arg
)
769 char buf
[MAX_ARG_LEN
];
770 char *argv
[MAX_ARG_COUNT
];
773 struct csys_block
*block
;
776 if ( num_blocks
> MAX_NUM_BLOCKS
) {
777 ERR("too many blocks specified");
781 block
= &blocks
[num_blocks
];
783 /* setup default field values */
784 block
->need_file
= 1;
790 WARN("only one boot block allowed");
793 block
->type
= BLOCK_TYPE_BOOT
;
798 WARN("only one config block allowed");
801 block
->type
= BLOCK_TYPE_CONF
;
806 WARN("only one web block allowed");
809 block
->type
= BLOCK_TYPE_WEBP
;
810 block
->size_hdr
= sizeof(struct csys_header
);
811 block
->size_csum
= 1;
812 block
->need_file
= 0;
817 WARN("only one runtime block allowed");
820 block
->type
= BLOCK_TYPE_CODE
;
821 block
->size_hdr
= sizeof(struct csys_header
);
822 block
->size_csum
= 2;
826 block
->type
= BLOCK_TYPE_XTRA
;
829 ERR("unknown block type \"%c\"", ch
);
833 argc
= parse_arg(arg
, buf
, argv
);
837 if (!is_empty_arg(p
)) {
838 block
->file_name
= strdup(p
);
839 if (block
->file_name
== NULL
) {
840 ERR("not enough memory");
843 } else if (block
->need_file
){
844 ERR("no file specified in %s", arg
);
848 if (block
->size_hdr
) {
850 if (!is_empty_arg(p
)) {
851 if (str2u32(p
, &block
->addr
) != 0) {
852 ERR("invalid start address in %s", arg
);
860 if (!is_empty_arg(p
)) {
861 if (str2u32(p
, &block
->size
) != 0) {
862 ERR("invalid block size in %s", arg
);
869 if (!is_empty_arg(p
) && (str2u8(p
, &block
->padc
) != 0)) {
870 ERR("invalid paddig character in %s", arg
);
883 struct csys_block
*block
;
889 /* collecting stats */
890 for (i
= 0; i
< num_blocks
; i
++) {
892 res
= block_stat_file(block
);
897 size_avail
= board
->flash_size
;
902 if (block
->size_set
) {
903 board
->boot_size
= block
->size
;
905 block
->size
= board
->boot_size
;
907 if (block
->size
> size_avail
) {
908 WARN("boot block is too big");
909 res
= ERR_INVALID_IMAGE
;
912 size_avail
-= board
->boot_size
;
914 /* configuration data */
917 if (block
->size_set
) {
918 board
->conf_size
= block
->size
;
920 block
->size
= board
->conf_size
;
922 if (block
->size
> size_avail
) {
923 WARN("config block is too big");
924 res
= ERR_INVALID_IMAGE
;
928 size_avail
-= board
->conf_size
;
933 if (block
->size_set
== 0)
934 block
->size
= board
->webp_size
;
935 board
->webp_size
= block
->size
;
936 if (block
->size
> board
->webp_size_max
) {
937 WARN("webpages block is too big");
938 res
= ERR_INVALID_IMAGE
;
940 memcpy(block
->sig
, board
->sig_webp
, 4);
941 if (block
->addr_set
== 0)
942 block
->addr
= board
->addr_webp
;
944 size_avail
-= board
->webp_size
;
949 if (block
->size_set
== 0) {
950 block
->size
=ALIGN(block
->file_size
+ block
->size_hdr
+
951 block
->size_csum
, 0x10000);
953 board
->code_size
= block
->size
;
954 if (board
->code_size
> size_avail
) {
955 WARN("code block is too big");
956 res
= ERR_INVALID_IMAGE
;
958 memcpy(code_block
->sig
, SIG_CSYS
, 4);
959 if (code_block
->addr_set
== 0)
960 code_block
->addr
= board
->addr_code
;
962 size_avail
-= board
->code_size
;
964 for (i
= 0; i
< num_blocks
; i
++) {
967 if (block
->type
!= BLOCK_TYPE_XTRA
)
970 if (block
->size_set
== 0)
971 block
->size
= ALIGN(block
->file_size
, 0x10000);
973 if (block
->size
> size_avail
) {
974 WARN("file %s is too big, size=%d, avail=%d",
975 block
->file_name
, block
->file_size
,
977 res
= ERR_INVALID_IMAGE
;
980 size_avail
-= block
->size
;
983 for (i
= 0; i
< num_blocks
; i
++) {
986 block
->size_avail
= block
->size
- block
->size_hdr
-
989 if (block
->size_avail
< block
->file_size
) {
990 WARN("file %s is too big, size=%d, avail=%d",
991 block
->file_name
, block
->file_size
,
993 res
= ERR_INVALID_IMAGE
;
1002 main(int argc
, char *argv
[])
1004 int optinvalid
= 0; /* flag for invalid option */
1006 int res
= ERR_FATAL
;
1010 progname
=basename(argv
[0]);
1012 opterr
= 0; /* could not print standard getopt error messages */
1016 c
= getopt(argc
, argv
, "b:B:c:dhkr:vw:x:");
1025 optinvalid
= parse_opt_block(c
,optarg
);
1028 if (optarg
!= NULL
&& *optarg
== '-') {
1033 optinvalid
= parse_opt_block(c
,optarg
);
1036 invalid_causes_error
= 0;
1039 keep_invalid_images
= 1;
1042 optinvalid
= parse_opt_board(c
,optarg
);
1048 usage(EXIT_SUCCESS
);
1054 if (optinvalid
!= 0 ){
1055 ERR("invalid option: -%c", optopt
);
1060 if (board
== NULL
) {
1061 ERR("no board specified");
1065 if (optind
== argc
) {
1066 ERR("no output file specified");
1070 ofname
= argv
[optind
++];
1072 if (optind
< argc
) {
1073 ERR("invalid option: %s", argv
[optind
]);
1077 res
= process_blocks();
1078 if (res
== ERR_FATAL
)
1081 if (res
== ERR_INVALID_IMAGE
) {
1082 if (invalid_causes_error
)
1085 if (keep_invalid_images
== 0) {
1086 WARN("generation of invalid images disabled", ofname
);
1090 WARN("generating invalid image", ofname
);
1093 outfile
= fopen(ofname
, "w");
1094 if (outfile
== NULL
) {
1095 ERRS("could not open \"%s\" for writing", ofname
);
1100 if (write_out_blocks(outfile
) != 0) {
1105 DBG(1,"Image file %s completed.", ofname
);
1110 if (res
== ERR_FATAL
) {
1114 if (res
== ERR_FATAL
)
1115 return EXIT_FAILURE
;
1117 return EXIT_SUCCESS
;
This page took 0.124229 seconds and 5 git commands to generate.