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