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 : %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("Volume Serial : 0x%x\n", le32_to_cpu(pbsx->vol_serial));
92 exfat_debug("Sector Size Bits : %u\n",
93 pbsx->sect_size_bits);
94 exfat_debug("Sector per Cluster bits : %u\n",
95 pbsx->sect_per_clus_bits);
98 static int exfat_write_boot_sector(struct exfat_blk_dev *bd,
99 struct exfat_user_input *ui, unsigned int *checksum,
103 unsigned int sec_idx = BOOT_SEC_IDX;
107 sec_idx += BACKUP_BOOT_SEC_IDX;
109 ppbr = malloc(sizeof(struct pbr));
111 exfat_err("Cannot allocate pbr: out of memory\n");
114 memset(ppbr, 0, sizeof(struct pbr));
116 exfat_setup_boot_sector(ppbr, bd, ui);
118 /* write main boot sector */
119 ret = exfat_write_sector(bd, ppbr, sec_idx);
121 exfat_err("main boot sector write failed\n");
126 boot_calc_checksum((unsigned char *)ppbr, sizeof(struct pbr),
134 static int exfat_write_extended_boot_sectors(struct exfat_blk_dev *bd,
135 unsigned int *checksum, bool is_backup)
139 unsigned int sec_idx = EXBOOT_SEC_IDX;
142 sec_idx += BACKUP_BOOT_SEC_IDX;
144 memset(&eb, 0, sizeof(struct exbs));
145 eb.signature = cpu_to_le16(PBR_SIGNATURE);
146 for (i = 0; i < EXBOOT_SEC_NUM; i++) {
147 if (exfat_write_sector(bd, &eb, sec_idx++)) {
148 exfat_err("extended boot sector write failed\n");
152 boot_calc_checksum((unsigned char *) &eb, sizeof(struct exbs),
159 static int exfat_write_oem_sector(struct exfat_blk_dev *bd,
160 unsigned int *checksum, bool is_backup)
164 unsigned int sec_idx = OEM_SEC_IDX;
166 oem = malloc(bd->sector_size);
171 sec_idx += BACKUP_BOOT_SEC_IDX;
173 memset(oem, 0xFF, bd->sector_size);
174 ret = exfat_write_sector(bd, oem, sec_idx);
176 exfat_err("oem sector write failed\n");
181 boot_calc_checksum((unsigned char *)oem, bd->sector_size, false,
184 /* Zero out reserved sector */
185 memset(oem, 0, bd->sector_size);
186 ret = exfat_write_sector(bd, oem, sec_idx + 1);
188 exfat_err("reserved sector write failed\n");
193 boot_calc_checksum((unsigned char *)oem, bd->sector_size, false,
201 static int exfat_create_volume_boot_record(struct exfat_blk_dev *bd,
202 struct exfat_user_input *ui, bool is_backup)
204 unsigned int checksum = 0;
207 ret = exfat_write_boot_sector(bd, ui, &checksum, is_backup);
210 ret = exfat_write_extended_boot_sectors(bd, &checksum, is_backup);
213 ret = exfat_write_oem_sector(bd, &checksum, is_backup);
217 return exfat_write_checksum_sector(bd, checksum, is_backup);
220 static int write_fat_entry(int fd, __le32 clu,
221 unsigned long long offset)
225 lseek(fd, finfo.fat_byte_off + (offset * sizeof(__le32)), SEEK_SET);
226 nbyte = write(fd, (__u8 *) &clu, sizeof(__le32));
227 if (nbyte != sizeof(int)) {
228 exfat_err("write failed, offset : %llu, clu : %x\n",
236 static int write_fat_entries(struct exfat_user_input *ui, int fd,
237 unsigned int clu, unsigned int length)
242 count = clu + round_up(length, ui->cluster_size) / ui->cluster_size;
244 for (; clu < count - 1; clu++) {
245 ret = write_fat_entry(fd, cpu_to_le32(clu + 1), clu);
250 ret = write_fat_entry(fd, cpu_to_le32(EXFAT_EOF_CLUSTER), clu);
257 static int exfat_create_fat_table(struct exfat_blk_dev *bd,
258 struct exfat_user_input *ui)
262 /* fat entry 0 should be media type field(0xF8) */
263 ret = write_fat_entry(bd->dev_fd, cpu_to_le32(0xfffffff8), 0);
265 exfat_err("fat 0 entry write failed\n");
269 /* fat entry 1 is historical precedence(0xFFFFFFFF) */
270 ret = write_fat_entry(bd->dev_fd, cpu_to_le32(0xffffffff), 1);
272 exfat_err("fat 1 entry write failed\n");
276 /* write bitmap entries */
277 clu = write_fat_entries(ui, bd->dev_fd, EXFAT_FIRST_CLUSTER,
278 finfo.bitmap_byte_len);
282 /* write upcase table entries */
283 clu = write_fat_entries(ui, bd->dev_fd, clu + 1, finfo.ut_byte_len);
287 /* write root directory entries */
288 clu = write_fat_entries(ui, bd->dev_fd, clu + 1, finfo.root_byte_len);
292 finfo.used_clu_cnt = clu + 1;
293 exfat_debug("Total used cluster count : %d\n", finfo.used_clu_cnt);
298 static int exfat_create_bitmap(struct exfat_blk_dev *bd)
301 unsigned int i, nbytes;
303 bitmap = calloc(finfo.bitmap_byte_len, sizeof(*bitmap));
307 for (i = 0; i < finfo.used_clu_cnt - EXFAT_FIRST_CLUSTER; i++)
308 exfat_set_bit(bd, bitmap, i);
310 lseek(bd->dev_fd, finfo.bitmap_byte_off, SEEK_SET);
311 nbytes = write(bd->dev_fd, bitmap, finfo.bitmap_byte_len);
312 if (nbytes != finfo.bitmap_byte_len) {
313 exfat_err("write failed, nbytes : %d, bitmap_len : %d\n",
314 nbytes, finfo.bitmap_byte_len);
323 static int exfat_create_root_dir(struct exfat_blk_dev *bd,
324 struct exfat_user_input *ui)
326 struct exfat_dentry ed[3];
327 int dentries_len = sizeof(struct exfat_dentry) * 3;
330 /* Set volume label entry */
331 ed[0].type = EXFAT_VOLUME;
332 memset(ed[0].vol_label, 0, 22);
333 memcpy(ed[0].vol_label, ui->volume_label, ui->volume_label_len);
334 ed[0].vol_char_cnt = ui->volume_label_len/2;
336 /* Set bitmap entry */
337 ed[1].type = EXFAT_BITMAP;
338 ed[1].bitmap_flags = 0;
339 ed[1].bitmap_start_clu = cpu_to_le32(EXFAT_FIRST_CLUSTER);
340 ed[1].bitmap_size = cpu_to_le64(finfo.bitmap_byte_len);
342 /* Set upcase table entry */
343 ed[2].type = EXFAT_UPCASE;
344 ed[2].upcase_checksum = cpu_to_le32(0xe619d30d);
345 ed[2].upcase_start_clu = cpu_to_le32(finfo.ut_start_clu);
346 ed[2].upcase_size = cpu_to_le64(EXFAT_UPCASE_TABLE_SIZE);
348 lseek(bd->dev_fd, finfo.root_byte_off, SEEK_SET);
349 nbytes = write(bd->dev_fd, ed, dentries_len);
350 if (nbytes != dentries_len) {
351 exfat_err("write failed, nbytes : %d, dentries_len : %d\n",
352 nbytes, dentries_len);
359 static void usage(void)
361 fprintf(stderr, "Usage: mkfs.exfat\n");
362 fprintf(stderr, "\t-L | --volume-label=label Set volume label\n");
363 fprintf(stderr, "\t-c | --cluster-size=size(or suffixed by 'K' or 'M') Specify cluster size\n");
364 fprintf(stderr, "\t-b | --boundary-align=size(or suffixed by 'K' or 'M') Specify boundary alignment\n");
365 fprintf(stderr, "\t-f | --full-format Full format\n");
366 fprintf(stderr, "\t-V | --version Show version\n");
367 fprintf(stderr, "\t-v | --verbose Print debug\n");
368 fprintf(stderr, "\t-h | --help Show help\n");
373 static struct option opts[] = {
374 {"volume-label", required_argument, NULL, 'L' },
375 {"cluster-size", required_argument, NULL, 'c' },
376 {"boundary-align", required_argument, NULL, 'b' },
377 {"full-format", no_argument, NULL, 'f' },
378 {"version", no_argument, NULL, 'V' },
379 {"verbose", no_argument, NULL, 'v' },
380 {"help", no_argument, NULL, 'h' },
381 {"?", no_argument, NULL, '?' },
385 static int exfat_build_mkfs_info(struct exfat_blk_dev *bd,
386 struct exfat_user_input *ui)
390 if (ui->boundary_align < bd->sector_size) {
391 exfat_err("boundary alignment is too small (min %d)\n",
395 finfo.fat_byte_off = round_up(24 * bd->sector_size,
397 finfo.fat_byte_len = round_up((bd->num_clusters * sizeof(int)),
399 finfo.clu_byte_off = round_up(finfo.fat_byte_off + finfo.fat_byte_len,
401 if (bd->size <= finfo.clu_byte_off) {
402 exfat_err("boundary alignment is too big\n");
405 finfo.total_clu_cnt = (bd->size - finfo.clu_byte_off) /
407 if (finfo.total_clu_cnt > EXFAT_MAX_NUM_CLUSTER) {
408 exfat_err("cluster size is too small\n");
412 finfo.bitmap_byte_off = finfo.clu_byte_off;
413 finfo.bitmap_byte_len = round_up(finfo.total_clu_cnt, 8) / 8;
414 clu_len = round_up(finfo.bitmap_byte_len, ui->cluster_size);
416 finfo.ut_start_clu = EXFAT_FIRST_CLUSTER + clu_len / ui->cluster_size;
417 finfo.ut_byte_off = finfo.bitmap_byte_off + clu_len;
418 finfo.ut_byte_len = EXFAT_UPCASE_TABLE_SIZE;
419 clu_len = round_up(finfo.ut_byte_len, ui->cluster_size);
421 finfo.root_start_clu = finfo.ut_start_clu + clu_len / ui->cluster_size;
422 finfo.root_byte_off = finfo.ut_byte_off + clu_len;
423 finfo.root_byte_len = sizeof(struct exfat_dentry) * 3;
424 finfo.volume_serial = get_new_serial();
429 static int exfat_zero_out_disk(struct exfat_blk_dev *bd,
430 struct exfat_user_input *ui)
433 unsigned long long total_written = 0;
435 unsigned int chunk_size = ui->cluster_size;
436 unsigned long long size;
439 size = finfo.root_byte_off + chunk_size;
443 buf = malloc(chunk_size);
447 memset(buf, 0, chunk_size);
448 lseek(bd->dev_fd, 0, SEEK_SET);
451 nbytes = write(bd->dev_fd, buf, chunk_size);
454 exfat_err("write failed(errno : %d)\n", errno);
457 total_written += nbytes;
458 } while (total_written < size);
461 exfat_debug("zero out written size : %llu, disk size : %llu\n",
462 total_written, bd->size);
466 static int make_exfat(struct exfat_blk_dev *bd, struct exfat_user_input *ui)
470 exfat_info("Creating exFAT filesystem(%s, cluster size=%u)\n\n",
471 ui->dev_name, ui->cluster_size);
473 exfat_info("Writing volume boot record: ");
474 ret = exfat_create_volume_boot_record(bd, ui, 0);
475 exfat_info("%s\n", ret ? "failed" : "done");
479 exfat_info("Writing backup volume boot record: ");
481 ret = exfat_create_volume_boot_record(bd, ui, 1);
482 exfat_info("%s\n", ret ? "failed" : "done");
486 exfat_info("Fat table creation: ");
487 ret = exfat_create_fat_table(bd, ui);
488 exfat_info("%s\n", ret ? "failed" : "done");
492 exfat_info("Allocation bitmap creation: ");
493 ret = exfat_create_bitmap(bd);
494 exfat_info("%s\n", ret ? "failed" : "done");
498 exfat_info("Upcase table creation: ");
499 ret = exfat_create_upcase_table(bd);
500 exfat_info("%s\n", ret ? "failed" : "done");
504 exfat_info("Writing root directory entry: ");
505 ret = exfat_create_root_dir(bd, ui);
506 exfat_info("%s\n", ret ? "failed" : "done");
513 static long long parse_size(const char *size)
516 unsigned long long byte_size = strtoull(size, &data_unit, 0);
518 switch (*data_unit) {
530 exfat_err("Wrong unit input('%c') for size\n",
538 int main(int argc, char *argv[])
541 int ret = EXIT_FAILURE;
542 struct exfat_blk_dev bd;
543 struct exfat_user_input ui;
544 bool version_only = false;
546 init_user_input(&ui);
548 if (!setlocale(LC_CTYPE, ""))
549 exfat_err("failed to init locale/codeset\n");
552 while ((c = getopt_long(argc, argv, "n:L:c:b:fVvh", opts, NULL)) != EOF)
555 * Make 'n' option fallthrough to 'L' option for for backward
556 * compatibility with old utils.
561 ret = exfat_utf16_enc(optarg,
562 ui.volume_label, sizeof(ui.volume_label));
566 ui.volume_label_len = ret;
570 ret = parse_size(optarg);
573 else if (ret & (ret - 1)) {
574 exfat_err("cluster size(%d) is not a power of 2)\n",
577 } else if (ret > EXFAT_MAX_CLUSTER_SIZE) {
578 exfat_err("cluster size(%d) exceeds max cluster size(%d)\n",
579 ui.cluster_size, EXFAT_MAX_CLUSTER_SIZE);
582 ui.cluster_size = ret;
585 ret = parse_size(optarg);
588 else if (ret & (ret - 1)) {
589 exfat_err("boundary align(%d) is not a power of 2)\n",
593 ui.boundary_align = ret;
602 print_level = EXFAT_DEBUG;
614 if (argc - optind != 1) {
618 memset(ui.dev_name, 0, sizeof(ui.dev_name));
619 snprintf(ui.dev_name, sizeof(ui.dev_name), "%s", argv[optind]);
621 ret = exfat_get_blk_dev_info(&ui, &bd);
625 ret = exfat_build_mkfs_info(&bd, &ui);
629 ret = exfat_zero_out_disk(&bd, &ui);
633 ret = make_exfat(&bd, &ui);
637 exfat_info("Synchronizing...\n");
638 ret = fsync(bd.dev_fd);
643 exfat_info("\nexFAT format complete!\n");
645 exfat_info("\nexFAT format fail!\n");