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 | |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 18 | #include "log_id.hpp" |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 19 | #include "pel_types.hpp" |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 20 | #include "pel_values.hpp" |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 21 | |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 22 | #include <phosphor-logging/log.hpp> |
| 23 | |
| 24 | namespace openpower |
| 25 | { |
| 26 | namespace pels |
| 27 | { |
| 28 | |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 29 | namespace pv = openpower::pels::pel_values; |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 30 | using namespace phosphor::logging; |
| 31 | |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 32 | PrivateHeader::PrivateHeader(uint16_t componentID, uint32_t obmcLogID, |
| 33 | uint64_t timestamp) |
| 34 | { |
| 35 | _header.id = static_cast<uint16_t>(SectionID::privateHeader); |
| 36 | _header.size = PrivateHeader::flattenedSize(); |
| 37 | _header.version = privateHeaderVersion; |
| 38 | _header.subType = 0; |
| 39 | _header.componentID = componentID; |
| 40 | |
| 41 | _createTimestamp = getBCDTime(timestamp); |
| 42 | |
| 43 | auto now = std::chrono::system_clock::now(); |
| 44 | _commitTimestamp = getBCDTime(now); |
| 45 | |
| 46 | _creatorID = static_cast<uint8_t>(CreatorID::openBMC); |
| 47 | |
| 48 | // Add support for reminder and telemetry log types here if |
| 49 | // ever necessary. |
| 50 | _logType = 0; |
| 51 | |
| 52 | _reservedByte = 0; |
| 53 | |
| 54 | // the final section count will be updated later |
| 55 | _sectionCount = 1; |
| 56 | |
| 57 | _obmcLogID = obmcLogID; |
| 58 | |
| 59 | _id = generatePELID(); |
| 60 | |
| 61 | _plid = _id; |
| 62 | |
| 63 | // Leave _creatorVersion at 0 |
| 64 | |
| 65 | _valid = true; |
| 66 | } |
| 67 | |
Matt Spinler | 31eed99 | 2019-10-09 14:07:52 -0500 | [diff] [blame] | 68 | PrivateHeader::PrivateHeader(Stream& pel) : |
| 69 | _creatorID(0), _logType(0), _reservedByte(0), _sectionCount(0), |
| 70 | _obmcLogID(0), _plid(0), _id(0) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 71 | { |
| 72 | try |
| 73 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 74 | unflatten(pel); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 75 | validate(); |
| 76 | } |
| 77 | catch (const std::exception& e) |
| 78 | { |
| 79 | log<level::ERR>("Cannot unflatten private header", |
| 80 | entry("ERROR=%s", e.what())); |
| 81 | _valid = false; |
| 82 | } |
| 83 | } |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 84 | std::optional<std::string> PrivateHeader::getJSON() const |
| 85 | { |
| 86 | char tmpPhVal[50]; |
| 87 | sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X", |
| 88 | _createTimestamp.month, _createTimestamp.day, |
| 89 | _createTimestamp.yearMSB, _createTimestamp.yearLSB, |
| 90 | _createTimestamp.hour, _createTimestamp.minutes, |
| 91 | _createTimestamp.seconds); |
| 92 | std::string phCreateTStr(tmpPhVal); |
| 93 | sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X", |
| 94 | _commitTimestamp.month, _commitTimestamp.day, |
| 95 | _createTimestamp.yearMSB, _commitTimestamp.yearLSB, |
| 96 | _commitTimestamp.hour, _commitTimestamp.minutes, |
| 97 | _commitTimestamp.seconds); |
| 98 | std::string phCommitTStr(tmpPhVal); |
| 99 | sprintf(tmpPhVal, "%c", _creatorID); |
| 100 | std::string creator(tmpPhVal); |
| 101 | creator = pv::creatorIDs.count(creator) ? pv::creatorIDs.at(creator) |
| 102 | : "Unknown CreatorID"; |
| 103 | std::string phCreatorVersionStr = |
| 104 | std::string(reinterpret_cast<const char*>(_creatorVersion.version)); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 105 | |
Aatir | c92b4eb | 2019-11-18 13:44:51 -0600 | [diff] [blame] | 106 | sprintf(tmpPhVal, "0x%X", _header.componentID); |
| 107 | std::string phCbStr(tmpPhVal); |
| 108 | sprintf(tmpPhVal, "%d", _header.subType); |
| 109 | std::string phStStr(tmpPhVal); |
| 110 | sprintf(tmpPhVal, "%d", privateHeaderVersion); |
| 111 | std::string phVerStr(tmpPhVal); |
| 112 | sprintf(tmpPhVal, "0x%X", _plid); |
| 113 | std::string phPlatformIDStr(tmpPhVal); |
| 114 | sprintf(tmpPhVal, "0x%X", _id); |
| 115 | std::string phLogEntryIDStr(tmpPhVal); |
| 116 | std::string ph = "{\"Section Version\": \"" + phVerStr + |
| 117 | "\"}, \n {\"Sub-section type\": \"" + phStStr + |
| 118 | "\"}, \n " |
| 119 | "{\"Log Committed by\": \"" + |
| 120 | phCbStr + "\"}, \n {\"Entry Creation\": \"" + |
| 121 | phCreateTStr + "\"}, \n {\"Entry Commit\": \"" + |
| 122 | phCommitTStr + "\"}, \n {\"Creator ID\": \"" + creator + |
| 123 | "\"}, \n {\"Creator Implementation\": \"" + |
| 124 | phCreatorVersionStr + "\"},\n {\"Platform Log ID\": \"" + |
| 125 | phPlatformIDStr + "\"},\n {\"Log Entry ID\": \"" + |
| 126 | phLogEntryIDStr + "\"}"; |
| 127 | |
| 128 | return ph; |
| 129 | } |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 130 | void PrivateHeader::validate() |
| 131 | { |
| 132 | bool failed = false; |
| 133 | |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 134 | if (header().id != static_cast<uint16_t>(SectionID::privateHeader)) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 135 | { |
| 136 | log<level::ERR>("Invalid private header section ID", |
| 137 | entry("ID=0x%X", header().id)); |
| 138 | failed = true; |
| 139 | } |
| 140 | |
| 141 | if (header().version != privateHeaderVersion) |
| 142 | { |
| 143 | log<level::ERR>("Invalid private header version", |
| 144 | entry("VERSION=0x%X", header().version)); |
| 145 | failed = true; |
| 146 | } |
| 147 | |
| 148 | if (_sectionCount < minSectionCount) |
| 149 | { |
| 150 | log<level::ERR>("Invalid section count in private header", |
| 151 | entry("SECTION_COUNT=0x%X", _sectionCount)); |
| 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 |