Ejemplo n.º 1
0
 public override int ioClose()
 {
     if (ioctl != null)
     {
         ioctl.ioClose();
     }
     return(base.ioClose());
 }
Ejemplo n.º 2
0
 public virtual void closeVirtualFile()
 {
     if (vFileOpen)
     {
         if (vFile != null)
         {
             vFile.ioClose();
             vFile = null;
         }
         vFileOpen = false;
     }
 }
Ejemplo n.º 3
0
        private IVirtualFile ioOpenPatchedFile(string fileName, int flags, int mode, IList <PatchInfo> patches)
        {
            IVirtualFile vFile = base.ioOpen(fileName, flags, mode);

            if (vFile == null)
            {
                return(null);
            }

            sbyte[] buffer = Utilities.readCompleteFile(vFile);
            vFile.ioClose();
            if (buffer == null)
            {
                return(null);
            }

            foreach (PatchInfo patch in patches)
            {
                patch.apply(buffer);
            }

            return(new ByteArrayVirtualFile(buffer));
        }
Ejemplo n.º 4
0
 public virtual int ioClose(IVirtualFile file)
 {
     return(file.ioClose());
 }
Ejemplo n.º 5
0
        public virtual int hleKernelLoadExec(PspString filename, int argSize, int argAddr)
        {
            string name = filename.String;

            // The PSP is replacing a loadexec of disc0:/PSP_GAME/SYSDIR/BOOT.BIN with EBOOT.BIN
            if (name.Equals(unencryptedBootPath))
            {
                Console.WriteLine(string.Format("sceKernelLoadExec '{0}' replaced by '{1}'", name, encryptedBootPath));
                name = encryptedBootPath;
            }

            ByteBuffer moduleBuffer = null;

            IVirtualFile vFile = Modules.IoFileMgrForUserModule.getVirtualFile(name, IoFileMgrForUser.PSP_O_RDONLY, 0);
            UmdIsoReader iso   = null;

            if (vFile is XmbIsoVirtualFile)
            {
                try
                {
                    IVirtualFile vFileLoadExec = ((XmbIsoVirtualFile)vFile).ioReadForLoadExec();
                    if (vFileLoadExec != null)
                    {
                        iso = ((XmbIsoVirtualFile)vFile).IsoReader;

                        vFile.ioClose();
                        vFile = vFileLoadExec;
                    }
                }
                catch (IOException e)
                {
                    Console.WriteLine("hleKernelLoadExec", e);
                }
            }

            if (vFile != null)
            {
                sbyte[] moduleBytes = Utilities.readCompleteFile(vFile);
                vFile.ioClose();
                if (moduleBytes != null)
                {
                    moduleBuffer = ByteBuffer.wrap(moduleBytes);
                }
            }
            else
            {
                SeekableDataInput moduleInput = Modules.IoFileMgrForUserModule.getFile(name, IoFileMgrForUser.PSP_O_RDONLY);
                if (moduleInput != null)
                {
                    try
                    {
                        sbyte[] moduleBytes = new sbyte[(int)moduleInput.Length()];
                        moduleInput.readFully(moduleBytes);
                        moduleInput.Dispose();
                        moduleBuffer = ByteBuffer.wrap(moduleBytes);
                    }
                    catch (IOException e)
                    {
                        Console.WriteLine(string.Format("sceKernelLoadExec - Error while loading module '{0}'", name), e);
                        return(ERROR_KERNEL_PROHIBIT_LOADEXEC_DEVICE);
                    }
                }
            }

            return(hleKernelLoadExec(moduleBuffer, argSize, argAddr, name, iso));
        }
