Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 1 | #include "oem_ibm_handler.hpp" |
| 2 | |
| 3 | #include "libpldm/entity.h" |
Andrew Jeffery | 27a022c | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 4 | #include "libpldm/pldm.h" |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 5 | |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 6 | #include "file_io_type_lid.hpp" |
| 7 | #include "libpldmresponder/file_io.hpp" |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 8 | #include "libpldmresponder/pdr_utils.hpp" |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 9 | |
Brad Bishop | 27403f4 | 2021-12-01 21:37:44 -0500 | [diff] [blame] | 10 | #include <libpldm/entity_oem_ibm.h> |
| 11 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 12 | using namespace pldm::pdr; |
| 13 | using namespace pldm::utils; |
| 14 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 15 | namespace pldm |
| 16 | { |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 17 | namespace responder |
| 18 | { |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 19 | namespace oem_ibm_platform |
| 20 | { |
| 21 | |
| 22 | int 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 Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 34 | if (entityType == PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE && |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 35 | 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 | |
| 50 | int pldm::responder::oem_ibm_platform::Handler:: |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 51 | oemSetStateEffecterStatesHandler( |
Varsha Kaverappa | 3fbd39e | 2020-09-28 01:40:22 -0500 | [diff] [blame] | 52 | 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 Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 56 | { |
| 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 Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 63 | if (entityType == PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE && |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 64 | stateSetId == PLDM_OEM_IBM_BOOT_STATE) |
| 65 | { |
| 66 | rc = setBootSide(entityInstance, currState, stateField, |
| 67 | codeUpdate); |
| 68 | } |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 69 | 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 Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 76 | 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 Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 82 | } |
| 83 | else if (stateField[currState].effecter_state == |
| 84 | uint8_t(CodeUpdateState::END)) |
| 85 | { |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 86 | 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 Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 96 | } |
| 97 | else if (stateField[currState].effecter_state == |
| 98 | uint8_t(CodeUpdateState::ABORT)) |
| 99 | { |
| 100 | codeUpdate->setCodeUpdateProgress(false); |
| 101 | codeUpdate->clearDirPath(LID_STAGING_DIR); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 102 | 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 Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 107 | } |
| 108 | else if (stateField[currState].effecter_state == |
| 109 | uint8_t(CodeUpdateState::ACCEPT)) |
| 110 | { |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 111 | auto sensorId = codeUpdate->getFirmwareUpdateSensor(); |
| 112 | sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 113 | uint8_t(CodeUpdateState::ACCEPT), |
| 114 | uint8_t(CodeUpdateState::END)); |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 115 | // TODO Set new Dbus property provided by code update app |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 116 | // sendCodeUpdateEvent(effecterId, ACCEPT, END); |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 117 | } |
| 118 | else if (stateField[currState].effecter_state == |
| 119 | uint8_t(CodeUpdateState::REJECT)) |
| 120 | { |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 121 | auto sensorId = codeUpdate->getFirmwareUpdateSensor(); |
| 122 | sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 123 | uint8_t(CodeUpdateState::REJECT), |
| 124 | uint8_t(CodeUpdateState::END)); |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 125 | // TODO Set new Dbus property provided by code update app |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 126 | // sendCodeUpdateEvent(effecterId, REJECT, END); |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 127 | } |
| 128 | } |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 129 | 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 Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 142 | 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 Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 155 | void buildAllCodeUpdateEffecterPDR(oem_ibm_platform::Handler* platformHandler, |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 156 | uint16_t entityType, uint16_t entityInstance, |
| 157 | uint16_t stateSetID, pdr_utils::Repo& repo) |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 158 | { |
| 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 Eda | bcf91ac | 2021-03-14 13:50:48 +0530 | [diff] [blame] | 170 | return; |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 171 | } |
| 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 Eda | cc5f158 | 2021-09-29 17:03:06 +0530 | [diff] [blame] | 177 | pdr->terminus_handle = TERMINUS_HANDLE; |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 178 | pdr->effecter_id = platformHandler->getNextEffecterId(); |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 179 | pdr->entity_type = entityType; |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 180 | pdr->entity_instance = entityInstance; |
Sagar Srinivas | 97296e0 | 2021-08-26 09:40:41 -0500 | [diff] [blame] | 181 | pdr->container_id = 1; |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 182 | 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 Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 198 | else if (stateSetID == PLDM_OEM_IBM_SYSTEM_POWER_STATE) |
| 199 | state->states[0].byte = 2; |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 200 | pldm::responder::pdr_utils::PdrEntry pdrEntry{}; |
| 201 | pdrEntry.data = entry.data(); |
| 202 | pdrEntry.size = pdrSize; |
| 203 | repo.addRecord(pdrEntry); |
| 204 | } |
| 205 | |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 206 | void buildAllCodeUpdateSensorPDR(oem_ibm_platform::Handler* platformHandler, |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 207 | uint16_t entityType, uint16_t entityInstance, |
| 208 | uint16_t stateSetID, pdr_utils::Repo& repo) |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 209 | { |
| 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 Eda | bcf91ac | 2021-03-14 13:50:48 +0530 | [diff] [blame] | 221 | return; |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 222 | } |
| 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 Eda | cc5f158 | 2021-09-29 17:03:06 +0530 | [diff] [blame] | 228 | pdr->terminus_handle = TERMINUS_HANDLE; |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 229 | pdr->sensor_id = platformHandler->getNextSensorId(); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 230 | pdr->entity_type = entityType; |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 231 | pdr->entity_instance = entityInstance; |
Sagar Srinivas | 97296e0 | 2021-08-26 09:40:41 -0500 | [diff] [blame] | 232 | pdr->container_id = 1; |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 233 | 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 Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 244 | if ((stateSetID == PLDM_OEM_IBM_BOOT_STATE) || |
Christian Geddes | 7f9523c | 2021-08-03 13:44:38 -0500 | [diff] [blame] | 245 | (stateSetID == PLDM_OEM_IBM_VERIFICATION_STATE)) |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 246 | 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 | |
| 255 | void pldm::responder::oem_ibm_platform::Handler::buildOEMPDR( |
| 256 | pdr_utils::Repo& repo) |
| 257 | { |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 258 | 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 Srinivas | 1b8d35f | 2021-11-10 10:54:40 -0600 | [diff] [blame] | 268 | ENTITY_INSTANCE_1, |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 269 | PLDM_OEM_IBM_SYSTEM_POWER_STATE, repo); |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 270 | |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 271 | 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 Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 283 | auto sensorId = findStateSensorId( |
| 284 | repo.getPdr(), 0, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE, |
Sagar Srinivas | f4a7dd7 | 2021-10-20 04:45:37 -0500 | [diff] [blame] | 285 | ENTITY_INSTANCE_0, 1, PLDM_OEM_IBM_VERIFICATION_STATE); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 286 | codeUpdate->setMarkerLidSensor(sensorId); |
| 287 | sensorId = findStateSensorId( |
| 288 | repo.getPdr(), 0, PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE, |
Sagar Srinivas | f4a7dd7 | 2021-10-20 04:45:37 -0500 | [diff] [blame] | 289 | ENTITY_INSTANCE_0, 1, PLDM_OEM_IBM_FIRMWARE_UPDATE_STATE); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 290 | codeUpdate->setFirmwareUpdateSensor(sensorId); |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 291 | } |
| 292 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 293 | void pldm::responder::oem_ibm_platform::Handler::setPlatformHandler( |
| 294 | pldm::responder::platform::Handler* handler) |
| 295 | { |
| 296 | platformHandler = handler; |
| 297 | } |
| 298 | |
Varsha Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 299 | int pldm::responder::oem_ibm_platform::Handler::sendEventToHost( |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 300 | std::vector<uint8_t>& requestMsg, uint8_t instanceId) |
Varsha Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 301 | { |
Varsha Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 302 | 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 Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 312 | 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 Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 331 | { |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 332 | std::cerr << "Failed to send BIOS attribute change event message \n"; |
Varsha Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 333 | } |
Varsha Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 334 | |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 335 | return rc; |
| 336 | } |
| 337 | |
| 338 | int 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 Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 347 | |
| 348 | return rc; |
| 349 | } |
| 350 | |
| 351 | void 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 Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 355 | 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 Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 379 | requester.markFree(mctp_eid, instanceId); |
Varsha Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 380 | return; |
| 381 | } |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 382 | rc = sendEventToHost(requestMsg, instanceId); |
Varsha Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 383 | if (rc != PLDM_SUCCESS) |
| 384 | { |
| 385 | std::cerr << "Failed to send event to host: " |
| 386 | << "rc=" << rc << std::endl; |
| 387 | } |
Varsha Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 391 | void pldm::responder::oem_ibm_platform::Handler::_processEndUpdate( |
| 392 | sdeventplus::source::EventBase& /*source */) |
Varsha Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 393 | { |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 394 | assembleImageEvent.reset(); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 395 | int retc = codeUpdate->assembleCodeUpdateImage(); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 396 | 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 Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 405 | |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 406 | void 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 Kaverappa | bb585b2 | 2020-09-10 06:15:42 -0500 | [diff] [blame] | 420 | } |
| 421 | |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 422 | void 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 Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 436 | 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 Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 442 | chassisOffMatch = std::make_unique<sdbusplus::bus::match_t>( |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 443 | pldm::utils::DBusHandler::getBus(), |
| 444 | propertiesChanged("/xyz/openbmc_project/state/chassis0", |
| 445 | "xyz.openbmc_project.State.Chassis"), |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 446 | [this](sdbusplus::message_t& msg) { |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 447 | 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 Srinivas | 79669c9 | 2021-04-28 15:43:30 -0500 | [diff] [blame] | 495 | |
Sagar Srinivas | 7f760b3 | 2021-05-12 07:46:56 -0500 | [diff] [blame] | 496 | void 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 Srinivas | 79669c9 | 2021-04-28 15:43:30 -0500 | [diff] [blame] | 506 | bool 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 | |
| 526 | void 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 Srinivas | 7f760b3 | 2021-05-12 07:46:56 -0500 | [diff] [blame] | 556 | |
| 557 | void 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 Barithaya | 99854a7 | 2021-09-29 06:58:11 -0500 | [diff] [blame] | 579 | int 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 Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 604 | } // namespace oem_ibm_platform |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 605 | } // namespace responder |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 606 | } // namespace pldm |