source: mainline/uspace/app/mkexfat/mkexfat.c@ 31718d1

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

mkexfat: Fix an error message.

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