blob: 58c3316ac474370cc4222e66ed128e32d449490c [file] [log] [blame]
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05001#include "fru.hpp"
2
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05003#include "common/utils.hpp"
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05004
George Liuc453e162022-12-21 17:16:23 +08005#include <libpldm/entity.h>
6#include <libpldm/utils.h>
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05007#include <systemd/sd-journal.h>
8
Riya Dixit49cfb132023-03-02 04:26:53 -06009#include <phosphor-logging/lg2.hpp>
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050010#include <sdbusplus/bus.hpp>
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050011
George Liu6492f522020-06-16 10:34:05 +080012#include <iostream>
George Liu5bfb0dc2021-05-01 14:28:41 +080013#include <optional>
George Liu6492f522020-06-16 10:34:05 +080014#include <set>
George Liu5bfb0dc2021-05-01 14:28:41 +080015#include <stack>
George Liu077fea22020-04-08 16:47:14 +080016
Riya Dixit49cfb132023-03-02 04:26:53 -060017PHOSPHOR_LOG2_USING;
18
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050019namespace pldm
20{
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050021namespace responder
22{
George Liu5bfb0dc2021-05-01 14:28:41 +080023
24constexpr auto root = "/xyz/openbmc_project/inventory/";
25
26std::optional<pldm_entity>
27 FruImpl::getEntityByObjectPath(const dbus::InterfaceMap& intfMaps)
28{
29 for (const auto& intfMap : intfMaps)
30 {
31 try
32 {
33 pldm_entity entity{};
34 entity.entity_type = parser.getEntityType(intfMap.first);
35 return entity;
36 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -050037 catch (const std::exception&)
George Liu5bfb0dc2021-05-01 14:28:41 +080038 {
39 continue;
40 }
41 }
42
43 return std::nullopt;
44}
45
46void FruImpl::updateAssociationTree(const dbus::ObjectValueTree& objects,
47 const std::string& path)
48{
49 if (path.find(root) == std::string::npos)
50 {
51 return;
52 }
53
54 std::stack<std::string> tmpObjPaths{};
55 tmpObjPaths.emplace(path);
56
57 auto obj = pldm::utils::findParent(path);
58 while ((obj + '/') != root)
59 {
60 tmpObjPaths.emplace(obj);
61 obj = pldm::utils::findParent(obj);
62 }
63
64 std::stack<std::string> tmpObj = tmpObjPaths;
65 while (!tmpObj.empty())
66 {
67 std::string s = tmpObj.top();
George Liu5bfb0dc2021-05-01 14:28:41 +080068 tmpObj.pop();
69 }
70 // Update pldm entity to assocition tree
71 std::string prePath = tmpObjPaths.top();
72 while (!tmpObjPaths.empty())
73 {
74 std::string currPath = tmpObjPaths.top();
75 tmpObjPaths.pop();
76
77 do
78 {
79 if (objToEntityNode.contains(currPath))
80 {
81 pldm_entity node =
82 pldm_entity_extract(objToEntityNode.at(currPath));
Manojkiran Eda8e715ae2021-07-04 21:46:43 +053083 if (pldm_entity_association_tree_find_with_locality(
84 entityTree, &node, false))
George Liu5bfb0dc2021-05-01 14:28:41 +080085 {
86 break;
87 }
88 }
89 else
90 {
91 if (!objects.contains(currPath))
92 {
93 break;
94 }
95
96 auto entityPtr = getEntityByObjectPath(objects.at(currPath));
97 if (!entityPtr)
98 {
99 break;
100 }
101
102 pldm_entity entity = *entityPtr;
103
104 for (auto& it : objToEntityNode)
105 {
106 pldm_entity node = pldm_entity_extract(it.second);
107 if (node.entity_type == entity.entity_type)
108 {
109 entity.entity_instance_num = node.entity_instance_num +
110 1;
111 break;
112 }
113 }
114
115 if (currPath == prePath)
116 {
Manojkiran Eda8e715ae2021-07-04 21:46:43 +0530117 auto node = pldm_entity_association_tree_add_entity(
George Liu5bfb0dc2021-05-01 14:28:41 +0800118 entityTree, &entity, 0xFFFF, nullptr,
Manojkiran Eda8e715ae2021-07-04 21:46:43 +0530119 PLDM_ENTITY_ASSOCIAION_PHYSICAL, false, true, 0xFFFF);
George Liu5bfb0dc2021-05-01 14:28:41 +0800120 objToEntityNode[currPath] = node;
121 }
122 else
123 {
124 if (objToEntityNode.contains(prePath))
125 {
Manojkiran Eda8e715ae2021-07-04 21:46:43 +0530126 auto node = pldm_entity_association_tree_add_entity(
George Liu5bfb0dc2021-05-01 14:28:41 +0800127 entityTree, &entity, 0xFFFF,
128 objToEntityNode[prePath],
Manojkiran Eda8e715ae2021-07-04 21:46:43 +0530129 PLDM_ENTITY_ASSOCIAION_PHYSICAL, false, true,
130 0xFFFF);
George Liu5bfb0dc2021-05-01 14:28:41 +0800131 objToEntityNode[currPath] = node;
132 }
133 }
134 }
135 } while (0);
136
137 prePath = currPath;
138 }
139}
140
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530141void FruImpl::buildFRUTable()
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500142{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530143 if (isBuilt)
144 {
145 return;
146 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500147
Tom Josephf0076332020-02-06 10:18:50 +0530148 fru_parser::DBusLookupInfo dbusInfo;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500149 // Read the all the inventory D-Bus objects
150 auto& bus = pldm::utils::DBusHandler::getBus();
151 dbus::ObjectValueTree objects;
152
153 try
154 {
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530155 dbusInfo = parser.inventoryLookup();
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500156 auto method = bus.new_method_call(
157 std::get<0>(dbusInfo).c_str(), std::get<1>(dbusInfo).c_str(),
158 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500159 auto reply = bus.call(method, dbusTimeout);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500160 reply.read(objects);
161 }
162 catch (const std::exception& e)
163 {
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500164 error("Failed building FRU table due to inventory lookup: {ERROR}",
165 "ERROR", e);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500166 return;
167 }
168
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500169 auto itemIntfsLookup = std::get<2>(dbusInfo);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500170
171 for (const auto& object : objects)
172 {
173 const auto& interfaces = object.second;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500174 for (const auto& interface : interfaces)
175 {
176 if (itemIntfsLookup.find(interface.first) != itemIntfsLookup.end())
177 {
Kamalkumar Patel2a103ef2024-02-14 02:58:08 -0600178 // checking fru present property is available or not.
179 if (!pldm::utils::checkForFruPresence(object.first.str))
180 {
181 continue;
182 }
183
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500184 // An exception will be thrown by getRecordInfo, if the item
185 // D-Bus interface name specified in FRU_Master.json does
186 // not have corresponding config jsons
187 try
188 {
George Liu5bfb0dc2021-05-01 14:28:41 +0800189 updateAssociationTree(objects, object.first.str);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500190 pldm_entity entity{};
George Liu5bfb0dc2021-05-01 14:28:41 +0800191 if (objToEntityNode.contains(object.first.str))
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500192 {
George Liu5bfb0dc2021-05-01 14:28:41 +0800193 pldm_entity_node* node =
194 objToEntityNode.at(object.first.str);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500195
George Liu5bfb0dc2021-05-01 14:28:41 +0800196 entity = pldm_entity_extract(node);
197 }
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500198
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530199 auto recordInfos = parser.getRecordInfo(interface.first);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500200 populateRecords(interfaces, recordInfos, entity);
George Liuc4ea6a92020-07-14 15:48:44 +0800201
202 associatedEntityMap.emplace(object.first, entity);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500203 break;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500204 }
205 catch (const std::exception& e)
206 {
Riya Dixit797f3382023-08-22 22:27:51 -0500207 error(
Riya Dixit49cfb132023-03-02 04:26:53 -0600208 "Config JSONs missing for the item interface type, interface = {INTF}",
209 "INTF", interface.first);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500210 break;
211 }
212 }
213 }
214 }
215
Andrew Jefferyb0ba34d2023-07-17 17:47:16 +0930216 int rc = pldm_entity_association_pdr_add_check(entityTree, pdrRepo, false,
217 TERMINUS_HANDLE);
218 if (rc < 0)
219 {
220 // pldm_entity_assocation_pdr_add() assert()ed on failure
221 error("Failed to add PLDM entity association PDR: {LIBPLDM_ERROR}",
222 "LIBPLDM_ERROR", rc);
223 throw std::runtime_error("Failed to add PLDM entity association PDR");
224 }
225
Sampa Misrac073a202021-05-08 10:56:05 -0500226 // save a copy of bmc's entity association tree
227 pldm_entity_association_tree_copy_root(entityTree, bmcEntityTree);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500228
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530229 isBuilt = true;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500230}
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500231std::string FruImpl::populatefwVersion()
232{
233 static constexpr auto fwFunctionalObjPath =
234 "/xyz/openbmc_project/software/functional";
235 auto& bus = pldm::utils::DBusHandler::getBus();
236 std::string currentBmcVersion;
237 try
238 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500239 auto method = bus.new_method_call(pldm::utils::mapperService,
240 fwFunctionalObjPath,
241 pldm::utils::dbusProperties, "Get");
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500242 method.append("xyz.openbmc_project.Association", "endpoints");
243 std::variant<std::vector<std::string>> paths;
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500244 auto reply = bus.call(method, dbusTimeout);
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500245 reply.read(paths);
246 auto fwRunningVersion = std::get<std::vector<std::string>>(paths)[0];
247 constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
248 auto version = pldm::utils::DBusHandler().getDbusPropertyVariant(
249 fwRunningVersion.c_str(), "Version", versionIntf);
250 currentBmcVersion = std::get<std::string>(version);
251 }
252 catch (const std::exception& e)
253 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600254 error("failed to make a d-bus call Asociation, ERROR= {ERR_EXCEP}",
255 "ERR_EXCEP", e.what());
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500256 return {};
257 }
258 return currentBmcVersion;
259}
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500260void FruImpl::populateRecords(
261 const pldm::responder::dbus::InterfaceMap& interfaces,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500262 const fru_parser::FruRecordInfos& recordInfos, const pldm_entity& entity)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500263{
264 // recordSetIdentifier for the FRU will be set when the first record gets
265 // added for the FRU
266 uint16_t recordSetIdentifier = 0;
267 auto numRecsCount = numRecs;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600268 static uint32_t bmc_record_handle = 0;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500269
Patrick Williams6da4f912023-05-10 07:50:53 -0500270 for (const auto& [recType, encType, fieldInfos] : recordInfos)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500271 {
272 std::vector<uint8_t> tlvs;
273 uint8_t numFRUFields = 0;
Patrick Williams6da4f912023-05-10 07:50:53 -0500274 for (const auto& [intf, prop, propType, fieldTypeNum] : fieldInfos)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500275 {
276 try
277 {
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500278 pldm::responder::dbus::Value propValue;
Manojkiran Eda2b7c1bf2021-09-09 12:26:00 +0530279
280 // Assuming that 0 container Id is assigned to the System (as
281 // that should be the top most container as per dbus hierarchy)
282 if (entity.entity_container_id == 0 && prop == "Version")
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500283 {
284 propValue = populatefwVersion();
285 }
286 else
287 {
288 propValue = interfaces.at(intf).at(prop);
289 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500290 if (propType == "bytearray")
291 {
292 auto byteArray = std::get<std::vector<uint8_t>>(propValue);
293 if (!byteArray.size())
294 {
295 continue;
296 }
297
298 numFRUFields++;
299 tlvs.emplace_back(fieldTypeNum);
300 tlvs.emplace_back(byteArray.size());
301 std::move(std::begin(byteArray), std::end(byteArray),
302 std::back_inserter(tlvs));
303 }
304 else if (propType == "string")
305 {
306 auto str = std::get<std::string>(propValue);
307 if (!str.size())
308 {
309 continue;
310 }
311
312 numFRUFields++;
313 tlvs.emplace_back(fieldTypeNum);
314 tlvs.emplace_back(str.size());
315 std::move(std::begin(str), std::end(str),
316 std::back_inserter(tlvs));
317 }
318 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500319 catch (const std::out_of_range&)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500320 {
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500321 continue;
322 }
323 }
324
325 if (tlvs.size())
326 {
327 if (numRecs == numRecsCount)
328 {
329 recordSetIdentifier = nextRSI();
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600330 bmc_record_handle = nextRecordHandle();
Andrew Jeffery2e248e82023-07-03 17:23:24 +0930331 int rc = pldm_pdr_add_fru_record_set_check(
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530332 pdrRepo, TERMINUS_HANDLE, recordSetIdentifier,
333 entity.entity_type, entity.entity_instance_num,
Andrew Jeffery2e248e82023-07-03 17:23:24 +0930334 entity.entity_container_id, &bmc_record_handle);
335 if (rc)
336 {
337 // pldm_pdr_add_fru_record_set() assert()ed on failure
338 throw std::runtime_error(
339 "Failed to add PDR FRU record set");
340 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500341 }
342 auto curSize = table.size();
343 table.resize(curSize + recHeaderSize + tlvs.size());
344 encode_fru_record(table.data(), table.size(), &curSize,
345 recordSetIdentifier, recType, numFRUFields,
346 encType, tlvs.data(), tlvs.size());
347 numRecs++;
348 }
349 }
350}
351
Pavithra Barithaya9e6631e2024-01-22 00:46:32 -0600352std::vector<uint8_t> FruImpl::tableResize()
353{
354 std::vector<uint8_t> tempTable;
355
356 if (table.size())
357 {
358 std::copy(table.begin(), table.end(), std::back_inserter(tempTable));
359 padBytes = pldm::utils::getNumPadBytes(table.size());
360 tempTable.resize(tempTable.size() + padBytes, 0);
361 }
362 return tempTable;
363}
364
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500365void FruImpl::getFRUTable(Response& response)
366{
367 auto hdrSize = response.size();
Pavithra Barithaya9e6631e2024-01-22 00:46:32 -0600368 std::vector<uint8_t> tempTable;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500369
Pavithra Barithaya9e6631e2024-01-22 00:46:32 -0600370 if (table.size())
371 {
372 tempTable = tableResize();
373 checksum = crc32(tempTable.data(), tempTable.size());
374 }
375 response.resize(hdrSize + tempTable.size() + sizeof(checksum), 0);
376 std::copy(tempTable.begin(), tempTable.end(), response.begin() + hdrSize);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500377
378 // Copy the checksum to response data
Pavithra Barithaya9e6631e2024-01-22 00:46:32 -0600379 auto iter = response.begin() + hdrSize + tempTable.size();
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500380 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
381 iter);
382}
383
Pavithra Barithaya9e6631e2024-01-22 00:46:32 -0600384void FruImpl::getFRURecordTableMetadata()
385{
386 std::vector<uint8_t> tempTable;
387 if (table.size())
388 {
389 tempTable = tableResize();
390 checksum = crc32(tempTable.data(), tempTable.size());
391 }
392}
393
John Wang9e82ad12020-06-12 10:53:32 +0800394int FruImpl::getFRURecordByOption(std::vector<uint8_t>& fruData,
395 uint16_t /* fruTableHandle */,
396 uint16_t recordSetIdentifer,
397 uint8_t recordType, uint8_t fieldType)
398{
Manojkiran Eda31a78442021-09-12 15:18:25 +0530399 using sum = uint32_t;
400
John Wang9e82ad12020-06-12 10:53:32 +0800401 // FRU table is built lazily, build if not done.
402 buildFRUTable();
403
404 /* 7 is sizeof(checksum,4) + padBytesMax(3)
405 * We can not know size of the record table got by options in advance, but
406 * it must be less than the source table. So it's safe to use sizeof the
407 * source table + 7 as the buffer length
408 */
409 size_t recordTableSize = table.size() - padBytes + 7;
410 fruData.resize(recordTableSize, 0);
411
Andrew Jeffery663783b2023-07-03 12:58:14 +0930412 int rc = get_fru_record_by_option_check(
413 table.data(), table.size() - padBytes, fruData.data(), &recordTableSize,
414 recordSetIdentifer, recordType, fieldType);
John Wang9e82ad12020-06-12 10:53:32 +0800415
Andrew Jeffery663783b2023-07-03 12:58:14 +0930416 if (rc != PLDM_SUCCESS || recordTableSize == 0)
John Wang9e82ad12020-06-12 10:53:32 +0800417 {
418 return PLDM_FRU_DATA_STRUCTURE_TABLE_UNAVAILABLE;
419 }
420
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500421 auto pads = pldm::utils::getNumPadBytes(recordTableSize);
Manojkiran Eda31a78442021-09-12 15:18:25 +0530422 crc32(fruData.data(), recordTableSize + pads);
John Wang9e82ad12020-06-12 10:53:32 +0800423
424 auto iter = fruData.begin() + recordTableSize + pads;
425 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
426 iter);
427 fruData.resize(recordTableSize + pads + sizeof(sum));
428
429 return PLDM_SUCCESS;
430}
431
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500432int FruImpl::setFRUTable(const std::vector<uint8_t>& fruData)
433{
434 auto record =
435 reinterpret_cast<const pldm_fru_record_data_format*>(fruData.data());
436 if (record)
437 {
438 if (oemFruHandler && record->record_type == PLDM_FRU_RECORD_TYPE_OEM)
439 {
440 auto rc = oemFruHandler->processOEMFRUTable(fruData);
441 if (!rc)
442 {
443 return PLDM_SUCCESS;
444 }
445 }
446 }
447 return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
448}
449
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500450namespace fru
451{
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500452Response Handler::getFRURecordTableMetadata(const pldm_msg* request,
453 size_t /*payloadLength*/)
454{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530455 // FRU table is built lazily, build if not done.
456 buildFRUTable();
457
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500458 constexpr uint8_t major = 0x01;
459 constexpr uint8_t minor = 0x00;
460 constexpr uint32_t maxSize = 0xFFFFFFFF;
461
462 Response response(sizeof(pldm_msg_hdr) +
463 PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES,
464 0);
465 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
466
Pavithra Barithaya9e6631e2024-01-22 00:46:32 -0600467 impl.getFRURecordTableMetadata();
468
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500469 auto rc = encode_get_fru_record_table_metadata_resp(
470 request->hdr.instance_id, PLDM_SUCCESS, major, minor, maxSize,
471 impl.size(), impl.numRSI(), impl.numRecords(), impl.checkSum(),
472 responsePtr);
473 if (rc != PLDM_SUCCESS)
474 {
475 return ccOnlyResponse(request, rc);
476 }
477
478 return response;
479}
480
481Response Handler::getFRURecordTable(const pldm_msg* request,
482 size_t payloadLength)
483{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530484 // FRU table is built lazily, build if not done.
485 buildFRUTable();
486
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500487 if (payloadLength != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES)
488 {
489 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
490 }
491
492 Response response(
493 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES, 0);
494 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
495
Patrick Williams6da4f912023-05-10 07:50:53 -0500496 auto rc = encode_get_fru_record_table_resp(request->hdr.instance_id,
497 PLDM_SUCCESS, 0,
498 PLDM_START_AND_END, responsePtr);
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500499 if (rc != PLDM_SUCCESS)
500 {
501 return ccOnlyResponse(request, rc);
502 }
503
504 impl.getFRUTable(response);
505
506 return response;
507}
508
John Wang9e82ad12020-06-12 10:53:32 +0800509Response Handler::getFRURecordByOption(const pldm_msg* request,
510 size_t payloadLength)
511{
512 if (payloadLength != sizeof(pldm_get_fru_record_by_option_req))
513 {
514 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
515 }
516
517 uint32_t retDataTransferHandle{};
518 uint16_t retFruTableHandle{};
519 uint16_t retRecordSetIdentifier{};
520 uint8_t retRecordType{};
521 uint8_t retFieldType{};
522 uint8_t retTransferOpFlag{};
523
524 auto rc = decode_get_fru_record_by_option_req(
525 request, payloadLength, &retDataTransferHandle, &retFruTableHandle,
526 &retRecordSetIdentifier, &retRecordType, &retFieldType,
527 &retTransferOpFlag);
528
529 if (rc != PLDM_SUCCESS)
530 {
531 return ccOnlyResponse(request, rc);
532 }
533
534 std::vector<uint8_t> fruData;
535 rc = impl.getFRURecordByOption(fruData, retFruTableHandle,
536 retRecordSetIdentifier, retRecordType,
537 retFieldType);
538 if (rc != PLDM_SUCCESS)
539 {
540 return ccOnlyResponse(request, rc);
541 }
542
Patrick Williams6da4f912023-05-10 07:50:53 -0500543 auto respPayloadLength = PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES +
544 fruData.size();
John Wang9e82ad12020-06-12 10:53:32 +0800545 Response response(sizeof(pldm_msg_hdr) + respPayloadLength, 0);
546 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
547
548 rc = encode_get_fru_record_by_option_resp(
549 request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END,
550 fruData.data(), fruData.size(), responsePtr, respPayloadLength);
551
552 if (rc != PLDM_SUCCESS)
553 {
554 return ccOnlyResponse(request, rc);
555 }
556
557 return response;
558}
559
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500560Response Handler::setFRURecordTable(const pldm_msg* request,
561 size_t payloadLength)
562{
563 uint32_t transferHandle{};
564 uint8_t transferOpFlag{};
565 struct variable_field fruData;
566
567 auto rc = decode_set_fru_record_table_req(
568 request, payloadLength, &transferHandle, &transferOpFlag, &fruData);
569
570 if (rc != PLDM_SUCCESS)
571 {
572 return ccOnlyResponse(request, rc);
573 }
574
575 Table table(fruData.ptr, fruData.ptr + fruData.length);
576 rc = impl.setFRUTable(table);
577 if (rc != PLDM_SUCCESS)
578 {
579 return ccOnlyResponse(request, rc);
580 }
581
582 Response response(sizeof(pldm_msg_hdr) +
583 PLDM_SET_FRU_RECORD_TABLE_RESP_BYTES);
584 struct pldm_msg* responsePtr = reinterpret_cast<pldm_msg*>(response.data());
585
586 rc = encode_set_fru_record_table_resp(
587 request->hdr.instance_id, PLDM_SUCCESS, 0 /* nextDataTransferHandle */,
588 response.size() - sizeof(pldm_msg_hdr), responsePtr);
589
590 if (rc != PLDM_SUCCESS)
591 {
592 return ccOnlyResponse(request, rc);
593 }
594
595 return response;
596}
597
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500598} // namespace fru
599
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500600} // namespace responder
601
602} // namespace pldm