blob: 6ff394a7de65468a7158ccd59ee14a80aad89aa1 [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
Matt Spinler2ea96f62022-02-23 16:31:01 -060022#include <fmt/format.h>
Matt Spinler386a61e2020-08-13 15:51:12 -050023
Matt Spinler7f1b9052022-03-22 09:18:58 -050024#include <phosphor-logging/log.hpp>
25
Matt Spinler386a61e2020-08-13 15:51:12 -050026namespace openpower::pels
27{
28
Matt Spinler7f1b9052022-03-22 09:18:58 -050029using namespace phosphor::logging;
30
Matt Spinler386a61e2020-08-13 15:51:12 -050031void ExtendedUserData::unflatten(Stream& stream)
32{
33 stream >> _header;
34
35 if (_header.size <= SectionHeader::flattenedSize() + 4)
36 {
37 throw std::out_of_range(
38 fmt::format("ExtendedUserData::unflatten: SectionHeader::size {} "
39 "too small",
40 _header.size));
41 }
42
43 size_t dataLength = _header.size - 4 - SectionHeader::flattenedSize();
44 _data.resize(dataLength);
45
46 stream >> _creatorID >> _reserved1B >> _reserved2B >> _data;
47}
48
49void ExtendedUserData::flatten(Stream& stream) const
50{
51 stream << _header << _creatorID << _reserved1B << _reserved2B << _data;
52}
53
54ExtendedUserData::ExtendedUserData(Stream& pel)
55{
56 try
57 {
58 unflatten(pel);
59 validate();
60 }
61 catch (const std::exception& e)
62 {
Matt Spinler7f1b9052022-03-22 09:18:58 -050063 log<level::ERR>(
Matt Spinler8ac20502023-01-04 11:03:58 -060064 fmt::format("Cannot unflatten ExtendedUserData section: {}",
65 e.what())
Matt Spinler7f1b9052022-03-22 09:18:58 -050066 .c_str());
Matt Spinler386a61e2020-08-13 15:51:12 -050067 _valid = false;
68 }
69}
70
71ExtendedUserData::ExtendedUserData(uint16_t componentID, uint8_t subType,
72 uint8_t version, uint8_t creatorID,
73 const std::vector<uint8_t>& data)
74{
75 _header.id = static_cast<uint16_t>(SectionID::extUserData);
76 _header.version = version;
77 _header.subType = subType;
78 _header.componentID = componentID;
79
80 _creatorID = creatorID;
81 _reserved1B = 0;
82 _reserved2B = 0;
83 _data = data;
84 _header.size = flattenedSize();
85 _valid = true;
86}
87
88void ExtendedUserData::validate()
89{
90 if (header().id != static_cast<uint16_t>(SectionID::extUserData))
91 {
Matt Spinler7f1b9052022-03-22 09:18:58 -050092 log<level::ERR>(
Matt Spinler8ac20502023-01-04 11:03:58 -060093 fmt::format("Invalid ExtendedUserData section ID: {0:#x}",
94 header().id)
Matt Spinler7f1b9052022-03-22 09:18:58 -050095 .c_str());
Matt Spinler386a61e2020-08-13 15:51:12 -050096 _valid = false;
97 }
98 else
99 {
100 _valid = true;
101 }
102}
103
104std::optional<std::string>
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500105 ExtendedUserData::getJSON(uint8_t /*creatorID*/,
106 const std::vector<std::string>& plugins
107 [[maybe_unused]]) const
Matt Spinler386a61e2020-08-13 15:51:12 -0500108{
109 // Use the creator ID value from the section.
110#ifdef PELTOOL
111 return user_data::getJSON(_header.componentID, _header.subType,
112 _header.version, _data, _creatorID, plugins);
113#endif
114 return std::nullopt;
115}
116
117bool ExtendedUserData::shrink(size_t newSize)
118{
119 // minimum size is 8B header + 4B of fields + 4B of data
120 if ((newSize < flattenedSize()) &&
121 (newSize >= (Section::flattenedSize() + 8)))
122 {
123 auto dataSize = newSize - Section::flattenedSize() - 4;
124
125 // Ensure it's 4B aligned
126 _data.resize((dataSize / 4) * 4);
127 _header.size = flattenedSize();
128 return true;
129 }
130
131 return false;
132}
133
134} // namespace openpower::pels