Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning(disable:4996)
- #include <stdio.h>
- #include <sys/io.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include "pci.h"
- #define MAX_BUS 256
- #define MAX_DEVICE 32
- #define MAX_FUNCTIONS 8
- #define ID_REGISTER 0
- #define DEVICEID_SHIFT 16
- #define BUS_SHIFT 16
- #define DEVICE_SHIFT 11
- #define FUNCTION_SHIFT 8
- #define REGISTER_SHIFT 2
- #define CONTROL_PORT 0x0CF8
- #define DATA_PORT 0x0CFC
- FILE *file;
- void outputCashLineSize(long regData) {
- fputs(" TASK 5\n", file);
- puts(" TASK 5");
- int cashLine = regData & 0xFF;
- fputs("Cash Line: ", file);
- printf("Cash Line: ");
- if (cashLine <= 128) {
- fprintf(file, " size of cash: %d\n", cashLine);
- printf(" size of cash: %d\n", cashLine);
- } else {
- fputs("invalid line number\n", file);
- puts("invalid line number\n");
- }
- }
- void outputClassCode(long regData) {
- fputs(" TASK 1\n", file);
- puts(" TASK 1");
- int classCode = (regData >> 24) & 0xFF;
- char *classData;
- switch (classCode) {
- case 0:
- classData = "devices before classification";
- break;
- case 1:
- classData = "storage controllers";
- break;
- case 2:
- classData = "network controllers";
- break;
- case 3:
- classData = "display controllers";
- break;
- case 4:
- classData = "multimedia devices";
- break;
- case 5:
- classData = "memory controllers";
- break;
- case 6:
- classData = "bridges";
- break;
- case 7:
- classData = "communication controllers";
- break;
- case 8:
- classData = "system peripherals";
- break;
- case 9:
- classData = "input device controllers";
- break;
- case 10:
- classData = "docking stations";
- break;
- case 11:
- classData = "processors";
- break;
- case 12:
- classData = "serial bus controllers";
- break;
- case 13:
- classData = "wireless interface controllers";
- break;
- default:
- classData = "invalid class number";
- break;
- }
- fprintf(file, "Class name: %s\n", classData);
- printf( "Class name: %s\n", classData);
- }
- void outputIO(long regData) {
- fputs(" TASK 9\n", file);
- puts(" TASK 9");
- long IOBase = regData & 0xFF;
- long IOLimit = (regData >> 8) & 0xFF;
- fprintf(file, "I/O BASE: %x\n", IOBase);
- printf( "I/O BASE: %x\n", IOBase);
- fprintf(file, "I/O LIMIT: %x\n", IOLimit);
- printf( "I/O LIMIT: %x\n", IOLimit);
- }
- char *getVendorName(int vendorID) {
- int i;
- for (i = 0; i < PCI_VENTABLE_LEN; i++) {
- if (PciVenTable[i].VendorId == vendorID) {
- return PciVenTable[i].VendorName;
- }
- }
- return NULL;
- }
- char *getDeviceName(int vendorID, int deviceID) {
- int i;
- for ( i = 0; i < PCI_DEVTABLE_LEN; i++) {
- if (PciDevTable[i].VendorId == vendorID && PciDevTable[i].DeviceId == deviceID) {
- return PciDevTable[i].DeviceName;
- }
- }
- return NULL;
- }
- void outputVendorData(int vendorID)
- {
- char *vendorName = getVendorName(vendorID);
- fprintf(file, "Vendor ID: %04d, %s\n", vendorID, vendorName ? vendorName : "unknown vendor");
- printf( "Vendor ID: %04d, %s\n", vendorID, vendorName ? vendorName : "Unknown vendor");
- }
- void outputDeviceData(int vendorID, int deviceID)
- {
- char *deviceName = getDeviceName(vendorID, deviceID);
- fprintf(file, "Device ID: %04d, %s\n", deviceID, deviceName ? deviceName : "unknown device");
- printf( "Device ID: %04d, %s\n", deviceID, deviceName ? deviceName : "Unknown device");
- }
- void outputGeneralData(int bus, int device, int function, int regData) {
- fprintf(file, "%x:%x:%x\n", bus, device, function);
- printf( "%x:%x:%x\n", bus, device, function);
- int deviceID = regData >> DEVICEID_SHIFT;
- int vendorID = regData & 0xFFFF;
- outputVendorData(vendorID);
- outputDeviceData(vendorID, deviceID);
- }
- long readRegister(int bus, int device, int function, int reg) {
- long configRegAddress =
- (1 << 31) |
- (bus << BUS_SHIFT) |
- (device << DEVICE_SHIFT) |
- (function << FUNCTION_SHIFT) |
- (reg << REGISTER_SHIFT);
- outl(configRegAddress, CONTROL_PORT);
- return inl(DATA_PORT);
- return 0;
- }
- bool isBridge(int bus, int device, int function) {
- long htypeRegData = readRegister(bus, device, function, 3);
- return ((htypeRegData >> 16) & 0xFF) & 1;
- }
- void printInfo(int bus, int device, int function) {
- long idRegData = readRegister(bus, device, function, ID_REGISTER);
- if (idRegData != 0xFFFFFFFF) { // if there is a device
- outputGeneralData(bus, device, function, idRegData);
- if (isBridge(bus, device, function)) {
- fprintf(file, "\nIs bridge\n\n");
- printf("\nIs bridge\n\n");
- outputIO(readRegister(bus, device, function, 7));
- } else {
- fprintf(file, "\nNot a bridge\n\n");
- printf("\nNot a bridge\n\n");
- outputClassCode(readRegister(bus,device,function,2));
- outputCashLineSize(readRegister(bus, device, function, 3));
- }
- fputs("---------------------------------------------------\n", file);
- puts("---------------------------------------------------\n");
- }
- }
- int main() {
- if (iopl(3)) {
- printf("I/O Privilege level change error: %s\nTry running under ROOT user\n", strerror(errno));
- return 1;
- }
- int bus;
- int device;
- int func;
- file = fopen("file.txt", "w");
- for ( bus = 0; bus < MAX_BUS; bus++) {
- for (device = 0; device < MAX_DEVICE; device++) {
- for ( func = 0; func < MAX_FUNCTIONS; func++) {
- printInfo(bus, device, func);
- }
- }
- }
- fclose(file);
- return 0;
- }
Add Comment
Please, Sign In to add comment