blob: 0dc8d3c156b073de31e7ea38dcc672426ed5fd1c [file] [log] [blame]
Eddie James2f9f9bb2021-09-20 14:26:31 -05001#include "occ_ffdc.hpp"
2
3#include "elog-errors.hpp"
4#include "utils.hpp"
5
6#include <errno.h>
7#include <fcntl.h>
8#include <stdio.h>
9#include <sys/ioctl.h>
10#include <unistd.h>
11
12#include <org/open_power/OCC/Device/error.hpp>
13#include <phosphor-logging/elog.hpp>
14#include <phosphor-logging/log.hpp>
15#include <xyz/openbmc_project/Common/error.hpp>
16#include <xyz/openbmc_project/Logging/Create/server.hpp>
17
18namespace open_power
19{
20namespace occ
21{
22
23static constexpr size_t max_ffdc_size = 8192;
24static constexpr size_t sbe_status_header_size = 8;
25
26static constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging";
27static constexpr auto loggingInterface = "org.open_power.Logging.PEL";
28
29using namespace phosphor::logging;
30using namespace sdbusplus::org::open_power::OCC::Device::Error;
31using InternalFailure =
32 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
33
34uint32_t FFDC::createPEL(const char* path, uint32_t src6, const char* msg,
35 int fd)
36{
37 uint32_t plid = 0;
38 std::vector<std::tuple<
39 sdbusplus::xyz::openbmc_project::Logging::server::Create::FFDCFormat,
40 uint8_t, uint8_t, sdbusplus::message::unix_fd>>
41 pelFFDCInfo;
42
43 log<level::INFO>("Creating PEL with SBE FFDC", entry("SRC6=%08x", src6));
44
45 if (fd > 0)
46 {
47 pelFFDCInfo.push_back(std::make_tuple(
48 sdbusplus::xyz::openbmc_project::Logging::server::Create::
49 FFDCFormat::Custom,
50 static_cast<uint8_t>(0xCB), static_cast<uint8_t>(0x01), fd));
51 }
52
53 std::map<std::string, std::string> additionalData;
54 additionalData.emplace("SRC6", std::to_string(src6));
55 additionalData.emplace("_PID", std::to_string(getpid()));
56 additionalData.emplace("SBE_ERR_MSG", msg);
57
Eddie James2f9f9bb2021-09-20 14:26:31 -050058 auto& bus = utils::getBus();
59
60 try
61 {
Eddie James338748b2021-10-29 10:06:50 -050062 std::string service =
63 utils::getService(loggingObjectPath, loggingInterface);
Eddie James2f9f9bb2021-09-20 14:26:31 -050064 auto method =
65 bus.new_method_call(service.c_str(), loggingObjectPath,
66 loggingInterface, "CreatePELWithFFDCFiles");
67 auto level =
68 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
69 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
70 Error);
71 method.append(path, level, additionalData, pelFFDCInfo);
72 auto response = bus.call(method);
73 std::tuple<uint32_t, uint32_t> reply = {0, 0};
74
75 response.read(reply);
76 plid = std::get<1>(reply);
77 }
78 catch (const sdbusplus::exception::exception& e)
79 {
80 log<level::ERR>("Failed to create PEL");
81 }
82
83 return plid;
84}
85
86// Reads the FFDC file and create an error log
87void FFDC::analyzeEvent()
88{
89 int tfd = -1;
90 size_t total = 0;
91 auto data = std::make_unique<unsigned char[]>(max_ffdc_size);
92 while (total < max_ffdc_size)
93 {
94 auto r = read(fd, data.get() + total, max_ffdc_size - total);
95 if (r < 0)
96 {
97 elog<ReadFailure>(
98 phosphor::logging::org::open_power::OCC::Device::ReadFailure::
99 CALLOUT_ERRNO(errno),
100 phosphor::logging::org::open_power::OCC::Device::ReadFailure::
101 CALLOUT_DEVICE_PATH(file.c_str()));
102 return;
103 }
104 if (!r)
105 {
106 break;
107 }
108 total += r;
109 }
110
111 lseek(fd, 0, SEEK_SET);
112
Eddie James338748b2021-10-29 10:06:50 -0500113 if (!total)
114 {
115 // no error
116 return;
117 }
118
Eddie James2f9f9bb2021-09-20 14:26:31 -0500119 uint32_t src6 = instance << 16;
120 src6 |= *(data.get() + 2) << 8;
121 src6 |= *(data.get() + 3);
122
123 if (total > sbe_status_header_size)
124 {
125 std::string templateString =
126 fs::temp_directory_path() / "OCC_FFDC_XXXXXX";
127 tfd = mkostemp(templateString.data(), O_RDWR);
128 if (tfd < 0)
129 {
130 log<level::ERR>("Couldn't create temporary FFDC file");
131 }
132 else
133 {
134 temporaryFiles.emplace_back(templateString, tfd);
135 size_t written = sbe_status_header_size;
136 while (written < total)
137 {
138 auto r = write(tfd, data.get() + written, total - written);
139 if (r < 0)
140 {
141 close(temporaryFiles.back().second);
142 fs::remove(temporaryFiles.back().first);
143 temporaryFiles.pop_back();
144 tfd = -1;
145 log<level::ERR>("Couldn't write temporary FFDC file");
146 break;
147 }
148 if (!r)
149 {
150 break;
151 }
152 written += r;
153 }
154 }
155 }
156
157 createPEL("org.open_power.Processor.Error.SbeChipOpFailure", src6,
158 "SBE command reported error", tfd);
159}
160
161} // namespace occ
162} // namespace open_power