blob: abe9e690bcd6151aec0b366a087b2f2365b231d4 [file] [log] [blame]
Sampa Misraaea5dde2020-08-31 08:33:47 -05001#include "oem_ibm_handler.hpp"
2
3#include "libpldm/entity.h"
Andrew Jeffery27a022c2022-08-10 23:12:49 +09304#include "libpldm/pldm.h"
Sampa Misraaea5dde2020-08-31 08:33:47 -05005
Sagar Srinivascfdbca72020-09-22 10:03:35 -05006#include "file_io_type_lid.hpp"
7#include "libpldmresponder/file_io.hpp"
Sagar Srinivas78a225a2020-08-27 00:52:20 -05008#include "libpldmresponder/pdr_utils.hpp"
Brad Bishop5079ac42021-08-19 18:35:06 -04009
Brad Bishop27403f42021-12-01 21:37:44 -050010#include <libpldm/entity_oem_ibm.h>
11
Brad Bishop5079ac42021-08-19 18:35:06 -040012using namespace pldm::pdr;
13using namespace pldm::utils;
14
Sampa Misraaea5dde2020-08-31 08:33:47 -050015namespace pldm
16{
Sampa Misraaea5dde2020-08-31 08:33:47 -050017namespace responder
18{
Sampa Misraaea5dde2020-08-31 08:33:47 -050019namespace oem_ibm_platform
20{
21
22int pldm::responder::oem_ibm_platform::Handler::
23 getOemStateSensorReadingsHandler(
24 EntityType entityType, EntityInstance entityInstance,
25 StateSetId stateSetId, CompositeCount compSensorCnt,
26 std::vector<get_sensor_state_field>& stateField)
27{
28 int rc = PLDM_SUCCESS;
29 stateField.clear();
30
31 for (size_t i = 0; i < compSensorCnt; i++)
32 {
33 uint8_t sensorOpState{};
Sagar Srinivas78a225a2020-08-27 00:52:20 -050034 if (entityType == PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE &&
Sampa Misraaea5dde2020-08-31 08:33:47 -050035 stateSetId == PLDM_OEM_IBM_BOOT_STATE)
36 {
37 sensorOpState = fetchBootSide(entityInstance, codeUpdate);
38 }
39 else
40 {
41 rc = PLDM_PLATFORM_INVALID_STATE_VALUE;
42 break;
43 }
44 stateField.push_back({PLDM_SENSOR_ENABLED, PLDM_SENSOR_UNKNOWN,
45 PLDM_SENSOR_UNKNOWN, sensorOpState});
46 }
47 return rc;
48}
49
50int pldm::responder::oem_ibm_platform::Handler::
Sampa Misra3a0e3b92020-10-21 05:58:00 -050051 oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -050052 uint16_t entityType, uint16_t entityInstance, uint16_t stateSetId,
53 uint8_t compEffecterCnt,
54 std::vector<set_effecter_state_field>& stateField,
55 uint16_t /*effecterId*/)
Sampa Misraaea5dde2020-08-31 08:33:47 -050056{
57 int rc = PLDM_SUCCESS;
58
59 for (uint8_t currState = 0; currState < compEffecterCnt; ++currState)
60 {
61 if (stateField[currState].set_request == PLDM_REQUEST_SET)
62 {
Sagar Srinivas78a225a2020-08-27 00:52:20 -050063 if (entityType == PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE &&
Sampa Misraaea5dde2020-08-31 08:33:47 -050064 stateSetId == PLDM_OEM_IBM_BOOT_STATE)
65 {
66 rc = setBootSide(entityInstance, currState, stateField,
67 codeUpdate);
68 }
Sagar Srinivascfdbca72020-09-22 10:03:35 -050069 else if (entityType == PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE &&
70 stateSetId == PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE)
71 {
72 if (stateField[currState].effecter_state ==
73 uint8_t(CodeUpdateState::START))
74 {
75 codeUpdate->setCodeUpdateProgress(true);
Sampa Misra3a0e3b92020-10-21 05:58:00 -050076 startUpdateEvent =
77 std::make_unique<sdeventplus::source::Defer>(
78 event,
79 std::bind(std::mem_fn(&oem_ibm_platform::Handler::
80 _processStartUpdate),
81 this, std::placeholders::_1));
Sagar Srinivascfdbca72020-09-22 10:03:35 -050082 }
83 else if (stateField[currState].effecter_state ==
84 uint8_t(CodeUpdateState::END))
85 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -050086 rc = PLDM_SUCCESS;
87 assembleImageEvent = std::make_unique<
88 sdeventplus::source::Defer>(
89 event,
90 std::bind(
91 std::mem_fn(
92 &oem_ibm_platform::Handler::_processEndUpdate),
93 this, std::placeholders::_1));
94
95 // sendCodeUpdateEvent(effecterId, END, START);
Sagar Srinivascfdbca72020-09-22 10:03:35 -050096 }
97 else if (stateField[currState].effecter_state ==
98 uint8_t(CodeUpdateState::ABORT))
99 {
100 codeUpdate->setCodeUpdateProgress(false);
101 codeUpdate->clearDirPath(LID_STAGING_DIR);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500102 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
103 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
104 uint8_t(CodeUpdateState::ABORT),
105 uint8_t(CodeUpdateState::START));
106 // sendCodeUpdateEvent(effecterId, ABORT, END);
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500107 }
108 else if (stateField[currState].effecter_state ==
109 uint8_t(CodeUpdateState::ACCEPT))
110 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500111 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
112 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
113 uint8_t(CodeUpdateState::ACCEPT),
114 uint8_t(CodeUpdateState::END));
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500115 // TODO Set new Dbus property provided by code update app
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500116 // sendCodeUpdateEvent(effecterId, ACCEPT, END);
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500117 }
118 else if (stateField[currState].effecter_state ==
119 uint8_t(CodeUpdateState::REJECT))
120 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500121 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
122 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
123 uint8_t(CodeUpdateState::REJECT),
124 uint8_t(CodeUpdateState::END));
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500125 // TODO Set new Dbus property provided by code update app
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500126 // sendCodeUpdateEvent(effecterId, REJECT, END);
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500127 }
128 }
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600129 else if (entityType == PLDM_ENTITY_SYSTEM_CHASSIS &&
130 stateSetId == PLDM_OEM_IBM_SYSTEM_POWER_STATE)
131 {
132 if (stateField[currState].effecter_state == POWER_CYCLE_HARD)
133 {
134 systemRebootEvent =
135 std::make_unique<sdeventplus::source::Defer>(
136 event,
137 std::bind(std::mem_fn(&oem_ibm_platform::Handler::
138 _processSystemReboot),
139 this, std::placeholders::_1));
140 }
141 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500142 else
143 {
144 rc = PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE;
145 }
146 }
147 if (rc != PLDM_SUCCESS)
148 {
149 break;
150 }
151 }
152 return rc;
153}
154
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530155void buildAllCodeUpdateEffecterPDR(oem_ibm_platform::Handler* platformHandler,
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600156 uint16_t entityType, uint16_t entityInstance,
157 uint16_t stateSetID, pdr_utils::Repo& repo)
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500158{
159 size_t pdrSize = 0;
160 pdrSize = sizeof(pldm_state_effecter_pdr) +
161 sizeof(state_effecter_possible_states);
162 std::vector<uint8_t> entry{};
163 entry.resize(pdrSize);
164 pldm_state_effecter_pdr* pdr =
165 reinterpret_cast<pldm_state_effecter_pdr*>(entry.data());
166 if (!pdr)
167 {
168 std::cerr << "Failed to get record by PDR type, ERROR:"
169 << PLDM_PLATFORM_INVALID_EFFECTER_ID << std::endl;
Manojkiran Edabcf91ac2021-03-14 13:50:48 +0530170 return;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500171 }
172 pdr->hdr.record_handle = 0;
173 pdr->hdr.version = 1;
174 pdr->hdr.type = PLDM_STATE_EFFECTER_PDR;
175 pdr->hdr.record_change_num = 0;
176 pdr->hdr.length = sizeof(pldm_state_effecter_pdr) - sizeof(pldm_pdr_hdr);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530177 pdr->terminus_handle = TERMINUS_HANDLE;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500178 pdr->effecter_id = platformHandler->getNextEffecterId();
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600179 pdr->entity_type = entityType;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500180 pdr->entity_instance = entityInstance;
Sagar Srinivas97296e02021-08-26 09:40:41 -0500181 pdr->container_id = 1;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500182 pdr->effecter_semantic_id = 0;
183 pdr->effecter_init = PLDM_NO_INIT;
184 pdr->has_description_pdr = false;
185 pdr->composite_effecter_count = 1;
186
187 auto* possibleStatesPtr = pdr->possible_states;
188 auto possibleStates =
189 reinterpret_cast<state_effecter_possible_states*>(possibleStatesPtr);
190 possibleStates->state_set_id = stateSetID;
191 possibleStates->possible_states_size = 2;
192 auto state =
193 reinterpret_cast<state_effecter_possible_states*>(possibleStates);
194 if (stateSetID == PLDM_OEM_IBM_BOOT_STATE)
195 state->states[0].byte = 6;
196 else if (stateSetID == PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE)
197 state->states[0].byte = 126;
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600198 else if (stateSetID == PLDM_OEM_IBM_SYSTEM_POWER_STATE)
199 state->states[0].byte = 2;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500200 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
201 pdrEntry.data = entry.data();
202 pdrEntry.size = pdrSize;
203 repo.addRecord(pdrEntry);
204}
205
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530206void buildAllCodeUpdateSensorPDR(oem_ibm_platform::Handler* platformHandler,
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500207 uint16_t entityType, uint16_t entityInstance,
208 uint16_t stateSetID, pdr_utils::Repo& repo)
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500209{
210 size_t pdrSize = 0;
211 pdrSize =
212 sizeof(pldm_state_sensor_pdr) + sizeof(state_sensor_possible_states);
213 std::vector<uint8_t> entry{};
214 entry.resize(pdrSize);
215 pldm_state_sensor_pdr* pdr =
216 reinterpret_cast<pldm_state_sensor_pdr*>(entry.data());
217 if (!pdr)
218 {
219 std::cerr << "Failed to get record by PDR type, ERROR:"
220 << PLDM_PLATFORM_INVALID_SENSOR_ID << std::endl;
Manojkiran Edabcf91ac2021-03-14 13:50:48 +0530221 return;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500222 }
223 pdr->hdr.record_handle = 0;
224 pdr->hdr.version = 1;
225 pdr->hdr.type = PLDM_STATE_SENSOR_PDR;
226 pdr->hdr.record_change_num = 0;
227 pdr->hdr.length = sizeof(pldm_state_sensor_pdr) - sizeof(pldm_pdr_hdr);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530228 pdr->terminus_handle = TERMINUS_HANDLE;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500229 pdr->sensor_id = platformHandler->getNextSensorId();
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500230 pdr->entity_type = entityType;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500231 pdr->entity_instance = entityInstance;
Sagar Srinivas97296e02021-08-26 09:40:41 -0500232 pdr->container_id = 1;
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500233 pdr->sensor_init = PLDM_NO_INIT;
234 pdr->sensor_auxiliary_names_pdr = false;
235 pdr->composite_sensor_count = 1;
236
237 auto* possibleStatesPtr = pdr->possible_states;
238 auto possibleStates =
239 reinterpret_cast<state_sensor_possible_states*>(possibleStatesPtr);
240 possibleStates->state_set_id = stateSetID;
241 possibleStates->possible_states_size = 2;
242 auto state =
243 reinterpret_cast<state_sensor_possible_states*>(possibleStates);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500244 if ((stateSetID == PLDM_OEM_IBM_BOOT_STATE) ||
Christian Geddes7f9523c2021-08-03 13:44:38 -0500245 (stateSetID == PLDM_OEM_IBM_VERIFICATION_STATE))
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500246 state->states[0].byte = 6;
247 else if (stateSetID == PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE)
248 state->states[0].byte = 126;
249 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
250 pdrEntry.data = entry.data();
251 pdrEntry.size = pdrSize;
252 repo.addRecord(pdrEntry);
253}
254
255void pldm::responder::oem_ibm_platform::Handler::buildOEMPDR(
256 pdr_utils::Repo& repo)
257{
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530258 buildAllCodeUpdateEffecterPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
259 ENTITY_INSTANCE_0, PLDM_OEM_IBM_BOOT_STATE,
260 repo);
261 buildAllCodeUpdateEffecterPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
262 ENTITY_INSTANCE_1, PLDM_OEM_IBM_BOOT_STATE,
263 repo);
264 buildAllCodeUpdateEffecterPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
265 ENTITY_INSTANCE_0,
266 PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE, repo);
267 buildAllCodeUpdateEffecterPDR(this, PLDM_ENTITY_SYSTEM_CHASSIS,
Sagar Srinivas1b8d35f2021-11-10 10:54:40 -0600268 ENTITY_INSTANCE_1,
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600269 PLDM_OEM_IBM_SYSTEM_POWER_STATE, repo);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500270
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530271 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
272 ENTITY_INSTANCE_0, PLDM_OEM_IBM_BOOT_STATE,
273 repo);
274 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
275 ENTITY_INSTANCE_1, PLDM_OEM_IBM_BOOT_STATE,
276 repo);
277 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
278 ENTITY_INSTANCE_0,
279 PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE, repo);
280 buildAllCodeUpdateSensorPDR(this, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
281 ENTITY_INSTANCE_0,
282 PLDM_OEM_IBM_VERIFICATION_STATE, repo);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500283 auto sensorId = findStateSensorId(
284 repo.getPdr(), 0, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
Sagar Srinivasf4a7dd72021-10-20 04:45:37 -0500285 ENTITY_INSTANCE_0, 1, PLDM_OEM_IBM_VERIFICATION_STATE);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500286 codeUpdate->setMarkerLidSensor(sensorId);
287 sensorId = findStateSensorId(
288 repo.getPdr(), 0, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE,
Sagar Srinivasf4a7dd72021-10-20 04:45:37 -0500289 ENTITY_INSTANCE_0, 1, PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500290 codeUpdate->setFirmwareUpdateSensor(sensorId);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500291}
292
Sampa Misraaea5dde2020-08-31 08:33:47 -0500293void pldm::responder::oem_ibm_platform::Handler::setPlatformHandler(
294 pldm::responder::platform::Handler* handler)
295{
296 platformHandler = handler;
297}
298
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500299int pldm::responder::oem_ibm_platform::Handler::sendEventToHost(
Sampa Misrac0c79482021-06-02 08:01:54 -0500300 std::vector<uint8_t>& requestMsg, uint8_t instanceId)
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500301{
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500302 if (requestMsg.size())
303 {
304 std::ostringstream tempStream;
305 for (int byte : requestMsg)
306 {
307 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
308 << " ";
309 }
310 std::cout << tempStream.str() << std::endl;
311 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500312 auto oemPlatformEventMessageResponseHandler =
313 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
314 uint8_t completionCode{};
315 uint8_t status{};
316 auto rc = decode_platform_event_message_resp(
317 response, respMsgLen, &completionCode, &status);
318 if (rc || completionCode)
319 {
320 std::cerr << "Failed to decode_platform_event_message_resp: "
321 << " for code update event rc=" << rc
322 << ", cc=" << static_cast<unsigned>(completionCode)
323 << std::endl;
324 }
325 };
326 auto rc = handler->registerRequest(
327 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
328 std::move(requestMsg),
329 std::move(oemPlatformEventMessageResponseHandler));
330 if (rc)
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500331 {
Sampa Misrac0c79482021-06-02 08:01:54 -0500332 std::cerr << "Failed to send BIOS attribute change event message \n";
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500333 }
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500334
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500335 return rc;
336}
337
338int encodeEventMsg(uint8_t eventType, const std::vector<uint8_t>& eventDataVec,
339 std::vector<uint8_t>& requestMsg, uint8_t instanceId)
340{
341 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
342
343 auto rc = encode_platform_event_message_req(
344 instanceId, 1 /*formatVersion*/, 0 /*tId*/, eventType,
345 eventDataVec.data(), eventDataVec.size(), request,
346 eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500347
348 return rc;
349}
350
351void pldm::responder::oem_ibm_platform::Handler::sendStateSensorEvent(
352 uint16_t sensorId, enum sensor_event_class_states sensorEventClass,
353 uint8_t sensorOffset, uint8_t eventState, uint8_t prevEventState)
354{
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500355 std::vector<uint8_t> sensorEventDataVec{};
356 size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
357 sensorEventDataVec.resize(sensorEventSize);
358 auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
359 sensorEventDataVec.data());
360 eventData->sensor_id = sensorId;
361 eventData->sensor_event_class_type = sensorEventClass;
362 auto eventClassStart = eventData->event_class;
363 auto eventClass =
364 reinterpret_cast<struct pldm_sensor_event_state_sensor_state*>(
365 eventClassStart);
366 eventClass->sensor_offset = sensorOffset;
367 eventClass->event_state = eventState;
368 eventClass->previous_event_state = prevEventState;
369 auto instanceId = requester.getInstanceId(mctp_eid);
370 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
371 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
372 sensorEventDataVec.size());
373 auto rc = encodeEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec, requestMsg,
374 instanceId);
375 if (rc != PLDM_SUCCESS)
376 {
377 std::cerr << "Failed to encode state sensor event, rc = " << rc
378 << std::endl;
Sampa Misrac0c79482021-06-02 08:01:54 -0500379 requester.markFree(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 {
385 std::cerr << "Failed to send event to host: "
386 << "rc=" << rc << std::endl;
387 }
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500388 return;
389}
390
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500391void pldm::responder::oem_ibm_platform::Handler::_processEndUpdate(
392 sdeventplus::source::EventBase& /*source */)
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500393{
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500394 assembleImageEvent.reset();
Adriana Kobylak9296f242021-09-22 15:52:00 +0000395 int retc = codeUpdate->assembleCodeUpdateImage();
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500396 if (retc != PLDM_SUCCESS)
397 {
398 codeUpdate->setCodeUpdateProgress(false);
399 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
400 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
401 uint8_t(CodeUpdateState::FAIL),
402 uint8_t(CodeUpdateState::START));
403 }
404}
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500405
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500406void pldm::responder::oem_ibm_platform::Handler::_processStartUpdate(
407 sdeventplus::source::EventBase& /*source */)
408{
409 codeUpdate->deleteImage();
410 CodeUpdateState state = CodeUpdateState::START;
411 auto rc = codeUpdate->setRequestedApplyTime();
412 if (rc != PLDM_SUCCESS)
413 {
414 std::cerr << "setRequestedApplyTime failed \n";
415 state = CodeUpdateState::FAIL;
416 }
417 auto sensorId = codeUpdate->getFirmwareUpdateSensor();
418 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, uint8_t(state),
419 uint8_t(CodeUpdateState::END));
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500420}
421
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600422void pldm::responder::oem_ibm_platform::Handler::_processSystemReboot(
423 sdeventplus::source::EventBase& /*source */)
424{
425 pldm::utils::PropertyValue value =
426 "xyz.openbmc_project.State.Chassis.Transition.Off";
427 pldm::utils::DBusMapping dbusMapping{"/xyz/openbmc_project/state/chassis0",
428 "xyz.openbmc_project.State.Chassis",
429 "RequestedPowerTransition", "string"};
430 try
431 {
432 dBusIntf->setDbusProperty(dbusMapping, value);
433 }
434 catch (const std::exception& e)
435 {
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600436 std::cerr << "Chassis State transition to Off failed,"
437 << "unable to set property RequestedPowerTransition"
438 << "ERROR=" << e.what() << "\n";
439 }
440
441 using namespace sdbusplus::bus::match::rules;
Patrick Williams84b790c2022-07-22 19:26:56 -0500442 chassisOffMatch = std::make_unique<sdbusplus::bus::match_t>(
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600443 pldm::utils::DBusHandler::getBus(),
444 propertiesChanged("/xyz/openbmc_project/state/chassis0",
445 "xyz.openbmc_project.State.Chassis"),
Patrick Williams84b790c2022-07-22 19:26:56 -0500446 [this](sdbusplus::message_t& msg) {
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600447 DbusChangedProps props{};
448 std::string intf;
449 msg.read(intf, props);
450 const auto itr = props.find("CurrentPowerState");
451 if (itr != props.end())
452 {
453 PropertyValue value = itr->second;
454 auto propVal = std::get<std::string>(value);
455 if (propVal ==
456 "xyz.openbmc_project.State.Chassis.PowerState.Off")
457 {
458 pldm::utils::DBusMapping dbusMapping{
459 "/xyz/openbmc_project/control/host0/"
460 "power_restore_policy/one_time",
461 "xyz.openbmc_project.Control.Power.RestorePolicy",
462 "PowerRestorePolicy", "string"};
463 value = "xyz.openbmc_project.Control.Power.RestorePolicy."
464 "Policy.AlwaysOn";
465 try
466 {
467 dBusIntf->setDbusProperty(dbusMapping, value);
468 }
469 catch (const std::exception& e)
470 {
471 std::cerr << "Setting one-time restore policy failed,"
472 << "unable to set property PowerRestorePolicy"
473 << "ERROR=" << e.what() << "\n";
474 }
475 dbusMapping = pldm::utils::DBusMapping{
476 "/xyz/openbmc_project/state/bmc0",
477 "xyz.openbmc_project.State.BMC",
478 "RequestedBMCTransition", "string"};
479 value = "xyz.openbmc_project.State.BMC.Transition.Reboot";
480 try
481 {
482 dBusIntf->setDbusProperty(dbusMapping, value);
483 }
484 catch (const std::exception& e)
485 {
486 std::cerr << "BMC state transition to reboot failed,"
487 << "unable to set property "
488 "RequestedBMCTransition"
489 << "ERROR=" << e.what() << "\n";
490 }
491 }
492 }
493 });
494}
Sagar Srinivas79669c92021-04-28 15:43:30 -0500495
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500496void pldm::responder::oem_ibm_platform::Handler::checkAndDisableWatchDog()
497{
498 if (!hostOff && setEventReceiverCnt == SET_EVENT_RECEIVER_SENT)
499 {
500 disableWatchDogTimer();
501 }
502
503 return;
504}
505
Sagar Srinivas79669c92021-04-28 15:43:30 -0500506bool pldm::responder::oem_ibm_platform::Handler::watchDogRunning()
507{
508 static constexpr auto watchDogObjectPath =
509 "/xyz/openbmc_project/watchdog/host0";
510 static constexpr auto watchDogEnablePropName = "Enabled";
511 static constexpr auto watchDogInterface =
512 "xyz.openbmc_project.State.Watchdog";
513 bool isWatchDogRunning = false;
514 try
515 {
516 isWatchDogRunning = pldm::utils::DBusHandler().getDbusProperty<bool>(
517 watchDogObjectPath, watchDogEnablePropName, watchDogInterface);
518 }
519 catch (const std::exception& e)
520 {
521 return false;
522 }
523 return isWatchDogRunning;
524}
525
526void pldm::responder::oem_ibm_platform::Handler::resetWatchDogTimer()
527{
528 static constexpr auto watchDogService = "xyz.openbmc_project.Watchdog";
529 static constexpr auto watchDogObjectPath =
530 "/xyz/openbmc_project/watchdog/host0";
531 static constexpr auto watchDogInterface =
532 "xyz.openbmc_project.State.Watchdog";
533 static constexpr auto watchDogResetPropName = "ResetTimeRemaining";
534
535 bool wdStatus = watchDogRunning();
536 if (wdStatus == false)
537 {
538 return;
539 }
540 try
541 {
542 auto& bus = pldm::utils::DBusHandler::getBus();
543 auto resetMethod =
544 bus.new_method_call(watchDogService, watchDogObjectPath,
545 watchDogInterface, watchDogResetPropName);
546 resetMethod.append(true);
547 bus.call_noreply(resetMethod);
548 }
549 catch (const std::exception& e)
550 {
551 std::cerr << "Failed To reset watchdog timer"
552 << "ERROR=" << e.what() << std::endl;
553 return;
554 }
555}
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500556
557void pldm::responder::oem_ibm_platform::Handler::disableWatchDogTimer()
558{
559 setEventReceiverCnt = 0;
560 pldm::utils::DBusMapping dbusMapping{"/xyz/openbmc_project/watchdog/host0",
561 "xyz.openbmc_project.State.Watchdog",
562 "Enabled", "bool"};
563 bool wdStatus = watchDogRunning();
564
565 if (!wdStatus)
566 {
567 return;
568 }
569 try
570 {
571 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, false);
572 }
573 catch (const std::exception& e)
574 {
575 std::cerr << "Failed To disable watchdog timer"
576 << "ERROR=" << e.what() << "\n";
577 }
578}
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500579int pldm::responder::oem_ibm_platform::Handler::checkBMCState()
580{
581 try
582 {
583 pldm::utils::PropertyValue propertyValue =
584 pldm::utils::DBusHandler().getDbusPropertyVariant(
585 "/xyz/openbmc_project/state/bmc0", "CurrentBMCState",
586 "xyz.openbmc_project.State.BMC");
587
588 if (std::get<std::string>(propertyValue) ==
589 "xyz.openbmc_project.State.BMC.BMCState.NotReady")
590 {
591 std::cerr << "GetPDR : PLDM stack is not ready for PDR exchange"
592 << std::endl;
593 return PLDM_ERROR_NOT_READY;
594 }
595 }
596 catch (const std::exception& e)
597 {
598 std::cerr << "Error getting the current BMC state" << std::endl;
599 return PLDM_ERROR;
600 }
601 return PLDM_SUCCESS;
602}
603
Sampa Misraaea5dde2020-08-31 08:33:47 -0500604} // namespace oem_ibm_platform
Sampa Misraaea5dde2020-08-31 08:33:47 -0500605} // namespace responder
Sampa Misraaea5dde2020-08-31 08:33:47 -0500606} // namespace pldm