Wednesday, January 26, 2011

Migration from Windows to Linux device control applications

This system call to the specified device sends control code and other data.

The appropriate device drivers in accordance with the instructions of the dwIoControlCode control code. For example, use IOCTL_DISK_GET_DRIVE_GEOMETRY can get from a physical drive structure parameters (media type, cylinder, TracksPerCylinder, number of sectors per track, etc.). Can be found on the MSDN Web site for all control codes defined, header files, and other details (see references for related links). Whether you need to input/output buffer, and how they structure and size, depend on the actual ioctl process involves equipment and operations, and invokes the specified by the dwIoControlCode OK. If the overlapped operation of pointer set to NULL, then the DeviceIoControl would block (synchronization). Otherwise, its work asynchronously. Linux function ioctlLinux you can use the ioctl — intioctl (intfildes, intrequest,/* arg */...); — To specify the device to send control information. The first parameter fildes is open () function returns the file descriptor for alleged specific devices. And the corresponding system call DeviceIOControl ioctl, the input parameters list is not fixed. It depends on what ioctl request, as well as the instructions request parameters, just as Windows function DeviceIOControl dwIoControlCode parameter. However, the migration period need to be aware of when choosing the correct request parameters, because the DeviceIOControl ioctl request of dwIoControlCode and have different values. But there is no dwIoControlCode and request explicit mapping list. Usually you can find the associated header file request parameter values defined for the selected parameter value. All control codes defined in/usr/include/{asm, linux}/* .h file. Parameter arg as specific equipment operation provides detailed command information. The data type of the arg depends on the specific control requests. This parameter can be used to send detailed commands and receive returned data. Migration example we see a from Windows to Linux migration example. This example involves from PC main IDE hard drive read SMART log. Step 1-identification device types as mentioned earlier, Linux devices are used as file. It is first necessary to describe the device's file on Linux. Only use this file to get the device control requires a handle to the device. In this example, the object is an IDE hard disk drive. Linux be described as/dev/hdb/dev/hda, etc. This example will be the migration of hard disk device pathname is \\\\.\\PhysicalDrive0. /Dev/hda is the device corresponding to the Linux name of the file. 2. the changes include the header file must # include header files for Linux (see table 3): table 3. # include header files WindowsLinux # include # include # include # include # include # include # include # include windows.h contains open and close the device functions (CreateFile and CloseHandle). Accordingly, on Linux is used to open () and close () function that contains the header file types.h, sys/sys/stat.h and fcntl. Windows devioctl.h for function DeviceIoControl, we change it to sys/ioctl.h to ensure that the function ioctl can work. Ntddscsi.h (it is a header file from the DDK) defines a set of control codes for device control. Because this case deals only with IDE hard drives, so simply linux/hdreg.h added to Linux program. For other situations, it should be sure to include all header files (with the required control code is defined). For example, if you access the CD-ROM rather than a hard disk drive that contains the linux/cdrom. 3. the correct function and parameter now we take a closer look at the code. Listing 4 shows the details of the order. Listing 4. command details unsignedcharcmdBuf [7]; cmdBuff [0] = SMART_READ_LOG;//UsedforspecifyingSMART "commands" cmdBuff [1] = 1;//IDEsectorcountregistercmdBuff [2] = 1;//IDEsectornumberregistercmdBuff [3] = SMART_CYL_LOW;//IDElowordercylindervaluecmdBuff [4] = SMART_CYL_HI;//IDEhighordercylindervaluecmdBuff [5] = 0xA0 | (((Dev->Id-1)&1)*16);//IDEdrive/headregiStercmdBuff [6] = SMART_CMD;//ActualIDEcommand command information from ATA command instruction. Because this code ported to Linux does not need to be modified, so there is no need for further analysis. Listing 5 shows the code to open the Windows primary hard drive. 5. open the Windows primary hard drive HANDLEdevHandle = CreateFile ("\\\\.\\PhysicalDrive0",//patnameGENERIC_WRITE | GENERIC_READ,//AccessModeFILE_SHARE_READ | FILE_SHARE_WRITE,//SharingModeNULL, OPEN_EXISTING, 0, NULL);

No comments:

Post a Comment