blob: 030fcb1b7d9d701a9e7b14f664fc4860b6e99bb1 [file] [log] [blame]
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06001#include "dbus_impl_requester.hpp"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05002#include "host_pdr_handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06003#include "invoker.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05004#include "libpldmresponder/base.hpp"
5#include "libpldmresponder/bios.hpp"
Deepak Kodihallie60c5822019-10-23 03:26:15 -05006#include "libpldmresponder/fru.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -05007#include "libpldmresponder/platform.hpp"
George Liu83409572019-12-24 18:42:54 +08008#include "utils.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05009
10#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053011#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050012#include <poll.h>
13#include <stdlib.h>
14#include <sys/socket.h>
15#include <sys/types.h>
16#include <sys/un.h>
17#include <unistd.h>
18
19#include <cstdio>
20#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050021#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050022#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053023#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050024#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060025#include <memory>
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060026#include <sdeventplus/event.hpp>
27#include <sdeventplus/source/io.hpp>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050028#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060029#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053030#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050031#include <vector>
32
33#include "libpldm/base.h"
34#include "libpldm/bios.h"
Deepak Kodihallic682fe22020-03-04 00:42:54 -060035#include "libpldm/pdr.h"
Sampa Misraa2fa0702019-05-31 01:28:55 -050036#include "libpldm/platform.h"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050037
38#ifdef OEM_IBM
39#include "libpldmresponder/file_io.hpp"
40#endif
41
42constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
43
Deepak Kodihallibc669f12019-11-28 08:52:07 -060044using namespace pldm::responder;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050045using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060046using namespace sdeventplus;
47using namespace sdeventplus::source;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050048
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060049static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060050 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050051{
52
53 Response response;
54 uint8_t eid = requestMsg[0];
55 uint8_t type = requestMsg[1];
56 pldm_header_info hdrFields{};
57 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
58 requestMsg.data() + sizeof(eid) + sizeof(type));
59 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
60 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060061 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050062 }
63 else if (PLDM_RESPONSE != hdrFields.msg_type)
64 {
65 auto request = reinterpret_cast<const pldm_msg*>(hdr);
66 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
67 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060068 try
69 {
70 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
71 request, requestLen);
72 }
73 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050074 {
75 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
76 response.resize(sizeof(pldm_msg_hdr));
77 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
78 pldm_header_info header{};
79 header.msg_type = PLDM_RESPONSE;
80 header.instance = hdrFields.instance;
81 header.pldm_type = hdrFields.pldm_type;
82 header.command = hdrFields.command;
83 auto result = pack_pldm_header(&header, responseHdr);
84 if (PLDM_SUCCESS != result)
85 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060086 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050087 }
88 response.insert(response.end(), completion_code);
89 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050090 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060091 else
92 {
93 requester.markFree(eid, hdr->instance_id);
94 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050095 return response;
96}
97
98void printBuffer(const std::vector<uint8_t>& buffer)
99{
100 std::ostringstream tempStream;
101 tempStream << "Buffer Data: ";
102 if (!buffer.empty())
103 {
104 for (int byte : buffer)
105 {
106 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
107 << " ";
108 }
109 }
Sampa Misraaa8ae722019-12-12 03:20:40 -0600110 std::cout << tempStream.str().c_str() << std::endl;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500111}
112
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530113void optionUsage(void)
114{
115 std::cerr << "Usage: pldmd [options]\n";
116 std::cerr << "Options:\n";
117 std::cerr
118 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
119 std::cerr << "Defaulted settings: --verbose=0 \n";
120}
121
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500122int main(int argc, char** argv)
123{
124
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530125 bool verbose = false;
126 static struct option long_options[] = {
127 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
128
129 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
130 switch (argflag)
131 {
132 case 'v':
133 switch (std::stoi(optarg))
134 {
135 case 0:
136 verbose = false;
137 break;
138 case 1:
139 verbose = true;
140 break;
141 default:
142 optionUsage();
143 break;
144 }
145 break;
146 default:
147 optionUsage();
148 break;
149 }
150
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500151 /* Create local socket. */
152 int returnCode = 0;
153 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
154 if (-1 == sockfd)
155 {
156 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600157 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500158 exit(EXIT_FAILURE);
159 }
160
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500161 auto event = Event::get_default();
162 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
163 pldm_pdr_init(), pldm_pdr_destroy);
164 std::unique_ptr<pldm_entity_association_tree,
165 decltype(&pldm_entity_association_tree_destroy)>
166 entityTree(pldm_entity_association_tree_init(),
167 pldm_entity_association_tree_destroy);
168 auto& bus = pldm::utils::DBusHandler::getBus();
169 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
170 std::unique_ptr<HostPDRHandler> hostPDRHandler;
171 auto hostEID = pldm::utils::readHostEID();
172 if (hostEID)
173 {
174 hostPDRHandler = std::make_unique<HostPDRHandler>(
175 sockfd, hostEID, event, pdrRepo.get(), dbusImplReq);
176 }
177
178 Invoker invoker{};
179 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
180 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
181 invoker.registerHandler(
182 PLDM_PLATFORM, std::make_unique<platform::Handler>(
183 PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get()));
184 invoker.registerHandler(
185 PLDM_FRU, std::make_unique<fru::Handler>(FRU_JSONS_DIR, pdrRepo.get(),
186 entityTree.get()));
187
188#ifdef OEM_IBM
189 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
190#endif
191
George Liu83409572019-12-24 18:42:54 +0800192 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500193
194 struct sockaddr_un addr
195 {
196 };
197 addr.sun_family = AF_UNIX;
198 const char path[] = "\0mctp-mux";
199 memcpy(addr.sun_path, path, sizeof(path) - 1);
200 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
201 sizeof(path) + sizeof(addr.sun_family) - 1);
202 if (-1 == result)
203 {
204 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600205 std::cerr << "Failed to connect to the socket, RC= " << returnCode
206 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500207 exit(EXIT_FAILURE);
208 }
209
210 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
211 if (-1 == result)
212 {
213 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600214 std::cerr << "Failed to send message type as pldm to mctp, RC= "
215 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500216 exit(EXIT_FAILURE);
217 }
218
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600219 auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
220 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600221 if (!(revents & EPOLLIN))
222 {
223 return;
224 }
225
226 // Outgoing message.
227 struct iovec iov[2]{};
228
229 // This structure contains the parameter information for the response
230 // message.
231 struct msghdr msg
232 {
233 };
234
235 int returnCode = 0;
236 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500237 if (0 == peekedLength)
238 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600239 std::cerr << "Socket has been closed \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500240 }
241 else if (peekedLength <= -1)
242 {
243 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600244 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500245 }
246 else
247 {
248 std::vector<uint8_t> requestMsg(peekedLength);
249 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600250 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500251 if (recvDataLength == peekedLength)
252 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530253 if (verbose)
254 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600255 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530256 printBuffer(requestMsg);
257 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500258 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
259 {
260 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600261 std::cerr << "Encountered Non-PLDM type message"
262 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500263 }
264 else
265 {
266 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600267 auto response =
268 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500269 if (!response.empty())
270 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500271 if (verbose)
272 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600273 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500274 printBuffer(response);
275 }
276
Zahed Hossain09a96e02019-08-06 07:42:37 -0500277 iov[0].iov_base = &requestMsg[0];
278 iov[0].iov_len =
279 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
280 iov[1].iov_base = response.data();
281 iov[1].iov_len = response.size();
282
283 msg.msg_iov = iov;
284 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
285
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600286 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500287 if (-1 == result)
288 {
289 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600290 std::cerr << "sendto system call failed, RC= "
291 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500292 }
293 }
294 }
295 }
296 else
297 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600298 std::cerr
299 << "Failure to read peeked length packet. peekedLength= "
300 << peekedLength << " recvDataLength=" << recvDataLength
301 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500302 }
303 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600304 };
305
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600306 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
307 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600308 IO io(event, socketFd(), EPOLLIN, std::move(callback));
309 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500310
311 result = shutdown(sockfd, SHUT_RDWR);
312 if (-1 == result)
313 {
314 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600315 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500316 exit(EXIT_FAILURE);
317 }
318 exit(EXIT_FAILURE);
319}