Mr. Z said: the compiling environment of the kernel driver

Source: Internet
Author: User

 

[Author: Zhang Pei] [Original: http://www.yiiyee.cn/blog/kernelbuildenviroment /]

This chapter drives Z to continue to drive the kingdom. On this ground, Xiao Z is a geographic snake and is used to running and running. You can tell me what you want to know.

The so-called high mountains must be humble. Before we bring everyone into the high mountains, we need to spend some time at the foot of the mountains in order to clarify some preparations. This section describes the driver compiling environment. The history of the Windows kernel driver and the development environment are very historical. It is not good at first, but good at the end. The bad times are the old society, and now it is a very good new era.

Old Society

It was cumbersome to prepare the compiling environment for kernel driver development before Win8. The programmer needs to manually download and install wdk (Note 1). the development environment is in the installed wdk. Wdk is short for Windows Driver kit, that is, the windows driver development kit. It provides a very simple development environment. It is not an IDE environment that is easy to develop, but just some compilation kits in bulk.

After wdk is installed, the link to the compiling environment of wdk is displayed in the Start Menu. Be careful not to delete the links. Otherwise, it will be troublesome because it is troublesome to manually generate links.

The compiling environment is classified. First, the target file is compiled based on the target system classification, that is, the target file on which the operating system runs. Most Microsoft products maintain backward compatibility. This rule also applies here: Drivers compiled using the win7 subsystem environment can generally run on Vista and XP systems, otherwise, it will not be true (note 2 ).

Secondly, according to the hardware platform classification, there are currently four windows operating platforms: x86, x64, IA64, and arm. Among them, arm is the story that started in Win8. It is not available yet, so only the first three hardware platforms are available (note 3 ).

Finally, we need to separate the checked (or debug) and free (or release) based on the compiled version. In this case, there must be six compilation environment links under each OS group.

In this book, if the driver is compiled using the old version wdk, the compiling environment of the win7 target system is used by default to generate the checked version. The target platform is x86 or x64. Therefore, only x86 checked build environment and x64 checked build environment are selected.

The compilation environment is actually a console. Of course, what is the difference between runtime and the Console environment that runs directly from cmd.exe? We already know that these black compilation environment icons are actually shortcuts. Let's take a look at the target content of its shortcut, and you may be able to understand the clues. Take the x64 checked build environment as an example. The following content is displayed:

C: \ windows \ system32 \ cmd.exe/k c: \ winddk \ 7600.16385.1 \ bin \ setenv. Bat c: \ winddk \ 7600.16385.1 \ chk x64 win7

This line is easy to read. In the original compilation environment, a console process that runs cmd.exe executes the/k parameter for initialization. In the help of the cmd.exe command, the/k parameter is described as follows: carries out the command specified by string but remains (after executing a command, do not exit the program ). That is to say, start the console process and execute commands. After the execution, the console program is left for users to continue using.

Then, all the content after/K is an initialization command:

C: \ winddk \ 7600.16385.1 \ bin \ setenv. Bat c: \ winddk \ 7600.16385.1 \ chk x64 win7

It can be split into several parts for analysis. The first setenv. bat is the batch file that initializes the compiling environment. The following is its parameter: the first parameter is the path of the wdk, which can be used to find the compiler program. The second parameter is used to specify the target file of the checked version to be compiled; third, specify that the hardware platform is x64, and third, specify that the target system is win7.

The setenv. BAT file in wdk is the master kitchen responsible for compiling environment configuration. If you pass the parameter to it, it will assign you a compilation environment of the type (dish ).

How can I compile the driver in this console? Use the following steps:

Use the CD command to locate the driver directory containing the source file;

Enter the build or BLD (short for build-CZ) command for compilation;

If the compilation is successful, the driver file will be generated. Otherwise, an error or warning message will be displayed. You can also view the detailed error or warning information in the relevant log file under the directory folder.

Here, the compilation process is clear. Some may ask me, what do I use to write code? Sorry, there is no kangzhuang avenue for you to use, but ten million paths are ready for use. You can use Notepad notepad or any text editor to edit the Code. If you are not bothered, you can use Visual Studio to write the code, but only edit the code.

Manual Integration vs Environment

The Visual Studio tool is not needed. It is uncomfortable to use a text editor to make your mistakes. So some smart people finally figured out how to manually transform the vs project and integrate the driver project into it.

I have encountered two transformation methods. The first is the stupid method, which is very troublesome. It tries to directly use the VC compiler to compile the driver. By transforming a common Win32 project, modifying its configurations, including library files, and compiling macros, it can compile the driver. Someone posted detailed implementation details. I tried but never succeeded.

The second method is very clever. It needs to create a makefile project and compile the driver by directly calling the wdk compiler. This method is clever and convenient. The only drawback is that the recent vs software does not include the makefile Project template. Therefore, it is impossible to create a makefile engineer through vs itself. The best way is to copy a ready-made makefile project file from somewhere else. The reader places an empty makefile project file in the project folder, opens the project file, and then drag the required source file into it. You can download this empty project file below this article.

Open the attributes of the makefile project, and you will see the nmake attribute setting interface.

This nmake.exe program is critical to the east and west. What is the application? Used to run makefile files! With regard to the nmake.exe applet, you can find it on your own, which must be included in the installation directory of visual stuio. The file attribute of this tool is described as Microsoft program maintenance utility (Microsoft engineering management tool ). Makefile is a project management file, which is widely used on Linux/Unix systems. In the past, on the Microsoft platform, we may have used a large number of projects, but now we don't need them. Instead, we use Visual Studio project files.

However, at this time, the Windows kernel driver is still using makefile and sources files. That's a coincidence! The two sides come together and give us a chance to use the vs makefile project to compile the driver.

In the nmake attribute settings, you must manually enter the implementation of the build/rebuild/clear commands. The above has already introduced the implementation of the console compilation environment, just copy it. Take the build command as an example. First, call setenv. BAT to initialize it, and then call build or BLK to compile it. Then we can compile the following two commands in build to generate the checked driver on the X86 platform under win7 OS:

Call % basedir % \ bin \ setenv. Bat % basedir % chk x86 win7; BLD

If you want to compile drivers of other types, just change the parameters. The two commands are not very beautiful, so we can only put a batch file named my_build.bat in the system directory. The content of the batch file is as follows:

@ Echo on
If "% 4" = "/a" Call my_clean % 1% 2% 3
Call % basedir % \ bin \ setenv. Bat % basedir % 1% 2% 3
BLD

Another clear command is to delete the target file and use the del or RD command. There is also a batch file named my_clean.bat with the following content:

If exist debug RD/S/q debug
If exist release RD/S/Q release
If exist OBJ % 1 _ % 3 _ % 2 RD/S/q obj % 1 _ % 3 _ % 2

New Age

Well, the bitter stories of the old society have come to an end. Now let's begin with the story of liberation. The new era has suddenly arrived, and vs2012 has given us a new life (here we will hear the applause of the Hero )! Vs2012 came out after Win8 was released. It has many features for Win8, but it can also run on win7/Vista systems. Download and install the vs2012 software first, and then install the wdk of Win8. The compiling environment of wdk is automatically integrated into vs2012. When you open vs2012 again, you can find the kernel driver project in the new project wizard.

You can't help creating a WDF driver project immediately, open it, and find that hundreds of lines of automatically generated code have smiled and lined up to welcome you. Are you very happy? After a bit of fun, suddenly press F7, rely on! After compilation, A. SYS file is generated successfully! It not only generates a. SYS file, but also provides you with a complete Driver Installation Package (including the signed. sys/. CAT/. inf file ). Great!

One day, new programmers will feel justified and taken for granted. At this moment, we old guys are already crying in happiness. Sorry, for the people who came along, please cherish the hard-won life.

Come on, let's take a closer look at the vs2012 driver project. Is the Template Selection interface when you create a vs2012 project. Here we can see several template groups currently provided under the Windows Driver directory tree. Now we only focus on the WDF group. After you click WDF, it contains three template items: kmdf with Automatic Code Generation, kmdf without automatic code, and umdf. Select the first template, enter the project name, and click "OK. No other settings need to be completed. directly generate the project file and jump into the generated project interface. Assume that the project name you entered is mykmdf1.

The solution includes two project projects: one is the driver project and the other is the driver installation package project. The former contains the actual driver code and generates the driver file. The later package the driver file. It generates a. Cat File Based on the. inf file and signs the driver file with the specified digital certificate.

Please move your finger and press F7 on the keyboard to start the compilation process and check whether the mykmdf1.sys file and the corresponding driver package can be generated successfully? This is an important milestone. Do not leave the team. If you encounter an error here, you need to find a solution.

1> ------ generated: Project: mykmdf1, configuration: Vista debug Win32 ------
1> stamping vistadebug \ mykmdf1.inf [version] section with driverver = 05/14/2013, 17.17.5.125
1> device. c
1> driver. c
1> queue. c
1> generating code...
1> mykmdf1.vcxproj-> C: \ Users \ mozhang \ Documents \ Visual Studio 2012 \ projects \ mykmdf1 \ vistadebug \ mykmdf1.sys
1> done adding additional store
1> successfully signed: C: \ Users \ mozhang \ Documents \ Visual Studio 2012 \ projects \ mykmdf1 \ vistadebug \ mykmdf1.sys
1>
2> ------ generated: Project: mykmdf1 package, configuration: Vista debug Win32 ------
2> ......................
2> signability test complete.
2>
2> errors:
2> none
2>
2> Warnings:
2> none
2>
2> catalog generation complete.
2> C: \ Users \ mozhang \ Documents ents \ Visual Studio 2012 \ projects \ mykmdf1 \ vistadebug \ mykmdf1 package \ mykmdf1.cat
2> done adding additional store
2> successfully signed: C: \ Users \ mozhang \ Documents \ Visual Studio 2012 \ projects \ mykmdf1 \ vistadebug \ mykmdf1 package \ mykmdf1.cat
2>
============ Generate: 2 successful instances, 0 failed instances, 0 latest instances, and 0 skipped instances ==========

Driver package project

Let's take a look at this automatically generated driver package project. What can it do for us? Through observation, we have done four things:

  1. Digital signature for the. SYS file
  2. Generate a. Cat File Based on the. inf file
  3. Timestamp the signature file
  4. Generate driver package

NOTE 1: The wdk installation directory cannot contain spaces or Chinese characters. If it is installed in the C: \ Documents ents and users directory, an error occurs, or it is installed in C: \ My driver directory is also incorrect. The error does not occur during installation, but during wdk compilation.

Note 2: The DLL files compiled by the kernel compiler do not conform to backward compatibility.

NOTE 3: systems after Win8 no longer support the IA64 platform, so Win8 only supports three hardware platforms.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.