SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "const.hpp" |
| 4 | #include "types.hpp" |
| 5 | |
| 6 | #include <cstddef> |
| 7 | #include <fstream> |
SunnySrivastava1984 | b421bfd | 2020-03-02 08:59:32 -0600 | [diff] [blame] | 8 | #include <nlohmann/json.hpp> |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 9 | #include <tuple> |
| 10 | |
| 11 | namespace openpower |
| 12 | { |
| 13 | namespace vpd |
| 14 | { |
| 15 | namespace manager |
| 16 | { |
| 17 | namespace 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 | */ |
| 42 | class 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 | */ |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame^] | 56 | EditorImpl(const std::string& record, const std::string& kwd, |
| 57 | Binary&& vpd) : |
| 58 | thisRecord(record, kwd), |
| 59 | vpdFile(std::move(vpd)) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | /** @brief Construct EditorImpl class |
| 64 | * |
| 65 | * @param[in] path - Path to the vpd file |
| 66 | */ |
SunnySrivastava1984 | b421bfd | 2020-03-02 08:59:32 -0600 | [diff] [blame] | 67 | EditorImpl(const inventory::Path& path, const nlohmann::json& json, |
| 68 | const std::string& record, const std::string& kwd) : |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 69 | vpdFilePath(path), |
SunnySrivastava1984 | b421bfd | 2020-03-02 08:59:32 -0600 | [diff] [blame] | 70 | jsonFile(json), thisRecord(record, kwd) |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 71 | { |
| 72 | } |
| 73 | |
| 74 | /** @brief Update data for keyword |
| 75 | * @param[in] kwdData - data to update |
| 76 | */ |
| 77 | void updateKeyword(const Binary& kwdData); |
| 78 | |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 79 | /** @brief Expands location code on DBUS |
| 80 | * @param[in] locationCodeType - "fcs" or "mts" |
| 81 | */ |
| 82 | void expandLocationCode(const std::string& locationCodeType); |
| 83 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 84 | private: |
| 85 | /** @brief read VTOC record from the vpd file |
| 86 | */ |
| 87 | void readVTOC(); |
| 88 | |
| 89 | /** @brief validate ecc data for the VTOC record |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 90 | * @param[in] iterator to VTOC record data |
| 91 | * @param[in] iterator to VTOC ECC data |
| 92 | * @param[in] Lenght of VTOC record |
| 93 | * @param[in] Length of ECC record |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 94 | */ |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 95 | void checkECC(Binary::const_iterator& itrToRecData, |
| 96 | Binary::const_iterator& itrToECCData, |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 97 | openpower::vpd::constants::RecordLength recLength, |
| 98 | openpower::vpd::constants::ECCLength eccLength); |
| 99 | |
| 100 | /** @brief reads value at the given offset |
| 101 | * @param[in] offset - offset value |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 102 | * @return value at that offset in bigendian |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 103 | */ |
| 104 | auto getValue(openpower::vpd::constants::offsets::Offsets offset); |
| 105 | |
| 106 | /** @brief Checks if required record name exist in the VPD file |
| 107 | * @param[in] iterator - pointing to start of PT kwd |
| 108 | * @param[in] ptLength - length of the PT kwd |
| 109 | */ |
| 110 | void checkPTForRecord(Binary::const_iterator& iterator, Byte ptLength); |
| 111 | |
| 112 | /** @brief Checks for reuired keyword in the record |
| 113 | */ |
| 114 | void checkRecordForKwd(); |
| 115 | |
| 116 | /** @brief update data for given keyword |
| 117 | * @param[in] kwdData- data to be updated |
| 118 | */ |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 119 | void updateData(const Binary& kwdData); |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 120 | |
| 121 | /** @brief update record ECC |
| 122 | */ |
| 123 | void updateRecordECC(); |
| 124 | |
SunnySrivastava1984 | b421bfd | 2020-03-02 08:59:32 -0600 | [diff] [blame] | 125 | /** @brief method to update cache once the data for kwd has been updated |
| 126 | */ |
| 127 | void updateCache(); |
| 128 | |
| 129 | /** @brief method to process and update CI in case required |
| 130 | * @param[in] - objectPath - path of the object to introspect |
| 131 | */ |
| 132 | void processAndUpdateCI(const std::string& objectPath); |
| 133 | |
| 134 | /** @brief method to process and update Extra Interface |
| 135 | * @param[in] Inventory - single inventory json subpart |
| 136 | * @param[in] objectPath - path of the object to introspect |
| 137 | */ |
| 138 | void processAndUpdateEI(const nlohmann::json& Inventory, |
| 139 | const inventory::Path& objPath); |
| 140 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 141 | /** @brief method to make busctl call |
| 142 | * |
| 143 | * @param[in] object - bus object path |
| 144 | * @param[in] interface - bus interface |
| 145 | * @param[in] property - property to update on BUS |
| 146 | * @param[in] data - data to be updayed on Bus |
| 147 | * |
| 148 | */ |
| 149 | template <typename T> |
| 150 | void makeDbusCall(const std::string& object, const std::string& interface, |
| 151 | const std::string& property, const std::variant<T>& data); |
| 152 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 153 | // path to the VPD file to edit |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame^] | 154 | const inventory::Path vpdFilePath; |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 155 | |
| 156 | // stream to perform operation on file |
| 157 | std::fstream vpdFileStream; |
| 158 | |
SunnySrivastava1984 | b421bfd | 2020-03-02 08:59:32 -0600 | [diff] [blame] | 159 | // file to store parsed json |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame^] | 160 | const nlohmann::json jsonFile; |
SunnySrivastava1984 | b421bfd | 2020-03-02 08:59:32 -0600 | [diff] [blame] | 161 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 162 | // structure to hold info about record to edit |
| 163 | struct RecInfo |
| 164 | { |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 165 | Binary kwdUpdatedData; // need access to it in case encoding is needed |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame^] | 166 | const std::string recName; |
| 167 | const std::string recKWd; |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 168 | openpower::vpd::constants::RecordOffset recOffset; |
| 169 | openpower::vpd::constants::ECCOffset recECCoffset; |
| 170 | std::size_t recECCLength; |
| 171 | std::size_t kwdDataLength; |
| 172 | openpower::vpd::constants::RecordSize recSize; |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 173 | openpower::vpd::constants::DataOffset kwDataOffset; |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 174 | // constructor |
| 175 | RecInfo(const std::string& rec, const std::string& kwd) : |
| 176 | recName(rec), recKWd(kwd), recOffset(0), recECCoffset(0), |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 177 | recECCLength(0), kwdDataLength(0), recSize(0), kwDataOffset(0) |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 178 | { |
| 179 | } |
| 180 | } thisRecord; |
| 181 | |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 182 | Binary vpdFile; |
| 183 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 184 | }; // class EditorImpl |
| 185 | |
| 186 | } // namespace editor |
| 187 | } // namespace manager |
| 188 | } // namespace vpd |
| 189 | } // namespace openpower |