printf("FS version %hhu.%hhu\n",
sb->version.major, sb->version.minor);
printf("Sector size %10u\n",
- BLOCK_SIZE(*sb));
+ SECTOR_SIZE(*sb));
printf("Cluster size %10u\n",
CLUSTER_SIZE(*sb));
}
-static void print_block_info(const struct exfat_super_block* sb)
+static void print_sector_info(const struct exfat_super_block* sb)
{
printf("Sectors count %10"PRIu64"\n",
- le64_to_cpu(sb->block_count));
+ le64_to_cpu(sb->sector_count));
}
static void print_cluster_info(const struct exfat_super_block* sb)
static void print_other_info(const struct exfat_super_block* sb)
{
printf("First sector %10"PRIu64"\n",
- le64_to_cpu(sb->block_start));
+ le64_to_cpu(sb->sector_start));
printf("FAT first sector %10u\n",
- le32_to_cpu(sb->fat_block_start));
+ le32_to_cpu(sb->fat_sector_start));
printf("FAT sectors count %10u\n",
- le32_to_cpu(sb->fat_block_count));
+ le32_to_cpu(sb->fat_sector_count));
printf("First cluster sector %10u\n",
- le32_to_cpu(sb->cluster_block_start));
+ le32_to_cpu(sb->cluster_sector_start));
printf("Root directory cluster %10u\n",
le32_to_cpu(sb->rootdir_cluster));
printf("Volume state 0x%04hx\n",
}
print_generic_info(&sb);
- print_block_info(&sb);
+ print_sector_info(&sb);
print_cluster_info(&sb);
print_other_info(&sb);
off_t a = 0, b = 0;
printf("Used sectors ");
- while (exfat_find_used_blocks(ef, &a, &b) == 0)
+ while (exfat_find_used_sectors(ef, &a, &b) == 0)
printf(" %"PRIu64"-%"PRIu64, a, b);
puts("");
}
{
struct exfat ef;
uint32_t free_clusters;
- uint64_t free_blocks;
+ uint64_t free_sectors;
if (exfat_mount(&ef, spec, "ro") != 0)
return 1;
free_clusters = exfat_count_free_clusters(&ef);
- free_blocks = (uint64_t) free_clusters << ef.sb->bpc_bits;
+ free_sectors = (uint64_t) free_clusters << ef.sb->spc_bits;
printf("Volume label %15s\n", exfat_get_label(&ef));
print_generic_info(ef.sb);
- print_block_info(ef.sb);
- printf("Free sectors %10"PRIu64"\n", free_blocks);
+ print_sector_info(ef.sb);
+ printf("Free sectors %10"PRIu64"\n", free_sectors);
print_cluster_info(ef.sb);
printf("Free clusters %10u\n", free_clusters);
print_other_info(ef.sb);
{
sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
- sfs->f_blocks = le64_to_cpu(ef.sb->block_count) >> ef.sb->bpc_bits;
+ sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
sfs->f_bavail = exfat_count_free_clusters(&ef);
sfs->f_bfree = sfs->f_bavail;
sfs->f_namemax = EXFAT_NAME_MAX;
b) no such thing as inode;
So here we assume that inode = cluster.
*/
- sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->bpc_bits;
- sfs->f_favail = sfs->f_bfree >> ef.sb->bpc_bits;
+ sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits;
+ sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits;
sfs->f_ffree = sfs->f_bavail;
return 0;
#include <string.h>
/*
- * Block to absolute offset.
+ * Sector to absolute offset.
*/
-static off_t b2o(const struct exfat* ef, off_t block)
+static off_t s2o(const struct exfat* ef, off_t sector)
{
- return block << ef->sb->block_bits;
+ return sector << ef->sb->sector_bits;
}
/*
- * Cluster to block.
+ * Cluster to sector.
*/
-static off_t c2b(const struct exfat* ef, cluster_t cluster)
+static off_t c2s(const struct exfat* ef, cluster_t cluster)
{
if (cluster < EXFAT_FIRST_DATA_CLUSTER)
exfat_bug("invalid cluster number %u", cluster);
- return le32_to_cpu(ef->sb->cluster_block_start) +
- ((off_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->bpc_bits);
+ return le32_to_cpu(ef->sb->cluster_sector_start) +
+ ((off_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->spc_bits);
}
/*
*/
off_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
{
- return b2o(ef, c2b(ef, cluster));
+ return s2o(ef, c2s(ef, cluster));
}
/*
- * Block to cluster.
+ * Sector to cluster.
*/
-static cluster_t b2c(const struct exfat* ef, off_t block)
+static cluster_t s2c(const struct exfat* ef, off_t sector)
{
- return ((block - le32_to_cpu(ef->sb->cluster_block_start)) >>
- ef->sb->bpc_bits) + EXFAT_FIRST_DATA_CLUSTER;
+ return ((sector - le32_to_cpu(ef->sb->cluster_sector_start)) >>
+ ef->sb->spc_bits) + EXFAT_FIRST_DATA_CLUSTER;
}
/*
if (IS_CONTIGUOUS(*node))
return cluster + 1;
- fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
+ fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
+ cluster * sizeof(cluster_t);
exfat_read_raw(&next, sizeof(next), fat_offset, ef->fd);
return le32_to_cpu(next);
if (contiguous)
return;
- fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
+ fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
+ current * sizeof(cluster_t);
next_le32 = cpu_to_le32(next);
exfat_write_raw(&next_le32, sizeof(next_le32), fat_offset, ef->fd);
static void erase_raw(struct exfat* ef, size_t size, off_t offset)
{
- exfat_write_raw(ef->zero_block, size, offset, ef->fd);
+ exfat_write_raw(ef->zero_sector, size, offset, ef->fd);
}
static int erase_range(struct exfat* ef, struct exfat_node* node,
uint64_t begin, uint64_t end)
{
- uint64_t block_boundary;
+ uint64_t sector_boundary;
cluster_t cluster;
if (begin >= end)
return 0;
- block_boundary = (node->size | (BLOCK_SIZE(*ef->sb) - 1)) + 1;
+ sector_boundary = (node->size | (SECTOR_SIZE(*ef->sb) - 1)) + 1;
cluster = exfat_advance_cluster(ef, node,
node->size / CLUSTER_SIZE(*ef->sb));
if (CLUSTER_INVALID(cluster))
exfat_error("invalid cluster in file");
return -EIO;
}
- /* erase from the beginning to the closest block boundary */
- erase_raw(ef, MIN(block_boundary, end) - node->size,
+ /* erase from the beginning to the closest sector boundary */
+ erase_raw(ef, MIN(sector_boundary, end) - node->size,
exfat_c2o(ef, cluster) + node->size % CLUSTER_SIZE(*ef->sb));
- /* erase whole blocks */
- while (block_boundary < end)
+ /* erase whole sectors */
+ while (sector_boundary < end)
{
- if (block_boundary % CLUSTER_SIZE(*ef->sb) == 0)
+ if (sector_boundary % CLUSTER_SIZE(*ef->sb) == 0)
cluster = exfat_next_cluster(ef, node, cluster);
- erase_raw(ef, BLOCK_SIZE(*ef->sb),
- exfat_c2o(ef, cluster) + block_boundary % CLUSTER_SIZE(*ef->sb));
- block_boundary += BLOCK_SIZE(*ef->sb);
+ erase_raw(ef, SECTOR_SIZE(*ef->sb),
+ exfat_c2o(ef, cluster) + sector_boundary % CLUSTER_SIZE(*ef->sb));
+ sector_boundary += SECTOR_SIZE(*ef->sb);
}
return 0;
}
return 0;
}
-int exfat_find_used_blocks(const struct exfat* ef, off_t* a, off_t* b)
+int exfat_find_used_sectors(const struct exfat* ef, off_t* a, off_t* b)
{
cluster_t ca, cb;
ca = cb = EXFAT_FIRST_DATA_CLUSTER - 1;
else
{
- ca = b2c(ef, *a);
- cb = b2c(ef, *b);
+ ca = s2c(ef, *a);
+ cb = s2c(ef, *b);
}
if (find_used_clusters(ef, &ca, &cb) != 0)
return 1;
if (*a != 0 || *b != 0)
- *a = c2b(ef, ca);
- *b = c2b(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / BLOCK_SIZE(*ef->sb);
+ *a = c2s(ef, ca);
+ *b = c2s(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / SECTOR_SIZE(*ef->sb);
return 0;
}
#define EXFAT_ATTRIB_DIRTY 0x40000
#define EXFAT_ATTRIB_UNLINKED 0x80000
#define IS_CONTIGUOUS(node) (((node).flags & EXFAT_ATTRIB_CONTIGUOUS) != 0)
-#define BLOCK_SIZE(sb) (1 << (sb).block_bits)
-#define CLUSTER_SIZE(sb) (BLOCK_SIZE(sb) << (sb).bpc_bits)
+#define SECTOR_SIZE(sb) (1 << (sb).sector_bits)
+#define CLUSTER_SIZE(sb) (SECTOR_SIZE(sb) << (sb).spc_bits)
#define CLUSTER_INVALID(c) ((c) == EXFAT_CLUSTER_BAD || (c) == EXFAT_CLUSTER_END)
#define MIN(a, b) ((a) < (b) ? (a) : (b))
cmap;
char label[EXFAT_ENAME_MAX * 6 + 1]; /* a character can occupy up to
6 bytes in UTF-8 */
- void* zero_block;
+ void* zero_sector;
int dmask, fmask;
uid_t uid;
gid_t gid;
void exfat_flush_cmap(struct exfat* ef);
int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size);
uint32_t exfat_count_free_clusters(const struct exfat* ef);
-int exfat_find_used_blocks(const struct exfat* ef, off_t* a, off_t* b);
+int exfat_find_used_sectors(const struct exfat* ef, off_t* a, off_t* b);
void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
struct stat* stbuf);
uint16_t exfat_add_checksum(const void* entry, uint16_t sum);
le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
const struct exfat_entry_meta2* meta2, const le16_t* name);
-uint32_t exfat_vbr_start_checksum(const void* block, size_t size);
-uint32_t exfat_vbr_add_checksum(const void* block, size_t size, uint32_t sum);
+uint32_t exfat_vbr_start_checksum(const void* sector, size_t size);
+uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum);
le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name);
void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb);
void exfat_print_info(const struct exfat_super_block* sb,
#define EXFAT_FIRST_DATA_CLUSTER 2
#define EXFAT_CLUSTER_FREE 0 /* free cluster */
-#define EXFAT_CLUSTER_BAD 0xfffffff7 /* cluster contains bad block */
+#define EXFAT_CLUSTER_BAD 0xfffffff7 /* cluster contains bad sector */
#define EXFAT_CLUSTER_END 0xffffffff /* final cluster of file or directory */
struct exfat_super_block
uint8_t jump[3]; /* 0x00 jmp and nop instructions */
uint8_t oem_name[8]; /* 0x03 "EXFAT " */
uint8_t __unused1[53]; /* 0x0B always 0 */
- le64_t block_start; /* 0x40 partition first block */
- le64_t block_count; /* 0x48 partition blocks count */
- le32_t fat_block_start; /* 0x50 FAT first block */
- le32_t fat_block_count; /* 0x54 FAT blocks count */
- le32_t cluster_block_start; /* 0x58 first cluster block */
+ le64_t sector_start; /* 0x40 partition first sector */
+ le64_t sector_count; /* 0x48 partition sectors count */
+ le32_t fat_sector_start; /* 0x50 FAT first sector */
+ le32_t fat_sector_count; /* 0x54 FAT sectors count */
+ le32_t cluster_sector_start; /* 0x58 first cluster sector */
le32_t cluster_count; /* 0x5C total clusters count */
le32_t rootdir_cluster; /* 0x60 first cluster of the root dir */
le32_t volume_serial; /* 0x64 volume serial number */
}
version;
le16_t volume_state; /* 0x6A volume state flags */
- uint8_t block_bits; /* 0x6C block size as (1 << n) */
- uint8_t bpc_bits; /* 0x6D blocks per cluster as (1 << n) */
+ uint8_t sector_bits; /* 0x6C sector size as (1 << n) */
+ uint8_t spc_bits; /* 0x6D sectors per cluster as (1 << n) */
uint8_t fat_count; /* 0x6E always 1 */
uint8_t drive_no; /* 0x6F always 0x80 */
uint8_t allocated_percent; /* 0x70 percentage of allocated space */
ef->noatime = match_option(options, "noatime");
}
-static int verify_vbr_checksum(void* block, off_t block_size, int fd)
+static int verify_vbr_checksum(void* sector, off_t sector_size, int fd)
{
uint32_t vbr_checksum;
int i;
- exfat_read_raw(block, block_size, 0, fd);
- vbr_checksum = exfat_vbr_start_checksum(block, block_size);
+ exfat_read_raw(sector, sector_size, 0, fd);
+ vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
for (i = 1; i < 11; i++)
{
- exfat_read_raw(block, block_size, i * block_size, fd);
- vbr_checksum = exfat_vbr_add_checksum(block, block_size, vbr_checksum);
+ exfat_read_raw(sector, sector_size, i * sector_size, fd);
+ vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
+ vbr_checksum);
}
- exfat_read_raw(block, block_size, i * block_size, fd);
- for (i = 0; i < block_size / sizeof(vbr_checksum); i++)
- if (((const uint32_t*) block)[i] != vbr_checksum)
+ exfat_read_raw(sector, sector_size, i * sector_size, fd);
+ for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
+ if (((const uint32_t*) sector)[i] != vbr_checksum)
{
exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
- ((const uint32_t*) block)[i], vbr_checksum);
+ ((const uint32_t*) sector)[i], vbr_checksum);
return 1;
}
return 0;
return -EIO;
}
/* officially exFAT supports cluster size up to 32 MB */
- if ((int) ef->sb->block_bits + (int) ef->sb->bpc_bits > 25)
+ if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
{
close(ef->fd);
free(ef->sb);
exfat_error("too big cluster size: 2^%d",
- (int) ef->sb->block_bits + (int) ef->sb->bpc_bits);
+ (int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
return -EIO;
}
- ef->zero_block = malloc(BLOCK_SIZE(*ef->sb));
- if (ef->zero_block == NULL)
+ ef->zero_sector = malloc(SECTOR_SIZE(*ef->sb));
+ if (ef->zero_sector == NULL)
{
close(ef->fd);
free(ef->sb);
- exfat_error("failed to allocate zero block");
+ exfat_error("failed to allocate zero sector");
return -ENOMEM;
}
- /* use zero_block as a temporary buffer for VBR checksum verification */
- if (verify_vbr_checksum(ef->zero_block, BLOCK_SIZE(*ef->sb), ef->fd) != 0)
+ /* use zero_sector as a temporary buffer for VBR checksum verification */
+ if (verify_vbr_checksum(ef->zero_sector, SECTOR_SIZE(*ef->sb), ef->fd) != 0)
{
- free(ef->zero_block);
+ free(ef->zero_sector);
close(ef->fd);
free(ef->sb);
return -EIO;
}
- memset(ef->zero_block, 0, BLOCK_SIZE(*ef->sb));
+ memset(ef->zero_sector, 0, SECTOR_SIZE(*ef->sb));
ef->root = malloc(sizeof(struct exfat_node));
if (ef->root == NULL)
{
- free(ef->zero_block);
+ free(ef->zero_sector);
close(ef->fd);
free(ef->sb);
exfat_error("failed to allocate root node");
exfat_put_node(ef, ef->root);
exfat_reset_cache(ef);
free(ef->root);
- free(ef->zero_block);
+ free(ef->zero_sector);
close(ef->fd);
free(ef->sb);
return -EIO;
exfat_reset_cache(ef);
free(ef->root);
ef->root = NULL;
- free(ef->zero_block);
- ef->zero_block = NULL;
+ free(ef->zero_sector);
+ ef->zero_sector = NULL;
free(ef->cmap.chunk);
ef->cmap.chunk = NULL;
if (fsync(ef->fd) < 0)
return cpu_to_le16(checksum);
}
-uint32_t exfat_vbr_start_checksum(const void* block, size_t size)
+uint32_t exfat_vbr_start_checksum(const void* sector, size_t size)
{
size_t i;
uint32_t sum = 0;
for (i = 0; i < size; i++)
/* skip volume_state and allocated_percent fields */
if (i != 0x6a && i != 0x6b && i != 0x70)
- sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) block)[i];
+ sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
return sum;
}
-uint32_t exfat_vbr_add_checksum(const void* block, size_t size, uint32_t sum)
+uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum)
{
size_t i;
for (i = 0; i < size; i++)
- sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) block)[i];
+ sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
return sum;
}
uint32_t free_clusters)
{
struct exfat_human_bytes hb;
- off_t total_space = (off_t) le64_to_cpu(sb->block_count) * BLOCK_SIZE(*sb);
+ off_t total_space = le64_to_cpu(sb->sector_count) * SECTOR_SIZE(*sb);
off_t avail_space = (off_t) free_clusters * CLUSTER_SIZE(*sb);
printf("File system version %hhu.%hhu\n",
sb->version.major, sb->version.minor);
- exfat_humanize_bytes(BLOCK_SIZE(*sb), &hb);
+ exfat_humanize_bytes(SECTOR_SIZE(*sb), &hb);
printf("Sector size %10"PRIu64" %s\n", hb.value, hb.unit);
exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb);
printf("Cluster size %10"PRIu64" %s\n", hb.value, hb.unit);
return errno;
free(bitmap);
- sb.cluster_block_start = cpu_to_le32(base / BLOCK_SIZE(sb));
+ sb.cluster_sector_start = cpu_to_le32(base / SECTOR_SIZE(sb));
bitmap_entry.start_cluster = cpu_to_le32(OFFSET_TO_CLUSTER(base));
bitmap_entry.size = cpu_to_le64(cbm_size());
return 0;
off_t fat_alignment(void)
{
- return (off_t) le32_to_cpu(sb.fat_block_start) * BLOCK_SIZE(sb);
+ return (off_t) le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb);
}
off_t fat_size(void)
{
- return (off_t) le32_to_cpu(sb.fat_block_count) * BLOCK_SIZE(sb);
+ return (off_t) le32_to_cpu(sb.fat_sector_count) * SECTOR_SIZE(sb);
}
static cluster_t fat_write_entry(cluster_t cluster, cluster_t value, int fd)
{
cluster_t c = 0;
- if (base != le32_to_cpu(sb.fat_block_start) * BLOCK_SIZE(sb))
+ if (base != le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb))
exfat_bug("unexpected FAT location: %"PRIu64" (expected %u)",
- base, le32_to_cpu(sb.fat_block_start) * BLOCK_SIZE(sb));
+ base, le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb));
if (!(c = fat_write_entry(c, 0xfffffff8, fd))) /* media type */
return errno;
int (*write_data)(off_t, int);
};
-static int init_sb(off_t volume_size, int block_bits, int bpc_bits,
+static int init_sb(off_t volume_size, int sector_bits, int spc_bits,
uint32_t volume_serial)
{
- uint32_t clusters_max = (volume_size >> block_bits >> bpc_bits);
- uint32_t fat_blocks = DIV_ROUND_UP(clusters_max * 4, 1 << block_bits);
+ uint32_t clusters_max = (volume_size >> sector_bits >> spc_bits);
+ uint32_t fat_sectors = DIV_ROUND_UP(clusters_max * 4, 1 << sector_bits);
uint32_t allocated_clusters;
memset(&sb, 0, sizeof(struct exfat_super_block));
sb.jump[1] = 0x76;
sb.jump[2] = 0x90;
memcpy(sb.oem_name, "EXFAT ", sizeof(sb.oem_name));
- sb.block_start = cpu_to_le64(0); /* FIXME */
- sb.block_count = cpu_to_le64(volume_size >> block_bits);
- sb.fat_block_start = cpu_to_le32(128); /* FIXME */
- sb.fat_block_count = cpu_to_le32(ROUND_UP(
- le32_to_cpu(sb.fat_block_start) + fat_blocks, 1 << bpc_bits) -
- le32_to_cpu(sb.fat_block_start));
- /* cluster_block_start will be set later */
+ sb.sector_start = cpu_to_le64(0); /* FIXME */
+ sb.sector_count = cpu_to_le64(volume_size >> sector_bits);
+ sb.fat_sector_start = cpu_to_le32(128); /* FIXME */
+ sb.fat_sector_count = cpu_to_le32(ROUND_UP(
+ le32_to_cpu(sb.fat_sector_start) + fat_sectors, 1 << spc_bits) -
+ le32_to_cpu(sb.fat_sector_start));
+ /* cluster_sector_start will be set later */
sb.cluster_count = cpu_to_le32(clusters_max -
- ((le32_to_cpu(sb.fat_block_start) +
- le32_to_cpu(sb.fat_block_count)) >> bpc_bits));
+ ((le32_to_cpu(sb.fat_sector_start) +
+ le32_to_cpu(sb.fat_sector_count)) >> spc_bits));
/* rootdir_cluster will be set later */
sb.volume_serial = cpu_to_le32(volume_serial);
sb.version.major = 1;
sb.version.minor = 0;
sb.volume_state = cpu_to_le16(0);
- sb.block_bits = block_bits;
- sb.bpc_bits = bpc_bits;
+ sb.sector_bits = sector_bits;
+ sb.spc_bits = spc_bits;
sb.fat_count = 1;
sb.drive_no = 0x80;
sb.allocated_percent = 0;
DIV_ROUND_UP(cbm_size(), CLUSTER_SIZE(sb)) +
DIV_ROUND_UP(uct_size(), CLUSTER_SIZE(sb)) +
DIV_ROUND_UP(rootdir_size(), CLUSTER_SIZE(sb));
- if (clusters_max < ((le32_to_cpu(sb.fat_block_start) +
- le32_to_cpu(sb.fat_block_count)) >> bpc_bits) + allocated_clusters)
+ if (clusters_max < ((le32_to_cpu(sb.fat_sector_start) +
+ le32_to_cpu(sb.fat_sector_count)) >> spc_bits) +
+ allocated_clusters)
{
exfat_error("too small volume (%"PRIu64" bytes)", volume_size);
return 1;
static int erase_device(int fd)
{
- uint64_t erase_blocks = (uint64_t)
- le32_to_cpu(sb.fat_block_start) +
- le32_to_cpu(sb.fat_block_count) +
- DIV_ROUND_UP(cbm_size(), 1 << sb.block_bits) +
- DIV_ROUND_UP(uct_size(), 1 << sb.block_bits) +
- DIV_ROUND_UP(rootdir_size(), 1 << sb.block_bits);
+ uint64_t erase_sectors = (uint64_t)
+ le32_to_cpu(sb.fat_sector_start) +
+ le32_to_cpu(sb.fat_sector_count) +
+ DIV_ROUND_UP(cbm_size(), 1 << sb.sector_bits) +
+ DIV_ROUND_UP(uct_size(), 1 << sb.sector_bits) +
+ DIV_ROUND_UP(rootdir_size(), 1 << sb.sector_bits);
uint64_t i;
- void* block;
+ void* sector;
if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
{
return 1;
}
- block = malloc(BLOCK_SIZE(sb));
- if (block == NULL)
+ sector = malloc(SECTOR_SIZE(sb));
+ if (sector == NULL)
{
- exfat_error("failed to allocate erase block");
+ exfat_error("failed to allocate erase sector");
return 1;
}
- memset(block, 0, BLOCK_SIZE(sb));
+ memset(sector, 0, SECTOR_SIZE(sb));
- for (i = 0; i < erase_blocks; i++)
+ for (i = 0; i < erase_sectors; i++)
{
- if (write(fd, block, BLOCK_SIZE(sb)) == -1)
+ if (write(fd, sector, SECTOR_SIZE(sb)) == -1)
{
- free(block);
- exfat_error("failed to erase block %"PRIu64, i);
+ free(sector);
+ exfat_error("failed to erase sector %"PRIu64, i);
return 1;
}
- if (i * 100 / erase_blocks != (i + 1) * 100 / erase_blocks)
+ if (i * 100 / erase_sectors != (i + 1) * 100 / erase_sectors)
{
- printf("\b\b\b%2"PRIu64"%%", (i + 1) * 100 / erase_blocks);
+ printf("\b\b\b%2"PRIu64"%%", (i + 1) * 100 / erase_sectors);
fflush(stdout);
}
}
- free(block);
+ free(sector);
return 0;
}
return 0;
}
-static int get_bpc_bits(int user_defined, off_t volume_size)
+static int get_spc_bits(int user_defined, off_t volume_size)
{
if (user_defined != -1)
return user_defined;
return (now.tv_sec << 20) | now.tv_usec;
}
-static int mkfs(const char* spec, int block_bits, int bpc_bits,
+static int mkfs(const char* spec, int sector_bits, int spc_bits,
const char* volume_label, uint32_t volume_serial)
{
int fd;
exfat_error("seek failed");
return 1;
}
- bpc_bits = get_bpc_bits(bpc_bits, volume_size);
+ spc_bits = get_spc_bits(spc_bits, volume_size);
if (set_volume_label(fd, volume_label) != 0)
{
return 1;
}
- if (init_sb(volume_size, block_bits, bpc_bits, volume_serial) != 0)
+ if (init_sb(volume_size, sector_bits, spc_bits, volume_serial) != 0)
{
close(fd);
return 1;
{
const char* spec = NULL;
char** pp;
- int bpc_bits = -1;
+ int spc_bits = -1;
const char* volume_label = NULL;
uint32_t volume_serial = 0;
pp++;
if (*pp == NULL)
usage(argv[0]);
- bpc_bits = logarithm2(atoi(*pp));
- if (bpc_bits < 0)
+ spc_bits = logarithm2(atoi(*pp));
+ if (spc_bits < 0)
{
exfat_error("invalid option value: `%s'", *pp);
return 1;
printf("mkexfatfs %u.%u.%u\n",
EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
- return mkfs(spec, 9, bpc_bits, volume_label, volume_serial);
+ return mkfs(spec, 9, spc_bits, volume_label, volume_serial);
}
extern struct exfat_entry_bitmap bitmap_entry;
extern struct exfat_entry_upcase upcase_entry;
-#define OFFSET_TO_BLOCK(off) ((off) >> (sb).block_bits)
-#define BLOCK_TO_CLUSTER(block) \
- ((((block) - le32_to_cpu((sb).cluster_block_start)) >> (sb).bpc_bits) + \
+#define OFFSET_TO_SECTOR(off) ((off) >> (sb).sector_bits)
+#define SECTOR_TO_CLUSTER(sector) \
+ ((((sector) - le32_to_cpu((sb).cluster_sector_start)) >> (sb).spc_bits) + \
EXFAT_FIRST_DATA_CLUSTER)
-#define OFFSET_TO_CLUSTER(off) BLOCK_TO_CLUSTER(OFFSET_TO_BLOCK(off))
+#define OFFSET_TO_CLUSTER(off) SECTOR_TO_CLUSTER(OFFSET_TO_SECTOR(off))
#endif /* ifndef MKFS_MKEXFAT_H_INCLUDED */
off_t vbr_size(void)
{
- return 12 * BLOCK_SIZE(sb);
+ return 12 * SECTOR_SIZE(sb);
}
int vbr_write(off_t base, int fd)
{
uint32_t checksum;
- le32_t* block = malloc(BLOCK_SIZE(sb));
+ le32_t* sector = malloc(SECTOR_SIZE(sb));
size_t i;
- if (block == NULL)
+ if (sector == NULL)
return errno;
if (write(fd, &sb, sizeof(struct exfat_super_block)) == -1)
return errno;
checksum = exfat_vbr_start_checksum(&sb, sizeof(struct exfat_super_block));
- memset(block, 0, BLOCK_SIZE(sb));
- block[BLOCK_SIZE(sb) / sizeof(block[0]) - 1] = cpu_to_le32(0xaa550000);
+ memset(sector, 0, SECTOR_SIZE(sb));
+ sector[SECTOR_SIZE(sb) / sizeof(sector[0]) - 1] = cpu_to_le32(0xaa550000);
for (i = 0; i < 8; i++)
{
- if (write(fd, block, BLOCK_SIZE(sb)) == -1)
+ if (write(fd, sector, SECTOR_SIZE(sb)) == -1)
return errno;
- checksum = exfat_vbr_add_checksum(block, BLOCK_SIZE(sb), checksum);
+ checksum = exfat_vbr_add_checksum(sector, SECTOR_SIZE(sb), checksum);
}
- memset(block, 0, BLOCK_SIZE(sb));
- if (write(fd, block, BLOCK_SIZE(sb)) == -1)
+ memset(sector, 0, SECTOR_SIZE(sb));
+ if (write(fd, sector, SECTOR_SIZE(sb)) == -1)
return errno;
- checksum = exfat_vbr_add_checksum(block, BLOCK_SIZE(sb), checksum);
- if (write(fd, block, BLOCK_SIZE(sb)) == -1)
+ checksum = exfat_vbr_add_checksum(sector, SECTOR_SIZE(sb), checksum);
+ if (write(fd, sector, SECTOR_SIZE(sb)) == -1)
return errno;
- checksum = exfat_vbr_add_checksum(block, BLOCK_SIZE(sb), checksum);
+ checksum = exfat_vbr_add_checksum(sector, SECTOR_SIZE(sb), checksum);
- for (i = 0; i < BLOCK_SIZE(sb) / sizeof(block[0]); i++)
- block[i] = cpu_to_le32(checksum);
- if (write(fd, block, BLOCK_SIZE(sb)) == -1)
+ for (i = 0; i < SECTOR_SIZE(sb) / sizeof(sector[0]); i++)
+ sector[i] = cpu_to_le32(checksum);
+ if (write(fd, sector, SECTOR_SIZE(sb)) == -1)
return errno;
- free(block);
+ free(sector);
return 0;
}