Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 1 | #include "file_io_type_dump.hpp" |
| 2 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 3 | #include "libpldm/base.h" |
| 4 | #include "oem/ibm/libpldm/file_io.h" |
| 5 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 6 | #include "common/utils.hpp" |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 7 | #include "utils.hpp" |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 8 | #include "xyz/openbmc_project/Common/error.hpp" |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | #include <systemd/sd-bus.h> |
| 12 | #include <unistd.h> |
| 13 | |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 14 | #include <sdbusplus/server.hpp> |
| 15 | #include <xyz/openbmc_project/Dump/NewDump/server.hpp> |
| 16 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 17 | #include <exception> |
| 18 | #include <filesystem> |
| 19 | #include <iostream> |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 20 | |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 21 | using namespace pldm::responder::utils; |
Deepak Kodihalli | fd279e1 | 2020-02-02 05:20:43 -0600 | [diff] [blame] | 22 | using namespace pldm::utils; |
| 23 | |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 24 | namespace pldm |
| 25 | { |
| 26 | namespace responder |
| 27 | { |
| 28 | |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 29 | static constexpr auto dumpEntry = "xyz.openbmc_project.Dump.Entry"; |
Deepak Kodihalli | 14b54a3 | 2020-09-29 05:20:02 -0500 | [diff] [blame] | 30 | static constexpr auto dumpObjPath = "/xyz/openbmc_project/dump/system"; |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 31 | int DumpHandler::fd = -1; |
| 32 | |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 33 | static 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 Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 73 | int DumpHandler::newFileAvailable(uint64_t length) |
| 74 | { |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 75 | static constexpr auto dumpInterface = "xyz.openbmc_project.Dump.NewDump"; |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 76 | 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 Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 101 | static std::string getOffloadUri(uint32_t fileHandle) |
| 102 | { |
| 103 | auto path = findDumpObjPath(fileHandle); |
| 104 | if (path.empty()) |
| 105 | { |
| 106 | return {}; |
| 107 | } |
| 108 | |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 109 | std::string socketInterface{}; |
| 110 | |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 111 | try |
| 112 | { |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 113 | socketInterface = |
| 114 | pldm::utils::DBusHandler().getDbusProperty<std::string>( |
| 115 | path.c_str(), "OffloadUri", dumpEntry); |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 116 | } |
| 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 Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 123 | return socketInterface; |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 126 | int DumpHandler::writeFromMemory(uint32_t, uint32_t length, uint64_t address) |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 127 | { |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 128 | if (DumpHandler::fd == -1) |
| 129 | { |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 130 | auto socketInterface = getOffloadUri(fileHandle); |
| 131 | int sock = setupUnixSocket(socketInterface); |
| 132 | if (sock < 0) |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 133 | { |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 134 | sock = -errno; |
| 135 | close(DumpHandler::fd); |
| 136 | std::cerr |
| 137 | << "DumpHandler::writeFromMemory: setupUnixSocket() failed" |
| 138 | << std::endl; |
| 139 | std::remove(socketInterface.c_str()); |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 140 | return PLDM_ERROR; |
| 141 | } |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 142 | |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 143 | DumpHandler::fd = sock; |
| 144 | } |
| 145 | return transferFileDataToSocket(DumpHandler::fd, length, address); |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 146 | } |
| 147 | |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 148 | int DumpHandler::write(const char* buffer, uint32_t, uint32_t& length) |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 149 | { |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 150 | int rc = writeToUnixSocket(DumpHandler::fd, buffer, length); |
| 151 | if (rc < 0) |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 152 | { |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 153 | 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 Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 159 | return PLDM_ERROR; |
| 160 | } |
| 161 | |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 162 | return PLDM_SUCCESS; |
| 163 | } |
| 164 | |
Deepak Kodihalli | fd279e1 | 2020-02-02 05:20:43 -0600 | [diff] [blame] | 165 | int DumpHandler::fileAck(uint8_t /*fileStatus*/) |
| 166 | { |
| 167 | if (DumpHandler::fd >= 0) |
| 168 | { |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 169 | auto path = findDumpObjPath(fileHandle); |
| 170 | if (!path.empty()) |
Deepak Kodihalli | fd279e1 | 2020-02-02 05:20:43 -0600 | [diff] [blame] | 171 | { |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 172 | PropertyValue value{true}; |
| 173 | DBusMapping dbusMapping{path, dumpEntry, "Offloaded", "bool"}; |
| 174 | try |
Deepak Kodihalli | fd279e1 | 2020-02-02 05:20:43 -0600 | [diff] [blame] | 175 | { |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 176 | pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value); |
Deepak Kodihalli | fd279e1 | 2020-02-02 05:20:43 -0600 | [diff] [blame] | 177 | } |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 178 | 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 Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 184 | |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 185 | close(DumpHandler::fd); |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame^] | 186 | auto socketInterface = getOffloadUri(fileHandle); |
| 187 | std::remove(socketInterface.c_str()); |
Deepak Kodihalli | 8cd6068 | 2020-04-02 02:59:22 -0500 | [diff] [blame] | 188 | DumpHandler::fd = -1; |
| 189 | return PLDM_SUCCESS; |
Deepak Kodihalli | fd279e1 | 2020-02-02 05:20:43 -0600 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | return PLDM_ERROR; |
| 194 | } |
| 195 | |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 196 | } // namespace responder |
| 197 | } // namespace pldm |