Skip to content

Commit

Permalink
Add Volume begin by start sector for Teensy
Browse files Browse the repository at this point in the history
  • Loading branch information
greiman committed May 9, 2022
1 parent 890600d commit 61a28e6
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 46 deletions.
Binary file modified doc/html.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SdFat
version=2.1.4-beta.3
version=2.1.4-beta.4
license=MIT
author=Bill Greiman <[email protected]>
maintainer=Bill Greiman <[email protected]>
Expand Down
4 changes: 2 additions & 2 deletions src/ExFatLib/ExFatFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ class ExFatFile {
* \return true for success or false for failure.
*/
bool contiguousRange(uint32_t* bgnSector, uint32_t* endSector);
/** \return The current cluster number for a file or directory. */
uint32_t curCluster() const {return m_curCluster;}
/** \return The current position for a file or directory. */
uint64_t curPosition() const {return m_curPosition;}

/** \return Total data length for file. */
uint64_t dataLength() const {return m_dataLength;}
/** \return Directory entry index. */
Expand Down Expand Up @@ -805,7 +806,6 @@ class ExFatFile {
bool openPrivate(ExFatFile* dir, ExName_t* fname, oflag_t oflag);
bool parsePathName(const char* path,
ExName_t* fname, const char** ptr);
uint32_t curCluster() const {return m_curCluster;}
ExFatVolume* volume() const {return m_vol;}
bool syncDir();
//----------------------------------------------------------------------------
Expand Down
38 changes: 20 additions & 18 deletions src/ExFatLib/ExFatPartition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,37 +266,39 @@ int32_t ExFatPartition::freeClusterCount() {
}
}
//------------------------------------------------------------------------------
bool ExFatPartition::init(FsBlockDevice* dev, uint8_t part) {
uint32_t volStart = 0;
uint8_t* cache;
bool ExFatPartition::init(FsBlockDevice* dev, uint8_t part, uint32_t volStart) {
pbs_t* pbs;
BpbExFat_t* bpb;
MbrSector_t* mbr;
MbrPart_t* mp;

m_fatType = 0;
m_blockDev = dev;
cacheInit(m_blockDev);
cache = dataCachePrepare(0, FsCache::CACHE_FOR_READ);
if (part > 4 || !cache) {
DBG_FAIL_MACRO;
goto fail;
}
if (part >= 1) {
mbr = reinterpret_cast<MbrSector_t*>(cache);
mp = &mbr->part[part - 1];
if ((mp->boot != 0 && mp->boot != 0X80) || mp->type == 0) {
// if part == 0 assume super floppy with FAT boot sector in sector zero
// if part > 0 assume mbr volume with partition table
if (part) {
if (part > 4) {
DBG_FAIL_MACRO;
goto fail;
}
volStart = getLe32(mp->relativeSectors);
cache = dataCachePrepare(volStart, FsCache::CACHE_FOR_READ);
if (!cache) {
mbr = reinterpret_cast<MbrSector_t*>
(dataCachePrepare(0, FsCache::CACHE_FOR_READ));
if (!mbr) {
DBG_FAIL_MACRO;
goto fail;
}
MbrPart_t* mp = mbr->part + part - 1;
if (mp->type == 0 || (mp->boot != 0 && mp->boot != 0X80)) {
DBG_FAIL_MACRO;
goto fail;
}
volStart = getLe32(mp->relativeSectors);
}
pbs = reinterpret_cast<pbs_t*>
(dataCachePrepare(volStart, FsCache::CACHE_FOR_READ));
if (!pbs) {
DBG_FAIL_MACRO;
goto fail;
}
pbs = reinterpret_cast<pbs_t*>(cache);
if (strncmp(pbs->oemName, "EXFAT", 5)) {
DBG_FAIL_MACRO;
goto fail;
Expand Down
5 changes: 3 additions & 2 deletions src/ExFatLib/ExFatPartition.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ class ExFatPartition {
* \param[in] part The partition to be used. Legal values for \a part are
* 1-4 to use the corresponding partition on a device formatted with
* a MBR, Master Boot Record, or zero if the device is formatted as
* a super floppy with the FAT boot sector in sector zero.
* a super floppy with the FAT boot sector in sector volStart.
* \param[in] volStart location of volume if part is zero.
*
* \return true for success or false for failure.
*/
bool init(FsBlockDevice* dev, uint8_t part);
bool init(FsBlockDevice* dev, uint8_t part, uint32_t volStart = 0);
/**
* Check for device busy.
*
Expand Down
8 changes: 5 additions & 3 deletions src/ExFatLib/ExFatVolume.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ class ExFatVolume : public ExFatPartition {
* Initialize an FatVolume object.
* \param[in] dev Device block driver.
* \param[in] setCwv Set current working volume if true.
* \param[in] part partition to initialize.
* \param[in] part Partition to initialize.
* \param[in] volStart Start sector of volume if part is zero.
* \return true for success or false for failure.
*/
bool begin(FsBlockDevice* dev, bool setCwv = true, uint8_t part = 1) {
if (!init(dev, part)) {
bool begin(FsBlockDevice* dev, bool setCwv = true,
uint8_t part = 1, uint32_t volStart = 0) {
if (!init(dev, part, volStart)) {
return false;
}
if (!chdir()) {
Expand Down
25 changes: 15 additions & 10 deletions src/FatLib/FatPartition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,9 @@ int32_t FatPartition::freeClusterCount() {
return -1;
}
//------------------------------------------------------------------------------
bool FatPartition::init(FsBlockDevice* dev, uint8_t part) {
bool FatPartition::init(FsBlockDevice* dev, uint8_t part, uint32_t volStart) {
uint32_t clusterCount;
uint32_t totalSectors;
uint32_t volumeStartSector = 0;
m_blockDev = dev;
pbs_t* pbs;
BpbFat32_t* bpb;
Expand All @@ -414,19 +413,25 @@ bool FatPartition::init(FsBlockDevice* dev, uint8_t part) {
}
mbr = reinterpret_cast<MbrSector_t*>
(dataCachePrepare(0, FsCache::CACHE_FOR_READ));
if (!mbr) {
DBG_FAIL_MACRO;
goto fail;
}
MbrPart_t* mp = mbr->part + part - 1;

if (!mbr || mp->type == 0 || (mp->boot != 0 && mp->boot != 0X80)) {
if (mp->type == 0 || (mp->boot != 0 && mp->boot != 0X80)) {
DBG_FAIL_MACRO;
goto fail;
}
volumeStartSector = getLe32(mp->relativeSectors);
volStart = getLe32(mp->relativeSectors);
}
pbs = reinterpret_cast<pbs_t*>
(dataCachePrepare(volumeStartSector, FsCache::CACHE_FOR_READ));
(dataCachePrepare(volStart, FsCache::CACHE_FOR_READ));
if (!pbs) {
DBG_FAIL_MACRO;
goto fail;
}
bpb = reinterpret_cast<BpbFat32_t*>(pbs->bpb);
if (!pbs || bpb->fatCount != 2 ||
getLe16(bpb->bytesPerSector) != m_bytesPerSector) {
if (bpb->fatCount != 2 || getLe16(bpb->bytesPerSector) != m_bytesPerSector) {
DBG_FAIL_MACRO;
goto fail;
}
Expand All @@ -445,7 +450,7 @@ bool FatPartition::init(FsBlockDevice* dev, uint8_t part) {
if (m_sectorsPerFat == 0) {
m_sectorsPerFat = getLe32(bpb->sectorsPerFat32);
}
m_fatStartSector = volumeStartSector + getLe16(bpb->reservedSectorCount);
m_fatStartSector = volStart + getLe16(bpb->reservedSectorCount);

// count for FAT16 zero for FAT32
m_rootDirEntryCount = getLe16(bpb->rootDirEntryCount);
Expand All @@ -462,7 +467,7 @@ bool FatPartition::init(FsBlockDevice* dev, uint8_t part) {
totalSectors = getLe32(bpb->totalSectors32);
}
// total data sectors
clusterCount = totalSectors - (m_dataStartSector - volumeStartSector);
clusterCount = totalSectors - (m_dataStartSector - volStart);

// divide by cluster size to get cluster count
clusterCount >>= m_sectorsPerClusterShift;
Expand Down
5 changes: 3 additions & 2 deletions src/FatLib/FatPartition.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ class FatPartition {
* \param[in] part The partition to be used. Legal values for \a part are
* 1-4 to use the corresponding partition on a device formatted with
* a MBR, Master Boot Record, or zero if the device is formatted as
* a super floppy with the FAT boot sector in sector zero.
* a super floppy with the FAT boot sector in sector volStart.
* \param[in] volStart location of volume if part is zero.
*
* \return true for success or false for failure.
*/
bool init(FsBlockDevice* dev, uint8_t part = 1);
bool init(FsBlockDevice* dev, uint8_t part = 1, uint32_t volStart = 0);
/** \return The number of entries in the root directory for FAT16 volumes. */
uint16_t rootDirEntryCount() const {
return m_rootDirEntryCount;
Expand Down
6 changes: 4 additions & 2 deletions src/FatLib/FatVolume.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ class FatVolume : public FatPartition {
* \param[in] dev Device block driver.
* \param[in] setCwv Set current working volume if true.
* \param[in] part partition to initialize.
* \param[in] volStart Start sector of volume if part is zero.
* \return true for success or false for failure.
*/
bool begin(FsBlockDevice* dev, bool setCwv = true, uint8_t part = 1) {
if (!init(dev, part)) {
bool begin(FsBlockDevice* dev, bool setCwv = true,
uint8_t part = 1, uint32_t volStart = 0) {
if (!init(dev, part, volStart)) {
return false;
}
if (!chdir()) {
Expand Down
5 changes: 5 additions & 0 deletions src/FsLib/FsFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ class FsBaseFile {
return m_fFile ? m_fFile->contiguousRange(bgnSector, endSector) :
m_xFile ? m_xFile->contiguousRange(bgnSector, endSector) : false;
}
/** \return The current cluster number for a file or directory. */
uint32_t curCluster() const {
return m_fFile ? m_fFile->curCluster() :
m_xFile ? m_xFile->curCluster() : 0;
}
/** \return The current position for a file or directory. */
uint64_t curPosition() const {
return m_fFile ? m_fFile->curPosition() :
Expand Down
7 changes: 4 additions & 3 deletions src/FsLib/FsVolume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@
#include "FsLib.h"
FsVolume* FsVolume::m_cwv = nullptr;
//------------------------------------------------------------------------------
bool FsVolume::begin(FsBlockDevice* blockDev, bool setCwv, uint8_t part) {
bool FsVolume::begin(FsBlockDevice* blockDev, bool setCwv,
uint8_t part, uint32_t volStart) {
m_blockDev = blockDev;
m_fVol = nullptr;
m_xVol = new (m_volMem) ExFatVolume;
if (m_xVol && m_xVol->begin(m_blockDev, false, part)) {
if (m_xVol && m_xVol->begin(m_blockDev, false, part, volStart)) {
goto done;
}
m_xVol = nullptr;
m_fVol = new (m_volMem) FatVolume;
if (m_fVol && m_fVol->begin(m_blockDev, false, part)) {
if (m_fVol && m_fVol->begin(m_blockDev, false, part, volStart)) {
goto done;
}
m_fVol = nullptr;
Expand Down
4 changes: 3 additions & 1 deletion src/FsLib/FsVolume.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ class FsVolume {
* \param[in] blockDev Device block driver.
* \param[in] setCwv Set current working volume if true.
* \param[in] part partition to initialize.
* \param[in] volStart Start sector of volume if part is zero.
* \return true for success or false for failure.
*/
bool begin(FsBlockDevice* blockDev, bool setCwv = true, uint8_t part = 1);
bool begin(FsBlockDevice* blockDev, bool setCwv = true, uint8_t
part = 1, uint32_t volStart = 0);
#ifndef DOXYGEN_SHOULD_SKIP_THIS
uint32_t __attribute__((error("use sectorsPerCluster()"))) blocksPerCluster();
#endif // DOXYGEN_SHOULD_SKIP_THIS
Expand Down
1 change: 0 additions & 1 deletion src/SdCard/SdSpiCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ bool SharedSpiCard::begin(SdSpiConfig spiConfig) {
goto fail;
}
#endif // USE_SD_CRC

// check SD version
if (!(cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) {
type(SD_CARD_TYPE_SD2);
Expand Down
2 changes: 1 addition & 1 deletion src/SdFat.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/** SdFat version for cpp use. */
#define SD_FAT_VERSION 20104
/** SdFat version as string. */
#define SD_FAT_VERSION_STR "2.1.4-beta.3"
#define SD_FAT_VERSION_STR "2.1.4-beta.4"
//==============================================================================
/**
* \class SdBase
Expand Down

0 comments on commit 61a28e6

Please sign in to comment.