Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 1 | #include "inventory_manager.hpp" |
| 2 | |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 3 | #include "common/utils.hpp" |
| 4 | #include "xyz/openbmc_project/Software/Version/server.hpp" |
| 5 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 6 | #include <libpldm/firmware_update.h> |
| 7 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 8 | #include <phosphor-logging/lg2.hpp> |
| 9 | |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 10 | #include <functional> |
| 11 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 12 | PHOSPHOR_LOG2_USING; |
| 13 | |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 14 | namespace pldm |
| 15 | { |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 16 | namespace fw_update |
| 17 | { |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 18 | void InventoryManager::discoverFDs(const std::vector<mctp_eid_t>& eids) |
| 19 | { |
| 20 | for (const auto& eid : eids) |
| 21 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 22 | auto instanceId = instanceIdDb.next(eid); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 23 | Request requestMsg(sizeof(pldm_msg_hdr) + |
| 24 | PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES); |
| 25 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 26 | auto rc = encode_query_device_identifiers_req( |
| 27 | instanceId, PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES, request); |
| 28 | if (rc) |
| 29 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 30 | instanceIdDb.free(eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 31 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 32 | "Failed to encode query device identifiers req for endpoint ID '{EID}', response code '{RC}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 33 | "EID", eid, "RC", rc); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 34 | continue; |
| 35 | } |
| 36 | |
| 37 | rc = handler.registerRequest( |
| 38 | eid, instanceId, PLDM_FWUP, PLDM_QUERY_DEVICE_IDENTIFIERS, |
| 39 | std::move(requestMsg), |
| 40 | std::move(std::bind_front(&InventoryManager::queryDeviceIdentifiers, |
| 41 | this))); |
| 42 | if (rc) |
| 43 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 44 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 45 | "Failed to send query device identifiers request for endpoint ID '{EID}', response code '{RC}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 46 | "EID", eid, "RC", rc); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void InventoryManager::queryDeviceIdentifiers(mctp_eid_t eid, |
| 52 | const pldm_msg* response, |
| 53 | size_t respMsgLen) |
| 54 | { |
| 55 | if (response == nullptr || !respMsgLen) |
| 56 | { |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 57 | error( |
| 58 | "No response received for query device identifiers for endpoint ID '{EID}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 59 | "EID", eid); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
| 63 | uint8_t completionCode = PLDM_SUCCESS; |
| 64 | uint32_t deviceIdentifiersLen = 0; |
| 65 | uint8_t descriptorCount = 0; |
| 66 | uint8_t* descriptorPtr = nullptr; |
| 67 | |
| 68 | auto rc = decode_query_device_identifiers_resp( |
| 69 | response, respMsgLen, &completionCode, &deviceIdentifiersLen, |
| 70 | &descriptorCount, &descriptorPtr); |
| 71 | if (rc) |
| 72 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 73 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 74 | "Failed to decode query device identifiers response for endpoint ID '{EID}' and descriptor count '{DESCRIPTOR_COUNT}', response code '{RC}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 75 | "EID", eid, "DESCRIPTOR_COUNT", descriptorCount, "RC", rc); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
| 79 | if (completionCode) |
| 80 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 81 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 82 | "Failed to query device identifiers response for endpoint ID '{EID}', completion code '{CC}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 83 | "EID", eid, "CC", completionCode); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
| 87 | Descriptors descriptors{}; |
| 88 | while (descriptorCount-- && (deviceIdentifiersLen > 0)) |
| 89 | { |
| 90 | uint16_t descriptorType = 0; |
| 91 | variable_field descriptorData{}; |
| 92 | |
| 93 | rc = decode_descriptor_type_length_value( |
| 94 | descriptorPtr, deviceIdentifiersLen, &descriptorType, |
| 95 | &descriptorData); |
| 96 | if (rc) |
| 97 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 98 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 99 | "Failed to decode descriptor type {TYPE}, length {LENGTH} and value for endpoint ID '{EID}', response code '{RC}'", |
| 100 | "TYPE", descriptorType, "LENGTH", deviceIdentifiersLen, "EID", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 101 | eid, "RC", rc); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
| 105 | if (descriptorType != PLDM_FWUP_VENDOR_DEFINED) |
| 106 | { |
| 107 | std::vector<uint8_t> descData( |
| 108 | descriptorData.ptr, descriptorData.ptr + descriptorData.length); |
| 109 | descriptors.emplace(descriptorType, std::move(descData)); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | uint8_t descriptorTitleStrType = 0; |
| 114 | variable_field descriptorTitleStr{}; |
| 115 | variable_field vendorDefinedDescriptorData{}; |
| 116 | |
| 117 | rc = decode_vendor_defined_descriptor_value( |
| 118 | descriptorData.ptr, descriptorData.length, |
| 119 | &descriptorTitleStrType, &descriptorTitleStr, |
| 120 | &vendorDefinedDescriptorData); |
| 121 | if (rc) |
| 122 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 123 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 124 | "Failed to decode vendor-defined descriptor value for endpoint ID '{EID}', response code '{RC}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 125 | "EID", eid, "RC", rc); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | |
| 129 | auto vendorDefinedDescriptorTitleStr = |
| 130 | utils::toString(descriptorTitleStr); |
| 131 | std::vector<uint8_t> vendorDescData( |
| 132 | vendorDefinedDescriptorData.ptr, |
| 133 | vendorDefinedDescriptorData.ptr + |
| 134 | vendorDefinedDescriptorData.length); |
| 135 | descriptors.emplace(descriptorType, |
| 136 | std::make_tuple(vendorDefinedDescriptorTitleStr, |
| 137 | vendorDescData)); |
| 138 | } |
| 139 | auto nextDescriptorOffset = |
| 140 | sizeof(pldm_descriptor_tlv().descriptor_type) + |
| 141 | sizeof(pldm_descriptor_tlv().descriptor_length) + |
| 142 | descriptorData.length; |
| 143 | descriptorPtr += nextDescriptorOffset; |
| 144 | deviceIdentifiersLen -= nextDescriptorOffset; |
| 145 | } |
| 146 | |
| 147 | descriptorMap.emplace(eid, std::move(descriptors)); |
| 148 | |
| 149 | // Send GetFirmwareParameters request |
| 150 | sendGetFirmwareParametersRequest(eid); |
| 151 | } |
| 152 | |
| 153 | void InventoryManager::sendGetFirmwareParametersRequest(mctp_eid_t eid) |
| 154 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 155 | auto instanceId = instanceIdDb.next(eid); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 156 | Request requestMsg(sizeof(pldm_msg_hdr) + |
| 157 | PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES); |
| 158 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 159 | auto rc = encode_get_firmware_parameters_req( |
| 160 | instanceId, PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES, request); |
| 161 | if (rc) |
| 162 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 163 | instanceIdDb.free(eid, instanceId); |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 164 | error( |
| 165 | "Failed to encode get firmware parameters req for endpoint ID '{EID}', response code '{RC}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 166 | "EID", eid, "RC", rc); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | |
| 170 | rc = handler.registerRequest( |
| 171 | eid, instanceId, PLDM_FWUP, PLDM_GET_FIRMWARE_PARAMETERS, |
| 172 | std::move(requestMsg), |
| 173 | std::move( |
| 174 | std::bind_front(&InventoryManager::getFirmwareParameters, this))); |
| 175 | if (rc) |
| 176 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 177 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 178 | "Failed to send get firmware parameters request for endpoint ID '{EID}', response code '{RC}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 179 | "EID", eid, "RC", rc); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | void InventoryManager::getFirmwareParameters(mctp_eid_t eid, |
| 184 | const pldm_msg* response, |
| 185 | size_t respMsgLen) |
| 186 | { |
| 187 | if (response == nullptr || !respMsgLen) |
| 188 | { |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 189 | error( |
| 190 | "No response received for get firmware parameters for endpoint ID '{EID}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 191 | "EID", eid); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 192 | descriptorMap.erase(eid); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | pldm_get_firmware_parameters_resp fwParams{}; |
| 197 | variable_field activeCompImageSetVerStr{}; |
| 198 | variable_field pendingCompImageSetVerStr{}; |
| 199 | variable_field compParamTable{}; |
| 200 | |
| 201 | auto rc = decode_get_firmware_parameters_resp( |
| 202 | response, respMsgLen, &fwParams, &activeCompImageSetVerStr, |
| 203 | &pendingCompImageSetVerStr, &compParamTable); |
| 204 | if (rc) |
| 205 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 206 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 207 | "Failed to decode get firmware parameters response for endpoint ID '{EID}', response code '{RC}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 208 | "EID", eid, "RC", rc); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
| 212 | if (fwParams.completion_code) |
| 213 | { |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 214 | auto fw_param_cc = fwParams.completion_code; |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 215 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 216 | "Failed to get firmware parameters response for endpoint ID '{EID}', completion code '{CC}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 217 | "EID", eid, "CC", fw_param_cc); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 218 | return; |
| 219 | } |
| 220 | |
| 221 | auto compParamPtr = compParamTable.ptr; |
| 222 | auto compParamTableLen = compParamTable.length; |
| 223 | pldm_component_parameter_entry compEntry{}; |
| 224 | variable_field activeCompVerStr{}; |
| 225 | variable_field pendingCompVerStr{}; |
| 226 | |
| 227 | ComponentInfo componentInfo{}; |
| 228 | while (fwParams.comp_count-- && (compParamTableLen > 0)) |
| 229 | { |
| 230 | auto rc = decode_get_firmware_parameters_resp_comp_entry( |
| 231 | compParamPtr, compParamTableLen, &compEntry, &activeCompVerStr, |
| 232 | &pendingCompVerStr); |
| 233 | if (rc) |
| 234 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 235 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 236 | "Failed to decode component parameter table entry for endpoint ID '{EID}', response code '{RC}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 237 | "EID", eid, "RC", rc); |
Tom Joseph | 75356c1 | 2021-06-20 03:52:40 -0700 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | |
| 241 | auto compClassification = compEntry.comp_classification; |
| 242 | auto compIdentifier = compEntry.comp_identifier; |
| 243 | componentInfo.emplace( |
| 244 | std::make_pair(compClassification, compIdentifier), |
| 245 | compEntry.comp_classification_index); |
| 246 | compParamPtr += sizeof(pldm_component_parameter_entry) + |
| 247 | activeCompVerStr.length + pendingCompVerStr.length; |
| 248 | compParamTableLen -= sizeof(pldm_component_parameter_entry) + |
| 249 | activeCompVerStr.length + pendingCompVerStr.length; |
| 250 | } |
| 251 | componentInfoMap.emplace(eid, std::move(componentInfo)); |
| 252 | } |
| 253 | |
| 254 | } // namespace fw_update |
| 255 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 256 | } // namespace pldm |