blob: 19b14d73e9e06cc107f0b1d7d07f92ac89a8130b [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);
Riya Dixit49cfb132023-03-02 04:26:53 -0600191 error("DumpHandler::writeFromMemory: setupUnixSocket() failed");
Ravi Tejace1c96f2020-10-05 23:13:01 -0500192 std::remove(socketInterface.c_str());
Sampa Misra18967162020-01-14 02:31:41 -0600193 return PLDM_ERROR;
194 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500195
Ravi Tejace1c96f2020-10-05 23:13:01 -0500196 DumpHandler::fd = sock;
197 }
198 return transferFileDataToSocket(DumpHandler::fd, length, address);
Sampa Misra18967162020-01-14 02:31:41 -0600199}
200
Jayashankar Padathdb124362021-01-28 21:12:34 -0600201int DumpHandler::write(const char* buffer, uint32_t, uint32_t& length,
Sampa Misra69508502020-09-08 00:08:21 -0500202 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra18967162020-01-14 02:31:41 -0600203{
Ravi Tejace1c96f2020-10-05 23:13:01 -0500204 int rc = writeToUnixSocket(DumpHandler::fd, buffer, length);
205 if (rc < 0)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500206 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500207 rc = -errno;
208 close(DumpHandler::fd);
209 auto socketInterface = getOffloadUri(fileHandle);
210 std::remove(socketInterface.c_str());
Riya Dixit49cfb132023-03-02 04:26:53 -0600211 error("DumpHandler::write: writeToUnixSocket() failed");
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500212 return PLDM_ERROR;
213 }
214
Sampa Misra18967162020-01-14 02:31:41 -0600215 return PLDM_SUCCESS;
216}
217
Jayashankar Padathdb124362021-01-28 21:12:34 -0600218int DumpHandler::fileAck(uint8_t fileStatus)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600219{
Jayashankar Padathdb124362021-01-28 21:12:34 -0600220 auto path = findDumpObjPath(fileHandle);
221 if (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600222 {
Jayashankar Padathdb124362021-01-28 21:12:34 -0600223 if (fileStatus != PLDM_SUCCESS)
224 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600225 error("Failue in resource dump file ack");
Jayashankar Padathdb124362021-01-28 21:12:34 -0600226 pldm::utils::reportError(
227 "xyz.openbmc_project.bmc.pldm.InternalFailure");
228
229 PropertyValue value{
230 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed"};
231 DBusMapping dbusMapping{path, "xyz.openbmc_project.Common.Progress",
232 "Status", "string"};
233 try
234 {
235 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
236 }
237 catch (const std::exception& e)
238 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600239 error(
240 "fileAck: Error {ERR_EXCEP} found while setting the dump progress status as Failed with objPath={OBJ_PATH} and intf=Common.Progress",
241 "ERR_EXCEP", e.what(), "OBJ_PATH", path.c_str());
Jayashankar Padathdb124362021-01-28 21:12:34 -0600242 }
243 }
244
245 if (fs::exists(resDumpDirPath))
246 {
247 fs::remove_all(resDumpDirPath);
248 }
249 return PLDM_SUCCESS;
250 }
251
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500252 if (!path.empty())
Jayashankar Padathdb124362021-01-28 21:12:34 -0600253 {
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500254 if (fileStatus == PLDM_ERROR_FILE_DISCARDED)
255 {
256 uint32_t val = 0xFFFFFFFF;
257 PropertyValue value = static_cast<uint32_t>(val);
258 auto dumpIntf = resDumpEntry;
259
260 if (dumpType == PLDM_FILE_TYPE_DUMP)
261 {
262 dumpIntf = systemDumpEntry;
263 }
264
265 DBusMapping dbusMapping{path.c_str(), dumpIntf, "SourceDumpId",
266 "uint32_t"};
267 try
268 {
269 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
270 }
271 catch (const std::exception& e)
272 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600273 error(
274 "fileAck: Failed to make a d-bus call to DUMP manager to reset source dump id of {FILE_PATH}, with ERROR={ERR_EXCEP}",
275 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500276 pldm::utils::reportError(
277 "xyz.openbmc_project.bmc.PLDM.fileAck.SourceDumpIdResetFail");
278 return PLDM_ERROR;
279 }
280
281 auto& bus = pldm::utils::DBusHandler::getBus();
282 try
283 {
284 auto method = bus.new_method_call(
285 "xyz.openbmc_project.Dump.Manager", path.c_str(),
286 "xyz.openbmc_project.Object.Delete", "Delete");
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500287 bus.call(method, dbusTimeout);
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500288 }
289 catch (const std::exception& e)
290 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600291 error(
292 "fileAck: Failed to make a d-bus method to delete the dump entry {FILE_PATH}, with ERROR={ERR_EXCEP}",
293 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500294 pldm::utils::reportError(
295 "xyz.openbmc_project.bmc.PLDM.fileAck.DumpEntryDeleteFail");
296 return PLDM_ERROR;
297 }
298 return PLDM_SUCCESS;
299 }
300
Jayashankar Padathdb124362021-01-28 21:12:34 -0600301 if (dumpType == PLDM_FILE_TYPE_DUMP ||
302 dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600303 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500304 PropertyValue value{true};
305 DBusMapping dbusMapping{path, dumpEntry, "Offloaded", "bool"};
306 try
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600307 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500308 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600309 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500310 catch (const std::exception& e)
311 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600312 error(
313 "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}",
314 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500315 }
Ravi Tejace1c96f2020-10-05 23:13:01 -0500316
Ravi Tejace1c96f2020-10-05 23:13:01 -0500317 auto socketInterface = getOffloadUri(fileHandle);
318 std::remove(socketInterface.c_str());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500319 if (DumpHandler::fd >= 0)
320 {
321 close(DumpHandler::fd);
322 DumpHandler::fd = -1;
323 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600324 }
Jayashankar Padathdb124362021-01-28 21:12:34 -0600325 return PLDM_SUCCESS;
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600326 }
327
328 return PLDM_ERROR;
329}
330
Jayashankar Padathdb124362021-01-28 21:12:34 -0600331int DumpHandler::readIntoMemory(uint32_t offset, uint32_t& length,
332 uint64_t address,
333 oem_platform::Handler* /*oemPlatformHandler*/)
334{
335 if (dumpType != PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
336 {
337 return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
338 }
339 return transferFileData(resDumpDirPath, true, offset, length, address);
340}
341
342int DumpHandler::read(uint32_t offset, uint32_t& length, Response& response,
343 oem_platform::Handler* /*oemPlatformHandler*/)
344{
345 if (dumpType != PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
346 {
347 return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
348 }
349 return readFile(resDumpDirPath, offset, length, response);
350}
351
Sampa Misra18967162020-01-14 02:31:41 -0600352} // namespace responder
353} // namespace pldm