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

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

ExFAT volume label support.

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