Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 16 | #include "src.hpp" |
| 17 | |
Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 18 | #include "device_callouts.hpp" |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 19 | #include "json_utils.hpp" |
| 20 | #include "paths.hpp" |
| 21 | #include "pel_values.hpp" |
Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 22 | #ifdef PELTOOL |
| 23 | #include <Python.h> |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 24 | |
Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 25 | #include <fifo_map.hpp> |
| 26 | #include <nlohmann/json.hpp> |
| 27 | #include <sstream> |
| 28 | #endif |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 29 | #include <fmt/format.h> |
| 30 | |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 31 | #include <phosphor-logging/log.hpp> |
| 32 | |
| 33 | namespace openpower |
| 34 | { |
| 35 | namespace pels |
| 36 | { |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 37 | namespace pv = openpower::pels::pel_values; |
| 38 | namespace rg = openpower::pels::message; |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 39 | using namespace phosphor::logging; |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 40 | using namespace std::string_literals; |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 41 | |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 42 | constexpr size_t ccinSize = 4; |
| 43 | |
Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 44 | #ifdef PELTOOL |
| 45 | // Use fifo_map as nlohmann::json's map. We are just ignoring the 'less' |
| 46 | // compare. With this map the keys are kept in FIFO order. |
| 47 | template <class K, class V, class dummy_compare, class A> |
| 48 | using fifoMap = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>; |
| 49 | using fifoJSON = nlohmann::basic_json<fifoMap>; |
| 50 | |
| 51 | void pyDecRef(PyObject* pyObj) |
| 52 | { |
| 53 | Py_XDECREF(pyObj); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @brief Returns a JSON string to append to SRC section. |
| 58 | * |
| 59 | * The returning string will contain a JSON object, but without |
| 60 | * the outer {}. If the input JSON isn't a JSON object (dict), then |
| 61 | * one will be created with the input added to a 'SRC Details' key. |
| 62 | * |
| 63 | * @param[in] json - The JSON to convert to a string |
| 64 | * |
| 65 | * @return std::string - The JSON string |
| 66 | */ |
| 67 | std::string prettyJSON(const fifoJSON& json) |
| 68 | { |
| 69 | fifoJSON output; |
| 70 | if (!json.is_object()) |
| 71 | { |
| 72 | output["SRC Details"] = json; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | for (const auto& [key, value] : json.items()) |
| 77 | { |
| 78 | output[key] = value; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Let nlohmann do the pretty printing. |
| 83 | std::stringstream stream; |
| 84 | stream << std::setw(4) << output; |
| 85 | |
| 86 | auto jsonString = stream.str(); |
| 87 | |
| 88 | // Now it looks like: |
| 89 | // { |
| 90 | // "Key": "Value", |
| 91 | // ... |
| 92 | // } |
| 93 | |
| 94 | // Replace the { and the following newline, and the } and its |
| 95 | // preceeding newline. |
| 96 | jsonString.erase(0, 2); |
| 97 | |
| 98 | auto pos = jsonString.find_last_of('}'); |
| 99 | jsonString.erase(pos - 1); |
| 100 | |
| 101 | return jsonString; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @brief Call Python modules to parse the data into a JSON string |
| 106 | * |
| 107 | * The module to call is based on the Creator Subsystem ID under the namespace |
| 108 | * "srcparsers". For example: "srcparsers.xsrc.xsrc" where "x" is the Creator |
| 109 | * Subsystem ID in ASCII lowercase. |
| 110 | * |
| 111 | * All modules must provide the following: |
| 112 | * Function: parseSRCToJson |
| 113 | * Argument list: |
| 114 | * 1. (str) ASCII string (Hex Word 1) |
| 115 | * 2. (str) Hex Word 2 |
| 116 | * 3. (str) Hex Word 3 |
| 117 | * 4. (str) Hex Word 4 |
| 118 | * 5. (str) Hex Word 5 |
| 119 | * 6. (str) Hex Word 6 |
| 120 | * 7. (str) Hex Word 7 |
| 121 | * 8. (str) Hex Word 8 |
| 122 | * 9. (str) Hex Word 9 |
| 123 | *-Return data: |
| 124 | * 1. (str) JSON string |
| 125 | * |
| 126 | * @param[in] hexwords - Vector of strings of Hexwords 1-9 |
| 127 | * @param[in] creatorID - The creatorID from the Private Header section |
| 128 | * @return std::optional<std::string> - The JSON string if it could be created, |
| 129 | * else std::nullopt |
| 130 | */ |
| 131 | std::optional<std::string> getPythonJSON(std::vector<std::string>& hexwords, |
| 132 | uint8_t creatorID) |
| 133 | { |
| 134 | PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pResult, *pBytes, |
| 135 | *eType, *eValue, *eTraceback; |
| 136 | std::string pErrStr; |
| 137 | std::string module = getNumberString("%c", tolower(creatorID)) + "src"; |
| 138 | pName = PyUnicode_FromString( |
| 139 | std::string("srcparsers." + module + "." + module).c_str()); |
| 140 | std::unique_ptr<PyObject, decltype(&pyDecRef)> modNamePtr(pName, &pyDecRef); |
| 141 | pModule = PyImport_Import(pName); |
| 142 | std::unique_ptr<PyObject, decltype(&pyDecRef)> modPtr(pModule, &pyDecRef); |
| 143 | if (pModule == NULL) |
| 144 | { |
| 145 | pErrStr = "No error string found"; |
| 146 | PyErr_Fetch(&eType, &eValue, &eTraceback); |
| 147 | if (eValue) |
| 148 | { |
| 149 | PyObject* pStr = PyObject_Str(eValue); |
| 150 | if (pStr) |
| 151 | { |
| 152 | pErrStr = PyUnicode_AsUTF8(pStr); |
| 153 | } |
| 154 | Py_XDECREF(pStr); |
| 155 | } |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | pDict = PyModule_GetDict(pModule); |
| 160 | pFunc = PyDict_GetItemString(pDict, "parseSRCToJson"); |
| 161 | if (PyCallable_Check(pFunc)) |
| 162 | { |
| 163 | pArgs = PyTuple_New(9); |
| 164 | std::unique_ptr<PyObject, decltype(&pyDecRef)> argPtr(pArgs, |
| 165 | &pyDecRef); |
| 166 | for (size_t i = 0; i < 9; i++) |
| 167 | { |
| 168 | if (i < hexwords.size()) |
| 169 | { |
| 170 | auto arg = hexwords[i]; |
| 171 | PyTuple_SetItem(pArgs, i, |
| 172 | Py_BuildValue("s#", arg.c_str(), 8)); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | PyTuple_SetItem(pArgs, i, Py_BuildValue("s", "00000000")); |
| 177 | } |
| 178 | } |
| 179 | pResult = PyObject_CallObject(pFunc, pArgs); |
| 180 | std::unique_ptr<PyObject, decltype(&pyDecRef)> resPtr(pResult, |
| 181 | &pyDecRef); |
| 182 | if (pResult) |
| 183 | { |
| 184 | pBytes = PyUnicode_AsEncodedString(pResult, "utf-8", "~E~"); |
| 185 | std::unique_ptr<PyObject, decltype(&pyDecRef)> pyBytePtr( |
| 186 | pBytes, &pyDecRef); |
| 187 | const char* output = PyBytes_AS_STRING(pBytes); |
| 188 | try |
| 189 | { |
| 190 | fifoJSON json = nlohmann::json::parse(output); |
| 191 | return prettyJSON(json); |
| 192 | } |
| 193 | catch (std::exception& e) |
| 194 | { |
| 195 | log<level::ERR>("Bad JSON from parser", |
| 196 | entry("ERROR=%s", e.what()), |
| 197 | entry("SRC=%s", hexwords.front().c_str()), |
| 198 | entry("PARSER_MODULE=%s", module.c_str())); |
| 199 | return std::nullopt; |
| 200 | } |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | pErrStr = "No error string found"; |
| 205 | PyErr_Fetch(&eType, &eValue, &eTraceback); |
| 206 | if (eValue) |
| 207 | { |
| 208 | PyObject* pStr = PyObject_Str(eValue); |
| 209 | if (pStr) |
| 210 | { |
| 211 | pErrStr = PyUnicode_AsUTF8(pStr); |
| 212 | } |
| 213 | Py_XDECREF(pStr); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | if (!pErrStr.empty()) |
| 219 | { |
| 220 | log<level::ERR>("Python exception thrown by parser", |
| 221 | entry("ERROR=%s", pErrStr.c_str()), |
| 222 | entry("SRC=%s", hexwords.front().c_str()), |
| 223 | entry("PARSER_MODULE=%s", module.c_str())); |
| 224 | } |
| 225 | Py_XDECREF(eType); |
| 226 | Py_XDECREF(eValue); |
| 227 | Py_XDECREF(eTraceback); |
| 228 | return std::nullopt; |
| 229 | } |
| 230 | #endif |
| 231 | |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 232 | void SRC::unflatten(Stream& stream) |
| 233 | { |
| 234 | stream >> _header >> _version >> _flags >> _reserved1B >> _wordCount >> |
| 235 | _reserved2B >> _size; |
| 236 | |
| 237 | for (auto& word : _hexData) |
| 238 | { |
| 239 | stream >> word; |
| 240 | } |
| 241 | |
| 242 | _asciiString = std::make_unique<src::AsciiString>(stream); |
| 243 | |
| 244 | if (hasAdditionalSections()) |
| 245 | { |
| 246 | // The callouts section is currently the only extra subsection type |
| 247 | _callouts = std::make_unique<src::Callouts>(stream); |
| 248 | } |
| 249 | } |
| 250 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 251 | void SRC::flatten(Stream& stream) const |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 252 | { |
| 253 | stream << _header << _version << _flags << _reserved1B << _wordCount |
| 254 | << _reserved2B << _size; |
| 255 | |
| 256 | for (auto& word : _hexData) |
| 257 | { |
| 258 | stream << word; |
| 259 | } |
| 260 | |
| 261 | _asciiString->flatten(stream); |
| 262 | |
| 263 | if (_callouts) |
| 264 | { |
| 265 | _callouts->flatten(stream); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | SRC::SRC(Stream& pel) |
| 270 | { |
| 271 | try |
| 272 | { |
| 273 | unflatten(pel); |
| 274 | validate(); |
| 275 | } |
| 276 | catch (const std::exception& e) |
| 277 | { |
| 278 | log<level::ERR>("Cannot unflatten SRC", entry("ERROR=%s", e.what())); |
| 279 | _valid = false; |
| 280 | } |
| 281 | } |
| 282 | |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 283 | SRC::SRC(const message::Entry& regEntry, const AdditionalData& additionalData, |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 284 | const nlohmann::json& jsonCallouts, const DataInterfaceBase& dataIface) |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 285 | { |
| 286 | _header.id = static_cast<uint16_t>(SectionID::primarySRC); |
| 287 | _header.version = srcSectionVersion; |
| 288 | _header.subType = srcSectionSubtype; |
| 289 | _header.componentID = regEntry.componentID; |
| 290 | |
| 291 | _version = srcVersion; |
| 292 | |
| 293 | _flags = 0; |
Vijay Lobo | f3702bb | 2021-04-09 15:10:19 -0500 | [diff] [blame] | 294 | |
| 295 | auto item = additionalData.getValue("POWER_THERMAL_CRITICAL_FAULT"); |
| 296 | if ((regEntry.src.powerFault.value_or(false)) || |
| 297 | (item.value_or("") == "TRUE")) |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 298 | { |
| 299 | _flags |= powerFaultEvent; |
| 300 | } |
| 301 | |
| 302 | _reserved1B = 0; |
| 303 | |
| 304 | _wordCount = numSRCHexDataWords + 1; |
| 305 | |
| 306 | _reserved2B = 0; |
| 307 | |
| 308 | // There are multiple fields encoded in the hex data words. |
| 309 | std::for_each(_hexData.begin(), _hexData.end(), |
| 310 | [](auto& word) { word = 0; }); |
Matt Spinler | 7c61918 | 2020-07-27 15:15:11 -0500 | [diff] [blame] | 311 | |
| 312 | // Hex Word 2 Nibbles: |
| 313 | // MIGVEPFF |
| 314 | // M: Partition dump status = 0 |
| 315 | // I: System boot state = TODO |
| 316 | // G: Partition Boot type = 0 |
| 317 | // V: BMC dump status = TODO |
| 318 | // E: Platform boot mode = 0 (side = temporary, speed = fast) |
| 319 | // P: Platform dump status = TODO |
| 320 | // FF: SRC format, set below |
| 321 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 322 | setBMCFormat(); |
| 323 | setBMCPosition(); |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 324 | setMotherboardCCIN(dataIface); |
| 325 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 326 | // Fill in the last 4 words from the AdditionalData property contents. |
| 327 | setUserDefinedHexWords(regEntry, additionalData); |
| 328 | |
| 329 | _asciiString = std::make_unique<src::AsciiString>(regEntry); |
| 330 | |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 331 | addCallouts(regEntry, additionalData, jsonCallouts, dataIface); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 332 | |
| 333 | _size = baseSRCSize; |
| 334 | _size += _callouts ? _callouts->flattenedSize() : 0; |
| 335 | _header.size = Section::flattenedSize() + _size; |
| 336 | |
| 337 | _valid = true; |
| 338 | } |
| 339 | |
| 340 | void SRC::setUserDefinedHexWords(const message::Entry& regEntry, |
| 341 | const AdditionalData& ad) |
| 342 | { |
| 343 | if (!regEntry.src.hexwordADFields) |
| 344 | { |
| 345 | return; |
| 346 | } |
| 347 | |
Harisuddin Mohamed Isa | 1a1b0df | 2020-11-23 16:34:36 +0800 | [diff] [blame] | 348 | // Save the AdditionalData value corresponding to the first element of |
| 349 | // adName tuple into _hexData[wordNum]. |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 350 | for (const auto& [wordNum, adName] : *regEntry.src.hexwordADFields) |
| 351 | { |
| 352 | // Can only set words 6 - 9 |
| 353 | if (!isUserDefinedWord(wordNum)) |
| 354 | { |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 355 | std::string msg = |
| 356 | "SRC user data word out of range: " + std::to_string(wordNum); |
| 357 | addDebugData(msg); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 358 | continue; |
| 359 | } |
| 360 | |
Harisuddin Mohamed Isa | 1a1b0df | 2020-11-23 16:34:36 +0800 | [diff] [blame] | 361 | auto value = ad.getValue(std::get<0>(adName)); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 362 | if (value) |
| 363 | { |
| 364 | _hexData[getWordIndexFromWordNum(wordNum)] = |
| 365 | std::strtoul(value.value().c_str(), nullptr, 0); |
| 366 | } |
| 367 | else |
| 368 | { |
Harisuddin Mohamed Isa | 1a1b0df | 2020-11-23 16:34:36 +0800 | [diff] [blame] | 369 | std::string msg = "Source for user data SRC word not found: " + |
| 370 | std::get<0>(adName); |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 371 | addDebugData(msg); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 376 | void SRC::setMotherboardCCIN(const DataInterfaceBase& dataIface) |
| 377 | { |
| 378 | uint32_t ccin = 0; |
| 379 | auto ccinString = dataIface.getMotherboardCCIN(); |
| 380 | |
| 381 | try |
| 382 | { |
| 383 | if (ccinString.size() == ccinSize) |
| 384 | { |
| 385 | ccin = std::stoi(ccinString, 0, 16); |
| 386 | } |
| 387 | } |
| 388 | catch (std::exception& e) |
| 389 | { |
| 390 | log<level::WARNING>("Could not convert motherboard CCIN to a number", |
| 391 | entry("CCIN=%s", ccinString.c_str())); |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | // Set the first 2 bytes |
| 396 | _hexData[1] |= ccin << 16; |
| 397 | } |
| 398 | |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 399 | void SRC::validate() |
| 400 | { |
| 401 | bool failed = false; |
| 402 | |
| 403 | if ((header().id != static_cast<uint16_t>(SectionID::primarySRC)) && |
| 404 | (header().id != static_cast<uint16_t>(SectionID::secondarySRC))) |
| 405 | { |
| 406 | log<level::ERR>("Invalid SRC section ID", |
| 407 | entry("ID=0x%X", header().id)); |
| 408 | failed = true; |
| 409 | } |
| 410 | |
| 411 | // Check the version in the SRC, not in the header |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 412 | if (_version != srcVersion) |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 413 | { |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 414 | log<level::ERR>("Invalid SRC version", entry("VERSION=0x%X", _version)); |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 415 | failed = true; |
| 416 | } |
| 417 | |
| 418 | _valid = failed ? false : true; |
| 419 | } |
| 420 | |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 421 | bool SRC::isBMCSRC() const |
| 422 | { |
| 423 | auto as = asciiString(); |
| 424 | if (as.length() >= 2) |
| 425 | { |
| 426 | uint8_t errorType = strtoul(as.substr(0, 2).c_str(), nullptr, 16); |
| 427 | return (errorType == static_cast<uint8_t>(SRCType::bmcError) || |
| 428 | errorType == static_cast<uint8_t>(SRCType::powerError)); |
| 429 | } |
| 430 | return false; |
| 431 | } |
| 432 | |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 433 | std::optional<std::string> SRC::getErrorDetails(message::Registry& registry, |
| 434 | DetailLevel type, |
| 435 | bool toCache) const |
| 436 | { |
| 437 | const std::string jsonIndent(indentLevel, 0x20); |
| 438 | std::string errorOut; |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 439 | if (isBMCSRC()) |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 440 | { |
| 441 | auto entry = registry.lookup("0x" + asciiString().substr(4, 4), |
| 442 | rg::LookupType::reasonCode, toCache); |
| 443 | if (entry) |
| 444 | { |
| 445 | errorOut.append(jsonIndent + "\"Error Details\": {\n"); |
| 446 | auto errorMsg = getErrorMessage(*entry); |
| 447 | if (errorMsg) |
| 448 | { |
| 449 | if (type == DetailLevel::message) |
| 450 | { |
| 451 | return errorMsg.value(); |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | jsonInsert(errorOut, "Message", errorMsg.value(), 2); |
| 456 | } |
| 457 | } |
| 458 | if (entry->src.hexwordADFields) |
| 459 | { |
Harisuddin Mohamed Isa | 1a1b0df | 2020-11-23 16:34:36 +0800 | [diff] [blame] | 460 | std::map<size_t, std::tuple<std::string, std::string>> |
| 461 | adFields = entry->src.hexwordADFields.value(); |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 462 | for (const auto& hexwordMap : adFields) |
| 463 | { |
Harisuddin Mohamed Isa | 1a1b0df | 2020-11-23 16:34:36 +0800 | [diff] [blame] | 464 | std::vector<std::string> valueDescr; |
| 465 | valueDescr.push_back(getNumberString( |
| 466 | "0x%X", |
| 467 | _hexData[getWordIndexFromWordNum(hexwordMap.first)])); |
| 468 | valueDescr.push_back(std::get<1>(hexwordMap.second)); |
| 469 | jsonInsertArray(errorOut, std::get<0>(hexwordMap.second), |
| 470 | valueDescr, 2); |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | errorOut.erase(errorOut.size() - 2); |
| 474 | errorOut.append("\n"); |
| 475 | errorOut.append(jsonIndent + "},\n"); |
| 476 | return errorOut; |
| 477 | } |
| 478 | } |
| 479 | return std::nullopt; |
| 480 | } |
| 481 | |
| 482 | std::optional<std::string> |
| 483 | SRC::getErrorMessage(const message::Entry& regEntry) const |
| 484 | { |
| 485 | try |
| 486 | { |
| 487 | if (regEntry.doc.messageArgSources) |
| 488 | { |
| 489 | size_t msgLen = regEntry.doc.message.length(); |
| 490 | char msg[msgLen + 1]; |
| 491 | strcpy(msg, regEntry.doc.message.c_str()); |
| 492 | std::vector<uint32_t> argSourceVals; |
| 493 | std::string message; |
| 494 | const auto& argValues = regEntry.doc.messageArgSources.value(); |
| 495 | for (size_t i = 0; i < argValues.size(); ++i) |
| 496 | { |
| 497 | argSourceVals.push_back(_hexData[getWordIndexFromWordNum( |
| 498 | argValues[i].back() - '0')]); |
| 499 | } |
| 500 | const char* msgPointer = msg; |
| 501 | while (*msgPointer) |
| 502 | { |
| 503 | if (*msgPointer == '%') |
| 504 | { |
| 505 | msgPointer++; |
| 506 | size_t wordIndex = *msgPointer - '0'; |
| 507 | if (isdigit(*msgPointer) && wordIndex >= 1 && |
| 508 | static_cast<uint16_t>(wordIndex) <= |
| 509 | argSourceVals.size()) |
| 510 | { |
| 511 | message.append(getNumberString( |
| 512 | "0x%X", argSourceVals[wordIndex - 1])); |
| 513 | } |
| 514 | else |
| 515 | { |
| 516 | message.append("%" + std::string(1, *msgPointer)); |
| 517 | } |
| 518 | } |
| 519 | else |
| 520 | { |
| 521 | message.push_back(*msgPointer); |
| 522 | } |
| 523 | msgPointer++; |
| 524 | } |
| 525 | return message; |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | return regEntry.doc.message; |
| 530 | } |
| 531 | } |
| 532 | catch (const std::exception& e) |
| 533 | { |
| 534 | log<level::ERR>("Cannot get error message from registry entry", |
| 535 | entry("ERROR=%s", e.what())); |
| 536 | } |
| 537 | return std::nullopt; |
| 538 | } |
| 539 | |
| 540 | std::optional<std::string> SRC::getCallouts() const |
| 541 | { |
| 542 | if (!_callouts) |
| 543 | { |
| 544 | return std::nullopt; |
| 545 | } |
| 546 | std::string printOut; |
| 547 | const std::string jsonIndent(indentLevel, 0x20); |
| 548 | const auto& callout = _callouts->callouts(); |
| 549 | const auto& compDescrp = pv::failingComponentType; |
| 550 | printOut.append(jsonIndent + "\"Callout Section\": {\n"); |
| 551 | jsonInsert(printOut, "Callout Count", std::to_string(callout.size()), 2); |
| 552 | printOut.append(jsonIndent + jsonIndent + "\"Callouts\": ["); |
| 553 | for (auto& entry : callout) |
| 554 | { |
| 555 | printOut.append("{\n"); |
| 556 | if (entry->fruIdentity()) |
| 557 | { |
| 558 | jsonInsert( |
| 559 | printOut, "FRU Type", |
| 560 | compDescrp.at(entry->fruIdentity()->failingComponentType()), 3); |
| 561 | jsonInsert(printOut, "Priority", |
| 562 | pv::getValue(entry->priority(), |
| 563 | pel_values::calloutPriorityValues), |
| 564 | 3); |
| 565 | if (!entry->locationCode().empty()) |
| 566 | { |
| 567 | jsonInsert(printOut, "Location Code", entry->locationCode(), 3); |
| 568 | } |
| 569 | if (entry->fruIdentity()->getPN().has_value()) |
| 570 | { |
| 571 | jsonInsert(printOut, "Part Number", |
| 572 | entry->fruIdentity()->getPN().value(), 3); |
| 573 | } |
| 574 | if (entry->fruIdentity()->getMaintProc().has_value()) |
| 575 | { |
Matt Spinler | 9e8b49e | 2020-09-10 13:15:26 -0500 | [diff] [blame] | 576 | jsonInsert(printOut, "Procedure", |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 577 | entry->fruIdentity()->getMaintProc().value(), 3); |
| 578 | if (pv::procedureDesc.find( |
| 579 | entry->fruIdentity()->getMaintProc().value()) != |
| 580 | pv::procedureDesc.end()) |
| 581 | { |
| 582 | jsonInsert( |
| 583 | printOut, "Description", |
| 584 | pv::procedureDesc.at( |
| 585 | entry->fruIdentity()->getMaintProc().value()), |
| 586 | 3); |
| 587 | } |
| 588 | } |
| 589 | if (entry->fruIdentity()->getCCIN().has_value()) |
| 590 | { |
| 591 | jsonInsert(printOut, "CCIN", |
| 592 | entry->fruIdentity()->getCCIN().value(), 3); |
| 593 | } |
| 594 | if (entry->fruIdentity()->getSN().has_value()) |
| 595 | { |
| 596 | jsonInsert(printOut, "Serial Number", |
| 597 | entry->fruIdentity()->getSN().value(), 3); |
| 598 | } |
| 599 | } |
| 600 | if (entry->pceIdentity()) |
| 601 | { |
| 602 | const auto& pceIdentMtms = entry->pceIdentity()->mtms(); |
| 603 | if (!pceIdentMtms.machineTypeAndModel().empty()) |
| 604 | { |
| 605 | jsonInsert(printOut, "PCE MTMS", |
| 606 | pceIdentMtms.machineTypeAndModel() + "_" + |
| 607 | pceIdentMtms.machineSerialNumber(), |
| 608 | 3); |
| 609 | } |
| 610 | if (!entry->pceIdentity()->enclosureName().empty()) |
| 611 | { |
| 612 | jsonInsert(printOut, "PCE Name", |
| 613 | entry->pceIdentity()->enclosureName(), 3); |
| 614 | } |
| 615 | } |
| 616 | if (entry->mru()) |
| 617 | { |
| 618 | const auto& mruCallouts = entry->mru()->mrus(); |
| 619 | std::string mruId; |
| 620 | for (auto& element : mruCallouts) |
| 621 | { |
| 622 | if (!mruId.empty()) |
| 623 | { |
| 624 | mruId.append(", " + getNumberString("%08X", element.id)); |
| 625 | } |
| 626 | else |
| 627 | { |
| 628 | mruId.append(getNumberString("%08X", element.id)); |
| 629 | } |
| 630 | } |
| 631 | jsonInsert(printOut, "MRU Id", mruId, 3); |
| 632 | } |
| 633 | printOut.erase(printOut.size() - 2); |
| 634 | printOut.append("\n" + jsonIndent + jsonIndent + "}, "); |
| 635 | }; |
| 636 | printOut.erase(printOut.size() - 2); |
| 637 | printOut.append("]\n" + jsonIndent + "}"); |
| 638 | return printOut; |
| 639 | } |
| 640 | |
Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 641 | std::optional<std::string> SRC::getJSON(message::Registry& registry, |
| 642 | const std::vector<std::string>& plugins, |
| 643 | uint8_t creatorID) const |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 644 | { |
| 645 | std::string ps; |
Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 646 | std::vector<std::string> hexwords; |
Harisuddin Mohamed Isa | bebeb94 | 2020-03-12 17:12:24 +0800 | [diff] [blame] | 647 | jsonInsert(ps, pv::sectionVer, getNumberString("%d", _header.version), 1); |
| 648 | jsonInsert(ps, pv::subSection, getNumberString("%d", _header.subType), 1); |
| 649 | jsonInsert(ps, pv::createdBy, getNumberString("0x%X", _header.componentID), |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 650 | 1); |
| 651 | jsonInsert(ps, "SRC Version", getNumberString("0x%02X", _version), 1); |
Harisuddin Mohamed Isa | c32e551 | 2020-02-06 18:05:21 +0800 | [diff] [blame] | 652 | jsonInsert(ps, "SRC Format", getNumberString("0x%02X", _hexData[0] & 0xFF), |
| 653 | 1); |
| 654 | jsonInsert(ps, "Virtual Progress SRC", |
| 655 | pv::boolString.at(_flags & virtualProgressSRC), 1); |
| 656 | jsonInsert(ps, "I5/OS Service Event Bit", |
| 657 | pv::boolString.at(_flags & i5OSServiceEventBit), 1); |
| 658 | jsonInsert(ps, "Hypervisor Dump Initiated", |
| 659 | pv::boolString.at(_flags & hypDumpInit), 1); |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 660 | jsonInsert(ps, "Power Control Net Fault", |
| 661 | pv::boolString.at(isPowerFaultEvent()), 1); |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 662 | |
| 663 | if (isBMCSRC()) |
| 664 | { |
| 665 | std::string ccinString; |
| 666 | uint32_t ccin = _hexData[1] >> 16; |
| 667 | |
| 668 | if (ccin) |
| 669 | { |
| 670 | ccinString = getNumberString("%04X", ccin); |
| 671 | } |
| 672 | // The PEL spec calls it a backplane, so call it that here. |
| 673 | jsonInsert(ps, "Backplane CCIN", ccinString, 1); |
Matt Spinler | afa2c79 | 2020-08-27 11:01:39 -0500 | [diff] [blame] | 674 | |
| 675 | jsonInsert(ps, "Deconfigured", |
| 676 | pv::boolString.at( |
| 677 | _hexData[3] & |
| 678 | static_cast<uint32_t>(ErrorStatusFlags::deconfigured)), |
| 679 | 1); |
| 680 | |
| 681 | jsonInsert( |
| 682 | ps, "Guarded", |
| 683 | pv::boolString.at(_hexData[3] & |
| 684 | static_cast<uint32_t>(ErrorStatusFlags::guarded)), |
| 685 | 1); |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 686 | } |
| 687 | |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 688 | auto errorDetails = getErrorDetails(registry, DetailLevel::json, true); |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 689 | if (errorDetails) |
| 690 | { |
| 691 | ps.append(errorDetails.value()); |
| 692 | } |
| 693 | jsonInsert(ps, "Valid Word Count", getNumberString("0x%02X", _wordCount), |
| 694 | 1); |
| 695 | std::string refcode = asciiString(); |
Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 696 | hexwords.push_back(refcode); |
Harisuddin Mohamed Isa | fecaa57 | 2020-03-11 16:04:50 +0800 | [diff] [blame] | 697 | std::string extRefcode; |
| 698 | size_t pos = refcode.find(0x20); |
| 699 | if (pos != std::string::npos) |
| 700 | { |
| 701 | size_t nextPos = refcode.find_first_not_of(0x20, pos); |
| 702 | if (nextPos != std::string::npos) |
| 703 | { |
| 704 | extRefcode = trimEnd(refcode.substr(nextPos)); |
| 705 | } |
| 706 | refcode.erase(pos); |
| 707 | } |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 708 | jsonInsert(ps, "Reference Code", refcode, 1); |
Harisuddin Mohamed Isa | fecaa57 | 2020-03-11 16:04:50 +0800 | [diff] [blame] | 709 | if (!extRefcode.empty()) |
| 710 | { |
| 711 | jsonInsert(ps, "Extended Reference Code", extRefcode, 1); |
| 712 | } |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 713 | for (size_t i = 2; i <= _wordCount; i++) |
| 714 | { |
Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 715 | std::string tmpWord = |
| 716 | getNumberString("%08X", _hexData[getWordIndexFromWordNum(i)]); |
| 717 | jsonInsert(ps, "Hex Word " + std::to_string(i), tmpWord, 1); |
| 718 | hexwords.push_back(tmpWord); |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 719 | } |
| 720 | auto calloutJson = getCallouts(); |
| 721 | if (calloutJson) |
| 722 | { |
| 723 | ps.append(calloutJson.value()); |
Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 724 | ps.append(",\n"); |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 725 | } |
Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 726 | std::string subsystem = getNumberString("%c", tolower(creatorID)); |
| 727 | bool srcDetailExists = false; |
| 728 | #ifdef PELTOOL |
| 729 | if (std::find(plugins.begin(), plugins.end(), subsystem + "src") != |
| 730 | plugins.end()) |
| 731 | { |
| 732 | auto pyJson = getPythonJSON(hexwords, creatorID); |
| 733 | if (pyJson) |
| 734 | { |
| 735 | ps.append(pyJson.value()); |
| 736 | srcDetailExists = true; |
| 737 | } |
| 738 | } |
| 739 | #endif |
| 740 | if (!srcDetailExists) |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 741 | { |
| 742 | ps.erase(ps.size() - 2); |
| 743 | } |
| 744 | return ps; |
| 745 | } |
| 746 | |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 747 | void SRC::addCallouts(const message::Entry& regEntry, |
| 748 | const AdditionalData& additionalData, |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 749 | const nlohmann::json& jsonCallouts, |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 750 | const DataInterfaceBase& dataIface) |
| 751 | { |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 752 | auto registryCallouts = |
| 753 | getRegistryCallouts(regEntry, additionalData, dataIface); |
| 754 | |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 755 | auto item = additionalData.getValue("CALLOUT_INVENTORY_PATH"); |
Miguel Gomez | 53ef155 | 2020-10-14 21:16:32 +0000 | [diff] [blame] | 756 | auto priority = additionalData.getValue("CALLOUT_PRIORITY"); |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 757 | |
Miguel Gomez | 53ef155 | 2020-10-14 21:16:32 +0000 | [diff] [blame] | 758 | std::optional<CalloutPriority> calloutPriority; |
| 759 | |
| 760 | // Only H, M or L priority values. |
| 761 | if (priority && !(*priority).empty()) |
| 762 | { |
| 763 | uint8_t p = (*priority)[0]; |
| 764 | if (p == 'H' || p == 'M' || p == 'L') |
| 765 | { |
| 766 | calloutPriority = static_cast<CalloutPriority>(p); |
| 767 | } |
| 768 | } |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 769 | // If the first registry callout says to use the passed in inventory |
| 770 | // path to get the location code for a symbolic FRU callout with a |
| 771 | // trusted location code, then do not add the inventory path as a |
| 772 | // normal FRU callout. |
| 773 | bool useInvForSymbolicFRULocCode = |
| 774 | !registryCallouts.empty() && registryCallouts[0].useInventoryLocCode && |
| 775 | !registryCallouts[0].symbolicFRUTrusted.empty(); |
| 776 | |
| 777 | if (item && !useInvForSymbolicFRULocCode) |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 778 | { |
Miguel Gomez | 53ef155 | 2020-10-14 21:16:32 +0000 | [diff] [blame] | 779 | addInventoryCallout(*item, calloutPriority, std::nullopt, dataIface); |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 780 | } |
| 781 | |
Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 782 | addDevicePathCallouts(additionalData, dataIface); |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 783 | |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 784 | addRegistryCallouts(registryCallouts, dataIface, |
| 785 | (useInvForSymbolicFRULocCode) ? item : std::nullopt); |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 786 | |
| 787 | if (!jsonCallouts.empty()) |
| 788 | { |
| 789 | addJSONCallouts(jsonCallouts, dataIface); |
| 790 | } |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | void SRC::addInventoryCallout(const std::string& inventoryPath, |
Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 794 | const std::optional<CalloutPriority>& priority, |
| 795 | const std::optional<std::string>& locationCode, |
Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 796 | const DataInterfaceBase& dataIface, |
| 797 | const std::vector<src::MRU::MRUCallout>& mrus) |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 798 | { |
| 799 | std::string locCode; |
| 800 | std::string fn; |
| 801 | std::string ccin; |
| 802 | std::string sn; |
| 803 | std::unique_ptr<src::Callout> callout; |
| 804 | |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 805 | try |
| 806 | { |
Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 807 | // Use the passed in location code if there otherwise look it up |
| 808 | if (locationCode) |
| 809 | { |
| 810 | locCode = *locationCode; |
| 811 | } |
| 812 | else |
| 813 | { |
| 814 | locCode = dataIface.getLocationCode(inventoryPath); |
| 815 | } |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 816 | |
Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 817 | try |
| 818 | { |
| 819 | dataIface.getHWCalloutFields(inventoryPath, fn, ccin, sn); |
| 820 | |
Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 821 | CalloutPriority p = |
| 822 | priority ? priority.value() : CalloutPriority::high; |
| 823 | |
Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 824 | callout = |
| 825 | std::make_unique<src::Callout>(p, locCode, fn, ccin, sn, mrus); |
Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 826 | } |
| 827 | catch (const SdBusError& e) |
| 828 | { |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 829 | std::string msg = |
| 830 | "No VPD found for " + inventoryPath + ": " + e.what(); |
| 831 | addDebugData(msg); |
Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 832 | |
| 833 | // Just create the callout with empty FRU fields |
Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 834 | callout = std::make_unique<src::Callout>( |
| 835 | CalloutPriority::high, locCode, fn, ccin, sn, mrus); |
Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 836 | } |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 837 | } |
| 838 | catch (const SdBusError& e) |
| 839 | { |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 840 | std::string msg = "Could not get location code for " + inventoryPath + |
| 841 | ": " + e.what(); |
| 842 | addDebugData(msg); |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 843 | |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 844 | callout = std::make_unique<src::Callout>(CalloutPriority::high, |
Matt Spinler | a27e2e5 | 2020-04-09 11:06:11 -0500 | [diff] [blame] | 845 | "no_vpd_for_fru"); |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 846 | } |
| 847 | |
Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 848 | createCalloutsObject(); |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 849 | _callouts->addCallout(std::move(callout)); |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 850 | } |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 851 | |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 852 | std::vector<message::RegistryCallout> |
| 853 | SRC::getRegistryCallouts(const message::Entry& regEntry, |
| 854 | const AdditionalData& additionalData, |
| 855 | const DataInterfaceBase& dataIface) |
| 856 | { |
| 857 | std::vector<message::RegistryCallout> registryCallouts; |
| 858 | |
| 859 | if (regEntry.callouts) |
| 860 | { |
Matt Spinler | 9a50c8d | 2021-04-12 14:22:26 -0500 | [diff] [blame] | 861 | std::vector<std::string> systemNames; |
| 862 | |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 863 | try |
| 864 | { |
Matt Spinler | 9a50c8d | 2021-04-12 14:22:26 -0500 | [diff] [blame] | 865 | systemNames = dataIface.getSystemNames(); |
| 866 | } |
| 867 | catch (const std::exception& e) |
| 868 | { |
| 869 | // Compatible interface not available yet |
| 870 | } |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 871 | |
Matt Spinler | 9a50c8d | 2021-04-12 14:22:26 -0500 | [diff] [blame] | 872 | try |
| 873 | { |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 874 | registryCallouts = message::Registry::getCallouts( |
| 875 | regEntry.callouts.value(), systemNames, additionalData); |
| 876 | } |
| 877 | catch (const std::exception& e) |
| 878 | { |
| 879 | addDebugData(fmt::format( |
| 880 | "Error parsing PEL message registry callout JSON: {}", |
| 881 | e.what())); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | return registryCallouts; |
| 886 | } |
| 887 | |
| 888 | void SRC::addRegistryCallouts( |
| 889 | const std::vector<message::RegistryCallout>& callouts, |
| 890 | const DataInterfaceBase& dataIface, |
| 891 | std::optional<std::string> trustedSymbolicFRUInvPath) |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 892 | { |
| 893 | try |
| 894 | { |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 895 | for (const auto& callout : callouts) |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 896 | { |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 897 | addRegistryCallout(callout, dataIface, trustedSymbolicFRUInvPath); |
| 898 | |
| 899 | // Only the first callout gets the inventory path |
| 900 | if (trustedSymbolicFRUInvPath) |
| 901 | { |
| 902 | trustedSymbolicFRUInvPath = std::nullopt; |
| 903 | } |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | catch (std::exception& e) |
| 907 | { |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 908 | std::string msg = |
| 909 | "Error parsing PEL message registry callout JSON: "s + e.what(); |
| 910 | addDebugData(msg); |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 914 | void SRC::addRegistryCallout( |
| 915 | const message::RegistryCallout& regCallout, |
| 916 | const DataInterfaceBase& dataIface, |
| 917 | const std::optional<std::string>& trustedSymbolicFRUInvPath) |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 918 | { |
| 919 | std::unique_ptr<src::Callout> callout; |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 920 | auto locCode = regCallout.locCode; |
| 921 | |
Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 922 | if (!locCode.empty()) |
| 923 | { |
| 924 | try |
| 925 | { |
| 926 | locCode = dataIface.expandLocationCode(locCode, 0); |
| 927 | } |
| 928 | catch (const std::exception& e) |
| 929 | { |
| 930 | auto msg = |
| 931 | "Unable to expand location code " + locCode + ": " + e.what(); |
| 932 | addDebugData(msg); |
| 933 | return; |
| 934 | } |
| 935 | } |
| 936 | |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 937 | // Via the PEL values table, get the priority enum. |
| 938 | // The schema will have validated the priority was a valid value. |
| 939 | auto priorityIt = |
| 940 | pv::findByName(regCallout.priority, pv::calloutPriorityValues); |
| 941 | assert(priorityIt != pv::calloutPriorityValues.end()); |
| 942 | auto priority = |
| 943 | static_cast<CalloutPriority>(std::get<pv::fieldValuePos>(*priorityIt)); |
| 944 | |
| 945 | if (!regCallout.procedure.empty()) |
| 946 | { |
| 947 | // Procedure callout |
| 948 | callout = |
| 949 | std::make_unique<src::Callout>(priority, regCallout.procedure); |
| 950 | } |
| 951 | else if (!regCallout.symbolicFRU.empty()) |
| 952 | { |
| 953 | // Symbolic FRU callout |
| 954 | callout = std::make_unique<src::Callout>( |
| 955 | priority, regCallout.symbolicFRU, locCode, false); |
| 956 | } |
| 957 | else if (!regCallout.symbolicFRUTrusted.empty()) |
| 958 | { |
| 959 | // Symbolic FRU with trusted location code callout |
| 960 | |
Matt Spinler | f00f9d0 | 2020-10-23 09:14:22 -0500 | [diff] [blame] | 961 | // Use the location code from the inventory path if there is one. |
| 962 | if (trustedSymbolicFRUInvPath) |
| 963 | { |
| 964 | try |
| 965 | { |
| 966 | locCode = dataIface.getLocationCode(*trustedSymbolicFRUInvPath); |
| 967 | } |
| 968 | catch (const std::exception& e) |
| 969 | { |
| 970 | addDebugData( |
| 971 | fmt::format("Could not get location code for {}: {}", |
| 972 | *trustedSymbolicFRUInvPath, e.what())); |
| 973 | locCode.clear(); |
| 974 | } |
| 975 | } |
| 976 | |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 977 | // The registry wants it to be trusted, but that requires a valid |
| 978 | // location code for it to actually be. |
| 979 | callout = std::make_unique<src::Callout>( |
| 980 | priority, regCallout.symbolicFRUTrusted, locCode, !locCode.empty()); |
| 981 | } |
| 982 | else |
| 983 | { |
Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 984 | // A hardware callout |
| 985 | std::string inventoryPath; |
| 986 | |
| 987 | try |
| 988 | { |
| 989 | // Get the inventory item from the unexpanded location code |
| 990 | inventoryPath = |
Matt Spinler | 2f9225a | 2020-08-05 12:58:49 -0500 | [diff] [blame] | 991 | dataIface.getInventoryFromLocCode(regCallout.locCode, 0, false); |
Matt Spinler | af191c7 | 2020-06-04 11:35:13 -0500 | [diff] [blame] | 992 | } |
| 993 | catch (const std::exception& e) |
| 994 | { |
| 995 | std::string msg = |
| 996 | "Unable to get inventory path from location code: " + locCode + |
| 997 | ": " + e.what(); |
| 998 | addDebugData(msg); |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | addInventoryCallout(inventoryPath, priority, locCode, dataIface); |
Matt Spinler | 0398458 | 2020-04-09 13:17:58 -0500 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | if (callout) |
| 1006 | { |
| 1007 | createCalloutsObject(); |
| 1008 | _callouts->addCallout(std::move(callout)); |
| 1009 | } |
| 1010 | } |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 1011 | |
Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 1012 | void SRC::addDevicePathCallouts(const AdditionalData& additionalData, |
| 1013 | const DataInterfaceBase& dataIface) |
| 1014 | { |
| 1015 | std::vector<device_callouts::Callout> callouts; |
| 1016 | auto i2cBus = additionalData.getValue("CALLOUT_IIC_BUS"); |
| 1017 | auto i2cAddr = additionalData.getValue("CALLOUT_IIC_ADDR"); |
| 1018 | auto devPath = additionalData.getValue("CALLOUT_DEVICE_PATH"); |
| 1019 | |
| 1020 | // A device callout contains either: |
| 1021 | // * CALLOUT_ERRNO, CALLOUT_DEVICE_PATH |
| 1022 | // * CALLOUT_ERRNO, CALLOUT_IIC_BUS, CALLOUT_IIC_ADDR |
| 1023 | // We don't care about the errno. |
| 1024 | |
| 1025 | if (devPath) |
| 1026 | { |
| 1027 | try |
| 1028 | { |
| 1029 | callouts = device_callouts::getCallouts(*devPath, |
| 1030 | dataIface.getSystemNames()); |
| 1031 | } |
| 1032 | catch (const std::exception& e) |
| 1033 | { |
| 1034 | addDebugData(e.what()); |
| 1035 | callouts.clear(); |
| 1036 | } |
| 1037 | } |
| 1038 | else if (i2cBus && i2cAddr) |
| 1039 | { |
| 1040 | size_t bus; |
| 1041 | uint8_t address; |
| 1042 | |
| 1043 | try |
| 1044 | { |
| 1045 | // If /dev/i2c- is prepended, remove it |
| 1046 | if (i2cBus->find("/dev/i2c-") != std::string::npos) |
| 1047 | { |
| 1048 | *i2cBus = i2cBus->substr(9); |
| 1049 | } |
| 1050 | |
| 1051 | bus = stoul(*i2cBus, nullptr, 0); |
| 1052 | address = stoul(*i2cAddr, nullptr, 0); |
| 1053 | } |
| 1054 | catch (const std::exception& e) |
| 1055 | { |
| 1056 | std::string msg = "Invalid CALLOUT_IIC_BUS " + *i2cBus + |
| 1057 | " or CALLOUT_IIC_ADDR " + *i2cAddr + |
| 1058 | " in AdditionalData property"; |
| 1059 | addDebugData(msg); |
| 1060 | return; |
| 1061 | } |
| 1062 | |
| 1063 | try |
| 1064 | { |
| 1065 | callouts = device_callouts::getI2CCallouts( |
| 1066 | bus, address, dataIface.getSystemNames()); |
| 1067 | } |
| 1068 | catch (const std::exception& e) |
| 1069 | { |
| 1070 | addDebugData(e.what()); |
| 1071 | callouts.clear(); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | for (const auto& callout : callouts) |
| 1076 | { |
| 1077 | // The priority shouldn't be invalid, but check just in case. |
| 1078 | CalloutPriority priority = CalloutPriority::high; |
| 1079 | |
| 1080 | if (!callout.priority.empty()) |
| 1081 | { |
| 1082 | auto p = pel_values::findByValue( |
| 1083 | static_cast<uint32_t>(callout.priority[0]), |
| 1084 | pel_values::calloutPriorityValues); |
| 1085 | |
| 1086 | if (p != pel_values::calloutPriorityValues.end()) |
| 1087 | { |
| 1088 | priority = static_cast<CalloutPriority>(callout.priority[0]); |
| 1089 | } |
| 1090 | else |
| 1091 | { |
| 1092 | std::string msg = |
| 1093 | "Invalid priority found in dev callout JSON: " + |
| 1094 | callout.priority[0]; |
| 1095 | addDebugData(msg); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | try |
| 1100 | { |
Matt Spinler | 2f9225a | 2020-08-05 12:58:49 -0500 | [diff] [blame] | 1101 | auto inventoryPath = dataIface.getInventoryFromLocCode( |
| 1102 | callout.locationCode, 0, false); |
Matt Spinler | 717de42 | 2020-06-04 13:10:14 -0500 | [diff] [blame] | 1103 | |
| 1104 | addInventoryCallout(inventoryPath, priority, std::nullopt, |
| 1105 | dataIface); |
| 1106 | } |
| 1107 | catch (const std::exception& e) |
| 1108 | { |
| 1109 | std::string msg = |
| 1110 | "Unable to get inventory path from location code: " + |
| 1111 | callout.locationCode + ": " + e.what(); |
| 1112 | addDebugData(msg); |
| 1113 | } |
| 1114 | |
| 1115 | // Until the code is there to convert these MRU value strings to |
| 1116 | // the official MRU values in the callout objects, just store |
| 1117 | // the MRU name in the debug UserData section. |
| 1118 | if (!callout.mru.empty()) |
| 1119 | { |
| 1120 | std::string msg = "MRU: " + callout.mru; |
| 1121 | addDebugData(msg); |
| 1122 | } |
| 1123 | |
| 1124 | // getCallouts() may have generated some debug data it stored |
| 1125 | // in a callout object. Save it as well. |
| 1126 | if (!callout.debug.empty()) |
| 1127 | { |
| 1128 | addDebugData(callout.debug); |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 1133 | void SRC::addJSONCallouts(const nlohmann::json& jsonCallouts, |
| 1134 | const DataInterfaceBase& dataIface) |
| 1135 | { |
| 1136 | if (jsonCallouts.empty()) |
| 1137 | { |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | if (!jsonCallouts.is_array()) |
| 1142 | { |
| 1143 | addDebugData("Callout JSON isn't an array"); |
| 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | for (const auto& callout : jsonCallouts) |
| 1148 | { |
| 1149 | try |
| 1150 | { |
| 1151 | addJSONCallout(callout, dataIface); |
| 1152 | } |
| 1153 | catch (const std::exception& e) |
| 1154 | { |
| 1155 | addDebugData(fmt::format( |
| 1156 | "Failed extracting callout data from JSON: {}", e.what())); |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | void SRC::addJSONCallout(const nlohmann::json& jsonCallout, |
| 1162 | const DataInterfaceBase& dataIface) |
| 1163 | { |
Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1164 | auto priority = getPriorityFromJSON(jsonCallout); |
| 1165 | std::string locCode; |
| 1166 | std::string unexpandedLocCode; |
| 1167 | std::unique_ptr<src::Callout> callout; |
| 1168 | |
| 1169 | // Expand the location code if it's there |
| 1170 | if (jsonCallout.contains("LocationCode")) |
| 1171 | { |
| 1172 | unexpandedLocCode = jsonCallout.at("LocationCode").get<std::string>(); |
| 1173 | |
| 1174 | try |
| 1175 | { |
| 1176 | locCode = dataIface.expandLocationCode(unexpandedLocCode, 0); |
| 1177 | } |
| 1178 | catch (const std::exception& e) |
| 1179 | { |
| 1180 | addDebugData(fmt::format("Unable to expand location code {}: {}", |
| 1181 | unexpandedLocCode, e.what())); |
| 1182 | // Use the value from the JSON so at least there's something |
| 1183 | locCode = unexpandedLocCode; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | // Create either a procedure, symbolic FRU, or normal FRU callout. |
| 1188 | if (jsonCallout.contains("Procedure")) |
| 1189 | { |
| 1190 | auto procedure = jsonCallout.at("Procedure").get<std::string>(); |
| 1191 | |
| 1192 | callout = std::make_unique<src::Callout>( |
| 1193 | static_cast<CalloutPriority>(priority), procedure, |
| 1194 | src::CalloutValueType::raw); |
| 1195 | } |
| 1196 | else if (jsonCallout.contains("SymbolicFRU")) |
| 1197 | { |
| 1198 | auto fru = jsonCallout.at("SymbolicFRU").get<std::string>(); |
| 1199 | |
| 1200 | bool trusted = false; |
| 1201 | if (jsonCallout.contains("TrustedLocationCode") && !locCode.empty()) |
| 1202 | { |
| 1203 | trusted = jsonCallout.at("TrustedLocationCode").get<bool>(); |
| 1204 | } |
| 1205 | |
| 1206 | callout = std::make_unique<src::Callout>( |
| 1207 | static_cast<CalloutPriority>(priority), fru, |
| 1208 | src::CalloutValueType::raw, locCode, trusted); |
| 1209 | } |
| 1210 | else |
| 1211 | { |
| 1212 | // A hardware FRU |
| 1213 | std::string inventoryPath; |
Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 1214 | std::vector<src::MRU::MRUCallout> mrus; |
Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1215 | |
| 1216 | if (jsonCallout.contains("InventoryPath")) |
| 1217 | { |
| 1218 | inventoryPath = jsonCallout.at("InventoryPath").get<std::string>(); |
| 1219 | } |
| 1220 | else |
| 1221 | { |
| 1222 | if (unexpandedLocCode.empty()) |
| 1223 | { |
| 1224 | throw std::runtime_error{"JSON callout needs either an " |
| 1225 | "inventory path or location code"}; |
| 1226 | } |
| 1227 | |
| 1228 | try |
| 1229 | { |
| 1230 | inventoryPath = dataIface.getInventoryFromLocCode( |
| 1231 | unexpandedLocCode, 0, false); |
| 1232 | } |
| 1233 | catch (const std::exception& e) |
| 1234 | { |
| 1235 | throw std::runtime_error{ |
| 1236 | fmt::format("Unable to get inventory path from " |
| 1237 | "location code: {}: {}", |
| 1238 | unexpandedLocCode, e.what())}; |
| 1239 | } |
| 1240 | } |
| 1241 | |
Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 1242 | if (jsonCallout.contains("MRUs")) |
| 1243 | { |
| 1244 | mrus = getMRUsFromJSON(jsonCallout.at("MRUs")); |
| 1245 | } |
| 1246 | |
Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1247 | // If the location code was also passed in, use that here too |
| 1248 | // so addInventoryCallout doesn't have to look it up. |
| 1249 | std::optional<std::string> lc; |
| 1250 | if (!locCode.empty()) |
| 1251 | { |
| 1252 | lc = locCode; |
| 1253 | } |
| 1254 | |
Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 1255 | addInventoryCallout(inventoryPath, priority, lc, dataIface, mrus); |
Matt Spinler | afa2c79 | 2020-08-27 11:01:39 -0500 | [diff] [blame] | 1256 | |
| 1257 | if (jsonCallout.contains("Deconfigured")) |
| 1258 | { |
| 1259 | if (jsonCallout.at("Deconfigured").get<bool>()) |
| 1260 | { |
| 1261 | setErrorStatusFlag(ErrorStatusFlags::deconfigured); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | if (jsonCallout.contains("Guarded")) |
| 1266 | { |
| 1267 | if (jsonCallout.at("Guarded").get<bool>()) |
| 1268 | { |
| 1269 | setErrorStatusFlag(ErrorStatusFlags::guarded); |
| 1270 | } |
| 1271 | } |
Matt Spinler | 3bdd011 | 2020-08-27 10:24:34 -0500 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | if (callout) |
| 1275 | { |
| 1276 | createCalloutsObject(); |
| 1277 | _callouts->addCallout(std::move(callout)); |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | CalloutPriority SRC::getPriorityFromJSON(const nlohmann::json& json) |
| 1282 | { |
| 1283 | // Looks like: |
| 1284 | // { |
| 1285 | // "Priority": "H" |
| 1286 | // } |
| 1287 | auto p = json.at("Priority").get<std::string>(); |
| 1288 | if (p.empty()) |
| 1289 | { |
| 1290 | throw std::runtime_error{"Priority field in callout is empty"}; |
| 1291 | } |
| 1292 | |
| 1293 | auto priority = static_cast<CalloutPriority>(p.front()); |
| 1294 | |
| 1295 | // Validate it |
| 1296 | auto priorityIt = pv::findByValue(static_cast<uint32_t>(priority), |
| 1297 | pv::calloutPriorityValues); |
| 1298 | if (priorityIt == pv::calloutPriorityValues.end()) |
| 1299 | { |
| 1300 | throw std::runtime_error{ |
| 1301 | fmt::format("Invalid priority '{}' found in JSON callout", p)}; |
| 1302 | } |
| 1303 | |
| 1304 | return priority; |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 1305 | } |
| 1306 | |
Matt Spinler | b8cb60f | 2020-08-27 10:55:55 -0500 | [diff] [blame] | 1307 | std::vector<src::MRU::MRUCallout> |
| 1308 | SRC::getMRUsFromJSON(const nlohmann::json& mruJSON) |
| 1309 | { |
| 1310 | std::vector<src::MRU::MRUCallout> mrus; |
| 1311 | |
| 1312 | // Looks like: |
| 1313 | // [ |
| 1314 | // { |
| 1315 | // "ID": 100, |
| 1316 | // "Priority": "H" |
| 1317 | // } |
| 1318 | // ] |
| 1319 | if (!mruJSON.is_array()) |
| 1320 | { |
| 1321 | addDebugData("MRU callout JSON is not an array"); |
| 1322 | return mrus; |
| 1323 | } |
| 1324 | |
| 1325 | for (const auto& mruCallout : mruJSON) |
| 1326 | { |
| 1327 | try |
| 1328 | { |
| 1329 | auto priority = getPriorityFromJSON(mruCallout); |
| 1330 | auto id = mruCallout.at("ID").get<uint32_t>(); |
| 1331 | |
| 1332 | src::MRU::MRUCallout mru{static_cast<uint32_t>(priority), id}; |
| 1333 | mrus.push_back(std::move(mru)); |
| 1334 | } |
| 1335 | catch (const std::exception& e) |
| 1336 | { |
| 1337 | addDebugData(fmt::format("Invalid MRU entry in JSON: {}: {}", |
| 1338 | mruCallout.dump(), e.what())); |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | return mrus; |
| 1343 | } |
| 1344 | |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 1345 | } // namespace pels |
| 1346 | } // namespace openpower |