blob: 520a7e00a2996502ad7db659f846d412c7ec3bf4 [file] [log] [blame]
Deepak Kodihallie08fcad2016-11-22 02:10:08 -06001#pragma once
2
Deepak Kodihalli023112f2016-11-22 22:02:14 -06003#include <cstddef>
Deepak Kodihallie08fcad2016-11-22 02:10:08 -06004#include "store.hpp"
5
6namespace openpower
7{
8namespace vpd
9{
10namespace parser
11{
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060012namespace keyword
13{
14
15/** @brief Encoding scheme of a VPD keyword's data */
16enum class Encoding
17{
18 ASCII, /**< data encoded in ascii */
19 RAW, /**< raw data */
20 // Keywords needing custom decoding
Dinesh Chinaric576b482017-07-17 16:34:10 -050021 B1, /**< The keyword B1 needs to be decoded specially */
22 UD /**< Special decoding of UD meant for UUID */
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060023};
24
25} // namespace keyword
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060026
Deepak Kodihalli023112f2016-11-22 22:02:14 -060027namespace internal
28{
29
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -060030using KeywordInfo = std::tuple<record::Keyword, keyword::Encoding>;
Deepak Kodihalli023112f2016-11-22 22:02:14 -060031using OffsetList = std::vector<uint32_t>;
Deepak Kodihalli683bf722016-11-24 06:50:43 -060032using KeywordMap = Parsed::mapped_type;
Deepak Kodihalli023112f2016-11-22 22:02:14 -060033
34}
35
Deepak Kodihallie08fcad2016-11-22 02:10:08 -060036/** @class Impl
37 * @brief Implements parser for OpenPOWER VPD
38 *
39 * An Impl object must be constructed by passing in OpenPOWER VPD in
40 * binary format. To parse the VPD, call the run() method. The run()
41 * method returns an openpower::vpd::Store object, which contains
42 * parsed VPD, and provides access methods for the VPD.
43 *
44 * Following is the algorithm used to parse OpenPOWER VPD:
45 * 1) Validate that the first record is VHDR, the header record.
46 * 2) From the VHDR record, get the offset of the VTOC record,
47 * which is the table of contents record.
48 * 3) Process the VTOC record - note offsets of supported records.
49 * 4) For each supported record :
50 * 4.1) Jump to record via offset. Add record name to parser output.
51 * 4.2) Process record - for each contained and supported keyword:
52 * 4.2.1) Note keyword name and value, associate this information to
53 * to the record noted in step 4.1).
54 */
55class Impl
56{
57 public:
58 Impl() = delete;
59 Impl(const Impl&) = delete;
60 Impl& operator=(const Impl&) = delete;
61 Impl(Impl&&) = delete;
62 Impl& operator=(Impl&&) = delete;
63 ~Impl() = default;
64
65 /** @brief Construct an Impl
66 *
67 * @param[in] vpdBuffer - Binary OpenPOWER VPD
68 */
69 explicit Impl(Binary&& vpdBuffer)
70 : vpd(std::move(vpdBuffer)),
71 out{}
72 {}
73
74 /** @brief Run the parser on binary OpenPOWER VPD
75 *
76 * @returns openpower::vpd::Store object
77 */
78 Store run();
79
80 private:
Deepak Kodihalli023112f2016-11-22 22:02:14 -060081 /** @brief Process the table of contents record, VHDR
82 *
83 * @returns List of offsets to records in VPD
84 */
85 internal::OffsetList readTOC() const;
86
87 /** @brief Read the PT keyword contained in the VHDR record,
88 * to obtain offsets to other records in the VPD.
89 *
90 * @param[in] iterator - iterator to buffer containing VPD
91 * @param[in] ptLength - Length of PT keyword data
92 *
93 * @returns List of offsets to records in VPD
94 */
95 internal::OffsetList readPT(Binary::const_iterator iterator,
96 std::size_t ptLen) const;
97
Deepak Kodihallia1143462016-11-24 06:28:45 -060098 /** @brief Read VPD information contained within a record
99 *
100 * @param[in] recordOffset - offset to a record location
101 * within the binary OpenPOWER VPD
102 */
103 void processRecord(std::size_t recordOffset);
104
Deepak Kodihalli4a475bd2016-11-24 07:08:20 -0600105 /** @brief Read keyword data
106 *
107 * @param[in] keyword - OpenPOWER VPD keyword
108 * @param[in] dataLength - Length of data to be read
109 * @param[in] iterator - iterator pointing to a Keyword's data in
110 * the VPD
111 *
112 * @returns keyword data as a string
113 */
114 std::string readKwData(const internal::KeywordInfo& keyword,
115 std::size_t dataLength,
116 Binary::const_iterator iterator);
Deepak Kodihallia1143462016-11-24 06:28:45 -0600117
Deepak Kodihalli683bf722016-11-24 06:50:43 -0600118 /** @brief While we're pointing at the keyword section of
119 * a record in the VPD, this will read all contained
120 * keywords and their values.
121 *
122 * @param[in] iterator - iterator pointing to a Keyword in the VPD
123 *
124 * @returns map of keyword:data
125 */
126 internal::KeywordMap readKeywords(Binary::const_iterator iterator);
127
Deepak Kodihalli810c9de2016-11-22 11:42:51 -0600128 /** @brief Checks if the VHDR record is present in the VPD */
129 void checkHeader() const;
130
Deepak Kodihallie08fcad2016-11-22 02:10:08 -0600131 /** @brief OpenPOWER VPD in binary format */
132 Binary vpd;
133
134 /** @brief parser output */
135 Parsed out;
136};
137
138} // namespace parser
139} // namespace vpd
140} // namespace openpower