Beispiel #1
0
        public void ElfLoaderConstructorTest()
        {
            var injectContext = new InjectContext();

            injectContext.SetInstanceType <PspMemory, LazyPspMemory>();
            var memory          = injectContext.GetInstance <PspMemory>();
            var memoryStream    = new PspMemoryStream(memory);
            var memoryPartition = new MemoryPartition(injectContext, PspMemory.MainOffset,
                                                      PspMemory.MainOffset + PspMemory.MainSize);

            var elfLoader = new ElfLoader();

            elfLoader.Load(File.OpenRead("../../../TestInput/minifire.elf"), "minifire.elf");
            elfLoader.AllocateAndWrite(memoryStream, memoryPartition);
            Assert.Equal(1, elfLoader.ProgramHeaders.Length);
            Assert.Equal(3, elfLoader.SectionHeaders.Length);

            Assert.Equal(
                "['','.rodata.sceModuleInfo']".Replace('\'', '"'),
                elfLoader.SectionHeadersByName.Keys.ToJson()
                );

            //ElfLoader.LoadAllocateMemory(MemoryPartition);
            //ElfLoader.LoadWriteToMemory(MemoryStream);

            //var ModuleInfo = ElfLoader.ModuleInfo;

            var pc = elfLoader.Header.EntryPoint;

            //var GP = ModuleInfo.GP;

            Assert.Equal(0x08900008, (int)pc);
            //Assert.Equal(0x00004821, (int)GP);
        }
Beispiel #2
0
        public void ElfLoaderConstructorTest()
        {
            var PspConfig          = new PspConfig();
            var PspEmulatorContext = new PspEmulatorContext(PspConfig);

            PspEmulatorContext.SetInstanceType <PspMemory, LazyPspMemory>();
            var Memory          = PspEmulatorContext.GetInstance <PspMemory>();
            var MemoryStream    = new PspMemoryStream(Memory);
            var MemoryPartition = new MemoryPartition(PspMemory.MainOffset, PspMemory.MainOffset + PspMemory.MainSize);

            var ElfLoader = new ElfLoader();

            ElfLoader.Load(File.OpenRead("../../../TestInput/minifire.elf"));
            ElfLoader.AllocateAndWrite(MemoryStream, MemoryPartition);
            Assert.AreEqual(1, ElfLoader.ProgramHeaders.Length);
            Assert.AreEqual(3, ElfLoader.SectionHeaders.Length);

            Assert.AreEqual(
                "['','.rodata.sceModuleInfo']".Replace('\'', '"'),
                ElfLoader.SectionHeadersByName.Keys.ToJson()
                );

            //ElfLoader.LoadAllocateMemory(MemoryPartition);
            //ElfLoader.LoadWriteToMemory(MemoryStream);

            //var ModuleInfo = ElfLoader.ModuleInfo;

            var PC = ElfLoader.Header.EntryPoint;

            //var GP = ModuleInfo.GP;

            Assert.AreEqual(0x08900008, (int)PC);
            //Assert.AreEqual(0x00004821, (int)GP);
        }
Beispiel #3
0
        public MemoryPartition AllocateItem(string Value)
        {
            var Partition       = Allocate(Value.Length + 1);
            var PartitionStream = new PspMemoryStream(PspMemory).SliceWithBounds(Partition.Low, Partition.High);

            PartitionStream.WriteStringz(Value);
            return(Partition);
        }
Beispiel #4
0
        public MemoryPartition AllocateItem <T>(T Value) where T : struct
        {
            var Partition       = Allocate(Marshal.SizeOf(typeof(T)));
            var PartitionStream = new PspMemoryStream(PspMemory).SliceWithBounds(Partition.Low, Partition.High);

            PartitionStream.WriteStruct(Value);
            return(Partition);
        }
