2014年6月30日 星期一

Windows Driver 3_ Provision Computer For Driver Deployment And Testing

In the previous discussion, I used [manually configure debuggers and do not provision], but in this way, the driver files must be installed by myself. In order to save time, I use [Provision computer and choose debugger settings], the more details is in 
http://msdn.microsoft.com/en-us/library/windows/hardware/hh698272(v=vs.85).aspx

I'll show steps briefly...
Assume that the driver has built already and the things we have to do is to  deploy and test it.

1. Go to C:\Program Files\Windows Kits\8.1\Remote\x86, then copy [WDK Test Target Setup x86-x86_en-us] into your target computer and install it. 


2. In Host Computer (Visual Studio 2013)

   [Driver] menu -> test-> Configure Computer

   choose second one, and notice that the computer name must as same as the name of your target computer,



you can find your target computer's name just like second pictures:


After clicking next, you have to set the pipe name. 



Note that the pipe name must as same as the pipename you set in vmware. The picture below shows the pipe name that I set at VMware before.
(We create this serial port to let virtual computer know that there's a port it can use. This serial port is a virtual port and VMware will receive the data from virtual computer through this port, then send the data out to the host computer according to the pipe name, the host computer must have the same pipe name to receive these data from VMware and handle them, and that's why we set a pipe name in Visual Studio and in VMware both the same.)




After finishing the configuration, system will connect to your target computer, and if success, it will show "complete" at last.

3. Now go back to the Visual Studio and in the Solution Manager Window(方案總管), right click [HelloWorld Package]-> properties -> Driver Install -> Deployment
Click [Enable deployment] and [Remove previous driver versions before deployment]
Then, entering Target Computer Name
Finally, in the [Hardware ID Driver Update], Type "Root\HelloWorld". Hardware ID does not identify a real hardware, it identifies an virtual device, and we must give it a place in the devie tree as child of the root node. The doc in Microsoft mentioned that we do not need to select [Hardware ID Driver Update] if it's a real hardware. Instead, we should choose [Install and Verify]


4. When you click F5 in your visual studio trying to remote debug to the target computer, this program will control the target computer, create a WDKRemoteUser account and move the driver packages, even run the driver automatically.


From now on, you don't need to copy your driver packages into the target computer manually, the debugger in Visual Studio will do it for U. Hurray!!!!!!





2014年6月29日 星期日

Windows Driver 2 _ HelloWorld Driver

It's time to write my first driver "HelloWorld"
I write this code in VS2013 and here's my steps

A. Create a new project
    (1) [File] menu->[New]->[Project], in the left pane, select [Installed]->[WDF], in the middle pane, select [Kernel Mode Driver, Empty(KMDF)]. Fill in "HelloWorld" in the name field and choose the project's location in the location field. Check out Create directory for solution. Finally click OK ! 
(Note that the name field can only be equal or less than 32 characters string, mentioned in Microsoft Official Doc)


    (2) In the Solution Explorer(方案總管), right click [HelloWorld]->add->new Item(新增項目). In the middle pane, select C++ File, and name "main.c" in the name field, then click add. Finally, you will see main.c under the Source Files
(Note that the file name extension is .c !!)



Here's the code:
-------------------------------------------------------------------------------------------------------
#include <ntddk.h>  

VOID DriverUnload(PDRIVER_OBJECT driver)
{
DbgPrint("first:HelloWorld End!");
}

NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pUnicodeString)
{
DbgPrint("first:HelloWorld Begin!");
pDriverObject->DriverUnload = DriverUnload;
return STATUS_SUCCESS;
}
-------------------------------------------------------------------------------------------------------

     (3) Right click Solution'HelloWorld'(2 Solutions)(方案'HelloWorld')-> configuration manager(組態管理員). Choose configuration and platform both HelloWorld and HelloWorld Package


     (4) Cancel Wpp Tracing
Solution Explorer->right click HelloWorld -> Properties -> set Run Wpp tracing to No -> OK


      (5) also in Properties->C/C++ -> middle pane 警告視為錯誤->否

    (6) Build project
Choose Build Solution->Build to build driver and create a driver package, the ouput window will show the build progress.


      (7) Navigate in Windows Explorer to your HelloWorld folder(..\Visual Studio 2013\Projects\HelloWorld), then go into win7Debug\HelloWorld Package folder, there will have several files:
