blob: 8b5f8bcb1a80c7aaadf902f285141fe6e6104310 [file] [log] [blame]
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +05301#pragma once
2
George Liuc453e162022-12-21 17:16:23 +08003#include <libpldm/bios.h>
4#include <libpldm/bios_table.h>
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +05305#include <stdint.h>
6
7#include <filesystem>
John Wangd9659342020-02-27 16:46:05 +08008#include <optional>
John Wange2efdcc2020-02-12 17:02:06 +08009#include <string>
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +053010#include <vector>
11
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +053012namespace 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
John Wangd9659342020-02-27 16:46:05 +0800140/** @brief Append Pad and Checksum
141 *
142 * @param[in,out] table - table to be appended with pad and checksum
143 */
144void appendPadAndChecksum(Table& table);
145
John Wang29683b52020-02-27 16:41:44 +0800146namespace string
147{
148
149/** @brief Get the string handle for the entry
150 * @param[in] entry - Pointer to a bios string table entry
151 * @return Handle to identify a string in the bios string table
152 */
153uint16_t decodeHandle(const pldm_bios_string_table_entry* entry);
154
155/** @brief Get the string from the entry
156 * @param[in] entry - Pointer to a bios string table entry
157 * @return The String
158 */
159std::string decodeString(const pldm_bios_string_table_entry* entry);
160
John Wangd9659342020-02-27 16:46:05 +0800161/** @brief construct entry of string table at the end of the given
162 * table
163 * @param[in,out] table - The given table
164 * @param[in] str - string itself
165 * @return pointer to the constructed entry
166 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400167const pldm_bios_string_table_entry*
168 constructEntry(Table& table, const std::string& str);
John Wangd9659342020-02-27 16:46:05 +0800169
John Wang29683b52020-02-27 16:41:44 +0800170} // namespace string
171
172namespace attribute
173{
174
175/** @struct TableHeader
176 * @brief Header of attribute table
177 */
178struct TableHeader
179{
180 uint16_t attrHandle;
181 uint8_t attrType;
182 uint16_t stringHandle;
183};
184
185/** @brief Decode header of attribute table entry
186 * @param[in] entry - Pointer to an attribute table entry
187 * @return Attribute table header
188 */
189TableHeader decodeHeader(const pldm_bios_attr_table_entry* entry);
190
John Wangd9659342020-02-27 16:46:05 +0800191/** @brief Find attribute entry by handle
192 * @param[in] table - attribute table
193 * @param[in] handle - attribute handle
194 * @return Pointer to the attribute table entry
195 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400196const pldm_bios_attr_table_entry*
197 findByHandle(const Table& table, uint16_t handle);
John Wangd9659342020-02-27 16:46:05 +0800198
John Wang45fed202020-04-01 16:42:26 +0800199/** @brief Find attribute entry by string handle
200 * @param[in] table - attribute table
201 * @param[in] handle - string handle
202 * @return Pointer to the attribute table entry
203 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400204const pldm_bios_attr_table_entry*
205 findByStringHandle(const Table& table, uint16_t handle);
John Wang45fed202020-04-01 16:42:26 +0800206
John Wang29683b52020-02-27 16:41:44 +0800207/** @struct StringField
208 * @brief String field of attribute table
209 */
210struct StringField
211{
212 uint8_t stringType;
213 uint16_t minLength;
214 uint16_t maxLength;
215 uint16_t defLength;
216 std::string defString;
217};
218
219/** @brief decode string entry of attribute table
220 * @param[in] entry - Pointer to an attribute table entry
221 * @return String field of the entry
222 */
223StringField decodeStringEntry(const pldm_bios_attr_table_entry* entry);
224
225/** @brief construct string entry of attribute table at the end of the given
226 * table
227 * @param[in,out] table - The given table
228 * @param[in] info - string info
229 * @return pointer to the constructed entry
230 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400231const pldm_bios_attr_table_entry* constructStringEntry(
232 Table& table, pldm_bios_table_attr_entry_string_info* info);
John Wang29683b52020-02-27 16:41:44 +0800233
John Wang95e6b3c2020-02-13 09:43:24 +0800234/** @struct IntegerField
235 * @brief Integer field of attribute table
236 */
237struct IntegerField
238{
239 uint64_t lowerBound;
240 uint64_t upperBound;
241 uint32_t scalarIncrement;
242 uint64_t defaultValue;
243};
244
245/** @brief construct integer entry of attribute table at the end of the
246 * given table
247 * @param[in,out] table - The given table
248 * @param[in] info - integer info
249 * @return pointer to the constructed entry
250 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400251const pldm_bios_attr_table_entry* constructIntegerEntry(
252 Table& table, pldm_bios_table_attr_entry_integer_info* info);
John Wang95e6b3c2020-02-13 09:43:24 +0800253
254/** @brief decode integer entry of attribute table
255 * @param[in] entry - Pointer to an attribute table entry
256 * @return Integer field of the entry
257 */
258IntegerField decodeIntegerEntry(const pldm_bios_attr_table_entry* entry);
259
John Wang3be70852020-02-13 15:59:04 +0800260/** @struct EnumField
261 * @brief Enum field of attribute table
262 */
263struct EnumField
264{
265 std::vector<uint16_t> possibleValueStringHandle;
266 std::vector<uint8_t> defaultValueIndex;
267};
268
269/** @brief decode enum entry of attribute table
270 * @param[in] entry - Pointer to an attribute table entry
271 * @return Enum field of the entry
272 */
273EnumField decodeEnumEntry(const pldm_bios_attr_table_entry* entry);
274
275/** @brief construct enum entry of attribute table at the end of the
276 * given table
277 * @param[in,out] table - The given table
278 * @param[in] info - enum info
279 * @return pointer to the constructed entry
280 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400281const pldm_bios_attr_table_entry* constructEnumEntry(
282 Table& table, pldm_bios_table_attr_entry_enum_info* info);
John Wang3be70852020-02-13 15:59:04 +0800283
John Wang29683b52020-02-27 16:41:44 +0800284} // namespace attribute
285
286namespace attribute_value
287{
288
289/** @struct TableHeader
290 * @brief Header of attribute value table
291 */
292struct TableHeader
293{
294 uint16_t attrHandle;
295 uint8_t attrType;
296};
297
298/** @brief Decode header of attribute value table
299 * @param[in] entry - Pointer to an attribute value table entry
300 * @return Attribute value table header
301 */
302TableHeader decodeHeader(const pldm_bios_attr_val_table_entry* entry);
303
304/** @brief Decode string entry of attribute value table
305 * @param[in] entry - Pointer to an attribute value table entry
306 * @return The decoded string
307 */
308std::string decodeStringEntry(const pldm_bios_attr_val_table_entry* entry);
309
John Wang95e6b3c2020-02-13 09:43:24 +0800310/** @brief Decode integer entry of attribute value table
311 * @param[in] entry - Pointer to an attribute value table entry
312 * @return The decoded integer
313 */
314uint64_t decodeIntegerEntry(const pldm_bios_attr_val_table_entry* entry);
315
John Wang3be70852020-02-13 15:59:04 +0800316/** @brief Decode enum entry of attribute value table
317 * @param[in] entry - Pointer to an attribute value table entry
318 * @return Current value string handle indices
319 */
320std::vector<uint8_t>
321 decodeEnumEntry(const pldm_bios_attr_val_table_entry* entry);
322
John Wang29683b52020-02-27 16:41:44 +0800323/** @brief Construct string entry of attribute value table at the end of the
324 * given table
325 * @param[in] table - The given table
326 * @param[in] attrHandle - attribute handle
327 * @param[in] attrType - attribute type
328 * @param[in] str - The string
329 * @return Pointer to the constructed entry
330 */
331const pldm_bios_attr_val_table_entry*
332 constructStringEntry(Table& table, uint16_t attrHandle, uint8_t attrType,
333 const std::string& str);
334
John Wang95e6b3c2020-02-13 09:43:24 +0800335/** @brief Construct integer entry of attribute value table at the end of
336 * the given table
337 * @param[in] table - The given table
338 * @param[in] attrHandle - attribute handle
339 * @param[in] attrType - attribute type
340 * @param[in] value - The integer
341 * @return Pointer to the constructed entry
342 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400343const pldm_bios_attr_val_table_entry* constructIntegerEntry(
344 Table& table, uint16_t attrHandle, uint8_t attrType, uint64_t value);
John Wang95e6b3c2020-02-13 09:43:24 +0800345
John Wang3be70852020-02-13 15:59:04 +0800346/** @brief Construct enum entry of attribute value table at the end of
347 * the given table
348 * @param[in] table - The given table
349 * @param[in] attrHandle - attribute handle
350 * @param[in] attrType - attribute type
351 * @param[in] handleIndices - handle indices
352 * @return Pointer to the constructed entry
353 */
354const pldm_bios_attr_val_table_entry*
355 constructEnumEntry(Table& table, uint16_t attrHandle, uint8_t attrType,
356 const std::vector<uint8_t>& handleIndices);
357
John Wangd9659342020-02-27 16:46:05 +0800358/** @brief construct a table with an new entry
359 * @param[in] table - the table need to be updated
360 * @param[in] entry - the new attribute value entry
361 * @param[in] size - size of the new entry
362 * @return newly constructed table, std::nullopt if failed
363 */
364std::optional<Table> updateTable(const Table& table, const void* entry,
365 size_t size);
366
John Wang29683b52020-02-27 16:41:44 +0800367} // namespace attribute_value
368
369} // namespace table
370
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +0530371} // namespace bios
372} // namespace responder
373} // namespace pldm