blob: 6462598050ca519d5f02840d5b86ab1979df40e6 [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
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 Spinlerf9bae182019-10-09 13:37:38 -050016#include "src.hpp"
17
Matt Spinler717de422020-06-04 13:10:14 -050018#include "device_callouts.hpp"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080019#include "json_utils.hpp"
20#include "paths.hpp"
21#include "pel_values.hpp"
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080022#ifdef PELTOOL
23#include <Python.h>
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080024
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080025#include <fifo_map.hpp>
26#include <nlohmann/json.hpp>
27#include <sstream>
28#endif
Matt Spinler5a90a952020-08-27 09:39:03 -050029#include <fmt/format.h>
30
Matt Spinlerf9bae182019-10-09 13:37:38 -050031#include <phosphor-logging/log.hpp>
32
33namespace openpower
34{
35namespace pels
36{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080037namespace pv = openpower::pels::pel_values;
38namespace rg = openpower::pels::message;
Matt Spinlerf9bae182019-10-09 13:37:38 -050039using namespace phosphor::logging;
Matt Spinler85f61a62020-06-03 16:28:55 -050040using namespace std::string_literals;
Matt Spinlerf9bae182019-10-09 13:37:38 -050041
Matt Spinler075e5ba2020-02-21 15:46:00 -060042constexpr size_t ccinSize = 4;
43
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080044#ifdef PELTOOL
45// Use fifo_map as nlohmann::json's map. We are just ignoring the 'less'
46// compare. With this map the keys are kept in FIFO order.
47template <class K, class V, class dummy_compare, class A>
48using fifoMap = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
49using fifoJSON = nlohmann::basic_json<fifoMap>;
50
51void pyDecRef(PyObject* pyObj)
52{
53 Py_XDECREF(pyObj);
54}
55
56/**
57 * @brief Returns a JSON string to append to SRC section.
58 *
59 * The returning string will contain a JSON object, but without
60 * the outer {}. If the input JSON isn't a JSON object (dict), then
61 * one will be created with the input added to a 'SRC Details' key.
62 *
63 * @param[in] json - The JSON to convert to a string
64 *
65 * @return std::string - The JSON string
66 */
67std::string prettyJSON(const fifoJSON& json)
68{
69 fifoJSON output;
70 if (!json.is_object())
71 {
72 output["SRC Details"] = json;
73 }
74 else
75 {
76 for (const auto& [key, value] : json.items())
77 {
78 output[key] = value;
79 }
80 }
81
82 // Let nlohmann do the pretty printing.
83 std::stringstream stream;
84 stream << std::setw(4) << output;
85
86 auto jsonString = stream.str();
87
88 // Now it looks like:
89 // {
90 // "Key": "Value",
91 // ...
92 // }
93
94 // Replace the { and the following newline, and the } and its
95 // preceeding newline.
96 jsonString.erase(0, 2);
97
98 auto pos = jsonString.find_last_of('}');
99 jsonString.erase(pos - 1);
100
101 return jsonString;
102}
103
104/**
105 * @brief Call Python modules to parse the data into a JSON string
106 *
107 * The module to call is based on the Creator Subsystem ID under the namespace
108 * "srcparsers". For example: "srcparsers.xsrc.xsrc" where "x" is the Creator
109 * Subsystem ID in ASCII lowercase.
110 *
111 * All modules must provide the following:
112 * Function: parseSRCToJson
113 * Argument list:
114 * 1. (str) ASCII string (Hex Word 1)
115 * 2. (str) Hex Word 2
116 * 3. (str) Hex Word 3
117 * 4. (str) Hex Word 4
118 * 5. (str) Hex Word 5
119 * 6. (str) Hex Word 6
120 * 7. (str) Hex Word 7
121 * 8. (str) Hex Word 8
122 * 9. (str) Hex Word 9
123 *-Return data:
124 * 1. (str) JSON string
125 *
126 * @param[in] hexwords - Vector of strings of Hexwords 1-9
127 * @param[in] creatorID - The creatorID from the Private Header section
128 * @return std::optional<std::string> - The JSON string if it could be created,
129 * else std::nullopt
130 */
131std::optional<std::string> getPythonJSON(std::vector<std::string>& hexwords,
132 uint8_t creatorID)
133{
134 PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pResult, *pBytes,
135 *eType, *eValue, *eTraceback;
136 std::string pErrStr;
137 std::string module = getNumberString("%c", tolower(creatorID)) + "src";
138 pName = PyUnicode_FromString(
139 std::string("srcparsers." + module + "." + module).c_str());
140 std::unique_ptr<PyObject, decltype(&pyDecRef)> modNamePtr(pName, &pyDecRef);
141 pModule = PyImport_Import(pName);
142 std::unique_ptr<PyObject, decltype(&pyDecRef)> modPtr(pModule, &pyDecRef);
143 if (pModule == NULL)
144 {
145 pErrStr = "No error string found";
146 PyErr_Fetch(&eType, &eValue, &eTraceback);
147 if (eValue)
148 {
149 PyObject* pStr = PyObject_Str(eValue);
150 if (pStr)
151 {
152 pErrStr = PyUnicode_AsUTF8(pStr);
153 }
154 Py_XDECREF(pStr);
155 }
156 }
157 else
158 {
159 pDict = PyModule_GetDict(pModule);
160 pFunc = PyDict_GetItemString(pDict, "parseSRCToJson");
161 if (PyCallable_Check(pFunc))
162 {
163 pArgs = PyTuple_New(9);
164 std::unique_ptr<PyObject, decltype(&pyDecRef)> argPtr(pArgs,
165 &pyDecRef);
166 for (size_t i = 0; i < 9; i++)
167 {
168 if (i < hexwords.size())
169 {
170 auto arg = hexwords[i];
171 PyTuple_SetItem(pArgs, i,
172 Py_BuildValue("s#", arg.c_str(), 8));
173 }
174 else
175 {
176 PyTuple_SetItem(pArgs, i, Py_BuildValue("s", "00000000"));
177 }
178 }
179 pResult = PyObject_CallObject(pFunc, pArgs);
180 std::unique_ptr<PyObject, decltype(&pyDecRef)> resPtr(pResult,
181 &pyDecRef);
182 if (pResult)
183 {
184 pBytes = PyUnicode_AsEncodedString(pResult, "utf-8", "~E~");
185 std::unique_ptr<PyObject, decltype(&pyDecRef)> pyBytePtr(
186 pBytes, &pyDecRef);
187 const char* output = PyBytes_AS_STRING(pBytes);
188 try
189 {
190 fifoJSON json = nlohmann::json::parse(output);
191 return prettyJSON(json);
192 }
193 catch (std::exception& e)
194 {
195 log<level::ERR>("Bad JSON from parser",
196 entry("ERROR=%s", e.what()),
197 entry("SRC=%s", hexwords.front().c_str()),
198 entry("PARSER_MODULE=%s", module.c_str()));
199 return std::nullopt;
200 }
201 }
202 else
203 {
204 pErrStr = "No error string found";
205 PyErr_Fetch(&eType, &eValue, &eTraceback);
206 if (eValue)
207 {
208 PyObject* pStr = PyObject_Str(eValue);
209 if (pStr)
210 {
211 pErrStr = PyUnicode_AsUTF8(pStr);
212 }
213 Py_XDECREF(pStr);
214 }
215 }
216 }
217 }
218 if (!pErrStr.empty())
219 {
220 log<level::ERR>("Python exception thrown by parser",
221 entry("ERROR=%s", pErrStr.c_str()),
222 entry("SRC=%s", hexwords.front().c_str()),
223 entry("PARSER_MODULE=%s", module.c_str()));
224 }
225 Py_XDECREF(eType);
226 Py_XDECREF(eValue);
227 Py_XDECREF(eTraceback);
228 return std::nullopt;
229}
230#endif
231
Matt Spinlerf9bae182019-10-09 13:37:38 -0500232void SRC::unflatten(Stream& stream)
233{
234 stream >> _header >> _version >> _flags >> _reserved1B >> _wordCount >>
235 _reserved2B >> _size;
236
237 for (auto& word : _hexData)
238 {
239 stream >> word;
240 }
241
242 _asciiString = std::make_unique<src::AsciiString>(stream);
243
244 if (hasAdditionalSections())
245 {
246 // The callouts section is currently the only extra subsection type
247 _callouts = std::make_unique<src::Callouts>(stream);
248 }
249}
250
Matt Spinler06885452019-11-06 10:35:42 -0600251void SRC::flatten(Stream& stream) const
Matt Spinlerf9bae182019-10-09 13:37:38 -0500252{
253 stream << _header << _version << _flags << _reserved1B << _wordCount
254 << _reserved2B << _size;
255
256 for (auto& word : _hexData)
257 {
258 stream << word;
259 }
260
261 _asciiString->flatten(stream);
262
263 if (_callouts)
264 {
265 _callouts->flatten(stream);
266 }
267}
268
269SRC::SRC(Stream& pel)
270{
271 try
272 {
273 unflatten(pel);
274 validate();
275 }
276 catch (const std::exception& e)
277 {
278 log<level::ERR>("Cannot unflatten SRC", entry("ERROR=%s", e.what()));
279 _valid = false;
280 }
281}
282
Matt Spinler075e5ba2020-02-21 15:46:00 -0600283SRC::SRC(const message::Entry& regEntry, const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500284 const nlohmann::json& jsonCallouts, const DataInterfaceBase& dataIface)
Matt Spinlerbd716f02019-10-15 10:54:11 -0500285{
286 _header.id = static_cast<uint16_t>(SectionID::primarySRC);
287 _header.version = srcSectionVersion;
288 _header.subType = srcSectionSubtype;
289 _header.componentID = regEntry.componentID;
290
291 _version = srcVersion;
292
293 _flags = 0;
294 if (regEntry.src.powerFault.value_or(false))
295 {
296 _flags |= powerFaultEvent;
297 }
298
299 _reserved1B = 0;
300
301 _wordCount = numSRCHexDataWords + 1;
302
303 _reserved2B = 0;
304
305 // There are multiple fields encoded in the hex data words.
306 std::for_each(_hexData.begin(), _hexData.end(),
307 [](auto& word) { word = 0; });
Matt Spinler7c619182020-07-27 15:15:11 -0500308
309 // Hex Word 2 Nibbles:
310 // MIGVEPFF
311 // M: Partition dump status = 0
312 // I: System boot state = TODO
313 // G: Partition Boot type = 0
314 // V: BMC dump status = TODO
315 // E: Platform boot mode = 0 (side = temporary, speed = fast)
316 // P: Platform dump status = TODO
317 // FF: SRC format, set below
318
Matt Spinlerbd716f02019-10-15 10:54:11 -0500319 setBMCFormat();
320 setBMCPosition();
Matt Spinler075e5ba2020-02-21 15:46:00 -0600321 setMotherboardCCIN(dataIface);
322
Matt Spinlerbd716f02019-10-15 10:54:11 -0500323 // Fill in the last 4 words from the AdditionalData property contents.
324 setUserDefinedHexWords(regEntry, additionalData);
325
326 _asciiString = std::make_unique<src::AsciiString>(regEntry);
327
Matt Spinler5a90a952020-08-27 09:39:03 -0500328 addCallouts(regEntry, additionalData, jsonCallouts, dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500329
330 _size = baseSRCSize;
331 _size += _callouts ? _callouts->flattenedSize() : 0;
332 _header.size = Section::flattenedSize() + _size;
333
334 _valid = true;
335}
336
337void SRC::setUserDefinedHexWords(const message::Entry& regEntry,
338 const AdditionalData& ad)
339{
340 if (!regEntry.src.hexwordADFields)
341 {
342 return;
343 }
344
345 // Save the AdditionalData value corresponding to the
346 // adName key in _hexData[wordNum].
347 for (const auto& [wordNum, adName] : *regEntry.src.hexwordADFields)
348 {
349 // Can only set words 6 - 9
350 if (!isUserDefinedWord(wordNum))
351 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500352 std::string msg =
353 "SRC user data word out of range: " + std::to_string(wordNum);
354 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500355 continue;
356 }
357
358 auto value = ad.getValue(adName);
359 if (value)
360 {
361 _hexData[getWordIndexFromWordNum(wordNum)] =
362 std::strtoul(value.value().c_str(), nullptr, 0);
363 }
364 else
365 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500366 std::string msg =
367 "Source for user data SRC word not found: " + adName;
368 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500369 }
370 }
371}
372
Matt Spinler075e5ba2020-02-21 15:46:00 -0600373void SRC::setMotherboardCCIN(const DataInterfaceBase& dataIface)
374{
375 uint32_t ccin = 0;
376 auto ccinString = dataIface.getMotherboardCCIN();
377
378 try
379 {
380 if (ccinString.size() == ccinSize)
381 {
382 ccin = std::stoi(ccinString, 0, 16);
383 }
384 }
385 catch (std::exception& e)
386 {
387 log<level::WARNING>("Could not convert motherboard CCIN to a number",
388 entry("CCIN=%s", ccinString.c_str()));
389 return;
390 }
391
392 // Set the first 2 bytes
393 _hexData[1] |= ccin << 16;
394}
395
Matt Spinlerf9bae182019-10-09 13:37:38 -0500396void SRC::validate()
397{
398 bool failed = false;
399
400 if ((header().id != static_cast<uint16_t>(SectionID::primarySRC)) &&
401 (header().id != static_cast<uint16_t>(SectionID::secondarySRC)))
402 {
403 log<level::ERR>("Invalid SRC section ID",
404 entry("ID=0x%X", header().id));
405 failed = true;
406 }
407
408 // Check the version in the SRC, not in the header
Matt Spinlerbd716f02019-10-15 10:54:11 -0500409 if (_version != srcVersion)
Matt Spinlerf9bae182019-10-09 13:37:38 -0500410 {
Matt Spinlerbd716f02019-10-15 10:54:11 -0500411 log<level::ERR>("Invalid SRC version", entry("VERSION=0x%X", _version));
Matt Spinlerf9bae182019-10-09 13:37:38 -0500412 failed = true;
413 }
414
415 _valid = failed ? false : true;
416}
417
Matt Spinler075e5ba2020-02-21 15:46:00 -0600418bool SRC::isBMCSRC() const
419{
420 auto as = asciiString();
421 if (as.length() >= 2)
422 {
423 uint8_t errorType = strtoul(as.substr(0, 2).c_str(), nullptr, 16);
424 return (errorType == static_cast<uint8_t>(SRCType::bmcError) ||
425 errorType == static_cast<uint8_t>(SRCType::powerError));
426 }
427 return false;
428}
429
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800430std::optional<std::string> SRC::getErrorDetails(message::Registry& registry,
431 DetailLevel type,
432 bool toCache) const
433{
434 const std::string jsonIndent(indentLevel, 0x20);
435 std::string errorOut;
Matt Spinler075e5ba2020-02-21 15:46:00 -0600436 if (isBMCSRC())
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800437 {
438 auto entry = registry.lookup("0x" + asciiString().substr(4, 4),
439 rg::LookupType::reasonCode, toCache);
440 if (entry)
441 {
442 errorOut.append(jsonIndent + "\"Error Details\": {\n");
443 auto errorMsg = getErrorMessage(*entry);
444 if (errorMsg)
445 {
446 if (type == DetailLevel::message)
447 {
448 return errorMsg.value();
449 }
450 else
451 {
452 jsonInsert(errorOut, "Message", errorMsg.value(), 2);
453 }
454 }
455 if (entry->src.hexwordADFields)
456 {
457 std::map<size_t, std::string> adFields =
458 entry->src.hexwordADFields.value();
459 for (const auto& hexwordMap : adFields)
460 {
461 jsonInsert(errorOut, hexwordMap.second,
462 getNumberString("0x%X",
463 _hexData[getWordIndexFromWordNum(
464 hexwordMap.first)]),
465 2);
466 }
467 }
468 errorOut.erase(errorOut.size() - 2);
469 errorOut.append("\n");
470 errorOut.append(jsonIndent + "},\n");
471 return errorOut;
472 }
473 }
474 return std::nullopt;
475}
476
477std::optional<std::string>
478 SRC::getErrorMessage(const message::Entry& regEntry) const
479{
480 try
481 {
482 if (regEntry.doc.messageArgSources)
483 {
484 size_t msgLen = regEntry.doc.message.length();
485 char msg[msgLen + 1];
486 strcpy(msg, regEntry.doc.message.c_str());
487 std::vector<uint32_t> argSourceVals;
488 std::string message;
489 const auto& argValues = regEntry.doc.messageArgSources.value();
490 for (size_t i = 0; i < argValues.size(); ++i)
491 {
492 argSourceVals.push_back(_hexData[getWordIndexFromWordNum(
493 argValues[i].back() - '0')]);
494 }
495 const char* msgPointer = msg;
496 while (*msgPointer)
497 {
498 if (*msgPointer == '%')
499 {
500 msgPointer++;
501 size_t wordIndex = *msgPointer - '0';
502 if (isdigit(*msgPointer) && wordIndex >= 1 &&
503 static_cast<uint16_t>(wordIndex) <=
504 argSourceVals.size())
505 {
506 message.append(getNumberString(
507 "0x%X", argSourceVals[wordIndex - 1]));
508 }
509 else
510 {
511 message.append("%" + std::string(1, *msgPointer));
512 }
513 }
514 else
515 {
516 message.push_back(*msgPointer);
517 }
518 msgPointer++;
519 }
520 return message;
521 }
522 else
523 {
524 return regEntry.doc.message;
525 }
526 }
527 catch (const std::exception& e)
528 {
529 log<level::ERR>("Cannot get error message from registry entry",
530 entry("ERROR=%s", e.what()));
531 }
532 return std::nullopt;
533}
534
535std::optional<std::string> SRC::getCallouts() const
536{
537 if (!_callouts)
538 {
539 return std::nullopt;
540 }
541 std::string printOut;
542 const std::string jsonIndent(indentLevel, 0x20);
543 const auto& callout = _callouts->callouts();
544 const auto& compDescrp = pv::failingComponentType;
545 printOut.append(jsonIndent + "\"Callout Section\": {\n");
546 jsonInsert(printOut, "Callout Count", std::to_string(callout.size()), 2);
547 printOut.append(jsonIndent + jsonIndent + "\"Callouts\": [");
548 for (auto& entry : callout)
549 {
550 printOut.append("{\n");
551 if (entry->fruIdentity())
552 {
553 jsonInsert(
554 printOut, "FRU Type",
555 compDescrp.at(entry->fruIdentity()->failingComponentType()), 3);
556 jsonInsert(printOut, "Priority",
557 pv::getValue(entry->priority(),
558 pel_values::calloutPriorityValues),
559 3);
560 if (!entry->locationCode().empty())
561 {
562 jsonInsert(printOut, "Location Code", entry->locationCode(), 3);
563 }
564 if (entry->fruIdentity()->getPN().has_value())
565 {
566 jsonInsert(printOut, "Part Number",
567 entry->fruIdentity()->getPN().value(), 3);
568 }
569 if (entry->fruIdentity()->getMaintProc().has_value())
570 {
571 jsonInsert(printOut, "Procedure Number",
572 entry->fruIdentity()->getMaintProc().value(), 3);
573 if (pv::procedureDesc.find(
574 entry->fruIdentity()->getMaintProc().value()) !=
575 pv::procedureDesc.end())
576 {
577 jsonInsert(
578 printOut, "Description",
579 pv::procedureDesc.at(
580 entry->fruIdentity()->getMaintProc().value()),
581 3);
582 }
583 }
584 if (entry->fruIdentity()->getCCIN().has_value())
585 {
586 jsonInsert(printOut, "CCIN",
587 entry->fruIdentity()->getCCIN().value(), 3);
588 }
589 if (entry->fruIdentity()->getSN().has_value())
590 {
591 jsonInsert(printOut, "Serial Number",
592 entry->fruIdentity()->getSN().value(), 3);
593 }
594 }
595 if (entry->pceIdentity())
596 {
597 const auto& pceIdentMtms = entry->pceIdentity()->mtms();
598 if (!pceIdentMtms.machineTypeAndModel().empty())
599 {
600 jsonInsert(printOut, "PCE MTMS",
601 pceIdentMtms.machineTypeAndModel() + "_" +
602 pceIdentMtms.machineSerialNumber(),
603 3);
604 }
605 if (!entry->pceIdentity()->enclosureName().empty())
606 {
607 jsonInsert(printOut, "PCE Name",
608 entry->pceIdentity()->enclosureName(), 3);
609 }
610 }
611 if (entry->mru())
612 {
613 const auto& mruCallouts = entry->mru()->mrus();
614 std::string mruId;
615 for (auto& element : mruCallouts)
616 {
617 if (!mruId.empty())
618 {
619 mruId.append(", " + getNumberString("%08X", element.id));
620 }
621 else
622 {
623 mruId.append(getNumberString("%08X", element.id));
624 }
625 }
626 jsonInsert(printOut, "MRU Id", mruId, 3);
627 }
628 printOut.erase(printOut.size() - 2);
629 printOut.append("\n" + jsonIndent + jsonIndent + "}, ");
630 };
631 printOut.erase(printOut.size() - 2);
632 printOut.append("]\n" + jsonIndent + "}");
633 return printOut;
634}
635
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800636std::optional<std::string> SRC::getJSON(message::Registry& registry,
637 const std::vector<std::string>& plugins,
638 uint8_t creatorID) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800639{
640 std::string ps;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800641 std::vector<std::string> hexwords;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800642 jsonInsert(ps, pv::sectionVer, getNumberString("%d", _header.version), 1);
643 jsonInsert(ps, pv::subSection, getNumberString("%d", _header.subType), 1);
644 jsonInsert(ps, pv::createdBy, getNumberString("0x%X", _header.componentID),
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800645 1);
646 jsonInsert(ps, "SRC Version", getNumberString("0x%02X", _version), 1);
Harisuddin Mohamed Isac32e5512020-02-06 18:05:21 +0800647 jsonInsert(ps, "SRC Format", getNumberString("0x%02X", _hexData[0] & 0xFF),
648 1);
649 jsonInsert(ps, "Virtual Progress SRC",
650 pv::boolString.at(_flags & virtualProgressSRC), 1);
651 jsonInsert(ps, "I5/OS Service Event Bit",
652 pv::boolString.at(_flags & i5OSServiceEventBit), 1);
653 jsonInsert(ps, "Hypervisor Dump Initiated",
654 pv::boolString.at(_flags & hypDumpInit), 1);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800655 jsonInsert(ps, "Power Control Net Fault",
656 pv::boolString.at(isPowerFaultEvent()), 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600657
658 if (isBMCSRC())
659 {
660 std::string ccinString;
661 uint32_t ccin = _hexData[1] >> 16;
662
663 if (ccin)
664 {
665 ccinString = getNumberString("%04X", ccin);
666 }
667 // The PEL spec calls it a backplane, so call it that here.
668 jsonInsert(ps, "Backplane CCIN", ccinString, 1);
669 }
670
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800671 auto errorDetails = getErrorDetails(registry, DetailLevel::json, true);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800672 if (errorDetails)
673 {
674 ps.append(errorDetails.value());
675 }
676 jsonInsert(ps, "Valid Word Count", getNumberString("0x%02X", _wordCount),
677 1);
678 std::string refcode = asciiString();
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800679 hexwords.push_back(refcode);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800680 std::string extRefcode;
681 size_t pos = refcode.find(0x20);
682 if (pos != std::string::npos)
683 {
684 size_t nextPos = refcode.find_first_not_of(0x20, pos);
685 if (nextPos != std::string::npos)
686 {
687 extRefcode = trimEnd(refcode.substr(nextPos));
688 }
689 refcode.erase(pos);
690 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800691 jsonInsert(ps, "Reference Code", refcode, 1);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800692 if (!extRefcode.empty())
693 {
694 jsonInsert(ps, "Extended Reference Code", extRefcode, 1);
695 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800696 for (size_t i = 2; i <= _wordCount; i++)
697 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800698 std::string tmpWord =
699 getNumberString("%08X", _hexData[getWordIndexFromWordNum(i)]);
700 jsonInsert(ps, "Hex Word " + std::to_string(i), tmpWord, 1);
701 hexwords.push_back(tmpWord);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800702 }
703 auto calloutJson = getCallouts();
704 if (calloutJson)
705 {
706 ps.append(calloutJson.value());
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800707 ps.append(",\n");
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800708 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800709 std::string subsystem = getNumberString("%c", tolower(creatorID));
710 bool srcDetailExists = false;
711#ifdef PELTOOL
712 if (std::find(plugins.begin(), plugins.end(), subsystem + "src") !=
713 plugins.end())
714 {
715 auto pyJson = getPythonJSON(hexwords, creatorID);
716 if (pyJson)
717 {
718 ps.append(pyJson.value());
719 srcDetailExists = true;
720 }
721 }
722#endif
723 if (!srcDetailExists)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800724 {
725 ps.erase(ps.size() - 2);
726 }
727 return ps;
728}
729
Matt Spinler03984582020-04-09 13:17:58 -0500730void SRC::addCallouts(const message::Entry& regEntry,
731 const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500732 const nlohmann::json& jsonCallouts,
Matt Spinlered046852020-03-13 13:58:15 -0500733 const DataInterfaceBase& dataIface)
734{
735 auto item = additionalData.getValue("CALLOUT_INVENTORY_PATH");
736 if (item)
737 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500738 addInventoryCallout(*item, std::nullopt, std::nullopt, dataIface);
Matt Spinlered046852020-03-13 13:58:15 -0500739 }
740
Matt Spinler717de422020-06-04 13:10:14 -0500741 addDevicePathCallouts(additionalData, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -0500742
743 if (regEntry.callouts)
744 {
745 addRegistryCallouts(regEntry, additionalData, dataIface);
746 }
Matt Spinler5a90a952020-08-27 09:39:03 -0500747
748 if (!jsonCallouts.empty())
749 {
750 addJSONCallouts(jsonCallouts, dataIface);
751 }
Matt Spinlered046852020-03-13 13:58:15 -0500752}
753
754void SRC::addInventoryCallout(const std::string& inventoryPath,
Matt Spinleraf191c72020-06-04 11:35:13 -0500755 const std::optional<CalloutPriority>& priority,
756 const std::optional<std::string>& locationCode,
Matt Spinlered046852020-03-13 13:58:15 -0500757 const DataInterfaceBase& dataIface)
758{
759 std::string locCode;
760 std::string fn;
761 std::string ccin;
762 std::string sn;
763 std::unique_ptr<src::Callout> callout;
764
Matt Spinlered046852020-03-13 13:58:15 -0500765 try
766 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500767 // Use the passed in location code if there otherwise look it up
768 if (locationCode)
769 {
770 locCode = *locationCode;
771 }
772 else
773 {
774 locCode = dataIface.getLocationCode(inventoryPath);
775 }
Matt Spinlered046852020-03-13 13:58:15 -0500776
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500777 try
778 {
779 dataIface.getHWCalloutFields(inventoryPath, fn, ccin, sn);
780
Matt Spinleraf191c72020-06-04 11:35:13 -0500781 CalloutPriority p =
782 priority ? priority.value() : CalloutPriority::high;
783
784 callout = std::make_unique<src::Callout>(p, locCode, fn, ccin, sn);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500785 }
786 catch (const SdBusError& e)
787 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500788 std::string msg =
789 "No VPD found for " + inventoryPath + ": " + e.what();
790 addDebugData(msg);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500791
792 // Just create the callout with empty FRU fields
793 callout = std::make_unique<src::Callout>(CalloutPriority::high,
794 locCode, fn, ccin, sn);
795 }
Matt Spinlered046852020-03-13 13:58:15 -0500796 }
797 catch (const SdBusError& e)
798 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500799 std::string msg = "Could not get location code for " + inventoryPath +
800 ": " + e.what();
801 addDebugData(msg);
Matt Spinlered046852020-03-13 13:58:15 -0500802
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500803 // If this were to happen, people would have to look in the UserData
804 // section that contains CALLOUT_INVENTORY_PATH to see what failed.
Matt Spinlered046852020-03-13 13:58:15 -0500805 callout = std::make_unique<src::Callout>(CalloutPriority::high,
Matt Spinlera27e2e52020-04-09 11:06:11 -0500806 "no_vpd_for_fru");
Matt Spinlered046852020-03-13 13:58:15 -0500807 }
808
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500809 createCalloutsObject();
Matt Spinlered046852020-03-13 13:58:15 -0500810 _callouts->addCallout(std::move(callout));
Matt Spinler03984582020-04-09 13:17:58 -0500811}
Matt Spinlered046852020-03-13 13:58:15 -0500812
Matt Spinler03984582020-04-09 13:17:58 -0500813void SRC::addRegistryCallouts(const message::Entry& regEntry,
814 const AdditionalData& additionalData,
815 const DataInterfaceBase& dataIface)
816{
817 try
818 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500819 auto systemNames = dataIface.getSystemNames();
Matt Spinler03984582020-04-09 13:17:58 -0500820
821 auto regCallouts = message::Registry::getCallouts(
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500822 regEntry.callouts.value(), systemNames, additionalData);
Matt Spinler03984582020-04-09 13:17:58 -0500823
824 for (const auto& regCallout : regCallouts)
825 {
826 addRegistryCallout(regCallout, dataIface);
827 }
828 }
829 catch (std::exception& e)
830 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500831 std::string msg =
832 "Error parsing PEL message registry callout JSON: "s + e.what();
833 addDebugData(msg);
Matt Spinler03984582020-04-09 13:17:58 -0500834 }
835}
836
837void SRC::addRegistryCallout(const message::RegistryCallout& regCallout,
838 const DataInterfaceBase& dataIface)
839{
840 std::unique_ptr<src::Callout> callout;
Matt Spinler03984582020-04-09 13:17:58 -0500841 auto locCode = regCallout.locCode;
842
Matt Spinleraf191c72020-06-04 11:35:13 -0500843 if (!locCode.empty())
844 {
845 try
846 {
847 locCode = dataIface.expandLocationCode(locCode, 0);
848 }
849 catch (const std::exception& e)
850 {
851 auto msg =
852 "Unable to expand location code " + locCode + ": " + e.what();
853 addDebugData(msg);
854 return;
855 }
856 }
857
Matt Spinler03984582020-04-09 13:17:58 -0500858 // Via the PEL values table, get the priority enum.
859 // The schema will have validated the priority was a valid value.
860 auto priorityIt =
861 pv::findByName(regCallout.priority, pv::calloutPriorityValues);
862 assert(priorityIt != pv::calloutPriorityValues.end());
863 auto priority =
864 static_cast<CalloutPriority>(std::get<pv::fieldValuePos>(*priorityIt));
865
866 if (!regCallout.procedure.empty())
867 {
868 // Procedure callout
869 callout =
870 std::make_unique<src::Callout>(priority, regCallout.procedure);
871 }
872 else if (!regCallout.symbolicFRU.empty())
873 {
874 // Symbolic FRU callout
875 callout = std::make_unique<src::Callout>(
876 priority, regCallout.symbolicFRU, locCode, false);
877 }
878 else if (!regCallout.symbolicFRUTrusted.empty())
879 {
880 // Symbolic FRU with trusted location code callout
881
882 // The registry wants it to be trusted, but that requires a valid
883 // location code for it to actually be.
884 callout = std::make_unique<src::Callout>(
885 priority, regCallout.symbolicFRUTrusted, locCode, !locCode.empty());
886 }
887 else
888 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500889 // A hardware callout
890 std::string inventoryPath;
891
892 try
893 {
894 // Get the inventory item from the unexpanded location code
895 inventoryPath =
Matt Spinler2f9225a2020-08-05 12:58:49 -0500896 dataIface.getInventoryFromLocCode(regCallout.locCode, 0, false);
Matt Spinleraf191c72020-06-04 11:35:13 -0500897 }
898 catch (const std::exception& e)
899 {
900 std::string msg =
901 "Unable to get inventory path from location code: " + locCode +
902 ": " + e.what();
903 addDebugData(msg);
904 return;
905 }
906
907 addInventoryCallout(inventoryPath, priority, locCode, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -0500908 }
909
910 if (callout)
911 {
912 createCalloutsObject();
913 _callouts->addCallout(std::move(callout));
914 }
915}
Matt Spinlered046852020-03-13 13:58:15 -0500916
Matt Spinler717de422020-06-04 13:10:14 -0500917void SRC::addDevicePathCallouts(const AdditionalData& additionalData,
918 const DataInterfaceBase& dataIface)
919{
920 std::vector<device_callouts::Callout> callouts;
921 auto i2cBus = additionalData.getValue("CALLOUT_IIC_BUS");
922 auto i2cAddr = additionalData.getValue("CALLOUT_IIC_ADDR");
923 auto devPath = additionalData.getValue("CALLOUT_DEVICE_PATH");
924
925 // A device callout contains either:
926 // * CALLOUT_ERRNO, CALLOUT_DEVICE_PATH
927 // * CALLOUT_ERRNO, CALLOUT_IIC_BUS, CALLOUT_IIC_ADDR
928 // We don't care about the errno.
929
930 if (devPath)
931 {
932 try
933 {
934 callouts = device_callouts::getCallouts(*devPath,
935 dataIface.getSystemNames());
936 }
937 catch (const std::exception& e)
938 {
939 addDebugData(e.what());
940 callouts.clear();
941 }
942 }
943 else if (i2cBus && i2cAddr)
944 {
945 size_t bus;
946 uint8_t address;
947
948 try
949 {
950 // If /dev/i2c- is prepended, remove it
951 if (i2cBus->find("/dev/i2c-") != std::string::npos)
952 {
953 *i2cBus = i2cBus->substr(9);
954 }
955
956 bus = stoul(*i2cBus, nullptr, 0);
957 address = stoul(*i2cAddr, nullptr, 0);
958 }
959 catch (const std::exception& e)
960 {
961 std::string msg = "Invalid CALLOUT_IIC_BUS " + *i2cBus +
962 " or CALLOUT_IIC_ADDR " + *i2cAddr +
963 " in AdditionalData property";
964 addDebugData(msg);
965 return;
966 }
967
968 try
969 {
970 callouts = device_callouts::getI2CCallouts(
971 bus, address, dataIface.getSystemNames());
972 }
973 catch (const std::exception& e)
974 {
975 addDebugData(e.what());
976 callouts.clear();
977 }
978 }
979
980 for (const auto& callout : callouts)
981 {
982 // The priority shouldn't be invalid, but check just in case.
983 CalloutPriority priority = CalloutPriority::high;
984
985 if (!callout.priority.empty())
986 {
987 auto p = pel_values::findByValue(
988 static_cast<uint32_t>(callout.priority[0]),
989 pel_values::calloutPriorityValues);
990
991 if (p != pel_values::calloutPriorityValues.end())
992 {
993 priority = static_cast<CalloutPriority>(callout.priority[0]);
994 }
995 else
996 {
997 std::string msg =
998 "Invalid priority found in dev callout JSON: " +
999 callout.priority[0];
1000 addDebugData(msg);
1001 }
1002 }
1003
1004 try
1005 {
Matt Spinler2f9225a2020-08-05 12:58:49 -05001006 auto inventoryPath = dataIface.getInventoryFromLocCode(
1007 callout.locationCode, 0, false);
Matt Spinler717de422020-06-04 13:10:14 -05001008
1009 addInventoryCallout(inventoryPath, priority, std::nullopt,
1010 dataIface);
1011 }
1012 catch (const std::exception& e)
1013 {
1014 std::string msg =
1015 "Unable to get inventory path from location code: " +
1016 callout.locationCode + ": " + e.what();
1017 addDebugData(msg);
1018 }
1019
1020 // Until the code is there to convert these MRU value strings to
1021 // the official MRU values in the callout objects, just store
1022 // the MRU name in the debug UserData section.
1023 if (!callout.mru.empty())
1024 {
1025 std::string msg = "MRU: " + callout.mru;
1026 addDebugData(msg);
1027 }
1028
1029 // getCallouts() may have generated some debug data it stored
1030 // in a callout object. Save it as well.
1031 if (!callout.debug.empty())
1032 {
1033 addDebugData(callout.debug);
1034 }
1035 }
1036}
1037
Matt Spinler5a90a952020-08-27 09:39:03 -05001038void SRC::addJSONCallouts(const nlohmann::json& jsonCallouts,
1039 const DataInterfaceBase& dataIface)
1040{
1041 if (jsonCallouts.empty())
1042 {
1043 return;
1044 }
1045
1046 if (!jsonCallouts.is_array())
1047 {
1048 addDebugData("Callout JSON isn't an array");
1049 return;
1050 }
1051
1052 for (const auto& callout : jsonCallouts)
1053 {
1054 try
1055 {
1056 addJSONCallout(callout, dataIface);
1057 }
1058 catch (const std::exception& e)
1059 {
1060 addDebugData(fmt::format(
1061 "Failed extracting callout data from JSON: {}", e.what()));
1062 }
1063 }
1064}
1065
1066void SRC::addJSONCallout(const nlohmann::json& jsonCallout,
1067 const DataInterfaceBase& dataIface)
1068{
Matt Spinler3bdd0112020-08-27 10:24:34 -05001069 auto priority = getPriorityFromJSON(jsonCallout);
1070 std::string locCode;
1071 std::string unexpandedLocCode;
1072 std::unique_ptr<src::Callout> callout;
1073
1074 // Expand the location code if it's there
1075 if (jsonCallout.contains("LocationCode"))
1076 {
1077 unexpandedLocCode = jsonCallout.at("LocationCode").get<std::string>();
1078
1079 try
1080 {
1081 locCode = dataIface.expandLocationCode(unexpandedLocCode, 0);
1082 }
1083 catch (const std::exception& e)
1084 {
1085 addDebugData(fmt::format("Unable to expand location code {}: {}",
1086 unexpandedLocCode, e.what()));
1087 // Use the value from the JSON so at least there's something
1088 locCode = unexpandedLocCode;
1089 }
1090 }
1091
1092 // Create either a procedure, symbolic FRU, or normal FRU callout.
1093 if (jsonCallout.contains("Procedure"))
1094 {
1095 auto procedure = jsonCallout.at("Procedure").get<std::string>();
1096
1097 callout = std::make_unique<src::Callout>(
1098 static_cast<CalloutPriority>(priority), procedure,
1099 src::CalloutValueType::raw);
1100 }
1101 else if (jsonCallout.contains("SymbolicFRU"))
1102 {
1103 auto fru = jsonCallout.at("SymbolicFRU").get<std::string>();
1104
1105 bool trusted = false;
1106 if (jsonCallout.contains("TrustedLocationCode") && !locCode.empty())
1107 {
1108 trusted = jsonCallout.at("TrustedLocationCode").get<bool>();
1109 }
1110
1111 callout = std::make_unique<src::Callout>(
1112 static_cast<CalloutPriority>(priority), fru,
1113 src::CalloutValueType::raw, locCode, trusted);
1114 }
1115 else
1116 {
1117 // A hardware FRU
1118 std::string inventoryPath;
1119
1120 if (jsonCallout.contains("InventoryPath"))
1121 {
1122 inventoryPath = jsonCallout.at("InventoryPath").get<std::string>();
1123 }
1124 else
1125 {
1126 if (unexpandedLocCode.empty())
1127 {
1128 throw std::runtime_error{"JSON callout needs either an "
1129 "inventory path or location code"};
1130 }
1131
1132 try
1133 {
1134 inventoryPath = dataIface.getInventoryFromLocCode(
1135 unexpandedLocCode, 0, false);
1136 }
1137 catch (const std::exception& e)
1138 {
1139 throw std::runtime_error{
1140 fmt::format("Unable to get inventory path from "
1141 "location code: {}: {}",
1142 unexpandedLocCode, e.what())};
1143 }
1144 }
1145
1146 // If the location code was also passed in, use that here too
1147 // so addInventoryCallout doesn't have to look it up.
1148 std::optional<std::string> lc;
1149 if (!locCode.empty())
1150 {
1151 lc = locCode;
1152 }
1153
1154 addInventoryCallout(inventoryPath, priority, lc, dataIface);
1155 }
1156
1157 if (callout)
1158 {
1159 createCalloutsObject();
1160 _callouts->addCallout(std::move(callout));
1161 }
1162}
1163
1164CalloutPriority SRC::getPriorityFromJSON(const nlohmann::json& json)
1165{
1166 // Looks like:
1167 // {
1168 // "Priority": "H"
1169 // }
1170 auto p = json.at("Priority").get<std::string>();
1171 if (p.empty())
1172 {
1173 throw std::runtime_error{"Priority field in callout is empty"};
1174 }
1175
1176 auto priority = static_cast<CalloutPriority>(p.front());
1177
1178 // Validate it
1179 auto priorityIt = pv::findByValue(static_cast<uint32_t>(priority),
1180 pv::calloutPriorityValues);
1181 if (priorityIt == pv::calloutPriorityValues.end())
1182 {
1183 throw std::runtime_error{
1184 fmt::format("Invalid priority '{}' found in JSON callout", p)};
1185 }
1186
1187 return priority;
Matt Spinler5a90a952020-08-27 09:39:03 -05001188}
1189
Matt Spinlerf9bae182019-10-09 13:37:38 -05001190} // namespace pels
1191} // namespace openpower