• 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 
19 #include "exfat_ondisk.h"
20 #include "libexfat.h"
21 #include "version.h"
22 #include "exfat_fs.h"
23 #include "exfat_dir.h"
24 
25 unsigned int print_level  = EXFAT_INFO;
26 
exfat_bitmap_set_range(struct exfat * exfat,char * bitmap,clus_t start_clus,clus_t count)27 void exfat_bitmap_set_range(struct exfat *exfat, char *bitmap,
28 			    clus_t start_clus, clus_t count)
29 {
30 	clus_t clus;
31 
32 	if (!exfat_heap_clus(exfat, start_clus) ||
33 	    !exfat_heap_clus(exfat, start_clus + count - 1))
34 		return;
35 
36 	clus = start_clus;
37 	while (clus < start_clus + count) {
38 		exfat_bitmap_set(bitmap, clus);
39 		clus++;
40 	}
41 }
42 
exfat_bitmap_find_bit(struct exfat * exfat,char * bmap,clus_t start_clu,clus_t * next,int bit)43 static int exfat_bitmap_find_bit(struct exfat *exfat, char *bmap,
44 				 clus_t start_clu, clus_t *next,
45 				 int bit)
46 {
47 	clus_t last_clu;
48 
49 	last_clu = le32_to_cpu(exfat->bs->bsx.clu_count) +
50 		EXFAT_FIRST_CLUSTER;
51 	while (start_clu < last_clu) {
52 		if (!!exfat_bitmap_get(bmap, start_clu) == bit) {
53 			*next = start_clu;
54 			return 0;
55 		}
56 		start_clu++;
57 	}
58 	return 1;
59 }
60 
exfat_bitmap_find_zero(struct exfat * exfat,char * bmap,clus_t start_clu,clus_t * next)61 int exfat_bitmap_find_zero(struct exfat *exfat, char *bmap,
62 			   clus_t start_clu, clus_t *next)
63 {
64 	return exfat_bitmap_find_bit(exfat, bmap,
65 				     start_clu, next, 0);
66 }
67 
exfat_bitmap_find_one(struct exfat * exfat,char * bmap,clus_t start_clu,clus_t * next)68 int exfat_bitmap_find_one(struct exfat *exfat, char *bmap,
69 			  clus_t start_clu, clus_t *next)
70 {
71 	return exfat_bitmap_find_bit(exfat, bmap,
72 				     start_clu, next, 1);
73 }
74 
exfat_bad_char(wchar_t w)75 wchar_t exfat_bad_char(wchar_t w)
76 {
77 	return (w < 0x0020)
78 		|| (w == '*') || (w == '?') || (w == '<') || (w == '>')
79 		|| (w == '|') || (w == '"') || (w == ':') || (w == '/')
80 		|| (w == '\\');
81 }
82 
boot_calc_checksum(unsigned char * sector,unsigned short size,bool is_boot_sec,__le32 * checksum)83 void boot_calc_checksum(unsigned char *sector, unsigned short size,
84 		bool is_boot_sec, __le32 *checksum)
85 {
86 	unsigned int index;
87 
88 	if (is_boot_sec) {
89 		for (index = 0; index < size; index++) {
90 			if ((index == 106) || (index == 107) || (index == 112))
91 				continue;
92 			*checksum = ((*checksum & 1) ? 0x80000000 : 0) +
93 				(*checksum >> 1) + sector[index];
94 		}
95 	} else {
96 		for (index = 0; index < size; index++) {
97 			*checksum = ((*checksum & 1) ? 0x80000000 : 0) +
98 				(*checksum >> 1) + sector[index];
99 		}
100 	}
101 }
102 
show_version(void)103 void show_version(void)
104 {
105 	printf("exfatprogs version : %s\n", EXFAT_PROGS_VERSION);
106 }
107 
sector_size_bits(unsigned int size)108 static inline unsigned int sector_size_bits(unsigned int size)
109 {
110 	unsigned int bits = 8;
111 
112 	do {
113 		bits++;
114 		size >>= 1;
115 	} while (size > 256);
116 
117 	return bits;
118 }
119 
exfat_set_default_cluster_size(struct exfat_blk_dev * bd,struct exfat_user_input * ui)120 static void exfat_set_default_cluster_size(struct exfat_blk_dev *bd,
121 		struct exfat_user_input *ui)
122 {
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;
127 	else
128 		ui->cluster_size = 128 * KB;
129 }
130 
init_user_input(struct exfat_user_input * ui)131 void init_user_input(struct exfat_user_input *ui)
132 {
133 	memset(ui, 0, sizeof(struct exfat_user_input));
134 	ui->writeable = true;
135 	ui->quick = true;
136 }
137 
exfat_get_blk_dev_info(struct exfat_user_input * ui,struct exfat_blk_dev * bd)138 int exfat_get_blk_dev_info(struct exfat_user_input *ui,
139 		struct exfat_blk_dev *bd)
140 {
141 	int fd, ret = -1;
142 	off_t blk_dev_size;
143 	struct stat st;
144 	unsigned long long blk_dev_offset = 0;
145 
146 	fd = open(ui->dev_name, ui->writeable ? O_RDWR|O_EXCL : O_RDONLY);
147 	if (fd < 0) {
148 		exfat_err("open failed : %s, %s\n", ui->dev_name,
149 			strerror(errno));
150 		return -1;
151 	}
152 	blk_dev_size = lseek(fd, 0, SEEK_END);
153 	if (blk_dev_size <= 0) {
154 		exfat_err("invalid block device size(%s)\n",
155 			ui->dev_name);
156 		ret = blk_dev_size;
157 		close(fd);
158 		goto out;
159 	}
160 
161 	if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode)) {
162 		char pathname[sizeof("/sys/dev/block/4294967295:4294967295/start")];
163 		FILE *fp;
164 
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");
168 		if (fp != NULL) {
169 			if (fscanf(fp, "%llu", &blk_dev_offset) == 1) {
170 				/*
171 				 * Linux kernel always reports partition offset
172 				 * in 512-byte units, regardless of sector size
173 				 */
174 				blk_dev_offset <<= 9;
175 			}
176 			fclose(fp);
177 		}
178 	}
179 
180 	bd->dev_fd = fd;
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);
185 
186 	if (!ui->boundary_align)
187 		ui->boundary_align = DEFAULT_BOUNDARY_ALIGNMENT;
188 
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;
194 
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",
200 		bd->num_sectors);
201 	exfat_debug("Number of the clusters : %u\n",
202 		bd->num_clusters);
203 
204 	ret = 0;
205 	bd->dev_fd = fd;
206 out:
207 	return ret;
208 }
209 
exfat_read(int fd,void * buf,size_t size,off_t offset)210 ssize_t exfat_read(int fd, void *buf, size_t size, off_t offset)
211 {
212 	return pread(fd, buf, size, offset);
213 }
214 
exfat_write(int fd,void * buf,size_t size,off_t offset)215 ssize_t exfat_write(int fd, void *buf, size_t size, off_t offset)
216 {
217 	return pwrite(fd, buf, size, offset);
218 }
219 
exfat_utf16_len(const __le16 * str,size_t max_size)220 size_t exfat_utf16_len(const __le16 *str, size_t max_size)
221 {
222 	size_t i = 0;
223 
224 	while (le16_to_cpu(str[i]) && i < max_size)
225 		i++;
226 	return i;
227 }
228 
exfat_utf16_enc(const char * in_str,__u16 * out_str,size_t out_size)229 ssize_t exfat_utf16_enc(const char *in_str, __u16 *out_str, size_t out_size)
230 {
231 	size_t mbs_len, out_len, i;
232 	wchar_t *wcs;
233 
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");
238 		return -errno;
239 	}
240 
241 	wcs = calloc(mbs_len+1, sizeof(wchar_t));
242 	if (!wcs)
243 		return -ENOMEM;
244 
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");
249 		free(wcs);
250 		return -errno;
251 	}
252 
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");
258 			free(wcs);
259 			return -E2BIG;
260 		}
261 
262 		/* Encode code point above Plane0 as UTF-16 surrogate pair */
263 		if (wcs[i] >= 0x10000) {
264 			out_str[out_len++] =
265 			  cpu_to_le16(((wcs[i] - 0x10000) >> 10) + 0xD800);
266 			wcs[i] = ((wcs[i] - 0x10000) & 0x3FF) + 0xDC00;
267 		}
268 
269 		out_str[out_len++] = cpu_to_le16(wcs[i]);
270 	}
271 
272 	free(wcs);
273 	return 2*out_len;
274 }
275 
exfat_utf16_dec(const __u16 * in_str,size_t in_len,char * out_str,size_t out_size)276 ssize_t exfat_utf16_dec(const __u16 *in_str, size_t in_len,
277 			char *out_str, size_t out_size)
278 {
279 	size_t wcs_len, out_len, c_len, i;
280 	char c_str[MB_LEN_MAX];
281 	wchar_t *wcs;
282 	mbstate_t ps;
283 	wchar_t w;
284 
285 	wcs = calloc(in_len/2+1, sizeof(wchar_t));
286 	if (!wcs)
287 		return -ENOMEM;
288 
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]);
292 		/*
293 		 * If wchar_t can store code point above Plane0
294 		 * then unpack UTF-16 surrogate pair to code point
295 		 */
296 #if WCHAR_MAX >= 0x10FFFF
297 		if (wcs[wcs_len] >= 0xD800 && wcs[wcs_len] <= 0xDBFF &&
298 		    i+1 < in_len/2) {
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) +
303 					       (w - 0xDC00);
304 				i++;
305 			}
306 		}
307 #endif
308 	}
309 
310 	memset(&ps, 0, sizeof(ps));
311 
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);
315 		/*
316 		 * If character is non-representable in current locale then
317 		 * try to store it as Unicode replacement code point U+FFFD
318 		 */
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");
327 			free(wcs);
328 			return -errno;
329 		}
330 		if (out_len+c_len > out_size) {
331 			exfat_err("input string is too long\n");
332 			free(wcs);
333 			return -E2BIG;
334 		}
335 		memcpy(out_str+out_len, c_str, c_len);
336 		out_len += c_len;
337 	}
338 
339 	free(wcs);
340 
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");
344 		return -errno;
345 	}
346 
347 	return out_len-1;
348 }
349 
exfat_get_root_entry_offset(struct exfat_blk_dev * bd)350 off_t exfat_get_root_entry_offset(struct exfat_blk_dev *bd)
351 {
352 	struct pbr *bs;
353 	int nbytes;
354 	unsigned int cluster_size, sector_size;
355 	off_t root_clu_off;
356 
357 	bs = (struct pbr *)malloc(EXFAT_MAX_SECTOR_SIZE);
358 	if (!bs) {
359 		exfat_err("failed to allocate memory\n");
360 		return -ENOMEM;
361 	}
362 
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);
366 		free(bs);
367 		return -1;
368 	}
369 
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");
372 		free(bs);
373 		return -1;
374 	}
375 
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) *
380 		cluster_size;
381 	free(bs);
382 
383 	return root_clu_off;
384 }
385 
exfat_conv_volume_label(struct exfat_dentry * vol_entry)386 char *exfat_conv_volume_label(struct exfat_dentry *vol_entry)
387 {
388 	char *volume_label;
389 	__le16 disk_label[VOLUME_LABEL_MAX_LEN];
390 
391 	volume_label = malloc(VOLUME_LABEL_BUFFER_SIZE);
392 	if (!volume_label)
393 		return NULL;
394 
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");
400 		free(volume_label);
401 		return NULL;
402 	}
403 
404 	return volume_label;
405 }
406 
exfat_read_volume_label(struct exfat * exfat)407 int exfat_read_volume_label(struct exfat *exfat)
408 {
409 	struct exfat_dentry *dentry;
410 	int err;
411 	__le16 disk_label[VOLUME_LABEL_MAX_LEN];
412 	struct exfat_lookup_filter filter = {
413 		.in.type = EXFAT_VOLUME,
414 		.in.filter = NULL,
415 	};
416 
417 	err = exfat_lookup_dentry_set(exfat, exfat->root, &filter);
418 	if (err)
419 		return err;
420 
421 	dentry = filter.out.dentry_set;
422 
423 	if (dentry->vol_char_cnt == 0)
424 		goto out;
425 
426 	if (dentry->vol_char_cnt > VOLUME_LABEL_MAX_LEN) {
427 		exfat_err("too long label. %d\n", dentry->vol_char_cnt);
428 		err = -EINVAL;
429 		goto out;
430 	}
431 
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");
436 		err = -EINVAL;
437 		goto out;
438 	}
439 
440 	exfat_info("label: %s\n", exfat->volume_label);
441 out:
442 	free(filter.out.dentry_set);
443 	return err;
444 }
445 
exfat_set_volume_label(struct exfat * exfat,char * label_input)446 int exfat_set_volume_label(struct exfat *exfat, char *label_input)
447 {
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;
452 
453 	struct exfat_lookup_filter filter = {
454 		.in.type = EXFAT_VOLUME,
455 		.in.filter = NULL,
456 	};
457 
458 	err = exfat_lookup_dentry_set(exfat, exfat->root, &filter);
459 	if (!err) {
460 		pvol = filter.out.dentry_set;
461 		dcount = filter.out.dentry_count;
462 		memset(pvol->vol_label, 0, sizeof(pvol->vol_label));
463 	} else {
464 		pvol = calloc(sizeof(struct exfat_dentry), 1);
465 		if (!pvol)
466 			return -ENOMEM;
467 
468 		dcount = 1;
469 		pvol->type = EXFAT_VOLUME;
470 	}
471 
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");
476 		free(pvol);
477 		return -1;
478 	}
479 
480 	memcpy(pvol->vol_label, volume_label, volume_label_len);
481 	pvol->vol_char_cnt = volume_label_len/2;
482 
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);
488 
489 	free(pvol);
490 
491 	return err;
492 }
493 
exfat_read_sector(struct exfat_blk_dev * bd,void * buf,unsigned int sec_off)494 int exfat_read_sector(struct exfat_blk_dev *bd, void *buf, unsigned int sec_off)
495 {
496 	int ret;
497 	unsigned long long offset =
498 		(unsigned long long)sec_off * bd->sector_size;
499 
500 	ret = pread(bd->dev_fd, buf, bd->sector_size, offset);
501 	if (ret < 0) {
502 		exfat_err("read failed, sec_off : %u\n", sec_off);
503 		return -1;
504 	}
505 	return 0;
506 }
507 
exfat_write_sector(struct exfat_blk_dev * bd,void * buf,unsigned int sec_off)508 int exfat_write_sector(struct exfat_blk_dev *bd, void *buf,
509 		unsigned int sec_off)
510 {
511 	int bytes;
512 	unsigned long long offset =
513 		(unsigned long long)sec_off * bd->sector_size;
514 
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,
518 			bytes);
519 		return -1;
520 	}
521 	return 0;
522 }
523 
exfat_write_checksum_sector(struct exfat_blk_dev * bd,unsigned int checksum,bool is_backup)524 int exfat_write_checksum_sector(struct exfat_blk_dev *bd,
525 		unsigned int checksum, bool is_backup)
526 {
527 	__le32 *checksum_buf;
528 	int ret = 0;
529 	unsigned int i;
530 	unsigned int sec_idx = CHECKSUM_SEC_IDX;
531 
532 	checksum_buf = malloc(bd->sector_size);
533 	if (!checksum_buf)
534 		return -1;
535 
536 	if (is_backup)
537 		sec_idx += BACKUP_BOOT_SEC_IDX;
538 
539 	for (i = 0; i < bd->sector_size / sizeof(int); i++)
540 		checksum_buf[i] = cpu_to_le32(checksum);
541 
542 	ret = exfat_write_sector(bd, checksum_buf, sec_idx);
543 	if (ret) {
544 		exfat_err("checksum sector write failed\n");
545 		goto free;
546 	}
547 
548 free:
549 	free(checksum_buf);
550 	return ret;
551 }
552 
exfat_show_volume_serial(int fd)553 int exfat_show_volume_serial(int fd)
554 {
555 	struct pbr *ppbr;
556 	int ret;
557 
558 	ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
559 	if (!ppbr) {
560 		exfat_err("Cannot allocate pbr: out of memory\n");
561 		return -1;
562 	}
563 
564 	/* read main boot sector */
565 	ret = exfat_read(fd, (char *)ppbr, EXFAT_MAX_SECTOR_SIZE, 0);
566 	if (ret < 0) {
567 		exfat_err("main boot sector read failed\n");
568 		ret = -1;
569 		goto free_ppbr;
570 	}
571 
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");
574 		ret = -1;
575 		goto free_ppbr;
576 	}
577 
578 	exfat_info("volume serial : 0x%x\n", ppbr->bsx.vol_serial);
579 
580 free_ppbr:
581 	free(ppbr);
582 	return ret;
583 }
584 
exfat_update_boot_checksum(struct exfat_blk_dev * bd,bool is_backup)585 static int exfat_update_boot_checksum(struct exfat_blk_dev *bd, bool is_backup)
586 {
587 	unsigned int checksum = 0;
588 	int ret, sec_idx, backup_sec_idx = 0;
589 	unsigned char *buf;
590 
591 	buf = malloc(bd->sector_size);
592 	if (!buf) {
593 		exfat_err("Cannot allocate pbr: out of memory\n");
594 		return -1;
595 	}
596 
597 	if (is_backup)
598 		backup_sec_idx = BACKUP_BOOT_SEC_IDX;
599 
600 	for (sec_idx = BOOT_SEC_IDX; sec_idx < CHECKSUM_SEC_IDX; sec_idx++) {
601 		bool is_boot_sec = false;
602 
603 		ret = exfat_read_sector(bd, buf, sec_idx + backup_sec_idx);
604 		if (ret < 0) {
605 			exfat_err("sector(%d) read failed\n", sec_idx);
606 			ret = -1;
607 			goto free_buf;
608 		}
609 
610 		if (sec_idx == BOOT_SEC_IDX)
611 			is_boot_sec = true;
612 
613 		boot_calc_checksum(buf, bd->sector_size, is_boot_sec,
614 			&checksum);
615 	}
616 
617 	ret = exfat_write_checksum_sector(bd, checksum, is_backup);
618 
619 free_buf:
620 	free(buf);
621 
622 	return ret;
623 }
624 
exfat_set_volume_serial(struct exfat_blk_dev * bd,struct exfat_user_input * ui)625 int exfat_set_volume_serial(struct exfat_blk_dev *bd,
626 		struct exfat_user_input *ui)
627 {
628 	int ret;
629 	struct pbr *ppbr;
630 
631 	ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
632 	if (!ppbr) {
633 		exfat_err("Cannot allocate pbr: out of memory\n");
634 		return -1;
635 	}
636 
637 	/* read main boot sector */
638 	ret = exfat_read(bd->dev_fd, (char *)ppbr, EXFAT_MAX_SECTOR_SIZE,
639 			BOOT_SEC_IDX);
640 	if (ret < 0) {
641 		exfat_err("main boot sector read failed\n");
642 		ret = -1;
643 		goto free_ppbr;
644 	}
645 
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");
648 		ret = -1;
649 		goto free_ppbr;
650 	}
651 
652 	bd->sector_size = 1 << ppbr->bsx.sect_size_bits;
653 	ppbr->bsx.vol_serial = ui->volume_serial;
654 
655 	/* update main boot sector */
656 	ret = exfat_write_sector(bd, (char *)ppbr, BOOT_SEC_IDX);
657 	if (ret < 0) {
658 		exfat_err("main boot sector write failed\n");
659 		ret = -1;
660 		goto free_ppbr;
661 	}
662 
663 	/* update backup boot sector */
664 	ret = exfat_write_sector(bd, (char *)ppbr, BACKUP_BOOT_SEC_IDX);
665 	if (ret < 0) {
666 		exfat_err("backup boot sector write failed\n");
667 		ret = -1;
668 		goto free_ppbr;
669 	}
670 
671 	ret = exfat_update_boot_checksum(bd, 0);
672 	if (ret < 0) {
673 		exfat_err("main checksum update failed\n");
674 		goto free_ppbr;
675 	}
676 
677 	ret = exfat_update_boot_checksum(bd, 1);
678 	if (ret < 0)
679 		exfat_err("backup checksum update failed\n");
680 free_ppbr:
681 	free(ppbr);
682 
683 	exfat_info("New volume serial : 0x%x\n", ui->volume_serial);
684 
685 	return ret;
686 }
687 
exfat_clus_to_blk_dev_off(struct exfat_blk_dev * bd,unsigned int clu_off_sectnr,unsigned int clu)688 unsigned int exfat_clus_to_blk_dev_off(struct exfat_blk_dev *bd,
689 		unsigned int clu_off_sectnr, unsigned int clu)
690 {
691 	return clu_off_sectnr * bd->sector_size +
692 		(clu - EXFAT_RESERVED_CLUSTERS) * bd->cluster_size;
693 }
694 
exfat_get_next_clus(struct exfat * exfat,clus_t clus,clus_t * next)695 int exfat_get_next_clus(struct exfat *exfat, clus_t clus, clus_t *next)
696 {
697 	off_t offset;
698 
699 	*next = EXFAT_EOF_CLUSTER;
700 
701 	if (!exfat_heap_clus(exfat, clus))
702 		return -EINVAL;
703 
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;
707 
708 	if (exfat_read(exfat->blk_dev->dev_fd, next, sizeof(*next), offset)
709 			!= sizeof(*next))
710 		return -EIO;
711 	*next = le32_to_cpu(*next);
712 	return 0;
713 }
714 
exfat_get_inode_next_clus(struct exfat * exfat,struct exfat_inode * node,clus_t clus,clus_t * next)715 int exfat_get_inode_next_clus(struct exfat *exfat, struct exfat_inode *node,
716 			      clus_t clus, clus_t *next)
717 {
718 	*next = EXFAT_EOF_CLUSTER;
719 
720 	if (node->is_contiguous) {
721 		if (!exfat_heap_clus(exfat, clus))
722 			return -EINVAL;
723 		*next = clus + 1;
724 		return 0;
725 	}
726 
727 	return exfat_get_next_clus(exfat, clus, next);
728 }
729 
exfat_set_fat(struct exfat * exfat,clus_t clus,clus_t next_clus)730 int exfat_set_fat(struct exfat *exfat, clus_t clus, clus_t next_clus)
731 {
732 	off_t offset;
733 
734 	offset = le32_to_cpu(exfat->bs->bsx.fat_offset) <<
735 		exfat->bs->bsx.sect_size_bits;
736 	offset += sizeof(clus_t) * clus;
737 
738 	if (exfat_write(exfat->blk_dev->dev_fd, &next_clus, sizeof(next_clus),
739 			offset) != sizeof(next_clus))
740 		return -EIO;
741 	return 0;
742 }
743 
exfat_s2o(struct exfat * exfat,off_t sect)744 off_t exfat_s2o(struct exfat *exfat, off_t sect)
745 {
746 	return sect << exfat->bs->bsx.sect_size_bits;
747 }
748 
exfat_c2o(struct exfat * exfat,unsigned int clus)749 off_t exfat_c2o(struct exfat *exfat, unsigned int clus)
750 {
751 	if (clus < EXFAT_FIRST_CLUSTER)
752 		return ~0L;
753 
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));
757 }
758 
exfat_o2c(struct exfat * exfat,off_t device_offset,unsigned int * clu,unsigned int * offset)759 int exfat_o2c(struct exfat *exfat, off_t device_offset,
760 	      unsigned int *clu, unsigned int *offset)
761 {
762 	off_t heap_offset;
763 
764 	heap_offset = exfat_s2o(exfat, le32_to_cpu(exfat->bs->bsx.clu_offset));
765 	if (device_offset < heap_offset)
766 		return -ERANGE;
767 
768 	*clu = (unsigned int)((device_offset - heap_offset) /
769 			      exfat->clus_size) + EXFAT_FIRST_CLUSTER;
770 	if (!exfat_heap_clus(exfat, *clu))
771 		return -ERANGE;
772 	*offset = (device_offset - heap_offset) % exfat->clus_size;
773 	return 0;
774 }
775 
exfat_heap_clus(struct exfat * exfat,clus_t clus)776 bool exfat_heap_clus(struct exfat *exfat, clus_t clus)
777 {
778 	return clus >= EXFAT_FIRST_CLUSTER &&
779 		(clus - EXFAT_FIRST_CLUSTER) < exfat->clus_count;
780 }
781 
exfat_root_clus_count(struct exfat * exfat)782 int exfat_root_clus_count(struct exfat *exfat)
783 {
784 	struct exfat_inode *node = exfat->root;
785 	clus_t clus, next;
786 	int clus_count = 0;
787 
788 	if (!exfat_heap_clus(exfat, node->first_clus))
789 		return -EIO;
790 
791 	clus = node->first_clus;
792 	do {
793 		if (exfat_bitmap_get(exfat->alloc_bitmap, clus))
794 			return -EINVAL;
795 
796 		exfat_bitmap_set(exfat->alloc_bitmap, clus);
797 
798 		if (exfat_get_inode_next_clus(exfat, node, clus, &next)) {
799 			exfat_err("ERROR: failed to read the fat entry of root");
800 			return -EIO;
801 		}
802 
803 		if (next != EXFAT_EOF_CLUSTER && !exfat_heap_clus(exfat, next))
804 			return -EINVAL;
805 
806 		clus = next;
807 		clus_count++;
808 	} while (clus != EXFAT_EOF_CLUSTER);
809 
810 	node->size = clus_count * exfat->clus_size;
811 	return 0;
812 }
813 
read_boot_sect(struct exfat_blk_dev * bdev,struct pbr ** bs)814 int read_boot_sect(struct exfat_blk_dev *bdev, struct pbr **bs)
815 {
816 	struct pbr *pbr;
817 	int err = 0;
818 	unsigned int sect_size, clu_size;
819 
820 	pbr = malloc(sizeof(struct pbr));
821 
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");
825 		err = -EIO;
826 		goto err;
827 	}
828 
829 	err = -EINVAL;
830 	if (memcmp(pbr->bpb.oem_name, "EXFAT   ", 8) != 0) {
831 		exfat_err("failed to find exfat file system\n");
832 		goto err;
833 	}
834 
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);
838 
839 	if (sect_size < 512 || sect_size > 4 * KB) {
840 		exfat_err("too small or big sector size: %d\n",
841 			  sect_size);
842 		goto err;
843 	}
844 
845 	if (clu_size < sect_size || clu_size > 32 * MB) {
846 		exfat_err("too small or big cluster size: %d\n",
847 			  clu_size);
848 		goto err;
849 	}
850 
851 	*bs = pbr;
852 	return 0;
853 err:
854 	free(pbr);
855 	return err;
856 }
857