otrx: avoid unneeded fseek() when calculating CRC32
[project/firmware-utils.git] / src / mkmylofw.c
index aa0a2e1bd37da99a8f6ee38d8470e11c13ba876b..fd79f1ca83d3a87157fe313750e25847134c3c0d 100644 (file)
@@ -1,21 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  *  Copyright (C) 2006-2008 Gabor Juhos <juhosg@openwrt.org>
- *
- *  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.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the
- *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- *  Boston, MA  02110-1301, USA.
- *
  */
 
 #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)
@@ -48,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;
@@ -59,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 {
@@ -89,7 +77,10 @@ struct cpx_board {
 #define CPX_BOARD_AR71XX(_did, _flash, _mod, _name, _desc) \
        CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x20000, 0x8000)
 
-#define ALIGN(x,y)     ((x)+((y)-1)) & ~((y)-1)
+#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;
@@ -100,7 +91,7 @@ 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;
 
@@ -141,9 +132,20 @@ struct cpx_board boards[] = {
        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}
 };
 
@@ -215,11 +217,12 @@ 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"
@@ -436,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);
@@ -457,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;
@@ -560,7 +565,7 @@ write_out_file(FILE *outfile, struct fw_block *block, uint32_t *crc)
        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;
 
@@ -600,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;
 }
 
 
@@ -638,7 +656,8 @@ write_out_blocks(FILE *outfile, uint32_t *crc)
        if (fw_num_partitions > 0) {
                desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
                desc.addr = HOST_TO_LE32(board->part_offset);
-               desc.dlen = HOST_TO_LE32(sizeof(struct mylo_partition_table));
+               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)
@@ -813,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];
@@ -976,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;
@@ -990,9 +1007,10 @@ parse_opt_partition(char ch, char *arg)
                goto err_out;
        }
 
-       part = &fw_partitions[fw_num_partitions++];
+       fp = &fw_parts[fw_num_partitions++];
+       part = &fp->mylo;
 
-       argc = parse_arg(arg, buf, argv);
+       parse_arg(arg, buf, argv);
 
        /* processing partition address */
        p = argv[0];
@@ -1048,13 +1066,21 @@ 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;