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