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
interruptFlightRecorderCallBack(Signal &,const struct signalfd_siginfo *)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
requestPLDMServiceName()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
processRxMsg(const std::vector<uint8_t> & requestMsg,Invoker & invoker,requester::Handler<requester::Request> & handler,fw_update::Manager * fwManager,pldm_tid_t tid)103 static std::optional<Response> processRxMsg(
104 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 = new (response.data()) pldm_msg_hdr;
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
optionUsage(void)168 void optionUsage(void)
169 {
170 info("Usage: pldmd [options]");
171 info("Options:");
172 info(" [--verbose] - would enable verbosity");
173 }
174
main(int argc,char ** argv)175 int main(int argc, char** argv)
176 {
177 bool verbose = false;
178 static struct option long_options[] = {
179 {"verbose", no_argument, nullptr, 'v'}, {nullptr, 0, nullptr, 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
224 std::unique_ptr<platform_mc::Manager> platformManager =
225 std::make_unique<platform_mc::Manager>(event, reqHandler, instanceIdDb);
226
227 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
228 hostEffecterParser =
229 std::make_unique<pldm::host_effecters::HostEffecterParser>(
230 &instanceIdDb, pldmTransport.getEventSource(), pdrRepo.get(),
231 &dbusHandler, HOST_JSONS_DIR, &reqHandler,
232 platformManager.get());
233 #ifdef LIBPLDMRESPONDER
234 using namespace pldm::state_sensor;
235 dbus_api::Host dbusImplHost(bus, "/xyz/openbmc_project/pldm");
236 std::unique_ptr<pldm_entity_association_tree,
237 decltype(&pldm_entity_association_tree_destroy)>
238 entityTree(pldm_entity_association_tree_init(),
239 pldm_entity_association_tree_destroy);
240 if (!entityTree)
241 {
242 throw std::runtime_error(
243 "Failed to instantiate general PDR entity association tree");
244 }
245 std::unique_ptr<pldm_entity_association_tree,
246 decltype(&pldm_entity_association_tree_destroy)>
247 bmcEntityTree(pldm_entity_association_tree_init(),
248 pldm_entity_association_tree_destroy);
249 if (!bmcEntityTree)
250 {
251 throw std::runtime_error(
252 "Failed to instantiate BMC PDR entity association tree");
253 }
254 std::shared_ptr<HostPDRHandler> hostPDRHandler;
255 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler;
256 std::unique_ptr<platform_config::Handler> platformConfigHandler{};
257 platformConfigHandler =
258 std::make_unique<platform_config::Handler>(PDR_JSONS_DIR);
259
260 if (hostEID)
261 {
262 hostPDRHandler = std::make_shared<HostPDRHandler>(
263 pldmTransport.getEventSource(), hostEID, event, pdrRepo.get(),
264 EVENTS_JSONS_DIR, entityTree.get(), bmcEntityTree.get(),
265 instanceIdDb, &reqHandler);
266
267 // HostFirmware interface needs access to hostPDR to know if host
268 // is running
269 dbusImplHost.setHostPdrObj(hostPDRHandler);
270
271 dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>(
272 pldmTransport.getEventSource(), hostEID, instanceIdDb, &reqHandler);
273 }
274
275 auto fruHandler = std::make_unique<fru::Handler>(
276 FRU_JSONS_DIR, FRU_MASTER_JSON, pdrRepo.get(), entityTree.get(),
277 bmcEntityTree.get());
278
279 // FRU table is built lazily when a FRU command or Get PDR command is
280 // handled. To enable building FRU table, the FRU handler is passed to the
281 // Platform handler.
282
283 pldm::responder::platform::EventMap addOnEventHandlers{
284 {PLDM_CPER_EVENT,
285 {[&platformManager](const pldm_msg* request, size_t payloadLength,
286 uint8_t formatVersion, uint8_t tid,
287 size_t eventDataOffset) {
288 return platformManager->handleCperEvent(
289 request, payloadLength, formatVersion, tid, eventDataOffset);
290 }}},
291 {PLDM_MESSAGE_POLL_EVENT,
292 {[&platformManager](const pldm_msg* request, size_t payloadLength,
293 uint8_t formatVersion, uint8_t tid,
294 size_t eventDataOffset) {
295 return platformManager->handlePldmMessagePollEvent(
296 request, payloadLength, formatVersion, tid, eventDataOffset);
297 }}},
298 {PLDM_SENSOR_EVENT,
299 {[&platformManager](const pldm_msg* request, size_t payloadLength,
300 uint8_t formatVersion, uint8_t tid,
301 size_t eventDataOffset) {
302 return platformManager->handleSensorEvent(
303 request, payloadLength, formatVersion, tid, eventDataOffset);
304 }}}};
305
306 auto platformHandler = std::make_unique<platform::Handler>(
307 &dbusHandler, hostEID, &instanceIdDb, PDR_JSONS_DIR, pdrRepo.get(),
308 hostPDRHandler.get(), dbusToPLDMEventHandler.get(), fruHandler.get(),
309 platformConfigHandler.get(), &reqHandler, event, true,
310 addOnEventHandlers);
311
312 auto biosHandler = std::make_unique<bios::Handler>(
313 pldmTransport.getEventSource(), hostEID, &instanceIdDb, &reqHandler,
314 platformConfigHandler.get(), requestPLDMServiceName);
315
316 auto baseHandler = std::make_unique<base::Handler>(event);
317
318 #ifdef OEM_AMPERE
319 pldm::oem_ampere::OemAMPERE oemAMPERE(
320 &dbusHandler, pldmTransport.getEventSource(), pdrRepo.get(),
321 instanceIdDb, event, invoker, hostPDRHandler.get(),
322 platformHandler.get(), fruHandler.get(), baseHandler.get(),
323 biosHandler.get(), platformManager.get(), &reqHandler);
324 #endif
325
326 #ifdef OEM_IBM
327 pldm::oem_ibm::OemIBM oemIBM(
328 &dbusHandler, pldmTransport.getEventSource(), hostEID, pdrRepo.get(),
329 instanceIdDb, event, invoker, hostPDRHandler.get(),
330 platformHandler.get(), fruHandler.get(), baseHandler.get(),
331 &reqHandler);
332 #endif
333
334 invoker.registerHandler(PLDM_BIOS, std::move(biosHandler));
335 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler));
336 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
337 invoker.registerHandler(PLDM_BASE, std::move(baseHandler));
338
339 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
340 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
341 bus, "/xyz/openbmc_project/pldm");
342
343 #endif
344
345 std::unique_ptr<fw_update::Manager> fwManager =
346 std::make_unique<fw_update::Manager>(event, reqHandler, instanceIdDb);
347 std::unique_ptr<MctpDiscovery> mctpDiscoveryHandler =
348 std::make_unique<MctpDiscovery>(
349 bus, std::initializer_list<MctpDiscoveryHandlerIntf*>{
350 fwManager.get(), platformManager.get()});
351 auto callback = [verbose, &invoker, &reqHandler, &fwManager, &pldmTransport,
352 TID](IO& io, int fd, uint32_t revents) mutable {
353 if (!(revents & EPOLLIN))
354 {
355 return;
356 }
357 if (fd < 0)
358 {
359 return;
360 }
361
362 int returnCode = 0;
363 void* requestMsg;
364 size_t recvDataLength;
365 returnCode = pldmTransport.recvMsg(TID, requestMsg, recvDataLength);
366
367 if (returnCode == PLDM_REQUESTER_SUCCESS)
368 {
369 std::vector<uint8_t> requestMsgVec(
370 static_cast<uint8_t*>(requestMsg),
371 static_cast<uint8_t*>(requestMsg) + recvDataLength);
372 FlightRecorder::GetInstance().saveRecord(requestMsgVec, false);
373 if (verbose)
374 {
375 printBuffer(Rx, requestMsgVec);
376 }
377 // process message and send response
378 auto response = processRxMsg(requestMsgVec, invoker, reqHandler,
379 fwManager.get(), TID);
380 if (response.has_value())
381 {
382 FlightRecorder::GetInstance().saveRecord(*response, true);
383 if (verbose)
384 {
385 printBuffer(Tx, *response);
386 }
387
388 returnCode = pldmTransport.sendMsg(TID, (*response).data(),
389 (*response).size());
390 if (returnCode != PLDM_REQUESTER_SUCCESS)
391 {
392 warning(
393 "Failed to send pldmTransport message for TID '{TID}', response code '{RETURN_CODE}'",
394 "TID", TID, "RETURN_CODE", returnCode);
395 }
396 }
397 }
398 // TODO check that we get here if mctp-demux dies?
399 else if (returnCode == PLDM_REQUESTER_RECV_FAIL)
400 {
401 // MCTP daemon has closed the socket this daemon is connected to.
402 // This may or may not be an error scenario, in either case the
403 // recovery mechanism for this daemon is to restart, and hence exit
404 // the event loop, that will cause this daemon to exit with a
405 // failure code.
406 error(
407 "MCTP daemon closed the socket, IO exiting with response code '{RC}'",
408 "RC", returnCode);
409 io.get_event().exit(0);
410 }
411 else
412 {
413 warning(
414 "Failed to receive PLDM request for pldmTransport, response code '{RETURN_CODE}'",
415 "RETURN_CODE", returnCode);
416 }
417 /* Free requestMsg after using */
418 free(requestMsg);
419 };
420
421 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
422 #ifndef SYSTEM_SPECIFIC_BIOS_JSON
423 try
424 {
425 bus.request_name(PLDMService);
426 }
427 catch (const sdbusplus::exception_t& e)
428 {
429 error("Failed to request D-Bus name {NAME} with error {ERROR}.", "NAME",
430 PLDMService, "ERROR", e);
431 }
432 #endif
433 IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback));
434 #ifdef LIBPLDMRESPONDER
435 if (hostPDRHandler)
436 {
437 hostPDRHandler->setHostFirmwareCondition();
438 }
439 #endif
440 stdplus::signal::block(SIGUSR1);
441 sdeventplus::source::Signal sigUsr1(
442 event, SIGUSR1, std::bind_front(&interruptFlightRecorderCallBack));
443 int returnCode = event.loop();
444 if (returnCode)
445 {
446 exit(EXIT_FAILURE);
447 }
448
449 exit(EXIT_SUCCESS);
450 }
451