source: mainline/uspace/app/mkexfat/mkexfat.c@ 89e2aac

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 89e2aac was 7354b5e, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove sys/typefmt.h

  • Property mode set to 100644
File size: 23.6 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 <malloc.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} exfat_cfg_t;
104
105
106static unsigned log2(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 int
115ebs_write(service_id_t service_id, exfat_cfg_t *cfg,
116 int base, uint32_t *chksum);
117
118static int
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};
129
130static void usage(void)
131{
132 printf("Usage: mkexfat [options] <device>\n"
133 "-c, --cluster-size ## Specify the cluster size (Kb)\n"
134 "-s, --fs-size ## Specify the filesystem size (sectors)\n");
135}
136
137/** Initialize the exFAT params structure.
138 *
139 * @param cfg Pointer to the exFAT params structure to initialize.
140 */
141static void
142cfg_params_initialize(exfat_cfg_t *cfg)
143{
144 unsigned long fat_bytes;
145 unsigned long fat_cls;
146 aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
147 cfg->sector_size;
148
149 if (cfg->cluster_size != 0) {
150 /* The user already choose the cluster size he wants */
151 cfg->total_clusters = volume_bytes / cfg->cluster_size;
152 goto skip_cluster_size_set;
153 }
154
155 cfg->total_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
156 cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
157
158 /* Compute the required cluster size to index
159 * the entire storage device and to keep the FAT
160 * size less or equal to 64 Mb.
161 */
162 while (cfg->total_clusters > 16000000ULL &&
163 (cfg->cluster_size < 32 * 1024 * 1024)) {
164
165 cfg->cluster_size <<= 1;
166 cfg->total_clusters = volume_bytes / cfg->cluster_size;
167 }
168
169skip_cluster_size_set:
170
171 /* Compute the FAT size in sectors */
172 fat_bytes = (cfg->total_clusters + 3) * sizeof(uint32_t);
173 cfg->fat_sector_count = div_round_up(fat_bytes, cfg->sector_size);
174
175 /* Compute the number of the first data sector */
176 cfg->data_start_sector = ROUND_UP(FAT_SECTOR_START +
177 cfg->fat_sector_count, cfg->cluster_size / cfg->sector_size);
178
179 /* Subtract the FAT space from the total
180 * number of available clusters.
181 */
182 fat_cls = div_round_up((cfg->data_start_sector -
183 FAT_SECTOR_START) * cfg->sector_size,
184 cfg->cluster_size);
185 if (fat_cls >= cfg->total_clusters) {
186 /* Insufficient disk space on device */
187 cfg->total_clusters = 0;
188 return;
189 }
190 cfg->total_clusters -= fat_cls;
191
192 /* Compute the bitmap size */
193 cfg->bitmap_size = div_round_up(cfg->total_clusters, 8);
194
195 /* Compute the number of clusters reserved to the bitmap */
196 cfg->allocated_clusters = div_round_up(cfg->bitmap_size,
197 cfg->cluster_size);
198
199 /* This account for the root directory */
200 cfg->allocated_clusters++;
201
202 /* Compute the number of clusters reserved to the upcase table */
203 cfg->allocated_clusters += div_round_up(sizeof(upcase_table),
204 cfg->cluster_size);
205
206 /* Will be set later */
207 cfg->rootdir_cluster = 0;
208
209 /* Bitmap always starts at the first free cluster */
210 cfg->bitmap_cluster = FIRST_FREE_CLUSTER;
211
212 /* The first sector of the partition is zero */
213 cfg->volume_start = 0;
214}
215
216/** Prints the exFAT structure values
217 *
218 * @param cfg Pointer to the exfat_cfg_t structure.
219 */
220static void
221cfg_print_info(exfat_cfg_t *cfg)
222{
223 printf("Sector size: %lu\n",
224 (unsigned long) cfg->sector_size);
225 printf("Cluster size: %lu\n",
226 (unsigned long) cfg->cluster_size);
227 printf("FAT size in sectors: %lu\n", cfg->fat_sector_count);
228 printf("Data start sector: %lu\n", cfg->data_start_sector);
229 printf("Total num of clusters: %lu\n", cfg->total_clusters);
230 printf("Total used clusters: %lu\n", cfg->allocated_clusters);
231 printf("Bitmap size: %lu\n", (unsigned long)
232 div_round_up(cfg->bitmap_size, cfg->cluster_size));
233 printf("Upcase table size: %lu\n", (unsigned long)
234 div_round_up(sizeof(upcase_table), cfg->cluster_size));
235}
236
237/** Initialize the Main Boot Sector fields.
238 *
239 * @param mbs Pointer to the Main Boot Sector structure.
240 * @param cfg Pointer to the exFAT configuration structure.
241 * @return Initial checksum value.
242 */
243static uint32_t
244vbr_initialize(exfat_bs_t *mbs, exfat_cfg_t *cfg)
245{
246 /* Fill the structure with zeroes */
247 memset(mbs, 0, sizeof(exfat_bs_t));
248
249 /* Init Jump Boot section */
250 mbs->jump[0] = 0xEB;
251 mbs->jump[1] = 0x76;
252 mbs->jump[2] = 0x90;
253
254 /* Set the filesystem name */
255 memcpy(mbs->oem_name, "EXFAT ", sizeof(mbs->oem_name));
256
257 mbs->volume_start = host2uint64_t_le(cfg->volume_start);
258 mbs->volume_count = host2uint64_t_le(cfg->volume_count);
259 mbs->fat_sector_start = host2uint32_t_le(FAT_SECTOR_START);
260 mbs->fat_sector_count = host2uint32_t_le(cfg->fat_sector_count);
261 mbs->data_start_sector = host2uint32_t_le(cfg->data_start_sector);
262
263 mbs->data_clusters = host2uint32_t_le(cfg->total_clusters);
264
265 mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
266 mbs->volume_serial = host2uint32_t_le(0xe1028172);
267 mbs->version.major = 1;
268 mbs->version.minor = 0;
269 mbs->volume_flags = host2uint16_t_le(0);
270 mbs->bytes_per_sector = log2(cfg->sector_size);
271 mbs->sec_per_cluster = log2(cfg->cluster_size / cfg->sector_size);
272
273 /* Maximum cluster size is 32 Mb */
274 assert((mbs->bytes_per_sector + mbs->sec_per_cluster) <= 25);
275
276 mbs->fat_count = 1;
277 mbs->drive_no = 0x80;
278 mbs->allocated_percent = 0;
279 mbs->signature = host2uint16_t_le(0xAA55);
280
281 return vbr_checksum_start(mbs, sizeof(exfat_bs_t));
282}
283
284static int
285bootsec_write(service_id_t service_id, exfat_cfg_t *cfg)
286{
287 exfat_bs_t mbs;
288 uint32_t vbr_checksum;
289 uint32_t *chksum_sector;
290 int rc;
291 unsigned idx;
292
293 chksum_sector = calloc(cfg->sector_size, sizeof(uint8_t));
294 if (!chksum_sector)
295 return ENOMEM;
296
297 vbr_checksum = vbr_initialize(&mbs, cfg);
298
299 /* Write the Main Boot Sector to disk */
300 rc = block_write_direct(service_id, MBS_SECTOR, 1, &mbs);
301 if (rc != EOK)
302 goto exit;
303
304 /* Write the Main extended boot sectors to disk */
305 rc = ebs_write(service_id, cfg, EBS_SECTOR_START, &vbr_checksum);
306 if (rc != EOK)
307 goto exit;
308
309 /* Write the Main Boot Sector backup to disk */
310 rc = block_write_direct(service_id, MBS_BACKUP_SECTOR, 1, &mbs);
311 if (rc != EOK)
312 goto exit;
313
314 /* Initialize the checksum sectors */
315 for (idx = 0; idx < cfg->sector_size / sizeof(uint32_t); ++idx)
316 chksum_sector[idx] = host2uint32_t_le(vbr_checksum);
317
318 /* Write the main checksum sector to disk */
319 rc = block_write_direct(service_id,
320 VBR_CHECKSUM_SECTOR, 1, chksum_sector);
321 if (rc != EOK)
322 goto exit;
323
324 /* Write the backup checksum sector to disk */
325 rc = block_write_direct(service_id,
326 VBR_BACKUP_CHECKSUM_SECTOR, 1, chksum_sector);
327 if (rc != EOK)
328 goto exit;
329
330 /* Write the Main extended boot sectors backup to disk */
331 rc = ebs_write(service_id, cfg,
332 EBS_BACKUP_SECTOR_START, &vbr_checksum);
333
334exit:
335 free(chksum_sector);
336 return rc;
337}
338
339/** Write the Main Extended Boot Sector to disk
340 *
341 * @param service_id The service id.
342 * @param cfg Pointer to the exFAT configuration structure.
343 * @param base Base sector of the EBS.
344 * @return EOK on success or a negative error code.
345 */
346static int
347ebs_write(service_id_t service_id, exfat_cfg_t *cfg, int base,
348 uint32_t *chksum)
349{
350 uint32_t *ebs = calloc(cfg->sector_size, sizeof(uint8_t));
351 int i, rc;
352
353 if (!ebs)
354 return ENOMEM;
355
356 ebs[cfg->sector_size / 4 - 1] = host2uint32_t_le(0xAA550000);
357
358 for (i = 0; i < EBS_SIZE; ++i) {
359 vbr_checksum_update(ebs, cfg->sector_size, chksum);
360
361 rc = block_write_direct(service_id,
362 i + base, 1, ebs);
363
364 if (rc != EOK)
365 goto exit;
366 }
367
368 /* The OEM record is not yet used
369 * by the official exFAT implementation, we'll fill
370 * it with zeroes.
371 */
372
373 memset(ebs, 0, cfg->sector_size);
374 vbr_checksum_update(ebs, cfg->sector_size, chksum);
375
376 rc = block_write_direct(service_id, i++ + base, 1, ebs);
377 if (rc != EOK)
378 goto exit;
379
380 /* The next sector is reserved, fill it with zeroes too */
381 vbr_checksum_update(ebs, cfg->sector_size, chksum);
382
383 rc = block_write_direct(service_id, i + base, 1, ebs);
384 if (rc != EOK)
385 goto exit;
386
387exit:
388 free(ebs);
389 return rc;
390}
391
392/** Initialize the FAT table.
393 *
394 * @param service_id The service id.
395 * @param cfg Pointer to the exfat_cfg structure.
396 * @return EOK on success or a negative error code.
397 */
398static int
399fat_initialize(service_id_t service_id, exfat_cfg_t *cfg)
400{
401 unsigned long i;
402 uint32_t *pfat;
403 int rc;
404
405 pfat = calloc(cfg->sector_size, 1);
406 if (!pfat)
407 return ENOMEM;
408
409 pfat[0] = host2uint32_t_le(0xFFFFFFF8);
410 pfat[1] = host2uint32_t_le(0xFFFFFFFF);
411
412 rc = block_write_direct(service_id, FAT_SECTOR_START, 1, pfat);
413 if (rc != EOK)
414 goto error;
415
416 pfat[0] = pfat[1] = 0;
417
418 for (i = 1; i < cfg->fat_sector_count; ++i) {
419 rc = block_write_direct(service_id,
420 FAT_SECTOR_START + i, 1, pfat);
421 if (rc != EOK)
422 goto error;
423 }
424
425error:
426 free(pfat);
427 return rc;
428}
429
430/** Allocate a given number of clusters and create a cluster chain.
431 *
432 * @param service_id The service id.
433 * @param cfg Pointer to the exfat configuration structure.
434 * @param cur_cls Cluster index from where to start the allocation.
435 * @param ncls Number of clusters to allocate.
436 * @return EOK on success or a negative error code.
437 */
438static int
439fat_allocate_clusters(service_id_t service_id, exfat_cfg_t *cfg,
440 uint32_t cur_cls, unsigned long ncls)
441{
442 int rc;
443 unsigned const fat_entries = cfg->sector_size / sizeof(uint32_t);
444 aoff64_t fat_sec = cur_cls / fat_entries + FAT_SECTOR_START;
445 uint32_t *fat;
446 uint32_t next_cls = cur_cls;
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(++next_cls);
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 if (!dev_path) {
790 printf(NAME ": Error, you must specify a valid block"
791 " device.\n");
792 usage();
793 return 1;
794 }
795
796 printf("Device = %s\n", dev_path);
797
798 rc = loc_service_get_id(dev_path, &service_id, 0);
799 if (rc != EOK) {
800 printf(NAME ": Error resolving device `%s'.\n", dev_path);
801 return 2;
802 }
803
804 rc = block_init(service_id, 2048);
805 if (rc != EOK) {
806 printf(NAME ": Error initializing libblock.\n");
807 return 2;
808 }
809
810 rc = block_get_bsize(service_id, &cfg.sector_size);
811 if (rc != EOK) {
812 printf(NAME ": Error determining device sector size.\n");
813 return 2;
814 }
815
816 user_fs_size *= cfg.sector_size;
817 if (user_fs_size > 0 && user_fs_size < 1024 * 1024) {
818 printf(NAME ": Error, fs size can't be less"
819 " than 1 Mb.\n");
820 return 1;
821 }
822
823
824 if (cfg.sector_size > 4096) {
825 printf(NAME ": Error, sector size can't be greater" \
826 " than 4096 bytes.\n");
827 return 2;
828 }
829
830 rc = block_get_nblocks(service_id, &cfg.volume_count);
831 if (rc != EOK) {
832 printf(NAME ": Warning, failed to obtain" \
833 " block device size.\n");
834
835 if (user_fs_size == 0) {
836 printf(NAME ": You must specify the" \
837 " filesystem size.\n");
838 return 1;
839 }
840 } else {
841 printf("Block device has %" PRIuOFF64 " blocks.\n",
842 cfg.volume_count);
843 }
844
845 if (user_fs_size != 0) {
846 if (user_fs_size > cfg.volume_count * cfg.sector_size) {
847 printf(NAME ": Error, the device is not big enough"
848 " to create a filesystem of"
849 " the specified size.\n");
850 return 1;
851 }
852
853 cfg.volume_count = user_fs_size / cfg.sector_size;
854 }
855
856 cfg_params_initialize(&cfg);
857 cfg_print_info(&cfg);
858
859 if (cfg.total_clusters <= cfg.allocated_clusters + 2) {
860 printf(NAME ": Error, insufficient disk space on device.\n");
861 return 2;
862 }
863
864 printf("Writing the allocation table.\n");
865
866 /* Initialize the FAT table */
867 rc = fat_initialize(service_id, &cfg);
868 if (rc != EOK) {
869 printf(NAME ": Error, failed to write the FAT to disk\n");
870 return 2;
871 }
872
873 /* Allocate clusters for the bitmap */
874 rc = fat_allocate_clusters(service_id, &cfg, cfg.bitmap_cluster,
875 div_round_up(cfg.bitmap_size, cfg.cluster_size));
876 if (rc != EOK) {
877 printf(NAME ": Error, failed to allocate" \
878 " clusters for bitmap.\n");
879 return 2;
880 }
881
882 next_cls = cfg.bitmap_cluster +
883 div_round_up(cfg.bitmap_size, cfg.cluster_size);
884 cfg.upcase_table_cluster = next_cls;
885
886 /* Allocate clusters for the upcase table */
887 rc = fat_allocate_clusters(service_id, &cfg, next_cls,
888 div_round_up(sizeof(upcase_table), cfg.cluster_size));
889 if (rc != EOK) {
890 printf(NAME ":Error, failed to allocate clusters" \
891 " for the upcase table.\n");
892 return 2;
893 }
894
895 next_cls += div_round_up(sizeof(upcase_table), cfg.cluster_size);
896 cfg.rootdir_cluster = next_cls;
897
898 /* Allocate a cluster for the root directory entry */
899 rc = fat_allocate_clusters(service_id, &cfg, next_cls, 1);
900 if (rc != EOK) {
901 printf(NAME ": Error, failed to allocate cluster" \
902 " for the root dentry.\n");
903 return 2;
904 }
905
906 printf("Writing the allocation bitmap.\n");
907
908 /* Write the allocation bitmap to disk */
909 rc = bitmap_write(service_id, &cfg);
910 if (rc != EOK) {
911 printf(NAME ": Error, failed to write the allocation" \
912 " bitmap to disk.\n");
913 return 2;
914 }
915
916 printf("Writing the upcase table.\n");
917
918 /* Write the upcase table to disk */
919 rc = upcase_table_write(service_id, &cfg);
920 if (rc != EOK) {
921 printf(NAME ": Error, failed to write the" \
922 " upcase table to disk.\n");
923 return 2;
924 }
925
926 printf("Writing the root directory.\n");
927
928 rc = root_dentries_write(service_id, &cfg);
929 if (rc != EOK) {
930 printf(NAME ": Error, failed to write the root directory" \
931 " entries to disk.\n");
932 return 2;
933 }
934
935 printf("Writing the boot sectors.\n");
936
937 rc = bootsec_write(service_id, &cfg);
938 if (rc != EOK) {
939 printf(NAME ": Error, failed to write the VBR to disk\n");
940 return 2;
941 }
942
943 printf("Success.\n");
944
945 return 0;
946}
947
948/**
949 * @}
950 */
951
Note: See TracBrowser for help on using the repository browser.