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-6114WG", "Edimax BR-6114WG", 2, SIG_BR6114WG
),
156 BOARD_ADM("BR-6524K", "Edimax BR-6524K", 2, SIG_BR6524K
),
157 BOARD_ADM("BR-6524KP", "Edimax BR-6524KP", 2, SIG_BR6524KP
),
158 BOARD_ADM("BR-6524WG", "Edimax BR-6524WG", 4, SIG_BR6524WG
),
159 BOARD_ADM("BR-6524WP", "Edimax BR-6524WP", 4, SIG_BR6524WP
),
160 BOARD_ADM("BR-6541K", "Edimax BR-6541K", 2, SIG_BR6541K
),
161 BOARD_ADM("BR-6541KP", "Edimax BR-6541K", 2, SIG_BR6541KP
),
162 BOARD_ADM("BR-6541WP", "Edimax BR-6541WP", 4, SIG_BR6541WP
),
163 BOARD_ADM("EW-7207APg", "Edimax EW-7207APg", 2, SIG_EW7207APg
),
164 BOARD_ADM("PS-1205UWg", "Edimax PS-1205UWg", 2, SIG_PS1205UWg
),
165 BOARD_ADM("PS-3205U", "Edimax PS-3205U", 2, SIG_PS3205U
),
166 BOARD_ADM("PS-3205UWg", "Edimax PS-3205UWg", 2, SIG_PS3205UWg
),
168 /* Hawking products */
169 BOARD_ADM("H2BR4", "Hawking H2BR4", 2, SIG_H2BR4
),
170 BOARD_ADM("H2WR54G", "Hawking H2WR54G", 4, SIG_H2WR54G
),
172 /* Planet products */
173 BOARD_ADM("XRT-401D", "Planet XRT-401D", 2, SIG_XRT401D
),
174 BOARD_ADM("XRT-402D", "Planet XRT-402D", 2, SIG_XRT402D
),
182 #define ERR(fmt, ...) do { \
184 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
187 #define ERRS(fmt, ...) do { \
190 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ \
194 #define WARN(fmt, ...) do { \
195 fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
198 #define DBG(lev, fmt, ...) do { \
199 if (verblevel < lev) \
201 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
205 #define ERR_INVALID_IMAGE -2
210 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
211 struct board_info
*board
;
213 fprintf(stream
, "Usage: %s [OPTIONS...] <file>\n", progname
);
217 " -B <board> create image for the board specified with <board>.\n"
218 " valid <board> values:\n"
220 for (board
= boards
; board
->model
!= NULL
; board
++){
223 board
->model
, board
->name
);
226 " -d don't throw error on invalid images\n"
227 " -k keep invalid images\n"
228 " -b <file>[:<len>[:<padc>]]\n"
229 " add boot code to the image\n"
230 " -c <file>[:<len>[:<padc>]]\n"
231 " add configuration settings to the image\n"
232 " -r <file>:[<addr>][:<len>[:<padc>]]\n"
233 " add runtime code to the image\n"
234 " -w [<file>:[<addr>][:<len>[:<padc>]]]\n"
235 " add webpages to the image\n"
236 " -x <file>[:<len>[:<padc>]]\n"
237 " add extra data at the end of the image\n"
238 " -h show this screen\n"
240 " <file> write output to the file <file>\n"
251 str2u32(char *arg
, uint32_t *val
)
257 t
= strtoul(arg
, &err
, 0);
258 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
268 str2u16(char *arg
, uint16_t *val
)
274 t
= strtoul(arg
, &err
, 0);
275 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
284 str2u8(char *arg
, uint8_t *val
)
290 t
= strtoul(arg
, &err
, 0);
291 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
300 str2sig(char *arg
, uint32_t *sig
)
302 if (strlen(arg
) != 4)
305 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
312 parse_arg(char *arg
, char *buf
, char *argv
[])
320 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
333 if (argl
>= MAX_ARG_LEN
) {
334 /* argument is too long */
335 argl
= MAX_ARG_LEN
-1;
338 memcpy(buf
, arg
, argl
);
341 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
342 tok
= strsep(ap
, ":");
347 else if (tok
[0] == '\0') {
360 required_arg(char c
, char *arg
)
362 if (arg
== NULL
|| *arg
!= '-')
365 ERR("option -%c requires an argument\n", c
);
371 is_empty_arg(char *arg
)
382 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
384 for ( ; len
> 0; len
--) {
391 csum8_get(struct csum_state
*css
)
401 csum16_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
406 t
= css
->tmp
+ (p
[0]<<8);
407 css
->val
+= LE16_TO_HOST(t
);
413 for ( ; len
> 1; len
-= 2, p
+=2 ) {
414 t
= p
[0] + (p
[1] << 8);
415 css
->val
+= LE16_TO_HOST(t
);
426 csum16_get(struct csum_state
*css
)
430 csum16_update(&pad
, 1, css
);
431 return ~css
->val
+ 1;
436 csum_init(struct csum_state
*css
, int size
)
446 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
450 csum8_update(p
,len
,css
);
453 csum16_update(p
,len
,css
);
460 csum_get(struct csum_state
*css
)
466 ret
= csum8_get(css
);
469 ret
= csum16_get(css
);
478 * routines to write data to the output file
481 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
482 struct csum_state
*css
)
486 fwrite(data
, len
, 1, outfile
);
488 ERRS("unable to write output file");
493 csum_update(data
, len
, css
);
501 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
502 struct csum_state
*css
)
505 size_t buflen
= sizeof(buf
);
508 memset(buf
, padc
, buflen
);
513 err
= write_out_data(outfile
, buf
, buflen
, css
);
525 block_stat_file(struct csys_block
*block
)
530 if (block
->file_name
== NULL
)
533 err
= stat(block
->file_name
, &st
);
535 ERRS("stat failed on %s", block
->file_name
);
539 block
->file_size
= st
.st_size
;
545 block_writeout_hdr(FILE *outfile
, struct csys_block
*block
)
547 struct csys_header hdr
;
550 if (block
->size_hdr
== 0)
553 /* setup header fields */
554 memcpy(hdr
.sig
, block
->sig
, 4);
555 hdr
.addr
= HOST_TO_LE32(block
->addr
);
556 hdr
.size
= HOST_TO_LE32(block
->size
-block
->size_hdr
);
558 DBG(1,"writing header for block");
559 res
= write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
),NULL
);
566 block_writeout_file(FILE *outfile
, struct csys_block
*block
)
568 char buf
[FILE_BUF_LEN
];
569 size_t buflen
= sizeof(buf
);
574 if (block
->file_name
== NULL
)
577 if (block
->file_size
== 0)
581 f
= fopen(block
->file_name
,"r");
583 ERRS("unable to open file: %s", block
->file_name
);
587 len
= block
->file_size
;
592 /* read data from source file */
594 fread(buf
, buflen
, 1, f
);
596 ERRS("unable to read from file: %s", block
->file_name
);
601 res
= write_out_data(outfile
, buf
, buflen
, block
->css
);
614 block_writeout_data(FILE *outfile
, struct csys_block
*block
)
619 res
= block_writeout_file(outfile
, block
);
623 /* write padding data if neccesary */
624 padlen
= block
->size_avail
- block
->file_size
;
625 DBG(1,"padding block, length=%d", padlen
);
626 res
= write_out_padding(outfile
, padlen
, block
->padc
, block
->css
);
633 block_writeout_csum(FILE *outfile
, struct csys_block
*block
)
638 if (block
->size_csum
== 0)
641 DBG(1,"writing checksum for block");
642 csum
= HOST_TO_LE16(csum_get(block
->css
));
643 res
= write_out_data(outfile
, (uint8_t *)&csum
, block
->size_csum
, NULL
);
650 block_writeout(FILE *outfile
, struct csys_block
*block
)
653 struct csum_state css
;
662 DBG(2, "writing block, file=%s, file_size=%d, space=%d",
663 block
->file_name
, block
->file_size
, block
->size_avail
);
664 res
= block_writeout_hdr(outfile
, block
);
668 if (block
->size_csum
!= 0) {
670 csum_init(&css
, block
->size_csum
);
673 res
= block_writeout_data(outfile
, block
);
677 res
= block_writeout_csum(outfile
, block
);
686 write_out_blocks(FILE *outfile
)
688 struct csys_block
*block
;
691 res
= block_writeout(outfile
, boot_block
);
695 res
= block_writeout(outfile
, conf_block
);
699 res
= block_writeout(outfile
, webp_block
);
703 res
= block_writeout(outfile
, code_block
);
708 for (i
=0; i
< num_blocks
; i
++) {
711 if (block
->type
!= BLOCK_TYPE_XTRA
)
714 res
= block_writeout(outfile
, block
);
724 find_board(char *model
)
726 struct board_info
*ret
;
727 struct board_info
*board
;
730 for (board
= boards
; board
->model
!= NULL
; board
++){
731 if (strcasecmp(model
, board
->model
) == 0) {
742 parse_opt_board(char ch
, char *arg
)
745 DBG(1,"parsing board option: -%c %s", ch
, arg
);
748 ERR("only one board option allowed");
752 if (required_arg(ch
, arg
))
755 board
= find_board(arg
);
757 ERR("invalid/unknown board specified: %s", arg
);
766 parse_opt_block(char ch
, char *arg
)
768 char buf
[MAX_ARG_LEN
];
769 char *argv
[MAX_ARG_COUNT
];
772 struct csys_block
*block
;
775 if ( num_blocks
> MAX_NUM_BLOCKS
) {
776 ERR("too many blocks specified");
780 block
= &blocks
[num_blocks
];
782 /* setup default field values */
783 block
->need_file
= 1;
789 WARN("only one boot block allowed");
792 block
->type
= BLOCK_TYPE_BOOT
;
797 WARN("only one config block allowed");
800 block
->type
= BLOCK_TYPE_CONF
;
805 WARN("only one web block allowed");
808 block
->type
= BLOCK_TYPE_WEBP
;
809 block
->size_hdr
= sizeof(struct csys_header
);
810 block
->size_csum
= 1;
811 block
->need_file
= 0;
816 WARN("only one runtime block allowed");
819 block
->type
= BLOCK_TYPE_CODE
;
820 block
->size_hdr
= sizeof(struct csys_header
);
821 block
->size_csum
= 2;
825 block
->type
= BLOCK_TYPE_XTRA
;
828 ERR("unknown block type \"%c\"", ch
);
832 argc
= parse_arg(arg
, buf
, argv
);
836 if (!is_empty_arg(p
)) {
837 block
->file_name
= strdup(p
);
838 if (block
->file_name
== NULL
) {
839 ERR("not enough memory");
842 } else if (block
->need_file
){
843 ERR("no file specified in %s", arg
);
847 if (block
->size_hdr
) {
849 if (!is_empty_arg(p
)) {
850 if (str2u32(p
, &block
->addr
) != 0) {
851 ERR("invalid start address in %s", arg
);
859 if (!is_empty_arg(p
)) {
860 if (str2u32(p
, &block
->size
) != 0) {
861 ERR("invalid block size in %s", arg
);
868 if (!is_empty_arg(p
) && (str2u8(p
, &block
->padc
) != 0)) {
869 ERR("invalid paddig character in %s", arg
);
882 struct csys_block
*block
;
888 /* collecting stats */
889 for (i
= 0; i
< num_blocks
; i
++) {
891 res
= block_stat_file(block
);
896 size_avail
= board
->flash_size
;
901 if (block
->size_set
) {
902 board
->boot_size
= block
->size
;
904 block
->size
= board
->boot_size
;
906 if (block
->size
> size_avail
) {
907 WARN("boot block is too big");
908 res
= ERR_INVALID_IMAGE
;
911 size_avail
-= board
->boot_size
;
913 /* configuration data */
916 if (block
->size_set
) {
917 board
->conf_size
= block
->size
;
919 block
->size
= board
->conf_size
;
921 if (block
->size
> size_avail
) {
922 WARN("config block is too big");
923 res
= ERR_INVALID_IMAGE
;
927 size_avail
-= board
->conf_size
;
932 if (block
->size_set
== 0)
933 block
->size
= board
->webp_size
;
934 board
->webp_size
= block
->size
;
935 if (block
->size
> board
->webp_size_max
) {
936 WARN("webpages block is too big");
937 res
= ERR_INVALID_IMAGE
;
939 memcpy(block
->sig
, board
->sig_webp
, 4);
940 if (block
->addr_set
== 0)
941 block
->addr
= board
->addr_webp
;
943 size_avail
-= board
->webp_size
;
948 if (block
->size_set
== 0) {
949 block
->size
=ALIGN(block
->file_size
+ block
->size_hdr
+
950 block
->size_csum
, 0x10000);
952 board
->code_size
= block
->size
;
953 if (board
->code_size
> size_avail
) {
954 WARN("code block is too big");
955 res
= ERR_INVALID_IMAGE
;
957 memcpy(code_block
->sig
, SIG_CSYS
, 4);
958 if (code_block
->addr_set
== 0)
959 code_block
->addr
= board
->addr_code
;
961 size_avail
-= board
->code_size
;
963 for (i
= 0; i
< num_blocks
; i
++) {
966 if (block
->type
!= BLOCK_TYPE_XTRA
)
969 if (block
->size_set
== 0)
970 block
->size
= ALIGN(block
->file_size
, 0x10000);
972 if (block
->size
> size_avail
) {
973 WARN("file %s is too big, size=%d, avail=%d",
974 block
->file_name
, block
->file_size
,
976 res
= ERR_INVALID_IMAGE
;
979 size_avail
-= block
->size
;
982 for (i
= 0; i
< num_blocks
; i
++) {
985 block
->size_avail
= block
->size
- block
->size_hdr
-
988 if (block
->size_avail
< block
->file_size
) {
989 WARN("file %s is too big, size=%d, avail=%d",
990 block
->file_name
, block
->file_size
,
992 res
= ERR_INVALID_IMAGE
;
1001 main(int argc
, char *argv
[])
1003 int optinvalid
= 0; /* flag for invalid option */
1005 int res
= ERR_FATAL
;
1009 progname
=basename(argv
[0]);
1011 opterr
= 0; /* could not print standard getopt error messages */
1015 c
= getopt(argc
, argv
, "b:B:c:dhkr:vw:x:");
1024 optinvalid
= parse_opt_block(c
,optarg
);
1027 if (optarg
!= NULL
&& *optarg
== '-') {
1032 optinvalid
= parse_opt_block(c
,optarg
);
1035 invalid_causes_error
= 0;
1038 keep_invalid_images
= 1;
1041 optinvalid
= parse_opt_board(c
,optarg
);
1047 usage(EXIT_SUCCESS
);
1053 if (optinvalid
!= 0 ){
1054 ERR("invalid option: -%c", optopt
);
1059 if (board
== NULL
) {
1060 ERR("no board specified");
1064 if (optind
== argc
) {
1065 ERR("no output file specified");
1069 ofname
= argv
[optind
++];
1071 if (optind
< argc
) {
1072 ERR("invalid option: %s", argv
[optind
]);
1076 res
= process_blocks();
1077 if (res
== ERR_FATAL
)
1080 if (res
== ERR_INVALID_IMAGE
) {
1081 if (invalid_causes_error
)
1084 if (keep_invalid_images
== 0) {
1085 WARN("generation of invalid images disabled", ofname
);
1089 WARN("generating invalid image", ofname
);
1092 outfile
= fopen(ofname
, "w");
1093 if (outfile
== NULL
) {
1094 ERRS("could not open \"%s\" for writing", ofname
);
1099 if (write_out_blocks(outfile
) != 0) {
1104 DBG(1,"Image file %s completed.", ofname
);
1109 if (res
== ERR_FATAL
) {
1113 if (res
== ERR_FATAL
)
1114 return EXIT_FAILURE
;
1116 return EXIT_SUCCESS
;
This page took 0.131934 seconds and 5 git commands to generate.