Ramesh Iyyar | c98bab5 | 2020-04-16 04:04:29 -0500 | [diff] [blame] | 1 | extern "C" {
|
| 2 | #include <libpdbg.h>
|
| 3 | }
|
Marri Devender Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 4 |
|
| 5 | #include "create_pel.hpp"
|
Ramesh Iyyar | c98bab5 | 2020-04-16 04:04:29 -0500 | [diff] [blame] | 6 | #include "phal_error.hpp"
|
Marri Devender Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 7 |
|
| 8 | #include <libekb.H>
|
| 9 | #include <libipl.H>
|
| 10 |
|
Jayanth Othayoth | 7a36d36 | 2020-07-11 08:09:03 -0500 | [diff] [blame] | 11 | #include <cstdlib>
|
Marri Devender Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 12 | #include <iomanip>
|
| 13 | #include <phosphor-logging/elog.hpp>
|
| 14 | #include <sstream>
|
Jayanth Othayoth | 7a36d36 | 2020-07-11 08:09:03 -0500 | [diff] [blame] | 15 | #include <string>
|
Marri Devender Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 16 |
|
| 17 | namespace openpower
|
| 18 | {
|
| 19 | namespace pel
|
| 20 | {
|
| 21 | using namespace phosphor::logging;
|
| 22 |
|
| 23 | namespace detail
|
| 24 | {
|
| 25 |
|
| 26 | // keys need to be unique so using counter value to generate unique key
|
| 27 | static int counter = 0;
|
| 28 |
|
| 29 | // list of debug traces
|
| 30 | static std::vector<std::pair<std::string, std::string>> traceLog;
|
| 31 |
|
| 32 | void processLogTraceCallback(void* private_data, const char* fmt, va_list ap)
|
| 33 | {
|
| 34 | va_list vap;
|
| 35 | va_copy(vap, ap);
|
Jayanth Othayoth | 748d32c | 2020-07-10 01:45:20 -0500 | [diff] [blame] | 36 | std::vector<char> logData(1 + std::vsnprintf(nullptr, 0, fmt, ap));
|
| 37 | std::vsnprintf(logData.data(), logData.size(), fmt, vap);
|
Marri Devender Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 38 | va_end(vap);
|
Jayanth Othayoth | 748d32c | 2020-07-10 01:45:20 -0500 | [diff] [blame] | 39 | std::string logstr(logData.begin(), logData.end());
|
Marri Devender Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 40 |
|
Jayanth Othayoth | 748d32c | 2020-07-10 01:45:20 -0500 | [diff] [blame] | 41 | log<level::INFO>(logstr.c_str());
|
Marri Devender Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 42 |
|
| 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 |
|
| 60 | void 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 |
|
| 81 | void reset()
|
| 82 | {
|
| 83 | // reset the trace log and counter
|
| 84 | traceLog.clear();
|
| 85 | counter = 0;
|
| 86 | }
|
Ramesh Iyyar | c98bab5 | 2020-04-16 04:04:29 -0500 | [diff] [blame] | 87 |
|
| 88 | void pDBGLogTraceCallbackHelper(int log_level, const char* fmt, va_list ap)
|
| 89 | {
|
| 90 | processLogTraceCallback(NULL, fmt, ap);
|
| 91 | }
|
Marri Devender Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 92 | } // namespace detail
|
| 93 |
|
Jayanth Othayoth | 7a36d36 | 2020-07-11 08:09:03 -0500 | [diff] [blame] | 94 | static 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 Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 112 | void addBootErrorCallbacks()
|
| 113 | {
|
Jayanth Othayoth | 7a36d36 | 2020-07-11 08:09:03 -0500 | [diff] [blame] | 114 | // 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 Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 119 |
|
| 120 | // add callback for debug traces
|
Ramesh Iyyar | c98bab5 | 2020-04-16 04:04:29 -0500 | [diff] [blame] | 121 | pdbg_set_logfunc(detail::pDBGLogTraceCallbackHelper);
|
| 122 | libekb_set_logfunc(detail::processLogTraceCallback, NULL);
|
Marri Devender Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 123 | ipl_set_logfunc(detail::processLogTraceCallback, NULL);
|
| 124 |
|
| 125 | // add callback for ipl failures
|
Jayanth Othayoth | aefde69 | 2020-03-27 07:56:47 -0500 | [diff] [blame] | 126 | ipl_set_error_callback_func(detail::processBootErrorCallback);
|
Marri Devender Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 127 | }
|
Jayanth Othayoth | 7a36d36 | 2020-07-11 08:09:03 -0500 | [diff] [blame] | 128 |
|
Marri Devender Rao | 7847960 | 2020-01-06 03:45:11 -0600 | [diff] [blame] | 129 | } // namespace pel
|
| 130 | } // namespace openpower
|