forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need help on hooking

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Cpasjuste



Joined: 29 May 2005
Posts: 214

PostPosted: Fri Oct 17, 2008 11:05 pm    Post subject: Need help on hooking Reply with quote

Hello, i spent i few hours to deal with hooking function's, and i'm having a problem that it seem's i won't be able to solve myself, i really need some help.

I'll try to explain it with my poor english :

I'm hooking an audio function like this and it work fine :

Code:

int sceAudioOutputPannedBlocking_patched(int channel, int leftvol, int rightvol, void *buf)
{
        return 0;
}
   orig_funcs[0] = sctrlHENFindFunction("sceAudio_Driver", "sceAudio", 0x13F592BC);
   sctrlHENPatchSyscall(orig_funcs[0], sceAudioOutputPannedBlocking_patched);


Of course, when this function is called, it return 0, so there is no sound output.
My problem is sometime i want, inside my hooked function, return the original function. I tried something like this :

Code:

int sceAudioOutputPannedBlocking_patched(int channel, int leftvol, int rightvol, void *buf)
{
        return sceAudioOutputPannedBlocking_orig(channel, leftvol, rightvol, buf);
}
   orig_funcs[0] = sctrlHENFindFunction("sceAudio_Driver", "sceAudio", 0x13F592BC);
        sctrlHENPatchSyscall((u32)sceAudioOutputPannedBlocking_orig, (void *)orig_funcs[0]);
   sctrlHENPatchSyscall(orig_funcs[0], sceAudioOutputPannedBlocking_patched);


But it's not working, there is something i must not understand ... maybe it's possible to call a function directly by it's nid ?
Any help would be great, thanks.
Back to top
View user's profile Send private message
Super Sheep



Joined: 23 Mar 2008
Posts: 31

PostPosted: Fri Oct 17, 2008 11:14 pm    Post subject: Reply with quote

Just call the function normally (sceAudioOutputPannedBlocking). That should work assuming you aren't in usermode.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Cpasjuste



Joined: 29 May 2005
Posts: 214

PostPosted: Fri Oct 17, 2008 11:28 pm    Post subject: Reply with quote

Thanks for the help SuperSheep.

I tried that but i get an error depending on how i'm returning the buffer, maybe i'm doing someting wrong.

I get a "Reserved instruction" exception or a "Bus error (instr)" exception.
Maybe the problem is i'm hooking an usermode function from a kernel module.
Back to top
View user's profile Send private message
Super Sheep



Joined: 23 Mar 2008
Posts: 31

PostPosted: Fri Oct 17, 2008 11:37 pm    Post subject: Reply with quote

Are you sure its usermode? Not just a kernel mode function being called from usermode?
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Cpasjuste



Joined: 29 May 2005
Posts: 214

PostPosted: Fri Oct 17, 2008 11:44 pm    Post subject: Reply with quote

No i'm not sure, but since i can hook it from a kernel module does it mean it's a kernel function?

The hook is successfull, when the hooked function is called, the sound is off because i'm returning 0. The only problem is when i try to call the original function in my hooked function :/
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sat Oct 18, 2008 1:44 am    Post subject: Reply with quote

Cpasjuste wrote:
No i'm not sure, but since i can hook it from a kernel module does it mean it's a kernel function?

The hook is successfull, when the hooked function is called, the sound is off because i'm returning 0. The only problem is when i try to call the original function in my hooked function :/


if your hooked function has an address like 0x8XXXXXXX, it's a kernel function and probably points out on the real function or on the stub function "J real_function; NOP". If not, it may indeed point on the sycall stub (but i won't see why).

Now, what does sctrlHENPatchSyscall ?
- tries to find the syscall entry referencing the old function address and patches it so it can call the new function instead ? if so, trying the first sctrlHENPatchSyscall on sceAudioOutputPannedBlocking_orig cannot work.
- replaces the two first instructions of sceAudioOutputPannedBlocking with "SYSCALL ID; JR $RA" ? it makes no sense.
- replaces the two first instructions of sceAudioOutputPannedBlocking syscall stub with "J new_func; NOP" ? if so, you need a function address which has a syscall stub to patch it : patching sceAudioOutputPannedBlocking_orig cannot work.

maybe it's something else...

First, be sure sctrlHENFindFunction returns the real address of the function (kernel address). If so, just use sctrlHENPatchSyscall to patch its stub entry with sceAudioOutputPannedBlocking_patched. If you want sceAudioOutputPannedBlocking_orig to call the real function :

Code:

typdef int (*sceAudioOutputPannedBlocking_orig_t)(int channel, int leftvol, int rightvol, void *buf);
static sceAudioOutputPannedBlocking_orig_t sceAudioOutputPannedBlocking_orig;
int sceAudioOutputPannedBlocking_patched(int channel, int leftvol, int rightvol, void *buf)
{
        return sceAudioOutputPannedBlocking_orig(channel, leftvol, rightvol, buf);
}
...
sceAudioOutputPannedBlocking_orig = (sceAudioOutputPannedBlocking_orig_t)sctrlHENFindFunction("sceAudio_Driver", "sceAudio", 0x13F592BC);
sctrlHENPatchSyscall((u32)sceAudioOutputPannedBlocking_orig, sceAudioOutputPannedBlocking_patched);


if sctrlHENFindFunction returns a stub entry like "J real_func; NOP", you need to extract real_func this way :

Code:

typdef int (*sceAudioOutputPannedBlocking_orig_t)(int channel, int leftvol, int rightvol, void *buf);
static sceAudioOutputPannedBlocking_orig_t sceAudioOutputPannedBlocking_orig;
int sceAudioOutputPannedBlocking_patched(int channel, int leftvol, int rightvol, void *buf)
{
        return sceAudioOutputPannedBlocking_orig(channel, leftvol, rightvol, buf);
}
...
int *stub = (int *)sctrlHENFindFunction("sceAudio_Driver", "sceAudio", 0x13F592BC);
sceAudioOutputPannedBlocking_orig = (sceAudioOutputPannedBlocking_orig *)(0x80000000|((*stub << 2) & 0x0FFFFFFF));
sctrlHENPatchSyscall((u32)stub, sceAudioOutputPannedBlocking_patched);


well, I guess you need to be creative :)
Back to top
View user's profile Send private message
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Sat Oct 18, 2008 4:22 am    Post subject: Reply with quote

sctrlHENFindFunction returns the exact original address.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group