DMIDecoder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/ACPI/DMIDecoder.h>
  27. #include <Kernel/VM/MemoryManager.h>
  28. #include <LibBareMetal/StdLib.h>
  29. namespace Kernel {
  30. static DMIDecoder* s_dmi_decoder;
  31. //#define SMBIOS_DEBUG
  32. #define SMBIOS_BASE_SEARCH_ADDR 0xf0000
  33. #define SMBIOS_END_SEARCH_ADDR 0xfffff
  34. #define SMBIOS_SEARCH_AREA_SIZE (SMBIOS_END_SEARCH_ADDR - SMBIOS_BASE_SEARCH_ADDR)
  35. DMIDecoder& DMIDecoder::the()
  36. {
  37. if (s_dmi_decoder == nullptr) {
  38. s_dmi_decoder = new DMIDecoder(true);
  39. }
  40. return *s_dmi_decoder;
  41. }
  42. void DMIDecoder::initialize()
  43. {
  44. if (s_dmi_decoder == nullptr) {
  45. s_dmi_decoder = new DMIDecoder(true);
  46. }
  47. }
  48. void DMIDecoder::initialize_untrusted()
  49. {
  50. if (s_dmi_decoder == nullptr) {
  51. s_dmi_decoder = new DMIDecoder(false);
  52. }
  53. }
  54. void DMIDecoder::set_64_bit_entry_initialization_values(PhysicalAddress entry)
  55. {
  56. klog() << "DMIDecoder: SMBIOS 64bit Entry point @ " << m_entry64bit_point;
  57. m_use_64bit_entry = true;
  58. auto region = MM.allocate_kernel_region(entry.page_base(), PAGE_ROUND_UP(SMBIOS_SEARCH_AREA_SIZE), "DMI Decoder 64 bit Initialization", Region::Access::Read, false, false);
  59. auto& entry_ptr = *(SMBIOS::EntryPoint64bit*)region->vaddr().offset(entry.offset_in_page()).as_ptr();
  60. m_structure_table = PhysicalAddress(entry_ptr.table_ptr);
  61. m_structures_count = entry_ptr.table_maximum_size;
  62. m_table_length = entry_ptr.table_maximum_size;
  63. }
  64. void DMIDecoder::set_32_bit_entry_initialization_values(PhysicalAddress entry)
  65. {
  66. klog() << "DMIDecoder: SMBIOS 32bit Entry point @ " << m_entry32bit_point;
  67. m_use_64bit_entry = false;
  68. auto region = MM.allocate_kernel_region(entry.page_base(), PAGE_ROUND_UP(SMBIOS_SEARCH_AREA_SIZE), "DMI Decoder 32 bit Initialization", Region::Access::Read, false, false);
  69. auto& entry_ptr = *(SMBIOS::EntryPoint32bit*)region->vaddr().offset(entry.offset_in_page()).as_ptr();
  70. m_structure_table = PhysicalAddress(entry_ptr.legacy_structure.smbios_table_ptr);
  71. m_structures_count = entry_ptr.legacy_structure.smbios_tables_count;
  72. m_table_length = entry_ptr.legacy_structure.smboios_table_length;
  73. }
  74. void DMIDecoder::initialize_parser()
  75. {
  76. if (m_entry32bit_point.is_null() && m_entry64bit_point.is_null()) {
  77. m_operable = false;
  78. klog() << "DMI Decoder is disabled. Cannot find SMBIOS tables.";
  79. return;
  80. }
  81. m_operable = true;
  82. klog() << "DMI Decoder is enabled";
  83. if (!m_entry64bit_point.is_null()) {
  84. set_64_bit_entry_initialization_values(m_entry64bit_point);
  85. } else if (!m_entry32bit_point.is_null()) {
  86. set_32_bit_entry_initialization_values(m_entry32bit_point);
  87. }
  88. klog() << "DMIDecoder: Data table @ " << m_structure_table;
  89. enumerate_smbios_tables();
  90. }
  91. void DMIDecoder::enumerate_smbios_tables()
  92. {
  93. u32 table_length = m_table_length;
  94. auto p_table = m_structure_table;
  95. auto region = MM.allocate_kernel_region(p_table.page_base(), PAGE_ROUND_UP(table_length), "DMI Decoder Enumerating SMBIOS", Region::Access::Read, false, false);
  96. volatile SMBIOS::TableHeader* v_table_ptr = (SMBIOS::TableHeader*)region->vaddr().offset(p_table.offset_in_page()).as_ptr();
  97. #ifdef SMBIOS_DEBUG
  98. dbg() << "DMIDecoder: Total Table length " << m_table_length;
  99. #endif
  100. u32 structures_count = 0;
  101. while (table_length > 0) {
  102. #ifdef SMBIOS_DEBUG
  103. dbg() << "DMIDecoder: Examining table @ P " << (void*)p_table.as_ptr() << " V " << const_cast<SMBIOS::TableHeader*>(v_table_ptr);
  104. #endif
  105. structures_count++;
  106. if (v_table_ptr->type == (u8)SMBIOS::TableType::EndOfTable) {
  107. klog() << "DMIDecoder: Detected table with type 127, End of SMBIOS data.";
  108. break;
  109. }
  110. klog() << "DMIDecoder: Detected table with type " << v_table_ptr->type;
  111. m_smbios_tables.append(p_table);
  112. table_length -= v_table_ptr->length;
  113. size_t table_size = get_table_size(p_table);
  114. p_table = p_table.offset(table_size);
  115. v_table_ptr = (SMBIOS::TableHeader*)((FlatPtr)v_table_ptr + table_size);
  116. #ifdef SMBIOS_DEBUG
  117. dbg() << "DMIDecoder: Next table @ P 0x" << p_table.get();
  118. #endif
  119. }
  120. m_structures_count = structures_count;
  121. }
  122. size_t DMIDecoder::get_table_size(PhysicalAddress table)
  123. {
  124. auto region = MM.allocate_kernel_region(table.page_base(), PAGE_ROUND_UP(m_table_length), "DMI Decoder Determining table size", Region::Access::Read, false, false);
  125. auto& table_v_ptr = (SMBIOS::TableHeader&)*region->vaddr().offset(table.offset_in_page()).as_ptr();
  126. #ifdef SMBIOS_DEBUG
  127. dbg() << "DMIDecoder: table legnth - " << table_v_ptr.length;
  128. #endif
  129. const char* strtab = (char*)&table_v_ptr + table_v_ptr.length;
  130. size_t index = 1;
  131. while (strtab[index - 1] != '\0' || strtab[index] != '\0') {
  132. if (index > m_table_length) {
  133. ASSERT_NOT_REACHED(); // FIXME: Instead of halting, find a better solution (Hint: use m_operable to disallow further use of DMIDecoder)
  134. }
  135. index++;
  136. }
  137. #ifdef SMBIOS_DEBUG
  138. dbg() << "DMIDecoder: table size - " << (table_v_ptr.length + index + 1);
  139. #endif
  140. return table_v_ptr.length + index + 1;
  141. }
  142. PhysicalAddress DMIDecoder::get_next_physical_table(PhysicalAddress p_table)
  143. {
  144. return p_table.offset(get_table_size(p_table));
  145. }
  146. PhysicalAddress DMIDecoder::get_smbios_physical_table_by_handle(u16 handle)
  147. {
  148. for (auto table : m_smbios_tables) {
  149. if (table.is_null())
  150. continue;
  151. auto region = MM.allocate_kernel_region(table.page_base(), PAGE_SIZE * 2, "DMI Decoder Finding Table", Region::Access::Read, false, false);
  152. SMBIOS::TableHeader* table_v_ptr = (SMBIOS::TableHeader*)region->vaddr().offset(table.offset_in_page()).as_ptr();
  153. if (table_v_ptr->handle == handle) {
  154. return table;
  155. }
  156. }
  157. return {};
  158. }
  159. PhysicalAddress DMIDecoder::get_smbios_physical_table_by_type(u8 table_type)
  160. {
  161. for (auto table : m_smbios_tables) {
  162. if (table.is_null())
  163. continue;
  164. auto region = MM.allocate_kernel_region(table.page_base(), PAGE_ROUND_UP(PAGE_SIZE * 2), "DMI Decoder Finding Table", Region::Access::Read, false, false);
  165. SMBIOS::TableHeader* table_v_ptr = (SMBIOS::TableHeader*)region->vaddr().offset(table.offset_in_page()).as_ptr();
  166. if (table_v_ptr->type == table_type) {
  167. return table;
  168. }
  169. }
  170. return {};
  171. }
  172. DMIDecoder::DMIDecoder(bool trusted)
  173. : m_entry32bit_point(find_entry32bit_point())
  174. , m_entry64bit_point(find_entry64bit_point())
  175. , m_structure_table(PhysicalAddress())
  176. , m_untrusted(!trusted)
  177. {
  178. if (!trusted) {
  179. klog() << "DMI Decoder initialized as untrusted due to user request.";
  180. }
  181. initialize_parser();
  182. }
  183. PhysicalAddress DMIDecoder::find_entry64bit_point()
  184. {
  185. PhysicalAddress paddr = PhysicalAddress(SMBIOS_BASE_SEARCH_ADDR);
  186. auto region = MM.allocate_kernel_region(paddr, PAGE_ROUND_UP(SMBIOS_SEARCH_AREA_SIZE), "DMI Decoder Entry Point 64 bit Finding", Region::Access::Read, false, false);
  187. char* tested_physical_ptr = (char*)paddr.get();
  188. for (char* entry_str = (char*)(region->vaddr().get()); entry_str < (char*)(region->vaddr().get() + (SMBIOS_SEARCH_AREA_SIZE)); entry_str += 16) {
  189. #ifdef SMBIOS_DEBUG
  190. dbg() << "DMI Decoder: Looking for 64 bit Entry point @ V " << (void*)entry_str << " P " << (void*)tested_physical_ptr;
  191. #endif
  192. if (!strncmp("_SM3_", entry_str, strlen("_SM3_")))
  193. return PhysicalAddress((FlatPtr)tested_physical_ptr);
  194. tested_physical_ptr += 16;
  195. }
  196. return {};
  197. }
  198. PhysicalAddress DMIDecoder::find_entry32bit_point()
  199. {
  200. PhysicalAddress paddr = PhysicalAddress(SMBIOS_BASE_SEARCH_ADDR);
  201. auto region = MM.allocate_kernel_region(paddr, PAGE_ROUND_UP(SMBIOS_SEARCH_AREA_SIZE), "DMI Decoder Entry Point 32 bit Finding", Region::Access::Read, false, false);
  202. char* tested_physical_ptr = (char*)paddr.get();
  203. for (char* entry_str = (char*)(region->vaddr().get()); entry_str < (char*)(region->vaddr().get() + (SMBIOS_SEARCH_AREA_SIZE)); entry_str += 16) {
  204. #ifdef SMBIOS_DEBUG
  205. dbg() << "DMI Decoder: Looking for 32 bit Entry point @ V " << (void*)entry_str << " P " << (void*)tested_physical_ptr;
  206. #endif
  207. if (!strncmp("_SM_", entry_str, strlen("_SM_")))
  208. return PhysicalAddress((FlatPtr)tested_physical_ptr);
  209. tested_physical_ptr += 16;
  210. }
  211. return {};
  212. }
  213. Vector<SMBIOS::PhysicalMemoryArray*>& DMIDecoder::get_physical_memory_areas()
  214. {
  215. // FIXME: Implement it...
  216. klog() << "DMIDecoder::get_physical_memory_areas() is not implemented.";
  217. ASSERT_NOT_REACHED();
  218. }
  219. bool DMIDecoder::is_reliable()
  220. {
  221. return !m_untrusted;
  222. }
  223. u64 DMIDecoder::get_bios_characteristics()
  224. {
  225. // FIXME: Make sure we have some mapping here so we don't rely on existing identity mapping...
  226. ASSERT_NOT_REACHED();
  227. ASSERT(m_operable == true);
  228. auto* bios_info = (SMBIOS::BIOSInfo*)get_smbios_physical_table_by_type(0).as_ptr();
  229. ASSERT(bios_info != nullptr);
  230. klog() << "DMIDecoder: BIOS info @ " << PhysicalAddress((FlatPtr)bios_info);
  231. return bios_info->bios_characteristics;
  232. }
  233. char* DMIDecoder::get_smbios_string(PhysicalAddress, u8)
  234. {
  235. // FIXME: Implement it...
  236. // FIXME: Make sure we have some mapping here so we don't rely on existing identity mapping...
  237. klog() << "DMIDecoder::get_smbios_string() is not implemented.";
  238. ASSERT_NOT_REACHED();
  239. return nullptr;
  240. }
  241. }