blob: 8d3b59edc3d2aa69c4abe6ba560d21e793e74c1f [file] [log] [blame]
Deepak Kodihallie08fcad2016-11-22 02:10:08 -06001#pragma once
2
3#include "store.hpp"
4
Patrick Venturec83c4dc2018-11-01 16:29:18 -07005#include <cstddef>
6
Deepak Kodihallie08fcad2016-11-22 02:10:08 -06007namespace openpower
8{
9namespace vpd
10{
11namespace parser
12{
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060013namespace keyword
14{
15
16/** @brief Encoding scheme of a VPD keyword's data */
17enum class Encoding
18{
19 ASCII, /**< data encoded in ascii */
20 RAW, /**< raw data */
21 // Keywords needing custom decoding
Patrick Venturec83c4dc2018-11-01 16:29:18 -070022 B1, /**< The keyword B1 needs to be decoded specially */
George Liuee79ca82019-07-12 11:05:33 +080023 MB, /**< Special decoding of MB meant for Build Date */
Patrick Venturec83c4dc2018-11-01 16:29:18 -070024 UD /**< Special decoding of UD meant for UUID */
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060025};
26
27} // namespace keyword
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060028
Deepak Kodihalli023112f2016-11-22 22:02:14 -060029namespace internal
30{
31
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060032using KeywordInfo = std::tuple<record::Keyword, keyword::Encoding>;
Deepak Kodihalli023112f2016-11-22 22:02:14 -060033using OffsetList = std::vector<uint32_t>;
Deepak Kodihalli683bf722016-11-24 06:50:43 -060034using KeywordMap = Parsed::mapped_type;
Deepak Kodihalli023112f2016-11-22 22:02:14 -060035
Patrick Venturec83c4dc2018-11-01 16:29:18 -070036} // namespace internal
Deepak Kodihalli023112f2016-11-22 22:02:14 -060037
Alpana Kumaric0aeac32019-11-28 05:20:10 -060038namespace
39{
40
41using RecordId = uint8_t;
42using RecordOffset = uint16_t;
43using RecordSize = uint16_t;
44using RecordType = uint16_t;
45using RecordLength = uint16_t;
46using KwSize = uint8_t;
47using PoundKwSize = uint16_t;
48using ECCOffset = uint16_t;
49using ECCLength = uint16_t;
50using LE2ByteData = uint16_t;
51
52} // namespace
53
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060054/** @class Impl
Alpana Kumari26a74af2019-09-10 23:53:58 -050055 * @brief Implements parser for VPD
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060056 *
Alpana Kumari26a74af2019-09-10 23:53:58 -050057 * An Impl object must be constructed by passing in VPD in
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060058 * binary format. To parse the VPD, call the run() method. The run()
59 * method returns an openpower::vpd::Store object, which contains
60 * parsed VPD, and provides access methods for the VPD.
61 *
Alpana Kumari26a74af2019-09-10 23:53:58 -050062 * Following is the algorithm used to parse IPZ/OpenPower VPD:
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060063 * 1) Validate that the first record is VHDR, the header record.
64 * 2) From the VHDR record, get the offset of the VTOC record,
65 * which is the table of contents record.
66 * 3) Process the VTOC record - note offsets of supported records.
67 * 4) For each supported record :
68 * 4.1) Jump to record via offset. Add record name to parser output.
69 * 4.2) Process record - for each contained and supported keyword:
70 * 4.2.1) Note keyword name and value, associate this information to
71 * to the record noted in step 4.1).
72 */
73class Impl
74{
Patrick Venturec83c4dc2018-11-01 16:29:18 -070075 public:
76 Impl() = delete;
77 Impl(const Impl&) = delete;
78 Impl& operator=(const Impl&) = delete;
79 Impl(Impl&&) = delete;
80 Impl& operator=(Impl&&) = delete;
81 ~Impl() = default;
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060082
Patrick Venturec83c4dc2018-11-01 16:29:18 -070083 /** @brief Construct an Impl
84 *
Alpana Kumari26a74af2019-09-10 23:53:58 -050085 * @param[in] vpdBuffer - Binary VPD
Patrick Venturec83c4dc2018-11-01 16:29:18 -070086 */
87 explicit Impl(Binary&& vpdBuffer) : vpd(std::move(vpdBuffer)), out{}
88 {
89 }
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060090
Alpana Kumari26a74af2019-09-10 23:53:58 -050091 /** @brief Run the parser on binary VPD
Patrick Venturec83c4dc2018-11-01 16:29:18 -070092 *
93 * @returns openpower::vpd::Store object
94 */
95 Store run();
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060096
Patrick Venturec83c4dc2018-11-01 16:29:18 -070097 private:
98 /** @brief Process the table of contents record, VHDR
99 *
100 * @returns List of offsets to records in VPD
101 */
102 internal::OffsetList readTOC() const;
Deepak Kodihalli023112f2016-11-22 22:02:14 -0600103
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700104 /** @brief Read the PT keyword contained in the VHDR record,
105 * to obtain offsets to other records in the VPD.
106 *
107 * @param[in] iterator - iterator to buffer containing VPD
108 * @param[in] ptLength - Length of PT keyword data
109 *
110 * @returns List of offsets to records in VPD
111 */
112 internal::OffsetList readPT(Binary::const_iterator iterator,
113 std::size_t ptLen) const;
Deepak Kodihalli023112f2016-11-22 22:02:14 -0600114
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700115 /** @brief Read VPD information contained within a record
116 *
117 * @param[in] recordOffset - offset to a record location
Alpana Kumari26a74af2019-09-10 23:53:58 -0500118 * within the binary VPD
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700119 */
120 void processRecord(std::size_t recordOffset);
Deepak Kodihallia1143462016-11-24 06:28:45 -0600121
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700122 /** @brief Read keyword data
123 *
Alpana Kumari26a74af2019-09-10 23:53:58 -0500124 * @param[in] keyword - VPD keyword
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700125 * @param[in] dataLength - Length of data to be read
126 * @param[in] iterator - iterator pointing to a Keyword's data in
127 * the VPD
128 *
129 * @returns keyword data as a string
130 */
131 std::string readKwData(const internal::KeywordInfo& keyword,
132 std::size_t dataLength,
133 Binary::const_iterator iterator);
Deepak Kodihallia1143462016-11-24 06:28:45 -0600134
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700135 /** @brief While we're pointing at the keyword section of
136 * a record in the VPD, this will read all contained
137 * keywords and their values.
138 *
139 * @param[in] iterator - iterator pointing to a Keyword in the VPD
140 *
141 * @returns map of keyword:data
142 */
143 internal::KeywordMap readKeywords(Binary::const_iterator iterator);
Deepak Kodihalli683bf722016-11-24 06:50:43 -0600144
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700145 /** @brief Checks if the VHDR record is present in the VPD */
146 void checkHeader() const;
Deepak Kodihalli810c9de2016-11-22 11:42:51 -0600147
Alpana Kumaric0aeac32019-11-28 05:20:10 -0600148 /** @brief Checks the ECC for VHDR Record.
149 * @returns Success(0) OR corrupted data(-1)
150 */
151 int vhdrEccCheck() const;
152
153 /** @brief Checks the ECC for VTOC Record.
154 * @returns Success(0) OR corrupted data(-1)
155 */
156 int vtocEccCheck() const;
157
158 /** @brief Checks the ECC for the given record.
159 *
160 * @param[in] iterator - iterator pointing to a record in the VPD
161 * @returns Success(0) OR corrupted data(-1)
162 */
163 int recordEccCheck(Binary::const_iterator iterator) const;
164
165 /** @brief This interface collects Offset of VTOC
166 * @returns VTOC Offset
167 */
168 RecordOffset getVtocOffset() const;
169
Alpana Kumari26a74af2019-09-10 23:53:58 -0500170 /** @brief VPD in binary format */
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700171 Binary vpd;
Deepak Kodihallie08fcad2016-11-22 02:10:08 -0600172
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700173 /** @brief parser output */
174 Parsed out;
Deepak Kodihallie08fcad2016-11-22 02:10:08 -0600175};
176
177} // namespace parser
178} // namespace vpd
179} // namespace openpower