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

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

Few bugs in VMM

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