blob: 26d3e320f62cd776d36d2eb86552d3447f5b5b73 [file] [log] [blame]
Jayashankar Padathdb124362021-01-28 21:12:34 -06001#include "dbus_to_file_handler.hpp"
2
Jayashankar Padathdb124362021-01-28 21:12:34 -06003#include "common/utils.hpp"
4
Andrew Jeffery21f128d2024-01-15 15:34:26 +10305#include <libpldm/oem/ibm/file_io.h>
George Liuc453e162022-12-21 17:16:23 +08006
Riya Dixit49cfb132023-03-02 04:26:53 -06007#include <phosphor-logging/lg2.hpp>
8
9PHOSPHOR_LOG2_USING;
10
Jayashankar Padathdb124362021-01-28 21:12:34 -060011namespace pldm
12{
13namespace requester
14{
15namespace oem_ibm
16{
Jayashankar Padathdb124362021-01-28 21:12:34 -060017using namespace pldm::utils;
18using namespace sdbusplus::bus::match::rules;
19
Jayashankar Padathdb124362021-01-28 21:12:34 -060020static constexpr auto resDumpProgressIntf =
21 "xyz.openbmc_project.Common.Progress";
22static constexpr auto resDumpStatus =
23 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed";
24
25DbusToFileHandler::DbusToFileHandler(
Andrew Jefferyfb8d1942024-07-25 23:12:36 +093026 int /* mctp_fd */, uint8_t mctp_eid, pldm::InstanceIdDb* instanceIdDb,
Sampa Misrac0c79482021-06-02 08:01:54 -050027 sdbusplus::message::object_path resDumpCurrentObjPath,
28 pldm::requester::Handler<pldm::requester::Request>* handler) :
Andrew Jefferyfb8d1942024-07-25 23:12:36 +093029 mctp_eid(mctp_eid),
30 instanceIdDb(instanceIdDb), resDumpCurrentObjPath(resDumpCurrentObjPath),
31 handler(handler)
Jayashankar Padathdb124362021-01-28 21:12:34 -060032{}
33
34void DbusToFileHandler::sendNewFileAvailableCmd(uint64_t fileSize)
35{
Andrew Jefferya330b2f2023-05-04 14:55:37 +093036 if (instanceIdDb == NULL)
Jayashankar Padathdb124362021-01-28 21:12:34 -060037 {
Riya Dixit49cfb132023-03-02 04:26:53 -060038 error(
Andrew Jefferya330b2f2023-05-04 14:55:37 +093039 "Failed to send resource dump parameters as instance ID DB is not set");
Jayashankar Padathdb124362021-01-28 21:12:34 -060040 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +053041 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Jayashankar Padathdb124362021-01-28 21:12:34 -060042 return;
43 }
Andrew Jefferya330b2f2023-05-04 14:55:37 +093044 auto instanceId = instanceIdDb->next(mctp_eid);
Jayashankar Padathdb124362021-01-28 21:12:34 -060045 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
Jayashankar Padath79612312022-06-13 12:47:05 -050046 PLDM_NEW_FILE_REQ_BYTES);
Jayashankar Padathdb124362021-01-28 21:12:34 -060047 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 Williams6da4f912023-05-10 07:50:53 -050051 auto rc = encode_new_file_req(instanceId,
52 PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS,
53 fileHandle, fileSize, request);
Jayashankar Padathdb124362021-01-28 21:12:34 -060054 if (rc != PLDM_SUCCESS)
55 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +093056 instanceIdDb->free(mctp_eid, instanceId);
Riya Dixitfc84f632024-04-06 14:00:02 -050057 error("Failed to encode new file request with response code '{RC}'",
58 "RC", rc);
Jayashankar Padathdb124362021-01-28 21:12:34 -060059 return;
60 }
61
Sampa Misrac0c79482021-06-02 08:01:54 -050062 auto newFileAvailableRespHandler = [this](mctp_eid_t /*eid*/,
63 const pldm_msg* response,
64 size_t respMsgLen) {
65 if (response == nullptr || !respMsgLen)
66 {
Riya Dixit49cfb132023-03-02 04:26:53 -060067 error("Failed to receive response for NewFileAvailable command");
Sampa Misrac0c79482021-06-02 08:01:54 -050068 return;
69 }
Jayashankar Padathdb124362021-01-28 21:12:34 -060070 uint8_t completionCode{};
Sampa Misrac0c79482021-06-02 08:01:54 -050071 auto rc = decode_new_file_resp(response, respMsgLen, &completionCode);
72 if (rc || completionCode)
Jayashankar Padathdb124362021-01-28 21:12:34 -060073 {
Riya Dixit49cfb132023-03-02 04:26:53 -060074 error(
Riya Dixitfc84f632024-04-06 14:00:02 -050075 "Failed to decode new file available response or remote terminus returned error, response code '{RC}' and completion code '{CC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -050076 "RC", rc, "CC", completionCode);
Sampa Misrac0c79482021-06-02 08:01:54 -050077 reportResourceDumpFailure();
Jayashankar Padathdb124362021-01-28 21:12:34 -060078 }
Sampa Misrac0c79482021-06-02 08:01:54 -050079 };
80 rc = handler->registerRequest(
81 mctp_eid, instanceId, PLDM_OEM, PLDM_NEW_FILE_AVAILABLE,
82 std::move(requestMsg), std::move(newFileAvailableRespHandler));
83 if (rc)
Jayashankar Padathdb124362021-01-28 21:12:34 -060084 {
Riya Dixitfc84f632024-04-06 14:00:02 -050085 error(
86 "Failed to send NewFileAvailable Request to Host, response code '{RC}'",
87 "RC", rc);
Sampa Misrac0c79482021-06-02 08:01:54 -050088 reportResourceDumpFailure();
89 }
90}
Jayashankar Padathdb124362021-01-28 21:12:34 -060091
Sampa Misrac0c79482021-06-02 08:01:54 -050092void DbusToFileHandler::reportResourceDumpFailure()
93{
Manojkiran Eda92fb0b52024-04-17 10:48:17 +053094 pldm::utils::reportError("xyz.openbmc_project.bmc.pldm.InternalFailure");
Sampa Misrac0c79482021-06-02 08:01:54 -050095
96 PropertyValue value{resDumpStatus};
97 DBusMapping dbusMapping{resDumpCurrentObjPath, resDumpProgressIntf,
98 "Status", "string"};
99 try
100 {
101 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
102 }
103 catch (const std::exception& e)
104 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500105 error("Failed to set resource dump operation status, error - {ERROR}",
106 "ERROR", e);
Jayashankar Padathdb124362021-01-28 21:12:34 -0600107 }
108}
109
110void DbusToFileHandler::processNewResourceDump(
111 const std::string& vspString, const std::string& resDumpReqPass)
112{
Jayashankar Padath99fa1862021-11-10 09:45:06 -0600113 try
114 {
115 std::string objPath = resDumpCurrentObjPath;
116 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
117 objPath.c_str(), "Status", resDumpProgressIntf);
118 const auto& curResDumpStatus = std::get<ResDumpStatus>(propVal);
119
120 if (curResDumpStatus !=
121 "xyz.openbmc_project.Common.Progress.OperationStatus.InProgress")
122 {
123 return;
124 }
125 }
126 catch (const sdbusplus::exception_t& e)
127 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600128 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500129 "Error '{ERROR}' found in getting current resource dump status while initiating a new resource dump with object path '{PATH}' and interface {INTERFACE}",
130 "ERROR", e, "PATH", resDumpCurrentObjPath, "INTERFACE",
Riya Dixit49cfb132023-03-02 04:26:53 -0600131 resDumpProgressIntf);
Jayashankar Padath99fa1862021-11-10 09:45:06 -0600132 }
133
Jayashankar Padathdb124362021-01-28 21:12:34 -0600134 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 Dixitfc84f632024-04-06 14:00:02 -0500151 error("Failed to open resource dump file '{PATH}'", "PATH",
Riya Dixit49cfb132023-03-02 04:26:53 -0600152 resDumpFilePath);
Jayashankar Padathdb124362021-01-28 21:12:34 -0600153 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 Dixit49cfb132023-03-02 04:26:53 -0600162 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500163 "Failed to set resource dump operation status, error - {ERROR}",
164 "ERROR", e);
Jayashankar Padathdb124362021-01-28 21:12:34 -0600165 }
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*)&paramSize, sizeof(paramSize));
173 fileHandle << paramBuf;
174 };
175 fileFunc(vspString);
176 fileFunc(resDumpReqPass);
177
Pavithra Barithayac047f802021-11-30 01:55:03 -0600178 std::string str;
179 if (!resDumpReqPass.empty())
180 {
181 str = getAcfFileContent();
182 }
183
184 fileFunc(str);
185
Jayashankar Padathdb124362021-01-28 21:12:34 -0600186 fileHandle.close();
187 size_t fileSize = fs::file_size(resDumpFilePath);
188
189 sendNewFileAvailableCmd(fileSize);
190}
191
Pavithra Barithayac047f802021-11-30 01:55:03 -0600192std::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 Kaverappa219ace92021-04-01 02:50:11 -0500208void 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 Dixitfc84f632024-04-06 14:00:02 -0500229 error("Failed to open certificate file '{PATH}'", "PATH", certFilePath);
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500230 return;
231 }
232
233 // Add csr to file
234 certFile << csr << std::endl;
235
236 certFile.close();
237 uint32_t fileSize = fs::file_size(certFilePath);
238
239 newFileAvailableSendToHost(fileSize, (uint32_t)stoi(fileHandle),
240 PLDM_FILE_TYPE_CERT_SIGNING_REQUEST);
241}
242
243void DbusToFileHandler::newFileAvailableSendToHost(const uint32_t fileSize,
244 const uint32_t fileHandle,
245 const uint16_t type)
246{
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930247 if (instanceIdDb == NULL)
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500248 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500249 error("Failed to send csr to remote terminus.");
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500250 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530251 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500252 return;
253 }
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930254 auto instanceId = instanceIdDb->next(mctp_eid);
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500255 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
256 PLDM_NEW_FILE_REQ_BYTES);
257 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
258
Patrick Williams6da4f912023-05-10 07:50:53 -0500259 auto rc = encode_new_file_req(instanceId, type, fileHandle, fileSize,
260 request);
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500261 if (rc != PLDM_SUCCESS)
262 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930263 instanceIdDb->free(mctp_eid, instanceId);
Riya Dixitfc84f632024-04-06 14:00:02 -0500264 error("Failed to encode new file request with response code '{RC}'",
265 "RC", rc);
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500266 return;
267 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500268 auto newFileAvailableRespHandler =
269 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Sampa Misrac0c79482021-06-02 08:01:54 -0500270 if (response == nullptr || !respMsgLen)
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500271 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600272 error(
273 "Failed to receive response for NewFileAvailable command for vmi");
Sampa Misrac0c79482021-06-02 08:01:54 -0500274 return;
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500275 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500276 uint8_t completionCode{};
277 auto rc = decode_new_file_resp(response, respMsgLen, &completionCode);
278 if (rc || completionCode)
279 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600280 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500281 "Failed to decode new file available response for vmi or remote terminus returned error, response code '{RC}' and completion code '{CC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500282 "RC", rc, "CC", completionCode);
Sampa Misrac0c79482021-06-02 08:01:54 -0500283 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530284 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Sampa Misrac0c79482021-06-02 08:01:54 -0500285 }
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 Kaverappa219ace92021-04-01 02:50:11 -0500291 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500292 error(
293 "Failed to send NewFileAvailable Request to Host for vmi, response code '{RC}'",
294 "RC", rc);
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500295 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530296 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500297 }
298}
299
Jayashankar Padathdb124362021-01-28 21:12:34 -0600300} // namespace oem_ibm
301} // namespace requester
302} // namespace pldm