Changeset 530


Ignore:
Timestamp:
Mar 27, 2015, 12:10:20 PM (9 years ago)
Author:
alain
Message:

1) The FAT library supports now only two modes to access the block device:

  • synchronous: the calling task detect transfer completion by polling the block device status. This mode is used by the boot code.
  • descheduling: The calling task is descheduled, and transfer completion is signaled by an IRQ. This mode is used by the kernel code to handle user system calls.

2) The generic IOC driver has been integrate in the FAT library: the _fat_ioc_access() function

select the proper driver (BDV,HBA,SDC,RDK), with the requested access mode.

Location:
soft/giet_vm/giet_fat32
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/giet_fat32/fat32.c

    r503 r530  
    55// Copyright (c) UPMC-LIP6
    66//////////////////////////////////////////////////////////////////////////////////
    7 // The fat32.h and fat32.c files define a library of access functions
     7// The fat.h and fat_common.c files define a library of access functions
    88// to a FAT32 disk on a block device. It is intended to be used
    99// by the GIET_VM nano-kernel for both the boot code and the kernel code.
    10 // This code uses functions defined in the utils.c and drivers.c files.
    1110//////////////////////////////////////////////////////////////////////////////////
    1211// Implementation notes:
     
    1514// 2. the "cluster" variable is actually a cluster index. A cluster contains
    1615//    typically 8 sectors (4K bytes) and the cluster index is a 32 bits word.
    17 // 2. This FAT32 library uses a FAT cache whose storage capacity is one
    18 //    sector (512 bytes, or 128 cluster indexes in FAT)
    19 // 3. This FAT32 library can be used in 3 modes: BOOT/BOOT/KERNEL/USER
    20 //    defining different behaviours for the IOC driver.
     16// 3. This FAT32 library uses a FAT cache whose storage capacity is one
     17//    sector (512 bytes = 128 cluster indexes in FAT)
    2118//////////////////////////////////////////////////////////////////////////////////
    2219
    2320#include <giet_config.h>
     21#include <hard_config.h>
    2422#include <fat32.h>
     23#include <utils.h>
     24#include <vmem.h>
     25#include <bdv_driver.h>
     26#include <hba_driver.h>
     27#include <sdc_driver.h>
     28#include <rdk_driver.h>
     29#include <mmc_driver.h>
    2530#include <tty0.h>
    26 #include <ioc_driver.h>
    27 #include <utils.h>
    2831
    2932//////////////////////////////////////////////////////////////////////////////////
     
    3134//////////////////////////////////////////////////////////////////////////////////
    3235
    33 extern fat32_fs_t fat __attribute__((aligned(512)));
     36extern fat32_fs_t _fat;
     37
     38extern unsigned int _ptabs_vaddr[GIET_NB_VSPACE_MAX][X_SIZE][Y_SIZE];
     39 
     40//////////////////////////////////////////////////////////////////////////////
     41// This function computes the memory buffer physical address, and calls
     42// the proper IOC driver depending on the subtype (BDV / HBA / SDC /RDK).
     43// The use_irq argument allows to activate the descheduling mode, if it
     44// supported by the IOC driver subtype
     45//////////////////////////////////////////////////////////////////////////////
     46// Return 0 in case of success, return -1 in case of error
     47//////////////////////////////////////////////////////////////////////////////
     48static
     49int _fat_ioc_access( unsigned int use_irq,
     50                     unsigned int to_mem,
     51                     unsigned int lba,
     52                     unsigned int buf_vaddr,
     53                     unsigned int count )
     54{
     55    // compute memory buffer physical address
     56    unsigned int       flags;         // for _v2p_translate
     57    unsigned long long buf_paddr;     // buffer physical address
     58
     59    if ( ((_get_mmu_mode() & 0x4) == 0 ) || USE_IOC_RDK )  // identity
     60    {
     61        buf_paddr = (unsigned long long)buf_vaddr;
     62    }
     63    else                                // V2P translation required
     64    {
     65        buf_paddr = _v2p_translate( buf_vaddr , &flags );
     66    }
     67
     68#if (GIET_DEBUG_FAT > 1)
     69unsigned int procid  = _get_procid();
     70unsigned int x       = procid >> (Y_WIDTH + P_WIDTH);
     71unsigned int y       = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1);
     72unsigned int p       = procid & ((1<<P_WIDTH)-1);
     73_printf("\n[DEBUG FAT] P[%d,%d,%d] enters _fat_ioc_access() at cycle %d\n"
     74        "  to_mem = %d / vaddr = %x / paddr = %l / sectors = %d / lba = %x\n",
     75        x, y , p, _get_proctime(), to_mem, buf_vaddr, buf_paddr, count, lba );
     76#endif
     77
     78    // cache coherence for both L1 & L2 caches
     79    if ( to_mem ) // memory write 
     80    {
     81        // L1 cache (only if L1 cache coherence not guaranteed by hardware)
     82        if ( GIET_NO_HARD_CC ) _dcache_buf_invalidate( buf_vaddr, count<<9 );
     83
     84        // L2 cache (only if we use an IO-Bridge component in architecture))
     85        if ( USE_IOB ) _mmc_inval( buf_paddr, count<<9 );
     86    }
     87    else         // memory read
     88    {
     89        // L1 cache : nothing to do for L1 write-through
     90
     91        // L2 cache (only if we use an IO-Bridge component in architecture))
     92        if ( USE_IOB ) _mmc_sync( buf_paddr, count<<9 );
     93    }
     94
     95    // call the proper descheduling physical device driver
     96 
     97#if   ( USE_IOC_BDV )
     98    return( _bdv_access( use_irq , to_mem , lba , buf_paddr , count ) );
     99#elif ( USE_IOC_HBA )
     100    return( _hba_access( use_irq , to_mem , lba , buf_paddr , count ) );
     101#elif ( USE_IOC_SPI )
     102    return( _sdc_access( use_irq , to_mem , lba , buf_paddr , count ) );
     103#elif ( USE_IOC_RDK )
     104    return( _rdk_access( use_irq , to_mem , lba , buf_paddr , count ) );
     105#else
     106    _printf("\n[FAT ERROR] in _fat_ioc_access() : no IOC driver\n");
     107    _exit();
     108#endif
     109
     110}  // end _fat_ioc_access()
     111
    34112
    35113//////////////////////////////////////////////////////////////////////////////////
    36114// This function displays the content of the FAT cache
    37115//////////////////////////////////////////////////////////////////////////////////
     116#if (GIET_DEBUG_FAT > 1)
     117static
    38118void _display_fat_cache()
    39119{
     
    45125
    46126    _puts("\n*********************** fat_cache_lba = ");
    47     _putx( fat.cache_lba );
     127    _putx( _fat.cache_lba );
    48128    _puts(" *****************************\n");
    49129
     
    58138        {
    59139            unsigned int byte  = (line<<5) + (word<<2);
    60             unsigned int hexa  = (fat.fat_cache[byte  ]<<24) |
    61                                  (fat.fat_cache[byte+1]<<16) |
    62                                  (fat.fat_cache[byte+2]<< 8) |
    63                                  (fat.fat_cache[byte+3]);
     140            unsigned int hexa  = (_fat.fat_cache[byte  ]<<24) |
     141                                 (_fat.fat_cache[byte+1]<<16) |
     142                                 (_fat.fat_cache[byte+2]<< 8) |
     143                                 (_fat.fat_cache[byte+3]);
    64144            _putx( hexa );
    65145            _puts(" | ");
    66146
    67147            // prepare display ascii
    68             temp[word] = fat.fat_cache[byte]         |
    69                          (fat.fat_cache[byte+1]<<8)  |
    70                          (fat.fat_cache[byte+2]<<16) |
    71                          (fat.fat_cache[byte+3]<<24) ;
     148            temp[word] = _fat.fat_cache[byte]         |
     149                         (_fat.fat_cache[byte+1]<<8)  |
     150                         (_fat.fat_cache[byte+2]<<16) |
     151                         (_fat.fat_cache[byte+3]<<24) ;
    72152        }
    73153       
     
    79159
    80160} // end _display_fat_cache() 
     161#endif
     162
     163//////////////////////////////////////////////////////////////////////////////////
     164// This function displays the FAT descriptor.
     165//////////////////////////////////////////////////////////////////////////////////
     166#if GIET_DEBUG_FAT
     167static
     168void _fat_print()
     169{
     170    _printf("\n########################## FAT32 ################################" 
     171            "\nFAT initialised                  %x"
     172            "\nSector Size  (bytes)             %x"
     173            "\nSectors per cluster              %x"
     174            "\nFAT region first lba             %x"
     175            "\nData region first lba            %x"
     176            "\nNumber of sectors for one FAT    %x"
     177            "\nNumber of free clusters          %x"
     178            "\nLast allocated cluster           %x"
     179            "\n#################################################################\n",
     180            _fat.initialised,
     181            _fat.sector_size,
     182            _fat.sectors_per_cluster,
     183            _fat.fat_lba,
     184            _fat.data_lba,
     185            _fat.fat_sectors,
     186            _fat.number_free_cluster,
     187            _fat.last_cluster_allocated );
     188} // end _fat_print()
     189#endif
    81190
    82191//////////////////////////////////////////////////////////////////////////////////
     
    84193// by an (offset,length) mnemonic defined in fat32.h file.
    85194//////////////////////////////////////////////////////////////////////////////////
    86 static inline int get_length( int offset,
    87                               int length )
     195static inline
     196int get_length( int offset,
     197                int length )
    88198{
    89199    return length;
     
    94204// The modified field in buffer is defined by the offset and size arguments.
    95205//////////////////////////////////////////////////////////////////////////////
    96 static void _write_entry( unsigned int   offset,
    97                          unsigned int   size,
    98                          char*          buffer,
    99                          unsigned int   value )
     206static
     207void _write_entry( unsigned int   offset,
     208                   unsigned int   size,
     209                   char*          buffer,
     210                   unsigned int   value )
    100211{
    101212    unsigned int turn = 0;
     
    116227// The analysed field in buffer is defined by the offset and size arguments.
    117228//////////////////////////////////////////////////////////////////////////////
    118 static unsigned int _read_entry( unsigned int   offset,
    119                                  unsigned int   size,
    120                                  char*          buffer,
    121                                  unsigned int   little_indian )
     229static
     230unsigned int _read_entry( unsigned int   offset,
     231                          unsigned int   size,
     232                          char*          buffer,
     233                          unsigned int   little_indian )
    122234{
    123235    unsigned int turn;
     
    156268// The DATA region indexing starts a cluster 2.
    157269//////////////////////////////////////////////////////////////////////////////////
    158 static inline unsigned int lba_to_cluster( unsigned int lba )                   
    159 {
    160    if (lba < fat.data_lba ) return 0;
    161 
    162    return ( (lba - fat.data_lba) / fat.sectors_per_cluster) + 2;
     270static inline
     271unsigned int lba_to_cluster( unsigned int lba )                   
     272{
     273   if (lba < _fat.data_lba ) return 0;
     274
     275   return ( (lba - _fat.data_lba) / _fat.sectors_per_cluster) + 2;
    163276}
    164277
     
    167280// from the cluster index. The cluster index must be larger than 2.
    168281//////////////////////////////////////////////////////////////////////////////////
    169 static inline unsigned int cluster_to_lba( unsigned int cluster )       
     282static inline
     283unsigned int cluster_to_lba( unsigned int cluster )       
    170284{
    171285   if ( cluster < 2 ) return 0;
    172286
    173    return  (fat.sectors_per_cluster * (cluster - 2)) + fat.data_lba;
     287   return  (_fat.sectors_per_cluster * (cluster - 2)) + _fat.data_lba;
    174288}
    175289
     
    179293// remark: a sector of FAT contains 128 cluster indexes.
    180294/////////////////////////////////////////////////////////////////////////////////
    181 static unsigned int _get_next_cluster( unsigned int mode,
    182                                        unsigned int cluster )
     295static
     296unsigned int _get_next_cluster( unsigned int use_irq,
     297                                unsigned int cluster )
    183298{
    184299    // compute lba of the sector containing the cluster index
    185     unsigned int lba = fat.fat_lba + (cluster / 128);
    186 
    187 #if GIET_DEBUG_FAT
     300    unsigned int lba = _fat.fat_lba + (cluster / 128);
     301
     302    if ( lba != _fat.cache_lba )      // miss in fat_cache
     303    {
     304        // access fat
     305        if( _fat_ioc_access( use_irq,
     306                             1,               // read
     307                             lba,
     308                             (unsigned int)_fat.fat_cache,
     309                             1 ) )            // one sector
     310        {
     311            _printf("[FAT_ERROR] in get_next_cluster_id() : cannot read block %x",
     312                    lba );
     313            return 1;
     314        }
     315        _fat.cache_lba = lba;
     316    }
     317
     318    unsigned int next = _read_entry( ((cluster % 128) * 4),
     319                                     4,
     320                                     _fat.fat_cache,
     321                                     1 );
     322#if (GIET_DEBUG_FAT > 1)
    188323unsigned int procid  = _get_procid();
    189324unsigned int x       = procid >> (Y_WIDTH + P_WIDTH);
    190325unsigned int y       = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1);
    191326unsigned int p       = procid & ((1<<P_WIDTH)-1);
    192 
    193 _printf("\n[FAT DEBUG] _get_next_cluster() : P[%d,%d,%d] enters for cluster %x\n",
    194       x , y , p , cluster );
    195 #endif
    196 
    197     if ( lba != fat.cache_lba )      // miss in fat_cache
    198     {
    199         // we cannot access fat in user mode
    200         if( mode == IOC_USER_MODE ) mode = IOC_KERNEL_MODE;
    201        
    202         // access fat
    203         if( _ioc_read( 0,                 // channel
    204                        mode,              // mode for IOC driver
    205                        lba,               // sector index
    206                        fat.fat_cache,     // fat cache
    207                        1 ) )              // one sector
    208         {
    209             _printf("[FAT_ERROR] in get_next cluster_id() : cannot read block %x", lba );
    210             return 1;
    211         }
    212         fat.cache_lba = lba;
    213    
    214 #if (GIET_DEBUG_FAT > 1)
     327_printf("\n[DEBUG FAT] P[%d,%d,%d] in _get_next_cluster() :  next = %x\n",
     328        x , y , p , next );
    215329_display_fat_cache();
    216 #endif
    217 
    218     }
    219     unsigned int next = _read_entry( ((cluster % 128) * 4),
    220                                      4,
    221                                      fat.fat_cache,
    222                                      1 );
    223 
    224 #if GIET_DEBUG_FAT
    225 _printf("\n[FAT DEBUG] _get_next_cluster() : P[%d,%d,%d] next cluster = %x\n", next );
    226330#endif
    227331
     
    241345// Return the '_' character if c is illegal
    242346////////////////////////////////////////////////////////////////
    243 static unsigned char illegal_short(unsigned char c)
     347static
     348unsigned char illegal_short(unsigned char c)
    244349{
    245350   const unsigned char illegal_char [] =";+=[]’,\"*\\<>/?:|\0";
     
    265370// Return 0 if not legal SFN
    266371/////////////////////////////////////////////////////////////////////////////////
    267 static int is_short( char* string,
    268                      char* sfn_string)
     372static
     373int is_short( char* string,
     374              char* sfn_string)
    269375{
    270376    int s_size   = 0;
     
    340446// Return 1 if name found, Return 0 if NUL character found,
    341447///////////////////////////////////////////////////////////////////////
    342 static int get_name_from_path( char*          pathname,
    343                                char*          name,
    344                                unsigned int*  nb_read )
     448static
     449int get_name_from_path( char*          pathname,
     450                        char*          name,
     451                        unsigned int*  nb_read )       
    345452{
    346453    if ( pathname[*nb_read] == 0 ) return 0;
     
    452559} // end get_name_from_long()
    453560
    454 //////////////////////////////////////////////////////////////////////////////////////
     561///////////////////////////////////////////////////////////////////////////////////
    455562// This function update a DIR_ENTRY, write a new value into a specific field
    456563// (ex : DIR_FILE_SIZE, when we want update the file size after a fat_write)
    457564// Return 0 in case of success, > 0 if failure.
    458 //////////////////////////////////////////////////////////////////////////////////////
     565///////////////////////////////////////////////////////////////////////////////////
    459566// TODO : make this function less complex
    460 //////////////////////////////////////////////////////////////////////////////////////
    461 static inline unsigned int update_entry( unsigned int fd_id,
    462                                          unsigned int field,
    463                                          unsigned int size,
    464                                          unsigned int value )
     567///////////////////////////////////////////////////////////////////////////////////
     568static inline
     569unsigned int _update_entry( unsigned int use_irq,
     570                            unsigned int fd_id,
     571                            unsigned int field,
     572                            unsigned int size,
     573                            unsigned int value )
    465574{
    466575    char dir_entry[32];   // buffer to store a full directory_entry
     
    468577    char file_name[256];  // buffer to store the name (not pathname) of the file
    469578
    470     char sfn_string[12]  = {[0 ... 10] = ' ', '\0'};       // buffer for a Short File Name
    471     unsigned int lba     = fat.fd[fd_id].lba_dir_entry;    // Lba of file dir_entry
     579    char sfn_string[12]  = {[0 ... 10] = ' ', '\0'};     // buffer Short File Name
     580    unsigned int lba     = _fat.fd[fd_id].lba_dir_entry;  // Lba of file dir_entry
    472581    unsigned int is_sfn;         
    473     unsigned int attr    = 0;                              // directory entry attribute
    474     unsigned int ord     = 0;                              // directory entry sequence
    475     unsigned int found   = 0;                              // name found
    476     unsigned int offset  = 0;                              // offset in fat_cache
     582    unsigned int attr    = 0;                            // dir entry attribute
     583    unsigned int ord     = 0;                            // dir entry sequence
     584    unsigned int found   = 0;                            // name found
     585    unsigned int offset  = 0;                            // offset in fat_cache
    477586    unsigned int i       = 0;
    478587    unsigned int nb_read = 0;
     
    482591   
    483592    // Get the name of the file.
    484     while ( get_name_from_path( fat.fd[fd_id].name, file_name, &nb_read ) )
     593    while ( get_name_from_path( _fat.fd[fd_id].name, file_name, &nb_read ) )
    485594    {
    486595    }
     
    489598    is_sfn = is_short( file_name, sfn_string );
    490599
    491     if ( _ioc_read( 0,               // channel
    492                     IOC_KERNEL_MODE, // mode for IOC driver
    493                     lba,             // sector index
    494                     fat.fat_cache,   // buffer address
    495                     1 ) )            // one sector
    496     {
    497         _printf("\n[FAT ERROR] in update_entry() cannot read sector %x\n", lba );
     600    // access FAT
     601    if ( _fat_ioc_access( use_irq,
     602                          1,               // read
     603                          lba,
     604                          (unsigned int)_fat.fat_cache,
     605                          1 ) )            // one sector
     606    {
     607        _printf("\n[FAT ERROR] in _update_entry() cannot read sector %x\n",
     608                lba );
    498609        return 1;
    499610    }
    500     fat.cache_lba = lba;
     611    _fat.cache_lba = lba;
    501612
    502613    // - the offset increment is an integer number of directory entry (32 bytes)
     
    504615    while ( !found )
    505616    {
    506         attr = _read_entry( DIR_ATTR, fat.fat_cache + offset, 0 );
    507         ord  = _read_entry( LDIR_ORD, fat.fat_cache + offset, 0 );
     617        attr = _read_entry( DIR_ATTR, _fat.fat_cache + offset, 0 );
     618        ord  = _read_entry( LDIR_ORD, _fat.fat_cache + offset, 0 );
    508619
    509620        if ( is_sfn == 1 )                                     // searched name is short
     
    520631                    (ord  != NO_MORE_ENTRY ) )         // SFN entry : checked
    521632            {
    522                 memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );   
     633                memcpy( dir_entry, _fat.fat_cache + offset, DIR_ENTRY_SIZE );   
    523634            }
    524635            else if (ord == NO_MORE_ENTRY )            // end of directory : return
    525636            {
    526                 _printf("\n[FAT ERROR] in update_entry() : reaches end of directory\n");
     637                _printf("\n[FAT ERROR] in _update_entry() : reaches end\n");
    527638                return 1;
    528639            }
     
    539650                    (ord != NO_MORE_ENTRY) )           // LFN entry : checked
    540651            {
    541                 memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );   
     652                memcpy( dir_entry, _fat.fat_cache + offset, DIR_ENTRY_SIZE );   
    542653            }
    543654            else if ( (attr != ATTR_LONG_NAME_MASK) &&
     
    550661            else if (ord == NO_MORE_ENTRY )                        // end of directory : return
    551662            {
    552                 _printf("\n[FAT ERROR] in update_entry() reaches end of directory\n");
     663                _printf("\n[FAT ERROR] in _update_entry() reaches end\n");
    553664                return 1;
    554665            }
     
    568679            if ( _strncmp( (char*)sfn_string, (char*)name_entry, 13 ) == 0 )
    569680            {
    570                 _write_entry(offset + field, size, fat.fat_cache, value);
     681                _write_entry(offset + field, size, _fat.fat_cache, value);
    571682                found = 1;
    572683            }
     
    581692
    582693            unsigned shift = ((ord & 0xf) - 1) * 13;
    583             if ( _strncmp( (char*)(file_name + shift), (char*)name_entry, 13 ) == 0 )
     694            if ( _strncmp( (char*)(file_name + shift),
     695                           (char*)name_entry, 13 ) == 0 )
    584696            {
    585697                if ( (ord & 0xf) == 1 )
    586698                {
    587699                    offset = offset + DIR_ENTRY_SIZE;
    588                     _write_entry(offset + field, size, fat.fat_cache, value);
     700                    _write_entry(offset + field, size, _fat.fat_cache, value);
    589701                    found = 1;
    590702                }
     
    599711    }
    600712
    601     return _ioc_write( 0,                // channel
    602                        IOC_KERNEL_MODE,  // mode
    603                        lba,              // sector index
    604                        fat.fat_cache,    // source buffer
    605                        1 );              // one sector
    606 }
    607 //////////////////////////////////////////////////////////////////////////////////
    608 // This function update FS_INFO:
     713    // write block to FAT
     714    if ( _fat_ioc_access( use_irq,
     715                          0,                // write
     716                          lba,
     717                          (unsigned int)_fat.fat_cache,
     718                          1 ) )             // one sector
     719    {
     720        _printf("\n[FAT ERROR] in _update_entry() cannot write sector %x\n",
     721                lba );
     722        return 1;
     723    }
     724
     725    return 0;
     726} // end _update_entry()
     727
     728
     729//////////////////////////////////////////////////////////////////////////////////
     730// This function update the FS_INFO block:
    609731// last cluster allocated and number of free cluster.
    610732// Return 0 in case of success, > 0 if failure.
    611733//////////////////////////////////////////////////////////////////////////////////
    612 static inline unsigned int _update_fs_info( )
    613 {
    614     unsigned int lba = fat.fs_info_lba;
    615 
    616 #if GIET_DEBUG_FAT
    617 _printf("\n[FAT DEBUG] _update_fs_info() : enters\n");
    618 #endif
    619 
    620     if ( lba == fat.cache_lba )            // hit cache
    621     {
    622         _write_entry( FS_FREE_CLUSTER     , fat.fat_cache, fat.number_free_cluster );
    623         _write_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, fat.last_cluster_allocated );
    624     }
    625     else                                   // miss cache
    626     {
    627         if ( _ioc_read( 0,                 // channel
    628                         IOC_KERNEL_MODE,   // mode for IOC driver
    629                         lba,               // sector index
    630                         fat.fat_cache,     // source buffer
    631                         1 ) )              // one sector
    632         {
    633             _printf("\n[FAT_ERROR] in _update_fat() cannot read block %x\n", lba );
     734static inline
     735unsigned int _update_fs_info( unsigned int use_irq )
     736{
     737    unsigned int lba = _fat.fs_info_lba;
     738
     739#if GIET_DEBUG_FAT
     740_printf("\n[DEBUG FAT] _update_fs_info()\n");
     741#endif
     742
     743    // update FAT cache in case of miss
     744    if ( lba != _fat.cache_lba )
     745    {
     746        if ( _fat_ioc_access( use_irq,
     747                              1,                 // read
     748                              lba,
     749                              (unsigned int)_fat.fat_cache,
     750                              1 ) )              // one sector
     751        {
     752            _printf("\n[FAT_ERROR] in _update_fs_info() cannot read block\n");
    634753            return 1;
    635754        }
    636         fat.cache_lba = lba;
    637         _write_entry( FS_FREE_CLUSTER     , fat.fat_cache, fat.number_free_cluster );
    638         _write_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, fat.last_cluster_allocated );
    639     }
    640     return _ioc_write( 0,                // channel
    641                        IOC_KERNEL_MODE,  // mode   
    642                        lba,              // sector index
    643                        fat.fat_cache,    // source buffer
    644                        1 );              // one sector
    645 }
     755        _fat.cache_lba = lba;
     756    }
     757
     758    _write_entry( FS_FREE_CLUSTER     , _fat.fat_cache, _fat.number_free_cluster );
     759    _write_entry( FS_FREE_CLUSTER_HINT, _fat.fat_cache, _fat.last_cluster_allocated );
     760   
     761    // write bloc to FAT
     762    if ( _fat_ioc_access( use_irq,
     763                          0,                // write
     764                          lba,
     765                          (unsigned int)_fat.fat_cache,
     766                          1 ) )             // one sector
     767    {
     768        _printf("\n[FAT_ERROR] in _update_fs_info() cannot write block\n");
     769        return 1;
     770    }
     771
     772    return 0;
     773}  // end _update_fs_info()
    646774
    647775//////////////////////////////////////////////////////////////////////////////////
     
    650778// Return 0 in case of success, > 0 if failure.
    651779//////////////////////////////////////////////////////////////////////////////////
    652 static inline unsigned int _update_fat( unsigned int cluster,
    653                                         unsigned int value  )
    654 {
    655     unsigned int lba = fat.fat_lba + (cluster / 128);
    656 
    657 #if GIET_DEBUG_FAT
    658 _printf("\n[FAT DEBUG] _update_fat() : cluster = %x / value = %x\n", cluster, value );
    659 #endif
    660 
    661     if ( lba == fat.cache_lba )            // hit cache
    662     {
    663         _write_entry( ((cluster % 128) << 2), 4, fat.fat_cache, value );
    664     }
    665     else                                   // miss cache
    666     {
    667         if ( _ioc_read( 0,                 // channel
    668                         IOC_KERNEL_MODE,   // mode for IOC driver
    669                         lba,               // sector index
    670                         fat.fat_cache,     // source buffer
    671                         1 ) )              // one sector
    672         {
    673             _printf("\n[FAT_ERROR] in _update_fat() cannot read block %x\n", lba );
     780static inline
     781unsigned int _update_fat( unsigned int use_irq,
     782                          unsigned int cluster,
     783                          unsigned int value  )
     784{
     785    unsigned int lba = _fat.fat_lba + (cluster / 128);
     786
     787#if GIET_DEBUG_FAT
     788_printf("\n[DEBUG FAT] _update_fat() : cluster = %x / value = %x\n",
     789        cluster, value );
     790#endif
     791
     792    // update FAT cache in case of miss
     793    if ( lba != _fat.cache_lba ) 
     794    {
     795        if ( _fat_ioc_access( use_irq,
     796                              1,                 // read
     797                              lba,
     798                              (unsigned int)_fat.fat_cache,
     799                              1 ) )              // one sector
     800        {
     801            _printf("\n[FAT_ERROR] in _update_fat() cannot read block %x\n",
     802                    lba );
    674803            return 1;
    675804        }
    676         fat.cache_lba = lba;
    677         _write_entry( ((cluster % 128) << 2), 4, fat.fat_cache, value );
    678     }
    679     return _ioc_write( 0,                // channel
    680                        IOC_KERNEL_MODE,  // mode     
    681                        lba,              // sector indexs
    682                        fat.fat_cache,    // source buffer
    683                        1 );              // one sector
     805        _fat.cache_lba = lba;
     806    }
     807
     808    _write_entry( ((cluster % 128) << 2), 4, _fat.fat_cache, value );
     809
     810    // write bloc to FAT
     811    if ( _fat_ioc_access( use_irq,
     812                          0,                // write
     813                          lba,
     814                          (unsigned int)_fat.fat_cache,
     815                          1 ) )             // one sector
     816    {
     817        _printf("\n[FAT_ERROR] in _update_fat() cannot write block %x\n",
     818                lba );
     819        return 1;
     820    }
     821
     822    return 0;
    684823} // end update_fat()
    685824
     
    689828// return 0 if success, -1 if failure
    690829//////////////////////////////////////////////////////////////////////////////////
    691 static inline int _fat_allocate( unsigned int fd_id,
    692                                  unsigned int count )
    693 {
    694     unsigned int next_cluster = fat.fd[fd_id].first_cluster;    // get first cluster
    695     unsigned int cluster_to_allocate = count;                   // clusters to allocate
    696     unsigned int last_cluster_file;                             // Last cluster (EOC)
    697     unsigned int free_cluster = fat.last_cluster_allocated + 1; // First free cluster
     830static inline
     831int _fat_allocate( unsigned int use_irq,
     832                   unsigned int fd_id,
     833                   unsigned int count )
     834{
     835    unsigned int next_cluster = _fat.fd[fd_id].first_cluster;    // first cluster
     836    unsigned int cluster_to_allocate = count;                   // to allocate
     837    unsigned int last_cluster_file;                             // Last cluster
     838    unsigned int free_cluster = _fat.last_cluster_allocated + 1; // First free
    698839
    699840    // Check if free_cluster is really free (must be true)
    700     if ( _get_next_cluster( IOC_KERNEL_MODE, free_cluster ) != FREE_CLUSTER)
     841    if ( _get_next_cluster( use_irq , free_cluster ) != FREE_CLUSTER)
    701842    {
    702843        _printf("\n[FAT ERROR] in _fat_allocate() : first free cluster not free\n");
     
    704845    }
    705846    // Check if FAT contains enough cluster free for this allocation
    706     if ( count > fat.number_free_cluster )
     847    if ( count > _fat.number_free_cluster )
    707848    {
    708849        _printf("\n[FAT ERROR] in _fat_allocate() : Not enough free cluster(s)\n");
     
    711852
    712853#if GIET_DEBUG_FAT
    713 _printf("\n[FAT DEBUG] _fat_allocate() for fd = %d\n" fd_id );
     854_printf("\n[DEBUG FAT] _fat_allocate() for fd = %d\n", fd_id );
    714855#endif
    715856
     
    718859    {
    719860        last_cluster_file = next_cluster;
    720         next_cluster      = _get_next_cluster( IOC_KERNEL_MODE, next_cluster );
     861        next_cluster      = _get_next_cluster( use_irq , next_cluster );
    721862    }
    722863    while ( next_cluster < END_OF_CHAIN_CLUSTER );
     
    727868
    728869#if GIET_DEBUG_FAT
    729 _printf("\n[FAT DEBUG] cluster to update = %x / free cluster = %x / nb_clusters %d\n",
     870_printf("\n[DEBUG FAT] cluster to update = %x / free cluster = %x / count = %d\n",
    730871        last_cluster_file , free_cluster , cluster_to_allocate );
    731872#endif
     
    733874        // update, in the FAT, the value of last cluster allocated by the index
    734875        // of free cluster.
    735         if ( _update_fat( last_cluster_file, free_cluster ) )
     876        if ( _update_fat( use_irq , last_cluster_file , free_cluster ) )
    736877        {
    737878            _printf("\n[FAT ERROR] in _fat_allocate() : update fat failed\n");
     
    748889            // update, in the FAT, the value of the last cluster allocated by
    749890            // END_OF_CHAIN_CLUSTER
    750             if ( _update_fat( last_cluster_file, END_OF_CHAIN_CLUSTER ) )
     891            if ( _update_fat( use_irq , last_cluster_file , END_OF_CHAIN_CLUSTER ) )
    751892            {
    752893                _printf("\n[FAT ERROR] in _fat_allocate() : update fat failed\n");
     
    758899
    759900        // Check if free_cluster is really free (must be true)
    760         if ( _get_next_cluster( IOC_KERNEL_MODE, free_cluster ) != FREE_CLUSTER)
     901        if ( _get_next_cluster( use_irq , free_cluster ) != FREE_CLUSTER)
    761902        {
    762903            _printf("\n[FAT ERROR] in _fat_allocate() : free_cluster not free\n");
     
    767908    // Update field number_free_cluster and last_cluster_allocated
    768909    // of structure fat for next fat_allocate
    769     fat.last_cluster_allocated = last_cluster_file;
    770     fat.number_free_cluster    = fat.number_free_cluster - count;
    771 
    772     if ( _update_fs_info() )
     910    _fat.last_cluster_allocated = last_cluster_file;
     911    _fat.number_free_cluster    = _fat.number_free_cluster - count;
     912
     913    if ( _update_fs_info( use_irq ) )
    773914    {
    774915        _printf("\n[FAT ERROR] in _fat_allocate() : update fs_info failed\n");
     
    779920}  // end _fat_allocate()
    780921
    781 ////////////////////////////////////////////////////////////////////////////////////////
     922//////////////////////////////////////////////////////////////////////////////////
    782923// This function read the blocks defined by the cluster index argument, in a data
    783924// region containing a directory to search the name of a file/firectory.
     
    790931// The cluster index is always stored in a SFN or LFN entry.
    791932// Return cluster index if name found / Return -1 if not found.
    792 ////////////////////////////////////////////////////////////////////////////////////////
    793 static int _scan_directory( unsigned int   mode,            // mode for IOC driver
    794                             unsigned int   cluster,         // cluster containing dir_entry
    795                             char*          file_name,       // searched file/directory name
    796                             unsigned int*  file_size,       // file size
    797                             unsigned int*  lba_dir_entry )  // lba of dir_entry
     933//////////////////////////////////////////////////////////////////////////////////
     934static
     935int _scan_directory( unsigned int   use_irq,         // use descheduling mode
     936                     unsigned int   cluster,         // cluster of dir_entry
     937                     char*          file_name,       // searched file/dir name
     938                     unsigned int*  file_size,       // file size
     939                     unsigned int*  lba_dir_entry )  // lba of dir_entry
    798940{
    799941    char dir_entry[32];   // buffer to store a full directory_entry
    800942    char name_entry[14];  // buffer to store a 13 characters (partial) name
    801943
    802     char sfn_string[12]    = {[0 ... 10] = ' ', '\0'};        // buffer for Short File Name
    803     unsigned int  is_sfn   = is_short(file_name, sfn_string); // if file_name is short
    804     unsigned int  offset   = 0;                               // byte offset in block
    805     unsigned int  block_id = fat.sectors_per_cluster;         // sector index initialisation
    806     unsigned int  lba      = cluster_to_lba(cluster);         // lba of cluster containing dir
    807     unsigned int  attr     = 0;                               // directory entry attribute
    808     unsigned int  ord      = 0;                               // directory entry sequence
    809     unsigned int  found    = 0;                               // searched name found
    810     unsigned int  long_name_found = 0;                        // a matching XTN has been found
    811     unsigned int  searched_cluster;                           // searched cluster index
     944    char sfn_string[12]    = {[0 ... 10] = ' ', '\0'};  // buffer Short File Name
     945    unsigned int  is_sfn   = is_short(file_name, sfn_string); // file_name is short
     946    unsigned int  offset   = 0;                         // byte offset in block
     947    unsigned int  block_id = _fat.sectors_per_cluster;   // sector index
     948    unsigned int  lba      = cluster_to_lba(cluster);   // lba of cluster
     949    unsigned int  attr     = 0;                         // dir entry attribute
     950    unsigned int  ord      = 0;                         // dir entry sequence
     951    unsigned int  found    = 0;                         // searched name found
     952    unsigned int  long_name_found = 0;                  // matching XTN found
     953    unsigned int  searched_cluster;                     // searched cluster index
    812954
    813955#if GIET_DEBUG_FAT
     
    817959unsigned int p       = procid & ((1<<P_WIDTH)-1);
    818960
    819 _printf("\n[FAT DEBUG] _scan_directory() : P[%d,%d,%d] enters for %s\n",
     961_printf("\n[DEBUG FAT] P[%d,%d,%d] enters _scan_directory() for %s\n",
    820962      x, y, p, file_name );
    821963#endif
     
    827969    // load first sector from DATA region into FAT cache
    828970    // other sectors will be loaded inside loop as required
    829     if( _ioc_read( 0,               // channel
    830                    mode,            // mode for IOC driver
    831                    lba,             // sector index
    832                    fat.fat_cache,   // buffer address
    833                    1 ) )            // one sector
    834     {
    835         _printf("[\nFAT ERROR] in _scan_directory() : cannot read sector %x\n", lba );
     971    if( _fat_ioc_access( use_irq,
     972                         1,               // read
     973                         lba,
     974                         (unsigned int)_fat.fat_cache,
     975                         1 ) )            // one sector
     976    {
     977        _printf("[\nFAT ERROR] in _scan_directory() : cannot read sector %x\n",
     978                lba );
    836979        return -1;
    837980    }
    838 
    839     fat.cache_lba = lba;
     981    _fat.cache_lba = lba;
    840982
    841983#if ( GIET_DEBUG_FAT > 1 )
     
    8581000            else                                          // get next cluster
    8591001            {
    860                 cluster = _get_next_cluster( mode, cluster );
     1002                cluster = _get_next_cluster( use_irq , cluster );
    8611003
    8621004                if ( cluster >= END_OF_CHAIN_CLUSTER  ) return END_OF_CHAIN_CLUSTER;
    8631005
    8641006                lba      = cluster_to_lba(cluster);
    865                 block_id = fat.sectors_per_cluster;
    866             }
    867             if( _ioc_read( 0,               // channel
    868                            mode,            // mode for IOC driver
    869                            lba,             // sector index
    870                            fat.fat_cache,   // buffer address
    871                            1 ) )            // one sector
    872             {
    873                 _printf("\n[FAT ERROR] in _scan_directory() : cannot read sector %x\n", lba);
     1007                block_id = _fat.sectors_per_cluster;
     1008            }
     1009            if( _fat_ioc_access( use_irq,
     1010                                 1,               // read
     1011                                 lba,
     1012                                 (unsigned int)_fat.fat_cache,
     1013                                 1 ) )            // one sector
     1014            {
     1015                _printf("\n[FAT ERROR] in _scan_directory() : cannot read sector %x\n",
     1016                        lba);
    8741017                return -1;
    8751018            }
    876             fat.cache_lba = lba;
     1019            _fat.cache_lba = lba;
    8771020            block_id--;
    8781021            offset = offset % 512;
     
    8821025        // if it a possible candidate for the searched name
    8831026
    884         attr = _read_entry( DIR_ATTR, fat.fat_cache + offset, 0);   
    885         ord  = _read_entry( LDIR_ORD, fat.fat_cache + offset, 0);
     1027        attr = _read_entry( DIR_ATTR, _fat.fat_cache + offset, 0);   
     1028        ord  = _read_entry( LDIR_ORD, _fat.fat_cache + offset, 0);
    8861029
    8871030        if ( is_sfn == 1 )                                // searched name is short
     
    8971040                      (ord  != NO_MORE_ENTRY ) )         // SFN entry : to be checked
    8981041            {
    899                 memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );   
     1042                memcpy( dir_entry, _fat.fat_cache + offset, DIR_ENTRY_SIZE );   
    9001043
    9011044                get_name_from_short( dir_entry, name_entry );
     
    9221065                (ord != NO_MORE_ENTRY) )                 // EXT entry : to be checked
    9231066            {
    924                 memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );
     1067                memcpy( dir_entry, _fat.fat_cache + offset, DIR_ENTRY_SIZE );
    9251068
    9261069                get_name_from_long( dir_entry, name_entry );
     
    9431086                if ( long_name_found )                   // LFN entry
    9441087                {
    945                     memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );
     1088                    memcpy( dir_entry, _fat.fat_cache + offset, DIR_ENTRY_SIZE );
    9461089                    found = 1;
    9471090                }
     
    9651108
    9661109#if GIET_DEBUG_FAT
    967 _printf("\n[FAT DEBUG] _scan_directory() : P[%d,%d,%d] found %s"
     1110_printf("\n[DEBUG FAT] _scan_directory() : P[%d,%d,%d] found %s"
    9681111        " : cluster = %x\n", x, y, p, file_name, searched_cluster );
    9691112#endif
     
    9991142// Return 0 if success, exit if failure
    10001143//////////////////////////////////////////////////////////////////////////
    1001 int _fat_init( unsigned int mode )   // mode for IOC driver
    1002 {
    1003     unsigned int   n;
     1144int _fat_init( unsigned int use_irq ) 
     1145{
    10041146
    10051147#if GIET_DEBUG_FAT
     
    10081150unsigned int y       = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1);
    10091151unsigned int p       = procid & ((1<<P_WIDTH)-1);
    1010 
    1011 if ( mode == IOC_BOOT_MODE )
    1012 _printf("\n[FAT DEBUG] _fat_init() : P[%d,%d,%d] enters in BOOT_MODE\n",x,y,p);
    1013 if ( mode == IOC_KERNEL_MODE )
    1014 _printf("\n[FAT DEBUG] _fat_init() : P[%d,%d,%d] enters in KERNEL_MODE\n",x,y,p);
    1015 if ( mode == IOC_USER_MODE )
    1016 _printf("\n[FAT DEBUG] _fat_init() : P[%d,%d,%d] enters in USER_MODE\n",x,y,p);
     1152_printf("\n[DEBUG FAT] P[%d,%d,%d] enters _fat_init\n",x,y,p);
    10171153#endif
    10181154
    10191155    // FAT initialisation should be done only once
    1020     if ( fat.initialised == FAT_INITIALISED )
    1021     {
    1022         _printf("\n[FAT WARNING] Strange, FAT already initialised...\n");
     1156    if ( _fat.initialised == FAT_INITIALISED )
     1157    {
     1158        _printf("\n[FAT ERROR] Strange, FAT already initialised...\n");
    10231159        _exit();
    10241160    }
    10251161
    10261162    // load Boot Record (VBR) into fat cache
    1027     if ( _ioc_read( 0,                   // channel
    1028                     mode,                // mode for IOC driver
    1029                     0,                   // sector index
    1030                     fat.fat_cache,       // buffer address
    1031                     1 ) )                // one sector
     1163    if ( _fat_ioc_access( use_irq,
     1164                          1,                   // read
     1165                          0,                   // sector index
     1166                          (unsigned int)_fat.fat_cache,
     1167                          1 ) )                // one sector
    10321168    {
    10331169        _printf("\n[FAT ERROR] in _fat_init() cannot load VBR\n");
    10341170        _exit();
    10351171    }
    1036     fat.cache_lba = 0;
     1172    _fat.cache_lba = 0;
    10371173
    10381174#if GIET_DEBUG_FAT > 1
    1039 _printf("\n[FAT DEBUG] _fat_init() : Boot Sector Loaded\n");
     1175_printf("\n[DEBUG FAT] _fat_init() : Boot Sector Loaded\n");
    10401176_display_fat_cache();
    10411177#endif
    10421178
    10431179    // checking various FAT32 assuptions from boot sector
    1044     if( _read_entry( BPB_BYTSPERSEC, fat.fat_cache, 1 ) != 512 )
     1180    if( _read_entry( BPB_BYTSPERSEC, _fat.fat_cache, 1 ) != 512 )
    10451181    {
    10461182        _printf("\n[FAT ERROR] The sector size must be 512 bytes\n");
    10471183        _exit();
    10481184    }
    1049     if( _read_entry( BPB_NUMFATS, fat.fat_cache, 1 ) != 1 )
     1185    if( _read_entry( BPB_NUMFATS, _fat.fat_cache, 1 ) != 1 )
    10501186    {
    10511187        _printf("\n[FAT ERROR] The number of FAT copies in FAT region must be 1\n");
    10521188        _exit();
    10531189    }
    1054     if( (_read_entry( BPB_FAT32_FATSZ32, fat.fat_cache, 1 ) & 0xF) != 0 )
    1055     {
    1056         _printf("\n[FAT ERROR] The FAT region in FAT32 must be multiple of 32 sectors\n");
     1190    if( (_read_entry( BPB_FAT32_FATSZ32, _fat.fat_cache, 1 ) & 0xF) != 0 )
     1191    {
     1192        _printf("\n[FAT ERROR] The FAT region must be multiple of 32 sectors\n");
    10571193        _exit();
    10581194    }
    1059     if( _read_entry( BPB_FAT32_ROOTCLUS, fat.fat_cache, 1 ) != 2 )
     1195    if( _read_entry( BPB_FAT32_ROOTCLUS, _fat.fat_cache, 1 ) != 2 )
    10601196    {
    10611197        _printf("\n[FAT ERROR] The first cluster index must be 2\n");
     
    10631199    }
    10641200    // FS Info always in sector 1
    1065     fat.fs_info_lba         = _read_entry( BPB_FAT32_FSINFO, fat.fat_cache, 1 );
     1201    _fat.fs_info_lba         = _read_entry( BPB_FAT32_FSINFO, _fat.fat_cache, 1 );
    10661202
    10671203    // initialise fat descriptor from VBR
    1068     fat.sectors_per_cluster = _read_entry( BPB_SECPERCLUS, fat.fat_cache, 1 );
    1069     fat.sector_size         = _read_entry( BPB_BYTSPERSEC, fat.fat_cache, 1 );
    1070     fat.cluster_size        = fat.sectors_per_cluster * 512;
    1071     fat.fat_sectors         = _read_entry( BPB_FAT32_FATSZ32, fat.fat_cache, 1 );
    1072     fat.fat_lba             = _read_entry( BPB_RSVDSECCNT, fat.fat_cache, 1 );
    1073     fat.data_lba            = fat.fat_lba + fat.fat_sectors;
    1074     fat.initialised         = FAT_INITIALISED;
     1204    _fat.sectors_per_cluster = _read_entry( BPB_SECPERCLUS, _fat.fat_cache, 1 );
     1205    _fat.sector_size         = _read_entry( BPB_BYTSPERSEC, _fat.fat_cache, 1 );
     1206    _fat.cluster_size        = _fat.sectors_per_cluster * 512;
     1207    _fat.fat_sectors         = _read_entry( BPB_FAT32_FATSZ32, _fat.fat_cache, 1 );
     1208    _fat.fat_lba             = _read_entry( BPB_RSVDSECCNT, _fat.fat_cache, 1 );
     1209    _fat.data_lba            = _fat.fat_lba + _fat.fat_sectors;
     1210    _fat.initialised         = FAT_INITIALISED;
    10751211
    10761212    // initalise the lock protecting the FAT
    1077     _spin_lock_init( &fat.fat_lock );
     1213    _spin_lock_init( &_fat.fat_lock );
    10781214
    10791215    // initialise file descriptor array
    1080     for( n = 0 ; n < GIET_OPEN_FILES_MAX ; n++ ) fat.fd[n].used = 0;
    1081 
    1082 #if GIET_DEBUG_FAT
    1083 _printf("\n[FAT DEBUG] _fat_init() : FS_INFO Sector = %x\n", fat.fs_info_lba );
     1216    unsigned int   n;
     1217    for( n = 0 ; n < GIET_OPEN_FILES_MAX ; n++ ) _fat.fd[n].used = 0;
     1218
     1219#if GIET_DEBUG_FAT
     1220_printf("\n[DEBUG FAT] _fat_init() : FS_INFO Sector = %x\n", _fat.fs_info_lba );
    10841221#endif
    10851222
    10861223    // load FS_INFO into fat cache
    1087     if ( _ioc_read( 0,                  // channel
    1088                     mode,               // mode for IOC driver
    1089                     fat.fs_info_lba,    // sector index
    1090                     fat.fat_cache,      // buffer address
    1091                     1 ) )               // one sector
     1224    if ( _fat_ioc_access( use_irq,
     1225                          1,                  // read
     1226                          _fat.fs_info_lba,   
     1227                          (unsigned int)_fat.fat_cache,
     1228                          1 ) )               // one sector
    10921229    {
    10931230        _printf("\n[FAT ERROR] in _fat_init() cannot load FS_INFO Sector\n");
    10941231        _exit();
    10951232    }
    1096     fat.cache_lba = fat.fs_info_lba;
    1097 
    1098     fat.number_free_cluster    = _read_entry( FS_FREE_CLUSTER     , fat.fat_cache, 1);
    1099     fat.last_cluster_allocated = _read_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, 1);
    1100 
     1233    _fat.cache_lba = _fat.fs_info_lba;
     1234
     1235    _fat.number_free_cluster    = _read_entry( FS_FREE_CLUSTER     , _fat.fat_cache, 1);
     1236    _fat.last_cluster_allocated = _read_entry( FS_FREE_CLUSTER_HINT, _fat.fat_cache, 1);
    11011237
    11021238#if GIET_DEBUG_FAT
    11031239_fat_print();
    1104 _printf("\n[FAT DEBUG] P[%d,%d,%d] exit _fat_init()\n" );
     1240_printf("\n[DEBUG FAT] P[%d,%d,%d] exit _fat_init()\n", x,y,p );
    11051241#endif
    11061242
    11071243    return 0;
    11081244}  // end _fat_init()
    1109 
    1110 /////////////////
    1111 void _fat_print()
    1112 {
    1113     _printf("\n########################## FAT32 ################################" 
    1114             "\nFAT initialised                  %x"
    1115             "\nSector Size  (bytes)             %x"
    1116             "\nSectors per cluster              %x"
    1117             "\nFAT region first lba             %x"
    1118             "\nData region first lba            %x"
    1119             "\nNumber of sectors for one FAT    %x"
    1120             "\nNumber of free clusters          %x"
    1121             "\nLast allocated cluster           %x"
    1122             "\n#################################################################\n",
    1123             fat.initialised,
    1124             fat.sector_size,
    1125             fat.sectors_per_cluster,
    1126             fat.fat_lba,
    1127             fat.data_lba,
    1128             fat.fat_sectors,
    1129             fat.number_free_cluster,
    1130             fat.last_cluster_allocated );
    1131 }
    11321245
    11331246///////////////////////////////////////////////////////////////////////////////
     
    11421255// Returns file descriptor index if success, returns -1 if error.
    11431256///////////////////////////////////////////////////////////////////////////////
    1144 int _fat_open( unsigned     mode,
     1257int _fat_open( unsigned int use_irq,       // use descheduling mode if possible
    11451258               char*        pathname,
    11461259               unsigned int creat )
     
    11601273unsigned int y       = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1);
    11611274unsigned int p       = procid & ((1<<P_WIDTH)-1);
    1162 
    1163 _printf("\n[FAT DEBUG] P[%d,%d,%d] enters _fat_open() for path %s\n",
     1275_printf("\n[DEBUG FAT] P[%d,%d,%d] enters _fat_open() for path %s\n",
    11641276        x, y, p, pathname );
    11651277#endif
     
    11731285
    11741286    // checking FAT initialised
    1175     if( fat.initialised != FAT_INITIALISED )
     1287    if( _fat.initialised != FAT_INITIALISED )
    11761288    {
    11771289        _printf("\n[FAT ERROR] in _fat_open() : FAT not initialised\n");
     
    11791291    }
    11801292    // takes the FAT lock for exclusive access
    1181     _spin_lock_acquire( &fat.fat_lock );
    1182 
    1183 #if GIET_DEBUG_FAT
    1184 _printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] takes the FAT lock\n",
     1293    _spin_lock_acquire( &_fat.fat_lock );
     1294
     1295#if GIET_DEBUG_FAT
     1296_printf("\n[DEBUG FAT] _fat_open() : P[%d,%d,%d] takes the FAT lock\n",
    11851297        x, y, p );
    11861298#endif
     
    11991311
    12001312#if GIET_DEBUG_FAT
    1201 _printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] search file/dir %s\n",
     1313_printf("\n[DEBUG FAT] _fat_open() : P[%d,%d,%d] search file/dir %s\n",
    12021314        x, y, p, name );
    12031315#endif
     
    12111323
    12121324        // scan current directory
    1213         cluster  = _scan_directory( mode, cluster, name, &file_size, &lba );
     1325        cluster = _scan_directory( use_irq , cluster , name , &file_size , &lba );
    12141326
    12151327        if( cluster == END_OF_CHAIN_CLUSTER && last_name && creat )
     
    12201332        {
    12211333            _printf("\n[FAT ERROR] in _fat_open() cannot found %s\n", name );
    1222             _spin_lock_release( &fat.fat_lock );
     1334            _spin_lock_release( &_fat.fat_lock );
    12231335            return -1;
    12241336        }
     
    12261338
    12271339#if GIET_DEBUG_FAT
    1228 _printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] cluster for %s = %x\n",
     1340_printf("\n[DEBUG FAT] P[%d,%d,%d] in _fat_open() : cluster for %s = %x\n",
    12291341       x, y, p, pathname, cluster );
    12301342#endif
    12311343
    12321344    // check the next value for cluster index found
    1233     unsigned next = _get_next_cluster( mode, cluster );
     1345    unsigned next = _get_next_cluster( use_irq , cluster );
    12341346
    12351347    if ( (next != BAD_CLUSTER) && (next != FREE_CLUSTER) )
     
    12371349        // Search an empty slot scanning open file descriptors array
    12381350        fd_id = 0;
    1239         while ( fat.fd[fd_id].used != 0 && fd_id < GIET_OPEN_FILES_MAX )
     1351        while ( _fat.fd[fd_id].used != 0 && fd_id < GIET_OPEN_FILES_MAX )
    12401352        {
    12411353            fd_id++;
     
    12451357        if ( fd_id < GIET_OPEN_FILES_MAX )
    12461358        {
    1247             fat.fd[fd_id].used          = 1;
    1248             fat.fd[fd_id].first_cluster = cluster;
    1249             fat.fd[fd_id].file_size     = file_size;
    1250             fat.fd[fd_id].lba_dir_entry = lba;
    1251             _strcpy( fat.fd[fd_id].name, pathname );
    1252 
    1253 #if GIET_DEBUG_FAT
    1254 _printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] exit : fd = %d for file %s\n",
     1359            _fat.fd[fd_id].used          = 1;
     1360            _fat.fd[fd_id].first_cluster = cluster;
     1361            _fat.fd[fd_id].file_size     = file_size;
     1362            _fat.fd[fd_id].lba_dir_entry = lba;
     1363            _strcpy( _fat.fd[fd_id].name, pathname );
     1364
     1365#if GIET_DEBUG_FAT
     1366_printf("\n[DEBUG FAT] _fat_open() : P[%d,%d,%d] exit : fd = %d for file %s\n",
    12551367        x, y, p, fd_id, pathname );
    12561368#endif
    12571369
    12581370            // release FAT lock
    1259             _spin_lock_release( &fat.fat_lock );
     1371            _spin_lock_release( &_fat.fat_lock );
    12601372
    12611373            return fd_id;
     
    12651377            _printf("\n[FAT ERROR] in _fat_open() for file %s : fd array full\n",
    12661378                    pathname );
    1267             _spin_lock_release( &fat.fat_lock );
     1379            _spin_lock_release( &_fat.fat_lock );
    12681380            return -1;
    12691381        }
     
    12731385        _printf("\n[FAT ERROR] in _fat_open() for file %s : bad cluster\n",
    12741386                pathname );
    1275         _spin_lock_release( &fat.fat_lock );
     1387        _spin_lock_release( &_fat.fat_lock );
    12761388        return -1;
    12771389    }
     
    12851397// Returns number of sectors transfered if success, < 0 if error.
    12861398///////////////////////////////////////////////////////////////////////////////
    1287 int _fat_read( unsigned int mode,       // mode for IOC driver
     1399int _fat_read( unsigned int use_irq,    // use descheduling mode if possible
    12881400               unsigned int fd_id,      // file descriptor
    12891401               void*        buffer,     // target buffer base address
     
    12911403               unsigned int offset )    // nuber of sectors to skip in file
    12921404{
    1293     unsigned int spc = fat.sectors_per_cluster;
     1405    unsigned int spc = _fat.sectors_per_cluster;
    12941406
    12951407    unsigned int file_size;         // number of bytes in file
     
    13061418        return -1;
    13071419    }
    1308     if ( fat.fd[fd_id].used != 1 )
     1420    if ( _fat.fd[fd_id].used != 1 )
    13091421    {
    13101422        _printf("\n[FAT ERROR] in _fat_read() : file not open\n");
     
    13181430
    13191431    // compute file size as a number of sectors
    1320     file_size    = fat.fd[fd_id].file_size;
     1432    file_size    = _fat.fd[fd_id].file_size;
    13211433    if ( file_size & 0x1FF ) file_sectors = (file_size >> 9) + 1;
    13221434    else                     file_sectors = (file_size >> 9);
     
    13371449   
    13381450    // get first cluster index
    1339     cluster = fat.fd[fd_id].first_cluster;
     1451    cluster = _fat.fd[fd_id].first_cluster;
    13401452
    13411453#if GIET_DEBUG_FAT
     
    13441456unsigned int y       = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1);
    13451457unsigned int p       = procid & ((1<<P_WIDTH)-1);
    1346 _printf("\n[FAT DEBUG] _fat_read() : P[%d,%d,%d] enters for file %s\n"
     1458_printf("\n[DEBUG FAT] _fat_read() : P[%d,%d,%d] enters for file %s\n"
    13471459        " - buffer vbase     = %x\n"
    13481460        " - skipped sectors  = %x\n"
     
    13501462        " - first cluster    = %x\n"
    13511463        " - skipped clusters = %x\n",
    1352         x, y, p,
    1353         fat.fd[fd_id].name,
    1354         (unsigned int)buffer,
    1355         offset,
    1356         count,
    1357         cluster,
    1358         clusters_to_skip );
     1464        x, y, p, _fat.fd[fd_id].name, (unsigned int)buffer,
     1465        offset, count, cluster, clusters_to_skip );
    13591466#endif
    13601467
    13611468    // compute index of first cluster to be loaded
    1362     // as we may need to scan the FAT, we use the kernel mode
    13631469    while ( clusters_to_skip )
    13641470    {
    1365         cluster = _get_next_cluster( IOC_KERNEL_MODE, cluster );
     1471        cluster = _get_next_cluster( use_irq , cluster );
    13661472        clusters_to_skip--;
    13671473    }
     
    13711477    unsigned int    lba;            // first sector index on device
    13721478    unsigned int    iter_sectors;   // number of sectors to load in iteration
    1373     char*           dst;            // pointer on target buffer
     1479    unsigned int    dst;            // pointer on target buffer
    13741480
    13751481    // initialize these variables for the first iteration
    13761482    todo_sectors  = total_sectors;
    1377     dst           = (char*)buffer;
     1483    dst           = (unsigned int)buffer;
    13781484    lba           = cluster_to_lba(cluster) + sectors_to_skip;
    13791485    if( total_sectors < (spc - sectors_to_skip) ) iter_sectors = total_sectors;
     
    13851491
    13861492#if GIET_DEBUG_FAT
    1387 _printf("\n[FAT DEBUG] _fat_read() : P[%d,%d,%d] makes an IOC read\n"
    1388         " - cluster = %x\n"
    1389         " - buffer  = %x\n"
    1390         " - lba     = %x\n"
    1391         " - sectors = %x\n",
    1392         x, y, p,
    1393         cluster,
    1394         (unsigned int)dst,
    1395         lba,
    1396         iter_sectors );
    1397 #endif
    1398 
    1399         if( _ioc_read( 0,                 // channel
    1400                        mode,              // mode for IOC driver
    1401                        lba,               // first sector index
    1402                        dst,               // buffer address
    1403                        iter_sectors ) )   // number of sectors
     1493_printf("\n[DEBUG FAT] _fat_read() : P[%d,%d,%d] makes an IOC read\n"
     1494        "  cluster = %x / buffer = %x / lba = %x / sectors = %d\n",
     1495        x, y, p, cluster, dst, lba, iter_sectors );
     1496#endif
     1497
     1498        if( _fat_ioc_access( use_irq,
     1499                             1,                 // read
     1500                             lba,
     1501                             dst,               // buffer address
     1502                             iter_sectors ) )   // number of sectors
    14041503        {
    14051504            _printf("\n[FAT ERROR] in _fat_read() cannot load block %x", lba );
     
    14081507         
    14091508        // update variables for next iteration
    1410         cluster      = _get_next_cluster( mode, cluster );
     1509        cluster      = _get_next_cluster( use_irq , cluster );
    14111510        todo_sectors = todo_sectors - iter_sectors;
    14121511        dst          = dst + (iter_sectors << 9);
     
    14261525// Allocate new clusters if the offset+count larger than current file size,
    14271526// but the offset should be smaller than the current file size...
    1428 // - fat    : pointer on FAT
    1429 // - mode   : mode for the IOC driver
    1430 // - fd_id  : open file descriptor index 
    1431 // - buffer : base address of the memory buffer (must be sector aligned)
    1432 // - offset : number of sectors to skip in file
    1433 // - count  : number of sectors to be written.
    14341527///////////////////////////////////////////////////////////////////////////////
    14351528// Returns number of sectors written if success, < 0 if error.
    14361529///////////////////////////////////////////////////////////////////////////////
    1437 int _fat_write( unsigned int mode,       // mode for IOC driver
     1530int _fat_write( unsigned int use_irq,    // use descheduling mode if possible
    14381531                unsigned int fd_id,      // file descriptor
    14391532                void*        buffer,     // target buffer base address
     
    14421535{
    14431536
    1444     unsigned int spc = fat.sectors_per_cluster;
     1537    unsigned int spc = _fat.sectors_per_cluster;
    14451538
    14461539    unsigned int file_size;         // number of bytes in file
     
    14541547
    14551548    // compute file size as a number of sectors
    1456     file_size    = fat.fd[fd_id].file_size;
     1549    file_size    = _fat.fd[fd_id].file_size;
    14571550    if ( file_size & 0x1FF ) file_sectors = (file_size >> 9) + 1;
    14581551    else                     file_sectors = (file_size >> 9);
     
    14731566unsigned int p       = procid & ((1<<P_WIDTH)-1);
    14741567
    1475 _printf("\n[FAT DEBUG] _fat_write() : P[%d,%d,%d] enters for file %s\n"
     1568_printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] enters for file %s\n"
    14761569        " - buffer vbase    = %x\n"
    14771570        " - skipped sectors = %x\n"
     
    14791572        " - file sectors    = %x\n"
    14801573        " - need_allocate   = %d\n",
    1481         x, y, p,
    1482         fat.fd[fd_id].name
    1483         (unsigned int)buffer,
    1484         offset,
    1485         count,
    1486         file_sectors,
    1487         allocate );
     1574        x, y, p, _fat.fd[fd_id].name, (unsigned int)buffer,
     1575        offset, count, file_sectors, allocate );
    14881576#endif
    14891577
     
    14941582        return -1;
    14951583    }
    1496     if ( fat.fd[fd_id].used != 1 )
     1584    if ( _fat.fd[fd_id].used != 1 )
    14971585    {
    14981586        _printf("\n[FAT ERROR] in _fat_write() : file not open\n");
     
    15071595    if ( allocate  )
    15081596    {
    1509         if ( _fat_allocate( fd_id, (required_cluster - current_cluster) ) < 0 )
     1597        if ( _fat_allocate( use_irq , fd_id, (required_cluster-current_cluster) ) )
    15101598        {
    15111599            _printf("\n[FAT ERROR] in _fat_write() : fat_allocate failed\n");
     
    15191607   
    15201608    // get first cluster index
    1521     cluster = fat.fd[fd_id].first_cluster;
    1522 
    1523 #if GIET_DEBUG_FAT
    1524 _printf("\n[FAT DEBUG] _fat_write() : P[%d,%d,%d] get cluster %x\n",
     1609    cluster = _fat.fd[fd_id].first_cluster;
     1610
     1611#if GIET_DEBUG_FAT
     1612_printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] get cluster %x\n",
    15251613        x, y, p, cluster );
    15261614#endif
    15271615
    15281616    // compute index of first cluster to be loaded
    1529     // as we may need to scan the FAT, we use the kernel mode
    15301617    while ( clusters_to_skip )
    15311618    {
    1532         cluster = _get_next_cluster( IOC_KERNEL_MODE, cluster );
     1619        cluster = _get_next_cluster( use_irq , cluster );
    15331620        clusters_to_skip--;
    15341621    }
     
    15381625    unsigned int    lba;            // first sector index on device
    15391626    unsigned int    iter_sectors;   // number of sectors to load in iteration
    1540     char*           src;            // pointer on target buffer
     1627    unsigned int    src;            // pointer on target buffer
    15411628
    15421629    // initialize these variables for the first iteration
    15431630    todo_sectors  = count;
    1544     src           = (char*)buffer;
     1631    src           = (unsigned int)buffer;
    15451632    lba           = cluster_to_lba(cluster) + sectors_to_skip;
    15461633    if( count < (spc - sectors_to_skip) ) iter_sectors = count;
     
    15521639
    15531640#if GIET_DEBUG_FAT
    1554 _printf("\n[FAT DEBUG] _fat_write() : P[%d,%d,%d] makes an IOC write"
    1555         " - cluster  = %x\n"
    1556         " - buffer   = %x\n"
    1557         " - lba      = %x\n"
    1558         " - sectors  = %x\n",
    1559         x, y, p,
    1560         cluster,
    1561         (unsigned int) src,
    1562         lba,
    1563         iter_sectors );
    1564 #endif
    1565 
    1566         if( _ioc_write( 0,                 // channel
    1567                         mode,              // mode for IOC driver
    1568                         lba,               // first sector index
    1569                         src,               // source buffer address
    1570                         iter_sectors ) )   // number of sectors
     1641_printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] makes an IOC write"
     1642        "  cluster = %x / buffer = %x / lba = %x / sectors = %d\n",
     1643        x, y, p, cluster, src, lba, iter_sectors );
     1644#endif
     1645
     1646        if( _fat_ioc_access( use_irq,
     1647                             0,                 // write
     1648                             lba,
     1649                             src,               // source buffer address
     1650                             iter_sectors ) )   // number of sectors
    15711651        {
    15721652            _printf("\n[FAT ERROR] in _fat_write() cannot write block %x\n", lba );
     
    15751655         
    15761656        // update variables for next iteration
    1577         cluster      = _get_next_cluster( mode, cluster );
     1657        cluster      = _get_next_cluster( use_irq , cluster );
    15781658        todo_sectors = todo_sectors - iter_sectors;
    15791659        src          = src + (iter_sectors << 9);
     
    15871667    if ( ( offset + count ) > file_sectors )
    15881668    {
    1589         fat.fd[fd_id].file_size = (count + offset) << 9;
     1669        _fat.fd[fd_id].file_size = (count + offset) << 9;
    15901670    }
    15911671
    15921672    // Update entry of directory with the new value
    15931673    // of file size (Field : DIR_FILE_SIZE)
    1594     if ( update_entry(fd_id, DIR_FILE_SIZE, fat.fd[fd_id].file_size) )
    1595     {
    1596             _printf("\n[FAT ERROR] in _fat_write() update entry failed\n");
    1597             return -1;
     1674    if ( _update_entry( use_irq, fd_id , DIR_FILE_SIZE , _fat.fd[fd_id].file_size ) )
     1675    {
     1676        _printf("\n[FAT ERROR] in _fat_write() update entry failed\n");
     1677        return -1;
    15981678    }
    15991679         
     
    16151695    if( (fd_id < GIET_OPEN_FILES_MAX) )
    16161696    {
    1617         file_size = fat.fd[fd_id].file_size;
     1697        file_size = _fat.fd[fd_id].file_size;
    16181698
    16191699        if ( file_size & 0x1FF ) file_sectors = (file_size >> 9) + 1;
     
    16381718    if( (fd_id < GIET_OPEN_FILES_MAX) )
    16391719    {
    1640         fat.fd[fd_id].used = 0;
     1720        _fat.fd[fd_id].used = 0;
    16411721        return 0;
    16421722    }
     
    16481728} // end fat_close()
    16491729
    1650 /////////////////////////////////////////////////////////////////////////////////////
     1730/////////////////////////////////////////////////////////////////////////////////
    16511731// The following function implement the user_level system call.
    16521732// The flags argument is not used, as file access modes are not implemented yet.
    1653 /////////////////////////////////////////////////////////////////////////////////////
     1733/////////////////////////////////////////////////////////////////////////////////
    16541734// Return the file descriptor index if success / return -1 if failure
    1655 /////////////////////////////////////////////////////////////////////////////////////
     1735/////////////////////////////////////////////////////////////////////////////////
    16561736int _fat_user_open( char*  pathname,         // absolute pathname from root
    16571737                    unsigned int flags )     // unused: TODO
    16581738{
    1659     return _fat_open( IOC_KERNEL_MODE,       // we use KERNEL_MODE, because
    1660                       pathname,              // we need to write into FAT cache
    1661                       0 );                   // no creation if not found
     1739    return _fat_open( 1,        // use descheduling mode if possible
     1740                      pathname,
     1741                      0 );      // no creation if not found
    16621742}
    16631743
    1664 /////////////////////////////////////////////////////////////////////////////////////
     1744/////////////////////////////////////////////////////////////////////////////////
    16651745// The following function implement the user_level system call.
    1666 // This function should be modified to respect the UNIX specification 
    1667 /////////////////////////////////////////////////////////////////////////////////////
     1746// This function should be modified to respect the UNIX specification.
     1747/////////////////////////////////////////////////////////////////////////////////
    16681748// Return number of sectors actually transfered if success / return -1 if failure
    1669 /////////////////////////////////////////////////////////////////////////////////////
     1749/////////////////////////////////////////////////////////////////////////////////
    16701750int _fat_user_read( unsigned int fd,        // file descriptor index
    16711751                    void*        buffer,    // destination buffer
     
    16731753                    unsigned int offset )   // number of sectors to skip
    16741754{
    1675     return _fat_read( IOC_USER_MODE,
     1755    return _fat_read( 1,        // use descheduling mode if possible
    16761756                      fd,
    16771757                      buffer, 
     
    16801760}
    16811761
    1682 /////////////////////////////////////////////////////////////////////////////////////
     1762/////////////////////////////////////////////////////////////////////////////////
    16831763// The following function implement the user_level system call.
    16841764// This function should be modified to respect the UNIX specification.
    1685 /////////////////////////////////////////////////////////////////////////////////////
     1765/////////////////////////////////////////////////////////////////////////////////
    16861766// Return number of sectors actually transfered if success / return -1 if failure
    1687 /////////////////////////////////////////////////////////////////////////////////////
     1767/////////////////////////////////////////////////////////////////////////////////
    16881768int _fat_user_write( unsigned int fd,       // file descriptor
    16891769                     void*        buffer,   // source buffer
     
    16911771                     unsigned int offset )  // number of sectors to skip on file
    16921772{
    1693     return _fat_write( IOC_USER_MODE,
     1773    return _fat_write( 1,       // use descheduling mode if possible
    16941774                       fd,
    16951775                       buffer, 
     
    16981778}
    16991779
    1700 /////////////////////////////////////////////////////////////////////////////////////
     1780/////////////////////////////////////////////////////////////////////////////////
    17011781int _fat_user_lseek( unsigned int fd_id,
    17021782                     unsigned int offset,
  • soft/giet_vm/giet_fat32/fat32.h

    r503 r530  
    1 ////////////////////////////////////////////////////////////////////////////
     1////////////////////////////////////////////////////////////////////////////////
    22// File     : fat32.h
    33// Date     : 01/09/2013
    44// Author   : Marco Jankovic / Alain Greiner
    55// Copyright (c) UPMC-LIP6
    6 ////////////////////////////////////////////////////////////////////////////
     6////////////////////////////////////////////////////////////////////////////////
    77
    88#ifndef _FAT32_H
     
    1212#include "kernel_locks.h"
    1313
    14 /*************** Partition Boot Sector Format ******************************************/
     14/*************** Partition Boot Sector Format **********************************/
    1515//                                     offset |  length
    1616#define BS_JMPBOOT                          0 ,  3
     
    5656#define FOURTH_PARTITION_BEGIN_LBA        502 ,  4
    5757#define FOURTH_PARTITION_SIZE             506 ,  4   
    58 /***************************************************************************************/
     58/*******************************************************************************/
    5959
    6060#define MBR_SIGNATURE_POSITION            510 , 2
    6161#define MBR_SIGNATURE_VALUE               0xAA55 
    6262
    63 /************** FAT_FS_INFO SECTOR  ****************************************************/
     63/************** FAT_FS_INFO SECTOR  ********************************************/
    6464#define FS_SIGNATURE_VALUE_1              0x52526141
    6565#define FS_SIGNATURE_VALUE_2              0x72724161
     
    7070#define FS_FREE_CLUSTER                   488 , 4
    7171#define FS_FREE_CLUSTER_HINT              492 , 4
    72 /***************************************************************************************/
     72/*******************************************************************************/
    7373
    7474#define DIR_ENTRY_SIZE          32
    7575                   
    76 /******* Directory Entry Structure (32 bytes) ******************************************/
     76/******* Directory Entry Structure (32 bytes) **********************************/
    7777//                          offset | length
    7878#define DIR_NAME                 0 , 11   // dir_entry name
     
    8585#define DIR_FST_CLUS_LO         26 ,  2   // cluster index 16 LSB bit
    8686#define DIR_FILE_SIZE           28 ,  4   // file size (up to 4 giga bytes)
    87 /***************************************************************************************/
    88 
    89 /******* LFN Directory Entry Structure  (32 bytes) *************************************/
     87/*******************************************************************************/
     88
     89/******* LFN Directory Entry Structure  (32 bytes) *****************************/
    9090//                          offset | length
    9191#define LDIR_ORD                 0 ,  1   // Sequence number (from 0x01 to 0x0f)   
     
    9797#define LDIR_RSVD               26 ,  2   // artifact of previous fat (must be 0)
    9898#define LDIR_NAME_3             28 ,  4   
    99 /***************************************************************************************/
    100 
    101 /***********************  DIR_ATTR values  (attributes) ********************************/
     99/*******************************************************************************/
     100
     101/***********************  DIR_ATTR values  (attributes) ************************/
    102102#define ATTR_READ_ONLY          0x01
    103103#define ATTR_HIDDEN             0x02
     
    107107#define ATTR_ARCHIVE            0x20
    108108#define ATTR_LONG_NAME_MASK     0x0f      // READ_ONLY|HIDDEN|SYSTEM|VOLUME_ID
    109 /***************************************************************************************/
    110 
    111 /********************* DIR_ORD special values ******************************************/
     109/*******************************************************************************/
     110
     111/********************* DIR_ORD special values **********************************/
    112112#define FREE_ENTRY              0xE5     // this entry is free in the directory
    113113#define NO_MORE_ENTRY           0x00     // no more entry in the directory
    114 /***************************************************************************************/
    115 
    116 /******************** CLuster Index Special Values *************************************/
     114/*******************************************************************************/
     115
     116/******************** CLuster Index Special Values *****************************/
    117117#define FREE_CLUSTER            0x00000000
    118118#define RESERVED_CLUSTER        0x00000001
    119119#define BAD_CLUSTER             0x0FFFFFF7
    120120#define END_OF_CHAIN_CLUSTER    0x0ffffff8
    121 /***************************************************************************************/
     121/*******************************************************************************/
    122122
    123123#define FAT_INITIALISED         0xBABEF00D
    124124
    125 /************ This struct defines a file descriptor ************************************/
     125/************ This struct defines a file descriptor ****************************/
    126126typedef struct file_descriptor_s
    127127{
     
    132132   char          name[244];                 // pathname
    133133}  file_desc_t;
    134 /***************************************************************************************/
    135 
    136 /************ This struct describes a FAT32 disk **********************************/
     134/*******************************************************************************/
     135
     136/************ This struct describes a FAT32 disk *******************************/
    137137typedef struct fat32_fs_s
    138138{
    139     char            fat_cache[512];          // FAT cache: 1 sector = 128 cluster indexes
     139    char            fat_cache[512];          // FAT cache: 1 sector
    140140    spin_lock_t     fat_lock;                // lock protecting exclusive access
    141141    file_desc_t     fd[GIET_OPEN_FILES_MAX]; // file descriptors array
    142     unsigned int    initialised;             // contains 0xBABEF00D when FAT initialised
     142    unsigned int    initialised;             // 0xBABEF00D when FAT initialised
    143143    unsigned int    sector_size;             // number of bytes (power of 2)
    144144    unsigned int    sectors_per_cluster;     // power of 2 (must be greater than 4)
    145     unsigned int    cluster_size;            // sector_size * sector_per_cluster (bytes)
    146     unsigned int    fat_sectors;             // number of sectors occupied by one FAT copy
     145    unsigned int    cluster_size;            // sector_size * sector_per_cluster
     146    unsigned int    fat_sectors;             // number of sectors for 1 FAT copy
    147147    unsigned int    fat_lba;                 // lba of first FAT sector
    148148    unsigned int    data_lba;                // lba of first data sector 
     
    152152    unsigned int    fs_info_lba;             // lba of fs_info
    153153} fat32_fs_t;
    154 /***************************************************************************************/
    155 
    156 /*********************** Export Functions  *********************************************/
    157 
    158 extern void _fat_print();
    159 
    160 // functions used for low level access to fat
    161 // (move sectors between block device and system buffers)
    162 
    163 extern int _fat_init(  unsigned int mode );        // mode for IOC driver
    164 
    165 extern int _fat_open(  unsigned int mode,          // mode for IOC driver
    166                        char*  pathname,            // file pathname from root
    167                        unsigned int create );      // create new file if non zero
    168 
    169 extern int _fat_read(  unsigned int mode,          // mode for IOC driver
    170                        unsigned int fd_id,         // file descriptor index
    171                        void*        buffer,        // destination buffer
    172                        unsigned int count,            // number of sectors to read
    173                        unsigned int offset );      // offset sector in file
    174 
    175 extern int _fat_write( unsigned int mode,         // mode for IOC driver
    176                        unsigned int fd_id,        // file descriptor index
    177                        void*        buffer,               // source buffer
    178                        unsigned int count,        // number of sectors to write
    179                        unsigned int offset );     // offset sector in file
    180 
    181 extern int _fat_fstat( unsigned int fd_id );      // file descriptor index
    182 
    183 extern int _fat_close( unsigned int fd_id );      // file descriptor index
    184 
    185 
    186 // functions used by user system calls
    187 // (should move bytes between system buffers and user buffers)
    188 
    189 extern int _fat_user_open(  char*        pathname,  // file pathname from root
    190                             unsigned int flags );   // unused
     154/*******************************************************************************/
     155
     156/*********************** Extern Functions  *************************************/
     157
     158extern int _fat_init( unsigned int use_irq );       // use IRQ if possible
     159
     160extern int _fat_open(  unsigned int use_irq,        // use IRQ if possible
     161                       char*  pathname,             // file pathname from root
     162                       unsigned int create );       // create new file if non zero
     163
     164extern int _fat_read(  unsigned int use_irq,        // use IRQ if possible
     165                       unsigned int fd_id,          // file descriptor index
     166                       void*        buffer,         // destination buffer
     167                       unsigned int count,          // number of sectors to read
     168                       unsigned int offset );       // offset sector in file
     169
     170extern int _fat_write( unsigned int use_irq,        // use IRQ if possible
     171                       unsigned int fd_id,          // file descriptor index
     172                       void*        buffer,                 // source buffer
     173                       unsigned int count,          // number of sectors to write
     174                       unsigned int offset );       // offset sector in file
     175
     176extern int _fat_fstat( unsigned int fd_id );        // file descriptor index
     177
     178extern int _fat_close( unsigned int fd_id );        // file descriptor index
     179
     180
     181extern int _fat_user_open( char*        pathname,   // file pathname from root
     182                           unsigned int flags );    // unused
    191183
    192184extern int _fat_user_read(  unsigned int fd_id,     // file descriptor index
    193185                            void*        buffer,    // destination buffer
    194186                            unsigned int count,     // number of sectors to read
    195                             unsigned int offset );  // number of sectors to skip in file
     187                            unsigned int offset );  // sectors to skip in file
    196188
    197189extern int _fat_user_write( unsigned int fd_id,     // file descriptor index
    198190                            void*        buffer,    // source buffer
    199191                            unsigned int count,     // number of sectors to write
    200                             unsigned int offset );  // number of sectors to skip in file
     192                            unsigned int offset );  // sectors to skip in file
    201193
    202194extern int _fat_user_lseek( unsigned int fd_id,
     
    204196                            unsigned int whence ); 
    205197
    206 /***************************************************************************************/
     198/*******************************************************************************/
    207199
    208200
Note: See TracChangeset for help on using the changeset viewer.