blob: 4bc89433d5c92bc9e36a6236e608dc63275f7e98 [file] [log] [blame]
Pavithra Barithaya0f74c982020-04-27 02:17:10 -05001#include "dbus_impl_pdr.hpp"
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06002#include "dbus_impl_requester.hpp"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05003#include "host_pdr_handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06004#include "invoker.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05005#include "libpldmresponder/base.hpp"
6#include "libpldmresponder/bios.hpp"
Deepak Kodihallie60c5822019-10-23 03:26:15 -05007#include "libpldmresponder/fru.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -05008#include "libpldmresponder/platform.hpp"
George Liu83409572019-12-24 18:42:54 +08009#include "utils.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050010
11#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053012#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050013#include <poll.h>
14#include <stdlib.h>
15#include <sys/socket.h>
16#include <sys/types.h>
17#include <sys/un.h>
18#include <unistd.h>
19
20#include <cstdio>
21#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050022#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050023#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053024#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050025#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060026#include <memory>
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060027#include <sdeventplus/event.hpp>
28#include <sdeventplus/source/io.hpp>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050029#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060030#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053031#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050032#include <vector>
33
34#include "libpldm/base.h"
35#include "libpldm/bios.h"
Deepak Kodihallic682fe22020-03-04 00:42:54 -060036#include "libpldm/pdr.h"
Sampa Misraa2fa0702019-05-31 01:28:55 -050037#include "libpldm/platform.h"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050038
39#ifdef OEM_IBM
40#include "libpldmresponder/file_io.hpp"
41#endif
42
43constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
44
Deepak Kodihallibc669f12019-11-28 08:52:07 -060045using namespace pldm::responder;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050046using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060047using namespace sdeventplus;
48using namespace sdeventplus::source;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050049
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060050static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060051 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050052{
53
54 Response response;
55 uint8_t eid = requestMsg[0];
56 uint8_t type = requestMsg[1];
57 pldm_header_info hdrFields{};
58 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
59 requestMsg.data() + sizeof(eid) + sizeof(type));
60 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
61 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060062 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050063 }
64 else if (PLDM_RESPONSE != hdrFields.msg_type)
65 {
66 auto request = reinterpret_cast<const pldm_msg*>(hdr);
67 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
68 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060069 try
70 {
71 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
72 request, requestLen);
73 }
74 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050075 {
76 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
77 response.resize(sizeof(pldm_msg_hdr));
78 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
79 pldm_header_info header{};
80 header.msg_type = PLDM_RESPONSE;
81 header.instance = hdrFields.instance;
82 header.pldm_type = hdrFields.pldm_type;
83 header.command = hdrFields.command;
84 auto result = pack_pldm_header(&header, responseHdr);
85 if (PLDM_SUCCESS != result)
86 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060087 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050088 }
89 response.insert(response.end(), completion_code);
90 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050091 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060092 else
93 {
94 requester.markFree(eid, hdr->instance_id);
95 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050096 return response;
97}
98
99void printBuffer(const std::vector<uint8_t>& buffer)
100{
101 std::ostringstream tempStream;
102 tempStream << "Buffer Data: ";
103 if (!buffer.empty())
104 {
105 for (int byte : buffer)
106 {
107 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
108 << " ";
109 }
110 }
Sampa Misraaa8ae722019-12-12 03:20:40 -0600111 std::cout << tempStream.str().c_str() << std::endl;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500112}
113
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530114void optionUsage(void)
115{
116 std::cerr << "Usage: pldmd [options]\n";
117 std::cerr << "Options:\n";
118 std::cerr
119 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
120 std::cerr << "Defaulted settings: --verbose=0 \n";
121}
122
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500123int main(int argc, char** argv)
124{
125
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530126 bool verbose = false;
127 static struct option long_options[] = {
128 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
129
130 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
131 switch (argflag)
132 {
133 case 'v':
134 switch (std::stoi(optarg))
135 {
136 case 0:
137 verbose = false;
138 break;
139 case 1:
140 verbose = true;
141 break;
142 default:
143 optionUsage();
144 break;
145 }
146 break;
147 default:
148 optionUsage();
149 break;
150 }
151
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500152 /* Create local socket. */
153 int returnCode = 0;
154 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
155 if (-1 == sockfd)
156 {
157 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600158 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500159 exit(EXIT_FAILURE);
160 }
161
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500162 auto event = Event::get_default();
163 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
164 pldm_pdr_init(), pldm_pdr_destroy);
165 std::unique_ptr<pldm_entity_association_tree,
166 decltype(&pldm_entity_association_tree_destroy)>
167 entityTree(pldm_entity_association_tree_init(),
168 pldm_entity_association_tree_destroy);
169 auto& bus = pldm::utils::DBusHandler::getBus();
170 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
171 std::unique_ptr<HostPDRHandler> hostPDRHandler;
172 auto hostEID = pldm::utils::readHostEID();
173 if (hostEID)
174 {
175 hostPDRHandler = std::make_unique<HostPDRHandler>(
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500176 sockfd, hostEID, event, pdrRepo.get(), entityTree.get(),
177 dbusImplReq);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500178 }
179
180 Invoker invoker{};
181 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
182 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530183 invoker.registerHandler(PLDM_PLATFORM,
184 std::make_unique<platform::Handler>(
185 PDR_JSONS_DIR, EVENTS_JSONS_DIR, pdrRepo.get(),
186 hostPDRHandler.get()));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500187 invoker.registerHandler(
188 PLDM_FRU, std::make_unique<fru::Handler>(FRU_JSONS_DIR, pdrRepo.get(),
189 entityTree.get()));
190
191#ifdef OEM_IBM
192 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
193#endif
194
George Liu83409572019-12-24 18:42:54 +0800195 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500196
197 struct sockaddr_un addr
198 {
199 };
200 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
236 {
237 };
238
239 int returnCode = 0;
240 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500241 if (0 == peekedLength)
242 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600243 std::cerr << "Socket has been closed \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500244 }
245 else if (peekedLength <= -1)
246 {
247 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600248 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500249 }
250 else
251 {
252 std::vector<uint8_t> requestMsg(peekedLength);
253 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600254 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500255 if (recvDataLength == peekedLength)
256 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530257 if (verbose)
258 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600259 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530260 printBuffer(requestMsg);
261 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500262 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
263 {
264 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600265 std::cerr << "Encountered Non-PLDM type message"
266 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500267 }
268 else
269 {
270 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600271 auto response =
272 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500273 if (!response.empty())
274 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500275 if (verbose)
276 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600277 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500278 printBuffer(response);
279 }
280
Zahed Hossain09a96e02019-08-06 07:42:37 -0500281 iov[0].iov_base = &requestMsg[0];
282 iov[0].iov_len =
283 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
284 iov[1].iov_base = response.data();
285 iov[1].iov_len = response.size();
286
287 msg.msg_iov = iov;
288 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
289
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600290 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500291 if (-1 == result)
292 {
293 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600294 std::cerr << "sendto system call failed, RC= "
295 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500296 }
297 }
298 }
299 }
300 else
301 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600302 std::cerr
303 << "Failure to read peeked length packet. peekedLength= "
304 << peekedLength << " recvDataLength=" << recvDataLength
305 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500306 }
307 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600308 };
309
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600310 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
311 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600312 IO io(event, socketFd(), EPOLLIN, std::move(callback));
313 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500314
315 result = shutdown(sockfd, SHUT_RDWR);
316 if (-1 == result)
317 {
318 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600319 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500320 exit(EXIT_FAILURE);
321 }
322 exit(EXIT_FAILURE);
323}