Ejemplo n.º 6
0
        private sbyte[] getCompressedPrxFile(string dirName, string fileName)
        {
            string proxyFileName;

            if (string.ReferenceEquals(dirName, null) || dirName.Length == 0)
            {
                proxyFileName = fileName;
            }
            else
            {
                proxyFileName = dirName + "/" + fileName;
            }

            IVirtualFile vFileUncompressed = base.ioOpen(proxyFileName, PSP_O_RDONLY, 0);

            if (vFileUncompressed == null)
            {
                return(null);
            }
            sbyte[] bufferUncompressed = Utilities.readCompleteFile(vFileUncompressed);
            vFileUncompressed.ioClose();
            if (bufferUncompressed == null)
            {
                return(null);
            }

            int headerMagic = Utilities.readUnaligned32(bufferUncompressed, 0);

            if (headerMagic != Elf32Header.ELF_MAGIC)
            {
                return(bufferUncompressed);
            }

            int lengthUncompressed = bufferUncompressed.Length;

            System.IO.MemoryStream osCompressed = new System.IO.MemoryStream(PSP_HEADER_SIZE + 9 + lengthUncompressed);
            try
            {
                writePspHeader(osCompressed, bufferUncompressed, fileName);
                // loadcore.prx and sysmem.prx need to be compressed using KL4E.
                // KL4E supports a version where the data is not compressed.
                // Use this simple version as we have no real KL4E compressor.
                if (isKl4eCompression(fileName))
                {
                    writeString(osCompressed, "KL4E", 4);
                    write8(osCompressed, 0x80);                     // Flag indicating that the rest of the data is uncompressed
                    write32(osCompressed, endianSwap32(lengthUncompressed));
                    writeBytes(osCompressed, bufferUncompressed, lengthUncompressed);
                }
                else
                {
                    GZIPOutputStream os = new GZIPOutputStream(osCompressed);
                    os.write(bufferUncompressed, 0, lengthUncompressed);
                    os.close();
                }
            }
            catch (IOException)
            {
            }

            sbyte[] bytes = osCompressed.toByteArray();
            fixPspSizeInHeader(bytes);

            return(bytes);
        }
Ejemplo n.º 7
0
 public virtual int ioClose()
 {
     return(vFile.ioClose());
 }
