blob: 38647ab6a8f46be505902d20b711399995330810 [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");
85 method.append(
86 sdbusplus::xyz::openbmc_project::Dump::server::convertForMessage(
87 NewDump::DumpType::System),
88 fileHandle, length);
89 bus.call_noreply(method);
90 }
91 catch (const std::exception& e)
92 {
93 std::cerr << "failed to make a d-bus call to DUMP manager, ERROR="
94 << e.what() << "\n";
95 return PLDM_ERROR;
96 }
97
98 return PLDM_SUCCESS;
99}
100
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500101static std::string getOffloadUri(uint32_t fileHandle)
102{
103 auto path = findDumpObjPath(fileHandle);
104 if (path.empty())
105 {
106 return {};
107 }
108
Ravi Tejace1c96f2020-10-05 23:13:01 -0500109 std::string socketInterface{};
110
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500111 try
112 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500113 socketInterface =
114 pldm::utils::DBusHandler().getDbusProperty<std::string>(
115 path.c_str(), "OffloadUri", dumpEntry);
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500116 }
117 catch (const std::exception& e)
118 {
119 std::cerr << "failed to make a d-bus call to DUMP manager, ERROR="
120 << e.what() << "\n";
121 }
122
Ravi Tejace1c96f2020-10-05 23:13:01 -0500123 return socketInterface;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500124}
125
Ravi Tejace1c96f2020-10-05 23:13:01 -0500126int DumpHandler::writeFromMemory(uint32_t, uint32_t length, uint64_t address)
Sampa Misra18967162020-01-14 02:31:41 -0600127{
Sampa Misra18967162020-01-14 02:31:41 -0600128 if (DumpHandler::fd == -1)
129 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500130 auto socketInterface = getOffloadUri(fileHandle);
131 int sock = setupUnixSocket(socketInterface);
132 if (sock < 0)
Sampa Misra18967162020-01-14 02:31:41 -0600133 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500134 sock = -errno;
135 close(DumpHandler::fd);
136 std::cerr
137 << "DumpHandler::writeFromMemory: setupUnixSocket() failed"
138 << std::endl;
139 std::remove(socketInterface.c_str());
Sampa Misra18967162020-01-14 02:31:41 -0600140 return PLDM_ERROR;
141 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500142
Ravi Tejace1c96f2020-10-05 23:13:01 -0500143 DumpHandler::fd = sock;
144 }
145 return transferFileDataToSocket(DumpHandler::fd, length, address);
Sampa Misra18967162020-01-14 02:31:41 -0600146}
147
Ravi Tejace1c96f2020-10-05 23:13:01 -0500148int DumpHandler::write(const char* buffer, uint32_t, uint32_t& length)
Sampa Misra18967162020-01-14 02:31:41 -0600149{
Ravi Tejace1c96f2020-10-05 23:13:01 -0500150 int rc = writeToUnixSocket(DumpHandler::fd, buffer, length);
151 if (rc < 0)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500152 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500153 rc = -errno;
154 close(DumpHandler::fd);
155 auto socketInterface = getOffloadUri(fileHandle);
156 std::remove(socketInterface.c_str());
157 std::cerr << "DumpHandler::write: writeToUnixSocket() failed"
158 << std::endl;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500159 return PLDM_ERROR;
160 }
161
Sampa Misra18967162020-01-14 02:31:41 -0600162 return PLDM_SUCCESS;
163}
164
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600165int DumpHandler::fileAck(uint8_t /*fileStatus*/)
166{
167 if (DumpHandler::fd >= 0)
168 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500169 auto path = findDumpObjPath(fileHandle);
170 if (!path.empty())
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600171 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500172 PropertyValue value{true};
173 DBusMapping dbusMapping{path, dumpEntry, "Offloaded", "bool"};
174 try
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600175 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500176 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600177 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500178 catch (const std::exception& e)
179 {
180 std::cerr
181 << "failed to make a d-bus call to DUMP manager, ERROR="
182 << e.what() << "\n";
183 }
Ravi Tejace1c96f2020-10-05 23:13:01 -0500184
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500185 close(DumpHandler::fd);
Ravi Tejace1c96f2020-10-05 23:13:01 -0500186 auto socketInterface = getOffloadUri(fileHandle);
187 std::remove(socketInterface.c_str());
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500188 DumpHandler::fd = -1;
189 return PLDM_SUCCESS;
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600190 }
191 }
192
193 return PLDM_ERROR;
194}
195
Sampa Misra18967162020-01-14 02:31:41 -0600196} // namespace responder
197} // namespace pldm