Strange flash0 write access problem

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

Moderators: cheriff, TyRaNiD

Post Reply
Cpasjuste
Posts: 214
Joined: Sun May 29, 2005 8:28 am

Strange flash0 write access problem

Post by Cpasjuste »

Hi.

I have a strange problem to write to the flash0 device on 3.71 firmware. In my new project, i have a little file manager and would like to be able to write to the flash.
I have a kernel module that export some functions to my user module, one of the function reassign the flash0 device in write mode. But it don't work. To find the problem i loaded my project with psplink, and saw that the device was correctly reassigned because i can copy a file with psplink to the flash0 device after executing my unlocking kernel function.
So i tought i would have to execute the copy function in my kernel prx to reproduce the same thing, but it wont work too. It's really strange i don't see where the problem is.

If someone have an idea it would be very cool because this make me silly :)
Cpasjuste
Posts: 214
Joined: Sun May 29, 2005 8:28 am

Post by Cpasjuste »

Maybe i will have some answer with an exemple.
In this kernel module (build for exporting functions), if i uncomment the "copyFileKernel" line in the module start, the function work.
If i call the function from my usermode eboot, the function do not work (of course my exports are working, the "execEboot" function is working fine, also the "copyFileKernel" function is working while writing on flash1).

Its seems to be a new sony syscall protection ?

Code: Select all

#include <psptypes.h>
#include <pspkernel.h>
#include <stdio.h>
#include <string.h>
#include <pspiofilemgr.h>
#include <psptypes.h>

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

PSP_MODULE_INFO&#40;"kernelExports", 0x1000, 0, 1&#41;;
PSP_NO_CREATE_MAIN_THREAD&#40;&#41;;

void copyFileKernel&#40;const char *readpath, const char *writepath&#41;
&#123;
	int fdin;
	int fdout;
	char write_buffer&#91;1024&#93;;
	
	printf&#40;"Writing %s\n", writepath&#41;;

	fdin = sceIoOpen&#40;readpath, PSP_O_RDONLY, 0777&#41;;
	if&#40;fdin >= 0&#41;
	&#123;
		int bytesRead = 1;
		fdout = sceIoOpen&#40;writepath, PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777&#41;;
		if&#40;fdout < 0&#41;
		&#123;
			printf&#40;"Couldn't open %s\n", writepath&#41;;
		&#125;

		bytesRead = sceIoRead&#40;fdin, write_buffer, sizeof&#40;write_buffer&#41;&#41;;
		while&#40;&#40;bytesRead > 0&#41; && &#40;fdout >= 0&#41;&#41;
		&#123;
			sceIoWrite&#40;fdout, write_buffer, bytesRead&#41;;
			bytesRead = sceIoRead&#40;fdin, write_buffer, sizeof&#40;write_buffer&#41;&#41;;
		&#125;

		if&#40;fdout >= 0&#41;
		&#123;
			sceIoClose&#40;fdout&#41;;
		&#125;

		if&#40;fdin >= 0&#41;
		&#123;
			sceIoClose&#40;fdin&#41;;
		&#125;
	&#125;
	else
	&#123;
		printf&#40;"Couldn't open %s\n", readpath&#41;;
	&#125;
&#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;

int module_start&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	sceIoUnassign&#40;"flash0&#58;"&#41;;
	sceIoAssign&#40;"flash0&#58;", "lflash0&#58;0,0", "flashfat0&#58;", IOASSIGN_RDWR, NULL, 0&#41;;
	
	// copyFileKernel&#40;"ms0&#58;/test.txt", "flash0&#58;/test.txt"&#41;;

	return 0;
&#125;

int module_stop&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	return 0;
&#125; 

Code: Select all

# Define the exports for the prx
PSP_BEGIN_EXPORTS

# These four lines are mandatory &#40;although you can add other functions like module_stop&#41;
# syslib is a psynonym for the single mandatory export.
PSP_EXPORT_START&#40;syslib, 0, 0x8000&#41;
PSP_EXPORT_FUNC_HASH&#40;module_start&#41;
PSP_EXPORT_VAR_HASH&#40;module_info&#41;
PSP_EXPORT_END

PSP_EXPORT_START&#40;kernelExports, 0, 0x4001&#41;
PSP_EXPORT_FUNC_HASH&#40;copyFileKernel&#41;
PSP_EXPORT_FUNC_HASH&#40;execEboot&#41;
PSP_EXPORT_END

PSP_END_EXPORTS 
.::Albandu51::.
Posts: 12
Joined: Thu Oct 04, 2007 12:33 am

Post by .::Albandu51::. »

Hi,
Yes this is annoying, a flash how to get a file in the flash0 in kernel user? Thanks

PS: Sorry my English i am english
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

.::Albandu51::. wrote:PS: Sorry my English i am english
LOL
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

Cpasjuste, you are forgetting to put the code of your function exported to user between "int k1 = pspSdkSetK1(0)" and pspSdkSetK1(k1). Never forget to put those in a function you export for user mode, except in some cases of "inocent code" such as int func { return 1; }
The firmware is full of k1 shit checks.
Cpasjuste
Posts: 214
Joined: Sun May 29, 2005 8:28 am

Post by Cpasjuste »

Many thanks for the tips moonlight.

It's very strange because even with the setK1 function it doesn't work.

I launch psplink, i load my hombrew with psplink, but i cant write to the flash0 device with my user mode filer while it's possible in the brackground with psplink (after having reassigned the flash0 device in write mode with my kernel prx).

