blob: 9005f66b302477a06c4026a993a8c4f4eb485690 [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
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -050072 auto reply = bus.call(
73 method,
74 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050075 reply.read(objects);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050076 }
Archana Kakani040c6182023-02-20 07:01:51 -060077 catch (const sdbusplus::exception_t& e)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050078 {
Riya Dixit49cfb132023-03-02 04:26:53 -060079 error(
80 "findDumpObjPath: Error {ERR_EXCEP} found with GetManagedObjects call in findDumpObjPath with objPath={OBJ_PATH} and intf={DUMP_INFT}",
81 "ERR_EXCEP", e.what(), "OBJ_PATH", DUMP_MANAGER_PATH, "DUMP_INFT",
82 dumpEntryIntf);
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050083 return curResDumpEntryPath;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050084 }
85
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050086 for (const auto& object : objects)
87 {
88 for (const auto& interface : object.second)
89 {
90 if (interface.first != dumpEntryIntf)
91 {
92 continue;
93 }
94
95 for (auto& propertyMap : interface.second)
96 {
97 if (propertyMap.first == "SourceDumpId")
98 {
99 auto dumpIdPtr = std::get_if<uint32_t>(&propertyMap.second);
100 if (dumpIdPtr != nullptr)
101 {
102 auto dumpId = *dumpIdPtr;
103 if (fileHandle == dumpId)
104 {
105 curResDumpEntryPath = object.first.str;
106 return curResDumpEntryPath;
107 }
108 }
109 else
110 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600111 error(
112 "Invalid SourceDumpId in curResDumpEntryPath {CUR_RES_DUMP_PATH} but continuing with next entry for a match...",
113 "CUR_RES_DUMP_PATH", curResDumpEntryPath);
Jayashankar Padathe7cc8692022-09-06 13:26:22 -0500114 }
115 }
116 }
117 }
118 }
Jayashankar Padathdb124362021-01-28 21:12:34 -0600119 return curResDumpEntryPath;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500120}
121
Sampa Misra18967162020-01-14 02:31:41 -0600122int DumpHandler::newFileAvailable(uint64_t length)
123{
Sampa Misra18967162020-01-14 02:31:41 -0600124 static constexpr auto dumpInterface = "xyz.openbmc_project.Dump.NewDump";
Sampa Misra18967162020-01-14 02:31:41 -0600125 auto& bus = pldm::utils::DBusHandler::getBus();
126
Jayashankar Padathdb124362021-01-28 21:12:34 -0600127 auto notifyObjPath = dumpObjPath;
128 if (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP)
129 {
130 // Setting the Notify path for resource dump
131 notifyObjPath = resDumpObjPath;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600132 }
133
Sampa Misra18967162020-01-14 02:31:41 -0600134 try
135 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500136 auto service = pldm::utils::DBusHandler().getService(notifyObjPath,
137 dumpInterface);
Sampa Misra18967162020-01-14 02:31:41 -0600138 using namespace sdbusplus::xyz::openbmc_project::Dump::server;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600139 auto method = bus.new_method_call(service.c_str(), notifyObjPath,
Sampa Misra18967162020-01-14 02:31:41 -0600140 dumpInterface, "Notify");
Dhruvaraj Subhashchandran41989eb2020-11-27 00:22:42 -0600141 method.append(fileHandle, length);
Sampa Misra18967162020-01-14 02:31:41 -0600142 bus.call_noreply(method);
143 }
144 catch (const std::exception& e)
145 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600146 error(
147 "newFileAvailable: Error {ERR_EXCEP} found while notifying new dump to dump manager with objPath={OBJ_PATH} and intf={DUMP_INTF}",
148 "ERR_EXCEP", e.what(), "OBJ_PATH", notifyObjPath, "DUMP_INTF",
149 dumpInterface);
Sampa Misra18967162020-01-14 02:31:41 -0600150 return PLDM_ERROR;
151 }
152
153 return PLDM_SUCCESS;
154}
155
Jayashankar Padathdb124362021-01-28 21:12:34 -0600156std::string DumpHandler::getOffloadUri(uint32_t fileHandle)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500157{
158 auto path = findDumpObjPath(fileHandle);
159 if (path.empty())
160 {
161 return {};
162 }
163
Ravi Tejace1c96f2020-10-05 23:13:01 -0500164 std::string socketInterface{};
165
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500166 try
167 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500168 socketInterface =
169 pldm::utils::DBusHandler().getDbusProperty<std::string>(
170 path.c_str(), "OffloadUri", dumpEntry);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500171 }
172 catch (const std::exception& e)
173 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600174 error(
175 "getOffloadUri: Error {ERR_EXCEP} found while fetching the dump offload URI with objPath={OBJ_PATH} and intf={SOCKET_INTF}",
176 "ERR_EXCEP", e.what(), "OBJ_PATH", path.c_str(), "SOCKET_INTF",
177 socketInterface);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500178 }
179
Ravi Tejace1c96f2020-10-05 23:13:01 -0500180 return socketInterface;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500181}
182
Jayashankar Padathdb124362021-01-28 21:12:34 -0600183int DumpHandler::writeFromMemory(uint32_t, uint32_t length, uint64_t address,
Sampa Misra69508502020-09-08 00:08:21 -0500184 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra18967162020-01-14 02:31:41 -0600185{
Sampa Misra18967162020-01-14 02:31:41 -0600186 if (DumpHandler::fd == -1)
187 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500188 auto socketInterface = getOffloadUri(fileHandle);
189 int sock = setupUnixSocket(socketInterface);
190 if (sock < 0)
Sampa Misra18967162020-01-14 02:31:41 -0600191 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500192 sock = -errno;
193 close(DumpHandler::fd);
Riya Dixit49cfb132023-03-02 04:26:53 -0600194 error("DumpHandler::writeFromMemory: setupUnixSocket() failed");
Ravi Tejace1c96f2020-10-05 23:13:01 -0500195 std::remove(socketInterface.c_str());
Sampa Misra18967162020-01-14 02:31:41 -0600196 return PLDM_ERROR;
197 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500198
Ravi Tejace1c96f2020-10-05 23:13:01 -0500199 DumpHandler::fd = sock;
200 }
201 return transferFileDataToSocket(DumpHandler::fd, length, address);
Sampa Misra18967162020-01-14 02:31:41 -0600202}
203
Jayashankar Padathdb124362021-01-28 21:12:34 -0600204int DumpHandler::write(const char* buffer, uint32_t, uint32_t& length,
Sampa Misra69508502020-09-08 00:08:21 -0500205 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra18967162020-01-14 02:31:41 -0600206{
Ravi Tejace1c96f2020-10-05 23:13:01 -0500207 int rc = writeToUnixSocket(DumpHandler::fd, buffer, length);
208 if (rc < 0)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500209 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500210 rc = -errno;
211 close(DumpHandler::fd);
212 auto socketInterface = getOffloadUri(fileHandle);
213 std::remove(socketInterface.c_str());
Riya Dixit49cfb132023-03-02 04:26:53 -0600214 error("DumpHandler::write: writeToUnixSocket() failed");
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(
230 "xyz.openbmc_project.bmc.pldm.InternalFailure");
231
232 PropertyValue value{
233 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed"};
234 DBusMapping dbusMapping{path, "xyz.openbmc_project.Common.Progress",
235 "Status", "string"};
236 try
237 {
238 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
239 }
240 catch (const std::exception& e)
241 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600242 error(
243 "fileAck: Error {ERR_EXCEP} found while setting the dump progress status as Failed with objPath={OBJ_PATH} and intf=Common.Progress",
244 "ERR_EXCEP", e.what(), "OBJ_PATH", path.c_str());
Jayashankar Padathdb124362021-01-28 21:12:34 -0600245 }
246 }
247
248 if (fs::exists(resDumpDirPath))
249 {
250 fs::remove_all(resDumpDirPath);
251 }
252 return PLDM_SUCCESS;
253 }
254
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500255 if (!path.empty())
Jayashankar Padathdb124362021-01-28 21:12:34 -0600256 {
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500257 if (fileStatus == PLDM_ERROR_FILE_DISCARDED)
258 {
259 uint32_t val = 0xFFFFFFFF;
260 PropertyValue value = static_cast<uint32_t>(val);
261 auto dumpIntf = resDumpEntry;
262
263 if (dumpType == PLDM_FILE_TYPE_DUMP)
264 {
265 dumpIntf = systemDumpEntry;
266 }
267
268 DBusMapping dbusMapping{path.c_str(), dumpIntf, "SourceDumpId",
269 "uint32_t"};
270 try
271 {
272 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
273 }
274 catch (const std::exception& e)
275 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600276 error(
277 "fileAck: Failed to make a d-bus call to DUMP manager to reset source dump id of {FILE_PATH}, with ERROR={ERR_EXCEP}",
278 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500279 pldm::utils::reportError(
280 "xyz.openbmc_project.bmc.PLDM.fileAck.SourceDumpIdResetFail");
281 return PLDM_ERROR;
282 }
283
284 auto& bus = pldm::utils::DBusHandler::getBus();
285 try
286 {
287 auto method = bus.new_method_call(
288 "xyz.openbmc_project.Dump.Manager", path.c_str(),
289 "xyz.openbmc_project.Object.Delete", "Delete");
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -0500290 bus.call(method,
291 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT))
292 .count());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500293 }
294 catch (const std::exception& e)
295 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600296 error(
297 "fileAck: Failed to make a d-bus method to delete the dump entry {FILE_PATH}, with ERROR={ERR_EXCEP}",
298 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500299 pldm::utils::reportError(
300 "xyz.openbmc_project.bmc.PLDM.fileAck.DumpEntryDeleteFail");
301 return PLDM_ERROR;
302 }
303 return PLDM_SUCCESS;
304 }
305
Jayashankar Padathdb124362021-01-28 21:12:34 -0600306 if (dumpType == PLDM_FILE_TYPE_DUMP ||
307 dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600308 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500309 PropertyValue value{true};
310 DBusMapping dbusMapping{path, dumpEntry, "Offloaded", "bool"};
311 try
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600312 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500313 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600314 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500315 catch (const std::exception& e)
316 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600317 error(
318 "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}",
319 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500320 }
Ravi Tejace1c96f2020-10-05 23:13:01 -0500321
Ravi Tejace1c96f2020-10-05 23:13:01 -0500322 auto socketInterface = getOffloadUri(fileHandle);
323 std::remove(socketInterface.c_str());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500324 if (DumpHandler::fd >= 0)
325 {
326 close(DumpHandler::fd);
327 DumpHandler::fd = -1;
328 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600329 }
Jayashankar Padathdb124362021-01-28 21:12:34 -0600330 return PLDM_SUCCESS;
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600331 }
332
333 return PLDM_ERROR;
334}
335
Jayashankar Padathdb124362021-01-28 21:12:34 -0600336int DumpHandler::readIntoMemory(uint32_t offset, uint32_t& length,
337 uint64_t address,
338 oem_platform::Handler* /*oemPlatformHandler*/)
339{
340 if (dumpType != PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
341 {
342 return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
343 }
344 return transferFileData(resDumpDirPath, true, offset, length, address);
345}
346
347int DumpHandler::read(uint32_t offset, uint32_t& length, Response& response,
348 oem_platform::Handler* /*oemPlatformHandler*/)
349{
350 if (dumpType != PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
351 {
352 return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
353 }
354 return readFile(resDumpDirPath, offset, length, response);
355}
356
Sampa Misra18967162020-01-14 02:31:41 -0600357} // namespace responder
358} // namespace pldm