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" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame^] | 5 | #include "hexdump.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 6 | #include "log_id.hpp" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame^] | 7 | #include "pel_values.hpp" |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 8 | #include "section_factory.hpp" |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 9 | #include "src.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 10 | #include "stream.hpp" |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 11 | #include "user_data_formats.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 12 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame^] | 13 | #include <iostream> |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 14 | #include <phosphor-logging/log.hpp> |
| 15 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 16 | namespace openpower |
| 17 | { |
| 18 | namespace pels |
| 19 | { |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 20 | namespace message = openpower::pels::message; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame^] | 21 | namespace pv = openpower::pels::pel_values; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 22 | |
| 23 | PEL::PEL(const message::Entry& entry, uint32_t obmcLogID, uint64_t timestamp, |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 24 | phosphor::logging::Entry::Level severity, |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 25 | const AdditionalData& additionalData, |
| 26 | const DataInterfaceBase& dataIface) |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 27 | { |
| 28 | _ph = std::make_unique<PrivateHeader>(entry.componentID, obmcLogID, |
| 29 | timestamp); |
| 30 | _uh = std::make_unique<UserHeader>(entry, severity); |
| 31 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 32 | auto src = std::make_unique<SRC>(entry, additionalData); |
| 33 | _optionalSections.push_back(std::move(src)); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 34 | |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 35 | auto mtms = std::make_unique<FailingMTMS>(dataIface); |
| 36 | _optionalSections.push_back(std::move(mtms)); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 37 | |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 38 | if (!additionalData.empty()) |
| 39 | { |
| 40 | auto ud = util::makeADUserDataSection(additionalData); |
| 41 | _optionalSections.push_back(std::move(ud)); |
| 42 | } |
| 43 | |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 44 | _ph->setSectionCount(2 + _optionalSections.size()); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 45 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 46 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 47 | PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 51 | PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 52 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 53 | populateFromRawData(data, obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 56 | void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 57 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 58 | Stream pelData{data}; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 59 | _ph = std::make_unique<PrivateHeader>(pelData); |
| 60 | if (obmcLogID != 0) |
| 61 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 62 | _ph->setOBMCLogID(obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | _uh = std::make_unique<UserHeader>(pelData); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 66 | |
| 67 | // Use the section factory to create the rest of the objects |
| 68 | for (size_t i = 2; i < _ph->sectionCount(); i++) |
| 69 | { |
| 70 | auto section = section_factory::create(pelData); |
| 71 | _optionalSections.push_back(std::move(section)); |
| 72 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool PEL::valid() const |
| 76 | { |
| 77 | bool valid = _ph->valid(); |
| 78 | |
| 79 | if (valid) |
| 80 | { |
| 81 | valid = _uh->valid(); |
| 82 | } |
| 83 | |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 84 | if (valid) |
| 85 | { |
| 86 | if (!std::all_of(_optionalSections.begin(), _optionalSections.end(), |
| 87 | [](const auto& section) { return section->valid(); })) |
| 88 | { |
| 89 | valid = false; |
| 90 | } |
| 91 | } |
| 92 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 93 | return valid; |
| 94 | } |
| 95 | |
| 96 | void PEL::setCommitTime() |
| 97 | { |
| 98 | auto now = std::chrono::system_clock::now(); |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 99 | _ph->setCommitTimestamp(getBCDTime(now)); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void PEL::assignID() |
| 103 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 104 | _ph->setID(generatePELID()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void PEL::flatten(std::vector<uint8_t>& pelBuffer) |
| 108 | { |
| 109 | Stream pelData{pelBuffer}; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 110 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 111 | if (!valid()) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 112 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 113 | using namespace phosphor::logging; |
| 114 | log<level::WARNING>("Unflattening an invalid PEL"); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 117 | _ph->flatten(pelData); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 118 | _uh->flatten(pelData); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 119 | |
| 120 | for (auto& section : _optionalSections) |
| 121 | { |
| 122 | section->flatten(pelData); |
| 123 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | std::vector<uint8_t> PEL::data() |
| 127 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 128 | std::vector<uint8_t> pelData; |
| 129 | flatten(pelData); |
| 130 | return pelData; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 133 | std::optional<SRC*> PEL::primarySRC() const |
| 134 | { |
| 135 | auto src = std::find_if( |
| 136 | _optionalSections.begin(), _optionalSections.end(), [](auto& section) { |
| 137 | return section->header().id == |
| 138 | static_cast<uint16_t>(SectionID::primarySRC); |
| 139 | }); |
| 140 | if (src != _optionalSections.end()) |
| 141 | { |
| 142 | return static_cast<SRC*>(src->get()); |
| 143 | } |
| 144 | |
| 145 | return std::nullopt; |
| 146 | } |
| 147 | |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 148 | namespace util |
| 149 | { |
| 150 | |
| 151 | std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad) |
| 152 | { |
| 153 | assert(!ad.empty()); |
| 154 | nlohmann::json json; |
| 155 | |
| 156 | // Remove the 'ESEL' entry, as it contains a full PEL in the value. |
| 157 | if (ad.getValue("ESEL")) |
| 158 | { |
| 159 | auto newAD = ad; |
| 160 | newAD.remove("ESEL"); |
| 161 | json = newAD.toJSON(); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | json = ad.toJSON(); |
| 166 | } |
| 167 | |
| 168 | auto jsonString = json.dump(); |
| 169 | std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end()); |
| 170 | |
| 171 | return std::make_unique<UserData>( |
| 172 | static_cast<uint16_t>(ComponentID::phosphorLogging), |
| 173 | static_cast<uint8_t>(UserDataFormat::json), |
| 174 | static_cast<uint8_t>(UserDataFormatVersion::json), jsonData); |
| 175 | } |
| 176 | |
| 177 | } // namespace util |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame^] | 178 | void PEL::printSectionInJSON(Section& section, std::string& buf) const |
| 179 | { |
| 180 | char tmpB[5]; |
| 181 | if (section.valid()) |
| 182 | { |
| 183 | uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8), |
| 184 | static_cast<uint8_t>(section.header().id)}; |
| 185 | sprintf(tmpB, "%c%c", id[0], id[1]); |
| 186 | std::string sectionID(tmpB); |
| 187 | std::string sectionName = pv::sectionTitles.count(sectionID) |
| 188 | ? pv::sectionTitles.at(sectionID) |
| 189 | : "Unknown Section"; |
| 190 | buf += "\n\"" + sectionName + "\":[\n "; |
| 191 | std::vector<uint8_t> data; |
| 192 | Stream s{data}; |
| 193 | section.flatten(s); |
| 194 | std::string dstr = dumpHex(std::data(data), data.size()); |
| 195 | buf += dstr + "\n],\n"; |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | buf += "\n\"Invalid Section \":[\n invalid \n],\n"; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void PEL::toJSON() |
| 204 | { |
| 205 | std::string buf = "{"; |
| 206 | printSectionInJSON(*(_ph.get()), buf); |
| 207 | printSectionInJSON(*(_uh.get()), buf); |
| 208 | for (auto& section : this->optionalSections()) |
| 209 | { |
| 210 | printSectionInJSON(*(section.get()), buf); |
| 211 | } |
| 212 | buf += "}"; |
| 213 | std::size_t found = buf.rfind(","); |
| 214 | if (found != std::string::npos) |
| 215 | buf.replace(found, 1, ""); |
| 216 | std::cout << buf << std::endl; |
| 217 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 218 | } // namespace pels |
| 219 | } // namespace openpower |