Install a bootstrap and return to application... how?

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

Moderators: cheriff, TyRaNiD

darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Install a bootstrap and return to application... how?

Post by darkness »

Hi,
I'm trying to code a bootstrap to return to my programm when exit.
But how? I'm been able to patch the sceKernelExitGame function, but I'm not been able to return to my programm!
I've never coded a bootstrap or a patch on the fly for a module, so where I can find a example or a src?
Thanks in advance!
(I've searched for it, but in all the post there are no solution)
Cpasjuste
Posts: 214
Joined: Sun May 29, 2005 8:28 am

Post by Cpasjuste »

Hi, here is a sample i wrote for MyPspMenu : http://mydedibox.fr/downloads/PSP/apiHookSample.tar.gz

Hope it can help you.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

cpajuste, I pm'd you in case you didn't notice >.<
Image
Upgrade your PSP
darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

? It works? It's very good and simple!
Congraturations, but how to start it after a launched eboot?
I think is not possible to launch it with a simple thread!
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

I couldn't understand what cpasjuste told me (how to use it.)
The prx patches the function sceKernelExitGame.
but if you load it from your app (at the beginning per example) the patch will work WHILE your app is running but after you exit it by running a game or any homebrew the patch is lost.

this is what he said:
In the "hook prx" i gave you, put your function to load your iso/eboot just before the sceKernelExitGame hook function. Since your module will be loaded before the hook function, it will be effective in this one.
and what he is saying is to put my function to load iso's or homebrew before the code part which patches sceKernelExitGame.
this is what I don't understand because if I don't use the function from the module the patch does not affect anything
Image
Upgrade your PSP
darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

I've understand it, but I'm not been able to start the function for patch sceKernelExitGame!
I've understant that if I start i before launch a pbp or a prx, the function will patch the sceKernelExitGame of my programm and not of the lauched PBP\PRX!
I've an idea to load the function as a plugin, but I don't know how!
However thanks a lot!
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

You must load the prx with pspSdkLoadStartModule.
try loading to User partition, I only tried to Kernel.
Image
Upgrade your PSP
darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

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

Post by Pirata Nervo »

I tried User partition and gives an error which means partition mismatch.
Probably because I am running user mode and trying to load a module to the same partition I am loading.
I will try loading a module to kernel mode partition and from that module load the prx to user partition.
as sceKernelLoadExec is from LoadExecForUser maybe it can only be hooked from user partition?
Image
Upgrade your PSP
Cpasjuste
Posts: 214
Joined: Sun May 29, 2005 8:28 am

Post by Cpasjuste »

This code should help you both, it's the mypspmenu eboot/iso loader code, it's a bit crappy for now, i wanted to clean it before but anyway ...
The sceKernelExitGame hook is taken from the psplinkusb sources (thanks Tyranid).

From your application, loadexec an user mode .pbp that will load this kernel mode module.
The rest is easy to understand (i think) by reading the above code :

Code: Select all

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/unistd.h>
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspiofilemgr.h>
#include <psptypes.h>
#include <pspsdk.h> 
#include <stdlib.h>
#include <pspumd.h>

#include "../../include/systemctrl_se.h"
#include "../../include/pspmodulemgr_kernel.h"
#include "../../include/systemctrl.h"
#include "nethost.h"
#include "common.h"

#define HOSTFSDRIVER_NAME "USBHostFSDriver"
#define HOSTFSDRIVER_PID  &#40;0x1C9&#41;
#define PSP_USBBUS_DRIVERNAME "USBBusDriver"

PSP_MODULE_INFO&#40;"mypspmenu_eboot_loader_main", 0x1000, 1, 1&#41;;   // 0x1000 = Kernel MODE
PSP_MAIN_THREAD_ATTR&#40;0&#41;;

int line = 1;
char buffer&#91;5&#93;&#91;128&#93;;

void doBlit&#40;char *msg&#41;
&#123;
	blit_string&#40;1, line, msg,0xffffff,0x000000&#41;;
	line++;
&#125;

void remap&#40;int usb&#41;
&#123;
	SceModule *mod;
	mod = sceKernelFindModuleByName&#40;"sceIOFileManager"&#41;;
	if&#40;mod == NULL&#41;	printf&#40;"FAIL => sceIOFileManager module not found\n"&#41;;
	u32 * addr;
	//addr = &#40;u32 *&#41;&#40;mod->gp_value+0x2b30&#41;;
	addr = &#40;u32 *&#41;&#40;mod->gp_value+0x2AE0&#41;;
	printf&#40;"addr &#58; 0x%08X\n", mod->gp_value+0x2AE0&#41;;
	memset&#40;addr, 0, 64&#41;;
	
	sceIoUnassign&#40;"ms0&#58;"&#41;;
	if&#40;usb&#41; sceIoAssign&#40;"ms0&#58;", "usbhost0&#58;", NULL, IOASSIGN_RDWR, NULL, 0&#41;;
	else sceIoAssign&#40;"ms0&#58;", "nethost0&#58;", NULL, IOASSIGN_RDWR, NULL, 0&#41;;
&#125;

void execEboot&#40;char *target&#41;
&#123;	
	struct SceKernelLoadExecVSHParam param;
			
	memset&#40;&param, 0, sizeof&#40;param&#41;&#41;;
			
	param.key = "game";
	param.size = sizeof&#40;param&#41;;
	param.args = strlen&#40;target&#41;+1;
	param.argp = target;

	sctrlKernelLoadExecVSHMs2&#40;target, &param&#41;;	

&#125;

void exit_to_mypspmenu&#40;void&#41; &#123;

	 execEboot&#40;"ms0&#58;/PSP/GAME/mypspmenu/EBOOT.PBP"&#41;;

&#125;


int unload_loader&#40;void&#41;

&#123;
	sceKernelDelayThread&#40;10*100000&#41;;


	SceModule *mod;

	SceUID modid;

	int ret = 0;

	int status;



	mod = sceKernelFindModuleByName&#40;"MYPSPMENU_BOOTLOADER"&#41;;

	if&#40;mod != NULL&#41;

	&#123;

		modid = mod->modid;

		ret = sceKernelStopModule&#40;modid, 0, NULL, &status, NULL&#41;;

		if&#40;ret >= 0&#41;

		&#123;

			ret = sceKernelUnloadModule&#40;modid&#41;;

		&#125;

	&#125;

	else

	&#123;

		return 0;

	&#125;



	return 0;

&#125;



int build_args&#40;char *args, const char *execfile, int argc, char **argv&#41;

&#123;

	int loc = 0;

	int i;



	strcpy&#40;args, execfile&#41;;

	loc += strlen&#40;execfile&#41; + 1;

	for&#40;i = 0; i < argc; i++&#41;

	&#123;

		strcpy&#40;&args&#91;loc&#93;, argv&#91;i&#93;&#41;;

		loc += strlen&#40;argv&#91;i&#93;&#41; + 1;

	&#125;



	return loc;

&#125;

int loadStartModuleWithArgs&#40;const char *name, int argc, char **argv&#41;
&#123;
	SceUID modid;
	int status;
	char args&#91;128&#93;;
	int len;

	sprintf&#40;tmp, "INFO => loading %s", name&#41;;
	doBlit&#40;tmp&#41;;
	modid = sceKernelLoadModule&#40;name, 0, NULL&#41;;
	if&#40;modid >= 0&#41;
	&#123;
		doBlit&#40;"SUCCESS => sceKernelStartModule"&#41;;
		len = build_args&#40;args, name, argc, argv&#41;;
		modid = sceKernelStartModule&#40;modid, len, &#40;void *&#41; args, &status, NULL&#41;;
	&#125;
	else
	&#123;
		sprintf&#40;tmp, "FAIL => error %x", modid&#41;;
		doBlit&#40;tmp&#41;;
		doBlit&#40;"INFO => Exiting in a few seconds..."&#41;;
		sceKernelDelayThread&#40;20*1000000&#41;;
		exit_to_mypspmenu&#40;&#41;;
	&#125;

	return modid;
&#125;


int loadStartDiscModule&#40;const char *name, int argc, char **argv&#41;

&#123;

	SceUID modid;

	int status;

	char args&#91;1024&#93;;

	int len;



	SceKernelLMOption option;

	SceUID mpid = PSP_MEMORY_PARTITION_USER;



	memset&#40;&option, 0, sizeof&#40;option&#41;&#41;;

   

	option.size = sizeof&#40;option&#41;;

	option.mpidtext = mpid;

	option.mpiddata = mpid;

	option.position = 0;

	option.access = 1; 

		

	modid = sceKernelLoadModuleDisc&#40;name, 0, &option&#41;;

	if&#40;modid >= 0&#41;

	&#123;

		len = build_args&#40;args, name, argc, argv&#41;;

		modid = sceKernelStartModule&#40;modid, len, &#40;void *&#41; args, &status, NULL&#41;;

	&#125;

	else

	&#123;
		sprintf&#40;tmp, "FAIL => error %x", modid&#41;;

		doBlit&#40;tmp&#41;;
		doBlit&#40;"INFO => Exiting in a few seconds..."&#41;;
		sceKernelDelayThread&#40;20*1000000&#41;;
		exit_to_mypspmenu&#40;&#41;;

	&#125;



	return modid;

&#125;

int loadStartModuleWithoutArgs&#40;char *path&#41;
&#123;
	u32 loadResult;
 	u32 startResult;
 	int status;

	loadResult = kuKernelLoadModule&#40;path, 0, NULL&#41;;
	if &#40;loadResult & 0x80000000&#41;&#123;
		return -1;
	&#125;
	else
	&#123;
		startResult = sceKernelStartModule&#40;loadResult, 0, NULL, &status, NULL&#41;;
	&#125;

	if &#40;loadResult != startResult&#41;&#123;
		return -2;
	&#125;
	return 0;
&#125; 


int loadStartModule&#40;const char *name, int argc, char **argv, int mode&#41;

&#123;

	SceUID modid;

	int status;

	char args&#91;1024&#93;;

	int len;

	if&#40;mode==2&#41; name = "disc0&#58;/PSP_GAME/SYSDIR/EBOOT.BIN";



	SceKernelLMOption option;

	SceUID mpid = PSP_MEMORY_PARTITION_USER;



	memset&#40;&option, 0, sizeof&#40;option&#41;&#41;;

   

	option.size = sizeof&#40;option&#41;;

	option.mpidtext = mpid;

	option.mpiddata = mpid;

	if&#40;mode != 2&#41; option.position = 1; else option.position = 0;

	option.access = 1; 


	if&#40;mode==0&#41; // normal eboot
	&#123;
		sprintf&#40;tmp,"ld => %s", name&#41;;
		doBlit&#40;tmp&#41;;
		modid = sceKernelLoadModuleMs2&#40;name, 0, &option&#41;;
	&#125;
	else if&#40;mode==1&#41; // psx eboot
	&#123;
		//printf&#40;"loading module => %s\n", name&#41;;
		sprintf&#40;tmp,"ld => %s", name&#41;;
		doBlit&#40;tmp&#41;;
		modid = sceKernelLoadModuleMs4&#40;name, 0, NULL&#41;;
	&#125;

	else if&#40;mode==2&#41; // umd eboot
	&#123;
		sprintf&#40;tmp,"ld => %s", name&#41;;
		doBlit&#40;tmp&#41;;
		modid = ModuleMgrForKernel_A1A78C58&#40;name, 0, &option&#41;;
	&#125;


	if&#40;modid >= 0&#41;

	&#123;

		len = build_args&#40;args, name, argc, argv&#41;;			

		modid = sceKernelStartModule&#40;modid, len, &#40;void *&#41; args, &status, NULL&#41;;
		doBlit&#40;"SUCCESS => sceKernelStartModule&#40;&#41;"&#41;;

	&#125;

	else

	&#123;
		//printf&#40;"could not load module => %s || Error => %x\n", name, modid&#41;;
		sprintf&#40;tmp,"FAIL => error %x", modid&#41;;
		doBlit&#40;tmp&#41;;
		doBlit&#40;"INFO => Exiting please wait..."&#41;;
		sceKernelDelayThread&#40;20*1000000&#41;;
		exit_to_mypspmenu&#40;&#41;;
		//execEboot&#40;"ms0&#58;/PSP/GAME/psplink/EBOOT.PBP"&#41;;
		//sceKernelSleepThread&#40;&#41;;

		return 0;

	&#125;



	return modid;

&#125;

int loadNetHost&#40;&#41;
&#123;
	strncpy&#40;address, buffer&#91;4&#93;, 16&#41;;
	strncpy&#40;port, "7513", 8&#41;;
	strncpy&#40;entry, "1", 9&#41;;
	strncpy&#40;adhocAddress, "0", 16&#41;;
	startNetHost&#40;address, port, entry, password, adhocAddress&#41;;
	
	return 0;
&#125;

int loadUsbHost&#40;&#41; 
&#123;

	loadStartModuleWithoutArgs&#40;"ms0&#58;/PSP/GAME/mypspmenu/system/usbhostfs.prx"&#41;;

	int retVal = 0;

	retVal =  sceUsbStart&#40;PSP_USBBUS_DRIVERNAME, 0, 0&#41;; //SceUsbStart

	if &#40;retVal != 0&#41; &#123;

		printf&#40;"Error starting USB Bus driver\n"&#41;;

		return 0;

	&#125;

	retVal =  sceUsbStart&#40;HOSTFSDRIVER_NAME, 0, 0&#41;;

	if &#40;retVal != 0&#41; &#123;

		printf&#40;"Error starting USB Host driver\n"&#41;;

		return 0;

	&#125;

	retVal = sceUsbActivate&#40;HOSTFSDRIVER_PID&#41;; //SceUsbActivate

			

	sceKernelDelayThread&#40;30*100000&#41;;



	return 0;

&#125;

static int readLine&#40;SceUID fd, char *Line, int MaxCharacters&#41;
&#123;
	int i;
	int Read;
	char TempCharacter;

	i = 0;

	if &#40;MaxCharacters == 0&#41;
	&#123;
		return 1;
	&#125;

	do
	&#123;
		Read = sceIoRead&#40;fd, &TempCharacter, 1&#41;;

		if &#40;Read && TempCharacter != '\n' && TempCharacter != '\r'&#41;
		&#123;
			Line&#91;i++&#93; = TempCharacter;
		&#125;

	&#125; while &#40;TempCharacter != '\n' && Read == 1 && i < MaxCharacters&#41;;

	Line&#91;i&#93; = 0;

	return !Read;
&#125;


int main_thread&#40;SceSize args, void *argp&#41;

&#123;
	int count = 0, eof = 0;


	unload_loader&#40;&#41;;


	sceKernelDelayThread&#40;10*100000&#41;;

	
	// Read and load the configuration line by line
	SceUID fd = sceIoOpen&#40;"ms0&#58;/PSP/GAME/mypspmenu/system/conf.log", PSP_O_RDONLY, 0777&#41;;
	while &#40;!eof&#41;

	&#123;

		eof = readLine&#40;fd, buffer&#91;count&#93;, 128&#41;;
		//doBlit&#40;buffer&#91;count&#93;&#41;;
		count++;
	&#125;
	sceIoClose&#40;fd&#41;;
	sceIoRemove&#40;"ms0&#58;/PSP/GAME/mypspmenu/system/conf.log"&#41;; 
	sceKernelDelayThread&#40;10*100000&#41;;

	strcpy&#40;tmpPrx, buffer&#91;0&#93;&#41;;
	strcat&#40;tmpPrx, ".bin"&#41;;

	if&#40;strcmp&#40;buffer&#91;1&#93;, "homebrew"&#41; == 0&#41; 
	&#123;
		doBlit&#40;"INFO => homebrew mode enabled"&#41;;
		if&#40;strcmp&#40;buffer&#91;3&#93;, "nethost"&#41; == 0&#41; 
		&#123;
			doBlit&#40;"INFO => nethost mode enabled ..."&#41;;
			doBlit&#40;"INFO => please wait while initializing net driver ..."&#41;;
			loadNetHost&#40;&#41;;
			remap&#40;0&#41;;
			loadStartModule&#40;buffer&#91;0&#93;, 0, NULL, 0&#41;;
		&#125;
		else if&#40;strcmp&#40;buffer&#91;3&#93;, "usbhost"&#41; == 0&#41;
		&#123;
			doBlit&#40;"INFO => usbhost mode enabled ..."&#41;;
			doBlit&#40;"INFO => please wait while initializing usb driver ..."&#41;;
			loadUsbHost&#40;&#41;;
			remap&#40;1&#41;;
			loadStartModule&#40;buffer&#91;0&#93;, 0, NULL, 0&#41;;
		&#125;
		else
		&#123;

			loadStartModule&#40;buffer&#91;0&#93;, 0, NULL, 0&#41;;
		&#125;
	&#125; 
	else if&#40;strcmp&#40;buffer&#91;1&#93;, "psx"&#41; == 0&#41; &#123;
		doBlit&#40;"INFO => psx mode enabled"&#41;;

		loadStartModule&#40;buffer&#91;0&#93;, 0, NULL, 1&#41;;
	&#125; 
	else if&#40;strcmp&#40;buffer&#91;1&#93;, "umd"&#41; == 0&#41; &#123;
		doBlit&#40;"INFO => umd mode enabled"&#41;;

		int i = sceUmdCheckMedium&#40;&#41;;

		if&#40;!&#40;i&#41;&#41; sceUmdWaitDriveStat&#40;UMD_WAITFORDISC&#41;;

		sceUmdActivate&#40;1, "disc0&#58;"&#41;;

		sceUmdWaitDriveStat&#40;UMD_WAITFORINIT&#41;;

		loadStartModule&#40;buffer&#91;0&#93;, 0, NULL, 2&#41;;
	&#125;
	else if&#40;strcmp&#40;buffer&#91;1&#93;, "iso"&#41; == 0&#41; 
	&#123;
		doBlit&#40;"INFO => iso mode enabled"&#41;;		

		if&#40;strcmp&#40;buffer&#91;3&#93;, "nethost"&#41; == 0&#41; 
		&#123;
			doBlit&#40;"INFO => nethost mode enabled ..."&#41;;
			doBlit&#40;"INFO => please wait while initializing net driver ..."&#41;;
			loadNetHost&#40;&#41;;
			//sceKernelDelayThread&#40;100*120000&#41;;
			sctrlSEUmountUmd&#40;&#41;;
			sctrlSEMountUmdFromFile&#40;buffer&#91;0&#93;, 0, 1&#41;;
		&#125;
		else if&#40;strcmp&#40;buffer&#91;3&#93;, "usbhost"&#41; == 0&#41; 
		&#123;
			doBlit&#40;"INFO => usbhost mode enabled ..."&#41;;
			doBlit&#40;"INFO => please wait while initializing usb driver ..."&#41;;
			loadUsbHost&#40;&#41;;
			sctrlSEUmountUmd&#40;&#41;;
			sctrlSEMountUmdFromFile&#40;buffer&#91;0&#93;, 0, 1&#41;;
		&#125;
		else
		&#123;
			sctrlSEUmountUmd&#40;&#41;;
			sctrlSEMountUmdFromFile&#40;buffer&#91;0&#93;, 0, 1&#41;;
		&#125;
		doBlit&#40;"INFO => starting iso..."&#41;;
		int i = sceUmdCheckMedium&#40;&#41;;

		if&#40;!&#40;i&#41;&#41; sceUmdWaitDriveStat&#40;UMD_WAITFORDISC&#41;;

		sceUmdActivate&#40;1, "disc0&#58;"&#41;;

		sceUmdWaitDriveStat&#40;UMD_WAITFORINIT&#41;;

		

		loadStartDiscModule&#40;"disc0&#58;/PSP_GAME/SYSDIR/EBOOT.BIN", 0, NULL&#41;;
	&#125;

	// Hook exit game function

	if&#40;atoi&#40;buffer&#91;2&#93;&#41;==66&#41; sctrlHENSetSpeed&#40;66, 33&#41;;
	else if&#40;atoi&#40;buffer&#91;2&#93;&#41;==111&#41; sctrlHENSetSpeed&#40;111, 55&#41;;
	else if&#40;atoi&#40;buffer&#91;2&#93;&#41;==222&#41; sctrlHENSetSpeed&#40;222, 111&#41;;
	else if&#40;atoi&#40;buffer&#91;2&#93;&#41;==266&#41; sctrlHENSetSpeed&#40;266, 133&#41;;
	else if&#40;atoi&#40;buffer&#91;2&#93;&#41;==333&#41; sctrlHENSetSpeed&#40;333, 133&#41;;

	SceModule *mod;
	mod = sceKernelFindModuleByName&#40;"sceLoadExec"&#41;;
	if&#40;mod&#41; apiHookByName&#40;mod->modid, "LoadExecForUser","sceKernelExitGame", exit_to_mypspmenu&#41;;

	sceKernelExitDeleteThread&#40;0&#41;;

	

	return 0;

&#125;



/* Entry point */

int module_start&#40;SceSize args, void *argp&#41;

&#123;

	int thid;



	/* Create a high priority thread */

	thid = sceKernelCreateThread&#40;"mypspmenu_eboot_loader_main_th", main_thread, 8, 0x1000, 0, NULL&#41;;

	if&#40;thid >= 0&#41;

	&#123;

		sceKernelStartThread&#40;thid, args, argp&#41;;

	&#125;



	return 0;

&#125;

int module_stop&#40;&#41;

&#123;
	sceKernelExitDeleteThread&#40;0&#41;;

	return 0;

&#125;

Last edited by Cpasjuste on Thu Jun 19, 2008 11:33 pm, edited 2 times in total.
darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

I've started it in Kernel memory partition and it works perfectly!
Excuse for my english but I've don't understand very well what you sayd!
I'm trying to start the prx with args, but I don't know how!
Is very simple I think but I don't know the structure of the args!
Example:

Code: Select all

/* I have to load the prx with a path
 * so, i've try to start the prx with args&#58;
 */

pspSdkLoadStartModule&#40;"ms0&#58;/PSP/GAME/Explorer/krnlmod.prx", PSP_MEMORY_PARTITION_KERNEL, 14, "random string"&#41;;
And in the prx:

Code: Select all

int module_start&#40;SceSize args, void *argp&#41;
&#123;
   char* mystring=&#40;char*&#41;malloc&#40;strlen&#40;argp&#41;&#41;;
   argp=mystring;
   printf&#40;"%s", mystring&#41;;
   ...
   ...
&#125;
This is that I want to do, but is fill of errors!
Can you give me a little help?
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

darkness:
what do you mean by "I've started it in Kernel memory partition and it works perfectly! "
started the pbp or the prx?
because I tried the prx and does not.
@cpasjute, thanks.
I will take a look at the code and see if I understand
Image
Upgrade your PSP
darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

cpasjute, I haven't understand very well your code!
This is the prx to load eboot etc.. and patch the exit game fuct?
I've writed this code, but the exit game funct is not be patched!

Code: Select all

    pspSdkLoadStartModule&#40;"ms0&#58;/PSP/GAME/Explorer/apihook.prx", PSP_MEMORY_PARTITION_KERNEL&#41;;
    
    execEboot&#40;"ms0&#58;/PSP/GAME/AdhocFileTransfer/EBOOT.PBP"&#41;;
Why? The prx loaded is stopped when load the eboot?
The prx works, because if I don't start a eboot it patch the exit game funct of my programm, and return to my programm!
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

I am trying something, should work.
I will let you know and post there if it works.
Image
Upgrade your PSP
darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

I've try to start the prx as a plugin, (I've put in in game.txt file), and It works perfectly!!!
But how to start it as a plugin from a application?
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

