what is " sceKernelSearchModuleByName "

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
ldqmoon
Posts: 13
Joined: Tue Dec 04, 2007 1:17 am

what is " sceKernelSearchModuleByName "

Post by ldqmoon »

I found this function used in hook function. but I can't find any explain of it.

it's declared like this:
extern SceUID sceKernelSearchModuleByName (const char *);

I use it in this way:
SceUID thid;
thid=sceKernelSearchModuleByName(mainHookSave[0].modname);

where mainHookSave[o].modname is "sceController_Service". the return value is -2147352365, I think this value incorrect.

I run this code under 3.71 M33, could you like give some information of it ? anythink will be ok.

TKS!
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

thid is meant to be "thread ID" and that function is for modules not threads :P
However, you can give whatever name you want.

the function is:
SceModule* sceKernelFindModuleByName(const char * modname);

http://alek.dark-alex.org/pspsdkdocs/gr ... ee904fd688
Image
Upgrade your PSP
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

The searchmodulebyname searchs a module by its name :)))

Given the module name, it returns its uid... if it finds it, an error, otherwise.
ldqmoon
Posts: 13
Joined: Tue Dec 04, 2007 1:17 am

Thank you

Post by ldqmoon »

Thank you very much!

however, I want to hook the key, when I press LEFT, it runs RIGHT, and when I press RIGHT, it runs LEFT. Here is my code. if there no problem with sceKernelFindModuleByName, I don't know why....

this is the main.c of PRX

Code: Select all

#include <pspctrl.h>
#include <psphprm.h>
#include <psppower.h>
#include <time.h>
#include <psprtc.h>

#include "main.h"
#include "hooktable.h"
char mainUmdId&#91;16&#93;;

// *** MODULE INITIALISATION ***
PSP_MODULE_INFO&#40;"KeyHook",0x1000,1,1&#41;;
// *** FUNCTIONS PATCH ***
typedef int &#40;*FUNC_CTRL&#41; &#40;SceCtrlData*, int&#41;;
typedef int &#40;*FUNC_LATCH&#41; &#40;SceCtrlLatch*&#41;;
#define PAD_THRESHOLD 50
SceCtrlData dupe_pad;
struct ctrlSetup controllerConfig;

unsigned char holdingNote = 0;
unsigned char enabled    = 0;
unsigned char inTurbo = 0; //Toggle every time we are called

//Macro Variables
unsigned char inMacro = 0; //0 if not in a macro, 1/2 if we are - index of macro + 1
u64 macroStartTime = 0; //Time the current macro piece started
unsigned char macroUpToIndex; //Index of current Macro Piece

//Works out how far an analog is pressed for remapping the analogs

//This returns the difference from center that the given control is, based on dupe_pad

void overrideControls&#40;SceCtrlData *pad_data&#41;
&#123;
	memcpy&#40;&dupe_pad, pad_data, sizeof&#40;SceCtrlData&#41;&#41;;
	///Set up the variables for processing
	inTurbo = !inTurbo; //Toggle the turbos~
	//Clear off the area for the remote, some useless bits up there
	dupe_pad.Buttons &= 0x00ffffff;

	//Set up some extra bits in the dupe_pad.Buttons &#40;remote and analog&#41;

	if &#40;sceHprmIsRemoteExist&#40;&#41;&#41; ///Remote
	&#123;
		u32 hprm = 0;
		sceHprmPeekCurrentKey&#40;&hprm&#41;;
		dupe_pad.Buttons |= &#40;hprm<<24&#41;;
	&#125;

		

		//Digitals
		pad_data->Buttons |= &#40;&#40;controllerConfig.digital_R.pressed & dupe_pad.Buttons&#41; || &#40;inTurbo && &#40;controllerConfig.digital_R.turbo & dupe_pad.Buttons&#41;&#41;&#41;?PSP_CTRL_LEFT&#58;0;

		pad_data->Buttons |= &#40;&#40;controllerConfig.digital_L.pressed & dupe_pad.Buttons&#41; || &#40;inTurbo && &#40;controllerConfig.digital_L.turbo & dupe_pad.Buttons&#41;&#41;&#41;?PSP_CTRL_RIGHT&#58;0;

	if &#40;pad_data->Buttons&#41; //if there are any keys set, send a power stay on!
		scePowerTick&#40;0&#41;;
&#125;



