private void CallStopModule(KModule module, int args, int argp) { if (module.ModuleStop == 0) { return; } // Create a thread KThread thread = new KThread(_kernel, module, _kernel.Partitions[2], "module_stop_thread", module.ModuleStop, 0, KThreadAttributes.User, 0x4000); _kernel.AddHandle(thread); thread.Start(( uint )args, ( uint )argp); // Setup handler so that we get the callback when the thread ends and we can kill it _kernel.Cpu.SetContextSafetyCallback(thread.ContextID, new ContextSafetyDelegate(this.KmoduleStopThreadEnd), ( int )thread.UID); Log.WriteLine(Verbosity.Verbose, Feature.Bios, "ModuleMgrForUser: starting module_stop thread with UID {0:X} for module {1}", thread.UID, module.Name); // Schedule so that our thread runs _kernel.Schedule(); }
// SDK location: /user/pspmodulemgr.h:141 // SDK declaration: int sceKernelUnloadModule(SceUID modid); public int sceKernelUnloadModule(int modid) { if (modid == FakeModuleUID) { return(0); } KModule module = _kernel.GetHandle <KModule>(modid); Debug.Assert(module != null); if (module == null) { return(-1); } // How do we unload? ruh roh return(0); }
// SDK location: /user/pspmodulemgr.h:132 // SDK declaration: int sceKernelStopModule(SceUID modid, SceSize argsize, void *argp, int *status, SceKernelSMOption *option); public int sceKernelStopModule(int modid, int argsize, int argp, int status, int option) { // argsize & argp are passed to module_stop // status is the return from module_stop // option is unused right now if (modid == FakeModuleUID) { if (status != 0) { unsafe { uint *pstatus = ( uint * )_memorySystem.Translate(( uint )status); * pstatus = 0; } } return(0); } KModule module = _kernel.GetHandle <KModule>(modid); Debug.Assert(module != null); if (module == null) { return(-1); } // Don't current support getting result - just set to 0? Debug.Assert(status == 0); if (status != 0) { unsafe { uint *pstatus = ( uint * )_memorySystem.Translate(( uint )status); * pstatus = 0; } } this.CallStopModule(_kernel.MainModule, argsize, argp); return(0); }
// manual add public int sceKernelGetModuleIdByAddress(int address) { // This is nasty - need to have a list foreach (KHandle handle in _kernel.Handles.Values) { if (handle is KModule) { KModule module = ( KModule )handle; if (module.LoadResults != null) { if ((address >= module.LoadResults.LowerBounds) && (address < module.LoadResults.UpperBounds)) { return(( int )module.UID); } } } } return(-1); }
// SDK location: /user/pspthreadman.h:169 // SDK declaration: SceUID sceKernelCreateThread(const char *name, SceKernelThreadEntry entry, int initPriority, int stackSize, SceUInt attr, SceKernelThreadOptParam *option); public int sceKernelCreateThread(int name, int entry, int initPriority, int stackSize, int attr, int option) { KModule module = _kernel.ActiveThread.Module; KThread thread = new KThread(_kernel, module, _kernel.Partitions[2], _kernel.ReadString(( uint )name), ( uint )entry, initPriority, ( KThreadAttributes )attr, ( uint )stackSize); _kernel.AddHandle(thread); // Option unused? Debug.Assert(option == 0); Log.WriteLine(Verbosity.Normal, Feature.Bios, "sceKernelCreateThread: created thread {0:X} {1} with entry {2:X8}", thread.UID, thread.Name, thread.EntryAddress); return(( int )thread.UID); }
private int LoadModule( IMediaFile file, Stream stream, int flags, int option ) { // Load! LoadParameters loadParams = new LoadParameters(); if( file != null ) { loadParams.Path = file.Parent; loadParams.FilePath = file.AbsolutePath; string fileName = file.Name.ToLowerInvariant(); foreach( string blacklisted in _moduleBlacklist ) { if( fileName == blacklisted ) { // Module is blacklisted - ignore Log.WriteLine( Verbosity.Normal, Feature.Bios, "LoadModule: module is blacklisted, ignoring" ); return FakeModuleUID; } } } #if DEBUG loadParams.AppendDatabase = true; #endif // Check to see if it's one we have a decoded version of string kernelLocation = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ); string prxLocation = Path.Combine( kernelLocation, "PRX" ); string lookasidePrx = Path.Combine( prxLocation, file.Name ); if( File.Exists( lookasidePrx ) == true ) { // Load ours instead Log.WriteLine( Verbosity.Normal, Feature.Bios, "LoadModule: lookaside prx found at {0}", lookasidePrx ); stream = File.OpenRead( lookasidePrx ); } Debug.Assert( stream != null ); if( stream == null ) { Log.WriteLine( Verbosity.Critical, Feature.Bios, "LoadModule: unable to load module {0}", file.Name ); return -1; } // Quick check to make sure it isn't encrypted before sending off to loader byte[] magicBytes = new byte[ 4 ]; stream.Read( magicBytes, 0, 4 ); stream.Seek( -4, SeekOrigin.Current ); // 0x7E, 0x50, 0x53, 0x50 = ~PSP bool encrypted = ( ( magicBytes[ 0 ] == 0x7E ) && ( magicBytes[ 1 ] == 0x50 ) && ( magicBytes[ 2 ] == 0x53 ) && ( magicBytes[ 3 ] == 0x50 ) ); //Debug::Assert( encrypted == false ); if( encrypted == true ) { Log.WriteLine( Verbosity.Critical, Feature.Bios, "LoadModule: module {0} is encrypted - unable to load", file.Name ); // We spoof the caller in to thinking we worked right... by just returning 0 ^_^ KModule fakemod = new KModule( _kernel, null ); fakemod.LoadParameters = loadParams; _kernel.AddHandle( fakemod ); if( Diag.IsAttached == true ) Diag.Instance.Client.OnModuleLoaded(); return ( int )fakemod.UID; } LoadResults results = _kernel.Bios._loader.LoadModule( ModuleType.Prx, stream, loadParams ); //Debug.Assert( results.Successful == true ); if( results.Successful == false ) { Log.WriteLine( Verbosity.Critical, Feature.Bios, "LoadModule: loader failed" ); return FakeModuleUID; } if( results.Ignored == true ) { // Faked Log.WriteLine( Verbosity.Normal, Feature.Bios, "LoadModule: ignoring module" ); return FakeModuleUID; } // Create a local representation of the module BiosModule module = new BiosModule( results.Name, results.Exports.ToArray() ); _kernel.Bios._metaModules.Add( module ); _kernel.Bios._metaModuleLookup.Add( module.Name, module ); KModule kmod = new KModule( _kernel, module ); kmod.UID = results.ModuleID; kmod.LoadParameters = loadParams; kmod.LoadResults = results; _kernel.UserModules.Add( kmod ); _kernel.AddHandle( kmod ); if( Diag.IsAttached == true ) Diag.Instance.Client.OnModuleLoaded(); return ( int )kmod.UID; }
private void CallStopModule( KModule module, int args, int argp ) { if( module.ModuleStop == 0 ) return; // Create a thread KThread thread = new KThread( _kernel, module, _kernel.Partitions[ 2 ], "module_stop_thread", module.ModuleStop, 0, KThreadAttributes.User, 0x4000 ); _kernel.AddHandle( thread ); thread.Start( ( uint )args, ( uint )argp ); // Setup handler so that we get the callback when the thread ends and we can kill it _kernel.Cpu.SetContextSafetyCallback( thread.ContextID, new ContextSafetyDelegate( this.KmoduleStopThreadEnd ), ( int )thread.UID ); Log.WriteLine( Verbosity.Verbose, Feature.Bios, "ModuleMgrForUser: starting module_stop thread with UID {0:X} for module {1}", thread.UID, module.Name ); // Schedule so that our thread runs _kernel.Schedule(); }
static int Main(string[] args) { var xmlDoc = new XmlDocument(); string outFilename = null; try { if (args.Length == 0) { xmlDoc.Load(Console.In); } else { for (int i = 0; i < args.Length; i++) { if (args[i] == "-in") { if (i == args.Length - 1) { Console.Write("Provide a filename for the -in argument."); return(1); } xmlDoc.Load(args[i + 1]); i++; // skip the next argument } else if (args[i] == "-out") { if (i == args.Length - 1) { Console.Write("Provide a filename for the -out argument."); return(1); } outFilename = args[i + 1]; i++; // skip the next argument } else { Console.Write("Invalid argument detected: " + args[i]); return(1); } } } } catch (Exception ex) { Console.WriteLine("Could not load XML file:"); Console.WriteLine(ex.Message); return(1); } var presetNode = xmlDoc.SelectSingleNode("preset"); var submodulesNode = presetNode.SelectSingleNode("submodules"); var kclip = new KClip(); try { kclip.ClipModules = KModule.SubmodulesNodeToKModuleList(submodulesNode); } catch (KmlParseException ex) { Console.WriteLine("Error when parsing KML:"); Console.WriteLine(ex.Message); return(1); } catch (Exception ex) { Console.WriteLine("Unhandled exception:"); Console.WriteLine(ex.ToString()); return(1); } var serializerSettings = new JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.Indented, NullValueHandling = NullValueHandling.Ignore, ContractResolver = new DefaultContractResolver() { NamingStrategy = new SnakeCaseNamingStrategy() } }; var serialized = JsonConvert.SerializeObject(kclip, serializerSettings); var result = new StringBuilder() .AppendLine("##KUSTOMCLIP##") .AppendLine(serialized) .AppendLine("##KUSTOMCLIP##") .ToString(); if (outFilename != null) { File.WriteAllText(outFilename, result); } else { Console.Write(result); } return(0); }
private int LoadModule(IMediaFile file, Stream stream, int flags, int option) { // Load! LoadParameters loadParams = new LoadParameters(); if (file != null) { loadParams.Path = file.Parent; loadParams.FilePath = file.AbsolutePath; string fileName = file.Name.ToLowerInvariant(); foreach (string blacklisted in _moduleBlacklist) { if (fileName == blacklisted) { // Module is blacklisted - ignore Log.WriteLine(Verbosity.Normal, Feature.Bios, "LoadModule: module is blacklisted, ignoring"); return(FakeModuleUID); } } } #if DEBUG loadParams.AppendDatabase = true; #endif // Check to see if it's one we have a decoded version of string kernelLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string prxLocation = Path.Combine(kernelLocation, "PRX"); string lookasidePrx = Path.Combine(prxLocation, file.Name); if (File.Exists(lookasidePrx) == true) { // Load ours instead Log.WriteLine(Verbosity.Normal, Feature.Bios, "LoadModule: lookaside prx found at {0}", lookasidePrx); stream = File.OpenRead(lookasidePrx); } Debug.Assert(stream != null); if (stream == null) { Log.WriteLine(Verbosity.Critical, Feature.Bios, "LoadModule: unable to load module {0}", file.Name); return(-1); } // Quick check to make sure it isn't encrypted before sending off to loader byte[] magicBytes = new byte[4]; stream.Read(magicBytes, 0, 4); stream.Seek(-4, SeekOrigin.Current); // 0x7E, 0x50, 0x53, 0x50 = ~PSP bool encrypted = ( (magicBytes[0] == 0x7E) && (magicBytes[1] == 0x50) && (magicBytes[2] == 0x53) && (magicBytes[3] == 0x50)); //Debug::Assert( encrypted == false ); if (encrypted == true) { Log.WriteLine(Verbosity.Critical, Feature.Bios, "LoadModule: module {0} is encrypted - unable to load", file.Name); // We spoof the caller in to thinking we worked right... by just returning 0 ^_^ KModule fakemod = new KModule(_kernel, null); fakemod.LoadParameters = loadParams; _kernel.AddHandle(fakemod); if (Diag.IsAttached == true) { Diag.Instance.Client.OnModuleLoaded(); } return(( int )fakemod.UID); } LoadResults results = _kernel.Bios._loader.LoadModule(ModuleType.Prx, stream, loadParams); //Debug.Assert( results.Successful == true ); if (results.Successful == false) { Log.WriteLine(Verbosity.Critical, Feature.Bios, "LoadModule: loader failed"); return(FakeModuleUID); } if (results.Ignored == true) { // Faked Log.WriteLine(Verbosity.Normal, Feature.Bios, "LoadModule: ignoring module"); return(FakeModuleUID); } // Create a local representation of the module BiosModule module = new BiosModule(results.Name, results.Exports.ToArray()); _kernel.Bios._metaModules.Add(module); _kernel.Bios._metaModuleLookup.Add(module.Name, module); KModule kmod = new KModule(_kernel, module); kmod.UID = results.ModuleID; kmod.LoadParameters = loadParams; kmod.LoadResults = results; _kernel.UserModules.Add(kmod); _kernel.AddHandle(kmod); if (Diag.IsAttached == true) { Diag.Instance.Client.OnModuleLoaded(); } return(( int )kmod.UID); }