2017-08-21 21:04:44 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : ListDevices.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Linux direct device access.
|
2017-08-21 21:04:44 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Gets a list of known physical devices.
|
2017-08-21 21:04:44 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2017-08-21 21:04:44 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 19:33:46 +00:00
|
|
|
|
|
2017-08-22 03:38:02 +01:00
|
|
|
|
using System;
|
2017-08-21 21:04:44 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Devices.Linux
|
|
|
|
|
|
{
|
2017-12-20 02:08:37 +00:00
|
|
|
|
static class ListDevices
|
2017-08-21 21:04:44 +01:00
|
|
|
|
{
|
|
|
|
|
|
const string PATH_SYS_DEVBLOCK = "/sys/block/";
|
|
|
|
|
|
|
2017-12-23 02:32:02 +00:00
|
|
|
|
/// <summary>
|
2017-12-23 20:04:36 +00:00
|
|
|
|
/// Gets a list of all known storage devices on Linux
|
2017-12-23 02:32:02 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>List of devices</returns>
|
2017-12-20 02:08:37 +00:00
|
|
|
|
internal static DeviceInfo[] GetList()
|
2017-08-21 21:04:44 +01:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
string[] sysdevs =
|
|
|
|
|
|
Directory.GetFileSystemEntries(PATH_SYS_DEVBLOCK, "*", SearchOption.TopDirectoryOnly);
|
2017-08-21 21:04:44 +01:00
|
|
|
|
DeviceInfo[] devices = new DeviceInfo[sysdevs.Length];
|
2018-06-22 08:08:38 +01:00
|
|
|
|
bool hasUdev;
|
2017-08-21 21:04:44 +01:00
|
|
|
|
|
|
|
|
|
|
IntPtr udev = IntPtr.Zero;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
udev = Extern.udev_new();
|
2017-08-21 21:04:44 +01:00
|
|
|
|
hasUdev = udev != IntPtr.Zero;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
catch { hasUdev = false; }
|
2017-08-21 21:04:44 +01:00
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < sysdevs.Length; i++)
|
|
|
|
|
|
{
|
2017-12-22 03:13:43 +00:00
|
|
|
|
devices[i] = new DeviceInfo {Path = "/dev/" + Path.GetFileName(sysdevs[i])};
|
2017-08-21 21:04:44 +01:00
|
|
|
|
|
|
|
|
|
|
if(hasUdev)
|
|
|
|
|
|
{
|
2017-12-23 20:04:36 +00:00
|
|
|
|
IntPtr udevDev =
|
|
|
|
|
|
Extern.udev_device_new_from_subsystem_sysname(udev, "block", Path.GetFileName(sysdevs[i]));
|
2017-12-20 17:15:26 +00:00
|
|
|
|
devices[i].Vendor = Extern.udev_device_get_property_value(udevDev, "ID_VENDOR");
|
2018-06-22 08:08:38 +01:00
|
|
|
|
devices[i].Model = Extern.udev_device_get_property_value(udevDev, "ID_MODEL");
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(!string.IsNullOrEmpty(devices[i].Model)) devices[i].Model = devices[i].Model.Replace('_', ' ');
|
|
|
|
|
|
devices[i].Serial = Extern.udev_device_get_property_value(udevDev, "ID_SCSI_SERIAL");
|
|
|
|
|
|
if(string.IsNullOrEmpty(devices[i].Serial))
|
|
|
|
|
|
devices[i].Serial = Extern.udev_device_get_property_value(udevDev, "ID_SERIAL_SHORT");
|
|
|
|
|
|
devices[i].Bus = Extern.udev_device_get_property_value(udevDev, "ID_BUS");
|
2017-08-21 21:04:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-22 03:13:43 +00:00
|
|
|
|
StreamReader sr;
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(File.Exists(Path.Combine(sysdevs[i], "device/vendor")) && string.IsNullOrEmpty(devices[i].Vendor))
|
2017-08-21 21:04:44 +01:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sr = new StreamReader(Path.Combine(sysdevs[i], "device/vendor"), Encoding.ASCII);
|
2017-12-22 03:13:43 +00:00
|
|
|
|
devices[i].Vendor = sr.ReadLine()?.Trim();
|
2017-08-21 21:04:44 +01:00
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
|
else if(devices[i].Path.StartsWith("/dev/loop", StringComparison.CurrentCulture))
|
|
|
|
|
|
devices[i].Vendor = "Linux";
|
2017-08-21 21:04:44 +01:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(File.Exists(Path.Combine(sysdevs[i], "device/model")) &&
|
2017-12-20 17:15:26 +00:00
|
|
|
|
(string.IsNullOrEmpty(devices[i].Model) || devices[i].Bus == "ata"))
|
2017-08-21 21:04:44 +01:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sr = new StreamReader(Path.Combine(sysdevs[i], "device/model"), Encoding.ASCII);
|
2017-12-22 03:13:43 +00:00
|
|
|
|
devices[i].Model = sr.ReadLine()?.Trim();
|
2017-08-21 21:04:44 +01:00
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
|
else if(devices[i].Path.StartsWith("/dev/loop", StringComparison.CurrentCulture))
|
|
|
|
|
|
devices[i].Model = "Linux";
|
2017-08-21 21:04:44 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(File.Exists(Path.Combine(sysdevs[i], "device/serial")) && string.IsNullOrEmpty(devices[i].Serial))
|
2017-08-21 21:04:44 +01:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sr = new StreamReader(Path.Combine(sysdevs[i], "device/serial"), Encoding.ASCII);
|
2017-12-22 03:13:43 +00:00
|
|
|
|
devices[i].Serial = sr.ReadLine()?.Trim();
|
2017-08-21 21:04:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(string.IsNullOrEmpty(devices[i].Vendor) || devices[i].Vendor == "ATA")
|
|
|
|
|
|
if(devices[i].Model != null)
|
2017-08-21 21:04:44 +01:00
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
|
string[] pieces = devices[i].Model.Split(' ');
|
2017-09-11 06:03:11 +01:00
|
|
|
|
if(pieces.Length > 1)
|
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
|
devices[i].Vendor = pieces[0];
|
2018-06-22 08:08:38 +01:00
|
|
|
|
devices[i].Model = devices[i].Model.Substring(pieces[0].Length + 1);
|
2017-09-11 06:03:11 +01:00
|
|
|
|
}
|
2017-08-21 21:04:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Get better device type from sysfs paths
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(string.IsNullOrEmpty(devices[i].Bus))
|
2017-08-21 21:04:44 +01:00
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(devices[i].Path.StartsWith("/dev/loop", StringComparison.CurrentCulture))
|
|
|
|
|
|
devices[i].Bus = "loop";
|
|
|
|
|
|
else if(devices[i].Path.StartsWith("/dev/nvme", StringComparison.CurrentCulture))
|
|
|
|
|
|
devices[i].Bus = "NVMe";
|
|
|
|
|
|
else if(devices[i].Path.StartsWith("/dev/mmc", StringComparison.CurrentCulture))
|
|
|
|
|
|
devices[i].Bus = "MMC/SD";
|
2017-08-21 21:04:44 +01:00
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
|
else devices[i].Bus = devices[i].Bus.ToUpper();
|
2017-08-21 21:04:44 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
switch(devices[i].Bus)
|
2017-08-21 21:04:44 +01:00
|
|
|
|
{
|
|
|
|
|
|
case "ATA":
|
|
|
|
|
|
case "ATAPI":
|
|
|
|
|
|
case "SCSI":
|
|
|
|
|
|
case "USB":
|
|
|
|
|
|
case "PCMCIA":
|
|
|
|
|
|
case "FireWire":
|
2017-09-29 06:45:20 +01:00
|
|
|
|
case "MMC/SD":
|
2017-12-20 17:15:26 +00:00
|
|
|
|
devices[i].Supported = true;
|
2017-08-21 21:04:44 +01:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return devices;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|