Exemplo n.º 1
0
        public bool IdentifyController(SafeHandle hDevice, out Kernel32.NVME_IDENTIFY_CONTROLLER_DATA data)
        {
            data = Kernel32.CreateStruct <Kernel32.NVME_IDENTIFY_CONTROLLER_DATA>();
            if (hDevice == null || hDevice.IsInvalid)
            {
                return(false);
            }

            bool result = false;

            Kernel32.NVME_PASS_THROUGH_IOCTL passThrough = Kernel32.CreateStruct <Kernel32.NVME_PASS_THROUGH_IOCTL>();
            passThrough.srb.HeaderLenght = (uint)Marshal.SizeOf <Kernel32.SRB_IO_CONTROL>();
            passThrough.srb.Signature    = Encoding.ASCII.GetBytes(Kernel32.IntelNVMeMiniPortSignature1);
            passThrough.srb.Timeout      = 10;
            passThrough.srb.ControlCode  = Kernel32.NVME_PASS_THROUGH_SRB_IO_CODE;
            passThrough.srb.ReturnCode   = 0;
            passThrough.srb.Length       = (uint)Marshal.SizeOf <Kernel32.NVME_PASS_THROUGH_IOCTL>() - (uint)Marshal.SizeOf <Kernel32.SRB_IO_CONTROL>();
            passThrough.NVMeCmd          = new uint[16];
            passThrough.NVMeCmd[0]       = 6; //identify
            passThrough.NVMeCmd[10]      = 1; //return to host
            passThrough.Direction        = Kernel32.NVME_DIRECTION.NVME_FROM_DEV_TO_HOST;
            passThrough.QueueId          = 0;
            passThrough.DataBufferLen    = (uint)passThrough.DataBuffer.Length;
            passThrough.MetaDataLen      = 0;
            passThrough.ReturnBufferLen  = (uint)Marshal.SizeOf <Kernel32.NVME_PASS_THROUGH_IOCTL>();

            int    length = Marshal.SizeOf <Kernel32.NVME_PASS_THROUGH_IOCTL>();
            IntPtr buffer = Marshal.AllocHGlobal(length);

            Marshal.StructureToPtr(passThrough, buffer, false);

            bool validTransfer = Kernel32.DeviceIoControl(hDevice, Kernel32.IOCTL.IOCTL_SCSI_MINIPORT, buffer, length, buffer, length, out _, IntPtr.Zero);

            if (validTransfer)
            {
                IntPtr offset    = Marshal.OffsetOf <Kernel32.NVME_PASS_THROUGH_IOCTL>(nameof(Kernel32.NVME_PASS_THROUGH_IOCTL.DataBuffer));
                var    newPtr    = IntPtr.Add(buffer, offset.ToInt32());
                int    finalSize = Marshal.SizeOf <Kernel32.NVME_IDENTIFY_CONTROLLER_DATA>();
                IntPtr ptr       = Marshal.AllocHGlobal(Marshal.SizeOf <Kernel32.NVME_IDENTIFY_CONTROLLER_DATA>());
                Kernel32.RtlZeroMemory(ptr, finalSize);
                int len = Math.Min(finalSize, passThrough.DataBuffer.Length);
                Kernel32.RtlCopyMemory(ptr, newPtr, (uint)len);
                Marshal.FreeHGlobal(buffer);

                Kernel32.NVME_IDENTIFY_CONTROLLER_DATA item = Marshal.PtrToStructure <Kernel32.NVME_IDENTIFY_CONTROLLER_DATA>(ptr);
                data = item;
                Marshal.FreeHGlobal(ptr);
                result = true;
            }
            else
            {
                Marshal.FreeHGlobal(buffer);
            }

            return(result);
        }
 public NVMeInfo(int index, Kernel32.NVME_IDENTIFY_CONTROLLER_DATA data)
 {
     Index               = index;
     VID                 = data.VID;
     SSVID               = data.SSVID;
     Serial              = GetString(data.SN);
     Model               = GetString(data.MN);
     Revision            = GetString(data.FR);
     IEEE                = data.IEEE;
     TotalCapacity       = BitConverter.ToUInt64(data.TNVMCAP, 0); // 128bit little endian
     UnallocatedCapacity = BitConverter.ToUInt64(data.UNVMCAP, 0);
     ControllerId        = data.CNTLID;
     NumberNamespaces    = data.NN;
 }
