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 #define BITS_PER_LONG (sizeof(long) * CHAR_BIT)
25 #ifdef WORDS_BIGENDIAN
26 #define BITOP_LE_SWIZZLE ((BITS_PER_LONG - 1) & ~0x7)
28 #define BITOP_LE_SWIZZLE 0
31 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
32 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
34 unsigned int print_level = EXFAT_INFO;
36 static inline void set_bit(int nr, void *addr)
38 unsigned long mask = BIT_MASK(nr);
39 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
44 static inline void clear_bit(int nr, void *addr)
46 unsigned long mask = BIT_MASK(nr);
47 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
52 static inline void set_bit_le(int nr, void *addr)
54 set_bit(nr ^ BITOP_LE_SWIZZLE, addr);
57 static inline void clear_bit_le(int nr, void *addr)
59 clear_bit(nr ^ BITOP_LE_SWIZZLE, addr);
62 void exfat_set_bit(struct exfat_blk_dev *bd, char *bitmap,
67 b = clu & ((bd->sector_size << 3) - 1);
69 set_bit_le(b, bitmap);
72 void exfat_clear_bit(struct exfat_blk_dev *bd, char *bitmap,
77 b = clu & ((bd->sector_size << 3) - 1);
79 clear_bit_le(b, bitmap);
82 wchar_t exfat_bad_char(wchar_t w)
85 || (w == '*') || (w == '?') || (w == '<') || (w == '>')
86 || (w == '|') || (w == '"') || (w == ':') || (w == '/')
90 void boot_calc_checksum(unsigned char *sector, unsigned short size,
91 bool is_boot_sec, __le32 *checksum)
96 for (index = 0; index < size; index++) {
97 if ((index == 106) || (index == 107) || (index == 112))
99 *checksum = ((*checksum & 1) ? 0x80000000 : 0) +
100 (*checksum >> 1) + sector[index];
103 for (index = 0; index < size; index++) {
104 *checksum = ((*checksum & 1) ? 0x80000000 : 0) +
105 (*checksum >> 1) + sector[index];
110 void show_version(void)
112 printf("exfatprogs version : %s\n", EXFAT_PROGS_VERSION);
115 static inline unsigned int sector_size_bits(unsigned int size)
117 unsigned int bits = 8;
122 } while (size > 256);
127 static void exfat_set_default_cluster_size(struct exfat_blk_dev *bd,
128 struct exfat_user_input *ui)
130 if (256 * MB >= bd->size)
131 ui->cluster_size = 4 * KB;
132 else if (32 * GB >= bd->size)
133 ui->cluster_size = 32 * KB;
135 ui->cluster_size = 128 * KB;
138 void init_user_input(struct exfat_user_input *ui)
140 memset(ui, 0, sizeof(struct exfat_user_input));
141 ui->writeable = true;
145 int exfat_get_blk_dev_info(struct exfat_user_input *ui,
146 struct exfat_blk_dev *bd)
151 unsigned long long blk_dev_offset = 0;
153 fd = open(ui->dev_name, ui->writeable ? O_RDWR|O_EXCL : O_RDONLY);
155 exfat_err("open failed : %s, %s\n", ui->dev_name,
159 blk_dev_size = lseek(fd, 0, SEEK_END);
160 if (blk_dev_size <= 0) {
161 exfat_err("invalid block device size(%s)\n",
168 if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode)) {
169 char pathname[sizeof("/sys/dev/block/4294967295:4294967295/start")];
172 snprintf(pathname, sizeof(pathname), "/sys/dev/block/%u:%u/start",
173 major(st.st_rdev), minor(st.st_rdev));
174 fp = fopen(pathname, "r");
176 if (fscanf(fp, "%llu", &blk_dev_offset) == 1) {
178 * Linux kernel always reports partition offset
179 * in 512-byte units, regardless of sector size
181 blk_dev_offset <<= 9;
188 bd->offset = blk_dev_offset;
189 bd->size = blk_dev_size;
190 if (!ui->cluster_size)
191 exfat_set_default_cluster_size(bd, ui);
193 if (!ui->boundary_align)
194 ui->boundary_align = DEFAULT_BOUNDARY_ALIGNMENT;
196 if (ioctl(fd, BLKSSZGET, &bd->sector_size) < 0)
197 bd->sector_size = DEFAULT_SECTOR_SIZE;
198 bd->sector_size_bits = sector_size_bits(bd->sector_size);
199 bd->num_sectors = blk_dev_size / bd->sector_size;
200 bd->num_clusters = blk_dev_size / ui->cluster_size;
202 exfat_debug("Block device name : %s\n", ui->dev_name);
203 exfat_debug("Block device offset : %llu\n", bd->offset);
204 exfat_debug("Block device size : %llu\n", bd->size);
205 exfat_debug("Block sector size : %u\n", bd->sector_size);
206 exfat_debug("Number of the sectors : %llu\n",
208 exfat_debug("Number of the clusters : %u\n",
217 ssize_t exfat_read(int fd, void *buf, size_t size, off_t offset)
219 return pread(fd, buf, size, offset);
222 ssize_t exfat_write(int fd, void *buf, size_t size, off_t offset)
224 return pwrite(fd, buf, size, offset);
227 size_t exfat_utf16_len(const __le16 *str, size_t max_size)
231 while (le16_to_cpu(str[i]) && i < max_size)
236 ssize_t exfat_utf16_enc(const char *in_str, __u16 *out_str, size_t out_size)
238 size_t mbs_len, out_len, i;
241 mbs_len = mbstowcs(NULL, in_str, 0);
242 if (mbs_len == (size_t)-1) {
243 if (errno == EINVAL || errno == EILSEQ)
244 exfat_err("invalid character sequence in current locale\n");
248 wcs = calloc(mbs_len+1, sizeof(wchar_t));
252 /* First convert multibyte char* string to wchar_t* string */
253 if (mbstowcs(wcs, in_str, mbs_len+1) == (size_t)-1) {
254 if (errno == EINVAL || errno == EILSEQ)
255 exfat_err("invalid character sequence in current locale\n");
260 /* Convert wchar_t* string (sequence of code points) to UTF-16 string */
261 for (i = 0, out_len = 0; i < mbs_len; i++) {
262 if (2*(out_len+1) > out_size ||
263 (wcs[i] >= 0x10000 && 2*(out_len+2) > out_size)) {
264 exfat_err("input string is too long\n");
269 /* Encode code point above Plane0 as UTF-16 surrogate pair */
270 if (wcs[i] >= 0x10000) {
272 cpu_to_le16(((wcs[i] - 0x10000) >> 10) + 0xD800);
273 wcs[i] = ((wcs[i] - 0x10000) & 0x3FF) + 0xDC00;
276 out_str[out_len++] = cpu_to_le16(wcs[i]);
283 ssize_t exfat_utf16_dec(const __u16 *in_str, size_t in_len,
284 char *out_str, size_t out_size)
286 size_t wcs_len, out_len, c_len, i;
287 char c_str[MB_LEN_MAX];
292 wcs = calloc(in_len/2+1, sizeof(wchar_t));
296 /* First convert UTF-16 string to wchar_t* string */
297 for (i = 0, wcs_len = 0; i < in_len/2; i++, wcs_len++) {
298 wcs[wcs_len] = le16_to_cpu(in_str[i]);
300 * If wchar_t can store code point above Plane0
301 * then unpack UTF-16 surrogate pair to code point
303 #if WCHAR_MAX >= 0x10FFFF
304 if (wcs[wcs_len] >= 0xD800 && wcs[wcs_len] <= 0xDBFF &&
306 w = le16_to_cpu(in_str[i+1]);
307 if (w >= 0xDC00 && w <= 0xDFFF) {
308 wcs[wcs_len] = 0x10000 +
309 ((wcs[wcs_len] - 0xD800) << 10) +
317 memset(&ps, 0, sizeof(ps));
319 /* And then convert wchar_t* string to multibyte char* string */
320 for (i = 0, out_len = 0, c_len = 0; i <= wcs_len; i++) {
321 c_len = wcrtomb(c_str, wcs[i], &ps);
323 * If character is non-representable in current locale then
324 * try to store it as Unicode replacement code point U+FFFD
326 if (c_len == (size_t)-1 && errno == EILSEQ)
327 c_len = wcrtomb(c_str, 0xFFFD, &ps);
328 /* If U+FFFD is also non-representable, try question mark */
329 if (c_len == (size_t)-1 && errno == EILSEQ)
330 c_len = wcrtomb(c_str, L'?', &ps);
331 /* If also (7bit) question mark fails then we cannot do more */
332 if (c_len == (size_t)-1) {
333 exfat_err("invalid UTF-16 sequence\n");
337 if (out_len+c_len > out_size) {
338 exfat_err("input string is too long\n");
342 memcpy(out_str+out_len, c_str, c_len);
348 /* Last iteration of above loop should have produced null byte */
349 if (c_len == 0 || out_str[out_len-1] != 0) {
350 exfat_err("invalid UTF-16 sequence\n");
357 off_t exfat_get_root_entry_offset(struct exfat_blk_dev *bd)
361 unsigned int cluster_size, sector_size;
364 bs = (struct pbr *)malloc(EXFAT_MAX_SECTOR_SIZE);
366 exfat_err("failed to allocate memory\n");
370 nbytes = exfat_read(bd->dev_fd, bs, EXFAT_MAX_SECTOR_SIZE, 0);
371 if (nbytes != EXFAT_MAX_SECTOR_SIZE) {
372 exfat_err("boot sector read failed: %d\n", errno);
377 sector_size = 1 << bs->bsx.sect_size_bits;
378 cluster_size = (1 << bs->bsx.sect_per_clus_bits) * sector_size;
379 root_clu_off = le32_to_cpu(bs->bsx.clu_offset) * sector_size +
380 (le32_to_cpu(bs->bsx.root_cluster) - EXFAT_RESERVED_CLUSTERS) *
387 char *exfat_conv_volume_label(struct exfat_dentry *vol_entry)
390 __le16 disk_label[VOLUME_LABEL_MAX_LEN];
392 volume_label = malloc(VOLUME_LABEL_BUFFER_SIZE);
396 memcpy(disk_label, vol_entry->vol_label, sizeof(disk_label));
397 memset(volume_label, 0, VOLUME_LABEL_BUFFER_SIZE);
398 if (exfat_utf16_dec(disk_label, vol_entry->vol_char_cnt*2,
399 volume_label, VOLUME_LABEL_BUFFER_SIZE) < 0) {
400 exfat_err("failed to decode volume label\n");
408 int exfat_show_volume_label(struct exfat_blk_dev *bd, off_t root_clu_off)
410 struct exfat_dentry *vol_entry;
414 vol_entry = malloc(sizeof(struct exfat_dentry));
416 exfat_err("failed to allocate memory\n");
420 nbytes = exfat_read(bd->dev_fd, vol_entry,
421 sizeof(struct exfat_dentry), root_clu_off);
422 if (nbytes != sizeof(struct exfat_dentry)) {
423 exfat_err("volume entry read failed: %d\n", errno);
428 volume_label = exfat_conv_volume_label(vol_entry);
434 exfat_info("label: %s\n", volume_label);
441 int exfat_set_volume_label(struct exfat_blk_dev *bd,
442 char *label_input, off_t root_clu_off)
444 struct exfat_dentry vol;
446 __u16 volume_label[VOLUME_LABEL_MAX_LEN];
447 int volume_label_len;
449 volume_label_len = exfat_utf16_enc(label_input,
450 volume_label, sizeof(volume_label));
451 if (volume_label_len < 0) {
452 exfat_err("failed to encode volume label\n");
456 vol.type = EXFAT_VOLUME;
457 memset(vol.vol_label, 0, sizeof(vol.vol_label));
458 memcpy(vol.vol_label, volume_label, volume_label_len);
459 vol.vol_char_cnt = volume_label_len/2;
461 nbytes = exfat_write(bd->dev_fd, &vol, sizeof(struct exfat_dentry),
463 if (nbytes != sizeof(struct exfat_dentry)) {
464 exfat_err("volume entry write failed: %d\n", errno);
469 exfat_info("new label: %s\n", label_input);
473 int exfat_read_sector(struct exfat_blk_dev *bd, void *buf, unsigned int sec_off)
476 unsigned long long offset = sec_off * bd->sector_size;
478 ret = pread(bd->dev_fd, buf, bd->sector_size, offset);
480 exfat_err("read failed, sec_off : %u\n", sec_off);
486 int exfat_write_sector(struct exfat_blk_dev *bd, void *buf,
487 unsigned int sec_off)
490 unsigned long long offset = sec_off * bd->sector_size;
492 bytes = pwrite(bd->dev_fd, buf, bd->sector_size, offset);
493 if (bytes != (int)bd->sector_size) {
494 exfat_err("write failed, sec_off : %u, bytes : %d\n", sec_off,
501 int exfat_write_checksum_sector(struct exfat_blk_dev *bd,
502 unsigned int checksum, bool is_backup)
504 __le32 *checksum_buf;
507 unsigned int sec_idx = CHECKSUM_SEC_IDX;
509 checksum_buf = malloc(bd->sector_size);
514 sec_idx += BACKUP_BOOT_SEC_IDX;
516 for (i = 0; i < bd->sector_size / sizeof(int); i++)
517 checksum_buf[i] = cpu_to_le32(checksum);
519 ret = exfat_write_sector(bd, checksum_buf, sec_idx);
521 exfat_err("checksum sector write failed\n");
530 int exfat_show_volume_serial(int fd)
535 ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
537 exfat_err("Cannot allocate pbr: out of memory\n");
541 /* read main boot sector */
542 ret = exfat_read(fd, (char *)ppbr, EXFAT_MAX_SECTOR_SIZE, 0);
544 exfat_err("main boot sector read failed\n");
549 exfat_info("volume serial : 0x%x\n", ppbr->bsx.vol_serial);
556 static int exfat_update_boot_checksum(struct exfat_blk_dev *bd, bool is_backup)
558 unsigned int checksum = 0;
559 int ret, sec_idx, backup_sec_idx = 0;
562 buf = malloc(bd->sector_size);
564 exfat_err("Cannot allocate pbr: out of memory\n");
569 backup_sec_idx = BACKUP_BOOT_SEC_IDX;
571 for (sec_idx = BOOT_SEC_IDX; sec_idx < CHECKSUM_SEC_IDX; sec_idx++) {
572 bool is_boot_sec = false;
574 ret = exfat_read_sector(bd, buf, sec_idx + backup_sec_idx);
576 exfat_err("sector(%d) read failed\n", sec_idx);
581 if (sec_idx == BOOT_SEC_IDX)
584 boot_calc_checksum(buf, bd->sector_size, is_boot_sec,
588 ret = exfat_write_checksum_sector(bd, checksum, is_backup);
596 int exfat_set_volume_serial(struct exfat_blk_dev *bd,
597 struct exfat_user_input *ui)
602 ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
604 exfat_err("Cannot allocate pbr: out of memory\n");
608 /* read main boot sector */
609 ret = exfat_read(bd->dev_fd, (char *)ppbr, EXFAT_MAX_SECTOR_SIZE,
612 exfat_err("main boot sector read failed\n");
617 bd->sector_size = 1 << ppbr->bsx.sect_size_bits;
618 ppbr->bsx.vol_serial = ui->volume_serial;
620 /* update main boot sector */
621 ret = exfat_write_sector(bd, (char *)ppbr, BOOT_SEC_IDX);
623 exfat_err("main boot sector write failed\n");
628 /* update backup boot sector */
629 ret = exfat_write_sector(bd, (char *)ppbr, BACKUP_BOOT_SEC_IDX);
631 exfat_err("backup boot sector write failed\n");
636 ret = exfat_update_boot_checksum(bd, 0);
638 exfat_err("main checksum update failed\n");
642 ret = exfat_update_boot_checksum(bd, 1);
644 exfat_err("backup checksum update failed\n");
648 exfat_info("New volume serial : 0x%x\n", ui->volume_serial);
653 unsigned int exfat_clus_to_blk_dev_off(struct exfat_blk_dev *bd,
654 unsigned int clu_off_sectnr, unsigned int clu)
656 return clu_off_sectnr * bd->sector_size +
657 (clu - EXFAT_RESERVED_CLUSTERS) * bd->cluster_size;