diff --git a/src/DSi_NAND.cpp b/src/DSi_NAND.cpp index 917578ea..fa94f9dc 100644 --- a/src/DSi_NAND.cpp +++ b/src/DSi_NAND.cpp @@ -52,7 +52,10 @@ bool Init(FILE* nandfile, u8* es_keyY) if (!nandfile) return false; - ff_disk_open(FF_ReadNAND, FF_WriteNAND); + fseek(nandfile, 0, SEEK_END); + u64 nandlen = ftell(nandfile); + + ff_disk_open(FF_ReadNAND, FF_WriteNAND, (LBA_t)(nandlen>>9)); FRESULT res; res = f_mount(&CurFS, "0:", 0); diff --git a/src/FATStorage.cpp b/src/FATStorage.cpp index 26262e36..a6dc85f9 100644 --- a/src/FATStorage.cpp +++ b/src/FATStorage.cpp @@ -15,3 +15,106 @@ You should have received a copy of the GNU General Public License along with melonDS. If not, see http://www.gnu.org/licenses/. */ + +#include "FATStorage.h" +#include "Platform.h" + + +FATStorage::FATStorage() +{ + printf("FATStorage begin\n"); + bool res = Build(".", 0x40000000, "melonDLDI.bin"); + printf("FATStorage result: %d\n", res); +} + +FATStorage::~FATStorage() +{ + // +} + + +FILE* FATStorage::FF_File; +u64 FATStorage::FF_FileSize; + +UINT FATStorage::FF_ReadStorage(BYTE* buf, LBA_t sector, UINT num) +{ + if (!FF_File) return 0; + + u64 addr = sector * 0x200ULL; + u32 len = num * 0x200; + + if ((addr+len) > FF_FileSize) + { + if (addr >= FF_FileSize) return 0; + len = FF_FileSize - addr; + num = len >> 9; + } + + fseek(FF_File, addr, SEEK_SET); + + u32 res = fread(buf, 0x200, num, FF_File); + if (res < num) + { + if (feof(FF_File)) + { + memset(&buf[0x200*res], 0, 0x200*(num-res)); + return num; + } + } + + return res; +} + +UINT FATStorage::FF_WriteStorage(BYTE* buf, LBA_t sector, UINT num) +{ + if (!FF_File) return 0; + + u64 addr = sector * 0x200ULL; + u32 len = num * 0x200; + + if ((addr+len) > FF_FileSize) + { + if (addr >= FF_FileSize) return 0; + len = FF_FileSize - addr; + num = len >> 9; + } + + fseek(FF_File, addr, SEEK_SET); + + u32 res = fwrite(buf, 0x200, num, FF_File); + return res; +} + + +bool FATStorage::Build(const char* sourcedir, u64 size, const char* filename) +{ + filesize = size; + + FF_File = Platform::OpenLocalFile(filename, "wb"); + if (!FF_File) + return false; + + FF_FileSize = size; + ff_disk_open(FF_ReadStorage, FF_WriteStorage, (LBA_t)(size>>9)); + + FRESULT res; + + MKFS_PARM fsopt; + fsopt.fmt = FM_FAT | FM_FAT32; + fsopt.au_size = 0; + fsopt.align = 1; + fsopt.n_fat = 1; + fsopt.n_root = 512; + + BYTE workbuf[FF_MAX_SS]; + res = f_mkfs("0:", &fsopt, workbuf, sizeof(workbuf)); + printf("MKFS RES %d\n", res); + + // + + ff_disk_close(); + fclose(FF_File); + FF_File = nullptr; + + return true; +} diff --git a/src/FATStorage.h b/src/FATStorage.h index c0f2fd9c..5608c958 100644 --- a/src/FATStorage.h +++ b/src/FATStorage.h @@ -19,8 +19,28 @@ #ifndef FATSTORAGE_H #define FATSTORAGE_H -#include "types.h" +#include -// +#include "types.h" +#include "fatfs/ff.h" + + +class FATStorage +{ +public: + FATStorage(); + ~FATStorage(); + +private: + FILE* file; + u64 filesize; + + static FILE* FF_File; + static u64 FF_FileSize; + static UINT FF_ReadStorage(BYTE* buf, LBA_t sector, UINT num); + static UINT FF_WriteStorage(BYTE* buf, LBA_t sector, UINT num); + + bool Build(const char* sourcedir, u64 size, const char* filename); +}; #endif // FATSTORAGE_H diff --git a/src/NDSCart.cpp b/src/NDSCart.cpp index a88ba59d..6d7ec21d 100644 --- a/src/NDSCart.cpp +++ b/src/NDSCart.cpp @@ -1164,10 +1164,12 @@ u8 CartRetailBT::SPIWrite(u8 val, u32 pos, bool last) CartHomebrew::CartHomebrew(u8* rom, u32 len, u32 chipid) : CartCommon(rom, len, chipid) { + test = nullptr; if (Config::DLDIEnable) { ApplyDLDIPatch(melonDLDI, sizeof(melonDLDI)); SDFile = Platform::OpenLocalFile(Config::DLDISDPath, "r+b"); + test = new FATStorage(); } else SDFile = nullptr; @@ -1176,6 +1178,7 @@ CartHomebrew::CartHomebrew(u8* rom, u32 len, u32 chipid) : CartCommon(rom, len, CartHomebrew::~CartHomebrew() { if (SDFile) fclose(SDFile); + if (test) delete test; } void CartHomebrew::Reset() diff --git a/src/NDSCart.h b/src/NDSCart.h index ecfb2091..ef3601ec 100644 --- a/src/NDSCart.h +++ b/src/NDSCart.h @@ -21,6 +21,7 @@ #include "types.h" #include "NDS_Header.h" +#include "FATStorage.h" namespace NDSCart { @@ -182,6 +183,7 @@ private: void ReadROM_B7(u32 addr, u32 len, u8* data, u32 offset); FILE* SDFile; + FATStorage* test; }; extern u16 SPICnt; diff --git a/src/fatfs/diskio.c b/src/fatfs/diskio.c index 225d9bf2..30a8c0aa 100644 --- a/src/fatfs/diskio.c +++ b/src/fatfs/diskio.c @@ -13,15 +13,17 @@ static ff_disk_read_cb ReadCb; static ff_disk_write_cb WriteCb; +static LBA_t SectorCount; static DSTATUS Status = STA_NOINIT | STA_NODISK; -void ff_disk_open(ff_disk_read_cb readcb, ff_disk_write_cb writecb) +void ff_disk_open(ff_disk_read_cb readcb, ff_disk_write_cb writecb, LBA_t seccnt) { if (!readcb) return; ReadCb = readcb; WriteCb = writecb; + SectorCount = seccnt; Status &= ~STA_NODISK; if (!writecb) Status |= STA_PROTECT; @@ -32,6 +34,7 @@ void ff_disk_close() { ReadCb = (void*)0; WriteCb = (void*)0; + SectorCount = 0; Status &= ~STA_PROTECT; Status |= STA_NODISK; @@ -123,12 +126,28 @@ DRESULT disk_ioctl ( { switch (cmd) { - case 0: // sync + case CTRL_SYNC: // TODO: fflush? return RES_OK; + + case GET_SECTOR_COUNT: + *(LBA_t*)buff = SectorCount; + return RES_OK; + + case GET_SECTOR_SIZE: + *(WORD*)buff = 0x200; + return RES_OK; + + case GET_BLOCK_SIZE: + *(DWORD*)buff = 1; + return RES_OK; + + case CTRL_TRIM: + // TODO?? + return RES_OK; } - //printf("disk_ioctl(%02X, %02X, %p)\n", pdrv, cmd, buff); + printf("FatFS: unknown disk_ioctl(%02X, %02X, %p)\n", pdrv, cmd, buff); return RES_PARERR; } diff --git a/src/fatfs/ff.h b/src/fatfs/ff.h index 6df78a8e..87aeacee 100644 --- a/src/fatfs/ff.h +++ b/src/fatfs/ff.h @@ -420,7 +420,7 @@ int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */ typedef UINT (*ff_disk_read_cb)(BYTE* buff, LBA_t sector, UINT count); typedef UINT (*ff_disk_write_cb)(BYTE* buff, LBA_t sector, UINT count); -void ff_disk_open(ff_disk_read_cb readcb, ff_disk_write_cb writecb); +void ff_disk_open(ff_disk_read_cb readcb, ff_disk_write_cb writecb, LBA_t seccnt); void ff_disk_close();