Trying to load an elf

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

Moderators: cheriff, TyRaNiD

Post Reply
Ruj89
Posts: 12
Joined: Tue Jun 12, 2007 7:12 am

Trying to load an elf

Post by Ruj89 »

I'm using this piece of code to load a 1.00 eboot
unpack-pbp.h

Code: Select all

// Dan Peori (peori@oopo.net)
// modded by Ruggero Russo (ruj89@crazynet.org)
// 0: no errors
//-1: infile errors
//-2: wrong infile format
//-3: memory allocation errors
//-4: outfile errors

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <string.h>
#include <stdlib.h>

#define printf pspDebugScreenPrintf

typedef struct &#123; char signature&#91;4&#93;; int version; int offset&#91;8&#93;; &#125; HEADER;

int extract_elf&#40;char file_path&#91;256&#93;&#41; &#123;
  HEADER header; 
  int total_size, size, infile, outfile;
  void *buffer; 

  // Check the argument count.
  if&#40;file_path == ""&#41; &#123; printf&#40;"USAGE&#58; %s <filename>\n", file_path&#41;; return -1; &#125;

  // Open the input file.
  if&#40;!&#40;infile = sceIoOpen&#40;file_path, PSP_O_RDONLY, 0777&#41;&#41;&#41; &#123; printf&#40;"ERROR&#58; Could not open the input file. &#40;%s&#41;\n", file_path&#41;; return -1; &#125;

  // Get the input file size.
  total_size = sceIoLseek&#40;infile, 0, PSP_SEEK_END&#41;; 
  sceIoLseek&#40;infile, 0, PSP_SEEK_SET&#41;;
  if &#40;total_size < 0&#41; &#123; printf&#40;"ERROR&#58; Could not get the input file size.\n"&#41;; return -1; &#125;

  // Read in the input file header.
  if &#40;sceIoRead&#40;infile, &header, sizeof&#40;HEADER&#41;&#41; < 0&#41; &#123; printf&#40;"ERROR&#58; Could not read the input file header.\n"&#41;; return -1; &#125;

  // Check the input file signature.
  if &#40;header.signature&#91;0&#93; != 0x00&#41; &#123; printf&#40;"ERROR&#58; Input file is not a PBP file or it is kxploited.\n"&#41;; return -2; &#125; else
  if &#40;header.signature&#91;1&#93; != 0x50&#41; &#123; printf&#40;"ERROR&#58; Input file is not a PBP file or it is kxploited.\n"&#41;; return -2; &#125; else
  if &#40;header.signature&#91;2&#93; != 0x42&#41; &#123; printf&#40;"ERROR&#58; Input file is not a PBP file or it is kxploited.\n"&#41;; return -2; &#125; else
  if &#40;header.signature&#91;3&#93; != 0x50&#41; &#123; printf&#40;"ERROR&#58; Input file is not a PBP file or it is kxploited.\n"&#41;; return -2; &#125;

  // For data.psp section...  
  // Get the size of the section data.
  size = header.offset&#91;7&#93; - header.offset&#91;6&#93;;
  
  // Allocate the section data buffer.
  buffer = malloc&#40;size&#41;;
  if &#40;buffer == NULL&#41; &#123; printf&#40;"ERROR&#58; Could not allocate the section data buffer. &#40;%d&#41;\n", size&#41;; return -3; &#125;
  
  // Read in the section data.
  if &#40;sceIoRead&#40;infile, buffer, size&#41; < 0&#41; &#123; printf&#40;"ERROR&#58; Could not read in the section data.\n"&#41;; return -1; &#125;
  
  // Open the output file.
  if&#40;!&#40;outfile = sceIoOpen&#40;"ms0&#58;/eboot.elf", PSP_O_WRONLY|PSP_O_CREAT, 0777&#41;&#41;&#41; &#123; printf&#40;"ERROR&#58; Could not open the output file. &#40;eboot.elf&#41;\n"&#41;; return -4; &#125;
  
  // Write out the section data.
  if &#40;sceIoWrite&#40;outfile, buffer, size&#41; < 0&#41; &#123; printf&#40;"ERROR&#58; Could not write out the section data.\n"&#41;; return -4; &#125;
  
  // Close the output file.
  if &#40;sceIoClose&#40;outfile&#41; < 0&#41; &#123; printf&#40;"ERROR&#58; Could not close the output file.\n"&#41;; return -4; &#125;
  
  // Free the section data buffer.
  free&#40;buffer&#41;;
  
  // Output the section information.
  printf&#40;"%8d bytes | eboot.elf\n", size&#41;;
  
  // Close the input file.
  if &#40;sceIoClose&#40;infile&#41; < 0&#41; &#123; printf&#40;"ERROR&#58; Could not close the input file.\n"&#41;; return -1; &#125;

  return 0;
&#125;
main.c

Code: Select all

#include <pspdebug.h>
#include <pspuser.h>
#include <string.h>
#include <pspsdk.h>

#include "unpack-pbp.h"

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

#define printf	pspDebugScreenPrintf

int extract_elf&#40;char file_path&#91;256&#93;&#41;;

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
	sceKernelExitGame&#40;&#41;;
	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;

	/* Note the use of THREAD_ATTR_USER to ensure the callback thread is in user mode */
	thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, PSP_THREAD_ATTR_USER, NULL&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;

	return thid;
&#125;

