blob: 85ab74151db3839bbe1ebf2845053f7e3d590a0b [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 */
Matt Spinlercb6b0592019-07-16 15:58:51 -050016#include "pel.hpp"
17
18#include "bcd_time.hpp"
Matt Spinlerc63e2e82019-12-02 15:50:12 -060019#include "extended_user_header.hpp"
Matt Spinleraa659472019-10-23 09:26:48 -050020#include "failing_mtms.hpp"
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080021#include "json_utils.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -050022#include "log_id.hpp"
Matt Spinlerf1e85e22019-11-01 11:31:31 -050023#include "pel_rules.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050024#include "pel_values.hpp"
Matt Spinler131870c2019-09-25 13:29:04 -050025#include "section_factory.hpp"
Matt Spinlerbd716f02019-10-15 10:54:11 -050026#include "src.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -050027#include "stream.hpp"
Matt Spinlerafa857c2019-10-24 13:03:46 -050028#include "user_data_formats.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -050029
Matt Spinler5b289b22020-03-26 14:27:19 -050030#include <sys/stat.h>
31#include <unistd.h>
32
Aatir186ce8c2019-10-20 15:13:39 -050033#include <iostream>
Matt Spinler07eefc52019-09-26 11:18:26 -050034#include <phosphor-logging/log.hpp>
35
Matt Spinlercb6b0592019-07-16 15:58:51 -050036namespace openpower
37{
38namespace pels
39{
Matt Spinlerb8323632019-09-20 15:11:04 -050040namespace message = openpower::pels::message;
Aatir186ce8c2019-10-20 15:13:39 -050041namespace pv = openpower::pels::pel_values;
Matt Spinler4bfc9082020-03-24 15:05:54 -050042using namespace phosphor::logging;
Matt Spinlerb8323632019-09-20 15:11:04 -050043
Matt Spinler677381b2020-01-23 10:04:29 -060044constexpr auto unknownValue = "Unknown";
45
Matt Spinler4bfc9082020-03-24 15:05:54 -050046PEL::PEL(const message::Entry& regEntry, uint32_t obmcLogID, uint64_t timestamp,
Matt Spinlerbd716f02019-10-15 10:54:11 -050047 phosphor::logging::Entry::Level severity,
Matt Spinler56ad2a02020-03-26 14:00:52 -050048 const AdditionalData& additionalData, const PelFFDC& ffdcFiles,
Matt Spinleraa659472019-10-23 09:26:48 -050049 const DataInterfaceBase& dataIface)
Matt Spinlerb8323632019-09-20 15:11:04 -050050{
Matt Spinler85f61a62020-06-03 16:28:55 -050051 std::map<std::string, std::vector<std::string>> debugData;
52
Matt Spinler4bfc9082020-03-24 15:05:54 -050053 _ph = std::make_unique<PrivateHeader>(regEntry.componentID, obmcLogID,
Matt Spinlerb8323632019-09-20 15:11:04 -050054 timestamp);
Matt Spinleraadccc82020-04-10 14:33:42 -050055 _uh = std::make_unique<UserHeader>(regEntry, severity, dataIface);
Matt Spinlerb8323632019-09-20 15:11:04 -050056
Matt Spinler4bfc9082020-03-24 15:05:54 -050057 auto src = std::make_unique<SRC>(regEntry, additionalData, dataIface);
Matt Spinler85f61a62020-06-03 16:28:55 -050058 if (!src->getDebugData().empty())
59 {
60 // Something didn't go as planned
61 debugData.emplace("SRC", src->getDebugData());
62 }
Matt Spinlerc63e2e82019-12-02 15:50:12 -060063
Matt Spinler4bfc9082020-03-24 15:05:54 -050064 auto euh = std::make_unique<ExtendedUserHeader>(dataIface, regEntry, *src);
Matt Spinlerc63e2e82019-12-02 15:50:12 -060065
Matt Spinlerbd716f02019-10-15 10:54:11 -050066 _optionalSections.push_back(std::move(src));
Matt Spinlerc63e2e82019-12-02 15:50:12 -060067 _optionalSections.push_back(std::move(euh));
Matt Spinlerb8323632019-09-20 15:11:04 -050068
Matt Spinleraa659472019-10-23 09:26:48 -050069 auto mtms = std::make_unique<FailingMTMS>(dataIface);
70 _optionalSections.push_back(std::move(mtms));
Matt Spinlerbd716f02019-10-15 10:54:11 -050071
Matt Spinler4dcd3f42020-01-22 14:55:07 -060072 auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface);
Matt Spinler85f61a62020-06-03 16:28:55 -050073 addUserDataSection(std::move(ud));
Matt Spinler4dcd3f42020-01-22 14:55:07 -060074
Matt Spinler9b7e94f2020-03-24 15:44:41 -050075 // Create a UserData section from AdditionalData.
Matt Spinlerafa857c2019-10-24 13:03:46 -050076 if (!additionalData.empty())
77 {
Matt Spinler4dcd3f42020-01-22 14:55:07 -060078 ud = util::makeADUserDataSection(additionalData);
Matt Spinler85f61a62020-06-03 16:28:55 -050079 addUserDataSection(std::move(ud));
Matt Spinlerafa857c2019-10-24 13:03:46 -050080 }
81
Matt Spinler56ad2a02020-03-26 14:00:52 -050082 // Add any FFDC files into UserData sections
83 for (const auto& file : ffdcFiles)
84 {
85 ud = util::makeFFDCuserDataSection(regEntry.componentID, file);
86 if (!ud)
87 {
Matt Spinler85f61a62020-06-03 16:28:55 -050088 // Add this error into the debug data UserData section
89 std::ostringstream msg;
90 msg << "Could not make PEL FFDC UserData section from file"
91 << std::hex << regEntry.componentID << " " << file.subType
92 << " " << file.version;
93 if (debugData.count("FFDC File"))
94 {
95 debugData.at("FFDC File").push_back(msg.str());
96 }
97 else
98 {
99 debugData.emplace("FFDC File",
100 std::vector<std::string>{msg.str()});
101 }
102
Matt Spinler56ad2a02020-03-26 14:00:52 -0500103 continue;
104 }
105
Matt Spinler85f61a62020-06-03 16:28:55 -0500106 addUserDataSection(std::move(ud));
107 }
Matt Spinler56ad2a02020-03-26 14:00:52 -0500108
Matt Spinler85f61a62020-06-03 16:28:55 -0500109 // Store in the PEL any important debug data created while
110 // building the PEL sections.
111 if (!debugData.empty())
112 {
113 nlohmann::json data;
114 data["PEL Internal Debug Data"] = debugData;
115 ud = util::makeJSONUserDataSection(data);
116
117 addUserDataSection(std::move(ud));
118
119 // Also put in the journal for debug
120 for (const auto& [name, data] : debugData)
121 {
122 for (const auto& message : data)
123 {
124 std::string entry = name + ": " + message;
125 log<level::INFO>(entry.c_str());
Matt Spinler56ad2a02020-03-26 14:00:52 -0500126 }
127 }
Matt Spinler56ad2a02020-03-26 14:00:52 -0500128 }
129
Matt Spinler97d19b42019-10-29 11:34:03 -0500130 _ph->setSectionCount(2 + _optionalSections.size());
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500131
132 checkRulesAndFix();
Matt Spinlerb8323632019-09-20 15:11:04 -0500133}
Matt Spinlercb6b0592019-07-16 15:58:51 -0500134
Matt Spinler07eefc52019-09-26 11:18:26 -0500135PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0)
Matt Spinlercb6b0592019-07-16 15:58:51 -0500136{
137}
138
Matt Spinler07eefc52019-09-26 11:18:26 -0500139PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -0500140{
Matt Spinler07eefc52019-09-26 11:18:26 -0500141 populateFromRawData(data, obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500142}
143
Matt Spinler07eefc52019-09-26 11:18:26 -0500144void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -0500145{
Matt Spinler07eefc52019-09-26 11:18:26 -0500146 Stream pelData{data};
Matt Spinlercb6b0592019-07-16 15:58:51 -0500147 _ph = std::make_unique<PrivateHeader>(pelData);
148 if (obmcLogID != 0)
149 {
Matt Spinler97d19b42019-10-29 11:34:03 -0500150 _ph->setOBMCLogID(obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500151 }
152
153 _uh = std::make_unique<UserHeader>(pelData);
Matt Spinler131870c2019-09-25 13:29:04 -0500154
155 // Use the section factory to create the rest of the objects
156 for (size_t i = 2; i < _ph->sectionCount(); i++)
157 {
158 auto section = section_factory::create(pelData);
159 _optionalSections.push_back(std::move(section));
160 }
Matt Spinlercb6b0592019-07-16 15:58:51 -0500161}
162
163bool PEL::valid() const
164{
165 bool valid = _ph->valid();
166
167 if (valid)
168 {
169 valid = _uh->valid();
170 }
171
Matt Spinler131870c2019-09-25 13:29:04 -0500172 if (valid)
173 {
174 if (!std::all_of(_optionalSections.begin(), _optionalSections.end(),
175 [](const auto& section) { return section->valid(); }))
176 {
177 valid = false;
178 }
179 }
180
Matt Spinlercb6b0592019-07-16 15:58:51 -0500181 return valid;
182}
183
184void PEL::setCommitTime()
185{
186 auto now = std::chrono::system_clock::now();
Matt Spinler97d19b42019-10-29 11:34:03 -0500187 _ph->setCommitTimestamp(getBCDTime(now));
Matt Spinlercb6b0592019-07-16 15:58:51 -0500188}
189
190void PEL::assignID()
191{
Matt Spinler97d19b42019-10-29 11:34:03 -0500192 _ph->setID(generatePELID());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500193}
194
Matt Spinler06885452019-11-06 10:35:42 -0600195void PEL::flatten(std::vector<uint8_t>& pelBuffer) const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500196{
197 Stream pelData{pelBuffer};
Matt Spinlerb8323632019-09-20 15:11:04 -0500198
Matt Spinler07eefc52019-09-26 11:18:26 -0500199 if (!valid())
Matt Spinlercb6b0592019-07-16 15:58:51 -0500200 {
Matt Spinler07eefc52019-09-26 11:18:26 -0500201 log<level::WARNING>("Unflattening an invalid PEL");
Matt Spinlercb6b0592019-07-16 15:58:51 -0500202 }
203
Matt Spinler07eefc52019-09-26 11:18:26 -0500204 _ph->flatten(pelData);
Matt Spinlerb8323632019-09-20 15:11:04 -0500205 _uh->flatten(pelData);
Matt Spinler07eefc52019-09-26 11:18:26 -0500206
207 for (auto& section : _optionalSections)
208 {
209 section->flatten(pelData);
210 }
Matt Spinlercb6b0592019-07-16 15:58:51 -0500211}
212
Matt Spinler06885452019-11-06 10:35:42 -0600213std::vector<uint8_t> PEL::data() const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500214{
Matt Spinler07eefc52019-09-26 11:18:26 -0500215 std::vector<uint8_t> pelData;
216 flatten(pelData);
217 return pelData;
Matt Spinlercb6b0592019-07-16 15:58:51 -0500218}
219
Matt Spinlerf1b46ff2020-01-22 14:10:04 -0600220size_t PEL::size() const
221{
222 size_t size = 0;
223
224 if (_ph)
225 {
226 size += _ph->header().size;
227 }
228
229 if (_uh)
230 {
231 size += _uh->header().size;
232 }
233
234 for (const auto& section : _optionalSections)
235 {
236 size += section->header().size;
237 }
238
239 return size;
240}
241
Matt Spinlerbd716f02019-10-15 10:54:11 -0500242std::optional<SRC*> PEL::primarySRC() const
243{
244 auto src = std::find_if(
245 _optionalSections.begin(), _optionalSections.end(), [](auto& section) {
246 return section->header().id ==
247 static_cast<uint16_t>(SectionID::primarySRC);
248 });
249 if (src != _optionalSections.end())
250 {
251 return static_cast<SRC*>(src->get());
252 }
253
254 return std::nullopt;
255}
256
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500257void PEL::checkRulesAndFix()
258{
259 auto [actionFlags, eventType] =
260 pel_rules::check(_uh->actionFlags(), _uh->eventType(), _uh->severity());
261
262 _uh->setActionFlags(actionFlags);
263 _uh->setEventType(eventType);
264}
265
Matt Spinleracb7c102020-01-10 13:49:22 -0600266void PEL::printSectionInJSON(const Section& section, std::string& buf,
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800267 std::map<uint16_t, size_t>& pluralSections,
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800268 message::Registry& registry,
269 const std::vector<std::string>& plugins,
270 uint8_t creatorID) const
Aatir186ce8c2019-10-20 15:13:39 -0500271{
272 char tmpB[5];
Aatir Manzurad0e0472019-10-07 13:18:37 -0500273 uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8),
274 static_cast<uint8_t>(section.header().id)};
275 sprintf(tmpB, "%c%c", id[0], id[1]);
276 std::string sectionID(tmpB);
277 std::string sectionName = pv::sectionTitles.count(sectionID)
278 ? pv::sectionTitles.at(sectionID)
279 : "Unknown Section";
Matt Spinleracb7c102020-01-10 13:49:22 -0600280
281 // Add a count if there are multiple of this type of section
282 auto count = pluralSections.find(section.header().id);
283 if (count != pluralSections.end())
284 {
285 sectionName += " " + std::to_string(count->second);
286 count->second++;
287 }
288
Aatir186ce8c2019-10-20 15:13:39 -0500289 if (section.valid())
290 {
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800291 std::optional<std::string> json;
292 if (sectionID == "PS" || sectionID == "SS")
293 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800294 json = section.getJSON(registry, plugins, creatorID);
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800295 }
296 else if (sectionID == "UD")
297 {
Harisuddin Mohamed Isa3fdcd4e2020-08-26 11:56:42 +0800298 json = section.getJSON(creatorID, plugins);
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800299 }
300 else
301 {
302 json = section.getJSON();
303 }
Matt Spinler4220a152020-03-26 10:18:09 -0500304
305 buf += "\"" + sectionName + "\": {\n";
306
Aatir Manzurad0e0472019-10-07 13:18:37 -0500307 if (json)
308 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800309 buf += *json + "\n},\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500310 }
311 else
312 {
Matt Spinler4220a152020-03-26 10:18:09 -0500313 jsonInsert(buf, pv::sectionVer,
314 getNumberString("%d", section.header().version), 1);
315 jsonInsert(buf, pv::subSection,
316 getNumberString("%d", section.header().subType), 1);
317 jsonInsert(buf, pv::createdBy,
318 getNumberString("0x%X", section.header().componentID),
319 1);
320
Aatir Manzurad0e0472019-10-07 13:18:37 -0500321 std::vector<uint8_t> data;
322 Stream s{data};
323 section.flatten(s);
Matt Spinler4220a152020-03-26 10:18:09 -0500324 std::string dstr =
325 dumpHex(std::data(data) + SectionHeader::flattenedSize(),
Harisuddin Mohamed Isa097ad122020-06-11 21:19:41 +0800326 data.size() - SectionHeader::flattenedSize(), 2);
Matt Spinler4220a152020-03-26 10:18:09 -0500327 std::string jsonIndent(indentLevel, 0x20);
328 buf += jsonIndent + "\"Data\": [\n";
329 buf += dstr;
330 buf += jsonIndent + "]\n";
331 buf += "},\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500332 }
Aatir186ce8c2019-10-20 15:13:39 -0500333 }
334 else
335 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800336 buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n";
Aatir186ce8c2019-10-20 15:13:39 -0500337 }
338}
339
Matt Spinleracb7c102020-01-10 13:49:22 -0600340std::map<uint16_t, size_t> PEL::getPluralSections() const
341{
342 std::map<uint16_t, size_t> sectionCounts;
343
344 for (const auto& section : optionalSections())
345 {
346 if (sectionCounts.find(section->header().id) == sectionCounts.end())
347 {
348 sectionCounts[section->header().id] = 1;
349 }
350 else
351 {
352 sectionCounts[section->header().id]++;
353 }
354 }
355
356 std::map<uint16_t, size_t> sections;
357 for (const auto& [id, count] : sectionCounts)
358 {
359 if (count > 1)
360 {
361 // Start with 0 here and printSectionInJSON()
362 // will increment it as it goes.
363 sections.emplace(id, 0);
364 }
365 }
366
367 return sections;
368}
369
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800370void PEL::toJSON(message::Registry& registry,
371 const std::vector<std::string>& plugins) const
Aatir186ce8c2019-10-20 15:13:39 -0500372{
Matt Spinleracb7c102020-01-10 13:49:22 -0600373 auto sections = getPluralSections();
374
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800375 std::string buf = "{\n";
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800376 printSectionInJSON(*(_ph.get()), buf, sections, registry, plugins);
377 printSectionInJSON(*(_uh.get()), buf, sections, registry, plugins);
Aatir186ce8c2019-10-20 15:13:39 -0500378 for (auto& section : this->optionalSections())
379 {
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800380 printSectionInJSON(*(section.get()), buf, sections, registry, plugins,
381 _ph->creatorID());
Aatir186ce8c2019-10-20 15:13:39 -0500382 }
383 buf += "}";
384 std::size_t found = buf.rfind(",");
385 if (found != std::string::npos)
386 buf.replace(found, 1, "");
387 std::cout << buf << std::endl;
388}
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800389
Matt Spinler85f61a62020-06-03 16:28:55 -0500390bool PEL::addUserDataSection(std::unique_ptr<UserData> userData)
391{
392 if (size() + userData->header().size > _maxPELSize)
393 {
394 if (userData->shrink(_maxPELSize - size()))
395 {
396 _optionalSections.push_back(std::move(userData));
397 }
398 else
399 {
400 log<level::WARNING>(
401 "Could not shrink UserData section. Dropping",
402 entry("SECTION_SIZE=%d\n", userData->header().size),
403 entry("COMPONENT_ID=0x%02X", userData->header().componentID),
404 entry("SUBTYPE=0x%X", userData->header().subType),
405 entry("VERSION=0x%X", userData->header().version));
406 return false;
407 }
408 }
409 else
410 {
411 _optionalSections.push_back(std::move(userData));
412 }
413 return true;
414}
415
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600416namespace util
417{
418
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600419std::unique_ptr<UserData> makeJSONUserDataSection(const nlohmann::json& json)
420{
421 auto jsonString = json.dump();
422 std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end());
423
424 // Pad to a 4 byte boundary
425 while ((jsonData.size() % 4) != 0)
426 {
427 jsonData.push_back(0);
428 }
429
430 return std::make_unique<UserData>(
431 static_cast<uint16_t>(ComponentID::phosphorLogging),
432 static_cast<uint8_t>(UserDataFormat::json),
433 static_cast<uint8_t>(UserDataFormatVersion::json), jsonData);
434}
435
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600436std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad)
437{
438 assert(!ad.empty());
439 nlohmann::json json;
440
441 // Remove the 'ESEL' entry, as it contains a full PEL in the value.
442 if (ad.getValue("ESEL"))
443 {
444 auto newAD = ad;
445 newAD.remove("ESEL");
446 json = newAD.toJSON();
447 }
448 else
449 {
450 json = ad.toJSON();
451 }
452
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600453 return makeJSONUserDataSection(json);
454}
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600455
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600456void addProcessNameToJSON(nlohmann::json& json,
457 const std::optional<std::string>& pid,
458 const DataInterfaceBase& dataIface)
459{
Matt Spinler677381b2020-01-23 10:04:29 -0600460 std::string name{unknownValue};
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600461
462 try
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600463 {
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600464 if (pid)
465 {
466 auto n = dataIface.getProcessName(*pid);
467 if (n)
468 {
469 name = *n;
470 }
471 }
472 }
473 catch (std::exception& e)
474 {
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600475 }
476
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600477 json["Process Name"] = std::move(name);
478}
479
Matt Spinler677381b2020-01-23 10:04:29 -0600480void addBMCFWVersionIDToJSON(nlohmann::json& json,
481 const DataInterfaceBase& dataIface)
482{
483 auto id = dataIface.getBMCFWVersionID();
484 if (id.empty())
485 {
486 id = unknownValue;
487 }
488
489 json["BMC Version ID"] = std::move(id);
490}
491
Matt Spinler4aa23a12020-02-03 15:05:09 -0600492std::string lastSegment(char separator, std::string data)
493{
494 auto pos = data.find_last_of(separator);
495 if (pos != std::string::npos)
496 {
497 data = data.substr(pos + 1);
498 }
499
500 return data;
501}
502
503void addStatesToJSON(nlohmann::json& json, const DataInterfaceBase& dataIface)
504{
505 json["BMCState"] = lastSegment('.', dataIface.getBMCState());
506 json["ChassisState"] = lastSegment('.', dataIface.getChassisState());
507 json["HostState"] = lastSegment('.', dataIface.getHostState());
508}
509
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600510std::unique_ptr<UserData>
511 makeSysInfoUserDataSection(const AdditionalData& ad,
512 const DataInterfaceBase& dataIface)
513{
514 nlohmann::json json;
515
516 addProcessNameToJSON(json, ad.getValue("_PID"), dataIface);
Matt Spinler677381b2020-01-23 10:04:29 -0600517 addBMCFWVersionIDToJSON(json, dataIface);
Matt Spinler4aa23a12020-02-03 15:05:09 -0600518 addStatesToJSON(json, dataIface);
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600519
520 return makeJSONUserDataSection(json);
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600521}
522
Matt Spinler5b289b22020-03-26 14:27:19 -0500523std::vector<uint8_t> readFD(int fd)
524{
525 std::vector<uint8_t> data;
526
527 // Get the size
528 struct stat s;
529 int r = fstat(fd, &s);
530 if (r != 0)
531 {
532 auto e = errno;
533 log<level::ERR>("Could not get FFDC file size from FD",
534 entry("ERRNO=%d", e));
535 return data;
536 }
537
538 if (0 == s.st_size)
539 {
540 log<level::ERR>("FFDC file is empty");
541 return data;
542 }
543
544 data.resize(s.st_size);
545
546 // Make sure its at the beginning, as maybe another
547 // extension already used it.
548 r = lseek(fd, 0, SEEK_SET);
549 if (r == -1)
550 {
551 auto e = errno;
552 log<level::ERR>("Could not seek to beginning of FFDC file",
553 entry("ERRNO=%d", e));
554 return data;
555 }
556
557 r = read(fd, data.data(), s.st_size);
558 if (r == -1)
559 {
560 auto e = errno;
561 log<level::ERR>("Could not read FFDC file", entry("ERRNO=%d", e));
562 }
563 else if (r != s.st_size)
564 {
565 log<level::WARNING>("Could not read full FFDC file",
566 entry("FILE_SIZE=%d", s.st_size),
567 entry("SIZE_READ=%d", r));
568 }
569
570 return data;
571}
572
Matt Spinler56ad2a02020-03-26 14:00:52 -0500573std::unique_ptr<UserData> makeFFDCuserDataSection(uint16_t componentID,
574 const PelFFDCfile& file)
575{
Matt Spinler5b289b22020-03-26 14:27:19 -0500576 auto data = readFD(file.fd);
577
578 if (data.empty())
579 {
580 return std::unique_ptr<UserData>();
581 }
582
583 // The data needs 4 Byte alignment, and save amount padded for the
584 // CBOR case.
585 uint32_t pad = 0;
586 while (data.size() % 4)
587 {
588 data.push_back(0);
589 pad++;
590 }
591
592 // For JSON, CBOR, and Text use our component ID, subType, and version,
593 // otherwise use the supplied ones.
594 uint16_t compID = static_cast<uint16_t>(ComponentID::phosphorLogging);
595 uint8_t subType{};
596 uint8_t version{};
597
598 switch (file.format)
599 {
600 case UserDataFormat::json:
601 subType = static_cast<uint8_t>(UserDataFormat::json);
602 version = static_cast<uint8_t>(UserDataFormatVersion::json);
603 break;
604 case UserDataFormat::cbor:
605 subType = static_cast<uint8_t>(UserDataFormat::cbor);
606 version = static_cast<uint8_t>(UserDataFormatVersion::cbor);
607
608 // The CBOR parser will fail on the extra pad bytes since they
609 // aren't CBOR. Add the amount we padded to the end and other
610 // code will remove it all before parsing.
611 {
612 data.resize(data.size() + 4);
613 Stream stream{data};
614 stream.offset(data.size() - 4);
615 stream << pad;
616 }
617
618 break;
619 case UserDataFormat::text:
620 subType = static_cast<uint8_t>(UserDataFormat::text);
621 version = static_cast<uint8_t>(UserDataFormatVersion::text);
622 break;
623 case UserDataFormat::custom:
624 default:
625 // Use the passed in values
626 compID = componentID;
627 subType = file.subType;
628 version = file.version;
629 break;
630 }
631
632 return std::make_unique<UserData>(compID, subType, version, data);
Matt Spinler56ad2a02020-03-26 14:00:52 -0500633}
634
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600635} // namespace util
636
Matt Spinlercb6b0592019-07-16 15:58:51 -0500637} // namespace pels
638} // namespace openpower