source: mainline/uspace/app/mkexfat/mkexfat.c@ 10b21a1b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 10b21a1b was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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