4 * Copyright (C) 2007 Gabor Juhos <juhosg@freemail.hu>
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)
140 struct board_info
*board
= NULL
;
142 struct csys_block
*boot_block
= NULL
;
143 struct csys_block
*conf_block
= NULL
;
144 struct csys_block
*webp_block
= NULL
;
145 struct csys_block
*code_block
= NULL
;
147 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
),
183 errmsgv(int syserr
, const char *fmt
, va_list arg_ptr
)
188 fprintf(stderr
, "%s: error, ", progname
);
189 vfprintf(stderr
, fmt
, arg_ptr
);
191 fprintf(stderr
, " (%s)", strerror(save
));
193 fprintf(stderr
, "\n");
197 errmsg(int syserr
, const char *fmt
, ...)
200 va_start(arg_ptr
, fmt
);
201 errmsgv(syserr
, fmt
, arg_ptr
);
206 dbgmsg(int level
, const char *fmt
, ...)
209 if (verblevel
>= level
) {
211 va_start(arg_ptr
, fmt
);
212 vfprintf(stderr
, fmt
, arg_ptr
);
213 fprintf(stderr
, "\n");
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 disable check for available space\n"
239 " add boot code to the image\n"
240 " -b <file>[:<len>[:<padc>]]\n"
241 " add boot code to the image\n"
242 " -c <file>[:<len>[:<padc>]]\n"
243 " add configuration settings to the image\n"
244 " -r <file>:[<addr>][:<len>[:<padc>]]\n"
245 " add runtime code to the image\n"
246 " -w [<file>:[<addr>][:<len>[:<padc>]]]\n"
247 " add webpages to the image\n"
248 " -x <file>[:<len>[:<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"
263 str2u32(char *arg
, uint32_t *val
)
269 t
= strtoul(arg
, &err
, 0);
270 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
280 str2u16(char *arg
, uint16_t *val
)
286 t
= strtoul(arg
, &err
, 0);
287 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
296 str2u8(char *arg
, uint8_t *val
)
302 t
= strtoul(arg
, &err
, 0);
303 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
312 str2sig(char *arg
, uint32_t *sig
)
314 if (strlen(arg
) != 4)
317 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
324 parse_arg(char *arg
, char *buf
, char *argv
[])
332 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
345 if (argl
>= MAX_ARG_LEN
) {
346 /* argument is too long */
347 argl
= MAX_ARG_LEN
-1;
350 memcpy(buf
, arg
, argl
);
353 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
354 tok
= strsep(ap
, ":");
359 else if (tok
[0] == '\0') {
372 required_arg(char c
, char *arg
)
374 if (arg
== NULL
|| *arg
!= '-')
377 errmsg(0,"option -%c requires an argument\n", c
);
383 is_empty_arg(char *arg
)
394 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
396 for ( ; len
> 0; len
--) {
403 csum8_get(struct csum_state
*css
)
413 csum16_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
418 t
= css
->tmp
+ (p
[0]<<8);
419 css
->val
+= LE16_TO_HOST(t
);
425 for ( ; len
> 1; len
-= 2, p
+=2 ) {
426 t
= p
[0] + (p
[1] << 8);
427 css
->val
+= LE16_TO_HOST(t
);
438 csum16_get(struct csum_state
*css
)
442 csum16_update(&pad
, 1, css
);
443 return ~css
->val
+ 1;
448 csum_init(struct csum_state
*css
, int size
)
458 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
462 csum8_update(p
,len
,css
);
465 csum16_update(p
,len
,css
);
472 csum_get(struct csum_state
*css
)
478 ret
= csum8_get(css
);
481 ret
= csum16_get(css
);
490 * routines to write data to the output file
493 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
494 struct csum_state
*css
)
498 fwrite(data
, len
, 1, outfile
);
500 errmsg(1,"unable to write output file");
505 csum_update(data
, len
, css
);
513 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
514 struct csum_state
*css
)
517 size_t buflen
= sizeof(buf
);
519 memset(buf
, padc
, buflen
);
524 if (write_out_data(outfile
, buf
, buflen
, css
))
535 block_stat_file(struct csys_block
*block
)
540 if (block
->file_name
== NULL
)
543 res
= stat(block
->file_name
, &st
);
545 errmsg(1, "stat failed on %s", block
->file_name
);
549 block
->file_size
= st
.st_size
;
555 block_writeout_hdr(FILE *outfile
, struct csys_block
*block
)
557 struct csys_header hdr
;
560 if (block
->size_hdr
== 0)
563 /* setup header fields */
564 memcpy(hdr
.sig
, block
->sig
, 4);
565 hdr
.addr
= HOST_TO_LE32(block
->addr
);
566 hdr
.size
= HOST_TO_LE32(block
->size
-block
->size_hdr
);
568 dbgmsg(1,"writing header for block");
569 res
= write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
),NULL
);
576 block_writeout_file(FILE *outfile
, struct csys_block
*block
)
578 char buf
[FILE_BUF_LEN
];
579 size_t buflen
= sizeof(buf
);
584 if (block
->file_name
== NULL
)
587 if (block
->file_size
== 0)
591 f
= fopen(block
->file_name
,"r");
593 errmsg(1,"unable to open file: %s", block
->file_name
);
597 len
= block
->file_size
;
602 /* read data from source file */
604 fread(buf
, buflen
, 1, f
);
606 errmsg(1,"unable to read from file: %s",
612 res
= write_out_data(outfile
, buf
, buflen
, block
->css
);
625 block_writeout_data(FILE *outfile
, struct csys_block
*block
)
630 res
= block_writeout_file(outfile
, block
);
634 /* write padding data if neccesary */
635 padlen
= block
->size_avail
- block
->file_size
;
636 dbgmsg(1,"padding block, length=%d", padlen
);
637 res
= write_out_padding(outfile
, padlen
, block
->padc
, block
->css
);
644 block_writeout_csum(FILE *outfile
, struct csys_block
*block
)
649 if (block
->size_csum
== 0)
652 dbgmsg(1,"writing checksum for block");
653 csum
= HOST_TO_LE16(csum_get(block
->css
));
654 res
= write_out_data(outfile
, (uint8_t *)&csum
, block
->size_csum
, NULL
);
661 block_writeout(FILE *outfile
, struct csys_block
*block
)
664 struct csum_state css
;
673 dbgmsg(2, "writing block, file=%s, file_size=%d, space=%d",
674 block
->file_name
, block
->file_size
, block
->size_avail
);
675 res
= block_writeout_hdr(outfile
, block
);
679 if (block
->size_csum
!= 0) {
681 csum_init(&css
, block
->size_csum
);
684 res
= block_writeout_data(outfile
, block
);
688 res
= block_writeout_csum(outfile
, block
);
697 write_out_blocks(FILE *outfile
)
699 struct csys_block
*block
;
702 res
= block_writeout(outfile
, boot_block
);
706 res
= block_writeout(outfile
, conf_block
);
710 res
= block_writeout(outfile
, webp_block
);
714 res
= block_writeout(outfile
, code_block
);
719 for (i
=0; i
< num_blocks
; i
++) {
722 if (block
->type
!= BLOCK_TYPE_XTRA
)
725 res
= block_writeout(outfile
, block
);
735 find_board(char *model
)
737 struct board_info
*ret
;
738 struct board_info
*board
;
741 for (board
= boards
; board
->model
!= NULL
; board
++){
742 if (strcmp(model
, board
->model
) == 0) {
753 parse_opt_board(char ch
, char *arg
)
756 dbgmsg(1,"parsing board option: -%c %s", ch
, arg
);
759 errmsg(0,"only one board option allowed");
763 if (required_arg(ch
, arg
))
766 board
= find_board(arg
);
768 errmsg(0,"invalid/unknown board specified: %s", arg
);
777 parse_opt_block(char ch
, char *arg
)
779 char buf
[MAX_ARG_LEN
];
780 char *argv
[MAX_ARG_COUNT
];
783 struct csys_block
*block
;
786 if ( num_blocks
> MAX_NUM_BLOCKS
) {
787 errmsg(0,"too many blocks specified");
791 block
= &blocks
[num_blocks
];
793 /* setup default field values */
794 block
->need_file
= 1;
800 errmsg(0,"only one boot block allowed");
803 block
->type
= BLOCK_TYPE_BOOT
;
808 errmsg(0,"only one config block allowed");
811 block
->type
= BLOCK_TYPE_CONF
;
816 errmsg(0,"only one web block allowed");
819 block
->type
= BLOCK_TYPE_WEBP
;
820 block
->size_hdr
= sizeof(struct csys_header
);
821 block
->size_csum
= 1;
822 block
->need_file
= 0;
827 errmsg(0,"only one runtime block allowed");
830 block
->type
= BLOCK_TYPE_CODE
;
831 block
->size_hdr
= sizeof(struct csys_header
);
832 block
->size_csum
= 2;
836 block
->type
= BLOCK_TYPE_XTRA
;
839 errmsg(0,"unknown block type \"%c\"", ch
);
843 argc
= parse_arg(arg
, buf
, argv
);
847 if (!is_empty_arg(p
)) {
848 block
->file_name
= strdup(p
);
849 if (block
->file_name
== NULL
) {
850 errmsg(0,"not enough memory");
853 } else if (block
->need_file
){
854 errmsg(0,"no file specified in %s", arg
);
858 if (block
->size_hdr
) {
860 if (!is_empty_arg(p
)) {
861 if (str2u32(p
, &block
->addr
) != 0) {
862 errmsg(0,"invalid start address in %s", arg
);
870 if (!is_empty_arg(p
)) {
871 if (str2u32(p
, &block
->size
) != 0) {
872 errmsg(0,"invalid block size in %s", arg
);
879 if (!is_empty_arg(p
) && (str2u8(p
, &block
->padc
) != 0)) {
880 errmsg(0,"invalid paddig character in %s", arg
);
893 struct csys_block
*block
;
898 /* collecting stats */
899 for (i
= 0; i
< num_blocks
; i
++) {
901 res
= block_stat_file(block
);
906 size_avail
= board
->flash_size
;
911 if (block
->size_set
) {
912 board
->boot_size
= block
->size
;
914 block
->size
= board
->boot_size
;
916 if (block
->size
> size_avail
) {
917 errmsg(0,"boot block is too big");
921 size_avail
-= board
->boot_size
;
923 /* configuration data */
926 if (block
->size_set
) {
927 board
->conf_size
= block
->size
;
929 block
->size
= board
->conf_size
;
931 if (block
->size
> size_avail
) {
932 errmsg(0,"config block is too big");
937 size_avail
-= board
->conf_size
;
942 if (block
->size_set
== 0)
943 block
->size
= board
->webp_size
;
944 board
->webp_size
= block
->size
;
945 if (block
->size
> board
->webp_size_max
) {
946 errmsg(0,"webpages block is too big");
949 memcpy(block
->sig
, board
->sig_webp
, 4);
950 if (block
->addr_set
== 0)
951 block
->addr
= board
->addr_webp
;
953 size_avail
-= board
->webp_size
;
958 if (block
->size_set
== 0) {
959 block
->size
=ALIGN(block
->file_size
+ block
->size_hdr
+
960 block
->size_csum
, 0x10000);
962 board
->code_size
= block
->size
;
963 if (board
->code_size
> size_avail
) {
964 errmsg(0,"code block is too big");
968 memcpy(code_block
->sig
, SIG_CSYS
, 4);
969 if (code_block
->addr_set
== 0)
970 code_block
->addr
= board
->addr_code
;
972 size_avail
-= board
->code_size
;
974 for (i
= 0; i
< num_blocks
; i
++) {
977 if (block
->type
!= BLOCK_TYPE_XTRA
)
980 if (block
->size_set
== 0)
981 block
->size
= ALIGN(block
->file_size
, 0x10000);
983 if (block
->size
> size_avail
) {
984 errmsg(0,"no space for file %s",
990 size_avail
-= block
->size
;
993 for (i
= 0; i
< num_blocks
; i
++) {
996 block
->size_avail
= block
->size
- block
->size_hdr
-
999 if (block
->size_avail
< block
->file_size
) {
1000 errmsg(0,"file %s is too big, size=%d, avail=%d",
1001 block
->file_name
, block
->file_size
,
1013 main(int argc
, char *argv
[])
1015 int optinvalid
= 0; /* flag for invalid option */
1017 int res
= EXIT_FAILURE
;
1021 progname
=basename(argv
[0]);
1023 opterr
= 0; /* could not print standard getopt error messages */
1027 c
= getopt(argc
, argv
, "b:c:dB:hr:vw:x:");
1036 optinvalid
= parse_opt_block(c
,optarg
);
1039 if (optarg
!= NULL
&& *optarg
== '-') {
1044 optinvalid
= parse_opt_block(c
,optarg
);
1050 optinvalid
= parse_opt_board(c
,optarg
);
1056 usage(EXIT_SUCCESS
);
1062 if (optinvalid
!= 0 ){
1063 errmsg(0, "invalid option: -%c", optopt
);
1068 if (optind
== argc
) {
1069 errmsg(0, "no output file specified");
1073 ofname
= argv
[optind
++];
1075 if (optind
< argc
) {
1076 errmsg(0, "invalid option: %s", argv
[optind
]);
1081 if (board
== NULL
) {
1082 errmsg(0, "no board specified");
1086 if (process_blocks() != 0) {
1090 outfile
= fopen(ofname
, "w");
1091 if (outfile
== NULL
) {
1092 errmsg(1, "could not open \"%s\" for writing", ofname
);
1096 if (write_out_blocks(outfile
) != 0)
1099 dbgmsg(1,"Image file %s completed.", ofname
);
1106 if (res
!= EXIT_SUCCESS
) {
This page took 0.0891189999999999 seconds and 5 git commands to generate.