Skip to content

Commit

Permalink
Implement patch offset caching
Browse files Browse the repository at this point in the history
Card read and other offsets not cached yet
  • Loading branch information
RocketRobz committed Apr 27, 2019
1 parent 5aab814 commit b6bc3b0
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 67 deletions.
9 changes: 9 additions & 0 deletions retail/arm9/include/conf_sd.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#ifndef CONF_SD_H
#define CONF_SD_H

#ifdef __cplusplus
extern "C"
{
#endif

int loadFromSD(configuration* conf, const char *bootstrapPath);

#ifdef __cplusplus
}
#endif

#endif // CONF_SD_H
11 changes: 10 additions & 1 deletion retail/arm9/include/nds_loader_arm9.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@

#include "configuration.h"

#ifdef __cplusplus
extern "C"
{
#endif

//#define LOAD_DEFAULT_NDS 0

void runNds(const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, configuration* conf);
void runNds(const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, u32 patchOffsetCacheCluster, configuration* conf);

#ifdef __cplusplus
}
#endif

#endif // NDS_LOADER_ARM9_H
32 changes: 27 additions & 5 deletions retail/arm9/source/conf_sd.c → retail/arm9/source/conf_sd.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include <stdlib.h> // strtol
#include <unistd.h>
//#include <stdio.h>
//#include <nds.h>
#include <nds.h>
#include <string>
#include <string.h>
#include <limits.h> // PATH_MAX
#include <nds/ndstypes.h>
/*#include <nds/ndstypes.h>
#include <nds/fifocommon.h>
#include <nds/arm9/console.h>
#include <nds/debug.h>
#include <nds/debug.h>*/
#include <fat.h>
#include "minIni.h"
#include "hex.h"
Expand All @@ -30,6 +32,8 @@ static off_t getSaveSize(const char* path) {
return fsize;
}

extern std::string ReplaceAll(std::string str, const std::string& from, const std::string& to);

static inline bool match(const char* section, const char* s, const char* key, const char* k) {
return (strcmp(section, s) == 0 && strcmp(key, k) == 0);
}
Expand Down Expand Up @@ -140,7 +144,7 @@ static int callback(const char *section, const char *key, const char *value, voi

} else if (match(section, "NDS-BOOTSTRAP", key, "CHEAT_DATA")) {
// Cheat data
conf->cheat_data = malloc(CHEAT_DATA_MAX_SIZE);
conf->cheat_data = (u32*)malloc(CHEAT_DATA_MAX_SIZE);
conf->cheat_data_len = 0;
char* str = strdup(value);
char* cheat = strtok(str, " ");
Expand Down Expand Up @@ -232,6 +236,8 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
nocashMessage("fatInitDefault");

ini_browse(callback, conf, "sd:/_nds/nds-bootstrap.ini");
mkdir("sd:/_nds/nds-bootstrap", 0777);
mkdir("sd:/_nds/nds-bootstrap/patchOffsetCache", 0777);

nitroFSInit(bootstrapPath);

Expand Down Expand Up @@ -281,7 +287,7 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
conf->saveSize = getSaveSize(conf->savPath);

conf->argc = 0;
conf->argv = malloc(ARG_MAX);
conf->argv = (const char**)malloc(ARG_MAX);
if (strcasecmp(conf->ndsPath + strlen(conf->ndsPath) - 5, ".argv") == 0) {
FILE* argfile = fopen(conf->ndsPath, "rb");

Expand Down Expand Up @@ -314,6 +320,22 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
conf->argc = 1; //++conf->argc;
}
realloc(conf->argv, conf->argc*sizeof(const char*));

std::string romFilename = ReplaceAll(conf->ndsPath, ".nds", ".bin");
const size_t last_slash_idx = romFilename.find_last_of("/");
if (std::string::npos != last_slash_idx)
{
romFilename.erase(0, last_slash_idx + 1);
}

std::string patchOffsetCacheFilePath = "sd:/_nds/nds-bootstrap/patchOffsetCache/"+romFilename;

if (access(patchOffsetCacheFilePath.c_str(), F_OK) != 0) {
FILE* patchOffsetCacheFile = fopen(patchOffsetCacheFilePath.c_str(), "wb");
char buffer[0x200] = {0};
fwrite(buffer, 1, sizeof(buffer), patchOffsetCacheFile);
fclose(patchOffsetCacheFile);
}

return 0;
}
34 changes: 30 additions & 4 deletions retail/arm9/source/main.c → retail/arm9/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <stdarg.h>
#include <limits.h> // PATH_MAX
#include <unistd.h>
#include <sys/stat.h>
#include <nds/ndstypes.h>
#include <nds.h>
/*#include <nds/ndstypes.h>
#include <nds/arm9/input.h>
#include <nds/fifocommon.h>
#include <nds/arm9/console.h>
#include <nds/system.h>
#include <nds/debug.h>
#include <nds/debug.h>*/

