1 #include "bios_table.hpp" 2 3 #include <fstream> 4 5 #include "bios_table.h" 6 7 namespace pldm 8 { 9 10 namespace responder 11 { 12 13 namespace bios 14 { 15 16 BIOSTable::BIOSTable(const char* filePath) : filePath(filePath) 17 { 18 } 19 20 bool BIOSTable::isEmpty() const noexcept 21 { 22 bool empty = false; 23 try 24 { 25 empty = fs::is_empty(filePath); 26 } 27 catch (fs::filesystem_error& e) 28 { 29 return true; 30 } 31 return empty; 32 } 33 34 void BIOSTable::store(const Table& table) 35 { 36 std::ofstream stream(filePath.string(), std::ios::out | std::ios::binary); 37 stream.write(reinterpret_cast<const char*>(table.data()), table.size()); 38 } 39 40 void BIOSTable::load(Response& response) const 41 { 42 auto currSize = response.size(); 43 auto fileSize = fs::file_size(filePath); 44 response.resize(currSize + fileSize); 45 std::ifstream stream(filePath.string(), std::ios::in | std::ios::binary); 46 stream.read(reinterpret_cast<char*>(response.data() + currSize), fileSize); 47 } 48 49 BIOSStringTable::BIOSStringTable(const Table& stringTable) : 50 stringTable(stringTable) 51 { 52 } 53 54 BIOSStringTable::BIOSStringTable(const BIOSTable& biosTable) 55 { 56 biosTable.load(stringTable); 57 } 58 59 std::string BIOSStringTable::findString(uint16_t handle) const 60 { 61 auto stringEntry = pldm_bios_table_string_find_by_handle( 62 stringTable.data(), stringTable.size(), handle); 63 if (stringEntry == nullptr) 64 { 65 throw std::invalid_argument("Invalid String Handle"); 66 } 67 return decodeString(stringEntry); 68 } 69 70 uint16_t BIOSStringTable::findHandle(const std::string& name) const 71 { 72 auto stringEntry = pldm_bios_table_string_find_by_string( 73 stringTable.data(), stringTable.size(), name.c_str()); 74 if (stringEntry == nullptr) 75 { 76 throw std::invalid_argument("Invalid String Name"); 77 } 78 79 return decodeHandle(stringEntry); 80 } 81 82 uint16_t 83 BIOSStringTable::decodeHandle(const pldm_bios_string_table_entry* entry) 84 { 85 return pldm_bios_table_string_entry_decode_handle(entry); 86 } 87 88 std::string 89 BIOSStringTable::decodeString(const pldm_bios_string_table_entry* entry) 90 { 91 auto strLength = pldm_bios_table_string_entry_decode_string_length(entry); 92 std::vector<char> buffer(strLength + 1 /* sizeof '\0' */); 93 pldm_bios_table_string_entry_decode_string(entry, buffer.data(), 94 buffer.size()); 95 return std::string(buffer.data(), buffer.data() + strLength); 96 } 97 98 } // namespace bios 99 } // namespace responder 100 } // namespace pldm 101