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 FunctionFunction 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:
#include  <ioperm.h>/* for libc5 */#include  <sys/io.h>/* for glibc */int ioperm(unsigned long from, unsigned long num, int turn_on);

Parameters:
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:

#include <ioperm.h>int iopl(int level);

Parameters:
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 ):

/** 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 _GCCvoid 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 inp( 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,datamov dx,addrout dx,al}}unsigned char inp( unsigned short addr){unsigned char cha;asm{mov dx,addrin al,dxmov cha ,al}return cha;}#endifint 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(100);            outp(0x70, i);c = inp(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.