HelloWorld.sys : the kernel mode driver file
HelloWorld.inf : an information file that Windows uses when driver is installed
HelloWorld.cat : a catalog file that the installer uses to verify the test signature for the driver package
WdfCoinstaller01011.dll : A co-installer for the Windows Driver Frameworks(WDF)


B. After building your project, it's time to test it on target computer.
Before testing your driver, you must copy your [HelloWorld Package]folder into your target computer.
Normally, you will have 4 files in your HelloWorld folder that I mentioned in last article.

     (1) Let's start from setting the DebugView 
          Run -> regedit -> open key :[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager] -> create a key [Debug Print Filter] under this folder -> add a DEFAULT DWORD value and set a value 0x0f on it -> reboot

     (2) open DebugView and check out [capture]->[capture kernel]

     (3) open osrloader and select the sys file you want to load. Then click [Register Service] and [Start Service] then you will see "first:HelloWorld Begin" in DebugView.
Click [Stop Service] then you will see "first:HelloWorld End". If you want to remove the driver, click [Unregister Service].






     P.s You don't need to install the osrloader to load the driver, just find out your inf file, and right click on it, click [install]. However, it's up to you to use osrloader or not.
Another way to load your driver is using command, just entering sc create HelloWorld binPath= "your_sys_file's_location" type= "kernel" start= "demand" error= "normal" Displayname= "HelloWorld", press Enter
then entering sc start HelloWorld


     If you use remote debug, you will see your string at Debugger Immediate Window in VS2013 

[Reference]
1. http://home.educities.edu.tw/fushiyun2000/driver_nt_style_c_tutorial_hello_world.htm
2. http://eagle-sw.blogspot.tw/2011/10/win7debugview.html
3. http://blogs.microsoft.co.il/sasha/2011/06/04/baby-steps-in-windows-device-driver-development-part-2-hello-world-driver/
4. http://blog.csdn.net/wsye88/article/details/26375459

Windows Driver 1_Setting Environment to Remote Debug

In this article, I will discuss how to set up the environment on Host/Target Computer to remote debug the driver running on target computer.
(p.s Target Computer is a virtual computer to test your driver )

A. Environment
Host Computer : Windows7 SP1 x86
                         WDK (Windows Driver Kit)8.1
                         Visual Studio 2013
Target Computer : Windows7 SP1 x86 (Run on VMware Player)
                             Debug View
                             osrLoader

B. Settings on VS2013
      (1) [ Driver] menu-> Test->Computer Configuration
      (2) select [Add New Computer] button -> choose [Manually configure debuggers and do not provision], remember to give it a name!
         


      (3)  choose [Kernel Mode] and set the values as the picture shows->next->finish


      (4) finally you will get the settings as follow



C. Settings on VM
     (1) open [Virtual Machine Settings]
     (2) delete the Printer in order to let new serial port=1
     (3) [add] -> serial port -> choose [output to named pipe] as its serial port type -> in the Named pipe field enter "\\.\pipe\com_1" and choose [this end is the server],[the other end is an application ] -> finish
Your settings may look the same as follow, and remember to check out the [Yield CPU on poll]


D.  After those settings, turn on the target computer on VM (i.e your target computer=windows7)
     (1) use system manager's identity to open the command line and enter "bcdedit" to open the Windows Boot Manager (以系統管理員身分執行)


     (2) enter a command bcdedit  /copy  {current}  /d  "Windows 7 Kernel Debug Mode"
This command will copy current boot settings and give it a new name, i.e Windows 7 Kernel Debug Mode, and you will see 2 booting selections in boot manager.


       (3) press WIN+R and open "msconfig" service -> choose [boot] (開機) menu, you'll see Windows 7 Kenel Debug Mode in it. Select to boot it as a default selection.(設定成預設值) Now choose it and click 進階選項-> check out  the debug(偵錯) box, and select debug serial port(com1), finally select transmit rate to 115200 -> OK -> reboot your target computer.


E. Wow!!! Get ready to remote debug
     (1) Go back to VS2013 and click [Debugging Tools for Windows-Kernel Debugger]


     (2) select I know what I'm doing, continue debugging
   (3) then you can see your debugging progress in Debugger Immediate Window, if it keeps waiting to reconnect for too long, trying to  reboot your vm again and the connection will be success.


Hurray!!!!!  You can remote debug your driver on target computer right now!

[Reference]
1. http://blog.csdn.net/iceamber2012/article/details/18809813
2. http://blog.csdn.net/iceamber2012/article/details/17963649


全國推廣動物認領養平台串聯貼紙

全國推廣動物認領養平台串聯貼紙