blob: 501de5a87f881890fd56d0951540e8be0bebeaf5 [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"
Deepak Kodihallie60c5822019-10-23 03:26:15 -05005#include "libpldmresponder/fru.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -05006#include "libpldmresponder/platform.hpp"
George Liu83409572019-12-24 18:42:54 +08007#include "utils.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05008
9#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053010#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050011#include <poll.h>
12#include <stdlib.h>
13#include <sys/socket.h>
14#include <sys/types.h>
15#include <sys/un.h>
16#include <unistd.h>
17
18#include <cstdio>
19#include <cstring>
20#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053021#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050022#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060023#include <memory>
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060024#include <sdeventplus/event.hpp>
25#include <sdeventplus/source/io.hpp>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050026#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060027#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053028#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050029#include <vector>
30
31#include "libpldm/base.h"
32#include "libpldm/bios.h"
Deepak Kodihallic682fe22020-03-04 00:42:54 -060033#include "libpldm/pdr.h"
Sampa Misraa2fa0702019-05-31 01:28:55 -050034#include "libpldm/platform.h"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050035
36#ifdef OEM_IBM
37#include "libpldmresponder/file_io.hpp"
38#endif
39
40constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
41
Deepak Kodihallibc669f12019-11-28 08:52:07 -060042using namespace pldm::responder;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050043using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060044using namespace sdeventplus;
45using namespace sdeventplus::source;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050046
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060047static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060048 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050049{
50
51 Response response;
52 uint8_t eid = requestMsg[0];
53 uint8_t type = requestMsg[1];
54 pldm_header_info hdrFields{};
55 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
56 requestMsg.data() + sizeof(eid) + sizeof(type));
57 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
58 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060059 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050060 }
61 else if (PLDM_RESPONSE != hdrFields.msg_type)
62 {
63 auto request = reinterpret_cast<const pldm_msg*>(hdr);
64 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
65 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060066 try
67 {
68 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
69 request, requestLen);
70 }
71 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050072 {
73 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
74 response.resize(sizeof(pldm_msg_hdr));
75 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
76 pldm_header_info header{};
77 header.msg_type = PLDM_RESPONSE;
78 header.instance = hdrFields.instance;
79 header.pldm_type = hdrFields.pldm_type;
80 header.command = hdrFields.command;
81 auto result = pack_pldm_header(&header, responseHdr);
82 if (PLDM_SUCCESS != result)
83 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060084 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050085 }
86 response.insert(response.end(), completion_code);
87 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050088 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060089 else
90 {
91 requester.markFree(eid, hdr->instance_id);
92 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050093 return response;
94}
95
96void printBuffer(const std::vector<uint8_t>& buffer)
97{
98 std::ostringstream tempStream;
99 tempStream << "Buffer Data: ";
100 if (!buffer.empty())
101 {
102 for (int byte : buffer)
103 {
104 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
105 << " ";
106 }
107 }
Sampa Misraaa8ae722019-12-12 03:20:40 -0600108 std::cout << tempStream.str().c_str() << std::endl;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500109}
110
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530111void optionUsage(void)
112{
113 std::cerr << "Usage: pldmd [options]\n";
114 std::cerr << "Options:\n";
115 std::cerr
116 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
117 std::cerr << "Defaulted settings: --verbose=0 \n";
118}
119
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500120int main(int argc, char** argv)
121{
122
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530123 bool verbose = false;
124 static struct option long_options[] = {
125 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
126
127 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
128 switch (argflag)
129 {
130 case 'v':
131 switch (std::stoi(optarg))
132 {
133 case 0:
134 verbose = false;
135 break;
136 case 1:
137 verbose = true;
138 break;
139 default:
140 optionUsage();
141 break;
142 }
143 break;
144 default:
145 optionUsage();
146 break;
147 }
148
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600149 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
150 pldm_pdr_init(), pldm_pdr_destroy);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500151 std::unique_ptr<pldm_entity_association_tree,
152 decltype(&pldm_entity_association_tree_destroy)>
153 entityTree(pldm_entity_association_tree_init(),
154 pldm_entity_association_tree_destroy);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600155
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600156 Invoker invoker{};
157 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
158 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600159 invoker.registerHandler(PLDM_PLATFORM, std::make_unique<platform::Handler>(
160 PDR_JSONS_DIR, pdrRepo.get()));
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500161 invoker.registerHandler(
162 PLDM_FRU, std::make_unique<fru::Handler>(FRU_JSONS_DIR, pdrRepo.get(),
163 entityTree.get()));
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500164
165#ifdef OEM_IBM
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600166 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500167#endif
168
169 /* Create local socket. */
170 int returnCode = 0;
171 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
172 if (-1 == sockfd)
173 {
174 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600175 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500176 exit(EXIT_FAILURE);
177 }
178
George Liu83409572019-12-24 18:42:54 +0800179 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500180
181 struct sockaddr_un addr
182 {
183 };
184 addr.sun_family = AF_UNIX;
185 const char path[] = "\0mctp-mux";
186 memcpy(addr.sun_path, path, sizeof(path) - 1);
187 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
188 sizeof(path) + sizeof(addr.sun_family) - 1);
189 if (-1 == result)
190 {
191 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600192 std::cerr << "Failed to connect to the socket, RC= " << returnCode
193 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500194 exit(EXIT_FAILURE);
195 }
196
197 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
198 if (-1 == result)
199 {
200 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600201 std::cerr << "Failed to send message type as pldm to mctp, RC= "
202 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500203 exit(EXIT_FAILURE);
204 }
205
George Liu0e02c322020-01-01 09:41:51 +0800206 auto& bus = pldm::utils::DBusHandler::getBus();
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600207 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600208 auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
209 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600210 if (!(revents & EPOLLIN))
211 {
212 return;
213 }
214
215 // Outgoing message.
216 struct iovec iov[2]{};
217
218 // This structure contains the parameter information for the response
219 // message.
220 struct msghdr msg
221 {
222 };
223
224 int returnCode = 0;
225 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500226 if (0 == peekedLength)
227 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600228 std::cerr << "Socket has been closed \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500229 }
230 else if (peekedLength <= -1)
231 {
232 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600233 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500234 }
235 else
236 {
237 std::vector<uint8_t> requestMsg(peekedLength);
238 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600239 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500240 if (recvDataLength == peekedLength)
241 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530242 if (verbose)
243 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600244 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530245 printBuffer(requestMsg);
246 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500247 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
248 {
249 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600250 std::cerr << "Encountered Non-PLDM type message"
251 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500252 }
253 else
254 {
255 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600256 auto response =
257 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500258 if (!response.empty())
259 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500260 if (verbose)
261 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600262 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500263 printBuffer(response);
264 }
265
Zahed Hossain09a96e02019-08-06 07:42:37 -0500266 iov[0].iov_base = &requestMsg[0];
267 iov[0].iov_len =
268 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
269 iov[1].iov_base = response.data();
270 iov[1].iov_len = response.size();
271
272 msg.msg_iov = iov;
273 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
274
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600275 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500276 if (-1 == result)
277 {
278 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600279 std::cerr << "sendto system call failed, RC= "
280 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500281 }
282 }
283 }
284 }
285 else
286 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600287 std::cerr
288 << "Failure to read peeked length packet. peekedLength= "
289 << peekedLength << " recvDataLength=" << recvDataLength
290 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500291 }
292 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600293 };
294
295 auto event = Event::get_default();
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600296 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
297 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600298 IO io(event, socketFd(), EPOLLIN, std::move(callback));
299 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500300
301 result = shutdown(sockfd, SHUT_RDWR);
302 if (-1 == result)
303 {
304 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600305 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500306 exit(EXIT_FAILURE);
307 }
308 exit(EXIT_FAILURE);
309}