diff --git a/FirmwareGen.sln b/FirmwareGen.sln index 9000ad4..3ae8437 100644 --- a/FirmwareGen.sln +++ b/FirmwareGen.sln @@ -5,15 +5,17 @@ VisualStudioVersion = 17.9.34407.89 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FirmwareGen", "FirmwareGen\FirmwareGen.csproj", "{85918ACF-6909-45DA-B56D-3D5113B7EA6C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PartitionOffsetsHelperTool", "PartitionOffsetsHelperTool\PartitionOffsetsHelperTool.csproj", "{AC699AC2-883A-4278-B2A8-F0F693DC8643}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PartitionOffsetsHelperTool", "PartitionOffsetsHelperTool\PartitionOffsetsHelperTool.csproj", "{AC699AC2-883A-4278-B2A8-F0F693DC8643}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|ARM32 = Debug|ARM32 Debug|ARM64 = Debug|ARM64 Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU + Release|ARM32 = Release|ARM32 Release|ARM64 = Release|ARM64 Release|x64 = Release|x64 Release|x86 = Release|x86 @@ -21,6 +23,8 @@ Global GlobalSection(ProjectConfigurationPlatforms) = postSolution {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Debug|ARM32.ActiveCfg = Debug|Any CPU + {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Debug|ARM32.Build.0 = Debug|Any CPU {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Debug|ARM64.ActiveCfg = Debug|ARM64 {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Debug|ARM64.Build.0 = Debug|ARM64 {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -29,14 +33,18 @@ Global {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Debug|x86.Build.0 = Debug|x86 {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|Any CPU.ActiveCfg = Release|Any CPU {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|Any CPU.Build.0 = Release|Any CPU - {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|ARM64.ActiveCfg = Release|ARM64 - {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|ARM64.Build.0 = Release|ARM64 + {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|ARM32.ActiveCfg = Release|Any CPU + {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|ARM32.Build.0 = Release|Any CPU + {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|ARM64.ActiveCfg = Release|Any CPU + {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|ARM64.Build.0 = Release|Any CPU {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|x64.ActiveCfg = Release|x64 {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|x64.Build.0 = Release|x64 {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|x86.ActiveCfg = Release|x86 {85918ACF-6909-45DA-B56D-3D5113B7EA6C}.Release|x86.Build.0 = Release|x86 {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Debug|ARM32.ActiveCfg = Debug|Any CPU + {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Debug|ARM32.Build.0 = Debug|Any CPU {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Debug|ARM64.ActiveCfg = Debug|Any CPU {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Debug|ARM64.Build.0 = Debug|Any CPU {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -45,6 +53,8 @@ Global {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Debug|x86.Build.0 = Debug|Any CPU {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Release|Any CPU.ActiveCfg = Release|Any CPU {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Release|Any CPU.Build.0 = Release|Any CPU + {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Release|ARM32.ActiveCfg = Release|Any CPU + {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Release|ARM32.Build.0 = Release|Any CPU {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Release|ARM64.ActiveCfg = Release|Any CPU {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Release|ARM64.Build.0 = Release|Any CPU {AC699AC2-883A-4278-B2A8-F0F693DC8643}.Release|x64.ActiveCfg = Release|Any CPU diff --git a/FirmwareGen/FirmwareGen.csproj b/FirmwareGen/FirmwareGen.csproj index 9c105c9..fe2b612 100644 --- a/FirmwareGen/FirmwareGen.csproj +++ b/FirmwareGen/FirmwareGen.csproj @@ -11,6 +11,6 @@ - + \ No newline at end of file diff --git a/FirmwareGen/Streams/DeviceStream.cs b/FirmwareGen/Streams/DeviceStream.cs deleted file mode 100644 index ff2adb2..0000000 --- a/FirmwareGen/Streams/DeviceStream.cs +++ /dev/null @@ -1,429 +0,0 @@ -/* - -Copyright (c) 2019, Gustave Monce - gus33000.me - @gus33000 -Copyright (c) 2018, Proto Beta Test - protobetatest.com - @ProtoBetaTest - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -*/ -using Microsoft.Win32.SafeHandles; -using System; -using System.ComponentModel; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices; - -namespace FirmwareGen.Streams -{ - public class DeviceStream : Stream - { - private const uint GENERIC_READ = 0x80000000; - private const uint GENERIC_WRITE = 0x40000000; - - private const uint OPEN_EXISTING = 3; - private const uint FILE_ATTRIBUTE_DEVICE = 0x40; - private const uint FILE_FLAG_NO_BUFFERING = 0x20000000; - private const uint FILE_FLAG_WRITE_THROUGH = 0x80000000; - private const uint DISK_BASE = 7; - - private const uint FILE_ANY_ACCESS = 0; - private const uint FILE_SHARE_READ = 1; - private const uint FILE_SHARE_WRITE = 2; - - private const uint FILE_DEVICE_FILE_SYSTEM = 9; - private const uint METHOD_BUFFERED = 0; - - private static readonly uint DISK_GET_DRIVE_GEOMETRY_EX = CTL_CODE(DISK_BASE, 0x0028, METHOD_BUFFERED, FILE_ANY_ACCESS); - private static readonly uint FSCTL_LOCK_VOLUME = CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 6, METHOD_BUFFERED, FILE_ANY_ACCESS); - private static readonly uint FSCTL_UNLOCK_VOLUME = CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 7, METHOD_BUFFERED, FILE_ANY_ACCESS); - private static readonly uint FSCTL_DISMOUNT_VOLUME = CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 8, METHOD_BUFFERED, FILE_ANY_ACCESS); - - private enum MEDIA_TYPE : int - { - Unknown = 0, - F5_1Pt2_512 = 1, - F3_1Pt44_512 = 2, - F3_2Pt88_512 = 3, - F3_20Pt8_512 = 4, - F3_720_512 = 5, - F5_360_512 = 6, - F5_320_512 = 7, - F5_320_1024 = 8, - F5_180_512 = 9, - F5_160_512 = 10, - RemovableMedia = 11, - FixedMedia = 12, - F3_120M_512 = 13, - F3_640_512 = 14, - F5_640_512 = 15, - F5_720_512 = 16, - F3_1Pt2_512 = 17, - F3_1Pt23_1024 = 18, - F5_1Pt23_1024 = 19, - F3_128Mb_512 = 20, - F3_230Mb_512 = 21, - F8_256_128 = 22, - F3_200Mb_512 = 23, - F3_240M_512 = 24, - F3_32M_512 = 25 - } - - [StructLayout(LayoutKind.Sequential)] - private struct DISK_GEOMETRY - { - internal long Cylinders; - internal MEDIA_TYPE MediaType; - internal uint TracksPerCylinder; - internal uint SectorsPerTrack; - internal uint BytesPerSector; - } - - [StructLayout(LayoutKind.Sequential)] - private struct DISK_GEOMETRY_EX - { - internal DISK_GEOMETRY Geometry; - internal long DiskSize; - - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] - internal byte[] Data; - } - - private SafeFileHandle handleValue = null; - private long _Position = 0; - private readonly long _length = 0; - private readonly uint _sectorsize = 0; - private readonly bool _canWrite = false; - private readonly bool _canRead = false; - private bool disposed = false; - - private static uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint Access) - { - return (DeviceType << 16) | (Access << 14) | (Function << 2) | Method; - } - - public DeviceStream(string device, FileAccess access) - { - if (string.IsNullOrEmpty(device)) - { - throw new ArgumentNullException(nameof(device)); - } - - uint fileAccess = 0; - switch (access) - { - case FileAccess.Read: - fileAccess = GENERIC_READ; - _canRead = true; - break; - case FileAccess.ReadWrite: - fileAccess = GENERIC_READ | GENERIC_WRITE; - _canRead = true; - _canWrite = true; - break; - case FileAccess.Write: - fileAccess = GENERIC_WRITE; - _canWrite = true; - break; - } - - string devicePath = @"\\.\PhysicalDrive" + device.ToLower().Replace(@"\\.\physicaldrive", ""); - - (_length, _sectorsize) = GetDiskProperties(devicePath); - - IntPtr ptr = NativeMethods.CreateFile(devicePath, fileAccess, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_DEVICE | FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH, IntPtr.Zero); - handleValue = new SafeFileHandle(ptr, true); - - if (handleValue.IsInvalid) - { - Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - } - - uint lpBytesReturned = 0; - uint result = NativeMethods.DeviceIoControl(handleValue, FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, ref lpBytesReturned, IntPtr.Zero); - - if (result == 0) - { - Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - } - - lpBytesReturned = 0; - result = NativeMethods.DeviceIoControl(handleValue, FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, ref lpBytesReturned, IntPtr.Zero); - - if (result == 0) - { - Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - } - } - - public override bool CanRead => _canRead; - - public override bool CanSeek => true; - - public override bool CanWrite => _canWrite; - - public override void Flush() - { - return; - } - - public override long Length => _length; - - public override long Position - { - get => _Position; - set => Seek(value, SeekOrigin.Begin); - } - - /// - /// Reads data - /// - /// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and - /// (offset + count - 1) replaced by the bytes read from the current source. - /// The zero-based byte offset in buffer at which to begin storing the data read from the current stream. - /// The maximum number of bytes to be read from the current stream. - /// - private int InternalRead(byte[] buffer, int offset, int count) - { - int BytesRead = 0; - byte[] BufBytes = new byte[count]; - if (!NativeMethods.ReadFile(handleValue.DangerousGetHandle(), BufBytes, count, ref BytesRead, IntPtr.Zero)) - { - Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - } - for (int i = 0; i < BytesRead; i++) - { - buffer[offset + i] = BufBytes[i]; - } - - _Position += count; - - return BytesRead; - } - - /// - /// Some devices cannot read portions that are not modulo a sector, this aims to fix that issue. - /// - /// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and - /// (offset + count - 1) replaced by the bytes read from the current source. - /// The zero-based byte offset in buffer at which to begin storing the data read from the current stream. - /// The maximum number of bytes to be read from the current stream. - /// - public override int Read(byte[] buffer, int offset, int count) - { - if (count % _sectorsize != 0) - { - long extrastart = Position % _sectorsize; - if (extrastart != 0) - { - _ = Seek(-extrastart, SeekOrigin.Current); - } - - long addedcount = _sectorsize - (count % _sectorsize); - long ncount = count + addedcount; - byte[] tmpbuffer = new byte[extrastart + buffer.Length + addedcount]; - buffer.CopyTo(tmpbuffer, extrastart); - _ = InternalRead(tmpbuffer, offset + (int)extrastart, (int)ncount); - tmpbuffer.ToList().Skip((int)extrastart).Take(count + offset).ToArray().CopyTo(buffer, 0); - return count; - } - - return InternalRead(buffer, offset, count); - } - - public override int ReadByte() - { - int BytesRead = 0; - byte[] lpBuffer = new byte[1]; - if (!NativeMethods.ReadFile( - handleValue.DangerousGetHandle(), // handle to file - lpBuffer, // data buffer - 1, // number of bytes to read - ref BytesRead, // number of bytes read - IntPtr.Zero - )) - { - Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - } - - _Position++; - - return lpBuffer[0]; - } - - public override void WriteByte(byte Byte) - { - int BytesWritten = 0; - byte[] lpBuffer = [Byte]; - if (!NativeMethods.WriteFile( - handleValue.DangerousGetHandle(), // handle to file - lpBuffer, // data buffer - 1, // number of bytes to write - ref BytesWritten, // number of bytes written - IntPtr.Zero - )) - { - Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - } - - _Position++; - } - - public override long Seek(long offset, SeekOrigin origin) - { - long off = offset; - - switch (origin) - { - case SeekOrigin.Current: - off += _Position; - break; - case SeekOrigin.End: - off += _length; - break; - } - - if (!NativeMethods.SetFilePointerEx(handleValue, off, out long ret, 0)) - { - return _Position; - } - - _Position = ret; - - return ret; - } - - public override void SetLength(long value) - { - throw new NotImplementedException(); - } - - public override void Write(byte[] buffer, int offset, int count) - { - int BytesWritten = 0; - byte[] BufBytes = new byte[count]; - for (int i = 0; i < count; i++) - { - BufBytes[offset + i] = buffer[i]; - } - - if (!NativeMethods.WriteFile(handleValue.DangerousGetHandle(), BufBytes, count, ref BytesWritten, IntPtr.Zero)) - { - Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - } - - _Position += count; - } - - public override void Close() - { - if (handleValue != null && !handleValue.IsInvalid && !handleValue.IsClosed) - { - try - { - uint lpBytesReturned = 0; - uint result = NativeMethods.DeviceIoControl(handleValue, FSCTL_UNLOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, ref lpBytesReturned, IntPtr.Zero); - - if (result == 0) - { - Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - } - } - catch { } - - handleValue.Close(); - handleValue.Dispose(); - handleValue = null; - } - base.Close(); - } - - public new void Dispose() - { - Dispose(true); - base.Dispose(); - } - - public new void Dispose(bool disposing) - { - // Check to see if Dispose has already been called. - if (!disposed) - { - if (disposing) - { - if (handleValue != null && !handleValue.IsInvalid && !handleValue.IsClosed) - { - try - { - uint lpBytesReturned = 0; - uint result = NativeMethods.DeviceIoControl(handleValue, FSCTL_UNLOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, ref lpBytesReturned, IntPtr.Zero); - - if (result == 0) - { - Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - } - } - catch { } - - handleValue.Close(); - handleValue.Dispose(); - handleValue = null; - } - } - // Note disposing has been done. - disposed = true; - } - } - - private static (long, uint) GetDiskProperties(string deviceName) - { - DISK_GEOMETRY_EX x = new(); - Execute(ref x, DISK_GET_DRIVE_GEOMETRY_EX, deviceName); - return (x.DiskSize, x.Geometry.BytesPerSector); - } - - private static void Execute(ref T x, uint dwIoControlCode, string lpFileName, uint dwDesiredAccess = GENERIC_READ, uint dwShareMode = FILE_SHARE_WRITE | FILE_SHARE_READ, IntPtr lpSecurityAttributes = default, uint dwCreationDisposition = OPEN_EXISTING, uint dwFlagsAndAttributes = 0, IntPtr hTemplateFile = default) - { - IntPtr hDevice = NativeMethods.CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); - - SafeFileHandle handleValue = new(hDevice, true); - - if (hDevice == default || handleValue.IsInvalid) - { - throw new Win32Exception(Marshal.GetLastWin32Error()); - } - - int nOutBufferSize = Marshal.SizeOf(typeof(T)); - IntPtr lpOutBuffer = Marshal.AllocHGlobal(nOutBufferSize); - uint lpBytesReturned = default; - - uint result = NativeMethods.DeviceIoControl(handleValue, dwIoControlCode, IntPtr.Zero, 0, lpOutBuffer, nOutBufferSize, ref lpBytesReturned, IntPtr.Zero); - - if (result == 0) - { - throw new Win32Exception(Marshal.GetLastWin32Error()); - } - - x = (T)Marshal.PtrToStructure(lpOutBuffer, typeof(T)); - Marshal.FreeHGlobal(lpOutBuffer); - - handleValue.Close(); - handleValue.Dispose(); - } - } -} \ No newline at end of file diff --git a/FirmwareGen/Streams/NativeMethods.cs b/FirmwareGen/Streams/NativeMethods.cs deleted file mode 100644 index 1f5463f..0000000 --- a/FirmwareGen/Streams/NativeMethods.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Microsoft.Win32.SafeHandles; -using System; -using System.Runtime.InteropServices; - -namespace FirmwareGen.Streams -{ - internal static class NativeMethods - { - [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - internal static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile); - - [DllImport("kernel32.dll", SetLastError = true)] - internal static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer, int nNumberOfBytesToRead, ref int lpNumberOfBytesRead, IntPtr lpOverlapped); - - [DllImport("kernel32.dll", SetLastError = true)] - internal static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, IntPtr lpOverlapped); - - [DllImport("kernel32.dll", SetLastError = true)] - internal static extern uint DeviceIoControl(SafeFileHandle hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer, int nOutBufferSize, ref uint lpBytesReturned, IntPtr lpOverlapped); - - [DllImport("kernel32.dll")] - internal static extern bool SetFilePointerEx(SafeFileHandle hFile, long liDistanceToMove, out long lpNewFilePointer, uint dwMoveMethod); - } -} \ No newline at end of file diff --git a/FirmwareGen/VirtualDisks/BlankVHDUtils.cs b/FirmwareGen/VirtualDisks/BlankVHDUtils.cs index 09ac9ef..1f93212 100644 --- a/FirmwareGen/VirtualDisks/BlankVHDUtils.cs +++ b/FirmwareGen/VirtualDisks/BlankVHDUtils.cs @@ -1,4 +1,4 @@ -using FirmwareGen.Streams; +using DiscUtils.Vhdx; using System; using System.IO; @@ -6,10 +6,14 @@ namespace FirmwareGen.VirtualDisks { internal class BlankVHDUtils { - private static void WriteGPTToDisk(string DiskId, byte[] PrimaryGPT, byte[] BackupGPT) + private static void WriteGPTToDisk(string TmpVHD, byte[] PrimaryGPT, byte[] BackupGPT) { const int chunkSize = 4096; - DeviceStream ds = new(DiskId, FileAccess.ReadWrite); + + DiscUtils.Setup.SetupHelper.RegisterAssembly(typeof(Disk).Assembly); + using DiscUtils.VirtualDisk outDisk = DiscUtils.VirtualDisk.OpenDisk(TmpVHD, FileAccess.ReadWrite); + + DiscUtils.Streams.SparseStream ds = outDisk.Content; // Primary GPT Logging.Log("Writing Primary GPT"); @@ -50,17 +54,11 @@ public static void PrepareVHD(string TmpVHD, byte[] PrimaryGPT, byte[] BackupGPT { const string SystemPartition = "Y:"; - Logging.Log("Mounting Main VHD"); - string DiskId = VolumeUtils.MountVirtualHardDisk(TmpVHD, false); - Logging.Log("Writing GPT"); - WriteGPTToDisk(DiskId, PrimaryGPT, BackupGPT); - - Logging.Log("Dismounting Main VHD"); - VolumeUtils.DismountVirtualHardDisk(TmpVHD); + WriteGPTToDisk(TmpVHD, PrimaryGPT, BackupGPT); Logging.Log("Mounting Main VHD"); - DiskId = VolumeUtils.MountVirtualHardDisk(TmpVHD, false); + string DiskId = VolumeUtils.MountVirtualHardDisk(TmpVHD, false); Logging.Log("Getting Windows Partition Drive Letter"); string VHDLetter = VolumeUtils.GetVirtualHardDiskLetterFromDiskID(DiskId); diff --git a/FirmwareGen/VirtualDisks/VHDUtils.cs b/FirmwareGen/VirtualDisks/VHDUtils.cs index c28a713..b0f81d1 100644 --- a/FirmwareGen/VirtualDisks/VHDUtils.cs +++ b/FirmwareGen/VirtualDisks/VHDUtils.cs @@ -1,7 +1,9 @@ using System; +using System.IO; using System.Text; using System.Text.RegularExpressions; -using Vanara.IO; +using DiscUtils.Streams; +using DiscUtils.Vhdx; namespace FirmwareGen.VirtualDisks { @@ -106,7 +108,10 @@ public static void UnmountVHD(string vhdfile) public static void CreateVHDX(string vhdfile, uint SectorSize, ulong DiskSize) { - using VirtualDisk virtualDisk = VirtualDisk.Create(vhdfile, DiskSize, true, 0, SectorSize); + DiscUtils.Setup.SetupHelper.RegisterAssembly(typeof(Disk).Assembly); + + using Stream fs = new FileStream(vhdfile, FileMode.CreateNew, FileAccess.ReadWrite); + using DiscUtils.VirtualDisk outDisk = Disk.InitializeDynamic(fs, Ownership.None, (long)DiskSize, DiscUtils.Geometry.FromCapacity((long)DiskSize, (int)SectorSize)); } ///