blob: 43508a726ab230b2179270d20a0b2d6906b3de21 [file] [log] [blame]
Tom Joseph75356c12021-06-20 03:52:40 -07001#include "inventory_manager.hpp"
2
Tom Joseph75356c12021-06-20 03:52:40 -07003#include "common/utils.hpp"
4#include "xyz/openbmc_project/Software/Version/server.hpp"
5
George Liuc453e162022-12-21 17:16:23 +08006#include <libpldm/firmware_update.h>
7
Riya Dixit49cfb132023-03-02 04:26:53 -06008#include <phosphor-logging/lg2.hpp>
9
Tom Joseph75356c12021-06-20 03:52:40 -070010#include <functional>
11
Riya Dixit49cfb132023-03-02 04:26:53 -060012PHOSPHOR_LOG2_USING;
13
Tom Joseph75356c12021-06-20 03:52:40 -070014namespace pldm
15{
Tom Joseph75356c12021-06-20 03:52:40 -070016namespace fw_update
17{
Tom Joseph75356c12021-06-20 03:52:40 -070018void InventoryManager::discoverFDs(const std::vector<mctp_eid_t>& eids)
19{
20 for (const auto& eid : eids)
21 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +093022 auto instanceId = instanceIdDb.next(eid);
Patrick Williams16c2a0a2024-08-16 15:20:59 -040023 Request requestMsg(
24 sizeof(pldm_msg_hdr) + PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES);
Tom Joseph75356c12021-06-20 03:52:40 -070025 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 Jefferya330b2f2023-05-04 14:55:37 +093030 instanceIdDb.free(eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -060031 error(
Riya Dixit76f2c602024-03-28 07:34:12 -050032 "Failed to encode query device identifiers req for endpoint ID '{EID}', response code '{RC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -050033 "EID", eid, "RC", rc);
Tom Joseph75356c12021-06-20 03:52:40 -070034 continue;
35 }
36
37 rc = handler.registerRequest(
38 eid, instanceId, PLDM_FWUP, PLDM_QUERY_DEVICE_IDENTIFIERS,
39 std::move(requestMsg),
Andrew Jefferyc14fb4b2024-07-25 22:13:09 +093040 std::bind_front(&InventoryManager::queryDeviceIdentifiers, this));
Tom Joseph75356c12021-06-20 03:52:40 -070041 if (rc)
42 {
Riya Dixit49cfb132023-03-02 04:26:53 -060043 error(
Riya Dixit76f2c602024-03-28 07:34:12 -050044 "Failed to send query device identifiers request for endpoint ID '{EID}', response code '{RC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -050045 "EID", eid, "RC", rc);
Tom Joseph75356c12021-06-20 03:52:40 -070046 }
47 }
48}
49
Patrick Williams16c2a0a2024-08-16 15:20:59 -040050void InventoryManager::queryDeviceIdentifiers(
51 mctp_eid_t eid, const pldm_msg* response, size_t respMsgLen)
Tom Joseph75356c12021-06-20 03:52:40 -070052{
53 if (response == nullptr || !respMsgLen)
54 {
Riya Dixit76f2c602024-03-28 07:34:12 -050055 error(
56 "No response received for query device identifiers for endpoint ID '{EID}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -050057 "EID", eid);
Tom Joseph75356c12021-06-20 03:52:40 -070058 return;
59 }
60
61 uint8_t completionCode = PLDM_SUCCESS;
62 uint32_t deviceIdentifiersLen = 0;
63 uint8_t descriptorCount = 0;
64 uint8_t* descriptorPtr = nullptr;
65
66 auto rc = decode_query_device_identifiers_resp(
67 response, respMsgLen, &completionCode, &deviceIdentifiersLen,
68 &descriptorCount, &descriptorPtr);
69 if (rc)
70 {
Riya Dixit49cfb132023-03-02 04:26:53 -060071 error(
Riya Dixit76f2c602024-03-28 07:34:12 -050072 "Failed to decode query device identifiers response for endpoint ID '{EID}' and descriptor count '{DESCRIPTOR_COUNT}', response code '{RC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -050073 "EID", eid, "DESCRIPTOR_COUNT", descriptorCount, "RC", rc);
Tom Joseph75356c12021-06-20 03:52:40 -070074 return;
75 }
76
77 if (completionCode)
78 {
Riya Dixit49cfb132023-03-02 04:26:53 -060079 error(
Riya Dixit76f2c602024-03-28 07:34:12 -050080 "Failed to query device identifiers response for endpoint ID '{EID}', completion code '{CC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -050081 "EID", eid, "CC", completionCode);
Tom Joseph75356c12021-06-20 03:52:40 -070082 return;
83 }
84
85 Descriptors descriptors{};
86 while (descriptorCount-- && (deviceIdentifiersLen > 0))
87 {
88 uint16_t descriptorType = 0;
89 variable_field descriptorData{};
90
91 rc = decode_descriptor_type_length_value(
92 descriptorPtr, deviceIdentifiersLen, &descriptorType,
93 &descriptorData);
94 if (rc)
95 {
Riya Dixit49cfb132023-03-02 04:26:53 -060096 error(
Riya Dixit76f2c602024-03-28 07:34:12 -050097 "Failed to decode descriptor type {TYPE}, length {LENGTH} and value for endpoint ID '{EID}', response code '{RC}'",
98 "TYPE", descriptorType, "LENGTH", deviceIdentifiersLen, "EID",
Riya Dixit1e5c81e2024-05-03 07:54:00 -050099 eid, "RC", rc);
Tom Joseph75356c12021-06-20 03:52:40 -0700100 return;
101 }
102
103 if (descriptorType != PLDM_FWUP_VENDOR_DEFINED)
104 {
105 std::vector<uint8_t> descData(
106 descriptorData.ptr, descriptorData.ptr + descriptorData.length);
107 descriptors.emplace(descriptorType, std::move(descData));
108 }
109 else
110 {
111 uint8_t descriptorTitleStrType = 0;
112 variable_field descriptorTitleStr{};
113 variable_field vendorDefinedDescriptorData{};
114
115 rc = decode_vendor_defined_descriptor_value(
116 descriptorData.ptr, descriptorData.length,
117 &descriptorTitleStrType, &descriptorTitleStr,
118 &vendorDefinedDescriptorData);
119 if (rc)
120 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600121 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500122 "Failed to decode vendor-defined descriptor value for endpoint ID '{EID}', response code '{RC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500123 "EID", eid, "RC", rc);
Tom Joseph75356c12021-06-20 03:52:40 -0700124 return;
125 }
126
127 auto vendorDefinedDescriptorTitleStr =
128 utils::toString(descriptorTitleStr);
129 std::vector<uint8_t> vendorDescData(
130 vendorDefinedDescriptorData.ptr,
131 vendorDefinedDescriptorData.ptr +
132 vendorDefinedDescriptorData.length);
133 descriptors.emplace(descriptorType,
134 std::make_tuple(vendorDefinedDescriptorTitleStr,
135 vendorDescData));
136 }
137 auto nextDescriptorOffset =
138 sizeof(pldm_descriptor_tlv().descriptor_type) +
139 sizeof(pldm_descriptor_tlv().descriptor_length) +
140 descriptorData.length;
141 descriptorPtr += nextDescriptorOffset;
142 deviceIdentifiersLen -= nextDescriptorOffset;
143 }
144
145 descriptorMap.emplace(eid, std::move(descriptors));
146
147 // Send GetFirmwareParameters request
148 sendGetFirmwareParametersRequest(eid);
149}
150
151void InventoryManager::sendGetFirmwareParametersRequest(mctp_eid_t eid)
152{
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930153 auto instanceId = instanceIdDb.next(eid);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400154 Request requestMsg(
155 sizeof(pldm_msg_hdr) + PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES);
Tom Joseph75356c12021-06-20 03:52:40 -0700156 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
157 auto rc = encode_get_firmware_parameters_req(
158 instanceId, PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES, request);
159 if (rc)
160 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930161 instanceIdDb.free(eid, instanceId);
Riya Dixit76f2c602024-03-28 07:34:12 -0500162 error(
163 "Failed to encode get firmware parameters req for endpoint ID '{EID}', response code '{RC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500164 "EID", eid, "RC", rc);
Tom Joseph75356c12021-06-20 03:52:40 -0700165 return;
166 }
167
168 rc = handler.registerRequest(
169 eid, instanceId, PLDM_FWUP, PLDM_GET_FIRMWARE_PARAMETERS,
170 std::move(requestMsg),
Andrew Jefferyc14fb4b2024-07-25 22:13:09 +0930171 std::bind_front(&InventoryManager::getFirmwareParameters, this));
Tom Joseph75356c12021-06-20 03:52:40 -0700172 if (rc)
173 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600174 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500175 "Failed to send get firmware parameters request for endpoint ID '{EID}', response code '{RC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500176 "EID", eid, "RC", rc);
Tom Joseph75356c12021-06-20 03:52:40 -0700177 }
178}
179
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400180void InventoryManager::getFirmwareParameters(
181 mctp_eid_t eid, const pldm_msg* response, size_t respMsgLen)
Tom Joseph75356c12021-06-20 03:52:40 -0700182{
183 if (response == nullptr || !respMsgLen)
184 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500185 error(
186 "No response received for get firmware parameters for endpoint ID '{EID}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500187 "EID", eid);
Tom Joseph75356c12021-06-20 03:52:40 -0700188 descriptorMap.erase(eid);
189 return;
190 }
191
192 pldm_get_firmware_parameters_resp fwParams{};
193 variable_field activeCompImageSetVerStr{};
194 variable_field pendingCompImageSetVerStr{};
195 variable_field compParamTable{};
196
197 auto rc = decode_get_firmware_parameters_resp(
198 response, respMsgLen, &fwParams, &activeCompImageSetVerStr,
199 &pendingCompImageSetVerStr, &compParamTable);
200 if (rc)
201 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600202 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500203 "Failed to decode get firmware parameters response for endpoint ID '{EID}', response code '{RC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500204 "EID", eid, "RC", rc);
Tom Joseph75356c12021-06-20 03:52:40 -0700205 return;
206 }
207
208 if (fwParams.completion_code)
209 {
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500210 auto fw_param_cc = fwParams.completion_code;
Riya Dixit49cfb132023-03-02 04:26:53 -0600211 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500212 "Failed to get firmware parameters response for endpoint ID '{EID}', completion code '{CC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500213 "EID", eid, "CC", fw_param_cc);
Tom Joseph75356c12021-06-20 03:52:40 -0700214 return;
215 }
216
217 auto compParamPtr = compParamTable.ptr;
218 auto compParamTableLen = compParamTable.length;
219 pldm_component_parameter_entry compEntry{};
220 variable_field activeCompVerStr{};
221 variable_field pendingCompVerStr{};
222
223 ComponentInfo componentInfo{};
224 while (fwParams.comp_count-- && (compParamTableLen > 0))
225 {
226 auto rc = decode_get_firmware_parameters_resp_comp_entry(
227 compParamPtr, compParamTableLen, &compEntry, &activeCompVerStr,
228 &pendingCompVerStr);
229 if (rc)
230 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600231 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500232 "Failed to decode component parameter table entry for endpoint ID '{EID}', response code '{RC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500233 "EID", eid, "RC", rc);
Tom Joseph75356c12021-06-20 03:52:40 -0700234 return;
235 }
236
237 auto compClassification = compEntry.comp_classification;
238 auto compIdentifier = compEntry.comp_identifier;
239 componentInfo.emplace(
240 std::make_pair(compClassification, compIdentifier),
241 compEntry.comp_classification_index);
242 compParamPtr += sizeof(pldm_component_parameter_entry) +
243 activeCompVerStr.length + pendingCompVerStr.length;
244 compParamTableLen -= sizeof(pldm_component_parameter_entry) +
245 activeCompVerStr.length + pendingCompVerStr.length;
246 }
247 componentInfoMap.emplace(eid, std::move(componentInfo));
248}
249
250} // namespace fw_update
251
Riya Dixit49cfb132023-03-02 04:26:53 -0600252} // namespace pldm