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