mtd: add fixwrgg command
authorStijn Tintel <stijn@linux-ipv6.be>
Sun, 7 Aug 2016 16:51:12 +0000 (18:51 +0200)
committerStijn Tintel <stijn@linux-ipv6.be>
Wed, 26 Oct 2016 22:50:42 +0000 (01:50 +0300)
Based on fixseama.

Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
Acked-by: John Crispin <john@phrozen.org>
package/system/mtd/src/Makefile
package/system/mtd/src/mtd.c
package/system/mtd/src/mtd.h
package/system/mtd/src/wrgg.c [new file with mode: 0644]
package/system/mtd/src/wrgg.h [new file with mode: 0644]

index 70c61e58ae1f17150a901a3b677accc21276c059..4e39a89315bc9605b4e21ec7e66c99fafae976fd 100644 (file)
@@ -4,7 +4,8 @@ LDFLAGS += -lubox
 
 obj = mtd.o jffs2.o crc32.o md5.o
 obj.seama = seama.o md5.o
 
 obj = mtd.o jffs2.o crc32.o md5.o
 obj.seama = seama.o md5.o
-obj.ar71xx = trx.o $(obj.seama)
+obj.wrgg = wrgg.o md5.o
+obj.ar71xx = trx.o $(obj.seama) $(obj.wrgg)
 obj.brcm = trx.o
 obj.brcm47xx = $(obj.brcm)
 obj.bcm53xx = $(obj.brcm) $(obj.seama)
 obj.brcm = trx.o
 obj.brcm47xx = $(obj.brcm)
 obj.bcm53xx = $(obj.brcm) $(obj.seama)
index 91e169eb413114539c4c646b72a5b5516cee57bb..e66e647c0135625bf57cbad69268063008642de3 100644 (file)
@@ -54,6 +54,7 @@
 
 #define TRX_MAGIC              0x48445230      /* "HDR0" */
 #define SEAMA_MAGIC            0x5ea3a417
 
 #define TRX_MAGIC              0x48445230      /* "HDR0" */
 #define SEAMA_MAGIC            0x5ea3a417
+#define WRGG03_MAGIC           0x20080321
 
 #if !defined(__BYTE_ORDER)
 #error "Unknown byte order"
 
 #if !defined(__BYTE_ORDER)
 #error "Unknown byte order"
 #if __BYTE_ORDER == __BIG_ENDIAN
 #define cpu_to_be32(x) (x)
 #define be32_to_cpu(x) (x)
 #if __BYTE_ORDER == __BIG_ENDIAN
 #define cpu_to_be32(x) (x)
 #define be32_to_cpu(x) (x)
+#define le32_to_cpu(x) bswap_32(x)
 #elif __BYTE_ORDER == __LITTLE_ENDIAN
 #define cpu_to_be32(x) bswap_32(x)
 #define be32_to_cpu(x) bswap_32(x)
 #elif __BYTE_ORDER == __LITTLE_ENDIAN
 #define cpu_to_be32(x) bswap_32(x)
 #define be32_to_cpu(x) bswap_32(x)
+#define le32_to_cpu(x)  (x)
 #else
 #error "Unsupported endianness"
 #endif
 #else
 #error "Unsupported endianness"
 #endif
@@ -73,6 +76,7 @@ enum mtd_image_format {
        MTD_IMAGE_FORMAT_UNKNOWN,
        MTD_IMAGE_FORMAT_TRX,
        MTD_IMAGE_FORMAT_SEAMA,
        MTD_IMAGE_FORMAT_UNKNOWN,
        MTD_IMAGE_FORMAT_TRX,
        MTD_IMAGE_FORMAT_SEAMA,
+       MTD_IMAGE_FORMAT_WRGG03,
 };
 
 static char *buf = NULL;
 };
 
 static char *buf = NULL;
@@ -201,6 +205,8 @@ image_check(int imagefd, const char *mtd)
                imageformat = MTD_IMAGE_FORMAT_TRX;
        else if (be32_to_cpu(magic) == SEAMA_MAGIC)
                imageformat = MTD_IMAGE_FORMAT_SEAMA;
                imageformat = MTD_IMAGE_FORMAT_TRX;
        else if (be32_to_cpu(magic) == SEAMA_MAGIC)
                imageformat = MTD_IMAGE_FORMAT_SEAMA;
