Skip to content

Commit

Permalink
Optimize looking through pre-loaded ROM during AP-fix patching
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Dec 17, 2024
1 parent 15ef236 commit a7454eb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 35 deletions.
2 changes: 1 addition & 1 deletion retail/bootloaderi/include/ips.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef IPS_H
#define IPS_H

bool applyIpsPatch(const tNDSHeader* ndsHeader, u8* ipsbyte, const bool arm9Only, const bool isESdk2, const bool isSdk5, const bool ROMinRAM, const bool usesCloneboot, const u32 cacheBlockSize);
bool applyIpsPatch(const tNDSHeader* ndsHeader, u8* ipsbyte, const bool arm9Only, const bool isESdk2, const bool isSdk5, const bool ROMinRAM, const u32 cacheBlockSize);
bool ipsHasOverlayPatch(const tNDSHeader* ndsHeader, u8* ipsbyte);

#endif // IPS_H
41 changes: 8 additions & 33 deletions retail/bootloaderi/source/arm7/ips.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ extern bool sharedWramEnabled;
extern bool overlaysInRam;

extern bool scfgBios9i(void);
extern u32 getRomLocation(const tNDSHeader* ndsHeader, const bool isESdk2, const bool isSdk5, const bool dsiBios);

bool applyIpsPatch(const tNDSHeader* ndsHeader, u8* ipsbyte, const bool arm9Only, const bool isESdk2, const bool isSdk5, const bool ROMinRAM, const bool usesCloneboot, const u32 cacheBlockSize) {
bool applyIpsPatch(const tNDSHeader* ndsHeader, u8* ipsbyte, const bool arm9Only, const bool isESdk2, const bool isSdk5, const bool ROMinRAM, const u32 cacheBlockSize) {
if (ipsbyte[0] != 'P' && ipsbyte[1] != 'A' && ipsbyte[2] != 'T' && ipsbyte[3] != 'C' && ipsbyte[4] != 'H' && ipsbyte[5] != 0) {
return false;
}

bool armPatched = false;
const bool dsiBios = scfgBios9i();
const u32 romLocation = getRomLocation(ndsHeader, isESdk2, isSdk5, dsiBios);
const u32 wramLocation = sharedWramEnabled ? 0x036F8000 : 0x03700000;

int ipson = 5;
Expand All @@ -49,38 +47,15 @@ bool applyIpsPatch(const tNDSHeader* ndsHeader, u8* ipsbyte, const bool arm9Only
return armPatched;
}
if (ROMinRAM) {
rombyte = (void*)romLocation;
if (usesCloneboot) {
rombyte -= 0x4000;
} else if (ndsHeader->arm9overlaySource == 0 || ndsHeader->arm9overlaySize == 0) {
rombyte -= (ndsHeader->arm7romOffset + ndsHeader->arm7binarySize);
} else if (ndsHeader->arm9overlaySource > ndsHeader->arm7romOffset) {
rombyte -= (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
} else {
rombyte -= ndsHeader->arm9overlaySource;
}
if (isSdk5 || (dsiBios && !isESdk2)) {
if (romLocation < (ndsHeader->unitCode > 0 ? 0x0C7E0000 : 0x0C800000) && (u32)rombyte >= 0x0C7C4000) {
rombyte += (ndsHeader->unitCode > 0 ? 0x1C000 : 0x3C000);
} else if (ndsHeader->unitCode == 0) {
if (romLocation < 0x0D000000 && (u32)rombyte >= 0x0CFFC000) {
rombyte += 0x4000;
}
} else {
if (romLocation < 0x0C800000 && (u32)rombyte >= 0x0C7FC000) {
rombyte += 0x4000;
} else if (romLocation < 0x0D000000 && (u32)rombyte >= 0x0CFE0000) {
rombyte += 0x20000;
}
extern u32 romMapLines;
extern u32 romMap[5][3];
int i = 0;
for (i = 0; i < romMapLines; i++) {
if (offset >= romMap[i][0] && (i == romMapLines-1 || offset < romMap[i+1][0])) {
break;
}
} else if (romLocation < 0x0D000000 && (u32)rombyte >= 0x0CFFC000 && dsiBios) {
rombyte += 0x4000;
} else if (romLocation < 0x0C800000 && (u32)rombyte >= 0x0C7C0000) {
rombyte += 0x40000;
}
if ((u32)rombyte == (consoleModel > 0 ? 0x0E000000 : 0x0D000000)) {
rombyte = (void*)wramLocation;
}
rombyte = (void*)(romMap[i][1]-romMap[i][0]);
} else {
rombyte = (void*)CACHE_ADRESS_START_DSIMODE;
rombyte -= (ndsHeader->arm9overlaySource/cacheBlockSize)*cacheBlockSize;
Expand Down
2 changes: 1 addition & 1 deletion retail/bootloaderi/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -2290,7 +2290,7 @@ int arm7_main(void) {
}

if (useApPatch) {
if (applyIpsPatch(ndsHeader, (u8*)IPS_LOCATION, (*(u8*)(IPS_LOCATION+apPatchSize-1) == 0xA9), !laterSdk, isSdk5(moduleParams), ROMinRAM, usesCloneboot, cacheBlockSize)) {
if (applyIpsPatch(ndsHeader, (u8*)IPS_LOCATION, (*(u8*)(IPS_LOCATION+apPatchSize-1) == 0xA9), !laterSdk, isSdk5(moduleParams), ROMinRAM, cacheBlockSize)) {
dbg_printf("AP-fix applied\n");
} else {
dbg_printf("Failed to apply AP-fix\n");
Expand Down

0 comments on commit a7454eb

Please sign in to comment.