public static bool GetHDDKeys(this MiraConnection p_Connection, out DumpHddKeys p_Output)
        {
            p_Output = new DumpHddKeys
            {
                Encrypted = new byte[0x60],
                Key       = new byte[0x20]
            };

            if (!p_Connection.Connected)
            {
                return(false);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Category    = (int)RPC_CATEGORY.RPCCAT_SYSTEM,
                Magic       = MiraConnection.c_Magic,
                ErrorType   = unchecked ((int)UtilityCmds.OrbisUtils_DumpHddKeys),
                Request     = true,
                PayloadSize = (ushort)Marshal.SizeOf <DumpHddKeys>()
            }, p_Connection.SerializeObject(p_Output));

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Result.ErrorType < 0)
            {
                return(false);
            }

            new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());
            p_Output = p_Connection.ReceiveObject <DumpHddKeys>();

            return(true);
        }
        public static byte[] Read(this MiraConnection p_Connection, int p_Descriptor, uint p_Offset, ulong p_Size)
        {
            if (!p_Connection.Connected)
            {
                return(null);
            }

            if (p_Descriptor < 0)
            {
                return(null);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_Read,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferRead>(),
            }, p_Connection.SerializeObject(new FileTransferRead
            {
                Handle = p_Descriptor,
                Offset = p_Offset,
                Size   = p_Size
            }));

            var s_Response = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Response.ErrorType != 0)
            {
                return(null);
            }

            var s_Payload = p_Connection.ReceiveObject <FileTransferRead>();

            var s_BlockCount = s_Payload.Size / 0x4000;
            var s_Leftover   = s_Payload.Size % 0x4000;

            byte[] s_Data;
            using (var s_Reader = new BinaryReader(p_Connection.GetStream(), Encoding.ASCII, true))
            {
                using (var s_Writer = new BinaryWriter(new MemoryStream()))
                {
                    for (ulong i = 0; i < s_BlockCount; ++i)
                    {
                        s_Writer.Write(s_Reader.ReadBytes(0x4000));
                    }

                    s_Writer.Write(s_Reader.ReadBytes((int)s_Leftover));
                    s_Data = ((MemoryStream)s_Writer.BaseStream).ToArray();
                }
            }

            return(s_Data);
        }
        public static void ReadFile(this MiraConnection p_Connection, string p_SourcePath, string p_LocalPath)
        {
            if (!p_Connection.Connected)
            {
                return;
            }

            int s_Descriptor = p_Connection.Open(p_SourcePath, 0, 0);

            if (s_Descriptor < 0)
            {
                return;
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_ReadFile,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferReadFile>()
            }, p_Connection.SerializeObject(new FileTransferReadFile
            {
                Handle = s_Descriptor,
                Size   = 0
            }));

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Result.ErrorType != 0)
            {
                return;
            }

            var s_Response = p_Connection.ReceiveObject <FileTransferReadFile>();

            var s_BlockCount = s_Response.Size / 0x4000;
            var s_Leftover   = s_Response.Size % 0x4000;

            using (var s_Reader = new BinaryReader(p_Connection.GetStream(), Encoding.ASCII, true))
            {
                using (var s_Writer = new BinaryWriter(new FileStream(p_LocalPath, FileMode.Create, FileAccess.ReadWrite)))
                {
                    for (ulong i = 0; i < s_BlockCount; ++i)
                    {
                        s_Writer.Write(s_Reader.ReadBytes(0x4000));
                    }

                    s_Writer.Write(s_Reader.ReadBytes((int)s_Leftover));
                }
            }

            // Close the handle
            p_Connection.Close(s_Descriptor);
        }
        public static bool WriteFile(this MiraConnection p_Connection, string p_Path, byte[] p_Data)
        {
            if (!p_Connection.Connected)
            {
                return(false);
            }

            var s_Descriptor = p_Connection.Open(p_Path, 0x0002 | 0x0200 /* O_RDWR | O_CREAT*/, 0777);

            if (s_Descriptor < 0)
            {
                return(false);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_WriteFile,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferWriteFile>()
            }, p_Connection.SerializeObject(new FileTransferWriteFile
            {
                Handle = s_Descriptor,
                Size   = (ulong)p_Data.Length
            }));

            var s_BlockCount = p_Data.Length / 0x4000;
            var s_Leftover   = p_Data.Length % 0x4000;

            using (var s_Reader = new BinaryReader(new MemoryStream(p_Data)))
            {
                using (var s_Writer = new BinaryWriter(p_Connection.GetStream(), Encoding.ASCII, true))
                {
                    for (int i = 0; i < s_BlockCount; ++i)
                    {
                        s_Writer.Write(s_Reader.ReadBytes(0x4000));
                    }

                    s_Writer.Write(s_Reader.ReadBytes((int)s_Leftover));
                }
            }

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            p_Connection.Close(s_Descriptor);

            return(s_Result.ErrorType == 0);
        }
        public static IEnumerable <FileTransferGetdents> GetDents(this MiraConnection p_Connection, string p_Path)
        {
            if (!p_Connection.Connected)
            {
                return(null);
            }

            // Send the request
            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_GetDents,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferGetdents>(),
            }, p_Connection.SerializeObject(new FileTransferGetdents
            {
                Path   = p_Path,
                Handle = -1,
                Type   = 0
            }));

            // Check the result
            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Result.ErrorType != 0)
            {
                return(Enumerable.Empty <FileTransferGetdents>());
            }

            // Read the amount of directory entries
            var   s_List      = new List <FileTransferGetdents>();
            ulong s_DentCount = 0;

            using (var s_Reader = new BinaryReader(p_Connection.GetStream(), Encoding.ASCII, true))
                s_DentCount = s_Reader.ReadUInt64();

            Debug.WriteLine($"Dent Count: {s_DentCount}");

            for (ulong i = 0; i < s_DentCount; ++i)
            {
                s_List.Add(p_Connection.ReceiveObject <FileTransferGetdents>());
            }

            return(s_List);
        }
