Index: soft/giet_vm/giet_fat32/fat32.c
===================================================================
--- soft/giet_vm/giet_fat32/fat32.c	(revision 443)
+++ soft/giet_vm/giet_fat32/fat32.c	(revision 458)
@@ -23,5 +23,5 @@
 #include <giet_config.h>
 #include <fat32.h>
-#include <tty_driver.h>
+#include <tty0.h>
 #include <ioc_driver.h>
 #include <utils.h>
@@ -725,5 +725,5 @@
     if ( _get_next_cluster( IOC_KERNEL_MODE, free_cluster ) != FREE_CLUSTER)
     {
-        _puts("\n[FAT ERROR] in _fat_allocate() : first free_cluster not free\n");
+        _puts("\n[FAT ERROR] in _fat_allocate() : first free cluster not free\n");
         return -1;
     }
@@ -755,7 +755,7 @@
 _putx( last_cluster_file );
 _puts(" / free cluster = ");
-_putx( free-cluster );
+_putx( free_cluster );
 _puts(" / clusters required = ");
-_putx( cluster_to allocate );
+_putx( cluster_to_allocate );
 _puts("\n");
 #endif
@@ -853,5 +853,5 @@
 _puts(",");
 _putd( p );
-_puts("] enters for ")
+_puts("] enters for ");
 _puts( file_name );
 _puts(" / is_sfn = ");
@@ -1018,5 +1018,5 @@
 _puts(" / cluster = ");
 _putx( searched_cluster );
-_puts("\n") 
+_puts("\n"); 
 #endif
 
@@ -1044,6 +1044,8 @@
 
 //////////////////////////////////////////////////////////////////////////
-// This function initializes the FAT structure, including the
-// files descriptors array, from informations found in the boot record.
+// This function initializes the FAT structure, including the open
+// files descriptors array and the lock protecting the FAT,
+// from informations found in the boot record.
+// This should be done only once.
 //////////////////////////////////////////////////////////////////////////
 // Return 0 if success, Return -1 if failure 
@@ -1065,10 +1067,17 @@
 _puts(",");
 _putd( p );
-_puts("] enters");
-if ( mode == IOC_BOOT_MODE   ) _puts(" / IOC_BOOT_MODE\n");
-if ( mode == IOC_KERNEL_MODE ) _puts(" / IOC_KERNEL_MODE\n");
-if ( mode == IOC_USER_MODE   ) _puts(" / IOC_USER_MODE\n");
-#endif
-    
+_puts("] enters _fat_init()");
+if ( mode == IOC_BOOT_MODE   ) _puts(" in BOOT_MODE\n");
+if ( mode == IOC_KERNEL_MODE ) _puts(" in KERNEL_MODE\n");
+if ( mode == IOC_USER_MODE   ) _puts(" in USER_MODE\n");
+#endif
+
+    // FAT initialisation should be done only once
+    if ( fat.initialised == FAT_INITIALISED )
+    {
+        _puts("\n[FAT WARNING] Strange initialisation  of a FAT already initialised...\n");
+        return 0;
+    }
+
     // load Boot Record (VBR) into fat cache
     if ( _ioc_read( 0,                   // channel
@@ -1118,6 +1127,8 @@
     fat.fat_lba             = _read_entry( BPB_RSVDSECCNT, fat.fat_cache, 1 );
     fat.data_lba            = fat.fat_lba + fat.fat_sectors;
-    fat.fat_lock.value      = 0;
     fat.initialised         = FAT_INITIALISED;
+
+    // initalise the lock protecting the FAT
+    _lock_init( &fat.fat_lock );
 
     // initialise file descriptor array
@@ -1148,5 +1159,5 @@
 #if GIET_DEBUG_FAT
 _fat_print();
-_puts("\n[FAT DEBUG] _fat_init() : P[");
+_puts("\n[FAT DEBUG] P[");
 _putd( x );
 _puts(",");
@@ -1154,5 +1165,5 @@
 _puts(",");
 _putd( p );
-_puts("] exit\n");
+_puts("] exit _fat_init()\n");
 #endif
 
@@ -1177,6 +1188,5 @@
 ///////////////////////////////////////////////////////////////////////////////
 // This function checks that the kernel FAT structure has been initialised.
-// It makes the FAT initialisation if it is the first open request. 
-// This function searches a file identified by the "pathname" argument.
+// It searches a file identified by the "pathname" argument.
 // It starts from root (cluster 2) to scan successively each subdirectory.
 // When the file is not found, but the path is found, and "creat" is set,
@@ -1206,4 +1216,32 @@
 unsigned int p       = procid & ((1<<P_WIDTH)-1);
 
+_puts("\n[FAT DEBUG] P[");
+_putd( x );
+_puts(",");
+_putd( y );
+_puts(",");
+_putd( p );
+_puts("] enters _fat_open() for path ");
+_puts( pathname );
+_puts("\n");
+#endif
+
+    // checking creat argument
+    if ( creat )
+    {
+        _puts("[FAT ERROR] in _fat_open() : create not supported yet\n");
+        return -1;
+    }
+
+    // checking FAT initialised
+    if( fat.initialised != FAT_INITIALISED )
+    {
+        _puts("[FAT ERROR] in _fat_open() : FAT not initialised\n");
+        return -1;
+    }
+    // takes the FAT lock for exclusive access
+    _lock_acquire( &fat.fat_lock );
+
+#if GIET_DEBUG_FAT
 _puts("\n[FAT DEBUG] _fat_open() : P[");
 _putd( x );
@@ -1212,39 +1250,6 @@
 _puts(",");
 _putd( p );
-_puts("] enters for path ");
-_puts( pathname );
-_puts("\n");
-#endif
-
-    // checking arguments
-    if ( creat )
-    {
-        _puts("[FAT ERROR] in _fat_open() : create not supported yet\n");
-        return -1;
-    }
-
-    // takes the FAT lock for exclusive access
-    _get_lock( &fat.fat_lock );
-
-#if GIET_DEBUG_FAT
-_puts("\n[FAT DEBUG] _fat_open() : P[");
-_putd( x );
-_puts(",");
-_putd( y );
-_puts(",");
-_putd( p );
 _puts("] takes the FAT lock\n");
 #endif
-
-    // FAT initialisation if required
-    if( fat.initialised != FAT_INITIALISED )
-    {
-        if ( _fat_init( mode ) )
-        {
-            _puts("[FAT ERROR] in _fat_open() : Cannot initialize FAT descriptor\n");
-            _release_lock( &fat.fat_lock );
-            return -1;
-        }
-    }
  
     // Scan the directories, starting from the root directory (cluster 2)
@@ -1291,5 +1296,5 @@
             _puts( name );
             _puts("\n");
-            _release_lock( &fat.fat_lock );
+            _lock_release( &fat.fat_lock );
             return -1;
         }
@@ -1346,5 +1351,5 @@
 
             // release FAT lock
-            _release_lock( &fat.fat_lock );
+            _lock_release( &fat.fat_lock );
 
             return fd_id;
@@ -1355,5 +1360,5 @@
             _puts( pathname );
             _puts(" : fd array full\n");
-            _release_lock( &fat.fat_lock );
+            _lock_release( &fat.fat_lock );
             return -1;
         }
@@ -1364,5 +1369,5 @@
         _puts( pathname );
         _puts(" : bad cluster\n");
-        _release_lock( &fat.fat_lock );
+        _lock_release( &fat.fat_lock );
         return -1;
     }
