blob: 9cf3b8ae184444995369d0cd377c7e76325a14a9 [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
20/** @class Editor
21 * @brief Implements VPD editing related functinality, currently
22 * implemented to support only keyword data update functionality.
23 *
24 * An Editor object must be constructed by passing in VPD in
25 * binary format. To edit the keyword data, call the updateKeyword() method.
26 * The method looks for the record name to update in VTOC and
27 * then looks for the keyword name in that record.
28 * when found it updates the data of keyword with the given data.
29 * It does not block keyword data update in case the length of new data is
30 * greater than or less than the current data length.
31 * If the new data length is more than the length alotted to that keyword
32 * the new data will be truncated to update only the allotted length.
33 * Similarly if the new data length is less then only that much data will
34 * be updated for the keyword and remaining bits will be left unchanged.
35 *
36 * Following is the algorithm used to update keyword:
37 * 1) Look for the record name in the given VPD file
38 * 2) Look for the keyword name for which data needs to be updated
39 * which is the table of contents record.
40 * 3) update the data for that keyword with the new data
41 */
42class EditorImpl
43{
44 public:
45 EditorImpl() = delete;
46 EditorImpl(const EditorImpl&) = delete;
47 EditorImpl& operator=(const EditorImpl&) = delete;
48 EditorImpl(EditorImpl&&) = delete;
49 EditorImpl& operator=(EditorImpl&&) = delete;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050050 ~EditorImpl()
51 {
52 }
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060053
54 /** @brief Construct EditorImpl class
55 *
56 * @param[in] path - Path to the vpd file
57 */
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050058 EditorImpl(const std::string& record, const std::string& kwd,
59 Binary&& vpd) :
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050060 startOffset(0),
61 thisRecord(record, kwd), vpdFile(std::move(vpd))
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050062 {
63 }
64
65 /** @brief Construct EditorImpl class
66 *
67 * @param[in] path - Path to the vpd file
68 */
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -060069 EditorImpl(const inventory::Path& path, const nlohmann::json& json,
Alpana Kumari920408d2020-05-14 00:07:03 -050070 const std::string& record, const std::string& kwd,
71 const sdbusplus::message::object_path& inventoryPath) :
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060072 vpdFilePath(path),
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050073 objPath(inventoryPath), startOffset(0), jsonFile(json),
74 thisRecord(record, kwd)
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060075 {
76 }
77
78 /** @brief Update data for keyword
79 * @param[in] kwdData - data to update
80 */
81 void updateKeyword(const Binary& kwdData);
82
SunnySrivastava198443306542020-04-01 02:50:20 -050083 /** @brief Expands location code on DBUS
84 * @param[in] locationCodeType - "fcs" or "mts"
85 */
86 void expandLocationCode(const std::string& locationCodeType);
87
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060088 private:
89 /** @brief read VTOC record from the vpd file
90 */
91 void readVTOC();
92
93 /** @brief validate ecc data for the VTOC record
SunnySrivastava19846d8314d2020-05-15 09:34:58 -050094 * @param[in] iterator to VTOC record data
95 * @param[in] iterator to VTOC ECC data
96 * @param[in] Lenght of VTOC record
97 * @param[in] Length of ECC record
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060098 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -050099 void checkECC(Binary::const_iterator& itrToRecData,
100 Binary::const_iterator& itrToECCData,
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600101 openpower::vpd::constants::RecordLength recLength,
102 openpower::vpd::constants::ECCLength eccLength);
103
104 /** @brief reads value at the given offset
105 * @param[in] offset - offset value
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500106 * @return value at that offset in bigendian
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600107 */
108 auto getValue(openpower::vpd::constants::offsets::Offsets offset);
109
110 /** @brief Checks if required record name exist in the VPD file
111 * @param[in] iterator - pointing to start of PT kwd
112 * @param[in] ptLength - length of the PT kwd
113 */
114 void checkPTForRecord(Binary::const_iterator& iterator, Byte ptLength);
115
116 /** @brief Checks for reuired keyword in the record
117 */
118 void checkRecordForKwd();
119
120 /** @brief update data for given keyword
121 * @param[in] kwdData- data to be updated
122 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500123 void updateData(const Binary& kwdData);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600124
125 /** @brief update record ECC
126 */
127 void updateRecordECC();
128
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600129 /** @brief method to update cache once the data for kwd has been updated
130 */
131 void updateCache();
132
133 /** @brief method to process and update CI in case required
134 * @param[in] - objectPath - path of the object to introspect
135 */
136 void processAndUpdateCI(const std::string& objectPath);
137
138 /** @brief method to process and update Extra Interface
139 * @param[in] Inventory - single inventory json subpart
140 * @param[in] objectPath - path of the object to introspect
141 */
142 void processAndUpdateEI(const nlohmann::json& Inventory,
143 const inventory::Path& objPath);
144
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600145 /** @brief method to make busctl call
146 *
147 * @param[in] object - bus object path
148 * @param[in] interface - bus interface
149 * @param[in] property - property to update on BUS
150 * @param[in] data - data to be updayed on Bus
151 *
152 */
153 template <typename T>
154 void makeDbusCall(const std::string& object, const std::string& interface,
155 const std::string& property, const std::variant<T>& data);
156
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600157 // path to the VPD file to edit
Alpana Kumari920408d2020-05-14 00:07:03 -0500158 inventory::Path vpdFilePath;
159
160 // inventory path of fru/module to update keyword
161 const inventory::Path objPath;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600162
163 // stream to perform operation on file
164 std::fstream vpdFileStream;
165
Alpana Kumari920408d2020-05-14 00:07:03 -0500166 // offset to get vpd data from EEPROM
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500167 uint32_t startOffset;
Alpana Kumari920408d2020-05-14 00:07:03 -0500168
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600169 // file to store parsed json
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500170 const nlohmann::json jsonFile;
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600171
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600172 // structure to hold info about record to edit
173 struct RecInfo
174 {
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500175 Binary kwdUpdatedData; // need access to it in case encoding is needed
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500176 const std::string recName;
177 const std::string recKWd;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600178 openpower::vpd::constants::RecordOffset recOffset;
179 openpower::vpd::constants::ECCOffset recECCoffset;
180 std::size_t recECCLength;
181 std::size_t kwdDataLength;
182 openpower::vpd::constants::RecordSize recSize;
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500183 openpower::vpd::constants::DataOffset kwDataOffset;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600184 // constructor
185 RecInfo(const std::string& rec, const std::string& kwd) :
186 recName(rec), recKWd(kwd), recOffset(0), recECCoffset(0),
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500187 recECCLength(0), kwdDataLength(0), recSize(0), kwDataOffset(0)
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600188 {
189 }
190 } thisRecord;
191
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500192 Binary vpdFile;
193
Alpana Kumari920408d2020-05-14 00:07:03 -0500194 // If requested Interface is common Interface
195 bool isCI;
196
197 /** @brief This API will be used to find out Parent FRU of Module/CPU
198 *
199 * @param[in] - moduleObjPath, object path of that FRU
200 * @param[in] - fruType, Type of Parent FRU
201 * for Module/CPU Parent Type- FruAndModule
202 *
203 * @return returns vpd file path of Parent Fru of that Module
204 */
205 std::string getSysPathForThisFruType(const std::string& moduleObjPath,
206 const std::string& fruType);
207
208 /** @brief This API will search for correct EEPROM path for asked CPU
209 * and will init vpdFilePath
210 */
211 void getVpdPathForCpu();
212
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600213}; // class EditorImpl
214
215} // namespace editor
216} // namespace manager
217} // namespace vpd
218} // namespace openpower