Skip to content

Commit

Permalink
Merge branch 'master' of github.com:devkitPro/libfat
Browse files Browse the repository at this point in the history
  • Loading branch information
Mystro256 committed Oct 1, 2017
2 parents 8edfec7 + e7b04a7 commit 780ce46
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>devkitPro)
endif

export TOPDIR := $(CURDIR)

export LIBFAT_MAJOR := 1
export LIBFAT_MINOR := 1
export LIBFAT_PATCH := 0
export LIBFAT_PATCH := 2

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

Expand Down
5 changes: 1 addition & 4 deletions source/disc.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,9 @@ const INTERFACE_ID _FAT_disc_interfaces[] = {
/* ====================== NDS ====================== */
#elif defined (NDS)
#include <nds/system.h>
#include <nds/memory.h>
#include <nds/arm9/dldi.h>

static const DISC_INTERFACE* get_io_dsisd (void) {
return isDSiMode() ? &__io_dsisd : NULL;
}

const INTERFACE_ID _FAT_disc_interfaces[] = {
{"sd", get_io_dsisd},
{"fat", dldiGetInternal},
Expand Down
13 changes: 5 additions & 8 deletions source/fatdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,16 +465,13 @@ int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf)

_FAT_lock(&partition->lock);

if(memcmp(&buf->f_flag, "SCAN", 4) == 0)
{
//Special command was given to sync the numberFreeCluster
_FAT_partition_createFSinfo(partition);
}

if(partition->filesysType == FS_FAT32)
if(partition->filesysType == FS_FAT32) {
// Sync FSinfo block
_FAT_partition_readFSinfo(partition);
freeClusterCount = partition->fat.numberFreeCluster;
else
} else {
freeClusterCount = _FAT_fat_freeClusterCount (partition);
}

// FAT clusters = POSIX blocks
buf->f_bsize = partition->bytesPerCluster; // File system block size.
Expand Down
42 changes: 28 additions & 14 deletions source/partition.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ enum FSIB
static const char FAT_SIG[3] = {'F', 'A', 'T'};
static const char FS_INFO_SIG1[4] = {'R', 'R', 'a', 'A'};
static const char FS_INFO_SIG2[4] = {'r', 'r', 'A', 'a'};
static const char FS_TWL_SIG[8] = { 0xe9, 0x00, 0x00, 0x54, 0x57, 0x4c, 0x20, 0x20 };

static bool isValidMBR(uint8_t *sectorBuffer) {
return (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
!memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
!memcmp(sectorBuffer, FS_TWL_SIG, sizeof(FS_TWL_SIG)));

}

sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuffer)
{
Expand All @@ -120,8 +128,8 @@ sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuf
for(i=0;i<4;i++,ptr+=16) {
sec_t part_lba = u8array_to_u32(ptr, 0x8);

if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
!memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) {
if (isValidMBR(sectorBuffer))
{
return part_lba;
}

Expand All @@ -141,8 +149,7 @@ sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuf

if(!_FAT_disc_readSectors (disc, part_lba2, 1, sectorBuffer)) return 0;

if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
!memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG)))
if (isValidMBR(sectorBuffer))
{
return part_lba2;
}
Expand All @@ -151,8 +158,10 @@ sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuf
}
} else {
if(!_FAT_disc_readSectors (disc, part_lba, 1, sectorBuffer)) return 0;
if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
!memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) {


if (isValidMBR(sectorBuffer))
{
return part_lba;
}
}
Expand Down Expand Up @@ -199,9 +208,7 @@ PARTITION* _FAT_partition_constructor_buf (const DISC_INTERFACE* disc, uint32_t
}
}

// Now verify that this is indeed a FAT partition
if (memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) &&
memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG)))
if (!isValidMBR(sectorBuffer))
{
return NULL;
}
Expand Down Expand Up @@ -348,6 +355,13 @@ PARTITION* _FAT_partition_getPartitionFromPath (const char* path) {
return (PARTITION*)devops->deviceData;
}

static void _FAT_updateFS_INFO(PARTITION * partition, uint8_t *sectorBuffer) {
partition->fat.numberFreeCluster = _FAT_fat_freeClusterCount(partition);
u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster);
u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster);
_FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer);
}

void _FAT_partition_createFSinfo(PARTITION * partition)
{
if(partition->readOnly || partition->filesysType != FS_FAT32)
Expand All @@ -364,14 +378,10 @@ void _FAT_partition_createFSinfo(PARTITION * partition)
sectorBuffer[FSIB_SIG2+i] = FS_INFO_SIG2[i];
}

partition->fat.numberFreeCluster = _FAT_fat_freeClusterCount(partition);
u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster);
u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster);

sectorBuffer[FSIB_bootSig_55] = 0x55;
sectorBuffer[FSIB_bootSig_AA] = 0xAA;

_FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer);
_FAT_updateFS_INFO(partition,sectorBuffer);

_FAT_mem_free(sectorBuffer);
}
Expand All @@ -398,6 +408,10 @@ void _FAT_partition_readFSinfo(PARTITION * partition)
_FAT_partition_createFSinfo(partition);
} else {
partition->fat.numberFreeCluster = u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster);
if(partition->fat.numberFreeCluster == 0xffffffff) {
_FAT_updateFS_INFO(partition,sectorBuffer);
partition->fat.numberFreeCluster = u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster);
}
partition->fat.numberLastAllocCluster = u8array_to_u32(sectorBuffer, FSIB_numberLastAllocCluster);
}
_FAT_mem_free(sectorBuffer);
Expand Down

0 comments on commit 780ce46

Please sign in to comment.