Skip to content

Commit

Permalink
Fixed Hiidra's booting occasional freezing issue
Browse files Browse the repository at this point in the history
Made WAD installation much faster
Fixed text scroll direction in hiidra boot screen
  • Loading branch information
Aurelio92 committed Apr 3, 2024
1 parent f73cfb7 commit 0734113
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 40 deletions.
Binary file modified driveRoot/apps/RVLoader/boot.dol
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function draw(onFocus)

--Scroll if needed
if lineHeight * (#logLines) > getDimensions()[2] then
Gfx.translate(0, lineHeight * (#logLines) - getDimensions()[2])
Gfx.translate(0, getDimensions()[2] - lineHeight * (#logLines))
end

for i = 1, #logLines do
Expand Down
2 changes: 2 additions & 0 deletions main/include/hiidra.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ int loadIOSModules(void);
void initHiidra();
void lockHiidraLogMutex();
void unlockHiidraLogMutex();
void hiidraSignalRedraw();
void hiidraWaitForRedraw();
u32 hiidraAddLogLine(const char* line, ...);
void hiidraUpdateLogLine(u32 index, const char* line, ...);
void luaRegisterHiidraLib(lua_State* L);
Expand Down
2 changes: 2 additions & 0 deletions main/include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ void lockSIMutex();
void unlockSIMutex();
void enableControllers();
void disableControllers();
void enableControlledRedraw();
void forceRedraw();
void mainWindowSwitchElement(const char* el);
Vector2 getScreenSize();
bool isVGAEnabled();
Expand Down
71 changes: 36 additions & 35 deletions main/source/hiidra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
#include "ELF.h"
#include "luasupport.h"

#define COPY_LIMIT (32*1024)
#define COPY_LIMIT (128*1024)

#define HIIDRA_LOG_LINES 32
#define HIIDRA_LOG_LINE_LENGTH 256

#define STACKSIZE 8192
#define STACKSIZE (128*1024)

#define MEM_PROT 0x0D8B420A

Expand All @@ -57,6 +57,7 @@ static mutex_t logMutex = 0;
static char logBuffer[HIIDRA_LOG_LINES][HIIDRA_LOG_LINE_LENGTH];
static u32 logLineIndex;
static u32 logNLines;
static lwpq_t logQueue = 0;

extern "C" {
extern void* SYS_AllocArena2MemLo(u32 size,u32 align);
Expand Down Expand Up @@ -131,16 +132,18 @@ void forgeKernel(char* kernel, u32 kernelSize, const uint8_t** extraModules, u32
u32 noteSize;
unsigned int loadersize = *(vu32*)(kernel) + *(vu32*)(kernel+4);
//Copy old kernel in a buffer so we can overwrite the kernel memory region
char* oldKernel = (char*)malloc(kernelSize);
char* oldKernel = (char*)SYS_AllocArena2MemLo(kernelSize, 0x20);
memcpy(oldKernel, kernel, kernelSize);
for (i = loadersize; i < kernelSize; i+=4) {
if (memcmp(oldKernel+i, HWAccess_ES, sizeof(HWAccess_ES)) == 0) {
printf("Found HWAccess_ES at %08X\r\n", i);
hiidraAddLogLine("Found HWAccess_ES at %08X", i);
memcpy(oldKernel+i, HWAccess_ESPatch, sizeof(HWAccess_ESPatch));
}

if (memcmp(oldKernel+i, KernelAccess, sizeof(KernelAccess)) == 0) {
printf("Found KernelAccess at %08X\r\n", i);
hiidraAddLogLine("Found KernelAccess at %08X", i);
memcpy(oldKernel+i, KernelAccessPatch, sizeof(KernelAccessPatch));
}
}
Expand Down Expand Up @@ -340,14 +343,23 @@ void forgeKernel(char* kernel, u32 kernelSize, const uint8_t** extraModules, u32
DCFlushRange(kernel, loadersize + elfSize);

printf("Saving hiidra.bin\n");
hiidraAddLogLine("Saving hiidra.bin");
FILE* fp = fopen("/hiidra.bin", "wb");
if (fp == NULL) {
printf("Couldn't\n");
} else {
printf("Saving %u\n", loadersize + elfSize);
fwrite(kernel, loadersize + elfSize, 1, fp);
u8* bufferToSave = (u8*)kernel;
u32 toSave = loadersize + elfSize;
printf("Saving %u\n", toSave);
while (toSave > 0) {
u32 chunkSize = (toSave > COPY_LIMIT) ? COPY_LIMIT : toSave;
fwrite(bufferToSave, chunkSize, 1, fp);
bufferToSave += chunkSize;
toSave -= chunkSize;
}
fclose(fp);
printf("Success\n");
hiidraAddLogLine("Success");
}
}

Expand Down Expand Up @@ -1037,35 +1049,6 @@ static void* bootHiidraThread(void* arg) {
char* gamePath = (char*)iosAlloc(hId, 0x80);
sprintf(gamePath, "/dev/usbfs%s", hcfg.GamePath);

//Copying cheats
/*memcpy((void*)0x80001900, codehandleronly_bin, codehandleronly_bin_size);
DCFlushRange((void*)0x80001900, codehandleronly_bin_size);
memcpy((void*)(0x80001900+codehandleronly_bin_size-8), cheats, cheatsSize);
DCFlushRange((void*)(0x80001900+codehandleronly_bin_size-8), cheatsSize);*/
/*
s32 gamePatcherFD = IOS_Open("/dev/patcher", 0);
if (gamePatcherFD >= 0) {
s32 gamePatcherRet;
u32* gctSize = (u32*)iosAlloc(hId, sizeof(u32));
u32** gctAddress = (u32**)iosAlloc(hId, sizeof(u32*));
*gctSize = cheatsSize;
gamePatcherRet = IOS_Ioctl(gamePatcherFD, 0x00, gctSize, sizeof(u32), gctAddress, sizeof(u32*));
printf("GCT Address: %08X %08X\n", (u32)(*gctAddress), (u32)MEM_PHYSICAL_TO_K0((void*)*gctAddress));
memcpy(MEM_PHYSICAL_TO_K0((void*)*gctAddress), cheats, cheatsSize);
DCFlushRange(MEM_PHYSICAL_TO_K0((void*)*gctAddress), cheatsSize);
*(u32*)0x800022A8 = (u32)MEM_PHYSICAL_TO_K0((void*)*gctAddress);
DCFlushRange((u8*)0x800022A8, 4);
//memcpy((void*)0x800022A0, cheats, cheatsSize);
//DCFlushRange((void*)0x800022A0, cheatsSize);
memcpy((void*)0x800022C0, cheats, cheatsSize);
DCFlushRange((void*)0x800022C0, cheatsSize);
*(u32*)0x800022A8 = 0x800022C0;
DCFlushRange((u8*)0x800022A8, 4);
IOS_Close(gamePatcherFD);
}
*/

printf("Launching %s\n", gamePath);
hiidraAddLogLine("Launching %s\n", gamePath);
if (wadTitleID) {
Expand Down Expand Up @@ -1105,6 +1088,7 @@ static void* bootHiidraThread(void* arg) {

void initHiidra() {
LWP_MutexInit(&logMutex, false);
LWP_InitQueue(&logQueue);
logLineIndex = 0;
logNLines = 0;
}
Expand All @@ -1116,11 +1100,19 @@ void lockHiidraLogMutex() {
}

void unlockHiidraLogMutex() {
if (logMutex) {
if (logMutex) {
LWP_MutexUnlock(logMutex);
}
}

void hiidraSignalRedraw() {
LWP_ThreadSignal(logQueue);
}

void hiidraWaitForRedraw() {
LWP_ThreadSleep(logQueue);
}

u32 hiidraAddLogLine(const char* line, ...) {
va_list args;
u32 prevIndex;
Expand All @@ -1143,6 +1135,9 @@ u32 hiidraAddLogLine(const char* line, ...) {

unlockHiidraLogMutex();

forceRedraw();
hiidraWaitForRedraw();

return prevIndex;
}

Expand All @@ -1153,6 +1148,9 @@ void hiidraUpdateLogLine(u32 index, const char* line, ...) {
vsnprintf(logBuffer[index], HIIDRA_LOG_LINE_LENGTH, line, args);
va_end(args);
unlockHiidraLogMutex();

forceRedraw();
hiidraWaitForRedraw();
}

static int lua_getLogLines(lua_State* L) {
Expand Down Expand Up @@ -1210,9 +1208,12 @@ int bootHiidra(HIIDRA_CFG hcfg, u32 gameIDU32, std::vector<uint32_t> cheats, boo
//Disable any controller input
disableControllers();


//Switch to Hiidra's loading screen
mainWindowSwitchElement("HiidraBootScreen");

enableControlledRedraw();

LWP_CreateThread(&bootHiidraThreadHandle, bootHiidraThread, bootHiidraThreadArg, bootHiidraThreadStack, STACKSIZE, 50);

return 0;
Expand Down
17 changes: 17 additions & 0 deletions main/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ Config mainConfig;
volatile u32 connectedPads;
static mutex_t SIMutex;
static bool controllersEnabled;
static bool controlledRedraw = false;
static bool hasToShutdown = false;
static lwpq_t redrawQueue;

extern "C" {
extern void udelay(int us);
Expand All @@ -82,6 +84,14 @@ void disableControllers() {
controllersEnabled = false;
}

void enableControlledRedraw() {
controlledRedraw = true;
}

void forceRedraw() {
LWP_ThreadSignal(redrawQueue);
}

void mainWindowSwitchElement(const char* el) {
mainWindow.switchToElement(el);
}
Expand Down Expand Up @@ -111,6 +121,7 @@ int main(int argc, char **argv) {
float mem1Max = (float)SYS_GetArena1Size() / 1048576.0f;
float mem2Max = (float)SYS_GetArena2Size() / 1048576.0f;

LWP_InitQueue(&redrawQueue);
LWP_MutexInit(&SIMutex, true);
enableControllers();
initHiidra();
Expand Down Expand Up @@ -295,6 +306,10 @@ int main(int argc, char **argv) {
}

while(1) {
if (controlledRedraw) {
LWP_ThreadSleep(redrawQueue);
}

if (controllersEnabled) {
lockSIMutex();
connectedPads = PAD_ScanPads();
Expand All @@ -319,6 +334,8 @@ int main(int argc, char **argv) {
//font.printf(300, 10, "PADS: %u", connectedPads);
Gfx::endDrawing();
//VIDEO_WaitVSync();

hiidraSignalRedraw();
}

return 0;
Expand Down
16 changes: 12 additions & 4 deletions main/source/rijndael.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

#include <stdio.h>
#include <string.h>
#include <malloc.h>

#define OUT_FILE_BUFFER_SIZE 4096
#define OUT_FILE_BUFFER_SIZE (128*1024)

#define u8 unsigned char /* 8 bits */
#define u32 unsigned long /* 32 bits */
Expand Down Expand Up @@ -374,12 +375,16 @@ void aes_decrypt_file(u8 *iv, FILE* infp, FILE* outfp, unsigned long long inLen,
u8 tempBlock[16];
u8 prevBlock[16];
u8 outblock[16];
u8 outBuffer[OUT_FILE_BUFFER_SIZE];
u8* outBuffer;
u32 outBufferIdx = 0;
unsigned int blockno = 0, i;

unsigned long long outLenFinal = outLen;

u32 progressPercentage = 0;

outBuffer = memalign(0x20, OUT_FILE_BUFFER_SIZE);

if (progressCb != NULL) {
progressCb(arg0, 0); //Print 0% progress
}
Expand Down Expand Up @@ -413,8 +418,9 @@ void aes_decrypt_file(u8 *iv, FILE* infp, FILE* outfp, unsigned long long inLen,
if (outBufferIdx == OUT_FILE_BUFFER_SIZE) {
outBufferIdx = 0;
fwrite(outBuffer, 1, OUT_FILE_BUFFER_SIZE, outfp);
if (progressCb != NULL) {
progressCb(arg0, 100 * (outLenFinal - outLen) / outLenFinal); //Print progress
if (progressCb != NULL && progressPercentage != (100 * (outLenFinal - outLen) / outLenFinal)) {
progressPercentage = 100 * (outLenFinal - outLen) / outLenFinal;
progressCb(arg0, progressPercentage); //Print progress
}
}
memcpy(prevBlock, tempBlock, 16);
Expand All @@ -428,6 +434,8 @@ void aes_decrypt_file(u8 *iv, FILE* infp, FILE* outfp, unsigned long long inLen,
if (progressCb != NULL) {
progressCb(arg0, 100); //Print 100% progress
}

free(outBuffer);
}

// CBC mode encryption
Expand Down

0 comments on commit 0734113

Please sign in to comment.