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