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