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