| Alexander Hansen | 40fb549 | 2025-10-28 17:56:12 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 | 
 | 2 | // SPDX-FileCopyrightText: Copyright 2019 IBM Corporation | 
 | 3 |  | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 4 | #include "src.hpp" | 
 | 5 |  | 
| Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 6 | #include "device_callouts.hpp" | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 7 | #include "json_utils.hpp" | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 8 | #include "pel_values.hpp" | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 9 | #ifdef PELTOOL | 
 | 10 | #include <Python.h> | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 11 |  | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 12 | #include <nlohmann/json.hpp> | 
| Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 13 |  | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 14 | #include <sstream> | 
 | 15 | #endif | 
| Matt Spinler | 0bacc8e | 2023-07-07 16:25:39 -0500 | [diff] [blame] | 16 | #include <phosphor-logging/lg2.hpp> | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 17 |  | 
| Jayanth Othayoth | 1aa90d4 | 2023-09-13 04:25:45 -0500 | [diff] [blame] | 18 | #include <format> | 
 | 19 |  | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 20 | namespace openpower | 
 | 21 | { | 
 | 22 | namespace pels | 
 | 23 | { | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 24 | namespace pv = openpower::pels::pel_values; | 
 | 25 | namespace rg = openpower::pels::message; | 
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 26 | using namespace std::string_literals; | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 27 |  | 
| Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 28 | constexpr size_t ccinSize = 4; | 
 | 29 |  | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 30 | #ifdef PELTOOL | 
| Sumit Kumar | 516935a | 2021-04-14 13:00:54 -0500 | [diff] [blame] | 31 | using orderedJSON = nlohmann::ordered_json; | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 32 |  | 
 | 33 | void 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 Kumar | 516935a | 2021-04-14 13:00:54 -0500 | [diff] [blame] | 49 | std::string prettyJSON(const orderedJSON& json) | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 50 | { | 
| Sumit Kumar | 516935a | 2021-04-14 13:00:54 -0500 | [diff] [blame] | 51 |     orderedJSON output; | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 52 |     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 Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 60 |             output["SRC Details"][key] = value; | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 61 |         } | 
 | 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 |  */ | 
 | 113 | std::optional<std::string> getPythonJSON(std::vector<std::string>& hexwords, | 
 | 114 |                                          uint8_t creatorID) | 
 | 115 | { | 
| Matt Spinler | be952d2 | 2022-07-01 11:30:11 -0500 | [diff] [blame] | 116 |     PyObject *pName, *pModule, *eType, *eValue, *eTraceback; | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 117 |     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 Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 123 |     if (pModule == NULL) | 
 | 124 |     { | 
 | 125 |         pErrStr = "No error string found"; | 
 | 126 |         PyErr_Fetch(&eType, &eValue, &eTraceback); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 127 |         if (eType) | 
 | 128 |         { | 
 | 129 |             Py_XDECREF(eType); | 
 | 130 |         } | 
 | 131 |         if (eTraceback) | 
 | 132 |         { | 
 | 133 |             Py_XDECREF(eTraceback); | 
 | 134 |         } | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 135 |         if (eValue) | 
 | 136 |         { | 
 | 137 |             PyObject* pStr = PyObject_Str(eValue); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 138 |             Py_XDECREF(eValue); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 139 |             if (pStr) | 
 | 140 |             { | 
 | 141 |                 pErrStr = PyUnicode_AsUTF8(pStr); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 142 |                 Py_XDECREF(pStr); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 143 |             } | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 144 |         } | 
 | 145 |     } | 
 | 146 |     else | 
 | 147 |     { | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 148 |         std::unique_ptr<PyObject, decltype(&pyDecRef)> modPtr( | 
 | 149 |             pModule, &pyDecRef); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 150 |         std::string funcToCall = "parseSRCToJson"; | 
| Matt Spinler | be952d2 | 2022-07-01 11:30:11 -0500 | [diff] [blame] | 151 |         PyObject* pKey = PyUnicode_FromString(funcToCall.c_str()); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 152 |         std::unique_ptr<PyObject, decltype(&pyDecRef)> keyPtr(pKey, &pyDecRef); | 
| Matt Spinler | be952d2 | 2022-07-01 11:30:11 -0500 | [diff] [blame] | 153 |         PyObject* pDict = PyModule_GetDict(pModule); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 154 |         Py_INCREF(pDict); | 
 | 155 |         if (!PyDict_Contains(pDict, pKey)) | 
 | 156 |         { | 
 | 157 |             Py_DECREF(pDict); | 
| Matt Spinler | 0bacc8e | 2023-07-07 16:25:39 -0500 | [diff] [blame] | 158 |             lg2::error( | 
 | 159 |                 "Python module error.  Function missing: {FUNC}, SRC = {SRC}, module = {MODULE}", | 
 | 160 |                 "FUNC", funcToCall, "SRC", hexwords.front(), "MODULE", module); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 161 |             return std::nullopt; | 
 | 162 |         } | 
| Matt Spinler | be952d2 | 2022-07-01 11:30:11 -0500 | [diff] [blame] | 163 |         PyObject* pFunc = PyDict_GetItemString(pDict, funcToCall.c_str()); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 164 |         Py_DECREF(pDict); | 
 | 165 |         Py_INCREF(pFunc); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 166 |         if (PyCallable_Check(pFunc)) | 
 | 167 |         { | 
| Matt Spinler | be952d2 | 2022-07-01 11:30:11 -0500 | [diff] [blame] | 168 |             PyObject* pArgs = PyTuple_New(9); | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 169 |             std::unique_ptr<PyObject, decltype(&pyDecRef)> argPtr( | 
 | 170 |                 pArgs, &pyDecRef); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 171 |             for (size_t i = 0; i < 9; i++) | 
 | 172 |             { | 
| Matt Spinler | c198403 | 2023-01-05 10:09:59 -0600 | [diff] [blame] | 173 |                 std::string arg{"00000000"}; | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 174 |                 if (i < hexwords.size()) | 
 | 175 |                 { | 
| Matt Spinler | c198403 | 2023-01-05 10:09:59 -0600 | [diff] [blame] | 176 |                     arg = hexwords[i]; | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 177 |                 } | 
| Matt Spinler | c198403 | 2023-01-05 10:09:59 -0600 | [diff] [blame] | 178 |                 PyTuple_SetItem(pArgs, i, Py_BuildValue("s", arg.c_str())); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 179 |             } | 
| Matt Spinler | be952d2 | 2022-07-01 11:30:11 -0500 | [diff] [blame] | 180 |             PyObject* pResult = PyObject_CallObject(pFunc, pArgs); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 181 |             Py_DECREF(pFunc); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 182 |             if (pResult) | 
 | 183 |             { | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 184 |                 std::unique_ptr<PyObject, decltype(&pyDecRef)> resPtr( | 
 | 185 |                     pResult, &pyDecRef); | 
| Matt Spinler | 91f6d3a | 2025-05-22 08:32:49 -0500 | [diff] [blame] | 186 |  | 
 | 187 |                 if (pResult == Py_None) | 
 | 188 |                 { | 
 | 189 |                     return std::nullopt; | 
 | 190 |                 } | 
 | 191 |  | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 192 |                 PyObject* pBytes = | 
 | 193 |                     PyUnicode_AsEncodedString(pResult, "utf-8", "~E~"); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 194 |                 std::unique_ptr<PyObject, decltype(&pyDecRef)> pyBytePtr( | 
 | 195 |                     pBytes, &pyDecRef); | 
 | 196 |                 const char* output = PyBytes_AS_STRING(pBytes); | 
 | 197 |                 try | 
 | 198 |                 { | 
| Matt Spinler | bb1c1d5 | 2021-06-03 13:18:48 -0600 | [diff] [blame] | 199 |                     orderedJSON json = orderedJSON::parse(output); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 200 |                     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 Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 206 |                 } | 
| Patrick Williams | 66491c6 | 2021-10-06 12:23:37 -0500 | [diff] [blame] | 207 |                 catch (const std::exception& e) | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 208 |                 { | 
| Matt Spinler | 0bacc8e | 2023-07-07 16:25:39 -0500 | [diff] [blame] | 209 |                     lg2::error( | 
 | 210 |                         "Bad JSON from parser. Error = {ERROR}, SRC = {SRC}, module = {MODULE}", | 
 | 211 |                         "ERROR", e, "SRC", hexwords.front(), "MODULE", module); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 212 |                     return std::nullopt; | 
 | 213 |                 } | 
 | 214 |             } | 
 | 215 |             else | 
 | 216 |             { | 
 | 217 |                 pErrStr = "No error string found"; | 
 | 218 |                 PyErr_Fetch(&eType, &eValue, &eTraceback); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 219 |                 if (eType) | 
 | 220 |                 { | 
 | 221 |                     Py_XDECREF(eType); | 
 | 222 |                 } | 
 | 223 |                 if (eTraceback) | 
 | 224 |                 { | 
 | 225 |                     Py_XDECREF(eTraceback); | 
 | 226 |                 } | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 227 |                 if (eValue) | 
 | 228 |                 { | 
 | 229 |                     PyObject* pStr = PyObject_Str(eValue); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 230 |                     Py_XDECREF(eValue); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 231 |                     if (pStr) | 
 | 232 |                     { | 
 | 233 |                         pErrStr = PyUnicode_AsUTF8(pStr); | 
| Harisuddin Mohamed Isa | 69c1827 | 2021-05-29 13:18:45 +0800 | [diff] [blame] | 234 |                         Py_XDECREF(pStr); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 235 |                     } | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 236 |                 } | 
 | 237 |             } | 
 | 238 |         } | 
 | 239 |     } | 
 | 240 |     if (!pErrStr.empty()) | 
 | 241 |     { | 
| Matt Spinler | 0bacc8e | 2023-07-07 16:25:39 -0500 | [diff] [blame] | 242 |         lg2::debug("Python exception thrown by parser. Error = {ERROR}, " | 
 | 243 |                    "SRC = {SRC}, module = {MODULE}", | 
 | 244 |                    "ERROR", pErrStr, "SRC", hexwords.front(), "MODULE", module); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 245 |     } | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 246 |     return std::nullopt; | 
 | 247 | } | 
 | 248 | #endif | 
 | 249 |  | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 250 | void 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 Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 269 | void SRC::flatten(Stream& stream) const | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 270 | { | 
 | 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 |  | 
 | 287 | SRC::SRC(Stream& pel) | 
 | 288 | { | 
 | 289 |     try | 
 | 290 |     { | 
 | 291 |         unflatten(pel); | 
 | 292 |         validate(); | 
 | 293 |     } | 
 | 294 |     catch (const std::exception& e) | 
 | 295 |     { | 
| Matt Spinler | 0bacc8e | 2023-07-07 16:25:39 -0500 | [diff] [blame] | 296 |         lg2::error("Cannot unflatten SRC, error = {ERROR}", "ERROR", e); | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 297 |         _valid = false; | 
 | 298 |     } | 
 | 299 | } | 
 | 300 |  | 
| Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 301 | SRC::SRC(const message::Entry& regEntry, const AdditionalData& additionalData, | 
| Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 302 |          const nlohmann::json& jsonCallouts, const DataInterfaceBase& dataIface) | 
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 303 | { | 
 | 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 Lobo | f3702bb | 2021-04-09 15:10:19 -0500 | [diff] [blame] | 312 |  | 
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 313 |     _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 Spinler | 7c61918 | 2020-07-27 15:15:11 -0500 | [diff] [blame] | 322 |  | 
 | 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 Kumar | 9d43a72 | 2021-08-24 09:46:19 -0500 | [diff] [blame] | 328 |     //   V: BMC dump status | 
| Matt Spinler | 7c61918 | 2020-07-27 15:15:11 -0500 | [diff] [blame] | 329 |     //   E: Platform boot mode = 0 (side = temporary, speed = fast) | 
| Sumit Kumar | 9d43a72 | 2021-08-24 09:46:19 -0500 | [diff] [blame] | 330 |     //   P: Platform dump status | 
| Matt Spinler | 7c61918 | 2020-07-27 15:15:11 -0500 | [diff] [blame] | 331 |     //  FF: SRC format, set below | 
 | 332 |  | 
| Vijay Lobo | 875b6c7 | 2021-10-20 17:38:56 -0500 | [diff] [blame] | 333 |     setProgressCode(dataIface); | 
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 334 |     setBMCFormat(); | 
 | 335 |     setBMCPosition(); | 
| Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 336 |     setMotherboardCCIN(dataIface); | 
 | 337 |  | 
| Matt Spinler | da5b76b | 2023-06-01 15:56:57 -0500 | [diff] [blame] | 338 |     if (regEntry.src.checkstopFlag) | 
 | 339 |     { | 
 | 340 |         setErrorStatusFlag(ErrorStatusFlags::hwCheckstop); | 
 | 341 |     } | 
 | 342 |  | 
| Matt Spinler | 3fe93e9 | 2023-04-14 14:06:59 -0500 | [diff] [blame] | 343 |     if (regEntry.src.deconfigFlag) | 
 | 344 |     { | 
 | 345 |         setErrorStatusFlag(ErrorStatusFlags::deconfigured); | 
 | 346 |     } | 
 | 347 |  | 
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 348 |     // 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 Kumar | 50bfa69 | 2022-01-06 06:48:26 -0600 | [diff] [blame] | 353 |     // Check for additional data - PEL_SUBSYSTEM | 
 | 354 |     auto ss = additionalData.getValue("PEL_SUBSYSTEM"); | 
 | 355 |     if (ss) | 
 | 356 |     { | 
| Matt Spinler | 6f07df3 | 2025-05-09 11:42:39 -0500 | [diff] [blame] | 357 |         auto eventSubsystem = std::stoul(*ss, nullptr, 16); | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 358 |         std::string subsystem = | 
 | 359 |             pv::getValue(eventSubsystem, pel_values::subsystemValues); | 
| Sumit Kumar | 50bfa69 | 2022-01-06 06:48:26 -0600 | [diff] [blame] | 360 |         if (subsystem == "invalid") | 
 | 361 |         { | 
| Matt Spinler | 0bacc8e | 2023-07-07 16:25:39 -0500 | [diff] [blame] | 362 |             lg2::warning("SRC: Invalid SubSystem value: {VAL}", "VAL", lg2::hex, | 
 | 363 |                          eventSubsystem); | 
| Sumit Kumar | 50bfa69 | 2022-01-06 06:48:26 -0600 | [diff] [blame] | 364 |         } | 
 | 365 |         else | 
 | 366 |         { | 
 | 367 |             _asciiString->setByte(2, eventSubsystem); | 
 | 368 |         } | 
 | 369 |     } | 
 | 370 |  | 
| Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 371 |     addCallouts(regEntry, additionalData, jsonCallouts, dataIface); | 
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 372 |  | 
 | 373 |     _size = baseSRCSize; | 
 | 374 |     _size += _callouts ? _callouts->flattenedSize() : 0; | 
| Matt Spinler | e2eb14a | 2025-05-09 13:34:51 -0500 | [diff] [blame] | 375 |     _header.size = Section::headerSize() + _size; | 
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 376 |  | 
 | 377 |     _valid = true; | 
 | 378 | } | 
 | 379 |  | 
 | 380 | void SRC::setUserDefinedHexWords(const message::Entry& regEntry, | 
 | 381 |                                  const AdditionalData& ad) | 
 | 382 | { | 
 | 383 |     if (!regEntry.src.hexwordADFields) | 
 | 384 |     { | 
 | 385 |         return; | 
 | 386 |     } | 
 | 387 |  | 
| Harisuddin Mohamed Isa | 1a1b0df | 2020-11-23 16:34:36 +0800 | [diff] [blame] | 388 |     // Save the AdditionalData value corresponding to the first element of | 
 | 389 |     // adName tuple into _hexData[wordNum]. | 
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 390 |     for (const auto& [wordNum, adName] : *regEntry.src.hexwordADFields) | 
 | 391 |     { | 
 | 392 |         // Can only set words 6 - 9 | 
 | 393 |         if (!isUserDefinedWord(wordNum)) | 
 | 394 |         { | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 395 |             std::string msg = | 
 | 396 |                 "SRC user data word out of range: " + std::to_string(wordNum); | 
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 397 |             addDebugData(msg); | 
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 398 |             continue; | 
 | 399 |         } | 
 | 400 |  | 
| Harisuddin Mohamed Isa | 1a1b0df | 2020-11-23 16:34:36 +0800 | [diff] [blame] | 401 |         auto value = ad.getValue(std::get<0>(adName)); | 
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 402 |         if (value) | 
 | 403 |         { | 
 | 404 |             _hexData[getWordIndexFromWordNum(wordNum)] = | 
 | 405 |                 std::strtoul(value.value().c_str(), nullptr, 0); | 
 | 406 |         } | 
 | 407 |         else | 
 | 408 |         { | 
| Harisuddin Mohamed Isa | 1a1b0df | 2020-11-23 16:34:36 +0800 | [diff] [blame] | 409 |             std::string msg = "Source for user data SRC word not found: " + | 
 | 410 |                               std::get<0>(adName); | 
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 411 |             addDebugData(msg); | 
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 412 |         } | 
 | 413 |     } | 
 | 414 | } | 
 | 415 |  | 
| Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 416 | void 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 Spinler | 6f07df3 | 2025-05-09 11:42:39 -0500 | [diff] [blame] | 425 |             ccin = std::stoi(ccinString, nullptr, 16); | 
| Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 426 |         } | 
 | 427 |     } | 
| Patrick Williams | 66491c6 | 2021-10-06 12:23:37 -0500 | [diff] [blame] | 428 |     catch (const std::exception& e) | 
| Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 429 |     { | 
| Matt Spinler | 0bacc8e | 2023-07-07 16:25:39 -0500 | [diff] [blame] | 430 |         lg2::warning("Could not convert motherboard CCIN {CCIN} to a number", | 
 | 431 |                      "CCIN", ccinString); | 
| Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 432 |         return; | 
 | 433 |     } | 
 | 434 |  | 
 | 435 |     // Set the first 2 bytes | 
 | 436 |     _hexData[1] |= ccin << 16; | 
 | 437 | } | 
 | 438 |  | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 439 | void 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 Spinler | 0bacc8e | 2023-07-07 16:25:39 -0500 | [diff] [blame] | 446 |         lg2::error("Invalid SRC section ID: {ID}", "ID", lg2::hex, header().id); | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 447 |         failed = true; | 
 | 448 |     } | 
 | 449 |  | 
 | 450 |     // Check the version in the SRC, not in the header | 
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 451 |     if (_version != srcVersion) | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 452 |     { | 
| Matt Spinler | 0bacc8e | 2023-07-07 16:25:39 -0500 | [diff] [blame] | 453 |         lg2::error("Invalid SRC version: {VERSION}", "VERSION", lg2::hex, | 
 | 454 |                    header().version); | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 455 |         failed = true; | 
 | 456 |     } | 
 | 457 |  | 
 | 458 |     _valid = failed ? false : true; | 
 | 459 | } | 
 | 460 |  | 
| Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 461 | bool 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 Spinler | 4deed97 | 2023-04-28 14:09:22 -0500 | [diff] [blame] | 473 | bool 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 Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 484 | std::optional<std::string> SRC::getErrorDetails( | 
 | 485 |     message::Registry& registry, DetailLevel type, bool toCache) const | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 486 | { | 
 | 487 |     const std::string jsonIndent(indentLevel, 0x20); | 
 | 488 |     std::string errorOut; | 
| Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 489 |     if (isBMCSRC()) | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 490 |     { | 
 | 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 Isa | 1a1b0df | 2020-11-23 16:34:36 +0800 | [diff] [blame] | 510 |                 std::map<size_t, std::tuple<std::string, std::string>> | 
 | 511 |                     adFields = entry->src.hexwordADFields.value(); | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 512 |                 for (const auto& hexwordMap : adFields) | 
 | 513 |                 { | 
| Zane Shelley | e8db29b | 2021-11-13 10:34:07 -0600 | [diff] [blame] | 514 |                     auto srcValue = getNumberString( | 
| Harisuddin Mohamed Isa | 1a1b0df | 2020-11-23 16:34:36 +0800 | [diff] [blame] | 515 |                         "0x%X", | 
| Zane Shelley | e8db29b | 2021-11-13 10:34:07 -0600 | [diff] [blame] | 516 |                         _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 Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 530 |                 } | 
 | 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 Williams | 2529115 | 2025-02-01 08:21:42 -0500 | [diff] [blame] | 541 | std::optional<std::string> SRC::getErrorMessage( | 
 | 542 |     const message::Entry& regEntry) const | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 543 | { | 
 | 544 |     try | 
 | 545 |     { | 
 | 546 |         if (regEntry.doc.messageArgSources) | 
 | 547 |         { | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 548 |             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 Williams | 0230abb | 2021-04-19 14:32:50 -0500 | [diff] [blame] | 556 |  | 
 | 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 Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 561 |             { | 
| Patrick Williams | 0230abb | 2021-04-19 14:32:50 -0500 | [diff] [blame] | 562 |                 if (*it == '%') | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 563 |                 { | 
| Patrick Williams | 0230abb | 2021-04-19 14:32:50 -0500 | [diff] [blame] | 564 |                     ++it; | 
 | 565 |  | 
 | 566 |                     size_t wordIndex = *it - '0'; | 
 | 567 |                     if (isdigit(*it) && wordIndex >= 1 && | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 568 |                         static_cast<uint16_t>(wordIndex) <= | 
 | 569 |                             argSourceVals.size()) | 
 | 570 |                     { | 
 | 571 |                         message.append(getNumberString( | 
| Zane Shelley | 39936e3 | 2021-11-13 16:19:34 -0600 | [diff] [blame] | 572 |                             "0x%08X", argSourceVals[wordIndex - 1])); | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 573 |                     } | 
 | 574 |                     else | 
 | 575 |                     { | 
| Patrick Williams | 0230abb | 2021-04-19 14:32:50 -0500 | [diff] [blame] | 576 |                         message.append("%" + std::string(1, *it)); | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 577 |                     } | 
 | 578 |                 } | 
 | 579 |                 else | 
 | 580 |                 { | 
| Patrick Williams | 0230abb | 2021-04-19 14:32:50 -0500 | [diff] [blame] | 581 |                     message.push_back(*it); | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 582 |                 } | 
| Patrick Williams | 0230abb | 2021-04-19 14:32:50 -0500 | [diff] [blame] | 583 |                 ++it; | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 584 |             } | 
| Patrick Williams | 0230abb | 2021-04-19 14:32:50 -0500 | [diff] [blame] | 585 |  | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 586 |             return message; | 
 | 587 |         } | 
 | 588 |         else | 
 | 589 |         { | 
 | 590 |             return regEntry.doc.message; | 
 | 591 |         } | 
 | 592 |     } | 
 | 593 |     catch (const std::exception& e) | 
 | 594 |     { | 
| Matt Spinler | 0bacc8e | 2023-07-07 16:25:39 -0500 | [diff] [blame] | 595 |         lg2::error( | 
 | 596 |             "Cannot get error message from registry entry, error = {ERROR}", | 
 | 597 |             "ERROR", e); | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 598 |     } | 
 | 599 |     return std::nullopt; | 
 | 600 | } | 
 | 601 |  | 
 | 602 | std::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 Spinler | 9e8b49e | 2020-09-10 13:15:26 -0500 | [diff] [blame] | 638 |                 jsonInsert(printOut, "Procedure", | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 639 |                            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 Williams | 2529115 | 2025-02-01 08:21:42 -0500 | [diff] [blame] | 703 | std::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 Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 707 | { | 
 | 708 |     std::string ps; | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 709 |     std::vector<std::string> hexwords; | 
| Harisuddin Mohamed Isa | bebeb94 | 2020-03-12 17:12:24 +0800 | [diff] [blame] | 710 |     jsonInsert(ps, pv::sectionVer, getNumberString("%d", _header.version), 1); | 
 | 711 |     jsonInsert(ps, pv::subSection, getNumberString("%d", _header.subType), 1); | 
| Matt Spinler | b832aa5 | 2023-03-21 15:32:34 -0500 | [diff] [blame] | 712 |     jsonInsert(ps, pv::createdBy, | 
 | 713 |                getComponentName(_header.componentID, creatorID), 1); | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 714 |     jsonInsert(ps, "SRC Version", getNumberString("0x%02X", _version), 1); | 
| Harisuddin Mohamed Isa | c32e551 | 2020-02-06 18:05:21 +0800 | [diff] [blame] | 715 |     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 Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 723 |  | 
 | 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 Spinler | afa2c79 | 2020-08-27 11:01:39 -0500 | [diff] [blame] | 735 |  | 
| Sumit Kumar | 3e27443 | 2021-09-14 06:37:56 -0500 | [diff] [blame] | 736 |         jsonInsert(ps, "Terminate FW Error", | 
 | 737 |                    pv::boolString.at( | 
 | 738 |                        _hexData[3] & | 
 | 739 |                        static_cast<uint32_t>(ErrorStatusFlags::terminateFwErr)), | 
 | 740 |                    1); | 
| Matt Spinler | 4deed97 | 2023-04-28 14:09:22 -0500 | [diff] [blame] | 741 |     } | 
 | 742 |  | 
 | 743 |     if (isBMCSRC() || isHostbootSRC()) | 
 | 744 |     { | 
| Matt Spinler | afa2c79 | 2020-08-27 11:01:39 -0500 | [diff] [blame] | 745 |         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 Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 753 |             pv::boolString.at( | 
 | 754 |                 _hexData[3] & static_cast<uint32_t>(ErrorStatusFlags::guarded)), | 
| Matt Spinler | afa2c79 | 2020-08-27 11:01:39 -0500 | [diff] [blame] | 755 |             1); | 
| Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 756 |     } | 
 | 757 |  | 
| Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 758 |     auto errorDetails = getErrorDetails(registry, DetailLevel::json, true); | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 759 |     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 Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 766 |     hexwords.push_back(refcode); | 
| Harisuddin Mohamed Isa | fecaa57 | 2020-03-11 16:04:50 +0800 | [diff] [blame] | 767 |     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 Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 778 |     jsonInsert(ps, "Reference Code", refcode, 1); | 
| Harisuddin Mohamed Isa | fecaa57 | 2020-03-11 16:04:50 +0800 | [diff] [blame] | 779 |     if (!extRefcode.empty()) | 
 | 780 |     { | 
 | 781 |         jsonInsert(ps, "Extended Reference Code", extRefcode, 1); | 
 | 782 |     } | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 783 |     for (size_t i = 2; i <= _wordCount; i++) | 
 | 784 |     { | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 785 |         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 Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 789 |     } | 
 | 790 |     auto calloutJson = getCallouts(); | 
 | 791 |     if (calloutJson) | 
 | 792 |     { | 
 | 793 |         ps.append(calloutJson.value()); | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 794 |         ps.append(",\n"); | 
| Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 795 |     } | 
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 796 |     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 Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 811 |     { | 
 | 812 |         ps.erase(ps.size() - 2); | 
 | 813 |     } | 
 | 814 |     return ps; | 
 | 815 | } | 
 | 816 |  | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 817 | void SRC::addCallouts(const message::Entry& regEntry, | 
 | 818 |                       const AdditionalData& additionalData, | 
| Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 819 |                       const nlohmann::json& jsonCallouts, | 
| Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 820 |                       const DataInterfaceBase& dataIface) | 
 | 821 | { | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 822 |     auto registryCallouts = | 
 | 823 |         getRegistryCallouts(regEntry, additionalData, dataIface); | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 824 |  | 
| Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 825 |     auto item = additionalData.getValue("CALLOUT_INVENTORY_PATH"); | 
| Miguel Gomez | 53ef155 | 2020-10-14 21:16:32 +0000 | [diff] [blame] | 826 |     auto priority = additionalData.getValue("CALLOUT_PRIORITY"); | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 827 |  | 
| Miguel Gomez | 53ef155 | 2020-10-14 21:16:32 +0000 | [diff] [blame] | 828 |     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 Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 839 |     // 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 Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 848 |     { | 
| Miguel Gomez | 53ef155 | 2020-10-14 21:16:32 +0000 | [diff] [blame] | 849 |         addInventoryCallout(*item, calloutPriority, std::nullopt, dataIface); | 
| Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 850 |     } | 
 | 851 |  | 
| Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 852 |     addDevicePathCallouts(additionalData, dataIface); | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 853 |  | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 854 |     addRegistryCallouts(registryCallouts, dataIface, | 
 | 855 |                         (useInvForSymbolicFRULocCode) ? item : std::nullopt); | 
| Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 856 |  | 
 | 857 |     if (!jsonCallouts.empty()) | 
 | 858 |     { | 
 | 859 |         addJSONCallouts(jsonCallouts, dataIface); | 
 | 860 |     } | 
| Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 861 | } | 
 | 862 |  | 
| Matt Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 863 | void 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 Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 874 | void SRC::addInventoryCallout(const std::string& inventoryPath, | 
| Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 875 |                               const std::optional<CalloutPriority>& priority, | 
 | 876 |                               const std::optional<std::string>& locationCode, | 
| Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 877 |                               const DataInterfaceBase& dataIface, | 
 | 878 |                               const std::vector<src::MRU::MRUCallout>& mrus) | 
| Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 879 | { | 
 | 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 Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 886 |     try | 
 | 887 |     { | 
| Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 888 |         // 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 Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 897 |  | 
| Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 898 |         try | 
 | 899 |         { | 
 | 900 |             dataIface.getHWCalloutFields(inventoryPath, fn, ccin, sn); | 
 | 901 |  | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 902 |             CalloutPriority p = | 
 | 903 |                 priority ? priority.value() : CalloutPriority::high; | 
| Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 904 |  | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 905 |             callout = | 
 | 906 |                 std::make_unique<src::Callout>(p, locCode, fn, ccin, sn, mrus); | 
| Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 907 |         } | 
| Patrick Williams | 45e8352 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 908 |         catch (const sdbusplus::exception_t& e) | 
| Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 909 |         { | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 910 |             std::string msg = | 
 | 911 |                 "No VPD found for " + inventoryPath + ": " + e.what(); | 
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 912 |             addDebugData(msg); | 
| Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 913 |  | 
 | 914 |             // Just create the callout with empty FRU fields | 
| Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 915 |             callout = std::make_unique<src::Callout>( | 
 | 916 |                 CalloutPriority::high, locCode, fn, ccin, sn, mrus); | 
| Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 917 |         } | 
| Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 918 |     } | 
| Patrick Williams | 45e8352 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 919 |     catch (const sdbusplus::exception_t& e) | 
| Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 920 |     { | 
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 921 |         std::string msg = "Could not get location code for " + inventoryPath + | 
 | 922 |                           ": " + e.what(); | 
 | 923 |         addDebugData(msg); | 
| Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 924 |  | 
| Matt Spinler | 479b692 | 2021-08-17 16:34:59 -0500 | [diff] [blame] | 925 |         // 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 Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 936 |     } | 
 | 937 |  | 
| Matt Spinler | 479b692 | 2021-08-17 16:34:59 -0500 | [diff] [blame] | 938 |     if (callout) | 
 | 939 |     { | 
 | 940 |         createCalloutsObject(); | 
 | 941 |         _callouts->addCallout(std::move(callout)); | 
 | 942 |     } | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 943 | } | 
| Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 944 |  | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 945 | std::vector<message::RegistryCallout> SRC::getRegistryCallouts( | 
 | 946 |     const message::Entry& regEntry, const AdditionalData& additionalData, | 
 | 947 |     const DataInterfaceBase& dataIface) | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 948 | { | 
 | 949 |     std::vector<message::RegistryCallout> registryCallouts; | 
 | 950 |  | 
 | 951 |     if (regEntry.callouts) | 
 | 952 |     { | 
| Matt Spinler | 9a50c8d | 2021-04-12 14:22:26 -0500 | [diff] [blame] | 953 |         std::vector<std::string> systemNames; | 
 | 954 |  | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 955 |         try | 
 | 956 |         { | 
| Matt Spinler | 9a50c8d | 2021-04-12 14:22:26 -0500 | [diff] [blame] | 957 |             systemNames = dataIface.getSystemNames(); | 
 | 958 |         } | 
 | 959 |         catch (const std::exception& e) | 
 | 960 |         { | 
 | 961 |             // Compatible interface not available yet | 
 | 962 |         } | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 963 |  | 
| Matt Spinler | 9a50c8d | 2021-04-12 14:22:26 -0500 | [diff] [blame] | 964 |         try | 
 | 965 |         { | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 966 |             registryCallouts = message::Registry::getCallouts( | 
 | 967 |                 regEntry.callouts.value(), systemNames, additionalData); | 
 | 968 |         } | 
 | 969 |         catch (const std::exception& e) | 
 | 970 |         { | 
| Jayanth Othayoth | 1aa90d4 | 2023-09-13 04:25:45 -0500 | [diff] [blame] | 971 |             addDebugData(std::format( | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 972 |                 "Error parsing PEL message registry callout JSON: {}", | 
 | 973 |                 e.what())); | 
 | 974 |         } | 
 | 975 |     } | 
 | 976 |  | 
 | 977 |     return registryCallouts; | 
 | 978 | } | 
 | 979 |  | 
 | 980 | void SRC::addRegistryCallouts( | 
 | 981 |     const std::vector<message::RegistryCallout>& callouts, | 
 | 982 |     const DataInterfaceBase& dataIface, | 
 | 983 |     std::optional<std::string> trustedSymbolicFRUInvPath) | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 984 | { | 
 | 985 |     try | 
 | 986 |     { | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 987 |         for (const auto& callout : callouts) | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 988 |         { | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 989 |             addRegistryCallout(callout, dataIface, trustedSymbolicFRUInvPath); | 
 | 990 |  | 
 | 991 |             // Only the first callout gets the inventory path | 
 | 992 |             if (trustedSymbolicFRUInvPath) | 
 | 993 |             { | 
 | 994 |                 trustedSymbolicFRUInvPath = std::nullopt; | 
 | 995 |             } | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 996 |         } | 
 | 997 |     } | 
| Patrick Williams | 66491c6 | 2021-10-06 12:23:37 -0500 | [diff] [blame] | 998 |     catch (const std::exception& e) | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 999 |     { | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 1000 |         std::string msg = | 
 | 1001 |             "Error parsing PEL message registry callout JSON: "s + e.what(); | 
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 1002 |         addDebugData(msg); | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1003 |     } | 
 | 1004 | } | 
 | 1005 |  | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 1006 | void SRC::addRegistryCallout( | 
 | 1007 |     const message::RegistryCallout& regCallout, | 
 | 1008 |     const DataInterfaceBase& dataIface, | 
 | 1009 |     const std::optional<std::string>& trustedSymbolicFRUInvPath) | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1010 | { | 
 | 1011 |     std::unique_ptr<src::Callout> callout; | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1012 |     auto locCode = regCallout.locCode; | 
| Matt Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 1013 |     bool locExpanded = true; | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1014 |  | 
| Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 1015 |     if (!locCode.empty()) | 
 | 1016 |     { | 
 | 1017 |         try | 
 | 1018 |         { | 
 | 1019 |             locCode = dataIface.expandLocationCode(locCode, 0); | 
 | 1020 |         } | 
 | 1021 |         catch (const std::exception& e) | 
 | 1022 |         { | 
| Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 1023 |             auto msg = "Unable to expand location code " + locCode + ": " + | 
 | 1024 |                        e.what(); | 
| Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 1025 |             addDebugData(msg); | 
| Matt Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 1026 |             locExpanded = false; | 
| Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 1027 |         } | 
 | 1028 |     } | 
 | 1029 |  | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1030 |     // Via the PEL values table, get the priority enum. | 
 | 1031 |     // The schema will have validated the priority was a valid value. | 
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 1032 |     auto priorityIt = | 
 | 1033 |         pv::findByName(regCallout.priority, pv::calloutPriorityValues); | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1034 |     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 Spinler | 2edce4e | 2024-01-17 11:13:51 -0600 | [diff] [blame] | 1041 |         callout = std::make_unique<src::Callout>(priority, regCallout.procedure, | 
 | 1042 |                                                  src::CalloutValueType::raw); | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1043 |     } | 
 | 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 Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 1053 |         bool trusted = false; | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1054 |  | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 1055 |         // 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 Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 1061 |                 trusted = true; | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 1062 |             } | 
 | 1063 |             catch (const std::exception& e) | 
 | 1064 |             { | 
 | 1065 |                 addDebugData( | 
| Jayanth Othayoth | 1aa90d4 | 2023-09-13 04:25:45 -0500 | [diff] [blame] | 1066 |                     std::format("Could not get location code for {}: {}", | 
| Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 1067 |                                 *trustedSymbolicFRUInvPath, e.what())); | 
 | 1068 |                 locCode.clear(); | 
 | 1069 |             } | 
 | 1070 |         } | 
 | 1071 |  | 
| Matt Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 1072 |         // 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 Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1078 |         // 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 Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 1081 |             priority, regCallout.symbolicFRUTrusted, locCode, trusted); | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1082 |     } | 
 | 1083 |     else | 
 | 1084 |     { | 
| Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 1085 |         // A hardware callout | 
| Matt Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 1086 |  | 
 | 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 Spinler | bad056b | 2023-01-25 14:16:57 -0600 | [diff] [blame] | 1095 |         std::vector<std::string> inventoryPaths; | 
| Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 1096 |  | 
 | 1097 |         try | 
 | 1098 |         { | 
 | 1099 |             // Get the inventory item from the unexpanded location code | 
| Matt Spinler | bad056b | 2023-01-25 14:16:57 -0600 | [diff] [blame] | 1100 |             inventoryPaths = | 
| Matt Spinler | 2f9225a | 2020-08-05 12:58:49 -0500 | [diff] [blame] | 1101 |                 dataIface.getInventoryFromLocCode(regCallout.locCode, 0, false); | 
| Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 1102 |         } | 
 | 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 Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 1109 |             if (!locCode.empty()) | 
 | 1110 |             { | 
 | 1111 |                 // Still add a callout with just the location code. | 
 | 1112 |                 addLocationCodeOnlyCallout(locCode, priority); | 
 | 1113 |             } | 
| Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 1114 |             return; | 
 | 1115 |         } | 
 | 1116 |  | 
| Matt Spinler | bad056b | 2023-01-25 14:16:57 -0600 | [diff] [blame] | 1117 |         // Just use first path returned since they all point to the same FRU. | 
 | 1118 |         addInventoryCallout(inventoryPaths[0], priority, locCode, dataIface); | 
| Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1119 |     } | 
 | 1120 |  | 
 | 1121 |     if (callout) | 
 | 1122 |     { | 
 | 1123 |         createCalloutsObject(); | 
 | 1124 |         _callouts->addCallout(std::move(callout)); | 
 | 1125 |     } | 
 | 1126 | } | 
| Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 1127 |  | 
| Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 1128 | void 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 Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 1172 |             std::string msg = | 
 | 1173 |                 "Invalid CALLOUT_IIC_BUS " + *i2cBus + " or CALLOUT_IIC_ADDR " + | 
 | 1174 |                 *i2cAddr + " in AdditionalData property"; | 
| Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 1175 |             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 Spinler | d7654dc | 2025-05-09 11:47:50 -0500 | [diff] [blame] | 1208 |                 auto msg = | 
 | 1209 |                     std::string{ | 
 | 1210 |                         "Invalid priority found in dev callout JSON: "} + | 
| Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 1211 |                     callout.priority[0]; | 
 | 1212 |                 addDebugData(msg); | 
 | 1213 |             } | 
 | 1214 |         } | 
 | 1215 |  | 
| Matt Spinler | 0d92b52 | 2021-06-16 13:28:17 -0600 | [diff] [blame] | 1216 |         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 Othayoth | 1aa90d4 | 2023-09-13 04:25:45 -0500 | [diff] [blame] | 1224 |             auto msg = std::format("Unable to expand location code {}: {}", | 
| Matt Spinler | 0d92b52 | 2021-06-16 13:28:17 -0600 | [diff] [blame] | 1225 |                                    callout.locationCode, e.what()); | 
 | 1226 |             addDebugData(msg); | 
