blob: a24a8f31cdd192a31c728961bb1e60e27061ae88 [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"
21#ifdef PELTOOL
22#include "user_data_json.hpp"
23#endif
Aatir Manzur51c92632019-09-06 13:30:48 -050024
Matt Spinler7f1b9052022-03-22 09:18:58 -050025#include <phosphor-logging/log.hpp>
Aatir Manzur51c92632019-09-06 13:30:48 -050026
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050027#include <format>
28
Aatir Manzur51c92632019-09-06 13:30:48 -050029namespace openpower
30{
31namespace pels
32{
33
Matt Spinler7f1b9052022-03-22 09:18:58 -050034using namespace phosphor::logging;
35
Aatir Manzur51c92632019-09-06 13:30:48 -050036void UserData::unflatten(Stream& stream)
37{
38 stream >> _header;
39
40 if (_header.size <= SectionHeader::flattenedSize())
41 {
42 throw std::out_of_range(
43 "UserData::unflatten: SectionHeader::size field too small");
44 }
45
46 size_t dataLength = _header.size - SectionHeader::flattenedSize();
47 _data.resize(dataLength);
48
49 stream >> _data;
50}
51
Matt Spinler06885452019-11-06 10:35:42 -060052void UserData::flatten(Stream& stream) const
Aatir Manzur51c92632019-09-06 13:30:48 -050053{
54 stream << _header << _data;
55}
56
57UserData::UserData(Stream& pel)
58{
59 try
60 {
61 unflatten(pel);
62 validate();
63 }
64 catch (const std::exception& e)
65 {
Matt Spinler8ac20502023-01-04 11:03:58 -060066 log<level::ERR>(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050067 std::format("Cannot unflatten user data: {}", e.what()).c_str());
Aatir Manzur51c92632019-09-06 13:30:48 -050068 _valid = false;
69 }
70}
71
72UserData::UserData(uint16_t componentID, uint8_t subType, uint8_t version,
73 const std::vector<uint8_t>& data)
74{
75 _header.id = static_cast<uint16_t>(SectionID::userData);
76 _header.size = Section::flattenedSize() + data.size();
77 _header.version = version;
78 _header.subType = subType;
79 _header.componentID = componentID;
80
81 _data = data;
82
83 _valid = true;
84}
85
86void UserData::validate()
87{
88 if (header().id != static_cast<uint16_t>(SectionID::userData))
89 {
Matt Spinler8ac20502023-01-04 11:03:58 -060090 log<level::ERR>(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050091 std::format("Invalid UserData section ID: {0:#x}", header().id)
Matt Spinler8ac20502023-01-04 11:03:58 -060092 .c_str());
Aatir Manzur51c92632019-09-06 13:30:48 -050093 _valid = false;
94 }
95 else
96 {
97 _valid = true;
98 }
99}
100
Harisuddin Mohamed Isa3fdcd4e2020-08-26 11:56:42 +0800101std::optional<std::string>
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500102 UserData::getJSON(uint8_t creatorID [[maybe_unused]],
103 const std::vector<std::string>& plugins
104 [[maybe_unused]]) const
Matt Spinleracb7c102020-01-10 13:49:22 -0600105{
106#ifdef PELTOOL
107 return user_data::getJSON(_header.componentID, _header.subType,
Harisuddin Mohamed Isa3fdcd4e2020-08-26 11:56:42 +0800108 _header.version, _data, creatorID, plugins);
Matt Spinleracb7c102020-01-10 13:49:22 -0600109#endif
110 return std::nullopt;
111}
112
Matt Spinlercbf3c4d2020-03-24 15:34:52 -0500113bool UserData::shrink(size_t newSize)
114{
115 // minimum size is 4 bytes plus the 8B header
116 if ((newSize < flattenedSize()) &&
117 (newSize >= (Section::flattenedSize() + 4)))
118 {
119 auto dataSize = newSize - Section::flattenedSize();
120
121 // Ensure it's 4B aligned
122 _data.resize((dataSize / 4) * 4);
123 _header.size = Section::flattenedSize() + _data.size();
124 return true;
125 }
126
127 return false;
128}
129
Aatir Manzur51c92632019-09-06 13:30:48 -0500130} // namespace pels
131} // namespace openpower