Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <sys/io.h>
  4. #include <string.h>
  5. #include "pci_compat.h"
  6.  
  7.  
  8. typedef unsigned char uChar;
  9. typedef unsigned int uInt;
  10. typedef unsigned short uShort;
  11.  
  12. #define MANAGEMENT_PORT 0x0CF8
  13. #define DATA_PORT 0x0CFC
  14. #define BUS_AMOUNT 256
  15. #define DEVICE_AMOUNT 32
  16. #define FUNCTION_AMOUNT 8
  17.  
  18. #define HEX_LEN 4
  19.  
  20. #define DEVICE_VENDER_REGISTER 0x00
  21. #define CLASSCODE_REVISIONID_REGISTER 2
  22. #define HEADER_TYPE_PLUS_REGISTER 3
  23. #define INTERRUPT_PLUS_REGISTER 15
  24.  
  25. #define BUS_NUMBER_REGISTER 6
  26.  
  27. int DeviceExists(uInt configuration)
  28. {
  29. return (configuration >> 16) != 0xFFFF;
  30. }
  31.  
  32. uInt GetConfigurationSpace(uInt busId, uInt deviceId, uInt functionId)
  33. {
  34. return (1 << 31) | (busId << 16) | (deviceId << 11) | (functionId << 8);
  35. }
  36.  
  37. uInt GetConfigurationForRegister(uInt configurationSpace, uInt registerNum)
  38. {
  39. uInt sendConfig = configurationSpace | (registerNum << 2);
  40. outl(sendConfig, MANAGEMENT_PORT);
  41. return inl(DATA_PORT);
  42. }
  43.  
  44. void PrintSeparator()
  45. {
  46. for (int i = 0; i < 20; i++)
  47. printf("-");
  48. printf("\n");
  49. }
  50.  
  51. char* FindVendorName(uShort vendor)
  52. {
  53. for (int i = 0; i < PCI_VENTABLE_LEN; i++)
  54. {
  55. if (vendor == PciVenTable[i].VenId)
  56. return PciVenTable[i].VenShort;
  57. }
  58. return NULL;
  59. }
  60.  
  61. char* FindDeviceName(uShort vendor, uShort device)
  62. {
  63. for (int i = 0; i < PCI_DEVTABLE_LEN; i++)
  64. {
  65. if (vendor == PciDevTable[i].VenId && device == PciDevTable[i].DevId)
  66. return PciDevTable[i].Chip;
  67. }
  68. return NULL;
  69. }
  70.  
  71. void PrintVendorAndDevice(uInt deviceVendorConfig)
  72. {
  73. uShort vendor = deviceVendorConfig & 0xFFFF;
  74. char* vendorName = FindVendorName(vendor);
  75. printf("Vendor ID: %x %s\n", vendor, vendorName ? vendorName : "No Match");
  76.  
  77. uShort device = (deviceVendorConfig >> 16) & 0xFFFF;
  78. char* deviceName = FindDeviceName(vendor, device);
  79. printf("Device ID: %x %s ", device, deviceName ? deviceName : "No Match");
  80. //printf("\n");
  81. }
  82.  
  83. void FindAndPrintDeviceCode(uChar baseClass, uChar subClass, uChar progIf)
  84. {
  85. for (int i = 0; i < PCI_CLASSCODETABLE_LEN; i++)
  86. {
  87. if (PciClassCodeTable[i].BaseClass == baseClass && PciClassCodeTable[i].SubClass == subClass && PciClassCodeTable[i].ProgIf == progIf)
  88. {
  89. printf("| %s %s %s\n", PciClassCodeTable[i].BaseDesc, PciClassCodeTable[i].SubDesc, PciClassCodeTable[i].ProgDesc);
  90. return;
  91. }
  92. }
  93. printf("\n");
  94. }
  95.  
  96. void PrintDeviceClass(uInt deviceConfiguration)
  97. {
  98. uInt ClassCodeConfig = GetConfigurationForRegister(deviceConfiguration, CLASSCODE_REVISIONID_REGISTER);
  99. FindAndPrintDeviceCode((ClassCodeConfig >> 24) & 0xFF, (ClassCodeConfig >> 16) & 0xFF, (ClassCodeConfig >> 8) & 0xFF);
  100. }
  101.  
  102. int IsBridge(uInt deviceConfiguration)
  103. {
  104. uInt headerType = (GetConfigurationForRegister(deviceConfiguration, HEADER_TYPE_PLUS_REGISTER) >> 16) & 0xFF;
  105. return (headerType >> 7) & 0x1;
  106. }
  107.  
  108. void PrintNotBridgeInfo(uInt deviceConfiguration)
  109. {
  110. uInt interruptConfig = GetConfigurationForRegister(deviceConfiguration, INTERRUPT_PLUS_REGISTER);
  111. uInt interruptLine = interruptConfig & 0xFF;
  112. printf("Interrupt Line ");
  113. if (interruptLine != 0xFF)
  114. printf("IRQ%d\n", interruptLine);
  115. else
  116. printf("not used\n");
  117.  
  118.  
  119. uInt interruptPin = (interruptConfig >> 8) & 0xFF;
  120. printf("Interrupt Pin: ");
  121. switch (interruptPin)
  122. {
  123. case 0: printf("not used\n"); break;
  124. case 0xFF: printf("reserved\n"); break;
  125. default: printf("INT%c#\n", interruptPin + 'A');
  126. }
  127. }
  128.  
  129. void PrintBridgeInfo(uInt deviceConfiguration)
  130. {
  131. uInt busNumberConfig = GetConfigurationForRegister(deviceConfiguration, BUS_NUMBER_REGISTER);
  132. printf("Primary Bus Number: %x, Secondary Bus Number: %x, Subordinate Bus Number: %x\n",
  133. busNumberConfig & 0xFF, (busNumberConfig >> 8) & 0xFF, (busNumberConfig >> 16) & 0xFF);
  134. }
  135.  
  136. void FindDevices()
  137. {
  138. int deviceAmount = 0;
  139. for (uInt busId = 0; busId < BUS_AMOUNT; busId++)
  140. {
  141. for (uInt deviceId = 0; deviceId < DEVICE_AMOUNT; deviceId++)
  142. {
  143. for (uInt functionId = 0; functionId < FUNCTION_AMOUNT; functionId++)
  144. {
  145. uInt deviceConfiguration = GetConfigurationSpace(busId, deviceId, functionId);
  146. uInt deviceVendorConfig = GetConfigurationForRegister(deviceConfiguration, DEVICE_VENDER_REGISTER);
  147. if (DeviceExists(deviceVendorConfig))
  148. {
  149. PrintSeparator();
  150. printf("%x:%x.%x\n", busId, deviceId, functionId);
  151.  
  152. PrintVendorAndDevice(deviceVendorConfig);
  153. printf("\n");
  154. //----------------------------------------------------
  155.  
  156. if (IsBridge(deviceConfiguration))
  157. PrintBridgeInfo(deviceConfiguration);
  158. else{
  159. cacheSize = deviceConfiguration & 0x000000FF;
  160. printf("\tcache size: %d ", cacheSize);}
  161. //PrintNotBridgeInfo(deviceConfiguration);
  162.  
  163. deviceAmount++;
  164. }
  165. }
  166. }
  167. }
  168. printf("\nTotal: %d\n", deviceAmount);
  169. }
  170.  
  171. int main()
  172. {
  173. if(iopl(3)) //задание уровня приоритета
  174. {
  175. printf("I/O Privilege level change error: %s\nTry running under ROOT user\n");
  176. return 1;
  177. }
  178.  
  179. FindDevices();
  180.  
  181. return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement