1 /*
2 	exfatfs.h (29.08.09)
3 	Definitions of structures and constants used in exFAT file system.
4 
5 	Free exFAT implementation.
6 	Copyright (C) 2010-2018  Andrew Nayenko
7 
8 	This program is free software; you can redistribute it and/or modify
9 	it under the terms of the GNU General Public License as published by
10 	the Free Software Foundation, either version 2 of the License, or
11 	(at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 	GNU General Public License for more details.
17 
18 	You should have received a copy of the GNU General Public License along
19 	with this program; if not, write to the Free Software Foundation, Inc.,
20 	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 
23 #ifndef EXFATFS_H_INCLUDED
24 #define EXFATFS_H_INCLUDED
25 
26 #include "byteorder.h"
27 #include "compiler.h"
28 
29 typedef uint32_t cluster_t;		/* cluster number */
30 
31 #define EXFAT_FIRST_DATA_CLUSTER 2
32 #define EXFAT_LAST_DATA_CLUSTER 0xfffffff6
33 
34 #define EXFAT_CLUSTER_FREE         0 /* free cluster */
35 #define EXFAT_CLUSTER_BAD 0xfffffff7 /* cluster contains bad sector */
36 #define EXFAT_CLUSTER_END 0xffffffff /* final cluster of file or directory */
37 
38 #define EXFAT_STATE_MOUNTED 2
39 
40 struct exfat_super_block
41 {
42 	uint8_t jump[3];				/* 0x00 jmp and nop instructions */
43 	uint8_t oem_name[8];			/* 0x03 "EXFAT   " */
44 	uint8_t	__unused1[53];			/* 0x0B always 0 */
45 	le64_t sector_start;			/* 0x40 partition first sector */
46 	le64_t sector_count;			/* 0x48 partition sectors count */
47 	le32_t fat_sector_start;		/* 0x50 FAT first sector */
48 	le32_t fat_sector_count;		/* 0x54 FAT sectors count */
49 	le32_t cluster_sector_start;	/* 0x58 first cluster sector */
50 	le32_t cluster_count;			/* 0x5C total clusters count */
51 	le32_t rootdir_cluster;			/* 0x60 first cluster of the root dir */
52 	le32_t volume_serial;			/* 0x64 volume serial number */
53 	struct							/* 0x68 FS version */
54 	{
55 		uint8_t minor;
56 		uint8_t major;
57 	}
58 	version;
59 	le16_t volume_state;			/* 0x6A volume state flags */
60 	uint8_t sector_bits;			/* 0x6C sector size as (1 << n) */
61 	uint8_t spc_bits;				/* 0x6D sectors per cluster as (1 << n) */
62 	uint8_t fat_count;				/* 0x6E always 1 */
63 	uint8_t drive_no;				/* 0x6F always 0x80 */
64 	uint8_t allocated_percent;		/* 0x70 percentage of allocated space */
65 	uint8_t __unused2[397];			/* 0x71 always 0 */
66 	le16_t boot_signature;			/* the value of 0xAA55 */
67 }
68 PACKED;
69 STATIC_ASSERT(sizeof(struct exfat_super_block) == 512);
70 
71 #define EXFAT_ENTRY_VALID     0x80
72 #define EXFAT_ENTRY_CONTINUED 0x40
73 #define EXFAT_ENTRY_OPTIONAL  0x20
74 
75 #define EXFAT_ENTRY_BITMAP    (0x01 | EXFAT_ENTRY_VALID)
76 #define EXFAT_ENTRY_UPCASE    (0x02 | EXFAT_ENTRY_VALID)
77 #define EXFAT_ENTRY_LABEL     (0x03 | EXFAT_ENTRY_VALID)
78 #define EXFAT_ENTRY_FILE      (0x05 | EXFAT_ENTRY_VALID)
79 #define EXFAT_ENTRY_FILE_INFO (0x00 | EXFAT_ENTRY_VALID | EXFAT_ENTRY_CONTINUED)
80 #define EXFAT_ENTRY_FILE_NAME (0x01 | EXFAT_ENTRY_VALID | EXFAT_ENTRY_CONTINUED)
81 #define EXFAT_ENTRY_FILE_TAIL (0x00 | EXFAT_ENTRY_VALID \
82                                     | EXFAT_ENTRY_CONTINUED \
83                                     | EXFAT_ENTRY_OPTIONAL)
84 
85 struct exfat_entry					/* common container for all entries */
86 {
87 	uint8_t type;					/* any of EXFAT_ENTRY_xxx */
88 	uint8_t data[31];
89 }
90 PACKED;
91 STATIC_ASSERT(sizeof(struct exfat_entry) == 32);
92 
93 #define EXFAT_ENAME_MAX 15
94 
95 struct exfat_entry_bitmap			/* allocated clusters bitmap */
96 {
97 	uint8_t type;					/* EXFAT_ENTRY_BITMAP */
98 	uint8_t __unknown1[19];
99 	le32_t start_cluster;
100 	le64_t size;					/* in bytes */
101 }
102 PACKED;
103 STATIC_ASSERT(sizeof(struct exfat_entry_bitmap) == 32);
104 
105 #define EXFAT_UPCASE_CHARS 0x10000
106 
107 struct exfat_entry_upcase			/* upper case translation table */
108 {
109 	uint8_t type;					/* EXFAT_ENTRY_UPCASE */
110 	uint8_t __unknown1[3];
111 	le32_t checksum;
112 	uint8_t __unknown2[12];
113 	le32_t start_cluster;
114 	le64_t size;					/* in bytes */
115 }
116 PACKED;
117 STATIC_ASSERT(sizeof(struct exfat_entry_upcase) == 32);
118 
119 struct exfat_entry_label			/* volume label */
120 {
121 	uint8_t type;					/* EXFAT_ENTRY_LABEL */
122 	uint8_t length;					/* number of characters */
123 	le16_t name[EXFAT_ENAME_MAX];	/* in UTF-16LE */
124 }
125 PACKED;
126 STATIC_ASSERT(sizeof(struct exfat_entry_label) == 32);
127 
128 #define EXFAT_ATTRIB_RO     0x01
129 #define EXFAT_ATTRIB_HIDDEN 0x02
130 #define EXFAT_ATTRIB_SYSTEM 0x04
131 #define EXFAT_ATTRIB_VOLUME 0x08
132 #define EXFAT_ATTRIB_DIR    0x10
133 #define EXFAT_ATTRIB_ARCH   0x20
134 
135 struct exfat_entry_meta1			/* file or directory info (part 1) */
136 {
137 	uint8_t type;					/* EXFAT_ENTRY_FILE */
138 	uint8_t continuations;
139 	le16_t checksum;
140 	le16_t attrib;					/* combination of EXFAT_ATTRIB_xxx */
141 	le16_t __unknown1;
142 	le16_t crtime, crdate;			/* creation date and time */
143 	le16_t mtime, mdate;			/* latest modification date and time */
144 	le16_t atime, adate;			/* latest access date and time */
145 	uint8_t crtime_cs;				/* creation time in cs (centiseconds) */
146 	uint8_t mtime_cs;				/* latest modification time in cs */
147 	uint8_t __unknown2[10];
148 }
149 PACKED;
150 STATIC_ASSERT(sizeof(struct exfat_entry_meta1) == 32);
151 
152 #define EXFAT_FLAG_ALWAYS1		(1u << 0)
153 #define EXFAT_FLAG_CONTIGUOUS	(1u << 1)
154 
155 struct exfat_entry_meta2			/* file or directory info (part 2) */
156 {
157 	uint8_t type;					/* EXFAT_ENTRY_FILE_INFO */
158 	uint8_t flags;					/* combination of EXFAT_FLAG_xxx */
159 	uint8_t __unknown1;
160 	uint8_t name_length;
161 	le16_t name_hash;
162 	le16_t __unknown2;
163 	le64_t valid_size;				/* in bytes, less or equal to size */
164 	uint8_t __unknown3[4];
165 	le32_t start_cluster;
166 	le64_t size;					/* in bytes */
167 }
168 PACKED;
169 STATIC_ASSERT(sizeof(struct exfat_entry_meta2) == 32);
170 
171 struct exfat_entry_name				/* file or directory name */
172 {
173 	uint8_t type;					/* EXFAT_ENTRY_FILE_NAME */
174 	uint8_t __unknown;
175 	le16_t name[EXFAT_ENAME_MAX];	/* in UTF-16LE */
176 }
177 PACKED;
178 STATIC_ASSERT(sizeof(struct exfat_entry_name) == 32);
179 
180 #endif /* ifndef EXFATFS_H_INCLUDED */
181