that's what I am trying to do lol.
Image
Upgrade your PSP
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

From what I can understand from Cpasjuste's code, it reads the file path from a file?
then loads and starts it with loadStartModule function.
right?

Well I tried this:

Code: Select all

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/unistd.h>
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspiofilemgr.h>
#include <psptypes.h>
#include <pspsdk.h>
#include <stdlib.h>
#include <pspumd.h>



#include "include/systemctrl_se.h"

#include "include/pspmodulemgr_kernel.h"

#include "include/systemctrl.h"
#include "apihook.h"



PSP_MODULE_INFO&#40;"hook sample", 0x1000, 1, 1&#41;;

PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;

int execEboot&#40;char *target&#41;
&#123;
	int loadexec = 0;

        struct SceKernelLoadExecVSHParam param;
        char argp&#91;256&#93;;
        int  args;

        strcpy&#40;argp, target&#41;;
        args = strlen&#40;target&#41;+1;

        memset&#40;&param, 0, sizeof&#40;param&#41;&#41;;
        param.size = sizeof&#40;param&#41;;
        param.args = args;
        param.argp = argp;
        param.key = NULL;
        param.vshmain_args_size = 0;
        param.vshmain_args = NULL;
        //sceKernelSuspendAllUserThreads&#40;&#41;; 

	loadexec = sctrlKernelLoadExecVSHMs2&#40;target, &param&#41;;


	return loadexec;
