Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 1 | #include "pldm_cmd_helper.hpp" |
| 2 | |
Andrew Jeffery | 27a022c | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 3 | #include "libpldm/pldm.h" |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 4 | |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 5 | #include "xyz/openbmc_project/Common/error.hpp" |
| 6 | |
| 7 | #include <systemd/sd-bus.h> |
| 8 | |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 9 | #include <sdbusplus/server.hpp> |
| 10 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
| 11 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 12 | #include <exception> |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 13 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 14 | using namespace pldm::utils; |
| 15 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 16 | namespace pldmtool |
| 17 | { |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 18 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 19 | namespace helper |
| 20 | { |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 21 | /* |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 22 | * Initialize the socket, send pldm command & recieve response from socket |
| 23 | * |
| 24 | */ |
| 25 | int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg, |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 26 | std::vector<uint8_t>& responseMsg, bool pldmVerbose) |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 27 | { |
| 28 | const char devPath[] = "\0mctp-mux"; |
| 29 | int returnCode = 0; |
| 30 | |
| 31 | int sockFd = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 32 | if (-1 == sockFd) |
| 33 | { |
| 34 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 35 | std::cerr << "Failed to create the socket : RC = " << sockFd << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 36 | return returnCode; |
| 37 | } |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 38 | Logger(pldmVerbose, "Success in creating the socket : RC = ", sockFd); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 39 | |
| 40 | struct sockaddr_un addr |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 41 | {}; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 42 | addr.sun_family = AF_UNIX; |
| 43 | |
| 44 | memcpy(addr.sun_path, devPath, sizeof(devPath) - 1); |
| 45 | |
| 46 | CustomFD socketFd(sockFd); |
| 47 | int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr), |
| 48 | sizeof(devPath) + sizeof(addr.sun_family) - 1); |
| 49 | if (-1 == result) |
| 50 | { |
| 51 | returnCode = -errno; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 52 | std::cerr << "Failed to connect to socket : RC = " << returnCode |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 53 | << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 54 | return returnCode; |
| 55 | } |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 56 | Logger(pldmVerbose, "Success in connecting to socket : RC = ", returnCode); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 57 | |
| 58 | auto pldmType = MCTP_MSG_TYPE_PLDM; |
| 59 | result = write(socketFd(), &pldmType, sizeof(pldmType)); |
| 60 | if (-1 == result) |
| 61 | { |
| 62 | returnCode = -errno; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 63 | std::cerr << "Failed to send message type as pldm to mctp : RC = " |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 64 | << returnCode << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 65 | return returnCode; |
| 66 | } |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 67 | Logger( |
| 68 | pldmVerbose, |
| 69 | "Success in sending message type as pldm to mctp : RC = ", returnCode); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 70 | |
| 71 | result = send(socketFd(), requestMsg.data(), requestMsg.size(), 0); |
| 72 | if (-1 == result) |
| 73 | { |
| 74 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 75 | std::cerr << "Write to socket failure : RC = " << returnCode << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 76 | return returnCode; |
| 77 | } |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 78 | Logger(pldmVerbose, "Write to socket successful : RC = ", result); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 79 | |
| 80 | // Read the response from socket |
| 81 | ssize_t peekedLength = recv(socketFd(), nullptr, 0, MSG_TRUNC | MSG_PEEK); |
| 82 | if (0 == peekedLength) |
| 83 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 84 | std::cerr << "Socket is closed : peekedLength = " << peekedLength |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 85 | << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 86 | return returnCode; |
| 87 | } |
| 88 | else if (peekedLength <= -1) |
| 89 | { |
| 90 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 91 | std::cerr << "recv() system call failed : RC = " << returnCode << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 92 | return returnCode; |
| 93 | } |
| 94 | else |
| 95 | { |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 96 | auto reqhdr = reinterpret_cast<const pldm_msg_hdr*>(&requestMsg[2]); |
| 97 | do |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 98 | { |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 99 | ssize_t peekedLength = |
| 100 | recv(socketFd(), nullptr, 0, MSG_PEEK | MSG_TRUNC); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 101 | responseMsg.resize(peekedLength); |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 102 | auto recvDataLength = |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 103 | recv(socketFd(), reinterpret_cast<void*>(responseMsg.data()), |
| 104 | peekedLength, 0); |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 105 | auto resphdr = |
| 106 | reinterpret_cast<const pldm_msg_hdr*>(&responseMsg[2]); |
| 107 | if (recvDataLength == peekedLength && |
| 108 | resphdr->instance_id == reqhdr->instance_id && |
| 109 | resphdr->request != PLDM_REQUEST) |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 110 | { |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 111 | Logger(pldmVerbose, "Total length:", recvDataLength); |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 112 | break; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 113 | } |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 114 | else if (recvDataLength != peekedLength) |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 115 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 116 | std::cerr << "Failure to read response length packet: length = " |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 117 | << recvDataLength << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 118 | return returnCode; |
| 119 | } |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 120 | } while (1); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 121 | } |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 122 | |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 123 | returnCode = shutdown(socketFd(), SHUT_RDWR); |
| 124 | if (-1 == returnCode) |
| 125 | { |
| 126 | returnCode = -errno; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 127 | std::cerr << "Failed to shutdown the socket : RC = " << returnCode |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 128 | << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 129 | return returnCode; |
| 130 | } |
| 131 | |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 132 | Logger(pldmVerbose, "Shutdown Socket successful : RC = ", returnCode); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 133 | return PLDM_SUCCESS; |
| 134 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 135 | |
| 136 | void CommandInterface::exec() |
| 137 | { |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 138 | static constexpr auto pldmObjPath = "/xyz/openbmc_project/pldm"; |
| 139 | static constexpr auto pldmRequester = "xyz.openbmc_project.PLDM.Requester"; |
| 140 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 141 | try |
| 142 | { |
| 143 | auto service = |
| 144 | pldm::utils::DBusHandler().getService(pldmObjPath, pldmRequester); |
| 145 | auto method = bus.new_method_call(service.c_str(), pldmObjPath, |
| 146 | pldmRequester, "GetInstanceId"); |
| 147 | method.append(mctp_eid); |
| 148 | auto reply = bus.call(method); |
| 149 | reply.read(instanceId); |
| 150 | } |
| 151 | catch (const std::exception& e) |
| 152 | { |
Manojkiran Eda | 0a725f8 | 2021-10-23 06:02:26 +0530 | [diff] [blame] | 153 | std::cerr << "GetInstanceId D-Bus call failed, MCTP id = " |
| 154 | << (unsigned)mctp_eid << ", error = " << e.what() << "\n"; |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 155 | return; |
| 156 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 157 | auto [rc, requestMsg] = createRequestMsg(); |
| 158 | if (rc != PLDM_SUCCESS) |
| 159 | { |
| 160 | std::cerr << "Failed to encode request message for " << pldmType << ":" |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 161 | << commandName << " rc = " << rc << "\n"; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 165 | std::vector<uint8_t> responseMsg; |
| 166 | rc = pldmSendRecv(requestMsg, responseMsg); |
| 167 | |
| 168 | if (rc != PLDM_SUCCESS) |
| 169 | { |
| 170 | std::cerr << "pldmSendRecv: Failed to receive RC = " << rc << "\n"; |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsg.data()); |
| 175 | parseResponseMsg(responsePtr, responseMsg.size() - sizeof(pldm_msg_hdr)); |
| 176 | } |
| 177 | |
| 178 | int CommandInterface::pldmSendRecv(std::vector<uint8_t>& requestMsg, |
| 179 | std::vector<uint8_t>& responseMsg) |
| 180 | { |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 181 | // Insert the PLDM message type and EID at the beginning of the |
| 182 | // msg. |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 183 | requestMsg.insert(requestMsg.begin(), MCTP_MSG_TYPE_PLDM); |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 184 | requestMsg.insert(requestMsg.begin(), mctp_eid); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 185 | |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 186 | bool mctpVerbose = pldmVerbose; |
| 187 | |
| 188 | // By default enable request/response msgs for pldmtool raw commands. |
| 189 | if (CommandInterface::pldmType == "raw") |
| 190 | { |
| 191 | pldmVerbose = true; |
| 192 | } |
| 193 | |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 194 | if (pldmVerbose) |
| 195 | { |
| 196 | std::cout << "pldmtool: "; |
| 197 | printBuffer(Tx, requestMsg); |
| 198 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 199 | |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 200 | if (mctp_eid != PLDM_ENTITY_ID) |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 201 | { |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 202 | int fd = pldm_open(); |
| 203 | if (-1 == fd) |
| 204 | { |
| 205 | std::cerr << "failed to init mctp " |
| 206 | << "\n"; |
| 207 | return -1; |
| 208 | } |
| 209 | uint8_t* responseMessage = nullptr; |
| 210 | size_t responseMessageSize{}; |
Sampa Misra | 9f8d2b0 | 2021-03-24 08:33:14 +0000 | [diff] [blame] | 211 | pldm_send_recv(mctp_eid, fd, requestMsg.data() + 2, |
| 212 | requestMsg.size() - 2, &responseMessage, |
| 213 | &responseMessageSize); |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 214 | |
| 215 | responseMsg.resize(responseMessageSize); |
| 216 | memcpy(responseMsg.data(), responseMessage, responseMsg.size()); |
| 217 | |
| 218 | free(responseMessage); |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 219 | if (pldmVerbose) |
| 220 | { |
| 221 | std::cout << "pldmtool: "; |
| 222 | printBuffer(Rx, responseMsg); |
| 223 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 224 | } |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 225 | else |
| 226 | { |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 227 | mctpSockSendRecv(requestMsg, responseMsg, mctpVerbose); |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 228 | if (pldmVerbose) |
| 229 | { |
| 230 | std::cout << "pldmtool: "; |
| 231 | printBuffer(Rx, responseMsg); |
| 232 | } |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 233 | responseMsg.erase(responseMsg.begin(), |
| 234 | responseMsg.begin() + 2 /* skip the mctp header */); |
| 235 | } |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 236 | return PLDM_SUCCESS; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 237 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 238 | } // namespace helper |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 239 | } // namespace pldmtool |