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
Line 
1/*
2 * Copyright (c) 2024 Jiri Svoboda
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
30/** @addtogroup mkexfat
31 * @{
32 */
33
34/**
35 * @file mkexfat.c
36 * @brief Tool for creating new exFAT file systems.
37 *
38 */
39
40#include <stdio.h>
41#include <stdbool.h>
42#include <stdint.h>
43#include <block.h>
44#include <assert.h>
45#include <errno.h>
46#include <stdlib.h>
47#include <byteorder.h>
48#include <align.h>
49#include <rndgen.h>
50#include <str.h>
51#include <getopt.h>
52#include <macros.h>
53#include "exfat.h"
54#include "upcase.h"
55
56#define NAME "mkexfat"
57
58/** First sector of the FAT */
59#define FAT_SECTOR_START 128
60
61/** First sector of the Main Extended Boot Region */
62#define EBS_SECTOR_START 1
63
64/** First sector of the Main Extended Boot Region Backup */
65#define EBS_BACKUP_SECTOR_START 13
66
67/** First sector of the Main Boot Sector */
68#define MBS_SECTOR 0
69
70/** First sector of the Main Boot Sector Backup */
71#define MBS_BACKUP_SECTOR 12
72
73/** VBR Checksum sector */
74#define VBR_CHECKSUM_SECTOR 11
75
76/** VBR Backup Checksum sector */
77#define VBR_BACKUP_CHECKSUM_SECTOR 23
78
79/** Size of the Main Extended Boot Region */
80#define EBS_SIZE 8
81
82/** Divide and round up. */
83#define div_round_up(a, b) (((a) + (b) - 1) / (b))
84
85/** The default size of each cluster is 4096 byte */
86#define DEFAULT_CLUSTER_SIZE 4096
87
88/** Index of the first free cluster on the device */
89#define FIRST_FREE_CLUSTER 2
90
91typedef struct exfat_cfg {
92 aoff64_t volume_start;
93 aoff64_t volume_count;
94 unsigned long fat_sector_count;
95 unsigned long data_start_sector;
96 unsigned long rootdir_cluster;
97 unsigned long upcase_table_cluster;
98 unsigned long bitmap_cluster;
99 unsigned long total_clusters;
100 unsigned long allocated_clusters;
101 size_t bitmap_size;
102 size_t sector_size;
103 size_t cluster_size;
104 const char *label;
105} exfat_cfg_t;
106
107static unsigned log2i(unsigned n);
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
115static errno_t
116ebs_write(service_id_t service_id, exfat_cfg_t *cfg,
117 int base, uint32_t *chksum);
118
119static errno_t
120bitmap_write(service_id_t service_id, exfat_cfg_t *cfg);
121
122static uint32_t
123upcase_table_checksum(void const *data, size_t nbytes);
124
125static struct option const long_options[] = {
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' },
130};
131
132static void usage(void)
133{
134 printf("Usage: mkexfat [options] <device>\n"
135 "-c, --cluster-size ## Specify the cluster size (Kb)\n"
136 "-s, --fs-size ## Specify the filesystem size (sectors)\n"
137 " --label ## Volume label\n");
138}
139
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{
147 unsigned long fat_bytes;
148 unsigned long fat_cls;
149 aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
150 cfg->sector_size;
151
152 if (cfg->cluster_size != 0) {
153 /* The user already choose the cluster size he wants */
154 cfg->total_clusters = volume_bytes / cfg->cluster_size;
155 goto skip_cluster_size_set;
156 }
157
158 cfg->total_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
159 cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
160
161 /*
162 * Compute the required cluster size to index
163 * the entire storage device and to keep the FAT
164 * size less or equal to 64 Mb.
165 */
166 while (cfg->total_clusters > 16000000ULL &&
167 (cfg->cluster_size < 32 * 1024 * 1024)) {
168
169 cfg->cluster_size <<= 1;
170 cfg->total_clusters = volume_bytes / cfg->cluster_size;
171 }
172
173skip_cluster_size_set:
174
175 /* Compute the FAT size in sectors */
176 fat_bytes = (cfg->total_clusters + 3) * sizeof(uint32_t);
177 cfg->fat_sector_count = div_round_up(fat_bytes, cfg->sector_size);
178
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
183 /*
184 * Subtract the FAT space from the total
185 * number of available clusters.
186 */
187 fat_cls = div_round_up((cfg->data_start_sector -
188 FAT_SECTOR_START) * cfg->sector_size,
189 cfg->cluster_size);
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;
196
197 /* Compute the bitmap size */
198 cfg->bitmap_size = div_round_up(cfg->total_clusters, 8);
199
200 /* Compute the number of clusters reserved to the bitmap */
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++;
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);
210
211 /* Will be set later */
212 cfg->rootdir_cluster = 0;
213
214 /* Bitmap always starts at the first free cluster */
215 cfg->bitmap_cluster = FIRST_FREE_CLUSTER;
216
217 /* The first sector of the partition is zero */
218 cfg->volume_start = 0;
219}
220
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{
228 printf("Sector size: %lu\n",
229 (unsigned long) cfg->sector_size);
230 printf("Cluster size: %lu\n",
231 (unsigned long) cfg->cluster_size);
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)
237 div_round_up(cfg->bitmap_size, cfg->cluster_size));
238 printf("Upcase table size: %lu\n", (unsigned long)
239 div_round_up(sizeof(upcase_table), cfg->cluster_size));
240}
241
242/** Initialize the Main Boot Sector fields.
243 *
244 * @param mbs Pointer to the Main Boot Sector structure.
245 * @param cfg Pointer to the exFAT configuration structure.
246 * @param chksum Place to store initial checksum value.
247 * @return EOK on success or error code
248 */
249static uint32_t
250vbr_initialize(exfat_bs_t *mbs, exfat_cfg_t *cfg, uint32_t *chksum)
251{
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
270 /* Fill the structure with zeroes */
271 memset(mbs, 0, sizeof(exfat_bs_t));
272
273 /* Init Jump Boot section */
274 mbs->jump[0] = 0xEB;
275 mbs->jump[1] = 0x76;
276 mbs->jump[2] = 0x90;
277
278 /* Set the filesystem name */
279 memcpy(mbs->oem_name, "EXFAT ", sizeof(mbs->oem_name));
280
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);
286
287 mbs->data_clusters = host2uint32_t_le(cfg->total_clusters);
288
289 mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
290 mbs->volume_serial = host2uint32_t_le(vsn);
291 mbs->version.major = 1;
292 mbs->version.minor = 0;
293 mbs->volume_flags = host2uint16_t_le(0);
294 mbs->bytes_per_sector = log2i(cfg->sector_size);
295 mbs->sec_per_cluster = log2i(cfg->cluster_size / cfg->sector_size);
296
297 /* Maximum cluster size is 32 Mb */
298 assert((mbs->bytes_per_sector + mbs->sec_per_cluster) <= 25);
299
300 mbs->fat_count = 1;
301 mbs->drive_no = 0x80;
302 mbs->allocated_percent = 0;
303 mbs->signature = host2uint16_t_le(0xAA55);
304
305 *chksum = vbr_checksum_start(mbs, sizeof(exfat_bs_t));
306 return EOK;
307}
308
309static errno_t
310bootsec_write(service_id_t service_id, exfat_cfg_t *cfg)
311{
312 exfat_bs_t mbs;
313 uint32_t vbr_checksum;
314 uint32_t *chksum_sector;
315 errno_t rc;
316 unsigned idx;
317
318 chksum_sector = calloc(cfg->sector_size, sizeof(uint8_t));
319 if (!chksum_sector)
320 return ENOMEM;
321
322 rc = vbr_initialize(&mbs, cfg, &vbr_checksum);
323 if (rc != EOK)
324 goto exit;
325
326 /* Write the Main Boot Sector to disk */
327 rc = block_write_direct(service_id, MBS_SECTOR, 1, &mbs);
328 if (rc != EOK)
329 goto exit;
330
331 /* Write the Main extended boot sectors to disk */
332 rc = ebs_write(service_id, cfg, EBS_SECTOR_START, &vbr_checksum);
333 if (rc != EOK)
334 goto exit;
335
336 /* Write the Main Boot Sector backup to disk */
337 rc = block_write_direct(service_id, MBS_BACKUP_SECTOR, 1, &mbs);
338 if (rc != EOK)
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;
350
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;
356
357 /* Write the Main extended boot sectors backup to disk */
358 rc = ebs_write(service_id, cfg,
359 EBS_BACKUP_SECTOR_START, &vbr_checksum);
360
361exit:
362 free(chksum_sector);
363 return rc;
364}
365
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.
370 * @param base Base sector of the EBS.
371 * @return EOK on success or an error code.
372 */
373static errno_t
374ebs_write(service_id_t service_id, exfat_cfg_t *cfg, int base,
375 uint32_t *chksum)
376{
377 uint32_t *ebs = calloc(cfg->sector_size, sizeof(uint8_t));
378 int i;
379 errno_t rc;
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) {
387 vbr_checksum_update(ebs, cfg->sector_size, chksum);
388
389 rc = block_write_direct(service_id,
390 i + base, 1, ebs);
391
392 if (rc != EOK)
393 goto exit;
394 }
395
396 /*
397 * The OEM record is not yet used
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);
411
412 rc = block_write_direct(service_id, i + base, 1, ebs);
413 if (rc != EOK)
414 goto exit;
415
416exit:
417 free(ebs);
418 return rc;
419}
420
421/** Initialize the FAT table.
422 *
423 * @param service_id The service id.
424 * @param cfg Pointer to the exfat_cfg structure.
425 * @return EOK on success or an error code.
426 */
427static errno_t
428fat_initialize(service_id_t service_id, exfat_cfg_t *cfg)
429{
430 unsigned long i;
431 uint32_t *pfat;
432 errno_t rc;
433
434 pfat = calloc(cfg->sector_size, 1);
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
445 pfat[0] = pfat[1] = 0;
446
447 for (i = 1; i < cfg->fat_sector_count; ++i) {
448 rc = block_write_direct(service_id,
449 FAT_SECTOR_START + i, 1, pfat);
450 if (rc != EOK)
451 goto error;
452 }
453
454error:
455 free(pfat);
456 return rc;
457}
458
459/** Allocate a given number of clusters and create a cluster chain.
460 *
461 * @param service_id The service id.
462 * @param cfg Pointer to the exfat configuration structure.
463 * @param cur_cls Cluster index from where to start the allocation.
464 * @param ncls Number of clusters to allocate.
465 * @return EOK on success or an error code.
466 */
467static errno_t
468fat_allocate_clusters(service_id_t service_id, exfat_cfg_t *cfg,
469 uint32_t cur_cls, unsigned long ncls)
470{
471 errno_t rc;
472 unsigned const fat_entries = cfg->sector_size / sizeof(uint32_t);
473 aoff64_t fat_sec = cur_cls / fat_entries + FAT_SECTOR_START;
474 uint32_t *fat;
475 uint32_t next_cls = cur_cls;
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)
492 fat[cur_cls] = host2uint32_t_le(++next_cls);
493
494 if (cur_cls == fat_entries) {
495 /*
496 * This sector is full, there are no more free entries,
497 * commit the changes to disk and restart from the next
498 * sector.
499 */
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;
505 } else if (ncls == 1) {
506 /*
507 * This is the last cluster of this chain, mark it
508 * with EOF.
509 */
510 fat[cur_cls] = host2uint32_t_le(0xFFFFFFFF);
511 }
512
513 rc = block_write_direct(service_id, fat_sec, 1, fat);
514
515exit:
516 free(fat);
517 return rc;
518}
519
520/** Initialize the allocation bitmap.
521 *
522 * @param service_id The service id.
523 * @param cfg Pointer to the exfat configuration structure.
524 * @return EOK on success or an error code.
525 */
526static errno_t
527bitmap_write(service_id_t service_id, exfat_cfg_t *cfg)
528{
529 unsigned long i, sec;
530 unsigned long allocated_cls;
531 errno_t rc = EOK;
532 bool need_reset = true;
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) {
544 if (need_reset) {
545 need_reset = false;
546 memset(bitmap, 0, cfg->sector_size);
547 }
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
558 allocated_cls -= i;
559 need_reset = true;
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
573/** Write the upcase table to disk. */
574static errno_t
575upcase_table_write(service_id_t service_id, exfat_cfg_t *cfg)
576{
577 errno_t rc = EOK;
578 aoff64_t start_sec, nsecs, i;
579 uint8_t *table_ptr;
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;
586
587 /* Compute the start sector of the upcase table */
588 start_sec = cfg->data_start_sector;
589 start_sec += ((cfg->upcase_table_cluster - 2) * cfg->cluster_size) /
590 cfg->sector_size;
591
592 /* Compute the number of sectors needed to store the table on disk */
593 nsecs = div_round_up(sizeof(upcase_table), cfg->sector_size);
594 table_ptr = (uint8_t *) upcase_table;
595
596 for (i = 0; i < nsecs; ++i,
597 table_ptr += min(table_size, cfg->sector_size),
598 table_size -= cfg->sector_size) {
599
600 if (table_size < cfg->sector_size) {
601 /*
602 * Reset the content of the unused part
603 * of the last sector.
604 */
605 memset(buf, 0, cfg->sector_size);
606 }
607 memcpy(buf, table_ptr, min(table_size, cfg->sector_size));
608
609 rc = block_write_direct(service_id,
610 start_sec + i, 1, buf);
611 if (rc != EOK)
612 goto exit;
613 }
614
615exit:
616 free(buf);
617 return rc;
618}
619
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.
624 * @return EOK on success or an error code.
625 */
626static errno_t
627root_dentries_write(service_id_t service_id, exfat_cfg_t *cfg)
628{
629 exfat_dentry_t *d;
630 aoff64_t rootdir_sec;
631 uint16_t wlabel[EXFAT_VOLLABEL_LEN + 1];
632 errno_t rc;
633 uint8_t *data;
634 unsigned long i;
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
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 }
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);
684 if (rc != EOK)
685 goto exit;
686
687 /*
688 * Fill the content of the sectors not used by the
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:
699 free(data);
700 return rc;
701}
702
703/** Given a number (n), returns the result of log2(n).
704 *
705 * It works only if n is a power of two.
706 */
707static unsigned
708log2i(unsigned n)
709{
710 unsigned r;
711
712 r = 0;
713 while (n >> r != 1)
714 ++r;
715
716 return r;
717}
718
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
733 checksum = ((checksum << 31) | (checksum >> 1)) +
734 octets[index];
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
747 for (index = 0; index < nbytes; ++index) {
748 *checksum = ((*checksum << 31) | (*checksum >> 1)) +
749 octets[index];
750 }
751}
752
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
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 */
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
786int main (int argc, char **argv)
787{
788 exfat_cfg_t cfg;
789 uint32_t next_cls;
790 char *dev_path;
791 service_id_t service_id;
792 errno_t rc;
793 int c, opt_ind;
794 aoff64_t user_fs_size = 0;
795
796 if (argc < 2) {
797 printf(NAME ": Error, argument missing\n");
798 usage();
799 return 1;
800 }
801
802 cfg.cluster_size = 0;
803 cfg.label = NULL;
804
805 c = 0;
806 optind = 0;
807 opt_ind = 0;
808 while (c != -1) {
809 c = getopt_long(argc, argv, "hs:c:L:",
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':
820 cfg.cluster_size = strtol(optarg, NULL, 10) * 1024;
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;
837 case 'L':
838 cfg.label = optarg;
839 break;
840 }
841 }
842
843 argv += optind;
844 dev_path = *argv;
845
846 if (!dev_path) {
847 printf(NAME ": Error, you must specify a valid block"
848 " device.\n");
849 usage();
850 return 1;
851 }
852
853 printf("Device = %s\n", dev_path);
854
855 rc = loc_service_get_id(dev_path, &service_id, 0);
856 if (rc != EOK) {
857 printf(NAME ": Error resolving device `%s'.\n", dev_path);
858 return 2;
859 }
860
861 rc = block_init(service_id);
862 if (rc != EOK) {
863 printf(NAME ": Error initializing libblock.\n");
864 return 2;
865 }
866
867 rc = block_get_bsize(service_id, &cfg.sector_size);
868 if (rc != EOK) {
869 printf(NAME ": Error determining device sector size.\n");
870 return 2;
871 }
872
873 user_fs_size *= cfg.sector_size;
874 if (user_fs_size > 0 && user_fs_size < 1024 * 1024) {
875 printf(NAME ": Error, fs size can't be less"
876 " than 1 Mb.\n");
877 return 1;
878 }
879
880 if (cfg.sector_size > 4096) {
881 printf(NAME ": Error, sector size can't be greater"
882 " than 4096 bytes.\n");
883 return 2;
884 }
885
886 rc = block_get_nblocks(service_id, &cfg.volume_count);
887 if (rc != EOK) {
888 printf(NAME ": Warning, failed to obtain"
889 " block device size.\n");
890
891 if (user_fs_size == 0) {
892 printf(NAME ": You must specify the"
893 " filesystem size.\n");
894 return 1;
895 }
896 } else {
897 printf("Block device has %" PRIuOFF64 " blocks.\n",
898 cfg.volume_count);
899 }
900
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"
904 " to create a filesystem of"
905 " the specified size.\n");
906 return 1;
907 }
908
909 cfg.volume_count = user_fs_size / cfg.sector_size;
910 }
911
912 cfg_params_initialize(&cfg);
913 cfg_print_info(&cfg);
914
915 if (cfg.total_clusters <= cfg.allocated_clusters + 2) {
916 printf(NAME ": Error, insufficient disk space on device.\n");
917 return 2;
918 }
919
920 printf("Writing the allocation table.\n");
921
922 /* Initialize the FAT table */
923 rc = fat_initialize(service_id, &cfg);
924 if (rc != EOK) {
925 printf(NAME ": Error, failed to write the FAT to disk\n");
926 return 2;
927 }
928
929 /* Allocate clusters for the bitmap */
930 rc = fat_allocate_clusters(service_id, &cfg, cfg.bitmap_cluster,
931 div_round_up(cfg.bitmap_size, cfg.cluster_size));
932 if (rc != EOK) {
933 printf(NAME ": Error, failed to allocate"
934 " clusters for bitmap.\n");
935 return 2;
936 }
937
938 next_cls = cfg.bitmap_cluster +
939 div_round_up(cfg.bitmap_size, cfg.cluster_size);
940 cfg.upcase_table_cluster = next_cls;
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) {
946 printf(NAME ":Error, failed to allocate clusters"
947 " for the upcase table.\n");
948 return 2;
949 }
950
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) {
957 printf(NAME ": Error, failed to allocate cluster"
958 " for the root dentry.\n");
959 return 2;
960 }
961
962 printf("Writing the allocation bitmap.\n");
963
964 /* Write the allocation bitmap to disk */
965 rc = bitmap_write(service_id, &cfg);
966 if (rc != EOK) {
967 printf(NAME ": Error, failed to write the allocation"
968 " bitmap to disk.\n");
969 return 2;
970 }
971
972 printf("Writing the upcase table.\n");
973
974 /* Write the upcase table to disk */
975 rc = upcase_table_write(service_id, &cfg);
976 if (rc != EOK) {
977 printf(NAME ": Error, failed to write the"
978 " upcase table to disk.\n");
979 return 2;
980 }
981
982 printf("Writing the root directory.\n");
983
984 rc = root_dentries_write(service_id, &cfg);
985 if (rc != EOK) {
986 printf(NAME ": Error, failed to write the root directory"
987 " entries to disk.\n");
988 return 2;
989 }
990
991 printf("Writing the boot sectors.\n");
992
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
999 printf("Success.\n");
1000
1001 return 0;
1002}
1003
1004/**
1005 * @}
1006 */
Note: See TracBrowser for help on using the repository browser.