Skip to content

Commit

Permalink
Fix ROM pre-loading for when attempting to load SRL from offset set i…
Browse files Browse the repository at this point in the history
…n soft-reset params
  • Loading branch information
RocketRobz committed Dec 28, 2024
1 parent 784a71d commit 68378d4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
9 changes: 6 additions & 3 deletions retail/bootloaderi/source/arm7/hook_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ int hookNdsRetailArm9(
extern u32 iUncompressedSize;
extern u32 overlaysSize;
extern bool overlayPatch;
extern u32 baseFntOff;
extern u32 baseFatOff;
extern u32 baseFatSize;
extern u32 romPaddingSize;
extern u32 dataToPreloadAddr;
extern u32 dataToPreloadSize;
Expand Down Expand Up @@ -262,10 +265,10 @@ int hookNdsRetailArm9(
if (strncmp(romTid, "AZE", 3) == 0) { // Zelda: Phantom Hourglass
ce9->valueBits |= b_bypassExceptionHandler;
}
if (!ROMinRAM && dsiWramAccess && !dsiWramMirrored && (ndsHeader->unitCode == 0 || !dsiModeConfirmed) && ndsHeader->fatSize != 0) {
const u32 fntFatSize = (ndsHeader->fatOffset-ndsHeader->filenameOffset)+ndsHeader->fatSize;
if (!ROMinRAM && dsiWramAccess && !dsiWramMirrored && (ndsHeader->unitCode == 0 || !dsiModeConfirmed) && baseFatSize != 0) {
const u32 fntFatSize = (baseFatOff-baseFntOff)+baseFatSize;
if (fntFatSize <= 0x80000) {
ce9->fntSrc = ndsHeader->filenameOffset;
ce9->fntSrc = baseFntOff;
ce9->fntFatSize = fntFatSize;
ce9->valueBits |= b_fntFatCached;
}
Expand Down
50 changes: 35 additions & 15 deletions retail/bootloaderi/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,19 @@ static aFile patchOffsetCacheFile;
u32 waitSysCyclesOffset = 0;
static u32 softResetParams[0x50/4] = {0};
u32 srlAddr = 0;
u16 baseHeaderCRC = 0;
u32 baseArm9Off = 0;
u32 baseArm9Size = 0;
u32 baseArm7Off = 0;
u32 baseArm7Size = 0;
u32 baseFntOff = 0;
// u32 baseFntSize = 0;
u32 baseFatOff = 0;
u32 baseFatSize = 0;
u32 baseArm9OvlSrc = 0;
u32 baseArm9OvlSize = 0;
u16 baseSecureCRC = 0;
u32 baseRomSize = 0;
u16 baseHeaderCRC = 0;
u32 baseChipID = 0;
u32 romPaddingSize = 0;
bool pkmnHeader = false;
Expand Down Expand Up @@ -766,9 +776,19 @@ static void loadBinary_ARM7(const tDSiHeader* dsiHeaderTemp, aFile* file) {
pkmnHeader = true;
}

fileRead((char*)&baseHeaderCRC, file, 0x15E, sizeof(u16));
fileRead((char*)&baseArm9Off, file, 0x20, sizeof(u32));
fileRead((char*)&baseArm9Size, file, 0x2C, sizeof(u32));
fileRead((char*)&baseArm7Off, file, 0x30, sizeof(u32));
fileRead((char*)&baseArm7Size, file, 0x3C, sizeof(u32));
fileRead((char*)&baseFntOff, file, 0x40, sizeof(u32));
// fileRead((char*)&baseFntSize, file, 0x44, sizeof(u32));
fileRead((char*)&baseFatOff, file, 0x48, sizeof(u32));
fileRead((char*)&baseFatSize, file, 0x4C, sizeof(u32));
fileRead((char*)&baseArm9OvlSrc, file, 0x50, sizeof(u32));
fileRead((char*)&baseArm9OvlSize, file, 0x54, sizeof(u32));
fileRead((char*)&baseSecureCRC, file, 0x6C, sizeof(u16));
fileRead((char*)&baseRomSize, file, 0x80, sizeof(u32));
fileRead((char*)&baseHeaderCRC, file, 0x15E, sizeof(u16));

u8 baseDeviceSize = 0;
fileRead((char*)&baseDeviceSize, file, 0x14, sizeof(u8));
Expand Down Expand Up @@ -1110,12 +1130,12 @@ static void loadROMintoRAM(const tNDSHeader* ndsHeader, const module_params_t* m
u32 romOffset = 0;
if (usesCloneboot) {
romOffset = 0x4000;
} else if (ndsHeader->arm9overlaySource == 0 || ndsHeader->arm9overlaySize == 0) {
romOffset = (ndsHeader->arm7romOffset + ndsHeader->arm7binarySize);
} else if (ndsHeader->arm9overlaySource > ndsHeader->arm7romOffset) {
romOffset = (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
} else if (baseArm9OvlSrc == 0 || baseArm9OvlSize == 0) {
romOffset = (baseArm7Off + baseArm7Size);
} else if (baseArm9OvlSrc > baseArm7Off) {
romOffset = (baseArm9Off + baseArm9Size);
} else {
romOffset = ndsHeader->arm9overlaySource;
romOffset = baseArm9OvlSrc;
}

if (!isSdk5(moduleParams) && *(u32*)((romLocation-romOffset)+0x003128AC) == 0x4B434148) {
Expand Down Expand Up @@ -1143,12 +1163,12 @@ static void buildRomMap(const tNDSHeader* ndsHeader, const module_params_t* modu
romOffset = 0x4000;
romSizeEdit -= 0x4000;
romSizeEdit += 0x88;
} else if (ndsHeader->arm9overlaySource == 0 || ndsHeader->arm9overlaySize == 0) {
romOffset = (ndsHeader->arm7romOffset + ndsHeader->arm7binarySize);
} else if (ndsHeader->arm9overlaySource > ndsHeader->arm7romOffset) {
romOffset = (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
} else if (baseArm9OvlSrc == 0 || baseArm9OvlSize == 0) {
romOffset = (baseArm7Off + baseArm7Size);
} else if (baseArm9OvlSrc > baseArm7Off) {
romOffset = (baseArm9Off + baseArm9Size);
} else {
romOffset = ndsHeader->arm9overlaySource;
romOffset = baseArm9OvlSrc;
}
if (!usesCloneboot) {
romSizeEdit -= romOffset;
Expand Down Expand Up @@ -1193,13 +1213,13 @@ static void buildRomMap(const tNDSHeader* ndsHeader, const module_params_t* modu
}

static void loadNitroFileInfoIntoRAM(const tNDSHeader* ndsHeader, aFile* romFile) {
if (ndsHeader->fatSize == 0) return;
if (baseFatSize == 0) return;

const u32 size = (ndsHeader->fatOffset-ndsHeader->filenameOffset)+ndsHeader->fatSize;
const u32 size = (baseFatOff-baseFntOff)+baseFatSize;
if (size > 0x80000) return;

sdmmc_set_ndma_slot(0);
fileRead((char*)0x03700000, romFile, ndsHeader->filenameOffset, size);
fileRead((char*)0x03700000, romFile, baseFatOff, size);
sdmmc_set_ndma_slot(4);

dbg_printf("Nitro file info pre-loaded into RAM at ");
Expand Down

0 comments on commit 68378d4

Please sign in to comment.