blob: d1ca6fa617f7b6c2f24fa6f046ac75d30899751b [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>(
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500175 sockfd, hostEID, event, pdrRepo.get(), entityTree.get(),
176 dbusImplReq);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500177 }
178
179 Invoker invoker{};
180 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
181 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530182 invoker.registerHandler(PLDM_PLATFORM,
183 std::make_unique<platform::Handler>(
184 PDR_JSONS_DIR, EVENTS_JSONS_DIR, pdrRepo.get(),
185 hostPDRHandler.get()));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500186 invoker.registerHandler(
187 PLDM_FRU, std::make_unique<fru::Handler>(FRU_JSONS_DIR, pdrRepo.get(),
188 entityTree.get()));
189
190#ifdef OEM_IBM
191 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
192#endif
193
George Liu83409572019-12-24 18:42:54 +0800194 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500195
196 struct sockaddr_un addr
197 {
198 };
199 addr.sun_family = AF_UNIX;
200 const char path[] = "\0mctp-mux";
201 memcpy(addr.sun_path, path, sizeof(path) - 1);
202 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
203 sizeof(path) + sizeof(addr.sun_family) - 1);
204 if (-1 == result)
205 {
206 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600207 std::cerr << "Failed to connect to the socket, RC= " << returnCode
208 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500209 exit(EXIT_FAILURE);
210 }
211
212 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
213 if (-1 == result)
214 {
215 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600216 std::cerr << "Failed to send message type as pldm to mctp, RC= "
217 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500218 exit(EXIT_FAILURE);
219 }
220
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600221 auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
222 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600223 if (!(revents & EPOLLIN))
224 {
225 return;
226 }
227
228 // Outgoing message.
229 struct iovec iov[2]{};
230
231 // This structure contains the parameter information for the response
232 // message.
233 struct msghdr msg
234 {
235 };
236
237 int returnCode = 0;
238 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500239 if (0 == peekedLength)
240 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600241 std::cerr << "Socket has been closed \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500242 }
243 else if (peekedLength <= -1)
244 {
245 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600246 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500247 }
248 else
249 {
250 std::vector<uint8_t> requestMsg(peekedLength);
251 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600252 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500253 if (recvDataLength == peekedLength)
254 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530255 if (verbose)
256 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600257 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530258 printBuffer(requestMsg);
259 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500260 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
261 {
262 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600263 std::cerr << "Encountered Non-PLDM type message"
264 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500265 }
266 else
267 {
268 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600269 auto response =
270 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500271 if (!response.empty())
272 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500273 if (verbose)
274 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600275 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500276 printBuffer(response);
277 }
278
Zahed Hossain09a96e02019-08-06 07:42:37 -0500279 iov[0].iov_base = &requestMsg[0];
280 iov[0].iov_len =
281 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
282 iov[1].iov_base = response.data();
283 iov[1].iov_len = response.size();
284
285 msg.msg_iov = iov;
286 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
287
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600288 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500289 if (-1 == result)
290 {
291 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600292 std::cerr << "sendto system call failed, RC= "
293 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500294 }
295 }
296 }
297 }
298 else
299 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600300 std::cerr
301 << "Failure to read peeked length packet. peekedLength= "
302 << peekedLength << " recvDataLength=" << recvDataLength
303 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500304 }
305 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600306 };
307
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600308 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
309 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600310 IO io(event, socketFd(), EPOLLIN, std::move(callback));
311 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500312
313 result = shutdown(sockfd, SHUT_RDWR);
314 if (-1 == result)
315 {
316 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600317 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500318 exit(EXIT_FAILURE);
319 }
320 exit(EXIT_FAILURE);
321}