Skip to content

Commit

Permalink
Cleanup Firmware Gen code to use DiscUtils for vhdx creation
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Feb 10, 2024
1 parent e413e91 commit 0ef7a06
Show file tree
Hide file tree
Showing 250 changed files with 32,407 additions and 468 deletions.
61 changes: 61 additions & 0 deletions DiscUtils/DiscUtils.Core/ApplePartitionMap/BlockZero.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
//
// 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 System;
using DiscUtils.Streams;

namespace DiscUtils.ApplePartitionMap
{
public sealed class BlockZero : IByteArraySerializable
{
public uint BlockCount;
public ushort BlockSize;
public ushort DeviceId;
public ushort DeviceType;
public ushort DriverCount;
public uint DriverData;
public ushort Signature;

public int Size
{
get { return 512; }
}

public int ReadFrom(byte[] buffer, int offset)
{
Signature = EndianUtilities.ToUInt16BigEndian(buffer, offset + 0);
BlockSize = EndianUtilities.ToUInt16BigEndian(buffer, offset + 2);
BlockCount = EndianUtilities.ToUInt32BigEndian(buffer, offset + 4);
DeviceType = EndianUtilities.ToUInt16BigEndian(buffer, offset + 8);
DeviceId = EndianUtilities.ToUInt16BigEndian(buffer, offset + 10);
DriverData = EndianUtilities.ToUInt32BigEndian(buffer, offset + 12);
DriverCount = EndianUtilities.ToUInt16LittleEndian(buffer, offset + 16);

return 512;
}

public void WriteTo(byte[] buffer, int offset)
{
throw new NotImplementedException();
}
}
}
153 changes: 153 additions & 0 deletions DiscUtils/DiscUtils.Core/ApplePartitionMap/PartitionMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
//
// 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 System;
using System.Collections.ObjectModel;
using System.IO;
using DiscUtils.Partitions;
using DiscUtils.Streams;

namespace DiscUtils.ApplePartitionMap
{
/// <summary>
/// Interprets Apple Partition Map structures that partition a disk.
/// </summary>
public sealed class PartitionMap : PartitionTable
{
private readonly PartitionMapEntry[] _partitions;
private readonly Stream _stream;

/// <summary>
/// Initializes a new instance of the PartitionMap class.
/// </summary>
/// <param name="stream">Stream containing the contents of a disk.</param>
public PartitionMap(Stream stream)
{
_stream = stream;

stream.Position = 0;
byte[] initialBytes = StreamUtilities.ReadExact(stream, 1024);

BlockZero b0 = new BlockZero();
b0.ReadFrom(initialBytes, 0);

PartitionMapEntry initialPart = new PartitionMapEntry(_stream);
initialPart.ReadFrom(initialBytes, 512);

byte[] partTableData = StreamUtilities.ReadExact(stream, (int)(initialPart.MapEntries - 1) * 512);

_partitions = new PartitionMapEntry[initialPart.MapEntries - 1];
for (uint i = 0; i < initialPart.MapEntries - 1; ++i)
{
_partitions[i] = new PartitionMapEntry(_stream);
_partitions[i].ReadFrom(partTableData, (int)(512 * i));
}
}

/// <summary>
/// Gets the GUID of the disk, always returns Guid.Empty.
/// </summary>
public override Guid DiskGuid
{
get { return Guid.Empty; }
}

/// <summary>
/// Gets the partitions present on the disk.
/// </summary>
public override ReadOnlyCollection<PartitionInfo> Partitions
{
get { return new ReadOnlyCollection<PartitionInfo>(_partitions); }
}

/// <summary>
/// Creates a new partition that encompasses the entire disk.
/// </summary>
/// <param name="type">The partition type.</param>
/// <param name="active">Whether the partition is active (bootable).</param>
/// <returns>The index of the partition.</returns>
/// <remarks>The partition table must be empty before this method is called,
/// otherwise IOException is thrown.</remarks>
public override int Create(WellKnownPartitionType type, bool active)
{
throw new NotImplementedException();
}

/// <summary>
/// Creates a new partition with a target size.
/// </summary>
/// <param name="size">The target size (in bytes).</param>
/// <param name="type">The partition type.</param>
/// <param name="active">Whether the partition is active (bootable).</param>
/// <returns>The index of the new partition.</returns>
public override int Create(long size, WellKnownPartitionType type, bool active)
{
throw new NotImplementedException();
}

/// <summary>
/// Creates a new aligned partition that encompasses the entire disk.
/// </summary>
/// <param name="type">The partition type.</param>
/// <param name="active">Whether the partition is active (bootable).</param>
/// <param name="alignment">The alignment (in byte).</param>
/// <returns>The index of the partition.</returns>
/// <remarks>The partition table must be empty before this method is called,
/// otherwise IOException is thrown.</remarks>
/// <remarks>
/// Traditionally partitions were aligned to the physical structure of the underlying disk,
/// however with modern storage greater efficiency is acheived by aligning partitions on
/// large values that are a power of two.
/// </remarks>
public override int CreateAligned(WellKnownPartitionType type, bool active, int alignment)
{
throw new NotImplementedException();
}

/// <summary>
/// Creates a new aligned partition with a target size.
/// </summary>
/// <param name="size">The target size (in bytes).</param>
/// <param name="type">The partition type.</param>
/// <param name="active">Whether the partition is active (bootable).</param>
/// <param name="alignment">The alignment (in byte).</param>
/// <returns>The index of the new partition.</returns>
/// <remarks>
/// Traditionally partitions were aligned to the physical structure of the underlying disk,
/// however with modern storage greater efficiency is achieved by aligning partitions on
/// large values that are a power of two.
/// </remarks>
public override int CreateAligned(long size, WellKnownPartitionType type, bool active, int alignment)
{
throw new NotImplementedException();
}

/// <summary>
/// Deletes a partition at a given index.
/// </summary>
/// <param name="index">The index of the partition.</param>
public override void Delete(int index)
{
throw new NotImplementedException();
}
}
}
112 changes: 112 additions & 0 deletions DiscUtils/DiscUtils.Core/ApplePartitionMap/PartitionMapEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
//
// 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 System;
using System.IO;
using DiscUtils.Partitions;
using DiscUtils.Streams;

