1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2019 Namjae Jeon <[email protected]>
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdbool.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/ioctl.h>
14 #include <fcntl.h>
15 #include <getopt.h>
16 #include <inttypes.h>
17 #include <limits.h>
18 #include <errno.h>
19 #include <locale.h>
20 #include <time.h>
21
22 #include "exfat_ondisk.h"
23 #include "libexfat.h"
24 #include "mkfs.h"
25
26 struct exfat_mkfs_info finfo;
27
28 /* random serial generator based on current time */
get_new_serial(void)29 static unsigned int get_new_serial(void)
30 {
31 struct timespec ts;
32
33 if (clock_gettime(CLOCK_REALTIME, &ts)) {
34 /* set 0000-0000 on error */
35 ts.tv_sec = 0;
36 ts.tv_nsec = 0;
37 }
38
39 return (unsigned int)(ts.tv_nsec << 12 | ts.tv_sec);
40 }
41
exfat_setup_boot_sector(struct pbr * ppbr,struct exfat_blk_dev * bd,struct exfat_user_input * ui)42 static void exfat_setup_boot_sector(struct pbr *ppbr,
43 struct exfat_blk_dev *bd, struct exfat_user_input *ui)
44 {
45 struct bpb64 *pbpb = &ppbr->bpb;
46 struct bsx64 *pbsx = &ppbr->bsx;
47 unsigned int i;
48
49 /* Fill exfat BIOS parameter block */
50 pbpb->jmp_boot[0] = 0xeb;
51 pbpb->jmp_boot[1] = 0x76;
52 pbpb->jmp_boot[2] = 0x90;
53 memcpy(pbpb->oem_name, "EXFAT ", 8);
54 memset(pbpb->res_zero, 0, 53);
55
56 /* Fill exfat extend BIOS parameter block */
57 pbsx->vol_offset = cpu_to_le64(bd->offset / bd->sector_size);
58 pbsx->vol_length = cpu_to_le64(bd->size / bd->sector_size);
59 pbsx->fat_offset = cpu_to_le32(finfo.fat_byte_off / bd->sector_size);
60 pbsx->fat_length = cpu_to_le32(finfo.fat_byte_len / bd->sector_size);
61 pbsx->clu_offset = cpu_to_le32(finfo.clu_byte_off / bd->sector_size);
62 pbsx->clu_count = cpu_to_le32(finfo.total_clu_cnt);
63 pbsx->root_cluster = cpu_to_le32(finfo.root_start_clu);
64 pbsx->vol_serial = cpu_to_le32(finfo.volume_serial);
65 pbsx->vol_flags = 0;
66 pbsx->sect_size_bits = bd->sector_size_bits;
67 pbsx->sect_per_clus_bits = 0;
68 /* Compute base 2 logarithm of ui->cluster_size / bd->sector_size */
69 for (i = ui->cluster_size / bd->sector_size; i > 1; i /= 2)
70 pbsx->sect_per_clus_bits++;
71 pbsx->num_fats = 1;
72 /* fs_version[0] : minor and fs_version[1] : major */
73 pbsx->fs_version[0] = 0;
74 pbsx->fs_version[1] = 1;
75 pbsx->phy_drv_no = 0x80;
76 memset(pbsx->reserved2, 0, 7);
77
78 memset(ppbr->boot_code, 0, 390);
79 ppbr->signature = cpu_to_le16(PBR_SIGNATURE);
80
81 exfat_debug("Volume Offset(sectors) : %" PRIu64 "\n",
82 le64_to_cpu(pbsx->vol_offset));
83 exfat_debug("Volume Length(sectors) : %" PRIu64 "\n",
84 le64_to_cpu(pbsx->vol_length));
85 exfat_debug("FAT Offset(sector offset) : %u\n",
86 le32_to_cpu(pbsx->fat_offset));
87 exfat_debug("FAT Length(sectors) : %u\n",
88 le32_to_cpu(pbsx->fat_length));
89 exfat_debug("Cluster Heap Offset (sector offset) : %u\n",
90 le32_to_cpu(pbsx->clu_offset));
91 exfat_debug("Cluster Count : %u\n",
92 le32_to_cpu(pbsx->clu_count));
93 exfat_debug("Root Cluster (cluster offset) : %u\n",
94 le32_to_cpu(pbsx->root_cluster));
95 exfat_debug("Volume Serial : 0x%x\n", le32_to_cpu(pbsx->vol_serial));
96 exfat_debug("Sector Size Bits : %u\n",
97 pbsx->sect_size_bits);
98 exfat_debug("Sector per Cluster bits : %u\n",
99 pbsx->sect_per_clus_bits);
100 }
101
exfat_write_boot_sector(struct exfat_blk_dev * bd,struct exfat_user_input * ui,unsigned int * checksum,bool is_backup)102 static int exfat_write_boot_sector(struct exfat_blk_dev *bd,
103 struct exfat_user_input *ui, unsigned int *checksum,
104 bool is_backup)
105 {
106 struct pbr *ppbr;
107 unsigned int sec_idx = BOOT_SEC_IDX;
108 int ret = 0;
109
110 if (is_backup)
111 sec_idx += BACKUP_BOOT_SEC_IDX;
112
113 ppbr = malloc(bd->sector_size);
114 if (!ppbr) {
115 exfat_err("Cannot allocate pbr: out of memory\n");
116 return -1;
117 }
118 memset(ppbr, 0, bd->sector_size);
119
120 exfat_setup_boot_sector(ppbr, bd, ui);
121
122 /* write main boot sector */
123 ret = exfat_write_sector(bd, ppbr, sec_idx);
124 if (ret < 0) {
125 exfat_err("main boot sector write failed\n");
126 ret = -1;
127 goto free_ppbr;
128 }
129
130 boot_calc_checksum((unsigned char *)ppbr, bd->sector_size,
131 true, checksum);
132
133 free_ppbr:
134 free(ppbr);
135 return ret;
136 }
137
exfat_write_extended_boot_sectors(struct exfat_blk_dev * bd,unsigned int * checksum,bool is_backup)138 static int exfat_write_extended_boot_sectors(struct exfat_blk_dev *bd,
139 unsigned int *checksum, bool is_backup)
140 {
141 char *peb;
142 __le16 *peb_signature;
143 int ret = 0;
144 int i;
145 unsigned int sec_idx = EXBOOT_SEC_IDX;
146
147 peb = malloc(bd->sector_size);
148 if (!peb)
149 return -1;
150
151 if (is_backup)
152 sec_idx += BACKUP_BOOT_SEC_IDX;
153
154 memset(peb, 0, bd->sector_size);
155 peb_signature = (__le16*) (peb + bd->sector_size - 2);
156 *peb_signature = cpu_to_le16(PBR_SIGNATURE);
157 for (i = 0; i < EXBOOT_SEC_NUM; i++) {
158 if (exfat_write_sector(bd, peb, sec_idx++)) {
159 exfat_err("extended boot sector write failed\n");
160 ret = -1;
161 goto free_peb;
162 }
163
164 boot_calc_checksum((unsigned char *) peb, bd->sector_size,
165 false, checksum);
166 }
167
168 free_peb:
169 free(peb);
170 return ret;
171 }
172
exfat_write_oem_sector(struct exfat_blk_dev * bd,unsigned int * checksum,bool is_backup)173 static int exfat_write_oem_sector(struct exfat_blk_dev *bd,
174 unsigned int *checksum, bool is_backup)
175 {
176 char *oem;
177 int ret = 0;
178 unsigned int sec_idx = OEM_SEC_IDX;
179
180 oem = malloc(bd->sector_size);
181 if (!oem)
182 return -1;
183
184 if (is_backup)
185 sec_idx += BACKUP_BOOT_SEC_IDX;
186
187 memset(oem, 0xFF, bd->sector_size);
188 ret = exfat_write_sector(bd, oem, sec_idx);
189 if (ret) {
190 exfat_err("oem sector write failed\n");
191 ret = -1;
192 goto free_oem;
193 }
194
195 boot_calc_checksum((unsigned char *)oem, bd->sector_size, false,
196 checksum);
197
198 /* Zero out reserved sector */
199 memset(oem, 0, bd->sector_size);
200 ret = exfat_write_sector(bd, oem, sec_idx + 1);
201 if (ret) {
202 exfat_err("reserved sector write failed\n");
203 ret = -1;
204 goto free_oem;
205 }
206
207 boot_calc_checksum((unsigned char *)oem, bd->sector_size, false,
208 checksum);
209
210 free_oem:
211 free(oem);
212 return ret;
213 }
214
exfat_create_volume_boot_record(struct exfat_blk_dev * bd,struct exfat_user_input * ui,bool is_backup)215 static int exfat_create_volume_boot_record(struct exfat_blk_dev *bd,
216 struct exfat_user_input *ui, bool is_backup)
217 {
218 unsigned int checksum = 0;
219 int ret;
220
221 ret = exfat_write_boot_sector(bd, ui, &checksum, is_backup);
222 if (ret)
223 return ret;
224 ret = exfat_write_extended_boot_sectors(bd, &checksum, is_backup);
225 if (ret)
226 return ret;
227 ret = exfat_write_oem_sector(bd, &checksum, is_backup);
228 if (ret)
229 return ret;
230
231 return exfat_write_checksum_sector(bd, checksum, is_backup);
232 }
233
write_fat_entry(int fd,__le32 clu,unsigned long long offset)234 static int write_fat_entry(int fd, __le32 clu,
235 unsigned long long offset)
236 {
237 int nbyte;
238 off_t fat_entry_offset = finfo.fat_byte_off + (offset * sizeof(__le32));
239
240 nbyte = pwrite(fd, (__u8 *) &clu, sizeof(__le32), fat_entry_offset);
241 if (nbyte != sizeof(int)) {
242 exfat_err("write failed, offset : %llu, clu : %x\n",
243 offset, clu);
244 return -1;
245 }
246
247 return 0;
248 }
249
write_fat_entries(struct exfat_user_input * ui,int fd,unsigned int clu,unsigned int length)250 static int write_fat_entries(struct exfat_user_input *ui, int fd,
251 unsigned int clu, unsigned int length)
252 {
253 int ret;
254 unsigned int count;
255
256 count = clu + round_up(length, ui->cluster_size) / ui->cluster_size;
257
258 for (; clu < count - 1; clu++) {
259 ret = write_fat_entry(fd, cpu_to_le32(clu + 1), clu);
260 if (ret)
261 return ret;
262 }
263
264 ret = write_fat_entry(fd, cpu_to_le32(EXFAT_EOF_CLUSTER), clu);
265 if (ret)
266 return ret;
267
268 return clu;
269 }
270
exfat_create_fat_table(struct exfat_blk_dev * bd,struct exfat_user_input * ui)271 static int exfat_create_fat_table(struct exfat_blk_dev *bd,
272 struct exfat_user_input *ui)
273 {
274 int ret, clu;
275
276 /* fat entry 0 should be media type field(0xF8) */
277 ret = write_fat_entry(bd->dev_fd, cpu_to_le32(0xfffffff8), 0);
278 if (ret) {
279 exfat_err("fat 0 entry write failed\n");
280 return ret;
281 }
282
283 /* fat entry 1 is historical precedence(0xFFFFFFFF) */
284 ret = write_fat_entry(bd->dev_fd, cpu_to_le32(0xffffffff), 1);
285 if (ret) {
286 exfat_err("fat 1 entry write failed\n");
287 return ret;
288 }
289
290 /* write bitmap entries */
291 clu = write_fat_entries(ui, bd->dev_fd, EXFAT_FIRST_CLUSTER,
292 finfo.bitmap_byte_len);
293 if (clu < 0)
294 return ret;
295
296 /* write upcase table entries */
297 clu = write_fat_entries(ui, bd->dev_fd, clu + 1, finfo.ut_byte_len);
298 if (clu < 0)
299 return ret;
300
301 /* write root directory entries */
302 clu = write_fat_entries(ui, bd->dev_fd, clu + 1, finfo.root_byte_len);
303 if (clu < 0)
304 return ret;
305
306 finfo.used_clu_cnt = clu + 1;
307 exfat_debug("Total used cluster count : %d\n", finfo.used_clu_cnt);
308
309 return ret;
310 }
311
exfat_create_bitmap(struct exfat_blk_dev * bd)312 static int exfat_create_bitmap(struct exfat_blk_dev *bd)
313 {
314 char *bitmap;
315 unsigned int i, nbytes;
316
317 bitmap = calloc(round_up(finfo.bitmap_byte_len, sizeof(bitmap_t)),
318 sizeof(*bitmap));
319 if (!bitmap)
320 return -1;
321
322 for (i = EXFAT_FIRST_CLUSTER; i < finfo.used_clu_cnt; i++)
323 exfat_bitmap_set(bitmap, i);
324
325 nbytes = pwrite(bd->dev_fd, bitmap, finfo.bitmap_byte_len, finfo.bitmap_byte_off);
326 if (nbytes != finfo.bitmap_byte_len) {
327 exfat_err("write failed, nbytes : %d, bitmap_len : %d\n",
328 nbytes, finfo.bitmap_byte_len);
329 free(bitmap);
330 return -1;
331 }
332
333 free(bitmap);
334 return 0;
335 }
336
exfat_create_root_dir(struct exfat_blk_dev * bd,struct exfat_user_input * ui)337 static int exfat_create_root_dir(struct exfat_blk_dev *bd,
338 struct exfat_user_input *ui)
339 {
340 struct exfat_dentry ed[3] = {0};
341 int dentries_len = sizeof(struct exfat_dentry) * 3;
342 int nbytes;
343
344 /* Set volume label entry */
345 ed[0].type = EXFAT_VOLUME;
346 memset(ed[0].vol_label, 0, 22);
347 memcpy(ed[0].vol_label, ui->volume_label, ui->volume_label_len);
348 ed[0].vol_char_cnt = ui->volume_label_len/2;
349
350 /* Set bitmap entry */
351 ed[1].type = EXFAT_BITMAP;
352 ed[1].bitmap_flags = 0;
353 ed[1].bitmap_start_clu = cpu_to_le32(EXFAT_FIRST_CLUSTER);
354 ed[1].bitmap_size = cpu_to_le64(finfo.bitmap_byte_len);
355
356 /* Set upcase table entry */
357 ed[2].type = EXFAT_UPCASE;
358 ed[2].upcase_checksum = cpu_to_le32(0xe619d30d);
359 ed[2].upcase_start_clu = cpu_to_le32(finfo.ut_start_clu);
360 ed[2].upcase_size = cpu_to_le64(EXFAT_UPCASE_TABLE_SIZE);
361
362 nbytes = pwrite(bd->dev_fd, ed, dentries_len, finfo.root_byte_off);
363 if (nbytes != dentries_len) {
364 exfat_err("write failed, nbytes : %d, dentries_len : %d\n",
365 nbytes, dentries_len);
366 return -1;
367 }
368
369 return 0;
370 }
371
usage(void)372 static void usage(void)
373 {
374 fputs("Usage: mkfs.exfat\n"
375 "\t-L | --volume-label=label Set volume label\n"
376 "\t-c | --cluster-size=size(or suffixed by 'K' or 'M') Specify cluster size\n"
377 "\t-b | --boundary-align=size(or suffixed by 'K' or 'M') Specify boundary alignment\n"
378 "\t --pack-bitmap Move bitmap into FAT segment\n"
379 "\t-f | --full-format Full format\n"
380 "\t-V | --version Show version\n"
381 "\t-v | --verbose Print debug\n"
382 "\t-h | --help Show help\n",
383 stderr);
384
385 exit(EXIT_FAILURE);
386 }
387
388 #define PACK_BITMAP (CHAR_MAX + 1)
389
390 static const struct option opts[] = {
391 {"volume-label", required_argument, NULL, 'L' },
392 {"cluster-size", required_argument, NULL, 'c' },
393 {"boundary-align", required_argument, NULL, 'b' },
394 {"pack-bitmap", no_argument, NULL, PACK_BITMAP },
395 {"full-format", no_argument, NULL, 'f' },
396 {"version", no_argument, NULL, 'V' },
397 {"verbose", no_argument, NULL, 'v' },
398 {"help", no_argument, NULL, 'h' },
399 {"?", no_argument, NULL, '?' },
400 {NULL, 0, NULL, 0 }
401 };
402
403 /*
404 * Moves the bitmap to just before the alignment boundary if there is space
405 * between the boundary and the end of the FAT. This may allow the FAT and the
406 * bitmap to share the same allocation unit on flash media, thereby improving
407 * performance and endurance.
408 */
exfat_pack_bitmap(const struct exfat_user_input * ui)409 static int exfat_pack_bitmap(const struct exfat_user_input *ui)
410 {
411 unsigned int fat_byte_end = finfo.fat_byte_off + finfo.fat_byte_len,
412 bitmap_byte_len = finfo.bitmap_byte_len,
413 bitmap_clu_len = round_up(bitmap_byte_len, ui->cluster_size),
414 bitmap_clu_cnt, total_clu_cnt, new_bitmap_clu_len;
415
416 for (;;) {
417 bitmap_clu_cnt = bitmap_clu_len / ui->cluster_size;
418 if (finfo.clu_byte_off - bitmap_clu_len < fat_byte_end ||
419 finfo.total_clu_cnt > EXFAT_MAX_NUM_CLUSTER -
420 bitmap_clu_cnt)
421 return -1;
422 total_clu_cnt = finfo.total_clu_cnt + bitmap_clu_cnt;
423 bitmap_byte_len = round_up(total_clu_cnt, 8) / 8;
424 new_bitmap_clu_len = round_up(bitmap_byte_len, ui->cluster_size);
425 if (new_bitmap_clu_len == bitmap_clu_len) {
426 finfo.clu_byte_off -= bitmap_clu_len;
427 finfo.total_clu_cnt = total_clu_cnt;
428 finfo.bitmap_byte_off -= bitmap_clu_len;
429 finfo.bitmap_byte_len = bitmap_byte_len;
430 return 0;
431 }
432 bitmap_clu_len = new_bitmap_clu_len;
433 }
434 }
435
exfat_build_mkfs_info(struct exfat_blk_dev * bd,struct exfat_user_input * ui)436 static int exfat_build_mkfs_info(struct exfat_blk_dev *bd,
437 struct exfat_user_input *ui)
438 {
439 unsigned long long total_clu_cnt;
440 int clu_len;
441
442 if (ui->cluster_size < bd->sector_size) {
443 exfat_err("cluster size (%u bytes) is smaller than sector size (%u bytes)\n",
444 ui->cluster_size, bd->sector_size);
445 return -1;
446 }
447 if (ui->boundary_align < bd->sector_size) {
448 exfat_err("boundary alignment is too small (min %d)\n",
449 bd->sector_size);
450 return -1;
451 }
452 finfo.fat_byte_off = round_up(bd->offset + 24 * bd->sector_size,
453 ui->boundary_align) - bd->offset;
454 /* Prevent integer overflow when computing the FAT length */
455 if (bd->num_clusters > UINT32_MAX / 4) {
456 exfat_err("cluster size (%u bytes) is too small\n", ui->cluster_size);
457 return -1;
458 }
459 finfo.fat_byte_len = round_up((bd->num_clusters * 4), ui->cluster_size);
460 finfo.clu_byte_off = round_up(bd->offset + finfo.fat_byte_off +
461 finfo.fat_byte_len, ui->boundary_align) - bd->offset;
462 if (bd->size <= finfo.clu_byte_off) {
463 exfat_err("boundary alignment is too big\n");
464 return -1;
465 }
466 total_clu_cnt = (bd->size - finfo.clu_byte_off) / ui->cluster_size;
467 if (total_clu_cnt > EXFAT_MAX_NUM_CLUSTER) {
468 exfat_err("cluster size is too small\n");
469 return -1;
470 }
471 finfo.total_clu_cnt = (unsigned int) total_clu_cnt;
472
473 finfo.bitmap_byte_off = finfo.clu_byte_off;
474 finfo.bitmap_byte_len = round_up(finfo.total_clu_cnt, 8) / 8;
475 if (ui->pack_bitmap)
476 exfat_pack_bitmap(ui);
477 clu_len = round_up(finfo.bitmap_byte_len, ui->cluster_size);
478
479 finfo.ut_start_clu = EXFAT_FIRST_CLUSTER + clu_len / ui->cluster_size;
480 finfo.ut_byte_off = finfo.bitmap_byte_off + clu_len;
481 finfo.ut_byte_len = EXFAT_UPCASE_TABLE_SIZE;
482 clu_len = round_up(finfo.ut_byte_len, ui->cluster_size);
483
484 finfo.root_start_clu = finfo.ut_start_clu + clu_len / ui->cluster_size;
485 finfo.root_byte_off = finfo.ut_byte_off + clu_len;
486 finfo.root_byte_len = sizeof(struct exfat_dentry) * 3;
487 finfo.volume_serial = get_new_serial();
488
489 return 0;
490 }
491
exfat_zero_out_disk(struct exfat_blk_dev * bd,struct exfat_user_input * ui)492 static int exfat_zero_out_disk(struct exfat_blk_dev *bd,
493 struct exfat_user_input *ui)
494 {
495 int nbytes;
496 unsigned long long total_written = 0;
497 char *buf;
498 unsigned int chunk_size = ui->cluster_size;
499 unsigned long long size;
500
501 if (ui->quick)
502 size = finfo.root_byte_off + chunk_size;
503 else
504 size = bd->size;
505
506 buf = malloc(chunk_size);
507 if (!buf)
508 return -1;
509
510 memset(buf, 0, chunk_size);
511 lseek(bd->dev_fd, 0, SEEK_SET);
512 do {
513
514 nbytes = write(bd->dev_fd, buf, chunk_size);
515 if (nbytes <= 0) {
516 if (nbytes < 0)
517 exfat_err("write failed(errno : %d)\n", errno);
518 break;
519 }
520 total_written += nbytes;
521 } while (total_written < size);
522
523 free(buf);
524 exfat_debug("zero out written size : %llu, disk size : %llu\n",
525 total_written, bd->size);
526 return 0;
527 }
528
make_exfat(struct exfat_blk_dev * bd,struct exfat_user_input * ui)529 static int make_exfat(struct exfat_blk_dev *bd, struct exfat_user_input *ui)
530 {
531 int ret;
532
533 exfat_info("Creating exFAT filesystem(%s, cluster size=%u)\n\n",
534 ui->dev_name, ui->cluster_size);
535
536 exfat_info("Writing volume boot record: ");
537 ret = exfat_create_volume_boot_record(bd, ui, 0);
538 exfat_info("%s\n", ret ? "failed" : "done");
539 if (ret)
540 return ret;
541
542 exfat_info("Writing backup volume boot record: ");
543 /* backup sector */
544 ret = exfat_create_volume_boot_record(bd, ui, 1);
545 exfat_info("%s\n", ret ? "failed" : "done");
546 if (ret)
547 return ret;
548
549 exfat_info("Fat table creation: ");
550 ret = exfat_create_fat_table(bd, ui);
551 exfat_info("%s\n", ret ? "failed" : "done");
552 if (ret)
553 return ret;
554
555 exfat_info("Allocation bitmap creation: ");
556 ret = exfat_create_bitmap(bd);
557 exfat_info("%s\n", ret ? "failed" : "done");
558 if (ret)
559 return ret;
560
561 exfat_info("Upcase table creation: ");
562 ret = exfat_create_upcase_table(bd);
563 exfat_info("%s\n", ret ? "failed" : "done");
564 if (ret)
565 return ret;
566
567 exfat_info("Writing root directory entry: ");
568 ret = exfat_create_root_dir(bd, ui);
569 exfat_info("%s\n", ret ? "failed" : "done");
570 if (ret)
571 return ret;
572
573 return 0;
574 }
575
parse_size(const char * size)576 static long long parse_size(const char *size)
577 {
578 char *data_unit;
579 unsigned long long byte_size = strtoull(size, &data_unit, 0);
580
581 switch (*data_unit) {
582 case 'M':
583 case 'm':
584 byte_size <<= 20;
585 break;
586 case 'K':
587 case 'k':
588 byte_size <<= 10;
589 break;
590 case '\0':
591 break;
592 default:
593 exfat_err("Wrong unit input('%c') for size\n",
594 *data_unit);
595 return -EINVAL;
596 }
597
598 return byte_size;
599 }
600
main(int argc,char * argv[])601 int main(int argc, char *argv[])
602 {
603 int c;
604 int ret = EXIT_FAILURE;
605 struct exfat_blk_dev bd;
606 struct exfat_user_input ui;
607 bool version_only = false;
608
609 init_user_input(&ui);
610
611 if (!setlocale(LC_CTYPE, ""))
612 exfat_err("failed to init locale/codeset\n");
613
614 opterr = 0;
615 while ((c = getopt_long(argc, argv, "n:L:c:b:fVvh", opts, NULL)) != EOF)
616 switch (c) {
617 /*
618 * Make 'n' option fallthrough to 'L' option for for backward
619 * compatibility with old utils.
620 */
621 case 'n':
622 case 'L':
623 {
624 ret = exfat_utf16_enc(optarg,
625 ui.volume_label, sizeof(ui.volume_label));
626 if (ret < 0)
627 goto out;
628
629 ui.volume_label_len = ret;
630 break;
631 }
632 case 'c':
633 ret = parse_size(optarg);
634 if (ret < 0)
635 goto out;
636 else if (ret & (ret - 1)) {
637 exfat_err("cluster size(%d) is not a power of 2)\n",
638 ret);
639 goto out;
640 } else if (ret > EXFAT_MAX_CLUSTER_SIZE) {
641 exfat_err("cluster size(%d) exceeds max cluster size(%d)\n",
642 ui.cluster_size, EXFAT_MAX_CLUSTER_SIZE);
643 goto out;
644 }
645 ui.cluster_size = ret;
646 break;
647 case 'b':
648 ret = parse_size(optarg);
649 if (ret < 0)
650 goto out;
651 else if (ret & (ret - 1)) {
652 exfat_err("boundary align(%d) is not a power of 2)\n",
653 ret);
654 goto out;
655 }
656 ui.boundary_align = ret;
657 break;
658 case PACK_BITMAP:
659 ui.pack_bitmap = true;
660 break;
661 case 'f':
662 ui.quick = false;
663 break;
664 case 'V':
665 version_only = true;
666 break;
667 case 'v':
668 print_level = EXFAT_DEBUG;
669 break;
670 case '?':
671 case 'h':
672 default:
673 usage();
674 }
675
676 show_version();
677 if (version_only)
678 exit(EXIT_FAILURE);
679
680 if (argc - optind != 1) {
681 usage();
682 }
683
684 memset(ui.dev_name, 0, sizeof(ui.dev_name));
685 snprintf(ui.dev_name, sizeof(ui.dev_name), "%s", argv[optind]);
686
687 ret = exfat_get_blk_dev_info(&ui, &bd);
688 if (ret < 0)
689 goto out;
690
691 ret = exfat_build_mkfs_info(&bd, &ui);
692 if (ret)
693 goto close;
694
695 ret = exfat_zero_out_disk(&bd, &ui);
696 if (ret)
697 goto close;
698
699 ret = make_exfat(&bd, &ui);
700 if (ret)
701 goto close;
702
703 exfat_info("Synchronizing...\n");
704 ret = fsync(bd.dev_fd);
705 close:
706 close(bd.dev_fd);
707 out:
708 if (!ret)
709 exfat_info("\nexFAT format complete!\n");
710 else
711 exfat_info("\nexFAT format fail!\n");
712 return ret;
713 }
714