blob: f3394a300613d81749307e010ed8a6194f20baaa [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";
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050049
Jayashankar Padathdb124362021-01-28 21:12:34 -060050 // Stores the current resource dump entry path
51 std::string curResDumpEntryPath{};
52
Riya Dixit754041d2024-02-20 06:15:49 -060053 ObjectValueTree objects;
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050054 // Select the dump entry interface for system dump or resource dump
55 DumpEntryInterface dumpEntryIntf = systemDumpEntry;
56 if ((dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP) ||
57 (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS))
58 {
59 dumpEntryIntf = resDumpEntry;
60 }
61
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050062 try
63 {
Riya Dixit754041d2024-02-20 06:15:49 -060064 objects = pldm::utils::DBusHandler::getManagedObj(DUMP_MANAGER_BUSNAME,
65 DUMP_MANAGER_PATH);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050066 }
Archana Kakani040c6182023-02-20 07:01:51 -060067 catch (const sdbusplus::exception_t& e)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050068 {
Riya Dixit49cfb132023-03-02 04:26:53 -060069 error(
70 "findDumpObjPath: Error {ERR_EXCEP} found with GetManagedObjects call in findDumpObjPath with objPath={OBJ_PATH} and intf={DUMP_INFT}",
71 "ERR_EXCEP", e.what(), "OBJ_PATH", DUMP_MANAGER_PATH, "DUMP_INFT",
72 dumpEntryIntf);
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050073 return curResDumpEntryPath;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050074 }
75
Jayashankar Padathe7cc8692022-09-06 13:26:22 -050076 for (const auto& object : objects)
77 {
78 for (const auto& interface : object.second)
79 {
80 if (interface.first != dumpEntryIntf)
81 {
82 continue;
83 }
84
85 for (auto& propertyMap : interface.second)
86 {
87 if (propertyMap.first == "SourceDumpId")
88 {
89 auto dumpIdPtr = std::get_if<uint32_t>(&propertyMap.second);
90 if (dumpIdPtr != nullptr)
91 {
92 auto dumpId = *dumpIdPtr;
93 if (fileHandle == dumpId)
94 {
95 curResDumpEntryPath = object.first.str;
96 return curResDumpEntryPath;
97 }
98 }
99 else
100 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600101 error(
102 "Invalid SourceDumpId in curResDumpEntryPath {CUR_RES_DUMP_PATH} but continuing with next entry for a match...",
103 "CUR_RES_DUMP_PATH", curResDumpEntryPath);
Jayashankar Padathe7cc8692022-09-06 13:26:22 -0500104 }
105 }
106 }
107 }
108 }
Jayashankar Padathdb124362021-01-28 21:12:34 -0600109 return curResDumpEntryPath;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500110}
111
Sampa Misra18967162020-01-14 02:31:41 -0600112int DumpHandler::newFileAvailable(uint64_t length)
113{
Sampa Misra18967162020-01-14 02:31:41 -0600114 static constexpr auto dumpInterface = "xyz.openbmc_project.Dump.NewDump";
Sampa Misra18967162020-01-14 02:31:41 -0600115 auto& bus = pldm::utils::DBusHandler::getBus();
116
Jayashankar Padathdb124362021-01-28 21:12:34 -0600117 auto notifyObjPath = dumpObjPath;
118 if (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP)
119 {
120 // Setting the Notify path for resource dump
121 notifyObjPath = resDumpObjPath;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600122 }
123
Sampa Misra18967162020-01-14 02:31:41 -0600124 try
125 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500126 auto service = pldm::utils::DBusHandler().getService(notifyObjPath,
127 dumpInterface);
Sampa Misra18967162020-01-14 02:31:41 -0600128 using namespace sdbusplus::xyz::openbmc_project::Dump::server;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600129 auto method = bus.new_method_call(service.c_str(), notifyObjPath,
Sampa Misra18967162020-01-14 02:31:41 -0600130 dumpInterface, "Notify");
Dhruvaraj Subhashchandran41989eb2020-11-27 00:22:42 -0600131 method.append(fileHandle, length);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000132 bus.call_noreply(method, dbusTimeout);
Sampa Misra18967162020-01-14 02:31:41 -0600133 }
134 catch (const std::exception& e)
135 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600136 error(
137 "newFileAvailable: Error {ERR_EXCEP} found while notifying new dump to dump manager with objPath={OBJ_PATH} and intf={DUMP_INTF}",
138 "ERR_EXCEP", e.what(), "OBJ_PATH", notifyObjPath, "DUMP_INTF",
139 dumpInterface);
Sampa Misra18967162020-01-14 02:31:41 -0600140 return PLDM_ERROR;
141 }
142
143 return PLDM_SUCCESS;
144}
145
Jayashankar Padathdb124362021-01-28 21:12:34 -0600146std::string DumpHandler::getOffloadUri(uint32_t fileHandle)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500147{
148 auto path = findDumpObjPath(fileHandle);
149 if (path.empty())
150 {
151 return {};
152 }
153
Ravi Tejace1c96f2020-10-05 23:13:01 -0500154 std::string socketInterface{};
155
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500156 try
157 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500158 socketInterface =
159 pldm::utils::DBusHandler().getDbusProperty<std::string>(
160 path.c_str(), "OffloadUri", dumpEntry);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500161 }
162 catch (const std::exception& e)
163 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600164 error(
165 "getOffloadUri: Error {ERR_EXCEP} found while fetching the dump offload URI with objPath={OBJ_PATH} and intf={SOCKET_INTF}",
166 "ERR_EXCEP", e.what(), "OBJ_PATH", path.c_str(), "SOCKET_INTF",
167 socketInterface);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500168 }
169
Ravi Tejace1c96f2020-10-05 23:13:01 -0500170 return socketInterface;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500171}
172
Jayashankar Padathdb124362021-01-28 21:12:34 -0600173int DumpHandler::writeFromMemory(uint32_t, uint32_t length, uint64_t address,
Sampa Misra69508502020-09-08 00:08:21 -0500174 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra18967162020-01-14 02:31:41 -0600175{
Sampa Misra18967162020-01-14 02:31:41 -0600176 if (DumpHandler::fd == -1)
177 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500178 auto socketInterface = getOffloadUri(fileHandle);
179 int sock = setupUnixSocket(socketInterface);
180 if (sock < 0)
Sampa Misra18967162020-01-14 02:31:41 -0600181 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500182 sock = -errno;
183 close(DumpHandler::fd);
Pavithra Barithaya5e3a2472023-07-10 08:43:59 -0500184 error(
185 "DumpHandler::writeFromMemory: setupUnixSocket() failed ERR={ERR_NO}",
186 "ERR_NO", sock);
Ravi Tejace1c96f2020-10-05 23:13:01 -0500187 std::remove(socketInterface.c_str());
Sampa Misra18967162020-01-14 02:31:41 -0600188 return PLDM_ERROR;
189 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500190
Ravi Tejace1c96f2020-10-05 23:13:01 -0500191 DumpHandler::fd = sock;
192 }
193 return transferFileDataToSocket(DumpHandler::fd, length, address);
Sampa Misra18967162020-01-14 02:31:41 -0600194}
195
Jayashankar Padathdb124362021-01-28 21:12:34 -0600196int DumpHandler::write(const char* buffer, uint32_t, uint32_t& length,
Sampa Misra69508502020-09-08 00:08:21 -0500197 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra18967162020-01-14 02:31:41 -0600198{
Ravi Tejace1c96f2020-10-05 23:13:01 -0500199 int rc = writeToUnixSocket(DumpHandler::fd, buffer, length);
200 if (rc < 0)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500201 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500202 rc = -errno;
203 close(DumpHandler::fd);
204 auto socketInterface = getOffloadUri(fileHandle);
205 std::remove(socketInterface.c_str());
Pavithra Barithaya5e3a2472023-07-10 08:43:59 -0500206 error("DumpHandler::write: writeToUnixSocket() failed ERR={RC_VAL}",
207 "RC_VAL", rc);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500208 return PLDM_ERROR;
209 }
210
Sampa Misra18967162020-01-14 02:31:41 -0600211 return PLDM_SUCCESS;
212}
213
Jayashankar Padathdb124362021-01-28 21:12:34 -0600214int DumpHandler::fileAck(uint8_t fileStatus)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600215{
Jayashankar Padathdb124362021-01-28 21:12:34 -0600216 auto path = findDumpObjPath(fileHandle);
217 if (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600218 {
Jayashankar Padathdb124362021-01-28 21:12:34 -0600219 if (fileStatus != PLDM_SUCCESS)
220 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600221 error("Failue in resource dump file ack");
Jayashankar Padathdb124362021-01-28 21:12:34 -0600222 pldm::utils::reportError(
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600223 "xyz.openbmc_project.PLDM.Error.fileAck.ResourceDumpFileAckFail",
224 pldm::PelSeverity::Informational);
Jayashankar Padathdb124362021-01-28 21:12:34 -0600225
226 PropertyValue value{
227 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed"};
228 DBusMapping dbusMapping{path, "xyz.openbmc_project.Common.Progress",
229 "Status", "string"};
230 try
231 {
232 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
233 }
234 catch (const std::exception& e)
235 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600236 error(
237 "fileAck: Error {ERR_EXCEP} found while setting the dump progress status as Failed with objPath={OBJ_PATH} and intf=Common.Progress",
238 "ERR_EXCEP", e.what(), "OBJ_PATH", path.c_str());
Jayashankar Padathdb124362021-01-28 21:12:34 -0600239 }
240 }
241
242 if (fs::exists(resDumpDirPath))
243 {
244 fs::remove_all(resDumpDirPath);
245 }
246 return PLDM_SUCCESS;
247 }
248
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500249 if (!path.empty())
Jayashankar Padathdb124362021-01-28 21:12:34 -0600250 {
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500251 if (fileStatus == PLDM_ERROR_FILE_DISCARDED)
252 {
253 uint32_t val = 0xFFFFFFFF;
254 PropertyValue value = static_cast<uint32_t>(val);
255 auto dumpIntf = resDumpEntry;
256
257 if (dumpType == PLDM_FILE_TYPE_DUMP)
258 {
259 dumpIntf = systemDumpEntry;
260 }
261
262 DBusMapping dbusMapping{path.c_str(), dumpIntf, "SourceDumpId",
263 "uint32_t"};
264 try
265 {
266 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
267 }
268 catch (const std::exception& e)
269 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600270 error(
271 "fileAck: Failed to make a d-bus call to DUMP manager to reset source dump id of {FILE_PATH}, with ERROR={ERR_EXCEP}",
272 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500273 pldm::utils::reportError(
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600274 "xyz.openbmc_project.PLDM.Error.fileAck.SourceDumpIdResetFail");
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500275 return PLDM_ERROR;
276 }
277
278 auto& bus = pldm::utils::DBusHandler::getBus();
279 try
280 {
281 auto method = bus.new_method_call(
282 "xyz.openbmc_project.Dump.Manager", path.c_str(),
283 "xyz.openbmc_project.Object.Delete", "Delete");
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500284 bus.call(method, dbusTimeout);
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500285 }
286 catch (const std::exception& e)
287 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600288 error(
289 "fileAck: Failed to make a d-bus method to delete the dump entry {FILE_PATH}, with ERROR={ERR_EXCEP}",
290 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500291 pldm::utils::reportError(
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600292 "xyz.openbmc_project.PLDM.Error.fileAck.DumpEntryDeleteFail");
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500293 return PLDM_ERROR;
294 }
295 return PLDM_SUCCESS;
296 }
297
Jayashankar Padathdb124362021-01-28 21:12:34 -0600298 if (dumpType == PLDM_FILE_TYPE_DUMP ||
299 dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP)
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600300 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500301 PropertyValue value{true};
302 DBusMapping dbusMapping{path, dumpEntry, "Offloaded", "bool"};
303 try
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600304 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500305 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600306 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500307 catch (const std::exception& e)
308 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600309 error(
310 "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}",
311 "FILE_PATH", path.c_str(), "ERR_EXCEP", e.what());
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500312 }
Ravi Tejace1c96f2020-10-05 23:13:01 -0500313
Ravi Tejace1c96f2020-10-05 23:13:01 -0500314 auto socketInterface = getOffloadUri(fileHandle);
315 std::remove(socketInterface.c_str());
Jayashankar Padath6289ea12022-06-13 12:33:10 -0500316 if (DumpHandler::fd >= 0)
317 {
318 close(DumpHandler::fd);
319 DumpHandler::fd = -1;
320 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600321 }
Jayashankar Padathdb124362021-01-28 21:12:34 -0600322 return PLDM_SUCCESS;
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600323 }
324
325 return PLDM_ERROR;
326}
327
Jayashankar Padathdb124362021-01-28 21:12:34 -0600328int DumpHandler::readIntoMemory(uint32_t offset, uint32_t& length,
329 uint64_t address,
330 oem_platform::Handler* /*oemPlatformHandler*/)
331{
332 if (dumpType != PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
333 {
334 return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
335 }
336 return transferFileData(resDumpDirPath, true, offset, length, address);
337}
338
339int DumpHandler::read(uint32_t offset, uint32_t& length, Response& response,
340 oem_platform::Handler* /*oemPlatformHandler*/)
341{
342 if (dumpType != PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS)
343 {
344 return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
345 }
346 return readFile(resDumpDirPath, offset, length, response);
347}
348
Sampa Misra18967162020-01-14 02:31:41 -0600349} // namespace responder
350} // namespace pldm