Exemple #1
0
        public static void SendPayload(FileInfo info)
        {
            Writes = 0;
            byte[] payload = File.ReadAllBytes(info.FullName);
            var    buf     = new byte[0x10];

            Device.ReadPipe(0x81, buf, 0x10, out _, IntPtr.Zero);
            WritePayload(Device, SwizzlePayload(payload));

            if (Writes % 2 != 1)
            {
                Console.WriteLine("Switching buffers...");
                Device.WritePipe(1, new byte[0x1000], 0x1000, out _, IntPtr.Zero);
            }

            var setup = new WINUSB_SETUP_PACKET
            {
                RequestType = 0x81,
                Request     = 0,
                Value       = 0,
                Index       = 0,
                Length      = 0x7000
            };

            Task.Run(() => Device.ControlTransfer(setup, new byte[0x7000], 0x7000, out var b, IntPtr.Zero));
        }
Exemple #2
0
        private static void Write(UsbK wrt, byte[] payload)
        {
            var buffer = new byte[0x1000];

            for (var i = 0; i < payload.Length - 1; i += 0x1000, _writes++)
            {
                Buffer.BlockCopy(payload, i, buffer, 0, 0x1000);
                wrt.WritePipe(1, buffer, 0x1000, out _, IntPtr.Zero);
            }
        }
Exemple #3
0
            public bool Write(byte[] data)
            {
                bool result = Device.WritePipe(1, data, data.Length, out int bytesTransfered, IntPtr.Zero);

                if (!result)
                {
                    return(false);
                }
                if (bytesTransfered != data.Length)
                {
                    return(false);
                }
                return(true);
            }
Exemple #4
0
        protected override int DriverWrite(byte[] p)
        {
            int bytesWritten = 0;

            try {
                if (DriverAPI != null)
                {
                    int numBytesToWrite = p.Length;
                    if (numBytesToWrite > 0)
                    {
                        Array.Copy(p, TempWriteBuffer, numBytesToWrite);
                        if (DriverAPI.WritePipe(WritePipeId, TempWriteBuffer, p.Length, out bytesWritten, IntPtr.Zero))
                        {
                            if (bytesWritten > 0)
                            {
                                if (Settings.DebugLevel > 7)
                                {
                                    Log.d(TAG, "DriverWrite Bytes:" + numBytesToWrite + "\n");
                                    String s = "{";
                                    for (int k = 0; k < p.Length; k++)
                                    {
                                        s += String.Format("{0:x2} ", p[k]);
                                    }
                                    s += "}\n";
                                    Log.d(TAG, s);
                                }
                            }
                        }
                        else
                        {
                            if (Settings.DebugLevel > 8)
                            {
                                Log.d(TAG, "DriverWrite " + Error.Message() + "\n");
                            }
                        }
                    }
                }
            } catch (Exception e) {
                if (Settings.DebugLevel > 0)
                {
                    Log.d(TAG, "DriverWrite exception:" + e.Message);
                }
            }
            return(bytesWritten);
        }
Exemple #5
0
        private int StmSubmitWrite(KSTM_INFO info,
                                   KSTM_XFER_CONTEXT xferContext,
                                   int xferContextIndex,
                                   IntPtr nativeOverlapped)
        {
            int notUsedForAsync;
            int bufferSize = xferContext.TransferLength;

            bool success   = mUsb.WritePipe(mPipeId, xferContext.Buffer, bufferSize, out notUsedForAsync, nativeOverlapped);
            int  errorCode = Marshal.GetLastWin32Error();

            if (!success && errorCode == ErrorCodes.IoPending)
            {
                return(ErrorCodes.Success);
            }

            Debug.WriteLine(
                String.Format("Failed submitting transfer. PipeID:{0:X2}h ErrorCode:{1:X8}h",
                              mPipeId,
                              errorCode),
                "ERROR");

            return(errorCode);
        }
Exemple #6
0
 public static void WriteBytes(this UsbK USB, byte[] Data)
 {
     USB.WritePipe(1, Data, Data.Length, out _, IntPtr.Zero);
 }
