• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2019 Namjae Jeon <linkinjeon@kernel.org>
4  */
5 
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/ioctl.h>
9 #include <sys/sysmacros.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <wchar.h>
17 #include <limits.h>
18 #include <assert.h>
19 
20 #include "exfat_ondisk.h"
21 #include "libexfat.h"
22 #include "version.h"
23 #include "exfat_fs.h"
24 #include "exfat_dir.h"
25 
26 unsigned int print_level  = EXFAT_INFO;
27 
exfat_bitmap_set_range(struct exfat * exfat,char * bitmap,clus_t start_clus,clus_t count)28 void exfat_bitmap_set_range(struct exfat *exfat, char *bitmap,
29 			    clus_t start_clus, clus_t count)
30 {
31 	clus_t clus;
32 
33 	if (!exfat_heap_clus(exfat, start_clus) ||
34 	    !exfat_heap_clus(exfat, start_clus + count - 1))
35 		return;
36 
37 	clus = start_clus;
38 	while (clus < start_clus + count) {
39 		exfat_bitmap_set(bitmap, clus);
40 		clus++;
41 	}
42 }
43 
exfat_bitmap_find_bit(struct exfat * exfat,char * bmap,clus_t start_clu,clus_t * next,int bit)44 static int exfat_bitmap_find_bit(struct exfat *exfat, char *bmap,
45 				 clus_t start_clu, clus_t *next,
46 				 int bit)
47 {
48 	clus_t last_clu;
49 
50 	last_clu = le32_to_cpu(exfat->bs->bsx.clu_count) +
51 		EXFAT_FIRST_CLUSTER;
52 	while (start_clu < last_clu) {
53 		if (!!exfat_bitmap_get(bmap, start_clu) == bit) {
54 			*next = start_clu;
55 			return 0;
56 		}
57 		start_clu++;
58 	}
59 	return 1;
60 }
61 
exfat_bitmap_find_zero(struct exfat * exfat,char * bmap,clus_t start_clu,clus_t * next)62 int exfat_bitmap_find_zero(struct exfat *exfat, char *bmap,
63 			   clus_t start_clu, clus_t *next)
64 {
65 	return exfat_bitmap_find_bit(exfat, bmap,
66 				     start_clu, next, 0);
67 }
68 
exfat_bitmap_find_one(struct exfat * exfat,char * bmap,clus_t start_clu,clus_t * next)69 int exfat_bitmap_find_one(struct exfat *exfat, char *bmap,
70 			  clus_t start_clu, clus_t *next)
71 {
72 	return exfat_bitmap_find_bit(exfat, bmap,
73 				     start_clu, next, 1);
74 }
75 
exfat_bad_char(wchar_t w)76 wchar_t exfat_bad_char(wchar_t w)
77 {
78 	return (w < 0x0020)
79 		|| (w == '*') || (w == '?') || (w == '<') || (w == '>')
80 		|| (w == '|') || (w == '"') || (w == ':') || (w == '/')
81 		|| (w == '\\');
82 }
83 
boot_calc_checksum(unsigned char * sector,unsigned short size,bool is_boot_sec,__le32 * checksum)84 void boot_calc_checksum(unsigned char *sector, unsigned short size,
85 		bool is_boot_sec, __le32 *checksum)
86 {
87 	unsigned int index;
88 
89 	if (is_boot_sec) {
90 		for (index = 0; index < size; index++) {
91 			if ((index == 106) || (index == 107) || (index == 112))
92 				continue;
93 			*checksum = ((*checksum & 1) ? 0x80000000 : 0) +
94 				(*checksum >> 1) + sector[index];
95 		}
96 	} else {
97 		for (index = 0; index < size; index++) {
98 			*checksum = ((*checksum & 1) ? 0x80000000 : 0) +
99 				(*checksum >> 1) + sector[index];
100 		}
101 	}
102 }
103 
show_version(void)104 void show_version(void)
105 {
106 	printf("exfatprogs version : %s\n", EXFAT_PROGS_VERSION);
107 }
108 
sector_size_bits(unsigned int size)109 static inline unsigned int sector_size_bits(unsigned int size)
110 {
111 	unsigned int bits = 8;
112 
113 	do {
114 		bits++;
115 		size >>= 1;
116 	} while (size > 256);
117 
118 	return bits;
119 }
120 
exfat_set_default_cluster_size(struct exfat_blk_dev * bd,struct exfat_user_input * ui)121 static void exfat_set_default_cluster_size(struct exfat_blk_dev *bd,
122 		struct exfat_user_input *ui)
123 {
124 	if (256 * MB >= bd->size)
125 		ui->cluster_size = 4 * KB;
126 	else if (32 * GB >= bd->size)
127 		ui->cluster_size = 32 * KB;
128 	else
129 		ui->cluster_size = 128 * KB;
130 }
131 
init_user_input(struct exfat_user_input * ui)132 void init_user_input(struct exfat_user_input *ui)
133 {
134 	memset(ui, 0, sizeof(struct exfat_user_input));
135 	ui->writeable = true;
136 	ui->quick = true;
137 }
138 
exfat_get_blk_dev_info(struct exfat_user_input * ui,struct exfat_blk_dev * bd)139 int exfat_get_blk_dev_info(struct exfat_user_input *ui,
140 		struct exfat_blk_dev *bd)
141 {
142 	int fd, ret = -1;
143 	off_t blk_dev_size;
144 	struct stat st;
145 	unsigned long long blk_dev_offset = 0;
146 
147 	fd = open(ui->dev_name, ui->writeable ? O_RDWR|O_EXCL : O_RDONLY);
148 	if (fd < 0) {
149 		exfat_err("open failed : %s, %s\n", ui->dev_name,
150 			strerror(errno));
151 		return -1;
152 	}
153 	blk_dev_size = lseek(fd, 0, SEEK_END);
154 	if (blk_dev_size <= 0) {
155 		exfat_err("invalid block device size(%s)\n",
156 			ui->dev_name);
157 		ret = blk_dev_size;
158 		close(fd);
159 		goto out;
160 	}
161 
162 	if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode)) {
163 		char pathname[sizeof("/sys/dev/block/4294967295:4294967295/start")];
164 		FILE *fp;
165 
166 		snprintf(pathname, sizeof(pathname), "/sys/dev/block/%u:%u/start",
167 			major(st.st_rdev), minor(st.st_rdev));
168 		fp = fopen(pathname, "r");
169 		if (fp != NULL) {
170 			if (fscanf(fp, "%llu", &blk_dev_offset) == 1) {
171 				/*
172 				 * Linux kernel always reports partition offset
173 				 * in 512-byte units, regardless of sector size
174 				 */
175 				blk_dev_offset <<= 9;
176 			}
177 			fclose(fp);
178 		}
179 	}
180 
181 	bd->dev_fd = fd;
182 	bd->offset = blk_dev_offset;
183 	bd->size = blk_dev_size;
184 	if (!ui->cluster_size)
185 		exfat_set_default_cluster_size(bd, ui);
186 
187 	if (!ui->boundary_align)
188 		ui->boundary_align = DEFAULT_BOUNDARY_ALIGNMENT;
189 
190 	if (ui->sector_size)
191 		bd->sector_size = ui->sector_size;
192 	else if (ioctl(fd, BLKSSZGET, &bd->sector_size) < 0)
193 		bd->sector_size = DEFAULT_SECTOR_SIZE;
194 	bd->sector_size_bits = sector_size_bits(bd->sector_size);
195 	bd->num_sectors = blk_dev_size / bd->sector_size;
196 	bd->num_clusters = blk_dev_size / ui->cluster_size;
197 
198 	exfat_debug("Block device name : %s\n", ui->dev_name);
199 	exfat_debug("Block device offset : %llu\n", bd->offset);
200 	exfat_debug("Block device size : %llu\n", bd->size);
201 	exfat_debug("Block sector size : %u\n", bd->sector_size);
202 	exfat_debug("Number of the sectors : %llu\n",
203 		bd->num_sectors);
204 	exfat_debug("Number of the clusters : %u\n",
205 		bd->num_clusters);
206 
207 	ret = 0;
208 	bd->dev_fd = fd;
209 out:
210 	return ret;
211 }
212 
exfat_read(int fd,void * buf,size_t size,off_t offset)213 ssize_t exfat_read(int fd, void *buf, size_t size, off_t offset)
214 {
215 	return pread(fd, buf, size, offset);
216 }
217 
exfat_write(int fd,void * buf,size_t size,off_t offset)218 ssize_t exfat_write(int fd, void *buf, size_t size, off_t offset)
219 {
220 	return pwrite(fd, buf, size, offset);
221 }
222 
exfat_write_zero(int fd,size_t size,off_t offset)223 ssize_t exfat_write_zero(int fd, size_t size, off_t offset)
224 {
225 	const char zero_buf[4 * KB] = {0};
226 
227 	lseek(fd, offset, SEEK_SET);
228 
229 	while (size > 0) {
230 		int iter_size = MIN(size, sizeof(zero_buf));
231 
232 		if (iter_size != write(fd, zero_buf, iter_size))
233 			return -EIO;
234 
235 		size -= iter_size;
236 	}
237 
238 	return 0;
239 }
240 
exfat_utf16_len(const __le16 * str,size_t max_size)241 size_t exfat_utf16_len(const __le16 *str, size_t max_size)
242 {
243 	size_t i = 0;
244 
245 	while (le16_to_cpu(str[i]) && i < max_size)
246 		i++;
247 	return i;
248 }
249 
exfat_utf16_enc(const char * in_str,__u16 * out_str,size_t out_size)250 ssize_t exfat_utf16_enc(const char *in_str, __u16 *out_str, size_t out_size)
251 {
252 	size_t mbs_len, out_len, i;
253 	wchar_t *wcs;
254 
255 	mbs_len = mbstowcs(NULL, in_str, 0);
256 	if (mbs_len == (size_t)-1) {
257 		if (errno == EINVAL || errno == EILSEQ)
258 			exfat_err("invalid character sequence in current locale\n");
259 		return -errno;
260 	}
261 
262 	wcs = calloc(mbs_len+1, sizeof(wchar_t));
263 	if (!wcs)
264 		return -ENOMEM;
265 
266 	/* First convert multibyte char* string to wchar_t* string */
267 	if (mbstowcs(wcs, in_str, mbs_len+1) == (size_t)-1) {
268 		if (errno == EINVAL || errno == EILSEQ)
269 			exfat_err("invalid character sequence in current locale\n");
270 		free(wcs);
271 		return -errno;
272 	}
273 
274 	/* Convert wchar_t* string (sequence of code points) to UTF-16 string */
275 	for (i = 0, out_len = 0; i < mbs_len; i++) {
276 		if (2*(out_len+1) > out_size ||
277 		    (wcs[i] >= 0x10000 && 2*(out_len+2) > out_size)) {
278 			exfat_err("input string is too long\n");
279 			free(wcs);
280 			return -E2BIG;
281 		}
282 
283 		/* Encode code point above Plane0 as UTF-16 surrogate pair */
284 		if (wcs[i] >= 0x10000) {
285 			out_str[out_len++] =
286 			  cpu_to_le16(((wcs[i] - 0x10000) >> 10) + 0xD800);
287 			wcs[i] = ((wcs[i] - 0x10000) & 0x3FF) + 0xDC00;
288 		}
289 
290 		out_str[out_len++] = cpu_to_le16(wcs[i]);
291 	}
292 
293 	free(wcs);
294 	return 2*out_len;
295 }
296 
exfat_utf16_dec(const __u16 * in_str,size_t in_len,char * out_str,size_t out_size)297 ssize_t exfat_utf16_dec(const __u16 *in_str, size_t in_len,
298 			char *out_str, size_t out_size)
299 {
300 	size_t wcs_len, out_len, c_len, i;
301 	char c_str[MB_LEN_MAX];
302 	wchar_t *wcs;
303 	mbstate_t ps;
304 	wchar_t w;
305 
306 	wcs = calloc(in_len/2+1, sizeof(wchar_t));
307 	if (!wcs)
308 		return -ENOMEM;
309 
310 	/* First convert UTF-16 string to wchar_t* string */
311 	for (i = 0, wcs_len = 0; i < in_len/2; i++, wcs_len++) {
312 		wcs[wcs_len] = le16_to_cpu(in_str[i]);
313 		/*
314 		 * If wchar_t can store code point above Plane0
315 		 * then unpack UTF-16 surrogate pair to code point
316 		 */
317 #if WCHAR_MAX >= 0x10FFFF
318 		if (wcs[wcs_len] >= 0xD800 && wcs[wcs_len] <= 0xDBFF &&
319 		    i+1 < in_len/2) {
320 			w = le16_to_cpu(in_str[i+1]);
321 			if (w >= 0xDC00 && w <= 0xDFFF) {
322 				wcs[wcs_len] = 0x10000 +
323 					       ((wcs[wcs_len] - 0xD800) << 10) +
324 					       (w - 0xDC00);
325 				i++;
326 			}
327 		}
328 #endif
329 	}
330 
331 	memset(&ps, 0, sizeof(ps));
332 
333 	/* And then convert wchar_t* string to multibyte char* string */
334 	for (i = 0, out_len = 0, c_len = 0; i <= wcs_len; i++) {
335 		c_len = wcrtomb(c_str, wcs[i], &ps);
336 		/*
337 		 * If character is non-representable in current locale then
338 		 * try to store it as Unicode replacement code point U+FFFD
339 		 */
340 		if (c_len == (size_t)-1 && errno == EILSEQ)
341 			c_len = wcrtomb(c_str, 0xFFFD, &ps);
342 		/* If U+FFFD is also non-representable, try question mark */
343 		if (c_len == (size_t)-1 && errno == EILSEQ)
344 			c_len = wcrtomb(c_str, L'?', &ps);
345 		/* If also (7bit) question mark fails then we cannot do more */
346 		if (c_len == (size_t)-1) {
347 			exfat_err("invalid UTF-16 sequence\n");
348 			free(wcs);
349 			return -errno;
350 		}
351 		if (out_len+c_len > out_size) {
352 			exfat_err("input string is too long\n");
353 			free(wcs);
354 			return -E2BIG;
355 		}
356 		memcpy(out_str+out_len, c_str, c_len);
357 		out_len += c_len;
358 	}
359 
360 	free(wcs);
361 
362 	/* Last iteration of above loop should have produced null byte */
363 	if (c_len == 0 || out_str[out_len-1] != 0) {
364 		exfat_err("invalid UTF-16 sequence\n");
365 		return -errno;
366 	}
367 
368 	return out_len-1;
369 }
370 
exfat_get_root_entry_offset(struct exfat_blk_dev * bd)371 off_t exfat_get_root_entry_offset(struct exfat_blk_dev *bd)
372 {
373 	struct pbr *bs;
374 	int nbytes;
375 	unsigned int cluster_size, sector_size;
376 	off_t root_clu_off;
377 
378 	bs = malloc(EXFAT_MAX_SECTOR_SIZE);
379 	if (!bs) {
380 		exfat_err("failed to allocate memory\n");
381 		return -ENOMEM;
382 	}
383 
384 	nbytes = exfat_read(bd->dev_fd, bs, EXFAT_MAX_SECTOR_SIZE, 0);
385 	if (nbytes != EXFAT_MAX_SECTOR_SIZE) {
386 		exfat_err("boot sector read failed: %d\n", errno);
387 		free(bs);
388 		return -1;
389 	}
390 
391 	if (memcmp(bs->bpb.oem_name, "EXFAT   ", 8) != 0) {
392 		exfat_err("Bad fs_name in boot sector, which does not describe a valid exfat filesystem\n");
393 		free(bs);
394 		return -1;
395 	}
396 
397 	sector_size = 1 << bs->bsx.sect_size_bits;
398 	cluster_size = (1 << bs->bsx.sect_per_clus_bits) * sector_size;
399 	root_clu_off = le32_to_cpu(bs->bsx.clu_offset) * sector_size +
400 		(le32_to_cpu(bs->bsx.root_cluster) - EXFAT_RESERVED_CLUSTERS) *
401 		cluster_size;
402 	free(bs);
403 
404 	return root_clu_off;
405 }
406 
exfat_conv_volume_label(struct exfat_dentry * vol_entry)407 char *exfat_conv_volume_label(struct exfat_dentry *vol_entry)
408 {
409 	char *volume_label;
410 	__le16 disk_label[VOLUME_LABEL_MAX_LEN];
411 
412 	volume_label = malloc(VOLUME_LABEL_BUFFER_SIZE);
413 	if (!volume_label)
414 		return NULL;
415 
416 	memcpy(disk_label, vol_entry->vol_label, sizeof(disk_label));
417 	memset(volume_label, 0, VOLUME_LABEL_BUFFER_SIZE);
418 	if (exfat_utf16_dec(disk_label, vol_entry->vol_char_cnt*2,
419 		volume_label, VOLUME_LABEL_BUFFER_SIZE) < 0) {
420 		exfat_err("failed to decode volume label\n");
421 		free(volume_label);
422 		return NULL;
423 	}
424 
425 	return volume_label;
426 }
427 
exfat_read_volume_label(struct exfat * exfat)428 int exfat_read_volume_label(struct exfat *exfat)
429 {
430 	struct exfat_dentry *dentry;
431 	int err;
432 	__le16 disk_label[VOLUME_LABEL_MAX_LEN];
433 	struct exfat_lookup_filter filter = {
434 		.in.type = EXFAT_VOLUME,
435 		.in.dentry_count = 0,
436 		.in.filter = NULL,
437 	};
438 
439 	err = exfat_lookup_dentry_set(exfat, exfat->root, &filter);
440 	if (err)
441 		return err;
442 
443 	dentry = filter.out.dentry_set;
444 
445 	if (dentry->vol_char_cnt == 0)
446 		goto out;
447 
448 	if (dentry->vol_char_cnt > VOLUME_LABEL_MAX_LEN) {
449 		exfat_err("too long label. %d\n", dentry->vol_char_cnt);
450 		err = -EINVAL;
451 		goto out;
452 	}
453 
454 	memcpy(disk_label, dentry->vol_label, sizeof(disk_label));
455 	if (exfat_utf16_dec(disk_label, dentry->vol_char_cnt*2,
456 		exfat->volume_label, sizeof(exfat->volume_label)) < 0) {
457 		exfat_err("failed to decode volume label\n");
458 		err = -EINVAL;
459 		goto out;
460 	}
461 
462 	exfat_info("label: %s\n", exfat->volume_label);
463 out:
464 	free(filter.out.dentry_set);
465 	return err;
466 }
467 
exfat_set_volume_label(struct exfat * exfat,char * label_input)468 int exfat_set_volume_label(struct exfat *exfat, char *label_input)
469 {
470 	struct exfat_dentry *pvol;
471 	struct exfat_dentry_loc loc;
472 	__u16 volume_label[VOLUME_LABEL_MAX_LEN];
473 	int volume_label_len, dcount, err;
474 
475 	struct exfat_lookup_filter filter = {
476 		.in.type = EXFAT_VOLUME,
477 		.in.dentry_count = 1,
478 		.in.filter = NULL,
479 	};
480 
481 	err = exfat_lookup_dentry_set(exfat, exfat->root, &filter);
482 	if (!err) {
483 		pvol = filter.out.dentry_set;
484 		dcount = filter.out.dentry_count;
485 		memset(pvol->vol_label, 0, sizeof(pvol->vol_label));
486 	} else {
487 		pvol = calloc(1, sizeof(struct exfat_dentry));
488 		if (!pvol)
489 			return -ENOMEM;
490 
491 		dcount = 1;
492 		pvol->type = EXFAT_VOLUME;
493 	}
494 
495 	volume_label_len = exfat_utf16_enc(label_input,
496 			volume_label, sizeof(volume_label));
497 	if (volume_label_len < 0) {
498 		exfat_err("failed to encode volume label\n");
499 		err = -1;
500 		goto out;
501 	}
502 
503 	pvol->vol_char_cnt = volume_label_len/2;
504 	err = exfat_check_name(volume_label, pvol->vol_char_cnt);
505 	if (err != pvol->vol_char_cnt) {
506 		exfat_err("volume label contain invalid character(%c)\n",
507 				le16_to_cpu(label_input[err]));
508 		err = -1;
509 		goto out;
510 	}
511 
512 	memcpy(pvol->vol_label, volume_label, volume_label_len);
513 
514 	loc.parent = exfat->root;
515 	loc.file_offset = filter.out.file_offset;
516 	loc.dev_offset = filter.out.dev_offset;
517 	err = exfat_add_dentry_set(exfat, &loc, pvol, dcount, false);
518 	exfat_info("new label: %s\n", label_input);
519 
520 out:
521 	free(pvol);
522 
523 	return err;
524 }
525 
print_guid(const char * msg,const __u8 * guid)526 static inline void print_guid(const char *msg, const __u8 *guid)
527 {
528 	exfat_info("%s: %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
529 			msg,
530 			guid[0], guid[1], guid[2], guid[3],
531 			guid[4], guid[5], guid[5], guid[7],
532 			guid[8], guid[9], guid[10], guid[11],
533 			guid[12], guid[13], guid[14], guid[15]);
534 }
535 
set_guid(__u8 * guid,const char * input)536 static int set_guid(__u8 *guid, const char *input)
537 {
538 	int i, j, zero_len = 0;
539 	int len = strlen(input);
540 
541 	if (len != EXFAT_GUID_LEN * 2 && len != EXFAT_GUID_LEN * 2 + 4) {
542 		exfat_err("invalid format for volume guid\n");
543 		return -EINVAL;
544 	}
545 
546 	for (i = 0, j = 0; i < len; i++) {
547 		unsigned char ch = input[i];
548 
549 		if (ch >= '0' && ch <= '9')
550 			ch -= '0';
551 		else if (ch >= 'a' && ch <= 'f')
552 			ch -= 'a' - 0xA;
553 		else if (ch >= 'A' && ch <= 'F')
554 			ch -= 'A' - 0xA;
555 		else if (ch == '-' && len == EXFAT_GUID_LEN * 2 + 4 &&
556 			 (i == 8 || i == 13 || i == 18 || i == 23))
557 			continue;
558 		else {
559 			exfat_err("invalid character '%c' for volume GUID\n", ch);
560 			return -EINVAL;
561 		}
562 
563 		if (j & 1)
564 			guid[j >> 1] |= ch;
565 		else
566 			guid[j >> 1] = ch << 4;
567 
568 		j++;
569 
570 		if (ch == 0)
571 			zero_len++;
572 	}
573 
574 	if (zero_len == EXFAT_GUID_LEN * 2) {
575 		exfat_err("%s is invalid for volume GUID\n", input);
576 		return -EINVAL;
577 	}
578 
579 	return 0;
580 }
581 
exfat_read_volume_guid(struct exfat * exfat)582 int exfat_read_volume_guid(struct exfat *exfat)
583 {
584 	int err;
585 	uint16_t checksum = 0;
586 	struct exfat_dentry *dentry;
587 	struct exfat_lookup_filter filter = {
588 		.in.type = EXFAT_GUID,
589 		.in.dentry_count = 1,
590 		.in.filter = NULL,
591 	};
592 
593 	err = exfat_lookup_dentry_set(exfat, exfat->root, &filter);
594 	if (err)
595 		return err;
596 
597 	dentry = filter.out.dentry_set;
598 	exfat_calc_dentry_checksum(dentry, &checksum, true);
599 
600 	if (cpu_to_le16(checksum) == dentry->dentry.guid.checksum)
601 		print_guid("GUID", dentry->dentry.guid.guid);
602 	else
603 		exfat_info("GUID is corrupted, please delete it or set a new one\n");
604 
605 	free(dentry);
606 
607 	return err;
608 }
609 
__exfat_set_volume_guid(struct exfat_dentry * dentry,const char * guid)610 int __exfat_set_volume_guid(struct exfat_dentry *dentry, const char *guid)
611 {
612 	int err;
613 	uint16_t checksum = 0;
614 
615 	memset(dentry, 0, sizeof(*dentry));
616 	dentry->type = EXFAT_GUID;
617 
618 	err = set_guid(dentry->dentry.guid.guid, guid);
619 	if (err)
620 		return err;
621 
622 	exfat_calc_dentry_checksum(dentry, &checksum, true);
623 	dentry->dentry.guid.checksum = cpu_to_le16(checksum);
624 
625 	return 0;
626 }
627 
628 /*
629  * Create/Update/Delete GUID dentry in root directory
630  *
631  * create/update GUID if @guid is not NULL.
632  * delete GUID if @guid is NULL.
633  */
exfat_set_volume_guid(struct exfat * exfat,const char * guid)634 int exfat_set_volume_guid(struct exfat *exfat, const char *guid)
635 {
636 	struct exfat_dentry *dentry;
637 	struct exfat_dentry_loc loc;
638 	int err;
639 
640 	struct exfat_lookup_filter filter = {
641 		.in.type = EXFAT_GUID,
642 		.in.dentry_count = 1,
643 		.in.filter = NULL,
644 	};
645 
646 	err = exfat_lookup_dentry_set(exfat, exfat->root, &filter);
647 	if (!err) {
648 		/* GUID entry is found */
649 		dentry = filter.out.dentry_set;
650 	} else {
651 		/* no GUID to delete */
652 		if (guid == NULL)
653 			return 0;
654 
655 		dentry = calloc(1, sizeof(*dentry));
656 		if (!dentry)
657 			return -ENOMEM;
658 	}
659 
660 	if (guid) {
661 		/* Set GUID */
662 		err = __exfat_set_volume_guid(dentry, guid);
663 		if (err)
664 			goto out;
665 	} else {
666 		/* Delete GUID */
667 		dentry->type &= ~EXFAT_INVAL;
668 	}
669 
670 	loc.parent = exfat->root;
671 	loc.file_offset = filter.out.file_offset;
672 	loc.dev_offset = filter.out.dev_offset;
673 	err = exfat_add_dentry_set(exfat, &loc, dentry, 1, false);
674 	if (!err) {
675 		if (guid)
676 			print_guid("new GUID", dentry->dentry.guid.guid);
677 		else
678 			exfat_info("GUID is deleted\n");
679 	}
680 
681 out:
682 	free(dentry);
683 
684 	return err;
685 }
686 
exfat_read_sector(struct exfat_blk_dev * bd,void * buf,unsigned int sec_off)687 int exfat_read_sector(struct exfat_blk_dev *bd, void *buf, unsigned int sec_off)
688 {
689 	int ret;
690 	unsigned long long offset =
691 		(unsigned long long)sec_off * bd->sector_size;
692 
693 	ret = pread(bd->dev_fd, buf, bd->sector_size, offset);
694 	if (ret < 0) {
695 		exfat_err("read failed, sec_off : %u\n", sec_off);
696 		return -1;
697 	}
698 	return 0;
699 }
700 
exfat_write_sector(struct exfat_blk_dev * bd,void * buf,unsigned int sec_off)701 int exfat_write_sector(struct exfat_blk_dev *bd, void *buf,
702 		unsigned int sec_off)
703 {
704 	int bytes;
705 	unsigned long long offset =
706 		(unsigned long long)sec_off * bd->sector_size;
707 
708 	bytes = pwrite(bd->dev_fd, buf, bd->sector_size, offset);
709 	if (bytes != (int)bd->sector_size) {
710 		exfat_err("write failed, sec_off : %u, bytes : %d\n", sec_off,
711 			bytes);
712 		return -1;
713 	}
714 	return 0;
715 }
716 
exfat_write_checksum_sector(struct exfat_blk_dev * bd,unsigned int checksum,bool is_backup)717 int exfat_write_checksum_sector(struct exfat_blk_dev *bd,
718 		unsigned int checksum, bool is_backup)
719 {
720 	__le32 *checksum_buf;
721 	int ret = 0;
722 	unsigned int i;
723 	unsigned int sec_idx = CHECKSUM_SEC_IDX;
724 
725 	checksum_buf = malloc(bd->sector_size);
726 	if (!checksum_buf)
727 		return -1;
728 
729 	if (is_backup)
730 		sec_idx += BACKUP_BOOT_SEC_IDX;
731 
732 	for (i = 0; i < bd->sector_size / sizeof(int); i++)
733 		checksum_buf[i] = cpu_to_le32(checksum);
734 
735 	ret = exfat_write_sector(bd, checksum_buf, sec_idx);
736 	if (ret) {
737 		exfat_err("checksum sector write failed\n");
738 		goto free;
739 	}
740 
741 free:
742 	free(checksum_buf);
743 	return ret;
744 }
745 
exfat_show_volume_serial(int fd)746 int exfat_show_volume_serial(int fd)
747 {
748 	struct pbr *ppbr;
749 	int ret;
750 
751 	ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
752 	if (!ppbr) {
753 		exfat_err("Cannot allocate pbr: out of memory\n");
754 		return -1;
755 	}
756 
757 	/* read main boot sector */
758 	ret = exfat_read(fd, (char *)ppbr, EXFAT_MAX_SECTOR_SIZE, 0);
759 	if (ret < 0) {
760 		exfat_err("main boot sector read failed\n");
761 		ret = -1;
762 		goto free_ppbr;
763 	}
764 
765 	if (memcmp(ppbr->bpb.oem_name, "EXFAT   ", 8) != 0) {
766 		exfat_err("Bad fs_name in boot sector, which does not describe a valid exfat filesystem\n");
767 		ret = -1;
768 		goto free_ppbr;
769 	}
770 
771 	exfat_info("volume serial : 0x%x\n", le32_to_cpu(ppbr->bsx.vol_serial));
772 
773 free_ppbr:
774 	free(ppbr);
775 	return ret;
776 }
777 
exfat_update_boot_checksum(struct exfat_blk_dev * bd,bool is_backup)778 static int exfat_update_boot_checksum(struct exfat_blk_dev *bd, bool is_backup)
779 {
780 	unsigned int checksum = 0;
781 	int ret, sec_idx, backup_sec_idx = 0;
782 	unsigned char *buf;
783 
784 	buf = malloc(bd->sector_size);
785 	if (!buf) {
786 		exfat_err("Cannot allocate pbr: out of memory\n");
787 		return -1;
788 	}
789 
790 	if (is_backup)
791 		backup_sec_idx = BACKUP_BOOT_SEC_IDX;
792 
793 	for (sec_idx = BOOT_SEC_IDX; sec_idx < CHECKSUM_SEC_IDX; sec_idx++) {
794 		bool is_boot_sec = false;
795 
796 		ret = exfat_read_sector(bd, buf, sec_idx + backup_sec_idx);
797 		if (ret < 0) {
798 			exfat_err("sector(%d) read failed\n", sec_idx);
799 			ret = -1;
800 			goto free_buf;
801 		}
802 
803 		if (sec_idx == BOOT_SEC_IDX)
804 			is_boot_sec = true;
805 
806 		boot_calc_checksum(buf, bd->sector_size, is_boot_sec,
807 			&checksum);
808 	}
809 
810 	ret = exfat_write_checksum_sector(bd, checksum, is_backup);
811 
812 free_buf:
813 	free(buf);
814 
815 	return ret;
816 }
817 
exfat_set_volume_serial(struct exfat_blk_dev * bd,struct exfat_user_input * ui)818 int exfat_set_volume_serial(struct exfat_blk_dev *bd,
819 		struct exfat_user_input *ui)
820 {
821 	int ret;
822 	struct pbr *ppbr;
823 
824 	ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
825 	if (!ppbr) {
826 		exfat_err("Cannot allocate pbr: out of memory\n");
827 		return -1;
828 	}
829 
830 	/* read main boot sector */
831 	ret = exfat_read(bd->dev_fd, (char *)ppbr, EXFAT_MAX_SECTOR_SIZE,
832 			BOOT_SEC_IDX);
833 	if (ret < 0) {
834 		exfat_err("main boot sector read failed\n");
835 		ret = -1;
836 		goto free_ppbr;
837 	}
838 
839 	if (memcmp(ppbr->bpb.oem_name, "EXFAT   ", 8) != 0) {
840 		exfat_err("Bad fs_name in boot sector, which does not describe a valid exfat filesystem\n");
841 		ret = -1;
842 		goto free_ppbr;
843 	}
844 
845 	bd->sector_size = 1 << ppbr->bsx.sect_size_bits;
846 	ppbr->bsx.vol_serial = cpu_to_le32(ui->volume_serial);
847 
848 	/* update main boot sector */
849 	ret = exfat_write_sector(bd, (char *)ppbr, BOOT_SEC_IDX);
850 	if (ret < 0) {
851 		exfat_err("main boot sector write failed\n");
852 		ret = -1;
853 		goto free_ppbr;
854 	}
855 
856 	/* update backup boot sector */
857 	ret = exfat_write_sector(bd, (char *)ppbr, BACKUP_BOOT_SEC_IDX);
858 	if (ret < 0) {
859 		exfat_err("backup boot sector write failed\n");
860 		ret = -1;
861 		goto free_ppbr;
862 	}
863 
864 	ret = exfat_update_boot_checksum(bd, 0);
865 	if (ret < 0) {
866 		exfat_err("main checksum update failed\n");
867 		goto free_ppbr;
868 	}
869 
870 	ret = exfat_update_boot_checksum(bd, 1);
871 	if (ret < 0)
872 		exfat_err("backup checksum update failed\n");
873 free_ppbr:
874 	free(ppbr);
875 
876 	exfat_info("New volume serial : 0x%x\n", ui->volume_serial);
877 
878 	return ret;
879 }
880 
exfat_clus_to_blk_dev_off(struct exfat_blk_dev * bd,unsigned int clu_off_sectnr,unsigned int clu)881 unsigned int exfat_clus_to_blk_dev_off(struct exfat_blk_dev *bd,
882 		unsigned int clu_off_sectnr, unsigned int clu)
883 {
884 	return clu_off_sectnr * bd->sector_size +
885 		(clu - EXFAT_RESERVED_CLUSTERS) * bd->cluster_size;
886 }
887 
exfat_get_next_clus(struct exfat * exfat,clus_t clus,clus_t * next)888 int exfat_get_next_clus(struct exfat *exfat, clus_t clus, clus_t *next)
889 {
890 	off_t offset;
891 
892 	*next = EXFAT_EOF_CLUSTER;
893 
894 	if (!exfat_heap_clus(exfat, clus))
895 		return -EINVAL;
896 
897 	offset = (off_t)le32_to_cpu(exfat->bs->bsx.fat_offset) <<
898 				exfat->bs->bsx.sect_size_bits;
899 	offset += sizeof(clus_t) * clus;
900 
901 	if (exfat_read(exfat->blk_dev->dev_fd, next, sizeof(*next), offset)
902 			!= sizeof(*next))
903 		return -EIO;
904 	*next = le32_to_cpu(*next);
905 	return 0;
906 }
907 
exfat_get_inode_next_clus(struct exfat * exfat,struct exfat_inode * node,clus_t clus,clus_t * next)908 int exfat_get_inode_next_clus(struct exfat *exfat, struct exfat_inode *node,
909 			      clus_t clus, clus_t *next)
910 {
911 	*next = EXFAT_EOF_CLUSTER;
912 
913 	if (node->is_contiguous) {
914 		if (!exfat_heap_clus(exfat, clus))
915 			return -EINVAL;
916 		*next = clus + 1;
917 		return 0;
918 	}
919 
920 	return exfat_get_next_clus(exfat, clus, next);
921 }
922 
exfat_set_fat(struct exfat * exfat,clus_t clus,clus_t next_clus)923 int exfat_set_fat(struct exfat *exfat, clus_t clus, clus_t next_clus)
924 {
925 	off_t offset;
926 
927 	offset = le32_to_cpu(exfat->bs->bsx.fat_offset) <<
928 		exfat->bs->bsx.sect_size_bits;
929 	offset += sizeof(clus_t) * clus;
930 
931 	next_clus = cpu_to_le32(next_clus);
932 
933 	if (exfat_write(exfat->blk_dev->dev_fd, &next_clus, sizeof(next_clus),
934 			offset) != sizeof(next_clus))
935 		return -EIO;
936 	return 0;
937 }
938 
exfat_s2o(struct exfat * exfat,off_t sect)939 off_t exfat_s2o(struct exfat *exfat, off_t sect)
940 {
941 	return sect << exfat->bs->bsx.sect_size_bits;
942 }
943 
exfat_c2o(struct exfat * exfat,unsigned int clus)944 off_t exfat_c2o(struct exfat *exfat, unsigned int clus)
945 {
946 	assert(clus >= EXFAT_FIRST_CLUSTER);
947 
948 	return exfat_s2o(exfat, le32_to_cpu(exfat->bs->bsx.clu_offset) +
949 				((off_t)(clus - EXFAT_FIRST_CLUSTER) <<
950 				 exfat->bs->bsx.sect_per_clus_bits));
951 }
952 
exfat_o2c(struct exfat * exfat,off_t device_offset,unsigned int * clu,unsigned int * offset)953 int exfat_o2c(struct exfat *exfat, off_t device_offset,
954 	      unsigned int *clu, unsigned int *offset)
955 {
956 	off_t heap_offset;
957 
958 	heap_offset = exfat_s2o(exfat, le32_to_cpu(exfat->bs->bsx.clu_offset));
959 	if (device_offset < heap_offset)
960 		return -ERANGE;
961 
962 	*clu = (unsigned int)((device_offset - heap_offset) /
963 			      exfat->clus_size) + EXFAT_FIRST_CLUSTER;
964 	if (!exfat_heap_clus(exfat, *clu))
965 		return -ERANGE;
966 	*offset = (device_offset - heap_offset) % exfat->clus_size;
967 	return 0;
968 }
969 
exfat_heap_clus(struct exfat * exfat,clus_t clus)970 bool exfat_heap_clus(struct exfat *exfat, clus_t clus)
971 {
972 	return clus >= EXFAT_FIRST_CLUSTER &&
973 		(clus - EXFAT_FIRST_CLUSTER) < exfat->clus_count;
974 }
975 
exfat_root_clus_count(struct exfat * exfat)976 int exfat_root_clus_count(struct exfat *exfat)
977 {
978 	struct exfat_inode *node = exfat->root;
979 	clus_t clus, next;
980 	int clus_count = 0;
981 
982 	if (!exfat_heap_clus(exfat, node->first_clus))
983 		return -EIO;
984 
985 	clus = node->first_clus;
986 	do {
987 		if (exfat_bitmap_get(exfat->alloc_bitmap, clus))
988 			return -EINVAL;
989 
990 		exfat_bitmap_set(exfat->alloc_bitmap, clus);
991 
992 		if (exfat_get_inode_next_clus(exfat, node, clus, &next)) {
993 			exfat_err("ERROR: failed to read the fat entry of root");
994 			return -EIO;
995 		}
996 
997 		if (next != EXFAT_EOF_CLUSTER && !exfat_heap_clus(exfat, next))
998 			return -EINVAL;
999 
1000 		clus = next;
1001 		clus_count++;
1002 	} while (clus != EXFAT_EOF_CLUSTER);
1003 
1004 	node->size = clus_count * exfat->clus_size;
1005 	return 0;
1006 }
1007 
read_boot_sect(struct exfat_blk_dev * bdev,struct pbr ** bs)1008 int read_boot_sect(struct exfat_blk_dev *bdev, struct pbr **bs)
1009 {
1010 	struct pbr *pbr;
1011 	int err = 0;
1012 	unsigned int sect_size, clu_size;
1013 
1014 	pbr = malloc(sizeof(struct pbr));
1015 	if (!pbr) {
1016 		exfat_err("failed to allocate memory\n");
1017 		return -ENOMEM;
1018 	}
1019 
1020 	if (exfat_read(bdev->dev_fd, pbr, sizeof(*pbr), 0) !=
1021 	    (ssize_t)sizeof(*pbr)) {
1022 		exfat_err("failed to read a boot sector\n");
1023 		err = -EIO;
1024 		goto err;
1025 	}
1026 
1027 	err = -EINVAL;
1028 	if (memcmp(pbr->bpb.oem_name, "EXFAT   ", 8) != 0) {
1029 		exfat_err("failed to find exfat file system\n");
1030 		goto err;
1031 	}
1032 
1033 	sect_size = 1 << pbr->bsx.sect_size_bits;
1034 	clu_size = 1 << (pbr->bsx.sect_size_bits +
1035 			 pbr->bsx.sect_per_clus_bits);
1036 
1037 	if (sect_size < 512 || sect_size > 4 * KB) {
1038 		exfat_err("too small or big sector size: %d\n",
1039 			  sect_size);
1040 		goto err;
1041 	}
1042 
1043 	if (clu_size < sect_size || clu_size > 32 * MB) {
1044 		exfat_err("too small or big cluster size: %d\n",
1045 			  clu_size);
1046 		goto err;
1047 	}
1048 
1049 	*bs = pbr;
1050 	return 0;
1051 err:
1052 	free(pbr);
1053 	return err;
1054 }
1055 
exfat_parse_ulong(const char * s,unsigned long * out)1056 int exfat_parse_ulong(const char *s, unsigned long *out)
1057 {
1058 	char *endptr;
1059 
1060 	errno = 0;
1061 
1062 	*out = strtoul(s, &endptr, 0);
1063 
1064 	if (errno)
1065 		return -errno;
1066 
1067 	if (s == endptr || *endptr != '\0')
1068 		return -EINVAL;
1069 
1070 	return 0;
1071 }
1072 
check_bad_utf16_char(unsigned short w)1073 static inline int check_bad_utf16_char(unsigned short w)
1074 {
1075 	return (w < 0x0020) || (w == '*') || (w == '?') || (w == '<') ||
1076 		(w == '>') || (w == '|') || (w == '"') || (w == ':') ||
1077 		(w == '/') || (w == '\\');
1078 }
1079 
exfat_check_name(__le16 * utf16_name,int len)1080 int exfat_check_name(__le16 *utf16_name, int len)
1081 {
1082 	int i;
1083 
1084 	for (i = 0; i < len; i++) {
1085 		if (check_bad_utf16_char(le16_to_cpu(utf16_name[i])))
1086 			break;
1087 	}
1088 
1089 	return i;
1090 }
1091