Ejemplo n.º 1
0
        private void WriteData(
            long Position,
            byte[]      Data,
            MemoryType Type,
            AMemoryPerm Perm)
        {
            Memory.Manager.Map(Position, Data.Length, (int)Type, AMemoryPerm.Write);

            Memory.WriteBytes(Position, Data);

            Memory.Manager.Reprotect(Position, Data.Length, Perm);
        }
Ejemplo n.º 2
0
        //http://switchbrew.org/index.php?title=Homebrew_ABI
        public static void WriteHbAbiData(AMemory Memory, long Position, int MainThreadHandle, string SwitchPath)
        {
            //MainThreadHandle.
            WriteConfigEntry(Memory, ref Position, 1, 0, MainThreadHandle);

            //NextLoadPath.
            WriteConfigEntry(Memory, ref Position, 2, 0, Position + 0x200, Position + 0x400);

            //Argv.
            long ArgvPosition = Position + 0xC00;

            Memory.WriteBytes(ArgvPosition, Encoding.ASCII.GetBytes(SwitchPath + "\0"));

            WriteConfigEntry(Memory, ref Position, 5, 0, 0, ArgvPosition);

            //AppletType.
            WriteConfigEntry(Memory, ref Position, 7);

            //EndOfList.
            WriteConfigEntry(Memory, ref Position, 0);
        }
Ejemplo n.º 3
0
        //http://switchbrew.org/index.php?title=Homebrew_ABI
        public static void WriteHbAbiData(AMemory Memory, long Position, int MainThreadHandle, string SwitchPath)
        {
            Memory.Manager.Map(Position, AMemoryMgr.PageSize, (int)MemoryType.Normal, AMemoryPerm.RW);

            //MainThreadHandle
            WriteConfigEntry(Memory, ref Position, 1, 0, MainThreadHandle);

            //NextLoadPath
            WriteConfigEntry(Memory, ref Position, 2, 0, Position + 0x200, Position + 0x400);

            // Argv
            long ArgvPosition = Position + 0xC00;

            WriteConfigEntry(Memory, ref Position, 5, 0, 0, ArgvPosition);
            Memory.WriteBytes(ArgvPosition, Encoding.ASCII.GetBytes(SwitchPath + "\0"));

            //AppletType
            WriteConfigEntry(Memory, ref Position, 7);

            //EndOfList
            WriteConfigEntry(Memory, ref Position, 0);
        }
Ejemplo n.º 4
0
        public static long IpcCall(
            Switch Ns,
            Process Process,
            AMemory Memory,
            KSession Session,
            IpcMessage Request,
            long CmdPtr)
        {
            IpcMessage Response = new IpcMessage();

            using (MemoryStream Raw = new MemoryStream(Request.RawData))
            {
                BinaryReader ReqReader = new BinaryReader(Raw);

                if (Request.Type == IpcMessageType.Request)
                {
                    Response.Type = IpcMessageType.Response;

                    using (MemoryStream ResMS = new MemoryStream())
                    {
                        BinaryWriter ResWriter = new BinaryWriter(ResMS);

                        ServiceCtx Context = new ServiceCtx(
                            Ns,
                            Process,
                            Memory,
                            Session,
                            Request,
                            Response,
                            ReqReader,
                            ResWriter);

                        Session.Service.CallMethod(Context);

                        Response.RawData = ResMS.ToArray();
                    }
                }
                else if (Request.Type == IpcMessageType.Control)
                {
                    long Magic = ReqReader.ReadInt64();
                    long CmdId = ReqReader.ReadInt64();

                    switch (CmdId)
                    {
                    case 0:
                    {
                        Request = FillResponse(Response, 0, Session.Service.ConvertToDomain());

                        break;
                    }

                    case 3:
                    {
                        Request = FillResponse(Response, 0, 0x500);

                        break;
                    }

                    //TODO: Whats the difference between IpcDuplicateSession/Ex?
                    case 2:
                    case 4:
                    {
                        int Unknown = ReqReader.ReadInt32();

                        int Handle = Process.HandleTable.OpenHandle(Session);

                        Response.HandleDesc = IpcHandleDesc.MakeMove(Handle);

                        Request = FillResponse(Response, 0);

                        break;
                    }

                    default: throw new NotImplementedException(CmdId.ToString());
                    }
                }
                else if (Request.Type == IpcMessageType.CloseSession)
                {
                    //TODO
                }
                else
                {
                    throw new NotImplementedException(Request.Type.ToString());
                }

                Memory.WriteBytes(CmdPtr, Response.GetBytes(CmdPtr));
            }

            return(0);
        }
