免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2994 | 回复: 4
打印 上一主题 下一主题

求教:用户空间访问I/O端口 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-08-31 20:59 |只看该作者 |倒序浏览
本帖最后由 theman_lai 于 2010-08-31 21:01 编辑

LDD3第九章中关于在用户空间访问I/O端口给了两个小程序inp 和outp,怎么使用这他们读写端口。
求教了。

论坛徽章:
0
2 [报告]
发表于 2010-08-31 21:02 |只看该作者

论坛徽章:
0
3 [报告]
发表于 2010-08-31 21:03 |只看该作者
提供的那两个读写端口函数如何使用?问题可能很菜,求教了

论坛徽章:
0
4 [报告]
发表于 2010-08-31 22:55 |只看该作者
iopl(3);
val = inp(port);
outp(port, val);

论坛徽章:
0
5 [报告]
发表于 2010-08-31 23:14 |只看该作者
回复 4# accessory
谢谢版主,您给的应该是系统函数,针对LDD3提供的inp源码生成的应用程序如何使用呢?
我把源码贴上来:/*
* inp.c -- read all the ports specified in hex on the command line.
*     The program uses the faster ioperm/iopl calls on x86, /dev/port
*     on other platforms. The program acts as inb/inw/inl according
*     to its own name
*
* Copyright (C) 1998,2000,2001 Alessandro Rubini
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation; either version 2 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program; if not, write to the Free Software
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <sys/io.h> /* linux-specific */

#ifdef __GLIBC__
#  include <sys/perm.h>
#endif

#define PORT_FILE "/dev/port"

char *prgname;

#ifdef __i386__
static int read_and_print_one(unsigned int port,int size)
{
    static int iopldone = 0;

    if (port > 1024) {
        if (!iopldone && iopl(3)) {
            fprintf(stderr, "%s: iopl(): %s\n", prgname, strerror(errno));
            return 1;
        }
        iopldone++;
    } else if (ioperm(port,size,1)) {
        fprintf(stderr, "%s: ioperm(%x): %s\n", prgname,
                port, strerror(errno));
        return 1;
    }

    if (size == 4)
        printf("%04x: %08x\n", port, inl(port));
    else if (size == 2)
        printf("%04x: %04x\n", port, inw(port));
    else
        printf("%04x: %02x\n", port, inb(port));
    return 0;
}
#else /* not i386 */

static int read_and_print_one(unsigned int port,int size)
{
    static int fd = -1;
    unsigned char b; unsigned short w; unsigned int l;

    if (fd < 0)
        fd = open(PORT_FILE, O_RDONLY);
    if (fd < 0) {
        fprintf(stderr, "%s: %s: %s\n", prgname, PORT_FILE, strerror(errno));
        return 1;
    }
    lseek(fd, port, SEEK_SET);
   
    if (size == 4) {
        read(fd, &l, 4);
        printf("%04x: 0x%08x\n", port, l);
    } else if (size == 2) {
        read(fd, &w, 2);
        printf("%04x: 0x%04x\n", port, w & 0xffff);
    } else {
        read(fd, &b, 1);
        printf("%04x: 0x%02x\n", port, b & 0xff);
    }
    return 0;
}

#endif /* i386 */


int main(int argc, char **argv)
{
    unsigned int i, n, port, size, error = 0;
   
    prgname = argv[0];
    /* find the data size */
    switch (prgname[strlen(prgname)-1]) {
        case 'w': size = 2; break;
        case 'l': size = 4; break;
        case 'b': case 'p': default:
            size = 1;
    }

    setuid(0); /* if we're setuid, force it on */
    for (i = 1; i < argc; i++) {
        if ( sscanf(argv, "%x%n", &port, &n) < 1
              || n != strlen(argv) ) {
            fprintf(stderr, "%s: argument \"%s\" is not a hex number\n",
                    argv[0], argv);
            error++; continue;
        }
        if (port & (size-1)) {
            fprintf(stderr, "%s: argument \"%s\" is not properly aligned\n",
                    argv[0], argv);
            error++; continue;
        }
        error += read_and_print_one(port, size);
    }
    exit (error ? 1 : 0);
}

谢谢!!!期待您的回复。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP