By Adam Shapiro – Senior PM EPIX
What are Universal Drivers
One of the new features available with the Windows Driver Kit 10 is the ability to create Universal Drivers for all Windows 10 versions. With a Universal driver, you can write the driver once, and compile the single source for each different architecture. The same source is used to create a driver for the Desktop, Mobile and IOT versions of Windows. This enables your driver to run on Phones, Tablets, Desktops, Servers, and Development board devices, without any code changes.
Creating Universal Drivers with WDK 10 and Visual Studio provides some big benefits to both the driver developer as well device manufacturers.
- The Windows Universal Platform provides a consistent set of APIs across all Windows 10 device types. This makes it easy to compile a driver for all platforms, without code changes.
- Since the all Windows 10 editions support Universal drivers, any Windows edition can be used as a driver test platform
- Device manufacturers can sell components to desktop as well as mobile OEMs/ODMS without needing a different driver
- Compiling for Universal is built into the WDK.
- Deploying a Universal Driver to a desktop, mobile, or IOT device is now built into the WDK.
Essentially, the set of API's a Universal Driver can call is the intersection or overlap of API's from Desktop, Mobile and IOT versions of Windows 10. For the most part, these API's are common to all versions. A Universal Windows driver calls only device driver interfaces (DDIs) that are part of UWP. If you are writing a new driver, you should create compile the driver as Universal. In addition, you should take advantage of the WDF frameworks when creating new drivers, as WDF is supported on all Windows 10 versions.
If you are porting an existing driver to Windows 10, most kernel mode drivers will build for Universal with little or no modification. In many cases, you can recompile an existing kernel-mode driver that runs on Windows 8.1 as a Universal Windows driver, as long as the driver does not work with any user-mode components. WDM and KMDF drivers that work with Windows 8.1 should recompile as Universal Windows drivers targeting Windows 10 with no conversion required. However, some User mode drivers may need to be modified since not all Win32 APIs are available as part of Windows Universal Platform. For these Win 32 APIs, replacement APIs may be available in some cases in Mobile and IOT. In rare instances, you may need to rewrite the driver of a replacement API is not available.
Get the tools to Build Universal Drivers
How to create a Universal Driver with Visual Studio and WDK 10
- In Visual Studio 2015 RC, open the existing driver project, or create a new one
- In the Solution Explorer pane, right-click the solution and choose Configuration Manager. Set the target operating system to Windows 10.
- Right-click the driver project and choose Properties. Under Configuration Properties->Driver, set the Target Platform to Universal. This is the default for all new Windows 10 drivers.
- Build the driver. You might see some linker errors due to missing APIs. The compiler uses a utility called API Validator to check if all the APIs called by the driver are in the Windows Universal Platform API set. If there are no issues, you will see a message in the build output that the "Driver is a Universal Driver".
- Fix the errors one by one by going through the error log. Refer to individual reference pages in the documentation for possible alternate APIs. If replacements are not available, you may need to redesign your driver.
How to deploy a Universal Driver
Typically when you test and debug a driver, the debugger and driver run on separate computers. The computer that runs the debugger is called the host computer, and the computer that runs the driver is called the target computer. The target computer is also called the test computer.
So far you've used Visual Studio to build a driver on the host computer. Now you need to configure a target computer. You first need to provision a computer for driver deployment and testing (WDK 10). Then you can deploy, install, load, and debug your driver:
See the MSDN topic: Provision a computer for driver deployment and testing (WDK 10).
Universal INF files
There are a few differences in deploying a driver to a desktop system compared to a mobile/IOT system. If you are building a universal or mobile driver package, you must use a universal INF file. If you are building a desktop driver package, you don't have to use a universal INF file, but doing so is recommended because of the performance benefits.
A universal INF file uses a subset of the INF syntax that is available to a Windows driver. A universal INF file installs a driver and configures device hardware, but does not perform any other action, such as running a co-installer. Some editions of Windows, such as Windows 10 Mobile, do not support the Plug and Play mechanism for driver installation. Therefore, driver installation takes place on an offline image of the target system. When Microsoft Visual Studio builds your driver for such a target system, it generates an XML-based configuration file that contains all of the registry settings to be applied. As a result, an INF file for such a system must perform only additive operations that do not depend on the runtime behavior of the system. An INF file with such restricted syntax is called a universal INF file.
InfVerif (InfVerif.exe) is a tool that you can use to test a driver INF file. In addition to reporting INF syntax problems, the tool reports if the INF file is universal. When you build a driver in Microsoft Visual Studio 2015 with Windows Driver Kit (WDK) 10, the compiler runs the tool automatically as part of the build process. Alternatively, you can run the InfVerif.exe tool from the command line.
The verification tool is part of the WDK 10 installation, and can be found in the \tools subdirectory of your WDK 10 installation, c:\Program Files(x86)\Windows Kits\10\tools\.
Create a deployment package for Mobile
If you are deploying a driver to a mobile device, you need to add a package file to your project. WDK 10 includes PkgGen, a tool that generates package files. You run PkgGen in Visual Studio when you build your driver, do the following:
To add the Package file:
- Right-click the driver project and choose Add->New Item. Next, under Visual C++->Windows Driver, choose Package Manifest. Click Add.
- Visual Studio adds a file called Package.pkg.xml to your driver project. You can right-click the file and choose properties to verify that the item type is PkgGen. Click OK.
- Right-click the driver project and choose Properties. Under Configuration Properties, open the PackageGen node and change Version to any value you like.
- Save your work and restart Visual Studio as administrator.
- Build your driver. Visual Studio links against the required libraries and generates a .cat file, an .inf file, and a driver binary.
Deploy Using Visual Studio F5
Once you have provisioned the target computer, phone, tablet or IOT device and modified the INF and created the deployment package (for Mobile) you can deploy the driver. First, set the target device for deploying the driver.
- On the host computer, open your solution in Visual Studio.
- In the Solution Explorer window, right-click the driver project, and choose Properties.
- In the Package Property Pages window, in the left pane, go to Configuration Properties > Driver Install > Deployment.
- Check Remove previous driver versions before deployment.
- For Remote Computer Name, select the name of the computer, phone or IOT device that you configured for testing and debugging.
- Select Hardware ID Driver Update, and enter the hardware ID for your driver. Click OK.
- On the Debug menu, choose Start Debugging, or press F5 on the keyboard.
- Visual Studio first shows progress in the Output window. Then it opens the Debugger Immediate Window and continues to show progress.
Wait until your driver has been deployed, installed, and loaded on the target computer. This might take a minute or two.
Try creating a Universal Driver for your next driver project!