2016-02-13 17:29:56 +01:00
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* If a working storage control module is available, it should be */
|
|
|
|
/* attached to the FatFs via a glue function rather than modifying it. */
|
|
|
|
/* This is an example of glue functions to attach various exsisting */
|
|
|
|
/* storage control modules to the FatFs module with a defined API. */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include "diskio.h" /* FatFs lower layer API */
|
2016-02-16 16:15:08 +01:00
|
|
|
#include "platform.h"
|
2016-02-13 17:29:56 +01:00
|
|
|
#include "sdmmc.h"
|
2016-02-25 16:57:01 +01:00
|
|
|
#include "nandio.h"
|
2016-02-16 16:15:08 +01:00
|
|
|
|
|
|
|
#define TYPE_SDCARD 0
|
|
|
|
#define TYPE_SYSNAND 1
|
|
|
|
#define TYPE_EMUNAND 2
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
DWORD offset;
|
|
|
|
DWORD subtype;
|
|
|
|
BYTE type;
|
|
|
|
} FATpartition;
|
|
|
|
|
|
|
|
FATpartition DriveInfo[28] = {
|
2016-02-16 16:26:46 +01:00
|
|
|
{ 0x000000, TYPE_SDCARD, 0 }, // 0 - SDCARD
|
2016-02-16 16:15:08 +01:00
|
|
|
{ 0x000000, TYPE_SYSNAND, P_CTRNAND }, // 1 - SYSNAND CTRNAND
|
|
|
|
{ 0x000000, TYPE_SYSNAND, P_TWLN }, // 2 - SYSNAND TWLN
|
|
|
|
{ 0x000000, TYPE_SYSNAND, P_TWLP }, // 3 - SYSNAND TWLP
|
|
|
|
{ 0x000000, TYPE_EMUNAND, P_CTRNAND }, // 4 - EMUNAND0 O3DS CTRNAND
|
|
|
|
{ 0x000000, TYPE_EMUNAND, P_TWLN }, // 5 - EMUNAND0 O3DS TWLN
|
|
|
|
{ 0x000000, TYPE_EMUNAND, P_TWLP }, // 6 - EMUNAND0 O3DS TWLP
|
|
|
|
{ 0x200000, TYPE_EMUNAND, P_CTRNAND }, // 7 - EMUNAND1 O3DS CTRNAND
|
|
|
|
{ 0x200000, TYPE_EMUNAND, P_TWLN }, // 8 - EMUNAND1 O3DS TWLN
|
|
|
|
{ 0x200000, TYPE_EMUNAND, P_TWLP }, // 9 - EMUNAND1 O3DS TWLP
|
|
|
|
{ 0x400000, TYPE_EMUNAND, P_CTRNAND }, // 10 - EMUNAND2 O3DS CTRNAND
|
|
|
|
{ 0x400000, TYPE_EMUNAND, P_TWLN }, // 11 - EMUNAND2 O3DS TWLN
|
|
|
|
{ 0x400000, TYPE_EMUNAND, P_TWLP }, // 12 - EMUNAND2 O3DS TWLP
|
|
|
|
{ 0x600000, TYPE_EMUNAND, P_CTRNAND }, // 13 - EMUNAND3 O3DS CTRNAND
|
|
|
|
{ 0x600000, TYPE_EMUNAND, P_TWLN }, // 14 - EMUNAND3 O3DS TWLN
|
|
|
|
{ 0x600000, TYPE_EMUNAND, P_TWLP }, // 15 - EMUNAND3 O3DS TWLP
|
|
|
|
{ 0x000000, TYPE_EMUNAND, P_CTRNAND }, // 16 - EMUNAND0 N3DS CTRNAND
|
|
|
|
{ 0x000000, TYPE_EMUNAND, P_TWLN }, // 17 - EMUNAND0 N3DS TWLN
|
|
|
|
{ 0x000000, TYPE_EMUNAND, P_TWLP }, // 18 - EMUNAND0 N3DS TWLP
|
|
|
|
{ 0x400000, TYPE_EMUNAND, P_CTRNAND }, // 19 - EMUNAND1 N3DS CTRNAND
|
|
|
|
{ 0x400000, TYPE_EMUNAND, P_TWLN }, // 20 - EMUNAND1 N3DS TWLN
|
|
|
|
{ 0x400000, TYPE_EMUNAND, P_TWLP }, // 21 - EMUNAND1 N3DS TWLP
|
|
|
|
{ 0x800000, TYPE_EMUNAND, P_CTRNAND }, // 22 - EMUNAND2 N3DS CTRNAND
|
|
|
|
{ 0x800000, TYPE_EMUNAND, P_TWLN }, // 23 - EMUNAND2 N3DS TWLN
|
|
|
|
{ 0x800000, TYPE_EMUNAND, P_TWLP }, // 24 - EMUNAND2 N3DS TWLP
|
|
|
|
{ 0xC00000, TYPE_EMUNAND, P_CTRNAND }, // 25 - EMUNAND3 N3DS CTRNAND
|
|
|
|
{ 0xC00000, TYPE_EMUNAND, P_TWLN }, // 26 - EMUNAND3 N3DS TWLN
|
|
|
|
{ 0xC00000, TYPE_EMUNAND, P_TWLP } // 27 - EMUNAND3 N3DS TWLP
|
|
|
|
};
|
|
|
|
|
2016-02-25 16:57:01 +01:00
|
|
|
static bool mode_n3ds = false;
|
2016-02-16 16:15:08 +01:00
|
|
|
|
2016-02-13 17:29:56 +01:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Get Drive Status */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
DSTATUS disk_status (
|
|
|
|
__attribute__((unused))
|
|
|
|
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Inidialize a Drive */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
DSTATUS disk_initialize (
|
|
|
|
__attribute__((unused))
|
|
|
|
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
|
|
|
)
|
|
|
|
{
|
2016-02-25 16:57:01 +01:00
|
|
|
mode_n3ds = (GetUnitPlatform() == PLATFORM_N3DS);
|
2016-02-13 17:29:56 +01:00
|
|
|
sdmmc_sdcard_init();
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Read Sector(s) */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
DRESULT disk_read (
|
|
|
|
__attribute__((unused))
|
|
|
|
BYTE pdrv, /* Physical drive nmuber to identify the drive */
|
|
|
|
BYTE *buff, /* Data buffer to store read data */
|
|
|
|
DWORD sector, /* Sector address in LBA */
|
|
|
|
UINT count /* Number of sectors to read */
|
|
|
|
)
|
|
|
|
{
|
2016-02-25 16:57:01 +01:00
|
|
|
if ((pdrv >= 4) && mode_n3ds)
|
|
|
|
pdrv += 12;
|
|
|
|
|
2016-02-16 16:26:46 +01:00
|
|
|
if (DriveInfo[pdrv].type == TYPE_SDCARD) {
|
2016-02-16 16:15:08 +01:00
|
|
|
if (sdmmc_sdcard_readsectors(sector, count, buff)) {
|
|
|
|
return RES_PARERR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PartitionInfo* partition = GetPartitionInfo(DriveInfo[pdrv].subtype);
|
|
|
|
if (partition == NULL) return RES_PARERR;
|
|
|
|
u32 offset = (sector * 0x200) + partition->offset;
|
|
|
|
SetNand(DriveInfo[pdrv].type == TYPE_EMUNAND, DriveInfo[pdrv].offset);
|
|
|
|
if (DecryptNandToMem(buff, offset, count * 0x200, partition) != 0)
|
|
|
|
return RES_PARERR;
|
|
|
|
}
|
2016-02-13 17:29:56 +01:00
|
|
|
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Write Sector(s) */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#if _USE_WRITE
|
|
|
|
DRESULT disk_write (
|
|
|
|
__attribute__((unused))
|
|
|
|
BYTE pdrv, /* Physical drive nmuber to identify the drive */
|
|
|
|
const BYTE *buff, /* Data to be written */
|
|
|
|
DWORD sector, /* Sector address in LBA */
|
|
|
|
UINT count /* Number of sectors to write */
|
|
|
|
)
|
|
|
|
{
|
2016-02-25 16:57:01 +01:00
|
|
|
if ((pdrv >= 4) && mode_n3ds)
|
|
|
|
pdrv += 12;
|
|
|
|
|
2016-02-16 16:26:46 +01:00
|
|
|
if (DriveInfo[pdrv].type == TYPE_SDCARD) {
|
2016-02-16 16:15:08 +01:00
|
|
|
if (sdmmc_sdcard_writesectors(sector, count, (BYTE *)buff)) {
|
|
|
|
return RES_PARERR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PartitionInfo* partition = GetPartitionInfo(DriveInfo[pdrv].subtype);
|
|
|
|
if (partition == NULL) return RES_PARERR;
|
|
|
|
u32 offset = (sector * 0x200) + partition->offset;
|
|
|
|
SetNand(DriveInfo[pdrv].type == TYPE_EMUNAND, DriveInfo[pdrv].offset);
|
|
|
|
// if (EncryptMemToNand(buff, offset, count * 0x200, partition) != 0)
|
|
|
|
return RES_PARERR;
|
|
|
|
// NO, not yet!
|
|
|
|
}
|
2016-02-13 17:29:56 +01:00
|
|
|
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Miscellaneous Functions */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#if _USE_IOCTL
|
|
|
|
DRESULT disk_ioctl (
|
|
|
|
__attribute__((unused))
|
|
|
|
BYTE pdrv, /* Physical drive nmuber (0..) */
|
|
|
|
__attribute__((unused))
|
|
|
|
BYTE cmd, /* Control code */
|
|
|
|
__attribute__((unused))
|
|
|
|
void *buff /* Buffer to send/receive control data */
|
|
|
|
)
|
|
|
|
{
|
2016-02-25 16:57:01 +01:00
|
|
|
if ((pdrv >= 4) && mode_n3ds)
|
|
|
|
pdrv += 12;
|
|
|
|
|
2016-02-13 17:29:56 +01:00
|
|
|
switch (cmd) {
|
|
|
|
case GET_SECTOR_SIZE:
|
|
|
|
*((DWORD*) buff) = 0x200;
|
|
|
|
return RES_OK;
|
|
|
|
case GET_SECTOR_COUNT:
|
2016-02-16 16:26:46 +01:00
|
|
|
*((DWORD*) buff) = getMMCDevice((DriveInfo[pdrv].type == TYPE_SDCARD) ? 1 : 0)->total_size;
|
2016-02-13 17:29:56 +01:00
|
|
|
return RES_OK;
|
|
|
|
case GET_BLOCK_SIZE:
|
|
|
|
*((DWORD*) buff) = 0x2000;
|
2016-02-16 16:26:46 +01:00
|
|
|
return (DriveInfo[pdrv].type == TYPE_SDCARD) ? RES_OK : RES_PARERR;
|
2016-02-13 17:29:56 +01:00
|
|
|
case CTRL_SYNC:
|
|
|
|
// nothing to do here - the disk_write function handles that
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
return RES_PARERR;
|
|
|
|
}
|
|
|
|
#endif
|