| 1 |
/* |
| 2 |
* Copyright (C) 2020 Gao Xiang |
| 3 |
* |
| 4 |
* This file may be redistributed under the terms of the |
| 5 |
* GNU Lesser General Public License |
| 6 |
* |
| 7 |
* https://docs.kernel.org/filesystems/erofs.html |
| 8 |
*/ |
| 9 |
#include <stddef.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "superblocks.h" |
| 13 |
#include "crc32c.h" |
| 14 |
|
| 15 |
#define EROFS_SUPER_OFFSET 1024 |
| 16 |
#define EROFS_SB_KBOFF (EROFS_SUPER_OFFSET >> 10) |
| 17 |
#define EROFS_FEATURE_SB_CSUM (1 << 0) |
| 18 |
|
| 19 |
#define EROFS_SUPER_MAGIC_V1 "\xe2\xe1\xf5\xe0" |
| 20 |
#define EROFS_MAGIC_OFF 0 |
| 21 |
|
| 22 |
/* All in little-endian */ |
| 23 |
struct erofs_super_block { |
| 24 |
uint32_t magic; |
| 25 |
uint32_t checksum; |
| 26 |
uint32_t feature_compat; |
| 27 |
uint8_t blkszbits; |
| 28 |
uint8_t reserved; |
| 29 |
|
| 30 |
uint16_t root_nid; |
| 31 |
uint64_t inos; |
| 32 |
|
| 33 |
uint64_t build_time; |
| 34 |
uint32_t build_time_nsec; |
| 35 |
uint32_t blocks; |
| 36 |
uint32_t meta_blkaddr; |
| 37 |
uint32_t xattr_blkaddr; |
| 38 |
uint8_t uuid[16]; |
| 39 |
uint8_t volume_name[16]; |
| 40 |
uint32_t feature_incompat; |
| 41 |
uint8_t reserved2[44]; |
| 42 |
} __attribute__((packed)); |
| 43 |
|
| 44 |
static int erofs_verify_checksum(blkid_probe pr, const struct blkid_idmag *mag, |
| 45 |
const struct erofs_super_block *sb) |
| 46 |
{ |
| 47 |
uint32_t expected, csum; |
| 48 |
size_t csummed_size; |
| 49 |
const unsigned char *csummed; |
| 50 |
|
| 51 |
if (!(le32_to_cpu(sb->feature_compat) & EROFS_FEATURE_SB_CSUM)) |
| 52 |
return 1; |
| 53 |
|
| 54 |
expected = le32_to_cpu(sb->checksum); |
| 55 |
|
| 56 |
/* kernel validates blkszbits 9..PAGE_SHIFT; |
| 57 |
* block size must be > EROFS_SUPER_OFFSET to avoid underflow */ |
| 58 |
if (sb->blkszbits < 9 || sb->blkszbits > 16 |
| 59 |
|| (1U << sb->blkszbits) <= EROFS_SUPER_OFFSET) |
| 60 |
return 0; |
| 61 |
|
| 62 |
csummed_size = (1U << sb->blkszbits) - EROFS_SUPER_OFFSET; |
| 63 |
|
| 64 |
csummed = blkid_probe_get_sb_buffer(pr, mag, csummed_size); |
| 65 |
if (!csummed) |
| 66 |
return 0; |
| 67 |
|
| 68 |
csum = ul_crc32c_exclude_offset(~0L, csummed, csummed_size, |
| 69 |
offsetof(struct erofs_super_block, checksum), |
| 70 |
sizeof_member(struct erofs_super_block, checksum)); |
| 71 |
|
| 72 |
return blkid_probe_verify_csum(pr, csum, expected); |
| 73 |
} |
| 74 |
|
| 75 |
static int probe_erofs(blkid_probe pr, const struct blkid_idmag *mag) |
| 76 |
{ |
| 77 |
const struct erofs_super_block *sb; |
| 78 |
|
| 79 |
sb = blkid_probe_get_sb(pr, mag, struct erofs_super_block); |
| 80 |
if (!sb) |
| 81 |
return errno ? -errno : BLKID_PROBE_NONE; |
| 82 |
|
| 83 |
/* block size must be between 512 and 64k */ |
| 84 |
if (sb->blkszbits < 9 || sb->blkszbits > 16) |
| 85 |
return BLKID_PROBE_NONE; |
| 86 |
|
| 87 |
if (!erofs_verify_checksum(pr, mag, sb)) |
| 88 |
return BLKID_PROBE_NONE; |
| 89 |
|
| 90 |
if (sb->volume_name[0]) |
| 91 |
blkid_probe_set_label(pr, (unsigned char *)sb->volume_name, |
| 92 |
sizeof(sb->volume_name)); |
| 93 |
|
| 94 |
blkid_probe_set_uuid(pr, sb->uuid); |
| 95 |
blkid_probe_set_fsblocksize(pr, 1U << sb->blkszbits); |
| 96 |
blkid_probe_set_block_size(pr, 1U << sb->blkszbits); |
| 97 |
blkid_probe_set_fssize(pr, (uint64_t) (1U << sb->blkszbits) * le32_to_cpu(sb->blocks)); |
| 98 |
|
| 99 |
return BLKID_PROBE_OK; |
| 100 |
} |
| 101 |
|
| 102 |
const struct blkid_idinfo erofs_idinfo = |
| 103 |
{ |
| 104 |
.name = "erofs", |
| 105 |
.usage = BLKID_USAGE_FILESYSTEM, |
| 106 |
.probefunc = probe_erofs, |
| 107 |
.magics = |
| 108 |
{ |
| 109 |
{ |
| 110 |
.magic = EROFS_SUPER_MAGIC_V1, |
| 111 |
.len = 4, |
| 112 |
.kboff = EROFS_SB_KBOFF, |
| 113 |
.sboff = EROFS_MAGIC_OFF, |
| 114 |
}, { NULL } |
| 115 |
} |
| 116 |
}; |