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