blob: 660411e1e603fed5ddce59f9c2f36a1db588a49c [file] [log] [blame]
Alexander Hansen40fb5492025-10-28 17:56:12 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3
Matt Spinlerf9bae182019-10-09 13:37:38 -05004#include "src.hpp"
5
Matt Spinler717de422020-06-04 13:10:14 -05006#include "device_callouts.hpp"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +08007#include "json_utils.hpp"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +08008#include "pel_values.hpp"
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +08009#ifdef PELTOOL
10#include <Python.h>
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080011
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080012#include <nlohmann/json.hpp>
Patrick Williams2544b412022-10-04 08:41:06 -050013
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080014#include <sstream>
15#endif
Matt Spinler0bacc8e2023-07-07 16:25:39 -050016#include <phosphor-logging/lg2.hpp>
Matt Spinlerf9bae182019-10-09 13:37:38 -050017
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050018#include <format>
19
Matt Spinlerf9bae182019-10-09 13:37:38 -050020namespace openpower
21{
22namespace pels
23{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080024namespace pv = openpower::pels::pel_values;
25namespace rg = openpower::pels::message;
Matt Spinler85f61a62020-06-03 16:28:55 -050026using namespace std::string_literals;
Matt Spinlerf9bae182019-10-09 13:37:38 -050027
Matt Spinler075e5ba2020-02-21 15:46:00 -060028constexpr size_t ccinSize = 4;
29
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080030#ifdef PELTOOL
Sumit Kumar516935a2021-04-14 13:00:54 -050031using orderedJSON = nlohmann::ordered_json;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080032
33void pyDecRef(PyObject* pyObj)
34{
35 Py_XDECREF(pyObj);
36}
37
38/**
39 * @brief Returns a JSON string to append to SRC section.
40 *
41 * The returning string will contain a JSON object, but without
42 * the outer {}. If the input JSON isn't a JSON object (dict), then
43 * one will be created with the input added to a 'SRC Details' key.
44 *
45 * @param[in] json - The JSON to convert to a string
46 *
47 * @return std::string - The JSON string
48 */
Sumit Kumar516935a2021-04-14 13:00:54 -050049std::string prettyJSON(const orderedJSON& json)
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080050{
Sumit Kumar516935a2021-04-14 13:00:54 -050051 orderedJSON output;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080052 if (!json.is_object())
53 {
54 output["SRC Details"] = json;
55 }
56 else
57 {
58 for (const auto& [key, value] : json.items())
59 {
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +080060 output["SRC Details"][key] = value;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080061 }
62 }
63
64 // Let nlohmann do the pretty printing.
65 std::stringstream stream;
66 stream << std::setw(4) << output;
67
68 auto jsonString = stream.str();
69
70 // Now it looks like:
71 // {
72 // "Key": "Value",
73 // ...
74 // }
75
76 // Replace the { and the following newline, and the } and its
77 // preceeding newline.
78 jsonString.erase(0, 2);
79
80 auto pos = jsonString.find_last_of('}');
81 jsonString.erase(pos - 1);
82
83 return jsonString;
84}
85
86/**
87 * @brief Call Python modules to parse the data into a JSON string
88 *
89 * The module to call is based on the Creator Subsystem ID under the namespace
90 * "srcparsers". For example: "srcparsers.xsrc.xsrc" where "x" is the Creator
91 * Subsystem ID in ASCII lowercase.
92 *
93 * All modules must provide the following:
94 * Function: parseSRCToJson
95 * Argument list:
96 * 1. (str) ASCII string (Hex Word 1)
97 * 2. (str) Hex Word 2
98 * 3. (str) Hex Word 3
99 * 4. (str) Hex Word 4
100 * 5. (str) Hex Word 5
101 * 6. (str) Hex Word 6
102 * 7. (str) Hex Word 7
103 * 8. (str) Hex Word 8
104 * 9. (str) Hex Word 9
105 *-Return data:
106 * 1. (str) JSON string
107 *
108 * @param[in] hexwords - Vector of strings of Hexwords 1-9
109 * @param[in] creatorID - The creatorID from the Private Header section
110 * @return std::optional<std::string> - The JSON string if it could be created,
111 * else std::nullopt
112 */
113std::optional<std::string> getPythonJSON(std::vector<std::string>& hexwords,
114 uint8_t creatorID)
115{
Matt Spinlerbe952d22022-07-01 11:30:11 -0500116 PyObject *pName, *pModule, *eType, *eValue, *eTraceback;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800117 std::string pErrStr;
118 std::string module = getNumberString("%c", tolower(creatorID)) + "src";
119 pName = PyUnicode_FromString(
120 std::string("srcparsers." + module + "." + module).c_str());
121 std::unique_ptr<PyObject, decltype(&pyDecRef)> modNamePtr(pName, &pyDecRef);
122 pModule = PyImport_Import(pName);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800123 if (pModule == NULL)
124 {
125 pErrStr = "No error string found";
126 PyErr_Fetch(&eType, &eValue, &eTraceback);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800127 if (eType)
128 {
129 Py_XDECREF(eType);
130 }
131 if (eTraceback)
132 {
133 Py_XDECREF(eTraceback);
134 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800135 if (eValue)
136 {
137 PyObject* pStr = PyObject_Str(eValue);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800138 Py_XDECREF(eValue);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800139 if (pStr)
140 {
141 pErrStr = PyUnicode_AsUTF8(pStr);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800142 Py_XDECREF(pStr);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800143 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800144 }
145 }
146 else
147 {
Patrick Williams075c7922024-08-16 15:19:49 -0400148 std::unique_ptr<PyObject, decltype(&pyDecRef)> modPtr(
149 pModule, &pyDecRef);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800150 std::string funcToCall = "parseSRCToJson";
Matt Spinlerbe952d22022-07-01 11:30:11 -0500151 PyObject* pKey = PyUnicode_FromString(funcToCall.c_str());
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800152 std::unique_ptr<PyObject, decltype(&pyDecRef)> keyPtr(pKey, &pyDecRef);
Matt Spinlerbe952d22022-07-01 11:30:11 -0500153 PyObject* pDict = PyModule_GetDict(pModule);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800154 Py_INCREF(pDict);
155 if (!PyDict_Contains(pDict, pKey))
156 {
157 Py_DECREF(pDict);
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500158 lg2::error(
159 "Python module error. Function missing: {FUNC}, SRC = {SRC}, module = {MODULE}",
160 "FUNC", funcToCall, "SRC", hexwords.front(), "MODULE", module);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800161 return std::nullopt;
162 }
Matt Spinlerbe952d22022-07-01 11:30:11 -0500163 PyObject* pFunc = PyDict_GetItemString(pDict, funcToCall.c_str());
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800164 Py_DECREF(pDict);
165 Py_INCREF(pFunc);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800166 if (PyCallable_Check(pFunc))
167 {
Matt Spinlerbe952d22022-07-01 11:30:11 -0500168 PyObject* pArgs = PyTuple_New(9);
Patrick Williams075c7922024-08-16 15:19:49 -0400169 std::unique_ptr<PyObject, decltype(&pyDecRef)> argPtr(
170 pArgs, &pyDecRef);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800171 for (size_t i = 0; i < 9; i++)
172 {
Matt Spinlerc1984032023-01-05 10:09:59 -0600173 std::string arg{"00000000"};
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800174 if (i < hexwords.size())
175 {
Matt Spinlerc1984032023-01-05 10:09:59 -0600176 arg = hexwords[i];
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800177 }
Matt Spinlerc1984032023-01-05 10:09:59 -0600178 PyTuple_SetItem(pArgs, i, Py_BuildValue("s", arg.c_str()));
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800179 }
Matt Spinlerbe952d22022-07-01 11:30:11 -0500180 PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800181 Py_DECREF(pFunc);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800182 if (pResult)
183 {
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800184 std::unique_ptr<PyObject, decltype(&pyDecRef)> resPtr(
185 pResult, &pyDecRef);
Matt Spinler91f6d3a2025-05-22 08:32:49 -0500186
187 if (pResult == Py_None)
188 {
189 return std::nullopt;
190 }
191
Patrick Williams075c7922024-08-16 15:19:49 -0400192 PyObject* pBytes =
193 PyUnicode_AsEncodedString(pResult, "utf-8", "~E~");
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800194 std::unique_ptr<PyObject, decltype(&pyDecRef)> pyBytePtr(
195 pBytes, &pyDecRef);
196 const char* output = PyBytes_AS_STRING(pBytes);
197 try
198 {
Matt Spinlerbb1c1d52021-06-03 13:18:48 -0600199 orderedJSON json = orderedJSON::parse(output);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800200 if ((json.is_object() && !json.empty()) ||
201 (json.is_array() && json.size() > 0) ||
202 (json.is_string() && json != ""))
203 {
204 return prettyJSON(json);
205 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800206 }
Patrick Williams66491c62021-10-06 12:23:37 -0500207 catch (const std::exception& e)
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800208 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500209 lg2::error(
210 "Bad JSON from parser. Error = {ERROR}, SRC = {SRC}, module = {MODULE}",
211 "ERROR", e, "SRC", hexwords.front(), "MODULE", module);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800212 return std::nullopt;
213 }
214 }
215 else
216 {
217 pErrStr = "No error string found";
218 PyErr_Fetch(&eType, &eValue, &eTraceback);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800219 if (eType)
220 {
221 Py_XDECREF(eType);
222 }
223 if (eTraceback)
224 {
225 Py_XDECREF(eTraceback);
226 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800227 if (eValue)
228 {
229 PyObject* pStr = PyObject_Str(eValue);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800230 Py_XDECREF(eValue);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800231 if (pStr)
232 {
233 pErrStr = PyUnicode_AsUTF8(pStr);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800234 Py_XDECREF(pStr);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800235 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800236 }
237 }
238 }
239 }
240 if (!pErrStr.empty())
241 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500242 lg2::debug("Python exception thrown by parser. Error = {ERROR}, "
243 "SRC = {SRC}, module = {MODULE}",
244 "ERROR", pErrStr, "SRC", hexwords.front(), "MODULE", module);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800245 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800246 return std::nullopt;
247}
248#endif
249
Matt Spinlerf9bae182019-10-09 13:37:38 -0500250void SRC::unflatten(Stream& stream)
251{
252 stream >> _header >> _version >> _flags >> _reserved1B >> _wordCount >>
253 _reserved2B >> _size;
254
255 for (auto& word : _hexData)
256 {
257 stream >> word;
258 }
259
260 _asciiString = std::make_unique<src::AsciiString>(stream);
261
262 if (hasAdditionalSections())
263 {
264 // The callouts section is currently the only extra subsection type
265 _callouts = std::make_unique<src::Callouts>(stream);
266 }
267}
268
Matt Spinler06885452019-11-06 10:35:42 -0600269void SRC::flatten(Stream& stream) const
Matt Spinlerf9bae182019-10-09 13:37:38 -0500270{
271 stream << _header << _version << _flags << _reserved1B << _wordCount
272 << _reserved2B << _size;
273
274 for (auto& word : _hexData)
275 {
276 stream << word;
277 }
278
279 _asciiString->flatten(stream);
280
281 if (_callouts)
282 {
283 _callouts->flatten(stream);
284 }
285}
286
287SRC::SRC(Stream& pel)
288{
289 try
290 {
291 unflatten(pel);
292 validate();
293 }
294 catch (const std::exception& e)
295 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500296 lg2::error("Cannot unflatten SRC, error = {ERROR}", "ERROR", e);
Matt Spinlerf9bae182019-10-09 13:37:38 -0500297 _valid = false;
298 }
299}
300
Matt Spinler075e5ba2020-02-21 15:46:00 -0600301SRC::SRC(const message::Entry& regEntry, const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500302 const nlohmann::json& jsonCallouts, const DataInterfaceBase& dataIface)
Matt Spinlerbd716f02019-10-15 10:54:11 -0500303{
304 _header.id = static_cast<uint16_t>(SectionID::primarySRC);
305 _header.version = srcSectionVersion;
306 _header.subType = srcSectionSubtype;
307 _header.componentID = regEntry.componentID;
308
309 _version = srcVersion;
310
311 _flags = 0;
Vijay Lobof3702bb2021-04-09 15:10:19 -0500312
Matt Spinlerbd716f02019-10-15 10:54:11 -0500313 _reserved1B = 0;
314
315 _wordCount = numSRCHexDataWords + 1;
316
317 _reserved2B = 0;
318
319 // There are multiple fields encoded in the hex data words.
320 std::for_each(_hexData.begin(), _hexData.end(),
321 [](auto& word) { word = 0; });
Matt Spinler7c619182020-07-27 15:15:11 -0500322
323 // Hex Word 2 Nibbles:
324 // MIGVEPFF
325 // M: Partition dump status = 0
326 // I: System boot state = TODO
327 // G: Partition Boot type = 0
Sumit Kumar9d43a722021-08-24 09:46:19 -0500328 // V: BMC dump status
Matt Spinler7c619182020-07-27 15:15:11 -0500329 // E: Platform boot mode = 0 (side = temporary, speed = fast)
Sumit Kumar9d43a722021-08-24 09:46:19 -0500330 // P: Platform dump status
Matt Spinler7c619182020-07-27 15:15:11 -0500331 // FF: SRC format, set below
332
Vijay Lobo875b6c72021-10-20 17:38:56 -0500333 setProgressCode(dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500334 setBMCFormat();
335 setBMCPosition();
Matt Spinler075e5ba2020-02-21 15:46:00 -0600336 setMotherboardCCIN(dataIface);
337
Matt Spinlerda5b76b2023-06-01 15:56:57 -0500338 if (regEntry.src.checkstopFlag)
339 {
340 setErrorStatusFlag(ErrorStatusFlags::hwCheckstop);
341 }
342
Matt Spinler3fe93e92023-04-14 14:06:59 -0500343 if (regEntry.src.deconfigFlag)
344 {
345 setErrorStatusFlag(ErrorStatusFlags::deconfigured);
346 }
347
Matt Spinlerbd716f02019-10-15 10:54:11 -0500348 // Fill in the last 4 words from the AdditionalData property contents.
349 setUserDefinedHexWords(regEntry, additionalData);
350
351 _asciiString = std::make_unique<src::AsciiString>(regEntry);
352
Sumit Kumar50bfa692022-01-06 06:48:26 -0600353 // Check for additional data - PEL_SUBSYSTEM
354 auto ss = additionalData.getValue("PEL_SUBSYSTEM");
355 if (ss)
356 {
Matt Spinler6f07df32025-05-09 11:42:39 -0500357 auto eventSubsystem = std::stoul(*ss, nullptr, 16);
Patrick Williams075c7922024-08-16 15:19:49 -0400358 std::string subsystem =
359 pv::getValue(eventSubsystem, pel_values::subsystemValues);
Sumit Kumar50bfa692022-01-06 06:48:26 -0600360 if (subsystem == "invalid")
361 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500362 lg2::warning("SRC: Invalid SubSystem value: {VAL}", "VAL", lg2::hex,
363 eventSubsystem);
Sumit Kumar50bfa692022-01-06 06:48:26 -0600364 }
365 else
366 {
367 _asciiString->setByte(2, eventSubsystem);
368 }
369 }
370
Matt Spinler5a90a952020-08-27 09:39:03 -0500371 addCallouts(regEntry, additionalData, jsonCallouts, dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500372
373 _size = baseSRCSize;
374 _size += _callouts ? _callouts->flattenedSize() : 0;
Matt Spinlere2eb14a2025-05-09 13:34:51 -0500375 _header.size = Section::headerSize() + _size;
Matt Spinlerbd716f02019-10-15 10:54:11 -0500376
377 _valid = true;
378}
379
380void SRC::setUserDefinedHexWords(const message::Entry& regEntry,
381 const AdditionalData& ad)
382{
383 if (!regEntry.src.hexwordADFields)
384 {
385 return;
386 }
387
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800388 // Save the AdditionalData value corresponding to the first element of
389 // adName tuple into _hexData[wordNum].
Matt Spinlerbd716f02019-10-15 10:54:11 -0500390 for (const auto& [wordNum, adName] : *regEntry.src.hexwordADFields)
391 {
392 // Can only set words 6 - 9
393 if (!isUserDefinedWord(wordNum))
394 {
Patrick Williams075c7922024-08-16 15:19:49 -0400395 std::string msg =
396 "SRC user data word out of range: " + std::to_string(wordNum);
Matt Spinler85f61a62020-06-03 16:28:55 -0500397 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500398 continue;
399 }
400
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800401 auto value = ad.getValue(std::get<0>(adName));
Matt Spinlerbd716f02019-10-15 10:54:11 -0500402 if (value)
403 {
404 _hexData[getWordIndexFromWordNum(wordNum)] =
405 std::strtoul(value.value().c_str(), nullptr, 0);
406 }
407 else
408 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800409 std::string msg = "Source for user data SRC word not found: " +
410 std::get<0>(adName);
Matt Spinler85f61a62020-06-03 16:28:55 -0500411 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500412 }
413 }
414}
415
Matt Spinler075e5ba2020-02-21 15:46:00 -0600416void SRC::setMotherboardCCIN(const DataInterfaceBase& dataIface)
417{
418 uint32_t ccin = 0;
419 auto ccinString = dataIface.getMotherboardCCIN();
420
421 try
422 {
423 if (ccinString.size() == ccinSize)
424 {
Matt Spinler6f07df32025-05-09 11:42:39 -0500425 ccin = std::stoi(ccinString, nullptr, 16);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600426 }
427 }
Patrick Williams66491c62021-10-06 12:23:37 -0500428 catch (const std::exception& e)
Matt Spinler075e5ba2020-02-21 15:46:00 -0600429 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500430 lg2::warning("Could not convert motherboard CCIN {CCIN} to a number",
431 "CCIN", ccinString);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600432 return;
433 }
434
435 // Set the first 2 bytes
436 _hexData[1] |= ccin << 16;
437}
438
Matt Spinlerf9bae182019-10-09 13:37:38 -0500439void SRC::validate()
440{
441 bool failed = false;
442
443 if ((header().id != static_cast<uint16_t>(SectionID::primarySRC)) &&
444 (header().id != static_cast<uint16_t>(SectionID::secondarySRC)))
445 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500446 lg2::error("Invalid SRC section ID: {ID}", "ID", lg2::hex, header().id);
Matt Spinlerf9bae182019-10-09 13:37:38 -0500447 failed = true;
448 }
449
450 // Check the version in the SRC, not in the header
Matt Spinlerbd716f02019-10-15 10:54:11 -0500451 if (_version != srcVersion)
Matt Spinlerf9bae182019-10-09 13:37:38 -0500452 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500453 lg2::error("Invalid SRC version: {VERSION}", "VERSION", lg2::hex,
454 header().version);
Matt Spinlerf9bae182019-10-09 13:37:38 -0500455 failed = true;
456 }
457
458 _valid = failed ? false : true;
459}
460
Matt Spinler075e5ba2020-02-21 15:46:00 -0600461bool SRC::isBMCSRC() const
462{
463 auto as = asciiString();
464 if (as.length() >= 2)
465 {
466 uint8_t errorType = strtoul(as.substr(0, 2).c_str(), nullptr, 16);
467 return (errorType == static_cast<uint8_t>(SRCType::bmcError) ||
468 errorType == static_cast<uint8_t>(SRCType::powerError));
469 }
470 return false;
471}
472
Matt Spinler4deed972023-04-28 14:09:22 -0500473bool SRC::isHostbootSRC() const
474{
475 auto as = asciiString();
476 if (as.length() >= 2)
477 {
478 uint8_t errorType = strtoul(as.substr(0, 2).c_str(), nullptr, 16);
479 return errorType == static_cast<uint8_t>(SRCType::hostbootError);
480 }
481 return false;
482}
483
Patrick Williams075c7922024-08-16 15:19:49 -0400484std::optional<std::string> SRC::getErrorDetails(
485 message::Registry& registry, DetailLevel type, bool toCache) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800486{
487 const std::string jsonIndent(indentLevel, 0x20);
488 std::string errorOut;
Matt Spinler075e5ba2020-02-21 15:46:00 -0600489 if (isBMCSRC())
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800490 {
491 auto entry = registry.lookup("0x" + asciiString().substr(4, 4),
492 rg::LookupType::reasonCode, toCache);
493 if (entry)
494 {
495 errorOut.append(jsonIndent + "\"Error Details\": {\n");
496 auto errorMsg = getErrorMessage(*entry);
497 if (errorMsg)
498 {
499 if (type == DetailLevel::message)
500 {
501 return errorMsg.value();
502 }
503 else
504 {
505 jsonInsert(errorOut, "Message", errorMsg.value(), 2);
506 }
507 }
508 if (entry->src.hexwordADFields)
509 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800510 std::map<size_t, std::tuple<std::string, std::string>>
511 adFields = entry->src.hexwordADFields.value();
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800512 for (const auto& hexwordMap : adFields)
513 {
Zane Shelleye8db29b2021-11-13 10:34:07 -0600514 auto srcValue = getNumberString(
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800515 "0x%X",
Zane Shelleye8db29b2021-11-13 10:34:07 -0600516 _hexData[getWordIndexFromWordNum(hexwordMap.first)]);
517
518 auto srcKey = std::get<0>(hexwordMap.second);
519 auto srcDesc = std::get<1>(hexwordMap.second);
520
521 // Only include this hex word in the error details if the
522 // description exists.
523 if (!srcDesc.empty())
524 {
525 std::vector<std::string> valueDescr;
526 valueDescr.push_back(srcValue);
527 valueDescr.push_back(srcDesc);
528 jsonInsertArray(errorOut, srcKey, valueDescr, 2);
529 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800530 }
531 }
532 errorOut.erase(errorOut.size() - 2);
533 errorOut.append("\n");
534 errorOut.append(jsonIndent + "},\n");
535 return errorOut;
536 }
537 }
538 return std::nullopt;
539}
540
Patrick Williams25291152025-02-01 08:21:42 -0500541std::optional<std::string> SRC::getErrorMessage(
542 const message::Entry& regEntry) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800543{
544 try
545 {
546 if (regEntry.doc.messageArgSources)
547 {
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800548 std::vector<uint32_t> argSourceVals;
549 std::string message;
550 const auto& argValues = regEntry.doc.messageArgSources.value();
551 for (size_t i = 0; i < argValues.size(); ++i)
552 {
553 argSourceVals.push_back(_hexData[getWordIndexFromWordNum(
554 argValues[i].back() - '0')]);
555 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500556
557 auto it = std::begin(regEntry.doc.message);
558 auto it_end = std::end(regEntry.doc.message);
559
560 while (it != it_end)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800561 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500562 if (*it == '%')
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800563 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500564 ++it;
565
566 size_t wordIndex = *it - '0';
567 if (isdigit(*it) && wordIndex >= 1 &&
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800568 static_cast<uint16_t>(wordIndex) <=
569 argSourceVals.size())
570 {
571 message.append(getNumberString(
Zane Shelley39936e32021-11-13 16:19:34 -0600572 "0x%08X", argSourceVals[wordIndex - 1]));
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800573 }
574 else
575 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500576 message.append("%" + std::string(1, *it));
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800577 }
578 }
579 else
580 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500581 message.push_back(*it);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800582 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500583 ++it;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800584 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500585
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800586 return message;
587 }
588 else
589 {
590 return regEntry.doc.message;
591 }
592 }
593 catch (const std::exception& e)
594 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500595 lg2::error(
596 "Cannot get error message from registry entry, error = {ERROR}",
597 "ERROR", e);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800598 }
599 return std::nullopt;
600}
601
602std::optional<std::string> SRC::getCallouts() const
603{
604 if (!_callouts)
605 {
606 return std::nullopt;
607 }
608 std::string printOut;
609 const std::string jsonIndent(indentLevel, 0x20);
610 const auto& callout = _callouts->callouts();
611 const auto& compDescrp = pv::failingComponentType;
612 printOut.append(jsonIndent + "\"Callout Section\": {\n");
613 jsonInsert(printOut, "Callout Count", std::to_string(callout.size()), 2);
614 printOut.append(jsonIndent + jsonIndent + "\"Callouts\": [");
615 for (auto& entry : callout)
616 {
617 printOut.append("{\n");
618 if (entry->fruIdentity())
619 {
620 jsonInsert(
621 printOut, "FRU Type",
622 compDescrp.at(entry->fruIdentity()->failingComponentType()), 3);
623 jsonInsert(printOut, "Priority",
624 pv::getValue(entry->priority(),
625 pel_values::calloutPriorityValues),
626 3);
627 if (!entry->locationCode().empty())
628 {
629 jsonInsert(printOut, "Location Code", entry->locationCode(), 3);
630 }
631 if (entry->fruIdentity()->getPN().has_value())
632 {
633 jsonInsert(printOut, "Part Number",
634 entry->fruIdentity()->getPN().value(), 3);
635 }
636 if (entry->fruIdentity()->getMaintProc().has_value())
637 {
Matt Spinler9e8b49e2020-09-10 13:15:26 -0500638 jsonInsert(printOut, "Procedure",
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800639 entry->fruIdentity()->getMaintProc().value(), 3);
640 if (pv::procedureDesc.find(
641 entry->fruIdentity()->getMaintProc().value()) !=
642 pv::procedureDesc.end())
643 {
644 jsonInsert(
645 printOut, "Description",
646 pv::procedureDesc.at(
647 entry->fruIdentity()->getMaintProc().value()),
648 3);
649 }
650 }
651 if (entry->fruIdentity()->getCCIN().has_value())
652 {
653 jsonInsert(printOut, "CCIN",
654 entry->fruIdentity()->getCCIN().value(), 3);
655 }
656 if (entry->fruIdentity()->getSN().has_value())
657 {
658 jsonInsert(printOut, "Serial Number",
659 entry->fruIdentity()->getSN().value(), 3);
660 }
661 }
662 if (entry->pceIdentity())
663 {
664 const auto& pceIdentMtms = entry->pceIdentity()->mtms();
665 if (!pceIdentMtms.machineTypeAndModel().empty())
666 {
667 jsonInsert(printOut, "PCE MTMS",
668 pceIdentMtms.machineTypeAndModel() + "_" +
669 pceIdentMtms.machineSerialNumber(),
670 3);
671 }
672 if (!entry->pceIdentity()->enclosureName().empty())
673 {
674 jsonInsert(printOut, "PCE Name",
675 entry->pceIdentity()->enclosureName(), 3);
676 }
677 }
678 if (entry->mru())
679 {
680 const auto& mruCallouts = entry->mru()->mrus();
681 std::string mruId;
682 for (auto& element : mruCallouts)
683 {
684 if (!mruId.empty())
685 {
686 mruId.append(", " + getNumberString("%08X", element.id));
687 }
688 else
689 {
690 mruId.append(getNumberString("%08X", element.id));
691 }
692 }
693 jsonInsert(printOut, "MRU Id", mruId, 3);
694 }
695 printOut.erase(printOut.size() - 2);
696 printOut.append("\n" + jsonIndent + jsonIndent + "}, ");
697 };
698 printOut.erase(printOut.size() - 2);
699 printOut.append("]\n" + jsonIndent + "}");
700 return printOut;
701}
702
Patrick Williams25291152025-02-01 08:21:42 -0500703std::optional<std::string> SRC::getJSON(message::Registry& registry,
704 const std::vector<std::string>& plugins
705 [[maybe_unused]],
706 uint8_t creatorID) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800707{
708 std::string ps;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800709 std::vector<std::string> hexwords;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800710 jsonInsert(ps, pv::sectionVer, getNumberString("%d", _header.version), 1);
711 jsonInsert(ps, pv::subSection, getNumberString("%d", _header.subType), 1);
Matt Spinlerb832aa52023-03-21 15:32:34 -0500712 jsonInsert(ps, pv::createdBy,
713 getComponentName(_header.componentID, creatorID), 1);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800714 jsonInsert(ps, "SRC Version", getNumberString("0x%02X", _version), 1);
Harisuddin Mohamed Isac32e5512020-02-06 18:05:21 +0800715 jsonInsert(ps, "SRC Format", getNumberString("0x%02X", _hexData[0] & 0xFF),
716 1);
717 jsonInsert(ps, "Virtual Progress SRC",
718 pv::boolString.at(_flags & virtualProgressSRC), 1);
719 jsonInsert(ps, "I5/OS Service Event Bit",
720 pv::boolString.at(_flags & i5OSServiceEventBit), 1);
721 jsonInsert(ps, "Hypervisor Dump Initiated",
722 pv::boolString.at(_flags & hypDumpInit), 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600723
724 if (isBMCSRC())
725 {
726 std::string ccinString;
727 uint32_t ccin = _hexData[1] >> 16;
728
729 if (ccin)
730 {
731 ccinString = getNumberString("%04X", ccin);
732 }
733 // The PEL spec calls it a backplane, so call it that here.
734 jsonInsert(ps, "Backplane CCIN", ccinString, 1);
Matt Spinlerafa2c792020-08-27 11:01:39 -0500735
Sumit Kumar3e274432021-09-14 06:37:56 -0500736 jsonInsert(ps, "Terminate FW Error",
737 pv::boolString.at(
738 _hexData[3] &
739 static_cast<uint32_t>(ErrorStatusFlags::terminateFwErr)),
740 1);
Matt Spinler4deed972023-04-28 14:09:22 -0500741 }
742
743 if (isBMCSRC() || isHostbootSRC())
744 {
Matt Spinlerafa2c792020-08-27 11:01:39 -0500745 jsonInsert(ps, "Deconfigured",
746 pv::boolString.at(
747 _hexData[3] &
748 static_cast<uint32_t>(ErrorStatusFlags::deconfigured)),
749 1);
750
751 jsonInsert(
752 ps, "Guarded",
Patrick Williams075c7922024-08-16 15:19:49 -0400753 pv::boolString.at(
754 _hexData[3] & static_cast<uint32_t>(ErrorStatusFlags::guarded)),
Matt Spinlerafa2c792020-08-27 11:01:39 -0500755 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600756 }
757
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800758 auto errorDetails = getErrorDetails(registry, DetailLevel::json, true);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800759 if (errorDetails)
760 {
761 ps.append(errorDetails.value());
762 }
763 jsonInsert(ps, "Valid Word Count", getNumberString("0x%02X", _wordCount),
764 1);
765 std::string refcode = asciiString();
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800766 hexwords.push_back(refcode);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800767 std::string extRefcode;
768 size_t pos = refcode.find(0x20);
769 if (pos != std::string::npos)
770 {
771 size_t nextPos = refcode.find_first_not_of(0x20, pos);
772 if (nextPos != std::string::npos)
773 {
774 extRefcode = trimEnd(refcode.substr(nextPos));
775 }
776 refcode.erase(pos);
777 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800778 jsonInsert(ps, "Reference Code", refcode, 1);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800779 if (!extRefcode.empty())
780 {
781 jsonInsert(ps, "Extended Reference Code", extRefcode, 1);
782 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800783 for (size_t i = 2; i <= _wordCount; i++)
784 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800785 std::string tmpWord =
786 getNumberString("%08X", _hexData[getWordIndexFromWordNum(i)]);
787 jsonInsert(ps, "Hex Word " + std::to_string(i), tmpWord, 1);
788 hexwords.push_back(tmpWord);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800789 }
790 auto calloutJson = getCallouts();
791 if (calloutJson)
792 {
793 ps.append(calloutJson.value());
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800794 ps.append(",\n");
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800795 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800796 std::string subsystem = getNumberString("%c", tolower(creatorID));
797 bool srcDetailExists = false;
798#ifdef PELTOOL
799 if (std::find(plugins.begin(), plugins.end(), subsystem + "src") !=
800 plugins.end())
801 {
802 auto pyJson = getPythonJSON(hexwords, creatorID);
803 if (pyJson)
804 {
805 ps.append(pyJson.value());
806 srcDetailExists = true;
807 }
808 }
809#endif
810 if (!srcDetailExists)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800811 {
812 ps.erase(ps.size() - 2);
813 }
814 return ps;
815}
816
Matt Spinler03984582020-04-09 13:17:58 -0500817void SRC::addCallouts(const message::Entry& regEntry,
818 const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500819 const nlohmann::json& jsonCallouts,
Matt Spinlered046852020-03-13 13:58:15 -0500820 const DataInterfaceBase& dataIface)
821{
Patrick Williams075c7922024-08-16 15:19:49 -0400822 auto registryCallouts =
823 getRegistryCallouts(regEntry, additionalData, dataIface);
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500824
Matt Spinlered046852020-03-13 13:58:15 -0500825 auto item = additionalData.getValue("CALLOUT_INVENTORY_PATH");
Miguel Gomez53ef1552020-10-14 21:16:32 +0000826 auto priority = additionalData.getValue("CALLOUT_PRIORITY");
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500827
Miguel Gomez53ef1552020-10-14 21:16:32 +0000828 std::optional<CalloutPriority> calloutPriority;
829
830 // Only H, M or L priority values.
831 if (priority && !(*priority).empty())
832 {
833 uint8_t p = (*priority)[0];
834 if (p == 'H' || p == 'M' || p == 'L')
835 {
836 calloutPriority = static_cast<CalloutPriority>(p);
837 }
838 }
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500839 // If the first registry callout says to use the passed in inventory
840 // path to get the location code for a symbolic FRU callout with a
841 // trusted location code, then do not add the inventory path as a
842 // normal FRU callout.
843 bool useInvForSymbolicFRULocCode =
844 !registryCallouts.empty() && registryCallouts[0].useInventoryLocCode &&
845 !registryCallouts[0].symbolicFRUTrusted.empty();
846
847 if (item && !useInvForSymbolicFRULocCode)
Matt Spinlered046852020-03-13 13:58:15 -0500848 {
Miguel Gomez53ef1552020-10-14 21:16:32 +0000849 addInventoryCallout(*item, calloutPriority, std::nullopt, dataIface);
Matt Spinlered046852020-03-13 13:58:15 -0500850 }
851
Matt Spinler717de422020-06-04 13:10:14 -0500852 addDevicePathCallouts(additionalData, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -0500853
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500854 addRegistryCallouts(registryCallouts, dataIface,
855 (useInvForSymbolicFRULocCode) ? item : std::nullopt);
Matt Spinler5a90a952020-08-27 09:39:03 -0500856
857 if (!jsonCallouts.empty())
858 {
859 addJSONCallouts(jsonCallouts, dataIface);
860 }
Matt Spinlered046852020-03-13 13:58:15 -0500861}
862
Matt Spinler7b923722025-03-19 13:17:23 -0500863void SRC::addLocationCodeOnlyCallout(const std::string& locationCode,
864 const CalloutPriority priority)
865{
866 std::string empty;
867 std::vector<src::MRU::MRUCallout> mrus;
868 auto callout = std::make_unique<src::Callout>(priority, locationCode, empty,
869 empty, empty, mrus);
870 createCalloutsObject();
871 _callouts->addCallout(std::move(callout));
872}
873
Matt Spinlered046852020-03-13 13:58:15 -0500874void SRC::addInventoryCallout(const std::string& inventoryPath,
Matt Spinleraf191c72020-06-04 11:35:13 -0500875 const std::optional<CalloutPriority>& priority,
876 const std::optional<std::string>& locationCode,
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500877 const DataInterfaceBase& dataIface,
878 const std::vector<src::MRU::MRUCallout>& mrus)
Matt Spinlered046852020-03-13 13:58:15 -0500879{
880 std::string locCode;
881 std::string fn;
882 std::string ccin;
883 std::string sn;
884 std::unique_ptr<src::Callout> callout;
885
Matt Spinlered046852020-03-13 13:58:15 -0500886 try
887 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500888 // Use the passed in location code if there otherwise look it up
889 if (locationCode)
890 {
891 locCode = *locationCode;
892 }
893 else
894 {
895 locCode = dataIface.getLocationCode(inventoryPath);
896 }
Matt Spinlered046852020-03-13 13:58:15 -0500897
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500898 try
899 {
900 dataIface.getHWCalloutFields(inventoryPath, fn, ccin, sn);
901
Patrick Williams075c7922024-08-16 15:19:49 -0400902 CalloutPriority p =
903 priority ? priority.value() : CalloutPriority::high;
Matt Spinleraf191c72020-06-04 11:35:13 -0500904
Patrick Williams075c7922024-08-16 15:19:49 -0400905 callout =
906 std::make_unique<src::Callout>(p, locCode, fn, ccin, sn, mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500907 }
Patrick Williams45e83522022-07-22 19:26:52 -0500908 catch (const sdbusplus::exception_t& e)
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500909 {
Patrick Williams075c7922024-08-16 15:19:49 -0400910 std::string msg =
911 "No VPD found for " + inventoryPath + ": " + e.what();
Matt Spinler85f61a62020-06-03 16:28:55 -0500912 addDebugData(msg);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500913
914 // Just create the callout with empty FRU fields
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500915 callout = std::make_unique<src::Callout>(
916 CalloutPriority::high, locCode, fn, ccin, sn, mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500917 }
Matt Spinlered046852020-03-13 13:58:15 -0500918 }
Patrick Williams45e83522022-07-22 19:26:52 -0500919 catch (const sdbusplus::exception_t& e)
Matt Spinlered046852020-03-13 13:58:15 -0500920 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500921 std::string msg = "Could not get location code for " + inventoryPath +
922 ": " + e.what();
923 addDebugData(msg);
Matt Spinlered046852020-03-13 13:58:15 -0500924
Matt Spinler479b6922021-08-17 16:34:59 -0500925 // Don't add a callout in this case, because:
926 // 1) With how the inventory is primed, there is no case where
927 // a location code is expected to be missing. This implies
928 // the caller is passing in something invalid.
929 // 2) The addDebugData call above will put the passed in path into
930 // a user data section that can be seen by development for debug.
931 // 3) Even if we wanted to do a 'no_vpd_for_fru' sort of maint.
932 // procedure, we don't have a good way to indicate to the user
933 // anything about the intended callout (they won't see user data).
934 // 4) Creating a new standalone event log for this problem isn't
935 // possible from inside a PEL section.
Matt Spinlered046852020-03-13 13:58:15 -0500936 }
937
Matt Spinler479b6922021-08-17 16:34:59 -0500938 if (callout)
939 {
940 createCalloutsObject();
941 _callouts->addCallout(std::move(callout));
942 }
Matt Spinler03984582020-04-09 13:17:58 -0500943}
Matt Spinlered046852020-03-13 13:58:15 -0500944
Patrick Williams075c7922024-08-16 15:19:49 -0400945std::vector<message::RegistryCallout> SRC::getRegistryCallouts(
946 const message::Entry& regEntry, const AdditionalData& additionalData,
947 const DataInterfaceBase& dataIface)
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500948{
949 std::vector<message::RegistryCallout> registryCallouts;
950
951 if (regEntry.callouts)
952 {
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500953 std::vector<std::string> systemNames;
954
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500955 try
956 {
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500957 systemNames = dataIface.getSystemNames();
958 }
959 catch (const std::exception& e)
960 {
961 // Compatible interface not available yet
962 }
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500963
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500964 try
965 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500966 registryCallouts = message::Registry::getCallouts(
967 regEntry.callouts.value(), systemNames, additionalData);
968 }
969 catch (const std::exception& e)
970 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -0500971 addDebugData(std::format(
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500972 "Error parsing PEL message registry callout JSON: {}",
973 e.what()));
974 }
975 }
976
977 return registryCallouts;
978}
979
980void SRC::addRegistryCallouts(
981 const std::vector<message::RegistryCallout>& callouts,
982 const DataInterfaceBase& dataIface,
983 std::optional<std::string> trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -0500984{
985 try
986 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500987 for (const auto& callout : callouts)
Matt Spinler03984582020-04-09 13:17:58 -0500988 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500989 addRegistryCallout(callout, dataIface, trustedSymbolicFRUInvPath);
990
991 // Only the first callout gets the inventory path
992 if (trustedSymbolicFRUInvPath)
993 {
994 trustedSymbolicFRUInvPath = std::nullopt;
995 }
Matt Spinler03984582020-04-09 13:17:58 -0500996 }
997 }
Patrick Williams66491c62021-10-06 12:23:37 -0500998 catch (const std::exception& e)
Matt Spinler03984582020-04-09 13:17:58 -0500999 {
Patrick Williams075c7922024-08-16 15:19:49 -04001000 std::string msg =
1001 "Error parsing PEL message registry callout JSON: "s + e.what();
Matt Spinler85f61a62020-06-03 16:28:55 -05001002 addDebugData(msg);
Matt Spinler03984582020-04-09 13:17:58 -05001003 }
1004}
1005
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001006void SRC::addRegistryCallout(
1007 const message::RegistryCallout& regCallout,
1008 const DataInterfaceBase& dataIface,
1009 const std::optional<std::string>& trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -05001010{
1011 std::unique_ptr<src::Callout> callout;
Matt Spinler03984582020-04-09 13:17:58 -05001012 auto locCode = regCallout.locCode;
Matt Spinler7b923722025-03-19 13:17:23 -05001013 bool locExpanded = true;
Matt Spinler03984582020-04-09 13:17:58 -05001014
Matt Spinleraf191c72020-06-04 11:35:13 -05001015 if (!locCode.empty())
1016 {
1017 try
1018 {
1019 locCode = dataIface.expandLocationCode(locCode, 0);
1020 }
1021 catch (const std::exception& e)
1022 {
Patrick Williams2544b412022-10-04 08:41:06 -05001023 auto msg = "Unable to expand location code " + locCode + ": " +
1024 e.what();
Matt Spinleraf191c72020-06-04 11:35:13 -05001025 addDebugData(msg);
Matt Spinler7b923722025-03-19 13:17:23 -05001026 locExpanded = false;
Matt Spinleraf191c72020-06-04 11:35:13 -05001027 }
1028 }
1029
Matt Spinler03984582020-04-09 13:17:58 -05001030 // Via the PEL values table, get the priority enum.
1031 // The schema will have validated the priority was a valid value.
Patrick Williams075c7922024-08-16 15:19:49 -04001032 auto priorityIt =
1033 pv::findByName(regCallout.priority, pv::calloutPriorityValues);
Matt Spinler03984582020-04-09 13:17:58 -05001034 assert(priorityIt != pv::calloutPriorityValues.end());
1035 auto priority =
1036 static_cast<CalloutPriority>(std::get<pv::fieldValuePos>(*priorityIt));
1037
1038 if (!regCallout.procedure.empty())
1039 {
1040 // Procedure callout
Matt Spinler2edce4e2024-01-17 11:13:51 -06001041 callout = std::make_unique<src::Callout>(priority, regCallout.procedure,
1042 src::CalloutValueType::raw);
Matt Spinler03984582020-04-09 13:17:58 -05001043 }
1044 else if (!regCallout.symbolicFRU.empty())
1045 {
1046 // Symbolic FRU callout
1047 callout = std::make_unique<src::Callout>(
1048 priority, regCallout.symbolicFRU, locCode, false);
1049 }
1050 else if (!regCallout.symbolicFRUTrusted.empty())
1051 {
1052 // Symbolic FRU with trusted location code callout
Matt Spinler7b923722025-03-19 13:17:23 -05001053 bool trusted = false;
Matt Spinler03984582020-04-09 13:17:58 -05001054
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001055 // Use the location code from the inventory path if there is one.
1056 if (trustedSymbolicFRUInvPath)
1057 {
1058 try
1059 {
1060 locCode = dataIface.getLocationCode(*trustedSymbolicFRUInvPath);
Matt Spinler7b923722025-03-19 13:17:23 -05001061 trusted = true;
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001062 }
1063 catch (const std::exception& e)
1064 {
1065 addDebugData(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001066 std::format("Could not get location code for {}: {}",
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001067 *trustedSymbolicFRUInvPath, e.what()));
1068 locCode.clear();
1069 }
1070 }
1071
Matt Spinler7b923722025-03-19 13:17:23 -05001072 // Can only trust the location code if it isn't empty and is expanded.
1073 if (!locCode.empty() && locExpanded)
1074 {
1075 trusted = true;
1076 }
1077
Matt Spinler03984582020-04-09 13:17:58 -05001078 // The registry wants it to be trusted, but that requires a valid
1079 // location code for it to actually be.
1080 callout = std::make_unique<src::Callout>(
Matt Spinler7b923722025-03-19 13:17:23 -05001081 priority, regCallout.symbolicFRUTrusted, locCode, trusted);
Matt Spinler03984582020-04-09 13:17:58 -05001082 }
1083 else
1084 {
Matt Spinleraf191c72020-06-04 11:35:13 -05001085 // A hardware callout
Matt Spinler7b923722025-03-19 13:17:23 -05001086
1087 // If couldn't expand the location code, don't bother
1088 // looking up the inventory path.
1089 if (!locExpanded && !locCode.empty())
1090 {
1091 addLocationCodeOnlyCallout(locCode, priority);
1092 return;
1093 }
1094
Matt Spinlerbad056b2023-01-25 14:16:57 -06001095 std::vector<std::string> inventoryPaths;
Matt Spinleraf191c72020-06-04 11:35:13 -05001096
1097 try
1098 {
1099 // Get the inventory item from the unexpanded location code
Matt Spinlerbad056b2023-01-25 14:16:57 -06001100 inventoryPaths =
Matt Spinler2f9225a2020-08-05 12:58:49 -05001101 dataIface.getInventoryFromLocCode(regCallout.locCode, 0, false);
Matt Spinleraf191c72020-06-04 11:35:13 -05001102 }
1103 catch (const std::exception& e)
1104 {
1105 std::string msg =
1106 "Unable to get inventory path from location code: " + locCode +
1107 ": " + e.what();
1108 addDebugData(msg);
Matt Spinler7b923722025-03-19 13:17:23 -05001109 if (!locCode.empty())
1110 {
1111 // Still add a callout with just the location code.
1112 addLocationCodeOnlyCallout(locCode, priority);
1113 }
Matt Spinleraf191c72020-06-04 11:35:13 -05001114 return;
1115 }
1116
Matt Spinlerbad056b2023-01-25 14:16:57 -06001117 // Just use first path returned since they all point to the same FRU.
1118 addInventoryCallout(inventoryPaths[0], priority, locCode, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -05001119 }
1120
1121 if (callout)
1122 {
1123 createCalloutsObject();
1124 _callouts->addCallout(std::move(callout));
1125 }
1126}
Matt Spinlered046852020-03-13 13:58:15 -05001127
Matt Spinler717de422020-06-04 13:10:14 -05001128void SRC::addDevicePathCallouts(const AdditionalData& additionalData,
1129 const DataInterfaceBase& dataIface)
1130{
1131 std::vector<device_callouts::Callout> callouts;
1132 auto i2cBus = additionalData.getValue("CALLOUT_IIC_BUS");
1133 auto i2cAddr = additionalData.getValue("CALLOUT_IIC_ADDR");
1134 auto devPath = additionalData.getValue("CALLOUT_DEVICE_PATH");
1135
1136 // A device callout contains either:
1137 // * CALLOUT_ERRNO, CALLOUT_DEVICE_PATH
1138 // * CALLOUT_ERRNO, CALLOUT_IIC_BUS, CALLOUT_IIC_ADDR
1139 // We don't care about the errno.
1140
1141 if (devPath)
1142 {
1143 try
1144 {
1145 callouts = device_callouts::getCallouts(*devPath,
1146 dataIface.getSystemNames());
1147 }
1148 catch (const std::exception& e)
1149 {
1150 addDebugData(e.what());
1151 callouts.clear();
1152 }
1153 }
1154 else if (i2cBus && i2cAddr)
1155 {
1156 size_t bus;
1157 uint8_t address;
1158
1159 try
1160 {
1161 // If /dev/i2c- is prepended, remove it
1162 if (i2cBus->find("/dev/i2c-") != std::string::npos)
1163 {
1164 *i2cBus = i2cBus->substr(9);
1165 }
1166
1167 bus = stoul(*i2cBus, nullptr, 0);
1168 address = stoul(*i2cAddr, nullptr, 0);
1169 }
1170 catch (const std::exception& e)
1171 {
Patrick Williams075c7922024-08-16 15:19:49 -04001172 std::string msg =
1173 "Invalid CALLOUT_IIC_BUS " + *i2cBus + " or CALLOUT_IIC_ADDR " +
1174 *i2cAddr + " in AdditionalData property";
Matt Spinler717de422020-06-04 13:10:14 -05001175 addDebugData(msg);
1176 return;
1177 }
1178
1179 try
1180 {
1181 callouts = device_callouts::getI2CCallouts(
1182 bus, address, dataIface.getSystemNames());
1183 }
1184 catch (const std::exception& e)
1185 {
1186 addDebugData(e.what());
1187 callouts.clear();
1188 }
1189 }
1190
1191 for (const auto& callout : callouts)
1192 {
1193 // The priority shouldn't be invalid, but check just in case.
1194 CalloutPriority priority = CalloutPriority::high;
1195
1196 if (!callout.priority.empty())
1197 {
1198 auto p = pel_values::findByValue(
1199 static_cast<uint32_t>(callout.priority[0]),
1200 pel_values::calloutPriorityValues);
1201
1202 if (p != pel_values::calloutPriorityValues.end())
1203 {
1204 priority = static_cast<CalloutPriority>(callout.priority[0]);
1205 }
1206 else
1207 {
Matt Spinlerd7654dc2025-05-09 11:47:50 -05001208 auto msg =
1209 std::string{
1210 "Invalid priority found in dev callout JSON: "} +
Matt Spinler717de422020-06-04 13:10:14 -05001211 callout.priority[0];
1212 addDebugData(msg);
1213 }
1214 }
1215
Matt Spinler0d92b522021-06-16 13:28:17 -06001216 std::optional<std::string> locCode;
1217
1218 try
1219 {
1220 locCode = dataIface.expandLocationCode(callout.locationCode, 0);
1221 }
1222 catch (const std::exception& e)
1223 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001224 auto msg = std::format("Unable to expand location code {}: {}",
Matt Spinler0d92b522021-06-16 13:28:17 -06001225 callout.locationCode, e.what());
1226 addDebugData(msg);
Matt Spinler7b923722025-03-19 13:17:23 -05001227
1228 // Add the callout with just the unexpanded location code.
1229 addLocationCodeOnlyCallout(callout.locationCode, priority);
1230 continue;
Matt Spinler0d92b522021-06-16 13:28:17 -06001231 }
1232
Matt Spinler717de422020-06-04 13:10:14 -05001233 try
1234 {
Matt Spinlerbad056b2023-01-25 14:16:57 -06001235 auto inventoryPaths = dataIface.getInventoryFromLocCode(
Matt Spinler2f9225a2020-08-05 12:58:49 -05001236 callout.locationCode, 0, false);
Matt Spinler717de422020-06-04 13:10:14 -05001237
Matt Spinlerbad056b2023-01-25 14:16:57 -06001238 // Just use first path returned since they all
1239 // point to the same FRU.
1240 addInventoryCallout(inventoryPaths[0], priority, locCode,
1241 dataIface);
Matt Spinler717de422020-06-04 13:10:14 -05001242 }
1243 catch (const std::exception& e)
1244 {
1245 std::string msg =
1246 "Unable to get inventory path from location code: " +
1247 callout.locationCode + ": " + e.what();
1248 addDebugData(msg);
Matt Spinler7b923722025-03-19 13:17:23 -05001249 // Add the callout with just the location code.
1250 addLocationCodeOnlyCallout(callout.locationCode, priority);
1251 continue;
Matt Spinler717de422020-06-04 13:10:14 -05001252 }
1253
1254 // Until the code is there to convert these MRU value strings to
1255 // the official MRU values in the callout objects, just store
1256 // the MRU name in the debug UserData section.
1257 if (!callout.mru.empty())
1258 {
1259 std::string msg = "MRU: " + callout.mru;
1260 addDebugData(msg);
1261 }
1262
1263 // getCallouts() may have generated some debug data it stored
1264 // in a callout object. Save it as well.
1265 if (!callout.debug.empty())
1266 {
1267 addDebugData(callout.debug);
1268 }
1269 }
1270}
1271
Matt Spinler5a90a952020-08-27 09:39:03 -05001272void SRC::addJSONCallouts(const nlohmann::json& jsonCallouts,
1273 const DataInterfaceBase& dataIface)
1274{
1275 if (jsonCallouts.empty())
1276 {
1277 return;
1278 }
1279
1280 if (!jsonCallouts.is_array())
1281 {
1282 addDebugData("Callout JSON isn't an array");
1283 return;
1284 }
1285
1286 for (const auto& callout : jsonCallouts)
1287 {
1288 try
1289 {
1290 addJSONCallout(callout, dataIface);
1291 }
1292 catch (const std::exception& e)
1293 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001294 addDebugData(std::format(
Matt Spinler5a90a952020-08-27 09:39:03 -05001295 "Failed extracting callout data from JSON: {}", e.what()));
1296 }
1297 }
1298}
1299
1300void SRC::addJSONCallout(const nlohmann::json& jsonCallout,
1301 const DataInterfaceBase& dataIface)
1302{
Matt Spinler3bdd0112020-08-27 10:24:34 -05001303 auto priority = getPriorityFromJSON(jsonCallout);
1304 std::string locCode;
1305 std::string unexpandedLocCode;
1306 std::unique_ptr<src::Callout> callout;
1307
1308 // Expand the location code if it's there
1309 if (jsonCallout.contains("LocationCode"))
1310 {
1311 unexpandedLocCode = jsonCallout.at("LocationCode").get<std::string>();
1312
1313 try
1314 {
1315 locCode = dataIface.expandLocationCode(unexpandedLocCode, 0);
1316 }
1317 catch (const std::exception& e)
1318 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001319 addDebugData(std::format("Unable to expand location code {}: {}",
Matt Spinler3bdd0112020-08-27 10:24:34 -05001320 unexpandedLocCode, e.what()));
1321 // Use the value from the JSON so at least there's something
1322 locCode = unexpandedLocCode;
1323 }
1324 }
1325
1326 // Create either a procedure, symbolic FRU, or normal FRU callout.
1327 if (jsonCallout.contains("Procedure"))
1328 {
1329 auto procedure = jsonCallout.at("Procedure").get<std::string>();
1330
Matt Spinler3c7ec6d2022-05-06 08:50:20 -05001331 // If it's the registry name instead of the raw name, convert.
1332 if (pv::maintenanceProcedures.find(procedure) !=
1333 pv::maintenanceProcedures.end())
1334 {
1335 procedure = pv::maintenanceProcedures.at(procedure);
1336 }
1337
Matt Spinler3bdd0112020-08-27 10:24:34 -05001338 callout = std::make_unique<src::Callout>(
1339 static_cast<CalloutPriority>(priority), procedure,
1340 src::CalloutValueType::raw);
1341 }
1342 else if (jsonCallout.contains("SymbolicFRU"))
1343 {
1344 auto fru = jsonCallout.at("SymbolicFRU").get<std::string>();
1345
Matt Spinler3c7ec6d2022-05-06 08:50:20 -05001346 // If it's the registry name instead of the raw name, convert.
1347 if (pv::symbolicFRUs.find(fru) != pv::symbolicFRUs.end())
1348 {
1349 fru = pv::symbolicFRUs.at(fru);
1350 }
1351
Matt Spinler3bdd0112020-08-27 10:24:34 -05001352 bool trusted = false;
1353 if (jsonCallout.contains("TrustedLocationCode") && !locCode.empty())
1354 {
1355 trusted = jsonCallout.at("TrustedLocationCode").get<bool>();
1356 }
1357
1358 callout = std::make_unique<src::Callout>(
1359 static_cast<CalloutPriority>(priority), fru,
1360 src::CalloutValueType::raw, locCode, trusted);
1361 }
1362 else
1363 {
1364 // A hardware FRU
1365 std::string inventoryPath;
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001366 std::vector<src::MRU::MRUCallout> mrus;
Matt Spinler3bdd0112020-08-27 10:24:34 -05001367
1368 if (jsonCallout.contains("InventoryPath"))
1369 {
1370 inventoryPath = jsonCallout.at("InventoryPath").get<std::string>();
1371 }
1372 else
1373 {
1374 if (unexpandedLocCode.empty())
1375 {
1376 throw std::runtime_error{"JSON callout needs either an "
1377 "inventory path or location code"};
1378 }
1379
1380 try
1381 {
Matt Spinlerbad056b2023-01-25 14:16:57 -06001382 auto inventoryPaths = dataIface.getInventoryFromLocCode(
Matt Spinler3bdd0112020-08-27 10:24:34 -05001383 unexpandedLocCode, 0, false);
Matt Spinlerbad056b2023-01-25 14:16:57 -06001384 // Just use first path returned since they all
1385 // point to the same FRU.
1386 inventoryPath = inventoryPaths[0];
Matt Spinler3bdd0112020-08-27 10:24:34 -05001387 }
1388 catch (const std::exception& e)
1389 {
Matt Spinler7b923722025-03-19 13:17:23 -05001390 addDebugData(std::format("Unable to get inventory path from "
1391 "location code: {}: {}",
1392 unexpandedLocCode, e.what()));
1393 addLocationCodeOnlyCallout(locCode, priority);
1394 return;
Matt Spinler3bdd0112020-08-27 10:24:34 -05001395 }
1396 }
1397
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001398 if (jsonCallout.contains("MRUs"))
1399 {
1400 mrus = getMRUsFromJSON(jsonCallout.at("MRUs"));
1401 }
1402
Matt Spinler3bdd0112020-08-27 10:24:34 -05001403 // If the location code was also passed in, use that here too
1404 // so addInventoryCallout doesn't have to look it up.
1405 std::optional<std::string> lc;
1406 if (!locCode.empty())
1407 {
1408 lc = locCode;
1409 }
1410
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001411 addInventoryCallout(inventoryPath, priority, lc, dataIface, mrus);
Matt Spinlerafa2c792020-08-27 11:01:39 -05001412
1413 if (jsonCallout.contains("Deconfigured"))
1414 {
1415 if (jsonCallout.at("Deconfigured").get<bool>())
1416 {
1417 setErrorStatusFlag(ErrorStatusFlags::deconfigured);
1418 }
1419 }
1420
1421 if (jsonCallout.contains("Guarded"))
1422 {
1423 if (jsonCallout.at("Guarded").get<bool>())
1424 {
1425 setErrorStatusFlag(ErrorStatusFlags::guarded);
1426 }
1427 }
Matt Spinler3bdd0112020-08-27 10:24:34 -05001428 }
1429
1430 if (callout)
1431 {
1432 createCalloutsObject();
1433 _callouts->addCallout(std::move(callout));
1434 }
1435}
1436
1437CalloutPriority SRC::getPriorityFromJSON(const nlohmann::json& json)
1438{
1439 // Looks like:
1440 // {
1441 // "Priority": "H"
1442 // }
1443 auto p = json.at("Priority").get<std::string>();
1444 if (p.empty())
1445 {
1446 throw std::runtime_error{"Priority field in callout is empty"};
1447 }
1448
1449 auto priority = static_cast<CalloutPriority>(p.front());
1450
1451 // Validate it
1452 auto priorityIt = pv::findByValue(static_cast<uint32_t>(priority),
1453 pv::calloutPriorityValues);
1454 if (priorityIt == pv::calloutPriorityValues.end())
1455 {
1456 throw std::runtime_error{
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001457 std::format("Invalid priority '{}' found in JSON callout", p)};
Matt Spinler3bdd0112020-08-27 10:24:34 -05001458 }
1459
1460 return priority;
Matt Spinler5a90a952020-08-27 09:39:03 -05001461}
1462
Patrick Williams25291152025-02-01 08:21:42 -05001463std::vector<src::MRU::MRUCallout> SRC::getMRUsFromJSON(
1464 const nlohmann::json& mruJSON)
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001465{
1466 std::vector<src::MRU::MRUCallout> mrus;
1467
1468 // Looks like:
1469 // [
1470 // {
1471 // "ID": 100,
1472 // "Priority": "H"
1473 // }
1474 // ]
1475 if (!mruJSON.is_array())
1476 {
1477 addDebugData("MRU callout JSON is not an array");
1478 return mrus;
1479 }
1480
1481 for (const auto& mruCallout : mruJSON)
1482 {
1483 try
1484 {
1485 auto priority = getPriorityFromJSON(mruCallout);
1486 auto id = mruCallout.at("ID").get<uint32_t>();
1487
1488 src::MRU::MRUCallout mru{static_cast<uint32_t>(priority), id};
1489 mrus.push_back(std::move(mru));
1490 }
1491 catch (const std::exception& e)
1492 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001493 addDebugData(std::format("Invalid MRU entry in JSON: {}: {}",
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001494 mruCallout.dump(), e.what()));
1495 }
1496 }
1497
1498 return mrus;
1499}
1500
Sumit Kumar3e274432021-09-14 06:37:56 -05001501std::vector<uint8_t> SRC::getSrcStruct()
1502{
1503 std::vector<uint8_t> data;
1504 Stream stream{data};
1505
1506 //------ Ref section 4.3 in PEL doc---
1507 //------ SRC Structure 40 bytes-------
1508 // Byte-0 | Byte-1 | Byte-2 | Byte-3 |
1509 // -----------------------------------
1510 // 02 | 08 | 00 | 09 | ==> Header
1511 // 00 | 00 | 00 | 48 | ==> Header
1512 // 00 | 00 | 00 | 00 | ==> Hex data word-2
1513 // 00 | 00 | 00 | 00 | ==> Hex data word-3
1514 // 00 | 00 | 00 | 00 | ==> Hex data word-4
1515 // 20 | 00 | 00 | 00 | ==> Hex data word-5
1516 // 00 | 00 | 00 | 00 | ==> Hex data word-6
1517 // 00 | 00 | 00 | 00 | ==> Hex data word-7
1518 // 00 | 00 | 00 | 00 | ==> Hex data word-8
1519 // 00 | 00 | 00 | 00 | ==> Hex data word-9
1520 // -----------------------------------
1521 // ASCII string - 8 bytes |
1522 // -----------------------------------
1523 // ASCII space NULL - 24 bytes |
1524 // -----------------------------------
1525 //_size = Base SRC struct: 8 byte header + hex data section + ASCII string
1526
1527 uint8_t flags = (_flags | postOPPanel);
1528
1529 stream << _version << flags << _reserved1B << _wordCount << _reserved2B
1530 << _size;
1531
1532 for (auto& word : _hexData)
1533 {
1534 stream << word;
1535 }
1536
1537 _asciiString->flatten(stream);
1538
1539 return data;
1540}
1541
Vijay Lobo875b6c72021-10-20 17:38:56 -05001542void SRC::setProgressCode(const DataInterfaceBase& dataIface)
1543{
1544 std::vector<uint8_t> progressSRC;
1545
1546 try
1547 {
1548 progressSRC = dataIface.getRawProgressSRC();
1549 }
1550 catch (const std::exception& e)
1551 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -05001552 lg2::error("Error getting progress code: {ERROR}", "ERROR", e);
Vijay Lobo875b6c72021-10-20 17:38:56 -05001553 return;
1554 }
1555
1556 _hexData[2] = getProgressCode(progressSRC);
1557}
1558
1559uint32_t SRC::getProgressCode(std::vector<uint8_t>& rawProgressSRC)
1560{
1561 uint32_t progressCode = 0;
1562
1563 // A valid progress SRC is at least 72 bytes
1564 if (rawProgressSRC.size() < 72)
1565 {
1566 return progressCode;
1567 }
1568
1569 try
1570 {
1571 // The ASCII string field in progress SRCs starts at offset 40.
1572 // Take the first 8 characters to put in the uint32:
1573 // "CC009189" -> 0xCC009189
1574 Stream stream{rawProgressSRC, 40};
1575 src::AsciiString aString{stream};
1576 auto progressCodeString = aString.get().substr(0, 8);
1577
1578 if (std::all_of(progressCodeString.begin(), progressCodeString.end(),
1579 [](char c) {
Patrick Williams075c7922024-08-16 15:19:49 -04001580 return std::isxdigit(static_cast<unsigned char>(c));
1581 }))
Vijay Lobo875b6c72021-10-20 17:38:56 -05001582 {
1583 progressCode = std::stoul(progressCodeString, nullptr, 16);
1584 }
1585 }
1586 catch (const std::exception& e)
1587 {}
1588
1589 return progressCode;
1590}
1591
Matt Spinlerf9bae182019-10-09 13:37:38 -05001592} // namespace pels
1593} // namespace openpower