| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 1 | #pragma once | 
 | 2 |  | 
| SunnySrivastava1984 | 19be6d3 | 2020-03-03 07:21:45 -0600 | [diff] [blame] | 3 | #include "const.hpp" | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 4 | #include "store.hpp" | 
 | 5 |  | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 6 | #include <cstddef> | 
| girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 7 | #include <fstream> | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 8 |  | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 9 | namespace openpower | 
 | 10 | { | 
 | 11 | namespace vpd | 
 | 12 | { | 
 | 13 | namespace parser | 
 | 14 | { | 
| Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 15 | namespace keyword | 
 | 16 | { | 
| Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 17 | /** @brief Encoding scheme of a VPD keyword's data */ | 
 | 18 | enum class Encoding | 
 | 19 | { | 
 | 20 |     ASCII, /**< data encoded in ascii */ | 
 | 21 |     RAW,   /**< raw data */ | 
 | 22 |     // Keywords needing custom decoding | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 23 |     B1, /**< The keyword B1 needs to be decoded specially */ | 
| George Liu | ee79ca8 | 2019-07-12 11:05:33 +0800 | [diff] [blame] | 24 |     MB, /**< Special decoding of MB meant for Build Date */ | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 25 |     UD  /**< Special decoding of UD meant for UUID */ | 
| Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 26 | }; | 
 | 27 |  | 
 | 28 | } // namespace keyword | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 29 |  | 
| Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 30 | namespace internal | 
 | 31 | { | 
 | 32 |  | 
| Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 33 | using KeywordInfo = std::tuple<record::Keyword, keyword::Encoding>; | 
| Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 34 | using OffsetList = std::vector<uint32_t>; | 
| Deepak Kodihalli | 683bf72 | 2016-11-24 06:50:43 -0600 | [diff] [blame] | 35 | using KeywordMap = Parsed::mapped_type; | 
| Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 36 |  | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 37 | } // namespace internal | 
| Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 38 |  | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 39 | /** @class Impl | 
| Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 40 |  *  @brief Implements parser for VPD | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 41 |  * | 
| Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 42 |  *  An Impl object must be constructed by passing in VPD in | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 43 |  *  binary format. To parse the VPD, call the run() method. The run() | 
 | 44 |  *  method returns an openpower::vpd::Store object, which contains | 
 | 45 |  *  parsed VPD, and provides access methods for the VPD. | 
 | 46 |  * | 
| Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 47 |  *  Following is the algorithm used to parse IPZ/OpenPower VPD: | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 48 |  *  1) Validate that the first record is VHDR, the header record. | 
 | 49 |  *  2) From the VHDR record, get the offset of the VTOC record, | 
 | 50 |  *     which is the table of contents record. | 
 | 51 |  *  3) Process the VTOC record - note offsets of supported records. | 
 | 52 |  *  4) For each supported record : | 
 | 53 |  *  4.1) Jump to record via offset. Add record name to parser output. | 
 | 54 |  *  4.2) Process record - for each contained and supported keyword: | 
 | 55 |  *  4.2.1) Note keyword name and value, associate this information to | 
 | 56 |  *         to the record noted in step 4.1). | 
 | 57 |  */ | 
 | 58 | class Impl | 
 | 59 | { | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 60 |   public: | 
 | 61 |     Impl() = delete; | 
 | 62 |     Impl(const Impl&) = delete; | 
 | 63 |     Impl& operator=(const Impl&) = delete; | 
 | 64 |     Impl(Impl&&) = delete; | 
 | 65 |     Impl& operator=(Impl&&) = delete; | 
 | 66 |     ~Impl() = default; | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 67 |  | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 68 |     /** @brief Construct an Impl | 
 | 69 |      * | 
| Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 70 |      *  @param[in] vpdBuffer - Binary VPD | 
| Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 71 |      *  @param[in] path - To call out FRU in case of any PEL. | 
| girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 72 |      *  @param[in] vpdFilePath - VPD File Path | 
 | 73 |      *  @param[in] vpdStartOffset - Start offset of VPD. | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 74 |      */ | 
| girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 75 |     Impl(const Binary& vpdBuffer, const std::string& path, | 
 | 76 |          const std::string& vpdFilePath, uint32_t vpdStartOffset) : | 
 | 77 |         vpd(vpdBuffer), | 
 | 78 |         inventoryPath(path), vpdFilePath(vpdFilePath), | 
 | 79 |         vpdStartOffset(vpdStartOffset), out{} | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 80 |     { | 
| girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 81 |         try | 
 | 82 |         { | 
 | 83 |             vpdFileStream.open(vpdFilePath, | 
 | 84 |                                std::ios::in | std::ios::out | std::ios::binary); | 
 | 85 |         } | 
 | 86 |         catch (const std::fstream::failure& e) | 
 | 87 |         { | 
 | 88 |             std::cout << e.what(); | 
 | 89 |         } | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 90 |     } | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 91 |  | 
| Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 92 |     /** @brief Run the parser on binary VPD | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 93 |      * | 
 | 94 |      *  @returns openpower::vpd::Store object | 
 | 95 |      */ | 
 | 96 |     Store run(); | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 97 |  | 
| SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 98 |     /** @brief check if VPD header is valid | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 99 |      */ | 
| SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 100 |     void checkVPDHeader(); | 
 | 101 |  | 
| Priyanga Ramasamy | 28079c8 | 2021-10-07 16:30:51 -0500 | [diff] [blame] | 102 |     /** @brief Read a specific VPD keyword from hardware. | 
 | 103 |      * This api is to read a specific VPD keyword directly from hardware. | 
 | 104 |      * @param[in] record - record name. | 
 | 105 |      * @param[in] keyword - keyword name. | 
 | 106 |      * @return keyword value. | 
 | 107 |      */ | 
 | 108 |     std::string readKwFromHw(const std::string& record, | 
 | 109 |                              const std::string& keyword); | 
 | 110 |  | 
| SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 111 |   private: | 
 | 112 |     /** @brief Process the table of contents record | 
 | 113 |      * | 
 | 114 |      *  @param[in] iterator - iterator to buffer containing VPD | 
 | 115 |      *  @returns Size of the PT keyword in VTOC | 
 | 116 |      */ | 
| girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 117 |     std::size_t readTOC(Binary::const_iterator& iterator); | 
| Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 118 |  | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 119 |     /** @brief Read the PT keyword contained in the VHDR record, | 
 | 120 |      *         to obtain offsets to other records in the VPD. | 
 | 121 |      * | 
 | 122 |      *  @param[in] iterator - iterator to buffer containing VPD | 
 | 123 |      *  @param[in] ptLength - Length of PT keyword data | 
 | 124 |      * | 
 | 125 |      *  @returns List of offsets to records in VPD | 
 | 126 |      */ | 
 | 127 |     internal::OffsetList readPT(Binary::const_iterator iterator, | 
| girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 128 |                                 std::size_t ptLen); | 
| Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 129 |  | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 130 |     /** @brief Read VPD information contained within a record | 
 | 131 |      * | 
 | 132 |      *  @param[in] recordOffset - offset to a record location | 
| Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 133 |      *      within the binary VPD | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 134 |      */ | 
 | 135 |     void processRecord(std::size_t recordOffset); | 
| Deepak Kodihalli | a114346 | 2016-11-24 06:28:45 -0600 | [diff] [blame] | 136 |  | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 137 |     /** @brief Read keyword data | 
 | 138 |      * | 
| Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 139 |      *  @param[in] keyword - VPD keyword | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 140 |      *  @param[in] dataLength - Length of data to be read | 
 | 141 |      *  @param[in] iterator - iterator pointing to a Keyword's data in | 
 | 142 |      *      the VPD | 
 | 143 |      * | 
 | 144 |      *  @returns keyword data as a string | 
 | 145 |      */ | 
 | 146 |     std::string readKwData(const internal::KeywordInfo& keyword, | 
 | 147 |                            std::size_t dataLength, | 
 | 148 |                            Binary::const_iterator iterator); | 
| Deepak Kodihalli | a114346 | 2016-11-24 06:28:45 -0600 | [diff] [blame] | 149 |  | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 150 |     /** @brief While we're pointing at the keyword section of | 
 | 151 |      *     a record in the VPD, this will read all contained | 
 | 152 |      *     keywords and their values. | 
 | 153 |      * | 
 | 154 |      *  @param[in] iterator - iterator pointing to a Keyword in the VPD | 
 | 155 |      * | 
 | 156 |      *  @returns map of keyword:data | 
 | 157 |      */ | 
 | 158 |     internal::KeywordMap readKeywords(Binary::const_iterator iterator); | 
| Deepak Kodihalli | 683bf72 | 2016-11-24 06:50:43 -0600 | [diff] [blame] | 159 |  | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 160 |     /** @brief Checks if the VHDR record is present in the VPD */ | 
| girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 161 |     void checkHeader(); | 
| Deepak Kodihalli | 810c9de | 2016-11-22 11:42:51 -0600 | [diff] [blame] | 162 |  | 
| Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 163 |     /** @brief Checks the ECC for VHDR Record. | 
 | 164 |      *  @returns Success(0) OR corrupted data(-1) | 
 | 165 |      */ | 
| girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 166 |     int vhdrEccCheck(); | 
| Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 167 |  | 
 | 168 |     /** @brief Checks the ECC for VTOC Record. | 
 | 169 |      *  @returns Success(0) OR corrupted data(-1) | 
 | 170 |      */ | 
| girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 171 |     int vtocEccCheck(); | 
| Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 172 |  | 
 | 173 |     /** @brief Checks the ECC for the given record. | 
 | 174 |      * | 
 | 175 |      * @param[in] iterator - iterator pointing to a record in the VPD | 
 | 176 |      * @returns Success(0) OR corrupted data(-1) | 
 | 177 |      */ | 
| girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 178 |     int recordEccCheck(Binary::const_iterator iterator); | 
| Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 179 |  | 
 | 180 |     /** @brief This interface collects Offset of VTOC | 
 | 181 |      *  @returns VTOC Offset | 
 | 182 |      */ | 
| SunnySrivastava1984 | 19be6d3 | 2020-03-03 07:21:45 -0600 | [diff] [blame] | 183 |     openpower::vpd::constants::RecordOffset getVtocOffset() const; | 
| Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 184 |  | 
| Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 185 |     /** @brief VPD in binary format */ | 
| PriyangaRamasamy | 33c61c2 | 2021-04-06 11:15:57 -0500 | [diff] [blame] | 186 |     const Binary& vpd; | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 187 |  | 
| Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 188 |     /** Inventory path to call out FRU if required */ | 
 | 189 |     const std::string inventoryPath; | 
 | 190 |  | 
| girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 191 |     /** Eeprom hardware path */ | 
 | 192 |     inventory::Path vpdFilePath; | 
 | 193 |  | 
 | 194 |     /** VPD Offset **/ | 
 | 195 |     uint32_t vpdStartOffset; | 
 | 196 |  | 
 | 197 |     /** File stream for VPD */ | 
 | 198 |     std::fstream vpdFileStream; | 
 | 199 |  | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 200 |     /** @brief parser output */ | 
 | 201 |     Parsed out; | 
| Deepak Kodihalli | e08fcad | 2016-11-22 02:10:08 -0600 | [diff] [blame] | 202 | }; | 
 | 203 |  | 
 | 204 | } // namespace parser | 
 | 205 | } // namespace vpd | 
| Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 206 | } // namespace openpower |