source: trunk/kernel/fs/fatfs.c @ 409

Last change on this file since 409 was 407, checked in by alain, 6 years ago

First implementation of fork/exec.

File size: 27.0 KB
Line 
1/*
2 * fatfs.c - FATFS file system API implementation.
3 *
4 * Author    Alain Greiner (2016,2017)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24
25#include <hal_types.h>
26#include <hal_special.h>
27#include <printk.h>
28#include <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
44extern vfs_ctx_t          fs_context[FS_TYPES_NR];   // allocated in vfs.c file
45
46extern 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
57static inline int get_length( int offset , int length ) { return length; }
58
59static inline int get_offset( int offset , int length ) { return offset; }
60
61
62//////////////////////////////////////////////////////////////////////////////////////////
63// This 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//////////////////////////////////////////////////////////////////////////////////////////
70static 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//////////////////////////////////////////////////////////////////////////////////////////
86static 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//////////////////////////////////////////////////////////////////////////////////////////
113static 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//////////////////////////////////////////////////////////////////////////////////////////
150static 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//////////////////////////////////////////////////////////////////////////////////////////
226void 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/////////////////////////////////////////////
249error_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
264fatfs_dmsg("\n[DBG] %s : core[%x,%d] enters / first_cluster_id = %d / searched_index = %d\n",
265__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, first_cluster_id, searched_page_index );
266
267    // get number of FAT slots per page
268    uint32_t slots_per_page = CONFIG_PPM_PAGE_SIZE >> 2;
269
270    // initialize loop variable
271    current_page_index  = first_cluster_id / slots_per_page;
272    current_page_offset = first_cluster_id % slots_per_page;
273    page_count_in_file  = 0;
274    next_cluster_id     = 0xFFFFFFFF;
275
276    // scan FAT (i.e. traverse FAT linked list)
277    while( page_count_in_file < searched_page_index )
278    {
279        // get pointer on current page descriptor
280        current_page_desc = mapper_get_page( mapper , current_page_index );
281
282        if( current_page_desc == NULL ) return EIO;
283
284        // get pointer on buffer for current page
285        xptr_t base_xp = ppm_page2base( XPTR( local_cxy , current_page_desc ) );
286        current_page_buffer = (uint32_t *)GET_PTR( base_xp );
287
288        // get FAT slot content
289        next_cluster_id = current_page_buffer[current_page_offset];
290
291fatfs_dmsg("\n[DBG] %s : core[%x,%d] traverse FAT / current_page_index = %d\n"
292"current_page_offset = %d / next_cluster_id = %d\n",
293__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, current_page_index,
294current_page_offset , next_cluster_id );
295
296        // update loop variables
297        current_page_index  = next_cluster_id / slots_per_page;
298        current_page_offset = next_cluster_id % slots_per_page;
299        page_count_in_file++;
300    }
301
302    if( next_cluster_id == 0xFFFFFFFF ) return EIO;
303   
304fatfs_dmsg("\n[DBG] %s : core[%x;%d] exit / cluster_id = %d\n",
305__FUNCTION__ , local_cxy, CURRENT_THREAD->core->lid, next_cluster_id );
306
307    *searched_cluster_id = next_cluster_id;
308    return 0;
309
310}  // end fatfs_get_cluster()
311
312
313
314///////////////////////////////////////////////////////////////////////////////////////
315// Generic API : the following functions are called by the kernel (VFS)
316//               and must be defined by all supported file systems.
317///////////////////////////////////////////////////////////////////////////////////////
318
319///////////////////////////////
320fatfs_ctx_t * fatfs_ctx_alloc()
321{
322    kmem_req_t    req;
323        req.type    = KMEM_FATFS_CTX;
324        req.size    = sizeof(fatfs_ctx_t);
325    req.flags   = AF_KERNEL | AF_ZERO;
326
327        return (fatfs_ctx_t *)kmem_alloc( &req );
328}
329
330//////////////////////////////////////////////
331void fatfs_ctx_init( fatfs_ctx_t * fatfs_ctx )
332{
333    error_t       error;
334    kmem_req_t    req;
335    uint8_t     * buffer;
336
337    fatfs_dmsg("\n[DBG] %s : enter for fatfs_ctx = %x\n",
338               __FUNCTION__ , fatfs_ctx );
339
340    assert( (fatfs_ctx != NULL) , __FUNCTION__ ,
341                   "cannot allocate memory for FATFS context\n" );
342
343    // allocate a 512 bytes buffer to store the boot record
344        req.type    = KMEM_512_BYTES;
345    req.flags   = AF_KERNEL | AF_ZERO;
346        buffer      = (uint8_t *)kmem_alloc( &req );
347
348    assert( (buffer != NULL) , __FUNCTION__ ,
349                   "cannot allocate memory for 512 bytes buffer\n" );
350     
351    fatfs_dmsg("\n[DBG] %s : allocated 512 bytes buffer\n", __FUNCTION__ );
352
353    // load the boot record from device
354    // using a synchronous access to IOC device 
355    error = dev_ioc_sync_read( buffer , 0 , 1 );
356
357fatfs_dmsg("\n[DBG] %s : buffer loaded\n", __FUNCTION__ );
358
359    assert( (error == 0) , __FUNCTION__ , "cannot access boot record\n" );
360
361#if (CONFIG_FATFS_DEBUG & 0x1)
362if( hal_time_stamp() > CONFIG_FATFS_DEBUG )
363{
364    uint32_t   line;
365    uint32_t   byte = 0;
366    printk("\n***** %s : FAT boot record\n", __FUNCTION__ );
367    for ( line = 0 ; line < 32 ; line++ )
368    {
369        printk(" %X | %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x |\n",
370               byte,
371               buffer[byte+ 0],buffer[byte+ 1],buffer[byte+ 2],buffer[byte+ 3],
372               buffer[byte+ 4],buffer[byte+ 5],buffer[byte+ 6],buffer[byte+ 7],
373               buffer[byte+ 8],buffer[byte+ 9],buffer[byte+10],buffer[byte+11],
374               buffer[byte+12],buffer[byte+13],buffer[byte+14],buffer[byte+15] );
375
376         byte += 16;
377    }
378}
379#endif
380
381    // check sector size from boot record
382    uint32_t sector_size = fatfs_get_record( BPB_BYTSPERSEC , buffer , 1 );
383
384    assert( (sector_size == 512) , __FUNCTION__ ,
385            "sector size must be 512 bytes\n" );
386
387    // check cluster size from boot record
388    uint32_t nb_sectors = fatfs_get_record( BPB_SECPERCLUS , buffer , 1 );
389
390    assert( (nb_sectors == 8) , __FUNCTION__ ,
391            "cluster size must be 8 sectors\n" );
392
393    // check number of FAT copies from boot record
394    uint32_t nb_fats = fatfs_get_record( BPB_NUMFATS , buffer , 1 );
395
396    assert( (nb_fats == 1) , __FUNCTION__ ,
397            "number of FAT copies must be 1\n" );
398
399    // get & check number of sectors in FAT from boot record
400    uint32_t fat_sectors = fatfs_get_record( BPB_FAT32_FATSZ32 , buffer , 1 );
401
402    assert( ((fat_sectors & 0xF) == 0) , __FUNCTION__ ,
403            "FAT not multiple of 16 sectors\n");
404
405    // get and check root cluster from boot record
406    uint32_t root_cluster = fatfs_get_record( BPB_FAT32_ROOTCLUS , buffer , 1 );
407
408    assert( (root_cluster == 2) , __FUNCTION__ ,
409            "root cluster index must be  2\n");
410
411    // get FAT lba from boot record
412    uint32_t fat_lba = fatfs_get_record( BPB_RSVDSECCNT , buffer , 1 );
413
414    // release the 512 bytes buffer
415    req.type = KMEM_512_BYTES;
416    req.ptr  = buffer;
417    kmem_free( &req );
418
419    fatfs_dmsg("\n[DBG] %s : boot record read & released\n",
420               __FUNCTION__ );
421
422    // allocate a mapper for the FAT itself
423    mapper_t * fat_mapper = mapper_create( FS_TYPE_FATFS );
424
425    assert( (fat_mapper != NULL) , __FUNCTION__ , "no memory for FAT mapper" );
426
427    // WARNING : the inode field MUST be NULL for the FAT mapper
428    fat_mapper->inode = NULL;
429
430    // initialize the FATFS context
431    fatfs_ctx->fat_begin_lba         = fat_lba;
432    fatfs_ctx->fat_sectors_count     = fat_sectors; 
433    fatfs_ctx->bytes_per_sector      = sector_size;
434    fatfs_ctx->sectors_per_cluster   = nb_sectors;
435    fatfs_ctx->cluster_begin_lba     = fat_lba + fat_sectors;
436    fatfs_ctx->root_dir_cluster      = 2;
437    fatfs_ctx->last_allocated_sector = 0;    // TODO ???
438    fatfs_ctx->last_allocated_index  = 0;    // TODO ???
439    fatfs_ctx->fat_mapper_xp         = XPTR( local_cxy , fat_mapper );
440
441fatfs_dmsg("\n[DBG] %s : exit for fatfs_ctx = %x\n", __FUNCTION__ , fatfs_ctx );
442
443}  // end fatfs_ctx_init()
444
445/////////////////////////////////////////////////
446void fatfs_ctx_destroy( fatfs_ctx_t * fatfs_ctx )
447{
448    kmem_req_t    req;
449    req.type = KMEM_FATFS_CTX;
450    req.ptr  = fatfs_ctx;
451    kmem_free( &req );
452}
453
454//////////////////////////////////////////////
455error_t fatfs_mapper_move_page( page_t * page,
456                                bool_t   to_mapper )
457{
458    error_t       error;
459    vfs_inode_t * inode;
460    mapper_t    * mapper;
461    uint32_t      index;       // page index in mapper
462    uint8_t     * buffer;      // page base address in mapper
463    uint32_t      count;       // number of sectors in a page
464    uint32_t      lba;         // block address on device
465    fatfs_ctx_t * fatfs_ctx;   // pointer on local FATFS context
466
467    // get pointer on mapper and page index from page descriptor
468    mapper = page->mapper;
469    index  = page->index;
470
471    // get inode pointer from mapper
472    inode = mapper->inode;
473
474fatfs_dmsg("\n[DBG] %s : core[%x,%d] enter for page %d / inode %x / mapper %x\n",
475__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , index , inode , mapper );
476
477    // get page base address
478    xptr_t base_xp = ppm_page2base( XPTR( local_cxy , page ) );
479    buffer = (uint8_t *)GET_PTR( base_xp );
480 
481    // get number of sectors for one page (from FATFS context)
482    fatfs_ctx = (fatfs_ctx_t *)fs_context[FS_TYPE_FATFS].extend;
483    count = fatfs_ctx->sectors_per_cluster;
484
485    // test FAT/normal inode
486    if( inode == NULL )      // it is the FAT mapper
487    {
488        // get lba from page index
489        lba = fatfs_ctx->fat_begin_lba + (count * index);
490 
491fatfs_dmsg("\n[DBG] %s : core[%x,%d] access FAT on device / lba = %d\n",
492__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , lba );
493
494        // access device
495        if( to_mapper ) error = dev_ioc_sync_read ( buffer , lba , count );
496        else            error = dev_ioc_write( buffer , lba , count );     
497
498        if( error ) return EIO;
499    }
500    else                     // it is a normal inode mapper
501    {
502        uint32_t  searched_cluster_id;
503
504        // get first_cluster_id from inode extension
505        uint32_t  first_cluster_id = (uint32_t)(intptr_t)inode->extend;
506
507        // compute cluster_id
508        if( index == 0 )            // no need to access FAT mapper
509        {
510            searched_cluster_id = first_cluster_id;
511        }
512        else                        // FAT mapper access required
513        {
514            // get cluster and local pointer on FAT mapper
515            xptr_t     fat_mapper_xp  = fatfs_ctx->fat_mapper_xp;
516            cxy_t      fat_mapper_cxy = GET_CXY( fat_mapper_xp );
517            mapper_t * fat_mapper_ptr = (mapper_t *)GET_PTR( fat_mapper_xp );
518
519            // access FAT mapper
520            if( fat_mapper_cxy == local_cxy )    // FAT mapper is local
521            {
522
523fatfs_dmsg("\n[DBG] %s : core[%x,%d] access local FAT mapper\n"
524"fat_mapper_cxy = %x / fat_mapper_ptr = %x / first_cluster_id = %d / index = %d\n",
525__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid ,
526fat_mapper_cxy , fat_mapper_ptr , first_cluster_id , index );
527
528                error = fatfs_get_cluster( fat_mapper_ptr,
529                                           first_cluster_id,
530                                           index,
531                                           &searched_cluster_id );
532            }
533            else                                 // FAT mapper is remote
534            {
535
536fatfs_dmsg("\n[DBG] %s : core[%x,%d] access remote FAT mapper\n"
537"fat_mapper_cxy = %x / fat_mapper_ptr = %x / first_cluster_id = %d / index = %d\n",
538__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid ,
539fat_mapper_cxy , fat_mapper_ptr , first_cluster_id , index );
540
541                rpc_fatfs_get_cluster_client( fat_mapper_cxy,
542                                              fat_mapper_ptr,
543                                              first_cluster_id,
544                                              index,
545                                              &searched_cluster_id,
546                                              &error );
547            }
548
549            if( error )  return EIO;
550        }
551
552fatfs_dmsg("\n[DBG] %s : core[%x,%d] access device for inode %x / cluster_id %d\n",
553__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , inode , searched_cluster_id );
554
555        // get lba from cluster_id
556        lba = fatfs_lba_from_cluster( fatfs_ctx , searched_cluster_id );
557
558        // access device
559        if( to_mapper ) error = dev_ioc_sync_read ( buffer , lba , count );
560        else            error = dev_ioc_write( buffer , lba , count );     
561
562        if( error ) return EIO;
563    }
564
565fatfs_dmsg("\n[DBG] %s : core[%x,%d] exit for page %d / inode %x / mapper %x\n",
566__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , index , inode , mapper );
567
568#if (CONFIG_FATFS_DEBUG & 0x1)
569if( hal_time_stamp() > CONFIG_FATFS_DEBUG )
570{
571    uint32_t * tab = (uint32_t *)buffer;
572    uint32_t line , word;
573    printk("\n***** %s : First 64 words of loaded page\n", __FUNCTION__ );
574    for( line = 0 ; line < 8 ; line++ )
575    {
576        printk("%X : ", line );
577        for( word = 0 ; word < 8 ; word++ ) printk("%X ", tab[(line<<3) + word] );
578        printk("\n");
579    }
580}
581#endif
582
583    return 0;
584
585}  // end fatfs_mapper_move_page()
586
587/////////////////////////////////////////////////////
588error_t fatfs_inode_load( vfs_inode_t * parent_inode,
589                          char        * name,
590                          xptr_t        child_inode_xp )
591{
592    // Two embedded loops:
593    // - scan the parent mapper pages
594    // - scan the directory entries in each 4 Kbytes page
595
596fatfs_dmsg("\n[DBG] %s : enter for child <%s> in parent inode %l\n",
597__FUNCTION__ , name , XPTR( local_cxy , parent_inode ) );
598
599    mapper_t * mapper = parent_inode->mapper;
600
601    assert( (mapper != NULL) , __FUNCTION__ , "parent mapper undefined\n");
602   
603    char       cname[CONFIG_VFS_MAX_NAME_LENGTH];  // name extracter from each directory entry
604
605    char       lfn1[16];         // buffer for one partial cname
606    char       lfn2[16];         // buffer for one partial cname
607    char       lfn3[16];         // buffer for one partial cname
608    page_t   * page;             // pointer on current page descriptor
609    uint8_t  * base;             // pointer on current page base
610    uint32_t   offset  = 0;      // byte offset in page
611    uint32_t   index   = 0;      // page index in mapper
612    uint32_t   attr;             // directory entry ATTR field
613    uint32_t   ord;              // directory entry ORD field
614    uint32_t   seq;              // sequence index
615    uint32_t   lfn     = 0;      // LFN entries number
616    uint32_t   size    = 0;      // searched file/dir size (bytes)
617    uint32_t   cluster = 0;      // searched file/dir cluster index
618    uint32_t   is_dir  = 0;      // searched file/dir type
619    uint32_t   dentry;           // directory entry index
620    int32_t    found   = 0;      // not found (0) / name found (1) / end of dir (-1)
621
622    // scan the parent directory mapper
623    while ( found == 0 )
624    {
625        // get one page
626        page = mapper_get_page( mapper , index );
627
628        assert( (page != NULL) , __FUNCTION__ , "bad parent mapper\n");
629
630        // get page base
631        xptr_t base_xp = ppm_page2base( XPTR( local_cxy , page ) );
632        base = (uint8_t *)GET_PTR( base_xp );
633
634#if (CONFIG_FATFS_DEBUG & 0x1)
635if( hal_time_stamp() > CONFIG_FATFS_DEBUG )
636{
637    uint32_t * buf = (uint32_t *)base;
638    uint32_t line , word;
639    printk("\n***** %s : First 16 dentries for parent inode %x\n",
640    __FUNCTION__ , parent_inode );
641    for( line = 0 ; line < 16 ; line++ )
642    {
643        printk("%X : ", line );
644        for( word = 0 ; word < 8 ; word++ ) printk("%X ", buf[(line<<4) + word] );
645        printk("\n");
646    }
647}
648#endif
649        // scan this page until end of directory, end of page, or name found
650        while( (offset < 4096) && (found == 0) )
651        {
652            attr = fatfs_get_record( DIR_ATTR , base + offset , 0 );   
653            ord  = fatfs_get_record( LDIR_ORD , base + offset , 0 );   
654
655            if (ord == NO_MORE_ENTRY)                 // no more entry => break
656            {
657                found = -1;
658            }
659            else if ( ord == FREE_ENTRY )             // free entry => skip
660            {
661                offset = offset + 32;
662            }
663            else if ( attr == ATTR_LONG_NAME_MASK )   // LFN entry => get partial cname
664            {
665                seq = ord & 0x3;
666                lfn = (seq > lfn) ? seq : lfn;   
667                if      ( seq == 1 ) fatfs_get_name_from_long( base + offset, lfn1 );
668                else if ( seq == 2 ) fatfs_get_name_from_long( base + offset, lfn2 );
669                else if ( seq == 3 ) fatfs_get_name_from_long( base + offset, lfn3 );
670                offset = offset + 32;
671            }
672            else                                 // NORMAL entry
673            {
674                // build the extracted name
675                if      ( lfn == 0 )
676                {
677                    fatfs_get_name_from_short( base + offset , cname );
678                }
679                else if ( lfn == 1 )
680                {
681                    strcpy( cname      , lfn1 );
682                }   
683                else if ( lfn == 2 ) 
684                {
685                    strcpy( cname      , lfn1 );
686                    strcpy( cname + 13 , lfn2 );
687                }
688                else if ( lfn == 3 ) 
689                {
690                    strcpy( cname      , lfn1 );
691                    strcpy( cname + 13 , lfn2 );
692                    strcpy( cname + 26 , lfn3 );
693                }
694
695                // get dentry arguments if extracted cname == searched name
696                if ( strcmp( name , cname ) == 0 )
697                {
698                    cluster = (fatfs_get_record( DIR_FST_CLUS_HI , base + offset , 1 ) << 16) |
699                              (fatfs_get_record( DIR_FST_CLUS_LO , base + offset , 1 )      ) ;
700                    dentry  = ((index<<12) + offset)>>5;
701                    is_dir  = ((attr & ATTR_DIRECTORY) == ATTR_DIRECTORY);
702                    size    = fatfs_get_record( DIR_FILE_SIZE , base + offset , 1 );
703                    found   = 1;
704                }
705                offset = offset + 32;
706                lfn    = 0;
707            }
708        }  // end loop on directory entries
709        index++;
710        offset = 0;
711    }  // end loop on pages
712
713    // analyse the result of scan
714
715    if ( found == -1 )  // found end of directory => failure
716    {
717
718fatfs_dmsg("\n[DBG] %s : exit / child <%s> not found in parent inode %l\n",
719__FUNCTION__ , name , XPTR( local_cxy , parent_inode ) );
720
721        return ENOENT;
722    }
723    else               // found searched child name
724    {
725        // get child inode cluster and local pointer
726        cxy_t         child_cxy = GET_CXY( child_inode_xp );
727        vfs_inode_t * child_ptr = (vfs_inode_t *)GET_PTR( child_inode_xp );
728
729        // update the child inode "type", "size", and "extend" fields
730        vfs_inode_type_t type = (is_dir) ? INODE_TYPE_DIR : INODE_TYPE_FILE;
731
732        hal_remote_sw( XPTR( child_cxy , &child_ptr->type   ) , type );
733        hal_remote_sw( XPTR( child_cxy , &child_ptr->size   ) , size );
734        hal_remote_sw( XPTR( child_cxy , &child_ptr->extend ) , cluster );
735
736fatfs_dmsg("\n[DBG] %s : exit / child <%s> found in parent inode %l\n",
737__FUNCTION__ , name , XPTR( local_cxy , parent_inode ) );
738
739        return 0;
740    }
741}  // end fatfs_inode_load()
Note: See TracBrowser for help on using the repository browser.