Skip to content

Commit 780ce46

Browse files
committed
Merge branch 'master' of github.com:devkitPro/libfat
2 parents 8edfec7 + e7b04a7 commit 780ce46

4 files changed

Lines changed: 37 additions & 29 deletions

File tree

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
ifeq ($(strip $(DEVKITPRO)),)
22
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>devkitPro)
33
endif
4-
4+
55
export TOPDIR := $(CURDIR)
6-
6+
77
export LIBFAT_MAJOR := 1
88
export LIBFAT_MINOR := 1
9-
export LIBFAT_PATCH := 0
9+
export LIBFAT_PATCH := 2
1010

1111
export VERSTRING := $(LIBFAT_MAJOR).$(LIBFAT_MINOR).$(LIBFAT_PATCH)
1212

source/disc.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,9 @@ const INTERFACE_ID _FAT_disc_interfaces[] = {
8787
/* ====================== NDS ====================== */
8888
#elif defined (NDS)
8989
#include <nds/system.h>
90+
#include <nds/memory.h>
9091
#include <nds/arm9/dldi.h>
9192

92-
static const DISC_INTERFACE* get_io_dsisd (void) {
93-
return isDSiMode() ? &__io_dsisd : NULL;
94-
}
95-
9693
const INTERFACE_ID _FAT_disc_interfaces[] = {
9794
{"sd", get_io_dsisd},
9895
{"fat", dldiGetInternal},

source/fatdir.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -465,16 +465,13 @@ int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf)
465465

466466
_FAT_lock(&partition->lock);
467467

468-
if(memcmp(&buf->f_flag, "SCAN", 4) == 0)
469-
{
470-
//Special command was given to sync the numberFreeCluster
471-
_FAT_partition_createFSinfo(partition);
472-
}
473-
474-
if(partition->filesysType == FS_FAT32)
468+
if(partition->filesysType == FS_FAT32) {
469+
// Sync FSinfo block
470+
_FAT_partition_readFSinfo(partition);
475471
freeClusterCount = partition->fat.numberFreeCluster;
476-
else
472+
} else {
477473
freeClusterCount = _FAT_fat_freeClusterCount (partition);
474+
}
478475

479476
// FAT clusters = POSIX blocks
480477
buf->f_bsize = partition->bytesPerCluster; // File system block size.

source/partition.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ enum FSIB
102102
static const char FAT_SIG[3] = {'F', 'A', 'T'};
103103
static const char FS_INFO_SIG1[4] = {'R', 'R', 'a', 'A'};
104104
static 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

106114
sec_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+
351365
void _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

Comments
 (0)