#include "configuration.h"
#include "nds_loader_arm9.h"
Expand Down Expand Up @@ -88,6 +90,15 @@ static void dopause(void) {
scanKeys();
}

std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;
}

static void getSFCG_ARM9(void) {
printf("SCFG_ROM ARM9 %X\n", REG_SCFG_ROM);
printf("SCFG_CLK ARM9 %X\n", REG_SCFG_CLK);
Expand Down Expand Up @@ -334,7 +345,9 @@ static int runNdsFile(configuration* conf) {

struct stat st;
struct stat stSav;
struct stat stPatchOffsetCache;
u32 clusterSav = 0;
u32 clusterPatchOffsetCache = 0;
char filePath[PATH_MAX];
int pathLen;
//const char* args[1];
Expand All @@ -347,6 +360,19 @@ static int runNdsFile(configuration* conf) {
clusterSav = stSav.st_ino;
}

std::string romFilename = ReplaceAll(conf->ndsPath, ".nds", ".bin");
const size_t last_slash_idx = romFilename.find_last_of("/");
if (std::string::npos != last_slash_idx)
{
romFilename.erase(0, last_slash_idx + 1);
}

std::string patchOffsetCacheFilePath = "sd:/_nds/nds-bootstrap/patchOffsetCache/"+romFilename;

if (stat(patchOffsetCacheFilePath.c_str(), &stPatchOffsetCache) >= 0) {
clusterPatchOffsetCache = stPatchOffsetCache.st_ino;
}

if (conf->argc <= 0 || !conf->argv) {
// Construct a command line if we weren't supplied with one
if (!getcwd(filePath, PATH_MAX)) {
Expand All @@ -364,13 +390,13 @@ static int runNdsFile(configuration* conf) {
//bool havedsiSD = false;
//bool havedsiSD = (argv[0][0] == 's' && argv[0][1] == 'd');

runNds(load_bin, load_bin_size, st.st_ino, clusterSav, conf);
runNds(load_bin, load_bin_size, st.st_ino, clusterSav, clusterPatchOffsetCache, conf);

return 0;
}

int main(int argc, char** argv) {
configuration* conf = malloc(sizeof(configuration));
configuration* conf = (configuration*)malloc(sizeof(configuration));
conf->initDisc = true;
conf->dldiPatchNds = true;

Expand Down
47 changes: 24 additions & 23 deletions retail/arm9/source/nds_loader_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ int loadCheatData(u32* cheat_data, u32 cheat_data_len) {
return true;
}

void runNds(const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, configuration* conf) {
void runNds(const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, u32 patchOffsetCacheCluster, configuration* conf) {
nocashMessage("runNds");

irqDisable(IRQ_ALL);
Expand All @@ -225,28 +225,29 @@ void runNds(const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, co
}
free(conf->argv);

lc0->saveFileCluster = saveCluster;
lc0->saveSize = conf->saveSize;
lc0->language = conf->language;
lc0->dsiMode = conf->dsiMode; // SDK 5
lc0->donorSdkVer = conf->donorSdkVer;
lc0->patchMpuRegion = conf->patchMpuRegion;
lc0->patchMpuSize = conf->patchMpuSize;
lc0->ceCached = conf->ceCached; // SDK 1-4
lc0->consoleModel = conf->consoleModel;
lc0->loadingScreen = conf->loadingScreen;
lc0->loadingDarkTheme = conf->loadingDarkTheme;
lc0->loadingSwapLcds = conf->loadingSwapLcds;
lc0->loadingFrames = conf->loadingFrames;
lc0->loadingFps = conf->loadingFps;
lc0->loadingBar = conf->loadingBar;
lc0->loadingBarYpos = conf->loadingBarYpos;
lc0->romread_LED = conf->romread_LED;
lc0->boostVram = conf->boostVram;
lc0->gameSoftReset = conf->gameSoftReset;
lc0->forceSleepPatch = conf->forceSleepPatch;
lc0->preciseVolumeControl = conf->preciseVolumeControl;
lc0->logging = conf->logging;
lc0->saveFileCluster = saveCluster;
lc0->saveSize = conf->saveSize;
lc0->patchOffsetCacheFileCluster = patchOffsetCacheCluster;
lc0->language = conf->language;
lc0->dsiMode = conf->dsiMode; // SDK 5
lc0->donorSdkVer = conf->donorSdkVer;
lc0->patchMpuRegion = conf->patchMpuRegion;
lc0->patchMpuSize = conf->patchMpuSize;
lc0->ceCached = conf->ceCached; // SDK 1-4
lc0->consoleModel = conf->consoleModel;
lc0->loadingScreen = conf->loadingScreen;
lc0->loadingDarkTheme = conf->loadingDarkTheme;
lc0->loadingSwapLcds = conf->loadingSwapLcds;
lc0->loadingFrames = conf->loadingFrames;
lc0->loadingFps = conf->loadingFps;
lc0->loadingBar = conf->loadingBar;
lc0->loadingBarYpos = conf->loadingBarYpos;
lc0->romread_LED = conf->romread_LED;
lc0->boostVram = conf->boostVram;
lc0->gameSoftReset = conf->gameSoftReset;
lc0->forceSleepPatch = conf->forceSleepPatch;
lc0->preciseVolumeControl = conf->preciseVolumeControl;
lc0->logging = conf->logging;

loadCheatData(conf->cheat_data, conf->cheat_data_len);
free(conf->cheat_data);
Expand Down
22 changes: 22 additions & 0 deletions retail/bootloader/include/patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@

//extern bool cardReadFound; // patch_arm9.c

typedef struct patchOffsetCacheContents {
u32 ver;
u32* moduleParamsOffset;
u32 a9IsThumb;
u32* cardReadOffset;
u32* cardPullOutOffset;
u32* cacheFlushOffset;
u32* cardIdOffset;
u32* cardReadDmaOffset;
u32* sleepOffset;
u32* randomPatchOffset;
u32* randomPatchSecondOffset;
u32 a7IsThumb;
u32* swi12Offset;
u32* swiGetPitchTableOffset;
u32* sleepPatchOffset;
u32* a7IrqHandlerOffset;
} patchOffsetCacheContents;

extern u32 patchOffsetCacheFileVersion;
extern patchOffsetCacheContents patchOffsetCache;

u32 generateA7Instr(int arg1, int arg2);
const u16* generateA7InstrThumb(int arg1, int arg2);
void patchBinary(const tNDSHeader* ndsHeader);
Expand Down
26 changes: 19 additions & 7 deletions retail/bootloader/source/arm7/find_arm9.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stddef.h> // NULL
#include "patch.h"
#include "nds_header.h"
#include "find.h"
#include "debug_file.h"
Expand Down Expand Up @@ -117,14 +118,25 @@ static const u16 initHeapEndFuncSignatureThumb[1] = {0xBD08};
u32* findModuleParamsOffset(const tNDSHeader* ndsHeader) {
dbg_printf("findModuleParamsOffset:\n");

u32* moduleParamsOffset = findOffset(
(u32*)ndsHeader->arm9destination, ndsHeader->arm9binarySize,
moduleParamsSignature, 2
);
if (moduleParamsOffset) {
dbg_printf("Module params offset found: ");
u32* moduleParamsOffset = 0;
if (patchOffsetCache.ver != patchOffsetCacheFileVersion) {
patchOffsetCache.moduleParamsOffset = 0;
} else {
moduleParamsOffset = patchOffsetCache.moduleParamsOffset;
}
if (!moduleParamsOffset) {
moduleParamsOffset = findOffset(
(u32*)ndsHeader->arm9destination, ndsHeader->arm9binarySize,
moduleParamsSignature, 2
);
if (moduleParamsOffset) {
dbg_printf("Module params offset found: ");
patchOffsetCache.moduleParamsOffset = moduleParamsOffset;
} else {
dbg_printf("Module params offset not found\n");
}
} else {
dbg_printf("Module params offset not found\n");
dbg_printf("Module params offset restored: ");
}

if (moduleParamsOffset) {
Expand Down
9 changes: 7 additions & 2 deletions retail/bootloader/source/arm7/hook_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "cardengine_header_arm7.h"
#include "cheat_engine.h"
#include "common.h"
#include "patch.h"
#include "find.h"
#include "hook.h"

Expand Down Expand Up @@ -102,7 +103,10 @@ int hookNdsRetailArm7(
) {
dbg_printf("hookNdsRetailArm7\n");

u32* hookLocation = hookInterruptHandler((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize);
u32* hookLocation = patchOffsetCache.a7IrqHandlerOffset;
if (!hookLocation) {
hookLocation = hookInterruptHandler((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize);
}

// SDK 5
bool sdk5 = isSdk5(moduleParams);
Expand Down Expand Up @@ -157,8 +161,9 @@ int hookNdsRetailArm7(
}

dbg_printf("hookLocation: ");
dbg_hexa(hookLocation);
dbg_hexa((u32)hookLocation);
dbg_printf("\n\n");
patchOffsetCache.a7IrqHandlerOffset = hookLocation;

u32* vblankHandler = hookLocation;
u32* timer0Handler = hookLocation + 3;
Expand Down
5 changes: 4 additions & 1 deletion retail/bootloader/source/arm7/load_crt0.s
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
.global dsiSD
.global saveFileCluster
.global saveSize
.global patchOffsetCacheFileCluster
.global language
.global dsiMode
.global donorSdkVer
Expand Down Expand Up @@ -77,7 +78,9 @@ dsiSD:
saveFileCluster:
.word 0x00000000 @ .sav file
saveSize:
.word 0x00000000 @ .sav file sive
.word 0x00000000 @ .sav file size
patchOffsetCacheFileCluster:
.word 0x00000000
language:
.word 0x00000000
dsiMode:
Expand Down
Loading

0 comments on commit b6bc3b0

Please sign in to comment.