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 "platform-mc/manager.hpp" 10 #include "requester/handler.hpp" 11 #include "requester/mctp_endpoint_discovery.hpp" 12 #include "requester/request.hpp" 13 14 #include <err.h> 15 #include <getopt.h> 16 #include <libpldm/base.h> 17 #include <libpldm/bios.h> 18 #include <libpldm/pdr.h> 19 #include <libpldm/platform.h> 20 #include <libpldm/transport.h> 21 #include <poll.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 <cstdlib> 35 #include <cstring> 36 #include <fstream> 37 #include <iomanip> 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_terminus_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 "oem_ibm.hpp" 65 #endif 66 67 #ifdef OEM_AMPERE 68 #include "oem/ampere/oem_ampere.hpp" 69 #endif 70 71 constexpr const char* PLDMService = "xyz.openbmc_project.PLDM"; 72 73 using namespace pldm; 74 using namespace sdeventplus; 75 using namespace sdeventplus::source; 76 using namespace pldm::responder; 77 using namespace pldm::utils; 78 using sdeventplus::source::Signal; 79 using namespace pldm::flightrecorder; 80 81 void interruptFlightRecorderCallBack(Signal& /*signal*/, 82 const struct signalfd_siginfo*) 83 { 84 error("Received SIGUR1(10) Signal interrupt"); 85 // obtain the flight recorder instance and dump the recorder 86 FlightRecorder::GetInstance().playRecorder(); 87 } 88 89 void requestPLDMServiceName() 90 { 91 try 92 { 93 auto& bus = pldm::utils::DBusHandler::getBus(); 94 bus.request_name(PLDMService); 95 } 96 catch (const sdbusplus::exception_t& e) 97 { 98 error("Failed to request D-Bus name {NAME} with error {ERROR}.", "NAME", 99 PLDMService, "ERROR", e); 100 } 101 } 102 103 static std::optional<Response> 104 processRxMsg(const std::vector<uint8_t>& requestMsg, Invoker& invoker, 105 requester::Handler<requester::Request>& handler, 106 fw_update::Manager* fwManager, pldm_tid_t tid) 107 { 108 uint8_t eid = tid; 109 110 pldm_header_info hdrFields{}; 111 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(requestMsg.data()); 112 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields)) 113 { 114 error("Empty PLDM request header"); 115 return std::nullopt; 116 } 117 118 if (PLDM_RESPONSE != hdrFields.msg_type) 119 { 120 Response response; 121 auto request = reinterpret_cast<const pldm_msg*>(hdr); 122 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr); 123 try 124 { 125 if (hdrFields.pldm_type != PLDM_FWUP) 126 { 127 response = 128 invoker.handle(tid, hdrFields.pldm_type, hdrFields.command, 129 request, requestLen); 130 } 131 else 132 { 133 response = fwManager->handleRequest(eid, hdrFields.command, 134 request, requestLen); 135 } 136 } 137 catch (const std::out_of_range& e) 138 { 139 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD; 140 response.resize(sizeof(pldm_msg_hdr)); 141 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data()); 142 pldm_header_info header{}; 143 header.msg_type = PLDM_RESPONSE; 144 header.instance = hdrFields.instance; 145 header.pldm_type = hdrFields.pldm_type; 146 header.command = hdrFields.command; 147 if (PLDM_SUCCESS != pack_pldm_header(&header, responseHdr)) 148 { 149 error( 150 "Failed to add response header for processing Rx, error - {ERROR}", 151 "ERROR", e); 152 return std::nullopt; 153 } 154 response.insert(response.end(), completion_code); 155 } 156 return response; 157 } 158 else if (PLDM_RESPONSE == hdrFields.msg_type) 159 { 160 auto response = reinterpret_cast<const pldm_msg*>(hdr); 161 size_t responseLen = requestMsg.size() - sizeof(struct pldm_msg_hdr); 162 handler.handleResponse(eid, hdrFields.instance, hdrFields.pldm_type, 163 hdrFields.command, response, responseLen); 164 } 165 return std::nullopt; 166 } 167 168 void optionUsage(void) 169 { 170 info("Usage: pldmd [options]"); 171 info("Options:"); 172 info(" [--verbose] - would enable verbosity"); 173 } 174 175 int main(int argc, char** argv) 176 { 177 bool verbose = false; 178 static struct option long_options[] = {{"verbose", no_argument, 0, 'v'}, 179 {0, 0, 0, 0}}; 180 181 auto argflag = getopt_long(argc, argv, "v", long_options, nullptr); 182 switch (argflag) 183 { 184 case 'v': 185 verbose = true; 186 break; 187 case -1: 188 break; 189 default: 190 optionUsage(); 191 exit(EXIT_FAILURE); 192 } 193 // Setup PLDM requester transport 194 auto hostEID = pldm::utils::readHostEID(); 195 /* To maintain current behaviour until we have the infrastructure to find 196 * and use the correct TIDs */ 197 pldm_tid_t TID = hostEID; 198 PldmTransport pldmTransport{}; 199 auto event = Event::get_default(); 200 auto& bus = pldm::utils::DBusHandler::getBus(); 201 sdbusplus::server::manager_t objManager(bus, 202 "/xyz/openbmc_project/software"); 203 sdbusplus::server::manager_t sensorObjManager( 204 bus, "/xyz/openbmc_project/sensors"); 205 206 InstanceIdDb instanceIdDb; 207 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm", 208 instanceIdDb); 209 sdbusplus::server::manager_t inventoryManager( 210 bus, "/xyz/openbmc_project/inventory"); 211 212 Invoker invoker{}; 213 requester::Handler<requester::Request> reqHandler(&pldmTransport, event, 214 instanceIdDb, verbose); 215 216 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo( 217 pldm_pdr_init(), pldm_pdr_destroy); 218 if (!pdrRepo) 219 { 220 throw std::runtime_error("Failed to instantiate PDR repository"); 221 } 222 DBusHandler dbusHandler; 223 std::unique_ptr<pldm::host_effecters::HostEffecterParser> 224 hostEffecterParser = 225 std::make_unique<pldm::host_effecters::HostEffecterParser>( 226 &instanceIdDb, pldmTransport.getEventSource(), pdrRepo.get(), 227 &dbusHandler, HOST_JSONS_DIR, &reqHandler); 228 #ifdef LIBPLDMRESPONDER 229 using namespace pldm::state_sensor; 230 dbus_api::Host dbusImplHost(bus, "/xyz/openbmc_project/pldm"); 231 std::unique_ptr<pldm_entity_association_tree, 232 decltype(&pldm_entity_association_tree_destroy)> 233 entityTree(pldm_entity_association_tree_init(), 234 pldm_entity_association_tree_destroy); 235 if (!entityTree) 236 { 237 throw std::runtime_error( 238 "Failed to instantiate general PDR entity association tree"); 239 } 240 std::unique_ptr<pldm_entity_association_tree, 241 decltype(&pldm_entity_association_tree_destroy)> 242 bmcEntityTree(pldm_entity_association_tree_init(), 243 pldm_entity_association_tree_destroy); 244 if (!bmcEntityTree) 245 { 246 throw std::runtime_error( 247 "Failed to instantiate BMC PDR entity association tree"); 248 } 249 std::shared_ptr<HostPDRHandler> hostPDRHandler; 250 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler; 251 std::unique_ptr<platform_config::Handler> platformConfigHandler{}; 252 platformConfigHandler = 253 std::make_unique<platform_config::Handler>(PDR_JSONS_DIR); 254 255 if (hostEID) 256 { 257 hostPDRHandler = std::make_shared<HostPDRHandler>( 258 pldmTransport.getEventSource(), hostEID, event, pdrRepo.get(), 259 EVENTS_JSONS_DIR, entityTree.get(), bmcEntityTree.get(), 260 instanceIdDb, &reqHandler); 261 262 // HostFirmware interface needs access to hostPDR to know if host 263 // is running 264 dbusImplHost.setHostPdrObj(hostPDRHandler); 265 266 dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>( 267 pldmTransport.getEventSource(), hostEID, instanceIdDb, &reqHandler); 268 } 269 270 auto fruHandler = std::make_unique<fru::Handler>( 271 FRU_JSONS_DIR, FRU_MASTER_JSON, pdrRepo.get(), entityTree.get(), 272 bmcEntityTree.get()); 273 274 // FRU table is built lazily when a FRU command or Get PDR command is 275 // handled. To enable building FRU table, the FRU handler is passed to the 276 // Platform handler. 277 278 std::unique_ptr<platform_mc::Manager> platformManager = 279 std::make_unique<platform_mc::Manager>(event, reqHandler, instanceIdDb); 280 281 pldm::responder::platform::EventMap addOnEventHandlers{ 282 {PLDM_CPER_EVENT, 283 {[&platformManager](const pldm_msg* request, size_t payloadLength, 284 uint8_t formatVersion, uint8_t tid, 285 size_t eventDataOffset) { 286 return platformManager->handleCperEvent( 287 request, payloadLength, formatVersion, tid, eventDataOffset); 288 }}}, 289 {PLDM_MESSAGE_POLL_EVENT, 290 {[&platformManager](const pldm_msg* request, size_t payloadLength, 291 uint8_t formatVersion, uint8_t tid, 292 size_t eventDataOffset) { 293 return platformManager->handlePldmMessagePollEvent( 294 request, payloadLength, formatVersion, tid, eventDataOffset); 295 }}}, 296 {PLDM_SENSOR_EVENT, 297 {[&platformManager](const pldm_msg* request, size_t payloadLength, 298 uint8_t formatVersion, uint8_t tid, 299 size_t eventDataOffset) { 300 return platformManager->handleSensorEvent( 301 request, payloadLength, formatVersion, tid, eventDataOffset); 302 }}}}; 303 304 auto platformHandler = std::make_unique<platform::Handler>( 305 &dbusHandler, hostEID, &instanceIdDb, PDR_JSONS_DIR, pdrRepo.get(), 306 hostPDRHandler.get(), dbusToPLDMEventHandler.get(), fruHandler.get(), 307 platformConfigHandler.get(), &reqHandler, event, true, 308 addOnEventHandlers); 309 310 auto biosHandler = std::make_unique<bios::Handler>( 311 pldmTransport.getEventSource(), hostEID, &instanceIdDb, &reqHandler, 312 platformConfigHandler.get(), requestPLDMServiceName); 313 314 auto baseHandler = std::make_unique<base::Handler>(event); 315 316 #ifdef OEM_AMPERE 317 pldm::oem_ampere::OemAMPERE oemAMPERE( 318 &dbusHandler, pldmTransport.getEventSource(), pdrRepo.get(), 319 instanceIdDb, event, invoker, hostPDRHandler.get(), 320 platformHandler.get(), fruHandler.get(), baseHandler.get(), 321 biosHandler.get(), platformManager.get(), &reqHandler); 322 #endif 323 324 #ifdef OEM_IBM 325 pldm::oem_ibm::OemIBM oemIBM( 326 &dbusHandler, pldmTransport.getEventSource(), hostEID, pdrRepo.get(), 327 instanceIdDb, event, invoker, hostPDRHandler.get(), 328 platformHandler.get(), fruHandler.get(), baseHandler.get(), 329 &reqHandler); 330 #endif 331 332 invoker.registerHandler(PLDM_BIOS, std::move(biosHandler)); 333 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler)); 334 invoker.registerHandler(PLDM_FRU, std::move(fruHandler)); 335 invoker.registerHandler(PLDM_BASE, std::move(baseHandler)); 336 337 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get()); 338 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent( 339 bus, "/xyz/openbmc_project/pldm"); 340 341 #endif 342 343 std::unique_ptr<fw_update::Manager> fwManager = 344 std::make_unique<fw_update::Manager>(event, reqHandler, instanceIdDb); 345 std::unique_ptr<MctpDiscovery> mctpDiscoveryHandler = 346 std::make_unique<MctpDiscovery>( 347 bus, std::initializer_list<MctpDiscoveryHandlerIntf*>{ 348 fwManager.get(), platformManager.get()}); 349 auto callback = [verbose, &invoker, &reqHandler, &fwManager, &pldmTransport, 350 TID](IO& io, int fd, uint32_t revents) mutable { 351 if (!(revents & EPOLLIN)) 352 { 353 return; 354 } 355 if (fd < 0) 356 { 357 return; 358 } 359 360 int returnCode = 0; 361 void* requestMsg; 362 size_t recvDataLength; 363 returnCode = pldmTransport.recvMsg(TID, requestMsg, recvDataLength); 364 365 if (returnCode == PLDM_REQUESTER_SUCCESS) 366 { 367 std::vector<uint8_t> requestMsgVec( 368 static_cast<uint8_t*>(requestMsg), 369 static_cast<uint8_t*>(requestMsg) + recvDataLength); 370 FlightRecorder::GetInstance().saveRecord(requestMsgVec, false); 371 if (verbose) 372 { 373 printBuffer(Rx, requestMsgVec); 374 } 375 // process message and send response 376 auto response = processRxMsg(requestMsgVec, invoker, reqHandler, 377 fwManager.get(), TID); 378 if (response.has_value()) 379 { 380 FlightRecorder::GetInstance().saveRecord(*response, true); 381 if (verbose) 382 { 383 printBuffer(Tx, *response); 384 } 385 386 returnCode = pldmTransport.sendMsg(TID, (*response).data(), 387 (*response).size()); 388 if (returnCode != PLDM_REQUESTER_SUCCESS) 389 { 390 warning( 391 "Failed to send pldmTransport message for TID '{TID}', response code '{RETURN_CODE}'", 392 "TID", TID, "RETURN_CODE", returnCode); 393 } 394 } 395 } 396 // TODO check that we get here if mctp-demux dies? 397 else if (returnCode == PLDM_REQUESTER_RECV_FAIL) 398 { 399 // MCTP daemon has closed the socket this daemon is connected to. 400 // This may or may not be an error scenario, in either case the 401 // recovery mechanism for this daemon is to restart, and hence exit 402 // the event loop, that will cause this daemon to exit with a 403 // failure code. 404 error( 405 "MCTP daemon closed the socket, IO exiting with response code '{RC}'", 406 "RC", returnCode); 407 io.get_event().exit(0); 408 } 409 else 410 { 411 warning( 412 "Failed to receive PLDM request for pldmTransport, response code '{RETURN_CODE}'", 413 "RETURN_CODE", returnCode); 414 } 415 /* Free requestMsg after using */ 416 free(requestMsg); 417 }; 418 419 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); 420 #ifndef SYSTEM_SPECIFIC_BIOS_JSON 421 try 422 { 423 bus.request_name(PLDMService); 424 } 425 catch (const sdbusplus::exception_t& e) 426 { 427 error("Failed to request D-Bus name {NAME} with error {ERROR}.", "NAME", 428 PLDMService, "ERROR", e); 429 } 430 #endif 431 IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback)); 432 #ifdef LIBPLDMRESPONDER 433 if (hostPDRHandler) 434 { 435 hostPDRHandler->setHostFirmwareCondition(); 436 } 437 #endif 438 stdplus::signal::block(SIGUSR1); 439 sdeventplus::source::Signal sigUsr1( 440 event, SIGUSR1, std::bind_front(&interruptFlightRecorderCallBack)); 441 int returnCode = event.loop(); 442 if (returnCode) 443 { 444 exit(EXIT_FAILURE); 445 } 446 447 exit(EXIT_SUCCESS); 448 } 449