blob: ed6daa255128e7425d209e739010288c26d2fdf4 [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 Isa0f717e12020-01-15 20:05:33 +080019#include "../paths.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050020#include "../pel.hpp"
Aatir7b291ec2019-11-19 10:37:37 -060021#include "../pel_types.hpp"
22#include "../pel_values.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050023
24#include <CLI/CLI.hpp>
Aatir7b291ec2019-11-19 10:37:37 -060025#include <bitset>
Aatir186ce8c2019-10-20 15:13:39 -050026#include <iostream>
Aatir7b291ec2019-11-19 10:37:37 -060027#include <phosphor-logging/log.hpp>
28#include <regex>
Aatir186ce8c2019-10-20 15:13:39 -050029#include <string>
Aatir7b291ec2019-11-19 10:37:37 -060030#include <xyz/openbmc_project/Common/File/error.hpp>
Aatir186ce8c2019-10-20 15:13:39 -050031
Aatir7b291ec2019-11-19 10:37:37 -060032namespace fs = std::filesystem;
Aatir186ce8c2019-10-20 15:13:39 -050033using namespace phosphor::logging;
34using namespace openpower::pels;
Aatir7b291ec2019-11-19 10:37:37 -060035namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
36namespace message = openpower::pels::message;
37namespace pv = openpower::pels::pel_values;
Aatir186ce8c2019-10-20 15:13:39 -050038
Aatire340c132019-12-09 14:19:29 -060039/**
Aatir37822f62019-12-10 14:40:27 -060040 * @brief helper function to get PEL commit timestamp from file name
41 * @retrun BCDTime - PEL commit timestamp
42 * @param[in] std::string - file name
43 */
44BCDTime fileNameToTimestamp(const std::string& fileName)
45{
46 std::string token = fileName.substr(0, fileName.find("_"));
47 int i = 0;
48 BCDTime tmp;
49 if (token.length() >= 14)
50 {
51 try
52 {
53 tmp.yearMSB = std::stoi(token.substr(i, 2), 0, 16);
54 }
55 catch (std::exception& err)
56 {
57 std::cout << "Conversion failure: " << err.what() << std::endl;
58 }
59 i += 2;
60 try
61 {
62 tmp.yearLSB = std::stoi(token.substr(i, 2), 0, 16);
63 }
64 catch (std::exception& err)
65 {
66 std::cout << "Conversion failure: " << err.what() << std::endl;
67 }
68 i += 2;
69 try
70 {
71 tmp.month = std::stoi(token.substr(i, 2), 0, 16);
72 }
73 catch (std::exception& err)
74 {
75 std::cout << "Conversion failure: " << err.what() << std::endl;
76 }
77 i += 2;
78 try
79 {
80 tmp.day = std::stoi(token.substr(i, 2), 0, 16);
81 }
82 catch (std::exception& err)
83 {
84 std::cout << "Conversion failure: " << err.what() << std::endl;
85 }
86 i += 2;
87 try
88 {
89 tmp.hour = std::stoi(token.substr(i, 2), 0, 16);
90 }
91 catch (std::exception& err)
92 {
93 std::cout << "Conversion failure: " << err.what() << std::endl;
94 }
95 i += 2;
96 try
97 {
98 tmp.minutes = std::stoi(token.substr(i, 2), 0, 16);
99 }
100 catch (std::exception& err)
101 {
102 std::cout << "Conversion failure: " << err.what() << std::endl;
103 }
104 i += 2;
105 try
106 {
107 tmp.seconds = std::stoi(token.substr(i, 2), 0, 16);
108 }
109 catch (std::exception& err)
110 {
111 std::cout << "Conversion failure: " << err.what() << std::endl;
112 }
113 i += 2;
114 try
115 {
116 tmp.hundredths = std::stoi(token.substr(i, 2), 0, 16);
117 }
118 catch (std::exception& err)
119 {
120 std::cout << "Conversion failure: " << err.what() << std::endl;
121 }
122 }
123 return tmp;
124}
125
126/**
127 * @brief helper function to get PEL id from file name
128 * @retrun uint32_t - PEL id
129 * @param[in] std::string - file name
130 */
131uint32_t fileNameToPELId(const std::string& fileName)
132{
133 uint32_t num = 0;
134 try
135 {
136 num = std::stoi(fileName.substr(fileName.find("_") + 1), 0, 16);
137 }
138 catch (std::exception& err)
139 {
140 std::cout << "Conversion failure: " << err.what() << std::endl;
141 }
142 return num;
143}
144
145/**
Aatire340c132019-12-09 14:19:29 -0600146 * @brief helper function to check string suffix
147 * @retrun bool - true with suffix matches
148 * @param[in] std::string - string to check for suffix
149 * @param[in] std::string - suffix string
150 */
151bool ends_with(const std::string& str, const std::string& end)
152{
153 size_t slen = str.size(), elen = end.size();
154 if (slen < elen)
155 return false;
156 while (elen)
157 {
158 if (str[--slen] != end[--elen])
159 return false;
160 }
161 return true;
162}
163
Aatir37822f62019-12-10 14:40:27 -0600164/**
165 * @brief get data form raw PEL file.
166 * @param[in] std::string Name of file with raw PEL
167 * @return std::vector<uint8_t> char vector read from raw PEL file.
168 */
Aatirbad5f8a2019-12-10 15:27:16 -0600169std::vector<uint8_t> getFileData(const std::string& name)
Aatir37822f62019-12-10 14:40:27 -0600170{
171 std::ifstream file(name, std::ifstream::in);
172 if (file.good())
173 {
174 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
175 std::istreambuf_iterator<char>()};
176 return data;
177 }
178 else
179 {
180 printf("Can't open raw PEL file");
181 return {};
182 }
183}
184
Aatir7b291ec2019-11-19 10:37:37 -0600185template <typename T>
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800186std::string genPELJSON(T itr, bool hidden, message::Registry& registry)
Aatir7b291ec2019-11-19 10:37:37 -0600187{
188 std::size_t found;
189 std::string val;
190 char tmpValStr[50];
191 std::string listStr;
192 char name[50];
193 sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", itr.second.yearMSB,
194 itr.second.yearLSB, itr.second.month, itr.second.day,
195 itr.second.hour, itr.second.minutes, itr.second.seconds,
196 itr.second.hundredths, itr.first);
197 std::string fileName(name);
198 fileName = EXTENSION_PERSIST_DIR "/pels/logs/" + fileName;
199 try
200 {
Aatir37822f62019-12-10 14:40:27 -0600201 std::vector<uint8_t> data = getFileData(fileName);
Aatir37822f62019-12-10 14:40:27 -0600202 if (!data.empty())
203 {
Aatir37822f62019-12-10 14:40:27 -0600204 PEL pel{data};
205 std::bitset<16> actionFlags{pel.userHeader().actionFlags()};
206 if (pel.valid() && (hidden || !actionFlags.test(hiddenFlagBit)))
Aatir7b291ec2019-11-19 10:37:37 -0600207 {
Aatir37822f62019-12-10 14:40:27 -0600208 // id
209 sprintf(tmpValStr, "0x%X", pel.privateHeader().id());
210 val = std::string(tmpValStr);
211 listStr += "\t\"" + val + "\": {\n";
212 // ASCII
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800213 if (pel.primarySRC())
214 {
215 val = pel.primarySRC().value()->asciiString();
216 listStr += "\t\t\"SRC\": \"" +
217 val.substr(0, val.find(0x20)) + "\",\n";
218 // Registry message
219 auto regVal = pel.primarySRC().value()->getErrorDetails(
220 registry, DetailLevel::message, true);
221 if (regVal)
222 {
223 val = regVal.value();
224 listStr += "\t\t\"Message\": \"" + val + "\",\n";
225 }
226 }
227 else
228 {
229 listStr += "\t\t\"SRC\": \"No SRC\",\n";
230 }
Aatir37822f62019-12-10 14:40:27 -0600231 // platformid
232 sprintf(tmpValStr, "0x%X", pel.privateHeader().plid());
233 val = std::string(tmpValStr);
234 listStr += "\t\t\"PLID\": \"" + val + "\",\n";
235 // creatorid
236 sprintf(tmpValStr, "%c", pel.privateHeader().creatorID());
237 std::string creatorID(tmpValStr);
238 val = pv::creatorIDs.count(creatorID)
239 ? pv::creatorIDs.at(creatorID)
240 : "Unknown Creator ID";
241 listStr += "\t\t\"CreatorID\": \"" + val + "\",\n";
242 // subsytem
243 std::string subsystem = pv::getValue(
244 pel.userHeader().subsystem(), pel_values::subsystemValues);
245 listStr += "\t\t\"Subsystem\": \"" + subsystem + "\",\n";
246 // commit time
247 sprintf(tmpValStr, "%02X/%02X/%02X%02X %02X:%02X:%02X",
248 pel.privateHeader().commitTimestamp().month,
249 pel.privateHeader().commitTimestamp().day,
250 pel.privateHeader().commitTimestamp().yearMSB,
251 pel.privateHeader().commitTimestamp().yearLSB,
252 pel.privateHeader().commitTimestamp().hour,
253 pel.privateHeader().commitTimestamp().minutes,
254 pel.privateHeader().commitTimestamp().seconds);
255 val = std::string(tmpValStr);
256 listStr += "\t\t\"Commit Time\": \"" + val + "\",\n";
257 // severity
258 std::string severity = pv::getValue(pel.userHeader().severity(),
259 pel_values::severityValues);
260 listStr += "\t\t\"Sev\": \"" + severity + "\",\n ";
261 // compID
262 sprintf(tmpValStr, "0x%X",
263 pel.privateHeader().header().componentID);
264 val = std::string(tmpValStr);
265 listStr += "\t\t\"CompID\": \"" + val + "\",\n ";
266
267 found = listStr.rfind(",");
268 if (found != std::string::npos)
269 {
270 listStr.replace(found, 1, "");
271 listStr += "\t}, \n";
272 }
Aatir7b291ec2019-11-19 10:37:37 -0600273 }
274 }
Aatir37822f62019-12-10 14:40:27 -0600275 else
276 {
277 log<level::ERR>("Empty PEL file",
278 entry("FILENAME=%s", fileName.c_str()),
279 entry("ERROR=%s", "Empty PEL file"));
280 }
Aatir7b291ec2019-11-19 10:37:37 -0600281 }
282 catch (std::exception& e)
283 {
284 log<level::ERR>("Hit exception while reading PEL File",
285 entry("FILENAME=%s", fileName.c_str()),
286 entry("ERROR=%s", e.what()));
287 }
288 return listStr;
289}
290/**
291 * @brief Print a list of PELs
292 */
293void printList(bool order, bool hidden)
294{
295 std::string listStr;
296 std::map<uint32_t, BCDTime> PELs;
297 std::size_t found;
298 listStr = "{\n";
299 for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs");
300 it != fs::directory_iterator(); ++it)
301 {
302 if (!fs::is_regular_file((*it).path()))
303 {
304 continue;
305 }
Aatir37822f62019-12-10 14:40:27 -0600306 else
Aatir7b291ec2019-11-19 10:37:37 -0600307 {
Aatir37822f62019-12-10 14:40:27 -0600308 PELs.emplace(fileNameToPELId((*it).path().filename()),
309 fileNameToTimestamp((*it).path().filename()));
Aatir7b291ec2019-11-19 10:37:37 -0600310 }
311 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800312 message::Registry registry(getMessageRegistryPath() /
313 message::registryFileName);
314 auto buildJSON = [&listStr, &hidden, &registry](const auto& i) {
315 listStr += genPELJSON(i, hidden, registry);
Aatir37822f62019-12-10 14:40:27 -0600316 };
Aatir7b291ec2019-11-19 10:37:37 -0600317 if (order)
318 {
319 std::for_each(PELs.rbegin(), PELs.rend(), buildJSON);
320 }
321 else
322 {
323 std::for_each(PELs.begin(), PELs.end(), buildJSON);
324 }
325
326 found = listStr.rfind(",");
327 if (found != std::string::npos)
328 {
329 listStr.replace(found, 1, "");
330 listStr += "\n}\n";
331 printf("%s", listStr.c_str());
332 }
333}
Aatir186ce8c2019-10-20 15:13:39 -0500334
335static void exitWithError(const std::string& help, const char* err)
336{
337 std::cerr << "ERROR: " << err << std::endl << help << std::endl;
338 exit(-1);
339}
340
341int main(int argc, char** argv)
342{
343 CLI::App app{"OpenBMC PEL Tool"};
344 std::string fileName;
Aatire340c132019-12-09 14:19:29 -0600345 std::string idPEL;
346 bool listPEL = false;
347 bool listPELDescOrd = false;
348 bool listPELShowHidden = false;
349 app.add_option("-f,--file", fileName,
350 "Display a PEL using its Raw PEL file");
351 app.add_option("-i, --id", idPEL, "Display a PEL based on its ID");
Aatirbad5f8a2019-12-10 15:27:16 -0600352 app.add_flag("-l", listPEL, "List PELs");
Aatir7b291ec2019-11-19 10:37:37 -0600353 app.add_flag("-r", listPELDescOrd, "Reverse order of output");
354 app.add_flag("-s", listPELShowHidden, "Show hidden PELs");
Aatir186ce8c2019-10-20 15:13:39 -0500355 CLI11_PARSE(app, argc, argv);
356
357 if (!fileName.empty())
358 {
359 std::vector<uint8_t> data = getFileData(fileName);
360 if (!data.empty())
361 {
362 PEL pel{data};
363 pel.toJSON();
364 }
365 else
366 {
367 exitWithError(app.help("", CLI::AppFormatMode::All),
368 "Raw PEL file can't be read.");
369 }
370 }
Aatir7b291ec2019-11-19 10:37:37 -0600371
Aatire340c132019-12-09 14:19:29 -0600372 else if (!idPEL.empty())
373 {
374 for (auto it =
375 fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs");
376 it != fs::directory_iterator(); ++it)
377 {
378 if (!fs::is_regular_file((*it).path()))
379 {
380 continue;
381 }
382 try
383 {
384 for (auto& c : idPEL)
385 c = toupper(c);
386 size_t found = idPEL.find("0X");
387 if (found == 0)
388 {
389 idPEL.erase(0, 2);
390 }
391 if (ends_with((*it).path(), idPEL))
392 {
393 std::vector<uint8_t> data = getFileData((*it).path());
394 if (!data.empty())
395 {
396 PEL pel{data};
397 if (pel.valid())
398 {
399 pel.toJSON();
400 }
401 else
402 {
403 log<level::ERR>(
404 "PEL File contains invalid PEL",
405 entry("FILENAME=%s", (*it).path().c_str()),
406 entry("ERROR=%s", "file contains invalid PEL"));
407 }
408 }
409 break;
410 }
411 }
412 catch (std::exception& e)
413 {
414 log<level::ERR>("Hit exception while reading PEL File",
415 entry("FILENAME=%s", (*it).path().c_str()),
416 entry("ERROR=%s", e.what()));
417 }
418 }
419 }
Aatir7b291ec2019-11-19 10:37:37 -0600420 else if (listPEL)
421 {
422
423 printList(listPELDescOrd, listPELShowHidden);
424 }
Aatir186ce8c2019-10-20 15:13:39 -0500425 else
426 {
427 exitWithError(app.help("", CLI::AppFormatMode::All),
428 "Raw PEL file path not specified.");
429 }
430 return 0;
431}