int sceCtrlPeekBufferPositiveFake&#40;SceCtrlData *pad_data, int count&#41;
&#123;
	u32 k1;
	k1 = pspSdkSetK1&#40;0&#41;;
	int res = &#40;&#40;FUNC_CTRL&#41; mainHookSave&#91;0&#93;.modfunc.addr&#41;&#40;pad_data, count&#41;;
	overrideControls&#40;pad_data&#41;;
	pspSdkSetK1&#40;k1&#41;;
	return res;
&#125;



int sceCtrlPeekBufferNegativeFake&#40;SceCtrlData *pad_data, int count&#41;
&#123;
	u32 k1;
	k1 = pspSdkSetK1&#40;0&#41;;
	int res = &#40;&#40;FUNC_CTRL&#41; mainHookSave&#91;1&#93;.modfunc.addr&#41;&#40;pad_data, count&#41;;
	overrideControls&#40;pad_data&#41;;
	pspSdkSetK1&#40;k1&#41;;
	return res;
&#125;



int sceCtrlReadBufferPositiveFake&#40;SceCtrlData *pad_data, int count&#41;
&#123;
	u32 k1;
	k1 = pspSdkSetK1&#40;0&#41;;
	int res = &#40;&#40;FUNC_CTRL&#41; mainHookSave&#91;2&#93;.modfunc.addr&#41;&#40;pad_data, count&#41;;
	overrideControls&#40;pad_data&#41;;
	pspSdkSetK1&#40;k1&#41;;
	return res;
&#125;



int sceCtrlReadBufferNegativeFake&#40;SceCtrlData *pad_data, int count&#41;
&#123;
	u32 k1;
	k1 = pspSdkSetK1&#40;0&#41;;
	int res = &#40;&#40;FUNC_CTRL&#41; mainHookSave&#91;3&#93;.modfunc.addr&#41;&#40;pad_data, count&#41;;
	overrideControls&#40;pad_data&#41;;
	pspSdkSetK1&#40;k1&#41;;
	return res;
&#125;



unsigned int previousPressed;

int sceCtrlPeekLatchFake&#40;SceCtrlLatch *latch_data&#41;
&#123;
	SceCtrlData pad;
	int res = sceCtrlPeekBufferPositiveFake&#40;&pad, 1&#41;;
	//Gen new Latch
	latch_data->uiMake  = &#40;previousPressed ^ pad.Buttons&#41; & pad.Buttons;
	latch_data->uiBreak = &#40;previousPressed ^ pad.Buttons&#41; & previousPressed;
	latch_data->uiPress   =  pad.Buttons;
	latch_data->uiRelease = ~pad.Buttons;
	previousPressed = pad.Buttons;

	return res;
&#125;



int sceCtrlReadLatchFake&#40;SceCtrlLatch *latch_data&#41;
&#123;
	SceCtrlData pad;
	int res = sceCtrlPeekBufferPositiveFake&#40;&pad, 1&#41;;
	//For one reason or another this is no good. Makes games run too slow
	//int res = sceCtrlReadBufferPositiveFake&#40;&pad, 1&#41;;
	//Gen new Latch
	latch_data->uiMake  = &#40;previousPressed ^ pad.Buttons&#41; & pad.Buttons;
	latch_data->uiBreak = &#40;previousPressed ^ pad.Buttons&#41; & previousPressed;
	latch_data->uiPress   =  pad.Buttons;
	latch_data->uiRelease = ~pad.Buttons;
	previousPressed = pad.Buttons;
	return res;
&#125;



// hook &#32447;&#31243;&#65292;&#37325;&#26032;&#23450;&#20041;&#20989;&#25968;

int HookThread &#40;SceSize args, void *argp&#41;
&#123;
	//SceUID loaderid, fd;
	int ret, x;
	//fd = 0; //shutup about unused ;&#41;
	previousPressed = 0;


	//Make sure kernel is in analog sampling mode!

	sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;

	
	// Patch syscall table, here the program use jmp func in stub table, so patch not applied here but in all user modules
  // u32 moduleHookFunc &#40;ModuleFunc *modfunc, SceUID modid, const char *library, SceUID nid, void *func&#41;


	for &#40;x=0;x<MAIN_HOOK_NBR;x++&#41;

	&#123;
		ret = moduleHookFunc&#40;&mainHookSave&#91;x&#93;.modfunc, sceKernelSearchModuleByName&#40;mainHookSave&#91;x&#93;.modname&#41;, mainHookSave&#91;x&#93;.libname, mainHookSave&#91;x&#93;.nid, mainHookSave&#91;x&#93;.func&#41;;


	&#125;


	sceKernelExitDeleteThread&#40;0&#41;;
	return 0;
