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
RevLine 
[dd22cc4]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>
[ffee7bf]41#include <assert.h>
42#include <errno.h>
[50e754e]43#include <malloc.h>
[ffee7bf]44#include <byteorder.h>
[6cf9aeb]45#include <align.h>
[ffee7bf]46#include <sys/types.h>
47#include <sys/typefmt.h>
[3467821]48#include <bool.h>
[da34d61d]49#include <str.h>
[ffee7bf]50#include "exfat.h"
[367014a]51#include "upcase.h"
[dd22cc4]52
53#define NAME "mkexfat"
54
[9744f2d]55/** First sector of the FAT */
56#define FAT_SECTOR_START 128
57
[55dbaeb]58/** First sector of the Main Extended Boot Region */
59#define EBS_SECTOR_START 1
60
[dabe1664]61/** First sector of the Main Extended Boot Region Backup */
62#define EBS_BACKUP_SECTOR_START 13
63
[3938375]64/** First sector of the Main Boot Sector */
65#define MBS_SECTOR 0
[dabe1664]66
[3938375]67/** First sector of the Main Boot Sector Backup */
68#define MBS_BACKUP_SECTOR 12
[dabe1664]69
[8271ae37]70/** VBR Checksum sector */
71#define VBR_CHECKSUM_SECTOR 11
72
73/** VBR Backup Checksum sector */
74#define VBR_BACKUP_CHECKSUM_SECTOR 23
75
[55dbaeb]76/** Size of the Main Extended Boot Region */
77#define EBS_SIZE 8
78
[9744f2d]79/** Divide and round up. */
80#define div_round_up(a, b) (((a) + (b) - 1) / (b))
81
[ffee7bf]82/** The default size of each cluster is 4096 byte */
83#define DEFAULT_CLUSTER_SIZE 4096
84
[78de7be2]85/** Index of the first free cluster on the device */
86#define FIRST_FREE_CLUSTER 2
87
[552efe3]88#define min(x, y) ((x) < (y) ? (x) : (y))
89
[9744f2d]90typedef struct exfat_cfg {
[ffee7bf]91 aoff64_t volume_start;
92 aoff64_t volume_count;
[6cf9aeb]93 unsigned long fat_sector_count;
94 unsigned long data_start_sector;
95 unsigned long rootdir_cluster;
[557e5b13]96 unsigned long upcase_table_cluster;
[da34d61d]97 unsigned long bitmap_cluster;
[6cf9aeb]98 unsigned long total_clusters;
[6aeab59c]99 unsigned long allocated_clusters;
100 size_t bitmap_size;
[ffee7bf]101 size_t sector_size;
102 size_t cluster_size;
[9744f2d]103} exfat_cfg_t;
104
[dabe1664]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
[6aeab59c]118static int
119bitmap_write(service_id_t service_id, exfat_cfg_t *cfg);
120
[da34d61d]121static uint32_t
122upcase_table_checksum(void const *data, size_t nbytes);
123
[116cb91]124static void usage(void)
125{
126 printf("Usage: mkexfat <device>\n");
127}
128
[9744f2d]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{
[5dbd696]136 unsigned long fat_bytes;
137 aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
138 cfg->sector_size;
[ffee7bf]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
[e15f4d5]144 * the entire storage device and to keep the FAT
145 * size less or equal to 64 Mb.
[ffee7bf]146 */
[e15f4d5]147 while (n_req_clusters > 16000000ULL &&
[ffee7bf]148 (cfg->cluster_size < 32 * 1024 * 1024)) {
149
150 cfg->cluster_size <<= 1;
151 n_req_clusters = volume_bytes / cfg->cluster_size;
152 }
[5dbd696]153
[e15f4d5]154 /* The first two clusters are reserved */
[6cf9aeb]155 cfg->total_clusters = n_req_clusters + 2;
[5dbd696]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);
[f8973122]160
[6cf9aeb]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
[6aeab59c]165 /* Compute the bitmap size */
166 cfg->bitmap_size = n_req_clusters / 8;
167
[a69e396]168 /* Compute the number of clusters reserved to the bitmap */
[6aeab59c]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++;
[a69e396]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);
[6aeab59c]178
[89a0a827]179 /* Will be set later */
[6cf9aeb]180 cfg->rootdir_cluster = 0;
181
[da34d61d]182 /* Bitmap always starts at the first free cluster */
183 cfg->bitmap_cluster = FIRST_FREE_CLUSTER;
184
[f8973122]185 /* The first sector of the partition is zero */
186 cfg->volume_start = 0;
[9744f2d]187}
188
[6cf9aeb]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);
[7f381e5]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));
[6cf9aeb]205}
206
[3938375]207/** Initialize the Main Boot Sector fields.
[9744f2d]208 *
[3938375]209 * @param mbs Pointer to the Main Boot Sector structure.
[9744f2d]210 * @param cfg Pointer to the exFAT configuration structure.
[dabe1664]211 * @return Initial checksum value.
[9744f2d]212 */
[dabe1664]213static uint32_t
[3938375]214vbr_initialize(exfat_bs_t *mbs, exfat_cfg_t *cfg)
[9744f2d]215{
216 /* Fill the structure with zeroes */
[3938375]217 memset(mbs, 0, sizeof(exfat_bs_t));
[9744f2d]218
219 /* Init Jump Boot section */
[3938375]220 mbs->jump[0] = 0xEB;
221 mbs->jump[1] = 0x76;
222 mbs->jump[2] = 0x90;
[9744f2d]223
224 /* Set the filesystem name */
[3938375]225 memcpy(mbs->oem_name, "EXFAT ", sizeof(mbs->oem_name));
[9744f2d]226
[3938375]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);
[6cf9aeb]232
[3938375]233 mbs->data_clusters = host2uint32_t_le(cfg->total_clusters -
[6cf9aeb]234 div_round_up(cfg->data_start_sector, cfg->cluster_size));
235
[557e5b13]236 mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
[3938375]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);
[ffee7bf]243
244 /* Maximum cluster size is 32 Mb */
[3938375]245 assert((mbs->bytes_per_sector + mbs->sec_per_cluster) <= 25);
[ffee7bf]246
[3938375]247 mbs->fat_count = 1;
248 mbs->drive_no = 0x80;
249 mbs->allocated_percent = 0;
250 mbs->signature = host2uint16_t_le(0xAA55);
[dabe1664]251
[3938375]252 return vbr_checksum_start(mbs, sizeof(exfat_bs_t));
[dabe1664]253}
254
255static int
256bootsec_write(service_id_t service_id, exfat_cfg_t *cfg)
257{
[3938375]258 exfat_bs_t mbs;
[dabe1664]259 uint32_t vbr_checksum;
[8271ae37]260 uint32_t *chksum_sector;
[dabe1664]261 int rc;
[8271ae37]262 unsigned idx;
263
264 chksum_sector = calloc(cfg->sector_size, sizeof(uint8_t));
265 if (!chksum_sector)
266 return ENOMEM;
[dabe1664]267
[3938375]268 vbr_checksum = vbr_initialize(&mbs, cfg);
[dabe1664]269
[3938375]270 /* Write the Main Boot Sector to disk */
271 rc = block_write_direct(service_id, MBS_SECTOR, 1, &mbs);
[dabe1664]272 if (rc != EOK)
[8271ae37]273 goto exit;
[dabe1664]274
[3938375]275 /* Write the Main extended boot sectors to disk */
276 rc = ebs_write(service_id, cfg, EBS_SECTOR_START, &vbr_checksum);
[dabe1664]277 if (rc != EOK)
[8271ae37]278 goto exit;
[dabe1664]279
[3938375]280 /* Write the Main Boot Sector backup to disk */
281 rc = block_write_direct(service_id, MBS_BACKUP_SECTOR, 1, &mbs);
[dabe1664]282 if (rc != EOK)
[8271ae37]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;
[dabe1664]294
[8271ae37]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;
[dabe1664]300
[3938375]301 /* Write the Main extended boot sectors backup to disk */
[8271ae37]302 rc = ebs_write(service_id, cfg,
[9587b37]303 EBS_BACKUP_SECTOR_START, &vbr_checksum);
[8271ae37]304
305exit:
306 free(chksum_sector);
307 return rc;
[9744f2d]308}
309
[55dbaeb]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.
[dabe1664]314 * @param base Base sector of the EBS.
[55dbaeb]315 * @return EOK on success or a negative error code.
316 */
317static int
[528acda]318ebs_write(service_id_t service_id, exfat_cfg_t *cfg, int base,
319 uint32_t *chksum)
[55dbaeb]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) {
[dabe1664]330 vbr_checksum_update(ebs, cfg->sector_size, chksum);
331
332 rc = block_write_direct(service_id,
[ff415f62]333 i + base, 1, ebs);
[55dbaeb]334
335 if (rc != EOK)
336 goto exit;
337 }
338
[dabe1664]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);
[ff415f62]353
[dabe1664]354 rc = block_write_direct(service_id, i++ + base, 1, ebs);
355 if (rc != EOK)
356 goto exit;
357
[55dbaeb]358exit:
359 free(ebs);
360 return rc;
361}
362
[7f381e5]363/** Initialize the FAT table.
[50e754e]364 *
[3938375]365 * @param service_id The service id.
[50e754e]366 * @param cfg Pointer to the exfat_cfg structure.
367 * @return EOK on success or a negative error code.
368 */
369static int
[7f381e5]370fat_initialize(service_id_t service_id, exfat_cfg_t *cfg)
[50e754e]371{
372 unsigned long i;
373 uint32_t *pfat;
374 int rc;
375
[6aeab59c]376 pfat = calloc(cfg->sector_size, 1);
[50e754e]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
[78de7be2]387 pfat[0] = pfat[1] = 0;
[50e754e]388
[6aeab59c]389 for (i = 1; i < cfg->fat_sector_count; ++i) {
[69c4172]390 rc = block_write_direct(service_id,
[b0fecad]391 FAT_SECTOR_START + i, 1, pfat);
[50e754e]392 if (rc != EOK)
393 goto error;
394 }
395
396error:
397 free(pfat);
398 return rc;
399}
400
[78de7be2]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);
[69f9cf5]415 aoff64_t fat_sec = cur_cls / fat_entries + FAT_SECTOR_START;
[78de7be2]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) {
[b200230]436 /* This sector is full, there are no more free entries,
437 * read the next sector and restart the search.
438 */
[78de7be2]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;
[b200230]444 } else if (ncls == 1) {
445 /* This is the last cluster of this chain, mark it
[ba55c194]446 * with EOF.
[b200230]447 */
[78de7be2]448 fat[cur_cls] = host2uint32_t_le(0xFFFFFFFF);
[b200230]449 }
[78de7be2]450
451 rc = block_write_direct(service_id, fat_sec, 1, fat);
452
453exit:
454 free(fat);
455 return rc;
456}
457
[6aeab59c]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;
[3467821]470 bool need_reset = true;
[6aeab59c]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) {
[3467821]482 if (need_reset) {
483 need_reset = false;
484 memset(bitmap, 0, cfg->sector_size);
485 }
[6aeab59c]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
[cbd8a72]496 allocated_cls -= i;
[3467821]497 need_reset = true;
[6aeab59c]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
[89a0a827]511/** Write the upcase table to disk. */
512static int
513upcase_table_write(service_id_t service_id, exfat_cfg_t *cfg)
514{
[552efe3]515 int rc = EOK;
[89a0a827]516 aoff64_t start_sec, nsecs, i;
517 uint8_t *table_ptr;
[552efe3]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;
[89a0a827]524
[b200230]525 /* Compute the start sector of the upcase table */
[89a0a827]526 start_sec = cfg->data_start_sector;
[552efe3]527 start_sec += ((cfg->upcase_table_cluster - 2) * cfg->cluster_size) /
[89a0a827]528 cfg->sector_size;
529
[b200230]530 /* Compute the number of sectors needed to store the table on disk */
[89a0a827]531 nsecs = div_round_up(sizeof(upcase_table), cfg->sector_size);
532 table_ptr = (uint8_t *) upcase_table;
533
[552efe3]534 for (i = 0; i < nsecs; ++i,
535 table_ptr += min(table_size, cfg->sector_size),
536 table_size -= cfg->sector_size) {
537
[b200230]538 if (table_size < cfg->sector_size) {
539 /* Reset the content of the unused part
540 * of the last sector.
541 */
[552efe3]542 memset(buf, 0, cfg->sector_size);
[b200230]543 }
[552efe3]544 memcpy(buf, table_ptr, min(table_size, cfg->sector_size));
545
[89a0a827]546 rc = block_write_direct(service_id,
[552efe3]547 start_sec + i, 1, buf);
[89a0a827]548 if (rc != EOK)
[8271ae37]549 goto exit;
[89a0a827]550 }
551
[8271ae37]552exit:
[552efe3]553 free(buf);
554 return rc;
[89a0a827]555}
556
[da34d61d]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
[dabe1664]611/** Given a number (n), returns the result of log2(n).
[ffee7bf]612 *
613 * It works only if n is a power of two.
614 */
[6cf9aeb]615static unsigned
616log2(unsigned n)
[ffee7bf]617{
618 unsigned r;
619
620 for (r = 0;n >> r != 1; ++r);
621
622 return r;
623}
624
[dabe1664]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
[528acda]639 checksum = ((checksum << 31) | (checksum >> 1)) +
640 octets[index];
[dabe1664]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
[528acda]653 for (index = 0; index < nbytes; ++index) {
654 *checksum = ((*checksum << 31) | (*checksum >> 1)) +
655 octets[index];
656 }
[dabe1664]657}
658
[392bd67c]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
[dd22cc4]678int main (int argc, char **argv)
679{
[9744f2d]680 exfat_cfg_t cfg;
[78de7be2]681 uint32_t next_cls;
[116cb91]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) {
[ffee7bf]701 printf(NAME ": Error resolving device `%s'.\n", dev_path);
[116cb91]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
[ffee7bf]711 rc = block_get_bsize(service_id, &cfg.sector_size);
[116cb91]712 if (rc != EOK) {
713 printf(NAME ": Error determining device block size.\n");
714 return 2;
715 }
716
[ffee7bf]717 if (cfg.sector_size > 4096) {
[3f6d4ea]718 printf(NAME ": Error, sector size can't be greater" \
[ffee7bf]719 " than 4096 bytes.\n");
720 return 2;
721 }
722
723 rc = block_get_nblocks(service_id, &cfg.volume_count);
[9744f2d]724 if (rc != EOK) {
[5dbd696]725 printf(NAME ": Warning, failed to obtain" \
726 " device block size.\n");
727 /* FIXME: the user should be able to specify the filesystem size */
[9744f2d]728 return 1;
729 } else {
730 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
[ffee7bf]731 cfg.volume_count);
[9744f2d]732 }
733
734 cfg_params_initialize(&cfg);
[6cf9aeb]735 cfg_print_info(&cfg);
[116cb91]736
[b200230]737 /* Initialize the FAT table */
[7f381e5]738 rc = fat_initialize(service_id, &cfg);
[50e754e]739 if (rc != EOK) {
[55dbaeb]740 printf(NAME ": Error, failed to write the FAT to disk\n");
[50e754e]741 return 2;
742 }
743
[78de7be2]744 /* Allocate clusters for the bitmap */
[da34d61d]745 rc = fat_allocate_clusters(service_id, &cfg, cfg.bitmap_cluster,
[78de7be2]746 div_round_up(cfg.bitmap_size, cfg.cluster_size));
747 if (rc != EOK) {
[528acda]748 printf(NAME ": Error, failed to allocate" \
749 " clusters for bitmap.\n");
[78de7be2]750 return 2;
751 }
752
[da34d61d]753 next_cls = cfg.bitmap_cluster +
[78de7be2]754 div_round_up(cfg.bitmap_size, cfg.cluster_size);
[557e5b13]755 cfg.upcase_table_cluster = next_cls;
[78de7be2]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) {
[528acda]761 printf(NAME ":Error, failed to allocate clusters" \
762 " for the upcase table.\n");
[78de7be2]763 return 2;
764 }
765
[69f9cf5]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) {
[528acda]772 printf(NAME ": Error, failed to allocate cluster" \
773 " for the root dentry.\n");
[69f9cf5]774 return 2;
775 }
776
[b200230]777 /* Write the allocation bitmap to disk */
[557e5b13]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
[b200230]785 /* Write the upcase table to disk */
[89a0a827]786 rc = upcase_table_write(service_id, &cfg);
787 if (rc != EOK) {
[528acda]788 printf(NAME ": Error, failed to write the" \
789 " upcase table to disk.\n");
[89a0a827]790 return 2;
791 }
792
[da34d61d]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
[557e5b13]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
[dd22cc4]806 return 0;
807}
[9744f2d]808
Note: See TracBrowser for help on using the repository browser.