X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=ginge.git;a=blobdiff_plain;f=loader%2Fpatches.c;h=901de511a511341346b8b1cb3ad694d7f159e64c;hp=99b42e0568acbc2b34cd8c7bd983988605ff0064;hb=HEAD;hpb=7b39b9a97e331926f3948f9fbf4cc00b285c874a diff --git a/loader/patches.c b/loader/patches.c index 99b42e0..901de51 100644 --- a/loader/patches.c +++ b/loader/patches.c @@ -7,7 +7,7 @@ #include #include "header.h" -#include "sys_cacheflush.h" +#include "syscalls.h" #include "override.c" @@ -31,6 +31,13 @@ static const unsigned int sig_open_a1[] = { }; #define sig_mask_open_a1 sig_mask_all +static const unsigned int sig_hw_open[] = { + 0xef900005, // svc 0x900005 + 0xe3700a01, // cmn r0, #0x1000 + 0xe1a04000, // mov r4, r0 +}; +#define sig_mask_hw_open sig_mask_all + static const unsigned int sig_mmap[] = { 0xe92d000f, // push {r0, r1, r2, r3} 0xe1a0000d, // mov r0, sp @@ -113,12 +120,12 @@ static const unsigned int sig_execve[] = { }; #define sig_mask_execve sig_mask_all -static const unsigned int sig_execve2[] = { +static const unsigned int sig_hw_execve[] = { 0xef90000b, // svc 0x90000b 0xe3700a01, // cmn r0, #4096 0xe1a04000, // mov r4, r0 }; -#define sig_mask_execve2 sig_mask_all +#define sig_mask_hw_execve sig_mask_all static const unsigned int sig_chdir[] = { 0xef90000c, // svc 0x90000c @@ -130,6 +137,24 @@ static const unsigned int sig_mask_chdir[] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000 }; +static const unsigned int sig_readlink[] = { + 0xef900055, // svc 0x900055 + 0xe3700a01, // cmn r0, #0x1000 + 0x312fff1e, // bxcc lr +}; +#define sig_mask_readlink sig_mask_all + +/* special */ +static const unsigned int sig_cache1[] = { + 0xee073f5e, // mcr 15, 0, r3, cr7, cr14, 2 +}; +#define sig_mask_cache1 sig_mask_all + +static const unsigned int sig_cache2[] = { + 0xee070f17, // mcr 15, 0, r0, cr7, cr7, 0 +}; +#define sig_mask_cache2 sig_mask_all + /* additional wrappers for harder case of syscalls within the code stream */ #ifdef PND /* fix PC, not needed on ARM9 */ # define SVC_CMN_R0_MOV_R4_PC_ADJ() \ @@ -152,8 +177,10 @@ asm( \ " ldmfd sp!, {r1-r3,r12,lr,pc}\n" \ ); -SVC_CMN_R0_MOV_R4_WRAPPER(hw_read, w_read) -SVC_CMN_R0_MOV_R4_WRAPPER(hw_ioctl, w_ioctl) +SVC_CMN_R0_MOV_R4_WRAPPER(hw_open, w_open_raw) +SVC_CMN_R0_MOV_R4_WRAPPER(hw_read, w_read_raw) +SVC_CMN_R0_MOV_R4_WRAPPER(hw_ioctl, w_ioctl_raw) +SVC_CMN_R0_MOV_R4_WRAPPER(hw_execve, w_execve_raw) #define PATCH_(p, f, t) { sig_##p, sig_mask_##p, ARRAY_SIZE(sig_##p), t, f, #p } #define PATCH(f) PATCH_(f, w_##f, 0) @@ -168,6 +195,7 @@ static const struct { } patches[] = { PATCH (open), PATCH_(open_a1, w_open, 0), + PATCH_(hw_open, hw_open, 1), PATCH (mmap), PATCH (mmap2), // mmap2 syscall PATCH (munmap), @@ -177,32 +205,33 @@ static const struct { PATCH (ioctl), PATCH_(hw_ioctl, hw_ioctl, 1), PATCH (sigaction), -// PATCH_(execve, execve2, 0), // hangs + PATCH_(hw_execve, hw_execve, 1), PATCH (chdir), + PATCH (readlink), + PATCH_(cache1, NULL, 2), + PATCH_(cache2, NULL, 2), }; void do_patches(void *ptr, unsigned int size) { + unsigned int *seg = (void *)(((long)ptr + 3) & ~3); + unsigned int *seg_end = seg + size / 4; int i, s; - for (i = 0; i < ARRAY_SIZE(patches); i++) { - const unsigned int *sig = patches[i].sig; - const unsigned int *sig_mask = patches[i].sig_mask; - unsigned int *seg = (void *)(((long)ptr + 3) & ~3); - unsigned int *seg_end = seg + size / 4; - unsigned int sig0 = sig[0]; + for (; seg < seg_end; seg++) { + for (i = 0; i < ARRAY_SIZE(patches); i++) { + const unsigned int *sig = patches[i].sig; + const unsigned int *sig_mask; - for (; seg < seg_end; seg++) { - if (*seg != sig0) + if (*seg != sig[0]) continue; + sig_mask = patches[i].sig_mask; for (s = 1; s < patches[i].sig_cnt; s++) if ((seg[s] ^ sig[s]) & sig_mask[s]) break; if (s == patches[i].sig_cnt) { - dbg(" patch #%i @ %08x type %d %s\n", - i, (int)seg, patches[i].type, patches[i].name); switch (patches[i].type) { case 0: seg[0] = 0xe51ff004; // ldr pc, [pc, #-4] @@ -213,11 +242,20 @@ void do_patches(void *ptr, unsigned int size) seg[1] = 0xe51ff004; // ldr pc, [pc, #-4] seg[2] = (unsigned int)patches[i].func; break; + case 2: + if (seg < (unsigned int *)ptr + 1 || (seg[-1] >> 28) != 0x0e) + // might be data + continue; + seg[0] = 0xe1a00000; // nop + break; default: err("bad patch type: %u\n", patches[i].type); abort(); } + dbg(" patch #%2i @ %08x type %d %s\n", + i, (int)seg, patches[i].type, patches[i].name); seg += patches[i].sig_cnt - 1; + break; } } }