Example #6
0
        public void RoundTripTest()
        {
            RpcMessageHeader header = RpcMessageHeader.Accepted(1);

            RpcMessageHeader clone = null;

            using (MemoryStream stream = new MemoryStream())
            {
                XdrDataWriter writer = new XdrDataWriter(stream);
                header.Write(writer);

                stream.Position = 0;
                XdrDataReader reader = new XdrDataReader(stream);
                clone = new RpcMessageHeader(reader);
            }

            Assert.Equal(header, clone);
        }
        public static bool Delete(this MiraConnection p_Connection, string p_Path)
        {
            if (!p_Connection.Connected)
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(p_Path))
            {
                return(false);
            }

            if (p_Path.Length > 255)
            {
                return(false);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = unchecked ((int)FileTransferCmds.FileTransfer_Delete),
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferDelete>(),
            }, p_Connection.SerializeObject(new FileTransferDelete
            {
                Path = p_Path
            }));

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Result.ErrorType != 0)
            {
                return(false);
            }

            return(true);
        }
        public static int Open(this MiraConnection p_Connection, string p_Path, int p_Flags, int p_Mode)
        {
            if (!p_Connection.Connected)
            {
                return(-1);
            }

            if (p_Path.Length > 255)
            {
                return(-1);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_Open,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferOpen>()
            }, p_Connection.SerializeObject(new FileTransferOpen
            {
                Flags  = p_Flags,
                Handle = -1,
                Mode   = p_Mode,
                Path   = p_Path
            }));

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Result.ErrorType != 0)
            {
                return(-1);
            }

            var s_Response = p_Connection.ReceiveObject <FileTransferOpen>();

            return(s_Response.Handle);
        }
        public static bool Stat(this MiraConnection p_Connection, string p_Path, out FileTransferStat p_Stat)
        {
            p_Stat = new FileTransferStat();

            if (!p_Connection.Connected)
            {
                return(false);
            }

            if (p_Path.Length > 255)
            {
                return(false);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = unchecked ((int)FileTransferCmds.FileTransfer_Stat),
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferStat>()
            }, p_Connection.SerializeObject(new FileTransferStat
            {
                Path = p_Path
            }));

            var s_Response = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Response.ErrorType != 0)
            {
                return(false);
            }

            p_Stat = p_Connection.ReceiveObject <FileTransferStat>();

            return(true);
        }
        public static int Kill(this MiraConnection p_Connection, int p_Pid, int p_Signal)
        {
            if (!p_Connection.Connected)
            {
                return(-1);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Category    = (int)RPC_CATEGORY.RPCCAT_DBG,
                Magic       = MiraConnection.c_Magic,
                ErrorType   = unchecked ((int)DebuggerCmds.Debugger_Kill),
                Request     = true,
                PayloadSize = (ushort)Marshal.SizeOf <DebuggerKill>()
            }, p_Connection.SerializeObject(new DebuggerKill
            {
                Pid    = p_Pid,
                Signal = p_Signal
            }));

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            return((int)s_Result.ErrorType);
        }
Example #11
0
        public bool Open()
        {
            if (IsOpen())
            {
                return(true);
            }

            var s_Connection = m_Device.Connection;

            // Open the handle to the tty we want, /dev/console, /dev/deci_tty7, etc
            m_Descriptor = s_Connection.Open(m_TtyPath, 0 /*O_RDONLY*/, 0);
            if (m_Descriptor < 0)
            {
                Program.SetStatus($"could not open tty {m_TtyPath}", 0);
                return(false);
            }

            // Send the RPC message
            s_Connection.SendMessage(new RpcMessageHeader
            {
                Category    = (int)RPC_CATEGORY.RPCCAT_LOG,
                Magic       = MiraConnection.c_Magic,
                ErrorType   = (int)ConsoleCmds.ConsoleCmd_Open,
                Request     = true,
                PayloadSize = (ushort)Marshal.SizeOf <ConsoleOpen>()
            }, new ConsoleOpen
            {
                Port = 0,
                TTY  = m_Descriptor
            });

            ConsoleOpen s_OpenArgs;

            using (var s_Reader = new BinaryReader(s_Connection.GetStream()))
            {
                var s_Header = new RpcMessageHeader(s_Reader.ReadUInt64());
                if (s_Header.ErrorType != 0)
                {
                    return(false);
                }

                var s_Data = s_Reader.ReadBytes(Marshal.SizeOf <ConsoleOpen>());
                s_OpenArgs = s_Connection.DeserializeObject <ConsoleOpen>(s_Data);
            }

            m_Port = s_OpenArgs.Port;
            if (m_Port <= 0)
            {
                return(false);
            }

            m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            {
                ReceiveTimeout = 240
            };

            try
            {
                m_Socket.Connect(m_Device.Hostname, m_Port);
            }
            catch (SocketException p_Exception)
            {
                Console.WriteLine(p_Exception.Message);
                return(false);
            }

            // Create a new listen thread
            m_Thread = new Thread(ConnectAndListen);
            m_Thread.Start();

            return(true);
        }