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 | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 16 | #include "user_header.hpp" |
| 17 | |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 18 | #include "pel_types.hpp" |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 19 | #include "severity.hpp" |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 20 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 21 | #include <iostream> |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 22 | #include <phosphor-logging/log.hpp> |
| 23 | |
| 24 | namespace openpower |
| 25 | { |
| 26 | namespace pels |
| 27 | { |
| 28 | |
| 29 | using namespace phosphor::logging; |
| 30 | |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 31 | void UserHeader::unflatten(Stream& stream) |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 32 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 33 | stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >> |
| 34 | _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >> |
| 35 | _actionFlags >> _reserved4Byte2; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 38 | void UserHeader::flatten(Stream& stream) const |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 39 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 40 | stream << _header << _eventSubsystem << _eventScope << _eventSeverity |
| 41 | << _eventType << _reserved4Byte1 << _problemDomain << _problemVector |
| 42 | << _actionFlags << _reserved4Byte2; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 45 | UserHeader::UserHeader(const message::Entry& entry, |
| 46 | phosphor::logging::Entry::Level severity) |
| 47 | { |
| 48 | _header.id = static_cast<uint16_t>(SectionID::userHeader); |
| 49 | _header.size = UserHeader::flattenedSize(); |
| 50 | _header.version = userHeaderVersion; |
| 51 | _header.subType = 0; |
| 52 | _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging); |
| 53 | |
| 54 | _eventSubsystem = entry.subsystem; |
| 55 | |
| 56 | _eventScope = entry.eventScope.value_or( |
| 57 | static_cast<uint8_t>(EventScope::entirePlatform)); |
| 58 | |
| 59 | // Get the severity from the registry if it's there, otherwise get it |
| 60 | // from the OpenBMC event log severity value. |
| 61 | _eventSeverity = |
| 62 | entry.severity.value_or(convertOBMCSeverityToPEL(severity)); |
| 63 | |
| 64 | // TODO: ibm-dev/dev/#1144 Handle manufacturing sev & action flags |
| 65 | |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 66 | if (entry.eventType) |
| 67 | { |
| 68 | _eventType = *entry.eventType; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | // There are different default event types for info errors |
| 73 | // vs non info ones. |
| 74 | auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0); |
| 75 | |
| 76 | _eventType = (sevType == SeverityType::nonError) |
| 77 | ? static_cast<uint8_t>(EventType::miscInformational) |
| 78 | : static_cast<uint8_t>(EventType::notApplicable); |
| 79 | } |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 80 | |
| 81 | _reserved4Byte1 = 0; |
| 82 | |
| 83 | // No uses for problem domain or vector |
| 84 | _problemDomain = 0; |
| 85 | _problemVector = 0; |
| 86 | |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 87 | // These will be cleaned up later in pel_rules::check() |
Matt Spinler | e07f915 | 2019-11-01 10:48:36 -0500 | [diff] [blame] | 88 | _actionFlags = entry.actionFlags.value_or(0); |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 89 | |
| 90 | _reserved4Byte2 = 0; |
| 91 | |
| 92 | _valid = true; |
| 93 | } |
| 94 | |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 95 | UserHeader::UserHeader(Stream& pel) |
| 96 | { |
| 97 | try |
| 98 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 99 | unflatten(pel); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 100 | validate(); |
| 101 | } |
| 102 | catch (const std::exception& e) |
| 103 | { |
| 104 | log<level::ERR>("Cannot unflatten user header", |
| 105 | entry("ERROR=%s", e.what())); |
| 106 | _valid = false; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void UserHeader::validate() |
| 111 | { |
| 112 | bool failed = false; |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 113 | if (header().id != static_cast<uint16_t>(SectionID::userHeader)) |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 114 | { |
| 115 | log<level::ERR>("Invalid user header section ID", |
| 116 | entry("ID=0x%X", header().id)); |
| 117 | failed = true; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | if (header().version != userHeaderVersion) |
| 121 | { |
| 122 | log<level::ERR>("Invalid user header version", |
| 123 | entry("VERSION=0x%X", header().version)); |
| 124 | failed = true; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | _valid = (failed) ? false : true; |
| 128 | } |
| 129 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 130 | std::string UserHeader::getValue(const uint8_t field, |
| 131 | const pel_values::PELValues& values) const |
| 132 | { |
| 133 | |
| 134 | auto tmp = pel_values::findByValue(field, values); |
| 135 | if (tmp != values.end()) |
| 136 | { |
| 137 | return std::get<pel_values::registryNamePos>(*tmp); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | return "invalid"; |
| 142 | } |
| 143 | } |
| 144 | std::optional<std::string> UserHeader::getJSON() const |
| 145 | { |
| 146 | std::string severity; |
| 147 | std::string subsystem; |
| 148 | std::string eventScope; |
| 149 | std::string eventType; |
| 150 | severity = getValue(_eventSeverity, pel_values::severityValues); |
| 151 | subsystem = getValue(_eventSubsystem, pel_values::subsystemValues); |
| 152 | eventScope = getValue(_eventScope, pel_values::eventScopeValues); |
| 153 | eventType = getValue(_eventType, pel_values::eventTypeValues); |
| 154 | char tmpUhVal[8]; |
| 155 | sprintf(tmpUhVal, "%d", userHeaderVersion); |
| 156 | std::string uhVerStr(tmpUhVal); |
| 157 | sprintf(tmpUhVal, "0x%X", _header.componentID); |
| 158 | std::string uhCbStr(tmpUhVal); |
| 159 | sprintf(tmpUhVal, "%d", _header.subType); |
| 160 | std::string uhStStr(tmpUhVal); |
| 161 | |
| 162 | std::string uh = "{\"Section Version\": \"" + uhVerStr + |
| 163 | "\"}, \n {\"Sub-section type\": \"" + uhStStr + |
| 164 | "\"}, \n " |
| 165 | "{\"Log Committed by\": \"" + |
| 166 | uhCbStr + "\"}, \n {\"Subsystem\": \"" + subsystem + |
| 167 | "\"},\n " |
| 168 | "{\"Event Scope\": \"" + |
| 169 | eventScope + "\"}, \n {\"Event Severity\":\"" + severity + |
| 170 | "\"},\n {\"Event Type\": \"" + eventType + "\"}"; |
| 171 | return uh; |
| 172 | } |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 173 | } // namespace pels |
| 174 | } // namespace openpower |