blob: 75f545286864f87c9e49ce333917d95c59d2de94 [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>
7
Deepak Kodihallie08fcad2016-11-22 02:10:08 -06008namespace openpower
9{
10namespace vpd
11{
12namespace parser
13{
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060014namespace keyword
15{
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060016/** @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
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060038/** @class Impl
Alpana Kumari26a74af2019-09-10 23:53:58 -050039 * @brief Implements parser for VPD
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060040 *
Alpana Kumari26a74af2019-09-10 23:53:58 -050041 * An Impl object must be constructed by passing in VPD in
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060042 * binary format. To parse the VPD, call the run() method. The run()
43 * method returns an openpower::vpd::Store object, which contains
44 * parsed VPD, and provides access methods for the VPD.
45 *
Alpana Kumari26a74af2019-09-10 23:53:58 -050046 * Following is the algorithm used to parse IPZ/OpenPower VPD:
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060047 * 1) Validate that the first record is VHDR, the header record.
48 * 2) From the VHDR record, get the offset of the VTOC record,
49 * which is the table of contents record.
50 * 3) Process the VTOC record - note offsets of supported records.
51 * 4) For each supported record :
52 * 4.1) Jump to record via offset. Add record name to parser output.
53 * 4.2) Process record - for each contained and supported keyword:
54 * 4.2.1) Note keyword name and value, associate this information to
55 * to the record noted in step 4.1).
56 */
57class Impl
58{
Patrick Venturec83c4dc2018-11-01 16:29:18 -070059 public:
60 Impl() = delete;
61 Impl(const Impl&) = delete;
62 Impl& operator=(const Impl&) = delete;
63 Impl(Impl&&) = delete;
64 Impl& operator=(Impl&&) = delete;
65 ~Impl() = default;
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060066
Patrick Venturec83c4dc2018-11-01 16:29:18 -070067 /** @brief Construct an Impl
68 *
Alpana Kumari26a74af2019-09-10 23:53:58 -050069 * @param[in] vpdBuffer - Binary VPD
Patrick Venturec83c4dc2018-11-01 16:29:18 -070070 */
PriyangaRamasamy33c61c22021-04-06 11:15:57 -050071 explicit Impl(const Binary& vpdBuffer) : vpd(vpdBuffer), out{}
Patrick Venturec83c4dc2018-11-01 16:29:18 -070072 {
73 }
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060074
Alpana Kumari26a74af2019-09-10 23:53:58 -050075 /** @brief Run the parser on binary VPD
Patrick Venturec83c4dc2018-11-01 16:29:18 -070076 *
77 * @returns openpower::vpd::Store object
78 */
79 Store run();
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060080
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060081 /** @brief check if VPD header is valid
Patrick Venturec83c4dc2018-11-01 16:29:18 -070082 */
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060083 void checkVPDHeader();
84
Priyanga Ramasamy28079c82021-10-07 16:30:51 -050085 /** @brief Read a specific VPD keyword from hardware.
86 * This api is to read a specific VPD keyword directly from hardware.
87 * @param[in] record - record name.
88 * @param[in] keyword - keyword name.
89 * @return keyword value.
90 */
91 std::string readKwFromHw(const std::string& record,
92 const std::string& keyword);
93
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060094 private:
95 /** @brief Process the table of contents record
96 *
97 * @param[in] iterator - iterator to buffer containing VPD
98 * @returns Size of the PT keyword in VTOC
99 */
100 std::size_t readTOC(Binary::const_iterator& iterator) const;
Deepak Kodihalli023112f2016-11-22 22:02:14 -0600101
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700102 /** @brief Read the PT keyword contained in the VHDR record,
103 * to obtain offsets to other records in the VPD.
104 *
105 * @param[in] iterator - iterator to buffer containing VPD
106 * @param[in] ptLength - Length of PT keyword data
107 *
108 * @returns List of offsets to records in VPD
109 */
110 internal::OffsetList readPT(Binary::const_iterator iterator,
111 std::size_t ptLen) const;
Deepak Kodihalli023112f2016-11-22 22:02:14 -0600112
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700113 /** @brief Read VPD information contained within a record
114 *
115 * @param[in] recordOffset - offset to a record location
Alpana Kumari26a74af2019-09-10 23:53:58 -0500116 * within the binary VPD
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700117 */
118 void processRecord(std::size_t recordOffset);
Deepak Kodihallia1143462016-11-24 06:28:45 -0600119
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700120 /** @brief Read keyword data
121 *
Alpana Kumari26a74af2019-09-10 23:53:58 -0500122 * @param[in] keyword - VPD keyword
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700123 * @param[in] dataLength - Length of data to be read
124 * @param[in] iterator - iterator pointing to a Keyword's data in
125 * the VPD
126 *
127 * @returns keyword data as a string
128 */
129 std::string readKwData(const internal::KeywordInfo& keyword,
130 std::size_t dataLength,
131 Binary::const_iterator iterator);
Deepak Kodihallia1143462016-11-24 06:28:45 -0600132
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700133 /** @brief While we're pointing at the keyword section of
134 * a record in the VPD, this will read all contained
135 * keywords and their values.
136 *
137 * @param[in] iterator - iterator pointing to a Keyword in the VPD
138 *
139 * @returns map of keyword:data
140 */
141 internal::KeywordMap readKeywords(Binary::const_iterator iterator);
Deepak Kodihalli683bf722016-11-24 06:50:43 -0600142
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700143 /** @brief Checks if the VHDR record is present in the VPD */
144 void checkHeader() const;
Deepak Kodihalli810c9de2016-11-22 11:42:51 -0600145
Alpana Kumaric0aeac32019-11-28 05:20:10 -0600146 /** @brief Checks the ECC for VHDR Record.
147 * @returns Success(0) OR corrupted data(-1)
148 */
149 int vhdrEccCheck() const;
150
151 /** @brief Checks the ECC for VTOC Record.
152 * @returns Success(0) OR corrupted data(-1)
153 */
154 int vtocEccCheck() const;
155
156 /** @brief Checks the ECC for the given record.
157 *
158 * @param[in] iterator - iterator pointing to a record in the VPD
159 * @returns Success(0) OR corrupted data(-1)
160 */
161 int recordEccCheck(Binary::const_iterator iterator) const;
162
163 /** @brief This interface collects Offset of VTOC
164 * @returns VTOC Offset
165 */
SunnySrivastava198419be6d32020-03-03 07:21:45 -0600166 openpower::vpd::constants::RecordOffset getVtocOffset() const;
Alpana Kumaric0aeac32019-11-28 05:20:10 -0600167
Alpana Kumari26a74af2019-09-10 23:53:58 -0500168 /** @brief VPD in binary format */
PriyangaRamasamy33c61c22021-04-06 11:15:57 -0500169 const Binary& vpd;
Deepak Kodihallie08fcad2016-11-22 02:10:08 -0600170
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700171 /** @brief parser output */
172 Parsed out;
Deepak Kodihallie08fcad2016-11-22 02:10:08 -0600173};
174
175} // namespace parser
176} // namespace vpd
Priyanga Ramasamy28079c82021-10-07 16:30:51 -0500177} // namespace openpower