int main&#40;int argc, char **argv&#41;
&#123;
  SceKernelLMOption option;
  int status;
  SceUID ssaver;

  pspDebugScreenInit&#40;&#41;;
  SetupCallbacks&#40;&#41;;
  extract_elf&#40;"ms0&#58;/eboot_mod.pbp"&#41;;

  memset&#40;&option, 0, sizeof&#40;option&#41;&#41;;
  option.size = sizeof&#40;option&#41;;
  option.mpidtext = 2;
  option.mpiddata = 2;
  option.position = 0;
  option.access = 1;

  ssaver = sceKernelLoadModule&#40;"ms0&#58;/eboot.elf",0,&option&#41;;
  if&#40;ssaver < 0&#41; printf&#40;"Can't load the elf file"&#41;;
  else sceKernelStartModule&#40;ssaver,0,NULL,&status,NULL&#41;;

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

Why does it show the error message "Can't load the elf file"? I tried to extract the elf using PSPBrew and to comment the extract_elf function, but it was the same...
Last edited by Ruj89 on Tue Jun 12, 2007 6:35 pm, edited 1 time in total.
be2003
Posts: 144
Joined: Thu Apr 20, 2006 2:46 pm

Post by be2003 »

my guess is that you cannot load and start an elf the same way you load and start a prx module

im pretty sure you can load a 1.00 eboot using sceKernelLoadExec
the prototype is in psploadexec.h
- be2003
blog
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);

use 0 for kmode thread
10011011 00101010 11010111 10001001 10111010
be2003
Posts: 144
Joined: Thu Apr 20, 2006 2:46 pm

Post by be2003 »

man, i constantly feel like an idiot
- be2003
blog
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

Pick your head up, cuz there are lots of people who would like to see you Fall :)

so don't get yourself down
10011011 00101010 11010111 10001001 10111010
Ruj89
Posts: 12
Joined: Tue Jun 12, 2007 7:12 am

Post by Ruj89 »

dot_blank wrote:PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);

use 0 for kmode thread
It's the same...
I use the cf 3.40 and i put my program in GAME150 folder. I also tried in GAME folder, setting it in 1.50 mode, but my program didn't work.

Is cf ok?
be2003 wrote:my guess is that you cannot load and start an elf the same way you load and start a prx module

im pretty sure you can load a 1.00 eboot using sceKernelLoadExec
the prototype is in psploadexec.h
Oh, I know that sceKernelLoadExec can load an eboot, but, as irshell, I don't want to reboot the kernel when I start the homebrew. That's my target...
be2003
Posts: 144
Joined: Thu Apr 20, 2006 2:46 pm

Post by be2003 »

why dont u try using some of the functions in the pspsdk utility library?
- be2003
blog
Ruj89
Posts: 12
Joined: Tue Jun 12, 2007 7:12 am

Post by Ruj89 »

I used

Code: Select all

  ssaver = pspSdkLoadStartModule&#40;"ms0&#58;/eboot.elf",PSP_MEMORY_PARTITION_KERNEL&#41;;
And i got the same result.
That's 3 days i'm trying to mix functions, please help me!
be2003
Posts: 144
Joined: Thu Apr 20, 2006 2:46 pm

Post by be2003 »

u did use all the patches in the pspsdk utility library right?
- be2003
blog
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

you want to add this function just before your defined printf

Code: Select all

__attribute__ &#40;&#40;constructor&#41;&#41;
void loaderInit&#40;&#41;&#123;
   pspKernelSetKernelPC&#40;&#41;;
   pspSdkInstallNoDeviceCheckPatch&#40;&#41;;
   pspSdkInstallNoPlainModuleCheckPatch&#40;&#41;;
   pspSdkInstallKernelLoadModulePatch&#40;&#41;;
&#125;
10011011 00101010 11010111 10001001 10111010
Ruj89
Posts: 12
Joined: Tue Jun 12, 2007 7:12 am

Post by Ruj89 »

These are the errors:

1) If I use the program normally, I have a SCE_KERNEL_ERROR_UNSUPPORTED_PRX_TYPE error

2) If I extract the data.psp using a PC program, there is another error:
SCE_KERNEL_ERROR_PARTITION_MISMATCH

3) If I change the partition to PSP_MEMORY_PARTITION_USER, this is the error:
SCE_KERNEL_ERROR_MEMBLOCK_ALLOC_FAILED


So I understand that my extract_elf function doesn't work, the elf should be loaded in an user partition, and I can't understand the third error.

Actually my main.c code is:

Code: Select all

#include <pspdebug.h>
#include <pspuser.h>
#include <string.h>
#include <pspsdk.h>

#include "unpack-pbp.h"

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

__attribute__ &#40;&#40;constructor&#41;&#41; void loaderInit&#40;&#41;&#123;
   pspKernelSetKernelPC&#40;&#41;;
   pspSdkInstallNoDeviceCheckPatch&#40;&#41;;
   pspSdkInstallNoPlainModuleCheckPatch&#40;&#41;;
   pspSdkInstallKernelLoadModulePatch&#40;&#41;;
&#125;

#define printf	pspDebugScreenPrintf

int extract_elf&#40;char file_path&#91;256&#93;&#41;;

/*&#40;callbacks...&#41;*/

int main&#40;int argc, char **argv&#41;
&#123;
  SceUID ssaver;

  pspDebugScreenInit&#40;&#41;;
  SetupCallbacks&#40;&#41;;
  extract_elf&#40;"ms0&#58;/eboot_mod.pbp"&#41;;

  ssaver = pspSdkLoadStartModule&#40;"ms0&#58;/eboot.elf",PSP_MEMORY_PARTITION_KERNEL&#41;;
  if&#40;ssaver != 0&#41; printf&#40;"ERROR&#58; Can't load the elf file &#40;0x%x&#41;",ssaver&#41;;

  sceKernelSleepThread&#40;&#41;;
  return 0;
&#125;
How can I fix it?
Post Reply