blob: 0a0e041a222a3bb6f88bdbcb6adba8c5ed3ed20e [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>
8#include <libpldm/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>
19#include <iostream>
Jayashankar Padathdb124362021-01-28 21:12:34 -060020#include <type_traits>
Sampa Misra18967162020-01-14 02:31:41 -060021
Riya Dixit49cfb132023-03-02 04:26:53 -060022PHOSPHOR_LOG2_USING;
23
Ravi Tejace1c96f2020-10-05 23:13:01 -050024using namespace pldm::responder::utils;
Deepak Kodihallifd279e12020-02-02 05:20:43 -060025using namespace pldm::utils;
26
Sampa Misra18967162020-01-14 02:31:41 -060027namespace pldm
28{
29namespace responder
30{
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050031static constexpr auto dumpEntry = "xyz.openbmc_project.Dump.Entry";
Deepak Kodihalli14b54a32020-09-29 05:20:02 -050032static constexpr auto dumpObjPath = "/xyz/openbmc_project/dump/system";
Jayashankar Padathdb124362021-01-28 21:12:34 -060033static constexpr auto systemDumpEntry = "xyz.openbmc_project.Dump.Entry.System";
34static constexpr auto resDumpObjPath = "/xyz/openbmc_project/dump/resource";
35static constexpr auto resDumpEntry = "com.ibm.Dump.Entry.Resource";
Sampa Misra18967162020-01-14 02:31:41 -060036
Jayashankar Padathdb124362021-01-28 21:12:34 -060037// Resource dump file path to be deleted once hyperviosr validates the input
38// parameters. Need to re-look in to this name when we support multiple
39// resource dumps.
40static constexpr auto resDumpDirPath = "/var/lib/pldm/resourcedump/1";
41
42int DumpHandler::fd = -1;
43namespace fs = std::filesystem;
44
45std::string DumpHandler::findDumpObjPath(uint32_t fileHandle)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050046{
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050047 static constexpr auto DUMP_MANAGER_BUSNAME =
48 "xyz.openbmc_project.Dump.Manager";
49 static constexpr auto DUMP_MANAGER_PATH = "/xyz/openbmc_project/dump";
50 static constexpr auto OBJECT_MANAGER_INTERFACE =
51 "org.freedesktop.DBus.ObjectManager";
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050052 auto& bus = pldm::utils::DBusHandler::getBus();
53
Jayashankar Padathdb124362021-01-28 21:12:34 -060054 // Stores the current resource dump entry path
55 std::string curResDumpEntryPath{};
56
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050057 dbus::ObjectValueTree objects;
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050058 // Select the dump entry interface for system dump or resource dump
59 DumpEntryInterface dumpEntryIntf = systemDumpEntry;
60 if ((dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP) ||
61 (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS))
62 {
63 dumpEntryIntf = resDumpEntry;
64 }
65
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050066 try
67 {
Archana Kakani040c6182023-02-20 07:01:51 -060068 auto method =
69 bus.new_method_call(DUMP_MANAGER_BUSNAME, DUMP_MANAGER_PATH,
70 OBJECT_MANAGER_INTERFACE, "GetManagedObjects");
71
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050072 auto reply = bus.call(method);
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050073 reply.read(objects);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050074 }
Archana Kakani040c6182023-02-20 07:01:51 -060075 catch (const sdbusplus::exception_t& e)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050076 {
Riya Dixit49cfb132023-03-02 04:26:53 -060077 error(
78 "findDumpObjPath: Error {ERR_EXCEP} found with GetManagedObjects call in findDumpObjPath with objPath={OBJ_PATH} and intf={DUMP_INFT}",
79 "ERR_EXCEP", e.what(), "OBJ_PATH", DUMP_MANAGER_PATH, "DUMP_INFT",
80 dumpEntryIntf);
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050081 return curResDumpEntryPath;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050082 }
83
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050084 for (const auto& object : objects)
85 {
86 for (const auto& interface : object.second)
87 {
88 if (interface.first != dumpEntryIntf)
89 {
90 continue;
91 }
92
93 for (auto& propertyMap : interface.second)
94 {
95 if (propertyMap.first == "SourceDumpId")
96 {
97 auto dumpIdPtr = std::get_if<uint32_t>(&propertyMap.second);
98 if (dumpIdPtr != nullptr)
99 {
100 auto dumpId = *dumpIdPtr;
101 if (fileHandle == dumpId)
102 {
103 curResDumpEntryPath = object.first.str;
104 return curResDumpEntryPath;
105 }
106 }
107 else
108 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600109 error(
110 "Invalid SourceDumpId in curResDumpEntryPath {CUR_RES_DUMP_PATH} but continuing with next entry for a match...",
111 "CUR_RES_DUMP_PATH", curResDumpEntryPath);
Jayashankar Padathe7cc8692022-09-06 13:26:22 -0500112 }
113 }
114 }
115 }
116 }
Jayashankar Padathdb124362021-01-28 21:12:34 -0600117 return curResDumpEntryPath;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500118}
119
Sampa Misra18967162020-01-14 02:31:41 -0600120int DumpHandler::newFileAvailable(uint64_t length)
121{
Sampa Misra18967162020-01-14 02:31:41 -0600122 static constexpr auto dumpInterface = "xyz.openbmc_project.Dump.NewDump";
Sampa Misra18967162020-01-14 02:31:41 -0600123 auto& bus = pldm::utils::DBusHandler::getBus();
124
Jayashankar Padathdb124362021-01-28 21:12:34 -0600125 auto notifyObjPath = dumpObjPath;
126 if (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP)
127 {
128 // Setting the Notify path for resource dump
129 notifyObjPath = resDumpObjPath;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600130 }
131
Sampa Misra18967162020-01-14 02:31:41 -0600132 try
133 {
134 auto service =
Jayashankar Padathdb124362021-01-28 21:12:34 -0600135 pldm::utils::DBusHandler().getService(notifyObjPath, dumpInterface);
Sampa Misra18967162020-01-14 02:31:41 -0600136 using namespace sdbusplus::xyz::openbmc_project::Dump::server;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600137 auto method = bus.new_method_call(service.c_str(), notifyObjPath,
Sampa Misra18967162020-01-14 02:31:41 -0600138 dumpInterface, "Notify");
Dhruvaraj Subhashchandran41989eb2020-11-27 00:22:42 -0600139 method.append(fileHandle, length);
Sampa Misra18967162020-01-14 02:31:41 -0600140 bus.call_noreply(method);
141 }
142 catch (const std::exception& e)
143 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600144 error(
145 "newFileAvailable: Error {ERR_EXCEP} found while notifying new dump to dump manager with objPath={OBJ_PATH} and intf={DUMP_INTF}",
146 "ERR_EXCEP", e.what(), "OBJ_PATH", notifyObjPath, "DUMP_INTF",
147 dumpInterface);
Sampa Misra18967162020-01-14 02:31:41 -0600148 return PLDM_ERROR;
149 }
150
151 return PLDM_SUCCESS;
152}
153
Jayashankar Padathdb124362021-01-28 21:12:34 -0600154std::string DumpHandler::getOffloadUri(uint32_t fileHandle)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500155{
156 auto path = findDumpObjPath(fileHandle);
157 if (path.empty())
158 {
159 return {};
160 }
161
Ravi Tejace1c96f2020-10-05 23:13:01 -0500162 std::string socketInterface{};
163
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500164 try
165 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500166 socketInterface =
167 pldm::utils::DBusHandler().getDbusProperty<std::string>(
168 path.c_str(), "OffloadUri", dumpEntry);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500169 }
170 catch (const std::exception& e)
171 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600172 error(
173 "getOffloadUri: Error {ERR_EXCEP} found while fetching the dump offload URI with objPath={OBJ_PATH} and intf={SOCKET_INTF}",
174 "ERR_EXCEP", e.what(), "OBJ_PATH", path.c_str(), "SOCKET_INTF",
175 socketInterface);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500176 }
177
Ravi Tejace1c96f2020-10-05 23:13:01 -0500178 return socketInterface;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500179}
180
Jayashankar Padathdb124362021-01-28 21:12:34 -0600181int DumpHandler::writeFromMemory(uint32_t, uint32_t length, uint64_t address,
Sampa Misra69508502020-09-08 00:08:21 -0500182 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra18967162020-01-14 02:31:41 -0600183{
Sampa Misra18967162020-01-14 02:31:41 -0600184 if (DumpHandler::fd == -1)
185 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500186 auto socketInterface = getOffloadUri(fileHandle);
187 int sock = setupUnixSocket(socketInterface);
188 if (sock < 0)
Sampa Misra18967162020-01-14 02:31:41 -0600189 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500190 sock = -errno;
191 close(DumpHandler::fd);
Riya Dixit49cfb132023-03-02 04:26:53 -0600192 error("DumpHandler::writeFromMemory: setupUnixSocket() failed");
Ravi Tejace1c96f2020-10-05 23:13:01 -0500193 std::remove(socketInterface.c_str());
Sampa Misra18967162020-01-14 02:31:41 -0600194 return PLDM_ERROR;
195 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500196
Ravi Tejace1c96f2020-10-05 23:13:01 -0500197 DumpHandler::fd = sock;
198 }
199 return transferFileDataToSocket(DumpHandler::fd, length, address);
Sampa Misra18967162020-01-14 02:31:41 -0600200}
201
Jayashankar Padathdb124362021-01-28 21:12:34 -0600202int DumpHandler::write(const char* buffer, uint32_t, uint32_t& length,
Sampa Misra69508502020-09-08 00:08:21 -0500203 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra18967162020-01-14 02:31:41 -0600204{
Ravi Tejace1c96f2020-10-05 23:13:01 -0500205 int rc = writeToUnixSocket(DumpHandler::fd, buffer, length);
206 if (rc < 0)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500207 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500208 rc = -errno;
209 close(DumpHandler::fd);
210 auto socketInterface = getOffloadUri(fileHandle);
211 std::remove(socketInterface.c_str());
Riya Dixit49cfb132023-03-02 04:26:53 -0600212 error("DumpHandler::write: writeToUnixSocket() failed");
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500213 return PLDM_ERROR;
214 }
215
Sampa Misra18967162020-01-14 02:31:41 -0600216 return PLDM_SUCCESS;
217}
218
Jayashankar Padathdb124362021-01-28 21:12:34 -0600219int DumpHandler::fileAck(uint8_t fileStatus)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600220{
Jayashankar Padathdb124362021-01-28 21:12:34 -0600221 auto path = findDumpObjPath(fileHandle);
222 if (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600223 {
Jayashankar Padathdb124362021-01-28 21:12:34 -0600224 if (fileStatus != PLDM_SUCCESS)
225 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600226 error("Failue in resource dump file ack");
Jayashankar Padathdb124362021-01-28 21:12:34 -0600227 pldm::utils::reportError(
228 "xyz.openbmc_project.bmc.pldm.InternalFailure");
229
230 PropertyValue value{
231 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed"};
232 DBusMapping dbusMapping{path, "xyz.openbmc_project.Common.Progress",
233 "Status", "string"};
234 try
235 {
236 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
237 }
238 catch (const std::exception& e)
239 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600240 error(
241 "fileAck: Error {ERR_EXCEP} found while setting the dump progress status as Failed with objPath={OBJ_PATH} and intf=Common.Progress",
242 "ERR_EXCEP", e.what(), "OBJ_PATH", path.c_str());
Jayashankar Padathdb124362021-01-28 21:12:34 -0600243 }
244 }
245
246 if (fs::exists(resDumpDirPath))
247 {
248 fs::remove_all(resDumpDirPath);
249 }
250 return PLDM_SUCCESS;
251 }
252
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500253 if (!path.empty())
Jayashankar Padathdb124362021-01-28 21:12:34 -0600254 {
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500255 if (fileStatus == PLDM_ERROR_FILE_DISCARDED)
256 {
257 uint32_t val = 0xFFFFFFFF;
258 PropertyValue value = static_cast<uint32_t>(val);
259 auto dumpIntf = resDumpEntry;
260
261 if (dumpType == PLDM_FILE_TYPE_DUMP)
262 {
263 dumpIntf = systemDumpEntry;
264 }
265
266 DBusMapping dbusMapping{path.c_str(), dumpIntf, "SourceDumpId",
267 "uint32_t"};
268 try
269 {
270 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
271 }
272 catch (const std::exception& e)
273 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600274 error(
275 "fileAck: Failed to make a d-bus call to DUMP manager to reset source dump id of {FILE_PATH}, with ERROR={ERR_EXCEP}",
276 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500277 pldm::utils::reportError(
278 "xyz.openbmc_project.bmc.PLDM.fileAck.SourceDumpIdResetFail");
279 return PLDM_ERROR;
280 }
281
282 auto& bus = pldm::utils::DBusHandler::getBus();
283 try
284 {
285 auto method = bus.new_method_call(
286 "xyz.openbmc_project.Dump.Manager", path.c_str(),
287 "xyz.openbmc_project.Object.Delete", "Delete");
288 bus.call(method);
289 }
290 catch (const std::exception& e)
291 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600292 error(
293 "fileAck: Failed to make a d-bus method to delete the dump entry {FILE_PATH}, with ERROR={ERR_EXCEP}",
294 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500295 pldm::utils::reportError(
296 "xyz.openbmc_project.bmc.PLDM.fileAck.DumpEntryDeleteFail");
297 return PLDM_ERROR;
298 }
299 return PLDM_SUCCESS;
300 }
301
Jayashankar Padathdb124362021-01-28 21:12:34 -0600302 if (dumpType == PLDM_FILE_TYPE_DUMP ||
303 dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600304 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500305 PropertyValue value{true};
306 DBusMapping dbusMapping{path, dumpEntry, "Offloaded", "bool"};
307 try
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600308 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500309 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600310 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500311 catch (const std::exception& e)
312 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600313 error(
314 "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}",
315 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500316 }
Ravi Tejace1c96f2020-10-05 23:13:01 -0500317
Ravi Tejace1c96f2020-10-05 23:13:01 -0500318 auto socketInterface = getOffloadUri(fileHandle);
319 std::remove(socketInterface.c_str());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500320 if (DumpHandler::fd >= 0)
321 {
322 close(DumpHandler::fd);
323 DumpHandler::fd = -1;
324 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600325 }
Jayashankar Padathdb124362021-01-28 21:12:34 -0600326 return PLDM_SUCCESS;
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600327 }
328
329 return PLDM_ERROR;
330}
331
Jayashankar Padathdb124362021-01-28 21:12:34 -0600332int DumpHandler::readIntoMemory(uint32_t offset, uint32_t& length,
333 uint64_t address,
334 oem_platform::Handler* /*oemPlatformHandler*/)
335{
336 if (dumpType != PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
337 {
338 return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
339 }
340 return transferFileData(resDumpDirPath, true, offset, length, address);
341}
342
343int DumpHandler::read(uint32_t offset, uint32_t& length, Response& response,
344 oem_platform::Handler* /*oemPlatformHandler*/)
345{
346 if (dumpType != PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
347 {
348 return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
349 }
350 return readFile(resDumpDirPath, offset, length, response);
351}
352
Sampa Misra18967162020-01-14 02:31:41 -0600353} // namespace responder
354} // namespace pldm