blob: cf0ef2b61c34122f7b1b7567001bea73063c00df [file] [log] [blame]
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -06001#pragma once
2
3#include "const.hpp"
4#include "types.hpp"
5
Patrick Williamsc78d8872023-05-10 07:50:56 -05006#include <nlohmann/json.hpp>
7
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -06008#include <cstddef>
9#include <fstream>
10#include <tuple>
11
12namespace openpower
13{
14namespace vpd
15{
16namespace manager
17{
18namespace editor
19{
20
PriyangaRamasamy794ad822021-03-29 02:44:16 -050021/** @class EditorImpl */
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060022class EditorImpl
23{
24 public:
25 EditorImpl() = delete;
26 EditorImpl(const EditorImpl&) = delete;
27 EditorImpl& operator=(const EditorImpl&) = delete;
28 EditorImpl(EditorImpl&&) = delete;
29 EditorImpl& operator=(EditorImpl&&) = delete;
Patrick Williamsc78d8872023-05-10 07:50:56 -050030 ~EditorImpl() {}
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060031
32 /** @brief Construct EditorImpl class
33 *
PriyangaRamasamy794ad822021-03-29 02:44:16 -050034 * @param[in] record - Record Name
35 * @param[in] kwd - Keyword
36 * @param[in] vpd - Vpd Vector
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060037 */
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050038 EditorImpl(const std::string& record, const std::string& kwd,
39 Binary&& vpd) :
Patrick Williams08dc31c2024-08-16 15:21:06 -040040 startOffset(0), thisRecord(record, kwd), vpdFile(std::move(vpd))
Patrick Williamsc78d8872023-05-10 07:50:56 -050041 {}
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050042
43 /** @brief Construct EditorImpl class
44 *
45 * @param[in] path - Path to the vpd file
PriyangaRamasamy794ad822021-03-29 02:44:16 -050046 * @param[in] json - Parsed inventory json
47 * @param[in] record - Record name
48 * @param[in] kwd - Keyword
49 * @param[in] inventoryPath - Inventory path of the vpd
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050050 */
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -060051 EditorImpl(const inventory::Path& path, const nlohmann::json& json,
Alpana Kumari920408d2020-05-14 00:07:03 -050052 const std::string& record, const std::string& kwd,
53 const sdbusplus::message::object_path& inventoryPath) :
Patrick Williams08dc31c2024-08-16 15:21:06 -040054 vpdFilePath(path), objPath(inventoryPath), startOffset(0),
55 jsonFile(json), thisRecord(record, kwd)
Patrick Williamsc78d8872023-05-10 07:50:56 -050056 {}
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060057
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053058 /** @brief Construct EditorImpl class
59 *
60 * @param[in] path - EEPROM path
61 * @param[in] json - Parsed inventory json object
62 * @param[in] record - Record name
63 * @param[in] kwd - Keyword name
64 */
65 EditorImpl(const inventory::Path& path, const nlohmann::json& json,
66 const std::string& record, const std::string& kwd) :
Patrick Williams08dc31c2024-08-16 15:21:06 -040067 vpdFilePath(path), jsonFile(json), thisRecord(record, kwd)
Patrick Williamsc78d8872023-05-10 07:50:56 -050068 {}
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053069
PriyangaRamasamy794ad822021-03-29 02:44:16 -050070 /**
71 * @brief Update data for keyword
72 * The method looks for the record name to update in VTOC and then
73 * looks for the keyword name in that record. when found it updates the data
74 * of keyword with the given data. It does not block keyword data update in
75 * case the length of new data is greater than or less than the current data
Manojkiran Edaaf921752024-06-17 15:10:21 +053076 * length. If the new data length is more than the length allotted to that
PriyangaRamasamy794ad822021-03-29 02:44:16 -050077 * keyword the new data will be truncated to update only the allotted
78 * length. Similarly if the new data length is less then only that much data
79 * will be updated for the keyword and remaining bits will be left
80 * unchanged.
81 *
82 * Following is the algorithm used to update keyword:
83 * 1) Look for the record name in the given VPD file
84 * 2) Look for the keyword name for which data needs to be updated
85 * which is the table of contents record.
86 * 3) update the data for that keyword with the new data
87 *
88 * @param[in] kwdData - data to update
Santosh Puranika0b23912022-02-10 13:37:09 +053089 * @param[in] offset - offset at which the VPD starts
PriyangaRamasamy794ad822021-03-29 02:44:16 -050090 * @param[in] updCache - Flag which tells whether to update Cache or not.
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060091 */
Santosh Puranika0b23912022-02-10 13:37:09 +053092 void updateKeyword(const Binary& kwdData, uint32_t offset,
93 const bool& updCache);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060094
SunnySrivastava198443306542020-04-01 02:50:20 -050095 /** @brief Expands location code on DBUS
96 * @param[in] locationCodeType - "fcs" or "mts"
97 */
98 void expandLocationCode(const std::string& locationCodeType);
99
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600100 private:
101 /** @brief read VTOC record from the vpd file
102 */
103 void readVTOC();
104
105 /** @brief validate ecc data for the VTOC record
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500106 * @param[in] itrToRecData -iterator to the record data
107 * @param[in] itrToECCData - iterator to the ECC data
108 * @param[in] recLength - Length of the record
109 * @param[in] eccLength - Length of the record's ECC
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600110 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500111 void checkECC(Binary::const_iterator& itrToRecData,
112 Binary::const_iterator& itrToECCData,
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600113 openpower::vpd::constants::RecordLength recLength,
114 openpower::vpd::constants::ECCLength eccLength);
115
116 /** @brief reads value at the given offset
117 * @param[in] offset - offset value
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500118 * @return value at that offset in bigendian
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600119 */
120 auto getValue(openpower::vpd::constants::offsets::Offsets offset);
121
122 /** @brief Checks if required record name exist in the VPD file
123 * @param[in] iterator - pointing to start of PT kwd
124 * @param[in] ptLength - length of the PT kwd
125 */
126 void checkPTForRecord(Binary::const_iterator& iterator, Byte ptLength);
127
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500128 /** @brief Checks for required keyword in the record */
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600129 void checkRecordForKwd();
130
131 /** @brief update data for given keyword
132 * @param[in] kwdData- data to be updated
133 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500134 void updateData(const Binary& kwdData);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600135
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500136 /** @brief update record ECC */
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600137 void updateRecordECC();
138
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500139 /** @brief method to update cache once the data for keyword has been updated
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600140 */
141 void updateCache();
142
143 /** @brief method to process and update CI in case required
144 * @param[in] - objectPath - path of the object to introspect
145 */
146 void processAndUpdateCI(const std::string& objectPath);
147
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500148 /** @brief method to process and update extra interface
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600149 * @param[in] Inventory - single inventory json subpart
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500150 * @param[in] objPath - path of the object to introspect
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600151 */
152 void processAndUpdateEI(const nlohmann::json& Inventory,
153 const inventory::Path& objPath);
154
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600155 /** @brief method to make busctl call
156 *
157 * @param[in] object - bus object path
158 * @param[in] interface - bus interface
159 * @param[in] property - property to update on BUS
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500160 * @param[in] data - data to be updated on Bus
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600161 *
162 */
163 template <typename T>
164 void makeDbusCall(const std::string& object, const std::string& interface,
165 const std::string& property, const std::variant<T>& data);
166
girik18bb9852022-11-16 05:48:13 -0600167 /** @brief Method to check the record's Data using ECC */
168 void checkRecordData();
169
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600170 // path to the VPD file to edit
Alpana Kumari920408d2020-05-14 00:07:03 -0500171 inventory::Path vpdFilePath;
172
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500173 // inventory path of the vpd fru to update keyword
Sunny Srivastavaf31a91b2022-06-09 08:11:29 -0500174 inventory::Path objPath{};
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600175
176 // stream to perform operation on file
177 std::fstream vpdFileStream;
178
girik18bb9852022-11-16 05:48:13 -0600179 // stream to operate on VPD data
180 std::fstream vpdDataFileStream;
181
Alpana Kumari920408d2020-05-14 00:07:03 -0500182 // offset to get vpd data from EEPROM
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500183 uint32_t startOffset;
Alpana Kumari920408d2020-05-14 00:07:03 -0500184
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600185 // file to store parsed json
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500186 const nlohmann::json jsonFile;
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600187
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600188 // structure to hold info about record to edit
189 struct RecInfo
190 {
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500191 Binary kwdUpdatedData; // need access to it in case encoding is needed
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500192 const std::string recName;
193 const std::string recKWd;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600194 openpower::vpd::constants::RecordOffset recOffset;
195 openpower::vpd::constants::ECCOffset recECCoffset;
196 std::size_t recECCLength;
197 std::size_t kwdDataLength;
198 openpower::vpd::constants::RecordSize recSize;
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500199 openpower::vpd::constants::DataOffset kwDataOffset;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600200 // constructor
201 RecInfo(const std::string& rec, const std::string& kwd) :
202 recName(rec), recKWd(kwd), recOffset(0), recECCoffset(0),
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500203 recECCLength(0), kwdDataLength(0), recSize(0), kwDataOffset(0)
Patrick Williamsc78d8872023-05-10 07:50:56 -0500204 {}
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600205 } thisRecord;
206
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500207 Binary vpdFile;
208
Alpana Kumari920408d2020-05-14 00:07:03 -0500209 // If requested Interface is common Interface
210 bool isCI;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600211}; // class EditorImpl
212
213} // namespace editor
214} // namespace manager
215} // namespace vpd
Patrick Williamsc78d8872023-05-10 07:50:56 -0500216} // namespace openpower