i386 UNIX driver which allows user level inb's and outb's

Brian Litzinger brian at apt.UUCP
Wed Jan 3 19:44:41 AEST 1990


here is a complete driver written for ISC System V.3.2 UNIX on a
386 PC like machine that allows user level programs to access IO ports.

Hope you like.

<>  Brian Litzinger @ APT Technology Inc., San Jose, CA
<>  UUCP:  {apple,sun,pyramid}!daver!apt!brian    brian at apt.UUCP
<>  VOICE: 408 370 9077      FAX: 408 370 9291

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  README Makefile peek.c peek.h in.c out.c System Master
#   Node
# Wrapped by brian at apt on Wed Jan  3 00:42:14 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'README' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(3327 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
Xpeek:
X
X    a driver that allow user level programs to do byte in's
X    and out's to IO ports on 386 PC like machines.
X
X    By Brian E. Litzinger
X
X
XI wrote this the other day for my own use and thought that some of
Xyou out there may appreciate having it too.  
X
XSo here it is. I wrote it on a 386 machine running Interactive
XSystem V.3.2 UNIX version 2.0.2, but it should be portable to
Xother System V's and 386 PC like machines.
X
XSome warnings to start off though.  This driver makes a rather
Xbig security hole in your system so you will probably want to
Xseriously restrict access to the device entry '/dev/peek' after
Xyou install this driver.  Otherwise, all sorts of users may be
Xpeeking and poking where you don't want them to.
X
XINSTALLATION:
X
XOn my system I copied peek.h to /usr/include/sys/peek.h so other
Xprograms I wrote could include it easily.
X
XYou may want to edit the Makefile and change the variables
XBIN, USER, GROUP, and MODE to values that you feel are appropriate.
XI find the defaults very appropriate. 8-)
X
XThe values 100 and 3ff in file 'System' are supposed to represent
Xthe range of IO ports this driver is allowed to access.  You may
Xwant to change these values to suite your needs, though, I don't
Xthink these limits are enforced by the kernel.
X
XYou may get some complaints from drivers that don't want to share
Xtheir IO space with another driver.  If you do you'll have to
Xadd the sharable IO space flag to the sdevice entry for those
Xdevices. See sdevice(4).
X
XAssumming you've got everything set up the way you want
Xenter 'make' and the files Driver.o, in and out will be produced.
X
XNext if you enter 'make install' the in and out files will be copied
Xto the BIN directory and the permissions set appropriately.
X
XNext you should enter 
X    /etc/insdriver
X
XIn response to the question 'Directory containing files?' answer '.',
Xwithout the quotes.
X
XIn response to the question 'Driver name' answer 'peek',
Xwithout the quotes.
X
XThe rest of the questions you should answer as you feel appropriate.
X
XAfter the driver has sucessfully installed run '/etc/kconfig' to
Xbuild and boot a new kernel.
X
XNow after your new kernel comes up, log in as root and change the
Xpermissions on the file '/dev/peek' to something that satisfies
Xyour sense of security.  For example:
X    chown root /dev/peek
X    chgrp root /dev/peek
X    chmod 700 /dev/peek
X
XOk, now you are ready to make a mess out of your system.  Try the
Xin command (it's marginally less risky than experimenting with the
Xout command).  Please realize you can do horribly damaging things
Xto your system with this driver.
X
XIf you have any standard serial ports in your system, the IO port
Xat base(of serial port)+7 is usually a scratch register.  For
Xexample if you have a standard COM1:/tty00 at 0x3f8, you should
Xbe able to do the following:
X
X# /etc/out 3ff 5a		/* Note that in and out take hex */
X# /etc/in 3ff			/* arguments without a leading 0x */
X5a
X#
X
XOf course I can't vouch for all the asy drivers out there, so try
Xthis at your own risk.
X
XThat is all there is to it!  Oh yea, if you want to remove the peek
Xdriver enter:
X    /etc/conf/bin/idinstall -d peek
Xthen use '/etc/kconfig' to build a boot a new kernel.
X
XHappy peeking!
X
X<>  Brian Litzinger @ APT Technology Inc., San Jose, CA
X<>  UUCP:  {apple,sun,pyramid}!daver!apt!brian    brian at apt.UUCP
END_OF_FILE
if test 3327 -ne `wc -c <'README'`; then
    echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(438 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
X# where to put executables, you probably don't want everyone using
X# these programs.
XBIN=/etc
XUSER=root
XGROUP=root
XMODE=700
X
Xall: Driver.o in out
X
XDriver.o : peek.c
X	cc -O -c peek.c -o Driver.o
X
Xinstall: all
X	cp in out $BIN
X	chown $USER $BIN/in $BIN/out
X	chgrp $GROUP $BIN/in $BIN/out
X	chmod $MODE $BIN/in $BIN/out
X
Xclean::
X	rm -f Driver.o in out core
X
Xshar::
X	shar README Makefile peek.c peek.h in.c out.c System Master Node >peek.shar
X
END_OF_FILE
if test 438 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'peek.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'peek.c'\"
else
echo shar: Extracting \"'peek.c'\" \(1166 characters\)
sed "s/^X//" >'peek.c' <<'END_OF_FILE'
X/* peek.c */
X/* By Brian E. Litzinger */
X
X#include <sys/types.h>
X#include <sys/signal.h>
X#include <sys/errno.h>
X#include <sys/param.h>
X#include <sys/dir.h>
X#include <sys/user.h>
X#include <sys/page.h>
X#include <sys/systm.h>
X#include <sys/tty.h>
X#include <sys/sysmacros.h>
X#include "peek.h"
X
X#define OPEN		0x0001
X
Xstatic int pk_state = 0;
X
Xstruct peek_t peek_data;
X
Xpkinit(dev)
X{
X    pk_state = 0;
X}
X
Xpkopen(dev)
Xdev_t dev;
X{
X    pk_state |= OPEN;
X}
X
Xpkioctl(dev, cmd, arg, mode)
Xdev_t dev;
Xint cmd, mode;
Xcaddr_t arg;
X{
X    register struct peek_t *pk = &peek_data;
X
X    switch (cmd) {
X	case PEEKOUT:
X            if (copyin((struct peek_t *) arg, pk, sizeof(struct peek_t))==-1) {
X		u.u_error = EFAULT;
X		return;
X	    }
X	    outb(pk->port,pk->data);
X	    return;
X	case PEEKIN:
X            if (copyin((struct peek_t *) arg, pk, sizeof(struct peek_t))==-1) {
X		u.u_error = EFAULT;
X		return;
X	    }
X	    pk->data = inb(pk->port);
X            if (copyout(pk,(struct peek_t *) arg, sizeof(struct peek_t))==-1) {
X		u.u_error = EFAULT;
X		return;
X	    }
X	    return;
X	default:
X	    u.u_error = EINVAL;
X	    return;
X    }
X}
X
Xpkclose(dev)
Xdev_t dev;
X{
X    pk_state &= ~OPEN;
X}
X
END_OF_FILE
if test 1166 -ne `wc -c <'peek.c'`; then
    echo shar: \"'peek.c'\" unpacked with wrong size!
fi
# end of 'peek.c'
fi
if test -f 'peek.h' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'peek.h'\"
else
echo shar: Extracting \"'peek.h'\" \(285 characters\)
sed "s/^X//" >'peek.h' <<'END_OF_FILE'
X/*	Peek Driver		*/
X/*      By Brian E. Litzinger    */
X
X#ident	"@(#)peek.h	1.0 - 89/12/23"
X
X/* IOCTL defines */
X#define PEEK		('P'<<8)
X#define PEEKIN		(PEEK|8)
X#define PEEKOUT		(PEEK|9)
X/*
X * Per peek structure
X */
X
Xstruct peek_t {
X    unsigned short port;
X    unsigned short data;
X};
END_OF_FILE
if test 285 -ne `wc -c <'peek.h'`; then
    echo shar: \"'peek.h'\" unpacked with wrong size!
fi
# end of 'peek.h'
fi
if test -f 'in.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'in.c'\"
else
echo shar: Extracting \"'in.c'\" \(523 characters\)
sed "s/^X//" >'in.c' <<'END_OF_FILE'
X/* in.c */
X/* By Brian E. Litzinger */
X
X#include <stdio.h>
X#include "peek.h"
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X    int fd;
X    struct peek_t pd;
X
X    if (argc!=2) {
X	fprintf(stderr,"Incorrect number of arguments.\n");
X	fprintf(stderr,"Usage: %s port\n",argv[0]);
X	exit(1);
X    }
X
X    if ((fd=open("/dev/peek", 0))==-1) {
X	perror("in open:");
X	exit(2);
X    }
X
X    sscanf(argv[1],"%x\n",&pd.port);
X
X    if (ioctl(fd,PEEKIN,&pd)==-1) {
X	perror("in ioctl:");
X	exit(3);
X    }
X
X    printf("%x: %x\n",pd.port,pd.data);
X}
END_OF_FILE
if test 523 -ne `wc -c <'in.c'`; then
    echo shar: \"'in.c'\" unpacked with wrong size!
