blob: ff94692ef734e45555ca1d5f840ca247d0d247a9 [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;
50 ~EditorImpl() = default;
51
52 /** @brief Construct EditorImpl class
53 *
54 * @param[in] path - Path to the vpd file
55 */
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -060056 EditorImpl(const inventory::Path& path, const nlohmann::json& json,
57 const std::string& record, const std::string& kwd) :
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060058 vpdFilePath(path),
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -060059 jsonFile(json), thisRecord(record, kwd)
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060060 {
61 }
62
63 /** @brief Update data for keyword
64 * @param[in] kwdData - data to update
65 */
66 void updateKeyword(const Binary& kwdData);
67
68 private:
69 /** @brief read VTOC record from the vpd file
70 */
71 void readVTOC();
72
73 /** @brief validate ecc data for the VTOC record
SunnySrivastava19846d8314d2020-05-15 09:34:58 -050074 * @param[in] iterator to VTOC record data
75 * @param[in] iterator to VTOC ECC data
76 * @param[in] Lenght of VTOC record
77 * @param[in] Length of ECC record
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060078 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -050079 void checkECC(Binary::const_iterator& itrToRecData,
80 Binary::const_iterator& itrToECCData,
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060081 openpower::vpd::constants::RecordLength recLength,
82 openpower::vpd::constants::ECCLength eccLength);
83
84 /** @brief reads value at the given offset
85 * @param[in] offset - offset value
SunnySrivastava19846d8314d2020-05-15 09:34:58 -050086 * @return value at that offset in bigendian
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060087 */
88 auto getValue(openpower::vpd::constants::offsets::Offsets offset);
89
90 /** @brief Checks if required record name exist in the VPD file
91 * @param[in] iterator - pointing to start of PT kwd
92 * @param[in] ptLength - length of the PT kwd
93 */
94 void checkPTForRecord(Binary::const_iterator& iterator, Byte ptLength);
95
96 /** @brief Checks for reuired keyword in the record
97 */
98 void checkRecordForKwd();
99
100 /** @brief update data for given keyword
101 * @param[in] kwdData- data to be updated
102 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500103 void updateData(const Binary& kwdData);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600104
105 /** @brief update record ECC
106 */
107 void updateRecordECC();
108
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600109 /** @brief method to update cache once the data for kwd has been updated
110 */
111 void updateCache();
112
113 /** @brief method to process and update CI in case required
114 * @param[in] - objectPath - path of the object to introspect
115 */
116 void processAndUpdateCI(const std::string& objectPath);
117
118 /** @brief method to process and update Extra Interface
119 * @param[in] Inventory - single inventory json subpart
120 * @param[in] objectPath - path of the object to introspect
121 */
122 void processAndUpdateEI(const nlohmann::json& Inventory,
123 const inventory::Path& objPath);
124
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600125 // path to the VPD file to edit
126 const inventory::Path& vpdFilePath;
127
128 // stream to perform operation on file
129 std::fstream vpdFileStream;
130
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600131 // file to store parsed json
132 const nlohmann::json& jsonFile;
133
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600134 // structure to hold info about record to edit
135 struct RecInfo
136 {
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500137 Binary kwdUpdatedData; // need access to it in case encoding is needed
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600138 const std::string& recName;
139 const std::string& recKWd;
140 openpower::vpd::constants::RecordOffset recOffset;
141 openpower::vpd::constants::ECCOffset recECCoffset;
142 std::size_t recECCLength;
143 std::size_t kwdDataLength;
144 openpower::vpd::constants::RecordSize recSize;
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500145 openpower::vpd::constants::DataOffset kwDataOffset;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600146 // constructor
147 RecInfo(const std::string& rec, const std::string& kwd) :
148 recName(rec), recKWd(kwd), recOffset(0), recECCoffset(0),
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500149 recECCLength(0), kwdDataLength(0), recSize(0), kwDataOffset(0)
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600150 {
151 }
152 } thisRecord;
153
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500154 Binary vpdFile;
155
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600156}; // class EditorImpl
157
158} // namespace editor
159} // namespace manager
160} // namespace vpd
161} // namespace openpower