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