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),
188 #define ERR(fmt, ...) do { \
190 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
193 #define ERRS(fmt, ...) do { \
196 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ \
200 #define WARN(fmt, ...) do { \
201 fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
204 #define DBG(lev, fmt, ...) do { \
205 if (verblevel < lev) \
207 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
211 #define ERR_INVALID_IMAGE -2
216 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
217 struct board_info *board;
219 fprintf(stream, "Usage: %s [OPTIONS...] <file>\n", progname);
223 " -B <board> create image for the board specified with <board>.\n"
224 " valid <board> values:\n"
226 for (board = boards; board->model != NULL; board++){
229 board->model, board->name);
232 " -d don't throw error on invalid images\n"
233 " -k keep invalid images\n"
234 " -b <file>[:<align>[:<padc>]]\n"
235 " add boot code to the image\n"
236 " -c <file>[:<align>[:<padc>]]\n"
237 " add configuration settings to the image\n"
238 " -r <file>:[<addr>][:<align>[:<padc>]]\n"
239 " add runtime code to the image\n"
240 " -w [<file>:[<addr>][:<align>[:<padc>]]]\n"
241 " add webpages to the image\n"
242 " -x <file>[:<align>[:<padc>]]\n"
243 " add extra data at the end of the image\n"
244 " -h show this screen\n"
246 " <file> write output to the file <file>\n"
252 static inline uint32_t align(uint32_t base, uint32_t alignment)
257 ret = (base + alignment - 1);
258 ret &= ~(alignment-1);
270 str2u32(char *arg, uint32_t *val)
276 t = strtoul(arg, &err, 0);
277 if (errno || (err==arg) || ((err != NULL) && *err)) {
287 str2u16(char *arg, uint16_t *val)
293 t = strtoul(arg, &err, 0);
294 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
303 str2u8(char *arg, uint8_t *val)
309 t = strtoul(arg, &err, 0);
310 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x100)) {
319 str2sig(char *arg, uint32_t *sig)
321 if (strlen(arg) != 4)
324 *sig = arg[0] | (arg[1] << 8) | (arg[2] << 16) | (arg[3] << 24);
331 parse_arg(char *arg, char *buf, char *argv[])
339 memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
352 if (argl >= MAX_ARG_LEN) {
353 /* argument is too long */
354 argl = MAX_ARG_LEN-1;
357 memcpy(buf, arg, argl);
360 for (i = 0; i < MAX_ARG_COUNT; i++) {
361 tok = strsep(ap, ":");
366 else if (tok[0] == '\0') {
379 required_arg(char c, char *arg)
381 if (arg == NULL || *arg != '-')
384 ERR("option -%c requires an argument\n", c);
390 is_empty_arg(char *arg)
401 csum8_update(uint8_t *p, uint32_t len, struct csum_state *css)
403 for ( ; len > 0; len --) {
410 csum8_get(struct csum_state *css)
420 csum16_update(uint8_t *p, uint32_t len, struct csum_state *css)
425 t = css->tmp + (p[0]<<8);
426 css->val += LE16_TO_HOST(t);
432 for ( ; len > 1; len -= 2, p +=2 ) {
433 t = p[0] + (p[1] << 8);
434 css->val += LE16_TO_HOST(t);
445 csum16_get(struct csum_state *css)
449 csum16_update(&pad, 1, css);
450 return ~css->val + 1;
455 csum_init(struct csum_state *css, int size)
465 csum_update(uint8_t *p, uint32_t len, struct csum_state *css)
469 csum8_update(p,len,css);
472 csum16_update(p,len,css);
479 csum_get(struct csum_state *css)
485 ret = csum8_get(css);
488 ret = csum16_get(css);
497 * routines to write data to the output file
500 write_out_data(FILE *outfile, uint8_t *data, size_t len,
501 struct csum_state *css)
505 fwrite(data, len, 1, outfile);
507 ERRS("unable to write output file");
512 csum_update(data, 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->align-block->size_hdr);
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=%d", 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];
791 struct csys_block *block;
794 if ( num_blocks > MAX_NUM_BLOCKS ) {
795 ERR("too many blocks specified");
799 block = &blocks[num_blocks];
801 /* setup default field values */
802 block->need_file = 1;
808 WARN("only one boot block allowed");
811 block->type = BLOCK_TYPE_BOOT;
816 WARN("only one config block allowed");
819 block->type = BLOCK_TYPE_CONF;
824 WARN("only one web block allowed");
827 block->type = BLOCK_TYPE_WEBP;
828 block->size_hdr = sizeof(struct csys_header);
829 block->size_csum = CSUM_SIZE_8;
830 block->need_file = 0;
835 WARN("only one runtime block allowed");
838 block->type = BLOCK_TYPE_CODE;
839 block->size_hdr = sizeof(struct csys_header);
840 block->size_csum = CSUM_SIZE_16;
844 block->type = BLOCK_TYPE_XTRA;
847 ERR("unknown block type \"%c\"", ch);
851 argc = parse_arg(arg, buf, argv);
855 if (!is_empty_arg(p)) {
856 block->file_name = strdup(p);
857 if (block->file_name == NULL) {
858 ERR("not enough memory");
861 } else if (block->need_file){
862 ERR("no file specified in %s", arg);
866 if (block->size_hdr) {
868 if (!is_empty_arg(p)) {
869 if (str2u32(p, &block->addr) != 0) {
870 ERR("invalid start address in %s", arg);
878 if (!is_empty_arg(p)) {
879 if (str2u32(p, &block->align) != 0) {
880 ERR("invalid alignment value in %s", arg);
883 block->align_set = 1;
887 if (!is_empty_arg(p) && (str2u8(p, &block->padc) != 0)) {
888 ERR("invalid paddig character in %s", arg);
901 struct csys_block *block;
907 /* collecting stats */
908 for (i = 0; i < num_blocks; i++) {
910 res = block_stat_file(block);
918 block->size = board->boot_size;
919 if (block->file_size > board->boot_size) {
920 WARN("boot block is too big");
921 res = ERR_INVALID_IMAGE;
924 offs += board->boot_size;
926 /* configuration data */
929 block->size = board->conf_size;
930 if (block->file_size > board->conf_size) {
931 WARN("config block is too big");
932 res = ERR_INVALID_IMAGE;
935 offs += board->conf_size;
941 memcpy(block->sig, board->sig_webp, 4);
943 if (block->addr_set == 0)
944 block->addr = board->addr_webp;
946 if (block->align_set == 0)
947 block->align = DEFAULT_BLOCK_ALIGN;
949 block->size = align(offs + block->file_size + block->size_hdr +
950 block->size_csum, block->align) - offs;
952 if (block->size > board->webp_size_max) {
953 WARN("webpages block is too big");
954 res = ERR_INVALID_IMAGE;
957 DBG(2,"webpages start at %08x, size=%08x", offs,
961 if (offs > board->flash_size) {
962 WARN("webp block is too big");
963 res = ERR_INVALID_IMAGE;
970 memcpy(code_block->sig, SIG_CSYS, 4);
972 if (block->addr_set == 0)
973 block->addr = board->addr_code;
975 if (block->align_set == 0)
976 block->align = DEFAULT_BLOCK_ALIGN;
978 block->size = align(offs + block->file_size +
979 block->size_hdr + block->size_csum,
980 block->align) - offs;
982 DBG(2,"code block start at %08x, size=%08x", offs,
986 if (offs > board->flash_size) {
987 WARN("code block is too big");
988 res = ERR_INVALID_IMAGE;
992 for (i = 0; i < num_blocks; i++) {
995 if (block->type != BLOCK_TYPE_XTRA)
998 if (block->align_set == 0)
999 block->align = DEFAULT_BLOCK_ALIGN;
1001 block->size = align(offs + block->file_size,
1002 block->align) - offs;
1004 DBG(2,"file %s start at %08x, size=%08x, align=%08x",
1005 block->file_name, offs, block->size, block->align);
1007 offs += block->size;
1008 if (offs > board->flash_size) {
1009 WARN("file %s is too big, size=%d, avail=%d",
1010 block->file_name, block->file_size,
1011 board->flash_size - offs);
1012 res = ERR_INVALID_IMAGE;
1016 for (i = 0; i < num_blocks; i++) {
1019 block->size_avail = block->size - block->size_hdr -
1022 if (block->size_avail < block->file_size) {
1023 WARN("file %s is too big, size=%d, avail=%d",
1024 block->file_name, block->file_size,
1026 res = ERR_INVALID_IMAGE;
1035 main(int argc, char *argv[])
1037 int optinvalid = 0; /* flag for invalid option */
1039 int res = ERR_FATAL;
1043 progname=basename(argv[0]);
1045 opterr = 0; /* could not print standard getopt error messages */
1049 c = getopt(argc, argv, "b:B:c:dhkr:vw:x:");
1058 optinvalid = parse_opt_block(c,optarg);
1061 if (optarg != NULL && *optarg == '-') {
1066 optinvalid = parse_opt_block(c,optarg);
1069 invalid_causes_error = 0;
1072 keep_invalid_images = 1;
1075 optinvalid = parse_opt_board(c,optarg);
1081 usage(EXIT_SUCCESS);
1087 if (optinvalid != 0 ){
1088 ERR("invalid option: -%c", optopt);
1093 if (board == NULL) {
1094 ERR("no board specified");
1098 if (optind == argc) {
1099 ERR("no output file specified");
1103 ofname = argv[optind++];
1105 if (optind < argc) {
1106 ERR("invalid option: %s", argv[optind]);
1110 res = process_blocks();
1111 if (res == ERR_FATAL)
1114 if (res == ERR_INVALID_IMAGE) {
1115 if (invalid_causes_error)
1118 if (keep_invalid_images == 0) {
1119 WARN("generation of invalid images disabled", ofname);
1123 WARN("generating invalid image", ofname);
1126 outfile = fopen(ofname, "w");
1127 if (outfile == NULL) {
1128 ERRS("could not open \"%s\" for writing", ofname);
1133 if (write_out_blocks(outfile) != 0) {
1138 DBG(1,"Image file %s completed.", ofname);
1143 if (res == ERR_FATAL) {
1147 if (res == ERR_FATAL)
1148 return EXIT_FAILURE;
1150 return EXIT_SUCCESS;