&#125;

void ExitNervOS&#40;void&#41; &#123;
	 execEboot&#40;"ms0&#58;/PSP/GAME/NervOS/EBOOT.PBP"&#41;;
&#125;

/*int build_args&#40;char *args, const char *execfile, int argc, char **argv&#41;
&#123;
	int loc = 0;
	int i;

	strcpy&#40;args, execfile&#41;;
	loc += strlen&#40;execfile&#41; + 1;

	for&#40;i = 0; i < argc; i++&#41;
	&#123;
		strcpy&#40;&args&#91;loc&#93;, argv&#91;i&#93;&#41;;
		loc += strlen&#40;argv&#91;i&#93;&#41; + 1;
	&#125;
	return loc;
&#125;

int loadStartModule&#40;const char *name, int argc, char **argv, int mode&#41;
&#123;
	SceUID modid = 0;
	int status;

	char args&#91;1024&#93;;
	int len;

	if&#40;mode==2&#41; name = "disc0&#58;/PSP_GAME/SYSDIR/EBOOT.BIN";

	SceKernelLMOption option;
	SceUID mpid = PSP_MEMORY_PARTITION_USER;

	memset&#40;&option, 0, sizeof&#40;option&#41;&#41;;

	option.size = sizeof&#40;option&#41;;
	option.mpidtext = mpid;
	option.mpiddata = mpid;

	if&#40;mode != 2&#41; option.position = 1; else option.position = 0;
	option.access = 1;


	if&#40;mode==0&#41; // normal eboot
		modid = sceKernelLoadModuleMs2&#40;name, 0, &option&#41;;
	else if&#40;mode==1&#41; // psx eboot
		modid = sceKernelLoadModuleMs4&#40;name, 0, NULL&#41;;
	else if&#40;mode==2&#41; // umd eboot
		modid = ModuleMgrForKernel_A1A78C58&#40;name, 0, &option&#41;;

	if&#40;modid >= 0&#41;
	&#123;
		len = build_args&#40;args, name, argc, argv&#41;;         
		modid = sceKernelStartModule&#40;modid, len, &#40;void *&#41; args, &status, NULL&#41;;
	&#125;
	else
	&#123;
		sceKernelDelayThread&#40;20*1000000&#41;;
		ExitNervOS&#40;&#41;;

		return 0;
	&#125;

	return modid;
&#125;*/

