1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Constants/Data Structures/Prototypes on a Volume with FAT Filesystem |
---|
5 | * |
---|
6 | * @ingroup libfs_dosfs |
---|
7 | * |
---|
8 | * Constants/Data Structures/Prototypes for Low-Level |
---|
9 | * Operations on a Volume with FAT Filesystem |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * |
---|
14 | * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia |
---|
15 | * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru> |
---|
16 | * |
---|
17 | * The license and distribution terms for this file may be |
---|
18 | * found in the file LICENSE in this distribution or at |
---|
19 | * http://www.rtems.org/license/LICENSE. |
---|
20 | */ |
---|
21 | |
---|
22 | #ifndef __DOSFS_FAT_H__ |
---|
23 | #define __DOSFS_FAT_H__ |
---|
24 | |
---|
25 | #include <sys/param.h> |
---|
26 | #include <sys/endian.h> |
---|
27 | #include <string.h> |
---|
28 | |
---|
29 | #include <rtems/seterr.h> |
---|
30 | |
---|
31 | #include <errno.h> |
---|
32 | #include <rtems/bdbuf.h> |
---|
33 | |
---|
34 | /** |
---|
35 | * @defgroup libfs_dosfs FAT FileSystem |
---|
36 | * |
---|
37 | * @ingroup libfs |
---|
38 | */ |
---|
39 | /**@{*/ |
---|
40 | #ifdef __cplusplus |
---|
41 | extern "C" { |
---|
42 | #endif |
---|
43 | |
---|
44 | #ifndef RC_OK |
---|
45 | #define RC_OK 0 |
---|
46 | #endif |
---|
47 | |
---|
48 | /* |
---|
49 | * Remember that all FAT file system on disk data structure is |
---|
50 | * "little endian"! |
---|
51 | * (derived from linux) |
---|
52 | */ |
---|
53 | /* |
---|
54 | * Conversion from and to little-endian byte order. (no-op on i386/i486) |
---|
55 | * |
---|
56 | * Naming: Ca_b_c, where a: F = from, T = to, b: LE = little-endian, |
---|
57 | * BE = big-endian, c: W = word (16 bits), L = longword (32 bits) |
---|
58 | */ |
---|
59 | #define CF_LE_W(v) le16toh(v) |
---|
60 | #define CF_LE_L(v) le32toh(v) |
---|
61 | #define CT_LE_W(v) htole16(v) |
---|
62 | #define CT_LE_L(v) htole32(v) |
---|
63 | |
---|
64 | #define FAT_HASH_SIZE 2 |
---|
65 | #define FAT_HASH_MODULE FAT_HASH_SIZE |
---|
66 | |
---|
67 | |
---|
68 | #define FAT_SECTOR512_SIZE 512 /* sector size (bytes) */ |
---|
69 | #define FAT_SECTOR512_BITS 9 /* log2(SECTOR_SIZE) */ |
---|
70 | |
---|
71 | /* maximum + 1 number of clusters for FAT12 */ |
---|
72 | #define FAT_FAT12_MAX_CLN 4085 |
---|
73 | |
---|
74 | /* maximum + 1 number of clusters for FAT16 */ |
---|
75 | #define FAT_FAT16_MAX_CLN 65525 |
---|
76 | |
---|
77 | #define FAT_FAT12 0x01 |
---|
78 | #define FAT_FAT16 0x02 |
---|
79 | #define FAT_FAT32 0x04 |
---|
80 | |
---|
81 | #define FAT_UNDEFINED_VALUE (uint32_t)0xFFFFFFFF |
---|
82 | |
---|
83 | #define FAT_FAT12_EOC 0x0FF8 |
---|
84 | #define FAT_FAT16_EOC 0xFFF8 |
---|
85 | #define FAT_FAT32_EOC (uint32_t)0x0FFFFFF8 |
---|
86 | |
---|
87 | #define FAT_FAT12_FREE 0x0000 |
---|
88 | #define FAT_FAT16_FREE 0x0000 |
---|
89 | #define FAT_FAT32_FREE 0x00000000 |
---|
90 | |
---|
91 | #define FAT_GENFAT_EOC (uint32_t)0xFFFFFFFF |
---|
92 | #define FAT_GENFAT_FREE (uint32_t)0x00000000 |
---|
93 | |
---|
94 | #define FAT_FAT12_SHIFT 0x04 |
---|
95 | |
---|
96 | #define FAT_FAT12_MASK 0x00000FFF |
---|
97 | #define FAT_FAT16_MASK 0x0000FFFF |
---|
98 | #define FAT_FAT32_MASK (uint32_t)0x0FFFFFFF |
---|
99 | |
---|
100 | #define FAT_MAX_BPB_SIZE 90 |
---|
101 | #define FAT_TOTAL_MBR_SIZE 512 |
---|
102 | |
---|
103 | /* size of useful information in FSInfo sector */ |
---|
104 | #define FAT_USEFUL_INFO_SIZE 12 |
---|
105 | |
---|
106 | #define FAT_GET_ADDR(x, ofs) ((uint8_t *)(x) + (ofs)) |
---|
107 | |
---|
108 | #define FAT_GET_VAL8(x, ofs) (uint8_t)(*((uint8_t *)(x) + (ofs))) |
---|
109 | |
---|
110 | #define FAT_GET_VAL16(x, ofs) \ |
---|
111 | (uint16_t)( (*((uint8_t *)(x) + (ofs))) | \ |
---|
112 | ((*((uint8_t *)(x) + (ofs) + 1)) << 8) ) |
---|
113 | |
---|
114 | #define FAT_GET_VAL32(x, ofs) \ |
---|
115 | (uint32_t)( (uint32_t)(*((uint8_t *)(x) + (ofs))) | \ |
---|
116 | ((uint32_t)(*((uint8_t *)(x) + (ofs) + 1)) << 8) | \ |
---|
117 | ((uint32_t)(*((uint8_t *)(x) + (ofs) + 2)) << 16) | \ |
---|
118 | ((uint32_t)(*((uint8_t *)(x) + (ofs) + 3)) << 24) ) |
---|
119 | |
---|
120 | #define FAT_SET_VAL8(x, ofs,val) \ |
---|
121 | (*((uint8_t *)(x)+(ofs))=(uint8_t)(val)) |
---|
122 | |
---|
123 | #define FAT_SET_VAL16(x, ofs,val) do { \ |
---|
124 | FAT_SET_VAL8((x),(ofs),(val)); \ |
---|
125 | FAT_SET_VAL8((x),(ofs)+1,(val)>>8);\ |
---|
126 | } while (0) |
---|
127 | |
---|
128 | #define FAT_SET_VAL32(x, ofs,val) do { \ |
---|
129 | uint32_t val1 = val; \ |
---|
130 | FAT_SET_VAL16((x),(ofs),(val1)&0xffff);\ |
---|
131 | FAT_SET_VAL16((x),(ofs)+2,(val1)>>16);\ |
---|
132 | } while (0) |
---|
133 | |
---|
134 | /* macros to access boot sector fields */ |
---|
135 | #define FAT_GET_BR_JMPBOOT(x) FAT_GET_VAL8( x, 0) |
---|
136 | #define FAT_SET_BR_JMPBOOT(x,val) FAT_SET_VAL8( x, 0,val) |
---|
137 | |
---|
138 | #define FAT_GET_ADDR_BR_OEMNAME(x) FAT_GET_ADDR( x, 3) |
---|
139 | #define FAT_BR_OEMNAME_SIZE (8) |
---|
140 | |
---|
141 | #define FAT_GET_BR_BYTES_PER_SECTOR(x) FAT_GET_VAL16(x, 11) |
---|
142 | #define FAT_SET_BR_BYTES_PER_SECTOR(x,val) FAT_SET_VAL16(x, 11,val) |
---|
143 | |
---|
144 | #define FAT_GET_BR_SECTORS_PER_CLUSTER(x) FAT_GET_VAL8( x, 13) |
---|
145 | #define FAT_SET_BR_SECTORS_PER_CLUSTER(x,val)FAT_SET_VAL8( x, 13,val) |
---|
146 | |
---|
147 | #define FAT_GET_BR_RESERVED_SECTORS_NUM(x) FAT_GET_VAL16(x, 14) |
---|
148 | #define FAT_SET_BR_RESERVED_SECTORS_NUM(x,val) FAT_SET_VAL16(x, 14,val) |
---|
149 | |
---|
150 | #define FAT_GET_BR_FAT_NUM(x) FAT_GET_VAL8( x, 16) |
---|
151 | #define FAT_SET_BR_FAT_NUM(x,val) FAT_SET_VAL8( x, 16,val) |
---|
152 | |
---|
153 | #define FAT_GET_BR_FILES_PER_ROOT_DIR(x) FAT_GET_VAL16(x, 17) |
---|
154 | #define FAT_SET_BR_FILES_PER_ROOT_DIR(x,val) FAT_SET_VAL16(x, 17,val) |
---|
155 | |
---|
156 | #define FAT_GET_BR_TOTAL_SECTORS_NUM16(x) FAT_GET_VAL16(x, 19) |
---|
157 | #define FAT_SET_BR_TOTAL_SECTORS_NUM16(x,val)FAT_SET_VAL16(x, 19,val) |
---|
158 | |
---|
159 | #define FAT_GET_BR_MEDIA(x) FAT_GET_VAL8( x, 21) |
---|
160 | #define FAT_SET_BR_MEDIA(x,val) FAT_SET_VAL8( x, 21,val) |
---|
161 | |
---|
162 | #define FAT_GET_BR_SECTORS_PER_FAT(x) FAT_GET_VAL16(x, 22) |
---|
163 | #define FAT_SET_BR_SECTORS_PER_FAT(x,val) FAT_SET_VAL16(x, 22,val) |
---|
164 | |
---|
165 | #define FAT_GET_BR_SECTORS_PER_TRACK(x) FAT_GET_VAL16(x, 24) |
---|
166 | #define FAT_SET_BR_SECTORS_PER_TRACK(x,val) FAT_SET_VAL16(x, 24,val) |
---|
167 | |
---|
168 | #define FAT_GET_BR_NUMBER_OF_HEADS(x) FAT_GET_VAL16(x, 26) |
---|
169 | #define FAT_SET_BR_NUMBER_OF_HEADS(x,val) FAT_SET_VAL16(x, 26,val) |
---|
170 | |
---|
171 | #define FAT_GET_BR_HIDDEN_SECTORS(x) FAT_GET_VAL32(x, 28) |
---|
172 | #define FAT_SET_BR_HIDDEN_SECTORS(x,val) FAT_SET_VAL32(x, 28,val) |
---|
173 | |
---|
174 | #define FAT_GET_BR_TOTAL_SECTORS_NUM32(x) FAT_GET_VAL32(x, 32) |
---|
175 | #define FAT_SET_BR_TOTAL_SECTORS_NUM32(x,val) FAT_SET_VAL32(x, 32,val) |
---|
176 | /* --- start of FAT12/16 specific fields */ |
---|
177 | #define FAT_GET_BR_DRVNUM(x) FAT_GET_VAL8( x, 36) |
---|
178 | #define FAT_SET_BR_DRVNUM(x,val) FAT_SET_VAL8( x, 36,val) |
---|
179 | |
---|
180 | #define FAT_GET_BR_RSVD1(x) FAT_GET_VAL8( x, 37) |
---|
181 | #define FAT_SET_BR_RSVD1(x,val) FAT_SET_VAL8( x, 37,val) |
---|
182 | |
---|
183 | #define FAT_GET_BR_BOOTSIG(x) FAT_GET_VAL8( x, 38) |
---|
184 | #define FAT_SET_BR_BOOTSIG(x,val) FAT_SET_VAL8( x, 38,val) |
---|
185 | #define FAT_BR_BOOTSIG_VAL (0x29) |
---|
186 | |
---|
187 | #define FAT_GET_BR_VOLID(x) FAT_GET_VAL32(x, 39) |
---|
188 | #define FAT_SET_BR_VOLID(x,val) FAT_SET_VAL32(x, 39,val) |
---|
189 | |
---|
190 | #define FAT_GET_ADDR_BR_VOLLAB(x) FAT_GET_ADDR (x, 43) |
---|
191 | #define FAT_BR_VOLLAB_SIZE (11) |
---|
192 | |
---|
193 | #define FAT_GET_ADDR_BR_FILSYSTYPE(x) FAT_GET_ADDR (x, 54) |
---|
194 | #define FAT_BR_FILSYSTYPE_SIZE (8) |
---|
195 | /* --- end of FAT12/16 specific fields */ |
---|
196 | /* --- start of FAT32 specific fields */ |
---|
197 | #define FAT_GET_BR_SECTORS_PER_FAT32(x) FAT_GET_VAL32(x, 36) |
---|
198 | #define FAT_SET_BR_SECTORS_PER_FAT32(x,val) FAT_SET_VAL32(x, 36,val) |
---|
199 | |
---|
200 | #define FAT_GET_BR_EXT_FLAGS(x) FAT_GET_VAL16(x, 40) |
---|
201 | #define FAT_SET_BR_EXT_FLAGS(x,val) FAT_SET_VAL16(x, 40,val) |
---|
202 | |
---|
203 | #define FAT_GET_BR_FSVER(x) FAT_GET_VAL16(x, 42) |
---|
204 | #define FAT_SET_BR_FSVER(x,val) FAT_SET_VAL16(x, 42,val) |
---|
205 | |
---|
206 | #define FAT_GET_BR_FAT32_ROOT_CLUSTER(x) FAT_GET_VAL32(x, 44) |
---|
207 | #define FAT_SET_BR_FAT32_ROOT_CLUSTER(x,val) FAT_SET_VAL32(x, 44,val) |
---|
208 | |
---|
209 | #define FAT_GET_BR_FAT32_FS_INFO_SECTOR(x) FAT_GET_VAL16(x, 48) |
---|
210 | #define FAT_SET_BR_FAT32_FS_INFO_SECTOR(x,val) FAT_SET_VAL16(x, 48,val) |
---|
211 | |
---|
212 | #define FAT_GET_BR_FAT32_BK_BOOT_SECTOR(x) FAT_GET_VAL16(x, 50) |
---|
213 | #define FAT_SET_BR_FAT32_BK_BOOT_SECTOR(x,val) FAT_SET_VAL16(x, 50,val) |
---|
214 | |
---|
215 | #define FAT_GET_ADDR_BR_FAT32_RESERVED(x) FAT_GET_ADDR (x, 52) |
---|
216 | #define FAT_BR_FAT32_RESERVED_SIZE (12) |
---|
217 | |
---|
218 | #define FAT_GET_BR_FAT32_DRVNUM(x) FAT_GET_VAL8( x, 64) |
---|
219 | #define FAT_SET_BR_FAT32_DRVNUM(x,val) FAT_SET_VAL8( x, 64,val) |
---|
220 | |
---|
221 | #define FAT_GET_BR_FAT32_RSVD1(x) FAT_GET_VAL8( x, 65) |
---|
222 | #define FAT_SET_BR_FAT32_RSVD1(x,val) FAT_SET_VAL8( x, 65,val) |
---|
223 | |
---|
224 | #define FAT_GET_BR_FAT32_BOOTSIG(x) FAT_GET_VAL8( x, 66) |
---|
225 | #define FAT_SET_BR_FAT32_BOOTSIG(x,val) FAT_SET_VAL8( x, 66,val) |
---|
226 | #define FAT_BR_FAT32_BOOTSIG_VAL (0x29) |
---|
227 | |
---|
228 | #define FAT_GET_BR_FAT32_VOLID(x) FAT_GET_VAL32(x, 67) |
---|
229 | #define FAT_SET_BR_FAT32_VOLID(x,val) FAT_SET_VAL32(x, 67,val) |
---|
230 | |
---|
231 | #define FAT_GET_ADDR_BR_FAT32_VOLLAB(x) FAT_GET_ADDR (x, 71) |
---|
232 | #define FAT_BR_FAT32_VOLLAB_SIZE (11) |
---|
233 | |
---|
234 | #define FAT_GET_ADDR_BR_FAT32_FILSYSTYPE(x) FAT_GET_ADDR (x, 82) |
---|
235 | #define FAT_BR_FAT32_FILSYSTYPE_SIZE (8) |
---|
236 | /* --- end of FAT32 specific fields */ |
---|
237 | |
---|
238 | #define FAT_GET_BR_SIGNATURE(x) FAT_GET_VAL16(x,510) |
---|
239 | #define FAT_SET_BR_SIGNATURE(x,val) FAT_SET_VAL16(x,510,val) |
---|
240 | #define FAT_BR_SIGNATURE_VAL (0xAA55) |
---|
241 | |
---|
242 | /* |
---|
243 | * FAT32 FSINFO description |
---|
244 | */ |
---|
245 | #define FAT_GET_FSINFO_LEAD_SIGNATURE(x) FAT_GET_VAL32(x, 0) |
---|
246 | #define FAT_SET_FSINFO_LEAD_SIGNATURE(x,val) FAT_SET_VAL32(x, 0,val) |
---|
247 | #define FAT_FSINFO_LEAD_SIGNATURE_VALUE (0x41615252) |
---|
248 | |
---|
249 | #define FAT_GET_FSINFO_STRUC_SIGNATURE(x) FAT_GET_VAL32(x,484) |
---|
250 | #define FAT_SET_FSINFO_STRUC_SIGNATURE(x,val) FAT_SET_VAL32(x,484,val) |
---|
251 | #define FAT_FSINFO_STRUC_SIGNATURE_VALUE (0x61417272) |
---|
252 | |
---|
253 | #define FAT_GET_FSINFO_TRAIL_SIGNATURE(x) FAT_GET_VAL32(x,508) |
---|
254 | #define FAT_SET_FSINFO_TRAIL_SIGNATURE(x,val) FAT_SET_VAL32(x,508,val) |
---|
255 | #define FAT_FSINFO_TRAIL_SIGNATURE_VALUE (0xAA550000) |
---|
256 | /* |
---|
257 | * I read FSInfo sector from offset 484 to access the information, so offsets |
---|
258 | * of these fields a relative |
---|
259 | */ |
---|
260 | #define FAT_GET_FSINFO_FREE_CLUSTER_COUNT(x) FAT_GET_VAL32(x, 4) |
---|
261 | #define FAT_SET_FSINFO_FREE_CLUSTER_COUNT(x,val) FAT_SET_VAL32(x, 4,val) |
---|
262 | #define FAT_GET_FSINFO_NEXT_FREE_CLUSTER(x) FAT_GET_VAL32(x, 8) |
---|
263 | #define FAT_SET_FSINFO_NEXT_FREE_CLUSTER(x,val) FAT_SET_VAL32(x, 8,val) |
---|
264 | |
---|
265 | #define FAT_FSI_INFO 484 |
---|
266 | #define FAT_FSINFO_STRUCT_OFFSET 488 |
---|
267 | #define FAT_FSINFO_FREE_CLUSTER_COUNT_OFFSET (FAT_FSINFO_STRUCT_OFFSET+0) |
---|
268 | |
---|
269 | #define FAT_FSINFO_NEXT_FREE_CLUSTER_OFFSET (FAT_FSINFO_STRUCT_OFFSET+4) |
---|
270 | |
---|
271 | #define FAT_RSRVD_CLN 0x02 |
---|
272 | |
---|
273 | #define FAT_FSI_LEADSIG_SIZE 0x04 |
---|
274 | |
---|
275 | #define FAT_TOTAL_FSINFO_SIZE 512 |
---|
276 | |
---|
277 | #define MS_BYTES_PER_CLUSTER_LIMIT 0x10000 /* 64K */ |
---|
278 | #define MS_BYTES_PER_CLUSTER_LIMIT_FAT12 0x1000 /* 4K */ |
---|
279 | |
---|
280 | #define FAT_BR_EXT_FLAGS_MIRROR 0x0080 |
---|
281 | |
---|
282 | #define FAT_BR_EXT_FLAGS_FAT_NUM 0x000F |
---|
283 | |
---|
284 | #define FAT_BR_MEDIA_FIXED 0xf8 |
---|
285 | |
---|
286 | #define FAT_DIRENTRY_SIZE 32 |
---|
287 | |
---|
288 | #define FAT_DIRENTRIES_PER_SEC512 16 |
---|
289 | |
---|
290 | /* |
---|
291 | * Volume descriptor |
---|
292 | * Description of the volume the FAT filesystem is located on - generally |
---|
293 | * the fields of the structure correspond to Boot Sector and BPB Structure |
---|
294 | * fields |
---|
295 | */ |
---|
296 | typedef struct fat_vol_s |
---|
297 | { |
---|
298 | uint16_t bps; /* bytes per sector */ |
---|
299 | uint8_t sec_log2; /* log2 of bps */ |
---|
300 | uint8_t sec_mul; /* log2 of 512bts sectors number per sector */ |
---|
301 | uint8_t spc; /* sectors per cluster */ |
---|
302 | uint8_t spc_log2; /* log2 of spc */ |
---|
303 | uint32_t bpc; /* bytes per cluster */ |
---|
304 | uint8_t bpc_log2; /* log2 of bytes per cluster */ |
---|
305 | uint8_t sectors_per_block; /* sectors per bdbuf block */ |
---|
306 | uint32_t bytes_per_block; /* number of bytes for the bduf block device handling */ |
---|
307 | uint8_t bytes_per_block_log2; /* log2 of bytes_per_block */ |
---|
308 | uint8_t fats; /* number of FATs */ |
---|
309 | uint8_t type; /* FAT type */ |
---|
310 | uint32_t mask; |
---|
311 | uint32_t eoc_val; |
---|
312 | uint16_t fat_loc; /* FAT start */ |
---|
313 | uint32_t fat_length; /* sectors per FAT */ |
---|
314 | uint32_t rdir_loc; /* root directory start */ |
---|
315 | uint16_t rdir_entrs; /* files per root directory */ |
---|
316 | uint32_t rdir_secs; /* sectors per root directory */ |
---|
317 | uint32_t rdir_size; /* root directory size in bytes */ |
---|
318 | uint32_t tot_secs; /* total count of sectors */ |
---|
319 | uint32_t data_fsec; /* first data sector */ |
---|
320 | uint32_t data_cls; /* count of data clusters */ |
---|
321 | uint32_t rdir_cl; /* first cluster of the root directory */ |
---|
322 | uint16_t info_sec; /* FSInfo Sector Structure location */ |
---|
323 | uint32_t free_cls; /* last known free clusters count */ |
---|
324 | uint32_t free_cls_in_fs_info; /* last known free clusters count |
---|
325 | in FS info sector */ |
---|
326 | uint32_t next_cl; /* next free cluster number */ |
---|
327 | uint32_t next_cl_in_fs_info; /* next free cluster number in FS |
---|
328 | info sector */ |
---|
329 | uint8_t mirror; /* mirroring enabla/disable */ |
---|
330 | uint32_t afat_loc; /* active FAT location */ |
---|
331 | uint8_t afat; /* the number of active FAT */ |
---|
332 | int fd; /* the disk device file descriptor */ |
---|
333 | rtems_disk_device *dd; /* disk device (see libblock) */ |
---|
334 | void *private_data; /* reserved */ |
---|
335 | } fat_vol_t; |
---|
336 | |
---|
337 | |
---|
338 | typedef struct fat_cache_s |
---|
339 | { |
---|
340 | uint32_t blk_num; |
---|
341 | bool modified; |
---|
342 | uint8_t state; |
---|
343 | rtems_bdbuf_buffer *buf; |
---|
344 | } fat_cache_t; |
---|
345 | |
---|
346 | /* |
---|
347 | * This structure identifies the instance of the filesystem on the FAT |
---|
348 | * ("fat-file") level. |
---|
349 | */ |
---|
350 | typedef struct fat_fs_info_s |
---|
351 | { |
---|
352 | fat_vol_t vol; /* volume descriptor */ |
---|
353 | rtems_chain_control *vhash; /* "vhash" of fat-file descriptors */ |
---|
354 | rtems_chain_control *rhash; /* "rhash" of fat-file descriptors */ |
---|
355 | char *uino; /* array of unique ino numbers */ |
---|
356 | uint32_t index; |
---|
357 | uint32_t uino_pool_size; /* size */ |
---|
358 | uint32_t uino_base; |
---|
359 | fat_cache_t c; /* cache */ |
---|
360 | uint8_t *sec_buf; /* just placeholder for anything */ |
---|
361 | } fat_fs_info_t; |
---|
362 | |
---|
363 | /* |
---|
364 | * FAT position is a the cluster and the offset into the |
---|
365 | * cluster. |
---|
366 | */ |
---|
367 | typedef struct fat_pos_s |
---|
368 | { |
---|
369 | uint32_t cln; |
---|
370 | uint32_t ofs; |
---|
371 | } fat_pos_t; |
---|
372 | |
---|
373 | /* |
---|
374 | * If the name we looking for is file we store not only first data cluster |
---|
375 | * number, but and cluster number and offset for directory entry for this |
---|
376 | * name. We also add the LFN start offset so we can delete it the whole |
---|
377 | * file name. We can then use this to delete the file. |
---|
378 | */ |
---|
379 | typedef struct fat_dir_pos_s |
---|
380 | { |
---|
381 | fat_pos_t sname; |
---|
382 | fat_pos_t lname; |
---|
383 | } fat_dir_pos_t; |
---|
384 | |
---|
385 | /* |
---|
386 | * Set the long name entries to this value for a short file name. |
---|
387 | */ |
---|
388 | #define FAT_FILE_SHORT_NAME (0xffffffff) |
---|
389 | |
---|
390 | #define FAT_FAT_OFFSET(fat_type, cln) \ |
---|
391 | ((fat_type) & FAT_FAT12 ? ((cln) + ((cln) >> 1)) : \ |
---|
392 | (fat_type) & FAT_FAT16 ? ((cln) << 1) : \ |
---|
393 | ((cln) << 2)) |
---|
394 | |
---|
395 | #define FAT_CLUSTER_IS_ODD(n) ((n) & 0x0001) |
---|
396 | |
---|
397 | #define FAT12_SHIFT 0x4 /* half of a byte */ |
---|
398 | |
---|
399 | /* initial size of array of unique ino */ |
---|
400 | #define FAT_UINO_POOL_INIT_SIZE 0x100 |
---|
401 | |
---|
402 | /* cache support */ |
---|
403 | #define FAT_CACHE_EMPTY 0x0 |
---|
404 | #define FAT_CACHE_ACTUAL 0x1 |
---|
405 | |
---|
406 | #define FAT_OP_TYPE_READ 0x1 |
---|
407 | #define FAT_OP_TYPE_GET 0x2 |
---|
408 | |
---|
409 | static inline void |
---|
410 | fat_dir_pos_init( |
---|
411 | fat_dir_pos_t *dir_pos |
---|
412 | ) |
---|
413 | { |
---|
414 | dir_pos->sname.cln = 0; |
---|
415 | dir_pos->sname.ofs = 0; |
---|
416 | dir_pos->lname.cln = FAT_FILE_SHORT_NAME; |
---|
417 | dir_pos->lname.ofs = FAT_FILE_SHORT_NAME; |
---|
418 | } |
---|
419 | |
---|
420 | static inline uint32_t |
---|
421 | fat_cluster_num_to_sector_num( |
---|
422 | const fat_fs_info_t *fs_info, |
---|
423 | uint32_t cln |
---|
424 | ) |
---|
425 | { |
---|
426 | if ( (cln == 0) && (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)) ) |
---|
427 | return fs_info->vol.rdir_loc; |
---|
428 | |
---|
429 | return (((cln - FAT_RSRVD_CLN) << fs_info->vol.spc_log2) + |
---|
430 | fs_info->vol.data_fsec); |
---|
431 | } |
---|
432 | |
---|
433 | static inline uint32_t |
---|
434 | fat_cluster_num_to_sector512_num( |
---|
435 | const fat_fs_info_t *fs_info, |
---|
436 | uint32_t cln |
---|
437 | ) |
---|
438 | { |
---|
439 | if (cln == 1) |
---|
440 | return 1; |
---|
441 | |
---|
442 | return (fat_cluster_num_to_sector_num(fs_info, cln) << |
---|
443 | fs_info->vol.sec_mul); |
---|
444 | } |
---|
445 | |
---|
446 | static inline uint32_t |
---|
447 | fat_block_num_to_cluster_num (const fat_fs_info_t *fs_info, |
---|
448 | const uint32_t block_number) |
---|
449 | { |
---|
450 | return block_number >> (fs_info->vol.bpc_log2 - fs_info->vol.bytes_per_block_log2); |
---|
451 | } |
---|
452 | |
---|
453 | static inline uint32_t |
---|
454 | fat_block_num_to_sector_num (const fat_fs_info_t *fs_info, |
---|
455 | const uint32_t block_number) |
---|
456 | { |
---|
457 | return block_number << (fs_info->vol.bytes_per_block_log2 - fs_info->vol.sec_log2); |
---|
458 | } |
---|
459 | |
---|
460 | static inline uint32_t |
---|
461 | fat_sector_num_to_block_num (const fat_fs_info_t *fs_info, |
---|
462 | const uint32_t sector_number) |
---|
463 | { |
---|
464 | return sector_number >> (fs_info->vol.bytes_per_block_log2 - fs_info->vol.sec_log2); |
---|
465 | } |
---|
466 | |
---|
467 | static inline uint32_t |
---|
468 | fat_sector_offset_to_block_offset (const fat_fs_info_t *fs_info, |
---|
469 | const uint32_t sector, |
---|
470 | const uint32_t sector_offset) |
---|
471 | { |
---|
472 | return sector_offset + |
---|
473 | ((sector - |
---|
474 | fat_block_num_to_sector_num (fs_info, |
---|
475 | fat_sector_num_to_block_num (fs_info, sector))) |
---|
476 | << fs_info->vol.sec_log2); |
---|
477 | } |
---|
478 | |
---|
479 | static inline void |
---|
480 | fat_buf_mark_modified(fat_fs_info_t *fs_info) |
---|
481 | { |
---|
482 | fs_info->c.modified = true; |
---|
483 | } |
---|
484 | |
---|
485 | int |
---|
486 | fat_buf_access(fat_fs_info_t *fs_info, |
---|
487 | uint32_t sec_num, |
---|
488 | int op_type, |
---|
489 | uint8_t **sec_buf); |
---|
490 | |
---|
491 | int |
---|
492 | fat_buf_release(fat_fs_info_t *fs_info); |
---|
493 | |
---|
494 | ssize_t |
---|
495 | _fat_block_read(fat_fs_info_t *fs_info, |
---|
496 | uint32_t start, |
---|
497 | uint32_t offset, |
---|
498 | uint32_t count, |
---|
499 | void *buff); |
---|
500 | |
---|
501 | ssize_t |
---|
502 | fat_cluster_write(fat_fs_info_t *fs_info, |
---|
503 | uint32_t start_cln, |
---|
504 | uint32_t offset, |
---|
505 | uint32_t count, |
---|
506 | const void *buff); |
---|
507 | |
---|
508 | ssize_t |
---|
509 | fat_sector_write(fat_fs_info_t *fs_info, |
---|
510 | uint32_t start, |
---|
511 | uint32_t offset, |
---|
512 | uint32_t count, |
---|
513 | const void *buff); |
---|
514 | |
---|
515 | ssize_t |
---|
516 | fat_cluster_set(fat_fs_info_t *fs_info, |
---|
517 | uint32_t start, |
---|
518 | uint32_t offset, |
---|
519 | uint32_t count, |
---|
520 | uint8_t pattern); |
---|
521 | |
---|
522 | |
---|
523 | int |
---|
524 | fat_init_volume_info(fat_fs_info_t *fs_info, const char *device); |
---|
525 | |
---|
526 | int |
---|
527 | fat_init_clusters_chain(fat_fs_info_t *fs_info, |
---|
528 | uint32_t start_cln); |
---|
529 | |
---|
530 | int |
---|
531 | fat_shutdown_drive(fat_fs_info_t *fs_info); |
---|
532 | |
---|
533 | |
---|
534 | uint32_t |
---|
535 | fat_get_unique_ino(fat_fs_info_t *fs_info); |
---|
536 | |
---|
537 | bool |
---|
538 | fat_ino_is_unique(fat_fs_info_t *fs_info, |
---|
539 | uint32_t ino); |
---|
540 | |
---|
541 | void |
---|
542 | fat_free_unique_ino(fat_fs_info_t *fs_info, |
---|
543 | uint32_t ino); |
---|
544 | |
---|
545 | int |
---|
546 | fat_sync(fat_fs_info_t *fs_info); |
---|
547 | |
---|
548 | #ifdef __cplusplus |
---|
549 | } |
---|
550 | #endif |
---|
551 | /**@}*/ |
---|
552 | #endif /* __DOSFS_FAT_H__ */ |
---|