Skip to content

Commit

Permalink
- Add IGR hook to WKF/DVD
Browse files Browse the repository at this point in the history
- Silence some WKF specific init messages
  • Loading branch information
emukidid committed Aug 14, 2017
1 parent 8a74f74 commit e5dea5c
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 23 deletions.
3 changes: 2 additions & 1 deletion cube/patches/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ build-wkf:
@$(CC) -O1 -ffunction-sections -mmultiple -mstring -c base/usbgecko.c
@$(CC) -O1 -ffunction-sections -mmultiple -mstring -c sdgecko/sd.c
@$(CC) -O1 -ffunction-sections -mmultiple -mstring -c base/frag.c
@$(CC) -O1 -ffunction-sections -mmultiple -mstring -c base/igr.c -DWKF

@$(LD) -o wkf.elf base.o wkf.o usbgecko.o sd.o frag.o --section-start .text=0x80001000 --gc-sections
@$(LD) -o wkf.elf base.o wkf.o usbgecko.o sd.o frag.o igr.o --section-start .text=0x80001000 --gc-sections
@rm -rf *.o
@$(OBJDUMP) -D wkf.elf > $(DISASM)/wkf_disasm.txt
@$(OBJCOPY) -O binary wkf.elf wkf.bin
Expand Down
22 changes: 15 additions & 7 deletions cube/patches/base/igr.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ typedef int s32;
typedef unsigned short u16;
typedef unsigned char u8;

#ifdef WKF
extern void wkfReinit();
#endif

