Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: expand wdk-sys coverage to include hid-related headers #260

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
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: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
"./tests/umdf-driver-workspace/Cargo.toml",
"./tests/wdk-macros-tests/Cargo.toml",
"./tests/wdk-sys-tests/Cargo.toml",
]
}
],
"rust-analyzer.cargo.features": "all"
}
23 changes: 12 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ wdk-sys = { path = "crates/wdk-sys", version = "0.3.0" }

# External Crates
anyhow = "1.0.86"
bindgen = "0.69.4"
bindgen = "0.69.5"
camino = "1.1.9"
cargo_metadata = "0.18.1"
cc = "1.1.0"
Expand Down
5 changes: 5 additions & 0 deletions crates/wdk-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ windows = { workspace = true, features = [
[dev-dependencies]
windows = { workspace = true, features = ["Win32_UI_Shell"] }

[features]
default = []

hid = []

[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = ["cfg(wdk_build_unstable)", "cfg(skip_umdf_static_crt_check)"]
Expand Down
18 changes: 3 additions & 15 deletions crates/wdk-build/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ pub trait BuilderExt {
///
/// Implementation may return `wdk_build::ConfigError` if it fails to create
/// a builder
fn wdk_default(
c_header_files: Vec<&str>,
config: impl Borrow<Config>,
) -> Result<Builder, ConfigError>;
fn wdk_default(config: impl Borrow<Config>) -> Result<Builder, ConfigError>;
}

#[derive(Debug)]
Expand All @@ -39,19 +36,10 @@ impl BuilderExt for Builder {
///
/// Will return `wdk_build::ConfigError` if any of the resolved include or
/// library paths do not exist
fn wdk_default(
c_header_files: Vec<&str>,
config: impl Borrow<Config>,
) -> Result<Self, ConfigError> {
fn wdk_default(config: impl Borrow<Config>) -> Result<Self, ConfigError> {
let config = config.borrow();

let mut builder = Self::default();

for c_header in c_header_files {
builder = builder.header(c_header);
}

builder = builder
let builder = Self::default()
.use_core() // Can't use std for kernel code
.derive_default(true) // allows for default initializing structs
// CStr types are safer and easier to work with when interacting with string constants
Expand Down
138 changes: 135 additions & 3 deletions crates/wdk-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ impl Config {
Ok(())
}

// TODO: deprecate in favor of include_paths()
/// Return header include paths required to build and link based off of the
/// configuration of `Config`
///
Expand Down Expand Up @@ -411,6 +412,7 @@ impl Config {
Ok(include_paths)
}

// TODO: deprecate in favor of library_paths()
/// Return library include paths required to build and link based off of
/// the configuration of [`Config`].
///
Expand Down Expand Up @@ -498,6 +500,7 @@ impl Config {
Ok(library_paths)
}

// TODO: Deprecate in favor of preprocessor_definitions_iter
/// Return an iterator of strings that represent compiler definitions
/// derived from the `Config`
pub fn get_preprocessor_definitions_iter(
Expand Down Expand Up @@ -541,10 +544,13 @@ impl Config {
.chain(
match self.driver_config {
DriverConfig::Wdm => {
vec![]
vec![
("_KERNEL_MODE", None), // Normally defined by msvc via /kernel flag
]
}
DriverConfig::Kmdf(kmdf_config) => {
let mut kmdf_definitions = vec![
("_KERNEL_MODE", None), // Normally defined by msvc via /kernel flag
("KMDF_VERSION_MAJOR", Some(kmdf_config.kmdf_version_major)),
(
"KMDF_VERSION_MINOR",
Expand Down Expand Up @@ -623,6 +629,134 @@ impl Config {
.map(std::string::ToString::to_string)
}

pub fn base_headers(&self) -> impl Iterator<Item = String> {
match &self.driver_config {
DriverConfig::Wdm | DriverConfig::Kmdf(_) => {
vec!["ntifs.h", "ntddk.h"]
}
DriverConfig::Umdf(_) => {
vec!["windows.h"]
}
}
.into_iter()
.map(std::string::ToString::to_string)
}

pub fn wdf_headers(&self) -> impl Iterator<Item = String> {
["wdf.h"].into_iter().map(std::string::ToString::to_string)
}

#[cfg(feature = "hid")]
wmmc88 marked this conversation as resolved.
Show resolved Hide resolved
pub fn hid_headers(&self) -> impl Iterator<Item = String> {
// HID Headers list from https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/_hid/
{
let mut hid_headers = vec!["hidclass.h", "hidsdi.h", "hidpi.h", "vhf.h"];
if let DriverConfig::Wdm | DriverConfig::Kmdf(_) = self.driver_config {
hid_headers.extend(["hidpddi.h", "hidport.h", "kbdmou.h", "ntdd8042.h"]);
}
if let DriverConfig::Kmdf(_) = self.driver_config {
hid_headers.extend(["HidSpiCx/1.0/hidspicx.h"]);
}
hid_headers
}
.into_iter()
.map(std::string::ToString::to_string)
}

pub fn bindgen_base_header_contents(&self) -> String {
let mut header_contents = self.base_headers().fold(String::new(), |mut acc, header| {
acc.push_str(r#"#include ""#);
acc.push_str(&header);
acc.push_str("\"\n");
acc
});

if let DriverConfig::Wdm | DriverConfig::Kmdf(_) = self.driver_config {
// TODO: Why is there no definition for this struct? Maybe blocklist this struct
// in bindgen.
header_contents.push_str(
r"
typedef union _KGDTENTRY64
{
struct
{
unsigned short LimitLow;
unsigned short BaseLow;
union
{
struct
{
unsigned char BaseMiddle;
unsigned char Flags1;
unsigned char Flags2;
unsigned char BaseHigh;
} Bytes;
struct
{
unsigned long BaseMiddle : 8;
unsigned long Type : 5;
unsigned long Dpl : 2;
unsigned long Present : 1;
unsigned long LimitHigh : 4;
unsigned long System : 1;
unsigned long LongMode : 1;
unsigned long DefaultBig : 1;
unsigned long Granularity : 1;
unsigned long BaseHigh : 8;
} Bits;
};
unsigned long BaseUpper;
unsigned long MustBeZero;
};
unsigned __int64 Alignment;
} KGDTENTRY64, *PKGDTENTRY64;

typedef union _KIDTENTRY64
{
struct
{
unsigned short OffsetLow;
unsigned short Selector;
unsigned short IstIndex : 3;
unsigned short Reserved0 : 5;
unsigned short Type : 5;
unsigned short Dpl : 2;
unsigned short Present : 1;
unsigned short OffsetMiddle;
unsigned long OffsetHigh;
unsigned long Reserved1;
};
unsigned __int64 Alignment;
} KIDTENTRY64, *PKIDTENTRY64;
",
);
}

header_contents
}

pub fn bindgen_wdf_header_contents(&self) -> Option<String> {
wmmc88 marked this conversation as resolved.
Show resolved Hide resolved
if let DriverConfig::Kmdf(_) | DriverConfig::Umdf(_) = self.driver_config {
return Some(self.wdf_headers().fold(String::new(), |mut acc, header| {
acc.push_str(r#"#include ""#);
acc.push_str(&header);
acc.push_str("\"\n");
acc
}));
}
None
}

#[cfg(feature = "hid")]
pub fn bindgen_hid_header_contents(&self) -> String {
self.hid_headers().fold(String::new(), |mut acc, header| {
acc.push_str(r#"#include ""#);
acc.push_str(&header);
acc.push_str("\"\n");
acc
})
}

/// Configure a Cargo build of a library that depends on the WDK. This
/// emits specially formatted prints to Cargo based on this [`Config`].
///
Expand Down Expand Up @@ -839,8 +973,6 @@ impl CpuArchitecture {
}

/// Converts from a cargo-provided [`std::str`] to a [`CpuArchitecture`].
///
/// #
#[must_use]
pub fn try_from_cargo_str<S: AsRef<str>>(cargo_str: S) -> Option<Self> {
// Specifically not using the [`std::convert::TryFrom`] trait to be more
Expand Down
4 changes: 4 additions & 0 deletions crates/wdk-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ anyhow.workspace = true
bindgen.workspace = true
cargo_metadata.workspace = true
cc.workspace = true
cfg-if.workspace = true
lazy_static.workspace = true
serde_json.workspace = true
thiserror.workspace = true
Expand All @@ -35,6 +36,9 @@ wdk-macros.workspace = true

[features]
default = []

hid = ["wdk-build/hid"]

nightly = ["wdk-macros/nightly"]
test-stubs = []

Expand Down
Loading
Loading