+       else if (le32_to_cpu(magic) == WRGG03_MAGIC)
+               imageformat = MTD_IMAGE_FORMAT_WRGG03;
 
        switch (imageformat) {
        case MTD_IMAGE_FORMAT_TRX:
 
        switch (imageformat) {
        case MTD_IMAGE_FORMAT_TRX:
@@ -209,6 +215,8 @@ image_check(int imagefd, const char *mtd)
                break;
        case MTD_IMAGE_FORMAT_SEAMA:
                break;
                break;
        case MTD_IMAGE_FORMAT_SEAMA:
                break;
+       case MTD_IMAGE_FORMAT_WRGG03:
+               break;
        default:
 #ifdef target_brcm
                if (!strcmp(mtd, "firmware"))
        default:
 #ifdef target_brcm
                if (!strcmp(mtd, "firmware"))
@@ -677,6 +685,10 @@ resume:
                        if (mtd_fixseama)
                                mtd_fixseama(mtd, 0, 0);
                        break;
                        if (mtd_fixseama)
                                mtd_fixseama(mtd, 0, 0);
                        break;
+               case MTD_IMAGE_FORMAT_WRGG03:
+                       if (mtd_fixwrgg)
+                               mtd_fixwrgg(mtd, 0, 0);
+                       break;
                default:
                        break;
                }
                default:
                        break;
                }
@@ -722,6 +734,10 @@ static void usage(void)
            fprintf(stderr,
        "        fixseama                fix the checksum in a seama header on first boot\n");
        }
            fprintf(stderr,
        "        fixseama                fix the checksum in a seama header on first boot\n");
        }
+       if (mtd_fixwrgg) {
+           fprintf(stderr,
+       "        fixwrgg                 fix the checksum in a wrgg header on first boot\n");
+       }
        fprintf(stderr,
        "Following options are available:\n"
        "        -q                      quiet mode (once: no [w] on writing,\n"
        fprintf(stderr,
        "Following options are available:\n"
        "        -q                      quiet mode (once: no [w] on writing,\n"
@@ -739,9 +755,9 @@ static void usage(void)
            fprintf(stderr,
        "        -o offset               offset of the image header in the partition(for fixtrx)\n");
        }
            fprintf(stderr,
        "        -o offset               offset of the image header in the partition(for fixtrx)\n");
        }
-       if (mtd_fixtrx || mtd_fixseama) {
+       if (mtd_fixtrx || mtd_fixseama || mtd_fixwrgg) {
                fprintf(stderr,
                fprintf(stderr,
-       "        -c datasize             amount of data to be used for checksum calculation (for fixtrx / fixseama)\n");
+       "        -c datasize             amount of data to be used for checksum calculation (for fixtrx / fixseama / fixwrgg)\n");
        }
        fprintf(stderr,
 #ifdef FIS_SUPPORT
        }
        fprintf(stderr,
 #ifdef FIS_SUPPORT
@@ -782,6 +798,7 @@ int main (int argc, char **argv)
                CMD_JFFS2WRITE,
                CMD_FIXTRX,
                CMD_FIXSEAMA,
                CMD_JFFS2WRITE,
                CMD_FIXTRX,
                CMD_FIXSEAMA,
+               CMD_FIXWRGG,
                CMD_VERIFY,
                CMD_DUMP,
                CMD_RESETBC,
                CMD_VERIFY,
                CMD_DUMP,
                CMD_RESETBC,
@@ -896,6 +913,9 @@ int main (int argc, char **argv)
        } else if (((strcmp(argv[0], "fixseama") == 0) && (argc == 2)) && mtd_fixseama) {
                cmd = CMD_FIXSEAMA;
                device = argv[1];
        } else if (((strcmp(argv[0], "fixseama") == 0) && (argc == 2)) && mtd_fixseama) {
                cmd = CMD_FIXSEAMA;
                device = argv[1];
+       } else if (((strcmp(argv[0], "fixwrgg") == 0) && (argc == 2)) && mtd_fixwrgg) {
+               cmd = CMD_FIXWRGG;
+               device = argv[1];
        } else if ((strcmp(argv[0], "verify") == 0) && (argc == 3)) {
                cmd = CMD_VERIFY;
                imagefile = argv[1];
        } else if ((strcmp(argv[0], "verify") == 0) && (argc == 3)) {
                cmd = CMD_VERIFY;
                imagefile = argv[1];
@@ -992,6 +1012,10 @@ int main (int argc, char **argv)
                        if (mtd_fixseama)
                                mtd_fixseama(device, 0, data_size);
                        break;
                        if (mtd_fixseama)
                                mtd_fixseama(device, 0, data_size);
                        break;
+               case CMD_FIXWRGG:
+                       if (mtd_fixwrgg)
+                               mtd_fixwrgg(device, 0, data_size);
+                       break;
        }
 
        sync();
        }
 
        sync();
