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