int main(int argc, char* argv[]) { const char* spec = NULL; int opt; int spc_bits = -1; const char* volume_label = NULL; uint32_t volume_serial = 0; uint64_t first_sector = 0; struct exfat_dev* dev; printf("mkexfatfs %u.%u.%u\n", EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); while ((opt = getopt(argc, argv, "i:n:p:s:V")) != -1) { switch (opt) { case 'i': volume_serial = strtol(optarg, NULL, 16); break; case 'n': volume_label = optarg; break; case 'p': first_sector = strtoll(optarg, NULL, 10); break; case 's': spc_bits = logarithm2(atoi(optarg)); if (spc_bits < 0) { exfat_error("invalid option value: `%s'", optarg); return 1; } break; case 'V': puts("Copyright (C) 2011-2013 Andrew Nayenko"); return 0; default: usage(argv[0]); break; } } if (argc - optind != 1) usage(argv[0]); spec = argv[optind]; dev = exfat_open(spec, EXFAT_MODE_RW); if (dev == NULL) return 1; if (setup(dev, 9, spc_bits, volume_label, volume_serial, first_sector) != 0) { exfat_close(dev); return 1; } if (exfat_close(dev) != 0) return 1; printf("File system created successfully.\n"); return 0; }
static int dump_sb(const char* spec) { struct exfat_dev* dev; struct exfat_super_block sb; dev = exfat_open(spec, EXFAT_MODE_RO); if (dev == NULL) return 1; if (exfat_read(dev, &sb, sizeof(struct exfat_super_block)) < 0) { exfat_close(dev); exfat_error("failed to read from '%s'", spec); return 1; } if (memcmp(sb.oem_name, "EXFAT ", sizeof(sb.oem_name)) != 0) { exfat_close(dev); exfat_error("exFAT file system is not found on '%s'", spec); return 1; } print_generic_info(&sb); print_sector_info(&sb); print_cluster_info(&sb); print_other_info(&sb); exfat_close(dev); return 0; }
int fuse_exfat_format(const char *label, ULONG dostype) { struct fuse_context* cntx = fuse_get_context(); struct exfat_mount_data* md = cntx->private_data; struct exfat_dev* dev; dev = exfat_open(md->device, EXFAT_MODE_RW); if (dev == NULL) return -EIO; if (setup(dev, 9, -1, label, 0, 0) != 0) { exfat_close(dev); return -EIO; } if (exfat_close(dev) != 0) return -EIO; return 0; }
void exfat_unmount(struct exfat* ef) { exfat_flush_nodes(ef); /* ignore return code */ exfat_flush(ef); /* ignore return code */ exfat_put_node(ef, ef->root); exfat_reset_cache(ef); free(ef->root); ef->root = NULL; finalize_super_block(ef); exfat_close(ef->dev); /* close descriptor immediately after fsync */ ef->dev = NULL; free(ef->zero_cluster); ef->zero_cluster = NULL; free(ef->cmap.chunk); ef->cmap.chunk = NULL; free(ef->sb); ef->sb = NULL; free(ef->upcase); ef->upcase = NULL; ef->upcase_chars = 0; }
int main(int argc, char* argv[]) { const char* spec = NULL; char** pp; int spc_bits = -1; const char* volume_label = NULL; uint32_t volume_serial = 0; uint64_t first_sector = 0; struct exfat_dev* dev; printf("mkexfatfs %u.%u.%u\n", EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); for (pp = argv + 1; *pp; pp++) { if (strcmp(*pp, "-s") == 0) { pp++; if (*pp == NULL) usage(argv[0]); spc_bits = logarithm2(atoi(*pp)); if (spc_bits < 0) { exfat_error("invalid option value: `%s'", *pp); return 1; } } else if (strcmp(*pp, "-n") == 0) { pp++; if (*pp == NULL) usage(argv[0]); volume_label = *pp; } else if (strcmp(*pp, "-i") == 0) { pp++; if (*pp == NULL) usage(argv[0]); volume_serial = strtol(*pp, NULL, 16); } else if (strcmp(*pp, "-p") == 0) { pp++; if (*pp == NULL) usage(argv[0]); first_sector = strtoll(*pp, NULL, 10); } else if (strcmp(*pp, "-v") == 0) { puts("Copyright (C) 2011-2013 Andrew Nayenko"); return 0; } else if (spec == NULL) spec = *pp; else usage(argv[0]); } if (spec == NULL) usage(argv[0]); dev = exfat_open(spec, EXFAT_MODE_RW); if (dev == NULL) return 1; if (setup(dev, 9, spc_bits, volume_label, volume_serial, first_sector) != 0) { exfat_close(dev); return 1; } if (exfat_close(dev) != 0) return 1; printf("File system created successfully.\n"); return 0; }
int exfat_mount(struct exfat* ef, const char* spec, const char* options) { int rc; enum exfat_mode mode; exfat_tzset(); memset(ef, 0, sizeof(struct exfat)); parse_options(ef, options); if (match_option(options, "ro")) mode = EXFAT_MODE_RO; else if (match_option(options, "ro_fallback")) mode = EXFAT_MODE_ANY; else mode = EXFAT_MODE_RW; ef->dev = exfat_open(spec, mode); if (ef->dev == NULL) return -EIO; if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO) { if (mode == EXFAT_MODE_ANY) ef->ro = -1; else ef->ro = 1; } ef->sb = malloc(sizeof(struct exfat_super_block)); if (ef->sb == NULL) { exfat_close(ef->dev); exfat_error("failed to allocate memory for the super block"); return -ENOMEM; } memset(ef->sb, 0, sizeof(struct exfat_super_block)); if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0) { exfat_close(ef->dev); free(ef->sb); exfat_error("failed to read boot sector"); return -EIO; } if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0) { exfat_close(ef->dev); free(ef->sb); exfat_error("exFAT file system is not found"); return -EIO; } /* sector cannot be smaller than 512 bytes */ if (ef->sb->sector_bits < 9) { exfat_close(ef->dev); exfat_error("too small sector size: 2^%hhd", ef->sb->sector_bits); free(ef->sb); return -EIO; } /* officially exFAT supports cluster size up to 32 MB */ if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25) { exfat_close(ef->dev); exfat_error("too big cluster size: 2^(%hhd+%hhd)", ef->sb->sector_bits, ef->sb->spc_bits); free(ef->sb); return -EIO; } ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb)); if (ef->zero_cluster == NULL) { exfat_close(ef->dev); free(ef->sb); exfat_error("failed to allocate zero sector"); return -ENOMEM; } /* use zero_cluster as a temporary buffer for VBR checksum verification */ if (!verify_vbr_checksum(ef->dev, ef->zero_cluster, SECTOR_SIZE(*ef->sb))) { free(ef->zero_cluster); exfat_close(ef->dev); free(ef->sb); return -EIO; } memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb)); if (ef->sb->version.major != 1 || ef->sb->version.minor != 0) { free(ef->zero_cluster); exfat_close(ef->dev); exfat_error("unsupported exFAT version: %hhu.%hhu", ef->sb->version.major, ef->sb->version.minor); free(ef->sb); return -EIO; } if (ef->sb->fat_count != 1) { free(ef->zero_cluster); exfat_close(ef->dev); exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count); free(ef->sb); return -EIO; } if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) > exfat_get_size(ef->dev)) { /* this can cause I/O errors later but we don't fail mounting to let user rescue data */ exfat_warn("file system is larger than underlying device: " "%"PRIu64" > %"PRIu64, le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb), exfat_get_size(ef->dev)); } ef->root = malloc(sizeof(struct exfat_node)); if (ef->root == NULL) { free(ef->zero_cluster); exfat_close(ef->dev); free(ef->sb); exfat_error("failed to allocate root node"); return -ENOMEM; } memset(ef->root, 0, sizeof(struct exfat_node)); ef->root->flags = EXFAT_ATTRIB_DIR; ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster); ef->root->fptr_cluster = ef->root->start_cluster; ef->root->name[0] = cpu_to_le16('\0'); ef->root->size = rootdir_size(ef); if (ef->root->size == 0) { free(ef->root); free(ef->zero_cluster); exfat_close(ef->dev); free(ef->sb); return -EIO; } /* exFAT does not have time attributes for the root directory */ ef->root->mtime = 0; ef->root->atime = 0; /* always keep at least 1 reference to the root node */ exfat_get_node(ef->root); rc = exfat_cache_directory(ef, ef->root); if (rc != 0) goto error; if (ef->upcase == NULL) { exfat_error("upcase table is not found"); goto error; } if (ef->cmap.chunk == NULL) { exfat_error("clusters bitmap is not found"); goto error; } if (prepare_super_block(ef) != 0) goto error; return 0; error: exfat_put_node(ef, ef->root); exfat_reset_cache(ef); free(ef->root); free(ef->zero_cluster); exfat_close(ef->dev); free(ef->sb); return -EIO; }