Ignore:
Timestamp:
Sep 29, 2014, 12:08:39 PM (10 years ago)
Author:
alain
Message:

Cosmetic.

File:
1 edited

Legend:

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

    r360 r417  
    1717// 2. This FAT32 library uses a FAT cache whose storage capacity is one
    1818//    sector (512 bytes, or 128 cluster indexes in FAT)
    19 // 3. This FAT32 library can be used in four modes: BOOT_PA/BOOT_VA/KERNEL/USER
     19// 3. This FAT32 library can be used in 3 modes: BOOT/BOOT/KERNEL/USER
    2020//    defining different behaviours for the IOC driver.
    2121//////////////////////////////////////////////////////////////////////////////////
     
    2828
    2929//////////////////////////////////////////////////////////////////////////////////
    30 //      Global variable used by all FAT access functions
    31 //////////////////////////////////////////////////////////////////////////////////
    32 
    33 __attribute__((section (".fatdata")))
    34 fat32_fs_t fat __attribute__((aligned(512)));
     30//      Global variable : internal FAT representation
     31//////////////////////////////////////////////////////////////////////////////////
     32
     33extern fat32_fs_t fat __attribute__((aligned(512)));
    3534
    3635//////////////////////////////////////////////////////////////////////////////////
     
    4241    unsigned int word;
    4342    unsigned int temp[9];
     43    unsigned int save_sr;  // for tty_get_lock()
     44 
    4445
    4546    temp[8] = 0;
    4647
    47     _printf("\n*********************** fat_cache_lba = %x *****************************\n",
    48             fat.cache_lba );
    49 
    50     for ( line=0 ; line<16 ; line++ )
    51     {
    52         // display line index
    53         _printf( "%x : ", line );
    54 
    55         // display data hexa
     48    _tty_get_lock( 0, &save_sr );
     49
     50    _puts("\n*********************** fat_cache_lba = ");
     51    _putx( fat.cache_lba );
     52    _puts(" *****************************\n");
     53
     54    for ( line = 0 ; line < 16 ; line++ )
     55    {
     56        // display line index
     57        _putx( line );
     58        _puts(" : ");
     59
     60        // display 8*4 bytes hexa
    5661        for ( word=0 ; word<8 ; word++ )
    5762        {
     
    6166                                 (fat.fat_cache[byte+2]<< 8) |
    6267                                 (fat.fat_cache[byte+3]);
    63             _printf("%x | ", hexa );
     68            _putx( hexa );
     69            _puts(" | ");
    6470
    6571            // prepare display ascii
     
    7177       
    7278        // display data ascii
    73         _printf( (char*)temp );
    74         _printf("\n");
    75     }
    76     _printf("**************************************************************************\n");
     79        _puts( (char*)temp );
     80        _puts("\n");
     81    }
     82    _puts("***************************************************************************\n");
     83
     84    _tty_release_lock( 0, &save_sr );
     85
    7786} // end display_fat_cache() 
    7887
    7988//////////////////////////////////////////////////////////////////////////////////
    8089// This function returns the length of a FAT field. This field is identified
    81 // man by an (offset,length) mnemonic defined in fat32.h file.
     90// by an (offset,length) mnemonic defined in fat32.h file.
    8291//////////////////////////////////////////////////////////////////////////////////
    8392static inline int get_length( int offset,
    84                               int length)
     93                              int length )
    8594{
    8695    return length;
     
    8998//////////////////////////////////////////////////////////////////////////////
    9099// Write one 32 bits word "value" in a char[] buffer.
    91 // This field is defined by the offset and size arguments.
     100// The modified field in buffer is defined by the offset and size arguments.
    92101//////////////////////////////////////////////////////////////////////////////
    93 static void write_entry( unsigned int   offset,
     102static void _write_entry( unsigned int   offset,
    94103                         unsigned int   size,
    95104                         char*          buffer,
     
    111120//////////////////////////////////////////////////////////////////////////////
    112121// Read one 32 bits word in a char[] buffer, taking endianness into account.
    113 // This field is defined by the offset and size arguments.
     122// The analysed field in buffer is defined by the offset and size arguments.
    114123//////////////////////////////////////////////////////////////////////////////
    115 static unsigned int read_entry( unsigned int   offset,
    116                                 unsigned int   size,
    117                                 char*          buffer,
    118                                 unsigned int   little_indian )
     124static unsigned int _read_entry( unsigned int   offset,
     125                                 unsigned int   size,
     126                                 char*          buffer,
     127                                 unsigned int   little_indian )
    119128{
    120129    unsigned int turn;
     
    176185// remark: a sector of FAT contains 128 cluster indexes.
    177186/////////////////////////////////////////////////////////////////////////////////
    178 static unsigned int get_next_cluster_id( unsigned int mode,
    179                                          unsigned int cluster )
     187static unsigned int _get_next_cluster( unsigned int mode,
     188                                       unsigned int cluster )
    180189{
    181190    // compute lba of the sector containing the cluster index
    182191    unsigned int lba = fat.fat_lba + (cluster / 128);
    183192
    184     if ( lba == fat.cache_lba )      // hit in cache
    185     {
    186         return read_entry( ((cluster % 128) * 4),
    187                            4,
    188                            fat.fat_cache,
    189                            1 );
    190     }
    191     else                                  // miss in cache
     193#if GIET_DEBUG_FAT
     194unsigned int procid  = _get_procid();
     195unsigned int cid     = procid / NB_PROCS_MAX;
     196unsigned int lpid    = procid % NB_PROCS_MAX;
     197unsigned int x       = cid >> Y_WIDTH;
     198unsigned int y       = cid & ((1<<Y_WIDTH) - 1);
     199_printf("\n[FAT DEBUG] _get_next_cluster() : P[%d,%d,%d] enters for cluster %x\n",
     200        x, y, lpid, cluster );
     201#endif
     202
     203    if ( lba != fat.cache_lba )      // miss in fat_cache
    192204    {
    193205        // we cannot access fat in user mode
     
    206218        }
    207219        fat.cache_lba = lba;
    208 
    209         return read_entry( ((cluster % 128) * 4),
    210                            4,
    211                            fat.fat_cache,
    212                            1 );
    213     }
     220   
     221#if (GIET_DEBUG_FAT > 1)
     222display_fat_cache();
     223#endif
     224
     225    }
     226    unsigned int next = _read_entry( ((cluster % 128) * 4),
     227                                     4,
     228                                     fat.fat_cache,
     229                                     1 );
     230
     231#if GIET_DEBUG_FAT
     232_printf("\n[FAT DEBUG] _get_next_cluster() : P[%d,%d,%d] next cluster = %x\n",
     233        x, y, lpid, next );
     234#endif
     235
     236    return next;
    214237}
    215238
     
    452475    char name_entry[14];  // buffer to store a 13 characters (partial) name
    453476    char file_name[256];  // buffer to store the name (not pathname) of the file
    454     char sfn[12]            = {[0 ... 10] = ' ', '\0'};      // buffer for a Short File Name
    455 
    456     unsigned int lba       = fat.fd[fd_id].lba_dir_entry;    // Lba of file dir_entry
     477
     478    char sfn_string[12]  = {[0 ... 10] = ' ', '\0'};       // buffer for a Short File Name
     479    unsigned int lba     = fat.fd[fd_id].lba_dir_entry;    // Lba of file dir_entry
    457480    unsigned int is_sfn;         
    458     unsigned int attr      = 0;                              // directory entry attribute
    459     unsigned int ord       = 0;                              // directory entry sequence
    460     unsigned int found     = 0;                              // name found
    461     unsigned int offset    = 0;
    462     unsigned int i         = 0;
    463     unsigned int nb_read   = 0;
     481    unsigned int attr    = 0;                              // directory entry attribute
     482    unsigned int ord     = 0;                              // directory entry sequence
     483    unsigned int found   = 0;                              // name found
     484    unsigned int offset  = 0;                              // offset in fat_cache
     485    unsigned int i       = 0;
     486    unsigned int nb_read = 0;
    464487
    465488    for ( i = 0 ; i < 32 ; i++ ) dir_entry[i]  = 0;
     
    471494    }
    472495
    473     // Check if file_name is short
    474     is_sfn = is_short( file_name, sfn );
     496    // Format file_name to SFN format
     497    is_sfn = is_short( file_name, sfn_string );
    475498
    476499    if ( _ioc_read( 0,               // channel
     
    489512    while ( !found )
    490513    {
    491         attr = read_entry( DIR_ATTR, fat.fat_cache + offset, 0 );
    492         ord  = read_entry( LDIR_ORD, fat.fat_cache + offset, 0 );
     514        attr = _read_entry( DIR_ATTR, fat.fat_cache + offset, 0 );
     515        ord  = _read_entry( LDIR_ORD, fat.fat_cache + offset, 0 );
    493516
    494517        if ( is_sfn == 1 )                                     // searched name is short
     
    551574            get_name_from_short( dir_entry, name_entry );
    552575
    553             if ( _strncmp( (char*)sfn, (char*)name_entry, 13 ) == 0 )
    554             {
    555                 write_entry(offset + field, size, fat.fat_cache, value);
     576            if ( _strncmp( (char*)sfn_string, (char*)name_entry, 13 ) == 0 )
     577            {
     578                _write_entry(offset + field, size, fat.fat_cache, value);
    556579                found = 1;
    557580            }
     
    571594                {
    572595                    offset = offset + DIR_ENTRY_SIZE;
    573                     write_entry(offset + field, size, fat.fat_cache, value);
     596                    _write_entry(offset + field, size, fat.fat_cache, value);
    574597                    found = 1;
    575598                }
     
    595618// Return 0 in case of success, > 0 if failure.
    596619//////////////////////////////////////////////////////////////////////////////////
    597 static inline unsigned int update_fs_info( )
     620static inline unsigned int _update_fs_info( )
    598621{
    599622    unsigned int lba = fat.fs_info_lba;
    600623
    601624#if GIET_DEBUG_FAT
    602 _printf("\n[FAT DEBUG] Enter update_fs_info()\n");
     625_printf("\n[FAT DEBUG] _update_fs_info() : enters\n");
    603626#endif
    604627
    605628    if ( lba == fat.cache_lba )            // hit cache
    606629    {
    607         write_entry( FS_FREE_CLUSTER     , fat.fat_cache, fat.number_free_cluster );
    608         write_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, fat.last_cluster_allocated );
     630        _write_entry( FS_FREE_CLUSTER     , fat.fat_cache, fat.number_free_cluster );
     631        _write_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, fat.last_cluster_allocated );
    609632    }
    610633    else                                   // miss cache
     
    616639                        1 ) )              // one sector
    617640        {
    618             _printf("[FAT_ERROR] in update_fat() cannot read block %x\n", lba );
     641            _printf("[FAT_ERROR] in _update_fat() cannot read block %x\n", lba );
    619642            return 1;
    620643        }
    621644        fat.cache_lba = lba;
    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 );
     645        _write_entry( FS_FREE_CLUSTER     , fat.fat_cache, fat.number_free_cluster );
     646        _write_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, fat.last_cluster_allocated );
    624647    }
    625648    return _ioc_write( 0,                // channel
     
    635658// Return 0 in case of success, > 0 if failure.
    636659//////////////////////////////////////////////////////////////////////////////////
    637 static inline unsigned int update_fat( unsigned int cluster,
    638                                        unsigned int value  )
     660static inline unsigned int _update_fat( unsigned int cluster,
     661                                        unsigned int value  )
    639662{
    640663    unsigned int lba = fat.fat_lba + (cluster / 128);
    641664
    642665#if GIET_DEBUG_FAT
    643 _printf("\n[FAT DEBUG] Enter update_fat() : cluster = %x / value = %x\n",
     666_printf("\n[FAT DEBUG] _update_fat() : cluster = %x / value = %x\n",
    644667        cluster, value );
    645668#endif
     
    647670    if ( lba == fat.cache_lba )            // hit cache
    648671    {
    649         write_entry( ((cluster % 128) << 2), 4, fat.fat_cache, value );
     672        _write_entry( ((cluster % 128) << 2), 4, fat.fat_cache, value );
    650673    }
    651674    else                                   // miss cache
     
    657680                        1 ) )              // one sector
    658681        {
    659             _printf("[FAT_ERROR] in update_fat() cannot read block %x\n");
     682            _printf("[FAT_ERROR] in _update_fat() cannot read block %x\n");
    660683            return 1;
    661684        }
    662685        fat.cache_lba = lba;
    663         write_entry( ((cluster % 128) << 2), 4, fat.fat_cache, value );
     686        _write_entry( ((cluster % 128) << 2), 4, fat.fat_cache, value );
    664687    }
    665688    return _ioc_write( 0,                // channel
     
    668691                       fat.fat_cache,    // source buffer
    669692                       1 );              // one sector
    670 }
     693} // end update_fat()
    671694
    672695//////////////////////////////////////////////////////////////////////////////////
    673696// This function allocate a count number of cluster to a file by calling the
    674 // update_fat function that takes care to update the chaining of clusters.
     697// _update_fat function that takes care to update the chaining of clusters.
    675698// return 0 if success, -1 if failure
    676699//////////////////////////////////////////////////////////////////////////////////
     
    678701                                 unsigned int count )
    679702{
    680     unsigned int next_cluster        = fat.fd[fd_id].first_cluster;   // Get the first cluster of file
    681     unsigned int cluster_to_allocate = count;                         // Number of cluster to allocate
    682 
    683     unsigned int last_cluster_file;                                   // Last cluster of the file (EOC)
    684 
    685     unsigned int free_cluster = fat.last_cluster_allocated + 1;       // First free cluster
     703    unsigned int next_cluster = fat.fd[fd_id].first_cluster;    // Get the first cluster of file
     704    unsigned int cluster_to_allocate = count;                   // Number of cluster to allocate
     705    unsigned int last_cluster_file;                             // Last cluster of the file (EOC)
     706    unsigned int free_cluster = fat.last_cluster_allocated + 1; // First free cluster
    686707
    687708    // Check if free_cluster is really free (must be true)
    688     if ( get_next_cluster_id( IOC_KERNEL_MODE, free_cluster ) != FREE_CLUSTER)
     709    if ( _get_next_cluster( IOC_KERNEL_MODE, free_cluster ) != FREE_CLUSTER)
    689710    {
    690711        _printf("\n[FAT ERROR] in _fat_allocate() : first free_cluster not free\n");
     
    699720
    700721#if GIET_DEBUG_FAT
    701 _printf("\n[FAT DEBUG] Enter in _fat_allocate() for file %d\n", fd_id );
     722_printf("\n[FAT DEBUG] _fat_allocate() for file %d\n", fd_id );
    702723#endif
    703724
     
    705726    do{
    706727        last_cluster_file = next_cluster;
    707         next_cluster      = get_next_cluster_id( IOC_KERNEL_MODE, next_cluster );
     728        next_cluster      = _get_next_cluster( IOC_KERNEL_MODE, next_cluster );
    708729    }while ( next_cluster < END_OF_CHAIN_CLUSTER );
    709730
     
    719740        // update, in the FAT, the value of last cluster allocated by the index
    720741        // of free cluster.
    721         if ( update_fat( last_cluster_file, free_cluster ) )
     742        if ( _update_fat( last_cluster_file, free_cluster ) )
    722743        {
    723744            _printf("\n[FAT ERROR] in _fat_allocate() : update fat failed\n");
     
    734755            // update, in the FAT, the value of the last cluster allocated by
    735756            // END_OF_CHAIN_CLUSTER
    736             if ( update_fat( last_cluster_file, END_OF_CHAIN_CLUSTER ) )
     757            if ( _update_fat( last_cluster_file, END_OF_CHAIN_CLUSTER ) )
    737758            {
    738759                _printf("\n[FAT ERROR] in _fat_allocate() : update fat failed\n");
     
    744765
    745766        // Check if free_cluster is really free (must be true)
    746         if ( get_next_cluster_id( IOC_KERNEL_MODE, free_cluster ) != FREE_CLUSTER)
     767        if ( _get_next_cluster( IOC_KERNEL_MODE, free_cluster ) != FREE_CLUSTER)
    747768        {
    748769            _printf("\n[FAT ERROR] in _fat_allocate() : free_cluster not free\n");
     
    756777    fat.number_free_cluster    = fat.number_free_cluster - count;
    757778
    758     if ( update_fs_info() )
     779    if ( _update_fs_info() )
    759780    {
    760781        _printf("\n[FAT ERROR] in _fat_allocate() : update fs_info failed\n");
     
    763784
    764785    return 0;
    765 }
    766 
    767 ///////////////////////////////////////////////////////////////////////////////
    768 // This function returns the cluster index from a (32 bytes) directory entry
    769 ///////////////////////////////////////////////////////////////////////////////
    770 static inline unsigned int read_cluster( char* buf )                 
    771 {
    772    unsigned int cluster = read_entry( DIR_FST_CLUS_HI, buf, 1 ) << 16;
    773    cluster = cluster | read_entry( DIR_FST_CLUS_LO, buf, 1 );
    774    return cluster;
    775 }
    776 
     786}  // end _fat_allocate()
    777787
    778788////////////////////////////////////////////////////////////////////////////////////////
    779789// This function read the blocks defined by the cluster index argument, in a data
    780 // region containing a directory to search the name of a file/firectory,
    781 // and returns the cluster index of the file/directory when the name has been found.
    782 // Return cluster index if name found / Return -1 if not found,
     790// region containing a directory to search the name of a file/firectory.
     791// It returns the cluster index of the file/directory when the name has been found,
     792// as well as the file size, and the lba.
     793// We consider 3 types of directory entries:
     794// - SFN : directory entry associated to a Short File Name (8.3)
     795// - LFN : directory entry associated to a Long File Name
     796// - XTN : directory entry containing only a name extension for a Long File Name
     797// The cluster index is always stored in a SFN or LFN entry.
     798// Return cluster index if name found / Return -1 if not found.
    783799////////////////////////////////////////////////////////////////////////////////////////
    784 static int scan_directory( unsigned int   mode,            // mode for IOC driver
    785                            unsigned int   cluster,         // cluster containing dir_entry
    786                            char*          file_name,       // searched file/directory name
    787                            unsigned int*  file_size,       // file size
    788                            unsigned int*  lba_dir_entry )  // lba of dir_entry
    789 {
    790 
    791 #if GIET_DEBUG_FAT
    792 _printf("\n[FAT DEBUG] enters _scan_directory() for dir/file %s\n", file_name );
    793 #endif
    794 
     800static int _scan_directory( unsigned int   mode,            // mode for IOC driver
     801                            unsigned int   cluster,         // cluster containing dir_entry
     802                            char*          file_name,       // searched file/directory name
     803                            unsigned int*  file_size,       // file size
     804                            unsigned int*  lba_dir_entry )  // lba of dir_entry
     805{
    795806    char dir_entry[32];   // buffer to store a full directory_entry
    796807    char name_entry[14];  // buffer to store a 13 characters (partial) name
    797808
    798     char sfn[12]            = {[0 ... 10] = ' ', '\0'};   // buffer for a Short File Name
    799     unsigned int  is_sfn    = is_short(file_name, sfn);   // if file_name is short, sfn = 1     
    800     unsigned int  offset    = 0;                          // byte offset in block
    801     unsigned int  block_id  = fat.sectors_per_cluster;   // sector index initialisation       
    802     unsigned int  lba       = cluster_to_lba(cluster);   
    803     unsigned int  attr      = 0;                          // directory entry attribute
    804     unsigned int  ord       = 0;                          // directory entry sequence
    805     unsigned int  found     = 0;                          // name found
     809    char sfn_string[12]    = {[0 ... 10] = ' ', '\0'};        // buffer for Short File Name
     810    unsigned int  is_sfn   = is_short(file_name, sfn_string); // if file_name is short
     811    unsigned int  offset   = 0;                               // byte offset in block
     812    unsigned int  block_id = fat.sectors_per_cluster;         // sector index initialisation
     813    unsigned int  lba      = cluster_to_lba(cluster);         // lba of cluster containing dir
     814    unsigned int  attr     = 0;                               // directory entry attribute
     815    unsigned int  ord      = 0;                               // directory entry sequence
     816    unsigned int  found    = 0;                               // searched name found
     817    unsigned int  long_name_found = 0;                        // a matching XTN has been found
     818    unsigned int  searched_cluster;                           // searched cluster index
     819
     820#if GIET_DEBUG_FAT
     821unsigned int procid  = _get_procid();
     822unsigned int cid     = procid / NB_PROCS_MAX;
     823unsigned int lpid    = procid % NB_PROCS_MAX;
     824unsigned int x       = cid >> Y_WIDTH;
     825unsigned int y       = cid & ((1<<Y_WIDTH) - 1);
     826_printf("\n[FAT DEBUG] _scan_directory() : P[%d,%d,%d] enters for %s / is_sfn = %d\n",
     827        x, y, lpid, file_name, is_sfn );
     828#endif
     829
    806830    unsigned int  i;
    807 
    808831    for( i = 0 ; i < 32 ; i++ ) dir_entry[i]  = 0;
    809832    for( i = 0 ; i < 14 ; i++ ) name_entry[i] = 0;
    810833
    811     // load first cluster sector from DATA region into FAT cache
     834    // load first sector from DATA region into FAT cache
    812835    // other sectors will be loaded inside loop as required
    813836    if( _ioc_read( 0,               // channel
     
    817840                   1 ) )            // one sector
    818841    {
    819         _printf("[FAT ERROR] in scan directory() cannot read sector %x\n", lba );
     842        _printf("[FAT ERROR] in _scan_directory() : cannot read sector %x\n", lba );
    820843        return -1;
    821844    }
     
    827850#endif
    828851
    829     // in this loop we scan all names in directory identified by cluster:
     852    // in this loop we scan all names in the directory identified by cluster index
    830853    // - the offset increment is an integer number of directory entry (32 bytes)
    831854    // - the exit condition is success (name found) or failure (end of directory)
    832     while( 1 )
     855    while( found == 0 )
    833856    {
    834857        // load a new sector if required
     
    842865            else                                          // get next cluster
    843866            {
    844                 cluster = get_next_cluster_id( mode, cluster );
     867                cluster = _get_next_cluster( mode, cluster );
    845868
    846869                if ( cluster >= END_OF_CHAIN_CLUSTER  ) return END_OF_CHAIN_CLUSTER;
     
    855878                           1 ) )            // one sector
    856879            {
    857                 _printf("[FAT ERROR] in scan directory() cannot read sector %x\n", lba );
     880                _printf("[FAT ERROR] in _scan_directory() : cannot read sector %x\n", lba );
    858881                return -1;
    859882            }
     
    863886        }
    864887
    865         // analyse a directory entry (pointed by fat.fat_cache + offset)
    866         if ( !found )
    867         {
    868             attr = read_entry( DIR_ATTR, fat.fat_cache + offset, 0);   
    869             ord  = read_entry( LDIR_ORD, fat.fat_cache + offset, 0);
    870 
    871             if ( is_sfn == 1 )                            // searched name is short
    872             {
    873                 if      ( (ord != FREE_ENTRY ) &&
    874                           (ord != NO_MORE_ENTRY) &&
    875                           (attr == ATTR_LONG_NAME_MASK) )    // LFN entry : skipped
     888        // store the directory entry pointed by offset in dir_entry buffer,
     889        // if it a possible candidate for the searched name
     890
     891        attr = _read_entry( DIR_ATTR, fat.fat_cache + offset, 0);   
     892        ord  = _read_entry( LDIR_ORD, fat.fat_cache + offset, 0);
     893
     894        if ( is_sfn == 1 )                                // searched name is short
     895        {
     896            if      ( (ord != FREE_ENTRY ) &&
     897                      (ord != NO_MORE_ENTRY) &&
     898                      (attr == ATTR_LONG_NAME_MASK) )    // EXT entry : skipped
     899            {
     900                offset     = offset + ((ord & 0xF) * DIR_ENTRY_SIZE);
     901            }
     902            else if ( (attr != ATTR_LONG_NAME_MASK) &&
     903                      (ord  != FREE_ENTRY) &&
     904                      (ord  != NO_MORE_ENTRY ) )         // SFN entry : to be checked
     905            {
     906                memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );   
     907
     908                get_name_from_short( dir_entry, name_entry );
     909
     910                if ( _strncmp( (char*)sfn_string,
     911                               (char*)name_entry, 13 ) == 0 )  // short name found
    876912                {
    877                                         offset     = offset + ((ord & 0xF) * DIR_ENTRY_SIZE);
    878                     continue;
     913                    found = 1;
    879914                }
    880                 else if ( (attr != ATTR_LONG_NAME_MASK) &&
    881                           (ord  != FREE_ENTRY) &&
    882                           (ord  != NO_MORE_ENTRY ) )         // SFN entry : checked
     915                else
    883916                {
    884                     memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );   
    885917                    offset = offset + DIR_ENTRY_SIZE;
    886918                }
    887                 else if (ord == NO_MORE_ENTRY )              // end of directory : return
     919            }
     920            else if (ord == NO_MORE_ENTRY )              // end of directory : return
     921            {
     922                return END_OF_CHAIN_CLUSTER;
     923            }
     924        }
     925        else                                      // searched name is long
     926        {
     927            if( (attr == ATTR_LONG_NAME_MASK) &&
     928                (ord != FREE_ENTRY) &&
     929                (ord != NO_MORE_ENTRY) )                 // EXT entry : to be checked
     930            {
     931                memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );
     932
     933                get_name_from_long( dir_entry, name_entry );
     934
     935                unsigned shift = ((ord & 0xf) - 1) * 13;
     936                if ( _strncmp( (char*)(file_name + shift),
     937                               (char*)name_entry, 13 ) == 0 )  // matching EXT
    888938                {
    889                     return END_OF_CHAIN_CLUSTER;
     939                    if( (ord & 0xf) == 1 )                    // long name found
     940                    {
     941                        long_name_found = 1;
     942                    }
    890943                }
    891                 else                                                                         // free entry : skipped
     944                offset = offset + DIR_ENTRY_SIZE;
     945            }
     946            else if( (attr != ATTR_LONG_NAME_MASK) &&
     947                     (ord  != FREE_ENTRY) &&
     948                     (ord  != NO_MORE_ENTRY) )
     949            {
     950                if ( long_name_found )                   // LFN entry
     951                {
     952                    memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );
     953                    found = 1;
     954                }
     955                else                                     // SFN entry: must be skipped
    892956                {
    893957                    offset = offset + DIR_ENTRY_SIZE;
    894                     continue;
    895958                }
    896959            }
    897             else                                      // searched name is long
    898             {
    899                 if( (attr == ATTR_LONG_NAME_MASK) &&
    900                     (ord != FREE_ENTRY) &&
    901                     (ord != NO_MORE_ENTRY) )                 // LFN entry : checked
    902                 {
    903                     memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );   
    904                     offset = offset + DIR_ENTRY_SIZE;
    905                 }
    906                 else if( (attr != ATTR_LONG_NAME_MASK) &&
    907                          (ord  != FREE_ENTRY) &&
    908                          (ord  != NO_MORE_ENTRY))                        // SFN entry : skipped
    909                 {
    910                     offset = offset + DIR_ENTRY_SIZE;
    911                     continue;
    912                 }
    913                 else if (ord == NO_MORE_ENTRY )                          // end of director : return
    914                 {
    915                     return END_OF_CHAIN_CLUSTER;
    916                 }
    917                 else                                           // free entry : skipped
    918                 {
    919                     offset = offset + DIR_ENTRY_SIZE;
    920                     continue;
    921                 }
    922             }
    923 
    924             // testing the name extracted from dir entry
    925             if ( is_sfn == 1 )                            // searched name is short
    926             {
    927                 get_name_from_short( dir_entry, name_entry );
    928 
    929                 if ( _strncmp( (char*)sfn, (char*)name_entry, 13 ) == 0 )
    930                 {
    931                     *file_size = read_entry( DIR_FILE_SIZE , dir_entry, 1 );
    932                     *lba_dir_entry = lba;
    933                     return read_cluster( dir_entry );
    934                 }
    935             }
    936             else                                         // searched name is long
    937             {
    938                 get_name_from_long( dir_entry, name_entry );
    939 
    940                 unsigned shift = ((ord & 0xf) - 1) * 13;
    941                 if ( _strncmp( (char*)(file_name + shift), (char*)name_entry, 13 ) == 0 )
    942                 {
    943                     if( (ord & 0xf) == 1 )  found = 1;
    944                     continue;
    945                 }
    946                 else                                                                 // no matching : skip
    947                 {
    948                     offset = offset + ((ord & 0xf) * DIR_ENTRY_SIZE);
    949                 }
    950             }
    951         }
    952         else    // file found
    953         {
    954             memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );   
    955             offset     = offset + DIR_ENTRY_SIZE;
    956             *file_size = read_entry( DIR_FILE_SIZE, dir_entry, 1 );
    957             *lba_dir_entry = lba;
    958 
    959             return read_cluster( dir_entry );
    960         }
    961     }
    962     return -1;
    963 } // end scan_directory()
     960            else if (ord == NO_MORE_ENTRY )                              // end of directory : return
     961            {
     962                return END_OF_CHAIN_CLUSTER;
     963            }
     964        }
     965    }  // end while
     966
     967    // returns cluster index
     968    *file_size       = _read_entry( DIR_FILE_SIZE, dir_entry, 1 );
     969    *lba_dir_entry   = lba;
     970    searched_cluster = (_read_entry( DIR_FST_CLUS_HI, dir_entry, 1 ) << 16) |
     971                       (_read_entry( DIR_FST_CLUS_LO, dir_entry, 1 )      ) ;
     972
     973#if GIET_DEBUG_FAT
     974_printf("\n[FAT DEBUG] _scan_directory() : P[%d,%d,%d] found %s / cluster = %x\n",
     975        x, y, lpid, file_name, searched_cluster );
     976#endif
     977
     978    return searched_cluster;
     979} // end _scan_directory()
    964980
    965981
     
    970986// Returns cluster index if success, Returns -1 if error.
    971987//////////////////////////////////////////////////////////////////////
    972 static int fat_create( char*           name,
    973                        unsigned int    is_file,
    974                        unsigned int    dir_cluster )
     988static int _fat_create( char*           name,
     989                        unsigned int    is_file,
     990                        unsigned int    dir_cluster )
    975991{
    976992    _printf("\n[FAT ERROR] _fat_create() not implemented\n");
     
    9991015unsigned int y       = cid & ((1<<Y_WIDTH) - 1);
    10001016
    1001 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] enters _fat_init()\n",
    1002         x, y, lpid );
     1017_printf("\n[FAT DEBUG] _fat_init() : P[%d,%d,%d] enters", x, y, lpid );
     1018if ( mode == IOC_BOOT_MODE   ) _printf(" / IOC_BOOT_MODE\n");
     1019if ( mode == IOC_KERNEL_MODE ) _printf(" / IOC_KERNEL_MODE\n");
     1020if ( mode == IOC_USER_MODE   ) _printf(" / IOC_USER_MODE\n");
    10031021#endif
    10041022   
     
    10161034
    10171035#if GIET_DEBUG_FAT
    1018 _printf("\n[FAT DEBUG] Boot Sector Loaded\n");
     1036_printf("\n[FAT DEBUG] _fat_init() : Boot Sector Loaded\n");
    10191037#endif
    10201038
    10211039    // checking various FAT32 assuptions from boot sector
    1022     if( read_entry( BPB_BYTSPERSEC, fat.fat_cache, 1 ) != 512 )
     1040    if( _read_entry( BPB_BYTSPERSEC, fat.fat_cache, 1 ) != 512 )
    10231041    {
    10241042        _printf("\n[FAT ERROR] The sector size must be 512 bytes\n");
    10251043        return -1; 
    10261044    }
    1027     if( read_entry( BPB_NUMFATS, fat.fat_cache, 1 ) != 1 )
     1045    if( _read_entry( BPB_NUMFATS, fat.fat_cache, 1 ) != 1 )
    10281046    {
    10291047        _printf("\n[FAT ERROR] The number of FAT copies in FAT region must be 1\n");
    10301048        return -1; 
    10311049    }
    1032     if( (read_entry( BPB_FAT32_FATSZ32, fat.fat_cache, 1 ) & 0xF) != 0 )
     1050    if( (_read_entry( BPB_FAT32_FATSZ32, fat.fat_cache, 1 ) & 0xF) != 0 )
    10331051    {
    10341052        _printf("\n[FAT ERROR] The FAT region in FAT32 must be multiple of 32 sectors\n");
    10351053        return -1; 
    10361054    }
    1037     if( read_entry( BPB_FAT32_ROOTCLUS, fat.fat_cache, 1 ) != 2 )
     1055    if( _read_entry( BPB_FAT32_ROOTCLUS, fat.fat_cache, 1 ) != 2 )
    10381056    {
    10391057        _printf("\n[FAT ERROR] The first cluster index must be 2\n");
     
    10411059    }
    10421060    // FS Info always in sector 1
    1043     fat.fs_info_lba         = read_entry( BPB_FAT32_FSINFO, fat.fat_cache, 1 );
     1061    fat.fs_info_lba         = _read_entry( BPB_FAT32_FSINFO, fat.fat_cache, 1 );
    10441062
    10451063    // initialise fat descriptor from VBR
    1046     fat.sectors_per_cluster = read_entry( BPB_SECPERCLUS, fat.fat_cache, 1 );
    1047     fat.sector_size         = read_entry( BPB_BYTSPERSEC, fat.fat_cache, 1 );
     1064    fat.sectors_per_cluster = _read_entry( BPB_SECPERCLUS, fat.fat_cache, 1 );
     1065    fat.sector_size         = _read_entry( BPB_BYTSPERSEC, fat.fat_cache, 1 );
    10481066    fat.cluster_size        = fat.sectors_per_cluster * 512;
    1049     fat.fat_sectors         = read_entry( BPB_FAT32_FATSZ32, fat.fat_cache, 1 );
    1050     fat.fat_lba             = read_entry( BPB_RSVDSECCNT, fat.fat_cache, 1 );
     1067    fat.fat_sectors         = _read_entry( BPB_FAT32_FATSZ32, fat.fat_cache, 1 );
     1068    fat.fat_lba             = _read_entry( BPB_RSVDSECCNT, fat.fat_cache, 1 );
    10511069    fat.data_lba            = fat.fat_lba + fat.fat_sectors;
    10521070    fat.fat_lock.value      = 0;
     
    10571075
    10581076#if GIET_DEBUG_FAT
    1059 _printf("\n[FAT DEBUG] FS_INFO Sector = %x\n", fat.fs_info_lba );
     1077_printf("\n[FAT DEBUG] _fat_init() : FS_INFO Sector = %x\n", fat.fs_info_lba );
    10601078#endif
    10611079
     
    10721090    fat.cache_lba = fat.fs_info_lba;
    10731091
    1074     fat.number_free_cluster    = read_entry( FS_FREE_CLUSTER     , fat.fat_cache, 1);
    1075     fat.last_cluster_allocated = read_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, 1);
    1076 
    1077 
    1078 #if GIET_DEBUG_FAT
    1079 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] initialises FAT descriptor\n", x, y, lpid );
     1092    fat.number_free_cluster    = _read_entry( FS_FREE_CLUSTER     , fat.fat_cache, 1);
     1093    fat.last_cluster_allocated = _read_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, 1);
     1094
     1095
     1096#if GIET_DEBUG_FAT
     1097_printf("\n[FAT DEBUG] _fat_init() : P[%d,%d,%d] initialises FAT descriptor\n", x, y, lpid );
    10801098_fat_print();
    1081 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] exit _fat_init()\n", x, y, lpid );
     1099_printf("\n[FAT DEBUG] _fat_init() : P[%d,%d,%d] exit\n", x, y, lpid );
    10821100#endif
    10831101
     
    11311149unsigned int x       = cid >> Y_WIDTH;
    11321150unsigned int y       = cid & ((1<<Y_WIDTH) - 1);
    1133 
    1134 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] enters _fat_open() for file %s\n",
     1151_printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] enters for path %s\n",
    11351152        x, y, lpid, pathname );
    11361153#endif
     
    11471164
    11481165#if GIET_DEBUG_FAT
    1149 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] takes the FAT lock\n",
     1166_printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] takes the FAT lock\n",
    11501167        x, y, lpid );
    11511168#endif
     
    11651182    // - The get_name_from_path() function extracts (successively)
    11661183    //   each directory name from the pathname, and store it in name[] buffer
    1167     // - The scan_directory() function scan one (or several) cluster(s) containing
     1184    // - The _scan_directory() function scan one (or several) cluster(s) containing
    11681185    //   a directory looking for name[], and return the cluster index
    11691186    //   corresponding to the directory/file found.
     
    11751192
    11761193#if GIET_DEBUG_FAT
    1177 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] search dir/file : %s\n",
     1194_printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] search dir/file : %s\n",
    11781195        x, y, lpid, name );
    11791196#endif
     
    11871204
    11881205        // scan current directory
    1189         cluster  = scan_directory( mode, cluster, name, &file_size, &lba );
     1206        cluster  = _scan_directory( mode, cluster, name, &file_size, &lba );
    11901207
    11911208        if( cluster == END_OF_CHAIN_CLUSTER && last_name && creat )
    11921209        {
    1193             cluster = fat_create( name, 1, dir_cluster );
     1210            cluster = _fat_create( name, 1, dir_cluster );
    11941211        }
    11951212        else if ( cluster == END_OF_CHAIN_CLUSTER )
     
    12041221    }
    12051222
     1223#if GIET_DEBUG_FAT
     1224_printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] found cluster index for file %s : %x\n",
     1225        x, y, lpid, pathname, cluster );
     1226#endif
     1227
    12061228    // check the next value for cluster index found
    1207     unsigned next = get_next_cluster_id( mode, cluster );
     1229    unsigned next = _get_next_cluster( mode, cluster );
    12081230
    12091231    if ( (next != BAD_CLUSTER) && (next != FREE_CLUSTER) )
     
    12261248
    12271249#if GIET_DEBUG_FAT
    1228 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] open file %s with fd = %d\n",
    1229         x, y, lpid, pathname, fd_id );
     1250_printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] exit : fd = %d for file %s\n",
     1251        x, y, lpid, fd_id, pathname );
    12301252#endif
    12311253
     
    13251347unsigned int x       = cid >> Y_WIDTH;
    13261348unsigned int y       = cid & ((1<<Y_WIDTH) - 1);
    1327 
    1328 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] enters _fat_read() for file %s\n"
     1349_printf("\n[FAT DEBUG] _fat_read() : P[%d,%d,%d] enters for file %s\n"
    13291350        " - buffer vbase    = %x\n"
    13301351        " - skipped sectors = %x\n"
     
    13401361    while ( clusters_to_skip )
    13411362    {
    1342         cluster = get_next_cluster_id( IOC_KERNEL_MODE, cluster );
     1363        cluster = _get_next_cluster( IOC_KERNEL_MODE, cluster );
    13431364        clusters_to_skip--;
    13441365    }
     
    13621383
    13631384#if GIET_DEBUG_FAT
    1364 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] makes an IOC read "
    1365         " for cluster %x : buf = %x / lba = %x / sectors = %d\n",
     1385_printf("\n[FAT DEBUG] _fat_read() : P[%d,%d,%d] makes an IOC read\n"
     1386        " - cluster = %x\n"
     1387        " - buf     = %x\n"
     1388        " - lba     = %x\n"
     1389        " - sectors = %d\n",
    13661390        x, y, lpid, cluster, (unsigned int)dst, lba, iter_sectors );
    13671391#endif
     
    13781402         
    13791403        // update variables for next iteration
    1380         cluster      = get_next_cluster_id( mode, cluster );
     1404        cluster      = _get_next_cluster( mode, cluster );
    13811405        todo_sectors = todo_sectors - iter_sectors;
    13821406        dst          = dst + (iter_sectors << 9);
     
    14431467unsigned int x       = cid >> Y_WIDTH;
    14441468unsigned int y       = cid & ((1<<Y_WIDTH) - 1);
    1445 
    1446 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] enters _fat_write() for file %s\n",
     1469_printf("\n[FAT DEBUG] _fat_write() : P[%d,%d,%d] enters for file %s\n",
    14471470        " - buffer vbase    = %x\n"
    14481471        " - skipped sectors = %x\n"
     
    14971520    while ( clusters_to_skip )
    14981521    {
    1499         cluster = get_next_cluster_id( IOC_KERNEL_MODE, cluster );
     1522        cluster = _get_next_cluster( IOC_KERNEL_MODE, cluster );
    15001523        clusters_to_skip--;
    15011524    }
     
    15191542
    15201543#if GIET_DEBUG_FAT
    1521 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] makes an IOC write : "
     1544_printf("\n[FAT DEBUG] _fat_write() : P[%d,%d,%d] makes an IOC write : "
    15221545        "buf = %x / lba = %x / sectors = %x\n",
    15231546        x, y, lpid, (unsigned int)src, lba, iter_sectors );
     
    15351558         
    15361559        // update variables for next iteration
    1537         cluster      = get_next_cluster_id( mode, cluster );
     1560        cluster      = _get_next_cluster( mode, cluster );
    15381561        todo_sectors = todo_sectors - iter_sectors;
    15391562        src          = src + (iter_sectors << 9);
     
    15601583    // returns number of sectors actually transfered
    15611584    return count;
    1562 }
     1585}  // end _fat_write()
    15631586
    15641587/////////////////////////////////////////////////////////////////////////////////
     
    16101633/////////////////////////////////////////////////////////////////////////////////////
    16111634// The following function implement the user_level system call.
    1612 // The flags argument is nor used, as file access modes are not implemented yet.
     1635// The flags argument is not used, as file access modes are not implemented yet.
    16131636/////////////////////////////////////////////////////////////////////////////////////
    16141637// Return the file descriptor index if success / return -1 if failure
Note: See TracChangeset for help on using the changeset viewer.