]> git.sven.stormbind.net Git - sven/exfatprogs.git/blob - include/exfat_ondisk.h
Add CVE ID to debian changelog
[sven/exfatprogs.git] / include / exfat_ondisk.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  Copyright (C) 2019 Namjae Jeon <linkinjeon@kernel.org>
4  */
5
6 #ifndef _EXFAT_H
7 #define _EXFAT_H
8
9 #include <stdint.h>
10 #include <linux/fs.h>
11
12 #ifdef HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15
16 #ifdef WORDS_BIGENDIAN
17 #define cpu_to_le16(x)  ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
18 #define cpu_to_le32(x)  \
19         ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >>  8) | \
20          (((x) & 0x0000ff00u) <<  8) | (((x) & 0x000000ffu) << 24))
21 #define cpu_to_le64(x)  (cpu_to_le32((uint64_t)(x)) << 32 | \
22                         cpu_to_le32((uint64_t)(x) >> 32))
23 #else
24 #define cpu_to_le16(x)  (x)
25 #define cpu_to_le32(x)  (x)
26 #define cpu_to_le64(x)  (x)
27 #endif
28
29 #define le64_to_cpu(x)  ((uint64_t)cpu_to_le64(x))
30 #define le32_to_cpu(x)  ((uint32_t)cpu_to_le32(x))
31 #define le16_to_cpu(x)  ((uint16_t)cpu_to_le16(x))
32
33 #define PBR_SIGNATURE           0xAA55
34
35 #define VOL_CLEAN               0x0000
36 #define VOL_DIRTY               0x0002
37
38 #define DENTRY_SIZE             32 /* directory entry size */
39 #define DENTRY_SIZE_BITS        5
40 /* exFAT allows 8388608(256MB) directory entries */
41 #define MAX_EXFAT_DENTRIES      8388608
42 #define MIN_FILE_DENTRIES       3
43 #define MAX_NAME_DENTRIES       17
44
45 /* dentry types */
46 #define MSDOS_DELETED           0xE5    /* deleted mark */
47 #define MSDOS_UNUSED            0x00    /* end of directory */
48
49 #define EXFAT_LAST              0x00    /* end of directory */
50 #define EXFAT_DELETE            ~(0x80)
51 #define IS_EXFAT_DELETED(x)     ((x) < 0x80) /* deleted file (0x01~0x7F) */
52 #define EXFAT_INVAL             0x80    /* invalid value */
53 #define EXFAT_BITMAP            0x81    /* allocation bitmap */
54 #define EXFAT_UPCASE            0x82    /* upcase table */
55 #define EXFAT_VOLUME            0x83    /* volume label */
56 #define EXFAT_FILE              0x85    /* file or dir */
57 #define EXFAT_GUID              0xA0
58 #define EXFAT_PADDING           0xA1
59 #define EXFAT_ACLTAB            0xA2
60 #define EXFAT_STREAM            0xC0    /* stream entry */
61 #define EXFAT_NAME              0xC1    /* file name entry */
62 #define EXFAT_ACL               0xC2    /* stream entry */
63
64 /* checksum types */
65 #define CS_DIR_ENTRY            0
66 #define CS_PBR_SECTOR           1
67 #define CS_DEFAULT              2
68
69 /* file attributes */
70 #define ATTR_READONLY           0x0001
71 #define ATTR_HIDDEN             0x0002
72 #define ATTR_SYSTEM             0x0004
73 #define ATTR_VOLUME             0x0008
74 #define ATTR_SUBDIR             0x0010
75 #define ATTR_ARCHIVE            0x0020
76 #define ATTR_EXTEND             (ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | \
77                                  ATTR_VOLUME) /* 0x000F */
78
79 #define ATTR_EXTEND_MASK        (ATTR_EXTEND | ATTR_SUBDIR | ATTR_ARCHIVE)
80 #define ATTR_RWMASK             (ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME | \
81                                  ATTR_SUBDIR | ATTR_ARCHIVE)
82
83 #define ATTR_READONLY_LE        cpu_to_le16(0x0001)
84 #define ATTR_HIDDEN_LE          cpu_to_le16(0x0002)
85 #define ATTR_SYSTEM_LE          cpu_to_le16(0x0004)
86 #define ATTR_VOLUME_LE          cpu_to_le16(0x0008)
87 #define ATTR_SUBDIR_LE          cpu_to_le16(0x0010)
88 #define ATTR_ARCHIVE_LE         cpu_to_le16(0x0020)
89
90 /* stream flags */
91 #define EXFAT_SF_CONTIGUOUS             0x02
92
93 #define CLUSTER_32(x)                   ((unsigned int)((x) & 0xFFFFFFFFU))
94 #define EXFAT_EOF_CLUSTER               CLUSTER_32(~0)
95 #define EXFAT_BAD_CLUSTER               (0xFFFFFFF7U)
96 #define EXFAT_FREE_CLUSTER              (0)
97 #define EXFAT_FIRST_CLUSTER             (2)
98 #define EXFAT_RESERVED_CLUSTERS         (2)
99
100
101 /* EXFAT BIOS parameter block (64 bytes) */
102 struct bpb64 {
103         __u8 jmp_boot[3];
104         __u8 oem_name[8];
105         __u8 res_zero[53];
106 };
107
108 /* EXFAT EXTEND BIOS parameter block (56 bytes) */
109 struct bsx64 {
110         __le64 vol_offset;
111         __le64 vol_length;
112         __le32 fat_offset;
113         __le32 fat_length;
114         __le32 clu_offset;
115         __le32 clu_count;
116         __le32 root_cluster;
117         __le32 vol_serial;
118         __u8 fs_version[2];
119         __le16 vol_flags;
120         __u8 sect_size_bits;
121         __u8 sect_per_clus_bits;
122         __u8 num_fats;
123         __u8 phy_drv_no;
124         __u8 perc_in_use;
125         __u8 reserved2[7];
126 };
127
128 /* Common PBR[Partition Boot Record] (512 bytes) */
129 struct pbr {
130         struct bpb64 bpb;
131         struct bsx64 bsx;
132         __u8 boot_code[390];
133         __le16 signature;
134 };
135
136 #define VOLUME_LABEL_MAX_LEN    11
137 #define VOLUME_GUID_LEN         16
138 #define ENTRY_NAME_MAX          15
139
140 struct exfat_dentry {
141         __u8 type;
142         union {
143                 struct {
144                         __u8 character_count;
145                         __le16 volume_label[VOLUME_LABEL_MAX_LEN];
146                         __u8 reserved[8];
147                 } __attribute__((packed)) vol; /* file directory entry */
148
149                 struct {
150                         __u8 num_ext;
151                         __le16 checksum;
152                         __le16 attr;
153                         __le16 reserved1;
154                         __le16 create_time;
155                         __le16 create_date;
156                         __le16 modify_time;
157                         __le16 modify_date;
158                         __le16 access_time;
159                         __le16 access_date;
160                         __u8 create_time_ms;
161                         __u8 modify_time_ms;
162                         __u8 create_tz;
163                         __u8 modify_tz;
164                         __u8 access_tz;
165                         __u8 reserved2[7];
166                 } __attribute__((packed)) file; /* file directory entry */
167                 struct {
168                         __u8 flags;
169                         __u8 reserved1;
170                         __u8 name_len;
171                         __le16 name_hash;
172                         __le16 reserved2;
173                         __le64 valid_size;
174                         __le32 reserved3;
175                         __le32 start_clu;
176                         __le64 size;
177                 } __attribute__((packed)) stream; /* stream extension directory entry */
178                 struct {
179                         __u8 flags;
180                         __le16 unicode_0_14[15];
181                 } __attribute__((packed)) name; /* file name directory entry */
182                 struct {
183                         __u8 flags;
184                         __u8 reserved[18];
185                         __le32 start_clu;
186                         __le64 size;
187                 } __attribute__((packed)) bitmap; /* allocation bitmap directory entry */
188                 struct {
189                         __u8 reserved1[3];
190                         __le32 checksum;
191                         __u8 reserved2[12];
192                         __le32 start_clu;
193                         __le64 size;
194                 } __attribute__((packed)) upcase; /* up-case table directory entry */
195                 struct {
196                         __u8 num_ext;
197                         __le16 checksum;
198                         __u16 flags;
199                         __u8 guid[VOLUME_GUID_LEN];
200                         __u8 reserved[10];
201                 } __attribute__((packed)) guid; /* volume GUID directory entry */
202         } __attribute__((packed)) dentry;
203 } __attribute__((packed));
204
205 #define vol_char_cnt                    dentry.vol.character_count
206 #define vol_label                       dentry.vol.volume_label
207 #define file_num_ext                    dentry.file.num_ext
208 #define file_checksum                   dentry.file.checksum
209 #define file_attr                       dentry.file.attr
210 #define file_create_time                dentry.file.create_time
211 #define file_create_date                dentry.file.create_date
212 #define file_modify_time                dentry.file.modify_time
213 #define file_modify_date                dentry.file.modify_date
214 #define file_access_time                dentry.file.access_time
215 #define file_access_date                dentry.file.access_date
216 #define file_create_time_ms             dentry.file.create_time_ms
217 #define file_modify_time_ms             dentry.file.modify_time_ms
218 #define file_access_time_ms             dentry.file.access_time_ms
219 #define stream_flags                    dentry.stream.flags
220 #define stream_name_len                 dentry.stream.name_len
221 #define stream_name_hash                dentry.stream.name_hash
222 #define stream_start_clu                dentry.stream.start_clu
223 #define stream_valid_size               dentry.stream.valid_size
224 #define stream_size                     dentry.stream.size
225 #define name_flags                      dentry.name.flags
226 #define name_unicode                    dentry.name.unicode_0_14
227 #define bitmap_flags                    dentry.bitmap.flags
228 #define bitmap_start_clu                dentry.bitmap.start_clu
229 #define bitmap_size                     dentry.bitmap.size
230 #define upcase_start_clu                dentry.upcase.start_clu
231 #define upcase_size                     dentry.upcase.size
232 #define upcase_checksum                 dentry.upcase.checksum
233
234 #endif /* !_EXFAT_H */