Merge pull request #34 from taxicat1/dsprotect-arm9-static

Implement DSProtect patching in ARM9 static
This commit is contained in:
Gericom
2025-12-14 10:13:05 +01:00
committed by GitHub
8 changed files with 96 additions and 17 deletions

View File

@@ -12,6 +12,7 @@
#include "patches/arm9/CardiReadRomIdCorePatch.h"
#include "patches/arm9/OSResetSystemPatch.h"
#include "patches/arm9/PokemonDownloaderArm9Patch.h"
#include "patches/arm9/DSProtectArm9Patch.h"
#include "patches/arm9/OverlayPatches/FsStartOverlayHookPatch.h"
#include "patches/arm9/OverlayPatches/DSProtectPatches/DSProtectOverlayPatch.h"
#include "patches/arm9/OverlayPatches/DSProtectPatches/DSProtectPuyoPuyo7Patch.h"
@@ -243,7 +244,9 @@ void Arm9Patcher::AddDSProtectPatches(
{
if (regularOverlayId == AP_LIST_OVERLAY_ID_STATIC_ARM9)
{
LOG_WARNING("Patching DSProtect in main memory currently not supported\n");
patchCollection.AddPatch(new DSProtectArm9Patch(
apListEntry->GetRegularOffset(), apListEntry->GetDSProtectVersion(),
apListEntry->GetDSProtectFunctionMask()));
}
else
{
@@ -255,17 +258,18 @@ void Arm9Patcher::AddDSProtectPatches(
u32 sOverlayId = apListEntry->GetSOverlayId();
if (sOverlayId != AP_LIST_OVERLAY_ID_INVALID)
{
auto version = apListEntry->GetDSProtectVersion();
if (version < DSProtectVersion::v2_00s)
{
version = (DSProtectVersion)((u32)version - (u32)DSProtectVersion::v2_00 + (u32)DSProtectVersion::v2_00s);
}
if (sOverlayId == AP_LIST_OVERLAY_ID_STATIC_ARM9)
{
LOG_WARNING("Patching DSProtect in main memory currently not supported\n");
patchCollection.AddPatch(new DSProtectArm9Patch(
apListEntry->GetSOffset(), version, ~0u));
}
else
{
auto version = apListEntry->GetDSProtectVersion();
if (version < DSProtectVersion::v2_00s)
{
version = (DSProtectVersion)((u32)version - (u32)DSProtectVersion::v2_00 + (u32)DSProtectVersion::v2_00s);
}
overlayHookPatch->AddOverlayPatch(new DSProtectOverlayPatch(
sOverlayId, apListEntry->GetSOffset(), version, ~0u));
}

View File

@@ -0,0 +1,19 @@
#include "common.h"
#include "DSProtectArm9Patch.h"
#include "OverlayPatches/DSProtectPatches/DSProtectOverlayPatch.h"
#include "ApList.h"
#include "sharedMemory.h"
#include "ndsHeader.h"
bool DSProtectArm9Patch::FindPatchTarget(PatchContext& patchContext)
{
return true; // target already specified by _arm9Offset
}
void DSProtectArm9Patch::ApplyPatch(PatchContext& patchContext)
{
auto patch = new DSProtectOverlayPatch(
AP_LIST_OVERLAY_ID_STATIC_ARM9, _arm9Offset, _version, _functionMask);
auto romHeader = (const nds_header_ntr_t*)TWL_SHARED_MEMORY->ntrSharedMem.romHeader;
patch->ApplyPatchForStaticArm9(romHeader->arm9LoadAddress);
}

View File

@@ -0,0 +1,19 @@
#pragma once
#include "../Patch.h"
#include "DSProtectVersion.h"
/// @brief Arm9 static patch for DS Protect.
class DSProtectArm9Patch : public Patch
{
public:
DSProtectArm9Patch(u32 arm9Offset, DSProtectVersion version, u32 functionMask)
: _arm9Offset(arm9Offset), _version(version), _functionMask(functionMask) { }
bool FindPatchTarget(PatchContext& patchContext) override;
void ApplyPatch(PatchContext& patchContext) override;
private:
u32 _arm9Offset;
DSProtectVersion _version;
u32 _functionMask;
};

View File

@@ -1,7 +1,5 @@
#include "common.h"
#include "../../../PatchContext.h"
#include "thumbInstructions.h"
#include "gameCode.h"
#include "DSProtectOverlayPatchAsm.h"
#include "DSProtectOverlayPatch.h"
@@ -23,7 +21,10 @@
const void* DSProtectOverlayPatch::InsertPatch(PatchContext& patchContext)
{
ConfigurePatch(patchContext);
// Next patch and target overlay ID
dsprotectpatch_nextAddress = next ? (const void*)next->InsertPatch(patchContext) : nullptr;
dsprotectpatch_overlayId = _overlayId;
CalculateOffsets();
u32 patchSize = SECTION_SIZE(dsprotectpatch);
void* patchAddress = patchContext.GetPatchHeap().Alloc(patchSize);
@@ -33,12 +34,21 @@ const void* DSProtectOverlayPatch::InsertPatch(PatchContext& patchContext)
return (const void*)entryAddress;
}
void DSProtectOverlayPatch::ConfigurePatch(PatchContext& patchContext) const
void DSProtectOverlayPatch::ApplyPatchForStaticArm9(u32 arm9LoadAddress) const
{
// Next patch and target overlay ID
dsprotectpatch_nextAddress = next ? (const void*)next->InsertPatch(patchContext) : nullptr;
dsprotectpatch_overlayId = _overlayId;
u32 fakeOverlayInfo[2];
fakeOverlayInfo[0] = _overlayId; // ovy_id
fakeOverlayInfo[1] = arm9LoadAddress; // ram_start
dsprotectpatch_nextAddress = nullptr;
dsprotectpatch_overlayId = _overlayId;
CalculateOffsets();
dsprotectpatch_executeWithParam(fakeOverlayInfo);
}
void DSProtectOverlayPatch::CalculateOffsets() const
{
u32 regionOffset = _overlayOffset;
// Default invalid, enable below

View File

@@ -10,6 +10,7 @@ public:
: _overlayId(overlayId), _overlayOffset(overlayOffset), _version(version), _functionMask(functionMask) { }
const void* InsertPatch(PatchContext& patchContext) override;
void ApplyPatchForStaticArm9(u32 arm9LoadAddress) const;
private:
u32 _overlayId;
@@ -17,5 +18,5 @@ private:
DSProtectVersion _version;
u32 _functionMask;
void ConfigurePatch(PatchContext& patchContext) const;
void CalculateOffsets() const;
};

View File

@@ -4,11 +4,12 @@
DEFINE_SECTION_SYMBOLS(dsprotectpatch);
extern "C" void dsprotectpatch_entry();
extern "C" void dsprotectpatch_executeWithParam(u32* overlayInfo);
extern u32 dsprotectpatch_patchType; // 0=write dsprotectpatch_writeWord, 1=copy subsequent word
extern u32 dsprotectpatch_writeWord; // Word to write if dsprotectpatch_patchType=0
extern u32 dsprotectpatch_offsetA1; // Target offset for A1 (0xFFFFFFFF=invalid)
extern u32 dsprotectpatch_offsetNotA1; // Target offset for NotA1 (0xFFFFFFFF=invalid)
extern u32 dsprotectpatch_offsetA1; // Target offset for A1 (0x8xxxxxxx=invalid)
extern u32 dsprotectpatch_offsetNotA1; // Target offset for NotA1 (0x8xxxxxxx=invalid)
extern u32 dsprotectpatch_overlayId; // Target overlay ID
extern const void* dsprotectpatch_nextAddress; // Next patch address

View File

@@ -86,4 +86,16 @@ dsprotectpatch_nextAddress:
.pool
.text
.global dsprotectpatch_executeWithParam
.type dsprotectpatch_executeWithParam, %function
dsprotectpatch_executeWithParam:
push {r5, lr}
movs r5, r0
bl dsprotectpatch_entry
pop {r5, pc}
.pool
.end

View File

@@ -64,6 +64,7 @@ B7KJ;0;1.28;111111;41;0x0;-1;0x0
B7XJ;0;1.26;111111;0;0x0;-1;0x0
B86E;0;2.01;111111;0;0x0;-1;0x0
B86P;0;2.01;111111;0;0x0;-1;0x0
B89J;0;1.08;111111;-2;0x1145AC;-1;0x0
B8RJ;0;1.27;111111;7;0x0;-1;0x0
BA5P;0;1.27;111111;0;0x0;-1;0x0
BA7J;2;2.00;111111;0;0x0;-1;0x0
@@ -78,6 +79,7 @@ BCDK;0;2.01;111111;-1;0x0;0;0x0
BCJK;0;2.01;111111;28;0x0;29;0x0
BCKJ;0;1.25;111111;0;0x0;-1;0x0
BCXJ;0;1.25;111111;11;0x0;-1;0x0
BCXJ;1;1.25;111111;11;0x0;-1;0x0
BD2E;0;1.23;111111;2;0x0;-1;0x0
BD2J;0;1.23;111111;2;0x0;-1;0x0
BD2P;0;1.23;111111;2;0x0;-1;0x0
@@ -138,13 +140,16 @@ BJCI;0;1.28;111111;4;0x0;-1;0x0
BJCP;0;1.28;111111;4;0x0;-1;0x0
BJKJ;0;1.26;111111;7;0x0;-1;0x0
BK3J;0;1.23;111111;18;0x4bf8;-1;0x0
BK3J;1;1.23;111111;18;0x4c00;-1;0x0
BK9E;0;1.28;111111;104;0x0;-1;0x0
BK9J;0;1.28;111111;104;0x0;-1;0x0
BK9P;0;1.28;111111;104;0x0;-1;0x0
BKCE;0;1.22;10110;0;0x0;-1;0x0
BKIE;0;1.23Z;111111;1;0xa504;-1;0x0
BKIE;1;1.23Z;111111;1;0xa504;-1;0x0
BKIJ;0;1.23Z;111111;1;0xa538;-1;0x0
BKIP;0;1.23Z;111111;1;0xa504;-1;0x0
BKIP;1;1.23Z;111111;1;0xa504;-1;0x0
BKJJ;0;1.23;111111;7;0x0;-1;0x0
BKKJ;0;1.25;111111;-2;0x5a44c;-1;0x0
BKMJ;0;1.23;111111;3;0x0;-1;0x0
@@ -309,6 +314,8 @@ C6OJ;0;1.26;111111;2;0x0;-1;0x0
CA5E;0;1.25;111111;0;0x0;-1;0x0
CBEJ;0;1.08;111111;-2;0x314dc;-1;0x0
CCTJ;0;1.05;10101;-2;0x3caa0;-1;0x0
CCUJ;0;1.06;010101;-2;0xAF5BC;-1;0x0
CCUJ;1;1.06;010101;-2;0xAF9F0;-1;0x0
CDOK;0;1.20;111111;0;0x0;-1;0x0
CFIE;0;1.06;10101;-2;0xcaa60;-1;0x0
CFIJ;0;1.06;10101;-2;0xca9a8;-1;0x0
@@ -319,6 +326,7 @@ CJRP;0;1.28;111111;2;0xd0;-1;0x0
CKDJ;0;1.20;111111;-2;0x6c360;-1;0x0
CL4K;0;1.28;111111;0;0x0;-1;0x0
CLJE;0;1.20;10110;138;0x0;-1;0x0
CLJJ;0;1.06;111111;4;0x4A4FC;-1;0x0
CLJK;0;2.03;111111;138;0x0;-1;0x0
CLJP;0;1.20;10110;138;0x0;-1;0x0
CLPD;0;2.03;111111;1;0x0;-1;0x0
@@ -439,6 +447,7 @@ V2GE;1;1.28;111111;15;0x0;-1;0x0
V2GE;2;1.28;111111;15;0x0;-1;0x0
V2GJ;0;1.28;111111;15;0x0;-1;0x0
V2GV;0;1.28;111111;15;0x0;-1;0x0
VAAE;0;1.26;111111;0;0x0;-1;0x0
VAAJ;0;1.26;111111;0;0x0;-1;0x0
VAAK;0;1.26;111111;0;0x0;-1;0x0
VAAV;0;1.26;111111;0;0x0;-1;0x0
@@ -462,6 +471,7 @@ VCPJ;0;1.26;111111;0;0x0;-1;0x0
VCTJ;0;2.01;111111;0;0x0;-1;0x0
VDEE;0;2.03;111111;15;0x0;16;0x0
VDEJ;0;2.01;111111;15;0x0;16;0x0
VDEJ;1;2.01;111111;15;0x0;16;0x0
VEBJ;0;2.03;111111;0;0x3b0;-1;0x0
VETP;0;1.27;111111;52;0x0;-1;0x0
VFBE;0;1.23;111111;12;0x0;-1;0x0
@@ -484,9 +494,11 @@ VPFE;0;1.26;111111;5;0x0;-1;0x0
VPFV;0;1.27;111111;5;0x0;-1;0x0
VPLJ;0;1.28;111111;32;0x0;-1;0x0
VPTJ;0;1.22;111111;30;0x0;-1;0x0
VPTJ;1;1.22;111111;30;0x0;-1;0x0
VPYJ;0;2.03;111111;9;0x0;8;0x0
VPYP;0;2.05;111111;9;0x0;8;0x0
VPYT;0;2.05;111111;9;0x0;8;0x0
VPYT;1;2.05;111111;9;0x0;8;0x0
VS3E;0;1.28;111111;2;0x0;-1;0x0
VS3V;0;1.28;111111;2;0x0;-1;0x0
VSNE;0;1.23;111111;1;0x0;-1;0x0
@@ -525,6 +537,7 @@ YFYK;0;1.00_2;110011;-2;0x69d40;-1;0x0
YG4K;0;1.08;10101;2;0x0;-1;0x0
YKGE;0;1.10;111111;28;0x0;-1;0x0
YKGJ;0;1.08;111111;28;0x0;-1;0x0
YKGJ;1;1.10;111111;26;0x0;-1;0x0
YKGP;0;1.10;111111;28;0x0;-1;0x0
YLUJ;0;1.22;111111;-2;0x7a678;-1;0x0
YLUP;0;1.27;111111;-2;0x7d554;-1;0x0
1 gameCode gameVersion dsprotVersion dsprotFuncMask regularOvlId regularOffset sOvlId sOffset
64 B7XJ 0 1.26 111111 0 0x0 -1 0x0
65 B86E 0 2.01 111111 0 0x0 -1 0x0
66 B86P 0 2.01 111111 0 0x0 -1 0x0
67 B89J 0 1.08 111111 -2 0x1145AC -1 0x0
68 B8RJ 0 1.27 111111 7 0x0 -1 0x0
69 BA5P 0 1.27 111111 0 0x0 -1 0x0
70 BA7J 2 2.00 111111 0 0x0 -1 0x0
79 BCJK 0 2.01 111111 28 0x0 29 0x0
80 BCKJ 0 1.25 111111 0 0x0 -1 0x0
81 BCXJ 0 1.25 111111 11 0x0 -1 0x0
82 BCXJ 1 1.25 111111 11 0x0 -1 0x0
83 BD2E 0 1.23 111111 2 0x0 -1 0x0
84 BD2J 0 1.23 111111 2 0x0 -1 0x0
85 BD2P 0 1.23 111111 2 0x0 -1 0x0
140 BJCP 0 1.28 111111 4 0x0 -1 0x0
141 BJKJ 0 1.26 111111 7 0x0 -1 0x0
142 BK3J 0 1.23 111111 18 0x4bf8 -1 0x0
143 BK3J 1 1.23 111111 18 0x4c00 -1 0x0
144 BK9E 0 1.28 111111 104 0x0 -1 0x0
145 BK9J 0 1.28 111111 104 0x0 -1 0x0
146 BK9P 0 1.28 111111 104 0x0 -1 0x0
147 BKCE 0 1.22 10110 0 0x0 -1 0x0
148 BKIE 0 1.23Z 111111 1 0xa504 -1 0x0
149 BKIE 1 1.23Z 111111 1 0xa504 -1 0x0
150 BKIJ 0 1.23Z 111111 1 0xa538 -1 0x0
151 BKIP 0 1.23Z 111111 1 0xa504 -1 0x0
152 BKIP 1 1.23Z 111111 1 0xa504 -1 0x0
153 BKJJ 0 1.23 111111 7 0x0 -1 0x0
154 BKKJ 0 1.25 111111 -2 0x5a44c -1 0x0
155 BKMJ 0 1.23 111111 3 0x0 -1 0x0
314 CA5E 0 1.25 111111 0 0x0 -1 0x0
315 CBEJ 0 1.08 111111 -2 0x314dc -1 0x0
316 CCTJ 0 1.05 10101 -2 0x3caa0 -1 0x0
317 CCUJ 0 1.06 010101 -2 0xAF5BC -1 0x0
318 CCUJ 1 1.06 010101 -2 0xAF9F0 -1 0x0
319 CDOK 0 1.20 111111 0 0x0 -1 0x0
320 CFIE 0 1.06 10101 -2 0xcaa60 -1 0x0
321 CFIJ 0 1.06 10101 -2 0xca9a8 -1 0x0
326 CKDJ 0 1.20 111111 -2 0x6c360 -1 0x0
327 CL4K 0 1.28 111111 0 0x0 -1 0x0
328 CLJE 0 1.20 10110 138 0x0 -1 0x0
329 CLJJ 0 1.06 111111 4 0x4A4FC -1 0x0
330 CLJK 0 2.03 111111 138 0x0 -1 0x0
331 CLJP 0 1.20 10110 138 0x0 -1 0x0
332 CLPD 0 2.03 111111 1 0x0 -1 0x0
447 V2GE 2 1.28 111111 15 0x0 -1 0x0
448 V2GJ 0 1.28 111111 15 0x0 -1 0x0
449 V2GV 0 1.28 111111 15 0x0 -1 0x0
450 VAAE 0 1.26 111111 0 0x0 -1 0x0
451 VAAJ 0 1.26 111111 0 0x0 -1 0x0
452 VAAK 0 1.26 111111 0 0x0 -1 0x0
453 VAAV 0 1.26 111111 0 0x0 -1 0x0
471 VCTJ 0 2.01 111111 0 0x0 -1 0x0
472 VDEE 0 2.03 111111 15 0x0 16 0x0
473 VDEJ 0 2.01 111111 15 0x0 16 0x0
474 VDEJ 1 2.01 111111 15 0x0 16 0x0
475 VEBJ 0 2.03 111111 0 0x3b0 -1 0x0
476 VETP 0 1.27 111111 52 0x0 -1 0x0
477 VFBE 0 1.23 111111 12 0x0 -1 0x0
494 VPFV 0 1.27 111111 5 0x0 -1 0x0
495 VPLJ 0 1.28 111111 32 0x0 -1 0x0
496 VPTJ 0 1.22 111111 30 0x0 -1 0x0
497 VPTJ 1 1.22 111111 30 0x0 -1 0x0
498 VPYJ 0 2.03 111111 9 0x0 8 0x0
499 VPYP 0 2.05 111111 9 0x0 8 0x0
500 VPYT 0 2.05 111111 9 0x0 8 0x0
501 VPYT 1 2.05 111111 9 0x0 8 0x0
502 VS3E 0 1.28 111111 2 0x0 -1 0x0
503 VS3V 0 1.28 111111 2 0x0 -1 0x0
504 VSNE 0 1.23 111111 1 0x0 -1 0x0
537 YG4K 0 1.08 10101 2 0x0 -1 0x0
538 YKGE 0 1.10 111111 28 0x0 -1 0x0
539 YKGJ 0 1.08 111111 28 0x0 -1 0x0
540 YKGJ 1 1.10 111111 26 0x0 -1 0x0
541 YKGP 0 1.10 111111 28 0x0 -1 0x0
542 YLUJ 0 1.22 111111 -2 0x7a678 -1 0x0
543 YLUP 0 1.27 111111 -2 0x7d554 -1 0x0