Code Monkey home page Code Monkey logo

findwdk's Introduction

FindWDK

CMake module for building drivers with Windows Development Kit (WDK) Build status CI

Introduction

FindWDK makes it possible to build kernel drivers and kernel libraries with Windows Development Kit (WDK) and CMake.

Requirements:

  • WDK 8.0 and higher
  • Visual Studio 2015 and higher
  • CMake 3.0 and higher

Usage

Add FindWDK to the module search path and call find_package:

list(APPEND CMAKE_MODULE_PATH "<path_to_FindWDK>")
find_package(WDK REQUIRED)

FindWDK will search for the latest installed Windows Development Kit (WDK) and expose commands for creating kernel drivers and kernel libraries. Also the following variables will be defined:

  • WDK_FOUND -- if false, do not try to use WDK
  • WDK_ROOT -- where WDK is installed
  • WDK_VERSION -- the version of the selected WDK
  • WDK_WINVER -- the WINVER used for kernel drivers and libraries (default value is 0x0601 and can be changed per target or globally)
  • WDK_NTDDI_VERSION -- the NTDDI_VERSION used for kernel drivers and libraries, if not set, the value will be automatically calculated by WINVER

WDKContentRoot environment variable overrides the default WDK search path.

Kernel driver

The following command adds a kernel driver target called <name> to be built from the source files listed in the command invocation:

wdk_add_driver(<name> 
    [EXCLUDE_FROM_ALL]
    [KMDF <kmdf_version>]
    [WINVER <winver_version>]
    [NTDDI_VERSION <ntddi_version>]
    source1 [source2 ...]
    )

Options:

  • EXCLUDE_FROM_ALL -- exclude from the default build target
  • KMDF <kmdf_version> -- use KMDF and set KMDF version
  • WINVER <winver_version> -- use specific WINVER version
  • NTDDI_VERSION <ntddi_version> -- use specific NTDDI_VERSION

Example:

wdk_add_driver(KmdfCppDriver 
    KMDF 1.15 
    WINVER 0x0602
    Main.cpp
    )

Kernel library

The following command adds a kernel library target called <name> to be built from the source files listed in the command invocation:

wdk_add_library(<name> [STATIC | SHARED]
    [EXCLUDE_FROM_ALL]
    [KMDF <kmdf_version>]
    [WINVER <winver_version>]
    [NTDDI_VERSION <ntddi_version>]
    source1 [source2 ...]
    )

Options:

  • EXCLUDE_FROM_ALL -- exclude from the default build target
  • KMDF <kmdf_version> -- use KMDF and set KMDF version
  • WINVER <winver_version> -- use specific WINVER version
  • NTDDI_VERSION <ntddi_version> -- use specific NTDDI_VERSION
  • STATIC or SHARED -- specify the type of library to be created

Example:

wdk_add_library(KmdfCppLib STATIC 
    KMDF 1.15
    WINVER 0x0602
    KmdfCppLib.h 
    KmdfCppLib.cpp
    )

Linking to WDK libraries

FindWDK creates imported targets for all WDK libraries. The naming pattern is WDK::<UPPERCASED_LIBNAME>. Linking a minifilter driver to FltMgr.lib is shown below:

target_link_libraries(MinifilterCppDriver WDK::FLTMGR)

Samples

Take a look at the samples folder to see how WMD and KMDF drivers and libraries are built.

License

FindWDK is licensed under the OSI-approved 3-clause BSD license. You can freely use it in your commercial or opensource software.

Version history

Version 1.0.2 (TBD)

Version 1.0.1 (13 Mar 2018)

  • New: Add ability to link to WDK libraries
  • New: Add MinifilterCppDriver sample
  • Fix: W4 warnings in C version of the driver, add missing /W4 /WX for C compiler

Version 1.0.0 (03 Feb 2018)

  • Initial public release

findwdk's People

Contributors

fdetro avatar kolanich avatar okazakinagisa avatar oxygen1a1 avatar sergiusthebest avatar thejcab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

findwdk's Issues

Set VS_PLATFORM_TOOLSET="WindowsKernelModeDriver10.0"

#sets the platform toolset to "WindowsKernelModeDriver10.0"
set_property(TARGET ${_target} PROPERTY VS_PLATFORM_TOOLSET WindowsKernelModeDriver10.0)

#sets the configuration type to "Driver"
set_property(TARGET ${_target} PROPERTY VS_CONFIGURATION_TYPE "Driver")

