Serenity Operating System
at hosted 281 lines 11 kB view raw
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 27#include <AK/StringView.h> 28#include <Kernel/ACPI/DMIDecoder.h> 29#include <Kernel/VM/MemoryManager.h> 30#include <LibBareMetal/StdLib.h> 31 32namespace Kernel { 33 34static DMIDecoder* s_dmi_decoder; 35 36//#define SMBIOS_DEBUG 37 38#define SMBIOS_BASE_SEARCH_ADDR 0xf0000 39#define SMBIOS_END_SEARCH_ADDR 0xfffff 40#define SMBIOS_SEARCH_AREA_SIZE (SMBIOS_END_SEARCH_ADDR - SMBIOS_BASE_SEARCH_ADDR) 41 42DMIDecoder& DMIDecoder::the() 43{ 44 if (s_dmi_decoder == nullptr) { 45 s_dmi_decoder = new DMIDecoder(true); 46 } 47 return *s_dmi_decoder; 48} 49 50void DMIDecoder::initialize() 51{ 52 if (s_dmi_decoder == nullptr) { 53 s_dmi_decoder = new DMIDecoder(true); 54 } 55} 56 57void DMIDecoder::initialize_untrusted() 58{ 59 if (s_dmi_decoder == nullptr) { 60 s_dmi_decoder = new DMIDecoder(false); 61 } 62} 63 64void DMIDecoder::set_64_bit_entry_initialization_values(PhysicalAddress entry) 65{ 66 klog() << "DMIDecoder: SMBIOS 64bit Entry point @ " << m_entry64bit_point; 67 m_use_64bit_entry = true; 68 69 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); 70 auto& entry_ptr = *(SMBIOS::EntryPoint64bit*)region->vaddr().offset(entry.offset_in_page()).as_ptr(); 71 m_structure_table = PhysicalAddress(entry_ptr.table_ptr); 72 m_structures_count = entry_ptr.table_maximum_size; 73 m_table_length = entry_ptr.table_maximum_size; 74} 75 76void DMIDecoder::set_32_bit_entry_initialization_values(PhysicalAddress entry) 77{ 78 klog() << "DMIDecoder: SMBIOS 32bit Entry point @ " << m_entry32bit_point; 79 m_use_64bit_entry = false; 80 81 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); 82 auto& entry_ptr = *(SMBIOS::EntryPoint32bit*)region->vaddr().offset(entry.offset_in_page()).as_ptr(); 83 84 m_structure_table = PhysicalAddress(entry_ptr.legacy_structure.smbios_table_ptr); 85 m_structures_count = entry_ptr.legacy_structure.smbios_tables_count; 86 m_table_length = entry_ptr.legacy_structure.smboios_table_length; 87} 88 89void DMIDecoder::initialize_parser() 90{ 91 92 if (m_entry32bit_point.is_null() && m_entry64bit_point.is_null()) { 93 m_operable = false; 94 klog() << "DMI Decoder is disabled. Cannot find SMBIOS tables."; 95 return; 96 } 97 98 m_operable = true; 99 klog() << "DMI Decoder is enabled"; 100 if (!m_entry64bit_point.is_null()) { 101 set_64_bit_entry_initialization_values(m_entry64bit_point); 102 } else if (!m_entry32bit_point.is_null()) { 103 set_32_bit_entry_initialization_values(m_entry32bit_point); 104 } 105 klog() << "DMIDecoder: Data table @ " << m_structure_table; 106 enumerate_smbios_tables(); 107} 108 109void DMIDecoder::enumerate_smbios_tables() 110{ 111 112 u32 table_length = m_table_length; 113 auto p_table = m_structure_table; 114 115 auto region = MM.allocate_kernel_region(p_table.page_base(), PAGE_ROUND_UP(table_length), "DMI Decoder Enumerating SMBIOS", Region::Access::Read, false, false); 116 volatile SMBIOS::TableHeader* v_table_ptr = (SMBIOS::TableHeader*)region->vaddr().offset(p_table.offset_in_page()).as_ptr(); 117 118#ifdef SMBIOS_DEBUG 119 dbg() << "DMIDecoder: Total Table length " << m_table_length; 120#endif 121 122 u32 structures_count = 0; 123 while (table_length > 0) { 124#ifdef SMBIOS_DEBUG 125 dbg() << "DMIDecoder: Examining table @ P " << (void*)p_table.as_ptr() << " V " << const_cast<SMBIOS::TableHeader*>(v_table_ptr); 126#endif 127 structures_count++; 128 if (v_table_ptr->type == (u8)SMBIOS::TableType::EndOfTable) { 129 klog() << "DMIDecoder: Detected table with type 127, End of SMBIOS data."; 130 break; 131 } 132 klog() << "DMIDecoder: Detected table with type " << v_table_ptr->type; 133 m_smbios_tables.append(p_table); 134 table_length -= v_table_ptr->length; 135 136 size_t table_size = get_table_size(p_table); 137 p_table = p_table.offset(table_size); 138 v_table_ptr = (SMBIOS::TableHeader*)((FlatPtr)v_table_ptr + table_size); 139#ifdef SMBIOS_DEBUG 140 dbg() << "DMIDecoder: Next table @ P 0x" << p_table.get(); 141#endif 142 } 143 m_structures_count = structures_count; 144} 145 146size_t DMIDecoder::get_table_size(PhysicalAddress table) 147{ 148 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); 149 auto& table_v_ptr = (SMBIOS::TableHeader&)*region->vaddr().offset(table.offset_in_page()).as_ptr(); 150#ifdef SMBIOS_DEBUG 151 dbg() << "DMIDecoder: table legnth - " << table_v_ptr.length; 152#endif 153 const char* strtab = (char*)&table_v_ptr + table_v_ptr.length; 154 size_t index = 1; 155 while (strtab[index - 1] != '\0' || strtab[index] != '\0') { 156 if (index > m_table_length) { 157 ASSERT_NOT_REACHED(); // FIXME: Instead of halting, find a better solution (Hint: use m_operable to disallow further use of DMIDecoder) 158 } 159 index++; 160 } 161#ifdef SMBIOS_DEBUG 162 dbg() << "DMIDecoder: table size - " << (table_v_ptr.length + index + 1); 163#endif 164 return table_v_ptr.length + index + 1; 165} 166 167PhysicalAddress DMIDecoder::get_next_physical_table(PhysicalAddress p_table) 168{ 169 return p_table.offset(get_table_size(p_table)); 170} 171 172PhysicalAddress DMIDecoder::get_smbios_physical_table_by_handle(u16 handle) 173{ 174 175 for (auto table : m_smbios_tables) { 176 if (table.is_null()) 177 continue; 178 auto region = MM.allocate_kernel_region(table.page_base(), PAGE_SIZE * 2, "DMI Decoder Finding Table", Region::Access::Read, false, false); 179 SMBIOS::TableHeader* table_v_ptr = (SMBIOS::TableHeader*)region->vaddr().offset(table.offset_in_page()).as_ptr(); 180 181 if (table_v_ptr->handle == handle) { 182 return table; 183 } 184 } 185 return {}; 186} 187PhysicalAddress DMIDecoder::get_smbios_physical_table_by_type(u8 table_type) 188{ 189 190 for (auto table : m_smbios_tables) { 191 if (table.is_null()) 192 continue; 193 auto region = MM.allocate_kernel_region(table.page_base(), PAGE_ROUND_UP(PAGE_SIZE * 2), "DMI Decoder Finding Table", Region::Access::Read, false, false); 194 SMBIOS::TableHeader* table_v_ptr = (SMBIOS::TableHeader*)region->vaddr().offset(table.offset_in_page()).as_ptr(); 195 if (table_v_ptr->type == table_type) { 196 return table; 197 } 198 } 199 return {}; 200} 201 202DMIDecoder::DMIDecoder(bool trusted) 203 : m_entry32bit_point(find_entry32bit_point()) 204 , m_entry64bit_point(find_entry64bit_point()) 205 , m_structure_table(PhysicalAddress()) 206 , m_untrusted(!trusted) 207{ 208 if (!trusted) { 209 klog() << "DMI Decoder initialized as untrusted due to user request."; 210 } 211 initialize_parser(); 212} 213 214PhysicalAddress DMIDecoder::find_entry64bit_point() 215{ 216 PhysicalAddress paddr = PhysicalAddress(SMBIOS_BASE_SEARCH_ADDR); 217 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); 218 219 char* tested_physical_ptr = (char*)paddr.get(); 220 for (char* entry_str = (char*)(region->vaddr().get()); entry_str < (char*)(region->vaddr().get() + (SMBIOS_SEARCH_AREA_SIZE)); entry_str += 16) { 221#ifdef SMBIOS_DEBUG 222 dbg() << "DMI Decoder: Looking for 64 bit Entry point @ V " << (void*)entry_str << " P " << (void*)tested_physical_ptr; 223#endif 224 if (!strncmp("_SM3_", entry_str, strlen("_SM3_"))) 225 return PhysicalAddress((FlatPtr)tested_physical_ptr); 226 227 tested_physical_ptr += 16; 228 } 229 return {}; 230} 231 232PhysicalAddress DMIDecoder::find_entry32bit_point() 233{ 234 PhysicalAddress paddr = PhysicalAddress(SMBIOS_BASE_SEARCH_ADDR); 235 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); 236 237 char* tested_physical_ptr = (char*)paddr.get(); 238 for (char* entry_str = (char*)(region->vaddr().get()); entry_str < (char*)(region->vaddr().get() + (SMBIOS_SEARCH_AREA_SIZE)); entry_str += 16) { 239#ifdef SMBIOS_DEBUG 240 dbg() << "DMI Decoder: Looking for 32 bit Entry point @ V " << (void*)entry_str << " P " << (void*)tested_physical_ptr; 241#endif 242 if (!strncmp("_SM_", entry_str, strlen("_SM_"))) 243 return PhysicalAddress((FlatPtr)tested_physical_ptr); 244 245 tested_physical_ptr += 16; 246 } 247 return {}; 248} 249 250Vector<SMBIOS::PhysicalMemoryArray*>& DMIDecoder::get_physical_memory_areas() 251{ 252 // FIXME: Implement it... 253 klog() << "DMIDecoder::get_physical_memory_areas() is not implemented."; 254 ASSERT_NOT_REACHED(); 255} 256bool DMIDecoder::is_reliable() 257{ 258 return !m_untrusted; 259} 260u64 DMIDecoder::get_bios_characteristics() 261{ 262 // FIXME: Make sure we have some mapping here so we don't rely on existing identity mapping... 263 ASSERT_NOT_REACHED(); 264 ASSERT(m_operable == true); 265 auto* bios_info = (SMBIOS::BIOSInfo*)get_smbios_physical_table_by_type(0).as_ptr(); 266 ASSERT(bios_info != nullptr); 267 268 klog() << "DMIDecoder: BIOS info @ " << PhysicalAddress((FlatPtr)bios_info); 269 return bios_info->bios_characteristics; 270} 271 272char* DMIDecoder::get_smbios_string(PhysicalAddress, u8) 273{ 274 // FIXME: Implement it... 275 // FIXME: Make sure we have some mapping here so we don't rely on existing identity mapping... 276 klog() << "DMIDecoder::get_smbios_string() is not implemented."; 277 ASSERT_NOT_REACHED(); 278 return nullptr; 279} 280 281}