#[derive(Debug)]
pub struct Vendored {
pub file: &'static str,
pub source: &'static str,
pub sha256: &'static str,
pub len: usize,
pub note: &'static str,
}
const RELEASE: &str = "host API release 6.0.0 (2026-07-21), archived in this organisation's \
private hardware-support repository";
pub const VENDORED: &[Vendored] = &[
Vendored {
file: "vendor/driver-x86_64-windows.bin",
source: RELEASE,
sha256: "51fd8539027e23600bfc6c9427e6f0a74f31f19046732caf39e850e5a09ec8f6",
len: 2_117_704,
note: "verified byte-identical to the copy installed on the instrument bench, so the \
hardware results recorded against that machine are results for THIS blob",
},
Vendored {
file: "vendor/driver-x86_64-linux.bin",
source: RELEASE,
sha256: "4033682b24d877ffa00e4efb9448541a6edf13f347eba6ea286d460ff9ef25f6",
len: 2_328_536,
note: "needs glibc >= 2.34, libstdc++ >= 3.4.30 and libudev at run time; glibc-only, so a \
musl system (Alpine) cannot load it. NOT exercised against a board — the bench \
workstation is Windows",
},
Vendored {
file: "vendor/driver-aarch64-linux.bin",
source: RELEASE,
sha256: "ef2afb3a3660d107fe94b3d307614a6b8a2a3fa467a7761d7afdea483e03a3dd",
len: 2_350_768,
note: "needs glibc >= 2.34 and libudev at run time; glibc-only, so a musl system (Alpine) \
cannot load it. NOT exercised against a board",
},
Vendored {
file: "vendor/driver-aarch64-macos.bin",
source: "the arm64 slice of the universal library in the release below",
sha256: "a1a4a16ccb18d5a68066d31b5608a82419f9ba939d3b44c3d30e5248f49b6607",
len: 2_088_080,
note: "sliced out of the universal binary, not shipped separately upstream; NOT exercised \
against a board",
},
];
#[cfg(feature = "bundled")]
pub(crate) use embed::staged_path;
#[cfg(feature = "bundled")]
mod embed {
use std::fs;
use std::io;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
static PART_SEQ: AtomicU64 = AtomicU64::new(0);
#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
pub(crate) const DRIVER: &[u8] = include_bytes!("../vendor/driver-x86_64-windows.bin");
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
pub(crate) const DRIVER: &[u8] = include_bytes!("../vendor/driver-x86_64-linux.bin");
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
pub(crate) const DRIVER: &[u8] = include_bytes!("../vendor/driver-aarch64-linux.bin");
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
pub(crate) const DRIVER: &[u8] = include_bytes!("../vendor/driver-aarch64-macos.bin");
#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
const DRIVER_FILE: &str = "vendor/driver-x86_64-windows.bin";
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
const DRIVER_FILE: &str = "vendor/driver-x86_64-linux.bin";
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
const DRIVER_FILE: &str = "vendor/driver-aarch64-linux.bin";
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
const DRIVER_FILE: &str = "vendor/driver-aarch64-macos.bin";
#[cfg(not(any(
all(target_os = "windows", target_arch = "x86_64"),
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "linux", target_arch = "aarch64"),
all(target_os = "macos", target_arch = "aarch64"),
)))]
compile_error!(
"`bundled` has no vendored device driver for this target. Vendored: Windows x86_64, \
Linux x86_64 and aarch64, and macOS aarch64. Build without the `bundled` feature and \
supply the driver in a directory ($KDI_DRIVER_DIR), or vendor the library for this \
target - see src/bundled.rs."
);
#[cfg(target_os = "windows")]
const STAGED_NAME: &str = "kdi_driver.dll";
#[cfg(target_os = "macos")]
const STAGED_NAME: &str = "libkdi_driver.dylib";
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
const STAGED_NAME: &str = "libkdi_driver.so";
pub(crate) fn staged_path() -> io::Result<PathBuf> {
let sha16 = &super::VENDORED
.iter()
.find(|v| v.file == DRIVER_FILE)
.expect("VENDORED has a row for this platform's driver")
.sha256[..16];
let dir = std::env::temp_dir().join(format!("kdi-driver-{sha16}"));
let path = dir.join(STAGED_NAME);
create_dir_private(&dir)?;
trusted_dir(&dir)?;
if is_staged(&path) {
return Ok(path);
}
let n = PART_SEQ.fetch_add(1, Ordering::Relaxed);
let part = dir.join(format!("{STAGED_NAME}.{}.{n}.part", std::process::id()));
fs::write(&part, DRIVER)?;
#[cfg(unix)]
fs::set_permissions(
&part,
<fs::Permissions as std::os::unix::fs::PermissionsExt>::from_mode(0o755),
)?;
if let Err(e) = fs::rename(&part, &path) {
let _ = fs::remove_file(&part);
if !is_staged(&path) {
return Err(io::Error::new(
e.kind(),
format!(
"could not stage the device driver at {}: {e}",
path.display()
),
));
}
}
Ok(path)
}
fn create_dir_private(dir: &std::path::Path) -> io::Result<()> {
let mut b = fs::DirBuilder::new();
b.recursive(true);
#[cfg(unix)]
<fs::DirBuilder as std::os::unix::fs::DirBuilderExt>::mode(&mut b, 0o700);
b.create(dir)
}
#[cfg(any(unix, test))]
fn trust_reason(is_dir: bool, mode: u32, uid: u32, euid: u32) -> Option<&'static str> {
if !is_dir {
return Some("not a directory");
}
if mode & 0o022 != 0 {
return Some("writable by others");
}
if uid != euid {
return Some("owned by another user");
}
None
}
#[cfg(unix)]
fn trusted_dir(dir: &std::path::Path) -> io::Result<()> {
use std::os::unix::fs::{MetadataExt, PermissionsExt};
let md = fs::symlink_metadata(dir)?;
let why = trust_reason(
md.file_type().is_dir(),
md.permissions().mode(),
md.uid(),
euid(),
);
if let Some(why) = why {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
format!(
"refusing to load the device driver from {}: it is {why} and so another user \
on this machine could choose what is loaded. Remove it, or point \
$KDI_DRIVER_DIR at a directory you control.",
dir.display(),
),
));
}
Ok(())
}
#[cfg(unix)]
fn euid() -> u32 {
extern "C" {
fn geteuid() -> u32;
}
unsafe { geteuid() }
}
#[cfg(not(unix))]
fn trusted_dir(_dir: &std::path::Path) -> io::Result<()> {
Ok(())
}
fn is_staged(path: &std::path::Path) -> bool {
fs::metadata(path).is_ok_and(|m| m.len() == DRIVER.len() as u64)
}
#[cfg(test)]
mod tests {
#[test]
fn staging_is_idempotent_and_complete() {
let a = super::staged_path().expect("stage the driver");
let b = super::staged_path().expect("stage it again");
assert_eq!(a, b);
assert_eq!(
std::fs::read(&b).expect("read it back").len(),
super::DRIVER.len()
);
let name = b.file_name().unwrap().to_string_lossy().to_lowercase();
assert!(name.contains("kdi"), "staged as {name}");
let dir = a.parent().unwrap().file_name().unwrap().to_string_lossy();
assert!(
dir.starts_with("kdi-driver-") && dir.len() == "kdi-driver-".len() + 16,
"content-addressed dir, got {dir}"
);
}
#[test]
fn trust_reason_catches_mode_and_owner() {
assert_eq!(
super::trust_reason(false, 0o700, 1, 1),
Some("not a directory")
);
assert_eq!(
super::trust_reason(true, 0o777, 1, 1),
Some("writable by others")
);
assert_eq!(
super::trust_reason(true, 0o755, 2, 1),
Some("owned by another user")
);
assert_eq!(super::trust_reason(true, 0o755, 1, 1), None);
assert_eq!(super::trust_reason(true, 0o700, 1, 1), None);
}
#[cfg(unix)]
#[test]
fn a_world_writable_staging_directory_is_refused() {
use std::os::unix::fs::PermissionsExt;
let dir = std::env::temp_dir().join(format!("kdi-trust-test-{}", std::process::id()));
std::fs::create_dir_all(&dir).expect("make the directory");
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700))
.expect("lock it down");
super::trusted_dir(&dir).expect("a private directory is fine");
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o777))
.expect("open it up");
let err = super::trusted_dir(&dir).expect_err("world-writable must be refused");
assert_eq!(err.kind(), std::io::ErrorKind::PermissionDenied);
assert!(err.to_string().contains("KDI_DRIVER_DIR"), "{err}");
std::fs::remove_dir_all(&dir).ok();
}
}
}