Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Ramesh Iyyar | 3af5c32 | 2020-12-04 00:38:42 -0600 | [diff] [blame] | 16 | #include "pldm_oem_cmds.hpp" |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 17 | |
| 18 | #include "dump_utils.hpp" |
Ramesh Iyyar | 3af5c32 | 2020-12-04 00:38:42 -0600 | [diff] [blame] | 19 | #include "pldm_utils.hpp" |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 20 | #include "xyz/openbmc_project/Common/error.hpp" |
| 21 | |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 22 | #include <fmt/core.h> |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 23 | #include <libpldm/base.h> |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 24 | #include <libpldm/file_io.h> |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 25 | #include <libpldm/platform.h> |
| 26 | #include <unistd.h> |
| 27 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 28 | #include <phosphor-logging/elog-errors.hpp> |
| 29 | #include <phosphor-logging/log.hpp> |
| 30 | #include <sdbusplus/bus.hpp> |
| 31 | |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 32 | #include <fstream> |
| 33 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 34 | namespace phosphor |
| 35 | { |
| 36 | namespace dump |
| 37 | { |
Dhruvaraj Subhashchandran | 59642e2 | 2020-03-19 03:37:44 -0500 | [diff] [blame] | 38 | namespace host |
| 39 | { |
| 40 | /** |
| 41 | * @brief Initiate offload of the dump with provided id |
| 42 | * |
| 43 | * @param[in] id - The Dump Source ID. |
| 44 | * |
| 45 | */ |
| 46 | void requestOffload(uint32_t id) |
| 47 | { |
| 48 | pldm::requestOffload(id); |
| 49 | } |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 50 | |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 51 | void requestDelete(uint32_t id, uint32_t dumpType) |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 52 | { |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 53 | pldm::requestDelete(id, dumpType); |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 54 | } |
Dhruvaraj Subhashchandran | 59642e2 | 2020-03-19 03:37:44 -0500 | [diff] [blame] | 55 | } // namespace host |
| 56 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 57 | namespace pldm |
| 58 | { |
| 59 | |
| 60 | using namespace phosphor::logging; |
| 61 | |
| 62 | constexpr auto eidPath = "/usr/share/pldm/host_eid"; |
| 63 | constexpr mctp_eid_t defaultEIDValue = 9; |
| 64 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 65 | using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 66 | using Reason = xyz::openbmc_project::Common::NotAllowed::REASON; |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 67 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 68 | mctp_eid_t readEID() |
| 69 | { |
| 70 | mctp_eid_t eid = defaultEIDValue; |
| 71 | |
| 72 | std::ifstream eidFile{eidPath}; |
| 73 | if (!eidFile.good()) |
| 74 | { |
| 75 | log<level::ERR>("Could not open host EID file"); |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 76 | elog<NotAllowed>(Reason("Required host dump action via pldm is not " |
| 77 | "allowed due to mctp end point read failed")); |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 78 | } |
| 79 | else |
| 80 | { |
| 81 | std::string eid; |
| 82 | eidFile >> eid; |
| 83 | if (!eid.empty()) |
| 84 | { |
| 85 | eid = strtol(eid.c_str(), nullptr, 10); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | log<level::ERR>("EID file was empty"); |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 90 | elog<NotAllowed>( |
| 91 | Reason("Required host dump action via pldm is not " |
| 92 | "allowed due to mctp end point read failed")); |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | return eid; |
| 97 | } |
| 98 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 99 | void requestOffload(uint32_t id) |
| 100 | { |
| 101 | uint16_t effecterId = 0x05; // TODO PhyP temporary Hardcoded value. |
| 102 | |
| 103 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterId) + sizeof(id) + |
| 104 | sizeof(uint8_t)> |
| 105 | requestMsg{}; |
| 106 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 107 | |
| 108 | std::array<uint8_t, sizeof(id)> effecterValue{}; |
| 109 | |
| 110 | memcpy(effecterValue.data(), &id, sizeof(id)); |
| 111 | |
| 112 | mctp_eid_t eid = readEID(); |
| 113 | |
| 114 | auto instanceID = getPLDMInstanceID(eid); |
| 115 | |
| 116 | auto rc = encode_set_numeric_effecter_value_req( |
| 117 | instanceID, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32, |
| 118 | effecterValue.data(), request, |
| 119 | requestMsg.size() - sizeof(pldm_msg_hdr)); |
| 120 | |
| 121 | if (rc != PLDM_SUCCESS) |
| 122 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 123 | log<level::ERR>( |
| 124 | fmt::format("Message encode failure. RC({})", rc).c_str()); |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 125 | elog<NotAllowed>(Reason("Host dump offload via pldm is not " |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 126 | "allowed due to encode failed")); |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | uint8_t* responseMsg = nullptr; |
| 130 | size_t responseMsgSize{}; |
| 131 | |
Ramesh Iyyar | 5765b1d | 2020-12-04 01:29:44 -0600 | [diff] [blame] | 132 | CustomFd fd(openPLDM()); |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 133 | |
Ramesh Iyyar | 5765b1d | 2020-12-04 01:29:44 -0600 | [diff] [blame] | 134 | rc = pldm_send_recv(eid, fd(), requestMsg.data(), requestMsg.size(), |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 135 | &responseMsg, &responseMsgSize); |
| 136 | if (rc < 0) |
| 137 | { |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 138 | auto e = errno; |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 139 | log<level::ERR>( |
| 140 | fmt::format("pldm_send failed, RC({}), errno({})", rc, e).c_str()); |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 141 | elog<NotAllowed>(Reason("Host dump offload via pldm is not " |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 142 | "allowed due to fileack send failed")); |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 143 | } |
| 144 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg); |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 145 | log<level::INFO>(fmt::format("Done. PLDM message, RC({})", |
| 146 | static_cast<uint16_t>(response->payload[0])) |
| 147 | .c_str()); |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 148 | } |
| 149 | |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 150 | void requestDelete(uint32_t dumpId, uint32_t dumpType) |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 151 | { |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 152 | pldm_fileio_file_type pldmDumpType; |
| 153 | switch (dumpType) |
| 154 | { |
| 155 | case PLDM_FILE_TYPE_DUMP: |
| 156 | pldmDumpType = PLDM_FILE_TYPE_DUMP; |
| 157 | break; |
Dhruvaraj Subhashchandran | 0c782d6 | 2021-03-24 13:27:13 -0500 | [diff] [blame] | 158 | case PLDM_FILE_TYPE_RESOURCE_DUMP: |
| 159 | pldmDumpType = PLDM_FILE_TYPE_RESOURCE_DUMP; |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 160 | break; |
| 161 | default: |
| 162 | throw std::runtime_error("Unknown pldm dump file-io type to delete " |
| 163 | "host dump"); |
| 164 | } |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 165 | const size_t pldmMsgHdrSize = sizeof(pldm_msg_hdr); |
| 166 | std::array<uint8_t, pldmMsgHdrSize + PLDM_FILE_ACK_REQ_BYTES> fileAckReqMsg; |
| 167 | |
| 168 | mctp_eid_t mctpEndPointId = readEID(); |
| 169 | |
| 170 | auto pldmInstanceId = getPLDMInstanceID(mctpEndPointId); |
| 171 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 172 | // - PLDM_SUCCESS - To indicate dump was readed (offloaded) or user decided, |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 173 | // no longer host dump is not required so, initiate deletion from |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 174 | // host memory |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 175 | int retCode = |
| 176 | encode_file_ack_req(pldmInstanceId, pldmDumpType, dumpId, PLDM_SUCCESS, |
| 177 | reinterpret_cast<pldm_msg*>(fileAckReqMsg.data())); |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 178 | |
| 179 | if (retCode != PLDM_SUCCESS) |
| 180 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 181 | log<level::ERR>( |
| 182 | fmt::format("Failed to encode pldm FileAck to delete host " |
| 183 | "dump,SRC_DUMP_ID({}), " |
| 184 | "PLDM_FILE_IO_TYPE({}),PLDM_RETURN_CODE({})", |
| 185 | dumpId, pldmDumpType, retCode) |
| 186 | .c_str()); |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 187 | elog<NotAllowed>(Reason("Host dump deletion via pldm is not " |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 188 | "allowed due to encode fileack failed")); |
| 189 | } |
| 190 | |
| 191 | uint8_t* pldmRespMsg = nullptr; |
| 192 | size_t pldmRespMsgSize; |
| 193 | |
| 194 | CustomFd pldmFd(openPLDM()); |
| 195 | |
| 196 | retCode = |
| 197 | pldm_send_recv(mctpEndPointId, pldmFd(), fileAckReqMsg.data(), |
| 198 | fileAckReqMsg.size(), &pldmRespMsg, &pldmRespMsgSize); |
| 199 | |
| 200 | std::unique_ptr<uint8_t, decltype(std::free)*> pldmRespMsgPtr{pldmRespMsg, |
| 201 | std::free}; |
| 202 | if (retCode != PLDM_REQUESTER_SUCCESS) |
| 203 | { |
| 204 | auto errorNumber = errno; |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 205 | log<level::ERR>( |
| 206 | fmt::format("Failed to send pldm FileAck to delete host dump, " |
| 207 | "SRC_DUMP_ID({}), PLDM_FILE_IO_TYPE({}), " |
| 208 | "PLDM_RETURN_CODE({}), ERRNO({}), ERRMSG({})", |
| 209 | dumpId, pldmDumpType, retCode, errorNumber, |
| 210 | strerror(errorNumber)) |
| 211 | .c_str()); |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 212 | elog<NotAllowed>(Reason("Host dump deletion via pldm is not " |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 213 | "allowed due to fileack send failed")); |
| 214 | } |
| 215 | |
| 216 | uint8_t completionCode; |
| 217 | |
| 218 | retCode = |
| 219 | decode_file_ack_resp(reinterpret_cast<pldm_msg*>(pldmRespMsgPtr.get()), |
| 220 | pldmRespMsgSize - pldmMsgHdrSize, &completionCode); |
| 221 | |
| 222 | if (retCode || completionCode) |
| 223 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 224 | log<level::ERR>( |
| 225 | fmt::format("Failed to delete host dump, SRC_DUMP_ID({}), " |
| 226 | "PLDM_FILE_IO_TYPE({}), PLDM_RETURN_CODE({}), " |
| 227 | "PLDM_COMPLETION_CODE({})", |
| 228 | dumpId, pldmDumpType, retCode, completionCode) |
| 229 | .c_str()); |
Dhruvaraj Subhashchandran | 4c63ce5 | 2020-12-18 02:07:22 -0600 | [diff] [blame] | 230 | elog<NotAllowed>(Reason("Host dump deletion via pldm is " |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 231 | "failed")); |
| 232 | } |
| 233 | |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 234 | log<level::INFO>( |
| 235 | fmt::format("Deleted host dump, SRC_DUMP_ID({})", dumpId).c_str()); |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 236 | } |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 237 | } // namespace pldm |
| 238 | } // namespace dump |
| 239 | } // namespace phosphor |