2012-08-05 15:48:15 +02:00
|
|
|
|
2012-08-12 23:26:24 +02:00
|
|
|
#include "apploader.h"
|
2012-01-21 21:57:41 +01:00
|
|
|
#include "patchcode.h"
|
2012-07-16 16:05:57 +02:00
|
|
|
#include "cios.h"
|
2012-08-12 23:26:24 +02:00
|
|
|
#include "fst.h"
|
|
|
|
#include "wip.h"
|
2012-10-13 00:25:22 +02:00
|
|
|
#include "debug.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "di.h"
|
|
|
|
#include "cache.h"
|
|
|
|
#include "config.h"
|
2012-08-12 23:26:24 +02:00
|
|
|
|
|
|
|
/* Apploader function pointers */
|
|
|
|
typedef int (*app_main)(void **dst, int *size, int *offset);
|
|
|
|
typedef void (*app_init)(void (*report)(const char *fmt, ...));
|
|
|
|
typedef void *(*app_final)();
|
|
|
|
typedef void (*app_entry)(void (**init)(void (*report)(const char *fmt, ...)), int (**main)(), void *(**final)());
|
2012-05-16 00:34:57 +02:00
|
|
|
|
|
|
|
/* Apploader pointers */
|
2012-07-26 00:12:17 +02:00
|
|
|
static u8 *appldr = (u8 *)0x81200000;
|
2012-08-12 23:26:24 +02:00
|
|
|
|
|
|
|
/* Variables */
|
2012-10-13 00:25:22 +02:00
|
|
|
static u32 buffer[0x20] __attribute__((aligned(32)));
|
2012-05-16 21:29:53 +02:00
|
|
|
|
2012-10-13 00:25:22 +02:00
|
|
|
void maindolpatches(void *dst, int len, u8 vidMode, u8 vipatch, u8 countryString, u8 patchVidModes, int aspectRatio, u32 returnTo);
|
2012-07-16 16:05:57 +02:00
|
|
|
static void patch_NoDiscinDrive(void *buffer, u32 len);
|
|
|
|
static void Anti_002_fix(void *Address, int Size);
|
2012-10-13 00:25:22 +02:00
|
|
|
static u8 Remove_001_Protection(void *Address, int Size);
|
|
|
|
//static u8 PrinceOfPersiaPatch();
|
|
|
|
//static u8 NewSuperMarioBrosPatch();
|
|
|
|
u8 hookpatched = 0;
|
2012-08-12 23:26:24 +02:00
|
|
|
|
2012-10-13 00:25:22 +02:00
|
|
|
static void simple_report(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
debug_string(fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 Apploader_Run(u8 vidMode, u8 vipatch, u8 countryString, u8 patchVidModes, int aspectRatio, u32 returnTo)
|
2012-05-16 21:29:53 +02:00
|
|
|
{
|
2012-08-12 23:26:24 +02:00
|
|
|
void *dst = NULL;
|
|
|
|
int len = 0;
|
|
|
|
int offset = 0;
|
2012-05-13 17:13:33 +02:00
|
|
|
u32 appldr_len;
|
|
|
|
s32 ret;
|
2012-10-13 00:25:22 +02:00
|
|
|
u32 Entry = 0;
|
2012-08-12 23:26:24 +02:00
|
|
|
app_init appldr_init;
|
|
|
|
app_main appldr_main;
|
2012-05-13 17:13:33 +02:00
|
|
|
app_final appldr_final;
|
2012-08-12 23:26:24 +02:00
|
|
|
|
2012-05-13 17:13:33 +02:00
|
|
|
/* Read apploader header */
|
2012-10-13 00:25:22 +02:00
|
|
|
ret = di_read(buffer, 0x20, 0x910);
|
2012-08-10 15:47:05 +02:00
|
|
|
if(ret < 0)
|
2012-10-13 00:25:22 +02:00
|
|
|
return Entry;
|
2012-05-13 17:13:33 +02:00
|
|
|
|
|
|
|
/* Calculate apploader length */
|
|
|
|
appldr_len = buffer[5] + buffer[6];
|
2012-08-12 23:26:24 +02:00
|
|
|
|
2012-05-13 17:13:33 +02:00
|
|
|
/* Read apploader code */
|
2012-10-13 00:25:22 +02:00
|
|
|
ret = di_read(appldr, appldr_len, 0x918);
|
2012-07-26 00:12:17 +02:00
|
|
|
if(ret < 0)
|
2012-08-12 23:26:24 +02:00
|
|
|
return ret;
|
2012-08-10 15:47:05 +02:00
|
|
|
|
2012-08-12 23:26:24 +02:00
|
|
|
/* Flush into memory */
|
2012-10-13 00:25:22 +02:00
|
|
|
sync_after_write(appldr, appldr_len);
|
2012-08-12 23:26:24 +02:00
|
|
|
|
|
|
|
/* Set apploader entry function */
|
|
|
|
app_entry appldr_entry = (app_entry)buffer[4];
|
|
|
|
|
|
|
|
/* Call apploader entry */
|
|
|
|
appldr_entry(&appldr_init, &appldr_main, &appldr_final);
|
|
|
|
|
|
|
|
/* Initialize apploader */
|
2012-10-13 00:25:22 +02:00
|
|
|
appldr_init(simple_report);
|
2012-08-12 23:26:24 +02:00
|
|
|
|
|
|
|
while(appldr_main(&dst, &len, &offset))
|
2012-05-16 21:29:53 +02:00
|
|
|
{
|
|
|
|
/* Read data from DVD */
|
2012-10-13 00:25:22 +02:00
|
|
|
di_read(dst, len, offset);
|
|
|
|
maindolpatches(dst, len, vidMode, vipatch, countryString, patchVidModes, aspectRatio, returnTo);
|
|
|
|
sync_after_write(dst, len);
|
2012-05-16 21:29:53 +02:00
|
|
|
}
|
2012-08-12 23:26:24 +02:00
|
|
|
|
2012-10-13 00:25:22 +02:00
|
|
|
/*free_wip();
|
2012-08-12 23:26:24 +02:00
|
|
|
if(hooktype != 0 && hookpatched)
|
2012-10-13 00:25:22 +02:00
|
|
|
ocarina_do_code(0);*/
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
/* Set entry point from apploader */
|
2012-10-13 00:25:22 +02:00
|
|
|
//*entry = appldr_final();
|
|
|
|
Entry = (u32)appldr_final();
|
2012-01-22 18:06:56 +01:00
|
|
|
|
2012-10-13 00:25:22 +02:00
|
|
|
return Entry;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-10-13 00:25:22 +02:00
|
|
|
void maindolpatches(void *dst, int len, u8 vidMode, u8 vipatch, u8 countryString, u8 patchVidModes, int aspectRatio, u32 returnTo)
|
2012-05-16 21:29:53 +02:00
|
|
|
{
|
2012-10-13 00:25:22 +02:00
|
|
|
//PrinceOfPersiaPatch();
|
|
|
|
//NewSuperMarioBrosPatch();
|
2012-07-16 16:05:57 +02:00
|
|
|
// Patch NoDiscInDrive only for IOS 249 < rev13 or IOS 222/223/224
|
2012-08-11 14:27:38 +02:00
|
|
|
if((CurrentIOS.Type == IOS_TYPE_WANIN && CurrentIOS.Revision < 13) || CurrentIOS.Type == IOS_TYPE_HERMES)
|
2012-07-16 16:05:57 +02:00
|
|
|
patch_NoDiscinDrive(dst, len);
|
2012-10-13 00:25:22 +02:00
|
|
|
//patchVideoModes(dst, len, vidMode, vmode, patchVidModes);
|
|
|
|
//if(conf->hooktype != 0 && dogamehooks(dst, len, 0))
|
|
|
|
// hookpatched = 1;
|
2012-05-16 21:29:53 +02:00
|
|
|
if(vipatch)
|
|
|
|
vidolpatcher(dst, len);
|
|
|
|
if(configbytes[0] != 0xCD)
|
|
|
|
langpatcher(dst, len);
|
2012-08-11 14:27:38 +02:00
|
|
|
if(CurrentIOS.Type == IOS_TYPE_WANIN && CurrentIOS.Revision < 13)
|
2012-07-16 16:05:57 +02:00
|
|
|
Anti_002_fix(dst, len);
|
2012-10-13 00:25:22 +02:00
|
|
|
//if(conf->countryString)
|
|
|
|
// PatchCountryStrings(dst, len); // Country Patch by WiiPower
|
2012-05-16 21:29:53 +02:00
|
|
|
if(aspectRatio != -1)
|
|
|
|
PatchAspectRatio(dst, len, aspectRatio);
|
2012-10-13 00:25:22 +02:00
|
|
|
if(returnTo > 0)
|
2012-07-16 16:05:57 +02:00
|
|
|
PatchReturnTo(dst, len, returnTo);
|
2012-05-16 21:29:53 +02:00
|
|
|
Remove_001_Protection(dst, len);
|
2012-10-13 00:25:22 +02:00
|
|
|
//do_wip_code((u8 *)dst, len);
|
2012-05-16 21:29:53 +02:00
|
|
|
}
|
|
|
|
|
2012-07-16 16:05:57 +02:00
|
|
|
static void patch_NoDiscinDrive(void *buffer, u32 len)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-07-16 16:05:57 +02:00
|
|
|
static const u8 oldcode[] = {0x54, 0x60, 0xF7, 0xFF, 0x40, 0x82, 0x00, 0x0C, 0x54, 0x60, 0x07, 0xFF, 0x41, 0x82, 0x00, 0x0C};
|
|
|
|
static const u8 newcode[] = {0x54, 0x60, 0xF7, 0xFF, 0x40, 0x82, 0x00, 0x0C, 0x54, 0x60, 0x07, 0xFF, 0x48, 0x00, 0x00, 0x0C};
|
|
|
|
u32 n;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-16 16:05:57 +02:00
|
|
|
/* Patch cover register */
|
|
|
|
for(n = 0; n < len - sizeof oldcode; n += 4) // n is not 4 aligned here, so you can get an out of buffer thing
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-07-16 16:05:57 +02:00
|
|
|
if (memcmp(buffer + n, (void *)oldcode, sizeof oldcode) == 0)
|
|
|
|
memcpy(buffer + n, (void *)newcode, sizeof newcode);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-07-16 16:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Anti_002_fix(void *Address, int Size)
|
|
|
|
{
|
|
|
|
static const u8 SearchPattern[] = {0x2C, 0x00, 0x00, 0x00, 0x48, 0x00, 0x02, 0x14, 0x3C, 0x60, 0x80, 0x00};
|
|
|
|
static const u8 PatchData[] = {0x2C, 0x00, 0x00, 0x00, 0x40, 0x82, 0x02, 0x14, 0x3C, 0x60, 0x80, 0x00};
|
|
|
|
void *Addr = Address;
|
|
|
|
void *Addr_end = Address + Size;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-16 16:05:57 +02:00
|
|
|
while(Addr <= Addr_end - sizeof SearchPattern)
|
|
|
|
{
|
|
|
|
if(memcmp(Addr, SearchPattern, sizeof SearchPattern) == 0)
|
|
|
|
memcpy(Addr, PatchData, sizeof PatchData);
|
|
|
|
Addr += 4;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
2012-10-13 00:25:22 +02:00
|
|
|
/*
|
|
|
|
static u8 PrinceOfPersiaPatch()
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-05-16 21:29:53 +02:00
|
|
|
if (memcmp("SPX", (char *) 0x80000000, 3) != 0 && memcmp("RPW", (char *) 0x80000000, 3) != 0)
|
2012-10-13 00:25:22 +02:00
|
|
|
return 0;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-10-13 00:25:22 +02:00
|
|
|
WIP_Code CodeList[5*sizeof(WIP_Code)];
|
2012-05-16 21:29:53 +02:00
|
|
|
CodeList[0].offset = 0x007AAC6A;
|
|
|
|
CodeList[0].srcaddress = 0x7A6B6F6A;
|
|
|
|
CodeList[0].dstaddress = 0x6F6A7A6B;
|
|
|
|
CodeList[1].offset = 0x007AAC75;
|
|
|
|
CodeList[1].srcaddress = 0x7C7A6939;
|
|
|
|
CodeList[1].dstaddress = 0x69397C7A;
|
|
|
|
CodeList[2].offset = 0x007AAC82;
|
|
|
|
CodeList[2].srcaddress = 0x7376686B;
|
|
|
|
CodeList[2].dstaddress = 0x686B7376;
|
|
|
|
CodeList[3].offset = 0x007AAC92;
|
|
|
|
CodeList[3].srcaddress = 0x80717570;
|
|
|
|
CodeList[3].dstaddress = 0x75708071;
|
|
|
|
CodeList[4].offset = 0x007AAC9D;
|
|
|
|
CodeList[4].srcaddress = 0x82806F3F;
|
|
|
|
CodeList[4].dstaddress = 0x6F3F8280;
|
|
|
|
|
2012-10-13 00:25:22 +02:00
|
|
|
if (set_wip_list(CodeList, 5) == 0)
|
|
|
|
return 0;
|
2012-05-16 21:29:53 +02:00
|
|
|
|
2012-10-13 00:25:22 +02:00
|
|
|
return 1;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-10-13 00:25:22 +02:00
|
|
|
static u8 NewSuperMarioBrosPatch()
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-10-13 00:25:22 +02:00
|
|
|
WIP_Code CodeList[3 * sizeof(WIP_Code)];
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-16 21:29:53 +02:00
|
|
|
if (memcmp("SMNE01", (char *) 0x80000000, 6) == 0)
|
|
|
|
{
|
|
|
|
CodeList[0].offset = 0x001AB610;
|
|
|
|
CodeList[0].srcaddress = 0x9421FFD0;
|
|
|
|
CodeList[0].dstaddress = 0x4E800020;
|
|
|
|
CodeList[1].offset = 0x001CED53;
|
|
|
|
CodeList[1].srcaddress = 0xDA000000;
|
|
|
|
CodeList[1].dstaddress = 0x71000000;
|
|
|
|
CodeList[2].offset = 0x001CED6B;
|
|
|
|
CodeList[2].srcaddress = 0xDA000000;
|
|
|
|
CodeList[2].dstaddress = 0x71000000;
|
|
|
|
}
|
|
|
|
else if (memcmp("SMNP01", (char *) 0x80000000, 6) == 0)
|
|
|
|
{
|
|
|
|
CodeList[0].offset = 0x001AB750;
|
|
|
|
CodeList[0].srcaddress = 0x9421FFD0;
|
|
|
|
CodeList[0].dstaddress = 0x4E800020;
|
|
|
|
CodeList[1].offset = 0x001CEE90;
|
|
|
|
CodeList[1].srcaddress = 0x38A000DA;
|
|
|
|
CodeList[1].dstaddress = 0x38A00071;
|
|
|
|
CodeList[2].offset = 0x001CEEA8;
|
|
|
|
CodeList[2].srcaddress = 0x388000DA;
|
|
|
|
CodeList[2].dstaddress = 0x38800071;
|
|
|
|
}
|
|
|
|
else if (memcmp("SMNJ01", (char *) 0x80000000, 6) == 0)
|
|
|
|
{
|
|
|
|
CodeList[0].offset = 0x001AB420;
|
|
|
|
CodeList[0].srcaddress = 0x9421FFD0;
|
|
|
|
CodeList[0].dstaddress = 0x4E800020;
|
|
|
|
CodeList[1].offset = 0x001CEB63;
|
|
|
|
CodeList[1].srcaddress = 0xDA000000;
|
|
|
|
CodeList[1].dstaddress = 0x71000000;
|
|
|
|
CodeList[2].offset = 0x001CEB7B;
|
|
|
|
CodeList[2].srcaddress = 0xDA000000;
|
|
|
|
CodeList[2].dstaddress = 0x71000000;
|
|
|
|
}
|
2012-10-13 00:25:22 +02:00
|
|
|
if(set_wip_list(CodeList, 3) == 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-10-13 00:25:22 +02:00
|
|
|
*/
|
|
|
|
static u8 Remove_001_Protection(void *Address, int Size)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
static const u8 SearchPattern[] = {0x40, 0x82, 0x00, 0x0C, 0x38, 0x60, 0x00, 0x01, 0x48, 0x00, 0x02, 0x44, 0x38, 0x61, 0x00, 0x18};
|
|
|
|
static const u8 PatchData[] = {0x40, 0x82, 0x00, 0x04, 0x38, 0x60, 0x00, 0x01, 0x48, 0x00, 0x02, 0x44, 0x38, 0x61, 0x00, 0x18};
|
|
|
|
u8 *Addr_end = Address + Size;
|
|
|
|
u8 *Addr;
|
|
|
|
|
|
|
|
for (Addr = Address; Addr <= Addr_end - sizeof SearchPattern; Addr += 4)
|
2012-05-12 18:03:14 +02:00
|
|
|
{
|
2012-01-21 21:57:41 +01:00
|
|
|
if (memcmp(Addr, SearchPattern, sizeof SearchPattern) == 0)
|
|
|
|
{
|
|
|
|
memcpy(Addr, PatchData, sizeof PatchData);
|
2012-10-13 00:25:22 +02:00
|
|
|
return 1;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-05-12 18:03:14 +02:00
|
|
|
}
|
2012-10-13 00:25:22 +02:00
|
|
|
return 0;
|
2012-08-12 23:26:24 +02:00
|
|
|
}
|