source: mainline/uspace/app/mkexfat/mkexfat.c@ 3e6a98c5

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

Standards-compliant boolean type.

  • Property mode set to 100644
File size: 23.7 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>
[f73b291]40#include <block.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>
[3e6a98c5]48#include <stdbool.h>
[da34d61d]49#include <str.h>
[ef144ef]50#include <getopt.h>
[062d900]51#include <macros.h>
[ffee7bf]52#include "exfat.h"
[367014a]53#include "upcase.h"
[dd22cc4]54
55#define NAME "mkexfat"
56
[9744f2d]57/** First sector of the FAT */
58#define FAT_SECTOR_START 128
59
[55dbaeb]60/** First sector of the Main Extended Boot Region */
61#define EBS_SECTOR_START 1
62
[dabe1664]63/** First sector of the Main Extended Boot Region Backup */
64#define EBS_BACKUP_SECTOR_START 13
65
[3938375]66/** First sector of the Main Boot Sector */
67#define MBS_SECTOR 0
[dabe1664]68
[3938375]69/** First sector of the Main Boot Sector Backup */
70#define MBS_BACKUP_SECTOR 12
[dabe1664]71
[8271ae37]72/** VBR Checksum sector */
73#define VBR_CHECKSUM_SECTOR 11
74
75/** VBR Backup Checksum sector */
76#define VBR_BACKUP_CHECKSUM_SECTOR 23
77
[55dbaeb]78/** Size of the Main Extended Boot Region */
79#define EBS_SIZE 8
80
[9744f2d]81/** Divide and round up. */
82#define div_round_up(a, b) (((a) + (b) - 1) / (b))
83
[ffee7bf]84/** The default size of each cluster is 4096 byte */
85#define DEFAULT_CLUSTER_SIZE 4096
86
[78de7be2]87/** Index of the first free cluster on the device */
88#define FIRST_FREE_CLUSTER 2
89
[552efe3]90
[9744f2d]91typedef struct exfat_cfg {
[ffee7bf]92 aoff64_t volume_start;
93 aoff64_t volume_count;
[6cf9aeb]94 unsigned long fat_sector_count;
95 unsigned long data_start_sector;
96 unsigned long rootdir_cluster;
[557e5b13]97 unsigned long upcase_table_cluster;
[da34d61d]98 unsigned long bitmap_cluster;
[6cf9aeb]99 unsigned long total_clusters;
[6aeab59c]100 unsigned long allocated_clusters;
101 size_t bitmap_size;
[ffee7bf]102 size_t sector_size;
103 size_t cluster_size;
[9744f2d]104} exfat_cfg_t;
105
[dabe1664]106
107static unsigned log2(unsigned n);
108
109static uint32_t
110vbr_checksum_start(void const *octets, size_t nbytes);
111
112static void
113vbr_checksum_update(void const *octets, size_t nbytes, uint32_t *checksum);
114
115static int
116ebs_write(service_id_t service_id, exfat_cfg_t *cfg,
117 int base, uint32_t *chksum);
118
[6aeab59c]119static int
120bitmap_write(service_id_t service_id, exfat_cfg_t *cfg);
121
[da34d61d]122static uint32_t
123upcase_table_checksum(void const *data, size_t nbytes);
124
[ef144ef]125static struct option const long_options[] = {
126 {"help", no_argument, 0, 'h'},
127 {"cluster-size", required_argument, 0, 'c'},
128 {"fs-size", required_argument, 0, 's'},
129};
130
[116cb91]131static void usage(void)
132{
[ef144ef]133 printf("Usage: mkexfat [options] <device>\n"
[00af658]134 "-c, --cluster-size ## Specify the cluster size (Kb)\n"
[b4b3cb05]135 "-s, --fs-size ## Specify the filesystem size (sectors)\n");
[116cb91]136}
137
[9744f2d]138/** Initialize the exFAT params structure.
139 *
140 * @param cfg Pointer to the exFAT params structure to initialize.
141 */
142static void
143cfg_params_initialize(exfat_cfg_t *cfg)
144{
[5dbd696]145 unsigned long fat_bytes;
[0f22528]146 unsigned long fat_cls;
[5dbd696]147 aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
148 cfg->sector_size;
[ef144ef]149
150 if (cfg->cluster_size != 0) {
151 /* The user already choose the cluster size he wants */
[d7f09583]152 cfg->total_clusters = volume_bytes / cfg->cluster_size;
[ef144ef]153 goto skip_cluster_size_set;
154 }
[ffee7bf]155
[d7f09583]156 cfg->total_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
[ffee7bf]157 cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
158
159 /* Compute the required cluster size to index
[e15f4d5]160 * the entire storage device and to keep the FAT
161 * size less or equal to 64 Mb.
[ffee7bf]162 */
[d7f09583]163 while (cfg->total_clusters > 16000000ULL &&
[ffee7bf]164 (cfg->cluster_size < 32 * 1024 * 1024)) {
165
166 cfg->cluster_size <<= 1;
[b6eb5da]167 cfg->total_clusters = volume_bytes / cfg->cluster_size;
[ffee7bf]168 }
[5dbd696]169
[ef144ef]170skip_cluster_size_set:
171
[5dbd696]172 /* Compute the FAT size in sectors */
[d7f09583]173 fat_bytes = (cfg->total_clusters + 3) * sizeof(uint32_t);
[5dbd696]174 cfg->fat_sector_count = div_round_up(fat_bytes, cfg->sector_size);
[f8973122]175
[6cf9aeb]176 /* Compute the number of the first data sector */
177 cfg->data_start_sector = ROUND_UP(FAT_SECTOR_START +
178 cfg->fat_sector_count, cfg->cluster_size / cfg->sector_size);
179
[9bbe54f]180 /* Subtract the FAT space from the total
[b6eb5da]181 * number of available clusters.
182 */
[0f22528]183 fat_cls = div_round_up((cfg->data_start_sector -
[b6eb5da]184 FAT_SECTOR_START) * cfg->sector_size,
185 cfg->cluster_size);
[0f22528]186 if (fat_cls >= cfg->total_clusters) {
187 /* Insufficient disk space on device */
188 cfg->total_clusters = 0;
189 return;
190 }
191 cfg->total_clusters -= fat_cls;
[b6eb5da]192
[6aeab59c]193 /* Compute the bitmap size */
[d7f09583]194 cfg->bitmap_size = div_round_up(cfg->total_clusters, 8);
[6aeab59c]195
[a69e396]196 /* Compute the number of clusters reserved to the bitmap */
[6aeab59c]197 cfg->allocated_clusters = div_round_up(cfg->bitmap_size,
198 cfg->cluster_size);
199
200 /* This account for the root directory */
201 cfg->allocated_clusters++;
[a69e396]202
203 /* Compute the number of clusters reserved to the upcase table */
204 cfg->allocated_clusters += div_round_up(sizeof(upcase_table),
205 cfg->cluster_size);
[6aeab59c]206
[89a0a827]207 /* Will be set later */
[6cf9aeb]208 cfg->rootdir_cluster = 0;
209
[da34d61d]210 /* Bitmap always starts at the first free cluster */
211 cfg->bitmap_cluster = FIRST_FREE_CLUSTER;
212
[f8973122]213 /* The first sector of the partition is zero */
214 cfg->volume_start = 0;
[9744f2d]215}
216
[6cf9aeb]217/** Prints the exFAT structure values
218 *
219 * @param cfg Pointer to the exfat_cfg_t structure.
220 */
221static void
222cfg_print_info(exfat_cfg_t *cfg)
223{
[8efc4c1]224 printf("Sector size: %lu\n",
[e04bfbf0]225 (unsigned long) cfg->sector_size);
[8efc4c1]226 printf("Cluster size: %lu\n",
[e04bfbf0]227 (unsigned long) cfg->cluster_size);
[8efc4c1]228 printf("FAT size in sectors: %lu\n", cfg->fat_sector_count);
229 printf("Data start sector: %lu\n", cfg->data_start_sector);
230 printf("Total num of clusters: %lu\n", cfg->total_clusters);
231 printf("Total used clusters: %lu\n", cfg->allocated_clusters);
232 printf("Bitmap size: %lu\n", (unsigned long)
[7f381e5]233 div_round_up(cfg->bitmap_size, cfg->cluster_size));
[8efc4c1]234 printf("Upcase table size: %lu\n", (unsigned long)
[7f381e5]235 div_round_up(sizeof(upcase_table), cfg->cluster_size));
[6cf9aeb]236}
237
[3938375]238/** Initialize the Main Boot Sector fields.
[9744f2d]239 *
[3938375]240 * @param mbs Pointer to the Main Boot Sector structure.
[9744f2d]241 * @param cfg Pointer to the exFAT configuration structure.
[dabe1664]242 * @return Initial checksum value.
[9744f2d]243 */
[dabe1664]244static uint32_t
[3938375]245vbr_initialize(exfat_bs_t *mbs, exfat_cfg_t *cfg)
[9744f2d]246{
247 /* Fill the structure with zeroes */
[3938375]248 memset(mbs, 0, sizeof(exfat_bs_t));
[9744f2d]249
250 /* Init Jump Boot section */
[3938375]251 mbs->jump[0] = 0xEB;
252 mbs->jump[1] = 0x76;
253 mbs->jump[2] = 0x90;
[9744f2d]254
255 /* Set the filesystem name */
[3938375]256 memcpy(mbs->oem_name, "EXFAT ", sizeof(mbs->oem_name));
[9744f2d]257
[3938375]258 mbs->volume_start = host2uint64_t_le(cfg->volume_start);
259 mbs->volume_count = host2uint64_t_le(cfg->volume_count);
260 mbs->fat_sector_start = host2uint32_t_le(FAT_SECTOR_START);
261 mbs->fat_sector_count = host2uint32_t_le(cfg->fat_sector_count);
262 mbs->data_start_sector = host2uint32_t_le(cfg->data_start_sector);
[6cf9aeb]263
[d7f09583]264 mbs->data_clusters = host2uint32_t_le(cfg->total_clusters);
[6cf9aeb]265
[557e5b13]266 mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
[09c954b]267 mbs->volume_serial = host2uint32_t_le(0xe1028172);
[3938375]268 mbs->version.major = 1;
269 mbs->version.minor = 0;
270 mbs->volume_flags = host2uint16_t_le(0);
271 mbs->bytes_per_sector = log2(cfg->sector_size);
272 mbs->sec_per_cluster = log2(cfg->cluster_size / cfg->sector_size);
[ffee7bf]273
274 /* Maximum cluster size is 32 Mb */
[3938375]275 assert((mbs->bytes_per_sector + mbs->sec_per_cluster) <= 25);
[ffee7bf]276
[3938375]277 mbs->fat_count = 1;
278 mbs->drive_no = 0x80;
279 mbs->allocated_percent = 0;
280 mbs->signature = host2uint16_t_le(0xAA55);
[dabe1664]281
[3938375]282 return vbr_checksum_start(mbs, sizeof(exfat_bs_t));
[dabe1664]283}
284
285static int
286bootsec_write(service_id_t service_id, exfat_cfg_t *cfg)
287{
[3938375]288 exfat_bs_t mbs;
[dabe1664]289 uint32_t vbr_checksum;
[8271ae37]290 uint32_t *chksum_sector;
[dabe1664]291 int rc;
[8271ae37]292 unsigned idx;
293
294 chksum_sector = calloc(cfg->sector_size, sizeof(uint8_t));
295 if (!chksum_sector)
296 return ENOMEM;
[dabe1664]297
[3938375]298 vbr_checksum = vbr_initialize(&mbs, cfg);
[dabe1664]299
[3938375]300 /* Write the Main Boot Sector to disk */
301 rc = block_write_direct(service_id, MBS_SECTOR, 1, &mbs);
[dabe1664]302 if (rc != EOK)
[8271ae37]303 goto exit;
[dabe1664]304
[3938375]305 /* Write the Main extended boot sectors to disk */
306 rc = ebs_write(service_id, cfg, EBS_SECTOR_START, &vbr_checksum);
[dabe1664]307 if (rc != EOK)
[8271ae37]308 goto exit;
[dabe1664]309
[3938375]310 /* Write the Main Boot Sector backup to disk */
311 rc = block_write_direct(service_id, MBS_BACKUP_SECTOR, 1, &mbs);
[dabe1664]312 if (rc != EOK)
[8271ae37]313 goto exit;
314
315 /* Initialize the checksum sectors */
316 for (idx = 0; idx < cfg->sector_size / sizeof(uint32_t); ++idx)
317 chksum_sector[idx] = host2uint32_t_le(vbr_checksum);
318
319 /* Write the main checksum sector to disk */
320 rc = block_write_direct(service_id,
321 VBR_CHECKSUM_SECTOR, 1, chksum_sector);
322 if (rc != EOK)
323 goto exit;
[dabe1664]324
[8271ae37]325 /* Write the backup checksum sector to disk */
326 rc = block_write_direct(service_id,
327 VBR_BACKUP_CHECKSUM_SECTOR, 1, chksum_sector);
328 if (rc != EOK)
329 goto exit;
[dabe1664]330
[3938375]331 /* Write the Main extended boot sectors backup to disk */
[8271ae37]332 rc = ebs_write(service_id, cfg,
[9587b37]333 EBS_BACKUP_SECTOR_START, &vbr_checksum);
[8271ae37]334
335exit:
336 free(chksum_sector);
337 return rc;
[9744f2d]338}
339
[55dbaeb]340/** Write the Main Extended Boot Sector to disk
341 *
342 * @param service_id The service id.
343 * @param cfg Pointer to the exFAT configuration structure.
[dabe1664]344 * @param base Base sector of the EBS.
[55dbaeb]345 * @return EOK on success or a negative error code.
346 */
347static int
[528acda]348ebs_write(service_id_t service_id, exfat_cfg_t *cfg, int base,
349 uint32_t *chksum)
[55dbaeb]350{
351 uint32_t *ebs = calloc(cfg->sector_size, sizeof(uint8_t));
352 int i, rc;
353
354 if (!ebs)
355 return ENOMEM;
356
357 ebs[cfg->sector_size / 4 - 1] = host2uint32_t_le(0xAA550000);
358
359 for (i = 0; i < EBS_SIZE; ++i) {
[dabe1664]360 vbr_checksum_update(ebs, cfg->sector_size, chksum);
361
362 rc = block_write_direct(service_id,
[ff415f62]363 i + base, 1, ebs);
[55dbaeb]364
365 if (rc != EOK)
366 goto exit;
367 }
368
[dabe1664]369 /* The OEM record is not yet used
370 * by the official exFAT implementation, we'll fill
371 * it with zeroes.
372 */
373
374 memset(ebs, 0, cfg->sector_size);
375 vbr_checksum_update(ebs, cfg->sector_size, chksum);
376
377 rc = block_write_direct(service_id, i++ + base, 1, ebs);
378 if (rc != EOK)
379 goto exit;
380
381 /* The next sector is reserved, fill it with zeroes too */
382 vbr_checksum_update(ebs, cfg->sector_size, chksum);
[ff415f62]383
[2601383]384 rc = block_write_direct(service_id, i + base, 1, ebs);
[dabe1664]385 if (rc != EOK)
386 goto exit;
387
[55dbaeb]388exit:
389 free(ebs);
390 return rc;
391}
392
[7f381e5]393/** Initialize the FAT table.
[50e754e]394 *
[3938375]395 * @param service_id The service id.
[50e754e]396 * @param cfg Pointer to the exfat_cfg structure.
397 * @return EOK on success or a negative error code.
398 */
399static int
[7f381e5]400fat_initialize(service_id_t service_id, exfat_cfg_t *cfg)
[50e754e]401{
402 unsigned long i;
403 uint32_t *pfat;
404 int rc;
405
[6aeab59c]406 pfat = calloc(cfg->sector_size, 1);
[50e754e]407 if (!pfat)
408 return ENOMEM;
409
410 pfat[0] = host2uint32_t_le(0xFFFFFFF8);
411 pfat[1] = host2uint32_t_le(0xFFFFFFFF);
412
413 rc = block_write_direct(service_id, FAT_SECTOR_START, 1, pfat);
414 if (rc != EOK)
415 goto error;
416
[78de7be2]417 pfat[0] = pfat[1] = 0;
[50e754e]418
[6aeab59c]419 for (i = 1; i < cfg->fat_sector_count; ++i) {
[69c4172]420 rc = block_write_direct(service_id,
[b0fecad]421 FAT_SECTOR_START + i, 1, pfat);
[50e754e]422 if (rc != EOK)
423 goto error;
424 }
425
426error:
427 free(pfat);
428 return rc;
429}
430
[78de7be2]431/** Allocate a given number of clusters and create a cluster chain.
432 *
433 * @param service_id The service id.
[aa37d6f]434 * @param cfg Pointer to the exfat configuration structure.
[78de7be2]435 * @param cur_cls Cluster index from where to start the allocation.
436 * @param ncls Number of clusters to allocate.
437 * @return EOK on success or a negative error code.
438 */
439static int
440fat_allocate_clusters(service_id_t service_id, exfat_cfg_t *cfg,
441 uint32_t cur_cls, unsigned long ncls)
442{
443 int rc;
444 unsigned const fat_entries = cfg->sector_size / sizeof(uint32_t);
[69f9cf5]445 aoff64_t fat_sec = cur_cls / fat_entries + FAT_SECTOR_START;
[78de7be2]446 uint32_t *fat;
[df4fbe1]447 uint32_t next_cls = cur_cls;
[78de7be2]448
449 cur_cls %= fat_entries;
450
451 fat = malloc(cfg->sector_size);
452 if (!fat)
453 return ENOMEM;
454
455loop:
456 rc = block_read_direct(service_id, fat_sec, 1, fat);
457 if (rc != EOK)
458 goto exit;
459
460 assert(fat[cur_cls] == 0);
461 assert(ncls > 0);
462
463 for (; cur_cls < fat_entries && ncls > 1; ++cur_cls, --ncls)
[df4fbe1]464 fat[cur_cls] = host2uint32_t_le(++next_cls);
[78de7be2]465
466 if (cur_cls == fat_entries) {
[b200230]467 /* This sector is full, there are no more free entries,
[2601383]468 * commit the changes to disk and restart from the next
469 * sector.
[b200230]470 */
[78de7be2]471 rc = block_write_direct(service_id, fat_sec++, 1, fat);
472 if (rc != EOK)
473 goto exit;
474 cur_cls = 0;
475 goto loop;
[b200230]476 } else if (ncls == 1) {
477 /* This is the last cluster of this chain, mark it
[ba55c194]478 * with EOF.
[b200230]479 */
[78de7be2]480 fat[cur_cls] = host2uint32_t_le(0xFFFFFFFF);
[b200230]481 }
[78de7be2]482
483 rc = block_write_direct(service_id, fat_sec, 1, fat);
484
485exit:
486 free(fat);
487 return rc;
488}
489
[6aeab59c]490/** Initialize the allocation bitmap.
491 *
492 * @param service_id The service id.
493 * @param cfg Pointer to the exfat configuration structure.
494 * @return EOK on success or a negative error code.
495 */
496static int
497bitmap_write(service_id_t service_id, exfat_cfg_t *cfg)
498{
499 unsigned long i, sec;
500 unsigned long allocated_cls;
501 int rc = EOK;
[3467821]502 bool need_reset = true;
[6aeab59c]503
504 /* Bitmap size in sectors */
505 size_t const bss = div_round_up(cfg->bitmap_size, cfg->sector_size);
506
507 uint8_t *bitmap = malloc(cfg->sector_size);
508 if (!bitmap)
509 return ENOMEM;
510
511 allocated_cls = cfg->allocated_clusters;
512
513 for (sec = 0; sec < bss; ++sec) {
[3467821]514 if (need_reset) {
515 need_reset = false;
516 memset(bitmap, 0, cfg->sector_size);
517 }
[6aeab59c]518 if (allocated_cls > 0) {
519 for (i = 0; i < allocated_cls; ++i) {
520 unsigned byte_idx = i / 8;
521 unsigned bit_idx = i % 8;
522
523 if (byte_idx == cfg->sector_size)
524 break;
525 bitmap[byte_idx] |= 1 << bit_idx;
526 }
527
[cbd8a72]528 allocated_cls -= i;
[3467821]529 need_reset = true;
[6aeab59c]530 }
531
532 rc = block_write_direct(service_id,
533 cfg->data_start_sector + sec, 1, bitmap);
534 if (rc != EOK)
535 goto exit;
536 }
537
538exit:
539 free(bitmap);
540 return rc;
541}
542
[89a0a827]543/** Write the upcase table to disk. */
544static int
545upcase_table_write(service_id_t service_id, exfat_cfg_t *cfg)
546{
[552efe3]547 int rc = EOK;
[89a0a827]548 aoff64_t start_sec, nsecs, i;
549 uint8_t *table_ptr;
[552efe3]550 uint8_t *buf;
551 size_t table_size = sizeof(upcase_table);
552
553 buf = malloc(cfg->sector_size);
554 if (!buf)
555 return ENOENT;
[89a0a827]556
[b200230]557 /* Compute the start sector of the upcase table */
[89a0a827]558 start_sec = cfg->data_start_sector;
[552efe3]559 start_sec += ((cfg->upcase_table_cluster - 2) * cfg->cluster_size) /
[89a0a827]560 cfg->sector_size;
561
[b200230]562 /* Compute the number of sectors needed to store the table on disk */
[89a0a827]563 nsecs = div_round_up(sizeof(upcase_table), cfg->sector_size);
564 table_ptr = (uint8_t *) upcase_table;
565
[552efe3]566 for (i = 0; i < nsecs; ++i,
567 table_ptr += min(table_size, cfg->sector_size),
568 table_size -= cfg->sector_size) {
569
[b200230]570 if (table_size < cfg->sector_size) {
571 /* Reset the content of the unused part
572 * of the last sector.
573 */
[552efe3]574 memset(buf, 0, cfg->sector_size);
[b200230]575 }
[552efe3]576 memcpy(buf, table_ptr, min(table_size, cfg->sector_size));
577
[89a0a827]578 rc = block_write_direct(service_id,
[552efe3]579 start_sec + i, 1, buf);
[89a0a827]580 if (rc != EOK)
[8271ae37]581 goto exit;
[89a0a827]582 }
583
[8271ae37]584exit:
[552efe3]585 free(buf);
586 return rc;
[89a0a827]587}
588
[da34d61d]589/** Initialize and write the root directory entries to disk.
590 *
591 * @param service_id The service id.
592 * @param cfg Pointer to the exFAT configuration structure.
593 * @return EOK on success or a negative error code.
594 */
595static int
596root_dentries_write(service_id_t service_id, exfat_cfg_t *cfg)
597{
598 exfat_dentry_t *d;
599 aoff64_t rootdir_sec;
600 int rc;
601 uint8_t *data;
[ebea7acf]602 unsigned long i;
[da34d61d]603
604 data = calloc(cfg->sector_size, 1);
605 if (!data)
606 return ENOMEM;
607
608 d = (exfat_dentry_t *) data;
609
610 /* Initialize the volume label dentry */
611 d->type = EXFAT_TYPE_VOLLABEL;
[09c954b]612 str_to_utf16(d->vollabel.label, 8, "HELENOS ");
613 d->vollabel.size = 8;
[da34d61d]614
615 d++;
616
617 /* Initialize the allocation bitmap dentry */
618 d->type = EXFAT_TYPE_BITMAP;
619 d->bitmap.flags = 0; /* First FAT */
620 d->bitmap.firstc = host2uint32_t_le(cfg->bitmap_cluster);
621 d->bitmap.size = host2uint64_t_le(cfg->bitmap_size);
622
623 d++;
624
625 /* Initialize the upcase table dentry */
626 d->type = EXFAT_TYPE_UCTABLE;
627 d->uctable.checksum = host2uint32_t_le(upcase_table_checksum(
628 upcase_table, sizeof(upcase_table)));
629 d->uctable.firstc = host2uint32_t_le(cfg->upcase_table_cluster);
630 d->uctable.size = host2uint64_t_le(sizeof(upcase_table));
631
632 /* Compute the number of the sector where the rootdir resides */
633
634 rootdir_sec = cfg->data_start_sector;
635 rootdir_sec += ((cfg->rootdir_cluster - 2) * cfg->cluster_size) /
636 cfg->sector_size;
637
638 rc = block_write_direct(service_id, rootdir_sec, 1, data);
[ebea7acf]639 if (rc != EOK)
640 goto exit;
[da34d61d]641
[ebea7acf]642 /* Fill the content of the sectors not used by the
643 * root directory with zeroes.
644 */
645 memset(data, 0, cfg->sector_size);
646 for (i = 1; i < cfg->cluster_size / cfg->sector_size; ++i) {
647 rc = block_write_direct(service_id, rootdir_sec + i, 1, data);
648 if (rc != EOK)
649 goto exit;
650 }
651
652exit:
[da34d61d]653 free(data);
654 return rc;
655}
656
[dabe1664]657/** Given a number (n), returns the result of log2(n).
[ffee7bf]658 *
659 * It works only if n is a power of two.
660 */
[6cf9aeb]661static unsigned
662log2(unsigned n)
[ffee7bf]663{
664 unsigned r;
665
666 for (r = 0;n >> r != 1; ++r);
667
668 return r;
669}
670
[dabe1664]671/** Initialize the VBR checksum calculation */
672static uint32_t
673vbr_checksum_start(void const *data, size_t nbytes)
674{
675 uint32_t checksum = 0;
676 size_t index;
677 uint8_t const *octets = (uint8_t *) data;
678
679 for (index = 0; index < nbytes; ++index) {
680 if (index == 106 || index == 107 || index == 112) {
681 /* Skip volume_flags and allocated_percent fields */
682 continue;
683 }
684
[528acda]685 checksum = ((checksum << 31) | (checksum >> 1)) +
686 octets[index];
[dabe1664]687 }
688
689 return checksum;
690}
691
692/** Update the VBR checksum */
693static void
694vbr_checksum_update(void const *data, size_t nbytes, uint32_t *checksum)
695{
696 size_t index;
697 uint8_t const *octets = (uint8_t *) data;
698
[528acda]699 for (index = 0; index < nbytes; ++index) {
700 *checksum = ((*checksum << 31) | (*checksum >> 1)) +
701 octets[index];
702 }
[dabe1664]703}
704
[392bd67c]705/** Compute the checksum of the upcase table.
706 *
707 * @param data Pointer to the upcase table.
708 * @param nbytes size of the upcase table in bytes.
709 * @return Checksum value.
710 */
711static uint32_t
712upcase_table_checksum(void const *data, size_t nbytes)
713{
714 size_t index;
715 uint32_t chksum = 0;
716 uint8_t const *octets = (uint8_t *) data;
717
718 for (index = 0; index < nbytes; ++index)
719 chksum = ((chksum << 31) | (chksum >> 1)) + octets[index];
720
721 return chksum;
722}
723
[aa37d6f]724/** Check if a given number is a power of two.
725 *
726 * @param n The number to check.
727 * @return true if n is a power of two, false otherwise.
728 */
[ef144ef]729static bool
730is_power_of_two(unsigned long n)
731{
732 if (n == 0)
733 return false;
734
735 return (n & (n - 1)) == 0;
736}
737
[dd22cc4]738int main (int argc, char **argv)
739{
[9744f2d]740 exfat_cfg_t cfg;
[78de7be2]741 uint32_t next_cls;
[116cb91]742 char *dev_path;
743 service_id_t service_id;
[ef144ef]744 int rc, c, opt_ind;
745 aoff64_t user_fs_size = 0;
[116cb91]746
747 if (argc < 2) {
748 printf(NAME ": Error, argument missing\n");
749 usage();
750 return 1;
751 }
752
[ef144ef]753 cfg.cluster_size = 0;
754
755 for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
756 c = getopt_long(argc, argv, "hs:c:",
757 long_options, &opt_ind);
758 switch (c) {
759 case 'h':
760 usage();
761 return 0;
762 case 's':
763 user_fs_size = (aoff64_t) strtol(optarg, NULL, 10);
764 break;
765
766 case 'c':
[00af658]767 cfg.cluster_size = strtol(optarg, NULL, 10) * 1024;
[ef144ef]768 if (cfg.cluster_size < 4096) {
769 printf(NAME ": Error, cluster size can't"
770 " be less than 4096 byte.\n");
771 return 1;
772 } else if (cfg.cluster_size > 32 * 1024 * 1024) {
773 printf(NAME ": Error, cluster size can't"
774 " be greater than 32 Mb");
775 return 1;
776 }
777
778 if (!is_power_of_two(cfg.cluster_size)) {
779 printf(NAME ": Error, the size of the cluster"
780 " must be a power of two.\n");
781 return 1;
782 }
783 break;
784 }
785 }
[116cb91]786
[ef144ef]787 argv += optind;
[116cb91]788 dev_path = *argv;
789
[8d8ad19]790 if (!dev_path) {
791 printf(NAME ": Error, you must specify a valid block"
792 " device.\n");
793 usage();
794 return 1;
795 }
796
[8efc4c1]797 printf("Device = %s\n", dev_path);
[116cb91]798
799 rc = loc_service_get_id(dev_path, &service_id, 0);
800 if (rc != EOK) {
[ffee7bf]801 printf(NAME ": Error resolving device `%s'.\n", dev_path);
[116cb91]802 return 2;
803 }
804
805 rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
806 if (rc != EOK) {
807 printf(NAME ": Error initializing libblock.\n");
808 return 2;
809 }
810
[ffee7bf]811 rc = block_get_bsize(service_id, &cfg.sector_size);
[116cb91]812 if (rc != EOK) {
[875bc8b]813 printf(NAME ": Error determining device sector size.\n");
[116cb91]814 return 2;
815 }
816
[b4b3cb05]817 user_fs_size *= cfg.sector_size;
[875bc8b]818 if (user_fs_size > 0 && user_fs_size < 1024 * 1024) {
[b4b3cb05]819 printf(NAME ": Error, fs size can't be less"
820 " than 1 Mb.\n");
821 return 1;
822 }
823
824
[ffee7bf]825 if (cfg.sector_size > 4096) {
[3f6d4ea]826 printf(NAME ": Error, sector size can't be greater" \
[ffee7bf]827 " than 4096 bytes.\n");
828 return 2;
829 }
830
831 rc = block_get_nblocks(service_id, &cfg.volume_count);
[9744f2d]832 if (rc != EOK) {
[5dbd696]833 printf(NAME ": Warning, failed to obtain" \
[875bc8b]834 " block device size.\n");
835
836 if (user_fs_size == 0) {
837 printf(NAME ": You must specify the" \
838 " filesystem size.\n");
839 return 1;
840 }
[9744f2d]841 } else {
[8efc4c1]842 printf("Block device has %" PRIuOFF64 " blocks.\n",
[ffee7bf]843 cfg.volume_count);
[9744f2d]844 }
845
[ef144ef]846 if (user_fs_size != 0) {
847 if (user_fs_size > cfg.volume_count * cfg.sector_size) {
848 printf(NAME ": Error, the device is not big enough"
[31718d1]849 " to create a filesystem of"
[ef144ef]850 " the specified size.\n");
851 return 1;
852 }
853
854 cfg.volume_count = user_fs_size / cfg.sector_size;
855 }
856
[9744f2d]857 cfg_params_initialize(&cfg);
[6cf9aeb]858 cfg_print_info(&cfg);
[116cb91]859
[0f22528]860 if (cfg.total_clusters <= cfg.allocated_clusters + 2) {
[d7f09583]861 printf(NAME ": Error, insufficient disk space on device.\n");
862 return 2;
863 }
864
[8efc4c1]865 printf("Writing the allocation table.\n");
[9ce1acf]866
[b200230]867 /* Initialize the FAT table */
[7f381e5]868 rc = fat_initialize(service_id, &cfg);
[50e754e]869 if (rc != EOK) {
[55dbaeb]870 printf(NAME ": Error, failed to write the FAT to disk\n");
[50e754e]871 return 2;
872 }
873
[78de7be2]874 /* Allocate clusters for the bitmap */
[da34d61d]875 rc = fat_allocate_clusters(service_id, &cfg, cfg.bitmap_cluster,
[78de7be2]876 div_round_up(cfg.bitmap_size, cfg.cluster_size));
877 if (rc != EOK) {
[528acda]878 printf(NAME ": Error, failed to allocate" \
879 " clusters for bitmap.\n");
[78de7be2]880 return 2;
881 }
882
[da34d61d]883 next_cls = cfg.bitmap_cluster +
[78de7be2]884 div_round_up(cfg.bitmap_size, cfg.cluster_size);
[557e5b13]885 cfg.upcase_table_cluster = next_cls;
[78de7be2]886
887 /* Allocate clusters for the upcase table */
888 rc = fat_allocate_clusters(service_id, &cfg, next_cls,
889 div_round_up(sizeof(upcase_table), cfg.cluster_size));
890 if (rc != EOK) {
[528acda]891 printf(NAME ":Error, failed to allocate clusters" \
892 " for the upcase table.\n");
[78de7be2]893 return 2;
894 }
895
[69f9cf5]896 next_cls += div_round_up(sizeof(upcase_table), cfg.cluster_size);
897 cfg.rootdir_cluster = next_cls;
898
899 /* Allocate a cluster for the root directory entry */
900 rc = fat_allocate_clusters(service_id, &cfg, next_cls, 1);
901 if (rc != EOK) {
[528acda]902 printf(NAME ": Error, failed to allocate cluster" \
903 " for the root dentry.\n");
[69f9cf5]904 return 2;
905 }
906
[8efc4c1]907 printf("Writing the allocation bitmap.\n");
[9ce1acf]908
[b200230]909 /* Write the allocation bitmap to disk */
[557e5b13]910 rc = bitmap_write(service_id, &cfg);
911 if (rc != EOK) {
912 printf(NAME ": Error, failed to write the allocation" \
913 " bitmap to disk.\n");
914 return 2;
915 }
916
[8efc4c1]917 printf("Writing the upcase table.\n");
[9ce1acf]918
[b200230]919 /* Write the upcase table to disk */
[89a0a827]920 rc = upcase_table_write(service_id, &cfg);
921 if (rc != EOK) {
[528acda]922 printf(NAME ": Error, failed to write the" \
923 " upcase table to disk.\n");
[89a0a827]924 return 2;
925 }
926
[8efc4c1]927 printf("Writing the root directory.\n");
[9ce1acf]928
[da34d61d]929 rc = root_dentries_write(service_id, &cfg);
930 if (rc != EOK) {
931 printf(NAME ": Error, failed to write the root directory" \
932 " entries to disk.\n");
933 return 2;
934 }
935
[8efc4c1]936 printf("Writing the boot sectors.\n");
[0f22528]937
[557e5b13]938 rc = bootsec_write(service_id, &cfg);
939 if (rc != EOK) {
940 printf(NAME ": Error, failed to write the VBR to disk\n");
941 return 2;
942 }
943
[9ce1acf]944 printf("Success.\n");
945
[dd22cc4]946 return 0;
947}
[9744f2d]948
[e517715]949/**
950 * @}
951 */
952
Note: See TracBrowser for help on using the repository browser.