blob: 92d1c700f6093699ae3bfe020606df7ce6b07c08 [file] [log] [blame]
Matt Spinler386a61e2020-08-13 15:51:12 -05001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "extended_user_data.hpp"
17
18#include "pel_types.hpp"
19#ifdef PELTOOL
20#include "user_data_json.hpp"
21#endif
Arya K Padman5bc26532024-04-10 06:19:25 -050022#include <phosphor-logging/lg2.hpp>
Matt Spinler7f1b9052022-03-22 09:18:58 -050023
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050024#include <format>
25
Matt Spinler386a61e2020-08-13 15:51:12 -050026namespace openpower::pels
27{
28
Matt Spinler386a61e2020-08-13 15:51:12 -050029void ExtendedUserData::unflatten(Stream& stream)
30{
31 stream >> _header;
32
33 if (_header.size <= SectionHeader::flattenedSize() + 4)
34 {
35 throw std::out_of_range(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050036 std::format("ExtendedUserData::unflatten: SectionHeader::size {} "
Matt Spinler386a61e2020-08-13 15:51:12 -050037 "too small",
38 _header.size));
39 }
40
41 size_t dataLength = _header.size - 4 - SectionHeader::flattenedSize();
42 _data.resize(dataLength);
43
44 stream >> _creatorID >> _reserved1B >> _reserved2B >> _data;
45}
46
47void ExtendedUserData::flatten(Stream& stream) const
48{
49 stream << _header << _creatorID << _reserved1B << _reserved2B << _data;
50}
51
52ExtendedUserData::ExtendedUserData(Stream& pel)
53{
54 try
55 {
56 unflatten(pel);
57 validate();
58 }
59 catch (const std::exception& e)
60 {
Arya K Padman5bc26532024-04-10 06:19:25 -050061 lg2::error("Cannot unflatten ExtendedUserData section: {EXCEPTION}",
62 "EXCEPTION", e);
Matt Spinler386a61e2020-08-13 15:51:12 -050063 _valid = false;
64 }
65}
66
67ExtendedUserData::ExtendedUserData(uint16_t componentID, uint8_t subType,
68 uint8_t version, uint8_t creatorID,
69 const std::vector<uint8_t>& data)
70{
71 _header.id = static_cast<uint16_t>(SectionID::extUserData);
72 _header.version = version;
73 _header.subType = subType;
74 _header.componentID = componentID;
75
76 _creatorID = creatorID;
77 _reserved1B = 0;
78 _reserved2B = 0;
79 _data = data;
80 _header.size = flattenedSize();
81 _valid = true;
82}
83
84void ExtendedUserData::validate()
85{
86 if (header().id != static_cast<uint16_t>(SectionID::extUserData))
87 {
Arya K Padman5bc26532024-04-10 06:19:25 -050088 lg2::error("Invalid ExtendedUserData section ID: {HEADER_ID}",
89 "HEADER_ID", lg2::hex, header().id);
Matt Spinler386a61e2020-08-13 15:51:12 -050090 _valid = false;
91 }
92 else
93 {
94 _valid = true;
95 }
96}
97
98std::optional<std::string>
Patrick Williamsd26fa3e2021-04-21 15:22:23 -050099 ExtendedUserData::getJSON(uint8_t /*creatorID*/,
100 const std::vector<std::string>& plugins
101 [[maybe_unused]]) const
Matt Spinler386a61e2020-08-13 15:51:12 -0500102{
103 // Use the creator ID value from the section.
104#ifdef PELTOOL
105 return user_data::getJSON(_header.componentID, _header.subType,
106 _header.version, _data, _creatorID, plugins);
107#endif
108 return std::nullopt;
109}
110
111bool ExtendedUserData::shrink(size_t newSize)
112{
113 // minimum size is 8B header + 4B of fields + 4B of data
114 if ((newSize < flattenedSize()) &&
115 (newSize >= (Section::flattenedSize() + 8)))
116 {
117 auto dataSize = newSize - Section::flattenedSize() - 4;
118
119 // Ensure it's 4B aligned
120 _data.resize((dataSize / 4) * 4);
121 _header.size = flattenedSize();
122 return true;
123 }
124
125 return false;
126}
127
128} // namespace openpower::pels