Rust port of the official Windows Driver Samples on Github. Leverages windows-drivers-rs

Overview

Rust Driver Samples

This is a Rust port of the driver samples from the original Windows Driver Samples on Github.

The repository provides examples and best practices for Windows driver development in Rust using crates from windows-drivers-rs.

Getting Started

Pre-requisites

Required

Invoke-RestMethod -Uri "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe" -OutFile "$env:USERPROFILE\Downloads\rustup-init.exe"
& "$env:USERPROFILE\Downloads\rustup-init.exe" -y

Rust Setup

Run the following commands after setting up Rust.

cargo install cargo-make --no-default-features --features tls-native

Note on arm64: ARM64 support for ring is not released yet, so TLS features must be disabled until arm64 is officially supported by ring (probably in 0.17.0 release)

Optional

These are not-required, but may make it easier to work in a rust environment:

cargo install cargo-expand cargo-edit cargo-workspaces

Documentation

cargo doc --document-private-items --open

Build and Test

Build

From an EWDK development command prompt, run:

cargo make

If build is successful, this will stamp the INF and create a CAT file placed with driver binary and INF in Package folder.

Install

One Time PC Setup

  1. If Bitlocker is enabled, suspend Bitlocker Example: manage-bde -protectors -disable C:

  2. Turn off Secure Boot via your UEFI/BIOS Settings Example: shutdown -r -o -t 0 then pick Advanced -> UEFI Settings

  3. If Bitlocker is enabled, suspend Bitlocker again Example: manage-bde -protectors -disable C:

  4. Turn on test signing bcdedit /set testsigning on

  5. Reboot shutdown -r -t 0

  6. Copy the following to the DUT (Device Under Test: the computer you want to test the driver on):

    1. .\target\..configuration..\package
    2. Path\To\Driver\DriverCertificate.cer
    3. The version of devgen.exe from the WDK Developer Tools that matches the archtecture of your DUT
    • Ex. C:\ewdk\Program Files\Windows Kits\10\Tools\10.0.22621.0\x64\devgen.exe
  7. Install the Certificate on the DUT:

    1. Double click the certificate
    2. Click Install Certificate
    3. Store Location: Local Machine -> Next
    4. Place all certificates in the following Store -> Browse -> Trusted Root Certification Authorities -> Ok -> Next
    5. Finish
  8. Install the driver from Admin Command Prompt:

    1. In the package directory, run: pnputil.exe /add-driver echo_2.inf /install
  9. Create a software device from Admin Command Prompt:

    1. In the directory that devgen.exe was copied to, run: devgen.exe /add /hardwareid "root\ECHO_2"

Test

  • To capture prints:
    • Start DebugView
      1. Enable Capture Kernel
      2. Enable Enable Verbose Kernel Output
    • Alternatively, you can see prints in an active Windbg session.
      1. Attach WinDBG
      2. ed nt!Kd_DEFAULT_Mask 0xFFFFFFFF

Usage

The echo driver can be tested by using the echo executable

  • Echoapp.exe --- Send single write and read request synchronously

  • Echoapp.exe -Async --- Send 100 reads and writes asynchronously

Exit the app anytime by pressing Ctrl-C

Windows driver development

Windows Driver Kit (WDK)

Take a look at the compilation of the new and changed driver-related content for Windows 11. Areas of improvement include camera, print, display, Near Field Communication (NFC), WLAN, Bluetooth, and more.

Find out what's new in the WDK

Windows Driver Frameworks

The Windows Driver Frameworks (WDF) are a set of libraries that make it simple to write high-quality device drivers.

WDF driver development guide

Samples

Use the samples in this repo to guide your Windows driver development. Whether you're just getting started or porting an older driver to the newest version of Windows, code samples are valuable guides on how to write drivers.

For information about important changes that need to be made to the WDK sample drivers before releasing device drivers based on the sample code, see the following topic:

From Sample Code to Production Driver - What to Change in the Samples

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

You might also like...
⚡️ Fast MagicString port driven by Rust and N-API

magic-string-rs 100% API compatible (port) MagicString by Rich-Harris implementation for Node and modern browsers, also, for rust, of course. Installa

This is a test to port C++'s ymfm and Rust's vgmplay to ESP32(Xtensa).
This is a test to port C++'s ymfm and Rust's vgmplay to ESP32(Xtensa).

m5stack-chipstream This is a test to port C++'s ymfm and Rust's vgmplay to ESP32(Xtensa). Still slow and impractical, depending on the sound chip you

Port of the fantastic Iconoir Icon Pack to Rust embedded devices, with a focus on speed, usability, and completeness.
Port of the fantastic Iconoir Icon Pack to Rust embedded devices, with a focus on speed, usability, and completeness.

