1 | /* |
---|
2 | * fat_fat_operations.c |
---|
3 | * |
---|
4 | * General operations on File Allocation Table |
---|
5 | * |
---|
6 | * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia |
---|
7 | * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru> |
---|
8 | */ |
---|
9 | |
---|
10 | #if HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <sys/types.h> |
---|
15 | #include <sys/stat.h> |
---|
16 | #include <fcntl.h> |
---|
17 | #include <unistd.h> |
---|
18 | #include <errno.h> |
---|
19 | #include <stdlib.h> |
---|
20 | |
---|
21 | #include <rtems/libio_.h> |
---|
22 | |
---|
23 | #include "fat.h" |
---|
24 | #include "fat_fat_operations.h" |
---|
25 | |
---|
26 | /* fat_scan_fat_for_free_clusters -- |
---|
27 | * Allocate chain of free clusters from Files Allocation Table |
---|
28 | * |
---|
29 | * PARAMETERS: |
---|
30 | * fs_info - FS info |
---|
31 | * chain - the number of the first allocated cluster (first cluster |
---|
32 | * in the chain) |
---|
33 | * count - count of clusters to allocate (chain length) |
---|
34 | * |
---|
35 | * RETURNS: |
---|
36 | * RC_OK on success, or error code if error occured (errno set |
---|
37 | * appropriately) |
---|
38 | * |
---|
39 | * |
---|
40 | */ |
---|
41 | int |
---|
42 | fat_scan_fat_for_free_clusters( |
---|
43 | fat_fs_info_t *fs_info, |
---|
44 | uint32_t *chain, |
---|
45 | uint32_t count, |
---|
46 | uint32_t *cls_added, |
---|
47 | uint32_t *last_cl, |
---|
48 | bool zero_fill |
---|
49 | ) |
---|
50 | { |
---|
51 | int rc = RC_OK; |
---|
52 | uint32_t cl4find = 2; |
---|
53 | uint32_t next_cln = 0; |
---|
54 | uint32_t save_cln = 0; |
---|
55 | uint32_t data_cls_val = fs_info->vol.data_cls + 2; |
---|
56 | uint32_t i = 2; |
---|
57 | |
---|
58 | *cls_added = 0; |
---|
59 | |
---|
60 | if (count == 0) |
---|
61 | return rc; |
---|
62 | |
---|
63 | if (fs_info->vol.next_cl != FAT_UNDEFINED_VALUE) |
---|
64 | cl4find = fs_info->vol.next_cl; |
---|
65 | |
---|
66 | /* |
---|
67 | * fs_info->vol.data_cls is exactly the count of data clusters |
---|
68 | * starting at cluster 2, so the maximum valid cluster number is |
---|
69 | * (fs_info->vol.data_cls + 1) |
---|
70 | */ |
---|
71 | while (i < data_cls_val) |
---|
72 | { |
---|
73 | rc = fat_get_fat_cluster(fs_info, cl4find, &next_cln); |
---|
74 | if ( rc != RC_OK ) |
---|
75 | { |
---|
76 | if (*cls_added != 0) |
---|
77 | fat_free_fat_clusters_chain(fs_info, (*chain)); |
---|
78 | return rc; |
---|
79 | } |
---|
80 | |
---|
81 | if (next_cln == FAT_GENFAT_FREE) |
---|
82 | { |
---|
83 | /* |
---|
84 | * We are enforced to process allocation of the first free cluster |
---|
85 | * by separate 'if' statement because otherwise undo function |
---|
86 | * wouldn't work properly |
---|
87 | */ |
---|
88 | if (*cls_added == 0) |
---|
89 | { |
---|
90 | *chain = cl4find; |
---|
91 | rc = fat_set_fat_cluster(fs_info, cl4find, FAT_GENFAT_EOC); |
---|
92 | if ( rc != RC_OK ) |
---|
93 | { |
---|
94 | /* |
---|
95 | * this is the first cluster we tried to allocate so no |
---|
96 | * cleanup activity needed |
---|
97 | */ |
---|
98 | return rc; |
---|
99 | } |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | /* set EOC value to new allocated cluster */ |
---|
104 | rc = fat_set_fat_cluster(fs_info, cl4find, FAT_GENFAT_EOC); |
---|
105 | if ( rc != RC_OK ) |
---|
106 | { |
---|
107 | /* cleanup activity */ |
---|
108 | fat_free_fat_clusters_chain(fs_info, (*chain)); |
---|
109 | return rc; |
---|
110 | } |
---|
111 | |
---|
112 | rc = fat_set_fat_cluster(fs_info, save_cln, cl4find); |
---|
113 | if ( rc != RC_OK ) |
---|
114 | goto cleanup; |
---|
115 | } |
---|
116 | |
---|
117 | if (zero_fill) { |
---|
118 | uint32_t sec = fat_cluster_num_to_sector_num(fs_info, |
---|
119 | cl4find); |
---|
120 | |
---|
121 | rc = _fat_block_zero(fs_info, sec, 0, fs_info->vol.bpc); |
---|
122 | if ( rc != RC_OK ) |
---|
123 | goto cleanup; |
---|
124 | } |
---|
125 | |
---|
126 | save_cln = cl4find; |
---|
127 | (*cls_added)++; |
---|
128 | |
---|
129 | /* have we satisfied request ? */ |
---|
130 | if (*cls_added == count) |
---|
131 | { |
---|
132 | fs_info->vol.next_cl = save_cln; |
---|
133 | if (fs_info->vol.free_cls != FAT_UNDEFINED_VALUE) |
---|
134 | fs_info->vol.free_cls -= (*cls_added); |
---|
135 | *last_cl = save_cln; |
---|
136 | fat_buf_release(fs_info); |
---|
137 | return rc; |
---|
138 | } |
---|
139 | } |
---|
140 | i++; |
---|
141 | cl4find++; |
---|
142 | if (cl4find >= data_cls_val) |
---|
143 | cl4find = 2; |
---|
144 | } |
---|
145 | |
---|
146 | fs_info->vol.next_cl = save_cln; |
---|
147 | if (fs_info->vol.free_cls != FAT_UNDEFINED_VALUE) |
---|
148 | fs_info->vol.free_cls -= (*cls_added); |
---|
149 | |
---|
150 | *last_cl = save_cln; |
---|
151 | fat_buf_release(fs_info); |
---|
152 | return RC_OK; |
---|
153 | |
---|
154 | cleanup: |
---|
155 | |
---|
156 | /* cleanup activity */ |
---|
157 | fat_free_fat_clusters_chain(fs_info, (*chain)); |
---|
158 | /* trying to save last allocated cluster for future use */ |
---|
159 | fat_set_fat_cluster(fs_info, cl4find, FAT_GENFAT_FREE); |
---|
160 | fat_buf_release(fs_info); |
---|
161 | return rc; |
---|
162 | } |
---|
163 | |
---|
164 | /* fat_free_fat_clusters_chain -- |
---|
165 | * Free chain of clusters in Files Allocation Table. |
---|
166 | * |
---|
167 | * PARAMETERS: |
---|
168 | * fs_info - FS info |
---|
169 | * chain - number of the first cluster in the chain |
---|
170 | * |
---|
171 | * RETURNS: |
---|
172 | * RC_OK on success, or -1 if error occured (errno set appropriately) |
---|
173 | */ |
---|
174 | int |
---|
175 | fat_free_fat_clusters_chain( |
---|
176 | fat_fs_info_t *fs_info, |
---|
177 | uint32_t chain |
---|
178 | ) |
---|
179 | { |
---|
180 | int rc = RC_OK, rc1 = RC_OK; |
---|
181 | uint32_t cur_cln = chain; |
---|
182 | uint32_t next_cln = 0; |
---|
183 | uint32_t freed_cls_cnt = 0; |
---|
184 | |
---|
185 | while ((cur_cln & fs_info->vol.mask) < fs_info->vol.eoc_val) |
---|
186 | { |
---|
187 | rc = fat_get_fat_cluster(fs_info, cur_cln, &next_cln); |
---|
188 | if ( rc != RC_OK ) |
---|
189 | { |
---|
190 | if(fs_info->vol.free_cls != FAT_UNDEFINED_VALUE) |
---|
191 | fs_info->vol.free_cls += freed_cls_cnt; |
---|
192 | |
---|
193 | fat_buf_release(fs_info); |
---|
194 | return rc; |
---|
195 | } |
---|
196 | |
---|
197 | rc = fat_set_fat_cluster(fs_info, cur_cln, FAT_GENFAT_FREE); |
---|
198 | if ( rc != RC_OK ) |
---|
199 | rc1 = rc; |
---|
200 | |
---|
201 | freed_cls_cnt++; |
---|
202 | cur_cln = next_cln; |
---|
203 | } |
---|
204 | |
---|
205 | fs_info->vol.next_cl = chain; |
---|
206 | if (fs_info->vol.free_cls != FAT_UNDEFINED_VALUE) |
---|
207 | fs_info->vol.free_cls += freed_cls_cnt; |
---|
208 | |
---|
209 | fat_buf_release(fs_info); |
---|
210 | if (rc1 != RC_OK) |
---|
211 | return rc1; |
---|
212 | |
---|
213 | return RC_OK; |
---|
214 | } |
---|
215 | |
---|
216 | /* fat_get_fat_cluster -- |
---|
217 | * Fetches the contents of the cluster (link to next cluster in the chain) |
---|
218 | * from Files Allocation Table. |
---|
219 | * |
---|
220 | * PARAMETERS: |
---|
221 | * fs_info - FS info |
---|
222 | * cln - number of cluster to fetch the contents from |
---|
223 | * ret_val - contents of the cluster 'cln' (link to next cluster in |
---|
224 | * the chain) |
---|
225 | * |
---|
226 | * RETURNS: |
---|
227 | * RC_OK on success, or -1 if error occured |
---|
228 | * and errno set appropriately |
---|
229 | */ |
---|
230 | int |
---|
231 | fat_get_fat_cluster( |
---|
232 | fat_fs_info_t *fs_info, |
---|
233 | uint32_t cln, |
---|
234 | uint32_t *ret_val |
---|
235 | ) |
---|
236 | { |
---|
237 | int rc = RC_OK; |
---|
238 | uint8_t *sec_buf; |
---|
239 | uint32_t sec = 0; |
---|
240 | uint32_t ofs = 0; |
---|
241 | |
---|
242 | /* sanity check */ |
---|
243 | if ( (cln < 2) || (cln > (fs_info->vol.data_cls + 1)) ) |
---|
244 | rtems_set_errno_and_return_minus_one(EIO); |
---|
245 | |
---|
246 | sec = (FAT_FAT_OFFSET(fs_info->vol.type, cln) >> fs_info->vol.sec_log2) + |
---|
247 | fs_info->vol.afat_loc; |
---|
248 | ofs = FAT_FAT_OFFSET(fs_info->vol.type, cln) & (fs_info->vol.bps - 1); |
---|
249 | |
---|
250 | rc = fat_buf_access(fs_info, sec, FAT_OP_TYPE_READ, &sec_buf); |
---|
251 | if (rc != RC_OK) |
---|
252 | return rc; |
---|
253 | |
---|
254 | switch ( fs_info->vol.type ) |
---|
255 | { |
---|
256 | case FAT_FAT12: |
---|
257 | /* |
---|
258 | * we are enforced in complex computations for FAT12 to escape CPU |
---|
259 | * align problems for some architectures |
---|
260 | */ |
---|
261 | *ret_val = (*(sec_buf + ofs)); |
---|
262 | if ( ofs == (fs_info->vol.bps - 1) ) |
---|
263 | { |
---|
264 | rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ, |
---|
265 | &sec_buf); |
---|
266 | if (rc != RC_OK) |
---|
267 | return rc; |
---|
268 | |
---|
269 | *ret_val |= *sec_buf << 8; |
---|
270 | } |
---|
271 | else |
---|
272 | { |
---|
273 | *ret_val |= *(sec_buf + ofs + 1) << 8; |
---|
274 | } |
---|
275 | |
---|
276 | if ( FAT_CLUSTER_IS_ODD(cln) ) |
---|
277 | *ret_val = (*ret_val) >> FAT12_SHIFT; |
---|
278 | else |
---|
279 | *ret_val = (*ret_val) & FAT_FAT12_MASK; |
---|
280 | break; |
---|
281 | |
---|
282 | case FAT_FAT16: |
---|
283 | *ret_val = *((uint16_t *)(sec_buf + ofs)); |
---|
284 | *ret_val = CF_LE_W(*ret_val); |
---|
285 | break; |
---|
286 | |
---|
287 | case FAT_FAT32: |
---|
288 | *ret_val = *((uint32_t *)(sec_buf + ofs)); |
---|
289 | *ret_val = CF_LE_L(*ret_val); |
---|
290 | break; |
---|
291 | |
---|
292 | default: |
---|
293 | rtems_set_errno_and_return_minus_one(EIO); |
---|
294 | break; |
---|
295 | } |
---|
296 | |
---|
297 | return RC_OK; |
---|
298 | } |
---|
299 | |
---|
300 | /* fat_set_fat_cluster -- |
---|
301 | * Set the contents of the cluster (link to next cluster in the chain) |
---|
302 | * from Files Allocation Table. |
---|
303 | * |
---|
304 | * PARAMETERS: |
---|
305 | * fs_info - FS info |
---|
306 | * cln - number of cluster to set contents to |
---|
307 | * in_val - value to set |
---|
308 | * |
---|
309 | * RETURNS: |
---|
310 | * RC_OK on success, or -1 if error occured |
---|
311 | * and errno set appropriately |
---|
312 | */ |
---|
313 | int |
---|
314 | fat_set_fat_cluster( |
---|
315 | fat_fs_info_t *fs_info, |
---|
316 | uint32_t cln, |
---|
317 | uint32_t in_val |
---|
318 | ) |
---|
319 | { |
---|
320 | int rc = RC_OK; |
---|
321 | uint32_t sec = 0; |
---|
322 | uint32_t ofs = 0; |
---|
323 | uint16_t fat16_clv = 0; |
---|
324 | uint32_t fat32_clv = 0; |
---|
325 | uint8_t *sec_buf = NULL; |
---|
326 | |
---|
327 | /* sanity check */ |
---|
328 | if ( (cln < 2) || (cln > (fs_info->vol.data_cls + 1)) ) |
---|
329 | rtems_set_errno_and_return_minus_one(EIO); |
---|
330 | |
---|
331 | sec = (FAT_FAT_OFFSET(fs_info->vol.type, cln) >> fs_info->vol.sec_log2) + |
---|
332 | fs_info->vol.afat_loc; |
---|
333 | ofs = FAT_FAT_OFFSET(fs_info->vol.type, cln) & (fs_info->vol.bps - 1); |
---|
334 | |
---|
335 | rc = fat_buf_access(fs_info, sec, FAT_OP_TYPE_READ, &sec_buf); |
---|
336 | if (rc != RC_OK) |
---|
337 | return rc; |
---|
338 | |
---|
339 | switch ( fs_info->vol.type ) |
---|
340 | { |
---|
341 | case FAT_FAT12: |
---|
342 | if ( FAT_CLUSTER_IS_ODD(cln) ) |
---|
343 | { |
---|
344 | fat16_clv = ((uint16_t )in_val) << FAT_FAT12_SHIFT; |
---|
345 | *(sec_buf + ofs) &= 0x0F; |
---|
346 | |
---|
347 | *(sec_buf + ofs) |= (uint8_t)(fat16_clv & 0x00F0); |
---|
348 | |
---|
349 | fat_buf_mark_modified(fs_info); |
---|
350 | |
---|
351 | if ( ofs == (fs_info->vol.bps - 1) ) |
---|
352 | { |
---|
353 | rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ, |
---|
354 | &sec_buf); |
---|
355 | if (rc != RC_OK) |
---|
356 | return rc; |
---|
357 | |
---|
358 | *sec_buf &= 0x00; |
---|
359 | |
---|
360 | *sec_buf |= (uint8_t)((fat16_clv & 0xFF00)>>8); |
---|
361 | |
---|
362 | fat_buf_mark_modified(fs_info); |
---|
363 | } |
---|
364 | else |
---|
365 | { |
---|
366 | *(sec_buf + ofs + 1) &= 0x00; |
---|
367 | |
---|
368 | *(sec_buf + ofs + 1) |= (uint8_t )((fat16_clv & 0xFF00)>>8); |
---|
369 | } |
---|
370 | } |
---|
371 | else |
---|
372 | { |
---|
373 | fat16_clv = ((uint16_t )in_val) & FAT_FAT12_MASK; |
---|
374 | *(sec_buf + ofs) &= 0x00; |
---|
375 | |
---|
376 | *(sec_buf + ofs) |= (uint8_t)(fat16_clv & 0x00FF); |
---|
377 | |
---|
378 | fat_buf_mark_modified(fs_info); |
---|
379 | |
---|
380 | if ( ofs == (fs_info->vol.bps - 1) ) |
---|
381 | { |
---|
382 | rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ, |
---|
383 | &sec_buf); |
---|
384 | if (rc != RC_OK) |
---|
385 | return rc; |
---|
386 | |
---|
387 | *sec_buf &= 0xF0; |
---|
388 | |
---|
389 | *sec_buf |= (uint8_t)((fat16_clv & 0xFF00)>>8); |
---|
390 | |
---|
391 | fat_buf_mark_modified(fs_info); |
---|
392 | } |
---|
393 | else |
---|
394 | { |
---|
395 | *(sec_buf + ofs + 1) &= 0xF0; |
---|
396 | |
---|
397 | *(sec_buf + ofs+1) |= (uint8_t)((fat16_clv & 0xFF00)>>8); |
---|
398 | } |
---|
399 | } |
---|
400 | break; |
---|
401 | |
---|
402 | case FAT_FAT16: |
---|
403 | *((uint16_t *)(sec_buf + ofs)) = |
---|
404 | (uint16_t )(CT_LE_W(in_val)); |
---|
405 | fat_buf_mark_modified(fs_info); |
---|
406 | break; |
---|
407 | |
---|
408 | case FAT_FAT32: |
---|
409 | fat32_clv = CT_LE_L((in_val & FAT_FAT32_MASK)); |
---|
410 | |
---|
411 | *((uint32_t *)(sec_buf + ofs)) &= CT_LE_L(0xF0000000); |
---|
412 | |
---|
413 | *((uint32_t *)(sec_buf + ofs)) |= fat32_clv; |
---|
414 | |
---|
415 | fat_buf_mark_modified(fs_info); |
---|
416 | break; |
---|
417 | |
---|
418 | default: |
---|
419 | rtems_set_errno_and_return_minus_one(EIO); |
---|
420 | break; |
---|
421 | |
---|
422 | } |
---|
423 | |
---|
424 | return RC_OK; |
---|
425 | } |
---|