source: mainline/uspace/app/mkexfat/mkexfat.c@ 552efe3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 552efe3 was 552efe3, checked in by Maurizio Lombardi <m.lombardi85@…>, 13 years ago

mkexfat: Fix memory access bug in the upcase_table_write() function.

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