[LEDE-DEV] [PATCH fstools] libblkid-tiny: add support for UBI superblock

Rafał Miłecki zajec5 at gmail.com
Tue Aug 1 12:37:57 PDT 2017


From: Rafał Miłecki <rafal at milecki.pl>

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 at milecki.pl>
---
 CMakeLists.txt                |  1 +
 libblkid-tiny/libblkid-tiny.c |  1 +
 libblkid-tiny/superblocks.h   |  1 +
 libblkid-tiny/ubi.c           | 50 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 53 insertions(+)
 create mode 100644 libblkid-tiny/ubi.c

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 826a852..8752612 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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
diff --git a/libblkid-tiny/libblkid-tiny.c b/libblkid-tiny/libblkid-tiny.c
index d718a1e..352d1f6 100644
--- a/libblkid-tiny/libblkid-tiny.c
+++ b/libblkid-tiny/libblkid-tiny.c
@@ -163,6 +163,7 @@ static const struct blkid_idinfo *idinfos[] =
 	&ext2_idinfo,
 	&jbd_idinfo,
 	&squashfs_idinfo,
+	&ubi_idinfo,
 	&ubifs_idinfo,
 	&jffs2_idinfo,
 	&hfsplus_idinfo,
diff --git a/libblkid-tiny/superblocks.h b/libblkid-tiny/superblocks.h
index d2b242f..cde8a40 100644
--- a/libblkid-tiny/superblocks.h
+++ b/libblkid-tiny/superblocks.h
@@ -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
index 0000000..8b93b5d
--- /dev/null
+++ b/libblkid-tiny/ubi.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2017 Rafał Miłecki <rafal at 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, NULL, 4, "%u", be32_to_cpu(hdr->image_seq));
+	return 0;
+}
+
+const struct blkid_idinfo ubi_idinfo =
+{
+	.name		= "ubi",
+	.usage		= BLKID_USAGE_FILESYSTEM,
+	.probefunc	= probe_ubi,
+	.magics		=
+	{
+		{ .magic = "UBI#", .len = 4 },
+		{ NULL }
+	}
+};
-- 
2.11.0




More information about the Lede-dev mailing list