WiiFlow_Lite/source/homebrew/homebrew.cpp
fix94.1 3b1dcf389a -added back some missing cover converting code for jpegs
-added some more debug information and more safety code
2014-03-24 18:59:24 +00:00

143 lines
3.1 KiB
C++

#include <gccore.h>
#include <ogcsys.h>
#include <stdio.h>
#include <string.h>
#include <ogc/machine/processor.h>
#include <ogc/lwp_threads.h>
#include <vector>
#include <string>
#include "homebrew.h"
#include "banner/AnimatedBanner.h"
#include "fileOps/fileOps.h"
#include "gecko/gecko.hpp"
static u8 *EXECUTE_ADDR = (u8*)0x92000000;
static u8 *BOOTER_ADDR = (u8*)0x93300000;
static entry BOOTER_ENTRY = (entry)BOOTER_ADDR;
static __argv *ARGS_ADDR = (__argv*)0x90100000;
static char *CMD_ADDR = (char*)ARGS_ADDR + sizeof(struct __argv);
u8 *appbooter_ptr = NULL;
u32 appbooter_size = 0;
using namespace std;
extern const u8 stub_bin[];
extern const u32 stub_bin_size;
u8 valid = 0;
static vector<string> Arguments;
static bool IsDollZ(u8 *buf)
{
u8 cmp1[] = {0x3C};
return memcmp(&buf[0x100], cmp1, sizeof(cmp1)) == 0;
}
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;
}
void AddBootArgument(const char * argv)
{
string arg(argv);
Arguments.push_back(arg);
}
bool LoadAppBooter(const char *filepath)
{
appbooter_ptr = fsop_ReadFile(filepath, &appbooter_size);
if(appbooter_size == 0 || appbooter_ptr == NULL)
return false;
return true;
}
bool LoadHomebrew(const char *filepath)
{
if(filepath == NULL)
return false;
u32 filesize = 0;
fsop_GetFileSizeBytes(filepath, &filesize);
if(filesize == 0)
return false;
if(filesize <= ((u32)BOOTER_ADDR - (u32)EXECUTE_ADDR))
{
fsop_ReadFileLoc(filepath, filesize, EXECUTE_ADDR);
DCFlushRange(EXECUTE_ADDR, filesize);
}
return true;
}
static int SetupARGV()
{
__argv *args = ARGS_ADDR;
memset(args, 0, sizeof(struct __argv));
args->argvMagic = ARGV_MAGIC;
u32 position = 0;
/** Count Arguments Size **/
u32 stringlength = 1;
for(u32 i = 0; i < Arguments.size(); i++)
stringlength += Arguments[i].size()+1;
args->length = stringlength;
/** Append Arguments **/
args->argc = Arguments.size();
args->commandLine = CMD_ADDR;
for(int i = 0; i < args->argc; i++)
{
strcpy(&args->commandLine[position], Arguments[i].c_str());
position += Arguments[i].size() + 1;
}
args->commandLine[args->length - 1] = '\0';
Arguments.clear();
return 0;
}
void writeStub()
{
/* Clear potential homebrew channel stub */
memset((void*)0x80001800, 0, 0x1800);
/* Extract our stub */
u32 StubSize = 0;
u8 *Stub = DecompressCopy(stub_bin, stub_bin_size, &StubSize);
/* Copy our own stub into memory */
memcpy((void*)0x80001800, Stub, StubSize);
DCFlushRange((void*)0x80001800, StubSize);
/* And free the memory again */
if(Stub != stub_bin)
free(Stub);
}
void BootHomebrew()
{
if(!IsDollZ(EXECUTE_ADDR) && !IsSpecialELF(EXECUTE_ADDR))
SetupARGV();
else
gprintf("Homebrew Boot Arguments disabled\n");
memcpy(BOOTER_ADDR, appbooter_ptr, appbooter_size);
DCFlushRange(BOOTER_ADDR, appbooter_size);
JumpToEntry(BOOTER_ENTRY);
}
void JumpToEntry(entry EntryPoint)
{
gprintf("Jumping to %08x\n", EntryPoint);
SYS_ResetSystem(SYS_SHUTDOWN, 0, 0);
__lwp_thread_stopmultitasking(EntryPoint);
}