namespace DiscUtils.ApplePartitionMap
{
public sealed class PartitionMapEntry : PartitionInfo, IByteArraySerializable
{
private readonly Stream _diskStream;
public uint BootBlock;
public uint BootBytes;
public uint Flags;
public uint LogicalBlocks;
public uint LogicalBlockStart;
public uint MapEntries;
public string Name;
public uint PhysicalBlocks;
public uint PhysicalBlockStart;
public ushort Signature;
public string Type;

public PartitionMapEntry(Stream diskStream)
{
_diskStream = diskStream;
}

public override byte BiosType
{
get { return 0xAF; }
}

public override long FirstSector
{
get { return PhysicalBlockStart; }
}

public override Guid GuidType
{
get { return Guid.Empty; }
}

public override long LastSector
{
get { return PhysicalBlockStart + PhysicalBlocks - 1; }
}

public override string TypeAsString
{
get { return Type; }
}

public override PhysicalVolumeType VolumeType
{
get { return PhysicalVolumeType.ApplePartition; }
}

public int Size
{
get { return 512; }
}

public int ReadFrom(byte[] buffer, int offset)
{
Signature = EndianUtilities.ToUInt16BigEndian(buffer, offset + 0);
MapEntries = EndianUtilities.ToUInt32BigEndian(buffer, offset + 4);
PhysicalBlockStart = EndianUtilities.ToUInt32BigEndian(buffer, offset + 8);
PhysicalBlocks = EndianUtilities.ToUInt32BigEndian(buffer, offset + 12);
Name = EndianUtilities.BytesToString(buffer, offset + 16, 32).TrimEnd('\0');
Type = EndianUtilities.BytesToString(buffer, offset + 48, 32).TrimEnd('\0');
LogicalBlockStart = EndianUtilities.ToUInt32BigEndian(buffer, offset + 80);
LogicalBlocks = EndianUtilities.ToUInt32BigEndian(buffer, offset + 84);
Flags = EndianUtilities.ToUInt32BigEndian(buffer, offset + 88);
BootBlock = EndianUtilities.ToUInt32BigEndian(buffer, offset + 92);
BootBytes = EndianUtilities.ToUInt32BigEndian(buffer, offset + 96);

return 512;
}

public void WriteTo(byte[] buffer, int offset)
{
throw new NotImplementedException();
}

public override SparseStream Open()
{
return new SubStream(_diskStream, PhysicalBlockStart * 512, PhysicalBlocks * 512);
}
}
}
66 changes: 66 additions & 0 deletions DiscUtils/DiscUtils.Core/ApplePartitionMap/PartitionMapFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
//
// 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 System.IO;
using DiscUtils.Partitions;
using DiscUtils.Streams;

namespace DiscUtils.ApplePartitionMap
{
[PartitionTableFactory]
public sealed class PartitionMapFactory : PartitionTableFactory
{
public override bool DetectIsPartitioned(Stream s)
{
if (s.Length < 1024)
{
return false;
}

s.Position = 0;

byte[] initialBytes = StreamUtilities.ReadExact(s, 1024);

BlockZero b0 = new BlockZero();
b0.ReadFrom(initialBytes, 0);
if (b0.Signature != 0x4552)
{
return false;
}

PartitionMapEntry initialPart = new PartitionMapEntry(s);
initialPart.ReadFrom(initialBytes, 512);

return initialPart.Signature == 0x504d;
}

public override PartitionTable DetectPartitionTable(VirtualDisk disk)
{
if (!DetectIsPartitioned(disk.Content))
{
return null;
}

return new PartitionMap(disk.Content);
}
}
}
Loading

0 comments on commit 0ef7a06

Please sign in to comment.