set_target_properties(${_target} 
                      PROPERTIES 
                      # Targeting Windows 10
                      VS_GLOBAL_TargetVersion "Windows10"
                      # Developing a Windows Driver Model (WDM) driver
                      VS_GLOBAL_DriverType "WDM" 
                      # Targeting Universal platform
                      VS_GLOBAL_DriverTargetPlatform "Universal" 
                      # Not using debug libraries
                      VS_GLOBAL_UseDebugLibraries "false"  
                      # Disabling API validation
                      VS_GLOBAL_ApiValidator_Enable "false"  
                      # Using local time for INF2CAT timestamping
                      VS_GLOBAL_Inf2CatUseLocalTime "true"  
                      # Turning off driver signing
                      VS_GLOBAL_SignMode "Off"  
                      # Targeting .NET Framework version 4.5
                      VS_GLOBAL_TargetFrameworkVersion "v4.5"
                      # Minimum required version of Visual Studio is 12.0 (Visual Studio 2013)
                      VS_GLOBAL_MinimumVisualStudioVersion "12.0"
                      )

if build error “GetDriverProjectAttributes” and “GetPackageFiles”

Disable the generation of ZERO_CHECK

set(CMAKE_SUPPRESS_REGENERATION true)

https://discourse.cmake.org/t/unable-to-fully-build-windows-umdf-driver-using-cmake/2368/12
https://gitlab.kitware.com/cmake/cmake/-/issues/23643
https://gitlab.kitware.com/cmake/cmake/-/issues/23717

<Project DefaultTargets="Build" ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
	<Target Name="GetDriverProjectAttributes" Returns="@(DriverProjectAttributes)"/>
	<Target Name="GetPackageFiles" Returns="@(FullyQualifiedFilesToPackage)"/>  
</Project>

Can't create a service for the driver

All versions are the same for both host and target
Windows version: Windows 10 home 21H2 19044.1826
WDK version: 10.0.22621.0

I try to run on host:

sc.exe create driver_name type=kernel binPath=C:\Users\WDKRemoteUser\Desktop\driver_name .sys start=demand
sc.exe start driver_name

this is the output I get when I query for the service:

SERVICE_NAME: driver_name 
        TYPE               : 1  KERNEL_DRIVER
        STATE              : 1  STOPPED
        WIN32_EXIT_CODE    : 87  (0x57)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

Problem it doesn't happen when I try to run driver as a service if I compile a test driver with a vs solution.
I tried to compare the compiler and linker options but could find something relevant that is missing that might cause this.
Here's the vs solution compiled driver compiler and linker options:

/ifcOutput "x64\Debug\" /GS /W4 /wd"4748" /wd"4603" /wd"4627" /wd"4986" /wd"4987" /Gy /Zc:wchar_t- /I"x64\Debug\" /analyze:"stacksize1024" /guard:cf /Zi /Gm- /Od /Fd"x64\Debug\vc143.pdb" /FI"C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared\warning.h" /Zc:inline /fp:precise /Zp8 /D "_WIN64" /D "_AMD64_" /D "AMD64" /D "DEPRECATE_DDK_FUNCTIONS=1" /D "MSC_NOOPT" /D "_WIN32_WINNT=0x0A00" /D "WINVER=0x0A00" /D "WINNT=1" /D "NTDDI_VERSION=0xA00000C" /D "DBG=1" /errorReport:prompt /GF /WX /Zc:forScope /GR- /Gz /Oy- /Oi /FC /Fa"x64\Debug\" /nologo /Fo"x64\Debug\" /Fp"x64\Debug\driver-test.pch" /diagnostics:column 
/OUT:"C:\Users\user\Desktop\driver-test\driver-test\x64\Debug\driver-test.sys"
/MANIFEST:NO
/PROFILE
/Driver
/PDB:"C:\Users\user\Desktop\driver-test\driver-test\x64\Debug\driver-test.pdb"
"C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\km\x64\BufferOverflowFastFailK.lib"
"C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\km\x64\ntoskrnl.lib"
"C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\km\x64\hal.lib"
"C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\km\x64\wmilib.lib"
"C:\Program Files (x86)\Windows Kits\10\lib\wdf\kmdf\x64\1.15\WdfLdr.lib"
"C:\Program Files (x86)\Windows Kits\10\lib\wdf\kmdf\x64\1.15\WdfDriverEntry.lib"
/RELEASE
/VERSION:"10.0"
/DEBUG
/MACHINE:X64
/ENTRY:"FxDriverEntry"
/WX
/OPT:REF
/INCREMENTAL:NO
/PGD:"C:\Users\user\Desktop\driver-test\driver-test\x64\Debug\driver-test.pgd"
/SUBSYSTEM:NATIVE",10.00"
/LTCGOUT:"x64\Debug\driver-test.iobj"
/OPT:ICF
/ERRORREPORT:PROMPT
/MERGE:"_TEXT=.text;_PAGE=PAGE"
/ILK:"x64\Debug\driver-test.ilk"
/NOLOGO
/NODEFAULTLIB
/SECTION:"INIT,d"

