blob: 063127f8983246711322e489f21190330d97574d [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
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 */
Aatir7b291ec2019-11-19 10:37:37 -060016#include "config.h"
17
Patrick Williams2544b412022-10-04 08:41:06 -050018#include "config_main.h"
19
Aatir7b291ec2019-11-19 10:37:37 -060020#include "../bcd_time.hpp"
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +080021#include "../json_utils.hpp"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080022#include "../paths.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050023#include "../pel.hpp"
Aatir7b291ec2019-11-19 10:37:37 -060024#include "../pel_types.hpp"
25#include "../pel_values.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050026
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +080027#include <Python.h>
28
Aatir186ce8c2019-10-20 15:13:39 -050029#include <CLI/CLI.hpp>
Patrick Williams2544b412022-10-04 08:41:06 -050030#include <phosphor-logging/log.hpp>
31
Aatir7b291ec2019-11-19 10:37:37 -060032#include <bitset>
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +080033#include <fstream>
Aatir186ce8c2019-10-20 15:13:39 -050034#include <iostream>
Aatir7b291ec2019-11-19 10:37:37 -060035#include <regex>
Aatir186ce8c2019-10-20 15:13:39 -050036#include <string>
37
Aatir7b291ec2019-11-19 10:37:37 -060038namespace fs = std::filesystem;
Matt Spinler7f1b9052022-03-22 09:18:58 -050039using namespace phosphor::logging;
Aatir186ce8c2019-10-20 15:13:39 -050040using namespace openpower::pels;
Aatir7b291ec2019-11-19 10:37:37 -060041namespace message = openpower::pels::message;
42namespace pv = openpower::pels::pel_values;
Aatir186ce8c2019-10-20 15:13:39 -050043
Miguel Gomez011ccae2021-03-25 23:42:09 +000044const uint8_t critSysTermSeverity = 0x51;
45
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +080046using PELFunc = std::function<void(const PEL&, bool hexDump)>;
Matt Spinler0d804ef2020-05-12 16:16:26 -050047message::Registry registry(getPELReadOnlyDataPath() / message::registryFileName,
Matt Spinlerd8e29002020-04-09 09:11:22 -050048 false);
Matt Spinler27de6f32020-02-21 08:35:57 -060049namespace service
50{
51constexpr auto logging = "xyz.openbmc_project.Logging";
52} // namespace service
53
54namespace interface
55{
56constexpr auto deleteObj = "xyz.openbmc_project.Object.Delete";
57constexpr auto deleteAll = "xyz.openbmc_project.Collection.DeleteAll";
58} // namespace interface
59
60namespace object_path
61{
62constexpr auto logEntry = "/xyz/openbmc_project/logging/entry/";
63constexpr auto logging = "/xyz/openbmc_project/logging";
64} // namespace object_path
65
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070066std::string pelLogDir()
67{
68 return std::string(EXTENSION_PERSIST_DIR) + "/pels/logs";
69}
70
Aatire340c132019-12-09 14:19:29 -060071/**
Aatir37822f62019-12-10 14:40:27 -060072 * @brief helper function to get PEL commit timestamp from file name
Sumit Kumar104c7962022-02-09 06:30:39 -060073 * @retrun uint64_t - PEL commit timestamp
Aatir37822f62019-12-10 14:40:27 -060074 * @param[in] std::string - file name
75 */
Sumit Kumar104c7962022-02-09 06:30:39 -060076uint64_t fileNameToTimestamp(const std::string& fileName)
Aatir37822f62019-12-10 14:40:27 -060077{
78 std::string token = fileName.substr(0, fileName.find("_"));
Sumit Kumar104c7962022-02-09 06:30:39 -060079 uint64_t bcdTime = 0;
Aatir37822f62019-12-10 14:40:27 -060080 if (token.length() >= 14)
81 {
Matt Spinlerbe952d22022-07-01 11:30:11 -050082 int i = 0;
83
Aatir37822f62019-12-10 14:40:27 -060084 try
85 {
Sumit Kumar104c7962022-02-09 06:30:39 -060086 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
87 bcdTime |= (static_cast<uint64_t>(tmp) << 56);
Aatir37822f62019-12-10 14:40:27 -060088 }
Patrick Williams66491c62021-10-06 12:23:37 -050089 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -060090 {
91 std::cout << "Conversion failure: " << err.what() << std::endl;
92 }
93 i += 2;
94 try
95 {
Sumit Kumar104c7962022-02-09 06:30:39 -060096 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
97 bcdTime |= (static_cast<uint64_t>(tmp) << 48);
Aatir37822f62019-12-10 14:40:27 -060098 }
Patrick Williams66491c62021-10-06 12:23:37 -050099 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600100 {
101 std::cout << "Conversion failure: " << err.what() << std::endl;
102 }
103 i += 2;
104 try
105 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600106 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
107 bcdTime |= (static_cast<uint64_t>(tmp) << 40);
Aatir37822f62019-12-10 14:40:27 -0600108 }
Patrick Williams66491c62021-10-06 12:23:37 -0500109 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600110 {
111 std::cout << "Conversion failure: " << err.what() << std::endl;
112 }
113 i += 2;
114 try
115 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600116 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
117 bcdTime |= (static_cast<uint64_t>(tmp) << 32);
Aatir37822f62019-12-10 14:40:27 -0600118 }
Patrick Williams66491c62021-10-06 12:23:37 -0500119 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600120 {
121 std::cout << "Conversion failure: " << err.what() << std::endl;
122 }
123 i += 2;
124 try
125 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600126 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
127 bcdTime |= (tmp << 24);
Aatir37822f62019-12-10 14:40:27 -0600128 }
Patrick Williams66491c62021-10-06 12:23:37 -0500129 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600130 {
131 std::cout << "Conversion failure: " << err.what() << std::endl;
132 }
133 i += 2;
134 try
135 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600136 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
137 bcdTime |= (tmp << 16);
Aatir37822f62019-12-10 14:40:27 -0600138 }
Patrick Williams66491c62021-10-06 12:23:37 -0500139 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600140 {
141 std::cout << "Conversion failure: " << err.what() << std::endl;
142 }
143 i += 2;
144 try
145 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600146 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
147 bcdTime |= (tmp << 8);
Aatir37822f62019-12-10 14:40:27 -0600148 }
Patrick Williams66491c62021-10-06 12:23:37 -0500149 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600150 {
151 std::cout << "Conversion failure: " << err.what() << std::endl;
152 }
153 i += 2;
154 try
155 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600156 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
157 bcdTime |= tmp;
Aatir37822f62019-12-10 14:40:27 -0600158 }
Patrick Williams66491c62021-10-06 12:23:37 -0500159 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600160 {
161 std::cout << "Conversion failure: " << err.what() << std::endl;
162 }
163 }
Sumit Kumar104c7962022-02-09 06:30:39 -0600164 return bcdTime;
Aatir37822f62019-12-10 14:40:27 -0600165}
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 */
172uint32_t fileNameToPELId(const std::string& fileName)
173{
174 uint32_t num = 0;
175 try
176 {
Sumit Kumara1e40842021-06-23 09:52:25 -0500177 num = std::stoul(fileName.substr(fileName.find("_") + 1), 0, 16);
Aatir37822f62019-12-10 14:40:27 -0600178 }
Patrick Williams66491c62021-10-06 12:23:37 -0500179 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600180 {
181 std::cout << "Conversion failure: " << err.what() << std::endl;
182 }
183 return num;
184}
185
186/**
Matt Spinler80129fe2023-01-19 15:09:05 -0600187 * @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
Aatire340c132019-12-09 14:19:29 -0600192 */
Matt Spinler80129fe2023-01-19 15:09:05 -0600193bool endsWithPelID(const std::string& str, const std::string& pelID)
Aatire340c132019-12-09 14:19:29 -0600194{
Matt Spinler80129fe2023-01-19 15:09:05 -0600195 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();
Aatire340c132019-12-09 14:19:29 -0600203 if (slen < elen)
204 return false;
205 while (elen)
206 {
Matt Spinler80129fe2023-01-19 15:09:05 -0600207 if (str[--slen] != pelID[--elen])
Aatire340c132019-12-09 14:19:29 -0600208 return false;
209 }
210 return true;
211}
212
Aatir37822f62019-12-10 14:40:27 -0600213/**
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 */
Aatirbad5f8a2019-12-10 15:27:16 -0600218std::vector<uint8_t> getFileData(const std::string& name)
Aatir37822f62019-12-10 14:40:27 -0600219{
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 {
Aatir37822f62019-12-10 14:40:27 -0600229 return {};
230 }
231}
232
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800233/**
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800234 * @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 */
241std::vector<std::string> getPlugins()
242{
243 Py_Initialize();
244 std::vector<std::string> plugins;
245 std::vector<std::string> siteDirs;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800246 std::array<std::string, 2> parserDirs = {"udparsers", "srcparsers"};
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800247 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 Isac8d6cc62020-08-19 22:47:19 +0800268 for (const auto& parserDir : parserDirs)
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800269 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800270 if (fs::exists(dir + "/" + parserDir))
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800271 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800272 for (const auto& entry :
273 fs::directory_iterator(dir + "/" + parserDir))
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800274 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800275 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 Isaf67bafd2020-07-06 17:51:21 +0800281 }
282 }
283 }
284 }
285 return plugins;
286}
287
288/**
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800289 * @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 Isa0c57a672020-03-27 14:16:28 +0800293 * @param[in] includeInfo - Boolean to include informational PELs
Miguel Gomez011ccae2021-03-25 23:42:09 +0000294 * @param[in] critSysTerm - Boolean to include critical error and system
295 * termination PELs
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800296 * @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 Isa6f005192020-03-10 17:39:53 +0800298 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800299 * @param[in] plugins - Vector of strings of plugins found in filesystem
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800300 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800301 * @return std::string - JSON string of PEL entry (empty if fullPEL is true)
302 */
Aatir7b291ec2019-11-19 10:37:37 -0600303template <typename T>
Miguel Gomez011ccae2021-03-25 23:42:09 +0000304std::string genPELJSON(T itr, bool hidden, bool includeInfo, bool critSysTerm,
305 bool fullPEL, bool& foundPEL,
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800306 const std::optional<std::regex>& scrubRegex,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500307 const std::vector<std::string>& plugins, bool hexDump,
308 bool archive)
Aatir7b291ec2019-11-19 10:37:37 -0600309{
Aatir7b291ec2019-11-19 10:37:37 -0600310 std::string val;
Aatir7b291ec2019-11-19 10:37:37 -0600311 std::string listStr;
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700312 char name[51];
Sumit Kumar104c7962022-02-09 06:30:39 -0600313 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 Kumar1d8835b2021-06-07 09:35:30 -0500323 auto fileName = (archive ? pelLogDir() + "/archive" : pelLogDir()) + name;
Aatir7b291ec2019-11-19 10:37:37 -0600324 try
325 {
Aatir37822f62019-12-10 14:40:27 -0600326 std::vector<uint8_t> data = getFileData(fileName);
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800327 if (data.empty())
Aatir37822f62019-12-10 14:40:27 -0600328 {
Matt Spinler7f1b9052022-03-22 09:18:58 -0500329 log<level::ERR>("Empty PEL file",
330 entry("FILENAME=%s", fileName.c_str()));
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800331 return listStr;
332 }
333 PEL pel{data};
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800334 if (!pel.valid())
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800335 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800336 return listStr;
337 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800338 if (!includeInfo && pel.userHeader().severity() == 0)
339 {
340 return listStr;
341 }
Miguel Gomez011ccae2021-03-25 23:42:09 +0000342 if (critSysTerm && pel.userHeader().severity() != critSysTermSeverity)
343 {
344 return listStr;
345 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800346 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 Isaa214ed32020-02-28 15:58:23 +0800356 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800357 return listStr;
358 }
359 }
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800360 if (hexDump)
361 {
362 std::cout << dumpHex(std::data(pel.data()), pel.size(), 0, false)
363 << std::endl;
364 }
365 else if (fullPEL)
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800366 {
367 if (!foundPEL)
368 {
369 std::cout << "[\n";
370 foundPEL = true;
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800371 }
372 else
Aatir7b291ec2019-11-19 10:37:37 -0600373 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800374 std::cout << ",\n\n";
375 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800376 pel.toJSON(registry, plugins);
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800377 }
378 else
379 {
380 // id
Matt Spinler3025d0b2021-06-02 15:12:52 -0600381 listStr += " \"" +
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800382 getNumberString("0x%X", pel.privateHeader().id()) +
383 "\": {\n";
384 // ASCII
385 if (pel.primarySRC())
386 {
387 val = pel.primarySRC().value()->asciiString();
Matt Spinler3025d0b2021-06-02 15:12:52 -0600388 jsonInsert(listStr, "SRC", trimEnd(val), 2);
389
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800390 // Registry message
391 auto regVal = pel.primarySRC().value()->getErrorDetails(
392 registry, DetailLevel::message, true);
393 if (regVal)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800394 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800395 val = regVal.value();
Matt Spinler3025d0b2021-06-02 15:12:52 -0600396 jsonInsert(listStr, "Message", val, 2);
Aatir37822f62019-12-10 14:40:27 -0600397 }
Aatir7b291ec2019-11-19 10:37:37 -0600398 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800399 else
400 {
Matt Spinler3025d0b2021-06-02 15:12:52 -0600401 jsonInsert(listStr, "SRC", "No SRC", 2);
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800402 }
Matt Spinler3025d0b2021-06-02 15:12:52 -0600403
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800404 // platformid
Matt Spinler3025d0b2021-06-02 15:12:52 -0600405 jsonInsert(listStr, "PLID",
406 getNumberString("0x%X", pel.privateHeader().plid()), 2);
407
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800408 // creatorid
409 std::string creatorID =
410 getNumberString("%c", pel.privateHeader().creatorID());
411 val = pv::creatorIDs.count(creatorID) ? pv::creatorIDs.at(creatorID)
412 : "Unknown Creator ID";
Matt Spinler3025d0b2021-06-02 15:12:52 -0600413 jsonInsert(listStr, "CreatorID", val, 2);
414
415 // subsystem
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800416 std::string subsystem = pv::getValue(pel.userHeader().subsystem(),
417 pel_values::subsystemValues);
Matt Spinler3025d0b2021-06-02 15:12:52 -0600418 jsonInsert(listStr, "Subsystem", subsystem, 2);
419
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800420 // commit time
Matt Spinlerbe952d22022-07-01 11:30:11 -0500421 char tmpValStr[50];
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800422 sprintf(tmpValStr, "%02X/%02X/%02X%02X %02X:%02X:%02X",
423 pel.privateHeader().commitTimestamp().month,
424 pel.privateHeader().commitTimestamp().day,
425 pel.privateHeader().commitTimestamp().yearMSB,
426 pel.privateHeader().commitTimestamp().yearLSB,
427 pel.privateHeader().commitTimestamp().hour,
428 pel.privateHeader().commitTimestamp().minutes,
429 pel.privateHeader().commitTimestamp().seconds);
Matt Spinler3025d0b2021-06-02 15:12:52 -0600430 jsonInsert(listStr, "Commit Time", tmpValStr, 2);
431
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800432 // severity
433 std::string severity = pv::getValue(pel.userHeader().severity(),
434 pel_values::severityValues);
Matt Spinler3025d0b2021-06-02 15:12:52 -0600435 jsonInsert(listStr, "Sev", severity, 2);
436
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800437 // compID
Matt Spinler9a808702023-03-28 09:39:26 -0500438 jsonInsert(
439 listStr, "CompID",
440 getComponentName(pel.privateHeader().header().componentID,
441 pel.privateHeader().creatorID()),
442 2);
Matt Spinler3025d0b2021-06-02 15:12:52 -0600443
Matt Spinlerbe952d22022-07-01 11:30:11 -0500444 auto found = listStr.rfind(",");
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800445 if (found != std::string::npos)
446 {
447 listStr.replace(found, 1, "");
Matt Spinler3025d0b2021-06-02 15:12:52 -0600448 listStr += " },\n";
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800449 }
450 foundPEL = true;
Aatir7b291ec2019-11-19 10:37:37 -0600451 }
452 }
Patrick Williams66491c62021-10-06 12:23:37 -0500453 catch (const std::exception& e)
Aatir7b291ec2019-11-19 10:37:37 -0600454 {
Matt Spinler7f1b9052022-03-22 09:18:58 -0500455 log<level::ERR>("Hit exception while reading PEL File",
456 entry("FILENAME=%s", fileName.c_str()),
457 entry("ERROR=%s", e.what()));
Aatir7b291ec2019-11-19 10:37:37 -0600458 }
459 return listStr;
460}
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800461
Aatir7b291ec2019-11-19 10:37:37 -0600462/**
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800463 * @brief Print a list of PELs or a JSON array of PELs
464 * @param[in] order - Boolean to print in reverse orser
465 * @param[in] hidden - Boolean to include hidden PELs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800466 * @param[in] includeInfo - Boolean to include informational PELs
Miguel Gomez011ccae2021-03-25 23:42:09 +0000467 * @param[in] critSysTerm - Boolean to include critical error and system
468 * termination PELs
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800469 * @param[in] fullPEL - Boolean to print full PEL into a JSON array
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800470 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800471 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Aatir7b291ec2019-11-19 10:37:37 -0600472 */
Miguel Gomez011ccae2021-03-25 23:42:09 +0000473void printPELs(bool order, bool hidden, bool includeInfo, bool critSysTerm,
474 bool fullPEL, const std::optional<std::regex>& scrubRegex,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500475 bool hexDump, bool archive = false)
Aatir7b291ec2019-11-19 10:37:37 -0600476{
477 std::string listStr;
Sumit Kumar104c7962022-02-09 06:30:39 -0600478 std::vector<std::pair<uint32_t, uint64_t>> PELs;
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800479 std::vector<std::string> plugins;
Aatir7b291ec2019-11-19 10:37:37 -0600480 listStr = "{\n";
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500481 for (auto it = (archive ? fs::directory_iterator(pelLogDir() + "/archive")
482 : fs::directory_iterator(pelLogDir()));
Aatir7b291ec2019-11-19 10:37:37 -0600483 it != fs::directory_iterator(); ++it)
484 {
485 if (!fs::is_regular_file((*it).path()))
486 {
487 continue;
488 }
Aatir37822f62019-12-10 14:40:27 -0600489 else
Aatir7b291ec2019-11-19 10:37:37 -0600490 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600491 PELs.emplace_back(fileNameToPELId((*it).path().filename()),
492 fileNameToTimestamp((*it).path().filename()));
Aatir7b291ec2019-11-19 10:37:37 -0600493 }
494 }
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500495
Sumit Kumar104c7962022-02-09 06:30:39 -0600496 // Sort the pairs based on second time parameter
Matt Spinlerbe952d22022-07-01 11:30:11 -0500497 std::sort(PELs.begin(), PELs.end(),
498 [](const auto& left, const auto& right) {
Patrick Williamsac1ba3f2023-05-10 07:50:16 -0500499 return left.second < right.second;
500 });
Sumit Kumar104c7962022-02-09 06:30:39 -0600501
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800502 bool foundPEL = false;
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800503
504 if (fullPEL && !hexDump)
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800505 {
506 plugins = getPlugins();
507 }
Miguel Gomez011ccae2021-03-25 23:42:09 +0000508 auto buildJSON = [&listStr, &hidden, &includeInfo, &critSysTerm, &fullPEL,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500509 &foundPEL, &scrubRegex, &plugins, &hexDump,
510 &archive](const auto& i) {
Miguel Gomez011ccae2021-03-25 23:42:09 +0000511 listStr += genPELJSON(i, hidden, includeInfo, critSysTerm, fullPEL,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500512 foundPEL, scrubRegex, plugins, hexDump, archive);
Aatir37822f62019-12-10 14:40:27 -0600513 };
Aatir7b291ec2019-11-19 10:37:37 -0600514 if (order)
515 {
516 std::for_each(PELs.rbegin(), PELs.rend(), buildJSON);
517 }
518 else
519 {
520 std::for_each(PELs.begin(), PELs.end(), buildJSON);
521 }
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800522 if (hexDump)
523 {
524 return;
525 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800526 if (foundPEL)
Aatir7b291ec2019-11-19 10:37:37 -0600527 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800528 if (fullPEL)
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800529 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800530 std::cout << "]" << std::endl;
531 }
532 else
533 {
534 std::size_t found;
535 found = listStr.rfind(",");
536 if (found != std::string::npos)
537 {
538 listStr.replace(found, 1, "");
539 listStr += "}\n";
540 printf("%s", listStr.c_str());
541 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800542 }
543 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800544 else
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800545 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800546 std::string emptyJSON = fullPEL ? "[]" : "{}";
547 std::cout << emptyJSON << std::endl;
Aatir7b291ec2019-11-19 10:37:37 -0600548 }
549}
Aatir186ce8c2019-10-20 15:13:39 -0500550
Matt Spinler27de6f32020-02-21 08:35:57 -0600551/**
552 * @brief Calls the function passed in on the PEL with the ID
553 * passed in.
554 *
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800555 * @param[in] id - The string version of the PEL or BMC Log ID, either with or
Matt Spinler27de6f32020-02-21 08:35:57 -0600556 * without the 0x prefix.
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800557 * @param[in] func - The std::function<void(const PEL&, bool hexDump)> function
558 * to run.
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800559 * @param[in] useBMC - if true, search by BMC Log ID, else search by PEL ID
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800560 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Matt Spinler27de6f32020-02-21 08:35:57 -0600561 */
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800562void callFunctionOnPEL(const std::string& id, const PELFunc& func,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500563 bool useBMC = false, bool hexDump = false,
564 bool archive = false)
Matt Spinler27de6f32020-02-21 08:35:57 -0600565{
566 std::string pelID{id};
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800567 if (!useBMC)
Matt Spinler27de6f32020-02-21 08:35:57 -0600568 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800569 std::transform(pelID.begin(), pelID.end(), pelID.begin(), toupper);
570
Matt Spinlerbe952d22022-07-01 11:30:11 -0500571 if (pelID.starts_with("0X"))
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800572 {
573 pelID.erase(0, 2);
574 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600575 }
576
577 bool found = false;
578
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500579 for (auto it = (archive ? fs::directory_iterator(pelLogDir() + "/archive")
580 : fs::directory_iterator(pelLogDir()));
Matt Spinler27de6f32020-02-21 08:35:57 -0600581 it != fs::directory_iterator(); ++it)
582 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800583 // The PEL ID is part of the filename, so use that to find the PEL if
584 // "useBMC" is set to false, otherwise we have to search within the PEL
Matt Spinler27de6f32020-02-21 08:35:57 -0600585
586 if (!fs::is_regular_file((*it).path()))
587 {
588 continue;
589 }
590
Matt Spinler80129fe2023-01-19 15:09:05 -0600591 if ((endsWithPelID((*it).path(), pelID) && !useBMC) || useBMC)
Matt Spinler27de6f32020-02-21 08:35:57 -0600592 {
Matt Spinler27de6f32020-02-21 08:35:57 -0600593 auto data = getFileData((*it).path());
594 if (!data.empty())
595 {
596 PEL pel{data};
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800597 if (!useBMC ||
598 (useBMC && pel.obmcLogID() == std::stoul(id, nullptr, 0)))
Matt Spinler27de6f32020-02-21 08:35:57 -0600599 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800600 found = true;
601 try
602 {
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800603 func(pel, hexDump);
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800604 break;
605 }
Patrick Williams66491c62021-10-06 12:23:37 -0500606 catch (const std::exception& e)
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800607 {
608 std::cerr << " Internal function threw an exception: "
609 << e.what() << "\n";
610 exit(1);
611 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600612 }
613 }
614 else
615 {
616 std::cerr << "Could not read PEL file\n";
617 exit(1);
618 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600619 }
620 }
621
622 if (!found)
623 {
624 std::cerr << "PEL not found\n";
625 exit(1);
626 }
627}
628
629/**
Matt Spinler25d986c2021-02-10 13:26:18 -0600630 * @brief Delete a PEL file.
Matt Spinler27de6f32020-02-21 08:35:57 -0600631 *
Matt Spinler25d986c2021-02-10 13:26:18 -0600632 * @param[in] id - The PEL ID to delete.
Matt Spinler27de6f32020-02-21 08:35:57 -0600633 */
Matt Spinler25d986c2021-02-10 13:26:18 -0600634void deletePEL(const std::string& id)
Matt Spinler27de6f32020-02-21 08:35:57 -0600635{
Matt Spinler25d986c2021-02-10 13:26:18 -0600636 std::string pelID{id};
Matt Spinler27de6f32020-02-21 08:35:57 -0600637
Matt Spinler25d986c2021-02-10 13:26:18 -0600638 std::transform(pelID.begin(), pelID.end(), pelID.begin(), toupper);
639
Matt Spinlerbe952d22022-07-01 11:30:11 -0500640 if (pelID.starts_with("0X"))
Matt Spinler27de6f32020-02-21 08:35:57 -0600641 {
Matt Spinler25d986c2021-02-10 13:26:18 -0600642 pelID.erase(0, 2);
Matt Spinler27de6f32020-02-21 08:35:57 -0600643 }
Matt Spinler25d986c2021-02-10 13:26:18 -0600644
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700645 for (auto it = fs::directory_iterator(pelLogDir());
Matt Spinler25d986c2021-02-10 13:26:18 -0600646 it != fs::directory_iterator(); ++it)
Matt Spinler27de6f32020-02-21 08:35:57 -0600647 {
Matt Spinler80129fe2023-01-19 15:09:05 -0600648 if (endsWithPelID((*it).path(), pelID))
Matt Spinler25d986c2021-02-10 13:26:18 -0600649 {
650 fs::remove((*it).path());
651 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600652 }
653}
654
655/**
Matt Spinler25d986c2021-02-10 13:26:18 -0600656 * @brief Delete all PEL files.
Matt Spinler27de6f32020-02-21 08:35:57 -0600657 */
658void deleteAllPELs()
659{
Matt Spinler7f1b9052022-03-22 09:18:58 -0500660 log<level::INFO>("peltool deleting all event logs");
Matt Spinler27de6f32020-02-21 08:35:57 -0600661
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700662 for (const auto& entry : fs::directory_iterator(pelLogDir()))
Matt Spinler27de6f32020-02-21 08:35:57 -0600663 {
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500664 if (!fs::is_regular_file(entry.path()))
665 {
666 continue;
667 }
Matt Spinler25d986c2021-02-10 13:26:18 -0600668 fs::remove(entry.path());
Matt Spinler27de6f32020-02-21 08:35:57 -0600669 }
670}
671
Matt Spinler1b420bc2020-02-21 08:54:25 -0600672/**
673 * @brief Display a single PEL
674 *
675 * @param[in] pel - the PEL to display
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800676 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Matt Spinler1b420bc2020-02-21 08:54:25 -0600677 */
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800678void displayPEL(const PEL& pel, bool hexDump)
Matt Spinler1b420bc2020-02-21 08:54:25 -0600679{
680 if (pel.valid())
681 {
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800682 if (hexDump)
683 {
Patrick Williams2544b412022-10-04 08:41:06 -0500684 std::string dstr = dumpHex(std::data(pel.data()), pel.size(), 0,
685 false);
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800686 std::cout << dstr << std::endl;
687 }
688 else
689 {
690 auto plugins = getPlugins();
691 pel.toJSON(registry, plugins);
692 }
Matt Spinler1b420bc2020-02-21 08:54:25 -0600693 }
694 else
695 {
696 std::cerr << "PEL was malformed\n";
697 exit(1);
698 }
699}
700
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800701/**
702 * @brief Print number of PELs
703 * @param[in] hidden - Bool to include hidden logs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800704 * @param[in] includeInfo - Bool to include informational logs
Miguel Gomez011ccae2021-03-25 23:42:09 +0000705 * @param[in] critSysTerm - Bool to include CritSysTerm
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800706 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800707 */
Miguel Gomez011ccae2021-03-25 23:42:09 +0000708void printPELCount(bool hidden, bool includeInfo, bool critSysTerm,
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800709 const std::optional<std::regex>& scrubRegex)
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800710{
711 std::size_t count = 0;
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700712
713 for (auto it = fs::directory_iterator(pelLogDir());
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800714 it != fs::directory_iterator(); ++it)
715 {
716 if (!fs::is_regular_file((*it).path()))
717 {
718 continue;
719 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800720 std::vector<uint8_t> data = getFileData((*it).path());
721 if (data.empty())
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800722 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800723 continue;
724 }
725 PEL pel{data};
726 if (!pel.valid())
727 {
728 continue;
729 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800730 if (!includeInfo && pel.userHeader().severity() == 0)
731 {
732 continue;
733 }
Miguel Gomez011ccae2021-03-25 23:42:09 +0000734 if (critSysTerm && pel.userHeader().severity() != critSysTermSeverity)
735 {
736 continue;
737 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800738 std::bitset<16> actionFlags{pel.userHeader().actionFlags()};
739 if (!hidden && actionFlags.test(hiddenFlagBit))
740 {
741 continue;
742 }
743 if (pel.primarySRC() && scrubRegex)
744 {
745 std::string val = pel.primarySRC().value()->asciiString();
746 if (std::regex_search(trimEnd(val), scrubRegex.value(),
747 std::regex_constants::match_not_null))
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800748 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800749 continue;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800750 }
751 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800752 count++;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800753 }
754 std::cout << "{\n"
755 << " \"Number of PELs found\": "
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800756 << getNumberString("%d", count) << "\n}\n";
757}
758
759/**
760 * @brief Generate regex pattern object from file contents
761 * @param[in] scrubFile - File containing regex pattern
762 * @return std::regex - SRC regex object
763 */
764std::regex genRegex(std::string& scrubFile)
765{
766 std::string pattern;
767 std::ifstream contents(scrubFile);
768 if (contents.fail())
769 {
770 std::cerr << "Can't open \"" << scrubFile << "\"\n";
771 exit(1);
772 }
773 std::string line;
774 while (std::getline(contents, line))
775 {
776 if (!line.empty())
777 {
778 pattern.append(line + "|");
779 }
780 }
781 try
782 {
783 std::regex scrubRegex(pattern, std::regex::icase);
784 return scrubRegex;
785 }
Patrick Williams66491c62021-10-06 12:23:37 -0500786 catch (const std::regex_error& e)
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800787 {
788 if (e.code() == std::regex_constants::error_collate)
789 std::cerr << "Invalid collating element request\n";
790 else if (e.code() == std::regex_constants::error_ctype)
791 std::cerr << "Invalid character class\n";
792 else if (e.code() == std::regex_constants::error_escape)
793 std::cerr << "Invalid escape character or trailing escape\n";
794 else if (e.code() == std::regex_constants::error_backref)
795 std::cerr << "Invalid back reference\n";
796 else if (e.code() == std::regex_constants::error_brack)
797 std::cerr << "Mismatched bracket ([ or ])\n";
798 else if (e.code() == std::regex_constants::error_paren)
799 {
800 // to catch return code error_badrepeat when error_paren is retured
801 // instead
802 size_t pos = pattern.find_first_of("*+?{");
803 while (pos != std::string::npos)
804 {
805 if (pos == 0 || pattern.substr(pos - 1, 1) == "|")
806 {
807 std::cerr
808 << "A repetition character (*, ?, +, or {) was not "
809 "preceded by a valid regular expression\n";
810 exit(1);
811 }
812 pos = pattern.find_first_of("*+?{", pos + 1);
813 }
814 std::cerr << "Mismatched parentheses (( or ))\n";
815 }
816 else if (e.code() == std::regex_constants::error_brace)
817 std::cerr << "Mismatched brace ({ or })\n";
818 else if (e.code() == std::regex_constants::error_badbrace)
819 std::cerr << "Invalid range inside a { }\n";
820 else if (e.code() == std::regex_constants::error_range)
821 std::cerr << "Invalid character range (e.g., [z-a])\n";
822 else if (e.code() == std::regex_constants::error_space)
823 std::cerr << "Insufficient memory to handle regular expression\n";
824 else if (e.code() == std::regex_constants::error_badrepeat)
825 std::cerr << "A repetition character (*, ?, +, or {) was not "
826 "preceded by a valid regular expression\n";
827 else if (e.code() == std::regex_constants::error_complexity)
828 std::cerr << "The requested match is too complex\n";
829 else if (e.code() == std::regex_constants::error_stack)
830 std::cerr << "Insufficient memory to evaluate a match\n";
831 exit(1);
832 }
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800833}
834
Aatir186ce8c2019-10-20 15:13:39 -0500835static void exitWithError(const std::string& help, const char* err)
836{
837 std::cerr << "ERROR: " << err << std::endl << help << std::endl;
838 exit(-1);
839}
840
841int main(int argc, char** argv)
842{
843 CLI::App app{"OpenBMC PEL Tool"};
844 std::string fileName;
Aatire340c132019-12-09 14:19:29 -0600845 std::string idPEL;
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800846 std::string bmcId;
Matt Spinler27de6f32020-02-21 08:35:57 -0600847 std::string idToDelete;
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800848 std::string scrubFile;
849 std::optional<std::regex> scrubRegex;
Aatire340c132019-12-09 14:19:29 -0600850 bool listPEL = false;
851 bool listPELDescOrd = false;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800852 bool hidden = false;
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800853 bool includeInfo = false;
Miguel Gomez011ccae2021-03-25 23:42:09 +0000854 bool critSysTerm = false;
Matt Spinler27de6f32020-02-21 08:35:57 -0600855 bool deleteAll = false;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800856 bool showPELCount = false;
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800857 bool fullPEL = false;
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800858 bool hexDump = false;
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500859 bool archive = false;
Matt Spinler27de6f32020-02-21 08:35:57 -0600860
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800861 app.set_help_flag("--help", "Print this help message and exit");
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800862 app.add_option("--file", fileName, "Display a PEL using its Raw PEL file");
Aatire340c132019-12-09 14:19:29 -0600863 app.add_option("-i, --id", idPEL, "Display a PEL based on its ID");
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800864 app.add_option("--bmc-id", bmcId,
865 "Display a PEL based on its BMC Event ID");
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800866 app.add_flag("-a", fullPEL, "Display all PELs");
Aatirbad5f8a2019-12-10 15:27:16 -0600867 app.add_flag("-l", listPEL, "List PELs");
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800868 app.add_flag("-n", showPELCount, "Show number of PELs");
Aatir7b291ec2019-11-19 10:37:37 -0600869 app.add_flag("-r", listPELDescOrd, "Reverse order of output");
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800870 app.add_flag("-h", hidden, "Include hidden PELs");
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800871 app.add_flag("-f,--info", includeInfo, "Include informational PELs");
Miguel Gomez011ccae2021-03-25 23:42:09 +0000872 app.add_flag("-t, --termination", critSysTerm,
873 "List only critical system terminating PELs");
Matt Spinler27de6f32020-02-21 08:35:57 -0600874 app.add_option("-d, --delete", idToDelete, "Delete a PEL based on its ID");
875 app.add_flag("-D, --delete-all", deleteAll, "Delete all PELs");
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800876 app.add_option("-s, --scrub", scrubFile,
877 "File containing SRC regular expressions to ignore");
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800878 app.add_flag("-x", hexDump, "Display PEL(s) in hexdump instead of JSON");
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500879 app.add_flag("--archive", archive, "List or display archived PELs");
Matt Spinler27de6f32020-02-21 08:35:57 -0600880
Aatir186ce8c2019-10-20 15:13:39 -0500881 CLI11_PARSE(app, argc, argv);
882
883 if (!fileName.empty())
884 {
885 std::vector<uint8_t> data = getFileData(fileName);
886 if (!data.empty())
887 {
888 PEL pel{data};
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800889 if (hexDump)
890 {
Patrick Williams2544b412022-10-04 08:41:06 -0500891 std::string dstr = dumpHex(std::data(pel.data()), pel.size(), 0,
892 false);
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800893 std::cout << dstr << std::endl;
894 }
895 else
896 {
897 auto plugins = getPlugins();
898 pel.toJSON(registry, plugins);
899 }
Aatir186ce8c2019-10-20 15:13:39 -0500900 }
901 else
902 {
903 exitWithError(app.help("", CLI::AppFormatMode::All),
904 "Raw PEL file can't be read.");
905 }
906 }
Aatire340c132019-12-09 14:19:29 -0600907 else if (!idPEL.empty())
908 {
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500909 callFunctionOnPEL(idPEL, displayPEL, false, hexDump, archive);
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800910 }
911 else if (!bmcId.empty())
912 {
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500913 callFunctionOnPEL(bmcId, displayPEL, true, hexDump, archive);
Aatire340c132019-12-09 14:19:29 -0600914 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800915 else if (fullPEL || listPEL)
Aatir7b291ec2019-11-19 10:37:37 -0600916 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800917 if (!scrubFile.empty())
918 {
919 scrubRegex = genRegex(scrubFile);
920 }
Miguel Gomez011ccae2021-03-25 23:42:09 +0000921 printPELs(listPELDescOrd, hidden, includeInfo, critSysTerm, fullPEL,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500922 scrubRegex, hexDump, archive);
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800923 }
924 else if (showPELCount)
925 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800926 if (!scrubFile.empty())
927 {
928 scrubRegex = genRegex(scrubFile);
929 }
Miguel Gomez011ccae2021-03-25 23:42:09 +0000930 printPELCount(hidden, includeInfo, critSysTerm, scrubRegex);
Aatir7b291ec2019-11-19 10:37:37 -0600931 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600932 else if (!idToDelete.empty())
933 {
Matt Spinler25d986c2021-02-10 13:26:18 -0600934 deletePEL(idToDelete);
Matt Spinler27de6f32020-02-21 08:35:57 -0600935 }
936 else if (deleteAll)
937 {
938 deleteAllPELs();
939 }
Aatir186ce8c2019-10-20 15:13:39 -0500940 else
941 {
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800942 std::cout << app.help("", CLI::AppFormatMode::All) << std::endl;
Aatir186ce8c2019-10-20 15:13:39 -0500943 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800944 Py_Finalize();
Aatir186ce8c2019-10-20 15:13:39 -0500945 return 0;
946}