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