From 001442fd8ac39022061911fd9a5782bef601ae4c Mon Sep 17 00:00:00 2001 From: Edoardo Lolletti Date: Sun, 28 Jul 2024 11:03:31 +0200 Subject: [PATCH] Add sd cluster size check If a sd is present, and its cluster size is bigger than 32k, abort installing --- arm9/src/main.c | 7 +++++++ arm9/src/storage.c | 29 +++++++++++++++++++++++++++++ arm9/src/storage.h | 2 ++ 3 files changed, 38 insertions(+) diff --git a/arm9/src/main.c b/arm9/src/main.c index c3d6a4a..d9228a9 100644 --- a/arm9/src/main.c +++ b/arm9/src/main.c @@ -200,6 +200,13 @@ int main(int argc, char **argv) messageBox("fatInitDefault()...\x1B[31mFailed\n\x1B[47m"); } + u32 clusterSize = getClusterSizeForPartition("sd:/"); + if(clusterSize > 32768) + { + messageBox("Sd card cluster size is too large"); + return 0; + } + //setup nand access if (!fatMountSimple("nand", &io_dsi_nand)) { diff --git a/arm9/src/storage.c b/arm9/src/storage.c index 31c728f..6f38508 100644 --- a/arm9/src/storage.c +++ b/arm9/src/storage.c @@ -226,3 +226,32 @@ bool removeIfExists(const char* path) { return remove(path) == 0 || errno == ENOENT; } + +// Filesystem type +typedef enum {FS_UNKNOWN, FS_FAT12, FS_FAT16, FS_FAT32} FS_TYPE; +//trimmed down PARTITION struct from libfat internals +typedef struct { + const void* disc; + void* cache; + // Info about the partition + FS_TYPE filesysType; + uint64_t totalSize; + sec_t rootDirStart; + uint32_t rootDirCluster; + uint32_t numberOfSectors; + sec_t dataStart; + uint32_t bytesPerSector; + uint32_t sectorsPerCluster; + uint32_t bytesPerCluster; + uint32_t fsInfoSector; +} PARTITION; + +extern PARTITION* _FAT_partition_getPartitionFromPath(const char* path); + +u32 getClusterSizeForPartition(const char* path) +{ + PARTITION* p = _FAT_partition_getPartitionFromPath(path); + if(!p) + return 0; + return p->bytesPerCluster; +} diff --git a/arm9/src/storage.h b/arm9/src/storage.h index 73d8a93..d166460 100644 --- a/arm9/src/storage.h +++ b/arm9/src/storage.h @@ -25,6 +25,8 @@ bool safeCreateDir(const char* path); //Files and directories bool removeIfExists(const char* path); +u32 getClusterSizeForPartition(const char* path); + #ifdef __cplusplus } #endif