package/busybox: fix endianes issue under FreeBSD 8.1
[openwrt.git] / tools / firmware-utils / src / mkcsysimg.c
index e1c0e71..4f2352a 100644 (file)
@@ -1,7 +1,6 @@
 /*
- *  $Id$
  *
- *  Copyright (C) 2007 Gabor Juhos <juhosg@freemail.hu>
+ *  Copyright (C) 2007-2009 Gabor Juhos <juhosg@openwrt.org>
  *
  *  This program was based on the code found in various Linux
  *  source tarballs released by Edimax for it's devices.
@@ -52,8 +51,6 @@
 #  define LE32_TO_HOST(x)      bswap_32(x)
 #endif
 
-#define ALIGN(x,y)     ((x)+((y)-1)) & ~((y)-1)
-
 #define MAX_NUM_BLOCKS 8
 #define MAX_ARG_COUNT  32
 #define MAX_ARG_LEN    1024
 #define BLOCK_TYPE_CODE        3
 #define BLOCK_TYPE_XTRA        4
 
+#define DEFAULT_BLOCK_ALIGN    0x10000U
+
+#define CSUM_SIZE_NONE 0
+#define CSUM_SIZE_8    1
+#define CSUM_SIZE_16   2
+
 
 struct csum_state{
        int     size;
@@ -82,17 +85,18 @@ struct csys_block {
        char            *file_name;     /* name of the file */
        uint32_t        file_size;      /* length of the file */
 
-       uint32_t        size;
-       int             size_set;
+       unsigned char   sig[SIG_LEN];
+       uint32_t        addr;
+       int             addr_set;
+       uint32_t        align;
+       int             align_set;
        uint8_t         padc;
 
+       uint32_t        size;
        uint32_t        size_hdr;
        uint32_t        size_csum;
        uint32_t        size_avail;
 
-       unsigned char   sig[SIG_LEN];
-       uint32_t        addr;
-       int             addr_set;
        struct csum_state *css;
 };
 
