DMIDecoder.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include <Kernel/ACPI/DMIDecoder.h>
  2. #include <Kernel/StdLib.h>
  3. #include <Kernel/VM/MemoryManager.h>
  4. static DMIDecoder* s_dmi_decoder;
  5. //#define SMBIOS_DEBUG
  6. #define SMBIOS_BASE_SEARCH_ADDR 0xf0000
  7. #define SMBIOS_END_SEARCH_ADDR 0xfffff
  8. #define SMBIOS_SEARCH_AREA_SIZE (SMBIOS_END_SEARCH_ADDR - SMBIOS_BASE_SEARCH_ADDR)
  9. DMIDecoder& DMIDecoder::the()
  10. {
  11. if (s_dmi_decoder == nullptr) {
  12. s_dmi_decoder = new DMIDecoder(true);
  13. }
  14. return *s_dmi_decoder;
  15. }
  16. void DMIDecoder::initialize()
  17. {
  18. if (s_dmi_decoder == nullptr) {
  19. s_dmi_decoder = new DMIDecoder(true);
  20. }
  21. }
  22. void DMIDecoder::initialize_untrusted()
  23. {
  24. if (s_dmi_decoder == nullptr) {
  25. s_dmi_decoder = new DMIDecoder(false);
  26. }
  27. }
  28. void DMIDecoder::initialize_parser()
  29. {
  30. if (m_entry32bit_point != nullptr || m_entry64bit_point != nullptr) {
  31. m_operable = true;
  32. kprintf("DMI Decoder is enabled\n");
  33. if (m_entry64bit_point != nullptr) {
  34. kprintf("DMIDecoder: SMBIOS 64bit Entry point @ P 0x%x\n", m_entry64bit_point);
  35. m_use_64bit_entry = true;
  36. m_structure_table = (SMBIOS::TableHeader*)m_entry64bit_point->table_ptr;
  37. m_structures_count = m_entry64bit_point->table_maximum_size;
  38. m_table_length = m_entry64bit_point->table_maximum_size;
  39. } else if (m_entry32bit_point != nullptr) {
  40. kprintf("DMIDecoder: SMBIOS 32bit Entry point @ P 0x%x\n", m_entry32bit_point);
  41. m_use_64bit_entry = false;
  42. m_structure_table = (SMBIOS::TableHeader*)m_entry32bit_point->legacy_structure.smbios_table_ptr;
  43. m_structures_count = m_entry32bit_point->legacy_structure.smbios_tables_count;
  44. m_table_length = m_entry32bit_point->legacy_structure.smboios_table_length;
  45. }
  46. kprintf("DMIDecoder: Data table @ P 0x%x\n", m_structure_table);
  47. enumerate_smbios_tables();
  48. } else {
  49. m_operable = false;
  50. kprintf("DMI Decoder is disabled. Cannot find SMBIOS tables.\n");
  51. }
  52. }
  53. void DMIDecoder::enumerate_smbios_tables()
  54. {
  55. u32 table_length = m_table_length;
  56. SMBIOS::TableHeader* physical_table = m_structure_table;
  57. auto region = MM.allocate_kernel_region(PAGE_ROUND_UP(table_length), "DMI Decoder Entry Point Finding", Region::Access::Read);
  58. PhysicalAddress paddr = PhysicalAddress((u32)physical_table & PAGE_MASK);
  59. mmap_region(*region, paddr);
  60. auto* v_smbios_table = (SMBIOS::TableHeader*)(region->vaddr().get() + ((u32)physical_table & (~PAGE_MASK)));
  61. u32 structures_count = 0;
  62. while (table_length > 0) {
  63. #ifdef SMBIOS_DEBUG
  64. dbgprintf("DMIDecoder: Examining table @ P 0x%x\n", physical_table);
  65. #endif
  66. structures_count++;
  67. if (v_smbios_table->type == (u32)SMBIOS::TableType::EndOfTable) {
  68. kprintf("DMIDecoder: Detected table with type 127, End of SMBIOS data.\n");
  69. break;
  70. }
  71. m_smbios_tables.append(physical_table);
  72. table_length -= v_smbios_table->length;
  73. kprintf("DMIDecoder: Detected table with type %u\n", v_smbios_table->type);
  74. SMBIOS::TableHeader* physical_next_table = get_next_physical_table(*physical_table);
  75. #ifdef SMBIOS_DEBUG
  76. dbgprintf("DMIDecoder: Next table @ P 0x%x\n", physical_next_table);
  77. #endif
  78. if (physical_next_table == nullptr)
  79. break;
  80. v_smbios_table = (SMBIOS::TableHeader*)(region->vaddr().get() + (u32)physical_next_table - paddr.get());
  81. physical_table = physical_next_table;
  82. }
  83. m_structures_count = structures_count;
  84. }
  85. size_t DMIDecoder::get_table_size(SMBIOS::TableHeader& table)
  86. {
  87. // FIXME: Make sure we have some mapping here so we don't rely on existing identity mapping...
  88. #ifdef SMBIOS_DEBUG
  89. dbgprintf("DMIDecoder: table legnth - 0x%x\n", table.length);
  90. #endif
  91. const char* strtab = (char*)&table + table.length;
  92. size_t index = 1;
  93. while (strtab[index - 1] != '\0' || strtab[index] != '\0') {
  94. if (index > m_table_length) {
  95. ASSERT_NOT_REACHED(); // FIXME: Instead of halting, find a better solution (Hint: use m_operable to disallow further use of DMIDecoder)
  96. }
  97. index++;
  98. }
  99. #ifdef SMBIOS_DEBUG
  100. dbgprintf("DMIDecoder: table size - 0x%x\n", table.length + i + 1);
  101. #endif
  102. return table.length + index + 1;
  103. }
  104. SMBIOS::TableHeader* DMIDecoder::get_next_physical_table(SMBIOS::TableHeader& p_table)
  105. {
  106. return (SMBIOS::TableHeader*)((u32)&p_table + get_table_size(p_table));
  107. }
  108. SMBIOS::TableHeader* DMIDecoder::get_smbios_physical_table_by_handle(u16 handle)
  109. {
  110. auto region = MM.allocate_kernel_region(PAGE_ROUND_UP(PAGE_SIZE * 2), "DMI Decoder Finding Table", Region::Access::Read);
  111. for (auto* table : m_smbios_tables) {
  112. if (!table)
  113. continue;
  114. PhysicalAddress paddr = PhysicalAddress((u32)table & PAGE_MASK);
  115. mmap_region(*region, paddr);
  116. SMBIOS::TableHeader* table_v_ptr = (SMBIOS::TableHeader*)(region->vaddr().get() + ((u32)table & (~PAGE_MASK)));
  117. if (table_v_ptr->handle == handle) {
  118. return table;
  119. }
  120. }
  121. return nullptr;
  122. }
  123. SMBIOS::TableHeader* DMIDecoder::get_smbios_physical_table_by_type(u8 table_type)
  124. {
  125. auto region = MM.allocate_kernel_region(PAGE_ROUND_UP(PAGE_SIZE * 2), "DMI Decoder Finding Table", Region::Access::Read);
  126. for (auto* table : m_smbios_tables) {
  127. if (!table)
  128. continue;
  129. PhysicalAddress paddr = PhysicalAddress((u32)table & PAGE_MASK);
  130. mmap_region(*region, paddr);
  131. SMBIOS::TableHeader* table_v_ptr = (SMBIOS::TableHeader*)(region->vaddr().get() + ((u32)table & (~PAGE_MASK)));
  132. if (table_v_ptr->type == table_type) {
  133. return table;
  134. }
  135. }
  136. return nullptr;
  137. }
  138. DMIDecoder::DMIDecoder(bool trusted)
  139. : m_entry32bit_point(find_entry32bit_point())
  140. , m_entry64bit_point(find_entry64bit_point())
  141. , m_structure_table(nullptr)
  142. , m_untrusted(!trusted)
  143. {
  144. if (!trusted) {
  145. kprintf("DMI Decoder initialized as untrusted due to user request.\n");
  146. }
  147. initialize_parser();
  148. }
  149. SMBIOS::EntryPoint64bit* DMIDecoder::find_entry64bit_point()
  150. {
  151. auto region = MM.allocate_kernel_region(PAGE_ROUND_UP(SMBIOS_SEARCH_AREA_SIZE), "DMI Decoder Entry Point Finding", Region::Access::Read);
  152. PhysicalAddress paddr = PhysicalAddress(SMBIOS_BASE_SEARCH_ADDR);
  153. mmap_region(*region, paddr);
  154. char* tested_physical_ptr = (char*)paddr.get();
  155. for (char* entry_str = (char*)(region->vaddr().get()); entry_str < (char*)(region->vaddr().get() + (SMBIOS_SEARCH_AREA_SIZE)); entry_str += 16) {
  156. #ifdef SMBIOS_DEBUG
  157. dbgprintf("DMI Decoder: Looking for 64 bit Entry point @ P 0x%x\n", entry_str, tested_physical_ptr);
  158. #endif
  159. if (!strncmp("_SM3_", entry_str, strlen("_SM3_")))
  160. return (SMBIOS::EntryPoint64bit*)tested_physical_ptr;
  161. tested_physical_ptr += 16;
  162. }
  163. return nullptr;
  164. }
  165. SMBIOS::EntryPoint32bit* DMIDecoder::find_entry32bit_point()
  166. {
  167. auto region = MM.allocate_kernel_region(PAGE_ROUND_UP(SMBIOS_SEARCH_AREA_SIZE), "DMI Decoder Entry Point Finding", Region::Access::Read);
  168. PhysicalAddress paddr = PhysicalAddress(SMBIOS_BASE_SEARCH_ADDR);
  169. mmap_region(*region, paddr);
  170. char* tested_physical_ptr = (char*)paddr.get();
  171. for (char* entry_str = (char*)(region->vaddr().get()); entry_str < (char*)(region->vaddr().get() + (SMBIOS_SEARCH_AREA_SIZE)); entry_str += 16) {
  172. #ifdef SMBIOS_DEBUG
  173. dbgprintf("DMI Decoder: Looking for 32 bit Entry point @ P 0x%x\n", tested_physical_ptr);
  174. #endif
  175. if (!strncmp("_SM_", entry_str, strlen("_SM_")))
  176. return (SMBIOS::EntryPoint32bit*)tested_physical_ptr;
  177. tested_physical_ptr += 16;
  178. }
  179. return nullptr;
  180. }
  181. void DMIDecoder::mmap(VirtualAddress vaddr, PhysicalAddress paddr, u32 length)
  182. {
  183. unsigned i = 0;
  184. while (length >= PAGE_SIZE) {
  185. MM.map_for_kernel(VirtualAddress(vaddr.offset(i * PAGE_SIZE).get()), PhysicalAddress(paddr.offset(i * PAGE_SIZE).get()));
  186. #ifdef SMBIOS_DEBUG
  187. dbgprintf("DMI Decoder: map - V 0x%x -> P 0x%x\n", vaddr.offset(i * PAGE_SIZE).get(), paddr.offset(i * PAGE_SIZE).get());
  188. #endif
  189. length -= PAGE_SIZE;
  190. i++;
  191. }
  192. if (length > 0) {
  193. MM.map_for_kernel(vaddr.offset(i * PAGE_SIZE), paddr.offset(i * PAGE_SIZE), true);
  194. }
  195. #ifdef SMBIOS_DEBUG
  196. dbgprintf("DMI Decoder: Finished mapping\n");
  197. #endif
  198. }
  199. void DMIDecoder::mmap_region(Region& region, PhysicalAddress paddr)
  200. {
  201. #ifdef SMBIOS_DEBUG
  202. dbgprintf("DMI Decoder: Mapping region, size - %u\n", region.size());
  203. #endif
  204. mmap(region.vaddr(), paddr, region.size());
  205. }
  206. Vector<SMBIOS::PhysicalMemoryArray*>& DMIDecoder::get_physical_memory_areas()
  207. {
  208. // FIXME: Implement it...
  209. kprintf("DMIDecoder::get_physical_memory_areas() is not implemented.\n");
  210. ASSERT_NOT_REACHED();
  211. }
  212. bool DMIDecoder::is_reliable()
  213. {
  214. return !m_untrusted;
  215. }
  216. u64 DMIDecoder::get_bios_characteristics()
  217. {
  218. // FIXME: Make sure we have some mapping here so we don't rely on existing identity mapping...
  219. ASSERT(m_operable == true);
  220. SMBIOS::BIOSInfo* bios_info = (SMBIOS::BIOSInfo*)get_smbios_physical_table_by_type(0);
  221. ASSERT(bios_info != nullptr);
  222. kprintf("DMIDecoder: BIOS info @ P 0x%x\n", bios_info);
  223. return bios_info->bios_characteristics;
  224. }
  225. char* DMIDecoder::get_smbios_string(SMBIOS::TableHeader&, u8)
  226. {
  227. // FIXME: Implement it...
  228. // FIXME: Make sure we have some mapping here so we don't rely on existing identity mapping...
  229. kprintf("DMIDecoder::get_smbios_string() is not implemented.\n");
  230. ASSERT_NOT_REACHED();
  231. return nullptr;
  232. }