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  */
16 #include "user_header.hpp"
17 
18 #include "json_utils.hpp"
19 #include "pel_types.hpp"
20 #include "pel_values.hpp"
21 #include "severity.hpp"
22 
23 #include <fmt/format.h>
24 
25 #include <phosphor-logging/log.hpp>
26 
27 #include <iostream>
28 
29 namespace openpower
30 {
31 namespace pels
32 {
33 
34 namespace pv = openpower::pels::pel_values;
35 using namespace phosphor::logging;
36 
37 void UserHeader::unflatten(Stream& stream)
38 {
39     stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >>
40         _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >>
41         _actionFlags >> _states;
42 }
43 
44 void UserHeader::flatten(Stream& stream) const
45 {
46     stream << _header << _eventSubsystem << _eventScope << _eventSeverity
47            << _eventType << _reserved4Byte1 << _problemDomain << _problemVector
48            << _actionFlags << _states;
49 }
50 
51 UserHeader::UserHeader(const message::Entry& entry,
52                        phosphor::logging::Entry::Level severity,
53                        const AdditionalData& additionalData,
54                        const DataInterfaceBase& dataIface)
55 {
56     _header.id = static_cast<uint16_t>(SectionID::userHeader);
57     _header.size = UserHeader::flattenedSize();
58     _header.version = userHeaderVersion;
59     _header.subType = 0;
60     _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
61 
62     std::optional<uint8_t> subsys;
63 
64     // Check for additional data - PEL_SUBSYSTEM
65     auto ss = additionalData.getValue("PEL_SUBSYSTEM");
66     if (ss)
67     {
68         auto eventSubsystem = std::stoul(*ss, NULL, 16);
69         std::string subsystemString = pv::getValue(eventSubsystem,
70                                                    pel_values::subsystemValues);
71         if (subsystemString == "invalid")
72         {
73             log<level::WARNING>(
74                 fmt::format(
75                     "UH: Invalid SubSystem value in PEL_SUBSYSTEM: {:#X}",
76                     eventSubsystem)
77                     .c_str());
78         }
79         else
80         {
81             subsys = eventSubsystem;
82         }
83     }
84     else
85     {
86         subsys = entry.subsystem;
87     }
88 
89     if (subsys)
90     {
91         _eventSubsystem = *subsys;
92     }
93     else
94     {
95         // Gotta use something, how about 'others'.
96         log<level::WARNING>(
97             "No PEL subystem value supplied for error, using 'others'");
98         _eventSubsystem = 0x70;
99     }
100 
101     _eventScope = entry.eventScope.value_or(
102         static_cast<uint8_t>(EventScope::entirePlatform));
103 
104     {
105         bool mfgSevStatus = false;
106         bool mfgActionFlagStatus = false;
107 
108         // Get the mfg severity & action flags
109         if (entry.mfgSeverity || entry.mfgActionFlags)
110         {
111             std::optional<uint8_t> sev = std::nullopt;
112             uint16_t val = 0;
113 
114             if (entry.mfgSeverity)
115             {
116                 // Find the mf severity possibly dependent on the system type.
117                 sev = getSeverity(entry.mfgSeverity.value(), dataIface);
118             }
119 
120             if (entry.mfgActionFlags)
121             {
122                 // Find the mfg action flags
123                 val = entry.mfgActionFlags.value();
124             }
125 
126             if (sev || val)
127             {
128                 bool mfgProp = dataIface.getQuiesceOnError();
129                 if (mfgProp)
130                 {
131                     if (sev)
132                     {
133                         _eventSeverity = *sev;
134                         mfgSevStatus = true;
135                     }
136 
137                     if (val)
138                     {
139                         _actionFlags = val;
140                         mfgActionFlagStatus = true;
141                     }
142                 }
143             }
144         }
145 
146         if (!mfgSevStatus)
147         {
148             // Get the severity from the registry if it's there, otherwise get
149             // it from the OpenBMC event log severity value.
150             if (!entry.severity)
151             {
152                 _eventSeverity = convertOBMCSeverityToPEL(severity);
153             }
154             else
155             {
156                 // Find the severity possibly dependent on the system type.
157                 auto sev = getSeverity(entry.severity.value(), dataIface);
158                 if (sev)
159                 {
160                     _eventSeverity = *sev;
161                 }
162                 else
163                 {
164                     // Either someone  screwed up the message registry
165                     // or getSystemNames failed.
166                     log<level::ERR>(
167                         "Failed finding the severity in the message registry",
168                         phosphor::logging::entry("ERROR=%s",
169                                                  entry.name.c_str()));
170 
171                     // Have to choose something, just use informational.
172                     _eventSeverity = 0;
173                 }
174             }
175         }
176 
177         // Convert Critical error (0x50) to Critical Error-System Termination
178         // (0x51), if the AdditionalData is set to SYSTEM_TERM
179         auto sevLevel = additionalData.getValue("SEVERITY_DETAIL");
180         if ((_eventSeverity & 0xF0) == 0x50)
181         {
182             if (sevLevel.value_or("") == "SYSTEM_TERM")
183             {
184                 // Change to Critical Error, System Termination
185                 _eventSeverity = 0x51;
186             }
187         }
188 
189         if (entry.eventType)
190         {
191             _eventType = *entry.eventType;
192         }
193         else
194         {
195             // There are different default event types for info errors
196             // vs non info ones.
197             auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
198             _eventType =
199                 (sevType == SeverityType::nonError)
200                     ? static_cast<uint8_t>(EventType::miscInformational)
201                     : static_cast<uint8_t>(EventType::notApplicable);
202         }
203 
204         _reserved4Byte1 = 0;
205 
206         // No uses for problem domain or vector
207         _problemDomain = 0;
208         _problemVector = 0;
209 
210         // These will be set in pel_rules::check() if they're still
211         // at the default value.
212         if (!mfgActionFlagStatus)
213         {
214             _actionFlags = entry.actionFlags.value_or(actionFlagsDefault);
215         }
216 
217         _states = 0;
218 
219         _valid = true;
220     }
221 }
222 
223 UserHeader::UserHeader(Stream& pel)
224 {
225     try
226     {
227         unflatten(pel);
228         validate();
229     }
230     catch (const std::exception& e)
231     {
232         log<level::ERR>("Cannot unflatten user header",
233                         entry("ERROR=%s", e.what()));
234         _valid = false;
235     }
236 }
237 
238 void UserHeader::validate()
239 {
240     bool failed = false;
241     if (header().id != static_cast<uint16_t>(SectionID::userHeader))
242     {
243         log<level::ERR>("Invalid user header section ID",
244                         entry("ID=0x%X", header().id));
245         failed = true;
246     }
247 
248     if (header().version != userHeaderVersion)
249     {
250         log<level::ERR>("Invalid user header version",
251                         entry("VERSION=0x%X", header().version));
252         failed = true;
253     }
254 
255     _valid = (failed) ? false : true;
256 }
257 
258 std::optional<std::string> UserHeader::getJSON() const
259 {
260     std::string severity;
261     std::string subsystem;
262     std::string eventScope;
263     std::string eventType;
264     std::vector<std::string> actionFlags;
265     severity = pv::getValue(_eventSeverity, pel_values::severityValues);
266     subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
267     eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
268     eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
269     actionFlags = pv::getValuesBitwise(_actionFlags,
270                                        pel_values::actionFlagsValues);
271 
272     std::string hostState{"Invalid"};
273     std::string hmcState{"Invalid"};
274     auto iter = pv::transmissionStates.find(
275         static_cast<TransmissionState>(hostTransmissionState()));
276     if (iter != pv::transmissionStates.end())
277     {
278         hostState = iter->second;
279     }
280     auto iter1 = pv::transmissionStates.find(
281         static_cast<TransmissionState>(hmcTransmissionState()));
282     if (iter1 != pv::transmissionStates.end())
283     {
284         hmcState = iter1->second;
285     }
286 
287     std::string uh;
288     jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
289     jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
290     jsonInsert(uh, "Log Committed by",
291                getNumberString("0x%X", _header.componentID), 1);
292     jsonInsert(uh, "Subsystem", subsystem, 1);
293     jsonInsert(uh, "Event Scope", eventScope, 1);
294     jsonInsert(uh, "Event Severity", severity, 1);
295     jsonInsert(uh, "Event Type", eventType, 1);
296     jsonInsertArray(uh, "Action Flags", actionFlags, 1);
297     jsonInsert(uh, "Host Transmission", hostState, 1);
298     jsonInsert(uh, "HMC Transmission", hmcState, 1);
299     uh.erase(uh.size() - 2);
300     return uh;
301 }
302 
303 std::optional<uint8_t> UserHeader::getSeverity(
304     const std::vector<message::RegistrySeverity>& severities,
305     const DataInterfaceBase& dataIface) const
306 {
307     const uint8_t* s = nullptr;
308     std::vector<std::string> systemNames;
309 
310     // getSystemNames makes D-Bus calls, so only call it if we
311     // know we'll need it because there is a system name in the sev list
312     if (std::any_of(severities.begin(), severities.end(),
313                     [](const auto& sev) { return !sev.system.empty(); }))
314     {
315         try
316         {
317             systemNames = dataIface.getSystemNames();
318         }
319         catch (const std::exception& e)
320         {
321             log<level::ERR>("Failed trying to look up system names on D-Bus",
322                             entry("ERROR=%s", e.what()));
323             return std::nullopt;
324         }
325     }
326 
327     // Find the severity to use for this system type, or use the default
328     // entry (where no system type is specified).
329     for (const auto& sev : severities)
330     {
331         if (std::find(systemNames.begin(), systemNames.end(), sev.system) !=
332             systemNames.end())
333         {
334             s = &sev.severity;
335             break;
336         }
337         else if (sev.system.empty())
338         {
339             s = &sev.severity;
340         }
341     }
342 
343     if (s)
344     {
345         return *s;
346     }
347 
348     return std::nullopt;
349 }
350 
351 } // namespace pels
352 } // namespace openpower
353