source: mainline/uspace/app/mkexfat/mkexfat.c@ 132ab5d1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 132ab5d1 was cde999a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Fix comments to stop referring to error codes as negative.

  • Property mode set to 100644
File size: 24.1 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 <stdlib.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 an 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;
354 int rc;
355
356 if (!ebs)
357 return ENOMEM;
358
359 ebs[cfg->sector_size / 4 - 1] = host2uint32_t_le(0xAA550000);
360
361 for (i = 0; i < EBS_SIZE; ++i) {
362 vbr_checksum_update(ebs, cfg->sector_size, chksum);
363
364 rc = block_write_direct(service_id,
365 i + base, 1, ebs);
366
367 if (rc != EOK)
368 goto exit;
369 }
370
371 /* The OEM record is not yet used
372 * by the official exFAT implementation, we'll fill
373 * it with zeroes.
374 */
375
376 memset(ebs, 0, cfg->sector_size);
377 vbr_checksum_update(ebs, cfg->sector_size, chksum);
378
379 rc = block_write_direct(service_id, i++ + base, 1, ebs);
380 if (rc != EOK)
381 goto exit;
382
383 /* The next sector is reserved, fill it with zeroes too */
384 vbr_checksum_update(ebs, cfg->sector_size, chksum);
385
386 rc = block_write_direct(service_id, i + base, 1, ebs);
387 if (rc != EOK)
388 goto exit;
389
390exit:
391 free(ebs);
392 return rc;
393}
394
395/** Initialize the FAT table.
396 *
397 * @param service_id The service id.
398 * @param cfg Pointer to the exfat_cfg structure.
399 * @return EOK on success or an error code.
400 */
401static int
402fat_initialize(service_id_t service_id, exfat_cfg_t *cfg)
403{
404 unsigned long i;
405 uint32_t *pfat;
406 int rc;
407
408 pfat = calloc(cfg->sector_size, 1);
409 if (!pfat)
410 return ENOMEM;
411
412 pfat[0] = host2uint32_t_le(0xFFFFFFF8);
413 pfat[1] = host2uint32_t_le(0xFFFFFFFF);
414
415 rc = block_write_direct(service_id, FAT_SECTOR_START, 1, pfat);
416 if (rc != EOK)
417 goto error;
418
419 pfat[0] = pfat[1] = 0;
420
421 for (i = 1; i < cfg->fat_sector_count; ++i) {
422 rc = block_write_direct(service_id,
423 FAT_SECTOR_START + i, 1, pfat);
424 if (rc != EOK)
425 goto error;
426 }
427
428error:
429 free(pfat);
430 return rc;
431}
432
433/** Allocate a given number of clusters and create a cluster chain.
434 *
435 * @param service_id The service id.
436 * @param cfg Pointer to the exfat configuration structure.
437 * @param cur_cls Cluster index from where to start the allocation.
438 * @param ncls Number of clusters to allocate.
439 * @return EOK on success or an error code.
440 */
441static int
442fat_allocate_clusters(service_id_t service_id, exfat_cfg_t *cfg,
443 uint32_t cur_cls, unsigned long ncls)
444{
445 int rc;
446 unsigned const fat_entries = cfg->sector_size / sizeof(uint32_t);
447 aoff64_t fat_sec = cur_cls / fat_entries + FAT_SECTOR_START;
448 uint32_t *fat;
449 uint32_t next_cls = cur_cls;
450
451 cur_cls %= fat_entries;
452
453 fat = malloc(cfg->sector_size);
454 if (!fat)
455 return ENOMEM;
456
457loop:
458 rc = block_read_direct(service_id, fat_sec, 1, fat);
459 if (rc != EOK)
460 goto exit;
461
462 assert(fat[cur_cls] == 0);
463 assert(ncls > 0);
464
465 for (; cur_cls < fat_entries && ncls > 1; ++cur_cls, --ncls)
466 fat[cur_cls] = host2uint32_t_le(++next_cls);
467
468 if (cur_cls == fat_entries) {
469 /* This sector is full, there are no more free entries,
470 * commit the changes to disk and restart from the next
471 * sector.
472 */
473 rc = block_write_direct(service_id, fat_sec++, 1, fat);
474 if (rc != EOK)
475 goto exit;
476 cur_cls = 0;
477 goto loop;
478 } else if (ncls == 1) {
479 /* This is the last cluster of this chain, mark it
480 * with EOF.
481 */
482 fat[cur_cls] = host2uint32_t_le(0xFFFFFFFF);
483 }
484
485 rc = block_write_direct(service_id, fat_sec, 1, fat);
486
487exit:
488 free(fat);
489 return rc;
490}
491
492/** Initialize the allocation bitmap.
493 *
494 * @param service_id The service id.
495 * @param cfg Pointer to the exfat configuration structure.
496 * @return EOK on success or an error code.
497 */
498static int
499bitmap_write(service_id_t service_id, exfat_cfg_t *cfg)
500{
501 unsigned long i, sec;
502 unsigned long allocated_cls;
503 int rc = EOK;
504 bool need_reset = true;
505
506 /* Bitmap size in sectors */
507 size_t const bss = div_round_up(cfg->bitmap_size, cfg->sector_size);
508
509 uint8_t *bitmap = malloc(cfg->sector_size);
510 if (!bitmap)
511 return ENOMEM;
512
513 allocated_cls = cfg->allocated_clusters;
514
515 for (sec = 0; sec < bss; ++sec) {
516 if (need_reset) {
517 need_reset = false;
518 memset(bitmap, 0, cfg->sector_size);
519 }
520 if (allocated_cls > 0) {
521 for (i = 0; i < allocated_cls; ++i) {
522 unsigned byte_idx = i / 8;
523 unsigned bit_idx = i % 8;
524
525 if (byte_idx == cfg->sector_size)
526 break;
527 bitmap[byte_idx] |= 1 << bit_idx;
528 }
529
530 allocated_cls -= i;
531 need_reset = true;
532 }
533
534 rc = block_write_direct(service_id,
535 cfg->data_start_sector + sec, 1, bitmap);
536 if (rc != EOK)
537 goto exit;
538 }
539
540exit:
541 free(bitmap);
542 return rc;
543}
544
545/** Write the upcase table to disk. */
546static int
547upcase_table_write(service_id_t service_id, exfat_cfg_t *cfg)
548{
549 int rc = EOK;
550 aoff64_t start_sec, nsecs, i;
551 uint8_t *table_ptr;
552 uint8_t *buf;
553 size_t table_size = sizeof(upcase_table);
554
555 buf = malloc(cfg->sector_size);
556 if (!buf)
557 return ENOENT;
558
559 /* Compute the start sector of the upcase table */
560 start_sec = cfg->data_start_sector;
561 start_sec += ((cfg->upcase_table_cluster - 2) * cfg->cluster_size) /
562 cfg->sector_size;
563
564 /* Compute the number of sectors needed to store the table on disk */
565 nsecs = div_round_up(sizeof(upcase_table), cfg->sector_size);
566 table_ptr = (uint8_t *) upcase_table;
567
568 for (i = 0; i < nsecs; ++i,
569 table_ptr += min(table_size, cfg->sector_size),
570 table_size -= cfg->sector_size) {
571
572 if (table_size < cfg->sector_size) {
573 /* Reset the content of the unused part
574 * of the last sector.
575 */
576 memset(buf, 0, cfg->sector_size);
577 }
578 memcpy(buf, table_ptr, min(table_size, cfg->sector_size));
579
580 rc = block_write_direct(service_id,
581 start_sec + i, 1, buf);
582 if (rc != EOK)
583 goto exit;
584 }
585
586exit:
587 free(buf);
588 return rc;
589}
590
591/** Initialize and write the root directory entries to disk.
592 *
593 * @param service_id The service id.
594 * @param cfg Pointer to the exFAT configuration structure.
595 * @return EOK on success or an error code.
596 */
597static int
598root_dentries_write(service_id_t service_id, exfat_cfg_t *cfg)
599{
600 exfat_dentry_t *d;
601 aoff64_t rootdir_sec;
602 uint16_t wlabel[EXFAT_VOLLABEL_LEN + 1];
603 int rc;
604 uint8_t *data;
605 unsigned long i;
606
607 data = calloc(cfg->sector_size, 1);
608 if (!data)
609 return ENOMEM;
610
611 d = (exfat_dentry_t *) data;
612
613 /* Initialize the volume label dentry */
614
615 if (cfg->label != NULL) {
616 memset(wlabel, 0, (EXFAT_VOLLABEL_LEN + 1) * sizeof(uint16_t));
617 rc = str_to_utf16(wlabel, EXFAT_VOLLABEL_LEN + 1, cfg->label);
618 if (rc != EOK) {
619 rc = EINVAL;
620 goto exit;
621 }
622
623 d->type = EXFAT_TYPE_VOLLABEL;
624 memcpy(d->vollabel.label, wlabel, EXFAT_VOLLABEL_LEN * 2);
625 d->vollabel.size = utf16_wsize(wlabel);
626 assert(d->vollabel.size <= EXFAT_VOLLABEL_LEN);
627
628 d++;
629 } else {
630 d->type = EXFAT_TYPE_VOLLABEL & ~EXFAT_TYPE_USED;
631 }
632
633 /* Initialize the allocation bitmap dentry */
634 d->type = EXFAT_TYPE_BITMAP;
635 d->bitmap.flags = 0; /* First FAT */
636 d->bitmap.firstc = host2uint32_t_le(cfg->bitmap_cluster);
637 d->bitmap.size = host2uint64_t_le(cfg->bitmap_size);
638
639 d++;
640
641 /* Initialize the upcase table dentry */
642 d->type = EXFAT_TYPE_UCTABLE;
643 d->uctable.checksum = host2uint32_t_le(upcase_table_checksum(
644 upcase_table, sizeof(upcase_table)));
645 d->uctable.firstc = host2uint32_t_le(cfg->upcase_table_cluster);
646 d->uctable.size = host2uint64_t_le(sizeof(upcase_table));
647
648 /* Compute the number of the sector where the rootdir resides */
649
650 rootdir_sec = cfg->data_start_sector;
651 rootdir_sec += ((cfg->rootdir_cluster - 2) * cfg->cluster_size) /
652 cfg->sector_size;
653
654 rc = block_write_direct(service_id, rootdir_sec, 1, data);
655 if (rc != EOK)
656 goto exit;
657
658 /* Fill the content of the sectors not used by the
659 * root directory with zeroes.
660 */
661 memset(data, 0, cfg->sector_size);
662 for (i = 1; i < cfg->cluster_size / cfg->sector_size; ++i) {
663 rc = block_write_direct(service_id, rootdir_sec + i, 1, data);
664 if (rc != EOK)
665 goto exit;
666 }
667
668exit:
669 free(data);
670 return rc;
671}
672
673/** Given a number (n), returns the result of log2(n).
674 *
675 * It works only if n is a power of two.
676 */
677static unsigned
678log2(unsigned n)
679{
680 unsigned r;
681
682 for (r = 0;n >> r != 1; ++r);
683
684 return r;
685}
686
687/** Initialize the VBR checksum calculation */
688static uint32_t
689vbr_checksum_start(void const *data, size_t nbytes)
690{
691 uint32_t checksum = 0;
692 size_t index;
693 uint8_t const *octets = (uint8_t *) data;
694
695 for (index = 0; index < nbytes; ++index) {
696 if (index == 106 || index == 107 || index == 112) {
697 /* Skip volume_flags and allocated_percent fields */
698 continue;
699 }
700
701 checksum = ((checksum << 31) | (checksum >> 1)) +
702 octets[index];
703 }
704
705 return checksum;
706}
707
708/** Update the VBR checksum */
709static void
710vbr_checksum_update(void const *data, size_t nbytes, uint32_t *checksum)
711{
712 size_t index;
713 uint8_t const *octets = (uint8_t *) data;
714
715 for (index = 0; index < nbytes; ++index) {
716 *checksum = ((*checksum << 31) | (*checksum >> 1)) +
717 octets[index];
718 }
719}
720
721/** Compute the checksum of the upcase table.
722 *
723 * @param data Pointer to the upcase table.
724 * @param nbytes size of the upcase table in bytes.
725 * @return Checksum value.
726 */
727static uint32_t
728upcase_table_checksum(void const *data, size_t nbytes)
729{
730 size_t index;
731 uint32_t chksum = 0;
732 uint8_t const *octets = (uint8_t *) data;
733
734 for (index = 0; index < nbytes; ++index)
735 chksum = ((chksum << 31) | (chksum >> 1)) + octets[index];
736
737 return chksum;
738}
739
740/** Check if a given number is a power of two.
741 *
742 * @param n The number to check.
743 * @return true if n is a power of two, false otherwise.
744 */
745static bool
746is_power_of_two(unsigned long n)
747{
748 if (n == 0)
749 return false;
750
751 return (n & (n - 1)) == 0;
752}
753
754int main (int argc, char **argv)
755{
756 exfat_cfg_t cfg;
757 uint32_t next_cls;
758 char *dev_path;
759 service_id_t service_id;
760 int rc;
761 int c, opt_ind;
762 aoff64_t user_fs_size = 0;
763
764 if (argc < 2) {
765 printf(NAME ": Error, argument missing\n");
766 usage();
767 return 1;
768 }
769
770 cfg.cluster_size = 0;
771 cfg.label = NULL;
772
773 for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
774 c = getopt_long(argc, argv, "hs:c:L:",
775 long_options, &opt_ind);
776 switch (c) {
777 case 'h':
778 usage();
779 return 0;
780 case 's':
781 user_fs_size = (aoff64_t) strtol(optarg, NULL, 10);
782 break;
783
784 case 'c':
785 cfg.cluster_size = strtol(optarg, NULL, 10) * 1024;
786 if (cfg.cluster_size < 4096) {
787 printf(NAME ": Error, cluster size can't"
788 " be less than 4096 byte.\n");
789 return 1;
790 } else if (cfg.cluster_size > 32 * 1024 * 1024) {
791 printf(NAME ": Error, cluster size can't"
792 " be greater than 32 Mb");
793 return 1;
794 }
795
796 if (!is_power_of_two(cfg.cluster_size)) {
797 printf(NAME ": Error, the size of the cluster"
798 " must be a power of two.\n");
799 return 1;
800 }
801 break;
802 case 'L':
803 cfg.label = optarg;
804 break;
805 }
806 }
807
808 argv += optind;
809 dev_path = *argv;
810
811 if (!dev_path) {
812 printf(NAME ": Error, you must specify a valid block"
813 " device.\n");
814 usage();
815 return 1;
816 }
817
818 printf("Device = %s\n", dev_path);
819
820 rc = loc_service_get_id(dev_path, &service_id, 0);
821 if (rc != EOK) {
822 printf(NAME ": Error resolving device `%s'.\n", dev_path);
823 return 2;
824 }
825
826 rc = block_init(service_id, 2048);
827 if (rc != EOK) {
828 printf(NAME ": Error initializing libblock.\n");
829 return 2;
830 }
831
832 rc = block_get_bsize(service_id, &cfg.sector_size);
833 if (rc != EOK) {
834 printf(NAME ": Error determining device sector size.\n");
835 return 2;
836 }
837
838 user_fs_size *= cfg.sector_size;
839 if (user_fs_size > 0 && user_fs_size < 1024 * 1024) {
840 printf(NAME ": Error, fs size can't be less"
841 " than 1 Mb.\n");
842 return 1;
843 }
844
845
846 if (cfg.sector_size > 4096) {
847 printf(NAME ": Error, sector size can't be greater" \
848 " than 4096 bytes.\n");
849 return 2;
850 }
851
852 rc = block_get_nblocks(service_id, &cfg.volume_count);
853 if (rc != EOK) {
854 printf(NAME ": Warning, failed to obtain" \
855 " block device size.\n");
856
857 if (user_fs_size == 0) {
858 printf(NAME ": You must specify the" \
859 " filesystem size.\n");
860 return 1;
861 }
862 } else {
863 printf("Block device has %" PRIuOFF64 " blocks.\n",
864 cfg.volume_count);
865 }
866
867 if (user_fs_size != 0) {
868 if (user_fs_size > cfg.volume_count * cfg.sector_size) {
869 printf(NAME ": Error, the device is not big enough"
870 " to create a filesystem of"
871 " the specified size.\n");
872 return 1;
873 }
874
875 cfg.volume_count = user_fs_size / cfg.sector_size;
876 }
877
878 cfg_params_initialize(&cfg);
879 cfg_print_info(&cfg);
880
881 if (cfg.total_clusters <= cfg.allocated_clusters + 2) {
882 printf(NAME ": Error, insufficient disk space on device.\n");
883 return 2;
884 }
885
886 printf("Writing the allocation table.\n");
887
888 /* Initialize the FAT table */
889 rc = fat_initialize(service_id, &cfg);
890 if (rc != EOK) {
891 printf(NAME ": Error, failed to write the FAT to disk\n");
892 return 2;
893 }
894
895 /* Allocate clusters for the bitmap */
896 rc = fat_allocate_clusters(service_id, &cfg, cfg.bitmap_cluster,
897 div_round_up(cfg.bitmap_size, cfg.cluster_size));
898 if (rc != EOK) {
899 printf(NAME ": Error, failed to allocate" \
900 " clusters for bitmap.\n");
901 return 2;
902 }
903
904 next_cls = cfg.bitmap_cluster +
905 div_round_up(cfg.bitmap_size, cfg.cluster_size);
906 cfg.upcase_table_cluster = next_cls;
907
908 /* Allocate clusters for the upcase table */
909 rc = fat_allocate_clusters(service_id, &cfg, next_cls,
910 div_round_up(sizeof(upcase_table), cfg.cluster_size));
911 if (rc != EOK) {
912 printf(NAME ":Error, failed to allocate clusters" \
913 " for the upcase table.\n");
914 return 2;
915 }
916
917 next_cls += div_round_up(sizeof(upcase_table), cfg.cluster_size);
918 cfg.rootdir_cluster = next_cls;
919
920 /* Allocate a cluster for the root directory entry */
921 rc = fat_allocate_clusters(service_id, &cfg, next_cls, 1);
922 if (rc != EOK) {
923 printf(NAME ": Error, failed to allocate cluster" \
924 " for the root dentry.\n");
925 return 2;
926 }
927
928 printf("Writing the allocation bitmap.\n");
929
930 /* Write the allocation bitmap to disk */
931 rc = bitmap_write(service_id, &cfg);
932 if (rc != EOK) {
933 printf(NAME ": Error, failed to write the allocation" \
934 " bitmap to disk.\n");
935 return 2;
936 }
937
938 printf("Writing the upcase table.\n");
939
940 /* Write the upcase table to disk */
941 rc = upcase_table_write(service_id, &cfg);
942 if (rc != EOK) {
943 printf(NAME ": Error, failed to write the" \
944 " upcase table to disk.\n");
945 return 2;
946 }
947
948 printf("Writing the root directory.\n");
949
950 rc = root_dentries_write(service_id, &cfg);
951 if (rc != EOK) {
952 printf(NAME ": Error, failed to write the root directory" \
953 " entries to disk.\n");
954 return 2;
955 }
956
957 printf("Writing the boot sectors.\n");
958
959 rc = bootsec_write(service_id, &cfg);
960 if (rc != EOK) {
961 printf(NAME ": Error, failed to write the VBR to disk\n");
962 return 2;
963 }
964
965 printf("Success.\n");
966
967 return 0;
968}
969
970/**
971 * @}
972 */
973
Note: See TracBrowser for help on using the repository browser.