Skip to content

Commit

Permalink
added optimized extended back
Browse files Browse the repository at this point in the history
  • Loading branch information
ekknod committed Aug 30, 2024
1 parent e2ba18a commit 1fa6ab2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 25 deletions.
72 changes: 62 additions & 10 deletions Client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,39 @@ QWORD cl::pci::get_physical_address(ULONG bus, ULONG slot)
return vm::read<QWORD>(0, (QWORD)(i - 10)) + (((slot >> 5) + 8 * ((slot & 0x1F) + 32i64 * bus)) << 12);
}

static BOOL read_io_virtual_address(QWORD address, PVOID buffer, DWORD length)
{
using namespace cl;
using namespace pci;

DWORD location = 0;
DWORD data_left = length;

while (data_left)
{
if (data_left >= 4)
{
DWORD data = vm::read<DWORD>(0, address + location);
*(DWORD*)((PBYTE)buffer + location) = data;
location += 4;
}
else if (data_left >= 2)
{
WORD data = vm::read<WORD>(0, address + location);
*(WORD*)((PBYTE)buffer + location) = data;
location += 2;
}
else
{
BYTE data = vm::read<BYTE>(0, address + location);
*(BYTE*)((PBYTE)buffer + location) = data;
location += 1;
}
data_left = length - location;
}
return 1;
}

BOOL cl::pci::read(BYTE bus, BYTE slot, BYTE func, DWORD offset, PVOID buffer, DWORD size)
{
if (PciIoAddressVirtual)
Expand All @@ -643,16 +676,7 @@ BOOL cl::pci::read(BYTE bus, BYTE slot, BYTE func, DWORD offset, PVOID buffer, D
QWORD delta = device - PciIoAddressPhysical;
QWORD virtu = PciIoAddressVirtual + delta;

if (size == 0x100 || size == 0xF00)
{
for (DWORD i = 0; i < size; i+= 4)
{
if (!controller->read_virtual(0, virtu + offset + i, (PVOID)((QWORD)buffer + i), sizeof(DWORD)))
return 0;
}
return 1;
}
return controller->read_virtual(0, virtu + offset, buffer, size);
return read_io_virtual_address(virtu + offset, buffer, size);
}

if (!has_io_access)
Expand Down Expand Up @@ -769,7 +793,35 @@ std::vector<DEVICE_INFO> get_devices_by_bus(std::vector<RAW_PCIENUM_OBJECT> &pci
static void pci_initialize_cfg(DEVICE_INFO &dev)
{
memset(dev.cfg.raw, 0, sizeof(dev.cfg.raw));

//
// legacy (0x00 - 0x100)
//
cl::pci::read(dev.bus, dev.slot, dev.func, 0, dev.cfg.raw, 0x100);

//
// optimized extended (0x100 - 0x1000)
//
WORD optimize_ptr = 0x100;
WORD max_size = sizeof(dev.cfg.raw);
for (WORD i = 0x100; i < max_size; i += 4)
{
cl::pci::read(dev.bus, dev.slot, dev.func, i, (PVOID)(dev.cfg.raw + i), 4);
if (i >= optimize_ptr)
{
optimize_ptr = GET_BITS(*(DWORD*)((PBYTE)dev.cfg.raw + optimize_ptr), 31, 20);
if (!optimize_ptr)
{
optimize_ptr = 0x1000; // disable
max_size = i + 0x30; // max data left 0x30

if (max_size > sizeof(dev.cfg.raw))
{
max_size = sizeof(dev.cfg.raw);
}
}
}
}
}

std::vector<PORT_DEVICE_INFO> cl::pci::get_port_devices(void)
Expand Down
37 changes: 23 additions & 14 deletions Client/scan/pci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace scan
static void PrintPcieInfo(PORT_DEVICE_INFO& port);
static void PrintPcieConfiguration(unsigned char *cfg, int size);

static void filter_pci_cfg(config::Pci &cfg);

std::wstring get_driver_name(DEVICE_INFO &dev)
{
if (!dev.drv_device_object)
Expand Down Expand Up @@ -623,23 +625,15 @@ static void scan::check_config(PORT_DEVICE_INFO &port)
}
}

// void filter_pci_cfg(config::Pci &cfg);;
static void scan::dumpcfg(std::vector<PORT_DEVICE_INFO> &devices)
{
for (auto& entry : devices)
{
for (auto& dev : entry.devices)
{
printf("[%d:%d:%d] [%02X:%02X]", dev.bus, dev.slot, dev.func, *(WORD*)(dev.cfg.raw), *(WORD*)(dev.cfg.raw + 0x02));
BYTE config[0x1000];
memcpy(config, dev.cfg.raw, sizeof(dev.cfg.raw));
cl::pci::read(dev.bus, dev.slot, dev.func, 0x100, config + 0x100, 0xF00);
PrintPcieConfiguration(config, sizeof(config));

// auto full = config::Pci{};
// memcpy(full.raw, config, 0x1000);
// filter_pci_cfg(full);

PrintPcieConfiguration(dev.cfg.raw, *(DWORD*)(dev.cfg.raw + 0x100) ? 0x1000 : 0x100);
filter_pci_cfg(dev.cfg);
printf("\n");
}
}
Expand Down Expand Up @@ -762,8 +756,7 @@ static void scan::PrintPcieConfiguration(unsigned char *cfg, int size)
printf("\n");
}

/*
void filter_pci_cfg(config::Pci &cfg)
static void scan::filter_pci_cfg(config::Pci &cfg)
{

printf(
Expand Down Expand Up @@ -1049,6 +1042,24 @@ void filter_pci_cfg(config::Pci &cfg)
if (!empty.cap_on)
continue;


if (i == 0x03) // dsn
{
auto dsn = cfg.get_dsn();
printf(
"\n[PCI DSN Capability - 0x%lx]\n"
"---------------------------------------------------------------------\n",
i
);
printf("DSN_CAP_NEXTPTR 0x%lx\n", dsn.hdr.cap_next_ptr());
printf("DSN_CAP_ON %ld\n", dsn.cap_on);
printf("DSN_CAP_ID 0x0%lx\n", dsn.hdr.cap_id());
printf("DSN 0x0%llx\n", dsn.serial);
printf("---------------------------------------------------------------------\n");

continue;
}

printf(
"\n[PCI Express Extended Capability - 0x%lx]\n"
"---------------------------------------------------------------------\n",
Expand All @@ -1061,5 +1072,3 @@ void filter_pci_cfg(config::Pci &cfg)
}
}

*/

16 changes: 15 additions & 1 deletion Client/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ namespace config {
}

struct Pci {
unsigned char raw[0x100];
unsigned char raw[0x1000];
Pci() { memset(raw, 0, sizeof(raw)); }

Pci(unsigned char *buffer, int size)
Expand Down Expand Up @@ -658,6 +658,20 @@ namespace config {
return 0;
}

auto get_dsn() -> pci::DSN {
auto cap = get_ext_capability_by_id(0x03);
auto res = pci::DSN{};
if (cap != 0)
{
auto hdr = *(DWORD*)(raw + cap);
res.cap_on = hdr != 0;
res.base_ptr = cap;
res.hdr.raw = hdr;
res.serial = *(UINT64*)(raw + cap + 0x04);
}
return res;
}

auto get_empty_extended_cap(BYTE id) -> pci::EmtpyExtPcieCap {
auto cap = get_ext_capability_by_id(id);
auto res = pci::EmtpyExtPcieCap{};
Expand Down

0 comments on commit 1fa6ab2

Please sign in to comment.