I am loosing my head :)
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

Try to use vsh mode instead of user mode, this is:

PSP_MODULE_INFO("blahblah", 0x800, 1, 0);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_VSH)

You could even reassign the flashes from it directly. M33-2 and 3 updaters do that, and they don't have any piece of kernel code.
SPITFIR3
Posts: 16
Joined: Sun Oct 21, 2007 3:25 am

Post by SPITFIR3 »

Hi moonlight, I'm trying the same thing for writing a file to flash0, but the files size always comes out 0 bytes.

FW = 3.71M33
Kernel = 3.71

If I change the write path to ms0 the file is written with the correct file size, i'm using vsh mode.

This is the code i'm using to write the file

Code: Select all

void flashTest&#40;const char *writepath&#41;
&#123;
	SceUID fd;
	
	sceIoUnassign&#40;"flash0&#58;"&#41;;
      
  sceIoAssign&#40;"flash0&#58;", "lflash0&#58;0,0", "flashfat0&#58;", IOASSIGN_RDWR, NULL, 0&#41;;
	
	fd = sceIoOpen&#40;writepath, PSP_O_WRONLY | PSP_O_CREAT, 0777&#41;;
	if&#40;fd < 0&#41;
	&#123;
		printf&#40;"\n  Cannot open file for writing.\n"&#41;;
		sceKernelExitGame&#40;&#41;;
	&#125;
	
	int flashfile = sceIoWrite&#40;fd, test_prx, size_test_prx&#41;;
	if&#40;flashfile != size_test_prx&#41;
	&#123;
		sceIoClose&#40;fd&#41;;
		printf&#40;"\n  Cannot write file.\n"&#41;;
		sceKernelExitGame&#40;&#41;;
	&#125;
	printf&#40;"  Writing file&#58; %s\n",writepath&#41;;
	sceKernelDelayThread&#40;300000&#41;;
	
	sceIoClose&#40;fd&#41;;
&#125; 
Thanks
hibbyware
Posts: 78
Joined: Wed Mar 28, 2007 10:29 am

Post by hibbyware »

@SPITFIR3

Have a look at my last post here...

http://forums.ps2dev.org/viewtopic.php?t=9648

The code will flash a test.txt but I use the same type of code to flash prx's aswell,
SPITFIR3
Posts: 16
Joined: Sun Oct 21, 2007 3:25 am

Post by SPITFIR3 »

hibbyware wrote:@SPITFIR3

Have a look at my last post here...

http://forums.ps2dev.org/viewtopic.php?t=9648

The code will flash a test.txt but I use the same type of code to flash prx's aswell,
I did try your code, but it seemed to have the same effect with the prx, it wrote the text file fine though.

I am using the prx in header format using the code I posted.

For the prx my write_buffer is this.

Code: Select all

char write_buffer&#91;1024&#93;;
and this installs fine to ms.

Instead of

Code: Select all

char write_buffer&#91;128*1024&#93;;
I'll try again with you code and see what happens.

Thanks
hibbyware
Posts: 78
Joined: Wed Mar 28, 2007 10:29 am

Post by hibbyware »

Over writing a prx in flash sometimes fails depending on the file permissions set for that file,

So you would have to change the file permission to allow it to over write it,

Other option which is not really a safe option is to delete the prx thats in flash before replacing it,
SPITFIR3
Posts: 16
Joined: Sun Oct 21, 2007 3:25 am

Post by SPITFIR3 »

Thanks hibbyware, didn't think about the file permissions, I'll give it another go with either deleting the file or changing it's permissions.

The prx i'm trying to write to flash0 don't exist there, it just seems whatever I try and that includes your direct code, it always results in a file size of 0 bytes, but the same code writing the same file to the memory stick works with the correct file size.

I just can't figure out why it's writing 0 bytes.

UPDATE:
The code below works on 3.52M33, give the correct bytes.

Code: Select all

void flashmyprx&#40;const char *writepath&#41;
&#123;
	SceUID fd;
	
	
	fd = sceIoOpen&#40;writepath, PSP_O_WRONLY | PSP_O_CREAT, 0777&#41;;
	if&#40;fd < 0&#41;
	&#123;
		printf&#40;"\n  Cannot open for writing.\n"&#41;;
		sceKernelDelayThread&#40;300000&#41;;
		sceKernelExitGame&#40;&#41;;
	&#125;
	
	int myprx = sceIoWrite&#40;fd, test_prx, size_test_prx&#41;;
	if&#40;myprx != size_test_prx&#41;
	&#123;
		sceIoClose&#40;fd&#41;;
		printf&#40;"\n  Cannot write file.\n"&#41;;
		sceKernelDelayThread&#40;300000&#41;;
		sceKernelExitGame&#40;&#41;;
	&#125;
	printf&#40;"  Writing file&#58; %s\n",writepath&#41;;
	sceKernelDelayThread&#40;300000&#41;;
	
	sceIoClose&#40;fd&#41;;
&#125;
So could it be different write permissions, if so how would I set these's for 3.71M33 or 3.80M33.

Thanks again.

UPDATE:
I have managed to get this to work now on 1 of my 3.80M33 psp's.

I've also found out why the files don't write. If the folder your writing to has it's hidden attribute set.

I've never had to set a files attribute before so don't know how to do it any help appreciated.

Thanks
SPITFIR3
Posts: 16
Joined: Sun Oct 21, 2007 3:25 am

Post by SPITFIR3 »

Sorted it out, I can now write, copy, remove files from flash0.
Post Reply