&#125;



///////////////////////////////////////////////////////////////////////////////////////////////
int HookStart&#40;void&#41;
&#123;
	SceUID thid;
    int ret&#91;MAIN_HOOK_NBR&#93;, x;

    sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;

	for &#40;x=0;x<MAIN_HOOK_NBR;x++&#41;
       &#123;

		ret&#91;x&#93; = moduleHookFunc&#40;&mainHookSave&#91;x&#93;.modfunc, sceKernelSearchModuleByName&#40;mainHookSave&#91;x&#93;.modname&#41;, mainHookSave&#91;x&#93;.libname, mainHookSave&#91;x&#93;.nid, mainHookSave&#91;x&#93;.func&#41;;

	&#125;
    for&#40;x=0; x<MAIN_HOOK_NBR; x++&#41;
         &#123;
      if&#40;ret&#91;x&#93;<=3&#41;
              &#123;
         return ret&#91;x&#93;;
               &#125;
         &#125;
	return 0;

&#125;


int module_start&#40;SceSize args, void *argp&#41;
&#123;
	return 0;
&#125;



/*****************************************************************************/

/* This is apparently never called when running as a VSH OE plugin.          */

/*****************************************************************************/
int module_stop&#40;SceSize args, void *argp&#41;
&#123;
	int x;
	// Restore functions
	for &#40;x = 0; x < MAIN_HOOK_NBR; x++&#41;
		moduleRestoreFunc&#40;&mainHookSave&#91;x&#93;.modfunc&#41;;

        return 0;
&#125;




this is the head file of main.c, the main.h

Code: Select all

#ifndef __MAIN_H__
#define __MAIN_H__


#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspumd.h>
#include <stdio.h>
#include <stdlib.h>
#include "conf.h"
#include "Utils/module.h"

extern SceUID sceKernelSearchModuleByName &#40;const char *&#41;;

//Global for use with the menu &#58;&#41;
extern struct ctrlSetup controllerConfig;

typedef struct MainHook
&#123;
 ModuleFunc modfunc;
 char modname&#91;32&#93;;
 char libname&#91;32&#93;;
 u32 nid;
 void *func;
&#125; MainHook;



// *** FUNCTIONS DECLARATIONS ***
int sceCtrlPeekBufferPositiveFake&#40;SceCtrlData *pad_data, int count&#41;;
int sceCtrlPeekBufferNegativeFake&#40;SceCtrlData *pad_data, int count&#41;;
int sceCtrlReadBufferPositiveFake&#40;SceCtrlData *pad_data, int count&#41;;
int sceCtrlReadBufferNegativeFake&#40;SceCtrlData *pad_data, int count&#41;;
//int vshCtrlReadBufferPositiveFake&#40;SceCtrlData *pad_data, int count&#41;;
int sceCtrlPeekLatchFake&#40;SceCtrlLatch *latch_data&#41;;
int sceCtrlReadLatchFake&#40;SceCtrlLatch *latch_data&#41;;
extern unsigned char enabled;

#endif



and this is head file of hooktable.h

Code: Select all

#ifndef HOOKTABLE_INCLUDED
#define HOOKTABLE_INCLUDED

#define	 MAIN_HOOK_NBR	6

MainHook mainHookSave&#91;MAIN_HOOK_NBR&#93; =
&#123;

	&#123; &#123; 0, NULL &#125;, "sceController_Service", "sceCtrl", 0x3A622550, sceCtrlPeekBufferPositiveFake &#125;,

	&#123; &#123; 0, NULL &#125;, "sceController_Service", "sceCtrl", 0xC152080A, sceCtrlPeekBufferNegativeFake &#125;,

	&#123; &#123; 0, NULL &#125;, "sceController_Service", "sceCtrl", 0x1F803938, sceCtrlReadBufferPositiveFake &#125;,

	&#123; &#123; 0, NULL &#125;, "sceController_Service", "sceCtrl", 0x60B81F86, sceCtrlReadBufferNegativeFake &#125;,



	&#123; &#123; 0, NULL &#125;, "sceController_Service", "sceCtrl", 0xb1d0e5cd, sceCtrlPeekLatchFake &#125;,

	&#123; &#123; 0, NULL &#125;, "sceController_Service", "sceCtrl", 0x0b588501, sceCtrlReadLatchFake &#125;,





