Reworked FreeBSD device detection.

This commit is contained in:
2017-12-11 20:52:15 +00:00
parent 2a96394bc0
commit 45f6a56479
2 changed files with 115 additions and 199 deletions

View File

@@ -1,110 +1,58 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using DiscImageChef.Console;
using path_id_t = System.UInt32;
using target_id_t = System.UInt32;
using lun_id_t = System.UInt32;
using static DiscImageChef.Devices.FreeBSD.Extern;
namespace DiscImageChef.Devices.FreeBSD
{
public static class ListDevices
static class ListDevices
{
const string XPT_DEVICE = "/dev/xpt0";
const path_id_t CAM_XPT_PATH_ID = 0xFFFFFFFF;
const target_id_t CAM_TARGET_WILDCARD = 0xFFFFFFFF;
const lun_id_t CAM_LUN_WILDCARD = 0xFFFFFFFF;
const int ccb_size = 1240;
public static DeviceInfo[] GetList()
internal static DeviceInfo[] GetList()
{
int fd = Extern.open(XPT_DEVICE, FileFlags.ReadWrite);
// MMC support was added to CAM in FreeBSD 12
int not_mmc_data = Marshal.SizeOf(typeof(mmc_params));
if(Environment.OSVersion.Version.Major >= 12)
not_mmc_data = 0;
int dev_match_result_size = Marshal.SizeOf(typeof(dev_match_result)) - not_mmc_data;
if(fd == -1)
{
DicConsole.ErrorWriteLine("Error {0} opening {1}", Marshal.GetLastWin32Error(), XPT_DEVICE);
return null;
}
int bufsize = dev_match_result_size * 100;
ccb_dev_match cdm = new ccb_dev_match();
cdm.ccb_h = new ccb_hdr();
cdm.ccb_h.path_id = CAM_XPT_PATH_ID;
cdm.ccb_h.target_id = CAM_TARGET_WILDCARD;
cdm.ccb_h.target_lun = CAM_LUN_WILDCARD;
cdm.ccb_h.func_code = xpt_opcode.XPT_DEV_MATCH;
cdm.match_buf_len = (uint)bufsize;
cdm.matches = Marshal.AllocHGlobal(bufsize);
IntPtr ccb = Marshal.AllocHGlobal(ccb_size);
Marshal.StructureToPtr(cdm, ccb, false);
int res = Extern.ioctl(fd, FreebsdIoctl.CAMIOCOMMAND, ccb);
if(res == -1)
{
DicConsole.ErrorWriteLine("Error {0} sending ioctl to CAM", Marshal.GetLastWin32Error());
Extern.close(fd);
Marshal.FreeHGlobal(cdm.matches);
Marshal.FreeHGlobal(ccb);
return null;
}
cdm = (ccb_dev_match)Marshal.PtrToStructure(ccb, typeof(ccb_dev_match));
DicConsole.DebugWriteLine("FreeBSD devices", "CAM returned {0} matches", cdm.num_matches);
if(cdm.num_matches == 0)
return null;
dev_match_result[] matches = new dev_match_result[cdm.num_matches];
byte[] buffer = new byte[bufsize];
Marshal.Copy(cdm.matches, buffer, 0, bufsize);
string[] passDevices = Directory.GetFiles("/dev/", "pass*", SearchOption.TopDirectoryOnly);
List<DeviceInfo> listDevices = new List<DeviceInfo>();
foreach(string passDevice in passDevices)
{
DeviceInfo deviceInfo = new DeviceInfo();
bool pathFound = false;
bool skipDevice = false;
IntPtr dev = cam_open_device(passDevice, FileFlags.ReadWrite);
cam_device camDevice = (cam_device)Marshal.PtrToStructure(dev, typeof(cam_device));
for(int i = 0; i < matches.Length; i++)
{
dev_match_type matchType = (dev_match_type)BitConverter.ToUInt32(buffer, i * dev_match_result_size);
IntPtr ccbPtr = cam_getccb(dev);
if(matchType == dev_match_type.DEV_MATCH_DEVICE)
{
byte[] data = new byte[Marshal.SizeOf(typeof(device_match_result))];
Buffer.BlockCopy(buffer, (i * dev_match_result_size) + 4, data, 0,
Marshal.SizeOf(typeof(device_match_result)));
IntPtr matchPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(device_match_result)));
Marshal.Copy(data, 0, matchPtr, Marshal.SizeOf(typeof(device_match_result)));
device_match_result dmr =
(device_match_result)Marshal.PtrToStructure(matchPtr, typeof(device_match_result));
Marshal.FreeHGlobal(matchPtr);
if(ccbPtr.ToInt64() == 0) continue;
if(dmr.flags.HasFlag(dev_result_flags.DEV_RESULT_UNCONFIGURED))
ccb_getdev cgd = (ccb_getdev)Marshal.PtrToStructure(ccbPtr, typeof(ccb_getdev));
cgd.ccb_h.func_code = xpt_opcode.XPT_GDEV_TYPE;
Marshal.StructureToPtr(cgd, ccbPtr, false);
int error = cam_send_ccb(dev, ccbPtr);
if(error < 0)
{
skipDevice = true;
cam_freeccb(ccbPtr);
continue;
}
if(pathFound)
{
listDevices.Add(deviceInfo);
deviceInfo = new DeviceInfo();
pathFound = false;
}
cgd = (ccb_getdev)Marshal.PtrToStructure(ccbPtr, typeof(ccb_getdev));
System.Console.WriteLine("{0}", dmr.protocol);
cam_freeccb(ccbPtr);
cam_close_device(dev);
skipDevice = false;
switch(dmr.protocol)
string simName = StringHandlers.CToString(camDevice.sim_name);
deviceInfo.path = passDevice;
byte[] serialNumber = new byte[camDevice.serial_num_len];
Array.Copy(camDevice.serial_num, 0, serialNumber, 0, serialNumber.Length);
deviceInfo.serial = StringHandlers.CToString(serialNumber);
switch(cgd.protocol)
{
case cam_proto.PROTO_ATA:
case cam_proto.PROTO_ATAPI:
@@ -112,14 +60,14 @@ namespace DiscImageChef.Devices.FreeBSD
{
// Little-endian FreeBSD gives it resorted
// Big-endian FreeBSD, no idea
byte[] atad_tneid = new byte[512];
byte[] atadTneid = new byte[512];
for(int aIndex = 0; aIndex < 512; aIndex += 2)
{
atad_tneid[aIndex] = dmr.ident_data[aIndex + 1];
atad_tneid[aIndex + 1] = dmr.ident_data[aIndex];
atadTneid[aIndex] = cgd.ident_data[aIndex + 1];
atadTneid[aIndex + 1] = cgd.ident_data[aIndex];
}
Decoders.ATA.Identify.IdentifyDevice? idt = Decoders.ATA.Identify.Decode(atad_tneid);
Decoders.ATA.Identify.IdentifyDevice? idt = Decoders.ATA.Identify.Decode(atadTneid);
if(idt.HasValue)
{
string[] separated = idt.Value.Model.Split(' ');
@@ -136,23 +84,22 @@ namespace DiscImageChef.Devices.FreeBSD
}
deviceInfo.serial = idt.Value.SerialNumber;
deviceInfo.bus = "ATA";
deviceInfo.supported = false;
deviceInfo.bus = simName == "ahcich" ? "SATA" : "ATA";
deviceInfo.supported = simName != "ata";
}
if(dmr.protocol == cam_proto.PROTO_ATAPI)
goto case cam_proto.PROTO_SCSI;
if(cgd.protocol == cam_proto.PROTO_ATAPI) goto case cam_proto.PROTO_SCSI;
break;
}
case cam_proto.PROTO_SCSI:
{
Decoders.SCSI.Inquiry.SCSIInquiry? inq = Decoders.SCSI.Inquiry.Decode(dmr.inq_data);
Decoders.SCSI.Inquiry.SCSIInquiry? inq = Decoders.SCSI.Inquiry.Decode(cgd.inq_data);
if(inq.HasValue)
{
deviceInfo.vendor = StringHandlers.CToString(inq.Value.VendorIdentification).Trim();
deviceInfo.model = StringHandlers.CToString(inq.Value.ProductIdentification).Trim();
deviceInfo.bus = dmr.protocol == cam_proto.PROTO_ATAPI ? "ATAPI" : "SCSI";
deviceInfo.supported = false;
deviceInfo.bus = simName == "ata" || simName == "ahcich" ? "ATAPI" : "SCSI";
deviceInfo.supported = simName != "ata";
}
break;
}
@@ -161,66 +108,16 @@ namespace DiscImageChef.Devices.FreeBSD
deviceInfo.supported = false;
break;
case cam_proto.PROTO_MMCSD:
if(!ArrayHelpers.ArrayIsNullOrEmpty(dmr.mmc_ident_data.model))
deviceInfo.model = StringHandlers.CToString(dmr.mmc_ident_data.model);
else
deviceInfo.model = string.Format("{0} card",
dmr.mmc_ident_data.card_features.HasFlag(mmc_card_features.CARD_FEATURE_SDIO)
? "SDIO"
: "Unknown");
if(dmr.mmc_ident_data.card_features.HasFlag(mmc_card_features.CARD_FEATURE_SD20) ||
dmr.mmc_ident_data.card_features.HasFlag(mmc_card_features.CARD_FEATURE_SDHC) ||
dmr.mmc_ident_data.card_features.HasFlag(mmc_card_features.CARD_FEATURE_SDIO))
deviceInfo.bus = "SD";
else if(dmr.mmc_ident_data.card_features.HasFlag(mmc_card_features.CARD_FEATURE_MMC))
deviceInfo.bus = "MMC";
else
deviceInfo.model = "Unknown card";
deviceInfo.bus = "MMC/SD";
deviceInfo.supported = false;
break;
default:
skipDevice = true;
break;
}
}
else if(matchType == dev_match_type.DEV_MATCH_PERIPH)
{
if(skipDevice)
continue;
byte[] data = new byte[Marshal.SizeOf(typeof(periph_match_result))];
Buffer.BlockCopy(buffer, (i * dev_match_result_size) + 4, data, 0,
Marshal.SizeOf(typeof(periph_match_result)));
IntPtr matchPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(periph_match_result)));
Marshal.Copy(data, 0, matchPtr, Marshal.SizeOf(typeof(periph_match_result)));
periph_match_result pmr =
(periph_match_result)Marshal.PtrToStructure(matchPtr, typeof(periph_match_result));
Marshal.FreeHGlobal(matchPtr);
if(deviceInfo.path == null || StringHandlers.CToString(pmr.periph_name) == "pass")
{
deviceInfo.path = string.Format("/dev/{0}{1}", StringHandlers.CToString(pmr.periph_name),
pmr.unit_number);
pathFound = true;
}
}
}
if(pathFound)
listDevices.Add(deviceInfo);
}
Marshal.FreeHGlobal(cdm.matches);
Marshal.FreeHGlobal(ccb);
Extern.close(fd);
if(listDevices.Count > 0)
return listDevices.OrderBy(t => t.path).ToArray();
return null;
return listDevices.Count > 0 ? listDevices.OrderBy(t => t.path).ToArray() : null;
}
}
}

View File

@@ -768,5 +768,24 @@ namespace DiscImageChef.Devices.FreeBSD
/// </summary>
public int fd;
}
[StructLayout(LayoutKind.Sequential)]
struct ccb_getdev
{
public ccb_hdr ccb_h;
public cam_proto protocol;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public byte[] inq_data;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)] public byte[] ident_data;
/// <summary>
/// device serial number
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 252)] public byte[] serial_num;
public byte inq_flags;
/// <summary>
/// length of the serial number
/// </summary>
public byte serial_num_len;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public IntPtr[] padding;
}
}