blob: a45be58bfed914a63fa1ec35166820bd4aa7375e [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
18#include "../bcd_time.hpp"
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +080019#include "../json_utils.hpp"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080020#include "../paths.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050021#include "../pel.hpp"
Aatir7b291ec2019-11-19 10:37:37 -060022#include "../pel_types.hpp"
23#include "../pel_values.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050024
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +080025#include <Python.h>
26
Aatir186ce8c2019-10-20 15:13:39 -050027#include <CLI/CLI.hpp>
Aatir7b291ec2019-11-19 10:37:37 -060028#include <bitset>
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +080029#include <fstream>
Aatir186ce8c2019-10-20 15:13:39 -050030#include <iostream>
Matt Spinler7f1b9052022-03-22 09:18:58 -050031#include <phosphor-logging/log.hpp>
Aatir7b291ec2019-11-19 10:37:37 -060032#include <regex>
Aatir186ce8c2019-10-20 15:13:39 -050033#include <string>
34
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070035#include "config_main.h"
36
Aatir7b291ec2019-11-19 10:37:37 -060037namespace fs = std::filesystem;
Matt Spinler7f1b9052022-03-22 09:18:58 -050038using namespace phosphor::logging;
Aatir186ce8c2019-10-20 15:13:39 -050039using namespace openpower::pels;
Aatir7b291ec2019-11-19 10:37:37 -060040namespace message = openpower::pels::message;
41namespace pv = openpower::pels::pel_values;
Aatir186ce8c2019-10-20 15:13:39 -050042
Miguel Gomez011ccae2021-03-25 23:42:09 +000043const uint8_t critSysTermSeverity = 0x51;
44
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +080045using PELFunc = std::function<void(const PEL&, bool hexDump)>;
Matt Spinler0d804ef2020-05-12 16:16:26 -050046message::Registry registry(getPELReadOnlyDataPath() / message::registryFileName,
Matt Spinlerd8e29002020-04-09 09:11:22 -050047 false);
Matt Spinler27de6f32020-02-21 08:35:57 -060048namespace service
49{
50constexpr auto logging = "xyz.openbmc_project.Logging";
51} // namespace service
52
53namespace interface
54{
55constexpr auto deleteObj = "xyz.openbmc_project.Object.Delete";
56constexpr auto deleteAll = "xyz.openbmc_project.Collection.DeleteAll";
57} // namespace interface
58
59namespace object_path
60{
61constexpr auto logEntry = "/xyz/openbmc_project/logging/entry/";
62constexpr auto logging = "/xyz/openbmc_project/logging";
63} // namespace object_path
64
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070065std::string pelLogDir()
66{
67 return std::string(EXTENSION_PERSIST_DIR) + "/pels/logs";
68}
69
Aatire340c132019-12-09 14:19:29 -060070/**
Aatir37822f62019-12-10 14:40:27 -060071 * @brief helper function to get PEL commit timestamp from file name
Sumit Kumar104c7962022-02-09 06:30:39 -060072 * @retrun uint64_t - PEL commit timestamp
Aatir37822f62019-12-10 14:40:27 -060073 * @param[in] std::string - file name
74 */
Sumit Kumar104c7962022-02-09 06:30:39 -060075uint64_t fileNameToTimestamp(const std::string& fileName)
Aatir37822f62019-12-10 14:40:27 -060076{
77 std::string token = fileName.substr(0, fileName.find("_"));
Sumit Kumar104c7962022-02-09 06:30:39 -060078 uint64_t bcdTime = 0;
Aatir37822f62019-12-10 14:40:27 -060079 if (token.length() >= 14)
80 {
Matt Spinlerbe952d22022-07-01 11:30:11 -050081 int i = 0;
82
Aatir37822f62019-12-10 14:40:27 -060083 try
84 {
Sumit Kumar104c7962022-02-09 06:30:39 -060085 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
86 bcdTime |= (static_cast<uint64_t>(tmp) << 56);
Aatir37822f62019-12-10 14:40:27 -060087 }
Patrick Williams66491c62021-10-06 12:23:37 -050088 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -060089 {
90 std::cout << "Conversion failure: " << err.what() << std::endl;
91 }
92 i += 2;
93 try
94 {
Sumit Kumar104c7962022-02-09 06:30:39 -060095 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
96 bcdTime |= (static_cast<uint64_t>(tmp) << 48);
Aatir37822f62019-12-10 14:40:27 -060097 }
Patrick Williams66491c62021-10-06 12:23:37 -050098 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -060099 {
100 std::cout << "Conversion failure: " << err.what() << std::endl;
101 }
102 i += 2;
103 try
104 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600105 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
106 bcdTime |= (static_cast<uint64_t>(tmp) << 40);
Aatir37822f62019-12-10 14:40:27 -0600107 }
Patrick Williams66491c62021-10-06 12:23:37 -0500108 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600109 {
110 std::cout << "Conversion failure: " << err.what() << std::endl;
111 }
112 i += 2;
113 try
114 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600115 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
116 bcdTime |= (static_cast<uint64_t>(tmp) << 32);
Aatir37822f62019-12-10 14:40:27 -0600117 }
Patrick Williams66491c62021-10-06 12:23:37 -0500118 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600119 {
120 std::cout << "Conversion failure: " << err.what() << std::endl;
121 }
122 i += 2;
123 try
124 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600125 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
126 bcdTime |= (tmp << 24);
Aatir37822f62019-12-10 14:40:27 -0600127 }
Patrick Williams66491c62021-10-06 12:23:37 -0500128 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600129 {
130 std::cout << "Conversion failure: " << err.what() << std::endl;
131 }
132 i += 2;
133 try
134 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600135 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
136 bcdTime |= (tmp << 16);
Aatir37822f62019-12-10 14:40:27 -0600137 }
Patrick Williams66491c62021-10-06 12:23:37 -0500138 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600139 {
140 std::cout << "Conversion failure: " << err.what() << std::endl;
141 }
142 i += 2;
143 try
144 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600145 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
146 bcdTime |= (tmp << 8);
Aatir37822f62019-12-10 14:40:27 -0600147 }
Patrick Williams66491c62021-10-06 12:23:37 -0500148 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600149 {
150 std::cout << "Conversion failure: " << err.what() << std::endl;
151 }
152 i += 2;
153 try
154 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600155 auto tmp = std::stoul(token.substr(i, 2), 0, 16);
156 bcdTime |= tmp;
Aatir37822f62019-12-10 14:40:27 -0600157 }
Patrick Williams66491c62021-10-06 12:23:37 -0500158 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600159 {
160 std::cout << "Conversion failure: " << err.what() << std::endl;
161 }
162 }
Sumit Kumar104c7962022-02-09 06:30:39 -0600163 return bcdTime;
Aatir37822f62019-12-10 14:40:27 -0600164}
165
166/**
167 * @brief helper function to get PEL id from file name
168 * @retrun uint32_t - PEL id
169 * @param[in] std::string - file name
170 */
171uint32_t fileNameToPELId(const std::string& fileName)
172{
173 uint32_t num = 0;
174 try
175 {
Sumit Kumara1e40842021-06-23 09:52:25 -0500176 num = std::stoul(fileName.substr(fileName.find("_") + 1), 0, 16);
Aatir37822f62019-12-10 14:40:27 -0600177 }
Patrick Williams66491c62021-10-06 12:23:37 -0500178 catch (const std::exception& err)
Aatir37822f62019-12-10 14:40:27 -0600179 {
180 std::cout << "Conversion failure: " << err.what() << std::endl;
181 }
182 return num;
183}
184
185/**
Aatire340c132019-12-09 14:19:29 -0600186 * @brief helper function to check string suffix
187 * @retrun bool - true with suffix matches
188 * @param[in] std::string - string to check for suffix
189 * @param[in] std::string - suffix string
190 */
191bool ends_with(const std::string& str, const std::string& end)
192{
193 size_t slen = str.size(), elen = end.size();
194 if (slen < elen)
195 return false;
196 while (elen)
197 {
198 if (str[--slen] != end[--elen])
199 return false;
200 }
201 return true;
202}
203
Aatir37822f62019-12-10 14:40:27 -0600204/**
205 * @brief get data form raw PEL file.
206 * @param[in] std::string Name of file with raw PEL
207 * @return std::vector<uint8_t> char vector read from raw PEL file.
208 */
Aatirbad5f8a2019-12-10 15:27:16 -0600209std::vector<uint8_t> getFileData(const std::string& name)
Aatir37822f62019-12-10 14:40:27 -0600210{
211 std::ifstream file(name, std::ifstream::in);
212 if (file.good())
213 {
214 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
215 std::istreambuf_iterator<char>()};
216 return data;
217 }
218 else
219 {
Aatir37822f62019-12-10 14:40:27 -0600220 return {};
221 }
222}
223
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800224/**
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800225 * @brief Initialize Python interpreter and gather all UD parser modules under
226 * the paths found in Python sys.path and the current user directory.
227 * This is to prevent calling a non-existant module which causes Python
228 * to print an import error message and breaking JSON output.
229 *
230 * @return std::vector<std::string> Vector of plugins found in filesystem
231 */
232std::vector<std::string> getPlugins()
233{
234 Py_Initialize();
235 std::vector<std::string> plugins;
236 std::vector<std::string> siteDirs;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800237 std::array<std::string, 2> parserDirs = {"udparsers", "srcparsers"};
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800238 PyObject* pName = PyUnicode_FromString("sys");
239 PyObject* pModule = PyImport_Import(pName);
240 Py_XDECREF(pName);
241 PyObject* pDict = PyModule_GetDict(pModule);
242 Py_XDECREF(pModule);
243 PyObject* pResult = PyDict_GetItemString(pDict, "path");
244 PyObject* pValue = PyUnicode_FromString(".");
245 PyList_Append(pResult, pValue);
246 Py_XDECREF(pValue);
247 auto list_size = PyList_Size(pResult);
248 for (auto i = 0; i < list_size; i++)
249 {
250 PyObject* item = PyList_GetItem(pResult, i);
251 PyObject* pBytes = PyUnicode_AsEncodedString(item, "utf-8", "~E~");
252 const char* output = PyBytes_AS_STRING(pBytes);
253 Py_XDECREF(pBytes);
254 std::string tmpStr(output);
255 siteDirs.push_back(tmpStr);
256 }
257 for (const auto& dir : siteDirs)
258 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800259 for (const auto& parserDir : parserDirs)
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800260 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800261 if (fs::exists(dir + "/" + parserDir))
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800262 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800263 for (const auto& entry :
264 fs::directory_iterator(dir + "/" + parserDir))
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800265 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800266 if (entry.is_directory() and
267 fs::exists(entry.path().string() + "/" +
268 entry.path().stem().string() + ".py"))
269 {
270 plugins.push_back(entry.path().stem());
271 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800272 }
273 }
274 }
275 }
276 return plugins;
277}
278
279/**
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800280 * @brief Creates JSON string of a PEL entry if fullPEL is false or prints to
281 * stdout the full PEL in JSON if fullPEL is true
282 * @param[in] itr - std::map iterator of <uint32_t, BCDTime>
283 * @param[in] hidden - Boolean to include hidden PELs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800284 * @param[in] includeInfo - Boolean to include informational PELs
Miguel Gomez011ccae2021-03-25 23:42:09 +0000285 * @param[in] critSysTerm - Boolean to include critical error and system
286 * termination PELs
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800287 * @param[in] fullPEL - Boolean to print full JSON representation of PEL
288 * @param[in] foundPEL - Boolean to check if any PEL is present
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800289 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800290 * @param[in] plugins - Vector of strings of plugins found in filesystem
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800291 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800292 * @return std::string - JSON string of PEL entry (empty if fullPEL is true)
293 */
Aatir7b291ec2019-11-19 10:37:37 -0600294template <typename T>
Miguel Gomez011ccae2021-03-25 23:42:09 +0000295std::string genPELJSON(T itr, bool hidden, bool includeInfo, bool critSysTerm,
296 bool fullPEL, bool& foundPEL,
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800297 const std::optional<std::regex>& scrubRegex,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500298 const std::vector<std::string>& plugins, bool hexDump,
299 bool archive)
Aatir7b291ec2019-11-19 10:37:37 -0600300{
Aatir7b291ec2019-11-19 10:37:37 -0600301 std::string val;
Aatir7b291ec2019-11-19 10:37:37 -0600302 std::string listStr;
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700303 char name[51];
Sumit Kumar104c7962022-02-09 06:30:39 -0600304 sprintf(name, "/%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X",
305 static_cast<uint8_t>((itr.second >> 56) & 0xFF),
306 static_cast<uint8_t>((itr.second >> 48) & 0xFF),
307 static_cast<uint8_t>((itr.second >> 40) & 0xFF),
308 static_cast<uint8_t>((itr.second >> 32) & 0xFF),
309 static_cast<uint8_t>((itr.second >> 24) & 0xFF),
310 static_cast<uint8_t>((itr.second >> 16) & 0xFF),
311 static_cast<uint8_t>((itr.second >> 8) & 0xFF),
312 static_cast<uint8_t>(itr.second & 0xFF), itr.first);
313
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500314 auto fileName = (archive ? pelLogDir() + "/archive" : pelLogDir()) + name;
Aatir7b291ec2019-11-19 10:37:37 -0600315 try
316 {
Aatir37822f62019-12-10 14:40:27 -0600317 std::vector<uint8_t> data = getFileData(fileName);
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800318 if (data.empty())
Aatir37822f62019-12-10 14:40:27 -0600319 {
Matt Spinler7f1b9052022-03-22 09:18:58 -0500320 log<level::ERR>("Empty PEL file",
321 entry("FILENAME=%s", fileName.c_str()));
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800322 return listStr;
323 }
324 PEL pel{data};
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800325 if (!pel.valid())
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800326 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800327 return listStr;
328 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800329 if (!includeInfo && pel.userHeader().severity() == 0)
330 {
331 return listStr;
332 }
Miguel Gomez011ccae2021-03-25 23:42:09 +0000333 if (critSysTerm && pel.userHeader().severity() != critSysTermSeverity)
334 {
335 return listStr;
336 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800337 std::bitset<16> actionFlags{pel.userHeader().actionFlags()};
338 if (!hidden && actionFlags.test(hiddenFlagBit))
339 {
340 return listStr;
341 }
342 if (pel.primarySRC() && scrubRegex)
343 {
344 val = pel.primarySRC().value()->asciiString();
345 if (std::regex_search(trimEnd(val), scrubRegex.value(),
346 std::regex_constants::match_not_null))
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800347 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800348 return listStr;
349 }
350 }
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800351 if (hexDump)
352 {
353 std::cout << dumpHex(std::data(pel.data()), pel.size(), 0, false)
354 << std::endl;
355 }
356 else if (fullPEL)
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800357 {
358 if (!foundPEL)
359 {
360 std::cout << "[\n";
361 foundPEL = true;
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800362 }
363 else
Aatir7b291ec2019-11-19 10:37:37 -0600364 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800365 std::cout << ",\n\n";
366 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800367 pel.toJSON(registry, plugins);
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800368 }
369 else
370 {
371 // id
Matt Spinler3025d0b2021-06-02 15:12:52 -0600372 listStr += " \"" +
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800373 getNumberString("0x%X", pel.privateHeader().id()) +
374 "\": {\n";
375 // ASCII
376 if (pel.primarySRC())
377 {
378 val = pel.primarySRC().value()->asciiString();
Matt Spinler3025d0b2021-06-02 15:12:52 -0600379 jsonInsert(listStr, "SRC", trimEnd(val), 2);
380
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800381 // Registry message
382 auto regVal = pel.primarySRC().value()->getErrorDetails(
383 registry, DetailLevel::message, true);
384 if (regVal)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800385 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800386 val = regVal.value();
Matt Spinler3025d0b2021-06-02 15:12:52 -0600387 jsonInsert(listStr, "Message", val, 2);
Aatir37822f62019-12-10 14:40:27 -0600388 }
Aatir7b291ec2019-11-19 10:37:37 -0600389 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800390 else
391 {
Matt Spinler3025d0b2021-06-02 15:12:52 -0600392 jsonInsert(listStr, "SRC", "No SRC", 2);
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800393 }
Matt Spinler3025d0b2021-06-02 15:12:52 -0600394
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800395 // platformid
Matt Spinler3025d0b2021-06-02 15:12:52 -0600396 jsonInsert(listStr, "PLID",
397 getNumberString("0x%X", pel.privateHeader().plid()), 2);
398
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800399 // creatorid
400 std::string creatorID =
401 getNumberString("%c", pel.privateHeader().creatorID());
402 val = pv::creatorIDs.count(creatorID) ? pv::creatorIDs.at(creatorID)
403 : "Unknown Creator ID";
Matt Spinler3025d0b2021-06-02 15:12:52 -0600404 jsonInsert(listStr, "CreatorID", val, 2);
405
406 // subsystem
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800407 std::string subsystem = pv::getValue(pel.userHeader().subsystem(),
408 pel_values::subsystemValues);
Matt Spinler3025d0b2021-06-02 15:12:52 -0600409 jsonInsert(listStr, "Subsystem", subsystem, 2);
410
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800411 // commit time
Matt Spinlerbe952d22022-07-01 11:30:11 -0500412 char tmpValStr[50];
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800413 sprintf(tmpValStr, "%02X/%02X/%02X%02X %02X:%02X:%02X",
414 pel.privateHeader().commitTimestamp().month,
415 pel.privateHeader().commitTimestamp().day,
416 pel.privateHeader().commitTimestamp().yearMSB,
417 pel.privateHeader().commitTimestamp().yearLSB,
418 pel.privateHeader().commitTimestamp().hour,
419 pel.privateHeader().commitTimestamp().minutes,
420 pel.privateHeader().commitTimestamp().seconds);
Matt Spinler3025d0b2021-06-02 15:12:52 -0600421 jsonInsert(listStr, "Commit Time", tmpValStr, 2);
422
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800423 // severity
424 std::string severity = pv::getValue(pel.userHeader().severity(),
425 pel_values::severityValues);
Matt Spinler3025d0b2021-06-02 15:12:52 -0600426 jsonInsert(listStr, "Sev", severity, 2);
427
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800428 // compID
Matt Spinler3025d0b2021-06-02 15:12:52 -0600429 jsonInsert(listStr, "CompID",
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800430 getNumberString(
Matt Spinler3025d0b2021-06-02 15:12:52 -0600431 "0x%X", pel.privateHeader().header().componentID),
432 2);
433
Matt Spinlerbe952d22022-07-01 11:30:11 -0500434 auto found = listStr.rfind(",");
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800435 if (found != std::string::npos)
436 {
437 listStr.replace(found, 1, "");
Matt Spinler3025d0b2021-06-02 15:12:52 -0600438 listStr += " },\n";
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800439 }
440 foundPEL = true;
Aatir7b291ec2019-11-19 10:37:37 -0600441 }
442 }
Patrick Williams66491c62021-10-06 12:23:37 -0500443 catch (const std::exception& e)
Aatir7b291ec2019-11-19 10:37:37 -0600444 {
Matt Spinler7f1b9052022-03-22 09:18:58 -0500445 log<level::ERR>("Hit exception while reading PEL File",
446 entry("FILENAME=%s", fileName.c_str()),
447 entry("ERROR=%s", e.what()));
Aatir7b291ec2019-11-19 10:37:37 -0600448 }
449 return listStr;
450}
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800451
Aatir7b291ec2019-11-19 10:37:37 -0600452/**
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800453 * @brief Print a list of PELs or a JSON array of PELs
454 * @param[in] order - Boolean to print in reverse orser
455 * @param[in] hidden - Boolean to include hidden PELs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800456 * @param[in] includeInfo - Boolean to include informational PELs
Miguel Gomez011ccae2021-03-25 23:42:09 +0000457 * @param[in] critSysTerm - Boolean to include critical error and system
458 * termination PELs
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800459 * @param[in] fullPEL - Boolean to print full PEL into a JSON array
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800460 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800461 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Aatir7b291ec2019-11-19 10:37:37 -0600462 */
Miguel Gomez011ccae2021-03-25 23:42:09 +0000463void printPELs(bool order, bool hidden, bool includeInfo, bool critSysTerm,
464 bool fullPEL, const std::optional<std::regex>& scrubRegex,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500465 bool hexDump, bool archive = false)
Aatir7b291ec2019-11-19 10:37:37 -0600466{
467 std::string listStr;
Sumit Kumar104c7962022-02-09 06:30:39 -0600468 std::vector<std::pair<uint32_t, uint64_t>> PELs;
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800469 std::vector<std::string> plugins;
Aatir7b291ec2019-11-19 10:37:37 -0600470 listStr = "{\n";
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500471 for (auto it = (archive ? fs::directory_iterator(pelLogDir() + "/archive")
472 : fs::directory_iterator(pelLogDir()));
Aatir7b291ec2019-11-19 10:37:37 -0600473 it != fs::directory_iterator(); ++it)
474 {
475 if (!fs::is_regular_file((*it).path()))
476 {
477 continue;
478 }
Aatir37822f62019-12-10 14:40:27 -0600479 else
Aatir7b291ec2019-11-19 10:37:37 -0600480 {
Sumit Kumar104c7962022-02-09 06:30:39 -0600481 PELs.emplace_back(fileNameToPELId((*it).path().filename()),
482 fileNameToTimestamp((*it).path().filename()));
Aatir7b291ec2019-11-19 10:37:37 -0600483 }
484 }
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500485
Sumit Kumar104c7962022-02-09 06:30:39 -0600486 // Sort the pairs based on second time parameter
Matt Spinlerbe952d22022-07-01 11:30:11 -0500487 std::sort(PELs.begin(), PELs.end(),
488 [](const auto& left, const auto& right) {
489 return left.second < right.second;
490 });
Sumit Kumar104c7962022-02-09 06:30:39 -0600491
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800492 bool foundPEL = false;
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800493
494 if (fullPEL && !hexDump)
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800495 {
496 plugins = getPlugins();
497 }
Miguel Gomez011ccae2021-03-25 23:42:09 +0000498 auto buildJSON = [&listStr, &hidden, &includeInfo, &critSysTerm, &fullPEL,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500499 &foundPEL, &scrubRegex, &plugins, &hexDump,
500 &archive](const auto& i) {
Miguel Gomez011ccae2021-03-25 23:42:09 +0000501 listStr += genPELJSON(i, hidden, includeInfo, critSysTerm, fullPEL,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500502 foundPEL, scrubRegex, plugins, hexDump, archive);
Aatir37822f62019-12-10 14:40:27 -0600503 };
Aatir7b291ec2019-11-19 10:37:37 -0600504 if (order)
505 {
506 std::for_each(PELs.rbegin(), PELs.rend(), buildJSON);
507 }
508 else
509 {
510 std::for_each(PELs.begin(), PELs.end(), buildJSON);
511 }
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800512 if (hexDump)
513 {
514 return;
515 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800516 if (foundPEL)
Aatir7b291ec2019-11-19 10:37:37 -0600517 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800518 if (fullPEL)
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800519 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800520 std::cout << "]" << std::endl;
521 }
522 else
523 {
524 std::size_t found;
525 found = listStr.rfind(",");
526 if (found != std::string::npos)
527 {
528 listStr.replace(found, 1, "");
529 listStr += "}\n";
530 printf("%s", listStr.c_str());
531 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800532 }
533 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800534 else
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800535 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800536 std::string emptyJSON = fullPEL ? "[]" : "{}";
537 std::cout << emptyJSON << std::endl;
Aatir7b291ec2019-11-19 10:37:37 -0600538 }
539}
Aatir186ce8c2019-10-20 15:13:39 -0500540
Matt Spinler27de6f32020-02-21 08:35:57 -0600541/**
542 * @brief Calls the function passed in on the PEL with the ID
543 * passed in.
544 *
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800545 * @param[in] id - The string version of the PEL or BMC Log ID, either with or
Matt Spinler27de6f32020-02-21 08:35:57 -0600546 * without the 0x prefix.
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800547 * @param[in] func - The std::function<void(const PEL&, bool hexDump)> function
548 * to run.
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800549 * @param[in] useBMC - if true, search by BMC Log ID, else search by PEL ID
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800550 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Matt Spinler27de6f32020-02-21 08:35:57 -0600551 */
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800552void callFunctionOnPEL(const std::string& id, const PELFunc& func,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500553 bool useBMC = false, bool hexDump = false,
554 bool archive = false)
Matt Spinler27de6f32020-02-21 08:35:57 -0600555{
556 std::string pelID{id};
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800557 if (!useBMC)
Matt Spinler27de6f32020-02-21 08:35:57 -0600558 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800559 std::transform(pelID.begin(), pelID.end(), pelID.begin(), toupper);
560
Matt Spinlerbe952d22022-07-01 11:30:11 -0500561 if (pelID.starts_with("0X"))
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800562 {
563 pelID.erase(0, 2);
564 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600565 }
566
567 bool found = false;
568
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500569 for (auto it = (archive ? fs::directory_iterator(pelLogDir() + "/archive")
570 : fs::directory_iterator(pelLogDir()));
Matt Spinler27de6f32020-02-21 08:35:57 -0600571 it != fs::directory_iterator(); ++it)
572 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800573 // The PEL ID is part of the filename, so use that to find the PEL if
574 // "useBMC" is set to false, otherwise we have to search within the PEL
Matt Spinler27de6f32020-02-21 08:35:57 -0600575
576 if (!fs::is_regular_file((*it).path()))
577 {
578 continue;
579 }
580
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800581 if ((ends_with((*it).path(), pelID) && !useBMC) || useBMC)
Matt Spinler27de6f32020-02-21 08:35:57 -0600582 {
Matt Spinler27de6f32020-02-21 08:35:57 -0600583 auto data = getFileData((*it).path());
584 if (!data.empty())
585 {
586 PEL pel{data};
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800587 if (!useBMC ||
588 (useBMC && pel.obmcLogID() == std::stoul(id, nullptr, 0)))
Matt Spinler27de6f32020-02-21 08:35:57 -0600589 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800590 found = true;
591 try
592 {
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800593 func(pel, hexDump);
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800594 break;
595 }
Patrick Williams66491c62021-10-06 12:23:37 -0500596 catch (const std::exception& e)
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800597 {
598 std::cerr << " Internal function threw an exception: "
599 << e.what() << "\n";
600 exit(1);
601 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600602 }
603 }
604 else
605 {
606 std::cerr << "Could not read PEL file\n";
607 exit(1);
608 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600609 }
610 }
611
612 if (!found)
613 {
614 std::cerr << "PEL not found\n";
615 exit(1);
616 }
617}
618
619/**
Matt Spinler25d986c2021-02-10 13:26:18 -0600620 * @brief Delete a PEL file.
Matt Spinler27de6f32020-02-21 08:35:57 -0600621 *
Matt Spinler25d986c2021-02-10 13:26:18 -0600622 * @param[in] id - The PEL ID to delete.
Matt Spinler27de6f32020-02-21 08:35:57 -0600623 */
Matt Spinler25d986c2021-02-10 13:26:18 -0600624void deletePEL(const std::string& id)
Matt Spinler27de6f32020-02-21 08:35:57 -0600625{
Matt Spinler25d986c2021-02-10 13:26:18 -0600626 std::string pelID{id};
Matt Spinler27de6f32020-02-21 08:35:57 -0600627
Matt Spinler25d986c2021-02-10 13:26:18 -0600628 std::transform(pelID.begin(), pelID.end(), pelID.begin(), toupper);
629
Matt Spinlerbe952d22022-07-01 11:30:11 -0500630 if (pelID.starts_with("0X"))
Matt Spinler27de6f32020-02-21 08:35:57 -0600631 {
Matt Spinler25d986c2021-02-10 13:26:18 -0600632 pelID.erase(0, 2);
Matt Spinler27de6f32020-02-21 08:35:57 -0600633 }
Matt Spinler25d986c2021-02-10 13:26:18 -0600634
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700635 for (auto it = fs::directory_iterator(pelLogDir());
Matt Spinler25d986c2021-02-10 13:26:18 -0600636 it != fs::directory_iterator(); ++it)
Matt Spinler27de6f32020-02-21 08:35:57 -0600637 {
Matt Spinler25d986c2021-02-10 13:26:18 -0600638 if (ends_with((*it).path(), pelID))
639 {
640 fs::remove((*it).path());
641 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600642 }
643}
644
645/**
Matt Spinler25d986c2021-02-10 13:26:18 -0600646 * @brief Delete all PEL files.
Matt Spinler27de6f32020-02-21 08:35:57 -0600647 */
648void deleteAllPELs()
649{
Matt Spinler7f1b9052022-03-22 09:18:58 -0500650 log<level::INFO>("peltool deleting all event logs");
Matt Spinler27de6f32020-02-21 08:35:57 -0600651
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700652 for (const auto& entry : fs::directory_iterator(pelLogDir()))
Matt Spinler27de6f32020-02-21 08:35:57 -0600653 {
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500654 if (!fs::is_regular_file(entry.path()))
655 {
656 continue;
657 }
Matt Spinler25d986c2021-02-10 13:26:18 -0600658 fs::remove(entry.path());
Matt Spinler27de6f32020-02-21 08:35:57 -0600659 }
660}
661
Matt Spinler1b420bc2020-02-21 08:54:25 -0600662/**
663 * @brief Display a single PEL
664 *
665 * @param[in] pel - the PEL to display
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800666 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Matt Spinler1b420bc2020-02-21 08:54:25 -0600667 */
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800668void displayPEL(const PEL& pel, bool hexDump)
Matt Spinler1b420bc2020-02-21 08:54:25 -0600669{
670 if (pel.valid())
671 {
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800672 if (hexDump)
673 {
674 std::string dstr =
675 dumpHex(std::data(pel.data()), pel.size(), 0, false);
676 std::cout << dstr << std::endl;
677 }
678 else
679 {
680 auto plugins = getPlugins();
681 pel.toJSON(registry, plugins);
682 }
Matt Spinler1b420bc2020-02-21 08:54:25 -0600683 }
684 else
685 {
686 std::cerr << "PEL was malformed\n";
687 exit(1);
688 }
689}
690
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800691/**
692 * @brief Print number of PELs
693 * @param[in] hidden - Bool to include hidden logs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800694 * @param[in] includeInfo - Bool to include informational logs
Miguel Gomez011ccae2021-03-25 23:42:09 +0000695 * @param[in] critSysTerm - Bool to include CritSysTerm
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800696 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800697 */
Miguel Gomez011ccae2021-03-25 23:42:09 +0000698void printPELCount(bool hidden, bool includeInfo, bool critSysTerm,
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800699 const std::optional<std::regex>& scrubRegex)
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800700{
701 std::size_t count = 0;
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700702
703 for (auto it = fs::directory_iterator(pelLogDir());
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800704 it != fs::directory_iterator(); ++it)
705 {
706 if (!fs::is_regular_file((*it).path()))
707 {
708 continue;
709 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800710 std::vector<uint8_t> data = getFileData((*it).path());
711 if (data.empty())
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800712 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800713 continue;
714 }
715 PEL pel{data};
716 if (!pel.valid())
717 {
718 continue;
719 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800720 if (!includeInfo && pel.userHeader().severity() == 0)
721 {
722 continue;
723 }
Miguel Gomez011ccae2021-03-25 23:42:09 +0000724 if (critSysTerm && pel.userHeader().severity() != critSysTermSeverity)
725 {
726 continue;
727 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800728 std::bitset<16> actionFlags{pel.userHeader().actionFlags()};
729 if (!hidden && actionFlags.test(hiddenFlagBit))
730 {
731 continue;
732 }
733 if (pel.primarySRC() && scrubRegex)
734 {
735 std::string val = pel.primarySRC().value()->asciiString();
736 if (std::regex_search(trimEnd(val), scrubRegex.value(),
737 std::regex_constants::match_not_null))
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800738 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800739 continue;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800740 }
741 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800742 count++;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800743 }
744 std::cout << "{\n"
745 << " \"Number of PELs found\": "
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800746 << getNumberString("%d", count) << "\n}\n";
747}
748
749/**
750 * @brief Generate regex pattern object from file contents
751 * @param[in] scrubFile - File containing regex pattern
752 * @return std::regex - SRC regex object
753 */
754std::regex genRegex(std::string& scrubFile)
755{
756 std::string pattern;
757 std::ifstream contents(scrubFile);
758 if (contents.fail())
759 {
760 std::cerr << "Can't open \"" << scrubFile << "\"\n";
761 exit(1);
762 }
763 std::string line;
764 while (std::getline(contents, line))
765 {
766 if (!line.empty())
767 {
768 pattern.append(line + "|");
769 }
770 }
771 try
772 {
773 std::regex scrubRegex(pattern, std::regex::icase);
774 return scrubRegex;
775 }
Patrick Williams66491c62021-10-06 12:23:37 -0500776 catch (const std::regex_error& e)
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800777 {
778 if (e.code() == std::regex_constants::error_collate)
779 std::cerr << "Invalid collating element request\n";
780 else if (e.code() == std::regex_constants::error_ctype)
781 std::cerr << "Invalid character class\n";
782 else if (e.code() == std::regex_constants::error_escape)
783 std::cerr << "Invalid escape character or trailing escape\n";
784 else if (e.code() == std::regex_constants::error_backref)
785 std::cerr << "Invalid back reference\n";
786 else if (e.code() == std::regex_constants::error_brack)
787 std::cerr << "Mismatched bracket ([ or ])\n";
788 else if (e.code() == std::regex_constants::error_paren)
789 {
790 // to catch return code error_badrepeat when error_paren is retured
791 // instead
792 size_t pos = pattern.find_first_of("*+?{");
793 while (pos != std::string::npos)
794 {
795 if (pos == 0 || pattern.substr(pos - 1, 1) == "|")
796 {
797 std::cerr
798 << "A repetition character (*, ?, +, or {) was not "
799 "preceded by a valid regular expression\n";
800 exit(1);
801 }
802 pos = pattern.find_first_of("*+?{", pos + 1);
803 }
804 std::cerr << "Mismatched parentheses (( or ))\n";
805 }
806 else if (e.code() == std::regex_constants::error_brace)
807 std::cerr << "Mismatched brace ({ or })\n";
808 else if (e.code() == std::regex_constants::error_badbrace)
809 std::cerr << "Invalid range inside a { }\n";
810 else if (e.code() == std::regex_constants::error_range)
811 std::cerr << "Invalid character range (e.g., [z-a])\n";
812 else if (e.code() == std::regex_constants::error_space)
813 std::cerr << "Insufficient memory to handle regular expression\n";
814 else if (e.code() == std::regex_constants::error_badrepeat)
815 std::cerr << "A repetition character (*, ?, +, or {) was not "
816 "preceded by a valid regular expression\n";
817 else if (e.code() == std::regex_constants::error_complexity)
818 std::cerr << "The requested match is too complex\n";
819 else if (e.code() == std::regex_constants::error_stack)
820 std::cerr << "Insufficient memory to evaluate a match\n";
821 exit(1);
822 }
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800823}
824
Aatir186ce8c2019-10-20 15:13:39 -0500825static void exitWithError(const std::string& help, const char* err)
826{
827 std::cerr << "ERROR: " << err << std::endl << help << std::endl;
828 exit(-1);
829}
830
831int main(int argc, char** argv)
832{
833 CLI::App app{"OpenBMC PEL Tool"};
834 std::string fileName;
Aatire340c132019-12-09 14:19:29 -0600835 std::string idPEL;
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800836 std::string bmcId;
Matt Spinler27de6f32020-02-21 08:35:57 -0600837 std::string idToDelete;
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800838 std::string scrubFile;
839 std::optional<std::regex> scrubRegex;
Aatire340c132019-12-09 14:19:29 -0600840 bool listPEL = false;
841 bool listPELDescOrd = false;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800842 bool hidden = false;
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800843 bool includeInfo = false;
Miguel Gomez011ccae2021-03-25 23:42:09 +0000844 bool critSysTerm = false;
Matt Spinler27de6f32020-02-21 08:35:57 -0600845 bool deleteAll = false;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800846 bool showPELCount = false;
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800847 bool fullPEL = false;
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800848 bool hexDump = false;
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500849 bool archive = false;
Matt Spinler27de6f32020-02-21 08:35:57 -0600850
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800851 app.set_help_flag("--help", "Print this help message and exit");
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800852 app.add_option("--file", fileName, "Display a PEL using its Raw PEL file");
Aatire340c132019-12-09 14:19:29 -0600853 app.add_option("-i, --id", idPEL, "Display a PEL based on its ID");
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800854 app.add_option("--bmc-id", bmcId,
855 "Display a PEL based on its BMC Event ID");
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800856 app.add_flag("-a", fullPEL, "Display all PELs");
Aatirbad5f8a2019-12-10 15:27:16 -0600857 app.add_flag("-l", listPEL, "List PELs");
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800858 app.add_flag("-n", showPELCount, "Show number of PELs");
Aatir7b291ec2019-11-19 10:37:37 -0600859 app.add_flag("-r", listPELDescOrd, "Reverse order of output");
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800860 app.add_flag("-h", hidden, "Include hidden PELs");
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800861 app.add_flag("-f,--info", includeInfo, "Include informational PELs");
Miguel Gomez011ccae2021-03-25 23:42:09 +0000862 app.add_flag("-t, --termination", critSysTerm,
863 "List only critical system terminating PELs");
Matt Spinler27de6f32020-02-21 08:35:57 -0600864 app.add_option("-d, --delete", idToDelete, "Delete a PEL based on its ID");
865 app.add_flag("-D, --delete-all", deleteAll, "Delete all PELs");
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800866 app.add_option("-s, --scrub", scrubFile,
867 "File containing SRC regular expressions to ignore");
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800868 app.add_flag("-x", hexDump, "Display PEL(s) in hexdump instead of JSON");
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500869 app.add_flag("--archive", archive, "List or display archived PELs");
Matt Spinler27de6f32020-02-21 08:35:57 -0600870
Aatir186ce8c2019-10-20 15:13:39 -0500871 CLI11_PARSE(app, argc, argv);
872
873 if (!fileName.empty())
874 {
875 std::vector<uint8_t> data = getFileData(fileName);
876 if (!data.empty())
877 {
878 PEL pel{data};
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800879 if (hexDump)
880 {
881 std::string dstr =
882 dumpHex(std::data(pel.data()), pel.size(), 0, false);
883 std::cout << dstr << std::endl;
884 }
885 else
886 {
887 auto plugins = getPlugins();
888 pel.toJSON(registry, plugins);
889 }
Aatir186ce8c2019-10-20 15:13:39 -0500890 }
891 else
892 {
893 exitWithError(app.help("", CLI::AppFormatMode::All),
894 "Raw PEL file can't be read.");
895 }
896 }
Aatire340c132019-12-09 14:19:29 -0600897 else if (!idPEL.empty())
898 {
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500899 callFunctionOnPEL(idPEL, displayPEL, false, hexDump, archive);
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800900 }
901 else if (!bmcId.empty())
902 {
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500903 callFunctionOnPEL(bmcId, displayPEL, true, hexDump, archive);
Aatire340c132019-12-09 14:19:29 -0600904 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800905 else if (fullPEL || listPEL)
Aatir7b291ec2019-11-19 10:37:37 -0600906 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800907 if (!scrubFile.empty())
908 {
909 scrubRegex = genRegex(scrubFile);
910 }
Miguel Gomez011ccae2021-03-25 23:42:09 +0000911 printPELs(listPELDescOrd, hidden, includeInfo, critSysTerm, fullPEL,
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500912 scrubRegex, hexDump, archive);
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800913 }
914 else if (showPELCount)
915 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800916 if (!scrubFile.empty())
917 {
918 scrubRegex = genRegex(scrubFile);
919 }
Miguel Gomez011ccae2021-03-25 23:42:09 +0000920 printPELCount(hidden, includeInfo, critSysTerm, scrubRegex);
Aatir7b291ec2019-11-19 10:37:37 -0600921 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600922 else if (!idToDelete.empty())
923 {
Matt Spinler25d986c2021-02-10 13:26:18 -0600924 deletePEL(idToDelete);
Matt Spinler27de6f32020-02-21 08:35:57 -0600925 }
926 else if (deleteAll)
927 {
928 deleteAllPELs();
929 }
Aatir186ce8c2019-10-20 15:13:39 -0500930 else
931 {
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800932 std::cout << app.help("", CLI::AppFormatMode::All) << std::endl;
Aatir186ce8c2019-10-20 15:13:39 -0500933 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800934 Py_Finalize();
Aatir186ce8c2019-10-20 15:13:39 -0500935 return 0;
936}