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

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

mkexfat: Fix comment, add description on top of function "is_power_of_two()"

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