Skip to content

Commit 5d95056

Browse files
authored
Slot1launch: Separate reading 1T-ROM and MROM cartridge headers - Fix address of ModuleParams - Fix loading and setting personalData (#2597)
* slot1launch: Split loading header for 1T-ROM and MROM cartridges * slot1launch: Fix moduleParams pointing to wrong data * slot1launch: Fix loading personalData passed to games
1 parent 5b86de9 commit 5d95056

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

slot1launch/bootloader/source/hook.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "hook.h"
2323
#include "common.h"
24-
#include "cardengine_arm7_bin.h"
2524

2625
extern unsigned long language;
2726
extern bool gameSoftReset;

slot1launch/bootloader/source/main.arm7.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ static const u32 cheatDataEndSignature[2] = {0xCF000000, 0x00000000};
105105

106106
// Module params
107107
static const u32 moduleParamsSignature[2] = {0xDEC00621, 0x2106C0DE};
108+
static module_params_t emulatedModuleParams;
109+
static module_params_t* moduleParams;
108110

109111
// Sleep input write
110112
static const u32 sleepInputWriteEndSignature1[2] = {0x04000136, 0x027FFFA8};
@@ -122,16 +124,33 @@ const char* getRomTid(const tNDSHeader* ndsHeader) {
122124
return romTid;
123125
}
124126

125-
static module_params_t* moduleParams;
126-
127127
u32* findModuleParamsOffset(const tNDSHeader* ndsHeader) {
128128
//dbg_printf("findModuleParamsOffset:\n");
129129

130130
u32* moduleParamsOffset = findOffset(
131131
(u32*)ndsHeader->arm9destination, ndsHeader->arm9binarySize,
132-
moduleParamsSignature, 2
132+
moduleParamsSignature, 2
133133
);
134-
return moduleParamsOffset;
134+
135+
// Return NULL if nothing is found
136+
if(moduleParamsOffset == NULL) {
137+
if (memcmp(ndsHeader->gameCode, "AS2E", 4) == 0) // Spider-Man 2 (USA) - Special case
138+
{
139+
emulatedModuleParams.sdk_version = LAST_NON_SDK5_VERSION;
140+
return (u32*)&emulatedModuleParams;
141+
}
142+
143+
return NULL;
144+
}
145+
146+
uintptr_t subtract_value = sizeof(module_params_t) - (sizeof(u32) * 2);
147+
uintptr_t base_ptr = (uintptr_t)moduleParamsOffset;
148+
149+
// This would be a really weird case. Return NULL
150+
if(base_ptr < subtract_value)
151+
return NULL;
152+
153+
return (u32*)(base_ptr - subtract_value);
135154
}
136155

137156
u32* findSleepInputWriteOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
@@ -140,7 +159,7 @@ u32* findSleepInputWriteOffset(const tNDSHeader* ndsHeader, const module_params_
140159
u32* offset = NULL;
141160
u32* endOffset = findOffset(
142161
(u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize,
143-
(moduleParams->sdk_version > 0x5000000) ? sleepInputWriteEndSignature5 : sleepInputWriteEndSignature1, 2
162+
isSdk5(moduleParams) ? sleepInputWriteEndSignature5 : sleepInputWriteEndSignature1, 2
144163
);
145164
if (endOffset) {
146165
offset = findOffsetBackwards(
@@ -273,9 +292,9 @@ static void my_readUserSettings(tNDSHeader* ndsHeader) {
273292
}
274293
}
275294

276-
PERSONAL_DATA* personalData = (PERSONAL_DATA*)((u32)__NDSHeader - (u32)ndsHeader + (u32)PersonalData); //(u8*)((u32)ndsHeader - 0x180)
295+
PERSONAL_DATA* personalData = (PERSONAL_DATA*)((u32)ndsHeader - ((u32)__NDSHeader - (u32)PersonalData)); //(u8*)((u32)ndsHeader - 0x180)
277296

278-
tonccpy(PersonalData, currentSettings, sizeof(PERSONAL_DATA));
297+
tonccpy(personalData, currentSettings, sizeof(PERSONAL_DATA));
279298

280299
if (useTwlCfg && (language == 0xFF || language == -1)) {
281300
language = twlCfgLang;
@@ -1031,15 +1050,15 @@ void arm7_main (void) {
10311050

10321051
ndsHeader = loadHeader(dsiHeaderTemp);
10331052

1053+
my_readUserSettings(ndsHeader); // Header has to be loaded first
1054+
10341055
bool isDSBrowser = (memcmp(ndsHeader->gameCode, "UBRP", 4) == 0);
10351056

10361057
arm9_extendedMemory = (dsiModeConfirmed || isDSBrowser);
10371058
if (!arm9_extendedMemory) {
10381059
tonccpy((u32*)0x023FF000, (u32*)(isSdk5(moduleParams) ? 0x02FFF000 : 0x027FF000), 0x1000);
10391060
}
10401061

1041-
my_readUserSettings(ndsHeader); // Header has to be loaded first
1042-
10431062
if (my_isDSiMode()) {
10441063
if ((REG_SNDEXTCNT & SNDEXTCNT_ENABLE) && ((!soundFreq && (REG_SNDEXTCNT & BIT(13))) || (soundFreq && !(REG_SNDEXTCNT & BIT(13))))) {
10451064
if (soundFreq) {

slot1launch/bootloader/source/module_params.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
#include <nds/ndstypes.h>
55

6+
// Not precise, since it doesn't really matter...
7+
#define FIRST_SDK5_VERSION 0x05000000
8+
#define LAST_NON_SDK5_VERSION (FIRST_SDK5_VERSION - 1)
9+
610
typedef struct {
711
u32 auto_load_list_offset;
812
u32 auto_load_list_end;
@@ -16,7 +20,7 @@ typedef struct {
1620
} module_params_t;
1721

1822
inline bool isSdk5(const module_params_t* moduleParams) {
19-
return (moduleParams->sdk_version > 0x5000000);
23+
return (moduleParams == NULL) || (moduleParams->sdk_version >= FIRST_SDK5_VERSION);
2024
}
2125

2226
#endif // MODULE_PARAMS_H

slot1launch/bootloader/source/read_card.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ int cardInit (sNDSHeaderExt* ndsHeader, u32* chipID)
273273
CARD_ACTIVATE | CARD_nRESET | CARD_CLK_SLOW | CARD_BLK_SIZE(1) | CARD_DELAY1(0x1FFF) | CARD_DELAY2(0x3F),
274274
NULL, 0);
275275

276-
*chipID=cardReadID(CARD_CLK_SLOW);
276+
*chipID=cardReadID(CARD_CLK_SLOW);
277+
normalChip = ((*chipID) & 0x80000000) != 0; // ROM chip ID MSB
277278
while (REG_ROMCTRL & CARD_BUSY);
278-
//u32 iCheapCard=iCardId&0x80000000;
279279

280280
// Read the header
281281
cardParamCommand (CARD_CMD_HEADER_READ, 0,
@@ -284,12 +284,22 @@ int cardInit (sNDSHeaderExt* ndsHeader, u32* chipID)
284284

285285
tonccpy(ndsHeader, headerData, 0x200);
286286

287-
if ((ndsHeader->unitCode != 0) || (ndsHeader->dsi_flags != 0)) {
287+
if((ndsHeader->unitCode != 0) || (ndsHeader->dsi_flags != 0)) {
288288
// Extended header found
289-
for (i = 1; i < 8; i++) {
290-
cardParamCommand (CARD_CMD_HEADER_READ, i * 0x200,
291-
CARD_ACTIVATE | CARD_nRESET | CARD_CLK_SLOW | CARD_BLK_SIZE(4) | CARD_DELAY1(0x1FFF) | CARD_DELAY2(0x3F),
292-
(void*)headerData + (i * 0x200), 0x200/sizeof(u32));
289+
if(normalChip) {
290+
// If 1T-ROM, read in blocks of 0x200 bytes, like the official DSi FW.
291+
// Also covers NAND.
292+
for (i = 1; i < 8; i++) {
293+
cardParamCommand (CARD_CMD_HEADER_READ, i * 0x200,
294+
CARD_ACTIVATE | CARD_nRESET | CARD_CLK_SLOW | CARD_BLK_SIZE(1) | CARD_DELAY1(0x1FFF) | CARD_DELAY2(0x3F),
295+
(void*)headerData + (i * 0x200), 0x200/sizeof(u32));
296+
}
297+
}
298+
else {
299+
// If MROM, read 0x1000 bytes, like the official DSi FW.
300+
cardParamCommand (CARD_CMD_HEADER_READ, 0,
301+
CARD_ACTIVATE | CARD_nRESET | CARD_CLK_SLOW | CARD_BLK_SIZE(4) | CARD_DELAY1(0x1FFF) | CARD_DELAY2(0x3F),
302+
(void*)headerData, 0x1000/sizeof(u32));
293303
}
294304
if (ndsHeader->dsi1[0]==0xFFFFFFFF && ndsHeader->dsi1[1]==0xFFFFFFFF
295305
&& ndsHeader->dsi1[2]==0xFFFFFFFF && ndsHeader->dsi1[3]==0xFFFFFFFF) {
@@ -321,7 +331,6 @@ int cardInit (sNDSHeaderExt* ndsHeader, u32* chipID)
321331
((ndsHeader->cardControlBF & (CARD_CLK_SLOW|CARD_DELAY1(0x1FFF))) + ((ndsHeader->cardControlBF & CARD_DELAY2(0x3F)) >> 16));
322332

323333
// Adjust card transfer method depending on the most significant bit of the chip ID
324-
normalChip = ((*chipID) & 0x80000000) != 0; // ROM chip ID MSB
325334
if (!normalChip) {
326335
portFlagsKey1 |= CARD_SEC_LARGE;
327336
}

0 commit comments

Comments
 (0)