blob: 3486372b980b1689bc2151e0f80df0652d3b7dcd [file] [log] [blame]
Sampa Misra18967162020-01-14 02:31:41 -06001#include "file_io_type_dump.hpp"
2
George Liu6492f522020-06-16 10:34:05 +08003#include "libpldm/base.h"
4#include "oem/ibm/libpldm/file_io.h"
5
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05006#include "common/utils.hpp"
Ravi Tejace1c96f2020-10-05 23:13:01 -05007#include "utils.hpp"
Sampa Misra18967162020-01-14 02:31:41 -06008#include "xyz/openbmc_project/Common/error.hpp"
9
10#include <stdint.h>
11#include <systemd/sd-bus.h>
12#include <unistd.h>
13
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>
Sampa Misra18967162020-01-14 02:31:41 -060020
Ravi Tejace1c96f2020-10-05 23:13:01 -050021using namespace pldm::responder::utils;
Deepak Kodihallifd279e12020-02-02 05:20:43 -060022using namespace pldm::utils;
23
Sampa Misra18967162020-01-14 02:31:41 -060024namespace pldm
25{
26namespace responder
27{
28
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050029static constexpr auto dumpEntry = "xyz.openbmc_project.Dump.Entry";
Deepak Kodihalli14b54a32020-09-29 05:20:02 -050030static constexpr auto dumpObjPath = "/xyz/openbmc_project/dump/system";
Sampa Misra18967162020-01-14 02:31:41 -060031int DumpHandler::fd = -1;
32
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050033static std::string findDumpObjPath(uint32_t fileHandle)
34{
35 static constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
36 static constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
37 static constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
38 static constexpr auto systemDumpEntry =
39 "xyz.openbmc_project.Dump.Entry.System";
40 auto& bus = pldm::utils::DBusHandler::getBus();
41
42 try
43 {
44 std::vector<std::string> paths;
45 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
46 MAPPER_INTERFACE, "GetSubTreePaths");
47 method.append(dumpObjPath);
48 method.append(0);
49 method.append(std::vector<std::string>({systemDumpEntry}));
50 auto reply = bus.call(method);
51 reply.read(paths);
52 for (const auto& path : paths)
53 {
54 auto dumpId = pldm::utils::DBusHandler().getDbusProperty<uint32_t>(
55 path.c_str(), "SourceDumpId", systemDumpEntry);
56 if (dumpId == fileHandle)
57 {
58 return path;
59 }
60 }
61 }
62 catch (const std::exception& e)
63 {
64 std::cerr << "failed to make a d-bus call to DUMP manager, ERROR="
65 << e.what() << "\n";
66 }
67
68 std::cerr << "failed to find dump object for dump id " << fileHandle
69 << "\n";
70 return {};
71}
72
Sampa Misra18967162020-01-14 02:31:41 -060073int DumpHandler::newFileAvailable(uint64_t length)
74{
Sampa Misra18967162020-01-14 02:31:41 -060075 static constexpr auto dumpInterface = "xyz.openbmc_project.Dump.NewDump";
Sampa Misra18967162020-01-14 02:31:41 -060076 auto& bus = pldm::utils::DBusHandler::getBus();
77
78 try
79 {
80 auto service =
81 pldm::utils::DBusHandler().getService(dumpObjPath, dumpInterface);
82 using namespace sdbusplus::xyz::openbmc_project::Dump::server;
83 auto method = bus.new_method_call(service.c_str(), dumpObjPath,
84 dumpInterface, "Notify");
Dhruvaraj Subhashchandran41989eb2020-11-27 00:22:42 -060085 method.append(fileHandle, length);
86
Sampa Misra18967162020-01-14 02:31:41 -060087 bus.call_noreply(method);
88 }
89 catch (const std::exception& e)
90 {
91 std::cerr << "failed to make a d-bus call to DUMP manager, ERROR="
92 << e.what() << "\n";
93 return PLDM_ERROR;
94 }
95
96 return PLDM_SUCCESS;
97}
98
Deepak Kodihalli8cd60682020-04-02 02:59:22 -050099static std::string getOffloadUri(uint32_t fileHandle)
100{
101 auto path = findDumpObjPath(fileHandle);
102 if (path.empty())
103 {
104 return {};
105 }
106
Ravi Tejace1c96f2020-10-05 23:13:01 -0500107 std::string socketInterface{};
108
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500109 try
110 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500111 socketInterface =
112 pldm::utils::DBusHandler().getDbusProperty<std::string>(
113 path.c_str(), "OffloadUri", dumpEntry);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500114 }
115 catch (const std::exception& e)
116 {
117 std::cerr << "failed to make a d-bus call to DUMP manager, ERROR="
118 << e.what() << "\n";
119 }
120
Ravi Tejace1c96f2020-10-05 23:13:01 -0500121 return socketInterface;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500122}
123
Ravi Tejace1c96f2020-10-05 23:13:01 -0500124int DumpHandler::writeFromMemory(uint32_t, uint32_t length, uint64_t address)
Sampa Misra18967162020-01-14 02:31:41 -0600125{
Sampa Misra18967162020-01-14 02:31:41 -0600126 if (DumpHandler::fd == -1)
127 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500128 auto socketInterface = getOffloadUri(fileHandle);
129 int sock = setupUnixSocket(socketInterface);
130 if (sock < 0)
Sampa Misra18967162020-01-14 02:31:41 -0600131 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500132 sock = -errno;
133 close(DumpHandler::fd);
134 std::cerr
135 << "DumpHandler::writeFromMemory: setupUnixSocket() failed"
136 << std::endl;
137 std::remove(socketInterface.c_str());
Sampa Misra18967162020-01-14 02:31:41 -0600138 return PLDM_ERROR;
139 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500140
Ravi Tejace1c96f2020-10-05 23:13:01 -0500141 DumpHandler::fd = sock;
142 }
143 return transferFileDataToSocket(DumpHandler::fd, length, address);
Sampa Misra18967162020-01-14 02:31:41 -0600144}
145
Ravi Tejace1c96f2020-10-05 23:13:01 -0500146int DumpHandler::write(const char* buffer, uint32_t, uint32_t& length)
Sampa Misra18967162020-01-14 02:31:41 -0600147{
Ravi Tejace1c96f2020-10-05 23:13:01 -0500148 int rc = writeToUnixSocket(DumpHandler::fd, buffer, length);
149 if (rc < 0)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500150 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500151 rc = -errno;
152 close(DumpHandler::fd);
153 auto socketInterface = getOffloadUri(fileHandle);
154 std::remove(socketInterface.c_str());
155 std::cerr << "DumpHandler::write: writeToUnixSocket() failed"
156 << std::endl;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500157 return PLDM_ERROR;
158 }
159
Sampa Misra18967162020-01-14 02:31:41 -0600160 return PLDM_SUCCESS;
161}
162
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600163int DumpHandler::fileAck(uint8_t /*fileStatus*/)
164{
165 if (DumpHandler::fd >= 0)
166 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500167 auto path = findDumpObjPath(fileHandle);
168 if (!path.empty())
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600169 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500170 PropertyValue value{true};
171 DBusMapping dbusMapping{path, dumpEntry, "Offloaded", "bool"};
172 try
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600173 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500174 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600175 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500176 catch (const std::exception& e)
177 {
178 std::cerr
179 << "failed to make a d-bus call to DUMP manager, ERROR="
180 << e.what() << "\n";
181 }
Ravi Tejace1c96f2020-10-05 23:13:01 -0500182
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500183 close(DumpHandler::fd);
Ravi Tejace1c96f2020-10-05 23:13:01 -0500184 auto socketInterface = getOffloadUri(fileHandle);
185 std::remove(socketInterface.c_str());
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500186 DumpHandler::fd = -1;
187 return PLDM_SUCCESS;
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600188 }
189 }
190
191 return PLDM_ERROR;
192}
193
Sampa Misra18967162020-01-14 02:31:41 -0600194} // namespace responder
195} // namespace pldm