Ignore:
Timestamp:
Dec 5, 2013, 12:45:34 PM (11 years ago)
Author:
devigne
Message:

giet_fat32/fat32.c :
Added _fat_write function. At the moment it does not support the creation of a
new file (the writing must exist in the disk image) and does not support the
allocation of additional cluster from the original file.

create_dmg :
Bug fix for support Linux (The tree files were not consistent with the
pathnames in boot.c).

giet_libs/stdlib.c and stdlib.h :
Added stdlib file. At the moment they only contain the atoi function.

giet_libs/stdio.h :
Bug fix in SYSCALL DEFINE. SYSCALL_FAT_READ, WRITE, CLOSE, LSEEK had the same
value.

Makefile :
Added compilation line for stdlib.o

File:
1 edited

Legend:

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

    r258 r259  
    11631163                unsigned int offset )    // nuber of sectors to skip in file
    11641164{
    1165     _tty_get_lock( 0 );
    1166     _puts("\n[FAT ERROR] _fat_write() function not implemented\n");
    1167     _tty_release_lock( 0 );
    1168     return -1;
     1165
     1166    unsigned int spc = fat.sectors_per_cluster;
     1167
     1168    unsigned int file_size;         // number of bytes in file
     1169    unsigned int file_sectors;      // number of sectors in file
     1170    unsigned int cluster;           // cluster index
     1171    unsigned int clusters_to_skip;  // number of clusters to skip because offset
     1172    unsigned int sectors_to_skip;   // number of sectors to skip in first iteration
     1173    unsigned int allocate;          // need allocate or not
     1174
     1175    // compute file size as a number of sectors
     1176    file_size    = fat.fd[fd_id].file_size;
     1177    if ( file_size & 0x1FF ) file_sectors = (file_size >> 9) + 1;
     1178    else                     file_sectors = (file_size >> 9);
     1179   
     1180    allocate = ( ((count + offset) / spc) > (file_sectors / spc) );
     1181
     1182#if GIET_DEBUG_FAT
     1183_tty_get_lock( 0 );
     1184_puts("\n[FAT DEBUG] Enter _fat_write() for file ");
     1185_puts( fat.fd[fd_id].name );
     1186_puts("\n - buffer base     = ");
     1187_putx( (unsigned int)buffer );
     1188_puts("\n - skipped sectors = ");
     1189_putd( offset );
     1190_puts("\n - write sectors    = ");
     1191_putd( count );
     1192_puts("\n - file size (sectors)  = ");
     1193_putd( file_sectors );
     1194_puts("\n - need allocate = ");
     1195allocate ? _puts( "True" ) : _puts( "False");
     1196_tty_release_lock( 0 );
     1197#endif
     1198
     1199    if ( allocate  )
     1200    {
     1201        _tty_get_lock( 0 );
     1202        _puts("\n[FAT ERROR] in _fat_write() : \n");
     1203        _puts("we need to allocate more cluster... But this function is not implemented\n");
     1204        _tty_release_lock( 0 );
     1205        return -1;
     1206    }
     1207    // arguments checking
     1208    if ( fd_id >= GIET_OPEN_FILES_MAX )
     1209    {
     1210        _tty_get_lock( 0 );
     1211        _puts("\n[FAT ERROR] in _fat_write() : illegal file descriptor index\n");
     1212        _tty_release_lock( 0 );
     1213        return -1;
     1214    }
     1215    if ( fat.fd[fd_id].used != 1 )
     1216    {
     1217        _tty_get_lock( 0 );
     1218        _puts("\n[FAT ERROR] in _fat_write() : file not open\n");
     1219        _tty_release_lock( 0 );
     1220        return -1;
     1221    }
     1222    if ( ((unsigned int)buffer & 0x1FF) != 0 )
     1223    {
     1224        _tty_get_lock( 0 );
     1225        _puts("\n[FAT ERROR] in _fat_write() : memory buffer not sector aligned\n");
     1226        _tty_release_lock( 0 );
     1227        return -1;
     1228    }
     1229
     1230    // compute clusters and sectors to be skipped
     1231    clusters_to_skip = offset / spc;
     1232    sectors_to_skip  = offset % spc;
     1233   
     1234    // get first cluster index
     1235    cluster = fat.fd[fd_id].first_cluster;
     1236
     1237#if GIET_DEBUG_FAT
     1238_tty_get_lock( 0 );
     1239_puts("\n - first cluster   = ");
     1240_putd( cluster );   
     1241_puts("\n - skiped clusters = ");
     1242_putd( clusters_to_skip );   
     1243_puts("\n");
     1244_tty_release_lock( 0 );
     1245#endif
     1246
     1247    // compute index of first cluster to be loaded
     1248    // as we may need to scan the FAT, we use the kernel mode
     1249    while ( clusters_to_skip )
     1250    {
     1251        cluster = get_next_cluster_id( IOC_KERNEL_MODE, cluster );
     1252        clusters_to_skip--;
     1253    }
     1254
     1255    // variables used in the loop on clusters
     1256    int             todo_sectors;   // number of sectors still to be loaded
     1257    unsigned int    lba;            // first sector index on device
     1258    unsigned int    iter_sectors;   // number of sectors to load in iteration
     1259    char*           src;            // pointer on target buffer
     1260
     1261    // initialize these variables for the first iteration
     1262    todo_sectors  = count;
     1263    src           = (char*)buffer;
     1264    lba           = cluster_to_lba(cluster) + sectors_to_skip;
     1265    if( count < (spc - sectors_to_skip) ) iter_sectors = count;
     1266    else                                  iter_sectors = spc - sectors_to_skip;
     1267
     1268    // loop on the clusters
     1269    while ( todo_sectors > 0 )
     1270    {
     1271
     1272#if GIET_DEBUG_FAT
     1273_tty_get_lock( 0 );
     1274_puts("\n[FAT DEBUG] _fat_write() IOC request : buf = ");
     1275_putx( (unsigned int)src );
     1276_puts(" / lba = ");
     1277_putd( lba );
     1278_puts(" / sectors = ");
     1279_putd( iter_sectors );
     1280_puts("\n"); 
     1281_tty_release_lock( 0 );
     1282#endif
     1283
     1284        if( _ioc_write( mode,              // mode for IOC driver
     1285                        lba,               // first sector index
     1286                        src,               // buffer address
     1287                        iter_sectors ) )   // number of sectors
     1288        {
     1289            _tty_get_lock( 0 );
     1290            _puts("\n[FAT ERROR] in _fat_write() cannot write block ");
     1291            _putd( lba );
     1292            _puts("\n");
     1293            _tty_release_lock( 0 );
     1294            return -1;
     1295        }
     1296         
     1297        // update variables for next iteration
     1298        cluster      = get_next_cluster_id( mode, cluster );
     1299        todo_sectors = todo_sectors - iter_sectors;
     1300        src          = src + (iter_sectors << 9);
     1301        lba          = cluster_to_lba(cluster);
     1302        if ( todo_sectors > spc ) iter_sectors = spc;
     1303        else                      iter_sectors = todo_sectors;
     1304    }
     1305         
     1306    // returns number of sectors actually transfered
     1307    return count;
    11691308}
    11701309
Note: See TracChangeset for help on using the changeset viewer.