source: mainline/uspace/app/mkexfat/mkexfat.c@ 00d9778

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

Fix block comment formatting (ccheck).

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