Index: trunk/kernel/vfs/fatfs.c
===================================================================
--- trunk/kernel/vfs/fatfs.c	(revision 401)
+++ trunk/kernel/vfs/fatfs.c	(revision 406)
@@ -249,33 +249,21 @@
 error_t fatfs_get_cluster( mapper_t * mapper,
                            uint32_t   first_cluster_id,
-                           uint32_t   page_index,
+                           uint32_t   searched_page_index,
                            uint32_t * searched_cluster_id )
 {
     page_t   * current_page_desc;      // pointer on current page descriptor
     uint32_t * current_page_buffer;    // pointer on current page (array of uint32_t)
-    uint32_t   current_page_index;     // index of current page in mapper 
+    uint32_t   current_page_index;     // index of current page in FAT 
     uint32_t   current_page_offset;    // offset of slot in current page
     uint32_t   page_count_in_file;     // index of page in file (index in linked list)
-    uint32_t   current_cluster_id;     // content of current FAT slot
-
-    assert( (page_index > 0) , __FUNCTION__ , "no FAT access required for first page\n");
-
-    fatfs_dmsg("\n[INFO] %s : enters / mapper = %x / first_cluster_id = %d / page_index = %d\n",
-               __FUNCTION__ , first_cluster_id , page_index );
-
-#if (CONFIG_FATFS_DEBUG > 1)
-    xptr_t     base_xp = ppm_page2base( XPTR( local_cxy , mapper_get_page ( mapper , 0 ) ) );
-    uint32_t * buf     = (uint32_t *)GET_PTR( base_xp );
-    uint32_t   line , word;
-    printk("\n***  FAT mapper content for first 256 entries ***\n");
-    for( line = 0 ; line < 16 ; line++ )
-    {
-        printk("%X : ", line );
-        for( word = 0 ; word < 16 ; word++ ) printk("%X ", buf[(line<<4) + word] );
-        printk("\n");
-    }
-#endif
-
-    // compute number of FAT slots per page
+    uint32_t   next_cluster_id;        // content of current FAT slot
+
+    assert( (searched_page_index > 0) , __FUNCTION__ , 
+    "no FAT access required for first page\n");
+
+    fatfs_dmsg("\n[DMSG] %s : enter / first_cluster_id = %d / searched_page_index = %d\n",
+    __FUNCTION__ , first_cluster_id , searched_page_index );
+
+    // get number of FAT slots per page
     uint32_t slots_per_page = CONFIG_PPM_PAGE_SIZE >> 2;
 
@@ -284,12 +272,9 @@
     current_page_offset = first_cluster_id % slots_per_page;
     page_count_in_file  = 0;
+    next_cluster_id     = 0xFFFFFFFF;
 
     // scan FAT (i.e. traverse FAT linked list)
-    while( page_count_in_file <= page_index )
-    {
-
-        fatfs_dmsg("\n[INFO] %s : page_index = %d / page_offset = %d / count = %d\n",
-               __FUNCTION__ , current_page_index , current_page_offset , page_count_in_file );
-
+    while( page_count_in_file < searched_page_index )
+    {
         // get pointer on current page descriptor
         current_page_desc = mapper_get_page( mapper , current_page_index );
@@ -302,16 +287,21 @@
 
         // get FAT slot content 
-        current_cluster_id = current_page_buffer[current_page_offset];
+        next_cluster_id = current_page_buffer[current_page_offset];
+
+        fatfs_dmsg("\n[DMSG] %s : traverse FAT / current_page_index = %d\n"
+                   "       current_page_offset = %d / next_cluster_id = %d\n",
+        __FUNCTION__ , current_page_index , current_page_offset , next_cluster_id );
 
         // update loop variables
-        current_page_index  = current_cluster_id / slots_per_page;
-        current_page_offset = current_cluster_id % slots_per_page;
+        current_page_index  = next_cluster_id / slots_per_page;
+        current_page_offset = next_cluster_id % slots_per_page;
         page_count_in_file++;
     }
-    
-    fatfs_dmsg("\n[INFO] %s : exit / cluster_id = %d\n",
-               __FUNCTION__ , current_cluster_id );
-
-    *searched_cluster_id = current_cluster_id;
+
+    if( next_cluster_id == 0xFFFFFFFF ) return EIO;
+   
+    fatfs_dmsg("\n[DMSG] %s : exit / cluster_id = %d\n", __FUNCTION__ , next_cluster_id );
+
+    *searched_cluster_id = next_cluster_id;
     return 0;
 
@@ -343,5 +333,5 @@
     uint8_t     * buffer;
 
-    fatfs_dmsg("\n[INFO] %s : enter for fatfs_ctx = %x\n",
+    fatfs_dmsg("\n[DMSG] %s : enter for fatfs_ctx = %x\n",
                __FUNCTION__ , fatfs_ctx );
 
@@ -357,5 +347,5 @@
                    "cannot allocate memory for 512 bytes buffer\n" );
      
-    fatfs_dmsg("\n[INFO] %s : allocated 512 bytes buffer\n", __FUNCTION__ );
+    fatfs_dmsg("\n[DMSG] %s : allocated 512 bytes buffer\n", __FUNCTION__ );
 
     // load the boot record from device
@@ -363,13 +353,14 @@
     error = dev_ioc_sync_read( buffer , 0 , 1 );
 
-    fatfs_dmsg("\n[INFO] %s : buffer loaded\n", __FUNCTION__ );
-
-    assert( (error == 0) , __FUNCTION__ ,
-                   "cannot access boot record\n" );
-
-#if (CONFIG_FATFS_DEBUG > 1)
+    fatfs_dmsg("\n[DMSG] %s : buffer loaded\n", __FUNCTION__ );
+
+    assert( (error == 0) , __FUNCTION__ , "cannot access boot record\n" );
+
+#if (CONFIG_FATFS_DEBUG & 0x1)
+if( hal_time_stamp() > CONFIG_FATFS_DEBUG )
+{
     uint32_t   line;
     uint32_t   byte = 0;
-    printk("\n***** FAT boot record\n" );
+    printk("\n***** %s : FAT boot record\n", __FUNCTION__ );
     for ( line = 0 ; line < 32 ; line++ )
     {
@@ -383,4 +374,5 @@
          byte += 16;
     }
+}
 #endif
 
@@ -423,5 +415,5 @@
     kmem_free( &req );
 
-    fatfs_dmsg("\n[INFO] %s : boot record read & released\n",
+    fatfs_dmsg("\n[DMSG] %s : boot record read & released\n",
                __FUNCTION__ );
 
@@ -445,5 +437,5 @@
     fatfs_ctx->fat_mapper_xp         = XPTR( local_cxy , fat_mapper );
 
-    fatfs_dmsg("\n[INFO] %s : exit for fatfs_ctx = %x\n",
+    fatfs_dmsg("\n[DMSG] %s : exit for fatfs_ctx = %x\n",
                __FUNCTION__ , fatfs_ctx );
 
@@ -472,15 +464,15 @@
     fatfs_ctx_t * fatfs_ctx;   // pointer on local FATFS context
 
-    // get pointer on source mapper and page index from page descriptor
+    // get pointer on mapper and page index from page descriptor
     mapper = page->mapper;
     index  = page->index;
 
-    // get VFS inode pointer from mapper
+    // get inode pointer from mapper
     inode = mapper->inode;
 
-    fatfs_dmsg("\n[INFO] %s : core[%x,%d] enter for inode %x / page_id = %d / mapper = %x\n",
-    __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , inode , index , mapper );
-
-    // get page to move base address
+    fatfs_dmsg("\n[DMSG] %s : core[%x,%d] enter for page %d / inode %x / mapper %x\n",
+    __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , index , inode , mapper );
+
+    // get page base address
     xptr_t base_xp = ppm_page2base( XPTR( local_cxy , page ) );
     buffer = (uint8_t *)GET_PTR( base_xp );
@@ -496,5 +488,5 @@
         lba = fatfs_ctx->fat_begin_lba + (count * index);
  
-        fatfs_dmsg("\n[INFO] %s : core[%x,%d] access FAT on device / lba = %d\n",
+        fatfs_dmsg("\n[DMSG] %s : core[%x,%d] access FAT on device / lba = %d\n",
         __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , lba );
 
@@ -519,4 +511,7 @@
         else                        // FAT mapper access required
         {
+            fatfs_dmsg("\n[DMSG] %s : core[%x,%d] must access FAT\n",
+            __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid );
+
             // get cluster and local pointer on FAT mapper
             xptr_t     fat_mapper_xp  = fatfs_ctx->fat_mapper_xp;
@@ -545,9 +540,9 @@
         }
 
+        fatfs_dmsg("\n[DMSG] %s : core[%x,%d] access device for inode %x / cluster_id %d\n",
+        __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , inode , searched_cluster_id );
+
         // get lba from cluster_id
         lba = fatfs_lba_from_cluster( fatfs_ctx , searched_cluster_id );
-
-        fatfs_dmsg("\n[INFO] %s : core[%x,%d] access device for inode %x / cluster_id = %d\n",
-        __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , inode , first_cluster_id );
 
         // access device
@@ -558,6 +553,21 @@
     }
 
-    fatfs_dmsg("\n[INFO] %s : core[%x,%d] exit for inode %x / page_id = %d / mapper = %x\n",
-    __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , inode , index , mapper );
+    fatfs_dmsg("\n[DMSG] %s : core[%x,%d] exit for page %d / inode %x / mapper %x\n",
+    __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , index , inode , mapper );
+
+#if (CONFIG_FATFS_DEBUG & 0x1)
+if( hal_time_stamp() > CONFIG_FATFS_DEBUG )
+{
+    uint32_t * tab = (uint32_t *)buffer;
+    uint32_t line , word;
+    printk("\n***** %s : First 64 words of loaded page\n", __FUNCTION__ );
+    for( line = 0 ; line < 8 ; line++ )
+    {
+        printk("%X : ", line );
+        for( word = 0 ; word < 8 ; word++ ) printk("%X ", tab[(line<<3) + word] );
+        printk("\n");
+    }
+}
+#endif
 
     return 0;
@@ -574,5 +584,5 @@
     // - scan the directory entries in each 4 Kbytes page
 
-    fatfs_dmsg("\n[INFO] %s : enter for child <%s> in parent inode %l\n",
+    fatfs_dmsg("\n[DMSG] %s : enter for child <%s> in parent inode %l\n",
                __FUNCTION__ , name , XPTR( local_cxy , parent_inode ) );
 
@@ -612,8 +622,11 @@
         base = (uint8_t *)GET_PTR( base_xp );
 
-#if (CONFIG_FATFS_DEBUG > 1)
+#if (CONFIG_FATFS_DEBUG & 0x1)
+if( hal_time_stamp() > CONFIG_FATFS_DEBUG )
+{
     uint32_t * buf = (uint32_t *)base;
     uint32_t line , word;
-    printk("\n***** first 16 dir entries for parent inode %x\n", parent_inode );
+    printk("\n***** %s : First 16 dentries for parent inode %x\n",
+    __FUNCTION__ , parent_inode );
     for( line = 0 ; line < 16 ; line++ )
     {
@@ -622,7 +635,6 @@
         printk("\n");
     }
+}
 #endif
-
-
         // scan this page until end of directory, end of page, or name found
         while( (offset < 4096) && (found == 0) )
@@ -693,5 +705,5 @@
     if ( found == -1 )  // found end of directory => failure 
     {
-        fatfs_dmsg("\n[INFO] %s : exit / child <%s> not found in parent inode %l\n",
+        fatfs_dmsg("\n[DMSG] %s : exit / child <%s> not found in parent inode %l\n",
                    __FUNCTION__ , name , XPTR( local_cxy , parent_inode ) );
 
@@ -711,5 +723,5 @@
         hal_remote_sw( XPTR( child_cxy , &child_ptr->extend ) , cluster );
 
-        fatfs_dmsg("\n[INFO] %s : exit / child <%s> found in parent inode %l\n",
+        fatfs_dmsg("\n[DMSG] %s : exit / child <%s> found in parent inode %l\n",
                    __FUNCTION__ , name , XPTR( local_cxy , parent_inode ) );
 
