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 */
22 #if defined(__CYGWIN__)
23 # include <byteswap.h>
28 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
29 # define HOST_TO_LE16(x) (x)
30 # define HOST_TO_LE32(x) (x)
31 # define LE16_TO_HOST(x) (x)
32 # define LE32_TO_HOST(x) (x)
34 # define HOST_TO_LE16(x) bswap_16(x)
35 # define HOST_TO_LE32(x) bswap_32(x)
36 # define LE16_TO_HOST(x) bswap_16(x)
37 # define LE32_TO_HOST(x) bswap_32(x)
40 #define MAX_NUM_BLOCKS 8
41 #define MAX_ARG_COUNT 32
42 #define MAX_ARG_LEN 1024
43 #define FILE_BUF_LEN (16*1024)
44 #define CSYS_PADC 0xFF
46 #define BLOCK_TYPE_BOOT 0
47 #define BLOCK_TYPE_CONF 1
48 #define BLOCK_TYPE_WEBP 2
49 #define BLOCK_TYPE_CODE 3
50 #define BLOCK_TYPE_XTRA 4
52 #define DEFAULT_BLOCK_ALIGN 0x10000U
54 #define CSUM_SIZE_NONE 0
56 #define CSUM_SIZE_16 2
68 int type
; /* type of the block */
71 char *file_name
; /* name of the file */
72 uint32_t file_size
; /* length of the file */
74 unsigned char sig
[SIG_LEN
];
86 struct csum_state
*css
;
95 char sig_boot
[SIG_LEN
];
96 char sig_conf
[SIG_LEN
];
97 char sig_webp
[SIG_LEN
];
102 uint32_t webp_size_max
;
109 #define BOARD(m, n, f, sigb, sigw, bs, cs, ws, ac, aw) {\
110 .model = m, .name = n, .flash_size = f<<20, \
111 .sig_boot = sigb, .sig_conf = SIG_CONF, .sig_webp = sigw, \
112 .boot_size = bs, .conf_size = cs, \
113 .webp_size = ws, .webp_size_max = 3*0x10000, \
114 .addr_code = ac, .addr_webp = aw \
117 #define BOARD_ADM(m,n,f, sigw) BOARD(m,n,f, ADM_BOOT_SIG, sigw, \
118 ADM_BOOT_SIZE, ADM_CONF_SIZE, ADM_WEBP_SIZE, \
119 ADM_CODE_ADDR, ADM_WEBP_ADDR)
128 int invalid_causes_error
= 1;
129 int keep_invalid_images
= 0;
131 struct board_info
*board
= NULL
;
133 struct csys_block
*boot_block
= NULL
;
134 struct csys_block
*conf_block
= NULL
;
135 struct csys_block
*webp_block
= NULL
;
136 struct csys_block
*code_block
= NULL
;
138 struct csys_block blocks
[MAX_NUM_BLOCKS
];
141 static struct board_info boards
[] = {
142 /* The original Edimax products */
143 BOARD_ADM("BR-6104K", "Edimax BR-6104K", 2, SIG_BR6104K
),
144 BOARD_ADM("BR-6104KP", "Edimax BR-6104KP", 2, SIG_BR6104KP
),
145 BOARD_ADM("BR-6104Wg", "Edimax BR-6104Wg", 2, SIG_BR6104Wg
),
146 BOARD_ADM("BR-6114WG", "Edimax BR-6114WG", 2, SIG_BR6114WG
),
147 BOARD_ADM("BR-6524K", "Edimax BR-6524K", 2, SIG_BR6524K
),
148 BOARD_ADM("BR-6524KP", "Edimax BR-6524KP", 2, SIG_BR6524KP
),
149 BOARD_ADM("BR-6524N", "Edimax BR-6524N", 2, SIG_BR6524N
),
150 BOARD_ADM("BR-6524WG", "Edimax BR-6524WG", 4, SIG_BR6524WG
),
151 BOARD_ADM("BR-6524WP", "Edimax BR-6524WP", 4, SIG_BR6524WP
),
152 BOARD_ADM("BR-6541K", "Edimax BR-6541K", 2, SIG_BR6541K
),
153 BOARD_ADM("BR-6541KP", "Edimax BR-6541K", 2, SIG_BR6541KP
),
154 BOARD_ADM("BR-6541WP", "Edimax BR-6541WP", 4, SIG_BR6541WP
),
155 BOARD_ADM("EW-7207APg", "Edimax EW-7207APg", 2, SIG_EW7207APg
),
156 BOARD_ADM("PS-1205UWg", "Edimax PS-1205UWg", 2, SIG_PS1205UWg
),
157 BOARD_ADM("PS-3205U", "Edimax PS-3205U", 2, SIG_PS3205U
),
158 BOARD_ADM("PS-3205UWg", "Edimax PS-3205UWg", 2, SIG_PS3205UWg
),
160 /* Hawking products */
161 BOARD_ADM("H2BR4", "Hawking H2BR4", 2, SIG_H2BR4
),
162 BOARD_ADM("H2WR54G", "Hawking H2WR54G", 4, SIG_H2WR54G
),
164 /* Planet products */
165 BOARD_ADM("XRT-401D", "Planet XRT-401D", 2, SIG_XRT401D
),
166 BOARD_ADM("XRT-402D", "Planet XRT-402D", 2, SIG_XRT402D
),
168 /* Conceptronic products */
169 BOARD_ADM("C54BSR4", "Conceptronic C54BSR4", 2, SIG_C54BSR4
),
171 /* OSBRiDGE products */
172 BOARD_ADM("5GXi", "OSBDRiDGE 5GXi", 2, SIG_5GXI
),
180 #define ERR(fmt, ...) do { \
182 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
185 #define ERRS(fmt, ...) do { \
188 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", progname, ## __VA_ARGS__ \
192 #define WARN(fmt, ...) do { \
193 fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
196 #define DBG(lev, fmt, ...) do { \
197 if (verblevel < lev) \
199 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
203 #define ERR_INVALID_IMAGE -2
208 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
209 struct board_info
*board
;
211 fprintf(stream
, "Usage: %s [OPTIONS...] <file>\n", progname
);
215 " -B <board> create image for the board specified with <board>.\n"
216 " valid <board> values:\n"
218 for (board
= boards
; board
->model
!= NULL
; board
++){
221 board
->model
, board
->name
);
224 " -d don't throw error on invalid images\n"
225 " -k keep invalid images\n"
226 " -b <file>[:<align>[:<padc>]]\n"
227 " add boot code to the image\n"
228 " -c <file>[:<align>[:<padc>]]\n"
229 " add configuration settings to the image\n"
230 " -r <file>:[<addr>][:<align>[:<padc>]]\n"
231 " add runtime code to the image\n"
232 " -w [<file>:[<addr>][:<align>[:<padc>]]]\n"
233 " add webpages to the image\n"
234 " -x <file>[:<align>[:<padc>]]\n"
235 " add extra data at the end of the image\n"
236 " -h show this screen\n"
238 " <file> write output to the file <file>\n"
244 static inline uint32_t align(uint32_t base
, uint32_t alignment
)
249 ret
= (base
+ alignment
- 1);
250 ret
&= ~(alignment
-1);
262 str2u32(char *arg
, uint32_t *val
)
268 t
= strtoul(arg
, &err
, 0);
269 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
279 str2u16(char *arg
, uint16_t *val
)
285 t
= strtoul(arg
, &err
, 0);
286 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
295 str2u8(char *arg
, uint8_t *val
)
301 t
= strtoul(arg
, &err
, 0);
302 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
311 str2sig(char *arg
, uint32_t *sig
)
313 if (strlen(arg
) != 4)
316 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
323 parse_arg(char *arg
, char *buf
, char *argv
[])
331 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
344 if (argl
>= MAX_ARG_LEN
) {
345 /* argument is too long */
346 argl
= MAX_ARG_LEN
-1;
349 memcpy(buf
, arg
, argl
);
352 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
353 tok
= strsep(ap
, ":");
358 else if (tok
[0] == '\0') {
371 required_arg(char c
, char *arg
)
373 if (arg
== NULL
|| *arg
!= '-')
376 ERR("option -%c requires an argument\n", c
);
382 is_empty_arg(char *arg
)
393 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
395 for ( ; len
> 0; len
--) {
402 csum8_get(struct csum_state
*css
)
412 csum16_update(void *data
, 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(void *data
, uint32_t len
, struct csum_state
*css
)
464 csum8_update(p
,len
,css
);
467 csum16_update(p
,len
,css
);
474 csum_get(struct csum_state
*css
)
480 ret
= csum8_get(css
);
483 ret
= csum16_get(css
);
486 ERR("invalid checksum size\n");
495 * routines to write data to the output file
498 write_out_data(FILE *outfile
, void *data
, size_t len
,
499 struct csum_state
*css
)
505 fwrite(ptr
, len
, 1, outfile
);
507 ERRS("unable to write output file");
512 csum_update(ptr
, len
, css
);
520 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
521 struct csum_state
*css
)
524 size_t buflen
= sizeof(buf
);
527 memset(buf
, padc
, buflen
);
532 err
= write_out_data(outfile
, buf
, buflen
, css
);
544 block_stat_file(struct csys_block
*block
)
549 if (block
->file_name
== NULL
)
552 err
= stat(block
->file_name
, &st
);
554 ERRS("stat failed on %s", block
->file_name
);
558 block
->file_size
= st
.st_size
;
564 block_writeout_hdr(FILE *outfile
, struct csys_block
*block
)
566 struct csys_header hdr
;
569 if (block
->size_hdr
== 0)
572 /* setup header fields */
573 memcpy(hdr
.sig
, block
->sig
, 4);
574 hdr
.addr
= HOST_TO_LE32(block
->addr
);
575 hdr
.size
= HOST_TO_LE32(block
->size
- block
->size_hdr
- block
->size_csum
);
577 DBG(1,"writing header for block");
578 res
= write_out_data(outfile
, (uint8_t *)&hdr
, sizeof(hdr
),NULL
);
585 block_writeout_file(FILE *outfile
, struct csys_block
*block
)
587 char buf
[FILE_BUF_LEN
];
588 size_t buflen
= sizeof(buf
);
593 if (block
->file_name
== NULL
)
596 if (block
->file_size
== 0)
600 f
= fopen(block
->file_name
,"r");
602 ERRS("unable to open file: %s", block
->file_name
);
606 len
= block
->file_size
;
611 /* read data from source file */
613 fread(buf
, buflen
, 1, f
);
615 ERRS("unable to read from file: %s", block
->file_name
);
620 res
= write_out_data(outfile
, buf
, buflen
, block
->css
);
633 block_writeout_data(FILE *outfile
, struct csys_block
*block
)
638 res
= block_writeout_file(outfile
, block
);
642 /* write padding data if neccesary */
643 padlen
= block
->size_avail
- block
->file_size
;
644 DBG(1,"padding block, length=%zu", padlen
);
645 res
= write_out_padding(outfile
, padlen
, block
->padc
, block
->css
);
652 block_writeout_csum(FILE *outfile
, struct csys_block
*block
)
657 if (block
->size_csum
== 0)
660 DBG(1,"writing checksum for block");
661 csum
= HOST_TO_LE16(csum_get(block
->css
));
662 res
= write_out_data(outfile
, (uint8_t *)&csum
, block
->size_csum
, NULL
);
669 block_writeout(FILE *outfile
, struct csys_block
*block
)
672 struct csum_state css
;
681 DBG(2, "writing block, file=%s, file_size=%d, space=%d",
682 block
->file_name
, block
->file_size
, block
->size_avail
);
683 res
= block_writeout_hdr(outfile
, block
);
687 if (block
->size_csum
!= 0) {
689 csum_init(&css
, block
->size_csum
);
692 res
= block_writeout_data(outfile
, block
);
696 res
= block_writeout_csum(outfile
, block
);
705 write_out_blocks(FILE *outfile
)
707 struct csys_block
*block
;
710 res
= block_writeout(outfile
, boot_block
);
714 res
= block_writeout(outfile
, conf_block
);
718 res
= block_writeout(outfile
, webp_block
);
722 res
= block_writeout(outfile
, code_block
);
727 for (i
=0; i
< num_blocks
; i
++) {
730 if (block
->type
!= BLOCK_TYPE_XTRA
)
733 res
= block_writeout(outfile
, block
);
743 find_board(char *model
)
745 struct board_info
*ret
;
746 struct board_info
*board
;
749 for (board
= boards
; board
->model
!= NULL
; board
++){
750 if (strcasecmp(model
, board
->model
) == 0) {
761 parse_opt_board(char ch
, char *arg
)
764 DBG(1,"parsing board option: -%c %s", ch
, arg
);
767 ERR("only one board option allowed");
771 if (required_arg(ch
, arg
))
774 board
= find_board(arg
);
776 ERR("invalid/unknown board specified: %s", arg
);
785 parse_opt_block(char ch
, char *arg
)
787 char buf
[MAX_ARG_LEN
];
788 char *argv
[MAX_ARG_COUNT
];
790 struct csys_block
*block
;
793 if ( num_blocks
> MAX_NUM_BLOCKS
) {
794 ERR("too many blocks specified");
798 block
= &blocks
[num_blocks
];
800 /* setup default field values */
801 block
->need_file
= 1;
807 WARN("only one boot block allowed");
810 block
->type
= BLOCK_TYPE_BOOT
;
815 WARN("only one config block allowed");
818 block
->type
= BLOCK_TYPE_CONF
;
823 WARN("only one web block allowed");
826 block
->type
= BLOCK_TYPE_WEBP
;
827 block
->size_hdr
= sizeof(struct csys_header
);
828 block
->size_csum
= CSUM_SIZE_8
;
829 block
->need_file
= 0;
834 WARN("only one runtime block allowed");
837 block
->type
= BLOCK_TYPE_CODE
;
838 block
->size_hdr
= sizeof(struct csys_header
);
839 block
->size_csum
= CSUM_SIZE_16
;
843 block
->type
= BLOCK_TYPE_XTRA
;
846 ERR("unknown block type \"%c\"", ch
);
850 parse_arg(arg
, buf
, argv
);
854 if (!is_empty_arg(p
)) {
855 block
->file_name
= strdup(p
);
856 if (block
->file_name
== NULL
) {
857 ERR("not enough memory");
860 } else if (block
->need_file
){
861 ERR("no file specified in %s", arg
);
865 if (block
->size_hdr
) {
867 if (!is_empty_arg(p
)) {
868 if (str2u32(p
, &block
->addr
) != 0) {
869 ERR("invalid start address in %s", arg
);
877 if (!is_empty_arg(p
)) {
878 if (str2u32(p
, &block
->align
) != 0) {
879 ERR("invalid alignment value in %s", arg
);
882 block
->align_set
= 1;
886 if (!is_empty_arg(p
) && (str2u8(p
, &block
->padc
) != 0)) {
887 ERR("invalid paddig character in %s", arg
);
900 struct csys_block
*block
;
906 /* collecting stats */
907 for (i
= 0; i
< num_blocks
; i
++) {
909 res
= block_stat_file(block
);
917 block
->size
= board
->boot_size
;
918 if (block
->file_size
> board
->boot_size
) {
919 WARN("boot block is too big");
920 res
= ERR_INVALID_IMAGE
;
923 offs
+= board
->boot_size
;
925 /* configuration data */
928 block
->size
= board
->conf_size
;
929 if (block
->file_size
> board
->conf_size
) {
930 WARN("config block is too big");
931 res
= ERR_INVALID_IMAGE
;
934 offs
+= board
->conf_size
;
940 memcpy(block
->sig
, board
->sig_webp
, 4);
942 if (block
->addr_set
== 0)
943 block
->addr
= board
->addr_webp
;
945 if (block
->align_set
== 0)
946 block
->align
= DEFAULT_BLOCK_ALIGN
;
948 block
->size
= align(offs
+ block
->file_size
+ block
->size_hdr
+
949 block
->size_csum
, block
->align
) - offs
;
951 if (block
->size
> board
->webp_size_max
) {
952 WARN("webpages block is too big");
953 res
= ERR_INVALID_IMAGE
;
956 DBG(2,"webpages start at %08x, size=%08x", offs
,
960 if (offs
> board
->flash_size
) {
961 WARN("webp block is too big");
962 res
= ERR_INVALID_IMAGE
;
969 memcpy(code_block
->sig
, SIG_CSYS
, 4);
971 if (block
->addr_set
== 0)
972 block
->addr
= board
->addr_code
;
974 if (block
->align_set
== 0)
975 block
->align
= DEFAULT_BLOCK_ALIGN
;
977 block
->size
= align(offs
+ block
->file_size
+
978 block
->size_hdr
+ block
->size_csum
,
979 block
->align
) - offs
;
981 DBG(2,"code block start at %08x, size=%08x", offs
,
985 if (offs
> board
->flash_size
) {
986 WARN("code block is too big");
987 res
= ERR_INVALID_IMAGE
;
991 for (i
= 0; i
< num_blocks
; i
++) {
994 if (block
->type
!= BLOCK_TYPE_XTRA
)
997 if (block
->align_set
== 0)
998 block
->align
= DEFAULT_BLOCK_ALIGN
;
1000 block
->size
= align(offs
+ block
->file_size
,
1001 block
->align
) - offs
;
1003 DBG(2,"file %s start at %08x, size=%08x, align=%08x",
1004 block
->file_name
, offs
, block
->size
, block
->align
);
1006 offs
+= block
->size
;
1007 if (offs
> board
->flash_size
) {
1008 WARN("file %s is too big, size=%d, avail=%d",
1009 block
->file_name
, block
->file_size
,
1010 board
->flash_size
- offs
);
1011 res
= ERR_INVALID_IMAGE
;
1015 for (i
= 0; i
< num_blocks
; i
++) {
1018 block
->size_avail
= block
->size
- block
->size_hdr
-
1021 if (block
->size_avail
< block
->file_size
) {
1022 WARN("file %s is too big, size=%d, avail=%d",
1023 block
->file_name
, block
->file_size
,
1025 res
= ERR_INVALID_IMAGE
;
1034 main(int argc
, char *argv
[])
1036 int optinvalid
= 0; /* flag for invalid option */
1038 int res
= ERR_FATAL
;
1042 progname
=basename(argv
[0]);
1044 opterr
= 0; /* could not print standard getopt error messages */
1048 c
= getopt(argc
, argv
, "b:B:c:dhkr:vw:x:");
1057 optinvalid
= parse_opt_block(c
,optarg
);
1060 if (optarg
!= NULL
&& *optarg
== '-') {
1065 optinvalid
= parse_opt_block(c
,optarg
);
1068 invalid_causes_error
= 0;
1071 keep_invalid_images
= 1;
1074 optinvalid
= parse_opt_board(c
,optarg
);
1080 usage(EXIT_SUCCESS
);
1086 if (optinvalid
!= 0 ){
1087 ERR("invalid option: -%c", optopt
);
1092 if (board
== NULL
) {
1093 ERR("no board specified");
1097 if (optind
== argc
) {
1098 ERR("no output file specified");
1102 ofname
= argv
[optind
++];
1104 if (optind
< argc
) {
1105 ERR("invalid option: %s", argv
[optind
]);
1109 res
= process_blocks();
1110 if (res
== ERR_FATAL
)
1113 if (res
== ERR_INVALID_IMAGE
) {
1114 if (invalid_causes_error
)
1117 if (keep_invalid_images
== 0) {
1118 WARN("generation of invalid images \"%s\" disabled", ofname
);
1122 WARN("generating invalid image: \"%s\"", ofname
);
1125 outfile
= fopen(ofname
, "w");
1126 if (outfile
== NULL
) {
1127 ERRS("could not open \"%s\" for writing", ofname
);
1132 if (write_out_blocks(outfile
) != 0) {
1137 DBG(1,"Image file %s completed.", ofname
);
1142 if (res
== ERR_FATAL
) {
1146 if (res
== ERR_FATAL
)
1147 return EXIT_FAILURE
;
1149 return EXIT_SUCCESS
;