otrx: avoid unneeded fseek() when calculating CRC32
[project/firmware-utils.git] / src / mkmylofw.c
index 1c540f5cd36a8322cdc19bd5b0d445542d0ebd29..fd79f1ca83d3a87157fe313750e25847134c3c0d 100644 (file)
@@ -1,11 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *  Copyright (C) 2006,2007 Gabor Juhos
- *
- *  This program is free software; you can redistribute  it and/or modify it
- *  under  the terms of  the GNU General  Public License as published by the
- *  Free Software Foundation;  either version 2 of the  License, or (at your
- *  option) any later version.
- *
+ *  Copyright (C) 2006-2008 Gabor Juhos <juhosg@openwrt.org>
  */
 
 #include <stdio.h>
 #include <errno.h>
 #include <sys/stat.h>
 #include <endian.h>     /* for __BYTE_ORDER */
-
-#if defined(__CYGWIN__)
-#  include <byteswap.h>
-#endif
+#include <byteswap.h>
 
 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
 #  define HOST_TO_LE16(x)      (x)
@@ -38,6 +30,7 @@
 #define MAX_ARG_COUNT   32
 #define MAX_ARG_LEN     1024
 #define FILE_BUF_LEN    (16*1024)
+#define PART_NAME_LEN  32
 
 struct fw_block {
        uint32_t        addr;
@@ -49,6 +42,11 @@ struct fw_block {
        uint32_t        crc;    /* crc value of the file */
 };
 
+struct fw_part {
+       struct mylo_partition   mylo;
+       char                    name[PART_NAME_LEN];
+};
+
 #define BLOCK_FLAG_HAVEHDR    0x0001
 
 struct cpx_board {
@@ -60,17 +58,29 @@ struct cpx_board {
        uint16_t        svid;   /* sub vendor id */
        uint16_t        sdid;   /* sub device id */
        uint32_t        flash_size;     /* size of flash */
+       uint32_t        part_offset;    /* offset of the partition_table */
+       uint32_t        part_size;      /* size of the partition_table */
 };
 
-#define BOARD(_vid, _did, _svid, _sdid, _flash, _mod, _name, _desc) {          \
-       .model = _mod, .name = _name, .desc = _desc,                            \
+#define BOARD(_vid, _did, _svid, _sdid, _flash, _mod, _name, _desc, _po, _ps) {                \
+       .model = _mod, .name = _name, .desc = _desc,                    \
        .vid = _vid, .did = _did, .svid = _svid, .sdid = _sdid,         \
-       .flash_size = (_flash << 20)    }
+       .flash_size = (_flash << 20),                                   \
+       .part_offset = _po, .part_size = _ps }
+
+#define CPX_BOARD(_did, _flash, _mod, _name, _desc, _po, _ps) \
+       BOARD(VENID_COMPEX, _did, VENID_COMPEX, _did, _flash, _mod, _name, _desc, _po, _ps)
 
-#define CPX_BOARD(_did, _flash, _mod, _name, _desc) \
-       BOARD(VENID_COMPEX, _did, VENID_COMPEX, _did, _flash, _mod, _name, _desc)
+#define CPX_BOARD_ADM(_did, _flash, _mod, _name, _desc) \
+       CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
 
-#define ALIGN(x,y)     ((x)+((y)-1)) & ~((y)-1)
+#define CPX_BOARD_AR71XX(_did, _flash, _mod, _name, _desc) \
+       CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x20000, 0x8000)
+
+#define CPX_BOARD_AR23XX(_did, _flash, _mod, _name, _desc) \
+       CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
+
+#define ALIGN(x,y)     (((x)+((y)-1)) & ~((y)-1))
 
 char   *progname;
 char   *ofname = NULL;
@@ -81,46 +91,61 @@ int fw_num_blocks = 0;
 int    verblevel = 0;
 
 struct mylo_fw_header fw_header;
