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 | */ |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
| 18 | #include "../bcd_time.hpp" |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 19 | #include "../paths.hpp" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 20 | #include "../pel.hpp" |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 21 | #include "../pel_types.hpp" |
| 22 | #include "../pel_values.hpp" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 23 | |
| 24 | #include <CLI/CLI.hpp> |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 25 | #include <bitset> |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 26 | #include <iostream> |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 27 | #include <phosphor-logging/log.hpp> |
| 28 | #include <regex> |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 29 | #include <string> |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 30 | #include <xyz/openbmc_project/Common/File/error.hpp> |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 31 | |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 32 | namespace fs = std::filesystem; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 33 | using namespace phosphor::logging; |
| 34 | using namespace openpower::pels; |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 35 | namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error; |
| 36 | namespace message = openpower::pels::message; |
| 37 | namespace pv = openpower::pels::pel_values; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 38 | |
Aatir | e340c13 | 2019-12-09 14:19:29 -0600 | [diff] [blame] | 39 | /** |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 40 | * @brief helper function to get PEL commit timestamp from file name |
| 41 | * @retrun BCDTime - PEL commit timestamp |
| 42 | * @param[in] std::string - file name |
| 43 | */ |
| 44 | BCDTime fileNameToTimestamp(const std::string& fileName) |
| 45 | { |
| 46 | std::string token = fileName.substr(0, fileName.find("_")); |
| 47 | int i = 0; |
| 48 | BCDTime tmp; |
| 49 | if (token.length() >= 14) |
| 50 | { |
| 51 | try |
| 52 | { |
| 53 | tmp.yearMSB = std::stoi(token.substr(i, 2), 0, 16); |
| 54 | } |
| 55 | catch (std::exception& err) |
| 56 | { |
| 57 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 58 | } |
| 59 | i += 2; |
| 60 | try |
| 61 | { |
| 62 | tmp.yearLSB = std::stoi(token.substr(i, 2), 0, 16); |
| 63 | } |
| 64 | catch (std::exception& err) |
| 65 | { |
| 66 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 67 | } |
| 68 | i += 2; |
| 69 | try |
| 70 | { |
| 71 | tmp.month = std::stoi(token.substr(i, 2), 0, 16); |
| 72 | } |
| 73 | catch (std::exception& err) |
| 74 | { |
| 75 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 76 | } |
| 77 | i += 2; |
| 78 | try |
| 79 | { |
| 80 | tmp.day = std::stoi(token.substr(i, 2), 0, 16); |
| 81 | } |
| 82 | catch (std::exception& err) |
| 83 | { |
| 84 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 85 | } |
| 86 | i += 2; |
| 87 | try |
| 88 | { |
| 89 | tmp.hour = std::stoi(token.substr(i, 2), 0, 16); |
| 90 | } |
| 91 | catch (std::exception& err) |
| 92 | { |
| 93 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 94 | } |
| 95 | i += 2; |
| 96 | try |
| 97 | { |
| 98 | tmp.minutes = std::stoi(token.substr(i, 2), 0, 16); |
| 99 | } |
| 100 | catch (std::exception& err) |
| 101 | { |
| 102 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 103 | } |
| 104 | i += 2; |
| 105 | try |
| 106 | { |
| 107 | tmp.seconds = std::stoi(token.substr(i, 2), 0, 16); |
| 108 | } |
| 109 | catch (std::exception& err) |
| 110 | { |
| 111 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 112 | } |
| 113 | i += 2; |
| 114 | try |
| 115 | { |
| 116 | tmp.hundredths = std::stoi(token.substr(i, 2), 0, 16); |
| 117 | } |
| 118 | catch (std::exception& err) |
| 119 | { |
| 120 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 121 | } |
| 122 | } |
| 123 | return tmp; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @brief helper function to get PEL id from file name |
| 128 | * @retrun uint32_t - PEL id |
| 129 | * @param[in] std::string - file name |
| 130 | */ |
| 131 | uint32_t fileNameToPELId(const std::string& fileName) |
| 132 | { |
| 133 | uint32_t num = 0; |
| 134 | try |
| 135 | { |
| 136 | num = std::stoi(fileName.substr(fileName.find("_") + 1), 0, 16); |
| 137 | } |
| 138 | catch (std::exception& err) |
| 139 | { |
| 140 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 141 | } |
| 142 | return num; |
| 143 | } |
| 144 | |
| 145 | /** |
Aatir | e340c13 | 2019-12-09 14:19:29 -0600 | [diff] [blame] | 146 | * @brief helper function to check string suffix |
| 147 | * @retrun bool - true with suffix matches |
| 148 | * @param[in] std::string - string to check for suffix |
| 149 | * @param[in] std::string - suffix string |
| 150 | */ |
| 151 | bool ends_with(const std::string& str, const std::string& end) |
| 152 | { |
| 153 | size_t slen = str.size(), elen = end.size(); |
| 154 | if (slen < elen) |
| 155 | return false; |
| 156 | while (elen) |
| 157 | { |
| 158 | if (str[--slen] != end[--elen]) |
| 159 | return false; |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 164 | /** |
| 165 | * @brief get data form raw PEL file. |
| 166 | * @param[in] std::string Name of file with raw PEL |
| 167 | * @return std::vector<uint8_t> char vector read from raw PEL file. |
| 168 | */ |
Aatir | bad5f8a | 2019-12-10 15:27:16 -0600 | [diff] [blame] | 169 | std::vector<uint8_t> getFileData(const std::string& name) |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 170 | { |
| 171 | std::ifstream file(name, std::ifstream::in); |
| 172 | if (file.good()) |
| 173 | { |
| 174 | std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), |
| 175 | std::istreambuf_iterator<char>()}; |
| 176 | return data; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | printf("Can't open raw PEL file"); |
| 181 | return {}; |
| 182 | } |
| 183 | } |
| 184 | |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 185 | template <typename T> |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 186 | std::string genPELJSON(T itr, bool hidden, message::Registry& registry) |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 187 | { |
| 188 | std::size_t found; |
| 189 | std::string val; |
| 190 | char tmpValStr[50]; |
| 191 | std::string listStr; |
| 192 | char name[50]; |
| 193 | sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", itr.second.yearMSB, |
| 194 | itr.second.yearLSB, itr.second.month, itr.second.day, |
| 195 | itr.second.hour, itr.second.minutes, itr.second.seconds, |
| 196 | itr.second.hundredths, itr.first); |
| 197 | std::string fileName(name); |
| 198 | fileName = EXTENSION_PERSIST_DIR "/pels/logs/" + fileName; |
| 199 | try |
| 200 | { |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 201 | std::vector<uint8_t> data = getFileData(fileName); |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 202 | if (!data.empty()) |
| 203 | { |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 204 | PEL pel{data}; |
| 205 | std::bitset<16> actionFlags{pel.userHeader().actionFlags()}; |
| 206 | if (pel.valid() && (hidden || !actionFlags.test(hiddenFlagBit))) |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 207 | { |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 208 | // id |
| 209 | sprintf(tmpValStr, "0x%X", pel.privateHeader().id()); |
| 210 | val = std::string(tmpValStr); |
| 211 | listStr += "\t\"" + val + "\": {\n"; |
| 212 | // ASCII |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 213 | if (pel.primarySRC()) |
| 214 | { |
| 215 | val = pel.primarySRC().value()->asciiString(); |
| 216 | listStr += "\t\t\"SRC\": \"" + |
| 217 | val.substr(0, val.find(0x20)) + "\",\n"; |
| 218 | // Registry message |
| 219 | auto regVal = pel.primarySRC().value()->getErrorDetails( |
| 220 | registry, DetailLevel::message, true); |
| 221 | if (regVal) |
| 222 | { |
| 223 | val = regVal.value(); |
| 224 | listStr += "\t\t\"Message\": \"" + val + "\",\n"; |
| 225 | } |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | listStr += "\t\t\"SRC\": \"No SRC\",\n"; |
| 230 | } |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 231 | // platformid |
| 232 | sprintf(tmpValStr, "0x%X", pel.privateHeader().plid()); |
| 233 | val = std::string(tmpValStr); |
| 234 | listStr += "\t\t\"PLID\": \"" + val + "\",\n"; |
| 235 | // creatorid |
| 236 | sprintf(tmpValStr, "%c", pel.privateHeader().creatorID()); |
| 237 | std::string creatorID(tmpValStr); |
| 238 | val = pv::creatorIDs.count(creatorID) |
| 239 | ? pv::creatorIDs.at(creatorID) |
| 240 | : "Unknown Creator ID"; |
| 241 | listStr += "\t\t\"CreatorID\": \"" + val + "\",\n"; |
| 242 | // subsytem |
| 243 | std::string subsystem = pv::getValue( |
| 244 | pel.userHeader().subsystem(), pel_values::subsystemValues); |
| 245 | listStr += "\t\t\"Subsystem\": \"" + subsystem + "\",\n"; |
| 246 | // commit time |
| 247 | sprintf(tmpValStr, "%02X/%02X/%02X%02X %02X:%02X:%02X", |
| 248 | pel.privateHeader().commitTimestamp().month, |
| 249 | pel.privateHeader().commitTimestamp().day, |
| 250 | pel.privateHeader().commitTimestamp().yearMSB, |
| 251 | pel.privateHeader().commitTimestamp().yearLSB, |
| 252 | pel.privateHeader().commitTimestamp().hour, |
| 253 | pel.privateHeader().commitTimestamp().minutes, |
| 254 | pel.privateHeader().commitTimestamp().seconds); |
| 255 | val = std::string(tmpValStr); |
| 256 | listStr += "\t\t\"Commit Time\": \"" + val + "\",\n"; |
| 257 | // severity |
| 258 | std::string severity = pv::getValue(pel.userHeader().severity(), |
| 259 | pel_values::severityValues); |
| 260 | listStr += "\t\t\"Sev\": \"" + severity + "\",\n "; |
| 261 | // compID |
| 262 | sprintf(tmpValStr, "0x%X", |
| 263 | pel.privateHeader().header().componentID); |
| 264 | val = std::string(tmpValStr); |
| 265 | listStr += "\t\t\"CompID\": \"" + val + "\",\n "; |
| 266 | |
| 267 | found = listStr.rfind(","); |
| 268 | if (found != std::string::npos) |
| 269 | { |
| 270 | listStr.replace(found, 1, ""); |
| 271 | listStr += "\t}, \n"; |
| 272 | } |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 273 | } |
| 274 | } |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 275 | else |
| 276 | { |
| 277 | log<level::ERR>("Empty PEL file", |
| 278 | entry("FILENAME=%s", fileName.c_str()), |
| 279 | entry("ERROR=%s", "Empty PEL file")); |
| 280 | } |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 281 | } |
| 282 | catch (std::exception& e) |
| 283 | { |
| 284 | log<level::ERR>("Hit exception while reading PEL File", |
| 285 | entry("FILENAME=%s", fileName.c_str()), |
| 286 | entry("ERROR=%s", e.what())); |
| 287 | } |
| 288 | return listStr; |
| 289 | } |
| 290 | /** |
| 291 | * @brief Print a list of PELs |
| 292 | */ |
| 293 | void printList(bool order, bool hidden) |
| 294 | { |
| 295 | std::string listStr; |
| 296 | std::map<uint32_t, BCDTime> PELs; |
| 297 | std::size_t found; |
| 298 | listStr = "{\n"; |
| 299 | for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs"); |
| 300 | it != fs::directory_iterator(); ++it) |
| 301 | { |
| 302 | if (!fs::is_regular_file((*it).path())) |
| 303 | { |
| 304 | continue; |
| 305 | } |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 306 | else |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 307 | { |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 308 | PELs.emplace(fileNameToPELId((*it).path().filename()), |
| 309 | fileNameToTimestamp((*it).path().filename())); |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 310 | } |
| 311 | } |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 312 | message::Registry registry(getMessageRegistryPath() / |
| 313 | message::registryFileName); |
| 314 | auto buildJSON = [&listStr, &hidden, ®istry](const auto& i) { |
| 315 | listStr += genPELJSON(i, hidden, registry); |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 316 | }; |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 317 | if (order) |
| 318 | { |
| 319 | std::for_each(PELs.rbegin(), PELs.rend(), buildJSON); |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | std::for_each(PELs.begin(), PELs.end(), buildJSON); |
| 324 | } |
| 325 | |
| 326 | found = listStr.rfind(","); |
| 327 | if (found != std::string::npos) |
| 328 | { |
| 329 | listStr.replace(found, 1, ""); |
| 330 | listStr += "\n}\n"; |
| 331 | printf("%s", listStr.c_str()); |
| 332 | } |
| 333 | } |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 334 | |
| 335 | static void exitWithError(const std::string& help, const char* err) |
| 336 | { |
| 337 | std::cerr << "ERROR: " << err << std::endl << help << std::endl; |
| 338 | exit(-1); |
| 339 | } |
| 340 | |
| 341 | int main(int argc, char** argv) |
| 342 | { |
| 343 | CLI::App app{"OpenBMC PEL Tool"}; |
| 344 | std::string fileName; |
Aatir | e340c13 | 2019-12-09 14:19:29 -0600 | [diff] [blame] | 345 | std::string idPEL; |
| 346 | bool listPEL = false; |
| 347 | bool listPELDescOrd = false; |
| 348 | bool listPELShowHidden = false; |
| 349 | app.add_option("-f,--file", fileName, |
| 350 | "Display a PEL using its Raw PEL file"); |
| 351 | app.add_option("-i, --id", idPEL, "Display a PEL based on its ID"); |
Aatir | bad5f8a | 2019-12-10 15:27:16 -0600 | [diff] [blame] | 352 | app.add_flag("-l", listPEL, "List PELs"); |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 353 | app.add_flag("-r", listPELDescOrd, "Reverse order of output"); |
| 354 | app.add_flag("-s", listPELShowHidden, "Show hidden PELs"); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 355 | CLI11_PARSE(app, argc, argv); |
| 356 | |
| 357 | if (!fileName.empty()) |
| 358 | { |
| 359 | std::vector<uint8_t> data = getFileData(fileName); |
| 360 | if (!data.empty()) |
| 361 | { |
| 362 | PEL pel{data}; |
| 363 | pel.toJSON(); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | exitWithError(app.help("", CLI::AppFormatMode::All), |
| 368 | "Raw PEL file can't be read."); |
| 369 | } |
| 370 | } |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 371 | |
Aatir | e340c13 | 2019-12-09 14:19:29 -0600 | [diff] [blame] | 372 | else if (!idPEL.empty()) |
| 373 | { |
| 374 | for (auto it = |
| 375 | fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs"); |
| 376 | it != fs::directory_iterator(); ++it) |
| 377 | { |
| 378 | if (!fs::is_regular_file((*it).path())) |
| 379 | { |
| 380 | continue; |
| 381 | } |
| 382 | try |
| 383 | { |
| 384 | for (auto& c : idPEL) |
| 385 | c = toupper(c); |
| 386 | size_t found = idPEL.find("0X"); |
| 387 | if (found == 0) |
| 388 | { |
| 389 | idPEL.erase(0, 2); |
| 390 | } |
| 391 | if (ends_with((*it).path(), idPEL)) |
| 392 | { |
| 393 | std::vector<uint8_t> data = getFileData((*it).path()); |
| 394 | if (!data.empty()) |
| 395 | { |
| 396 | PEL pel{data}; |
| 397 | if (pel.valid()) |
| 398 | { |
| 399 | pel.toJSON(); |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | log<level::ERR>( |
| 404 | "PEL File contains invalid PEL", |
| 405 | entry("FILENAME=%s", (*it).path().c_str()), |
| 406 | entry("ERROR=%s", "file contains invalid PEL")); |
| 407 | } |
| 408 | } |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | catch (std::exception& e) |
| 413 | { |
| 414 | log<level::ERR>("Hit exception while reading PEL File", |
| 415 | entry("FILENAME=%s", (*it).path().c_str()), |
| 416 | entry("ERROR=%s", e.what())); |
| 417 | } |
| 418 | } |
| 419 | } |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 420 | else if (listPEL) |
| 421 | { |
| 422 | |
| 423 | printList(listPELDescOrd, listPELShowHidden); |
| 424 | } |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 425 | else |
| 426 | { |
| 427 | exitWithError(app.help("", CLI::AppFormatMode::All), |
| 428 | "Raw PEL file path not specified."); |
| 429 | } |
| 430 | return 0; |
| 431 | } |