blob: 648ae6c1f176b53e407ebe88dff985042e6f588e [file] [log] [blame]
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06001#include "dbus_impl_requester.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06002#include "invoker.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05003#include "libpldmresponder/base.hpp"
4#include "libpldmresponder/bios.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -05005#include "libpldmresponder/platform.hpp"
George Liu83409572019-12-24 18:42:54 +08006#include "utils.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05007
8#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +05309#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050010#include <poll.h>
11#include <stdlib.h>
12#include <sys/socket.h>
13#include <sys/types.h>
14#include <sys/un.h>
15#include <unistd.h>
16
17#include <cstdio>
18#include <cstring>
19#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053020#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050021#include <iterator>
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060022#include <sdeventplus/event.hpp>
23#include <sdeventplus/source/io.hpp>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050024#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060025#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053026#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050027#include <vector>
28
29#include "libpldm/base.h"
30#include "libpldm/bios.h"
Sampa Misraa2fa0702019-05-31 01:28:55 -050031#include "libpldm/platform.h"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050032
33#ifdef OEM_IBM
34#include "libpldmresponder/file_io.hpp"
35#endif
36
37constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
38
Deepak Kodihallibc669f12019-11-28 08:52:07 -060039using namespace pldm::responder;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050040using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060041using namespace sdeventplus;
42using namespace sdeventplus::source;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050043
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060044static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060045 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050046{
47
48 Response response;
49 uint8_t eid = requestMsg[0];
50 uint8_t type = requestMsg[1];
51 pldm_header_info hdrFields{};
52 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
53 requestMsg.data() + sizeof(eid) + sizeof(type));
54 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
55 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060056 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050057 }
58 else if (PLDM_RESPONSE != hdrFields.msg_type)
59 {
60 auto request = reinterpret_cast<const pldm_msg*>(hdr);
61 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
62 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060063 try
64 {
65 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
66 request, requestLen);
67 }
68 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050069 {
70 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
71 response.resize(sizeof(pldm_msg_hdr));
72 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
73 pldm_header_info header{};
74 header.msg_type = PLDM_RESPONSE;
75 header.instance = hdrFields.instance;
76 header.pldm_type = hdrFields.pldm_type;
77 header.command = hdrFields.command;
78 auto result = pack_pldm_header(&header, responseHdr);
79 if (PLDM_SUCCESS != result)
80 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060081 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050082 }
83 response.insert(response.end(), completion_code);
84 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050085 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060086 else
87 {
88 requester.markFree(eid, hdr->instance_id);
89 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050090 return response;
91}
92
93void printBuffer(const std::vector<uint8_t>& buffer)
94{
95 std::ostringstream tempStream;
96 tempStream << "Buffer Data: ";
97 if (!buffer.empty())
98 {
99 for (int byte : buffer)
100 {
101 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
102 << " ";
103 }
104 }
Sampa Misraaa8ae722019-12-12 03:20:40 -0600105 std::cout << tempStream.str().c_str() << std::endl;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500106}
107
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530108void optionUsage(void)
109{
110 std::cerr << "Usage: pldmd [options]\n";
111 std::cerr << "Options:\n";
112 std::cerr
113 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
114 std::cerr << "Defaulted settings: --verbose=0 \n";
115}
116
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500117int main(int argc, char** argv)
118{
119
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530120 bool verbose = false;
121 static struct option long_options[] = {
122 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
123
124 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
125 switch (argflag)
126 {
127 case 'v':
128 switch (std::stoi(optarg))
129 {
130 case 0:
131 verbose = false;
132 break;
133 case 1:
134 verbose = true;
135 break;
136 default:
137 optionUsage();
138 break;
139 }
140 break;
141 default:
142 optionUsage();
143 break;
144 }
145
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600146 Invoker invoker{};
147 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
148 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
149 invoker.registerHandler(PLDM_PLATFORM,
150 std::make_unique<platform::Handler>());
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500151
152#ifdef OEM_IBM
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600153 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500154#endif
155
156 /* Create local socket. */
157 int returnCode = 0;
158 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
159 if (-1 == sockfd)
160 {
161 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600162 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500163 exit(EXIT_FAILURE);
164 }
165
George Liu83409572019-12-24 18:42:54 +0800166 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500167
168 struct sockaddr_un addr
169 {
170 };
171 addr.sun_family = AF_UNIX;
172 const char path[] = "\0mctp-mux";
173 memcpy(addr.sun_path, path, sizeof(path) - 1);
174 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
175 sizeof(path) + sizeof(addr.sun_family) - 1);
176 if (-1 == result)
177 {
178 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600179 std::cerr << "Failed to connect to the socket, RC= " << returnCode
180 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500181 exit(EXIT_FAILURE);
182 }
183
184 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
185 if (-1 == result)
186 {
187 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600188 std::cerr << "Failed to send message type as pldm to mctp, RC= "
189 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500190 exit(EXIT_FAILURE);
191 }
192
George Liu0e02c322020-01-01 09:41:51 +0800193 auto& bus = pldm::utils::DBusHandler::getBus();
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600194 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600195 auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
196 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600197 if (!(revents & EPOLLIN))
198 {
199 return;
200 }
201
202 // Outgoing message.
203 struct iovec iov[2]{};
204
205 // This structure contains the parameter information for the response
206 // message.
207 struct msghdr msg
208 {
209 };
210
211 int returnCode = 0;
212 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500213 if (0 == peekedLength)
214 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600215 std::cerr << "Socket has been closed \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500216 }
217 else if (peekedLength <= -1)
218 {
219 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600220 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500221 }
222 else
223 {
224 std::vector<uint8_t> requestMsg(peekedLength);
225 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600226 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500227 if (recvDataLength == peekedLength)
228 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530229 if (verbose)
230 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600231 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530232 printBuffer(requestMsg);
233 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500234 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
235 {
236 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600237 std::cerr << "Encountered Non-PLDM type message"
238 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500239 }
240 else
241 {
242 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600243 auto response =
244 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500245 if (!response.empty())
246 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500247 if (verbose)
248 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600249 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500250 printBuffer(response);
251 }
252
Zahed Hossain09a96e02019-08-06 07:42:37 -0500253 iov[0].iov_base = &requestMsg[0];
254 iov[0].iov_len =
255 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
256 iov[1].iov_base = response.data();
257 iov[1].iov_len = response.size();
258
259 msg.msg_iov = iov;
260 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
261
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600262 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500263 if (-1 == result)
264 {
265 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600266 std::cerr << "sendto system call failed, RC= "
267 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500268 }
269 }
270 }
271 }
272 else
273 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600274 std::cerr
275 << "Failure to read peeked length packet. peekedLength= "
276 << peekedLength << " recvDataLength=" << recvDataLength
277 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500278 }
279 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600280 };
281
282 auto event = Event::get_default();
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600283 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
284 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600285 IO io(event, socketFd(), EPOLLIN, std::move(callback));
286 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500287
288 result = shutdown(sockfd, SHUT_RDWR);
289 if (-1 == result)
290 {
291 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600292 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500293 exit(EXIT_FAILURE);
294 }
295 exit(EXIT_FAILURE);
296}