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