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 | */ |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 16 | #include "private_header.hpp" |
| 17 | |
Harisuddin Mohamed Isa | 2ecb465 | 2020-01-10 10:51:02 +0800 | [diff] [blame] | 18 | #include "json_utils.hpp" |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 19 | #include "log_id.hpp" |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 20 | #include "pel_types.hpp" |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 21 | #include "pel_values.hpp" |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 22 | |
Matt Spinler | 8ac2050 | 2023-01-04 11:03:58 -0600 | [diff] [blame] | 23 | #include <fmt/format.h> |
| 24 | |
Matt Spinler | 7f1b905 | 2022-03-22 09:18:58 -0500 | [diff] [blame] | 25 | #include <phosphor-logging/log.hpp> |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 26 | |
| 27 | namespace openpower |
| 28 | { |
| 29 | namespace pels |
| 30 | { |
| 31 | |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 32 | namespace pv = openpower::pels::pel_values; |
Matt Spinler | 7f1b905 | 2022-03-22 09:18:58 -0500 | [diff] [blame] | 33 | using namespace phosphor::logging; |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 34 | |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 35 | PrivateHeader::PrivateHeader(uint16_t componentID, uint32_t obmcLogID, |
| 36 | uint64_t timestamp) |
| 37 | { |
| 38 | _header.id = static_cast<uint16_t>(SectionID::privateHeader); |
| 39 | _header.size = PrivateHeader::flattenedSize(); |
| 40 | _header.version = privateHeaderVersion; |
| 41 | _header.subType = 0; |
| 42 | _header.componentID = componentID; |
| 43 | |
| 44 | _createTimestamp = getBCDTime(timestamp); |
| 45 | |
| 46 | auto now = std::chrono::system_clock::now(); |
| 47 | _commitTimestamp = getBCDTime(now); |
| 48 | |
| 49 | _creatorID = static_cast<uint8_t>(CreatorID::openBMC); |
| 50 | |
| 51 | // Add support for reminder and telemetry log types here if |
| 52 | // ever necessary. |
| 53 | _logType = 0; |
| 54 | |
| 55 | _reservedByte = 0; |
| 56 | |
| 57 | // the final section count will be updated later |
| 58 | _sectionCount = 1; |
| 59 | |
| 60 | _obmcLogID = obmcLogID; |
| 61 | |
| 62 | _id = generatePELID(); |
| 63 | |
| 64 | _plid = _id; |
| 65 | |
| 66 | // Leave _creatorVersion at 0 |
| 67 | |
| 68 | _valid = true; |
| 69 | } |
| 70 | |
Matt Spinler | 31eed99 | 2019-10-09 14:07:52 -0500 | [diff] [blame] | 71 | PrivateHeader::PrivateHeader(Stream& pel) : |
| 72 | _creatorID(0), _logType(0), _reservedByte(0), _sectionCount(0), |
| 73 | _obmcLogID(0), _plid(0), _id(0) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 74 | { |
| 75 | try |
| 76 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 77 | unflatten(pel); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 78 | validate(); |
| 79 | } |
| 80 | catch (const std::exception& e) |
| 81 | { |
Matt Spinler | 8ac2050 | 2023-01-04 11:03:58 -0600 | [diff] [blame] | 82 | log<level::ERR>( |
Matt Spinler | 9f5c734 | 2023-03-13 16:27:04 -0500 | [diff] [blame] | 83 | fmt::format("Cannot unflatten private header: {}", e.what()) |
Matt Spinler | 8ac2050 | 2023-01-04 11:03:58 -0600 | [diff] [blame] | 84 | .c_str()); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 85 | _valid = false; |
| 86 | } |
| 87 | } |
Matt Spinler | b832aa5 | 2023-03-21 15:32:34 -0500 | [diff] [blame] | 88 | std::optional<std::string> PrivateHeader::getJSON(uint8_t creatorID) const |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 89 | { |
| 90 | char tmpPhVal[50]; |
Harisuddin Mohamed Isa | 160c51c | 2020-02-13 23:42:20 +0800 | [diff] [blame] | 91 | sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X", |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 92 | _createTimestamp.month, _createTimestamp.day, |
| 93 | _createTimestamp.yearMSB, _createTimestamp.yearLSB, |
| 94 | _createTimestamp.hour, _createTimestamp.minutes, |
| 95 | _createTimestamp.seconds); |
| 96 | std::string phCreateTStr(tmpPhVal); |
Harisuddin Mohamed Isa | 160c51c | 2020-02-13 23:42:20 +0800 | [diff] [blame] | 97 | sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X", |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 98 | _commitTimestamp.month, _commitTimestamp.day, |
Sumit Kumar | 74348c6 | 2021-10-26 05:48:28 -0500 | [diff] [blame] | 99 | _commitTimestamp.yearMSB, _commitTimestamp.yearLSB, |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 100 | _commitTimestamp.hour, _commitTimestamp.minutes, |
| 101 | _commitTimestamp.seconds); |
| 102 | std::string phCommitTStr(tmpPhVal); |
Harisuddin Mohamed Isa | bebeb94 | 2020-03-12 17:12:24 +0800 | [diff] [blame] | 103 | std::string creator = getNumberString("%c", _creatorID); |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 104 | creator = pv::creatorIDs.count(creator) ? pv::creatorIDs.at(creator) |
| 105 | : "Unknown CreatorID"; |
| 106 | std::string phCreatorVersionStr = |
| 107 | std::string(reinterpret_cast<const char*>(_creatorVersion.version)); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 108 | |
Harisuddin Mohamed Isa | 2ecb465 | 2020-01-10 10:51:02 +0800 | [diff] [blame] | 109 | std::string ph; |
Harisuddin Mohamed Isa | bebeb94 | 2020-03-12 17:12:24 +0800 | [diff] [blame] | 110 | jsonInsert(ph, pv::sectionVer, getNumberString("%d", privateHeaderVersion), |
| 111 | 1); |
| 112 | jsonInsert(ph, pv::subSection, getNumberString("%d", _header.subType), 1); |
Matt Spinler | b832aa5 | 2023-03-21 15:32:34 -0500 | [diff] [blame] | 113 | jsonInsert(ph, pv::createdBy, |
| 114 | getComponentName(_header.componentID, creatorID), 1); |
Harisuddin Mohamed Isa | 2ecb465 | 2020-01-10 10:51:02 +0800 | [diff] [blame] | 115 | jsonInsert(ph, "Created at", phCreateTStr, 1); |
| 116 | jsonInsert(ph, "Committed at", phCommitTStr, 1); |
| 117 | jsonInsert(ph, "Creator Subsystem", creator, 1); |
| 118 | jsonInsert(ph, "CSSVER", phCreatorVersionStr, 1); |
Harisuddin Mohamed Isa | bebeb94 | 2020-03-12 17:12:24 +0800 | [diff] [blame] | 119 | jsonInsert(ph, "Platform Log Id", getNumberString("0x%X", _plid), 1); |
| 120 | jsonInsert(ph, "Entry Id", getNumberString("0x%X", _id), 1); |
| 121 | jsonInsert(ph, "BMC Event Log Id", std::to_string(_obmcLogID), 1); |
Harisuddin Mohamed Isa | 2ecb465 | 2020-01-10 10:51:02 +0800 | [diff] [blame] | 122 | ph.erase(ph.size() - 2); |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 123 | |
| 124 | return ph; |
| 125 | } |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 126 | void PrivateHeader::validate() |
| 127 | { |
| 128 | bool failed = false; |
| 129 | |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 130 | if (header().id != static_cast<uint16_t>(SectionID::privateHeader)) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 131 | { |
Matt Spinler | 8ac2050 | 2023-01-04 11:03:58 -0600 | [diff] [blame] | 132 | log<level::ERR>(fmt::format("Invalid private header section ID: {0:#x}", |
| 133 | header().id) |
| 134 | .c_str()); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 135 | failed = true; |
| 136 | } |
| 137 | |
| 138 | if (header().version != privateHeaderVersion) |
| 139 | { |
Matt Spinler | 8ac2050 | 2023-01-04 11:03:58 -0600 | [diff] [blame] | 140 | log<level::ERR>(fmt::format("Invalid private header version: {0:#x}", |
| 141 | header().version) |
| 142 | .c_str()); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 143 | failed = true; |
| 144 | } |
| 145 | |
| 146 | if (_sectionCount < minSectionCount) |
| 147 | { |
Matt Spinler | 8ac2050 | 2023-01-04 11:03:58 -0600 | [diff] [blame] | 148 | log<level::ERR>( |
| 149 | fmt::format("Invalid section count in private header: {0:#x}", |
| 150 | _sectionCount) |
| 151 | .c_str()); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 152 | failed = true; |
| 153 | } |
| 154 | |
| 155 | _valid = (failed) ? false : true; |
| 156 | } |
| 157 | |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 158 | void PrivateHeader::unflatten(Stream& stream) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 159 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 160 | stream >> _header >> _createTimestamp >> _commitTimestamp >> _creatorID >> |
| 161 | _logType >> _reservedByte >> _sectionCount >> _obmcLogID >> |
| 162 | _creatorVersion >> _plid >> _id; |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 163 | } |
| 164 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 165 | void PrivateHeader::flatten(Stream& stream) const |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 166 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 167 | stream << _header << _createTimestamp << _commitTimestamp << _creatorID |
| 168 | << _logType << _reservedByte << _sectionCount << _obmcLogID |
| 169 | << _creatorVersion << _plid << _id; |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | Stream& operator>>(Stream& s, CreatorVersion& cv) |
| 173 | { |
| 174 | for (size_t i = 0; i < sizeof(CreatorVersion); i++) |
| 175 | { |
| 176 | s >> cv.version[i]; |
| 177 | } |
| 178 | return s; |
| 179 | } |
| 180 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 181 | Stream& operator<<(Stream& s, const CreatorVersion& cv) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 182 | { |
| 183 | for (size_t i = 0; i < sizeof(CreatorVersion); i++) |
| 184 | { |
| 185 | s << cv.version[i]; |
| 186 | } |
| 187 | return s; |
| 188 | } |
| 189 | |
| 190 | } // namespace pels |
| 191 | } // namespace openpower |