1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2019 Namjae Jeon <linkinjeon@kernel.org>
11 #include <sys/types.h>
13 #include <sys/ioctl.h>
21 #include "exfat_ondisk.h"
25 struct exfat_mkfs_info finfo;
27 /* random serial generator based on current time */
28 static unsigned int get_new_serial(void)
32 if (clock_gettime(CLOCK_REALTIME, &ts)) {
33 /* set 0000-0000 on error */
38 return (unsigned int)(ts.tv_nsec << 12 | ts.tv_sec);
41 static void exfat_setup_boot_sector(struct pbr *ppbr,
42 struct exfat_blk_dev *bd, struct exfat_user_input *ui)
44 struct bpb64 *pbpb = &ppbr->bpb;
45 struct bsx64 *pbsx = &ppbr->bsx;
48 /* Fill exfat BIOS paramemter block */
49 pbpb->jmp_boot[0] = 0xeb;
50 pbpb->jmp_boot[1] = 0x76;
51 pbpb->jmp_boot[2] = 0x90;
52 memcpy(pbpb->oem_name, "EXFAT ", 8);
53 memset(pbpb->res_zero, 0, 53);
55 /* Fill exfat extend BIOS paramemter block */
57 pbsx->vol_length = cpu_to_le64(bd->size / bd->sector_size);
58 pbsx->fat_offset = cpu_to_le32(finfo.fat_byte_off / bd->sector_size);
59 pbsx->fat_length = cpu_to_le32(finfo.fat_byte_len / bd->sector_size);
60 pbsx->clu_offset = cpu_to_le32(finfo.clu_byte_off / bd->sector_size);
61 pbsx->clu_count = cpu_to_le32(finfo.total_clu_cnt);
62 pbsx->root_cluster = cpu_to_le32(finfo.root_start_clu);
63 pbsx->vol_serial = cpu_to_le32(finfo.volume_serial);
65 pbsx->sect_size_bits = bd->sector_size_bits;
66 pbsx->sect_per_clus_bits = 0;
67 /* Compute base 2 logarithm of ui->cluster_size / bd->sector_size */
68 for (i = ui->cluster_size / bd->sector_size; i > 1; i /= 2)
69 pbsx->sect_per_clus_bits++;
71 /* fs_version[0] : minor and fs_version[1] : major */
72 pbsx->fs_version[0] = 0;
73 pbsx->fs_version[1] = 1;
74 memset(pbsx->reserved2, 0, 7);
76 memset(ppbr->boot_code, 0, 390);
77 ppbr->signature = cpu_to_le16(PBR_SIGNATURE);
79 exfat_debug("Volume Length(sectors) : %" PRIu64 "\n",
80 le64_to_cpu(pbsx->vol_length));
81 exfat_debug("FAT Offset(sector offset) : %u\n",
82 le32_to_cpu(pbsx->fat_offset));
83 exfat_debug("FAT Length(sectors) : %u\n",
84 le32_to_cpu(pbsx->fat_length));
85 exfat_debug("Cluster Heap Offset (sector offset) : %u\n",
86 le32_to_cpu(pbsx->clu_offset));
87 exfat_debug("Cluster Count (sectors) : %u\n",
88 le32_to_cpu(pbsx->clu_count));
89 exfat_debug("Root Cluster (cluster offset) : %u\n",
90 le32_to_cpu(pbsx->root_cluster));
91 exfat_debug("Sector Size Bits : %u\n",
92 pbsx->sect_size_bits);
93 exfat_debug("Sector per Cluster bits : %u\n",
94 pbsx->sect_per_clus_bits);
97 static int exfat_write_sector(struct exfat_blk_dev *bd, void *buf,
101 unsigned long long offset = sec_off * bd->sector_size;
103 lseek(bd->dev_fd, offset, SEEK_SET);
104 bytes = write(bd->dev_fd, buf, bd->sector_size);
105 if (bytes != (int)bd->sector_size) {
106 exfat_err("write failed, sec_off : %u, bytes : %d\n", sec_off,
113 static int exfat_write_boot_sector(struct exfat_blk_dev *bd,
114 struct exfat_user_input *ui, unsigned int *checksum,
118 unsigned int sec_idx = BOOT_SEC_IDX;
122 sec_idx += BACKUP_BOOT_SEC_IDX;
124 ppbr = malloc(sizeof(struct pbr));
126 exfat_err("Cannot allocate pbr: out of memory\n");
129 memset(ppbr, 0, sizeof(struct pbr));
131 exfat_setup_boot_sector(ppbr, bd, ui);
133 /* write main boot sector */
134 ret = exfat_write_sector(bd, ppbr, sec_idx);
136 exfat_err("main boot sector write failed\n");
141 boot_calc_checksum((unsigned char *)ppbr, sizeof(struct pbr),
149 static int exfat_write_extended_boot_sectors(struct exfat_blk_dev *bd,
150 unsigned int *checksum, bool is_backup)
154 unsigned int sec_idx = EXBOOT_SEC_IDX;
157 sec_idx += BACKUP_BOOT_SEC_IDX;
159 memset(&eb, 0, sizeof(struct exbs));
160 eb.signature = cpu_to_le16(PBR_SIGNATURE);
161 for (i = 0; i < EXBOOT_SEC_NUM; i++) {
162 if (exfat_write_sector(bd, &eb, sec_idx++)) {
163 exfat_err("extended boot sector write failed\n");
167 boot_calc_checksum((unsigned char *) &eb, sizeof(struct exbs),
174 static int exfat_write_oem_sector(struct exfat_blk_dev *bd,
175 unsigned int *checksum, bool is_backup)
179 unsigned int sec_idx = OEM_SEC_IDX;
181 oem = malloc(bd->sector_size);
186 sec_idx += BACKUP_BOOT_SEC_IDX;
188 memset(oem, 0xFF, bd->sector_size);
189 ret = exfat_write_sector(bd, oem, sec_idx);
191 exfat_err("oem sector write failed\n");
196 boot_calc_checksum((unsigned char *)oem, bd->sector_size, false,
199 /* Zero out reserved sector */
200 memset(oem, 0, bd->sector_size);
201 ret = exfat_write_sector(bd, oem, sec_idx + 1);
203 exfat_err("reserved sector write failed\n");
208 boot_calc_checksum((unsigned char *)oem, bd->sector_size, false,
216 static int exfat_write_checksum_sector(struct exfat_blk_dev *bd,
217 unsigned int checksum, bool is_backup)
219 __le32 *checksum_buf;
222 unsigned int sec_idx = CHECKSUM_SEC_IDX;
224 checksum_buf = malloc(bd->sector_size);
229 sec_idx += BACKUP_BOOT_SEC_IDX;
231 for (i = 0; i < bd->sector_size / sizeof(int); i++)
232 checksum_buf[i] = cpu_to_le32(checksum);
234 ret = exfat_write_sector(bd, checksum_buf, sec_idx);
236 exfat_err("checksum sector write failed\n");
245 static int exfat_create_volume_boot_record(struct exfat_blk_dev *bd,
246 struct exfat_user_input *ui, bool is_backup)
248 unsigned int checksum = 0;
251 ret = exfat_write_boot_sector(bd, ui, &checksum, is_backup);
254 ret = exfat_write_extended_boot_sectors(bd, &checksum, is_backup);
257 ret = exfat_write_oem_sector(bd, &checksum, is_backup);
261 return exfat_write_checksum_sector(bd, checksum, is_backup);
264 static int write_fat_entry(int fd, __le32 clu,
265 unsigned long long offset)
269 lseek(fd, finfo.fat_byte_off + (offset * sizeof(__le32)), SEEK_SET);
270 nbyte = write(fd, (__u8 *) &clu, sizeof(__le32));
271 if (nbyte != sizeof(int)) {
272 exfat_err("write failed, offset : %llu, clu : %x\n",
280 static int write_fat_entries(struct exfat_user_input *ui, int fd,
281 unsigned int clu, unsigned int length)
286 count = clu + round_up(length, ui->cluster_size) / ui->cluster_size;
288 for (; clu < count - 1; clu++) {
289 ret = write_fat_entry(fd, cpu_to_le32(clu + 1), clu);
294 ret = write_fat_entry(fd, cpu_to_le32(EXFAT_EOF_CLUSTER), clu);
301 static int exfat_create_fat_table(struct exfat_blk_dev *bd,
302 struct exfat_user_input *ui)
306 /* fat entry 0 should be media type field(0xF8) */
307 ret = write_fat_entry(bd->dev_fd, cpu_to_le32(0xfffffff8), 0);
309 exfat_err("fat 0 entry write failed\n");
313 /* fat entry 1 is historical precedence(0xFFFFFFFF) */
314 ret = write_fat_entry(bd->dev_fd, cpu_to_le32(0xffffffff), 1);
316 exfat_err("fat 1 entry write failed\n");
320 /* write bitmap entries */
321 clu = write_fat_entries(ui, bd->dev_fd, EXFAT_FIRST_CLUSTER,
322 finfo.bitmap_byte_len);
326 /* write upcase table entries */
327 clu = write_fat_entries(ui, bd->dev_fd, clu + 1, finfo.ut_byte_len);
331 /* write root directory entries */
332 clu = write_fat_entries(ui, bd->dev_fd, clu + 1, finfo.root_byte_len);
336 finfo.used_clu_cnt = clu + 1;
337 exfat_debug("Total used cluster count : %d\n", finfo.used_clu_cnt);
342 static int exfat_create_bitmap(struct exfat_blk_dev *bd)
345 unsigned int i, nbytes;
347 bitmap = calloc(finfo.bitmap_byte_len, sizeof(*bitmap));
351 for (i = 0; i < finfo.used_clu_cnt - EXFAT_FIRST_CLUSTER; i++)
352 exfat_set_bit(bd, bitmap, i);
354 lseek(bd->dev_fd, finfo.bitmap_byte_off, SEEK_SET);
355 nbytes = write(bd->dev_fd, bitmap, finfo.bitmap_byte_len);
356 if (nbytes != finfo.bitmap_byte_len) {
357 exfat_err("write failed, nbytes : %d, bitmap_len : %d\n",
358 nbytes, finfo.bitmap_byte_len);
367 static int exfat_create_root_dir(struct exfat_blk_dev *bd,
368 struct exfat_user_input *ui)
370 struct exfat_dentry ed[3];
371 int dentries_len = sizeof(struct exfat_dentry) * 3;
374 /* Set volume label entry */
375 ed[0].type = EXFAT_VOLUME;
376 memset(ed[0].vol_label, 0, 22);
377 memcpy(ed[0].vol_label, ui->volume_label, ui->volume_label_len);
378 ed[0].vol_char_cnt = ui->volume_label_len/2;
380 /* Set bitmap entry */
381 ed[1].type = EXFAT_BITMAP;
382 ed[1].bitmap_flags = 0;
383 ed[1].bitmap_start_clu = cpu_to_le32(EXFAT_FIRST_CLUSTER);
384 ed[1].bitmap_size = cpu_to_le64(finfo.bitmap_byte_len);
386 /* Set upcase table entry */
387 ed[2].type = EXFAT_UPCASE;
388 ed[2].upcase_checksum = cpu_to_le32(0xe619d30d);
389 ed[2].upcase_start_clu = cpu_to_le32(finfo.ut_start_clu);
390 ed[2].upcase_size = cpu_to_le64(EXFAT_UPCASE_TABLE_SIZE);
392 lseek(bd->dev_fd, finfo.root_byte_off, SEEK_SET);
393 nbytes = write(bd->dev_fd, ed, dentries_len);
394 if (nbytes != dentries_len) {
395 exfat_err("write failed, nbytes : %d, dentries_len : %d\n",
396 nbytes, dentries_len);
403 static void usage(void)
405 fprintf(stderr, "Usage: mkfs.exfat\n");
406 fprintf(stderr, "\t-L | --volume-label=label Set volume label\n");
407 fprintf(stderr, "\t-c | --cluster-size=size(or suffixed by 'K' or 'M') Specify cluster size\n");
408 fprintf(stderr, "\t-b | --boundary-align=size(or suffixed by 'K' or 'M') Specify boundary alignment\n");
409 fprintf(stderr, "\t-f | --full-format Full format\n");
410 fprintf(stderr, "\t-V | --version Show version\n");
411 fprintf(stderr, "\t-v | --verbose Print debug\n");
412 fprintf(stderr, "\t-h | --help Show help\n");
417 static struct option opts[] = {
418 {"volume-label", required_argument, NULL, 'L' },
419 {"cluster-size", required_argument, NULL, 'c' },
420 {"boundary-align", required_argument, NULL, 'b' },
421 {"full-format", no_argument, NULL, 'f' },
422 {"version", no_argument, NULL, 'V' },
423 {"verbose", no_argument, NULL, 'v' },
424 {"help", no_argument, NULL, 'h' },
425 {"?", no_argument, NULL, '?' },
429 static int exfat_build_mkfs_info(struct exfat_blk_dev *bd,
430 struct exfat_user_input *ui)
434 if (ui->boundary_align < bd->sector_size) {
435 exfat_err("boundary alignment is too small (min %d)\n",
439 finfo.fat_byte_off = round_up(24 * bd->sector_size,
441 finfo.fat_byte_len = round_up((bd->num_clusters * sizeof(int)),
443 finfo.clu_byte_off = round_up(finfo.fat_byte_off + finfo.fat_byte_len,
445 if (bd->size <= finfo.clu_byte_off) {
446 exfat_err("boundary alignment is too big\n");
449 finfo.total_clu_cnt = (bd->size - finfo.clu_byte_off) /
451 if (finfo.total_clu_cnt > EXFAT_MAX_NUM_CLUSTER) {
452 exfat_err("cluster size is too small\n");
456 finfo.bitmap_byte_off = finfo.clu_byte_off;
457 finfo.bitmap_byte_len = round_up(finfo.total_clu_cnt, 8) / 8;
458 clu_len = round_up(finfo.bitmap_byte_len, ui->cluster_size);
460 finfo.ut_start_clu = EXFAT_FIRST_CLUSTER + clu_len / ui->cluster_size;
461 finfo.ut_byte_off = finfo.bitmap_byte_off + clu_len;
462 finfo.ut_byte_len = EXFAT_UPCASE_TABLE_SIZE;
463 clu_len = round_up(finfo.ut_byte_len, ui->cluster_size);
465 finfo.root_start_clu = finfo.ut_start_clu + clu_len / ui->cluster_size;
466 finfo.root_byte_off = finfo.ut_byte_off + clu_len;
467 finfo.root_byte_len = sizeof(struct exfat_dentry) * 3;
468 finfo.volume_serial = get_new_serial();
473 static int exfat_zero_out_disk(struct exfat_blk_dev *bd,
474 struct exfat_user_input *ui)
477 unsigned long long total_written = 0;
479 unsigned int chunk_size = ui->cluster_size;
480 unsigned long long size;
483 size = finfo.root_byte_off + chunk_size;
487 buf = malloc(chunk_size);
491 memset(buf, 0, chunk_size);
492 lseek(bd->dev_fd, 0, SEEK_SET);
495 nbytes = write(bd->dev_fd, buf, chunk_size);
498 exfat_err("write failed(errno : %d)\n", errno);
501 total_written += nbytes;
502 } while (total_written < size);
505 exfat_debug("zero out written size : %llu, disk size : %llu\n",
506 total_written, bd->size);
510 static int make_exfat(struct exfat_blk_dev *bd, struct exfat_user_input *ui)
514 exfat_info("Creating exFAT filesystem(%s, cluster size=%u)\n\n",
515 ui->dev_name, ui->cluster_size);
517 exfat_info("Writing volume boot record: ");
518 ret = exfat_create_volume_boot_record(bd, ui, 0);
519 exfat_info("%s\n", ret ? "failed" : "done");
523 exfat_info("Writing backup volume boot record: ");
525 ret = exfat_create_volume_boot_record(bd, ui, 1);
526 exfat_info("%s\n", ret ? "failed" : "done");
530 exfat_info("Fat table creation: ");
531 ret = exfat_create_fat_table(bd, ui);
532 exfat_info("%s\n", ret ? "failed" : "done");
536 exfat_info("Allocation bitmap creation: ");
537 ret = exfat_create_bitmap(bd);
538 exfat_info("%s\n", ret ? "failed" : "done");
542 exfat_info("Upcase table creation: ");
543 ret = exfat_create_upcase_table(bd);
544 exfat_info("%s\n", ret ? "failed" : "done");
548 exfat_info("Writing root directory entry: ");
549 ret = exfat_create_root_dir(bd, ui);
550 exfat_info("%s\n", ret ? "failed" : "done");
557 static long long parse_size(const char *size)
560 unsigned long long byte_size = strtoull(size, &data_unit, 0);
562 switch (*data_unit) {
574 exfat_err("Wrong unit input('%c') for size\n",
582 int main(int argc, char *argv[])
585 int ret = EXIT_FAILURE;
586 struct exfat_blk_dev bd;
587 struct exfat_user_input ui;
588 bool version_only = false;
590 init_user_input(&ui);
592 if (!setlocale(LC_CTYPE, ""))
593 exfat_err("failed to init locale/codeset\n");
596 while ((c = getopt_long(argc, argv, "n:L:c:b:fVvh", opts, NULL)) != EOF)
599 * Make 'n' option fallthrough to 'L' option for for backward
600 * compatibility with old utils.
605 ret = exfat_utf16_enc(optarg,
606 ui.volume_label, sizeof(ui.volume_label));
610 ui.volume_label_len = ret;
614 ret = parse_size(optarg);
617 else if (ret & (ret - 1)) {
618 exfat_err("cluster size(%d) is not a power of 2)\n",
621 } else if (ret > EXFAT_MAX_CLUSTER_SIZE) {
622 exfat_err("cluster size(%d) exceeds max cluster size(%d)\n",
623 ui.cluster_size, EXFAT_MAX_CLUSTER_SIZE);
626 ui.cluster_size = ret;
629 ret = parse_size(optarg);
632 else if (ret & (ret - 1)) {
633 exfat_err("boundary align(%d) is not a power of 2)\n",
637 ui.boundary_align = ret;
646 print_level = EXFAT_DEBUG;
658 if (argc - optind != 1) {
662 memset(ui.dev_name, 0, sizeof(ui.dev_name));
663 snprintf(ui.dev_name, sizeof(ui.dev_name), "%s", argv[optind]);
665 ret = exfat_get_blk_dev_info(&ui, &bd);
669 ret = exfat_build_mkfs_info(&bd, &ui);
673 ret = exfat_zero_out_disk(&bd, &ui);
677 ret = make_exfat(&bd, &ui);
681 exfat_info("Synchronizing...\n");
682 ret = fsync(bd.dev_fd);
687 exfat_info("\nexFAT format complete!\n");
689 exfat_info("\nexFAT format fail!\n");