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>()); |
TOM JOSEPH | d4d97a5 | 2020-03-23 14:36:34 +0530 | [diff] [blame] | 182 | invoker.registerHandler(PLDM_PLATFORM, |
| 183 | std::make_unique<platform::Handler>( |
| 184 | PDR_JSONS_DIR, EVENTS_JSONS_DIR, pdrRepo.get(), |
| 185 | hostPDRHandler.get())); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 186 | invoker.registerHandler( |
| 187 | PLDM_FRU, std::make_unique<fru::Handler>(FRU_JSONS_DIR, pdrRepo.get(), |
| 188 | entityTree.get())); |
| 189 | |
| 190 | #ifdef OEM_IBM |
| 191 | invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>()); |
| 192 | #endif |
| 193 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 194 | pldm::utils::CustomFD socketFd(sockfd); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 195 | |
| 196 | struct sockaddr_un addr |
| 197 | { |
| 198 | }; |
| 199 | addr.sun_family = AF_UNIX; |
| 200 | const char path[] = "\0mctp-mux"; |
| 201 | memcpy(addr.sun_path, path, sizeof(path) - 1); |
| 202 | int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr), |
| 203 | sizeof(path) + sizeof(addr.sun_family) - 1); |
| 204 | if (-1 == result) |
| 205 | { |
| 206 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 207 | std::cerr << "Failed to connect to the socket, RC= " << returnCode |
| 208 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 209 | exit(EXIT_FAILURE); |
| 210 | } |
| 211 | |
| 212 | result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM)); |
| 213 | if (-1 == result) |
| 214 | { |
| 215 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 216 | std::cerr << "Failed to send message type as pldm to mctp, RC= " |
| 217 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 218 | exit(EXIT_FAILURE); |
| 219 | } |
| 220 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 221 | auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd, |
| 222 | uint32_t revents) { |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 223 | if (!(revents & EPOLLIN)) |
| 224 | { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | // Outgoing message. |
| 229 | struct iovec iov[2]{}; |
| 230 | |
| 231 | // This structure contains the parameter information for the response |
| 232 | // message. |
| 233 | struct msghdr msg |
| 234 | { |
| 235 | }; |
| 236 | |
| 237 | int returnCode = 0; |
| 238 | ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 239 | if (0 == peekedLength) |
| 240 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 241 | std::cerr << "Socket has been closed \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 242 | } |
| 243 | else if (peekedLength <= -1) |
| 244 | { |
| 245 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 246 | std::cerr << "recv system call failed, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 247 | } |
| 248 | else |
| 249 | { |
| 250 | std::vector<uint8_t> requestMsg(peekedLength); |
| 251 | auto recvDataLength = recv( |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 252 | fd, static_cast<void*>(requestMsg.data()), peekedLength, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 253 | if (recvDataLength == peekedLength) |
| 254 | { |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 255 | if (verbose) |
| 256 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 257 | std::cout << "Received Msg" << std::endl; |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 258 | printBuffer(requestMsg); |
| 259 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 260 | if (MCTP_MSG_TYPE_PLDM != requestMsg[1]) |
| 261 | { |
| 262 | // Skip this message and continue. |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 263 | std::cerr << "Encountered Non-PLDM type message" |
| 264 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 265 | } |
| 266 | else |
| 267 | { |
| 268 | // process message and send response |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 269 | auto response = |
| 270 | processRxMsg(requestMsg, invoker, dbusImplReq); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 271 | if (!response.empty()) |
| 272 | { |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 273 | if (verbose) |
| 274 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 275 | std::cout << "Sending Msg" << std::endl; |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 276 | printBuffer(response); |
| 277 | } |
| 278 | |
Zahed Hossain | 09a96e0 | 2019-08-06 07:42:37 -0500 | [diff] [blame] | 279 | iov[0].iov_base = &requestMsg[0]; |
| 280 | iov[0].iov_len = |
| 281 | sizeof(requestMsg[0]) + sizeof(requestMsg[1]); |
| 282 | iov[1].iov_base = response.data(); |
| 283 | iov[1].iov_len = response.size(); |
| 284 | |
| 285 | msg.msg_iov = iov; |
| 286 | msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]); |
| 287 | |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 288 | int result = sendmsg(fd, &msg, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 289 | if (-1 == result) |
| 290 | { |
| 291 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 292 | std::cerr << "sendto system call failed, RC= " |
| 293 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | else |
| 299 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 300 | std::cerr |
| 301 | << "Failure to read peeked length packet. peekedLength= " |
| 302 | << peekedLength << " recvDataLength=" << recvDataLength |
| 303 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 304 | } |
| 305 | } |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 306 | }; |
| 307 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 308 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 309 | bus.request_name("xyz.openbmc_project.PLDM"); |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 310 | IO io(event, socketFd(), EPOLLIN, std::move(callback)); |
| 311 | event.loop(); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 312 | |
| 313 | result = shutdown(sockfd, SHUT_RDWR); |
| 314 | if (-1 == result) |
| 315 | { |
| 316 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 317 | std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 318 | exit(EXIT_FAILURE); |
| 319 | } |
| 320 | exit(EXIT_FAILURE); |
| 321 | } |