blob: 8058155e55f4f19886de547b7ba700c54caf884a [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 {
309 log<level::ERR>("Cannot unflatten SRC", entry("ERROR=%s", e.what()));
310 _valid = false;
311 }
312}
313
Matt Spinler075e5ba2020-02-21 15:46:00 -0600314SRC::SRC(const message::Entry& regEntry, const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500315 const nlohmann::json& jsonCallouts, const DataInterfaceBase& dataIface)
Matt Spinlerbd716f02019-10-15 10:54:11 -0500316{
317 _header.id = static_cast<uint16_t>(SectionID::primarySRC);
318 _header.version = srcSectionVersion;
319 _header.subType = srcSectionSubtype;
320 _header.componentID = regEntry.componentID;
321
322 _version = srcVersion;
323
324 _flags = 0;
Vijay Lobof3702bb2021-04-09 15:10:19 -0500325
Matt Spinlerbd716f02019-10-15 10:54:11 -0500326 _reserved1B = 0;
327
328 _wordCount = numSRCHexDataWords + 1;
329
330 _reserved2B = 0;
331
332 // There are multiple fields encoded in the hex data words.
333 std::for_each(_hexData.begin(), _hexData.end(),
334 [](auto& word) { word = 0; });
Matt Spinler7c619182020-07-27 15:15:11 -0500335
336 // Hex Word 2 Nibbles:
337 // MIGVEPFF
338 // M: Partition dump status = 0
339 // I: System boot state = TODO
340 // G: Partition Boot type = 0
Sumit Kumar9d43a722021-08-24 09:46:19 -0500341 // V: BMC dump status
Matt Spinler7c619182020-07-27 15:15:11 -0500342 // E: Platform boot mode = 0 (side = temporary, speed = fast)
Sumit Kumar9d43a722021-08-24 09:46:19 -0500343 // P: Platform dump status
Matt Spinler7c619182020-07-27 15:15:11 -0500344 // FF: SRC format, set below
345
Vijay Lobo875b6c72021-10-20 17:38:56 -0500346 setProgressCode(dataIface);
Sumit Kumar9d43a722021-08-24 09:46:19 -0500347 setDumpStatus(dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500348 setBMCFormat();
349 setBMCPosition();
Matt Spinler075e5ba2020-02-21 15:46:00 -0600350 setMotherboardCCIN(dataIface);
351
Matt Spinlerbd716f02019-10-15 10:54:11 -0500352 // Fill in the last 4 words from the AdditionalData property contents.
353 setUserDefinedHexWords(regEntry, additionalData);
354
355 _asciiString = std::make_unique<src::AsciiString>(regEntry);
356
Sumit Kumar50bfa692022-01-06 06:48:26 -0600357 // Check for additional data - PEL_SUBSYSTEM
358 auto ss = additionalData.getValue("PEL_SUBSYSTEM");
359 if (ss)
360 {
361 auto eventSubsystem = std::stoul(*ss, NULL, 16);
Patrick Williams2544b412022-10-04 08:41:06 -0500362 std::string subsystem = pv::getValue(eventSubsystem,
363 pel_values::subsystemValues);
Sumit Kumar50bfa692022-01-06 06:48:26 -0600364 if (subsystem == "invalid")
365 {
366 log<level::WARNING>(
367 fmt::format("SRC: Invalid SubSystem value:{:#X}",
368 eventSubsystem)
369 .c_str());
370 }
371 else
372 {
373 _asciiString->setByte(2, eventSubsystem);
374 }
375 }
376
Matt Spinler5a90a952020-08-27 09:39:03 -0500377 addCallouts(regEntry, additionalData, jsonCallouts, dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500378
379 _size = baseSRCSize;
380 _size += _callouts ? _callouts->flattenedSize() : 0;
381 _header.size = Section::flattenedSize() + _size;
382
383 _valid = true;
384}
385
386void SRC::setUserDefinedHexWords(const message::Entry& regEntry,
387 const AdditionalData& ad)
388{
389 if (!regEntry.src.hexwordADFields)
390 {
391 return;
392 }
393
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800394 // Save the AdditionalData value corresponding to the first element of
395 // adName tuple into _hexData[wordNum].
Matt Spinlerbd716f02019-10-15 10:54:11 -0500396 for (const auto& [wordNum, adName] : *regEntry.src.hexwordADFields)
397 {
398 // Can only set words 6 - 9
399 if (!isUserDefinedWord(wordNum))
400 {
Patrick Williams2544b412022-10-04 08:41:06 -0500401 std::string msg = "SRC user data word out of range: " +
402 std::to_string(wordNum);
Matt Spinler85f61a62020-06-03 16:28:55 -0500403 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500404 continue;
405 }
406
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800407 auto value = ad.getValue(std::get<0>(adName));
Matt Spinlerbd716f02019-10-15 10:54:11 -0500408 if (value)
409 {
410 _hexData[getWordIndexFromWordNum(wordNum)] =
411 std::strtoul(value.value().c_str(), nullptr, 0);
412 }
413 else
414 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800415 std::string msg = "Source for user data SRC word not found: " +
416 std::get<0>(adName);
Matt Spinler85f61a62020-06-03 16:28:55 -0500417 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500418 }
419 }
420}
421
Matt Spinler075e5ba2020-02-21 15:46:00 -0600422void SRC::setMotherboardCCIN(const DataInterfaceBase& dataIface)
423{
424 uint32_t ccin = 0;
425 auto ccinString = dataIface.getMotherboardCCIN();
426
427 try
428 {
429 if (ccinString.size() == ccinSize)
430 {
431 ccin = std::stoi(ccinString, 0, 16);
432 }
433 }
Patrick Williams66491c62021-10-06 12:23:37 -0500434 catch (const std::exception& e)
Matt Spinler075e5ba2020-02-21 15:46:00 -0600435 {
436 log<level::WARNING>("Could not convert motherboard CCIN to a number",
437 entry("CCIN=%s", ccinString.c_str()));
438 return;
439 }
440
441 // Set the first 2 bytes
442 _hexData[1] |= ccin << 16;
443}
444
Matt Spinlerf9bae182019-10-09 13:37:38 -0500445void SRC::validate()
446{
447 bool failed = false;
448
449 if ((header().id != static_cast<uint16_t>(SectionID::primarySRC)) &&
450 (header().id != static_cast<uint16_t>(SectionID::secondarySRC)))
451 {
452 log<level::ERR>("Invalid SRC section ID",
453 entry("ID=0x%X", header().id));
454 failed = true;
455 }
456
457 // Check the version in the SRC, not in the header
Matt Spinlerbd716f02019-10-15 10:54:11 -0500458 if (_version != srcVersion)
Matt Spinlerf9bae182019-10-09 13:37:38 -0500459 {
Matt Spinlerbd716f02019-10-15 10:54:11 -0500460 log<level::ERR>("Invalid SRC version", entry("VERSION=0x%X", _version));
Matt Spinlerf9bae182019-10-09 13:37:38 -0500461 failed = true;
462 }
463
464 _valid = failed ? false : true;
465}
466
Matt Spinler075e5ba2020-02-21 15:46:00 -0600467bool SRC::isBMCSRC() const
468{
469 auto as = asciiString();
470 if (as.length() >= 2)
471 {
472 uint8_t errorType = strtoul(as.substr(0, 2).c_str(), nullptr, 16);
473 return (errorType == static_cast<uint8_t>(SRCType::bmcError) ||
474 errorType == static_cast<uint8_t>(SRCType::powerError));
475 }
476 return false;
477}
478
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800479std::optional<std::string> SRC::getErrorDetails(message::Registry& registry,
480 DetailLevel type,
481 bool toCache) const
482{
483 const std::string jsonIndent(indentLevel, 0x20);
484 std::string errorOut;
Matt Spinler075e5ba2020-02-21 15:46:00 -0600485 if (isBMCSRC())
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800486 {
487 auto entry = registry.lookup("0x" + asciiString().substr(4, 4),
488 rg::LookupType::reasonCode, toCache);
489 if (entry)
490 {
491 errorOut.append(jsonIndent + "\"Error Details\": {\n");
492 auto errorMsg = getErrorMessage(*entry);
493 if (errorMsg)
494 {
495 if (type == DetailLevel::message)
496 {
497 return errorMsg.value();
498 }
499 else
500 {
501 jsonInsert(errorOut, "Message", errorMsg.value(), 2);
502 }
503 }
504 if (entry->src.hexwordADFields)
505 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800506 std::map<size_t, std::tuple<std::string, std::string>>
507 adFields = entry->src.hexwordADFields.value();
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800508 for (const auto& hexwordMap : adFields)
509 {
Zane Shelleye8db29b2021-11-13 10:34:07 -0600510 auto srcValue = getNumberString(
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800511 "0x%X",
Zane Shelleye8db29b2021-11-13 10:34:07 -0600512 _hexData[getWordIndexFromWordNum(hexwordMap.first)]);
513
514 auto srcKey = std::get<0>(hexwordMap.second);
515 auto srcDesc = std::get<1>(hexwordMap.second);
516
517 // Only include this hex word in the error details if the
518 // description exists.
519 if (!srcDesc.empty())
520 {
521 std::vector<std::string> valueDescr;
522 valueDescr.push_back(srcValue);
523 valueDescr.push_back(srcDesc);
524 jsonInsertArray(errorOut, srcKey, valueDescr, 2);
525 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800526 }
527 }
528 errorOut.erase(errorOut.size() - 2);
529 errorOut.append("\n");
530 errorOut.append(jsonIndent + "},\n");
531 return errorOut;
532 }
533 }
534 return std::nullopt;
535}
536
537std::optional<std::string>
538 SRC::getErrorMessage(const message::Entry& regEntry) const
539{
540 try
541 {
542 if (regEntry.doc.messageArgSources)
543 {
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800544 std::vector<uint32_t> argSourceVals;
545 std::string message;
546 const auto& argValues = regEntry.doc.messageArgSources.value();
547 for (size_t i = 0; i < argValues.size(); ++i)
548 {
549 argSourceVals.push_back(_hexData[getWordIndexFromWordNum(
550 argValues[i].back() - '0')]);
551 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500552
553 auto it = std::begin(regEntry.doc.message);
554 auto it_end = std::end(regEntry.doc.message);
555
556 while (it != it_end)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800557 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500558 if (*it == '%')
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800559 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500560 ++it;
561
562 size_t wordIndex = *it - '0';
563 if (isdigit(*it) && wordIndex >= 1 &&
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800564 static_cast<uint16_t>(wordIndex) <=
565 argSourceVals.size())
566 {
567 message.append(getNumberString(
Zane Shelley39936e32021-11-13 16:19:34 -0600568 "0x%08X", argSourceVals[wordIndex - 1]));
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800569 }
570 else
571 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500572 message.append("%" + std::string(1, *it));
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800573 }
574 }
575 else
576 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500577 message.push_back(*it);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800578 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500579 ++it;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800580 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500581
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800582 return message;
583 }
584 else
585 {
586 return regEntry.doc.message;
587 }
588 }
589 catch (const std::exception& e)
590 {
591 log<level::ERR>("Cannot get error message from registry entry",
592 entry("ERROR=%s", e.what()));
593 }
594 return std::nullopt;
595}
596
597std::optional<std::string> SRC::getCallouts() const
598{
599 if (!_callouts)
600 {
601 return std::nullopt;
602 }
603 std::string printOut;
604 const std::string jsonIndent(indentLevel, 0x20);
605 const auto& callout = _callouts->callouts();
606 const auto& compDescrp = pv::failingComponentType;
607 printOut.append(jsonIndent + "\"Callout Section\": {\n");
608 jsonInsert(printOut, "Callout Count", std::to_string(callout.size()), 2);
609 printOut.append(jsonIndent + jsonIndent + "\"Callouts\": [");
610 for (auto& entry : callout)
611 {
612 printOut.append("{\n");
613 if (entry->fruIdentity())
614 {
615 jsonInsert(
616 printOut, "FRU Type",
617 compDescrp.at(entry->fruIdentity()->failingComponentType()), 3);
618 jsonInsert(printOut, "Priority",
619 pv::getValue(entry->priority(),
620 pel_values::calloutPriorityValues),
621 3);
622 if (!entry->locationCode().empty())
623 {
624 jsonInsert(printOut, "Location Code", entry->locationCode(), 3);
625 }
626 if (entry->fruIdentity()->getPN().has_value())
627 {
628 jsonInsert(printOut, "Part Number",
629 entry->fruIdentity()->getPN().value(), 3);
630 }
631 if (entry->fruIdentity()->getMaintProc().has_value())
632 {
Matt Spinler9e8b49e2020-09-10 13:15:26 -0500633 jsonInsert(printOut, "Procedure",
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800634 entry->fruIdentity()->getMaintProc().value(), 3);
635 if (pv::procedureDesc.find(
636 entry->fruIdentity()->getMaintProc().value()) !=
637 pv::procedureDesc.end())
638 {
639 jsonInsert(
640 printOut, "Description",
641 pv::procedureDesc.at(
642 entry->fruIdentity()->getMaintProc().value()),
643 3);
644 }
645 }
646 if (entry->fruIdentity()->getCCIN().has_value())
647 {
648 jsonInsert(printOut, "CCIN",
649 entry->fruIdentity()->getCCIN().value(), 3);
650 }
651 if (entry->fruIdentity()->getSN().has_value())
652 {
653 jsonInsert(printOut, "Serial Number",
654 entry->fruIdentity()->getSN().value(), 3);
655 }
656 }
657 if (entry->pceIdentity())
658 {
659 const auto& pceIdentMtms = entry->pceIdentity()->mtms();
660 if (!pceIdentMtms.machineTypeAndModel().empty())
661 {
662 jsonInsert(printOut, "PCE MTMS",
663 pceIdentMtms.machineTypeAndModel() + "_" +
664 pceIdentMtms.machineSerialNumber(),
665 3);
666 }
667 if (!entry->pceIdentity()->enclosureName().empty())
668 {
669 jsonInsert(printOut, "PCE Name",
670 entry->pceIdentity()->enclosureName(), 3);
671 }
672 }
673 if (entry->mru())
674 {
675 const auto& mruCallouts = entry->mru()->mrus();
676 std::string mruId;
677 for (auto& element : mruCallouts)
678 {
679 if (!mruId.empty())
680 {
681 mruId.append(", " + getNumberString("%08X", element.id));
682 }
683 else
684 {
685 mruId.append(getNumberString("%08X", element.id));
686 }
687 }
688 jsonInsert(printOut, "MRU Id", mruId, 3);
689 }
690 printOut.erase(printOut.size() - 2);
691 printOut.append("\n" + jsonIndent + jsonIndent + "}, ");
692 };
693 printOut.erase(printOut.size() - 2);
694 printOut.append("]\n" + jsonIndent + "}");
695 return printOut;
696}
697
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800698std::optional<std::string> SRC::getJSON(message::Registry& registry,
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500699 const std::vector<std::string>& plugins
700 [[maybe_unused]],
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800701 uint8_t creatorID) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800702{
703 std::string ps;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800704 std::vector<std::string> hexwords;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800705 jsonInsert(ps, pv::sectionVer, getNumberString("%d", _header.version), 1);
706 jsonInsert(ps, pv::subSection, getNumberString("%d", _header.subType), 1);
707 jsonInsert(ps, pv::createdBy, getNumberString("0x%X", _header.componentID),
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800708 1);
709 jsonInsert(ps, "SRC Version", getNumberString("0x%02X", _version), 1);
Harisuddin Mohamed Isac32e5512020-02-06 18:05:21 +0800710 jsonInsert(ps, "SRC Format", getNumberString("0x%02X", _hexData[0] & 0xFF),
711 1);
712 jsonInsert(ps, "Virtual Progress SRC",
713 pv::boolString.at(_flags & virtualProgressSRC), 1);
714 jsonInsert(ps, "I5/OS Service Event Bit",
715 pv::boolString.at(_flags & i5OSServiceEventBit), 1);
716 jsonInsert(ps, "Hypervisor Dump Initiated",
717 pv::boolString.at(_flags & hypDumpInit), 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600718
719 if (isBMCSRC())
720 {
721 std::string ccinString;
722 uint32_t ccin = _hexData[1] >> 16;
723
724 if (ccin)
725 {
726 ccinString = getNumberString("%04X", ccin);
727 }
728 // The PEL spec calls it a backplane, so call it that here.
729 jsonInsert(ps, "Backplane CCIN", ccinString, 1);
Matt Spinlerafa2c792020-08-27 11:01:39 -0500730
Sumit Kumar3e274432021-09-14 06:37:56 -0500731 jsonInsert(ps, "Terminate FW Error",
732 pv::boolString.at(
733 _hexData[3] &
734 static_cast<uint32_t>(ErrorStatusFlags::terminateFwErr)),
735 1);
Matt Spinlerafa2c792020-08-27 11:01:39 -0500736 jsonInsert(ps, "Deconfigured",
737 pv::boolString.at(
738 _hexData[3] &
739 static_cast<uint32_t>(ErrorStatusFlags::deconfigured)),
740 1);
741
742 jsonInsert(
743 ps, "Guarded",
744 pv::boolString.at(_hexData[3] &
745 static_cast<uint32_t>(ErrorStatusFlags::guarded)),
746 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600747 }
748
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800749 auto errorDetails = getErrorDetails(registry, DetailLevel::json, true);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800750 if (errorDetails)
751 {
752 ps.append(errorDetails.value());
753 }
754 jsonInsert(ps, "Valid Word Count", getNumberString("0x%02X", _wordCount),
755 1);
756 std::string refcode = asciiString();
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800757 hexwords.push_back(refcode);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800758 std::string extRefcode;
759 size_t pos = refcode.find(0x20);
760 if (pos != std::string::npos)
761 {
762 size_t nextPos = refcode.find_first_not_of(0x20, pos);
763 if (nextPos != std::string::npos)
764 {
765 extRefcode = trimEnd(refcode.substr(nextPos));
766 }
767 refcode.erase(pos);
768 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800769 jsonInsert(ps, "Reference Code", refcode, 1);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800770 if (!extRefcode.empty())
771 {
772 jsonInsert(ps, "Extended Reference Code", extRefcode, 1);
773 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800774 for (size_t i = 2; i <= _wordCount; i++)
775 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800776 std::string tmpWord =
777 getNumberString("%08X", _hexData[getWordIndexFromWordNum(i)]);
778 jsonInsert(ps, "Hex Word " + std::to_string(i), tmpWord, 1);
779 hexwords.push_back(tmpWord);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800780 }
781 auto calloutJson = getCallouts();
782 if (calloutJson)
783 {
784 ps.append(calloutJson.value());
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800785 ps.append(",\n");
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800786 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800787 std::string subsystem = getNumberString("%c", tolower(creatorID));
788 bool srcDetailExists = false;
789#ifdef PELTOOL
790 if (std::find(plugins.begin(), plugins.end(), subsystem + "src") !=
791 plugins.end())
792 {
793 auto pyJson = getPythonJSON(hexwords, creatorID);
794 if (pyJson)
795 {
796 ps.append(pyJson.value());
797 srcDetailExists = true;
798 }
799 }
800#endif
801 if (!srcDetailExists)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800802 {
803 ps.erase(ps.size() - 2);
804 }
805 return ps;
806}
807
Matt Spinler03984582020-04-09 13:17:58 -0500808void SRC::addCallouts(const message::Entry& regEntry,
809 const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500810 const nlohmann::json& jsonCallouts,
Matt Spinlered046852020-03-13 13:58:15 -0500811 const DataInterfaceBase& dataIface)
812{
Patrick Williams2544b412022-10-04 08:41:06 -0500813 auto registryCallouts = getRegistryCallouts(regEntry, additionalData,
814 dataIface);
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500815
Matt Spinlered046852020-03-13 13:58:15 -0500816 auto item = additionalData.getValue("CALLOUT_INVENTORY_PATH");
Miguel Gomez53ef1552020-10-14 21:16:32 +0000817 auto priority = additionalData.getValue("CALLOUT_PRIORITY");
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500818
Miguel Gomez53ef1552020-10-14 21:16:32 +0000819 std::optional<CalloutPriority> calloutPriority;
820
821 // Only H, M or L priority values.
822 if (priority && !(*priority).empty())
823 {
824 uint8_t p = (*priority)[0];
825 if (p == 'H' || p == 'M' || p == 'L')
826 {
827 calloutPriority = static_cast<CalloutPriority>(p);
828 }
829 }
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500830 // If the first registry callout says to use the passed in inventory
831 // path to get the location code for a symbolic FRU callout with a
832 // trusted location code, then do not add the inventory path as a
833 // normal FRU callout.
834 bool useInvForSymbolicFRULocCode =
835 !registryCallouts.empty() && registryCallouts[0].useInventoryLocCode &&
836 !registryCallouts[0].symbolicFRUTrusted.empty();
837
838 if (item && !useInvForSymbolicFRULocCode)
Matt Spinlered046852020-03-13 13:58:15 -0500839 {
Miguel Gomez53ef1552020-10-14 21:16:32 +0000840 addInventoryCallout(*item, calloutPriority, std::nullopt, dataIface);
Matt Spinlered046852020-03-13 13:58:15 -0500841 }
842
Matt Spinler717de422020-06-04 13:10:14 -0500843 addDevicePathCallouts(additionalData, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -0500844
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500845 addRegistryCallouts(registryCallouts, dataIface,
846 (useInvForSymbolicFRULocCode) ? item : std::nullopt);
Matt Spinler5a90a952020-08-27 09:39:03 -0500847
848 if (!jsonCallouts.empty())
849 {
850 addJSONCallouts(jsonCallouts, dataIface);
851 }
Matt Spinlered046852020-03-13 13:58:15 -0500852}
853
854void SRC::addInventoryCallout(const std::string& inventoryPath,
Matt Spinleraf191c72020-06-04 11:35:13 -0500855 const std::optional<CalloutPriority>& priority,
856 const std::optional<std::string>& locationCode,
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500857 const DataInterfaceBase& dataIface,
858 const std::vector<src::MRU::MRUCallout>& mrus)
Matt Spinlered046852020-03-13 13:58:15 -0500859{
860 std::string locCode;
861 std::string fn;
862 std::string ccin;
863 std::string sn;
864 std::unique_ptr<src::Callout> callout;
865
Matt Spinlered046852020-03-13 13:58:15 -0500866 try
867 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500868 // Use the passed in location code if there otherwise look it up
869 if (locationCode)
870 {
871 locCode = *locationCode;
872 }
873 else
874 {
875 locCode = dataIface.getLocationCode(inventoryPath);
876 }
Matt Spinlered046852020-03-13 13:58:15 -0500877
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500878 try
879 {
880 dataIface.getHWCalloutFields(inventoryPath, fn, ccin, sn);
881
Patrick Williams2544b412022-10-04 08:41:06 -0500882 CalloutPriority p = priority ? priority.value()
883 : CalloutPriority::high;
Matt Spinleraf191c72020-06-04 11:35:13 -0500884
Patrick Williams2544b412022-10-04 08:41:06 -0500885 callout = std::make_unique<src::Callout>(p, locCode, fn, ccin, sn,
886 mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500887 }
Patrick Williams45e83522022-07-22 19:26:52 -0500888 catch (const sdbusplus::exception_t& e)
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500889 {
Patrick Williams2544b412022-10-04 08:41:06 -0500890 std::string msg = "No VPD found for " + inventoryPath + ": " +
891 e.what();
Matt Spinler85f61a62020-06-03 16:28:55 -0500892 addDebugData(msg);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500893
894 // Just create the callout with empty FRU fields
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500895 callout = std::make_unique<src::Callout>(
896 CalloutPriority::high, locCode, fn, ccin, sn, mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500897 }
Matt Spinlered046852020-03-13 13:58:15 -0500898 }
Patrick Williams45e83522022-07-22 19:26:52 -0500899 catch (const sdbusplus::exception_t& e)
Matt Spinlered046852020-03-13 13:58:15 -0500900 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500901 std::string msg = "Could not get location code for " + inventoryPath +
902 ": " + e.what();
903 addDebugData(msg);
Matt Spinlered046852020-03-13 13:58:15 -0500904
Matt Spinler479b6922021-08-17 16:34:59 -0500905 // Don't add a callout in this case, because:
906 // 1) With how the inventory is primed, there is no case where
907 // a location code is expected to be missing. This implies
908 // the caller is passing in something invalid.
909 // 2) The addDebugData call above will put the passed in path into
910 // a user data section that can be seen by development for debug.
911 // 3) Even if we wanted to do a 'no_vpd_for_fru' sort of maint.
912 // procedure, we don't have a good way to indicate to the user
913 // anything about the intended callout (they won't see user data).
914 // 4) Creating a new standalone event log for this problem isn't
915 // possible from inside a PEL section.
Matt Spinlered046852020-03-13 13:58:15 -0500916 }
917
Matt Spinler479b6922021-08-17 16:34:59 -0500918 if (callout)
919 {
920 createCalloutsObject();
921 _callouts->addCallout(std::move(callout));
922 }
Matt Spinler03984582020-04-09 13:17:58 -0500923}
Matt Spinlered046852020-03-13 13:58:15 -0500924
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500925std::vector<message::RegistryCallout>
926 SRC::getRegistryCallouts(const message::Entry& regEntry,
927 const AdditionalData& additionalData,
928 const DataInterfaceBase& dataIface)
929{
930 std::vector<message::RegistryCallout> registryCallouts;
931
932 if (regEntry.callouts)
933 {
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500934 std::vector<std::string> systemNames;
935
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500936 try
937 {
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500938 systemNames = dataIface.getSystemNames();
939 }
940 catch (const std::exception& e)
941 {
942 // Compatible interface not available yet
943 }
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500944
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500945 try
946 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500947 registryCallouts = message::Registry::getCallouts(
948 regEntry.callouts.value(), systemNames, additionalData);
949 }
950 catch (const std::exception& e)
951 {
952 addDebugData(fmt::format(
953 "Error parsing PEL message registry callout JSON: {}",
954 e.what()));
955 }
956 }
957
958 return registryCallouts;
959}
960
961void SRC::addRegistryCallouts(
962 const std::vector<message::RegistryCallout>& callouts,
963 const DataInterfaceBase& dataIface,
964 std::optional<std::string> trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -0500965{
966 try
967 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500968 for (const auto& callout : callouts)
Matt Spinler03984582020-04-09 13:17:58 -0500969 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500970 addRegistryCallout(callout, dataIface, trustedSymbolicFRUInvPath);
971
972 // Only the first callout gets the inventory path
973 if (trustedSymbolicFRUInvPath)
974 {
975 trustedSymbolicFRUInvPath = std::nullopt;
976 }
Matt Spinler03984582020-04-09 13:17:58 -0500977 }
978 }
Patrick Williams66491c62021-10-06 12:23:37 -0500979 catch (const std::exception& e)
Matt Spinler03984582020-04-09 13:17:58 -0500980 {
Patrick Williams2544b412022-10-04 08:41:06 -0500981 std::string msg = "Error parsing PEL message registry callout JSON: "s +
982 e.what();
Matt Spinler85f61a62020-06-03 16:28:55 -0500983 addDebugData(msg);
Matt Spinler03984582020-04-09 13:17:58 -0500984 }
985}
986
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500987void SRC::addRegistryCallout(
988 const message::RegistryCallout& regCallout,
989 const DataInterfaceBase& dataIface,
990 const std::optional<std::string>& trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -0500991{
992 std::unique_ptr<src::Callout> callout;
Matt Spinler03984582020-04-09 13:17:58 -0500993 auto locCode = regCallout.locCode;
994
Matt Spinleraf191c72020-06-04 11:35:13 -0500995 if (!locCode.empty())
996 {
997 try
998 {
999 locCode = dataIface.expandLocationCode(locCode, 0);
1000 }
1001 catch (const std::exception& e)
1002 {
Patrick Williams2544b412022-10-04 08:41:06 -05001003 auto msg = "Unable to expand location code " + locCode + ": " +
1004 e.what();
Matt Spinleraf191c72020-06-04 11:35:13 -05001005 addDebugData(msg);
1006 return;
1007 }
1008 }
1009
Matt Spinler03984582020-04-09 13:17:58 -05001010 // Via the PEL values table, get the priority enum.
1011 // The schema will have validated the priority was a valid value.
Patrick Williams2544b412022-10-04 08:41:06 -05001012 auto priorityIt = pv::findByName(regCallout.priority,
1013 pv::calloutPriorityValues);
Matt Spinler03984582020-04-09 13:17:58 -05001014 assert(priorityIt != pv::calloutPriorityValues.end());
1015 auto priority =
1016 static_cast<CalloutPriority>(std::get<pv::fieldValuePos>(*priorityIt));
1017
1018 if (!regCallout.procedure.empty())
1019 {
1020 // Procedure callout
Patrick Williams2544b412022-10-04 08:41:06 -05001021 callout = std::make_unique<src::Callout>(priority,
1022 regCallout.procedure);
Matt Spinler03984582020-04-09 13:17:58 -05001023 }
1024 else if (!regCallout.symbolicFRU.empty())
1025 {
1026 // Symbolic FRU callout
1027 callout = std::make_unique<src::Callout>(
1028 priority, regCallout.symbolicFRU, locCode, false);
1029 }
1030 else if (!regCallout.symbolicFRUTrusted.empty())
1031 {
1032 // Symbolic FRU with trusted location code callout
1033
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001034 // Use the location code from the inventory path if there is one.
1035 if (trustedSymbolicFRUInvPath)
1036 {
1037 try
1038 {
1039 locCode = dataIface.getLocationCode(*trustedSymbolicFRUInvPath);
1040 }
1041 catch (const std::exception& e)
1042 {
1043 addDebugData(
1044 fmt::format("Could not get location code for {}: {}",
1045 *trustedSymbolicFRUInvPath, e.what()));
1046 locCode.clear();
1047 }
1048 }
1049
Matt Spinler03984582020-04-09 13:17:58 -05001050 // The registry wants it to be trusted, but that requires a valid
1051 // location code for it to actually be.
1052 callout = std::make_unique<src::Callout>(
1053 priority, regCallout.symbolicFRUTrusted, locCode, !locCode.empty());
1054 }
1055 else
1056 {
Matt Spinleraf191c72020-06-04 11:35:13 -05001057 // A hardware callout
1058 std::string inventoryPath;
1059
1060 try
1061 {
1062 // Get the inventory item from the unexpanded location code
1063 inventoryPath =
Matt Spinler2f9225a2020-08-05 12:58:49 -05001064 dataIface.getInventoryFromLocCode(regCallout.locCode, 0, false);
Matt Spinleraf191c72020-06-04 11:35:13 -05001065 }
1066 catch (const std::exception& e)
1067 {
1068 std::string msg =
1069 "Unable to get inventory path from location code: " + locCode +
1070 ": " + e.what();
1071 addDebugData(msg);
1072 return;
1073 }
1074
1075 addInventoryCallout(inventoryPath, priority, locCode, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -05001076 }
1077
1078 if (callout)
1079 {
1080 createCalloutsObject();
1081 _callouts->addCallout(std::move(callout));
1082 }
1083}
Matt Spinlered046852020-03-13 13:58:15 -05001084
Matt Spinler717de422020-06-04 13:10:14 -05001085void SRC::addDevicePathCallouts(const AdditionalData& additionalData,
1086 const DataInterfaceBase& dataIface)
1087{
1088 std::vector<device_callouts::Callout> callouts;
1089 auto i2cBus = additionalData.getValue("CALLOUT_IIC_BUS");
1090 auto i2cAddr = additionalData.getValue("CALLOUT_IIC_ADDR");
1091 auto devPath = additionalData.getValue("CALLOUT_DEVICE_PATH");
1092
1093 // A device callout contains either:
1094 // * CALLOUT_ERRNO, CALLOUT_DEVICE_PATH
1095 // * CALLOUT_ERRNO, CALLOUT_IIC_BUS, CALLOUT_IIC_ADDR
1096 // We don't care about the errno.
1097
1098 if (devPath)
1099 {
1100 try
1101 {
1102 callouts = device_callouts::getCallouts(*devPath,
1103 dataIface.getSystemNames());
1104 }
1105 catch (const std::exception& e)
1106 {
1107 addDebugData(e.what());
1108 callouts.clear();
1109 }
1110 }
1111 else if (i2cBus && i2cAddr)
1112 {
1113 size_t bus;
1114 uint8_t address;
1115
1116 try
1117 {
1118 // If /dev/i2c- is prepended, remove it
1119 if (i2cBus->find("/dev/i2c-") != std::string::npos)
1120 {
1121 *i2cBus = i2cBus->substr(9);
1122 }
1123
1124 bus = stoul(*i2cBus, nullptr, 0);
1125 address = stoul(*i2cAddr, nullptr, 0);
1126 }
1127 catch (const std::exception& e)
1128 {
1129 std::string msg = "Invalid CALLOUT_IIC_BUS " + *i2cBus +
1130 " or CALLOUT_IIC_ADDR " + *i2cAddr +
1131 " in AdditionalData property";
1132 addDebugData(msg);
1133 return;
1134 }
1135
1136 try
1137 {
1138 callouts = device_callouts::getI2CCallouts(
1139 bus, address, dataIface.getSystemNames());
1140 }
1141 catch (const std::exception& e)
1142 {
1143 addDebugData(e.what());
1144 callouts.clear();
1145 }
1146 }
1147
1148 for (const auto& callout : callouts)
1149 {
1150 // The priority shouldn't be invalid, but check just in case.
1151 CalloutPriority priority = CalloutPriority::high;
1152
1153 if (!callout.priority.empty())
1154 {
1155 auto p = pel_values::findByValue(
1156 static_cast<uint32_t>(callout.priority[0]),
1157 pel_values::calloutPriorityValues);
1158
1159 if (p != pel_values::calloutPriorityValues.end())
1160 {
1161 priority = static_cast<CalloutPriority>(callout.priority[0]);
1162 }
1163 else
1164 {
1165 std::string msg =
1166 "Invalid priority found in dev callout JSON: " +
1167 callout.priority[0];
1168 addDebugData(msg);
1169 }
1170 }
1171
Matt Spinler0d92b522021-06-16 13:28:17 -06001172 std::optional<std::string> locCode;
1173
1174 try
1175 {
1176 locCode = dataIface.expandLocationCode(callout.locationCode, 0);
1177 }
1178 catch (const std::exception& e)
1179 {
1180 auto msg = fmt::format("Unable to expand location code {}: {}",
1181 callout.locationCode, e.what());
1182 addDebugData(msg);
1183 }
1184
Matt Spinler717de422020-06-04 13:10:14 -05001185 try
1186 {
Matt Spinler2f9225a2020-08-05 12:58:49 -05001187 auto inventoryPath = dataIface.getInventoryFromLocCode(
1188 callout.locationCode, 0, false);
Matt Spinler717de422020-06-04 13:10:14 -05001189
Matt Spinler0d92b522021-06-16 13:28:17 -06001190 addInventoryCallout(inventoryPath, priority, locCode, dataIface);
Matt Spinler717de422020-06-04 13:10:14 -05001191 }
1192 catch (const std::exception& e)
1193 {
1194 std::string msg =
1195 "Unable to get inventory path from location code: " +
1196 callout.locationCode + ": " + e.what();
1197 addDebugData(msg);
1198 }
1199
1200 // Until the code is there to convert these MRU value strings to
1201 // the official MRU values in the callout objects, just store
1202 // the MRU name in the debug UserData section.
1203 if (!callout.mru.empty())
1204 {
1205 std::string msg = "MRU: " + callout.mru;
1206 addDebugData(msg);
1207 }
1208
1209 // getCallouts() may have generated some debug data it stored
1210 // in a callout object. Save it as well.
1211 if (!callout.debug.empty())
1212 {
1213 addDebugData(callout.debug);
1214 }
1215 }
1216}
1217
Matt Spinler5a90a952020-08-27 09:39:03 -05001218void SRC::addJSONCallouts(const nlohmann::json& jsonCallouts,
1219 const DataInterfaceBase& dataIface)
1220{
1221 if (jsonCallouts.empty())
1222 {
1223 return;
1224 }
1225
1226 if (!jsonCallouts.is_array())
1227 {
1228 addDebugData("Callout JSON isn't an array");
1229 return;
1230 }
1231
1232 for (const auto& callout : jsonCallouts)
1233 {
1234 try
1235 {
1236 addJSONCallout(callout, dataIface);
1237 }
1238 catch (const std::exception& e)
1239 {
1240 addDebugData(fmt::format(
1241 "Failed extracting callout data from JSON: {}", e.what()));
1242 }
1243 }
1244}
1245
1246void SRC::addJSONCallout(const nlohmann::json& jsonCallout,
1247 const DataInterfaceBase& dataIface)
1248{
Matt Spinler3bdd0112020-08-27 10:24:34 -05001249 auto priority = getPriorityFromJSON(jsonCallout);
1250 std::string locCode;
1251 std::string unexpandedLocCode;
1252 std::unique_ptr<src::Callout> callout;
1253
1254 // Expand the location code if it's there
1255 if (jsonCallout.contains("LocationCode"))
1256 {
1257 unexpandedLocCode = jsonCallout.at("LocationCode").get<std::string>();
1258
1259 try
1260 {
1261 locCode = dataIface.expandLocationCode(unexpandedLocCode, 0);
1262 }
1263 catch (const std::exception& e)
1264 {
1265 addDebugData(fmt::format("Unable to expand location code {}: {}",
1266 unexpandedLocCode, e.what()));
1267 // Use the value from the JSON so at least there's something
1268 locCode = unexpandedLocCode;
1269 }
1270 }
1271
1272 // Create either a procedure, symbolic FRU, or normal FRU callout.
1273 if (jsonCallout.contains("Procedure"))
1274 {
1275 auto procedure = jsonCallout.at("Procedure").get<std::string>();
1276
Matt Spinler3c7ec6d2022-05-06 08:50:20 -05001277 // If it's the registry name instead of the raw name, convert.
1278 if (pv::maintenanceProcedures.find(procedure) !=
1279 pv::maintenanceProcedures.end())
1280 {
1281 procedure = pv::maintenanceProcedures.at(procedure);
1282 }
1283
Matt Spinler3bdd0112020-08-27 10:24:34 -05001284 callout = std::make_unique<src::Callout>(
1285 static_cast<CalloutPriority>(priority), procedure,
1286 src::CalloutValueType::raw);
1287 }
1288 else if (jsonCallout.contains("SymbolicFRU"))
1289 {
1290 auto fru = jsonCallout.at("SymbolicFRU").get<std::string>();
1291
Matt Spinler3c7ec6d2022-05-06 08:50:20 -05001292 // If it's the registry name instead of the raw name, convert.
1293 if (pv::symbolicFRUs.find(fru) != pv::symbolicFRUs.end())
1294 {
1295 fru = pv::symbolicFRUs.at(fru);
1296 }
1297
Matt Spinler3bdd0112020-08-27 10:24:34 -05001298 bool trusted = false;
1299 if (jsonCallout.contains("TrustedLocationCode") && !locCode.empty())
1300 {
1301 trusted = jsonCallout.at("TrustedLocationCode").get<bool>();
1302 }
1303
1304 callout = std::make_unique<src::Callout>(
1305 static_cast<CalloutPriority>(priority), fru,
1306 src::CalloutValueType::raw, locCode, trusted);
1307 }
1308 else
1309 {
1310 // A hardware FRU
1311 std::string inventoryPath;
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001312 std::vector<src::MRU::MRUCallout> mrus;
Matt Spinler3bdd0112020-08-27 10:24:34 -05001313
1314 if (jsonCallout.contains("InventoryPath"))
1315 {
1316 inventoryPath = jsonCallout.at("InventoryPath").get<std::string>();
1317 }
1318 else
1319 {
1320 if (unexpandedLocCode.empty())
1321 {
1322 throw std::runtime_error{"JSON callout needs either an "
1323 "inventory path or location code"};
1324 }
1325
1326 try
1327 {
1328 inventoryPath = dataIface.getInventoryFromLocCode(
1329 unexpandedLocCode, 0, false);
1330 }
1331 catch (const std::exception& e)
1332 {
1333 throw std::runtime_error{
1334 fmt::format("Unable to get inventory path from "
1335 "location code: {}: {}",
1336 unexpandedLocCode, e.what())};
1337 }
1338 }
1339
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001340 if (jsonCallout.contains("MRUs"))
1341 {
1342 mrus = getMRUsFromJSON(jsonCallout.at("MRUs"));
1343 }
1344
Matt Spinler3bdd0112020-08-27 10:24:34 -05001345 // If the location code was also passed in, use that here too
1346 // so addInventoryCallout doesn't have to look it up.
1347 std::optional<std::string> lc;
1348 if (!locCode.empty())
1349 {
1350 lc = locCode;
1351 }
1352
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001353 addInventoryCallout(inventoryPath, priority, lc, dataIface, mrus);
Matt Spinlerafa2c792020-08-27 11:01:39 -05001354
1355 if (jsonCallout.contains("Deconfigured"))
1356 {
1357 if (jsonCallout.at("Deconfigured").get<bool>())
1358 {
1359 setErrorStatusFlag(ErrorStatusFlags::deconfigured);
1360 }
1361 }
1362
1363 if (jsonCallout.contains("Guarded"))
1364 {
1365 if (jsonCallout.at("Guarded").get<bool>())
1366 {
1367 setErrorStatusFlag(ErrorStatusFlags::guarded);
1368 }
1369 }
Matt Spinler3bdd0112020-08-27 10:24:34 -05001370 }
1371
1372 if (callout)
1373 {
1374 createCalloutsObject();
1375 _callouts->addCallout(std::move(callout));
1376 }
1377}
1378
1379CalloutPriority SRC::getPriorityFromJSON(const nlohmann::json& json)
1380{
1381 // Looks like:
1382 // {
1383 // "Priority": "H"
1384 // }
1385 auto p = json.at("Priority").get<std::string>();
1386 if (p.empty())
1387 {
1388 throw std::runtime_error{"Priority field in callout is empty"};
1389 }
1390
1391 auto priority = static_cast<CalloutPriority>(p.front());
1392
1393 // Validate it
1394 auto priorityIt = pv::findByValue(static_cast<uint32_t>(priority),
1395 pv::calloutPriorityValues);
1396 if (priorityIt == pv::calloutPriorityValues.end())
1397 {
1398 throw std::runtime_error{
1399 fmt::format("Invalid priority '{}' found in JSON callout", p)};
1400 }
1401
1402 return priority;
Matt Spinler5a90a952020-08-27 09:39:03 -05001403}
1404
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001405std::vector<src::MRU::MRUCallout>
1406 SRC::getMRUsFromJSON(const nlohmann::json& mruJSON)
1407{
1408 std::vector<src::MRU::MRUCallout> mrus;
1409
1410 // Looks like:
1411 // [
1412 // {
1413 // "ID": 100,
1414 // "Priority": "H"
1415 // }
1416 // ]
1417 if (!mruJSON.is_array())
1418 {
1419 addDebugData("MRU callout JSON is not an array");
1420 return mrus;
1421 }
1422
1423 for (const auto& mruCallout : mruJSON)
1424 {
1425 try
1426 {
1427 auto priority = getPriorityFromJSON(mruCallout);
1428 auto id = mruCallout.at("ID").get<uint32_t>();
1429
1430 src::MRU::MRUCallout mru{static_cast<uint32_t>(priority), id};
1431 mrus.push_back(std::move(mru));
1432 }
1433 catch (const std::exception& e)
1434 {
1435 addDebugData(fmt::format("Invalid MRU entry in JSON: {}: {}",
1436 mruCallout.dump(), e.what()));
1437 }
1438 }
1439
1440 return mrus;
1441}
1442
Sumit Kumar9d43a722021-08-24 09:46:19 -05001443void SRC::setDumpStatus(const DataInterfaceBase& dataIface)
1444{
1445 std::vector<bool> dumpStatus{false, false, false};
1446
1447 try
1448 {
1449 std::vector<std::string> dumpType = {"bmc/entry", "resource/entry",
1450 "system/entry"};
1451 dumpStatus = dataIface.checkDumpStatus(dumpType);
1452
1453 // For bmc - set bit 0 of nibble [4-7] bits of byte-1 SP dump
1454 // For resource - set bit 2 of nibble [4-7] bits of byte-2 Hypervisor
1455 // For system - set bit 1 of nibble [4-7] bits of byte-2 HW dump
1456 _hexData[0] |= ((dumpStatus[0] << 19) | (dumpStatus[1] << 9) |
1457 (dumpStatus[2] << 10));
1458 }
1459 catch (const std::exception& e)
1460 {
Matt Spinler35a405b2022-03-02 11:42:42 -06001461 log<level::ERR>(
1462 fmt::format("Checking dump status failed: {}", e.what()).c_str());
Sumit Kumar9d43a722021-08-24 09:46:19 -05001463 }
1464}
1465
Sumit Kumar3e274432021-09-14 06:37:56 -05001466std::vector<uint8_t> SRC::getSrcStruct()
1467{
1468 std::vector<uint8_t> data;
1469 Stream stream{data};
1470
1471 //------ Ref section 4.3 in PEL doc---
1472 //------ SRC Structure 40 bytes-------
1473 // Byte-0 | Byte-1 | Byte-2 | Byte-3 |
1474 // -----------------------------------
1475 // 02 | 08 | 00 | 09 | ==> Header
1476 // 00 | 00 | 00 | 48 | ==> Header
1477 // 00 | 00 | 00 | 00 | ==> Hex data word-2
1478 // 00 | 00 | 00 | 00 | ==> Hex data word-3
1479 // 00 | 00 | 00 | 00 | ==> Hex data word-4
1480 // 20 | 00 | 00 | 00 | ==> Hex data word-5
1481 // 00 | 00 | 00 | 00 | ==> Hex data word-6
1482 // 00 | 00 | 00 | 00 | ==> Hex data word-7
1483 // 00 | 00 | 00 | 00 | ==> Hex data word-8
1484 // 00 | 00 | 00 | 00 | ==> Hex data word-9
1485 // -----------------------------------
1486 // ASCII string - 8 bytes |
1487 // -----------------------------------
1488 // ASCII space NULL - 24 bytes |
1489 // -----------------------------------
1490 //_size = Base SRC struct: 8 byte header + hex data section + ASCII string
1491
1492 uint8_t flags = (_flags | postOPPanel);
1493
1494 stream << _version << flags << _reserved1B << _wordCount << _reserved2B
1495 << _size;
1496
1497 for (auto& word : _hexData)
1498 {
1499 stream << word;
1500 }
1501
1502 _asciiString->flatten(stream);
1503
1504 return data;
1505}
1506
Vijay Lobo875b6c72021-10-20 17:38:56 -05001507void SRC::setProgressCode(const DataInterfaceBase& dataIface)
1508{
1509 std::vector<uint8_t> progressSRC;
1510
1511 try
1512 {
1513 progressSRC = dataIface.getRawProgressSRC();
1514 }
1515 catch (const std::exception& e)
1516 {
1517 log<level::ERR>(
1518 fmt::format("Error getting progress code: {}", e.what()).c_str());
1519 return;
1520 }
1521
1522 _hexData[2] = getProgressCode(progressSRC);
1523}
1524
1525uint32_t SRC::getProgressCode(std::vector<uint8_t>& rawProgressSRC)
1526{
1527 uint32_t progressCode = 0;
1528
1529 // A valid progress SRC is at least 72 bytes
1530 if (rawProgressSRC.size() < 72)
1531 {
1532 return progressCode;
1533 }
1534
1535 try
1536 {
1537 // The ASCII string field in progress SRCs starts at offset 40.
1538 // Take the first 8 characters to put in the uint32:
1539 // "CC009189" -> 0xCC009189
1540 Stream stream{rawProgressSRC, 40};
1541 src::AsciiString aString{stream};
1542 auto progressCodeString = aString.get().substr(0, 8);
1543
1544 if (std::all_of(progressCodeString.begin(), progressCodeString.end(),
1545 [](char c) {
1546 return std::isxdigit(static_cast<unsigned char>(c));
1547 }))
1548 {
1549 progressCode = std::stoul(progressCodeString, nullptr, 16);
1550 }
1551 }
1552 catch (const std::exception& e)
1553 {}
1554
1555 return progressCode;
1556}
1557
Matt Spinlerf9bae182019-10-09 13:37:38 -05001558} // namespace pels
1559} // namespace openpower