embedded-iconoir - Icons for every device, ever. What is embedded-iconor? embedded-iconoir is a library that allows you to use Iconoir on embedded dev

A small utility for tracking the change in opening and closing of issues in a GitHub repo

A small utility for tracking the change in opening and closing of issues in a GitHub repo. This tool can be used to build visualizations for issue triage over time with the hope of motivating closing more issues than are opened.

Crate of GitHub’s collection of gitignores, embedded, automatically updated

Gitignores GitHub’s collection of gitignores, embedded, automatically updated. API documentation. Public Domain via CC0-1.0 (same as source data). MSR

Make the github cli even better with fuzzy finding
Make the github cli even better with fuzzy finding

github-repo-clone (grc) Github Repo Clone is a command line utility written in rust that leverages the power of fuzzy finding with the github cli Usag

Self-Hosted alternative to GitHub Gists

Gists Self-Hosted GitHub Gists Features Upload code snippets Syntax Highlighting Comments Versioning through Git Fork gists Gist privacy: public, unli

Github mirror of codeberg repo. Monitor live bandwidth usage/ network speed on PC. Native version also available for Android, separately.
Github mirror of codeberg repo. Monitor live bandwidth usage/ network speed on PC. Native version also available for Android, separately.

Netspeed Monitor Netspeed is a cross-platform desktop application that shows the live upload speed, download speed and day's usage as an overlay. Feat

runs init, preview and apply on pulumi stacks right in your Github Actions. Inspired from Atalantis for Terraform

pulumi-actions runs init, preview and apply on pulumi stacks right in your Github-Actions. Inspired from Atlantis for Terraform PREVIEW Release Curren

