]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/superblocks/exfat.c
libblkid: exfat: fix fail to find volume label
[thirdparty/util-linux.git] / libblkid / src / superblocks / exfat.c
1 /*
2 * Copyright (C) 2010 Andrew Nayenko <resver@gmail.com>
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 static int exfat_valid_superblock(blkid_probe pr, const struct exfat_super_block *sb)
165 {
166 if (le16_to_cpu(sb->BootSignature) != 0xAA55)
167 return 0;
168
169 if (!CLUSTER_SIZE(sb))
170 return 0;
171
172 if (memcmp(sb->JumpBoot, "\xEB\x76\x90", 3) != 0)
173 return 0;
174
175 for (size_t i = 0; i < sizeof(sb->MustBeZero); i++)
176 if (sb->MustBeZero[i] != 0x00)
177 return 0;
178
179 if (!exfat_validate_checksum(pr, sb))
180 return 0;
181
182 return 1;
183 }
184
185 /* function prototype to avoid warnings (duplicate in partitions/dos.c) */
186 extern int blkid_probe_is_exfat(blkid_probe pr);
187
188 /*
189 * This function is used by MBR partition table parser to avoid
190 * misinterpretation of exFAT filesystem.
191 */
192 int blkid_probe_is_exfat(blkid_probe pr)
193 {
194 struct exfat_super_block *sb;
195 const struct blkid_idmag *mag = NULL;
196 int rc;
197
198 rc = blkid_probe_get_idmag(pr, &vfat_idinfo, NULL, &mag);
199 if (rc < 0)
200 return rc; /* error */
201 if (rc != BLKID_PROBE_OK || !mag)
202 return 0;
203
204 sb = blkid_probe_get_sb(pr, mag, struct exfat_super_block);
205 if (!sb)
206 return 0;
207
208 if (memcmp(sb->FileSystemName, "EXFAT ", 8) != 0)
209 return 0;
210
211 return exfat_valid_superblock(pr, sb);
212 }
213
214 static int probe_exfat(blkid_probe pr, const struct blkid_idmag *mag)
215 {
216 struct exfat_super_block *sb;
217 struct exfat_entry_label *label;
218
219 sb = blkid_probe_get_sb(pr, mag, struct exfat_super_block);
220 if (!sb)
221 return errno ? -errno : BLKID_PROBE_NONE;
222
223 if (!exfat_valid_superblock(pr, sb))
224 return BLKID_PROBE_NONE;
225
226 label = find_label(pr, sb);
227 if (label)
228 blkid_probe_set_utf8label(pr, label->name,
229 min((size_t) label->length * 2, sizeof(label->name)),
230 UL_ENCODE_UTF16LE);
231 else if (errno)
232 return -errno;
233
234 blkid_probe_sprintf_uuid(pr, sb->VolumeSerialNumber, 4,
235 "%02hhX%02hhX-%02hhX%02hhX",
236 sb->VolumeSerialNumber[3], sb->VolumeSerialNumber[2],
237 sb->VolumeSerialNumber[1], sb->VolumeSerialNumber[0]);
238
239 blkid_probe_sprintf_version(pr, "%u.%u",
240 sb->FileSystemRevision.vermaj, sb->FileSystemRevision.vermin);
241
242 blkid_probe_set_fsblocksize(pr, BLOCK_SIZE(sb));
243 blkid_probe_set_block_size(pr, BLOCK_SIZE(sb));
244 blkid_probe_set_fssize(pr, BLOCK_SIZE(sb) * le64_to_cpu(sb->VolumeLength));
245
246 return BLKID_PROBE_OK;
247 }
248
249 const struct blkid_idinfo exfat_idinfo =
250 {
251 .name = "exfat",
252 .usage = BLKID_USAGE_FILESYSTEM,
253 .probefunc = probe_exfat,
254 .magics =
255 {
256 { .magic = "EXFAT ", .len = 8, .sboff = 3 },
257 { NULL }
258 }
259 };