libblkid-tiny: add support for UBI superblock
authorRafał Miłecki <rafal@milecki.pl>
Mon, 7 Aug 2017 09:20:09 +0000 (11:20 +0200)
committerRafał Miłecki <rafal@milecki.pl>
Mon, 7 Aug 2017 09:20:09 +0000 (11:20 +0200)
Detecting UBI superblock may be useful for tools wanting to simplify or
automate attaching UBI. Please note it's not directly related to the
ubifs support which is just a filesystem working on top of UBI volume.

This patch adds simple code reading UBI version and unique number and
setting it in the blkid_probe.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
CMakeLists.txt
libblkid-tiny/libblkid-tiny.c
libblkid-tiny/superblocks.h
libblkid-tiny/ubi.c [new file with mode: 0644]

index 826a8523ddfe3b387244a5c9e6d9abfb1728620f..8752612a934e01af958a19591864883806dae51a 100644 (file)
@@ -26,6 +26,7 @@ ADD_LIBRARY(blkid-tiny SHARED
                libblkid-tiny/vfat.c
                libblkid-tiny/hfs.c
                libblkid-tiny/swap.c
+               libblkid-tiny/ubi.c
                libblkid-tiny/ubifs.c
                libblkid-tiny/squashfs.c
                libblkid-tiny/btrfs.c
index d718a1efa1aca863d70a1502eb836cfeafd9107e..352d1f62ab6e8f07cb0762482b04cf30549dedb4 100644 (file)
@@ -163,6 +163,7 @@ static const struct blkid_idinfo *idinfos[] =
        &ext2_idinfo,
        &jbd_idinfo,
        &squashfs_idinfo,
+       &ubi_idinfo,
        &ubifs_idinfo,
        &jffs2_idinfo,
        &hfsplus_idinfo,
index d2b242fb2ff10a377d88f0aac4f28e118458b332..cde8a40a711c980e20c03393a0bbf283f6db1e9a 100644 (file)
@@ -62,6 +62,7 @@ extern const struct blkid_idinfo netware_idinfo;
 extern const struct blkid_idinfo sysv_idinfo;
 extern const struct blkid_idinfo xenix_idinfo;
 extern const struct blkid_idinfo btrfs_idinfo;
+extern const struct blkid_idinfo ubi_idinfo;
 extern const struct blkid_idinfo ubifs_idinfo;
 extern const struct blkid_idinfo zfs_idinfo;
 extern const struct blkid_idinfo bfs_idinfo;
diff --git a/libblkid-tiny/ubi.c b/libblkid-tiny/ubi.c
new file mode 100644 (file)
index 0000000..0739c32
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdint.h>
+
+#include "superblocks.h"
+
+struct ubi_ec_hdr {
+       uint32_t        magic;
+       uint8_t         version;
+       uint8_t         padding1[3];
+       uint64_t        ec;
+       uint32_t        vid_hdr_offset;
+       uint32_t        data_offset;
+       uint32_t        image_seq;
+       uint8_t         padding2[32];
+       uint32_t        hdr_crc;
+} __attribute__((packed));
+
+static int probe_ubi(blkid_probe pr, const struct blkid_idmag *mag)
+{
+       struct ubi_ec_hdr *hdr;
+
+       hdr = blkid_probe_get_sb(pr, mag, struct ubi_ec_hdr);
+       if (!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));
+       return 0;
+}
+
+const struct blkid_idinfo ubi_idinfo =
+{
+       .name           = "ubi",
+       .usage          = BLKID_USAGE_RAID,
+       .probefunc      = probe_ubi,
+       .magics         =
+       {
+               { .magic = "UBI#", .len = 4 },
+               { NULL }
+       }
+};