EDIT:
Actually after some playing I don't know what I did but I get this everytime I try to run the service:

Windows cannot verify the digital signature for this file. A recent hardware or software change might have installed a file that is signed incorrectly or damaged, or that might be malicious software from an unknown source.

Integrity check is disabled, test signing and debug are on, bcdedit output:

Windows Boot Manager
--------------------
...
Windows Boot Loader
-------------------
...
loadoptions             DDISABLE_INTEGRITY_CHECK
...
nointegritychecks       Yes
testsigning             Yes
...
debug                   Yes

To be clear I never signed the driver with a test signature before this problem occurred, so I don't know how I even got this?

Possible typo

"${WDK_COMPILE_DEFINITIONS};$<$<CONFIG:Debug>:${WDK_COMPILE_DEFINITIONS_DEBUG};_WIN32_WINNT=${WDK_WINVER}>"

${WDK_COMPILE_DEFINITIONS};$&lt;$CONFIG:Debug:${WDK_COMPILE_DEFINITIONS_DEBUG}
>
;_WIN32_WINNT=${WDK_WINVER}

Is this intended or Typo?

the plain signature for target_link_libraries has already been used with X

A "the plain signature for target_link_libraries has already been used with ..." error is generated when trying to add private link dependencies after a target was generated with wdk_add_driver :

wdk_add_driver(driver WINVER 0x0A00)
target_link_libraries(driver PRIVATE WDK::WDMSEC)

This can be workaround by just removing the "PRIVATE" option but our use-case is slightly more complex since the link dependencies are set by down-stream cmake infrastructure files that are shared among different projects.

This is the patch that we're currently using to mitigate the issue

diff --git a/FindWdk.cmake b/FindWdk.cmake
index 6987305..c5ba280 100644
--- a/FindWdk.cmake
+++ b/FindWdk.cmake
@@ -155,15 +155,15 @@ function(wdk_add_driver _target)
         "${WDK_ROOT}/Include/${WDK_INC_VERSION}/km/crt"
         )
 
-    target_link_libraries(${_target} WDK::NTOSKRNL WDK::HAL WDK::BUFFEROVERFLOWK WDK::WMILIB)
+    target_link_libraries(${_target} PRIVATE WDK::NTOSKRNL WDK::HAL WDK::BUFFEROVERFLOWK WDK::WMILIB)
 
     if(CMAKE_SIZEOF_VOID_P EQUAL 4)
-        target_link_libraries(${_target} WDK::MEMCMP)
+        target_link_libraries(${_target} PRIVATE WDK::MEMCMP)
     endif()
 
     if(DEFINED WDK_KMDF)
         target_include_directories(${_target} SYSTEM PRIVATE "${WDK_ROOT}/Include/wdf/kmdf/${WDK_KMDF}")
-        target_link_libraries(${_target}
+        target_link_libraries(${_target} PRIVATE
             "${WDK_ROOT}/Lib/wdf/kmdf/${WDK_PLATFORM}/${WDK_KMDF}/WdfDriverEntry.lib"
             "${WDK_ROOT}/Lib/wdf/kmdf/${WDK_PLATFORM}/${WDK_KMDF}/WdfLdr.lib"
             )

about Wpp Tracing

image
Why is there no wpp option?

In general, kmdf has a wpp option, for example:
Y_MQ$R2CSB@ NUQV 4~C}D

Global compile options are missing

Simplified example:

top-level CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)

project(FindWDK_IssueTest)

add_compile_options("/MP")

add_subdirectory(Exe)
add_subdirectory(Sys)

Exe CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)

project(FindWDK_IssueTest_Exe)

add_executable(FindWDK_IssueTest_Exe Main.cpp)

Sys CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)

project(FindWDK_IssueTest_Sys)

find_package(WDK REQUIRED)

wdk_add_driver(FindWDK_IssueTest_Sys Main.cpp)

So, I added a compile option /MP to the top-level CMakeLists.txt and want it to work on all subdirectories.
But when I open the generated solution, I see that the compile option only applies to the project Exe and not both.
image
image

Of course, there is a workaround:

top-level CMakeLists.txt:

