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