blob: 9b7dd494ce040c8233193d776aba9a7b5d5dff57 [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 }
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
John Wange297b9f2020-02-03 10:18:13 +080076uint16_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
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +053088} // namespace bios
89} // namespace responder
90} // namespace pldm