blob: 1783c776ef858228bcad63157674c110b217842a [file] [log] [blame]
Sampa Misra854e61f2019-08-22 04:36:47 -05001#include "file_io_type_pel.hpp"
2
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05003#include "common/utils.hpp"
Sampa Misra854e61f2019-08-22 04:36:47 -05004#include "xyz/openbmc_project/Common/error.hpp"
5
George Liuc453e162022-12-21 17:16:23 +08006#include <libpldm/base.h>
7#include <libpldm/file_io.h>
Sampa Misra854e61f2019-08-22 04:36:47 -05008#include <stdint.h>
9#include <systemd/sd-bus.h>
10#include <unistd.h>
11
Riya Dixit49cfb132023-03-02 04:26:53 -060012#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +080013#include <sdbusplus/server.hpp>
14#include <xyz/openbmc_project/Logging/Entry/server.hpp>
15
Sampa Misra854e61f2019-08-22 04:36:47 -050016#include <exception>
17#include <filesystem>
Matt Spinler39d8c7a2020-03-05 11:28:59 -060018#include <fstream>
Sampa Misraaa8ae722019-12-12 03:20:40 -060019#include <iostream>
Sampa Misra854e61f2019-08-22 04:36:47 -050020#include <vector>
Sampa Misra854e61f2019-08-22 04:36:47 -050021
Riya Dixit49cfb132023-03-02 04:26:53 -060022PHOSPHOR_LOG2_USING;
23
Sampa Misra854e61f2019-08-22 04:36:47 -050024namespace pldm
25{
26namespace responder
27{
Matt Spinler39d8c7a2020-03-05 11:28:59 -060028using namespace sdbusplus::xyz::openbmc_project::Logging::server;
29
30namespace detail
31{
Matt Spinler39d8c7a2020-03-05 11:28:59 -060032/**
33 * @brief Finds the Entry::Level value for the severity of the PEL
34 * passed in.
35 *
36 * The severity byte is at offset 10 in the User Header section,
37 * which is always after the 48 byte Private Header section.
38 *
39 * @param[in] pelFileName - The file containing the PEL
40 *
41 * @return Entry::Level - The severity value for the Entry
42 */
43Entry::Level getEntryLevelFromPEL(const std::string& pelFileName)
44{
45 const std::map<uint8_t, Entry::Level> severityMap{
46 {0x00, Entry::Level::Informational}, // Informational event
47 {0x10, Entry::Level::Warning}, // Recoverable error
48 {0x20, Entry::Level::Warning}, // Predictive error
49 {0x40, Entry::Level::Error}, // Unrecoverable error
50 {0x50, Entry::Level::Error}, // Critical error
51 {0x60, Entry::Level::Error}, // Error from a diagnostic test
52 {0x70, Entry::Level::Warning} // Recoverable symptom
53 };
54
55 const size_t severityOffset = 0x3A;
56
57 size_t size = 0;
58 if (fs::exists(pelFileName))
59 {
60 size = fs::file_size(pelFileName);
61 }
62
63 if (size > severityOffset)
64 {
65 std::ifstream pel{pelFileName};
66 if (pel.good())
67 {
68 pel.seekg(severityOffset);
69
70 uint8_t sev;
71 pel.read(reinterpret_cast<char*>(&sev), 1);
72
73 // Get the type
74 sev = sev & 0xF0;
75
76 auto entry = severityMap.find(sev);
77 if (entry != severityMap.end())
78 {
79 return entry->second;
80 }
81 }
82 else
83 {
Riya Dixit49cfb132023-03-02 04:26:53 -060084 error("Unable to open PEL file {PEL_FILE_NAME}", "PEL_FILE_NAME",
85 pelFileName);
Matt Spinler39d8c7a2020-03-05 11:28:59 -060086 }
87 }
88
89 return Entry::Level::Error;
90}
91} // namespace detail
92
Deepak Kodihalli15211b42019-12-14 02:24:49 -060093int PelHandler::readIntoMemory(uint32_t offset, uint32_t& length,
Sampa Misra69508502020-09-08 00:08:21 -050094 uint64_t address,
95 oem_platform::Handler* /*oemPlatformHandler*/)
Deepak Kodihallif6d3a832019-11-19 07:00:29 -060096{
Deepak Kodihalli15211b42019-12-14 02:24:49 -060097 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
98 static constexpr auto logInterface = "org.open_power.Logging.PEL";
99
George Liu0e02c322020-01-01 09:41:51 +0800100 auto& bus = pldm::utils::DBusHandler::getBus();
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600101
102 try
103 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500104 auto service = pldm::utils::DBusHandler().getService(logObjPath,
105 logInterface);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600106 auto method = bus.new_method_call(service.c_str(), logObjPath,
107 logInterface, "GetPEL");
108 method.append(fileHandle);
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -0500109 auto reply = bus.call(
110 method,
111 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600112 sdbusplus::message::unix_fd fd{};
113 reply.read(fd);
114 auto rc = transferFileData(fd, true, offset, length, address);
115 return rc;
116 }
117 catch (const std::exception& e)
118 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600119 error(
120 "GetPEL D-Bus call failed, PEL id = 0x{FILE_HANDLE}, error ={ERR_EXCEP}",
121 "FILE_HANDLE", lg2::hex, fileHandle, "ERR_EXCEP", e.what());
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600122 return PLDM_ERROR;
123 }
124
125 return PLDM_SUCCESS;
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600126}
127
Sampa Misra69508502020-09-08 00:08:21 -0500128int PelHandler::read(uint32_t offset, uint32_t& length, Response& response,
129 oem_platform::Handler* /*oemPlatformHandler*/)
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600130{
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600131 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
132 static constexpr auto logInterface = "org.open_power.Logging.PEL";
George Liu0e02c322020-01-01 09:41:51 +0800133 auto& bus = pldm::utils::DBusHandler::getBus();
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600134
135 try
136 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500137 auto service = pldm::utils::DBusHandler().getService(logObjPath,
138 logInterface);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600139 auto method = bus.new_method_call(service.c_str(), logObjPath,
140 logInterface, "GetPEL");
141 method.append(fileHandle);
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -0500142 auto reply = bus.call(
143 method,
144 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600145 sdbusplus::message::unix_fd fd{};
146 reply.read(fd);
Pavithraba0738b2020-01-29 08:47:51 -0600147
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600148 off_t fileSize = lseek(fd, 0, SEEK_END);
149 if (fileSize == -1)
150 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600151 error("file seek failed");
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600152 return PLDM_ERROR;
153 }
154 if (offset >= fileSize)
155 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600156 error(
157 "Offset exceeds file size, OFFSET={OFFSET} FILE_SIZE={FILE_SIZE}",
158 "OFFSET", offset, "FILE_SIZE", fileSize);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600159 return PLDM_DATA_OUT_OF_RANGE;
160 }
161 if (offset + length > fileSize)
162 {
163 length = fileSize - offset;
164 }
165 auto rc = lseek(fd, offset, SEEK_SET);
166 if (rc == -1)
167 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600168 error("file seek failed");
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600169 return PLDM_ERROR;
170 }
171 size_t currSize = response.size();
172 response.resize(currSize + length);
173 auto filePos = reinterpret_cast<char*>(response.data());
174 filePos += currSize;
175 rc = ::read(fd, filePos, length);
176 if (rc == -1)
177 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600178 error("file read failed");
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600179 return PLDM_ERROR;
180 }
181 if (rc != length)
182 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600183 error(
184 "mismatch between number of characters to read and the length read, LENGTH={LEN} COUNT={CNT}",
185 "LEN", length, "CNT", rc);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600186 return PLDM_ERROR;
187 }
188 }
189 catch (const std::exception& e)
190 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600191 error(
192 "GetPEL D-Bus call failed on PEL ID 0x{FILE_HANDLE}, error ={ERR_EXCEP}",
193 "FILE_HANDLE", lg2::hex, fileHandle, "ERR_EXCEP", e.what());
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600194 return PLDM_ERROR;
195 }
196 return PLDM_SUCCESS;
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600197}
198
Sampa Misra854e61f2019-08-22 04:36:47 -0500199int PelHandler::writeFromMemory(uint32_t offset, uint32_t length,
Sampa Misra69508502020-09-08 00:08:21 -0500200 uint64_t address,
201 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra854e61f2019-08-22 04:36:47 -0500202{
Sampa Misraf5087a42019-12-03 04:51:36 -0600203 char tmpFile[] = "/tmp/pel.XXXXXX";
204 int fd = mkstemp(tmpFile);
205 if (fd == -1)
206 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600207 error("failed to create a temporary pel, ERROR={ERR}", "ERR", errno);
Sampa Misraf5087a42019-12-03 04:51:36 -0600208 return PLDM_ERROR;
209 }
210 close(fd);
211 fs::path path(tmpFile);
Sampa Misra854e61f2019-08-22 04:36:47 -0500212
213 auto rc = transferFileData(path, false, offset, length, address);
214 if (rc == PLDM_SUCCESS)
215 {
216 rc = storePel(path.string());
217 }
Sampa Misra854e61f2019-08-22 04:36:47 -0500218 return rc;
219}
220
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600221int PelHandler::fileAck(uint8_t /*fileStatus*/)
222{
223 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
224 static constexpr auto logInterface = "org.open_power.Logging.PEL";
George Liu0e02c322020-01-01 09:41:51 +0800225 auto& bus = pldm::utils::DBusHandler::getBus();
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600226
227 try
228 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500229 auto service = pldm::utils::DBusHandler().getService(logObjPath,
230 logInterface);
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600231 auto method = bus.new_method_call(service.c_str(), logObjPath,
232 logInterface, "HostAck");
233 method.append(fileHandle);
234 bus.call_noreply(method);
235 }
236 catch (const std::exception& e)
237 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600238 error(
239 "HostAck D-Bus call failed on PEL ID 0x{FILE_HANDLE}, error ={ERR_EXCEP}",
240 "FILE_HANDLE", lg2::hex, fileHandle, "ERR_EXCEP", e.what());
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600241 return PLDM_ERROR;
242 }
243
244 return PLDM_SUCCESS;
245}
246
Sampa Misra854e61f2019-08-22 04:36:47 -0500247int PelHandler::storePel(std::string&& pelFileName)
248{
249 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
250 static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create";
251
George Liu0e02c322020-01-01 09:41:51 +0800252 auto& bus = pldm::utils::DBusHandler::getBus();
Sampa Misra854e61f2019-08-22 04:36:47 -0500253
254 try
255 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500256 auto service = pldm::utils::DBusHandler().getService(logObjPath,
257 logInterface);
Sampa Misra854e61f2019-08-22 04:36:47 -0500258 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
259 std::map<std::string, std::string> addlData{};
Sampa Misra854e61f2019-08-22 04:36:47 -0500260 auto severity =
261 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
Matt Spinler39d8c7a2020-03-05 11:28:59 -0600262 detail::getEntryLevelFromPEL(pelFileName));
263 addlData.emplace("RAWPEL", std::move(pelFileName));
Sampa Misra854e61f2019-08-22 04:36:47 -0500264
265 auto method = bus.new_method_call(service.c_str(), logObjPath,
266 logInterface, "Create");
267 method.append("xyz.openbmc_project.Host.Error.Event", severity,
268 addlData);
269 bus.call_noreply(method);
270 }
271 catch (const std::exception& e)
272 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600273 error("failed to make a d-bus call to PEL daemon, ERROR={ERR_EXCEP}",
274 "ERR_EXCEP", e.what());
Sampa Misra854e61f2019-08-22 04:36:47 -0500275 return PLDM_ERROR;
276 }
277
278 return PLDM_SUCCESS;
279}
280
George Liud37b4952021-06-28 15:14:29 +0800281int PelHandler::write(const char* buffer, uint32_t offset, uint32_t& length,
282 oem_platform::Handler* /*oemPlatformHandler*/)
283{
284 int rc = PLDM_SUCCESS;
285
286 if (offset > 0)
287 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600288 error("Offset is non zero");
George Liud37b4952021-06-28 15:14:29 +0800289 return PLDM_ERROR;
290 }
291
292 char tmpFile[] = "/tmp/pel.XXXXXX";
293 auto fd = mkstemp(tmpFile);
294 if (fd == -1)
295 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600296 error("failed to create a temporary pel, ERROR={ERR}", "ERR", errno);
George Liud37b4952021-06-28 15:14:29 +0800297 return PLDM_ERROR;
298 }
299
300 size_t written = 0;
301 do
302 {
303 if ((rc = ::write(fd, buffer, length - written)) == -1)
304 {
305 break;
306 }
307 written += rc;
308 buffer += rc;
309 } while (rc && written < length);
310 close(fd);
311
312 if (rc == -1)
313 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600314 error("file write failed, ERROR={ERR}, LENGTH={LEN}, OFFSET={OFFSET}",
315 "ERR", errno, "LEN", length, "OFFSET", offset);
George Liud37b4952021-06-28 15:14:29 +0800316 fs::remove(tmpFile);
317 return PLDM_ERROR;
318 }
319
320 if (written == length)
321 {
322 fs::path path(tmpFile);
323 rc = storePel(path.string());
324 if (rc != PLDM_SUCCESS)
325 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600326 error("save PEL failed, ERROR = {RC} tmpFile = {TMP_FILE}", "RC",
327 rc, "TMP_FILE", tmpFile);
George Liud37b4952021-06-28 15:14:29 +0800328 }
329 }
330
331 return rc;
332}
333
Sampa Misra854e61f2019-08-22 04:36:47 -0500334} // namespace responder
335} // namespace pldm