]> git.sven.stormbind.net Git - sven/exfatprogs.git/blob - include/libexfat.h
c21dc06756559ae5fa93c558fcae81e3ab8396d6
[sven/exfatprogs.git] / include / libexfat.h
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 tabel 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 enum {
47         BOOT_SEC_IDX = 0,
48         EXBOOT_SEC_IDX,
49         EXBOOT_SEC_NUM = 8,
50         OEM_SEC_IDX,
51         RESERVED_SEC_IDX,
52         CHECKSUM_SEC_IDX,
53         BACKUP_BOOT_SEC_IDX,
54 };
55
56 struct exfat_blk_dev {
57         int dev_fd;
58         unsigned long long size;
59         unsigned int sector_size;
60         unsigned int sector_size_bits;
61         unsigned long long num_sectors;
62         unsigned int num_clusters;
63         unsigned int cluster_size;
64 };
65
66 struct exfat_user_input {
67         char dev_name[255];
68         bool writeable;
69         unsigned int cluster_size;
70         unsigned int sec_per_clu;
71         unsigned int boundary_align;
72         bool quick;
73         __u16 volume_label[VOLUME_LABEL_MAX_LEN];
74         int volume_label_len;
75         unsigned int volume_serial;
76 };
77
78 void show_version(void);
79
80 void exfat_set_bit(struct exfat_blk_dev *bd, char *bitmap,
81                 unsigned int clu);
82 void exfat_clear_bit(struct exfat_blk_dev *bd, char *bitmap,
83                 unsigned int clu);
84 wchar_t exfat_bad_char(wchar_t w);
85 void boot_calc_checksum(unsigned char *sector, unsigned short size,
86                 bool is_boot_sec, __le32 *checksum);
87 void init_user_input(struct exfat_user_input *ui);
88 int exfat_get_blk_dev_info(struct exfat_user_input *ui,
89                 struct exfat_blk_dev *bd);
90 ssize_t exfat_read(int fd, void *buf, size_t size, off_t offset);
91 ssize_t exfat_write(int fd, void *buf, size_t size, off_t offset);
92
93 size_t exfat_utf16_len(const __le16 *str, size_t max_size);
94 ssize_t exfat_utf16_enc(const char *in_str, __u16 *out_str, size_t out_size);
95 ssize_t exfat_utf16_dec(const __u16 *in_str, size_t in_len,
96                         char *out_str, size_t out_size);
97 off_t exfat_get_root_entry_offset(struct exfat_blk_dev *bd);
98 int exfat_show_volume_label(struct exfat_blk_dev *bd, off_t root_clu_off);
99 int exfat_set_volume_label(struct exfat_blk_dev *bd,
100                 char *label_input, off_t root_clu_off);
101 int exfat_read_sector(struct exfat_blk_dev *bd, void *buf,
102                 unsigned int sec_off);
103 int exfat_write_sector(struct exfat_blk_dev *bd, void *buf,
104                 unsigned int sec_off);
105 int exfat_write_checksum_sector(struct exfat_blk_dev *bd,
106                 unsigned int checksum, bool is_backup);
107 char *exfat_conv_volume_serial(struct exfat_dentry *vol_entry);
108 int exfat_show_volume_serial(struct exfat_blk_dev *bd,
109                 struct exfat_user_input *ui);
110 int exfat_set_volume_serial(struct exfat_blk_dev *bd,
111                 struct exfat_user_input *ui);
112 unsigned int exfat_clus_to_blk_dev_off(struct exfat_blk_dev *bd,
113                 unsigned int clu_off, unsigned int clu);
114
115
116 /*
117  * Exfat Print
118  */
119
120 extern unsigned int print_level;
121
122 #define EXFAT_ERROR     (1)
123 #define EXFAT_INFO      (2)
124 #define EXFAT_DEBUG     (3)
125
126 #define exfat_msg(level, dir, fmt, ...)                                 \
127         do {                                                            \
128                 if (print_level >= level) {                             \
129                         fprintf(dir, fmt, ##__VA_ARGS__);               \
130                 }                                                       \
131         } while (0)                                                     \
132
133 #define exfat_err(fmt, ...)     exfat_msg(EXFAT_ERROR, stderr,          \
134                                         fmt, ##__VA_ARGS__)
135 #define exfat_info(fmt, ...)    exfat_msg(EXFAT_INFO, stdout,           \
136                                         fmt, ##__VA_ARGS__)
137 #define exfat_debug(fmt, ...)   exfat_msg(EXFAT_DEBUG, stdout,          \
138                                         "[%s:%4d] " fmt, __func__,      \
139                                         __LINE__, ##__VA_ARGS__)
140
141 #endif /* !_LIBEXFAT_H */