//	Attempt at getting nid overloading working with menus.

//	Can't get this to work&#58; moduleHookFunc&#91;4&#93; return &#58; 0x2

//  Incorrect NID or something? &#58;&#40;

//	&#123; &#123; 0, NULL &#125;, "sceVshBridge_Driver", "sceVshBridge", 0xc6395c03, vshCtrlReadBufferPositiveFake &#125;,

&#125;;

#endif

the next is the PRX loader

Code: Select all

#include <pspsdk.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <stdlib.h>
#include <string.h>
#include <pspdisplay.h>
#include "grap.h"

/* Define the module info section */
PSP_MODULE_INFO&#40;"CONTROLTEST", 0, 1, 1&#41;;
PSP_HEAP_SIZE_KB&#40;20480&#41;;

/* Define the main thread's attribute value &#40;optional&#41; */
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;

/* Define printf, just to make typing easier */
#define printf	pspDebugScreenPrintf

void dump_threadstatus&#40;void&#41;;
int HookStart&#40;void&#41;;

int done = 0;




/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
	done = 1;
	return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
	int cbid;

	cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;
	sceKernelSleepThreadCB&#40;&#41;;

	return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41;
&#123;
	int thid = 0;

	thid = sceKernelCreateThread&#40;"update_thread", CallbackThread,
				     0x11, 0xFA0, 0, 0&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;

	return thid;
&#125;




void DrawPic&#40;int x, int y, int Flag, u32 color&#41;
&#123;
    switch&#40;Flag&#41;
     &#123;
     case 0&#58;     // &#19977;&#35282;
           DrawTri&#40;x, y, color&#41;;
           break;
      case 1&#58;    // &#26041;&#22359;
          DrawRect&#40;x, y, color&#41;;
          break;
      case 2&#58;    //  &#21449;
          DrawCross&#40;x, y, color&#41;;
          break;
      case 3&#58;   // &#22278;&#22280;
          DrawCir&#40;x, y, color&#41;;
          break;
     &#125;
&#125;

