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");