2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-02-04 17:12:35 +00:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : MHDDLog.cs
|
2016-07-28 18:13:49 +01:00
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2016-02-04 17:12:35 +00:00
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Component : Core algorithms.
|
2016-02-04 17:12:35 +00:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Glue logic to create a binary media scan log in MHDD's format.
|
2016-02-04 17:12:35 +00:00
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as
|
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program 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 General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-02-04 17:12:35 +00:00
|
|
|
// ****************************************************************************/
|
2016-07-28 18:13:49 +01:00
|
|
|
|
2016-02-04 17:12:35 +00:00
|
|
|
using System;
|
2017-12-21 14:30:38 +00:00
|
|
|
using System.Globalization;
|
2016-02-04 17:12:35 +00:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using DiscImageChef.Devices;
|
|
|
|
|
|
2017-05-27 15:54:03 +01:00
|
|
|
namespace DiscImageChef.Core.Logging
|
2016-02-04 17:12:35 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
class MhddLog
|
2016-02-04 17:12:35 +00:00
|
|
|
{
|
2017-12-22 21:46:38 +00:00
|
|
|
MemoryStream mhddFs;
|
|
|
|
|
string logFile;
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
internal MhddLog(string outputFile, Device dev, ulong blocks, ulong blockSize, ulong blocksToRead)
|
2016-02-04 17:12:35 +00:00
|
|
|
{
|
2017-12-21 06:06:19 +00:00
|
|
|
if(dev == null || string.IsNullOrEmpty(outputFile)) return;
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2017-12-22 21:46:38 +00:00
|
|
|
mhddFs = new MemoryStream();
|
|
|
|
|
logFile = outputFile;
|
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
string mode;
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
switch(dev.Type)
|
|
|
|
|
{
|
|
|
|
|
case DeviceType.ATA:
|
|
|
|
|
case DeviceType.ATAPI:
|
|
|
|
|
mode = "MODE: IDE";
|
|
|
|
|
break;
|
|
|
|
|
case DeviceType.SCSI:
|
|
|
|
|
mode = "MODE: SCSI";
|
|
|
|
|
break;
|
|
|
|
|
case DeviceType.MMC:
|
|
|
|
|
mode = "MODE: MMC";
|
|
|
|
|
break;
|
|
|
|
|
case DeviceType.NVMe:
|
|
|
|
|
mode = "MODE: NVMe";
|
|
|
|
|
break;
|
|
|
|
|
case DeviceType.SecureDigital:
|
|
|
|
|
mode = "MODE: SD";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
mode = "MODE: IDE";
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2017-12-21 23:00:30 +00:00
|
|
|
string device = $"DEVICE: {dev.Manufacturer} {dev.Model}";
|
|
|
|
|
string fw = $"F/W: {dev.Revision}";
|
|
|
|
|
string sn = $"S/N: {dev.Serial}";
|
|
|
|
|
string sectors = string.Format(new CultureInfo("en-US"), "SECTORS: {0:n0}", blocks);
|
|
|
|
|
string sectorsize = string.Format(new CultureInfo("en-US"), "SECTOR SIZE: {0:n0} bytes",
|
|
|
|
|
blockSize);
|
|
|
|
|
string scanblocksize = string.Format(new CultureInfo("en-US"),
|
|
|
|
|
"SCAN BLOCK SIZE: {0:n0} sectors", blocksToRead);
|
|
|
|
|
const string MHDD_VER = "VER:2 ";
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
byte[] deviceBytes = Encoding.ASCII.GetBytes(device);
|
|
|
|
|
byte[] modeBytes = Encoding.ASCII.GetBytes(mode);
|
|
|
|
|
byte[] fwBytes = Encoding.ASCII.GetBytes(fw);
|
|
|
|
|
byte[] snBytes = Encoding.ASCII.GetBytes(sn);
|
|
|
|
|
byte[] sectorsBytes = Encoding.ASCII.GetBytes(sectors);
|
|
|
|
|
byte[] sectorsizeBytes = Encoding.ASCII.GetBytes(sectorsize);
|
|
|
|
|
byte[] scanblocksizeBytes = Encoding.ASCII.GetBytes(scanblocksize);
|
2017-12-21 23:00:30 +00:00
|
|
|
byte[] verBytes = Encoding.ASCII.GetBytes(MHDD_VER);
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
uint pointer = (uint)(deviceBytes.Length + modeBytes.Length + fwBytes.Length + snBytes.Length +
|
|
|
|
|
sectorsBytes.Length + sectorsizeBytes.Length + scanblocksizeBytes.Length +
|
|
|
|
|
verBytes.Length + 2 * 9 + // New lines
|
|
|
|
|
4); // Pointer
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
byte[] newLine = new byte[2];
|
|
|
|
|
newLine[0] = 0x0D;
|
|
|
|
|
newLine[1] = 0x0A;
|
|
|
|
|
|
|
|
|
|
mhddFs.Write(BitConverter.GetBytes(pointer), 0, 4);
|
|
|
|
|
mhddFs.Write(newLine, 0, 2);
|
|
|
|
|
mhddFs.Write(verBytes, 0, verBytes.Length);
|
|
|
|
|
mhddFs.Write(newLine, 0, 2);
|
|
|
|
|
mhddFs.Write(modeBytes, 0, modeBytes.Length);
|
|
|
|
|
mhddFs.Write(newLine, 0, 2);
|
|
|
|
|
mhddFs.Write(deviceBytes, 0, deviceBytes.Length);
|
|
|
|
|
mhddFs.Write(newLine, 0, 2);
|
|
|
|
|
mhddFs.Write(fwBytes, 0, fwBytes.Length);
|
|
|
|
|
mhddFs.Write(newLine, 0, 2);
|
|
|
|
|
mhddFs.Write(snBytes, 0, snBytes.Length);
|
|
|
|
|
mhddFs.Write(newLine, 0, 2);
|
|
|
|
|
mhddFs.Write(sectorsBytes, 0, sectorsBytes.Length);
|
|
|
|
|
mhddFs.Write(newLine, 0, 2);
|
|
|
|
|
mhddFs.Write(sectorsizeBytes, 0, sectorsizeBytes.Length);
|
|
|
|
|
mhddFs.Write(newLine, 0, 2);
|
|
|
|
|
mhddFs.Write(scanblocksizeBytes, 0, scanblocksizeBytes.Length);
|
|
|
|
|
mhddFs.Write(newLine, 0, 2);
|
2016-02-04 17:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-20 02:08:37 +00:00
|
|
|
internal void Write(ulong sector, double duration)
|
2016-02-04 17:12:35 +00:00
|
|
|
{
|
2017-12-22 21:46:38 +00:00
|
|
|
if(logFile == null) return;
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
byte[] sectorBytes = BitConverter.GetBytes(sector);
|
|
|
|
|
byte[] durationBytes = BitConverter.GetBytes((ulong)(duration * 1000));
|
|
|
|
|
|
|
|
|
|
mhddFs.Write(sectorBytes, 0, 8);
|
|
|
|
|
mhddFs.Write(durationBytes, 0, 8);
|
2016-02-04 17:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-20 02:08:37 +00:00
|
|
|
internal void Close()
|
2016-02-04 17:12:35 +00:00
|
|
|
{
|
2017-12-22 21:46:38 +00:00
|
|
|
if(logFile == null) return;
|
|
|
|
|
|
|
|
|
|
FileStream fs = new FileStream(logFile, FileMode.Create);
|
|
|
|
|
mhddFs.WriteTo(fs);
|
|
|
|
|
mhddFs.Close();
|
|
|
|
|
fs.Close();
|
2016-02-04 17:12:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|