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-6524WG", "Edimax BR-6524WG", 4, SIG_BR6524WG
),
164 BOARD_ADM("BR-6524WP", "Edimax BR-6524WP", 4, SIG_BR6524WP
),
165 BOARD_ADM("BR-6541K", "Edimax BR-6541K", 2, SIG_BR6541K
),
166 BOARD_ADM("BR-6541KP", "Edimax BR-6541K", 2, SIG_BR6541KP
),
167 BOARD_ADM("BR-6541WP", "Edimax BR-6541WP", 4, SIG_BR6541WP
),
168 BOARD_ADM("EW-7207APg", "Edimax EW-7207APg", 2, SIG_EW7207APg
),
169 BOARD_ADM("PS-1205UWg", "Edimax PS-1205UWg", 2, SIG_PS1205UWg
),
170 BOARD_ADM("PS-3205U", "Edimax PS-3205U", 2, SIG_PS3205U
),
171 BOARD_ADM("PS-3205UWg", "Edimax PS-3205UWg", 2, SIG_PS3205UWg
),
173 /* Hawking products */
174 BOARD_ADM("H2BR4", "Hawking H2BR4", 2, SIG_H2BR4
),
175 BOARD_ADM("H2WR54G", "Hawking H2WR54G", 4, SIG_H2WR54G
),
177 /* Planet products */
178 BOARD_ADM("XRT-401D", "Planet XRT-401D", 2, SIG_XRT401D
),
179 BOARD_ADM("XRT-402D", "Planet XRT-402D", 2, SIG_XRT402D
),
181 /* Conceptronic products */
182 BOARD_ADM("C54BSR4", "Conceptronic C54BSR4", 2, SIG_C54BSR4
),
184 /* OSBRiDGE products */
185 BOARD_ADM("5GXi", "OSBDRiDGE 5GXi", 2, SIG_5GXI
),
193 #define ERR(fmt, ...) do { \
195 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
198 #define ERRS(fmt, ...) do { \
201 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ \
205 #define WARN(fmt, ...) do { \
206 fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
209 #define DBG(lev, fmt, ...) do { \
210 if (verblevel < lev) \
212 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
216 #define ERR_INVALID_IMAGE -2
221 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
222 struct board_info
*board
;
224 fprintf(stream
, "Usage: %s [OPTIONS...] <file>\n", progname
);
228 " -B <board> create image for the board specified with <board>.\n"
229 " valid <board> values:\n"
231 for (board
= boards
; board
->model
!= NULL
; board
++){
234 board
->model
, board
->name
);
237 " -d don't throw error on invalid images\n"
238 " -k keep invalid images\n"
239 " -b <file>[:<align>[:<padc>]]\n"
240 " add boot code to the image\n"
241 " -c <file>[:<align>[:<padc>]]\n"
242 " add configuration settings to the image\n"
243 " -r <file>:[<addr>][:<align>[:<padc>]]\n"
244 " add runtime code to the image\n"
245 " -w [<file>:[<addr>][:<align>[:<padc>]]]\n"
246 " add webpages to the image\n"
247 " -x <file>[:<align>[:<padc>]]\n"
248 " add extra data at the end of the image\n"
249 " -h show this screen\n"
251 " <file> write output to the file <file>\n"
257 static inline uint32_t align(uint32_t base
, uint32_t alignment
)
262 ret
= (base
+ alignment
- 1);
263 ret
&= ~(alignment
-1);
275 str2u32(char *arg
, uint32_t *val
)
281 t
= strtoul(arg
, &err
, 0);
282 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
292 str2u16(char *arg
, uint16_t *val
)
298 t
= strtoul(arg
, &err
, 0);
299 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
308 str2u8(char *arg
, uint8_t *val
)
314 t
= strtoul(arg
, &err
, 0);
315 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
324 str2sig(char *arg
, uint32_t *sig
)
326 if (strlen(arg
) != 4)
329 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
336 parse_arg(char *arg
, char *buf
, char *argv
[])
344 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
357 if (argl
>= MAX_ARG_LEN
) {
358 /* argument is too long */
359 argl
= MAX_ARG_LEN
-1;
362 memcpy(buf
, arg
, argl
);
365 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
366 tok
= strsep(ap
, ":");
371 else if (tok
[0] == '\0') {
384 required_arg(char c
, char *arg
)
386 if (arg
== NULL
|| *arg
!= '-')
389 ERR("option -%c requires an argument\n", c
);
395 is_empty_arg(char *arg
)
406 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
408 for ( ; len
> 0; len
--) {
415 csum8_get(struct csum_state
*css
)
425 csum16_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
430 t
= css
->tmp
+ (p
[0]<<8);
431 css
->val
+= LE16_TO_HOST(t
);
437 for ( ; len
> 1; len
-= 2, p
+=2 ) {
438 t
= p
[0] + (p
[1] << 8);
439 css
->val
+= LE16_TO_HOST(t
);
450 csum16_get(struct csum_state
*css
)
454 csum16_update(&pad
, 1, css
);
455 return ~css
->val
+ 1;
460 csum_init(struct csum_state
*css
, int size
)
470 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
474 csum8_update(p
,len
,css
);
477 csum16_update(p
,len
,css
);
484 csum_get(struct csum_state
*css
)
490 ret
= csum8_get(css
);
493 ret
= csum16_get(css
);
502 * routines to write data to the output file
505 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
506 struct csum_state
*css
)
510 fwrite(data
, len
, 1, outfile
);
512 ERRS("unable to write output file");
517 csum_update(data
, len
, css
);
525 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
526 struct csum_state
*css
)
529 size_t buflen
= sizeof(buf
);
532 memset(buf
, padc
, buflen
);
537 err
= write_out_data(outfile
, buf
, buflen
, css
);
549 block_stat_file(struct csys_block
*block
)
554 if (block
->file_name
== NULL
)
557 err
= stat(block
->file_name
, &st
);
559 ERRS("stat failed on %s", block
->file_name
);
563 block
->file_size
= st
.st_size
;
569 block_writeout_hdr(FILE *outfile
, struct csys_block
*block
)
571 struct csys_header hdr
;
574 if (block
->size_hdr
== 0)
577 /* setup header fields */
578 memcpy(hdr
.sig
, block
->sig
, 4);
579 hdr
.addr
= HOST_TO_LE32(block
->addr
);
580 hdr
.size
= HOST_TO_LE32(block
->size
- block
->size_hdr
- block
->size_csum
);
582 DBG(1,"writing header for block");
583 res
= write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
),NULL
);
590 block_writeout_file(FILE *outfile
, struct csys_block
*block
)
592 char buf
[FILE_BUF_LEN
];
593 size_t buflen
= sizeof(buf
);
598 if (block
->file_name
== NULL
)
601 if (block
->file_size
== 0)
605 f
= fopen(block
->file_name
,"r");
607 ERRS("unable to open file: %s", block
->file_name
);
611 len
= block
->file_size
;
616 /* read data from source file */
618 fread(buf
, buflen
, 1, f
);
620 ERRS("unable to read from file: %s", block
->file_name
);
625 res
= write_out_data(outfile
, buf
, buflen
, block
->css
);
638 block_writeout_data(FILE *outfile
, struct csys_block
*block
)
643 res
= block_writeout_file(outfile
, block
);
647 /* write padding data if neccesary */
648 padlen
= block
->size_avail
- block
->file_size
;
649 DBG(1,"padding block, length=%d", padlen
);
650 res
= write_out_padding(outfile
, padlen
, block
->padc
, block
->css
);
657 block_writeout_csum(FILE *outfile
, struct csys_block
*block
)
662 if (block
->size_csum
== 0)
665 DBG(1,"writing checksum for block");
666 csum
= HOST_TO_LE16(csum_get(block
->css
));
667 res
= write_out_data(outfile
, (uint8_t *)&csum
, block
->size_csum
, NULL
);
674 block_writeout(FILE *outfile
, struct csys_block
*block
)
677 struct csum_state css
;
686 DBG(2, "writing block, file=%s, file_size=%d, space=%d",
687 block
->file_name
, block
->file_size
, block
->size_avail
);
688 res
= block_writeout_hdr(outfile
, block
);
692 if (block
->size_csum
!= 0) {
694 csum_init(&css
, block
->size_csum
);
697 res
= block_writeout_data(outfile
, block
);
701 res
= block_writeout_csum(outfile
, block
);
710 write_out_blocks(FILE *outfile
)
712 struct csys_block
*block
;
715 res
= block_writeout(outfile
, boot_block
);
719 res
= block_writeout(outfile
, conf_block
);
723 res
= block_writeout(outfile
, webp_block
);
727 res
= block_writeout(outfile
, code_block
);
732 for (i
=0; i
< num_blocks
; i
++) {
735 if (block
->type
!= BLOCK_TYPE_XTRA
)
738 res
= block_writeout(outfile
, block
);
748 find_board(char *model
)
750 struct board_info
*ret
;
751 struct board_info
*board
;
754 for (board
= boards
; board
->model
!= NULL
; board
++){
755 if (strcasecmp(model
, board
->model
) == 0) {
766 parse_opt_board(char ch
, char *arg
)
769 DBG(1,"parsing board option: -%c %s", ch
, arg
);
772 ERR("only one board option allowed");
776 if (required_arg(ch
, arg
))
779 board
= find_board(arg
);
781 ERR("invalid/unknown board specified: %s", arg
);
790 parse_opt_block(char ch
, char *arg
)
792 char buf
[MAX_ARG_LEN
];
793 char *argv
[MAX_ARG_COUNT
];
796 struct csys_block
*block
;
799 if ( num_blocks
> MAX_NUM_BLOCKS
) {
800 ERR("too many blocks specified");
804 block
= &blocks
[num_blocks
];
806 /* setup default field values */
807 block
->need_file
= 1;
813 WARN("only one boot block allowed");
816 block
->type
= BLOCK_TYPE_BOOT
;
821 WARN("only one config block allowed");
824 block
->type
= BLOCK_TYPE_CONF
;
829 WARN("only one web block allowed");
832 block
->type
= BLOCK_TYPE_WEBP
;
833 block
->size_hdr
= sizeof(struct csys_header
);
834 block
->size_csum
= CSUM_SIZE_8
;
835 block
->need_file
= 0;
840 WARN("only one runtime block allowed");
843 block
->type
= BLOCK_TYPE_CODE
;
844 block
->size_hdr
= sizeof(struct csys_header
);
845 block
->size_csum
= CSUM_SIZE_16
;
849 block
->type
= BLOCK_TYPE_XTRA
;
852 ERR("unknown block type \"%c\"", ch
);
856 argc
= parse_arg(arg
, buf
, argv
);
860 if (!is_empty_arg(p
)) {
861 block
->file_name
= strdup(p
);
862 if (block
->file_name
== NULL
) {
863 ERR("not enough memory");
866 } else if (block
->need_file
){
867 ERR("no file specified in %s", arg
);
871 if (block
->size_hdr
) {
873 if (!is_empty_arg(p
)) {
874 if (str2u32(p
, &block
->addr
) != 0) {
875 ERR("invalid start address in %s", arg
);
883 if (!is_empty_arg(p
)) {
884 if (str2u32(p
, &block
->align
) != 0) {
885 ERR("invalid alignment value in %s", arg
);
888 block
->align_set
= 1;
892 if (!is_empty_arg(p
) && (str2u8(p
, &block
->padc
) != 0)) {
893 ERR("invalid paddig character in %s", arg
);
906 struct csys_block
*block
;
912 /* collecting stats */
913 for (i
= 0; i
< num_blocks
; i
++) {
915 res
= block_stat_file(block
);
923 block
->size
= board
->boot_size
;
924 if (block
->file_size
> board
->boot_size
) {
925 WARN("boot block is too big");
926 res
= ERR_INVALID_IMAGE
;
929 offs
+= board
->boot_size
;
931 /* configuration data */
934 block
->size
= board
->conf_size
;
935 if (block
->file_size
> board
->conf_size
) {
936 WARN("config block is too big");
937 res
= ERR_INVALID_IMAGE
;
940 offs
+= board
->conf_size
;
946 memcpy(block
->sig
, board
->sig_webp
, 4);
948 if (block
->addr_set
== 0)
949 block
->addr
= board
->addr_webp
;
951 if (block
->align_set
== 0)
952 block
->align
= DEFAULT_BLOCK_ALIGN
;
954 block
->size
= align(offs
+ block
->file_size
+ block
->size_hdr
+
955 block
->size_csum
, block
->align
) - offs
;
957 if (block
->size
> board
->webp_size_max
) {
958 WARN("webpages block is too big");
959 res
= ERR_INVALID_IMAGE
;
962 DBG(2,"webpages start at %08x, size=%08x", offs
,
966 if (offs
> board
->flash_size
) {
967 WARN("webp block is too big");
968 res
= ERR_INVALID_IMAGE
;
975 memcpy(code_block
->sig
, SIG_CSYS
, 4);
977 if (block
->addr_set
== 0)
978 block
->addr
= board
->addr_code
;
980 if (block
->align_set
== 0)
981 block
->align
= DEFAULT_BLOCK_ALIGN
;
983 block
->size
= align(offs
+ block
->file_size
+
984 block
->size_hdr
+ block
->size_csum
,
985 block
->align
) - offs
;
987 DBG(2,"code block start at %08x, size=%08x", offs
,
991 if (offs
> board
->flash_size
) {
992 WARN("code block is too big");
993 res
= ERR_INVALID_IMAGE
;
997 for (i
= 0; i
< num_blocks
; i
++) {
1000 if (block
->type
!= BLOCK_TYPE_XTRA
)
1003 if (block
->align_set
== 0)
1004 block
->align
= DEFAULT_BLOCK_ALIGN
;
1006 block
->size
= align(offs
+ block
->file_size
,
1007 block
->align
) - offs
;
1009 DBG(2,"file %s start at %08x, size=%08x, align=%08x",
1010 block
->file_name
, offs
, block
->size
, block
->align
);
1012 offs
+= block
->size
;
1013 if (offs
> board
->flash_size
) {
1014 WARN("file %s is too big, size=%d, avail=%d",
1015 block
->file_name
, block
->file_size
,
1016 board
->flash_size
- offs
);
1017 res
= ERR_INVALID_IMAGE
;
1021 for (i
= 0; i
< num_blocks
; i
++) {
1024 block
->size_avail
= block
->size
- block
->size_hdr
-
1027 if (block
->size_avail
< block
->file_size
) {
1028 WARN("file %s is too big, size=%d, avail=%d",
1029 block
->file_name
, block
->file_size
,
1031 res
= ERR_INVALID_IMAGE
;
1040 main(int argc
, char *argv
[])
1042 int optinvalid
= 0; /* flag for invalid option */
1044 int res
= ERR_FATAL
;
1048 progname
=basename(argv
[0]);
1050 opterr
= 0; /* could not print standard getopt error messages */
1054 c
= getopt(argc
, argv
, "b:B:c:dhkr:vw:x:");
1063 optinvalid
= parse_opt_block(c
,optarg
);
1066 if (optarg
!= NULL
&& *optarg
== '-') {
1071 optinvalid
= parse_opt_block(c
,optarg
);
1074 invalid_causes_error
= 0;
1077 keep_invalid_images
= 1;
1080 optinvalid
= parse_opt_board(c
,optarg
);
1086 usage(EXIT_SUCCESS
);
1092 if (optinvalid
!= 0 ){
1093 ERR("invalid option: -%c", optopt
);
1098 if (board
== NULL
) {
1099 ERR("no board specified");
1103 if (optind
== argc
) {
1104 ERR("no output file specified");
1108 ofname
= argv
[optind
++];
1110 if (optind
< argc
) {
1111 ERR("invalid option: %s", argv
[optind
]);
1115 res
= process_blocks();
1116 if (res
== ERR_FATAL
)
1119 if (res
== ERR_INVALID_IMAGE
) {
1120 if (invalid_causes_error
)
1123 if (keep_invalid_images
== 0) {
1124 WARN("generation of invalid images disabled", ofname
);
1128 WARN("generating invalid image", ofname
);
1131 outfile
= fopen(ofname
, "w");
1132 if (outfile
== NULL
) {
1133 ERRS("could not open \"%s\" for writing", ofname
);
1138 if (write_out_blocks(outfile
) != 0) {
1143 DBG(1,"Image file %s completed.", ofname
);
1148 if (res
== ERR_FATAL
) {
1152 if (res
== ERR_FATAL
)
1153 return EXIT_FAILURE
;
1155 return EXIT_SUCCESS
;
This page took 0.094607 seconds and 5 git commands to generate.