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-2023  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 #ifdef __UBOOT__
27 #include <linux/stat.h>
28 #include <linux/types.h>
29 #include <log.h>
30 #include <stdint.h>
31 #include <vsprintf.h>
32 
33 struct timespec {
34 	time_t	tv_sec;		/* seconds */
35 	long	tv_nsec;	/* nanoseconds */
36 };
37 
38 #define time(t)		1741234567	/* Thu Mar  6 05:16:07 CET 2025 */
39 #define geteuid(n)	1000
40 #define getegid(n)	1000
41 #define strtol(n, e, b)	simple_strtol(n, e, b)
42 #define off_t		unsigned long long
43 #endif
44 
45 #include "byteorder.h"
46 #include "compiler.h"
47 
48 typedef uint32_t cluster_t;		/* cluster number */
49 
50 #define EXFAT_FIRST_DATA_CLUSTER 2
51 #define EXFAT_LAST_DATA_CLUSTER 0xfffffff6
52 
53 #define EXFAT_CLUSTER_FREE         0 /* free cluster */
54 #define EXFAT_CLUSTER_BAD 0xfffffff7 /* cluster contains bad sector */
55 #define EXFAT_CLUSTER_END 0xffffffff /* final cluster of file or directory */
56 
57 #define EXFAT_STATE_MOUNTED 2
58 
59 struct exfat_super_block
60 {
61 	uint8_t jump[3];				/* 0x00 jmp and nop instructions */
62 	uint8_t oem_name[8];			/* 0x03 "EXFAT   " */
63 	uint8_t	__unused1[53];			/* 0x0B always 0 */
64 	le64_t sector_start;			/* 0x40 partition first sector */
65 	le64_t sector_count;			/* 0x48 partition sectors count */
66 	le32_t fat_sector_start;		/* 0x50 FAT first sector */
67 	le32_t fat_sector_count;		/* 0x54 FAT sectors count */
68 	le32_t cluster_sector_start;	/* 0x58 first cluster sector */
69 	le32_t cluster_count;			/* 0x5C total clusters count */
70 	le32_t rootdir_cluster;			/* 0x60 first cluster of the root dir */
71 	le32_t volume_serial;			/* 0x64 volume serial number */
72 	struct							/* 0x68 FS version */
73 	{
74 		uint8_t minor;
75 		uint8_t major;
76 	}
77 	version;
78 	le16_t volume_state;			/* 0x6A volume state flags */
79 	uint8_t sector_bits;			/* 0x6C sector size as (1 << n) */
80 	uint8_t spc_bits;				/* 0x6D sectors per cluster as (1 << n) */
81 	uint8_t fat_count;				/* 0x6E always 1 */
82 	uint8_t drive_no;				/* 0x6F always 0x80 */
83 	uint8_t allocated_percent;		/* 0x70 percentage of allocated space */
84 	uint8_t __unused2[397];			/* 0x71 always 0 */
85 	le16_t boot_signature;			/* the value of 0xAA55 */
86 }
87 PACKED;
88 STATIC_ASSERT(sizeof(struct exfat_super_block) == 512);
89 
90 #define EXFAT_ENTRY_VALID     0x80
91 #define EXFAT_ENTRY_CONTINUED 0x40
92 #define EXFAT_ENTRY_OPTIONAL  0x20
93 
94 #define EXFAT_ENTRY_BITMAP    (0x01 | EXFAT_ENTRY_VALID)
95 #define EXFAT_ENTRY_UPCASE    (0x02 | EXFAT_ENTRY_VALID)
96 #define EXFAT_ENTRY_LABEL     (0x03 | EXFAT_ENTRY_VALID)
97 #define EXFAT_ENTRY_FILE      (0x05 | EXFAT_ENTRY_VALID)
98 #define EXFAT_ENTRY_FILE_INFO (0x00 | EXFAT_ENTRY_VALID | EXFAT_ENTRY_CONTINUED)
99 #define EXFAT_ENTRY_FILE_NAME (0x01 | EXFAT_ENTRY_VALID | EXFAT_ENTRY_CONTINUED)
100 #define EXFAT_ENTRY_FILE_TAIL (0x00 | EXFAT_ENTRY_VALID \
101                                     | EXFAT_ENTRY_CONTINUED \
102                                     | EXFAT_ENTRY_OPTIONAL)
103 
104 struct exfat_entry					/* common container for all entries */
105 {
106 	uint8_t type;					/* any of EXFAT_ENTRY_xxx */
107 	uint8_t data[31];
108 }
109 PACKED;
110 STATIC_ASSERT(sizeof(struct exfat_entry) == 32);
111 
112 #define EXFAT_ENAME_MAX 15
113 
114 struct exfat_entry_bitmap			/* allocated clusters bitmap */
115 {
116 	uint8_t type;					/* EXFAT_ENTRY_BITMAP */
117 	uint8_t __unknown1[19];
118 	le32_t start_cluster;
119 	le64_t size;					/* in bytes */
120 }
121 PACKED;
122 STATIC_ASSERT(sizeof(struct exfat_entry_bitmap) == 32);
123 
124 #define EXFAT_UPCASE_CHARS 0x10000
125 
126 struct exfat_entry_upcase			/* upper case translation table */
127 {
128 	uint8_t type;					/* EXFAT_ENTRY_UPCASE */
129 	uint8_t __unknown1[3];
130 	le32_t checksum;
131 	uint8_t __unknown2[12];
132 	le32_t start_cluster;
133 	le64_t size;					/* in bytes */
134 }
135 PACKED;
136 STATIC_ASSERT(sizeof(struct exfat_entry_upcase) == 32);
137 
138 struct exfat_entry_label			/* volume label */
139 {
140 	uint8_t type;					/* EXFAT_ENTRY_LABEL */
141 	uint8_t length;					/* number of characters */
142 	le16_t name[EXFAT_ENAME_MAX];	/* in UTF-16LE */
143 }
144 PACKED;
145 STATIC_ASSERT(sizeof(struct exfat_entry_label) == 32);
146 
147 #define EXFAT_ATTRIB_RO     0x01
148 #define EXFAT_ATTRIB_HIDDEN 0x02
149 #define EXFAT_ATTRIB_SYSTEM 0x04
150 #define EXFAT_ATTRIB_VOLUME 0x08
151 #define EXFAT_ATTRIB_DIR    0x10
152 #define EXFAT_ATTRIB_ARCH   0x20
153 
154 struct exfat_entry_meta1			/* file or directory info (part 1) */
155 {
156 	uint8_t type;					/* EXFAT_ENTRY_FILE */
157 	uint8_t continuations;
158 	le16_t checksum;
159 	le16_t attrib;					/* combination of EXFAT_ATTRIB_xxx */
160 	le16_t __unknown1;
161 	le16_t crtime, crdate;			/* creation date and time */
162 	le16_t mtime, mdate;			/* latest modification date and time */
163 	le16_t atime, adate;			/* latest access date and time */
164 	uint8_t crtime_cs;				/* creation time in cs (centiseconds) */
165 	uint8_t mtime_cs;				/* latest modification time in cs */
166 	uint8_t crtime_tzo, mtime_tzo, atime_tzo;	/* timezone offset encoded */
167 	uint8_t __unknown2[7];
168 }
169 PACKED;
170 STATIC_ASSERT(sizeof(struct exfat_entry_meta1) == 32);
171 
172 #define EXFAT_FLAG_ALWAYS1		(1u << 0)
173 #define EXFAT_FLAG_CONTIGUOUS	(1u << 1)
174 
175 struct exfat_entry_meta2			/* file or directory info (part 2) */
176 {
177 	uint8_t type;					/* EXFAT_ENTRY_FILE_INFO */
178 	uint8_t flags;					/* combination of EXFAT_FLAG_xxx */
179 	uint8_t __unknown1;
180 	uint8_t name_length;
181 	le16_t name_hash;
182 	le16_t __unknown2;
183 	le64_t valid_size;				/* in bytes, less or equal to size */
184 	uint8_t __unknown3[4];
185 	le32_t start_cluster;
186 	le64_t size;					/* in bytes */
187 }
188 PACKED;
189 STATIC_ASSERT(sizeof(struct exfat_entry_meta2) == 32);
190 
191 struct exfat_entry_name				/* file or directory name */
192 {
193 	uint8_t type;					/* EXFAT_ENTRY_FILE_NAME */
194 	uint8_t __unknown;
195 	le16_t name[EXFAT_ENAME_MAX];	/* in UTF-16LE */
196 }
197 PACKED;
198 STATIC_ASSERT(sizeof(struct exfat_entry_name) == 32);
199 
200 #endif /* ifndef EXFATFS_H_INCLUDED */
201