1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2007-2009 Gabor Juhos <juhosg@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>
15 #include <unistd.h> /* for unlink() */
17 #include <getopt.h> /* for getopt() */
21 #include <endian.h> /* for __BYTE_ORDER */
26 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
27 # define HOST_TO_LE16(x) (x)
28 # define HOST_TO_LE32(x) (x)
29 # define LE16_TO_HOST(x) (x)
30 # define LE32_TO_HOST(x) (x)
32 # define HOST_TO_LE16(x) bswap_16(x)
33 # define HOST_TO_LE32(x) bswap_32(x)
34 # define LE16_TO_HOST(x) bswap_16(x)
35 # define LE32_TO_HOST(x) bswap_32(x)
38 #define MAX_NUM_BLOCKS 8
39 #define MAX_ARG_COUNT 32
40 #define MAX_ARG_LEN 1024
41 #define FILE_BUF_LEN (16*1024)
42 #define CSYS_PADC 0xFF
44 #define BLOCK_TYPE_BOOT 0
45 #define BLOCK_TYPE_CONF 1
46 #define BLOCK_TYPE_WEBP 2
47 #define BLOCK_TYPE_CODE 3
48 #define BLOCK_TYPE_XTRA 4
50 #define DEFAULT_BLOCK_ALIGN 0x10000U
52 #define CSUM_SIZE_NONE 0
54 #define CSUM_SIZE_16 2
66 int type
; /* type of the block */
69 char *file_name
; /* name of the file */
70 uint32_t file_size
; /* length of the file */
72 unsigned char sig
[SIG_LEN
];
84 struct csum_state
*css
;
93 char sig_boot
[SIG_LEN
];
94 char sig_conf
[SIG_LEN
];
95 char sig_webp
[SIG_LEN
];
100 uint32_t webp_size_max
;
107 #define BOARD(m, n, f, sigb, sigw, bs, cs, ws, ac, aw) {\
108 .model = m, .name = n, .flash_size = f<<20, \
109 .sig_boot = sigb, .sig_conf = SIG_CONF, .sig_webp = sigw, \
110 .boot_size = bs, .conf_size = cs, \
111 .webp_size = ws, .webp_size_max = 3*0x10000, \
112 .addr_code = ac, .addr_webp = aw \
115 #define BOARD_ADM(m,n,f, sigw) BOARD(m,n,f, ADM_BOOT_SIG, sigw, \
116 ADM_BOOT_SIZE, ADM_CONF_SIZE, ADM_WEBP_SIZE, \
117 ADM_CODE_ADDR, ADM_WEBP_ADDR)
126 int invalid_causes_error
= 1;
127 int keep_invalid_images
= 0;
129 struct board_info
*board
= NULL
;
131 struct csys_block
*boot_block
= NULL
;
132 struct csys_block
*conf_block
= NULL
;
133 struct csys_block
*webp_block
= NULL
;
134 struct csys_block
*code_block
= NULL
;
136 struct csys_block blocks
[MAX_NUM_BLOCKS
];
139 static struct board_info boards
[] = {
140 /* The original Edimax products */
141 BOARD_ADM("BR-6104K", "Edimax BR-6104K", 2, SIG_BR6104K
),
142 BOARD_ADM("BR-6104KP", "Edimax BR-6104KP", 2, SIG_BR6104KP
),
143 BOARD_ADM("BR-6104Wg", "Edimax BR-6104Wg", 2, SIG_BR6104Wg
),
144 BOARD_ADM("BR-6114WG", "Edimax BR-6114WG", 2, SIG_BR6114WG
),
145 BOARD_ADM("BR-6524K", "Edimax BR-6524K", 2, SIG_BR6524K
),
146 BOARD_ADM("BR-6524KP", "Edimax BR-6524KP", 2, SIG_BR6524KP
),
147 BOARD_ADM("BR-6524N", "Edimax BR-6524N", 2, SIG_BR6524N
),
148 BOARD_ADM("BR-6524WG", "Edimax BR-6524WG", 4, SIG_BR6524WG
),
149 BOARD_ADM("BR-6524WP", "Edimax BR-6524WP", 4, SIG_BR6524WP
),
150 BOARD_ADM("BR-6541K", "Edimax BR-6541K", 2, SIG_BR6541K
),
151 BOARD_ADM("BR-6541KP", "Edimax BR-6541K", 2, SIG_BR6541KP
),
152 BOARD_ADM("BR-6541WP", "Edimax BR-6541WP", 4, SIG_BR6541WP
),
153 BOARD_ADM("EW-7207APg", "Edimax EW-7207APg", 2, SIG_EW7207APg
),
154 BOARD_ADM("PS-1205UWg", "Edimax PS-1205UWg", 2, SIG_PS1205UWg
),
155 BOARD_ADM("PS-3205U", "Edimax PS-3205U", 2, SIG_PS3205U
),
156 BOARD_ADM("PS-3205UWg", "Edimax PS-3205UWg", 2, SIG_PS3205UWg
),
158 /* Hawking products */
159 BOARD_ADM("H2BR4", "Hawking H2BR4", 2, SIG_H2BR4
),
160 BOARD_ADM("H2WR54G", "Hawking H2WR54G", 4, SIG_H2WR54G
),
162 /* Planet products */
163 BOARD_ADM("XRT-401D", "Planet XRT-401D", 2, SIG_XRT401D
),
164 BOARD_ADM("XRT-402D", "Planet XRT-402D", 2, SIG_XRT402D
),
166 /* Conceptronic products */
167 BOARD_ADM("C54BSR4", "Conceptronic C54BSR4", 2, SIG_C54BSR4
),
169 /* OSBRiDGE products */
170 BOARD_ADM("5GXi", "OSBDRiDGE 5GXi", 2, SIG_5GXI
),
178 #define ERR(fmt, ...) do { \
180 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
183 #define ERRS(fmt, ...) do { \
186 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", progname, ## __VA_ARGS__ \
190 #define WARN(fmt, ...) do { \
191 fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
194 #define DBG(lev, fmt, ...) do { \
195 if (verblevel < lev) \
197 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
201 #define ERR_INVALID_IMAGE -2
206 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
207 struct board_info
*board
;
209 fprintf(stream
, "Usage: %s [OPTIONS...] <file>\n", progname
);
213 " -B <board> create image for the board specified with <board>.\n"
214 " valid <board> values:\n"
216 for (board
= boards
; board
->model
!= NULL
; board
++){
219 board
->model
, board
->name
);
222 " -d don't throw error on invalid images\n"
223 " -k keep invalid images\n"
224 " -b <file>[:<align>[:<padc>]]\n"
225 " add boot code to the image\n"
226 " -c <file>[:<align>[:<padc>]]\n"
227 " add configuration settings to the image\n"
228 " -r <file>:[<addr>][:<align>[:<padc>]]\n"
229 " add runtime code to the image\n"
230 " -w [<file>:[<addr>][:<align>[:<padc>]]]\n"
231 " add webpages to the image\n"
232 " -x <file>[:<align>[:<padc>]]\n"
233 " add extra data at the end of the image\n"
234 " -h show this screen\n"
236 " <file> write output to the file <file>\n"
242 static inline uint32_t align(uint32_t base
, uint32_t alignment
)
247 ret
= (base
+ alignment
- 1);
248 ret
&= ~(alignment
-1);
260 str2u32(char *arg
, uint32_t *val
)
266 t
= strtoul(arg
, &err
, 0);
267 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
277 str2u16(char *arg
, uint16_t *val
)
283 t
= strtoul(arg
, &err
, 0);
284 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
293 str2u8(char *arg
, uint8_t *val
)
299 t
= strtoul(arg
, &err
, 0);
300 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
309 str2sig(char *arg
, uint32_t *sig
)
311 if (strlen(arg
) != 4)
314 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
321 parse_arg(char *arg
, char *buf
, char *argv
[])
329 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
342 if (argl
>= MAX_ARG_LEN
) {
343 /* argument is too long */
344 argl
= MAX_ARG_LEN
-1;
347 memcpy(buf
, arg
, argl
);
350 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
351 tok
= strsep(ap
, ":");
356 else if (tok
[0] == '\0') {
369 required_arg(char c
, char *arg
)
371 if (arg
== NULL
|| *arg
!= '-')
374 ERR("option -%c requires an argument\n", c
);
380 is_empty_arg(char *arg
)
391 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
393 for ( ; len
> 0; len
--) {
400 csum8_get(struct csum_state
*css
)
410 csum16_update(void *data
, uint32_t len
, struct csum_state
*css
)
416 t
= css
->tmp
+ (p
[0]<<8);
417 css
->val
+= LE16_TO_HOST(t
);
423 for ( ; len
> 1; len
-= 2, p
+=2 ) {
424 t
= p
[0] + (p
[1] << 8);
425 css
->val
+= LE16_TO_HOST(t
);
436 csum16_get(struct csum_state
*css
)
440 csum16_update(&pad
, 1, css
);
441 return ~css
->val
+ 1;
446 csum_init(struct csum_state
*css
, int size
)
456 csum_update(void *data
, 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
);
484 ERR("invalid checksum size\n");
493 * routines to write data to the output file
496 write_out_data(FILE *outfile
, void *data
, size_t len
,
497 struct csum_state
*css
)
503 fwrite(ptr
, len
, 1, outfile
);
505 ERRS("unable to write output file");
510 csum_update(ptr
, len
, css
);
518 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
519 struct csum_state
*css
)
522 size_t buflen
= sizeof(buf
);
525 memset(buf
, padc
, buflen
);
530 err
= write_out_data(outfile
, buf
, buflen
, css
);
542 block_stat_file(struct csys_block
*block
)
547 if (block
->file_name
== NULL
)
550 err
= stat(block
->file_name
, &st
);
552 ERRS("stat failed on %s", block
->file_name
);
556 block
->file_size
= st
.st_size
;
562 block_writeout_hdr(FILE *outfile
, struct csys_block
*block
)
564 struct csys_header hdr
;
567 if (block
->size_hdr
== 0)
570 /* setup header fields */
571 memcpy(hdr
.sig
, block
->sig
, 4);
572 hdr
.addr
= HOST_TO_LE32(block
->addr
);
573 hdr
.size
= HOST_TO_LE32(block
->size
- block
->size_hdr
- block
->size_csum
);
575 DBG(1,"writing header for block");
576 res
= write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
),NULL
);
583 block_writeout_file(FILE *outfile
, struct csys_block
*block
)
585 char buf
[FILE_BUF_LEN
];
586 size_t buflen
= sizeof(buf
);
591 if (block
->file_name
== NULL
)
594 if (block
->file_size
== 0)
598 f
= fopen(block
->file_name
,"r");
600 ERRS("unable to open file: %s", block
->file_name
);
604 len
= block
->file_size
;
609 /* read data from source file */
611 fread(buf
, buflen
, 1, f
);
613 ERRS("unable to read from file: %s", block
->file_name
);
618 res
= write_out_data(outfile
, buf
, buflen
, block
->css
);
631 block_writeout_data(FILE *outfile
, struct csys_block
*block
)
636 res
= block_writeout_file(outfile
, block
);
640 /* write padding data if neccesary */
641 padlen
= block
->size_avail
- block
->file_size
;
642 DBG(1,"padding block, length=%zu", padlen
);
643 res
= write_out_padding(outfile
, padlen
, block
->padc
, block
->css
);
650 block_writeout_csum(FILE *outfile
, struct csys_block
*block
)
655 if (block
->size_csum
== 0)
658 DBG(1,"writing checksum for block");
659 csum
= HOST_TO_LE16(csum_get(block
->css
));
660 res
= write_out_data(outfile
, (uint8_t *)&csum
, block
->size_csum
, NULL
);
667 block_writeout(FILE *outfile
, struct csys_block
*block
)
670 struct csum_state css
;
679 DBG(2, "writing block, file=%s, file_size=%d, space=%d",
680 block
->file_name
, block
->file_size
, block
->size_avail
);
681 res
= block_writeout_hdr(outfile
, block
);
685 if (block
->size_csum
!= 0) {
687 csum_init(&css
, block
->size_csum
);
690 res
= block_writeout_data(outfile
, block
);
694 res
= block_writeout_csum(outfile
, block
);
703 write_out_blocks(FILE *outfile
)
705 struct csys_block
*block
;
708 res
= block_writeout(outfile
, boot_block
);
712 res
= block_writeout(outfile
, conf_block
);
716 res
= block_writeout(outfile
, webp_block
);
720 res
= block_writeout(outfile
, code_block
);
725 for (i
=0; i
< num_blocks
; i
++) {
728 if (block
->type
!= BLOCK_TYPE_XTRA
)
731 res
= block_writeout(outfile
, block
);
741 find_board(char *model
)
743 struct board_info
*ret
;
744 struct board_info
*board
;
747 for (board
= boards
; board
->model
!= NULL
; board
++){
748 if (strcasecmp(model
, board
->model
) == 0) {
759 parse_opt_board(char ch
, char *arg
)
762 DBG(1,"parsing board option: -%c %s", ch
, arg
);
765 ERR("only one board option allowed");
769 if (required_arg(ch
, arg
))
772 board
= find_board(arg
);
774 ERR("invalid/unknown board specified: %s", arg
);
783 parse_opt_block(char ch
, char *arg
)
785 char buf
[MAX_ARG_LEN
];
786 char *argv
[MAX_ARG_COUNT
];
788 struct csys_block
*block
;
791 if ( num_blocks
> MAX_NUM_BLOCKS
) {
792 ERR("too many blocks specified");
796 block
= &blocks
[num_blocks
];
798 /* setup default field values */
799 block
->need_file
= 1;
805 WARN("only one boot block allowed");
808 block
->type
= BLOCK_TYPE_BOOT
;
813 WARN("only one config block allowed");
816 block
->type
= BLOCK_TYPE_CONF
;
821 WARN("only one web block allowed");
824 block
->type
= BLOCK_TYPE_WEBP
;
825 block
->size_hdr
= sizeof(struct csys_header
);
826 block
->size_csum
= CSUM_SIZE_8
;
827 block
->need_file
= 0;
832 WARN("only one runtime block allowed");
835 block
->type
= BLOCK_TYPE_CODE
;
836 block
->size_hdr
= sizeof(struct csys_header
);
837 block
->size_csum
= CSUM_SIZE_16
;
841 block
->type
= BLOCK_TYPE_XTRA
;
844 ERR("unknown block type \"%c\"", ch
);
848 parse_arg(arg
, buf
, argv
);
852 if (!is_empty_arg(p
)) {
853 block
->file_name
= strdup(p
);
854 if (block
->file_name
== NULL
) {
855 ERR("not enough memory");
858 } else if (block
->need_file
){
859 ERR("no file specified in %s", arg
);
863 if (block
->size_hdr
) {
865 if (!is_empty_arg(p
)) {
866 if (str2u32(p
, &block
->addr
) != 0) {
867 ERR("invalid start address in %s", arg
);
875 if (!is_empty_arg(p
)) {
876 if (str2u32(p
, &block
->align
) != 0) {
877 ERR("invalid alignment value in %s", arg
);
880 block
->align_set
= 1;
884 if (!is_empty_arg(p
) && (str2u8(p
, &block
->padc
) != 0)) {
885 ERR("invalid paddig character in %s", arg
);
898 struct csys_block
*block
;
904 /* collecting stats */
905 for (i
= 0; i
< num_blocks
; i
++) {
907 res
= block_stat_file(block
);
915 block
->size
= board
->boot_size
;
916 if (block
->file_size
> board
->boot_size
) {
917 WARN("boot block is too big");
918 res
= ERR_INVALID_IMAGE
;
921 offs
+= board
->boot_size
;
923 /* configuration data */
926 block
->size
= board
->conf_size
;
927 if (block
->file_size
> board
->conf_size
) {
928 WARN("config block is too big");
929 res
= ERR_INVALID_IMAGE
;
932 offs
+= board
->conf_size
;
938 memcpy(block
->sig
, board
->sig_webp
, 4);
940 if (block
->addr_set
== 0)
941 block
->addr
= board
->addr_webp
;
943 if (block
->align_set
== 0)
944 block
->align
= DEFAULT_BLOCK_ALIGN
;
946 block
->size
= align(offs
+ block
->file_size
+ block
->size_hdr
+
947 block
->size_csum
, block
->align
) - offs
;
949 if (block
->size
> board
->webp_size_max
) {
950 WARN("webpages block is too big");
951 res
= ERR_INVALID_IMAGE
;
954 DBG(2,"webpages start at %08x, size=%08x", offs
,
958 if (offs
> board
->flash_size
) {
959 WARN("webp block is too big");
960 res
= ERR_INVALID_IMAGE
;
967 memcpy(code_block
->sig
, SIG_CSYS
, 4);
969 if (block
->addr_set
== 0)
970 block
->addr
= board
->addr_code
;
972 if (block
->align_set
== 0)
973 block
->align
= DEFAULT_BLOCK_ALIGN
;
975 block
->size
= align(offs
+ block
->file_size
+
976 block
->size_hdr
+ block
->size_csum
,
977 block
->align
) - offs
;
979 DBG(2,"code block start at %08x, size=%08x", offs
,
983 if (offs
> board
->flash_size
) {
984 WARN("code block is too big");
985 res
= ERR_INVALID_IMAGE
;
989 for (i
= 0; i
< num_blocks
; i
++) {
992 if (block
->type
!= BLOCK_TYPE_XTRA
)
995 if (block
->align_set
== 0)
996 block
->align
= DEFAULT_BLOCK_ALIGN
;
998 block
->size
= align(offs
+ block
->file_size
,
999 block
->align
) - offs
;
1001 DBG(2,"file %s start at %08x, size=%08x, align=%08x",
1002 block
->file_name
, offs
, block
->size
, block
->align
);
1004 offs
+= block
->size
;
1005 if (offs
> board
->flash_size
) {
1006 WARN("file %s is too big, size=%d, avail=%d",
1007 block
->file_name
, block
->file_size
,
1008 board
->flash_size
- offs
);
1009 res
= ERR_INVALID_IMAGE
;
1013 for (i
= 0; i
< num_blocks
; i
++) {
1016 block
->size_avail
= block
->size
- block
->size_hdr
-
1019 if (block
->size_avail
< block
->file_size
) {
1020 WARN("file %s is too big, size=%d, avail=%d",
1021 block
->file_name
, block
->file_size
,
1023 res
= ERR_INVALID_IMAGE
;
1032 main(int argc
, char *argv
[])
1034 int optinvalid
= 0; /* flag for invalid option */
1036 int res
= ERR_FATAL
;
1040 progname
=basename(argv
[0]);
1042 opterr
= 0; /* could not print standard getopt error messages */
1046 c
= getopt(argc
, argv
, "b:B:c:dhkr:vw:x:");
1055 optinvalid
= parse_opt_block(c
,optarg
);
1058 if (optarg
!= NULL
&& *optarg
== '-') {
1063 optinvalid
= parse_opt_block(c
,optarg
);
1066 invalid_causes_error
= 0;
1069 keep_invalid_images
= 1;
1072 optinvalid
= parse_opt_board(c
,optarg
);
1078 usage(EXIT_SUCCESS
);
1084 if (optinvalid
!= 0 ){
1085 ERR("invalid option: -%c", optopt
);
1090 if (board
== NULL
) {
1091 ERR("no board specified");
1095 if (optind
== argc
) {
1096 ERR("no output file specified");
1100 ofname
= argv
[optind
++];
1102 if (optind
< argc
) {
1103 ERR("invalid option: %s", argv
[optind
]);
1107 res
= process_blocks();
1108 if (res
== ERR_FATAL
)
1111 if (res
== ERR_INVALID_IMAGE
) {
1112 if (invalid_causes_error
)
1115 if (keep_invalid_images
== 0) {
1116 WARN("generation of invalid images \"%s\" disabled", ofname
);
1120 WARN("generating invalid image: \"%s\"", ofname
);
1123 outfile
= fopen(ofname
, "w");
1124 if (outfile
== NULL
) {
1125 ERRS("could not open \"%s\" for writing", ofname
);
1130 if (write_out_blocks(outfile
) != 0) {
1135 DBG(1,"Image file %s completed.", ofname
);
1140 if (res
== ERR_FATAL
) {
1144 if (res
== ERR_FATAL
)
1145 return EXIT_FAILURE
;
1147 return EXIT_SUCCESS
;