Ejemplo n.º 8
0
        public XmbIsoVirtualFile(string umdFilename) : base(null)
        {
            this.umdFilename = umdFilename;
            umdName          = System.IO.Path.GetFileName(umdFilename);

            File cacheDirectory   = new File(CacheDirectory);
            bool createCacheFiles = !cacheDirectory.Directory;

            if (createCacheFiles)
            {
                cacheDirectory.mkdirs();
            }

            try
            {
                UmdIsoReader       iso = new UmdIsoReader(umdFilename);
                IVirtualFileSystem vfs = new UmdIsoVirtualFileSystem(iso);
                sections           = new PbpSection[umdFilenames.Length + 1];
                sections[0]        = new PbpSection();
                sections[0].index  = 0;
                sections[0].offset = 0;
                sections[0].size   = 0x28;
                sections[0].availableInContents = true;
                int       offset = 0x28;
                SceIoStat stat   = new SceIoStat();
                for (int i = 0; i < umdFilenames.Length; i++)
                {
                    PbpSection section = new PbpSection();
                    section.index       = i + 1;
                    section.offset      = offset;
                    section.umdFilename = umdFilenames[i];
                    if (vfs.ioGetstat(section.umdFilename, stat) >= 0)
                    {
                        section.size = (int)stat.size;
                        if (log.TraceEnabled)
                        {
                            log.trace(string.Format("{0}: mapping {1} at offset 0x{2:X}, size 0x{3:X}", umdFilename, umdFilenames[i], section.offset, section.size));
                        }
                    }

                    string cacheFileName = getCacheFileName(section);
                    File   cacheFile     = new File(cacheFileName);

                    // Create only cache files for PARAM.SFO and ICON0.PNG
                    if (createCacheFiles && i < 2)
                    {
                        IVirtualFile vFile = vfs.ioOpen(section.umdFilename, IoFileMgrForUser.PSP_O_RDONLY, 0);
                        if (vFile != null)
                        {
                            section.size = (int)vFile.Length();
                            sbyte[] buffer = new sbyte[section.size];
                            int     Length = vFile.ioRead(buffer, 0, buffer.Length);
                            vFile.ioClose();

                            System.IO.Stream os = new System.IO.FileStream(cacheFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                            os.Write(buffer, 0, Length);
                            os.Close();
                        }
                    }

                    if (cacheFile.canRead())
                    {
                        section.cacheFile = cacheFile;
                    }

                    sections[section.index] = section;
                    offset += section.size;
                }
                totalLength = offset;

                contents = new sbyte[offset];
                ByteBuffer buffer = ByteBuffer.wrap(contents).order(ByteOrder.LITTLE_ENDIAN);
                buffer.putInt(PBP.PBP_MAGIC);
                buffer.putInt(0x10000);                 // version
                for (int i = 1; i < sections.Length; i++)
                {
                    buffer.putInt(sections[i].offset);
                }
                int endSectionOffset = sections[sections.Length - 1].offset + sections[sections.Length - 1].size;
                for (int i = sections.Length; i <= 8; i++)
                {
                    buffer.putInt(endSectionOffset);
                }

                if (log.TraceEnabled)
                {
                    log.trace(string.Format("{0}: PBP header :{1}", umdFilename, Utilities.getMemoryDump(contents, sections[0].offset, sections[0].size)));
                }
                vfs.ioExit();
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("XmbIsoVirtualFile", e);
            }
            catch (IOException e)
            {
                Console.WriteLine("XmbIsoVirtualFile", e);
            }
        }
Ejemplo n.º 9
0
        protected internal virtual void readSection(PbpSection section)
        {
            if (section.size > 0)
            {
                try
                {
                    if (section.cacheFile != null)
                    {
                        //if (log.DebugEnabled)
                        {
                            Console.WriteLine(string.Format("XmbIsoVirtualFile.readSection from Cache {0}", section.cacheFile));
                        }

                        System.IO.Stream @is = new System.IO.FileStream(section.cacheFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                        @is.Read(contents, section.offset, section.size);
                        @is.Close();
                    }
                    else
                    {
                        //if (log.DebugEnabled)
                        {
                            Console.WriteLine(string.Format("XmbIsoVirtualFile.readSection from UMD {0}", section.umdFilename));
                        }

                        UmdIsoReader       iso   = new UmdIsoReader(umdFilename);
                        IVirtualFileSystem vfs   = new UmdIsoVirtualFileSystem(iso);
                        IVirtualFile       vFile = vfs.ioOpen(section.umdFilename, IoFileMgrForUser.PSP_O_RDONLY, 0);
                        if (vFile != null)
                        {
                            vFile.ioRead(contents, section.offset, section.size);
                            vFile.ioClose();
                        }
                        vfs.ioExit();
                    }
                }
                catch (IOException e)
                {
                    Console.WriteLine("readSection", e);
                }

                // PARAM.SFO?
                if (section.index == 1)
                {
                    // Patch the CATEGORY in the PARAM.SFO:
                    // the VSH is checking that the CATEGORY value is starting
                    // with 'M' (meaning MemoryStick) and not 'U' (UMD).
                    // Change the first letter 'U' into 'M'.
                    int offset           = section.offset;
                    int keyTableOffset   = readUnaligned32(contents, offset + 8) + offset;
                    int valueTableOffset = readUnaligned32(contents, offset + 12) + offset;
                    int numberKeys       = readUnaligned32(contents, offset + 16);
                    for (int i = 0; i < numberKeys; i++)
                    {
                        int    keyOffset = readUnaligned16(contents, offset + 20 + i * 16);
                        string key       = Utilities.readStringZ(contents, keyTableOffset + keyOffset);
                        if ("CATEGORY".Equals(key))
                        {
                            int  valueOffset    = readUnaligned32(contents, offset + 20 + i * 16 + 12);
                            char valueFirstChar = (char)contents[valueTableOffset + valueOffset];

                            // Change the first letter 'U' into 'M'.
                            if (valueFirstChar == 'U')
                            {
                                contents[valueTableOffset + valueOffset] = (sbyte)'M';
                            }
                            break;
                        }
                    }
                }
            }

            section.availableInContents = true;
        }
Ejemplo n.º 10
0
        public virtual bool loadAndRun()
        {
            if (!enableReboot)
            {
                return(false);
            }

            Memory mem = Memory.Instance;

            StringBuilder      localFileName = new StringBuilder();
            IVirtualFileSystem vfs           = Modules.IoFileMgrForUserModule.getVirtualFileSystem(rebootFileName, localFileName);

            if (vfs == null)
            {
                return(false);
            }

            IVirtualFile vFile = vfs.ioOpen(localFileName.ToString(), IoFileMgrForUser.PSP_O_RDONLY, 0);

            if (vFile == null)
            {
                return(false);
            }

            int rebootFileLength = (int)vFile.Length();

            if (rebootFileLength <= 0)
            {
                return(false);
            }

            SceModule rebootModule = new SceModule(true);

            rebootModule.modname     = Name;
            rebootModule.pspfilename = rebootFileName;
            rebootModule.baseAddress = rebootBaseAddress;
            rebootModule.text_addr   = rebootBaseAddress;
            rebootModule.text_size   = rebootFileLength;
            rebootModule.data_size   = 0;
            rebootModule.bss_size    = 0x26B80;

            const bool fromSyscall = false;

            Emulator.Instance.initNewPsp(fromSyscall);
            Emulator.Instance.ModuleLoaded = true;
            HLEModuleManager.Instance.startModules(fromSyscall);
            Modules.ThreadManForUserModule.Initialise(rebootModule, rebootModule.baseAddress, 0, rebootModule.pspfilename, -1, 0, fromSyscall);

            int        rebootMemSize = rebootModule.text_size + rebootModule.data_size + rebootModule.bss_size;
            SysMemInfo rebootMemInfo = Modules.SysMemUserForUserModule.malloc(VSHELL_PARTITION_ID, "reboot", PSP_SMEM_Addr, rebootMemSize, rebootModule.text_addr);

            if (rebootMemInfo == null)
            {
                return(false);
            }

            TPointer rebootBinAddr = new TPointer(mem, rebootBaseAddress);
            int      readLength    = vFile.ioRead(rebootBinAddr, rebootFileLength);

            vFile.ioClose();
            if (readLength != rebootFileLength)
            {
                return(false);
            }

            markMMIO();

            addFunctionNames(rebootModule);

            SysMemInfo          rebootParamInfo         = Modules.SysMemUserForUserModule.malloc(VSHELL_PARTITION_ID, "reboot-parameters", PSP_SMEM_Addr, 0x10000, rebootParamAddress);
            TPointer            sceLoadCoreBootInfoAddr = new TPointer(mem, rebootParamInfo.addr);
            SceLoadCoreBootInfo sceLoadCoreBootInfo     = new SceLoadCoreBootInfo();

            sceLoadCoreBootInfoAddr.clear(0x1000 + 0x1C000 + 0x380);

            TPointer startAddr = new TPointer(sceLoadCoreBootInfoAddr, 0x1000);

            TPointer sceKernelLoadExecVSHParamAddr = new TPointer(startAddr, 0x1C000);
            TPointer loadModeStringAddr            = new TPointer(sceKernelLoadExecVSHParamAddr, 48);

            loadModeStringAddr.StringZ = "vsh";
            SceKernelLoadExecVSHParam sceKernelLoadExecVSHParam = new SceKernelLoadExecVSHParam();

            sceKernelLoadExecVSHParamAddr.setValue32(48);
            sceKernelLoadExecVSHParam.flags   = 0x10000;
            sceKernelLoadExecVSHParam.keyAddr = loadModeStringAddr;
            sceKernelLoadExecVSHParam.write(sceKernelLoadExecVSHParamAddr);

            sceLoadCoreBootInfo.memBase      = MemoryMap.START_KERNEL;
            sceLoadCoreBootInfo.memSize      = MemoryMap.SIZE_RAM;
            sceLoadCoreBootInfo.startAddr    = startAddr;
            sceLoadCoreBootInfo.endAddr      = new TPointer(sceKernelLoadExecVSHParamAddr, 0x380);
            sceLoadCoreBootInfo.modProtId    = -1;
            sceLoadCoreBootInfo.modArgProtId = -1;
            sceLoadCoreBootInfo.model        = Model.Model;
            sceLoadCoreBootInfo.dipswLo      = Modules.KDebugForKernelModule.sceKernelDipswLow32();
            sceLoadCoreBootInfo.dipswHi      = Modules.KDebugForKernelModule.sceKernelDipswHigh32();
            sceLoadCoreBootInfo.unknown72    = MemoryMap.END_USERSPACE | unchecked ((int)0x80000000);         // Must be larger than 0x89000000 + size of pspbtcnf.bin file
            sceLoadCoreBootInfo.cpTime       = Modules.KDebugForKernelModule.sceKernelDipswCpTime();
            sceLoadCoreBootInfo.write(sceLoadCoreBootInfoAddr);

            SceKernelThreadInfo rootThread = Modules.ThreadManForUserModule.getRootThread(null);

            if (rootThread != null)
            {
                rootThread.cpuContext._a0 = sceLoadCoreBootInfoAddr.Address;
                rootThread.cpuContext._a1 = sceKernelLoadExecVSHParamAddr.Address;
                rootThread.cpuContext._a2 = SCE_INIT_APITYPE_KERNEL_REBOOT;
                rootThread.cpuContext._a3 = Modules.SysMemForKernelModule.sceKernelGetInitialRandomValue();
            }

            // This will set the Log4j MDC values for the root thread
            Emulator.Scheduler.addAction(new SetLog4jMDC());

            //if (log.DebugEnabled)
            {
                Console.WriteLine(string.Format("sceReboot arg0={0}, arg1={1}", sceLoadCoreBootInfoAddr, sceKernelLoadExecVSHParamAddr));
            }

            return(true);
        }