1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2019 Namjae Jeon <linkinjeon@kernel.org>
9 #include <sys/sysmacros.h>
19 #include "exfat_ondisk.h"
23 #include "exfat_dir.h"
25 unsigned int print_level = EXFAT_INFO;
27 void exfat_bitmap_set_range(struct exfat *exfat, char *bitmap,
28 clus_t start_clus, clus_t count)
32 if (!exfat_heap_clus(exfat, start_clus) ||
33 !exfat_heap_clus(exfat, start_clus + count - 1))
37 while (clus < start_clus + count) {
38 exfat_bitmap_set(bitmap, clus);
43 static int exfat_bitmap_find_bit(struct exfat *exfat, char *bmap,
44 clus_t start_clu, clus_t *next,
49 last_clu = le32_to_cpu(exfat->bs->bsx.clu_count) +
51 while (start_clu < last_clu) {
52 if (!!exfat_bitmap_get(bmap, start_clu) == bit) {
61 int exfat_bitmap_find_zero(struct exfat *exfat, char *bmap,
62 clus_t start_clu, clus_t *next)
64 return exfat_bitmap_find_bit(exfat, bmap,
68 int exfat_bitmap_find_one(struct exfat *exfat, char *bmap,
69 clus_t start_clu, clus_t *next)
71 return exfat_bitmap_find_bit(exfat, bmap,
75 wchar_t exfat_bad_char(wchar_t w)
78 || (w == '*') || (w == '?') || (w == '<') || (w == '>')
79 || (w == '|') || (w == '"') || (w == ':') || (w == '/')
83 void boot_calc_checksum(unsigned char *sector, unsigned short size,
84 bool is_boot_sec, __le32 *checksum)
89 for (index = 0; index < size; index++) {
90 if ((index == 106) || (index == 107) || (index == 112))
92 *checksum = ((*checksum & 1) ? 0x80000000 : 0) +
93 (*checksum >> 1) + sector[index];
96 for (index = 0; index < size; index++) {
97 *checksum = ((*checksum & 1) ? 0x80000000 : 0) +
98 (*checksum >> 1) + sector[index];
103 void show_version(void)
105 printf("exfatprogs version : %s\n", EXFAT_PROGS_VERSION);
108 static inline unsigned int sector_size_bits(unsigned int size)
110 unsigned int bits = 8;
115 } while (size > 256);
120 static void exfat_set_default_cluster_size(struct exfat_blk_dev *bd,
121 struct exfat_user_input *ui)
123 if (256 * MB >= bd->size)
124 ui->cluster_size = 4 * KB;
125 else if (32 * GB >= bd->size)
126 ui->cluster_size = 32 * KB;
128 ui->cluster_size = 128 * KB;
131 void init_user_input(struct exfat_user_input *ui)
133 memset(ui, 0, sizeof(struct exfat_user_input));
134 ui->writeable = true;
138 int exfat_get_blk_dev_info(struct exfat_user_input *ui,
139 struct exfat_blk_dev *bd)
144 unsigned long long blk_dev_offset = 0;
146 fd = open(ui->dev_name, ui->writeable ? O_RDWR|O_EXCL : O_RDONLY);
148 exfat_err("open failed : %s, %s\n", ui->dev_name,
152 blk_dev_size = lseek(fd, 0, SEEK_END);
153 if (blk_dev_size <= 0) {
154 exfat_err("invalid block device size(%s)\n",
161 if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode)) {
162 char pathname[sizeof("/sys/dev/block/4294967295:4294967295/start")];
165 snprintf(pathname, sizeof(pathname), "/sys/dev/block/%u:%u/start",
166 major(st.st_rdev), minor(st.st_rdev));
167 fp = fopen(pathname, "r");
169 if (fscanf(fp, "%llu", &blk_dev_offset) == 1) {
171 * Linux kernel always reports partition offset
172 * in 512-byte units, regardless of sector size
174 blk_dev_offset <<= 9;
181 bd->offset = blk_dev_offset;
182 bd->size = blk_dev_size;
183 if (!ui->cluster_size)
184 exfat_set_default_cluster_size(bd, ui);
186 if (!ui->boundary_align)
187 ui->boundary_align = DEFAULT_BOUNDARY_ALIGNMENT;
189 if (ioctl(fd, BLKSSZGET, &bd->sector_size) < 0)
190 bd->sector_size = DEFAULT_SECTOR_SIZE;
191 bd->sector_size_bits = sector_size_bits(bd->sector_size);
192 bd->num_sectors = blk_dev_size / bd->sector_size;
193 bd->num_clusters = blk_dev_size / ui->cluster_size;
195 exfat_debug("Block device name : %s\n", ui->dev_name);
196 exfat_debug("Block device offset : %llu\n", bd->offset);
197 exfat_debug("Block device size : %llu\n", bd->size);
198 exfat_debug("Block sector size : %u\n", bd->sector_size);
199 exfat_debug("Number of the sectors : %llu\n",
201 exfat_debug("Number of the clusters : %u\n",
210 ssize_t exfat_read(int fd, void *buf, size_t size, off_t offset)
212 return pread(fd, buf, size, offset);
215 ssize_t exfat_write(int fd, void *buf, size_t size, off_t offset)
217 return pwrite(fd, buf, size, offset);
220 size_t exfat_utf16_len(const __le16 *str, size_t max_size)
224 while (le16_to_cpu(str[i]) && i < max_size)
229 ssize_t exfat_utf16_enc(const char *in_str, __u16 *out_str, size_t out_size)
231 size_t mbs_len, out_len, i;
234 mbs_len = mbstowcs(NULL, in_str, 0);
235 if (mbs_len == (size_t)-1) {
236 if (errno == EINVAL || errno == EILSEQ)
237 exfat_err("invalid character sequence in current locale\n");
241 wcs = calloc(mbs_len+1, sizeof(wchar_t));
245 /* First convert multibyte char* string to wchar_t* string */
246 if (mbstowcs(wcs, in_str, mbs_len+1) == (size_t)-1) {
247 if (errno == EINVAL || errno == EILSEQ)
248 exfat_err("invalid character sequence in current locale\n");
253 /* Convert wchar_t* string (sequence of code points) to UTF-16 string */
254 for (i = 0, out_len = 0; i < mbs_len; i++) {
255 if (2*(out_len+1) > out_size ||
256 (wcs[i] >= 0x10000 && 2*(out_len+2) > out_size)) {
257 exfat_err("input string is too long\n");
262 /* Encode code point above Plane0 as UTF-16 surrogate pair */
263 if (wcs[i] >= 0x10000) {
265 cpu_to_le16(((wcs[i] - 0x10000) >> 10) + 0xD800);
266 wcs[i] = ((wcs[i] - 0x10000) & 0x3FF) + 0xDC00;
269 out_str[out_len++] = cpu_to_le16(wcs[i]);
276 ssize_t exfat_utf16_dec(const __u16 *in_str, size_t in_len,
277 char *out_str, size_t out_size)
279 size_t wcs_len, out_len, c_len, i;
280 char c_str[MB_LEN_MAX];
285 wcs = calloc(in_len/2+1, sizeof(wchar_t));
289 /* First convert UTF-16 string to wchar_t* string */
290 for (i = 0, wcs_len = 0; i < in_len/2; i++, wcs_len++) {
291 wcs[wcs_len] = le16_to_cpu(in_str[i]);
293 * If wchar_t can store code point above Plane0
294 * then unpack UTF-16 surrogate pair to code point
296 #if WCHAR_MAX >= 0x10FFFF
297 if (wcs[wcs_len] >= 0xD800 && wcs[wcs_len] <= 0xDBFF &&
299 w = le16_to_cpu(in_str[i+1]);
300 if (w >= 0xDC00 && w <= 0xDFFF) {
301 wcs[wcs_len] = 0x10000 +
302 ((wcs[wcs_len] - 0xD800) << 10) +
310 memset(&ps, 0, sizeof(ps));
312 /* And then convert wchar_t* string to multibyte char* string */
313 for (i = 0, out_len = 0, c_len = 0; i <= wcs_len; i++) {
314 c_len = wcrtomb(c_str, wcs[i], &ps);
316 * If character is non-representable in current locale then
317 * try to store it as Unicode replacement code point U+FFFD
319 if (c_len == (size_t)-1 && errno == EILSEQ)
320 c_len = wcrtomb(c_str, 0xFFFD, &ps);
321 /* If U+FFFD is also non-representable, try question mark */
322 if (c_len == (size_t)-1 && errno == EILSEQ)
323 c_len = wcrtomb(c_str, L'?', &ps);
324 /* If also (7bit) question mark fails then we cannot do more */
325 if (c_len == (size_t)-1) {
326 exfat_err("invalid UTF-16 sequence\n");
330 if (out_len+c_len > out_size) {
331 exfat_err("input string is too long\n");
335 memcpy(out_str+out_len, c_str, c_len);
341 /* Last iteration of above loop should have produced null byte */
342 if (c_len == 0 || out_str[out_len-1] != 0) {
343 exfat_err("invalid UTF-16 sequence\n");
350 off_t exfat_get_root_entry_offset(struct exfat_blk_dev *bd)
354 unsigned int cluster_size, sector_size;
357 bs = (struct pbr *)malloc(EXFAT_MAX_SECTOR_SIZE);
359 exfat_err("failed to allocate memory\n");
363 nbytes = exfat_read(bd->dev_fd, bs, EXFAT_MAX_SECTOR_SIZE, 0);
364 if (nbytes != EXFAT_MAX_SECTOR_SIZE) {
365 exfat_err("boot sector read failed: %d\n", errno);
370 if (memcmp(bs->bpb.oem_name, "EXFAT ", 8) != 0) {
371 exfat_err("Bad fs_name in boot sector, which does not describe a valid exfat filesystem\n");
376 sector_size = 1 << bs->bsx.sect_size_bits;
377 cluster_size = (1 << bs->bsx.sect_per_clus_bits) * sector_size;
378 root_clu_off = le32_to_cpu(bs->bsx.clu_offset) * sector_size +
379 (le32_to_cpu(bs->bsx.root_cluster) - EXFAT_RESERVED_CLUSTERS) *
386 char *exfat_conv_volume_label(struct exfat_dentry *vol_entry)
389 __le16 disk_label[VOLUME_LABEL_MAX_LEN];
391 volume_label = malloc(VOLUME_LABEL_BUFFER_SIZE);
395 memcpy(disk_label, vol_entry->vol_label, sizeof(disk_label));
396 memset(volume_label, 0, VOLUME_LABEL_BUFFER_SIZE);
397 if (exfat_utf16_dec(disk_label, vol_entry->vol_char_cnt*2,
398 volume_label, VOLUME_LABEL_BUFFER_SIZE) < 0) {
399 exfat_err("failed to decode volume label\n");
407 int exfat_read_volume_label(struct exfat *exfat)
409 struct exfat_dentry *dentry;
411 __le16 disk_label[VOLUME_LABEL_MAX_LEN];
412 struct exfat_lookup_filter filter = {
413 .in.type = EXFAT_VOLUME,
417 err = exfat_lookup_dentry_set(exfat, exfat->root, &filter);
421 dentry = filter.out.dentry_set;
423 if (dentry->vol_char_cnt == 0)
426 if (dentry->vol_char_cnt > VOLUME_LABEL_MAX_LEN) {
427 exfat_err("too long label. %d\n", dentry->vol_char_cnt);
432 memcpy(disk_label, dentry->vol_label, sizeof(disk_label));
433 if (exfat_utf16_dec(disk_label, dentry->vol_char_cnt*2,
434 exfat->volume_label, sizeof(exfat->volume_label)) < 0) {
435 exfat_err("failed to decode volume label\n");
440 exfat_info("label: %s\n", exfat->volume_label);
442 free(filter.out.dentry_set);
446 int exfat_set_volume_label(struct exfat *exfat, char *label_input)
448 struct exfat_dentry *pvol;
449 struct exfat_dentry_loc loc;
450 __u16 volume_label[VOLUME_LABEL_MAX_LEN];
451 int volume_label_len, dcount, err;
453 struct exfat_lookup_filter filter = {
454 .in.type = EXFAT_VOLUME,
458 err = exfat_lookup_dentry_set(exfat, exfat->root, &filter);
460 pvol = filter.out.dentry_set;
461 dcount = filter.out.dentry_count;
462 memset(pvol->vol_label, 0, sizeof(pvol->vol_label));
464 pvol = calloc(sizeof(struct exfat_dentry), 1);
469 pvol->type = EXFAT_VOLUME;
472 volume_label_len = exfat_utf16_enc(label_input,
473 volume_label, sizeof(volume_label));
474 if (volume_label_len < 0) {
475 exfat_err("failed to encode volume label\n");
480 memcpy(pvol->vol_label, volume_label, volume_label_len);
481 pvol->vol_char_cnt = volume_label_len/2;
483 loc.parent = exfat->root;
484 loc.file_offset = filter.out.file_offset;
485 loc.dev_offset = filter.out.dev_offset;
486 err = exfat_add_dentry_set(exfat, &loc, pvol, dcount, false);
487 exfat_info("new label: %s\n", label_input);
494 int exfat_read_sector(struct exfat_blk_dev *bd, void *buf, unsigned int sec_off)
497 unsigned long long offset =
498 (unsigned long long)sec_off * bd->sector_size;
500 ret = pread(bd->dev_fd, buf, bd->sector_size, offset);
502 exfat_err("read failed, sec_off : %u\n", sec_off);
508 int exfat_write_sector(struct exfat_blk_dev *bd, void *buf,
509 unsigned int sec_off)
512 unsigned long long offset =
513 (unsigned long long)sec_off * bd->sector_size;
515 bytes = pwrite(bd->dev_fd, buf, bd->sector_size, offset);
516 if (bytes != (int)bd->sector_size) {
517 exfat_err("write failed, sec_off : %u, bytes : %d\n", sec_off,
524 int exfat_write_checksum_sector(struct exfat_blk_dev *bd,
525 unsigned int checksum, bool is_backup)
527 __le32 *checksum_buf;
530 unsigned int sec_idx = CHECKSUM_SEC_IDX;
532 checksum_buf = malloc(bd->sector_size);
537 sec_idx += BACKUP_BOOT_SEC_IDX;
539 for (i = 0; i < bd->sector_size / sizeof(int); i++)
540 checksum_buf[i] = cpu_to_le32(checksum);
542 ret = exfat_write_sector(bd, checksum_buf, sec_idx);
544 exfat_err("checksum sector write failed\n");
553 int exfat_show_volume_serial(int fd)
558 ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
560 exfat_err("Cannot allocate pbr: out of memory\n");
564 /* read main boot sector */
565 ret = exfat_read(fd, (char *)ppbr, EXFAT_MAX_SECTOR_SIZE, 0);
567 exfat_err("main boot sector read failed\n");
572 if (memcmp(ppbr->bpb.oem_name, "EXFAT ", 8) != 0) {
573 exfat_err("Bad fs_name in boot sector, which does not describe a valid exfat filesystem\n");
578 exfat_info("volume serial : 0x%x\n", ppbr->bsx.vol_serial);
585 static int exfat_update_boot_checksum(struct exfat_blk_dev *bd, bool is_backup)
587 unsigned int checksum = 0;
588 int ret, sec_idx, backup_sec_idx = 0;
591 buf = malloc(bd->sector_size);
593 exfat_err("Cannot allocate pbr: out of memory\n");
598 backup_sec_idx = BACKUP_BOOT_SEC_IDX;
600 for (sec_idx = BOOT_SEC_IDX; sec_idx < CHECKSUM_SEC_IDX; sec_idx++) {
601 bool is_boot_sec = false;
603 ret = exfat_read_sector(bd, buf, sec_idx + backup_sec_idx);
605 exfat_err("sector(%d) read failed\n", sec_idx);
610 if (sec_idx == BOOT_SEC_IDX)
613 boot_calc_checksum(buf, bd->sector_size, is_boot_sec,
617 ret = exfat_write_checksum_sector(bd, checksum, is_backup);
625 int exfat_set_volume_serial(struct exfat_blk_dev *bd,
626 struct exfat_user_input *ui)
631 ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
633 exfat_err("Cannot allocate pbr: out of memory\n");
637 /* read main boot sector */
638 ret = exfat_read(bd->dev_fd, (char *)ppbr, EXFAT_MAX_SECTOR_SIZE,
641 exfat_err("main boot sector read failed\n");
646 if (memcmp(ppbr->bpb.oem_name, "EXFAT ", 8) != 0) {
647 exfat_err("Bad fs_name in boot sector, which does not describe a valid exfat filesystem\n");
652 bd->sector_size = 1 << ppbr->bsx.sect_size_bits;
653 ppbr->bsx.vol_serial = ui->volume_serial;
655 /* update main boot sector */
656 ret = exfat_write_sector(bd, (char *)ppbr, BOOT_SEC_IDX);
658 exfat_err("main boot sector write failed\n");
663 /* update backup boot sector */
664 ret = exfat_write_sector(bd, (char *)ppbr, BACKUP_BOOT_SEC_IDX);
666 exfat_err("backup boot sector write failed\n");
671 ret = exfat_update_boot_checksum(bd, 0);
673 exfat_err("main checksum update failed\n");
677 ret = exfat_update_boot_checksum(bd, 1);
679 exfat_err("backup checksum update failed\n");
683 exfat_info("New volume serial : 0x%x\n", ui->volume_serial);
688 unsigned int exfat_clus_to_blk_dev_off(struct exfat_blk_dev *bd,
689 unsigned int clu_off_sectnr, unsigned int clu)
691 return clu_off_sectnr * bd->sector_size +
692 (clu - EXFAT_RESERVED_CLUSTERS) * bd->cluster_size;
695 int exfat_get_next_clus(struct exfat *exfat, clus_t clus, clus_t *next)
699 *next = EXFAT_EOF_CLUSTER;
701 if (!exfat_heap_clus(exfat, clus))
704 offset = (off_t)le32_to_cpu(exfat->bs->bsx.fat_offset) <<
705 exfat->bs->bsx.sect_size_bits;
706 offset += sizeof(clus_t) * clus;
708 if (exfat_read(exfat->blk_dev->dev_fd, next, sizeof(*next), offset)
711 *next = le32_to_cpu(*next);
715 int exfat_get_inode_next_clus(struct exfat *exfat, struct exfat_inode *node,
716 clus_t clus, clus_t *next)
718 *next = EXFAT_EOF_CLUSTER;
720 if (node->is_contiguous) {
721 if (!exfat_heap_clus(exfat, clus))
727 return exfat_get_next_clus(exfat, clus, next);
730 int exfat_set_fat(struct exfat *exfat, clus_t clus, clus_t next_clus)
734 offset = le32_to_cpu(exfat->bs->bsx.fat_offset) <<
735 exfat->bs->bsx.sect_size_bits;
736 offset += sizeof(clus_t) * clus;
738 if (exfat_write(exfat->blk_dev->dev_fd, &next_clus, sizeof(next_clus),
739 offset) != sizeof(next_clus))
744 off_t exfat_s2o(struct exfat *exfat, off_t sect)
746 return sect << exfat->bs->bsx.sect_size_bits;
749 off_t exfat_c2o(struct exfat *exfat, unsigned int clus)
751 if (clus < EXFAT_FIRST_CLUSTER)
754 return exfat_s2o(exfat, le32_to_cpu(exfat->bs->bsx.clu_offset) +
755 ((off_t)(clus - EXFAT_FIRST_CLUSTER) <<
756 exfat->bs->bsx.sect_per_clus_bits));
759 int exfat_o2c(struct exfat *exfat, off_t device_offset,
760 unsigned int *clu, unsigned int *offset)
764 heap_offset = exfat_s2o(exfat, le32_to_cpu(exfat->bs->bsx.clu_offset));
765 if (device_offset < heap_offset)
768 *clu = (unsigned int)((device_offset - heap_offset) /
769 exfat->clus_size) + EXFAT_FIRST_CLUSTER;
770 if (!exfat_heap_clus(exfat, *clu))
772 *offset = (device_offset - heap_offset) % exfat->clus_size;
776 bool exfat_heap_clus(struct exfat *exfat, clus_t clus)
778 return clus >= EXFAT_FIRST_CLUSTER &&
779 (clus - EXFAT_FIRST_CLUSTER) < exfat->clus_count;
782 int exfat_root_clus_count(struct exfat *exfat)
784 struct exfat_inode *node = exfat->root;
788 if (!exfat_heap_clus(exfat, node->first_clus))
791 clus = node->first_clus;
793 if (exfat_bitmap_get(exfat->alloc_bitmap, clus))
796 exfat_bitmap_set(exfat->alloc_bitmap, clus);
798 if (exfat_get_inode_next_clus(exfat, node, clus, &next)) {
799 exfat_err("ERROR: failed to read the fat entry of root");
803 if (next != EXFAT_EOF_CLUSTER && !exfat_heap_clus(exfat, next))
808 } while (clus != EXFAT_EOF_CLUSTER);
810 node->size = clus_count * exfat->clus_size;
814 int read_boot_sect(struct exfat_blk_dev *bdev, struct pbr **bs)
818 unsigned int sect_size, clu_size;
820 pbr = malloc(sizeof(struct pbr));
822 if (exfat_read(bdev->dev_fd, pbr, sizeof(*pbr), 0) !=
823 (ssize_t)sizeof(*pbr)) {
824 exfat_err("failed to read a boot sector\n");
830 if (memcmp(pbr->bpb.oem_name, "EXFAT ", 8) != 0) {
831 exfat_err("failed to find exfat file system\n");
835 sect_size = 1 << pbr->bsx.sect_size_bits;
836 clu_size = 1 << (pbr->bsx.sect_size_bits +
837 pbr->bsx.sect_per_clus_bits);
839 if (sect_size < 512 || sect_size > 4 * KB) {
840 exfat_err("too small or big sector size: %d\n",
845 if (clu_size < sect_size || clu_size > 32 * MB) {
846 exfat_err("too small or big cluster size: %d\n",