1 2 #include "common/flight_recorder.hpp" 3 #include "common/instance_id.hpp" 4 #include "common/transport.hpp" 5 #include "common/utils.hpp" 6 #include "dbus_impl_requester.hpp" 7 #include "fw-update/manager.hpp" 8 #include "invoker.hpp" 9 #include "requester/handler.hpp" 10 #include "requester/mctp_endpoint_discovery.hpp" 11 #include "requester/request.hpp" 12 13 #include <err.h> 14 #include <getopt.h> 15 #include <libpldm/base.h> 16 #include <libpldm/bios.h> 17 #include <libpldm/pdr.h> 18 #include <libpldm/platform.h> 19 #include <libpldm/transport.h> 20 #include <poll.h> 21 #include <stdlib.h> 22 #include <sys/socket.h> 23 #include <sys/types.h> 24 #include <sys/un.h> 25 #include <unistd.h> 26 27 #include <phosphor-logging/lg2.hpp> 28 #include <sdeventplus/event.hpp> 29 #include <sdeventplus/source/io.hpp> 30 #include <sdeventplus/source/signal.hpp> 31 #include <stdplus/signal.hpp> 32 33 #include <cstdio> 34 #include <cstring> 35 #include <fstream> 36 #include <iomanip> 37 #include <iostream> 38 #include <iterator> 39 #include <memory> 40 #include <ranges> 41 #include <sstream> 42 #include <stdexcept> 43 #include <string> 44 #include <vector> 45 46 PHOSPHOR_LOG2_USING; 47 48 #ifdef LIBPLDMRESPONDER 49 #include "dbus_impl_pdr.hpp" 50 #include "host-bmc/dbus_to_event_handler.hpp" 51 #include "host-bmc/dbus_to_host_effecters.hpp" 52 #include "host-bmc/host_condition.hpp" 53 #include "host-bmc/host_pdr_handler.hpp" 54 #include "libpldmresponder/base.hpp" 55 #include "libpldmresponder/bios.hpp" 56 #include "libpldmresponder/fru.hpp" 57 #include "libpldmresponder/oem_handler.hpp" 58 #include "libpldmresponder/platform.hpp" 59 #include "libpldmresponder/platform_config.hpp" 60 #include "xyz/openbmc_project/PLDM/Event/server.hpp" 61 #endif 62 63 #ifdef OEM_IBM 64 #include "libpldmresponder/file_io.hpp" 65 #include "libpldmresponder/fru_oem_ibm.hpp" 66 #include "libpldmresponder/oem_ibm_handler.hpp" 67 #endif 68 69 constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1; 70 71 using namespace pldm; 72 using namespace sdeventplus; 73 using namespace sdeventplus::source; 74 using namespace pldm::responder; 75 using namespace pldm::utils; 76 using sdeventplus::source::Signal; 77 using namespace pldm::flightrecorder; 78 79 void interruptFlightRecorderCallBack(Signal& /*signal*/, 80 const struct signalfd_siginfo*) 81 { 82 error("Received SIGUR1(10) Signal interrupt"); 83 // obtain the flight recorder instance and dump the recorder 84 FlightRecorder::GetInstance().playRecorder(); 85 } 86 87 static std::optional<Response> 88 processRxMsg(const std::vector<uint8_t>& requestMsg, Invoker& invoker, 89 requester::Handler<requester::Request>& handler, 90 fw_update::Manager* fwManager, pldm_tid_t tid) 91 { 92 uint8_t eid = tid; 93 94 pldm_header_info hdrFields{}; 95 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(requestMsg.data()); 96 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields)) 97 { 98 error("Empty PLDM request header"); 99 return std::nullopt; 100 } 101 102 if (PLDM_RESPONSE != hdrFields.msg_type) 103 { 104 Response response; 105 auto request = reinterpret_cast<const pldm_msg*>(hdr); 106 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr); 107 try 108 { 109 if (hdrFields.pldm_type != PLDM_FWUP) 110 { 111 response = invoker.handle(tid, hdrFields.pldm_type, 112 hdrFields.command, request, 113 requestLen); 114 } 115 else 116 { 117 response = fwManager->handleRequest(eid, hdrFields.command, 118 request, requestLen); 119 } 120 } 121 catch (const std::out_of_range& e) 122 { 123 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD; 124 response.resize(sizeof(pldm_msg_hdr)); 125 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data()); 126 pldm_header_info header{}; 127 header.msg_type = PLDM_RESPONSE; 128 header.instance = hdrFields.instance; 129 header.pldm_type = hdrFields.pldm_type; 130 header.command = hdrFields.command; 131 if (PLDM_SUCCESS != pack_pldm_header(&header, responseHdr)) 132 { 133 error("Failed adding response header: {ERROR}", "ERROR", e); 134 return std::nullopt; 135 } 136 response.insert(response.end(), completion_code); 137 } 138 return response; 139 } 140 else if (PLDM_RESPONSE == hdrFields.msg_type) 141 { 142 auto response = reinterpret_cast<const pldm_msg*>(hdr); 143 size_t responseLen = requestMsg.size() - sizeof(struct pldm_msg_hdr); 144 handler.handleResponse(eid, hdrFields.instance, hdrFields.pldm_type, 145 hdrFields.command, response, responseLen); 146 } 147 return std::nullopt; 148 } 149 150 void optionUsage(void) 151 { 152 info("Usage: pldmd [options]"); 153 info("Options:"); 154 info(" [--verbose] - would enable verbosity"); 155 } 156 157 int main(int argc, char** argv) 158 { 159 bool verbose = false; 160 static struct option long_options[] = {{"verbose", no_argument, 0, 'v'}, 161 {0, 0, 0, 0}}; 162 163 auto argflag = getopt_long(argc, argv, "v", long_options, nullptr); 164 switch (argflag) 165 { 166 case 'v': 167 verbose = true; 168 break; 169 case -1: 170 break; 171 default: 172 optionUsage(); 173 exit(EXIT_FAILURE); 174 } 175 // Setup PLDM requester transport 176 auto hostEID = pldm::utils::readHostEID(); 177 /* To maintain current behaviour until we have the infrastructure to find 178 * and use the correct TIDs */ 179 pldm_tid_t TID = hostEID; 180 PldmTransport pldmTransport{}; 181 auto event = Event::get_default(); 182 auto& bus = pldm::utils::DBusHandler::getBus(); 183 sdbusplus::server::manager_t objManager(bus, 184 "/xyz/openbmc_project/software"); 185 186 InstanceIdDb instanceIdDb; 187 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm", 188 instanceIdDb); 189 sdbusplus::server::manager_t inventoryManager( 190 bus, "/xyz/openbmc_project/inventory"); 191 192 Invoker invoker{}; 193 requester::Handler<requester::Request> reqHandler(&pldmTransport, event, 194 instanceIdDb, verbose); 195 196 #ifdef LIBPLDMRESPONDER 197 using namespace pldm::state_sensor; 198 dbus_api::Host dbusImplHost(bus, "/xyz/openbmc_project/pldm"); 199 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo( 200 pldm_pdr_init(), pldm_pdr_destroy); 201 if (!pdrRepo) 202 { 203 throw std::runtime_error("Failed to instantiate PDR repository"); 204 } 205 std::unique_ptr<pldm_entity_association_tree, 206 decltype(&pldm_entity_association_tree_destroy)> 207 entityTree(pldm_entity_association_tree_init(), 208 pldm_entity_association_tree_destroy); 209 if (!entityTree) 210 { 211 throw std::runtime_error( 212 "Failed to instantiate general PDR entity association tree"); 213 } 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); 218 if (!bmcEntityTree) 219 { 220 throw std::runtime_error( 221 "Failed to instantiate BMC PDR entity association tree"); 222 } 223 std::shared_ptr<HostPDRHandler> hostPDRHandler; 224 std::unique_ptr<pldm::host_effecters::HostEffecterParser> 225 hostEffecterParser; 226 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler; 227 DBusHandler dbusHandler; 228 std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; 229 std::unique_ptr<platform_config::Handler> platformConfigHandler{}; 230 platformConfigHandler = std::make_unique<platform_config::Handler>(); 231 std::unique_ptr<oem_fru::Handler> oemFruHandler{}; 232 233 #ifdef OEM_IBM 234 std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate = 235 std::make_unique<pldm::responder::CodeUpdate>(&dbusHandler); 236 codeUpdate->clearDirPath(LID_STAGING_DIR); 237 oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>( 238 &dbusHandler, codeUpdate.get(), pldmTransport.getEventSource(), hostEID, 239 instanceIdDb, event, &reqHandler); 240 codeUpdate->setOemPlatformHandler(oemPlatformHandler.get()); 241 oemFruHandler = std::make_unique<oem_ibm_fru::Handler>(pdrRepo.get()); 242 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>( 243 oemPlatformHandler.get(), 244 pldmTransport.getEventSource(), 245 hostEID, &instanceIdDb, &reqHandler)); 246 #endif 247 if (hostEID) 248 { 249 hostPDRHandler = std::make_shared<HostPDRHandler>( 250 pldmTransport.getEventSource(), hostEID, event, pdrRepo.get(), 251 EVENTS_JSONS_DIR, entityTree.get(), bmcEntityTree.get(), 252 instanceIdDb, &reqHandler, oemPlatformHandler.get()); 253 // HostFirmware interface needs access to hostPDR to know if host 254 // is running 255 dbusImplHost.setHostPdrObj(hostPDRHandler); 256 257 hostEffecterParser = 258 std::make_unique<pldm::host_effecters::HostEffecterParser>( 259 &instanceIdDb, pldmTransport.getEventSource(), pdrRepo.get(), 260 &dbusHandler, HOST_JSONS_DIR, &reqHandler); 261 dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>( 262 pldmTransport.getEventSource(), hostEID, instanceIdDb, &reqHandler); 263 } 264 auto biosHandler = std::make_unique<bios::Handler>( 265 pldmTransport.getEventSource(), hostEID, &instanceIdDb, &reqHandler, 266 platformConfigHandler.get()); 267 268 auto fruHandler = std::make_unique<fru::Handler>( 269 FRU_JSONS_DIR, FRU_MASTER_JSON, pdrRepo.get(), entityTree.get(), 270 bmcEntityTree.get(), oemFruHandler.get()); 271 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. 275 auto platformHandler = std::make_unique<platform::Handler>( 276 &dbusHandler, hostEID, &instanceIdDb, PDR_JSONS_DIR, pdrRepo.get(), 277 hostPDRHandler.get(), dbusToPLDMEventHandler.get(), fruHandler.get(), 278 oemPlatformHandler.get(), platformConfigHandler.get(), &reqHandler, 279 event, true); 280 #ifdef OEM_IBM 281 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler = 282 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>( 283 oemPlatformHandler.get()); 284 oemIbmPlatformHandler->setPlatformHandler(platformHandler.get()); 285 286 pldm::responder::oem_ibm_fru::Handler* oemIbmFruHandler = 287 dynamic_cast<pldm::responder::oem_ibm_fru::Handler*>( 288 oemFruHandler.get()); 289 oemIbmFruHandler->setIBMFruHandler(fruHandler.get()); 290 #endif 291 292 invoker.registerHandler(PLDM_BIOS, std::move(biosHandler)); 293 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler)); 294 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>( 295 event, oemPlatformHandler.get())); 296 invoker.registerHandler(PLDM_FRU, std::move(fruHandler)); 297 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get()); 298 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent( 299 bus, "/xyz/openbmc_project/pldm"); 300 301 #endif 302 303 std::unique_ptr<fw_update::Manager> fwManager = 304 std::make_unique<fw_update::Manager>(event, reqHandler, instanceIdDb); 305 std::unique_ptr<MctpDiscovery> mctpDiscoveryHandler = 306 std::make_unique<MctpDiscovery>(bus, fwManager.get()); 307 auto callback = [verbose, &invoker, &reqHandler, &fwManager, &pldmTransport, 308 TID](IO& io, int fd, uint32_t revents) mutable { 309 if (!(revents & EPOLLIN)) 310 { 311 return; 312 } 313 if (fd < 0) 314 { 315 return; 316 } 317 318 int returnCode = 0; 319 void* requestMsg; 320 size_t recvDataLength; 321 returnCode = pldmTransport.recvMsg(TID, requestMsg, recvDataLength); 322 323 if (returnCode == PLDM_REQUESTER_SUCCESS) 324 { 325 std::vector<uint8_t> requestMsgVec( 326 static_cast<uint8_t*>(requestMsg), 327 static_cast<uint8_t*>(requestMsg) + recvDataLength); 328 FlightRecorder::GetInstance().saveRecord(requestMsgVec, false); 329 if (verbose) 330 { 331 printBuffer(Rx, requestMsgVec); 332 } 333 // process message and send response 334 auto response = processRxMsg(requestMsgVec, invoker, reqHandler, 335 fwManager.get(), TID); 336 if (response.has_value()) 337 { 338 FlightRecorder::GetInstance().saveRecord(*response, true); 339 if (verbose) 340 { 341 printBuffer(Tx, *response); 342 } 343 344 returnCode = pldmTransport.sendMsg(TID, (*response).data(), 345 (*response).size()); 346 if (returnCode != PLDM_REQUESTER_SUCCESS) 347 { 348 warning("Failed to send PLDM response: {RETURN_CODE}", 349 "RETURN_CODE", returnCode); 350 } 351 } 352 } 353 // TODO check that we get here if mctp-demux dies? 354 else if (returnCode == PLDM_REQUESTER_RECV_FAIL) 355 { 356 // MCTP daemon has closed the socket this daemon is connected to. 357 // This may or may not be an error scenario, in either case the 358 // recovery mechanism for this daemon is to restart, and hence exit 359 // the event loop, that will cause this daemon to exit with a 360 // failure code. 361 error("io exiting"); 362 io.get_event().exit(0); 363 } 364 else 365 { 366 warning("Failed to receive PLDM request: {RETURN_CODE}", 367 "RETURN_CODE", returnCode); 368 } 369 }; 370 371 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); 372 bus.request_name("xyz.openbmc_project.PLDM"); 373 IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback)); 374 #ifdef LIBPLDMRESPONDER 375 if (hostPDRHandler) 376 { 377 hostPDRHandler->setHostFirmwareCondition(); 378 } 379 #endif 380 stdplus::signal::block(SIGUSR1); 381 sdeventplus::source::Signal sigUsr1( 382 event, SIGUSR1, std::bind_front(&interruptFlightRecorderCallBack)); 383 int returnCode = event.loop(); 384 if (returnCode) 385 { 386 exit(EXIT_FAILURE); 387 } 388 389 exit(EXIT_SUCCESS); 390 } 391