blob: 70596a44381d4f0558b707f267249d9fc40e3d82 [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>
8#include <tuple>
9
10namespace openpower
11{
12namespace vpd
13{
14namespace manager
15{
16namespace editor
17{
18
19/** @class Editor
20 * @brief Implements VPD editing related functinality, currently
21 * implemented to support only keyword data update functionality.
22 *
23 * An Editor object must be constructed by passing in VPD in
24 * binary format. To edit the keyword data, call the updateKeyword() method.
25 * The method looks for the record name to update in VTOC and
26 * then looks for the keyword name in that record.
27 * when found it updates the data of keyword with the given data.
28 * It does not block keyword data update in case the length of new data is
29 * greater than or less than the current data length.
30 * If the new data length is more than the length alotted to that keyword
31 * the new data will be truncated to update only the allotted length.
32 * Similarly if the new data length is less then only that much data will
33 * be updated for the keyword and remaining bits will be left unchanged.
34 *
35 * Following is the algorithm used to update keyword:
36 * 1) Look for the record name in the given VPD file
37 * 2) Look for the keyword name for which data needs to be updated
38 * which is the table of contents record.
39 * 3) update the data for that keyword with the new data
40 */
41class EditorImpl
42{
43 public:
44 EditorImpl() = delete;
45 EditorImpl(const EditorImpl&) = delete;
46 EditorImpl& operator=(const EditorImpl&) = delete;
47 EditorImpl(EditorImpl&&) = delete;
48 EditorImpl& operator=(EditorImpl&&) = delete;
49 ~EditorImpl() = default;
50
51 /** @brief Construct EditorImpl class
52 *
53 * @param[in] path - Path to the vpd file
54 */
55 EditorImpl(const inventory::Path& path, const std::string& record,
56 const std::string& kwd) :
57 vpdFilePath(path),
58 thisRecord(record, kwd)
59 {
60 }
61
62 /** @brief Update data for keyword
63 * @param[in] kwdData - data to update
64 */
65 void updateKeyword(const Binary& kwdData);
66
67 private:
68 /** @brief read VTOC record from the vpd file
69 */
70 void readVTOC();
71
72 /** @brief validate ecc data for the VTOC record
SunnySrivastava19846d8314d2020-05-15 09:34:58 -050073 * @param[in] iterator to VTOC record data
74 * @param[in] iterator to VTOC ECC data
75 * @param[in] Lenght of VTOC record
76 * @param[in] Length of ECC record
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060077 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -050078 void checkECC(Binary::const_iterator& itrToRecData,
79 Binary::const_iterator& itrToECCData,
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060080 openpower::vpd::constants::RecordLength recLength,
81 openpower::vpd::constants::ECCLength eccLength);
82
83 /** @brief reads value at the given offset
84 * @param[in] offset - offset value
SunnySrivastava19846d8314d2020-05-15 09:34:58 -050085 * @return value at that offset in bigendian
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060086 */
87 auto getValue(openpower::vpd::constants::offsets::Offsets offset);
88
89 /** @brief Checks if required record name exist in the VPD file
90 * @param[in] iterator - pointing to start of PT kwd
91 * @param[in] ptLength - length of the PT kwd
92 */
93 void checkPTForRecord(Binary::const_iterator& iterator, Byte ptLength);
94
95 /** @brief Checks for reuired keyword in the record
96 */
97 void checkRecordForKwd();
98
99 /** @brief update data for given keyword
100 * @param[in] kwdData- data to be updated
101 */
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500102 void updateData(const Binary& kwdData);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600103
104 /** @brief update record ECC
105 */
106 void updateRecordECC();
107
108 // path to the VPD file to edit
109 const inventory::Path& vpdFilePath;
110
111 // stream to perform operation on file
112 std::fstream vpdFileStream;
113
114 // structure to hold info about record to edit
115 struct RecInfo
116 {
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500117 Binary kwdUpdatedData; // need access to it in case encoding is needed
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600118 const std::string& recName;
119 const std::string& recKWd;
120 openpower::vpd::constants::RecordOffset recOffset;
121 openpower::vpd::constants::ECCOffset recECCoffset;
122 std::size_t recECCLength;
123 std::size_t kwdDataLength;
124 openpower::vpd::constants::RecordSize recSize;
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500125 openpower::vpd::constants::DataOffset kwDataOffset;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600126 // constructor
127 RecInfo(const std::string& rec, const std::string& kwd) :
128 recName(rec), recKWd(kwd), recOffset(0), recECCoffset(0),
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500129 recECCLength(0), kwdDataLength(0), recSize(0), kwDataOffset(0)
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600130 {
131 }
132 } thisRecord;
133
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500134 Binary vpdFile;
135
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600136}; // class EditorImpl
137
138} // namespace editor
139} // namespace manager
140} // namespace vpd
141} // namespace openpower