blob: 16ba9288393d1842d28e08e5e8ff1380f4690a9c [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
Matt Spinlerc180bc82023-09-12 09:29:54 -050012#include <org/open_power/Logging/PEL/server.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -060013#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +080014#include <sdbusplus/server.hpp>
15#include <xyz/openbmc_project/Logging/Entry/server.hpp>
16
Sampa Misra854e61f2019-08-22 04:36:47 -050017#include <exception>
18#include <filesystem>
Matt Spinler39d8c7a2020-03-05 11:28:59 -060019#include <fstream>
Sampa Misraaa8ae722019-12-12 03:20:40 -060020#include <iostream>
Sampa Misra854e61f2019-08-22 04:36:47 -050021#include <vector>
Sampa Misra854e61f2019-08-22 04:36:47 -050022
Riya Dixit49cfb132023-03-02 04:26:53 -060023PHOSPHOR_LOG2_USING;
24
Sampa Misra854e61f2019-08-22 04:36:47 -050025namespace pldm
26{
27namespace responder
28{
Matt Spinler39d8c7a2020-03-05 11:28:59 -060029using namespace sdbusplus::xyz::openbmc_project::Logging::server;
Matt Spinlerc180bc82023-09-12 09:29:54 -050030using namespace sdbusplus::org::open_power::Logging::server;
Matt Spinler39d8c7a2020-03-05 11:28:59 -060031
32namespace detail
33{
Matt Spinler39d8c7a2020-03-05 11:28:59 -060034/**
35 * @brief Finds the Entry::Level value for the severity of the PEL
36 * passed in.
37 *
38 * The severity byte is at offset 10 in the User Header section,
39 * which is always after the 48 byte Private Header section.
40 *
41 * @param[in] pelFileName - The file containing the PEL
42 *
43 * @return Entry::Level - The severity value for the Entry
44 */
45Entry::Level getEntryLevelFromPEL(const std::string& pelFileName)
46{
47 const std::map<uint8_t, Entry::Level> severityMap{
48 {0x00, Entry::Level::Informational}, // Informational event
49 {0x10, Entry::Level::Warning}, // Recoverable error
50 {0x20, Entry::Level::Warning}, // Predictive error
51 {0x40, Entry::Level::Error}, // Unrecoverable error
52 {0x50, Entry::Level::Error}, // Critical error
53 {0x60, Entry::Level::Error}, // Error from a diagnostic test
54 {0x70, Entry::Level::Warning} // Recoverable symptom
55 };
56
57 const size_t severityOffset = 0x3A;
58
59 size_t size = 0;
60 if (fs::exists(pelFileName))
61 {
62 size = fs::file_size(pelFileName);
63 }
64
65 if (size > severityOffset)
66 {
67 std::ifstream pel{pelFileName};
68 if (pel.good())
69 {
70 pel.seekg(severityOffset);
71
72 uint8_t sev;
73 pel.read(reinterpret_cast<char*>(&sev), 1);
74
75 // Get the type
76 sev = sev & 0xF0;
77
78 auto entry = severityMap.find(sev);
79 if (entry != severityMap.end())
80 {
81 return entry->second;
82 }
83 }
84 else
85 {
Riya Dixit49cfb132023-03-02 04:26:53 -060086 error("Unable to open PEL file {PEL_FILE_NAME}", "PEL_FILE_NAME",
87 pelFileName);
Matt Spinler39d8c7a2020-03-05 11:28:59 -060088 }
89 }
90
91 return Entry::Level::Error;
92}
93} // namespace detail
94
Deepak Kodihalli15211b42019-12-14 02:24:49 -060095int PelHandler::readIntoMemory(uint32_t offset, uint32_t& length,
Sampa Misra69508502020-09-08 00:08:21 -050096 uint64_t address,
97 oem_platform::Handler* /*oemPlatformHandler*/)
Deepak Kodihallif6d3a832019-11-19 07:00:29 -060098{
Deepak Kodihalli15211b42019-12-14 02:24:49 -060099 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
100 static constexpr auto logInterface = "org.open_power.Logging.PEL";
101
George Liu0e02c322020-01-01 09:41:51 +0800102 auto& bus = pldm::utils::DBusHandler::getBus();
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600103
104 try
105 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500106 auto service = pldm::utils::DBusHandler().getService(logObjPath,
107 logInterface);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600108 auto method = bus.new_method_call(service.c_str(), logObjPath,
109 logInterface, "GetPEL");
110 method.append(fileHandle);
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -0500111 auto reply = bus.call(
112 method,
113 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600114 sdbusplus::message::unix_fd fd{};
115 reply.read(fd);
116 auto rc = transferFileData(fd, true, offset, length, address);
117 return rc;
118 }
119 catch (const std::exception& e)
120 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600121 error(
122 "GetPEL D-Bus call failed, PEL id = 0x{FILE_HANDLE}, error ={ERR_EXCEP}",
123 "FILE_HANDLE", lg2::hex, fileHandle, "ERR_EXCEP", e.what());
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600124 return PLDM_ERROR;
125 }
126
127 return PLDM_SUCCESS;
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600128}
129
Sampa Misra69508502020-09-08 00:08:21 -0500130int PelHandler::read(uint32_t offset, uint32_t& length, Response& response,
131 oem_platform::Handler* /*oemPlatformHandler*/)
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600132{
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600133 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
134 static constexpr auto logInterface = "org.open_power.Logging.PEL";
George Liu0e02c322020-01-01 09:41:51 +0800135 auto& bus = pldm::utils::DBusHandler::getBus();
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600136
137 try
138 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500139 auto service = pldm::utils::DBusHandler().getService(logObjPath,
140 logInterface);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600141 auto method = bus.new_method_call(service.c_str(), logObjPath,
142 logInterface, "GetPEL");
143 method.append(fileHandle);
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -0500144 auto reply = bus.call(
145 method,
146 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600147 sdbusplus::message::unix_fd fd{};
148 reply.read(fd);
Pavithraba0738b2020-01-29 08:47:51 -0600149
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600150 off_t fileSize = lseek(fd, 0, SEEK_END);
151 if (fileSize == -1)
152 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600153 error("file seek failed");
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600154 return PLDM_ERROR;
155 }
156 if (offset >= fileSize)
157 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600158 error(
Sagar Srinivas82257922023-07-10 08:12:22 -0500159 "Offset exceeds file size, OFFSET={OFFSET} FILE_SIZE={FILE_SIZE} FILE_HANDLE{FILE_HANDLE}",
160 "OFFSET", offset, "FILE_SIZE", fileSize, "FILE_HANDLE",
161 fileHandle);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600162 return PLDM_DATA_OUT_OF_RANGE;
163 }
164 if (offset + length > fileSize)
165 {
166 length = fileSize - offset;
167 }
168 auto rc = lseek(fd, offset, SEEK_SET);
169 if (rc == -1)
170 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600171 error("file seek failed");
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600172 return PLDM_ERROR;
173 }
174 size_t currSize = response.size();
175 response.resize(currSize + length);
176 auto filePos = reinterpret_cast<char*>(response.data());
177 filePos += currSize;
178 rc = ::read(fd, filePos, length);
179 if (rc == -1)
180 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600181 error("file read failed");
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600182 return PLDM_ERROR;
183 }
184 if (rc != length)
185 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600186 error(
187 "mismatch between number of characters to read and the length read, LENGTH={LEN} COUNT={CNT}",
188 "LEN", length, "CNT", rc);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600189 return PLDM_ERROR;
190 }
191 }
192 catch (const std::exception& e)
193 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600194 error(
195 "GetPEL D-Bus call failed on PEL ID 0x{FILE_HANDLE}, error ={ERR_EXCEP}",
196 "FILE_HANDLE", lg2::hex, fileHandle, "ERR_EXCEP", e.what());
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600197 return PLDM_ERROR;
198 }
199 return PLDM_SUCCESS;
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600200}
201
Sampa Misra854e61f2019-08-22 04:36:47 -0500202int PelHandler::writeFromMemory(uint32_t offset, uint32_t length,
Sampa Misra69508502020-09-08 00:08:21 -0500203 uint64_t address,
204 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra854e61f2019-08-22 04:36:47 -0500205{
Sampa Misraf5087a42019-12-03 04:51:36 -0600206 char tmpFile[] = "/tmp/pel.XXXXXX";
207 int fd = mkstemp(tmpFile);
208 if (fd == -1)
209 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600210 error("failed to create a temporary pel, ERROR={ERR}", "ERR", errno);
Sampa Misraf5087a42019-12-03 04:51:36 -0600211 return PLDM_ERROR;
212 }
213 close(fd);
214 fs::path path(tmpFile);
Sampa Misra854e61f2019-08-22 04:36:47 -0500215
216 auto rc = transferFileData(path, false, offset, length, address);
217 if (rc == PLDM_SUCCESS)
218 {
219 rc = storePel(path.string());
220 }
Sampa Misra854e61f2019-08-22 04:36:47 -0500221 return rc;
222}
223
Matt Spinlerc180bc82023-09-12 09:29:54 -0500224int PelHandler::fileAck(uint8_t fileStatus)
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600225{
226 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
227 static constexpr auto logInterface = "org.open_power.Logging.PEL";
Matt Spinlerc180bc82023-09-12 09:29:54 -0500228 static std::string service;
George Liu0e02c322020-01-01 09:41:51 +0800229 auto& bus = pldm::utils::DBusHandler::getBus();
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600230
Matt Spinlerc180bc82023-09-12 09:29:54 -0500231 if (service.empty())
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600232 {
Matt Spinlerc180bc82023-09-12 09:29:54 -0500233 try
234 {
235 service = pldm::utils::DBusHandler().getService(logObjPath,
236 logInterface);
237 }
238 catch (const sdbusplus::exception_t& e)
239 {
240 error("Mapper call failed when trying to find logging service "
241 "to ack PEL ID {FILE_HANDLE} error = {ERR_EXCEP}",
242 "FILE_HANDLE", lg2::hex, fileHandle, "ERR_EXCEP", e);
243 return PLDM_ERROR;
244 }
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600245 }
Matt Spinlerc180bc82023-09-12 09:29:54 -0500246
247 if (fileStatus == PLDM_SUCCESS)
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600248 {
Matt Spinlerc180bc82023-09-12 09:29:54 -0500249 try
250 {
251 auto method = bus.new_method_call(service.c_str(), logObjPath,
252 logInterface, "HostAck");
253 method.append(fileHandle);
254 bus.call_noreply(method);
255 }
256 catch (const std::exception& e)
257 {
258 error(
259 "HostAck D-Bus call failed on PEL ID {FILE_HANDLE}, error = {ERR_EXCEP}",
260 "FILE_HANDLE", lg2::hex, fileHandle, "ERR_EXCEP", e);
261 return PLDM_ERROR;
262 }
263 }
264 else
265 {
266 PEL::RejectionReason reason{};
267 if (fileStatus == PLDM_FULL_FILE_DISCARDED)
268 {
269 reason = PEL::RejectionReason::HostFull;
270 }
271 else if (fileStatus == PLDM_ERROR_FILE_DISCARDED)
272 {
273 reason = PEL::RejectionReason::BadPEL;
274 }
275 else
276 {
277 error(
278 "Invalid file status {STATUS} in PEL file ack response for PEL {FILE_HANDLE}",
279 "STATUS", lg2::hex, fileStatus, "FILE_HANDLE", lg2::hex,
280 fileHandle);
281 return PLDM_ERROR;
282 }
283
284 try
285 {
286 auto method = bus.new_method_call(service.c_str(), logObjPath,
287 logInterface, "HostReject");
288 method.append(fileHandle, reason);
289 bus.call_noreply(method);
290 }
291 catch (const std::exception& e)
292 {
293 error("HostReject D-Bus call failed on PEL ID {FILE_HANDLE}, "
294 "error = {ERR_EXCEP}, status = {STATUS}",
295 "FILE_HANDLE", lg2::hex, fileHandle, "ERR_EXCEP", e, "STATUS",
296 lg2::hex, fileStatus);
297 return PLDM_ERROR;
298 }
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600299 }
300
301 return PLDM_SUCCESS;
302}
303
Sampa Misra854e61f2019-08-22 04:36:47 -0500304int PelHandler::storePel(std::string&& pelFileName)
305{
306 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
307 static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create";
308
George Liu0e02c322020-01-01 09:41:51 +0800309 auto& bus = pldm::utils::DBusHandler::getBus();
Sampa Misra854e61f2019-08-22 04:36:47 -0500310
311 try
312 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500313 auto service = pldm::utils::DBusHandler().getService(logObjPath,
314 logInterface);
Sampa Misra854e61f2019-08-22 04:36:47 -0500315 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
316 std::map<std::string, std::string> addlData{};
Sampa Misra854e61f2019-08-22 04:36:47 -0500317 auto severity =
318 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
Matt Spinler39d8c7a2020-03-05 11:28:59 -0600319 detail::getEntryLevelFromPEL(pelFileName));
320 addlData.emplace("RAWPEL", std::move(pelFileName));
Sampa Misra854e61f2019-08-22 04:36:47 -0500321
322 auto method = bus.new_method_call(service.c_str(), logObjPath,
323 logInterface, "Create");
324 method.append("xyz.openbmc_project.Host.Error.Event", severity,
325 addlData);
326 bus.call_noreply(method);
327 }
328 catch (const std::exception& e)
329 {
Sagar Srinivas90403472023-08-21 06:08:55 -0500330 error(
331 "failed to make a d-bus call to PEL daemon, PEL_FILE_NAME={PEL_FILE_NAME), ERROR={ERR_EXCEP}",
332 "PEL_FILE_NAME", pelFileName, "ERR_EXCEP", e.what());
Sampa Misra854e61f2019-08-22 04:36:47 -0500333 return PLDM_ERROR;
334 }
335
336 return PLDM_SUCCESS;
337}
338
George Liud37b4952021-06-28 15:14:29 +0800339int PelHandler::write(const char* buffer, uint32_t offset, uint32_t& length,
340 oem_platform::Handler* /*oemPlatformHandler*/)
341{
342 int rc = PLDM_SUCCESS;
343
344 if (offset > 0)
345 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600346 error("Offset is non zero");
George Liud37b4952021-06-28 15:14:29 +0800347 return PLDM_ERROR;
348 }
349
350 char tmpFile[] = "/tmp/pel.XXXXXX";
351 auto fd = mkstemp(tmpFile);
352 if (fd == -1)
353 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600354 error("failed to create a temporary pel, ERROR={ERR}", "ERR", errno);
George Liud37b4952021-06-28 15:14:29 +0800355 return PLDM_ERROR;
356 }
357
358 size_t written = 0;
359 do
360 {
361 if ((rc = ::write(fd, buffer, length - written)) == -1)
362 {
363 break;
364 }
365 written += rc;
366 buffer += rc;
367 } while (rc && written < length);
368 close(fd);
369
370 if (rc == -1)
371 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600372 error("file write failed, ERROR={ERR}, LENGTH={LEN}, OFFSET={OFFSET}",
373 "ERR", errno, "LEN", length, "OFFSET", offset);
George Liud37b4952021-06-28 15:14:29 +0800374 fs::remove(tmpFile);
375 return PLDM_ERROR;
376 }
377
378 if (written == length)
379 {
380 fs::path path(tmpFile);
381 rc = storePel(path.string());
382 if (rc != PLDM_SUCCESS)
383 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600384 error("save PEL failed, ERROR = {RC} tmpFile = {TMP_FILE}", "RC",
385 rc, "TMP_FILE", tmpFile);
George Liud37b4952021-06-28 15:14:29 +0800386 }
387 }
388
389 return rc;
390}
391
Sampa Misra854e61f2019-08-22 04:36:47 -0500392} // namespace responder
393} // namespace pldm