-struct mylo_partition fw_partitions[MYLO_MAX_PARTITIONS];
+struct fw_part fw_parts[MYLO_MAX_PARTITIONS];
 struct fw_block fw_blocks[MAX_FW_BLOCKS];
+struct cpx_board *board;
 
 struct cpx_board boards[] = {
-       CPX_BOARD(DEVID_COMPEX_NP18A, 4,
+       CPX_BOARD_ADM(DEVID_COMPEX_NP18A, 4,
                "NP18A", "Compex NetPassage 18A",
                "Dualband Wireless A+G Internet Gateway"),
-       CPX_BOARD(DEVID_COMPEX_NP26G8M, 2,
+       CPX_BOARD_ADM(DEVID_COMPEX_NP26G8M, 2,
                "NP26G8M", "Compex NetPassage 26G (8M)",
                "Wireless-G Broadband Multimedia Gateway"),
-       CPX_BOARD(DEVID_COMPEX_NP26G16M, 4,
+       CPX_BOARD_ADM(DEVID_COMPEX_NP26G16M, 4,
                "NP26G16M", "Compex NetPassage 26G (16M)",
                "Wireless-G Broadband Multimedia Gateway"),
-       CPX_BOARD(DEVID_COMPEX_NP27G, 4,
+       CPX_BOARD_ADM(DEVID_COMPEX_NP27G, 4,
                "NP27G", "Compex NetPassage 27G",
                "Wireless-G 54Mbps eXtended Range Router"),
-       CPX_BOARD(DEVID_COMPEX_NP28G, 4,
+       CPX_BOARD_ADM(DEVID_COMPEX_NP28G, 4,
                "NP28G", "Compex NetPassage 28G",
                "Wireless 108Mbps Super-G XR Multimedia Router with 4 USB Ports"),
-       CPX_BOARD(DEVID_COMPEX_NP28GHS, 4,
+       CPX_BOARD_ADM(DEVID_COMPEX_NP28GHS, 4,
                "NP28GHS", "Compex NetPassage 28G (HotSpot)",
                "HotSpot Solution"),
-       CPX_BOARD(DEVID_COMPEX_WP18, 4,
+       CPX_BOARD_ADM(DEVID_COMPEX_WP18, 4,
                "WP18", "Compex NetPassage WP18",
                "Wireless-G 54Mbps A+G Dualband Access Point"),
-       CPX_BOARD(DEVID_COMPEX_WP54G, 4,
+       CPX_BOARD_ADM(DEVID_COMPEX_WP54G, 4,
                "WP54G", "Compex WP54G",
                "Wireless-G 54Mbps XR Access Point"),
-       CPX_BOARD(DEVID_COMPEX_WP54Gv1C, 2,
+       CPX_BOARD_ADM(DEVID_COMPEX_WP54Gv1C, 2,
                "WP54Gv1C", "Compex WP54G rev.1C",
                "Wireless-G 54Mbps XR Access Point"),
-       CPX_BOARD(DEVID_COMPEX_WP54AG, 4,
+       CPX_BOARD_ADM(DEVID_COMPEX_WP54AG, 4,
                "WP54AG", "Compex WP54AG",
                "Wireless-AG 54Mbps XR Access Point"),
-       CPX_BOARD(DEVID_COMPEX_WPP54G, 4,
+       CPX_BOARD_ADM(DEVID_COMPEX_WPP54G, 4,
                "WPP54G", "Compex WPP54G",
                "Outdoor Access Point"),
-       CPX_BOARD(DEVID_COMPEX_WPP54AG, 4,
+       CPX_BOARD_ADM(DEVID_COMPEX_WPP54AG, 4,
                "WPP54AG", "Compex WPP54AG",
                "Outdoor Access Point"),
+
+       CPX_BOARD_AR71XX(DEVID_COMPEX_WP543, 2,
+               "WP543", "Compex WP543",
+               "BareBoard"),
+       CPX_BOARD_AR71XX(DEVID_COMPEX_WPE72, 8,
+               "WPE72", "Compex WPE72",
+               "BareBoard"),
+
+       CPX_BOARD_AR23XX(DEVID_COMPEX_NP25G, 4,
+               "NP25G", "Compex NetPassage 25G",
+               "Wireless 54Mbps XR Router"),
+       CPX_BOARD_AR23XX(DEVID_COMPEX_WPE53G, 4,
+               "WPE53G", "Compex NetPassage 25G",
+               "Wireless 54Mbps XR Access Point"),
        {.model = NULL}
 };
 
@@ -192,15 +217,17 @@ usage(int status)
 "                  define block at <addr> with length of <len>.\n"
 "                  valid <flag> values:\n"
 "                      h : add crc header before the file data.\n"
-"  -p <addr>:<len>[:<flags>[:<param>[:<file>]]]\n"
+"  -p <addr>:<len>[:<flags>[:<param>[:<name>[:<file>]]]]\n"
 "                  add partition at <addr>, with size of <len> to the\n"
-"                  partition table, set partition flags to <flags> and\n"
-"                  partition parameter to <param>. If the <file> is specified\n"
-"                  content of the file is also added to the firmware image.\n"
+"                  partition table, set partition name to <name>, partition \n"
+"                  flags to <flags> and partition parameter to <param>.\n"
+"                  If the <file> is specified content of the file will be \n"
+"                  added to the firmware image.\n"
 "                  valid <flag> values:\n"
 "                      a:  this is the active partition. The bootloader loads\n"
 "                          the firmware from this partition.\n"
 "                      h:  the partition data have a header.\n"
+"                      l:  the partition data uses LZMA compression.\n"
 "                      p:  the bootloader loads data from this partition to\n"
 "                          the RAM before decompress it.\n"
 "  -h              show this screen\n"
@@ -265,7 +292,7 @@ uint32_t
 get_crc(uint8_t *p, uint32_t len)
 {
        uint32_t crc;
-       
+
        crc = 0;
        update_crc(p ,len , &crc);
        return crc;
@@ -277,7 +304,7 @@ str2u32(char *arg, uint32_t *val)
 {
        char *err = NULL;
        uint32_t t;
-       
+
        errno=0;
        t = strtoul(arg, &err, 0);
        if (errno || (err==arg) || ((err != NULL) && *err)) {
@@ -300,7 +327,7 @@ str2u16(char *arg, uint16_t *val)
        if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
                return -1;
        }
-       
+
        *val = t & 0xFFFF;
        return 0;
 }
@@ -313,7 +340,7 @@ find_board(char *model){
 
        board = NULL;
        for (tmp = boards; tmp->model != NULL; tmp++){
-               if (strcmp(model, tmp->model) == 0) {
+               if (strcasecmp(model, tmp->model) == 0) {
                        board = tmp;
                        break;
                }
@@ -331,19 +358,19 @@ get_file_crc(struct fw_block *ff)
        uint32_t readlen = sizeof(buf);
        int res = -1;
        size_t len;
-       
+
        if ((ff->flags & BLOCK_FLAG_HAVEHDR) == 0) {
                res = 0;
                goto out;
        }
-       
+
        errno = 0;
        f = fopen(ff->name,"r");
        if (errno) {
                errmsg(1,"unable to open file %s", ff->name);
                goto out;
        }
-       
+
        ff->crc = 0;
        len = ff->size;
        while (len > 0) {
@@ -362,7 +389,7 @@ get_file_crc(struct fw_block *ff)
        }
 
        res = 0;
-       
+
 out_close:
        fclose(f);
 out:
@@ -412,7 +439,7 @@ process_partitions(void)
        int i;
 
        for (i = 0; i < fw_num_partitions; i++) {
-               part = &fw_partitions[i];
+               part = &fw_parts[i].mylo;
 
                if (part->addr > flash_size) {
                        errmsg(0, "invalid partition at 0x%08X", part->addr);
@@ -433,18 +460,20 @@ process_partitions(void)
  * routines to write data to the output file
  */
 int
-write_out_data(FILE *outfile, uint8_t *data, size_t len, uint32_t *crc)
+write_out_data(FILE *outfile, void *data, size_t len, uint32_t *crc)
 {
+       uint8_t *ptr = data;
+
        errno = 0;
 
-       fwrite(data, len, 1, outfile);
+       fwrite(ptr, len, 1, outfile);
        if (errno) {
                errmsg(1,"unable to write output file");
                return -1;
        }
 
        if (crc) {
-               update_crc(data, len, crc);
+               update_crc(ptr, len, crc);
        }
 
        return 0;
@@ -463,18 +492,17 @@ int
 write_out_padding(FILE *outfile, size_t len, uint8_t padc, uint32_t *crc)
 {
        uint8_t buff[512];
-       size_t  buflen;
-       
+       size_t  buflen = sizeof(buff);
+
        memset(buff, padc, buflen);
-       
-       buflen = sizeof(buff);
+
        while (len > 0) {
                if (len < buflen)
                        buflen = len;
 
                if (write_out_data(outfile, buff, buflen, crc))
                        return -1;
-                       
+
                len -= buflen;
        }
 
@@ -491,24 +519,24 @@ write_out_file(FILE *outfile, struct fw_block *block, uint32_t *crc)
        size_t len;
 
        errno = 0;
-       
+
        if (block->name == NULL) {
                return 0;
        }
-       
+
        if ((block->flags & BLOCK_FLAG_HAVEHDR) != 0) {
                struct mylo_partition_header ph;
-               
+
                if (get_file_crc(block) != 0)
                        return -1;
-               
+
                ph.crc = HOST_TO_LE32(block->crc);
                ph.len = HOST_TO_LE32(block->size);
-               
+
                if (write_out_data(outfile, (uint8_t *)&ph, sizeof(ph), crc) != 0)
                        return -1;
        }
-       
+
        f = fopen(block->name,"r");
        if (errno) {
                errmsg(1,"unable to open file: %s", block->name);
@@ -530,17 +558,17 @@ write_out_file(FILE *outfile, struct fw_block *block, uint32_t *crc)
 
                if (write_out_data(outfile, buff, buflen, crc) != 0)
                        return -1;
-                       
+
                len -= buflen;
        }
 
        fclose(f);
 
        /* align next block on a 4 byte boundary */
-       len = ALIGN(len,4) - block->size;
+       len = block->size % 4;
        if (write_out_padding(outfile, len, 0xFF, crc))
                return -1;
-       
+
        dbgmsg(1,"file %s written out", block->name);
        return 0;
 }
@@ -577,26 +605,39 @@ int
 write_out_partitions(FILE *outfile, uint32_t *crc)
 {
        struct mylo_partition_table p;
-       struct mylo_partition *p1, *p2;
+       char part_names[MYLO_MAX_PARTITIONS][PART_NAME_LEN];
+       int ret;
        int i;
 
        if (fw_num_partitions == 0)
                return 0;
 
        memset(&p, 0, sizeof(p));
+       memset(part_names, 0, sizeof(part_names));
 
        p.magic = HOST_TO_LE32(MYLO_MAGIC_PARTITIONS);
        for (i = 0; i < fw_num_partitions; i++) {
-               p1 = &p.partitions[i];
-               p2 = &fw_partitions[i];
-               p1->flags = HOST_TO_LE16(p2->flags);
-               p1->type = HOST_TO_LE16(PARTITION_TYPE_USED);
-               p1->addr = HOST_TO_LE32(p2->addr);
-               p1->size = HOST_TO_LE32(p2->size);
-               p1->param = HOST_TO_LE32(p2->param);
+               struct mylo_partition *mp;
+               struct fw_part *fp;
+
+               mp = &p.partitions[i];
+               fp = &fw_parts[i];
+               mp->flags = HOST_TO_LE16(fp->mylo.flags);
+               mp->type = HOST_TO_LE16(PARTITION_TYPE_USED);
+               mp->addr = HOST_TO_LE32(fp->mylo.addr);
+               mp->size = HOST_TO_LE32(fp->mylo.size);
+               mp->param = HOST_TO_LE32(fp->mylo.param);
+
+               memcpy(part_names[i], fp->name, PART_NAME_LEN);
        }
 
-       return write_out_data(outfile, (uint8_t *)&p, sizeof(p), crc);
+       ret = write_out_data(outfile, (uint8_t *)&p, sizeof(p), crc);
+       if (ret)
+               return ret;
+
+       ret = write_out_data(outfile, (uint8_t *)part_names, sizeof(part_names),
+                               crc);
+       return ret;
 }
 
 
@@ -613,11 +654,11 @@ write_out_blocks(FILE *outfile, uint32_t *crc)
         * for the partition table
         */
        if (fw_num_partitions > 0) {
-
                desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
-               desc.addr = HOST_TO_LE32(0x10000);
-               desc.dlen = HOST_TO_LE32(sizeof(struct mylo_partition_table));
-               desc.blen = HOST_TO_LE32(0x10000);
+               desc.addr = HOST_TO_LE32(board->part_offset);
+               desc.dlen = HOST_TO_LE32(sizeof(struct mylo_partition_table) +
+                                       (MYLO_MAX_PARTITIONS * PART_NAME_LEN));
+               desc.blen = HOST_TO_LE32(board->part_size);
 
                if (write_out_desc(outfile, &desc, crc) != 0)
                        return -1;
@@ -628,7 +669,7 @@ write_out_blocks(FILE *outfile, uint32_t *crc)
         */
        for (i = 0; i < fw_num_blocks; i++) {
                b = &fw_blocks[i];
-               
+
                /* detect block size */
                dlen = b->size;
                if ((b->flags & BLOCK_FLAG_HAVEHDR) != 0) {
@@ -728,7 +769,7 @@ required_arg(char c, char *arg)
                errmsg(0,"option %c requires an argument\n", c);
                return -1;
        }
-       
+
        return 0;
 }
 
@@ -755,7 +796,7 @@ parse_opt_flags(char ch, char *arg)
                errmsg(0,"invalid firmware flags: %s", arg);
                goto err_out;
        }
-       
+
        dbgmsg(1, "firmware flags set to %X bytes", fw_header.flags);
 
        return 0;
@@ -776,7 +817,7 @@ parse_opt_size(char ch, char *arg)
                errmsg(0,"invalid flash size: %s", arg);
                goto err_out;
        }
-       
+
        dbgmsg(1, "flash size set to %d bytes", flash_size);
 
        return 0;
@@ -791,14 +832,13 @@ parse_opt_id(char ch, char *arg)
 {
        char buf[MAX_ARG_LEN];
        char *argv[MAX_ARG_COUNT];
-       int argc;
        char *p;
 
        if (required_arg(ch, arg)) {
                goto err_out;
        }
 
-       argc = parse_arg(arg, buf, argv);
+       parse_arg(arg, buf, argv);
 
        /* processing vendor ID*/
        p = argv[0];
@@ -906,12 +946,12 @@ parse_opt_block(char ch, char *arg)
                errmsg(0,"invalid block length: %s", p);
                goto err_out;
        }
-       
+
        if (argc < 3) {
                dbgmsg(1,"empty block %s", arg);
                goto success;
        }
-       
+
        /* processing flags */
        p = argv[2];
        if (is_empty_arg(p) == 0) {
@@ -933,17 +973,17 @@ parse_opt_block(char ch, char *arg)
                errmsg(0,"file name missing in %s", arg);
                goto err_out;
        }
-       
+
        b->name = strdup(p);
        if (b->name == NULL) {
                errmsg(0,"not enough memory");
                goto err_out;
        }
-       
+
 success:
 
        return 0;
-       
+
 err_out:
        return -1;
 }
@@ -954,10 +994,9 @@ parse_opt_partition(char ch, char *arg)
 {
        char buf[MAX_ARG_LEN];
        char *argv[MAX_ARG_COUNT];
-       int argc;
        char *p;
-
        struct mylo_partition *part;
+       struct fw_part *fp;
 
        if (required_arg(ch, arg)) {
                goto err_out;
@@ -967,10 +1006,11 @@ parse_opt_partition(char ch, char *arg)
                errmsg(0, "too many partitions specified");
                goto err_out;
        }
-       
-       part = &fw_partitions[fw_num_partitions++];
 
-       argc = parse_arg(arg, buf, argv);
+       fp = &fw_parts[fw_num_partitions++];
+       part = &fp->mylo;
+
+       parse_arg(arg, buf, argv);
 
        /* processing partition address */
        p = argv[0];
@@ -1003,6 +1043,9 @@ parse_opt_partition(char ch, char *arg)
                        case 'p':
                                part->flags |= PARTITION_FLAG_PRELOAD;
                                break;
+                       case 'l':
+                               part->flags |= PARTITION_FLAG_LZMA;
+                               break;
                        case 'h':
                                part->flags |= PARTITION_FLAG_HAVEHDR;
                                break;
@@ -1023,16 +1066,24 @@ parse_opt_partition(char ch, char *arg)
                goto err_out;
        }
 
+       p = argv[4];
+       if (is_empty_arg(p)) {
+               /* set default partition parameter */
+               fp->name[0] = '\0';
+       } else {
+               strncpy(fp->name, p, PART_NAME_LEN);
+       }
+
 #if 1
        if (part->size == 0) {
                part->size = flash_size - part->addr;
        }
 
        /* processing file parameter */
-       p = argv[4];
+       p = argv[5];
        if (is_empty_arg(p) == 0) {
                struct fw_block *b;
-               
+
                if (fw_num_blocks == MAX_FW_BLOCKS) {
                        errmsg(0,"too many blocks specified", p);
                        goto err_out;
@@ -1057,27 +1108,25 @@ err_out:
 int
 parse_opt_board(char ch, char *arg)
 {
-       struct cpx_board *board;
-       
        if (required_arg(ch, arg)) {
                goto err_out;
        }
-       
+
        board = find_board(arg);
        if (board == NULL){
                errmsg(0,"invalid/unknown board specified: %s", arg);
                goto err_out;
        }
-       
+
        fw_header.vid = board->vid;
        fw_header.did = board->did;
        fw_header.svid = board->svid;
        fw_header.sdid = board->sdid;
-       
+
        flash_size = board->flash_size;
 
        return 0;
-       
+
 err_out:
        return -1;
 }
@@ -1111,11 +1160,11 @@ main(int argc, char *argv[])
 
        FILE  *outfile;
        uint32_t crc;
-       
+
        progname=basename(argv[0]);
-       
+
        memset(&fw_header, 0, sizeof(fw_header));
-       
+
        /* init header defaults */
        fw_header.vid = VENID_COMPEX;
        fw_header.did = DEVID_COMPEX_WP54G;
@@ -1165,24 +1214,29 @@ main(int argc, char *argv[])
                        goto out;
                }
        }
-       
+
        if (optind == argc) {
                errmsg(0, "no output file specified");
                goto out;
        }
 
        ofname = argv[optind++];
-       
+
        if (optind < argc) {
                errmsg(0, "invalid option: %s", argv[optind]);
                goto out;
        }
-       
+
+       if (!board) {
+               errmsg(0, "no board specified");
+               goto out;
+       }
+
        if (flash_size == 0) {
                errmsg(0, "no flash size specified");
                goto out;
        }
-       
+
        if (process_files() != 0) {
                goto out;
        }
@@ -1202,7 +1256,7 @@ main(int argc, char *argv[])
 
        if (write_out_header(outfile, &crc) != 0)
                goto out_flush;
-               
+
        if (write_out_blocks(outfile, &crc) != 0)
                goto out_flush;