Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 1 | #include "dbus_to_file_handler.hpp" |
| 2 | |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 3 | #include "common/utils.hpp" |
| 4 | |
Andrew Jeffery | 21f128d | 2024-01-15 15:34:26 +1030 | [diff] [blame] | 5 | #include <libpldm/oem/ibm/file_io.h> |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 6 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
| 8 | |
| 9 | PHOSPHOR_LOG2_USING; |
| 10 | |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 11 | namespace pldm |
| 12 | { |
| 13 | namespace requester |
| 14 | { |
| 15 | namespace oem_ibm |
| 16 | { |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 17 | using namespace pldm::utils; |
| 18 | using namespace sdbusplus::bus::match::rules; |
| 19 | |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 20 | static constexpr auto resDumpProgressIntf = |
| 21 | "xyz.openbmc_project.Common.Progress"; |
| 22 | static constexpr auto resDumpStatus = |
| 23 | "xyz.openbmc_project.Common.Progress.OperationStatus.Failed"; |
| 24 | |
| 25 | DbusToFileHandler::DbusToFileHandler( |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 26 | int mctp_fd, uint8_t mctp_eid, pldm::InstanceIdDb* instanceIdDb, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 27 | sdbusplus::message::object_path resDumpCurrentObjPath, |
| 28 | pldm::requester::Handler<pldm::requester::Request>* handler) : |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 29 | mctp_fd(mctp_fd), |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 30 | mctp_eid(mctp_eid), instanceIdDb(instanceIdDb), |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 31 | resDumpCurrentObjPath(resDumpCurrentObjPath), handler(handler) |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 32 | {} |
| 33 | |
| 34 | void DbusToFileHandler::sendNewFileAvailableCmd(uint64_t fileSize) |
| 35 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 36 | if (instanceIdDb == NULL) |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 37 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 38 | error( |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 39 | "Failed to send resource dump parameters as instance ID DB is not set"); |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 40 | pldm::utils::reportError( |
Pavithra Barithaya | d28f08c | 2021-12-15 03:37:14 -0600 | [diff] [blame] | 41 | "xyz.openbmc_project.PLDM.Error.sendNewFileAvailableCmd.SendDumpParametersFail"); |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 42 | return; |
| 43 | } |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 44 | auto instanceId = instanceIdDb->next(mctp_eid); |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 45 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
Jayashankar Padath | 7961231 | 2022-06-13 12:47:05 -0500 | [diff] [blame] | 46 | PLDM_NEW_FILE_REQ_BYTES); |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 47 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 48 | // Need to revisit this logic at the time of multiple resource dump support |
| 49 | uint32_t fileHandle = 1; |
| 50 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 51 | auto rc = encode_new_file_req(instanceId, |
| 52 | PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS, |
| 53 | fileHandle, fileSize, request); |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 54 | if (rc != PLDM_SUCCESS) |
| 55 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 56 | instanceIdDb->free(mctp_eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 57 | error("Failed to encode_new_file_req, rc = {RC}", "RC", rc); |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 61 | auto newFileAvailableRespHandler = [this](mctp_eid_t /*eid*/, |
| 62 | const pldm_msg* response, |
| 63 | size_t respMsgLen) { |
| 64 | if (response == nullptr || !respMsgLen) |
| 65 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 66 | error("Failed to receive response for NewFileAvailable command"); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 67 | return; |
| 68 | } |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 69 | uint8_t completionCode{}; |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 70 | auto rc = decode_new_file_resp(response, respMsgLen, &completionCode); |
| 71 | if (rc || completionCode) |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 72 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 73 | error( |
| 74 | "Failed to decode_new_file_resp or Host returned error for new_file_available rc={RC}, cc = {CC}", |
| 75 | "RC", rc, "CC", static_cast<unsigned>(completionCode)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 76 | reportResourceDumpFailure(); |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 77 | } |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 78 | }; |
| 79 | rc = handler->registerRequest( |
| 80 | mctp_eid, instanceId, PLDM_OEM, PLDM_NEW_FILE_AVAILABLE, |
| 81 | std::move(requestMsg), std::move(newFileAvailableRespHandler)); |
| 82 | if (rc) |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 83 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 84 | error("Failed to send NewFileAvailable Request to Host"); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 85 | reportResourceDumpFailure(); |
| 86 | } |
| 87 | } |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 88 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 89 | void DbusToFileHandler::reportResourceDumpFailure() |
| 90 | { |
Pavithra Barithaya | d28f08c | 2021-12-15 03:37:14 -0600 | [diff] [blame] | 91 | pldm::utils::reportError( |
| 92 | "xyz.openbmc_project.PLDM.Error.ReportResourceDumpFail", |
| 93 | pldm::PelSeverity::Warning); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 94 | |
| 95 | PropertyValue value{resDumpStatus}; |
| 96 | DBusMapping dbusMapping{resDumpCurrentObjPath, resDumpProgressIntf, |
| 97 | "Status", "string"}; |
| 98 | try |
| 99 | { |
| 100 | pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value); |
| 101 | } |
| 102 | catch (const std::exception& e) |
| 103 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 104 | error("failed to set resource dump operation status, ERROR={ERR_EXCEP}", |
| 105 | "ERR_EXCEP", e.what()); |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | void DbusToFileHandler::processNewResourceDump( |
| 110 | const std::string& vspString, const std::string& resDumpReqPass) |
| 111 | { |
Jayashankar Padath | 99fa186 | 2021-11-10 09:45:06 -0600 | [diff] [blame] | 112 | try |
| 113 | { |
| 114 | std::string objPath = resDumpCurrentObjPath; |
| 115 | auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant( |
| 116 | objPath.c_str(), "Status", resDumpProgressIntf); |
| 117 | const auto& curResDumpStatus = std::get<ResDumpStatus>(propVal); |
| 118 | |
| 119 | if (curResDumpStatus != |
| 120 | "xyz.openbmc_project.Common.Progress.OperationStatus.InProgress") |
| 121 | { |
| 122 | return; |
| 123 | } |
| 124 | } |
| 125 | catch (const sdbusplus::exception_t& e) |
| 126 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 127 | error( |
| 128 | "Error {ERR_EXCEP} found in getting current resource dump status while initiating a new resource dump with objPath={DUMP_OBJ_PATH} and intf={DUMP_PROG_INTF}", |
| 129 | "ERR_EXCEP", e.what(), "DUMP_OBJ_PATH", |
| 130 | resDumpCurrentObjPath.str.c_str(), "DUMP_PROG_INTF", |
| 131 | resDumpProgressIntf); |
Jayashankar Padath | 99fa186 | 2021-11-10 09:45:06 -0600 | [diff] [blame] | 132 | } |
| 133 | |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 134 | namespace fs = std::filesystem; |
| 135 | const fs::path resDumpDirPath = "/var/lib/pldm/resourcedump"; |
| 136 | |
| 137 | if (!fs::exists(resDumpDirPath)) |
| 138 | { |
| 139 | fs::create_directories(resDumpDirPath); |
| 140 | } |
| 141 | |
| 142 | // Need to reconsider this logic to set the value as "1" when we have the |
| 143 | // support to handle multiple resource dumps |
| 144 | fs::path resDumpFilePath = resDumpDirPath / "1"; |
| 145 | |
| 146 | std::ofstream fileHandle; |
| 147 | fileHandle.open(resDumpFilePath, std::ios::out | std::ofstream::binary); |
| 148 | |
| 149 | if (!fileHandle) |
| 150 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 151 | error("resource dump file open error:{RES_DUMP_PATH}", "RES_DUMP_PATH", |
| 152 | resDumpFilePath); |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 153 | PropertyValue value{resDumpStatus}; |
| 154 | DBusMapping dbusMapping{resDumpCurrentObjPath, resDumpProgressIntf, |
| 155 | "Status", "string"}; |
| 156 | try |
| 157 | { |
| 158 | pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value); |
| 159 | } |
| 160 | catch (const std::exception& e) |
| 161 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 162 | error( |
| 163 | "failed to set resource dump operation status, ERROR={ERR_EXCEP}", |
| 164 | "ERR_EXCEP", e.what()); |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 165 | } |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | // Fill up the file with resource dump parameters and respective sizes |
| 170 | auto fileFunc = [&fileHandle](auto& paramBuf) { |
| 171 | uint32_t paramSize = paramBuf.size(); |
| 172 | fileHandle.write((char*)¶mSize, sizeof(paramSize)); |
| 173 | fileHandle << paramBuf; |
| 174 | }; |
| 175 | fileFunc(vspString); |
| 176 | fileFunc(resDumpReqPass); |
| 177 | |
Pavithra Barithaya | c047f80 | 2021-11-30 01:55:03 -0600 | [diff] [blame] | 178 | std::string str; |
| 179 | if (!resDumpReqPass.empty()) |
| 180 | { |
| 181 | str = getAcfFileContent(); |
| 182 | } |
| 183 | |
| 184 | fileFunc(str); |
| 185 | |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 186 | fileHandle.close(); |
| 187 | size_t fileSize = fs::file_size(resDumpFilePath); |
| 188 | |
| 189 | sendNewFileAvailableCmd(fileSize); |
| 190 | } |
| 191 | |
Pavithra Barithaya | c047f80 | 2021-11-30 01:55:03 -0600 | [diff] [blame] | 192 | std::string DbusToFileHandler::getAcfFileContent() |
| 193 | { |
| 194 | std::string str; |
| 195 | static constexpr auto acfDirPath = "/etc/acf/service.acf"; |
| 196 | if (fs::exists(acfDirPath)) |
| 197 | { |
| 198 | std::ifstream file; |
| 199 | file.open(acfDirPath); |
| 200 | std::stringstream acfBuf; |
| 201 | acfBuf << file.rdbuf(); |
| 202 | str = acfBuf.str(); |
| 203 | file.close(); |
| 204 | } |
| 205 | return str; |
| 206 | } |
| 207 | |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 208 | void DbusToFileHandler::newCsrFileAvailable(const std::string& csr, |
| 209 | const std::string fileHandle) |
| 210 | { |
| 211 | namespace fs = std::filesystem; |
| 212 | std::string dirPath = "/var/lib/ibm/bmcweb"; |
| 213 | const fs::path certDirPath = dirPath; |
| 214 | |
| 215 | if (!fs::exists(certDirPath)) |
| 216 | { |
| 217 | fs::create_directories(certDirPath); |
| 218 | fs::permissions(certDirPath, |
| 219 | fs::perms::others_read | fs::perms::owner_write); |
| 220 | } |
| 221 | |
| 222 | fs::path certFilePath = certDirPath / ("CSR_" + fileHandle); |
| 223 | std::ofstream certFile; |
| 224 | |
| 225 | certFile.open(certFilePath, std::ios::out | std::ofstream::binary); |
| 226 | |
| 227 | if (!certFile) |
| 228 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 229 | error("cert file open error: {CERT_PATH}", "CERT_PATH", |
| 230 | certFilePath.c_str()); |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
| 234 | // Add csr to file |
| 235 | certFile << csr << std::endl; |
| 236 | |
| 237 | certFile.close(); |
| 238 | uint32_t fileSize = fs::file_size(certFilePath); |
| 239 | |
| 240 | newFileAvailableSendToHost(fileSize, (uint32_t)stoi(fileHandle), |
| 241 | PLDM_FILE_TYPE_CERT_SIGNING_REQUEST); |
| 242 | } |
| 243 | |
| 244 | void DbusToFileHandler::newFileAvailableSendToHost(const uint32_t fileSize, |
| 245 | const uint32_t fileHandle, |
| 246 | const uint16_t type) |
| 247 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 248 | if (instanceIdDb == NULL) |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 249 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 250 | error("Failed to send csr to host."); |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 251 | pldm::utils::reportError( |
Pavithra Barithaya | d28f08c | 2021-12-15 03:37:14 -0600 | [diff] [blame] | 252 | "xyz.openbmc_project.PLDM.Error.SendFileToHostFail"); |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 253 | return; |
| 254 | } |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 255 | auto instanceId = instanceIdDb->next(mctp_eid); |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 256 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 257 | PLDM_NEW_FILE_REQ_BYTES); |
| 258 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 259 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 260 | auto rc = encode_new_file_req(instanceId, type, fileHandle, fileSize, |
| 261 | request); |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 262 | if (rc != PLDM_SUCCESS) |
| 263 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 264 | instanceIdDb->free(mctp_eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 265 | error("Failed to encode_new_file_req, rc = {RC}", "RC", rc); |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 266 | return; |
| 267 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 268 | auto newFileAvailableRespHandler = |
| 269 | [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) { |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 270 | if (response == nullptr || !respMsgLen) |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 271 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 272 | error( |
| 273 | "Failed to receive response for NewFileAvailable command for vmi"); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 274 | return; |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 275 | } |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 276 | uint8_t completionCode{}; |
| 277 | auto rc = decode_new_file_resp(response, respMsgLen, &completionCode); |
| 278 | if (rc || completionCode) |
| 279 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 280 | error( |
| 281 | "Failed to decode_new_file_resp for vmi, or Host returned error for new_file_available rc = {RC}, cc = {CC}", |
| 282 | "RC", rc, "CC", static_cast<unsigned>(completionCode)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 283 | pldm::utils::reportError( |
Pavithra Barithaya | d28f08c | 2021-12-15 03:37:14 -0600 | [diff] [blame] | 284 | "xyz.openbmc_project.PLDM.Error.DecodeNewFileResponseFail"); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 285 | } |
| 286 | }; |
| 287 | rc = handler->registerRequest( |
| 288 | mctp_eid, instanceId, PLDM_OEM, PLDM_NEW_FILE_AVAILABLE, |
| 289 | std::move(requestMsg), std::move(newFileAvailableRespHandler)); |
| 290 | if (rc) |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 291 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 292 | error("Failed to send NewFileAvailable Request to Host for vmi"); |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 293 | pldm::utils::reportError( |
Pavithra Barithaya | d28f08c | 2021-12-15 03:37:14 -0600 | [diff] [blame] | 294 | "xyz.openbmc_project.PLDM.Error.NewFileAvailableRequestFail"); |
Varsha Kaverappa | 219ace9 | 2021-04-01 02:50:11 -0500 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 298 | } // namespace oem_ibm |
| 299 | } // namespace requester |
| 300 | } // namespace pldm |