WiiFlow_Lite/source/loader/wip.c

142 lines
2.8 KiB
C
Raw Normal View History

2012-01-21 21:57:41 +01:00
#include <gccore.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "mem2.hpp"
2012-01-21 21:57:41 +01:00
#include "gecko.h"
#include "wip.h"
2012-01-21 21:57:41 +01:00
static WIP_Code * CodeList = NULL;
static u32 CodesCount = 0;
static u32 ProcessedLength = 0;
static u32 Counter = 0;
void do_wip_code(u8 * dst, u32 len)
{
if(!CodeList)
return;
if(Counter < 3)
{
Counter++;
return;
}
u32 i = 0;
s32 n = 0;
s32 offset = 0;
for(i = 0; i < CodesCount; i++)
{
for(n = 0; n < 4; n++)
{
offset = CodeList[i].offset+n-ProcessedLength;
if(offset < 0 || (u32)offset >= len)
continue;
if(dst[offset] == ((u8 *)&CodeList[i].srcaddress)[n])
{
dst[offset] = ((u8 *)&CodeList[i].dstaddress)[n];
gprintf("WIP: %08X Address Patched.\n", CodeList[i].offset + n);
}
else
{
gprintf("WIP: %08X Address does not match with WIP entry.\n", CodeList[i].offset+n);
gprintf("Destination: %02X | Should be: %02X.\n", dst[offset], ((u8 *)&CodeList[i].srcaddress)[n]);
}
}
}
ProcessedLength += len;
Counter++;
2012-01-21 21:57:41 +01:00
}
//! for internal patches only
//! .wip files override internal patches
//! the codelist has to be freed if the set fails
//! if set was successful the codelist will be freed when it's done
bool set_wip_list(WIP_Code * list, int size)
{
if(!CodeList && size > 0)
{
CodeList = list;
CodesCount = size;
return true;
}
return false;
}
2012-01-21 21:57:41 +01:00
void wip_reset_counter()
{
ProcessedLength = 0;
//alternative dols don't need a skip. only main.dol.
Counter = 3;
2012-01-21 21:57:41 +01:00
}
void free_wip()
{
if(CodeList)
free(CodeList);
2012-01-21 21:57:41 +01:00
CodesCount = 0;
ProcessedLength = 0;
2012-01-21 21:57:41 +01:00
}
int load_wip_patches(u8 *dir, u8 *gameid)
{
char filepath[150];
char GameID[8];
memset(GameID, 0, sizeof(GameID));
memcpy(GameID, gameid, 6);
2012-01-21 21:57:41 +01:00
snprintf(filepath, sizeof(filepath), "%s/%s.wip", dir, GameID);
FILE *fp = fopen(filepath, "rb");
if(!fp)
2012-01-21 21:57:41 +01:00
{
memset(GameID, 0, sizeof(GameID));
memcpy(GameID, gameid, 3);
2012-01-21 21:57:41 +01:00
snprintf(filepath, sizeof(filepath), "%s/%s.wip", dir, GameID);
fp = fopen(filepath, "rb");
}
if(!fp)
return -1;
2012-01-21 21:57:41 +01:00
char line[255];
gprintf("\nLoading WIP code from %s.\n", filepath);
2012-01-21 21:57:41 +01:00
while(fgets(line, sizeof(line), fp))
{
if(line[0] == '#' || strlen(line) < 26)
continue;
2012-01-21 21:57:41 +01:00
u32 offset = (u32) strtoul(line, NULL, 16);
u32 srcaddress = (u32) strtoul(line+9, NULL, 16);
u32 dstaddress = (u32) strtoul(line+18, NULL, 16);
2012-01-21 21:57:41 +01:00
if(!CodeList)
CodeList = MEM2_alloc(sizeof(WIP_Code));
2012-01-21 21:57:41 +01:00
WIP_Code *tmp = MEM2_realloc(CodeList, (CodesCount+1)*sizeof(WIP_Code));
if(!tmp)
{
free(CodeList);
fclose(fp);
return -1;
}
CodeList = tmp;
CodeList[CodesCount].offset = offset;
CodeList[CodesCount].srcaddress = srcaddress;
CodeList[CodesCount].dstaddress = dstaddress;
CodesCount++;
}
fclose(fp);
gprintf("\n");
2012-01-21 21:57:41 +01:00
return 0;
}