blob: ed4c3f357bc7c122934bbad90d7b8b1853ff5fc8 [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
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 Othayothda9b5832021-11-05 04:19:43 -050016#include "config.h"
17
Matt Spinlercb6b0592019-07-16 15:58:51 -050018#include "pel.hpp"
19
20#include "bcd_time.hpp"
Matt Spinler386a61e2020-08-13 15:51:12 -050021#include "extended_user_data.hpp"
Matt Spinlerc63e2e82019-12-02 15:50:12 -060022#include "extended_user_header.hpp"
Matt Spinleraa659472019-10-23 09:26:48 -050023#include "failing_mtms.hpp"
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080024#include "json_utils.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -050025#include "log_id.hpp"
Matt Spinlerf1e85e22019-11-01 11:31:31 -050026#include "pel_rules.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050027#include "pel_values.hpp"
Matt Spinler131870c2019-09-25 13:29:04 -050028#include "section_factory.hpp"
Matt Spinlerbd716f02019-10-15 10:54:11 -050029#include "src.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -050030#include "stream.hpp"
Matt Spinlerafa857c2019-10-24 13:03:46 -050031#include "user_data_formats.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -050032
Jayanth Othayoth92b20662021-11-05 00:09:15 -050033#ifdef PEL_ENABLE_PHAL
Jayanth Othayothda9b5832021-11-05 04:19:43 -050034#include "phal_service_actions.hpp"
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050035#include "sbe_ffdc_handler.hpp"
36#endif
37
Ben Tynere32b7e72021-05-18 12:38:40 -050038#include <fmt/format.h>
Matt Spinler5b289b22020-03-26 14:27:19 -050039#include <sys/stat.h>
40#include <unistd.h>
41
Aatir186ce8c2019-10-20 15:13:39 -050042#include <iostream>
Matt Spinler07eefc52019-09-26 11:18:26 -050043#include <phosphor-logging/log.hpp>
44
Matt Spinlercb6b0592019-07-16 15:58:51 -050045namespace openpower
46{
47namespace pels
48{
Matt Spinlerb8323632019-09-20 15:11:04 -050049namespace message = openpower::pels::message;
Aatir186ce8c2019-10-20 15:13:39 -050050namespace pv = openpower::pels::pel_values;
Matt Spinler4bfc9082020-03-24 15:05:54 -050051using namespace phosphor::logging;
Matt Spinlerb8323632019-09-20 15:11:04 -050052
Matt Spinler677381b2020-01-23 10:04:29 -060053constexpr auto unknownValue = "Unknown";
54
Matt Spinler4bfc9082020-03-24 15:05:54 -050055PEL::PEL(const message::Entry& regEntry, uint32_t obmcLogID, uint64_t timestamp,
Matt Spinlerbd716f02019-10-15 10:54:11 -050056 phosphor::logging::Entry::Level severity,
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050057 const AdditionalData& additionalData, const PelFFDC& ffdcFilesIn,
Matt Spinleraa659472019-10-23 09:26:48 -050058 const DataInterfaceBase& dataIface)
Matt Spinlerb8323632019-09-20 15:11:04 -050059{
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050060 // No changes in input, for non SBE error related requests
61 PelFFDC ffdcFiles = ffdcFilesIn;
62
Jayanth Othayoth92b20662021-11-05 00:09:15 -050063#ifdef PEL_ENABLE_PHAL
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050064 // 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 Spinler85f61a62020-06-03 16:28:55 -050084 std::map<std::string, std::vector<std::string>> debugData;
Matt Spinler5a90a952020-08-27 09:39:03 -050085 nlohmann::json callouts;
Matt Spinler85f61a62020-06-03 16:28:55 -050086
Matt Spinler4bfc9082020-03-24 15:05:54 -050087 _ph = std::make_unique<PrivateHeader>(regEntry.componentID, obmcLogID,
Matt Spinlerb8323632019-09-20 15:11:04 -050088 timestamp);
Vijay Lobo6b3f3452021-04-15 23:04:42 -050089 _uh = std::make_unique<UserHeader>(regEntry, severity, additionalData,
90 dataIface);
Matt Spinlerb8323632019-09-20 15:11:04 -050091
Matt Spinler5a90a952020-08-27 09:39:03 -050092 // 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 Spinler85f61a62020-06-03 16:28:55 -0500109 if (!src->getDebugData().empty())
110 {
111 // Something didn't go as planned
112 debugData.emplace("SRC", src->getDebugData());
113 }
Matt Spinlerc63e2e82019-12-02 15:50:12 -0600114
Matt Spinler4bfc9082020-03-24 15:05:54 -0500115 auto euh = std::make_unique<ExtendedUserHeader>(dataIface, regEntry, *src);
Matt Spinlerc63e2e82019-12-02 15:50:12 -0600116
Matt Spinlerbd716f02019-10-15 10:54:11 -0500117 _optionalSections.push_back(std::move(src));
Matt Spinlerc63e2e82019-12-02 15:50:12 -0600118 _optionalSections.push_back(std::move(euh));
Matt Spinlerb8323632019-09-20 15:11:04 -0500119
Matt Spinleraa659472019-10-23 09:26:48 -0500120 auto mtms = std::make_unique<FailingMTMS>(dataIface);
121 _optionalSections.push_back(std::move(mtms));
Matt Spinlerbd716f02019-10-15 10:54:11 -0500122
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600123 auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface);
Matt Spinler85f61a62020-06-03 16:28:55 -0500124 addUserDataSection(std::move(ud));
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600125
Matt Spinler9b7e94f2020-03-24 15:44:41 -0500126 // Create a UserData section from AdditionalData.
Matt Spinlerafa857c2019-10-24 13:03:46 -0500127 if (!additionalData.empty())
128 {
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600129 ud = util::makeADUserDataSection(additionalData);
Matt Spinler85f61a62020-06-03 16:28:55 -0500130 addUserDataSection(std::move(ud));
Matt Spinlerafa857c2019-10-24 13:03:46 -0500131 }
132
Matt Spinler56ad2a02020-03-26 14:00:52 -0500133 // 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 Spinler85f61a62020-06-03 16:28:55 -0500139 // 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 Spinler56ad2a02020-03-26 14:00:52 -0500154 continue;
155 }
156
Matt Spinler85f61a62020-06-03 16:28:55 -0500157 addUserDataSection(std::move(ud));
158 }
Matt Spinler56ad2a02020-03-26 14:00:52 -0500159
Jayanth Othayothda9b5832021-11-05 04:19:43 -0500160#ifdef PEL_ENABLE_PHAL
161 auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID);
Jayanth Othayoth3ef7b602021-11-09 06:40:38 -0600162 openpower::pels::phal::createServiceActions(callouts, path, dataIface,
163 plid());
Jayanth Othayothda9b5832021-11-05 04:19:43 -0500164#endif
165
Matt Spinler85f61a62020-06-03 16:28:55 -0500166 // Store in the PEL any important debug data created while
167 // building the PEL sections.
168 if (!debugData.empty())
169 {
170 nlohmann::json data;
171 data["PEL Internal Debug Data"] = debugData;
172 ud = util::makeJSONUserDataSection(data);
173
174 addUserDataSection(std::move(ud));
175
176 // Also put in the journal for debug
177 for (const auto& [name, data] : debugData)
178 {
179 for (const auto& message : data)
180 {
181 std::string entry = name + ": " + message;
182 log<level::INFO>(entry.c_str());
Matt Spinler56ad2a02020-03-26 14:00:52 -0500183 }
184 }
Matt Spinler56ad2a02020-03-26 14:00:52 -0500185 }
186
Matt Spinler97d19b42019-10-29 11:34:03 -0500187 _ph->setSectionCount(2 + _optionalSections.size());
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500188
189 checkRulesAndFix();
Matt Spinlerb8323632019-09-20 15:11:04 -0500190}
Matt Spinlercb6b0592019-07-16 15:58:51 -0500191
Matt Spinler07eefc52019-09-26 11:18:26 -0500192PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0)
Matt Spinlercb6b0592019-07-16 15:58:51 -0500193{
194}
195
Matt Spinler07eefc52019-09-26 11:18:26 -0500196PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -0500197{
Matt Spinler07eefc52019-09-26 11:18:26 -0500198 populateFromRawData(data, obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500199}
200
Matt Spinler07eefc52019-09-26 11:18:26 -0500201void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -0500202{
Matt Spinler07eefc52019-09-26 11:18:26 -0500203 Stream pelData{data};
Matt Spinlercb6b0592019-07-16 15:58:51 -0500204 _ph = std::make_unique<PrivateHeader>(pelData);
205 if (obmcLogID != 0)
206 {
Matt Spinler97d19b42019-10-29 11:34:03 -0500207 _ph->setOBMCLogID(obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500208 }
209
210 _uh = std::make_unique<UserHeader>(pelData);
Matt Spinler131870c2019-09-25 13:29:04 -0500211
212 // Use the section factory to create the rest of the objects
213 for (size_t i = 2; i < _ph->sectionCount(); i++)
214 {
215 auto section = section_factory::create(pelData);
216 _optionalSections.push_back(std::move(section));
217 }
Matt Spinlercb6b0592019-07-16 15:58:51 -0500218}
219
220bool PEL::valid() const
221{
222 bool valid = _ph->valid();
223
224 if (valid)
225 {
226 valid = _uh->valid();
227 }
228
Matt Spinler131870c2019-09-25 13:29:04 -0500229 if (valid)
230 {
231 if (!std::all_of(_optionalSections.begin(), _optionalSections.end(),
232 [](const auto& section) { return section->valid(); }))
233 {
234 valid = false;
235 }
236 }
237
Matt Spinlercb6b0592019-07-16 15:58:51 -0500238 return valid;
239}
240
241void PEL::setCommitTime()
242{
243 auto now = std::chrono::system_clock::now();
Matt Spinler97d19b42019-10-29 11:34:03 -0500244 _ph->setCommitTimestamp(getBCDTime(now));
Matt Spinlercb6b0592019-07-16 15:58:51 -0500245}
246
247void PEL::assignID()
248{
Matt Spinler97d19b42019-10-29 11:34:03 -0500249 _ph->setID(generatePELID());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500250}
251
Matt Spinler06885452019-11-06 10:35:42 -0600252void PEL::flatten(std::vector<uint8_t>& pelBuffer) const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500253{
254 Stream pelData{pelBuffer};
Matt Spinlerb8323632019-09-20 15:11:04 -0500255
Matt Spinler07eefc52019-09-26 11:18:26 -0500256 if (!valid())
Matt Spinlercb6b0592019-07-16 15:58:51 -0500257 {
Matt Spinler07eefc52019-09-26 11:18:26 -0500258 log<level::WARNING>("Unflattening an invalid PEL");
Matt Spinlercb6b0592019-07-16 15:58:51 -0500259 }
260
Matt Spinler07eefc52019-09-26 11:18:26 -0500261 _ph->flatten(pelData);
Matt Spinlerb8323632019-09-20 15:11:04 -0500262 _uh->flatten(pelData);
Matt Spinler07eefc52019-09-26 11:18:26 -0500263
264 for (auto& section : _optionalSections)
265 {
266 section->flatten(pelData);
267 }
Matt Spinlercb6b0592019-07-16 15:58:51 -0500268}
269
Matt Spinler06885452019-11-06 10:35:42 -0600270std::vector<uint8_t> PEL::data() const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500271{
Matt Spinler07eefc52019-09-26 11:18:26 -0500272 std::vector<uint8_t> pelData;
273 flatten(pelData);
274 return pelData;
Matt Spinlercb6b0592019-07-16 15:58:51 -0500275}
276
Matt Spinlerf1b46ff2020-01-22 14:10:04 -0600277size_t PEL::size() const
278{
279 size_t size = 0;
280
281 if (_ph)
282 {
283 size += _ph->header().size;
284 }
285
286 if (_uh)
287 {
288 size += _uh->header().size;
289 }
290
291 for (const auto& section : _optionalSections)
292 {
293 size += section->header().size;
294 }
295
296 return size;
297}
298
Matt Spinlerbd716f02019-10-15 10:54:11 -0500299std::optional<SRC*> PEL::primarySRC() const
300{
301 auto src = std::find_if(
302 _optionalSections.begin(), _optionalSections.end(), [](auto& section) {
303 return section->header().id ==
304 static_cast<uint16_t>(SectionID::primarySRC);
305 });
306 if (src != _optionalSections.end())
307 {
308 return static_cast<SRC*>(src->get());
309 }
310
311 return std::nullopt;
312}
313
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500314void PEL::checkRulesAndFix()
315{
Matt Spinler1f93c592020-09-10 10:43:08 -0500316 // Only fix if the action flags are at their default value which
317 // means they weren't specified in the registry. Otherwise
318 // assume the user knows what they are doing.
319 if (_uh->actionFlags() == actionFlagsDefault)
320 {
321 auto [actionFlags, eventType] =
322 pel_rules::check(0, _uh->eventType(), _uh->severity());
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500323
Matt Spinler1f93c592020-09-10 10:43:08 -0500324 _uh->setActionFlags(actionFlags);
325 _uh->setEventType(eventType);
326 }
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500327}
328
Matt Spinleracb7c102020-01-10 13:49:22 -0600329void PEL::printSectionInJSON(const Section& section, std::string& buf,
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800330 std::map<uint16_t, size_t>& pluralSections,
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800331 message::Registry& registry,
332 const std::vector<std::string>& plugins,
333 uint8_t creatorID) const
Aatir186ce8c2019-10-20 15:13:39 -0500334{
335 char tmpB[5];
Aatir Manzurad0e0472019-10-07 13:18:37 -0500336 uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8),
337 static_cast<uint8_t>(section.header().id)};
338 sprintf(tmpB, "%c%c", id[0], id[1]);
339 std::string sectionID(tmpB);
340 std::string sectionName = pv::sectionTitles.count(sectionID)
341 ? pv::sectionTitles.at(sectionID)
342 : "Unknown Section";
Matt Spinleracb7c102020-01-10 13:49:22 -0600343
344 // Add a count if there are multiple of this type of section
345 auto count = pluralSections.find(section.header().id);
346 if (count != pluralSections.end())
347 {
348 sectionName += " " + std::to_string(count->second);
349 count->second++;
350 }
351
Aatir186ce8c2019-10-20 15:13:39 -0500352 if (section.valid())
353 {
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800354 std::optional<std::string> json;
355 if (sectionID == "PS" || sectionID == "SS")
356 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800357 json = section.getJSON(registry, plugins, creatorID);
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800358 }
Matt Spinler386a61e2020-08-13 15:51:12 -0500359 else if ((sectionID == "UD") || (sectionID == "ED"))
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800360 {
Harisuddin Mohamed Isa3fdcd4e2020-08-26 11:56:42 +0800361 json = section.getJSON(creatorID, plugins);
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800362 }
363 else
364 {
365 json = section.getJSON();
366 }
Matt Spinler4220a152020-03-26 10:18:09 -0500367
368 buf += "\"" + sectionName + "\": {\n";
369
Aatir Manzurad0e0472019-10-07 13:18:37 -0500370 if (json)
371 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800372 buf += *json + "\n},\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500373 }
374 else
375 {
Matt Spinler4220a152020-03-26 10:18:09 -0500376 jsonInsert(buf, pv::sectionVer,
377 getNumberString("%d", section.header().version), 1);
378 jsonInsert(buf, pv::subSection,
379 getNumberString("%d", section.header().subType), 1);
380 jsonInsert(buf, pv::createdBy,
381 getNumberString("0x%X", section.header().componentID),
382 1);
383
Aatir Manzurad0e0472019-10-07 13:18:37 -0500384 std::vector<uint8_t> data;
385 Stream s{data};
386 section.flatten(s);
Matt Spinler4220a152020-03-26 10:18:09 -0500387 std::string dstr =
388 dumpHex(std::data(data) + SectionHeader::flattenedSize(),
Harisuddin Mohamed Isa097ad122020-06-11 21:19:41 +0800389 data.size() - SectionHeader::flattenedSize(), 2);
Matt Spinler4220a152020-03-26 10:18:09 -0500390 std::string jsonIndent(indentLevel, 0x20);
391 buf += jsonIndent + "\"Data\": [\n";
392 buf += dstr;
393 buf += jsonIndent + "]\n";
394 buf += "},\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500395 }
Aatir186ce8c2019-10-20 15:13:39 -0500396 }
397 else
398 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800399 buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n";
Aatir186ce8c2019-10-20 15:13:39 -0500400 }
401}
402
Matt Spinleracb7c102020-01-10 13:49:22 -0600403std::map<uint16_t, size_t> PEL::getPluralSections() const
404{
405 std::map<uint16_t, size_t> sectionCounts;
406
407 for (const auto& section : optionalSections())
408 {
409 if (sectionCounts.find(section->header().id) == sectionCounts.end())
410 {
411 sectionCounts[section->header().id] = 1;
412 }
413 else
414 {
415 sectionCounts[section->header().id]++;
416 }
417 }
418
419 std::map<uint16_t, size_t> sections;
420 for (const auto& [id, count] : sectionCounts)
421 {
422 if (count > 1)
423 {
424 // Start with 0 here and printSectionInJSON()
425 // will increment it as it goes.
426 sections.emplace(id, 0);
427 }
428 }
429
430 return sections;
431}
432
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800433void PEL::toJSON(message::Registry& registry,
434 const std::vector<std::string>& plugins) const
Aatir186ce8c2019-10-20 15:13:39 -0500435{
Matt Spinleracb7c102020-01-10 13:49:22 -0600436 auto sections = getPluralSections();
437
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800438 std::string buf = "{\n";
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800439 printSectionInJSON(*(_ph.get()), buf, sections, registry, plugins);
440 printSectionInJSON(*(_uh.get()), buf, sections, registry, plugins);
Aatir186ce8c2019-10-20 15:13:39 -0500441 for (auto& section : this->optionalSections())
442 {
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800443 printSectionInJSON(*(section.get()), buf, sections, registry, plugins,
444 _ph->creatorID());
Aatir186ce8c2019-10-20 15:13:39 -0500445 }
446 buf += "}";
447 std::size_t found = buf.rfind(",");
448 if (found != std::string::npos)
449 buf.replace(found, 1, "");
450 std::cout << buf << std::endl;
451}
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800452
Matt Spinler85f61a62020-06-03 16:28:55 -0500453bool PEL::addUserDataSection(std::unique_ptr<UserData> userData)
454{
455 if (size() + userData->header().size > _maxPELSize)
456 {
457 if (userData->shrink(_maxPELSize - size()))
458 {
459 _optionalSections.push_back(std::move(userData));
460 }
461 else
462 {
463 log<level::WARNING>(
464 "Could not shrink UserData section. Dropping",
465 entry("SECTION_SIZE=%d\n", userData->header().size),
466 entry("COMPONENT_ID=0x%02X", userData->header().componentID),
467 entry("SUBTYPE=0x%X", userData->header().subType),
468 entry("VERSION=0x%X", userData->header().version));
469 return false;
470 }
471 }
472 else
473 {
474 _optionalSections.push_back(std::move(userData));
475 }
476 return true;
477}
478
Matt Spinler5a90a952020-08-27 09:39:03 -0500479nlohmann::json PEL::getCalloutJSON(const PelFFDC& ffdcFiles)
480{
481 nlohmann::json callouts;
482
483 for (const auto& file : ffdcFiles)
484 {
485 if ((file.format == UserDataFormat::json) &&
486 (file.subType == jsonCalloutSubtype))
487 {
488 auto data = util::readFD(file.fd);
489 if (data.empty())
490 {
491 throw std::runtime_error{
492 "Could not get data from JSON callout file descriptor"};
493 }
494
495 std::string jsonString{data.begin(), data.begin() + data.size()};
496
497 callouts = nlohmann::json::parse(jsonString);
498 break;
499 }
500 }
501
502 return callouts;
503}
504
Andrew Geissler44fc3162020-07-09 09:21:31 -0500505bool PEL::isCalloutPresent() const
506{
507 auto pSRC = primarySRC();
508 if (!pSRC)
509 {
510 return false;
511 }
512
513 bool calloutPresent = false;
514 if ((*pSRC)->callouts())
515 {
516 for (auto& i : (*pSRC)->callouts()->callouts())
517 {
518 if (((*i).fruIdentity()))
519 {
520 auto& fruId = (*i).fruIdentity();
521 if ((*fruId).failingComponentType() != 0)
522 {
523 calloutPresent = true;
524 break;
525 }
526 }
527 }
528 }
529
530 return calloutPresent;
531}
532
Sumit Kumar3160a542021-04-26 08:07:04 -0500533void PEL::updateSysInfoInExtendedUserDataSection(
534 const DataInterfaceBase& dataIface)
535{
536 const AdditionalData additionalData;
537
538 // Check for PEL from Hostboot
539 if (_ph->creatorID() == static_cast<uint8_t>(CreatorID::hostboot))
540 {
541 // Get the ED section from PEL
542 auto op = std::find_if(_optionalSections.begin(),
543 _optionalSections.end(), [](auto& section) {
544 return section->header().id ==
545 static_cast<uint16_t>(
546 SectionID::extUserData);
547 });
548
549 // Check for ED section found and its not the last section of PEL
550 if (op != _optionalSections.end())
551 {
552 // Get the extended user data class mapped to found section
553 auto extUserData = static_cast<ExtendedUserData*>(op->get());
554
555 // Check for the creator ID is for OpenBMC
556 if (extUserData->creatorID() ==
557 static_cast<uint8_t>(CreatorID::openBMC))
558 {
559 // Update subtype and component id
560 auto subType = static_cast<uint8_t>(UserDataFormat::json);
561 auto componentId =
562 static_cast<uint16_t>(ComponentID::phosphorLogging);
563
564 // Update system data to ED section
565 auto ud =
566 util::makeSysInfoUserDataSection(additionalData, dataIface);
567 extUserData->updateDataSection(subType, componentId,
568 ud->data());
569 }
570 }
571 }
572}
573
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600574namespace util
575{
576
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600577std::unique_ptr<UserData> makeJSONUserDataSection(const nlohmann::json& json)
578{
579 auto jsonString = json.dump();
580 std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end());
581
582 // Pad to a 4 byte boundary
583 while ((jsonData.size() % 4) != 0)
584 {
585 jsonData.push_back(0);
586 }
587
588 return std::make_unique<UserData>(
589 static_cast<uint16_t>(ComponentID::phosphorLogging),
590 static_cast<uint8_t>(UserDataFormat::json),
591 static_cast<uint8_t>(UserDataFormatVersion::json), jsonData);
592}
593
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600594std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad)
595{
596 assert(!ad.empty());
597 nlohmann::json json;
598
599 // Remove the 'ESEL' entry, as it contains a full PEL in the value.
600 if (ad.getValue("ESEL"))
601 {
602 auto newAD = ad;
603 newAD.remove("ESEL");
604 json = newAD.toJSON();
605 }
606 else
607 {
608 json = ad.toJSON();
609 }
610
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600611 return makeJSONUserDataSection(json);
612}
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600613
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600614void addProcessNameToJSON(nlohmann::json& json,
615 const std::optional<std::string>& pid,
616 const DataInterfaceBase& dataIface)
617{
Matt Spinler677381b2020-01-23 10:04:29 -0600618 std::string name{unknownValue};
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600619
620 try
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600621 {
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600622 if (pid)
623 {
624 auto n = dataIface.getProcessName(*pid);
625 if (n)
626 {
627 name = *n;
628 }
629 }
630 }
Patrick Williams66491c62021-10-06 12:23:37 -0500631 catch (const std::exception& e)
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600632 {
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600633 }
634
Sumit Kumar3160a542021-04-26 08:07:04 -0500635 if (pid)
636 {
637 json["Process Name"] = std::move(name);
638 }
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600639}
640
Matt Spinler677381b2020-01-23 10:04:29 -0600641void addBMCFWVersionIDToJSON(nlohmann::json& json,
642 const DataInterfaceBase& dataIface)
643{
644 auto id = dataIface.getBMCFWVersionID();
645 if (id.empty())
646 {
647 id = unknownValue;
648 }
649
Matt Spinlerc2b8a512021-05-21 12:44:42 -0600650 json["FW Version ID"] = std::move(id);
Matt Spinler677381b2020-01-23 10:04:29 -0600651}
652
Matt Spinler4aa23a12020-02-03 15:05:09 -0600653std::string lastSegment(char separator, std::string data)
654{
655 auto pos = data.find_last_of(separator);
656 if (pos != std::string::npos)
657 {
658 data = data.substr(pos + 1);
659 }
660
661 return data;
662}
663
Ben Tynere32b7e72021-05-18 12:38:40 -0500664void addIMKeyword(nlohmann::json& json, const DataInterfaceBase& dataIface)
665{
666 auto keyword = dataIface.getSystemIMKeyword();
667
668 std::string value{};
669
670 std::for_each(keyword.begin(), keyword.end(), [&](const auto& byte) {
671 value += fmt::format("{:02X}", byte);
672 });
673
674 json["System IM"] = value;
675}
676
Matt Spinler4aa23a12020-02-03 15:05:09 -0600677void addStatesToJSON(nlohmann::json& json, const DataInterfaceBase& dataIface)
678{
679 json["BMCState"] = lastSegment('.', dataIface.getBMCState());
680 json["ChassisState"] = lastSegment('.', dataIface.getChassisState());
681 json["HostState"] = lastSegment('.', dataIface.getHostState());
Sumit Kumar2c36fdd2021-09-21 03:12:11 -0500682 json["BootState"] = lastSegment('.', dataIface.getBootState());
Matt Spinler4aa23a12020-02-03 15:05:09 -0600683}
684
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600685std::unique_ptr<UserData>
686 makeSysInfoUserDataSection(const AdditionalData& ad,
687 const DataInterfaceBase& dataIface)
688{
689 nlohmann::json json;
690
691 addProcessNameToJSON(json, ad.getValue("_PID"), dataIface);
Matt Spinler677381b2020-01-23 10:04:29 -0600692 addBMCFWVersionIDToJSON(json, dataIface);
Ben Tynere32b7e72021-05-18 12:38:40 -0500693 addIMKeyword(json, dataIface);
Matt Spinler4aa23a12020-02-03 15:05:09 -0600694 addStatesToJSON(json, dataIface);
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600695
696 return makeJSONUserDataSection(json);
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600697}
698
Matt Spinler5b289b22020-03-26 14:27:19 -0500699std::vector<uint8_t> readFD(int fd)
700{
701 std::vector<uint8_t> data;
702
703 // Get the size
704 struct stat s;
705 int r = fstat(fd, &s);
706 if (r != 0)
707 {
708 auto e = errno;
709 log<level::ERR>("Could not get FFDC file size from FD",
710 entry("ERRNO=%d", e));
711 return data;
712 }
713
714 if (0 == s.st_size)
715 {
716 log<level::ERR>("FFDC file is empty");
717 return data;
718 }
719
720 data.resize(s.st_size);
721
722 // Make sure its at the beginning, as maybe another
723 // extension already used it.
724 r = lseek(fd, 0, SEEK_SET);
725 if (r == -1)
726 {
727 auto e = errno;
728 log<level::ERR>("Could not seek to beginning of FFDC file",
729 entry("ERRNO=%d", e));
730 return data;
731 }
732
733 r = read(fd, data.data(), s.st_size);
734 if (r == -1)
735 {
736 auto e = errno;
737 log<level::ERR>("Could not read FFDC file", entry("ERRNO=%d", e));
738 }
739 else if (r != s.st_size)
740 {
741 log<level::WARNING>("Could not read full FFDC file",
742 entry("FILE_SIZE=%d", s.st_size),
743 entry("SIZE_READ=%d", r));
744 }
745
746 return data;
747}
748
Matt Spinler56ad2a02020-03-26 14:00:52 -0500749std::unique_ptr<UserData> makeFFDCuserDataSection(uint16_t componentID,
750 const PelFFDCfile& file)
751{
Matt Spinler5b289b22020-03-26 14:27:19 -0500752 auto data = readFD(file.fd);
753
754 if (data.empty())
755 {
756 return std::unique_ptr<UserData>();
757 }
758
759 // The data needs 4 Byte alignment, and save amount padded for the
760 // CBOR case.
761 uint32_t pad = 0;
762 while (data.size() % 4)
763 {
764 data.push_back(0);
765 pad++;
766 }
767
768 // For JSON, CBOR, and Text use our component ID, subType, and version,
769 // otherwise use the supplied ones.
770 uint16_t compID = static_cast<uint16_t>(ComponentID::phosphorLogging);
771 uint8_t subType{};
772 uint8_t version{};
773
774 switch (file.format)
775 {
776 case UserDataFormat::json:
777 subType = static_cast<uint8_t>(UserDataFormat::json);
778 version = static_cast<uint8_t>(UserDataFormatVersion::json);
779 break;
780 case UserDataFormat::cbor:
781 subType = static_cast<uint8_t>(UserDataFormat::cbor);
782 version = static_cast<uint8_t>(UserDataFormatVersion::cbor);
783
784 // The CBOR parser will fail on the extra pad bytes since they
785 // aren't CBOR. Add the amount we padded to the end and other
786 // code will remove it all before parsing.
787 {
788 data.resize(data.size() + 4);
789 Stream stream{data};
790 stream.offset(data.size() - 4);
791 stream << pad;
792 }
793
794 break;
795 case UserDataFormat::text:
796 subType = static_cast<uint8_t>(UserDataFormat::text);
797 version = static_cast<uint8_t>(UserDataFormatVersion::text);
798 break;
799 case UserDataFormat::custom:
800 default:
801 // Use the passed in values
802 compID = componentID;
803 subType = file.subType;
804 version = file.version;
805 break;
806 }
807
808 return std::make_unique<UserData>(compID, subType, version, data);
Matt Spinler56ad2a02020-03-26 14:00:52 -0500809}
810
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600811} // namespace util
812
Matt Spinlercb6b0592019-07-16 15:58:51 -0500813} // namespace pels
814} // namespace openpower