1 | /* |
---|
2 | * fatfs.c - FATFS file system API implementation. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016,2017) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | |
---|
25 | #include <hal_types.h> |
---|
26 | #include <hal_special.h> |
---|
27 | #include <printk.h> |
---|
28 | #include <kmem.h> |
---|
29 | #include <ppm.h> |
---|
30 | #include <vfs.h> |
---|
31 | #include <string.h> |
---|
32 | #include <rpc.h> |
---|
33 | #include <mapper.h> |
---|
34 | #include <cluster.h> |
---|
35 | #include <dev_ioc.h> |
---|
36 | #include <fatfs.h> |
---|
37 | |
---|
38 | |
---|
39 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
40 | // Extern variables |
---|
41 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
42 | |
---|
43 | extern vfs_ctx_t fs_context[FS_TYPES_NR]; // allocated in vfs.c file |
---|
44 | |
---|
45 | extern remote_barrier_t global_barrier; // allocated in kernel_init.c |
---|
46 | |
---|
47 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
48 | // FATFS private and static functions |
---|
49 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
50 | |
---|
51 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
52 | // These functions return the "offset" and "length" values of an |
---|
53 | // [offset,length] constant defined in the fatfs.h file. |
---|
54 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
55 | |
---|
56 | static inline int get_length( int offset , int length ) { return length; } |
---|
57 | |
---|
58 | static inline int get_offset( int offset , int length ) { return offset; } |
---|
59 | |
---|
60 | |
---|
61 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
62 | // This function returns the LBA of the first sector of a FAT cluster. |
---|
63 | // This function can be called by any thread running in any cluster. |
---|
64 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
65 | // @ ctx : pointer on FATFS context. |
---|
66 | // @ cluster : cluster index in FATFS. |
---|
67 | // @ return the lba value. |
---|
68 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
69 | static inline uint32_t fatfs_lba_from_cluster( fatfs_ctx_t * ctx, |
---|
70 | uint32_t cluster ) |
---|
71 | { |
---|
72 | return (ctx->cluster_begin_lba + ((cluster - 2) << 3)); |
---|
73 | } |
---|
74 | |
---|
75 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
76 | // This function display the content of the FATFS context. |
---|
77 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
78 | void fatfs_ctx_display() |
---|
79 | { |
---|
80 | uint32_t type = FS_TYPE_FATFS; |
---|
81 | vfs_ctx_t * vfs_ctx = &fs_context[FS_TYPE_FATFS]; |
---|
82 | fatfs_ctx_t * fatfs_ctx = (fatfs_ctx_t *)vfs_ctx->extend; |
---|
83 | |
---|
84 | printk("\n*** FAT context ***\n" |
---|
85 | "- fat_sectors = %d\n" |
---|
86 | "- sector size = %d\n" |
---|
87 | "- cluster size = %d\n" |
---|
88 | "- fat_first_lba = %d\n" |
---|
89 | "- data_first_lba = %d\n" |
---|
90 | "- root_dir_cluster = %d\n" |
---|
91 | "- mapper_xp = %l\n", |
---|
92 | fatfs_ctx->fat_sectors_count, |
---|
93 | fatfs_ctx->bytes_per_sector, |
---|
94 | fatfs_ctx->sectors_per_cluster * fatfs_ctx->bytes_per_sector, |
---|
95 | fatfs_ctx->fat_begin_lba, |
---|
96 | fatfs_ctx->cluster_begin_lba, |
---|
97 | fatfs_ctx->root_dir_cluster, |
---|
98 | fatfs_ctx->fat_mapper_xp ); |
---|
99 | } |
---|
100 | |
---|
101 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
102 | // This function return an integer record value (one, two, or four bytes) |
---|
103 | // from a memory buffer, taking into account endianness. |
---|
104 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
105 | // @ offset : first byte of record in buffer. |
---|
106 | // @ size : record length in bytes (1/2/4). |
---|
107 | // @ buffer : pointer on buffer base. |
---|
108 | // @ little endian : the most significant byte has the highest address when true. |
---|
109 | // @ return the integer value in a 32 bits word. |
---|
110 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
111 | static uint32_t fatfs_get_record( uint32_t offset, |
---|
112 | uint32_t size, |
---|
113 | uint8_t * buffer, |
---|
114 | uint32_t little_endian ) |
---|
115 | { |
---|
116 | uint32_t n; |
---|
117 | uint32_t res = 0; |
---|
118 | |
---|
119 | if ( little_endian) |
---|
120 | { |
---|
121 | for( n = size ; n > 0 ; n-- ) res = (res<<8) | buffer[offset+n-1]; |
---|
122 | } |
---|
123 | else |
---|
124 | { |
---|
125 | for( n = 0 ; n < size ; n++ ) res = (res<<8) | buffer[offset+n]; |
---|
126 | } |
---|
127 | return res; |
---|
128 | |
---|
129 | } // end fatfs_get_record() |
---|
130 | |
---|
131 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
132 | // This static function retun in the <name> buffer a short name stored in |
---|
133 | // a SFN FATFS directory entry. |
---|
134 | /////////////////////////i//////////////////////////////////////////////////////////////// |
---|
135 | // @ buffer : pointer on buffer containing the directory entry. |
---|
136 | // @ name : [out] buffer allocated by the caller. |
---|
137 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
138 | static void fatfs_get_name_from_short( uint8_t * buffer, |
---|
139 | char * name ) |
---|
140 | { |
---|
141 | uint32_t i; |
---|
142 | uint32_t j = 0; |
---|
143 | |
---|
144 | // get name |
---|
145 | for ( i = 0; i < 8 && buffer[i] != ' '; i++ ) |
---|
146 | { |
---|
147 | name[j] = to_lower( buffer[i] ); |
---|
148 | j++; |
---|
149 | } |
---|
150 | |
---|
151 | // get extension |
---|
152 | for ( i = 8; i < 8 + 3 && buffer[i] != ' '; i++ ) |
---|
153 | { |
---|
154 | // we entered the loop so there is an extension. add the dot |
---|
155 | if ( i == 8 ) |
---|
156 | { |
---|
157 | name[j] = '.'; |
---|
158 | j++; |
---|
159 | } |
---|
160 | |
---|
161 | name[j] = to_lower( buffer[i] ); |
---|
162 | j++; |
---|
163 | } |
---|
164 | |
---|
165 | name[j] = '\0'; |
---|
166 | } |
---|
167 | |
---|
168 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
169 | // This static function retun in the <name> buffer a partial name stored in |
---|
170 | // a LFN FATFS directory entry. |
---|
171 | /////////////////////////i//////////////////////////////////////////////////////////////// |
---|
172 | // @ buffer : pointer on buffer containing the directory entry. |
---|
173 | // @ name : [out] buffer allocated by the caller. |
---|
174 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
175 | static void fatfs_get_name_from_long( uint8_t * buffer, |
---|
176 | char * name ) |
---|
177 | { |
---|
178 | uint32_t name_offset = 0; |
---|
179 | uint32_t buffer_offset = get_length(LDIR_ORD); |
---|
180 | uint32_t l_name_1 = get_length(LDIR_NAME_1); |
---|
181 | uint32_t l_name_2 = get_length(LDIR_NAME_2); |
---|
182 | uint32_t l_name_3 = get_length(LDIR_NAME_3); |
---|
183 | uint32_t l_attr = get_length(LDIR_ATTR); |
---|
184 | uint32_t l_type = get_length(LDIR_TYPE); |
---|
185 | uint32_t l_chksum = get_length(LDIR_CHKSUM); |
---|
186 | uint32_t l_rsvd = get_length(LDIR_RSVD); |
---|
187 | |
---|
188 | uint32_t j = 0; |
---|
189 | uint32_t eof = 0; |
---|
190 | |
---|
191 | while ( (buffer_offset != DIR_ENTRY_SIZE) && (!eof) ) |
---|
192 | { |
---|
193 | while (j != l_name_1 && !eof ) |
---|
194 | { |
---|
195 | if ( (buffer[buffer_offset] == 0x00) || |
---|
196 | (buffer[buffer_offset] == 0xFF) ) |
---|
197 | { |
---|
198 | eof = 1; |
---|
199 | continue; |
---|
200 | } |
---|
201 | name[name_offset] = buffer[buffer_offset]; |
---|
202 | buffer_offset += 2; |
---|
203 | j += 2; |
---|
204 | name_offset++; |
---|
205 | } |
---|
206 | |
---|
207 | buffer_offset += (l_attr + l_type + l_chksum); |
---|
208 | j = 0; |
---|
209 | |
---|
210 | while (j != l_name_2 && !eof ) |
---|
211 | { |
---|
212 | if ( (buffer[buffer_offset] == 0x00) || |
---|
213 | (buffer[buffer_offset] == 0xFF) ) |
---|
214 | { |
---|
215 | eof = 1; |
---|
216 | continue; |
---|
217 | } |
---|
218 | name[name_offset] = buffer[buffer_offset]; |
---|
219 | buffer_offset += 2; |
---|
220 | j += 2; |
---|
221 | name_offset++; |
---|
222 | } |
---|
223 | |
---|
224 | buffer_offset += l_rsvd; |
---|
225 | j = 0; |
---|
226 | |
---|
227 | while (j != l_name_3 && !eof ) |
---|
228 | { |
---|
229 | if ( (buffer[buffer_offset] == 0x00) || |
---|
230 | (buffer[buffer_offset] == 0xFF) ) |
---|
231 | { |
---|
232 | eof = 1; |
---|
233 | continue; |
---|
234 | } |
---|
235 | name[name_offset] = buffer[buffer_offset]; |
---|
236 | buffer_offset += 2; |
---|
237 | j += 2; |
---|
238 | name_offset++; |
---|
239 | } |
---|
240 | } |
---|
241 | name[name_offset] = 0; |
---|
242 | |
---|
243 | } // end get_name_from_long() |
---|
244 | |
---|
245 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
246 | // This function returns the FATFS cluster index of a page identified by its page |
---|
247 | // index in the file, using the FAT mapper. It scans the FAT mapper, starting from the |
---|
248 | // FATFS cluster index allocated to the first page of the file, until it reaches the |
---|
249 | // searched page. The FAT mapper is automatically updated in case of miss. |
---|
250 | // This function can be called by any thread running in any cluster, as it uses the |
---|
251 | // RPC_FATFS_GET_CLUSTER to access the remote FAT mapper if required. |
---|
252 | // We use a RPC to scan the FAT because the RPC_FIFO will avoid contention |
---|
253 | // in the cluster containing the FAT mapper, and the RPC latency is not critical |
---|
254 | // compared to the device access latency. |
---|
255 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
256 | // @ ctx : pointer on local FATFS context. |
---|
257 | // @ first_cluster : first cluster allocated to a file in FATFS. |
---|
258 | // @ page_index : index of searched page in file (one page occupies one cluster). |
---|
259 | // @ cluster_index : [out] pointer on buffer for FATFS cluster index. |
---|
260 | // @ return 0 if success / return EIO if a FAT cluster miss cannot be solved. |
---|
261 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
262 | static error_t fatfs_cluster_from_index( fatfs_ctx_t * ctx, |
---|
263 | uint32_t first_cluster, |
---|
264 | uint32_t page_index, |
---|
265 | uint32_t * cluster_index ) |
---|
266 | { |
---|
267 | uint32_t searched_cluster; // searched FATFS cluster index |
---|
268 | error_t error; |
---|
269 | |
---|
270 | // get extended pointer on FAT mapper |
---|
271 | xptr_t fat_mapper_xp = ctx->fat_mapper_xp; |
---|
272 | |
---|
273 | // get cluster cxy and local pointer on FAT mapper |
---|
274 | cxy_t fat_mapper_cxy = GET_CXY( fat_mapper_xp ); |
---|
275 | mapper_t * fat_mapper_ptr = (mapper_t *)GET_PTR( fat_mapper_xp ); |
---|
276 | |
---|
277 | if( fat_mapper_cxy == local_cxy ) // FAT mapper is local |
---|
278 | { |
---|
279 | error = fatfs_get_cluster( fat_mapper_ptr, |
---|
280 | first_cluster, |
---|
281 | page_index, |
---|
282 | &searched_cluster ); |
---|
283 | } |
---|
284 | else // FAT mapper is remote |
---|
285 | { |
---|
286 | rpc_fatfs_get_cluster_client( fat_mapper_cxy, |
---|
287 | fat_mapper_ptr, |
---|
288 | first_cluster, |
---|
289 | page_index, |
---|
290 | &searched_cluster, |
---|
291 | &error ); |
---|
292 | } |
---|
293 | |
---|
294 | if( error ) |
---|
295 | { |
---|
296 | printk("\n[ERROR] in %s : cannot access FAT\n", __FUNCTION__ ); |
---|
297 | return error; |
---|
298 | } |
---|
299 | |
---|
300 | // return success |
---|
301 | *cluster_index = searched_cluster; |
---|
302 | return 0; |
---|
303 | |
---|
304 | } // end fatfs_cluster_from_index() |
---|
305 | |
---|
306 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
307 | // FATFS specific but extern function (used by RPC_FATFS_GET_CLUSTER) |
---|
308 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
309 | |
---|
310 | ///////////////////////////////////////////// |
---|
311 | error_t fatfs_get_cluster( mapper_t * mapper, |
---|
312 | uint32_t first_cluster, |
---|
313 | uint32_t searched_page, |
---|
314 | uint32_t * cluster ) |
---|
315 | { |
---|
316 | page_t * current_page_desc; // pointer on current page descriptor |
---|
317 | uint32_t * current_page_buffer; // pointer on current page (array of uint32_t) |
---|
318 | uint32_t current_page_index; // index of current page in mapper |
---|
319 | uint32_t current_page_offset; // offset of slot in current page |
---|
320 | uint32_t page_count_in_file; // index of page in file (index in linked list) |
---|
321 | uint32_t current_cluster; // content of current FAT slot |
---|
322 | |
---|
323 | fatfs_dmsg("\n[INFO] %s : enters / first_cluster_id = %d / searched_page = %d\n", |
---|
324 | __FUNCTION__ , first_cluster , searched_page ); |
---|
325 | |
---|
326 | #if CONFIG_FATFS_DEBUG |
---|
327 | uint32_t * buf = (uint32_t *)ppm_page2vaddr( mapper_get_page ( mapper , 0 ) ); |
---|
328 | uint32_t line , word; |
---|
329 | printk("\n*** FAT content for first 128 entries ***\n"); |
---|
330 | for( line = 0 ; line < 8 ; line++ ) |
---|
331 | { |
---|
332 | printk("%d : "); |
---|
333 | for( word = 0 ; word < 16 ; word++ ) printk("%X ", buf[(line<<4) + word] ); |
---|
334 | printk("\n"); |
---|
335 | } |
---|
336 | #endif |
---|
337 | |
---|
338 | // compute number of FAT slots per page |
---|
339 | uint32_t slots_per_page = CONFIG_PPM_PAGE_SIZE >> 2; |
---|
340 | |
---|
341 | // initialize loop variable |
---|
342 | current_page_index = first_cluster / slots_per_page; |
---|
343 | current_page_offset = first_cluster % slots_per_page; |
---|
344 | page_count_in_file = 0; |
---|
345 | |
---|
346 | // scan FAT (i.e. traverse FAT linked list) |
---|
347 | while( page_count_in_file <= searched_page ) |
---|
348 | { |
---|
349 | |
---|
350 | fatfs_dmsg("\n[INFO] %s : page_index = %d / page_offset = %d / count = %d\n", |
---|
351 | __FUNCTION__ , current_page_index , current_page_offset , page_count_in_file ); |
---|
352 | |
---|
353 | // get pointer on current page descriptor |
---|
354 | current_page_desc = mapper_get_page( mapper , current_page_index ); |
---|
355 | |
---|
356 | if( current_page_desc == NULL ) return EIO; |
---|
357 | |
---|
358 | // get pointer on buffer for current page |
---|
359 | current_page_buffer = (uint32_t *)ppm_page2vaddr( current_page_desc ); |
---|
360 | |
---|
361 | // get FAT slot content |
---|
362 | current_cluster = current_page_buffer[current_page_offset]; |
---|
363 | |
---|
364 | // update loop variables |
---|
365 | current_page_index = current_cluster / slots_per_page; |
---|
366 | current_page_offset = current_cluster % slots_per_page; |
---|
367 | page_count_in_file++; |
---|
368 | } |
---|
369 | |
---|
370 | fatfs_dmsg("\n[INFO] %s : exit / cluster_id = %d\n", |
---|
371 | __FUNCTION__ , current_cluster ); |
---|
372 | |
---|
373 | *cluster = current_cluster; |
---|
374 | return 0; |
---|
375 | |
---|
376 | } // end fatfs_get_cluster() |
---|
377 | |
---|
378 | |
---|
379 | |
---|
380 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
381 | // Generic API : the following functions are called by the kernel (VFS) |
---|
382 | // and must be defined by all supported file systems. |
---|
383 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
384 | |
---|
385 | /////////////////////////////// |
---|
386 | fatfs_ctx_t * fatfs_ctx_alloc() |
---|
387 | { |
---|
388 | kmem_req_t req; |
---|
389 | req.type = KMEM_FATFS_CTX; |
---|
390 | req.size = sizeof(fatfs_ctx_t); |
---|
391 | req.flags = AF_KERNEL | AF_ZERO; |
---|
392 | |
---|
393 | return (fatfs_ctx_t *)kmem_alloc( &req ); |
---|
394 | } |
---|
395 | |
---|
396 | ////////////////////////////////////////////// |
---|
397 | void fatfs_ctx_init( fatfs_ctx_t * fatfs_ctx ) |
---|
398 | { |
---|
399 | error_t error; |
---|
400 | kmem_req_t req; |
---|
401 | uint8_t * buffer; |
---|
402 | |
---|
403 | fatfs_dmsg("\n[INFO] %s : enters for fatfs_ctx = %x\n", |
---|
404 | __FUNCTION__ , fatfs_ctx ); |
---|
405 | |
---|
406 | assert( (fatfs_ctx != NULL) , __FUNCTION__ , |
---|
407 | "cannot allocate memory for FATFS context\n" ); |
---|
408 | |
---|
409 | // allocate a 512 bytes buffer to store the boot record |
---|
410 | req.type = KMEM_512_BYTES; |
---|
411 | req.flags = AF_KERNEL | AF_ZERO; |
---|
412 | buffer = (uint8_t *)kmem_alloc( &req ); |
---|
413 | |
---|
414 | assert( (buffer != NULL) , __FUNCTION__ , |
---|
415 | "cannot allocate memory for 512 bytes buffer\n" ); |
---|
416 | |
---|
417 | // load the boot record from device |
---|
418 | // using a synchronous access to IOC device |
---|
419 | error = dev_ioc_sync_read( buffer , 0 , 1 ); |
---|
420 | |
---|
421 | assert( (error == 0) , __FUNCTION__ , |
---|
422 | "cannot access boot record\n" ); |
---|
423 | |
---|
424 | #if (CONFIG_FATFS_DEBUG > 1) |
---|
425 | uint32_t line; |
---|
426 | uint32_t byte = 0; |
---|
427 | printk("\n*** boot record at cycle %d ***\n", hal_get_cycles() ); |
---|
428 | for ( line = 0 ; line < 32 ; line++ ) |
---|
429 | { |
---|
430 | printk(" %X | %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x |\n", |
---|
431 | byte, |
---|
432 | buffer[byte+ 0],buffer[byte+ 1],buffer[byte+ 2],buffer[byte+ 3], |
---|
433 | buffer[byte+ 4],buffer[byte+ 5],buffer[byte+ 6],buffer[byte+ 7], |
---|
434 | buffer[byte+ 8],buffer[byte+ 9],buffer[byte+10],buffer[byte+11], |
---|
435 | buffer[byte+12],buffer[byte+13],buffer[byte+14],buffer[byte+15] ); |
---|
436 | |
---|
437 | byte += 16; |
---|
438 | } |
---|
439 | #endif |
---|
440 | |
---|
441 | // check sector size from boot record |
---|
442 | uint32_t sector_size = fatfs_get_record( BPB_BYTSPERSEC , buffer , 1 ); |
---|
443 | |
---|
444 | nolock_assert( (sector_size == 512) , __FUNCTION__ , |
---|
445 | "sector size must be 512 bytes\n" ); |
---|
446 | |
---|
447 | // check cluster size from boot record |
---|
448 | uint32_t nb_sectors = fatfs_get_record( BPB_SECPERCLUS , buffer , 1 ); |
---|
449 | |
---|
450 | nolock_assert( (nb_sectors == 8) , __FUNCTION__ , |
---|
451 | "cluster size must be 8 sectors\n" ); |
---|
452 | |
---|
453 | // check number of FAT copies from boot record |
---|
454 | uint32_t nb_fats = fatfs_get_record( BPB_NUMFATS , buffer , 1 ); |
---|
455 | |
---|
456 | nolock_assert( (nb_fats == 1) , __FUNCTION__ , |
---|
457 | "number of FAT copies must be 1\n" ); |
---|
458 | |
---|
459 | // get & check number of sectors in FAT from boot record |
---|
460 | uint32_t fat_sectors = fatfs_get_record( BPB_FAT32_FATSZ32 , buffer , 1 ); |
---|
461 | |
---|
462 | nolock_assert( ((fat_sectors & 0xF) == 0) , __FUNCTION__ , |
---|
463 | "FAT not multiple of 16 sectors\n"); |
---|
464 | |
---|
465 | // get and check root cluster from boot record |
---|
466 | uint32_t root_cluster = fatfs_get_record( BPB_FAT32_ROOTCLUS , buffer , 1 ); |
---|
467 | |
---|
468 | nolock_assert( (root_cluster == 2) , __FUNCTION__ , |
---|
469 | "root cluster index must be 2\n"); |
---|
470 | |
---|
471 | // get FAT lba from boot record |
---|
472 | uint32_t fat_lba = fatfs_get_record( BPB_RSVDSECCNT , buffer , 1 ); |
---|
473 | |
---|
474 | // release the 512 bytes buffer |
---|
475 | req.type = KMEM_512_BYTES; |
---|
476 | req.ptr = buffer; |
---|
477 | kmem_free( &req ); |
---|
478 | |
---|
479 | // allocate a mapper for the FAT itself |
---|
480 | mapper_t * fat_mapper = mapper_create( FS_TYPE_FATFS ); |
---|
481 | |
---|
482 | assert( (fat_mapper != NULL) , __FUNCTION__ , "no memory for FAT mapper" ); |
---|
483 | |
---|
484 | // WARNING : the inode field MUST be NULL for the FAT mapper |
---|
485 | fat_mapper->inode = NULL; |
---|
486 | |
---|
487 | // initialize the FATFS context |
---|
488 | fatfs_ctx->fat_begin_lba = fat_lba; |
---|
489 | fatfs_ctx->fat_sectors_count = fat_sectors; |
---|
490 | fatfs_ctx->bytes_per_sector = sector_size; |
---|
491 | fatfs_ctx->sectors_per_cluster = nb_sectors; |
---|
492 | fatfs_ctx->cluster_begin_lba = fat_lba + fat_sectors; |
---|
493 | fatfs_ctx->root_dir_cluster = 2; |
---|
494 | fatfs_ctx->last_allocated_sector = 0; // TODO ??? |
---|
495 | fatfs_ctx->last_allocated_index = 0; // TODO ??? |
---|
496 | fatfs_ctx->fat_mapper_xp = XPTR( local_cxy , fat_mapper ); |
---|
497 | |
---|
498 | } // end fatfs_ctx_init() |
---|
499 | |
---|
500 | ///////////////////////////////////////////////// |
---|
501 | void fatfs_ctx_destroy( fatfs_ctx_t * fatfs_ctx ) |
---|
502 | { |
---|
503 | kmem_req_t req; |
---|
504 | req.type = KMEM_FATFS_CTX; |
---|
505 | req.ptr = fatfs_ctx; |
---|
506 | kmem_free( &req ); |
---|
507 | } |
---|
508 | |
---|
509 | ////////////////////////////////////////////// |
---|
510 | error_t fatfs_mapper_move_page( page_t * page, |
---|
511 | bool_t to_mapper ) |
---|
512 | { |
---|
513 | error_t error; |
---|
514 | |
---|
515 | // get pointer on source mapper and page index from page descriptor |
---|
516 | mapper_t * mapper = page->mapper; |
---|
517 | uint32_t index = page->index; |
---|
518 | |
---|
519 | // get VFS inode pointer from mapper |
---|
520 | vfs_inode_t * inode = mapper->inode; |
---|
521 | |
---|
522 | fatfs_dmsg("\n[INFO] %s : enter for inode %x / page_index = %d / mapper = %x\n", |
---|
523 | __FUNCTION__ , inode , index , mapper ); |
---|
524 | |
---|
525 | // get memory buffer base address |
---|
526 | uint8_t * buffer = (uint8_t *)ppm_page2vaddr( page ); |
---|
527 | |
---|
528 | // get number of sectors from FATFS context |
---|
529 | fatfs_ctx_t * fatfs_ctx = (fatfs_ctx_t *)fs_context[FS_TYPE_FATFS].extend; |
---|
530 | uint32_t count = fatfs_ctx->sectors_per_cluster; |
---|
531 | |
---|
532 | // analyse the mapper type : FAT or normal inode |
---|
533 | if( inode == NULL ) // it is the FAT mapper |
---|
534 | { |
---|
535 | // get lba from page index |
---|
536 | uint32_t lba = fatfs_ctx->fat_begin_lba + (count * index); |
---|
537 | |
---|
538 | fatfs_dmsg("\n[INFO] %s : for FAT / lba = %d\n", |
---|
539 | __FUNCTION__ , lba ); |
---|
540 | |
---|
541 | // access device |
---|
542 | if( to_mapper ) error = dev_ioc_sync_read ( buffer , lba , count ); |
---|
543 | else error = dev_ioc_write( buffer , lba , count ); |
---|
544 | |
---|
545 | if( error ) return EIO; |
---|
546 | |
---|
547 | fatfs_dmsg("\n[INFO] %s : exit for FAT / page_index = %d / mapper = %x\n", |
---|
548 | __FUNCTION__ , index , mapper ); |
---|
549 | } |
---|
550 | else // it is a normal inode mapper |
---|
551 | { |
---|
552 | // get first cluster index from inode extension |
---|
553 | uint32_t first_cluster = (uint32_t)(intptr_t)inode->extend; |
---|
554 | |
---|
555 | fatfs_dmsg("\n[INFO] %s : for inode %x / first cluster_id = %d\n", |
---|
556 | __FUNCTION__ , inode , first_cluster ); |
---|
557 | |
---|
558 | // compute FATFS_cluster index for the accessed page |
---|
559 | uint32_t cluster = 0; |
---|
560 | error_t error = fatfs_cluster_from_index( fatfs_ctx, |
---|
561 | first_cluster, |
---|
562 | index, |
---|
563 | &cluster ); |
---|
564 | if( error ) return EIO; |
---|
565 | |
---|
566 | // get lba from cluster |
---|
567 | uint32_t lba = fatfs_lba_from_cluster( fatfs_ctx , cluster ); |
---|
568 | |
---|
569 | fatfs_dmsg("\n[INFO] %s : for inode %x / page = %d / cluster_id = %d / lba = %x\n", |
---|
570 | __FUNCTION__ , inode , index , cluster , lba ); |
---|
571 | |
---|
572 | // access device |
---|
573 | if( to_mapper ) error = dev_ioc_sync_read ( buffer , lba , count ); |
---|
574 | else error = dev_ioc_write( buffer , lba , count ); |
---|
575 | |
---|
576 | if( error ) return EIO; |
---|
577 | |
---|
578 | fatfs_dmsg("\n[INFO] %s : exit for inode %x / page = %x / mapper = %x\n", |
---|
579 | __FUNCTION__ , inode , page , mapper ); |
---|
580 | } |
---|
581 | |
---|
582 | return 0; |
---|
583 | |
---|
584 | } // end fatfs_mapper_move_page() |
---|
585 | |
---|
586 | ///////////////////////////////////////////////////////////////// |
---|
587 | error_t fatfs_inode_load( vfs_inode_t * parent_inode, |
---|
588 | char * name, |
---|
589 | xptr_t child_inode_xp ) |
---|
590 | { |
---|
591 | // Two embedded loops: |
---|
592 | // - scan the parent mapper pages |
---|
593 | // - scan the directory entries in each 4 Kbytes page |
---|
594 | |
---|
595 | fatfs_dmsg("\n[INFO] %s : enter for child <%s> in parent inode %l\n", |
---|
596 | __FUNCTION__ , name , XPTR( local_cxy , parent_inode ) ); |
---|
597 | |
---|
598 | mapper_t * mapper = parent_inode->mapper; |
---|
599 | |
---|
600 | assert( (mapper != NULL) , __FUNCTION__ , "parent mapper undefined\n"); |
---|
601 | |
---|
602 | char cname[CONFIG_VFS_MAX_NAME_LENGTH]; // name extracter from each directory entry |
---|
603 | |
---|
604 | char lfn1[16]; // buffer for one partial cname |
---|
605 | char lfn2[16]; // buffer for one partial cname |
---|
606 | char lfn3[16]; // buffer for one partial cname |
---|
607 | page_t * page; // pointer on current page descriptor |
---|
608 | uint8_t * base; // pointer on current page base |
---|
609 | uint32_t offset = 0; // byte offset in page |
---|
610 | uint32_t index = 0; // page index in mapper |
---|
611 | uint32_t attr; // directory entry ATTR field |
---|
612 | uint32_t ord; // directory entry ORD field |
---|
613 | uint32_t seq; // sequence index |
---|
614 | uint32_t lfn = 0; // LFN entries number |
---|
615 | uint32_t size = 0; // searched file/dir size (bytes) |
---|
616 | uint32_t cluster = 0; // searched file/dir cluster index |
---|
617 | uint32_t is_dir = 0; // searched file/dir type |
---|
618 | uint32_t dentry; // directory entry index |
---|
619 | int32_t found = 0; // not found (0) / name found (1) / end of dir (-1) |
---|
620 | |
---|
621 | // scan the parent directory mapper |
---|
622 | while ( found == 0 ) |
---|
623 | { |
---|
624 | // get one page |
---|
625 | page = mapper_get_page( mapper , index ); |
---|
626 | |
---|
627 | assert( (page != NULL) , __FUNCTION__ , "bad parent mapper\n"); |
---|
628 | |
---|
629 | // get page base |
---|
630 | base = ppm_page2vaddr( page ); |
---|
631 | |
---|
632 | // scan this page until end of directory, end of page, or name found |
---|
633 | while( (offset < 4096) && (found == 0) ) |
---|
634 | { |
---|
635 | attr = fatfs_get_record( DIR_ATTR , base + offset , 0 ); |
---|
636 | ord = fatfs_get_record( LDIR_ORD , base + offset , 0 ); |
---|
637 | |
---|
638 | if (ord == NO_MORE_ENTRY) // no more entry => break |
---|
639 | { |
---|
640 | found = -1; |
---|
641 | } |
---|
642 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
643 | { |
---|
644 | offset = offset + 32; |
---|
645 | } |
---|
646 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => get partial cname |
---|
647 | { |
---|
648 | seq = ord & 0x3; |
---|
649 | lfn = (seq > lfn) ? seq : lfn; |
---|
650 | if ( seq == 1 ) fatfs_get_name_from_long( base + offset, lfn1 ); |
---|
651 | else if ( seq == 2 ) fatfs_get_name_from_long( base + offset, lfn2 ); |
---|
652 | else if ( seq == 3 ) fatfs_get_name_from_long( base + offset, lfn3 ); |
---|
653 | offset = offset + 32; |
---|
654 | } |
---|
655 | else // NORMAL entry |
---|
656 | { |
---|
657 | // build the extracted name |
---|
658 | if ( lfn == 0 ) |
---|
659 | { |
---|
660 | fatfs_get_name_from_short( base + offset , cname ); |
---|
661 | } |
---|
662 | else if ( lfn == 1 ) |
---|
663 | { |
---|
664 | strcpy( cname , lfn1 ); |
---|
665 | } |
---|
666 | else if ( lfn == 2 ) |
---|
667 | { |
---|
668 | strcpy( cname , lfn1 ); |
---|
669 | strcpy( cname + 13 , lfn2 ); |
---|
670 | } |
---|
671 | else if ( lfn == 3 ) |
---|
672 | { |
---|
673 | strcpy( cname , lfn1 ); |
---|
674 | strcpy( cname + 13 , lfn2 ); |
---|
675 | strcpy( cname + 26 , lfn3 ); |
---|
676 | } |
---|
677 | |
---|
678 | // get dentry arguments if extracted cname == searched name |
---|
679 | if ( strcmp( name , cname ) == 0 ) |
---|
680 | { |
---|
681 | cluster = (fatfs_get_record( DIR_FST_CLUS_HI , base + offset , 1 ) << 16) | |
---|
682 | (fatfs_get_record( DIR_FST_CLUS_LO , base + offset , 1 ) ) ; |
---|
683 | dentry = ((index<<12) + offset)>>5; |
---|
684 | is_dir = ((attr & ATTR_DIRECTORY) == ATTR_DIRECTORY); |
---|
685 | size = fatfs_get_record( DIR_FILE_SIZE , base + offset , 1 ); |
---|
686 | found = 1; |
---|
687 | } |
---|
688 | offset = offset + 32; |
---|
689 | lfn = 0; |
---|
690 | } |
---|
691 | } // end loop on directory entries |
---|
692 | index++; |
---|
693 | offset = 0; |
---|
694 | } // end loop on pages |
---|
695 | |
---|
696 | // analyse the result of scan |
---|
697 | |
---|
698 | if ( found == -1 ) // found end of directory => failure |
---|
699 | { |
---|
700 | fatfs_dmsg("\n[INFO] %s : exit / child <%s> not found in parent inode %l\n", |
---|
701 | __FUNCTION__ , name , XPTR( local_cxy , parent_inode ) ); |
---|
702 | |
---|
703 | return ENOENT; |
---|
704 | } |
---|
705 | else // found searched child name |
---|
706 | { |
---|
707 | // get child inode cluster and local pointer |
---|
708 | cxy_t child_cxy = GET_CXY( child_inode_xp ); |
---|
709 | vfs_inode_t * child_ptr = (vfs_inode_t *)GET_PTR( child_inode_xp ); |
---|
710 | |
---|
711 | // update the child inode "type", "size", and "extend" fields |
---|
712 | vfs_inode_type_t type = (is_dir) ? INODE_TYPE_DIR : INODE_TYPE_FILE; |
---|
713 | |
---|
714 | hal_remote_sw( XPTR( child_cxy , &child_ptr->type ) , type ); |
---|
715 | hal_remote_sw( XPTR( child_cxy , &child_ptr->size ) , size ); |
---|
716 | hal_remote_sw( XPTR( child_cxy , &child_ptr->extend ) , cluster ); |
---|
717 | |
---|
718 | fatfs_dmsg("\n[INFO] %s : exit / child <%s> found in parent inode %l\n", |
---|
719 | __FUNCTION__ , name , XPTR( local_cxy , parent_inode ) ); |
---|
720 | |
---|
721 | return 0; |
---|
722 | } |
---|
723 | } // end fatfs_inode_load() |
---|