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