fi
# end of 'in.c'
fi
if test -f 'out.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'out.c'\"
else
echo shar: Extracting \"'out.c'\" \(573 characters\)
sed "s/^X//" >'out.c' <<'END_OF_FILE'
X/* out.c */
X/* By Brian E. Litzinger */
X
X#include <stdio.h>
X#include "peek.h"
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X    int fd;
X    struct peek_t pd;
X
X    if (argc!=3) {
X	fprintf(stderr,"Incorrect number of arguments.\n");
X	fprintf(stderr,"Usage: %s port data\n",argv[0]);
X	exit(1);
X    }
X
X    if ((fd=open("/dev/peek", 0))==-1) {
X	perror("out open:");
X	exit(1);
X    }
X
X    sscanf(argv[1],"%x\n",&pd.port);
X    sscanf(argv[2],"%x\n",&pd.data);
X
X    
X    if (ioctl(fd,PEEKOUT,&pd)==-1) {
X	perror("in ioctl:");
X	exit(3);
X    }
X
X    printf("%x< %x\n",pd.port,pd.data);
X}
END_OF_FILE
if test 573 -ne `wc -c <'out.c'`; then
    echo shar: \"'out.c'\" unpacked with wrong size!
fi
# end of 'out.c'
fi
if test -f 'System' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'System'\"
else
echo shar: Extracting \"'System'\" \(27 characters\)
sed "s/^X//" >'System' <<'END_OF_FILE'
Xpeek	Y	8	0	0	0	100	3ff	0	0
END_OF_FILE
if test 27 -ne `wc -c <'System'`; then
    echo shar: \"'System'\" unpacked with wrong size!
fi
# end of 'System'
fi
if test -f 'Master' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Master'\"
else
echo shar: Extracting \"'Master'\" \(30 characters\)
sed "s/^X//" >'Master' <<'END_OF_FILE'
Xpeek	Ioci	icHO	pk	0	0	1	32	-1
END_OF_FILE
if test 30 -ne `wc -c <'Master'`; then
    echo shar: \"'Master'\" unpacked with wrong size!
fi
# end of 'Master'
fi
if test -f 'Node' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Node'\"
else
echo shar: Extracting \"'Node'\" \(15 characters\)
sed "s/^X//" >'Node' <<'END_OF_FILE'
Xpeek 	peek	c	0
END_OF_FILE
if test 15 -ne `wc -c <'Node'`; then
    echo shar: \"'Node'\" unpacked with wrong size!
fi
# end of 'Node'
fi
echo shar: End of shell archive.
exit 0



More information about the Comp.unix.i386 mailing list