2020-12-17 17:42:14 +01:00
|
|
|
#include <errno.h>
|
2020-05-02 00:09:42 +02:00
|
|
|
#include <stdio.h>
|
2020-12-17 17:42:14 +01:00
|
|
|
#include <string.h>
|
2020-05-02 00:09:42 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2020-05-20 16:54:58 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include "pci.h"
|
|
|
|
|
|
|
|
char pci_path[PATH_MAX] = ".";
|
2020-05-02 00:09:42 +02:00
|
|
|
|
2020-05-21 20:08:35 +02:00
|
|
|
__attribute__((stdcall)) uint32_t pci_read(uint32_t bus, uint32_t dev,
|
|
|
|
uint32_t fun, uint32_t offset,
|
|
|
|
size_t len) {
|
2021-12-12 19:26:37 +01:00
|
|
|
char path_colon[PATH_MAX*2];
|
|
|
|
char path_underscore[PATH_MAX*2];
|
2020-05-02 00:09:42 +02:00
|
|
|
uint32_t value = 0;
|
2021-12-12 19:26:37 +01:00
|
|
|
sprintf(path_colon, "%s/%4.4x:%2.2x:%2.2x.%u/config", pci_path, 0, bus, dev,
|
|
|
|
fun);
|
|
|
|
sprintf(path_underscore, "%s/%4.4x_%2.2x_%2.2x.%u/config", pci_path, 0, bus,
|
|
|
|
dev, fun);
|
|
|
|
int fd = open(path_colon, O_RDONLY);
|
2020-12-17 17:42:14 +01:00
|
|
|
if (fd == -1) {
|
2021-12-12 19:26:37 +01:00
|
|
|
fd = open(path_underscore, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
// fprintf(stderr, "[pci] error: failed to open config file '%s': %s\n",
|
|
|
|
// path, strerror(errno));
|
|
|
|
return UINT32_MAX;
|
|
|
|
}
|
2020-05-20 16:54:58 +02:00
|
|
|
}
|
2020-05-02 00:09:42 +02:00
|
|
|
lseek(fd, offset, SEEK_SET);
|
|
|
|
read(fd, &value, len);
|
|
|
|
close(fd);
|
|
|
|
return value;
|
|
|
|
}
|