3 * Copyright (C) 2007 Gabor Juhos <juhosg at 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
),
190 #define ERR(fmt, ...) do { \
192 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
195 #define ERRS(fmt, ...) do { \
198 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ \
202 #define WARN(fmt, ...) do { \
203 fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
206 #define DBG(lev, fmt, ...) do { \
207 if (verblevel < lev) \
209 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
213 #define ERR_INVALID_IMAGE -2
218 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
219 struct board_info
*board
;
221 fprintf(stream
, "Usage: %s [OPTIONS...] <file>\n", progname
);
225 " -B <board> create image for the board specified with <board>.\n"
226 " valid <board> values:\n"
228 for (board
= boards
; board
->model
!= NULL
; board
++){
231 board
->model
, board
->name
);
234 " -d don't throw error on invalid images\n"
235 " -k keep invalid images\n"
236 " -b <file>[:<align>[:<padc>]]\n"
237 " add boot code to the image\n"
238 " -c <file>[:<align>[:<padc>]]\n"
239 " add configuration settings to the image\n"
240 " -r <file>:[<addr>][:<align>[:<padc>]]\n"
241 " add runtime code to the image\n"
242 " -w [<file>:[<addr>][:<align>[:<padc>]]]\n"
243 " add webpages to the image\n"
244 " -x <file>[:<align>[:<padc>]]\n"
245 " add extra data at the end of the image\n"
246 " -h show this screen\n"
248 " <file> write output to the file <file>\n"
254 static inline uint32_t align(uint32_t base
, uint32_t alignment
)
259 ret
= (base
+ alignment
- 1);
260 ret
&= ~(alignment
-1);
272 str2u32(char *arg
, uint32_t *val
)
278 t
= strtoul(arg
, &err
, 0);
279 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
289 str2u16(char *arg
, uint16_t *val
)
295 t
= strtoul(arg
, &err
, 0);
296 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
305 str2u8(char *arg
, uint8_t *val
)
311 t
= strtoul(arg
, &err
, 0);
312 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
321 str2sig(char *arg
, uint32_t *sig
)
323 if (strlen(arg
) != 4)
326 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
333 parse_arg(char *arg
, char *buf
, char *argv
[])
341 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
354 if (argl
>= MAX_ARG_LEN
) {
355 /* argument is too long */
356 argl
= MAX_ARG_LEN
-1;
359 memcpy(buf
, arg
, argl
);
362 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
363 tok
= strsep(ap
, ":");
368 else if (tok
[0] == '\0') {
381 required_arg(char c
, char *arg
)
383 if (arg
== NULL
|| *arg
!= '-')
386 ERR("option -%c requires an argument\n", c
);
392 is_empty_arg(char *arg
)
403 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
405 for ( ; len
> 0; len
--) {
412 csum8_get(struct csum_state
*css
)
422 csum16_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
427 t
= css
->tmp
+ (p
[0]<<8);
428 css
->val
+= LE16_TO_HOST(t
);
434 for ( ; len
> 1; len
-= 2, p
+=2 ) {
435 t
= p
[0] + (p
[1] << 8);
436 css
->val
+= LE16_TO_HOST(t
);
447 csum16_get(struct csum_state
*css
)
451 csum16_update(&pad
, 1, css
);
452 return ~css
->val
+ 1;
457 csum_init(struct csum_state
*css
, int size
)
467 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
471 csum8_update(p
,len
,css
);
474 csum16_update(p
,len
,css
);
481 csum_get(struct csum_state
*css
)
487 ret
= csum8_get(css
);
490 ret
= csum16_get(css
);
499 * routines to write data to the output file
502 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
503 struct csum_state
*css
)
507 fwrite(data
, len
, 1, outfile
);
509 ERRS("unable to write output file");
514 csum_update(data
, len
, css
);
522 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
523 struct csum_state
*css
)
526 size_t buflen
= sizeof(buf
);
529 memset(buf
, padc
, buflen
);
534 err
= write_out_data(outfile
, buf
, buflen
, css
);
546 block_stat_file(struct csys_block
*block
)
551 if (block
->file_name
== NULL
)
554 err
= stat(block
->file_name
, &st
);
556 ERRS("stat failed on %s", block
->file_name
);
560 block
->file_size
= st
.st_size
;
566 block_writeout_hdr(FILE *outfile
, struct csys_block
*block
)
568 struct csys_header hdr
;
571 if (block
->size_hdr
== 0)
574 /* setup header fields */
575 memcpy(hdr
.sig
, block
->sig
, 4);
576 hdr
.addr
= HOST_TO_LE32(block
->addr
);
577 hdr
.size
= HOST_TO_LE32(block
->align
-block
->size_hdr
);
579 DBG(1,"writing header for block");
580 res
= write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
),NULL
);
587 block_writeout_file(FILE *outfile
, struct csys_block
*block
)
589 char buf
[FILE_BUF_LEN
];
590 size_t buflen
= sizeof(buf
);
595 if (block
->file_name
== NULL
)
598 if (block
->file_size
== 0)
602 f
= fopen(block
->file_name
,"r");
604 ERRS("unable to open file: %s", block
->file_name
);
608 len
= block
->file_size
;
613 /* read data from source file */
615 fread(buf
, buflen
, 1, f
);
617 ERRS("unable to read from file: %s", block
->file_name
);
622 res
= write_out_data(outfile
, buf
, buflen
, block
->css
);
635 block_writeout_data(FILE *outfile
, struct csys_block
*block
)
640 res
= block_writeout_file(outfile
, block
);
644 /* write padding data if neccesary */
645 padlen
= block
->size_avail
- block
->file_size
;
646 DBG(1,"padding block, length=%d", padlen
);
647 res
= write_out_padding(outfile
, padlen
, block
->padc
, block
->css
);
654 block_writeout_csum(FILE *outfile
, struct csys_block
*block
)
659 if (block
->size_csum
== 0)
662 DBG(1,"writing checksum for block");
663 csum
= HOST_TO_LE16(csum_get(block
->css
));
664 res
= write_out_data(outfile
, (uint8_t *)&csum
, block
->size_csum
, NULL
);
671 block_writeout(FILE *outfile
, struct csys_block
*block
)
674 struct csum_state css
;
683 DBG(2, "writing block, file=%s, file_size=%d, space=%d",
684 block
->file_name
, block
->file_size
, block
->size_avail
);
685 res
= block_writeout_hdr(outfile
, block
);
689 if (block
->size_csum
!= 0) {
691 csum_init(&css
, block
->size_csum
);
694 res
= block_writeout_data(outfile
, block
);
698 res
= block_writeout_csum(outfile
, block
);
707 write_out_blocks(FILE *outfile
)
709 struct csys_block
*block
;
712 res
= block_writeout(outfile
, boot_block
);
716 res
= block_writeout(outfile
, conf_block
);
720 res
= block_writeout(outfile
, webp_block
);
724 res
= block_writeout(outfile
, code_block
);
729 for (i
=0; i
< num_blocks
; i
++) {
732 if (block
->type
!= BLOCK_TYPE_XTRA
)
735 res
= block_writeout(outfile
, block
);
745 find_board(char *model
)
747 struct board_info
*ret
;
748 struct board_info
*board
;
751 for (board
= boards
; board
->model
!= NULL
; board
++){
752 if (strcasecmp(model
, board
->model
) == 0) {
763 parse_opt_board(char ch
, char *arg
)
766 DBG(1,"parsing board option: -%c %s", ch
, arg
);
769 ERR("only one board option allowed");
773 if (required_arg(ch
, arg
))
776 board
= find_board(arg
);
778 ERR("invalid/unknown board specified: %s", arg
);
787 parse_opt_block(char ch
, char *arg
)
789 char buf
[MAX_ARG_LEN
];
790 char *argv
[MAX_ARG_COUNT
];
793 struct csys_block
*block
;
796 if ( num_blocks
> MAX_NUM_BLOCKS
) {
797 ERR("too many blocks specified");
801 block
= &blocks
[num_blocks
];
803 /* setup default field values */
804 block
->need_file
= 1;
810 WARN("only one boot block allowed");
813 block
->type
= BLOCK_TYPE_BOOT
;
818 WARN("only one config block allowed");
821 block
->type
= BLOCK_TYPE_CONF
;
826 WARN("only one web block allowed");
829 block
->type
= BLOCK_TYPE_WEBP
;
830 block
->size_hdr
= sizeof(struct csys_header
);
831 block
->size_csum
= CSUM_SIZE_8
;
832 block
->need_file
= 0;
837 WARN("only one runtime block allowed");
840 block
->type
= BLOCK_TYPE_CODE
;
841 block
->size_hdr
= sizeof(struct csys_header
);
842 block
->size_csum
= CSUM_SIZE_16
;
846 block
->type
= BLOCK_TYPE_XTRA
;
849 ERR("unknown block type \"%c\"", ch
);
853 argc
= parse_arg(arg
, buf
, argv
);
857 if (!is_empty_arg(p
)) {
858 block
->file_name
= strdup(p
);
859 if (block
->file_name
== NULL
) {
860 ERR("not enough memory");
863 } else if (block
->need_file
){
864 ERR("no file specified in %s", arg
);
868 if (block
->size_hdr
) {
870 if (!is_empty_arg(p
)) {
871 if (str2u32(p
, &block
->addr
) != 0) {
872 ERR("invalid start address in %s", arg
);
880 if (!is_empty_arg(p
)) {
881 if (str2u32(p
, &block
->align
) != 0) {
882 ERR("invalid alignment value in %s", arg
);
885 block
->align_set
= 1;
889 if (!is_empty_arg(p
) && (str2u8(p
, &block
->padc
) != 0)) {
890 ERR("invalid paddig character in %s", arg
);
903 struct csys_block
*block
;
909 /* collecting stats */
910 for (i
= 0; i
< num_blocks
; i
++) {
912 res
= block_stat_file(block
);
920 block
->size
= board
->boot_size
;
921 if (block
->file_size
> board
->boot_size
) {
922 WARN("boot block is too big");
923 res
= ERR_INVALID_IMAGE
;
926 offs
+= board
->boot_size
;
928 /* configuration data */
931 block
->size
= board
->conf_size
;
932 if (block
->file_size
> board
->conf_size
) {
933 WARN("config block is too big");
934 res
= ERR_INVALID_IMAGE
;
937 offs
+= board
->conf_size
;
943 memcpy(block
->sig
, board
->sig_webp
, 4);
945 if (block
->addr_set
== 0)
946 block
->addr
= board
->addr_webp
;
948 if (block
->align_set
== 0)
949 block
->align
= DEFAULT_BLOCK_ALIGN
;
951 block
->size
= align(offs
+ block
->file_size
+ block
->size_hdr
+
952 block
->size_csum
, block
->align
) - offs
;
954 if (block
->size
> board
->webp_size_max
) {
955 WARN("webpages block is too big");
956 res
= ERR_INVALID_IMAGE
;
959 DBG(2,"webpages start at %08x, size=%08x", offs
,
963 if (offs
> board
->flash_size
) {
964 WARN("webp block is too big");
965 res
= ERR_INVALID_IMAGE
;
972 memcpy(code_block
->sig
, SIG_CSYS
, 4);
974 if (block
->addr_set
== 0)
975 block
->addr
= board
->addr_code
;
977 if (block
->align_set
== 0)
978 block
->align
= DEFAULT_BLOCK_ALIGN
;
980 block
->size
= align(offs
+ block
->file_size
+
981 block
->size_hdr
+ block
->size_csum
,
982 block
->align
) - offs
;
984 DBG(2,"code block start at %08x, size=%08x", offs
,
988 if (offs
> board
->flash_size
) {
989 WARN("code block is too big");
990 res
= ERR_INVALID_IMAGE
;
994 for (i
= 0; i
< num_blocks
; i
++) {
997 if (block
->type
!= BLOCK_TYPE_XTRA
)
1000 if (block
->align_set
== 0)
1001 block
->align
= DEFAULT_BLOCK_ALIGN
;
1003 block
->size
= align(offs
+ block
->file_size
,
1004 block
->align
) - offs
;
1006 DBG(2,"file %s start at %08x, size=%08x, align=%08x",
1007 block
->file_name
, offs
, block
->size
, block
->align
);
1009 offs
+= block
->size
;
1010 if (offs
> board
->flash_size
) {
1011 WARN("file %s is too big, size=%d, avail=%d",
1012 block
->file_name
, block
->file_size
,
1013 board
->flash_size
- offs
);
1014 res
= ERR_INVALID_IMAGE
;
1018 for (i
= 0; i
< num_blocks
; i
++) {
1021 block
->size_avail
= block
->size
- block
->size_hdr
-
1024 if (block
->size_avail
< block
->file_size
) {
1025 WARN("file %s is too big, size=%d, avail=%d",
1026 block
->file_name
, block
->file_size
,
1028 res
= ERR_INVALID_IMAGE
;
1037 main(int argc
, char *argv
[])
1039 int optinvalid
= 0; /* flag for invalid option */
1041 int res
= ERR_FATAL
;
1045 progname
=basename(argv
[0]);
1047 opterr
= 0; /* could not print standard getopt error messages */
1051 c
= getopt(argc
, argv
, "b:B:c:dhkr:vw:x:");
1060 optinvalid
= parse_opt_block(c
,optarg
);
1063 if (optarg
!= NULL
&& *optarg
== '-') {
1068 optinvalid
= parse_opt_block(c
,optarg
);
1071 invalid_causes_error
= 0;
1074 keep_invalid_images
= 1;
1077 optinvalid
= parse_opt_board(c
,optarg
);
1083 usage(EXIT_SUCCESS
);
1089 if (optinvalid
!= 0 ){
1090 ERR("invalid option: -%c", optopt
);
1095 if (board
== NULL
) {
1096 ERR("no board specified");
1100 if (optind
== argc
) {
1101 ERR("no output file specified");
1105 ofname
= argv
[optind
++];
1107 if (optind
< argc
) {
1108 ERR("invalid option: %s", argv
[optind
]);
1112 res
= process_blocks();
1113 if (res
== ERR_FATAL
)
1116 if (res
== ERR_INVALID_IMAGE
) {
1117 if (invalid_causes_error
)
1120 if (keep_invalid_images
== 0) {
1121 WARN("generation of invalid images disabled", ofname
);
1125 WARN("generating invalid image", ofname
);
1128 outfile
= fopen(ofname
, "w");
1129 if (outfile
== NULL
) {
1130 ERRS("could not open \"%s\" for writing", ofname
);
1135 if (write_out_blocks(outfile
) != 0) {
1140 DBG(1,"Image file %s completed.", ofname
);
1145 if (res
== ERR_FATAL
) {
1149 if (res
== ERR_FATAL
)
1150 return EXIT_FAILURE
;
1152 return EXIT_SUCCESS
;
This page took 0.104368 seconds and 5 git commands to generate.