blob: 98a2fa1c61869e91354626e341da45d54e97efcf [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) :
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050040 startOffset(0),
41 thisRecord(record, kwd), vpdFile(std::move(vpd))
Patrick Williamsc78d8872023-05-10 07:50:56 -050042 {}
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050043
44 /** @brief Construct EditorImpl class
45 *
46 * @param[in] path - Path to the vpd file
PriyangaRamasamy794ad822021-03-29 02:44:16 -050047 * @param[in] json - Parsed inventory json
48 * @param[in] record - Record name
49 * @param[in] kwd - Keyword
50 * @param[in] inventoryPath - Inventory path of the vpd
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050051 */
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -060052 EditorImpl(const inventory::Path& path, const nlohmann::json& json,
Alpana Kumari920408d2020-05-14 00:07:03 -050053 const std::string& record, const std::string& kwd,
54 const sdbusplus::message::object_path& inventoryPath) :
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060055 vpdFilePath(path),
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050056 objPath(inventoryPath), startOffset(0), jsonFile(json),
57 thisRecord(record, kwd)
Patrick Williamsc78d8872023-05-10 07:50:56 -050058 {}
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060059
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053060 /** @brief Construct EditorImpl class
61 *
62 * @param[in] path - EEPROM path
63 * @param[in] json - Parsed inventory json object
64 * @param[in] record - Record name
65 * @param[in] kwd - Keyword name
66 */
67 EditorImpl(const inventory::Path& path, const nlohmann::json& json,
68 const std::string& record, const std::string& kwd) :
69 vpdFilePath(path),
70 jsonFile(json), thisRecord(record, kwd)
Patrick Williamsc78d8872023-05-10 07:50:56 -050071 {}
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053072
PriyangaRamasamy794ad822021-03-29 02:44:16 -050073 /**
74 * @brief Update data for keyword
75 * The method looks for the record name to update in VTOC and then
76 * looks for the keyword name in that record. when found it updates the data
77 * of keyword with the given data. It does not block keyword data update in
78 * case the length of new data is greater than or less than the current data
Manojkiran Edaaf921752024-06-17 15:10:21 +053079 * length. If the new data length is more than the length allotted to that
PriyangaRamasamy794ad822021-03-29 02:44:16 -050080 * keyword the new data will be truncated to update only the allotted
81 * length. Similarly if the new data length is less then only that much data
82 * will be updated for the keyword and remaining bits will be left
83 * unchanged.
84 *
85 * Following is the algorithm used to update keyword:
86 * 1) Look for the record name in the given VPD file
87 * 2) Look for the keyword name for which data needs to be updated
88 * which is the table of contents record.
89 * 3) update the data for that keyword with the new data
90 *
91 * @param[in] kwdData - data to update
Santosh Puranika0b23912022-02-10 13:37:09 +053092 * @param[in] offset - offset at which the VPD starts
PriyangaRamasamy794ad822021-03-29 02:44:16 -050093 * @param[in] updCache - Flag which tells whether to update Cache or not.
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060094 */
Santosh Puranika0b23912022-02-10 13:37:09 +053095 void updateKeyword(const Binary& kwdData, uint32_t offset,
96 const bool& updCache);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060097
SunnySrivastava198443306542020-04-01 02:50:20 -050098 /** @brief Expands location code on DBUS
99 * @param[in] locationCodeType - "fcs" or "mts"
100 */
101 void expandLocationCode(const std::string& locationCodeType);
102
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600103 private:
104 /** @brief read VTOC record from the vpd file
105 */
106 void readVTOC();
107
108 /** @brief validate ecc data for the VTOC record
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500109 * @param[in] itrToRecData -iterator to the record data
110 * @param[in] itrToECCData - iterator to the ECC data
111 * @param[in] recLength - Length of the record
112 * @param[in] eccLength - Length of the record's ECC
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600113 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500114 void checkECC(Binary::const_iterator& itrToRecData,
115 Binary::const_iterator& itrToECCData,
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600116 openpower::vpd::constants::RecordLength recLength,
117 openpower::vpd::constants::ECCLength eccLength);
118
119 /** @brief reads value at the given offset
120 * @param[in] offset - offset value
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500121 * @return value at that offset in bigendian
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600122 */
123 auto getValue(openpower::vpd::constants::offsets::Offsets offset);
124
125 /** @brief Checks if required record name exist in the VPD file
126 * @param[in] iterator - pointing to start of PT kwd
127 * @param[in] ptLength - length of the PT kwd
128 */
129 void checkPTForRecord(Binary::const_iterator& iterator, Byte ptLength);
130
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500131 /** @brief Checks for required keyword in the record */
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600132 void checkRecordForKwd();
133
134 /** @brief update data for given keyword
135 * @param[in] kwdData- data to be updated
136 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500137 void updateData(const Binary& kwdData);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600138
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500139 /** @brief update record ECC */
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600140 void updateRecordECC();
141
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500142 /** @brief method to update cache once the data for keyword has been updated
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600143 */
144 void updateCache();
145
146 /** @brief method to process and update CI in case required
147 * @param[in] - objectPath - path of the object to introspect
148 */
149 void processAndUpdateCI(const std::string& objectPath);
150
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500151 /** @brief method to process and update extra interface
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600152 * @param[in] Inventory - single inventory json subpart
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500153 * @param[in] objPath - path of the object to introspect
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600154 */
155 void processAndUpdateEI(const nlohmann::json& Inventory,
156 const inventory::Path& objPath);
157
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600158 /** @brief method to make busctl call
159 *
160 * @param[in] object - bus object path
161 * @param[in] interface - bus interface
162 * @param[in] property - property to update on BUS
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500163 * @param[in] data - data to be updated on Bus
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600164 *
165 */
166 template <typename T>
167 void makeDbusCall(const std::string& object, const std::string& interface,
168 const std::string& property, const std::variant<T>& data);
169
girik18bb9852022-11-16 05:48:13 -0600170 /** @brief Method to check the record's Data using ECC */
171 void checkRecordData();
172
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600173 // path to the VPD file to edit
Alpana Kumari920408d2020-05-14 00:07:03 -0500174 inventory::Path vpdFilePath;
175
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500176 // inventory path of the vpd fru to update keyword
Sunny Srivastavaf31a91b2022-06-09 08:11:29 -0500177 inventory::Path objPath{};
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600178
179 // stream to perform operation on file
180 std::fstream vpdFileStream;
181
girik18bb9852022-11-16 05:48:13 -0600182 // stream to operate on VPD data
183 std::fstream vpdDataFileStream;
184
Alpana Kumari920408d2020-05-14 00:07:03 -0500185 // offset to get vpd data from EEPROM
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500186 uint32_t startOffset;
Alpana Kumari920408d2020-05-14 00:07:03 -0500187
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600188 // file to store parsed json
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500189 const nlohmann::json jsonFile;
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600190
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600191 // structure to hold info about record to edit
192 struct RecInfo
193 {
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500194 Binary kwdUpdatedData; // need access to it in case encoding is needed
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500195 const std::string recName;
196 const std::string recKWd;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600197 openpower::vpd::constants::RecordOffset recOffset;
198 openpower::vpd::constants::ECCOffset recECCoffset;
199 std::size_t recECCLength;
200 std::size_t kwdDataLength;
201 openpower::vpd::constants::RecordSize recSize;
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500202 openpower::vpd::constants::DataOffset kwDataOffset;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600203 // constructor
204 RecInfo(const std::string& rec, const std::string& kwd) :
205 recName(rec), recKWd(kwd), recOffset(0), recECCoffset(0),
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500206 recECCLength(0), kwdDataLength(0), recSize(0), kwDataOffset(0)
Patrick Williamsc78d8872023-05-10 07:50:56 -0500207 {}
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600208 } thisRecord;
209
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500210 Binary vpdFile;
211
Alpana Kumari920408d2020-05-14 00:07:03 -0500212 // If requested Interface is common Interface
213 bool isCI;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600214}; // class EditorImpl
215
216} // namespace editor
217} // namespace manager
218} // namespace vpd
Patrick Williamsc78d8872023-05-10 07:50:56 -0500219} // namespace openpower