| 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 | |
| Jayanth Othayoth | da9b583 | 2021-11-05 04:19:43 -0500 | [diff] [blame] | 4 | #include "config.h" |
| 5 | |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 6 | #include "pel.hpp" |
| 7 | |
| 8 | #include "bcd_time.hpp" |
| Matt Spinler | 386a61e | 2020-08-13 15:51:12 -0500 | [diff] [blame] | 9 | #include "extended_user_data.hpp" |
| Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 10 | #include "extended_user_header.hpp" |
| Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 11 | #include "failing_mtms.hpp" |
| Andrew Geissler | f8e750d | 2022-01-14 14:56:13 -0600 | [diff] [blame] | 12 | #include "fru_identity.hpp" |
| Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 13 | #include "json_utils.hpp" |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 14 | #include "log_id.hpp" |
| Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 15 | #include "pel_rules.hpp" |
| Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 16 | #include "pel_values.hpp" |
| Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 17 | #include "section_factory.hpp" |
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 18 | #include "src.hpp" |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 19 | #include "stream.hpp" |
| Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 20 | #include "user_data_formats.hpp" |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 21 | |
| Jayanth Othayoth | 92b2066 | 2021-11-05 00:09:15 -0500 | [diff] [blame] | 22 | #ifdef PEL_ENABLE_PHAL |
| Jayanth Othayoth | da9b583 | 2021-11-05 04:19:43 -0500 | [diff] [blame] | 23 | #include "phal_service_actions.hpp" |
| Jayanth Othayoth | e8bdeea | 2021-06-03 03:01:16 -0500 | [diff] [blame] | 24 | #include "sbe_ffdc_handler.hpp" |
| Deepa Karthikeyan | ff35be3 | 2024-10-15 09:10:49 -0500 | [diff] [blame] | 25 | |
| 26 | #include <libguard/guard_interface.hpp> |
| 27 | #include <libguard/include/guard_record.hpp> |
| 28 | |
| 29 | namespace libguard = openpower::guard; |
| Jayanth Othayoth | e8bdeea | 2021-06-03 03:01:16 -0500 | [diff] [blame] | 30 | #endif |
| 31 | |
| Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 32 | #include <sys/stat.h> |
| 33 | #include <unistd.h> |
| 34 | |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 35 | #include <phosphor-logging/lg2.hpp> |
| Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 36 | |
| Jayanth Othayoth | 1aa90d4 | 2023-09-13 04:25:45 -0500 | [diff] [blame] | 37 | #include <format> |
| Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 38 | #include <iostream> |
| Arya K Padman | d8ae618 | 2024-07-19 06:25:10 -0500 | [diff] [blame] | 39 | #include <ranges> |
| Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 40 | |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 41 | namespace openpower |
| 42 | { |
| 43 | namespace pels |
| 44 | { |
| Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 45 | namespace pv = openpower::pels::pel_values; |
| Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 46 | |
| Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 47 | constexpr auto unknownValue = "Unknown"; |
| Arya K Padman | d8ae618 | 2024-07-19 06:25:10 -0500 | [diff] [blame] | 48 | constexpr auto AdDIMMInfoFetchError = "DIMMs Info Fetch Error"; |
| Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 49 | |
| Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 50 | PEL::PEL(const message::Entry& regEntry, uint32_t obmcLogID, uint64_t timestamp, |
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 51 | phosphor::logging::Entry::Level severity, |
| Jayanth Othayoth | e8bdeea | 2021-06-03 03:01:16 -0500 | [diff] [blame] | 52 | const AdditionalData& additionalData, const PelFFDC& ffdcFilesIn, |
| Matt Spinler | 9d92109 | 2022-12-15 11:54:49 -0600 | [diff] [blame] | 53 | const DataInterfaceBase& dataIface, const JournalBase& journal) |
| Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 54 | { |
| Jayanth Othayoth | e8bdeea | 2021-06-03 03:01:16 -0500 | [diff] [blame] | 55 | // No changes in input, for non SBE error related requests |
| 56 | PelFFDC ffdcFiles = ffdcFilesIn; |
| 57 | |
| Jayanth Othayoth | 92b2066 | 2021-11-05 00:09:15 -0500 | [diff] [blame] | 58 | #ifdef PEL_ENABLE_PHAL |
| Jayanth Othayoth | e8bdeea | 2021-06-03 03:01:16 -0500 | [diff] [blame] | 59 | // Add sbe ffdc processed data into ffdcfiles. |
| 60 | namespace sbe = openpower::pels::sbe; |
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 61 | auto processReq = |
| 62 | std::any_of(ffdcFiles.begin(), ffdcFiles.end(), [](const auto& file) { |
| 63 | return file.format == UserDataFormat::custom && |
| 64 | file.subType == sbe::sbeFFDCSubType; |
| 65 | }); |
| Jayanth Othayoth | e8bdeea | 2021-06-03 03:01:16 -0500 | [diff] [blame] | 66 | // sbeFFDC can't be destroyed until the end of the PEL constructor |
| 67 | // because it needs to keep around the FFDC Files to be used below. |
| 68 | std::unique_ptr<sbe::SbeFFDC> sbeFFDCPtr; |
| 69 | if (processReq) |
| 70 | { |
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 71 | sbeFFDCPtr = |
| 72 | std::make_unique<sbe::SbeFFDC>(additionalData, ffdcFilesIn); |
| Jayanth Othayoth | e8bdeea | 2021-06-03 03:01:16 -0500 | [diff] [blame] | 73 | const auto& sbeFFDCFiles = sbeFFDCPtr->getSbeFFDC(); |
| 74 | ffdcFiles.insert(ffdcFiles.end(), sbeFFDCFiles.begin(), |
| 75 | sbeFFDCFiles.end()); |
| Jayanth Othayoth | 742b00b | 2022-06-30 05:16:57 -0500 | [diff] [blame] | 76 | |
| 77 | // update pel priority for spare clock failures |
| 78 | if (auto customSeverity = sbeFFDCPtr->getSeverity()) |
| 79 | { |
| 80 | severity = customSeverity.value(); |
| 81 | } |
| Jayanth Othayoth | e8bdeea | 2021-06-03 03:01:16 -0500 | [diff] [blame] | 82 | } |
| 83 | #endif |
| 84 | |
| Arya K Padman | d8ae618 | 2024-07-19 06:25:10 -0500 | [diff] [blame] | 85 | DebugData debugData; |
| Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 86 | nlohmann::json callouts; |
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 87 | |
| Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 88 | _ph = std::make_unique<PrivateHeader>(regEntry.componentID, obmcLogID, |
| Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 89 | timestamp); |
| Vijay Lobo | 6b3f345 | 2021-04-15 23:04:42 -0500 | [diff] [blame] | 90 | _uh = std::make_unique<UserHeader>(regEntry, severity, additionalData, |
| 91 | dataIface); |
| Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 92 | |
| Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 93 | // Extract any callouts embedded in an FFDC file. |
| 94 | if (!ffdcFiles.empty()) |
| 95 | { |
| 96 | try |
| 97 | { |
| 98 | callouts = getCalloutJSON(ffdcFiles); |
| 99 | } |
| 100 | catch (const std::exception& e) |
| 101 | { |
| 102 | debugData.emplace("FFDC file JSON callouts error", |
| 103 | std::vector<std::string>{e.what()}); |
| 104 | } |
| 105 | } |
| 106 | |
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 107 | auto src = |
| 108 | std::make_unique<SRC>(regEntry, additionalData, callouts, dataIface); |
| Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 109 | |
| Arya K Padman | d8ae618 | 2024-07-19 06:25:10 -0500 | [diff] [blame] | 110 | nlohmann::json adSysInfoData(nlohmann::json::value_t::object); |
| 111 | addAdDetailsForDIMMsCallout(src, dataIface, adSysInfoData, debugData); |
| 112 | |
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 113 | if (!src->getDebugData().empty()) |
| 114 | { |
| 115 | // Something didn't go as planned |
| 116 | debugData.emplace("SRC", src->getDebugData()); |
| 117 | } |
| Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 118 | |
| Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 119 | auto euh = std::make_unique<ExtendedUserHeader>(dataIface, regEntry, *src); |
| Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 120 | |
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 121 | _optionalSections.push_back(std::move(src)); |
| Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 122 | _optionalSections.push_back(std::move(euh)); |
| Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 123 | |
| Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 124 | auto mtms = std::make_unique<FailingMTMS>(dataIface); |
| 125 | _optionalSections.push_back(std::move(mtms)); |
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 126 | |
| Arya K Padman | d8ae618 | 2024-07-19 06:25:10 -0500 | [diff] [blame] | 127 | auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface, true, |
| 128 | adSysInfoData); |
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 129 | addUserDataSection(std::move(ud)); |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 130 | |
| Sumit Kumar | 3e27443 | 2021-09-14 06:37:56 -0500 | [diff] [blame] | 131 | // Check for pel severity of type - 0x51 = critical error, system |
| 132 | // termination and update terminate bit in SRC for pels |
| 133 | updateTerminateBitInSRCSection(); |
| 134 | |
| Matt Spinler | 9b7e94f | 2020-03-24 15:44:41 -0500 | [diff] [blame] | 135 | // Create a UserData section from AdditionalData. |
| Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 136 | if (!additionalData.empty()) |
| 137 | { |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 138 | ud = util::makeADUserDataSection(additionalData); |
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 139 | addUserDataSection(std::move(ud)); |
| Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 142 | // Add any FFDC files into UserData sections |
| 143 | for (const auto& file : ffdcFiles) |
| 144 | { |
| 145 | ud = util::makeFFDCuserDataSection(regEntry.componentID, file); |
| 146 | if (!ud) |
| 147 | { |
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 148 | // Add this error into the debug data UserData section |
| 149 | std::ostringstream msg; |
| 150 | msg << "Could not make PEL FFDC UserData section from file" |
| 151 | << std::hex << regEntry.componentID << " " << file.subType |
| 152 | << " " << file.version; |
| 153 | if (debugData.count("FFDC File")) |
| 154 | { |
| 155 | debugData.at("FFDC File").push_back(msg.str()); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | debugData.emplace("FFDC File", |
| 160 | std::vector<std::string>{msg.str()}); |
| 161 | } |
| 162 | |
| Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 163 | continue; |
| 164 | } |
| 165 | |
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 166 | addUserDataSection(std::move(ud)); |
| 167 | } |
| Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 168 | |
| Jayanth Othayoth | da9b583 | 2021-11-05 04:19:43 -0500 | [diff] [blame] | 169 | #ifdef PEL_ENABLE_PHAL |
| 170 | auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID); |
| Deepa Karthikeyan | ff35be3 | 2024-10-15 09:10:49 -0500 | [diff] [blame] | 171 | openpower::pels::phal::createServiceActions(callouts, dataIface, plid()); |
| Jayanth Othayoth | da9b583 | 2021-11-05 04:19:43 -0500 | [diff] [blame] | 172 | #endif |
| 173 | |
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 174 | // Store in the PEL any important debug data created while |
| 175 | // building the PEL sections. |
| 176 | if (!debugData.empty()) |
| 177 | { |
| 178 | nlohmann::json data; |
| 179 | data["PEL Internal Debug Data"] = debugData; |
| 180 | ud = util::makeJSONUserDataSection(data); |
| 181 | |
| 182 | addUserDataSection(std::move(ud)); |
| 183 | |
| 184 | // Also put in the journal for debug |
| Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 185 | for (const auto& [name, msgs] : debugData) |
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 186 | { |
| Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 187 | for (const auto& message : msgs) |
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 188 | { |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 189 | lg2::info("{NAME}: {MSG}", "NAME", name, "MSG", message); |
| Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 190 | } |
| 191 | } |
| Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 192 | } |
| 193 | |
| Matt Spinler | 9d92109 | 2022-12-15 11:54:49 -0600 | [diff] [blame] | 194 | addJournalSections(regEntry, journal); |
| 195 | |
| Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 196 | _ph->setSectionCount(2 + _optionalSections.size()); |
| Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 197 | |
| 198 | checkRulesAndFix(); |
| Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 199 | } |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 200 | |
| Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 201 | PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0) {} |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 202 | |
| Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 203 | PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID) |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 204 | { |
| Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 205 | populateFromRawData(data, obmcLogID); |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 208 | void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID) |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 209 | { |
| Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 210 | Stream pelData{data}; |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 211 | _ph = std::make_unique<PrivateHeader>(pelData); |
| 212 | if (obmcLogID != 0) |
| 213 | { |
| Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 214 | _ph->setOBMCLogID(obmcLogID); |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | _uh = std::make_unique<UserHeader>(pelData); |
| Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 218 | |
| 219 | // Use the section factory to create the rest of the objects |
| 220 | for (size_t i = 2; i < _ph->sectionCount(); i++) |
| 221 | { |
| 222 | auto section = section_factory::create(pelData); |
| 223 | _optionalSections.push_back(std::move(section)); |
| 224 | } |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | bool PEL::valid() const |
| 228 | { |
| 229 | bool valid = _ph->valid(); |
| 230 | |
| 231 | if (valid) |
| 232 | { |
| 233 | valid = _uh->valid(); |
| 234 | } |
| 235 | |
| Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 236 | if (valid) |
| 237 | { |
| 238 | if (!std::all_of(_optionalSections.begin(), _optionalSections.end(), |
| 239 | [](const auto& section) { return section->valid(); })) |
| 240 | { |
| 241 | valid = false; |
| 242 | } |
| 243 | } |
| 244 | |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 245 | return valid; |
| 246 | } |
| 247 | |
| 248 | void PEL::setCommitTime() |
| 249 | { |
| 250 | auto now = std::chrono::system_clock::now(); |
| Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 251 | _ph->setCommitTimestamp(getBCDTime(now)); |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void PEL::assignID() |
| 255 | { |
| Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 256 | _ph->setID(generatePELID()); |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 257 | } |
| 258 | |
| Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 259 | void PEL::flatten(std::vector<uint8_t>& pelBuffer) const |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 260 | { |
| 261 | Stream pelData{pelBuffer}; |
| Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 262 | |
| Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 263 | if (!valid()) |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 264 | { |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 265 | lg2::warning("Unflattening an invalid PEL"); |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 266 | } |
| 267 | |
| Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 268 | _ph->flatten(pelData); |
| Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 269 | _uh->flatten(pelData); |
| Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 270 | |
| 271 | for (auto& section : _optionalSections) |
| 272 | { |
| 273 | section->flatten(pelData); |
| 274 | } |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 275 | } |
| 276 | |
| Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 277 | std::vector<uint8_t> PEL::data() const |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 278 | { |
| Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 279 | std::vector<uint8_t> pelData; |
| 280 | flatten(pelData); |
| 281 | return pelData; |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 282 | } |
| 283 | |
| Matt Spinler | f1b46ff | 2020-01-22 14:10:04 -0600 | [diff] [blame] | 284 | size_t PEL::size() const |
| 285 | { |
| 286 | size_t size = 0; |
| 287 | |
| 288 | if (_ph) |
| 289 | { |
| 290 | size += _ph->header().size; |
| 291 | } |
| 292 | |
| 293 | if (_uh) |
| 294 | { |
| 295 | size += _uh->header().size; |
| 296 | } |
| 297 | |
| 298 | for (const auto& section : _optionalSections) |
| 299 | { |
| 300 | size += section->header().size; |
| 301 | } |
| 302 | |
| 303 | return size; |
| 304 | } |
| 305 | |
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 306 | std::optional<SRC*> PEL::primarySRC() const |
| 307 | { |
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 308 | auto src = std::find_if( |
| 309 | _optionalSections.begin(), _optionalSections.end(), [](auto& section) { |
| 310 | return section->header().id == |
| 311 | static_cast<uint16_t>(SectionID::primarySRC); |
| 312 | }); |
| Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 313 | if (src != _optionalSections.end()) |
| 314 | { |
| 315 | return static_cast<SRC*>(src->get()); |
| 316 | } |
| 317 | |
| 318 | return std::nullopt; |
| 319 | } |
| 320 | |
| Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 321 | void PEL::checkRulesAndFix() |
| 322 | { |
| Matt Spinler | 1f93c59 | 2020-09-10 10:43:08 -0500 | [diff] [blame] | 323 | // Only fix if the action flags are at their default value which |
| 324 | // means they weren't specified in the registry. Otherwise |
| 325 | // assume the user knows what they are doing. |
| 326 | if (_uh->actionFlags() == actionFlagsDefault) |
| 327 | { |
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 328 | auto [actionFlags, eventType] = |
| 329 | pel_rules::check(0, _uh->eventType(), _uh->severity()); |
| Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 330 | |
| Matt Spinler | 1f93c59 | 2020-09-10 10:43:08 -0500 | [diff] [blame] | 331 | _uh->setActionFlags(actionFlags); |
| 332 | _uh->setEventType(eventType); |
| 333 | } |
| Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 334 | } |
| 335 | |
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 336 | void PEL::printSectionInJSON( |
| 337 | const Section& section, std::string& buf, |
| 338 | std::map<uint16_t, size_t>& pluralSections, message::Registry& registry, |
| 339 | const std::vector<std::string>& plugins, uint8_t creatorID) const |
| Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 340 | { |
| 341 | char tmpB[5]; |
| Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 342 | uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8), |
| 343 | static_cast<uint8_t>(section.header().id)}; |
| 344 | sprintf(tmpB, "%c%c", id[0], id[1]); |
| 345 | std::string sectionID(tmpB); |
| 346 | std::string sectionName = pv::sectionTitles.count(sectionID) |
| 347 | ? pv::sectionTitles.at(sectionID) |
| 348 | : "Unknown Section"; |
| Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 349 | |
| 350 | // Add a count if there are multiple of this type of section |
| 351 | auto count = pluralSections.find(section.header().id); |
| 352 | if (count != pluralSections.end()) |
| 353 | { |
| 354 | sectionName += " " + std::to_string(count->second); |
| 355 | count->second++; |
| 356 | } |
| 357 | |
| Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 358 | if (section.valid()) |
| 359 | { |
| Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 360 | std::optional<std::string> json; |
| 361 | if (sectionID == "PS" || sectionID == "SS") |
| 362 | { |
| Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 363 | json = section.getJSON(registry, plugins, creatorID); |
| Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 364 | } |
| Matt Spinler | 386a61e | 2020-08-13 15:51:12 -0500 | [diff] [blame] | 365 | else if ((sectionID == "UD") || (sectionID == "ED")) |
| Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 366 | { |
| Harisuddin Mohamed Isa | 3fdcd4e | 2020-08-26 11:56:42 +0800 | [diff] [blame] | 367 | json = section.getJSON(creatorID, plugins); |
| Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 368 | } |
| 369 | else |
| 370 | { |
| Matt Spinler | b832aa5 | 2023-03-21 15:32:34 -0500 | [diff] [blame] | 371 | json = section.getJSON(creatorID); |
| Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 372 | } |
| Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 373 | |
| 374 | buf += "\"" + sectionName + "\": {\n"; |
| 375 | |
| Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 376 | if (json) |
| 377 | { |
| Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 378 | buf += *json + "\n},\n"; |
| Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 379 | } |
| 380 | else |
| 381 | { |
| Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 382 | jsonInsert(buf, pv::sectionVer, |
| 383 | getNumberString("%d", section.header().version), 1); |
| 384 | jsonInsert(buf, pv::subSection, |
| 385 | getNumberString("%d", section.header().subType), 1); |
| 386 | jsonInsert(buf, pv::createdBy, |
| 387 | getNumberString("0x%X", section.header().componentID), |
| 388 | 1); |
| 389 | |
| Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 390 | std::vector<uint8_t> data; |
| 391 | Stream s{data}; |
| 392 | section.flatten(s); |
| Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 393 | std::string dstr = |
| 394 | dumpHex(std::data(data) + SectionHeader::flattenedSize(), |
| Arya K Padman | 8c8aaa0 | 2024-04-28 23:23:45 -0500 | [diff] [blame] | 395 | data.size() - SectionHeader::flattenedSize(), 2) |
| 396 | .get(); |
| Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 397 | std::string jsonIndent(indentLevel, 0x20); |
| 398 | buf += jsonIndent + "\"Data\": [\n"; |
| 399 | buf += dstr; |
| 400 | buf += jsonIndent + "]\n"; |
| 401 | buf += "},\n"; |
| Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 402 | } |
| Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 403 | } |
| 404 | else |
| 405 | { |
| Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 406 | buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n"; |
| Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 410 | std::map<uint16_t, size_t> PEL::getPluralSections() const |
| 411 | { |
| 412 | std::map<uint16_t, size_t> sectionCounts; |
| 413 | |
| 414 | for (const auto& section : optionalSections()) |
| 415 | { |
| 416 | if (sectionCounts.find(section->header().id) == sectionCounts.end()) |
| 417 | { |
| 418 | sectionCounts[section->header().id] = 1; |
| 419 | } |
| 420 | else |
| 421 | { |
| 422 | sectionCounts[section->header().id]++; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | std::map<uint16_t, size_t> sections; |
| 427 | for (const auto& [id, count] : sectionCounts) |
| 428 | { |
| 429 | if (count > 1) |
| 430 | { |
| 431 | // Start with 0 here and printSectionInJSON() |
| 432 | // will increment it as it goes. |
| 433 | sections.emplace(id, 0); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | return sections; |
| 438 | } |
| 439 | |
| Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 440 | void PEL::toJSON(message::Registry& registry, |
| 441 | const std::vector<std::string>& plugins) const |
| Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 442 | { |
| Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 443 | auto sections = getPluralSections(); |
| 444 | |
| Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 445 | std::string buf = "{\n"; |
| Matt Spinler | b832aa5 | 2023-03-21 15:32:34 -0500 | [diff] [blame] | 446 | printSectionInJSON(*(_ph.get()), buf, sections, registry, plugins, |
| 447 | _ph->creatorID()); |
| 448 | printSectionInJSON(*(_uh.get()), buf, sections, registry, plugins, |
| 449 | _ph->creatorID()); |
| Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 450 | for (auto& section : this->optionalSections()) |
| 451 | { |
| Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 452 | printSectionInJSON(*(section.get()), buf, sections, registry, plugins, |
| 453 | _ph->creatorID()); |
| Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 454 | } |
| 455 | buf += "}"; |
| 456 | std::size_t found = buf.rfind(","); |
| 457 | if (found != std::string::npos) |
| 458 | buf.replace(found, 1, ""); |
| 459 | std::cout << buf << std::endl; |
| 460 | } |
| Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 461 | |
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 462 | bool PEL::addUserDataSection(std::unique_ptr<UserData> userData) |
| 463 | { |
| 464 | if (size() + userData->header().size > _maxPELSize) |
| 465 | { |
| 466 | if (userData->shrink(_maxPELSize - size())) |
| 467 | { |
| 468 | _optionalSections.push_back(std::move(userData)); |
| 469 | } |
| 470 | else |
| 471 | { |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 472 | lg2::warning("Could not shrink UserData section. Dropping. " |
| 473 | "Section size = {SSIZE}, Component ID = {COMP_ID}, " |
| 474 | "Subtype = {SUBTYPE}, Version = {VERSION}", |
| 475 | "SSIZE", userData->header().size, "COMP_ID", |
| 476 | userData->header().componentID, "SUBTYPE", |
| 477 | userData->header().subType, "VERSION", |
| 478 | userData->header().version); |
| Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 479 | return false; |
| 480 | } |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | _optionalSections.push_back(std::move(userData)); |
| 485 | } |
| 486 | return true; |
| 487 | } |
| 488 | |
| Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 489 | nlohmann::json PEL::getCalloutJSON(const PelFFDC& ffdcFiles) |
| 490 | { |
| 491 | nlohmann::json callouts; |
| 492 | |
| 493 | for (const auto& file : ffdcFiles) |
| 494 | { |
| 495 | if ((file.format == UserDataFormat::json) && |
| 496 | (file.subType == jsonCalloutSubtype)) |
| 497 | { |
| 498 | auto data = util::readFD(file.fd); |
| 499 | if (data.empty()) |
| 500 | { |
| 501 | throw std::runtime_error{ |
| 502 | "Could not get data from JSON callout file descriptor"}; |
| 503 | } |
| 504 | |
| 505 | std::string jsonString{data.begin(), data.begin() + data.size()}; |
| 506 | |
| 507 | callouts = nlohmann::json::parse(jsonString); |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | return callouts; |
| 513 | } |
| 514 | |
| Andrew Geissler | f8e750d | 2022-01-14 14:56:13 -0600 | [diff] [blame] | 515 | bool PEL::isHwCalloutPresent() const |
| Andrew Geissler | 44fc316 | 2020-07-09 09:21:31 -0500 | [diff] [blame] | 516 | { |
| 517 | auto pSRC = primarySRC(); |
| 518 | if (!pSRC) |
| 519 | { |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | bool calloutPresent = false; |
| 524 | if ((*pSRC)->callouts()) |
| 525 | { |
| 526 | for (auto& i : (*pSRC)->callouts()->callouts()) |
| 527 | { |
| 528 | if (((*i).fruIdentity())) |
| 529 | { |
| 530 | auto& fruId = (*i).fruIdentity(); |
| Andrew Geissler | f8e750d | 2022-01-14 14:56:13 -0600 | [diff] [blame] | 531 | if ((*fruId).failingComponentType() == |
| 532 | src::FRUIdentity::hardwareFRU) |
| Andrew Geissler | 44fc316 | 2020-07-09 09:21:31 -0500 | [diff] [blame] | 533 | { |
| 534 | calloutPresent = true; |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | return calloutPresent; |
| 542 | } |
| 543 | |
| Sumit Kumar | 3160a54 | 2021-04-26 08:07:04 -0500 | [diff] [blame] | 544 | void PEL::updateSysInfoInExtendedUserDataSection( |
| 545 | const DataInterfaceBase& dataIface) |
| 546 | { |
| 547 | const AdditionalData additionalData; |
| 548 | |
| 549 | // Check for PEL from Hostboot |
| 550 | if (_ph->creatorID() == static_cast<uint8_t>(CreatorID::hostboot)) |
| 551 | { |
| 552 | // Get the ED section from PEL |
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 553 | auto op = std::find_if( |
| 554 | _optionalSections.begin(), _optionalSections.end(), |
| 555 | [](auto& section) { |
| 556 | return section->header().id == |
| 557 | static_cast<uint16_t>(SectionID::extUserData); |
| 558 | }); |
| Sumit Kumar | 3160a54 | 2021-04-26 08:07:04 -0500 | [diff] [blame] | 559 | |
| 560 | // Check for ED section found and its not the last section of PEL |
| 561 | if (op != _optionalSections.end()) |
| 562 | { |
| 563 | // Get the extended user data class mapped to found section |
| 564 | auto extUserData = static_cast<ExtendedUserData*>(op->get()); |
| 565 | |
| 566 | // Check for the creator ID is for OpenBMC |
| 567 | if (extUserData->creatorID() == |
| 568 | static_cast<uint8_t>(CreatorID::openBMC)) |
| 569 | { |
| 570 | // Update subtype and component id |
| 571 | auto subType = static_cast<uint8_t>(UserDataFormat::json); |
| 572 | auto componentId = |
| 573 | static_cast<uint16_t>(ComponentID::phosphorLogging); |
| 574 | |
| 575 | // Update system data to ED section |
| George Liu | 9ac0d9b | 2022-07-15 10:57:38 +0800 | [diff] [blame] | 576 | auto ud = util::makeSysInfoUserDataSection(additionalData, |
| 577 | dataIface, false); |
| Sumit Kumar | 3160a54 | 2021-04-26 08:07:04 -0500 | [diff] [blame] | 578 | extUserData->updateDataSection(subType, componentId, |
| 579 | ud->data()); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
| Matt Spinler | 8e65f4e | 2023-05-02 13:40:08 -0500 | [diff] [blame] | 585 | bool PEL::getDeconfigFlag() const |
| 586 | { |
| 587 | auto creator = static_cast<CreatorID>(_ph->creatorID()); |
| 588 | |
| 589 | if ((creator == CreatorID::openBMC) || (creator == CreatorID::hostboot)) |
| 590 | { |
| 591 | auto src = primarySRC(); |
| 592 | return (*src)->getErrorStatusFlag(SRC::ErrorStatusFlags::deconfigured); |
| 593 | } |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | bool PEL::getGuardFlag() const |
| 598 | { |
| 599 | auto creator = static_cast<CreatorID>(_ph->creatorID()); |
| 600 | |
| 601 | if ((creator == CreatorID::openBMC) || (creator == CreatorID::hostboot)) |
| 602 | { |
| 603 | auto src = primarySRC(); |
| 604 | return (*src)->getErrorStatusFlag(SRC::ErrorStatusFlags::guarded); |
| 605 | } |
| 606 | return false; |
| 607 | } |
| 608 | |
| Sumit Kumar | 3e27443 | 2021-09-14 06:37:56 -0500 | [diff] [blame] | 609 | void PEL::updateTerminateBitInSRCSection() |
| 610 | { |
| 611 | // Check for pel severity of type - 0x51 = critical error, system |
| 612 | // termination |
| 613 | if (_uh->severity() == 0x51) |
| 614 | { |
| 615 | // Get the primary SRC section |
| 616 | auto pSRC = primarySRC(); |
| 617 | if (pSRC) |
| 618 | { |
| 619 | (*pSRC)->setTerminateBit(); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| Matt Spinler | 9d92109 | 2022-12-15 11:54:49 -0600 | [diff] [blame] | 624 | void PEL::addJournalSections(const message::Entry& regEntry, |
| 625 | const JournalBase& journal) |
| 626 | { |
| 627 | if (!regEntry.journalCapture) |
| 628 | { |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | // Write all unwritten journal data to disk. |
| 633 | journal.sync(); |
| 634 | |
| 635 | const auto& jc = regEntry.journalCapture.value(); |
| 636 | std::vector<std::vector<std::string>> allMessages; |
| 637 | |
| 638 | if (std::holds_alternative<size_t>(jc)) |
| 639 | { |
| 640 | // Get the previous numLines journal entries |
| 641 | const auto& numLines = std::get<size_t>(jc); |
| 642 | try |
| 643 | { |
| 644 | auto messages = journal.getMessages("", numLines); |
| 645 | if (!messages.empty()) |
| 646 | { |
| 647 | allMessages.push_back(std::move(messages)); |
| 648 | } |
| 649 | } |
| 650 | catch (const std::exception& e) |
| 651 | { |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 652 | lg2::error("Failed during journal collection: {ERROR}", "ERROR", e); |
| Matt Spinler | 9d92109 | 2022-12-15 11:54:49 -0600 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | else if (std::holds_alternative<message::AppCaptureList>(jc)) |
| 656 | { |
| 657 | // Get journal entries based on the syslog id field. |
| 658 | const auto& sections = std::get<message::AppCaptureList>(jc); |
| 659 | for (const auto& [syslogID, numLines] : sections) |
| 660 | { |
| 661 | try |
| 662 | { |
| 663 | auto messages = journal.getMessages(syslogID, numLines); |
| 664 | if (!messages.empty()) |
| 665 | { |
| 666 | allMessages.push_back(std::move(messages)); |
| 667 | } |
| 668 | } |
| 669 | catch (const std::exception& e) |
| 670 | { |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 671 | lg2::error("Failed during journal collection: {ERROR}", "ERROR", |
| 672 | e); |
| Matt Spinler | 9d92109 | 2022-12-15 11:54:49 -0600 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | // Create the UserData sections |
| 678 | for (const auto& messages : allMessages) |
| 679 | { |
| 680 | auto buffer = util::flattenLines(messages); |
| 681 | |
| 682 | // If the buffer is way too big, it can overflow the uint16_t |
| 683 | // PEL section size field that is checked below so do a cursory |
| 684 | // check here. |
| 685 | if (buffer.size() > _maxPELSize) |
| 686 | { |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 687 | lg2::warning( |
| 688 | "Journal UserData section does not fit in PEL, dropping. " |
| 689 | "PEL size = {PEL_SIZE}, data size = {DATA_SIZE}", |
| 690 | "PEL_SIZE", size(), "DATA_SIZE", buffer.size()); |
| Matt Spinler | 9d92109 | 2022-12-15 11:54:49 -0600 | [diff] [blame] | 691 | continue; |
| 692 | } |
| 693 | |
| 694 | // Sections must be 4 byte aligned. |
| 695 | while (buffer.size() % 4 != 0) |
| 696 | { |
| 697 | buffer.push_back(0); |
| 698 | } |
| 699 | |
| 700 | auto ud = std::make_unique<UserData>( |
| 701 | static_cast<uint16_t>(ComponentID::phosphorLogging), |
| 702 | static_cast<uint8_t>(UserDataFormat::text), |
| 703 | static_cast<uint8_t>(UserDataFormatVersion::text), buffer); |
| 704 | |
| 705 | if (size() + ud->header().size <= _maxPELSize) |
| 706 | { |
| 707 | _optionalSections.push_back(std::move(ud)); |
| 708 | } |
| 709 | else |
| 710 | { |
| 711 | // Don't attempt to shrink here since we'd be dropping the |
| 712 | // most recent journal entries which would be confusing. |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 713 | lg2::warning( |
| 714 | "Journal UserData section does not fit in PEL, dropping. " |
| 715 | "PEL size = {PEL_SIZE}, data size = {DATA_SIZE}", |
| 716 | "PEL_SIZE", size(), "DATA_SIZE", buffer.size()); |
| Matt Spinler | 9d92109 | 2022-12-15 11:54:49 -0600 | [diff] [blame] | 717 | ud.reset(); |
| 718 | continue; |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
| Arya K Padman | d8ae618 | 2024-07-19 06:25:10 -0500 | [diff] [blame] | 723 | void PEL::addAdDetailsForDIMMsCallout( |
| 724 | const std::unique_ptr<SRC>& src, const DataInterfaceBase& dataIface, |
| 725 | nlohmann::json& adSysInfoData, DebugData& debugData) |
| 726 | { |
| 727 | if (!src->callouts()) |
| 728 | { |
| 729 | // No callouts |
| 730 | return; |
| 731 | } |
| 732 | |
| Matt Spinler | cc06b7f | 2025-05-09 11:47:25 -0500 | [diff] [blame] | 733 | auto isDIMMCallout = [&dataIface](const auto& callout) { |
| Arya K Padman | d8ae618 | 2024-07-19 06:25:10 -0500 | [diff] [blame] | 734 | auto locCode{callout->locationCode()}; |
| 735 | if (locCode.empty()) |
| 736 | { |
| 737 | // Not a hardware callout. No action required |
| 738 | return false; |
| 739 | } |
| 740 | else |
| 741 | { |
| Arya K Padman | ced8ed7 | 2024-09-02 05:18:07 -0500 | [diff] [blame] | 742 | return const_cast<DataInterfaceBase&>(dataIface).isDIMM(locCode); |
| Arya K Padman | d8ae618 | 2024-07-19 06:25:10 -0500 | [diff] [blame] | 743 | } |
| 744 | }; |
| 745 | auto addAdDIMMDetails = [&dataIface, &adSysInfoData, |
| 746 | &debugData](const auto& callout) { |
| 747 | auto dimmLocCode{callout->locationCode()}; |
| 748 | |
| 749 | auto diPropVal = dataIface.getDIProperty(dimmLocCode); |
| 750 | if (!diPropVal.has_value()) |
| 751 | { |
| 752 | std::string errMsg{ |
| 753 | std::format("Failed reading DI property from " |
| 754 | "VINI Interface for the LocationCode:[{}]", |
| 755 | dimmLocCode)}; |
| 756 | debugData[AdDIMMInfoFetchError].emplace_back(errMsg); |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | util::addDIMMInfo(dimmLocCode, diPropVal.value(), adSysInfoData); |
| 761 | } |
| 762 | }; |
| 763 | |
| 764 | auto DIMMsCallouts = src->callouts()->callouts() | |
| 765 | std::views::filter(isDIMMCallout); |
| 766 | |
| 767 | std::ranges::for_each(DIMMsCallouts, addAdDIMMDetails); |
| 768 | } |
| 769 | |
| Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 770 | namespace util |
| 771 | { |
| 772 | |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 773 | std::unique_ptr<UserData> makeJSONUserDataSection(const nlohmann::json& json) |
| 774 | { |
| Matt Spinler | 13db1d3 | 2024-10-08 11:22:54 -0500 | [diff] [blame] | 775 | std::string jsonString; |
| 776 | try |
| 777 | { |
| 778 | jsonString = json.dump(); |
| 779 | } |
| 780 | catch (const std::exception& e) |
| 781 | { |
| 782 | lg2::error("json.dump() failed with: {ERROR}", "ERROR", e); |
| 783 | jsonString = "Invalid JSON passed to makeJSONUserDataSection!"; |
| 784 | } |
| 785 | |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 786 | std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end()); |
| 787 | |
| 788 | // Pad to a 4 byte boundary |
| 789 | while ((jsonData.size() % 4) != 0) |
| 790 | { |
| 791 | jsonData.push_back(0); |
| 792 | } |
| 793 | |
| 794 | return std::make_unique<UserData>( |
| 795 | static_cast<uint16_t>(ComponentID::phosphorLogging), |
| 796 | static_cast<uint8_t>(UserDataFormat::json), |
| 797 | static_cast<uint8_t>(UserDataFormatVersion::json), jsonData); |
| 798 | } |
| 799 | |
| Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 800 | std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad) |
| 801 | { |
| 802 | assert(!ad.empty()); |
| 803 | nlohmann::json json; |
| 804 | |
| 805 | // Remove the 'ESEL' entry, as it contains a full PEL in the value. |
| 806 | if (ad.getValue("ESEL")) |
| 807 | { |
| 808 | auto newAD = ad; |
| 809 | newAD.remove("ESEL"); |
| 810 | json = newAD.toJSON(); |
| 811 | } |
| 812 | else |
| 813 | { |
| 814 | json = ad.toJSON(); |
| 815 | } |
| 816 | |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 817 | return makeJSONUserDataSection(json); |
| 818 | } |
| Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 819 | |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 820 | void addProcessNameToJSON(nlohmann::json& json, |
| 821 | const std::optional<std::string>& pid, |
| 822 | const DataInterfaceBase& dataIface) |
| 823 | { |
| Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 824 | std::string name{unknownValue}; |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 825 | |
| 826 | try |
| Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 827 | { |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 828 | if (pid) |
| 829 | { |
| 830 | auto n = dataIface.getProcessName(*pid); |
| 831 | if (n) |
| 832 | { |
| 833 | name = *n; |
| 834 | } |
| 835 | } |
| 836 | } |
| Patrick Williams | 66491c6 | 2021-10-06 12:23:37 -0500 | [diff] [blame] | 837 | catch (const std::exception& e) |
| Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 838 | {} |
| Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 839 | |
| Sumit Kumar | 3160a54 | 2021-04-26 08:07:04 -0500 | [diff] [blame] | 840 | if (pid) |
| 841 | { |
| 842 | json["Process Name"] = std::move(name); |
| 843 | } |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 844 | } |
| 845 | |
| Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 846 | void addBMCFWVersionIDToJSON(nlohmann::json& json, |
| 847 | const DataInterfaceBase& dataIface) |
| 848 | { |
| 849 | auto id = dataIface.getBMCFWVersionID(); |
| 850 | if (id.empty()) |
| 851 | { |
| 852 | id = unknownValue; |
| 853 | } |
| 854 | |
| Matt Spinler | c2b8a51 | 2021-05-21 12:44:42 -0600 | [diff] [blame] | 855 | json["FW Version ID"] = std::move(id); |
| Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 856 | } |
| 857 | |
| Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 858 | std::string lastSegment(char separator, std::string data) |
| 859 | { |
| 860 | auto pos = data.find_last_of(separator); |
| 861 | if (pos != std::string::npos) |
| 862 | { |
| 863 | data = data.substr(pos + 1); |
| 864 | } |
| 865 | |
| 866 | return data; |
| 867 | } |
| 868 | |
| Ben Tyner | e32b7e7 | 2021-05-18 12:38:40 -0500 | [diff] [blame] | 869 | void addIMKeyword(nlohmann::json& json, const DataInterfaceBase& dataIface) |
| 870 | { |
| 871 | auto keyword = dataIface.getSystemIMKeyword(); |
| 872 | |
| 873 | std::string value{}; |
| 874 | |
| 875 | std::for_each(keyword.begin(), keyword.end(), [&](const auto& byte) { |
| Jayanth Othayoth | 1aa90d4 | 2023-09-13 04:25:45 -0500 | [diff] [blame] | 876 | value += std::format("{:02X}", byte); |
| Ben Tyner | e32b7e7 | 2021-05-18 12:38:40 -0500 | [diff] [blame] | 877 | }); |
| 878 | |
| 879 | json["System IM"] = value; |
| 880 | } |
| 881 | |
| Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 882 | void addStatesToJSON(nlohmann::json& json, const DataInterfaceBase& dataIface) |
| 883 | { |
| 884 | json["BMCState"] = lastSegment('.', dataIface.getBMCState()); |
| 885 | json["ChassisState"] = lastSegment('.', dataIface.getChassisState()); |
| 886 | json["HostState"] = lastSegment('.', dataIface.getHostState()); |
| Sumit Kumar | 2c36fdd | 2021-09-21 03:12:11 -0500 | [diff] [blame] | 887 | json["BootState"] = lastSegment('.', dataIface.getBootState()); |
| Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 888 | } |
| 889 | |
| George Liu | 9ac0d9b | 2022-07-15 10:57:38 +0800 | [diff] [blame] | 890 | void addBMCUptime(nlohmann::json& json, const DataInterfaceBase& dataIface) |
| 891 | { |
| 892 | auto seconds = dataIface.getUptimeInSeconds(); |
| 893 | if (seconds) |
| 894 | { |
| 895 | json["BMCUptime"] = dataIface.getBMCUptime(*seconds); |
| 896 | } |
| 897 | else |
| 898 | { |
| 899 | json["BMCUptime"] = ""; |
| 900 | } |
| 901 | json["BMCLoad"] = dataIface.getBMCLoadAvg(); |
| 902 | } |
| 903 | |
| Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 904 | std::unique_ptr<UserData> makeSysInfoUserDataSection( |
| 905 | const AdditionalData& ad, const DataInterfaceBase& dataIface, |
| Arya K Padman | d8ae618 | 2024-07-19 06:25:10 -0500 | [diff] [blame] | 906 | bool addUptime, const nlohmann::json& adSysInfoData) |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 907 | { |
| 908 | nlohmann::json json; |
| 909 | |
| 910 | addProcessNameToJSON(json, ad.getValue("_PID"), dataIface); |
| Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 911 | addBMCFWVersionIDToJSON(json, dataIface); |
| Ben Tyner | e32b7e7 | 2021-05-18 12:38:40 -0500 | [diff] [blame] | 912 | addIMKeyword(json, dataIface); |
| Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 913 | addStatesToJSON(json, dataIface); |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 914 | |
| George Liu | 9ac0d9b | 2022-07-15 10:57:38 +0800 | [diff] [blame] | 915 | if (addUptime) |
| 916 | { |
| 917 | addBMCUptime(json, dataIface); |
| 918 | } |
| 919 | |
| Arya K Padman | d8ae618 | 2024-07-19 06:25:10 -0500 | [diff] [blame] | 920 | if (!adSysInfoData.empty()) |
| 921 | { |
| 922 | json.update(adSysInfoData); |
| 923 | } |
| 924 | |
| Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 925 | return makeJSONUserDataSection(json); |
| Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 926 | } |
| 927 | |
| Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 928 | std::vector<uint8_t> readFD(int fd) |
| 929 | { |
| 930 | std::vector<uint8_t> data; |
| 931 | |
| 932 | // Get the size |
| 933 | struct stat s; |
| 934 | int r = fstat(fd, &s); |
| 935 | if (r != 0) |
| 936 | { |
| 937 | auto e = errno; |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 938 | lg2::error("Could not get FFDC file size from FD, errno = {ERRNO}", |
| 939 | "ERRNO", e); |
| Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 940 | return data; |
| 941 | } |
| 942 | |
| 943 | if (0 == s.st_size) |
| 944 | { |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 945 | lg2::error("FFDC file is empty"); |
| Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 946 | return data; |
| 947 | } |
| 948 | |
| 949 | data.resize(s.st_size); |
| 950 | |
| 951 | // Make sure its at the beginning, as maybe another |
| 952 | // extension already used it. |
| 953 | r = lseek(fd, 0, SEEK_SET); |
| 954 | if (r == -1) |
| 955 | { |
| 956 | auto e = errno; |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 957 | lg2::error("Could not seek to beginning of FFDC file, errno = {ERRNO}", |
| 958 | "ERRNO", e); |
| Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 959 | return data; |
| 960 | } |
| 961 | |
| 962 | r = read(fd, data.data(), s.st_size); |
| 963 | if (r == -1) |
| 964 | { |
| 965 | auto e = errno; |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 966 | lg2::error("Could not read FFDC file, errno = {ERRNO}", "ERRNO", e); |
| Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 967 | } |
| 968 | else if (r != s.st_size) |
| 969 | { |
| Matt Spinler | fd2da66 | 2023-07-07 16:25:14 -0500 | [diff] [blame] | 970 | lg2::warning("Could not read full FFDC file. " |
| 971 | "File size = {FSIZE}, Size read = {SIZE_READ}", |
| 972 | "FSIZE", s.st_size, "SIZE_READ", r); |
| Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | return data; |
| 976 | } |
| 977 | |
| Patrick Williams | 2529115 | 2025-02-01 08:21:42 -0500 | [diff] [blame] | 978 | std::unique_ptr<UserData> makeFFDCuserDataSection(uint16_t componentID, |
| 979 | const PelFFDCfile& file) |
| Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 980 | { |
| Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 981 | auto data = readFD(file.fd); |
| 982 | |
| 983 | if (data.empty()) |
| 984 | { |
| 985 | return std::unique_ptr<UserData>(); |
| 986 | } |
| 987 | |
| 988 | // The data needs 4 Byte alignment, and save amount padded for the |
| 989 | // CBOR case. |
| 990 | uint32_t pad = 0; |
| 991 | while (data.size() % 4) |
| 992 | { |
| 993 | data.push_back(0); |
| 994 | pad++; |
| 995 | } |
| 996 | |
| 997 | // For JSON, CBOR, and Text use our component ID, subType, and version, |
| 998 | // otherwise use the supplied ones. |
| 999 | uint16_t compID = static_cast<uint16_t>(ComponentID::phosphorLogging); |
| 1000 | uint8_t subType{}; |
| 1001 | uint8_t version{}; |
| 1002 | |
| 1003 | switch (file.format) |
| 1004 | { |
| 1005 | case UserDataFormat::json: |
| 1006 | subType = static_cast<uint8_t>(UserDataFormat::json); |
| 1007 | version = static_cast<uint8_t>(UserDataFormatVersion::json); |
| 1008 | break; |
| 1009 | case UserDataFormat::cbor: |
| 1010 | subType = static_cast<uint8_t>(UserDataFormat::cbor); |
| 1011 | version = static_cast<uint8_t>(UserDataFormatVersion::cbor); |
| 1012 | |
| 1013 | // The CBOR parser will fail on the extra pad bytes since they |
| 1014 | // aren't CBOR. Add the amount we padded to the end and other |
| 1015 | // code will remove it all before parsing. |
| 1016 | { |
| 1017 | data.resize(data.size() + 4); |
| 1018 | Stream stream{data}; |
| 1019 | stream.offset(data.size() - 4); |
| 1020 | stream << pad; |
| 1021 | } |
| 1022 | |
| 1023 | break; |
| 1024 | case UserDataFormat::text: |
| 1025 | subType = static_cast<uint8_t>(UserDataFormat::text); |
| 1026 | version = static_cast<uint8_t>(UserDataFormatVersion::text); |
| 1027 | break; |
| 1028 | case UserDataFormat::custom: |
| 1029 | default: |
| 1030 | // Use the passed in values |
| 1031 | compID = componentID; |
| 1032 | subType = file.subType; |
| 1033 | version = file.version; |
| 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | return std::make_unique<UserData>(compID, subType, version, data); |
| Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 1038 | } |
| 1039 | |
| Matt Spinler | 9d92109 | 2022-12-15 11:54:49 -0600 | [diff] [blame] | 1040 | std::vector<uint8_t> flattenLines(const std::vector<std::string>& lines) |
| 1041 | { |
| 1042 | std::vector<uint8_t> out; |
| 1043 | |
| 1044 | for (const auto& line : lines) |
| 1045 | { |
| 1046 | out.insert(out.end(), line.begin(), line.end()); |
| 1047 | |
| 1048 | if (out.back() != '\n') |
| 1049 | { |
| 1050 | out.push_back('\n'); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | return out; |
| 1055 | } |
| 1056 | |
| Arya K Padman | d8ae618 | 2024-07-19 06:25:10 -0500 | [diff] [blame] | 1057 | void addDIMMInfo(const std::string& locationCode, |
| 1058 | const std::vector<std::uint8_t>& diPropVal, |
| 1059 | nlohmann::json& adSysInfoData) |
| 1060 | { |
| 1061 | nlohmann::json dimmInfoObj; |
| 1062 | dimmInfoObj["Location Code"] = locationCode; |
| 1063 | std::ranges::transform( |
| 1064 | diPropVal, std::back_inserter(dimmInfoObj["DRAM Manufacturer ID"]), |
| 1065 | [](const auto& diPropEachByte) { |
| 1066 | return std::format("{:#04x}", diPropEachByte); |
| 1067 | }); |
| 1068 | adSysInfoData["DIMMs Additional Info"] += dimmInfoObj; |
| 1069 | } |
| 1070 | |
| Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 1071 | } // namespace util |
| 1072 | |
| Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 1073 | } // namespace pels |
| 1074 | } // namespace openpower |