| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Maurizio Lombardi
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup fs
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file mkexfat.c
|
|---|
| 35 | * @brief Tool for creating new exFAT file systems.
|
|---|
| 36 | *
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <stdio.h>
|
|---|
| 40 | #include <stdbool.h>
|
|---|
| 41 | #include <stdint.h>
|
|---|
| 42 | #include <block.h>
|
|---|
| 43 | #include <assert.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <stdlib.h>
|
|---|
| 46 | #include <byteorder.h>
|
|---|
| 47 | #include <align.h>
|
|---|
| 48 | #include <str.h>
|
|---|
| 49 | #include <getopt.h>
|
|---|
| 50 | #include <macros.h>
|
|---|
| 51 | #include "exfat.h"
|
|---|
| 52 | #include "upcase.h"
|
|---|
| 53 |
|
|---|
| 54 | #define NAME "mkexfat"
|
|---|
| 55 |
|
|---|
| 56 | /** First sector of the FAT */
|
|---|
| 57 | #define FAT_SECTOR_START 128
|
|---|
| 58 |
|
|---|
| 59 | /** First sector of the Main Extended Boot Region */
|
|---|
| 60 | #define EBS_SECTOR_START 1
|
|---|
| 61 |
|
|---|
| 62 | /** First sector of the Main Extended Boot Region Backup */
|
|---|
| 63 | #define EBS_BACKUP_SECTOR_START 13
|
|---|
| 64 |
|
|---|
| 65 | /** First sector of the Main Boot Sector */
|
|---|
| 66 | #define MBS_SECTOR 0
|
|---|
| 67 |
|
|---|
| 68 | /** First sector of the Main Boot Sector Backup */
|
|---|
| 69 | #define MBS_BACKUP_SECTOR 12
|
|---|
| 70 |
|
|---|
| 71 | /** VBR Checksum sector */
|
|---|
| 72 | #define VBR_CHECKSUM_SECTOR 11
|
|---|
| 73 |
|
|---|
| 74 | /** VBR Backup Checksum sector */
|
|---|
| 75 | #define VBR_BACKUP_CHECKSUM_SECTOR 23
|
|---|
| 76 |
|
|---|
| 77 | /** Size of the Main Extended Boot Region */
|
|---|
| 78 | #define EBS_SIZE 8
|
|---|
| 79 |
|
|---|
| 80 | /** Divide and round up. */
|
|---|
| 81 | #define div_round_up(a, b) (((a) + (b) - 1) / (b))
|
|---|
| 82 |
|
|---|
| 83 | /** The default size of each cluster is 4096 byte */
|
|---|
| 84 | #define DEFAULT_CLUSTER_SIZE 4096
|
|---|
| 85 |
|
|---|
| 86 | /** Index of the first free cluster on the device */
|
|---|
| 87 | #define FIRST_FREE_CLUSTER 2
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | typedef struct exfat_cfg {
|
|---|
| 91 | aoff64_t volume_start;
|
|---|
| 92 | aoff64_t volume_count;
|
|---|
| 93 | unsigned long fat_sector_count;
|
|---|
| 94 | unsigned long data_start_sector;
|
|---|
| 95 | unsigned long rootdir_cluster;
|
|---|
| 96 | unsigned long upcase_table_cluster;
|
|---|
| 97 | unsigned long bitmap_cluster;
|
|---|
| 98 | unsigned long total_clusters;
|
|---|
| 99 | unsigned long allocated_clusters;
|
|---|
| 100 | size_t bitmap_size;
|
|---|
| 101 | size_t sector_size;
|
|---|
| 102 | size_t cluster_size;
|
|---|
| 103 | const char *label;
|
|---|
| 104 | } exfat_cfg_t;
|
|---|
| 105 |
|
|---|
| 106 | static unsigned log2i(unsigned n);
|
|---|
| 107 |
|
|---|
| 108 | static uint32_t
|
|---|
| 109 | vbr_checksum_start(void const *octets, size_t nbytes);
|
|---|
| 110 |
|
|---|
| 111 | static void
|
|---|
| 112 | vbr_checksum_update(void const *octets, size_t nbytes, uint32_t *checksum);
|
|---|
| 113 |
|
|---|
| 114 | static errno_t
|
|---|
| 115 | ebs_write(service_id_t service_id, exfat_cfg_t *cfg,
|
|---|
| 116 | int base, uint32_t *chksum);
|
|---|
| 117 |
|
|---|
| 118 | static errno_t
|
|---|
| 119 | bitmap_write(service_id_t service_id, exfat_cfg_t *cfg);
|
|---|
| 120 |
|
|---|
| 121 | static uint32_t
|
|---|
| 122 | upcase_table_checksum(void const *data, size_t nbytes);
|
|---|
| 123 |
|
|---|
| 124 | static struct option const long_options[] = {
|
|---|
| 125 | { "help", no_argument, 0, 'h' },
|
|---|
| 126 | { "cluster-size", required_argument, 0, 'c' },
|
|---|
| 127 | { "fs-size", required_argument, 0, 's' },
|
|---|
| 128 | { "label", required_argument, 0, 'L' },
|
|---|
| 129 | };
|
|---|
| 130 |
|
|---|
| 131 | static void usage(void)
|
|---|
| 132 | {
|
|---|
| 133 | printf("Usage: mkexfat [options] <device>\n"
|
|---|
| 134 | "-c, --cluster-size ## Specify the cluster size (Kb)\n"
|
|---|
| 135 | "-s, --fs-size ## Specify the filesystem size (sectors)\n"
|
|---|
| 136 | " --label ## Volume label\n");
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /** Initialize the exFAT params structure.
|
|---|
| 140 | *
|
|---|
| 141 | * @param cfg Pointer to the exFAT params structure to initialize.
|
|---|
| 142 | */
|
|---|
| 143 | static void
|
|---|
| 144 | cfg_params_initialize(exfat_cfg_t *cfg)
|
|---|
| 145 | {
|
|---|
| 146 | unsigned long fat_bytes;
|
|---|
| 147 | unsigned long fat_cls;
|
|---|
| 148 | aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
|
|---|
| 149 | cfg->sector_size;
|
|---|
| 150 |
|
|---|
| 151 | if (cfg->cluster_size != 0) {
|
|---|
| 152 | /* The user already choose the cluster size he wants */
|
|---|
| 153 | cfg->total_clusters = volume_bytes / cfg->cluster_size;
|
|---|
| 154 | goto skip_cluster_size_set;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | cfg->total_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
|
|---|
| 158 | cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
|
|---|
| 159 |
|
|---|
| 160 | /*
|
|---|
| 161 | * Compute the required cluster size to index
|
|---|
| 162 | * the entire storage device and to keep the FAT
|
|---|
| 163 | * size less or equal to 64 Mb.
|
|---|
| 164 | */
|
|---|
| 165 | while (cfg->total_clusters > 16000000ULL &&
|
|---|
| 166 | (cfg->cluster_size < 32 * 1024 * 1024)) {
|
|---|
| 167 |
|
|---|
| 168 | cfg->cluster_size <<= 1;
|
|---|
| 169 | cfg->total_clusters = volume_bytes / cfg->cluster_size;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | skip_cluster_size_set:
|
|---|
| 173 |
|
|---|
| 174 | /* Compute the FAT size in sectors */
|
|---|
| 175 | fat_bytes = (cfg->total_clusters + 3) * sizeof(uint32_t);
|
|---|
| 176 | cfg->fat_sector_count = div_round_up(fat_bytes, cfg->sector_size);
|
|---|
| 177 |
|
|---|
| 178 | /* Compute the number of the first data sector */
|
|---|
| 179 | cfg->data_start_sector = ROUND_UP(FAT_SECTOR_START +
|
|---|
| 180 | cfg->fat_sector_count, cfg->cluster_size / cfg->sector_size);
|
|---|
| 181 |
|
|---|
| 182 | /*
|
|---|
| 183 | * Subtract the FAT space from the total
|
|---|
| 184 | * number of available clusters.
|
|---|
| 185 | */
|
|---|
| 186 | fat_cls = div_round_up((cfg->data_start_sector -
|
|---|
| 187 | FAT_SECTOR_START) * cfg->sector_size,
|
|---|
| 188 | cfg->cluster_size);
|
|---|
| 189 | if (fat_cls >= cfg->total_clusters) {
|
|---|
| 190 | /* Insufficient disk space on device */
|
|---|
| 191 | cfg->total_clusters = 0;
|
|---|
| 192 | return;
|
|---|
| 193 | }
|
|---|
| 194 | cfg->total_clusters -= fat_cls;
|
|---|
| 195 |
|
|---|
| 196 | /* Compute the bitmap size */
|
|---|
| 197 | cfg->bitmap_size = div_round_up(cfg->total_clusters, 8);
|
|---|
| 198 |
|
|---|
| 199 | /* Compute the number of clusters reserved to the bitmap */
|
|---|
| 200 | cfg->allocated_clusters = div_round_up(cfg->bitmap_size,
|
|---|
| 201 | cfg->cluster_size);
|
|---|
| 202 |
|
|---|
| 203 | /* This account for the root directory */
|
|---|
| 204 | cfg->allocated_clusters++;
|
|---|
| 205 |
|
|---|
| 206 | /* Compute the number of clusters reserved to the upcase table */
|
|---|
| 207 | cfg->allocated_clusters += div_round_up(sizeof(upcase_table),
|
|---|
| 208 | cfg->cluster_size);
|
|---|
| 209 |
|
|---|
| 210 | /* Will be set later */
|
|---|
| 211 | cfg->rootdir_cluster = 0;
|
|---|
| 212 |
|
|---|
| 213 | /* Bitmap always starts at the first free cluster */
|
|---|
| 214 | cfg->bitmap_cluster = FIRST_FREE_CLUSTER;
|
|---|
| 215 |
|
|---|
| 216 | /* The first sector of the partition is zero */
|
|---|
| 217 | cfg->volume_start = 0;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /** Prints the exFAT structure values
|
|---|
| 221 | *
|
|---|
| 222 | * @param cfg Pointer to the exfat_cfg_t structure.
|
|---|
| 223 | */
|
|---|
| 224 | static void
|
|---|
| 225 | cfg_print_info(exfat_cfg_t *cfg)
|
|---|
| 226 | {
|
|---|
| 227 | printf("Sector size: %lu\n",
|
|---|
| 228 | (unsigned long) cfg->sector_size);
|
|---|
| 229 | printf("Cluster size: %lu\n",
|
|---|
| 230 | (unsigned long) cfg->cluster_size);
|
|---|
| 231 | printf("FAT size in sectors: %lu\n", cfg->fat_sector_count);
|
|---|
| 232 | printf("Data start sector: %lu\n", cfg->data_start_sector);
|
|---|
| 233 | printf("Total num of clusters: %lu\n", cfg->total_clusters);
|
|---|
| 234 | printf("Total used clusters: %lu\n", cfg->allocated_clusters);
|
|---|
| 235 | printf("Bitmap size: %lu\n", (unsigned long)
|
|---|
| 236 | div_round_up(cfg->bitmap_size, cfg->cluster_size));
|
|---|
| 237 | printf("Upcase table size: %lu\n", (unsigned long)
|
|---|
| 238 | div_round_up(sizeof(upcase_table), cfg->cluster_size));
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | /** Initialize the Main Boot Sector fields.
|
|---|
| 242 | *
|
|---|
| 243 | * @param mbs Pointer to the Main Boot Sector structure.
|
|---|
| 244 | * @param cfg Pointer to the exFAT configuration structure.
|
|---|
| 245 | * @return Initial checksum value.
|
|---|
| 246 | */
|
|---|
| 247 | static uint32_t
|
|---|
| 248 | vbr_initialize(exfat_bs_t *mbs, exfat_cfg_t *cfg)
|
|---|
| 249 | {
|
|---|
| 250 | /* Fill the structure with zeroes */
|
|---|
| 251 | memset(mbs, 0, sizeof(exfat_bs_t));
|
|---|
| 252 |
|
|---|
| 253 | /* Init Jump Boot section */
|
|---|
| 254 | mbs->jump[0] = 0xEB;
|
|---|
| 255 | mbs->jump[1] = 0x76;
|
|---|
| 256 | mbs->jump[2] = 0x90;
|
|---|
| 257 |
|
|---|
| 258 | /* Set the filesystem name */
|
|---|
| 259 | memcpy(mbs->oem_name, "EXFAT ", sizeof(mbs->oem_name));
|
|---|
| 260 |
|
|---|
| 261 | mbs->volume_start = host2uint64_t_le(cfg->volume_start);
|
|---|
| 262 | mbs->volume_count = host2uint64_t_le(cfg->volume_count);
|
|---|
| 263 | mbs->fat_sector_start = host2uint32_t_le(FAT_SECTOR_START);
|
|---|
| 264 | mbs->fat_sector_count = host2uint32_t_le(cfg->fat_sector_count);
|
|---|
| 265 | mbs->data_start_sector = host2uint32_t_le(cfg->data_start_sector);
|
|---|
| 266 |
|
|---|
| 267 | mbs->data_clusters = host2uint32_t_le(cfg->total_clusters);
|
|---|
| 268 |
|
|---|
| 269 | mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
|
|---|
| 270 | mbs->volume_serial = host2uint32_t_le(0xe1028172);
|
|---|
| 271 | mbs->version.major = 1;
|
|---|
| 272 | mbs->version.minor = 0;
|
|---|
| 273 | mbs->volume_flags = host2uint16_t_le(0);
|
|---|
| 274 | mbs->bytes_per_sector = log2i(cfg->sector_size);
|
|---|
| 275 | mbs->sec_per_cluster = log2i(cfg->cluster_size / cfg->sector_size);
|
|---|
| 276 |
|
|---|
| 277 | /* Maximum cluster size is 32 Mb */
|
|---|
| 278 | assert((mbs->bytes_per_sector + mbs->sec_per_cluster) <= 25);
|
|---|
| 279 |
|
|---|
| 280 | mbs->fat_count = 1;
|
|---|
| 281 | mbs->drive_no = 0x80;
|
|---|
| 282 | mbs->allocated_percent = 0;
|
|---|
| 283 | mbs->signature = host2uint16_t_le(0xAA55);
|
|---|
| 284 |
|
|---|
| 285 | return vbr_checksum_start(mbs, sizeof(exfat_bs_t));
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | static errno_t
|
|---|
| 289 | bootsec_write(service_id_t service_id, exfat_cfg_t *cfg)
|
|---|
| 290 | {
|
|---|
| 291 | exfat_bs_t mbs;
|
|---|
| 292 | uint32_t vbr_checksum;
|
|---|
| 293 | uint32_t *chksum_sector;
|
|---|
| 294 | errno_t rc;
|
|---|
| 295 | unsigned idx;
|
|---|
| 296 |
|
|---|
| 297 | chksum_sector = calloc(cfg->sector_size, sizeof(uint8_t));
|
|---|
| 298 | if (!chksum_sector)
|
|---|
| 299 | return ENOMEM;
|
|---|
| 300 |
|
|---|
| 301 | vbr_checksum = vbr_initialize(&mbs, cfg);
|
|---|
| 302 |
|
|---|
| 303 | /* Write the Main Boot Sector to disk */
|
|---|
| 304 | rc = block_write_direct(service_id, MBS_SECTOR, 1, &mbs);
|
|---|
| 305 | if (rc != EOK)
|
|---|
| 306 | goto exit;
|
|---|
| 307 |
|
|---|
| 308 | /* Write the Main extended boot sectors to disk */
|
|---|
| 309 | rc = ebs_write(service_id, cfg, EBS_SECTOR_START, &vbr_checksum);
|
|---|
| 310 | if (rc != EOK)
|
|---|
| 311 | goto exit;
|
|---|
| 312 |
|
|---|
| 313 | /* Write the Main Boot Sector backup to disk */
|
|---|
| 314 | rc = block_write_direct(service_id, MBS_BACKUP_SECTOR, 1, &mbs);
|
|---|
| 315 | if (rc != EOK)
|
|---|
| 316 | goto exit;
|
|---|
| 317 |
|
|---|
| 318 | /* Initialize the checksum sectors */
|
|---|
| 319 | for (idx = 0; idx < cfg->sector_size / sizeof(uint32_t); ++idx)
|
|---|
| 320 | chksum_sector[idx] = host2uint32_t_le(vbr_checksum);
|
|---|
| 321 |
|
|---|
| 322 | /* Write the main checksum sector to disk */
|
|---|
| 323 | rc = block_write_direct(service_id,
|
|---|
| 324 | VBR_CHECKSUM_SECTOR, 1, chksum_sector);
|
|---|
| 325 | if (rc != EOK)
|
|---|
| 326 | goto exit;
|
|---|
| 327 |
|
|---|
| 328 | /* Write the backup checksum sector to disk */
|
|---|
| 329 | rc = block_write_direct(service_id,
|
|---|
| 330 | VBR_BACKUP_CHECKSUM_SECTOR, 1, chksum_sector);
|
|---|
| 331 | if (rc != EOK)
|
|---|
| 332 | goto exit;
|
|---|
| 333 |
|
|---|
| 334 | /* Write the Main extended boot sectors backup to disk */
|
|---|
| 335 | rc = ebs_write(service_id, cfg,
|
|---|
| 336 | EBS_BACKUP_SECTOR_START, &vbr_checksum);
|
|---|
| 337 |
|
|---|
| 338 | exit:
|
|---|
| 339 | free(chksum_sector);
|
|---|
| 340 | return rc;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | /** Write the Main Extended Boot Sector to disk
|
|---|
| 344 | *
|
|---|
| 345 | * @param service_id The service id.
|
|---|
| 346 | * @param cfg Pointer to the exFAT configuration structure.
|
|---|
| 347 | * @param base Base sector of the EBS.
|
|---|
| 348 | * @return EOK on success or an error code.
|
|---|
| 349 | */
|
|---|
| 350 | static errno_t
|
|---|
| 351 | ebs_write(service_id_t service_id, exfat_cfg_t *cfg, int base,
|
|---|
| 352 | uint32_t *chksum)
|
|---|
| 353 | {
|
|---|
| 354 | uint32_t *ebs = calloc(cfg->sector_size, sizeof(uint8_t));
|
|---|
| 355 | int i;
|
|---|
| 356 | errno_t rc;
|
|---|
| 357 |
|
|---|
| 358 | if (!ebs)
|
|---|
| 359 | return ENOMEM;
|
|---|
| 360 |
|
|---|
| 361 | ebs[cfg->sector_size / 4 - 1] = host2uint32_t_le(0xAA550000);
|
|---|
| 362 |
|
|---|
| 363 | for (i = 0; i < EBS_SIZE; ++i) {
|
|---|
| 364 | vbr_checksum_update(ebs, cfg->sector_size, chksum);
|
|---|
| 365 |
|
|---|
| 366 | rc = block_write_direct(service_id,
|
|---|
| 367 | i + base, 1, ebs);
|
|---|
| 368 |
|
|---|
| 369 | if (rc != EOK)
|
|---|
| 370 | goto exit;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | /*
|
|---|
| 374 | * The OEM record is not yet used
|
|---|
| 375 | * by the official exFAT implementation, we'll fill
|
|---|
| 376 | * it with zeroes.
|
|---|
| 377 | */
|
|---|
| 378 |
|
|---|
| 379 | memset(ebs, 0, cfg->sector_size);
|
|---|
| 380 | vbr_checksum_update(ebs, cfg->sector_size, chksum);
|
|---|
| 381 |
|
|---|
| 382 | rc = block_write_direct(service_id, i++ + base, 1, ebs);
|
|---|
| 383 | if (rc != EOK)
|
|---|
| 384 | goto exit;
|
|---|
| 385 |
|
|---|
| 386 | /* The next sector is reserved, fill it with zeroes too */
|
|---|
| 387 | vbr_checksum_update(ebs, cfg->sector_size, chksum);
|
|---|
| 388 |
|
|---|
| 389 | rc = block_write_direct(service_id, i + base, 1, ebs);
|
|---|
| 390 | if (rc != EOK)
|
|---|
| 391 | goto exit;
|
|---|
| 392 |
|
|---|
| 393 | exit:
|
|---|
| 394 | free(ebs);
|
|---|
| 395 | return rc;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | /** Initialize the FAT table.
|
|---|
| 399 | *
|
|---|
| 400 | * @param service_id The service id.
|
|---|
| 401 | * @param cfg Pointer to the exfat_cfg structure.
|
|---|
| 402 | * @return EOK on success or an error code.
|
|---|
| 403 | */
|
|---|
| 404 | static errno_t
|
|---|
| 405 | fat_initialize(service_id_t service_id, exfat_cfg_t *cfg)
|
|---|
| 406 | {
|
|---|
| 407 | unsigned long i;
|
|---|
| 408 | uint32_t *pfat;
|
|---|
| 409 | errno_t rc;
|
|---|
| 410 |
|
|---|
| 411 | pfat = calloc(cfg->sector_size, 1);
|
|---|
| 412 | if (!pfat)
|
|---|
| 413 | return ENOMEM;
|
|---|
| 414 |
|
|---|
| 415 | pfat[0] = host2uint32_t_le(0xFFFFFFF8);
|
|---|
| 416 | pfat[1] = host2uint32_t_le(0xFFFFFFFF);
|
|---|
| 417 |
|
|---|
| 418 | rc = block_write_direct(service_id, FAT_SECTOR_START, 1, pfat);
|
|---|
| 419 | if (rc != EOK)
|
|---|
| 420 | goto error;
|
|---|
| 421 |
|
|---|
| 422 | pfat[0] = pfat[1] = 0;
|
|---|
| 423 |
|
|---|
| 424 | for (i = 1; i < cfg->fat_sector_count; ++i) {
|
|---|
| 425 | rc = block_write_direct(service_id,
|
|---|
| 426 | FAT_SECTOR_START + i, 1, pfat);
|
|---|
| 427 | if (rc != EOK)
|
|---|
| 428 | goto error;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | error:
|
|---|
| 432 | free(pfat);
|
|---|
| 433 | return rc;
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | /** Allocate a given number of clusters and create a cluster chain.
|
|---|
| 437 | *
|
|---|
| 438 | * @param service_id The service id.
|
|---|
| 439 | * @param cfg Pointer to the exfat configuration structure.
|
|---|
| 440 | * @param cur_cls Cluster index from where to start the allocation.
|
|---|
| 441 | * @param ncls Number of clusters to allocate.
|
|---|
| 442 | * @return EOK on success or an error code.
|
|---|
| 443 | */
|
|---|
| 444 | static errno_t
|
|---|
| 445 | fat_allocate_clusters(service_id_t service_id, exfat_cfg_t *cfg,
|
|---|
| 446 | uint32_t cur_cls, unsigned long ncls)
|
|---|
| 447 | {
|
|---|
| 448 | errno_t rc;
|
|---|
| 449 | unsigned const fat_entries = cfg->sector_size / sizeof(uint32_t);
|
|---|
| 450 | aoff64_t fat_sec = cur_cls / fat_entries + FAT_SECTOR_START;
|
|---|
| 451 | uint32_t *fat;
|
|---|
| 452 | uint32_t next_cls = cur_cls;
|
|---|
| 453 |
|
|---|
| 454 | cur_cls %= fat_entries;
|
|---|
| 455 |
|
|---|
| 456 | fat = malloc(cfg->sector_size);
|
|---|
| 457 | if (!fat)
|
|---|
| 458 | return ENOMEM;
|
|---|
| 459 |
|
|---|
| 460 | loop:
|
|---|
| 461 | rc = block_read_direct(service_id, fat_sec, 1, fat);
|
|---|
| 462 | if (rc != EOK)
|
|---|
| 463 | goto exit;
|
|---|
| 464 |
|
|---|
| 465 | assert(fat[cur_cls] == 0);
|
|---|
| 466 | assert(ncls > 0);
|
|---|
| 467 |
|
|---|
| 468 | for (; cur_cls < fat_entries && ncls > 1; ++cur_cls, --ncls)
|
|---|
| 469 | fat[cur_cls] = host2uint32_t_le(++next_cls);
|
|---|
| 470 |
|
|---|
| 471 | if (cur_cls == fat_entries) {
|
|---|
| 472 | /*
|
|---|
| 473 | * This sector is full, there are no more free entries,
|
|---|
| 474 | * commit the changes to disk and restart from the next
|
|---|
| 475 | * sector.
|
|---|
| 476 | */
|
|---|
| 477 | rc = block_write_direct(service_id, fat_sec++, 1, fat);
|
|---|
| 478 | if (rc != EOK)
|
|---|
| 479 | goto exit;
|
|---|
| 480 | cur_cls = 0;
|
|---|
| 481 | goto loop;
|
|---|
| 482 | } else if (ncls == 1) {
|
|---|
| 483 | /*
|
|---|
| 484 | * This is the last cluster of this chain, mark it
|
|---|
| 485 | * with EOF.
|
|---|
| 486 | */
|
|---|
| 487 | fat[cur_cls] = host2uint32_t_le(0xFFFFFFFF);
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | rc = block_write_direct(service_id, fat_sec, 1, fat);
|
|---|
| 491 |
|
|---|
| 492 | exit:
|
|---|
| 493 | free(fat);
|
|---|
| 494 | return rc;
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | /** Initialize the allocation bitmap.
|
|---|
| 498 | *
|
|---|
| 499 | * @param service_id The service id.
|
|---|
| 500 | * @param cfg Pointer to the exfat configuration structure.
|
|---|
| 501 | * @return EOK on success or an error code.
|
|---|
| 502 | */
|
|---|
| 503 | static errno_t
|
|---|
| 504 | bitmap_write(service_id_t service_id, exfat_cfg_t *cfg)
|
|---|
| 505 | {
|
|---|
| 506 | unsigned long i, sec;
|
|---|
| 507 | unsigned long allocated_cls;
|
|---|
| 508 | errno_t rc = EOK;
|
|---|
| 509 | bool need_reset = true;
|
|---|
| 510 |
|
|---|
| 511 | /* Bitmap size in sectors */
|
|---|
| 512 | size_t const bss = div_round_up(cfg->bitmap_size, cfg->sector_size);
|
|---|
| 513 |
|
|---|
| 514 | uint8_t *bitmap = malloc(cfg->sector_size);
|
|---|
| 515 | if (!bitmap)
|
|---|
| 516 | return ENOMEM;
|
|---|
| 517 |
|
|---|
| 518 | allocated_cls = cfg->allocated_clusters;
|
|---|
| 519 |
|
|---|
| 520 | for (sec = 0; sec < bss; ++sec) {
|
|---|
| 521 | if (need_reset) {
|
|---|
| 522 | need_reset = false;
|
|---|
| 523 | memset(bitmap, 0, cfg->sector_size);
|
|---|
| 524 | }
|
|---|
| 525 | if (allocated_cls > 0) {
|
|---|
| 526 | for (i = 0; i < allocated_cls; ++i) {
|
|---|
| 527 | unsigned byte_idx = i / 8;
|
|---|
| 528 | unsigned bit_idx = i % 8;
|
|---|
| 529 |
|
|---|
| 530 | if (byte_idx == cfg->sector_size)
|
|---|
| 531 | break;
|
|---|
| 532 | bitmap[byte_idx] |= 1 << bit_idx;
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | allocated_cls -= i;
|
|---|
| 536 | need_reset = true;
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | rc = block_write_direct(service_id,
|
|---|
| 540 | cfg->data_start_sector + sec, 1, bitmap);
|
|---|
| 541 | if (rc != EOK)
|
|---|
| 542 | goto exit;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | exit:
|
|---|
| 546 | free(bitmap);
|
|---|
| 547 | return rc;
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | /** Write the upcase table to disk. */
|
|---|
| 551 | static errno_t
|
|---|
| 552 | upcase_table_write(service_id_t service_id, exfat_cfg_t *cfg)
|
|---|
| 553 | {
|
|---|
| 554 | errno_t rc = EOK;
|
|---|
| 555 | aoff64_t start_sec, nsecs, i;
|
|---|
| 556 | uint8_t *table_ptr;
|
|---|
| 557 | uint8_t *buf;
|
|---|
| 558 | size_t table_size = sizeof(upcase_table);
|
|---|
| 559 |
|
|---|
| 560 | buf = malloc(cfg->sector_size);
|
|---|
| 561 | if (!buf)
|
|---|
| 562 | return ENOENT;
|
|---|
| 563 |
|
|---|
| 564 | /* Compute the start sector of the upcase table */
|
|---|
| 565 | start_sec = cfg->data_start_sector;
|
|---|
| 566 | start_sec += ((cfg->upcase_table_cluster - 2) * cfg->cluster_size) /
|
|---|
| 567 | cfg->sector_size;
|
|---|
| 568 |
|
|---|
| 569 | /* Compute the number of sectors needed to store the table on disk */
|
|---|
| 570 | nsecs = div_round_up(sizeof(upcase_table), cfg->sector_size);
|
|---|
| 571 | table_ptr = (uint8_t *) upcase_table;
|
|---|
| 572 |
|
|---|
| 573 | for (i = 0; i < nsecs; ++i,
|
|---|
| 574 | table_ptr += min(table_size, cfg->sector_size),
|
|---|
| 575 | table_size -= cfg->sector_size) {
|
|---|
| 576 |
|
|---|
| 577 | if (table_size < cfg->sector_size) {
|
|---|
| 578 | /*
|
|---|
| 579 | * Reset the content of the unused part
|
|---|
| 580 | * of the last sector.
|
|---|
| 581 | */
|
|---|
| 582 | memset(buf, 0, cfg->sector_size);
|
|---|
| 583 | }
|
|---|
| 584 | memcpy(buf, table_ptr, min(table_size, cfg->sector_size));
|
|---|
| 585 |
|
|---|
| 586 | rc = block_write_direct(service_id,
|
|---|
| 587 | start_sec + i, 1, buf);
|
|---|
| 588 | if (rc != EOK)
|
|---|
| 589 | goto exit;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | exit:
|
|---|
| 593 | free(buf);
|
|---|
| 594 | return rc;
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | /** Initialize and write the root directory entries to disk.
|
|---|
| 598 | *
|
|---|
| 599 | * @param service_id The service id.
|
|---|
| 600 | * @param cfg Pointer to the exFAT configuration structure.
|
|---|
| 601 | * @return EOK on success or an error code.
|
|---|
| 602 | */
|
|---|
| 603 | static errno_t
|
|---|
| 604 | root_dentries_write(service_id_t service_id, exfat_cfg_t *cfg)
|
|---|
| 605 | {
|
|---|
| 606 | exfat_dentry_t *d;
|
|---|
| 607 | aoff64_t rootdir_sec;
|
|---|
| 608 | uint16_t wlabel[EXFAT_VOLLABEL_LEN + 1];
|
|---|
| 609 | errno_t rc;
|
|---|
| 610 | uint8_t *data;
|
|---|
| 611 | unsigned long i;
|
|---|
| 612 |
|
|---|
| 613 | data = calloc(cfg->sector_size, 1);
|
|---|
| 614 | if (!data)
|
|---|
| 615 | return ENOMEM;
|
|---|
| 616 |
|
|---|
| 617 | d = (exfat_dentry_t *) data;
|
|---|
| 618 |
|
|---|
| 619 | /* Initialize the volume label dentry */
|
|---|
| 620 |
|
|---|
| 621 | if (cfg->label != NULL) {
|
|---|
| 622 | memset(wlabel, 0, (EXFAT_VOLLABEL_LEN + 1) * sizeof(uint16_t));
|
|---|
| 623 | rc = str_to_utf16(wlabel, EXFAT_VOLLABEL_LEN + 1, cfg->label);
|
|---|
| 624 | if (rc != EOK) {
|
|---|
| 625 | rc = EINVAL;
|
|---|
| 626 | goto exit;
|
|---|
| 627 | }
|
|---|
| 628 |
|
|---|
| 629 | d->type = EXFAT_TYPE_VOLLABEL;
|
|---|
| 630 | memcpy(d->vollabel.label, wlabel, EXFAT_VOLLABEL_LEN * 2);
|
|---|
| 631 | d->vollabel.size = utf16_wsize(wlabel);
|
|---|
| 632 | assert(d->vollabel.size <= EXFAT_VOLLABEL_LEN);
|
|---|
| 633 |
|
|---|
| 634 | d++;
|
|---|
| 635 | } else {
|
|---|
| 636 | d->type = EXFAT_TYPE_VOLLABEL & ~EXFAT_TYPE_USED;
|
|---|
| 637 | }
|
|---|
| 638 |
|
|---|
| 639 | /* Initialize the allocation bitmap dentry */
|
|---|
| 640 | d->type = EXFAT_TYPE_BITMAP;
|
|---|
| 641 | d->bitmap.flags = 0; /* First FAT */
|
|---|
| 642 | d->bitmap.firstc = host2uint32_t_le(cfg->bitmap_cluster);
|
|---|
| 643 | d->bitmap.size = host2uint64_t_le(cfg->bitmap_size);
|
|---|
| 644 |
|
|---|
| 645 | d++;
|
|---|
| 646 |
|
|---|
| 647 | /* Initialize the upcase table dentry */
|
|---|
| 648 | d->type = EXFAT_TYPE_UCTABLE;
|
|---|
| 649 | d->uctable.checksum = host2uint32_t_le(upcase_table_checksum(
|
|---|
| 650 | upcase_table, sizeof(upcase_table)));
|
|---|
| 651 | d->uctable.firstc = host2uint32_t_le(cfg->upcase_table_cluster);
|
|---|
| 652 | d->uctable.size = host2uint64_t_le(sizeof(upcase_table));
|
|---|
| 653 |
|
|---|
| 654 | /* Compute the number of the sector where the rootdir resides */
|
|---|
| 655 |
|
|---|
| 656 | rootdir_sec = cfg->data_start_sector;
|
|---|
| 657 | rootdir_sec += ((cfg->rootdir_cluster - 2) * cfg->cluster_size) /
|
|---|
| 658 | cfg->sector_size;
|
|---|
| 659 |
|
|---|
| 660 | rc = block_write_direct(service_id, rootdir_sec, 1, data);
|
|---|
| 661 | if (rc != EOK)
|
|---|
| 662 | goto exit;
|
|---|
| 663 |
|
|---|
| 664 | /*
|
|---|
| 665 | * Fill the content of the sectors not used by the
|
|---|
| 666 | * root directory with zeroes.
|
|---|
| 667 | */
|
|---|
| 668 | memset(data, 0, cfg->sector_size);
|
|---|
| 669 | for (i = 1; i < cfg->cluster_size / cfg->sector_size; ++i) {
|
|---|
| 670 | rc = block_write_direct(service_id, rootdir_sec + i, 1, data);
|
|---|
| 671 | if (rc != EOK)
|
|---|
| 672 | goto exit;
|
|---|
| 673 | }
|
|---|
| 674 |
|
|---|
| 675 | exit:
|
|---|
| 676 | free(data);
|
|---|
| 677 | return rc;
|
|---|
| 678 | }
|
|---|
| 679 |
|
|---|
| 680 | /** Given a number (n), returns the result of log2(n).
|
|---|
| 681 | *
|
|---|
| 682 | * It works only if n is a power of two.
|
|---|
| 683 | */
|
|---|
| 684 | static unsigned
|
|---|
| 685 | log2i(unsigned n)
|
|---|
| 686 | {
|
|---|
| 687 | unsigned r;
|
|---|
| 688 |
|
|---|
| 689 | r = 0;
|
|---|
| 690 | while (n >> r != 1)
|
|---|
| 691 | ++r;
|
|---|
| 692 |
|
|---|
| 693 | return r;
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | /** Initialize the VBR checksum calculation */
|
|---|
| 697 | static uint32_t
|
|---|
| 698 | vbr_checksum_start(void const *data, size_t nbytes)
|
|---|
| 699 | {
|
|---|
| 700 | uint32_t checksum = 0;
|
|---|
| 701 | size_t index;
|
|---|
| 702 | uint8_t const *octets = (uint8_t *) data;
|
|---|
| 703 |
|
|---|
| 704 | for (index = 0; index < nbytes; ++index) {
|
|---|
| 705 | if (index == 106 || index == 107 || index == 112) {
|
|---|
| 706 | /* Skip volume_flags and allocated_percent fields */
|
|---|
| 707 | continue;
|
|---|
| 708 | }
|
|---|
| 709 |
|
|---|
| 710 | checksum = ((checksum << 31) | (checksum >> 1)) +
|
|---|
| 711 | octets[index];
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 | return checksum;
|
|---|
| 715 | }
|
|---|
| 716 |
|
|---|
| 717 | /** Update the VBR checksum */
|
|---|
| 718 | static void
|
|---|
| 719 | vbr_checksum_update(void const *data, size_t nbytes, uint32_t *checksum)
|
|---|
| 720 | {
|
|---|
| 721 | size_t index;
|
|---|
| 722 | uint8_t const *octets = (uint8_t *) data;
|
|---|
| 723 |
|
|---|
| 724 | for (index = 0; index < nbytes; ++index) {
|
|---|
| 725 | *checksum = ((*checksum << 31) | (*checksum >> 1)) +
|
|---|
| 726 | octets[index];
|
|---|
| 727 | }
|
|---|
| 728 | }
|
|---|
| 729 |
|
|---|
| 730 | /** Compute the checksum of the upcase table.
|
|---|
| 731 | *
|
|---|
| 732 | * @param data Pointer to the upcase table.
|
|---|
| 733 | * @param nbytes size of the upcase table in bytes.
|
|---|
| 734 | * @return Checksum value.
|
|---|
| 735 | */
|
|---|
| 736 | static uint32_t
|
|---|
| 737 | upcase_table_checksum(void const *data, size_t nbytes)
|
|---|
| 738 | {
|
|---|
| 739 | size_t index;
|
|---|
| 740 | uint32_t chksum = 0;
|
|---|
| 741 | uint8_t const *octets = (uint8_t *) data;
|
|---|
| 742 |
|
|---|
| 743 | for (index = 0; index < nbytes; ++index)
|
|---|
| 744 | chksum = ((chksum << 31) | (chksum >> 1)) + octets[index];
|
|---|
| 745 |
|
|---|
| 746 | return chksum;
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | /** Check if a given number is a power of two.
|
|---|
| 750 | *
|
|---|
| 751 | * @param n The number to check.
|
|---|
| 752 | * @return true if n is a power of two, false otherwise.
|
|---|
| 753 | */
|
|---|
| 754 | static bool
|
|---|
| 755 | is_power_of_two(unsigned long n)
|
|---|
| 756 | {
|
|---|
| 757 | if (n == 0)
|
|---|
| 758 | return false;
|
|---|
| 759 |
|
|---|
| 760 | return (n & (n - 1)) == 0;
|
|---|
| 761 | }
|
|---|
| 762 |
|
|---|
| 763 | int main (int argc, char **argv)
|
|---|
| 764 | {
|
|---|
| 765 | exfat_cfg_t cfg;
|
|---|
| 766 | uint32_t next_cls;
|
|---|
| 767 | char *dev_path;
|
|---|
| 768 | service_id_t service_id;
|
|---|
| 769 | errno_t rc;
|
|---|
| 770 | int c, opt_ind;
|
|---|
| 771 | aoff64_t user_fs_size = 0;
|
|---|
| 772 |
|
|---|
| 773 | if (argc < 2) {
|
|---|
| 774 | printf(NAME ": Error, argument missing\n");
|
|---|
| 775 | usage();
|
|---|
| 776 | return 1;
|
|---|
| 777 | }
|
|---|
| 778 |
|
|---|
| 779 | cfg.cluster_size = 0;
|
|---|
| 780 | cfg.label = NULL;
|
|---|
| 781 |
|
|---|
| 782 | c = 0;
|
|---|
| 783 | optind = 0;
|
|---|
| 784 | opt_ind = 0;
|
|---|
| 785 | while (c != -1) {
|
|---|
| 786 | c = getopt_long(argc, argv, "hs:c:L:",
|
|---|
| 787 | long_options, &opt_ind);
|
|---|
| 788 | switch (c) {
|
|---|
| 789 | case 'h':
|
|---|
| 790 | usage();
|
|---|
| 791 | return 0;
|
|---|
| 792 | case 's':
|
|---|
| 793 | user_fs_size = (aoff64_t) strtol(optarg, NULL, 10);
|
|---|
| 794 | break;
|
|---|
| 795 |
|
|---|
| 796 | case 'c':
|
|---|
| 797 | cfg.cluster_size = strtol(optarg, NULL, 10) * 1024;
|
|---|
| 798 | if (cfg.cluster_size < 4096) {
|
|---|
| 799 | printf(NAME ": Error, cluster size can't"
|
|---|
| 800 | " be less than 4096 byte.\n");
|
|---|
| 801 | return 1;
|
|---|
| 802 | } else if (cfg.cluster_size > 32 * 1024 * 1024) {
|
|---|
| 803 | printf(NAME ": Error, cluster size can't"
|
|---|
| 804 | " be greater than 32 Mb");
|
|---|
| 805 | return 1;
|
|---|
| 806 | }
|
|---|
| 807 |
|
|---|
| 808 | if (!is_power_of_two(cfg.cluster_size)) {
|
|---|
| 809 | printf(NAME ": Error, the size of the cluster"
|
|---|
| 810 | " must be a power of two.\n");
|
|---|
| 811 | return 1;
|
|---|
| 812 | }
|
|---|
| 813 | break;
|
|---|
| 814 | case 'L':
|
|---|
| 815 | cfg.label = optarg;
|
|---|
| 816 | break;
|
|---|
| 817 | }
|
|---|
| 818 | }
|
|---|
| 819 |
|
|---|
| 820 | argv += optind;
|
|---|
| 821 | dev_path = *argv;
|
|---|
| 822 |
|
|---|
| 823 | if (!dev_path) {
|
|---|
| 824 | printf(NAME ": Error, you must specify a valid block"
|
|---|
| 825 | " device.\n");
|
|---|
| 826 | usage();
|
|---|
| 827 | return 1;
|
|---|
| 828 | }
|
|---|
| 829 |
|
|---|
| 830 | printf("Device = %s\n", dev_path);
|
|---|
| 831 |
|
|---|
| 832 | rc = loc_service_get_id(dev_path, &service_id, 0);
|
|---|
| 833 | if (rc != EOK) {
|
|---|
| 834 | printf(NAME ": Error resolving device `%s'.\n", dev_path);
|
|---|
| 835 | return 2;
|
|---|
| 836 | }
|
|---|
| 837 |
|
|---|
| 838 | rc = block_init(service_id, 2048);
|
|---|
| 839 | if (rc != EOK) {
|
|---|
| 840 | printf(NAME ": Error initializing libblock.\n");
|
|---|
| 841 | return 2;
|
|---|
| 842 | }
|
|---|
| 843 |
|
|---|
| 844 | rc = block_get_bsize(service_id, &cfg.sector_size);
|
|---|
| 845 | if (rc != EOK) {
|
|---|
| 846 | printf(NAME ": Error determining device sector size.\n");
|
|---|
| 847 | return 2;
|
|---|
| 848 | }
|
|---|
| 849 |
|
|---|
| 850 | user_fs_size *= cfg.sector_size;
|
|---|
| 851 | if (user_fs_size > 0 && user_fs_size < 1024 * 1024) {
|
|---|
| 852 | printf(NAME ": Error, fs size can't be less"
|
|---|
| 853 | " than 1 Mb.\n");
|
|---|
| 854 | return 1;
|
|---|
| 855 | }
|
|---|
| 856 |
|
|---|
| 857 |
|
|---|
| 858 | if (cfg.sector_size > 4096) {
|
|---|
| 859 | printf(NAME ": Error, sector size can't be greater"
|
|---|
| 860 | " than 4096 bytes.\n");
|
|---|
| 861 | return 2;
|
|---|
| 862 | }
|
|---|
| 863 |
|
|---|
| 864 | rc = block_get_nblocks(service_id, &cfg.volume_count);
|
|---|
| 865 | if (rc != EOK) {
|
|---|
| 866 | printf(NAME ": Warning, failed to obtain"
|
|---|
| 867 | " block device size.\n");
|
|---|
| 868 |
|
|---|
| 869 | if (user_fs_size == 0) {
|
|---|
| 870 | printf(NAME ": You must specify the"
|
|---|
| 871 | " filesystem size.\n");
|
|---|
| 872 | return 1;
|
|---|
| 873 | }
|
|---|
| 874 | } else {
|
|---|
| 875 | printf("Block device has %" PRIuOFF64 " blocks.\n",
|
|---|
| 876 | cfg.volume_count);
|
|---|
| 877 | }
|
|---|
| 878 |
|
|---|
| 879 | if (user_fs_size != 0) {
|
|---|
| 880 | if (user_fs_size > cfg.volume_count * cfg.sector_size) {
|
|---|
| 881 | printf(NAME ": Error, the device is not big enough"
|
|---|
| 882 | " to create a filesystem of"
|
|---|
| 883 | " the specified size.\n");
|
|---|
| 884 | return 1;
|
|---|
| 885 | }
|
|---|
| 886 |
|
|---|
| 887 | cfg.volume_count = user_fs_size / cfg.sector_size;
|
|---|
| 888 | }
|
|---|
| 889 |
|
|---|
| 890 | cfg_params_initialize(&cfg);
|
|---|
| 891 | cfg_print_info(&cfg);
|
|---|
| 892 |
|
|---|
| 893 | if (cfg.total_clusters <= cfg.allocated_clusters + 2) {
|
|---|
| 894 | printf(NAME ": Error, insufficient disk space on device.\n");
|
|---|
| 895 | return 2;
|
|---|
| 896 | }
|
|---|
| 897 |
|
|---|
| 898 | printf("Writing the allocation table.\n");
|
|---|
| 899 |
|
|---|
| 900 | /* Initialize the FAT table */
|
|---|
| 901 | rc = fat_initialize(service_id, &cfg);
|
|---|
| 902 | if (rc != EOK) {
|
|---|
| 903 | printf(NAME ": Error, failed to write the FAT to disk\n");
|
|---|
| 904 | return 2;
|
|---|
| 905 | }
|
|---|
| 906 |
|
|---|
| 907 | /* Allocate clusters for the bitmap */
|
|---|
| 908 | rc = fat_allocate_clusters(service_id, &cfg, cfg.bitmap_cluster,
|
|---|
| 909 | div_round_up(cfg.bitmap_size, cfg.cluster_size));
|
|---|
| 910 | if (rc != EOK) {
|
|---|
| 911 | printf(NAME ": Error, failed to allocate"
|
|---|
| 912 | " clusters for bitmap.\n");
|
|---|
| 913 | return 2;
|
|---|
| 914 | }
|
|---|
| 915 |
|
|---|
| 916 | next_cls = cfg.bitmap_cluster +
|
|---|
| 917 | div_round_up(cfg.bitmap_size, cfg.cluster_size);
|
|---|
| 918 | cfg.upcase_table_cluster = next_cls;
|
|---|
| 919 |
|
|---|
| 920 | /* Allocate clusters for the upcase table */
|
|---|
| 921 | rc = fat_allocate_clusters(service_id, &cfg, next_cls,
|
|---|
| 922 | div_round_up(sizeof(upcase_table), cfg.cluster_size));
|
|---|
| 923 | if (rc != EOK) {
|
|---|
| 924 | printf(NAME ":Error, failed to allocate clusters"
|
|---|
| 925 | " for the upcase table.\n");
|
|---|
| 926 | return 2;
|
|---|
| 927 | }
|
|---|
| 928 |
|
|---|
| 929 | next_cls += div_round_up(sizeof(upcase_table), cfg.cluster_size);
|
|---|
| 930 | cfg.rootdir_cluster = next_cls;
|
|---|
| 931 |
|
|---|
| 932 | /* Allocate a cluster for the root directory entry */
|
|---|
| 933 | rc = fat_allocate_clusters(service_id, &cfg, next_cls, 1);
|
|---|
| 934 | if (rc != EOK) {
|
|---|
| 935 | printf(NAME ": Error, failed to allocate cluster"
|
|---|
| 936 | " for the root dentry.\n");
|
|---|
| 937 | return 2;
|
|---|
| 938 | }
|
|---|
| 939 |
|
|---|
| 940 | printf("Writing the allocation bitmap.\n");
|
|---|
| 941 |
|
|---|
| 942 | /* Write the allocation bitmap to disk */
|
|---|
| 943 | rc = bitmap_write(service_id, &cfg);
|
|---|
| 944 | if (rc != EOK) {
|
|---|
| 945 | printf(NAME ": Error, failed to write the allocation"
|
|---|
| 946 | " bitmap to disk.\n");
|
|---|
| 947 | return 2;
|
|---|
| 948 | }
|
|---|
| 949 |
|
|---|
| 950 | printf("Writing the upcase table.\n");
|
|---|
| 951 |
|
|---|
| 952 | /* Write the upcase table to disk */
|
|---|
| 953 | rc = upcase_table_write(service_id, &cfg);
|
|---|
| 954 | if (rc != EOK) {
|
|---|
| 955 | printf(NAME ": Error, failed to write the"
|
|---|
| 956 | " upcase table to disk.\n");
|
|---|
| 957 | return 2;
|
|---|
| 958 | }
|
|---|
| 959 |
|
|---|
| 960 | printf("Writing the root directory.\n");
|
|---|
| 961 |
|
|---|
| 962 | rc = root_dentries_write(service_id, &cfg);
|
|---|
| 963 | if (rc != EOK) {
|
|---|
| 964 | printf(NAME ": Error, failed to write the root directory"
|
|---|
| 965 | " entries to disk.\n");
|
|---|
| 966 | return 2;
|
|---|
| 967 | }
|
|---|
| 968 |
|
|---|
| 969 | printf("Writing the boot sectors.\n");
|
|---|
| 970 |
|
|---|
| 971 | rc = bootsec_write(service_id, &cfg);
|
|---|
| 972 | if (rc != EOK) {
|
|---|
| 973 | printf(NAME ": Error, failed to write the VBR to disk\n");
|
|---|
| 974 | return 2;
|
|---|
| 975 | }
|
|---|
| 976 |
|
|---|
| 977 | printf("Success.\n");
|
|---|
| 978 |
|
|---|
| 979 | return 0;
|
|---|
| 980 | }
|
|---|
| 981 |
|
|---|
| 982 | /**
|
|---|
| 983 | * @}
|
|---|
| 984 | */
|
|---|
| 985 |
|
|---|