@@ -102,6 +102,14 @@ enum FSIB
102102static const char FAT_SIG [3 ] = {'F' , 'A' , 'T' };
103103static const char FS_INFO_SIG1 [4 ] = {'R' , 'R' , 'a' , 'A' };
104104static const char FS_INFO_SIG2 [4 ] = {'r' , 'r' , 'A' , 'a' };
105+ static const char FS_TWL_SIG [8 ] = { 0xe9 , 0x00 , 0x00 , 0x54 , 0x57 , 0x4c , 0x20 , 0x20 };
106+
107+ static bool isValidMBR (uint8_t * sectorBuffer ) {
108+ return (!memcmp (sectorBuffer + BPB_FAT16_fileSysType , FAT_SIG , sizeof (FAT_SIG )) ||
109+ !memcmp (sectorBuffer + BPB_FAT32_fileSysType , FAT_SIG , sizeof (FAT_SIG )) ||
110+ !memcmp (sectorBuffer , FS_TWL_SIG , sizeof (FS_TWL_SIG )));
111+
112+ }
105113
106114sec_t FindFirstValidPartition_buf (const DISC_INTERFACE * disc , uint8_t * sectorBuffer )
107115{
@@ -120,8 +128,8 @@ sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuf
120128 for (i = 0 ;i < 4 ;i ++ ,ptr += 16 ) {
121129 sec_t part_lba = u8array_to_u32 (ptr , 0x8 );
122130
123- if (! memcmp (sectorBuffer + BPB_FAT16_fileSysType , FAT_SIG , sizeof ( FAT_SIG )) ||
124- ! memcmp ( sectorBuffer + BPB_FAT32_fileSysType , FAT_SIG , sizeof ( FAT_SIG ))) {
131+ if (isValidMBR (sectorBuffer ))
132+ {
125133 return part_lba ;
126134 }
127135
@@ -141,8 +149,7 @@ sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuf
141149
142150 if (!_FAT_disc_readSectors (disc , part_lba2 , 1 , sectorBuffer )) return 0 ;
143151
144- if (!memcmp (sectorBuffer + BPB_FAT16_fileSysType , FAT_SIG , sizeof (FAT_SIG )) ||
145- !memcmp (sectorBuffer + BPB_FAT32_fileSysType , FAT_SIG , sizeof (FAT_SIG )))
152+ if (isValidMBR (sectorBuffer ))
146153 {
147154 return part_lba2 ;
148155 }
@@ -151,8 +158,10 @@ sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuf
151158 }
152159 } else {
153160 if (!_FAT_disc_readSectors (disc , part_lba , 1 , sectorBuffer )) return 0 ;
154- if (!memcmp (sectorBuffer + BPB_FAT16_fileSysType , FAT_SIG , sizeof (FAT_SIG )) ||
155- !memcmp (sectorBuffer + BPB_FAT32_fileSysType , FAT_SIG , sizeof (FAT_SIG ))) {
161+
162+
163+ if (isValidMBR (sectorBuffer ))
164+ {
156165 return part_lba ;
157166 }
158167 }
@@ -199,9 +208,7 @@ PARTITION* _FAT_partition_constructor_buf (const DISC_INTERFACE* disc, uint32_t
199208 }
200209 }
201210
202- // Now verify that this is indeed a FAT partition
203- if (memcmp (sectorBuffer + BPB_FAT16_fileSysType , FAT_SIG , sizeof (FAT_SIG )) &&
204- memcmp (sectorBuffer + BPB_FAT32_fileSysType , FAT_SIG , sizeof (FAT_SIG )))
211+ if (!isValidMBR (sectorBuffer ))
205212 {
206213 return NULL ;
207214 }
@@ -348,6 +355,13 @@ PARTITION* _FAT_partition_getPartitionFromPath (const char* path) {
348355 return (PARTITION * )devops -> deviceData ;
349356}
350357
358+ static void _FAT_updateFS_INFO (PARTITION * partition , uint8_t * sectorBuffer ) {
359+ partition -> fat .numberFreeCluster = _FAT_fat_freeClusterCount (partition );
360+ u32_to_u8array (sectorBuffer , FSIB_numberOfFreeCluster , partition -> fat .numberFreeCluster );
361+ u32_to_u8array (sectorBuffer , FSIB_numberLastAllocCluster , partition -> fat .numberLastAllocCluster );
362+ _FAT_disc_writeSectors (partition -> disc , partition -> fsInfoSector , 1 , sectorBuffer );
363+ }
364+
351365void _FAT_partition_createFSinfo (PARTITION * partition )
352366{
353367 if (partition -> readOnly || partition -> filesysType != FS_FAT32 )
@@ -364,14 +378,10 @@ void _FAT_partition_createFSinfo(PARTITION * partition)
364378 sectorBuffer [FSIB_SIG2 + i ] = FS_INFO_SIG2 [i ];
365379 }
366380
367- partition -> fat .numberFreeCluster = _FAT_fat_freeClusterCount (partition );
368- u32_to_u8array (sectorBuffer , FSIB_numberOfFreeCluster , partition -> fat .numberFreeCluster );
369- u32_to_u8array (sectorBuffer , FSIB_numberLastAllocCluster , partition -> fat .numberLastAllocCluster );
370-
371381 sectorBuffer [FSIB_bootSig_55 ] = 0x55 ;
372382 sectorBuffer [FSIB_bootSig_AA ] = 0xAA ;
373383
374- _FAT_disc_writeSectors (partition -> disc , partition -> fsInfoSector , 1 , sectorBuffer );
384+ _FAT_updateFS_INFO (partition , sectorBuffer );
375385
376386 _FAT_mem_free (sectorBuffer );
377387}
@@ -398,6 +408,10 @@ void _FAT_partition_readFSinfo(PARTITION * partition)
398408 _FAT_partition_createFSinfo (partition );
399409 } else {
400410 partition -> fat .numberFreeCluster = u8array_to_u32 (sectorBuffer , FSIB_numberOfFreeCluster );
411+ if (partition -> fat .numberFreeCluster == 0xffffffff ) {
412+ _FAT_updateFS_INFO (partition ,sectorBuffer );
413+ partition -> fat .numberFreeCluster = u8array_to_u32 (sectorBuffer , FSIB_numberOfFreeCluster );
414+ }
401415 partition -> fat .numberLastAllocCluster = u8array_to_u32 (sectorBuffer , FSIB_numberLastAllocCluster );
402416 }
403417 _FAT_mem_free (sectorBuffer );
0 commit comments