blob: 063c2ad40a644cdee26ec03e4d31f938aa2c6c63 [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>
Patrick Williams2544b412022-10-04 08:41:06 -050026
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080027#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
Sumit Kumar516935a2021-04-14 13:00:54 -050045using orderedJSON = nlohmann::ordered_json;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080046
47void pyDecRef(PyObject* pyObj)
48{
49 Py_XDECREF(pyObj);
50}
51
52/**
53 * @brief Returns a JSON string to append to SRC section.
54 *
55 * The returning string will contain a JSON object, but without
56 * the outer {}. If the input JSON isn't a JSON object (dict), then
57 * one will be created with the input added to a 'SRC Details' key.
58 *
59 * @param[in] json - The JSON to convert to a string
60 *
61 * @return std::string - The JSON string
62 */
Sumit Kumar516935a2021-04-14 13:00:54 -050063std::string prettyJSON(const orderedJSON& json)
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080064{
Sumit Kumar516935a2021-04-14 13:00:54 -050065 orderedJSON output;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080066 if (!json.is_object())
67 {
68 output["SRC Details"] = json;
69 }
70 else
71 {
72 for (const auto& [key, value] : json.items())
73 {
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +080074 output["SRC Details"][key] = value;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080075 }
76 }
77
78 // Let nlohmann do the pretty printing.
79 std::stringstream stream;
80 stream << std::setw(4) << output;
81
82 auto jsonString = stream.str();
83
84 // Now it looks like:
85 // {
86 // "Key": "Value",
87 // ...
88 // }
89
90 // Replace the { and the following newline, and the } and its
91 // preceeding newline.
92 jsonString.erase(0, 2);
93
94 auto pos = jsonString.find_last_of('}');
95 jsonString.erase(pos - 1);
96
97 return jsonString;
98}
99
100/**
101 * @brief Call Python modules to parse the data into a JSON string
102 *
103 * The module to call is based on the Creator Subsystem ID under the namespace
104 * "srcparsers". For example: "srcparsers.xsrc.xsrc" where "x" is the Creator
105 * Subsystem ID in ASCII lowercase.
106 *
107 * All modules must provide the following:
108 * Function: parseSRCToJson
109 * Argument list:
110 * 1. (str) ASCII string (Hex Word 1)
111 * 2. (str) Hex Word 2
112 * 3. (str) Hex Word 3
113 * 4. (str) Hex Word 4
114 * 5. (str) Hex Word 5
115 * 6. (str) Hex Word 6
116 * 7. (str) Hex Word 7
117 * 8. (str) Hex Word 8
118 * 9. (str) Hex Word 9
119 *-Return data:
120 * 1. (str) JSON string
121 *
122 * @param[in] hexwords - Vector of strings of Hexwords 1-9
123 * @param[in] creatorID - The creatorID from the Private Header section
124 * @return std::optional<std::string> - The JSON string if it could be created,
125 * else std::nullopt
126 */
127std::optional<std::string> getPythonJSON(std::vector<std::string>& hexwords,
128 uint8_t creatorID)
129{
Matt Spinlerbe952d22022-07-01 11:30:11 -0500130 PyObject *pName, *pModule, *eType, *eValue, *eTraceback;
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";
Matt Spinlerbe952d22022-07-01 11:30:11 -0500165 PyObject* pKey = PyUnicode_FromString(funcToCall.c_str());
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800166 std::unique_ptr<PyObject, decltype(&pyDecRef)> keyPtr(pKey, &pyDecRef);
Matt Spinlerbe952d22022-07-01 11:30:11 -0500167 PyObject* 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 }
Matt Spinlerbe952d22022-07-01 11:30:11 -0500180 PyObject* pFunc = PyDict_GetItemString(pDict, funcToCall.c_str());
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800181 Py_DECREF(pDict);
182 Py_INCREF(pFunc);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800183 if (PyCallable_Check(pFunc))
184 {
Matt Spinlerbe952d22022-07-01 11:30:11 -0500185 PyObject* pArgs = PyTuple_New(9);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800186 std::unique_ptr<PyObject, decltype(&pyDecRef)> argPtr(pArgs,
187 &pyDecRef);
188 for (size_t i = 0; i < 9; i++)
189 {
Matt Spinlerc1984032023-01-05 10:09:59 -0600190 std::string arg{"00000000"};
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800191 if (i < hexwords.size())
192 {
Matt Spinlerc1984032023-01-05 10:09:59 -0600193 arg = hexwords[i];
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800194 }
Matt Spinlerc1984032023-01-05 10:09:59 -0600195 PyTuple_SetItem(pArgs, i, Py_BuildValue("s", arg.c_str()));
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800196 }
Matt Spinlerbe952d22022-07-01 11:30:11 -0500197 PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800198 Py_DECREF(pFunc);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800199 if (pResult)
200 {
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800201 std::unique_ptr<PyObject, decltype(&pyDecRef)> resPtr(
202 pResult, &pyDecRef);
Patrick Williams2544b412022-10-04 08:41:06 -0500203 PyObject* pBytes = PyUnicode_AsEncodedString(pResult, "utf-8",
204 "~E~");
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800205 std::unique_ptr<PyObject, decltype(&pyDecRef)> pyBytePtr(
206 pBytes, &pyDecRef);
207 const char* output = PyBytes_AS_STRING(pBytes);
208 try
209 {
Matt Spinlerbb1c1d52021-06-03 13:18:48 -0600210 orderedJSON json = orderedJSON::parse(output);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800211 if ((json.is_object() && !json.empty()) ||
212 (json.is_array() && json.size() > 0) ||
213 (json.is_string() && json != ""))
214 {
215 return prettyJSON(json);
216 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800217 }
Patrick Williams66491c62021-10-06 12:23:37 -0500218 catch (const std::exception& e)
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800219 {
220 log<level::ERR>("Bad JSON from parser",
221 entry("ERROR=%s", e.what()),
222 entry("SRC=%s", hexwords.front().c_str()),
223 entry("PARSER_MODULE=%s", module.c_str()));
224 return std::nullopt;
225 }
226 }
227 else
228 {
229 pErrStr = "No error string found";
230 PyErr_Fetch(&eType, &eValue, &eTraceback);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800231 if (eType)
232 {
233 Py_XDECREF(eType);
234 }
235 if (eTraceback)
236 {
237 Py_XDECREF(eTraceback);
238 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800239 if (eValue)
240 {
241 PyObject* pStr = PyObject_Str(eValue);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800242 Py_XDECREF(eValue);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800243 if (pStr)
244 {
245 pErrStr = PyUnicode_AsUTF8(pStr);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800246 Py_XDECREF(pStr);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800247 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800248 }
249 }
250 }
251 }
252 if (!pErrStr.empty())
253 {
Matt Spinler3279cc52022-02-15 11:02:00 -0600254 log<level::DEBUG>("Python exception thrown by parser",
255 entry("ERROR=%s", pErrStr.c_str()),
256 entry("SRC=%s", hexwords.front().c_str()),
257 entry("PARSER_MODULE=%s", module.c_str()));
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800258 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800259 return std::nullopt;
260}
261#endif
262
Matt Spinlerf9bae182019-10-09 13:37:38 -0500263void SRC::unflatten(Stream& stream)
264{
265 stream >> _header >> _version >> _flags >> _reserved1B >> _wordCount >>
266 _reserved2B >> _size;
267
268 for (auto& word : _hexData)
269 {
270 stream >> word;
271 }
272
273 _asciiString = std::make_unique<src::AsciiString>(stream);
274
275 if (hasAdditionalSections())
276 {
277 // The callouts section is currently the only extra subsection type
278 _callouts = std::make_unique<src::Callouts>(stream);
279 }
280}
281
Matt Spinler06885452019-11-06 10:35:42 -0600282void SRC::flatten(Stream& stream) const
Matt Spinlerf9bae182019-10-09 13:37:38 -0500283{
284 stream << _header << _version << _flags << _reserved1B << _wordCount
285 << _reserved2B << _size;
286
287 for (auto& word : _hexData)
288 {
289 stream << word;
290 }
291
292 _asciiString->flatten(stream);
293
294 if (_callouts)
295 {
296 _callouts->flatten(stream);
297 }
298}
299
300SRC::SRC(Stream& pel)
301{
302 try
303 {
304 unflatten(pel);
305 validate();
306 }
307 catch (const std::exception& e)
308 {
Matt Spinler8ac20502023-01-04 11:03:58 -0600309 log<level::ERR>(
310 fmt::format("Cannot unflatten SRC: {}", e.what()).c_str());
Matt Spinlerf9bae182019-10-09 13:37:38 -0500311 _valid = false;
312 }
313}
314
Matt Spinler075e5ba2020-02-21 15:46:00 -0600315SRC::SRC(const message::Entry& regEntry, const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500316 const nlohmann::json& jsonCallouts, const DataInterfaceBase& dataIface)
Matt Spinlerbd716f02019-10-15 10:54:11 -0500317{
318 _header.id = static_cast<uint16_t>(SectionID::primarySRC);
319 _header.version = srcSectionVersion;
320 _header.subType = srcSectionSubtype;
321 _header.componentID = regEntry.componentID;
322
323 _version = srcVersion;
324
325 _flags = 0;
Vijay Lobof3702bb2021-04-09 15:10:19 -0500326
Matt Spinlerbd716f02019-10-15 10:54:11 -0500327 _reserved1B = 0;
328
329 _wordCount = numSRCHexDataWords + 1;
330
331 _reserved2B = 0;
332
333 // There are multiple fields encoded in the hex data words.
334 std::for_each(_hexData.begin(), _hexData.end(),
335 [](auto& word) { word = 0; });
Matt Spinler7c619182020-07-27 15:15:11 -0500336
337 // Hex Word 2 Nibbles:
338 // MIGVEPFF
339 // M: Partition dump status = 0
340 // I: System boot state = TODO
341 // G: Partition Boot type = 0
Sumit Kumar9d43a722021-08-24 09:46:19 -0500342 // V: BMC dump status
Matt Spinler7c619182020-07-27 15:15:11 -0500343 // E: Platform boot mode = 0 (side = temporary, speed = fast)
Sumit Kumar9d43a722021-08-24 09:46:19 -0500344 // P: Platform dump status
Matt Spinler7c619182020-07-27 15:15:11 -0500345 // FF: SRC format, set below
346
Vijay Lobo875b6c72021-10-20 17:38:56 -0500347 setProgressCode(dataIface);
Sumit Kumar9d43a722021-08-24 09:46:19 -0500348 setDumpStatus(dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500349 setBMCFormat();
350 setBMCPosition();
Matt Spinler075e5ba2020-02-21 15:46:00 -0600351 setMotherboardCCIN(dataIface);
352
Matt Spinlerbd716f02019-10-15 10:54:11 -0500353 // Fill in the last 4 words from the AdditionalData property contents.
354 setUserDefinedHexWords(regEntry, additionalData);
355
356 _asciiString = std::make_unique<src::AsciiString>(regEntry);
357
Sumit Kumar50bfa692022-01-06 06:48:26 -0600358 // Check for additional data - PEL_SUBSYSTEM
359 auto ss = additionalData.getValue("PEL_SUBSYSTEM");
360 if (ss)
361 {
362 auto eventSubsystem = std::stoul(*ss, NULL, 16);
Patrick Williams2544b412022-10-04 08:41:06 -0500363 std::string subsystem = pv::getValue(eventSubsystem,
364 pel_values::subsystemValues);
Sumit Kumar50bfa692022-01-06 06:48:26 -0600365 if (subsystem == "invalid")
366 {
367 log<level::WARNING>(
368 fmt::format("SRC: Invalid SubSystem value:{:#X}",
369 eventSubsystem)
370 .c_str());
371 }
372 else
373 {
374 _asciiString->setByte(2, eventSubsystem);
375 }
376 }
377
Matt Spinler5a90a952020-08-27 09:39:03 -0500378 addCallouts(regEntry, additionalData, jsonCallouts, dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500379
380 _size = baseSRCSize;
381 _size += _callouts ? _callouts->flattenedSize() : 0;
382 _header.size = Section::flattenedSize() + _size;
383
384 _valid = true;
385}
386
387void SRC::setUserDefinedHexWords(const message::Entry& regEntry,
388 const AdditionalData& ad)
389{
390 if (!regEntry.src.hexwordADFields)
391 {
392 return;
393 }
394
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800395 // Save the AdditionalData value corresponding to the first element of
396 // adName tuple into _hexData[wordNum].
Matt Spinlerbd716f02019-10-15 10:54:11 -0500397 for (const auto& [wordNum, adName] : *regEntry.src.hexwordADFields)
398 {
399 // Can only set words 6 - 9
400 if (!isUserDefinedWord(wordNum))
401 {
Patrick Williams2544b412022-10-04 08:41:06 -0500402 std::string msg = "SRC user data word out of range: " +
403 std::to_string(wordNum);
Matt Spinler85f61a62020-06-03 16:28:55 -0500404 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500405 continue;
406 }
407
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800408 auto value = ad.getValue(std::get<0>(adName));
Matt Spinlerbd716f02019-10-15 10:54:11 -0500409 if (value)
410 {
411 _hexData[getWordIndexFromWordNum(wordNum)] =
412 std::strtoul(value.value().c_str(), nullptr, 0);
413 }
414 else
415 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800416 std::string msg = "Source for user data SRC word not found: " +
417 std::get<0>(adName);
Matt Spinler85f61a62020-06-03 16:28:55 -0500418 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500419 }
420 }
421}
422
Matt Spinler075e5ba2020-02-21 15:46:00 -0600423void SRC::setMotherboardCCIN(const DataInterfaceBase& dataIface)
424{
425 uint32_t ccin = 0;
426 auto ccinString = dataIface.getMotherboardCCIN();
427
428 try
429 {
430 if (ccinString.size() == ccinSize)
431 {
432 ccin = std::stoi(ccinString, 0, 16);
433 }
434 }
Patrick Williams66491c62021-10-06 12:23:37 -0500435 catch (const std::exception& e)
Matt Spinler075e5ba2020-02-21 15:46:00 -0600436 {
437 log<level::WARNING>("Could not convert motherboard CCIN to a number",
438 entry("CCIN=%s", ccinString.c_str()));
439 return;
440 }
441
442 // Set the first 2 bytes
443 _hexData[1] |= ccin << 16;
444}
445
Matt Spinlerf9bae182019-10-09 13:37:38 -0500446void SRC::validate()
447{
448 bool failed = false;
449
450 if ((header().id != static_cast<uint16_t>(SectionID::primarySRC)) &&
451 (header().id != static_cast<uint16_t>(SectionID::secondarySRC)))
452 {
Matt Spinler8ac20502023-01-04 11:03:58 -0600453 log<level::ERR>(
454 fmt::format("Invalid SRC section ID: {0:#x}", header().id).c_str());
Matt Spinlerf9bae182019-10-09 13:37:38 -0500455 failed = true;
456 }
457
458 // Check the version in the SRC, not in the header
Matt Spinlerbd716f02019-10-15 10:54:11 -0500459 if (_version != srcVersion)
Matt Spinlerf9bae182019-10-09 13:37:38 -0500460 {
Matt Spinler8ac20502023-01-04 11:03:58 -0600461 log<level::ERR>(
462 fmt::format("Invalid SRC version: {0:#x}", header().version)
463 .c_str());
Matt Spinlerf9bae182019-10-09 13:37:38 -0500464 failed = true;
465 }
466
467 _valid = failed ? false : true;
468}
469
Matt Spinler075e5ba2020-02-21 15:46:00 -0600470bool SRC::isBMCSRC() const
471{
472 auto as = asciiString();
473 if (as.length() >= 2)
474 {
475 uint8_t errorType = strtoul(as.substr(0, 2).c_str(), nullptr, 16);
476 return (errorType == static_cast<uint8_t>(SRCType::bmcError) ||
477 errorType == static_cast<uint8_t>(SRCType::powerError));
478 }
479 return false;
480}
481
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800482std::optional<std::string> SRC::getErrorDetails(message::Registry& registry,
483 DetailLevel type,
484 bool toCache) const
485{
486 const std::string jsonIndent(indentLevel, 0x20);
487 std::string errorOut;
Matt Spinler075e5ba2020-02-21 15:46:00 -0600488 if (isBMCSRC())
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800489 {
490 auto entry = registry.lookup("0x" + asciiString().substr(4, 4),
491 rg::LookupType::reasonCode, toCache);
492 if (entry)
493 {
494 errorOut.append(jsonIndent + "\"Error Details\": {\n");
495 auto errorMsg = getErrorMessage(*entry);
496 if (errorMsg)
497 {
498 if (type == DetailLevel::message)
499 {
500 return errorMsg.value();
501 }
502 else
503 {
504 jsonInsert(errorOut, "Message", errorMsg.value(), 2);
505 }
506 }
507 if (entry->src.hexwordADFields)
508 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800509 std::map<size_t, std::tuple<std::string, std::string>>
510 adFields = entry->src.hexwordADFields.value();
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800511 for (const auto& hexwordMap : adFields)
512 {
Zane Shelleye8db29b2021-11-13 10:34:07 -0600513 auto srcValue = getNumberString(
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800514 "0x%X",
Zane Shelleye8db29b2021-11-13 10:34:07 -0600515 _hexData[getWordIndexFromWordNum(hexwordMap.first)]);
516
517 auto srcKey = std::get<0>(hexwordMap.second);
518 auto srcDesc = std::get<1>(hexwordMap.second);
519
520 // Only include this hex word in the error details if the
521 // description exists.
522 if (!srcDesc.empty())
523 {
524 std::vector<std::string> valueDescr;
525 valueDescr.push_back(srcValue);
526 valueDescr.push_back(srcDesc);
527 jsonInsertArray(errorOut, srcKey, valueDescr, 2);
528 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800529 }
530 }
531 errorOut.erase(errorOut.size() - 2);
532 errorOut.append("\n");
533 errorOut.append(jsonIndent + "},\n");
534 return errorOut;
535 }
536 }
537 return std::nullopt;
538}
539
540std::optional<std::string>
541 SRC::getErrorMessage(const message::Entry& regEntry) const
542{
543 try
544 {
545 if (regEntry.doc.messageArgSources)
546 {
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800547 std::vector<uint32_t> argSourceVals;
548 std::string message;
549 const auto& argValues = regEntry.doc.messageArgSources.value();
550 for (size_t i = 0; i < argValues.size(); ++i)
551 {
552 argSourceVals.push_back(_hexData[getWordIndexFromWordNum(
553 argValues[i].back() - '0')]);
554 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500555
556 auto it = std::begin(regEntry.doc.message);
557 auto it_end = std::end(regEntry.doc.message);
558
559 while (it != it_end)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800560 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500561 if (*it == '%')
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800562 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500563 ++it;
564
565 size_t wordIndex = *it - '0';
566 if (isdigit(*it) && wordIndex >= 1 &&
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800567 static_cast<uint16_t>(wordIndex) <=
568 argSourceVals.size())
569 {
570 message.append(getNumberString(
Zane Shelley39936e32021-11-13 16:19:34 -0600571 "0x%08X", argSourceVals[wordIndex - 1]));
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800572 }
573 else
574 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500575 message.append("%" + std::string(1, *it));
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800576 }
577 }
578 else
579 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500580 message.push_back(*it);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800581 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500582 ++it;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800583 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500584
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800585 return message;
586 }
587 else
588 {
589 return regEntry.doc.message;
590 }
591 }
592 catch (const std::exception& e)
593 {
594 log<level::ERR>("Cannot get error message from registry entry",
595 entry("ERROR=%s", e.what()));
596 }
597 return std::nullopt;
598}
599
600std::optional<std::string> SRC::getCallouts() const
601{
602 if (!_callouts)
603 {
604 return std::nullopt;
605 }
606 std::string printOut;
607 const std::string jsonIndent(indentLevel, 0x20);
608 const auto& callout = _callouts->callouts();
609 const auto& compDescrp = pv::failingComponentType;
610 printOut.append(jsonIndent + "\"Callout Section\": {\n");
611 jsonInsert(printOut, "Callout Count", std::to_string(callout.size()), 2);
612 printOut.append(jsonIndent + jsonIndent + "\"Callouts\": [");
613 for (auto& entry : callout)
614 {
615 printOut.append("{\n");
616 if (entry->fruIdentity())
617 {
618 jsonInsert(
619 printOut, "FRU Type",
620 compDescrp.at(entry->fruIdentity()->failingComponentType()), 3);
621 jsonInsert(printOut, "Priority",
622 pv::getValue(entry->priority(),
623 pel_values::calloutPriorityValues),
624 3);
625 if (!entry->locationCode().empty())
626 {
627 jsonInsert(printOut, "Location Code", entry->locationCode(), 3);
628 }
629 if (entry->fruIdentity()->getPN().has_value())
630 {
631 jsonInsert(printOut, "Part Number",
632 entry->fruIdentity()->getPN().value(), 3);
633 }
634 if (entry->fruIdentity()->getMaintProc().has_value())
635 {
Matt Spinler9e8b49e2020-09-10 13:15:26 -0500636 jsonInsert(printOut, "Procedure",
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800637 entry->fruIdentity()->getMaintProc().value(), 3);
638 if (pv::procedureDesc.find(
639 entry->fruIdentity()->getMaintProc().value()) !=
640 pv::procedureDesc.end())
641 {
642 jsonInsert(
643 printOut, "Description",
644 pv::procedureDesc.at(
645 entry->fruIdentity()->getMaintProc().value()),
646 3);
647 }
648 }
649 if (entry->fruIdentity()->getCCIN().has_value())
650 {
651 jsonInsert(printOut, "CCIN",
652 entry->fruIdentity()->getCCIN().value(), 3);
653 }
654 if (entry->fruIdentity()->getSN().has_value())
655 {
656 jsonInsert(printOut, "Serial Number",
657 entry->fruIdentity()->getSN().value(), 3);
658 }
659 }
660 if (entry->pceIdentity())
661 {
662 const auto& pceIdentMtms = entry->pceIdentity()->mtms();
663 if (!pceIdentMtms.machineTypeAndModel().empty())
664 {
665 jsonInsert(printOut, "PCE MTMS",
666 pceIdentMtms.machineTypeAndModel() + "_" +
667 pceIdentMtms.machineSerialNumber(),
668 3);
669 }
670 if (!entry->pceIdentity()->enclosureName().empty())
671 {
672 jsonInsert(printOut, "PCE Name",
673 entry->pceIdentity()->enclosureName(), 3);
674 }
675 }
676 if (entry->mru())
677 {
678 const auto& mruCallouts = entry->mru()->mrus();
679 std::string mruId;
680 for (auto& element : mruCallouts)
681 {
682 if (!mruId.empty())
683 {
684 mruId.append(", " + getNumberString("%08X", element.id));
685 }
686 else
687 {
688 mruId.append(getNumberString("%08X", element.id));
689 }
690 }
691 jsonInsert(printOut, "MRU Id", mruId, 3);
692 }
693 printOut.erase(printOut.size() - 2);
694 printOut.append("\n" + jsonIndent + jsonIndent + "}, ");
695 };
696 printOut.erase(printOut.size() - 2);
697 printOut.append("]\n" + jsonIndent + "}");
698 return printOut;
699}
700
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800701std::optional<std::string> SRC::getJSON(message::Registry& registry,
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500702 const std::vector<std::string>& plugins
703 [[maybe_unused]],
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800704 uint8_t creatorID) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800705{
706 std::string ps;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800707 std::vector<std::string> hexwords;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800708 jsonInsert(ps, pv::sectionVer, getNumberString("%d", _header.version), 1);
709 jsonInsert(ps, pv::subSection, getNumberString("%d", _header.subType), 1);
Matt Spinlerb832aa52023-03-21 15:32:34 -0500710 jsonInsert(ps, pv::createdBy,
711 getComponentName(_header.componentID, creatorID), 1);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800712 jsonInsert(ps, "SRC Version", getNumberString("0x%02X", _version), 1);
Harisuddin Mohamed Isac32e5512020-02-06 18:05:21 +0800713 jsonInsert(ps, "SRC Format", getNumberString("0x%02X", _hexData[0] & 0xFF),
714 1);
715 jsonInsert(ps, "Virtual Progress SRC",
716 pv::boolString.at(_flags & virtualProgressSRC), 1);
717 jsonInsert(ps, "I5/OS Service Event Bit",
718 pv::boolString.at(_flags & i5OSServiceEventBit), 1);
719 jsonInsert(ps, "Hypervisor Dump Initiated",
720 pv::boolString.at(_flags & hypDumpInit), 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600721
722 if (isBMCSRC())
723 {
724 std::string ccinString;
725 uint32_t ccin = _hexData[1] >> 16;
726
727 if (ccin)
728 {
729 ccinString = getNumberString("%04X", ccin);
730 }
731 // The PEL spec calls it a backplane, so call it that here.
732 jsonInsert(ps, "Backplane CCIN", ccinString, 1);
Matt Spinlerafa2c792020-08-27 11:01:39 -0500733
Sumit Kumar3e274432021-09-14 06:37:56 -0500734 jsonInsert(ps, "Terminate FW Error",
735 pv::boolString.at(
736 _hexData[3] &
737 static_cast<uint32_t>(ErrorStatusFlags::terminateFwErr)),
738 1);
Matt Spinlerafa2c792020-08-27 11:01:39 -0500739 jsonInsert(ps, "Deconfigured",
740 pv::boolString.at(
741 _hexData[3] &
742 static_cast<uint32_t>(ErrorStatusFlags::deconfigured)),
743 1);
744
745 jsonInsert(
746 ps, "Guarded",
747 pv::boolString.at(_hexData[3] &
748 static_cast<uint32_t>(ErrorStatusFlags::guarded)),
749 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600750 }
751
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800752 auto errorDetails = getErrorDetails(registry, DetailLevel::json, true);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800753 if (errorDetails)
754 {
755 ps.append(errorDetails.value());
756 }
757 jsonInsert(ps, "Valid Word Count", getNumberString("0x%02X", _wordCount),
758 1);
759 std::string refcode = asciiString();
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800760 hexwords.push_back(refcode);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800761 std::string extRefcode;
762 size_t pos = refcode.find(0x20);
763 if (pos != std::string::npos)
764 {
765 size_t nextPos = refcode.find_first_not_of(0x20, pos);
766 if (nextPos != std::string::npos)
767 {
768 extRefcode = trimEnd(refcode.substr(nextPos));
769 }
770 refcode.erase(pos);
771 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800772 jsonInsert(ps, "Reference Code", refcode, 1);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800773 if (!extRefcode.empty())
774 {
775 jsonInsert(ps, "Extended Reference Code", extRefcode, 1);
776 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800777 for (size_t i = 2; i <= _wordCount; i++)
778 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800779 std::string tmpWord =
780 getNumberString("%08X", _hexData[getWordIndexFromWordNum(i)]);
781 jsonInsert(ps, "Hex Word " + std::to_string(i), tmpWord, 1);
782 hexwords.push_back(tmpWord);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800783 }
784 auto calloutJson = getCallouts();
785 if (calloutJson)
786 {
787 ps.append(calloutJson.value());
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800788 ps.append(",\n");
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800789 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800790 std::string subsystem = getNumberString("%c", tolower(creatorID));
791 bool srcDetailExists = false;
792#ifdef PELTOOL
793 if (std::find(plugins.begin(), plugins.end(), subsystem + "src") !=
794 plugins.end())
795 {
796 auto pyJson = getPythonJSON(hexwords, creatorID);
797 if (pyJson)
798 {
799 ps.append(pyJson.value());
800 srcDetailExists = true;
801 }
802 }
803#endif
804 if (!srcDetailExists)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800805 {
806 ps.erase(ps.size() - 2);
807 }
808 return ps;
809}
810
Matt Spinler03984582020-04-09 13:17:58 -0500811void SRC::addCallouts(const message::Entry& regEntry,
812 const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500813 const nlohmann::json& jsonCallouts,
Matt Spinlered046852020-03-13 13:58:15 -0500814 const DataInterfaceBase& dataIface)
815{
Patrick Williams2544b412022-10-04 08:41:06 -0500816 auto registryCallouts = getRegistryCallouts(regEntry, additionalData,
817 dataIface);
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500818
Matt Spinlered046852020-03-13 13:58:15 -0500819 auto item = additionalData.getValue("CALLOUT_INVENTORY_PATH");
Miguel Gomez53ef1552020-10-14 21:16:32 +0000820 auto priority = additionalData.getValue("CALLOUT_PRIORITY");
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500821
Miguel Gomez53ef1552020-10-14 21:16:32 +0000822 std::optional<CalloutPriority> calloutPriority;
823
824 // Only H, M or L priority values.
825 if (priority && !(*priority).empty())
826 {
827 uint8_t p = (*priority)[0];
828 if (p == 'H' || p == 'M' || p == 'L')
829 {
830 calloutPriority = static_cast<CalloutPriority>(p);
831 }
832 }
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500833 // If the first registry callout says to use the passed in inventory
834 // path to get the location code for a symbolic FRU callout with a
835 // trusted location code, then do not add the inventory path as a
836 // normal FRU callout.
837 bool useInvForSymbolicFRULocCode =
838 !registryCallouts.empty() && registryCallouts[0].useInventoryLocCode &&
839 !registryCallouts[0].symbolicFRUTrusted.empty();
840
841 if (item && !useInvForSymbolicFRULocCode)
Matt Spinlered046852020-03-13 13:58:15 -0500842 {
Miguel Gomez53ef1552020-10-14 21:16:32 +0000843 addInventoryCallout(*item, calloutPriority, std::nullopt, dataIface);
Matt Spinlered046852020-03-13 13:58:15 -0500844 }
845
Matt Spinler717de422020-06-04 13:10:14 -0500846 addDevicePathCallouts(additionalData, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -0500847
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500848 addRegistryCallouts(registryCallouts, dataIface,
849 (useInvForSymbolicFRULocCode) ? item : std::nullopt);
Matt Spinler5a90a952020-08-27 09:39:03 -0500850
851 if (!jsonCallouts.empty())
852 {
853 addJSONCallouts(jsonCallouts, dataIface);
854 }
Matt Spinlered046852020-03-13 13:58:15 -0500855}
856
857void SRC::addInventoryCallout(const std::string& inventoryPath,
Matt Spinleraf191c72020-06-04 11:35:13 -0500858 const std::optional<CalloutPriority>& priority,
859 const std::optional<std::string>& locationCode,
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500860 const DataInterfaceBase& dataIface,
861 const std::vector<src::MRU::MRUCallout>& mrus)
Matt Spinlered046852020-03-13 13:58:15 -0500862{
863 std::string locCode;
864 std::string fn;
865 std::string ccin;
866 std::string sn;
867 std::unique_ptr<src::Callout> callout;
868
Matt Spinlered046852020-03-13 13:58:15 -0500869 try
870 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500871 // Use the passed in location code if there otherwise look it up
872 if (locationCode)
873 {
874 locCode = *locationCode;
875 }
876 else
877 {
878 locCode = dataIface.getLocationCode(inventoryPath);
879 }
Matt Spinlered046852020-03-13 13:58:15 -0500880
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500881 try
882 {
883 dataIface.getHWCalloutFields(inventoryPath, fn, ccin, sn);
884
Patrick Williams2544b412022-10-04 08:41:06 -0500885 CalloutPriority p = priority ? priority.value()
886 : CalloutPriority::high;
Matt Spinleraf191c72020-06-04 11:35:13 -0500887
Patrick Williams2544b412022-10-04 08:41:06 -0500888 callout = std::make_unique<src::Callout>(p, locCode, fn, ccin, sn,
889 mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500890 }
Patrick Williams45e83522022-07-22 19:26:52 -0500891 catch (const sdbusplus::exception_t& e)
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500892 {
Patrick Williams2544b412022-10-04 08:41:06 -0500893 std::string msg = "No VPD found for " + inventoryPath + ": " +
894 e.what();
Matt Spinler85f61a62020-06-03 16:28:55 -0500895 addDebugData(msg);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500896
897 // Just create the callout with empty FRU fields
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500898 callout = std::make_unique<src::Callout>(
899 CalloutPriority::high, locCode, fn, ccin, sn, mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500900 }
Matt Spinlered046852020-03-13 13:58:15 -0500901 }
Patrick Williams45e83522022-07-22 19:26:52 -0500902 catch (const sdbusplus::exception_t& e)
Matt Spinlered046852020-03-13 13:58:15 -0500903 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500904 std::string msg = "Could not get location code for " + inventoryPath +
905 ": " + e.what();
906 addDebugData(msg);
Matt Spinlered046852020-03-13 13:58:15 -0500907
Matt Spinler479b6922021-08-17 16:34:59 -0500908 // Don't add a callout in this case, because:
909 // 1) With how the inventory is primed, there is no case where
910 // a location code is expected to be missing. This implies
911 // the caller is passing in something invalid.
912 // 2) The addDebugData call above will put the passed in path into
913 // a user data section that can be seen by development for debug.
914 // 3) Even if we wanted to do a 'no_vpd_for_fru' sort of maint.
915 // procedure, we don't have a good way to indicate to the user
916 // anything about the intended callout (they won't see user data).
917 // 4) Creating a new standalone event log for this problem isn't
918 // possible from inside a PEL section.
Matt Spinlered046852020-03-13 13:58:15 -0500919 }
920
Matt Spinler479b6922021-08-17 16:34:59 -0500921 if (callout)
922 {
923 createCalloutsObject();
924 _callouts->addCallout(std::move(callout));
925 }
Matt Spinler03984582020-04-09 13:17:58 -0500926}
Matt Spinlered046852020-03-13 13:58:15 -0500927
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500928std::vector<message::RegistryCallout>
929 SRC::getRegistryCallouts(const message::Entry& regEntry,
930 const AdditionalData& additionalData,
931 const DataInterfaceBase& dataIface)
932{
933 std::vector<message::RegistryCallout> registryCallouts;
934
935 if (regEntry.callouts)
936 {
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500937 std::vector<std::string> systemNames;
938
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500939 try
940 {
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500941 systemNames = dataIface.getSystemNames();
942 }
943 catch (const std::exception& e)
944 {
945 // Compatible interface not available yet
946 }
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500947
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500948 try
949 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500950 registryCallouts = message::Registry::getCallouts(
951 regEntry.callouts.value(), systemNames, additionalData);
952 }
953 catch (const std::exception& e)
954 {
955 addDebugData(fmt::format(
956 "Error parsing PEL message registry callout JSON: {}",
957 e.what()));
958 }
959 }
960
961 return registryCallouts;
962}
963
964void SRC::addRegistryCallouts(
965 const std::vector<message::RegistryCallout>& callouts,
966 const DataInterfaceBase& dataIface,
967 std::optional<std::string> trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -0500968{
969 try
970 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500971 for (const auto& callout : callouts)
Matt Spinler03984582020-04-09 13:17:58 -0500972 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500973 addRegistryCallout(callout, dataIface, trustedSymbolicFRUInvPath);
974
975 // Only the first callout gets the inventory path
976 if (trustedSymbolicFRUInvPath)
977 {
978 trustedSymbolicFRUInvPath = std::nullopt;
979 }
Matt Spinler03984582020-04-09 13:17:58 -0500980 }
981 }
Patrick Williams66491c62021-10-06 12:23:37 -0500982 catch (const std::exception& e)
Matt Spinler03984582020-04-09 13:17:58 -0500983 {
Patrick Williams2544b412022-10-04 08:41:06 -0500984 std::string msg = "Error parsing PEL message registry callout JSON: "s +
985 e.what();
Matt Spinler85f61a62020-06-03 16:28:55 -0500986 addDebugData(msg);
Matt Spinler03984582020-04-09 13:17:58 -0500987 }
988}
989
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500990void SRC::addRegistryCallout(
991 const message::RegistryCallout& regCallout,
992 const DataInterfaceBase& dataIface,
993 const std::optional<std::string>& trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -0500994{
995 std::unique_ptr<src::Callout> callout;
Matt Spinler03984582020-04-09 13:17:58 -0500996 auto locCode = regCallout.locCode;
997
Matt Spinleraf191c72020-06-04 11:35:13 -0500998 if (!locCode.empty())
999 {
1000 try
1001 {
1002 locCode = dataIface.expandLocationCode(locCode, 0);
1003 }
1004 catch (const std::exception& e)
1005 {
Patrick Williams2544b412022-10-04 08:41:06 -05001006 auto msg = "Unable to expand location code " + locCode + ": " +
1007 e.what();
Matt Spinleraf191c72020-06-04 11:35:13 -05001008 addDebugData(msg);
1009 return;
1010 }
1011 }
1012
Matt Spinler03984582020-04-09 13:17:58 -05001013 // Via the PEL values table, get the priority enum.
1014 // The schema will have validated the priority was a valid value.
Patrick Williams2544b412022-10-04 08:41:06 -05001015 auto priorityIt = pv::findByName(regCallout.priority,
1016 pv::calloutPriorityValues);
Matt Spinler03984582020-04-09 13:17:58 -05001017 assert(priorityIt != pv::calloutPriorityValues.end());
1018 auto priority =
1019 static_cast<CalloutPriority>(std::get<pv::fieldValuePos>(*priorityIt));
1020
1021 if (!regCallout.procedure.empty())
1022 {
1023 // Procedure callout
Patrick Williams2544b412022-10-04 08:41:06 -05001024 callout = std::make_unique<src::Callout>(priority,
1025 regCallout.procedure);
Matt Spinler03984582020-04-09 13:17:58 -05001026 }
1027 else if (!regCallout.symbolicFRU.empty())
1028 {
1029 // Symbolic FRU callout
1030 callout = std::make_unique<src::Callout>(
1031 priority, regCallout.symbolicFRU, locCode, false);
1032 }
1033 else if (!regCallout.symbolicFRUTrusted.empty())
1034 {
1035 // Symbolic FRU with trusted location code callout
1036
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001037 // Use the location code from the inventory path if there is one.
1038 if (trustedSymbolicFRUInvPath)
1039 {
1040 try
1041 {
1042 locCode = dataIface.getLocationCode(*trustedSymbolicFRUInvPath);
1043 }
1044 catch (const std::exception& e)
1045 {
1046 addDebugData(
1047 fmt::format("Could not get location code for {}: {}",
1048 *trustedSymbolicFRUInvPath, e.what()));
1049 locCode.clear();
1050 }
1051 }
1052
Matt Spinler03984582020-04-09 13:17:58 -05001053 // The registry wants it to be trusted, but that requires a valid
1054 // location code for it to actually be.
1055 callout = std::make_unique<src::Callout>(
1056 priority, regCallout.symbolicFRUTrusted, locCode, !locCode.empty());
1057 }
1058 else
1059 {
Matt Spinleraf191c72020-06-04 11:35:13 -05001060 // A hardware callout
Matt Spinlerbad056b2023-01-25 14:16:57 -06001061 std::vector<std::string> inventoryPaths;
Matt Spinleraf191c72020-06-04 11:35:13 -05001062
1063 try
1064 {
1065 // Get the inventory item from the unexpanded location code
Matt Spinlerbad056b2023-01-25 14:16:57 -06001066 inventoryPaths =
Matt Spinler2f9225a2020-08-05 12:58:49 -05001067 dataIface.getInventoryFromLocCode(regCallout.locCode, 0, false);
Matt Spinleraf191c72020-06-04 11:35:13 -05001068 }
1069 catch (const std::exception& e)
1070 {
1071 std::string msg =
1072 "Unable to get inventory path from location code: " + locCode +
1073 ": " + e.what();
1074 addDebugData(msg);
1075 return;
1076 }
1077
Matt Spinlerbad056b2023-01-25 14:16:57 -06001078 // Just use first path returned since they all point to the same FRU.
1079 addInventoryCallout(inventoryPaths[0], priority, locCode, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -05001080 }
1081
1082 if (callout)
1083 {
1084 createCalloutsObject();
1085 _callouts->addCallout(std::move(callout));
1086 }
1087}
Matt Spinlered046852020-03-13 13:58:15 -05001088
Matt Spinler717de422020-06-04 13:10:14 -05001089void SRC::addDevicePathCallouts(const AdditionalData& additionalData,
1090 const DataInterfaceBase& dataIface)
1091{
1092 std::vector<device_callouts::Callout> callouts;
1093 auto i2cBus = additionalData.getValue("CALLOUT_IIC_BUS");
1094 auto i2cAddr = additionalData.getValue("CALLOUT_IIC_ADDR");
1095 auto devPath = additionalData.getValue("CALLOUT_DEVICE_PATH");
1096
1097 // A device callout contains either:
1098 // * CALLOUT_ERRNO, CALLOUT_DEVICE_PATH
1099 // * CALLOUT_ERRNO, CALLOUT_IIC_BUS, CALLOUT_IIC_ADDR
1100 // We don't care about the errno.
1101
1102 if (devPath)
1103 {
1104 try
1105 {
1106 callouts = device_callouts::getCallouts(*devPath,
1107 dataIface.getSystemNames());
1108 }
1109 catch (const std::exception& e)
1110 {
1111 addDebugData(e.what());
1112 callouts.clear();
1113 }
1114 }
1115 else if (i2cBus && i2cAddr)
1116 {
1117 size_t bus;
1118 uint8_t address;
1119
1120 try
1121 {
1122 // If /dev/i2c- is prepended, remove it
1123 if (i2cBus->find("/dev/i2c-") != std::string::npos)
1124 {
1125 *i2cBus = i2cBus->substr(9);
1126 }
1127
1128 bus = stoul(*i2cBus, nullptr, 0);
1129 address = stoul(*i2cAddr, nullptr, 0);
1130 }
1131 catch (const std::exception& e)
1132 {
1133 std::string msg = "Invalid CALLOUT_IIC_BUS " + *i2cBus +
1134 " or CALLOUT_IIC_ADDR " + *i2cAddr +
1135 " in AdditionalData property";
1136 addDebugData(msg);
1137 return;
1138 }
1139
1140 try
1141 {
1142 callouts = device_callouts::getI2CCallouts(
1143 bus, address, dataIface.getSystemNames());
1144 }
1145 catch (const std::exception& e)
1146 {
1147 addDebugData(e.what());
1148 callouts.clear();
1149 }
1150 }
1151
1152 for (const auto& callout : callouts)
1153 {
1154 // The priority shouldn't be invalid, but check just in case.
1155 CalloutPriority priority = CalloutPriority::high;
1156
1157 if (!callout.priority.empty())
1158 {
1159 auto p = pel_values::findByValue(
1160 static_cast<uint32_t>(callout.priority[0]),
1161 pel_values::calloutPriorityValues);
1162
1163 if (p != pel_values::calloutPriorityValues.end())
1164 {
1165 priority = static_cast<CalloutPriority>(callout.priority[0]);
1166 }
1167 else
1168 {
1169 std::string msg =
1170 "Invalid priority found in dev callout JSON: " +
1171 callout.priority[0];
1172 addDebugData(msg);
1173 }
1174 }
1175
Matt Spinler0d92b522021-06-16 13:28:17 -06001176 std::optional<std::string> locCode;
1177
1178 try
1179 {
1180 locCode = dataIface.expandLocationCode(callout.locationCode, 0);
1181 }
1182 catch (const std::exception& e)
1183 {
1184 auto msg = fmt::format("Unable to expand location code {}: {}",
1185 callout.locationCode, e.what());
1186 addDebugData(msg);
1187 }
1188
Matt Spinler717de422020-06-04 13:10:14 -05001189 try
1190 {
Matt Spinlerbad056b2023-01-25 14:16:57 -06001191 auto inventoryPaths = dataIface.getInventoryFromLocCode(
Matt Spinler2f9225a2020-08-05 12:58:49 -05001192 callout.locationCode, 0, false);
Matt Spinler717de422020-06-04 13:10:14 -05001193
Matt Spinlerbad056b2023-01-25 14:16:57 -06001194 // Just use first path returned since they all
1195 // point to the same FRU.
1196 addInventoryCallout(inventoryPaths[0], priority, locCode,
1197 dataIface);
Matt Spinler717de422020-06-04 13:10:14 -05001198 }
1199 catch (const std::exception& e)
1200 {
1201 std::string msg =
1202 "Unable to get inventory path from location code: " +
1203 callout.locationCode + ": " + e.what();
1204 addDebugData(msg);
1205 }
1206
1207 // Until the code is there to convert these MRU value strings to
1208 // the official MRU values in the callout objects, just store
1209 // the MRU name in the debug UserData section.
1210 if (!callout.mru.empty())
1211 {
1212 std::string msg = "MRU: " + callout.mru;
1213 addDebugData(msg);
1214 }
1215
1216 // getCallouts() may have generated some debug data it stored
1217 // in a callout object. Save it as well.
1218 if (!callout.debug.empty())
1219 {
1220 addDebugData(callout.debug);
1221 }
1222 }
1223}
1224
Matt Spinler5a90a952020-08-27 09:39:03 -05001225void SRC::addJSONCallouts(const nlohmann::json& jsonCallouts,
1226 const DataInterfaceBase& dataIface)
1227{
1228 if (jsonCallouts.empty())
1229 {
1230 return;
1231 }
1232
1233 if (!jsonCallouts.is_array())
1234 {
1235 addDebugData("Callout JSON isn't an array");
1236 return;
1237 }
1238
1239 for (const auto& callout : jsonCallouts)
1240 {
1241 try
1242 {
1243 addJSONCallout(callout, dataIface);
1244 }
1245 catch (const std::exception& e)
1246 {
1247 addDebugData(fmt::format(
1248 "Failed extracting callout data from JSON: {}", e.what()));
1249 }
1250 }
1251}
1252
1253void SRC::addJSONCallout(const nlohmann::json& jsonCallout,
1254 const DataInterfaceBase& dataIface)
1255{
Matt Spinler3bdd0112020-08-27 10:24:34 -05001256 auto priority = getPriorityFromJSON(jsonCallout);
1257 std::string locCode;
1258 std::string unexpandedLocCode;
1259 std::unique_ptr<src::Callout> callout;
1260
1261 // Expand the location code if it's there
1262 if (jsonCallout.contains("LocationCode"))
1263 {
1264 unexpandedLocCode = jsonCallout.at("LocationCode").get<std::string>();
1265
1266 try
1267 {
1268 locCode = dataIface.expandLocationCode(unexpandedLocCode, 0);
1269 }
1270 catch (const std::exception& e)
1271 {
1272 addDebugData(fmt::format("Unable to expand location code {}: {}",
1273 unexpandedLocCode, e.what()));
1274 // Use the value from the JSON so at least there's something
1275 locCode = unexpandedLocCode;
1276 }
1277 }
1278
1279 // Create either a procedure, symbolic FRU, or normal FRU callout.
1280 if (jsonCallout.contains("Procedure"))
1281 {
1282 auto procedure = jsonCallout.at("Procedure").get<std::string>();
1283
Matt Spinler3c7ec6d2022-05-06 08:50:20 -05001284 // If it's the registry name instead of the raw name, convert.
1285 if (pv::maintenanceProcedures.find(procedure) !=
1286 pv::maintenanceProcedures.end())
1287 {
1288 procedure = pv::maintenanceProcedures.at(procedure);
1289 }
1290
Matt Spinler3bdd0112020-08-27 10:24:34 -05001291 callout = std::make_unique<src::Callout>(
1292 static_cast<CalloutPriority>(priority), procedure,
1293 src::CalloutValueType::raw);
1294 }
1295 else if (jsonCallout.contains("SymbolicFRU"))
1296 {
1297 auto fru = jsonCallout.at("SymbolicFRU").get<std::string>();
1298
Matt Spinler3c7ec6d2022-05-06 08:50:20 -05001299 // If it's the registry name instead of the raw name, convert.
1300 if (pv::symbolicFRUs.find(fru) != pv::symbolicFRUs.end())
1301 {
1302 fru = pv::symbolicFRUs.at(fru);
1303 }
1304
Matt Spinler3bdd0112020-08-27 10:24:34 -05001305 bool trusted = false;
1306 if (jsonCallout.contains("TrustedLocationCode") && !locCode.empty())
1307 {
1308 trusted = jsonCallout.at("TrustedLocationCode").get<bool>();
1309 }
1310
1311 callout = std::make_unique<src::Callout>(
1312 static_cast<CalloutPriority>(priority), fru,
1313 src::CalloutValueType::raw, locCode, trusted);
1314 }
1315 else
1316 {
1317 // A hardware FRU
1318 std::string inventoryPath;
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001319 std::vector<src::MRU::MRUCallout> mrus;
Matt Spinler3bdd0112020-08-27 10:24:34 -05001320
1321 if (jsonCallout.contains("InventoryPath"))
1322 {
1323 inventoryPath = jsonCallout.at("InventoryPath").get<std::string>();
1324 }
1325 else
1326 {
1327 if (unexpandedLocCode.empty())
1328 {
1329 throw std::runtime_error{"JSON callout needs either an "
1330 "inventory path or location code"};
1331 }
1332
1333 try
1334 {
Matt Spinlerbad056b2023-01-25 14:16:57 -06001335 auto inventoryPaths = dataIface.getInventoryFromLocCode(
Matt Spinler3bdd0112020-08-27 10:24:34 -05001336 unexpandedLocCode, 0, false);
Matt Spinlerbad056b2023-01-25 14:16:57 -06001337 // Just use first path returned since they all
1338 // point to the same FRU.
1339 inventoryPath = inventoryPaths[0];
Matt Spinler3bdd0112020-08-27 10:24:34 -05001340 }
1341 catch (const std::exception& e)
1342 {
1343 throw std::runtime_error{
1344 fmt::format("Unable to get inventory path from "
1345 "location code: {}: {}",
1346 unexpandedLocCode, e.what())};
1347 }
1348 }
1349
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001350 if (jsonCallout.contains("MRUs"))
1351 {
1352 mrus = getMRUsFromJSON(jsonCallout.at("MRUs"));
1353 }
1354
Matt Spinler3bdd0112020-08-27 10:24:34 -05001355 // If the location code was also passed in, use that here too
1356 // so addInventoryCallout doesn't have to look it up.
1357 std::optional<std::string> lc;
1358 if (!locCode.empty())
1359 {
1360 lc = locCode;
1361 }
1362
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001363 addInventoryCallout(inventoryPath, priority, lc, dataIface, mrus);
Matt Spinlerafa2c792020-08-27 11:01:39 -05001364
1365 if (jsonCallout.contains("Deconfigured"))
1366 {
1367 if (jsonCallout.at("Deconfigured").get<bool>())
1368 {
1369 setErrorStatusFlag(ErrorStatusFlags::deconfigured);
1370 }
1371 }
1372
1373 if (jsonCallout.contains("Guarded"))
1374 {
1375 if (jsonCallout.at("Guarded").get<bool>())
1376 {
1377 setErrorStatusFlag(ErrorStatusFlags::guarded);
1378 }
1379 }
Matt Spinler3bdd0112020-08-27 10:24:34 -05001380 }
1381
1382 if (callout)
1383 {
1384 createCalloutsObject();
1385 _callouts->addCallout(std::move(callout));
1386 }
1387}
1388
1389CalloutPriority SRC::getPriorityFromJSON(const nlohmann::json& json)
1390{
1391 // Looks like:
1392 // {
1393 // "Priority": "H"
1394 // }
1395 auto p = json.at("Priority").get<std::string>();
1396 if (p.empty())
1397 {
1398 throw std::runtime_error{"Priority field in callout is empty"};
1399 }
1400
1401 auto priority = static_cast<CalloutPriority>(p.front());
1402
1403 // Validate it
1404 auto priorityIt = pv::findByValue(static_cast<uint32_t>(priority),
1405 pv::calloutPriorityValues);
1406 if (priorityIt == pv::calloutPriorityValues.end())
1407 {
1408 throw std::runtime_error{
1409 fmt::format("Invalid priority '{}' found in JSON callout", p)};
1410 }
1411
1412 return priority;
Matt Spinler5a90a952020-08-27 09:39:03 -05001413}
1414
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001415std::vector<src::MRU::MRUCallout>
1416 SRC::getMRUsFromJSON(const nlohmann::json& mruJSON)
1417{
1418 std::vector<src::MRU::MRUCallout> mrus;
1419
1420 // Looks like:
1421 // [
1422 // {
1423 // "ID": 100,
1424 // "Priority": "H"
1425 // }
1426 // ]
1427 if (!mruJSON.is_array())
1428 {
1429 addDebugData("MRU callout JSON is not an array");
1430 return mrus;
1431 }
1432
1433 for (const auto& mruCallout : mruJSON)
1434 {
1435 try
1436 {
1437 auto priority = getPriorityFromJSON(mruCallout);
1438 auto id = mruCallout.at("ID").get<uint32_t>();
1439
1440 src::MRU::MRUCallout mru{static_cast<uint32_t>(priority), id};
1441 mrus.push_back(std::move(mru));
1442 }
1443 catch (const std::exception& e)
1444 {
1445 addDebugData(fmt::format("Invalid MRU entry in JSON: {}: {}",
1446 mruCallout.dump(), e.what()));
1447 }
1448 }
1449
1450 return mrus;
1451}
1452
Sumit Kumar9d43a722021-08-24 09:46:19 -05001453void SRC::setDumpStatus(const DataInterfaceBase& dataIface)
1454{
1455 std::vector<bool> dumpStatus{false, false, false};
1456
1457 try
1458 {
1459 std::vector<std::string> dumpType = {"bmc/entry", "resource/entry",
1460 "system/entry"};
1461 dumpStatus = dataIface.checkDumpStatus(dumpType);
1462
1463 // For bmc - set bit 0 of nibble [4-7] bits of byte-1 SP dump
1464 // For resource - set bit 2 of nibble [4-7] bits of byte-2 Hypervisor
1465 // For system - set bit 1 of nibble [4-7] bits of byte-2 HW dump
1466 _hexData[0] |= ((dumpStatus[0] << 19) | (dumpStatus[1] << 9) |
1467 (dumpStatus[2] << 10));
1468 }
1469 catch (const std::exception& e)
1470 {
Matt Spinler35a405b2022-03-02 11:42:42 -06001471 log<level::ERR>(
1472 fmt::format("Checking dump status failed: {}", e.what()).c_str());
Sumit Kumar9d43a722021-08-24 09:46:19 -05001473 }
1474}
1475
Sumit Kumar3e274432021-09-14 06:37:56 -05001476std::vector<uint8_t> SRC::getSrcStruct()
1477{
1478 std::vector<uint8_t> data;
1479 Stream stream{data};
1480
1481 //------ Ref section 4.3 in PEL doc---
1482 //------ SRC Structure 40 bytes-------
1483 // Byte-0 | Byte-1 | Byte-2 | Byte-3 |
1484 // -----------------------------------
1485 // 02 | 08 | 00 | 09 | ==> Header
1486 // 00 | 00 | 00 | 48 | ==> Header
1487 // 00 | 00 | 00 | 00 | ==> Hex data word-2
1488 // 00 | 00 | 00 | 00 | ==> Hex data word-3
1489 // 00 | 00 | 00 | 00 | ==> Hex data word-4
1490 // 20 | 00 | 00 | 00 | ==> Hex data word-5
1491 // 00 | 00 | 00 | 00 | ==> Hex data word-6
1492 // 00 | 00 | 00 | 00 | ==> Hex data word-7
1493 // 00 | 00 | 00 | 00 | ==> Hex data word-8
1494 // 00 | 00 | 00 | 00 | ==> Hex data word-9
1495 // -----------------------------------
1496 // ASCII string - 8 bytes |
1497 // -----------------------------------
1498 // ASCII space NULL - 24 bytes |
1499 // -----------------------------------
1500 //_size = Base SRC struct: 8 byte header + hex data section + ASCII string
1501
1502 uint8_t flags = (_flags | postOPPanel);
1503
1504 stream << _version << flags << _reserved1B << _wordCount << _reserved2B
1505 << _size;
1506
1507 for (auto& word : _hexData)
1508 {
1509 stream << word;
1510 }
1511
1512 _asciiString->flatten(stream);
1513
1514 return data;
1515}
1516
Vijay Lobo875b6c72021-10-20 17:38:56 -05001517void SRC::setProgressCode(const DataInterfaceBase& dataIface)
1518{
1519 std::vector<uint8_t> progressSRC;
1520
1521 try
1522 {
1523 progressSRC = dataIface.getRawProgressSRC();
1524 }
1525 catch (const std::exception& e)
1526 {
1527 log<level::ERR>(
1528 fmt::format("Error getting progress code: {}", e.what()).c_str());
1529 return;
1530 }
1531
1532 _hexData[2] = getProgressCode(progressSRC);
1533}
1534
1535uint32_t SRC::getProgressCode(std::vector<uint8_t>& rawProgressSRC)
1536{
1537 uint32_t progressCode = 0;
1538
1539 // A valid progress SRC is at least 72 bytes
1540 if (rawProgressSRC.size() < 72)
1541 {
1542 return progressCode;
1543 }
1544
1545 try
1546 {
1547 // The ASCII string field in progress SRCs starts at offset 40.
1548 // Take the first 8 characters to put in the uint32:
1549 // "CC009189" -> 0xCC009189
1550 Stream stream{rawProgressSRC, 40};
1551 src::AsciiString aString{stream};
1552 auto progressCodeString = aString.get().substr(0, 8);
1553
1554 if (std::all_of(progressCodeString.begin(), progressCodeString.end(),
1555 [](char c) {
1556 return std::isxdigit(static_cast<unsigned char>(c));
1557 }))
1558 {
1559 progressCode = std::stoul(progressCodeString, nullptr, 16);
1560 }
1561 }
1562 catch (const std::exception& e)
1563 {}
1564
1565 return progressCode;
1566}
1567
Matt Spinlerf9bae182019-10-09 13:37:38 -05001568} // namespace pels
1569} // namespace openpower