| 1 |
/* |
| 2 |
* Copyright (C) 2010 Andrew Nayenko <[email protected]> |
| 3 |
* |
| 4 |
* This file may be redistributed under the terms of the |
| 5 |
* GNU Lesser General Public License. |
| 6 |
*/ |
| 7 |
#include "superblocks.h" |
| 8 |
|
| 9 |
struct exfat_super_block { |
| 10 |
uint8_t JumpBoot[3]; |
| 11 |
uint8_t FileSystemName[8]; |
| 12 |
uint8_t MustBeZero[53]; |
| 13 |
uint64_t PartitionOffset; |
| 14 |
uint64_t VolumeLength; |
| 15 |
uint32_t FatOffset; |
| 16 |
uint32_t FatLength; |
| 17 |
uint32_t ClusterHeapOffset; |
| 18 |
uint32_t ClusterCount; |
| 19 |
uint32_t FirstClusterOfRootDirectory; |
| 20 |
uint8_t VolumeSerialNumber[4]; |
| 21 |
struct { |
| 22 |
uint8_t vermin; |
| 23 |
uint8_t vermaj; |
| 24 |
} FileSystemRevision; |
| 25 |
uint16_t VolumeFlags; |
| 26 |
uint8_t BytesPerSectorShift; |
| 27 |
uint8_t SectorsPerClusterShift; |
| 28 |
uint8_t NumberOfFats; |
| 29 |
uint8_t DriveSelect; |
| 30 |
uint8_t PercentInUse; |
| 31 |
uint8_t Reserved[7]; |
| 32 |
uint8_t BootCode[390]; |
| 33 |
uint16_t BootSignature; |
| 34 |
} __attribute__((__packed__)); |
| 35 |
|
| 36 |
struct exfat_entry_label { |
| 37 |
uint8_t type; |
| 38 |
uint8_t length; |
| 39 |
uint8_t name[22]; |
| 40 |
uint8_t reserved[8]; |
| 41 |
} __attribute__((__packed__)); |
| 42 |
|
| 43 |
#define BLOCK_SIZE(sb) ((sb)->BytesPerSectorShift < 32 ? (1u << (sb)->BytesPerSectorShift) : 0) |
| 44 |
#define CLUSTER_SIZE(sb) ((sb)->SectorsPerClusterShift < 32 ? (BLOCK_SIZE(sb) << (sb)->SectorsPerClusterShift) : 0) |
| 45 |
#define EXFAT_FIRST_DATA_CLUSTER 2 |
| 46 |
#define EXFAT_LAST_DATA_CLUSTER 0xffffff6 |
| 47 |
#define EXFAT_ENTRY_SIZE 32 |
| 48 |
|
| 49 |
#define EXFAT_ENTRY_EOD 0x00 |
| 50 |
#define EXFAT_ENTRY_LABEL 0x83 |
| 51 |
|
| 52 |
#define EXFAT_MAX_DIR_SIZE (256 * 1024 * 1024) |
| 53 |
|
| 54 |
static uint64_t block_to_offset(const struct exfat_super_block *sb, |
| 55 |
uint64_t block) |
| 56 |
{ |
| 57 |
return block << sb->BytesPerSectorShift; |
| 58 |
} |
| 59 |
|
| 60 |
static uint64_t cluster_to_block(const struct exfat_super_block *sb, |
| 61 |
uint32_t cluster) |
| 62 |
{ |
| 63 |
return le32_to_cpu(sb->ClusterHeapOffset) + |
| 64 |
((uint64_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) |
| 65 |
<< sb->SectorsPerClusterShift); |
| 66 |
} |
| 67 |
|
| 68 |
static uint64_t cluster_to_offset(const struct exfat_super_block *sb, |
| 69 |
uint32_t cluster) |
| 70 |
{ |
| 71 |
return block_to_offset(sb, cluster_to_block(sb, cluster)); |
| 72 |
} |
| 73 |
|
| 74 |
static uint32_t next_cluster(blkid_probe pr, |
| 75 |
const struct exfat_super_block *sb, uint32_t cluster) |
| 76 |
{ |
| 77 |
uint32_t *nextp, next; |
| 78 |
uint64_t fat_offset; |
| 79 |
|
| 80 |
fat_offset = block_to_offset(sb, le32_to_cpu(sb->FatOffset)) |
| 81 |
+ (uint64_t) cluster * sizeof(cluster); |
| 82 |
nextp = (uint32_t *) blkid_probe_get_buffer(pr, fat_offset, |
| 83 |
sizeof(uint32_t)); |
| 84 |
if (!nextp) |
| 85 |
return 0; |
| 86 |
memcpy(&next, nextp, sizeof(next)); |
| 87 |
return le32_to_cpu(next); |
| 88 |
} |
| 89 |
|
| 90 |
static struct exfat_entry_label *find_label(blkid_probe pr, |
| 91 |
const struct exfat_super_block *sb) |
| 92 |
{ |
| 93 |
uint32_t cluster = le32_to_cpu(sb->FirstClusterOfRootDirectory); |
| 94 |
uint64_t offset = cluster_to_offset(sb, cluster); |
| 95 |
uint8_t *entry; |
| 96 |
const size_t max_iter = EXFAT_MAX_DIR_SIZE / EXFAT_ENTRY_SIZE; |
| 97 |
size_t i = 0; |
| 98 |
|
| 99 |
for (; i < max_iter; i++) { |
| 100 |
entry = (uint8_t *) blkid_probe_get_buffer(pr, offset, |
| 101 |
EXFAT_ENTRY_SIZE); |
| 102 |
if (!entry) |
| 103 |
return NULL; |
| 104 |
if (entry[0] == EXFAT_ENTRY_EOD) |
| 105 |
return NULL; |
| 106 |
if (entry[0] == EXFAT_ENTRY_LABEL) |
| 107 |
return (struct exfat_entry_label *) entry; |
| 108 |
|
| 109 |
offset += EXFAT_ENTRY_SIZE; |
| 110 |
if (CLUSTER_SIZE(sb) && (offset % CLUSTER_SIZE(sb)) == 0) { |
| 111 |
cluster = next_cluster(pr, sb, cluster); |
| 112 |
if (cluster < EXFAT_FIRST_DATA_CLUSTER) |
| 113 |
return NULL; |
| 114 |
if (cluster > EXFAT_LAST_DATA_CLUSTER) |
| 115 |
return NULL; |
| 116 |
offset = cluster_to_offset(sb, cluster); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
/* From https://docs.microsoft.com/en-us/windows/win32/fileio/exfat-specification#34-main-and-backup-boot-checksum-sub-regions */ |
| 124 |
static uint32_t exfat_boot_checksum(const unsigned char *sectors, |
| 125 |
size_t sector_size) |
| 126 |
{ |
| 127 |
uint32_t n_bytes = sector_size * 11; |
| 128 |
uint32_t checksum = 0; |
| 129 |
|
| 130 |
for (size_t i = 0; i < n_bytes; i++) { |
| 131 |
if ((i == 106) || (i == 107) || (i == 112)) |
| 132 |
continue; |
| 133 |
|
| 134 |
checksum = ((checksum & 1) ? 0x80000000 : 0) + (checksum >> 1) |
| 135 |
+ (uint32_t) sectors[i]; |
| 136 |
} |
| 137 |
|
| 138 |
return checksum; |
| 139 |
} |
| 140 |
|
| 141 |
static int exfat_validate_checksum(blkid_probe pr, |
| 142 |
const struct exfat_super_block *sb) |
| 143 |
{ |
| 144 |
size_t sector_size = BLOCK_SIZE(sb); |
| 145 |
/* 11 sectors will be checksummed, the 12th contains the expected */ |
| 146 |
const unsigned char *data = blkid_probe_get_buffer(pr, 0, sector_size * 12); |
| 147 |
if (!data) |
| 148 |
return 0; |
| 149 |
|
| 150 |
uint32_t checksum = exfat_boot_checksum(data, sector_size); |
| 151 |
|
| 152 |
/* The expected checksum is repeated, check all of them */ |
| 153 |
for (size_t i = 0; i < sector_size / sizeof(uint32_t); i++) { |
| 154 |
size_t offset = sector_size * 11 + i * 4; |
| 155 |
uint32_t *expected_addr = (uint32_t *) &data[offset]; |
| 156 |
uint32_t expected = le32_to_cpu(*expected_addr); |
| 157 |
if (!blkid_probe_verify_csum(pr, checksum, expected)) |
| 158 |
return 0; |
| 159 |
}; |
| 160 |
|
| 161 |
return 1; |
| 162 |
} |
| 163 |
|
| 164 |
#define in_range_inclusive(val, start, stop) (val >= start && val <= stop) |
| 165 |
|
| 166 |
static int exfat_valid_superblock(blkid_probe pr, const struct exfat_super_block *sb) |
| 167 |
{ |
| 168 |
if (le16_to_cpu(sb->BootSignature) != 0xAA55) |
| 169 |
return 0; |
| 170 |
|
| 171 |
if (!CLUSTER_SIZE(sb)) |
| 172 |
return 0; |
| 173 |
|
| 174 |
if (memcmp(sb->JumpBoot, "\xEB\x76\x90", 3) != 0) |
| 175 |
return 0; |
| 176 |
|
| 177 |
if (memcmp(sb->FileSystemName, "EXFAT ", 8) != 0) |
| 178 |
return 0; |
| 179 |
|
| 180 |
for (size_t i = 0; i < sizeof(sb->MustBeZero); i++) |
| 181 |
if (sb->MustBeZero[i] != 0x00) |
| 182 |
return 0; |
| 183 |
|
| 184 |
if (!in_range_inclusive(sb->NumberOfFats, 1, 2)) |
| 185 |
return 0; |
| 186 |
|
| 187 |
if (!in_range_inclusive(sb->BytesPerSectorShift, 9, 12)) |
| 188 |
return 0; |
| 189 |
|
| 190 |
if (!in_range_inclusive(sb->SectorsPerClusterShift, |
| 191 |
0, |
| 192 |
25 - sb->BytesPerSectorShift)) |
| 193 |
return 0; |
| 194 |
|
| 195 |
if (!in_range_inclusive(le32_to_cpu(sb->FatOffset), |
| 196 |
24, |
| 197 |
le32_to_cpu(sb->ClusterHeapOffset) - |
| 198 |
(le32_to_cpu(sb->FatLength) * sb->NumberOfFats))) |
| 199 |
return 0; |
| 200 |
|
| 201 |
if (!in_range_inclusive(le32_to_cpu(sb->ClusterHeapOffset), |
| 202 |
le32_to_cpu(sb->FatOffset) + |
| 203 |
le32_to_cpu(sb->FatLength) * sb->NumberOfFats, |
| 204 |
1U << (32 - 1))) |
| 205 |
return 0; |
| 206 |
|
| 207 |
if (!in_range_inclusive(le32_to_cpu(sb->FirstClusterOfRootDirectory), |
| 208 |
2, |
| 209 |
le32_to_cpu(sb->ClusterCount) + 1)) |
| 210 |
return 0; |
| 211 |
|
| 212 |
if (!exfat_validate_checksum(pr, sb)) |
| 213 |
return 0; |
| 214 |
|
| 215 |
return 1; |
| 216 |
} |
| 217 |
|
| 218 |
/* function prototype to avoid warnings (duplicate in partitions/dos.c) */ |
| 219 |
extern int blkid_probe_is_exfat(blkid_probe pr); |
| 220 |
|
| 221 |
/* |
| 222 |
* This function is used by MBR partition table parser to avoid |
| 223 |
* misinterpretation of exFAT filesystem. |
| 224 |
*/ |
| 225 |
int blkid_probe_is_exfat(blkid_probe pr) |
| 226 |
{ |
| 227 |
const struct exfat_super_block *sb; |
| 228 |
const struct blkid_idmag *mag = NULL; |
| 229 |
int rc; |
| 230 |
|
| 231 |
rc = blkid_probe_get_idmag(pr, &vfat_idinfo, NULL, &mag); |
| 232 |
if (rc < 0) |
| 233 |
return rc; /* error */ |
| 234 |
if (rc != BLKID_PROBE_OK || !mag) |
| 235 |
return 0; |
| 236 |
|
| 237 |
sb = blkid_probe_get_sb(pr, mag, struct exfat_super_block); |
| 238 |
if (!sb) |
| 239 |
return 0; |
| 240 |
|
| 241 |
if (memcmp(sb->FileSystemName, "EXFAT ", 8) != 0) |
| 242 |
return 0; |
| 243 |
|
| 244 |
return exfat_valid_superblock(pr, sb); |
| 245 |
} |
| 246 |
|
| 247 |
static int probe_exfat(blkid_probe pr, const struct blkid_idmag *mag) |
| 248 |
{ |
| 249 |
const struct exfat_super_block *sb; |
| 250 |
struct exfat_entry_label *label; |
| 251 |
|
| 252 |
sb = blkid_probe_get_sb(pr, mag, struct exfat_super_block); |
| 253 |
if (!sb) |
| 254 |
return errno ? -errno : BLKID_PROBE_NONE; |
| 255 |
|
| 256 |
if (!exfat_valid_superblock(pr, sb)) |
| 257 |
return BLKID_PROBE_NONE; |
| 258 |
|
| 259 |
label = find_label(pr, sb); |
| 260 |
if (label) |
| 261 |
blkid_probe_set_utf8label(pr, label->name, |
| 262 |
min((size_t) label->length * 2, sizeof(label->name)), |
| 263 |
UL_ENCODE_UTF16LE); |
| 264 |
else if (errno) |
| 265 |
return -errno; |
| 266 |
|
| 267 |
blkid_probe_sprintf_uuid(pr, sb->VolumeSerialNumber, 4, |
| 268 |
"%02hhX%02hhX-%02hhX%02hhX", |
| 269 |
sb->VolumeSerialNumber[3], sb->VolumeSerialNumber[2], |
| 270 |
sb->VolumeSerialNumber[1], sb->VolumeSerialNumber[0]); |
| 271 |
|
| 272 |
blkid_probe_sprintf_version(pr, "%u.%u", |
| 273 |
sb->FileSystemRevision.vermaj, sb->FileSystemRevision.vermin); |
| 274 |
|
| 275 |
blkid_probe_set_fsblocksize(pr, BLOCK_SIZE(sb)); |
| 276 |
blkid_probe_set_block_size(pr, BLOCK_SIZE(sb)); |
| 277 |
blkid_probe_set_fssize(pr, BLOCK_SIZE(sb) * le64_to_cpu(sb->VolumeLength)); |
| 278 |
|
| 279 |
return BLKID_PROBE_OK; |
| 280 |
} |
| 281 |
|
| 282 |
const struct blkid_idinfo exfat_idinfo = |
| 283 |
{ |
| 284 |
.name = "exfat", |
| 285 |
.usage = BLKID_USAGE_FILESYSTEM, |
| 286 |
.probefunc = probe_exfat, |
| 287 |
.magics = |
| 288 |
{ |
| 289 |
{ .magic = "EXFAT ", .len = 8, .sboff = 3 }, |
| 290 |
{ NULL } |
| 291 |
} |
| 292 |
}; |