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-05-06 14:03:43 +02:00
|
|
|
#include <vector>
|
2012-01-21 21:57:41 +01:00
|
|
|
#include <string>
|
2012-08-05 15:48:15 +02:00
|
|
|
#include "gecko/gecko.h"
|
|
|
|
#include "memory/mem2.hpp"
|
|
|
|
#include "memory/smartptr.hpp"
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-26 13:40:31 +02:00
|
|
|
#define EXECUTE_ADDR ((u8 *)0x92000000)
|
|
|
|
#define BOOTER_ADDR ((u8 *)0x93000000)
|
|
|
|
#define ARGS_ADDR ((u8 *)0x93200000)
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-06 14:03:43 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2012-01-21 21:57:41 +01:00
|
|
|
extern const u8 app_booter_bin[];
|
|
|
|
extern const u32 app_booter_bin_size;
|
|
|
|
|
2012-05-23 18:27:55 +02:00
|
|
|
extern const u8 stub_bin[];
|
|
|
|
extern const u32 stub_bin_size;
|
|
|
|
|
2012-01-21 21:57:41 +01:00
|
|
|
typedef void (*entrypoint) (void);
|
|
|
|
extern "C" { void __exception_closeall(); }
|
|
|
|
|
2012-07-10 02:18:37 +02:00
|
|
|
u32 buffer_size = 0;
|
|
|
|
|
2012-05-06 14:03:43 +02:00
|
|
|
static vector<string> Arguments;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-01-24 23:09:33 +01:00
|
|
|
bool IsDollZ (u8 *buff)
|
|
|
|
{
|
2012-04-06 12:16:01 +02:00
|
|
|
u8 dollz_stamp[] = {0x3C};
|
|
|
|
int dollz_offs = 0x100;
|
2012-01-24 23:09:33 +01:00
|
|
|
|
2012-04-06 12:16:01 +02:00
|
|
|
int ret = memcmp (&buff[dollz_offs], dollz_stamp, sizeof(dollz_stamp));
|
|
|
|
if (ret == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2012-01-24 23:09:33 +01:00
|
|
|
}
|
|
|
|
|
2012-01-21 21:57:41 +01:00
|
|
|
void AddBootArgument(const char * argv)
|
|
|
|
{
|
2012-05-19 18:06:06 +02:00
|
|
|
string arg(argv);
|
2012-01-21 21:57:41 +01:00
|
|
|
Arguments.push_back(arg);
|
|
|
|
}
|
|
|
|
|
2012-05-19 18:06:06 +02:00
|
|
|
int LoadHomebrew(const char *filepath)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-04-06 12:16:01 +02:00
|
|
|
if(!filepath)
|
|
|
|
return -1;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
FILE *file = fopen(filepath ,"rb");
|
2012-04-06 12:16:01 +02:00
|
|
|
if(!file)
|
|
|
|
return -2;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
u32 filesize = ftell(file);
|
|
|
|
rewind(file);
|
|
|
|
|
2012-07-10 02:18:37 +02:00
|
|
|
buffer_size = filesize;
|
|
|
|
fread(EXECUTE_ADDR, 1, buffer_size, file);
|
|
|
|
DCFlushRange(EXECUTE_ADDR, buffer_size);
|
2012-05-13 17:13:33 +02:00
|
|
|
fclose(file);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-04-06 12:16:01 +02:00
|
|
|
return 1;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int SetupARGV(struct __argv * args)
|
|
|
|
{
|
2012-04-06 12:16:01 +02:00
|
|
|
if(!args)
|
|
|
|
return -1;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
bzero(args, sizeof(struct __argv));
|
|
|
|
args->argvMagic = ARGV_MAGIC;
|
|
|
|
|
|
|
|
u32 argc = 0;
|
|
|
|
u32 position = 0;
|
|
|
|
u32 stringlength = 1;
|
|
|
|
|
|
|
|
/** Append Arguments **/
|
|
|
|
for(u32 i = 0; i < Arguments.size(); i++)
|
|
|
|
stringlength += Arguments[i].size()+1;
|
|
|
|
|
|
|
|
args->length = stringlength;
|
|
|
|
//! Put the argument into mem2 too, to avoid overwriting it
|
|
|
|
args->commandLine = (char *) ARGS_ADDR + sizeof(struct __argv);
|
|
|
|
|
|
|
|
/** Append Arguments **/
|
|
|
|
for(u32 i = 0; i < Arguments.size(); i++)
|
|
|
|
{
|
|
|
|
strcpy(&args->commandLine[position], Arguments[i].c_str());
|
|
|
|
position += Arguments[i].size() + 1;
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
args->argc = argc;
|
|
|
|
|
|
|
|
args->commandLine[args->length - 1] = '\0';
|
|
|
|
args->argv = &args->commandLine;
|
|
|
|
args->endARGV = args->argv + 1;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* Copy our own stub into memory */
|
|
|
|
memcpy((void*)0x80001800, stub_bin, stub_bin_size);
|
|
|
|
DCFlushRange((void*)0x80001800, stub_bin_size);
|
|
|
|
}
|
|
|
|
|
2012-07-28 18:41:48 +02:00
|
|
|
int BootHomebrew()
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
struct __argv args;
|
2012-07-10 02:18:37 +02:00
|
|
|
if (!IsDollZ(EXECUTE_ADDR))
|
2012-01-24 23:09:33 +01:00
|
|
|
SetupARGV(&args);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
memcpy(BOOTER_ADDR, app_booter_bin, app_booter_bin_size);
|
|
|
|
DCFlushRange(BOOTER_ADDR, app_booter_bin_size);
|
|
|
|
|
2012-07-19 20:19:28 +02:00
|
|
|
entrypoint exeEntryPoint = (entrypoint)BOOTER_ADDR;
|
|
|
|
u32 cookie;
|
2012-02-11 23:12:58 +01:00
|
|
|
|
|
|
|
memmove(ARGS_ADDR, &args, sizeof(args));
|
2012-01-24 23:09:33 +01:00
|
|
|
DCFlushRange(ARGS_ADDR, sizeof(args) + args.length);
|
2012-02-11 23:12:58 +01:00
|
|
|
|
2012-07-19 20:19:28 +02:00
|
|
|
/* cleaning up and load dol */
|
|
|
|
SYS_ResetSystem(SYS_SHUTDOWN, 0, 0);
|
|
|
|
_CPU_ISR_Disable(cookie);
|
2012-07-17 13:30:08 +02:00
|
|
|
__exception_closeall();
|
2012-07-19 20:19:28 +02:00
|
|
|
exeEntryPoint();
|
|
|
|
_CPU_ISR_Restore(cookie);
|
2012-01-21 21:57:41 +01:00
|
|
|
return 0;
|
2012-04-06 12:16:01 +02:00
|
|
|
}
|