blob: c17a84735fcbd7623344c0eb2c41da50feb65191 [file] [log] [blame]
Marek Vasut9ad82a72025-03-17 04:12:45 +01001/*
2 mount.c (22.10.09)
3 exFAT file system implementation library.
4
5 Free exFAT implementation.
6 Copyright (C) 2010-2023 Andrew Nayenko
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21*/
22
23#include "exfat.h"
24#include <string.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <inttypes.h>
Marek Vasut894cf722025-03-17 04:12:46 +010028#ifndef __UBOOT__
Marek Vasut9ad82a72025-03-17 04:12:45 +010029#include <unistd.h>
30#include <sys/types.h>
Marek Vasut894cf722025-03-17 04:12:46 +010031#endif
Marek Vasut9ad82a72025-03-17 04:12:45 +010032
33static uint64_t rootdir_size(const struct exfat* ef)
34{
35 uint32_t clusters = 0;
36 uint32_t clusters_max = le32_to_cpu(ef->sb->cluster_count);
37 cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
38
39 /* Iterate all clusters of the root directory to calculate its size.
40 It can't be contiguous because there is no flag to indicate this. */
41 do
42 {
43 if (clusters == clusters_max) /* infinite loop detected */
44 {
45 exfat_error("root directory cannot occupy all %d clusters",
46 clusters);
47 return 0;
48 }
49 if (CLUSTER_INVALID(*ef->sb, rootdir_cluster))
50 {
51 exfat_error("bad cluster %#x while reading root directory",
52 rootdir_cluster);
53 return 0;
54 }
55 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
56 clusters++;
57 }
58 while (rootdir_cluster != EXFAT_CLUSTER_END);
59
60 return (uint64_t) clusters * CLUSTER_SIZE(*ef->sb);
61}
62
63static const char* get_option(const char* options, const char* option_name)
64{
65 const char* p;
66 size_t length = strlen(option_name);
67
68 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
69 if ((p == options || p[-1] == ',') && p[length] == '=')
70 return p + length + 1;
71 return NULL;
72}
73
74static int get_int_option(const char* options, const char* option_name,
75 int base, int default_value)
76{
77 const char* p = get_option(options, option_name);
78
79 if (p == NULL)
80 return default_value;
81 return strtol(p, NULL, base);
82}
83
84static void parse_options(struct exfat* ef, const char* options)
85{
86 int opt_umask;
87
88 opt_umask = get_int_option(options, "umask", 8, 0);
89 ef->dmask = get_int_option(options, "dmask", 8, opt_umask);
90 ef->fmask = get_int_option(options, "fmask", 8, opt_umask);
91
92 ef->uid = get_int_option(options, "uid", 10, geteuid());
93 ef->gid = get_int_option(options, "gid", 10, getegid());
94
95 ef->noatime = exfat_match_option(options, "noatime");
96
97 switch (get_int_option(options, "repair", 10, 0))
98 {
99 case 1:
100 ef->repair = EXFAT_REPAIR_ASK;
101 break;
102 case 2:
103 ef->repair = EXFAT_REPAIR_YES;
104 break;
105 default:
106 ef->repair = EXFAT_REPAIR_NO;
107 break;
108 }
109}
110
111static bool verify_vbr_checksum(const struct exfat* ef, void* sector)
112{
113 off_t sector_size = SECTOR_SIZE(*ef->sb);
114 uint32_t vbr_checksum;
115 size_t i;
116
117 if (exfat_pread(ef->dev, sector, sector_size, 0) < 0)
118 {
119 exfat_error("failed to read boot sector");
120 return false;
121 }
122 vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
123 for (i = 1; i < 11; i++)
124 {
125 if (exfat_pread(ef->dev, sector, sector_size, i * sector_size) < 0)
126 {
127 exfat_error("failed to read VBR sector");
128 return false;
129 }
130 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
131 vbr_checksum);
132 }
133 if (exfat_pread(ef->dev, sector, sector_size, i * sector_size) < 0)
134 {
135 exfat_error("failed to read VBR checksum sector");
136 return false;
137 }
138 for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
139 if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
140 {
141 exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
142 le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
143 if (!EXFAT_REPAIR(invalid_vbr_checksum, ef, sector, vbr_checksum))
144 return false;
145 }
146 return true;
147}
148
149static int commit_super_block(const struct exfat* ef)
150{
151 if (exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
152 {
153 exfat_error("failed to write super block");
154 return 1;
155 }
156 return exfat_fsync(ef->dev);
157}
158
159int exfat_soil_super_block(const struct exfat* ef)
160{
161 if (ef->ro)
162 return 0;
163
164 ef->sb->volume_state = cpu_to_le16(
165 le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED);
166 return commit_super_block(ef);
167}
168
169static void exfat_free(struct exfat* ef)
170{
171 exfat_close(ef->dev); /* first of all, close the descriptor */
172 ef->dev = NULL; /* struct exfat_dev is freed by exfat_close() */
173 free(ef->root);
174 ef->root = NULL;
175 free(ef->zero_cluster);
176 ef->zero_cluster = NULL;
177 free(ef->cmap.chunk);
178 ef->cmap.chunk = NULL;
179 free(ef->upcase);
180 ef->upcase = NULL;
181 free(ef->sb);
182 ef->sb = NULL;
183}
184
185int exfat_mount(struct exfat* ef, const char* spec, const char* options)
186{
187 int rc;
188 enum exfat_mode mode;
189
190 exfat_tzset();
191 memset(ef, 0, sizeof(struct exfat));
192
193 parse_options(ef, options);
194
195 if (exfat_match_option(options, "ro"))
196 mode = EXFAT_MODE_RO;
197 else if (exfat_match_option(options, "ro_fallback"))
198 mode = EXFAT_MODE_ANY;
199 else
200 mode = EXFAT_MODE_RW;
201 ef->dev = exfat_open(spec, mode);
202 if (ef->dev == NULL)
203 return -ENODEV;
204 if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
205 {
206 if (mode == EXFAT_MODE_ANY)
207 ef->ro = -1;
208 else
209 ef->ro = 1;
210 }
211
212 ef->sb = malloc(sizeof(struct exfat_super_block));
213 if (ef->sb == NULL)
214 {
215 exfat_error("failed to allocate memory for the super block");
216 exfat_free(ef);
217 return -ENOMEM;
218 }
219 memset(ef->sb, 0, sizeof(struct exfat_super_block));
220
221 if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
222 {
223 exfat_error("failed to read boot sector");
224 exfat_free(ef);
225 return -EIO;
226 }
227 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
228 {
229 exfat_error("exFAT file system is not found");
230 exfat_free(ef);
231 return -EIO;
232 }
233 /* sector cannot be smaller than 512 bytes */
234 if (ef->sb->sector_bits < 9)
235 {
236 exfat_error("too small sector size: 2^%hhd", ef->sb->sector_bits);
237 exfat_free(ef);
238 return -EIO;
239 }
240 /* officially exFAT supports cluster size up to 32 MB */
241 if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
242 {
243 exfat_error("too big cluster size: 2^(%hhd+%hhd)",
244 ef->sb->sector_bits, ef->sb->spc_bits);
245 exfat_free(ef);
246 return -EIO;
247 }
248 ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
249 if (ef->zero_cluster == NULL)
250 {
251 exfat_error("failed to allocate zero sector");
252 exfat_free(ef);
253 return -ENOMEM;
254 }
255 /* use zero_cluster as a temporary buffer for VBR checksum verification */
256 if (!verify_vbr_checksum(ef, ef->zero_cluster))
257 {
258 exfat_free(ef);
259 return -EIO;
260 }
261 memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
262 if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
263 {
264 exfat_error("unsupported exFAT version: %hhu.%hhu",
265 ef->sb->version.major, ef->sb->version.minor);
266 exfat_free(ef);
267 return -EIO;
268 }
269 if (ef->sb->fat_count != 1)
270 {
271 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
272 exfat_free(ef);
273 return -EIO;
274 }
275 if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) >
276 (uint64_t) exfat_get_size(ef->dev))
277 {
278 /* this can cause I/O errors later but we don't fail mounting to let
279 user rescue data */
280 exfat_warn("file system in sectors is larger than device: "
281 "%"PRIu64" * %d > %"PRIu64,
282 le64_to_cpu(ef->sb->sector_count), SECTOR_SIZE(*ef->sb),
283 exfat_get_size(ef->dev));
284 }
285 if ((off_t) le32_to_cpu(ef->sb->cluster_count) * CLUSTER_SIZE(*ef->sb) >
286 exfat_get_size(ef->dev))
287 {
288 exfat_error("file system in clusters is larger than device: "
289 "%u * %d > %"PRIu64,
290 le32_to_cpu(ef->sb->cluster_count), CLUSTER_SIZE(*ef->sb),
291 exfat_get_size(ef->dev));
292 exfat_free(ef);
293 return -EIO;
294 }
295 if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
296 exfat_warn("volume was not unmounted cleanly");
297
298 ef->root = malloc(sizeof(struct exfat_node));
299 if (ef->root == NULL)
300 {
301 exfat_error("failed to allocate root node");
302 exfat_free(ef);
303 return -ENOMEM;
304 }
305 memset(ef->root, 0, sizeof(struct exfat_node));
306 ef->root->attrib = EXFAT_ATTRIB_DIR;
307 ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
308 ef->root->fptr_cluster = ef->root->start_cluster;
309 ef->root->name[0] = cpu_to_le16('\0');
310 ef->root->valid_size = ef->root->size = rootdir_size(ef);
311 if (ef->root->size == 0)
312 {
313 exfat_free(ef);
314 return -EIO;
315 }
316 /* exFAT does not have time attributes for the root directory */
317 ef->root->mtime = 0;
318 ef->root->atime = 0;
319 /* always keep at least 1 reference to the root node */
320 exfat_get_node(ef->root);
321
322 rc = exfat_cache_directory(ef, ef->root);
323 if (rc != 0)
324 goto error;
325 if (ef->upcase == NULL)
326 {
327 exfat_error("upcase table is not found");
328 goto error;
329 }
330 if (ef->cmap.chunk == NULL)
331 {
332 exfat_error("clusters bitmap is not found");
333 goto error;
334 }
335
336 return 0;
337
338error:
339 exfat_put_node(ef, ef->root);
340 exfat_reset_cache(ef);
341 exfat_free(ef);
342 return -EIO;
343}
344
345static void finalize_super_block(struct exfat* ef)
346{
347 if (ef->ro)
348 return;
349
350 ef->sb->volume_state = cpu_to_le16(
351 le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
352
353 /* Some implementations set the percentage of allocated space to 0xff
354 on FS creation and never update it. In this case leave it as is. */
355 if (ef->sb->allocated_percent != 0xff)
356 {
357 uint32_t free, total;
358
359 free = exfat_count_free_clusters(ef);
360 total = le32_to_cpu(ef->sb->cluster_count);
361 ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
362 }
363
364 commit_super_block(ef); /* ignore return code */
365}
366
367void exfat_unmount(struct exfat* ef)
368{
369 exfat_flush_nodes(ef); /* ignore return code */
370 exfat_flush(ef); /* ignore return code */
371 exfat_put_node(ef, ef->root);
372 exfat_reset_cache(ef);
373 finalize_super_block(ef);
374 exfat_free(ef); /* will close the descriptor */
375}