2024-11-06 14:52:08 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* @file pcitree.cpp
|
2026-06-08 23:45:43 +02:00
|
|
|
|
* @author Bj<EFBFBD>rn Ruytenberg (bjorn@bjornweb.nl)
|
2024-11-06 14:52:08 +01:00
|
|
|
|
* @brief !pcitree command
|
|
|
|
|
|
* @details
|
|
|
|
|
|
* @version 0.10.3
|
|
|
|
|
|
* @date 2024-10-31
|
|
|
|
|
|
*
|
|
|
|
|
|
* @copyright This project is released under the GNU Public License v3.
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
// Global Variables
|
|
|
|
|
|
//
|
2026-06-08 23:45:43 +02:00
|
|
|
|
extern BOOLEAN g_IsKdModuleLoaded;
|
2024-11-06 14:52:08 +01:00
|
|
|
|
extern BOOLEAN g_IsSerialConnectedToRemoteDebuggee;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief help of the !pcitree command
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return VOID
|
|
|
|
|
|
*/
|
|
|
|
|
|
VOID
|
|
|
|
|
|
CommandPcitreeHelp()
|
|
|
|
|
|
{
|
|
|
|
|
|
ShowMessages("!pcitree : enumerates all PCIe endpoints on the debuggee.\n\n");
|
|
|
|
|
|
|
2024-12-11 15:43:41 +01:00
|
|
|
|
ShowMessages("syntax : \t!pcitree\n");
|
2024-11-06 14:52:08 +01:00
|
|
|
|
|
|
|
|
|
|
ShowMessages("\n");
|
2024-12-11 15:43:41 +01:00
|
|
|
|
ShowMessages("\t\te.g : !pcitree\n");
|
2024-11-06 14:52:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief !pcitree command handler
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param CommandTokens
|
|
|
|
|
|
* @param Command
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return VOID
|
|
|
|
|
|
*/
|
|
|
|
|
|
VOID
|
|
|
|
|
|
CommandPcitree(vector<CommandToken> CommandTokens, string Command)
|
|
|
|
|
|
{
|
|
|
|
|
|
BOOL Status;
|
|
|
|
|
|
ULONG ReturnedLength;
|
2026-07-24 19:36:31 +02:00
|
|
|
|
DEBUGGEE_PCITREE_REQUEST_RESPONSE_PACKET PcitreePacket = {};
|
2024-11-06 14:52:08 +01:00
|
|
|
|
|
|
|
|
|
|
if (CommandTokens.size() != 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
ShowMessages("incorrect use of the '%s'\n\n",
|
|
|
|
|
|
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
|
|
|
|
|
|
CommandPcitreeHelp();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-17 21:01:03 +01:00
|
|
|
|
//
|
|
|
|
|
|
// Send buffer
|
|
|
|
|
|
//
|
|
|
|
|
|
if (g_IsSerialConnectedToRemoteDebuggee)
|
2024-11-06 14:52:08 +01:00
|
|
|
|
{
|
2024-11-17 21:01:03 +01:00
|
|
|
|
KdSendPcitreePacketToDebuggee(&PcitreePacket);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2026-06-08 23:45:43 +02:00
|
|
|
|
AssertShowMessageReturnStmt(g_IsKdModuleLoaded, g_DeviceHandle, ASSERT_MESSAGE_KD_NOT_LOADED, ASSERT_MESSAGE_DRIVER_NOT_LOADED, AssertReturn);
|
2024-11-06 14:52:08 +01:00
|
|
|
|
|
2024-11-17 21:01:03 +01:00
|
|
|
|
//
|
|
|
|
|
|
// Send IOCTL
|
|
|
|
|
|
//
|
2026-07-20 12:27:14 +02:00
|
|
|
|
Status = PlatformDeviceIoControl(
|
2024-11-17 21:01:03 +01:00
|
|
|
|
g_DeviceHandle, // Handle to device
|
|
|
|
|
|
IOCTL_PCIE_ENDPOINT_ENUM, // IO Control Code (IOCTL)
|
|
|
|
|
|
&PcitreePacket, // Input Buffer to driver.
|
|
|
|
|
|
SIZEOF_DEBUGGEE_PCITREE_REQUEST_RESPONSE_PACKET, // Input buffer length
|
|
|
|
|
|
&PcitreePacket, // Output Buffer from driver.
|
|
|
|
|
|
SIZEOF_DEBUGGEE_PCITREE_REQUEST_RESPONSE_PACKET, // Length of output
|
|
|
|
|
|
// buffer in bytes.
|
|
|
|
|
|
&ReturnedLength, // Bytes placed in buffer.
|
|
|
|
|
|
NULL // synchronous call
|
|
|
|
|
|
);
|
2024-11-06 14:52:08 +01:00
|
|
|
|
|
2024-11-17 21:01:03 +01:00
|
|
|
|
if (!Status)
|
|
|
|
|
|
{
|
2026-07-20 12:27:14 +02:00
|
|
|
|
ShowMessages("ioctl failed with code 0x%x\n", PlatformGetLastError());
|
2024-11-17 21:01:03 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-11-17 15:33:10 +01:00
|
|
|
|
|
2024-11-17 21:01:03 +01:00
|
|
|
|
if (PcitreePacket.KernelStatus == DEBUGGER_OPERATION_WAS_SUCCESSFUL)
|
|
|
|
|
|
{
|
|
|
|
|
|
//
|
|
|
|
|
|
// Print PCI device tree
|
|
|
|
|
|
//
|
2024-12-10 16:04:06 +01:00
|
|
|
|
ShowMessages("%-12s | %-9s | %-17s | %s \n%s\n", "DBDF", "VID:DID", "Vendor Name", "Device Name", "----------------------------------------------------------------------");
|
2025-01-26 19:42:28 +01:00
|
|
|
|
for (UINT8 i = 0; i < (PcitreePacket.DeviceInfoListNum < DEV_MAX_NUM ? PcitreePacket.DeviceInfoListNum : DEV_MAX_NUM); i++)
|
2024-11-17 21:01:03 +01:00
|
|
|
|
{
|
2025-01-26 19:42:28 +01:00
|
|
|
|
Vendor * CurrentVendor = GetVendorById(PcitreePacket.DeviceInfoList[i].ConfigSpace.VendorId);
|
2024-12-11 15:43:41 +01:00
|
|
|
|
CHAR * CurrentVendorName = (CHAR *)"N/A";
|
|
|
|
|
|
CHAR * CurrentDeviceName = (CHAR *)"N/A";
|
2024-12-10 16:04:06 +01:00
|
|
|
|
|
|
|
|
|
|
if (CurrentVendor != NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentVendorName = CurrentVendor->VendorName;
|
2025-01-26 19:42:28 +01:00
|
|
|
|
Device * CurrentDevice = GetDeviceFromVendor(CurrentVendor, PcitreePacket.DeviceInfoList[i].ConfigSpace.DeviceId);
|
2024-12-10 16:04:06 +01:00
|
|
|
|
|
|
|
|
|
|
if (CurrentDevice != NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentDeviceName = CurrentDevice->DeviceName;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ShowMessages("%04x:%02x:%02x:%x | %04x:%04x | %-17.*s | %.*s\n",
|
2024-11-17 21:01:03 +01:00
|
|
|
|
0, // TODO: Add support for domains beyond 0000
|
2025-01-26 19:42:28 +01:00
|
|
|
|
PcitreePacket.DeviceInfoList[i].Bus,
|
|
|
|
|
|
PcitreePacket.DeviceInfoList[i].Device,
|
|
|
|
|
|
PcitreePacket.DeviceInfoList[i].Function,
|
|
|
|
|
|
PcitreePacket.DeviceInfoList[i].ConfigSpace.VendorId,
|
|
|
|
|
|
PcitreePacket.DeviceInfoList[i].ConfigSpace.DeviceId,
|
2026-07-17 17:54:05 +02:00
|
|
|
|
PlatformStrnlen(CurrentVendorName, PCI_NAME_STR_LENGTH),
|
2024-12-10 16:04:06 +01:00
|
|
|
|
CurrentVendorName,
|
2026-07-17 17:54:05 +02:00
|
|
|
|
PlatformStrnlen(CurrentDeviceName, PCI_NAME_STR_LENGTH),
|
2024-12-10 16:04:06 +01:00
|
|
|
|
CurrentDeviceName
|
2024-11-06 14:52:08 +01:00
|
|
|
|
|
2024-11-17 21:01:03 +01:00
|
|
|
|
);
|
2024-12-10 16:04:06 +01:00
|
|
|
|
|
|
|
|
|
|
FreeVendor(CurrentVendor);
|
2024-11-17 15:33:10 +01:00
|
|
|
|
}
|
2024-12-10 16:04:06 +01:00
|
|
|
|
FreePciIdDatabase();
|
2024-11-06 14:52:08 +01:00
|
|
|
|
}
|
2024-11-17 21:01:03 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//
|
|
|
|
|
|
// An err occurred, no results
|
|
|
|
|
|
//
|
|
|
|
|
|
ShowErrorMessage(PcitreePacket.KernelStatus);
|
|
|
|
|
|
}
|
2024-11-06 14:52:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|