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