1 | /*
|
---|
2 | * Copyright (c) 2012 Maurizio Lombardi
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup fs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file mkexfat.c
|
---|
35 | * @brief Tool for creating new exFAT file systems.
|
---|
36 | *
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <libblock.h>
|
---|
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 <bool.h>
|
---|
49 | #include "exfat.h"
|
---|
50 | #include "upcase.h"
|
---|
51 |
|
---|
52 | #define NAME "mkexfat"
|
---|
53 |
|
---|
54 | /** First sector of the FAT */
|
---|
55 | #define FAT_SECTOR_START 128
|
---|
56 |
|
---|
57 | /** First sector of the Main Extended Boot Region */
|
---|
58 | #define EBS_SECTOR_START 1
|
---|
59 |
|
---|
60 | /** First sector of the Main Extended Boot Region Backup */
|
---|
61 | #define EBS_BACKUP_SECTOR_START 13
|
---|
62 |
|
---|
63 | /** First sector of the Main Boot Sector */
|
---|
64 | #define MBS_SECTOR 0
|
---|
65 |
|
---|
66 | /** First sector of the Main Boot Sector Backup */
|
---|
67 | #define MBS_BACKUP_SECTOR 12
|
---|
68 |
|
---|
69 | /** Size of the Main Extended Boot Region */
|
---|
70 | #define EBS_SIZE 8
|
---|
71 |
|
---|
72 | /** Divide and round up. */
|
---|
73 | #define div_round_up(a, b) (((a) + (b) - 1) / (b))
|
---|
74 |
|
---|
75 | /** The default size of each cluster is 4096 byte */
|
---|
76 | #define DEFAULT_CLUSTER_SIZE 4096
|
---|
77 |
|
---|
78 | /** Index of the first free cluster on the device */
|
---|
79 | #define FIRST_FREE_CLUSTER 2
|
---|
80 |
|
---|
81 | #define min(x, y) ((x) < (y) ? (x) : (y))
|
---|
82 |
|
---|
83 | typedef struct exfat_cfg {
|
---|
84 | aoff64_t volume_start;
|
---|
85 | aoff64_t volume_count;
|
---|
86 | unsigned long fat_sector_count;
|
---|
87 | unsigned long data_start_sector;
|
---|
88 | unsigned long rootdir_cluster;
|
---|
89 | unsigned long upcase_table_cluster;
|
---|
90 | unsigned long total_clusters;
|
---|
91 | unsigned long allocated_clusters;
|
---|
92 | size_t bitmap_size;
|
---|
93 | size_t sector_size;
|
---|
94 | size_t cluster_size;
|
---|
95 | } exfat_cfg_t;
|
---|
96 |
|
---|
97 |
|
---|
98 | static unsigned log2(unsigned n);
|
---|
99 |
|
---|
100 | static uint32_t
|
---|
101 | vbr_checksum_start(void const *octets, size_t nbytes);
|
---|
102 |
|
---|
103 | static void
|
---|
104 | vbr_checksum_update(void const *octets, size_t nbytes, uint32_t *checksum);
|
---|
105 |
|
---|
106 | static int
|
---|
107 | ebs_write(service_id_t service_id, exfat_cfg_t *cfg,
|
---|
108 | int base, uint32_t *chksum);
|
---|
109 |
|
---|
110 | static int
|
---|
111 | bitmap_write(service_id_t service_id, exfat_cfg_t *cfg);
|
---|
112 |
|
---|
113 | static void usage(void)
|
---|
114 | {
|
---|
115 | printf("Usage: mkexfat <device>\n");
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** Initialize the exFAT params structure.
|
---|
119 | *
|
---|
120 | * @param cfg Pointer to the exFAT params structure to initialize.
|
---|
121 | */
|
---|
122 | static void
|
---|
123 | cfg_params_initialize(exfat_cfg_t *cfg)
|
---|
124 | {
|
---|
125 | unsigned long fat_bytes;
|
---|
126 | aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
|
---|
127 | cfg->sector_size;
|
---|
128 |
|
---|
129 | aoff64_t n_req_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
|
---|
130 | cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
|
---|
131 |
|
---|
132 | /* Compute the required cluster size to index
|
---|
133 | * the entire storage device and to keep the FAT
|
---|
134 | * size less or equal to 64 Mb.
|
---|
135 | */
|
---|
136 | while (n_req_clusters > 16000000ULL &&
|
---|
137 | (cfg->cluster_size < 32 * 1024 * 1024)) {
|
---|
138 |
|
---|
139 | cfg->cluster_size <<= 1;
|
---|
140 | n_req_clusters = volume_bytes / cfg->cluster_size;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* The first two clusters are reserved */
|
---|
144 | cfg->total_clusters = n_req_clusters + 2;
|
---|
145 |
|
---|
146 | /* Compute the FAT size in sectors */
|
---|
147 | fat_bytes = (cfg->total_clusters + 1) * 4;
|
---|
148 | cfg->fat_sector_count = div_round_up(fat_bytes, cfg->sector_size);
|
---|
149 |
|
---|
150 | /* Compute the number of the first data sector */
|
---|
151 | cfg->data_start_sector = ROUND_UP(FAT_SECTOR_START +
|
---|
152 | cfg->fat_sector_count, cfg->cluster_size / cfg->sector_size);
|
---|
153 |
|
---|
154 | /* Compute the bitmap size */
|
---|
155 | cfg->bitmap_size = n_req_clusters / 8;
|
---|
156 |
|
---|
157 | /* Compute the number of clusters reserved to the bitmap */
|
---|
158 | cfg->allocated_clusters = div_round_up(cfg->bitmap_size,
|
---|
159 | cfg->cluster_size);
|
---|
160 |
|
---|
161 | /* This account for the root directory */
|
---|
162 | cfg->allocated_clusters++;
|
---|
163 |
|
---|
164 | /* Compute the number of clusters reserved to the upcase table */
|
---|
165 | cfg->allocated_clusters += div_round_up(sizeof(upcase_table),
|
---|
166 | cfg->cluster_size);
|
---|
167 |
|
---|
168 | /* Will be set later */
|
---|
169 | cfg->rootdir_cluster = 0;
|
---|
170 |
|
---|
171 | /* The first sector of the partition is zero */
|
---|
172 | cfg->volume_start = 0;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /** Prints the exFAT structure values
|
---|
176 | *
|
---|
177 | * @param cfg Pointer to the exfat_cfg_t structure.
|
---|
178 | */
|
---|
179 | static void
|
---|
180 | cfg_print_info(exfat_cfg_t *cfg)
|
---|
181 | {
|
---|
182 | printf(NAME ": Sector size: %lu\n", cfg->sector_size);
|
---|
183 | printf(NAME ": Cluster size: %lu\n", cfg->cluster_size);
|
---|
184 | printf(NAME ": FAT size in sectors: %lu\n", cfg->fat_sector_count);
|
---|
185 | printf(NAME ": Data start sector: %lu\n", cfg->data_start_sector);
|
---|
186 | printf(NAME ": Total num of clusters: %lu\n", cfg->total_clusters);
|
---|
187 | printf(NAME ": Bitmap size: %lu\n",
|
---|
188 | div_round_up(cfg->bitmap_size, cfg->cluster_size));
|
---|
189 | printf(NAME ": Upcase table size: %lu\n",
|
---|
190 | div_round_up(sizeof(upcase_table), cfg->cluster_size));
|
---|
191 | }
|
---|
192 |
|
---|
193 | /** Initialize the Main Boot Sector fields.
|
---|
194 | *
|
---|
195 | * @param mbs Pointer to the Main Boot Sector structure.
|
---|
196 | * @param cfg Pointer to the exFAT configuration structure.
|
---|
197 | * @return Initial checksum value.
|
---|
198 | */
|
---|
199 | static uint32_t
|
---|
200 | vbr_initialize(exfat_bs_t *mbs, exfat_cfg_t *cfg)
|
---|
201 | {
|
---|
202 | /* Fill the structure with zeroes */
|
---|
203 | memset(mbs, 0, sizeof(exfat_bs_t));
|
---|
204 |
|
---|
205 | /* Init Jump Boot section */
|
---|
206 | mbs->jump[0] = 0xEB;
|
---|
207 | mbs->jump[1] = 0x76;
|
---|
208 | mbs->jump[2] = 0x90;
|
---|
209 |
|
---|
210 | /* Set the filesystem name */
|
---|
211 | memcpy(mbs->oem_name, "EXFAT ", sizeof(mbs->oem_name));
|
---|
212 |
|
---|
213 | mbs->volume_start = host2uint64_t_le(cfg->volume_start);
|
---|
214 | mbs->volume_count = host2uint64_t_le(cfg->volume_count);
|
---|
215 | mbs->fat_sector_start = host2uint32_t_le(FAT_SECTOR_START);
|
---|
216 | mbs->fat_sector_count = host2uint32_t_le(cfg->fat_sector_count);
|
---|
217 | mbs->data_start_sector = host2uint32_t_le(cfg->data_start_sector);
|
---|
218 |
|
---|
219 | mbs->data_clusters = host2uint32_t_le(cfg->total_clusters -
|
---|
220 | div_round_up(cfg->data_start_sector, cfg->cluster_size));
|
---|
221 |
|
---|
222 | mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
|
---|
223 | mbs->volume_serial = 0;
|
---|
224 | mbs->version.major = 1;
|
---|
225 | mbs->version.minor = 0;
|
---|
226 | mbs->volume_flags = host2uint16_t_le(0);
|
---|
227 | mbs->bytes_per_sector = log2(cfg->sector_size);
|
---|
228 | mbs->sec_per_cluster = log2(cfg->cluster_size / cfg->sector_size);
|
---|
229 |
|
---|
230 | /* Maximum cluster size is 32 Mb */
|
---|
231 | assert((mbs->bytes_per_sector + mbs->sec_per_cluster) <= 25);
|
---|
232 |
|
---|
233 | mbs->fat_count = 1;
|
---|
234 | mbs->drive_no = 0x80;
|
---|
235 | mbs->allocated_percent = 0;
|
---|
236 | mbs->signature = host2uint16_t_le(0xAA55);
|
---|
237 |
|
---|
238 | return vbr_checksum_start(mbs, sizeof(exfat_bs_t));
|
---|
239 | }
|
---|
240 |
|
---|
241 | static int
|
---|
242 | bootsec_write(service_id_t service_id, exfat_cfg_t *cfg)
|
---|
243 | {
|
---|
244 | exfat_bs_t mbs;
|
---|
245 | uint32_t vbr_checksum;
|
---|
246 | uint32_t initial_checksum;
|
---|
247 | int rc;
|
---|
248 |
|
---|
249 | vbr_checksum = vbr_initialize(&mbs, cfg);
|
---|
250 | initial_checksum = vbr_checksum;
|
---|
251 |
|
---|
252 | /* Write the Main Boot Sector to disk */
|
---|
253 | rc = block_write_direct(service_id, MBS_SECTOR, 1, &mbs);
|
---|
254 | if (rc != EOK)
|
---|
255 | return rc;
|
---|
256 |
|
---|
257 | /* Write the Main extended boot sectors to disk */
|
---|
258 | rc = ebs_write(service_id, cfg, EBS_SECTOR_START, &vbr_checksum);
|
---|
259 | if (rc != EOK)
|
---|
260 | return rc;
|
---|
261 |
|
---|
262 | /* Write the Main Boot Sector backup to disk */
|
---|
263 | rc = block_write_direct(service_id, MBS_BACKUP_SECTOR, 1, &mbs);
|
---|
264 | if (rc != EOK)
|
---|
265 | return rc;
|
---|
266 |
|
---|
267 | /* Restore the checksum to its initial value */
|
---|
268 | vbr_checksum = initial_checksum;
|
---|
269 |
|
---|
270 | /* Write the Main extended boot sectors backup to disk */
|
---|
271 | return ebs_write(service_id, cfg,
|
---|
272 | EBS_BACKUP_SECTOR_START, &vbr_checksum);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /** Write the Main Extended Boot Sector to disk
|
---|
276 | *
|
---|
277 | * @param service_id The service id.
|
---|
278 | * @param cfg Pointer to the exFAT configuration structure.
|
---|
279 | * @param base Base sector of the EBS.
|
---|
280 | * @return EOK on success or a negative error code.
|
---|
281 | */
|
---|
282 | static int
|
---|
283 | ebs_write(service_id_t service_id, exfat_cfg_t *cfg, int base, uint32_t *chksum)
|
---|
284 | {
|
---|
285 | uint32_t *ebs = calloc(cfg->sector_size, sizeof(uint8_t));
|
---|
286 | int i, rc;
|
---|
287 | unsigned idx;
|
---|
288 |
|
---|
289 | if (!ebs)
|
---|
290 | return ENOMEM;
|
---|
291 |
|
---|
292 | ebs[cfg->sector_size / 4 - 1] = host2uint32_t_le(0xAA550000);
|
---|
293 |
|
---|
294 | for (i = 0; i < EBS_SIZE; ++i) {
|
---|
295 | vbr_checksum_update(ebs, cfg->sector_size, chksum);
|
---|
296 |
|
---|
297 | rc = block_write_direct(service_id,
|
---|
298 | i + EBS_SECTOR_START + base, 1, ebs);
|
---|
299 |
|
---|
300 | if (rc != EOK)
|
---|
301 | goto exit;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /* The OEM record is not yet used
|
---|
305 | * by the official exFAT implementation, we'll fill
|
---|
306 | * it with zeroes.
|
---|
307 | */
|
---|
308 |
|
---|
309 | memset(ebs, 0, cfg->sector_size);
|
---|
310 | vbr_checksum_update(ebs, cfg->sector_size, chksum);
|
---|
311 |
|
---|
312 | rc = block_write_direct(service_id, i++ + base, 1, ebs);
|
---|
313 | if (rc != EOK)
|
---|
314 | goto exit;
|
---|
315 |
|
---|
316 | /* The next sector is reserved, fill it with zeroes too */
|
---|
317 | vbr_checksum_update(ebs, cfg->sector_size, chksum);
|
---|
318 | rc = block_write_direct(service_id, i++ + base, 1, ebs);
|
---|
319 | if (rc != EOK)
|
---|
320 | goto exit;
|
---|
321 |
|
---|
322 | /* Write the checksum sector */
|
---|
323 | for (idx = 0; idx < cfg->sector_size / sizeof(uint32_t); ++idx)
|
---|
324 | ebs[idx] = host2uint32_t_le(*chksum);
|
---|
325 |
|
---|
326 | rc = block_write_direct(service_id, i + base, 1, ebs);
|
---|
327 |
|
---|
328 | exit:
|
---|
329 | free(ebs);
|
---|
330 | return rc;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /** Initialize the FAT table.
|
---|
334 | *
|
---|
335 | * @param service_id The service id.
|
---|
336 | * @param cfg Pointer to the exfat_cfg structure.
|
---|
337 | * @return EOK on success or a negative error code.
|
---|
338 | */
|
---|
339 | static int
|
---|
340 | fat_initialize(service_id_t service_id, exfat_cfg_t *cfg)
|
---|
341 | {
|
---|
342 | unsigned long i;
|
---|
343 | uint32_t *pfat;
|
---|
344 | int rc;
|
---|
345 |
|
---|
346 | pfat = calloc(cfg->sector_size, 1);
|
---|
347 | if (!pfat)
|
---|
348 | return ENOMEM;
|
---|
349 |
|
---|
350 | pfat[0] = host2uint32_t_le(0xFFFFFFF8);
|
---|
351 | pfat[1] = host2uint32_t_le(0xFFFFFFFF);
|
---|
352 |
|
---|
353 | rc = block_write_direct(service_id, FAT_SECTOR_START, 1, pfat);
|
---|
354 | if (rc != EOK)
|
---|
355 | goto error;
|
---|
356 |
|
---|
357 | pfat[0] = pfat[1] = 0;
|
---|
358 |
|
---|
359 | for (i = 1; i < cfg->fat_sector_count; ++i) {
|
---|
360 | rc = block_write_direct(service_id,
|
---|
361 | FAT_SECTOR_START + i, 1, pfat);
|
---|
362 | if (rc != EOK)
|
---|
363 | goto error;
|
---|
364 | }
|
---|
365 |
|
---|
366 | error:
|
---|
367 | free(pfat);
|
---|
368 | return rc;
|
---|
369 | }
|
---|
370 |
|
---|
371 | /** Allocate a given number of clusters and create a cluster chain.
|
---|
372 | *
|
---|
373 | * @param service_id The service id.
|
---|
374 | * @param cfg Pointer to the exfat configuration number.
|
---|
375 | * @param cur_cls Cluster index from where to start the allocation.
|
---|
376 | * @param ncls Number of clusters to allocate.
|
---|
377 | * @return EOK on success or a negative error code.
|
---|
378 | */
|
---|
379 | static int
|
---|
380 | fat_allocate_clusters(service_id_t service_id, exfat_cfg_t *cfg,
|
---|
381 | uint32_t cur_cls, unsigned long ncls)
|
---|
382 | {
|
---|
383 | int rc;
|
---|
384 | unsigned const fat_entries = cfg->sector_size / sizeof(uint32_t);
|
---|
385 | aoff64_t fat_sec = cur_cls / fat_entries + FAT_SECTOR_START;
|
---|
386 | uint32_t *fat;
|
---|
387 |
|
---|
388 | cur_cls %= fat_entries;
|
---|
389 |
|
---|
390 | fat = malloc(cfg->sector_size);
|
---|
391 | if (!fat)
|
---|
392 | return ENOMEM;
|
---|
393 |
|
---|
394 | loop:
|
---|
395 | rc = block_read_direct(service_id, fat_sec, 1, fat);
|
---|
396 | if (rc != EOK)
|
---|
397 | goto exit;
|
---|
398 |
|
---|
399 | assert(fat[cur_cls] == 0);
|
---|
400 | assert(ncls > 0);
|
---|
401 |
|
---|
402 | for (; cur_cls < fat_entries && ncls > 1; ++cur_cls, --ncls)
|
---|
403 | fat[cur_cls] = host2uint32_t_le(cur_cls + 1);
|
---|
404 |
|
---|
405 | if (cur_cls == fat_entries) {
|
---|
406 | /* This sector is full, there are no more free entries,
|
---|
407 | * read the next sector and restart the search.
|
---|
408 | */
|
---|
409 | rc = block_write_direct(service_id, fat_sec++, 1, fat);
|
---|
410 | if (rc != EOK)
|
---|
411 | goto exit;
|
---|
412 | cur_cls = 0;
|
---|
413 | goto loop;
|
---|
414 | } else if (ncls == 1) {
|
---|
415 | /* This is the last cluster of this chain, mark it
|
---|
416 | * with EOF.
|
---|
417 | */
|
---|
418 | fat[cur_cls] = host2uint32_t_le(0xFFFFFFFF);
|
---|
419 | }
|
---|
420 |
|
---|
421 | rc = block_write_direct(service_id, fat_sec, 1, fat);
|
---|
422 |
|
---|
423 | exit:
|
---|
424 | free(fat);
|
---|
425 | return rc;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /** Initialize the allocation bitmap.
|
---|
429 | *
|
---|
430 | * @param service_id The service id.
|
---|
431 | * @param cfg Pointer to the exfat configuration structure.
|
---|
432 | * @return EOK on success or a negative error code.
|
---|
433 | */
|
---|
434 | static int
|
---|
435 | bitmap_write(service_id_t service_id, exfat_cfg_t *cfg)
|
---|
436 | {
|
---|
437 | unsigned long i, sec;
|
---|
438 | unsigned long allocated_cls;
|
---|
439 | int rc = EOK;
|
---|
440 | bool need_reset = true;
|
---|
441 |
|
---|
442 | /* Bitmap size in sectors */
|
---|
443 | size_t const bss = div_round_up(cfg->bitmap_size, cfg->sector_size);
|
---|
444 |
|
---|
445 | uint8_t *bitmap = malloc(cfg->sector_size);
|
---|
446 | if (!bitmap)
|
---|
447 | return ENOMEM;
|
---|
448 |
|
---|
449 | allocated_cls = cfg->allocated_clusters;
|
---|
450 |
|
---|
451 | for (sec = 0; sec < bss; ++sec) {
|
---|
452 | if (need_reset) {
|
---|
453 | need_reset = false;
|
---|
454 | memset(bitmap, 0, cfg->sector_size);
|
---|
455 | }
|
---|
456 | if (allocated_cls > 0) {
|
---|
457 | for (i = 0; i < allocated_cls; ++i) {
|
---|
458 | unsigned byte_idx = i / 8;
|
---|
459 | unsigned bit_idx = i % 8;
|
---|
460 |
|
---|
461 | if (byte_idx == cfg->sector_size)
|
---|
462 | break;
|
---|
463 | bitmap[byte_idx] |= 1 << bit_idx;
|
---|
464 | }
|
---|
465 |
|
---|
466 | allocated_cls -= i;
|
---|
467 | need_reset = true;
|
---|
468 | }
|
---|
469 |
|
---|
470 | rc = block_write_direct(service_id,
|
---|
471 | cfg->data_start_sector + sec, 1, bitmap);
|
---|
472 | if (rc != EOK)
|
---|
473 | goto exit;
|
---|
474 | }
|
---|
475 |
|
---|
476 | exit:
|
---|
477 | free(bitmap);
|
---|
478 | return rc;
|
---|
479 | }
|
---|
480 |
|
---|
481 | /** Write the upcase table to disk. */
|
---|
482 | static int
|
---|
483 | upcase_table_write(service_id_t service_id, exfat_cfg_t *cfg)
|
---|
484 | {
|
---|
485 | int rc = EOK;
|
---|
486 | aoff64_t start_sec, nsecs, i;
|
---|
487 | uint8_t *table_ptr;
|
---|
488 | uint8_t *buf;
|
---|
489 | size_t table_size = sizeof(upcase_table);
|
---|
490 |
|
---|
491 | buf = malloc(cfg->sector_size);
|
---|
492 | if (!buf)
|
---|
493 | return ENOENT;
|
---|
494 |
|
---|
495 | /* Compute the start sector of the upcase table */
|
---|
496 | start_sec = cfg->data_start_sector;
|
---|
497 | start_sec += ((cfg->upcase_table_cluster - 2) * cfg->cluster_size) /
|
---|
498 | cfg->sector_size;
|
---|
499 |
|
---|
500 | /* Compute the number of sectors needed to store the table on disk */
|
---|
501 | nsecs = div_round_up(sizeof(upcase_table), cfg->sector_size);
|
---|
502 | table_ptr = (uint8_t *) upcase_table;
|
---|
503 |
|
---|
504 | for (i = 0; i < nsecs; ++i,
|
---|
505 | table_ptr += min(table_size, cfg->sector_size),
|
---|
506 | table_size -= cfg->sector_size) {
|
---|
507 |
|
---|
508 | if (table_size < cfg->sector_size) {
|
---|
509 | /* Reset the content of the unused part
|
---|
510 | * of the last sector.
|
---|
511 | */
|
---|
512 | memset(buf, 0, cfg->sector_size);
|
---|
513 | }
|
---|
514 | memcpy(buf, table_ptr, min(table_size, cfg->sector_size));
|
---|
515 |
|
---|
516 | rc = block_write_direct(service_id,
|
---|
517 | start_sec + i, 1, buf);
|
---|
518 | if (rc != EOK)
|
---|
519 | break;
|
---|
520 | }
|
---|
521 |
|
---|
522 | free(buf);
|
---|
523 | return rc;
|
---|
524 | }
|
---|
525 |
|
---|
526 | /** Given a number (n), returns the result of log2(n).
|
---|
527 | *
|
---|
528 | * It works only if n is a power of two.
|
---|
529 | */
|
---|
530 | static unsigned
|
---|
531 | log2(unsigned n)
|
---|
532 | {
|
---|
533 | unsigned r;
|
---|
534 |
|
---|
535 | for (r = 0;n >> r != 1; ++r);
|
---|
536 |
|
---|
537 | return r;
|
---|
538 | }
|
---|
539 |
|
---|
540 | /** Initialize the VBR checksum calculation */
|
---|
541 | static uint32_t
|
---|
542 | vbr_checksum_start(void const *data, size_t nbytes)
|
---|
543 | {
|
---|
544 | uint32_t checksum = 0;
|
---|
545 | size_t index;
|
---|
546 | uint8_t const *octets = (uint8_t *) data;
|
---|
547 |
|
---|
548 | for (index = 0; index < nbytes; ++index) {
|
---|
549 | if (index == 106 || index == 107 || index == 112) {
|
---|
550 | /* Skip volume_flags and allocated_percent fields */
|
---|
551 | continue;
|
---|
552 | }
|
---|
553 |
|
---|
554 | checksum = ((checksum << 31) | (checksum >> 1)) + octets[index];
|
---|
555 | }
|
---|
556 |
|
---|
557 | return checksum;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /** Update the VBR checksum */
|
---|
561 | static void
|
---|
562 | vbr_checksum_update(void const *data, size_t nbytes, uint32_t *checksum)
|
---|
563 | {
|
---|
564 | size_t index;
|
---|
565 | uint8_t const *octets = (uint8_t *) data;
|
---|
566 |
|
---|
567 | for (index = 0; index < nbytes; ++index)
|
---|
568 | *checksum = ((*checksum << 31) | (*checksum >> 1)) + octets[index];
|
---|
569 | }
|
---|
570 |
|
---|
571 | int main (int argc, char **argv)
|
---|
572 | {
|
---|
573 | exfat_cfg_t cfg;
|
---|
574 | uint32_t next_cls;
|
---|
575 | char *dev_path;
|
---|
576 | service_id_t service_id;
|
---|
577 | int rc;
|
---|
578 |
|
---|
579 | if (argc < 2) {
|
---|
580 | printf(NAME ": Error, argument missing\n");
|
---|
581 | usage();
|
---|
582 | return 1;
|
---|
583 | }
|
---|
584 |
|
---|
585 | /* TODO: Add parameters */
|
---|
586 |
|
---|
587 | ++argv;
|
---|
588 | dev_path = *argv;
|
---|
589 |
|
---|
590 | printf(NAME ": Device = %s\n", dev_path);
|
---|
591 |
|
---|
592 | rc = loc_service_get_id(dev_path, &service_id, 0);
|
---|
593 | if (rc != EOK) {
|
---|
594 | printf(NAME ": Error resolving device `%s'.\n", dev_path);
|
---|
595 | return 2;
|
---|
596 | }
|
---|
597 |
|
---|
598 | rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
|
---|
599 | if (rc != EOK) {
|
---|
600 | printf(NAME ": Error initializing libblock.\n");
|
---|
601 | return 2;
|
---|
602 | }
|
---|
603 |
|
---|
604 | rc = block_get_bsize(service_id, &cfg.sector_size);
|
---|
605 | if (rc != EOK) {
|
---|
606 | printf(NAME ": Error determining device block size.\n");
|
---|
607 | return 2;
|
---|
608 | }
|
---|
609 |
|
---|
610 | if (cfg.sector_size > 4096) {
|
---|
611 | printf(NAME ": Error, sector size can't be greater" \
|
---|
612 | " than 4096 bytes.\n");
|
---|
613 | return 2;
|
---|
614 | }
|
---|
615 |
|
---|
616 | rc = block_get_nblocks(service_id, &cfg.volume_count);
|
---|
617 | if (rc != EOK) {
|
---|
618 | printf(NAME ": Warning, failed to obtain" \
|
---|
619 | " device block size.\n");
|
---|
620 | /* FIXME: the user should be able to specify the filesystem size */
|
---|
621 | return 1;
|
---|
622 | } else {
|
---|
623 | printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
|
---|
624 | cfg.volume_count);
|
---|
625 | }
|
---|
626 |
|
---|
627 | cfg_params_initialize(&cfg);
|
---|
628 | cfg_print_info(&cfg);
|
---|
629 |
|
---|
630 | /* Initialize the FAT table */
|
---|
631 | rc = fat_initialize(service_id, &cfg);
|
---|
632 | if (rc != EOK) {
|
---|
633 | printf(NAME ": Error, failed to write the FAT to disk\n");
|
---|
634 | return 2;
|
---|
635 | }
|
---|
636 |
|
---|
637 | /* Allocate clusters for the bitmap */
|
---|
638 | rc = fat_allocate_clusters(service_id, &cfg, FIRST_FREE_CLUSTER,
|
---|
639 | div_round_up(cfg.bitmap_size, cfg.cluster_size));
|
---|
640 | if (rc != EOK) {
|
---|
641 | printf(NAME ": Error, failed to allocate clusters for bitmap.\n");
|
---|
642 | return 2;
|
---|
643 | }
|
---|
644 |
|
---|
645 | next_cls = FIRST_FREE_CLUSTER +
|
---|
646 | div_round_up(cfg.bitmap_size, cfg.cluster_size);
|
---|
647 | cfg.upcase_table_cluster = next_cls;
|
---|
648 |
|
---|
649 | /* Allocate clusters for the upcase table */
|
---|
650 | rc = fat_allocate_clusters(service_id, &cfg, next_cls,
|
---|
651 | div_round_up(sizeof(upcase_table), cfg.cluster_size));
|
---|
652 | if (rc != EOK) {
|
---|
653 | printf(NAME ":Error, failed to allocate clusters foe the upcase table.\n");
|
---|
654 | return 2;
|
---|
655 | }
|
---|
656 |
|
---|
657 | next_cls += div_round_up(sizeof(upcase_table), cfg.cluster_size);
|
---|
658 | cfg.rootdir_cluster = next_cls;
|
---|
659 |
|
---|
660 | /* Allocate a cluster for the root directory entry */
|
---|
661 | rc = fat_allocate_clusters(service_id, &cfg, next_cls, 1);
|
---|
662 | if (rc != EOK) {
|
---|
663 | printf(NAME ": Error, failed to allocate cluster for the root dentry.\n");
|
---|
664 | return 2;
|
---|
665 | }
|
---|
666 |
|
---|
667 | /* Write the allocation bitmap to disk */
|
---|
668 | rc = bitmap_write(service_id, &cfg);
|
---|
669 | if (rc != EOK) {
|
---|
670 | printf(NAME ": Error, failed to write the allocation" \
|
---|
671 | " bitmap to disk.\n");
|
---|
672 | return 2;
|
---|
673 | }
|
---|
674 |
|
---|
675 | /* Write the upcase table to disk */
|
---|
676 | rc = upcase_table_write(service_id, &cfg);
|
---|
677 | if (rc != EOK) {
|
---|
678 | printf(NAME ": Error, failed to write the upcase table to disk.\n");
|
---|
679 | return 2;
|
---|
680 | }
|
---|
681 |
|
---|
682 | rc = bootsec_write(service_id, &cfg);
|
---|
683 | if (rc != EOK) {
|
---|
684 | printf(NAME ": Error, failed to write the VBR to disk\n");
|
---|
685 | return 2;
|
---|
686 | }
|
---|
687 |
|
---|
688 | return 0;
|
---|
689 | }
|
---|
690 |
|
---|