blob: f64428dd9c9baa02cf5fbe60e7fc8720beca4f2f [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
2 * Copyright © 2019 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 */
Aatir Manzur51c92632019-09-06 13:30:48 -050016#include "user_data.hpp"
17
18#include "pel_types.hpp"
Matt Spinleracb7c102020-01-10 13:49:22 -060019#ifdef PELTOOL
20#include "user_data_json.hpp"
21#endif
Aatir Manzur51c92632019-09-06 13:30:48 -050022
Arya K Padman5bc26532024-04-10 06:19:25 -050023#include <phosphor-logging/lg2.hpp>
Aatir Manzur51c92632019-09-06 13:30:48 -050024
25namespace openpower
26{
27namespace pels
28{
29
Aatir Manzur51c92632019-09-06 13:30:48 -050030void UserData::unflatten(Stream& stream)
31{
32 stream >> _header;
33
34 if (_header.size <= SectionHeader::flattenedSize())
35 {
36 throw std::out_of_range(
37 "UserData::unflatten: SectionHeader::size field too small");
38 }
39
40 size_t dataLength = _header.size - SectionHeader::flattenedSize();
41 _data.resize(dataLength);
42
43 stream >> _data;
44}
45
Matt Spinler06885452019-11-06 10:35:42 -060046void UserData::flatten(Stream& stream) const
Aatir Manzur51c92632019-09-06 13:30:48 -050047{
48 stream << _header << _data;
49}
50
51UserData::UserData(Stream& pel)
52{
53 try
54 {
55 unflatten(pel);
56 validate();
57 }
58 catch (const std::exception& e)
59 {
Arya K Padman5bc26532024-04-10 06:19:25 -050060 lg2::error("Cannot unflatten user data: {EXCEPTION}", "EXCEPTION", e);
Aatir Manzur51c92632019-09-06 13:30:48 -050061 _valid = false;
62 }
63}
64
65UserData::UserData(uint16_t componentID, uint8_t subType, uint8_t version,
66 const std::vector<uint8_t>& data)
67{
68 _header.id = static_cast<uint16_t>(SectionID::userData);
Matt Spinlere2eb14a2025-05-09 13:34:51 -050069 _header.size = Section::headerSize() + data.size();
Aatir Manzur51c92632019-09-06 13:30:48 -050070 _header.version = version;
71 _header.subType = subType;
72 _header.componentID = componentID;
73
74 _data = data;
75
76 _valid = true;
77}
78
79void UserData::validate()
80{
81 if (header().id != static_cast<uint16_t>(SectionID::userData))
82 {
Arya K Padman5bc26532024-04-10 06:19:25 -050083 lg2::error("Invalid UserData section ID: {HEADER_ID}", "HEADER_ID",
84 lg2::hex, header().id);
Aatir Manzur51c92632019-09-06 13:30:48 -050085 _valid = false;
86 }
87 else
88 {
89 _valid = true;
90 }
91}
92
Patrick Williams075c7922024-08-16 15:19:49 -040093std::optional<std::string> UserData::getJSON(
94 uint8_t creatorID [[maybe_unused]],
95 const std::vector<std::string>& plugins [[maybe_unused]]) const
Matt Spinleracb7c102020-01-10 13:49:22 -060096{
97#ifdef PELTOOL
98 return user_data::getJSON(_header.componentID, _header.subType,
Harisuddin Mohamed Isa3fdcd4e2020-08-26 11:56:42 +080099 _header.version, _data, creatorID, plugins);
Matt Spinleracb7c102020-01-10 13:49:22 -0600100#endif
101 return std::nullopt;
102}
103
Matt Spinlercbf3c4d2020-03-24 15:34:52 -0500104bool UserData::shrink(size_t newSize)
105{
106 // minimum size is 4 bytes plus the 8B header
Matt Spinlere2eb14a2025-05-09 13:34:51 -0500107 if ((newSize < flattenedSize()) && (newSize >= (Section::headerSize() + 4)))
Matt Spinlercbf3c4d2020-03-24 15:34:52 -0500108 {
Matt Spinlere2eb14a2025-05-09 13:34:51 -0500109 auto dataSize = newSize - Section::headerSize();
Matt Spinlercbf3c4d2020-03-24 15:34:52 -0500110
111 // Ensure it's 4B aligned
112 _data.resize((dataSize / 4) * 4);
Matt Spinlere2eb14a2025-05-09 13:34:51 -0500113 _header.size = Section::headerSize() + _data.size();
Matt Spinlercbf3c4d2020-03-24 15:34:52 -0500114 return true;
115 }
116
117 return false;
118}
119
Aatir Manzur51c92632019-09-06 13:30:48 -0500120} // namespace pels
121} // namespace openpower