Skip to content

Commit

Permalink
Bring back some of the old install code
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Dec 10, 2023
1 parent c03f1ec commit c1c81a8
Show file tree
Hide file tree
Showing 6 changed files with 487 additions and 15 deletions.
46 changes: 36 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,59 @@ on:
branches: [master]
pull_request:
branches: [master]
workflow_dispatch:

jobs:
build:
runs-on: windows-latest
strategy:
matrix:
architecture: [x86, x64, arm64]
platform: [win]
target: [win-x86,win-x64,win-arm64]
include:
- target: win-x86
platform: win
architecture: x86
- target: win-x64
platform: win
architecture: x64
- target: win-arm64
platform: win
architecture: arm64
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4.1.1

- name: Install .NET SDK
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v4.0.0
with:
dotnet-version: "8.x"
dotnet-version: "8.0.x"

- name: Add MSBuild to PATH
uses: microsoft/setup-msbuild@v1.0.2
uses: microsoft/setup-msbuild@v1.3.1

- name: Build utilities
shell: pwsh
run: |
msbuild /m /t:restore,driverupdater:publish /p:Platform=${{ matrix.architecture }} /p:RuntimeIdentifier=${{ matrix.platform }}-${{ matrix.architecture }} /p:PublishDir=${{ github.workspace }}/artifacts/${{ matrix.platform }}-${{ matrix.architecture }} /p:PublishSingleFile=true /p:PublishTrimmed=true DriverUpdater.sln
msbuild /m /t:restore,driverupdater:publish /p:Platform=${{ matrix.architecture }} /p:RuntimeIdentifier=${{ matrix.platform }}-${{ matrix.architecture }} /p:PublishDir=${{ github.workspace }}/artifacts/${{ matrix.platform }}-${{ matrix.architecture }} /p:PublishSingleFile=true /p:PublishTrimmed=false /p:Configuration=Release DriverUpdater.sln
- name: Upload artifact
uses: actions/upload-artifact@v2
- name: Create PDB Output Directory
shell: pwsh
run: |
mkdir ${{ github.workspace }}\artifacts\${{ matrix.platform }}-${{ matrix.architecture }}\PDBs
- name: Move PDBs
shell: pwsh
run: |
move ${{ github.workspace }}\artifacts\${{ matrix.platform }}-${{ matrix.architecture }}\*.pdb ${{ github.workspace }}\artifacts\${{ matrix.platform }}-${{ matrix.architecture }}\PDBs\
- name: Upload artifact (Binaries)
uses: actions/upload-artifact@v3.1.3
with:
name: ${{ matrix.platform }}-${{ matrix.architecture }}
name: ${{ matrix.platform }}-${{ matrix.architecture }}-binaries
path: ${{ github.workspace }}/artifacts/${{ matrix.platform }}-${{ matrix.architecture }}

- name: Upload artifact (Symbols)
uses: actions/upload-artifact@v3.1.3
with:
name: ${{ matrix.platform }}-${{ matrix.architecture }}-symbols
path: ${{ github.workspace }}/artifacts/${{ matrix.platform }}-${{ matrix.architecture }}\PDBs
142 changes: 142 additions & 0 deletions DriverUpdater/CksLicensing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using DiscUtils.Registry;
using System.Collections.Generic;
using System.IO;