Exemple #7
0
        private int sendCommand(HUD_COMMAND cmd, HUD_DIR dir, ref byte[] buffer)
        {
            uint   calcCrc     = 0x00000000;
            int    transferred = 0;
            ushort cmdDataLen  = 0;
            bool   success     = false;
            int    pos         = 0;

            if (buffer != null || buffer.Length > 0)
            {
                if (buffer.Length > 492)
                {
                    tbStatus.Text += "Invalid Buffer Size mus tbe less than 492 bytes\r\n";
                    return(-1);
                }
                cmdDataLen = (ushort)buffer.Length;
            }

            tbStatus.Text += "Sending data\r\n";

            /* packet header */
            byte[] temp = new byte[20 + cmdDataLen];
            temp[0] = (byte)HUD_SB.SB_COMMAND;    /* start byte */
            temp[1] = (byte)HUD_CTRL.CB_DISP_IMG; /* control flag */

            ushort len1 = (ushort)(8 + cmdDataLen);

            temp[2] = (byte)(len1 >> 8);
            temp[3] = (byte)len1;

            uint len2 = (uint)(8 + cmdDataLen);

            temp[4] = (byte)(len2 >> 24);
            temp[5] = (byte)(len2 >> 16);
            temp[6] = (byte)(len2 >> 8);
            temp[7] = (byte)len2;

            temp[8]  = (byte)(calcCrc >> 24); /* CRC */
            temp[9]  = (byte)(calcCrc >> 16);
            temp[10] = (byte)(calcCrc >> 8);
            temp[11] = (byte)calcCrc;

            /* command header */
            temp[12] = (byte)cmd;              /* Command Byte */
            temp[13] = (byte)dir;              /* read 0x00 or write 0x01 command */
            commandId++;
            temp[14] = (byte)(commandId >> 8); /* Command ID number */
            temp[15] = (byte)commandId;

            temp[16] = (byte)(cmdDataLen >> 8); /* data length */
            temp[17] = (byte)cmdDataLen;

            temp[18] = (byte)0x00; /* reserved */
            temp[19] = (byte)0x00;

            /* command data */
            pos = 20;
            if (cmdDataLen > 0)
            {
                for (int x = 0; x < cmdDataLen; x++)
                {
                    temp[pos] = buffer[x];
                }
            }

            tbStatus.Text += "====> Packet Length: " + cmdDataLen + " <====\r\n";

            /* send via CDC */
            success = usb.WritePipe(EP_CDC, temp, temp.Length, out transferred, IntPtr.Zero);
            if (!success)
            {
                tbStatus.Text += "Failed to send command to HUD\r\n";
            }
            else
            {
                tbStatus.Text += "====> Sent " + transferred + " Bytes <====\r\n";
            }

            return(commandId);
        }
Exemple #8
0
 public static void WriteBuffer(this UsbK USB, byte[] Buf)
 {
     USB.WritePipe(WritePipeId, Buf, Buf.Length, out int _, IntPtr.Zero);
 }
