blob: 5fd6ece66a68e3f1877a088cc4ed26712f277661 [file] [log] [blame]
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +05301#include "bios_table.hpp"
2
3#include <fstream>
4
John Wangf719f3b2020-01-17 08:46:22 +08005#include "bios_table.h"
6
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +05307namespace pldm
8{
9
10namespace responder
11{
12
13namespace bios
14{
15
16BIOSTable::BIOSTable(const char* filePath) : filePath(filePath)
17{
18}
19
20bool 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
34void 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
40void 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
John Wange297b9f2020-02-03 10:18:13 +080049BIOSStringTable::BIOSStringTable(const Table& stringTable) :
50 stringTable(stringTable)
John Wangf719f3b2020-01-17 08:46:22 +080051{
John Wange297b9f2020-02-03 10:18:13 +080052}
53
54BIOSStringTable::BIOSStringTable(const BIOSTable& biosTable)
55{
56 biosTable.load(stringTable);
John Wangf719f3b2020-01-17 08:46:22 +080057}
58
59std::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 }
John Wange2efdcc2020-02-12 17:02:06 +080067 return decodeString(stringEntry);
John Wangf719f3b2020-01-17 08:46:22 +080068}
69
John Wange297b9f2020-02-03 10:18:13 +080070uint16_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
John Wange2efdcc2020-02-12 17:02:06 +080079 return decodeHandle(stringEntry);
80}
81
82uint16_t
83 BIOSStringTable::decodeHandle(const pldm_bios_string_table_entry* entry)
84{
85 return pldm_bios_table_string_entry_decode_handle(entry);
86}
87
88std::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);
John Wange297b9f2020-02-03 10:18:13 +080096}
97
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +053098} // namespace bios
99} // namespace responder
100} // namespace pldm