Ioperm-set Port permissions in Windows

Source: Internet
Author: User

There are two system calls for setting Port permissions in Windows: ioperm and iopl functions.
1. ioperm Function
Function Description:
Set the I/O port access capability for the calling process. Ioperm requires Super User Permissions. Only the low-end [0-0x3ff] I/O port can be set. to specify more port capabilities, you can use the iopl function. This call can only be used on the i386 platform.

Usage:
[Cpp]
# Include <ioperm. h>/* for libc5 */
# Include <sys/io. h>/* for glibc */
Int ioperm (unsigned long from, unsigned long num, int turn_on );

# Include <ioperm. h>/* for libc5 */
# Include <sys/io. h>/* for glibc */
Int ioperm (unsigned long from, unsigned long num, int turn_on); parameter:
From: Start port address.
Num: the number of ports that need to be modified.
Turn_on: The New Power bit of the port. 1 is enabled, 0 is disabled.
Return description:
0 is returned when execution is successful. -1 is returned for failure, and errno is set to one of the following values
EINVAL: the parameter is invalid.
EIO: This call is not supported
EPERM: unable to call the process.

2. iopl Functions

Function Description: This call is used to modify the operation Port permissions of the current process. It can be used for permissions on all 65536 ports. Therefore, ioperm is equivalent to a subset of the call. Like ioperm, this call is only applicable to the i386 platform.

Usage:
[Cpp]
# Include <ioperm. h>
Int iopl (int level );

# Include <ioperm. h>
Int iopl (int level); parameter:
Level: the port permission level. The read/write port is 3. The default capability level is 0, and the user space cannot be read or written.
Return Description: 0 is returned when execution is successful. -1 is returned for failure, and errno is set to one of the following values
EINVAL: The level value is greater than 3.
ENOSYS: The call is not implemented.
EPERM: unable to call the process.

Currently, the main part has been changed to support GCC compilation. Supports GCC inline assembly.
The Code is as follows (the C compiler of Windows is used by default ):
[Cpp]
/*
* Function: read cmos data using rtc port (0x70 -- port, 0x71 -- data)
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <windows. h>
 
Extern int install (int );
Extern int uninstall (int );
Extern ioperm (unsigned long from, unsigned long num, int turn_on );
 
# Ifdef _ GCC
Void outp (unsigned short addr, unsigned short data ){
_ Asm __(
"Mov % 1, % al ;"
"Mov % 0, % dx ;"
"Out % al, % dx ;"
:/* No output */
: "M" (addr), "m" (data)/* input addr and data */
: "Ax", "dx"
);
}
Unsigned char Indium (unsigned short addr)
{
Unsigned char cha;
_ Asm __(
"Mov % 1, % dx ;"
"In % dx, % al ;"
"Mov % al, % 0 ;"
: "= R" (cha)/* output to var (cha )*/
: "M" (addr)/* input addr */
: "Ax", "dx"
);
Return cha;
}
# Else/* using windows asm */
Void outp (unsigned short addr, unsigned short data ){
Asm {
Mov al, data
Mov dx, addr
Out dx, al
}
}
Unsigned char Indium (unsigned short addr)
{
Unsigned char cha;
Asm {
Mov dx, addr
In al, dx
Mov cha, al
}
Return cha;
}
 
# Endif
 
Int main (void)
{
Int I;
Unsigned char c;
Install (0 );
Sleep (500 );
If (ioperm (0x70, 2, 1 )){
Fprintf (stderr, "Error: ioperm () failed. Please install ioperm. sys driver. \ n ");
} Else {
For (I = 0; I <260; I ++)
{
// Sleep (1, 100 );
Outp (0x70, I );
C = Indium (0x71 );
Printf ("% 02x", c );
If (I + 1) % 20 = 0)
Printf ("\ n ");
}
}
Uninstall (0 );
Return 0;
}

/*
* Function: read cmos data using rtc port (0x70 -- port, 0x71 -- data)
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <windows. h>

Extern int install (int );
Extern int uninstall (int );
Extern ioperm (unsigned long from, unsigned long num, int turn_on );

# Ifdef _ GCC
Void outp (unsigned short addr, unsigned short data ){
_ Asm __(
"Mov % 1, % al ;"
"Mov % 0, % dx ;"
"Out % al, % dx ;"
:/* No output */
: "M" (addr), "m" (data)/* input addr and data */
: "Ax", "dx"
);
}
Unsigned char Indium (unsigned short addr)
{
Unsigned char cha;
_ Asm __(
"Mov % 1, % dx ;"
"In % dx, % al ;"
"Mov % al, % 0 ;"
: "= R" (cha)/* output to var (cha )*/
: "M" (addr)/* input addr */
: "Ax", "dx"
);
Return cha;
}
# Else/* using windows asm */
Void outp (unsigned short addr, unsigned short data ){
Asm {
Mov al, data
Mov dx, addr
Out dx, al
}
}
Unsigned char Indium (unsigned short addr)
{
Unsigned char cha;
Asm {
Mov dx, addr
In al, dx
Mov cha, al
}
Return cha;
}

# Endif

Int main (void)
{
Int I;
Unsigned char c;
Install (0 );
Sleep (500 );
If (ioperm (0x70, 2, 1 )){
Fprintf (stderr, "Error: ioperm () failed. Please install ioperm. sys driver. \ n ");
} Else {
For (I = 0; I <260; I ++)
{
// Sleep (1, 100 );
Outp (0x70, I );
C = Indium (0x71 );
Printf ("% 02x", c );
If (I + 1) % 20 = 0)
Printf ("\ n ");
}
}
Uninstall (0 );
Return 0;
}

 

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.