source: mainline/uspace/app/mkexfat/mkexfat.c@ 41bbab6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 41bbab6 was 81b1db8, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Avoid using standard C function names for local functions.

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