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