blob: 98903a4f7165a3717434259bdfc9cadcf96817cf [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>
Brad Bishop27403f42021-12-01 21:37:44 -05008#include <libpldm/entity_oem_ibm.h>
9
Riya Dixit49cfb132023-03-02 04:26:53 -060010#include <phosphor-logging/lg2.hpp>
11
12PHOSPHOR_LOG2_USING;
13
Brad Bishop5079ac42021-08-19 18:35:06 -040014using namespace pldm::pdr;
15using namespace pldm::utils;
16
Sampa Misraaea5dde2020-08-31 08:33:47 -050017namespace pldm
18{
Sampa Misraaea5dde2020-08-31 08:33:47 -050019namespace responder
20{
Sampa Misraaea5dde2020-08-31 08:33:47 -050021namespace oem_ibm_platform
22{
Sampa Misraaea5dde2020-08-31 08:33:47 -050023int pldm::responder::oem_ibm_platform::Handler::
24 getOemStateSensorReadingsHandler(
25 EntityType entityType, EntityInstance entityInstance,
26 StateSetId stateSetId, CompositeCount compSensorCnt,
27 std::vector<get_sensor_state_field>& stateField)
28{
29 int rc = PLDM_SUCCESS;
30 stateField.clear();
31
32 for (size_t i = 0; i < compSensorCnt; i++)
33 {
34 uint8_t sensorOpState{};
Sagar Srinivas78a225a2020-08-27 00:52:20 -050035 if (entityType == PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE &&
Sampa Misraaea5dde2020-08-31 08:33:47 -050036 stateSetId == PLDM_OEM_IBM_BOOT_STATE)
37 {
38 sensorOpState = fetchBootSide(entityInstance, codeUpdate);
39 }
40 else
41 {
42 rc = PLDM_PLATFORM_INVALID_STATE_VALUE;
43 break;
44 }
45 stateField.push_back({PLDM_SENSOR_ENABLED, PLDM_SENSOR_UNKNOWN,
46 PLDM_SENSOR_UNKNOWN, sensorOpState});
47 }
48 return rc;
49}
50
51int pldm::responder::oem_ibm_platform::Handler::
Sampa Misra3a0e3b92020-10-21 05:58:00 -050052 oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -050053 uint16_t entityType, uint16_t entityInstance, uint16_t stateSetId,
54 uint8_t compEffecterCnt,
55 std::vector<set_effecter_state_field>& stateField,
56 uint16_t /*effecterId*/)
Sampa Misraaea5dde2020-08-31 08:33:47 -050057{
58 int rc = PLDM_SUCCESS;
59
60 for (uint8_t currState = 0; currState < compEffecterCnt; ++currState)
61 {
62 if (stateField[currState].set_request == PLDM_REQUEST_SET)
63 {
Sagar Srinivas78a225a2020-08-27 00:52:20 -050064 if (entityType == PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE &&
Sampa Misraaea5dde2020-08-31 08:33:47 -050065 stateSetId == PLDM_OEM_IBM_BOOT_STATE)
66 {
67 rc = setBootSide(entityInstance, currState, stateField,
68 codeUpdate);
69 }
Sagar Srinivascfdbca72020-09-22 10:03:35 -050070 else if (entityType == PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE &&
71 stateSetId == PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE)
72 {
73 if (stateField[currState].effecter_state ==
74 uint8_t(CodeUpdateState::START))
75 {
76 codeUpdate->setCodeUpdateProgress(true);
Sampa Misra3a0e3b92020-10-21 05:58:00 -050077 startUpdateEvent =
78 std::make_unique<sdeventplus::source::Defer>(
79 event,
80 std::bind(std::mem_fn(&oem_ibm_platform::Handler::
81 _processStartUpdate),
82 this, std::placeholders::_1));
Sagar Srinivascfdbca72020-09-22 10:03:35 -050083 }
84 else if (stateField[currState].effecter_state ==
85 uint8_t(CodeUpdateState::END))
86 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -050087 rc = PLDM_SUCCESS;
88 assembleImageEvent = std::make_unique<
89 sdeventplus::source::Defer>(
90 event,
91 std::bind(
92 std::mem_fn(
93 &oem_ibm_platform::Handler::_processEndUpdate),
94 this, std::placeholders::_1));
95
96 // sendCodeUpdateEvent(effecterId, END, START);
Sagar Srinivascfdbca72020-09-22 10:03:35 -050097 }
98 else if (stateField[currState].effecter_state ==
99 uint8_t(CodeUpdateState::ABORT))
100 {
101 codeUpdate->setCodeUpdateProgress(false);
102 codeUpdate->clearDirPath(LID_STAGING_DIR);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500103 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
104 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
105 uint8_t(CodeUpdateState::ABORT),
106 uint8_t(CodeUpdateState::START));
107 // sendCodeUpdateEvent(effecterId, ABORT, END);
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500108 }
109 else if (stateField[currState].effecter_state ==
110 uint8_t(CodeUpdateState::ACCEPT))
111 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500112 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
113 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
114 uint8_t(CodeUpdateState::ACCEPT),
115 uint8_t(CodeUpdateState::END));
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500116 // TODO Set new Dbus property provided by code update app
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500117 // sendCodeUpdateEvent(effecterId, ACCEPT, END);
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500118 }
119 else if (stateField[currState].effecter_state ==
120 uint8_t(CodeUpdateState::REJECT))
121 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500122 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
123 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
124 uint8_t(CodeUpdateState::REJECT),
125 uint8_t(CodeUpdateState::END));
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500126 // TODO Set new Dbus property provided by code update app
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500127 // sendCodeUpdateEvent(effecterId, REJECT, END);
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500128 }
129 }
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600130 else if (entityType == PLDM_ENTITY_SYSTEM_CHASSIS &&
131 stateSetId == PLDM_OEM_IBM_SYSTEM_POWER_STATE)
132 {
133 if (stateField[currState].effecter_state == POWER_CYCLE_HARD)
134 {
135 systemRebootEvent =
136 std::make_unique<sdeventplus::source::Defer>(
137 event,
138 std::bind(std::mem_fn(&oem_ibm_platform::Handler::
139 _processSystemReboot),
140 this, std::placeholders::_1));
141 }
142 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500143 else
144 {
145 rc = PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE;
146 }
147 }
148 if (rc != PLDM_SUCCESS)
149 {
150 break;
151 }
152 }
153 return rc;
154}
155
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530156void buildAllCodeUpdateEffecterPDR(oem_ibm_platform::Handler* platformHandler,
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600157 uint16_t entityType, uint16_t entityInstance,
158 uint16_t stateSetID, pdr_utils::Repo& repo)
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500159{
160 size_t pdrSize = 0;
161 pdrSize = sizeof(pldm_state_effecter_pdr) +
162 sizeof(state_effecter_possible_states);
163 std::vector<uint8_t> entry{};
164 entry.resize(pdrSize);
165 pldm_state_effecter_pdr* pdr =
166 reinterpret_cast<pldm_state_effecter_pdr*>(entry.data());
167 if (!pdr)
168 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600169 error("Failed to get record by PDR type, ERROR:{ERR_CODE}", "ERR_CODE",
170 lg2::hex,
171 static_cast<unsigned>(PLDM_PLATFORM_INVALID_EFFECTER_ID));
Manojkiran Edabcf91ac2021-03-14 13:50:48 +0530172 return;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500173 }
174 pdr->hdr.record_handle = 0;
175 pdr->hdr.version = 1;
176 pdr->hdr.type = PLDM_STATE_EFFECTER_PDR;
177 pdr->hdr.record_change_num = 0;
178 pdr->hdr.length = sizeof(pldm_state_effecter_pdr) - sizeof(pldm_pdr_hdr);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530179 pdr->terminus_handle = TERMINUS_HANDLE;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500180 pdr->effecter_id = platformHandler->getNextEffecterId();
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600181 pdr->entity_type = entityType;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500182 pdr->entity_instance = entityInstance;
Sagar Srinivas97296e02021-08-26 09:40:41 -0500183 pdr->container_id = 1;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500184 pdr->effecter_semantic_id = 0;
185 pdr->effecter_init = PLDM_NO_INIT;
186 pdr->has_description_pdr = false;
187 pdr->composite_effecter_count = 1;
188
189 auto* possibleStatesPtr = pdr->possible_states;
190 auto possibleStates =
191 reinterpret_cast<state_effecter_possible_states*>(possibleStatesPtr);
192 possibleStates->state_set_id = stateSetID;
193 possibleStates->possible_states_size = 2;
194 auto state =
195 reinterpret_cast<state_effecter_possible_states*>(possibleStates);
196 if (stateSetID == PLDM_OEM_IBM_BOOT_STATE)
197 state->states[0].byte = 6;
198 else if (stateSetID == PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE)
199 state->states[0].byte = 126;
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600200 else if (stateSetID == PLDM_OEM_IBM_SYSTEM_POWER_STATE)
201 state->states[0].byte = 2;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500202 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
203 pdrEntry.data = entry.data();
204 pdrEntry.size = pdrSize;
205 repo.addRecord(pdrEntry);
206}
207
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530208void buildAllCodeUpdateSensorPDR(oem_ibm_platform::Handler* platformHandler,
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500209 uint16_t entityType, uint16_t entityInstance,
210 uint16_t stateSetID, pdr_utils::Repo& repo)
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500211{
212 size_t pdrSize = 0;
Patrick Williams6da4f912023-05-10 07:50:53 -0500213 pdrSize = sizeof(pldm_state_sensor_pdr) +
214 sizeof(state_sensor_possible_states);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500215 std::vector<uint8_t> entry{};
216 entry.resize(pdrSize);
217 pldm_state_sensor_pdr* pdr =
218 reinterpret_cast<pldm_state_sensor_pdr*>(entry.data());
219 if (!pdr)
220 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600221 error("Failed to get record by PDR type, ERROR:{ERR_CODE}", "ERR_CODE",
222 lg2::hex, static_cast<unsigned>(PLDM_PLATFORM_INVALID_SENSOR_ID));
Manojkiran Edabcf91ac2021-03-14 13:50:48 +0530223 return;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500224 }
225 pdr->hdr.record_handle = 0;
226 pdr->hdr.version = 1;
227 pdr->hdr.type = PLDM_STATE_SENSOR_PDR;
228 pdr->hdr.record_change_num = 0;
229 pdr->hdr.length = sizeof(pldm_state_sensor_pdr) - sizeof(pldm_pdr_hdr);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530230 pdr->terminus_handle = TERMINUS_HANDLE;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500231 pdr->sensor_id = platformHandler->getNextSensorId();
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500232 pdr->entity_type = entityType;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500233 pdr->entity_instance = entityInstance;
Sagar Srinivas97296e02021-08-26 09:40:41 -0500234 pdr->container_id = 1;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500235 pdr->sensor_init = PLDM_NO_INIT;
236 pdr->sensor_auxiliary_names_pdr = false;
237 pdr->composite_sensor_count = 1;
238
239 auto* possibleStatesPtr = pdr->possible_states;
240 auto possibleStates =
241 reinterpret_cast<state_sensor_possible_states*>(possibleStatesPtr);
242 possibleStates->state_set_id = stateSetID;
243 possibleStates->possible_states_size = 2;
244 auto state =
245 reinterpret_cast<state_sensor_possible_states*>(possibleStates);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500246 if ((stateSetID == PLDM_OEM_IBM_BOOT_STATE) ||
Christian Geddes7f9523c2021-08-03 13:44:38 -0500247 (stateSetID == PLDM_OEM_IBM_VERIFICATION_STATE))
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500248 state->states[0].byte = 6;
249 else if (stateSetID == PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE)
250 state->states[0].byte = 126;
251 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
252 pdrEntry.data = entry.data();
253 pdrEntry.size = pdrSize;
254 repo.addRecord(pdrEntry);
255}
256
257void pldm::responder::oem_ibm_platform::Handler::buildOEMPDR(
258 pdr_utils::Repo& repo)
259{
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530260 buildAllCodeUpdateEffecterPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
261 ENTITY_INSTANCE_0, PLDM_OEM_IBM_BOOT_STATE,
262 repo);
263 buildAllCodeUpdateEffecterPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
264 ENTITY_INSTANCE_1, PLDM_OEM_IBM_BOOT_STATE,
265 repo);
266 buildAllCodeUpdateEffecterPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
267 ENTITY_INSTANCE_0,
268 PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE, repo);
269 buildAllCodeUpdateEffecterPDR(this, PLDM_ENTITY_SYSTEM_CHASSIS,
Sagar Srinivas1b8d35f2021-11-10 10:54:40 -0600270 ENTITY_INSTANCE_1,
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600271 PLDM_OEM_IBM_SYSTEM_POWER_STATE, repo);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500272
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530273 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
274 ENTITY_INSTANCE_0, PLDM_OEM_IBM_BOOT_STATE,
275 repo);
276 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
277 ENTITY_INSTANCE_1, PLDM_OEM_IBM_BOOT_STATE,
278 repo);
279 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
280 ENTITY_INSTANCE_0,
281 PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE, repo);
282 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
283 ENTITY_INSTANCE_0,
284 PLDM_OEM_IBM_VERIFICATION_STATE, repo);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500285 auto sensorId = findStateSensorId(
286 repo.getPdr(), 0, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
Sagar Srinivasf4a7dd72021-10-20 04:45:37 -0500287 ENTITY_INSTANCE_0, 1, PLDM_OEM_IBM_VERIFICATION_STATE);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500288 codeUpdate->setMarkerLidSensor(sensorId);
289 sensorId = findStateSensorId(
290 repo.getPdr(), 0, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
Sagar Srinivasf4a7dd72021-10-20 04:45:37 -0500291 ENTITY_INSTANCE_0, 1, PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500292 codeUpdate->setFirmwareUpdateSensor(sensorId);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500293}
294
Sampa Misraaea5dde2020-08-31 08:33:47 -0500295void pldm::responder::oem_ibm_platform::Handler::setPlatformHandler(
296 pldm::responder::platform::Handler* handler)
297{
298 platformHandler = handler;
299}
300
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500301int pldm::responder::oem_ibm_platform::Handler::sendEventToHost(
Sampa Misrac0c79482021-06-02 08:01:54 -0500302 std::vector<uint8_t>& requestMsg, uint8_t instanceId)
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500303{
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500304 if (requestMsg.size())
305 {
306 std::ostringstream tempStream;
307 for (int byte : requestMsg)
308 {
309 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
310 << " ";
311 }
312 std::cout << tempStream.str() << std::endl;
313 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500314 auto oemPlatformEventMessageResponseHandler =
315 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Riya Dixit49cfb132023-03-02 04:26:53 -0600316 uint8_t completionCode{};
317 uint8_t status{};
318 auto rc = decode_platform_event_message_resp(response, respMsgLen,
319 &completionCode, &status);
320 if (rc || completionCode)
321 {
322 error(
323 "Failed to decode_platform_event_message_resp: for code update event rc={RC}, cc={CC}",
324 "RC", rc, "CC", static_cast<unsigned>(completionCode));
325 }
326 };
Sampa Misrac0c79482021-06-02 08:01:54 -0500327 auto rc = handler->registerRequest(
328 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
329 std::move(requestMsg),
330 std::move(oemPlatformEventMessageResponseHandler));
331 if (rc)
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500332 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600333 error("Failed to send BIOS attribute change event message ");
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500334 }
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500335
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500336 return rc;
337}
338
339int encodeEventMsg(uint8_t eventType, const std::vector<uint8_t>& eventDataVec,
340 std::vector<uint8_t>& requestMsg, uint8_t instanceId)
341{
342 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
343
344 auto rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -0600345 instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType,
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500346 eventDataVec.data(), eventDataVec.size(), request,
347 eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500348
349 return rc;
350}
351
352void pldm::responder::oem_ibm_platform::Handler::sendStateSensorEvent(
353 uint16_t sensorId, enum sensor_event_class_states sensorEventClass,
354 uint8_t sensorOffset, uint8_t eventState, uint8_t prevEventState)
355{
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500356 std::vector<uint8_t> sensorEventDataVec{};
357 size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
358 sensorEventDataVec.resize(sensorEventSize);
359 auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
360 sensorEventDataVec.data());
361 eventData->sensor_id = sensorId;
362 eventData->sensor_event_class_type = sensorEventClass;
363 auto eventClassStart = eventData->event_class;
364 auto eventClass =
365 reinterpret_cast<struct pldm_sensor_event_state_sensor_state*>(
366 eventClassStart);
367 eventClass->sensor_offset = sensorOffset;
368 eventClass->event_state = eventState;
369 eventClass->previous_event_state = prevEventState;
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930370 auto instanceId = instanceIdDb.next(mctp_eid);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500371 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
372 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
373 sensorEventDataVec.size());
374 auto rc = encodeEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec, requestMsg,
375 instanceId);
376 if (rc != PLDM_SUCCESS)
377 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600378 error("Failed to encode state sensor event, rc = {RC}", "RC", rc);
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930379 instanceIdDb.free(mctp_eid, instanceId);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500380 return;
381 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500382 rc = sendEventToHost(requestMsg, instanceId);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500383 if (rc != PLDM_SUCCESS)
384 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600385 error("Failed to send event to host: rc={RC}", "RC", rc);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500386 }
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500387 return;
388}
389
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500390void pldm::responder::oem_ibm_platform::Handler::_processEndUpdate(
391 sdeventplus::source::EventBase& /*source */)
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500392{
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500393 assembleImageEvent.reset();
Adriana Kobylak9296f242021-09-22 15:52:00 +0000394 int retc = codeUpdate->assembleCodeUpdateImage();
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500395 if (retc != PLDM_SUCCESS)
396 {
397 codeUpdate->setCodeUpdateProgress(false);
398 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
399 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
400 uint8_t(CodeUpdateState::FAIL),
401 uint8_t(CodeUpdateState::START));
402 }
403}
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500404
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500405void pldm::responder::oem_ibm_platform::Handler::_processStartUpdate(
406 sdeventplus::source::EventBase& /*source */)
407{
408 codeUpdate->deleteImage();
409 CodeUpdateState state = CodeUpdateState::START;
410 auto rc = codeUpdate->setRequestedApplyTime();
411 if (rc != PLDM_SUCCESS)
412 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600413 error("setRequestedApplyTime failed");
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500414 state = CodeUpdateState::FAIL;
415 }
416 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
417 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, uint8_t(state),
418 uint8_t(CodeUpdateState::END));
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500419}
420
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600421void pldm::responder::oem_ibm_platform::Handler::_processSystemReboot(
422 sdeventplus::source::EventBase& /*source */)
423{
424 pldm::utils::PropertyValue value =
425 "xyz.openbmc_project.State.Chassis.Transition.Off";
426 pldm::utils::DBusMapping dbusMapping{"/xyz/openbmc_project/state/chassis0",
427 "xyz.openbmc_project.State.Chassis",
428 "RequestedPowerTransition", "string"};
429 try
430 {
431 dBusIntf->setDbusProperty(dbusMapping, value);
432 }
433 catch (const std::exception& e)
434 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600435 error(
436 "Chassis State transition to Off failed, unable to set property RequestedPowerTransition ERROR={ERR_EXCEP}",
437 "ERR_EXCEP", e.what());
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600438 }
439
440 using namespace sdbusplus::bus::match::rules;
Patrick Williams84b790c2022-07-22 19:26:56 -0500441 chassisOffMatch = std::make_unique<sdbusplus::bus::match_t>(
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600442 pldm::utils::DBusHandler::getBus(),
443 propertiesChanged("/xyz/openbmc_project/state/chassis0",
444 "xyz.openbmc_project.State.Chassis"),
Patrick Williams84b790c2022-07-22 19:26:56 -0500445 [this](sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500446 DbusChangedProps props{};
447 std::string intf;
448 msg.read(intf, props);
449 const auto itr = props.find("CurrentPowerState");
450 if (itr != props.end())
451 {
452 PropertyValue value = itr->second;
453 auto propVal = std::get<std::string>(value);
454 if (propVal == "xyz.openbmc_project.State.Chassis.PowerState.Off")
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600455 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500456 pldm::utils::DBusMapping dbusMapping{
457 "/xyz/openbmc_project/control/host0/"
458 "power_restore_policy/one_time",
459 "xyz.openbmc_project.Control.Power.RestorePolicy",
460 "PowerRestorePolicy", "string"};
461 value = "xyz.openbmc_project.Control.Power.RestorePolicy."
462 "Policy.AlwaysOn";
463 try
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600464 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500465 dBusIntf->setDbusProperty(dbusMapping, value);
466 }
467 catch (const std::exception& e)
468 {
469 error(
470 "Setting one-time restore policy failed, unable to set property PowerRestorePolicy ERROR={ERR_EXCEP}",
471 "ERR_EXCEP", e.what());
472 }
473 dbusMapping = pldm::utils::DBusMapping{
474 "/xyz/openbmc_project/state/bmc0",
475 "xyz.openbmc_project.State.BMC", "RequestedBMCTransition",
476 "string"};
477 value = "xyz.openbmc_project.State.BMC.Transition.Reboot";
478 try
479 {
480 dBusIntf->setDbusProperty(dbusMapping, value);
481 }
482 catch (const std::exception& e)
483 {
484 error(
485 "BMC state transition to reboot failed, unable to set property RequestedBMCTransition ERROR={ERR_EXCEP}",
486 "ERR_EXCEP", e.what());
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600487 }
488 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500489 }
Patrick Williamsa6756622023-10-20 11:19:15 -0500490 });
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600491}
Sagar Srinivas79669c92021-04-28 15:43:30 -0500492
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500493void pldm::responder::oem_ibm_platform::Handler::checkAndDisableWatchDog()
494{
495 if (!hostOff && setEventReceiverCnt == SET_EVENT_RECEIVER_SENT)
496 {
497 disableWatchDogTimer();
498 }
499
500 return;
501}
502
Sagar Srinivas79669c92021-04-28 15:43:30 -0500503bool pldm::responder::oem_ibm_platform::Handler::watchDogRunning()
504{
505 static constexpr auto watchDogObjectPath =
506 "/xyz/openbmc_project/watchdog/host0";
507 static constexpr auto watchDogEnablePropName = "Enabled";
508 static constexpr auto watchDogInterface =
509 "xyz.openbmc_project.State.Watchdog";
510 bool isWatchDogRunning = false;
511 try
512 {
513 isWatchDogRunning = pldm::utils::DBusHandler().getDbusProperty<bool>(
514 watchDogObjectPath, watchDogEnablePropName, watchDogInterface);
515 }
516 catch (const std::exception& e)
517 {
518 return false;
519 }
520 return isWatchDogRunning;
521}
522
523void pldm::responder::oem_ibm_platform::Handler::resetWatchDogTimer()
524{
525 static constexpr auto watchDogService = "xyz.openbmc_project.Watchdog";
526 static constexpr auto watchDogObjectPath =
527 "/xyz/openbmc_project/watchdog/host0";
528 static constexpr auto watchDogInterface =
529 "xyz.openbmc_project.State.Watchdog";
530 static constexpr auto watchDogResetPropName = "ResetTimeRemaining";
531
532 bool wdStatus = watchDogRunning();
533 if (wdStatus == false)
534 {
535 return;
536 }
537 try
538 {
539 auto& bus = pldm::utils::DBusHandler::getBus();
540 auto resetMethod =
541 bus.new_method_call(watchDogService, watchDogObjectPath,
542 watchDogInterface, watchDogResetPropName);
543 resetMethod.append(true);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000544 bus.call_noreply(resetMethod, dbusTimeout);
Sagar Srinivas79669c92021-04-28 15:43:30 -0500545 }
546 catch (const std::exception& e)
547 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600548 error("Failed To reset watchdog timer ERROR={ERR_EXCEP}", "ERR_EXCEP",
549 e.what());
Sagar Srinivas79669c92021-04-28 15:43:30 -0500550 return;
551 }
552}
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500553
554void pldm::responder::oem_ibm_platform::Handler::disableWatchDogTimer()
555{
556 setEventReceiverCnt = 0;
557 pldm::utils::DBusMapping dbusMapping{"/xyz/openbmc_project/watchdog/host0",
558 "xyz.openbmc_project.State.Watchdog",
559 "Enabled", "bool"};
560 bool wdStatus = watchDogRunning();
561
562 if (!wdStatus)
563 {
564 return;
565 }
566 try
567 {
568 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, false);
569 }
570 catch (const std::exception& e)
571 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600572 error("Failed To disable watchdog timer ERROR={ERR_EXCEP}", "ERR_EXCEP",
573 e.what());
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500574 }
575}
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500576int pldm::responder::oem_ibm_platform::Handler::checkBMCState()
577{
578 try
579 {
580 pldm::utils::PropertyValue propertyValue =
581 pldm::utils::DBusHandler().getDbusPropertyVariant(
582 "/xyz/openbmc_project/state/bmc0", "CurrentBMCState",
583 "xyz.openbmc_project.State.BMC");
584
585 if (std::get<std::string>(propertyValue) ==
586 "xyz.openbmc_project.State.BMC.BMCState.NotReady")
587 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600588 error("GetPDR : PLDM stack is not ready for PDR exchange");
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500589 return PLDM_ERROR_NOT_READY;
590 }
591 }
592 catch (const std::exception& e)
593 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600594 error("Error getting the current BMC state");
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500595 return PLDM_ERROR;
596 }
597 return PLDM_SUCCESS;
598}
599
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500600const pldm_pdr_record*
601 pldm::responder::oem_ibm_platform::Handler::fetchLastBMCRecord(
602 const pldm_pdr* repo)
603{
604 return pldm_pdr_find_last_in_range(repo, BMC_PDR_START_RANGE,
605 BMC_PDR_END_RANGE);
606}
607
608bool pldm::responder::oem_ibm_platform::Handler::checkRecordHandleInRange(
609 const uint32_t& record_handle)
610{
611 return record_handle >= HOST_PDR_START_RANGE &&
612 record_handle <= HOST_PDR_END_RANGE;
613}
614
Sampa Misraaea5dde2020-08-31 08:33:47 -0500615} // namespace oem_ibm_platform
Sampa Misraaea5dde2020-08-31 08:33:47 -0500616} // namespace responder
Sampa Misraaea5dde2020-08-31 08:33:47 -0500617} // namespace pldm