Files
Aaru.Server/DiscImageChef.Devices/FreeBSD/ListDevices.cs

151 lines
6.3 KiB
C#
Raw Normal View History

// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : ListDevices.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : FreeBSD direct device access.
//
// --[ Description ] ----------------------------------------------------------
//
// Gets a list of known devices.
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
2017-12-11 20:52:15 +00:00
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
2017-12-11 20:52:15 +00:00
using static DiscImageChef.Devices.FreeBSD.Extern;
namespace DiscImageChef.Devices.FreeBSD
{
2017-12-11 20:52:15 +00:00
static class ListDevices
{
2017-12-11 20:52:15 +00:00
internal static DeviceInfo[] GetList()
{
2017-12-11 20:52:15 +00:00
string[] passDevices = Directory.GetFiles("/dev/", "pass*", SearchOption.TopDirectoryOnly);
List<DeviceInfo> listDevices = new List<DeviceInfo>();
2017-12-11 20:52:15 +00:00
foreach(string passDevice in passDevices)
{
2017-12-11 20:52:15 +00:00
DeviceInfo deviceInfo = new DeviceInfo();
IntPtr dev = cam_open_device(passDevice, FileFlags.ReadWrite);
cam_device camDevice = (cam_device)Marshal.PtrToStructure(dev, typeof(cam_device));
2017-12-11 20:52:15 +00:00
IntPtr ccbPtr = cam_getccb(dev);
2017-12-11 20:52:15 +00:00
if(ccbPtr.ToInt64() == 0) continue;
2017-12-11 20:52:15 +00:00
ccb_getdev cgd = (ccb_getdev)Marshal.PtrToStructure(ccbPtr, typeof(ccb_getdev));
2017-12-11 20:52:15 +00:00
cgd.ccb_h.func_code = xpt_opcode.XPT_GDEV_TYPE;
2017-12-11 20:52:15 +00:00
Marshal.StructureToPtr(cgd, ccbPtr, false);
2017-12-11 20:52:15 +00:00
int error = cam_send_ccb(dev, ccbPtr);
2017-12-11 20:52:15 +00:00
if(error < 0)
{
2017-12-11 20:52:15 +00:00
cam_freeccb(ccbPtr);
continue;
}
2017-12-11 20:52:15 +00:00
cgd = (ccb_getdev)Marshal.PtrToStructure(ccbPtr, typeof(ccb_getdev));
2017-12-11 20:52:15 +00:00
cam_freeccb(ccbPtr);
cam_close_device(dev);
2017-12-11 20:52:15 +00:00
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:
case cam_proto.PROTO_SATAPM:
{
2017-12-11 20:52:15 +00:00
// Little-endian FreeBSD gives it resorted
// Big-endian FreeBSD, no idea
byte[] atadTneid = new byte[512];
for(int aIndex = 0; aIndex < 512; aIndex += 2)
{
2017-12-11 20:52:15 +00:00
atadTneid[aIndex] = cgd.ident_data[aIndex + 1];
atadTneid[aIndex + 1] = cgd.ident_data[aIndex];
}
2017-12-11 20:52:15 +00:00
Decoders.ATA.Identify.IdentifyDevice? idt = Decoders.ATA.Identify.Decode(atadTneid);
if(idt.HasValue)
{
2017-12-11 20:52:15 +00:00
string[] separated = idt.Value.Model.Split(' ');
2017-12-11 20:52:15 +00:00
if(separated.Length == 1)
{
2017-12-11 20:52:15 +00:00
deviceInfo.vendor = "ATA";
deviceInfo.model = separated[0];
}
else
2017-12-11 20:52:15 +00:00
{
deviceInfo.vendor = separated[0];
deviceInfo.model = separated[separated.Length - 1];
}
2017-12-11 20:52:15 +00:00
deviceInfo.serial = idt.Value.SerialNumber;
deviceInfo.bus = simName == "ahcich" ? "SATA" : "ATA";
deviceInfo.supported = simName != "ata";
}
if(cgd.protocol == cam_proto.PROTO_ATAPI) goto case cam_proto.PROTO_SCSI;
break;
}
2017-12-11 20:52:15 +00:00
case cam_proto.PROTO_SCSI:
{
2017-12-11 20:52:15 +00:00
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 = simName == "ata" || simName == "ahcich" ? "ATAPI" : "SCSI";
deviceInfo.supported = simName != "ata";
}
break;
}
2017-12-11 20:52:15 +00:00
case cam_proto.PROTO_NVME:
deviceInfo.bus = "NVMe";
deviceInfo.supported = false;
break;
case cam_proto.PROTO_MMCSD:
deviceInfo.model = "Unknown card";
deviceInfo.bus = "MMC/SD";
deviceInfo.supported = false;
break;
}
listDevices.Add(deviceInfo);
2017-12-11 20:52:15 +00:00
}
2017-12-11 20:52:15 +00:00
return listDevices.Count > 0 ? listDevices.OrderBy(t => t.path).ToArray() : null;
}
}
}