Comments
  • error

    error

    **********************************************************************
    ** Visual Studio 2022 Developer Command Prompt v17.7.4
    ** Copyright (c) 2022 Microsoft Corporation
    **********************************************************************
    [vcvarsall.bat] Environment initialized for: 'x64'
    
    C:\Program Files\Microsoft Visual Studio\2022\Enterprise>cd /d D:\code\rust\code\windows\Windows-rust-driver-samples
    
    D:\code\rust\code\windows\Windows-rust-driver-samples>cargo build
       Compiling wdk-sys v0.1.0
    error: failed to run custom build command for `wdk-sys v0.1.0`
    
    Caused by:
      process didn't exit successfully: `D:\code\rust\code\windows\Windows-rust-driver-samples\target\debug\build\wdk-sys-109708047c4dbb69\build-script-build` (exit code: 101)
      --- stderr
      thread 'main' panicked at 'WDKContentRoot should be able to be detected. Ensure that the WDK is installed, or that the environment setup scripts in the eWDK have been run.', C:\Users\Administrator\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wdk-build-0.1.0\src\lib.rs:111:64
      note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    
    D:\code\rust\code\windows\Windows-rust-driver-samples>
    
    opened by kouzhudong 5
  • error: linking with `link.exe` failed: exit code: 1120

    error: linking with `link.exe` failed: exit code: 1120

    D:\code\rust\code\windows\Windows-rust-driver-samples\general\echo\kmdf\driver\DriverSync>cargo +nightly make
    [cargo-make] INFO - cargo make 0.37.1
    [cargo-make] INFO - Calling cargo metadata to extract project info
    [cargo-make] INFO - Cargo metadata done
    [cargo-make] INFO - Project: echo-2
    [cargo-make] INFO - Build File: Makefile.toml
    [cargo-make] INFO - Task: default
    [cargo-make] INFO - Profile: development
    [cargo-make] INFO - Running Task: legacy-migration
    [cargo-make] INFO - Execute Command: "cargo" "fmt"
    [cargo-make] INFO - Skipping Task: format-toml-conditioned-flow
    [cargo-make] INFO - Execute Command: "cargo" "build" "--all-features"
       Compiling proc-macro2 v1.0.67
       Compiling unicode-ident v1.0.12
       Compiling windows_x86_64_msvc v0.48.5
       Compiling winapi v0.3.9
       Compiling once_cell v1.18.0
       Compiling glob v0.3.1
       Compiling rustversion v1.0.14
       Compiling lazy_static v1.4.0
       Compiling libc v0.2.148
       Compiling rustix v0.38.13
       Compiling prettyplease v0.2.15
       Compiling clang-sys v1.6.1
       Compiling bitflags v2.4.0
       Compiling regex-syntax v0.7.5
       Compiling log v0.4.20
       Compiling windows-targets v0.48.5
       Compiling windows-sys v0.48.0
       Compiling memchr v2.6.3
       Compiling minimal-lexical v0.2.1
       Compiling serde v1.0.188
       Compiling quote v1.0.33
       Compiling syn v2.0.33
       Compiling nom v7.1.3
       Compiling thiserror v1.0.48
       Compiling bindgen v0.68.1
       Compiling libloading v0.7.4
       Compiling either v1.9.0
       Compiling serde_json v1.0.107
       Compiling wdk-build v0.1.0
       Compiling windows-core v0.51.1
       Compiling itoa v1.0.9
       Compiling regex-automata v0.3.8
       Compiling rustc-hash v1.1.0
       Compiling shlex v1.2.0
       Compiling errno v0.3.3
       Compiling home v0.5.5
       Compiling cexpr v0.6.0
       Compiling which v4.4.2
       Compiling lazycell v1.3.0
       Compiling ryu v1.0.15
       Compiling peeking_take_while v0.1.2
       Compiling windows v0.51.1
       Compiling cfg-if v1.0.0
       Compiling tracing-core v0.1.31
       Compiling overload v0.1.1
       Compiling thread_local v1.1.7
       Compiling regex v1.9.5
       Compiling tracing-log v0.1.3
       Compiling nu-ansi-term v0.46.0
       Compiling sharded-slab v0.1.4
       Compiling smallvec v1.11.0
       Compiling spin v0.5.2
       Compiling paste v1.0.14
       Compiling tracing-subscriber v0.3.17
       Compiling wdk-panic v0.1.0
       Compiling serde_derive v1.0.188
       Compiling thiserror-impl v1.0.48
       Compiling wdk-macros v0.1.0
       Compiling wdk-sys v0.1.0
       Compiling wdk v0.1.0
       Compiling echo-2 v0.1.0 (D:\code\rust\code\windows\Windows-rust-driver-samples\general\echo\kmdf\driver\DriverSync)
       Compiling wdk-alloc v0.1.0
        Finished dev [unoptimized + debuginfo] target(s) in 3m 05s
    [cargo-make] INFO - Execute Command: "cargo" "test" "--all-features"
       Compiling spin v0.5.2
       Compiling wdk-panic v0.1.0
       Compiling lazy_static v1.4.0
       Compiling wdk-sys v0.1.0
       Compiling wdk-alloc v0.1.0
       Compiling wdk v0.1.0
       Compiling echo-2 v0.1.0 (D:\code\rust\code\windows\Windows-rust-driver-samples\general\echo\kmdf\driver\DriverSync)
    error: linking with `link.exe` failed: exit code: 1120
      |
      = note: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\VC\\Tools\\MSVC\\14.37.32822\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\rustcmf3ZsW\\symbols.o" "D:\\code\\rust\\code\\windows\\Windows-rust-driver-samples\\target\\debug\\deps\\echo_2-dfe775342f36be9b.test-4e25e9a00f55b6b4.test.23704e6cc03fe591-cgu.0.rcgu.o.rcgu.o" "/LIBPATH:D:\\code\\rust\\code\\windows\\Windows-rust-driver-samples\\target\\debug\\deps" "/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.22621.0\\km\\x64" "/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\wdf\\kmdf\\x64\\1.33" "/LIBPATH:C:\\Users\\Administrator\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "C:\\Users\\Administrator\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-6856e76ba99f7e60.rlib" "kernel32.lib" "kernel32.lib" "advapi32.lib" "bcrypt.lib" "kernel32.lib" "ntdll.lib" "userenv.lib" "ws2_32.lib" "kernel32.lib" "ws2_32.lib" "kernel32.lib" "msvcrt.lib" "BufferOverflowFastFailK.lib" "ntoskrnl.lib" "hal.lib" "wmilib.lib" "WdfLdr.lib" "WdfDriverEntry.lib" "/NXCOMPAT" "/LIBPATH:C:\\Users\\Administrator\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "/OUT:D:\\code\\rust\\code\\windows\\Windows-rust-driver-samples\\target\\debug\\deps\\echo_2-dfe775342f36be9b.exe" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\\Users\\Administrator\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\Administrator\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\Administrator\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:C:\\Users\\Administrator\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis" "/NXCOMPAT" "/DYNAMICBASE" "/MAP" "/MAPINFO:EXPORTS" "/OPT:REF,ICF" "/INTEGRITYCHECK" "/MANIFEST:NO" "/DRIVER" "/NODEFAULTLIB" "/SUBSYSTEM:NATIVE" "/KERNEL" "/ENTRY:FxDriverEntry"
      = note: msvcrt.lib(tlssup.obj) : warning LNK4257: 未对内核模式编译对象文件;映像可能不会运行
              msvcrt.lib(std_type_info_static.obj) : warning LNK4257: 未对内核模式编译对象文件;映像可能不会运行
              msvcrt.lib(delete_scalar_size.obj) : warning LNK4257: 未对内核模式编译对象文件;映像可能不会运行
              msvcrt.lib(delete_scalar.obj) : warning LNK4257: 未对内核模式编译对象文件;映像可能不会运行
              msvcrt.lib(gs_support.obj) : warning LNK4257: 未对内核模式编译对象文件;映像可能不会运行
              msvcrt.lib(guard_support.obj) : warning LNK4257: 未对内核模式编译对象文件;映像可能不会运行
              msvcrt.lib(gs_cookie.obj) : warning LNK4257: 未对内核模式编译对象文件;映像可能不会运行
              msvcrt.lib(loadcfg.obj) : warning LNK4257: 未对内核模式编译对象文件;映像可能不会运行
              echo_2-dfe775342f36be9b.test-4e25e9a00f55b6b4.test.23704e6cc03fe591-cgu.0.rcgu.o.rcgu.o : error LNK2019: 无法解析的外部符号 floor,函数 _ZN52_$LT$$u5b$f64$u5d$$u20$as$u20$test..stats..Stats$GT$9quartiles17h70ec751af0614305E 中引用了该符号
              echo_2-dfe775342f36be9b.test-4e25e9a00f55b6b4.test.23704e6cc03fe591-cgu.0.rcgu.o.rcgu.o : error LNK2019: 无法解析的外部符号 _CxxThrowException,函数 __rust_start_panic 中引用了该符号
              msvcrt.lib(delete_scalar.obj) : error LNK2019: 无法解析的外部符号 free,函数 "void __cdecl operator delete(void *)" (??3@YAXPEAX@Z) 中引用了该符号
              D:\code\rust\code\windows\Windows-rust-driver-samples\target\debug\deps\echo_2-dfe775342f36be9b.exe : fatal error LNK1120: 3 个无法解析的外部命令
    
    
    error: could not compile `echo-2` (lib test) due to previous error
    [cargo-make] ERROR - Error while executing command, exit code: 101
    [cargo-make] WARN - Build Failed.
    
    D:\code\rust\code\windows\Windows-rust-driver-samples\general\echo\kmdf\driver\DriverSync>
    
    opened by kouzhudong 1