char filepath&#91;255&#93;;

char * readPath&#40;char * Filename&#41;

&#123;

	char line&#91;255&#93;;

	FILE * File;

	char path&#91;255&#93;;
	strcpy&#40;path, ""&#41;;


	File = fopen&#40;Filename, "r"&#41;;

	if&#40;File == NULL&#41; return NULL;



	while&#40;fgets&#40;line, 255, File&#41; != NULL&#41;

	&#123;
		strcpy&#40;filepath, line&#41;;

	&#125;


	fclose&#40;File&#41;;


	return filepath;

&#125;



int main_thread&#40;SceSize args, void *argp&#41;

&#123;


	sceKernelDelayThread&#40;10*100000&#41;;

	char *path = "";;
	strcpy&#40;path, ""&#41;;

	path = readPath&#40;"ms0&#58;/NervOS/documents/filepath.temp"&#41;;

	//loadStartModule&#40;path, 0, NULL, 0&#41;;

	execEboot&#40;path&#41;;

	// Hook exit game function
	SceModule *mod;
	mod = sceKernelFindModuleByName&#40;"sceLoadExec"&#41;;
	if&#40;mod&#41; apiHookByName&#40;mod->modid, "LoadExecForUser","sceKernelExitGame", ExitNervOS&#41;;

	sceKernelExitDeleteThread&#40;0&#41;;

	

	return 0;

