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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2024-12-19 10:45:18 +00:00
|
|
|
// Copyright © 2011-2025 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;
|
2022-12-05 22:03:12 +00:00
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
using Aaru.Core.Graphics;
|
2020-02-27 00:33:26 +00:00
|
|
|
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;
|
2022-12-05 22:03:12 +00:00
|
|
|
readonly IMediaGraph _mediaGraph;
|
2023-10-03 22:57:50 +01:00
|
|
|
readonly MemoryStream _mhddFs;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
/// <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>
|
2023-10-05 02:46:32 +01:00
|
|
|
/// <param name="mediaGraphDimensions">Dimensions in pixel for a square that would contain the block map graph</param>
|
2023-10-03 22:57:50 +01:00
|
|
|
internal MhddLog(string outputFile, Device dev, ulong blocks, ulong blockSize, ulong blocksToRead, bool @private,
|
|
|
|
|
uint mediaGraphDimensions = 0)
|
2016-02-04 17:12:35 +00:00
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(dev == null || string.IsNullOrEmpty(outputFile)) return;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-12-05 22:03:12 +00:00
|
|
|
if(mediaGraphDimensions > 0)
|
|
|
|
|
_mediaGraph = new BlockMap((int)mediaGraphDimensions, (int)mediaGraphDimensions, blocks);
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_mhddFs = new MemoryStream();
|
|
|
|
|
_logFile = outputFile;
|
|
|
|
|
|
2024-05-01 17:18:37 +01:00
|
|
|
string mode = dev.Type switch
|
|
|
|
|
{
|
|
|
|
|
DeviceType.ATA or DeviceType.ATAPI => "MODE: IDE",
|
|
|
|
|
DeviceType.SCSI => "MODE: SCSI",
|
|
|
|
|
DeviceType.MMC => "MODE: MMC",
|
|
|
|
|
DeviceType.NVMe => "MODE: NVMe",
|
|
|
|
|
DeviceType.SecureDigital => "MODE: SD",
|
|
|
|
|
_ => "MODE: IDE"
|
|
|
|
|
};
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2023-10-03 22:57:50 +01:00
|
|
|
var device = $"DEVICE: {dev.Manufacturer} {dev.Model}";
|
|
|
|
|
var fw = $"F/W: {dev.FirmwareRevision}";
|
|
|
|
|
var sn = $"S/N: {(@private ? "" : dev.Serial)}";
|
|
|
|
|
var sectors = string.Format(new CultureInfo("en-US"), "SECTORS: {0:n0}", blocks);
|
|
|
|
|
var sectorSize = string.Format(new CultureInfo("en-US"), "SECTOR SIZE: {0:n0} bytes", blockSize);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2023-10-03 22:57:50 +01:00
|
|
|
var 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);
|
|
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
var 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
|
|
|
|
2023-10-03 22:57:50 +01:00
|
|
|
var 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);
|
2023-10-03 22:57:50 +01:00
|
|
|
_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);
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Logs a new read</summary>
|
|
|
|
|
/// <param name="sector">Starting sector</param>
|
|
|
|
|
/// <param name="duration">Duration in milliseconds</param>
|
2023-10-05 02:46:32 +01:00
|
|
|
/// <param name="length">How many sectors where read at once</param>
|
2022-12-05 22:03:12 +00:00
|
|
|
internal void Write(ulong sector, double duration, uint length = 1)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_logFile == null) return;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
byte[] sectorBytes = BitConverter.GetBytes(sector);
|
|
|
|
|
byte[] durationBytes = BitConverter.GetBytes((ulong)(duration * 1000));
|
|
|
|
|
|
2023-10-03 22:57:50 +01:00
|
|
|
_mhddFs.Write(sectorBytes, 0, 8);
|
2022-03-06 13:29:38 +00:00
|
|
|
_mhddFs.Write(durationBytes, 0, 8);
|
2022-12-05 22:03:12 +00:00
|
|
|
|
|
|
|
|
switch(duration)
|
|
|
|
|
{
|
2023-10-03 22:57:50 +01:00
|
|
|
case < 3:
|
|
|
|
|
_mediaGraph?.PaintSectors(sector, length, 0x00, 0xFF, 0x00);
|
2022-12-05 22:03:12 +00:00
|
|
|
|
|
|
|
|
break;
|
2023-10-03 22:57:50 +01:00
|
|
|
case >= 3 and < 10:
|
|
|
|
|
_mediaGraph?.PaintSectors(sector, length, 0x80, 0xFF, 0x00);
|
2022-12-05 22:03:12 +00:00
|
|
|
|
|
|
|
|
break;
|
2023-10-03 22:57:50 +01:00
|
|
|
case >= 10 and < 50:
|
|
|
|
|
_mediaGraph?.PaintSectors(sector, length, 0xFF, 0xFF, 0x00);
|
2022-12-05 22:03:12 +00:00
|
|
|
|
|
|
|
|
break;
|
2023-10-03 22:57:50 +01:00
|
|
|
case >= 50 and < 150:
|
|
|
|
|
_mediaGraph?.PaintSectors(sector, length, 0xFF, 0xAB, 0x00);
|
2022-12-05 22:03:12 +00:00
|
|
|
|
|
|
|
|
break;
|
2023-10-03 22:57:50 +01:00
|
|
|
case >= 150 and < 500:
|
|
|
|
|
_mediaGraph?.PaintSectors(sector, length, 0xFF, 0x56, 0x00);
|
2022-12-05 22:03:12 +00:00
|
|
|
|
|
|
|
|
break;
|
2023-10-03 22:57:50 +01:00
|
|
|
case >= 500:
|
|
|
|
|
_mediaGraph?.PaintSectors(sector, length, 0xFF, 0x00, 0x00);
|
2022-12-05 22:03:12 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Closes and writes to file the MHDD log</summary>
|
|
|
|
|
internal void Close()
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_logFile == null) return;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
var fs = new FileStream(_logFile, FileMode.Create);
|
|
|
|
|
_mhddFs.WriteTo(fs);
|
|
|
|
|
_mhddFs.Close();
|
|
|
|
|
fs.Close();
|
2022-12-05 22:03:12 +00:00
|
|
|
|
|
|
|
|
_mediaGraph?.WriteTo(Path.GetFileNameWithoutExtension(_logFile) + ".png");
|
2016-02-04 17:12:35 +00:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|