1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2019 Namjae Jeon <linkinjeon@kernel.org>
18 #include "exfat_ondisk.h"
22 #ifdef WORDS_BIGENDIAN
23 #define BITOP_LE_SWIZZLE (~0x7)
25 #define BITOP_LE_SWIZZLE 0
28 #define BIT_MASK(nr) ((1) << ((nr) % 32))
29 #define BIT_WORD(nr) ((nr) / 32)
31 unsigned int print_level = EXFAT_INFO;
33 static inline void set_bit(int nr, unsigned int *addr)
35 unsigned long mask = BIT_MASK(nr);
36 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
41 static inline void clear_bit(int nr, unsigned int *addr)
43 unsigned long mask = BIT_MASK(nr);
44 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
49 static inline void set_bit_le(int nr, void *addr)
51 set_bit(nr ^ BITOP_LE_SWIZZLE, addr);
54 static inline void clear_bit_le(int nr, void *addr)
56 clear_bit(nr ^ BITOP_LE_SWIZZLE, addr);
59 void exfat_set_bit(struct exfat_blk_dev *bd, char *bitmap,
64 b = clu & ((bd->sector_size << 3) - 1);
66 set_bit_le(b, bitmap);
69 void exfat_clear_bit(struct exfat_blk_dev *bd, char *bitmap,
74 b = clu & ((bd->sector_size << 3) - 1);
76 clear_bit_le(b, bitmap);
79 wchar_t exfat_bad_char(wchar_t w)
82 || (w == '*') || (w == '?') || (w == '<') || (w == '>')
83 || (w == '|') || (w == '"') || (w == ':') || (w == '/')
87 void boot_calc_checksum(unsigned char *sector, unsigned short size,
88 bool is_boot_sec, __le32 *checksum)
93 for (index = 0; index < size; index++) {
94 if ((index == 106) || (index == 107) || (index == 112))
96 *checksum = ((*checksum & 1) ? 0x80000000 : 0) +
97 (*checksum >> 1) + sector[index];
100 for (index = 0; index < size; index++) {
101 *checksum = ((*checksum & 1) ? 0x80000000 : 0) +
102 (*checksum >> 1) + sector[index];
107 void show_version(void)
109 printf("exfatprogs version : %s\n", EXFAT_PROGS_VERSION);
112 static inline unsigned int sector_size_bits(unsigned int size)
114 unsigned int bits = 8;
119 } while (size > 256);
124 static void exfat_set_default_cluster_size(struct exfat_blk_dev *bd,
125 struct exfat_user_input *ui)
127 if (256 * MB >= bd->size)
128 ui->cluster_size = 4 * KB;
129 else if (32 * GB >= bd->size)
130 ui->cluster_size = 32 * KB;
132 ui->cluster_size = 128 * KB;
135 void init_user_input(struct exfat_user_input *ui)
137 memset(ui, 0, sizeof(struct exfat_user_input));
138 ui->writeable = true;
142 int exfat_get_blk_dev_info(struct exfat_user_input *ui,
143 struct exfat_blk_dev *bd)
148 fd = open(ui->dev_name, ui->writeable ? O_RDWR|O_EXCL : O_RDONLY);
150 exfat_err("open failed : %s, %s\n", ui->dev_name,
154 blk_dev_size = lseek(fd, 0, SEEK_END);
155 if (blk_dev_size <= 0) {
156 exfat_err("invalid block device size(%s)\n",
164 bd->size = blk_dev_size;
165 if (!ui->cluster_size)
166 exfat_set_default_cluster_size(bd, ui);
168 if (!ui->boundary_align)
169 ui->boundary_align = DEFAULT_BOUNDARY_ALIGNMENT;
171 if (ioctl(fd, BLKSSZGET, &bd->sector_size) < 0)
172 bd->sector_size = DEFAULT_SECTOR_SIZE;
173 bd->sector_size_bits = sector_size_bits(bd->sector_size);
174 bd->num_sectors = blk_dev_size / DEFAULT_SECTOR_SIZE;
175 bd->num_clusters = blk_dev_size / ui->cluster_size;
177 exfat_debug("Block device name : %s\n", ui->dev_name);
178 exfat_debug("Block device size : %lld\n", bd->size);
179 exfat_debug("Block sector size : %u\n", bd->sector_size);
180 exfat_debug("Number of the sectors : %llu\n",
182 exfat_debug("Number of the clusters : %u\n",
191 ssize_t exfat_read(int fd, void *buf, size_t size, off_t offset)
193 return pread(fd, buf, size, offset);
196 ssize_t exfat_write(int fd, void *buf, size_t size, off_t offset)
198 return pwrite(fd, buf, size, offset);
201 size_t exfat_utf16_len(const __le16 *str, size_t max_size)
205 while (le16_to_cpu(str[i]) && i < max_size)
210 ssize_t exfat_utf16_enc(const char *in_str, __u16 *out_str, size_t out_size)
212 size_t mbs_len, out_len, i;
215 mbs_len = mbstowcs(NULL, in_str, 0);
216 if (mbs_len == (size_t)-1) {
217 if (errno == EINVAL || errno == EILSEQ)
218 exfat_err("invalid character sequence in current locale\n");
222 wcs = calloc(mbs_len+1, sizeof(wchar_t));
226 /* First convert multibyte char* string to wchar_t* string */
227 if (mbstowcs(wcs, in_str, mbs_len+1) == (size_t)-1) {
228 if (errno == EINVAL || errno == EILSEQ)
229 exfat_err("invalid character sequence in current locale\n");
234 /* Convert wchar_t* string (sequence of code points) to UTF-16 string */
235 for (i = 0, out_len = 0; i < mbs_len; i++) {
236 if (2*(out_len+1) > out_size ||
237 (wcs[i] >= 0x10000 && 2*(out_len+2) > out_size)) {
238 exfat_err("input string is too long\n");
243 /* Encode code point above Plane0 as UTF-16 surrogate pair */
244 if (wcs[i] >= 0x10000) {
246 cpu_to_le16(((wcs[i] - 0x10000) >> 10) + 0xD800);
247 wcs[i] = ((wcs[i] - 0x10000) & 0x3FF) + 0xDC00;
250 out_str[out_len++] = cpu_to_le16(wcs[i]);
257 ssize_t exfat_utf16_dec(const __u16 *in_str, size_t in_len,
258 char *out_str, size_t out_size)
260 size_t wcs_len, out_len, c_len, i;
261 char c_str[MB_LEN_MAX];
266 wcs = calloc(in_len/2+1, sizeof(wchar_t));
270 /* First convert UTF-16 string to wchar_t* string */
271 for (i = 0, wcs_len = 0; i < in_len/2; i++, wcs_len++) {
272 wcs[wcs_len] = le16_to_cpu(in_str[i]);
274 * If wchar_t can store code point above Plane0
275 * then unpack UTF-16 surrogate pair to code point
277 #if WCHAR_MAX >= 0x10FFFF
278 if (wcs[wcs_len] >= 0xD800 && wcs[wcs_len] <= 0xDBFF &&
280 w = le16_to_cpu(in_str[i+1]);
281 if (w >= 0xDC00 && w <= 0xDFFF) {
282 wcs[wcs_len] = 0x10000 +
283 ((wcs[wcs_len] - 0xD800) << 10) +
291 memset(&ps, 0, sizeof(ps));
293 /* And then convert wchar_t* string to multibyte char* string */
294 for (i = 0, out_len = 0, c_len = 0; i <= wcs_len; i++) {
295 c_len = wcrtomb(c_str, wcs[i], &ps);
297 * If character is non-representable in current locale then
298 * try to store it as Unicode replacement code point U+FFFD
300 if (c_len == (size_t)-1 && errno == EILSEQ)
301 c_len = wcrtomb(c_str, 0xFFFD, &ps);
302 /* If U+FFFD is also non-representable, try question mark */
303 if (c_len == (size_t)-1 && errno == EILSEQ)
304 c_len = wcrtomb(c_str, L'?', &ps);
305 /* If also (7bit) question mark fails then we cannot do more */
306 if (c_len == (size_t)-1) {
307 exfat_err("invalid UTF-16 sequence\n");
311 if (out_len+c_len > out_size) {
312 exfat_err("input string is too long\n");
316 memcpy(out_str+out_len, c_str, c_len);
322 /* Last iteration of above loop should have produced null byte */
323 if (c_len == 0 || out_str[out_len-1] != 0) {
324 exfat_err("invalid UTF-16 sequence\n");
331 off_t exfat_get_root_entry_offset(struct exfat_blk_dev *bd)
335 unsigned int cluster_size;
338 bs = (struct pbr *)malloc(sizeof(struct pbr));
340 exfat_err("failed to allocate memory\n");
344 nbytes = exfat_read(bd->dev_fd, bs, sizeof(struct pbr), 0);
345 if (nbytes != sizeof(struct pbr)) {
346 exfat_err("boot sector read failed: %d\n", errno);
350 cluster_size = (1 << bs->bsx.sect_per_clus_bits) * bd->sector_size;
351 root_clu_off = le32_to_cpu(bs->bsx.clu_offset) * bd->sector_size +
352 le32_to_cpu(bs->bsx.root_cluster - EXFAT_REVERVED_CLUSTERS)
359 char *exfat_conv_volume_serial(struct exfat_dentry *vol_entry)
362 __le16 disk_label[VOLUME_LABEL_MAX_LEN];
364 volume_label = malloc(VOLUME_LABEL_BUFFER_SIZE);
368 memcpy(disk_label, vol_entry->vol_label, sizeof(disk_label));
369 memset(volume_label, 0, VOLUME_LABEL_BUFFER_SIZE);
370 if (exfat_utf16_dec(disk_label, vol_entry->vol_char_cnt*2,
371 volume_label, VOLUME_LABEL_BUFFER_SIZE) < 0) {
372 exfat_err("failed to decode volume label\n");
379 int exfat_show_volume_label(struct exfat_blk_dev *bd, off_t root_clu_off)
381 struct exfat_dentry *vol_entry;
385 vol_entry = malloc(sizeof(struct exfat_dentry));
387 exfat_err("failed to allocate memory\n");
391 nbytes = exfat_read(bd->dev_fd, vol_entry,
392 sizeof(struct exfat_dentry), root_clu_off);
393 if (nbytes != sizeof(struct exfat_dentry)) {
394 exfat_err("volume entry read failed: %d\n", errno);
398 volume_label = exfat_conv_volume_serial(vol_entry);
402 exfat_info("label: %s\n", volume_label);
408 int exfat_set_volume_label(struct exfat_blk_dev *bd,
409 char *label_input, off_t root_clu_off)
411 struct exfat_dentry vol;
413 __u16 volume_label[VOLUME_LABEL_MAX_LEN];
414 int volume_label_len;
416 volume_label_len = exfat_utf16_enc(label_input,
417 volume_label, sizeof(volume_label));
418 if (volume_label_len < 0) {
419 exfat_err("failed to encode volume label\n");
423 vol.type = EXFAT_VOLUME;
424 memset(vol.vol_label, 0, sizeof(vol.vol_label));
425 memcpy(vol.vol_label, volume_label, volume_label_len);
426 vol.vol_char_cnt = volume_label_len/2;
428 nbytes = exfat_write(bd->dev_fd, &vol, sizeof(struct exfat_dentry),
430 if (nbytes != sizeof(struct exfat_dentry)) {
431 exfat_err("volume entry write failed: %d\n", errno);
436 exfat_info("new label: %s\n", label_input);
440 int exfat_read_sector(struct exfat_blk_dev *bd, void *buf, unsigned int sec_off)
443 unsigned long long offset = sec_off * bd->sector_size;
445 lseek(bd->dev_fd, offset, SEEK_SET);
446 ret = read(bd->dev_fd, buf, bd->sector_size);
448 exfat_err("read failed, sec_off : %u\n", sec_off);
454 int exfat_write_sector(struct exfat_blk_dev *bd, void *buf,
455 unsigned int sec_off)
458 unsigned long long offset = sec_off * bd->sector_size;
460 lseek(bd->dev_fd, offset, SEEK_SET);
461 bytes = write(bd->dev_fd, buf, bd->sector_size);
462 if (bytes != (int)bd->sector_size) {
463 exfat_err("write failed, sec_off : %u, bytes : %d\n", sec_off,
470 int exfat_write_checksum_sector(struct exfat_blk_dev *bd,
471 unsigned int checksum, bool is_backup)
473 __le32 *checksum_buf;
476 unsigned int sec_idx = CHECKSUM_SEC_IDX;
478 checksum_buf = malloc(bd->sector_size);
483 sec_idx += BACKUP_BOOT_SEC_IDX;
485 for (i = 0; i < bd->sector_size / sizeof(int); i++)
486 checksum_buf[i] = cpu_to_le32(checksum);
488 ret = exfat_write_sector(bd, checksum_buf, sec_idx);
490 exfat_err("checksum sector write failed\n");
499 int exfat_show_volume_serial(struct exfat_blk_dev *bd,
500 struct exfat_user_input *ui)
505 ppbr = malloc(bd->sector_size);
507 exfat_err("Cannot allocate pbr: out of memory\n");
511 /* read main boot sector */
512 ret = exfat_read_sector(bd, (char *)ppbr, BOOT_SEC_IDX);
514 exfat_err("main boot sector read failed\n");
519 exfat_info("volume serial : 0x%x\n", ppbr->bsx.vol_serial);
526 static int exfat_update_boot_checksum(struct exfat_blk_dev *bd, bool is_backup)
528 unsigned int checksum = 0;
529 int ret, sec_idx, backup_sec_idx = 0;
530 int sector_size = bd->sector_size;
533 buf = malloc(bd->sector_size);
535 exfat_err("Cannot allocate pbr: out of memory\n");
540 backup_sec_idx = BACKUP_BOOT_SEC_IDX;
542 for (sec_idx = BOOT_SEC_IDX; sec_idx < CHECKSUM_SEC_IDX; sec_idx++) {
543 bool is_boot_sec = false;
545 ret = exfat_read_sector(bd, buf, sec_idx + backup_sec_idx);
547 exfat_err("sector(%d) read failed\n", sec_idx);
552 if (sec_idx == BOOT_SEC_IDX) {
554 sector_size = sizeof(struct pbr);
555 } else if (sec_idx >= EXBOOT_SEC_IDX && sec_idx < OEM_SEC_IDX)
556 sector_size = sizeof(struct exbs);
558 boot_calc_checksum(buf, sector_size, is_boot_sec,
562 ret = exfat_write_checksum_sector(bd, checksum, is_backup);
570 int exfat_set_volume_serial(struct exfat_blk_dev *bd,
571 struct exfat_user_input *ui)
576 ppbr = malloc(bd->sector_size);
578 exfat_err("Cannot allocate pbr: out of memory\n");
582 /* read main boot sector */
583 ret = exfat_read_sector(bd, (char *)ppbr, BOOT_SEC_IDX);
585 exfat_err("main boot sector read failed\n");
590 ppbr->bsx.vol_serial = ui->volume_serial;
592 /* update main boot sector */
593 ret = exfat_write_sector(bd, (char *)ppbr, BOOT_SEC_IDX);
595 exfat_err("main boot sector write failed\n");
600 /* update backup boot sector */
601 ret = exfat_write_sector(bd, (char *)ppbr, BACKUP_BOOT_SEC_IDX);
603 exfat_err("backup boot sector write failed\n");
608 ret = exfat_update_boot_checksum(bd, 0);
610 exfat_err("main checksum update failed\n");
614 ret = exfat_update_boot_checksum(bd, 1);
616 exfat_err("backup checksum update failed\n");
620 exfat_info("New volume serial : 0x%x\n", ui->volume_serial);
625 unsigned int exfat_clus_to_blk_dev_off(struct exfat_blk_dev *bd,
626 unsigned int clu_off_sectnr, unsigned int clu)
628 return clu_off_sectnr * bd->sector_size +
629 (clu - EXFAT_REVERVED_CLUSTERS) * bd->cluster_size;