index 9b2e32ffd4f4adc53cb38d87ed744a252e64f12e..50a42da14bbd5b3f7acc6f94adcbc299f3bfb359 100644 (file)
@@ -27,5 +27,6 @@ extern int trx_fixup(int fd, const char *name)  __attribute__ ((weak));
 extern int trx_check(int imagefd, const char *mtd, char *buf, int *len) __attribute__ ((weak));
 extern int mtd_fixtrx(const char *mtd, size_t offset, size_t data_size) __attribute__ ((weak));
 extern int mtd_fixseama(const char *mtd, size_t offset, size_t data_size) __attribute__ ((weak));
 extern int trx_check(int imagefd, const char *mtd, char *buf, int *len) __attribute__ ((weak));
 extern int mtd_fixtrx(const char *mtd, size_t offset, size_t data_size) __attribute__ ((weak));
 extern int mtd_fixseama(const char *mtd, size_t offset, size_t data_size) __attribute__ ((weak));
+extern int mtd_fixwrgg(const char *mtd, size_t offset, size_t data_size) __attribute__ ((weak));
 extern int mtd_resetbc(const char *mtd) __attribute__ ((weak));
 #endif /* __mtd_h */
 extern int mtd_resetbc(const char *mtd) __attribute__ ((weak));
 #endif /* __mtd_h */