&#125;



/* Entry point */

int module_start&#40;SceSize args, void *argp&#41;

&#123;

	int thid;



	/* Create a high priority thread */

	thid = sceKernelCreateThread&#40;"hook_sample_thread", main_thread, 8, 64*1024, 0, NULL&#41;;

	if&#40;thid >= 0&#41;

	&#123;

		sceKernelStartThread&#40;thid, args, argp&#41;;

	&#125;



	return 0;

&#125;

int module_stop&#40;&#41;

&#123;
	sceKernelExitDeleteThread&#40;0&#41;;

	return 0;

&#125;
But crashes and shows a black screen only.

I also tried with the uncommented codeand with the loadStartModule uncommented and execEboot(path); commented and seems to load something but then crashes too.

Edit: Nvm, it was something in my PBP. It works and launches the correct file but does not patch anything >.< (I tried with loadstart module and without)
Image
Upgrade your PSP
Cpasjuste
Posts: 214
Joined: Sun May 29, 2005 8:28 am

Post by Cpasjuste »

With your "execEboot(path); " function, it won't work of course since you loadexec an eboot. When you loadexec something, the hook is lost ...you need to use the load/start module function to load your .pbp for the hook to be active.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

I used it first but then it didn't work so I tired with the loadexec :P
Image
Upgrade your PSP
darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

