Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 1 | #include "dbus_impl_requester.hpp" |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 2 | #include "host_pdr_handler.hpp" |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 3 | #include "invoker.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 4 | #include "libpldmresponder/base.hpp" |
| 5 | #include "libpldmresponder/bios.hpp" |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 6 | #include "libpldmresponder/fru.hpp" |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 7 | #include "libpldmresponder/platform.hpp" |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 8 | #include "utils.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 9 | |
| 10 | #include <err.h> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 11 | #include <getopt.h> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 12 | #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 Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 21 | #include <fstream> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 22 | #include <iomanip> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 23 | #include <iostream> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 24 | #include <iterator> |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 25 | #include <memory> |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 26 | #include <sdeventplus/event.hpp> |
| 27 | #include <sdeventplus/source/io.hpp> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 28 | #include <sstream> |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 29 | #include <stdexcept> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 30 | #include <string> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
| 33 | #include "libpldm/base.h" |
| 34 | #include "libpldm/bios.h" |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 35 | #include "libpldm/pdr.h" |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 36 | #include "libpldm/platform.h" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 37 | |
| 38 | #ifdef OEM_IBM |
| 39 | #include "libpldmresponder/file_io.hpp" |
| 40 | #endif |
| 41 | |
| 42 | constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1; |
| 43 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 44 | using namespace pldm::responder; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 45 | using namespace pldm; |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 46 | using namespace sdeventplus; |
| 47 | using namespace sdeventplus::source; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 48 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 49 | static Response processRxMsg(const std::vector<uint8_t>& requestMsg, |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 50 | Invoker& invoker, dbus_api::Requester& requester) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 51 | { |
| 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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 61 | std::cerr << "Empty PLDM request header \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 62 | } |
| 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 Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 68 | 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 74 | { |
| 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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 86 | std::cerr << "Failed adding response header \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 87 | } |
| 88 | response.insert(response.end(), completion_code); |
| 89 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 90 | } |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 91 | else |
| 92 | { |
| 93 | requester.markFree(eid, hdr->instance_id); |
| 94 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 95 | return response; |
| 96 | } |
| 97 | |
| 98 | void 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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 110 | std::cout << tempStream.str().c_str() << std::endl; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 113 | void 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 122 | int main(int argc, char** argv) |
| 123 | { |
| 124 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 125 | 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 151 | /* 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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 157 | std::cerr << "Failed to create the socket, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 158 | exit(EXIT_FAILURE); |
| 159 | } |
| 160 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 161 | 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 Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 175 | sockfd, hostEID, event, pdrRepo.get(), entityTree.get(), |
| 176 | dbusImplReq); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | Invoker invoker{}; |
| 180 | invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>()); |
| 181 | invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>()); |
| 182 | invoker.registerHandler( |
| 183 | PLDM_PLATFORM, std::make_unique<platform::Handler>( |
| 184 | PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get())); |
| 185 | invoker.registerHandler( |
| 186 | PLDM_FRU, std::make_unique<fru::Handler>(FRU_JSONS_DIR, pdrRepo.get(), |
| 187 | entityTree.get())); |
| 188 | |
| 189 | #ifdef OEM_IBM |
| 190 | invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>()); |
| 191 | #endif |
| 192 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 193 | pldm::utils::CustomFD socketFd(sockfd); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 194 | |
| 195 | struct sockaddr_un addr |
| 196 | { |
| 197 | }; |
| 198 | addr.sun_family = AF_UNIX; |
| 199 | const char path[] = "\0mctp-mux"; |
| 200 | memcpy(addr.sun_path, path, sizeof(path) - 1); |
| 201 | int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr), |
| 202 | sizeof(path) + sizeof(addr.sun_family) - 1); |
| 203 | if (-1 == result) |
| 204 | { |
| 205 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 206 | std::cerr << "Failed to connect to the socket, RC= " << returnCode |
| 207 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 208 | exit(EXIT_FAILURE); |
| 209 | } |
| 210 | |
| 211 | result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM)); |
| 212 | if (-1 == result) |
| 213 | { |
| 214 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 215 | std::cerr << "Failed to send message type as pldm to mctp, RC= " |
| 216 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 217 | exit(EXIT_FAILURE); |
| 218 | } |
| 219 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 220 | auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd, |
| 221 | uint32_t revents) { |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 222 | if (!(revents & EPOLLIN)) |
| 223 | { |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | // Outgoing message. |
| 228 | struct iovec iov[2]{}; |
| 229 | |
| 230 | // This structure contains the parameter information for the response |
| 231 | // message. |
| 232 | struct msghdr msg |
| 233 | { |
| 234 | }; |
| 235 | |
| 236 | int returnCode = 0; |
| 237 | ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 238 | if (0 == peekedLength) |
| 239 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 240 | std::cerr << "Socket has been closed \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 241 | } |
| 242 | else if (peekedLength <= -1) |
| 243 | { |
| 244 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 245 | std::cerr << "recv system call failed, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 246 | } |
| 247 | else |
| 248 | { |
| 249 | std::vector<uint8_t> requestMsg(peekedLength); |
| 250 | auto recvDataLength = recv( |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 251 | fd, static_cast<void*>(requestMsg.data()), peekedLength, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 252 | if (recvDataLength == peekedLength) |
| 253 | { |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 254 | if (verbose) |
| 255 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 256 | std::cout << "Received Msg" << std::endl; |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 257 | printBuffer(requestMsg); |
| 258 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 259 | if (MCTP_MSG_TYPE_PLDM != requestMsg[1]) |
| 260 | { |
| 261 | // Skip this message and continue. |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 262 | std::cerr << "Encountered Non-PLDM type message" |
| 263 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 264 | } |
| 265 | else |
| 266 | { |
| 267 | // process message and send response |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 268 | auto response = |
| 269 | processRxMsg(requestMsg, invoker, dbusImplReq); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 270 | if (!response.empty()) |
| 271 | { |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 272 | if (verbose) |
| 273 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 274 | std::cout << "Sending Msg" << std::endl; |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 275 | printBuffer(response); |
| 276 | } |
| 277 | |
Zahed Hossain | 09a96e0 | 2019-08-06 07:42:37 -0500 | [diff] [blame] | 278 | iov[0].iov_base = &requestMsg[0]; |
| 279 | iov[0].iov_len = |
| 280 | sizeof(requestMsg[0]) + sizeof(requestMsg[1]); |
| 281 | iov[1].iov_base = response.data(); |
| 282 | iov[1].iov_len = response.size(); |
| 283 | |
| 284 | msg.msg_iov = iov; |
| 285 | msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]); |
| 286 | |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 287 | int result = sendmsg(fd, &msg, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 288 | if (-1 == result) |
| 289 | { |
| 290 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 291 | std::cerr << "sendto system call failed, RC= " |
| 292 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | else |
| 298 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 299 | std::cerr |
| 300 | << "Failure to read peeked length packet. peekedLength= " |
| 301 | << peekedLength << " recvDataLength=" << recvDataLength |
| 302 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 303 | } |
| 304 | } |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 305 | }; |
| 306 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 307 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 308 | bus.request_name("xyz.openbmc_project.PLDM"); |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 309 | IO io(event, socketFd(), EPOLLIN, std::move(callback)); |
| 310 | event.loop(); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 311 | |
| 312 | result = shutdown(sockfd, SHUT_RDWR); |
| 313 | if (-1 == result) |
| 314 | { |
| 315 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 316 | std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 317 | exit(EXIT_FAILURE); |
| 318 | } |
| 319 | exit(EXIT_FAILURE); |
| 320 | } |