Skip to content

Commit

Permalink
Update patch system to support some Debug SDK ROMs
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Mar 1, 2024
1 parent 6ca785b commit e6bcb18
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 45 deletions.
2 changes: 1 addition & 1 deletion retail/bootloader/include/find.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ inline u16* findOffsetBackwardsThumb4(const u16* start, u32 dataSize, const u16*
u32* findModuleParamsOffset(const tNDSHeader* ndsHeader);
u32* findLtdModuleParamsOffset(const tNDSHeader* ndsHeader);
u32* findCardReadEndOffsetType0(const tNDSHeader* ndsHeader, const module_params_t* moduleParams, u32 startOffset);
u32* findCardReadEndOffsetType1(const tNDSHeader* ndsHeader, u32 startOffset);
u32* findCardReadEndOffsetType1(const tNDSHeader* ndsHeader, const module_params_t* moduleParams, u32 startOffset);
u16* findCardReadEndOffsetThumb(const tNDSHeader* ndsHeader, u32 startOffset);
u16* findCardReadEndOffsetThumb5Type0(const tNDSHeader* ndsHeader, const module_params_t* moduleParams, u32 startOffset); // SDK 5
u16* findCardReadEndOffsetThumb5Type1(const tNDSHeader* ndsHeader, const module_params_t* moduleParams, u32 startOffset); // SDK 5
Expand Down
2 changes: 1 addition & 1 deletion retail/bootloader/include/patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

//extern bool cardReadFound; // patch_arm9.c

#define patchOffsetCacheFileVersion 42 // Change when new functions are being patched, some offsets removed,
#define patchOffsetCacheFileVersion 43 // Change when new functions are being patched, some offsets removed,
// the offset order changed, and/or the function signatures changed (not added)

typedef struct patchOffsetCacheContents {
Expand Down
31 changes: 26 additions & 5 deletions retail/bootloader/source/arm7/find_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ static const u16 wramClearSignature4Thumb[1] = {0x80E0};
static const u16 wramClearSignatureTwlThumb[3] = {0xB570, 0x1C05, 0x2D01};

// Relocate
static const u32 relocateStartSignature[1] = {0x027FFFFA};
static const u32 relocateStartSignature5[1] = {0x3381C0DE}; // 33 81 C0 DE DE C0 81 33 00 00 00 00 is the marker for the beggining of the relocated area :-)
static const u32 relocateStartSignature5Alt[1] = {0x2106C0DE};
static const u32 relocateStartSignature[1] = {0x027FFFFA};
static const u32 relocateStartSignature5[1] = {0x3381C0DE}; // 33 81 C0 DE DE C0 81 33 00 00 00 00 is the marker for the beggining of the relocated area :-)
static const u32 relocateStartSignature5Alt[1] = {0x2106C0DE};
static const u32 relocateStartSignature5Alt2[1] = {0x02FFFFFA};

static const u32 nextFunctiontSignature[1] = {0xE92D4000};
static const u32 relocateValidateSignature[1] = {0x400010C};
Expand Down Expand Up @@ -296,15 +297,35 @@ bool a7GetReloc(const tNDSHeader* ndsHeader, const module_params_t* moduleParams
(u32*)ndsHeader->arm7destination, 0x800,
relocateStartSignature5Alt, 1
);
if (relocationStart>0) relocationStart += 0x28;
if (relocationStart) relocationStart += 0x28;
}

if (!relocationStart) {
dbg_printf("Relocation start not found. Trying alt 2\n");
relocationStart = (u32)findOffset(
(u32*)ndsHeader->arm7destination, 0x800,
relocateStartSignature5Alt2, 1
);
if (relocationStart) {
int i = 0;
while ((*(u32*)relocationStart != 0) && (i < 0x100)) {
relocationStart += 4;
i += 4;
}
if (*(u32*)relocationStart != 0) {
relocationStart = 0;
} else {
relocationStart -= 8;
}
}
}

if (relocationStart) {
patchOffsetCache.relocateStartOffset = relocationStart;
}
}
if (!relocationStart) {
dbg_printf("Relocation start alt not found\n");
dbg_printf("Relocation start alt 2 not found\n");
return false;
}

