HelenOS sources
This source file includes following definitions.
- usage
- cfg_params_initialize
- cfg_print_info
- vbr_initialize
- bootsec_write
- ebs_write
- fat_initialize
- fat_allocate_clusters
- bitmap_write
- upcase_table_write
- root_dentries_write
- log2i
- vbr_checksum_start
- vbr_checksum_update
- upcase_table_checksum
- is_power_of_two
- main
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <block.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <byteorder.h>
#include <align.h>
#include <rndgen.h>
#include <str.h>
#include <getopt.h>
#include <macros.h>
#include "exfat.h"
#include "upcase.h"
#define NAME "mkexfat"
#define FAT_SECTOR_START 128
#define EBS_SECTOR_START 1
#define EBS_BACKUP_SECTOR_START 13
#define MBS_SECTOR 0
#define MBS_BACKUP_SECTOR 12
#define VBR_CHECKSUM_SECTOR 11
#define VBR_BACKUP_CHECKSUM_SECTOR 23
#define EBS_SIZE 8
#define div_round_up(a, b) (((a) + (b) - 1) / (b))
#define DEFAULT_CLUSTER_SIZE 4096
#define FIRST_FREE_CLUSTER 2
typedef struct exfat_cfg {
aoff64_t volume_start;
aoff64_t volume_count;
unsigned long fat_sector_count;
unsigned long data_start_sector;
unsigned long rootdir_cluster;
unsigned long upcase_table_cluster;
unsigned long bitmap_cluster;
unsigned long total_clusters;
unsigned long allocated_clusters;
size_t bitmap_size;
size_t sector_size;
size_t cluster_size;
const char *label;
} exfat_cfg_t;
static unsigned log2i(unsigned n);
static uint32_t
vbr_checksum_start(void const *octets, size_t nbytes);
static void
vbr_checksum_update(void const *octets, size_t nbytes, uint32_t *checksum);
static errno_t
ebs_write(service_id_t service_id, exfat_cfg_t *cfg,
int base, uint32_t *chksum);
static errno_t
bitmap_write(service_id_t service_id, exfat_cfg_t *cfg);
static uint32_t
upcase_table_checksum(void const *data, size_t nbytes);
static struct option const long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "cluster-size", required_argument, 0, 'c' },
{ "fs-size", required_argument, 0, 's' },
{ "label", required_argument, 0, 'L' },
};
static void usage(void)
{
printf("Usage: mkexfat [options] <device>\n"
"-c, --cluster-size ## Specify the cluster size (Kb)\n"
"-s, --fs-size ## Specify the filesystem size (sectors)\n"
" --label ## Volume label\n");
}
static void
cfg_params_initialize(exfat_cfg_t *cfg)
{
unsigned long fat_bytes;
unsigned long fat_cls;
aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
cfg->sector_size;
if (cfg->cluster_size != 0) {
cfg->total_clusters = volume_bytes / cfg->cluster_size;
goto skip_cluster_size_set;
}
cfg->total_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
while (cfg->total_clusters > 16000000ULL &&
(cfg->cluster_size < 32 * 1024 * 1024)) {
cfg->cluster_size <<= 1;
cfg->total_clusters = volume_bytes / cfg->cluster_size;
}
skip_cluster_size_set:
fat_bytes = (cfg->total_clusters + 3) * sizeof(uint32_t);
cfg->fat_sector_count = div_round_up(fat_bytes, cfg->sector_size);
cfg->data_start_sector = ROUND_UP(FAT_SECTOR_START +
cfg->fat_sector_count, cfg->cluster_size / cfg->sector_size);
fat_cls = div_round_up((cfg->data_start_sector -
FAT_SECTOR_START) * cfg->sector_size,
cfg->cluster_size);
if (fat_cls >= cfg->total_clusters) {
cfg->total_clusters = 0;
return;
}
cfg->total_clusters -= fat_cls;
cfg->bitmap_size = div_round_up(cfg->total_clusters, 8);
cfg->allocated_clusters = div_round_up(cfg->bitmap_size,
cfg->cluster_size);
cfg->allocated_clusters++;
cfg->allocated_clusters += div_round_up(sizeof(upcase_table),
cfg->cluster_size);
cfg->rootdir_cluster = 0;
cfg->bitmap_cluster = FIRST_FREE_CLUSTER;
cfg->volume_start = 0;
}
static void
cfg_print_info(exfat_cfg_t *cfg)
{
printf("Sector size: %lu\n",
(unsigned long) cfg->sector_size);
printf("Cluster size: %lu\n",
(unsigned long) cfg->cluster_size);
printf("FAT size in sectors: %lu\n", cfg->fat_sector_count);
printf("Data start sector: %lu\n", cfg->data_start_sector);
printf("Total num of clusters: %lu\n", cfg->total_clusters);
printf("Total used clusters: %lu\n", cfg->allocated_clusters);
printf("Bitmap size: %lu\n", (unsigned long)
div_round_up(cfg->bitmap_size, cfg->cluster_size));
printf("Upcase table size: %lu\n", (unsigned long)
div_round_up(sizeof(upcase_table), cfg->cluster_size));
}
static uint32_t
vbr_initialize(exfat_bs_t *mbs, exfat_cfg_t *cfg, uint32_t *chksum)
{
rndgen_t *rndgen;
errno_t rc;
uint32_t vsn;
rc = rndgen_create(&rndgen);
if (rc != EOK)
return rc;
rc = rndgen_uint32(rndgen, &vsn);
if (rc != EOK) {
rndgen_destroy(rndgen);
return rc;
}
rndgen_destroy(rndgen);
memset(mbs, 0, sizeof(exfat_bs_t));
mbs->jump[0] = 0xEB;
mbs->jump[1] = 0x76;
mbs->jump[2] = 0x90;
memcpy(mbs->oem_name, "EXFAT ", sizeof(mbs->oem_name));
mbs->volume_start = host2uint64_t_le(cfg->volume_start);
mbs->volume_count = host2uint64_t_le(cfg->volume_count);
mbs->fat_sector_start = host2uint32_t_le(FAT_SECTOR_START);
mbs->fat_sector_count = host2uint32_t_le(cfg->fat_sector_count);
mbs->data_start_sector = host2uint32_t_le(cfg->data_start_sector);
mbs->data_clusters = host2uint32_t_le(cfg->total_clusters);
mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
mbs->volume_serial = host2uint32_t_le(vsn);
mbs->version.major = 1;
mbs->version.minor = 0;
mbs->volume_flags = host2uint16_t_le(0);
mbs->bytes_per_sector = log2i(cfg->sector_size);
mbs->sec_per_cluster = log2i(cfg->cluster_size / cfg->sector_size);
assert((mbs->bytes_per_sector + mbs->sec_per_cluster) <= 25);
mbs->fat_count = 1;
mbs->drive_no = 0x80;
mbs->allocated_percent = 0;
mbs->signature = host2uint16_t_le(0xAA55);
*chksum = vbr_checksum_start(mbs, sizeof(exfat_bs_t));
return EOK;
}
static errno_t
bootsec_write(service_id_t service_id, exfat_cfg_t *cfg)
{
exfat_bs_t mbs;
uint32_t vbr_checksum;
uint32_t *chksum_sector;
errno_t rc;
unsigned idx;
chksum_sector = calloc(cfg->sector_size, sizeof(uint8_t));
if (!chksum_sector)
return ENOMEM;
rc = vbr_initialize(&mbs, cfg, &vbr_checksum);
if (rc != EOK)
goto exit;
rc = block_write_direct(service_id, MBS_SECTOR, 1, &mbs);
if (rc != EOK)
goto exit;
rc = ebs_write(service_id, cfg, EBS_SECTOR_START, &vbr_checksum);
if (rc != EOK)
goto exit;
rc = block_write_direct(service_id, MBS_BACKUP_SECTOR, 1, &mbs);
if (rc != EOK)
goto exit;
for (idx = 0; idx < cfg->sector_size / sizeof(uint32_t); ++idx)
chksum_sector[idx] = host2uint32_t_le(vbr_checksum);
rc = block_write_direct(service_id,
VBR_CHECKSUM_SECTOR, 1, chksum_sector);
if (rc != EOK)
goto exit;
rc = block_write_direct(service_id,
VBR_BACKUP_CHECKSUM_SECTOR, 1, chksum_sector);
if (rc != EOK)
goto exit;
rc = ebs_write(service_id, cfg,
EBS_BACKUP_SECTOR_START, &vbr_checksum);
exit:
free(chksum_sector);
return rc;
}
static errno_t
ebs_write(service_id_t service_id, exfat_cfg_t *cfg, int base,
uint32_t *chksum)
{
uint32_t *ebs = calloc(cfg->sector_size, sizeof(uint8_t));
int i;
errno_t rc;
if (!ebs)
return ENOMEM;
ebs[cfg->sector_size / 4 - 1] = host2uint32_t_le(0xAA550000);
for (i = 0; i < EBS_SIZE; ++i) {
vbr_checksum_update(ebs, cfg->sector_size, chksum);
rc = block_write_direct(service_id,
i + base, 1, ebs);
if (rc != EOK)
goto exit;
}
memset(ebs, 0, cfg->sector_size);
vbr_checksum_update(ebs, cfg->sector_size, chksum);
rc = block_write_direct(service_id, i++ + base, 1, ebs);
if (rc != EOK)
goto exit;
vbr_checksum_update(ebs, cfg->sector_size, chksum);
rc = block_write_direct(service_id, i + base, 1, ebs);
if (rc != EOK)
goto exit;
exit:
free(ebs);
return rc;
}
static errno_t
fat_initialize(service_id_t service_id, exfat_cfg_t *cfg)
{
unsigned long i;
uint32_t *pfat;
errno_t rc;
pfat = calloc(cfg->sector_size, 1);
if (!pfat)
return ENOMEM;
pfat[0] = host2uint32_t_le(0xFFFFFFF8);
pfat[1] = host2uint32_t_le(0xFFFFFFFF);
rc = block_write_direct(service_id, FAT_SECTOR_START, 1, pfat);
if (rc != EOK)
goto error;
pfat[0] = pfat[1] = 0;
for (i = 1; i < cfg->fat_sector_count; ++i) {
rc = block_write_direct(service_id,
FAT_SECTOR_START + i, 1, pfat);
if (rc != EOK)
goto error;
}
error:
free(pfat);
return rc;
}
static errno_t
fat_allocate_clusters(service_id_t service_id, exfat_cfg_t *cfg,
uint32_t cur_cls, unsigned long ncls)
{
errno_t rc;
unsigned const fat_entries = cfg->sector_size / sizeof(uint32_t);
aoff64_t fat_sec = cur_cls / fat_entries + FAT_SECTOR_START;
uint32_t *fat;
uint32_t next_cls = cur_cls;
cur_cls %= fat_entries;
fat = malloc(cfg->sector_size);
if (!fat)
return ENOMEM;
loop:
rc = block_read_direct(service_id, fat_sec, 1, fat);
if (rc != EOK)
goto exit;
assert(fat[cur_cls] == 0);
assert(ncls > 0);
for (; cur_cls < fat_entries && ncls > 1; ++cur_cls, --ncls)
fat[cur_cls] = host2uint32_t_le(++next_cls);
if (cur_cls == fat_entries) {
rc = block_write_direct(service_id, fat_sec++, 1, fat);
if (rc != EOK)
goto exit;
cur_cls = 0;
goto loop;
} else if (ncls == 1) {
fat[cur_cls] = host2uint32_t_le(0xFFFFFFFF);
}
rc = block_write_direct(service_id, fat_sec, 1, fat);
exit:
free(fat);
return rc;
}
static errno_t
bitmap_write(service_id_t service_id, exfat_cfg_t *cfg)
{
unsigned long i, sec;
unsigned long allocated_cls;
errno_t rc = EOK;
bool need_reset = true;
size_t const bss = div_round_up(cfg->bitmap_size, cfg->sector_size);
uint8_t *bitmap = malloc(cfg->sector_size);
if (!bitmap)
return ENOMEM;
allocated_cls = cfg->allocated_clusters;
for (sec = 0; sec < bss; ++sec) {
if (need_reset) {
need_reset = false;
memset(bitmap, 0, cfg->sector_size);
}
if (allocated_cls > 0) {
for (i = 0; i < allocated_cls; ++i) {
unsigned byte_idx = i / 8;
unsigned bit_idx = i % 8;
if (byte_idx == cfg->sector_size)
break;
bitmap[byte_idx] |= 1 << bit_idx;
}
allocated_cls -= i;
need_reset = true;
}
rc = block_write_direct(service_id,
cfg->data_start_sector + sec, 1, bitmap);
if (rc != EOK)
goto exit;
}
exit:
free(bitmap);
return rc;
}
static errno_t
upcase_table_write(service_id_t service_id, exfat_cfg_t *cfg)
{
errno_t rc = EOK;
aoff64_t start_sec, nsecs, i;
uint8_t *table_ptr;
uint8_t *buf;
size_t table_size = sizeof(upcase_table);
buf = malloc(cfg->sector_size);
if (!buf)
return ENOENT;
start_sec = cfg->data_start_sector;
start_sec += ((cfg->upcase_table_cluster - 2) * cfg->cluster_size) /
cfg->sector_size;
nsecs = div_round_up(sizeof(upcase_table), cfg->sector_size);
table_ptr = (uint8_t *) upcase_table;
for (i = 0; i < nsecs; ++i,
table_ptr += min(table_size, cfg->sector_size),
table_size -= cfg->sector_size) {
if (table_size < cfg->sector_size) {
memset(buf, 0, cfg->sector_size);
}
memcpy(buf, table_ptr, min(table_size, cfg->sector_size));
rc = block_write_direct(service_id,
start_sec + i, 1, buf);
if (rc != EOK)
goto exit;
}
exit:
free(buf);
return rc;
}
static errno_t
root_dentries_write(service_id_t service_id, exfat_cfg_t *cfg)
{
exfat_dentry_t *d;
aoff64_t rootdir_sec;
uint16_t wlabel[EXFAT_VOLLABEL_LEN + 1];
errno_t rc;
uint8_t *data;
unsigned long i;
data = calloc(cfg->sector_size, 1);
if (!data)
return ENOMEM;
d = (exfat_dentry_t *) data;
if (cfg->label != NULL) {
memset(wlabel, 0, (EXFAT_VOLLABEL_LEN + 1) * sizeof(uint16_t));
rc = str_to_utf16(wlabel, EXFAT_VOLLABEL_LEN + 1, cfg->label);
if (rc != EOK) {
rc = EINVAL;
goto exit;
}
d->type = EXFAT_TYPE_VOLLABEL;
memcpy(d->vollabel.label, wlabel, EXFAT_VOLLABEL_LEN * 2);
d->vollabel.size = utf16_wsize(wlabel);
assert(d->vollabel.size <= EXFAT_VOLLABEL_LEN);
d++;
} else {
d->type = EXFAT_TYPE_VOLLABEL & ~EXFAT_TYPE_USED;
}
d->type = EXFAT_TYPE_BITMAP;
d->bitmap.flags = 0;
d->bitmap.firstc = host2uint32_t_le(cfg->bitmap_cluster);
d->bitmap.size = host2uint64_t_le(cfg->bitmap_size);
d++;
d->type = EXFAT_TYPE_UCTABLE;
d->uctable.checksum = host2uint32_t_le(upcase_table_checksum(
upcase_table, sizeof(upcase_table)));
d->uctable.firstc = host2uint32_t_le(cfg->upcase_table_cluster);
d->uctable.size = host2uint64_t_le(sizeof(upcase_table));
rootdir_sec = cfg->data_start_sector;
rootdir_sec += ((cfg->rootdir_cluster - 2) * cfg->cluster_size) /
cfg->sector_size;
rc = block_write_direct(service_id, rootdir_sec, 1, data);
if (rc != EOK)
goto exit;
memset(data, 0, cfg->sector_size);
for (i = 1; i < cfg->cluster_size / cfg->sector_size; ++i) {
rc = block_write_direct(service_id, rootdir_sec + i, 1, data);
if (rc != EOK)
goto exit;
}
exit:
free(data);
return rc;
}
static unsigned
log2i(unsigned n)
{
unsigned r;
r = 0;
while (n >> r != 1)
++r;
return r;
}
static uint32_t
vbr_checksum_start(void const *data, size_t nbytes)
{
uint32_t checksum = 0;
size_t index;
uint8_t const *octets = (uint8_t *) data;
for (index = 0; index < nbytes; ++index) {
if (index == 106 || index == 107 || index == 112) {
continue;
}
checksum = ((checksum << 31) | (checksum >> 1)) +
octets[index];
}
return checksum;
}
static void
vbr_checksum_update(void const *data, size_t nbytes, uint32_t *checksum)
{
size_t index;
uint8_t const *octets = (uint8_t *) data;
for (index = 0; index < nbytes; ++index) {
*checksum = ((*checksum << 31) | (*checksum >> 1)) +
octets[index];
}
}
static uint32_t
upcase_table_checksum(void const *data, size_t nbytes)
{
size_t index;
uint32_t chksum = 0;
uint8_t const *octets = (uint8_t *) data;
for (index = 0; index < nbytes; ++index)
chksum = ((chksum << 31) | (chksum >> 1)) + octets[index];
return chksum;
}
static bool
is_power_of_two(unsigned long n)
{
if (n == 0)
return false;
return (n & (n - 1)) == 0;
}
int main (int argc, char **argv)
{
exfat_cfg_t cfg;
uint32_t next_cls;
char *dev_path;
service_id_t service_id;
errno_t rc;
int c, opt_ind;
aoff64_t user_fs_size = 0;
if (argc < 2) {
printf(NAME ": Error, argument missing\n");
usage();
return 1;
}
cfg.cluster_size = 0;
cfg.label = NULL;
c = 0;
optind = 0;
opt_ind = 0;
while (c != -1) {
c = getopt_long(argc, argv, "hs:c:L:",
long_options, &opt_ind);
switch (c) {
case 'h':
usage();
return 0;
case 's':
user_fs_size = (aoff64_t) strtol(optarg, NULL, 10);
break;
case 'c':
cfg.cluster_size = strtol(optarg, NULL, 10) * 1024;
if (cfg.cluster_size < 4096) {
printf(NAME ": Error, cluster size can't"
" be less than 4096 byte.\n");
return 1;
} else if (cfg.cluster_size > 32 * 1024 * 1024) {
printf(NAME ": Error, cluster size can't"
" be greater than 32 Mb");
return 1;
}
if (!is_power_of_two(cfg.cluster_size)) {
printf(NAME ": Error, the size of the cluster"
" must be a power of two.\n");
return 1;
}
break;
case 'L':
cfg.label = optarg;
break;
}
}
argv += optind;
dev_path = *argv;
if (!dev_path) {
printf(NAME ": Error, you must specify a valid block"
" device.\n");
usage();
return 1;
}
printf("Device = %s\n", dev_path);
rc = loc_service_get_id(dev_path, &service_id, 0);
if (rc != EOK) {
printf(NAME ": Error resolving device `%s'.\n", dev_path);
return 2;
}
rc = block_init(service_id);
if (rc != EOK) {
printf(NAME ": Error initializing libblock.\n");
return 2;
}
rc = block_get_bsize(service_id, &cfg.sector_size);
if (rc != EOK) {
printf(NAME ": Error determining device sector size.\n");
return 2;
}
user_fs_size *= cfg.sector_size;
if (user_fs_size > 0 && user_fs_size < 1024 * 1024) {
printf(NAME ": Error, fs size can't be less"
" than 1 Mb.\n");
return 1;
}
if (cfg.sector_size > 4096) {
printf(NAME ": Error, sector size can't be greater"
" than 4096 bytes.\n");
return 2;
}
rc = block_get_nblocks(service_id, &cfg.volume_count);
if (rc != EOK) {
printf(NAME ": Warning, failed to obtain"
" block device size.\n");
if (user_fs_size == 0) {
printf(NAME ": You must specify the"
" filesystem size.\n");
return 1;
}
} else {
printf("Block device has %" PRIuOFF64 " blocks.\n",
cfg.volume_count);
}
if (user_fs_size != 0) {
if (user_fs_size > cfg.volume_count * cfg.sector_size) {
printf(NAME ": Error, the device is not big enough"
" to create a filesystem of"
" the specified size.\n");
return 1;
}
cfg.volume_count = user_fs_size / cfg.sector_size;
}
cfg_params_initialize(&cfg);
cfg_print_info(&cfg);
if (cfg.total_clusters <= cfg.allocated_clusters + 2) {
printf(NAME ": Error, insufficient disk space on device.\n");
return 2;
}
printf("Writing the allocation table.\n");
rc = fat_initialize(service_id, &cfg);
if (rc != EOK) {
printf(NAME ": Error, failed to write the FAT to disk\n");
return 2;
}
rc = fat_allocate_clusters(service_id, &cfg, cfg.bitmap_cluster,
div_round_up(cfg.bitmap_size, cfg.cluster_size));
if (rc != EOK) {
printf(NAME ": Error, failed to allocate"
" clusters for bitmap.\n");
return 2;
}
next_cls = cfg.bitmap_cluster +
div_round_up(cfg.bitmap_size, cfg.cluster_size);
cfg.upcase_table_cluster = next_cls;
rc = fat_allocate_clusters(service_id, &cfg, next_cls,
div_round_up(sizeof(upcase_table), cfg.cluster_size));
if (rc != EOK) {
printf(NAME ":Error, failed to allocate clusters"
" for the upcase table.\n");
return 2;
}
next_cls += div_round_up(sizeof(upcase_table), cfg.cluster_size);
cfg.rootdir_cluster = next_cls;
rc = fat_allocate_clusters(service_id, &cfg, next_cls, 1);
if (rc != EOK) {
printf(NAME ": Error, failed to allocate cluster"
" for the root dentry.\n");
return 2;
}
printf("Writing the allocation bitmap.\n");
rc = bitmap_write(service_id, &cfg);
if (rc != EOK) {
printf(NAME ": Error, failed to write the allocation"
" bitmap to disk.\n");
return 2;
}
printf("Writing the upcase table.\n");
rc = upcase_table_write(service_id, &cfg);
if (rc != EOK) {
printf(NAME ": Error, failed to write the"
" upcase table to disk.\n");
return 2;
}
printf("Writing the root directory.\n");
rc = root_dentries_write(service_id, &cfg);
if (rc != EOK) {
printf(NAME ": Error, failed to write the root directory"
" entries to disk.\n");
return 2;
}
printf("Writing the boot sectors.\n");
rc = bootsec_write(service_id, &cfg);
if (rc != EOK) {
printf(NAME ": Error, failed to write the VBR to disk\n");
return 2;
}
printf("Success.\n");
return 0;
}
HelenOS homepage, sources at GitHub