blob: b1aa060b336074430d0ebfd10de8a75fa984bdbb [file] [log] [blame]
Deepak Kodihallie08fcad2016-11-22 02:10:08 -06001#pragma once
2
SunnySrivastava198419be6d32020-03-03 07:21:45 -06003#include "const.hpp"
Deepak Kodihallie08fcad2016-11-22 02:10:08 -06004#include "store.hpp"
5
Patrick Venturec83c4dc2018-11-01 16:29:18 -07006#include <cstddef>
girik18bb9852022-11-16 05:48:13 -06007#include <fstream>
Patrick Venturec83c4dc2018-11-01 16:29:18 -07008
Deepak Kodihallie08fcad2016-11-22 02:10:08 -06009namespace openpower
10{
11namespace vpd
12{
13namespace parser
14{
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060015namespace keyword
16{
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060017/** @brief Encoding scheme of a VPD keyword's data */
18enum class Encoding
19{
20 ASCII, /**< data encoded in ascii */
21 RAW, /**< raw data */
22 // Keywords needing custom decoding
Patrick Venturec83c4dc2018-11-01 16:29:18 -070023 B1, /**< The keyword B1 needs to be decoded specially */
George Liuee79ca82019-07-12 11:05:33 +080024 MB, /**< Special decoding of MB meant for Build Date */
Patrick Venturec83c4dc2018-11-01 16:29:18 -070025 UD /**< Special decoding of UD meant for UUID */
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060026};
27
28} // namespace keyword
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060029
Deepak Kodihalli023112f2016-11-22 22:02:14 -060030namespace internal
31{
32
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060033using KeywordInfo = std::tuple<record::Keyword, keyword::Encoding>;
Deepak Kodihalli023112f2016-11-22 22:02:14 -060034using OffsetList = std::vector<uint32_t>;
Deepak Kodihalli683bf722016-11-24 06:50:43 -060035using KeywordMap = Parsed::mapped_type;
Deepak Kodihalli023112f2016-11-22 22:02:14 -060036
Patrick Venturec83c4dc2018-11-01 16:29:18 -070037} // namespace internal
Deepak Kodihalli023112f2016-11-22 22:02:14 -060038
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060039/** @class Impl
Alpana Kumari26a74af2019-09-10 23:53:58 -050040 * @brief Implements parser for VPD
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060041 *
Alpana Kumari26a74af2019-09-10 23:53:58 -050042 * An Impl object must be constructed by passing in VPD in
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060043 * 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 Kumari26a74af2019-09-10 23:53:58 -050047 * Following is the algorithm used to parse IPZ/OpenPower VPD:
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060048 * 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 */
58class Impl
59{
Patrick Venturec83c4dc2018-11-01 16:29:18 -070060 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 Kodihallie08fcad2016-11-22 02:10:08 -060067
Patrick Venturec83c4dc2018-11-01 16:29:18 -070068 /** @brief Construct an Impl
69 *
Alpana Kumari26a74af2019-09-10 23:53:58 -050070 * @param[in] vpdBuffer - Binary VPD
Sunny Srivastavaf31a91b2022-06-09 08:11:29 -050071 * @param[in] path - To call out FRU in case of any PEL.
girik18bb9852022-11-16 05:48:13 -060072 * @param[in] vpdFilePath - VPD File Path
73 * @param[in] vpdStartOffset - Start offset of VPD.
Patrick Venturec83c4dc2018-11-01 16:29:18 -070074 */
girik18bb9852022-11-16 05:48:13 -060075 Impl(const Binary& vpdBuffer, const std::string& path,
76 const std::string& vpdFilePath, uint32_t vpdStartOffset) :
Patrick Williams08dc31c2024-08-16 15:21:06 -040077 vpd(vpdBuffer), inventoryPath(path), vpdFilePath(vpdFilePath),
girik18bb9852022-11-16 05:48:13 -060078 vpdStartOffset(vpdStartOffset), out{}
Patrick Venturec83c4dc2018-11-01 16:29:18 -070079 {
jinuthomas45d54972023-07-03 04:36:29 -050080#ifndef ManagerTest
Patrick Williams08dc31c2024-08-16 15:21:06 -040081 vpdFileStream.exceptions(
82 std::ifstream::badbit | std::ifstream::failbit);
jinuthomas45d54972023-07-03 04:36:29 -050083#endif
girik18bb9852022-11-16 05:48:13 -060084 try
85 {
86 vpdFileStream.open(vpdFilePath,
87 std::ios::in | std::ios::out | std::ios::binary);
88 }
jinuthomas45d54972023-07-03 04:36:29 -050089 catch (const std::fstream::failure& fail)
girik18bb9852022-11-16 05:48:13 -060090 {
jinuthomas45d54972023-07-03 04:36:29 -050091 std::cerr << "Exception in file handling [" << vpdFilePath
92 << "] error : " << fail.what();
93 throw;
girik18bb9852022-11-16 05:48:13 -060094 }
Patrick Venturec83c4dc2018-11-01 16:29:18 -070095 }
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060096
Alpana Kumari26a74af2019-09-10 23:53:58 -050097 /** @brief Run the parser on binary VPD
Patrick Venturec83c4dc2018-11-01 16:29:18 -070098 *
99 * @returns openpower::vpd::Store object
100 */
101 Store run();
Deepak Kodihallie08fcad2016-11-22 02:10:08 -0600102
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600103 /** @brief check if VPD header is valid
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700104 */
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600105 void checkVPDHeader();
106
Priyanga Ramasamy28079c82021-10-07 16:30:51 -0500107 /** @brief Read a specific VPD keyword from hardware.
108 * This api is to read a specific VPD keyword directly from hardware.
109 * @param[in] record - record name.
110 * @param[in] keyword - keyword name.
111 * @return keyword value.
112 */
113 std::string readKwFromHw(const std::string& record,
114 const std::string& keyword);
115
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600116 private:
117 /** @brief Process the table of contents record
118 *
119 * @param[in] iterator - iterator to buffer containing VPD
120 * @returns Size of the PT keyword in VTOC
121 */
girik18bb9852022-11-16 05:48:13 -0600122 std::size_t readTOC(Binary::const_iterator& iterator);
Deepak Kodihalli023112f2016-11-22 22:02:14 -0600123
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700124 /** @brief Read the PT keyword contained in the VHDR record,
125 * to obtain offsets to other records in the VPD.
126 *
127 * @param[in] iterator - iterator to buffer containing VPD
128 * @param[in] ptLength - Length of PT keyword data
129 *
130 * @returns List of offsets to records in VPD
131 */
132 internal::OffsetList readPT(Binary::const_iterator iterator,
girik18bb9852022-11-16 05:48:13 -0600133 std::size_t ptLen);
Deepak Kodihalli023112f2016-11-22 22:02:14 -0600134
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700135 /** @brief Read VPD information contained within a record
136 *
137 * @param[in] recordOffset - offset to a record location
Alpana Kumari26a74af2019-09-10 23:53:58 -0500138 * within the binary VPD
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700139 */
140 void processRecord(std::size_t recordOffset);
Deepak Kodihallia1143462016-11-24 06:28:45 -0600141
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700142 /** @brief Read keyword data
143 *
Alpana Kumari26a74af2019-09-10 23:53:58 -0500144 * @param[in] keyword - VPD keyword
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700145 * @param[in] dataLength - Length of data to be read
146 * @param[in] iterator - iterator pointing to a Keyword's data in
147 * the VPD
148 *
149 * @returns keyword data as a string
150 */
151 std::string readKwData(const internal::KeywordInfo& keyword,
152 std::size_t dataLength,
153 Binary::const_iterator iterator);
Deepak Kodihallia1143462016-11-24 06:28:45 -0600154
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700155 /** @brief While we're pointing at the keyword section of
156 * a record in the VPD, this will read all contained
157 * keywords and their values.
158 *
159 * @param[in] iterator - iterator pointing to a Keyword in the VPD
160 *
161 * @returns map of keyword:data
162 */
163 internal::KeywordMap readKeywords(Binary::const_iterator iterator);
Deepak Kodihalli683bf722016-11-24 06:50:43 -0600164
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700165 /** @brief Checks if the VHDR record is present in the VPD */
girik18bb9852022-11-16 05:48:13 -0600166 void checkHeader();
Deepak Kodihalli810c9de2016-11-22 11:42:51 -0600167
Alpana Kumaric0aeac32019-11-28 05:20:10 -0600168 /** @brief Checks the ECC for VHDR Record.
169 * @returns Success(0) OR corrupted data(-1)
170 */
girik18bb9852022-11-16 05:48:13 -0600171 int vhdrEccCheck();
Alpana Kumaric0aeac32019-11-28 05:20:10 -0600172
173 /** @brief Checks the ECC for VTOC Record.
174 * @returns Success(0) OR corrupted data(-1)
175 */
girik18bb9852022-11-16 05:48:13 -0600176 int vtocEccCheck();
Alpana Kumaric0aeac32019-11-28 05:20:10 -0600177
178 /** @brief Checks the ECC for the given record.
179 *
180 * @param[in] iterator - iterator pointing to a record in the VPD
181 * @returns Success(0) OR corrupted data(-1)
182 */
girik18bb9852022-11-16 05:48:13 -0600183 int recordEccCheck(Binary::const_iterator iterator);
Alpana Kumaric0aeac32019-11-28 05:20:10 -0600184
185 /** @brief This interface collects Offset of VTOC
186 * @returns VTOC Offset
187 */
SunnySrivastava198419be6d32020-03-03 07:21:45 -0600188 openpower::vpd::constants::RecordOffset getVtocOffset() const;
Alpana Kumaric0aeac32019-11-28 05:20:10 -0600189
Alpana Kumari26a74af2019-09-10 23:53:58 -0500190 /** @brief VPD in binary format */
PriyangaRamasamy33c61c22021-04-06 11:15:57 -0500191 const Binary& vpd;
Deepak Kodihallie08fcad2016-11-22 02:10:08 -0600192
Sunny Srivastavaf31a91b2022-06-09 08:11:29 -0500193 /** Inventory path to call out FRU if required */
194 const std::string inventoryPath;
195
girik18bb9852022-11-16 05:48:13 -0600196 /** Eeprom hardware path */
197 inventory::Path vpdFilePath;
198
199 /** VPD Offset **/
200 uint32_t vpdStartOffset;
201
202 /** File stream for VPD */
203 std::fstream vpdFileStream;
204
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700205 /** @brief parser output */
206 Parsed out;
Deepak Kodihallie08fcad2016-11-22 02:10:08 -0600207};
208
209} // namespace parser
210} // namespace vpd
Patrick Williamsc78d8872023-05-10 07:50:56 -0500211} // namespace openpower