blob: 30fbf51065e8d1bf77819e44b3292555de934820 [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);
162 openpower::pels::phal::createServiceActions(callouts, path, dataIface);
163#endif
164
Matt Spinler85f61a62020-06-03 16:28:55 -0500165 // 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 Spinler56ad2a02020-03-26 14:00:52 -0500182 }
183 }
Matt Spinler56ad2a02020-03-26 14:00:52 -0500184 }
185
Matt Spinler97d19b42019-10-29 11:34:03 -0500186 _ph->setSectionCount(2 + _optionalSections.size());
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500187
188 checkRulesAndFix();
Matt Spinlerb8323632019-09-20 15:11:04 -0500189}
Matt Spinlercb6b0592019-07-16 15:58:51 -0500190
Matt Spinler07eefc52019-09-26 11:18:26 -0500191PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0)
Matt Spinlercb6b0592019-07-16 15:58:51 -0500192{
193}
194
Matt Spinler07eefc52019-09-26 11:18:26 -0500195PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -0500196{
Matt Spinler07eefc52019-09-26 11:18:26 -0500197 populateFromRawData(data, obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500198}
199
Matt Spinler07eefc52019-09-26 11:18:26 -0500200void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -0500201{
Matt Spinler07eefc52019-09-26 11:18:26 -0500202 Stream pelData{data};
Matt Spinlercb6b0592019-07-16 15:58:51 -0500203 _ph = std::make_unique<PrivateHeader>(pelData);
204 if (obmcLogID != 0)
205 {
Matt Spinler97d19b42019-10-29 11:34:03 -0500206 _ph->setOBMCLogID(obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500207 }
208
209 _uh = std::make_unique<UserHeader>(pelData);
Matt Spinler131870c2019-09-25 13:29:04 -0500210
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 Spinlercb6b0592019-07-16 15:58:51 -0500217}
218
219bool PEL::valid() const
220{
221 bool valid = _ph->valid();
222
223 if (valid)
224 {
225 valid = _uh->valid();
226 }
227
Matt Spinler131870c2019-09-25 13:29:04 -0500228 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 Spinlercb6b0592019-07-16 15:58:51 -0500237 return valid;
238}
239
240void PEL::setCommitTime()
241{
242 auto now = std::chrono::system_clock::now();
Matt Spinler97d19b42019-10-29 11:34:03 -0500243 _ph->setCommitTimestamp(getBCDTime(now));
Matt Spinlercb6b0592019-07-16 15:58:51 -0500244}
245
246void PEL::assignID()
247{
Matt Spinler97d19b42019-10-29 11:34:03 -0500248 _ph->setID(generatePELID());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500249}
250
Matt Spinler06885452019-11-06 10:35:42 -0600251void PEL::flatten(std::vector<uint8_t>& pelBuffer) const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500252{
253 Stream pelData{pelBuffer};
Matt Spinlerb8323632019-09-20 15:11:04 -0500254
Matt Spinler07eefc52019-09-26 11:18:26 -0500255 if (!valid())
Matt Spinlercb6b0592019-07-16 15:58:51 -0500256 {
Matt Spinler07eefc52019-09-26 11:18:26 -0500257 log<level::WARNING>("Unflattening an invalid PEL");
Matt Spinlercb6b0592019-07-16 15:58:51 -0500258 }
259
Matt Spinler07eefc52019-09-26 11:18:26 -0500260 _ph->flatten(pelData);
Matt Spinlerb8323632019-09-20 15:11:04 -0500261 _uh->flatten(pelData);
Matt Spinler07eefc52019-09-26 11:18:26 -0500262
263 for (auto& section : _optionalSections)
264 {
265 section->flatten(pelData);
266 }
Matt Spinlercb6b0592019-07-16 15:58:51 -0500267}
268
Matt Spinler06885452019-11-06 10:35:42 -0600269std::vector<uint8_t> PEL::data() const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500270{
Matt Spinler07eefc52019-09-26 11:18:26 -0500271 std::vector<uint8_t> pelData;
272 flatten(pelData);
273 return pelData;
Matt Spinlercb6b0592019-07-16 15:58:51 -0500274}
275
Matt Spinlerf1b46ff2020-01-22 14:10:04 -0600276size_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 Spinlerbd716f02019-10-15 10:54:11 -0500298std::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 Spinlerf1e85e22019-11-01 11:31:31 -0500313void PEL::checkRulesAndFix()
314{
Matt Spinler1f93c592020-09-10 10:43:08 -0500315 // 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 Spinlerf1e85e22019-11-01 11:31:31 -0500322
Matt Spinler1f93c592020-09-10 10:43:08 -0500323 _uh->setActionFlags(actionFlags);
324 _uh->setEventType(eventType);
325 }
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500326}
327
Matt Spinleracb7c102020-01-10 13:49:22 -0600328void PEL::printSectionInJSON(const Section& section, std::string& buf,
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800329 std::map<uint16_t, size_t>& pluralSections,
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800330 message::Registry& registry,
331 const std::vector<std::string>& plugins,
332 uint8_t creatorID) const
Aatir186ce8c2019-10-20 15:13:39 -0500333{
334 char tmpB[5];
Aatir Manzurad0e0472019-10-07 13:18:37 -0500335 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 Spinleracb7c102020-01-10 13:49:22 -0600342
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
Aatir186ce8c2019-10-20 15:13:39 -0500351 if (section.valid())
352 {
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800353 std::optional<std::string> json;
354 if (sectionID == "PS" || sectionID == "SS")
355 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800356 json = section.getJSON(registry, plugins, creatorID);
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800357 }
Matt Spinler386a61e2020-08-13 15:51:12 -0500358 else if ((sectionID == "UD") || (sectionID == "ED"))
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800359 {
Harisuddin Mohamed Isa3fdcd4e2020-08-26 11:56:42 +0800360 json = section.getJSON(creatorID, plugins);
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800361 }
362 else
363 {
364 json = section.getJSON();
365 }
Matt Spinler4220a152020-03-26 10:18:09 -0500366
367 buf += "\"" + sectionName + "\": {\n";
368
Aatir Manzurad0e0472019-10-07 13:18:37 -0500369 if (json)
370 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800371 buf += *json + "\n},\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500372 }
373 else
374 {
Matt Spinler4220a152020-03-26 10:18:09 -0500375 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 Manzurad0e0472019-10-07 13:18:37 -0500383 std::vector<uint8_t> data;
384 Stream s{data};
385 section.flatten(s);
Matt Spinler4220a152020-03-26 10:18:09 -0500386 std::string dstr =
387 dumpHex(std::data(data) + SectionHeader::flattenedSize(),
Harisuddin Mohamed Isa097ad122020-06-11 21:19:41 +0800388 data.size() - SectionHeader::flattenedSize(), 2);
Matt Spinler4220a152020-03-26 10:18:09 -0500389 std::string jsonIndent(indentLevel, 0x20);
390 buf += jsonIndent + "\"Data\": [\n";
391 buf += dstr;
392 buf += jsonIndent + "]\n";
393 buf += "},\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500394 }
Aatir186ce8c2019-10-20 15:13:39 -0500395 }
396 else
397 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800398 buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n";
Aatir186ce8c2019-10-20 15:13:39 -0500399 }
400}
401
Matt Spinleracb7c102020-01-10 13:49:22 -0600402std::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 Isaf67bafd2020-07-06 17:51:21 +0800432void PEL::toJSON(message::Registry& registry,
433 const std::vector<std::string>& plugins) const
Aatir186ce8c2019-10-20 15:13:39 -0500434{
Matt Spinleracb7c102020-01-10 13:49:22 -0600435 auto sections = getPluralSections();
436
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800437 std::string buf = "{\n";
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800438 printSectionInJSON(*(_ph.get()), buf, sections, registry, plugins);
439 printSectionInJSON(*(_uh.get()), buf, sections, registry, plugins);
Aatir186ce8c2019-10-20 15:13:39 -0500440 for (auto& section : this->optionalSections())
441 {
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800442 printSectionInJSON(*(section.get()), buf, sections, registry, plugins,
443 _ph->creatorID());
Aatir186ce8c2019-10-20 15:13:39 -0500444 }
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 Isa600d15a2019-12-20 12:42:26 +0800451
Matt Spinler85f61a62020-06-03 16:28:55 -0500452bool 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 Spinler5a90a952020-08-27 09:39:03 -0500478nlohmann::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 Geissler44fc3162020-07-09 09:21:31 -0500504bool 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 Kumar3160a542021-04-26 08:07:04 -0500532void 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 Spinlerc7c3e402020-01-22 15:07:25 -0600573namespace util
574{
575
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600576std::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 Spinlerc7c3e402020-01-22 15:07:25 -0600593std::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 Spinler4dcd3f42020-01-22 14:55:07 -0600610 return makeJSONUserDataSection(json);
611}
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600612
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600613void addProcessNameToJSON(nlohmann::json& json,
614 const std::optional<std::string>& pid,
615 const DataInterfaceBase& dataIface)
616{
Matt Spinler677381b2020-01-23 10:04:29 -0600617 std::string name{unknownValue};
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600618
619 try
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600620 {
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600621 if (pid)
622 {
623 auto n = dataIface.getProcessName(*pid);
624 if (n)
625 {
626 name = *n;
627 }
628 }
629 }
Patrick Williams66491c62021-10-06 12:23:37 -0500630 catch (const std::exception& e)
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600631 {
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600632 }
633
Sumit Kumar3160a542021-04-26 08:07:04 -0500634 if (pid)
635 {
636 json["Process Name"] = std::move(name);
637 }
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600638}
639
Matt Spinler677381b2020-01-23 10:04:29 -0600640void 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 Spinlerc2b8a512021-05-21 12:44:42 -0600649 json["FW Version ID"] = std::move(id);
Matt Spinler677381b2020-01-23 10:04:29 -0600650}
651
Matt Spinler4aa23a12020-02-03 15:05:09 -0600652std::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 Tynere32b7e72021-05-18 12:38:40 -0500663void 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 Spinler4aa23a12020-02-03 15:05:09 -0600676void 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 Kumar2c36fdd2021-09-21 03:12:11 -0500681 json["BootState"] = lastSegment('.', dataIface.getBootState());
Matt Spinler4aa23a12020-02-03 15:05:09 -0600682}
683
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600684std::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 Spinler677381b2020-01-23 10:04:29 -0600691 addBMCFWVersionIDToJSON(json, dataIface);
Ben Tynere32b7e72021-05-18 12:38:40 -0500692 addIMKeyword(json, dataIface);
Matt Spinler4aa23a12020-02-03 15:05:09 -0600693 addStatesToJSON(json, dataIface);
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600694
695 return makeJSONUserDataSection(json);
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600696}
697
Matt Spinler5b289b22020-03-26 14:27:19 -0500698std::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 Spinler56ad2a02020-03-26 14:00:52 -0500748std::unique_ptr<UserData> makeFFDCuserDataSection(uint16_t componentID,
749 const PelFFDCfile& file)
750{
Matt Spinler5b289b22020-03-26 14:27:19 -0500751 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 Spinler56ad2a02020-03-26 14:00:52 -0500808}
809
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600810} // namespace util
811
Matt Spinlercb6b0592019-07-16 15:58:51 -0500812} // namespace pels
813} // namespace openpower