2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-02-04 17:12:35 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-12-03 16:07:10 +00:00
|
|
|
// Copyright © 2011-2023 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;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
using Aaru.Devices;
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
namespace Aaru.Core.Logging;
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <summary>Implements a log in the format used by MHDD</summary>
|
2022-03-07 07:36:44 +00:00
|
|
|
sealed class MhddLog
|
2016-02-04 17:12:35 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
const string MHDD_VER = "VER:2 ";
|
|
|
|
|
readonly string _logFile;
|
|
|
|
|
readonly MemoryStream _mhddFs;
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes the MHDD log</summary>
|
|
|
|
|
/// <param name="outputFile">Log file</param>
|
|
|
|
|
/// <param name="dev">Device</param>
|
|
|
|
|
/// <param name="blocks">Blocks in media</param>
|
|
|
|
|
/// <param name="blockSize">Bytes per block</param>
|
|
|
|
|
/// <param name="blocksToRead">How many blocks read at once</param>
|
|
|
|
|
/// <param name="private">Disable saving paths or serial numbers in log</param>
|
2022-03-07 07:36:44 +00:00
|
|
|
internal MhddLog(string outputFile, Device dev, ulong blocks, ulong blockSize, ulong blocksToRead, bool @private)
|
2016-02-04 17:12:35 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
if(dev == null ||
|
|
|
|
|
string.IsNullOrEmpty(outputFile))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_mhddFs = new MemoryStream();
|
|
|
|
|
_logFile = outputFile;
|
|
|
|
|
|
|
|
|
|
string mode;
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
switch(dev.Type)
|
2016-02-04 17:12:35 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
case DeviceType.ATA:
|
|
|
|
|
case DeviceType.ATAPI:
|
|
|
|
|
mode = "MODE: IDE";
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case DeviceType.SCSI:
|
|
|
|
|
mode = "MODE: SCSI";
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case DeviceType.MMC:
|
|
|
|
|
mode = "MODE: MMC";
|
2016-02-04 17:12:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case DeviceType.NVMe:
|
|
|
|
|
mode = "MODE: NVMe";
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case DeviceType.SecureDigital:
|
|
|
|
|
mode = "MODE: SD";
|
2017-12-22 21:46:38 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
mode = "MODE: IDE";
|
|
|
|
|
|
|
|
|
|
break;
|
2016-02-04 17:12:35 +00:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
string device = $"DEVICE: {dev.Manufacturer} {dev.Model}";
|
|
|
|
|
string fw = $"F/W: {dev.FirmwareRevision}";
|
|
|
|
|
string sn = $"S/N: {(@private ? "" : 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);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
string scanBlockSize = string.Format(new CultureInfo("en-US"), "SCAN BLOCK SIZE: {0:n0} sectors", blocksToRead);
|
2022-03-06 13:29:38 +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);
|
|
|
|
|
byte[] verBytes = Encoding.ASCII.GetBytes(MHDD_VER);
|
|
|
|
|
|
2022-11-15 15:58:43 +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
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
byte[] newLine = new byte[2];
|
2022-03-06 13:29:38 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Logs a new read</summary>
|
|
|
|
|
/// <param name="sector">Starting sector</param>
|
|
|
|
|
/// <param name="duration">Duration in milliseconds</param>
|
|
|
|
|
internal void Write(ulong sector, double duration)
|
|
|
|
|
{
|
|
|
|
|
if(_logFile == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
byte[] sectorBytes = BitConverter.GetBytes(sector);
|
|
|
|
|
byte[] durationBytes = BitConverter.GetBytes((ulong)(duration * 1000));
|
|
|
|
|
|
|
|
|
|
_mhddFs.Write(sectorBytes, 0, 8);
|
|
|
|
|
_mhddFs.Write(durationBytes, 0, 8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Closes and writes to file the MHDD log</summary>
|
|
|
|
|
internal void Close()
|
|
|
|
|
{
|
|
|
|
|
if(_logFile == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var 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
|
|
|
}
|