Exemplo n.º 3
0
        public bool IdentifyController(SafeHandle hDevice, out Kernel32.NVME_IDENTIFY_CONTROLLER_DATA data)
        {
            data = Kernel32.CreateStruct <Kernel32.NVME_IDENTIFY_CONTROLLER_DATA>();
            if (hDevice == null || hDevice.IsInvalid)
            {
                return(false);
            }

            bool result = false;

            Kernel32.STORAGE_QUERY_BUFFER nptwb = Kernel32.CreateStruct <Kernel32.STORAGE_QUERY_BUFFER>();
            nptwb.ProtocolSpecific.ProtocolType             = Kernel32.STORAGE_PROTOCOL_TYPE.ProtocolTypeNvme;
            nptwb.ProtocolSpecific.DataType                 = (uint)Kernel32.STORAGE_PROTOCOL_NVME_DATA_TYPE.NVMeDataTypeIdentify;
            nptwb.ProtocolSpecific.ProtocolDataRequestValue = (uint)Kernel32.STORAGE_PROTOCOL_NVME_PROTOCOL_DATA_REQUEST_VALUE.NVMeIdentifyCnsController;
            nptwb.ProtocolSpecific.ProtocolDataOffset       = (uint)Marshal.SizeOf <Kernel32.STORAGE_PROTOCOL_SPECIFIC_DATA>();
            nptwb.ProtocolSpecific.ProtocolDataLength       = (uint)nptwb.Buffer.Length;
            nptwb.PropertyId = Kernel32.STORAGE_PROPERTY_ID.StorageAdapterProtocolSpecificProperty;
            nptwb.QueryType  = Kernel32.STORAGE_QUERY_TYPE.PropertyStandardQuery;

            int    length = Marshal.SizeOf <Kernel32.STORAGE_QUERY_BUFFER>();
            IntPtr buffer = Marshal.AllocHGlobal(length);

            Marshal.StructureToPtr(nptwb, buffer, false);
            bool validTransfer = Kernel32.DeviceIoControl(hDevice, Kernel32.IOCTL.IOCTL_STORAGE_QUERY_PROPERTY, buffer, length, buffer, length, out _, IntPtr.Zero);

            if (validTransfer)
            {
                //map NVME_IDENTIFY_CONTROLLER_DATA to nptwb.Buffer
                IntPtr offset = Marshal.OffsetOf <Kernel32.STORAGE_QUERY_BUFFER>(nameof(Kernel32.STORAGE_QUERY_BUFFER.Buffer));
                var    newPtr = IntPtr.Add(buffer, offset.ToInt32());
                Kernel32.NVME_IDENTIFY_CONTROLLER_DATA item = Marshal.PtrToStructure <Kernel32.NVME_IDENTIFY_CONTROLLER_DATA>(newPtr);
                data = item;
                Marshal.FreeHGlobal(buffer);
                result = true;
            }
            else
            {
                Marshal.FreeHGlobal(buffer);
            }

            return(result);
        }
Exemplo n.º 4
0
        public Storage.NVMeInfo GetInfo()
        {
            if (_handle == null || _handle.IsClosed)
            {
                return(null);
            }


            bool valid = false;
            var  data  = new Kernel32.NVME_IDENTIFY_CONTROLLER_DATA();

            if (NVMeDrive != null)
            {
                valid = NVMeDrive.IdentifyController(_handle, out data);
            }

            if (!valid)
            {
                return(null);
            }


            return(new NVMeInfo(_driveNumber, data));
        }
