2012-01-21 21:57:41 +01:00
|
|
|
#include <gccore.h>
|
|
|
|
#include <ogcsys.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ogc/machine/processor.h>
|
2012-12-08 18:30:00 +01:00
|
|
|
#include <ogc/lwp_threads.h>
|
2012-05-06 14:03:43 +02:00
|
|
|
#include <vector>
|
2012-01-21 21:57:41 +01:00
|
|
|
#include <string>
|
2012-12-08 18:30:00 +01:00
|
|
|
#include "homebrew.h"
|
2019-01-16 19:54:51 +01:00
|
|
|
#include "defines.h"
|
2018-11-03 18:10:34 +01:00
|
|
|
#include "loader/sys.h"
|
2013-01-06 19:33:29 +01:00
|
|
|
#include "banner/AnimatedBanner.h"
|
2013-11-19 16:27:52 +01:00
|
|
|
#include "fileOps/fileOps.h"
|
2012-12-08 17:17:35 +01:00
|
|
|
#include "gecko/gecko.hpp"
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2014-03-24 19:59:24 +01:00
|
|
|
static u8 *EXECUTE_ADDR = (u8*)0x92000000;
|
2012-12-08 19:45:48 +01:00
|
|
|
|
2014-03-24 19:59:24 +01:00
|
|
|
static u8 *BOOTER_ADDR = (u8*)0x93300000;
|
|
|
|
static entry BOOTER_ENTRY = (entry)BOOTER_ADDR;
|
|
|
|
|
2014-06-24 15:33:54 +02:00
|
|
|
static __argv *ARGS_ADDR = (__argv*)0x93300800; //more than twice as much as the appbooter, just for safety
|
2014-03-24 19:59:24 +01:00
|
|
|
static char *CMD_ADDR = (char*)ARGS_ADDR + sizeof(struct __argv);
|
|
|
|
|
2014-06-24 15:33:54 +02:00
|
|
|
char *homebrew_ptr = NULL;
|
|
|
|
u32 homebrew_size = 0;
|
|
|
|
|
2014-03-24 19:59:24 +01:00
|
|
|
u8 *appbooter_ptr = NULL;
|
|
|
|
u32 appbooter_size = 0;
|
2012-05-06 14:03:43 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2018-11-03 18:10:34 +01:00
|
|
|
extern const u8 wfstub_bin[];
|
|
|
|
extern const u32 wfstub_bin_size;
|
2012-05-23 18:27:55 +02:00
|
|
|
extern const u8 stub_bin[];
|
|
|
|
extern const u32 stub_bin_size;
|
|
|
|
|
2013-01-21 18:12:18 +01:00
|
|
|
u8 valid = 0;
|
2012-07-10 02:18:37 +02:00
|
|
|
|
2012-05-06 14:03:43 +02:00
|
|
|
static vector<string> Arguments;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-08-17 20:28:39 +02:00
|
|
|
static bool IsDollZ(u8 *buf)
|
2012-01-24 23:09:33 +01:00
|
|
|
{
|
2012-08-17 20:28:39 +02:00
|
|
|
u8 cmp1[] = {0x3C};
|
|
|
|
return memcmp(&buf[0x100], cmp1, sizeof(cmp1)) == 0;
|
|
|
|
}
|
2012-04-06 12:16:01 +02:00
|
|
|
|
2012-08-17 20:28:39 +02:00
|
|
|
static bool IsSpecialELF(u8 *buf)
|
|
|
|
{
|
|
|
|
u32 cmp1[] = {0x7F454C46};
|
|
|
|
u8 cmp2[] = {0x00};
|
|
|
|
return memcmp(buf, cmp1, sizeof(cmp1)) == 0 && memcmp(&buf[0x24], cmp2, sizeof(cmp2)) == 0;
|
2012-01-24 23:09:33 +01:00
|
|
|
}
|
|
|
|
|
2014-06-24 15:33:54 +02:00
|
|
|
void AddBootArgument(const char *argv)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-05-19 18:06:06 +02:00
|
|
|
string arg(argv);
|
2012-01-21 21:57:41 +01:00
|
|
|
Arguments.push_back(arg);
|
|
|
|
}
|
|
|
|
|
2014-06-24 15:33:54 +02:00
|
|
|
void AddBootArgument(const char *argv, unsigned int size)
|
|
|
|
{
|
|
|
|
string arg(argv, size);
|
|
|
|
Arguments.push_back(arg);
|
|
|
|
}
|
|
|
|
|
2013-11-19 16:27:52 +01:00
|
|
|
bool LoadAppBooter(const char *filepath)
|
|
|
|
{
|
2014-03-30 23:45:47 +02:00
|
|
|
u8 *tmp_ptr = fsop_ReadFile(filepath, &appbooter_size);
|
|
|
|
if(appbooter_size == 0 || tmp_ptr == NULL)
|
2014-03-24 19:59:24 +01:00
|
|
|
return false;
|
2014-03-30 23:45:47 +02:00
|
|
|
appbooter_ptr = (u8*)MEM2_lo_alloc(appbooter_size); /* safety because upper mem2 is dol */
|
|
|
|
if(appbooter_ptr == NULL)
|
|
|
|
{
|
|
|
|
free(tmp_ptr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memcpy(appbooter_ptr, tmp_ptr, appbooter_size);
|
|
|
|
DCFlushRange(appbooter_ptr, appbooter_size);
|
|
|
|
free(tmp_ptr);
|
2014-03-24 19:59:24 +01:00
|
|
|
return true;
|
2013-11-19 16:27:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LoadHomebrew(const char *filepath)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2013-01-21 18:12:18 +01:00
|
|
|
if(filepath == NULL)
|
2013-11-19 16:27:52 +01:00
|
|
|
return false;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2013-11-19 16:27:52 +01:00
|
|
|
u32 filesize = 0;
|
|
|
|
fsop_GetFileSizeBytes(filepath, &filesize);
|
|
|
|
if(filesize == 0)
|
|
|
|
return false;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2013-01-21 18:12:18 +01:00
|
|
|
if(filesize <= ((u32)BOOTER_ADDR - (u32)EXECUTE_ADDR))
|
|
|
|
{
|
2013-11-19 16:27:52 +01:00
|
|
|
fsop_ReadFileLoc(filepath, filesize, EXECUTE_ADDR);
|
2013-01-21 18:12:18 +01:00
|
|
|
DCFlushRange(EXECUTE_ADDR, filesize);
|
2014-06-24 15:33:54 +02:00
|
|
|
homebrew_ptr = (char*)EXECUTE_ADDR;
|
|
|
|
homebrew_size = filesize;
|
2013-01-21 18:12:18 +01:00
|
|
|
}
|
2013-11-19 16:27:52 +01:00
|
|
|
return true;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-06-24 15:33:54 +02:00
|
|
|
char *GetHomebrew(unsigned int *size)
|
|
|
|
{
|
|
|
|
*size = homebrew_size;
|
|
|
|
return homebrew_ptr;
|
|
|
|
}
|
|
|
|
|
2014-03-24 19:59:24 +01:00
|
|
|
static int SetupARGV()
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2014-03-24 19:59:24 +01:00
|
|
|
__argv *args = ARGS_ADDR;
|
2013-02-07 13:25:39 +01:00
|
|
|
memset(args, 0, sizeof(struct __argv));
|
2012-01-21 21:57:41 +01:00
|
|
|
args->argvMagic = ARGV_MAGIC;
|
|
|
|
|
|
|
|
u32 position = 0;
|
|
|
|
|
2014-03-24 19:59:24 +01:00
|
|
|
/** Count Arguments Size **/
|
|
|
|
u32 stringlength = 1;
|
2012-01-21 21:57:41 +01:00
|
|
|
for(u32 i = 0; i < Arguments.size(); i++)
|
|
|
|
stringlength += Arguments[i].size()+1;
|
|
|
|
args->length = stringlength;
|
|
|
|
|
|
|
|
/** Append Arguments **/
|
2014-03-24 19:59:24 +01:00
|
|
|
args->argc = Arguments.size();
|
|
|
|
args->commandLine = CMD_ADDR;
|
|
|
|
for(int i = 0; i < args->argc; i++)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2014-06-24 15:33:54 +02:00
|
|
|
memcpy(&args->commandLine[position], Arguments[i].c_str(), Arguments[i].size() + 1);
|
2012-01-21 21:57:41 +01:00
|
|
|
position += Arguments[i].size() + 1;
|
|
|
|
}
|
|
|
|
args->commandLine[args->length - 1] = '\0';
|
|
|
|
Arguments.clear();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-28 18:41:48 +02:00
|
|
|
void writeStub()
|
2012-05-27 18:37:52 +02:00
|
|
|
{
|
|
|
|
/* Clear potential homebrew channel stub */
|
|
|
|
memset((void*)0x80001800, 0, 0x1800);
|
|
|
|
|
2013-01-06 19:33:29 +01:00
|
|
|
/* Extract our stub */
|
|
|
|
u32 StubSize = 0;
|
2018-11-03 18:10:34 +01:00
|
|
|
#ifdef APP_WIIFLOW
|
|
|
|
u8 *Stub = DecompressCopy(wfstub_bin, wfstub_bin_size, &StubSize);
|
|
|
|
#else
|
2013-01-06 19:33:29 +01:00
|
|
|
u8 *Stub = DecompressCopy(stub_bin, stub_bin_size, &StubSize);
|
2018-11-03 18:10:34 +01:00
|
|
|
#endif
|
2013-01-06 19:33:29 +01:00
|
|
|
|
2012-05-27 18:37:52 +02:00
|
|
|
/* Copy our own stub into memory */
|
2013-01-06 19:33:29 +01:00
|
|
|
memcpy((void*)0x80001800, Stub, StubSize);
|
|
|
|
DCFlushRange((void*)0x80001800, StubSize);
|
|
|
|
|
|
|
|
/* And free the memory again */
|
2018-11-26 00:37:55 +01:00
|
|
|
#ifdef APP_WIIFLOW
|
|
|
|
if(Stub != wfstub_bin)
|
|
|
|
#else
|
|
|
|
if(Stub != stub_bin)
|
|
|
|
#endif
|
2014-03-01 00:11:02 +01:00
|
|
|
free(Stub);
|
2012-05-27 18:37:52 +02:00
|
|
|
}
|
|
|
|
|
2012-12-08 19:45:48 +01:00
|
|
|
void BootHomebrew()
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-08-17 20:28:39 +02:00
|
|
|
if(!IsDollZ(EXECUTE_ADDR) && !IsSpecialELF(EXECUTE_ADDR))
|
2014-03-24 19:59:24 +01:00
|
|
|
SetupARGV();
|
2012-08-17 20:28:39 +02:00
|
|
|
else
|
|
|
|
gprintf("Homebrew Boot Arguments disabled\n");
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2014-03-24 19:59:24 +01:00
|
|
|
memcpy(BOOTER_ADDR, appbooter_ptr, appbooter_size);
|
|
|
|
DCFlushRange(BOOTER_ADDR, appbooter_size);
|
2012-02-11 23:12:58 +01:00
|
|
|
|
2012-12-08 19:45:48 +01:00
|
|
|
JumpToEntry(BOOTER_ENTRY);
|
2012-04-06 12:16:01 +02:00
|
|
|
}
|
2012-12-08 18:30:00 +01:00
|
|
|
|
2014-03-30 23:45:47 +02:00
|
|
|
extern "C" { extern void __exception_closeall(); }
|
|
|
|
u32 AppEntrypoint = 0;
|
2012-12-08 19:45:48 +01:00
|
|
|
void JumpToEntry(entry EntryPoint)
|
2012-12-08 18:30:00 +01:00
|
|
|
{
|
2014-03-30 23:45:47 +02:00
|
|
|
AppEntrypoint = (u32)EntryPoint;
|
|
|
|
gprintf("Jumping to %08x\n", AppEntrypoint);
|
|
|
|
u32 level = IRQ_Disable();
|
|
|
|
__IOS_ShutdownSubsystems();
|
|
|
|
__exception_closeall();
|
2014-03-31 16:33:24 +02:00
|
|
|
__lwp_thread_closeall(); //dont like it but whatever
|
2014-03-30 23:45:47 +02:00
|
|
|
asm volatile (
|
|
|
|
"lis %r3, AppEntrypoint@h\n"
|
|
|
|
"ori %r3, %r3, AppEntrypoint@l\n"
|
|
|
|
"lwz %r3, 0(%r3)\n"
|
|
|
|
"mtlr %r3\n"
|
|
|
|
"blr\n"
|
|
|
|
);
|
|
|
|
IRQ_Restore(level);
|
|
|
|
}
|