blob: 192112e16a3299ceffe23705b6b24b6f408b104f [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"
Deepak Kodihalliac19bd62020-06-16 08:25:23 -05009#include "host-bmc/dbus_to_host_effecters.hpp"
10#include "host-bmc/host_pdr_handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -060011#include "invoker.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050012#include "libpldmresponder/base.hpp"
13#include "libpldmresponder/bios.hpp"
Deepak Kodihallie60c5822019-10-23 03:26:15 -050014#include "libpldmresponder/fru.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -050015#include "libpldmresponder/platform.hpp"
Chicago Duan174665f2020-07-08 17:41:05 +080016#include "xyz/openbmc_project/PLDM/Event/server.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050017
18#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053019#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050020#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
George Liu6492f522020-06-16 10:34:05 +080027#include <sdeventplus/event.hpp>
28#include <sdeventplus/source/io.hpp>
29
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050030#include <cstdio>
31#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050032#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050033#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053034#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050035#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060036#include <memory>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050037#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060038#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053039#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050040#include <vector>
41
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050042#ifdef OEM_IBM
43#include "libpldmresponder/file_io.hpp"
44#endif
45
46constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
47
Deepak Kodihallibc669f12019-11-28 08:52:07 -060048using namespace pldm::responder;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050049using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060050using namespace sdeventplus;
51using namespace sdeventplus::source;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050052
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060053static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060054 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050055{
56
57 Response response;
58 uint8_t eid = requestMsg[0];
59 uint8_t type = requestMsg[1];
60 pldm_header_info hdrFields{};
61 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
62 requestMsg.data() + sizeof(eid) + sizeof(type));
63 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
64 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060065 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050066 }
67 else if (PLDM_RESPONSE != hdrFields.msg_type)
68 {
69 auto request = reinterpret_cast<const pldm_msg*>(hdr);
70 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
71 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060072 try
73 {
74 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
75 request, requestLen);
76 }
77 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050078 {
79 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
80 response.resize(sizeof(pldm_msg_hdr));
81 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
82 pldm_header_info header{};
83 header.msg_type = PLDM_RESPONSE;
84 header.instance = hdrFields.instance;
85 header.pldm_type = hdrFields.pldm_type;
86 header.command = hdrFields.command;
87 auto result = pack_pldm_header(&header, responseHdr);
88 if (PLDM_SUCCESS != result)
89 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060090 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050091 }
92 response.insert(response.end(), completion_code);
93 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050094 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060095 else
96 {
97 requester.markFree(eid, hdr->instance_id);
98 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050099 return response;
100}
101
102void printBuffer(const std::vector<uint8_t>& buffer)
103{
104 std::ostringstream tempStream;
105 tempStream << "Buffer Data: ";
106 if (!buffer.empty())
107 {
108 for (int byte : buffer)
109 {
110 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
111 << " ";
112 }
113 }
Sampa Misraaa8ae722019-12-12 03:20:40 -0600114 std::cout << tempStream.str().c_str() << std::endl;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500115}
116
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530117void optionUsage(void)
118{
119 std::cerr << "Usage: pldmd [options]\n";
120 std::cerr << "Options:\n";
121 std::cerr
122 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
123 std::cerr << "Defaulted settings: --verbose=0 \n";
124}
125
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500126int main(int argc, char** argv)
127{
128
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530129 bool verbose = false;
130 static struct option long_options[] = {
131 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
132
133 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
134 switch (argflag)
135 {
136 case 'v':
137 switch (std::stoi(optarg))
138 {
139 case 0:
140 verbose = false;
141 break;
142 case 1:
143 verbose = true;
144 break;
145 default:
146 optionUsage();
147 break;
148 }
149 break;
150 default:
151 optionUsage();
152 break;
153 }
154
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500155 /* Create local socket. */
156 int returnCode = 0;
157 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
158 if (-1 == sockfd)
159 {
160 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600161 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500162 exit(EXIT_FAILURE);
163 }
164
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500165 auto event = Event::get_default();
166 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
167 pldm_pdr_init(), pldm_pdr_destroy);
168 std::unique_ptr<pldm_entity_association_tree,
169 decltype(&pldm_entity_association_tree_destroy)>
170 entityTree(pldm_entity_association_tree_init(),
171 pldm_entity_association_tree_destroy);
172 auto& bus = pldm::utils::DBusHandler::getBus();
173 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
174 std::unique_ptr<HostPDRHandler> hostPDRHandler;
Sampa Misrac0c60542020-07-01 02:34:25 -0500175 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
176 hostEffecterParser;
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500177 auto dbusHandler = std::make_unique<DBusHandler>();
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500178 auto hostEID = pldm::utils::readHostEID();
179 if (hostEID)
180 {
181 hostPDRHandler = std::make_unique<HostPDRHandler>(
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500182 sockfd, hostEID, event, pdrRepo.get(), entityTree.get(),
183 dbusImplReq);
Sampa Misrac0c60542020-07-01 02:34:25 -0500184 hostEffecterParser =
185 std::make_unique<pldm::host_effecters::HostEffecterParser>(
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500186 &dbusImplReq, sockfd, pdrRepo.get(), dbusHandler.get(),
Sampa Misrac0c60542020-07-01 02:34:25 -0500187 HOST_JSONS_DIR, verbose);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500188 }
189
190 Invoker invoker{};
191 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
192 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530193 auto fruHandler = std::make_unique<fru::Handler>(
194 FRU_JSONS_DIR, pdrRepo.get(), entityTree.get());
195 // FRU table is built lazily when a FRU command or Get PDR command is
196 // handled. To enable building FRU table, the FRU handler is passed to the
197 // Platform handler.
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530198 invoker.registerHandler(PLDM_PLATFORM,
199 std::make_unique<platform::Handler>(
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500200 dbusHandler.get(), PDR_JSONS_DIR,
201 EVENTS_JSONS_DIR, pdrRepo.get(),
202 hostPDRHandler.get(), fruHandler.get(), true));
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530203 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500204
205#ifdef OEM_IBM
206 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
207#endif
208
George Liu83409572019-12-24 18:42:54 +0800209 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500210
211 struct sockaddr_un addr
George Liu6492f522020-06-16 10:34:05 +0800212 {};
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500213 addr.sun_family = AF_UNIX;
214 const char path[] = "\0mctp-mux";
215 memcpy(addr.sun_path, path, sizeof(path) - 1);
216 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
217 sizeof(path) + sizeof(addr.sun_family) - 1);
218 if (-1 == result)
219 {
220 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600221 std::cerr << "Failed to connect to the socket, RC= " << returnCode
222 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500223 exit(EXIT_FAILURE);
224 }
225
226 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
227 if (-1 == result)
228 {
229 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600230 std::cerr << "Failed to send message type as pldm to mctp, RC= "
231 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500232 exit(EXIT_FAILURE);
233 }
234
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500235 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
Chicago Duan174665f2020-07-08 17:41:05 +0800236 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
237 bus, "/xyz/openbmc_project/pldm");
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600238 auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
239 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600240 if (!(revents & EPOLLIN))
241 {
242 return;
243 }
244
245 // Outgoing message.
246 struct iovec iov[2]{};
247
248 // This structure contains the parameter information for the response
249 // message.
250 struct msghdr msg
George Liu6492f522020-06-16 10:34:05 +0800251 {};
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600252
253 int returnCode = 0;
254 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500255 if (0 == peekedLength)
256 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600257 std::cerr << "Socket has been closed \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500258 }
259 else if (peekedLength <= -1)
260 {
261 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600262 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500263 }
264 else
265 {
266 std::vector<uint8_t> requestMsg(peekedLength);
267 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600268 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500269 if (recvDataLength == peekedLength)
270 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530271 if (verbose)
272 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600273 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530274 printBuffer(requestMsg);
275 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500276 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
277 {
278 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600279 std::cerr << "Encountered Non-PLDM type message"
280 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500281 }
282 else
283 {
284 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600285 auto response =
286 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500287 if (!response.empty())
288 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500289 if (verbose)
290 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600291 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500292 printBuffer(response);
293 }
294
Zahed Hossain09a96e02019-08-06 07:42:37 -0500295 iov[0].iov_base = &requestMsg[0];
296 iov[0].iov_len =
297 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
298 iov[1].iov_base = response.data();
299 iov[1].iov_len = response.size();
300
301 msg.msg_iov = iov;
302 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
303
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600304 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500305 if (-1 == result)
306 {
307 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600308 std::cerr << "sendto system call failed, RC= "
309 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500310 }
311 }
312 }
313 }
314 else
315 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600316 std::cerr
317 << "Failure to read peeked length packet. peekedLength= "
318 << peekedLength << " recvDataLength=" << recvDataLength
319 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500320 }
321 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600322 };
323
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600324 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
325 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600326 IO io(event, socketFd(), EPOLLIN, std::move(callback));
327 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500328
329 result = shutdown(sockfd, SHUT_RDWR);
330 if (-1 == result)
331 {
332 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600333 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500334 exit(EXIT_FAILURE);
335 }
336 exit(EXIT_FAILURE);
337}