source: mainline/uspace/app/mkexfat/mkexfat.c@ 00d23a2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 00d23a2 was f3504c1, checked in by Jiri Svoboda <jiri@…>, 8 years ago

ExFAT volume label support.

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