blob: 152c013a0a55958c782d1ba441a652403f6029a6 [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
Jayanth Othayoth7a36d362020-07-11 08:09:03 -050011#include <cstdlib>
Marri Devender Rao78479602020-01-06 03:45:11 -060012#include <iomanip>
13#include <phosphor-logging/elog.hpp>
14#include <sstream>
Jayanth Othayoth7a36d362020-07-11 08:09:03 -050015#include <string>
Marri Devender Rao78479602020-01-06 03:45:11 -060016
17namespace openpower
18{
19namespace pel
20{
21using namespace phosphor::logging;
22
23namespace detail
24{
25
26// keys need to be unique so using counter value to generate unique key
27static int counter = 0;
28
29// list of debug traces
30static std::vector<std::pair<std::string, std::string>> traceLog;
31
32void processLogTraceCallback(void* private_data, const char* fmt, va_list ap)
33{
34 va_list vap;
35 va_copy(vap, ap);
Jayanth Othayoth748d32c2020-07-10 01:45:20 -050036 std::vector<char> logData(1 + std::vsnprintf(nullptr, 0, fmt, ap));
37 std::vsnprintf(logData.data(), logData.size(), fmt, vap);
Marri Devender Rao78479602020-01-06 03:45:11 -060038 va_end(vap);
Jayanth Othayoth748d32c2020-07-10 01:45:20 -050039 std::string logstr(logData.begin(), logData.end());
Marri Devender Rao78479602020-01-06 03:45:11 -060040
Jayanth Othayoth748d32c2020-07-10 01:45:20 -050041 log<level::INFO>(logstr.c_str());
Marri Devender Rao78479602020-01-06 03:45:11 -060042
43 char timeBuf[80];
44 time_t t = time(0);
45 tm myTm{};
46 gmtime_r(&t, &myTm);
47 strftime(timeBuf, 80, "%Y-%m-%d %H:%M:%S", &myTm);
48
49 // key values need to be unique for PEL
50 // TODO #openbmc/dev/issues/1563
51 // If written to Json no need to worry about unique KEY
52 std::stringstream str;
53 str << std::setfill('0');
54 str << "LOG" << std::setw(3) << counter;
55 str << " " << timeBuf;
56 traceLog.emplace_back(std::make_pair(str.str(), std::move(logstr)));
57 counter++;
58}
59
60void processBootErrorCallback(bool status)
61{
62 log<level::INFO>("processBootCallback ", entry("STATUS=%d", status));
63 try
64 {
65 // If failure in hwp execution
66 if (!status)
67 {
68 FFDCData ffdc = libekb_get_ffdc();
69 ffdc.insert(ffdc.end(), traceLog.begin(), traceLog.end());
70 openpower::pel::createBootErrorPEL(ffdc);
71 }
72 }
73 catch (std::exception& ex)
74 {
75 reset();
76 throw ex;
77 }
78 reset();
79}
80
81void reset()
82{
83 // reset the trace log and counter
84 traceLog.clear();
85 counter = 0;
86}
Ramesh Iyyarc98bab52020-04-16 04:04:29 -050087
88void pDBGLogTraceCallbackHelper(int log_level, const char* fmt, va_list ap)
89{
90 processLogTraceCallback(NULL, fmt, ap);
91}
Marri Devender Rao78479602020-01-06 03:45:11 -060092} // namespace detail
93
Jayanth Othayoth7a36d362020-07-11 08:09:03 -050094static inline uint8_t getLogLevelFromEnv(const char* env, const uint8_t dValue)
95{
96 auto logLevel = dValue;
97 try
98 {
99 if (const char* env_p = std::getenv(env))
100 {
101 logLevel = std::stoi(env_p);
102 }
103 }
104 catch (std::exception& e)
105 {
106 log<level::ERR>(("Conversion Failure"), entry("ENVIRONMENT=%s", env),
107 entry("EXCEPTION=%s", e.what()));
108 }
109 return logLevel;
110}
111
Marri Devender Rao78479602020-01-06 03:45:11 -0600112void addBootErrorCallbacks()
113{
Jayanth Othayoth7a36d362020-07-11 08:09:03 -0500114 // Get individual phal repos log level from environment variable
115 // and update the log level.
116 pdbg_set_loglevel(getLogLevelFromEnv("PDBG_LOG", PDBG_INFO));
117 libekb_set_loglevel(getLogLevelFromEnv("LIBEKB_LOG", LIBEKB_LOG_IMP));
118 ipl_set_loglevel(getLogLevelFromEnv("IPL_LOG", IPL_INFO));
Marri Devender Rao78479602020-01-06 03:45:11 -0600119
120 // add callback for debug traces
Ramesh Iyyarc98bab52020-04-16 04:04:29 -0500121 pdbg_set_logfunc(detail::pDBGLogTraceCallbackHelper);
122 libekb_set_logfunc(detail::processLogTraceCallback, NULL);
Marri Devender Rao78479602020-01-06 03:45:11 -0600123 ipl_set_logfunc(detail::processLogTraceCallback, NULL);
124
125 // add callback for ipl failures
Jayanth Othayothaefde692020-03-27 07:56:47 -0500126 ipl_set_error_callback_func(detail::processBootErrorCallback);
Marri Devender Rao78479602020-01-06 03:45:11 -0600127}
Jayanth Othayoth7a36d362020-07-11 08:09:03 -0500128
Marri Devender Rao78479602020-01-06 03:45:11 -0600129} // namespace pel
130} // namespace openpower