source: mainline/uspace/app/mkexfat/mkexfat.c@ d92060f

Last change on this file since d92060f was 18b6a88, checked in by Jiri Svoboda <jiri@…>, 8 years ago

More ccheck fixes, sometimes with manual intervention.

  • Property mode set to 100644
File size: 24.2 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>
[8d2dd7f2]40#include <stdbool.h>
41#include <stdint.h>
[f73b291]42#include <block.h>
[ffee7bf]43#include <assert.h>
44#include <errno.h>
[38d150e]45#include <stdlib.h>
[ffee7bf]46#include <byteorder.h>
[6cf9aeb]47#include <align.h>
[da34d61d]48#include <str.h>
[ef144ef]49#include <getopt.h>
[062d900]50#include <macros.h>
[ffee7bf]51#include "exfat.h"
[367014a]52#include "upcase.h"
[dd22cc4]53
54#define NAME "mkexfat"
55
[9744f2d]56/** First sector of the FAT */
57#define FAT_SECTOR_START 128
58
[55dbaeb]59/** First sector of the Main Extended Boot Region */
60#define EBS_SECTOR_START 1
61
[dabe1664]62/** First sector of the Main Extended Boot Region Backup */
63#define EBS_BACKUP_SECTOR_START 13
64
[3938375]65/** First sector of the Main Boot Sector */
66#define MBS_SECTOR 0
[dabe1664]67
[3938375]68/** First sector of the Main Boot Sector Backup */
69#define MBS_BACKUP_SECTOR 12
[dabe1664]70
[8271ae37]71/** VBR Checksum sector */
72#define VBR_CHECKSUM_SECTOR 11
73
74/** VBR Backup Checksum sector */
75#define VBR_BACKUP_CHECKSUM_SECTOR 23
76
[55dbaeb]77/** Size of the Main Extended Boot Region */
78#define EBS_SIZE 8
79
[9744f2d]80/** Divide and round up. */
81#define div_round_up(a, b) (((a) + (b) - 1) / (b))
82
[ffee7bf]83/** The default size of each cluster is 4096 byte */
84#define DEFAULT_CLUSTER_SIZE 4096
85
[78de7be2]86/** Index of the first free cluster on the device */
87#define FIRST_FREE_CLUSTER 2
88
[552efe3]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;
[f3504c1]103 const char *label;
[9744f2d]104} exfat_cfg_t;
105
[81b1db8]106static unsigned log2i(unsigned n);
[dabe1664]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
[b7fd2a0]114static errno_t
[dabe1664]115ebs_write(service_id_t service_id, exfat_cfg_t *cfg,
116 int base, uint32_t *chksum);
117
[b7fd2a0]118static errno_t
[6aeab59c]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
[ef144ef]124static struct option const long_options[] = {
[18b6a88]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' },
[ef144ef]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"
[f3504c1]135 "-s, --fs-size ## Specify the filesystem size (sectors)\n"
136 " --label ## Volume label\n");
[116cb91]137}
138
[9744f2d]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{
[5dbd696]146 unsigned long fat_bytes;
[0f22528]147 unsigned long fat_cls;
[5dbd696]148 aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
149 cfg->sector_size;
[ef144ef]150
151 if (cfg->cluster_size != 0) {
152 /* The user already choose the cluster size he wants */
[d7f09583]153 cfg->total_clusters = volume_bytes / cfg->cluster_size;
[ef144ef]154 goto skip_cluster_size_set;
155 }
[ffee7bf]156
[d7f09583]157 cfg->total_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
[ffee7bf]158 cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
159
160 /* Compute the required cluster size to index
[e15f4d5]161 * the entire storage device and to keep the FAT
162 * size less or equal to 64 Mb.
[ffee7bf]163 */
[d7f09583]164 while (cfg->total_clusters > 16000000ULL &&
[ffee7bf]165 (cfg->cluster_size < 32 * 1024 * 1024)) {
166
167 cfg->cluster_size <<= 1;
[b6eb5da]168 cfg->total_clusters = volume_bytes / cfg->cluster_size;
[ffee7bf]169 }
[5dbd696]170
[ef144ef]171skip_cluster_size_set:
172
[5dbd696]173 /* Compute the FAT size in sectors */
[d7f09583]174 fat_bytes = (cfg->total_clusters + 3) * sizeof(uint32_t);
[5dbd696]175 cfg->fat_sector_count = div_round_up(fat_bytes, cfg->sector_size);
[f8973122]176
[6cf9aeb]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
[9bbe54f]181 /* Subtract the FAT space from the total
[b6eb5da]182 * number of available clusters.
183 */
[0f22528]184 fat_cls = div_round_up((cfg->data_start_sector -
[b6eb5da]185 FAT_SECTOR_START) * cfg->sector_size,
186 cfg->cluster_size);
[0f22528]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;
[b6eb5da]193
[6aeab59c]194 /* Compute the bitmap size */
[d7f09583]195 cfg->bitmap_size = div_round_up(cfg->total_clusters, 8);
[6aeab59c]196
[a69e396]197 /* Compute the number of clusters reserved to the bitmap */
[6aeab59c]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++;
[a69e396]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);
[6aeab59c]207
[89a0a827]208 /* Will be set later */
[6cf9aeb]209 cfg->rootdir_cluster = 0;
210
[da34d61d]211 /* Bitmap always starts at the first free cluster */
212 cfg->bitmap_cluster = FIRST_FREE_CLUSTER;
213
[f8973122]214 /* The first sector of the partition is zero */
215 cfg->volume_start = 0;
[9744f2d]216}
217
[6cf9aeb]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{
[8efc4c1]225 printf("Sector size: %lu\n",
[e04bfbf0]226 (unsigned long) cfg->sector_size);
[8efc4c1]227 printf("Cluster size: %lu\n",
[e04bfbf0]228 (unsigned long) cfg->cluster_size);
[8efc4c1]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)
[7f381e5]234 div_round_up(cfg->bitmap_size, cfg->cluster_size));
[8efc4c1]235 printf("Upcase table size: %lu\n", (unsigned long)
[7f381e5]236 div_round_up(sizeof(upcase_table), cfg->cluster_size));
[6cf9aeb]237}
238
[3938375]239/** Initialize the Main Boot Sector fields.
[9744f2d]240 *
[3938375]241 * @param mbs Pointer to the Main Boot Sector structure.
[9744f2d]242 * @param cfg Pointer to the exFAT configuration structure.
[dabe1664]243 * @return Initial checksum value.
[9744f2d]244 */
[dabe1664]245static uint32_t
[3938375]246vbr_initialize(exfat_bs_t *mbs, exfat_cfg_t *cfg)
[9744f2d]247{
248 /* Fill the structure with zeroes */
[3938375]249 memset(mbs, 0, sizeof(exfat_bs_t));
[9744f2d]250
251 /* Init Jump Boot section */
[3938375]252 mbs->jump[0] = 0xEB;
253 mbs->jump[1] = 0x76;
254 mbs->jump[2] = 0x90;
[9744f2d]255
256 /* Set the filesystem name */
[3938375]257 memcpy(mbs->oem_name, "EXFAT ", sizeof(mbs->oem_name));
[9744f2d]258
[3938375]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);
[6cf9aeb]264
[d7f09583]265 mbs->data_clusters = host2uint32_t_le(cfg->total_clusters);
[6cf9aeb]266
[557e5b13]267 mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
[09c954b]268 mbs->volume_serial = host2uint32_t_le(0xe1028172);
[3938375]269 mbs->version.major = 1;
270 mbs->version.minor = 0;
271 mbs->volume_flags = host2uint16_t_le(0);
[81b1db8]272 mbs->bytes_per_sector = log2i(cfg->sector_size);
273 mbs->sec_per_cluster = log2i(cfg->cluster_size / cfg->sector_size);
[ffee7bf]274
275 /* Maximum cluster size is 32 Mb */
[3938375]276 assert((mbs->bytes_per_sector + mbs->sec_per_cluster) <= 25);
[ffee7bf]277
[3938375]278 mbs->fat_count = 1;
279 mbs->drive_no = 0x80;
280 mbs->allocated_percent = 0;
281 mbs->signature = host2uint16_t_le(0xAA55);
[dabe1664]282
[3938375]283 return vbr_checksum_start(mbs, sizeof(exfat_bs_t));
[dabe1664]284}
285
[b7fd2a0]286static errno_t
[dabe1664]287bootsec_write(service_id_t service_id, exfat_cfg_t *cfg)
288{
[3938375]289 exfat_bs_t mbs;
[dabe1664]290 uint32_t vbr_checksum;
[8271ae37]291 uint32_t *chksum_sector;
[b7fd2a0]292 errno_t rc;
[8271ae37]293 unsigned idx;
294
295 chksum_sector = calloc(cfg->sector_size, sizeof(uint8_t));
296 if (!chksum_sector)
297 return ENOMEM;
[dabe1664]298
[3938375]299 vbr_checksum = vbr_initialize(&mbs, cfg);
[dabe1664]300
[3938375]301 /* Write the Main Boot Sector to disk */
302 rc = block_write_direct(service_id, MBS_SECTOR, 1, &mbs);
[dabe1664]303 if (rc != EOK)
[8271ae37]304 goto exit;
[dabe1664]305
[3938375]306 /* Write the Main extended boot sectors to disk */
307 rc = ebs_write(service_id, cfg, EBS_SECTOR_START, &vbr_checksum);
[dabe1664]308 if (rc != EOK)
[8271ae37]309 goto exit;
[dabe1664]310
[3938375]311 /* Write the Main Boot Sector backup to disk */
312 rc = block_write_direct(service_id, MBS_BACKUP_SECTOR, 1, &mbs);
[dabe1664]313 if (rc != EOK)
[8271ae37]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;
[dabe1664]325
[8271ae37]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;
[dabe1664]331
[1b20da0]332 /* Write the Main extended boot sectors backup to disk */
[8271ae37]333 rc = ebs_write(service_id, cfg,
[9587b37]334 EBS_BACKUP_SECTOR_START, &vbr_checksum);
[8271ae37]335
336exit:
337 free(chksum_sector);
338 return rc;
[9744f2d]339}
340
[55dbaeb]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.
[dabe1664]345 * @param base Base sector of the EBS.
[cde999a]346 * @return EOK on success or an error code.
[55dbaeb]347 */
[b7fd2a0]348static errno_t
[528acda]349ebs_write(service_id_t service_id, exfat_cfg_t *cfg, int base,
350 uint32_t *chksum)
[55dbaeb]351{
352 uint32_t *ebs = calloc(cfg->sector_size, sizeof(uint8_t));
[d5c1051]353 int i;
[b7fd2a0]354 errno_t rc;
[55dbaeb]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) {
[dabe1664]362 vbr_checksum_update(ebs, cfg->sector_size, chksum);
363
364 rc = block_write_direct(service_id,
[ff415f62]365 i + base, 1, ebs);
[55dbaeb]366
367 if (rc != EOK)
368 goto exit;
369 }
370
[dabe1664]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);
[ff415f62]385
[2601383]386 rc = block_write_direct(service_id, i + base, 1, ebs);
[dabe1664]387 if (rc != EOK)
388 goto exit;
389
[55dbaeb]390exit:
391 free(ebs);
392 return rc;
393}
394
[7f381e5]395/** Initialize the FAT table.
[50e754e]396 *
[3938375]397 * @param service_id The service id.
[50e754e]398 * @param cfg Pointer to the exfat_cfg structure.
[cde999a]399 * @return EOK on success or an error code.
[50e754e]400 */
[b7fd2a0]401static errno_t
[7f381e5]402fat_initialize(service_id_t service_id, exfat_cfg_t *cfg)
[50e754e]403{
404 unsigned long i;
405 uint32_t *pfat;
[b7fd2a0]406 errno_t rc;
[50e754e]407
[6aeab59c]408 pfat = calloc(cfg->sector_size, 1);
[50e754e]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
[78de7be2]419 pfat[0] = pfat[1] = 0;
[50e754e]420
[6aeab59c]421 for (i = 1; i < cfg->fat_sector_count; ++i) {
[69c4172]422 rc = block_write_direct(service_id,
[b0fecad]423 FAT_SECTOR_START + i, 1, pfat);
[50e754e]424 if (rc != EOK)
425 goto error;
426 }
427
428error:
429 free(pfat);
430 return rc;
431}
432
[78de7be2]433/** Allocate a given number of clusters and create a cluster chain.
434 *
435 * @param service_id The service id.
[aa37d6f]436 * @param cfg Pointer to the exfat configuration structure.
[78de7be2]437 * @param cur_cls Cluster index from where to start the allocation.
438 * @param ncls Number of clusters to allocate.
[cde999a]439 * @return EOK on success or an error code.
[78de7be2]440 */
[b7fd2a0]441static errno_t
[78de7be2]442fat_allocate_clusters(service_id_t service_id, exfat_cfg_t *cfg,
443 uint32_t cur_cls, unsigned long ncls)
444{
[b7fd2a0]445 errno_t rc;
[78de7be2]446 unsigned const fat_entries = cfg->sector_size / sizeof(uint32_t);
[69f9cf5]447 aoff64_t fat_sec = cur_cls / fat_entries + FAT_SECTOR_START;
[78de7be2]448 uint32_t *fat;
[df4fbe1]449 uint32_t next_cls = cur_cls;
[78de7be2]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)
[df4fbe1]466 fat[cur_cls] = host2uint32_t_le(++next_cls);
[78de7be2]467
468 if (cur_cls == fat_entries) {
[b200230]469 /* This sector is full, there are no more free entries,
[2601383]470 * commit the changes to disk and restart from the next
471 * sector.
[b200230]472 */
[78de7be2]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;
[b200230]478 } else if (ncls == 1) {
479 /* This is the last cluster of this chain, mark it
[ba55c194]480 * with EOF.
[b200230]481 */
[78de7be2]482 fat[cur_cls] = host2uint32_t_le(0xFFFFFFFF);
[b200230]483 }
[78de7be2]484
485 rc = block_write_direct(service_id, fat_sec, 1, fat);
486
487exit:
488 free(fat);
489 return rc;
490}
491
[6aeab59c]492/** Initialize the allocation bitmap.
493 *
494 * @param service_id The service id.
495 * @param cfg Pointer to the exfat configuration structure.
[cde999a]496 * @return EOK on success or an error code.
[6aeab59c]497 */
[b7fd2a0]498static errno_t
[6aeab59c]499bitmap_write(service_id_t service_id, exfat_cfg_t *cfg)
500{
501 unsigned long i, sec;
502 unsigned long allocated_cls;
[b7fd2a0]503 errno_t rc = EOK;
[3467821]504 bool need_reset = true;
[6aeab59c]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) {
[3467821]516 if (need_reset) {
517 need_reset = false;
518 memset(bitmap, 0, cfg->sector_size);
519 }
[6aeab59c]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
[cbd8a72]530 allocated_cls -= i;
[3467821]531 need_reset = true;
[6aeab59c]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
[89a0a827]545/** Write the upcase table to disk. */
[b7fd2a0]546static errno_t
[89a0a827]547upcase_table_write(service_id_t service_id, exfat_cfg_t *cfg)
548{
[b7fd2a0]549 errno_t rc = EOK;
[89a0a827]550 aoff64_t start_sec, nsecs, i;
551 uint8_t *table_ptr;
[552efe3]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;
[89a0a827]558
[b200230]559 /* Compute the start sector of the upcase table */
[89a0a827]560 start_sec = cfg->data_start_sector;
[552efe3]561 start_sec += ((cfg->upcase_table_cluster - 2) * cfg->cluster_size) /
[89a0a827]562 cfg->sector_size;
563
[b200230]564 /* Compute the number of sectors needed to store the table on disk */
[89a0a827]565 nsecs = div_round_up(sizeof(upcase_table), cfg->sector_size);
566 table_ptr = (uint8_t *) upcase_table;
567
[552efe3]568 for (i = 0; i < nsecs; ++i,
569 table_ptr += min(table_size, cfg->sector_size),
570 table_size -= cfg->sector_size) {
571
[b200230]572 if (table_size < cfg->sector_size) {
573 /* Reset the content of the unused part
574 * of the last sector.
575 */
[552efe3]576 memset(buf, 0, cfg->sector_size);
[b200230]577 }
[552efe3]578 memcpy(buf, table_ptr, min(table_size, cfg->sector_size));
579
[89a0a827]580 rc = block_write_direct(service_id,
[552efe3]581 start_sec + i, 1, buf);
[89a0a827]582 if (rc != EOK)
[8271ae37]583 goto exit;
[89a0a827]584 }
585
[8271ae37]586exit:
[552efe3]587 free(buf);
588 return rc;
[89a0a827]589}
590
[da34d61d]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.
[cde999a]595 * @return EOK on success or an error code.
[da34d61d]596 */
[b7fd2a0]597static errno_t
[da34d61d]598root_dentries_write(service_id_t service_id, exfat_cfg_t *cfg)
599{
600 exfat_dentry_t *d;
601 aoff64_t rootdir_sec;
[f3504c1]602 uint16_t wlabel[EXFAT_VOLLABEL_LEN + 1];
[b7fd2a0]603 errno_t rc;
[da34d61d]604 uint8_t *data;
[ebea7acf]605 unsigned long i;
[da34d61d]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
[f3504c1]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 }
[da34d61d]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);
[ebea7acf]655 if (rc != EOK)
656 goto exit;
[da34d61d]657
[ebea7acf]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:
[da34d61d]669 free(data);
670 return rc;
671}
672
[dabe1664]673/** Given a number (n), returns the result of log2(n).
[ffee7bf]674 *
675 * It works only if n is a power of two.
676 */
[6cf9aeb]677static unsigned
[81b1db8]678log2i(unsigned n)
[ffee7bf]679{
680 unsigned r;
681
[84239b1]682 r = 0;
683 while (n >> r != 1)
684 ++r;
[ffee7bf]685
686 return r;
687}
688
[dabe1664]689/** Initialize the VBR checksum calculation */
690static uint32_t
691vbr_checksum_start(void const *data, size_t nbytes)
692{
693 uint32_t checksum = 0;
694 size_t index;
695 uint8_t const *octets = (uint8_t *) data;
696
697 for (index = 0; index < nbytes; ++index) {
698 if (index == 106 || index == 107 || index == 112) {
699 /* Skip volume_flags and allocated_percent fields */
700 continue;
701 }
702
[528acda]703 checksum = ((checksum << 31) | (checksum >> 1)) +
704 octets[index];
[dabe1664]705 }
706
707 return checksum;
708}
709
710/** Update the VBR checksum */
711static void
712vbr_checksum_update(void const *data, size_t nbytes, uint32_t *checksum)
713{
714 size_t index;
715 uint8_t const *octets = (uint8_t *) data;
716
[528acda]717 for (index = 0; index < nbytes; ++index) {
718 *checksum = ((*checksum << 31) | (*checksum >> 1)) +
719 octets[index];
720 }
[dabe1664]721}
722
[392bd67c]723/** Compute the checksum of the upcase table.
724 *
725 * @param data Pointer to the upcase table.
726 * @param nbytes size of the upcase table in bytes.
727 * @return Checksum value.
728 */
729static uint32_t
730upcase_table_checksum(void const *data, size_t nbytes)
731{
732 size_t index;
733 uint32_t chksum = 0;
734 uint8_t const *octets = (uint8_t *) data;
735
736 for (index = 0; index < nbytes; ++index)
737 chksum = ((chksum << 31) | (chksum >> 1)) + octets[index];
738
739 return chksum;
740}
741
[aa37d6f]742/** Check if a given number is a power of two.
743 *
744 * @param n The number to check.
745 * @return true if n is a power of two, false otherwise.
746 */
[ef144ef]747static bool
748is_power_of_two(unsigned long n)
749{
750 if (n == 0)
751 return false;
752
753 return (n & (n - 1)) == 0;
754}
755
[dd22cc4]756int main (int argc, char **argv)
757{
[9744f2d]758 exfat_cfg_t cfg;
[78de7be2]759 uint32_t next_cls;
[116cb91]760 char *dev_path;
761 service_id_t service_id;
[b7fd2a0]762 errno_t rc;
[d5c1051]763 int c, opt_ind;
[ef144ef]764 aoff64_t user_fs_size = 0;
[116cb91]765
766 if (argc < 2) {
767 printf(NAME ": Error, argument missing\n");
768 usage();
769 return 1;
770 }
771
[ef144ef]772 cfg.cluster_size = 0;
[f3504c1]773 cfg.label = NULL;
[ef144ef]774
[84239b1]775 c = 0;
776 optind = 0;
777 opt_ind = 0;
778 while (c != -1) {
[f3504c1]779 c = getopt_long(argc, argv, "hs:c:L:",
[ef144ef]780 long_options, &opt_ind);
781 switch (c) {
782 case 'h':
783 usage();
784 return 0;
785 case 's':
786 user_fs_size = (aoff64_t) strtol(optarg, NULL, 10);
787 break;
788
789 case 'c':
[00af658]790 cfg.cluster_size = strtol(optarg, NULL, 10) * 1024;
[ef144ef]791 if (cfg.cluster_size < 4096) {
792 printf(NAME ": Error, cluster size can't"
793 " be less than 4096 byte.\n");
794 return 1;
795 } else if (cfg.cluster_size > 32 * 1024 * 1024) {
796 printf(NAME ": Error, cluster size can't"
797 " be greater than 32 Mb");
798 return 1;
799 }
800
801 if (!is_power_of_two(cfg.cluster_size)) {
802 printf(NAME ": Error, the size of the cluster"
803 " must be a power of two.\n");
804 return 1;
805 }
806 break;
[f3504c1]807 case 'L':
808 cfg.label = optarg;
809 break;
[ef144ef]810 }
811 }
[116cb91]812
[ef144ef]813 argv += optind;
[116cb91]814 dev_path = *argv;
815
[8d8ad19]816 if (!dev_path) {
817 printf(NAME ": Error, you must specify a valid block"
818 " device.\n");
819 usage();
820 return 1;
821 }
822
[8efc4c1]823 printf("Device = %s\n", dev_path);
[116cb91]824
825 rc = loc_service_get_id(dev_path, &service_id, 0);
826 if (rc != EOK) {
[ffee7bf]827 printf(NAME ": Error resolving device `%s'.\n", dev_path);
[116cb91]828 return 2;
829 }
830
[fc22069]831 rc = block_init(service_id, 2048);
[116cb91]832 if (rc != EOK) {
833 printf(NAME ": Error initializing libblock.\n");
834 return 2;
835 }
836
[ffee7bf]837 rc = block_get_bsize(service_id, &cfg.sector_size);
[116cb91]838 if (rc != EOK) {
[875bc8b]839 printf(NAME ": Error determining device sector size.\n");
[116cb91]840 return 2;
841 }
842
[b4b3cb05]843 user_fs_size *= cfg.sector_size;
[875bc8b]844 if (user_fs_size > 0 && user_fs_size < 1024 * 1024) {
[b4b3cb05]845 printf(NAME ": Error, fs size can't be less"
846 " than 1 Mb.\n");
847 return 1;
848 }
849
850
[ffee7bf]851 if (cfg.sector_size > 4096) {
[84239b1]852 printf(NAME ": Error, sector size can't be greater"
[ffee7bf]853 " than 4096 bytes.\n");
854 return 2;
855 }
856
857 rc = block_get_nblocks(service_id, &cfg.volume_count);
[9744f2d]858 if (rc != EOK) {
[84239b1]859 printf(NAME ": Warning, failed to obtain"
[875bc8b]860 " block device size.\n");
861
862 if (user_fs_size == 0) {
[84239b1]863 printf(NAME ": You must specify the"
[875bc8b]864 " filesystem size.\n");
865 return 1;
866 }
[9744f2d]867 } else {
[8efc4c1]868 printf("Block device has %" PRIuOFF64 " blocks.\n",
[ffee7bf]869 cfg.volume_count);
[9744f2d]870 }
871
[ef144ef]872 if (user_fs_size != 0) {
873 if (user_fs_size > cfg.volume_count * cfg.sector_size) {
874 printf(NAME ": Error, the device is not big enough"
[31718d1]875 " to create a filesystem of"
[ef144ef]876 " the specified size.\n");
877 return 1;
878 }
879
880 cfg.volume_count = user_fs_size / cfg.sector_size;
881 }
882
[9744f2d]883 cfg_params_initialize(&cfg);
[6cf9aeb]884 cfg_print_info(&cfg);
[116cb91]885
[0f22528]886 if (cfg.total_clusters <= cfg.allocated_clusters + 2) {
[d7f09583]887 printf(NAME ": Error, insufficient disk space on device.\n");
888 return 2;
889 }
890
[8efc4c1]891 printf("Writing the allocation table.\n");
[9ce1acf]892
[b200230]893 /* Initialize the FAT table */
[7f381e5]894 rc = fat_initialize(service_id, &cfg);
[50e754e]895 if (rc != EOK) {
[55dbaeb]896 printf(NAME ": Error, failed to write the FAT to disk\n");
[50e754e]897 return 2;
898 }
899
[78de7be2]900 /* Allocate clusters for the bitmap */
[da34d61d]901 rc = fat_allocate_clusters(service_id, &cfg, cfg.bitmap_cluster,
[78de7be2]902 div_round_up(cfg.bitmap_size, cfg.cluster_size));
903 if (rc != EOK) {
[84239b1]904 printf(NAME ": Error, failed to allocate"
[528acda]905 " clusters for bitmap.\n");
[78de7be2]906 return 2;
907 }
908
[da34d61d]909 next_cls = cfg.bitmap_cluster +
[78de7be2]910 div_round_up(cfg.bitmap_size, cfg.cluster_size);
[557e5b13]911 cfg.upcase_table_cluster = next_cls;
[78de7be2]912
913 /* Allocate clusters for the upcase table */
914 rc = fat_allocate_clusters(service_id, &cfg, next_cls,
915 div_round_up(sizeof(upcase_table), cfg.cluster_size));
916 if (rc != EOK) {
[84239b1]917 printf(NAME ":Error, failed to allocate clusters"
[528acda]918 " for the upcase table.\n");
[78de7be2]919 return 2;
920 }
921
[69f9cf5]922 next_cls += div_round_up(sizeof(upcase_table), cfg.cluster_size);
923 cfg.rootdir_cluster = next_cls;
924
925 /* Allocate a cluster for the root directory entry */
926 rc = fat_allocate_clusters(service_id, &cfg, next_cls, 1);
927 if (rc != EOK) {
[84239b1]928 printf(NAME ": Error, failed to allocate cluster"
[528acda]929 " for the root dentry.\n");
[69f9cf5]930 return 2;
931 }
932
[8efc4c1]933 printf("Writing the allocation bitmap.\n");
[9ce1acf]934
[b200230]935 /* Write the allocation bitmap to disk */
[557e5b13]936 rc = bitmap_write(service_id, &cfg);
937 if (rc != EOK) {
[84239b1]938 printf(NAME ": Error, failed to write the allocation"
[557e5b13]939 " bitmap to disk.\n");
940 return 2;
941 }
942
[8efc4c1]943 printf("Writing the upcase table.\n");
[9ce1acf]944
[b200230]945 /* Write the upcase table to disk */
[89a0a827]946 rc = upcase_table_write(service_id, &cfg);
947 if (rc != EOK) {
[84239b1]948 printf(NAME ": Error, failed to write the"
[528acda]949 " upcase table to disk.\n");
[89a0a827]950 return 2;
951 }
952
[8efc4c1]953 printf("Writing the root directory.\n");
[9ce1acf]954
[da34d61d]955 rc = root_dentries_write(service_id, &cfg);
956 if (rc != EOK) {
[84239b1]957 printf(NAME ": Error, failed to write the root directory"
[da34d61d]958 " entries to disk.\n");
959 return 2;
960 }
961
[8efc4c1]962 printf("Writing the boot sectors.\n");
[0f22528]963
[557e5b13]964 rc = bootsec_write(service_id, &cfg);
965 if (rc != EOK) {
966 printf(NAME ": Error, failed to write the VBR to disk\n");
967 return 2;
968 }
969
[9ce1acf]970 printf("Success.\n");
971
[dd22cc4]972 return 0;
973}
[9744f2d]974
[e517715]975/**
976 * @}
977 */
978
Note: See TracBrowser for help on using the repository browser.