namespace DriverUpdater
{
internal class CksLicensing
{
private string DrivePath = "";

public CksLicensing(string DrivePath)
{
this.DrivePath = DrivePath;
}

private static readonly int[] Empty = new int[0];

private static int[] Locate(byte[] self, byte[] candidate)
{
if (IsEmptyLocate(self, candidate))
{
return Empty;
}

List<int>? list = new();

for (int i = 0; i < self.Length; i++)
{
if (!IsMatch(self, i, candidate))
{
continue;
}

list.Add(i);
}

return list.Count == 0 ? Empty : list.ToArray();
}

private static bool IsMatch(byte[] array, int position, byte[] candidate)
{
if (candidate.Length > (array.Length - position))
{
return false;
}

for (int i = 0; i < candidate.Length; i++)
{
if (array[position + i] != candidate[i])
{
return false;
}
}

return true;
}

private static bool IsEmptyLocate(byte[] array, byte[] candidate)
{
return array == null
|| candidate == null
|| array.Length == 0
|| candidate.Length == 0
|| candidate.Length > array.Length;
}

public void SetLicensedState()
{
try
{
using RegistryHive hive = new(
File.Open(
Path.Combine(DrivePath, "Windows\\System32\\config\\SYSTEM"),
FileMode.Open,
FileAccess.ReadWrite
), DiscUtils.Streams.Ownership.Dispose);

RegistryKey key = hive.Root.OpenSubKey("ControlSet001\\Control\\ProductOptions");
byte[] value = (byte[])key.GetValue("ProductPolicy");

byte[] oldValue = new byte[]
{
0x43, 0x00, 0x6F, 0x00, 0x64, 0x00, 0x65, 0x00, 0x49, 0x00, 0x6E,
0x00, 0x74, 0x00, 0x65, 0x00, 0x67, 0x00, 0x72, 0x00, 0x69, 0x00,
0x74, 0x00, 0x79, 0x00, 0x2D, 0x00, 0x41, 0x00, 0x6C, 0x00, 0x6C,
0x00, 0x6F, 0x00, 0x77, 0x00, 0x43, 0x00, 0x6F, 0x00, 0x6E, 0x00,
0x66, 0x00, 0x69, 0x00, 0x67, 0x00, 0x75, 0x00, 0x72, 0x00, 0x61,
0x00, 0x62, 0x00, 0x6C, 0x00, 0x65, 0x00, 0x50, 0x00, 0x6F, 0x00,
0x6C, 0x00, 0x69, 0x00, 0x63, 0x00, 0x79, 0x00, 0x2D, 0x00, 0x43,
0x00, 0x75, 0x00, 0x73, 0x00, 0x74, 0x00, 0x6F, 0x00, 0x6D, 0x00,
0x4B, 0x00, 0x65, 0x00, 0x72, 0x00, 0x6E, 0x00, 0x65, 0x00, 0x6C,
0x00, 0x53, 0x00, 0x69, 0x00, 0x67, 0x00, 0x6E, 0x00, 0x65, 0x00,
0x72, 0x00, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00
};

byte[] newValue = new byte[]
{
0x43, 0x00, 0x6F, 0x00, 0x64, 0x00, 0x65, 0x00, 0x49, 0x00, 0x6E,
0x00, 0x74, 0x00, 0x65, 0x00, 0x67, 0x00, 0x72, 0x00, 0x69, 0x00,
0x74, 0x00, 0x79, 0x00, 0x2D, 0x00, 0x41, 0x00, 0x6C, 0x00, 0x6C,
0x00, 0x6F, 0x00, 0x77, 0x00, 0x43, 0x00, 0x6F, 0x00, 0x6E, 0x00,
0x66, 0x00, 0x69, 0x00, 0x67, 0x00, 0x75, 0x00, 0x72, 0x00, 0x61,
0x00, 0x62, 0x00, 0x6C, 0x00, 0x65, 0x00, 0x50, 0x00, 0x6F, 0x00,
0x6C, 0x00, 0x69, 0x00, 0x63, 0x00, 0x79, 0x00, 0x2D, 0x00, 0x43,
0x00, 0x75, 0x00, 0x73, 0x00, 0x74, 0x00, 0x6F, 0x00, 0x6D, 0x00,
0x4B, 0x00, 0x65, 0x00, 0x72, 0x00, 0x6E, 0x00, 0x65, 0x00, 0x6C,
0x00, 0x53, 0x00, 0x69, 0x00, 0x67, 0x00, 0x6E, 0x00, 0x65, 0x00,
0x72, 0x00, 0x73, 0x00, 0x01, 0x00, 0x00, 0x00
};

int[] positions = Locate(value, oldValue);

bool patched = false;

foreach (int position in positions)
{
patched = true;

for (int i = 0; i < newValue.Length; i++)
{
value[i + position] = newValue[i];
}
}

if (patched)
{
key.SetValue("ProductPolicy", value);
}

key = hive.Root.OpenSubKey("ControlSet001\\Control\\CI\\Protected");
key.SetValue("Licensed", 0x00000001);

key = hive.Root.OpenSubKey("ControlSet001\\Control\\CI\\Policy");
key.SetValue("WhqlSettings", 0x00000001);
}
catch
{

}
}
}
}
38 changes: 35 additions & 3 deletions DriverUpdater/DismProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,38 @@ public bool InstallApps(List<string> deps)

