Files
Aaru/Aaru.Images/VMware/Write.cs

382 lines
12 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Write.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Writes VMware disk images.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Structs;
using Schemas;
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages
{
2020-07-22 13:20:25 +01:00
public sealed partial class VMware
{
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
2020-02-29 18:03:35 +00:00
uint sectorSize)
{
if(options != null)
{
2020-07-20 21:11:32 +01:00
if(options.TryGetValue("adapter", out _adapterType))
switch(_adapterType.ToLowerInvariant())
{
case "ide":
case "lsilogic":
case "buslogic":
2020-07-20 21:11:32 +01:00
_adapterType = _adapterType.ToLowerInvariant();
2020-02-29 18:03:35 +00:00
break;
case "legacyesx":
2020-07-20 21:11:32 +01:00
_adapterType = "legacyESX";
2020-02-29 18:03:35 +00:00
break;
default:
2020-07-20 21:11:32 +01:00
ErrorMessage = $"Invalid adapter type {_adapterType}";
2020-02-29 18:03:35 +00:00
return false;
}
2020-02-29 18:03:35 +00:00
else
2020-07-20 21:11:32 +01:00
_adapterType = "ide";
if(options.TryGetValue("hwversion", out string tmpValue))
{
2020-07-20 21:11:32 +01:00
if(!uint.TryParse(tmpValue, out _hwversion))
{
ErrorMessage = "Invalid value for hwversion option";
2020-02-29 18:03:35 +00:00
return false;
}
}
2020-02-29 18:03:35 +00:00
else
2020-07-20 21:11:32 +01:00
_hwversion = 4;
if(options.TryGetValue("split", out tmpValue))
{
if(!bool.TryParse(tmpValue, out bool tmpBool))
{
ErrorMessage = "Invalid value for split option";
2020-02-29 18:03:35 +00:00
return false;
}
if(tmpBool)
{
ErrorMessage = "Splitted images not yet implemented";
2020-02-29 18:03:35 +00:00
return false;
}
}
if(options.TryGetValue("sparse", out tmpValue))
{
if(!bool.TryParse(tmpValue, out bool tmpBool))
{
ErrorMessage = "Invalid value for sparse option";
2020-02-29 18:03:35 +00:00
return false;
}
if(tmpBool)
{
ErrorMessage = "Sparse images not yet implemented";
2020-02-29 18:03:35 +00:00
return false;
}
}
}
else
{
2020-07-20 21:11:32 +01:00
_adapterType = "ide";
_hwversion = 4;
}
if(sectorSize != 512)
{
ErrorMessage = "Unsupported sector size";
2020-02-29 18:03:35 +00:00
return false;
}
if(!SupportedMediaTypes.Contains(mediaType))
{
2020-07-22 13:20:25 +01:00
ErrorMessage = $"Unsupported media format {mediaType}";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
_imageInfo = new ImageInfo
2020-02-29 18:03:35 +00:00
{
2020-07-20 04:34:16 +01:00
MediaType = mediaType,
SectorSize = sectorSize,
Sectors = sectors
2020-02-29 18:03:35 +00:00
};
try
{
2020-07-22 13:20:25 +01:00
_writingBaseName =
Path.Combine(Path.GetDirectoryName(path) ?? "", Path.GetFileNameWithoutExtension(path));
2020-07-20 21:11:32 +01:00
_descriptorStream = new StreamWriter(path, false, Encoding.ASCII);
2020-02-29 18:03:35 +00:00
// TODO: Support split
2020-07-20 21:11:32 +01:00
_writingStream = new FileStream(_writingBaseName + "-flat.vmdk", FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.None);
}
catch(IOException e)
{
ErrorMessage = $"Could not create new image file, exception {e.Message}";
2020-02-29 18:03:35 +00:00
return false;
}
IsWriting = true;
ErrorMessage = null;
2020-02-29 18:03:35 +00:00
return true;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool WriteMediaTag(byte[] data, MediaTagType tag)
{
ErrorMessage = "Writing media tags is not supported.";
2020-02-29 18:03:35 +00:00
return false;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool WriteSector(byte[] data, ulong sectorAddress)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2020-02-29 18:03:35 +00:00
return false;
}
if(data.Length != 512)
{
ErrorMessage = "Incorrect data size";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
if(sectorAddress >= _imageInfo.Sectors)
{
ErrorMessage = "Tried to write past image size";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
_writingStream.Seek((long)(sectorAddress * 512), SeekOrigin.Begin);
_writingStream.Write(data, 0, data.Length);
ErrorMessage = "";
2020-02-29 18:03:35 +00:00
return true;
}
// TODO: Implement sparse and split
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool WriteSectors(byte[] data, ulong sectorAddress, uint length)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2020-02-29 18:03:35 +00:00
return false;
}
if(data.Length % 512 != 0)
{
ErrorMessage = "Incorrect data size";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
if(sectorAddress + length > _imageInfo.Sectors)
{
ErrorMessage = "Tried to write past image size";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
_writingStream.Seek((long)(sectorAddress * 512), SeekOrigin.Begin);
_writingStream.Write(data, 0, data.Length);
ErrorMessage = "";
2020-02-29 18:03:35 +00:00
return true;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
{
ErrorMessage = "Writing sectors with tags is not supported.";
2020-02-29 18:03:35 +00:00
return false;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool WriteSectorsLong(byte[] data, ulong sectorAddress, uint length)
{
ErrorMessage = "Writing sectors with tags is not supported.";
2020-02-29 18:03:35 +00:00
return false;
}
// TODO: Implement sparse and split
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Close()
{
if(!IsWriting)
{
ErrorMessage = "Image is not opened for writing";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
_writingStream.Flush();
_writingStream.Close();
2020-07-20 21:11:32 +01:00
if(_imageInfo.Cylinders == 0)
{
2020-07-20 21:11:32 +01:00
_imageInfo.Cylinders = (uint)(_imageInfo.Sectors / 16 / 63);
_imageInfo.Heads = 16;
_imageInfo.SectorsPerTrack = 63;
2020-07-20 21:11:32 +01:00
while(_imageInfo.Cylinders == 0)
{
2020-07-20 21:11:32 +01:00
_imageInfo.Heads--;
2020-07-20 21:11:32 +01:00
if(_imageInfo.Heads == 0)
{
2020-07-20 21:11:32 +01:00
_imageInfo.SectorsPerTrack--;
_imageInfo.Heads = 16;
}
2020-07-20 21:11:32 +01:00
_imageInfo.Cylinders = (uint)(_imageInfo.Sectors / _imageInfo.Heads / _imageInfo.SectorsPerTrack);
2020-07-20 21:11:32 +01:00
if(_imageInfo.Cylinders == 0 &&
_imageInfo.Heads == 0 &&
_imageInfo.SectorsPerTrack == 0)
2020-02-29 18:03:35 +00:00
break;
}
}
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine("# Disk DescriptorFile");
_descriptorStream.WriteLine("version=1");
_descriptorStream.WriteLine($"CID={new Random().Next(int.MinValue, int.MaxValue):x8}");
_descriptorStream.WriteLine("parentCID=ffffffff");
_descriptorStream.WriteLine("createType=\"monolithicFlat\"");
_descriptorStream.WriteLine();
_descriptorStream.WriteLine("# Extent description");
_descriptorStream.WriteLine($"RW {_imageInfo.Sectors} FLAT \"{_writingBaseName + "-flat.vmdk"}\" 0");
_descriptorStream.WriteLine();
_descriptorStream.WriteLine("# The Disk Data Base");
_descriptorStream.WriteLine("#DDB");
_descriptorStream.WriteLine();
_descriptorStream.WriteLine($"ddb.virtualHWVersion = \"{_hwversion}\"");
_descriptorStream.WriteLine($"ddb.geometry.cylinders = \"{_imageInfo.Cylinders}\"");
_descriptorStream.WriteLine($"ddb.geometry.heads = \"{_imageInfo.Heads}\"");
_descriptorStream.WriteLine($"ddb.geometry.sectors = \"{_imageInfo.SectorsPerTrack}\"");
_descriptorStream.WriteLine($"ddb.adapterType = \"{_adapterType}\"");
_descriptorStream.Flush();
_descriptorStream.Close();
IsWriting = false;
ErrorMessage = "";
2020-02-29 18:03:35 +00:00
return true;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-12-31 13:17:27 +00:00
public bool SetMetadata(ImageInfo metadata) => true;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool SetGeometry(uint cylinders, uint heads, uint sectorsPerTrack)
{
if(cylinders > ushort.MaxValue)
{
ErrorMessage = "Too many cylinders.";
2020-02-29 18:03:35 +00:00
return false;
}
if(heads > byte.MaxValue)
{
ErrorMessage = "Too many heads.";
2020-02-29 18:03:35 +00:00
return false;
}
if(sectorsPerTrack > byte.MaxValue)
{
ErrorMessage = "Too many sectors per track.";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
_imageInfo.SectorsPerTrack = sectorsPerTrack;
_imageInfo.Heads = heads;
_imageInfo.Cylinders = cylinders;
return true;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool WriteSectorTag(byte[] data, ulong sectorAddress, SectorTagType tag)
{
ErrorMessage = "Unsupported feature";
2020-02-29 18:03:35 +00:00
return false;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool WriteSectorsTag(byte[] data, ulong sectorAddress, uint length, SectorTagType tag)
{
ErrorMessage = "Unsupported feature";
2020-02-29 18:03:35 +00:00
return false;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-12-31 13:17:27 +00:00
public bool SetDumpHardware(List<DumpHardwareType> dumpHardware) => false;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-12-31 13:17:27 +00:00
public bool SetCicmMetadata(CICMMetadataType metadata) => false;
}
}