blob: 7252b46d23dcfe0046e024cbc76a454475b7696d [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 Spinlerf9bae182019-10-09 13:37:38 -050029#include <phosphor-logging/log.hpp>
30
31namespace openpower
32{
33namespace pels
34{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080035namespace pv = openpower::pels::pel_values;
36namespace rg = openpower::pels::message;
Matt Spinlerf9bae182019-10-09 13:37:38 -050037using namespace phosphor::logging;
Matt Spinler85f61a62020-06-03 16:28:55 -050038using namespace std::string_literals;
Matt Spinlerf9bae182019-10-09 13:37:38 -050039
Matt Spinler075e5ba2020-02-21 15:46:00 -060040constexpr size_t ccinSize = 4;
41
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080042#ifdef PELTOOL
43// Use fifo_map as nlohmann::json's map. We are just ignoring the 'less'
44// compare. With this map the keys are kept in FIFO order.
45template <class K, class V, class dummy_compare, class A>
46using fifoMap = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
47using fifoJSON = nlohmann::basic_json<fifoMap>;
48
49void pyDecRef(PyObject* pyObj)
50{
51 Py_XDECREF(pyObj);
52}
53
54/**
55 * @brief Returns a JSON string to append to SRC section.
56 *
57 * The returning string will contain a JSON object, but without
58 * the outer {}. If the input JSON isn't a JSON object (dict), then
59 * one will be created with the input added to a 'SRC Details' key.
60 *
61 * @param[in] json - The JSON to convert to a string
62 *
63 * @return std::string - The JSON string
64 */
65std::string prettyJSON(const fifoJSON& json)
66{
67 fifoJSON output;
68 if (!json.is_object())
69 {
70 output["SRC Details"] = json;
71 }
72 else
73 {
74 for (const auto& [key, value] : json.items())
75 {
76 output[key] = value;
77 }
78 }
79
80 // Let nlohmann do the pretty printing.
81 std::stringstream stream;
82 stream << std::setw(4) << output;
83
84 auto jsonString = stream.str();
85
86 // Now it looks like:
87 // {
88 // "Key": "Value",
89 // ...
90 // }
91
92 // Replace the { and the following newline, and the } and its
93 // preceeding newline.
94 jsonString.erase(0, 2);
95
96 auto pos = jsonString.find_last_of('}');
97 jsonString.erase(pos - 1);
98
99 return jsonString;
100}
101
102/**
103 * @brief Call Python modules to parse the data into a JSON string
104 *
105 * The module to call is based on the Creator Subsystem ID under the namespace
106 * "srcparsers". For example: "srcparsers.xsrc.xsrc" where "x" is the Creator
107 * Subsystem ID in ASCII lowercase.
108 *
109 * All modules must provide the following:
110 * Function: parseSRCToJson
111 * Argument list:
112 * 1. (str) ASCII string (Hex Word 1)
113 * 2. (str) Hex Word 2
114 * 3. (str) Hex Word 3
115 * 4. (str) Hex Word 4
116 * 5. (str) Hex Word 5
117 * 6. (str) Hex Word 6
118 * 7. (str) Hex Word 7
119 * 8. (str) Hex Word 8
120 * 9. (str) Hex Word 9
121 *-Return data:
122 * 1. (str) JSON string
123 *
124 * @param[in] hexwords - Vector of strings of Hexwords 1-9
125 * @param[in] creatorID - The creatorID from the Private Header section
126 * @return std::optional<std::string> - The JSON string if it could be created,
127 * else std::nullopt
128 */
129std::optional<std::string> getPythonJSON(std::vector<std::string>& hexwords,
130 uint8_t creatorID)
131{
132 PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pResult, *pBytes,
133 *eType, *eValue, *eTraceback;
134 std::string pErrStr;
135 std::string module = getNumberString("%c", tolower(creatorID)) + "src";
136 pName = PyUnicode_FromString(
137 std::string("srcparsers." + module + "." + module).c_str());
138 std::unique_ptr<PyObject, decltype(&pyDecRef)> modNamePtr(pName, &pyDecRef);
139 pModule = PyImport_Import(pName);
140 std::unique_ptr<PyObject, decltype(&pyDecRef)> modPtr(pModule, &pyDecRef);
141 if (pModule == NULL)
142 {
143 pErrStr = "No error string found";
144 PyErr_Fetch(&eType, &eValue, &eTraceback);
145 if (eValue)
146 {
147 PyObject* pStr = PyObject_Str(eValue);
148 if (pStr)
149 {
150 pErrStr = PyUnicode_AsUTF8(pStr);
151 }
152 Py_XDECREF(pStr);
153 }
154 }
155 else
156 {
157 pDict = PyModule_GetDict(pModule);
158 pFunc = PyDict_GetItemString(pDict, "parseSRCToJson");
159 if (PyCallable_Check(pFunc))
160 {
161 pArgs = PyTuple_New(9);
162 std::unique_ptr<PyObject, decltype(&pyDecRef)> argPtr(pArgs,
163 &pyDecRef);
164 for (size_t i = 0; i < 9; i++)
165 {
166 if (i < hexwords.size())
167 {
168 auto arg = hexwords[i];
169 PyTuple_SetItem(pArgs, i,
170 Py_BuildValue("s#", arg.c_str(), 8));
171 }
172 else
173 {
174 PyTuple_SetItem(pArgs, i, Py_BuildValue("s", "00000000"));
175 }
176 }
177 pResult = PyObject_CallObject(pFunc, pArgs);
178 std::unique_ptr<PyObject, decltype(&pyDecRef)> resPtr(pResult,
179 &pyDecRef);
180 if (pResult)
181 {
182 pBytes = PyUnicode_AsEncodedString(pResult, "utf-8", "~E~");
183 std::unique_ptr<PyObject, decltype(&pyDecRef)> pyBytePtr(
184 pBytes, &pyDecRef);
185 const char* output = PyBytes_AS_STRING(pBytes);
186 try
187 {
188 fifoJSON json = nlohmann::json::parse(output);
189 return prettyJSON(json);
190 }
191 catch (std::exception& e)
192 {
193 log<level::ERR>("Bad JSON from parser",
194 entry("ERROR=%s", e.what()),
195 entry("SRC=%s", hexwords.front().c_str()),
196 entry("PARSER_MODULE=%s", module.c_str()));
197 return std::nullopt;
198 }
199 }
200 else
201 {
202 pErrStr = "No error string found";
203 PyErr_Fetch(&eType, &eValue, &eTraceback);
204 if (eValue)
205 {
206 PyObject* pStr = PyObject_Str(eValue);
207 if (pStr)
208 {
209 pErrStr = PyUnicode_AsUTF8(pStr);
210 }
211 Py_XDECREF(pStr);
212 }
213 }
214 }
215 }
216 if (!pErrStr.empty())
217 {
218 log<level::ERR>("Python exception thrown by parser",
219 entry("ERROR=%s", pErrStr.c_str()),
220 entry("SRC=%s", hexwords.front().c_str()),
221 entry("PARSER_MODULE=%s", module.c_str()));
222 }
223 Py_XDECREF(eType);
224 Py_XDECREF(eValue);
225 Py_XDECREF(eTraceback);
226 return std::nullopt;
227}
228#endif
229
Matt Spinlerf9bae182019-10-09 13:37:38 -0500230void SRC::unflatten(Stream& stream)
231{
232 stream >> _header >> _version >> _flags >> _reserved1B >> _wordCount >>
233 _reserved2B >> _size;
234
235 for (auto& word : _hexData)
236 {
237 stream >> word;
238 }
239
240 _asciiString = std::make_unique<src::AsciiString>(stream);
241
242 if (hasAdditionalSections())
243 {
244 // The callouts section is currently the only extra subsection type
245 _callouts = std::make_unique<src::Callouts>(stream);
246 }
247}
248
Matt Spinler06885452019-11-06 10:35:42 -0600249void SRC::flatten(Stream& stream) const
Matt Spinlerf9bae182019-10-09 13:37:38 -0500250{
251 stream << _header << _version << _flags << _reserved1B << _wordCount
252 << _reserved2B << _size;
253
254 for (auto& word : _hexData)
255 {
256 stream << word;
257 }
258
259 _asciiString->flatten(stream);
260
261 if (_callouts)
262 {
263 _callouts->flatten(stream);
264 }
265}
266
267SRC::SRC(Stream& pel)
268{
269 try
270 {
271 unflatten(pel);
272 validate();
273 }
274 catch (const std::exception& e)
275 {
276 log<level::ERR>("Cannot unflatten SRC", entry("ERROR=%s", e.what()));
277 _valid = false;
278 }
279}
280
Matt Spinler075e5ba2020-02-21 15:46:00 -0600281SRC::SRC(const message::Entry& regEntry, const AdditionalData& additionalData,
282 const DataInterfaceBase& dataIface)
Matt Spinlerbd716f02019-10-15 10:54:11 -0500283{
284 _header.id = static_cast<uint16_t>(SectionID::primarySRC);
285 _header.version = srcSectionVersion;
286 _header.subType = srcSectionSubtype;
287 _header.componentID = regEntry.componentID;
288
289 _version = srcVersion;
290
291 _flags = 0;
292 if (regEntry.src.powerFault.value_or(false))
293 {
294 _flags |= powerFaultEvent;
295 }
296
297 _reserved1B = 0;
298
299 _wordCount = numSRCHexDataWords + 1;
300
301 _reserved2B = 0;
302
303 // There are multiple fields encoded in the hex data words.
304 std::for_each(_hexData.begin(), _hexData.end(),
305 [](auto& word) { word = 0; });
Matt Spinler7c619182020-07-27 15:15:11 -0500306
307 // Hex Word 2 Nibbles:
308 // MIGVEPFF
309 // M: Partition dump status = 0
310 // I: System boot state = TODO
311 // G: Partition Boot type = 0
312 // V: BMC dump status = TODO
313 // E: Platform boot mode = 0 (side = temporary, speed = fast)
314 // P: Platform dump status = TODO
315 // FF: SRC format, set below
316
Matt Spinlerbd716f02019-10-15 10:54:11 -0500317 setBMCFormat();
318 setBMCPosition();
Matt Spinler075e5ba2020-02-21 15:46:00 -0600319 setMotherboardCCIN(dataIface);
320
Matt Spinlerbd716f02019-10-15 10:54:11 -0500321 // Fill in the last 4 words from the AdditionalData property contents.
322 setUserDefinedHexWords(regEntry, additionalData);
323
324 _asciiString = std::make_unique<src::AsciiString>(regEntry);
325
Matt Spinler03984582020-04-09 13:17:58 -0500326 addCallouts(regEntry, additionalData, dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500327
328 _size = baseSRCSize;
329 _size += _callouts ? _callouts->flattenedSize() : 0;
330 _header.size = Section::flattenedSize() + _size;
331
332 _valid = true;
333}
334
335void SRC::setUserDefinedHexWords(const message::Entry& regEntry,
336 const AdditionalData& ad)
337{
338 if (!regEntry.src.hexwordADFields)
339 {
340 return;
341 }
342
343 // Save the AdditionalData value corresponding to the
344 // adName key in _hexData[wordNum].
345 for (const auto& [wordNum, adName] : *regEntry.src.hexwordADFields)
346 {
347 // Can only set words 6 - 9
348 if (!isUserDefinedWord(wordNum))
349 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500350 std::string msg =
351 "SRC user data word out of range: " + std::to_string(wordNum);
352 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500353 continue;
354 }
355
356 auto value = ad.getValue(adName);
357 if (value)
358 {
359 _hexData[getWordIndexFromWordNum(wordNum)] =
360 std::strtoul(value.value().c_str(), nullptr, 0);
361 }
362 else
363 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500364 std::string msg =
365 "Source for user data SRC word not found: " + adName;
366 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500367 }
368 }
369}
370
Matt Spinler075e5ba2020-02-21 15:46:00 -0600371void SRC::setMotherboardCCIN(const DataInterfaceBase& dataIface)
372{
373 uint32_t ccin = 0;
374 auto ccinString = dataIface.getMotherboardCCIN();
375
376 try
377 {
378 if (ccinString.size() == ccinSize)
379 {
380 ccin = std::stoi(ccinString, 0, 16);
381 }
382 }
383 catch (std::exception& e)
384 {
385 log<level::WARNING>("Could not convert motherboard CCIN to a number",
386 entry("CCIN=%s", ccinString.c_str()));
387 return;
388 }
389
390 // Set the first 2 bytes
391 _hexData[1] |= ccin << 16;
392}
393
Matt Spinlerf9bae182019-10-09 13:37:38 -0500394void SRC::validate()
395{
396 bool failed = false;
397
398 if ((header().id != static_cast<uint16_t>(SectionID::primarySRC)) &&
399 (header().id != static_cast<uint16_t>(SectionID::secondarySRC)))
400 {
401 log<level::ERR>("Invalid SRC section ID",
402 entry("ID=0x%X", header().id));
403 failed = true;
404 }
405
406 // Check the version in the SRC, not in the header
Matt Spinlerbd716f02019-10-15 10:54:11 -0500407 if (_version != srcVersion)
Matt Spinlerf9bae182019-10-09 13:37:38 -0500408 {
Matt Spinlerbd716f02019-10-15 10:54:11 -0500409 log<level::ERR>("Invalid SRC version", entry("VERSION=0x%X", _version));
Matt Spinlerf9bae182019-10-09 13:37:38 -0500410 failed = true;
411 }
412
413 _valid = failed ? false : true;
414}
415
Matt Spinler075e5ba2020-02-21 15:46:00 -0600416bool SRC::isBMCSRC() const
417{
418 auto as = asciiString();
419 if (as.length() >= 2)
420 {
421 uint8_t errorType = strtoul(as.substr(0, 2).c_str(), nullptr, 16);
422 return (errorType == static_cast<uint8_t>(SRCType::bmcError) ||
423 errorType == static_cast<uint8_t>(SRCType::powerError));
424 }
425 return false;
426}
427
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800428std::optional<std::string> SRC::getErrorDetails(message::Registry& registry,
429 DetailLevel type,
430 bool toCache) const
431{
432 const std::string jsonIndent(indentLevel, 0x20);
433 std::string errorOut;
Matt Spinler075e5ba2020-02-21 15:46:00 -0600434 if (isBMCSRC())
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800435 {
436 auto entry = registry.lookup("0x" + asciiString().substr(4, 4),
437 rg::LookupType::reasonCode, toCache);
438 if (entry)
439 {
440 errorOut.append(jsonIndent + "\"Error Details\": {\n");
441 auto errorMsg = getErrorMessage(*entry);
442 if (errorMsg)
443 {
444 if (type == DetailLevel::message)
445 {
446 return errorMsg.value();
447 }
448 else
449 {
450 jsonInsert(errorOut, "Message", errorMsg.value(), 2);
451 }
452 }
453 if (entry->src.hexwordADFields)
454 {
455 std::map<size_t, std::string> adFields =
456 entry->src.hexwordADFields.value();
457 for (const auto& hexwordMap : adFields)
458 {
459 jsonInsert(errorOut, hexwordMap.second,
460 getNumberString("0x%X",
461 _hexData[getWordIndexFromWordNum(
462 hexwordMap.first)]),
463 2);
464 }
465 }
466 errorOut.erase(errorOut.size() - 2);
467 errorOut.append("\n");
468 errorOut.append(jsonIndent + "},\n");
469 return errorOut;
470 }
471 }
472 return std::nullopt;
473}
474
475std::optional<std::string>
476 SRC::getErrorMessage(const message::Entry& regEntry) const
477{
478 try
479 {
480 if (regEntry.doc.messageArgSources)
481 {
482 size_t msgLen = regEntry.doc.message.length();
483 char msg[msgLen + 1];
484 strcpy(msg, regEntry.doc.message.c_str());
485 std::vector<uint32_t> argSourceVals;
486 std::string message;
487 const auto& argValues = regEntry.doc.messageArgSources.value();
488 for (size_t i = 0; i < argValues.size(); ++i)
489 {
490 argSourceVals.push_back(_hexData[getWordIndexFromWordNum(
491 argValues[i].back() - '0')]);
492 }
493 const char* msgPointer = msg;
494 while (*msgPointer)
495 {
496 if (*msgPointer == '%')
497 {
498 msgPointer++;
499 size_t wordIndex = *msgPointer - '0';
500 if (isdigit(*msgPointer) && wordIndex >= 1 &&
501 static_cast<uint16_t>(wordIndex) <=
502 argSourceVals.size())
503 {
504 message.append(getNumberString(
505 "0x%X", argSourceVals[wordIndex - 1]));
506 }
507 else
508 {
509 message.append("%" + std::string(1, *msgPointer));
510 }
511 }
512 else
513 {
514 message.push_back(*msgPointer);
515 }
516 msgPointer++;
517 }
518 return message;
519 }
520 else
521 {
522 return regEntry.doc.message;
523 }
524 }
525 catch (const std::exception& e)
526 {
527 log<level::ERR>("Cannot get error message from registry entry",
528 entry("ERROR=%s", e.what()));
529 }
530 return std::nullopt;
531}
532
533std::optional<std::string> SRC::getCallouts() const
534{
535 if (!_callouts)
536 {
537 return std::nullopt;
538 }
539 std::string printOut;
540 const std::string jsonIndent(indentLevel, 0x20);
541 const auto& callout = _callouts->callouts();
542 const auto& compDescrp = pv::failingComponentType;
543 printOut.append(jsonIndent + "\"Callout Section\": {\n");
544 jsonInsert(printOut, "Callout Count", std::to_string(callout.size()), 2);
545 printOut.append(jsonIndent + jsonIndent + "\"Callouts\": [");
546 for (auto& entry : callout)
547 {
548 printOut.append("{\n");
549 if (entry->fruIdentity())
550 {
551 jsonInsert(
552 printOut, "FRU Type",
553 compDescrp.at(entry->fruIdentity()->failingComponentType()), 3);
554 jsonInsert(printOut, "Priority",
555 pv::getValue(entry->priority(),
556 pel_values::calloutPriorityValues),
557 3);
558 if (!entry->locationCode().empty())
559 {
560 jsonInsert(printOut, "Location Code", entry->locationCode(), 3);
561 }
562 if (entry->fruIdentity()->getPN().has_value())
563 {
564 jsonInsert(printOut, "Part Number",
565 entry->fruIdentity()->getPN().value(), 3);
566 }
567 if (entry->fruIdentity()->getMaintProc().has_value())
568 {
569 jsonInsert(printOut, "Procedure Number",
570 entry->fruIdentity()->getMaintProc().value(), 3);
571 if (pv::procedureDesc.find(
572 entry->fruIdentity()->getMaintProc().value()) !=
573 pv::procedureDesc.end())
574 {
575 jsonInsert(
576 printOut, "Description",
577 pv::procedureDesc.at(
578 entry->fruIdentity()->getMaintProc().value()),
579 3);
580 }
581 }
582 if (entry->fruIdentity()->getCCIN().has_value())
583 {
584 jsonInsert(printOut, "CCIN",
585 entry->fruIdentity()->getCCIN().value(), 3);
586 }
587 if (entry->fruIdentity()->getSN().has_value())
588 {
589 jsonInsert(printOut, "Serial Number",
590 entry->fruIdentity()->getSN().value(), 3);
591 }
592 }
593 if (entry->pceIdentity())
594 {
595 const auto& pceIdentMtms = entry->pceIdentity()->mtms();
596 if (!pceIdentMtms.machineTypeAndModel().empty())
597 {
598 jsonInsert(printOut, "PCE MTMS",
599 pceIdentMtms.machineTypeAndModel() + "_" +
600 pceIdentMtms.machineSerialNumber(),
601 3);
602 }
603 if (!entry->pceIdentity()->enclosureName().empty())
604 {
605 jsonInsert(printOut, "PCE Name",
606 entry->pceIdentity()->enclosureName(), 3);
607 }
608 }
609 if (entry->mru())
610 {
611 const auto& mruCallouts = entry->mru()->mrus();
612 std::string mruId;
613 for (auto& element : mruCallouts)
614 {
615 if (!mruId.empty())
616 {
617 mruId.append(", " + getNumberString("%08X", element.id));
618 }
619 else
620 {
621 mruId.append(getNumberString("%08X", element.id));
622 }
623 }
624 jsonInsert(printOut, "MRU Id", mruId, 3);
625 }
626 printOut.erase(printOut.size() - 2);
627 printOut.append("\n" + jsonIndent + jsonIndent + "}, ");
628 };
629 printOut.erase(printOut.size() - 2);
630 printOut.append("]\n" + jsonIndent + "}");
631 return printOut;
632}
633
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800634std::optional<std::string> SRC::getJSON(message::Registry& registry,
635 const std::vector<std::string>& plugins,
636 uint8_t creatorID) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800637{
638 std::string ps;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800639 std::vector<std::string> hexwords;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800640 jsonInsert(ps, pv::sectionVer, getNumberString("%d", _header.version), 1);
641 jsonInsert(ps, pv::subSection, getNumberString("%d", _header.subType), 1);
642 jsonInsert(ps, pv::createdBy, getNumberString("0x%X", _header.componentID),
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800643 1);
644 jsonInsert(ps, "SRC Version", getNumberString("0x%02X", _version), 1);
Harisuddin Mohamed Isac32e5512020-02-06 18:05:21 +0800645 jsonInsert(ps, "SRC Format", getNumberString("0x%02X", _hexData[0] & 0xFF),
646 1);
647 jsonInsert(ps, "Virtual Progress SRC",
648 pv::boolString.at(_flags & virtualProgressSRC), 1);
649 jsonInsert(ps, "I5/OS Service Event Bit",
650 pv::boolString.at(_flags & i5OSServiceEventBit), 1);
651 jsonInsert(ps, "Hypervisor Dump Initiated",
652 pv::boolString.at(_flags & hypDumpInit), 1);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800653 jsonInsert(ps, "Power Control Net Fault",
654 pv::boolString.at(isPowerFaultEvent()), 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600655
656 if (isBMCSRC())
657 {
658 std::string ccinString;
659 uint32_t ccin = _hexData[1] >> 16;
660
661 if (ccin)
662 {
663 ccinString = getNumberString("%04X", ccin);
664 }
665 // The PEL spec calls it a backplane, so call it that here.
666 jsonInsert(ps, "Backplane CCIN", ccinString, 1);
667 }
668
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800669 auto errorDetails = getErrorDetails(registry, DetailLevel::json, true);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800670 if (errorDetails)
671 {
672 ps.append(errorDetails.value());
673 }
674 jsonInsert(ps, "Valid Word Count", getNumberString("0x%02X", _wordCount),
675 1);
676 std::string refcode = asciiString();
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800677 hexwords.push_back(refcode);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800678 std::string extRefcode;
679 size_t pos = refcode.find(0x20);
680 if (pos != std::string::npos)
681 {
682 size_t nextPos = refcode.find_first_not_of(0x20, pos);
683 if (nextPos != std::string::npos)
684 {
685 extRefcode = trimEnd(refcode.substr(nextPos));
686 }
687 refcode.erase(pos);
688 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800689 jsonInsert(ps, "Reference Code", refcode, 1);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800690 if (!extRefcode.empty())
691 {
692 jsonInsert(ps, "Extended Reference Code", extRefcode, 1);
693 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800694 for (size_t i = 2; i <= _wordCount; i++)
695 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800696 std::string tmpWord =
697 getNumberString("%08X", _hexData[getWordIndexFromWordNum(i)]);
698 jsonInsert(ps, "Hex Word " + std::to_string(i), tmpWord, 1);
699 hexwords.push_back(tmpWord);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800700 }
701 auto calloutJson = getCallouts();
702 if (calloutJson)
703 {
704 ps.append(calloutJson.value());
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800705 ps.append(",\n");
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800706 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800707 std::string subsystem = getNumberString("%c", tolower(creatorID));
708 bool srcDetailExists = false;
709#ifdef PELTOOL
710 if (std::find(plugins.begin(), plugins.end(), subsystem + "src") !=
711 plugins.end())
712 {
713 auto pyJson = getPythonJSON(hexwords, creatorID);
714 if (pyJson)
715 {
716 ps.append(pyJson.value());
717 srcDetailExists = true;
718 }
719 }
720#endif
721 if (!srcDetailExists)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800722 {
723 ps.erase(ps.size() - 2);
724 }
725 return ps;
726}
727
Matt Spinler03984582020-04-09 13:17:58 -0500728void SRC::addCallouts(const message::Entry& regEntry,
729 const AdditionalData& additionalData,
Matt Spinlered046852020-03-13 13:58:15 -0500730 const DataInterfaceBase& dataIface)
731{
732 auto item = additionalData.getValue("CALLOUT_INVENTORY_PATH");
733 if (item)
734 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500735 addInventoryCallout(*item, std::nullopt, std::nullopt, dataIface);
Matt Spinlered046852020-03-13 13:58:15 -0500736 }
737
Matt Spinler717de422020-06-04 13:10:14 -0500738 addDevicePathCallouts(additionalData, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -0500739
740 if (regEntry.callouts)
741 {
742 addRegistryCallouts(regEntry, additionalData, dataIface);
743 }
Matt Spinlered046852020-03-13 13:58:15 -0500744}
745
746void SRC::addInventoryCallout(const std::string& inventoryPath,
Matt Spinleraf191c72020-06-04 11:35:13 -0500747 const std::optional<CalloutPriority>& priority,
748 const std::optional<std::string>& locationCode,
Matt Spinlered046852020-03-13 13:58:15 -0500749 const DataInterfaceBase& dataIface)
750{
751 std::string locCode;
752 std::string fn;
753 std::string ccin;
754 std::string sn;
755 std::unique_ptr<src::Callout> callout;
756
Matt Spinlered046852020-03-13 13:58:15 -0500757 try
758 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500759 // Use the passed in location code if there otherwise look it up
760 if (locationCode)
761 {
762 locCode = *locationCode;
763 }
764 else
765 {
766 locCode = dataIface.getLocationCode(inventoryPath);
767 }
Matt Spinlered046852020-03-13 13:58:15 -0500768
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500769 try
770 {
771 dataIface.getHWCalloutFields(inventoryPath, fn, ccin, sn);
772
Matt Spinleraf191c72020-06-04 11:35:13 -0500773 CalloutPriority p =
774 priority ? priority.value() : CalloutPriority::high;
775
776 callout = std::make_unique<src::Callout>(p, locCode, fn, ccin, sn);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500777 }
778 catch (const SdBusError& e)
779 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500780 std::string msg =
781 "No VPD found for " + inventoryPath + ": " + e.what();
782 addDebugData(msg);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500783
784 // Just create the callout with empty FRU fields
785 callout = std::make_unique<src::Callout>(CalloutPriority::high,
786 locCode, fn, ccin, sn);
787 }
Matt Spinlered046852020-03-13 13:58:15 -0500788 }
789 catch (const SdBusError& e)
790 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500791 std::string msg = "Could not get location code for " + inventoryPath +
792 ": " + e.what();
793 addDebugData(msg);
Matt Spinlered046852020-03-13 13:58:15 -0500794
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500795 // If this were to happen, people would have to look in the UserData
796 // section that contains CALLOUT_INVENTORY_PATH to see what failed.
Matt Spinlered046852020-03-13 13:58:15 -0500797 callout = std::make_unique<src::Callout>(CalloutPriority::high,
Matt Spinlera27e2e52020-04-09 11:06:11 -0500798 "no_vpd_for_fru");
Matt Spinlered046852020-03-13 13:58:15 -0500799 }
800
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500801 createCalloutsObject();
Matt Spinlered046852020-03-13 13:58:15 -0500802 _callouts->addCallout(std::move(callout));
Matt Spinler03984582020-04-09 13:17:58 -0500803}
Matt Spinlered046852020-03-13 13:58:15 -0500804
Matt Spinler03984582020-04-09 13:17:58 -0500805void SRC::addRegistryCallouts(const message::Entry& regEntry,
806 const AdditionalData& additionalData,
807 const DataInterfaceBase& dataIface)
808{
809 try
810 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500811 auto systemNames = dataIface.getSystemNames();
Matt Spinler03984582020-04-09 13:17:58 -0500812
813 auto regCallouts = message::Registry::getCallouts(
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500814 regEntry.callouts.value(), systemNames, additionalData);
Matt Spinler03984582020-04-09 13:17:58 -0500815
816 for (const auto& regCallout : regCallouts)
817 {
818 addRegistryCallout(regCallout, dataIface);
819 }
820 }
821 catch (std::exception& e)
822 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500823 std::string msg =
824 "Error parsing PEL message registry callout JSON: "s + e.what();
825 addDebugData(msg);
Matt Spinler03984582020-04-09 13:17:58 -0500826 }
827}
828
829void SRC::addRegistryCallout(const message::RegistryCallout& regCallout,
830 const DataInterfaceBase& dataIface)
831{
832 std::unique_ptr<src::Callout> callout;
Matt Spinler03984582020-04-09 13:17:58 -0500833 auto locCode = regCallout.locCode;
834
Matt Spinleraf191c72020-06-04 11:35:13 -0500835 if (!locCode.empty())
836 {
837 try
838 {
839 locCode = dataIface.expandLocationCode(locCode, 0);
840 }
841 catch (const std::exception& e)
842 {
843 auto msg =
844 "Unable to expand location code " + locCode + ": " + e.what();
845 addDebugData(msg);
846 return;
847 }
848 }
849
Matt Spinler03984582020-04-09 13:17:58 -0500850 // Via the PEL values table, get the priority enum.
851 // The schema will have validated the priority was a valid value.
852 auto priorityIt =
853 pv::findByName(regCallout.priority, pv::calloutPriorityValues);
854 assert(priorityIt != pv::calloutPriorityValues.end());
855 auto priority =
856 static_cast<CalloutPriority>(std::get<pv::fieldValuePos>(*priorityIt));
857
858 if (!regCallout.procedure.empty())
859 {
860 // Procedure callout
861 callout =
862 std::make_unique<src::Callout>(priority, regCallout.procedure);
863 }
864 else if (!regCallout.symbolicFRU.empty())
865 {
866 // Symbolic FRU callout
867 callout = std::make_unique<src::Callout>(
868 priority, regCallout.symbolicFRU, locCode, false);
869 }
870 else if (!regCallout.symbolicFRUTrusted.empty())
871 {
872 // Symbolic FRU with trusted location code callout
873
874 // The registry wants it to be trusted, but that requires a valid
875 // location code for it to actually be.
876 callout = std::make_unique<src::Callout>(
877 priority, regCallout.symbolicFRUTrusted, locCode, !locCode.empty());
878 }
879 else
880 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500881 // A hardware callout
882 std::string inventoryPath;
883
884 try
885 {
886 // Get the inventory item from the unexpanded location code
887 inventoryPath =
Matt Spinler2f9225a2020-08-05 12:58:49 -0500888 dataIface.getInventoryFromLocCode(regCallout.locCode, 0, false);
Matt Spinleraf191c72020-06-04 11:35:13 -0500889 }
890 catch (const std::exception& e)
891 {
892 std::string msg =
893 "Unable to get inventory path from location code: " + locCode +
894 ": " + e.what();
895 addDebugData(msg);
896 return;
897 }
898
899 addInventoryCallout(inventoryPath, priority, locCode, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -0500900 }
901
902 if (callout)
903 {
904 createCalloutsObject();
905 _callouts->addCallout(std::move(callout));
906 }
907}
Matt Spinlered046852020-03-13 13:58:15 -0500908
Matt Spinler717de422020-06-04 13:10:14 -0500909void SRC::addDevicePathCallouts(const AdditionalData& additionalData,
910 const DataInterfaceBase& dataIface)
911{
912 std::vector<device_callouts::Callout> callouts;
913 auto i2cBus = additionalData.getValue("CALLOUT_IIC_BUS");
914 auto i2cAddr = additionalData.getValue("CALLOUT_IIC_ADDR");
915 auto devPath = additionalData.getValue("CALLOUT_DEVICE_PATH");
916
917 // A device callout contains either:
918 // * CALLOUT_ERRNO, CALLOUT_DEVICE_PATH
919 // * CALLOUT_ERRNO, CALLOUT_IIC_BUS, CALLOUT_IIC_ADDR
920 // We don't care about the errno.
921
922 if (devPath)
923 {
924 try
925 {
926 callouts = device_callouts::getCallouts(*devPath,
927 dataIface.getSystemNames());
928 }
929 catch (const std::exception& e)
930 {
931 addDebugData(e.what());
932 callouts.clear();
933 }
934 }
935 else if (i2cBus && i2cAddr)
936 {
937 size_t bus;
938 uint8_t address;
939
940 try
941 {
942 // If /dev/i2c- is prepended, remove it
943 if (i2cBus->find("/dev/i2c-") != std::string::npos)
944 {
945 *i2cBus = i2cBus->substr(9);
946 }
947
948 bus = stoul(*i2cBus, nullptr, 0);
949 address = stoul(*i2cAddr, nullptr, 0);
950 }
951 catch (const std::exception& e)
952 {
953 std::string msg = "Invalid CALLOUT_IIC_BUS " + *i2cBus +
954 " or CALLOUT_IIC_ADDR " + *i2cAddr +
955 " in AdditionalData property";
956 addDebugData(msg);
957 return;
958 }
959
960 try
961 {
962 callouts = device_callouts::getI2CCallouts(
963 bus, address, dataIface.getSystemNames());
964 }
965 catch (const std::exception& e)
966 {
967 addDebugData(e.what());
968 callouts.clear();
969 }
970 }
971
972 for (const auto& callout : callouts)
973 {
974 // The priority shouldn't be invalid, but check just in case.
975 CalloutPriority priority = CalloutPriority::high;
976
977 if (!callout.priority.empty())
978 {
979 auto p = pel_values::findByValue(
980 static_cast<uint32_t>(callout.priority[0]),
981 pel_values::calloutPriorityValues);
982
983 if (p != pel_values::calloutPriorityValues.end())
984 {
985 priority = static_cast<CalloutPriority>(callout.priority[0]);
986 }
987 else
988 {
989 std::string msg =
990 "Invalid priority found in dev callout JSON: " +
991 callout.priority[0];
992 addDebugData(msg);
993 }
994 }
995
996 try
997 {
Matt Spinler2f9225a2020-08-05 12:58:49 -0500998 auto inventoryPath = dataIface.getInventoryFromLocCode(
999 callout.locationCode, 0, false);
Matt Spinler717de422020-06-04 13:10:14 -05001000
1001 addInventoryCallout(inventoryPath, priority, std::nullopt,
1002 dataIface);
1003 }
1004 catch (const std::exception& e)
1005 {
1006 std::string msg =
1007 "Unable to get inventory path from location code: " +
1008 callout.locationCode + ": " + e.what();
1009 addDebugData(msg);
1010 }
1011
1012 // Until the code is there to convert these MRU value strings to
1013 // the official MRU values in the callout objects, just store
1014 // the MRU name in the debug UserData section.
1015 if (!callout.mru.empty())
1016 {
1017 std::string msg = "MRU: " + callout.mru;
1018 addDebugData(msg);
1019 }
1020
1021 // getCallouts() may have generated some debug data it stored
1022 // in a callout object. Save it as well.
1023 if (!callout.debug.empty())
1024 {
1025 addDebugData(callout.debug);
1026 }
1027 }
1028}
1029
Matt Spinlerf9bae182019-10-09 13:37:38 -05001030} // namespace pels
1031} // namespace openpower