Changeset 259 for soft/giet_vm/giet_fat32
- Timestamp:
- Dec 5, 2013, 12:45:34 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/giet_fat32/fat32.c
r258 r259 1163 1163 unsigned int offset ) // nuber of sectors to skip in file 1164 1164 { 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 = "); 1195 allocate ? _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; 1169 1308 } 1170 1309
Note: See TracChangeset
for help on using the changeset viewer.