blob: 811f3fb3f1dbf5d625a518a48175d038c4970d5d [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(
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600227 "xyz.openbmc_project.PLDM.Error.fileAck.ResourceDumpFileAckFail",
228 pldm::PelSeverity::Informational);
Jayashankar Padathdb124362021-01-28 21:12:34 -0600229
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(
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600278 "xyz.openbmc_project.PLDM.Error.fileAck.SourceDumpIdResetFail");
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500279 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");
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500288 bus.call(method, dbusTimeout);
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500289 }
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(
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600296 "xyz.openbmc_project.PLDM.Error.fileAck.DumpEntryDeleteFail");
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500297 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