Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 16 | #include "pel.hpp" |
| 17 | |
| 18 | #include "bcd_time.hpp" |
Matt Spinler | 386a61e | 2020-08-13 15:51:12 -0500 | [diff] [blame] | 19 | #include "extended_user_data.hpp" |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 20 | #include "extended_user_header.hpp" |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 21 | #include "failing_mtms.hpp" |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 22 | #include "json_utils.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 23 | #include "log_id.hpp" |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 24 | #include "pel_rules.hpp" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 25 | #include "pel_values.hpp" |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 26 | #include "section_factory.hpp" |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 27 | #include "src.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 28 | #include "stream.hpp" |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 29 | #include "user_data_formats.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 30 | |
Ben Tyner | e32b7e7 | 2021-05-18 12:38:40 -0500 | [diff] [blame] | 31 | #include <fmt/format.h> |
Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 32 | #include <sys/stat.h> |
| 33 | #include <unistd.h> |
| 34 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 35 | #include <iostream> |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 36 | #include <phosphor-logging/log.hpp> |
| 37 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 38 | namespace openpower |
| 39 | { |
| 40 | namespace pels |
| 41 | { |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 42 | namespace message = openpower::pels::message; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 43 | namespace pv = openpower::pels::pel_values; |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 44 | using namespace phosphor::logging; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 45 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 46 | constexpr auto unknownValue = "Unknown"; |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 47 | constexpr uint8_t jsonCalloutSubtype = 0xCA; |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 48 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 49 | PEL::PEL(const message::Entry& regEntry, uint32_t obmcLogID, uint64_t timestamp, |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 50 | phosphor::logging::Entry::Level severity, |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 51 | const AdditionalData& additionalData, const PelFFDC& ffdcFiles, |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 52 | const DataInterfaceBase& dataIface) |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 53 | { |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 54 | std::map<std::string, std::vector<std::string>> debugData; |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 55 | nlohmann::json callouts; |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 56 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 57 | _ph = std::make_unique<PrivateHeader>(regEntry.componentID, obmcLogID, |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 58 | timestamp); |
Vijay Lobo | 6b3f345 | 2021-04-15 23:04:42 -0500 | [diff] [blame] | 59 | _uh = std::make_unique<UserHeader>(regEntry, severity, additionalData, |
| 60 | dataIface); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 61 | |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 62 | // Extract any callouts embedded in an FFDC file. |
| 63 | if (!ffdcFiles.empty()) |
| 64 | { |
| 65 | try |
| 66 | { |
| 67 | callouts = getCalloutJSON(ffdcFiles); |
| 68 | } |
| 69 | catch (const std::exception& e) |
| 70 | { |
| 71 | debugData.emplace("FFDC file JSON callouts error", |
| 72 | std::vector<std::string>{e.what()}); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | auto src = |
| 77 | std::make_unique<SRC>(regEntry, additionalData, callouts, dataIface); |
| 78 | |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 79 | if (!src->getDebugData().empty()) |
| 80 | { |
| 81 | // Something didn't go as planned |
| 82 | debugData.emplace("SRC", src->getDebugData()); |
| 83 | } |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 84 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 85 | auto euh = std::make_unique<ExtendedUserHeader>(dataIface, regEntry, *src); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 86 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 87 | _optionalSections.push_back(std::move(src)); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 88 | _optionalSections.push_back(std::move(euh)); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 89 | |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 90 | auto mtms = std::make_unique<FailingMTMS>(dataIface); |
| 91 | _optionalSections.push_back(std::move(mtms)); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 92 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 93 | auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface); |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 94 | addUserDataSection(std::move(ud)); |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 95 | |
Matt Spinler | 9b7e94f | 2020-03-24 15:44:41 -0500 | [diff] [blame] | 96 | // Create a UserData section from AdditionalData. |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 97 | if (!additionalData.empty()) |
| 98 | { |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 99 | ud = util::makeADUserDataSection(additionalData); |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 100 | addUserDataSection(std::move(ud)); |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 103 | // Add any FFDC files into UserData sections |
| 104 | for (const auto& file : ffdcFiles) |
| 105 | { |
| 106 | ud = util::makeFFDCuserDataSection(regEntry.componentID, file); |
| 107 | if (!ud) |
| 108 | { |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 109 | // Add this error into the debug data UserData section |
| 110 | std::ostringstream msg; |
| 111 | msg << "Could not make PEL FFDC UserData section from file" |
| 112 | << std::hex << regEntry.componentID << " " << file.subType |
| 113 | << " " << file.version; |
| 114 | if (debugData.count("FFDC File")) |
| 115 | { |
| 116 | debugData.at("FFDC File").push_back(msg.str()); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | debugData.emplace("FFDC File", |
| 121 | std::vector<std::string>{msg.str()}); |
| 122 | } |
| 123 | |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 124 | continue; |
| 125 | } |
| 126 | |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 127 | addUserDataSection(std::move(ud)); |
| 128 | } |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 129 | |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 130 | // Store in the PEL any important debug data created while |
| 131 | // building the PEL sections. |
| 132 | if (!debugData.empty()) |
| 133 | { |
| 134 | nlohmann::json data; |
| 135 | data["PEL Internal Debug Data"] = debugData; |
| 136 | ud = util::makeJSONUserDataSection(data); |
| 137 | |
| 138 | addUserDataSection(std::move(ud)); |
| 139 | |
| 140 | // Also put in the journal for debug |
| 141 | for (const auto& [name, data] : debugData) |
| 142 | { |
| 143 | for (const auto& message : data) |
| 144 | { |
| 145 | std::string entry = name + ": " + message; |
| 146 | log<level::INFO>(entry.c_str()); |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 147 | } |
| 148 | } |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 151 | _ph->setSectionCount(2 + _optionalSections.size()); |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 152 | |
| 153 | checkRulesAndFix(); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 154 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 155 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 156 | PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 157 | { |
| 158 | } |
| 159 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 160 | PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 161 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 162 | populateFromRawData(data, obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 163 | } |
| 164 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 165 | void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 166 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 167 | Stream pelData{data}; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 168 | _ph = std::make_unique<PrivateHeader>(pelData); |
| 169 | if (obmcLogID != 0) |
| 170 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 171 | _ph->setOBMCLogID(obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | _uh = std::make_unique<UserHeader>(pelData); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 175 | |
| 176 | // Use the section factory to create the rest of the objects |
| 177 | for (size_t i = 2; i < _ph->sectionCount(); i++) |
| 178 | { |
| 179 | auto section = section_factory::create(pelData); |
| 180 | _optionalSections.push_back(std::move(section)); |
| 181 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool PEL::valid() const |
| 185 | { |
| 186 | bool valid = _ph->valid(); |
| 187 | |
| 188 | if (valid) |
| 189 | { |
| 190 | valid = _uh->valid(); |
| 191 | } |
| 192 | |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 193 | if (valid) |
| 194 | { |
| 195 | if (!std::all_of(_optionalSections.begin(), _optionalSections.end(), |
| 196 | [](const auto& section) { return section->valid(); })) |
| 197 | { |
| 198 | valid = false; |
| 199 | } |
| 200 | } |
| 201 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 202 | return valid; |
| 203 | } |
| 204 | |
| 205 | void PEL::setCommitTime() |
| 206 | { |
| 207 | auto now = std::chrono::system_clock::now(); |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 208 | _ph->setCommitTimestamp(getBCDTime(now)); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void PEL::assignID() |
| 212 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 213 | _ph->setID(generatePELID()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 216 | void PEL::flatten(std::vector<uint8_t>& pelBuffer) const |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 217 | { |
| 218 | Stream pelData{pelBuffer}; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 219 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 220 | if (!valid()) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 221 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 222 | log<level::WARNING>("Unflattening an invalid PEL"); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 225 | _ph->flatten(pelData); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 226 | _uh->flatten(pelData); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 227 | |
| 228 | for (auto& section : _optionalSections) |
| 229 | { |
| 230 | section->flatten(pelData); |
| 231 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 232 | } |
| 233 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 234 | std::vector<uint8_t> PEL::data() const |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 235 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 236 | std::vector<uint8_t> pelData; |
| 237 | flatten(pelData); |
| 238 | return pelData; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 239 | } |
| 240 | |
Matt Spinler | f1b46ff | 2020-01-22 14:10:04 -0600 | [diff] [blame] | 241 | size_t PEL::size() const |
| 242 | { |
| 243 | size_t size = 0; |
| 244 | |
| 245 | if (_ph) |
| 246 | { |
| 247 | size += _ph->header().size; |
| 248 | } |
| 249 | |
| 250 | if (_uh) |
| 251 | { |
| 252 | size += _uh->header().size; |
| 253 | } |
| 254 | |
| 255 | for (const auto& section : _optionalSections) |
| 256 | { |
| 257 | size += section->header().size; |
| 258 | } |
| 259 | |
| 260 | return size; |
| 261 | } |
| 262 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 263 | std::optional<SRC*> PEL::primarySRC() const |
| 264 | { |
| 265 | auto src = std::find_if( |
| 266 | _optionalSections.begin(), _optionalSections.end(), [](auto& section) { |
| 267 | return section->header().id == |
| 268 | static_cast<uint16_t>(SectionID::primarySRC); |
| 269 | }); |
| 270 | if (src != _optionalSections.end()) |
| 271 | { |
| 272 | return static_cast<SRC*>(src->get()); |
| 273 | } |
| 274 | |
| 275 | return std::nullopt; |
| 276 | } |
| 277 | |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 278 | void PEL::checkRulesAndFix() |
| 279 | { |
Matt Spinler | 1f93c59 | 2020-09-10 10:43:08 -0500 | [diff] [blame] | 280 | // Only fix if the action flags are at their default value which |
| 281 | // means they weren't specified in the registry. Otherwise |
| 282 | // assume the user knows what they are doing. |
| 283 | if (_uh->actionFlags() == actionFlagsDefault) |
| 284 | { |
| 285 | auto [actionFlags, eventType] = |
| 286 | pel_rules::check(0, _uh->eventType(), _uh->severity()); |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 287 | |
Matt Spinler | 1f93c59 | 2020-09-10 10:43:08 -0500 | [diff] [blame] | 288 | _uh->setActionFlags(actionFlags); |
| 289 | _uh->setEventType(eventType); |
| 290 | } |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 291 | } |
| 292 | |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 293 | void PEL::printSectionInJSON(const Section& section, std::string& buf, |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 294 | std::map<uint16_t, size_t>& pluralSections, |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 295 | message::Registry& registry, |
| 296 | const std::vector<std::string>& plugins, |
| 297 | uint8_t creatorID) const |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 298 | { |
| 299 | char tmpB[5]; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 300 | uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8), |
| 301 | static_cast<uint8_t>(section.header().id)}; |
| 302 | sprintf(tmpB, "%c%c", id[0], id[1]); |
| 303 | std::string sectionID(tmpB); |
| 304 | std::string sectionName = pv::sectionTitles.count(sectionID) |
| 305 | ? pv::sectionTitles.at(sectionID) |
| 306 | : "Unknown Section"; |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 307 | |
| 308 | // Add a count if there are multiple of this type of section |
| 309 | auto count = pluralSections.find(section.header().id); |
| 310 | if (count != pluralSections.end()) |
| 311 | { |
| 312 | sectionName += " " + std::to_string(count->second); |
| 313 | count->second++; |
| 314 | } |
| 315 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 316 | if (section.valid()) |
| 317 | { |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 318 | std::optional<std::string> json; |
| 319 | if (sectionID == "PS" || sectionID == "SS") |
| 320 | { |
Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 321 | json = section.getJSON(registry, plugins, creatorID); |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 322 | } |
Matt Spinler | 386a61e | 2020-08-13 15:51:12 -0500 | [diff] [blame] | 323 | else if ((sectionID == "UD") || (sectionID == "ED")) |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 324 | { |
Harisuddin Mohamed Isa | 3fdcd4e | 2020-08-26 11:56:42 +0800 | [diff] [blame] | 325 | json = section.getJSON(creatorID, plugins); |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 326 | } |
| 327 | else |
| 328 | { |
| 329 | json = section.getJSON(); |
| 330 | } |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 331 | |
| 332 | buf += "\"" + sectionName + "\": {\n"; |
| 333 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 334 | if (json) |
| 335 | { |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 336 | buf += *json + "\n},\n"; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 337 | } |
| 338 | else |
| 339 | { |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 340 | jsonInsert(buf, pv::sectionVer, |
| 341 | getNumberString("%d", section.header().version), 1); |
| 342 | jsonInsert(buf, pv::subSection, |
| 343 | getNumberString("%d", section.header().subType), 1); |
| 344 | jsonInsert(buf, pv::createdBy, |
| 345 | getNumberString("0x%X", section.header().componentID), |
| 346 | 1); |
| 347 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 348 | std::vector<uint8_t> data; |
| 349 | Stream s{data}; |
| 350 | section.flatten(s); |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 351 | std::string dstr = |
| 352 | dumpHex(std::data(data) + SectionHeader::flattenedSize(), |
Harisuddin Mohamed Isa | 097ad12 | 2020-06-11 21:19:41 +0800 | [diff] [blame] | 353 | data.size() - SectionHeader::flattenedSize(), 2); |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 354 | std::string jsonIndent(indentLevel, 0x20); |
| 355 | buf += jsonIndent + "\"Data\": [\n"; |
| 356 | buf += dstr; |
| 357 | buf += jsonIndent + "]\n"; |
| 358 | buf += "},\n"; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 359 | } |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 360 | } |
| 361 | else |
| 362 | { |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 363 | buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n"; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 367 | std::map<uint16_t, size_t> PEL::getPluralSections() const |
| 368 | { |
| 369 | std::map<uint16_t, size_t> sectionCounts; |
| 370 | |
| 371 | for (const auto& section : optionalSections()) |
| 372 | { |
| 373 | if (sectionCounts.find(section->header().id) == sectionCounts.end()) |
| 374 | { |
| 375 | sectionCounts[section->header().id] = 1; |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | sectionCounts[section->header().id]++; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | std::map<uint16_t, size_t> sections; |
| 384 | for (const auto& [id, count] : sectionCounts) |
| 385 | { |
| 386 | if (count > 1) |
| 387 | { |
| 388 | // Start with 0 here and printSectionInJSON() |
| 389 | // will increment it as it goes. |
| 390 | sections.emplace(id, 0); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return sections; |
| 395 | } |
| 396 | |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 397 | void PEL::toJSON(message::Registry& registry, |
| 398 | const std::vector<std::string>& plugins) const |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 399 | { |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 400 | auto sections = getPluralSections(); |
| 401 | |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 402 | std::string buf = "{\n"; |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 403 | printSectionInJSON(*(_ph.get()), buf, sections, registry, plugins); |
| 404 | printSectionInJSON(*(_uh.get()), buf, sections, registry, plugins); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 405 | for (auto& section : this->optionalSections()) |
| 406 | { |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 407 | printSectionInJSON(*(section.get()), buf, sections, registry, plugins, |
| 408 | _ph->creatorID()); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 409 | } |
| 410 | buf += "}"; |
| 411 | std::size_t found = buf.rfind(","); |
| 412 | if (found != std::string::npos) |
| 413 | buf.replace(found, 1, ""); |
| 414 | std::cout << buf << std::endl; |
| 415 | } |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 416 | |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 417 | bool PEL::addUserDataSection(std::unique_ptr<UserData> userData) |
| 418 | { |
| 419 | if (size() + userData->header().size > _maxPELSize) |
| 420 | { |
| 421 | if (userData->shrink(_maxPELSize - size())) |
| 422 | { |
| 423 | _optionalSections.push_back(std::move(userData)); |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | log<level::WARNING>( |
| 428 | "Could not shrink UserData section. Dropping", |
| 429 | entry("SECTION_SIZE=%d\n", userData->header().size), |
| 430 | entry("COMPONENT_ID=0x%02X", userData->header().componentID), |
| 431 | entry("SUBTYPE=0x%X", userData->header().subType), |
| 432 | entry("VERSION=0x%X", userData->header().version)); |
| 433 | return false; |
| 434 | } |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | _optionalSections.push_back(std::move(userData)); |
| 439 | } |
| 440 | return true; |
| 441 | } |
| 442 | |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 443 | nlohmann::json PEL::getCalloutJSON(const PelFFDC& ffdcFiles) |
| 444 | { |
| 445 | nlohmann::json callouts; |
| 446 | |
| 447 | for (const auto& file : ffdcFiles) |
| 448 | { |
| 449 | if ((file.format == UserDataFormat::json) && |
| 450 | (file.subType == jsonCalloutSubtype)) |
| 451 | { |
| 452 | auto data = util::readFD(file.fd); |
| 453 | if (data.empty()) |
| 454 | { |
| 455 | throw std::runtime_error{ |
| 456 | "Could not get data from JSON callout file descriptor"}; |
| 457 | } |
| 458 | |
| 459 | std::string jsonString{data.begin(), data.begin() + data.size()}; |
| 460 | |
| 461 | callouts = nlohmann::json::parse(jsonString); |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return callouts; |
| 467 | } |
| 468 | |
Andrew Geissler | 44fc316 | 2020-07-09 09:21:31 -0500 | [diff] [blame] | 469 | bool PEL::isCalloutPresent() const |
| 470 | { |
| 471 | auto pSRC = primarySRC(); |
| 472 | if (!pSRC) |
| 473 | { |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | bool calloutPresent = false; |
| 478 | if ((*pSRC)->callouts()) |
| 479 | { |
| 480 | for (auto& i : (*pSRC)->callouts()->callouts()) |
| 481 | { |
| 482 | if (((*i).fruIdentity())) |
| 483 | { |
| 484 | auto& fruId = (*i).fruIdentity(); |
| 485 | if ((*fruId).failingComponentType() != 0) |
| 486 | { |
| 487 | calloutPresent = true; |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return calloutPresent; |
| 495 | } |
| 496 | |
Sumit Kumar | 3160a54 | 2021-04-26 08:07:04 -0500 | [diff] [blame] | 497 | void PEL::updateSysInfoInExtendedUserDataSection( |
| 498 | const DataInterfaceBase& dataIface) |
| 499 | { |
| 500 | const AdditionalData additionalData; |
| 501 | |
| 502 | // Check for PEL from Hostboot |
| 503 | if (_ph->creatorID() == static_cast<uint8_t>(CreatorID::hostboot)) |
| 504 | { |
| 505 | // Get the ED section from PEL |
| 506 | auto op = std::find_if(_optionalSections.begin(), |
| 507 | _optionalSections.end(), [](auto& section) { |
| 508 | return section->header().id == |
| 509 | static_cast<uint16_t>( |
| 510 | SectionID::extUserData); |
| 511 | }); |
| 512 | |
| 513 | // Check for ED section found and its not the last section of PEL |
| 514 | if (op != _optionalSections.end()) |
| 515 | { |
| 516 | // Get the extended user data class mapped to found section |
| 517 | auto extUserData = static_cast<ExtendedUserData*>(op->get()); |
| 518 | |
| 519 | // Check for the creator ID is for OpenBMC |
| 520 | if (extUserData->creatorID() == |
| 521 | static_cast<uint8_t>(CreatorID::openBMC)) |
| 522 | { |
| 523 | // Update subtype and component id |
| 524 | auto subType = static_cast<uint8_t>(UserDataFormat::json); |
| 525 | auto componentId = |
| 526 | static_cast<uint16_t>(ComponentID::phosphorLogging); |
| 527 | |
| 528 | // Update system data to ED section |
| 529 | auto ud = |
| 530 | util::makeSysInfoUserDataSection(additionalData, dataIface); |
| 531 | extUserData->updateDataSection(subType, componentId, |
| 532 | ud->data()); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 538 | namespace util |
| 539 | { |
| 540 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 541 | std::unique_ptr<UserData> makeJSONUserDataSection(const nlohmann::json& json) |
| 542 | { |
| 543 | auto jsonString = json.dump(); |
| 544 | std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end()); |
| 545 | |
| 546 | // Pad to a 4 byte boundary |
| 547 | while ((jsonData.size() % 4) != 0) |
| 548 | { |
| 549 | jsonData.push_back(0); |
| 550 | } |
| 551 | |
| 552 | return std::make_unique<UserData>( |
| 553 | static_cast<uint16_t>(ComponentID::phosphorLogging), |
| 554 | static_cast<uint8_t>(UserDataFormat::json), |
| 555 | static_cast<uint8_t>(UserDataFormatVersion::json), jsonData); |
| 556 | } |
| 557 | |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 558 | std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad) |
| 559 | { |
| 560 | assert(!ad.empty()); |
| 561 | nlohmann::json json; |
| 562 | |
| 563 | // Remove the 'ESEL' entry, as it contains a full PEL in the value. |
| 564 | if (ad.getValue("ESEL")) |
| 565 | { |
| 566 | auto newAD = ad; |
| 567 | newAD.remove("ESEL"); |
| 568 | json = newAD.toJSON(); |
| 569 | } |
| 570 | else |
| 571 | { |
| 572 | json = ad.toJSON(); |
| 573 | } |
| 574 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 575 | return makeJSONUserDataSection(json); |
| 576 | } |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 577 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 578 | void addProcessNameToJSON(nlohmann::json& json, |
| 579 | const std::optional<std::string>& pid, |
| 580 | const DataInterfaceBase& dataIface) |
| 581 | { |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 582 | std::string name{unknownValue}; |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 583 | |
| 584 | try |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 585 | { |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 586 | if (pid) |
| 587 | { |
| 588 | auto n = dataIface.getProcessName(*pid); |
| 589 | if (n) |
| 590 | { |
| 591 | name = *n; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | catch (std::exception& e) |
| 596 | { |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 597 | } |
| 598 | |
Sumit Kumar | 3160a54 | 2021-04-26 08:07:04 -0500 | [diff] [blame] | 599 | if (pid) |
| 600 | { |
| 601 | json["Process Name"] = std::move(name); |
| 602 | } |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 603 | } |
| 604 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 605 | void addBMCFWVersionIDToJSON(nlohmann::json& json, |
| 606 | const DataInterfaceBase& dataIface) |
| 607 | { |
| 608 | auto id = dataIface.getBMCFWVersionID(); |
| 609 | if (id.empty()) |
| 610 | { |
| 611 | id = unknownValue; |
| 612 | } |
| 613 | |
Matt Spinler | c2b8a51 | 2021-05-21 12:44:42 -0600 | [diff] [blame] | 614 | json["FW Version ID"] = std::move(id); |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 615 | } |
| 616 | |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 617 | std::string lastSegment(char separator, std::string data) |
| 618 | { |
| 619 | auto pos = data.find_last_of(separator); |
| 620 | if (pos != std::string::npos) |
| 621 | { |
| 622 | data = data.substr(pos + 1); |
| 623 | } |
| 624 | |
| 625 | return data; |
| 626 | } |
| 627 | |
Ben Tyner | e32b7e7 | 2021-05-18 12:38:40 -0500 | [diff] [blame] | 628 | void addIMKeyword(nlohmann::json& json, const DataInterfaceBase& dataIface) |
| 629 | { |
| 630 | auto keyword = dataIface.getSystemIMKeyword(); |
| 631 | |
| 632 | std::string value{}; |
| 633 | |
| 634 | std::for_each(keyword.begin(), keyword.end(), [&](const auto& byte) { |
| 635 | value += fmt::format("{:02X}", byte); |
| 636 | }); |
| 637 | |
| 638 | json["System IM"] = value; |
| 639 | } |
| 640 | |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 641 | void addStatesToJSON(nlohmann::json& json, const DataInterfaceBase& dataIface) |
| 642 | { |
| 643 | json["BMCState"] = lastSegment('.', dataIface.getBMCState()); |
| 644 | json["ChassisState"] = lastSegment('.', dataIface.getChassisState()); |
| 645 | json["HostState"] = lastSegment('.', dataIface.getHostState()); |
| 646 | } |
| 647 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 648 | std::unique_ptr<UserData> |
| 649 | makeSysInfoUserDataSection(const AdditionalData& ad, |
| 650 | const DataInterfaceBase& dataIface) |
| 651 | { |
| 652 | nlohmann::json json; |
| 653 | |
| 654 | addProcessNameToJSON(json, ad.getValue("_PID"), dataIface); |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 655 | addBMCFWVersionIDToJSON(json, dataIface); |
Ben Tyner | e32b7e7 | 2021-05-18 12:38:40 -0500 | [diff] [blame] | 656 | addIMKeyword(json, dataIface); |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 657 | addStatesToJSON(json, dataIface); |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 658 | |
| 659 | return makeJSONUserDataSection(json); |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 660 | } |
| 661 | |
Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 662 | std::vector<uint8_t> readFD(int fd) |
| 663 | { |
| 664 | std::vector<uint8_t> data; |
| 665 | |
| 666 | // Get the size |
| 667 | struct stat s; |
| 668 | int r = fstat(fd, &s); |
| 669 | if (r != 0) |
| 670 | { |
| 671 | auto e = errno; |
| 672 | log<level::ERR>("Could not get FFDC file size from FD", |
| 673 | entry("ERRNO=%d", e)); |
| 674 | return data; |
| 675 | } |
| 676 | |
| 677 | if (0 == s.st_size) |
| 678 | { |
| 679 | log<level::ERR>("FFDC file is empty"); |
| 680 | return data; |
| 681 | } |
| 682 | |
| 683 | data.resize(s.st_size); |
| 684 | |
| 685 | // Make sure its at the beginning, as maybe another |
| 686 | // extension already used it. |
| 687 | r = lseek(fd, 0, SEEK_SET); |
| 688 | if (r == -1) |
| 689 | { |
| 690 | auto e = errno; |
| 691 | log<level::ERR>("Could not seek to beginning of FFDC file", |
| 692 | entry("ERRNO=%d", e)); |
| 693 | return data; |
| 694 | } |
| 695 | |
| 696 | r = read(fd, data.data(), s.st_size); |
| 697 | if (r == -1) |
| 698 | { |
| 699 | auto e = errno; |
| 700 | log<level::ERR>("Could not read FFDC file", entry("ERRNO=%d", e)); |
| 701 | } |
| 702 | else if (r != s.st_size) |
| 703 | { |
| 704 | log<level::WARNING>("Could not read full FFDC file", |
| 705 | entry("FILE_SIZE=%d", s.st_size), |
| 706 | entry("SIZE_READ=%d", r)); |
| 707 | } |
| 708 | |
| 709 | return data; |
| 710 | } |
| 711 | |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 712 | std::unique_ptr<UserData> makeFFDCuserDataSection(uint16_t componentID, |
| 713 | const PelFFDCfile& file) |
| 714 | { |
Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 715 | auto data = readFD(file.fd); |
| 716 | |
| 717 | if (data.empty()) |
| 718 | { |
| 719 | return std::unique_ptr<UserData>(); |
| 720 | } |
| 721 | |
| 722 | // The data needs 4 Byte alignment, and save amount padded for the |
| 723 | // CBOR case. |
| 724 | uint32_t pad = 0; |
| 725 | while (data.size() % 4) |
| 726 | { |
| 727 | data.push_back(0); |
| 728 | pad++; |
| 729 | } |
| 730 | |
| 731 | // For JSON, CBOR, and Text use our component ID, subType, and version, |
| 732 | // otherwise use the supplied ones. |
| 733 | uint16_t compID = static_cast<uint16_t>(ComponentID::phosphorLogging); |
| 734 | uint8_t subType{}; |
| 735 | uint8_t version{}; |
| 736 | |
| 737 | switch (file.format) |
| 738 | { |
| 739 | case UserDataFormat::json: |
| 740 | subType = static_cast<uint8_t>(UserDataFormat::json); |
| 741 | version = static_cast<uint8_t>(UserDataFormatVersion::json); |
| 742 | break; |
| 743 | case UserDataFormat::cbor: |
| 744 | subType = static_cast<uint8_t>(UserDataFormat::cbor); |
| 745 | version = static_cast<uint8_t>(UserDataFormatVersion::cbor); |
| 746 | |
| 747 | // The CBOR parser will fail on the extra pad bytes since they |
| 748 | // aren't CBOR. Add the amount we padded to the end and other |
| 749 | // code will remove it all before parsing. |
| 750 | { |
| 751 | data.resize(data.size() + 4); |
| 752 | Stream stream{data}; |
| 753 | stream.offset(data.size() - 4); |
| 754 | stream << pad; |
| 755 | } |
| 756 | |
| 757 | break; |
| 758 | case UserDataFormat::text: |
| 759 | subType = static_cast<uint8_t>(UserDataFormat::text); |
| 760 | version = static_cast<uint8_t>(UserDataFormatVersion::text); |
| 761 | break; |
| 762 | case UserDataFormat::custom: |
| 763 | default: |
| 764 | // Use the passed in values |
| 765 | compID = componentID; |
| 766 | subType = file.subType; |
| 767 | version = file.version; |
| 768 | break; |
| 769 | } |
| 770 | |
| 771 | return std::make_unique<UserData>(compID, subType, version, data); |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 772 | } |
| 773 | |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 774 | } // namespace util |
| 775 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 776 | } // namespace pels |
| 777 | } // namespace openpower |