blob: d1dc7c9df1cf544d3acb6808cfd46a12ef6f2fe1 [file] [log] [blame]
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +05301#pragma once
2
3#include <stdint.h>
4
5#include <filesystem>
John Wange2efdcc2020-02-12 17:02:06 +08006#include <string>
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +05307#include <vector>
8
9#include "libpldm/bios.h"
John Wang29683b52020-02-27 16:41:44 +080010#include "libpldm/bios_table.h"
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +053011
12namespace pldm
13{
14
15namespace responder
16{
17
18namespace bios
19{
20
21using Table = std::vector<uint8_t>;
22using Response = std::vector<uint8_t>;
23namespace fs = std::filesystem;
24
25/** @class BIOSTable
26 *
27 * @brief Provides APIs for storing and loading BIOS tables
28 *
29 * Typical usage is as follows:
30 * BIOSTable table(BIOS_STRING_TABLE_FILE_PATH);
31 * if (table.isEmpty()) { // no persisted table
32 * // construct BIOSTable 't'
33 * // prepare Response 'r'
34 * // send response to GetBIOSTable
35 * table.store(t); // persisted
36 * }
37 * else { // persisted table exists
38 * // create Response 'r' which has response fields (except
39 * // the table itself) to a GetBIOSTable command
40 * table.load(r); // actual table will be pushed back to the vector
41 * }
42 */
43class BIOSTable
44{
45 public:
46 /** @brief Ctor - set file path to persist BIOS table
47 *
48 * @param[in] filePath - file where BIOS table should be persisted
49 */
50 BIOSTable(const char* filePath);
51
52 /** @brief Checks if there's a persisted BIOS table
53 *
54 * @return bool - true if table exists, false otherwise
55 */
56 bool isEmpty() const noexcept;
57
58 /** @brief Persist a BIOS table(string/attribute/attribute value)
59 *
60 * @param[in] table - BIOS table
61 */
62 void store(const Table& table);
63
64 /** @brief Load BIOS table from persistent store to memory
65 *
66 * @param[in,out] response - PLDM response message to GetBIOSTable
67 * (excluding table), table will be pushed back to this.
68 */
69 void load(Response& response) const;
70
71 private:
72 // file storing PLDM BIOS table
73 fs::path filePath;
74};
75
John Wange2efdcc2020-02-12 17:02:06 +080076/** @class BIOSStringTableInterface
77 * @brief Provide interfaces to the BIOS string table operations
78 */
79class BIOSStringTableInterface
80{
81 public:
82 virtual ~BIOSStringTableInterface() = default;
83
84 /** @brief Find the string name from the BIOS string table for a string
85 * handle
86 * @param[in] handle - string handle
87 * @return name of the corresponding BIOS string
88 */
89 virtual std::string findString(uint16_t handle) const = 0;
90
91 /** @brief Find the string handle from the BIOS string table by the given
92 * name
93 * @param[in] name - name of the BIOS string
94 * @return handle of the string
95 */
96 virtual uint16_t findHandle(const std::string& name) const = 0;
97};
98
99/** @class BIOSStringTable
100 * @brief Collection of BIOS string table operations.
101 */
102class BIOSStringTable : public BIOSStringTableInterface
John Wangf719f3b2020-01-17 08:46:22 +0800103{
104 public:
John Wange297b9f2020-02-03 10:18:13 +0800105 /** @brief Constructs BIOSStringTable
John Wangf719f3b2020-01-17 08:46:22 +0800106 *
John Wange297b9f2020-02-03 10:18:13 +0800107 * @param[in] stringTable - The stringTable in RAM
John Wangf719f3b2020-01-17 08:46:22 +0800108 */
John Wange297b9f2020-02-03 10:18:13 +0800109 BIOSStringTable(const Table& stringTable);
110
111 /** @brief Constructs BIOSStringTable
112 *
113 * @param[in] biosTable - The BIOSTable
114 */
115 BIOSStringTable(const BIOSTable& biosTable);
John Wangf719f3b2020-01-17 08:46:22 +0800116
117 /** @brief Find the string name from the BIOS string table for a string
118 * handle
119 * @param[in] handle - string handle
John Wange297b9f2020-02-03 10:18:13 +0800120 * @return name of the corresponding BIOS string
121 * @throw std::invalid_argument if the string can not be found.
John Wangf719f3b2020-01-17 08:46:22 +0800122 */
John Wange2efdcc2020-02-12 17:02:06 +0800123 std::string findString(uint16_t handle) const override;
John Wangf719f3b2020-01-17 08:46:22 +0800124
John Wange297b9f2020-02-03 10:18:13 +0800125 /** @brief Find the string handle from the BIOS string table by the given
126 * name
127 * @param[in] name - name of the BIOS string
128 * @return handle of the string
129 * @throw std::invalid_argument if the string can not be found
130 */
John Wange2efdcc2020-02-12 17:02:06 +0800131 uint16_t findHandle(const std::string& name) const override;
132
John Wangf719f3b2020-01-17 08:46:22 +0800133 private:
134 Table stringTable;
135};
136
John Wang29683b52020-02-27 16:41:44 +0800137namespace table
138{
139
140namespace string
141{
142
143/** @brief Get the string handle for the entry
144 * @param[in] entry - Pointer to a bios string table entry
145 * @return Handle to identify a string in the bios string table
146 */
147uint16_t decodeHandle(const pldm_bios_string_table_entry* entry);
148
149/** @brief Get the string from the entry
150 * @param[in] entry - Pointer to a bios string table entry
151 * @return The String
152 */
153std::string decodeString(const pldm_bios_string_table_entry* entry);
154
155} // namespace string
156
157namespace attribute
158{
159
160/** @struct TableHeader
161 * @brief Header of attribute table
162 */
163struct TableHeader
164{
165 uint16_t attrHandle;
166 uint8_t attrType;
167 uint16_t stringHandle;
168};
169
170/** @brief Decode header of attribute table entry
171 * @param[in] entry - Pointer to an attribute table entry
172 * @return Attribute table header
173 */
174TableHeader decodeHeader(const pldm_bios_attr_table_entry* entry);
175
176/** @struct StringField
177 * @brief String field of attribute table
178 */
179struct StringField
180{
181 uint8_t stringType;
182 uint16_t minLength;
183 uint16_t maxLength;
184 uint16_t defLength;
185 std::string defString;
186};
187
188/** @brief decode string entry of attribute table
189 * @param[in] entry - Pointer to an attribute table entry
190 * @return String field of the entry
191 */
192StringField decodeStringEntry(const pldm_bios_attr_table_entry* entry);
193
194/** @brief construct string entry of attribute table at the end of the given
195 * table
196 * @param[in,out] table - The given table
197 * @param[in] info - string info
198 * @return pointer to the constructed entry
199 */
200const pldm_bios_attr_table_entry*
201 constructStringEntry(Table& table,
202 pldm_bios_table_attr_entry_string_info* info);
203
204} // namespace attribute
205
206namespace attribute_value
207{
208
209/** @struct TableHeader
210 * @brief Header of attribute value table
211 */
212struct TableHeader
213{
214 uint16_t attrHandle;
215 uint8_t attrType;
216};
217
218/** @brief Decode header of attribute value table
219 * @param[in] entry - Pointer to an attribute value table entry
220 * @return Attribute value table header
221 */
222TableHeader decodeHeader(const pldm_bios_attr_val_table_entry* entry);
223
224/** @brief Decode string entry of attribute value table
225 * @param[in] entry - Pointer to an attribute value table entry
226 * @return The decoded string
227 */
228std::string decodeStringEntry(const pldm_bios_attr_val_table_entry* entry);
229
230/** @brief Construct string entry of attribute value table at the end of the
231 * given table
232 * @param[in] table - The given table
233 * @param[in] attrHandle - attribute handle
234 * @param[in] attrType - attribute type
235 * @param[in] str - The string
236 * @return Pointer to the constructed entry
237 */
238const pldm_bios_attr_val_table_entry*
239 constructStringEntry(Table& table, uint16_t attrHandle, uint8_t attrType,
240 const std::string& str);
241
242} // namespace attribute_value
243
244} // namespace table
245
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +0530246} // namespace bios
247} // namespace responder
248} // namespace pldm