Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 1 | #include "pel.hpp" |
| 2 | |
| 3 | #include "bcd_time.hpp" |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 4 | #include "failing_mtms.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 5 | #include "log_id.hpp" |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 6 | #include "section_factory.hpp" |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 7 | #include "src.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 8 | #include "stream.hpp" |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 9 | #include "user_data_formats.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 10 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 11 | #include <phosphor-logging/log.hpp> |
| 12 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 13 | namespace openpower |
| 14 | { |
| 15 | namespace pels |
| 16 | { |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 17 | namespace message = openpower::pels::message; |
| 18 | |
| 19 | PEL::PEL(const message::Entry& entry, uint32_t obmcLogID, uint64_t timestamp, |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 20 | phosphor::logging::Entry::Level severity, |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 21 | const AdditionalData& additionalData, |
| 22 | const DataInterfaceBase& dataIface) |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 23 | { |
| 24 | _ph = std::make_unique<PrivateHeader>(entry.componentID, obmcLogID, |
| 25 | timestamp); |
| 26 | _uh = std::make_unique<UserHeader>(entry, severity); |
| 27 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 28 | auto src = std::make_unique<SRC>(entry, additionalData); |
| 29 | _optionalSections.push_back(std::move(src)); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 30 | |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 31 | auto mtms = std::make_unique<FailingMTMS>(dataIface); |
| 32 | _optionalSections.push_back(std::move(mtms)); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 33 | |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 34 | if (!additionalData.empty()) |
| 35 | { |
| 36 | auto ud = util::makeADUserDataSection(additionalData); |
| 37 | _optionalSections.push_back(std::move(ud)); |
| 38 | } |
| 39 | |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 40 | _ph->setSectionCount(2 + _optionalSections.size()); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 41 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 42 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 43 | PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 47 | PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 48 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 49 | populateFromRawData(data, obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 52 | void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 53 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 54 | Stream pelData{data}; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 55 | _ph = std::make_unique<PrivateHeader>(pelData); |
| 56 | if (obmcLogID != 0) |
| 57 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 58 | _ph->setOBMCLogID(obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | _uh = std::make_unique<UserHeader>(pelData); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 62 | |
| 63 | // Use the section factory to create the rest of the objects |
| 64 | for (size_t i = 2; i < _ph->sectionCount(); i++) |
| 65 | { |
| 66 | auto section = section_factory::create(pelData); |
| 67 | _optionalSections.push_back(std::move(section)); |
| 68 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool PEL::valid() const |
| 72 | { |
| 73 | bool valid = _ph->valid(); |
| 74 | |
| 75 | if (valid) |
| 76 | { |
| 77 | valid = _uh->valid(); |
| 78 | } |
| 79 | |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 80 | if (valid) |
| 81 | { |
| 82 | if (!std::all_of(_optionalSections.begin(), _optionalSections.end(), |
| 83 | [](const auto& section) { return section->valid(); })) |
| 84 | { |
| 85 | valid = false; |
| 86 | } |
| 87 | } |
| 88 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 89 | return valid; |
| 90 | } |
| 91 | |
| 92 | void PEL::setCommitTime() |
| 93 | { |
| 94 | auto now = std::chrono::system_clock::now(); |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 95 | _ph->setCommitTimestamp(getBCDTime(now)); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void PEL::assignID() |
| 99 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 100 | _ph->setID(generatePELID()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void PEL::flatten(std::vector<uint8_t>& pelBuffer) |
| 104 | { |
| 105 | Stream pelData{pelBuffer}; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 106 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 107 | if (!valid()) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 108 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 109 | using namespace phosphor::logging; |
| 110 | log<level::WARNING>("Unflattening an invalid PEL"); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 113 | _ph->flatten(pelData); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 114 | _uh->flatten(pelData); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 115 | |
| 116 | for (auto& section : _optionalSections) |
| 117 | { |
| 118 | section->flatten(pelData); |
| 119 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | std::vector<uint8_t> PEL::data() |
| 123 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 124 | std::vector<uint8_t> pelData; |
| 125 | flatten(pelData); |
| 126 | return pelData; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 129 | std::optional<SRC*> PEL::primarySRC() const |
| 130 | { |
| 131 | auto src = std::find_if( |
| 132 | _optionalSections.begin(), _optionalSections.end(), [](auto& section) { |
| 133 | return section->header().id == |
| 134 | static_cast<uint16_t>(SectionID::primarySRC); |
| 135 | }); |
| 136 | if (src != _optionalSections.end()) |
| 137 | { |
| 138 | return static_cast<SRC*>(src->get()); |
| 139 | } |
| 140 | |
| 141 | return std::nullopt; |
| 142 | } |
| 143 | |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 144 | namespace util |
| 145 | { |
| 146 | |
| 147 | std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad) |
| 148 | { |
| 149 | assert(!ad.empty()); |
| 150 | nlohmann::json json; |
| 151 | |
| 152 | // Remove the 'ESEL' entry, as it contains a full PEL in the value. |
| 153 | if (ad.getValue("ESEL")) |
| 154 | { |
| 155 | auto newAD = ad; |
| 156 | newAD.remove("ESEL"); |
| 157 | json = newAD.toJSON(); |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | json = ad.toJSON(); |
| 162 | } |
| 163 | |
| 164 | auto jsonString = json.dump(); |
| 165 | std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end()); |
| 166 | |
| 167 | return std::make_unique<UserData>( |
| 168 | static_cast<uint16_t>(ComponentID::phosphorLogging), |
| 169 | static_cast<uint8_t>(UserDataFormat::json), |
| 170 | static_cast<uint8_t>(UserDataFormatVersion::json), jsonData); |
| 171 | } |
| 172 | |
| 173 | } // namespace util |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 174 | } // namespace pels |
| 175 | } // namespace openpower |