Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/io.h>
- #include <string.h>
- #include "pci_compat.h"
- typedef unsigned char uChar;
- typedef unsigned int uInt;
- typedef unsigned short uShort;
- #define MANAGEMENT_PORT 0x0CF8
- #define DATA_PORT 0x0CFC
- #define BUS_AMOUNT 256
- #define DEVICE_AMOUNT 32
- #define FUNCTION_AMOUNT 8
- #define HEX_LEN 4
- #define DEVICE_VENDER_REGISTER 0x00
- #define CLASSCODE_REVISIONID_REGISTER 2
- #define HEADER_TYPE_PLUS_REGISTER 3
- #define INTERRUPT_PLUS_REGISTER 15
- #define BUS_NUMBER_REGISTER 6
- int DeviceExists(uInt configuration)
- {
- return (configuration >> 16) != 0xFFFF;
- }
- uInt GetConfigurationSpace(uInt busId, uInt deviceId, uInt functionId)
- {
- return (1 << 31) | (busId << 16) | (deviceId << 11) | (functionId << 8);
- }
- uInt GetConfigurationForRegister(uInt configurationSpace, uInt registerNum)
- {
- uInt sendConfig = configurationSpace | (registerNum << 2);
- outl(sendConfig, MANAGEMENT_PORT);
- return inl(DATA_PORT);
- }
- void PrintSeparator()
- {
- for (int i = 0; i < 20; i++)
- printf("-");
- printf("\n");
- }
- char* FindVendorName(uShort vendor)
- {
- for (int i = 0; i < PCI_VENTABLE_LEN; i++)
- {
- if (vendor == PciVenTable[i].VenId)
- return PciVenTable[i].VenShort;
- }
- return NULL;
- }
- char* FindDeviceName(uShort vendor, uShort device)
- {
- for (int i = 0; i < PCI_DEVTABLE_LEN; i++)
- {
- if (vendor == PciDevTable[i].VenId && device == PciDevTable[i].DevId)
- return PciDevTable[i].Chip;
- }
- return NULL;
- }
- void PrintVendorAndDevice(uInt deviceVendorConfig)
- {
- uShort vendor = deviceVendorConfig & 0xFFFF;
- char* vendorName = FindVendorName(vendor);
- printf("Vendor ID: %x %s\n", vendor, vendorName ? vendorName : "No Match");
- uShort device = (deviceVendorConfig >> 16) & 0xFFFF;
- char* deviceName = FindDeviceName(vendor, device);
- printf("Device ID: %x %s ", device, deviceName ? deviceName : "No Match");
- //printf("\n");
- }
- void FindAndPrintDeviceCode(uChar baseClass, uChar subClass, uChar progIf)
- {
- for (int i = 0; i < PCI_CLASSCODETABLE_LEN; i++)
- {
- if (PciClassCodeTable[i].BaseClass == baseClass && PciClassCodeTable[i].SubClass == subClass && PciClassCodeTable[i].ProgIf == progIf)
- {
- printf("| %s %s %s\n", PciClassCodeTable[i].BaseDesc, PciClassCodeTable[i].SubDesc, PciClassCodeTable[i].ProgDesc);
- return;
- }
- }
- printf("\n");
- }
- void PrintDeviceClass(uInt deviceConfiguration)
- {
- uInt ClassCodeConfig = GetConfigurationForRegister(deviceConfiguration, CLASSCODE_REVISIONID_REGISTER);
- FindAndPrintDeviceCode((ClassCodeConfig >> 24) & 0xFF, (ClassCodeConfig >> 16) & 0xFF, (ClassCodeConfig >> 8) & 0xFF);
- }
- int IsBridge(uInt deviceConfiguration)
- {
- uInt headerType = (GetConfigurationForRegister(deviceConfiguration, HEADER_TYPE_PLUS_REGISTER) >> 16) & 0xFF;
- return (headerType >> 7) & 0x1;
- }
- void PrintNotBridgeInfo(uInt deviceConfiguration)
- {
- uInt interruptConfig = GetConfigurationForRegister(deviceConfiguration, INTERRUPT_PLUS_REGISTER);
- uInt interruptLine = interruptConfig & 0xFF;
- printf("Interrupt Line ");
- if (interruptLine != 0xFF)
- printf("IRQ%d\n", interruptLine);
- else
- printf("not used\n");
- uInt interruptPin = (interruptConfig >> 8) & 0xFF;
- printf("Interrupt Pin: ");
- switch (interruptPin)
- {
- case 0: printf("not used\n"); break;
- case 0xFF: printf("reserved\n"); break;
- default: printf("INT%c#\n", interruptPin + 'A');
- }
- }
- void PrintBridgeInfo(uInt deviceConfiguration)
- {
- uInt busNumberConfig = GetConfigurationForRegister(deviceConfiguration, BUS_NUMBER_REGISTER);
- printf("Primary Bus Number: %x, Secondary Bus Number: %x, Subordinate Bus Number: %x\n",
- busNumberConfig & 0xFF, (busNumberConfig >> 8) & 0xFF, (busNumberConfig >> 16) & 0xFF);
- }
- void FindDevices()
- {
- int deviceAmount = 0;
- for (uInt busId = 0; busId < BUS_AMOUNT; busId++)
- {
- for (uInt deviceId = 0; deviceId < DEVICE_AMOUNT; deviceId++)
- {
- for (uInt functionId = 0; functionId < FUNCTION_AMOUNT; functionId++)
- {
- uInt deviceConfiguration = GetConfigurationSpace(busId, deviceId, functionId);
- uInt deviceVendorConfig = GetConfigurationForRegister(deviceConfiguration, DEVICE_VENDER_REGISTER);
- if (DeviceExists(deviceVendorConfig))
- {
- PrintSeparator();
- printf("%x:%x.%x\n", busId, deviceId, functionId);
- PrintVendorAndDevice(deviceVendorConfig);
- printf("\n");
- //----------------------------------------------------
- if (IsBridge(deviceConfiguration))
- PrintBridgeInfo(deviceConfiguration);
- else{
- cacheSize = deviceConfiguration & 0x000000FF;
- printf("\tcache size: %d ", cacheSize);}
- //PrintNotBridgeInfo(deviceConfiguration);
- deviceAmount++;
- }
- }
- }
- }
- printf("\nTotal: %d\n", deviceAmount);
- }
- int main()
- {
- if(iopl(3)) //задание уровня приоритета
- {
- printf("I/O Privilege level change error: %s\nTry running under ROOT user\n");
- return 1;
- }
- FindDevices();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement