source: trunk/kernel/vfs/fatfs.c @ 406

Last change on this file since 406 was 406, checked in by alain, 7 years ago

This version executed successfully the user "init" process on a mono-processor TSAR architecture.

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