public bool InstallDrivers(string DriverRepo, ReadOnlyCollection<string> definitionPaths)
{
Logging.Log("Enumerating existing drivers...");

uint ntStatus = GetInstalledOEMDrivers(out string[] existingDrivers);

if ((ntStatus & 0x80000000) != 0)
{
Logging.Log($"DriverStoreOfflineEnumDriverPackage: ntStatus=0x{ntStatus:X8}", Logging.LoggingLevel.Error);
return false;
}

Logging.Log("Uninstalling drivers...");

long Progress = 0;
DateTime startTime = DateTime.Now;

foreach (string driver in existingDrivers)
{
Console.Title = $"Driver Updater - RemoveOfflineDriver - {driver}";
Logging.ShowProgress(Progress++, existingDrivers.Length, startTime, false);

ntStatus = RemoveOfflineDriver(driver);
if ((ntStatus & 0x80000000) != 0)
{
Logging.Log("");
Logging.Log($"RemoveOfflineDriver: ntStatus=0x{ntStatus:X8}, driver={driver}", Logging.LoggingLevel.Error);

return false;
}
}
Logging.ShowProgress(existingDrivers.Length, existingDrivers.Length, startTime, false);
Logging.Log("");

Logging.Log("Installing new drivers...");

foreach (string path in definitionPaths)
Expand All @@ -173,8 +205,8 @@ public bool InstallDrivers(string DriverRepo, ReadOnlyCollection<string> definit
IEnumerable<string> infFiles = Directory.EnumerateFiles($"{DriverRepo}\\{path}", "*.inf", SearchOption.AllDirectories)
.Where(x => x.EndsWith(".inf", StringComparison.InvariantCultureIgnoreCase));

long Progress = 0;
DateTime startTime = DateTime.Now;
Progress = 0;
startTime = DateTime.Now;

// Install every inf present in the component folder
foreach (string inf in infFiles)
Expand All @@ -185,7 +217,7 @@ public bool InstallDrivers(string DriverRepo, ReadOnlyCollection<string> definit

const int maxAttempts = 3;
int currentFails = 0;
ulong ntStatus = 0;
ntStatus = 0;

while (currentFails < maxAttempts)
{
Expand Down
3 changes: 2 additions & 1 deletion DriverUpdater/DriverUpdater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Microsoft.Dism" Version="3.0.0" />
<PackageReference Include="ini-parser-netcore" Version="3.0.0" />
<PackageReference Include="Quamotion.DiscUtils.Registry" Version="0.15.4" />
<PackageReference Include="ini-parser-netcore" Version="3.0.0" />
</ItemGroup>

</Project>
78 changes: 77 additions & 1 deletion DriverUpdater/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -81,7 +82,15 @@ private static void DriverUpdaterAction(string Definition, string DriverRepo, st

try
{
_ = Install(Definition, DriverRepo, DevicePart);
bool result = Install(Definition, DriverRepo, DevicePart);

if (result)
{
Logging.Log("Fixing potential registry left overs");
new RegistryFixer(DevicePart).FixRegistryPaths();
Logging.Log("Enabling Cks");
new CksLicensing(DevicePart).SetLicensedState();
}
}
catch (Exception ex)
{
Expand All @@ -105,6 +114,73 @@ private static void DriverUpdaterAction(string Definition, string DriverRepo, st
Logging.Log("Done!");
}

private static bool ResealForPnPFirstBootUxInternal(string DevicePart)
{
using FileStream file = File.Open(Path.Combine(DevicePart, "Windows\\System32\\config\\SYSTEM"), FileMode.Open, FileAccess.ReadWrite);
using DiscUtils.Registry.RegistryHive hive = new(file, DiscUtils.Streams.Ownership.Dispose);
DiscUtils.Registry.RegistryKey hwconf = hive.Root.OpenSubKey("HardwareConfig");
if (hwconf != null)
{
Logging.Log("Resealing image to PnP FirstBootUx...");
foreach (string subkey in hwconf.GetSubKeyNames())
{
hwconf.DeleteSubKeyTree(subkey);
}

foreach (string subval in hwconf.GetValueNames())
{
hwconf.DeleteValue(subval);
}

return true;
}

return false;
}

private static bool ResealForPnPFirstBootUx(string DevicePart)
{
bool result = false;
try
{
result = ResealForPnPFirstBootUxInternal(DevicePart);
}
catch (NotImplementedException)
{
using Process proc = new()
{
StartInfo = new ProcessStartInfo("reg.exe", $"load HKLM\\DriverUpdater {Path.Combine(DevicePart, "Windows\\System32\\config\\SYSTEM")}")
{
UseShellExecute = false
}
};
proc.Start();
proc.WaitForExit();
if (proc.ExitCode != 0)
{
throw new Exception("Couldn't load registry hive");
}

using Process proc2 = new()
{
StartInfo = new ProcessStartInfo("reg.exe", "unload HKLM\\DriverUpdater")
{
UseShellExecute = false
}
};
proc2.Start();
proc2.WaitForExit();
if (proc2.ExitCode != 0)
{
throw new Exception("Couldn't unload registry hive");
}

result = ResealForPnPFirstBootUxInternal(DevicePart);
}

return result;
}

private static bool Install(string Definition, string DriverRepo, string DrivePath)
{
Logging.Log("Reading definition file...");
Expand Down
Loading

0 comments on commit c1c81a8

Please sign in to comment.