Skip to content
Open
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ GodMode9 provides access to system data via drives, a listing of what each drive
* __`9: RAM DRIVE / IMGNAND TWLP`__: This provides access to the RAM drive. All data stored inside the RAM drive is temporary and will be wiped after a reboot. When a NAND image is mounted, this provides access to the mounted NAND image's TWLP.
* __`I: IMGNAND VIRTUAL`__: When a NAND image is mounted, this provides access to the partitions inside the NAND image.
* __`C: GAMECART`__: This is read-only and provides access to the game cartridge currently inserted into the cart slot. This can be used for dumps of CTR and TWL mode cartridges. Flash cards are supported only to a limited extent.
* __`H: DSPICO SD`__: This provides access to the SD card in a DSpico flashcart and mounts automatically. At the drive list, use `R+SELECT` to unmount or remount it. Unmount before ejecting the cart.
* __`G: GAME IMAGE`__: CIA/NCSD/NCCH/EXEFS/ROMFS/FIRM images can be accessed via this drive when mounted. This is read-only.
* __`K: AESKEYDB IMAGE`__: An `aeskeydb.bin` image can be mounted and accessed via this drive. The drive shows all keys inside the aeskeydb.bin. This is read-only.
* __`T: TICKET.DB IMAGE / BDRI IMAGE`__: Ticket database files can be mounted and accessed via this drive. This provides easy and quick access to all tickets inside the `ticket.db`. This drive also provides access to other BDRI images, such as the Title database (`title.db`).
Expand Down
49 changes: 42 additions & 7 deletions arm9/source/fatfs/diskio.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@
#include "nand.h"
#include "sdmmc.h"
#include "rtc.h"
#include "vcart.h"


#define FREE_MIN_SECTORS 0x2000 // minimum sectors for the free drive to appear (4MB)

#define FPDRV(pdrv) (((pdrv >= 7) && !imgnand_mode) ? pdrv + 3 : pdrv)
// Volume2Partition resolution table
PARTITION VolToPart[] = {
{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0},
{5, 0}, {6, 0}, {7, 0}, {8, 0}, {9, 0}, {FAT_FLASHCART_VOLUME, 0}
};

STATIC_ASSERT(countof(VolToPart) == FAT_VOLUME_COUNT);

#define FPDRV(pdrv) ((((pdrv) >= FAT_IMAGE_FIRST) && ((pdrv) < FAT_IMAGE_END) && !imgnand_mode) ? \
(pdrv) + FAT_VOLUME_COUNT - FAT_IMAGE_FIRST : (pdrv))
#define PART_INFO(pdrv) (DriveInfo + FPDRV(pdrv))
#define PART_TYPE(pdrv) (DriveInfo[FPDRV(pdrv)].type)

Expand All @@ -29,6 +39,7 @@
#define TYPE_SDCARD (1UL<<4)
#define TYPE_IMAGE (1UL<<5)
#define TYPE_RAMDRV (1UL<<6)
#define TYPE_FLASHCART (1UL<<7)

#define SUBTYPE_CTRN 0
#define SUBTYPE_CTRN_N 1
Expand All @@ -47,7 +58,7 @@ typedef struct {
BYTE keyslot;
} FATpartition;