| Matt Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 1227 |  | 
 | 1228 |             // Add the callout with just the unexpanded location code. | 
 | 1229 |             addLocationCodeOnlyCallout(callout.locationCode, priority); | 
 | 1230 |             continue; | 
| Matt Spinler | 0d92b52 | 2021-06-16 13:28:17 -0600 | [diff] [blame] | 1231 |         } | 
 | 1232 |  | 
| Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 1233 |         try | 
 | 1234 |         { | 
| Matt Spinler | bad056b | 2023-01-25 14:16:57 -0600 | [diff] [blame] | 1235 |             auto inventoryPaths = dataIface.getInventoryFromLocCode( | 
| Matt Spinler | 2f9225a | 2020-08-05 12:58:49 -0500 | [diff] [blame] | 1236 |                 callout.locationCode, 0, false); | 
| Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 1237 |  | 
| Matt Spinler | bad056b | 2023-01-25 14:16:57 -0600 | [diff] [blame] | 1238 |             // Just use first path returned since they all | 
 | 1239 |             // point to the same FRU. | 
 | 1240 |             addInventoryCallout(inventoryPaths[0], priority, locCode, | 
 | 1241 |                                 dataIface); | 
| Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 1242 |         } | 
 | 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 Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 1249 |             // Add the callout with just the location code. | 
 | 1250 |             addLocationCodeOnlyCallout(callout.locationCode, priority); | 
 | 1251 |             continue; | 
| Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 1252 |         } | 
 | 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 Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 1272 | void 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 Othayoth | 1aa90d4 | 2023-09-13 04:25:45 -0500 | [diff] [blame] | 1294 |             addDebugData(std::format( | 
| Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 1295 |                 "Failed extracting callout data from JSON: {}", e.what())); | 
 | 1296 |         } | 
 | 1297 |     } | 
 | 1298 | } | 
 | 1299 |  | 
 | 1300 | void SRC::addJSONCallout(const nlohmann::json& jsonCallout, | 
 | 1301 |                          const DataInterfaceBase& dataIface) | 
 | 1302 | { | 
| Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1303 |     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 Othayoth | 1aa90d4 | 2023-09-13 04:25:45 -0500 | [diff] [blame] | 1319 |             addDebugData(std::format("Unable to expand location code {}: {}", | 
| Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1320 |                                      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 Spinler | 3c7ec6d | 2022-05-06 08:50:20 -0500 | [diff] [blame] | 1331 |         // 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 Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1338 |         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 Spinler | 3c7ec6d | 2022-05-06 08:50:20 -0500 | [diff] [blame] | 1346 |         // 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 Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1352 |         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 Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 1366 |         std::vector<src::MRU::MRUCallout> mrus; | 
| Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1367 |  | 
 | 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 Spinler | bad056b | 2023-01-25 14:16:57 -0600 | [diff] [blame] | 1382 |                 auto inventoryPaths = dataIface.getInventoryFromLocCode( | 
| Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1383 |                     unexpandedLocCode, 0, false); | 
| Matt Spinler | bad056b | 2023-01-25 14:16:57 -0600 | [diff] [blame] | 1384 |                 // Just use first path returned since they all | 
 | 1385 |                 // point to the same FRU. | 
 | 1386 |                 inventoryPath = inventoryPaths[0]; | 
| Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1387 |             } | 
 | 1388 |             catch (const std::exception& e) | 
 | 1389 |             { | 
| Matt Spinler | 7b92372 | 2025-03-19 13:17:23 -0500 | [diff] [blame] | 1390 |                 addDebugData(std::format("Unable to get inventory path from " | 
 | 1391 |                                          "location code: {}: {}", | 
 | 1392 |                                          unexpandedLocCode, e.what())); | 
 | 1393 |                 addLocationCodeOnlyCallout(locCode, priority); | 
 | 1394 |                 return; | 
| Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1395 |             } | 
 | 1396 |         } | 
 | 1397 |  | 
| Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 1398 |         if (jsonCallout.contains("MRUs")) | 
 | 1399 |         { | 
 | 1400 |             mrus = getMRUsFromJSON(jsonCallout.at("MRUs")); | 
 | 1401 |         } | 
 | 1402 |  | 
| Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1403 |         // 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 Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 1411 |         addInventoryCallout(inventoryPath, priority, lc, dataIface, mrus); | 
| Matt Spinler | afa2c79 | 2020-08-27 11:01:39 -0500 | [diff] [blame] | 1412 |  | 
 | 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 Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1428 |     } | 
 | 1429 |  | 
 | 1430 |     if (callout) | 
 | 1431 |     { | 
 | 1432 |         createCalloutsObject(); | 
 | 1433 |         _callouts->addCallout(std::move(callout)); | 
 | 1434 |     } | 
 | 1435 | } | 
 | 1436 |  | 
 | 1437 | CalloutPriority 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 Othayoth | 1aa90d4 | 2023-09-13 04:25:45 -0500 | [diff] [blame] | 1457 |             std::format("Invalid priority '{}' found in JSON callout", p)}; | 
| Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1458 |     } | 
 | 1459 |  | 
 | 1460 |     return priority; | 
| Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 1461 | } | 
 | 1462 |  | 
| Patrick Williams | 2529115 | 2025-02-01 08:21:42 -0500 | [diff] [blame] | 1463 | std::vector<src::MRU::MRUCallout> SRC::getMRUsFromJSON( | 
 | 1464 |     const nlohmann::json& mruJSON) | 
| Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 1465 | { | 
 | 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 Othayoth | 1aa90d4 | 2023-09-13 04:25:45 -0500 | [diff] [blame] | 1493 |             addDebugData(std::format("Invalid MRU entry in JSON: {}: {}", | 
| Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 1494 |                                      mruCallout.dump(), e.what())); | 
 | 1495 |         } | 
 | 1496 |     } | 
 | 1497 |  | 
 | 1498 |     return mrus; | 
 | 1499 | } | 
 | 1500 |  | 
| Sumit Kumar | 3e27443 | 2021-09-14 06:37:56 -0500 | [diff] [blame] | 1501 | std::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 Lobo | 875b6c7 | 2021-10-20 17:38:56 -0500 | [diff] [blame] | 1542 | void 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 Spinler | 0bacc8e | 2023-07-07 16:25:39 -0500 | [diff] [blame] | 1552 |         lg2::error("Error getting progress code: {ERROR}", "ERROR", e); | 
| Vijay Lobo | 875b6c7 | 2021-10-20 17:38:56 -0500 | [diff] [blame] | 1553 |         return; | 
 | 1554 |     } | 
 | 1555 |  | 
 | 1556 |     _hexData[2] = getProgressCode(progressSRC); | 
 | 1557 | } | 
 | 1558 |  | 
 | 1559 | uint32_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 Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 1580 |                             return std::isxdigit(static_cast<unsigned char>(c)); | 
 | 1581 |                         })) | 
| Vijay Lobo | 875b6c7 | 2021-10-20 17:38:56 -0500 | [diff] [blame] | 1582 |         { | 
 | 1583 |             progressCode = std::stoul(progressCodeString, nullptr, 16); | 
 | 1584 |         } | 
 | 1585 |     } | 
 | 1586 |     catch (const std::exception& e) | 
 | 1587 |     {} | 
 | 1588 |  | 
 | 1589 |     return progressCode; | 
 | 1590 | } | 
 | 1591 |  | 
| Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 1592 | } // namespace pels | 
 | 1593 | } // namespace openpower |