George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 1 | #include "libpldm/base.h" |
| 2 | #include "libpldm/bios.h" |
| 3 | #include "libpldm/pdr.h" |
| 4 | #include "libpldm/platform.h" |
| 5 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 6 | #include "common/utils.hpp" |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 7 | #include "dbus_impl_requester.hpp" |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 8 | #include "fw-update/manager.hpp" |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 9 | #include "invoker.hpp" |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 10 | #include "requester/handler.hpp" |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 11 | #include "requester/mctp_endpoint_discovery.hpp" |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 12 | #include "requester/request.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 13 | |
| 14 | #include <err.h> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 15 | #include <getopt.h> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 16 | #include <poll.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/un.h> |
| 21 | #include <unistd.h> |
| 22 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 23 | #include <sdeventplus/event.hpp> |
| 24 | #include <sdeventplus/source/io.hpp> |
| 25 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 26 | #include <cstdio> |
| 27 | #include <cstring> |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 28 | #include <fstream> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 29 | #include <iomanip> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 30 | #include <iostream> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 31 | #include <iterator> |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 32 | #include <memory> |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 33 | #include <optional> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 34 | #include <sstream> |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 35 | #include <stdexcept> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 36 | #include <string> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 37 | #include <vector> |
| 38 | |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 39 | #ifdef LIBPLDMRESPONDER |
| 40 | #include "dbus_impl_pdr.hpp" |
| 41 | #include "host-bmc/dbus_to_event_handler.hpp" |
| 42 | #include "host-bmc/dbus_to_host_effecters.hpp" |
Tom Joseph | 20aa3e0 | 2021-08-17 04:44:19 -0700 | [diff] [blame] | 43 | #include "host-bmc/host_condition.hpp" |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 44 | #include "host-bmc/host_pdr_handler.hpp" |
| 45 | #include "libpldmresponder/base.hpp" |
| 46 | #include "libpldmresponder/bios.hpp" |
| 47 | #include "libpldmresponder/fru.hpp" |
| 48 | #include "libpldmresponder/oem_handler.hpp" |
| 49 | #include "libpldmresponder/platform.hpp" |
| 50 | #include "xyz/openbmc_project/PLDM/Event/server.hpp" |
| 51 | #endif |
| 52 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 53 | #ifdef OEM_IBM |
| 54 | #include "libpldmresponder/file_io.hpp" |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 55 | #include "libpldmresponder/oem_ibm_handler.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 56 | #endif |
| 57 | |
| 58 | constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1; |
| 59 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 60 | using namespace pldm; |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 61 | using namespace sdeventplus; |
| 62 | using namespace sdeventplus::source; |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 63 | using namespace pldm::responder; |
| 64 | using namespace pldm::utils; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 65 | |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 66 | static std::optional<Response> |
| 67 | processRxMsg(const std::vector<uint8_t>& requestMsg, Invoker& invoker, |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 68 | requester::Handler<requester::Request>& handler, |
| 69 | fw_update::Manager* fwManager) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 70 | { |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 71 | using type = uint8_t; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 72 | uint8_t eid = requestMsg[0]; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 73 | pldm_header_info hdrFields{}; |
| 74 | auto hdr = reinterpret_cast<const pldm_msg_hdr*>( |
| 75 | requestMsg.data() + sizeof(eid) + sizeof(type)); |
| 76 | if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields)) |
| 77 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 78 | std::cerr << "Empty PLDM request header \n"; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 79 | return std::nullopt; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 80 | } |
George Liu | b7095ff | 2021-06-14 16:01:57 +0800 | [diff] [blame] | 81 | |
| 82 | if (PLDM_RESPONSE != hdrFields.msg_type) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 83 | { |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 84 | Response response; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 85 | auto request = reinterpret_cast<const pldm_msg*>(hdr); |
| 86 | size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) - |
| 87 | sizeof(eid) - sizeof(type); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 88 | try |
| 89 | { |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 90 | if (hdrFields.pldm_type != PLDM_FWUP) |
| 91 | { |
| 92 | response = |
| 93 | invoker.handle(hdrFields.pldm_type, hdrFields.command, |
| 94 | request, requestLen); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | response = fwManager->handleRequest(eid, hdrFields.command, |
| 99 | request, requestLen); |
| 100 | } |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 101 | } |
| 102 | catch (const std::out_of_range& e) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 103 | { |
| 104 | uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD; |
| 105 | response.resize(sizeof(pldm_msg_hdr)); |
| 106 | auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data()); |
| 107 | pldm_header_info header{}; |
| 108 | header.msg_type = PLDM_RESPONSE; |
| 109 | header.instance = hdrFields.instance; |
| 110 | header.pldm_type = hdrFields.pldm_type; |
| 111 | header.command = hdrFields.command; |
George Liu | b7095ff | 2021-06-14 16:01:57 +0800 | [diff] [blame] | 112 | if (PLDM_SUCCESS != pack_pldm_header(&header, responseHdr)) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 113 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 114 | std::cerr << "Failed adding response header \n"; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 115 | return std::nullopt; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 116 | } |
| 117 | response.insert(response.end(), completion_code); |
| 118 | } |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 119 | return response; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 120 | } |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 121 | else if (PLDM_RESPONSE == hdrFields.msg_type) |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 122 | { |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 123 | auto response = reinterpret_cast<const pldm_msg*>(hdr); |
| 124 | size_t responseLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) - |
| 125 | sizeof(eid) - sizeof(type); |
| 126 | handler.handleResponse(eid, hdrFields.instance, hdrFields.pldm_type, |
| 127 | hdrFields.command, response, responseLen); |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 128 | } |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 129 | return std::nullopt; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 132 | void optionUsage(void) |
| 133 | { |
| 134 | std::cerr << "Usage: pldmd [options]\n"; |
| 135 | std::cerr << "Options:\n"; |
| 136 | std::cerr |
| 137 | << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n"; |
| 138 | std::cerr << "Defaulted settings: --verbose=0 \n"; |
| 139 | } |
| 140 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 141 | int main(int argc, char** argv) |
| 142 | { |
| 143 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 144 | bool verbose = false; |
| 145 | static struct option long_options[] = { |
| 146 | {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}}; |
| 147 | |
| 148 | auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr); |
| 149 | switch (argflag) |
| 150 | { |
| 151 | case 'v': |
| 152 | switch (std::stoi(optarg)) |
| 153 | { |
| 154 | case 0: |
| 155 | verbose = false; |
| 156 | break; |
| 157 | case 1: |
| 158 | verbose = true; |
| 159 | break; |
| 160 | default: |
| 161 | optionUsage(); |
Brad Bishop | c80f3ef | 2021-10-07 16:34:13 -0400 | [diff] [blame] | 162 | exit(EXIT_FAILURE); |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 163 | } |
| 164 | break; |
Brad Bishop | c80f3ef | 2021-10-07 16:34:13 -0400 | [diff] [blame] | 165 | case -1: |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 166 | break; |
Brad Bishop | c80f3ef | 2021-10-07 16:34:13 -0400 | [diff] [blame] | 167 | default: |
| 168 | exit(EXIT_FAILURE); |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 169 | } |
| 170 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 171 | /* Create local socket. */ |
| 172 | int returnCode = 0; |
| 173 | int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 174 | if (-1 == sockfd) |
| 175 | { |
| 176 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 177 | std::cerr << "Failed to create the socket, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 178 | exit(EXIT_FAILURE); |
| 179 | } |
| 180 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 181 | auto event = Event::get_default(); |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 182 | auto& bus = pldm::utils::DBusHandler::getBus(); |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 183 | sdbusplus::server::manager::manager objManager( |
| 184 | bus, "/xyz/openbmc_project/software"); |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 185 | dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm"); |
Pavithra Barithaya | 319ebb3 | 2021-05-06 06:09:11 -0500 | [diff] [blame] | 186 | |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 187 | Invoker invoker{}; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 188 | requester::Handler<requester::Request> reqHandler(sockfd, event, |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 189 | dbusImplReq, verbose); |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 190 | #ifdef LIBPLDMRESPONDER |
| 191 | using namespace pldm::state_sensor; |
Tom Joseph | 20aa3e0 | 2021-08-17 04:44:19 -0700 | [diff] [blame] | 192 | dbus_api::Host dbusImplHost(bus, "/xyz/openbmc_project/pldm"); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 193 | std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo( |
| 194 | pldm_pdr_init(), pldm_pdr_destroy); |
| 195 | std::unique_ptr<pldm_entity_association_tree, |
| 196 | decltype(&pldm_entity_association_tree_destroy)> |
| 197 | entityTree(pldm_entity_association_tree_init(), |
| 198 | pldm_entity_association_tree_destroy); |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame] | 199 | std::unique_ptr<pldm_entity_association_tree, |
| 200 | decltype(&pldm_entity_association_tree_destroy)> |
| 201 | bmcEntityTree(pldm_entity_association_tree_init(), |
| 202 | pldm_entity_association_tree_destroy); |
Pavithra Barithaya | 319ebb3 | 2021-05-06 06:09:11 -0500 | [diff] [blame] | 203 | std::shared_ptr<HostPDRHandler> hostPDRHandler; |
Sampa Misra | c0c6054 | 2020-07-01 02:34:25 -0500 | [diff] [blame] | 204 | std::unique_ptr<pldm::host_effecters::HostEffecterParser> |
| 205 | hostEffecterParser; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 206 | std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler; |
Brad Bishop | fc0d14d | 2021-10-12 19:34:20 -0400 | [diff] [blame] | 207 | DBusHandler dbusHandler; |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 208 | auto hostEID = pldm::utils::readHostEID(); |
| 209 | if (hostEID) |
| 210 | { |
Pavithra Barithaya | 319ebb3 | 2021-05-06 06:09:11 -0500 | [diff] [blame] | 211 | hostPDRHandler = std::make_shared<HostPDRHandler>( |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 212 | sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR, |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 213 | entityTree.get(), bmcEntityTree.get(), dbusImplReq, &reqHandler); |
Pavithra Barithaya | 319ebb3 | 2021-05-06 06:09:11 -0500 | [diff] [blame] | 214 | // HostFirmware interface needs access to hostPDR to know if host |
| 215 | // is running |
| 216 | dbusImplHost.setHostPdrObj(hostPDRHandler); |
| 217 | |
Sampa Misra | c0c6054 | 2020-07-01 02:34:25 -0500 | [diff] [blame] | 218 | hostEffecterParser = |
| 219 | std::make_unique<pldm::host_effecters::HostEffecterParser>( |
Brad Bishop | fc0d14d | 2021-10-12 19:34:20 -0400 | [diff] [blame] | 220 | &dbusImplReq, sockfd, pdrRepo.get(), &dbusHandler, |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 221 | HOST_JSONS_DIR, &reqHandler); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 222 | dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>( |
| 223 | sockfd, hostEID, dbusImplReq, &reqHandler); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 224 | } |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 225 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
| 226 | |
| 227 | #ifdef OEM_IBM |
| 228 | std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate = |
Brad Bishop | fc0d14d | 2021-10-12 19:34:20 -0400 | [diff] [blame] | 229 | std::make_unique<pldm::responder::CodeUpdate>(&dbusHandler); |
Varsha Kaverappa | 3ca29df | 2020-09-27 12:39:22 -0500 | [diff] [blame] | 230 | codeUpdate->clearDirPath(LID_STAGING_DIR); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 231 | oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>( |
Brad Bishop | fc0d14d | 2021-10-12 19:34:20 -0400 | [diff] [blame] | 232 | &dbusHandler, codeUpdate.get(), sockfd, hostEID, dbusImplReq, event, |
| 233 | &reqHandler); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 234 | codeUpdate->setOemPlatformHandler(oemPlatformHandler.get()); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 235 | invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>( |
| 236 | oemPlatformHandler.get(), sockfd, |
| 237 | hostEID, &dbusImplReq, &reqHandler)); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 238 | #endif |
Sagar Srinivas | a6a8ccd | 2021-04-01 07:58:33 -0500 | [diff] [blame] | 239 | invoker.registerHandler( |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 240 | PLDM_BIOS, std::make_unique<bios::Handler>(sockfd, hostEID, |
| 241 | &dbusImplReq, &reqHandler)); |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 242 | auto fruHandler = std::make_unique<fru::Handler>( |
Manojkiran Eda | 03b01ca | 2021-06-29 08:55:09 +0530 | [diff] [blame] | 243 | FRU_JSONS_DIR, FRU_MASTER_JSON, pdrRepo.get(), entityTree.get(), |
| 244 | bmcEntityTree.get()); |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 245 | // FRU table is built lazily when a FRU command or Get PDR command is |
| 246 | // handled. To enable building FRU table, the FRU handler is passed to the |
| 247 | // Platform handler. |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 248 | auto platformHandler = std::make_unique<platform::Handler>( |
Brad Bishop | fc0d14d | 2021-10-12 19:34:20 -0400 | [diff] [blame] | 249 | &dbusHandler, PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get(), |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 250 | dbusToPLDMEventHandler.get(), fruHandler.get(), |
Sampa Misra | 5fb37d5 | 2021-03-06 07:26:00 -0600 | [diff] [blame] | 251 | oemPlatformHandler.get(), event, true); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 252 | #ifdef OEM_IBM |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 253 | pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler = |
| 254 | dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>( |
| 255 | oemPlatformHandler.get()); |
| 256 | oemIbmPlatformHandler->setPlatformHandler(platformHandler.get()); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 257 | #endif |
| 258 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 259 | invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler)); |
Sagar Srinivas | 79669c9 | 2021-04-28 15:43:30 -0500 | [diff] [blame] | 260 | invoker.registerHandler( |
| 261 | PLDM_BASE, |
| 262 | std::make_unique<base::Handler>(hostEID, dbusImplReq, event, |
| 263 | oemPlatformHandler.get(), &reqHandler)); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 264 | invoker.registerHandler(PLDM_FRU, std::move(fruHandler)); |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 265 | dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get()); |
| 266 | sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent( |
| 267 | bus, "/xyz/openbmc_project/pldm"); |
Tom Joseph | 20aa3e0 | 2021-08-17 04:44:19 -0700 | [diff] [blame] | 268 | |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 269 | #endif |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 270 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 271 | pldm::utils::CustomFD socketFd(sockfd); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 272 | |
| 273 | struct sockaddr_un addr |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 274 | {}; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 275 | addr.sun_family = AF_UNIX; |
| 276 | const char path[] = "\0mctp-mux"; |
| 277 | memcpy(addr.sun_path, path, sizeof(path) - 1); |
| 278 | int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr), |
| 279 | sizeof(path) + sizeof(addr.sun_family) - 1); |
| 280 | if (-1 == result) |
| 281 | { |
| 282 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 283 | std::cerr << "Failed to connect to the socket, RC= " << returnCode |
| 284 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 285 | exit(EXIT_FAILURE); |
| 286 | } |
| 287 | |
| 288 | result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM)); |
| 289 | if (-1 == result) |
| 290 | { |
| 291 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 292 | std::cerr << "Failed to send message type as pldm to mctp, RC= " |
| 293 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 294 | exit(EXIT_FAILURE); |
| 295 | } |
| 296 | |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 297 | std::unique_ptr<fw_update::Manager> fwManager = |
| 298 | std::make_unique<fw_update::Manager>(event, reqHandler, dbusImplReq); |
| 299 | std::unique_ptr<MctpDiscovery> mctpDiscoveryHandler = |
| 300 | std::make_unique<MctpDiscovery>(bus, fwManager.get()); |
| 301 | |
| 302 | auto callback = [verbose, &invoker, &reqHandler, |
| 303 | &fwManager](IO& io, int fd, uint32_t revents) { |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 304 | if (!(revents & EPOLLIN)) |
| 305 | { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | // Outgoing message. |
| 310 | struct iovec iov[2]{}; |
| 311 | |
| 312 | // This structure contains the parameter information for the response |
| 313 | // message. |
| 314 | struct msghdr msg |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 315 | {}; |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 316 | |
| 317 | int returnCode = 0; |
| 318 | ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 319 | if (0 == peekedLength) |
| 320 | { |
Deepak Kodihalli | 23c5204 | 2020-09-01 03:04:32 -0500 | [diff] [blame] | 321 | // MCTP daemon has closed the socket this daemon is connected to. |
| 322 | // This may or may not be an error scenario, in either case the |
| 323 | // recovery mechanism for this daemon is to restart, and hence exit |
| 324 | // the event loop, that will cause this daemon to exit with a |
| 325 | // failure code. |
| 326 | io.get_event().exit(0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 327 | } |
| 328 | else if (peekedLength <= -1) |
| 329 | { |
| 330 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 331 | std::cerr << "recv system call failed, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 332 | } |
| 333 | else |
| 334 | { |
| 335 | std::vector<uint8_t> requestMsg(peekedLength); |
| 336 | auto recvDataLength = recv( |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 337 | fd, static_cast<void*>(requestMsg.data()), peekedLength, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 338 | if (recvDataLength == peekedLength) |
| 339 | { |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 340 | if (verbose) |
| 341 | { |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 342 | printBuffer(Rx, requestMsg); |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 343 | } |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 344 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 345 | if (MCTP_MSG_TYPE_PLDM != requestMsg[1]) |
| 346 | { |
| 347 | // Skip this message and continue. |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 348 | } |
| 349 | else |
| 350 | { |
| 351 | // process message and send response |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 352 | auto response = processRxMsg(requestMsg, invoker, |
| 353 | reqHandler, fwManager.get()); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 354 | if (response.has_value()) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 355 | { |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 356 | if (verbose) |
| 357 | { |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 358 | printBuffer(Tx, *response); |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 359 | } |
| 360 | |
Zahed Hossain | 09a96e0 | 2019-08-06 07:42:37 -0500 | [diff] [blame] | 361 | iov[0].iov_base = &requestMsg[0]; |
| 362 | iov[0].iov_len = |
| 363 | sizeof(requestMsg[0]) + sizeof(requestMsg[1]); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 364 | iov[1].iov_base = (*response).data(); |
| 365 | iov[1].iov_len = (*response).size(); |
Zahed Hossain | 09a96e0 | 2019-08-06 07:42:37 -0500 | [diff] [blame] | 366 | |
| 367 | msg.msg_iov = iov; |
| 368 | msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]); |
| 369 | |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 370 | int result = sendmsg(fd, &msg, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 371 | if (-1 == result) |
| 372 | { |
| 373 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 374 | std::cerr << "sendto system call failed, RC= " |
| 375 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | else |
| 381 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 382 | std::cerr |
| 383 | << "Failure to read peeked length packet. peekedLength= " |
| 384 | << peekedLength << " recvDataLength=" << recvDataLength |
| 385 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 386 | } |
| 387 | } |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 388 | }; |
| 389 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 390 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 391 | bus.request_name("xyz.openbmc_project.PLDM"); |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 392 | IO io(event, socketFd(), EPOLLIN, std::move(callback)); |
Manojkiran Eda | 88bc718 | 2021-09-08 18:37:30 +0530 | [diff] [blame] | 393 | #ifdef LIBPLDMRESPONDER |
| 394 | if (hostPDRHandler) |
| 395 | { |
| 396 | hostPDRHandler->setHostFirmwareCondition(); |
| 397 | } |
| 398 | #endif |
Brad Bishop | 3a22b97 | 2021-10-07 17:01:58 -0400 | [diff] [blame] | 399 | returnCode = event.loop(); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 400 | |
Brad Bishop | 3a22b97 | 2021-10-07 17:01:58 -0400 | [diff] [blame] | 401 | if (shutdown(sockfd, SHUT_RDWR)) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 402 | { |
Brad Bishop | 3a22b97 | 2021-10-07 17:01:58 -0400 | [diff] [blame] | 403 | std::perror("Failed to shutdown the socket"); |
| 404 | } |
| 405 | if (returnCode) |
| 406 | { |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 407 | exit(EXIT_FAILURE); |
| 408 | } |
Brad Bishop | 3a22b97 | 2021-10-07 17:01:58 -0400 | [diff] [blame] | 409 | |
| 410 | exit(EXIT_SUCCESS); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 411 | } |