blob: 7e764bbc215a376903c9adfcfae386b35eb63bac [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 <nlohmann/json.hpp>
26#include <sstream>
27#endif
Matt Spinler5a90a952020-08-27 09:39:03 -050028#include <fmt/format.h>
29
Matt Spinlerf9bae182019-10-09 13:37:38 -050030#include <phosphor-logging/log.hpp>
31
32namespace openpower
33{
34namespace pels
35{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080036namespace pv = openpower::pels::pel_values;
37namespace rg = openpower::pels::message;
Matt Spinlerf9bae182019-10-09 13:37:38 -050038using namespace phosphor::logging;
Matt Spinler85f61a62020-06-03 16:28:55 -050039using namespace std::string_literals;
Matt Spinlerf9bae182019-10-09 13:37:38 -050040
Matt Spinler075e5ba2020-02-21 15:46:00 -060041constexpr size_t ccinSize = 4;
42
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080043#ifdef PELTOOL
Sumit Kumar516935a2021-04-14 13:00:54 -050044using orderedJSON = nlohmann::ordered_json;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080045
46void pyDecRef(PyObject* pyObj)
47{
48 Py_XDECREF(pyObj);
49}
50
51/**
52 * @brief Returns a JSON string to append to SRC section.
53 *
54 * The returning string will contain a JSON object, but without
55 * the outer {}. If the input JSON isn't a JSON object (dict), then
56 * one will be created with the input added to a 'SRC Details' key.
57 *
58 * @param[in] json - The JSON to convert to a string
59 *
60 * @return std::string - The JSON string
61 */
Sumit Kumar516935a2021-04-14 13:00:54 -050062std::string prettyJSON(const orderedJSON& json)
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080063{
Sumit Kumar516935a2021-04-14 13:00:54 -050064 orderedJSON output;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080065 if (!json.is_object())
66 {
67 output["SRC Details"] = json;
68 }
69 else
70 {
71 for (const auto& [key, value] : json.items())
72 {
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +080073 output["SRC Details"][key] = value;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080074 }
75 }
76
77 // Let nlohmann do the pretty printing.
78 std::stringstream stream;
79 stream << std::setw(4) << output;
80
81 auto jsonString = stream.str();
82
83 // Now it looks like:
84 // {
85 // "Key": "Value",
86 // ...
87 // }
88
89 // Replace the { and the following newline, and the } and its
90 // preceeding newline.
91 jsonString.erase(0, 2);
92
93 auto pos = jsonString.find_last_of('}');
94 jsonString.erase(pos - 1);
95
96 return jsonString;
97}
98
99/**
100 * @brief Call Python modules to parse the data into a JSON string
101 *
102 * The module to call is based on the Creator Subsystem ID under the namespace
103 * "srcparsers". For example: "srcparsers.xsrc.xsrc" where "x" is the Creator
104 * Subsystem ID in ASCII lowercase.
105 *
106 * All modules must provide the following:
107 * Function: parseSRCToJson
108 * Argument list:
109 * 1. (str) ASCII string (Hex Word 1)
110 * 2. (str) Hex Word 2
111 * 3. (str) Hex Word 3
112 * 4. (str) Hex Word 4
113 * 5. (str) Hex Word 5
114 * 6. (str) Hex Word 6
115 * 7. (str) Hex Word 7
116 * 8. (str) Hex Word 8
117 * 9. (str) Hex Word 9
118 *-Return data:
119 * 1. (str) JSON string
120 *
121 * @param[in] hexwords - Vector of strings of Hexwords 1-9
122 * @param[in] creatorID - The creatorID from the Private Header section
123 * @return std::optional<std::string> - The JSON string if it could be created,
124 * else std::nullopt
125 */
126std::optional<std::string> getPythonJSON(std::vector<std::string>& hexwords,
127 uint8_t creatorID)
128{
129 PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pResult, *pBytes,
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800130 *eType, *eValue, *eTraceback, *pKey;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800131 std::string pErrStr;
132 std::string module = getNumberString("%c", tolower(creatorID)) + "src";
133 pName = PyUnicode_FromString(
134 std::string("srcparsers." + module + "." + module).c_str());
135 std::unique_ptr<PyObject, decltype(&pyDecRef)> modNamePtr(pName, &pyDecRef);
136 pModule = PyImport_Import(pName);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800137 if (pModule == NULL)
138 {
139 pErrStr = "No error string found";
140 PyErr_Fetch(&eType, &eValue, &eTraceback);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800141 if (eType)
142 {
143 Py_XDECREF(eType);
144 }
145 if (eTraceback)
146 {
147 Py_XDECREF(eTraceback);
148 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800149 if (eValue)
150 {
151 PyObject* pStr = PyObject_Str(eValue);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800152 Py_XDECREF(eValue);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800153 if (pStr)
154 {
155 pErrStr = PyUnicode_AsUTF8(pStr);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800156 Py_XDECREF(pStr);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800157 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800158 }
159 }
160 else
161 {
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800162 std::unique_ptr<PyObject, decltype(&pyDecRef)> modPtr(pModule,
163 &pyDecRef);
164 std::string funcToCall = "parseSRCToJson";
165 pKey = PyUnicode_FromString(funcToCall.c_str());
166 std::unique_ptr<PyObject, decltype(&pyDecRef)> keyPtr(pKey, &pyDecRef);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800167 pDict = PyModule_GetDict(pModule);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800168 Py_INCREF(pDict);
169 if (!PyDict_Contains(pDict, pKey))
170 {
171 Py_DECREF(pDict);
172 log<level::ERR>(
173 "Python module error",
174 entry("ERROR=%s",
175 std::string(funcToCall + " function missing").c_str()),
176 entry("SRC=%s", hexwords.front().c_str()),
177 entry("PARSER_MODULE=%s", module.c_str()));
178 return std::nullopt;
179 }
180 pFunc = PyDict_GetItemString(pDict, funcToCall.c_str());
181 Py_DECREF(pDict);
182 Py_INCREF(pFunc);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800183 if (PyCallable_Check(pFunc))
184 {
185 pArgs = PyTuple_New(9);
186 std::unique_ptr<PyObject, decltype(&pyDecRef)> argPtr(pArgs,
187 &pyDecRef);
188 for (size_t i = 0; i < 9; i++)
189 {
190 if (i < hexwords.size())
191 {
192 auto arg = hexwords[i];
193 PyTuple_SetItem(pArgs, i,
194 Py_BuildValue("s#", arg.c_str(), 8));
195 }
196 else
197 {
198 PyTuple_SetItem(pArgs, i, Py_BuildValue("s", "00000000"));
199 }
200 }
201 pResult = PyObject_CallObject(pFunc, pArgs);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800202 Py_DECREF(pFunc);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800203 if (pResult)
204 {
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800205 std::unique_ptr<PyObject, decltype(&pyDecRef)> resPtr(
206 pResult, &pyDecRef);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800207 pBytes = PyUnicode_AsEncodedString(pResult, "utf-8", "~E~");
208 std::unique_ptr<PyObject, decltype(&pyDecRef)> pyBytePtr(
209 pBytes, &pyDecRef);
210 const char* output = PyBytes_AS_STRING(pBytes);
211 try
212 {
Matt Spinlerbb1c1d52021-06-03 13:18:48 -0600213 orderedJSON json = orderedJSON::parse(output);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800214 if ((json.is_object() && !json.empty()) ||
215 (json.is_array() && json.size() > 0) ||
216 (json.is_string() && json != ""))
217 {
218 return prettyJSON(json);
219 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800220 }
Patrick Williams66491c62021-10-06 12:23:37 -0500221 catch (const std::exception& e)
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800222 {
223 log<level::ERR>("Bad JSON from parser",
224 entry("ERROR=%s", e.what()),
225 entry("SRC=%s", hexwords.front().c_str()),
226 entry("PARSER_MODULE=%s", module.c_str()));
227 return std::nullopt;
228 }
229 }
230 else
231 {
232 pErrStr = "No error string found";
233 PyErr_Fetch(&eType, &eValue, &eTraceback);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800234 if (eType)
235 {
236 Py_XDECREF(eType);
237 }
238 if (eTraceback)
239 {
240 Py_XDECREF(eTraceback);
241 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800242 if (eValue)
243 {
244 PyObject* pStr = PyObject_Str(eValue);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800245 Py_XDECREF(eValue);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800246 if (pStr)
247 {
248 pErrStr = PyUnicode_AsUTF8(pStr);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800249 Py_XDECREF(pStr);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800250 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800251 }
252 }
253 }
254 }
255 if (!pErrStr.empty())
256 {
Matt Spinler3279cc52022-02-15 11:02:00 -0600257 log<level::DEBUG>("Python exception thrown by parser",
258 entry("ERROR=%s", pErrStr.c_str()),
259 entry("SRC=%s", hexwords.front().c_str()),
260 entry("PARSER_MODULE=%s", module.c_str()));
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800261 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800262 return std::nullopt;
263}
264#endif
265
Matt Spinlerf9bae182019-10-09 13:37:38 -0500266void SRC::unflatten(Stream& stream)
267{
268 stream >> _header >> _version >> _flags >> _reserved1B >> _wordCount >>
269 _reserved2B >> _size;
270
271 for (auto& word : _hexData)
272 {
273 stream >> word;
274 }
275
276 _asciiString = std::make_unique<src::AsciiString>(stream);
277
278 if (hasAdditionalSections())
279 {
280 // The callouts section is currently the only extra subsection type
281 _callouts = std::make_unique<src::Callouts>(stream);
282 }
283}
284
Matt Spinler06885452019-11-06 10:35:42 -0600285void SRC::flatten(Stream& stream) const
Matt Spinlerf9bae182019-10-09 13:37:38 -0500286{
287 stream << _header << _version << _flags << _reserved1B << _wordCount
288 << _reserved2B << _size;
289
290 for (auto& word : _hexData)
291 {
292 stream << word;
293 }
294
295 _asciiString->flatten(stream);
296
297 if (_callouts)
298 {
299 _callouts->flatten(stream);
300 }
301}
302
303SRC::SRC(Stream& pel)
304{
305 try
306 {
307 unflatten(pel);
308 validate();
309 }
310 catch (const std::exception& e)
311 {
312 log<level::ERR>("Cannot unflatten SRC", entry("ERROR=%s", e.what()));
313 _valid = false;
314 }
315}
316
Matt Spinler075e5ba2020-02-21 15:46:00 -0600317SRC::SRC(const message::Entry& regEntry, const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500318 const nlohmann::json& jsonCallouts, const DataInterfaceBase& dataIface)
Matt Spinlerbd716f02019-10-15 10:54:11 -0500319{
320 _header.id = static_cast<uint16_t>(SectionID::primarySRC);
321 _header.version = srcSectionVersion;
322 _header.subType = srcSectionSubtype;
323 _header.componentID = regEntry.componentID;
324
325 _version = srcVersion;
326
327 _flags = 0;
Vijay Lobof3702bb2021-04-09 15:10:19 -0500328
Matt Spinlerbd716f02019-10-15 10:54:11 -0500329 _reserved1B = 0;
330
331 _wordCount = numSRCHexDataWords + 1;
332
333 _reserved2B = 0;
334
335 // There are multiple fields encoded in the hex data words.
336 std::for_each(_hexData.begin(), _hexData.end(),
337 [](auto& word) { word = 0; });
Matt Spinler7c619182020-07-27 15:15:11 -0500338
339 // Hex Word 2 Nibbles:
340 // MIGVEPFF
341 // M: Partition dump status = 0
342 // I: System boot state = TODO
343 // G: Partition Boot type = 0
Sumit Kumar9d43a722021-08-24 09:46:19 -0500344 // V: BMC dump status
Matt Spinler7c619182020-07-27 15:15:11 -0500345 // E: Platform boot mode = 0 (side = temporary, speed = fast)
Sumit Kumar9d43a722021-08-24 09:46:19 -0500346 // P: Platform dump status
Matt Spinler7c619182020-07-27 15:15:11 -0500347 // FF: SRC format, set below
348
Sumit Kumar9d43a722021-08-24 09:46:19 -0500349 setDumpStatus(dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500350 setBMCFormat();
351 setBMCPosition();
Matt Spinler075e5ba2020-02-21 15:46:00 -0600352 setMotherboardCCIN(dataIface);
353
Matt Spinlerbd716f02019-10-15 10:54:11 -0500354 // Fill in the last 4 words from the AdditionalData property contents.
355 setUserDefinedHexWords(regEntry, additionalData);
356
357 _asciiString = std::make_unique<src::AsciiString>(regEntry);
358
Sumit Kumar50bfa692022-01-06 06:48:26 -0600359 // Check for additional data - PEL_SUBSYSTEM
360 auto ss = additionalData.getValue("PEL_SUBSYSTEM");
361 if (ss)
362 {
363 auto eventSubsystem = std::stoul(*ss, NULL, 16);
364 std::string subsystem =
365 pv::getValue(eventSubsystem, pel_values::subsystemValues);
366 if (subsystem == "invalid")
367 {
368 log<level::WARNING>(
369 fmt::format("SRC: Invalid SubSystem value:{:#X}",
370 eventSubsystem)
371 .c_str());
372 }
373 else
374 {
375 _asciiString->setByte(2, eventSubsystem);
376 }
377 }
378
Matt Spinler5a90a952020-08-27 09:39:03 -0500379 addCallouts(regEntry, additionalData, jsonCallouts, dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500380
381 _size = baseSRCSize;
382 _size += _callouts ? _callouts->flattenedSize() : 0;
383 _header.size = Section::flattenedSize() + _size;
384
385 _valid = true;
386}
387
388void SRC::setUserDefinedHexWords(const message::Entry& regEntry,
389 const AdditionalData& ad)
390{
391 if (!regEntry.src.hexwordADFields)
392 {
393 return;
394 }
395
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800396 // Save the AdditionalData value corresponding to the first element of
397 // adName tuple into _hexData[wordNum].
Matt Spinlerbd716f02019-10-15 10:54:11 -0500398 for (const auto& [wordNum, adName] : *regEntry.src.hexwordADFields)
399 {
400 // Can only set words 6 - 9
401 if (!isUserDefinedWord(wordNum))
402 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500403 std::string msg =
404 "SRC user data word out of range: " + std::to_string(wordNum);
405 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500406 continue;
407 }
408
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800409 auto value = ad.getValue(std::get<0>(adName));
Matt Spinlerbd716f02019-10-15 10:54:11 -0500410 if (value)
411 {
412 _hexData[getWordIndexFromWordNum(wordNum)] =
413 std::strtoul(value.value().c_str(), nullptr, 0);
414 }
415 else
416 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800417 std::string msg = "Source for user data SRC word not found: " +
418 std::get<0>(adName);
Matt Spinler85f61a62020-06-03 16:28:55 -0500419 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500420 }
421 }
422}
423
Matt Spinler075e5ba2020-02-21 15:46:00 -0600424void SRC::setMotherboardCCIN(const DataInterfaceBase& dataIface)
425{
426 uint32_t ccin = 0;
427 auto ccinString = dataIface.getMotherboardCCIN();
428
429 try
430 {
431 if (ccinString.size() == ccinSize)
432 {
433 ccin = std::stoi(ccinString, 0, 16);
434 }
435 }
Patrick Williams66491c62021-10-06 12:23:37 -0500436 catch (const std::exception& e)
Matt Spinler075e5ba2020-02-21 15:46:00 -0600437 {
438 log<level::WARNING>("Could not convert motherboard CCIN to a number",
439 entry("CCIN=%s", ccinString.c_str()));
440 return;
441 }
442
443 // Set the first 2 bytes
444 _hexData[1] |= ccin << 16;
445}
446
Matt Spinlerf9bae182019-10-09 13:37:38 -0500447void SRC::validate()
448{
449 bool failed = false;
450
451 if ((header().id != static_cast<uint16_t>(SectionID::primarySRC)) &&
452 (header().id != static_cast<uint16_t>(SectionID::secondarySRC)))
453 {
454 log<level::ERR>("Invalid SRC section ID",
455 entry("ID=0x%X", header().id));
456 failed = true;
457 }
458
459 // Check the version in the SRC, not in the header
Matt Spinlerbd716f02019-10-15 10:54:11 -0500460 if (_version != srcVersion)
Matt Spinlerf9bae182019-10-09 13:37:38 -0500461 {
Matt Spinlerbd716f02019-10-15 10:54:11 -0500462 log<level::ERR>("Invalid SRC version", entry("VERSION=0x%X", _version));
Matt Spinlerf9bae182019-10-09 13:37:38 -0500463 failed = true;
464 }
465
466 _valid = failed ? false : true;
467}
468
Matt Spinler075e5ba2020-02-21 15:46:00 -0600469bool SRC::isBMCSRC() const
470{
471 auto as = asciiString();
472 if (as.length() >= 2)
473 {
474 uint8_t errorType = strtoul(as.substr(0, 2).c_str(), nullptr, 16);
475 return (errorType == static_cast<uint8_t>(SRCType::bmcError) ||
476 errorType == static_cast<uint8_t>(SRCType::powerError));
477 }
478 return false;
479}
480
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800481std::optional<std::string> SRC::getErrorDetails(message::Registry& registry,
482 DetailLevel type,
483 bool toCache) const
484{
485 const std::string jsonIndent(indentLevel, 0x20);
486 std::string errorOut;
Matt Spinler075e5ba2020-02-21 15:46:00 -0600487 if (isBMCSRC())
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800488 {
489 auto entry = registry.lookup("0x" + asciiString().substr(4, 4),
490 rg::LookupType::reasonCode, toCache);
491 if (entry)
492 {
493 errorOut.append(jsonIndent + "\"Error Details\": {\n");
494 auto errorMsg = getErrorMessage(*entry);
495 if (errorMsg)
496 {
497 if (type == DetailLevel::message)
498 {
499 return errorMsg.value();
500 }
501 else
502 {
503 jsonInsert(errorOut, "Message", errorMsg.value(), 2);
504 }
505 }
Harisuddin Mohamed Isa5a3d8f42022-03-24 19:20:37 +0800506 if (entry->doc.notes)
507 {
508 jsonInsertArray(errorOut, "Notes", entry->doc.notes.value(), 2);
509 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800510 if (entry->src.hexwordADFields)
511 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800512 std::map<size_t, std::tuple<std::string, std::string>>
513 adFields = entry->src.hexwordADFields.value();
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800514 for (const auto& hexwordMap : adFields)
515 {
Zane Shelleye8db29b2021-11-13 10:34:07 -0600516 auto srcValue = getNumberString(
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800517 "0x%X",
Zane Shelleye8db29b2021-11-13 10:34:07 -0600518 _hexData[getWordIndexFromWordNum(hexwordMap.first)]);
519
520 auto srcKey = std::get<0>(hexwordMap.second);
521 auto srcDesc = std::get<1>(hexwordMap.second);
522
523 // Only include this hex word in the error details if the
524 // description exists.
525 if (!srcDesc.empty())
526 {
527 std::vector<std::string> valueDescr;
528 valueDescr.push_back(srcValue);
529 valueDescr.push_back(srcDesc);
530 jsonInsertArray(errorOut, srcKey, valueDescr, 2);
531 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800532 }
533 }
534 errorOut.erase(errorOut.size() - 2);
535 errorOut.append("\n");
536 errorOut.append(jsonIndent + "},\n");
537 return errorOut;
538 }
539 }
540 return std::nullopt;
541}
542
543std::optional<std::string>
544 SRC::getErrorMessage(const message::Entry& regEntry) const
545{
546 try
547 {
548 if (regEntry.doc.messageArgSources)
549 {
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800550 std::vector<uint32_t> argSourceVals;
551 std::string message;
552 const auto& argValues = regEntry.doc.messageArgSources.value();
553 for (size_t i = 0; i < argValues.size(); ++i)
554 {
555 argSourceVals.push_back(_hexData[getWordIndexFromWordNum(
556 argValues[i].back() - '0')]);
557 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500558
559 auto it = std::begin(regEntry.doc.message);
560 auto it_end = std::end(regEntry.doc.message);
561
562 while (it != it_end)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800563 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500564 if (*it == '%')
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800565 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500566 ++it;
567
568 size_t wordIndex = *it - '0';
569 if (isdigit(*it) && wordIndex >= 1 &&
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800570 static_cast<uint16_t>(wordIndex) <=
571 argSourceVals.size())
572 {
573 message.append(getNumberString(
Zane Shelley39936e32021-11-13 16:19:34 -0600574 "0x%08X", argSourceVals[wordIndex - 1]));
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800575 }
576 else
577 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500578 message.append("%" + std::string(1, *it));
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800579 }
580 }
581 else
582 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500583 message.push_back(*it);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800584 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500585 ++it;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800586 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500587
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800588 return message;
589 }
590 else
591 {
592 return regEntry.doc.message;
593 }
594 }
595 catch (const std::exception& e)
596 {
597 log<level::ERR>("Cannot get error message from registry entry",
598 entry("ERROR=%s", e.what()));
599 }
600 return std::nullopt;
601}
602
603std::optional<std::string> SRC::getCallouts() const
604{
605 if (!_callouts)
606 {
607 return std::nullopt;
608 }
609 std::string printOut;
610 const std::string jsonIndent(indentLevel, 0x20);
611 const auto& callout = _callouts->callouts();
612 const auto& compDescrp = pv::failingComponentType;
613 printOut.append(jsonIndent + "\"Callout Section\": {\n");
614 jsonInsert(printOut, "Callout Count", std::to_string(callout.size()), 2);
615 printOut.append(jsonIndent + jsonIndent + "\"Callouts\": [");
616 for (auto& entry : callout)
617 {
618 printOut.append("{\n");
619 if (entry->fruIdentity())
620 {
621 jsonInsert(
622 printOut, "FRU Type",
623 compDescrp.at(entry->fruIdentity()->failingComponentType()), 3);
624 jsonInsert(printOut, "Priority",
625 pv::getValue(entry->priority(),
626 pel_values::calloutPriorityValues),
627 3);
628 if (!entry->locationCode().empty())
629 {
630 jsonInsert(printOut, "Location Code", entry->locationCode(), 3);
631 }
632 if (entry->fruIdentity()->getPN().has_value())
633 {
634 jsonInsert(printOut, "Part Number",
635 entry->fruIdentity()->getPN().value(), 3);
636 }
637 if (entry->fruIdentity()->getMaintProc().has_value())
638 {
Matt Spinler9e8b49e2020-09-10 13:15:26 -0500639 jsonInsert(printOut, "Procedure",
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800640 entry->fruIdentity()->getMaintProc().value(), 3);
641 if (pv::procedureDesc.find(
642 entry->fruIdentity()->getMaintProc().value()) !=
643 pv::procedureDesc.end())
644 {
645 jsonInsert(
646 printOut, "Description",
647 pv::procedureDesc.at(
648 entry->fruIdentity()->getMaintProc().value()),
649 3);
650 }
651 }
652 if (entry->fruIdentity()->getCCIN().has_value())
653 {
654 jsonInsert(printOut, "CCIN",
655 entry->fruIdentity()->getCCIN().value(), 3);
656 }
657 if (entry->fruIdentity()->getSN().has_value())
658 {
659 jsonInsert(printOut, "Serial Number",
660 entry->fruIdentity()->getSN().value(), 3);
661 }
662 }
663 if (entry->pceIdentity())
664 {
665 const auto& pceIdentMtms = entry->pceIdentity()->mtms();
666 if (!pceIdentMtms.machineTypeAndModel().empty())
667 {
668 jsonInsert(printOut, "PCE MTMS",
669 pceIdentMtms.machineTypeAndModel() + "_" +
670 pceIdentMtms.machineSerialNumber(),
671 3);
672 }
673 if (!entry->pceIdentity()->enclosureName().empty())
674 {
675 jsonInsert(printOut, "PCE Name",
676 entry->pceIdentity()->enclosureName(), 3);
677 }
678 }
679 if (entry->mru())
680 {
681 const auto& mruCallouts = entry->mru()->mrus();
682 std::string mruId;
683 for (auto& element : mruCallouts)
684 {
685 if (!mruId.empty())
686 {
687 mruId.append(", " + getNumberString("%08X", element.id));
688 }
689 else
690 {
691 mruId.append(getNumberString("%08X", element.id));
692 }
693 }
694 jsonInsert(printOut, "MRU Id", mruId, 3);
695 }
696 printOut.erase(printOut.size() - 2);
697 printOut.append("\n" + jsonIndent + jsonIndent + "}, ");
698 };
699 printOut.erase(printOut.size() - 2);
700 printOut.append("]\n" + jsonIndent + "}");
701 return printOut;
702}
703
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800704std::optional<std::string> SRC::getJSON(message::Registry& registry,
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500705 const std::vector<std::string>& plugins
706 [[maybe_unused]],
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800707 uint8_t creatorID) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800708{
709 std::string ps;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800710 std::vector<std::string> hexwords;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800711 jsonInsert(ps, pv::sectionVer, getNumberString("%d", _header.version), 1);
712 jsonInsert(ps, pv::subSection, getNumberString("%d", _header.subType), 1);
713 jsonInsert(ps, pv::createdBy, getNumberString("0x%X", _header.componentID),
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800714 1);
715 jsonInsert(ps, "SRC Version", getNumberString("0x%02X", _version), 1);
Harisuddin Mohamed Isac32e5512020-02-06 18:05:21 +0800716 jsonInsert(ps, "SRC Format", getNumberString("0x%02X", _hexData[0] & 0xFF),
717 1);
718 jsonInsert(ps, "Virtual Progress SRC",
719 pv::boolString.at(_flags & virtualProgressSRC), 1);
720 jsonInsert(ps, "I5/OS Service Event Bit",
721 pv::boolString.at(_flags & i5OSServiceEventBit), 1);
722 jsonInsert(ps, "Hypervisor Dump Initiated",
723 pv::boolString.at(_flags & hypDumpInit), 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600724
725 if (isBMCSRC())
726 {
727 std::string ccinString;
728 uint32_t ccin = _hexData[1] >> 16;
729
730 if (ccin)
731 {
732 ccinString = getNumberString("%04X", ccin);
733 }
734 // The PEL spec calls it a backplane, so call it that here.
735 jsonInsert(ps, "Backplane CCIN", ccinString, 1);
Matt Spinlerafa2c792020-08-27 11:01:39 -0500736
Sumit Kumar3e274432021-09-14 06:37:56 -0500737 jsonInsert(ps, "Terminate FW Error",
738 pv::boolString.at(
739 _hexData[3] &
740 static_cast<uint32_t>(ErrorStatusFlags::terminateFwErr)),
741 1);
Matt Spinlerafa2c792020-08-27 11:01:39 -0500742 jsonInsert(ps, "Deconfigured",
743 pv::boolString.at(
744 _hexData[3] &
745 static_cast<uint32_t>(ErrorStatusFlags::deconfigured)),
746 1);
747
748 jsonInsert(
749 ps, "Guarded",
750 pv::boolString.at(_hexData[3] &
751 static_cast<uint32_t>(ErrorStatusFlags::guarded)),
752 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600753 }
754
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800755 auto errorDetails = getErrorDetails(registry, DetailLevel::json, true);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800756 if (errorDetails)
757 {
758 ps.append(errorDetails.value());
759 }
760 jsonInsert(ps, "Valid Word Count", getNumberString("0x%02X", _wordCount),
761 1);
762 std::string refcode = asciiString();
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800763 hexwords.push_back(refcode);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800764 std::string extRefcode;
765 size_t pos = refcode.find(0x20);
766 if (pos != std::string::npos)
767 {
768 size_t nextPos = refcode.find_first_not_of(0x20, pos);
769 if (nextPos != std::string::npos)
770 {
771 extRefcode = trimEnd(refcode.substr(nextPos));
772 }
773 refcode.erase(pos);
774 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800775 jsonInsert(ps, "Reference Code", refcode, 1);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800776 if (!extRefcode.empty())
777 {
778 jsonInsert(ps, "Extended Reference Code", extRefcode, 1);
779 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800780 for (size_t i = 2; i <= _wordCount; i++)
781 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800782 std::string tmpWord =
783 getNumberString("%08X", _hexData[getWordIndexFromWordNum(i)]);
784 jsonInsert(ps, "Hex Word " + std::to_string(i), tmpWord, 1);
785 hexwords.push_back(tmpWord);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800786 }
787 auto calloutJson = getCallouts();
788 if (calloutJson)
789 {
790 ps.append(calloutJson.value());
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800791 ps.append(",\n");
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800792 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800793 std::string subsystem = getNumberString("%c", tolower(creatorID));
794 bool srcDetailExists = false;
795#ifdef PELTOOL
796 if (std::find(plugins.begin(), plugins.end(), subsystem + "src") !=
797 plugins.end())
798 {
799 auto pyJson = getPythonJSON(hexwords, creatorID);
800 if (pyJson)
801 {
802 ps.append(pyJson.value());
803 srcDetailExists = true;
804 }
805 }
806#endif
807 if (!srcDetailExists)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800808 {
809 ps.erase(ps.size() - 2);
810 }
811 return ps;
812}
813
Matt Spinler03984582020-04-09 13:17:58 -0500814void SRC::addCallouts(const message::Entry& regEntry,
815 const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500816 const nlohmann::json& jsonCallouts,
Matt Spinlered046852020-03-13 13:58:15 -0500817 const DataInterfaceBase& dataIface)
818{
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500819 auto registryCallouts =
820 getRegistryCallouts(regEntry, additionalData, dataIface);
821
Matt Spinlered046852020-03-13 13:58:15 -0500822 auto item = additionalData.getValue("CALLOUT_INVENTORY_PATH");
Miguel Gomez53ef1552020-10-14 21:16:32 +0000823 auto priority = additionalData.getValue("CALLOUT_PRIORITY");
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500824
Miguel Gomez53ef1552020-10-14 21:16:32 +0000825 std::optional<CalloutPriority> calloutPriority;
826
827 // Only H, M or L priority values.
828 if (priority && !(*priority).empty())
829 {
830 uint8_t p = (*priority)[0];
831 if (p == 'H' || p == 'M' || p == 'L')
832 {
833 calloutPriority = static_cast<CalloutPriority>(p);
834 }
835 }
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500836 // If the first registry callout says to use the passed in inventory
837 // path to get the location code for a symbolic FRU callout with a
838 // trusted location code, then do not add the inventory path as a
839 // normal FRU callout.
840 bool useInvForSymbolicFRULocCode =
841 !registryCallouts.empty() && registryCallouts[0].useInventoryLocCode &&
842 !registryCallouts[0].symbolicFRUTrusted.empty();
843
844 if (item && !useInvForSymbolicFRULocCode)
Matt Spinlered046852020-03-13 13:58:15 -0500845 {
Miguel Gomez53ef1552020-10-14 21:16:32 +0000846 addInventoryCallout(*item, calloutPriority, std::nullopt, dataIface);
Matt Spinlered046852020-03-13 13:58:15 -0500847 }
848
Matt Spinler717de422020-06-04 13:10:14 -0500849 addDevicePathCallouts(additionalData, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -0500850
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500851 addRegistryCallouts(registryCallouts, dataIface,
852 (useInvForSymbolicFRULocCode) ? item : std::nullopt);
Matt Spinler5a90a952020-08-27 09:39:03 -0500853
854 if (!jsonCallouts.empty())
855 {
856 addJSONCallouts(jsonCallouts, dataIface);
857 }
Matt Spinlered046852020-03-13 13:58:15 -0500858}
859
860void SRC::addInventoryCallout(const std::string& inventoryPath,
Matt Spinleraf191c72020-06-04 11:35:13 -0500861 const std::optional<CalloutPriority>& priority,
862 const std::optional<std::string>& locationCode,
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500863 const DataInterfaceBase& dataIface,
864 const std::vector<src::MRU::MRUCallout>& mrus)
Matt Spinlered046852020-03-13 13:58:15 -0500865{
866 std::string locCode;
867 std::string fn;
868 std::string ccin;
869 std::string sn;
870 std::unique_ptr<src::Callout> callout;
871
Matt Spinlered046852020-03-13 13:58:15 -0500872 try
873 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500874 // Use the passed in location code if there otherwise look it up
875 if (locationCode)
876 {
877 locCode = *locationCode;
878 }
879 else
880 {
881 locCode = dataIface.getLocationCode(inventoryPath);
882 }
Matt Spinlered046852020-03-13 13:58:15 -0500883
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500884 try
885 {
886 dataIface.getHWCalloutFields(inventoryPath, fn, ccin, sn);
887
Matt Spinleraf191c72020-06-04 11:35:13 -0500888 CalloutPriority p =
889 priority ? priority.value() : CalloutPriority::high;
890
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500891 callout =
892 std::make_unique<src::Callout>(p, locCode, fn, ccin, sn, mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500893 }
Patrick Williamse99a4fd2021-09-02 09:44:53 -0500894 catch (const sdbusplus::exception::exception& e)
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500895 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500896 std::string msg =
897 "No VPD found for " + inventoryPath + ": " + e.what();
898 addDebugData(msg);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500899
900 // Just create the callout with empty FRU fields
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500901 callout = std::make_unique<src::Callout>(
902 CalloutPriority::high, locCode, fn, ccin, sn, mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500903 }
Matt Spinlered046852020-03-13 13:58:15 -0500904 }
Patrick Williamse99a4fd2021-09-02 09:44:53 -0500905 catch (const sdbusplus::exception::exception& e)
Matt Spinlered046852020-03-13 13:58:15 -0500906 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500907 std::string msg = "Could not get location code for " + inventoryPath +
908 ": " + e.what();
909 addDebugData(msg);
Matt Spinlered046852020-03-13 13:58:15 -0500910
Matt Spinler479b6922021-08-17 16:34:59 -0500911 // Don't add a callout in this case, because:
912 // 1) With how the inventory is primed, there is no case where
913 // a location code is expected to be missing. This implies
914 // the caller is passing in something invalid.
915 // 2) The addDebugData call above will put the passed in path into
916 // a user data section that can be seen by development for debug.
917 // 3) Even if we wanted to do a 'no_vpd_for_fru' sort of maint.
918 // procedure, we don't have a good way to indicate to the user
919 // anything about the intended callout (they won't see user data).
920 // 4) Creating a new standalone event log for this problem isn't
921 // possible from inside a PEL section.
Matt Spinlered046852020-03-13 13:58:15 -0500922 }
923
Matt Spinler479b6922021-08-17 16:34:59 -0500924 if (callout)
925 {
926 createCalloutsObject();
927 _callouts->addCallout(std::move(callout));
928 }
Matt Spinler03984582020-04-09 13:17:58 -0500929}
Matt Spinlered046852020-03-13 13:58:15 -0500930
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500931std::vector<message::RegistryCallout>
932 SRC::getRegistryCallouts(const message::Entry& regEntry,
933 const AdditionalData& additionalData,
934 const DataInterfaceBase& dataIface)
935{
936 std::vector<message::RegistryCallout> registryCallouts;
937
938 if (regEntry.callouts)
939 {
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500940 std::vector<std::string> systemNames;
941
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500942 try
943 {
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500944 systemNames = dataIface.getSystemNames();
945 }
946 catch (const std::exception& e)
947 {
948 // Compatible interface not available yet
949 }
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500950
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500951 try
952 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500953 registryCallouts = message::Registry::getCallouts(
954 regEntry.callouts.value(), systemNames, additionalData);
955 }
956 catch (const std::exception& e)
957 {
958 addDebugData(fmt::format(
959 "Error parsing PEL message registry callout JSON: {}",
960 e.what()));
961 }
962 }
963
964 return registryCallouts;
965}
966
967void SRC::addRegistryCallouts(
968 const std::vector<message::RegistryCallout>& callouts,
969 const DataInterfaceBase& dataIface,
970 std::optional<std::string> trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -0500971{
972 try
973 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500974 for (const auto& callout : callouts)
Matt Spinler03984582020-04-09 13:17:58 -0500975 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500976 addRegistryCallout(callout, dataIface, trustedSymbolicFRUInvPath);
977
978 // Only the first callout gets the inventory path
979 if (trustedSymbolicFRUInvPath)
980 {
981 trustedSymbolicFRUInvPath = std::nullopt;
982 }
Matt Spinler03984582020-04-09 13:17:58 -0500983 }
984 }
Patrick Williams66491c62021-10-06 12:23:37 -0500985 catch (const std::exception& e)
Matt Spinler03984582020-04-09 13:17:58 -0500986 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500987 std::string msg =
988 "Error parsing PEL message registry callout JSON: "s + e.what();
989 addDebugData(msg);
Matt Spinler03984582020-04-09 13:17:58 -0500990 }
991}
992
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500993void SRC::addRegistryCallout(
994 const message::RegistryCallout& regCallout,
995 const DataInterfaceBase& dataIface,
996 const std::optional<std::string>& trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -0500997{
998 std::unique_ptr<src::Callout> callout;
Matt Spinler03984582020-04-09 13:17:58 -0500999 auto locCode = regCallout.locCode;
1000
Matt Spinleraf191c72020-06-04 11:35:13 -05001001 if (!locCode.empty())
1002 {
1003 try
1004 {
1005 locCode = dataIface.expandLocationCode(locCode, 0);
1006 }
1007 catch (const std::exception& e)
1008 {
1009 auto msg =
1010 "Unable to expand location code " + locCode + ": " + e.what();
1011 addDebugData(msg);
1012 return;
1013 }
1014 }
1015
Matt Spinler03984582020-04-09 13:17:58 -05001016 // Via the PEL values table, get the priority enum.
1017 // The schema will have validated the priority was a valid value.
1018 auto priorityIt =
1019 pv::findByName(regCallout.priority, pv::calloutPriorityValues);
1020 assert(priorityIt != pv::calloutPriorityValues.end());
1021 auto priority =
1022 static_cast<CalloutPriority>(std::get<pv::fieldValuePos>(*priorityIt));
1023
1024 if (!regCallout.procedure.empty())
1025 {
1026 // Procedure callout
1027 callout =
1028 std::make_unique<src::Callout>(priority, regCallout.procedure);
1029 }
1030 else if (!regCallout.symbolicFRU.empty())
1031 {
1032 // Symbolic FRU callout
1033 callout = std::make_unique<src::Callout>(
1034 priority, regCallout.symbolicFRU, locCode, false);
1035 }
1036 else if (!regCallout.symbolicFRUTrusted.empty())
1037 {
1038 // Symbolic FRU with trusted location code callout
1039
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001040 // Use the location code from the inventory path if there is one.
1041 if (trustedSymbolicFRUInvPath)
1042 {
1043 try
1044 {
1045 locCode = dataIface.getLocationCode(*trustedSymbolicFRUInvPath);
1046 }
1047 catch (const std::exception& e)
1048 {
1049 addDebugData(
1050 fmt::format("Could not get location code for {}: {}",
1051 *trustedSymbolicFRUInvPath, e.what()));
1052 locCode.clear();
1053 }
1054 }
1055
Matt Spinler03984582020-04-09 13:17:58 -05001056 // The registry wants it to be trusted, but that requires a valid
1057 // location code for it to actually be.
1058 callout = std::make_unique<src::Callout>(
1059 priority, regCallout.symbolicFRUTrusted, locCode, !locCode.empty());
1060 }
1061 else
1062 {
Matt Spinleraf191c72020-06-04 11:35:13 -05001063 // A hardware callout
1064 std::string inventoryPath;
1065
1066 try
1067 {
1068 // Get the inventory item from the unexpanded location code
1069 inventoryPath =
Matt Spinler2f9225a2020-08-05 12:58:49 -05001070 dataIface.getInventoryFromLocCode(regCallout.locCode, 0, false);
Matt Spinleraf191c72020-06-04 11:35:13 -05001071 }
1072 catch (const std::exception& e)
1073 {
1074 std::string msg =
1075 "Unable to get inventory path from location code: " + locCode +
1076 ": " + e.what();
1077 addDebugData(msg);
1078 return;
1079 }
1080
1081 addInventoryCallout(inventoryPath, priority, locCode, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -05001082 }
1083
1084 if (callout)
1085 {
1086 createCalloutsObject();
1087 _callouts->addCallout(std::move(callout));
1088 }
1089}
Matt Spinlered046852020-03-13 13:58:15 -05001090
Matt Spinler717de422020-06-04 13:10:14 -05001091void SRC::addDevicePathCallouts(const AdditionalData& additionalData,
1092 const DataInterfaceBase& dataIface)
1093{
1094 std::vector<device_callouts::Callout> callouts;
1095 auto i2cBus = additionalData.getValue("CALLOUT_IIC_BUS");
1096 auto i2cAddr = additionalData.getValue("CALLOUT_IIC_ADDR");
1097 auto devPath = additionalData.getValue("CALLOUT_DEVICE_PATH");
1098
1099 // A device callout contains either:
1100 // * CALLOUT_ERRNO, CALLOUT_DEVICE_PATH
1101 // * CALLOUT_ERRNO, CALLOUT_IIC_BUS, CALLOUT_IIC_ADDR
1102 // We don't care about the errno.
1103
1104 if (devPath)
1105 {
1106 try
1107 {
1108 callouts = device_callouts::getCallouts(*devPath,
1109 dataIface.getSystemNames());
1110 }
1111 catch (const std::exception& e)
1112 {
1113 addDebugData(e.what());
1114 callouts.clear();
1115 }
1116 }
1117 else if (i2cBus && i2cAddr)
1118 {
1119 size_t bus;
1120 uint8_t address;
1121
1122 try
1123 {
1124 // If /dev/i2c- is prepended, remove it
1125 if (i2cBus->find("/dev/i2c-") != std::string::npos)
1126 {
1127 *i2cBus = i2cBus->substr(9);
1128 }
1129
1130 bus = stoul(*i2cBus, nullptr, 0);
1131 address = stoul(*i2cAddr, nullptr, 0);
1132 }
1133 catch (const std::exception& e)
1134 {
1135 std::string msg = "Invalid CALLOUT_IIC_BUS " + *i2cBus +
1136 " or CALLOUT_IIC_ADDR " + *i2cAddr +
1137 " in AdditionalData property";
1138 addDebugData(msg);
1139 return;
1140 }
1141
1142 try
1143 {
1144 callouts = device_callouts::getI2CCallouts(
1145 bus, address, dataIface.getSystemNames());
1146 }
1147 catch (const std::exception& e)
1148 {
1149 addDebugData(e.what());
1150 callouts.clear();
1151 }
1152 }
1153
1154 for (const auto& callout : callouts)
1155 {
1156 // The priority shouldn't be invalid, but check just in case.
1157 CalloutPriority priority = CalloutPriority::high;
1158
1159 if (!callout.priority.empty())
1160 {
1161 auto p = pel_values::findByValue(
1162 static_cast<uint32_t>(callout.priority[0]),
1163 pel_values::calloutPriorityValues);
1164
1165 if (p != pel_values::calloutPriorityValues.end())
1166 {
1167 priority = static_cast<CalloutPriority>(callout.priority[0]);
1168 }
1169 else
1170 {
1171 std::string msg =
1172 "Invalid priority found in dev callout JSON: " +
1173 callout.priority[0];
1174 addDebugData(msg);
1175 }
1176 }
1177
Matt Spinler0d92b522021-06-16 13:28:17 -06001178 std::optional<std::string> locCode;
1179
1180 try
1181 {
1182 locCode = dataIface.expandLocationCode(callout.locationCode, 0);
1183 }
1184 catch (const std::exception& e)
1185 {
1186 auto msg = fmt::format("Unable to expand location code {}: {}",
1187 callout.locationCode, e.what());
1188 addDebugData(msg);
1189 }
1190
Matt Spinler717de422020-06-04 13:10:14 -05001191 try
1192 {
Matt Spinler2f9225a2020-08-05 12:58:49 -05001193 auto inventoryPath = dataIface.getInventoryFromLocCode(
1194 callout.locationCode, 0, false);
Matt Spinler717de422020-06-04 13:10:14 -05001195
Matt Spinler0d92b522021-06-16 13:28:17 -06001196 addInventoryCallout(inventoryPath, priority, locCode, dataIface);
Matt Spinler717de422020-06-04 13:10:14 -05001197 }
1198 catch (const std::exception& e)
1199 {
1200 std::string msg =
1201 "Unable to get inventory path from location code: " +
1202 callout.locationCode + ": " + e.what();
1203 addDebugData(msg);
1204 }
1205
1206 // Until the code is there to convert these MRU value strings to
1207 // the official MRU values in the callout objects, just store
1208 // the MRU name in the debug UserData section.
1209 if (!callout.mru.empty())
1210 {
1211 std::string msg = "MRU: " + callout.mru;
1212 addDebugData(msg);
1213 }
1214
1215 // getCallouts() may have generated some debug data it stored
1216 // in a callout object. Save it as well.
1217 if (!callout.debug.empty())
1218 {
1219 addDebugData(callout.debug);
1220 }
1221 }
1222}
1223
Matt Spinler5a90a952020-08-27 09:39:03 -05001224void SRC::addJSONCallouts(const nlohmann::json& jsonCallouts,
1225 const DataInterfaceBase& dataIface)
1226{
1227 if (jsonCallouts.empty())
1228 {
1229 return;
1230 }
1231
1232 if (!jsonCallouts.is_array())
1233 {
1234 addDebugData("Callout JSON isn't an array");
1235 return;
1236 }
1237
1238 for (const auto& callout : jsonCallouts)
1239 {
1240 try
1241 {
1242 addJSONCallout(callout, dataIface);
1243 }
1244 catch (const std::exception& e)
1245 {
1246 addDebugData(fmt::format(
1247 "Failed extracting callout data from JSON: {}", e.what()));
1248 }
1249 }
1250}
1251
1252void SRC::addJSONCallout(const nlohmann::json& jsonCallout,
1253 const DataInterfaceBase& dataIface)
1254{
Matt Spinler3bdd0112020-08-27 10:24:34 -05001255 auto priority = getPriorityFromJSON(jsonCallout);
1256 std::string locCode;
1257 std::string unexpandedLocCode;
1258 std::unique_ptr<src::Callout> callout;
1259
1260 // Expand the location code if it's there
1261 if (jsonCallout.contains("LocationCode"))
1262 {
1263 unexpandedLocCode = jsonCallout.at("LocationCode").get<std::string>();
1264
1265 try
1266 {
1267 locCode = dataIface.expandLocationCode(unexpandedLocCode, 0);
1268 }
1269 catch (const std::exception& e)
1270 {
1271 addDebugData(fmt::format("Unable to expand location code {}: {}",
1272 unexpandedLocCode, e.what()));
1273 // Use the value from the JSON so at least there's something
1274 locCode = unexpandedLocCode;
1275 }
1276 }
1277
1278 // Create either a procedure, symbolic FRU, or normal FRU callout.
1279 if (jsonCallout.contains("Procedure"))
1280 {
1281 auto procedure = jsonCallout.at("Procedure").get<std::string>();
1282
1283 callout = std::make_unique<src::Callout>(
1284 static_cast<CalloutPriority>(priority), procedure,
1285 src::CalloutValueType::raw);
1286 }
1287 else if (jsonCallout.contains("SymbolicFRU"))
1288 {
1289 auto fru = jsonCallout.at("SymbolicFRU").get<std::string>();
1290
1291 bool trusted = false;
1292 if (jsonCallout.contains("TrustedLocationCode") && !locCode.empty())
1293 {
1294 trusted = jsonCallout.at("TrustedLocationCode").get<bool>();
1295 }
1296
1297 callout = std::make_unique<src::Callout>(
1298 static_cast<CalloutPriority>(priority), fru,
1299 src::CalloutValueType::raw, locCode, trusted);
1300 }
1301 else
1302 {
1303 // A hardware FRU
1304 std::string inventoryPath;
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001305 std::vector<src::MRU::MRUCallout> mrus;
Matt Spinler3bdd0112020-08-27 10:24:34 -05001306
1307 if (jsonCallout.contains("InventoryPath"))
1308 {
1309 inventoryPath = jsonCallout.at("InventoryPath").get<std::string>();
1310 }
1311 else
1312 {
1313 if (unexpandedLocCode.empty())
1314 {
1315 throw std::runtime_error{"JSON callout needs either an "
1316 "inventory path or location code"};
1317 }
1318
1319 try
1320 {
1321 inventoryPath = dataIface.getInventoryFromLocCode(
1322 unexpandedLocCode, 0, false);
1323 }
1324 catch (const std::exception& e)
1325 {
1326 throw std::runtime_error{
1327 fmt::format("Unable to get inventory path from "
1328 "location code: {}: {}",
1329 unexpandedLocCode, e.what())};
1330 }
1331 }
1332
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001333 if (jsonCallout.contains("MRUs"))
1334 {
1335 mrus = getMRUsFromJSON(jsonCallout.at("MRUs"));
1336 }
1337
Matt Spinler3bdd0112020-08-27 10:24:34 -05001338 // If the location code was also passed in, use that here too
1339 // so addInventoryCallout doesn't have to look it up.
1340 std::optional<std::string> lc;
1341 if (!locCode.empty())
1342 {
1343 lc = locCode;
1344 }
1345
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001346 addInventoryCallout(inventoryPath, priority, lc, dataIface, mrus);
Matt Spinlerafa2c792020-08-27 11:01:39 -05001347
1348 if (jsonCallout.contains("Deconfigured"))
1349 {
1350 if (jsonCallout.at("Deconfigured").get<bool>())
1351 {
1352 setErrorStatusFlag(ErrorStatusFlags::deconfigured);
1353 }
1354 }
1355
1356 if (jsonCallout.contains("Guarded"))
1357 {
1358 if (jsonCallout.at("Guarded").get<bool>())
1359 {
1360 setErrorStatusFlag(ErrorStatusFlags::guarded);
1361 }
1362 }
Matt Spinler3bdd0112020-08-27 10:24:34 -05001363 }
1364
1365 if (callout)
1366 {
1367 createCalloutsObject();
1368 _callouts->addCallout(std::move(callout));
1369 }
1370}
1371
1372CalloutPriority SRC::getPriorityFromJSON(const nlohmann::json& json)
1373{
1374 // Looks like:
1375 // {
1376 // "Priority": "H"
1377 // }
1378 auto p = json.at("Priority").get<std::string>();
1379 if (p.empty())
1380 {
1381 throw std::runtime_error{"Priority field in callout is empty"};
1382 }
1383
1384 auto priority = static_cast<CalloutPriority>(p.front());
1385
1386 // Validate it
1387 auto priorityIt = pv::findByValue(static_cast<uint32_t>(priority),
1388 pv::calloutPriorityValues);
1389 if (priorityIt == pv::calloutPriorityValues.end())
1390 {
1391 throw std::runtime_error{
1392 fmt::format("Invalid priority '{}' found in JSON callout", p)};
1393 }
1394
1395 return priority;
Matt Spinler5a90a952020-08-27 09:39:03 -05001396}
1397
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001398std::vector<src::MRU::MRUCallout>
1399 SRC::getMRUsFromJSON(const nlohmann::json& mruJSON)
1400{
1401 std::vector<src::MRU::MRUCallout> mrus;
1402
1403 // Looks like:
1404 // [
1405 // {
1406 // "ID": 100,
1407 // "Priority": "H"
1408 // }
1409 // ]
1410 if (!mruJSON.is_array())
1411 {
1412 addDebugData("MRU callout JSON is not an array");
1413 return mrus;
1414 }
1415
1416 for (const auto& mruCallout : mruJSON)
1417 {
1418 try
1419 {
1420 auto priority = getPriorityFromJSON(mruCallout);
1421 auto id = mruCallout.at("ID").get<uint32_t>();
1422
1423 src::MRU::MRUCallout mru{static_cast<uint32_t>(priority), id};
1424 mrus.push_back(std::move(mru));
1425 }
1426 catch (const std::exception& e)
1427 {
1428 addDebugData(fmt::format("Invalid MRU entry in JSON: {}: {}",
1429 mruCallout.dump(), e.what()));
1430 }
1431 }
1432
1433 return mrus;
1434}
1435
Sumit Kumar9d43a722021-08-24 09:46:19 -05001436void SRC::setDumpStatus(const DataInterfaceBase& dataIface)
1437{
1438 std::vector<bool> dumpStatus{false, false, false};
1439
1440 try
1441 {
1442 std::vector<std::string> dumpType = {"bmc/entry", "resource/entry",
1443 "system/entry"};
1444 dumpStatus = dataIface.checkDumpStatus(dumpType);
1445
1446 // For bmc - set bit 0 of nibble [4-7] bits of byte-1 SP dump
1447 // For resource - set bit 2 of nibble [4-7] bits of byte-2 Hypervisor
1448 // For system - set bit 1 of nibble [4-7] bits of byte-2 HW dump
1449 _hexData[0] |= ((dumpStatus[0] << 19) | (dumpStatus[1] << 9) |
1450 (dumpStatus[2] << 10));
1451 }
1452 catch (const std::exception& e)
1453 {
Matt Spinler35a405b2022-03-02 11:42:42 -06001454 log<level::ERR>(
1455 fmt::format("Checking dump status failed: {}", e.what()).c_str());
Sumit Kumar9d43a722021-08-24 09:46:19 -05001456 }
1457}
1458
Sumit Kumar3e274432021-09-14 06:37:56 -05001459std::vector<uint8_t> SRC::getSrcStruct()
1460{
1461 std::vector<uint8_t> data;
1462 Stream stream{data};
1463
1464 //------ Ref section 4.3 in PEL doc---
1465 //------ SRC Structure 40 bytes-------
1466 // Byte-0 | Byte-1 | Byte-2 | Byte-3 |
1467 // -----------------------------------
1468 // 02 | 08 | 00 | 09 | ==> Header
1469 // 00 | 00 | 00 | 48 | ==> Header
1470 // 00 | 00 | 00 | 00 | ==> Hex data word-2
1471 // 00 | 00 | 00 | 00 | ==> Hex data word-3
1472 // 00 | 00 | 00 | 00 | ==> Hex data word-4
1473 // 20 | 00 | 00 | 00 | ==> Hex data word-5
1474 // 00 | 00 | 00 | 00 | ==> Hex data word-6
1475 // 00 | 00 | 00 | 00 | ==> Hex data word-7
1476 // 00 | 00 | 00 | 00 | ==> Hex data word-8
1477 // 00 | 00 | 00 | 00 | ==> Hex data word-9
1478 // -----------------------------------
1479 // ASCII string - 8 bytes |
1480 // -----------------------------------
1481 // ASCII space NULL - 24 bytes |
1482 // -----------------------------------
1483 //_size = Base SRC struct: 8 byte header + hex data section + ASCII string
1484
1485 uint8_t flags = (_flags | postOPPanel);
1486
1487 stream << _version << flags << _reserved1B << _wordCount << _reserved2B
1488 << _size;
1489
1490 for (auto& word : _hexData)
1491 {
1492 stream << word;
1493 }
1494
1495 _asciiString->flatten(stream);
1496
1497 return data;
1498}
1499
Matt Spinlerf9bae182019-10-09 13:37:38 -05001500} // namespace pels
1501} // namespace openpower