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 | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 19 | #include "../json_utils.hpp" |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 20 | #include "../paths.hpp" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 21 | #include "../pel.hpp" |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 22 | #include "../pel_types.hpp" |
| 23 | #include "../pel_values.hpp" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 24 | |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 25 | #include <Python.h> |
| 26 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 27 | #include <CLI/CLI.hpp> |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 28 | #include <bitset> |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 29 | #include <fstream> |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 30 | #include <iostream> |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 31 | #include <phosphor-logging/log.hpp> |
| 32 | #include <regex> |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 33 | #include <string> |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 34 | #include <xyz/openbmc_project/Common/File/error.hpp> |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 35 | |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 36 | namespace fs = std::filesystem; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 37 | using namespace phosphor::logging; |
| 38 | using namespace openpower::pels; |
Matt Spinler | 27de6f3 | 2020-02-21 08:35:57 -0600 | [diff] [blame] | 39 | using sdbusplus::exception::SdBusError; |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 40 | namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error; |
| 41 | namespace message = openpower::pels::message; |
| 42 | namespace pv = openpower::pels::pel_values; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 43 | |
Matt Spinler | 27de6f3 | 2020-02-21 08:35:57 -0600 | [diff] [blame] | 44 | using PELFunc = std::function<void(const PEL&)>; |
Matt Spinler | 0d804ef | 2020-05-12 16:16:26 -0500 | [diff] [blame] | 45 | message::Registry registry(getPELReadOnlyDataPath() / message::registryFileName, |
Matt Spinler | d8e2900 | 2020-04-09 09:11:22 -0500 | [diff] [blame] | 46 | false); |
Matt Spinler | 27de6f3 | 2020-02-21 08:35:57 -0600 | [diff] [blame] | 47 | namespace service |
| 48 | { |
| 49 | constexpr auto logging = "xyz.openbmc_project.Logging"; |
| 50 | } // namespace service |
| 51 | |
| 52 | namespace interface |
| 53 | { |
| 54 | constexpr auto deleteObj = "xyz.openbmc_project.Object.Delete"; |
| 55 | constexpr auto deleteAll = "xyz.openbmc_project.Collection.DeleteAll"; |
| 56 | } // namespace interface |
| 57 | |
| 58 | namespace object_path |
| 59 | { |
| 60 | constexpr auto logEntry = "/xyz/openbmc_project/logging/entry/"; |
| 61 | constexpr auto logging = "/xyz/openbmc_project/logging"; |
| 62 | } // namespace object_path |
| 63 | |
Aatir | e340c13 | 2019-12-09 14:19:29 -0600 | [diff] [blame] | 64 | /** |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 65 | * @brief helper function to get PEL commit timestamp from file name |
| 66 | * @retrun BCDTime - PEL commit timestamp |
| 67 | * @param[in] std::string - file name |
| 68 | */ |
| 69 | BCDTime fileNameToTimestamp(const std::string& fileName) |
| 70 | { |
| 71 | std::string token = fileName.substr(0, fileName.find("_")); |
| 72 | int i = 0; |
| 73 | BCDTime tmp; |
| 74 | if (token.length() >= 14) |
| 75 | { |
| 76 | try |
| 77 | { |
| 78 | tmp.yearMSB = std::stoi(token.substr(i, 2), 0, 16); |
| 79 | } |
| 80 | catch (std::exception& err) |
| 81 | { |
| 82 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 83 | } |
| 84 | i += 2; |
| 85 | try |
| 86 | { |
| 87 | tmp.yearLSB = std::stoi(token.substr(i, 2), 0, 16); |
| 88 | } |
| 89 | catch (std::exception& err) |
| 90 | { |
| 91 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 92 | } |
| 93 | i += 2; |
| 94 | try |
| 95 | { |
| 96 | tmp.month = std::stoi(token.substr(i, 2), 0, 16); |
| 97 | } |
| 98 | catch (std::exception& err) |
| 99 | { |
| 100 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 101 | } |
| 102 | i += 2; |
| 103 | try |
| 104 | { |
| 105 | tmp.day = std::stoi(token.substr(i, 2), 0, 16); |
| 106 | } |
| 107 | catch (std::exception& err) |
| 108 | { |
| 109 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 110 | } |
| 111 | i += 2; |
| 112 | try |
| 113 | { |
| 114 | tmp.hour = std::stoi(token.substr(i, 2), 0, 16); |
| 115 | } |
| 116 | catch (std::exception& err) |
| 117 | { |
| 118 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 119 | } |
| 120 | i += 2; |
| 121 | try |
| 122 | { |
| 123 | tmp.minutes = std::stoi(token.substr(i, 2), 0, 16); |
| 124 | } |
| 125 | catch (std::exception& err) |
| 126 | { |
| 127 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 128 | } |
| 129 | i += 2; |
| 130 | try |
| 131 | { |
| 132 | tmp.seconds = std::stoi(token.substr(i, 2), 0, 16); |
| 133 | } |
| 134 | catch (std::exception& err) |
| 135 | { |
| 136 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 137 | } |
| 138 | i += 2; |
| 139 | try |
| 140 | { |
| 141 | tmp.hundredths = std::stoi(token.substr(i, 2), 0, 16); |
| 142 | } |
| 143 | catch (std::exception& err) |
| 144 | { |
| 145 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 146 | } |
| 147 | } |
| 148 | return tmp; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @brief helper function to get PEL id from file name |
| 153 | * @retrun uint32_t - PEL id |
| 154 | * @param[in] std::string - file name |
| 155 | */ |
| 156 | uint32_t fileNameToPELId(const std::string& fileName) |
| 157 | { |
| 158 | uint32_t num = 0; |
| 159 | try |
| 160 | { |
| 161 | num = std::stoi(fileName.substr(fileName.find("_") + 1), 0, 16); |
| 162 | } |
| 163 | catch (std::exception& err) |
| 164 | { |
| 165 | std::cout << "Conversion failure: " << err.what() << std::endl; |
| 166 | } |
| 167 | return num; |
| 168 | } |
| 169 | |
| 170 | /** |
Aatir | e340c13 | 2019-12-09 14:19:29 -0600 | [diff] [blame] | 171 | * @brief helper function to check string suffix |
| 172 | * @retrun bool - true with suffix matches |
| 173 | * @param[in] std::string - string to check for suffix |
| 174 | * @param[in] std::string - suffix string |
| 175 | */ |
| 176 | bool ends_with(const std::string& str, const std::string& end) |
| 177 | { |
| 178 | size_t slen = str.size(), elen = end.size(); |
| 179 | if (slen < elen) |
| 180 | return false; |
| 181 | while (elen) |
| 182 | { |
| 183 | if (str[--slen] != end[--elen]) |
| 184 | return false; |
| 185 | } |
| 186 | return true; |
| 187 | } |
| 188 | |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 189 | /** |
| 190 | * @brief get data form raw PEL file. |
| 191 | * @param[in] std::string Name of file with raw PEL |
| 192 | * @return std::vector<uint8_t> char vector read from raw PEL file. |
| 193 | */ |
Aatir | bad5f8a | 2019-12-10 15:27:16 -0600 | [diff] [blame] | 194 | std::vector<uint8_t> getFileData(const std::string& name) |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 195 | { |
| 196 | std::ifstream file(name, std::ifstream::in); |
| 197 | if (file.good()) |
| 198 | { |
| 199 | std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), |
| 200 | std::istreambuf_iterator<char>()}; |
| 201 | return data; |
| 202 | } |
| 203 | else |
| 204 | { |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 205 | return {}; |
| 206 | } |
| 207 | } |
| 208 | |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 209 | /** |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 210 | * @brief Initialize Python interpreter and gather all UD parser modules under |
| 211 | * the paths found in Python sys.path and the current user directory. |
| 212 | * This is to prevent calling a non-existant module which causes Python |
| 213 | * to print an import error message and breaking JSON output. |
| 214 | * |
| 215 | * @return std::vector<std::string> Vector of plugins found in filesystem |
| 216 | */ |
| 217 | std::vector<std::string> getPlugins() |
| 218 | { |
| 219 | Py_Initialize(); |
| 220 | std::vector<std::string> plugins; |
| 221 | std::vector<std::string> siteDirs; |
| 222 | PyObject* pName = PyUnicode_FromString("sys"); |
| 223 | PyObject* pModule = PyImport_Import(pName); |
| 224 | Py_XDECREF(pName); |
| 225 | PyObject* pDict = PyModule_GetDict(pModule); |
| 226 | Py_XDECREF(pModule); |
| 227 | PyObject* pResult = PyDict_GetItemString(pDict, "path"); |
| 228 | PyObject* pValue = PyUnicode_FromString("."); |
| 229 | PyList_Append(pResult, pValue); |
| 230 | Py_XDECREF(pValue); |
| 231 | auto list_size = PyList_Size(pResult); |
| 232 | for (auto i = 0; i < list_size; i++) |
| 233 | { |
| 234 | PyObject* item = PyList_GetItem(pResult, i); |
| 235 | PyObject* pBytes = PyUnicode_AsEncodedString(item, "utf-8", "~E~"); |
| 236 | const char* output = PyBytes_AS_STRING(pBytes); |
| 237 | Py_XDECREF(pBytes); |
| 238 | std::string tmpStr(output); |
| 239 | siteDirs.push_back(tmpStr); |
| 240 | } |
| 241 | for (const auto& dir : siteDirs) |
| 242 | { |
| 243 | if (fs::exists(dir + "/udparsers")) |
| 244 | { |
| 245 | for (const auto& entry : fs::directory_iterator(dir + "/udparsers")) |
| 246 | { |
| 247 | if (entry.is_directory() and |
| 248 | fs::exists(entry.path().string() + "/" + |
| 249 | entry.path().stem().string() + ".py")) |
| 250 | { |
| 251 | plugins.push_back(entry.path().stem()); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | return plugins; |
| 257 | } |
| 258 | |
| 259 | /** |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 260 | * @brief Creates JSON string of a PEL entry if fullPEL is false or prints to |
| 261 | * stdout the full PEL in JSON if fullPEL is true |
| 262 | * @param[in] itr - std::map iterator of <uint32_t, BCDTime> |
| 263 | * @param[in] hidden - Boolean to include hidden PELs |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 264 | * @param[in] includeInfo - Boolean to include informational PELs |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 265 | * @param[in] fullPEL - Boolean to print full JSON representation of PEL |
| 266 | * @param[in] foundPEL - Boolean to check if any PEL is present |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 267 | * @param[in] scrubRegex - SRC regex object |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 268 | * @param[in] plugins - Vector of strings of plugins found in filesystem |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 269 | * @return std::string - JSON string of PEL entry (empty if fullPEL is true) |
| 270 | */ |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 271 | template <typename T> |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 272 | std::string genPELJSON(T itr, bool hidden, bool includeInfo, bool fullPEL, |
| 273 | bool& foundPEL, |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 274 | const std::optional<std::regex>& scrubRegex, |
| 275 | const std::vector<std::string>& plugins) |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 276 | { |
| 277 | std::size_t found; |
| 278 | std::string val; |
| 279 | char tmpValStr[50]; |
| 280 | std::string listStr; |
| 281 | char name[50]; |
| 282 | sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", itr.second.yearMSB, |
| 283 | itr.second.yearLSB, itr.second.month, itr.second.day, |
| 284 | itr.second.hour, itr.second.minutes, itr.second.seconds, |
| 285 | itr.second.hundredths, itr.first); |
| 286 | std::string fileName(name); |
| 287 | fileName = EXTENSION_PERSIST_DIR "/pels/logs/" + fileName; |
| 288 | try |
| 289 | { |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 290 | std::vector<uint8_t> data = getFileData(fileName); |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 291 | if (data.empty()) |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 292 | { |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 293 | log<level::ERR>("Empty PEL file", |
| 294 | entry("FILENAME=%s", fileName.c_str())); |
| 295 | return listStr; |
| 296 | } |
| 297 | PEL pel{data}; |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 298 | if (!pel.valid()) |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 299 | { |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 300 | return listStr; |
| 301 | } |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 302 | if (!includeInfo && pel.userHeader().severity() == 0) |
| 303 | { |
| 304 | return listStr; |
| 305 | } |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 306 | std::bitset<16> actionFlags{pel.userHeader().actionFlags()}; |
| 307 | if (!hidden && actionFlags.test(hiddenFlagBit)) |
| 308 | { |
| 309 | return listStr; |
| 310 | } |
| 311 | if (pel.primarySRC() && scrubRegex) |
| 312 | { |
| 313 | val = pel.primarySRC().value()->asciiString(); |
| 314 | if (std::regex_search(trimEnd(val), scrubRegex.value(), |
| 315 | std::regex_constants::match_not_null)) |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 316 | { |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 317 | return listStr; |
| 318 | } |
| 319 | } |
| 320 | if (fullPEL) |
| 321 | { |
| 322 | if (!foundPEL) |
| 323 | { |
| 324 | std::cout << "[\n"; |
| 325 | foundPEL = true; |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 326 | } |
| 327 | else |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 328 | { |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 329 | std::cout << ",\n\n"; |
| 330 | } |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 331 | pel.toJSON(registry, plugins); |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 332 | } |
| 333 | else |
| 334 | { |
| 335 | // id |
| 336 | listStr += "\t\"" + |
| 337 | getNumberString("0x%X", pel.privateHeader().id()) + |
| 338 | "\": {\n"; |
| 339 | // ASCII |
| 340 | if (pel.primarySRC()) |
| 341 | { |
| 342 | val = pel.primarySRC().value()->asciiString(); |
| 343 | listStr += "\t\t\"SRC\": \"" + trimEnd(val) + "\",\n"; |
| 344 | // Registry message |
| 345 | auto regVal = pel.primarySRC().value()->getErrorDetails( |
| 346 | registry, DetailLevel::message, true); |
| 347 | if (regVal) |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 348 | { |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 349 | val = regVal.value(); |
| 350 | listStr += "\t\t\"Message\": \"" + val + "\",\n"; |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 351 | } |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 352 | } |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 353 | else |
| 354 | { |
| 355 | listStr += "\t\t\"SRC\": \"No SRC\",\n"; |
| 356 | } |
| 357 | // platformid |
| 358 | listStr += "\t\t\"PLID\": \"" + |
| 359 | getNumberString("0x%X", pel.privateHeader().plid()) + |
| 360 | "\",\n"; |
| 361 | // creatorid |
| 362 | std::string creatorID = |
| 363 | getNumberString("%c", pel.privateHeader().creatorID()); |
| 364 | val = pv::creatorIDs.count(creatorID) ? pv::creatorIDs.at(creatorID) |
| 365 | : "Unknown Creator ID"; |
| 366 | listStr += "\t\t\"CreatorID\": \"" + val + "\",\n"; |
| 367 | // subsytem |
| 368 | std::string subsystem = pv::getValue(pel.userHeader().subsystem(), |
| 369 | pel_values::subsystemValues); |
| 370 | listStr += "\t\t\"Subsystem\": \"" + subsystem + "\",\n"; |
| 371 | // commit time |
| 372 | sprintf(tmpValStr, "%02X/%02X/%02X%02X %02X:%02X:%02X", |
| 373 | pel.privateHeader().commitTimestamp().month, |
| 374 | pel.privateHeader().commitTimestamp().day, |
| 375 | pel.privateHeader().commitTimestamp().yearMSB, |
| 376 | pel.privateHeader().commitTimestamp().yearLSB, |
| 377 | pel.privateHeader().commitTimestamp().hour, |
| 378 | pel.privateHeader().commitTimestamp().minutes, |
| 379 | pel.privateHeader().commitTimestamp().seconds); |
| 380 | val = std::string(tmpValStr); |
| 381 | listStr += "\t\t\"Commit Time\": \"" + val + "\",\n"; |
| 382 | // severity |
| 383 | std::string severity = pv::getValue(pel.userHeader().severity(), |
| 384 | pel_values::severityValues); |
| 385 | listStr += "\t\t\"Sev\": \"" + severity + "\",\n "; |
| 386 | // compID |
| 387 | listStr += "\t\t\"CompID\": \"" + |
| 388 | getNumberString( |
| 389 | "0x%X", pel.privateHeader().header().componentID) + |
| 390 | "\",\n "; |
| 391 | found = listStr.rfind(","); |
| 392 | if (found != std::string::npos) |
| 393 | { |
| 394 | listStr.replace(found, 1, ""); |
| 395 | listStr += "\t},\n"; |
| 396 | } |
| 397 | foundPEL = true; |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | catch (std::exception& e) |
| 401 | { |
| 402 | log<level::ERR>("Hit exception while reading PEL File", |
| 403 | entry("FILENAME=%s", fileName.c_str()), |
| 404 | entry("ERROR=%s", e.what())); |
| 405 | } |
| 406 | return listStr; |
| 407 | } |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 408 | |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 409 | /** |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 410 | * @brief Print a list of PELs or a JSON array of PELs |
| 411 | * @param[in] order - Boolean to print in reverse orser |
| 412 | * @param[in] hidden - Boolean to include hidden PELs |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 413 | * @param[in] includeInfo - Boolean to include informational PELs |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 414 | * @param[in] fullPEL - Boolean to print full PEL into a JSON array |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 415 | * @param[in] scrubRegex - SRC regex object |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 416 | */ |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 417 | void printPELs(bool order, bool hidden, bool includeInfo, bool fullPEL, |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 418 | const std::optional<std::regex>& scrubRegex) |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 419 | { |
| 420 | std::string listStr; |
| 421 | std::map<uint32_t, BCDTime> PELs; |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 422 | std::vector<std::string> plugins; |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 423 | listStr = "{\n"; |
| 424 | for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs"); |
| 425 | it != fs::directory_iterator(); ++it) |
| 426 | { |
| 427 | if (!fs::is_regular_file((*it).path())) |
| 428 | { |
| 429 | continue; |
| 430 | } |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 431 | else |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 432 | { |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 433 | PELs.emplace(fileNameToPELId((*it).path().filename()), |
| 434 | fileNameToTimestamp((*it).path().filename())); |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 435 | } |
| 436 | } |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 437 | bool foundPEL = false; |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 438 | if (fullPEL) |
| 439 | { |
| 440 | plugins = getPlugins(); |
| 441 | } |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 442 | auto buildJSON = [&listStr, &hidden, &includeInfo, &fullPEL, &foundPEL, |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 443 | &scrubRegex, &plugins](const auto& i) { |
| 444 | listStr += genPELJSON(i, hidden, includeInfo, fullPEL, foundPEL, |
| 445 | scrubRegex, plugins); |
Aatir | 37822f6 | 2019-12-10 14:40:27 -0600 | [diff] [blame] | 446 | }; |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 447 | if (order) |
| 448 | { |
| 449 | std::for_each(PELs.rbegin(), PELs.rend(), buildJSON); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | std::for_each(PELs.begin(), PELs.end(), buildJSON); |
| 454 | } |
| 455 | |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 456 | if (foundPEL) |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 457 | { |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 458 | if (fullPEL) |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 459 | { |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 460 | std::cout << "]" << std::endl; |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | std::size_t found; |
| 465 | found = listStr.rfind(","); |
| 466 | if (found != std::string::npos) |
| 467 | { |
| 468 | listStr.replace(found, 1, ""); |
| 469 | listStr += "}\n"; |
| 470 | printf("%s", listStr.c_str()); |
| 471 | } |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 472 | } |
| 473 | } |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 474 | else |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 475 | { |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 476 | std::string emptyJSON = fullPEL ? "[]" : "{}"; |
| 477 | std::cout << emptyJSON << std::endl; |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 478 | } |
| 479 | } |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 480 | |
Matt Spinler | 27de6f3 | 2020-02-21 08:35:57 -0600 | [diff] [blame] | 481 | /** |
| 482 | * @brief Calls the function passed in on the PEL with the ID |
| 483 | * passed in. |
| 484 | * |
| 485 | * @param[in] id - The string version of the PEL ID, either with or |
| 486 | * without the 0x prefix. |
| 487 | * @param[in] func - The std::function<void(const PEL&)> function to run. |
| 488 | */ |
| 489 | void callFunctionOnPEL(const std::string& id, const PELFunc& func) |
| 490 | { |
| 491 | std::string pelID{id}; |
| 492 | std::transform(pelID.begin(), pelID.end(), pelID.begin(), toupper); |
| 493 | |
| 494 | if (pelID.find("0X") == 0) |
| 495 | { |
| 496 | pelID.erase(0, 2); |
| 497 | } |
| 498 | |
| 499 | bool found = false; |
| 500 | |
| 501 | for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs"); |
| 502 | it != fs::directory_iterator(); ++it) |
| 503 | { |
| 504 | // The PEL ID is part of the filename, so use that to find the PEL. |
| 505 | |
| 506 | if (!fs::is_regular_file((*it).path())) |
| 507 | { |
| 508 | continue; |
| 509 | } |
| 510 | |
| 511 | if (ends_with((*it).path(), pelID)) |
| 512 | { |
| 513 | found = true; |
| 514 | |
| 515 | auto data = getFileData((*it).path()); |
| 516 | if (!data.empty()) |
| 517 | { |
| 518 | PEL pel{data}; |
| 519 | |
| 520 | try |
| 521 | { |
| 522 | func(pel); |
| 523 | } |
| 524 | catch (std::exception& e) |
| 525 | { |
| 526 | std::cerr |
| 527 | << " Internal function threw an exception: " << e.what() |
| 528 | << "\n"; |
| 529 | exit(1); |
| 530 | } |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | std::cerr << "Could not read PEL file\n"; |
| 535 | exit(1); |
| 536 | } |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if (!found) |
| 542 | { |
| 543 | std::cerr << "PEL not found\n"; |
| 544 | exit(1); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * @brief Delete a PEL by deleting its corresponding event log. |
| 550 | * |
| 551 | * @param[in] pel - The PEL to delete |
| 552 | */ |
| 553 | void deletePEL(const PEL& pel) |
| 554 | { |
| 555 | std::string path{object_path::logEntry}; |
| 556 | path += std::to_string(pel.obmcLogID()); |
| 557 | |
| 558 | try |
| 559 | { |
| 560 | auto bus = sdbusplus::bus::new_default(); |
| 561 | auto method = bus.new_method_call(service::logging, path.c_str(), |
| 562 | interface::deleteObj, "Delete"); |
| 563 | auto reply = bus.call(method); |
| 564 | } |
| 565 | catch (const SdBusError& e) |
| 566 | { |
| 567 | std::cerr << "D-Bus call to delete event log " << pel.obmcLogID() |
| 568 | << " failed: " << e.what() << "\n"; |
| 569 | exit(1); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * @brief Delete all PELs by deleting all event logs. |
| 575 | */ |
| 576 | void deleteAllPELs() |
| 577 | { |
| 578 | try |
| 579 | { |
| 580 | // This may move to an audit log some day |
| 581 | log<level::INFO>("peltool deleting all event logs"); |
| 582 | |
| 583 | auto bus = sdbusplus::bus::new_default(); |
| 584 | auto method = |
| 585 | bus.new_method_call(service::logging, object_path::logging, |
| 586 | interface::deleteAll, "DeleteAll"); |
| 587 | auto reply = bus.call(method); |
| 588 | } |
| 589 | catch (const SdBusError& e) |
| 590 | { |
| 591 | std::cerr << "D-Bus call to delete all event logs failed: " << e.what() |
| 592 | << "\n"; |
| 593 | exit(1); |
| 594 | } |
| 595 | } |
| 596 | |
Matt Spinler | 1b420bc | 2020-02-21 08:54:25 -0600 | [diff] [blame] | 597 | /** |
| 598 | * @brief Display a single PEL |
| 599 | * |
| 600 | * @param[in] pel - the PEL to display |
| 601 | */ |
| 602 | void displayPEL(const PEL& pel) |
| 603 | { |
| 604 | if (pel.valid()) |
| 605 | { |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 606 | auto plugins = getPlugins(); |
| 607 | pel.toJSON(registry, plugins); |
Matt Spinler | 1b420bc | 2020-02-21 08:54:25 -0600 | [diff] [blame] | 608 | } |
| 609 | else |
| 610 | { |
| 611 | std::cerr << "PEL was malformed\n"; |
| 612 | exit(1); |
| 613 | } |
| 614 | } |
| 615 | |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 616 | /** |
| 617 | * @brief Print number of PELs |
| 618 | * @param[in] hidden - Bool to include hidden logs |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 619 | * @param[in] includeInfo - Bool to include informational logs |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 620 | * @param[in] scrubRegex - SRC regex object |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 621 | */ |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 622 | void printPELCount(bool hidden, bool includeInfo, |
| 623 | const std::optional<std::regex>& scrubRegex) |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 624 | { |
| 625 | std::size_t count = 0; |
| 626 | for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs"); |
| 627 | it != fs::directory_iterator(); ++it) |
| 628 | { |
| 629 | if (!fs::is_regular_file((*it).path())) |
| 630 | { |
| 631 | continue; |
| 632 | } |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 633 | std::vector<uint8_t> data = getFileData((*it).path()); |
| 634 | if (data.empty()) |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 635 | { |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 636 | continue; |
| 637 | } |
| 638 | PEL pel{data}; |
| 639 | if (!pel.valid()) |
| 640 | { |
| 641 | continue; |
| 642 | } |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 643 | if (!includeInfo && pel.userHeader().severity() == 0) |
| 644 | { |
| 645 | continue; |
| 646 | } |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 647 | std::bitset<16> actionFlags{pel.userHeader().actionFlags()}; |
| 648 | if (!hidden && actionFlags.test(hiddenFlagBit)) |
| 649 | { |
| 650 | continue; |
| 651 | } |
| 652 | if (pel.primarySRC() && scrubRegex) |
| 653 | { |
| 654 | std::string val = pel.primarySRC().value()->asciiString(); |
| 655 | if (std::regex_search(trimEnd(val), scrubRegex.value(), |
| 656 | std::regex_constants::match_not_null)) |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 657 | { |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 658 | continue; |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 659 | } |
| 660 | } |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 661 | count++; |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 662 | } |
| 663 | std::cout << "{\n" |
| 664 | << " \"Number of PELs found\": " |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 665 | << getNumberString("%d", count) << "\n}\n"; |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * @brief Generate regex pattern object from file contents |
| 670 | * @param[in] scrubFile - File containing regex pattern |
| 671 | * @return std::regex - SRC regex object |
| 672 | */ |
| 673 | std::regex genRegex(std::string& scrubFile) |
| 674 | { |
| 675 | std::string pattern; |
| 676 | std::ifstream contents(scrubFile); |
| 677 | if (contents.fail()) |
| 678 | { |
| 679 | std::cerr << "Can't open \"" << scrubFile << "\"\n"; |
| 680 | exit(1); |
| 681 | } |
| 682 | std::string line; |
| 683 | while (std::getline(contents, line)) |
| 684 | { |
| 685 | if (!line.empty()) |
| 686 | { |
| 687 | pattern.append(line + "|"); |
| 688 | } |
| 689 | } |
| 690 | try |
| 691 | { |
| 692 | std::regex scrubRegex(pattern, std::regex::icase); |
| 693 | return scrubRegex; |
| 694 | } |
| 695 | catch (std::regex_error& e) |
| 696 | { |
| 697 | if (e.code() == std::regex_constants::error_collate) |
| 698 | std::cerr << "Invalid collating element request\n"; |
| 699 | else if (e.code() == std::regex_constants::error_ctype) |
| 700 | std::cerr << "Invalid character class\n"; |
| 701 | else if (e.code() == std::regex_constants::error_escape) |
| 702 | std::cerr << "Invalid escape character or trailing escape\n"; |
| 703 | else if (e.code() == std::regex_constants::error_backref) |
| 704 | std::cerr << "Invalid back reference\n"; |
| 705 | else if (e.code() == std::regex_constants::error_brack) |
| 706 | std::cerr << "Mismatched bracket ([ or ])\n"; |
| 707 | else if (e.code() == std::regex_constants::error_paren) |
| 708 | { |
| 709 | // to catch return code error_badrepeat when error_paren is retured |
| 710 | // instead |
| 711 | size_t pos = pattern.find_first_of("*+?{"); |
| 712 | while (pos != std::string::npos) |
| 713 | { |
| 714 | if (pos == 0 || pattern.substr(pos - 1, 1) == "|") |
| 715 | { |
| 716 | std::cerr |
| 717 | << "A repetition character (*, ?, +, or {) was not " |
| 718 | "preceded by a valid regular expression\n"; |
| 719 | exit(1); |
| 720 | } |
| 721 | pos = pattern.find_first_of("*+?{", pos + 1); |
| 722 | } |
| 723 | std::cerr << "Mismatched parentheses (( or ))\n"; |
| 724 | } |
| 725 | else if (e.code() == std::regex_constants::error_brace) |
| 726 | std::cerr << "Mismatched brace ({ or })\n"; |
| 727 | else if (e.code() == std::regex_constants::error_badbrace) |
| 728 | std::cerr << "Invalid range inside a { }\n"; |
| 729 | else if (e.code() == std::regex_constants::error_range) |
| 730 | std::cerr << "Invalid character range (e.g., [z-a])\n"; |
| 731 | else if (e.code() == std::regex_constants::error_space) |
| 732 | std::cerr << "Insufficient memory to handle regular expression\n"; |
| 733 | else if (e.code() == std::regex_constants::error_badrepeat) |
| 734 | std::cerr << "A repetition character (*, ?, +, or {) was not " |
| 735 | "preceded by a valid regular expression\n"; |
| 736 | else if (e.code() == std::regex_constants::error_complexity) |
| 737 | std::cerr << "The requested match is too complex\n"; |
| 738 | else if (e.code() == std::regex_constants::error_stack) |
| 739 | std::cerr << "Insufficient memory to evaluate a match\n"; |
| 740 | exit(1); |
| 741 | } |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 742 | } |
| 743 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 744 | static void exitWithError(const std::string& help, const char* err) |
| 745 | { |
| 746 | std::cerr << "ERROR: " << err << std::endl << help << std::endl; |
| 747 | exit(-1); |
| 748 | } |
| 749 | |
| 750 | int main(int argc, char** argv) |
| 751 | { |
| 752 | CLI::App app{"OpenBMC PEL Tool"}; |
| 753 | std::string fileName; |
Aatir | e340c13 | 2019-12-09 14:19:29 -0600 | [diff] [blame] | 754 | std::string idPEL; |
Matt Spinler | 27de6f3 | 2020-02-21 08:35:57 -0600 | [diff] [blame] | 755 | std::string idToDelete; |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 756 | std::string scrubFile; |
| 757 | std::optional<std::regex> scrubRegex; |
Aatir | e340c13 | 2019-12-09 14:19:29 -0600 | [diff] [blame] | 758 | bool listPEL = false; |
| 759 | bool listPELDescOrd = false; |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 760 | bool hidden = false; |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 761 | bool includeInfo = false; |
Matt Spinler | 27de6f3 | 2020-02-21 08:35:57 -0600 | [diff] [blame] | 762 | bool deleteAll = false; |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 763 | bool showPELCount = false; |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 764 | bool fullPEL = false; |
Matt Spinler | 27de6f3 | 2020-02-21 08:35:57 -0600 | [diff] [blame] | 765 | |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 766 | app.set_help_flag("--help", "Print this help message and exit"); |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 767 | app.add_option("--file", fileName, "Display a PEL using its Raw PEL file"); |
Aatir | e340c13 | 2019-12-09 14:19:29 -0600 | [diff] [blame] | 768 | app.add_option("-i, --id", idPEL, "Display a PEL based on its ID"); |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 769 | app.add_flag("-a", fullPEL, "Display all PELs"); |
Aatir | bad5f8a | 2019-12-10 15:27:16 -0600 | [diff] [blame] | 770 | app.add_flag("-l", listPEL, "List PELs"); |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 771 | app.add_flag("-n", showPELCount, "Show number of PELs"); |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 772 | app.add_flag("-r", listPELDescOrd, "Reverse order of output"); |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 773 | app.add_flag("-h", hidden, "Include hidden PELs"); |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 774 | app.add_flag("-f,--info", includeInfo, "Include informational PELs"); |
Matt Spinler | 27de6f3 | 2020-02-21 08:35:57 -0600 | [diff] [blame] | 775 | app.add_option("-d, --delete", idToDelete, "Delete a PEL based on its ID"); |
| 776 | app.add_flag("-D, --delete-all", deleteAll, "Delete all PELs"); |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 777 | app.add_option("-s, --scrub", scrubFile, |
| 778 | "File containing SRC regular expressions to ignore"); |
Matt Spinler | 27de6f3 | 2020-02-21 08:35:57 -0600 | [diff] [blame] | 779 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 780 | CLI11_PARSE(app, argc, argv); |
| 781 | |
| 782 | if (!fileName.empty()) |
| 783 | { |
| 784 | std::vector<uint8_t> data = getFileData(fileName); |
| 785 | if (!data.empty()) |
| 786 | { |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 787 | auto plugins = getPlugins(); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 788 | PEL pel{data}; |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 789 | pel.toJSON(registry, plugins); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 790 | } |
| 791 | else |
| 792 | { |
| 793 | exitWithError(app.help("", CLI::AppFormatMode::All), |
| 794 | "Raw PEL file can't be read."); |
| 795 | } |
| 796 | } |
Aatir | e340c13 | 2019-12-09 14:19:29 -0600 | [diff] [blame] | 797 | else if (!idPEL.empty()) |
| 798 | { |
Matt Spinler | 1b420bc | 2020-02-21 08:54:25 -0600 | [diff] [blame] | 799 | callFunctionOnPEL(idPEL, displayPEL); |
Aatir | e340c13 | 2019-12-09 14:19:29 -0600 | [diff] [blame] | 800 | } |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 801 | else if (fullPEL || listPEL) |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 802 | { |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 803 | if (!scrubFile.empty()) |
| 804 | { |
| 805 | scrubRegex = genRegex(scrubFile); |
| 806 | } |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 807 | printPELs(listPELDescOrd, hidden, includeInfo, fullPEL, scrubRegex); |
Harisuddin Mohamed Isa | d4002f3 | 2020-02-26 16:19:58 +0800 | [diff] [blame] | 808 | } |
| 809 | else if (showPELCount) |
| 810 | { |
Harisuddin Mohamed Isa | 6f00519 | 2020-03-10 17:39:53 +0800 | [diff] [blame] | 811 | if (!scrubFile.empty()) |
| 812 | { |
| 813 | scrubRegex = genRegex(scrubFile); |
| 814 | } |
Harisuddin Mohamed Isa | 0c57a67 | 2020-03-27 14:16:28 +0800 | [diff] [blame] | 815 | printPELCount(hidden, includeInfo, scrubRegex); |
Aatir | 7b291ec | 2019-11-19 10:37:37 -0600 | [diff] [blame] | 816 | } |
Matt Spinler | 27de6f3 | 2020-02-21 08:35:57 -0600 | [diff] [blame] | 817 | else if (!idToDelete.empty()) |
| 818 | { |
| 819 | callFunctionOnPEL(idToDelete, deletePEL); |
| 820 | } |
| 821 | else if (deleteAll) |
| 822 | { |
| 823 | deleteAllPELs(); |
| 824 | } |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 825 | else |
| 826 | { |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 827 | std::cout << app.help("", CLI::AppFormatMode::All) << std::endl; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 828 | } |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 829 | Py_Finalize(); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 830 | return 0; |
| 831 | } |