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 MAX_NUM_BLOCKS 8
56 #define MAX_ARG_COUNT 32
57 #define MAX_ARG_LEN 1024
58 #define FILE_BUF_LEN (16*1024)
59 #define CSYS_PADC 0xFF
61 #define BLOCK_TYPE_BOOT 0
62 #define BLOCK_TYPE_CONF 1
63 #define BLOCK_TYPE_WEBP 2
64 #define BLOCK_TYPE_CODE 3
65 #define BLOCK_TYPE_XTRA 4
67 #define DEFAULT_BLOCK_ALIGN 0x10000U
69 #define CSUM_SIZE_NONE 0
71 #define CSUM_SIZE_16 2
83 int type
; /* type of the block */
86 char *file_name
; /* name of the file */
87 uint32_t file_size
; /* length of the file */
89 unsigned char sig
[SIG_LEN
];
101 struct csum_state
*css
;
110 char sig_boot
[SIG_LEN
];
111 char sig_conf
[SIG_LEN
];
112 char sig_webp
[SIG_LEN
];
117 uint32_t webp_size_max
;
124 #define BOARD(m, n, f, sigb, sigw, bs, cs, ws, ac, aw) {\
125 .model = m, .name = n, .flash_size = f<<20, \
126 .sig_boot = sigb, .sig_conf = SIG_CONF, .sig_webp = sigw, \
127 .boot_size = bs, .conf_size = cs, \
128 .webp_size = ws, .webp_size_max = 3*0x10000, \
129 .addr_code = ac, .addr_webp = aw \
132 #define BOARD_ADM(m,n,f, sigw) BOARD(m,n,f, ADM_BOOT_SIG, sigw, \
133 ADM_BOOT_SIZE, ADM_CONF_SIZE, ADM_WEBP_SIZE, \
134 ADM_CODE_ADDR, ADM_WEBP_ADDR)
143 int invalid_causes_error
= 1;
144 int keep_invalid_images
= 0;
146 struct board_info
*board
= NULL
;
148 struct csys_block
*boot_block
= NULL
;
149 struct csys_block
*conf_block
= NULL
;
150 struct csys_block
*webp_block
= NULL
;
151 struct csys_block
*code_block
= NULL
;
153 struct csys_block blocks
[MAX_NUM_BLOCKS
];
156 static struct board_info boards
[] = {
157 /* The original Edimax products */
158 BOARD_ADM("BR-6104K", "Edimax BR-6104K", 2, SIG_BR6104K
),
159 BOARD_ADM("BR-6104KP", "Edimax BR-6104KP", 2, SIG_BR6104KP
),
160 BOARD_ADM("BR-6104Wg", "Edimax BR-6104Wg", 2, SIG_BR6104Wg
),
161 BOARD_ADM("BR-6114WG", "Edimax BR-6114WG", 2, SIG_BR6114WG
),
162 BOARD_ADM("BR-6524K", "Edimax BR-6524K", 2, SIG_BR6524K
),
163 BOARD_ADM("BR-6524KP", "Edimax BR-6524KP", 2, SIG_BR6524KP
),
164 BOARD_ADM("BR-6524WG", "Edimax BR-6524WG", 4, SIG_BR6524WG
),
165 BOARD_ADM("BR-6524WP", "Edimax BR-6524WP", 4, SIG_BR6524WP
),
166 BOARD_ADM("BR-6541K", "Edimax BR-6541K", 2, SIG_BR6541K
),
167 BOARD_ADM("BR-6541KP", "Edimax BR-6541K", 2, SIG_BR6541KP
),
168 BOARD_ADM("BR-6541WP", "Edimax BR-6541WP", 4, SIG_BR6541WP
),
169 BOARD_ADM("EW-7207APg", "Edimax EW-7207APg", 2, SIG_EW7207APg
),
170 BOARD_ADM("PS-1205UWg", "Edimax PS-1205UWg", 2, SIG_PS1205UWg
),
171 BOARD_ADM("PS-3205U", "Edimax PS-3205U", 2, SIG_PS3205U
),
172 BOARD_ADM("PS-3205UWg", "Edimax PS-3205UWg", 2, SIG_PS3205UWg
),
174 /* Hawking products */
175 BOARD_ADM("H2BR4", "Hawking H2BR4", 2, SIG_H2BR4
),
176 BOARD_ADM("H2WR54G", "Hawking H2WR54G", 4, SIG_H2WR54G
),
178 /* Planet products */
179 BOARD_ADM("XRT-401D", "Planet XRT-401D", 2, SIG_XRT401D
),
180 BOARD_ADM("XRT-402D", "Planet XRT-402D", 2, SIG_XRT402D
),
182 /* Conceptronic products */
183 BOARD_ADM("C54BSR4", "Conceptronic C54BSR4", 2, SIG_C54BSR4
),
191 #define ERR(fmt, ...) do { \
193 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
196 #define ERRS(fmt, ...) do { \
199 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ \
203 #define WARN(fmt, ...) do { \
204 fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
207 #define DBG(lev, fmt, ...) do { \
208 if (verblevel < lev) \
210 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
214 #define ERR_INVALID_IMAGE -2
219 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
220 struct board_info
*board
;
222 fprintf(stream
, "Usage: %s [OPTIONS...] <file>\n", progname
);
226 " -B <board> create image for the board specified with <board>.\n"
227 " valid <board> values:\n"
229 for (board
= boards
; board
->model
!= NULL
; board
++){
232 board
->model
, board
->name
);
235 " -d don't throw error on invalid images\n"
236 " -k keep invalid images\n"
237 " -b <file>[:<align>[:<padc>]]\n"
238 " add boot code to the image\n"
239 " -c <file>[:<align>[:<padc>]]\n"
240 " add configuration settings to the image\n"
241 " -r <file>:[<addr>][:<align>[:<padc>]]\n"
242 " add runtime code to the image\n"
243 " -w [<file>:[<addr>][:<align>[:<padc>]]]\n"
244 " add webpages to the image\n"
245 " -x <file>[:<align>[:<padc>]]\n"
246 " add extra data at the end of the image\n"
247 " -h show this screen\n"
249 " <file> write output to the file <file>\n"
255 static inline uint32_t align(uint32_t base
, uint32_t alignment
)
260 ret
= (base
+ alignment
- 1);
261 ret
&= ~(alignment
-1);
273 str2u32(char *arg
, uint32_t *val
)
279 t
= strtoul(arg
, &err
, 0);
280 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
290 str2u16(char *arg
, uint16_t *val
)
296 t
= strtoul(arg
, &err
, 0);
297 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
306 str2u8(char *arg
, uint8_t *val
)
312 t
= strtoul(arg
, &err
, 0);
313 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
322 str2sig(char *arg
, uint32_t *sig
)
324 if (strlen(arg
) != 4)
327 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
334 parse_arg(char *arg
, char *buf
, char *argv
[])
342 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
355 if (argl
>= MAX_ARG_LEN
) {
356 /* argument is too long */
357 argl
= MAX_ARG_LEN
-1;
360 memcpy(buf
, arg
, argl
);
363 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
364 tok
= strsep(ap
, ":");
369 else if (tok
[0] == '\0') {
382 required_arg(char c
, char *arg
)
384 if (arg
== NULL
|| *arg
!= '-')
387 ERR("option -%c requires an argument\n", c
);
393 is_empty_arg(char *arg
)
404 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
406 for ( ; len
> 0; len
--) {
413 csum8_get(struct csum_state
*css
)
423 csum16_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
428 t
= css
->tmp
+ (p
[0]<<8);
429 css
->val
+= LE16_TO_HOST(t
);
435 for ( ; len
> 1; len
-= 2, p
+=2 ) {
436 t
= p
[0] + (p
[1] << 8);
437 css
->val
+= LE16_TO_HOST(t
);
448 csum16_get(struct csum_state
*css
)
452 csum16_update(&pad
, 1, css
);
453 return ~css
->val
+ 1;
458 csum_init(struct csum_state
*css
, int size
)
468 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
472 csum8_update(p
,len
,css
);
475 csum16_update(p
,len
,css
);
482 csum_get(struct csum_state
*css
)
488 ret
= csum8_get(css
);
491 ret
= csum16_get(css
);
500 * routines to write data to the output file
503 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
504 struct csum_state
*css
)
508 fwrite(data
, len
, 1, outfile
);
510 ERRS("unable to write output file");
515 csum_update(data
, len
, css
);
523 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
524 struct csum_state
*css
)
527 size_t buflen
= sizeof(buf
);
530 memset(buf
, padc
, buflen
);
535 err
= write_out_data(outfile
, buf
, buflen
, css
);
547 block_stat_file(struct csys_block
*block
)
552 if (block
->file_name
== NULL
)
555 err
= stat(block
->file_name
, &st
);
557 ERRS("stat failed on %s", block
->file_name
);
561 block
->file_size
= st
.st_size
;
567 block_writeout_hdr(FILE *outfile
, struct csys_block
*block
)
569 struct csys_header hdr
;
572 if (block
->size_hdr
== 0)
575 /* setup header fields */
576 memcpy(hdr
.sig
, block
->sig
, 4);
577 hdr
.addr
= HOST_TO_LE32(block
->addr
);
578 hdr
.size
= HOST_TO_LE32(block
->align
-block
->size_hdr
);
580 DBG(1,"writing header for block");
581 res
= write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
),NULL
);
588 block_writeout_file(FILE *outfile
, struct csys_block
*block
)
590 char buf
[FILE_BUF_LEN
];
591 size_t buflen
= sizeof(buf
);
596 if (block
->file_name
== NULL
)
599 if (block
->file_size
== 0)
603 f
= fopen(block
->file_name
,"r");
605 ERRS("unable to open file: %s", block
->file_name
);
609 len
= block
->file_size
;
614 /* read data from source file */
616 fread(buf
, buflen
, 1, f
);
618 ERRS("unable to read from file: %s", block
->file_name
);
623 res
= write_out_data(outfile
, buf
, buflen
, block
->css
);
636 block_writeout_data(FILE *outfile
, struct csys_block
*block
)
641 res
= block_writeout_file(outfile
, block
);
645 /* write padding data if neccesary */
646 padlen
= block
->size_avail
- block
->file_size
;
647 DBG(1,"padding block, length=%d", padlen
);
648 res
= write_out_padding(outfile
, padlen
, block
->padc
, block
->css
);
655 block_writeout_csum(FILE *outfile
, struct csys_block
*block
)
660 if (block
->size_csum
== 0)
663 DBG(1,"writing checksum for block");
664 csum
= HOST_TO_LE16(csum_get(block
->css
));
665 res
= write_out_data(outfile
, (uint8_t *)&csum
, block
->size_csum
, NULL
);
672 block_writeout(FILE *outfile
, struct csys_block
*block
)
675 struct csum_state css
;
684 DBG(2, "writing block, file=%s, file_size=%d, space=%d",
685 block
->file_name
, block
->file_size
, block
->size_avail
);
686 res
= block_writeout_hdr(outfile
, block
);
690 if (block
->size_csum
!= 0) {
692 csum_init(&css
, block
->size_csum
);
695 res
= block_writeout_data(outfile
, block
);
699 res
= block_writeout_csum(outfile
, block
);
708 write_out_blocks(FILE *outfile
)
710 struct csys_block
*block
;
713 res
= block_writeout(outfile
, boot_block
);
717 res
= block_writeout(outfile
, conf_block
);
721 res
= block_writeout(outfile
, webp_block
);
725 res
= block_writeout(outfile
, code_block
);
730 for (i
=0; i
< num_blocks
; i
++) {
733 if (block
->type
!= BLOCK_TYPE_XTRA
)
736 res
= block_writeout(outfile
, block
);
746 find_board(char *model
)
748 struct board_info
*ret
;
749 struct board_info
*board
;
752 for (board
= boards
; board
->model
!= NULL
; board
++){
753 if (strcasecmp(model
, board
->model
) == 0) {
764 parse_opt_board(char ch
, char *arg
)
767 DBG(1,"parsing board option: -%c %s", ch
, arg
);
770 ERR("only one board option allowed");
774 if (required_arg(ch
, arg
))
777 board
= find_board(arg
);
779 ERR("invalid/unknown board specified: %s", arg
);
788 parse_opt_block(char ch
, char *arg
)
790 char buf
[MAX_ARG_LEN
];
791 char *argv
[MAX_ARG_COUNT
];
794 struct csys_block
*block
;
797 if ( num_blocks
> MAX_NUM_BLOCKS
) {
798 ERR("too many blocks specified");
802 block
= &blocks
[num_blocks
];
804 /* setup default field values */
805 block
->need_file
= 1;
811 WARN("only one boot block allowed");
814 block
->type
= BLOCK_TYPE_BOOT
;
819 WARN("only one config block allowed");
822 block
->type
= BLOCK_TYPE_CONF
;
827 WARN("only one web block allowed");
830 block
->type
= BLOCK_TYPE_WEBP
;
831 block
->size_hdr
= sizeof(struct csys_header
);
832 block
->size_csum
= CSUM_SIZE_8
;
833 block
->need_file
= 0;
838 WARN("only one runtime block allowed");
841 block
->type
= BLOCK_TYPE_CODE
;
842 block
->size_hdr
= sizeof(struct csys_header
);
843 block
->size_csum
= CSUM_SIZE_16
;
847 block
->type
= BLOCK_TYPE_XTRA
;
850 ERR("unknown block type \"%c\"", ch
);
854 argc
= parse_arg(arg
, buf
, argv
);
858 if (!is_empty_arg(p
)) {
859 block
->file_name
= strdup(p
);
860 if (block
->file_name
== NULL
) {
861 ERR("not enough memory");
864 } else if (block
->need_file
){
865 ERR("no file specified in %s", arg
);
869 if (block
->size_hdr
) {
871 if (!is_empty_arg(p
)) {
872 if (str2u32(p
, &block
->addr
) != 0) {
873 ERR("invalid start address in %s", arg
);
881 if (!is_empty_arg(p
)) {
882 if (str2u32(p
, &block
->align
) != 0) {
883 ERR("invalid alignment value in %s", arg
);
886 block
->align_set
= 1;
890 if (!is_empty_arg(p
) && (str2u8(p
, &block
->padc
) != 0)) {
891 ERR("invalid paddig character in %s", arg
);
904 struct csys_block
*block
;
910 /* collecting stats */
911 for (i
= 0; i
< num_blocks
; i
++) {
913 res
= block_stat_file(block
);
921 block
->size
= board
->boot_size
;
922 if (block
->file_size
> board
->boot_size
) {
923 WARN("boot block is too big");
924 res
= ERR_INVALID_IMAGE
;
927 offs
+= board
->boot_size
;
929 /* configuration data */
932 block
->size
= board
->conf_size
;
933 if (block
->file_size
> board
->conf_size
) {
934 WARN("config block is too big");
935 res
= ERR_INVALID_IMAGE
;
938 offs
+= board
->conf_size
;
944 memcpy(block
->sig
, board
->sig_webp
, 4);
946 if (block
->addr_set
== 0)
947 block
->addr
= board
->addr_webp
;
949 if (block
->align_set
== 0)
950 block
->align
= DEFAULT_BLOCK_ALIGN
;
952 block
->size
= align(offs
+ block
->file_size
+ block
->size_hdr
+
953 block
->size_csum
, block
->align
) - offs
;
955 if (block
->size
> board
->webp_size_max
) {
956 WARN("webpages block is too big");
957 res
= ERR_INVALID_IMAGE
;
960 DBG(2,"webpages start at %08x, size=%08x", offs
,
964 if (offs
> board
->flash_size
) {
965 WARN("webp block is too big");
966 res
= ERR_INVALID_IMAGE
;
973 memcpy(code_block
->sig
, SIG_CSYS
, 4);
975 if (block
->addr_set
== 0)
976 block
->addr
= board
->addr_code
;
978 if (block
->align_set
== 0)
979 block
->align
= DEFAULT_BLOCK_ALIGN
;
981 block
->size
= align(offs
+ block
->file_size
+
982 block
->size_hdr
+ block
->size_csum
,
983 block
->align
) - offs
;
985 DBG(2,"code block start at %08x, size=%08x", offs
,
989 if (offs
> board
->flash_size
) {
990 WARN("code block is too big");
991 res
= ERR_INVALID_IMAGE
;
995 for (i
= 0; i
< num_blocks
; i
++) {
998 if (block
->type
!= BLOCK_TYPE_XTRA
)
1001 if (block
->align_set
== 0)
1002 block
->align
= DEFAULT_BLOCK_ALIGN
;
1004 block
->size
= align(offs
+ block
->file_size
,
1005 block
->align
) - offs
;
1007 DBG(2,"file %s start at %08x, size=%08x, align=%08x",
1008 block
->file_name
, offs
, block
->size
, block
->align
);
1010 offs
+= block
->size
;
1011 if (offs
> board
->flash_size
) {
1012 WARN("file %s is too big, size=%d, avail=%d",
1013 block
->file_name
, block
->file_size
,
1014 board
->flash_size
- offs
);
1015 res
= ERR_INVALID_IMAGE
;
1019 for (i
= 0; i
< num_blocks
; i
++) {
1022 block
->size_avail
= block
->size
- block
->size_hdr
-
1025 if (block
->size_avail
< block
->file_size
) {
1026 WARN("file %s is too big, size=%d, avail=%d",
1027 block
->file_name
, block
->file_size
,
1029 res
= ERR_INVALID_IMAGE
;
1038 main(int argc
, char *argv
[])
1040 int optinvalid
= 0; /* flag for invalid option */
1042 int res
= ERR_FATAL
;
1046 progname
=basename(argv
[0]);
1048 opterr
= 0; /* could not print standard getopt error messages */
1052 c
= getopt(argc
, argv
, "b:B:c:dhkr:vw:x:");
1061 optinvalid
= parse_opt_block(c
,optarg
);
1064 if (optarg
!= NULL
&& *optarg
== '-') {
1069 optinvalid
= parse_opt_block(c
,optarg
);
1072 invalid_causes_error
= 0;
1075 keep_invalid_images
= 1;
1078 optinvalid
= parse_opt_board(c
,optarg
);
1084 usage(EXIT_SUCCESS
);
1090 if (optinvalid
!= 0 ){
1091 ERR("invalid option: -%c", optopt
);
1096 if (board
== NULL
) {
1097 ERR("no board specified");
1101 if (optind
== argc
) {
1102 ERR("no output file specified");
1106 ofname
= argv
[optind
++];
1108 if (optind
< argc
) {
1109 ERR("invalid option: %s", argv
[optind
]);
1113 res
= process_blocks();
1114 if (res
== ERR_FATAL
)
1117 if (res
== ERR_INVALID_IMAGE
) {
1118 if (invalid_causes_error
)
1121 if (keep_invalid_images
== 0) {
1122 WARN("generation of invalid images disabled", ofname
);
1126 WARN("generating invalid image", ofname
);
1129 outfile
= fopen(ofname
, "w");
1130 if (outfile
== NULL
) {
1131 ERRS("could not open \"%s\" for writing", ofname
);
1136 if (write_out_blocks(outfile
) != 0) {
1141 DBG(1,"Image file %s completed.", ofname
);
1146 if (res
== ERR_FATAL
) {
1150 if (res
== ERR_FATAL
)
1151 return EXIT_FAILURE
;
1153 return EXIT_SUCCESS
;
This page took 0.124027 seconds and 5 git commands to generate.