Marek Vasut | 9ad82a7 | 2025-03-17 04:12:45 +0100 | [diff] [blame] | 1 | /* |
| 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 Vasut | 894cf72 | 2025-03-17 04:12:46 +0100 | [diff] [blame] | 28 | #ifndef __UBOOT__ |
Marek Vasut | 9ad82a7 | 2025-03-17 04:12:45 +0100 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <sys/types.h> |
Marek Vasut | 894cf72 | 2025-03-17 04:12:46 +0100 | [diff] [blame] | 31 | #endif |
Marek Vasut | 9ad82a7 | 2025-03-17 04:12:45 +0100 | [diff] [blame] | 32 | |
| 33 | static 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 | |
| 63 | static 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 | |
| 74 | static 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 | |
| 84 | static 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 | |
| 111 | static 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 | |
| 149 | static 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 | |
| 159 | int 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 | |
| 169 | static 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 | |
| 185 | int 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 | { |
Marek Vasut | 94b4c6b | 2025-03-17 04:12:48 +0100 | [diff] [blame^] | 229 | #ifndef __UBOOT__ |
| 230 | exfat_error( |
| 231 | #else |
| 232 | exfat_debug( |
| 233 | #endif |
| 234 | "exFAT file system is not found"); |
Marek Vasut | 9ad82a7 | 2025-03-17 04:12:45 +0100 | [diff] [blame] | 235 | exfat_free(ef); |
| 236 | return -EIO; |
| 237 | } |
| 238 | /* sector cannot be smaller than 512 bytes */ |
| 239 | if (ef->sb->sector_bits < 9) |
| 240 | { |
| 241 | exfat_error("too small sector size: 2^%hhd", ef->sb->sector_bits); |
| 242 | exfat_free(ef); |
| 243 | return -EIO; |
| 244 | } |
| 245 | /* officially exFAT supports cluster size up to 32 MB */ |
| 246 | if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25) |
| 247 | { |
| 248 | exfat_error("too big cluster size: 2^(%hhd+%hhd)", |
| 249 | ef->sb->sector_bits, ef->sb->spc_bits); |
| 250 | exfat_free(ef); |
| 251 | return -EIO; |
| 252 | } |
| 253 | ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb)); |
| 254 | if (ef->zero_cluster == NULL) |
| 255 | { |
| 256 | exfat_error("failed to allocate zero sector"); |
| 257 | exfat_free(ef); |
| 258 | return -ENOMEM; |
| 259 | } |
| 260 | /* use zero_cluster as a temporary buffer for VBR checksum verification */ |
| 261 | if (!verify_vbr_checksum(ef, ef->zero_cluster)) |
| 262 | { |
| 263 | exfat_free(ef); |
| 264 | return -EIO; |
| 265 | } |
| 266 | memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb)); |
| 267 | if (ef->sb->version.major != 1 || ef->sb->version.minor != 0) |
| 268 | { |
| 269 | exfat_error("unsupported exFAT version: %hhu.%hhu", |
| 270 | ef->sb->version.major, ef->sb->version.minor); |
| 271 | exfat_free(ef); |
| 272 | return -EIO; |
| 273 | } |
| 274 | if (ef->sb->fat_count != 1) |
| 275 | { |
| 276 | exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count); |
| 277 | exfat_free(ef); |
| 278 | return -EIO; |
| 279 | } |
| 280 | if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) > |
| 281 | (uint64_t) exfat_get_size(ef->dev)) |
| 282 | { |
| 283 | /* this can cause I/O errors later but we don't fail mounting to let |
| 284 | user rescue data */ |
| 285 | exfat_warn("file system in sectors is larger than device: " |
| 286 | "%"PRIu64" * %d > %"PRIu64, |
| 287 | le64_to_cpu(ef->sb->sector_count), SECTOR_SIZE(*ef->sb), |
| 288 | exfat_get_size(ef->dev)); |
| 289 | } |
| 290 | if ((off_t) le32_to_cpu(ef->sb->cluster_count) * CLUSTER_SIZE(*ef->sb) > |
| 291 | exfat_get_size(ef->dev)) |
| 292 | { |
| 293 | exfat_error("file system in clusters is larger than device: " |
| 294 | "%u * %d > %"PRIu64, |
| 295 | le32_to_cpu(ef->sb->cluster_count), CLUSTER_SIZE(*ef->sb), |
| 296 | exfat_get_size(ef->dev)); |
| 297 | exfat_free(ef); |
| 298 | return -EIO; |
| 299 | } |
| 300 | if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED) |
| 301 | exfat_warn("volume was not unmounted cleanly"); |
| 302 | |
| 303 | ef->root = malloc(sizeof(struct exfat_node)); |
| 304 | if (ef->root == NULL) |
| 305 | { |
| 306 | exfat_error("failed to allocate root node"); |
| 307 | exfat_free(ef); |
| 308 | return -ENOMEM; |
| 309 | } |
| 310 | memset(ef->root, 0, sizeof(struct exfat_node)); |
| 311 | ef->root->attrib = EXFAT_ATTRIB_DIR; |
| 312 | ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster); |
| 313 | ef->root->fptr_cluster = ef->root->start_cluster; |
| 314 | ef->root->name[0] = cpu_to_le16('\0'); |
| 315 | ef->root->valid_size = ef->root->size = rootdir_size(ef); |
| 316 | if (ef->root->size == 0) |
| 317 | { |
| 318 | exfat_free(ef); |
| 319 | return -EIO; |
| 320 | } |
| 321 | /* exFAT does not have time attributes for the root directory */ |
| 322 | ef->root->mtime = 0; |
| 323 | ef->root->atime = 0; |
| 324 | /* always keep at least 1 reference to the root node */ |
| 325 | exfat_get_node(ef->root); |
| 326 | |
| 327 | rc = exfat_cache_directory(ef, ef->root); |
| 328 | if (rc != 0) |
| 329 | goto error; |
| 330 | if (ef->upcase == NULL) |
| 331 | { |
| 332 | exfat_error("upcase table is not found"); |
| 333 | goto error; |
| 334 | } |
| 335 | if (ef->cmap.chunk == NULL) |
| 336 | { |
| 337 | exfat_error("clusters bitmap is not found"); |
| 338 | goto error; |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | |
| 343 | error: |
| 344 | exfat_put_node(ef, ef->root); |
| 345 | exfat_reset_cache(ef); |
| 346 | exfat_free(ef); |
| 347 | return -EIO; |
| 348 | } |
| 349 | |
| 350 | static void finalize_super_block(struct exfat* ef) |
| 351 | { |
| 352 | if (ef->ro) |
| 353 | return; |
| 354 | |
| 355 | ef->sb->volume_state = cpu_to_le16( |
| 356 | le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED); |
| 357 | |
| 358 | /* Some implementations set the percentage of allocated space to 0xff |
| 359 | on FS creation and never update it. In this case leave it as is. */ |
| 360 | if (ef->sb->allocated_percent != 0xff) |
| 361 | { |
| 362 | uint32_t free, total; |
| 363 | |
| 364 | free = exfat_count_free_clusters(ef); |
| 365 | total = le32_to_cpu(ef->sb->cluster_count); |
| 366 | ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total; |
| 367 | } |
| 368 | |
| 369 | commit_super_block(ef); /* ignore return code */ |
| 370 | } |
| 371 | |
| 372 | void exfat_unmount(struct exfat* ef) |
| 373 | { |
| 374 | exfat_flush_nodes(ef); /* ignore return code */ |
| 375 | exfat_flush(ef); /* ignore return code */ |
| 376 | exfat_put_node(ef, ef->root); |
| 377 | exfat_reset_cache(ef); |
| 378 | finalize_super_block(ef); |
| 379 | exfat_free(ef); /* will close the descriptor */ |
| 380 | } |