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-03-03 13:27:51 +01:00
|
|
|
#include "nand.h"
|
2016-02-13 17:29:56 +01:00
|
|
|
#include "sdmmc.h"
|
2016-02-16 16:15:08 +01:00
|
|
|
|
2016-02-27 14:02:03 +01:00
|
|
|
#define TYPE_SDCARD 0
|
|
|
|
#define TYPE_SYSNAND 1
|
|
|
|
#define TYPE_EMUNAND 2
|
|
|
|
|
2016-03-03 13:27:51 +01:00
|
|
|
#define SUBTYPE_CTRN 0
|
2016-02-27 14:02:03 +01:00
|
|
|
#define SUBTYPE_CTRN_N 1
|
|
|
|
#define SUBTYPE_TWLN 2
|
|
|
|
#define SUBTYPE_TWLP 3
|
|
|
|
#define SUBTYPE_NONE 4
|
2016-02-16 16:15:08 +01:00
|
|
|
|
2016-03-03 13:27:51 +01:00
|
|
|
#define SUBTYPE(pd) ((mode_n3ds && (DriveInfo[pd].subtype == SUBTYPE_CTRN)) ? SUBTYPE_CTRN_N : DriveInfo[pd].subtype)
|
|
|
|
|
2016-02-16 16:15:08 +01:00
|
|
|
typedef struct {
|
2016-02-27 14:02:03 +01:00
|
|
|
BYTE type;
|
|
|
|
BYTE subtype;
|
2016-02-16 16:15:08 +01:00
|
|
|
} FATpartition;
|
|
|
|
|
2016-02-27 14:02:03 +01:00
|
|
|
typedef struct {
|
|
|
|
DWORD offset;
|
2016-02-27 14:13:04 +01:00
|
|
|
DWORD size;
|
2016-02-27 14:02:03 +01:00
|
|
|
BYTE keyslot;
|
|
|
|
} SubtypeDesc;
|
|
|
|
|
2016-03-03 13:27:51 +01:00
|
|
|
FATpartition DriveInfo[7] = {
|
|
|
|
{ TYPE_SDCARD, SUBTYPE_NONE }, // 0 - SDCARD
|
|
|
|
{ TYPE_SYSNAND, SUBTYPE_CTRN }, // 1 - SYSNAND CTRNAND
|
|
|
|
{ TYPE_SYSNAND, SUBTYPE_TWLN }, // 2 - SYSNAND TWLN
|
|
|
|
{ TYPE_SYSNAND, SUBTYPE_TWLP }, // 3 - SYSNAND TWLP
|
|
|
|
{ TYPE_EMUNAND, SUBTYPE_CTRN }, // 4 - EMUNAND CTRNAND
|
|
|
|
{ TYPE_EMUNAND, SUBTYPE_TWLN }, // 5 - EMUNAND TWLN
|
|
|
|
{ TYPE_EMUNAND, SUBTYPE_TWLP }, // 6 - EMUNAND TWLP
|
2016-02-27 14:02:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
SubtypeDesc SubTypes[4] = {
|
2016-03-03 13:27:51 +01:00
|
|
|
{ 0x05CAE5, 0x179F1B, 0x4 }, // O3DS CTRNAND
|
|
|
|
{ 0x05CAD7, 0x20E969, 0x5 }, // N3DS CTRNAND
|
|
|
|
{ 0x000097, 0x047DA9, 0x3 }, // TWLN
|
|
|
|
{ 0x04808D, 0x0105B3, 0x3 } // TWLP
|
2016-02-16 16:15:08 +01:00
|
|
|
};
|
|
|
|
|
2016-02-25 16:57:01 +01:00
|
|
|
static bool mode_n3ds = false;
|
2016-02-27 14:02:03 +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-03-03 13:27:51 +01:00
|
|
|
sdmmc_sdcard_init(); // multiple inits should not be required (also, below)
|
|
|
|
InitNandCrypto();
|
2016-02-13 17:29:56 +01:00
|
|
|
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-27 14:02:03 +01:00
|
|
|
{
|
|
|
|
BYTE type = DriveInfo[pdrv].type;
|
|
|
|
|
|
|
|
if (type == TYPE_SDCARD) {
|
2016-02-16 16:15:08 +01:00
|
|
|
if (sdmmc_sdcard_readsectors(sector, count, buff)) {
|
|
|
|
return RES_PARERR;
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-03 13:27:51 +01:00
|
|
|
BYTE subtype = SUBTYPE(pdrv);
|
|
|
|
BYTE keyslot = SubTypes[subtype].keyslot;
|
2016-02-27 14:02:03 +01:00
|
|
|
DWORD isector = SubTypes[subtype].offset + sector;
|
|
|
|
|
2016-03-03 13:27:51 +01:00
|
|
|
if (ReadNandSectors(buff, isector, count, keyslot, type == TYPE_EMUNAND))
|
2016-02-27 14:13:04 +01:00
|
|
|
return RES_PARERR;
|
2016-02-16 16:15:08 +01:00
|
|
|
}
|
2016-02-13 17:29:56 +01:00
|
|
|
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Write Sector(s) */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
2016-02-26 19:43:30 +01:00
|
|
|
|
2016-02-13 17:29:56 +01:00
|
|
|
#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-03-03 13:27:51 +01:00
|
|
|
BYTE type = DriveInfo[pdrv].type;
|
2016-02-25 16:57:01 +01:00
|
|
|
|
2016-03-03 13:27:51 +01:00
|
|
|
if (type == TYPE_SDCARD) {
|
2016-02-16 16:15:08 +01:00
|
|
|
if (sdmmc_sdcard_writesectors(sector, count, (BYTE *)buff)) {
|
|
|
|
return RES_PARERR;
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-03 13:27:51 +01:00
|
|
|
/*BYTE subtype = SUBTYPE(pdrv);
|
|
|
|
BYTE keyslot = SubTypes[subtype].keyslot;
|
2016-02-27 14:02:03 +01:00
|
|
|
DWORD isector = SubTypes[subtype].offset + sector;
|
|
|
|
|
2016-03-03 13:27:51 +01:00
|
|
|
if (WriteNandSectors(buff, isector, count, keyslot, type == TYPE_EMUNAND))
|
|
|
|
return RES_PARERR;*/ // stubbed!
|
2016-02-16 16:15:08 +01:00
|
|
|
}
|
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 */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
switch (cmd) {
|
|
|
|
case GET_SECTOR_SIZE:
|
|
|
|
*((DWORD*) buff) = 0x200;
|
|
|
|
return RES_OK;
|
|
|
|
case GET_SECTOR_COUNT:
|
2016-02-27 14:13:04 +01:00
|
|
|
if (DriveInfo[pdrv].type == TYPE_SDCARD) {
|
|
|
|
*((DWORD*) buff) = getMMCDevice(1)->total_size;
|
|
|
|
} else {
|
2016-03-03 13:27:51 +01:00
|
|
|
*((DWORD*) buff) = SubTypes[SUBTYPE(pdrv)].size;
|
2016-02-27 14:13:04 +01:00
|
|
|
}
|
2016-02-13 17:29:56 +01:00
|
|
|
return RES_OK;
|
|
|
|
case GET_BLOCK_SIZE:
|
|
|
|
*((DWORD*) buff) = 0x2000;
|
2016-02-26 17:03:25 +01:00
|
|
|
return RES_OK;
|
2016-02-13 17:29:56 +01:00
|
|
|
case CTRL_SYNC:
|
|
|
|
// nothing to do here - the disk_write function handles that
|
|
|
|
return RES_OK;
|
|
|
|
}
|
2016-02-27 14:13:04 +01:00
|
|
|
return RES_PARERR;
|
2016-02-13 17:29:56 +01:00
|
|
|
}
|
|
|
|
#endif
|