All pastes #1051696 Raw Edit

PSP Flasher

public c v1 · immutable
#1051696 ·published 2008-06-20 03:53 UTC
rendered paste body
//Written and developed only by Neo_The_User at the moment//DO NOT TEST NOR USE ANY OF THIS CODE//ALL UNTESTED! USE AT YOUR OWN RISK!//psp-config command not found when compiling under GCC.//Do not modify the #include order or names or the PSP will not read it.//Unknown reason why but it just works and thats all the matters at this time as far as #includes go//flash0 is what the PSP calls the Flashrom (NAND)//Makefile does not contain a specific directory.//Directory issues are in last line (of Makefile). I do not know where it is supposed to be. /lib/build.mak probably? WILD GUESS!//Save this file as main.c#include <pspkernel.h>#include <pspdebug.h>#include <malloc.h>#include <stdio.h>#include <string.h>#include <pspctrl.h>#define printf pspDebugScreenPrintfPSP_MODULE_INFO("Version Flasher",0x1000,1,7);static char version[8];int main_menu(int);void draw_menu(int);int file_copy(char[128],char[128]);int flash(char[8]);int isvalidtxt(char[128]);int isvaliddat(char[128]);//Exit callbacks and XMB preperation for application exit. -Neo_The_User//As seen at the bottom of this code. -Neo_The_Userint exit_callback(int arg1,int arg2,void *common){	sceKernelExitGame();	return 0;}int CallbackThread(SceSize args,void *argp){	int cbid;	cbid=sceKernelCreateCallback("Exit Callback",exit_callback,NULL);	sceKernelRegisterExitCallback(cbid);	sceKernelSleepThreadCB();	return 0;}int SetupCallbacks(void){	int thid=0;	thid=sceKernelCreateThread("update_thread",CallbackThread,0x11,0xFA0,0,0);	if(thid>=0) sceKernelStartThread(thid,0,0);	return thid;}void draw_menu(int curopt){	int i;	//Not Pandora serial. Similar to the PSP's service mode's serial -Neo_The_User		u32 white=0x00FFFFFF;	u32 black=0;	char msgs[5][64];		pspDebugScreenClear();		sprintf(msgs[0],"1. Backup current version files");	sprintf(msgs[1],"2. Restore version from backup");	sprintf(msgs[2],"3. Flash version to 3.50");	sprintf(msgs[3],"4. Flash version to 3.52");	sprintf(msgs[4],"5. Exit Program");		//1.7 is version of the flasher. Not the firmware or anything to do with the PSP. -Neo_The_User	pspDebugScreenClear();	printf("###Version Flasher 1.7 by download###\n");	// % represents varible. Do not use this application as a reliable source to detect your firmware. -Neo_The_User	// % represents firmware version. Can detect firmware from 1.50 to 3.90 but will only work on 3.50 and 3.52. -Neo_The_User	//Goal is to make it for non M33 3.80 and up. -Neo_The_User	printf("Your current firmware version is %s\n",version);		for(i=0;i<5;i++)	{		if(curopt-1==i)		{			//Default debugging screen. -Neo_The_User			pspDebugScreenSetTextColor(black);			pspDebugScreenSetBackColor(white);			printf("%s",msgs[i]);			pspDebugScreenSetTextColor(white);			pspDebugScreenSetBackColor(black);			printf("\n");		}		else printf("%s\n",msgs[i]);	}}int main_menu(int curopt){	int opt=curopt;		char *buffer;	int size;	int f=sceIoOpen("flash0:/vsh/etc/version.txt",PSP_O_RDONLY,0777);	size=sceIoLseek(f,0,SEEK_END);	sceIoLseek(f,0,SEEK_SET);	buffer=(char*)malloc(size+16);	sceIoRead(f,buffer,size);	sprintf(version,"%c.%c%c",buffer[80],buffer[82],buffer[84]);	sceIoClose(f);		draw_menu(opt);		SceCtrlData pad;	sceCtrlSetSamplingCycle(0);	sceCtrlSetSamplingMode(PSP_CTRL_MODE_DIGITAL);		while(1)	{		sceCtrlReadBufferPositive(&pad,1);		sceKernelDelayThread(1500);				if(pad.Buttons&PSP_CTRL_DOWN)		{			if(opt<5) opt++;			draw_menu(opt);			sceKernelDelayThread(200000);		}		else if(pad.Buttons&PSP_CTRL_UP)		{			if(opt>1) opt--;			draw_menu(opt);			sceKernelDelayThread(200000);		}		else if(pad.Buttons&PSP_CTRL_CROSS)		{			return opt;		}	}}int file_copy(char srcf[128],char dstf[128]){	SceUID src,dst;	char *buffer;	int size=0;		//Directory of flash0 /vsh/etc (cannot pinpoint exact location.) -Neo_The_User		//Unable to access flash0 possible error code = 80020130 or 8001B002 if anything. -Neo_The_User	//make flash0 writable -Neo_The_User	if(sceIoUnassign("flash0:")<0)	{		//First Attempt. If failed I made it try again.		printf("Unable to access flash0\n");		return -1;	}	if(sceIoAssign("flash0:","lflash0:0,0","flashfat0:",0,IOASSIGN_RDWR,0)<0)	{		//Second Attempt. If failed then further attempts to access flash0 will fail as well.		//Don't bother running another check. -Neo_The_User		printf("Unable to access flash0\n");		return -1;	}	if((src=sceIoOpen(srcf,PSP_O_RDONLY,0777))<0) return -1;	size=sceIoLseek(src,0,SEEK_END);	sceIoLseek(src,0,SEEK_SET);	buffer=(char*)malloc(size+16);	if(sceIoRead(src,buffer,size)<size) return -1;	if((dst=sceIoOpen(dstf,PSP_O_WRONLY|PSP_O_CREAT|PSP_O_TRUNC,0777))<0) return -1;	if(sceIoWrite(dst,buffer,size)!=size) return -1;	free(buffer);	sceIoClose(src);	sceIoClose(dst);	return 0;}int flash(char ver[8]){	char srcd[32];	char srct[32];		memset(srcd,0,32);	sprintf(srcd,"./%s/index.dat",ver);	memset(srct,0,32);	sprintf(srct,"./%s/version.txt",ver);	if(!isvaliddat(srcd))	{		//Error signs / warnings below to keep user aware. -Neo_The_User		//Use these signs to your advantage to figure where the flasher breaks. -Neo_The_User		//Please report to me the exact spots where it hangs and crashes. Thank you. -Neo_The_User		printf("Invalid index.dat file in %s\n",srcd);		sceKernelDelayThread(500000);		return -1;	}	if(!isvalidtxt(srct))	{		//If you get this error then your PSP Firmware does not support this application. -Neo_The_User		//Don't ask. This is what this application is for. downgrade PSPs without hassle -Neo_The_User		//And to change, upgrade, and downgrade firmware to any version the user wants. -Neo_The_User		//Of coarse that will only be achieved when program is finished and successful. -Neo_The_User		printf("Invalid version.txt file in %s\n",srct);		sceKernelDelayThread(500000);		return -1;	}	printf("Copying ./%s/index.dat to flash0:/vsh/etc/index.dat\n",ver);	if(file_copy(srcd,"flash0:/vsh/etc/index.dat")<0)	{		//If you get this error then this program was written wrong. -Neo_The_User		//Give this application full read and write access to the flash memory. -Neo_The_User		printf("Error: Couldn't copy file\n");		return -1;	}		//Again. Full read and write access. -Neo_The_User	//Requires more code to be written. -Neo_The_User	printf("Copying ./%s/version.txt to flash0:/vsh/etc/version.txt\n",ver);	if(file_copy(srct,"flash0:/vsh/etc/version.txt")<0)	{		printf("Error: Couldn't copy file\n");		return -1;	}	printf("Finished flashing files...");	sceKernelDelayThread(500000);}int isvalidtxt(char file[128]){	FILE *f;	char *buffer;	int size;	char cmp[16]="release";	int ret;		f=fopen(file,"rb");	fseek(f,0,SEEK_END);	size=ftell(f);	rewind(f);	buffer=(char*)malloc(size+16);	fread(buffer,1,size,f);	fclose(f);		if(memcmp(buffer,cmp,7)==0) ret=1;	else ret=0;		free(buffer);	return ret;}int isvaliddat(char file[128]){	FILE *f;	char *buffer;	int size;	char cmp[16]="PSPsysGP";	int ret;		f=fopen(file,"rb");	fseek(f,0,SEEK_END);	size=ftell(f);	rewind(f);	buffer=(char*)malloc(size+16);	fread(buffer,1,size,f);	fclose(f);		if(memcmp(buffer,cmp,8)==0) ret=1;	else ret=0;		free(buffer);	return ret;}int main(){	SetupCallbacks();	pspDebugScreenInit();	int sel=0;		while(sel!=5)	{		sel=main_menu(1);		if(sel==1)		{			//Error warning below. -Neo_The_User			//I did the best I could to get the program to do as much as possible. -Neo_The_User			//Even if not full read and write access. -Neo_The_User			printf("Copying flash0:/vsh/etc/index.dat to ./bak/index.dat\n");			if((file_copy("flash0:/vsh/etc/index.dat","./bak/index.dat"))<0) printf("Error: Couldn't copy file\n");			else			{				printf("Copying flash0:/vsh/etc/version.txt to ./bak/version.txt\n");				if((file_copy("flash0:/vsh/etc/version.txt","./bak/version.txt"))<0) printf("Error: Couldn't copy file\n");				else printf("Finished backing up files...\n");			}			sceKernelDelayThread(500000);		}		if(sel==2)		{			if(!isvaliddat("./bak/index.dat")||!isvalidtxt("./bak/version.txt")) printf("Invalid backups to restore from\n");			else			{				printf("Copying ./bak/index.dat to flash0:/vsh/etc/index.dat\n");				if((file_copy("./bak/index.dat","flash0:/vsh/etc/index.dat"))<0) printf("Error: Couldn't copy file\n");				else				{					printf("Copying ./bak/version.txt to flash0:/vsh/etc/version.txt\n");					if((file_copy("./bak/version.txt","flash0:/vsh/etc/version.txt"))<0) printf("Error: Couldn't copy file\n");					else printf("Finished restoring files...\n");				}			}			sceKernelDelayThread(500000);		}		if(sel==3)		{			if((flash("350"))<0) printf("Error: Couldn't copy file\n");		}		if(sel==4)		{			if((flash("352"))<0) printf("Error: Couldn't copy file\n");		}	}		printf("\n\nExiting...\n");	sceKernelDelayThread(300000);		sceKernelExitGame();	return 0;}