blob: fa1c89c64f0a3f09f1cdd28abb2d3df17e20021c [file] [log] [blame]
George Liu6492f522020-06-16 10:34:05 +08001#include "libpldm/base.h"
2#include "libpldm/bios.h"
3#include "libpldm/pdr.h"
4#include "libpldm/platform.h"
5
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05006#include "common/utils.hpp"
Pavithra Barithaya0f74c982020-04-27 02:17:10 -05007#include "dbus_impl_pdr.hpp"
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06008#include "dbus_impl_requester.hpp"
Deepak Kodihalliac19bd62020-06-16 08:25:23 -05009#include "host-bmc/dbus_to_host_effecters.hpp"
10#include "host-bmc/host_pdr_handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -060011#include "invoker.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050012#include "libpldmresponder/base.hpp"
13#include "libpldmresponder/bios.hpp"
Deepak Kodihallie60c5822019-10-23 03:26:15 -050014#include "libpldmresponder/fru.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -050015#include "libpldmresponder/platform.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050016
17#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053018#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050019#include <poll.h>
20#include <stdlib.h>
21#include <sys/socket.h>
22#include <sys/types.h>
23#include <sys/un.h>
24#include <unistd.h>
25
George Liu6492f522020-06-16 10:34:05 +080026#include <sdeventplus/event.hpp>
27#include <sdeventplus/source/io.hpp>
28
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050029#include <cstdio>
30#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050031#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050032#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053033#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050034#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060035#include <memory>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050036#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060037#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053038#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050039#include <vector>
40
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050041#ifdef OEM_IBM
42#include "libpldmresponder/file_io.hpp"
43#endif
44
45constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
46
Deepak Kodihallibc669f12019-11-28 08:52:07 -060047using namespace pldm::responder;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050048using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060049using namespace sdeventplus;
50using namespace sdeventplus::source;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050051
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060052static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060053 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050054{
55
56 Response response;
57 uint8_t eid = requestMsg[0];
58 uint8_t type = requestMsg[1];
59 pldm_header_info hdrFields{};
60 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
61 requestMsg.data() + sizeof(eid) + sizeof(type));
62 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
63 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060064 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050065 }
66 else if (PLDM_RESPONSE != hdrFields.msg_type)
67 {
68 auto request = reinterpret_cast<const pldm_msg*>(hdr);
69 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
70 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060071 try
72 {
73 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
74 request, requestLen);
75 }
76 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050077 {
78 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
79 response.resize(sizeof(pldm_msg_hdr));
80 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
81 pldm_header_info header{};
82 header.msg_type = PLDM_RESPONSE;
83 header.instance = hdrFields.instance;
84 header.pldm_type = hdrFields.pldm_type;
85 header.command = hdrFields.command;
86 auto result = pack_pldm_header(&header, responseHdr);
87 if (PLDM_SUCCESS != result)
88 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060089 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050090 }
91 response.insert(response.end(), completion_code);
92 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050093 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060094 else
95 {
96 requester.markFree(eid, hdr->instance_id);
97 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050098 return response;
99}
100
101void printBuffer(const std::vector<uint8_t>& buffer)
102{
103 std::ostringstream tempStream;
104 tempStream << "Buffer Data: ";
105 if (!buffer.empty())
106 {
107 for (int byte : buffer)
108 {
109 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
110 << " ";
111 }
112 }
Sampa Misraaa8ae722019-12-12 03:20:40 -0600113 std::cout << tempStream.str().c_str() << std::endl;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500114}
115
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530116void optionUsage(void)
117{
118 std::cerr << "Usage: pldmd [options]\n";
119 std::cerr << "Options:\n";
120 std::cerr
121 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
122 std::cerr << "Defaulted settings: --verbose=0 \n";
123}
124
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500125int main(int argc, char** argv)
126{
127
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530128 bool verbose = false;
129 static struct option long_options[] = {
130 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
131
132 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
133 switch (argflag)
134 {
135 case 'v':
136 switch (std::stoi(optarg))
137 {
138 case 0:
139 verbose = false;
140 break;
141 case 1:
142 verbose = true;
143 break;
144 default:
145 optionUsage();
146 break;
147 }
148 break;
149 default:
150 optionUsage();
151 break;
152 }
153
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500154 /* Create local socket. */
155 int returnCode = 0;
156 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
157 if (-1 == sockfd)
158 {
159 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600160 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500161 exit(EXIT_FAILURE);
162 }
163
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500164 auto event = Event::get_default();
165 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
166 pldm_pdr_init(), pldm_pdr_destroy);
167 std::unique_ptr<pldm_entity_association_tree,
168 decltype(&pldm_entity_association_tree_destroy)>
169 entityTree(pldm_entity_association_tree_init(),
170 pldm_entity_association_tree_destroy);
171 auto& bus = pldm::utils::DBusHandler::getBus();
172 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
173 std::unique_ptr<HostPDRHandler> hostPDRHandler;
174 auto hostEID = pldm::utils::readHostEID();
175 if (hostEID)
176 {
177 hostPDRHandler = std::make_unique<HostPDRHandler>(
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500178 sockfd, hostEID, event, pdrRepo.get(), entityTree.get(),
179 dbusImplReq);
Tom Joseph250c4752020-04-15 10:32:45 +0530180 DBusHandler dbusHandler;
181 pldm::host_effecters::HostEffecterParser hostEffecterParser(
182 &dbusImplReq, sockfd, pdrRepo.get(), &dbusHandler, HOST_JSONS_DIR,
183 verbose);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500184 }
185
186 Invoker invoker{};
187 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
188 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530189 auto fruHandler = std::make_unique<fru::Handler>(
190 FRU_JSONS_DIR, pdrRepo.get(), entityTree.get());
191 // FRU table is built lazily when a FRU command or Get PDR command is
192 // handled. To enable building FRU table, the FRU handler is passed to the
193 // Platform handler.
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530194 invoker.registerHandler(PLDM_PLATFORM,
195 std::make_unique<platform::Handler>(
196 PDR_JSONS_DIR, EVENTS_JSONS_DIR, pdrRepo.get(),
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530197 hostPDRHandler.get(), fruHandler.get()));
198 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500199
200#ifdef OEM_IBM
201 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
202#endif
203
George Liu83409572019-12-24 18:42:54 +0800204 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500205
206 struct sockaddr_un addr
George Liu6492f522020-06-16 10:34:05 +0800207 {};
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500208 addr.sun_family = AF_UNIX;
209 const char path[] = "\0mctp-mux";
210 memcpy(addr.sun_path, path, sizeof(path) - 1);
211 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
212 sizeof(path) + sizeof(addr.sun_family) - 1);
213 if (-1 == result)
214 {
215 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600216 std::cerr << "Failed to connect to the socket, RC= " << returnCode
217 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500218 exit(EXIT_FAILURE);
219 }
220
221 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
222 if (-1 == result)
223 {
224 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600225 std::cerr << "Failed to send message type as pldm to mctp, RC= "
226 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500227 exit(EXIT_FAILURE);
228 }
229
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500230 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600231 auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
232 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600233 if (!(revents & EPOLLIN))
234 {
235 return;
236 }
237
238 // Outgoing message.
239 struct iovec iov[2]{};
240
241 // This structure contains the parameter information for the response
242 // message.
243 struct msghdr msg
George Liu6492f522020-06-16 10:34:05 +0800244 {};
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600245
246 int returnCode = 0;
247 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500248 if (0 == peekedLength)
249 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600250 std::cerr << "Socket has been closed \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500251 }
252 else if (peekedLength <= -1)
253 {
254 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600255 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500256 }
257 else
258 {
259 std::vector<uint8_t> requestMsg(peekedLength);
260 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600261 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500262 if (recvDataLength == peekedLength)
263 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530264 if (verbose)
265 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600266 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530267 printBuffer(requestMsg);
268 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500269 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
270 {
271 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600272 std::cerr << "Encountered Non-PLDM type message"
273 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500274 }
275 else
276 {
277 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600278 auto response =
279 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500280 if (!response.empty())
281 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500282 if (verbose)
283 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600284 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500285 printBuffer(response);
286 }
287
Zahed Hossain09a96e02019-08-06 07:42:37 -0500288 iov[0].iov_base = &requestMsg[0];
289 iov[0].iov_len =
290 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
291 iov[1].iov_base = response.data();
292 iov[1].iov_len = response.size();
293
294 msg.msg_iov = iov;
295 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
296
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600297 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500298 if (-1 == result)
299 {
300 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600301 std::cerr << "sendto system call failed, RC= "
302 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500303 }
304 }
305 }
306 }
307 else
308 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600309 std::cerr
310 << "Failure to read peeked length packet. peekedLength= "
311 << peekedLength << " recvDataLength=" << recvDataLength
312 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500313 }
314 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600315 };
316
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600317 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
318 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600319 IO io(event, socketFd(), EPOLLIN, std::move(callback));
320 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500321
322 result = shutdown(sockfd, SHUT_RDWR);
323 if (-1 == result)
324 {
325 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600326 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500327 exit(EXIT_FAILURE);
328 }
329 exit(EXIT_FAILURE);
330}