Skip to content

Commit

Permalink
Fix ROMs which contain arm9 offset above 0x4000 not booting
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Feb 5, 2024
1 parent a5e782a commit 44782c2
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 45 deletions.
4 changes: 2 additions & 2 deletions retail/bootloader/source/arm7/hook_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ int hookNdsRetailArm9(
} else if (ndsHeader->arm9overlaySource == 0 || ndsHeader->arm9overlaySize == 0) {
romOffset = (ndsHeader->arm7romOffset + ndsHeader->arm7binarySize);
} else {
romOffset = (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
romOffset = ndsHeader->arm9overlaySource;
}
} else {
romOffset = (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
romOffset = ndsHeader->arm9overlaySource;
}
ce9->romLocation -= romOffset;

Expand Down
6 changes: 2 additions & 4 deletions retail/bootloader/source/arm7/ips.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ bool applyIpsPatch(const tNDSHeader* ndsHeader, u8* ipsbyte, bool arm9Only, bool
rombyte -= ndsHeader->arm7romOffset;
rombyte -= ndsHeader->arm7binarySize;
} else {
rombyte -= ndsHeader->arm9romOffset;
rombyte -= ndsHeader->arm9binarySize;
rombyte -= ndsHeader->arm9overlaySource;
}
} else {
rombyte -= ndsHeader->arm9romOffset;
rombyte -= ndsHeader->arm9binarySize;
rombyte -= ndsHeader->arm9overlaySource;
}
}
ipson += 3;
Expand Down
30 changes: 14 additions & 16 deletions retail/bootloader/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,9 @@ static bool isROMLoadableInRAM(const tDSiHeader* dsiHeader, const tNDSHeader* nd
romSize -= ndsHeader->arm7romOffset;
romSize -= ndsHeader->arm7binarySize;
} else {
romSize -= ndsHeader->arm9romOffset;
romSize -= ndsHeader->arm9binarySize;
romSize -= ndsHeader->arm9overlaySource;
}
res = ((expansionPakFound || (extendedMemory && !dsDebugRam)) && (ndsHeader->unitCode == 3 ? (usesCloneboot ? ((u32)dsiHeader->arm9iromOffset-0x8000) : ((u32)dsiHeader->arm9iromOffset-ndsHeader->arm9romOffset-ndsHeader->arm9binarySize))+ioverlaysSize : romSize) <= romSizeLimit);
res = ((expansionPakFound || (extendedMemory && !dsDebugRam)) && (ndsHeader->unitCode == 3 ? (usesCloneboot ? ((u32)dsiHeader->arm9iromOffset-0x8000) : ((u32)dsiHeader->arm9iromOffset-ndsHeader->arm9overlaySource))+ioverlaysSize : romSize) <= romSizeLimit);
if (res) {
dbg_printf(expansionPakFound ? "ROM is loadable into Slot-2 RAM\n" : "ROM is loadable into RAM\n");
}
Expand Down Expand Up @@ -844,7 +843,7 @@ static void loadOverlaysintoRAM(const tNDSHeader* ndsHeader, const module_params
while (1) {
u32 readLen = (len > bufferSize) ? bufferSize : len;

fileRead((char*)buffer, file, (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize)+dst, readLen);
fileRead((char*)buffer, file, ndsHeader->arm9overlaySource+dst, readLen);
tonccpy((char*)romLocation+dst, (char*)buffer, readLen);

len -= bufferSize;
Expand All @@ -856,11 +855,11 @@ static void loadOverlaysintoRAM(const tNDSHeader* ndsHeader, const module_params
}
toncset((char*)buffer, 0, bufferSize);
} else {
fileRead((char*)romLocation, file, ndsHeader->arm9romOffset + ndsHeader->arm9binarySize, overlaysSize);
fileRead((char*)romLocation, file, ndsHeader->arm9overlaySource, overlaysSize);
}

if (!isSdk5(moduleParams) && *(u32*)((romLocation-ndsHeader->arm9romOffset-ndsHeader->arm9binarySize)+0x003128AC) == 0x4B434148) {
*(u32*)((romLocation-ndsHeader->arm9romOffset-ndsHeader->arm9binarySize)+0x3128AC) = 0xA00; // Primary fix for Mario's Holiday
if (!isSdk5(moduleParams) && *(u32*)((romLocation-ndsHeader->arm9overlaySource)+0x003128AC) == 0x4B434148) {
*(u32*)((romLocation-ndsHeader->arm9overlaySource)+0x3128AC) = 0xA00; // Primary fix for Mario's Holiday
}
}
}
Expand All @@ -879,8 +878,8 @@ static void loadOverlaysintoFile(const tNDSHeader* ndsHeader, const module_param
while (1) {
u32 readLen = (len > bufferSize) ? bufferSize : len;

fileRead((char*)buffer, file, (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize)+dst, readLen);
fileWrite((char*)buffer, &apFixOverlaysFile, (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize)+dst, readLen);
fileRead((char*)buffer, file, ndsHeader->arm9overlaySource+dst, readLen);
fileWrite((char*)buffer, &apFixOverlaysFile, ndsHeader->arm9overlaySource+dst, readLen);

len -= bufferSize;
dst += bufferSize;
Expand Down Expand Up @@ -908,7 +907,7 @@ static void loadIOverlaysintoRAM(const tDSiHeader* dsiHeader, aFile* file, const
// Load overlays into RAM
if (ioverlaysSize>0x700000) return;

u32 romOffset = usesCloneboot ? 0x8000 : (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
u32 romOffset = usesCloneboot ? 0x8000 : ndsHeader->arm9overlaySource;
fileRead((char*)romLocation+((u32)dsiHeader->arm9iromOffset-romOffset), file, (u32)dsiHeader->arm9iromOffset+dsiHeader->arm9ibinarySize, ioverlaysSize);
}

Expand All @@ -921,12 +920,11 @@ static void loadROMintoRAM(const tNDSHeader* ndsHeader, const module_params_t* m
romSizeEdit += 0x88;
} else if (ndsHeader->arm9overlaySource == 0 || ndsHeader->arm9overlaySize == 0) {
romOffset = (ndsHeader->arm7romOffset + ndsHeader->arm7binarySize);
romSizeEdit -= ndsHeader->arm7romOffset;
romSizeEdit -= ndsHeader->arm7binarySize;
} else {
romOffset = (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
romSizeEdit -= ndsHeader->arm9romOffset;
romSizeEdit -= ndsHeader->arm9binarySize;
romOffset = ndsHeader->arm9overlaySource;
}
if (!usesCloneboot) {
romSizeEdit -= romOffset;
}

if ((_io_dldi_features & FEATURE_SLOT_GBA) && s2FlashcardId != 0) {
Expand Down Expand Up @@ -1268,7 +1266,7 @@ int arm7_main(void) {
}

// Calculate overlay pack size
for (u32 i = ndsHeader->arm9romOffset+ndsHeader->arm9binarySize; i < ndsHeader->arm7romOffset; i++) {
for (u32 i = ndsHeader->arm9overlaySource; i < ndsHeader->arm7romOffset; i++) {
overlaysSize++;
}
if (ndsHeader->unitCode == 3) {
Expand Down
2 changes: 1 addition & 1 deletion retail/bootloaderi/source/arm7/hook_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ int hookNdsRetailArm7(
} else if (ndsHeader->arm9overlaySource == 0 || ndsHeader->arm9overlaySize == 0) {
romOffset = (ndsHeader->arm7romOffset + ndsHeader->arm7binarySize);
} else {
romOffset = (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
romOffset = ndsHeader->arm9overlaySource;
}
ce7->romLocation -= romOffset;

Expand Down
2 changes: 1 addition & 1 deletion retail/bootloaderi/source/arm7/hook_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ int hookNdsRetailArm9(
} else if (ndsHeader->arm9overlaySource == 0 || ndsHeader->arm9overlaySize == 0) {
romOffset = (ndsHeader->arm7romOffset + ndsHeader->arm7binarySize);
} else {
romOffset = (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
romOffset = ndsHeader->arm9overlaySource;
}
configureRomMap(ce9, ndsHeader, romOffset, dsiMode, consoleModel);
}
Expand Down
5 changes: 2 additions & 3 deletions retail/bootloaderi/source/arm7/ips.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ bool applyIpsPatch(const tNDSHeader* ndsHeader, u8* ipsbyte, const bool arm9Only
rombyte -= ndsHeader->arm7romOffset;
rombyte -= ndsHeader->arm7binarySize;
} else {
rombyte -= ndsHeader->arm9romOffset;
rombyte -= ndsHeader->arm9binarySize;
rombyte -= ndsHeader->arm9overlaySource;
}
if (ndsHeader->unitCode == 0 || !dsiModeConfirmed) {
if (isSdk5 || (dsiBios && !isESdk2)) {
Expand All @@ -81,7 +80,7 @@ bool applyIpsPatch(const tNDSHeader* ndsHeader, u8* ipsbyte, const bool arm9Only
}
} else {
rombyte = (void*)CACHE_ADRESS_START_DSIMODE;
rombyte -= ((ndsHeader->arm9romOffset + ndsHeader->arm9binarySize)/cacheBlockSize)*cacheBlockSize;
rombyte -= (ndsHeader->arm9overlaySource/cacheBlockSize)*cacheBlockSize;
}
}
ipson += 3;
Expand Down
26 changes: 12 additions & 14 deletions retail/bootloaderi/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,12 +803,11 @@ static bool isROMLoadableInRAM(const tDSiHeader* dsiHeader, const tNDSHeader* nd
romSize += 0x88;
} else if (ndsHeader->arm9overlaySource == 0 || ndsHeader->arm9overlaySize == 0) {
romOffset = (ndsHeader->arm7romOffset + ndsHeader->arm7binarySize);
romSize -= ndsHeader->arm7romOffset;
romSize -= ndsHeader->arm7binarySize;
} else {
romOffset = (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
romSize -= ndsHeader->arm9romOffset;
romSize -= ndsHeader->arm9binarySize;
romOffset = ndsHeader->arm9overlaySource;
}
if (!usesCloneboot) {
romSize -= romOffset;
}
res = ((consoleModel> 0 && twlType && ((u32)dsiHeader->arm9iromOffset - romOffset)+ioverlaysSize <= (cheatsEnabled ? dev_CACHE_ADRESS_SIZE_TWLSDK_CHEAT : dev_CACHE_ADRESS_SIZE_TWLSDK))
|| (consoleModel> 0 && !twlType && romSize <= (dsiModeConfirmed ? 0x01800000 : 0x01BC0000))
Expand Down Expand Up @@ -978,7 +977,7 @@ static void loadOverlaysintoRAM(const tNDSHeader* ndsHeader, const module_params
}

u32 overlaysLocation = CACHE_ADRESS_START_DSIMODE;
u32 alignedOverlaysOffset = ((ndsHeader->arm9romOffset + ndsHeader->arm9binarySize)/cacheBlockSize)*cacheBlockSize;
u32 alignedOverlaysOffset = (ndsHeader->arm9overlaySource/cacheBlockSize)*cacheBlockSize;
u32 newOverlaysSize = 0;
for (u32 i = alignedOverlaysOffset; i < ndsHeader->arm7romOffset; i+= cacheBlockSize) {
newOverlaysSize += cacheBlockSize;
Expand Down Expand Up @@ -1021,7 +1020,7 @@ static void loadIOverlaysintoRAM(const tDSiHeader* dsiHeader, aFile* file, const
} else if (ndsHeader->arm9overlaySource == 0 || ndsHeader->arm9overlaySize == 0) {
romOffset = (ndsHeader->arm7romOffset + ndsHeader->arm7binarySize);
} else {
romOffset = (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
romOffset = ndsHeader->arm9overlaySource;
}
fileRead((char*)ROM_LOCATION_TWLSDK+((u32)dsiHeader->arm9iromOffset-romOffset), file, (u32)dsiHeader->arm9iromOffset+dsiHeader->arm9ibinarySize, ioverlaysSize);
}
Expand All @@ -1042,12 +1041,11 @@ static void loadROMintoRAM(const tNDSHeader* ndsHeader, const module_params_t* m
romSizeEdit += 0x88;
} else if (ndsHeader->arm9overlaySource == 0 || ndsHeader->arm9overlaySize == 0) {
romOffset = (ndsHeader->arm7romOffset + ndsHeader->arm7binarySize);
romSizeEdit -= ndsHeader->arm7romOffset;
romSizeEdit -= ndsHeader->arm7binarySize;
} else {
romOffset = (ndsHeader->arm9romOffset + ndsHeader->arm9binarySize);
romSizeEdit -= ndsHeader->arm9romOffset;
romSizeEdit -= ndsHeader->arm9binarySize;
romOffset = ndsHeader->arm9overlaySource;
}
if (!usesCloneboot) {
romSizeEdit -= romOffset;
}

u32 romLocationChange = romLocation;
Expand Down Expand Up @@ -1612,7 +1610,7 @@ int arm7_main(void) {
dbg_printf("\n");

// Calculate overlay pack size
for (u32 i = ndsHeader->arm9romOffset+ndsHeader->arm9binarySize; i < ndsHeader->arm7romOffset; i++) {
for (u32 i = ndsHeader->arm9overlaySource; i < ndsHeader->arm7romOffset; i++) {
overlaysSize++;
}
if (ROMsupportsDsiMode(&dsiHeaderTemp.ndshdr) && dsiModeConfirmed) {
Expand Down Expand Up @@ -2148,7 +2146,7 @@ int arm7_main(void) {
getFileFromCluster(apFixOverlaysFile, apFixOverlaysCluster, gameOnFlashcard);
buildFatTableCacheCompressed(apFixOverlaysFile);

u32 alignedOverlaysOffset = ((ndsHeader->arm9romOffset + ndsHeader->arm9binarySize)/cacheBlockSize)*cacheBlockSize;
u32 alignedOverlaysOffset = (ndsHeader->arm9overlaySource/cacheBlockSize)*cacheBlockSize;
u32 newOverlaysSize = 0;
for (u32 i = alignedOverlaysOffset; i < ndsHeader->arm7romOffset; i+= cacheBlockSize) {
newOverlaysSize += cacheBlockSize;
Expand Down
4 changes: 2 additions & 2 deletions retail/cardengine/arm9/source/cardengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ static inline void cardReadNormal(u8* dst, u32 src, u32 len) {
nocashMessage("\n");*/

//nocashMessage("aaaaaaaaaa\n");
fileRead((char*)dst, (ce9->apFixOverlaysCluster && src >= ndsHeader->arm9romOffset+ndsHeader->arm9binarySize && src < ndsHeader->arm7romOffset) ? &apFixOverlaysFile : &romFile, src, len);
fileRead((char*)dst, (ce9->apFixOverlaysCluster && src >= ndsHeader->arm9overlaySource && src < ndsHeader->arm7romOffset) ? &apFixOverlaysFile : &romFile, src, len);

//nocashMessage("end\n");

Expand Down Expand Up @@ -712,7 +712,7 @@ void cardRead(u32* cacheStruct, u8* dst0, u32 src0, u32 len0) {
src = 0x8000 + (src & 0x1FF);
}

if ((ce9->valueBits & ROMinRAM) || ((ce9->valueBits & overlaysCached) && src >= ndsHeader->arm9romOffset+ndsHeader->arm9binarySize && src < ndsHeader->arm7romOffset)) {
if ((ce9->valueBits & ROMinRAM) || ((ce9->valueBits & overlaysCached) && src >= ndsHeader->arm9overlaySource && src < ndsHeader->arm7romOffset)) {
if (src >= 0 && src < 0x160) {
u32 newSrc = (u32)ndsHeader+src;
tonccpy(dst, (u8*)newSrc, len);
Expand Down
4 changes: 2 additions & 2 deletions retail/cardenginei/arm9/source/cardengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ static u32 newOverlaysSize = 0;
static inline void cardReadNormal(u8* dst, u32 src, u32 len) {
#ifdef DLDI
while (sharedAddr[3]==0x444D4152); // Wait during a RAM dump
fileRead((char*)dst, ((ce9->valueBits & overlaysCached) && src >= ndsHeader->arm9romOffset+ndsHeader->arm9binarySize && src < ndsHeader->arm7romOffset) ? apFixOverlaysFile : romFile, src, len);
fileRead((char*)dst, ((ce9->valueBits & overlaysCached) && src >= ndsHeader->arm9overlaySource && src < ndsHeader->arm7romOffset) ? apFixOverlaysFile : romFile, src, len);
#else
if (newOverlayOffset == 0) {
newOverlayOffset = ((ndsHeader->arm9romOffset + ndsHeader->arm9binarySize)/ce9->cacheBlockSize)*ce9->cacheBlockSize;
newOverlayOffset = (ndsHeader->arm9overlaySource/ce9->cacheBlockSize)*ce9->cacheBlockSize;
for (u32 i = newOverlayOffset; i < ndsHeader->arm7romOffset; i+= ce9->cacheBlockSize) {
newOverlaysSize += ce9->cacheBlockSize;
}
Expand Down

0 comments on commit 44782c2

Please sign in to comment.