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 unsignedcharcmdBuff [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); from theThe device on and off by explaining, we need two parameters (file pathname, and device access mode) to open a Linux device. According to the front of the original code, the first argument should be the/dev/hda, second O_RDONLY | O_NONBLOCK. The modified code is as follows: HANDLEdevHandle = open ("/dev/hda", O_RDONLY | O_NONBLOCK);. Corresponding to CloseHandle (devHandle); changes to close (devHandle);. The main part of the transplantations is how to use ioctl to access specific devices and obtain the required information. The original Windows code as shown in Listing 6: inventory of the source code on the DeviceIoControl 6.Windows typedefstruct_Bufer {UCHARreq [8];//DetailedcommandinformationothertancontrolcodeULONGDataBufferSize;//SizeofDataBuffer, hereis512UCHARDataBuffer [512];//DataBufer} Bufer; BufferregBufer; memcpy (regBuffer.req, cmdBuff, 7);//req [7] isreservedforfutureuse.Mustbezero.regBuffer.DataBufferSize = 512; unsignedintsize = 512 + 12;//SizeofregBufer//8forreq, 4forDataBufferSize, 512fordataDWORDbytesRet = 0;//Numberofbytesreturnedintretval;//Returnedvalueretval = DeviceIoControl (devHandle, IOCTL_IDE_PASS_THROUGH,//ControlcoderegBuffer,//InputBuffer, includingdetailedcommandsize, regBuffer,//OutputBuffer, usethesamebuffereresize, & bytesRet, NULL); if (! retval) cout <"deviceiocontrolfailed.">"deviceiocontrolfailed.">
The device handle in the two platforms is the first argument, it from CreateFile and Linux open () returns. But Windows control codes and Linux on the definition of requests vary greatly, so that there is no fixed rule to identify these two parameters are mapped, as indicated above. IOCTL_IDE_PASS_THROUGH is defined in the header file ntddscsi.h as CTL_CODE (IOCTL_SCSI_BASE, 0x040a, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS). Passed in the header file to find the definition,/usr/include/linux/hdreg.h can choose the corresponding Linux control code HDIO_DRIVE_CMD. In addition, the device to complete specific tasks require detailed command information. The command stored in the cache, and returns the data of the memory space in the exchange of data in the process. We use the same caching to send commands and gets the required log information. Linux cache size can be changed; not necessarily out of eight bytes. This example uses the order of four bytes. The corresponding Linux code (listing 7) looks simple, because its structure and function parameters than Windows is simple. List of source code 7.Linux function ioctl intretval; unsignedcharreq [4 + 512];//Enoughforreturneddataandthe4bytedetailedcommandinformationreq [0] = cmdBuff [6];//Considertherequirementinthissample, only4bytesareusedreq [1] = cmdBuff [2]; req [2] = cmdBuff [0]; req [3] = cmdBuff [1]; retval = ioctl (devHandle, HDIO_DRIVE_CMD, & req); if (ret) cout <"ioctlfailed.">"ioctlfailed.">
The task now is to compile a Linux platform for the program and correct the remaining syntax errors. According to the Linux version and the build environment, you may need another modification.
No comments:
Post a Comment