1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
/*
* Copyright (C) 2020 Gao Xiang
*
* This file may be redistributed under the terms of the
* GNU Lesser General Public License
*
* https://docs.kernel.org/filesystems/erofs.html
*/
#include <stddef.h>
#include <string.h>
#include "superblocks.h"
#include "crc32c.h"
#define EROFS_SUPER_OFFSET 1024
#define EROFS_SB_KBOFF (EROFS_SUPER_OFFSET >> 10)
#define EROFS_FEATURE_SB_CSUM (1 << 0)
#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];
} __attribute__((packed));
static int erofs_verify_checksum(blkid_probe pr, const struct blkid_idmag *mag,
const struct erofs_super_block *sb)
{
uint32_t expected, csum;
size_t csummed_size;
unsigned char *csummed;
if (!(le32_to_cpu(sb->feature_compat) & EROFS_FEATURE_SB_CSUM))
return 1;
expected = le32_to_cpu(sb->checksum);
csummed_size = (1U << sb->blkszbits) - EROFS_SUPER_OFFSET;
csummed = blkid_probe_get_sb_buffer(pr, mag, csummed_size);
if (!csummed)
return 0;
csum = ul_crc32c_exclude_offset(~0L, csummed, csummed_size,
offsetof(struct erofs_super_block, checksum),
sizeof_member(struct erofs_super_block, checksum));
return blkid_probe_verify_csum(pr, csum, expected);
}
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;
/* EROFS is restricted to 4KiB block size */
if (sb->blkszbits > 31 || (1U << sb->blkszbits) > 4096)
return BLKID_PROBE_NONE;
if (!erofs_verify_checksum(pr, mag, sb))
return 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);
blkid_probe_set_fsblocksize(pr, 1U << sb->blkszbits);
blkid_probe_set_block_size(pr, 1U << sb->blkszbits);
blkid_probe_set_fssize(pr, (uint64_t) (1U << sb->blkszbits) * le32_to_cpu(sb->blocks));
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 }
}
};
|