firmware-utils: replace GPL 2.0 boilerplate/reference with SPDX
[openwrt/staging/ldir.git] / tools / firmware-utils / src / mkplanexfw.c
index 4b32c736956e02ac67888a0dde58ec252c57bfec..c86b6b5b88ff400195f2c70eb5555e9a59d15276 100644 (file)
@@ -1,10 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0-only
 /*
  *  Copyright (C) 2009 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 version 2 as published
- *  by the Free Software Foundation.
- *
  */
 
 #include <stdio.h>
@@ -40,6 +36,7 @@ struct board_info {
        char            *id;
        uint32_t        seed;
        uint8_t         unk[2];
+       uint32_t        datalen;
 };
 
 /*
@@ -58,10 +55,12 @@ static struct board_info boards[] = {
                .id             = "MZK-W04NU",
                .seed           = 2,
                .unk            = {0x04, 0x08},
+               .datalen        = 0x770000,
        }, {
                .id             = "MZK-W300NH",
                .seed           = 4,
                .unk            = {0x00, 0x00},
+               .datalen        = 0x770000,
        }, {
                /* terminating entry */
        }
@@ -79,7 +78,7 @@ static struct board_info boards[] = {
 #define ERRS(fmt, ...) do { \
        int save = errno; \
        fflush(0); \
-       fprintf(stderr, "[%s] *** error: " fmt "\n", \
+       fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
                        progname, ## __VA_ARGS__, strerror(save)); \
 } while (0)
 
@@ -102,7 +101,6 @@ static struct board_info *find_board(char *id)
 void usage(int status)
 {
        FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
-       struct board_info *board;
 
        fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
        fprintf(stream,
@@ -189,9 +187,13 @@ int main(int argc, char *argv[])
                goto err;
        }
 
-       buflen = (st.st_size + 3) & ~3;
-       buflen += sizeof(*hdr);
+       if (st.st_size > board->datalen) {
+               ERR("file '%s' is too big - max size: 0x%08X (exceeds %lu bytes)\n",
+                   ifname, board->datalen, st.st_size - board->datalen);
+               goto err;
+       }
 
+       buflen = board->datalen + 0x10000;
        buf = malloc(buflen);
        if (!buf) {
                ERR("no memory for buffer\n");
@@ -201,7 +203,7 @@ int main(int argc, char *argv[])
        memset(buf, 0xff, buflen);
        hdr = (struct planex_hdr *)buf;
 
-       hdr->datalen = HOST_TO_BE32(buflen - sizeof(*hdr));
+       hdr->datalen = HOST_TO_BE32(board->datalen);
        hdr->unk1[0] = board->unk[0];
        hdr->unk1[1] = board->unk[1];
 
@@ -223,7 +225,7 @@ int main(int argc, char *argv[])
        seed = HOST_TO_BE32(board->seed);
        sha1_starts(&ctx);
        sha1_update(&ctx, (uchar *) &seed, sizeof(seed));
-       sha1_update(&ctx, buf + sizeof(*hdr), buflen - sizeof(*hdr));
+       sha1_update(&ctx, buf + sizeof(*hdr), board->datalen);
        sha1_finish(&ctx, hdr->sha1sum);
 
        outfile = fopen(ofname, "w");
@@ -241,7 +243,6 @@ int main(int argc, char *argv[])
 
        res = EXIT_SUCCESS;
 
- out_flush:
        fflush(outfile);
 
  err_close_out: