summaryrefslogtreecommitdiffstats
path: root/mkfs/mkfs.c
blob: 71f2b1045d21deb4b44eaa5779449d3cff7b6a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *   Copyright (C) 2019 Namjae Jeon <linkinjeon@kernel.org>
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <limits.h>
#include <errno.h>
#include <locale.h>
#include <time.h>

#include "exfat_ondisk.h"
#include "libexfat.h"
#include "mkfs.h"

struct exfat_mkfs_info finfo;

/* random serial generator based on current time */
static unsigned int get_new_serial(void)
{
	struct timespec ts;

	if (clock_gettime(CLOCK_REALTIME, &ts)) {
		/* set 0000-0000 on error */
		ts.tv_sec = 0;
		ts.tv_nsec = 0;
	}

	return (unsigned int)(ts.tv_nsec << 12 | ts.tv_sec);
}

static void exfat_setup_boot_sector(struct pbr *ppbr,
		struct exfat_blk_dev *bd, struct exfat_user_input *ui)
{
	struct bpb64 *pbpb = &ppbr->bpb;
	struct bsx64 *pbsx = &ppbr->bsx;
	unsigned int i;

	/* Fill exfat BIOS parameter block */
	pbpb->jmp_boot[0] = 0xeb;
	pbpb->jmp_boot[1] = 0x76;
	pbpb->jmp_boot[2] = 0x90;
	memcpy(pbpb->oem_name, "EXFAT   ", 8);
	memset(pbpb->res_zero, 0, 53);

	/* Fill exfat extend BIOS parameter block */
	pbsx->vol_offset = cpu_to_le64(bd->offset / bd->sector_size);
	pbsx->vol_length = cpu_to_le64(bd->size / bd->sector_size);
	pbsx->fat_offset = cpu_to_le32(finfo.fat_byte_off / bd->sector_size);
	pbsx->fat_length = cpu_to_le32(finfo.fat_byte_len / bd->sector_size);
	pbsx->clu_offset = cpu_to_le32(finfo.clu_byte_off / bd->sector_size);
	pbsx->clu_count = cpu_to_le32(finfo.total_clu_cnt);
	pbsx->root_cluster = cpu_to_le32(finfo.root_start_clu);
	pbsx->vol_serial = cpu_to_le32(finfo.volume_serial);
	pbsx->vol_flags = 0;
	pbsx->sect_size_bits = bd->sector_size_bits;
	pbsx->sect_per_clus_bits = 0;
	/* Compute base 2 logarithm of ui->cluster_size / bd->sector_size */
	for (i = ui->cluster_size / bd->sector_size; i > 1; i /= 2)
		pbsx->sect_per_clus_bits++;
	pbsx->num_fats = 1;
	/* fs_version[0] : minor and fs_version[1] : major */
	pbsx->fs_version[0] = 0;
	pbsx->fs_version[1] = 1;
	pbsx->phy_drv_no = 0x80;
	memset(pbsx->reserved2, 0, 7);

	memset(ppbr->boot_code, 0, 390);
	ppbr->signature = cpu_to_le16(PBR_SIGNATURE);

	exfat_debug("Volume Offset(sectors) : %" PRIu64 "\n",
		le64_to_cpu(pbsx->vol_offset));
	exfat_debug("Volume Length(sectors) : %" PRIu64 "\n",
		le64_to_cpu(pbsx->vol_length));
	exfat_debug("FAT Offset(sector offset) : %u\n",
		le32_to_cpu(pbsx->fat_offset));
	exfat_debug("FAT Length(sectors) : %u\n",
		le32_to_cpu(pbsx->fat_length));
	exfat_debug("Cluster Heap Offset (sector offset) : %u\n",
		le32_to_cpu(pbsx->clu_offset));
	exfat_debug("Cluster Count : %u\n",
		le32_to_cpu(pbsx->clu_count));
	exfat_debug("Root Cluster (cluster offset) : %u\n",
		le32_to_cpu(pbsx->root_cluster));
	exfat_debug("Volume Serial : 0x%x\n", le32_to_cpu(pbsx->vol_serial));
	exfat_debug("Sector Size Bits : %u\n",
		pbsx->sect_size_bits);
	exfat_debug("Sector per Cluster bits : %u\n",
		pbsx->sect_per_clus_bits);
}

static int exfat_write_boot_sector(struct exfat_blk_dev *bd,
		struct exfat_user_input *ui, unsigned int *checksum,
		bool is_backup)
{
	struct pbr *ppbr;
	unsigned int sec_idx = BOOT_SEC_IDX;
	int ret = 0;

	if (is_backup)
		sec_idx += BACKUP_BOOT_SEC_IDX;

	ppbr = malloc(bd->sector_size);
	if (!ppbr) {
		exfat_err("Cannot allocate pbr: out of memory\n");
		return -1;
	}
	memset(ppbr, 0, bd->sector_size);

	exfat_setup_boot_sector(ppbr, bd, ui);

	/* write main boot sector */
	ret = exfat_write_sector(bd, ppbr, sec_idx);
	if (ret < 0) {
		exfat_err("main boot sector write failed\n");
		ret = -1;
		goto free_ppbr;
	}

	boot_calc_checksum((unsigned char *)ppbr, bd->sector_size,
		true, checksum);

free_ppbr:
	free(ppbr);
	return ret;
}

static int exfat_write_extended_boot_sectors(struct exfat_blk_dev *bd,
		unsigned int *checksum, bool is_backup)
{
	char *peb;
	__le16 *peb_signature;
	int ret = 0;
	int i;
	unsigned int sec_idx = EXBOOT_SEC_IDX;

	peb = malloc(bd->sector_size);
	if (!peb)
		return -1;

	if (is_backup)
		sec_idx += BACKUP_BOOT_SEC_IDX;

	memset(peb, 0, bd->sector_size);
	peb_signature = (__le16*) (peb + bd->sector_size - 2);
	*peb_signature = cpu_to_le16(PBR_SIGNATURE);
	for (i = 0; i < EXBOOT_SEC_NUM; i++) {
		if (exfat_write_sector(bd, peb, sec_idx++)) {
			exfat_err("extended boot sector write failed\n");
			ret = -1;
			goto free_peb;
		}

		boot_calc_checksum((unsigned char *) peb, bd->sector_size,
			false, checksum);
	}

free_peb:
	free(peb);
	return ret;
}

static int exfat_write_oem_sector(struct exfat_blk_dev *bd,
		unsigned int *checksum, bool is_backup)
{
	char *oem;
	int ret = 0;
	unsigned int sec_idx = OEM_SEC_IDX;

	oem = malloc(bd->sector_size);
	if (!oem)
		return -1;

	if (is_backup)
		sec_idx += BACKUP_BOOT_SEC_IDX;

	memset(oem, 0xFF, bd->sector_size);
	ret = exfat_write_sector(bd, oem, sec_idx);
	if (ret) {
		exfat_err("oem sector write failed\n");
		ret = -1;
		goto free_oem;
	}

	boot_calc_checksum((unsigned char *)oem, bd->sector_size, false,
		checksum);

	/* Zero out reserved sector */
	memset(oem, 0, bd->sector_size);
	ret = exfat_write_sector(bd, oem, sec_idx + 1);
	if (ret) {
		exfat_err("reserved sector write failed\n");
		ret = -1;
		goto free_oem;
	}

	boot_calc_checksum((unsigned char *)oem, bd->sector_size, false,
		checksum);

free_oem:
	free(oem);
	return ret;
}

static int exfat_create_volume_boot_record(struct exfat_blk_dev *bd,
		struct exfat_user_input *ui, bool is_backup)
{
	unsigned int checksum = 0;
	int ret;

	ret = exfat_write_boot_sector(bd, ui, &checksum, is_backup);
	if (ret)
		return ret;
	ret = exfat_write_extended_boot_sectors(bd, &checksum, is_backup);
	if (ret)
		return ret;
	ret = exfat_write_oem_sector(bd, &checksum, is_backup);
	if (ret)
		return ret;

	return exfat_write_checksum_sector(bd, checksum, is_backup);
}

static int write_fat_entry(int fd, __le32 clu,
		unsigned long long offset)
{
	int nbyte;
	off_t fat_entry_offset = finfo.fat_byte_off + (offset * sizeof(__le32));

	nbyte = pwrite(fd, (__u8 *) &clu, sizeof(__le32), fat_entry_offset);
	if (nbyte != sizeof(int)) {
		exfat_err("write failed, offset : %llu, clu : %x\n",
			offset, clu);
		return -1;
	}

	return 0;
}

static int write_fat_entries(struct exfat_user_input *ui, int fd,
		unsigned int clu, unsigned int length)
{
	int ret;
	unsigned int count;

	count = clu + round_up(length, ui->cluster_size) / ui->cluster_size;

	for (; clu < count - 1; clu++) {
		ret = write_fat_entry(fd, cpu_to_le32(clu + 1), clu);
		if (ret)
			return ret;
	}

	ret = write_fat_entry(fd, cpu_to_le32(EXFAT_EOF_CLUSTER), clu);
	if (ret)
		return ret;

	return clu;
}

static int exfat_create_fat_table(struct exfat_blk_dev *bd,
		struct exfat_user_input *ui)
{
	int ret, clu;

	/* fat entry 0 should be media type field(0xF8) */
	ret = write_fat_entry(bd->dev_fd, cpu_to_le32(0xfffffff8), 0);
	if (ret) {
		exfat_err("fat 0 entry write failed\n");
		return ret;
	}

	/* fat entry 1 is historical precedence(0xFFFFFFFF) */
	ret = write_fat_entry(bd->dev_fd, cpu_to_le32(0xffffffff), 1);
	if (ret) {
		exfat_err("fat 1 entry write failed\n");
		return ret;
	}

	/* write bitmap entries */
	clu = write_fat_entries(ui, bd->dev_fd, EXFAT_FIRST_CLUSTER,
		finfo.bitmap_byte_len);
	if (clu < 0)
		return ret;

	/* write upcase table entries */
	clu = write_fat_entries(ui, bd->dev_fd, clu + 1, finfo.ut_byte_len);
	if (clu < 0)
		return ret;

	/* write root directory entries */
	clu = write_fat_entries(ui, bd->dev_fd, clu + 1, finfo.root_byte_len);
	if (clu < 0)
		return ret;

	finfo.used_clu_cnt = clu + 1;
	exfat_debug("Total used cluster count : %d\n", finfo.used_clu_cnt);

	return ret;
}

static int exfat_create_bitmap(struct exfat_blk_dev *bd)
{
	char *bitmap;
	unsigned int full_bytes, rem_bits, zero_offset;
	unsigned int nbytes;

	bitmap = malloc(finfo.bitmap_byte_len);
	if (!bitmap)
		return -1;

	full_bytes = finfo.used_clu_cnt / 8;
	rem_bits = finfo.used_clu_cnt % 8;
	zero_offset = full_bytes;

	memset(bitmap, 0xff, full_bytes);

	if (rem_bits != 0) {
		bitmap[full_bytes] = (1 << rem_bits) - 1;
		++zero_offset;
	}

	if (zero_offset < finfo.bitmap_byte_len)
		memset(bitmap + zero_offset, 0, finfo.bitmap_byte_len - zero_offset);


	nbytes = pwrite(bd->dev_fd, bitmap, finfo.bitmap_byte_len, finfo.bitmap_byte_off);
	if (nbytes != finfo.bitmap_byte_len) {
		exfat_err("write failed, nbytes : %d, bitmap_len : %d\n",
			nbytes, finfo.bitmap_byte_len);
		free(bitmap);
		return -1;
	}

	free(bitmap);
	return 0;
}

static int exfat_create_root_dir(struct exfat_blk_dev *bd,
		struct exfat_user_input *ui)
{
	struct exfat_dentry ed[4] = {0};
	int dentries_len = sizeof(ed);
	int nbytes;

	/* Set volume label entry */
	ed[0].type = EXFAT_VOLUME;
	memset(ed[0].vol_label, 0, 22);
	memcpy(ed[0].vol_label, ui->volume_label, ui->volume_label_len);
	ed[0].vol_char_cnt = ui->volume_label_len/2;

	/* Set volume GUID entry */
	if (ui->guid) {
		if (__exfat_set_volume_guid(&ed[1], ui->guid))
			return -1;
	} else {
		/*
		 * Since a single empty entry cannot be allocated for a
		 * file, this can reserve the entry for volume GUID.
		 */
		ed[1].type = EXFAT_GUID & ~EXFAT_INVAL;
	}

	/* Set bitmap entry */
	ed[2].type = EXFAT_BITMAP;
	ed[2].bitmap_flags = 0;
	ed[2].bitmap_start_clu = cpu_to_le32(EXFAT_FIRST_CLUSTER);
	ed[2].bitmap_size = cpu_to_le64(finfo.bitmap_byte_len);

	/* Set upcase table entry */
	ed[3].type = EXFAT_UPCASE;
	ed[3].upcase_checksum = cpu_to_le32(0xe619d30d);
	ed[3].upcase_start_clu = cpu_to_le32(finfo.ut_start_clu);
	ed[3].upcase_size = cpu_to_le64(EXFAT_UPCASE_TABLE_SIZE);

	nbytes = pwrite(bd->dev_fd, ed, dentries_len, finfo.root_byte_off);
	if (nbytes != dentries_len) {
		exfat_err("write failed, nbytes : %d, dentries_len : %d\n",
			nbytes, dentries_len);
		return -1;
	}

	return 0;
}

static void usage(void)
{
	fputs("Usage: mkfs.exfat\n"
		"\t-L | --volume-label=label                              Set volume label\n"
		"\t-U | --volume-guid=guid                                Set volume GUID\n"
		"\t-s | --sector-size=size(or suffixed by 'K')            Specify sector size\n"
		"\t-c | --cluster-size=size(or suffixed by 'K' or 'M')    Specify cluster size\n"
		"\t-b | --boundary-align=size(or suffixed by 'K' or 'M')  Specify boundary alignment\n"
		"\t     --pack-bitmap                                     Move bitmap into FAT segment\n"
		"\t-f | --full-format                                     Full format\n"
		"\t-V | --version                                         Show version\n"
		"\t-q | --quiet                                           Print only errors\n"
		"\t-v | --verbose                                         Print debug\n"
		"\t-h | --help                                            Show help\n",
		stderr);

	exit(EXIT_FAILURE);
}

#define PACK_BITMAP (CHAR_MAX + 1)

static const struct option opts[] = {
	{"volume-label",	required_argument,	NULL,	'L' },
	{"volume-guid",		required_argument,	NULL,	'U' },
	{"sector-size",		required_argument,	NULL,	's' },
	{"cluster-size",	required_argument,	NULL,	'c' },
	{"boundary-align",	required_argument,	NULL,	'b' },
	{"pack-bitmap",		no_argument,		NULL,	PACK_BITMAP },
	{"full-format",		no_argument,		NULL,	'f' },
	{"version",		no_argument,		NULL,	'V' },
	{"quiet",		no_argument,		NULL,	'q' },
	{"verbose",		no_argument,		NULL,	'v' },
	{"help",		no_argument,		NULL,	'h' },
	{"?",			no_argument,		NULL,	'?' },
	{NULL,			0,			NULL,	 0  }
};

/*
 * Moves the bitmap to just before the alignment boundary if there is space
 * between the boundary and the end of the FAT. This may allow the FAT and the
 * bitmap to share the same allocation unit on flash media, thereby improving
 * performance and endurance.
 */
static int exfat_pack_bitmap(const struct exfat_user_input *ui)
{
	unsigned int fat_byte_end = finfo.fat_byte_off + finfo.fat_byte_len,
		bitmap_byte_len = finfo.bitmap_byte_len,
		bitmap_clu_len = round_up(bitmap_byte_len, ui->cluster_size),
		bitmap_clu_cnt, total_clu_cnt, new_bitmap_clu_len;

	for (;;) {
		bitmap_clu_cnt = bitmap_clu_len / ui->cluster_size;
		if (finfo.clu_byte_off - bitmap_clu_len < fat_byte_end ||
				finfo.total_clu_cnt > EXFAT_MAX_NUM_CLUSTER -
					bitmap_clu_cnt)
			return -1;
		total_clu_cnt = finfo.total_clu_cnt + bitmap_clu_cnt;
		bitmap_byte_len = round_up(total_clu_cnt, 8) / 8;
		new_bitmap_clu_len = round_up(bitmap_byte_len, ui->cluster_size);
		if (new_bitmap_clu_len == bitmap_clu_len) {
			finfo.clu_byte_off -= bitmap_clu_len;
			finfo.total_clu_cnt = total_clu_cnt;
			finfo.bitmap_byte_off -= bitmap_clu_len;
			finfo.bitmap_byte_len = bitmap_byte_len;
			return 0;
		}
		bitmap_clu_len = new_bitmap_clu_len;
	}
}

static int exfat_build_mkfs_info(struct exfat_blk_dev *bd,
		struct exfat_user_input *ui)
{
	unsigned long long total_clu_cnt;
	unsigned long long max_clusters;
	int clu_len;
	int num_fats = 1;

	if (ui->cluster_size < bd->sector_size) {
		exfat_err("cluster size (%u bytes) is smaller than sector size (%u bytes)\n",
			  ui->cluster_size, bd->sector_size);
		return -1;
	}
	if (ui->boundary_align < bd->sector_size) {
		exfat_err("boundary alignment is too small (min %d)\n",
				bd->sector_size);
		return -1;
	}
	finfo.fat_byte_off = round_up(bd->offset + 24 * bd->sector_size,
			ui->boundary_align) - bd->offset;

