blob: c03354a929ab723a9b69bfe4653fac7864249b09 [file] [log] [blame]
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -06001#pragma once
2
3#include "const.hpp"
4#include "types.hpp"
5
6#include <cstddef>
7#include <fstream>
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -06008#include <nlohmann/json.hpp>
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -06009#include <tuple>
10
11namespace openpower
12{
13namespace vpd
14{
15namespace manager
16{
17namespace editor
18{
19
PriyangaRamasamy794ad822021-03-29 02:44:16 -050020/** @class EditorImpl */
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060021class EditorImpl
22{
23 public:
24 EditorImpl() = delete;
25 EditorImpl(const EditorImpl&) = delete;
26 EditorImpl& operator=(const EditorImpl&) = delete;
27 EditorImpl(EditorImpl&&) = delete;
28 EditorImpl& operator=(EditorImpl&&) = delete;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050029 ~EditorImpl()
30 {
31 }
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060032
33 /** @brief Construct EditorImpl class
34 *
PriyangaRamasamy794ad822021-03-29 02:44:16 -050035 * @param[in] record - Record Name
36 * @param[in] kwd - Keyword
37 * @param[in] vpd - Vpd Vector
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060038 */
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050039 EditorImpl(const std::string& record, const std::string& kwd,
40 Binary&& vpd) :
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050041 startOffset(0),
42 thisRecord(record, kwd), vpdFile(std::move(vpd))
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050043 {
44 }
45
46 /** @brief Construct EditorImpl class
47 *
48 * @param[in] path - Path to the vpd file
PriyangaRamasamy794ad822021-03-29 02:44:16 -050049 * @param[in] json - Parsed inventory json
50 * @param[in] record - Record name
51 * @param[in] kwd - Keyword
52 * @param[in] inventoryPath - Inventory path of the vpd
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050053 */
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -060054 EditorImpl(const inventory::Path& path, const nlohmann::json& json,
Alpana Kumari920408d2020-05-14 00:07:03 -050055 const std::string& record, const std::string& kwd,
56 const sdbusplus::message::object_path& inventoryPath) :
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060057 vpdFilePath(path),
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050058 objPath(inventoryPath), startOffset(0), jsonFile(json),
59 thisRecord(record, kwd)
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060060 {
61 }
62
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053063 /** @brief Construct EditorImpl class
64 *
65 * @param[in] path - EEPROM path
66 * @param[in] json - Parsed inventory json object
67 * @param[in] record - Record name
68 * @param[in] kwd - Keyword name
69 */
70 EditorImpl(const inventory::Path& path, const nlohmann::json& json,
71 const std::string& record, const std::string& kwd) :
72 vpdFilePath(path),
73 jsonFile(json), thisRecord(record, kwd)
74 {
75 }
76
PriyangaRamasamy794ad822021-03-29 02:44:16 -050077 /**
78 * @brief Update data for keyword
79 * The method looks for the record name to update in VTOC and then
80 * looks for the keyword name in that record. when found it updates the data
81 * of keyword with the given data. It does not block keyword data update in
82 * case the length of new data is greater than or less than the current data
83 * length. If the new data length is more than the length alotted to that
84 * keyword the new data will be truncated to update only the allotted
85 * length. Similarly if the new data length is less then only that much data
86 * will be updated for the keyword and remaining bits will be left
87 * unchanged.
88 *
89 * Following is the algorithm used to update keyword:
90 * 1) Look for the record name in the given VPD file
91 * 2) Look for the keyword name for which data needs to be updated
92 * which is the table of contents record.
93 * 3) update the data for that keyword with the new data
94 *
95 * @param[in] kwdData - data to update
96 * @param[in] updCache - Flag which tells whether to update Cache or not.
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060097 */
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053098 void updateKeyword(const Binary& kwdData, const bool& updCache);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060099
SunnySrivastava198443306542020-04-01 02:50:20 -0500100 /** @brief Expands location code on DBUS
101 * @param[in] locationCodeType - "fcs" or "mts"
102 */
103 void expandLocationCode(const std::string& locationCodeType);
104
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600105 private:
106 /** @brief read VTOC record from the vpd file
107 */
108 void readVTOC();
109
110 /** @brief validate ecc data for the VTOC record
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500111 * @param[in] itrToRecData -iterator to the record data
112 * @param[in] itrToECCData - iterator to the ECC data
113 * @param[in] recLength - Length of the record
114 * @param[in] eccLength - Length of the record's ECC
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600115 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500116 void checkECC(Binary::const_iterator& itrToRecData,
117 Binary::const_iterator& itrToECCData,
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600118 openpower::vpd::constants::RecordLength recLength,
119 openpower::vpd::constants::ECCLength eccLength);
120
121 /** @brief reads value at the given offset
122 * @param[in] offset - offset value
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500123 * @return value at that offset in bigendian
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600124 */
125 auto getValue(openpower::vpd::constants::offsets::Offsets offset);
126
127 /** @brief Checks if required record name exist in the VPD file
128 * @param[in] iterator - pointing to start of PT kwd
129 * @param[in] ptLength - length of the PT kwd
130 */
131 void checkPTForRecord(Binary::const_iterator& iterator, Byte ptLength);
132
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500133 /** @brief Checks for required keyword in the record */
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600134 void checkRecordForKwd();
135
136 /** @brief update data for given keyword
137 * @param[in] kwdData- data to be updated
138 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500139 void updateData(const Binary& kwdData);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600140
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500141 /** @brief update record ECC */
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600142 void updateRecordECC();
143
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500144 /** @brief method to update cache once the data for keyword has been updated
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600145 */
146 void updateCache();
147
148 /** @brief method to process and update CI in case required
149 * @param[in] - objectPath - path of the object to introspect
150 */
151 void processAndUpdateCI(const std::string& objectPath);
152
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500153 /** @brief method to process and update extra interface
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600154 * @param[in] Inventory - single inventory json subpart
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500155 * @param[in] objPath - path of the object to introspect
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600156 */
157 void processAndUpdateEI(const nlohmann::json& Inventory,
158 const inventory::Path& objPath);
159
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600160 /** @brief method to make busctl call
161 *
162 * @param[in] object - bus object path
163 * @param[in] interface - bus interface
164 * @param[in] property - property to update on BUS
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500165 * @param[in] data - data to be updated on Bus
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600166 *
167 */
168 template <typename T>
169 void makeDbusCall(const std::string& object, const std::string& interface,
170 const std::string& property, const std::variant<T>& data);
171
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600172 // path to the VPD file to edit
Alpana Kumari920408d2020-05-14 00:07:03 -0500173 inventory::Path vpdFilePath;
174
PriyangaRamasamy794ad822021-03-29 02:44:16 -0500175 // inventory path of the vpd fru to update keyword
Alpana Kumari920408d2020-05-14 00:07:03 -0500176 const inventory::Path objPath;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600177
178 // stream to perform operation on file
179 std::fstream vpdFileStream;
180
Alpana Kumari920408d2020-05-14 00:07:03 -0500181 // offset to get vpd data from EEPROM
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500182 uint32_t startOffset;
Alpana Kumari920408d2020-05-14 00:07:03 -0500183
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600184 // file to store parsed json
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500185 const nlohmann::json jsonFile;
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600186
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600187 // structure to hold info about record to edit
188 struct RecInfo
189 {
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500190 Binary kwdUpdatedData; // need access to it in case encoding is needed
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500191 const std::string recName;
192 const std::string recKWd;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600193 openpower::vpd::constants::RecordOffset recOffset;
194 openpower::vpd::constants::ECCOffset recECCoffset;
195 std::size_t recECCLength;
196 std::size_t kwdDataLength;
197 openpower::vpd::constants::RecordSize recSize;
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500198 openpower::vpd::constants::DataOffset kwDataOffset;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600199 // constructor
200 RecInfo(const std::string& rec, const std::string& kwd) :
201 recName(rec), recKWd(kwd), recOffset(0), recECCoffset(0),
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500202 recECCLength(0), kwdDataLength(0), recSize(0), kwDataOffset(0)
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600203 {
204 }
205 } 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
Santosh Puranik32c46502022-02-10 08:55:07 +0530216} // namespace openpower