mirror of
https://github.com/d0k3/GodMode9.git
synced 2025-06-26 13:42:47 +00:00
Allow listing virtual files
This commit is contained in:
parent
ba61f77ee0
commit
ad995919cf
75
source/fs.c
75
source/fs.c
@ -1,5 +1,6 @@
|
|||||||
#include "draw.h"
|
#include "draw.h"
|
||||||
#include "fs.h"
|
#include "fs.h"
|
||||||
|
#include "virtual.h"
|
||||||
#include "fatfs/ff.h"
|
#include "fatfs/ff.h"
|
||||||
|
|
||||||
#define MAX_FS 7
|
#define MAX_FS 7
|
||||||
@ -55,7 +56,7 @@ void DeinitFS() {
|
|||||||
int PathToNumFS(const char* path) {
|
int PathToNumFS(const char* path) {
|
||||||
int fsnum = *path - (int) '0';
|
int fsnum = *path - (int) '0';
|
||||||
if ((fsnum < 0) || (fsnum >= MAX_FS) || (path[1] != ':')) {
|
if ((fsnum < 0) || (fsnum >= MAX_FS) || (path[1] != ':')) {
|
||||||
ShowPrompt(false, "Invalid path");
|
if (!IsVirtualPath(path)) ShowPrompt(false, "Invalid path (%s)", path);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return fsnum;
|
return fsnum;
|
||||||
@ -63,7 +64,11 @@ int PathToNumFS(const char* path) {
|
|||||||
|
|
||||||
bool CheckWritePermissions(const char* path) {
|
bool CheckWritePermissions(const char* path) {
|
||||||
int pdrv = PathToNumFS(path);
|
int pdrv = PathToNumFS(path);
|
||||||
if (pdrv < 0) return false;
|
if (pdrv < 0) {
|
||||||
|
if (IsVirtualPath(path)) // this is a hack, but okay for now
|
||||||
|
pdrv = (*path == 'S') ? 1 : 4;
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
|
||||||
if ((pdrv >= 1) && (pdrv <= 3) && (write_permission_level < 3)) {
|
if ((pdrv >= 1) && (pdrv <= 3) && (write_permission_level < 3)) {
|
||||||
if (ShowPrompt(true, "Writing to the SysNAND is locked!\nUnlock it now?"))
|
if (ShowPrompt(true, "Writing to the SysNAND is locked!\nUnlock it now?"))
|
||||||
@ -375,22 +380,30 @@ void SortDirStruct(DirStruct* contents) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool GetRootDirContentsWorker(DirStruct* contents) {
|
bool GetRootDirContentsWorker(DirStruct* contents) {
|
||||||
static const char* drvname[16] = {
|
static const char* drvname[] = {
|
||||||
"SDCARD",
|
"SDCARD",
|
||||||
"SYSNAND CTRNAND", "SYSNAND TWLN", "SYSNAND TWLP",
|
"SYSNAND CTRNAND", "SYSNAND TWLN", "SYSNAND TWLP",
|
||||||
"EMUNAND CTRNAND", "EMUNAND TWLN", "EMUNAND TWLP"
|
"EMUNAND CTRNAND", "EMUNAND TWLN", "EMUNAND TWLP",
|
||||||
|
"SYSNAND VIRTUAL", "EMUNAND VIRTUAL"
|
||||||
|
};
|
||||||
|
static const char* drvnum[] = {
|
||||||
|
"0", "1", "2", "3", "4", "5", "6", "S", "E"
|
||||||
};
|
};
|
||||||
u32 n_entries = 0;
|
u32 n_entries = 0;
|
||||||
|
|
||||||
for (u32 pdrv = 0; (pdrv < MAX_FS) && (pdrv < MAX_ENTRIES); pdrv++) {
|
// virtual root objects hacked in
|
||||||
if (!fs_mounted[pdrv]) continue;
|
for (u32 pdrv = 0; (pdrv < MAX_FS+2) && (n_entries < MAX_ENTRIES); pdrv++) {
|
||||||
memset(contents->entry[n_entries].path, 0x00, 16);
|
DirEntry* entry = &(contents->entry[n_entries]);
|
||||||
snprintf(contents->entry[n_entries].path + 0, 4, "%lu:", pdrv);
|
if ((pdrv < MAX_FS) && !fs_mounted[pdrv]) continue;
|
||||||
snprintf(contents->entry[n_entries].path + 4, 32, "[%lu:] %s", pdrv, drvname[pdrv]);
|
if ((pdrv == MAX_FS+0) && (!fs_mounted[1] || !fs_mounted[2] || !fs_mounted[3])) continue;
|
||||||
contents->entry[n_entries].name = contents->entry[n_entries].path + 4;
|
if ((pdrv == MAX_FS+1) && (!fs_mounted[4] || !fs_mounted[5] || !fs_mounted[6])) continue;
|
||||||
contents->entry[n_entries].size = GetTotalSpace(contents->entry[n_entries].path);
|
memset(entry->path, 0x00, 64);
|
||||||
contents->entry[n_entries].type = T_ROOT;
|
snprintf(entry->path + 0, 4, "%s:", drvnum[pdrv]);
|
||||||
contents->entry[n_entries].marked = 0;
|
snprintf(entry->path + 4, 32, "[%s:] %s", drvnum[pdrv], drvname[pdrv]);
|
||||||
|
entry->name = entry->path + 4;
|
||||||
|
entry->size = GetTotalSpace(entry->path);
|
||||||
|
entry->type = T_ROOT;
|
||||||
|
entry->marked = 0;
|
||||||
n_entries++;
|
n_entries++;
|
||||||
}
|
}
|
||||||
contents->n_entries = n_entries;
|
contents->n_entries = n_entries;
|
||||||
@ -398,6 +411,23 @@ bool GetRootDirContentsWorker(DirStruct* contents) {
|
|||||||
return contents->n_entries;
|
return contents->n_entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GetVirtualDirContentsWorker(DirStruct* contents, const char* path) {
|
||||||
|
if (strchr(path, '/')) return false; // only top level paths
|
||||||
|
for (u32 n = 0; (n < virtualFileList_size) && (contents->n_entries < MAX_ENTRIES); n++) {
|
||||||
|
VirtualFile vfile;
|
||||||
|
DirEntry* entry = &(contents->entry[contents->n_entries]);
|
||||||
|
snprintf(entry->path, 256, "%s/%s", path, virtualFileList[n]);
|
||||||
|
if (!FindVirtualFile(&vfile, entry->path)) continue;
|
||||||
|
entry->name = entry->path + strnlen(path, 256) + 1;
|
||||||
|
entry->size = vfile.size;
|
||||||
|
entry->type = T_FILE;
|
||||||
|
entry->marked = 0;
|
||||||
|
contents->n_entries++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true; // not much we can check here
|
||||||
|
}
|
||||||
|
|
||||||
bool GetDirContentsWorker(DirStruct* contents, char* fpath, int fsize, bool recursive) {
|
bool GetDirContentsWorker(DirStruct* contents, char* fpath, int fsize, bool recursive) {
|
||||||
DIR pdir;
|
DIR pdir;
|
||||||
FILINFO fno;
|
FILINFO fno;
|
||||||
@ -450,8 +480,6 @@ void GetDirContents(DirStruct* contents, const char* path) {
|
|||||||
if (!GetRootDirContentsWorker(contents))
|
if (!GetRootDirContentsWorker(contents))
|
||||||
contents->n_entries = 0; // not required, but so what?
|
contents->n_entries = 0; // not required, but so what?
|
||||||
} else {
|
} else {
|
||||||
char fpath[256]; // 256 is the maximum length of a full path
|
|
||||||
strncpy(fpath, path, 256);
|
|
||||||
// create virtual '..' entry
|
// create virtual '..' entry
|
||||||
contents->entry->name = contents->entry->path + 8;
|
contents->entry->name = contents->entry->path + 8;
|
||||||
strncpy(contents->entry->path, "*?*?*", 8);
|
strncpy(contents->entry->path, "*?*?*", 8);
|
||||||
@ -459,8 +487,15 @@ void GetDirContents(DirStruct* contents, const char* path) {
|
|||||||
contents->entry->type = T_DOTDOT;
|
contents->entry->type = T_DOTDOT;
|
||||||
contents->entry->size = 0;
|
contents->entry->size = 0;
|
||||||
contents->n_entries = 1;
|
contents->n_entries = 1;
|
||||||
if (!GetDirContentsWorker(contents, fpath, 256, false))
|
if (IsVirtualPath(path)) {
|
||||||
contents->n_entries = 0;
|
if (!GetVirtualDirContentsWorker(contents, path))
|
||||||
|
contents->n_entries = 0;
|
||||||
|
} else {
|
||||||
|
char fpath[256]; // 256 is the maximum length of a full path
|
||||||
|
strncpy(fpath, path, 256);
|
||||||
|
if (!GetDirContentsWorker(contents, fpath, 256, false))
|
||||||
|
contents->n_entries = 0;
|
||||||
|
}
|
||||||
SortDirStruct(contents);
|
SortDirStruct(contents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -471,11 +506,11 @@ uint64_t GetFreeSpace(const char* path)
|
|||||||
FATFS *fs_ptr;
|
FATFS *fs_ptr;
|
||||||
char fsname[4] = { '\0' };
|
char fsname[4] = { '\0' };
|
||||||
int pdrv = PathToNumFS(path);
|
int pdrv = PathToNumFS(path);
|
||||||
if (pdrv < 0) return -1;
|
if (pdrv < 0) return 0;
|
||||||
|
|
||||||
snprintf(fsname, 3, "%i:", pdrv);
|
snprintf(fsname, 3, "%i:", pdrv);
|
||||||
if (f_getfree(fsname, &free_clusters, &fs_ptr) != FR_OK)
|
if (f_getfree(fsname, &free_clusters, &fs_ptr) != FR_OK)
|
||||||
return -1;
|
return 0;
|
||||||
|
|
||||||
return (uint64_t) free_clusters * fs[pdrv].csize * _MAX_SS;
|
return (uint64_t) free_clusters * fs[pdrv].csize * _MAX_SS;
|
||||||
}
|
}
|
||||||
@ -483,7 +518,7 @@ uint64_t GetFreeSpace(const char* path)
|
|||||||
uint64_t GetTotalSpace(const char* path)
|
uint64_t GetTotalSpace(const char* path)
|
||||||
{
|
{
|
||||||
int pdrv = PathToNumFS(path);
|
int pdrv = PathToNumFS(path);
|
||||||
if (pdrv < 0) return -1;
|
if (pdrv < 0) return 0;
|
||||||
|
|
||||||
return (uint64_t) (fs[pdrv].n_fatent - 2) * fs[pdrv].csize * _MAX_SS;
|
return (uint64_t) (fs[pdrv].n_fatent - 2) * fs[pdrv].csize * _MAX_SS;
|
||||||
}
|
}
|
||||||
|
80
source/nand/virtual.c
Normal file
80
source/nand/virtual.c
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#include "virtual.h"
|
||||||
|
#include "sdmmc.h"
|
||||||
|
|
||||||
|
#define VFLAG_ON_O3DS NAND_TYPE_O3DS
|
||||||
|
#define VFLAG_ON_N3DS NAND_TYPE_N3DS
|
||||||
|
#define VFLAG_ON_NO3DS NAND_TYPE_NO3DS
|
||||||
|
#define VFLAG_ON_ALL (VFLAG_ON_O3DS | VFLAG_ON_N3DS | VFLAG_ON_NO3DS)
|
||||||
|
#define VFLAG_NAND_SIZE (1<<29)
|
||||||
|
#define VFLAG_ON_EMUNAND (1<<30)
|
||||||
|
|
||||||
|
VirtualFile virtualFileTemplates[] = {
|
||||||
|
{ "twln.bin" , 0x00012E00, 0x08FB5200, 0x03, VFLAG_ON_ALL | VFLAG_EXT_NAND_REMOUNT },
|
||||||
|
{ "twlp.bin" , 0x09011A00, 0x020B6600, 0x03, VFLAG_ON_ALL | VFLAG_EXT_NAND_REMOUNT },
|
||||||
|
{ "agbsave.bin" , 0x0B100000, 0x00030000, 0x07, VFLAG_ON_ALL },
|
||||||
|
{ "firm0.bin" , 0x0B130000, 0x00400000, 0x06, VFLAG_ON_ALL },
|
||||||
|
{ "firm1.bin" , 0x0B530000, 0x00400000, 0x06, VFLAG_ON_ALL },
|
||||||
|
{ "ctrnand_fat.bin" , 0x0B95CA00, 0x2F3E3600, 0x04, VFLAG_ON_O3DS | VFLAG_EXT_NAND_REMOUNT },
|
||||||
|
{ "ctrnand_fat.bin" , 0x0B95AE00, 0x41D2D200, 0x05, VFLAG_ON_N3DS | VFLAG_EXT_NAND_REMOUNT },
|
||||||
|
{ "ctrnand_fat.bin" , 0x0B95AE00, 0x41D2D200, 0x04, VFLAG_ON_NO3DS | VFLAG_EXT_NAND_REMOUNT },
|
||||||
|
{ "ctrnand_full.bin" , 0x0B930000, 0x2F5D0000, 0x04, VFLAG_ON_O3DS | VFLAG_EXT_NAND_REMOUNT },
|
||||||
|
{ "ctrnand_full.bin" , 0x0B930000, 0x41ED0000, 0x05, VFLAG_ON_N3DS | VFLAG_EXT_NAND_REMOUNT },
|
||||||
|
{ "ctrnand_full.bin" , 0x0B930000, 0x41ED0000, 0x04, VFLAG_ON_NO3DS | VFLAG_EXT_NAND_REMOUNT },
|
||||||
|
{ "nand.bin" , 0x00000000, 0x00000000, 0xFF, VFLAG_ON_ALL | VFLAG_NAND_SIZE | VFLAG_EXT_NAND_REMOUNT },
|
||||||
|
{ "nand_minsize.bin" , 0x00000000, 0x3AF00000, 0xFF, VFLAG_ON_O3DS | VFLAG_EXT_NAND_REMOUNT },
|
||||||
|
{ "nand_minsize.bin" , 0x00000000, 0x4D800000, 0xFF, VFLAG_ON_N3DS | VFLAG_ON_NO3DS | VFLAG_EXT_NAND_REMOUNT },
|
||||||
|
{ "nand_hdr.bin" , 0x00000000, 0x00000200, 0xFF, VFLAG_ON_ALL },
|
||||||
|
{ "sector0x96.bin" , 0x00012C00, 0x00000200, 0xFF, VFLAG_ON_ALL }
|
||||||
|
};
|
||||||
|
|
||||||
|
bool IsVirtualPath(const char* path) {
|
||||||
|
return (strncmp(path, "S:", 2) == 0) || (strncmp(path, "E:", 2) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FindVirtualFile(VirtualFile* vfile, const char* path)
|
||||||
|
{
|
||||||
|
char* fname = strchr(path, '/');
|
||||||
|
bool on_emunand = (*path == 'E');
|
||||||
|
u8 nand_type = CheckNandType(on_emunand);
|
||||||
|
|
||||||
|
// fix the name
|
||||||
|
if (!fname) return false;
|
||||||
|
fname++;
|
||||||
|
|
||||||
|
// more safety checks
|
||||||
|
if (!IsVirtualPath(path) || (fname - path != 3))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// parse the template list, get the correct one
|
||||||
|
u32 n_templates = sizeof(virtualFileTemplates) / sizeof(VirtualFile);
|
||||||
|
VirtualFile* curr_template = NULL;
|
||||||
|
for (u32 i = 0; i < n_templates; i++) {
|
||||||
|
curr_template = &virtualFileTemplates[i];
|
||||||
|
if ((curr_template->flags & nand_type) && (strncmp(fname, curr_template->name, 32) == 0))
|
||||||
|
break;
|
||||||
|
curr_template = NULL;
|
||||||
|
}
|
||||||
|
if (!curr_template) return false;
|
||||||
|
|
||||||
|
// copy current template to vfile
|
||||||
|
memcpy(vfile, curr_template, sizeof(VirtualFile));
|
||||||
|
|
||||||
|
// process special flags
|
||||||
|
if (vfile->flags & VFLAG_NAND_SIZE)
|
||||||
|
vfile->size = getMMCDevice(0)->total_size * 0x200;
|
||||||
|
if (on_emunand) vfile->flags |= VFLAG_ON_EMUNAND;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ReadVirtualFile(const VirtualFile* vfile, u8* buffer, u32 offset, u32 count)
|
||||||
|
{
|
||||||
|
// simple wrapper function for ReadNandSectors(u8* buffer, u32 sector, u32 count, u32 keyslot, bool read_emunand)
|
||||||
|
return ReadNandSectors(buffer, (vfile->offset + offset) / 0x200, (count+0x1FF) / 0x200, vfile->keyslot, vfile->flags & VFLAG_ON_EMUNAND);
|
||||||
|
}
|
||||||
|
|
||||||
|
int WriteVirtualFile(const VirtualFile* vfile, const u8* buffer, u32 offset, u32 count)
|
||||||
|
{
|
||||||
|
// simple wrapper function for WriteNandSectors(const u8* buffer, u32 sector, u32 count, u32 keyslot, bool write_emunand)
|
||||||
|
return WriteNandSectors(buffer, (vfile->offset + offset) / 0x200, (count+0x1FF) / 0x200, vfile->keyslot, vfile->flags & VFLAG_ON_EMUNAND);
|
||||||
|
}
|
25
source/nand/virtual.h
Normal file
25
source/nand/virtual.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
#include "nand.h"
|
||||||
|
|
||||||
|
#define VFLAG_EXT_NAND_REMOUNT (1<<31)
|
||||||
|
|
||||||
|
static const char* virtualFileList[] = { // must have a match in virtualFileTemplates[]
|
||||||
|
"twln.bin", "twlp.bin", "agbsave.bin", "firm0.bin", "firm1.bin", "ctrnand_fat.bin", "ctrnand_full.bin",
|
||||||
|
"nand.bin", "nand_minsize.bin", "nand_hdr.bin", "sector0x96.bin"
|
||||||
|
};
|
||||||
|
static const u32 virtualFileList_size = sizeof(virtualFileList) / sizeof(char*);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char name[32];
|
||||||
|
u32 offset; // must be a multiple of 0x200
|
||||||
|
u32 size;
|
||||||
|
u32 keyslot;
|
||||||
|
u32 flags;
|
||||||
|
} __attribute__((packed)) VirtualFile;
|
||||||
|
|
||||||
|
bool IsVirtualPath(const char* path);
|
||||||
|
bool FindVirtualFile(VirtualFile* vfile, const char* path);
|
||||||
|
int ReadVirtualFile(const VirtualFile* vfile, u8* buffer, u32 offset, u32 count);
|
||||||
|
int WriteVirtualFile(const VirtualFile* vfile, const u8* buffer, u32 offset, u32 count);
|
Loading…
x
Reference in New Issue
Block a user