@@ -152,6 +156,7 @@ static struct board_info boards[] = {
        /* The original Edimax products */
        BOARD_ADM("BR-6104K", "Edimax BR-6104K", 2, SIG_BR6104K),
        BOARD_ADM("BR-6104KP", "Edimax BR-6104KP", 2, SIG_BR6104KP),
+       BOARD_ADM("BR-6104Wg", "Edimax BR-6104Wg", 2, SIG_BR6104Wg),
        BOARD_ADM("BR-6114WG", "Edimax BR-6114WG", 2, SIG_BR6114WG),
        BOARD_ADM("BR-6524K", "Edimax BR-6524K", 2, SIG_BR6524K),
        BOARD_ADM("BR-6524KP", "Edimax BR-6524KP", 2, SIG_BR6524KP),
@@ -173,6 +178,12 @@ static struct board_info boards[] = {
        BOARD_ADM("XRT-401D", "Planet XRT-401D", 2, SIG_XRT401D),
        BOARD_ADM("XRT-402D", "Planet XRT-402D", 2, SIG_XRT402D),
 
+       /* Conceptronic products */
+       BOARD_ADM("C54BSR4", "Conceptronic C54BSR4", 2, SIG_C54BSR4),
+
+       /* OSBRiDGE products */
+       BOARD_ADM("5GXi", "OSBDRiDGE 5GXi", 2, SIG_5GXI),
+
        {.model = NULL}
 };
 
@@ -225,15 +236,15 @@ usage(int status)
        fprintf(stream,
 "  -d              don't throw error on invalid images\n"
 "  -k              keep invalid images\n"
-"  -b <file>[:<len>[:<padc>]]\n"
+"  -b <file>[:<align>[:<padc>]]\n"
 "                  add boot code to the image\n"
-"  -c <file>[:<len>[:<padc>]]\n"
+"  -c <file>[:<align>[:<padc>]]\n"
 "                  add configuration settings to the image\n"
-"  -r <file>:[<addr>][:<len>[:<padc>]]\n"
+"  -r <file>:[<addr>][:<align>[:<padc>]]\n"
 "                  add runtime code to the image\n"
-"  -w [<file>:[<addr>][:<len>[:<padc>]]]\n"
+"  -w [<file>:[<addr>][:<align>[:<padc>]]]\n"
 "                  add webpages to the image\n"
-"  -x <file>[:<len>[:<padc>]]\n"
+"  -x <file>[:<align>[:<padc>]]\n"
 "                  add extra data at the end of the image\n"
 "  -h              show this screen\n"
 "Parameters:\n"
@@ -243,6 +254,19 @@ usage(int status)
        exit(status);
 }
 
+static inline uint32_t align(uint32_t base, uint32_t alignment)
+{
+       uint32_t ret;
+
+       if (alignment) {
+               ret = (base + alignment - 1);
+               ret &= ~(alignment-1);
+       } else {
+               ret = base;
+       }
+
+       return ret;
+}
 
 /*
  * argument parsing
@@ -446,10 +470,10 @@ void
 csum_update(uint8_t *p, uint32_t len, struct csum_state *css)
 {
        switch (css->size) {
-       case 1:
+       case CSUM_SIZE_8:
                csum8_update(p,len,css);
                break;
-       case 2:
+       case CSUM_SIZE_16:
                csum16_update(p,len,css);
                break;
        }
@@ -462,10 +486,10 @@ csum_get(struct csum_state *css)
        uint16_t ret;
 
        switch (css->size) {
-       case 1:
+       case CSUM_SIZE_8:
                ret = csum8_get(css);
                break;
-       case 2:
+       case CSUM_SIZE_16:
                ret = csum16_get(css);
                break;
        }
@@ -553,7 +577,7 @@ block_writeout_hdr(FILE *outfile, struct csys_block *block)
        /* setup header fields */
        memcpy(hdr.sig, block->sig, 4);
        hdr.addr = HOST_TO_LE32(block->addr);
-       hdr.size = HOST_TO_LE32(block->size-block->size_hdr);
+       hdr.size = HOST_TO_LE32(block->size - block->size_hdr - block->size_csum);
 
        DBG(1,"writing header for block");
        res = write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr),NULL);
@@ -807,7 +831,7 @@ parse_opt_block(char ch, char *arg)
                }
                block->type = BLOCK_TYPE_WEBP;
                block->size_hdr = sizeof(struct csys_header);
-               block->size_csum = 1;
+               block->size_csum = CSUM_SIZE_8;
                block->need_file = 0;
                webp_block = block;
                break;
@@ -818,7 +842,7 @@ parse_opt_block(char ch, char *arg)
                }
                block->type = BLOCK_TYPE_CODE;
                block->size_hdr = sizeof(struct csys_header);
-               block->size_csum = 2;
+               block->size_csum = CSUM_SIZE_16;
                code_block = block;
                break;
        case 'x':