Ejemplo n.º 5
0
        public Executable(IExecutable Exe, KMemoryManager MemoryManager, AMemory Memory, long ImageBase)
        {
            Dynamic = new List <ElfDyn>();

            FilePath = Exe.FilePath;

            if (FilePath != null)
            {
                Name = Path.GetFileNameWithoutExtension(FilePath.Replace(Homebrew.TemporaryNroSuffix, ""));
            }

            this.Memory        = Memory;
            this.MemoryManager = MemoryManager;
            this.ImageBase     = ImageBase;
            this.ImageEnd      = ImageBase;

            long TextPosition = ImageBase + (uint)Exe.TextOffset;
            long ROPosition   = ImageBase + (uint)Exe.ROOffset;
            long DataPosition = ImageBase + (uint)Exe.DataOffset;

            long TextSize = (uint)IntUtils.AlignUp(Exe.Text.Length, KMemoryManager.PageSize);
            long ROSize   = (uint)IntUtils.AlignUp(Exe.RO.Length, KMemoryManager.PageSize);
            long DataSize = (uint)IntUtils.AlignUp(Exe.Data.Length, KMemoryManager.PageSize);

            long DataAndBssSize = (uint)IntUtils.AlignUp(Exe.BssSize, KMemoryManager.PageSize) + DataSize;

            ImageEnd = DataPosition + DataAndBssSize;

            MemoryManager.HleMapProcessCode(TextPosition, TextSize + ROSize + DataAndBssSize);

            MemoryManager.SetProcessMemoryPermission(ROPosition, ROSize, MemoryPermission.Read);
            MemoryManager.SetProcessMemoryPermission(DataPosition, DataAndBssSize, MemoryPermission.ReadAndWrite);

            Memory.WriteBytes(TextPosition, Exe.Text);
            Memory.WriteBytes(ROPosition, Exe.RO);
            Memory.WriteBytes(DataPosition, Exe.Data);

            if (Exe.Mod0Offset == 0)
            {
                return;
            }

            long Mod0Offset = ImageBase + Exe.Mod0Offset;

            int  Mod0Magic        = Memory.ReadInt32(Mod0Offset + 0x0);
            long DynamicOffset    = Memory.ReadInt32(Mod0Offset + 0x4) + Mod0Offset;
            long BssStartOffset   = Memory.ReadInt32(Mod0Offset + 0x8) + Mod0Offset;
            long BssEndOffset     = Memory.ReadInt32(Mod0Offset + 0xc) + Mod0Offset;
            long EhHdrStartOffset = Memory.ReadInt32(Mod0Offset + 0x10) + Mod0Offset;
            long EhHdrEndOffset   = Memory.ReadInt32(Mod0Offset + 0x14) + Mod0Offset;
            long ModObjOffset     = Memory.ReadInt32(Mod0Offset + 0x18) + Mod0Offset;

            while (true)
            {
                long TagVal = Memory.ReadInt64(DynamicOffset + 0);
                long Value  = Memory.ReadInt64(DynamicOffset + 8);

                DynamicOffset += 0x10;

                ElfDynTag Tag = (ElfDynTag)TagVal;

                if (Tag == ElfDynTag.DT_NULL)
                {
                    break;
                }

                Dynamic.Add(new ElfDyn(Tag, Value));
            }

            long StrTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_STRTAB);
            long SymTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_SYMTAB);

            long SymEntSize = GetFirstValue(ElfDynTag.DT_SYMENT);

            List <ElfSym> Symbols = new List <ElfSym>();

            while ((ulong)SymTblAddr < (ulong)StrTblAddr)
            {
                ElfSym Sym = GetSymbol(SymTblAddr, StrTblAddr);

                Symbols.Add(Sym);

                SymTblAddr += SymEntSize;
            }

            SymbolTable = Array.AsReadOnly(Symbols.OrderBy(x => x.Value).ToArray());
        }