source: mainline/uspace/app/mkexfat/mkexfat.c

Last change on this file was 7ae01d5, checked in by Jiri Svoboda <jiri@…>, 15 months ago

Remove unused comm_size parameter of block_init()

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