1cb7f2d44SDeepak Kodihalli #pragma once
2cb7f2d44SDeepak Kodihalli 
3c453e164SGeorge Liu #include <libpldm/bios.h>
4c453e164SGeorge Liu #include <libpldm/bios_table.h>
5cb7f2d44SDeepak Kodihalli 
6*b3b84b49SPavithra Barithaya #include <cstdint>
7cb7f2d44SDeepak Kodihalli #include <filesystem>
8d965934fSJohn Wang #include <optional>
9e2efdcceSJohn Wang #include <string>
10cb7f2d44SDeepak Kodihalli #include <vector>
11cb7f2d44SDeepak Kodihalli 
12cb7f2d44SDeepak Kodihalli namespace pldm
13cb7f2d44SDeepak Kodihalli {
14cb7f2d44SDeepak Kodihalli 
15cb7f2d44SDeepak Kodihalli namespace responder
16cb7f2d44SDeepak Kodihalli {
17cb7f2d44SDeepak Kodihalli 
18cb7f2d44SDeepak Kodihalli namespace bios
19cb7f2d44SDeepak Kodihalli {
20cb7f2d44SDeepak Kodihalli 
21cb7f2d44SDeepak Kodihalli using Table = std::vector<uint8_t>;
22cb7f2d44SDeepak Kodihalli using Response = std::vector<uint8_t>;
23cb7f2d44SDeepak Kodihalli namespace fs = std::filesystem;
24cb7f2d44SDeepak Kodihalli 
25cb7f2d44SDeepak Kodihalli /** @class BIOSTable
26cb7f2d44SDeepak Kodihalli  *
27cb7f2d44SDeepak Kodihalli  *  @brief Provides APIs for storing and loading BIOS tables
28cb7f2d44SDeepak Kodihalli  *
29cb7f2d44SDeepak Kodihalli  *  Typical usage is as follows:
30cb7f2d44SDeepak Kodihalli  *  BIOSTable table(BIOS_STRING_TABLE_FILE_PATH);
31cb7f2d44SDeepak Kodihalli  *  if (table.isEmpty()) { // no persisted table
32cb7f2d44SDeepak Kodihalli  *    // construct BIOSTable 't'
33cb7f2d44SDeepak Kodihalli  *    // prepare Response 'r'
34cb7f2d44SDeepak Kodihalli  *    // send response to GetBIOSTable
35cb7f2d44SDeepak Kodihalli  *    table.store(t); // persisted
36cb7f2d44SDeepak Kodihalli  *  }
37cb7f2d44SDeepak Kodihalli  *  else { // persisted table exists
38cb7f2d44SDeepak Kodihalli  *    // create Response 'r' which has response fields (except
39cb7f2d44SDeepak Kodihalli  *    // the table itself) to a GetBIOSTable command
40cb7f2d44SDeepak Kodihalli  *    table.load(r); // actual table will be pushed back to the vector
41cb7f2d44SDeepak Kodihalli  *  }
42cb7f2d44SDeepak Kodihalli  */
43cb7f2d44SDeepak Kodihalli class BIOSTable
44cb7f2d44SDeepak Kodihalli {
45cb7f2d44SDeepak Kodihalli   public:
46cb7f2d44SDeepak Kodihalli     /** @brief Ctor - set file path to persist BIOS table
47cb7f2d44SDeepak Kodihalli      *
48cb7f2d44SDeepak Kodihalli      *  @param[in] filePath - file where BIOS table should be persisted
49cb7f2d44SDeepak Kodihalli      */
50cb7f2d44SDeepak Kodihalli     BIOSTable(const char* filePath);
51cb7f2d44SDeepak Kodihalli 
52cb7f2d44SDeepak Kodihalli     /** @brief Checks if there's a persisted BIOS table
53cb7f2d44SDeepak Kodihalli      *
54cb7f2d44SDeepak Kodihalli      *  @return bool - true if table exists, false otherwise
55cb7f2d44SDeepak Kodihalli      */
56cb7f2d44SDeepak Kodihalli     bool isEmpty() const noexcept;
57cb7f2d44SDeepak Kodihalli 
58cb7f2d44SDeepak Kodihalli     /** @brief Persist a BIOS table(string/attribute/attribute value)
59cb7f2d44SDeepak Kodihalli      *
60cb7f2d44SDeepak Kodihalli      *  @param[in] table - BIOS table
61cb7f2d44SDeepak Kodihalli      */
62cb7f2d44SDeepak Kodihalli     void store(const Table& table);
63cb7f2d44SDeepak Kodihalli 
64cb7f2d44SDeepak Kodihalli     /** @brief Load BIOS table from persistent store to memory
65cb7f2d44SDeepak Kodihalli      *
66cb7f2d44SDeepak Kodihalli      *  @param[in,out] response - PLDM response message to GetBIOSTable
67cb7f2d44SDeepak Kodihalli      *  (excluding table), table will be pushed back to this.
68cb7f2d44SDeepak Kodihalli      */
69cb7f2d44SDeepak Kodihalli     void load(Response& response) const;
70cb7f2d44SDeepak Kodihalli 
71cb7f2d44SDeepak Kodihalli   private:
72cb7f2d44SDeepak Kodihalli     // file storing PLDM BIOS table
73cb7f2d44SDeepak Kodihalli     fs::path filePath;
74cb7f2d44SDeepak Kodihalli };
75cb7f2d44SDeepak Kodihalli 
76e2efdcceSJohn Wang /** @class BIOSStringTableInterface
77e2efdcceSJohn Wang  *  @brief Provide interfaces to the BIOS string table operations
78e2efdcceSJohn Wang  */
79e2efdcceSJohn Wang class BIOSStringTableInterface
80e2efdcceSJohn Wang {
81e2efdcceSJohn Wang   public:
82e2efdcceSJohn Wang     virtual ~BIOSStringTableInterface() = default;
83e2efdcceSJohn Wang 
84e2efdcceSJohn Wang     /** @brief Find the string name from the BIOS string table for a string
85e2efdcceSJohn Wang      * handle
86e2efdcceSJohn Wang      *  @param[in] handle - string handle
87e2efdcceSJohn Wang      *  @return name of the corresponding BIOS string
88e2efdcceSJohn Wang      */
89e2efdcceSJohn Wang     virtual std::string findString(uint16_t handle) const = 0;
90e2efdcceSJohn Wang 
91e2efdcceSJohn Wang     /** @brief Find the string handle from the BIOS string table by the given
92e2efdcceSJohn Wang      *         name
93e2efdcceSJohn Wang      *  @param[in] name - name of the BIOS string
94e2efdcceSJohn Wang      *  @return handle of the string
95e2efdcceSJohn Wang      */
96e2efdcceSJohn Wang     virtual uint16_t findHandle(const std::string& name) const = 0;
97e2efdcceSJohn Wang };
98e2efdcceSJohn Wang 
99e2efdcceSJohn Wang /** @class BIOSStringTable
100e2efdcceSJohn Wang  *  @brief Collection of BIOS string table operations.
101e2efdcceSJohn Wang  */
102e2efdcceSJohn Wang class BIOSStringTable : public BIOSStringTableInterface
103f719f3bfSJohn Wang {
104f719f3bfSJohn Wang   public:
105e297b9f4SJohn Wang     /** @brief Constructs BIOSStringTable
106f719f3bfSJohn Wang      *
107e297b9f4SJohn Wang      *  @param[in] stringTable - The stringTable in RAM
108f719f3bfSJohn Wang      */
109e297b9f4SJohn Wang     BIOSStringTable(const Table& stringTable);
110e297b9f4SJohn Wang 
111e297b9f4SJohn Wang     /** @brief Constructs BIOSStringTable
112e297b9f4SJohn Wang      *
113e297b9f4SJohn Wang      *  @param[in] biosTable - The BIOSTable
114e297b9f4SJohn Wang      */
115e297b9f4SJohn Wang     BIOSStringTable(const BIOSTable& biosTable);
116f719f3bfSJohn Wang 
117f719f3bfSJohn Wang     /** @brief Find the string name from the BIOS string table for a string
118f719f3bfSJohn Wang      * handle
119f719f3bfSJohn Wang      *  @param[in] handle - string handle
120e297b9f4SJohn Wang      *  @return name of the corresponding BIOS string
121e297b9f4SJohn Wang      *  @throw std::invalid_argument if the string can not be found.
122f719f3bfSJohn Wang      */
123e2efdcceSJohn Wang     std::string findString(uint16_t handle) const override;
124f719f3bfSJohn Wang 
125e297b9f4SJohn Wang     /** @brief Find the string handle from the BIOS string table by the given
126e297b9f4SJohn Wang      *         name
127e297b9f4SJohn Wang      *  @param[in] name - name of the BIOS string
128e297b9f4SJohn Wang      *  @return handle of the string
129e297b9f4SJohn Wang      *  @throw std::invalid_argument if the string can not be found
130e297b9f4SJohn Wang      */
131e2efdcceSJohn Wang     uint16_t findHandle(const std::string& name) const override;
132e2efdcceSJohn Wang 
13329683b53SJohn Wang   private:
13429683b53SJohn Wang     Table stringTable;
13529683b53SJohn Wang };
13629683b53SJohn Wang 
13729683b53SJohn Wang namespace table
13829683b53SJohn Wang {
13929683b53SJohn Wang 
140d965934fSJohn Wang /** @brief Append Pad and Checksum
141d965934fSJohn Wang  *
142d965934fSJohn Wang  *  @param[in,out] table - table to be appended with pad and checksum
143d965934fSJohn Wang  */
144d965934fSJohn Wang void appendPadAndChecksum(Table& table);
145d965934fSJohn Wang 
14629683b53SJohn Wang namespace string
14729683b53SJohn Wang {
14829683b53SJohn Wang 
149e2efdcceSJohn Wang /** @brief Get the string handle for the entry
150e2efdcceSJohn Wang  *  @param[in] entry - Pointer to a bios string table entry
151e2efdcceSJohn Wang  *  @return Handle to identify a string in the bios string table
152e2efdcceSJohn Wang  */
15329683b53SJohn Wang uint16_t decodeHandle(const pldm_bios_string_table_entry* entry);
154e2efdcceSJohn Wang 
155e2efdcceSJohn Wang /** @brief Get the string from the entry
156e2efdcceSJohn Wang  *  @param[in] entry - Pointer to a bios string table entry
157e2efdcceSJohn Wang  *  @return The String
158e2efdcceSJohn Wang  */
15929683b53SJohn Wang std::string decodeString(const pldm_bios_string_table_entry* entry);
160e297b9f4SJohn Wang 
161d965934fSJohn Wang /** @brief construct entry of string table at the end of the given
162d965934fSJohn Wang  *         table
163d965934fSJohn Wang  *  @param[in,out] table - The given table
164d965934fSJohn Wang  *  @param[in] str - string itself
165d965934fSJohn Wang  *  @return pointer to the constructed entry
166d965934fSJohn Wang  */
16716c2a0a0SPatrick Williams const pldm_bios_string_table_entry*
16816c2a0a0SPatrick Williams     constructEntry(Table& table, const std::string& str);
169d965934fSJohn Wang 
17029683b53SJohn Wang } // namespace string
17129683b53SJohn Wang 
17229683b53SJohn Wang namespace attribute
17329683b53SJohn Wang {
17429683b53SJohn Wang 
17529683b53SJohn Wang /** @struct TableHeader
17629683b53SJohn Wang  *  @brief Header of attribute table
17729683b53SJohn Wang  */
17829683b53SJohn Wang struct TableHeader
17929683b53SJohn Wang {
18029683b53SJohn Wang     uint16_t attrHandle;
18129683b53SJohn Wang     uint8_t attrType;
18229683b53SJohn Wang     uint16_t stringHandle;
183f719f3bfSJohn Wang };
184f719f3bfSJohn Wang 
18529683b53SJohn Wang /** @brief Decode header of attribute table entry
18629683b53SJohn Wang  *  @param[in] entry - Pointer to an attribute table entry
18729683b53SJohn Wang  *  @return Attribute table header
18829683b53SJohn Wang  */
18929683b53SJohn Wang TableHeader decodeHeader(const pldm_bios_attr_table_entry* entry);
19029683b53SJohn Wang 
191d965934fSJohn Wang /** @brief Find attribute entry by handle
192d965934fSJohn Wang  *  @param[in] table - attribute table
193d965934fSJohn Wang  *  @param[in] handle - attribute handle
194d965934fSJohn Wang  *  @return Pointer to the attribute table entry
195d965934fSJohn Wang  */
19616c2a0a0SPatrick Williams const pldm_bios_attr_table_entry*
19716c2a0a0SPatrick Williams     findByHandle(const Table& table, uint16_t handle);
198d965934fSJohn Wang 
19945fed20fSJohn Wang /** @brief Find attribute entry by string handle
20045fed20fSJohn Wang  *  @param[in] table - attribute table
20145fed20fSJohn Wang  *  @param[in] handle - string handle
20245fed20fSJohn Wang  *  @return Pointer to the attribute table entry
20345fed20fSJohn Wang  */
20416c2a0a0SPatrick Williams const pldm_bios_attr_table_entry*
20516c2a0a0SPatrick Williams     findByStringHandle(const Table& table, uint16_t handle);
20645fed20fSJohn Wang 
20729683b53SJohn Wang /** @struct StringField
20829683b53SJohn Wang  *  @brief String field of attribute table
20929683b53SJohn Wang  */
21029683b53SJohn Wang struct StringField
21129683b53SJohn Wang {
21229683b53SJohn Wang     uint8_t stringType;
21329683b53SJohn Wang     uint16_t minLength;
21429683b53SJohn Wang     uint16_t maxLength;
21529683b53SJohn Wang     uint16_t defLength;
21629683b53SJohn Wang     std::string defString;
21729683b53SJohn Wang };
21829683b53SJohn Wang 
21929683b53SJohn Wang /** @brief decode string entry of attribute table
22029683b53SJohn Wang  *  @param[in] entry - Pointer to an attribute table entry
22129683b53SJohn Wang  *  @return String field of the entry
22229683b53SJohn Wang  */
22329683b53SJohn Wang StringField decodeStringEntry(const pldm_bios_attr_table_entry* entry);
22429683b53SJohn Wang 
22529683b53SJohn Wang /** @brief construct string entry of attribute table at the end of the given
22629683b53SJohn Wang  *         table
22729683b53SJohn Wang  *  @param[in,out] table - The given table
22829683b53SJohn Wang  *  @param[in] info - string info
22929683b53SJohn Wang  *  @return pointer to the constructed entry
23029683b53SJohn Wang  */
23116c2a0a0SPatrick Williams const pldm_bios_attr_table_entry* constructStringEntry(
23216c2a0a0SPatrick Williams     Table& table, pldm_bios_table_attr_entry_string_info* info);
23329683b53SJohn Wang 
23495e6b3c1SJohn Wang /** @struct IntegerField
23595e6b3c1SJohn Wang  *  @brief Integer field of attribute table
23695e6b3c1SJohn Wang  */
23795e6b3c1SJohn Wang struct IntegerField
23895e6b3c1SJohn Wang {
23995e6b3c1SJohn Wang     uint64_t lowerBound;
24095e6b3c1SJohn Wang     uint64_t upperBound;
24195e6b3c1SJohn Wang     uint32_t scalarIncrement;
24295e6b3c1SJohn Wang     uint64_t defaultValue;
24395e6b3c1SJohn Wang };
24495e6b3c1SJohn Wang 
24595e6b3c1SJohn Wang /** @brief construct integer entry of attribute table at the end of the
24695e6b3c1SJohn Wang  *         given table
24795e6b3c1SJohn Wang  *  @param[in,out] table - The given table
24895e6b3c1SJohn Wang  *  @param[in] info - integer info
24995e6b3c1SJohn Wang  *  @return pointer to the constructed entry
25095e6b3c1SJohn Wang  */
25116c2a0a0SPatrick Williams const pldm_bios_attr_table_entry* constructIntegerEntry(
25216c2a0a0SPatrick Williams     Table& table, pldm_bios_table_attr_entry_integer_info* info);
25395e6b3c1SJohn Wang 
25495e6b3c1SJohn Wang /** @brief decode integer entry of attribute table
25595e6b3c1SJohn Wang  *  @param[in] entry - Pointer to an attribute table entry
25695e6b3c1SJohn Wang  *  @return Integer field of the entry
25795e6b3c1SJohn Wang  */
25895e6b3c1SJohn Wang IntegerField decodeIntegerEntry(const pldm_bios_attr_table_entry* entry);
25995e6b3c1SJohn Wang 
2603be7085eSJohn Wang /** @struct EnumField
2613be7085eSJohn Wang  *  @brief Enum field of attribute table
2623be7085eSJohn Wang  */
2633be7085eSJohn Wang struct EnumField
2643be7085eSJohn Wang {
2653be7085eSJohn Wang     std::vector<uint16_t> possibleValueStringHandle;
2663be7085eSJohn Wang     std::vector<uint8_t> defaultValueIndex;
2673be7085eSJohn Wang };
2683be7085eSJohn Wang 
2693be7085eSJohn Wang /** @brief decode enum entry of attribute table
2703be7085eSJohn Wang  *  @param[in] entry - Pointer to an attribute table entry
2713be7085eSJohn Wang  *  @return Enum field of the entry
2723be7085eSJohn Wang  */
2733be7085eSJohn Wang EnumField decodeEnumEntry(const pldm_bios_attr_table_entry* entry);
2743be7085eSJohn Wang 
2753be7085eSJohn Wang /** @brief construct enum entry of attribute table at the end of the
2763be7085eSJohn Wang  *         given table
2773be7085eSJohn Wang  *  @param[in,out] table - The given table
2783be7085eSJohn Wang  *  @param[in] info - enum info
2793be7085eSJohn Wang  *  @return pointer to the constructed entry
2803be7085eSJohn Wang  */
28116c2a0a0SPatrick Williams const pldm_bios_attr_table_entry* constructEnumEntry(
28216c2a0a0SPatrick Williams     Table& table, pldm_bios_table_attr_entry_enum_info* info);
2833be7085eSJohn Wang 
28429683b53SJohn Wang } // namespace attribute
28529683b53SJohn Wang 
28629683b53SJohn Wang namespace attribute_value
28729683b53SJohn Wang {
28829683b53SJohn Wang 
28929683b53SJohn Wang /** @struct TableHeader
29029683b53SJohn Wang  *  @brief Header of attribute value table
29129683b53SJohn Wang  */
29229683b53SJohn Wang struct TableHeader
29329683b53SJohn Wang {
29429683b53SJohn Wang     uint16_t attrHandle;
29529683b53SJohn Wang     uint8_t attrType;
29629683b53SJohn Wang };
29729683b53SJohn Wang 
29829683b53SJohn Wang /** @brief Decode header of attribute value table
29929683b53SJohn Wang  *  @param[in] entry - Pointer to an attribute value table entry
30029683b53SJohn Wang  *  @return Attribute value table header
30129683b53SJohn Wang  */
30229683b53SJohn Wang TableHeader decodeHeader(const pldm_bios_attr_val_table_entry* entry);
30329683b53SJohn Wang 
30429683b53SJohn Wang /** @brief Decode string entry of attribute value table
30529683b53SJohn Wang  *  @param[in] entry - Pointer to an attribute value table entry
30629683b53SJohn Wang  *  @return The decoded string
30729683b53SJohn Wang  */
30829683b53SJohn Wang std::string decodeStringEntry(const pldm_bios_attr_val_table_entry* entry);
30929683b53SJohn Wang 
31095e6b3c1SJohn Wang /** @brief Decode integer entry of attribute value table
31195e6b3c1SJohn Wang  *  @param[in] entry - Pointer to an attribute value table entry
31295e6b3c1SJohn Wang  *  @return The decoded integer
31395e6b3c1SJohn Wang  */
31495e6b3c1SJohn Wang uint64_t decodeIntegerEntry(const pldm_bios_attr_val_table_entry* entry);
31595e6b3c1SJohn Wang 
3163be7085eSJohn Wang /** @brief Decode enum entry of attribute value table
3173be7085eSJohn Wang  *  @param[in] entry - Pointer to an attribute value table entry
3183be7085eSJohn Wang  *  @return Current value string handle indices
3193be7085eSJohn Wang  */
3203be7085eSJohn Wang std::vector<uint8_t>
3213be7085eSJohn Wang     decodeEnumEntry(const pldm_bios_attr_val_table_entry* entry);
3223be7085eSJohn Wang 
32329683b53SJohn Wang /** @brief Construct string entry of attribute value table at the end of the
32429683b53SJohn Wang  *         given table
32529683b53SJohn Wang  *  @param[in] table - The given table
32629683b53SJohn Wang  *  @param[in] attrHandle - attribute handle
32729683b53SJohn Wang  *  @param[in] attrType - attribute type
32829683b53SJohn Wang  *  @param[in] str - The string
32929683b53SJohn Wang  *  @return Pointer to the constructed entry
33029683b53SJohn Wang  */
33129683b53SJohn Wang const pldm_bios_attr_val_table_entry*
33229683b53SJohn Wang     constructStringEntry(Table& table, uint16_t attrHandle, uint8_t attrType,
33329683b53SJohn Wang                          const std::string& str);
33429683b53SJohn Wang 
33595e6b3c1SJohn Wang /** @brief Construct integer entry of attribute value table at the end of
33695e6b3c1SJohn Wang  *         the given table
33795e6b3c1SJohn Wang  *  @param[in] table - The given table
33895e6b3c1SJohn Wang  *  @param[in] attrHandle - attribute handle
33995e6b3c1SJohn Wang  *  @param[in] attrType - attribute type
34095e6b3c1SJohn Wang  *  @param[in] value - The integer
34195e6b3c1SJohn Wang  *  @return Pointer to the constructed entry
34295e6b3c1SJohn Wang  */
34316c2a0a0SPatrick Williams const pldm_bios_attr_val_table_entry* constructIntegerEntry(
34416c2a0a0SPatrick Williams     Table& table, uint16_t attrHandle, uint8_t attrType, uint64_t value);
34595e6b3c1SJohn Wang 
3463be7085eSJohn Wang /** @brief Construct enum entry of attribute value table at the end of
3473be7085eSJohn Wang  *         the given table
3483be7085eSJohn Wang  *  @param[in] table - The given table
3493be7085eSJohn Wang  *  @param[in] attrHandle - attribute handle
3503be7085eSJohn Wang  *  @param[in] attrType - attribute type
3513be7085eSJohn Wang  *  @param[in] handleIndices -  handle indices
3523be7085eSJohn Wang  *  @return Pointer to the constructed entry
3533be7085eSJohn Wang  */
3543be7085eSJohn Wang const pldm_bios_attr_val_table_entry*
3553be7085eSJohn Wang     constructEnumEntry(Table& table, uint16_t attrHandle, uint8_t attrType,
3563be7085eSJohn Wang                        const std::vector<uint8_t>& handleIndices);
3573be7085eSJohn Wang 
358d965934fSJohn Wang /** @brief construct a table with an new entry
359d965934fSJohn Wang  *  @param[in] table - the table need to be updated
360d965934fSJohn Wang  *  @param[in] entry - the new attribute value entry
361d965934fSJohn Wang  *  @param[in] size - size of the new entry
362d965934fSJohn Wang  *  @return newly constructed table, std::nullopt if failed
363d965934fSJohn Wang  */
364d965934fSJohn Wang std::optional<Table> updateTable(const Table& table, const void* entry,
365d965934fSJohn Wang                                  size_t size);
366d965934fSJohn Wang 
36729683b53SJohn Wang } // namespace attribute_value
36829683b53SJohn Wang 
36929683b53SJohn Wang } // namespace table
37029683b53SJohn Wang 
371cb7f2d44SDeepak Kodihalli } // namespace bios
372cb7f2d44SDeepak Kodihalli } // namespace responder
373cb7f2d44SDeepak Kodihalli } // namespace pldm
374