Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions hub-tools/CSHubUpdater/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
!*.cs
!*.csproj
!*.csproj.user
!*.resx
!*.ico
31 changes: 31 additions & 0 deletions hub-tools/CSHubUpdater/CSHubUpdater.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.36105.23 d17.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSHubUpdater", "CSHubUpdater\CSHubUpdater.csproj", "{1181DC7D-0413-4F95-8ED8-2631F1848FCF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1181DC7D-0413-4F95-8ED8-2631F1848FCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1181DC7D-0413-4F95-8ED8-2631F1848FCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1181DC7D-0413-4F95-8ED8-2631F1848FCF}.Debug|x64.ActiveCfg = Debug|x64
{1181DC7D-0413-4F95-8ED8-2631F1848FCF}.Debug|x64.Build.0 = Debug|x64
{1181DC7D-0413-4F95-8ED8-2631F1848FCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1181DC7D-0413-4F95-8ED8-2631F1848FCF}.Release|Any CPU.Build.0 = Release|Any CPU
{1181DC7D-0413-4F95-8ED8-2631F1848FCF}.Release|x64.ActiveCfg = Release|x64
{1181DC7D-0413-4F95-8ED8-2631F1848FCF}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A1C3689F-BCDD-4EAF-A981-470D338D0778}
EndGlobalSection
EndGlobal
17 changes: 17 additions & 0 deletions hub-tools/CSHubUpdater/CSHubUpdater/CSHubUpdater.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="clroni" Version="6.3.0-dev4" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions hub-tools/CSHubUpdater/CSHubUpdater/CSHubUpdater.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Update="UpdateHub.cs">
<SubType>Form</SubType>
</Compile>
<Compile Update="UpdaterApp.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup>
</Project>
134 changes: 134 additions & 0 deletions hub-tools/CSHubUpdater/CSHubUpdater/HubBitFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.IO.Compression;
using System.Threading;
using System.Runtime.CompilerServices;

namespace CSHubUpdater
{
internal class HubBitFile : IHubBitFile
{
const string HeaderSignature = "ONIXHUBX";
const ushort HeaderVersion = 1;
private static readonly int HeaderSize = Unsafe.SizeOf<BitFileHeader>();

const int MaxFileSize = 5 * 1024 * 1024; // Anything beyond this size will result in an error

public ushort HubId => header.HardwareID;
public ushort HwRevision => header.HardwareRev;
public ushort FwVer => header.FwVersion;
public ReadOnlyMemory<byte> Data { get; }

readonly BitFileHeader header;

private HubBitFile(BitFileHeader header, byte[] data)
{
this.header = header;
Data = data;
}

public static async Task<IHubBitFile> CreateAsync(FileInfo info, CancellationToken cancellationToken)
{
if (info == null || !info.Exists)
{
throw new FileNotFoundException("Selected file does not exist");
}
if (info.Length > MaxFileSize)
{
throw new ArgumentException("File size is greater than supported");
}

await using var stream = info.OpenRead();
byte[] headerBuffer = new byte[HeaderSize];
await stream.ReadExactlyAsync(headerBuffer, cancellationToken);
var header = MemoryMarshal.Read<BitFileHeader>(headerBuffer);
ValidateHeader(header, stream.Length);

int dataLength = (int)header.Length;
int paddedSize = (dataLength + 3) & ~3;
byte[] dataBuffer = new byte[paddedSize];
Array.Fill(dataBuffer, (byte)0xFF);
await stream.ReadExactlyAsync(dataBuffer.AsMemory(0, dataLength), cancellationToken);
byte[] computedHash = SHA1.HashData(new ReadOnlySpan<byte>(dataBuffer, 0, dataLength));
if (!header.Sha1HashSpan.SequenceEqual(computedHash))
{
throw new InvalidDataException("Hashes do not match. Possible file corruption");
}

cancellationToken.ThrowIfCancellationRequested();
return new HubBitFile(header, dataBuffer);
}

static void ValidateHeader(BitFileHeader header, long streamLength)
{
if (!header.SignatureSpan.SequenceEqual(System.Text.Encoding.ASCII.GetBytes(HeaderSignature)))
{
throw new InvalidDataException("Invalid firmware file (Invalid header).");
}

if (header.Version != HeaderVersion)
{
throw new InvalidDataException($"Unextpected file version. Expected {HeaderVersion} but reported {header.Version}.");
}

if ((ulong)streamLength < (header.Length + (ulong)HeaderSize))
{
throw new InvalidDataException("File size is smaller than reported.");
}
}



[StructLayout(LayoutKind.Explicit, Size = 64, Pack = 1)]
unsafe struct BitFileHeader
{
[FieldOffset(0)]
public fixed byte Signature[8];
[FieldOffset(8)]
public ushort Version;
[FieldOffset(10)]
public ushort HardwareID;
[FieldOffset(12)]
public ushort HardwareRev;
[FieldOffset(14)]
public ushort FwVersion;
[FieldOffset(16)]
public ulong Length;
[FieldOffset(24)]
public fixed byte Sha1Hash[20];
[FieldOffset(44)]
public fixed byte Reserved[20];

public ReadOnlySpan<byte> SignatureSpan
{
get
{
fixed (byte* ptr = Signature)
{
return new ReadOnlySpan<byte>(ptr, 8);
}
}
}
public ReadOnlySpan<byte> Sha1HashSpan
{
get
{
fixed (byte* ptr = Sha1Hash)
{
return new Span<byte>(ptr, 20);
}
}

}
}


}
}
Loading