blob: 263a2019c4f89e245922a4b9b74f5fc2f3156f3c [file] [log] [blame]
Sampa Misraaea5dde2020-08-31 08:33:47 -05001#include "oem_ibm_handler.hpp"
2
Sagar Srinivascfdbca72020-09-22 10:03:35 -05003#include "file_io_type_lid.hpp"
4#include "libpldmresponder/file_io.hpp"
Sagar Srinivas78a225a2020-08-27 00:52:20 -05005#include "libpldmresponder/pdr_utils.hpp"
Brad Bishop5079ac42021-08-19 18:35:06 -04006
George Liuc453e162022-12-21 17:16:23 +08007#include <libpldm/entity.h>
Andrew Jeffery21f128d2024-01-15 15:34:26 +10308#include <libpldm/oem/ibm/entity.h>
Brad Bishop27403f42021-12-01 21:37:44 -05009
Riya Dixit49cfb132023-03-02 04:26:53 -060010#include <phosphor-logging/lg2.hpp>
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -060011#include <xyz/openbmc_project/State/BMC/client.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -060012
13PHOSPHOR_LOG2_USING;
14
Brad Bishop5079ac42021-08-19 18:35:06 -040015using namespace pldm::pdr;
16using namespace pldm::utils;
17
Sampa Misraaea5dde2020-08-31 08:33:47 -050018namespace pldm
19{
Sampa Misraaea5dde2020-08-31 08:33:47 -050020namespace responder
21{
Sampa Misraaea5dde2020-08-31 08:33:47 -050022namespace oem_ibm_platform
23{
Sampa Misraaea5dde2020-08-31 08:33:47 -050024int pldm::responder::oem_ibm_platform::Handler::
25 getOemStateSensorReadingsHandler(
26 EntityType entityType, EntityInstance entityInstance,
27 StateSetId stateSetId, CompositeCount compSensorCnt,
28 std::vector<get_sensor_state_field>& stateField)
29{
30 int rc = PLDM_SUCCESS;
31 stateField.clear();
32
33 for (size_t i = 0; i < compSensorCnt; i++)
34 {
35 uint8_t sensorOpState{};
Sagar Srinivas78a225a2020-08-27 00:52:20 -050036 if (entityType == PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE &&
Sampa Misraaea5dde2020-08-31 08:33:47 -050037 stateSetId == PLDM_OEM_IBM_BOOT_STATE)
38 {
39 sensorOpState = fetchBootSide(entityInstance, codeUpdate);
40 }
41 else
42 {
43 rc = PLDM_PLATFORM_INVALID_STATE_VALUE;
44 break;
45 }
46 stateField.push_back({PLDM_SENSOR_ENABLED, PLDM_SENSOR_UNKNOWN,
47 PLDM_SENSOR_UNKNOWN, sensorOpState});
48 }
49 return rc;
50}
51
52int pldm::responder::oem_ibm_platform::Handler::
Sampa Misra3a0e3b92020-10-21 05:58:00 -050053 oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -050054 uint16_t entityType, uint16_t entityInstance, uint16_t stateSetId,
55 uint8_t compEffecterCnt,
56 std::vector<set_effecter_state_field>& stateField,
57 uint16_t /*effecterId*/)
Sampa Misraaea5dde2020-08-31 08:33:47 -050058{
59 int rc = PLDM_SUCCESS;
60
61 for (uint8_t currState = 0; currState < compEffecterCnt; ++currState)
62 {
63 if (stateField[currState].set_request == PLDM_REQUEST_SET)
64 {
Sagar Srinivas78a225a2020-08-27 00:52:20 -050065 if (entityType == PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE &&
Sampa Misraaea5dde2020-08-31 08:33:47 -050066 stateSetId == PLDM_OEM_IBM_BOOT_STATE)
67 {
68 rc = setBootSide(entityInstance, currState, stateField,
69 codeUpdate);
70 }
Sagar Srinivascfdbca72020-09-22 10:03:35 -050071 else if (entityType == PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE &&
72 stateSetId == PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE)
73 {
74 if (stateField[currState].effecter_state ==
75 uint8_t(CodeUpdateState::START))
76 {
77 codeUpdate->setCodeUpdateProgress(true);
Sampa Misra3a0e3b92020-10-21 05:58:00 -050078 startUpdateEvent =
79 std::make_unique<sdeventplus::source::Defer>(
80 event,
81 std::bind(std::mem_fn(&oem_ibm_platform::Handler::
82 _processStartUpdate),
83 this, std::placeholders::_1));
Sagar Srinivascfdbca72020-09-22 10:03:35 -050084 }
85 else if (stateField[currState].effecter_state ==
86 uint8_t(CodeUpdateState::END))
87 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -050088 rc = PLDM_SUCCESS;
89 assembleImageEvent = std::make_unique<
90 sdeventplus::source::Defer>(
91 event,
92 std::bind(
93 std::mem_fn(
94 &oem_ibm_platform::Handler::_processEndUpdate),
95 this, std::placeholders::_1));
96
97 // sendCodeUpdateEvent(effecterId, END, START);
Sagar Srinivascfdbca72020-09-22 10:03:35 -050098 }
99 else if (stateField[currState].effecter_state ==
100 uint8_t(CodeUpdateState::ABORT))
101 {
102 codeUpdate->setCodeUpdateProgress(false);
103 codeUpdate->clearDirPath(LID_STAGING_DIR);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500104 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
105 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
106 uint8_t(CodeUpdateState::ABORT),
107 uint8_t(CodeUpdateState::START));
108 // sendCodeUpdateEvent(effecterId, ABORT, END);
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500109 }
110 else if (stateField[currState].effecter_state ==
111 uint8_t(CodeUpdateState::ACCEPT))
112 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500113 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
114 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
115 uint8_t(CodeUpdateState::ACCEPT),
116 uint8_t(CodeUpdateState::END));
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500117 // TODO Set new Dbus property provided by code update app
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500118 // sendCodeUpdateEvent(effecterId, ACCEPT, END);
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500119 }
120 else if (stateField[currState].effecter_state ==
121 uint8_t(CodeUpdateState::REJECT))
122 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500123 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
124 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
125 uint8_t(CodeUpdateState::REJECT),
126 uint8_t(CodeUpdateState::END));
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500127 // TODO Set new Dbus property provided by code update app
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500128 // sendCodeUpdateEvent(effecterId, REJECT, END);
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500129 }
130 }
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600131 else if (entityType == PLDM_ENTITY_SYSTEM_CHASSIS &&
132 stateSetId == PLDM_OEM_IBM_SYSTEM_POWER_STATE)
133 {
134 if (stateField[currState].effecter_state == POWER_CYCLE_HARD)
135 {
136 systemRebootEvent =
137 std::make_unique<sdeventplus::source::Defer>(
138 event,
139 std::bind(std::mem_fn(&oem_ibm_platform::Handler::
140 _processSystemReboot),
141 this, std::placeholders::_1));
142 }
143 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500144 else
145 {
146 rc = PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE;
147 }
148 }
149 if (rc != PLDM_SUCCESS)
150 {
151 break;
152 }
153 }
154 return rc;
155}
156
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530157void buildAllCodeUpdateEffecterPDR(oem_ibm_platform::Handler* platformHandler,
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600158 uint16_t entityType, uint16_t entityInstance,
159 uint16_t stateSetID, pdr_utils::Repo& repo)
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500160{
161 size_t pdrSize = 0;
162 pdrSize = sizeof(pldm_state_effecter_pdr) +
163 sizeof(state_effecter_possible_states);
164 std::vector<uint8_t> entry{};
165 entry.resize(pdrSize);
166 pldm_state_effecter_pdr* pdr =
167 reinterpret_cast<pldm_state_effecter_pdr*>(entry.data());
168 if (!pdr)
169 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500170 error("Failed to get record by PDR type, error - {ERROR}", "ERROR",
Riya Dixit49cfb132023-03-02 04:26:53 -0600171 lg2::hex,
172 static_cast<unsigned>(PLDM_PLATFORM_INVALID_EFFECTER_ID));
Manojkiran Edabcf91ac2021-03-14 13:50:48 +0530173 return;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500174 }
175 pdr->hdr.record_handle = 0;
176 pdr->hdr.version = 1;
177 pdr->hdr.type = PLDM_STATE_EFFECTER_PDR;
178 pdr->hdr.record_change_num = 0;
179 pdr->hdr.length = sizeof(pldm_state_effecter_pdr) - sizeof(pldm_pdr_hdr);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530180 pdr->terminus_handle = TERMINUS_HANDLE;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500181 pdr->effecter_id = platformHandler->getNextEffecterId();
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600182 pdr->entity_type = entityType;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500183 pdr->entity_instance = entityInstance;
Sagar Srinivas97296e02021-08-26 09:40:41 -0500184 pdr->container_id = 1;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500185 pdr->effecter_semantic_id = 0;
186 pdr->effecter_init = PLDM_NO_INIT;
187 pdr->has_description_pdr = false;
188 pdr->composite_effecter_count = 1;
189
190 auto* possibleStatesPtr = pdr->possible_states;
191 auto possibleStates =
192 reinterpret_cast<state_effecter_possible_states*>(possibleStatesPtr);
193 possibleStates->state_set_id = stateSetID;
194 possibleStates->possible_states_size = 2;
195 auto state =
196 reinterpret_cast<state_effecter_possible_states*>(possibleStates);
197 if (stateSetID == PLDM_OEM_IBM_BOOT_STATE)
198 state->states[0].byte = 6;
199 else if (stateSetID == PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE)
200 state->states[0].byte = 126;
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600201 else if (stateSetID == PLDM_OEM_IBM_SYSTEM_POWER_STATE)
202 state->states[0].byte = 2;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500203 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
204 pdrEntry.data = entry.data();
205 pdrEntry.size = pdrSize;
206 repo.addRecord(pdrEntry);
207}
208
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530209void buildAllCodeUpdateSensorPDR(oem_ibm_platform::Handler* platformHandler,
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500210 uint16_t entityType, uint16_t entityInstance,
211 uint16_t stateSetID, pdr_utils::Repo& repo)
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500212{
213 size_t pdrSize = 0;
Patrick Williams6da4f912023-05-10 07:50:53 -0500214 pdrSize = sizeof(pldm_state_sensor_pdr) +
215 sizeof(state_sensor_possible_states);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500216 std::vector<uint8_t> entry{};
217 entry.resize(pdrSize);
218 pldm_state_sensor_pdr* pdr =
219 reinterpret_cast<pldm_state_sensor_pdr*>(entry.data());
220 if (!pdr)
221 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500222 error("Failed to get record by PDR type, error - {ERROR}", "ERROR",
Riya Dixit49cfb132023-03-02 04:26:53 -0600223 lg2::hex, static_cast<unsigned>(PLDM_PLATFORM_INVALID_SENSOR_ID));
Manojkiran Edabcf91ac2021-03-14 13:50:48 +0530224 return;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500225 }
226 pdr->hdr.record_handle = 0;
227 pdr->hdr.version = 1;
228 pdr->hdr.type = PLDM_STATE_SENSOR_PDR;
229 pdr->hdr.record_change_num = 0;
230 pdr->hdr.length = sizeof(pldm_state_sensor_pdr) - sizeof(pldm_pdr_hdr);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530231 pdr->terminus_handle = TERMINUS_HANDLE;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500232 pdr->sensor_id = platformHandler->getNextSensorId();
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500233 pdr->entity_type = entityType;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500234 pdr->entity_instance = entityInstance;
Sagar Srinivas97296e02021-08-26 09:40:41 -0500235 pdr->container_id = 1;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500236 pdr->sensor_init = PLDM_NO_INIT;
237 pdr->sensor_auxiliary_names_pdr = false;
238 pdr->composite_sensor_count = 1;
239
240 auto* possibleStatesPtr = pdr->possible_states;
241 auto possibleStates =
242 reinterpret_cast<state_sensor_possible_states*>(possibleStatesPtr);
243 possibleStates->state_set_id = stateSetID;
244 possibleStates->possible_states_size = 2;
245 auto state =
246 reinterpret_cast<state_sensor_possible_states*>(possibleStates);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500247 if ((stateSetID == PLDM_OEM_IBM_BOOT_STATE) ||
Christian Geddes7f9523c2021-08-03 13:44:38 -0500248 (stateSetID == PLDM_OEM_IBM_VERIFICATION_STATE))
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500249 state->states[0].byte = 6;
250 else if (stateSetID == PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE)
251 state->states[0].byte = 126;
252 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
253 pdrEntry.data = entry.data();
254 pdrEntry.size = pdrSize;
255 repo.addRecord(pdrEntry);
256}
257
258void pldm::responder::oem_ibm_platform::Handler::buildOEMPDR(
259 pdr_utils::Repo& repo)
260{
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530261 buildAllCodeUpdateEffecterPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
262 ENTITY_INSTANCE_0, PLDM_OEM_IBM_BOOT_STATE,
263 repo);
264 buildAllCodeUpdateEffecterPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
265 ENTITY_INSTANCE_1, PLDM_OEM_IBM_BOOT_STATE,
266 repo);
267 buildAllCodeUpdateEffecterPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
268 ENTITY_INSTANCE_0,
269 PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE, repo);
270 buildAllCodeUpdateEffecterPDR(this, PLDM_ENTITY_SYSTEM_CHASSIS,
Sagar Srinivas1b8d35f2021-11-10 10:54:40 -0600271 ENTITY_INSTANCE_1,
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600272 PLDM_OEM_IBM_SYSTEM_POWER_STATE, repo);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500273
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530274 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
275 ENTITY_INSTANCE_0, PLDM_OEM_IBM_BOOT_STATE,
276 repo);
277 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
278 ENTITY_INSTANCE_1, PLDM_OEM_IBM_BOOT_STATE,
279 repo);
280 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
281 ENTITY_INSTANCE_0,
282 PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE, repo);
283 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
284 ENTITY_INSTANCE_0,
285 PLDM_OEM_IBM_VERIFICATION_STATE, repo);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500286 auto sensorId = findStateSensorId(
287 repo.getPdr(), 0, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
Sagar Srinivasf4a7dd72021-10-20 04:45:37 -0500288 ENTITY_INSTANCE_0, 1, PLDM_OEM_IBM_VERIFICATION_STATE);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500289 codeUpdate->setMarkerLidSensor(sensorId);
290 sensorId = findStateSensorId(
291 repo.getPdr(), 0, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
Sagar Srinivasf4a7dd72021-10-20 04:45:37 -0500292 ENTITY_INSTANCE_0, 1, PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500293 codeUpdate->setFirmwareUpdateSensor(sensorId);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500294}
295
Sampa Misraaea5dde2020-08-31 08:33:47 -0500296void pldm::responder::oem_ibm_platform::Handler::setPlatformHandler(
297 pldm::responder::platform::Handler* handler)
298{
299 platformHandler = handler;
300}
301
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500302int pldm::responder::oem_ibm_platform::Handler::sendEventToHost(
Sampa Misrac0c79482021-06-02 08:01:54 -0500303 std::vector<uint8_t>& requestMsg, uint8_t instanceId)
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500304{
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500305 if (requestMsg.size())
306 {
307 std::ostringstream tempStream;
308 for (int byte : requestMsg)
309 {
310 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
311 << " ";
312 }
313 std::cout << tempStream.str() << std::endl;
314 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500315 auto oemPlatformEventMessageResponseHandler =
316 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Riya Dixit49cfb132023-03-02 04:26:53 -0600317 uint8_t completionCode{};
318 uint8_t status{};
319 auto rc = decode_platform_event_message_resp(response, respMsgLen,
320 &completionCode, &status);
321 if (rc || completionCode)
322 {
323 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500324 "Failed to decode platform event message response for code update event with response code '{RC}' and completion code '{CC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500325 "RC", rc, "CC", completionCode);
Riya Dixit49cfb132023-03-02 04:26:53 -0600326 }
327 };
Sampa Misrac0c79482021-06-02 08:01:54 -0500328 auto rc = handler->registerRequest(
329 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
330 std::move(requestMsg),
331 std::move(oemPlatformEventMessageResponseHandler));
332 if (rc)
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500333 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600334 error("Failed to send BIOS attribute change event message ");
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500335 }
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500336
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500337 return rc;
338}
339
340int encodeEventMsg(uint8_t eventType, const std::vector<uint8_t>& eventDataVec,
341 std::vector<uint8_t>& requestMsg, uint8_t instanceId)
342{
343 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
344
345 auto rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -0600346 instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType,
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500347 eventDataVec.data(), eventDataVec.size(), request,
348 eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500349
350 return rc;
351}
352
353void pldm::responder::oem_ibm_platform::Handler::sendStateSensorEvent(
354 uint16_t sensorId, enum sensor_event_class_states sensorEventClass,
355 uint8_t sensorOffset, uint8_t eventState, uint8_t prevEventState)
356{
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500357 std::vector<uint8_t> sensorEventDataVec{};
358 size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
359 sensorEventDataVec.resize(sensorEventSize);
360 auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
361 sensorEventDataVec.data());
362 eventData->sensor_id = sensorId;
363 eventData->sensor_event_class_type = sensorEventClass;
364 auto eventClassStart = eventData->event_class;
365 auto eventClass =
366 reinterpret_cast<struct pldm_sensor_event_state_sensor_state*>(
367 eventClassStart);
368 eventClass->sensor_offset = sensorOffset;
369 eventClass->event_state = eventState;
370 eventClass->previous_event_state = prevEventState;
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930371 auto instanceId = instanceIdDb.next(mctp_eid);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500372 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
373 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
374 sensorEventDataVec.size());
375 auto rc = encodeEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec, requestMsg,
376 instanceId);
377 if (rc != PLDM_SUCCESS)
378 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500379 error("Failed to encode state sensor event with response code '{RC}'",
380 "RC", rc);
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930381 instanceIdDb.free(mctp_eid, instanceId);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500382 return;
383 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500384 rc = sendEventToHost(requestMsg, instanceId);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500385 if (rc != PLDM_SUCCESS)
386 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500387 error(
388 "Failed to send event to remote terminus with response code '{RC}'",
389 "RC", rc);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500390 }
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500391 return;
392}
393
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500394void pldm::responder::oem_ibm_platform::Handler::_processEndUpdate(
395 sdeventplus::source::EventBase& /*source */)
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500396{
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500397 assembleImageEvent.reset();
Adriana Kobylak9296f242021-09-22 15:52:00 +0000398 int retc = codeUpdate->assembleCodeUpdateImage();
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500399 if (retc != PLDM_SUCCESS)
400 {
401 codeUpdate->setCodeUpdateProgress(false);
402 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
403 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
404 uint8_t(CodeUpdateState::FAIL),
405 uint8_t(CodeUpdateState::START));
406 }
407}
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500408
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500409void pldm::responder::oem_ibm_platform::Handler::_processStartUpdate(
410 sdeventplus::source::EventBase& /*source */)
411{
412 codeUpdate->deleteImage();
413 CodeUpdateState state = CodeUpdateState::START;
414 auto rc = codeUpdate->setRequestedApplyTime();
415 if (rc != PLDM_SUCCESS)
416 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600417 error("setRequestedApplyTime failed");
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500418 state = CodeUpdateState::FAIL;
419 }
420 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
421 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, uint8_t(state),
422 uint8_t(CodeUpdateState::END));
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500423}
424
Kamalkumar Patel15ce5a12024-05-07 11:45:11 -0500425void pldm::responder::oem_ibm_platform::Handler::updateOemDbusPaths(
426 std::string& dbusPath)
427{
428 std::string toFind("system1/chassis1/motherboard1");
429 if (dbusPath.find(toFind) != std::string::npos)
430 {
431 size_t pos = dbusPath.find(toFind);
432 dbusPath.replace(pos, toFind.length(), "system/chassis/motherboard");
433 }
434 toFind = "system1";
435 if (dbusPath.find(toFind) != std::string::npos)
436 {
437 size_t pos = dbusPath.find(toFind);
438 dbusPath.replace(pos, toFind.length(), "system");
439 }
440 /* below logic to replace path 'motherboard/socket/chassis' to
441 'motherboard/chassis' or 'motherboard/socket123/chassis' to
442 'motherboard/chassis' */
443 toFind = "socket";
444 size_t pos1 = dbusPath.find(toFind);
445 // while loop to detect multiple substring 'socket' in the path
446 while (pos1 != std::string::npos)
447 {
448 size_t pos2 = dbusPath.substr(pos1 + 1).find('/') + 1;
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530449 // Replacing starting from substring to next occurrence of char '/'
Kamalkumar Patel15ce5a12024-05-07 11:45:11 -0500450 dbusPath.replace(pos1, pos2 + 1, "");
451 pos1 = dbusPath.find(toFind);
452 }
453}
454
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600455void pldm::responder::oem_ibm_platform::Handler::_processSystemReboot(
456 sdeventplus::source::EventBase& /*source */)
457{
458 pldm::utils::PropertyValue value =
459 "xyz.openbmc_project.State.Chassis.Transition.Off";
460 pldm::utils::DBusMapping dbusMapping{"/xyz/openbmc_project/state/chassis0",
461 "xyz.openbmc_project.State.Chassis",
462 "RequestedPowerTransition", "string"};
463 try
464 {
465 dBusIntf->setDbusProperty(dbusMapping, value);
466 }
467 catch (const std::exception& e)
468 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600469 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500470 "Failure in chassis State transition to Off, unable to set property RequestedPowerTransition, error - {ERROR}",
471 "ERROR", e);
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600472 }
473
474 using namespace sdbusplus::bus::match::rules;
Patrick Williams84b790c2022-07-22 19:26:56 -0500475 chassisOffMatch = std::make_unique<sdbusplus::bus::match_t>(
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600476 pldm::utils::DBusHandler::getBus(),
477 propertiesChanged("/xyz/openbmc_project/state/chassis0",
478 "xyz.openbmc_project.State.Chassis"),
Patrick Williams84b790c2022-07-22 19:26:56 -0500479 [this](sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500480 DbusChangedProps props{};
481 std::string intf;
482 msg.read(intf, props);
483 const auto itr = props.find("CurrentPowerState");
484 if (itr != props.end())
485 {
486 PropertyValue value = itr->second;
487 auto propVal = std::get<std::string>(value);
488 if (propVal == "xyz.openbmc_project.State.Chassis.PowerState.Off")
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600489 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500490 pldm::utils::DBusMapping dbusMapping{
491 "/xyz/openbmc_project/control/host0/"
492 "power_restore_policy/one_time",
493 "xyz.openbmc_project.Control.Power.RestorePolicy",
494 "PowerRestorePolicy", "string"};
495 value = "xyz.openbmc_project.Control.Power.RestorePolicy."
496 "Policy.AlwaysOn";
497 try
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600498 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500499 dBusIntf->setDbusProperty(dbusMapping, value);
500 }
501 catch (const std::exception& e)
502 {
503 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500504 "Failure in setting one-time restore policy, unable to set property PowerRestorePolicy, error - {ERROR}",
505 "ERROR", e);
Patrick Williams6da4f912023-05-10 07:50:53 -0500506 }
507 dbusMapping = pldm::utils::DBusMapping{
508 "/xyz/openbmc_project/state/bmc0",
509 "xyz.openbmc_project.State.BMC", "RequestedBMCTransition",
510 "string"};
511 value = "xyz.openbmc_project.State.BMC.Transition.Reboot";
512 try
513 {
514 dBusIntf->setDbusProperty(dbusMapping, value);
515 }
516 catch (const std::exception& e)
517 {
518 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500519 "Failure in BMC state transition to reboot, unable to set property RequestedBMCTransition , error - {ERROR}",
520 "ERROR", e);
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600521 }
522 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500523 }
Patrick Williamsa6756622023-10-20 11:19:15 -0500524 });
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600525}
Sagar Srinivas79669c92021-04-28 15:43:30 -0500526
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500527void pldm::responder::oem_ibm_platform::Handler::checkAndDisableWatchDog()
528{
529 if (!hostOff && setEventReceiverCnt == SET_EVENT_RECEIVER_SENT)
530 {
531 disableWatchDogTimer();
532 }
533
534 return;
535}
536
Sagar Srinivas79669c92021-04-28 15:43:30 -0500537bool pldm::responder::oem_ibm_platform::Handler::watchDogRunning()
538{
539 static constexpr auto watchDogObjectPath =
540 "/xyz/openbmc_project/watchdog/host0";
541 static constexpr auto watchDogEnablePropName = "Enabled";
542 static constexpr auto watchDogInterface =
543 "xyz.openbmc_project.State.Watchdog";
544 bool isWatchDogRunning = false;
545 try
546 {
547 isWatchDogRunning = pldm::utils::DBusHandler().getDbusProperty<bool>(
548 watchDogObjectPath, watchDogEnablePropName, watchDogInterface);
549 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500550 catch (const std::exception&)
Sagar Srinivas79669c92021-04-28 15:43:30 -0500551 {
552 return false;
553 }
554 return isWatchDogRunning;
555}
556
557void pldm::responder::oem_ibm_platform::Handler::resetWatchDogTimer()
558{
559 static constexpr auto watchDogService = "xyz.openbmc_project.Watchdog";
560 static constexpr auto watchDogObjectPath =
561 "/xyz/openbmc_project/watchdog/host0";
562 static constexpr auto watchDogInterface =
563 "xyz.openbmc_project.State.Watchdog";
564 static constexpr auto watchDogResetPropName = "ResetTimeRemaining";
565
566 bool wdStatus = watchDogRunning();
567 if (wdStatus == false)
568 {
569 return;
570 }
571 try
572 {
573 auto& bus = pldm::utils::DBusHandler::getBus();
574 auto resetMethod =
575 bus.new_method_call(watchDogService, watchDogObjectPath,
576 watchDogInterface, watchDogResetPropName);
577 resetMethod.append(true);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000578 bus.call_noreply(resetMethod, dbusTimeout);
Sagar Srinivas79669c92021-04-28 15:43:30 -0500579 }
580 catch (const std::exception& e)
581 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500582 error("Failed to reset watchdog timer, error - {ERROR}", "ERROR", e);
Sagar Srinivas79669c92021-04-28 15:43:30 -0500583 return;
584 }
585}
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500586
587void pldm::responder::oem_ibm_platform::Handler::disableWatchDogTimer()
588{
589 setEventReceiverCnt = 0;
590 pldm::utils::DBusMapping dbusMapping{"/xyz/openbmc_project/watchdog/host0",
591 "xyz.openbmc_project.State.Watchdog",
592 "Enabled", "bool"};
593 bool wdStatus = watchDogRunning();
594
595 if (!wdStatus)
596 {
597 return;
598 }
599 try
600 {
601 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, false);
602 }
603 catch (const std::exception& e)
604 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500605 error("Failed to disable watchdog timer, error - {ERROR}", "ERROR", e);
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500606 }
607}
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500608int pldm::responder::oem_ibm_platform::Handler::checkBMCState()
609{
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600610 using BMC = sdbusplus::client::xyz::openbmc_project::state::BMC<>;
611 auto bmcPath = sdbusplus::message::object_path(BMC::namespace_path::value) /
612 BMC::namespace_path::bmc;
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500613 try
614 {
615 pldm::utils::PropertyValue propertyValue =
616 pldm::utils::DBusHandler().getDbusPropertyVariant(
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600617 bmcPath.str.c_str(), "CurrentBMCState", BMC::interface);
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500618
619 if (std::get<std::string>(propertyValue) ==
620 "xyz.openbmc_project.State.BMC.BMCState.NotReady")
621 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600622 error("GetPDR : PLDM stack is not ready for PDR exchange");
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500623 return PLDM_ERROR_NOT_READY;
624 }
625 }
626 catch (const std::exception& e)
627 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500628 error("Error getting the current BMC state, error - {ERROR}", "ERROR",
629 e);
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500630 return PLDM_ERROR;
631 }
632 return PLDM_SUCCESS;
633}
634
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500635const pldm_pdr_record*
636 pldm::responder::oem_ibm_platform::Handler::fetchLastBMCRecord(
637 const pldm_pdr* repo)
638{
639 return pldm_pdr_find_last_in_range(repo, BMC_PDR_START_RANGE,
640 BMC_PDR_END_RANGE);
641}
642
643bool pldm::responder::oem_ibm_platform::Handler::checkRecordHandleInRange(
644 const uint32_t& record_handle)
645{
646 return record_handle >= HOST_PDR_START_RANGE &&
647 record_handle <= HOST_PDR_END_RANGE;
648}
649
Sagar Srinivas90314a32023-10-17 10:38:03 -0500650void Handler::processSetEventReceiver()
651{
652 this->setEventReceiver();
653}
654
Sagar Srinivas18145f72022-04-11 07:38:26 -0500655void pldm::responder::oem_ibm_platform::Handler::startStopTimer(bool value)
656{
657 if (value)
658 {
659 timer.restart(
660 std::chrono::seconds(HEARTBEAT_TIMEOUT + HEARTBEAT_TIMEOUT_DELTA));
661 }
662 else
663 {
664 timer.setEnabled(value);
665 }
666}
667
668void pldm::responder::oem_ibm_platform::Handler::setSurvTimer(uint8_t tid,
669 bool value)
670{
671 if ((hostOff || hostTransitioningToOff || (tid != HYPERVISOR_TID)) &&
672 timer.isEnabled())
673 {
674 startStopTimer(false);
675 return;
676 }
677 if (value)
678 {
679 startStopTimer(value);
680 }
681 else if (timer.isEnabled())
682 {
683 info(
684 "Failed to stop surveillance timer while remote terminus status is ‘{HOST_TRANST_OFF}’ with Terminus ID ‘{TID}’ ",
685 "HOST_TRANST_OFF", hostTransitioningToOff, "TID", tid);
686 startStopTimer(value);
687 pldm::utils::reportError(
688 "xyz.openbmc_project.PLDM.Error.setSurvTimer.RecvSurveillancePingFail");
689 }
690}
691
Sampa Misraaea5dde2020-08-31 08:33:47 -0500692} // namespace oem_ibm_platform
Sampa Misraaea5dde2020-08-31 08:33:47 -0500693} // namespace responder
Sampa Misraaea5dde2020-08-31 08:33:47 -0500694} // namespace pldm