blob: e400e350b0105d6aa5656b596a2edd34fd02475f [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
Matt Spinleracb7c102020-01-10 13:49:22 -060018#include "json_utils.hpp"
Aatir Manzur51c92632019-09-06 13:30:48 -050019#include "pel_types.hpp"
Matt Spinleracb7c102020-01-10 13:49:22 -060020#include "user_data_formats.hpp"
Matt Spinler2ea96f62022-02-23 16:31:01 -060021
22#include <fmt/format.h>
Matt Spinleracb7c102020-01-10 13:49:22 -060023#ifdef PELTOOL
24#include "user_data_json.hpp"
25#endif
Aatir Manzur51c92632019-09-06 13:30:48 -050026
Matt Spinler2ea96f62022-02-23 16:31:01 -060027#include "trace.hpp"
Aatir Manzur51c92632019-09-06 13:30:48 -050028
29namespace openpower
30{
31namespace pels
32{
33
Aatir Manzur51c92632019-09-06 13:30:48 -050034void UserData::unflatten(Stream& stream)
35{
36 stream >> _header;
37
38 if (_header.size <= SectionHeader::flattenedSize())
39 {
40 throw std::out_of_range(
41 "UserData::unflatten: SectionHeader::size field too small");
42 }
43
44 size_t dataLength = _header.size - SectionHeader::flattenedSize();
45 _data.resize(dataLength);
46
47 stream >> _data;
48}
49
Matt Spinler06885452019-11-06 10:35:42 -060050void UserData::flatten(Stream& stream) const
Aatir Manzur51c92632019-09-06 13:30:48 -050051{
52 stream << _header << _data;
53}
54
55UserData::UserData(Stream& pel)
56{
57 try
58 {
59 unflatten(pel);
60 validate();
61 }
62 catch (const std::exception& e)
63 {
Matt Spinler2ea96f62022-02-23 16:31:01 -060064 trace::error(fmt::format("Cannot unflatten user data: {}", e.what()));
Aatir Manzur51c92632019-09-06 13:30:48 -050065 _valid = false;
66 }
67}
68
69UserData::UserData(uint16_t componentID, uint8_t subType, uint8_t version,
70 const std::vector<uint8_t>& data)
71{
72 _header.id = static_cast<uint16_t>(SectionID::userData);
73 _header.size = Section::flattenedSize() + data.size();
74 _header.version = version;
75 _header.subType = subType;
76 _header.componentID = componentID;
77
78 _data = data;
79
80 _valid = true;
81}
82
83void UserData::validate()
84{
85 if (header().id != static_cast<uint16_t>(SectionID::userData))
86 {
Matt Spinler2ea96f62022-02-23 16:31:01 -060087 trace::error(
88 fmt::format("Invalid user data section ID: {}", header().id));
Aatir Manzur51c92632019-09-06 13:30:48 -050089 _valid = false;
90 }
91 else
92 {
93 _valid = true;
94 }
95}
96
Harisuddin Mohamed Isa3fdcd4e2020-08-26 11:56:42 +080097std::optional<std::string>
Patrick Williamsd26fa3e2021-04-21 15:22:23 -050098 UserData::getJSON(uint8_t creatorID [[maybe_unused]],
99 const std::vector<std::string>& plugins
100 [[maybe_unused]]) const
Matt Spinleracb7c102020-01-10 13:49:22 -0600101{
102#ifdef PELTOOL
103 return user_data::getJSON(_header.componentID, _header.subType,
Harisuddin Mohamed Isa3fdcd4e2020-08-26 11:56:42 +0800104 _header.version, _data, creatorID, plugins);
Matt Spinleracb7c102020-01-10 13:49:22 -0600105#endif
106 return std::nullopt;
107}
108
Matt Spinlercbf3c4d2020-03-24 15:34:52 -0500109bool UserData::shrink(size_t newSize)
110{
111 // minimum size is 4 bytes plus the 8B header
112 if ((newSize < flattenedSize()) &&
113 (newSize >= (Section::flattenedSize() + 4)))
114 {
115 auto dataSize = newSize - Section::flattenedSize();
116
117 // Ensure it's 4B aligned
118 _data.resize((dataSize / 4) * 4);
119 _header.size = Section::flattenedSize() + _data.size();
120 return true;
121 }
122
123 return false;
124}
125
Aatir Manzur51c92632019-09-06 13:30:48 -0500126} // namespace pels
127} // namespace openpower