Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 1 | #include "fru.hpp" |
| 2 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 3 | #include "common/utils.hpp" |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 4 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 5 | #include <libpldm/entity.h> |
| 6 | #include <libpldm/utils.h> |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 7 | #include <systemd/sd-journal.h> |
| 8 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 9 | #include <phosphor-logging/lg2.hpp> |
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 | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 12 | #include <optional> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 13 | #include <set> |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 14 | #include <stack> |
George Liu | 077fea2 | 2020-04-08 16:47:14 +0800 | [diff] [blame] | 15 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 16 | PHOSPHOR_LOG2_USING; |
| 17 | |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 18 | namespace pldm |
| 19 | { |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 20 | namespace responder |
| 21 | { |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 22 | |
| 23 | constexpr auto root = "/xyz/openbmc_project/inventory/"; |
| 24 | |
| 25 | std::optional<pldm_entity> |
| 26 | FruImpl::getEntityByObjectPath(const dbus::InterfaceMap& intfMaps) |
| 27 | { |
| 28 | for (const auto& intfMap : intfMaps) |
| 29 | { |
| 30 | try |
| 31 | { |
| 32 | pldm_entity entity{}; |
| 33 | entity.entity_type = parser.getEntityType(intfMap.first); |
| 34 | return entity; |
| 35 | } |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 36 | catch (const std::exception&) |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 37 | { |
| 38 | continue; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return std::nullopt; |
| 43 | } |
| 44 | |
| 45 | void FruImpl::updateAssociationTree(const dbus::ObjectValueTree& objects, |
| 46 | const std::string& path) |
| 47 | { |
| 48 | if (path.find(root) == std::string::npos) |
| 49 | { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | std::stack<std::string> tmpObjPaths{}; |
| 54 | tmpObjPaths.emplace(path); |
| 55 | |
| 56 | auto obj = pldm::utils::findParent(path); |
| 57 | while ((obj + '/') != root) |
| 58 | { |
| 59 | tmpObjPaths.emplace(obj); |
| 60 | obj = pldm::utils::findParent(obj); |
| 61 | } |
| 62 | |
| 63 | std::stack<std::string> tmpObj = tmpObjPaths; |
| 64 | while (!tmpObj.empty()) |
| 65 | { |
| 66 | std::string s = tmpObj.top(); |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 67 | tmpObj.pop(); |
| 68 | } |
| 69 | // Update pldm entity to assocition tree |
| 70 | std::string prePath = tmpObjPaths.top(); |
| 71 | while (!tmpObjPaths.empty()) |
| 72 | { |
| 73 | std::string currPath = tmpObjPaths.top(); |
| 74 | tmpObjPaths.pop(); |
| 75 | |
| 76 | do |
| 77 | { |
| 78 | if (objToEntityNode.contains(currPath)) |
| 79 | { |
| 80 | pldm_entity node = |
| 81 | pldm_entity_extract(objToEntityNode.at(currPath)); |
Manojkiran Eda | 8e715ae | 2021-07-04 21:46:43 +0530 | [diff] [blame] | 82 | if (pldm_entity_association_tree_find_with_locality( |
| 83 | entityTree, &node, false)) |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 84 | { |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | if (!objects.contains(currPath)) |
| 91 | { |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | auto entityPtr = getEntityByObjectPath(objects.at(currPath)); |
| 96 | if (!entityPtr) |
| 97 | { |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | pldm_entity entity = *entityPtr; |
| 102 | |
Pavithra Barithaya | 15b9411 | 2024-04-10 02:42:12 -0500 | [diff] [blame] | 103 | for (const auto& it : objToEntityNode) |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 104 | { |
| 105 | pldm_entity node = pldm_entity_extract(it.second); |
| 106 | if (node.entity_type == entity.entity_type) |
| 107 | { |
| 108 | entity.entity_instance_num = node.entity_instance_num + |
| 109 | 1; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (currPath == prePath) |
| 115 | { |
Manojkiran Eda | 8e715ae | 2021-07-04 21:46:43 +0530 | [diff] [blame] | 116 | auto node = pldm_entity_association_tree_add_entity( |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 117 | entityTree, &entity, 0xFFFF, nullptr, |
Manojkiran Eda | 8e715ae | 2021-07-04 21:46:43 +0530 | [diff] [blame] | 118 | PLDM_ENTITY_ASSOCIAION_PHYSICAL, false, true, 0xFFFF); |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 119 | objToEntityNode[currPath] = node; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | if (objToEntityNode.contains(prePath)) |
| 124 | { |
Manojkiran Eda | 8e715ae | 2021-07-04 21:46:43 +0530 | [diff] [blame] | 125 | auto node = pldm_entity_association_tree_add_entity( |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 126 | entityTree, &entity, 0xFFFF, |
| 127 | objToEntityNode[prePath], |
Manojkiran Eda | 8e715ae | 2021-07-04 21:46:43 +0530 | [diff] [blame] | 128 | PLDM_ENTITY_ASSOCIAION_PHYSICAL, false, true, |
| 129 | 0xFFFF); |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 130 | objToEntityNode[currPath] = node; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } while (0); |
| 135 | |
| 136 | prePath = currPath; |
| 137 | } |
| 138 | } |
| 139 | |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 140 | void FruImpl::buildFRUTable() |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 141 | { |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 142 | if (isBuilt) |
| 143 | { |
| 144 | return; |
| 145 | } |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 146 | |
Tom Joseph | f007633 | 2020-02-06 10:18:50 +0530 | [diff] [blame] | 147 | fru_parser::DBusLookupInfo dbusInfo; |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 148 | |
| 149 | try |
| 150 | { |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 151 | dbusInfo = parser.inventoryLookup(); |
Riya Dixit | 754041d | 2024-02-20 06:15:49 -0600 | [diff] [blame] | 152 | objects = pldm::utils::DBusHandler::getInventoryObjects< |
| 153 | pldm::utils::DBusHandler>(); |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 154 | } |
| 155 | catch (const std::exception& e) |
| 156 | { |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 157 | error( |
| 158 | "Failed to build FRU table due to inventory lookup, error - {ERROR}", |
| 159 | "ERROR", e); |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 163 | auto itemIntfsLookup = std::get<2>(dbusInfo); |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 164 | |
| 165 | for (const auto& object : objects) |
| 166 | { |
| 167 | const auto& interfaces = object.second; |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 168 | for (const auto& interface : interfaces) |
| 169 | { |
Sagar Srinivas | 06f9b29 | 2024-03-31 11:35:28 -0500 | [diff] [blame] | 170 | if (itemIntfsLookup.contains(interface.first)) |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 171 | { |
Kamalkumar Patel | 2a103ef | 2024-02-14 02:58:08 -0600 | [diff] [blame] | 172 | // checking fru present property is available or not. |
| 173 | if (!pldm::utils::checkForFruPresence(object.first.str)) |
| 174 | { |
| 175 | continue; |
| 176 | } |
| 177 | |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 178 | // An exception will be thrown by getRecordInfo, if the item |
| 179 | // D-Bus interface name specified in FRU_Master.json does |
| 180 | // not have corresponding config jsons |
| 181 | try |
| 182 | { |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 183 | updateAssociationTree(objects, object.first.str); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 184 | pldm_entity entity{}; |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 185 | if (objToEntityNode.contains(object.first.str)) |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 186 | { |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 187 | pldm_entity_node* node = |
| 188 | objToEntityNode.at(object.first.str); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 189 | |
George Liu | 5bfb0dc | 2021-05-01 14:28:41 +0800 | [diff] [blame] | 190 | entity = pldm_entity_extract(node); |
| 191 | } |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 192 | |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 193 | auto recordInfos = parser.getRecordInfo(interface.first); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 194 | populateRecords(interfaces, recordInfos, entity); |
George Liu | c4ea6a9 | 2020-07-14 15:48:44 +0800 | [diff] [blame] | 195 | |
| 196 | associatedEntityMap.emplace(object.first, entity); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 197 | break; |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 198 | } |
| 199 | catch (const std::exception& e) |
| 200 | { |
Riya Dixit | 797f338 | 2023-08-22 22:27:51 -0500 | [diff] [blame] | 201 | error( |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 202 | "Config JSONs missing for the item '{INTERFACE}', error - {ERROR}", |
| 203 | "INTERFACE", interface.first, "ERROR", e); |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
Andrew Jeffery | b0ba34d | 2023-07-17 17:47:16 +0930 | [diff] [blame] | 210 | int rc = pldm_entity_association_pdr_add_check(entityTree, pdrRepo, false, |
| 211 | TERMINUS_HANDLE); |
| 212 | if (rc < 0) |
| 213 | { |
| 214 | // pldm_entity_assocation_pdr_add() assert()ed on failure |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 215 | error("Failed to add PLDM entity association PDR, response code '{RC}'", |
| 216 | "RC", rc); |
Andrew Jeffery | b0ba34d | 2023-07-17 17:47:16 +0930 | [diff] [blame] | 217 | throw std::runtime_error("Failed to add PLDM entity association PDR"); |
| 218 | } |
| 219 | |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame] | 220 | // save a copy of bmc's entity association tree |
| 221 | pldm_entity_association_tree_copy_root(entityTree, bmcEntityTree); |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 222 | |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 223 | isBuilt = true; |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 224 | } |
Pavithra Barithaya | 47180ac | 2020-10-28 02:12:05 -0500 | [diff] [blame] | 225 | std::string FruImpl::populatefwVersion() |
| 226 | { |
| 227 | static constexpr auto fwFunctionalObjPath = |
| 228 | "/xyz/openbmc_project/software/functional"; |
| 229 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 230 | std::string currentBmcVersion; |
| 231 | try |
| 232 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 233 | auto method = bus.new_method_call(pldm::utils::mapperService, |
| 234 | fwFunctionalObjPath, |
| 235 | pldm::utils::dbusProperties, "Get"); |
Pavithra Barithaya | 47180ac | 2020-10-28 02:12:05 -0500 | [diff] [blame] | 236 | method.append("xyz.openbmc_project.Association", "endpoints"); |
| 237 | std::variant<std::vector<std::string>> paths; |
vkaverap@in.ibm.com | 91a092f | 2023-09-18 23:39:44 -0500 | [diff] [blame] | 238 | auto reply = bus.call(method, dbusTimeout); |
Pavithra Barithaya | 47180ac | 2020-10-28 02:12:05 -0500 | [diff] [blame] | 239 | reply.read(paths); |
| 240 | auto fwRunningVersion = std::get<std::vector<std::string>>(paths)[0]; |
| 241 | constexpr auto versionIntf = "xyz.openbmc_project.Software.Version"; |
| 242 | auto version = pldm::utils::DBusHandler().getDbusPropertyVariant( |
| 243 | fwRunningVersion.c_str(), "Version", versionIntf); |
| 244 | currentBmcVersion = std::get<std::string>(version); |
| 245 | } |
| 246 | catch (const std::exception& e) |
| 247 | { |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 248 | error("Failed to make a d-bus call Asociation, error - {ERROR}", |
| 249 | "ERROR", e); |
Pavithra Barithaya | 47180ac | 2020-10-28 02:12:05 -0500 | [diff] [blame] | 250 | return {}; |
| 251 | } |
| 252 | return currentBmcVersion; |
| 253 | } |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 254 | void FruImpl::populateRecords( |
| 255 | const pldm::responder::dbus::InterfaceMap& interfaces, |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 256 | const fru_parser::FruRecordInfos& recordInfos, const pldm_entity& entity) |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 257 | { |
| 258 | // recordSetIdentifier for the FRU will be set when the first record gets |
| 259 | // added for the FRU |
| 260 | uint16_t recordSetIdentifier = 0; |
| 261 | auto numRecsCount = numRecs; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 262 | static uint32_t bmc_record_handle = 0; |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 263 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 264 | for (const auto& [recType, encType, fieldInfos] : recordInfos) |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 265 | { |
| 266 | std::vector<uint8_t> tlvs; |
| 267 | uint8_t numFRUFields = 0; |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 268 | for (const auto& [intf, prop, propType, fieldTypeNum] : fieldInfos) |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 269 | { |
| 270 | try |
| 271 | { |
Pavithra Barithaya | 47180ac | 2020-10-28 02:12:05 -0500 | [diff] [blame] | 272 | pldm::responder::dbus::Value propValue; |
Manojkiran Eda | 2b7c1bf | 2021-09-09 12:26:00 +0530 | [diff] [blame] | 273 | |
| 274 | // Assuming that 0 container Id is assigned to the System (as |
| 275 | // that should be the top most container as per dbus hierarchy) |
| 276 | if (entity.entity_container_id == 0 && prop == "Version") |
Pavithra Barithaya | 47180ac | 2020-10-28 02:12:05 -0500 | [diff] [blame] | 277 | { |
| 278 | propValue = populatefwVersion(); |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | propValue = interfaces.at(intf).at(prop); |
| 283 | } |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 284 | if (propType == "bytearray") |
| 285 | { |
| 286 | auto byteArray = std::get<std::vector<uint8_t>>(propValue); |
| 287 | if (!byteArray.size()) |
| 288 | { |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | numFRUFields++; |
| 293 | tlvs.emplace_back(fieldTypeNum); |
| 294 | tlvs.emplace_back(byteArray.size()); |
| 295 | std::move(std::begin(byteArray), std::end(byteArray), |
| 296 | std::back_inserter(tlvs)); |
| 297 | } |
| 298 | else if (propType == "string") |
| 299 | { |
| 300 | auto str = std::get<std::string>(propValue); |
| 301 | if (!str.size()) |
| 302 | { |
| 303 | continue; |
| 304 | } |
| 305 | |
| 306 | numFRUFields++; |
| 307 | tlvs.emplace_back(fieldTypeNum); |
| 308 | tlvs.emplace_back(str.size()); |
| 309 | std::move(std::begin(str), std::end(str), |
| 310 | std::back_inserter(tlvs)); |
| 311 | } |
| 312 | } |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 313 | catch (const std::out_of_range&) |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 314 | { |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 315 | continue; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | if (tlvs.size()) |
| 320 | { |
| 321 | if (numRecs == numRecsCount) |
| 322 | { |
| 323 | recordSetIdentifier = nextRSI(); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 324 | bmc_record_handle = nextRecordHandle(); |
Andrew Jeffery | 2e248e8 | 2023-07-03 17:23:24 +0930 | [diff] [blame] | 325 | int rc = pldm_pdr_add_fru_record_set_check( |
Manojkiran Eda | cc5f158 | 2021-09-29 17:03:06 +0530 | [diff] [blame] | 326 | pdrRepo, TERMINUS_HANDLE, recordSetIdentifier, |
| 327 | entity.entity_type, entity.entity_instance_num, |
Andrew Jeffery | 2e248e8 | 2023-07-03 17:23:24 +0930 | [diff] [blame] | 328 | entity.entity_container_id, &bmc_record_handle); |
| 329 | if (rc) |
| 330 | { |
| 331 | // pldm_pdr_add_fru_record_set() assert()ed on failure |
| 332 | throw std::runtime_error( |
| 333 | "Failed to add PDR FRU record set"); |
| 334 | } |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 335 | } |
| 336 | auto curSize = table.size(); |
| 337 | table.resize(curSize + recHeaderSize + tlvs.size()); |
| 338 | encode_fru_record(table.data(), table.size(), &curSize, |
| 339 | recordSetIdentifier, recType, numFRUFields, |
| 340 | encType, tlvs.data(), tlvs.size()); |
| 341 | numRecs++; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
Pavithra Barithaya | 9e6631e | 2024-01-22 00:46:32 -0600 | [diff] [blame] | 346 | std::vector<uint8_t> FruImpl::tableResize() |
| 347 | { |
| 348 | std::vector<uint8_t> tempTable; |
| 349 | |
| 350 | if (table.size()) |
| 351 | { |
| 352 | std::copy(table.begin(), table.end(), std::back_inserter(tempTable)); |
| 353 | padBytes = pldm::utils::getNumPadBytes(table.size()); |
| 354 | tempTable.resize(tempTable.size() + padBytes, 0); |
| 355 | } |
| 356 | return tempTable; |
| 357 | } |
| 358 | |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 359 | void FruImpl::getFRUTable(Response& response) |
| 360 | { |
| 361 | auto hdrSize = response.size(); |
Pavithra Barithaya | 9e6631e | 2024-01-22 00:46:32 -0600 | [diff] [blame] | 362 | std::vector<uint8_t> tempTable; |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 363 | |
Pavithra Barithaya | 9e6631e | 2024-01-22 00:46:32 -0600 | [diff] [blame] | 364 | if (table.size()) |
| 365 | { |
| 366 | tempTable = tableResize(); |
| 367 | checksum = crc32(tempTable.data(), tempTable.size()); |
| 368 | } |
| 369 | response.resize(hdrSize + tempTable.size() + sizeof(checksum), 0); |
| 370 | std::copy(tempTable.begin(), tempTable.end(), response.begin() + hdrSize); |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 371 | |
| 372 | // Copy the checksum to response data |
Pavithra Barithaya | 9e6631e | 2024-01-22 00:46:32 -0600 | [diff] [blame] | 373 | auto iter = response.begin() + hdrSize + tempTable.size(); |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 374 | std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum), |
| 375 | iter); |
| 376 | } |
| 377 | |
Pavithra Barithaya | 9e6631e | 2024-01-22 00:46:32 -0600 | [diff] [blame] | 378 | void FruImpl::getFRURecordTableMetadata() |
| 379 | { |
| 380 | std::vector<uint8_t> tempTable; |
| 381 | if (table.size()) |
| 382 | { |
| 383 | tempTable = tableResize(); |
| 384 | checksum = crc32(tempTable.data(), tempTable.size()); |
| 385 | } |
| 386 | } |
| 387 | |
John Wang | 9e82ad1 | 2020-06-12 10:53:32 +0800 | [diff] [blame] | 388 | int FruImpl::getFRURecordByOption(std::vector<uint8_t>& fruData, |
| 389 | uint16_t /* fruTableHandle */, |
| 390 | uint16_t recordSetIdentifer, |
| 391 | uint8_t recordType, uint8_t fieldType) |
| 392 | { |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 393 | using sum = uint32_t; |
| 394 | |
John Wang | 9e82ad1 | 2020-06-12 10:53:32 +0800 | [diff] [blame] | 395 | // FRU table is built lazily, build if not done. |
| 396 | buildFRUTable(); |
| 397 | |
| 398 | /* 7 is sizeof(checksum,4) + padBytesMax(3) |
| 399 | * We can not know size of the record table got by options in advance, but |
| 400 | * it must be less than the source table. So it's safe to use sizeof the |
| 401 | * source table + 7 as the buffer length |
| 402 | */ |
| 403 | size_t recordTableSize = table.size() - padBytes + 7; |
| 404 | fruData.resize(recordTableSize, 0); |
| 405 | |
Andrew Jeffery | 663783b | 2023-07-03 12:58:14 +0930 | [diff] [blame] | 406 | int rc = get_fru_record_by_option_check( |
| 407 | table.data(), table.size() - padBytes, fruData.data(), &recordTableSize, |
| 408 | recordSetIdentifer, recordType, fieldType); |
John Wang | 9e82ad1 | 2020-06-12 10:53:32 +0800 | [diff] [blame] | 409 | |
Andrew Jeffery | 663783b | 2023-07-03 12:58:14 +0930 | [diff] [blame] | 410 | if (rc != PLDM_SUCCESS || recordTableSize == 0) |
John Wang | 9e82ad1 | 2020-06-12 10:53:32 +0800 | [diff] [blame] | 411 | { |
| 412 | return PLDM_FRU_DATA_STRUCTURE_TABLE_UNAVAILABLE; |
| 413 | } |
| 414 | |
Sridevi Ramesh | eefe49b | 2022-06-27 11:51:02 -0500 | [diff] [blame] | 415 | auto pads = pldm::utils::getNumPadBytes(recordTableSize); |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 416 | crc32(fruData.data(), recordTableSize + pads); |
John Wang | 9e82ad1 | 2020-06-12 10:53:32 +0800 | [diff] [blame] | 417 | |
| 418 | auto iter = fruData.begin() + recordTableSize + pads; |
| 419 | std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum), |
| 420 | iter); |
| 421 | fruData.resize(recordTableSize + pads + sizeof(sum)); |
| 422 | |
| 423 | return PLDM_SUCCESS; |
| 424 | } |
| 425 | |
Pavithra Barithaya | a410c65 | 2021-07-22 01:32:47 -0500 | [diff] [blame] | 426 | int FruImpl::setFRUTable(const std::vector<uint8_t>& fruData) |
| 427 | { |
| 428 | auto record = |
| 429 | reinterpret_cast<const pldm_fru_record_data_format*>(fruData.data()); |
| 430 | if (record) |
| 431 | { |
| 432 | if (oemFruHandler && record->record_type == PLDM_FRU_RECORD_TYPE_OEM) |
| 433 | { |
| 434 | auto rc = oemFruHandler->processOEMFRUTable(fruData); |
| 435 | if (!rc) |
| 436 | { |
| 437 | return PLDM_SUCCESS; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | return PLDM_ERROR_UNSUPPORTED_PLDM_CMD; |
| 442 | } |
| 443 | |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 444 | namespace fru |
| 445 | { |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 446 | Response Handler::getFRURecordTableMetadata(const pldm_msg* request, |
| 447 | size_t /*payloadLength*/) |
| 448 | { |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 449 | // FRU table is built lazily, build if not done. |
| 450 | buildFRUTable(); |
| 451 | |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 452 | constexpr uint8_t major = 0x01; |
| 453 | constexpr uint8_t minor = 0x00; |
| 454 | constexpr uint32_t maxSize = 0xFFFFFFFF; |
| 455 | |
| 456 | Response response(sizeof(pldm_msg_hdr) + |
| 457 | PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES, |
| 458 | 0); |
| 459 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 460 | |
Pavithra Barithaya | 9e6631e | 2024-01-22 00:46:32 -0600 | [diff] [blame] | 461 | impl.getFRURecordTableMetadata(); |
| 462 | |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 463 | auto rc = encode_get_fru_record_table_metadata_resp( |
| 464 | request->hdr.instance_id, PLDM_SUCCESS, major, minor, maxSize, |
| 465 | impl.size(), impl.numRSI(), impl.numRecords(), impl.checkSum(), |
| 466 | responsePtr); |
| 467 | if (rc != PLDM_SUCCESS) |
| 468 | { |
| 469 | return ccOnlyResponse(request, rc); |
| 470 | } |
| 471 | |
| 472 | return response; |
| 473 | } |
| 474 | |
| 475 | Response Handler::getFRURecordTable(const pldm_msg* request, |
| 476 | size_t payloadLength) |
| 477 | { |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 478 | // FRU table is built lazily, build if not done. |
| 479 | buildFRUTable(); |
| 480 | |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 481 | if (payloadLength != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES) |
| 482 | { |
| 483 | return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); |
| 484 | } |
| 485 | |
| 486 | Response response( |
| 487 | sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES, 0); |
| 488 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 489 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 490 | auto rc = encode_get_fru_record_table_resp(request->hdr.instance_id, |
| 491 | PLDM_SUCCESS, 0, |
| 492 | PLDM_START_AND_END, responsePtr); |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 493 | if (rc != PLDM_SUCCESS) |
| 494 | { |
| 495 | return ccOnlyResponse(request, rc); |
| 496 | } |
| 497 | |
| 498 | impl.getFRUTable(response); |
| 499 | |
| 500 | return response; |
| 501 | } |
| 502 | |
John Wang | 9e82ad1 | 2020-06-12 10:53:32 +0800 | [diff] [blame] | 503 | Response Handler::getFRURecordByOption(const pldm_msg* request, |
| 504 | size_t payloadLength) |
| 505 | { |
| 506 | if (payloadLength != sizeof(pldm_get_fru_record_by_option_req)) |
| 507 | { |
| 508 | return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); |
| 509 | } |
| 510 | |
| 511 | uint32_t retDataTransferHandle{}; |
| 512 | uint16_t retFruTableHandle{}; |
| 513 | uint16_t retRecordSetIdentifier{}; |
| 514 | uint8_t retRecordType{}; |
| 515 | uint8_t retFieldType{}; |
| 516 | uint8_t retTransferOpFlag{}; |
| 517 | |
| 518 | auto rc = decode_get_fru_record_by_option_req( |
| 519 | request, payloadLength, &retDataTransferHandle, &retFruTableHandle, |
| 520 | &retRecordSetIdentifier, &retRecordType, &retFieldType, |
| 521 | &retTransferOpFlag); |
| 522 | |
| 523 | if (rc != PLDM_SUCCESS) |
| 524 | { |
| 525 | return ccOnlyResponse(request, rc); |
| 526 | } |
| 527 | |
| 528 | std::vector<uint8_t> fruData; |
| 529 | rc = impl.getFRURecordByOption(fruData, retFruTableHandle, |
| 530 | retRecordSetIdentifier, retRecordType, |
| 531 | retFieldType); |
| 532 | if (rc != PLDM_SUCCESS) |
| 533 | { |
| 534 | return ccOnlyResponse(request, rc); |
| 535 | } |
| 536 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 537 | auto respPayloadLength = PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + |
| 538 | fruData.size(); |
John Wang | 9e82ad1 | 2020-06-12 10:53:32 +0800 | [diff] [blame] | 539 | Response response(sizeof(pldm_msg_hdr) + respPayloadLength, 0); |
| 540 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 541 | |
| 542 | rc = encode_get_fru_record_by_option_resp( |
| 543 | request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END, |
| 544 | fruData.data(), fruData.size(), responsePtr, respPayloadLength); |
| 545 | |
| 546 | if (rc != PLDM_SUCCESS) |
| 547 | { |
| 548 | return ccOnlyResponse(request, rc); |
| 549 | } |
| 550 | |
| 551 | return response; |
| 552 | } |
| 553 | |
Pavithra Barithaya | a410c65 | 2021-07-22 01:32:47 -0500 | [diff] [blame] | 554 | Response Handler::setFRURecordTable(const pldm_msg* request, |
| 555 | size_t payloadLength) |
| 556 | { |
| 557 | uint32_t transferHandle{}; |
| 558 | uint8_t transferOpFlag{}; |
| 559 | struct variable_field fruData; |
| 560 | |
| 561 | auto rc = decode_set_fru_record_table_req( |
| 562 | request, payloadLength, &transferHandle, &transferOpFlag, &fruData); |
| 563 | |
| 564 | if (rc != PLDM_SUCCESS) |
| 565 | { |
| 566 | return ccOnlyResponse(request, rc); |
| 567 | } |
| 568 | |
| 569 | Table table(fruData.ptr, fruData.ptr + fruData.length); |
| 570 | rc = impl.setFRUTable(table); |
| 571 | if (rc != PLDM_SUCCESS) |
| 572 | { |
| 573 | return ccOnlyResponse(request, rc); |
| 574 | } |
| 575 | |
| 576 | Response response(sizeof(pldm_msg_hdr) + |
| 577 | PLDM_SET_FRU_RECORD_TABLE_RESP_BYTES); |
| 578 | struct pldm_msg* responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 579 | |
| 580 | rc = encode_set_fru_record_table_resp( |
| 581 | request->hdr.instance_id, PLDM_SUCCESS, 0 /* nextDataTransferHandle */, |
| 582 | response.size() - sizeof(pldm_msg_hdr), responsePtr); |
| 583 | |
| 584 | if (rc != PLDM_SUCCESS) |
| 585 | { |
| 586 | return ccOnlyResponse(request, rc); |
| 587 | } |
| 588 | |
| 589 | return response; |
| 590 | } |
| 591 | |
Deepak Kodihalli | e60c582 | 2019-10-23 03:26:15 -0500 | [diff] [blame] | 592 | } // namespace fru |
| 593 | |
Deepak Kodihalli | 70e8db0 | 2019-10-21 00:59:46 -0500 | [diff] [blame] | 594 | } // namespace responder |
| 595 | |
| 596 | } // namespace pldm |