Advertisement
Egor_Vakar

Aktos_lab4

Nov 27th, 2022
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <sys/io.h>
  5. #include "pci.h"
  6.  
  7. unsigned getValueOfReg(const int pci, const int device, const int function, const int reg)
  8. {
  9.     int address = (1 << 31) | (pci << 16) | (device << 11) | (function << 8) | (reg << 2);
  10.     outl(address, 0x0CF8);
  11.     int res = inl(0x0CFC);
  12.     return res;
  13. }
  14.  
  15. int main()
  16. {
  17.     unsigned value, deviceId, vendorId, intPin;
  18.  
  19.     if(iopl(3))
  20.     {
  21.         printf("I/O Privilege level change error: %s\nTry running under ROOT user\n",(char *)strerror(errno));
  22.     }
  23.     else {
  24.         for (int pci = 0; pci < 256; pci++) {
  25.             for (int device = 0; device < 32; device++) {
  26.                 for (int function = 0; function < 8; function++) {
  27.                     value = getValueOfReg(pci, device, function, 0);
  28.                     deviceId = (value >> 16) & 0xFFFF;
  29.                     if (value != -1) {
  30.                         vendorId = value & 0xFFFF;
  31.  
  32.                         puts("______________________________________________________");
  33.                         printf("Address: %x-%x-%x\n", pci, device, function);
  34.                         for (int i = 0; i < PCI_VENTABLE_LEN; ++i) {
  35.                             if (PciVenTable[i].VendorId == vendorId) {
  36.                                 printf("VendorID: %s\n", PciVenTable[i].VendorName);
  37.                             }
  38.                         }
  39.                         for (int i = 0; i < PCI_DEVTABLE_LEN; ++i) {
  40.                             if (PciDevTable[i].VendorId == vendorId && PciDevTable[i].DeviceId == deviceId) {
  41.                                 printf("DeviceID: %s\n", PciDevTable[i].DeviceName);
  42.                             }
  43.                         }
  44.                         if ((getValueOfReg(pci, device, function, 3) >> 16) == 0) {
  45.  
  46.                             for (int i = 0; i < 6; ++i) {
  47.                                 if (getValueOfReg(pci, device, function, i + 4) & 0x1)
  48.                                     printf("Base Address Register %x: %x\n", i,
  49.                                            getValueOfReg(pci, device, function, i + 4));
  50.                             }
  51.  
  52.  
  53.                         }else{
  54.                             printf("Primary Bus Number: %02x\n", (getValueOfReg(pci, device, function, 6) >> 0) & 0xFF);
  55.                             printf("Secondary Bus Number: %02x\n", (getValueOfReg(pci, device, function, 6) >> 8) & 0xFF);
  56.                             printf("Subordinate Bus Number: %02x\n", (getValueOfReg(pci, device, function, 6) >> 16) & 0xFF);
  57.                            
  58.                             printf("Memory Base: %02x\n", (getValueOfReg(pci, device, function, 8) >> 0) & 0xFF);
  59.                             printf("Memory Limit: %02x\n", (getValueOfReg(pci, device, function, 8) >> 16) & 0xFF);
  60.                         }
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.     }
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement