Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 1 | #include "fru.hpp" |
| 2 | |
Pavithra Barithaya | 47180ac | 2020-10-28 02:12:05 -0500 | [diff] [blame] | 3 | #include "libpldm/entity.h" |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 4 | #include "libpldm/utils.h" |
| 5 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 6 | #include "common/utils.hpp" |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 7 | |
| 8 | #include <systemd/sd-journal.h> |
| 9 | |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 10 | #include <sdbusplus/bus.hpp> |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 11 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 12 | #include <iostream> |
| 13 | #include <set> |
George Liu | 077fea2 | 2020-04-08 16:47:14 +0800 | [diff] [blame] | 14 | |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 15 | namespace pldm |
| 16 | { |
| 17 | |
| 18 | namespace responder |
| 19 | { |
| 20 | |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 21 | void FruImpl::buildFRUTable() |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 22 | { |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 23 | |
| 24 | if (isBuilt) |
| 25 | { |
| 26 | return; |
| 27 | } |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 28 | |
Tom Joseph | f007633 | 2020-02-06 10:18:50 +0530 | [diff] [blame] | 29 | fru_parser::DBusLookupInfo dbusInfo; |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 30 | // Read the all the inventory D-Bus objects |
| 31 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 32 | dbus::ObjectValueTree objects; |
| 33 | |
| 34 | try |
| 35 | { |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 36 | dbusInfo = parser.inventoryLookup(); |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 37 | auto method = bus.new_method_call( |
| 38 | std::get<0>(dbusInfo).c_str(), std::get<1>(dbusInfo).c_str(), |
| 39 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 40 | auto reply = bus.call(method); |
| 41 | reply.read(objects); |
| 42 | } |
| 43 | catch (const std::exception& e) |
| 44 | { |
| 45 | std::cerr << "Look up of inventory objects failed and PLDM FRU table " |
Brad Bishop | 0b54073 | 2021-08-18 09:42:41 -0400 | [diff] [blame^] | 46 | "creation failed\n"; |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 47 | return; |
| 48 | } |
| 49 | |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 50 | auto itemIntfsLookup = std::get<2>(dbusInfo); |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 51 | |
| 52 | for (const auto& object : objects) |
| 53 | { |
| 54 | const auto& interfaces = object.second; |
| 55 | |
| 56 | for (const auto& interface : interfaces) |
| 57 | { |
| 58 | if (itemIntfsLookup.find(interface.first) != itemIntfsLookup.end()) |
| 59 | { |
| 60 | // An exception will be thrown by getRecordInfo, if the item |
| 61 | // D-Bus interface name specified in FRU_Master.json does |
| 62 | // not have corresponding config jsons |
| 63 | try |
| 64 | { |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 65 | pldm_entity entity{}; |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 66 | entity.entity_type = parser.getEntityType(interface.first); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 67 | pldm_entity_node* parent = nullptr; |
| 68 | auto parentObj = pldm::utils::findParent(object.first.str); |
| 69 | // To add a FRU to the entity association tree, we need to |
| 70 | // determine if the FRU has a parent (D-Bus object). For eg |
| 71 | // /system/backplane's parent is /system. /system has no |
| 72 | // parent. Some D-Bus pathnames might just be namespaces |
| 73 | // (not D-Bus objects), so we need to iterate upwards until |
| 74 | // a parent is found, or we reach the root ("/"). |
| 75 | // Parents are always added first before children in the |
| 76 | // entity association tree. We're relying on the fact that |
| 77 | // the std::map containing object paths from the |
| 78 | // GetManagedObjects call will have a sorted pathname list. |
| 79 | do |
| 80 | { |
| 81 | auto iter = objToEntityNode.find(parentObj); |
| 82 | if (iter != objToEntityNode.end()) |
| 83 | { |
| 84 | parent = iter->second; |
| 85 | break; |
| 86 | } |
| 87 | parentObj = pldm::utils::findParent(parentObj); |
| 88 | } while (parentObj != "/"); |
| 89 | |
| 90 | auto node = pldm_entity_association_tree_add( |
George Liu | 64a8f0f | 2021-06-12 10:56:11 +0800 | [diff] [blame] | 91 | entityTree, &entity, 0xFFFF, parent, |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 92 | PLDM_ENTITY_ASSOCIAION_PHYSICAL); |
| 93 | objToEntityNode[object.first.str] = node; |
| 94 | |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 95 | auto recordInfos = parser.getRecordInfo(interface.first); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 96 | populateRecords(interfaces, recordInfos, entity); |
George Liu | c4ea6a9 | 2020-07-14 15:48:44 +0800 | [diff] [blame] | 97 | |
| 98 | associatedEntityMap.emplace(object.first, entity); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 99 | break; |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 100 | } |
| 101 | catch (const std::exception& e) |
| 102 | { |
| 103 | std::cout << "Config JSONs missing for the item " |
| 104 | "interface type, interface = " |
| 105 | << interface.first << "\n"; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 112 | pldm_entity_association_pdr_add(entityTree, pdrRepo, false); |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame] | 113 | // save a copy of bmc's entity association tree |
| 114 | pldm_entity_association_tree_copy_root(entityTree, bmcEntityTree); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 115 | |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 116 | if (table.size()) |
| 117 | { |
| 118 | padBytes = utils::getNumPadBytes(table.size()); |
| 119 | table.resize(table.size() + padBytes, 0); |
| 120 | |
| 121 | // Calculate the checksum |
George Liu | 077fea2 | 2020-04-08 16:47:14 +0800 | [diff] [blame] | 122 | checksum = crc32(table.data(), table.size()); |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 123 | } |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 124 | isBuilt = true; |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 125 | } |
Pavithra Barithaya | 47180ac | 2020-10-28 02:12:05 -0500 | [diff] [blame] | 126 | std::string FruImpl::populatefwVersion() |
| 127 | { |
| 128 | static constexpr auto fwFunctionalObjPath = |
| 129 | "/xyz/openbmc_project/software/functional"; |
| 130 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 131 | std::string currentBmcVersion; |
| 132 | try |
| 133 | { |
| 134 | auto method = |
| 135 | bus.new_method_call(pldm::utils::mapperService, fwFunctionalObjPath, |
| 136 | pldm::utils::dbusProperties, "Get"); |
| 137 | method.append("xyz.openbmc_project.Association", "endpoints"); |
| 138 | std::variant<std::vector<std::string>> paths; |
| 139 | auto reply = bus.call(method); |
| 140 | reply.read(paths); |
| 141 | auto fwRunningVersion = std::get<std::vector<std::string>>(paths)[0]; |
| 142 | constexpr auto versionIntf = "xyz.openbmc_project.Software.Version"; |
| 143 | auto version = pldm::utils::DBusHandler().getDbusPropertyVariant( |
| 144 | fwRunningVersion.c_str(), "Version", versionIntf); |
| 145 | currentBmcVersion = std::get<std::string>(version); |
| 146 | } |
| 147 | catch (const std::exception& e) |
| 148 | { |
| 149 | std::cerr << "failed to make a d-bus call " |
| 150 | "Asociation, ERROR= " |
| 151 | << e.what() << "\n"; |
| 152 | return {}; |
| 153 | } |
| 154 | return currentBmcVersion; |
| 155 | } |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 156 | void FruImpl::populateRecords( |
| 157 | const pldm::responder::dbus::InterfaceMap& interfaces, |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 158 | const fru_parser::FruRecordInfos& recordInfos, const pldm_entity& entity) |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 159 | { |
| 160 | // recordSetIdentifier for the FRU will be set when the first record gets |
| 161 | // added for the FRU |
| 162 | uint16_t recordSetIdentifier = 0; |
| 163 | auto numRecsCount = numRecs; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 164 | static uint32_t bmc_record_handle = 0; |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 165 | |
| 166 | for (auto const& [recType, encType, fieldInfos] : recordInfos) |
| 167 | { |
| 168 | std::vector<uint8_t> tlvs; |
| 169 | uint8_t numFRUFields = 0; |
| 170 | for (auto const& [intf, prop, propType, fieldTypeNum] : fieldInfos) |
| 171 | { |
Pavithra Barithaya | 47180ac | 2020-10-28 02:12:05 -0500 | [diff] [blame] | 172 | |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 173 | try |
| 174 | { |
Pavithra Barithaya | 47180ac | 2020-10-28 02:12:05 -0500 | [diff] [blame] | 175 | pldm::responder::dbus::Value propValue; |
| 176 | if (entity.entity_type == PLDM_ENTITY_SYSTEM_LOGICAL && |
| 177 | prop == "Version") |
| 178 | { |
| 179 | propValue = populatefwVersion(); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | propValue = interfaces.at(intf).at(prop); |
| 184 | } |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 185 | if (propType == "bytearray") |
| 186 | { |
| 187 | auto byteArray = std::get<std::vector<uint8_t>>(propValue); |
| 188 | if (!byteArray.size()) |
| 189 | { |
| 190 | continue; |
| 191 | } |
| 192 | |
| 193 | numFRUFields++; |
| 194 | tlvs.emplace_back(fieldTypeNum); |
| 195 | tlvs.emplace_back(byteArray.size()); |
| 196 | std::move(std::begin(byteArray), std::end(byteArray), |
| 197 | std::back_inserter(tlvs)); |
| 198 | } |
| 199 | else if (propType == "string") |
| 200 | { |
| 201 | auto str = std::get<std::string>(propValue); |
| 202 | if (!str.size()) |
| 203 | { |
| 204 | continue; |
| 205 | } |
| 206 | |
| 207 | numFRUFields++; |
| 208 | tlvs.emplace_back(fieldTypeNum); |
| 209 | tlvs.emplace_back(str.size()); |
| 210 | std::move(std::begin(str), std::end(str), |
| 211 | std::back_inserter(tlvs)); |
| 212 | } |
| 213 | } |
| 214 | catch (const std::out_of_range& e) |
| 215 | { |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 216 | continue; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (tlvs.size()) |
| 221 | { |
| 222 | if (numRecs == numRecsCount) |
| 223 | { |
| 224 | recordSetIdentifier = nextRSI(); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 225 | bmc_record_handle = nextRecordHandle(); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 226 | pldm_pdr_add_fru_record_set( |
| 227 | pdrRepo, 0, recordSetIdentifier, entity.entity_type, |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 228 | entity.entity_instance_num, entity.entity_container_id, |
| 229 | bmc_record_handle); |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 230 | } |
| 231 | auto curSize = table.size(); |
| 232 | table.resize(curSize + recHeaderSize + tlvs.size()); |
| 233 | encode_fru_record(table.data(), table.size(), &curSize, |
| 234 | recordSetIdentifier, recType, numFRUFields, |
| 235 | encType, tlvs.data(), tlvs.size()); |
| 236 | numRecs++; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void FruImpl::getFRUTable(Response& response) |
| 242 | { |
| 243 | auto hdrSize = response.size(); |
| 244 | |
| 245 | response.resize(hdrSize + table.size() + sizeof(checksum), 0); |
| 246 | std::copy(table.begin(), table.end(), response.begin() + hdrSize); |
| 247 | |
| 248 | // Copy the checksum to response data |
| 249 | auto iter = response.begin() + hdrSize + table.size(); |
| 250 | std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum), |
| 251 | iter); |
| 252 | } |
| 253 | |
John Wang | 9e82ad1 | 2020-06-12 10:53:32 +0800 | [diff] [blame] | 254 | int FruImpl::getFRURecordByOption(std::vector<uint8_t>& fruData, |
| 255 | uint16_t /* fruTableHandle */, |
| 256 | uint16_t recordSetIdentifer, |
| 257 | uint8_t recordType, uint8_t fieldType) |
| 258 | { |
| 259 | // FRU table is built lazily, build if not done. |
| 260 | buildFRUTable(); |
| 261 | |
| 262 | /* 7 is sizeof(checksum,4) + padBytesMax(3) |
| 263 | * We can not know size of the record table got by options in advance, but |
| 264 | * it must be less than the source table. So it's safe to use sizeof the |
| 265 | * source table + 7 as the buffer length |
| 266 | */ |
| 267 | size_t recordTableSize = table.size() - padBytes + 7; |
| 268 | fruData.resize(recordTableSize, 0); |
| 269 | |
| 270 | get_fru_record_by_option(table.data(), table.size() - padBytes, |
| 271 | fruData.data(), &recordTableSize, |
| 272 | recordSetIdentifer, recordType, fieldType); |
| 273 | |
| 274 | if (recordTableSize == 0) |
| 275 | { |
| 276 | return PLDM_FRU_DATA_STRUCTURE_TABLE_UNAVAILABLE; |
| 277 | } |
| 278 | |
| 279 | auto pads = utils::getNumPadBytes(recordTableSize); |
| 280 | auto sum = crc32(fruData.data(), recordTableSize + pads); |
| 281 | |
| 282 | auto iter = fruData.begin() + recordTableSize + pads; |
| 283 | std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum), |
| 284 | iter); |
| 285 | fruData.resize(recordTableSize + pads + sizeof(sum)); |
| 286 | |
| 287 | return PLDM_SUCCESS; |
| 288 | } |
| 289 | |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 290 | namespace fru |
| 291 | { |
| 292 | |
| 293 | Response Handler::getFRURecordTableMetadata(const pldm_msg* request, |
| 294 | size_t /*payloadLength*/) |
| 295 | { |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 296 | // FRU table is built lazily, build if not done. |
| 297 | buildFRUTable(); |
| 298 | |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 299 | constexpr uint8_t major = 0x01; |
| 300 | constexpr uint8_t minor = 0x00; |
| 301 | constexpr uint32_t maxSize = 0xFFFFFFFF; |
| 302 | |
| 303 | Response response(sizeof(pldm_msg_hdr) + |
| 304 | PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES, |
| 305 | 0); |
| 306 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 307 | |
| 308 | auto rc = encode_get_fru_record_table_metadata_resp( |
| 309 | request->hdr.instance_id, PLDM_SUCCESS, major, minor, maxSize, |
| 310 | impl.size(), impl.numRSI(), impl.numRecords(), impl.checkSum(), |
| 311 | responsePtr); |
| 312 | if (rc != PLDM_SUCCESS) |
| 313 | { |
| 314 | return ccOnlyResponse(request, rc); |
| 315 | } |
| 316 | |
| 317 | return response; |
| 318 | } |
| 319 | |
| 320 | Response Handler::getFRURecordTable(const pldm_msg* request, |
| 321 | size_t payloadLength) |
| 322 | { |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 323 | // FRU table is built lazily, build if not done. |
| 324 | buildFRUTable(); |
| 325 | |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 326 | if (payloadLength != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES) |
| 327 | { |
| 328 | return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); |
| 329 | } |
| 330 | |
| 331 | Response response( |
| 332 | sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES, 0); |
| 333 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 334 | |
| 335 | auto rc = |
| 336 | encode_get_fru_record_table_resp(request->hdr.instance_id, PLDM_SUCCESS, |
| 337 | 0, PLDM_START_AND_END, responsePtr); |
| 338 | if (rc != PLDM_SUCCESS) |
| 339 | { |
| 340 | return ccOnlyResponse(request, rc); |
| 341 | } |
| 342 | |
| 343 | impl.getFRUTable(response); |
| 344 | |
| 345 | return response; |
| 346 | } |
| 347 | |
John Wang | 9e82ad1 | 2020-06-12 10:53:32 +0800 | [diff] [blame] | 348 | Response Handler::getFRURecordByOption(const pldm_msg* request, |
| 349 | size_t payloadLength) |
| 350 | { |
| 351 | if (payloadLength != sizeof(pldm_get_fru_record_by_option_req)) |
| 352 | { |
| 353 | return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); |
| 354 | } |
| 355 | |
| 356 | uint32_t retDataTransferHandle{}; |
| 357 | uint16_t retFruTableHandle{}; |
| 358 | uint16_t retRecordSetIdentifier{}; |
| 359 | uint8_t retRecordType{}; |
| 360 | uint8_t retFieldType{}; |
| 361 | uint8_t retTransferOpFlag{}; |
| 362 | |
| 363 | auto rc = decode_get_fru_record_by_option_req( |
| 364 | request, payloadLength, &retDataTransferHandle, &retFruTableHandle, |
| 365 | &retRecordSetIdentifier, &retRecordType, &retFieldType, |
| 366 | &retTransferOpFlag); |
| 367 | |
| 368 | if (rc != PLDM_SUCCESS) |
| 369 | { |
| 370 | return ccOnlyResponse(request, rc); |
| 371 | } |
| 372 | |
| 373 | std::vector<uint8_t> fruData; |
| 374 | rc = impl.getFRURecordByOption(fruData, retFruTableHandle, |
| 375 | retRecordSetIdentifier, retRecordType, |
| 376 | retFieldType); |
| 377 | if (rc != PLDM_SUCCESS) |
| 378 | { |
| 379 | return ccOnlyResponse(request, rc); |
| 380 | } |
| 381 | |
| 382 | auto respPayloadLength = |
| 383 | PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + fruData.size(); |
| 384 | Response response(sizeof(pldm_msg_hdr) + respPayloadLength, 0); |
| 385 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 386 | |
| 387 | rc = encode_get_fru_record_by_option_resp( |
| 388 | request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END, |
| 389 | fruData.data(), fruData.size(), responsePtr, respPayloadLength); |
| 390 | |
| 391 | if (rc != PLDM_SUCCESS) |
| 392 | { |
| 393 | return ccOnlyResponse(request, rc); |
| 394 | } |
| 395 | |
| 396 | return response; |
| 397 | } |
| 398 | |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 399 | } // namespace fru |
| 400 | |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 401 | } // namespace responder |
| 402 | |
| 403 | } // namespace pldm |