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