	max_clusters = (bd->size - finfo.fat_byte_off - 8 * num_fats - 1) /
		(ui->cluster_size + 4 * num_fats) + 1;
	/* Prevent integer overflow when computing the FAT length */
	if (max_clusters > UINT_MAX / 4 - 2) {
		exfat_err("cluster size (%u bytes) is too small\n", ui->cluster_size);
		return -1;
	}
	finfo.fat_byte_len = round_up((max_clusters + 2) * 4, bd->sector_size);
	finfo.clu_byte_off = round_up(bd->offset + finfo.fat_byte_off +
		finfo.fat_byte_len * num_fats, ui->boundary_align) - bd->offset;
	if (bd->size <= finfo.clu_byte_off) {
		exfat_err("boundary alignment is too big\n");
		return -1;
	}
	total_clu_cnt = (bd->size - finfo.clu_byte_off) / ui->cluster_size;
	if (total_clu_cnt > EXFAT_MAX_NUM_CLUSTER) {
		exfat_err("cluster size is too small\n");
		return -1;
	}
	finfo.total_clu_cnt = (unsigned int) total_clu_cnt;

	finfo.bitmap_byte_off = finfo.clu_byte_off;
	finfo.bitmap_byte_len = round_up(finfo.total_clu_cnt, 8) / 8;
	if (ui->pack_bitmap)
		exfat_pack_bitmap(ui);
	clu_len = round_up(finfo.bitmap_byte_len, ui->cluster_size);

	finfo.ut_start_clu = EXFAT_FIRST_CLUSTER + clu_len / ui->cluster_size;
	finfo.ut_byte_off = finfo.bitmap_byte_off + clu_len;
	finfo.ut_byte_len = EXFAT_UPCASE_TABLE_SIZE;
	clu_len = round_up(finfo.ut_byte_len, ui->cluster_size);

	finfo.root_start_clu = finfo.ut_start_clu + clu_len / ui->cluster_size;
	finfo.root_byte_off = finfo.ut_byte_off + clu_len;
	finfo.root_byte_len = sizeof(struct exfat_dentry) * 3;
	finfo.volume_serial = get_new_serial();

	return 0;
}

static int exfat_zero_out_disk(struct exfat_blk_dev *bd,
		struct exfat_user_input *ui)
{
	int ret;
	unsigned long long total_written = 0;
	unsigned long long size;

	if (ui->quick)
		size = finfo.root_byte_off + ui->cluster_size;
	else
		size = bd->size;

	ret = exfat_write_zero(bd->dev_fd, size, 0);
	if (ret) {
		exfat_err("write failed(errno : %d)\n", errno);
		return ret;
	}

	exfat_debug("zero out written size : %llu, disk size : %llu\n",
		total_written, bd->size);
	return 0;
}

static int make_exfat(struct exfat_blk_dev *bd, struct exfat_user_input *ui)
{
	int ret;

	exfat_info("Creating exFAT filesystem(%s, cluster size=%u)\n\n",
		ui->dev_name, ui->cluster_size);

	exfat_info("Writing volume boot record: ");
	ret = exfat_create_volume_boot_record(bd, ui, 0);
	exfat_info("%s\n", ret ? "failed" : "done");
	if (ret)
		return ret;

	exfat_info("Writing backup volume boot record: ");
	/* backup sector */
	ret = exfat_create_volume_boot_record(bd, ui, 1);
	exfat_info("%s\n", ret ? "failed" : "done");
	if (ret)
		return ret;

	exfat_info("Fat table creation: ");
	ret = exfat_create_fat_table(bd, ui);
	exfat_info("%s\n", ret ? "failed" : "done");
	if (ret)
		return ret;

	exfat_info("Allocation bitmap creation: ");
	ret = exfat_create_bitmap(bd);
	exfat_info("%s\n", ret ? "failed" : "done");
	if (ret)
		return ret;

	exfat_info("Upcase table creation: ");
	ret = exfat_create_upcase_table(bd);
	exfat_info("%s\n", ret ? "failed" : "done");
	if (ret)
		return ret;

	exfat_info("Writing root directory entry: ");
	ret = exfat_create_root_dir(bd, ui);
	exfat_info("%s\n", ret ? "failed" : "done");
	if (ret)
		return ret;

	return 0;
}

