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