参考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