Vanya_Shestakov

Untitled

Dec 21st, 2021 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.14 KB | None | 0 0
  1. #pragma warning(disable:4996)
  2. #include <stdio.h>
  3. #include <sys/io.h>
  4. #include <errno.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdbool.h>
  8. #include "pci.h"
  9.  
  10. #define MAX_BUS 256
  11. #define MAX_DEVICE 32
  12. #define MAX_FUNCTIONS 8
  13.  
  14. #define ID_REGISTER 0
  15.  
  16. #define DEVICEID_SHIFT 16
  17. #define BUS_SHIFT 16
  18. #define DEVICE_SHIFT 11
  19. #define FUNCTION_SHIFT 8
  20. #define REGISTER_SHIFT 2
  21.  
  22. #define CONTROL_PORT 0x0CF8
  23. #define DATA_PORT 0x0CFC
  24.  
  25. FILE *file;
  26.  
  27. void outputCashLineSize(long regData) {
  28.     fputs(" TASK 5\n", file);
  29.     puts(" TASK 5");
  30.  
  31.     int cashLine = regData & 0xFF;
  32.     fputs("Cash Line: ", file);
  33.     printf("Cash Line: ");
  34.  
  35.     if (cashLine <= 128) {
  36.         fprintf(file, " size of cash: %d\n", cashLine);
  37.         printf(" size of cash: %d\n", cashLine);
  38.     } else {
  39.         fputs("invalid line number\n", file);
  40.         puts("invalid line number\n");
  41.     }
  42. }
  43.  
  44. void outputClassCode(long regData) {
  45.     fputs(" TASK 1\n", file);
  46.     puts(" TASK 1");
  47.  
  48.  
  49.     int classCode = (regData >> 24) & 0xFF;
  50.     char *classData;
  51.     switch (classCode) {
  52.         case 0:
  53.             classData = "devices before classification";
  54.             break;
  55.         case 1:
  56.             classData = "storage controllers";
  57.             break;
  58.         case 2:
  59.             classData = "network controllers";
  60.             break;
  61.         case 3:
  62.             classData = "display controllers";
  63.             break;
  64.         case 4:
  65.             classData = "multimedia devices";
  66.             break;
  67.         case 5:
  68.             classData = "memory controllers";
  69.             break;
  70.         case 6:
  71.             classData = "bridges";
  72.             break;
  73.         case 7:
  74.             classData = "communication controllers";
  75.             break;
  76.         case 8:
  77.             classData = "system peripherals";
  78.             break;
  79.         case 9:
  80.             classData = "input device controllers";
  81.             break;
  82.         case 10:
  83.             classData = "docking stations";
  84.             break;
  85.         case 11:
  86.             classData = "processors";
  87.             break;
  88.         case 12:
  89.             classData = "serial bus controllers";
  90.             break;
  91.         case 13:
  92.             classData = "wireless interface controllers";
  93.             break;
  94.         default:
  95.             classData = "invalid class number";
  96.             break;
  97.     }
  98.     fprintf(file, "Class name: %s\n", classData);
  99.     printf( "Class name: %s\n", classData);
  100. }
  101.  
  102. void outputIO(long regData) {
  103.     fputs(" TASK 9\n", file);
  104.     puts(" TASK 9");
  105.  
  106.     long IOBase = regData & 0xFF;
  107.     long IOLimit = (regData >> 8) & 0xFF;
  108.     fprintf(file, "I/O BASE: %x\n", IOBase);
  109.     printf( "I/O BASE: %x\n", IOBase);
  110.  
  111.     fprintf(file, "I/O LIMIT: %x\n", IOLimit);
  112.     printf( "I/O LIMIT: %x\n", IOLimit);
  113. }
  114.  
  115.  
  116. char *getVendorName(int vendorID) {
  117.     int i;
  118.     for (i = 0; i < PCI_VENTABLE_LEN; i++) {
  119.         if (PciVenTable[i].VendorId == vendorID) {
  120.             return PciVenTable[i].VendorName;
  121.         }
  122.     }
  123.     return NULL;
  124. }
  125.  
  126. char *getDeviceName(int vendorID, int deviceID) {
  127.     int i;
  128.     for ( i = 0; i < PCI_DEVTABLE_LEN; i++) {
  129.         if (PciDevTable[i].VendorId == vendorID && PciDevTable[i].DeviceId == deviceID) {
  130.             return PciDevTable[i].DeviceName;
  131.         }
  132.     }
  133.     return NULL;
  134. }
  135.  
  136. void outputVendorData(int vendorID)
  137. {
  138.     char *vendorName = getVendorName(vendorID);
  139.     fprintf(file, "Vendor ID: %04d, %s\n", vendorID, vendorName ? vendorName : "unknown vendor");
  140.     printf( "Vendor ID: %04d, %s\n", vendorID, vendorName ? vendorName : "Unknown vendor");
  141. }
  142.  
  143. void outputDeviceData(int vendorID, int deviceID)
  144. {
  145.     char *deviceName = getDeviceName(vendorID, deviceID);
  146.     fprintf(file, "Device ID: %04d, %s\n", deviceID, deviceName ? deviceName : "unknown device");
  147.     printf( "Device ID: %04d, %s\n", deviceID, deviceName ? deviceName : "Unknown device");
  148. }
  149.  
  150. void outputGeneralData(int bus, int device, int function, int regData) {
  151.     fprintf(file, "%x:%x:%x\n", bus, device, function);
  152.     printf( "%x:%x:%x\n", bus, device, function);
  153.     int deviceID = regData >> DEVICEID_SHIFT;
  154.     int vendorID = regData & 0xFFFF;
  155.     outputVendorData(vendorID);
  156.     outputDeviceData(vendorID, deviceID);
  157. }
  158.  
  159.  
  160.  
  161. long readRegister(int bus, int device, int function, int reg) {
  162.     long configRegAddress =
  163.             (1 << 31) |
  164.             (bus << BUS_SHIFT) |
  165.             (device << DEVICE_SHIFT) |
  166.             (function << FUNCTION_SHIFT) |
  167.             (reg << REGISTER_SHIFT);
  168.     outl(configRegAddress, CONTROL_PORT);
  169.     return inl(DATA_PORT);
  170.  
  171.     return 0;
  172. }
  173.  
  174. bool isBridge(int bus, int device, int function) {
  175.     long htypeRegData = readRegister(bus, device, function, 3);
  176.     return ((htypeRegData >> 16) & 0xFF) & 1;
  177. }
  178.  
  179.  
  180. void printInfo(int bus, int device, int function) {
  181.     long idRegData = readRegister(bus, device, function, ID_REGISTER);
  182.  
  183.     if (idRegData != 0xFFFFFFFF) { // if there is a device
  184.         outputGeneralData(bus, device, function, idRegData);
  185.  
  186.         if (isBridge(bus, device, function)) {
  187.             fprintf(file, "\nIs bridge\n\n");
  188.             printf("\nIs bridge\n\n");
  189.             outputIO(readRegister(bus, device, function, 7));
  190.            
  191.         } else {
  192.             fprintf(file, "\nNot a bridge\n\n");
  193.             printf("\nNot a bridge\n\n");
  194.             outputClassCode(readRegister(bus,device,function,2));
  195.             outputCashLineSize(readRegister(bus, device, function, 3));
  196.         }
  197.         fputs("---------------------------------------------------\n", file);
  198.         puts("---------------------------------------------------\n");
  199.     }
  200. }
  201.  
  202. int main() {
  203.  
  204.     if (iopl(3)) {
  205.         printf("I/O Privilege level change error: %s\nTry running under ROOT user\n", strerror(errno));
  206.  
  207.         return 1;
  208.     }
  209.  
  210.     int bus;
  211.     int device;
  212.     int func;
  213.     file = fopen("file.txt", "w");
  214.  
  215.     for ( bus = 0; bus < MAX_BUS; bus++) {
  216.         for (device = 0; device < MAX_DEVICE; device++) {
  217.             for ( func = 0; func < MAX_FUNCTIONS; func++) {
  218.                 printInfo(bus, device, func);
  219.             }
  220.         }
  221.     }
  222.  
  223.     fclose(file);
  224.     return 0;
  225. }
Add Comment
Please, Sign In to add comment