blob: 698886be009748c8903ace9f77e99c452be6c70d [file] [log] [blame]
Ramesh Iyyarc98bab52020-04-16 04:04:29 -05001extern "C" {
2#include <libpdbg.h>
3}
Marri Devender Rao78479602020-01-06 03:45:11 -06004
5#include "create_pel.hpp"
Ramesh Iyyarc98bab52020-04-16 04:04:29 -05006#include "phal_error.hpp"
Marri Devender Rao78479602020-01-06 03:45:11 -06007
8#include <libekb.H>
9#include <libipl.H>
10
11#include <iomanip>
12#include <phosphor-logging/elog.hpp>
13#include <sstream>
14
15namespace openpower
16{
17namespace pel
18{
19using namespace phosphor::logging;
20
21namespace detail
22{
23
24// keys need to be unique so using counter value to generate unique key
25static int counter = 0;
26
27// list of debug traces
28static std::vector<std::pair<std::string, std::string>> traceLog;
29
30void processLogTraceCallback(void* private_data, const char* fmt, va_list ap)
31{
32 va_list vap;
33 va_copy(vap, ap);
Jayanth Othayoth748d32c2020-07-10 01:45:20 -050034 std::vector<char> logData(1 + std::vsnprintf(nullptr, 0, fmt, ap));
35 std::vsnprintf(logData.data(), logData.size(), fmt, vap);
Marri Devender Rao78479602020-01-06 03:45:11 -060036 va_end(vap);
Jayanth Othayoth748d32c2020-07-10 01:45:20 -050037 std::string logstr(logData.begin(), logData.end());
Marri Devender Rao78479602020-01-06 03:45:11 -060038
Jayanth Othayoth748d32c2020-07-10 01:45:20 -050039 log<level::INFO>(logstr.c_str());
Marri Devender Rao78479602020-01-06 03:45:11 -060040
41 char timeBuf[80];
42 time_t t = time(0);
43 tm myTm{};
44 gmtime_r(&t, &myTm);
45 strftime(timeBuf, 80, "%Y-%m-%d %H:%M:%S", &myTm);
46
47 // key values need to be unique for PEL
48 // TODO #openbmc/dev/issues/1563
49 // If written to Json no need to worry about unique KEY
50 std::stringstream str;
51 str << std::setfill('0');
52 str << "LOG" << std::setw(3) << counter;
53 str << " " << timeBuf;
54 traceLog.emplace_back(std::make_pair(str.str(), std::move(logstr)));
55 counter++;
56}
57
58void processBootErrorCallback(bool status)
59{
60 log<level::INFO>("processBootCallback ", entry("STATUS=%d", status));
61 try
62 {
63 // If failure in hwp execution
64 if (!status)
65 {
66 FFDCData ffdc = libekb_get_ffdc();
67 ffdc.insert(ffdc.end(), traceLog.begin(), traceLog.end());
68 openpower::pel::createBootErrorPEL(ffdc);
69 }
70 }
71 catch (std::exception& ex)
72 {
73 reset();
74 throw ex;
75 }
76 reset();
77}
78
79void reset()
80{
81 // reset the trace log and counter
82 traceLog.clear();
83 counter = 0;
84}
Ramesh Iyyarc98bab52020-04-16 04:04:29 -050085
86void pDBGLogTraceCallbackHelper(int log_level, const char* fmt, va_list ap)
87{
88 processLogTraceCallback(NULL, fmt, ap);
89}
Marri Devender Rao78479602020-01-06 03:45:11 -060090} // namespace detail
91
92void addBootErrorCallbacks()
93{
94 // set log level to info
Ramesh Iyyarc98bab52020-04-16 04:04:29 -050095 pdbg_set_loglevel(PDBG_INFO);
96 libekb_set_loglevel(LIBEKB_LOG_INF);
Marri Devender Rao78479602020-01-06 03:45:11 -060097 ipl_set_loglevel(IPL_INFO);
98
99 // add callback for debug traces
Ramesh Iyyarc98bab52020-04-16 04:04:29 -0500100 pdbg_set_logfunc(detail::pDBGLogTraceCallbackHelper);
101 libekb_set_logfunc(detail::processLogTraceCallback, NULL);
Marri Devender Rao78479602020-01-06 03:45:11 -0600102 ipl_set_logfunc(detail::processLogTraceCallback, NULL);
103
104 // add callback for ipl failures
Jayanth Othayothaefde692020-03-27 07:56:47 -0500105 ipl_set_error_callback_func(detail::processBootErrorCallback);
Marri Devender Rao78479602020-01-06 03:45:11 -0600106}
107} // namespace pel
108} // namespace openpower