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