]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: ubi: add checksum support
authorThomas Weißschuh <thomas@t-8ch.de>
Tue, 13 Sep 2022 12:09:34 +0000 (14:09 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Tue, 13 Sep 2022 12:13:18 +0000 (14:13 +0200)
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
libblkid/src/superblocks/ubi.c

index 0739c32c6e970aa8c4207a44b1e32af4b8831bde..fbb7c5e33bb9806b4b741b4474742bbb07f5700d 100644 (file)
@@ -11,6 +11,7 @@
 #include <stdint.h>
 
 #include "superblocks.h"
+#include "crc32.h"
 
 struct ubi_ec_hdr {
        uint32_t        magic;
@@ -24,6 +25,14 @@ struct ubi_ec_hdr {
        uint32_t        hdr_crc;
 } __attribute__((packed));
 
+static int ubi_verify_csum(blkid_probe pr, const struct ubi_ec_hdr *hdr)
+{
+       return blkid_probe_verify_csum(pr,
+                       ul_crc32(~0LL, (unsigned char *) hdr,
+                               sizeof(*hdr) - sizeof(hdr->hdr_crc)),
+                       be32_to_cpu(hdr->hdr_crc));
+}
+
 static int probe_ubi(blkid_probe pr, const struct blkid_idmag *mag)
 {
        struct ubi_ec_hdr *hdr;
@@ -32,6 +41,9 @@ static int probe_ubi(blkid_probe pr, const struct blkid_idmag *mag)
        if (!hdr)
                return -1;
 
+       if (!ubi_verify_csum(pr, hdr))
+               return -1;
+
        blkid_probe_sprintf_version(pr, "%u", hdr->version);
        blkid_probe_sprintf_uuid(pr, (unsigned char *)&hdr->image_seq, 4, "%u",
                                 be32_to_cpu(hdr->image_seq));