Expand Down
114 changes: 102 additions & 12 deletions retail/bootloader/source/arm7/find_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ static const u32 cardReadEndSignature3Elab[3] = {0x04100010, 0x040001A4, 0
static const u32 cardReadEndSignatureAlt[2] = {0x040001A4, 0x04100010};
static const u32 cardReadEndSignatureSdk2Alt[3] = {0x040001A4, 0x04100010, 0xE92D000F}; // SDK 2
static const u32 cardReadEndSignatureAlt2[3] = {0x040001A4, 0x040001A1, 0x04100010};
static const u32 cardReadEndSignature5Elab[4] = {0x040001A4, 0x04100010, 0xE92D4010, 0xE59F00A0}; // SDK 5
static const u16 cardReadEndSignatureThumb[4] = {0x01A4, 0x0400, 0x0200, 0x0000};
static const u16 cardReadEndSignatureThumb5[4] = {0x01A4, 0x0400, 0xFE00, 0xFFFF}; // SDK 5
static const u16 cardReadEndSignatureThumb5Alt1[5] = {0x01A4, 0x0400, 0x0010, 0x0410, 0xB510}; // SDK 5
static const u32 cardReadStartSignature[1] = {0xE92D4FF0};
static const u32 cardReadStartSignatureAlt[1] = {0xE92D47F0};
static const u32 cardReadStartSignatureAlt2[1] = {0xE92D4070};
static const u32 cardReadStartSignatureDebug[3] = {0xE92D000F, 0xE92D47F0, 0xE24DD010}; // DEBUG
static const u32 cardReadStartSignatureDebugAlt[3] = {0xE92D47F0, 0xE1A05000, 0xE59F40E4}; // DEBUG
static const u32 cardReadStartSignature5[1] = {0xE92D4FF8}; // SDK 5
static const u32 cardReadStartSignature5Alt[4] = {0xE92D4010}; // SDK 5.5
static const u32 cardReadCheckSignatureMvDK4[3] = {0xE5C02289, 0xE5C02288, 0xE5D0028A};
Expand All @@ -41,8 +44,10 @@ static const u32 cardPullOutSignature1[4] = {0xE92D4000, 0xE24DD004, 0xE
static const u32 cardPullOutSignature1Elab[5] = {0xE92D4000, 0xE24DD004, 0xE201003F, 0xE3500011, 0x1A00000F}; // SDK 2
static const u32 cardPullOutSignature2Alt[4] = {0xE92D000F, 0xE92D4030, 0xE24DD004, 0xE59D0014}; // SDK 2
static const u32 cardPullOutSignature4[4] = {0xE92D4008, 0xE201003F, 0xE3500011, 0x1A00000D}; // SDK >= 4
static const u32 cardPullOutSignatureDebug[5] = {0xE92D000F, 0xE92D4038, 0xE59D0014, 0xE200503F, 0xE3550011}; // SDK 4 (DEBUG)
static const u32 cardPullOutSignature5[4] = {0xE92D4010, 0xE201003F, 0xE3500011, 0x1A000012}; // SDK 5
static const u32 cardPullOutSignature5Alt[4] = {0xE92D4038, 0xE201003F, 0xE3500011, 0x1A000011}; // SDK 5
static const u32 cardPullOutSignatureDebug5[5] = {0xE92D000F, 0xE92D4038, 0xE59D0014, 0xE200403F, 0xE3540011}; // SDK 5 (DEBUG)
static const u16 cardPullOutSignatureThumb[5] = {0xB508, 0x203F, 0x4008, 0x2811, 0xD10E};
static const u16 cardPullOutSignatureThumbAlt[4] = {0xB500, 0xB081, 0x203F, 0x4001};
static const u16 cardPullOutSignatureThumbAlt2[4] = {0xB5F8, 0x203F, 0x4008, 0x2811};
Expand All @@ -58,6 +63,7 @@ static const u16 cardPullOutSignatureThumb5Alt[4] = {0xB538, 0x203F, 0x4008, 0x2
static const u32 cardIdEndSignature[2] = {0x040001A4, 0x04100010};
static const u32 cardIdEndSignature5[4] = {0xE8BD8010, 0x02FFFAE0, 0x040001A4, 0x04100010}; // SDK 5
static const u32 cardIdEndSignature5Alt[3] = {0x02FFFAE0, 0x040001A4, 0x04100010}; // SDK 5
static const u32 cardIdEndSignatureDebug5[4] = {0x0AFFFFFA, 0xE59F0008, 0xE5900000, 0xE8BD8010}; // SDK 5
static const u16 cardIdEndSignatureThumb[6] = {0xFFFF, 0xF8FF, 0x01A4, 0x0400, 0x0010, 0x0410};
static const u16 cardIdEndSignatureThumbAlt[6] = {0xFFFF, 0xF8FF, 0x0000, 0xA700, 0xE000, 0xFFFF};
static const u16 cardIdEndSignatureThumb5[8] = {0xFAE0, 0x02FF, 0xFFFF, 0xF8FF, 0x01A4, 0x0400, 0x0010, 0x0410}; // SDK 5
Expand All @@ -74,6 +80,7 @@ static const u16 cardIdStartSignatureThumbAlt3[2] = {0xB510, 0x24B8};
// Card read DMA
static const u32 cardReadDmaEndSignature[2] = {0x01FF8000, 0x000001FF};
static const u32 cardReadDmaEndSignatureSdk2Alt[2] = {0x01FF8000, 0xE92D4030}; // SDK 2
static const u32 cardReadDmaEndSignatureDebug[3] = {0xE28DD010, 0xE12FFF1E, 0x000001FF}; // DEBUG
static const u16 cardReadDmaEndSignatureThumbAlt[4] = {0x8000, 0x01FF, 0x0000, 0x0200};
static const u32 cardReadDmaStartSignature[1] = {0xE92D4FF8};
static const u32 cardReadDmaStartSignatureSdk2Alt[1] = {0xE92D4070};
Expand Down Expand Up @@ -276,18 +283,25 @@ u32* findCardReadEndOffsetType0(const tNDSHeader* ndsHeader, const module_params
return cardReadEndOffset;
}

u32* findCardReadEndOffsetType1(const tNDSHeader* ndsHeader, u32 startOffset) {
u32* findCardReadEndOffsetType1(const tNDSHeader* ndsHeader, const module_params_t* moduleParams, u32 startOffset) {
dbg_printf("findCardReadEndOffsetType1:\n");

// const char* romTid = getRomTid(ndsHeader);

u32* cardReadEndOffset = NULL;
//readType = 1;
cardReadEndOffset = findOffset(
(u32*)startOffset, iUncompressedSize-(startOffset-0x02000000),//ndsHeader->arm9binarySize,
cardReadEndSignatureAlt, 2
);

if (isSdk5(moduleParams)) {
cardReadEndOffset = findOffset(
(u32*)startOffset, iUncompressedSize-(startOffset-0x02000000),//ndsHeader->arm9binarySize,
cardReadEndSignature5Elab, 4
);
}
if (!cardReadEndOffset) {
cardReadEndOffset = findOffset(
(u32*)startOffset, iUncompressedSize-(startOffset-0x02000000),//ndsHeader->arm9binarySize,
cardReadEndSignatureAlt, 2
);
}

if (!cardReadEndOffset) {
cardReadEndOffset = findOffset(
Expand Down Expand Up @@ -446,6 +460,30 @@ u32* findCardReadStartOffsetType1(const u32* cardReadEndOffset) {
dbg_printf("ARM9 Card read start alt 2 (type 1) not found\n");
}

if (!cardReadStartOffset) {
cardReadStartOffset = findOffsetBackwards(
cardReadEndOffset, 0x178,
cardReadStartSignatureDebug, 3
);
if (cardReadStartOffset) {
dbg_printf("ARM9 Card read start DEBUG (type 1) found\n");
} else {
dbg_printf("ARM9 Card read start DEBUG (type 1) not found\n");
}
}

if (!cardReadStartOffset) {
cardReadStartOffset = findOffsetBackwards(
cardReadEndOffset, 0x118,
cardReadStartSignatureDebugAlt, 3
);
if (cardReadStartOffset) {
dbg_printf("ARM9 Card read start DEBUG alt (type 1) found\n");
} else {
dbg_printf("ARM9 Card read start DEBUG alt (type 1) not found\n");
}
}

dbg_printf("\n");
return cardReadStartOffset;
}
Expand Down Expand Up @@ -596,8 +634,7 @@ u32* findCardPullOutOffset(const tNDSHeader* ndsHeader, const module_params_t* m
//if (!usesThumb) {

u32* cardPullOutOffset = 0;
if (moduleParams->sdk_version > 0x5000000) {
// SDK 5
if (moduleParams->sdk_version > 0x5000000) { // SDK 5
cardPullOutOffset = findOffset(
(u32*)ndsHeader->arm9destination, iUncompressedSize,//ndsHeader->arm9binarySize,
cardPullOutSignature5, 4
Expand All @@ -609,7 +646,6 @@ u32* findCardPullOutOffset(const tNDSHeader* ndsHeader, const module_params_t* m
}

if (!cardPullOutOffset) {
// SDK 5
cardPullOutOffset = findOffset(
(u32*)ndsHeader->arm9destination, iUncompressedSize,//ndsHeader->arm9binarySize,
cardPullOutSignature5Alt, 4
Expand All @@ -620,6 +656,18 @@ u32* findCardPullOutOffset(const tNDSHeader* ndsHeader, const module_params_t* m
dbg_printf("Card pull out handler SDK 5 alt not found\n");
}
}

if (!cardPullOutOffset) {
cardPullOutOffset = findOffset(
(u32*)ndsHeader->arm9destination, iUncompressedSize,//ndsHeader->arm9binarySize,
cardPullOutSignatureDebug5, 5
);
if (cardPullOutOffset) {
dbg_printf("Card pull out handler SDK 5 DEBUG found\n");
} else {
dbg_printf("Card pull out handler SDK 5 DEBUG not found\n");
}
}
} else {
if (moduleParams->sdk_version > 0x2008000 && moduleParams->sdk_version < 0x3000000) {
// SDK 2
Expand Down Expand Up @@ -683,6 +731,18 @@ u32* findCardPullOutOffset(const tNDSHeader* ndsHeader, const module_params_t* m
dbg_printf("Card pull out handler not found\n");
}
}

if (!cardPullOutOffset) {
cardPullOutOffset = findOffset(
(u32*)ndsHeader->arm9destination, iUncompressedSize,//ndsHeader->arm9binarySize,
cardPullOutSignatureDebug, 5
);
if (cardPullOutOffset) {
dbg_printf("Card pull out handler DEBUG found\n");
} else {
dbg_printf("Card pull out handler DEBUG not found\n");
}
}
}

dbg_printf("\n");
Expand Down Expand Up @@ -808,14 +868,13 @@ u32* findCardIdEndOffset(const tNDSHeader* ndsHeader, const module_params_t* mod
u32* cardIdEndOffset = NULL;

if (isSdk5(moduleParams)) {
// SDK 5
if (cardReadEndOffset) {
cardIdEndOffset = findOffsetBackwards(
(u32*)cardReadEndOffset, 0x800,
cardIdEndSignature5, 4
);
} else {
cardIdEndOffset = findOffsetBackwards(
cardIdEndOffset = findOffset(
(u32*)ndsHeader->arm9destination, iUncompressedSize,
cardIdEndSignature5, 4
);
Expand All @@ -827,7 +886,6 @@ u32* findCardIdEndOffset(const tNDSHeader* ndsHeader, const module_params_t* mod
}

if (!cardIdEndOffset) {
// SDK 5
cardIdEndOffset = findOffset(
(u32*)ndsHeader->arm9destination, iUncompressedSize,
cardIdEndSignature5Alt, 3
Expand All @@ -838,6 +896,25 @@ u32* findCardIdEndOffset(const tNDSHeader* ndsHeader, const module_params_t* mod
dbg_printf("Card ID end SDK 5 alt not found\n");
}
}

if (!cardIdEndOffset) {
if (cardReadEndOffset) {
cardIdEndOffset = findOffsetBackwards(
(u32*)cardReadEndOffset, 0x800,
cardIdEndSignatureDebug5, 4
);
} else {
cardIdEndOffset = findOffset(
(u32*)ndsHeader->arm9destination, iUncompressedSize,
cardIdEndSignatureDebug5, 4
);
}
if (cardIdEndOffset) {
dbg_printf("Card ID end SDK 5 DEBUG found: ");
} else {
dbg_printf("Card ID end SDK 5 DEBUG not found\n");
}
}
} else {
if (cardReadEndOffset) {
cardIdEndOffset = findOffset(
Expand Down Expand Up @@ -1018,6 +1095,18 @@ u32* findCardReadDmaEndOffset(const tNDSHeader* ndsHeader, const module_params_t
}
}

if (!cardReadDmaEndOffset) {
cardReadDmaEndOffset = findOffset(
(u32*)ndsHeader->arm9destination, iUncompressedSize,//ndsHeader->arm9binarySize,
cardReadDmaEndSignatureDebug, 3
);
if (cardReadDmaEndOffset) {
dbg_printf("Card read DMA end DEBUG found: ");
} else {
dbg_printf("Card read DMA end DEBUG not found\n");
}
}

if (cardReadDmaEndOffset) {
dbg_hexa((u32)cardReadDmaEndOffset);
dbg_printf("\n");
Expand Down Expand Up @@ -1069,6 +1158,7 @@ u32* findCardReadDmaStartOffset(const module_params_t* moduleParams, const u32*
);
if (cardReadDmaStartOffset) {
dbg_printf("Card read DMA start SDK 5 found: ");
if (cardReadDmaStartOffset[-1] == 0xE92D000F) cardReadDmaStartOffset--;
} else {
dbg_printf("Card read DMA start SDK 5 not found\n");
}
Expand Down
15 changes: 11 additions & 4 deletions retail/bootloader/source/arm7/patch_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static bool patchCardRead(cardengineArm9* ce9, const tNDSHeader* ndsHeader, cons
}
if (!cardReadEndOffset) {
//dbg_printf("Trying alt...\n");
cardReadEndOffset = findCardReadEndOffsetType1(ndsHeader, startOffset);
cardReadEndOffset = findCardReadEndOffsetType1(ndsHeader, moduleParams, startOffset);
if (cardReadEndOffset) {
readType = 1;
if (*(cardReadEndOffset - 1) == 0xFFFFFE00) {
Expand Down Expand Up @@ -217,9 +217,9 @@ static void patchCacheFlush(cardengineArm9* ce9, bool usesThumb, u32* cardPullOu
tonccpy(forceToPowerOffOffset, cardPullOutPatch, 0x4);
}*/

static void patchCardId(cardengineArm9* ce9, const tNDSHeader* ndsHeader, const module_params_t* moduleParams, bool usesThumb, u32* cardReadEndOffset) {
static bool patchCardId(cardengineArm9* ce9, const tNDSHeader* ndsHeader, const module_params_t* moduleParams, bool usesThumb, u32* cardReadEndOffset) {
if (!isPawsAndClaws(ndsHeader) && !cardReadEndOffset) {
return;
return true;
}

// Card ID
Expand Down Expand Up @@ -250,7 +250,11 @@ static void patchCardId(cardengineArm9* ce9, const tNDSHeader* ndsHeader, const
dbg_printf("cardId location : ");
dbg_hexa((u32)cardIdStartOffset);
dbg_printf("\n\n");
} else if (isSdk5(moduleParams)) {
return false;
}

return true;
}

static void patchCardReadDma(cardengineArm9* ce9, const tNDSHeader* ndsHeader, const module_params_t* moduleParams, bool usesThumb) {
Expand Down Expand Up @@ -2052,7 +2056,10 @@ u32 patchCardNdsArm9(cardengineArm9* ce9, const tNDSHeader* ndsHeader, const mod
//patchForceToPowerOff(ce9, ndsHeader, usesThumb);

if (!isPawsAndClaws(ndsHeader)) {
patchCardId(ce9, ndsHeader, moduleParams, usesThumb, cardReadEndOffset);
if (!patchCardId(ce9, ndsHeader, moduleParams, usesThumb, cardReadEndOffset)) {
dbg_printf("ERR_LOAD_OTHR\n\n");
return ERR_LOAD_OTHR;
}
}

patchCardReadDma(ce9, ndsHeader, moduleParams, usesThumb);
Expand Down
12 changes: 12 additions & 0 deletions retail/bootloader/source/arm7/patch_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,18 @@ void patchBinary(cardengineArm9* ce9, const tNDSHeader* ndsHeader, module_params
const u16* branchCode7 = generateA7InstrThumb(0x020BAC60, 0x020BACB6);
tonccpy((void*)0x020BAC60, branchCode7, 0x4);
}

// Shantae DSi (03/06/09 build)
else if (strcmp(romTid, "AIPE") == 0 && ndsHeader->headerCRC16 == 0x700E && !extendedMemory) {
*(u32*)0x02046BFC -= 2;
*(u32*)0x02046DE4 -= 2;
}

// Shantae DSi (04/01/09 build)
else if (strcmp(romTid, "NTRJ") == 0 && ndsHeader->headerCRC16 == 0xAC4C && !extendedMemory) {
*(u32*)0x0203FB20 -= 2;
*(u32*)0x0203FD14 -= 2;
}
}

void rsetA7Cache(void)
Expand Down
2 changes: 1 addition & 1 deletion retail/bootloaderi/include/find.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ u32* findDsiModeCheck2Offset(const u32* dsiModeCheckOffset, bool usesThumb);
u32* findCardHashInitOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams);
u16* findCardHashInitOffsetThumb(const tNDSHeader* ndsHeader, const module_params_t* moduleParams);
u32* findCardReadEndOffsetType0(const tNDSHeader* ndsHeader, const module_params_t* moduleParams, u32 startOffset);
u32* findCardReadEndOffsetType1(const tNDSHeader* ndsHeader, u32 startOffset);
u32* findCardReadEndOffsetType1(const tNDSHeader* ndsHeader, const module_params_t* moduleParams, u32 startOffset);
u16* findCardReadEndOffsetThumb(const tNDSHeader* ndsHeader, u32 startOffset);
u16* findCardReadEndOffsetThumb5Type0(const tNDSHeader* ndsHeader, const module_params_t* moduleParams, u32 startOffset); // SDK 5
u16* findCardReadEndOffsetThumb5Type1(const tNDSHeader* ndsHeader, const module_params_t* moduleParams, u32 startOffset); // SDK 5
Expand Down
2 changes: 1 addition & 1 deletion retail/bootloaderi/include/patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

//extern bool cardReadFound; // patch_arm9.c

#define patchOffsetCacheFileVersion 101 // Change when new functions are being patched, some offsets removed,
#define patchOffsetCacheFileVersion 102 // Change when new functions are being patched, some offsets removed,
// the offset order changed, and/or the function signatures changed (not added)

typedef struct patchOffsetCacheContents {
Expand Down
Loading

0 comments on commit e6bcb18

Please sign in to comment.