blob: e01563ecc596fe1bbc98015733b8ab51e6fbbbb2 [file] [log] [blame]
Alexander Hansen40fb5492025-10-28 17:56:12 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3
Aatir Manzur51c92632019-09-06 13:30:48 -05004#include "user_data.hpp"
5
6#include "pel_types.hpp"
Matt Spinleracb7c102020-01-10 13:49:22 -06007#ifdef PELTOOL
8#include "user_data_json.hpp"
9#endif
Aatir Manzur51c92632019-09-06 13:30:48 -050010
Arya K Padman5bc26532024-04-10 06:19:25 -050011#include <phosphor-logging/lg2.hpp>
Aatir Manzur51c92632019-09-06 13:30:48 -050012
13namespace openpower
14{
15namespace pels
16{
17
Aatir Manzur51c92632019-09-06 13:30:48 -050018void UserData::unflatten(Stream& stream)
19{
20 stream >> _header;
21
22 if (_header.size <= SectionHeader::flattenedSize())
23 {
24 throw std::out_of_range(
25 "UserData::unflatten: SectionHeader::size field too small");
26 }
27
28 size_t dataLength = _header.size - SectionHeader::flattenedSize();
29 _data.resize(dataLength);
30
31 stream >> _data;
32}
33
Matt Spinler06885452019-11-06 10:35:42 -060034void UserData::flatten(Stream& stream) const
Aatir Manzur51c92632019-09-06 13:30:48 -050035{
36 stream << _header << _data;
37}
38
39UserData::UserData(Stream& pel)
40{
41 try
42 {
43 unflatten(pel);
44 validate();
45 }
46 catch (const std::exception& e)
47 {
Arya K Padman5bc26532024-04-10 06:19:25 -050048 lg2::error("Cannot unflatten user data: {EXCEPTION}", "EXCEPTION", e);
Aatir Manzur51c92632019-09-06 13:30:48 -050049 _valid = false;
50 }
51}
52
53UserData::UserData(uint16_t componentID, uint8_t subType, uint8_t version,
54 const std::vector<uint8_t>& data)
55{
56 _header.id = static_cast<uint16_t>(SectionID::userData);
Matt Spinlere2eb14a2025-05-09 13:34:51 -050057 _header.size = Section::headerSize() + data.size();
Aatir Manzur51c92632019-09-06 13:30:48 -050058 _header.version = version;
59 _header.subType = subType;
60 _header.componentID = componentID;
61
62 _data = data;
63
64 _valid = true;
65}
66
67void UserData::validate()
68{
69 if (header().id != static_cast<uint16_t>(SectionID::userData))
70 {
Arya K Padman5bc26532024-04-10 06:19:25 -050071 lg2::error("Invalid UserData section ID: {HEADER_ID}", "HEADER_ID",
72 lg2::hex, header().id);
Aatir Manzur51c92632019-09-06 13:30:48 -050073 _valid = false;
74 }
75 else
76 {
77 _valid = true;
78 }
79}
80
Patrick Williams075c7922024-08-16 15:19:49 -040081std::optional<std::string> UserData::getJSON(
82 uint8_t creatorID [[maybe_unused]],
83 const std::vector<std::string>& plugins [[maybe_unused]]) const
Matt Spinleracb7c102020-01-10 13:49:22 -060084{
85#ifdef PELTOOL
86 return user_data::getJSON(_header.componentID, _header.subType,
Harisuddin Mohamed Isa3fdcd4e2020-08-26 11:56:42 +080087 _header.version, _data, creatorID, plugins);
Matt Spinleracb7c102020-01-10 13:49:22 -060088#endif
89 return std::nullopt;
90}
91
Matt Spinlercbf3c4d2020-03-24 15:34:52 -050092bool UserData::shrink(size_t newSize)
93{
94 // minimum size is 4 bytes plus the 8B header
Matt Spinlere2eb14a2025-05-09 13:34:51 -050095 if ((newSize < flattenedSize()) && (newSize >= (Section::headerSize() + 4)))
Matt Spinlercbf3c4d2020-03-24 15:34:52 -050096 {
Matt Spinlere2eb14a2025-05-09 13:34:51 -050097 auto dataSize = newSize - Section::headerSize();
Matt Spinlercbf3c4d2020-03-24 15:34:52 -050098
99 // Ensure it's 4B aligned
100 _data.resize((dataSize / 4) * 4);
Matt Spinlere2eb14a2025-05-09 13:34:51 -0500101 _header.size = Section::headerSize() + _data.size();
Matt Spinlercbf3c4d2020-03-24 15:34:52 -0500102 return true;
103 }
104
105 return false;
106}
107
Aatir Manzur51c92632019-09-06 13:30:48 -0500108} // namespace pels
109} // namespace openpower