Owner
Microsoft
Open source projects and samples from Microsoft
Microsoft
Rust drivers for the SIM7000 series of modems

sim7000 This crate provides drivers for the SIM7000 series of chips. The current code implements enough commands to bring up a TCP connection and run

The Techno Creatives 11 Dec 29, 2022
Rusty Rootkit: Windows Kernel Driver in Rust for Red Teamers

Windows Kernel Driver in Rust (Rusty Rootkit) for Red Teamers Features (Development in progress) Protect / unprotect process (Done) Elevate to NT AUTH

null 283 Jan 1, 2023
A cross-platform serial port library in Rust. Provides a blocking I/O interface and port enumeration including USB device information.

Note: This is a fork of the original serialport-rs project on GitLab. Please note there have been some changes to both the supported targets and which

Serialport 128 Jan 4, 2023
Rustymind is a driver and parser for NeuroSky MindWave EEG headset written in pure Rust.

Rustymind is a driver and parser for NeuroSky MindWave EEG headset written in pure Rust. You can use it to connect, interact, and plot real time data from the headset.

Junjun Dong 34 Sep 13, 2022
TI LDC1312/LDC1314/LDC1612/LDC1614 inductance-to-digital converter driver for Rust embedded-hal

ldc1x1x Rust embedded-hal 1.x driver for Texas Instruments (TI) I²C inductance-to-digital converters (LDC): LDC1312/LDC1314, LDC1612/LDC1614. Includes

null 2 Oct 2, 2022
An embedded-hal driver for the TT21100 multi-touch touchscreen controller

tt21100 An embedded-hal driver for the TT21100 multi-touch touchscreen controller. If there is a feature which has not yet been implemented and which

Jesse Braham 5 Jan 9, 2023
BlueR - Official BlueZ Bindings for Rust

This library provides the official Rust interface to the Linux Bluetooth protocol stack (BlueZ). Both publishing local and consuming remote GATT services using idiomatic Rust code is supported. L2CAP sockets are presented using an API similar to Tokio networking.

BlueZ 121 Dec 26, 2022
A working example of multi targets compilation for Rust using Github Actions.

A working example of multi targets compilation for Rust using Github Actions. Supports Windows, MacOSX, x86_64, ARM and Raspberry PI Linux.

Nicolas Vanhoren 41 Dec 17, 2022
A cross-platform serial port library in Rust.

Introduction serialport-rs is a general-purpose cross-platform serial port library for Rust. It provides a blocking I/O interface and port enumeration

Bryant Mairs 143 Nov 5, 2021
A Rust port of parinfer.

parinfer-rust Infer parentheses for Clojure, Lisp and Scheme. https://github.com/eraserhd/parinfer-rust A full-featured, super fast implementation of

Jason Felice 432 Dec 31, 2022