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