1 | /*
|
---|
2 | * Copyright (c) 2012 Maurizio Lombardi
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup fs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file mkexfat.c
|
---|
35 | * @brief Tool for creating new exFAT file systems.
|
---|
36 | *
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <block.h>
|
---|
41 | #include <assert.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <malloc.h>
|
---|
44 | #include <byteorder.h>
|
---|
45 | #include <align.h>
|
---|
46 | #include <sys/types.h>
|
---|
47 | #include <sys/typefmt.h>
|
---|
48 | #include <stdbool.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 |
|
---|
91 | typedef struct exfat_cfg {
|
---|
92 | aoff64_t volume_start;
|
---|
93 | aoff64_t volume_count;
|
---|
94 | unsigned long fat_sector_count;
|
---|
95 | unsigned long data_start_sector;
|
---|
96 | unsigned long rootdir_cluster;
|
---|
97 | unsigned long upcase_table_cluster;
|
---|
98 | unsigned long bitmap_cluster;
|
---|
99 | unsigned long total_clusters;
|
---|
100 | unsigned long allocated_clusters;
|
---|
101 | size_t bitmap_size;
|
---|
102 | size_t sector_size;
|
---|
103 | size_t cluster_size;
|
---|
104 | } exfat_cfg_t;
|
---|
105 |
|
---|
106 |
|
---|
107 | static unsigned log2(unsigned n);
|
---|
108 |
|
---|
109 | static uint32_t
|
---|
110 | vbr_checksum_start(void const *octets, size_t nbytes);
|
---|
111 |
|
---|
112 | static void
|
---|
113 | vbr_checksum_update(void const *octets, size_t nbytes, uint32_t *checksum);
|
---|
114 |
|
---|
115 | static int
|
---|
116 | ebs_write(service_id_t service_id, exfat_cfg_t *cfg,
|
---|
117 | int base, uint32_t *chksum);
|
---|
118 |
|
---|
119 | static int
|
---|
120 | bitmap_write(service_id_t service_id, exfat_cfg_t *cfg);
|
---|
121 |
|
---|
122 | static uint32_t
|
---|
123 | upcase_table_checksum(void const *data, size_t nbytes);
|
---|
124 |
|
---|
125 | static 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 |
|
---|
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 | }
|
---|
137 |
|
---|
138 | /** Initialize the exFAT params structure.
|
---|
139 | *
|
---|
140 | * @param cfg Pointer to the exFAT params structure to initialize.
|
---|
141 | */
|
---|
142 | static void
|
---|
143 | cfg_params_initialize(exfat_cfg_t *cfg)
|
---|
144 | {
|
---|
145 | unsigned long fat_bytes;
|
---|
146 | unsigned long fat_cls;
|
---|
147 | aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
|
---|
148 | cfg->sector_size;
|
---|
149 |
|
---|
150 | if (cfg->cluster_size != 0) {
|
---|
151 | /* The user already choose the cluster size he wants */
|
---|
152 | cfg->total_clusters = volume_bytes / cfg->cluster_size;
|
---|
153 | goto skip_cluster_size_set;
|
---|
154 | }
|
---|
155 |
|
---|
156 | cfg->total_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
|
---|
157 | cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
|
---|
158 |
|
---|
159 | /* Compute the required cluster size to index
|
---|
160 | * the entire storage device and to keep the FAT
|
---|
161 | * size less or equal to 64 Mb.
|
---|
162 | */
|
---|
163 | while (cfg->total_clusters > 16000000ULL &&
|
---|
164 | (cfg->cluster_size < 32 * 1024 * 1024)) {
|
---|
165 |
|
---|
166 | cfg->cluster_size <<= 1;
|
---|
167 | cfg->total_clusters = volume_bytes / cfg->cluster_size;
|
---|
168 | }
|
---|
169 |
|
---|
170 | skip_cluster_size_set:
|
---|
171 |
|
---|
172 | /* Compute the FAT size in sectors */
|
---|
173 | fat_bytes = (cfg->total_clusters + 3) * sizeof(uint32_t);
|
---|
174 | cfg->fat_sector_count = div_round_up(fat_bytes, cfg->sector_size);
|
---|
175 |
|
---|
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 |
|
---|
180 | /* Subtract the FAT space from the total
|
---|
181 | * number of available clusters.
|
---|
182 | */
|
---|
183 | fat_cls = div_round_up((cfg->data_start_sector -
|
---|
184 | FAT_SECTOR_START) * cfg->sector_size,
|
---|
185 | cfg->cluster_size);
|
---|
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;
|
---|
192 |
|
---|
193 | /* Compute the bitmap size */
|
---|
194 | cfg->bitmap_size = div_round_up(cfg->total_clusters, 8);
|
---|
195 |
|
---|
196 | /* Compute the number of clusters reserved to the bitmap */
|
---|
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++;
|
---|
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);
|
---|
206 |
|
---|
207 | /* Will be set later */
|
---|
208 | cfg->rootdir_cluster = 0;
|
---|
209 |
|
---|
210 | /* Bitmap always starts at the first free cluster */
|
---|
211 | cfg->bitmap_cluster = FIRST_FREE_CLUSTER;
|
---|
212 |
|
---|
213 | /* The first sector of the partition is zero */
|
---|
214 | cfg->volume_start = 0;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** Prints the exFAT structure values
|
---|
218 | *
|
---|
219 | * @param cfg Pointer to the exfat_cfg_t structure.
|
---|
220 | */
|
---|
221 | static void
|
---|
222 | cfg_print_info(exfat_cfg_t *cfg)
|
---|
223 | {
|
---|
224 | printf("Sector size: %lu\n",
|
---|
225 | (unsigned long) cfg->sector_size);
|
---|
226 | printf("Cluster size: %lu\n",
|
---|
227 | (unsigned long) cfg->cluster_size);
|
---|
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)
|
---|
233 | div_round_up(cfg->bitmap_size, cfg->cluster_size));
|
---|
234 | printf("Upcase table size: %lu\n", (unsigned long)
|
---|
235 | div_round_up(sizeof(upcase_table), cfg->cluster_size));
|
---|
236 | }
|
---|
237 |
|
---|
238 | /** Initialize the Main Boot Sector fields.
|
---|
239 | *
|
---|
240 | * @param mbs Pointer to the Main Boot Sector structure.
|
---|
241 | * @param cfg Pointer to the exFAT configuration structure.
|
---|
242 | * @return Initial checksum value.
|
---|
243 | */
|
---|
244 | static uint32_t
|
---|
245 | vbr_initialize(exfat_bs_t *mbs, exfat_cfg_t *cfg)
|
---|
246 | {
|
---|
247 | /* Fill the structure with zeroes */
|
---|
248 | memset(mbs, 0, sizeof(exfat_bs_t));
|
---|
249 |
|
---|
250 | /* Init Jump Boot section */
|
---|
251 | mbs->jump[0] = 0xEB;
|
---|
252 | mbs->jump[1] = 0x76;
|
---|
253 | mbs->jump[2] = 0x90;
|
---|
254 |
|
---|
255 | /* Set the filesystem name */
|
---|
256 | memcpy(mbs->oem_name, "EXFAT ", sizeof(mbs->oem_name));
|
---|
257 |
|
---|
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);
|
---|
263 |
|
---|
264 | mbs->data_clusters = host2uint32_t_le(cfg->total_clusters);
|
---|
265 |
|
---|
266 | mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
|
---|
267 | mbs->volume_serial = host2uint32_t_le(0xe1028172);
|
---|
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);
|
---|
273 |
|
---|
274 | /* Maximum cluster size is 32 Mb */
|
---|
275 | assert((mbs->bytes_per_sector + mbs->sec_per_cluster) <= 25);
|
---|
276 |
|
---|
277 | mbs->fat_count = 1;
|
---|
278 | mbs->drive_no = 0x80;
|
---|
279 | mbs->allocated_percent = 0;
|
---|
280 | mbs->signature = host2uint16_t_le(0xAA55);
|
---|
281 |
|
---|
282 | return vbr_checksum_start(mbs, sizeof(exfat_bs_t));
|
---|
283 | }
|
---|
284 |
|
---|
285 | static int
|
---|
286 | bootsec_write(service_id_t service_id, exfat_cfg_t *cfg)
|
---|
287 | {
|
---|
288 | exfat_bs_t mbs;
|
---|
289 | uint32_t vbr_checksum;
|
---|
290 | uint32_t *chksum_sector;
|
---|
291 | int rc;
|
---|
292 | unsigned idx;
|
---|
293 |
|
---|
294 | chksum_sector = calloc(cfg->sector_size, sizeof(uint8_t));
|
---|
295 | if (!chksum_sector)
|
---|
296 | return ENOMEM;
|
---|
297 |
|
---|
298 | vbr_checksum = vbr_initialize(&mbs, cfg);
|
---|
299 |
|
---|
300 | /* Write the Main Boot Sector to disk */
|
---|
301 | rc = block_write_direct(service_id, MBS_SECTOR, 1, &mbs);
|
---|
302 | if (rc != EOK)
|
---|
303 | goto exit;
|
---|
304 |
|
---|
305 | /* Write the Main extended boot sectors to disk */
|
---|
306 | rc = ebs_write(service_id, cfg, EBS_SECTOR_START, &vbr_checksum);
|
---|
307 | if (rc != EOK)
|
---|
308 | goto exit;
|
---|
309 |
|
---|
310 | /* Write the Main Boot Sector backup to disk */
|
---|
311 | rc = block_write_direct(service_id, MBS_BACKUP_SECTOR, 1, &mbs);
|
---|
312 | if (rc != EOK)
|
---|
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;
|
---|
324 |
|
---|
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;
|
---|
330 |
|
---|
331 | /* Write the Main extended boot sectors backup to disk */
|
---|
332 | rc = ebs_write(service_id, cfg,
|
---|
333 | EBS_BACKUP_SECTOR_START, &vbr_checksum);
|
---|
334 |
|
---|
335 | exit:
|
---|
336 | free(chksum_sector);
|
---|
337 | return rc;
|
---|
338 | }
|
---|
339 |
|
---|
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.
|
---|
344 | * @param base Base sector of the EBS.
|
---|
345 | * @return EOK on success or a negative error code.
|
---|
346 | */
|
---|
347 | static int
|
---|
348 | ebs_write(service_id_t service_id, exfat_cfg_t *cfg, int base,
|
---|
349 | uint32_t *chksum)
|
---|
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) {
|
---|
360 | vbr_checksum_update(ebs, cfg->sector_size, chksum);
|
---|
361 |
|
---|
362 | rc = block_write_direct(service_id,
|
---|
363 | i + base, 1, ebs);
|
---|
364 |
|
---|
365 | if (rc != EOK)
|
---|
366 | goto exit;
|
---|
367 | }
|
---|
368 |
|
---|
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);
|
---|
383 |
|
---|
384 | rc = block_write_direct(service_id, i + base, 1, ebs);
|
---|
385 | if (rc != EOK)
|
---|
386 | goto exit;
|
---|
387 |
|
---|
388 | exit:
|
---|
389 | free(ebs);
|
---|
390 | return rc;
|
---|
391 | }
|
---|
392 |
|
---|
393 | /** Initialize the FAT table.
|
---|
394 | *
|
---|
395 | * @param service_id The service id.
|
---|
396 | * @param cfg Pointer to the exfat_cfg structure.
|
---|
397 | * @return EOK on success or a negative error code.
|
---|
398 | */
|
---|
399 | static int
|
---|
400 | fat_initialize(service_id_t service_id, exfat_cfg_t *cfg)
|
---|
401 | {
|
---|
402 | unsigned long i;
|
---|
403 | uint32_t *pfat;
|
---|
404 | int rc;
|
---|
405 |
|
---|
406 | pfat = calloc(cfg->sector_size, 1);
|
---|
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 |
|
---|
417 | pfat[0] = pfat[1] = 0;
|
---|
418 |
|
---|
419 | for (i = 1; i < cfg->fat_sector_count; ++i) {
|
---|
420 | rc = block_write_direct(service_id,
|
---|
421 | FAT_SECTOR_START + i, 1, pfat);
|
---|
422 | if (rc != EOK)
|
---|
423 | goto error;
|
---|
424 | }
|
---|
425 |
|
---|
426 | error:
|
---|
427 | free(pfat);
|
---|
428 | return rc;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /** Allocate a given number of clusters and create a cluster chain.
|
---|
432 | *
|
---|
433 | * @param service_id The service id.
|
---|
434 | * @param cfg Pointer to the exfat configuration structure.
|
---|
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 | */
|
---|
439 | static int
|
---|
440 | fat_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);
|
---|
445 | aoff64_t fat_sec = cur_cls / fat_entries + FAT_SECTOR_START;
|
---|
446 | uint32_t *fat;
|
---|
447 | uint32_t next_cls = cur_cls;
|
---|
448 |
|
---|
449 | cur_cls %= fat_entries;
|
---|
450 |
|
---|
451 | fat = malloc(cfg->sector_size);
|
---|
452 | if (!fat)
|
---|
453 | return ENOMEM;
|
---|
454 |
|
---|
455 | loop:
|
---|
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)
|
---|
464 | fat[cur_cls] = host2uint32_t_le(++next_cls);
|
---|
465 |
|
---|
466 | if (cur_cls == fat_entries) {
|
---|
467 | /* This sector is full, there are no more free entries,
|
---|
468 | * commit the changes to disk and restart from the next
|
---|
469 | * sector.
|
---|
470 | */
|
---|
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;
|
---|
476 | } else if (ncls == 1) {
|
---|
477 | /* This is the last cluster of this chain, mark it
|
---|
478 | * with EOF.
|
---|
479 | */
|
---|
480 | fat[cur_cls] = host2uint32_t_le(0xFFFFFFFF);
|
---|
481 | }
|
---|
482 |
|
---|
483 | rc = block_write_direct(service_id, fat_sec, 1, fat);
|
---|
484 |
|
---|
485 | exit:
|
---|
486 | free(fat);
|
---|
487 | return rc;
|
---|
488 | }
|
---|
489 |
|
---|
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 | */
|
---|
496 | static int
|
---|
497 | bitmap_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;
|
---|
502 | bool need_reset = true;
|
---|
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) {
|
---|
514 | if (need_reset) {
|
---|
515 | need_reset = false;
|
---|
516 | memset(bitmap, 0, cfg->sector_size);
|
---|
517 | }
|
---|
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 |
|
---|
528 | allocated_cls -= i;
|
---|
529 | need_reset = true;
|
---|
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 |
|
---|
538 | exit:
|
---|
539 | free(bitmap);
|
---|
540 | return rc;
|
---|
541 | }
|
---|
542 |
|
---|
543 | /** Write the upcase table to disk. */
|
---|
544 | static int
|
---|
545 | upcase_table_write(service_id_t service_id, exfat_cfg_t *cfg)
|
---|
546 | {
|
---|
547 | int rc = EOK;
|
---|
548 | aoff64_t start_sec, nsecs, i;
|
---|
549 | uint8_t *table_ptr;
|
---|
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;
|
---|
556 |
|
---|
557 | /* Compute the start sector of the upcase table */
|
---|
558 | start_sec = cfg->data_start_sector;
|
---|
559 | start_sec += ((cfg->upcase_table_cluster - 2) * cfg->cluster_size) /
|
---|
560 | cfg->sector_size;
|
---|
561 |
|
---|
562 | /* Compute the number of sectors needed to store the table on disk */
|
---|
563 | nsecs = div_round_up(sizeof(upcase_table), cfg->sector_size);
|
---|
564 | table_ptr = (uint8_t *) upcase_table;
|
---|
565 |
|
---|
566 | for (i = 0; i < nsecs; ++i,
|
---|
567 | table_ptr += min(table_size, cfg->sector_size),
|
---|
568 | table_size -= cfg->sector_size) {
|
---|
569 |
|
---|
570 | if (table_size < cfg->sector_size) {
|
---|
571 | /* Reset the content of the unused part
|
---|
572 | * of the last sector.
|
---|
573 | */
|
---|
574 | memset(buf, 0, cfg->sector_size);
|
---|
575 | }
|
---|
576 | memcpy(buf, table_ptr, min(table_size, cfg->sector_size));
|
---|
577 |
|
---|
578 | rc = block_write_direct(service_id,
|
---|
579 | start_sec + i, 1, buf);
|
---|
580 | if (rc != EOK)
|
---|
581 | goto exit;
|
---|
582 | }
|
---|
583 |
|
---|
584 | exit:
|
---|
585 | free(buf);
|
---|
586 | return rc;
|
---|
587 | }
|
---|
588 |
|
---|
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 | */
|
---|
595 | static int
|
---|
596 | root_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;
|
---|
602 | unsigned long i;
|
---|
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;
|
---|
612 | str_to_utf16(d->vollabel.label, 8, "HELENOS ");
|
---|
613 | d->vollabel.size = 8;
|
---|
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);
|
---|
639 | if (rc != EOK)
|
---|
640 | goto exit;
|
---|
641 |
|
---|
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 |
|
---|
652 | exit:
|
---|
653 | free(data);
|
---|
654 | return rc;
|
---|
655 | }
|
---|
656 |
|
---|
657 | /** Given a number (n), returns the result of log2(n).
|
---|
658 | *
|
---|
659 | * It works only if n is a power of two.
|
---|
660 | */
|
---|
661 | static unsigned
|
---|
662 | log2(unsigned n)
|
---|
663 | {
|
---|
664 | unsigned r;
|
---|
665 |
|
---|
666 | for (r = 0;n >> r != 1; ++r);
|
---|
667 |
|
---|
668 | return r;
|
---|
669 | }
|
---|
670 |
|
---|
671 | /** Initialize the VBR checksum calculation */
|
---|
672 | static uint32_t
|
---|
673 | vbr_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 |
|
---|
685 | checksum = ((checksum << 31) | (checksum >> 1)) +
|
---|
686 | octets[index];
|
---|
687 | }
|
---|
688 |
|
---|
689 | return checksum;
|
---|
690 | }
|
---|
691 |
|
---|
692 | /** Update the VBR checksum */
|
---|
693 | static void
|
---|
694 | vbr_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 |
|
---|
699 | for (index = 0; index < nbytes; ++index) {
|
---|
700 | *checksum = ((*checksum << 31) | (*checksum >> 1)) +
|
---|
701 | octets[index];
|
---|
702 | }
|
---|
703 | }
|
---|
704 |
|
---|
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 | */
|
---|
711 | static uint32_t
|
---|
712 | upcase_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 |
|
---|
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 | */
|
---|
729 | static bool
|
---|
730 | is_power_of_two(unsigned long n)
|
---|
731 | {
|
---|
732 | if (n == 0)
|
---|
733 | return false;
|
---|
734 |
|
---|
735 | return (n & (n - 1)) == 0;
|
---|
736 | }
|
---|
737 |
|
---|
738 | int main (int argc, char **argv)
|
---|
739 | {
|
---|
740 | exfat_cfg_t cfg;
|
---|
741 | uint32_t next_cls;
|
---|
742 | char *dev_path;
|
---|
743 | service_id_t service_id;
|
---|
744 | int rc, c, opt_ind;
|
---|
745 | aoff64_t user_fs_size = 0;
|
---|
746 |
|
---|
747 | if (argc < 2) {
|
---|
748 | printf(NAME ": Error, argument missing\n");
|
---|
749 | usage();
|
---|
750 | return 1;
|
---|
751 | }
|
---|
752 |
|
---|
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':
|
---|
767 | cfg.cluster_size = strtol(optarg, NULL, 10) * 1024;
|
---|
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 | }
|
---|
786 |
|
---|
787 | argv += optind;
|
---|
788 | dev_path = *argv;
|
---|
789 |
|
---|
790 | if (!dev_path) {
|
---|
791 | printf(NAME ": Error, you must specify a valid block"
|
---|
792 | " device.\n");
|
---|
793 | usage();
|
---|
794 | return 1;
|
---|
795 | }
|
---|
796 |
|
---|
797 | printf("Device = %s\n", dev_path);
|
---|
798 |
|
---|
799 | rc = loc_service_get_id(dev_path, &service_id, 0);
|
---|
800 | if (rc != EOK) {
|
---|
801 | printf(NAME ": Error resolving device `%s'.\n", dev_path);
|
---|
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 |
|
---|
811 | rc = block_get_bsize(service_id, &cfg.sector_size);
|
---|
812 | if (rc != EOK) {
|
---|
813 | printf(NAME ": Error determining device sector size.\n");
|
---|
814 | return 2;
|
---|
815 | }
|
---|
816 |
|
---|
817 | user_fs_size *= cfg.sector_size;
|
---|
818 | if (user_fs_size > 0 && user_fs_size < 1024 * 1024) {
|
---|
819 | printf(NAME ": Error, fs size can't be less"
|
---|
820 | " than 1 Mb.\n");
|
---|
821 | return 1;
|
---|
822 | }
|
---|
823 |
|
---|
824 |
|
---|
825 | if (cfg.sector_size > 4096) {
|
---|
826 | printf(NAME ": Error, sector size can't be greater" \
|
---|
827 | " than 4096 bytes.\n");
|
---|
828 | return 2;
|
---|
829 | }
|
---|
830 |
|
---|
831 | rc = block_get_nblocks(service_id, &cfg.volume_count);
|
---|
832 | if (rc != EOK) {
|
---|
833 | printf(NAME ": Warning, failed to obtain" \
|
---|
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 | }
|
---|
841 | } else {
|
---|
842 | printf("Block device has %" PRIuOFF64 " blocks.\n",
|
---|
843 | cfg.volume_count);
|
---|
844 | }
|
---|
845 |
|
---|
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"
|
---|
849 | " to create a filesystem of"
|
---|
850 | " the specified size.\n");
|
---|
851 | return 1;
|
---|
852 | }
|
---|
853 |
|
---|
854 | cfg.volume_count = user_fs_size / cfg.sector_size;
|
---|
855 | }
|
---|
856 |
|
---|
857 | cfg_params_initialize(&cfg);
|
---|
858 | cfg_print_info(&cfg);
|
---|
859 |
|
---|
860 | if (cfg.total_clusters <= cfg.allocated_clusters + 2) {
|
---|
861 | printf(NAME ": Error, insufficient disk space on device.\n");
|
---|
862 | return 2;
|
---|
863 | }
|
---|
864 |
|
---|
865 | printf("Writing the allocation table.\n");
|
---|
866 |
|
---|
867 | /* Initialize the FAT table */
|
---|
868 | rc = fat_initialize(service_id, &cfg);
|
---|
869 | if (rc != EOK) {
|
---|
870 | printf(NAME ": Error, failed to write the FAT to disk\n");
|
---|
871 | return 2;
|
---|
872 | }
|
---|
873 |
|
---|
874 | /* Allocate clusters for the bitmap */
|
---|
875 | rc = fat_allocate_clusters(service_id, &cfg, cfg.bitmap_cluster,
|
---|
876 | div_round_up(cfg.bitmap_size, cfg.cluster_size));
|
---|
877 | if (rc != EOK) {
|
---|
878 | printf(NAME ": Error, failed to allocate" \
|
---|
879 | " clusters for bitmap.\n");
|
---|
880 | return 2;
|
---|
881 | }
|
---|
882 |
|
---|
883 | next_cls = cfg.bitmap_cluster +
|
---|
884 | div_round_up(cfg.bitmap_size, cfg.cluster_size);
|
---|
885 | cfg.upcase_table_cluster = next_cls;
|
---|
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) {
|
---|
891 | printf(NAME ":Error, failed to allocate clusters" \
|
---|
892 | " for the upcase table.\n");
|
---|
893 | return 2;
|
---|
894 | }
|
---|
895 |
|
---|
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) {
|
---|
902 | printf(NAME ": Error, failed to allocate cluster" \
|
---|
903 | " for the root dentry.\n");
|
---|
904 | return 2;
|
---|
905 | }
|
---|
906 |
|
---|
907 | printf("Writing the allocation bitmap.\n");
|
---|
908 |
|
---|
909 | /* Write the allocation bitmap to disk */
|
---|
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 |
|
---|
917 | printf("Writing the upcase table.\n");
|
---|
918 |
|
---|
919 | /* Write the upcase table to disk */
|
---|
920 | rc = upcase_table_write(service_id, &cfg);
|
---|
921 | if (rc != EOK) {
|
---|
922 | printf(NAME ": Error, failed to write the" \
|
---|
923 | " upcase table to disk.\n");
|
---|
924 | return 2;
|
---|
925 | }
|
---|
926 |
|
---|
927 | printf("Writing the root directory.\n");
|
---|
928 |
|
---|
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 |
|
---|
936 | printf("Writing the boot sectors.\n");
|
---|
937 |
|
---|
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 |
|
---|
944 | printf("Success.\n");
|
---|
945 |
|
---|
946 | return 0;
|
---|
947 | }
|
---|
948 |
|
---|
949 | /**
|
---|
950 | * @}
|
---|
951 | */
|
---|
952 |
|
---|