• 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 #ifndef _LIBEXFAT_H
7 
8 #include <stdbool.h>
9 #include <sys/types.h>
10 #include <wchar.h>
11 #include <limits.h>
12 
13 #define KB			(1024)
14 #define MB			(1024*1024)
15 #define GB			(1024UL*1024UL*1024UL)
16 
17 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
18 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
19 #define round_down(x, y) ((x) & ~__round_mask(x, y))
20 
21 #define MIN(a, b)	((a) < (b) ? (a) : (b))
22 #define MAX(a, b)	((a) > (b) ? (a) : (b))
23 
24 #define DIV_ROUND_UP(__i, __d)	(((__i) + (__d) - 1) / (__d))
25 
26 #define EXFAT_MIN_NUM_SEC_VOL		(2048)
27 #define EXFAT_MAX_NUM_SEC_VOL		((2 << 64) - 1)
28 
29 #define EXFAT_MAX_NUM_CLUSTER		(0xFFFFFFF5)
30 
31 #define DEFAULT_BOUNDARY_ALIGNMENT	(1024*1024)
32 
33 #define DEFAULT_SECTOR_SIZE	(512)
34 
35 #define VOLUME_LABEL_BUFFER_SIZE	(VOLUME_LABEL_MAX_LEN*MB_LEN_MAX+1)
36 
37 /* Upcase table macro */
38 #define EXFAT_UPCASE_TABLE_SIZE		(5836)
39 
40 /* Flags for tune.exfat and exfatlabel */
41 #define EXFAT_GET_VOLUME_LABEL		0x01
42 #define EXFAT_SET_VOLUME_LABEL		0x02
43 #define EXFAT_GET_VOLUME_SERIAL		0x03
44 #define EXFAT_SET_VOLUME_SERIAL		0x04
45 
46 #define EXFAT_MAX_SECTOR_SIZE		4096
47 
48 enum {
49 	BOOT_SEC_IDX = 0,
50 	EXBOOT_SEC_IDX,
51 	EXBOOT_SEC_NUM = 8,
52 	OEM_SEC_IDX,
53 	RESERVED_SEC_IDX,
54 	CHECKSUM_SEC_IDX,
55 	BACKUP_BOOT_SEC_IDX,
56 };
57 
58 struct exfat_blk_dev {
59 	int dev_fd;
60 	unsigned long long offset;
61 	unsigned long long size;
62 	unsigned int sector_size;
63 	unsigned int sector_size_bits;
64 	unsigned long long num_sectors;
65 	unsigned int num_clusters;
66 	unsigned int cluster_size;
67 };
68 
69 struct exfat_user_input {
70 	char dev_name[255];
71 	bool writeable;
72 	unsigned int cluster_size;
73 	unsigned int sec_per_clu;
74 	unsigned int boundary_align;
75 	bool pack_bitmap;
76 	bool quick;
77 	__u16 volume_label[VOLUME_LABEL_MAX_LEN];
78 	int volume_label_len;
79 	unsigned int volume_serial;
80 };
81 
82 void show_version(void);
83 
84 void exfat_set_bit(struct exfat_blk_dev *bd, char *bitmap,
85 		unsigned int clu);
86 void exfat_clear_bit(struct exfat_blk_dev *bd, char *bitmap,
87 		unsigned int clu);
88 wchar_t exfat_bad_char(wchar_t w);
89 void boot_calc_checksum(unsigned char *sector, unsigned short size,
90 		bool is_boot_sec, __le32 *checksum);
91 void init_user_input(struct exfat_user_input *ui);
92 int exfat_get_blk_dev_info(struct exfat_user_input *ui,
93 		struct exfat_blk_dev *bd);
94 ssize_t exfat_read(int fd, void *buf, size_t size, off_t offset);
95 ssize_t exfat_write(int fd, void *buf, size_t size, off_t offset);
96 
97 size_t exfat_utf16_len(const __le16 *str, size_t max_size);
98 ssize_t exfat_utf16_enc(const char *in_str, __u16 *out_str, size_t out_size);
99 ssize_t exfat_utf16_dec(const __u16 *in_str, size_t in_len,
100 			char *out_str, size_t out_size);
101 off_t exfat_get_root_entry_offset(struct exfat_blk_dev *bd);
102 int exfat_show_volume_label(struct exfat_blk_dev *bd, off_t root_clu_off);
103 int exfat_set_volume_label(struct exfat_blk_dev *bd,
104 		char *label_input, off_t root_clu_off);
105 int exfat_read_sector(struct exfat_blk_dev *bd, void *buf,
106 		unsigned int sec_off);
107 int exfat_write_sector(struct exfat_blk_dev *bd, void *buf,
108 		unsigned int sec_off);
109 int exfat_write_checksum_sector(struct exfat_blk_dev *bd,
110 		unsigned int checksum, bool is_backup);
111 char *exfat_conv_volume_label(struct exfat_dentry *vol_entry);
112 int exfat_show_volume_serial(int fd);
113 int exfat_set_volume_serial(struct exfat_blk_dev *bd,
114 		struct exfat_user_input *ui);
115 unsigned int exfat_clus_to_blk_dev_off(struct exfat_blk_dev *bd,
116 		unsigned int clu_off, unsigned int clu);
117 
118 
119 /*
120  * Exfat Print
121  */
122 
123 extern unsigned int print_level;
124 
125 #define EXFAT_ERROR	(1)
126 #define EXFAT_INFO	(2)
127 #define EXFAT_DEBUG	(3)
128 
129 #define exfat_msg(level, dir, fmt, ...)					\
130 	do {								\
131 		if (print_level >= level) {				\
132 			fprintf(dir, fmt, ##__VA_ARGS__);		\
133 		}							\
134 	} while (0)							\
135 
136 #define exfat_err(fmt, ...)	exfat_msg(EXFAT_ERROR, stderr,		\
137 					fmt, ##__VA_ARGS__)
138 #define exfat_info(fmt, ...)	exfat_msg(EXFAT_INFO, stdout,		\
139 					fmt, ##__VA_ARGS__)
140 #define exfat_debug(fmt, ...)	exfat_msg(EXFAT_DEBUG, stdout,		\
141 					"[%s:%4d] " fmt, __func__, 	\
142 					__LINE__, ##__VA_ARGS__)
143 
144 #endif /* !_LIBEXFAT_H */
145