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>
22 #include "exfat_ondisk.h"
26 struct exfat_mkfs_info finfo;
28 /* random serial generator based on current time */
29 static unsigned int get_new_serial(void)
33 if (clock_gettime(CLOCK_REALTIME, &ts)) {
34 /* set 0000-0000 on error */
39 return (unsigned int)(ts.tv_nsec << 12 | ts.tv_sec);
42 static void exfat_setup_boot_sector(struct pbr *ppbr,
43 struct exfat_blk_dev *bd, struct exfat_user_input *ui)
45 struct bpb64 *pbpb = &ppbr->bpb;
46 struct bsx64 *pbsx = &ppbr->bsx;
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);
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);
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++;
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);
78 memset(ppbr->boot_code, 0, 390);
79 ppbr->signature = cpu_to_le16(PBR_SIGNATURE);
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);
102 static int exfat_write_boot_sector(struct exfat_blk_dev *bd,
103 struct exfat_user_input *ui, unsigned int *checksum,
107 unsigned int sec_idx = BOOT_SEC_IDX;
111 sec_idx += BACKUP_BOOT_SEC_IDX;
113 ppbr = malloc(bd->sector_size);
115 exfat_err("Cannot allocate pbr: out of memory\n");
118 memset(ppbr, 0, bd->sector_size);
120 exfat_setup_boot_sector(ppbr, bd, ui);
122 /* write main boot sector */
123 ret = exfat_write_sector(bd, ppbr, sec_idx);
125 exfat_err("main boot sector write failed\n");
130 boot_calc_checksum((unsigned char *)ppbr, bd->sector_size,
138 static int exfat_write_extended_boot_sectors(struct exfat_blk_dev *bd,
139 unsigned int *checksum, bool is_backup)
142 __le16 *peb_signature;
145 unsigned int sec_idx = EXBOOT_SEC_IDX;
147 peb = malloc(bd->sector_size);
152 sec_idx += BACKUP_BOOT_SEC_IDX;
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");
164 boot_calc_checksum((unsigned char *) peb, bd->sector_size,
173 static int exfat_write_oem_sector(struct exfat_blk_dev *bd,
174 unsigned int *checksum, bool is_backup)
178 unsigned int sec_idx = OEM_SEC_IDX;
180 oem = malloc(bd->sector_size);
185 sec_idx += BACKUP_BOOT_SEC_IDX;
187 memset(oem, 0xFF, bd->sector_size);
188 ret = exfat_write_sector(bd, oem, sec_idx);
190 exfat_err("oem sector write failed\n");
195 boot_calc_checksum((unsigned char *)oem, bd->sector_size, false,
198 /* Zero out reserved sector */
199 memset(oem, 0, bd->sector_size);
200 ret = exfat_write_sector(bd, oem, sec_idx + 1);
202 exfat_err("reserved sector write failed\n");
207 boot_calc_checksum((unsigned char *)oem, bd->sector_size, false,
215 static int exfat_create_volume_boot_record(struct exfat_blk_dev *bd,
216 struct exfat_user_input *ui, bool is_backup)
218 unsigned int checksum = 0;
221 ret = exfat_write_boot_sector(bd, ui, &checksum, is_backup);
224 ret = exfat_write_extended_boot_sectors(bd, &checksum, is_backup);
227 ret = exfat_write_oem_sector(bd, &checksum, is_backup);
231 return exfat_write_checksum_sector(bd, checksum, is_backup);
234 static int write_fat_entry(int fd, __le32 clu,
235 unsigned long long offset)
238 off_t fat_entry_offset = finfo.fat_byte_off + (offset * sizeof(__le32));
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",
250 static int write_fat_entries(struct exfat_user_input *ui, int fd,
251 unsigned int clu, unsigned int length)
256 count = clu + round_up(length, ui->cluster_size) / ui->cluster_size;
258 for (; clu < count - 1; clu++) {
259 ret = write_fat_entry(fd, cpu_to_le32(clu + 1), clu);
264 ret = write_fat_entry(fd, cpu_to_le32(EXFAT_EOF_CLUSTER), clu);
271 static int exfat_create_fat_table(struct exfat_blk_dev *bd,
272 struct exfat_user_input *ui)
276 /* fat entry 0 should be media type field(0xF8) */
277 ret = write_fat_entry(bd->dev_fd, cpu_to_le32(0xfffffff8), 0);
279 exfat_err("fat 0 entry write failed\n");
283 /* fat entry 1 is historical precedence(0xFFFFFFFF) */
284 ret = write_fat_entry(bd->dev_fd, cpu_to_le32(0xffffffff), 1);
286 exfat_err("fat 1 entry write failed\n");
290 /* write bitmap entries */
291 clu = write_fat_entries(ui, bd->dev_fd, EXFAT_FIRST_CLUSTER,
292 finfo.bitmap_byte_len);
296 /* write upcase table entries */
297 clu = write_fat_entries(ui, bd->dev_fd, clu + 1, finfo.ut_byte_len);
301 /* write root directory entries */
302 clu = write_fat_entries(ui, bd->dev_fd, clu + 1, finfo.root_byte_len);
306 finfo.used_clu_cnt = clu + 1;
307 exfat_debug("Total used cluster count : %d\n", finfo.used_clu_cnt);
312 static int exfat_create_bitmap(struct exfat_blk_dev *bd)
315 unsigned int i, nbytes;
317 bitmap = calloc(round_up(finfo.bitmap_byte_len, sizeof(bitmap_t)),
322 for (i = EXFAT_FIRST_CLUSTER; i < finfo.used_clu_cnt; i++)
323 exfat_bitmap_set(bitmap, i);
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);
337 static int exfat_create_root_dir(struct exfat_blk_dev *bd,
338 struct exfat_user_input *ui)
340 struct exfat_dentry ed[3] = {0};
341 int dentries_len = sizeof(struct exfat_dentry) * 3;
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;
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);
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);
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);
372 static void usage(void)
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",
388 #define PACK_BITMAP (CHAR_MAX + 1)
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, '?' },
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.
409 static int exfat_pack_bitmap(const struct exfat_user_input *ui)
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;
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 -
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;
432 bitmap_clu_len = new_bitmap_clu_len;
436 static int exfat_build_mkfs_info(struct exfat_blk_dev *bd,
437 struct exfat_user_input *ui)
439 unsigned long long total_clu_cnt;
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);
447 if (ui->boundary_align < bd->sector_size) {
448 exfat_err("boundary alignment is too small (min %d)\n",
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);
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");
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");
471 finfo.total_clu_cnt = (unsigned int) total_clu_cnt;
473 finfo.bitmap_byte_off = finfo.clu_byte_off;
474 finfo.bitmap_byte_len = round_up(finfo.total_clu_cnt, 8) / 8;
476 exfat_pack_bitmap(ui);
477 clu_len = round_up(finfo.bitmap_byte_len, ui->cluster_size);
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);
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();
492 static int exfat_zero_out_disk(struct exfat_blk_dev *bd,
493 struct exfat_user_input *ui)
496 unsigned long long total_written = 0;
498 unsigned int chunk_size = ui->cluster_size;
499 unsigned long long size;
502 size = finfo.root_byte_off + chunk_size;
506 buf = malloc(chunk_size);
510 memset(buf, 0, chunk_size);
511 lseek(bd->dev_fd, 0, SEEK_SET);
514 nbytes = write(bd->dev_fd, buf, chunk_size);
517 exfat_err("write failed(errno : %d)\n", errno);
520 total_written += nbytes;
521 } while (total_written < size);
524 exfat_debug("zero out written size : %llu, disk size : %llu\n",
525 total_written, bd->size);
529 static int make_exfat(struct exfat_blk_dev *bd, struct exfat_user_input *ui)
533 exfat_info("Creating exFAT filesystem(%s, cluster size=%u)\n\n",
534 ui->dev_name, ui->cluster_size);
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");
542 exfat_info("Writing backup volume boot record: ");
544 ret = exfat_create_volume_boot_record(bd, ui, 1);
545 exfat_info("%s\n", ret ? "failed" : "done");
549 exfat_info("Fat table creation: ");
550 ret = exfat_create_fat_table(bd, ui);
551 exfat_info("%s\n", ret ? "failed" : "done");
555 exfat_info("Allocation bitmap creation: ");
556 ret = exfat_create_bitmap(bd);
557 exfat_info("%s\n", ret ? "failed" : "done");
561 exfat_info("Upcase table creation: ");
562 ret = exfat_create_upcase_table(bd);
563 exfat_info("%s\n", ret ? "failed" : "done");
567 exfat_info("Writing root directory entry: ");
568 ret = exfat_create_root_dir(bd, ui);
569 exfat_info("%s\n", ret ? "failed" : "done");
576 static long long parse_size(const char *size)
579 unsigned long long byte_size = strtoull(size, &data_unit, 0);
581 switch (*data_unit) {
593 exfat_err("Wrong unit input('%c') for size\n",
601 int main(int argc, char *argv[])
604 int ret = EXIT_FAILURE;
605 struct exfat_blk_dev bd;
606 struct exfat_user_input ui;
607 bool version_only = false;
609 init_user_input(&ui);
611 if (!setlocale(LC_CTYPE, ""))
612 exfat_err("failed to init locale/codeset\n");
615 while ((c = getopt_long(argc, argv, "n:L:c:b:fVvh", opts, NULL)) != EOF)
618 * Make 'n' option fallthrough to 'L' option for for backward
619 * compatibility with old utils.
624 ret = exfat_utf16_enc(optarg,
625 ui.volume_label, sizeof(ui.volume_label));
629 ui.volume_label_len = ret;
633 ret = parse_size(optarg);
636 else if (ret & (ret - 1)) {
637 exfat_err("cluster size(%d) is not a power of 2)\n",
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);
645 ui.cluster_size = ret;
648 ret = parse_size(optarg);
651 else if (ret & (ret - 1)) {
652 exfat_err("boundary align(%d) is not a power of 2)\n",
656 ui.boundary_align = ret;
659 ui.pack_bitmap = true;
668 print_level = EXFAT_DEBUG;
680 if (argc - optind != 1) {
684 memset(ui.dev_name, 0, sizeof(ui.dev_name));
685 snprintf(ui.dev_name, sizeof(ui.dev_name), "%s", argv[optind]);
687 ret = exfat_get_blk_dev_info(&ui, &bd);
691 ret = exfat_build_mkfs_info(&bd, &ui);
695 ret = exfat_zero_out_disk(&bd, &ui);
699 ret = make_exfat(&bd, &ui);
703 exfat_info("Synchronizing...\n");
704 ret = fsync(bd.dev_fd);
709 exfat_info("\nexFAT format complete!\n");
711 exfat_info("\nexFAT format fail!\n");