summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--firmware/drivers/fat.c23
-rw-r--r--firmware/export/fat.h1
2 files changed, 20 insertions, 4 deletions
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c
index 8f4e3e0632..ac6d240d8e 100644
--- a/firmware/drivers/fat.c
+++ b/firmware/drivers/fat.c
@@ -1218,6 +1218,7 @@ int fat_open(unsigned int startcluster,
1218 file->firstcluster = startcluster; 1218 file->firstcluster = startcluster;
1219 file->lastcluster = startcluster; 1219 file->lastcluster = startcluster;
1220 file->lastsector = 0; 1220 file->lastsector = 0;
1221 file->clusternum = 0;
1221 file->sectornum = 0; 1222 file->sectornum = 0;
1222 file->eof = false; 1223 file->eof = false;
1223 1224
@@ -1243,6 +1244,7 @@ int fat_create_file(char* name,
1243 file->firstcluster = 0; 1244 file->firstcluster = 0;
1244 file->lastcluster = 0; 1245 file->lastcluster = 0;
1245 file->lastsector = 0; 1246 file->lastsector = 0;
1247 file->clusternum = 0;
1246 file->sectornum = 0; 1248 file->sectornum = 0;
1247 file->eof = false; 1249 file->eof = false;
1248 } 1250 }
@@ -1508,6 +1510,7 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
1508{ 1510{
1509 int cluster = file->lastcluster; 1511 int cluster = file->lastcluster;
1510 int sector = file->lastsector; 1512 int sector = file->lastsector;
1513 int clusternum = file->clusternum;
1511 int numsec = file->sectornum; 1514 int numsec = file->sectornum;
1512 bool eof = file->eof; 1515 bool eof = file->eof;
1513 int first=0, last=0; 1516 int first=0, last=0;
@@ -1533,17 +1536,21 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
1533 cluster = get_next_cluster(cluster); 1536 cluster = get_next_cluster(cluster);
1534 sector = cluster2sec(cluster); 1537 sector = cluster2sec(cluster);
1535 } 1538 }
1539
1540 clusternum++;
1541 numsec=1;
1542
1536 if (!cluster) { 1543 if (!cluster) {
1537 eof = true; 1544 eof = true;
1538 if ( write ) { 1545 if ( write ) {
1539 /* remember last cluster, in case 1546 /* remember last cluster, in case
1540 we want to append to the file */ 1547 we want to append to the file */
1541 cluster = oldcluster; 1548 cluster = oldcluster;
1549 clusternum--;
1542 } 1550 }
1543 } 1551 }
1544 else 1552 else
1545 eof = false; 1553 eof = false;
1546 numsec=1;
1547 } 1554 }
1548 else { 1555 else {
1549 if (sector) 1556 if (sector)
@@ -1583,6 +1590,7 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
1583 1590
1584 file->lastcluster = cluster; 1591 file->lastcluster = cluster;
1585 file->lastsector = sector; 1592 file->lastsector = sector;
1593 file->clusternum = clusternum;
1586 file->sectornum = numsec; 1594 file->sectornum = numsec;
1587 file->eof = eof; 1595 file->eof = eof;
1588 1596
@@ -1595,7 +1603,7 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
1595 1603
1596int fat_seek(struct fat_file *file, unsigned int seeksector ) 1604int fat_seek(struct fat_file *file, unsigned int seeksector )
1597{ 1605{
1598 int clusternum=0, sectornum=0, sector=0; 1606 int clusternum=0, numclusters=0, sectornum=0, sector=0;
1599 int cluster = file->firstcluster; 1607 int cluster = file->firstcluster;
1600 int i; 1608 int i;
1601 1609
@@ -1604,10 +1612,16 @@ int fat_seek(struct fat_file *file, unsigned int seeksector )
1604 /* we need to find the sector BEFORE the requested, since 1612 /* we need to find the sector BEFORE the requested, since
1605 the file struct stores the last accessed sector */ 1613 the file struct stores the last accessed sector */
1606 seeksector--; 1614 seeksector--;
1607 clusternum = seeksector / fat_bpb.bpb_secperclus; 1615 numclusters = clusternum = seeksector / fat_bpb.bpb_secperclus;
1608 sectornum = seeksector % fat_bpb.bpb_secperclus; 1616 sectornum = seeksector % fat_bpb.bpb_secperclus;
1609 1617
1610 for (i=0; i<clusternum; i++) { 1618 if (file->clusternum && clusternum >= file->clusternum)
1619 {
1620 cluster = file->lastcluster;
1621 numclusters -= file->clusternum;
1622 }
1623
1624 for (i=0; i<numclusters; i++) {
1611 cluster = get_next_cluster(cluster); 1625 cluster = get_next_cluster(cluster);
1612 if (!cluster) { 1626 if (!cluster) {
1613 DEBUGF("Seeking beyond the end of the file! " 1627 DEBUGF("Seeking beyond the end of the file! "
@@ -1627,6 +1641,7 @@ int fat_seek(struct fat_file *file, unsigned int seeksector )
1627 1641
1628 file->lastcluster = cluster; 1642 file->lastcluster = cluster;
1629 file->lastsector = sector; 1643 file->lastsector = sector;
1644 file->clusternum = clusternum;
1630 file->sectornum = sectornum + 1; 1645 file->sectornum = sectornum + 1;
1631 return 0; 1646 return 0;
1632} 1647}
diff --git a/firmware/export/fat.h b/firmware/export/fat.h
index ec71fceb05..921f04eb8d 100644
--- a/firmware/export/fat.h
+++ b/firmware/export/fat.h
@@ -51,6 +51,7 @@ struct fat_file
51 int firstcluster; /* first cluster in file */ 51 int firstcluster; /* first cluster in file */
52 int lastcluster; /* cluster of last access */ 52 int lastcluster; /* cluster of last access */
53 int lastsector; /* sector of last access */ 53 int lastsector; /* sector of last access */
54 int clusternum; /* current clusternum */
54 int sectornum; /* sector number in this cluster */ 55 int sectornum; /* sector number in this cluster */
55 unsigned int direntry; /* short dir entry index from start of dir */ 56 unsigned int direntry; /* short dir entry index from start of dir */
56 unsigned int direntries; /* number of dir entries used by this file */ 57 unsigned int direntries; /* number of dir entries used by this file */