diff --git a/package/system/mtd/src/wrgg.c b/package/system/mtd/src/wrgg.c
new file mode 100644 (file)
index 0000000..e0b5ca1
--- /dev/null
@@ -0,0 +1,190 @@
+/*
+ * wrgg.c
+ *
+ * Copyright (C) 2005 Mike Baker
+ * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
+ * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
+ * Copyright (C) 2016 Stijn Tintel <stijn@linux-ipv6.be>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ */
+
+#include <endian.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <errno.h>
+#include <arpa/inet.h>
+
+#include <sys/ioctl.h>
+#include <mtd/mtd-user.h>
+#include "mtd.h"
+#include "wrgg.h"
+#include "md5.h"
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define STORE32_LE(X)           ((((X) & 0x000000FF) << 24) | (((X) & 0x0000FF00) << 8) | (((X) & 0x00FF0000) >> 8) | (((X) & 0xFF000000) >> 24))
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define STORE32_LE(X)           (X)
+#else
+#error unknown endianness!
+#endif
+
+ssize_t pread(int fd, void *buf, size_t count, off_t offset);
+ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
+
+int
+wrgg_fix_md5(struct wrgg03_header *shdr, int fd, size_t data_offset, size_t data_size)
+{
+       char *buf;
+       ssize_t res;
+       MD5_CTX ctx;
+       unsigned char digest[16];
+       int i;
+       int err = 0;
+
+       buf = malloc(data_size);
+       if (!buf) {
+               err = -ENOMEM;
+               goto err_out;
+       }
+
+       res = pread(fd, buf, data_size, data_offset);
+       if (res != data_size) {
+               perror("pread");
+               err = -EIO;
+               goto err_free;
+       }
+
+       MD5_Init(&ctx);
+       MD5_Update(&ctx, (char *)&shdr->offset, sizeof(shdr->offset));
+       MD5_Update(&ctx, (char *)&shdr->dev_name, sizeof(shdr->dev_name));
+       MD5_Update(&ctx, buf, data_size);
+       MD5_Final(digest, &ctx);
+
+       if (!memcmp(digest, shdr->digest, sizeof(digest))) {
+               if (quiet < 2)
+                       fprintf(stderr, "the header is fixed already\n");
+               return -1;
+       }
+
+       if (quiet < 2) {
+               fprintf(stderr, "new size:%u, new MD5: ", data_size);
+               for (i = 0; i < sizeof(digest); i++)
+                       fprintf(stderr, "%02x", digest[i]);
+
+               fprintf(stderr, "\n");
+       }
+
+       /* update the size in the image */
+       shdr->size = htonl(data_size);
+
+       /* update the checksum in the image */
+       memcpy(shdr->digest, digest, sizeof(digest));
+
+err_free:
+       free(buf);
+err_out:
+       return err;
+}
+
+int
+mtd_fixwrgg(const char *mtd, size_t offset, size_t data_size)
+{
+       int fd;
+       char *first_block;
+       ssize_t res;
+       size_t block_offset;
+       size_t data_offset;
+       struct wrgg03_header *shdr;
+
+       if (quiet < 2)
+               fprintf(stderr, "Trying to fix WRGG header in %s at 0x%x...\n",
+                       mtd, offset);
+
+       block_offset = offset & ~(erasesize - 1);
+       offset -= block_offset;
+
+       fd = mtd_check_open(mtd);
+       if(fd < 0) {
+               fprintf(stderr, "Could not open mtd device: %s\n", mtd);
+               exit(1);
+       }
+
+       if (block_offset + erasesize > mtdsize) {
+               fprintf(stderr, "Offset too large, device size 0x%x\n",
+                       mtdsize);
+               exit(1);
+       }
+
+       first_block = malloc(erasesize);
+       if (!first_block) {
+               perror("malloc");
+               exit(1);
+       }
+
+       res = pread(fd, first_block, erasesize, block_offset);
+       if (res != erasesize) {
+               perror("pread");
+               exit(1);
+       }
+
+       shdr = (struct wrgg03_header *)(first_block + offset);
+       if (shdr->magic1 != htonl(STORE32_LE(WRGG03_MAGIC))) {
+               fprintf(stderr, "magic1 %x\n", shdr->magic1);
+               fprintf(stderr, "htonl(WRGG03_MAGIC) %x\n", WRGG03_MAGIC);
+               fprintf(stderr, "No WRGG header found\n");
+               exit(1);
+       } else if (!ntohl(shdr->size)) {
+               fprintf(stderr, "WRGG entity with empty image\n");
+               exit(1);
+       }
+
+       data_offset = offset + sizeof(struct wrgg03_header);
+       if (!data_size)
+               data_size = mtdsize - data_offset;
+       if (data_size > ntohl(shdr->size))
+               data_size = ntohl(shdr->size);
+       if (wrgg_fix_md5(shdr, fd, data_offset, data_size))
+               goto out;
+
+       if (mtd_erase_block(fd, block_offset)) {
+               fprintf(stderr, "Can't erease block at 0x%x (%s)\n",
+                       block_offset, strerror(errno));
+               exit(1);
+       }
+
+       if (quiet < 2)
+               fprintf(stderr, "Rewriting block at 0x%x\n", block_offset);
+
+       if (pwrite(fd, first_block, erasesize, block_offset) != erasesize) {
+               fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
+               exit(1);
+       }
+
+       if (quiet < 2)
+               fprintf(stderr, "Done.\n");
+
+out:
+       close (fd);
+       sync();
+
+       return 0;
+}
diff --git a/package/system/mtd/src/wrgg.h b/package/system/mtd/src/wrgg.h
new file mode 100644 (file)
index 0000000..0c4b367
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef __wrgg_h
+#define __wrgg_h
+
+#define WRGG03_MAGIC   0x20080321
+
+struct wrgg03_header {
+       char            signature[32];
+       uint32_t        magic1;
+       uint32_t        magic2;
+       char            version[16];
+       char            model[16];
+       uint32_t        flag[2];
+       uint32_t        reserve[2];
+       char            buildno[16];
+       uint32_t        size;
+       uint32_t        offset;
+       char            dev_name[32];
+       char            digest[16];
+} __attribute__ ((packed));
+#endif /* __wrgg_h */