1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) 2019 Namjae Jeon <linkinjeon@kernel.org>
14 #define MB (1024*1024)
15 #define GB (1024UL*1024UL*1024UL)
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))
21 #define MIN(a, b) ((a) < (b) ? (a) : (b))
22 #define MAX(a, b) ((a) > (b) ? (a) : (b))
24 #define DIV_ROUND_UP(__i, __d) (((__i) + (__d) - 1) / (__d))
26 #define EXFAT_MIN_NUM_SEC_VOL (2048)
27 #define EXFAT_MAX_NUM_SEC_VOL ((2 << 64) - 1)
29 #define EXFAT_MAX_NUM_CLUSTER (0xFFFFFFF5)
31 #define DEFAULT_BOUNDARY_ALIGNMENT (1024*1024)
33 #define DEFAULT_SECTOR_SIZE (512)
35 #define VOLUME_LABEL_BUFFER_SIZE (VOLUME_LABEL_MAX_LEN*MB_LEN_MAX+1)
37 /* Upcase table macro */
38 #define EXFAT_UPCASE_TABLE_SIZE (5836)
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
46 #define EXFAT_MAX_SECTOR_SIZE 4096
58 struct exfat_blk_dev {
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;
69 struct exfat_user_input {
72 unsigned int cluster_size;
73 unsigned int sec_per_clu;
74 unsigned int boundary_align;
77 __u16 volume_label[VOLUME_LABEL_MAX_LEN];
79 unsigned int volume_serial;
82 void show_version(void);
84 void exfat_set_bit(struct exfat_blk_dev *bd, char *bitmap,
86 void exfat_clear_bit(struct exfat_blk_dev *bd, char *bitmap,
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);
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);
123 extern unsigned int print_level;
125 #define EXFAT_ERROR (1)
126 #define EXFAT_INFO (2)
127 #define EXFAT_DEBUG (3)
129 #define exfat_msg(level, dir, fmt, ...) \
131 if (print_level >= level) { \
132 fprintf(dir, fmt, ##__VA_ARGS__); \
136 #define exfat_err(fmt, ...) exfat_msg(EXFAT_ERROR, stderr, \
138 #define exfat_info(fmt, ...) exfat_msg(EXFAT_INFO, stdout, \
140 #define exfat_debug(fmt, ...) exfat_msg(EXFAT_DEBUG, stdout, \
141 "[%s:%4d] " fmt, __func__, \
142 __LINE__, ##__VA_ARGS__)
144 #endif /* !_LIBEXFAT_H */