// /*************************************************************************** // The Disc Image Chef // ---------------------------------------------------------------------------- // // Filename : Structs.cs // Author(s) : Natalia Portillo // // Component : FreeBSD direct device access. // // --[ Description ] ---------------------------------------------------------- // // Contains [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] structures necessary for directly interfacing devices under // FreeBSD. // // --[ License ] -------------------------------------------------------------- // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, see . // // ---------------------------------------------------------------------------- // Copyright © 2011-2018 Natalia Portillo // ****************************************************************************/ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using lun_id_t = System.UInt32; using path_id_t = System.UInt32; using target_id_t = System.UInt32; // ReSharper disable BuiltInTypeReferenceStyle #pragma warning disable 649 #pragma warning disable 169 namespace DiscImageChef.Devices.FreeBSD { [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct AtaCmd { public CamAtaIoFlags flags; public byte command; public byte features; public byte lba_low; public byte lba_mid; public byte lba_high; public byte device; public byte lba_low_exp; public byte lba_mid_exp; public byte lba_high_exp; public byte features_exp; public byte sector_count; public byte sector_count_exp; public byte control; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct AtaRes { public CamAtaIoFlags flags; public byte status; public byte error; public byte lba_low; public byte lba_mid; public byte lba_high; public byte device; public byte lba_low_exp; public byte lba_mid_exp; public byte lba_high_exp; public byte sector_count; public byte sector_count_exp; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CamPinfo { public uint priority; public uint generation; public int index; } [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct ListEntry { /// /// LIST_ENTRY(ccb_hdr)=le->*le_next /// public IntPtr LeNext; /// /// LIST_ENTRY(ccb_hdr)=le->**le_prev /// public IntPtr LePrev; } [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct SlistEntry { /// /// SLIST_ENTRY(ccb_hdr)=sle->*sle_next /// public IntPtr SleNext; } [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct TailqEntry { /// /// TAILQ_ENTRY(ccb_hdr)=tqe->*tqe_next /// public IntPtr TqeNext; /// /// TAILQ_ENTRY(ccb_hdr)=tqe->**tqe_prev /// public IntPtr TqePrev; } [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct StailqEntry { /// /// STAILQ_ENTRY(ccb_hdr)=stqe->*stqe_next /// public IntPtr StqeNext; } [StructLayout(LayoutKind.Explicit)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CamqEntry { [FieldOffset(0)] public ListEntry le; [FieldOffset(0)] public SlistEntry sle; [FieldOffset(0)] public TailqEntry tqe; [FieldOffset(0)] public StailqEntry stqe; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct Timeval { public long tv_sec; /// long public IntPtr tv_usec; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CcbQosArea { public Timeval etime; public UIntPtr sim_data; public UIntPtr periph_data; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CcbHdr { public CamPinfo pinfo; public CamqEntry xpt_links; public CamqEntry sim_links; public CamqEntry periph_links; public uint retry_count; public IntPtr cbfcnp; public XptOpcode func_code; public CamStatus status; public IntPtr path; public uint path_id; public uint target_id; public ulong target_lun; public CcbFlags flags; public uint xflags; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public IntPtr[] periph_priv; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public IntPtr[] sim_priv; public CcbQosArea qos; public uint timeout; public Timeval softtimeout; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct ScsiSenseData { const int SSD_FULL_SIZE = 252; public byte error_code; [MarshalAs(UnmanagedType.ByValArray, SizeConst = SSD_FULL_SIZE - 1)] public byte[] sense_buf; } /// /// SCSI I/O Request CCB used for the XPT_SCSI_IO and XPT_CONT_TARGET_IO function codes. /// [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CcbScsiio { public CcbHdr ccb_h; /// Ptr for next CCB for action public IntPtr next_ccb; /// Ptr to mapping info public IntPtr req_map; /// Ptr to the data buf/SG list public IntPtr data_ptr; /// Data transfer length public uint dxfer_len; /// Autosense storage public ScsiSenseData sense_data; /// Number of bytes to autosense public byte sense_len; /// Number of bytes for the CDB public byte cdb_len; /// Number of SG list entries public short sglist_cnt; /// Returned SCSI status public byte scsi_status; /// Autosense resid length: 2's comp public sbyte sense_resid; /// Transfer residual length: 2's comp public int resid; /// /// Area for the CDB send, or pointer to the CDB bytes to send /// const int IOCDBLEN = 16; [MarshalAs(UnmanagedType.ByValArray, SizeConst = IOCDBLEN)] public byte[] cdb_bytes; /// Pointer to the message buffer public IntPtr msg_ptr; /// Number of bytes for the Message public short msg_len; /// /// What to do for tag queueing. The tag action should be either the define below (to send a non-tagged /// transaction) or one of the defined scsi tag messages from scsi_message.h. /// public byte tag_action; /// tag id from initator (target mode) public uint tag_id; /// initiator id of who selected public uint init_id; } /// /// SCSI I/O Request CCB used for the XPT_SCSI_IO and XPT_CONT_TARGET_IO function codes. /// [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CcbScsiio64 { public CcbHdr ccb_h; /// Ptr for next CCB for action public IntPtr next_ccb; /// Ptr to mapping info public IntPtr req_map; /// Ptr to the data buf/SG list public IntPtr data_ptr; /// Data transfer length public uint dxfer_len; /// Autosense storage public ScsiSenseData sense_data; /// Number of bytes to autosense public byte sense_len; /// Number of bytes for the CDB public byte cdb_len; /// Number of SG list entries public short sglist_cnt; /// Returned SCSI status public byte scsi_status; /// Autosense resid length: 2's comp public sbyte sense_resid; /// Transfer residual length: 2's comp public int resid; public uint alignment; /// /// Area for the CDB send, or pointer to the CDB bytes to send /// const int IOCDBLEN = 16; [MarshalAs(UnmanagedType.ByValArray, SizeConst = IOCDBLEN)] public byte[] cdb_bytes; /// Pointer to the message buffer public IntPtr msg_ptr; /// Number of bytes for the Message public short msg_len; /// /// What to do for tag queueing. The tag action should be either the define below (to send a non-tagged /// transaction) or one of the defined scsi tag messages from scsi_message.h. /// public byte tag_action; /// tag id from initator (target mode) public uint tag_id; /// initiator id of who selected public uint init_id; } /// /// ATA I/O Request CCB used for the XPT_ATA_IO function code. /// [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CcbAtaio { public CcbHdr ccb_h; /// Ptr for next CCB for action public IntPtr next_ccb; /// ATA command register set public AtaCmd cmd; /// ATA result register set public AtaRes res; /// Ptr to the data buf/SG list public IntPtr data_ptr; /// Data transfer length public uint dxfer_len; /// Transfer residual length: 2's comp public int resid; /// Flags for the rest of the buffer public byte ata_flags; public uint aux; public uint unused; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct NvmeCommand { ushort opc_fuse_rsvd1; /// /// command identifier /// public ushort cid; /// /// namespace identifier /// public uint nsid; /// /// reserved /// public uint rsvd2; /// /// reserved /// public uint rsvd3; /// /// metadata pointer /// public ulong mptr; /// /// prp entry 1 /// public ulong prp1; /// /// prp entry 2 /// public ulong prp2; /// /// command-specific /// public uint cdw10; /// /// command-specific /// public uint cdw11; /// /// command-specific /// public uint cdw12; /// /// command-specific /// public uint cdw13; /// /// command-specific /// public uint cdw14; /// /// command-specific /// public uint cdw15; /// /// opcode /// public byte Opc => (byte)((opc_fuse_rsvd1 & 0xFF00) >> 8); /// /// fused operation /// public byte Fuse => (byte)((opc_fuse_rsvd1 & 0xC0) >> 6); /// /// reserved /// public byte Rsvd1 => (byte)(opc_fuse_rsvd1 & 0x3F); } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct NvmeStatus { ushort status; /// /// phase tag /// public byte P => (byte)((status & 0x8000) >> 15); /// /// status code /// public byte Sc => (byte)((status & 0x7F80) >> 7); /// /// status code type /// public byte Sct => (byte)((status & 0x70) >> 4); /// /// reserved /// public byte Rsvd2 => (byte)((status & 0xC) >> 15); /// /// more /// public byte M => (byte)((status & 0x2) >> 1); /// /// do not retry /// public byte Dnr => (byte)(status & 0x1); } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct NvmeCompletion { /// /// command-specific /// public uint cdw0; /// /// reserved /// public uint rsvd1; /// /// submission queue head pointer /// public ushort sqhd; /// /// submission queue identifier /// public ushort sqid; /// /// command identifier /// public ushort cid; public NvmeStatus status; } /// /// NVMe I/O Request CCB used for the XPT_NVME_IO and XPT_NVME_ADMIN function codes. /// [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CcbNvmeio { public CcbHdr ccb_h; /// Ptr for next CCB for action public IntPtr next_ccb; /// NVME command, per NVME standard public NvmeCommand cmd; /// NVME completion, per NVME standard public NvmeCompletion cpl; /// Ptr to the data buf/SG list public IntPtr data_ptr; /// Data transfer length public uint dxfer_len; /// Number of SG list entries public ushort sglist_cnt; /// padding for removed uint32_t public ushort unused; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct PeriphMatchPattern { const int DEV_IDLEN = 16; [MarshalAs(UnmanagedType.ByValArray, SizeConst = DEV_IDLEN)] public byte[] periph_name; public uint unit_number; public path_id_t path_id; public target_id_t target_id; public lun_id_t target_lun; public PeriphPatternFlags flags; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct DeviceIdMatchPattern { public byte id_len; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public byte[] id; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct ScsiStaticInquiryPattern { const int SID_VENDOR_SIZE = 8; const int SID_PRODUCT_SIZE = 16; const int SID_REVISION_SIZE = 4; public byte type; public byte media_type; [MarshalAs(UnmanagedType.ByValArray, SizeConst = SID_VENDOR_SIZE + 1)] public byte[] vendor; [MarshalAs(UnmanagedType.ByValArray, SizeConst = SID_PRODUCT_SIZE + 1)] public byte[] product; [MarshalAs(UnmanagedType.ByValArray, SizeConst = SID_REVISION_SIZE + 1)] public byte[] revision; } [StructLayout(LayoutKind.Explicit)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct DeviceMatchPatternData { [FieldOffset(0)] public ScsiStaticInquiryPattern inq_pat; [FieldOffset(0)] public DeviceIdMatchPattern devid_pat; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct DeviceMatchPattern { public uint path_id; public uint target_id; public uint target_lun; public DevPatternFlags flags; public DeviceMatchPatternData data; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct BusMatchPattern { const int DEV_IDLEN = 16; public path_id_t path_id; [MarshalAs(UnmanagedType.ByValArray, SizeConst = DEV_IDLEN)] public byte[] dev_name; public uint unit_number; public uint bus_id; BusPatternFlags flags; } [StructLayout(LayoutKind.Explicit)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct MatchPattern { [FieldOffset(0)] public PeriphMatchPattern periph_pattern; [FieldOffset(0)] public DeviceMatchPattern device_pattern; [FieldOffset(0)] public BusMatchPattern bus_pattern; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct DevMatchPattern { public DevMatchType type; public MatchPattern pattern; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct PeriphMatchResult { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] periph_name; public uint unit_number; public path_id_t path_id; public target_id_t target_id; public lun_id_t target_lun; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct MmcCid { public uint mid; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] pnm; public uint psn; public ushort oid; public ushort mdt_year; public byte mdt_month; public byte prv; public byte fwrev; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct MmcParams { /// /// Card model /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)] public byte[] model; /// /// Card OCR /// public uint card_ocr; /// /// OCR of the IO portion of the card /// public uint io_ocr; /// /// Card CID -- raw /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public uint[] card_cid; /// /// Card CID -- parsed /// public MmcCid cid; /// /// Card CSD -- raw /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public uint[] card_csd; /// /// Card RCA /// public ushort card_rca; /// /// What kind of card is it /// public MmcCardFeatures card_features; public byte sdio_func_count; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct DeviceMatchResult { public path_id_t path_id; public target_id_t target_id; public lun_id_t target_lun; public CamProto protocol; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public byte[] inq_data; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)] public byte[] ident_data; public DevResultFlags flags; public MmcParams mmc_ident_data; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct BusMatchResult { public path_id_t path_id; const int DEV_IDLEN = 16; [MarshalAs(UnmanagedType.ByValArray, SizeConst = DEV_IDLEN)] public byte[] dev_name; public uint unit_number; public uint bus_id; } [StructLayout(LayoutKind.Explicit)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct MatchResult { [FieldOffset(0)] public PeriphMatchResult periph_result; [FieldOffset(0)] public DeviceMatchResult device_result; [FieldOffset(0)] public BusMatchResult bus_result; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct DevMatchResult { public DevMatchType type; public MatchResult result; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CcbDmCookie { public IntPtr bus; public IntPtr target; public IntPtr device; public IntPtr periph; public IntPtr pdrv; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CcbDevPosition { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public CamGenerations[] generations; DevPosType position_type; public CcbDmCookie cookie; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CcbDevMatch { public CcbHdr ccb_h; CcbDevMatchStatus status; public uint num_patterns; public uint pattern_buf_len; /// /// dev_match_pattern* /// public IntPtr patterns; public uint num_matches; public uint match_buf_len; /// /// dev_match_result* /// public IntPtr matches; public CcbDevPosition pos; } [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CamDevice { const int MAXPATHLEN = 1024; const int DEV_IDLEN = 16; const int SIM_IDLEN = 16; /// /// Pathname of the device given by the user. This may be null if the user states the device name and unit number /// separately. /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAXPATHLEN)] public byte[] DevicePath; /// /// Device name given by the user. /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = DEV_IDLEN + 1)] public byte[] GivenDevName; /// /// Unit number given by the user. /// public uint GivenUnitNumber; /// /// Name of the device, e.g. 'pass' /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = DEV_IDLEN + 1)] public byte[] DeviceName; /// /// Unit number of the passthrough device associated with this particular device. /// public uint DevUnitNum; /// /// Controller name, e.g. 'ahc' /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = SIM_IDLEN + 1)] public byte[] SimName; /// /// Controller unit number /// public uint SimUnitNumber; /// /// Controller bus number /// public uint BusId; /// /// Logical Unit Number /// public lun_id_t TargetLun; /// /// Target ID /// public target_id_t TargetId; /// /// System SCSI bus number /// public path_id_t PathId; /// /// type of peripheral device /// public ushort PdType; /// /// SCSI Inquiry data /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public byte[] InqData; /// /// device serial number /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 252)] public byte[] SerialNum; /// /// length of the serial number /// public byte SerialNumLen; /// /// Negotiated sync period /// public byte SyncPeriod; /// /// Negotiated sync offset /// public byte SyncOffset; /// /// Negotiated bus width /// public byte BusWidth; /// /// file descriptor for device /// public int Fd; } [StructLayout(LayoutKind.Sequential)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] struct CcbGetdev { public CcbHdr ccb_h; public CamProto protocol; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public byte[] inq_data; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)] public byte[] ident_data; /// /// device serial number /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 252)] public byte[] serial_num; public byte inq_flags; /// /// length of the serial number /// public byte serial_num_len; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public IntPtr[] padding; } }