简化的pcie配置空间读程序,支持0x100以上空间

参考setpci命令实现简化的pcie配置空间读程序,支持0x100以上空间

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

int pci_conf_read(char pathname[], int offset);

int main(int argc, char ** argv[])
{
        int offset;
        unsigned char buf[4];
        char devname[32];
        char pathname[128];
        int ret;

        if (argc == 3)
        {
                sscanf(argv[1], "%s", devname);
                sscanf(argv[2], "%x", &offset);
        }
        else
        {
                printf("need 2 params\n");
                return 1;
        }

        sprintf(pathname, "%s%s%s", "/sys/bus/pci/devices/0000:", devname, "/config");
        ret = pci_conf_read(pathname, offset);

        return ret;
}

int pci_conf_read(char pathname[], int offset)
{
        int fd, count, ret;
        unsigned char buf[4];

        fd = open(pathname, O_RDONLY);
        offset = offset/4*4;
        count = 4;
        if (offset > 0x1000)
        {
                printf("error offset = %04x\n", offset);
                return 1;
        }

        if((ret = pread(fd, buf, count, offset)) == -1)
        {
                printf("pread error\n");
                return 1;
        }
        else
        {
                printf("[%04x] = %02x%02x%02x%02x\n", offset, buf[3], buf[2], buf[1], buf[0]);
                return 0;
        }

        return 0;
}

usage:

sh-4.1# ./13 04:00.0 0
[0000] = 863210b5
sh-4.1# ./13 04:00.0 100
[0100] = fb410003
sh-4.1# ./13 04:00.0 1ec
[01ec] = 00000000
sh-4.1# ./13 04:00.0 1e8
[01e8] = 00000000
sh-4.1# ./13 04:00.0 668
[0668] = 00000f77
sh-4.1# ./13 04:00.0 7f 
[007c] = 00000000
sh-4.1# ./13 04:00.0 3f
[003c] = 0010010b
sh-4.1# ./13 04:00.0 10
[0010] = fb500004
sh-4.1# ./13 04:00.0 14
[0014] = 00000000
sh-4.1# ./13 04:00.0 18
[0018] = 000e0504
sh-4.1# ./13 04:00.0 1c
[001c] = 00004121
sh-4.1# ./13 04:00.0 20
[0020] = fb40faf0
sh-4.1# ./13 04:00.0 24
[0024] = cff10001
sh-4.1# ./13 04:00.0 1e8
[01e8] = 00000000


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值