Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 1 | #include "dbus_impl_requester.hpp" |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 2 | #include "invoker.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 3 | #include "libpldmresponder/base.hpp" |
| 4 | #include "libpldmresponder/bios.hpp" |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 5 | #include "libpldmresponder/fru.hpp" |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 6 | #include "libpldmresponder/platform.hpp" |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 7 | #include "utils.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 8 | |
| 9 | #include <err.h> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 10 | #include <getopt.h> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 11 | #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 Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 21 | #include <iostream> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 22 | #include <iterator> |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 23 | #include <memory> |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 24 | #include <sdeventplus/event.hpp> |
| 25 | #include <sdeventplus/source/io.hpp> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 26 | #include <sstream> |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 27 | #include <stdexcept> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 28 | #include <string> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
| 31 | #include "libpldm/base.h" |
| 32 | #include "libpldm/bios.h" |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 33 | #include "libpldm/pdr.h" |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 34 | #include "libpldm/platform.h" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 35 | |
| 36 | #ifdef OEM_IBM |
| 37 | #include "libpldmresponder/file_io.hpp" |
| 38 | #endif |
| 39 | |
| 40 | constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1; |
| 41 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 42 | using namespace pldm::responder; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 43 | using namespace pldm; |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 44 | using namespace sdeventplus; |
| 45 | using namespace sdeventplus::source; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 46 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 47 | static Response processRxMsg(const std::vector<uint8_t>& requestMsg, |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 48 | Invoker& invoker, dbus_api::Requester& requester) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 49 | { |
| 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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 59 | std::cerr << "Empty PLDM request header \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 60 | } |
| 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 Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 66 | 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 72 | { |
| 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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 84 | std::cerr << "Failed adding response header \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 85 | } |
| 86 | response.insert(response.end(), completion_code); |
| 87 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 88 | } |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 89 | else |
| 90 | { |
| 91 | requester.markFree(eid, hdr->instance_id); |
| 92 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 93 | return response; |
| 94 | } |
| 95 | |
| 96 | void 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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 108 | std::cout << tempStream.str().c_str() << std::endl; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 111 | void 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 120 | int main(int argc, char** argv) |
| 121 | { |
| 122 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 123 | 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 Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 149 | std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo( |
| 150 | pldm_pdr_init(), pldm_pdr_destroy); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 151 | 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 Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 155 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 156 | Invoker invoker{}; |
| 157 | invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>()); |
| 158 | invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>()); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 159 | invoker.registerHandler(PLDM_PLATFORM, std::make_unique<platform::Handler>( |
| 160 | PDR_JSONS_DIR, pdrRepo.get())); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 161 | invoker.registerHandler( |
| 162 | PLDM_FRU, std::make_unique<fru::Handler>(FRU_JSONS_DIR, pdrRepo.get(), |
| 163 | entityTree.get())); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 164 | |
| 165 | #ifdef OEM_IBM |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 166 | invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>()); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 167 | #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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 175 | std::cerr << "Failed to create the socket, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 176 | exit(EXIT_FAILURE); |
| 177 | } |
| 178 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 179 | pldm::utils::CustomFD socketFd(sockfd); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 180 | |
| 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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 192 | std::cerr << "Failed to connect to the socket, RC= " << returnCode |
| 193 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 194 | 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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 201 | std::cerr << "Failed to send message type as pldm to mctp, RC= " |
| 202 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 203 | exit(EXIT_FAILURE); |
| 204 | } |
| 205 | |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 206 | auto& bus = pldm::utils::DBusHandler::getBus(); |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 207 | dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm"); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 208 | auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd, |
| 209 | uint32_t revents) { |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 210 | 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 226 | if (0 == peekedLength) |
| 227 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 228 | std::cerr << "Socket has been closed \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 229 | } |
| 230 | else if (peekedLength <= -1) |
| 231 | { |
| 232 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 233 | std::cerr << "recv system call failed, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 234 | } |
| 235 | else |
| 236 | { |
| 237 | std::vector<uint8_t> requestMsg(peekedLength); |
| 238 | auto recvDataLength = recv( |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 239 | fd, static_cast<void*>(requestMsg.data()), peekedLength, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 240 | if (recvDataLength == peekedLength) |
| 241 | { |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 242 | if (verbose) |
| 243 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 244 | std::cout << "Received Msg" << std::endl; |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 245 | printBuffer(requestMsg); |
| 246 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 247 | if (MCTP_MSG_TYPE_PLDM != requestMsg[1]) |
| 248 | { |
| 249 | // Skip this message and continue. |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 250 | std::cerr << "Encountered Non-PLDM type message" |
| 251 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 252 | } |
| 253 | else |
| 254 | { |
| 255 | // process message and send response |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 256 | auto response = |
| 257 | processRxMsg(requestMsg, invoker, dbusImplReq); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 258 | if (!response.empty()) |
| 259 | { |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 260 | if (verbose) |
| 261 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 262 | std::cout << "Sending Msg" << std::endl; |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 263 | printBuffer(response); |
| 264 | } |
| 265 | |
Zahed Hossain | 09a96e0 | 2019-08-06 07:42:37 -0500 | [diff] [blame] | 266 | 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 Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 275 | int result = sendmsg(fd, &msg, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 276 | if (-1 == result) |
| 277 | { |
| 278 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 279 | std::cerr << "sendto system call failed, RC= " |
| 280 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | else |
| 286 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 287 | std::cerr |
| 288 | << "Failure to read peeked length packet. peekedLength= " |
| 289 | << peekedLength << " recvDataLength=" << recvDataLength |
| 290 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 291 | } |
| 292 | } |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | auto event = Event::get_default(); |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 296 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 297 | bus.request_name("xyz.openbmc_project.PLDM"); |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 298 | IO io(event, socketFd(), EPOLLIN, std::move(callback)); |
| 299 | event.loop(); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 300 | |
| 301 | result = shutdown(sockfd, SHUT_RDWR); |
| 302 | if (-1 == result) |
| 303 | { |
| 304 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 305 | std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 306 | exit(EXIT_FAILURE); |
| 307 | } |
| 308 | exit(EXIT_FAILURE); |
| 309 | } |