driver-updater

Windows app that checks Windows Update and hardware vendors for newer drivers and silently installs the driver.
git clone https://dittus.dev/git/driver-updater.git
Log | Files | Refs | README | Download

Intel.cs (5322B)


      1 using System.IO;
      2 using System.IO.Compression;
      3 using System.IO.Enumeration;
      4 using System.Text.Json;
      5 
      6 namespace DriverUpdater;
      7 
      8 // Checks Intel's driver catalog. It's the feed Intel Driver & Support Assistant reads: a zip holding a
      9 // JSON list of driver packages, each with the hardware ID patterns it supports. It covers graphics,
     10 // wireless, and Ethernet drivers.
     11 //
     12 // Intel rows are check only. Intel packs graphics, Wi-Fi, and Bluetooth drivers in three different
     13 // installer formats, and installing the driver alone needs unpacking code for each. That waits for
     14 // a machine with Intel hardware to test it on.
     15 static class Intel
     16 {
     17     const string CatalogUrl = "https://dsadata.intel.com/data/en";
     18 
     19     public static async Task<List<DriverUpdate>> CheckAsync(IReadOnlyList<InstalledDevice> installed)
     20     {
     21         // Skip the download on machines with no Intel PCI devices (VEN_8086) or Intel Bluetooth (VID_8087).
     22         bool hasIntelHardware = installed.Any(d =>
     23             d.IsFromVendor("8086") || d.DeviceId.StartsWith(@"USB\VID_8087&", StringComparison.OrdinalIgnoreCase));
     24         if (!hasIntelHardware)
     25             return [];
     26 
     27         var packages = await DownloadCatalogAsync();
     28 
     29         // Packages list the Windows releases they support, like "windows-11-25h2-64". Matching on
     30         // the major version means a new Windows release works before Intel adds it to the list.
     31         string windows = Environment.OSVersion.Version.Build >= 22000 ? "windows-11-" : "windows-10-";
     32 
     33         var updates = new List<DriverUpdate>();
     34         var packagesSeen = new HashSet<int>();
     35         foreach (var device in installed)
     36         {
     37             var match = NewestPackageFor(device, packages, windows);
     38 
     39             // One installer covers every device in a package, so list each package once.
     40             if (match is not var (package, file, version) || !packagesSeen.Add(package.Id))
     41                 continue;
     42 
     43             bool isNewer = !Version.TryParse(device.DriverVersion, out var installedVersion)
     44                 || version > installedVersion;
     45             updates.Add(new DriverUpdate
     46             {
     47                 Device = device.Name,
     48                 Source = "Intel",
     49                 InstalledVersion = device.DriverVersion,
     50                 AvailableVersion = version.ToString(),
     51                 ReleaseDate = package.DisplayReleaseDate,
     52                 SizeBytes = file.Size,
     53                 NotesUrl = new Uri(package.Url),
     54                 Status = isNewer ? "Newer on Intel's site" : "Up to date",
     55             });
     56         }
     57         return updates;
     58     }
     59 
     60     static async Task<List<Package>> DownloadCatalogAsync()
     61     {
     62         byte[] zipBytes = await Web.Client.GetByteArrayAsync(CatalogUrl);
     63         using var zip = new ZipArchive(new MemoryStream(zipBytes));
     64         await using var json = zip.GetEntry("software-configurations.json")!.Open();
     65         return await JsonSerializer.DeserializeAsync<List<Package>>(json) ?? [];
     66     }
     67 
     68     // The newest package with a component whose patterns match this device. Some patterns appear in
     69     // more than one package, and "Historical" packages hold old releases kept for reference.
     70     static (Package, PackageFile, Version)? NewestPackageFor(
     71         InstalledDevice device, List<Package> packages, string windows)
     72     {
     73         // "PCI\VEN_8086&DEV_51F0&SUBSYS_00948086&REV_01\3&11583659&0&A3" has the bus, the hardware
     74         // fields, and the instance path. Only the hardware fields take part in matching.
     75         string[] deviceIdParts = device.DeviceId.Split('\\');
     76         if (deviceIdParts.Length < 3)
     77             return null;
     78         string[] deviceFields = deviceIdParts[1].Split('&');
     79 
     80         var candidates =
     81             from package in packages
     82             where !package.Name.StartsWith("Historical")
     83             let file = package.Files.FirstOrDefault(f =>
     84                 f.OperatingSystems.Any(os => os.StartsWith(windows) && os.EndsWith("-64")))
     85             where file is not null
     86             from component in package.Components
     87             where component.DetectionType == "Default"
     88             where Version.TryParse(component.Version, out _)
     89             where component.DetectionValues?.Any(pattern => Matches(deviceFields, pattern)) == true
     90             select (package, file, Version.Parse(component.Version!));
     91 
     92         return candidates.Any() ? candidates.MaxBy(candidate => candidate.Item3) : null;
     93     }
     94 
     95     // Patterns look like "VEN_8086&DEV_51F0&SUBSYS_*8086". They're compared field by field, so a
     96     // wildcard can't spill from one field into the next. A pattern can have fewer fields than the ID.
     97     static bool Matches(string[] deviceFields, string pattern)
     98     {
     99         string[] patternFields = pattern.Split('&');
    100         return patternFields.Length <= deviceFields.Length
    101             && patternFields.Zip(deviceFields).All(pair =>
    102                 FileSystemName.MatchesSimpleExpression(pair.First, pair.Second, ignoreCase: true));
    103     }
    104 
    105     // The parts of the catalog the app reads.
    106     record Package(int Id, string Name, DateTime? DisplayReleaseDate, string Url,
    107         List<PackageFile> Files, List<Component> Components);
    108     record PackageFile(string Url, long Size, List<string> OperatingSystems);
    109     record Component(string? Version, string DetectionType, List<string>? DetectionValues);
    110 }