Advertisement
Alyks

Untitled

Dec 11th, 2020
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.10 KB | None | 0 0
  1. #include <sys/io.h>
  2. #include "pci_compat.h"
  3. #include <stdio.h>
  4.  
  5. int isBridge(unsigned int mainAddress) {
  6.     unsigned int address = mainAddress + 0x0C;
  7.     outl(address, 0xCF8);
  8.     unsigned int result = (char) (inl(0xCFC) >> 16);
  9.  
  10.     return result != 0;
  11. }
  12.  
  13. void printDeviceAndVendor(unsigned int registerResult) {
  14.     unsigned short int vendorId = registerResult;
  15.     unsigned short int deviceId = registerResult >> 16;
  16.     int vendorFound = 0;
  17.     int deviceFound = 0;
  18.  
  19.     printf("Vendor code: %04x\nDevice code: %04x\n", vendorId, deviceId);
  20.  
  21.     for(int i = 0; i < PCI_VENTABLE_LEN && !vendorFound; i++) {
  22.         if(vendorId == PciVenTable[i].VenId) {
  23.             printf("Vendor: %s\n", PciVenTable[i].VenShort);
  24.             vendorFound = 1;
  25.         }
  26.     }
  27.  
  28.     if(vendorFound) {
  29.         for(int i = 0; i < PCI_DEVTABLE_LEN && !deviceFound; i++) {
  30.             if(deviceId == PciDevTable[i].DevId) {
  31.                 printf("Device: %s\n", PciDevTable[i].Chip);
  32.                 deviceFound = 1;
  33.             }
  34.         }
  35.  
  36.         if(!deviceFound) {
  37.             puts("Device not found");
  38.         }
  39.     } else {
  40.         puts("Vendor not found");
  41.     }
  42. }
  43.  
  44. void printMemoryBase(unsigned int mainAddress) {
  45.     if(!isBridge(mainAddress)) {
  46.         unsigned int address = mainAddress + 0x20;
  47.         outl(address, 0xCF8);
  48.         unsigned int result = inl(0xCFC);
  49.         char memoryBase = result >> 8;
  50.         printf("Memory base: %x\n", memoryBase);
  51.  
  52.         address = mainAddress + 0x24;
  53.         outl(address, 0xCF8);
  54.         result = inl(0xCFC);
  55.         memoryBase = result >> 8;
  56.         printf("Prefetchable memory base: %x\n", memoryBase);
  57.     }
  58. }
  59.  
  60. void printInterruptPin(unsigned int mainAddress) {
  61.     if(!isBridge(mainAddress)) {
  62.         unsigned int address = mainAddress + 0x3C;
  63.         outl(address, 0x0CF8);
  64.         unsigned int result = inl(0xCFC);
  65.         unsigned short int pin = result >> 8;
  66.         printf("Interrupt pin: %02x\n", pin);
  67.     }
  68. }
  69.  
  70. void printClassCode(unsigned int mainAddress) {
  71.     if(isBridge(mainAddress)) {
  72.         unsigned int address = mainAddress + 0x08;
  73.         outl(address, 0x0CF8);
  74.         unsigned int result = inl(0xCFC);
  75.  
  76.         char interface = result >> 8;
  77.         char subClass = result >> 16;
  78.         char baseClass = result >> 24;
  79.  
  80.         int interfaceFound = 0;
  81.  
  82.         printf("Interface : %02x\n", interface);
  83.         printf("Subclass : %02x\n", subClass);
  84.         printf("Baseclass : %02x\n", baseClass);
  85.  
  86.         for(int i = 0; i < PCI_CLASSCODETABLE_LEN && !interfaceFound; i++) {
  87.             if(interface == PciClassCodeTable[i].ProgIf && subClass == PciClassCodeTable[i].SubClass && baseClass == PciClassCodeTable[i].BaseClass) {
  88.                 printf("Description: %s %s %s\n", PciClassCodeTable[i].BaseDesc, PciClassCodeTable[i].SubDesc, PciClassCodeTable[i].ProgDesc);
  89.                 interfaceFound = 1;
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95. int main() {
  96.     if(iopl(3)) {
  97.         puts("Error, try run this program under root user");
  98.     } else {
  99.         unsigned int mainAddress, registerAddress, registerResult;
  100.  
  101.         for(int busNum = 0; busNum < 256; busNum++) {
  102.             for(int deviceNum = 0; deviceNum < 32; deviceNum++) {
  103.                 for(int funcNum = 0; funcNum < 8; funcNum++) {
  104.                     mainAddress = (1 << 31) + (busNum << 16) + (deviceNum << 11) + (funcNum << 8);
  105.                     registerAddress = mainAddress;
  106.                     outl(registerAddress, 0xCF8);
  107.                     registerResult = inl(0xCFC);
  108.  
  109.                     if (registerResult >> 16 != 0xFFFF && registerResult != 0) {
  110.                         printf("Address: %08x\nBus number: %02x,\nDevice number: %02x,\nFunction number: %01x\n", mainAddress, busNum, deviceNum, funcNum);
  111.                         printDeviceAndVendor(registerResult);
  112.                         printMemoryBase(mainAddress);
  113.                         printClassCode(mainAddress);
  114.                         puts("-------------------------------------------------------------");
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.     }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement