mtd: add CRC signature to RedBoot partition map
authorDaniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>
Sat, 1 Sep 2018 01:14:24 +0000 (18:14 -0700)
committerPetr Štetiar <ynezz@true.cz>
Wed, 17 Jul 2019 20:59:29 +0000 (22:59 +0200)
The code for calculating the CRC32 signatures for RedBoot FIS partitions
was already included, but for unknown reasons, it was never invoked. Some
bootloaders enforce checking these for loaded kernels, so they should be
written. This patch does so.

Tested-by: Brian Gonyer <bgonyer@gmail.com>
Signed-off-by: Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>
package/system/mtd/src/fis.c
package/system/mtd/src/fis.h
package/system/mtd/src/mtd.c

index b50411cfebb0892bb7631aa0d0577fda90b7c462..e314c102580df46c2394a14d65cb85f269dbab5e 100644 (file)
@@ -243,14 +243,14 @@ fis_remap(struct fis_part *old, int n_old, struct fis_part *new, int n_new)
                memset(desc, 0, sizeof(struct fis_image_desc));
                memcpy(desc->hdr.name, part->name, sizeof(desc->hdr.name));
                desc->crc.desc = 0;
-               desc->crc.file = 0;
+               desc->crc.file = part->crc;
 
                desc->hdr.flash_base = offset;
                desc->hdr.mem_base = part->loadaddr;
                desc->hdr.entry_point = part->loadaddr;
                desc->hdr.size = (part->size > 0) ? part->size : size;
-               desc->hdr.data_length = desc->hdr.size;
-
+               desc->hdr.data_length = (part->length > 0) ? part->length :
+                                                               desc->hdr.size;
                offset += desc->hdr.size;
                size -= desc->hdr.size;
        }
index bdf1103d8a435e44b4970fa4d041e21f6fd02b08..2be2cb1f04ce3cc62d534f9fa7f104f319e6e5de 100644 (file)
@@ -6,6 +6,8 @@ struct fis_part {
        uint32_t offset;
        uint32_t loadaddr;
        uint32_t size;
+       uint32_t length;
+       uint32_t crc;
 };
 
 int fis_validate(struct fis_part *old, int n_old, struct fis_part *new, int n_new);
index eccb4f6a1a98cde97f21be69ffba2463cc4ebb55..9baed3fd5e9938792320320d45e9c4976d00dc47 100644 (file)
@@ -44,6 +44,7 @@
 #include <sys/reboot.h>
 #include <linux/reboot.h>
 #include <mtd/mtd-user.h>
+#include "crc32.h"
 #include "fis.h"
 #include "mtd.h"
 
@@ -473,12 +474,14 @@ mtd_write(int imagefd, const char *mtd, char *fis_layout, size_t part_offset)
        ssize_t r, w, e;
        ssize_t skip = 0;
        uint32_t offset = 0;
+       int buflen_raw = 0;
        int jffs2_replaced = 0;
        int skip_bad_blocks = 0;
 
 #ifdef FIS_SUPPORT
        static struct fis_part new_parts[MAX_ARGS];
        static struct fis_part old_parts[MAX_ARGS];
+       struct fis_part *cur_part = NULL;
        int n_new = 0, n_old = 0;
 
        if (fis_layout) {
@@ -488,6 +491,8 @@ mtd_write(int imagefd, const char *mtd, char *fis_layout, size_t part_offset)
 
                memset(&old_parts, 0, sizeof(old_parts));
                memset(&new_parts, 0, sizeof(new_parts));
+               if (!part_offset)
+                       cur_part = new_parts;
 
                do {
                        next = strchr(tmp, ':');
@@ -588,6 +593,9 @@ resume:
                        buflen += r;
                }
 
+               if (buflen_raw == 0)
+                       buflen_raw = buflen;
+
                if (buflen == 0)
                        break;
 
@@ -599,6 +607,7 @@ resume:
 
                if (skip > 0) {
                        skip -= buflen;
+                       buflen_raw = 0;
                        buflen = 0;
                        if (skip <= 0)
                                indicate_writing(mtd);
@@ -622,6 +631,7 @@ resume:
                                w += skip;
                                e += skip;
                                skip -= buflen;
+                               buflen_raw = 0;
                                buflen = 0;
                                offset = 0;
                                continue;
@@ -687,6 +697,17 @@ resume:
                }
                w += buflen;
 
+#ifdef FIS_SUPPORT
+               if (cur_part && cur_part->size
+               && cur_part < &new_parts[MAX_ARGS - 1]
+               && cur_part->length + buflen_raw > cur_part->size)
+                       cur_part++;
+               if (cur_part) {
+                       cur_part->length += buflen_raw;
+                       cur_part->crc = crc32(cur_part->crc, buf, buflen_raw);
+               }
+#endif
+               buflen_raw = 0;
                buflen = 0;
                offset = 0;
        }