Exemple #9
0
        public static void CommandHandleLoop(this UsbK USB)
        {
            Array.Clear(CommandBlockBuf, 0, CommandBlockLength);
            USB.ReadPipe(ReadPipeId, CommandBlockBuf, CommandBlockLength, out int readbuf, IntPtr.Zero);
            if (readbuf == 0)
            {
                return;
            }
            if (readbuf != CommandBlockLength)
            {
                Program.Warn.LogL($"Command block read size mismatch (expected: 0x{CommandBlockLength:X}, got 0x{readbuf:X}!)");
                return;
            }
            using (var strm = new MemoryStream(CommandBlockBuf))
            {
                using (var inblock = new BinaryReader(strm))
                {
                    var inmagic = inblock.ReadUInt32();
                    if (inmagic != Command.InputMagic)
                    {
                        Program.Warn.LogL($"Input magic mismatch (expected: 0x{Command.InputMagic:X}, got 0x{inmagic:X}!)");
                        return;
                    }
                    var command = inblock.ReadUInt32();
                    if (!Enum.IsDefined(typeof(CommandId), command))
                    {
                        Program.Warn.LogL($"Invalid command Id (got {command}!)");
                        return;
                    }

                    CommandId cmd = (CommandId)command;
                    Program.Log.LogL("Received command: " + cmd.ToString());

                    var outblockbuf = new byte[CommandBlockLength];
                    var bufs        = new List <byte[]>();

                    using (var wstrm = new MemoryStream(outblockbuf))
                    {
                        using (var outblock = new BinaryWriter(wstrm))
                        {
                            bool out_written = false;

                            void WriteOutBlockBase(uint Result)
                            {
                                if (out_written)
                                {
                                    return;
                                }
                                out_written = true;
                                outblock.Write(Command.OutputMagic);
                                outblock.Write(Result);
                            }

                            switch (cmd)
                            {
                            case CommandId.GetDriveCount:
                            {
                                var drives = DriveInfo.GetDrives().Where(drive => drive.IsReady).ToList();
                                LastDrives = drives;
                                WriteOutBlockBase(Command.ResultSuccess);
                                outblock.Write((uint)drives.Count);
                                break;
                            }

                            case CommandId.GetDriveInfo:
                            {
                                var driveidx = inblock.ReadUInt32();
                                var drives   = LastDrives;
                                if (LastDrives == null)
                                {
                                    drives     = DriveInfo.GetDrives().Where(drive => drive.IsReady).ToList();
                                    LastDrives = drives;
                                }
                                if (driveidx >= drives.Count)
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                }
                                else
                                {
                                    var drive = drives[(int)driveidx];
                                    WriteOutBlockBase(Command.ResultSuccess);
                                    string label  = drive.VolumeLabel;
                                    string prefix = drive.Name.Substring(0, 1);
                                    if (string.IsNullOrEmpty(label) || label.IsUnicode())
                                    {
                                        label = prefix;
                                    }
                                    outblock.WriteStringEx(label);
                                    outblock.WriteStringEx(prefix);
                                    outblock.Write((uint)drive.TotalFreeSpace);
                                    outblock.Write((uint)drive.TotalSize);
                                }
                                break;
                            }

                            case CommandId.StatPath:
                            {
                                var   path     = inblock.ReadStringEx().NormalizeAsPath();
                                uint  type     = 0;
                                ulong filesize = 0;
                                if (File.Exists(path))
                                {
                                    type     = 1;
                                    filesize = (ulong)new FileInfo(path).Length;
                                }
                                else if (Directory.Exists(path))
                                {
                                    type = 2;
                                }
                                else
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                    break;
                                }
                                WriteOutBlockBase(Command.ResultSuccess);
                                outblock.Write(type);
                                outblock.Write(filesize);
                                break;
                            }

                            case CommandId.GetFileCount:
                            {
                                var path = inblock.ReadStringEx().NormalizeAsPath();
                                if (Directory.Exists(path))
                                {
                                    if ((LastFilesPath != path) || (LastFiles == null))
                                    {
                                        LastFilesPath = path;
                                        LastFiles     = Directory.GetFiles(path);
                                    }
                                    uint count = (uint)LastFiles.Length;
                                    WriteOutBlockBase(Command.ResultSuccess);
                                    outblock.Write(count);
                                }
                                else
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                }
                                break;
                            }

                            case CommandId.GetFile:
                            {
                                var path    = inblock.ReadStringEx().NormalizeAsPath();
                                var fileidx = inblock.ReadUInt32();
                                if (Directory.Exists(path))
                                {
                                    if ((LastFilesPath != path) || (LastFiles == null))
                                    {
                                        LastFilesPath = path;
                                        LastFiles     = Directory.GetFiles(path);
                                    }

                                    if (fileidx >= LastFiles.Length)
                                    {
                                        WriteOutBlockBase(Command.ResultInvalidInput);
                                    }
                                    else
                                    {
                                        var filename = Path.GetFileName(LastFiles[fileidx]);
                                        WriteOutBlockBase(Command.ResultSuccess);
                                        outblock.WriteStringEx(filename);
                                    }
                                }
                                else
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                }
                                break;
                            }

                            case CommandId.GetDirectoryCount:
                            {
                                var path = inblock.ReadStringEx().NormalizeAsPath();
                                if (Directory.Exists(path))
                                {
                                    if ((LastDirectoriesPath != path) || (LastDirectories == null))
                                    {
                                        LastDirectoriesPath = path;
                                        LastDirectories     = Directory.GetDirectories(path);
                                    }

                                    uint count = (uint)LastDirectories.Length;
                                    WriteOutBlockBase(Command.ResultSuccess);
                                    outblock.Write(count);
                                }
                                else
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                }
                                break;
                            }

                            case CommandId.GetDirectory:
                            {
                                var path   = inblock.ReadStringEx().NormalizeAsPath();
                                var diridx = inblock.ReadUInt32();
                                if (Directory.Exists(path))
                                {
                                    if ((LastDirectoriesPath != path) || (LastDirectories == null))
                                    {
                                        LastDirectoriesPath = path;
                                        LastDirectories     = Directory.GetDirectories(path);
                                    }

                                    if (diridx >= LastDirectories.Length)
                                    {
                                        WriteOutBlockBase(Command.ResultInvalidInput);
                                    }
                                    else
                                    {
                                        var dirname = Path.GetFileName(LastDirectories[diridx]);
                                        WriteOutBlockBase(Command.ResultSuccess);
                                        outblock.WriteStringEx(dirname);
                                    }
                                }
                                else
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                }
                                break;
                            }

                            case CommandId.ReadFile:
                            {
                                var path   = inblock.ReadStringEx().NormalizeAsPath();
                                var offset = inblock.ReadUInt64();
                                var size   = inblock.ReadUInt64();
                                try
                                {
                                    if (string.IsNullOrEmpty(LastRFile) || (LastRFile != path))
                                    {
                                        if (LastReadFile != null)
                                        {
                                            LastReadFile.Close();
                                            LastReadFile = null;
                                        }
                                        LastRFile    = path;
                                        LastReadFile = new BinaryReader(new FileStream(LastRFile, FileMode.Open, FileAccess.Read));
                                    }
                                    LastReadFile.BaseStream.Seek((long)offset, SeekOrigin.Begin);
                                    byte[] data   = new byte[size];
                                    var    rbytes = LastReadFile.Read(data, 0, (int)size);
                                    if (rbytes > 0)
                                    {
                                        WriteOutBlockBase(Command.ResultSuccess);
                                        outblock.Write((ulong)rbytes);
                                        bufs.Add(data);
                                    }
                                }
                                catch
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                    break;
                                }
                                break;
                            }

                            case CommandId.WriteFile:
                            {
                                var path = inblock.ReadStringEx().NormalizeAsPath();
                                var size = inblock.ReadUInt64();
                                var data = USB.ReadBuffer(size);
                                try
                                {
                                    if (string.IsNullOrEmpty(LastWFile) || (LastWFile != path))
                                    {
                                        if (LastWriteFile != null)
                                        {
                                            LastWriteFile.Close();
                                            LastWriteFile = null;
                                        }
                                        LastWFile     = path;
                                        LastWriteFile = new FileStream(path, FileMode.Append, FileAccess.Write);
                                    }
                                    LastWriteFile.Write(data, 0, (int)size);
                                    WriteOutBlockBase(Command.ResultSuccess);
                                }
                                catch
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                    break;
                                }
                                break;
                            }

                            case CommandId.Create:
                            {
                                var type = inblock.ReadUInt32();
                                var path = inblock.ReadStringEx().NormalizeAsPath();
                                switch (type)
                                {
                                case 1:
                                {
                                    try
                                    {
                                        File.Create(path);
                                        WriteOutBlockBase(Command.ResultSuccess);
                                    }
                                    catch
                                    {
                                        WriteOutBlockBase(Command.ResultInvalidInput);
                                    }
                                    break;
                                }

                                case 2:
                                {
                                    try
                                    {
                                        Directory.CreateDirectory(path);
                                        WriteOutBlockBase(Command.ResultSuccess);
                                    }
                                    catch
                                    {
                                        WriteOutBlockBase(Command.ResultInvalidInput);
                                    }
                                    break;
                                }

                                default:
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                    break;
                                }
                                }
                                break;
                            }

                            case CommandId.Delete:
                            {
                                var type = inblock.ReadUInt32();
                                var path = inblock.ReadStringEx().NormalizeAsPath();
                                switch (type)
                                {
                                case 1:
                                {
                                    try
                                    {
                                        File.Delete(path);
                                        WriteOutBlockBase(Command.ResultSuccess);
                                    }
                                    catch
                                    {
                                        WriteOutBlockBase(Command.ResultInvalidInput);
                                    }
                                    break;
                                }

                                case 2:
                                {
                                    try
                                    {
                                        Directory.Delete(path, true);
                                        WriteOutBlockBase(Command.ResultSuccess);
                                    }
                                    catch
                                    {
                                        WriteOutBlockBase(Command.ResultInvalidInput);
                                    }
                                    break;
                                }

                                default:
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                    break;
                                }
                                }
                                break;
                            }

                            case CommandId.Rename:
                            {
                                var type    = inblock.ReadUInt32();
                                var path    = inblock.ReadStringEx().NormalizeAsPath();
                                var pathnew = inblock.ReadStringEx().NormalizeAsPath();
                                switch (type)
                                {
                                case 1:
                                {
                                    try
                                    {
                                        Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(path, pathnew);
                                        WriteOutBlockBase(Command.ResultSuccess);
                                    }
                                    catch
                                    {
                                        WriteOutBlockBase(Command.ResultInvalidInput);
                                    }
                                    break;
                                }

                                case 2:
                                {
                                    try
                                    {
                                        Microsoft.VisualBasic.FileIO.FileSystem.RenameDirectory(path, pathnew);
                                        WriteOutBlockBase(Command.ResultSuccess);
                                    }
                                    catch
                                    {
                                        WriteOutBlockBase(Command.ResultInvalidInput);
                                    }
                                    break;
                                }

                                default:
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                    break;
                                }
                                }
                                break;
                            }

                            case CommandId.GetSpecialPathCount:
                            {
                                WriteOutBlockBase(Command.ResultSuccess);
                                outblock.Write((uint)SpecialPathList.Count);
                                break;
                            }

                            case CommandId.GetSpecialPath:
                            {
                                var spathidx = inblock.ReadUInt32();
                                if (spathidx >= SpecialPathList.Count)
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                }
                                else
                                {
                                    var spath = SpecialPathList.ElementAt((int)spathidx);
                                    var path  = Environment.GetFolderPath(spath.Value).NormalizeAsGoldleafPath();
                                    WriteOutBlockBase(Command.ResultSuccess);
                                    outblock.WriteStringEx(spath.Key);
                                    outblock.WriteStringEx(path);
                                }
                                break;
                            }

                            case CommandId.SelectFile:
                            {
                                try
                                {
                                    var openfd = new OpenFileDialog
                                    {
                                        Filter      = "Any file (*.*)|*.*",
                                        Title       = "Select file for Goldleaf",
                                        Multiselect = false
                                    };
                                    if (openfd.ShowDialog() == DialogResult.OK)
                                    {
                                        WriteOutBlockBase(Command.ResultSuccess);
                                        var path = openfd.FileName.NormalizeAsGoldleafPath();
                                        outblock.WriteStringEx(path);
                                    }
                                    else
                                    {
                                        WriteOutBlockBase(Command.ResultInvalidInput);
                                    }
                                }
                                catch
                                {
                                    WriteOutBlockBase(Command.ResultInvalidInput);
                                }
                                break;
                            }
                            }
                        }
                    }

                    USB.WritePipe(WritePipeId, outblockbuf, CommandBlockLength, out int writtenbuf, IntPtr.Zero);

                    if (bufs.Any())
                    {
                        foreach (var buf in bufs)
                        {
                            USB.WriteBuffer(buf);
                        }
                    }

                    if (writtenbuf != CommandBlockLength)
                    {
                        Program.Command.LogL($"Command block write size mismatch (expected: 0x{CommandBlockLength:X}, got 0x{writtenbuf:X}!)");
                    }
                }
            }
        }