set(MY_GLOBAL_COMPILE_OPTIONS "/MP")
add_compile_options(${MY_GLOBAL_COMPILE_OPTIONS})

Sys CMakeLists.txt:

wdk_add_driver(FindWDK_IssueTest_Sys Main.cpp)
target_compile_options(FindWDK_IssueTest_Sys PRIVATE ${MY_GLOBAL_COMPILE_OPTIONS})

It works, but obviously not so elegantly.

I personally think this is a bug. So, I was wondering if it is possible for FindWDK to keep global compile options that are safe for driver projects?

Thanks in advance!

cl : Command line warning D9025 : overriding '/GR' with '/GR-'

wdk_add_driver and wdk_add_driver add the compilation switch /GR-, which conflicts with /GR normally added by CMake (found with CMake 3.18 with Ninja and MSVC 16.8 preview, x64).

Q: Would there be a way to remove existing compilation flags for WDK targets? I noticed all the user-mode stuff is there when building a kernel driver, including inappropriate stuff: UCRT include directories, references to kernel32.lib and friends... even exception modes (/EHsc).

DriverEntry not working on windows7 X64

hello again, if set this:

string(CONCAT WDK_LINK_FLAGS
#        "/ENTRY:DriverEntry "  

why this do not work,if modifly DriverEntry to FxDriverEntry,sys load ok,but build err so.

then i creat vsproject use cmakeGUI, i found the complie is FxDriverEntry, and i delete Fx, the sys file was loaded on win7. so my problen is how to set: "/ENTRY:DriverEntry " ? thx.

WindowsSdkDir vs WDKContentRoot

I'm using VS2019 and WDK 10, WindowsSdkDir is automatically set while WDKContentRoot not.
Could we add some kine of fallback to WindowsSdkDir?

[Troubleshooting] Cannot build kmdf driver

Hello! I am attempting to build a personal project of mine https://github.com/Ty3r0X/centipede using just the Visual Studio build tools 2022 installation. I decided to replace my non-functional batch build script with cmake, however when compiling I get:

-- The C compiler identification is MSVC 19.41.34120.0
-- The CXX compiler identification is MSVC 19.41.34120.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.30/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: 'Z:/build/CMakeFiles/CMakeScratch/TryCompile-9ou4ph'

    Run Build Command(s): C:/Users/ty3r0x/AppData/Local/Microsoft/WinGet/Links/ninja.exe -v cmTC_eac2c
    [1/2] C:\PROGRA~2\MICROS~2\2022\BUILDT~1\VC\Tools\MSVC\1441~1.341\bin\Hostx64\x64\cl.exe  /nologo   /DWIN32 /D_WINDOWS  /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoCMakeFiles\cmTC_eac2c.dir\testCCompiler.c.obj /FdCMakeFiles\cmTC_eac2c.dir\ /FS -c Z:\build\CMakeFiles\CMakeScratch\TryCompile-9ou4ph\testCCompiler.c
    [2/2] C:\Windows\system32\cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_eac2c.dir --rc="C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\rc.exe" --mt="C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\mt.exe" --manifests  -- C:\PROGRA~2\MICROS~2\2022\BUILDT~1\VC\Tools\MSVC\1441~1.341\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmTC_eac2c.dir\testCCompiler.c.obj  /out:cmTC_eac2c.exe /implib:cmTC_eac2c.lib /pdb:cmTC_eac2c.pdb /version:0.0 /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    FAILED: cmTC_eac2c.exe
    C:\Windows\system32\cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_eac2c.dir --rc="C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\rc.exe" --mt="C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\mt.exe" --manifests  -- C:\PROGRA~2\MICROS~2\2022\BUILDT~1\VC\Tools\MSVC\1441~1.341\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmTC_eac2c.dir\testCCompiler.c.obj  /out:cmTC_eac2c.exe /implib:cmTC_eac2c.lib /pdb:cmTC_eac2c.pdb /version:0.0 /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    RC Pass 1: command "C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\rc.exe /fo CMakeFiles\cmTC_eac2c.dir/manifest.res CMakeFiles\cmTC_eac2c.dir/manifest.rc" failed (exit code 0) with the following output:
    no such file or directory
    ninja: build stopped: subcommand failed.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)


-- Configuring incomplete, errors occurred!

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

project(centipede)

set_property(GLOBAL PROPERTY USER_FOLDERS ON)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4 /WX")

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../cmake")
find_package(WDK REQUIRED)

add_subdirectory(src)

src/CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

wdk_add_driver(centipede
  KMDF 1.33
  entrypoint.c
)

Apologies for my rather unprofessional issue, but I kept looking on other resources over to no avail. Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.