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