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 auto strLength = 68 pldm_bios_table_string_entry_decode_string_length(stringEntry); 69 std::vector<char> buffer(strLength + 1 /* sizeof '\0' */); 70 pldm_bios_table_string_entry_decode_string(stringEntry, buffer.data(), 71 buffer.size()); 72 73 return std::string(buffer.data(), buffer.data() + strLength); 74 } 75 76 uint16_t BIOSStringTable::findHandle(const std::string& name) const 77 { 78 auto stringEntry = pldm_bios_table_string_find_by_string( 79 stringTable.data(), stringTable.size(), name.c_str()); 80 if (stringEntry == nullptr) 81 { 82 throw std::invalid_argument("Invalid String Name"); 83 } 84 85 return pldm_bios_table_string_entry_decode_handle(stringEntry); 86 } 87 88 } // namespace bios 89 } // namespace responder 90 } // namespace pldm 91