Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 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 Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 16 | #include "user_data.hpp" |
| 17 | |
| 18 | #include "pel_types.hpp" |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 19 | #ifdef PELTOOL |
| 20 | #include "user_data_json.hpp" |
| 21 | #endif |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 22 | |
Arya K Padman | 5bc2653 | 2024-04-10 06:19:25 -0500 | [diff] [blame] | 23 | #include <phosphor-logging/lg2.hpp> |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 24 | |
| 25 | namespace openpower |
| 26 | { |
| 27 | namespace pels |
| 28 | { |
| 29 | |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 30 | void 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 Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 46 | void UserData::flatten(Stream& stream) const |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 47 | { |
| 48 | stream << _header << _data; |
| 49 | } |
| 50 | |
| 51 | UserData::UserData(Stream& pel) |
| 52 | { |
| 53 | try |
| 54 | { |
| 55 | unflatten(pel); |
| 56 | validate(); |
| 57 | } |
| 58 | catch (const std::exception& e) |
| 59 | { |
Arya K Padman | 5bc2653 | 2024-04-10 06:19:25 -0500 | [diff] [blame] | 60 | lg2::error("Cannot unflatten user data: {EXCEPTION}", "EXCEPTION", e); |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 61 | _valid = false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | UserData::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 Spinler | e2eb14a | 2025-05-09 13:34:51 -0500 | [diff] [blame] | 69 | _header.size = Section::headerSize() + data.size(); |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 70 | _header.version = version; |
| 71 | _header.subType = subType; |
| 72 | _header.componentID = componentID; |
| 73 | |
| 74 | _data = data; |
| 75 | |
| 76 | _valid = true; |
| 77 | } |
| 78 | |
| 79 | void UserData::validate() |
| 80 | { |
| 81 | if (header().id != static_cast<uint16_t>(SectionID::userData)) |
| 82 | { |
Arya K Padman | 5bc2653 | 2024-04-10 06:19:25 -0500 | [diff] [blame] | 83 | lg2::error("Invalid UserData section ID: {HEADER_ID}", "HEADER_ID", |
| 84 | lg2::hex, header().id); |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 85 | _valid = false; |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | _valid = true; |
| 90 | } |
| 91 | } |
| 92 | |
Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 93 | std::optional<std::string> UserData::getJSON( |
| 94 | uint8_t creatorID [[maybe_unused]], |
| 95 | const std::vector<std::string>& plugins [[maybe_unused]]) const |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 96 | { |
| 97 | #ifdef PELTOOL |
| 98 | return user_data::getJSON(_header.componentID, _header.subType, |
Harisuddin Mohamed Isa | 3fdcd4e | 2020-08-26 11:56:42 +0800 | [diff] [blame] | 99 | _header.version, _data, creatorID, plugins); |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 100 | #endif |
| 101 | return std::nullopt; |
| 102 | } |
| 103 | |
Matt Spinler | cbf3c4d | 2020-03-24 15:34:52 -0500 | [diff] [blame] | 104 | bool UserData::shrink(size_t newSize) |
| 105 | { |
| 106 | // minimum size is 4 bytes plus the 8B header |
Matt Spinler | e2eb14a | 2025-05-09 13:34:51 -0500 | [diff] [blame] | 107 | if ((newSize < flattenedSize()) && (newSize >= (Section::headerSize() + 4))) |
Matt Spinler | cbf3c4d | 2020-03-24 15:34:52 -0500 | [diff] [blame] | 108 | { |
Matt Spinler | e2eb14a | 2025-05-09 13:34:51 -0500 | [diff] [blame] | 109 | auto dataSize = newSize - Section::headerSize(); |
Matt Spinler | cbf3c4d | 2020-03-24 15:34:52 -0500 | [diff] [blame] | 110 | |
| 111 | // Ensure it's 4B aligned |
| 112 | _data.resize((dataSize / 4) * 4); |
Matt Spinler | e2eb14a | 2025-05-09 13:34:51 -0500 | [diff] [blame] | 113 | _header.size = Section::headerSize() + _data.size(); |
Matt Spinler | cbf3c4d | 2020-03-24 15:34:52 -0500 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
| 117 | return false; |
| 118 | } |
| 119 | |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 120 | } // namespace pels |
| 121 | } // namespace openpower |