summaryrefslogtreecommitdiff
path: root/firmware/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/drivers')
-rw-r--r--firmware/drivers/fat.c52
1 files changed, 22 insertions, 30 deletions
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c
index a60a017d3e..89b322e10a 100644
--- a/firmware/drivers/fat.c
+++ b/firmware/drivers/fat.c
@@ -996,32 +996,21 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
996 if ( sector == -1 ) 996 if ( sector == -1 )
997 return 0; 997 return 0;
998 998
999 if ( write && !cluster) { 999 if (!write)
1000 /* new file */ 1000 first = last = sector;
1001 cluster = find_free_cluster(fat_bpb.fsinfo.nextfree);
1002 if (!cluster) {
1003 cluster = find_free_cluster(0);
1004 if (!cluster) {
1005 DEBUGF("fat_readwrite(): Disk full!\n");
1006 return -3;
1007 }
1008 }
1009 file->firstcluster = cluster;
1010 sector = cluster2sec(cluster);
1011 if (sector<0)
1012 return -1;
1013 }
1014 1001
1015 /* find sequential sectors and read/write them all at once */ 1002 /* find sequential sectors and read/write them all at once */
1016 for (i=0; i<sectorcount && sector>=0; i++ ) { 1003 for (i=0; i<sectorcount && sector>=0; i++ ) {
1017 if ( numsec >= fat_bpb.bpb_secperclus ) { 1004 numsec++;
1005 if ( numsec >= fat_bpb.bpb_secperclus || !cluster) {
1018 int oldcluster = cluster; 1006 int oldcluster = cluster;
1019 cluster = get_next_cluster(cluster); 1007 cluster = get_next_cluster(cluster);
1020 if (!cluster) { 1008 if (!cluster) {
1021 if ( write ) { 1009 if ( write ) {
1022 /* writing past end-of-file, 1010 if (!oldcluster) /* new file */
1023 find a new free cluster to use. */ 1011 cluster = find_free_cluster(fat_bpb.fsinfo.nextfree);
1024 cluster = find_free_cluster(oldcluster+1); 1012 else /* writing past end-of-file */
1013 cluster = find_free_cluster(oldcluster+1);
1025 if (!cluster) { 1014 if (!cluster) {
1026 /* no free cluster found after last, 1015 /* no free cluster found after last,
1027 search from beginning */ 1016 search from beginning */
@@ -1032,8 +1021,12 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
1032 DEBUGF("fat_readwrite(): Disk full!\n"); 1021 DEBUGF("fat_readwrite(): Disk full!\n");
1033 } 1022 }
1034 } 1023 }
1035 if ( cluster ) 1024 if ( cluster ) {
1036 update_fat_entry(oldcluster, cluster); 1025 if ( !oldcluster )
1026 file->firstcluster = cluster;
1027 else
1028 update_fat_entry(oldcluster, cluster);
1029 }
1037 } 1030 }
1038 else { 1031 else {
1039 /* reading past end-of-file */ 1032 /* reading past end-of-file */
@@ -1051,21 +1044,20 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
1051 } 1044 }
1052 else 1045 else
1053 sector++; 1046 sector++;
1054 numsec++;
1055
1056 if (!first)
1057 first = sector;
1058 if (!last)
1059 last = sector;
1060 1047
1048 if (write && !first)
1049 first = last = sector;
1050
1061 if ( (sector != last+1) || /* not sequential any more? */ 1051 if ( (sector != last+1) || /* not sequential any more? */
1062 (i == sectorcount-1) || /* last sector requested? */ 1052 (i == sectorcount-1) || /* last sector requested? */
1063 (last-first+1 == 256) ) { /* max 256 sectors per ata request */ 1053 (last-first+1 == 256) ) { /* max 256 sectors per ata request */
1064 int count = last-first+1; 1054 int count = last - first + 1;
1055 int start = first + fat_bpb.startsector;
1056 LDEBUGF("s=%x, l=%x, f=%x, i=%d\n",sector,last,first,i);
1065 if (write) 1057 if (write)
1066 err = ata_write_sectors(first + fat_bpb.startsector, count, buf); 1058 err = ata_write_sectors(start, count, buf);
1067 else 1059 else
1068 err = ata_read_sectors(first + fat_bpb.startsector, count, buf); 1060 err = ata_read_sectors(start, count, buf);
1069 if (err) { 1061 if (err) {
1070 DEBUGF( "fat_readwrite() - Couldn't read sector %d" 1062 DEBUGF( "fat_readwrite() - Couldn't read sector %d"
1071 " (error code %d)\n", sector,err); 1063 " (error code %d)\n", sector,err);