Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 1 | |
| 2 | #include "platform.hpp" |
| 3 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 4 | #include "utils.hpp" |
| 5 | |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 6 | namespace pldm |
| 7 | { |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 8 | namespace responder |
| 9 | { |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 10 | namespace platform |
| 11 | { |
| 12 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 13 | using InternalFailure = |
| 14 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 15 | |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame^] | 16 | static const Json empty{}; |
| 17 | |
| 18 | void Handler::addDbusObjMaps(uint16_t effecterId, |
| 19 | std::tuple<DbusMappings, DbusValMaps> dbusObj) |
| 20 | { |
| 21 | dbusObjMaps.emplace(effecterId, dbusObj); |
| 22 | } |
| 23 | |
| 24 | const std::tuple<DbusMappings, DbusValMaps>& |
| 25 | Handler::getDbusObjMaps(uint16_t effecterId) const |
| 26 | { |
| 27 | return dbusObjMaps.at(effecterId); |
| 28 | } |
| 29 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 30 | void Handler::generateStateEffecterRepo(const Json& json, Repo& repo) |
| 31 | { |
| 32 | static const std::vector<Json> emptyList{}; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 33 | auto entries = json.value("entries", emptyList); |
| 34 | for (const auto& e : entries) |
| 35 | { |
| 36 | size_t pdrSize = 0; |
| 37 | auto effecters = e.value("effecters", emptyList); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 38 | for (const auto& effecter : effecters) |
| 39 | { |
| 40 | auto set = effecter.value("set", empty); |
| 41 | auto statesSize = set.value("size", 0); |
| 42 | if (!statesSize) |
| 43 | { |
| 44 | std::cerr << "Malformed PDR JSON return " |
| 45 | "pdrEntry;- no state set " |
| 46 | "info, TYPE=" |
| 47 | << PLDM_STATE_EFFECTER_PDR << "\n"; |
| 48 | throw InternalFailure(); |
| 49 | } |
| 50 | pdrSize += sizeof(state_effecter_possible_states) - |
| 51 | sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize); |
| 52 | } |
| 53 | pdrSize += sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t); |
| 54 | |
| 55 | std::vector<uint8_t> entry{}; |
| 56 | entry.resize(pdrSize); |
| 57 | |
| 58 | pldm_state_effecter_pdr* pdr = |
| 59 | reinterpret_cast<pldm_state_effecter_pdr*>(entry.data()); |
| 60 | pdr->hdr.record_handle = 0; |
| 61 | pdr->hdr.version = 1; |
| 62 | pdr->hdr.type = PLDM_STATE_EFFECTER_PDR; |
| 63 | pdr->hdr.record_change_num = 0; |
| 64 | pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr); |
| 65 | |
| 66 | pdr->terminus_handle = 0; |
| 67 | pdr->effecter_id = this->getNextEffecterId(); |
| 68 | pdr->entity_type = e.value("type", 0); |
| 69 | pdr->entity_instance = e.value("instance", 0); |
| 70 | pdr->container_id = e.value("container", 0); |
| 71 | pdr->effecter_semantic_id = 0; |
| 72 | pdr->effecter_init = PLDM_NO_INIT; |
| 73 | pdr->has_description_pdr = false; |
| 74 | pdr->composite_effecter_count = effecters.size(); |
| 75 | |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame^] | 76 | DbusMappings dbusMappings{}; |
| 77 | DbusValMaps dbusValMaps{}; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 78 | uint8_t* start = |
| 79 | entry.data() + sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t); |
| 80 | for (const auto& effecter : effecters) |
| 81 | { |
| 82 | auto set = effecter.value("set", empty); |
| 83 | state_effecter_possible_states* possibleStates = |
| 84 | reinterpret_cast<state_effecter_possible_states*>(start); |
| 85 | possibleStates->state_set_id = set.value("id", 0); |
| 86 | possibleStates->possible_states_size = set.value("size", 0); |
| 87 | |
| 88 | start += sizeof(possibleStates->state_set_id) + |
| 89 | sizeof(possibleStates->possible_states_size); |
| 90 | static const std::vector<uint8_t> emptyStates{}; |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame^] | 91 | PossibleValues stateValues; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 92 | auto states = set.value("states", emptyStates); |
| 93 | for (const auto& state : states) |
| 94 | { |
| 95 | auto index = state / 8; |
| 96 | auto bit = state - (index * 8); |
| 97 | bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index); |
| 98 | bf->byte |= 1 << bit; |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame^] | 99 | stateValues.emplace_back(std::move(state)); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 100 | } |
| 101 | start += possibleStates->possible_states_size; |
| 102 | |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame^] | 103 | auto dbusEntry = effecter.value("dbus", empty); |
| 104 | auto objectPath = dbusEntry.value("path", ""); |
| 105 | auto interface = dbusEntry.value("interface", ""); |
| 106 | auto propertyName = dbusEntry.value("property_name", ""); |
| 107 | auto propertyType = dbusEntry.value("property_type", ""); |
| 108 | pldm::utils::DBusMapping dbusMapping{objectPath, interface, |
| 109 | propertyName, propertyType}; |
| 110 | dbusMappings.emplace_back(std::move(dbusMapping)); |
| 111 | |
| 112 | Json propValues = dbusEntry["property_values"]; |
| 113 | StatestoDbusVal dbusIdToValMap = |
| 114 | populateMapping(propertyType, propValues, stateValues); |
| 115 | if (!dbusIdToValMap.empty()) |
| 116 | { |
| 117 | dbusValMaps.emplace_back(std::move(dbusIdToValMap)); |
| 118 | } |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 119 | } |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame^] | 120 | |
| 121 | addDbusObjMaps( |
| 122 | pdr->effecter_id, |
| 123 | std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps))); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 124 | PdrEntry pdrEntry{}; |
| 125 | pdrEntry.data = entry.data(); |
| 126 | pdrEntry.size = pdrSize; |
| 127 | repo.addRecord(pdrEntry); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void Handler::generate(const std::string& dir, Repo& repo) |
| 132 | { |
| 133 | // A map of PDR type to a lambda that handles creation of that PDR type. |
| 134 | // The lambda essentially would parse the platform specific PDR JSONs to |
| 135 | // generate the PDR structures. This function iterates through the map to |
| 136 | // invoke all lambdas, so that all PDR types can be created. |
| 137 | const std::map<Type, std::function<void(const Json& json, Repo& repo)>> |
| 138 | generators = { |
| 139 | {PLDM_STATE_EFFECTER_PDR, [this](const auto& json, Repo& repo) { |
| 140 | generateStateEffecterRepo(json, repo); |
| 141 | }}}; |
| 142 | |
| 143 | Type pdrType{}; |
| 144 | for (const auto& dirEntry : fs::directory_iterator(dir)) |
| 145 | { |
| 146 | try |
| 147 | { |
| 148 | auto json = readJson(dirEntry.path().string()); |
| 149 | if (!json.empty()) |
| 150 | { |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame^] | 151 | auto effecterPDRs = json.value("effecterPDRs", empty); |
| 152 | for (const auto& effecter : effecterPDRs) |
| 153 | { |
| 154 | pdrType = effecter.value("pdrType", 0); |
| 155 | generators.at(pdrType)(effecter, repo); |
| 156 | } |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | catch (const InternalFailure& e) |
| 160 | { |
| 161 | std::cerr << "PDR config directory does not exist or empty, TYPE= " |
| 162 | << pdrType << "PATH= " << dirEntry |
| 163 | << " ERROR=" << e.what() << "\n"; |
| 164 | } |
| 165 | catch (const Json::exception& e) |
| 166 | { |
| 167 | std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType |
| 168 | << " ERROR=" << e.what() << "\n"; |
| 169 | pldm::utils::reportError( |
| 170 | "xyz.openbmc_project.bmc.pldm.InternalFailure"); |
| 171 | } |
| 172 | catch (const std::exception& e) |
| 173 | { |
| 174 | std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType |
| 175 | << " ERROR=" << e.what() << "\n"; |
| 176 | pldm::utils::reportError( |
| 177 | "xyz.openbmc_project.bmc.pldm.InternalFailure"); |
| 178 | } |
| 179 | } |
| 180 | } |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 181 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 182 | Response Handler::getPDR(const pldm_msg* request, size_t payloadLength) |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 183 | { |
| 184 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0); |
| 185 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 186 | |
| 187 | if (payloadLength != PLDM_GET_PDR_REQ_BYTES) |
| 188 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 189 | return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | uint32_t recordHandle{}; |
| 193 | uint32_t dataTransferHandle{}; |
| 194 | uint8_t transferOpFlag{}; |
| 195 | uint16_t reqSizeBytes{}; |
| 196 | uint16_t recordChangeNum{}; |
| 197 | |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 198 | auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle, |
| 199 | &dataTransferHandle, &transferOpFlag, |
| 200 | &reqSizeBytes, &recordChangeNum); |
| 201 | if (rc != PLDM_SUCCESS) |
| 202 | { |
| 203 | return CmdHandler::ccOnlyResponse(request, rc); |
| 204 | } |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 205 | |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 206 | uint16_t respSizeBytes{}; |
| 207 | uint8_t* recordData = nullptr; |
| 208 | try |
| 209 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 210 | pdr_utils::PdrEntry e; |
| 211 | auto record = pdr::getRecordByHandle(pdrRepo, recordHandle, e); |
| 212 | if (record == NULL) |
| 213 | { |
| 214 | return CmdHandler::ccOnlyResponse( |
| 215 | request, PLDM_PLATFORM_INVALID_RECORD_HANDLE); |
| 216 | } |
| 217 | |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 218 | if (reqSizeBytes) |
| 219 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 220 | respSizeBytes = e.size; |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 221 | if (respSizeBytes > reqSizeBytes) |
| 222 | { |
| 223 | respSizeBytes = reqSizeBytes; |
| 224 | } |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 225 | recordData = e.data; |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 226 | } |
| 227 | response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES + |
| 228 | respSizeBytes, |
| 229 | 0); |
| 230 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Deepak Kodihalli | 22b5a7d | 2020-03-17 23:28:41 -0500 | [diff] [blame] | 231 | rc = encode_get_pdr_resp( |
| 232 | request->hdr.instance_id, PLDM_SUCCESS, e.handle.nextRecordHandle, |
| 233 | 0, PLDM_START_AND_END, respSizeBytes, recordData, 0, responsePtr); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 234 | if (rc != PLDM_SUCCESS) |
| 235 | { |
| 236 | return ccOnlyResponse(request, rc); |
| 237 | } |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 238 | } |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 239 | catch (const std::exception& e) |
| 240 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 241 | std::cerr << "Error accessing PDR, HANDLE=" << recordHandle |
| 242 | << " ERROR=" << e.what() << "\n"; |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 243 | return CmdHandler::ccOnlyResponse(request, PLDM_ERROR); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 244 | } |
| 245 | return response; |
| 246 | } |
| 247 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 248 | Response Handler::setStateEffecterStates(const pldm_msg* request, |
| 249 | size_t payloadLength) |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 250 | { |
| 251 | Response response( |
| 252 | sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0); |
| 253 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 254 | uint16_t effecterId; |
| 255 | uint8_t compEffecterCnt; |
| 256 | constexpr auto maxCompositeEffecterCnt = 8; |
| 257 | std::vector<set_effecter_state_field> stateField(maxCompositeEffecterCnt, |
| 258 | {0, 0}); |
| 259 | |
| 260 | if ((payloadLength > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) || |
| 261 | (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) + |
| 262 | sizeof(set_effecter_state_field))) |
| 263 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 264 | return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | int rc = decode_set_state_effecter_states_req(request, payloadLength, |
| 268 | &effecterId, &compEffecterCnt, |
| 269 | stateField.data()); |
| 270 | |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 271 | if (rc != PLDM_SUCCESS) |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 272 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 273 | return CmdHandler::ccOnlyResponse(request, rc); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 274 | } |
| 275 | |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 276 | stateField.resize(compEffecterCnt); |
| 277 | const pldm::utils::DBusHandler dBusIntf; |
| 278 | rc = setStateEffecterStatesHandler<pldm::utils::DBusHandler>( |
| 279 | dBusIntf, effecterId, stateField); |
| 280 | if (rc != PLDM_SUCCESS) |
| 281 | { |
| 282 | return CmdHandler::ccOnlyResponse(request, rc); |
| 283 | } |
| 284 | |
| 285 | rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc, |
| 286 | responsePtr); |
| 287 | if (rc != PLDM_SUCCESS) |
| 288 | { |
| 289 | return ccOnlyResponse(request, rc); |
| 290 | } |
| 291 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 292 | return response; |
| 293 | } |
| 294 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 295 | } // namespace platform |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 296 | } // namespace responder |
| 297 | } // namespace pldm |