source: mainline/uspace/app/mkexfat/mkexfat.c@ 528acda

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

mkexfat: cstyle

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