static long long parse_size(const char *size)
{
	char *data_unit;
	unsigned long long byte_size = strtoull(size, &data_unit, 0);

	switch (*data_unit) {
	case 'M':
	case 'm':
		byte_size <<= 20;
		break;
	case 'K':
	case 'k':
		byte_size <<= 10;
		break;
	case '\0':
		break;
	default:
		exfat_err("Wrong unit input('%c') for size\n",
				*data_unit);
		return -EINVAL;
	}

	return byte_size;
}

int main(int argc, char *argv[])
{
	int c;
	int ret = EXIT_FAILURE;
	struct exfat_blk_dev bd;
	struct exfat_user_input ui;
	bool version_only = false;
	bool quiet = false;

	init_user_input(&ui);

	if (!setlocale(LC_CTYPE, ""))
		exfat_err("failed to init locale/codeset\n");

	opterr = 0;
	while ((c = getopt_long(argc, argv, "n:L:U:s:c:b:fVqvh", opts, NULL)) != EOF)
		switch (c) {
		/*
		 * Make 'n' option fallthrough to 'L' option for for backward
		 * compatibility with old utils.
		 */
		case 'n':
		case 'L':
		{
			ret = exfat_utf16_enc(optarg,
				ui.volume_label, sizeof(ui.volume_label));
			if (ret < 0)
				goto out;

			ui.volume_label_len = ret;
			break;
		}
		case 'U':
			if (*optarg != '\0' && *optarg != '\r')
				ui.guid = optarg;
			break;
		case 's':
			ret = parse_size(optarg);
			if (ret < 0)
				goto out;
			else if (ret & (ret - 1)) {
				exfat_err("sector size(%d) is not a power of 2\n",
					ret);
				goto out;
			} else if ((ret & 0x1e00) == 0) {
				exfat_err("sector size(%d) must be 512, 1024, "
					"2048 or 4096 bytes\n",
					ret);
				goto out;
			}
			ui.sector_size = ret;
			break;
		case 'c':
			ret = parse_size(optarg);
			if (ret < 0)
				goto out;
			else if (ret & (ret - 1)) {
				exfat_err("cluster size(%d) is not a power of 2)\n",
					ret);
				goto out;
			} else if (ret > EXFAT_MAX_CLUSTER_SIZE) {
				exfat_err("cluster size(%d) exceeds max cluster size(%d)\n",
					ui.cluster_size, EXFAT_MAX_CLUSTER_SIZE);
				goto out;
			}
			ui.cluster_size = ret;
			break;
		case 'b':
			ret = parse_size(optarg);
			if (ret < 0)
				goto out;
			else if (ret & (ret - 1)) {
				exfat_err("boundary align(%d) is not a power of 2)\n",
					ret);
				goto out;
			}
			ui.boundary_align = ret;
			break;
		case PACK_BITMAP:
			ui.pack_bitmap = true;
			break;
		case 'f':
			ui.quick = false;
			break;
		case 'V':
			version_only = true;
			break;
		case 'q':
			print_level = EXFAT_ERROR;
			quiet = true;
			break;
		case 'v':
			print_level = EXFAT_DEBUG;
			break;
		case '?':
		case 'h':
		default:
			usage();
	}

	if (version_only) {
		show_version();
		exit(EXIT_FAILURE);
	} else if (!quiet) {
		show_version();
	}

	if (argc - optind != 1) {
		usage();
	}

	if (ui.sector_size && ui.cluster_size && ui.sector_size > ui.cluster_size) {
		exfat_err("cluster size (%u bytes) is smaller than sector size (%u bytes)\n",
				  ui.cluster_size, ui.sector_size);
		ret = -1;
		goto out;
	}

	ui.dev_name = argv[optind];

	ret = exfat_get_blk_dev_info(&ui, &bd);
	if (ret < 0)
		goto out;

	ret = exfat_build_mkfs_info(&bd, &ui);
	if (ret)
		goto close;

	ret = exfat_zero_out_disk(&bd, &ui);
	if (ret)
		goto close;

	ret = make_exfat(&bd, &ui);
	if (ret)
		goto close;

	exfat_info("Synchronizing...\n");
	ret = fsync(bd.dev_fd);
close:
	close(bd.dev_fd);
out:
	if (!ret)
		exfat_info("\nexFAT format complete!\n");
	else
		exfat_err("\nexFAT format fail!\n");
	return ret;
}