⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pciscan.cpp

📁 对PCI总线设备配置空间进行访问`枚举的小程序,
💻 CPP
字号:
/******************************************************************************
Copyright (c) VIA Technologies(China), Inc.
Author: Charlie Feng, Software Team
File: pciscan.cpp
Version: 0.2
Last Modified: 2007-04-25
Description: This program scans and dumps all the devices connected to the 
             PCI buses of the local machine. 
******************************************************************************/

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "winio.h"
#include "pciscan.h"

const char* VERSION = "v0.2";


/******************************************************
Function Name: main
Receives: 
Returns: int
Description: entry of execution
******************************************************/
int main(void)
{
	bool bResult;
	// Initialize WinIO lib resources
	bResult = InitializeWinIo();
	
	if(!bResult) {
		printf("WinIO Initialization Failed.\n");
		return -1;
	}
	int bus, dev, func;
	int nBusCount=0, nDeviceCount=0, nFunctionCount=0;
	DWORD dwAddress;
	DWORD dwData;
	FILE* hFile;
	char szFileName[FILE_NAME_MAX];
	
	// Display welcome message
	WelcomeMsg();
	// Scan all possible PCI BUSes
	for(bus = 0; bus <= BUS_MAX; ++bus) {
		// Scan each device connected to the BUS
		for(dev = 0; dev <= DEVICE_MAX; ++dev) {
			// List all the functions provided by the device
			for(func = 0; func <= FUNCTION_MAX; ++func) {
				// Calculate the Index 
				dwAddress = PCI_Index(bus, dev, func);

				// Get Vendor ID
				_outpd(PCI_CONFIG_ADDRESS, dwAddress);
				dwData = _inpd(PCI_CONFIG_DATA);

				// Check out whether the Vendor ID is legal or not
				if ((WORD)dwData != 0xFFFF) {
					// Dump the Identifiers to screen
					printf("    |\t%2.2X\t%2.2X\t%1X\t", bus, dev, func);

					// Dump Vendor ID and Device ID
					printf("%4.4X\t%4.4X\t", (WORD)dwData, dwData>>16);

					// Dump Class Code
					_outpd(PCI_CONFIG_ADDRESS, dwAddress | 0x8);
					dwData = _inpd(PCI_CONFIG_DATA);
					printf("%6.6lX\t|\n", dwData>>8);

					// Create and Open Backup Files
					sprintf(szFileName, "BUS-%2.2X-DEV-%2.2X-FUNC-%X.inf", bus, dev, func);

					if (NULL != (hFile = fopen(szFileName, "wb"))) {
						// Dump all the Configuration Space to hard disk
						for (int i = 0; i < 256; i += 4) {
							// Get a specific register
							_outpd(PCI_CONFIG_ADDRESS, dwAddress | i);
							dwData = _inpd(PCI_CONFIG_DATA);

							// Write to file stream
							fprintf(hFile, "%8.8X\t\r\n", (DWORD)dwData);
						}
						// Close the file
						fclose(hFile);
					}
					_outpd(PCI_CONFIG_ADDRESS, dwAddress | 0xC);
					dwData = _inpd(PCI_CONFIG_DATA);
					if ( ((dwData & 0x800000) == 0) && (!func) ) break;
				} // end of "if Vendor ID is legal"
			} // end of Function
		} // end of Device
	} // end of Bus

	// Release WinIO lib resources
    ShutdownWinIo();
	printf("    \\===================================================\\\n");

	printf("Done.");
	getchar();
	return 0;
}


/**************************************************
Function Name: PCI_Index
Receives: unsigned int, unsigned int, unsigned int
Returns: DWORD
Description: Calculate the index that can
             be written to PCI_CONFIG_ADDRESS
**************************************************/
DWORD PCI_Index(unsigned int nBus, unsigned int nDevice, unsigned int nFunction)
{
	DWORD dwIndex = 0;
	dwIndex |= ( (nBus & BUS_MAX) << 8 );
	dwIndex |= ( (nDevice & DEVICE_MAX) << 3 );
	dwIndex |= (nFunction & FUNCTION_MAX);
	dwIndex <<= 8;
	dwIndex |= 0x80000000L;
	return dwIndex;
}

/**************************************************
Function Name: WelcomeMsg
Receives:
Returns:
Description: Show welcome
**************************************************/
void WelcomeMsg()
{
	printf("   *******************************************************");
	printf("\n   *\t     Welcome to PCISCAN %s for Windows.        *\n", VERSION);
	printf("   *\t (c) Copyright VIA Technologies (China), Inc.    *\n");
	printf("   *******************************************************\n");
	printf("Scanning Your PCI...\n");
	printf("    |===================================================|\n");
	printf("    |\tBus\tDevice\tFunc\tVendor\tDevice\tClass\t|\n");
	printf("    |---------------------------------------------------|\n");	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -