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