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