Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dynamic FAT variant detection #55 #56

Merged
merged 8 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ elseif((FREERTOS_PLUS_FAT_PORT STREQUAL "A_CUSTOM_PORT") AND (NOT TARGET freerto
" freertos_plus_fat)")
endif()

# This library requires access to a heap
# FreeRTOS/FreeRTOS-Kernel previously defaulted to heap4.c
if(NOT DEFINED FREERTOS_HEAP)
message(STATUS "FREERTOS_HEAP not set, setting FREERTOS_HEAP=4")
set(FREERTOS_HEAP 4)
endif()

# Select the appropriate Build Test configuration
# This is only used when freertos_config is not defined, otherwise the build test will be performed
Expand Down
32 changes: 14 additions & 18 deletions ff_ioman.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,18 +705,21 @@ static uint8_t ucAssumeFATType = 0;
*
* In practice however, this does not always seem to be a correct assumption.
*
* The first 12 or 16 bits in the FAT table may also help to determine the
* The end-of-chain value in the FAT table may also help to determine the
* correct FAT-type:
*
* FAT-12: ( firstWord & 0x3FF ) == 0x3F8 )
* FAT-16: ( firstWord == 0xFFF8 )
* endOfChain = bits 4-32 at beginning of table
* FAT-12: endOfChain == 0x0FF8
* FAT-16: endOfChain == 0xFFF8
* FAT-32: endOfChain == 0x0FFFFFF8
*/

static FF_Error_t prvDetermineFatType( FF_IOManager_t * pxIOManager )
{
FF_Partition_t * pxPartition;
FF_Buffer_t * pxBuffer;
uint32_t ulFirstWord = 0ul;
/* final cluster sentinel value */
uint32_t ulEndOfChain = 0ul;
FF_Error_t xError = FF_ERR_NONE;

pxPartition = &( pxIOManager->xPartition );
Expand Down Expand Up @@ -751,7 +754,7 @@ static FF_Error_t prvDetermineFatType( FF_IOManager_t * pxIOManager )
}
else
{
ulFirstWord = ( uint32_t ) FF_getShort( pxBuffer->pucBuffer, 0x0000 );
ulEndOfChain = FF_getLong( pxBuffer->pucBuffer, 0x0000 ) & 0xFFFFFFF8ul;
xError = FF_ReleaseBuffer( pxIOManager, pxBuffer );
}
}
Expand All @@ -764,7 +767,7 @@ static FF_Error_t prvDetermineFatType( FF_IOManager_t * pxIOManager )
/* FAT12 */
pxPartition->ucType = FF_T_FAT12;
#if ( ffconfigFAT_CHECK != 0 )
if( ( ulFirstWord & 0x3FF ) != 0x3F8 )
if( ulEndOfChain != 0x0FF8 )
{
xError = FF_createERR( FF_ERR_IOMAN_NOT_FAT_FORMATTED, FF_DETERMINEFATTYPE );
}
Expand All @@ -783,20 +786,20 @@ static FF_Error_t prvDetermineFatType( FF_IOManager_t * pxIOManager )
pxPartition->ucType = FF_T_FAT16;
#if ( ffconfigFAT_CHECK != 0 )
{
if( ulFirstWord == 0xFFF8 )
if( ulEndOfChain == 0xFFF8 )
{
xError = FF_ERR_NONE;
}
else
{
if( ( ulFirstWord & 0x3FF ) != 0x3F8 )
if( ulEndOfChain == 0x0FF8 )
{
FF_PRINTF( "Part at %lu is probably a FAT12\n", pxIOManager->xPartition.ulFATBeginLBA );
}
else
{
FF_PRINTF( "Partition at %lu has strange FAT data %08lX\n",
pxIOManager->xPartition.ulFATBeginLBA, ulFirstWord );
pxIOManager->xPartition.ulFATBeginLBA, ulEndOfChain );
}

xError = FF_createERR( FF_ERR_IOMAN_INVALID_FORMAT, FF_DETERMINEFATTYPE );
Expand All @@ -809,16 +812,9 @@ static FF_Error_t prvDetermineFatType( FF_IOManager_t * pxIOManager )
/* FAT 32! */
pxPartition->ucType = FF_T_FAT32;
#if ( ffconfigFAT_CHECK != 0 )
if( ( ( ulFirstWord & 0x0FFFFFF8 ) != 0x0FFFFFF8 ) &&
( ( ulFirstWord & 0x0FFFFFF8 ) != 0x0FFFFFF0 ) )
if( ulEndOfChain != 0x0FFFFFF8 )
{
/* _HT_
* I had an SD-card which worked well in Linux/W32
* but FreeRTOS+FAT returned me this error
* So for me I left out this check (just issue a warning for now)
*/
FF_PRINTF( "prvDetermineFatType: firstWord %08lX\n", ulFirstWord );
xError = FF_ERR_NONE; /* FF_ERR_IOMAN_NOT_FAT_FORMATTED; */
xError = FF_createERR( FF_ERR_IOMAN_NOT_FAT_FORMATTED, FF_DETERMINEFATTYPE );
}
#endif /* ffconfigFAT_CHECK */
xError = FF_ERR_NONE;
Expand Down
Loading