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