int main&#40;void&#41;
&#123;
      int x=200, y=100;
      int flag=0;
      u32 color=ARGB&#40;255,0,255,0&#41;;
      int Counter=0;
    int rets;
	SceCtrlData pad;
   SceUID modid;

	pspDebugScreenInit&#40;&#41;;
	SetupCallbacks&#40;&#41;;

      // mode 0, pixel format&#58; ABGR
	sceDisplaySetMode&#40;0, SCREEN_WIDTH, SCREEN_HEIGHT&#41;;

	sceCtrlSetSamplingCycle&#40;0&#41;;
	sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;

    modid = pspSdkLoadStartModule&#40;"keyhook.prx", PSP_MEMORY_PARTITION_KERNEL&#41;;
    if &#40;modid < 0&#41;
        &#123;
        pspDebugScreenPrintf&#40;"Error 0x%08X loading/starting keyhook.prx.\n", modid&#41;;
        sceKernelDelayThread&#40;5*1000*1000&#41;;
        sceKernelExitGame&#40;&#41;;
        &#125;

   pspDebugScreenPrintf&#40;"load hook\n"&#41;;
   rets=HookStart&#40;&#41;;
   if&#40;rets!=0&#41;
       &#123;
      pspDebugScreenPrintf&#40;"load failed. return value&#58; %d\n", rets&#41;;
       &#125;
   else
      pspDebugScreenPrintf&#40;"load ok. return value&#58; %d\n",rets&#41;;
	while&#40;!done&#41;&#123;
		pspDebugScreenSetXY&#40;0, 2&#41;;

    		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
            printf&#40;"This is a PSP key test program. have fun!         www.ACGclub.com  \n"&#41;; 

		printf&#40;"Analog X = %d ", pad.Lx&#41;;
		printf&#40;"Analog Y = %d \n", pad.Ly&#41;;
             FillRect&#40;x-1,y-1,19,19, ARGB&#40;255,0,0,0&#41;&#41;;
             DrawPic&#40;x,y, flag, color&#41;;

		if &#40;pad.Buttons != 0&#41;&#123;
			if &#40;pad.Buttons & PSP_CTRL_SQUARE&#41;&#123;
				printf&#40;"Square pressed \n"&#41;;
                         flag = 1;
			&#125;
			if &#40;pad.Buttons & PSP_CTRL_TRIANGLE&#41;&#123;
				printf&#40;"Triangle pressed \n"&#41;;
                         flag =0;
			&#125; 
			if &#40;pad.Buttons & PSP_CTRL_CIRCLE&#41;&#123;
				printf&#40;"Cicle pressed \n"&#41;;
                         flag =3;
			&#125; 
			if &#40;pad.Buttons & PSP_CTRL_CROSS&#41;&#123;
				printf&#40;"Cross pressed \n"&#41;;
                         flag =2;
			&#125; 

			if &#40;pad.Buttons & PSP_CTRL_UP&#41;&#123;
				printf&#40;"Up pressed \n"&#41;;
                         y--;
                         if&#40;y<0&#41;&#123;
                                y=0;
                                &#125;
			&#125; 
			if &#40;pad.Buttons & PSP_CTRL_DOWN&#41;&#123;
				printf&#40;"Down pressed \n"&#41;;
                         y++;
                         if&#40;y>SCREEN_HEIGHT&#41; y=SCREEN_HEIGHT;
			&#125; 
			if &#40;pad.Buttons & PSP_CTRL_LEFT&#41;&#123;
				printf&#40;"Left pressed \n"&#41;;
                         x--;
                         if&#40;x<0&#41; x=0;
			&#125; 
			if &#40;pad.Buttons & PSP_CTRL_RIGHT&#41;&#123;
				printf&#40;"Right pressed \n"&#41;;
                         x++;
                         if&#40;x>SCREEN_WIDTH&#41;  x=SCREEN_WIDTH;
			&#125;      

			if &#40;pad.Buttons & PSP_CTRL_START&#41;&#123;
				printf&#40;"Start pressed \n"&#41;;
                         Counter++;
                         if&#40;Counter>3&#41; Counter = 0;
                         switch&#40;Counter&#41;&#123;
                          case 0&#58; color=ARGB&#40;255, 255, 0, 0&#41;; break;
                          case 1&#58; color=ARGB&#40;255, 255, 255, 0&#41;; break;
                          case 2&#58; color=ARGB&#40;255, 255, 0, 255&#41;; break;
                          case 3&#58; color=ARGB&#40;255, 0, 255, 0&#41;;   break;
                                &#125;
			&#125;
			if &#40;pad.Buttons & PSP_CTRL_SELECT&#41;&#123;
				printf&#40;"Select pressed \n"&#41;;
			&#125;
			if &#40;pad.Buttons & PSP_CTRL_LTRIGGER&#41;&#123;
				printf&#40;"L-trigger pressed \n"&#41;;
			&#125;
			if &#40;pad.Buttons & PSP_CTRL_RTRIGGER&#41;&#123;
				printf&#40;"R-trigger pressed \n"&#41;;
			&#125;      
		&#125;
	&#125;

	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;

I modify the code base on RemaPSP. I modify the
void overrideControls(SceCtrlData *pad_data)
function, left others.
ldqmoon
Posts: 13
Joined: Tue Dec 04, 2007 1:17 am

Post by ldqmoon »

I find out that when I call moduleHookFunc function like this:
ret = moduleHookFunc(&mainHookSave[x].modfunc, sceKernelSearchModuleByName(mainHookSave[x].modname), mainHookSave[x].libname, mainHookSave[x].nid, mainHookSave[x].func);

it always stop here( the red line next), and return 2, it seems no address found.

the moduleHookFunc function from the module.c,

u32 moduleHookFunc (ModuleFunc *modfunc, SceUID modid, const char *library, SceUID nid, void *func)

{

u32 *addr;




// Verify parameters

if ((!(modfunc)) || (!(library)) || (!(func))) return 1;



// Find address of function in entry table and get pointer in entry table

addr = moduleFindFunc(moduleFindLibrary(modid,library),nid);



// If not found

if (!(addr)) return 2;



// Copy address of function in structure

modfunc->addr = *addr;



// Find address of function in syscall table and get pointer in syscall table

modfunc->sysaddr = moduleFindSyscallFunc(modfunc->addr);



// If not found

if (!(modfunc->sysaddr)) return 3;



// Hook function (copy func address to syscall table, overwrite old func)

return moduleHookAddr(modfunc->sysaddr,(u32) func);

}
Post Reply