From 70b83292d623b48d83cf49a59ebddf21e16ab768 Mon Sep 17 00:00:00 2001 From: Gao Xiang Date: Mon, 22 Nov 2021 22:46:43 +0800 Subject: [PATCH] libblkid: add erofs filesystem support Enhanced Read-Only File System (EROFS) has been included in Linux kernel, many Linux distributions, buildroot and Android AOSP for a while. Plus, nowadays, it's known that EROFS has been commercially used by several Android vendors for their system partitions. This patch adds support for detecting EROFS filesystem to libblkid. Since util-linux/util-linux@cd129b7 introduced blkid_probe_set_block_size function to report filesystem block size, TWRP is using blkid 2.25.0, which means blkid_probe_set_block_size wasn't supported and required, so remove the blkszbits judgement and blkid_probe_set_block_size for fixing building. Test: Add erofs mount point to twrp.flags, build and boot OP4A7A:/ # logcat | grep erofs I (6)[571:recovery]erofs: (device dm-0): mounted with opts: , root inode @ nid 58. I (6)[571:recovery]erofs: (device dm-9): mounted with opts: , root inode @ nid 43. I (6)[571:recovery]erofs: (device dm-1): mounted with opts: , root inode @ nid 52. I (6)[571:recovery]erofs: (device dm-10): mounted with opts: , root inode @ nid 45. I (6)[571:recovery]erofs: (device dm-8): mounted with opts: , root inode @ nid 42. I (0)[571:recovery]erofs: (device dm-12): mounted with opts: , root inode @ nid 38. I (3)[571:recovery]erofs: (device dm-0): mounted with opts: , root inode @ nid 58. I (3)[571:recovery]erofs: (device dm-1): mounted with opts: , root inode @ nid 52. I (3)[571:recovery]erofs: (device dm-0): mounted with opts: , root inode @ nid 58. I (3)[571:recovery]erofs: (device dm-0): mounted with opts: , root inode @ nid 58. I (3)[571:recovery]erofs: (device dm-9): mounted with opts: , root inode @ nid 43. I (2)[571:recovery]erofs: (device dm-1): mounted with opts: , root inode @ nid 52. I (2)[571:recovery]erofs: (device dm-10): mounted with opts: , root inode @ nid 45. I (2)[571:recovery]erofs: (device dm-8): mounted with opts: , root inode @ nid 42. I (2)[571:recovery]erofs: (device dm-12): mounted with opts: , root inode @ nid 38. I (6)[571:recovery]erofs: (device dm-0): mounted with opts: , root inode @ nid 58. I (6)[571:recovery]erofs: (device dm-0): mounted with opts: , root inode @ nid 58. OP4A7A:/ # cat /tmp/recovery.log | grep erofs Current_File_System: erofs Fstab_File_System: erofs Current_File_System: erofs Fstab_File_System: erofs Fstab_File_System: erofs Current_File_System: erofs Fstab_File_System: erofs Current_File_System: erofs Fstab_File_System: erofs Current_File_System: erofs Fstab_File_System: erofs Current_File_System: erofs Fstab_File_System: erofs Now TWRP can recognize the EROFS filesystem perfectly after setting fs in twrp.flags Signed-off-by: Gao Xiang [@pomelohan: Adapt for TWRP's libblkid version and add erofs.c to build] Co-authored-by: GarfieldHan <2652609017@qq.com> Signed-off-by: GarfieldHan <2652609017@qq.com> Change-Id: I00263757b5b6ed881165e65795ef849d86763165 --- libblkid/Android.mk | 1 + libblkid/src/superblocks/erofs.c | 71 ++++++++++++++++++++++++++ libblkid/src/superblocks/superblocks.c | 3 +- libblkid/src/superblocks/superblocks.h | 1 + 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 libblkid/src/superblocks/erofs.c diff --git a/libblkid/Android.mk b/libblkid/Android.mk index 6bdbe355..bc797a21 100755 --- a/libblkid/Android.mk +++ b/libblkid/Android.mk @@ -146,6 +146,7 @@ LOCAL_SRC_FILES = src/cache.c \ src/superblocks/drbdproxy_datalog.c \ src/superblocks/exfat.c \ src/superblocks/ext.c \ + src/superblocks/erofs.c \ src/superblocks/f2fs.c \ src/superblocks/gfs.c \ src/superblocks/hfs.c \ diff --git a/libblkid/src/superblocks/erofs.c b/libblkid/src/superblocks/erofs.c new file mode 100644 index 00000000..0bf15913 --- /dev/null +++ b/libblkid/src/superblocks/erofs.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2020 Gao Xiang + * + * This file may be redistributed under the terms of the + * GNU Lesser General Public License + */ +#include +#include + +#include "superblocks.h" + +#define EROFS_SUPER_OFFSET 1024 +#define EROFS_SB_KBOFF (EROFS_SUPER_OFFSET >> 10) + +#define EROFS_SUPER_MAGIC_V1 "\xe2\xe1\xf5\xe0" +#define EROFS_MAGIC_OFF 0 + +/* All in little-endian */ +struct erofs_super_block { + uint32_t magic; + uint32_t checksum; + uint32_t feature_compat; + uint8_t blkszbits; + uint8_t reserved; + + uint16_t root_nid; + uint64_t inos; + + uint64_t build_time; + uint32_t build_time_nsec; + uint32_t blocks; + uint32_t meta_blkaddr; + uint32_t xattr_blkaddr; + uint8_t uuid[16]; + uint8_t volume_name[16]; + uint32_t feature_incompat; + uint8_t reserved2[44]; +}; + +static int probe_erofs(blkid_probe pr, const struct blkid_idmag *mag) +{ + struct erofs_super_block *sb; + + sb = blkid_probe_get_sb(pr, mag, struct erofs_super_block); + if (!sb) + return errno ? -errno : BLKID_PROBE_NONE; + + if (sb->volume_name[0]) + blkid_probe_set_label(pr, (unsigned char *)sb->volume_name, + sizeof(sb->volume_name)); + + blkid_probe_set_uuid(pr, sb->uuid); + + return BLKID_PROBE_OK; +} + +const struct blkid_idinfo erofs_idinfo = +{ + .name = "erofs", + .usage = BLKID_USAGE_FILESYSTEM, + .probefunc = probe_erofs, + .magics = + { + { + .magic = EROFS_SUPER_MAGIC_V1, + .len = 4, + .kboff = EROFS_SB_KBOFF, + .sboff = EROFS_MAGIC_OFF, + }, { NULL } + } +}; diff --git a/libblkid/src/superblocks/superblocks.c b/libblkid/src/superblocks/superblocks.c index 80bd6e59..9d0d2cb1 100644 --- a/libblkid/src/superblocks/superblocks.c +++ b/libblkid/src/superblocks/superblocks.c @@ -155,7 +155,8 @@ static const struct blkid_idinfo *idinfos[] = &befs_idinfo, &nilfs2_idinfo, &exfat_idinfo, - &f2fs_idinfo + &f2fs_idinfo, + &erofs_idinfo }; /* diff --git a/libblkid/src/superblocks/superblocks.h b/libblkid/src/superblocks/superblocks.h index 3bbfb9c1..808ae6a6 100644 --- a/libblkid/src/superblocks/superblocks.h +++ b/libblkid/src/superblocks/superblocks.h @@ -74,6 +74,7 @@ extern const struct blkid_idinfo nilfs2_idinfo; extern const struct blkid_idinfo exfat_idinfo; extern const struct blkid_idinfo f2fs_idinfo; extern const struct blkid_idinfo bcache_idinfo; +extern const struct blkid_idinfo erofs_idinfo; /* * superblock functions