1 #include "fru.hpp" 2 3 #include "libpldm/entity.h" 4 #include "libpldm/utils.h" 5 6 #include "common/utils.hpp" 7 8 #include <systemd/sd-journal.h> 9 10 #include <sdbusplus/bus.hpp> 11 12 #include <iostream> 13 #include <set> 14 15 namespace pldm 16 { 17 18 namespace responder 19 { 20 21 void FruImpl::buildFRUTable() 22 { 23 24 if (isBuilt) 25 { 26 return; 27 } 28 29 fru_parser::DBusLookupInfo dbusInfo; 30 // Read the all the inventory D-Bus objects 31 auto& bus = pldm::utils::DBusHandler::getBus(); 32 dbus::ObjectValueTree objects; 33 34 try 35 { 36 dbusInfo = parser.inventoryLookup(); 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 " 46 "creation failed"; 47 return; 48 } 49 50 auto itemIntfsLookup = std::get<2>(dbusInfo); 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 { 65 pldm_entity entity{}; 66 entity.entity_type = parser.getEntityType(interface.first); 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( 91 entityTree, &entity, parent, 92 PLDM_ENTITY_ASSOCIAION_PHYSICAL); 93 objToEntityNode[object.first.str] = node; 94 95 auto recordInfos = parser.getRecordInfo(interface.first); 96 populateRecords(interfaces, recordInfos, entity); 97 98 associatedEntityMap.emplace(object.first, entity); 99 break; 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 112 pldm_entity_association_pdr_add(entityTree, pdrRepo, false); 113 // save a copy of bmc's entity association tree 114 pldm_entity_association_tree_copy_root(entityTree, bmcEntityTree); 115 116 if (table.size()) 117 { 118 padBytes = utils::getNumPadBytes(table.size()); 119 table.resize(table.size() + padBytes, 0); 120 121 // Calculate the checksum 122 checksum = crc32(table.data(), table.size()); 123 } 124 isBuilt = true; 125 } 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 } 156 void FruImpl::populateRecords( 157 const pldm::responder::dbus::InterfaceMap& interfaces, 158 const fru_parser::FruRecordInfos& recordInfos, const pldm_entity& entity) 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; 164 165 for (auto const& [recType, encType, fieldInfos] : recordInfos) 166 { 167 std::vector<uint8_t> tlvs; 168 uint8_t numFRUFields = 0; 169 for (auto const& [intf, prop, propType, fieldTypeNum] : fieldInfos) 170 { 171 172 try 173 { 174 pldm::responder::dbus::Value propValue; 175 if (entity.entity_type == PLDM_ENTITY_SYSTEM_LOGICAL && 176 prop == "Version") 177 { 178 propValue = populatefwVersion(); 179 } 180 else 181 { 182 propValue = interfaces.at(intf).at(prop); 183 } 184 if (propType == "bytearray") 185 { 186 auto byteArray = std::get<std::vector<uint8_t>>(propValue); 187 if (!byteArray.size()) 188 { 189 continue; 190 } 191 192 numFRUFields++; 193 tlvs.emplace_back(fieldTypeNum); 194 tlvs.emplace_back(byteArray.size()); 195 std::move(std::begin(byteArray), std::end(byteArray), 196 std::back_inserter(tlvs)); 197 } 198 else if (propType == "string") 199 { 200 auto str = std::get<std::string>(propValue); 201 if (!str.size()) 202 { 203 continue; 204 } 205 206 numFRUFields++; 207 tlvs.emplace_back(fieldTypeNum); 208 tlvs.emplace_back(str.size()); 209 std::move(std::begin(str), std::end(str), 210 std::back_inserter(tlvs)); 211 } 212 } 213 catch (const std::out_of_range& e) 214 { 215 continue; 216 } 217 } 218 219 if (tlvs.size()) 220 { 221 if (numRecs == numRecsCount) 222 { 223 recordSetIdentifier = nextRSI(); 224 pldm_pdr_add_fru_record_set( 225 pdrRepo, 0, recordSetIdentifier, entity.entity_type, 226 entity.entity_instance_num, entity.entity_container_id); 227 } 228 auto curSize = table.size(); 229 table.resize(curSize + recHeaderSize + tlvs.size()); 230 encode_fru_record(table.data(), table.size(), &curSize, 231 recordSetIdentifier, recType, numFRUFields, 232 encType, tlvs.data(), tlvs.size()); 233 numRecs++; 234 } 235 } 236 } 237 238 void FruImpl::getFRUTable(Response& response) 239 { 240 auto hdrSize = response.size(); 241 242 response.resize(hdrSize + table.size() + sizeof(checksum), 0); 243 std::copy(table.begin(), table.end(), response.begin() + hdrSize); 244 245 // Copy the checksum to response data 246 auto iter = response.begin() + hdrSize + table.size(); 247 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum), 248 iter); 249 } 250 251 int FruImpl::getFRURecordByOption(std::vector<uint8_t>& fruData, 252 uint16_t /* fruTableHandle */, 253 uint16_t recordSetIdentifer, 254 uint8_t recordType, uint8_t fieldType) 255 { 256 // FRU table is built lazily, build if not done. 257 buildFRUTable(); 258 259 /* 7 is sizeof(checksum,4) + padBytesMax(3) 260 * We can not know size of the record table got by options in advance, but 261 * it must be less than the source table. So it's safe to use sizeof the 262 * source table + 7 as the buffer length 263 */ 264 size_t recordTableSize = table.size() - padBytes + 7; 265 fruData.resize(recordTableSize, 0); 266 267 get_fru_record_by_option(table.data(), table.size() - padBytes, 268 fruData.data(), &recordTableSize, 269 recordSetIdentifer, recordType, fieldType); 270 271 if (recordTableSize == 0) 272 { 273 return PLDM_FRU_DATA_STRUCTURE_TABLE_UNAVAILABLE; 274 } 275 276 auto pads = utils::getNumPadBytes(recordTableSize); 277 auto sum = crc32(fruData.data(), recordTableSize + pads); 278 279 auto iter = fruData.begin() + recordTableSize + pads; 280 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum), 281 iter); 282 fruData.resize(recordTableSize + pads + sizeof(sum)); 283 284 return PLDM_SUCCESS; 285 } 286 287 namespace fru 288 { 289 290 Response Handler::getFRURecordTableMetadata(const pldm_msg* request, 291 size_t /*payloadLength*/) 292 { 293 // FRU table is built lazily, build if not done. 294 buildFRUTable(); 295 296 constexpr uint8_t major = 0x01; 297 constexpr uint8_t minor = 0x00; 298 constexpr uint32_t maxSize = 0xFFFFFFFF; 299 300 Response response(sizeof(pldm_msg_hdr) + 301 PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES, 302 0); 303 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); 304 305 auto rc = encode_get_fru_record_table_metadata_resp( 306 request->hdr.instance_id, PLDM_SUCCESS, major, minor, maxSize, 307 impl.size(), impl.numRSI(), impl.numRecords(), impl.checkSum(), 308 responsePtr); 309 if (rc != PLDM_SUCCESS) 310 { 311 return ccOnlyResponse(request, rc); 312 } 313 314 return response; 315 } 316 317 Response Handler::getFRURecordTable(const pldm_msg* request, 318 size_t payloadLength) 319 { 320 // FRU table is built lazily, build if not done. 321 buildFRUTable(); 322 323 if (payloadLength != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES) 324 { 325 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); 326 } 327 328 Response response( 329 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES, 0); 330 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); 331 332 auto rc = 333 encode_get_fru_record_table_resp(request->hdr.instance_id, PLDM_SUCCESS, 334 0, PLDM_START_AND_END, responsePtr); 335 if (rc != PLDM_SUCCESS) 336 { 337 return ccOnlyResponse(request, rc); 338 } 339 340 impl.getFRUTable(response); 341 342 return response; 343 } 344 345 Response Handler::getFRURecordByOption(const pldm_msg* request, 346 size_t payloadLength) 347 { 348 if (payloadLength != sizeof(pldm_get_fru_record_by_option_req)) 349 { 350 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); 351 } 352 353 uint32_t retDataTransferHandle{}; 354 uint16_t retFruTableHandle{}; 355 uint16_t retRecordSetIdentifier{}; 356 uint8_t retRecordType{}; 357 uint8_t retFieldType{}; 358 uint8_t retTransferOpFlag{}; 359 360 auto rc = decode_get_fru_record_by_option_req( 361 request, payloadLength, &retDataTransferHandle, &retFruTableHandle, 362 &retRecordSetIdentifier, &retRecordType, &retFieldType, 363 &retTransferOpFlag); 364 365 if (rc != PLDM_SUCCESS) 366 { 367 return ccOnlyResponse(request, rc); 368 } 369 370 std::vector<uint8_t> fruData; 371 rc = impl.getFRURecordByOption(fruData, retFruTableHandle, 372 retRecordSetIdentifier, retRecordType, 373 retFieldType); 374 if (rc != PLDM_SUCCESS) 375 { 376 return ccOnlyResponse(request, rc); 377 } 378 379 auto respPayloadLength = 380 PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + fruData.size(); 381 Response response(sizeof(pldm_msg_hdr) + respPayloadLength, 0); 382 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); 383 384 rc = encode_get_fru_record_by_option_resp( 385 request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END, 386 fruData.data(), fruData.size(), responsePtr, respPayloadLength); 387 388 if (rc != PLDM_SUCCESS) 389 { 390 return ccOnlyResponse(request, rc); 391 } 392 393 return response; 394 } 395 396 } // namespace fru 397 398 } // namespace responder 399 400 } // namespace pldm 401