@@ -857,11 +881,11 @@ parse_opt_block(char ch, char *arg)
 
        p = argv[i++];
        if (!is_empty_arg(p)) {
-               if (str2u32(p, &block->size) != 0) {
-                       ERR("invalid block size in %s", arg);
+               if (str2u32(p, &block->align) != 0) {
+                       ERR("invalid alignment value in %s", arg);
                        return ERR_FATAL;
                }
-               block->size_set = 1;
+               block->align_set = 1;
        }
 
        p = argv[i++];
@@ -880,7 +904,7 @@ int
 process_blocks(void)
 {
        struct csys_block *block;
-       uint32_t size_avail;
+       uint32_t offs = 0;
        int i;
        int res;
 
@@ -893,72 +917,82 @@ process_blocks(void)
                        return res;
        }
 
-       size_avail = board->flash_size;
-
        /* bootloader */
        block = boot_block;
        if (block) {
-               if (block->size_set) {
-                       board->boot_size= block->size;
-               } else {
-                       block->size = board->boot_size;
-               }
-               if (block->size > size_avail) {
+               block->size = board->boot_size;
+               if (block->file_size > board->boot_size) {
                        WARN("boot block is too big");
                        res = ERR_INVALID_IMAGE;
                }
        }
-       size_avail -= board->boot_size;
+       offs += board->boot_size;
 
        /* configuration data */
        block = conf_block;
        if (block) {
-               if (block->size_set) {
-                       board->conf_size = block->size;
-               } else {
-                       block->size = board->conf_size;
-               }
-               if (block->size > size_avail) {
+               block->size = board->conf_size;
+               if (block->file_size > board->conf_size) {
                        WARN("config block is too big");
                        res = ERR_INVALID_IMAGE;
                }
-
        }
-       size_avail -= board->conf_size;
+       offs += board->conf_size;
 
        /* webpages */
        block = webp_block;
        if (block) {
-               if (block->size_set == 0)
-                       block->size = board->webp_size;
-               board->webp_size = block->size;
+
+               memcpy(block->sig, board->sig_webp, 4);
+
+               if (block->addr_set == 0)
+                       block->addr = board->addr_webp;
+
+               if (block->align_set == 0)
+                       block->align = DEFAULT_BLOCK_ALIGN;
+
+               block->size = align(offs + block->file_size + block->size_hdr +
+                               block->size_csum, block->align) - offs;
+
                if (block->size > board->webp_size_max) {
                        WARN("webpages block is too big");
                        res = ERR_INVALID_IMAGE;
                }
-               memcpy(block->sig, board->sig_webp, 4);
-               if (block->addr_set == 0)
-                       block->addr = board->addr_webp;
+
+               DBG(2,"webpages start at %08x, size=%08x", offs,
+                               block->size);
+
+               offs += block->size;
+               if (offs > board->flash_size) {
+                       WARN("webp block is too big");
+                       res = ERR_INVALID_IMAGE;
+               }
        }
-       size_avail -= board->webp_size;
 
        /* runtime code */
        block = code_block;
        if (block) {
-               if (block->size_set == 0) {
-                       block->size =ALIGN(block->file_size+ block->size_hdr +
-                               block->size_csum, 0x10000);
-               }
-               board->code_size = block->size;
-               if (board->code_size > size_avail) {
+               memcpy(code_block->sig, SIG_CSYS, 4);
+
+               if (block->addr_set == 0)
+                       block->addr = board->addr_code;
+
+               if (block->align_set == 0)
+                       block->align = DEFAULT_BLOCK_ALIGN;
+
+               block->size = align(offs + block->file_size +
+                               block->size_hdr + block->size_csum,
+                               block->align) - offs;
+
+               DBG(2,"code block start at %08x, size=%08x", offs,
+                               block->size);
+
+               offs += block->size;
+               if (offs > board->flash_size) {
                        WARN("code block is too big");
                        res = ERR_INVALID_IMAGE;
                }
-               memcpy(code_block->sig, SIG_CSYS, 4);
-               if (code_block->addr_set == 0)
-                       code_block->addr = board->addr_code;
        }
-       size_avail -= board->code_size;
 
        for (i = 0; i < num_blocks; i++) {
                block = &blocks[i];
@@ -966,17 +1000,22 @@ process_blocks(void)
                if (block->type != BLOCK_TYPE_XTRA)
                        continue;
 
-               if (block->size_set == 0)
-                       block->size = ALIGN(block->file_size, 0x10000);
+               if (block->align_set == 0)
+                       block->align = DEFAULT_BLOCK_ALIGN;
 
-               if (block->size > size_avail) {
+               block->size = align(offs + block->file_size,
+                               block->align) - offs;
+
+               DBG(2,"file %s start at %08x, size=%08x, align=%08x",
+                       block->file_name, offs, block->size, block->align);
+
+               offs += block->size;
+               if (offs > board->flash_size) {
                        WARN("file %s is too big, size=%d, avail=%d",
                                block->file_name, block->file_size,
-                               size_avail);
+                               board->flash_size - offs);
                        res = ERR_INVALID_IMAGE;
                }
-
-               size_avail -= block->size;
        }
 
        for (i = 0; i < num_blocks; i++) {
This page took 0.029492 seconds and 4 git commands to generate.