blob: f1c6bec0182147366ce5fd2ab21daf06c41174c1 [file] [log] [blame]
Sampa Misra18967162020-01-14 02:31:41 -06001#include "file_io_type_dump.hpp"
2
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05003#include "common/utils.hpp"
Ravi Tejace1c96f2020-10-05 23:13:01 -05004#include "utils.hpp"
Sampa Misra18967162020-01-14 02:31:41 -06005#include "xyz/openbmc_project/Common/error.hpp"
6
George Liuc453e162022-12-21 17:16:23 +08007#include <libpldm/base.h>
Andrew Jeffery21f128d2024-01-15 15:34:26 +10308#include <libpldm/oem/ibm/file_io.h>
Sampa Misra18967162020-01-14 02:31:41 -06009#include <stdint.h>
10#include <systemd/sd-bus.h>
11#include <unistd.h>
12
Riya Dixit49cfb132023-03-02 04:26:53 -060013#include <phosphor-logging/lg2.hpp>
Sampa Misra18967162020-01-14 02:31:41 -060014#include <sdbusplus/server.hpp>
15#include <xyz/openbmc_project/Dump/NewDump/server.hpp>
16
George Liu6492f522020-06-16 10:34:05 +080017#include <exception>
18#include <filesystem>
Jayashankar Padathdb124362021-01-28 21:12:34 -060019#include <type_traits>
Sampa Misra18967162020-01-14 02:31:41 -060020
Riya Dixit49cfb132023-03-02 04:26:53 -060021PHOSPHOR_LOG2_USING;
22
Ravi Tejace1c96f2020-10-05 23:13:01 -050023using namespace pldm::responder::utils;
Deepak Kodihallifd279e12020-02-02 05:20:43 -060024using namespace pldm::utils;
25
Sampa Misra18967162020-01-14 02:31:41 -060026namespace pldm
27{
28namespace responder
29{
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050030static constexpr auto dumpEntry = "xyz.openbmc_project.Dump.Entry";
Deepak Kodihalli14b54a32020-09-29 05:20:02 -050031static constexpr auto dumpObjPath = "/xyz/openbmc_project/dump/system";
Jayashankar Padathdb124362021-01-28 21:12:34 -060032static constexpr auto systemDumpEntry = "xyz.openbmc_project.Dump.Entry.System";
33static constexpr auto resDumpObjPath = "/xyz/openbmc_project/dump/resource";
34static constexpr auto resDumpEntry = "com.ibm.Dump.Entry.Resource";
Sampa Misra18967162020-01-14 02:31:41 -060035
Jayashankar Padathdb124362021-01-28 21:12:34 -060036// Resource dump file path to be deleted once hyperviosr validates the input
37// parameters. Need to re-look in to this name when we support multiple
38// resource dumps.
39static constexpr auto resDumpDirPath = "/var/lib/pldm/resourcedump/1";
40
41int DumpHandler::fd = -1;
42namespace fs = std::filesystem;
43
44std::string DumpHandler::findDumpObjPath(uint32_t fileHandle)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050045{
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050046 static constexpr auto DUMP_MANAGER_BUSNAME =
47 "xyz.openbmc_project.Dump.Manager";
48 static constexpr auto DUMP_MANAGER_PATH = "/xyz/openbmc_project/dump";
49 static constexpr auto OBJECT_MANAGER_INTERFACE =
50 "org.freedesktop.DBus.ObjectManager";
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050051 auto& bus = pldm::utils::DBusHandler::getBus();
52
Jayashankar Padathdb124362021-01-28 21:12:34 -060053 // Stores the current resource dump entry path
54 std::string curResDumpEntryPath{};
55
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050056 dbus::ObjectValueTree objects;
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050057 // Select the dump entry interface for system dump or resource dump
58 DumpEntryInterface dumpEntryIntf = systemDumpEntry;
59 if ((dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP) ||
60 (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS))
61 {
62 dumpEntryIntf = resDumpEntry;
63 }
64
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050065 try
66 {
Archana Kakani040c6182023-02-20 07:01:51 -060067 auto method =
68 bus.new_method_call(DUMP_MANAGER_BUSNAME, DUMP_MANAGER_PATH,
69 OBJECT_MANAGER_INTERFACE, "GetManagedObjects");
70
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -050071 auto reply = bus.call(method, dbusTimeout);
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050072 reply.read(objects);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050073 }
Archana Kakani040c6182023-02-20 07:01:51 -060074 catch (const sdbusplus::exception_t& e)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050075 {
Riya Dixit49cfb132023-03-02 04:26:53 -060076 error(
77 "findDumpObjPath: Error {ERR_EXCEP} found with GetManagedObjects call in findDumpObjPath with objPath={OBJ_PATH} and intf={DUMP_INFT}",
78 "ERR_EXCEP", e.what(), "OBJ_PATH", DUMP_MANAGER_PATH, "DUMP_INFT",
79 dumpEntryIntf);
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050080 return curResDumpEntryPath;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050081 }
82
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050083 for (const auto& object : objects)
84 {
85 for (const auto& interface : object.second)
86 {
87 if (interface.first != dumpEntryIntf)
88 {
89 continue;
90 }
91
92 for (auto& propertyMap : interface.second)
93 {
94 if (propertyMap.first == "SourceDumpId")
95 {
96 auto dumpIdPtr = std::get_if<uint32_t>(&propertyMap.second);
97 if (dumpIdPtr != nullptr)
98 {
99 auto dumpId = *dumpIdPtr;
100 if (fileHandle == dumpId)
101 {
102 curResDumpEntryPath = object.first.str;
103 return curResDumpEntryPath;
104 }
105 }
106 else
107 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600108 error(
109 "Invalid SourceDumpId in curResDumpEntryPath {CUR_RES_DUMP_PATH} but continuing with next entry for a match...",
110 "CUR_RES_DUMP_PATH", curResDumpEntryPath);
Jayashankar Padathe7cc8692022-09-06 13:26:22 -0500111 }
112 }
113 }
114 }
115 }
Jayashankar Padathdb124362021-01-28 21:12:34 -0600116 return curResDumpEntryPath;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500117}
118
Sampa Misra18967162020-01-14 02:31:41 -0600119int DumpHandler::newFileAvailable(uint64_t length)
120{
Sampa Misra18967162020-01-14 02:31:41 -0600121 static constexpr auto dumpInterface = "xyz.openbmc_project.Dump.NewDump";
Sampa Misra18967162020-01-14 02:31:41 -0600122 auto& bus = pldm::utils::DBusHandler::getBus();
123
Jayashankar Padathdb124362021-01-28 21:12:34 -0600124 auto notifyObjPath = dumpObjPath;
125 if (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP)
126 {
127 // Setting the Notify path for resource dump
128 notifyObjPath = resDumpObjPath;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600129 }
130
Sampa Misra18967162020-01-14 02:31:41 -0600131 try
132 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500133 auto service = pldm::utils::DBusHandler().getService(notifyObjPath,
134 dumpInterface);
Sampa Misra18967162020-01-14 02:31:41 -0600135 using namespace sdbusplus::xyz::openbmc_project::Dump::server;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600136 auto method = bus.new_method_call(service.c_str(), notifyObjPath,
Sampa Misra18967162020-01-14 02:31:41 -0600137 dumpInterface, "Notify");
Dhruvaraj Subhashchandran41989eb2020-11-27 00:22:42 -0600138 method.append(fileHandle, length);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000139 bus.call_noreply(method, dbusTimeout);
Sampa Misra18967162020-01-14 02:31:41 -0600140 }
141 catch (const std::exception& e)
142 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600143 error(
144 "newFileAvailable: Error {ERR_EXCEP} found while notifying new dump to dump manager with objPath={OBJ_PATH} and intf={DUMP_INTF}",
145 "ERR_EXCEP", e.what(), "OBJ_PATH", notifyObjPath, "DUMP_INTF",
146 dumpInterface);
Sampa Misra18967162020-01-14 02:31:41 -0600147 return PLDM_ERROR;
148 }
149
150 return PLDM_SUCCESS;
151}
152
Jayashankar Padathdb124362021-01-28 21:12:34 -0600153std::string DumpHandler::getOffloadUri(uint32_t fileHandle)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500154{
155 auto path = findDumpObjPath(fileHandle);
156 if (path.empty())
157 {
158 return {};
159 }
160
Ravi Tejace1c96f2020-10-05 23:13:01 -0500161 std::string socketInterface{};
162
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500163 try
164 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500165 socketInterface =
166 pldm::utils::DBusHandler().getDbusProperty<std::string>(
167 path.c_str(), "OffloadUri", dumpEntry);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500168 }
169 catch (const std::exception& e)
170 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600171 error(
172 "getOffloadUri: Error {ERR_EXCEP} found while fetching the dump offload URI with objPath={OBJ_PATH} and intf={SOCKET_INTF}",
173 "ERR_EXCEP", e.what(), "OBJ_PATH", path.c_str(), "SOCKET_INTF",
174 socketInterface);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500175 }
176
Ravi Tejace1c96f2020-10-05 23:13:01 -0500177 return socketInterface;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500178}
179
Jayashankar Padathdb124362021-01-28 21:12:34 -0600180int DumpHandler::writeFromMemory(uint32_t, uint32_t length, uint64_t address,
Sampa Misra69508502020-09-08 00:08:21 -0500181 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra18967162020-01-14 02:31:41 -0600182{
Sampa Misra18967162020-01-14 02:31:41 -0600183 if (DumpHandler::fd == -1)
184 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500185 auto socketInterface = getOffloadUri(fileHandle);
186 int sock = setupUnixSocket(socketInterface);
187 if (sock < 0)
Sampa Misra18967162020-01-14 02:31:41 -0600188 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500189 sock = -errno;
190 close(DumpHandler::fd);
Pavithra Barithaya5e3a2472023-07-10 08:43:59 -0500191 error(
192 "DumpHandler::writeFromMemory: setupUnixSocket() failed ERR={ERR_NO}",
193 "ERR_NO", sock);
Ravi Tejace1c96f2020-10-05 23:13:01 -0500194 std::remove(socketInterface.c_str());
Sampa Misra18967162020-01-14 02:31:41 -0600195 return PLDM_ERROR;
196 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500197
Ravi Tejace1c96f2020-10-05 23:13:01 -0500198 DumpHandler::fd = sock;
199 }
200 return transferFileDataToSocket(DumpHandler::fd, length, address);
Sampa Misra18967162020-01-14 02:31:41 -0600201}
202
Jayashankar Padathdb124362021-01-28 21:12:34 -0600203int DumpHandler::write(const char* buffer, uint32_t, uint32_t& length,
Sampa Misra69508502020-09-08 00:08:21 -0500204 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra18967162020-01-14 02:31:41 -0600205{
Ravi Tejace1c96f2020-10-05 23:13:01 -0500206 int rc = writeToUnixSocket(DumpHandler::fd, buffer, length);
207 if (rc < 0)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500208 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500209 rc = -errno;
210 close(DumpHandler::fd);
211 auto socketInterface = getOffloadUri(fileHandle);
212 std::remove(socketInterface.c_str());
Pavithra Barithaya5e3a2472023-07-10 08:43:59 -0500213 error("DumpHandler::write: writeToUnixSocket() failed ERR={RC_VAL}",
214 "RC_VAL", rc);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500215 return PLDM_ERROR;
216 }
217
Sampa Misra18967162020-01-14 02:31:41 -0600218 return PLDM_SUCCESS;
219}
220
Jayashankar Padathdb124362021-01-28 21:12:34 -0600221int DumpHandler::fileAck(uint8_t fileStatus)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600222{
Jayashankar Padathdb124362021-01-28 21:12:34 -0600223 auto path = findDumpObjPath(fileHandle);
224 if (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600225 {
Jayashankar Padathdb124362021-01-28 21:12:34 -0600226 if (fileStatus != PLDM_SUCCESS)
227 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600228 error("Failue in resource dump file ack");
Jayashankar Padathdb124362021-01-28 21:12:34 -0600229 pldm::utils::reportError(
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600230 "xyz.openbmc_project.PLDM.Error.fileAck.ResourceDumpFileAckFail",
231 pldm::PelSeverity::Informational);
Jayashankar Padathdb124362021-01-28 21:12:34 -0600232
233 PropertyValue value{
234 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed"};
235 DBusMapping dbusMapping{path, "xyz.openbmc_project.Common.Progress",
236 "Status", "string"};
237 try
238 {
239 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
240 }
241 catch (const std::exception& e)
242 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600243 error(
244 "fileAck: Error {ERR_EXCEP} found while setting the dump progress status as Failed with objPath={OBJ_PATH} and intf=Common.Progress",
245 "ERR_EXCEP", e.what(), "OBJ_PATH", path.c_str());
Jayashankar Padathdb124362021-01-28 21:12:34 -0600246 }
247 }
248
249 if (fs::exists(resDumpDirPath))
250 {
251 fs::remove_all(resDumpDirPath);
252 }
253 return PLDM_SUCCESS;
254 }
255
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500256 if (!path.empty())
Jayashankar Padathdb124362021-01-28 21:12:34 -0600257 {
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500258 if (fileStatus == PLDM_ERROR_FILE_DISCARDED)
259 {
260 uint32_t val = 0xFFFFFFFF;
261 PropertyValue value = static_cast<uint32_t>(val);
262 auto dumpIntf = resDumpEntry;
263
264 if (dumpType == PLDM_FILE_TYPE_DUMP)
265 {
266 dumpIntf = systemDumpEntry;
267 }
268
269 DBusMapping dbusMapping{path.c_str(), dumpIntf, "SourceDumpId",
270 "uint32_t"};
271 try
272 {
273 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
274 }
275 catch (const std::exception& e)
276 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600277 error(
278 "fileAck: Failed to make a d-bus call to DUMP manager to reset source dump id of {FILE_PATH}, with ERROR={ERR_EXCEP}",
279 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500280 pldm::utils::reportError(
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600281 "xyz.openbmc_project.PLDM.Error.fileAck.SourceDumpIdResetFail");
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500282 return PLDM_ERROR;
283 }
284
285 auto& bus = pldm::utils::DBusHandler::getBus();
286 try
287 {
288 auto method = bus.new_method_call(
289 "xyz.openbmc_project.Dump.Manager", path.c_str(),
290 "xyz.openbmc_project.Object.Delete", "Delete");
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500291 bus.call(method, dbusTimeout);
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500292 }
293 catch (const std::exception& e)
294 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600295 error(
296 "fileAck: Failed to make a d-bus method to delete the dump entry {FILE_PATH}, with ERROR={ERR_EXCEP}",
297 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500298 pldm::utils::reportError(
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600299 "xyz.openbmc_project.PLDM.Error.fileAck.DumpEntryDeleteFail");
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500300 return PLDM_ERROR;
301 }
302 return PLDM_SUCCESS;
303 }
304
Jayashankar Padathdb124362021-01-28 21:12:34 -0600305 if (dumpType == PLDM_FILE_TYPE_DUMP ||
306 dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600307 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500308 PropertyValue value{true};
309 DBusMapping dbusMapping{path, dumpEntry, "Offloaded", "bool"};
310 try
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600311 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500312 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600313 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500314 catch (const std::exception& e)
315 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600316 error(
317 "fileAck: Failed to make a d-bus method to set the dump offloaded property to true with path={FILE_PATH} and with ERROR={ERR_EXCEP}",
318 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500319 }
Ravi Tejace1c96f2020-10-05 23:13:01 -0500320
Ravi Tejace1c96f2020-10-05 23:13:01 -0500321 auto socketInterface = getOffloadUri(fileHandle);
322 std::remove(socketInterface.c_str());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500323 if (DumpHandler::fd >= 0)
324 {
325 close(DumpHandler::fd);
326 DumpHandler::fd = -1;
327 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600328 }
Jayashankar Padathdb124362021-01-28 21:12:34 -0600329 return PLDM_SUCCESS;
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600330 }
331
332 return PLDM_ERROR;
333}
334
Jayashankar Padathdb124362021-01-28 21:12:34 -0600335int DumpHandler::readIntoMemory(uint32_t offset, uint32_t& length,
336 uint64_t address,
337 oem_platform::Handler* /*oemPlatformHandler*/)
338{
339 if (dumpType != PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
340 {
341 return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
342 }
343 return transferFileData(resDumpDirPath, true, offset, length, address);
344}
345
346int DumpHandler::read(uint32_t offset, uint32_t& length, Response& response,
347 oem_platform::Handler* /*oemPlatformHandler*/)
348{
349 if (dumpType != PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
350 {
351 return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
352 }
353 return readFile(resDumpDirPath, offset, length, response);
354}
355
Sampa Misra18967162020-01-14 02:31:41 -0600356} // namespace responder
357} // namespace pldm