source: mainline/uspace/app/mkexfat/mkexfat.c@ 875bc8b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 875bc8b was 875bc8b, checked in by Maurizio Lombardi <m.lombardi85@…>, 13 years ago

mkexfat: force the user to specify the fs size if we failed to obtain the block device size.

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