diff --git a/source/arm9/fs.c b/source/arm9/fs.c index 71ca67f..2cfb691 100644 --- a/source/arm9/fs.c +++ b/source/arm9/fs.c @@ -17,10 +17,17 @@ */ #include +#include #include "types.h" #include "fs.h" #include "fatfs/ff.h" +typedef struct +{ + u8 *mem; + size_t memSize; + size_t dataSize; +} DevBuf; static FATFS fsTable[FS_MAX_DRIVES] = {0}; static const char *const fsPathTable[FS_MAX_DRIVES] = {FS_DRIVE_NAMES}; @@ -37,6 +44,8 @@ static bool devStatTable[FS_MAX_DEVICES] = {0}; static bool fsStatBackupTable[FS_MAX_DRIVES] = {0}; +static DevBuf devBuf; + s32 fMount(FsDrive drive) { @@ -125,6 +134,57 @@ return FR_OK; } +static bool devBufAllocate(DevBuf *devBuf, u32 size) +{ + devBuf->mem = malloc(size); + if(!devBuf->mem) return false; + + devBuf->memSize = size; + + return true; +} + +s32 fCreateDeviceBuffer(u32 size) +{ + if(!size || size > 0x40000) return -30; + if(devBuf.mem) return -31; + + if(!devBufAllocate(&devBuf, size)) + return -30; + + return FR_OK; +} + +static bool isValidDevBufHandle(DevBufHandle handle) +{ + if(handle != FR_OK) + return false; + + if(!devBuf.mem) + return false; + + return true; +} + +static bool devBufFree(DevBuf *devBuf) +{ + free(devBuf->mem); + devBuf->mem = NULL; + + devBuf->memSize = 0; + + return true; +} + +s32 fFreeDeviceBuffer(DevBufHandle handle) +{ + if(!isValidDevBufHandle(handle)) return -30; + + devBufFree(&devBuf); + + return FR_OK; +} + static s32 findUnusedFileSlot(void) { if(fHandles >= FS_MAX_FILES) return -1;