2010-09-19 01:04:39 +02:00
|
|
|
#include <gccore.h>
|
|
|
|
#include <ogcsys.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ogc/machine/processor.h>
|
|
|
|
#include <wiiuse/wpad.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2010-12-31 00:49:22 +01:00
|
|
|
#include "Controls/DeviceHandler.hpp"
|
2011-07-23 21:49:51 +02:00
|
|
|
#include "settings/CSettings.h"
|
2012-01-08 19:24:46 +01:00
|
|
|
#include "system/IosLoader.h"
|
2011-07-23 21:49:51 +02:00
|
|
|
#include "lstub.h"
|
|
|
|
#include "sys.h"
|
|
|
|
#include "gecko.h"
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2021-08-01 19:00:15 +02:00
|
|
|
/* GCC 11 false positives */
|
|
|
|
#if __GNUC__ > 10
|
|
|
|
#pragma GCC diagnostic ignored "-Warray-bounds"
|
|
|
|
#pragma GCC diagnostic ignored "-Wstringop-overflow"
|
|
|
|
#endif
|
|
|
|
|
2011-07-23 21:49:51 +02:00
|
|
|
#define EXECUTE_ADDR ((u8 *) 0x92000000)
|
|
|
|
#define BOOTER_ADDR ((u8 *) 0x93000000)
|
|
|
|
#define ARGS_ADDR ((u8 *) 0x93200000)
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-23 21:49:51 +02:00
|
|
|
extern const u8 app_booter_bin[];
|
|
|
|
extern const u32 app_booter_bin_size;
|
|
|
|
|
|
|
|
typedef void (*entrypoint) (void);
|
|
|
|
extern "C" { void __exception_closeall(); }
|
|
|
|
|
|
|
|
static u8 *homebrewbuffer = EXECUTE_ADDR;
|
2010-11-06 16:30:14 +01:00
|
|
|
static u32 homebrewsize = 0;
|
2010-09-19 01:04:39 +02:00
|
|
|
static std::vector<std::string> Arguments;
|
|
|
|
|
2014-07-14 18:31:52 +02:00
|
|
|
void AddBootArgument(const char *argv)
|
2010-09-19 01:04:39 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
std::string arg(argv);
|
|
|
|
Arguments.push_back(arg);
|
2010-09-19 01:04:39 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 18:31:52 +02:00
|
|
|
void AddBootArgument(const char *argv, unsigned int size)
|
|
|
|
{
|
|
|
|
std::string arg(argv, size);
|
|
|
|
Arguments.push_back(arg);
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int CopyHomebrewMemory(u8 *temp, u32 pos, u32 len)
|
2010-09-19 01:04:39 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
homebrewsize += len;
|
|
|
|
memcpy((homebrewbuffer) + pos, temp, len);
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return 1;
|
2010-09-19 01:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreeHomebrewBuffer()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
homebrewbuffer = EXECUTE_ADDR;
|
|
|
|
homebrewsize = 0;
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
Arguments.clear();
|
2010-09-19 01:04:39 +02:00
|
|
|
}
|
|
|
|
|
2012-01-23 22:26:41 +01:00
|
|
|
static inline bool IsDollZ (const u8 *buf)
|
|
|
|
{
|
|
|
|
return (buf[0x100] == 0x3C);
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
static int SetupARGV(struct __argv * args)
|
2010-09-19 01:04:39 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!args) return -1;
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
bzero(args, sizeof(struct __argv));
|
|
|
|
args->argvMagic = ARGV_MAGIC;
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
u32 argc = 0;
|
|
|
|
u32 position = 0;
|
|
|
|
u32 stringlength = 1;
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
/** Append Arguments **/
|
|
|
|
for (u32 i = 0; i < Arguments.size(); i++)
|
|
|
|
{
|
|
|
|
stringlength += Arguments[i].size() + 1;
|
|
|
|
}
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
args->length = stringlength;
|
2011-07-25 19:50:03 +02:00
|
|
|
//! Put the argument into mem2 too, to avoid overwriting it
|
2011-07-26 00:28:22 +02:00
|
|
|
args->commandLine = (char *) ARGS_ADDR + sizeof(struct __argv);
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
/** Append Arguments **/
|
|
|
|
for (u32 i = 0; i < Arguments.size(); i++)
|
|
|
|
{
|
2014-07-14 18:31:52 +02:00
|
|
|
memcpy(&args->commandLine[position], Arguments[i].c_str(), Arguments[i].size() +1);
|
2011-07-26 00:28:22 +02:00
|
|
|
position += Arguments[i].size() + 1;
|
|
|
|
argc++;
|
|
|
|
}
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
args->argc = argc;
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
args->commandLine[args->length - 1] = '\0';
|
|
|
|
args->argv = &args->commandLine;
|
|
|
|
args->endARGV = args->argv + 1;
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
Arguments.clear();
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return 0;
|
2010-09-19 01:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int RunAppbooter()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if (homebrewsize == 0) return -1;
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
ExitApp();
|
2010-11-06 16:30:14 +01:00
|
|
|
|
2013-10-01 23:13:08 +02:00
|
|
|
// Reload IOS 58 if available, else reload Entry IOS
|
|
|
|
s32 ret = IosLoader::ReloadIosSafe(58);
|
|
|
|
if(ret < 0 && Settings.EntryIOS != IOS_GetVersion())
|
2012-01-08 19:24:46 +01:00
|
|
|
IosLoader::ReloadIosKeepingRights(Settings.EntryIOS);
|
2014-04-29 18:08:42 +02:00
|
|
|
gprintf("Reloaded to IOS%d\n", IOS_GetVersion());
|
2011-07-23 21:49:51 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
struct __argv args;
|
|
|
|
SetupARGV(&args);
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
u32 cpu_isr;
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2012-01-08 19:24:46 +01:00
|
|
|
DCFlushRange(homebrewbuffer, homebrewsize);
|
|
|
|
|
2011-07-23 21:49:51 +02:00
|
|
|
memcpy(BOOTER_ADDR, app_booter_bin, app_booter_bin_size);
|
|
|
|
DCFlushRange(BOOTER_ADDR, app_booter_bin_size);
|
2012-01-23 22:26:41 +01:00
|
|
|
ICInvalidateRange(BOOTER_ADDR, app_booter_bin_size);
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
entrypoint entry = (entrypoint) BOOTER_ADDR;
|
2011-07-23 21:49:51 +02:00
|
|
|
|
2012-01-23 22:26:41 +01:00
|
|
|
if (!IsDollZ(homebrewbuffer))
|
|
|
|
memcpy(ARGS_ADDR, &args, sizeof(struct __argv));
|
|
|
|
else
|
|
|
|
memset(ARGS_ADDR, 0, sizeof(struct __argv));
|
|
|
|
|
|
|
|
DCFlushRange(ARGS_ADDR, sizeof(struct __argv) + args.length);
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
u64 currentStub = getStubDest();
|
|
|
|
loadStub();
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (Set_Stub_Split(0x00010001, "UNEO") < 0)
|
|
|
|
{
|
|
|
|
if (Set_Stub_Split(0x00010001, "ULNR") < 0)
|
|
|
|
{
|
|
|
|
if (!currentStub) currentStub = 0x100000002ULL;
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
Set_Stub(currentStub);
|
|
|
|
}
|
|
|
|
}
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2014-04-29 18:08:42 +02:00
|
|
|
gprintf("Exiting USBLoaderGX...\n\n");
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
SYS_ResetSystem(SYS_SHUTDOWN, 0, 0);
|
|
|
|
_CPU_ISR_Disable( cpu_isr );
|
|
|
|
__exception_closeall();
|
|
|
|
entry();
|
|
|
|
_CPU_ISR_Restore( cpu_isr );
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return 0;
|
2010-09-19 01:04:39 +02:00
|
|
|
}
|
|
|
|
|
2010-11-06 16:30:14 +01:00
|
|
|
int BootHomebrew(const char * filepath)
|
2010-09-19 01:04:39 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
FILE *file = fopen(filepath, "rb");
|
|
|
|
if (!file) return -1;
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
fseek(file, 0, SEEK_END);
|
2011-11-13 10:09:27 +01:00
|
|
|
u32 filesize = ftell(file);
|
2011-07-26 00:28:22 +02:00
|
|
|
rewind(file);
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-11-13 10:09:27 +01:00
|
|
|
if (fread(homebrewbuffer, 1, filesize, file) != filesize)
|
2011-07-26 00:28:22 +02:00
|
|
|
{
|
|
|
|
fclose(file);
|
|
|
|
DeviceHandler::DestroyInstance();
|
|
|
|
Sys_BackToLoader();
|
2020-11-08 22:28:26 +01:00
|
|
|
return -1;
|
2011-07-26 00:28:22 +02:00
|
|
|
}
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-11-13 10:09:27 +01:00
|
|
|
homebrewsize = filesize;
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-11-13 10:09:27 +01:00
|
|
|
fclose(file);
|
2010-09-19 01:04:39 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
AddBootArgument(filepath);
|
|
|
|
return RunAppbooter();
|
2010-09-19 01:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int BootHomebrewFromMem()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
return RunAppbooter();
|
2010-09-19 01:04:39 +02:00
|
|
|
}
|