Exemplo n.º 5
0
        public bool IdentifyController(SafeHandle hDevice, out Kernel32.NVME_IDENTIFY_CONTROLLER_DATA data)
        {
            data = Kernel32.CreateStruct <Kernel32.NVME_IDENTIFY_CONTROLLER_DATA>();
            if (hDevice?.IsInvalid != false)
            {
                return(false);
            }

            bool result = false;

            Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS buffers = Kernel32.CreateStruct <Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS>();

            buffers.Spt.Length             = (ushort)Marshal.SizeOf <Kernel32.SCSI_PASS_THROUGH>();
            buffers.Spt.PathId             = 0;
            buffers.Spt.TargetId           = 0;
            buffers.Spt.Lun                = 0;
            buffers.Spt.SenseInfoLength    = 24;
            buffers.Spt.DataTransferLength = (uint)buffers.DataBuf.Length;
            buffers.Spt.TimeOutValue       = 2;
            buffers.Spt.DataBufferOffset   = Marshal.OffsetOf(typeof(Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS), nameof(Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS.DataBuf));
            buffers.Spt.SenseInfoOffset    = (uint)Marshal.OffsetOf(typeof(Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS), nameof(Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS.SenseBuf));
            buffers.Spt.CdbLength          = 16;
            buffers.Spt.Cdb[0]             = 0xB5; // SECURITY PROTOCOL IN
            buffers.Spt.Cdb[1]             = 0xFE; // Samsung Protocol
            buffers.Spt.Cdb[3]             = 5;    // Identify
            buffers.Spt.Cdb[8]             = 0;    // Transfer Length
            buffers.Spt.Cdb[9]             = 0x40; // Transfer Length
            buffers.Spt.DataIn             = (byte)Kernel32.SCSI_IOCTL_DATA.SCSI_IOCTL_DATA_OUT;
            buffers.DataBuf[0]             = 1;

            int    length = Marshal.SizeOf <Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS>();
            IntPtr buffer = Marshal.AllocHGlobal(length);

            Marshal.StructureToPtr(buffers, buffer, false);
            bool validTransfer = Kernel32.DeviceIoControl(hDevice, Kernel32.IOCTL.IOCTL_SCSI_PASS_THROUGH, buffer, length, buffer, length, out _, IntPtr.Zero);

            Marshal.FreeHGlobal(buffer);

            if (validTransfer)
            {
                //read data from samsung SSD
                buffers                        = Kernel32.CreateStruct <Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS>();
                buffers.Spt.Length             = (ushort)Marshal.SizeOf <Kernel32.SCSI_PASS_THROUGH>();
                buffers.Spt.PathId             = 0;
                buffers.Spt.TargetId           = 0;
                buffers.Spt.Lun                = 0;
                buffers.Spt.SenseInfoLength    = 24;
                buffers.Spt.DataTransferLength = (uint)buffers.DataBuf.Length;
                buffers.Spt.TimeOutValue       = 2;
                buffers.Spt.DataBufferOffset   = Marshal.OffsetOf <Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS>(nameof(Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS.DataBuf));
                buffers.Spt.SenseInfoOffset    = (uint)Marshal.OffsetOf <Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS>(nameof(Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS.SenseBuf));
                buffers.Spt.CdbLength          = 16;
                buffers.Spt.Cdb[0]             = 0xA2; // SECURITY PROTOCOL IN
                buffers.Spt.Cdb[1]             = 0xFE; // Samsung Protocol
                buffers.Spt.Cdb[3]             = 5;    // Identify
                buffers.Spt.Cdb[8]             = 2;    // Transfer Length (high)
                buffers.Spt.Cdb[9]             = 0;    // Transfer Length (low)
                buffers.Spt.DataIn             = (byte)Kernel32.SCSI_IOCTL_DATA.SCSI_IOCTL_DATA_IN;

                length = Marshal.SizeOf <Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS>();
                buffer = Marshal.AllocHGlobal(length);
                Marshal.StructureToPtr(buffers, buffer, false);

                validTransfer = Kernel32.DeviceIoControl(hDevice, Kernel32.IOCTL.IOCTL_SCSI_PASS_THROUGH, buffer, length, buffer, length, out _, IntPtr.Zero);
                if (validTransfer)
                {
                    IntPtr offset = Marshal.OffsetOf <Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS>(nameof(Kernel32.SCSI_PASS_THROUGH_WITH_BUFFERS.DataBuf));
                    IntPtr newPtr = IntPtr.Add(buffer, offset.ToInt32());
                    data = Marshal.PtrToStructure <Kernel32.NVME_IDENTIFY_CONTROLLER_DATA>(newPtr);
                    Marshal.FreeHGlobal(buffer);
                    result = true;
                }
                else
                {
                    Marshal.FreeHGlobal(buffer);
                }
            }

            return(result);
        }