#define DOLHDRLENGTH 256
#define MAXTEXTSECTION 7
#define MAXDATASECTION 11
Expand Down Expand Up @@ -67,6 +71,10 @@ void load_dol() {
void exit_to_pref(void) {
u8 igr_exit_type = *(u8*)VAR_IGR_EXIT_TYPE;

#ifdef WKF
wkfReinit();
#endif

if(igr_exit_type == IGR_HARDRESET) {
// int i = 0;
// for (i = 0; i < 10; i++) {
Expand All @@ -88,16 +96,16 @@ void exit_to_pref(void) {
// R = 00200000
// Z = 00100000
// D = 00040000
volatile u16* const _memReg = (u16*)0xCC004000;
volatile u16* const _dspReg = (u16*)0xCC005000;
//volatile u16* const _memReg = (u16*)0xCC004000;
//volatile u16* const _dspReg = (u16*)0xCC005000;
/* stop audio dma */
_dspReg[27] = (_dspReg[27]&~0x8000);
//_dspReg[27] = (_dspReg[27]&~0x8000);
/* disable dcache and icache */
asm volatile("sync ; isync ; mfspr 3,1008 ; rlwinm 3,3,0,18,15 ; mtspr 1008,3");
//asm volatile("sync ; isync ; mfspr 3,1008 ; rlwinm 3,3,0,18,15 ; mtspr 1008,3");
/* disable memory protection */
_memReg[15] = 0xF;
_memReg[16] = 0;
_memReg[8] = 0xFF;
//_memReg[15] = 0xF;
//_memReg[16] = 0;
//_memReg[8] = 0xFF;
//load_dol();
}
else if (igr_exit_type == IGR_USBGKOFLASH) {
Expand Down
12 changes: 12 additions & 0 deletions cube/patches/dvd/base.S
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
_start:
b handle_disc_swap # Re-authenticates a DVD-R disc
b dvd_read_patched_section # Either reads from SD, or allows the DVD read to occur.
b check_exit # checks for a button combo and exits if true

.globl check_exit
check_exit:
lwz r5, 0(r4)
rlwinm r5, r5, 16, 16, 31
andi. r5, r5, 0x0234 # B,Z,R,D-Pad Down
cmplwi r5, 0x0234
beq do_exit
blr
do_exit:
b exit_to_pref

.globl dcache_flush_icache_inv
dcache_flush_icache_inv:
Expand Down
1 change: 1 addition & 0 deletions cube/patches/usbgecko/usbgecko.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
***************************************************************************/

#include "../../reservedarea.h"
#include "../base/common.h"

typedef unsigned int u32;
typedef int s32;
Expand Down
12 changes: 12 additions & 0 deletions cube/patches/wkf/base.S
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ _start:
b patched_memcpy # void patched_memcpy(dst,src,size)
b patched_memcpy_dbg
b adjust_read
b check_exit # checks for a button combo and exits if true

.globl check_exit
check_exit:
lwz r5, 0(r4)
rlwinm r5, r5, 16, 16, 31
andi. r5, r5, 0x0234 # B,Z,R,D-Pad Down
cmplwi r5, 0x0234
beq do_exit
blr
do_exit:
b exit_to_pref

.globl dcache_flush_icache_inv
dcache_flush_icache_inv:
Expand Down
14 changes: 11 additions & 3 deletions cube/patches/wkf/wkf.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
extern void print_int_hex(unsigned int num);

void wkfWriteOffset(u32 offset) {
static volatile u32* const wkf = (u32*)0xCC006000;
volatile u32* wkf = (volatile u32*)0xCC006000;
wkf[2] = 0xDE000000;
wkf[3] = offset;
wkf[4] = 0x5A000000;
Expand All @@ -19,8 +19,9 @@ void wkfWriteOffset(u32 offset) {
while( wkf[7] & 1);
}


void wkfRead(void* dst, int len, u32 offset) {
volatile unsigned long* dvd = (volatile unsigned long*)0xCC006000;
volatile u32* dvd = (volatile u32*)0xCC006000;
dvd[2] = 0xA8000000;
dvd[3] = offset >> 2;
dvd[4] = len;
Expand All @@ -31,7 +32,7 @@ void wkfRead(void* dst, int len, u32 offset) {

// Adjusts the offset on the WKF for fragmented reads
void adjust_read() {
volatile unsigned long* dvd = (volatile unsigned long*)0xCC006000;
volatile u32* dvd = (volatile u32*)0xCC006000;

u32 dst = dvd[5];
u32 len = dvd[4];
Expand Down Expand Up @@ -109,6 +110,13 @@ void adjust_read() {
}
}

void wkfReinit(void) {
asm("mfmsr 5");
asm("rlwinm 5,5,0,17,15");
asm("mtmsr 5");
// TODO re-init WKF
}

//void swap_disc() {
// int isDisc1 = (*(u32*)(VAR_DISC_1_LBA)) == (*(u32*)VAR_CUR_DISC_LBA);
// *(u32*)VAR_CUR_DISC_LBA = isDisc1 ? *(u32*)(VAR_DISC_2_LBA) : *(u32*)(VAR_DISC_1_LBA);
Expand Down
2 changes: 2 additions & 0 deletions cube/swiss/include/patcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ enum patchIds {
/* Function jump locations for the DVD patch */
#define ENABLE_BACKUP_DISC (LO_RESERVE_DVD | 0x00)
#define READ_REAL_OR_PATCHED (LO_RESERVE_DVD | 0x04)
#define IGR_CHECK_DVD (LO_RESERVE_DVD | 0x08)

/* Function jump locations for the WKF/WASP patch */
#define PATCHED_MEMCPY_WKF (LO_RESERVE)
#define PATCHED_MEMCPY_DBG_WKF (LO_RESERVE | 0x04)
#define ADJUST_LBA_OFFSET (LO_RESERVE | 0x08)
#define IGR_CHECK_WKF (LO_RESERVE | 0x0C)

/* Function jump locations for the USBGecko patch */
#define PATCHED_MEMCPY_USB (LO_RESERVE)
Expand Down
21 changes: 10 additions & 11 deletions cube/swiss/source/devices/wiikeyfusion/deviceHandler-wiikeyfusion.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,20 @@ s32 deviceHandler_WKF_setupFile(file_handle* file, file_handle* file2) {
s32 deviceHandler_WKF_init(file_handle* file){
struct statvfs buf;

DrawFrameStart();
DrawMessageBox(D_INFO,"Init Wiikey Fusion");
DrawFrameFinish();
wkfReinit();
int ret = fatMountSimple ("wkf", wkf) ? 1 : 0;
initial_WKF_info.freeSpaceInKB = initial_WKF_info.totalSpaceInKB = 0;
if(ret) {
memset(&buf, 0, sizeof(statvfs));
DrawFrameStart();
DrawMessageBox(D_INFO,"Reading filesystem info for wkf:/");
DrawFrameFinish();

int res = statvfs("wkf:/", &buf);
initial_WKF_info.freeSpaceInKB = !res ? (u32)((uint64_t)((uint64_t)buf.f_bsize*(uint64_t)buf.f_bfree)/1024LL):0;
initial_WKF_info.totalSpaceInKB = !res ? (u32)((uint64_t)((uint64_t)buf.f_bsize*(uint64_t)buf.f_blocks)/1024LL):0;
if(deviceHandler_getStatEnabled()) {
memset(&buf, 0, sizeof(statvfs));
DrawFrameStart();
DrawMessageBox(D_INFO,"Reading filesystem info for wkf:/");
DrawFrameFinish();

int res = statvfs("wkf:/", &buf);
initial_WKF_info.freeSpaceInKB = !res ? (u32)((uint64_t)((uint64_t)buf.f_bsize*(uint64_t)buf.f_bfree)/1024LL):0;
initial_WKF_info.totalSpaceInKB = !res ? (u32)((uint64_t)((uint64_t)buf.f_bsize*(uint64_t)buf.f_blocks)/1024LL):0;
}
}

return ret;
Expand Down
4 changes: 3 additions & 1 deletion cube/swiss/source/patcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -1960,7 +1960,9 @@ int Patch_IGR(void *data, u32 length, int dataType) {
while(*(u32*)(data + i + iEnd) != 0x4E800020) iEnd += 4; // branch relative from the end
u32 properAddress = Calc_ProperAddress(data, dataType, (u32)(data + i + iEnd) -(u32)(data));
print_gecko("Found:[SPEC2_MakeStatusA] @ %08X (%08X)\n", properAddress, i + iEnd);
*(u32*)(data+i+iEnd) = branch(IGR_CHECK, properAddress);
u32 igrJump = (devices[DEVICE_CUR] == &__device_wkf) ? IGR_CHECK_WKF : IGR_CHECK;
if(devices[DEVICE_CUR] == &__device_dvd) igrJump = IGR_CHECK_DVD;
*(u32*)(data+i+iEnd) = branch(igrJump, properAddress);
return 1;
}
}
Expand Down

0 comments on commit e5dea5c

Please sign in to comment.