FATpartition DriveInfo[13] = {
FATpartition DriveInfo[FAT_VOLUME_COUNT + FAT_IMAGE_COUNT] = {
{ TYPE_SDCARD, SUBTYPE_NONE, 0, 0, 0xFF }, // 0 - SDCARD
{ TYPE_SYSNAND, SUBTYPE_CTRN, 0, 0, 0xFF }, // 1 - SYSNAND CTRNAND
{ TYPE_SYSNAND, SUBTYPE_TWLN, 0, 0, 0xFF }, // 2 - SYSNAND TWLN
Expand All @@ -58,9 +69,10 @@ FATpartition DriveInfo[13] = {
{ TYPE_IMGNAND, SUBTYPE_CTRN, 0, 0, 0xFF }, // 7 - IMGNAND CTRNAND
{ TYPE_IMGNAND, SUBTYPE_TWLN, 0, 0, 0xFF }, // 8 - IMGNAND TWLN
{ TYPE_IMGNAND, SUBTYPE_TWLP, 0, 0, 0xFF }, // 9 - IMGNAND TWLP
{ TYPE_IMAGE, SUBTYPE_NONE, 0, 0, 0xFF }, // X - IMAGE
{ TYPE_SYSNAND, SUBTYPE_FREE, 0, 0, 0xFF }, // Y - SYSNAND BONUS
{ TYPE_RAMDRV, SUBTYPE_NONE, 0, 0, 0xFF } // Z - RAMDRIVE
{ TYPE_FLASHCART, SUBTYPE_NONE, 0, 0, 0xFF }, // H - FLASHCART
{ TYPE_IMAGE, SUBTYPE_NONE, 0, 0, 0xFF }, // 7 - IMAGE (without NAND image)
{ TYPE_SYSNAND, SUBTYPE_FREE, 0, 0, 0xFF }, // 8 - SYSNAND BONUS (without NAND image)
{ TYPE_RAMDRV, SUBTYPE_NONE, 0, 0, 0xFF } // 9 - RAMDRIVE (without NAND image)
};

static BYTE imgnand_mode = 0x00;
Expand Down Expand Up @@ -96,6 +108,12 @@ DSTATUS disk_status (
BYTE pdrv /* Physical drive number to identify the drive */
)
{
if ((pdrv == FAT_FLASHCART_VOLUME) && !GetVCartDSpicoSize())
return STA_NOINIT|STA_NODISK;
if ((pdrv >= FAT_IMAGE_FIRST) && (pdrv < FAT_IMAGE_END) &&
((PART_TYPE(pdrv) == TYPE_IMAGE) || (PART_TYPE(pdrv) == TYPE_IMGNAND)) &&
(GetFatDriveIndex(GetMountPath()) == FAT_FLASHCART_VOLUME) && !GetVCartDSpicoSize())
return STA_NOINIT|STA_NODISK;
return RES_OK;
}

Expand Down Expand Up @@ -148,6 +166,10 @@ DSTATUS disk_initialize (
} else if (type == TYPE_RAMDRV) {
InitRamDrive();
fat_info->size = (GetRamDriveSize() + 0x1FF) / 0x200;
} else if (type == TYPE_FLASHCART) {
u64 sectors = GetVCartDSpicoSize() / 0x200;
if (!sectors || (sectors > UINT32_MAX)) return STA_NOINIT|STA_NODISK;
fat_info->size = sectors;
}

return RES_OK;
Expand All @@ -167,6 +189,7 @@ DRESULT disk_read (
UINT count /* Number of sectors to read */
)
{
if (disk_status(pdrv) & STA_NOINIT) return RES_NOTRDY;
BYTE type = PART_TYPE(pdrv);

if (type == TYPE_NONE) {
Expand All @@ -180,6 +203,11 @@ DRESULT disk_read (
} else if (type == TYPE_RAMDRV) {
if (ReadRamDriveSectors(buff, sector, count) != 0)
return RES_ERROR;
} else if (type == TYPE_FLASHCART) {
if (!count || (sector >= PART_INFO(pdrv)->size) || (count > PART_INFO(pdrv)->size - sector))
return RES_PARERR;
if (ReadVCartDSpicoBytes(buff, (u64) sector * 0x200, (u64) count * 0x200) != 0)
return RES_ERROR;
} else {
FATpartition* fat_info = PART_INFO(pdrv);
if (ReadNandSectors(buff, fat_info->offset + sector, count, fat_info->keyslot, type) != 0)
Expand All @@ -204,6 +232,7 @@ DRESULT disk_write (
UINT count /* Number of sectors to write */
)
{
if (disk_status(pdrv) & STA_NOINIT) return RES_NOTRDY;
BYTE type = PART_TYPE(pdrv);

if (type == TYPE_NONE) {
Expand All @@ -217,6 +246,11 @@ DRESULT disk_write (
} else if (type == TYPE_RAMDRV) {
if (WriteRamDriveSectors(buff, sector, count) != 0)
return RES_ERROR;
} else if (type == TYPE_FLASHCART) {
if (!count || (sector >= PART_INFO(pdrv)->size) || (count > PART_INFO(pdrv)->size - sector))
return RES_PARERR;
if (WriteVCartDSpicoBytes(buff, (u64) sector * 0x200, (u64) count * 0x200) != 0)
return RES_ERROR;
} else {
FATpartition* fat_info = PART_INFO(pdrv);
if (WriteNandSectors(buff, fat_info->offset + sector, count, fat_info->keyslot, type) != 0)
Expand All @@ -243,6 +277,7 @@ DRESULT disk_ioctl (
void *buff /* Buffer to send/receive control data */
)
{
if (disk_status(pdrv) & STA_NOINIT) return RES_NOTRDY;
BYTE type = PART_TYPE(pdrv);
FATpartition* fat_info = PART_INFO(pdrv);

Expand All @@ -254,11 +289,11 @@ DRESULT disk_ioctl (
*((DWORD*) buff) = fat_info->size;
return RES_OK;
case GET_BLOCK_SIZE:
*((DWORD*) buff) = ((type == TYPE_IMAGE) || (type == TYPE_RAMDRV)) ? 0x1 : 0x2000;
*((DWORD*) buff) = ((type == TYPE_IMAGE) || (type == TYPE_RAMDRV) || (type == TYPE_FLASHCART)) ? 0x1 : 0x2000;
return RES_OK;
case CTRL_SYNC:
if ((type == TYPE_IMAGE) || (type == TYPE_IMGNAND))
SyncImage();
return (SyncImage() == FR_OK) ? RES_OK : RES_ERROR;
// nothing else to do here - sdmmc.c handles the rest
return RES_OK;
}
Expand Down
4 changes: 3 additions & 1 deletion arm9/source/fatfs/ff.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,9 @@ typedef struct {
/* File/Volume controls */
/*--------------------------------*/

#if FF_VOLUMES < 1 || FF_VOLUMES > 10
/* Local extension: extra volumes require string IDs and no numeric getcwd(). */
#if FF_VOLUMES < 1 || FF_VOLUMES > 255 || \
(FF_VOLUMES > 10 && (FF_STR_VOLUME_ID != 1 || FF_FS_RPATH != 0))
#error Wrong FF_VOLUMES setting
#endif
static FATFS* FatFs[FF_VOLUMES]; /* Pointer to the filesystem objects (logical drives) */
Expand Down
9 changes: 5 additions & 4 deletions arm9/source/fatfs/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,13 @@
/ Drive/Volume Configurations
/---------------------------------------------------------------------------*/

#define FF_VOLUMES 10
/* Number of volumes (logical drives) to be used. (1-10) */
#include "volume.h"
#define FF_VOLUMES FAT_VOLUME_COUNT
/* Volumes above 9 use string IDs; see the local guard in ff.c. */


#define FF_STR_VOLUME_ID 0
#define FF_VOLUME_STRS "sdcard","sysnand","systwln","systwlp","emunand","emutwln","emutwlp","imgnand","imgtwln","imgtwlp"
#define FF_STR_VOLUME_ID 1
#define FF_VOLUME_STRS FAT_VOLUME_STRS
/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
/ When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
/ number in the path name. FF_VOLUME_STRS defines the volume ID strings for each
Expand Down
19 changes: 19 additions & 0 deletions arm9/source/fatfs/volume.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "volume.h"
#include <stddef.h>

static const char* const drive_paths[FAT_VOLUME_COUNT] = {
"0:", "1:", "2:", "3:", "4:", "5:", "6:", "7:", "8:", "9:", FLASHCART_DRIVE
};

const char* GetFatDrivePath(unsigned int volume) {
return (volume < FAT_VOLUME_COUNT) ? drive_paths[volume] : NULL;
}

int GetFatDriveIndex(const char* path) {
if (!path || !path[0] || (path[1] != ':')) return -1;
char name = path[0];
if ((name >= 'a') && (name <= 'z')) name -= 'a' - 'A';
for (unsigned int i = 0; i < FAT_VOLUME_COUNT; i++)
if (name == drive_paths[i][0]) return i;
return -1;
}
14 changes: 14 additions & 0 deletions arm9/source/fatfs/volume.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#define FAT_VOLUME_COUNT 11
#define FAT_IMAGE_FIRST 7
#define FAT_IMAGE_COUNT 3
#define FAT_IMAGE_END (FAT_IMAGE_FIRST + FAT_IMAGE_COUNT)
#define FAT_RAM_VOLUME 9
#define FAT_FLASHCART_VOLUME 10

#define FLASHCART_DRIVE "H:"
#define FAT_VOLUME_STRS "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "H"

const char* GetFatDrivePath(unsigned int volume);
int GetFatDriveIndex(const char* path);
19 changes: 14 additions & 5 deletions arm9/source/filesys/fsdrive.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ int DriveType(const char* path) {
} else if ((pdrv >= 0) && (pdrv < NORM_FS)) {
if (pdrv == 0) {
type = DRV_FAT | DRV_SDCARD | DRV_STDFAT;
} else if (pdrv == FAT_FLASHCART_VOLUME) {
type = DRV_FAT | DRV_FLASHCART | DRV_STDFAT;
} else if ((pdrv == 8) && !(GetMountState() & IMG_NAND)) {
type = DRV_FAT | DRV_SYSNAND | DRV_BONUS | DRV_STDFAT;
} else if ((pdrv == 9) && !(GetMountState() & IMG_NAND)) {
Expand All @@ -39,7 +41,7 @@ int DriveType(const char* path) {
type = DRV_FAT | DRV_EMUNAND | DRV_CTRNAND | DRV_STDFAT;
} else if ((pdrv == 5) || (pdrv == 6)) {
type = DRV_FAT | DRV_EMUNAND | DRV_TWLNAND | DRV_STDFAT;
} else if ((pdrv >= 7) && (pdrv <= 9) &&
} else if ((pdrv >= FAT_IMAGE_FIRST) && (pdrv < FAT_IMAGE_END) &&
(GetMountState() & (IMG_FAT|IMG_NAND))) {
type = DRV_FAT | DRV_IMAGE | DRV_STDFAT;
}
Expand Down Expand Up @@ -76,6 +78,10 @@ void SetFSSearch(const char* pattern, const char* path) {
} else *search_pattern = *search_path = '\0';
}

const char* GetFSSearchPath(void) {
return search_path;
}

void SetTitleManagerMode(bool mode) {
title_manager_mode = mode;
}
Expand Down Expand Up @@ -104,7 +110,12 @@ bool GetRootDirContentsWorker(DirStruct* contents) {
entry->name = entry->path + entry->p_name;
memset(entry->path, 0x00, 256);
snprintf(entry->path, 4, "%s", drvnum[i]);
if ((*(drvnum[i]) >= '7') && (*(drvnum[i]) <= '9') && !(GetMountState() & IMG_NAND)) // Drive 7...9 handling
if (GetFatDriveIndex(drvnum[i]) == FAT_FLASHCART_VOLUME) {
char dslabel[DRV_LABEL_LEN];
if (!GetFATVolumeLabel(FLASHCART_DRIVE, dslabel) || !(*dslabel))
strcpy(dslabel, STR_LAB_NOLABEL);
snprintf(entry->name, 252, "[%s] %s (%s)", drvnum[i], drvname[i], dslabel);
} else if ((*(drvnum[i]) >= '7') && (*(drvnum[i]) <= '9') && !(GetMountState() & IMG_NAND)) // Drive 7...9 handling
snprintf(entry->name, 252, "[%s] %s", drvnum[i],
(*(drvnum[i]) == '7') ? STR_LAB_FAT_IMAGE :
(*(drvnum[i]) == '8') ? STR_LAB_BONUS_DRIVE :
Expand Down Expand Up @@ -231,13 +242,11 @@ uint64_t GetFreeSpace(const char* path)
{
DWORD free_clusters;
FATFS* fsptr;
char fsname[4] = { '\0' };
int pdrv = GetMountedFSNum(path);
FATFS* fsobj = GetMountedFSObject(path);
if ((pdrv < 0) || !fsobj) return 0;

snprintf(fsname, sizeof(fsname), "%i:", pdrv);
if (f_getfree(fsname, &free_clusters, &fsptr) != FR_OK)
if (f_getfree(GetFatDrivePath(pdrv), &free_clusters, &fsptr) != FR_OK)
return 0;

return (uint64_t) free_clusters * fsobj->csize * FF_MAX_SS;
Expand Down
12 changes: 8 additions & 4 deletions arm9/source/filesys/fsdrive.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#include "common.h"
#include "fsdir.h"
#include "volume.h"

#define NORM_FS 10
#define IMGN_FS 3 // image normal filesystems
#define NORM_FS FAT_VOLUME_COUNT
#define IMGN_FS FAT_IMAGE_COUNT // image normal filesystems

// primary drive types
#define DRV_UNKNOWN (0<<0)
Expand All @@ -28,6 +29,7 @@
#define DRV_TITLEMAN (1UL<<16)
#define DRV_SEARCH (1UL<<17)
#define DRV_STDFAT (1UL<<18) // standard FAT drive without limitations
#define DRV_FLASHCART (1UL<<19)

#define DRV_LABEL_LEN (36)

Expand All @@ -41,17 +43,19 @@
STR_LAB_MEMORY_VIRTUAL, \
STR_LAB_VRAM_VIRTUAL, \
STR_LAB_TITLE_MANAGER, \
STR_LAB_LAST_SEARCH
STR_LAB_LAST_SEARCH, \
"DSPICO SD"

#define FS_DRVNUM \
"0:", "1:", "2:", "3:", "A:", "S:", "4:", "5:", "6:", "B:", "E:", "7:", "8:", "9:", \
"I:", "C:", "G:", "K:", "T:", "D:", "F:", "M:", "V:", "Y:", "Z:"
"I:", "C:", "G:", "K:", "T:", "D:", "F:", "M:", "V:", "Y:", "Z:", FLASHCART_DRIVE

/** Function to identify the type of a drive **/
int DriveType(const char* path);

/** Set search pattern / path / mode for special Z: drive **/
void SetFSSearch(const char* pattern, const char* path);
const char* GetFSSearchPath(void);

/** Enable title manager for special processing of mounted title.db **/
void SetTitleManagerMode(bool mode);
Expand Down
Loading