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