blob: c1c864ccabc1e60313ef9f20127028228f5d7f7d [file] [log] [blame]
George Liu6492f522020-06-16 10:34:05 +08001#include "libpldm/base.h"
2#include "libpldm/bios.h"
3#include "libpldm/pdr.h"
4#include "libpldm/platform.h"
5
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05006#include "common/utils.hpp"
Pavithra Barithaya0f74c982020-04-27 02:17:10 -05007#include "dbus_impl_pdr.hpp"
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06008#include "dbus_impl_requester.hpp"
George Liucae18662020-05-15 09:32:57 +08009#include "host-bmc/dbus_to_event_handler.hpp"
Deepak Kodihalliac19bd62020-06-16 08:25:23 -050010#include "host-bmc/dbus_to_host_effecters.hpp"
11#include "host-bmc/host_pdr_handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -060012#include "invoker.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050013#include "libpldmresponder/base.hpp"
14#include "libpldmresponder/bios.hpp"
Deepak Kodihallie60c5822019-10-23 03:26:15 -050015#include "libpldmresponder/fru.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050016#include "libpldmresponder/oem_handler.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -050017#include "libpldmresponder/platform.hpp"
Chicago Duan174665f2020-07-08 17:41:05 +080018#include "xyz/openbmc_project/PLDM/Event/server.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050019
20#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053021#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050022#include <poll.h>
23#include <stdlib.h>
24#include <sys/socket.h>
25#include <sys/types.h>
26#include <sys/un.h>
27#include <unistd.h>
28
George Liu6492f522020-06-16 10:34:05 +080029#include <sdeventplus/event.hpp>
30#include <sdeventplus/source/io.hpp>
31
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050032#include <cstdio>
33#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050034#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050035#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053036#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050037#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060038#include <memory>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050039#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060040#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053041#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050042#include <vector>
43
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050044#ifdef OEM_IBM
45#include "libpldmresponder/file_io.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050046#include "libpldmresponder/oem_ibm_handler.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050047#endif
48
49constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
50
Deepak Kodihallibc669f12019-11-28 08:52:07 -060051using namespace pldm::responder;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050052using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060053using namespace sdeventplus;
54using namespace sdeventplus::source;
George Liucae18662020-05-15 09:32:57 +080055using namespace pldm::state_sensor;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050056
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060057static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060058 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050059{
60
61 Response response;
62 uint8_t eid = requestMsg[0];
63 uint8_t type = requestMsg[1];
64 pldm_header_info hdrFields{};
65 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
66 requestMsg.data() + sizeof(eid) + sizeof(type));
67 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
68 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060069 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050070 }
71 else if (PLDM_RESPONSE != hdrFields.msg_type)
72 {
73 auto request = reinterpret_cast<const pldm_msg*>(hdr);
74 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
75 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060076 try
77 {
78 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
79 request, requestLen);
80 }
81 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050082 {
83 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
84 response.resize(sizeof(pldm_msg_hdr));
85 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
86 pldm_header_info header{};
87 header.msg_type = PLDM_RESPONSE;
88 header.instance = hdrFields.instance;
89 header.pldm_type = hdrFields.pldm_type;
90 header.command = hdrFields.command;
91 auto result = pack_pldm_header(&header, responseHdr);
92 if (PLDM_SUCCESS != result)
93 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060094 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050095 }
96 response.insert(response.end(), completion_code);
97 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050098 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060099 else
100 {
101 requester.markFree(eid, hdr->instance_id);
102 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500103 return response;
104}
105
106void printBuffer(const std::vector<uint8_t>& buffer)
107{
108 std::ostringstream tempStream;
109 tempStream << "Buffer Data: ";
110 if (!buffer.empty())
111 {
112 for (int byte : buffer)
113 {
114 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
115 << " ";
116 }
117 }
Sampa Misraaa8ae722019-12-12 03:20:40 -0600118 std::cout << tempStream.str().c_str() << std::endl;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500119}
120
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530121void optionUsage(void)
122{
123 std::cerr << "Usage: pldmd [options]\n";
124 std::cerr << "Options:\n";
125 std::cerr
126 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
127 std::cerr << "Defaulted settings: --verbose=0 \n";
128}
129
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500130int main(int argc, char** argv)
131{
132
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530133 bool verbose = false;
134 static struct option long_options[] = {
135 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
136
137 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
138 switch (argflag)
139 {
140 case 'v':
141 switch (std::stoi(optarg))
142 {
143 case 0:
144 verbose = false;
145 break;
146 case 1:
147 verbose = true;
148 break;
149 default:
150 optionUsage();
151 break;
152 }
153 break;
154 default:
155 optionUsage();
156 break;
157 }
158
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500159 /* Create local socket. */
160 int returnCode = 0;
161 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
162 if (-1 == sockfd)
163 {
164 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600165 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500166 exit(EXIT_FAILURE);
167 }
168
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500169 auto event = Event::get_default();
170 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
171 pldm_pdr_init(), pldm_pdr_destroy);
172 std::unique_ptr<pldm_entity_association_tree,
173 decltype(&pldm_entity_association_tree_destroy)>
174 entityTree(pldm_entity_association_tree_init(),
175 pldm_entity_association_tree_destroy);
176 auto& bus = pldm::utils::DBusHandler::getBus();
177 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
178 std::unique_ptr<HostPDRHandler> hostPDRHandler;
Sampa Misrac0c60542020-07-01 02:34:25 -0500179 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
180 hostEffecterParser;
George Liucae18662020-05-15 09:32:57 +0800181 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler;
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500182 auto dbusHandler = std::make_unique<DBusHandler>();
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500183 auto hostEID = pldm::utils::readHostEID();
184 if (hostEID)
185 {
186 hostPDRHandler = std::make_unique<HostPDRHandler>(
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600187 sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR,
188 entityTree.get(), dbusImplReq);
Sampa Misrac0c60542020-07-01 02:34:25 -0500189 hostEffecterParser =
190 std::make_unique<pldm::host_effecters::HostEffecterParser>(
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500191 &dbusImplReq, sockfd, pdrRepo.get(), dbusHandler.get(),
Sampa Misrac0c60542020-07-01 02:34:25 -0500192 HOST_JSONS_DIR, verbose);
George Liucae18662020-05-15 09:32:57 +0800193 dbusToPLDMEventHandler =
194 std::make_unique<DbusToPLDMEvent>(sockfd, hostEID, dbusImplReq);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500195 }
196
197 Invoker invoker{};
Sampa Misraaea5dde2020-08-31 08:33:47 -0500198 std::unique_ptr<oem_platform::Handler> oemPlatformHandler{};
199
200#ifdef OEM_IBM
201 std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate =
202 std::make_unique<pldm::responder::CodeUpdate>(dbusHandler.get());
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500203 codeUpdate->clearDirPath(LID_STAGING_DIR);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500204 oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>(
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500205 dbusHandler.get(), codeUpdate.get(), sockfd, hostEID, dbusImplReq,
206 event);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500207 codeUpdate->setOemPlatformHandler(oemPlatformHandler.get());
208 invoker.registerHandler(
209 PLDM_OEM, std::make_unique<oem_ibm::Handler>(oemPlatformHandler.get()));
210#endif
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500211 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
Tom Joseph7f839f92020-09-21 10:20:44 +0530212 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>(
213 sockfd, hostEID, &dbusImplReq));
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530214 auto fruHandler = std::make_unique<fru::Handler>(
215 FRU_JSONS_DIR, pdrRepo.get(), entityTree.get());
216 // FRU table is built lazily when a FRU command or Get PDR command is
217 // handled. To enable building FRU table, the FRU handler is passed to the
218 // Platform handler.
Sampa Misraaea5dde2020-08-31 08:33:47 -0500219 auto platformHandler = std::make_unique<platform::Handler>(
220 dbusHandler.get(), PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get(),
221 dbusToPLDMEventHandler.get(), fruHandler.get(),
222 oemPlatformHandler.get(), true);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500223#ifdef OEM_IBM
Sampa Misraaea5dde2020-08-31 08:33:47 -0500224 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler =
225 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>(
226 oemPlatformHandler.get());
227 oemIbmPlatformHandler->setPlatformHandler(platformHandler.get());
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500228#endif
229
Sampa Misraaea5dde2020-08-31 08:33:47 -0500230 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler));
231 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
232
George Liu83409572019-12-24 18:42:54 +0800233 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500234
235 struct sockaddr_un addr
George Liu6492f522020-06-16 10:34:05 +0800236 {};
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500237 addr.sun_family = AF_UNIX;
238 const char path[] = "\0mctp-mux";
239 memcpy(addr.sun_path, path, sizeof(path) - 1);
240 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
241 sizeof(path) + sizeof(addr.sun_family) - 1);
242 if (-1 == result)
243 {
244 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600245 std::cerr << "Failed to connect to the socket, RC= " << returnCode
246 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500247 exit(EXIT_FAILURE);
248 }
249
250 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
251 if (-1 == result)
252 {
253 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600254 std::cerr << "Failed to send message type as pldm to mctp, RC= "
255 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500256 exit(EXIT_FAILURE);
257 }
258
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500259 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
Chicago Duan174665f2020-07-08 17:41:05 +0800260 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
261 bus, "/xyz/openbmc_project/pldm");
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500262 auto callback = [verbose, &invoker, &dbusImplReq](IO& io, int fd,
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600263 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600264 if (!(revents & EPOLLIN))
265 {
266 return;
267 }
268
269 // Outgoing message.
270 struct iovec iov[2]{};
271
272 // This structure contains the parameter information for the response
273 // message.
274 struct msghdr msg
George Liu6492f522020-06-16 10:34:05 +0800275 {};
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600276
277 int returnCode = 0;
278 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500279 if (0 == peekedLength)
280 {
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500281 // MCTP daemon has closed the socket this daemon is connected to.
282 // This may or may not be an error scenario, in either case the
283 // recovery mechanism for this daemon is to restart, and hence exit
284 // the event loop, that will cause this daemon to exit with a
285 // failure code.
286 io.get_event().exit(0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500287 }
288 else if (peekedLength <= -1)
289 {
290 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600291 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500292 }
293 else
294 {
295 std::vector<uint8_t> requestMsg(peekedLength);
296 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600297 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500298 if (recvDataLength == peekedLength)
299 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530300 if (verbose)
301 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600302 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530303 printBuffer(requestMsg);
304 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500305 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
306 {
307 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600308 std::cerr << "Encountered Non-PLDM type message"
309 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500310 }
311 else
312 {
313 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600314 auto response =
315 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500316 if (!response.empty())
317 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500318 if (verbose)
319 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600320 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500321 printBuffer(response);
322 }
323
Zahed Hossain09a96e02019-08-06 07:42:37 -0500324 iov[0].iov_base = &requestMsg[0];
325 iov[0].iov_len =
326 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
327 iov[1].iov_base = response.data();
328 iov[1].iov_len = response.size();
329
330 msg.msg_iov = iov;
331 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
332
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600333 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500334 if (-1 == result)
335 {
336 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600337 std::cerr << "sendto system call failed, RC= "
338 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500339 }
340 }
341 }
342 }
343 else
344 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600345 std::cerr
346 << "Failure to read peeked length packet. peekedLength= "
347 << peekedLength << " recvDataLength=" << recvDataLength
348 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500349 }
350 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600351 };
352
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600353 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
354 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600355 IO io(event, socketFd(), EPOLLIN, std::move(callback));
356 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500357
358 result = shutdown(sockfd, SHUT_RDWR);
359 if (-1 == result)
360 {
361 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600362 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500363 exit(EXIT_FAILURE);
364 }
365 exit(EXIT_FAILURE);
366}