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