blob: 6833441e1e072df79b4a5270bd8b16d65f8d21b3 [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 Spinler0bacc8e2023-07-07 16:25:39 -050029#include <phosphor-logging/lg2.hpp>
Matt Spinlerf9bae182019-10-09 13:37:38 -050030
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050031#include <format>
32
Matt Spinlerf9bae182019-10-09 13:37:38 -050033namespace 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 Spinler85f61a62020-06-03 16:28:55 -050039using namespace std::string_literals;
Matt Spinlerf9bae182019-10-09 13:37:38 -050040
Matt Spinler075e5ba2020-02-21 15:46:00 -060041constexpr size_t ccinSize = 4;
42
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080043#ifdef PELTOOL
Sumit Kumar516935a2021-04-14 13:00:54 -050044using orderedJSON = nlohmann::ordered_json;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080045
46void pyDecRef(PyObject* pyObj)
47{
48 Py_XDECREF(pyObj);
49}
50
51/**
52 * @brief Returns a JSON string to append to SRC section.
53 *
54 * The returning string will contain a JSON object, but without
55 * the outer {}. If the input JSON isn't a JSON object (dict), then
56 * one will be created with the input added to a 'SRC Details' key.
57 *
58 * @param[in] json - The JSON to convert to a string
59 *
60 * @return std::string - The JSON string
61 */
Sumit Kumar516935a2021-04-14 13:00:54 -050062std::string prettyJSON(const orderedJSON& json)
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080063{
Sumit Kumar516935a2021-04-14 13:00:54 -050064 orderedJSON output;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080065 if (!json.is_object())
66 {
67 output["SRC Details"] = json;
68 }
69 else
70 {
71 for (const auto& [key, value] : json.items())
72 {
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +080073 output["SRC Details"][key] = value;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080074 }
75 }
76
77 // Let nlohmann do the pretty printing.
78 std::stringstream stream;
79 stream << std::setw(4) << output;
80
81 auto jsonString = stream.str();
82
83 // Now it looks like:
84 // {
85 // "Key": "Value",
86 // ...
87 // }
88
89 // Replace the { and the following newline, and the } and its
90 // preceeding newline.
91 jsonString.erase(0, 2);
92
93 auto pos = jsonString.find_last_of('}');
94 jsonString.erase(pos - 1);
95
96 return jsonString;
97}
98
99/**
100 * @brief Call Python modules to parse the data into a JSON string
101 *
102 * The module to call is based on the Creator Subsystem ID under the namespace
103 * "srcparsers". For example: "srcparsers.xsrc.xsrc" where "x" is the Creator
104 * Subsystem ID in ASCII lowercase.
105 *
106 * All modules must provide the following:
107 * Function: parseSRCToJson
108 * Argument list:
109 * 1. (str) ASCII string (Hex Word 1)
110 * 2. (str) Hex Word 2
111 * 3. (str) Hex Word 3
112 * 4. (str) Hex Word 4
113 * 5. (str) Hex Word 5
114 * 6. (str) Hex Word 6
115 * 7. (str) Hex Word 7
116 * 8. (str) Hex Word 8
117 * 9. (str) Hex Word 9
118 *-Return data:
119 * 1. (str) JSON string
120 *
121 * @param[in] hexwords - Vector of strings of Hexwords 1-9
122 * @param[in] creatorID - The creatorID from the Private Header section
123 * @return std::optional<std::string> - The JSON string if it could be created,
124 * else std::nullopt
125 */
126std::optional<std::string> getPythonJSON(std::vector<std::string>& hexwords,
127 uint8_t creatorID)
128{
Matt Spinlerbe952d22022-07-01 11:30:11 -0500129 PyObject *pName, *pModule, *eType, *eValue, *eTraceback;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800130 std::string pErrStr;
131 std::string module = getNumberString("%c", tolower(creatorID)) + "src";
132 pName = PyUnicode_FromString(
133 std::string("srcparsers." + module + "." + module).c_str());
134 std::unique_ptr<PyObject, decltype(&pyDecRef)> modNamePtr(pName, &pyDecRef);
135 pModule = PyImport_Import(pName);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800136 if (pModule == NULL)
137 {
138 pErrStr = "No error string found";
139 PyErr_Fetch(&eType, &eValue, &eTraceback);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800140 if (eType)
141 {
142 Py_XDECREF(eType);
143 }
144 if (eTraceback)
145 {
146 Py_XDECREF(eTraceback);
147 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800148 if (eValue)
149 {
150 PyObject* pStr = PyObject_Str(eValue);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800151 Py_XDECREF(eValue);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800152 if (pStr)
153 {
154 pErrStr = PyUnicode_AsUTF8(pStr);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800155 Py_XDECREF(pStr);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800156 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800157 }
158 }
159 else
160 {
Patrick Williams075c7922024-08-16 15:19:49 -0400161 std::unique_ptr<PyObject, decltype(&pyDecRef)> modPtr(
162 pModule, &pyDecRef);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800163 std::string funcToCall = "parseSRCToJson";
Matt Spinlerbe952d22022-07-01 11:30:11 -0500164 PyObject* pKey = PyUnicode_FromString(funcToCall.c_str());
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800165 std::unique_ptr<PyObject, decltype(&pyDecRef)> keyPtr(pKey, &pyDecRef);
Matt Spinlerbe952d22022-07-01 11:30:11 -0500166 PyObject* pDict = PyModule_GetDict(pModule);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800167 Py_INCREF(pDict);
168 if (!PyDict_Contains(pDict, pKey))
169 {
170 Py_DECREF(pDict);
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500171 lg2::error(
172 "Python module error. Function missing: {FUNC}, SRC = {SRC}, module = {MODULE}",
173 "FUNC", funcToCall, "SRC", hexwords.front(), "MODULE", module);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800174 return std::nullopt;
175 }
Matt Spinlerbe952d22022-07-01 11:30:11 -0500176 PyObject* pFunc = PyDict_GetItemString(pDict, funcToCall.c_str());
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800177 Py_DECREF(pDict);
178 Py_INCREF(pFunc);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800179 if (PyCallable_Check(pFunc))
180 {
Matt Spinlerbe952d22022-07-01 11:30:11 -0500181 PyObject* pArgs = PyTuple_New(9);
Patrick Williams075c7922024-08-16 15:19:49 -0400182 std::unique_ptr<PyObject, decltype(&pyDecRef)> argPtr(
183 pArgs, &pyDecRef);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800184 for (size_t i = 0; i < 9; i++)
185 {
Matt Spinlerc1984032023-01-05 10:09:59 -0600186 std::string arg{"00000000"};
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800187 if (i < hexwords.size())
188 {
Matt Spinlerc1984032023-01-05 10:09:59 -0600189 arg = hexwords[i];
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800190 }
Matt Spinlerc1984032023-01-05 10:09:59 -0600191 PyTuple_SetItem(pArgs, i, Py_BuildValue("s", arg.c_str()));
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800192 }
Matt Spinlerbe952d22022-07-01 11:30:11 -0500193 PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800194 Py_DECREF(pFunc);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800195 if (pResult)
196 {
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800197 std::unique_ptr<PyObject, decltype(&pyDecRef)> resPtr(
198 pResult, &pyDecRef);
Matt Spinler91f6d3a2025-05-22 08:32:49 -0500199
200 if (pResult == Py_None)
201 {
202 return std::nullopt;
203 }
204
Patrick Williams075c7922024-08-16 15:19:49 -0400205 PyObject* pBytes =
206 PyUnicode_AsEncodedString(pResult, "utf-8", "~E~");
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800207 std::unique_ptr<PyObject, decltype(&pyDecRef)> pyBytePtr(
208 pBytes, &pyDecRef);
209 const char* output = PyBytes_AS_STRING(pBytes);
210 try
211 {
Matt Spinlerbb1c1d52021-06-03 13:18:48 -0600212 orderedJSON json = orderedJSON::parse(output);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800213 if ((json.is_object() && !json.empty()) ||
214 (json.is_array() && json.size() > 0) ||
215 (json.is_string() && json != ""))
216 {
217 return prettyJSON(json);
218 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800219 }
Patrick Williams66491c62021-10-06 12:23:37 -0500220 catch (const std::exception& e)
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800221 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500222 lg2::error(
223 "Bad JSON from parser. Error = {ERROR}, SRC = {SRC}, module = {MODULE}",
224 "ERROR", e, "SRC", hexwords.front(), "MODULE", module);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800225 return std::nullopt;
226 }
227 }
228 else
229 {
230 pErrStr = "No error string found";
231 PyErr_Fetch(&eType, &eValue, &eTraceback);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800232 if (eType)
233 {
234 Py_XDECREF(eType);
235 }
236 if (eTraceback)
237 {
238 Py_XDECREF(eTraceback);
239 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800240 if (eValue)
241 {
242 PyObject* pStr = PyObject_Str(eValue);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800243 Py_XDECREF(eValue);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800244 if (pStr)
245 {
246 pErrStr = PyUnicode_AsUTF8(pStr);
Harisuddin Mohamed Isa69c18272021-05-29 13:18:45 +0800247 Py_XDECREF(pStr);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800248 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800249 }
250 }
251 }
252 }
253 if (!pErrStr.empty())
254 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500255 lg2::debug("Python exception thrown by parser. Error = {ERROR}, "
256 "SRC = {SRC}, module = {MODULE}",
257 "ERROR", pErrStr, "SRC", hexwords.front(), "MODULE", module);
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800258 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800259 return std::nullopt;
260}
261#endif
262
Matt Spinlerf9bae182019-10-09 13:37:38 -0500263void SRC::unflatten(Stream& stream)
264{
265 stream >> _header >> _version >> _flags >> _reserved1B >> _wordCount >>
266 _reserved2B >> _size;
267
268 for (auto& word : _hexData)
269 {
270 stream >> word;
271 }
272
273 _asciiString = std::make_unique<src::AsciiString>(stream);
274
275 if (hasAdditionalSections())
276 {
277 // The callouts section is currently the only extra subsection type
278 _callouts = std::make_unique<src::Callouts>(stream);
279 }
280}
281
Matt Spinler06885452019-11-06 10:35:42 -0600282void SRC::flatten(Stream& stream) const
Matt Spinlerf9bae182019-10-09 13:37:38 -0500283{
284 stream << _header << _version << _flags << _reserved1B << _wordCount
285 << _reserved2B << _size;
286
287 for (auto& word : _hexData)
288 {
289 stream << word;
290 }
291
292 _asciiString->flatten(stream);
293
294 if (_callouts)
295 {
296 _callouts->flatten(stream);
297 }
298}
299
300SRC::SRC(Stream& pel)
301{
302 try
303 {
304 unflatten(pel);
305 validate();
306 }
307 catch (const std::exception& e)
308 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500309 lg2::error("Cannot unflatten SRC, error = {ERROR}", "ERROR", e);
Matt Spinlerf9bae182019-10-09 13:37:38 -0500310 _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);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500347 setBMCFormat();
348 setBMCPosition();
Matt Spinler075e5ba2020-02-21 15:46:00 -0600349 setMotherboardCCIN(dataIface);
350
Matt Spinlerda5b76b2023-06-01 15:56:57 -0500351 if (regEntry.src.checkstopFlag)
352 {
353 setErrorStatusFlag(ErrorStatusFlags::hwCheckstop);
354 }
355
Matt Spinler3fe93e92023-04-14 14:06:59 -0500356 if (regEntry.src.deconfigFlag)
357 {
358 setErrorStatusFlag(ErrorStatusFlags::deconfigured);
359 }
360
Matt Spinlerbd716f02019-10-15 10:54:11 -0500361 // Fill in the last 4 words from the AdditionalData property contents.
362 setUserDefinedHexWords(regEntry, additionalData);
363
364 _asciiString = std::make_unique<src::AsciiString>(regEntry);
365
Sumit Kumar50bfa692022-01-06 06:48:26 -0600366 // Check for additional data - PEL_SUBSYSTEM
367 auto ss = additionalData.getValue("PEL_SUBSYSTEM");
368 if (ss)
369 {
Matt Spinler6f07df32025-05-09 11:42:39 -0500370 auto eventSubsystem = std::stoul(*ss, nullptr, 16);
Patrick Williams075c7922024-08-16 15:19:49 -0400371 std::string subsystem =
372 pv::getValue(eventSubsystem, pel_values::subsystemValues);
Sumit Kumar50bfa692022-01-06 06:48:26 -0600373 if (subsystem == "invalid")
374 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500375 lg2::warning("SRC: Invalid SubSystem value: {VAL}", "VAL", lg2::hex,
376 eventSubsystem);
Sumit Kumar50bfa692022-01-06 06:48:26 -0600377 }
378 else
379 {
380 _asciiString->setByte(2, eventSubsystem);
381 }
382 }
383
Matt Spinler5a90a952020-08-27 09:39:03 -0500384 addCallouts(regEntry, additionalData, jsonCallouts, dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500385
386 _size = baseSRCSize;
387 _size += _callouts ? _callouts->flattenedSize() : 0;
Matt Spinlere2eb14a2025-05-09 13:34:51 -0500388 _header.size = Section::headerSize() + _size;
Matt Spinlerbd716f02019-10-15 10:54:11 -0500389
390 _valid = true;
391}
392
393void SRC::setUserDefinedHexWords(const message::Entry& regEntry,
394 const AdditionalData& ad)
395{
396 if (!regEntry.src.hexwordADFields)
397 {
398 return;
399 }
400
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800401 // Save the AdditionalData value corresponding to the first element of
402 // adName tuple into _hexData[wordNum].
Matt Spinlerbd716f02019-10-15 10:54:11 -0500403 for (const auto& [wordNum, adName] : *regEntry.src.hexwordADFields)
404 {
405 // Can only set words 6 - 9
406 if (!isUserDefinedWord(wordNum))
407 {
Patrick Williams075c7922024-08-16 15:19:49 -0400408 std::string msg =
409 "SRC user data word out of range: " + std::to_string(wordNum);
Matt Spinler85f61a62020-06-03 16:28:55 -0500410 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500411 continue;
412 }
413
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800414 auto value = ad.getValue(std::get<0>(adName));
Matt Spinlerbd716f02019-10-15 10:54:11 -0500415 if (value)
416 {
417 _hexData[getWordIndexFromWordNum(wordNum)] =
418 std::strtoul(value.value().c_str(), nullptr, 0);
419 }
420 else
421 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800422 std::string msg = "Source for user data SRC word not found: " +
423 std::get<0>(adName);
Matt Spinler85f61a62020-06-03 16:28:55 -0500424 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500425 }
426 }
427}
428
Matt Spinler075e5ba2020-02-21 15:46:00 -0600429void SRC::setMotherboardCCIN(const DataInterfaceBase& dataIface)
430{
431 uint32_t ccin = 0;
432 auto ccinString = dataIface.getMotherboardCCIN();
433
434 try
435 {
436 if (ccinString.size() == ccinSize)
437 {
Matt Spinler6f07df32025-05-09 11:42:39 -0500438 ccin = std::stoi(ccinString, nullptr, 16);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600439 }
440 }
Patrick Williams66491c62021-10-06 12:23:37 -0500441 catch (const std::exception& e)
Matt Spinler075e5ba2020-02-21 15:46:00 -0600442 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500443 lg2::warning("Could not convert motherboard CCIN {CCIN} to a number",
444 "CCIN", ccinString);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600445 return;
446 }
447
448 // Set the first 2 bytes
449 _hexData[1] |= ccin << 16;
450}
451
Matt Spinlerf9bae182019-10-09 13:37:38 -0500452void SRC::validate()
453{
454 bool failed = false;
455
456 if ((header().id != static_cast<uint16_t>(SectionID::primarySRC)) &&
457 (header().id != static_cast<uint16_t>(SectionID::secondarySRC)))
458 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500459 lg2::error("Invalid SRC section ID: {ID}", "ID", lg2::hex, header().id);
Matt Spinlerf9bae182019-10-09 13:37:38 -0500460 failed = true;
461 }
462
463 // Check the version in the SRC, not in the header
Matt Spinlerbd716f02019-10-15 10:54:11 -0500464 if (_version != srcVersion)
Matt Spinlerf9bae182019-10-09 13:37:38 -0500465 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500466 lg2::error("Invalid SRC version: {VERSION}", "VERSION", lg2::hex,
467 header().version);
Matt Spinlerf9bae182019-10-09 13:37:38 -0500468 failed = true;
469 }
470
471 _valid = failed ? false : true;
472}
473
Matt Spinler075e5ba2020-02-21 15:46:00 -0600474bool SRC::isBMCSRC() const
475{
476 auto as = asciiString();
477 if (as.length() >= 2)
478 {
479 uint8_t errorType = strtoul(as.substr(0, 2).c_str(), nullptr, 16);
480 return (errorType == static_cast<uint8_t>(SRCType::bmcError) ||
481 errorType == static_cast<uint8_t>(SRCType::powerError));
482 }
483 return false;
484}
485
Matt Spinler4deed972023-04-28 14:09:22 -0500486bool SRC::isHostbootSRC() const
487{
488 auto as = asciiString();
489 if (as.length() >= 2)
490 {
491 uint8_t errorType = strtoul(as.substr(0, 2).c_str(), nullptr, 16);
492 return errorType == static_cast<uint8_t>(SRCType::hostbootError);
493 }
494 return false;
495}
496
Patrick Williams075c7922024-08-16 15:19:49 -0400497std::optional<std::string> SRC::getErrorDetails(
498 message::Registry& registry, DetailLevel type, bool toCache) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800499{
500 const std::string jsonIndent(indentLevel, 0x20);
501 std::string errorOut;
Matt Spinler075e5ba2020-02-21 15:46:00 -0600502 if (isBMCSRC())
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800503 {
504 auto entry = registry.lookup("0x" + asciiString().substr(4, 4),
505 rg::LookupType::reasonCode, toCache);
506 if (entry)
507 {
508 errorOut.append(jsonIndent + "\"Error Details\": {\n");
509 auto errorMsg = getErrorMessage(*entry);
510 if (errorMsg)
511 {
512 if (type == DetailLevel::message)
513 {
514 return errorMsg.value();
515 }
516 else
517 {
518 jsonInsert(errorOut, "Message", errorMsg.value(), 2);
519 }
520 }
521 if (entry->src.hexwordADFields)
522 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800523 std::map<size_t, std::tuple<std::string, std::string>>
524 adFields = entry->src.hexwordADFields.value();
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800525 for (const auto& hexwordMap : adFields)
526 {
Zane Shelleye8db29b2021-11-13 10:34:07 -0600527 auto srcValue = getNumberString(
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800528 "0x%X",
Zane Shelleye8db29b2021-11-13 10:34:07 -0600529 _hexData[getWordIndexFromWordNum(hexwordMap.first)]);
530
531 auto srcKey = std::get<0>(hexwordMap.second);
532 auto srcDesc = std::get<1>(hexwordMap.second);
533
534 // Only include this hex word in the error details if the
535 // description exists.
536 if (!srcDesc.empty())
537 {
538 std::vector<std::string> valueDescr;
539 valueDescr.push_back(srcValue);
540 valueDescr.push_back(srcDesc);
541 jsonInsertArray(errorOut, srcKey, valueDescr, 2);
542 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800543 }
544 }
545 errorOut.erase(errorOut.size() - 2);
546 errorOut.append("\n");
547 errorOut.append(jsonIndent + "},\n");
548 return errorOut;
549 }
550 }
551 return std::nullopt;
552}
553
Patrick Williams25291152025-02-01 08:21:42 -0500554std::optional<std::string> SRC::getErrorMessage(
555 const message::Entry& regEntry) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800556{
557 try
558 {
559 if (regEntry.doc.messageArgSources)
560 {
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800561 std::vector<uint32_t> argSourceVals;
562 std::string message;
563 const auto& argValues = regEntry.doc.messageArgSources.value();
564 for (size_t i = 0; i < argValues.size(); ++i)
565 {
566 argSourceVals.push_back(_hexData[getWordIndexFromWordNum(
567 argValues[i].back() - '0')]);
568 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500569
570 auto it = std::begin(regEntry.doc.message);
571 auto it_end = std::end(regEntry.doc.message);
572
573 while (it != it_end)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800574 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500575 if (*it == '%')
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800576 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500577 ++it;
578
579 size_t wordIndex = *it - '0';
580 if (isdigit(*it) && wordIndex >= 1 &&
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800581 static_cast<uint16_t>(wordIndex) <=
582 argSourceVals.size())
583 {
584 message.append(getNumberString(
Zane Shelley39936e32021-11-13 16:19:34 -0600585 "0x%08X", argSourceVals[wordIndex - 1]));
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800586 }
587 else
588 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500589 message.append("%" + std::string(1, *it));
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800590 }
591 }
592 else
593 {
Patrick Williams0230abb2021-04-19 14:32:50 -0500594 message.push_back(*it);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800595 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500596 ++it;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800597 }
Patrick Williams0230abb2021-04-19 14:32:50 -0500598
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800599 return message;
600 }
601 else
602 {
603 return regEntry.doc.message;
604 }
605 }
606 catch (const std::exception& e)
607 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -0500608 lg2::error(
609 "Cannot get error message from registry entry, error = {ERROR}",
610 "ERROR", e);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800611 }
612 return std::nullopt;
613}
614
615std::optional<std::string> SRC::getCallouts() const
616{
617 if (!_callouts)
618 {
619 return std::nullopt;
620 }
621 std::string printOut;
622 const std::string jsonIndent(indentLevel, 0x20);
623 const auto& callout = _callouts->callouts();
624 const auto& compDescrp = pv::failingComponentType;
625 printOut.append(jsonIndent + "\"Callout Section\": {\n");
626 jsonInsert(printOut, "Callout Count", std::to_string(callout.size()), 2);
627 printOut.append(jsonIndent + jsonIndent + "\"Callouts\": [");
628 for (auto& entry : callout)
629 {
630 printOut.append("{\n");
631 if (entry->fruIdentity())
632 {
633 jsonInsert(
634 printOut, "FRU Type",
635 compDescrp.at(entry->fruIdentity()->failingComponentType()), 3);
636 jsonInsert(printOut, "Priority",
637 pv::getValue(entry->priority(),
638 pel_values::calloutPriorityValues),
639 3);
640 if (!entry->locationCode().empty())
641 {
642 jsonInsert(printOut, "Location Code", entry->locationCode(), 3);
643 }
644 if (entry->fruIdentity()->getPN().has_value())
645 {
646 jsonInsert(printOut, "Part Number",
647 entry->fruIdentity()->getPN().value(), 3);
648 }
649 if (entry->fruIdentity()->getMaintProc().has_value())
650 {
Matt Spinler9e8b49e2020-09-10 13:15:26 -0500651 jsonInsert(printOut, "Procedure",
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800652 entry->fruIdentity()->getMaintProc().value(), 3);
653 if (pv::procedureDesc.find(
654 entry->fruIdentity()->getMaintProc().value()) !=
655 pv::procedureDesc.end())
656 {
657 jsonInsert(
658 printOut, "Description",
659 pv::procedureDesc.at(
660 entry->fruIdentity()->getMaintProc().value()),
661 3);
662 }
663 }
664 if (entry->fruIdentity()->getCCIN().has_value())
665 {
666 jsonInsert(printOut, "CCIN",
667 entry->fruIdentity()->getCCIN().value(), 3);
668 }
669 if (entry->fruIdentity()->getSN().has_value())
670 {
671 jsonInsert(printOut, "Serial Number",
672 entry->fruIdentity()->getSN().value(), 3);
673 }
674 }
675 if (entry->pceIdentity())
676 {
677 const auto& pceIdentMtms = entry->pceIdentity()->mtms();
678 if (!pceIdentMtms.machineTypeAndModel().empty())
679 {
680 jsonInsert(printOut, "PCE MTMS",
681 pceIdentMtms.machineTypeAndModel() + "_" +
682 pceIdentMtms.machineSerialNumber(),
683 3);
684 }
685 if (!entry->pceIdentity()->enclosureName().empty())
686 {
687 jsonInsert(printOut, "PCE Name",
688 entry->pceIdentity()->enclosureName(), 3);
689 }
690 }
691 if (entry->mru())
692 {
693 const auto& mruCallouts = entry->mru()->mrus();
694 std::string mruId;
695 for (auto& element : mruCallouts)
696 {
697 if (!mruId.empty())
698 {
699 mruId.append(", " + getNumberString("%08X", element.id));
700 }
701 else
702 {
703 mruId.append(getNumberString("%08X", element.id));
704 }
705 }
706 jsonInsert(printOut, "MRU Id", mruId, 3);
707 }
708 printOut.erase(printOut.size() - 2);
709 printOut.append("\n" + jsonIndent + jsonIndent + "}, ");
710 };
711 printOut.erase(printOut.size() - 2);
712 printOut.append("]\n" + jsonIndent + "}");
713 return printOut;
714}
715
Patrick Williams25291152025-02-01 08:21:42 -0500716std::optional<std::string> SRC::getJSON(message::Registry& registry,
717 const std::vector<std::string>& plugins
718 [[maybe_unused]],
719 uint8_t creatorID) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800720{
721 std::string ps;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800722 std::vector<std::string> hexwords;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800723 jsonInsert(ps, pv::sectionVer, getNumberString("%d", _header.version), 1);
724 jsonInsert(ps, pv::subSection, getNumberString("%d", _header.subType), 1);
Matt Spinlerb832aa52023-03-21 15:32:34 -0500725 jsonInsert(ps, pv::createdBy,
726 getComponentName(_header.componentID, creatorID), 1);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800727 jsonInsert(ps, "SRC Version", getNumberString("0x%02X", _version), 1);
Harisuddin Mohamed Isac32e5512020-02-06 18:05:21 +0800728 jsonInsert(ps, "SRC Format", getNumberString("0x%02X", _hexData[0] & 0xFF),
729 1);
730 jsonInsert(ps, "Virtual Progress SRC",
731 pv::boolString.at(_flags & virtualProgressSRC), 1);
732 jsonInsert(ps, "I5/OS Service Event Bit",
733 pv::boolString.at(_flags & i5OSServiceEventBit), 1);
734 jsonInsert(ps, "Hypervisor Dump Initiated",
735 pv::boolString.at(_flags & hypDumpInit), 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600736
737 if (isBMCSRC())
738 {
739 std::string ccinString;
740 uint32_t ccin = _hexData[1] >> 16;
741
742 if (ccin)
743 {
744 ccinString = getNumberString("%04X", ccin);
745 }
746 // The PEL spec calls it a backplane, so call it that here.
747 jsonInsert(ps, "Backplane CCIN", ccinString, 1);
Matt Spinlerafa2c792020-08-27 11:01:39 -0500748
Sumit Kumar3e274432021-09-14 06:37:56 -0500749 jsonInsert(ps, "Terminate FW Error",
750 pv::boolString.at(
751 _hexData[3] &
752 static_cast<uint32_t>(ErrorStatusFlags::terminateFwErr)),
753 1);
Matt Spinler4deed972023-04-28 14:09:22 -0500754 }
755
756 if (isBMCSRC() || isHostbootSRC())
757 {
Matt Spinlerafa2c792020-08-27 11:01:39 -0500758 jsonInsert(ps, "Deconfigured",
759 pv::boolString.at(
760 _hexData[3] &
761 static_cast<uint32_t>(ErrorStatusFlags::deconfigured)),
762 1);
763
764 jsonInsert(
765 ps, "Guarded",
Patrick Williams075c7922024-08-16 15:19:49 -0400766 pv::boolString.at(
767 _hexData[3] & static_cast<uint32_t>(ErrorStatusFlags::guarded)),
Matt Spinlerafa2c792020-08-27 11:01:39 -0500768 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600769 }
770
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800771 auto errorDetails = getErrorDetails(registry, DetailLevel::json, true);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800772 if (errorDetails)
773 {
774 ps.append(errorDetails.value());
775 }
776 jsonInsert(ps, "Valid Word Count", getNumberString("0x%02X", _wordCount),
777 1);
778 std::string refcode = asciiString();
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800779 hexwords.push_back(refcode);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800780 std::string extRefcode;
781 size_t pos = refcode.find(0x20);
782 if (pos != std::string::npos)
783 {
784 size_t nextPos = refcode.find_first_not_of(0x20, pos);
785 if (nextPos != std::string::npos)
786 {
787 extRefcode = trimEnd(refcode.substr(nextPos));
788 }
789 refcode.erase(pos);
790 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800791 jsonInsert(ps, "Reference Code", refcode, 1);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800792 if (!extRefcode.empty())
793 {
794 jsonInsert(ps, "Extended Reference Code", extRefcode, 1);
795 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800796 for (size_t i = 2; i <= _wordCount; i++)
797 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800798 std::string tmpWord =
799 getNumberString("%08X", _hexData[getWordIndexFromWordNum(i)]);
800 jsonInsert(ps, "Hex Word " + std::to_string(i), tmpWord, 1);
801 hexwords.push_back(tmpWord);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800802 }
803 auto calloutJson = getCallouts();
804 if (calloutJson)
805 {
806 ps.append(calloutJson.value());
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800807 ps.append(",\n");
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800808 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800809 std::string subsystem = getNumberString("%c", tolower(creatorID));
810 bool srcDetailExists = false;
811#ifdef PELTOOL
812 if (std::find(plugins.begin(), plugins.end(), subsystem + "src") !=
813 plugins.end())
814 {
815 auto pyJson = getPythonJSON(hexwords, creatorID);
816 if (pyJson)
817 {
818 ps.append(pyJson.value());
819 srcDetailExists = true;
820 }
821 }
822#endif
823 if (!srcDetailExists)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800824 {
825 ps.erase(ps.size() - 2);
826 }
827 return ps;
828}
829
Matt Spinler03984582020-04-09 13:17:58 -0500830void SRC::addCallouts(const message::Entry& regEntry,
831 const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500832 const nlohmann::json& jsonCallouts,
Matt Spinlered046852020-03-13 13:58:15 -0500833 const DataInterfaceBase& dataIface)
834{
Patrick Williams075c7922024-08-16 15:19:49 -0400835 auto registryCallouts =
836 getRegistryCallouts(regEntry, additionalData, dataIface);
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500837
Matt Spinlered046852020-03-13 13:58:15 -0500838 auto item = additionalData.getValue("CALLOUT_INVENTORY_PATH");
Miguel Gomez53ef1552020-10-14 21:16:32 +0000839 auto priority = additionalData.getValue("CALLOUT_PRIORITY");
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500840
Miguel Gomez53ef1552020-10-14 21:16:32 +0000841 std::optional<CalloutPriority> calloutPriority;
842
843 // Only H, M or L priority values.
844 if (priority && !(*priority).empty())
845 {
846 uint8_t p = (*priority)[0];
847 if (p == 'H' || p == 'M' || p == 'L')
848 {
849 calloutPriority = static_cast<CalloutPriority>(p);
850 }
851 }
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500852 // If the first registry callout says to use the passed in inventory
853 // path to get the location code for a symbolic FRU callout with a
854 // trusted location code, then do not add the inventory path as a
855 // normal FRU callout.
856 bool useInvForSymbolicFRULocCode =
857 !registryCallouts.empty() && registryCallouts[0].useInventoryLocCode &&
858 !registryCallouts[0].symbolicFRUTrusted.empty();
859
860 if (item && !useInvForSymbolicFRULocCode)
Matt Spinlered046852020-03-13 13:58:15 -0500861 {
Miguel Gomez53ef1552020-10-14 21:16:32 +0000862 addInventoryCallout(*item, calloutPriority, std::nullopt, dataIface);
Matt Spinlered046852020-03-13 13:58:15 -0500863 }
864
Matt Spinler717de422020-06-04 13:10:14 -0500865 addDevicePathCallouts(additionalData, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -0500866
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500867 addRegistryCallouts(registryCallouts, dataIface,
868 (useInvForSymbolicFRULocCode) ? item : std::nullopt);
Matt Spinler5a90a952020-08-27 09:39:03 -0500869
870 if (!jsonCallouts.empty())
871 {
872 addJSONCallouts(jsonCallouts, dataIface);
873 }
Matt Spinlered046852020-03-13 13:58:15 -0500874}
875
Matt Spinler7b923722025-03-19 13:17:23 -0500876void SRC::addLocationCodeOnlyCallout(const std::string& locationCode,
877 const CalloutPriority priority)
878{
879 std::string empty;
880 std::vector<src::MRU::MRUCallout> mrus;
881 auto callout = std::make_unique<src::Callout>(priority, locationCode, empty,
882 empty, empty, mrus);
883 createCalloutsObject();
884 _callouts->addCallout(std::move(callout));
885}
886
Matt Spinlered046852020-03-13 13:58:15 -0500887void SRC::addInventoryCallout(const std::string& inventoryPath,
Matt Spinleraf191c72020-06-04 11:35:13 -0500888 const std::optional<CalloutPriority>& priority,
889 const std::optional<std::string>& locationCode,
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500890 const DataInterfaceBase& dataIface,
891 const std::vector<src::MRU::MRUCallout>& mrus)
Matt Spinlered046852020-03-13 13:58:15 -0500892{
893 std::string locCode;
894 std::string fn;
895 std::string ccin;
896 std::string sn;
897 std::unique_ptr<src::Callout> callout;
898
Matt Spinlered046852020-03-13 13:58:15 -0500899 try
900 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500901 // Use the passed in location code if there otherwise look it up
902 if (locationCode)
903 {
904 locCode = *locationCode;
905 }
906 else
907 {
908 locCode = dataIface.getLocationCode(inventoryPath);
909 }
Matt Spinlered046852020-03-13 13:58:15 -0500910
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500911 try
912 {
913 dataIface.getHWCalloutFields(inventoryPath, fn, ccin, sn);
914
Patrick Williams075c7922024-08-16 15:19:49 -0400915 CalloutPriority p =
916 priority ? priority.value() : CalloutPriority::high;
Matt Spinleraf191c72020-06-04 11:35:13 -0500917
Patrick Williams075c7922024-08-16 15:19:49 -0400918 callout =
919 std::make_unique<src::Callout>(p, locCode, fn, ccin, sn, mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500920 }
Patrick Williams45e83522022-07-22 19:26:52 -0500921 catch (const sdbusplus::exception_t& e)
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500922 {
Patrick Williams075c7922024-08-16 15:19:49 -0400923 std::string msg =
924 "No VPD found for " + inventoryPath + ": " + e.what();
Matt Spinler85f61a62020-06-03 16:28:55 -0500925 addDebugData(msg);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500926
927 // Just create the callout with empty FRU fields
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500928 callout = std::make_unique<src::Callout>(
929 CalloutPriority::high, locCode, fn, ccin, sn, mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500930 }
Matt Spinlered046852020-03-13 13:58:15 -0500931 }
Patrick Williams45e83522022-07-22 19:26:52 -0500932 catch (const sdbusplus::exception_t& e)
Matt Spinlered046852020-03-13 13:58:15 -0500933 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500934 std::string msg = "Could not get location code for " + inventoryPath +
935 ": " + e.what();
936 addDebugData(msg);
Matt Spinlered046852020-03-13 13:58:15 -0500937
Matt Spinler479b6922021-08-17 16:34:59 -0500938 // Don't add a callout in this case, because:
939 // 1) With how the inventory is primed, there is no case where
940 // a location code is expected to be missing. This implies
941 // the caller is passing in something invalid.
942 // 2) The addDebugData call above will put the passed in path into
943 // a user data section that can be seen by development for debug.
944 // 3) Even if we wanted to do a 'no_vpd_for_fru' sort of maint.
945 // procedure, we don't have a good way to indicate to the user
946 // anything about the intended callout (they won't see user data).
947 // 4) Creating a new standalone event log for this problem isn't
948 // possible from inside a PEL section.
Matt Spinlered046852020-03-13 13:58:15 -0500949 }
950
Matt Spinler479b6922021-08-17 16:34:59 -0500951 if (callout)
952 {
953 createCalloutsObject();
954 _callouts->addCallout(std::move(callout));
955 }
Matt Spinler03984582020-04-09 13:17:58 -0500956}
Matt Spinlered046852020-03-13 13:58:15 -0500957
Patrick Williams075c7922024-08-16 15:19:49 -0400958std::vector<message::RegistryCallout> SRC::getRegistryCallouts(
959 const message::Entry& regEntry, const AdditionalData& additionalData,
960 const DataInterfaceBase& dataIface)
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500961{
962 std::vector<message::RegistryCallout> registryCallouts;
963
964 if (regEntry.callouts)
965 {
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500966 std::vector<std::string> systemNames;
967
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500968 try
969 {
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500970 systemNames = dataIface.getSystemNames();
971 }
972 catch (const std::exception& e)
973 {
974 // Compatible interface not available yet
975 }
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500976
Matt Spinler9a50c8d2021-04-12 14:22:26 -0500977 try
978 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500979 registryCallouts = message::Registry::getCallouts(
980 regEntry.callouts.value(), systemNames, additionalData);
981 }
982 catch (const std::exception& e)
983 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -0500984 addDebugData(std::format(
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500985 "Error parsing PEL message registry callout JSON: {}",
986 e.what()));
987 }
988 }
989
990 return registryCallouts;
991}
992
993void SRC::addRegistryCallouts(
994 const std::vector<message::RegistryCallout>& callouts,
995 const DataInterfaceBase& dataIface,
996 std::optional<std::string> trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -0500997{
998 try
999 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001000 for (const auto& callout : callouts)
Matt Spinler03984582020-04-09 13:17:58 -05001001 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001002 addRegistryCallout(callout, dataIface, trustedSymbolicFRUInvPath);
1003
1004 // Only the first callout gets the inventory path
1005 if (trustedSymbolicFRUInvPath)
1006 {
1007 trustedSymbolicFRUInvPath = std::nullopt;
1008 }
Matt Spinler03984582020-04-09 13:17:58 -05001009 }
1010 }
Patrick Williams66491c62021-10-06 12:23:37 -05001011 catch (const std::exception& e)
Matt Spinler03984582020-04-09 13:17:58 -05001012 {
Patrick Williams075c7922024-08-16 15:19:49 -04001013 std::string msg =
1014 "Error parsing PEL message registry callout JSON: "s + e.what();
Matt Spinler85f61a62020-06-03 16:28:55 -05001015 addDebugData(msg);
Matt Spinler03984582020-04-09 13:17:58 -05001016 }
1017}
1018
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001019void SRC::addRegistryCallout(
1020 const message::RegistryCallout& regCallout,
1021 const DataInterfaceBase& dataIface,
1022 const std::optional<std::string>& trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -05001023{
1024 std::unique_ptr<src::Callout> callout;
Matt Spinler03984582020-04-09 13:17:58 -05001025 auto locCode = regCallout.locCode;
Matt Spinler7b923722025-03-19 13:17:23 -05001026 bool locExpanded = true;
Matt Spinler03984582020-04-09 13:17:58 -05001027
Matt Spinleraf191c72020-06-04 11:35:13 -05001028 if (!locCode.empty())
1029 {
1030 try
1031 {
1032 locCode = dataIface.expandLocationCode(locCode, 0);
1033 }
1034 catch (const std::exception& e)
1035 {
Patrick Williams2544b412022-10-04 08:41:06 -05001036 auto msg = "Unable to expand location code " + locCode + ": " +
1037 e.what();
Matt Spinleraf191c72020-06-04 11:35:13 -05001038 addDebugData(msg);
Matt Spinler7b923722025-03-19 13:17:23 -05001039 locExpanded = false;
Matt Spinleraf191c72020-06-04 11:35:13 -05001040 }
1041 }
1042
Matt Spinler03984582020-04-09 13:17:58 -05001043 // Via the PEL values table, get the priority enum.
1044 // The schema will have validated the priority was a valid value.
Patrick Williams075c7922024-08-16 15:19:49 -04001045 auto priorityIt =
1046 pv::findByName(regCallout.priority, pv::calloutPriorityValues);
Matt Spinler03984582020-04-09 13:17:58 -05001047 assert(priorityIt != pv::calloutPriorityValues.end());
1048 auto priority =
1049 static_cast<CalloutPriority>(std::get<pv::fieldValuePos>(*priorityIt));
1050
1051 if (!regCallout.procedure.empty())
1052 {
1053 // Procedure callout
Matt Spinler2edce4e2024-01-17 11:13:51 -06001054 callout = std::make_unique<src::Callout>(priority, regCallout.procedure,
1055 src::CalloutValueType::raw);
Matt Spinler03984582020-04-09 13:17:58 -05001056 }
1057 else if (!regCallout.symbolicFRU.empty())
1058 {
1059 // Symbolic FRU callout
1060 callout = std::make_unique<src::Callout>(
1061 priority, regCallout.symbolicFRU, locCode, false);
1062 }
1063 else if (!regCallout.symbolicFRUTrusted.empty())
1064 {
1065 // Symbolic FRU with trusted location code callout
Matt Spinler7b923722025-03-19 13:17:23 -05001066 bool trusted = false;
Matt Spinler03984582020-04-09 13:17:58 -05001067
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001068 // Use the location code from the inventory path if there is one.
1069 if (trustedSymbolicFRUInvPath)
1070 {
1071 try
1072 {
1073 locCode = dataIface.getLocationCode(*trustedSymbolicFRUInvPath);
Matt Spinler7b923722025-03-19 13:17:23 -05001074 trusted = true;
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001075 }
1076 catch (const std::exception& e)
1077 {
1078 addDebugData(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001079 std::format("Could not get location code for {}: {}",
Matt Spinlerf00f9d02020-10-23 09:14:22 -05001080 *trustedSymbolicFRUInvPath, e.what()));
1081 locCode.clear();
1082 }
1083 }
1084
Matt Spinler7b923722025-03-19 13:17:23 -05001085 // Can only trust the location code if it isn't empty and is expanded.
1086 if (!locCode.empty() && locExpanded)
1087 {
1088 trusted = true;
1089 }
1090
Matt Spinler03984582020-04-09 13:17:58 -05001091 // The registry wants it to be trusted, but that requires a valid
1092 // location code for it to actually be.
1093 callout = std::make_unique<src::Callout>(
Matt Spinler7b923722025-03-19 13:17:23 -05001094 priority, regCallout.symbolicFRUTrusted, locCode, trusted);
Matt Spinler03984582020-04-09 13:17:58 -05001095 }
1096 else
1097 {
Matt Spinleraf191c72020-06-04 11:35:13 -05001098 // A hardware callout
Matt Spinler7b923722025-03-19 13:17:23 -05001099
1100 // If couldn't expand the location code, don't bother
1101 // looking up the inventory path.
1102 if (!locExpanded && !locCode.empty())
1103 {
1104 addLocationCodeOnlyCallout(locCode, priority);
1105 return;
1106 }
1107
Matt Spinlerbad056b2023-01-25 14:16:57 -06001108 std::vector<std::string> inventoryPaths;
Matt Spinleraf191c72020-06-04 11:35:13 -05001109
1110 try
1111 {
1112 // Get the inventory item from the unexpanded location code
Matt Spinlerbad056b2023-01-25 14:16:57 -06001113 inventoryPaths =
Matt Spinler2f9225a2020-08-05 12:58:49 -05001114 dataIface.getInventoryFromLocCode(regCallout.locCode, 0, false);
Matt Spinleraf191c72020-06-04 11:35:13 -05001115 }
1116 catch (const std::exception& e)
1117 {
1118 std::string msg =
1119 "Unable to get inventory path from location code: " + locCode +
1120 ": " + e.what();
1121 addDebugData(msg);
Matt Spinler7b923722025-03-19 13:17:23 -05001122 if (!locCode.empty())
1123 {
1124 // Still add a callout with just the location code.
1125 addLocationCodeOnlyCallout(locCode, priority);
1126 }
Matt Spinleraf191c72020-06-04 11:35:13 -05001127 return;
1128 }
1129
Matt Spinlerbad056b2023-01-25 14:16:57 -06001130 // Just use first path returned since they all point to the same FRU.
1131 addInventoryCallout(inventoryPaths[0], priority, locCode, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -05001132 }
1133
1134 if (callout)
1135 {
1136 createCalloutsObject();
1137 _callouts->addCallout(std::move(callout));
1138 }
1139}
Matt Spinlered046852020-03-13 13:58:15 -05001140
Matt Spinler717de422020-06-04 13:10:14 -05001141void SRC::addDevicePathCallouts(const AdditionalData& additionalData,
1142 const DataInterfaceBase& dataIface)
1143{
1144 std::vector<device_callouts::Callout> callouts;
1145 auto i2cBus = additionalData.getValue("CALLOUT_IIC_BUS");
1146 auto i2cAddr = additionalData.getValue("CALLOUT_IIC_ADDR");
1147 auto devPath = additionalData.getValue("CALLOUT_DEVICE_PATH");
1148
1149 // A device callout contains either:
1150 // * CALLOUT_ERRNO, CALLOUT_DEVICE_PATH
1151 // * CALLOUT_ERRNO, CALLOUT_IIC_BUS, CALLOUT_IIC_ADDR
1152 // We don't care about the errno.
1153
1154 if (devPath)
1155 {
1156 try
1157 {
1158 callouts = device_callouts::getCallouts(*devPath,
1159 dataIface.getSystemNames());
1160 }
1161 catch (const std::exception& e)
1162 {
1163 addDebugData(e.what());
1164 callouts.clear();
1165 }
1166 }
1167 else if (i2cBus && i2cAddr)
1168 {
1169 size_t bus;
1170 uint8_t address;
1171
1172 try
1173 {
1174 // If /dev/i2c- is prepended, remove it
1175 if (i2cBus->find("/dev/i2c-") != std::string::npos)
1176 {
1177 *i2cBus = i2cBus->substr(9);
1178 }
1179
1180 bus = stoul(*i2cBus, nullptr, 0);
1181 address = stoul(*i2cAddr, nullptr, 0);
1182 }
1183 catch (const std::exception& e)
1184 {
Patrick Williams075c7922024-08-16 15:19:49 -04001185 std::string msg =
1186 "Invalid CALLOUT_IIC_BUS " + *i2cBus + " or CALLOUT_IIC_ADDR " +
1187 *i2cAddr + " in AdditionalData property";
Matt Spinler717de422020-06-04 13:10:14 -05001188 addDebugData(msg);
1189 return;
1190 }
1191
1192 try
1193 {
1194 callouts = device_callouts::getI2CCallouts(
1195 bus, address, dataIface.getSystemNames());
1196 }
1197 catch (const std::exception& e)
1198 {
1199 addDebugData(e.what());
1200 callouts.clear();
1201 }
1202 }
1203
1204 for (const auto& callout : callouts)
1205 {
1206 // The priority shouldn't be invalid, but check just in case.
1207 CalloutPriority priority = CalloutPriority::high;
1208
1209 if (!callout.priority.empty())
1210 {
1211 auto p = pel_values::findByValue(
1212 static_cast<uint32_t>(callout.priority[0]),
1213 pel_values::calloutPriorityValues);
1214
1215 if (p != pel_values::calloutPriorityValues.end())
1216 {
1217 priority = static_cast<CalloutPriority>(callout.priority[0]);
1218 }
1219 else
1220 {
Matt Spinlerd7654dc2025-05-09 11:47:50 -05001221 auto msg =
1222 std::string{
1223 "Invalid priority found in dev callout JSON: "} +
Matt Spinler717de422020-06-04 13:10:14 -05001224 callout.priority[0];
1225 addDebugData(msg);
1226 }
1227 }
1228
Matt Spinler0d92b522021-06-16 13:28:17 -06001229 std::optional<std::string> locCode;
1230
1231 try
1232 {
1233 locCode = dataIface.expandLocationCode(callout.locationCode, 0);
1234 }
1235 catch (const std::exception& e)
1236 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001237 auto msg = std::format("Unable to expand location code {}: {}",
Matt Spinler0d92b522021-06-16 13:28:17 -06001238 callout.locationCode, e.what());
1239 addDebugData(msg);
Matt Spinler7b923722025-03-19 13:17:23 -05001240
1241 // Add the callout with just the unexpanded location code.
1242 addLocationCodeOnlyCallout(callout.locationCode, priority);
1243 continue;
Matt Spinler0d92b522021-06-16 13:28:17 -06001244 }
1245
Matt Spinler717de422020-06-04 13:10:14 -05001246 try
1247 {
Matt Spinlerbad056b2023-01-25 14:16:57 -06001248 auto inventoryPaths = dataIface.getInventoryFromLocCode(
Matt Spinler2f9225a2020-08-05 12:58:49 -05001249 callout.locationCode, 0, false);
Matt Spinler717de422020-06-04 13:10:14 -05001250
Matt Spinlerbad056b2023-01-25 14:16:57 -06001251 // Just use first path returned since they all
1252 // point to the same FRU.
1253 addInventoryCallout(inventoryPaths[0], priority, locCode,
1254 dataIface);
Matt Spinler717de422020-06-04 13:10:14 -05001255 }
1256 catch (const std::exception& e)
1257 {
1258 std::string msg =
1259 "Unable to get inventory path from location code: " +
1260 callout.locationCode + ": " + e.what();
1261 addDebugData(msg);
Matt Spinler7b923722025-03-19 13:17:23 -05001262 // Add the callout with just the location code.
1263 addLocationCodeOnlyCallout(callout.locationCode, priority);
1264 continue;
Matt Spinler717de422020-06-04 13:10:14 -05001265 }
1266
1267 // Until the code is there to convert these MRU value strings to
1268 // the official MRU values in the callout objects, just store
1269 // the MRU name in the debug UserData section.
1270 if (!callout.mru.empty())
1271 {
1272 std::string msg = "MRU: " + callout.mru;
1273 addDebugData(msg);
1274 }
1275
1276 // getCallouts() may have generated some debug data it stored
1277 // in a callout object. Save it as well.
1278 if (!callout.debug.empty())
1279 {
1280 addDebugData(callout.debug);
1281 }
1282 }
1283}
1284
Matt Spinler5a90a952020-08-27 09:39:03 -05001285void SRC::addJSONCallouts(const nlohmann::json& jsonCallouts,
1286 const DataInterfaceBase& dataIface)
1287{
1288 if (jsonCallouts.empty())
1289 {
1290 return;
1291 }
1292
1293 if (!jsonCallouts.is_array())
1294 {
1295 addDebugData("Callout JSON isn't an array");
1296 return;
1297 }
1298
1299 for (const auto& callout : jsonCallouts)
1300 {
1301 try
1302 {
1303 addJSONCallout(callout, dataIface);
1304 }
1305 catch (const std::exception& e)
1306 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001307 addDebugData(std::format(
Matt Spinler5a90a952020-08-27 09:39:03 -05001308 "Failed extracting callout data from JSON: {}", e.what()));
1309 }
1310 }
1311}
1312
1313void SRC::addJSONCallout(const nlohmann::json& jsonCallout,
1314 const DataInterfaceBase& dataIface)
1315{
Matt Spinler3bdd0112020-08-27 10:24:34 -05001316 auto priority = getPriorityFromJSON(jsonCallout);
1317 std::string locCode;
1318 std::string unexpandedLocCode;
1319 std::unique_ptr<src::Callout> callout;
1320
1321 // Expand the location code if it's there
1322 if (jsonCallout.contains("LocationCode"))
1323 {
1324 unexpandedLocCode = jsonCallout.at("LocationCode").get<std::string>();
1325
1326 try
1327 {
1328 locCode = dataIface.expandLocationCode(unexpandedLocCode, 0);
1329 }
1330 catch (const std::exception& e)
1331 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001332 addDebugData(std::format("Unable to expand location code {}: {}",
Matt Spinler3bdd0112020-08-27 10:24:34 -05001333 unexpandedLocCode, e.what()));
1334 // Use the value from the JSON so at least there's something
1335 locCode = unexpandedLocCode;
1336 }
1337 }
1338
1339 // Create either a procedure, symbolic FRU, or normal FRU callout.
1340 if (jsonCallout.contains("Procedure"))
1341 {
1342 auto procedure = jsonCallout.at("Procedure").get<std::string>();
1343
Matt Spinler3c7ec6d2022-05-06 08:50:20 -05001344 // If it's the registry name instead of the raw name, convert.
1345 if (pv::maintenanceProcedures.find(procedure) !=
1346 pv::maintenanceProcedures.end())
1347 {
1348 procedure = pv::maintenanceProcedures.at(procedure);
1349 }
1350
Matt Spinler3bdd0112020-08-27 10:24:34 -05001351 callout = std::make_unique<src::Callout>(
1352 static_cast<CalloutPriority>(priority), procedure,
1353 src::CalloutValueType::raw);
1354 }
1355 else if (jsonCallout.contains("SymbolicFRU"))
1356 {
1357 auto fru = jsonCallout.at("SymbolicFRU").get<std::string>();
1358
Matt Spinler3c7ec6d2022-05-06 08:50:20 -05001359 // If it's the registry name instead of the raw name, convert.
1360 if (pv::symbolicFRUs.find(fru) != pv::symbolicFRUs.end())
1361 {
1362 fru = pv::symbolicFRUs.at(fru);
1363 }
1364
Matt Spinler3bdd0112020-08-27 10:24:34 -05001365 bool trusted = false;
1366 if (jsonCallout.contains("TrustedLocationCode") && !locCode.empty())
1367 {
1368 trusted = jsonCallout.at("TrustedLocationCode").get<bool>();
1369 }
1370
1371 callout = std::make_unique<src::Callout>(
1372 static_cast<CalloutPriority>(priority), fru,
1373 src::CalloutValueType::raw, locCode, trusted);
1374 }
1375 else
1376 {
1377 // A hardware FRU
1378 std::string inventoryPath;
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001379 std::vector<src::MRU::MRUCallout> mrus;
Matt Spinler3bdd0112020-08-27 10:24:34 -05001380
1381 if (jsonCallout.contains("InventoryPath"))
1382 {
1383 inventoryPath = jsonCallout.at("InventoryPath").get<std::string>();
1384 }
1385 else
1386 {
1387 if (unexpandedLocCode.empty())
1388 {
1389 throw std::runtime_error{"JSON callout needs either an "
1390 "inventory path or location code"};
1391 }
1392
1393 try
1394 {
Matt Spinlerbad056b2023-01-25 14:16:57 -06001395 auto inventoryPaths = dataIface.getInventoryFromLocCode(
Matt Spinler3bdd0112020-08-27 10:24:34 -05001396 unexpandedLocCode, 0, false);
Matt Spinlerbad056b2023-01-25 14:16:57 -06001397 // Just use first path returned since they all
1398 // point to the same FRU.
1399 inventoryPath = inventoryPaths[0];
Matt Spinler3bdd0112020-08-27 10:24:34 -05001400 }
1401 catch (const std::exception& e)
1402 {
Matt Spinler7b923722025-03-19 13:17:23 -05001403 addDebugData(std::format("Unable to get inventory path from "
1404 "location code: {}: {}",
1405 unexpandedLocCode, e.what()));
1406 addLocationCodeOnlyCallout(locCode, priority);
1407 return;
Matt Spinler3bdd0112020-08-27 10:24:34 -05001408 }
1409 }
1410
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001411 if (jsonCallout.contains("MRUs"))
1412 {
1413 mrus = getMRUsFromJSON(jsonCallout.at("MRUs"));
1414 }
1415
Matt Spinler3bdd0112020-08-27 10:24:34 -05001416 // If the location code was also passed in, use that here too
1417 // so addInventoryCallout doesn't have to look it up.
1418 std::optional<std::string> lc;
1419 if (!locCode.empty())
1420 {
1421 lc = locCode;
1422 }
1423
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001424 addInventoryCallout(inventoryPath, priority, lc, dataIface, mrus);
Matt Spinlerafa2c792020-08-27 11:01:39 -05001425
1426 if (jsonCallout.contains("Deconfigured"))
1427 {
1428 if (jsonCallout.at("Deconfigured").get<bool>())
1429 {
1430 setErrorStatusFlag(ErrorStatusFlags::deconfigured);
1431 }
1432 }
1433
1434 if (jsonCallout.contains("Guarded"))
1435 {
1436 if (jsonCallout.at("Guarded").get<bool>())
1437 {
1438 setErrorStatusFlag(ErrorStatusFlags::guarded);
1439 }
1440 }
Matt Spinler3bdd0112020-08-27 10:24:34 -05001441 }
1442
1443 if (callout)
1444 {
1445 createCalloutsObject();
1446 _callouts->addCallout(std::move(callout));
1447 }
1448}
1449
1450CalloutPriority SRC::getPriorityFromJSON(const nlohmann::json& json)
1451{
1452 // Looks like:
1453 // {
1454 // "Priority": "H"
1455 // }
1456 auto p = json.at("Priority").get<std::string>();
1457 if (p.empty())
1458 {
1459 throw std::runtime_error{"Priority field in callout is empty"};
1460 }
1461
1462 auto priority = static_cast<CalloutPriority>(p.front());
1463
1464 // Validate it
1465 auto priorityIt = pv::findByValue(static_cast<uint32_t>(priority),
1466 pv::calloutPriorityValues);
1467 if (priorityIt == pv::calloutPriorityValues.end())
1468 {
1469 throw std::runtime_error{
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001470 std::format("Invalid priority '{}' found in JSON callout", p)};
Matt Spinler3bdd0112020-08-27 10:24:34 -05001471 }
1472
1473 return priority;
Matt Spinler5a90a952020-08-27 09:39:03 -05001474}
1475
Patrick Williams25291152025-02-01 08:21:42 -05001476std::vector<src::MRU::MRUCallout> SRC::getMRUsFromJSON(
1477 const nlohmann::json& mruJSON)
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001478{
1479 std::vector<src::MRU::MRUCallout> mrus;
1480
1481 // Looks like:
1482 // [
1483 // {
1484 // "ID": 100,
1485 // "Priority": "H"
1486 // }
1487 // ]
1488 if (!mruJSON.is_array())
1489 {
1490 addDebugData("MRU callout JSON is not an array");
1491 return mrus;
1492 }
1493
1494 for (const auto& mruCallout : mruJSON)
1495 {
1496 try
1497 {
1498 auto priority = getPriorityFromJSON(mruCallout);
1499 auto id = mruCallout.at("ID").get<uint32_t>();
1500
1501 src::MRU::MRUCallout mru{static_cast<uint32_t>(priority), id};
1502 mrus.push_back(std::move(mru));
1503 }
1504 catch (const std::exception& e)
1505 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -05001506 addDebugData(std::format("Invalid MRU entry in JSON: {}: {}",
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001507 mruCallout.dump(), e.what()));
1508 }
1509 }
1510
1511 return mrus;
1512}
1513
Sumit Kumar3e274432021-09-14 06:37:56 -05001514std::vector<uint8_t> SRC::getSrcStruct()
1515{
1516 std::vector<uint8_t> data;
1517 Stream stream{data};
1518
1519 //------ Ref section 4.3 in PEL doc---
1520 //------ SRC Structure 40 bytes-------
1521 // Byte-0 | Byte-1 | Byte-2 | Byte-3 |
1522 // -----------------------------------
1523 // 02 | 08 | 00 | 09 | ==> Header
1524 // 00 | 00 | 00 | 48 | ==> Header
1525 // 00 | 00 | 00 | 00 | ==> Hex data word-2
1526 // 00 | 00 | 00 | 00 | ==> Hex data word-3
1527 // 00 | 00 | 00 | 00 | ==> Hex data word-4
1528 // 20 | 00 | 00 | 00 | ==> Hex data word-5
1529 // 00 | 00 | 00 | 00 | ==> Hex data word-6
1530 // 00 | 00 | 00 | 00 | ==> Hex data word-7
1531 // 00 | 00 | 00 | 00 | ==> Hex data word-8
1532 // 00 | 00 | 00 | 00 | ==> Hex data word-9
1533 // -----------------------------------
1534 // ASCII string - 8 bytes |
1535 // -----------------------------------
1536 // ASCII space NULL - 24 bytes |
1537 // -----------------------------------
1538 //_size = Base SRC struct: 8 byte header + hex data section + ASCII string
1539
1540 uint8_t flags = (_flags | postOPPanel);
1541
1542 stream << _version << flags << _reserved1B << _wordCount << _reserved2B
1543 << _size;
1544
1545 for (auto& word : _hexData)
1546 {
1547 stream << word;
1548 }
1549
1550 _asciiString->flatten(stream);
1551
1552 return data;
1553}
1554
Vijay Lobo875b6c72021-10-20 17:38:56 -05001555void SRC::setProgressCode(const DataInterfaceBase& dataIface)
1556{
1557 std::vector<uint8_t> progressSRC;
1558
1559 try
1560 {
1561 progressSRC = dataIface.getRawProgressSRC();
1562 }
1563 catch (const std::exception& e)
1564 {
Matt Spinler0bacc8e2023-07-07 16:25:39 -05001565 lg2::error("Error getting progress code: {ERROR}", "ERROR", e);
Vijay Lobo875b6c72021-10-20 17:38:56 -05001566 return;
1567 }
1568
1569 _hexData[2] = getProgressCode(progressSRC);
1570}
1571
1572uint32_t SRC::getProgressCode(std::vector<uint8_t>& rawProgressSRC)
1573{
1574 uint32_t progressCode = 0;
1575
1576 // A valid progress SRC is at least 72 bytes
1577 if (rawProgressSRC.size() < 72)
1578 {
1579 return progressCode;
1580 }
1581
1582 try
1583 {
1584 // The ASCII string field in progress SRCs starts at offset 40.
1585 // Take the first 8 characters to put in the uint32:
1586 // "CC009189" -> 0xCC009189
1587 Stream stream{rawProgressSRC, 40};
1588 src::AsciiString aString{stream};
1589 auto progressCodeString = aString.get().substr(0, 8);
1590
1591 if (std::all_of(progressCodeString.begin(), progressCodeString.end(),
1592 [](char c) {
Patrick Williams075c7922024-08-16 15:19:49 -04001593 return std::isxdigit(static_cast<unsigned char>(c));
1594 }))
Vijay Lobo875b6c72021-10-20 17:38:56 -05001595 {
1596 progressCode = std::stoul(progressCodeString, nullptr, 16);
1597 }
1598 }
1599 catch (const std::exception& e)
1600 {}
1601
1602 return progressCode;
1603}
1604
Matt Spinlerf9bae182019-10-09 13:37:38 -05001605} // namespace pels
1606} // namespace openpower