/** * Created by Peter Stuge and placed in the Public Domain. * * To compile: * gcc -O2 -o io io.c * To use (as root): * read from port 0x80: ./io r80 * write 0x69 to port 0x80: ./io w80=69 * * Multiple commands can be given at once, any whitespace will be ignored. */ #include #include #include #include #include #include #include #if defined(__FreeBSD__) #include void do_outb(unsigned char d,u_int p) { outb(p,d); } unsigned char do_inb(u_int p) { return inb(p); } #else #include void do_outb(unsigned char d,unsigned short p) { outb(d,p); } unsigned char do_inb(unsigned short p) { return inb(p); } #endif void ib(unsigned short p) { static unsigned short op=0; static unsigned char oc=0; unsigned char c; c=do_inb(p); printf("r0x%04x=%02x\n",p,c); oc=c; op=p; } void ob(unsigned short p,unsigned char c) { static unsigned short op=0; static unsigned char oc=0; do_outb(c,p); printf("w0x%04x=%02x\n",p,c); oc=c; op=p; } int main(int argc,char *argv[]) { char *s; int ret,arg=1; #if defined(__FreeBSD__) int iofd; #endif unsigned int i,j; unsigned char c; if(argc<2) return 0; #if defined(__FreeBSD__) if((iofd=open("/dev/io",O_RDWR))<0) { #else if(iopl(3)) { #endif perror("iopl"); return 1; } for(s=argv[arg];*s;) { switch(*(s++)) { case 'r': if(1!=(ret=sscanf(s,"%x%n",&i,&j))) { fprintf(stderr,"sscanf=%d: %s\n",ret,strerror(errno)); return 1; } ib(i); s+=j; usleep(10000); break; case 'w': if(2!=(ret=sscanf(s,"%x=%hhx%n",&i,&c,&j))) { fprintf(stderr,"sscanf=%d: %s\n",ret,strerror(errno)); return 1; } ob(i,c); s+=j; usleep(1000); break; default: printf("unknown command '%c'\n",*(s-1)); break; } if(!*s&&arg