blob: 04f3639f3bb0a2b9f6e465158f8d24c257b79274 [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
Sampa Misra69508502020-09-08 00:08:21 -0500124int DumpHandler::writeFromMemory(uint32_t /*offset*/, uint32_t length,
125 uint64_t address,
126 oem_platform::Handler* /*oemPlatformHandler*/)
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
Sampa Misra69508502020-09-08 00:08:21 -0500148int DumpHandler::write(const char* buffer, uint32_t /*offset*/,
149 uint32_t& length,
150 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misra18967162020-01-14 02:31:41 -0600151{
Ravi Tejace1c96f2020-10-05 23:13:01 -0500152 int rc = writeToUnixSocket(DumpHandler::fd, buffer, length);
153 if (rc < 0)
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500154 {
Ravi Tejace1c96f2020-10-05 23:13:01 -0500155 rc = -errno;
156 close(DumpHandler::fd);
157 auto socketInterface = getOffloadUri(fileHandle);
158 std::remove(socketInterface.c_str());
159 std::cerr << "DumpHandler::write: writeToUnixSocket() failed"
160 << std::endl;
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500161 return PLDM_ERROR;
162 }
163
Sampa Misra18967162020-01-14 02:31:41 -0600164 return PLDM_SUCCESS;
165}
166
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600167int DumpHandler::fileAck(uint8_t /*fileStatus*/)
168{
169 if (DumpHandler::fd >= 0)
170 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500171 auto path = findDumpObjPath(fileHandle);
172 if (!path.empty())
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600173 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500174 PropertyValue value{true};
175 DBusMapping dbusMapping{path, dumpEntry, "Offloaded", "bool"};
176 try
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600177 {
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500178 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600179 }
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500180 catch (const std::exception& e)
181 {
182 std::cerr
183 << "failed to make a d-bus call to DUMP manager, ERROR="
184 << e.what() << "\n";
185 }
Ravi Tejace1c96f2020-10-05 23:13:01 -0500186
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500187 close(DumpHandler::fd);
Ravi Tejace1c96f2020-10-05 23:13:01 -0500188 auto socketInterface = getOffloadUri(fileHandle);
189 std::remove(socketInterface.c_str());
Deepak Kodihalli8cd60682020-04-02 02:59:22 -0500190 DumpHandler::fd = -1;
191 return PLDM_SUCCESS;
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600192 }
193 }
194
195 return PLDM_ERROR;
196}
197
Sampa Misra18967162020-01-14 02:31:41 -0600198} // namespace responder
199} // namespace pldm