Pirata Nervo, have you resolved?
I have a bad idea:
can we patch the loadexec fuct to don't stop the loaded prxs?
I think it's possible...or I haven't understand very well what do the psp when start a new executables
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

Nop I did not try anymore since I made my post with the code. I have been working on other things.

And yes you can patch the function but re-write a loadexec function is not easy, I saw one before made/reversed (probably reversed) by dark alex and of course it's in MIPS and C.
Image
Upgrade your PSP
Cpasjuste
Posts: 214
Joined: Sun May 29, 2005 8:28 am

Post by Cpasjuste »

The code i posted works out of the box, i don't understand what is the problem. If i have the time i will write and post a sample that load a hard coded eboot with the sceKernelExitGame hooked.
darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

Yes, please...
psp.padawan
Posts: 10
Joined: Mon Nov 12, 2007 7:48 pm

Post by psp.padawan »

Hi All,

I use Cpasjuste's api hook sample and it works fine. try calling the apihook first before calling loadStartModule()

Code: Select all

int main_thread&#40;SceSize args, void *argp&#41;
&#123;
   sceKernelDelayThread&#40;10*100000&#41;;

   char *path = "";
   strcpy&#40;path, ""&#41;;

   path = readPath&#40;"ms0&#58;/NervOS/documents/filepath.temp"&#41;;

   // Hook exit game function
   SceModule *mod;
   mod = sceKernelFindModuleByName&#40;"sceLoadExec"&#41;;
   if&#40;mod&#41; apiHookByName&#40;mod->modid, "LoadExecForUser","sceKernelExitGame", ExitNervOS&#41;;

   loadStartModule&#40;path, 0, NULL, 0&#41;;

   return sceKernelExitDeleteThread&#40;0&#41;;
&#125;
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

Yeh I did.
My prx crashes :(
Would you mind to post your prx source code?
I have tried a lot of different ways and nothing.
I am still trying.
Image
Upgrade your PSP
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

Ok this is my prx now:

Code: Select all

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/unistd.h>
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspiofilemgr.h>
#include <psptypes.h>
#include <pspsdk.h>
#include <stdlib.h>
#include <pspumd.h>
#include <pspmodulemgr_kernel.h>
#include <psploadexec_kernel.h>
#include <systemctrl.h>
#include <systemctrl_se.h>

#include "apihook.h"

PSP_MODULE_INFO&#40;"NervOS_PatchExit", 0x1000, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;0&#41;;

int execEboot&#40;char *target&#41;
&#123;
	int loadexec = 0;

        struct SceKernelLoadExecVSHParam param;
        char argp&#91;256&#93;;
        int  args;

        strcpy&#40;argp, target&#41;;
        args = strlen&#40;target&#41;+1;

        memset&#40;&param, 0, sizeof&#40;param&#41;&#41;;
        param.size = sizeof&#40;param&#41;;
        param.args = args;
        param.argp = argp;
        param.key = NULL;
        param.vshmain_args_size = 0;
        param.vshmain_args = NULL;
        //sceKernelSuspendAllUserThreads&#40;&#41;; 

	loadexec = sctrlKernelLoadExecVSHMs2&#40;target, &param&#41;;


	return loadexec;
&#125;

void ExitNervOS&#40;void&#41; &#123;
	 execEboot&#40;"ms0&#58;/PSP/GAME/NervOS/EBOOT.PBP"&#41;;
&#125;

int build_args&#40;char *args, const char *execfile, int argc, char **argv&#41;
&#123;
	int loc = 0;
	int i;

	strcpy&#40;args, execfile&#41;;
	loc += strlen&#40;execfile&#41; + 1;

	for&#40;i = 0; i < argc; i++&#41;
	&#123;
		strcpy&#40;&args&#91;loc&#93;, argv&#91;i&#93;&#41;;
		loc += strlen&#40;argv&#91;i&#93;&#41; + 1;
	&#125;
	return loc;
&#125;

int writeDebug&#40;char * debug&#41;
&#123;
	int fd;

	char buff&#91;30&#93;;
	strcpy&#40;buff, debug&#41;;

	fd = sceIoOpen&#40;"ms0&#58;/debug.txt", PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777&#41;;

	if&#40;!fd&#41; return -1;

	sceIoWrite&#40;fd, buff, sizeof&#40;buff&#41;&#41;;

	sceIoClose&#40;fd&#41;;

	return 0;
&#125;

int loadStartModule&#40;const char *name, int argc, char **argv, int mode&#41;
&#123;
	SceUID modid = 0;
	int status;

	char args&#91;1024&#93;;
	int len;

	if&#40;mode==2&#41; name = "disc0&#58;/PSP_GAME/SYSDIR/EBOOT.BIN";

	SceKernelLMOption option;
	SceUID mpid = PSP_MEMORY_PARTITION_KERNEL;

	memset&#40;&option, 0, sizeof&#40;option&#41;&#41;;

	option.size = sizeof&#40;option&#41;;
	option.mpidtext = mpid;
	option.mpiddata = mpid;

	if&#40;mode != 2&#41; option.position = 1; else option.position = 0;
	option.access = 1;


	if&#40;mode==0&#41; // normal eboot
		modid = sceKernelLoadModuleMs2&#40;name, 0, &option&#41;;
	else if&#40;mode==1&#41; // psx eboot
		modid = sceKernelLoadModuleMs4&#40;name, 0, NULL&#41;;
	else if&#40;mode==2&#41; // umd eboot
		modid = ModuleMgrForKernel_A1A78C58&#40;name, 0, &option&#41;;

	if&#40;modid >= 0&#41;
	&#123;
		len = build_args&#40;args, name, argc, argv&#41;;         
		modid = sceKernelStartModule&#40;modid, len, &#40;void *&#41; args, &status, NULL&#41;;
	&#125;
	else
	&#123;
		writeDebug&#40;"Error executing module"&#41;;
		sceKernelDelayThread&#40;20*1000000&#41;;
		ExitNervOS&#40;&#41;;

		return 0;
	&#125;

	return modid;
&#125;

char filepath&#91;255&#93;;

char * readPath&#40;char * Filename&#41;
&#123;
	int fd = sceIoOpen&#40;Filename, PSP_O_RDONLY, 0777&#41;;

	if&#40;!fd&#41; return NULL;

	sceIoRead&#40;fd, filepath, sizeof&#40;filepath&#41;&#41;;

	sceIoClose&#40;fd&#41;;

	return filepath;

&#125;

int main_thread&#40;SceSize args, void *argp&#41;
&#123;
	sceKernelDelayThread&#40;1000000&#41;;

	char *path = "";;
	strcpy&#40;path, ""&#41;;

	writeDebug&#40;"debug 1"&#41;;

	path = readPath&#40;"ms0&#58;/NervOS/documents/filepath.temp"&#41;;

	writeDebug&#40;"debug 2"&#41;;

	// Hook exit game function
	SceModule *mod;
	mod = sceKernelFindModuleByName&#40;"sceLoadExec"&#41;;
	if&#40;mod&#41; apiHookByName&#40;mod->modid, "LoadExecForUser","sceKernelExitGame", ExitNervOS&#41;;
	else &#123;
		writeDebug&#40;"Error finding sceLoadExec"&#41;;
	&#125;

	loadStartModule&#40;path, 0, NULL, 0&#41;; 

	sceKernelExitDeleteThread&#40;0&#41;;

	return 0;

&#125;



/* Entry point */

int module_start&#40;SceSize args, void *argp&#41;
&#123;
	int thid;

	/* Create a high priority thread */

	thid = sceKernelCreateThread&#40;"hook_sample_thread", main_thread, 8, 64*1024, 0, NULL&#41;;

	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, args, argp&#41;;
	&#125;

	return 0;
&#125;

int module_stop&#40;&#41;
&#123;
	sceKernelExitDeleteThread&#40;0&#41;;
	return 0;
&#125;
the debug.txt file is written with this text inside:
Error executing module
so the problem is inside the loadStartModule function :/
It's not loading/starting it correctly
Image
Upgrade your PSP
psp.padawan
Posts: 10
Joined: Mon Nov 12, 2007 7:48 pm

Post by psp.padawan »

Im creating a sample at the moment. I'll post it as soon as I finish it.

You can also have a look at the hense sdk samples from moonlight. Look at the loadmodule sample. That's basically what I use, Ive just added some stuff into it.

The Ebootloader calls loadprx.prx with the file you want to load as a parameter. Ive put the apihook call in the loadprx.prx kernel module.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

What moonlight samples?
Edit:
Seems to be returning "partition mismatch" when loading the module (homebrew), I am loading it to kernel partition as in the sample. If I change to user partition, I get a failed to allocate the memory block error
Image
Upgrade your PSP
psp.padawan
Posts: 10
Joined: Mon Nov 12, 2007 7:48 pm

Post by psp.padawan »

moonlight released some sdk samples for HEN. You can find it in his website.

Anyway you can try this. I used the 3.71 M33 sdk with it:

Code: Select all

#include <pspsdk.h>
#include <pspkernel.h>
#include <systemctrl.h>
#include <systemctrl_se.h>
#include <apihook.h>

#include <string.h>
#include <stdio.h>

PSP_MODULE_INFO&#40;"LoaderPRX", PSP_MODULE_KERNEL, 1, 0&#41;;
PSP_MAIN_THREAD_ATTR&#40;0&#41;;

void executeEboot&#40;char *target&#41;
&#123;	
	struct SceKernelLoadExecVSHParam param;
	memset&#40;&param, 0, sizeof&#40;param&#41;&#41;;	
	param.size = sizeof&#40;param&#41;;
	param.args = strlen&#40;target&#41;+1;
	param.argp = target;
	param.key = "game";

	sctrlKernelLoadExecVSHMs2&#40;target, &param&#41;;	
&#125;

void reloadMain&#40;&#41; 
&#123;
	executeEboot&#40;"ms0&#58;/PSP/GAME/bootloader/EBOOT.PBP"&#41;;
&#125;

int main_thread&#40;SceSize args, void *argp&#41;
&#123;
	SceModule2 *mod;
	SceUID uid = 0;

	/* Unload the loader to free user memory */
	while &#40;&#40;mod = sceKernelFindModuleByName&#40;"BootLoader"&#41;&#41;&#41;
	&#123;
		sceKernelStopModule&#40;mod->modid, 0, NULL, NULL, NULL&#41;;
		sceKernelUnloadModule&#40;mod->modid&#41;;
	&#125;

	// Hook exit game function
	mod= sceKernelFindModuleByName&#40;"sceLoadExec"&#41;;
	if&#40;mod&#41; apiHookByName&#40;mod->modid, "LoadExecForUser","sceKernelExitGame", reloadMain&#41;; 	

	if &#40;strstr&#40;&#40;char *&#41;argp, ".PBP"&#41;&#41;
	&#123;					
		uid = sceKernelLoadModuleForLoadExecVSHMs2&#40;argp, 0, NULL&#41;;
	&#125;

	if &#40;uid < 0&#41;
	&#123;
		Kprintf&#40;"Error %08X loading module", uid&#41;;
	&#125;

	uid = sceKernelStartModule&#40;uid, strlen&#40;argp&#41;+1, argp, NULL, NULL&#41;;

	if &#40;uid < 0&#41;
	&#123;
		Kprintf&#40;"Error %08X starting module", uid&#41;;
	&#125;

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

int module_start&#40;SceSize args, void *argp&#41;
&#123;
	SceUID th = sceKernelCreateThread&#40;"main_thread", main_thread, 8, 16*1024, 0, NULL&#41;;

	if &#40;th >= 0&#41;
	&#123;
		sceKernelStartThread&#40;th, args, argp&#41;;
	&#125;
	return 0;
&#125;

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

Code: Select all

TARGET = loadprx
OBJS = main.o apihook.o libs.o

INCDIR = ../../include
CFLAGS = -O2 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

BUILD_PRX = 1
PSP_FW_VERSION = 371

USE_KERNEL_LIBC = 1
USE_KERNEL_LIBS = 1

LIBDIR = ../../lib
LIBS = -lpspmodulemgr_kernel -lpspsystemctrl_kernel
LDFLAGS = -mno-crt0 -nostartfiles

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak

darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

Why have you patch the exit game api befor launch the new module?
It's patch the current loaded api( I think), and when launch the new executables the exit game function is not patched!
It's true?
Post Reply