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