Beispiel #5
0
        public void _LoadFile(string fileName)
        {
            //GC.Collect();
            SetVirtualFolder(Path.GetDirectoryName(fileName));

            var memoryStream = new PspMemoryStream(PspMemory);

            var arguments = new[]
            {
                "ms0:/PSP/GAME/virtual/EBOOT.PBP",
            };

            Stream loadStream = File.OpenRead(fileName);
            //using ()
            {
                var elfLoadStreamTry = new List <Stream>();
                //Stream ElfLoadStream = null;

                var    format = new FormatDetector().DetectSubType(loadStream);
                string title  = null;
                switch (format)
                {
                case FormatDetector.SubType.Pbp:
                {
                    var pbp = new Pbp().Load(loadStream);
                    elfLoadStreamTry.Add(pbp[Pbp.Types.PspData]);
                    Logger.TryCatch(() =>
                        {
                            var paramSfo = new Psf().Load(pbp[Pbp.Types.ParamSfo]);

                            if (paramSfo.EntryDictionary.ContainsKey("TITLE"))
                            {
                                title = (string)paramSfo.EntryDictionary["TITLE"];
                            }

                            if (paramSfo.EntryDictionary.ContainsKey("PSP_SYSTEM_VER"))
                            {
                                HleConfig.FirmwareVersion = paramSfo.EntryDictionary["PSP_SYSTEM_VER"].ToString();
                            }
                        });
                }
                break;

                case FormatDetector.SubType.Elf:
                    elfLoadStreamTry.Add(loadStream);
                    break;

                case FormatDetector.SubType.Dax:
                case FormatDetector.SubType.Cso:
                case FormatDetector.SubType.Iso:
                {
                    arguments[0] = "disc0:/PSP/GAME/SYSDIR/EBOOT.BIN";

                    var iso = SetIso(fileName);
                    Logger.TryCatch(() =>
                        {
                            var paramSfo = new Psf().Load(iso.Root.Locate("/PSP_GAME/PARAM.SFO").Open());
                            title        = (string)paramSfo.EntryDictionary["TITLE"];
                        });

                    var filesToTry = new[]
                    {
                        "/PSP_GAME/SYSDIR/BOOT.BIN",
                        "/PSP_GAME/SYSDIR/EBOOT.BIN",
                        "/PSP_GAME/SYSDIR/EBOOT.OLD",
                    };

                    foreach (var fileToTry in filesToTry)
                    {
                        try
                        {
                            elfLoadStreamTry.Add(iso.Root.Locate(fileToTry).Open());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                        //if (ElfLoadStream.Length != 0) break;
                    }

                    /*
                     * if (ElfLoadStream.Length == 0)
                     * {
                     *  throw (new Exception(String.Format("{0} files are empty", String.Join(", ", FilesToTry))));
                     * }
                     */
                }
                break;

                default:
                    throw (new NotImplementedException("Can't load format '" + format + "'"));
                }

                Exception      loadException  = null;
                HleModuleGuest hleModuleGuest = null;

                foreach (var elfLoadStream in elfLoadStreamTry)
                {
                    try
                    {
                        loadException = null;

                        if (elfLoadStream.Length < 256)
                        {
                            throw(new InvalidProgramException("File too short"));
                        }

                        hleModuleGuest = Loader.LoadModule(
                            elfLoadStream,
                            memoryStream,
                            MemoryManager.GetPartition(MemoryPartitions.User),
                            ModuleManager,
                            title,
                            moduleName: fileName,
                            isMainModule: true
                            );

                        loadException = null;

                        break;
                    }
                    catch (InvalidProgramException e)
                    {
                        loadException = e;
                    }
                }

                if (loadException != null)
                {
                    throw loadException;
                }

                RegisterSyscalls();

                const uint startArgumentAddress = 0x08000100;
                var        endArgumentAddress   = startArgumentAddress;

                var argumentsChunk = arguments
                                     .Select(argument => Encoding.UTF8.GetBytes(argument + "\0"))
                                     .Aggregate(new byte[] { }, (accumulate, chunk) => accumulate.Concat(chunk))
                ;

                var reservedSyscallsPartition = MemoryManager.GetPartition(MemoryPartitions.Kernel0).Allocate(
                    0x100,
                    Name: "ReservedSyscallsPartition"
                    );
                var argumentsPartition = MemoryManager.GetPartition(MemoryPartitions.Kernel0).Allocate(
                    argumentsChunk.Length,
                    Name: "ArgumentsPartition"
                    );
                PspMemory.WriteBytes(argumentsPartition.Low, argumentsChunk);

                Debug.Assert(ThreadManForUser != null);

                // @TODO: Use Module Manager

                //var MainThread = ThreadManager.Create();
                //var CpuThreadState = MainThread.CpuThreadState;
                var currentCpuThreadState = new CpuThreadState(CpuProcessor);
                {
                    if (hleModuleGuest == null)
                    {
                        //throw new InvalidOperationException("hleModuleGuest == null");
                    }
                    //CpuThreadState.PC = Loader.InitInfo.PC;
                    currentCpuThreadState.Gp           = hleModuleGuest.InitInfo.Gp;
                    currentCpuThreadState.CallerModule = hleModuleGuest;

                    var threadId = (int)ThreadManForUser.sceKernelCreateThread(currentCpuThreadState, "<EntryPoint>",
                                                                               hleModuleGuest.InitInfo.Pc, 10, 0x1000, PspThreadAttributes.ClearStack, null);

                    //var Thread = HleThreadManager.GetThreadById(ThreadId);
                    ThreadManForUser._sceKernelStartThread(currentCpuThreadState, threadId, argumentsPartition.Size,
                                                           argumentsPartition.Low);
                    //Console.WriteLine("RA: 0x{0:X}", CurrentCpuThreadState.RA);
                }
                currentCpuThreadState.DumpRegisters();
                MemoryManager.GetPartition(MemoryPartitions.User).Dump();
                //ModuleManager.LoadedGuestModules.Add(HleModuleGuest);

                //MainThread.CurrentStatus = HleThread.Status.Ready;
            }
        }
Beispiel #6
0
        public void _LoadFile(String FileName)
        {
            //GC.Collect();

            SetVirtualFolder(Path.GetDirectoryName(FileName));

            var MemoryStream = new PspMemoryStream(PspMemory);

            var    Loader     = PspEmulatorContext.GetInstance <ElfPspLoader>();
            Stream LoadStream = File.OpenRead(FileName);
            //using ()
            {
                Stream ElfLoadStream = null;

                var Format = new FormatDetector().Detect(LoadStream);
                switch (Format)
                {
                case "Pbp":
                    ElfLoadStream = new Pbp().Load(LoadStream)["psp.data"];
                    break;

                case "Elf":
                    ElfLoadStream = LoadStream;
                    break;

                case "Cso":
                case "Iso":
                {
                    var Iso = SetIso(FileName);
                    ElfLoadStream = Iso.Root.Locate("/PSP_GAME/SYSDIR/BOOT.BIN").Open();
                }
                break;

                default:
                    throw (new NotImplementedException("Can't load format '" + Format + "'"));
                }

                Loader.Load(
                    ElfLoadStream,
                    MemoryStream,
                    HleState.MemoryManager.GetPartition(HleMemoryManager.Partitions.User),
                    HleState.ModuleManager
                    );

                RegisterSyscalls();

                uint CODE_PTR_ARGUMENTS = 0x08000100;

                {
                    var BinaryWriter = new BinaryWriter(MemoryStream);
                    var StreamWriter = new StreamWriter(MemoryStream); StreamWriter.AutoFlush = true;
                    MemoryStream.Position = CODE_PTR_ARGUMENTS;

                    BinaryWriter.Write((uint)(CODE_PTR_ARGUMENTS + 4)); BinaryWriter.Flush();
                    StreamWriter.Write("ms0:/PSP/GAME/virtual/EBOOT.PBP\0"); StreamWriter.Flush();
                }

                uint argc = 1;
                uint argv = CODE_PTR_ARGUMENTS + 4;
                //uint argv = CODE_PTR_ARGUMENTS;

                var MainThread     = HleState.ThreadManager.Create();
                var CpuThreadState = MainThread.CpuThreadState;
                {
                    CpuThreadState.PC     = Loader.InitInfo.PC;
                    CpuThreadState.GP     = Loader.InitInfo.GP;
                    CpuThreadState.SP     = HleState.MemoryManager.GetPartition(HleMemoryManager.Partitions.User).Allocate(0x1000, MemoryPartition.Anchor.High, Alignment: 0x100).High;
                    CpuThreadState.K0     = MainThread.CpuThreadState.SP;
                    CpuThreadState.RA     = HleEmulatorSpecialAddresses.CODE_PTR_EXIT_THREAD;
                    CpuThreadState.GPR[4] = (int)argc;                     // A0
                    CpuThreadState.GPR[5] = (int)argv;                     // A1
                }
                CpuThreadState.DumpRegisters();
                HleState.MemoryManager.GetPartition(HleMemoryManager.Partitions.User).Dump();

                MainThread.CurrentStatus = HleThread.Status.Ready;
            }
        }