blob: 136142900967289f9ba8781b8160313b295c1772 [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
Pavithra Barithaya0f74c982020-04-27 02:17:10 -05006#include "dbus_impl_pdr.hpp"
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06007#include "dbus_impl_requester.hpp"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05008#include "host_pdr_handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06009#include "invoker.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050010#include "libpldmresponder/base.hpp"
11#include "libpldmresponder/bios.hpp"
Deepak Kodihallie60c5822019-10-23 03:26:15 -050012#include "libpldmresponder/fru.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -050013#include "libpldmresponder/platform.hpp"
George Liu83409572019-12-24 18:42:54 +080014#include "utils.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050015
16#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053017#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050018#include <poll.h>
19#include <stdlib.h>
20#include <sys/socket.h>
21#include <sys/types.h>
22#include <sys/un.h>
23#include <unistd.h>
24
George Liu6492f522020-06-16 10:34:05 +080025#include <sdeventplus/event.hpp>
26#include <sdeventplus/source/io.hpp>
27
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050028#include <cstdio>
29#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050030#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050031#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053032#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050033#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060034#include <memory>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050035#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060036#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053037#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050038#include <vector>
39
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050040#ifdef OEM_IBM
41#include "libpldmresponder/file_io.hpp"
42#endif
43
44constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
45
Deepak Kodihallibc669f12019-11-28 08:52:07 -060046using namespace pldm::responder;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050047using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060048using namespace sdeventplus;
49using namespace sdeventplus::source;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050050
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060051static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060052 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050053{
54
55 Response response;
56 uint8_t eid = requestMsg[0];
57 uint8_t type = requestMsg[1];
58 pldm_header_info hdrFields{};
59 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
60 requestMsg.data() + sizeof(eid) + sizeof(type));
61 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
62 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060063 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050064 }
65 else if (PLDM_RESPONSE != hdrFields.msg_type)
66 {
67 auto request = reinterpret_cast<const pldm_msg*>(hdr);
68 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
69 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060070 try
71 {
72 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
73 request, requestLen);
74 }
75 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050076 {
77 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
78 response.resize(sizeof(pldm_msg_hdr));
79 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
80 pldm_header_info header{};
81 header.msg_type = PLDM_RESPONSE;
82 header.instance = hdrFields.instance;
83 header.pldm_type = hdrFields.pldm_type;
84 header.command = hdrFields.command;
85 auto result = pack_pldm_header(&header, responseHdr);
86 if (PLDM_SUCCESS != result)
87 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060088 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050089 }
90 response.insert(response.end(), completion_code);
91 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050092 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060093 else
94 {
95 requester.markFree(eid, hdr->instance_id);
96 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050097 return response;
98}
99
100void printBuffer(const std::vector<uint8_t>& buffer)
101{
102 std::ostringstream tempStream;
103 tempStream << "Buffer Data: ";
104 if (!buffer.empty())
105 {
106 for (int byte : buffer)
107 {
108 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
109 << " ";
110 }
111 }
Sampa Misraaa8ae722019-12-12 03:20:40 -0600112 std::cout << tempStream.str().c_str() << std::endl;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500113}
114
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530115void optionUsage(void)
116{
117 std::cerr << "Usage: pldmd [options]\n";
118 std::cerr << "Options:\n";
119 std::cerr
120 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
121 std::cerr << "Defaulted settings: --verbose=0 \n";
122}
123
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500124int main(int argc, char** argv)
125{
126
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530127 bool verbose = false;
128 static struct option long_options[] = {
129 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
130
131 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
132 switch (argflag)
133 {
134 case 'v':
135 switch (std::stoi(optarg))
136 {
137 case 0:
138 verbose = false;
139 break;
140 case 1:
141 verbose = true;
142 break;
143 default:
144 optionUsage();
145 break;
146 }
147 break;
148 default:
149 optionUsage();
150 break;
151 }
152
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500153 /* Create local socket. */
154 int returnCode = 0;
155 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
156 if (-1 == sockfd)
157 {
158 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600159 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500160 exit(EXIT_FAILURE);
161 }
162
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500163 auto event = Event::get_default();
164 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
165 pldm_pdr_init(), pldm_pdr_destroy);
166 std::unique_ptr<pldm_entity_association_tree,
167 decltype(&pldm_entity_association_tree_destroy)>
168 entityTree(pldm_entity_association_tree_init(),
169 pldm_entity_association_tree_destroy);
170 auto& bus = pldm::utils::DBusHandler::getBus();
171 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
172 std::unique_ptr<HostPDRHandler> hostPDRHandler;
173 auto hostEID = pldm::utils::readHostEID();
174 if (hostEID)
175 {
176 hostPDRHandler = std::make_unique<HostPDRHandler>(
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500177 sockfd, hostEID, event, pdrRepo.get(), entityTree.get(),
178 dbusImplReq);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500179 }
180
181 Invoker invoker{};
182 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
183 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530184 invoker.registerHandler(PLDM_PLATFORM,
185 std::make_unique<platform::Handler>(
186 PDR_JSONS_DIR, EVENTS_JSONS_DIR, pdrRepo.get(),
187 hostPDRHandler.get()));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500188 invoker.registerHandler(
189 PLDM_FRU, std::make_unique<fru::Handler>(FRU_JSONS_DIR, pdrRepo.get(),
190 entityTree.get()));
191
192#ifdef OEM_IBM
193 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
194#endif
195
George Liu83409572019-12-24 18:42:54 +0800196 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500197
198 struct sockaddr_un addr
George Liu6492f522020-06-16 10:34:05 +0800199 {};
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500200 addr.sun_family = AF_UNIX;
201 const char path[] = "\0mctp-mux";
202 memcpy(addr.sun_path, path, sizeof(path) - 1);
203 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
204 sizeof(path) + sizeof(addr.sun_family) - 1);
205 if (-1 == result)
206 {
207 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600208 std::cerr << "Failed to connect to the socket, RC= " << returnCode
209 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500210 exit(EXIT_FAILURE);
211 }
212
213 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
214 if (-1 == result)
215 {
216 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600217 std::cerr << "Failed to send message type as pldm to mctp, RC= "
218 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500219 exit(EXIT_FAILURE);
220 }
221
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500222 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600223 auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
224 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600225 if (!(revents & EPOLLIN))
226 {
227 return;
228 }
229
230 // Outgoing message.
231 struct iovec iov[2]{};
232
233 // This structure contains the parameter information for the response
234 // message.
235 struct msghdr msg
George Liu6492f522020-06-16 10:34:05 +0800236 {};
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600237
238 int returnCode = 0;
239 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500240 if (0 == peekedLength)
241 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600242 std::cerr << "Socket has been closed \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500243 }
244 else if (peekedLength <= -1)
245 {
246 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600247 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500248 }
249 else
250 {
251 std::vector<uint8_t> requestMsg(peekedLength);
252 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600253 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500254 if (recvDataLength == peekedLength)
255 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530256 if (verbose)
257 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600258 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530259 printBuffer(requestMsg);
260 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500261 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
262 {
263 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600264 std::cerr << "Encountered Non-PLDM type message"
265 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500266 }
267 else
268 {
269 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600270 auto response =
271 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500272 if (!response.empty())
273 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500274 if (verbose)
275 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600276 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500277 printBuffer(response);
278 }
279
Zahed Hossain09a96e02019-08-06 07:42:37 -0500280 iov[0].iov_base = &requestMsg[0];
281 iov[0].iov_len =
282 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
283 iov[1].iov_base = response.data();
284 iov[1].iov_len = response.size();
285
286 msg.msg_iov = iov;
287 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
288
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600289 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500290 if (-1 == result)
291 {
292 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600293 std::cerr << "sendto system call failed, RC= "
294 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500295 }
296 }
297 }
298 }
299 else
300 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600301 std::cerr
302 << "Failure to read peeked length packet. peekedLength= "
303 << peekedLength << " recvDataLength=" << recvDataLength
304 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500305 }
306 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600307 };
308
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600309 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
310 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600311 IO io(event, socketFd(), EPOLLIN, std::move(callback));
312 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500313
314 result = shutdown(sockfd, SHUT_RDWR);
315 if (-1 == result)
316 {
317 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600318 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500319 exit(EXIT_FAILURE);
320 }
321 exit(EXIT_FAILURE);
322}