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" |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 5 | #include "libpldmresponder/platform.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 6 | #include "libpldmresponder/utils.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 7 | |
| 8 | #include <err.h> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 9 | #include <getopt.h> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 10 | #include <poll.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <sys/socket.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/un.h> |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #include <cstdio> |
| 18 | #include <cstring> |
| 19 | #include <iomanip> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 20 | #include <iostream> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 21 | #include <iterator> |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 22 | #include <sdeventplus/event.hpp> |
| 23 | #include <sdeventplus/source/io.hpp> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 24 | #include <sstream> |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 25 | #include <stdexcept> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 26 | #include <string> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
| 29 | #include "libpldm/base.h" |
| 30 | #include "libpldm/bios.h" |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 31 | #include "libpldm/platform.h" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 32 | |
| 33 | #ifdef OEM_IBM |
| 34 | #include "libpldmresponder/file_io.hpp" |
| 35 | #endif |
| 36 | |
| 37 | constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1; |
| 38 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 39 | using namespace pldm::responder; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 40 | using namespace pldm; |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 41 | using namespace sdeventplus; |
| 42 | using namespace sdeventplus::source; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 43 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 44 | static Response processRxMsg(const std::vector<uint8_t>& requestMsg, |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 45 | Invoker& invoker, dbus_api::Requester& requester) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 46 | { |
| 47 | |
| 48 | Response response; |
| 49 | uint8_t eid = requestMsg[0]; |
| 50 | uint8_t type = requestMsg[1]; |
| 51 | pldm_header_info hdrFields{}; |
| 52 | auto hdr = reinterpret_cast<const pldm_msg_hdr*>( |
| 53 | requestMsg.data() + sizeof(eid) + sizeof(type)); |
| 54 | if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields)) |
| 55 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 56 | std::cerr << "Empty PLDM request header \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 57 | } |
| 58 | else if (PLDM_RESPONSE != hdrFields.msg_type) |
| 59 | { |
| 60 | auto request = reinterpret_cast<const pldm_msg*>(hdr); |
| 61 | size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) - |
| 62 | sizeof(eid) - sizeof(type); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 63 | try |
| 64 | { |
| 65 | response = invoker.handle(hdrFields.pldm_type, hdrFields.command, |
| 66 | request, requestLen); |
| 67 | } |
| 68 | catch (const std::out_of_range& e) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 69 | { |
| 70 | uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD; |
| 71 | response.resize(sizeof(pldm_msg_hdr)); |
| 72 | auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data()); |
| 73 | pldm_header_info header{}; |
| 74 | header.msg_type = PLDM_RESPONSE; |
| 75 | header.instance = hdrFields.instance; |
| 76 | header.pldm_type = hdrFields.pldm_type; |
| 77 | header.command = hdrFields.command; |
| 78 | auto result = pack_pldm_header(&header, responseHdr); |
| 79 | if (PLDM_SUCCESS != result) |
| 80 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 81 | std::cerr << "Failed adding response header \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 82 | } |
| 83 | response.insert(response.end(), completion_code); |
| 84 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 85 | } |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 86 | else |
| 87 | { |
| 88 | requester.markFree(eid, hdr->instance_id); |
| 89 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 90 | return response; |
| 91 | } |
| 92 | |
| 93 | void printBuffer(const std::vector<uint8_t>& buffer) |
| 94 | { |
| 95 | std::ostringstream tempStream; |
| 96 | tempStream << "Buffer Data: "; |
| 97 | if (!buffer.empty()) |
| 98 | { |
| 99 | for (int byte : buffer) |
| 100 | { |
| 101 | tempStream << std::setfill('0') << std::setw(2) << std::hex << byte |
| 102 | << " "; |
| 103 | } |
| 104 | } |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 105 | std::cout << tempStream.str().c_str() << std::endl; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 108 | void optionUsage(void) |
| 109 | { |
| 110 | std::cerr << "Usage: pldmd [options]\n"; |
| 111 | std::cerr << "Options:\n"; |
| 112 | std::cerr |
| 113 | << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n"; |
| 114 | std::cerr << "Defaulted settings: --verbose=0 \n"; |
| 115 | } |
| 116 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 117 | int main(int argc, char** argv) |
| 118 | { |
| 119 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 120 | bool verbose = false; |
| 121 | static struct option long_options[] = { |
| 122 | {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}}; |
| 123 | |
| 124 | auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr); |
| 125 | switch (argflag) |
| 126 | { |
| 127 | case 'v': |
| 128 | switch (std::stoi(optarg)) |
| 129 | { |
| 130 | case 0: |
| 131 | verbose = false; |
| 132 | break; |
| 133 | case 1: |
| 134 | verbose = true; |
| 135 | break; |
| 136 | default: |
| 137 | optionUsage(); |
| 138 | break; |
| 139 | } |
| 140 | break; |
| 141 | default: |
| 142 | optionUsage(); |
| 143 | break; |
| 144 | } |
| 145 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 146 | Invoker invoker{}; |
| 147 | invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>()); |
| 148 | invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>()); |
| 149 | invoker.registerHandler(PLDM_PLATFORM, |
| 150 | std::make_unique<platform::Handler>()); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 151 | |
| 152 | #ifdef OEM_IBM |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 153 | invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>()); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 154 | #endif |
| 155 | |
| 156 | /* Create local socket. */ |
| 157 | int returnCode = 0; |
| 158 | int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 159 | if (-1 == sockfd) |
| 160 | { |
| 161 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 162 | std::cerr << "Failed to create the socket, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 163 | exit(EXIT_FAILURE); |
| 164 | } |
| 165 | |
| 166 | responder::utils::CustomFD socketFd(sockfd); |
| 167 | |
| 168 | struct sockaddr_un addr |
| 169 | { |
| 170 | }; |
| 171 | addr.sun_family = AF_UNIX; |
| 172 | const char path[] = "\0mctp-mux"; |
| 173 | memcpy(addr.sun_path, path, sizeof(path) - 1); |
| 174 | int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr), |
| 175 | sizeof(path) + sizeof(addr.sun_family) - 1); |
| 176 | if (-1 == result) |
| 177 | { |
| 178 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 179 | std::cerr << "Failed to connect to the socket, RC= " << returnCode |
| 180 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 181 | exit(EXIT_FAILURE); |
| 182 | } |
| 183 | |
| 184 | result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM)); |
| 185 | if (-1 == result) |
| 186 | { |
| 187 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 188 | std::cerr << "Failed to send message type as pldm to mctp, RC= " |
| 189 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 190 | exit(EXIT_FAILURE); |
| 191 | } |
| 192 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 193 | auto bus = sdbusplus::bus::new_default(); |
| 194 | dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm"); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 195 | auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd, |
| 196 | uint32_t revents) { |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 197 | if (!(revents & EPOLLIN)) |
| 198 | { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | // Outgoing message. |
| 203 | struct iovec iov[2]{}; |
| 204 | |
| 205 | // This structure contains the parameter information for the response |
| 206 | // message. |
| 207 | struct msghdr msg |
| 208 | { |
| 209 | }; |
| 210 | |
| 211 | int returnCode = 0; |
| 212 | ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 213 | if (0 == peekedLength) |
| 214 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 215 | std::cerr << "Socket has been closed \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 216 | } |
| 217 | else if (peekedLength <= -1) |
| 218 | { |
| 219 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 220 | std::cerr << "recv system call failed, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 221 | } |
| 222 | else |
| 223 | { |
| 224 | std::vector<uint8_t> requestMsg(peekedLength); |
| 225 | auto recvDataLength = recv( |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 226 | fd, static_cast<void*>(requestMsg.data()), peekedLength, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 227 | if (recvDataLength == peekedLength) |
| 228 | { |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 229 | if (verbose) |
| 230 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 231 | std::cout << "Received Msg" << std::endl; |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 232 | printBuffer(requestMsg); |
| 233 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 234 | if (MCTP_MSG_TYPE_PLDM != requestMsg[1]) |
| 235 | { |
| 236 | // Skip this message and continue. |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 237 | std::cerr << "Encountered Non-PLDM type message" |
| 238 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 239 | } |
| 240 | else |
| 241 | { |
| 242 | // process message and send response |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 243 | auto response = |
| 244 | processRxMsg(requestMsg, invoker, dbusImplReq); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 245 | if (!response.empty()) |
| 246 | { |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 247 | if (verbose) |
| 248 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 249 | std::cout << "Sending Msg" << std::endl; |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 250 | printBuffer(response); |
| 251 | } |
| 252 | |
Zahed Hossain | 09a96e0 | 2019-08-06 07:42:37 -0500 | [diff] [blame] | 253 | iov[0].iov_base = &requestMsg[0]; |
| 254 | iov[0].iov_len = |
| 255 | sizeof(requestMsg[0]) + sizeof(requestMsg[1]); |
| 256 | iov[1].iov_base = response.data(); |
| 257 | iov[1].iov_len = response.size(); |
| 258 | |
| 259 | msg.msg_iov = iov; |
| 260 | msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]); |
| 261 | |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 262 | int result = sendmsg(fd, &msg, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 263 | if (-1 == result) |
| 264 | { |
| 265 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 266 | std::cerr << "sendto system call failed, RC= " |
| 267 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | else |
| 273 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 274 | std::cerr |
| 275 | << "Failure to read peeked length packet. peekedLength= " |
| 276 | << peekedLength << " recvDataLength=" << recvDataLength |
| 277 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 278 | } |
| 279 | } |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | auto event = Event::get_default(); |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 283 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 284 | bus.request_name("xyz.openbmc_project.PLDM"); |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 285 | IO io(event, socketFd(), EPOLLIN, std::move(callback)); |
| 286 | event.loop(); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 287 | |
| 288 | result = shutdown(sockfd, SHUT_RDWR); |
| 289 | if (-1 == result) |
| 290 | { |
| 291 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame^] | 292 | std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 293 | exit(EXIT_FAILURE); |
| 294 | } |
| 295 | exit(EXIT_FAILURE); |
| 296 | } |