blob: 303f413979bcab9777f3ba7a479a67cfe52ccac7 [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>
13#include <set>
George Liu077fea22020-04-08 16:47:14 +080014
Riya Dixit49cfb132023-03-02 04:26:53 -060015PHOSPHOR_LOG2_USING;
16
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050017namespace pldm
18{
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050019namespace responder
20{
Tom Joseph33e9c7e2020-06-11 22:09:52 +053021void FruImpl::buildFRUTable()
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050022{
Tom Joseph33e9c7e2020-06-11 22:09:52 +053023 if (isBuilt)
24 {
25 return;
26 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050027
Tom Josephf0076332020-02-06 10:18:50 +053028 fru_parser::DBusLookupInfo dbusInfo;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050029 // Read the all the inventory D-Bus objects
30 auto& bus = pldm::utils::DBusHandler::getBus();
31 dbus::ObjectValueTree objects;
32
33 try
34 {
Tom Joseph33e9c7e2020-06-11 22:09:52 +053035 dbusInfo = parser.inventoryLookup();
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050036 auto method = bus.new_method_call(
37 std::get<0>(dbusInfo).c_str(), std::get<1>(dbusInfo).c_str(),
38 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -050039 auto reply = bus.call(
40 method,
41 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050042 reply.read(objects);
43 }
44 catch (const std::exception& e)
45 {
Riya Dixit49cfb132023-03-02 04:26:53 -060046 error(
47 "Look up of inventory objects failed and PLDM FRU table creation failed");
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050048 return;
49 }
50
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050051 auto itemIntfsLookup = std::get<2>(dbusInfo);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050052
53 for (const auto& object : objects)
54 {
55 const auto& interfaces = object.second;
Sridevi Ramesheefe49b2022-06-27 11:51:02 -050056 bool isPresent = pldm::utils::checkForFruPresence(object.first.str);
57 // Do not create fru record if fru is not present.
58 // Pick up the next available fru.
59 if (!isPresent)
60 {
61 continue;
62 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050063 for (const auto& interface : interfaces)
64 {
65 if (itemIntfsLookup.find(interface.first) != itemIntfsLookup.end())
66 {
67 // An exception will be thrown by getRecordInfo, if the item
68 // D-Bus interface name specified in FRU_Master.json does
69 // not have corresponding config jsons
70 try
71 {
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050072 pldm_entity entity{};
Tom Joseph33e9c7e2020-06-11 22:09:52 +053073 entity.entity_type = parser.getEntityType(interface.first);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050074 pldm_entity_node* parent = nullptr;
75 auto parentObj = pldm::utils::findParent(object.first.str);
76 // To add a FRU to the entity association tree, we need to
77 // determine if the FRU has a parent (D-Bus object). For eg
78 // /system/backplane's parent is /system. /system has no
79 // parent. Some D-Bus pathnames might just be namespaces
80 // (not D-Bus objects), so we need to iterate upwards until
81 // a parent is found, or we reach the root ("/").
82 // Parents are always added first before children in the
83 // entity association tree. We're relying on the fact that
84 // the std::map containing object paths from the
85 // GetManagedObjects call will have a sorted pathname list.
86 do
87 {
88 auto iter = objToEntityNode.find(parentObj);
89 if (iter != objToEntityNode.end())
90 {
91 parent = iter->second;
92 break;
93 }
94 parentObj = pldm::utils::findParent(parentObj);
95 } while (parentObj != "/");
96
97 auto node = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +080098 entityTree, &entity, 0xFFFF, parent,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050099 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
100 objToEntityNode[object.first.str] = node;
101
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530102 auto recordInfos = parser.getRecordInfo(interface.first);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500103 populateRecords(interfaces, recordInfos, entity);
George Liuc4ea6a92020-07-14 15:48:44 +0800104
105 associatedEntityMap.emplace(object.first, entity);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500106 break;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500107 }
108 catch (const std::exception& e)
109 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600110 info(
111 "Config JSONs missing for the item interface type, interface = {INTF}",
112 "INTF", interface.first);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500113 break;
114 }
115 }
116 }
117 }
118
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530119 pldm_entity_association_pdr_add(entityTree, pdrRepo, false,
120 TERMINUS_HANDLE);
Sampa Misrac073a202021-05-08 10:56:05 -0500121 // save a copy of bmc's entity association tree
122 pldm_entity_association_tree_copy_root(entityTree, bmcEntityTree);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500123
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500124 if (table.size())
125 {
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500126 padBytes = pldm::utils::getNumPadBytes(table.size());
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500127 table.resize(table.size() + padBytes, 0);
128
129 // Calculate the checksum
George Liu077fea22020-04-08 16:47:14 +0800130 checksum = crc32(table.data(), table.size());
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500131 }
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530132 isBuilt = true;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500133}
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500134std::string FruImpl::populatefwVersion()
135{
136 static constexpr auto fwFunctionalObjPath =
137 "/xyz/openbmc_project/software/functional";
138 auto& bus = pldm::utils::DBusHandler::getBus();
139 std::string currentBmcVersion;
140 try
141 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500142 auto method = bus.new_method_call(pldm::utils::mapperService,
143 fwFunctionalObjPath,
144 pldm::utils::dbusProperties, "Get");
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500145 method.append("xyz.openbmc_project.Association", "endpoints");
146 std::variant<std::vector<std::string>> paths;
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -0500147 auto reply = bus.call(
148 method,
149 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500150 reply.read(paths);
151 auto fwRunningVersion = std::get<std::vector<std::string>>(paths)[0];
152 constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
153 auto version = pldm::utils::DBusHandler().getDbusPropertyVariant(
154 fwRunningVersion.c_str(), "Version", versionIntf);
155 currentBmcVersion = std::get<std::string>(version);
156 }
157 catch (const std::exception& e)
158 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600159 error("failed to make a d-bus call Asociation, ERROR= {ERR_EXCEP}",
160 "ERR_EXCEP", e.what());
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500161 return {};
162 }
163 return currentBmcVersion;
164}
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500165void FruImpl::populateRecords(
166 const pldm::responder::dbus::InterfaceMap& interfaces,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500167 const fru_parser::FruRecordInfos& recordInfos, const pldm_entity& entity)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500168{
169 // recordSetIdentifier for the FRU will be set when the first record gets
170 // added for the FRU
171 uint16_t recordSetIdentifier = 0;
172 auto numRecsCount = numRecs;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600173 static uint32_t bmc_record_handle = 0;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500174
Patrick Williams6da4f912023-05-10 07:50:53 -0500175 for (const auto& [recType, encType, fieldInfos] : recordInfos)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500176 {
177 std::vector<uint8_t> tlvs;
178 uint8_t numFRUFields = 0;
Patrick Williams6da4f912023-05-10 07:50:53 -0500179 for (const auto& [intf, prop, propType, fieldTypeNum] : fieldInfos)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500180 {
181 try
182 {
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500183 pldm::responder::dbus::Value propValue;
Manojkiran Eda2b7c1bf2021-09-09 12:26:00 +0530184
185 // Assuming that 0 container Id is assigned to the System (as
186 // that should be the top most container as per dbus hierarchy)
187 if (entity.entity_container_id == 0 && prop == "Version")
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500188 {
189 propValue = populatefwVersion();
190 }
191 else
192 {
193 propValue = interfaces.at(intf).at(prop);
194 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500195 if (propType == "bytearray")
196 {
197 auto byteArray = std::get<std::vector<uint8_t>>(propValue);
198 if (!byteArray.size())
199 {
200 continue;
201 }
202
203 numFRUFields++;
204 tlvs.emplace_back(fieldTypeNum);
205 tlvs.emplace_back(byteArray.size());
206 std::move(std::begin(byteArray), std::end(byteArray),
207 std::back_inserter(tlvs));
208 }
209 else if (propType == "string")
210 {
211 auto str = std::get<std::string>(propValue);
212 if (!str.size())
213 {
214 continue;
215 }
216
217 numFRUFields++;
218 tlvs.emplace_back(fieldTypeNum);
219 tlvs.emplace_back(str.size());
220 std::move(std::begin(str), std::end(str),
221 std::back_inserter(tlvs));
222 }
223 }
224 catch (const std::out_of_range& e)
225 {
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500226 continue;
227 }
228 }
229
230 if (tlvs.size())
231 {
232 if (numRecs == numRecsCount)
233 {
234 recordSetIdentifier = nextRSI();
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600235 bmc_record_handle = nextRecordHandle();
Andrew Jeffery2e248e82023-07-03 17:23:24 +0930236 int rc = pldm_pdr_add_fru_record_set_check(
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530237 pdrRepo, TERMINUS_HANDLE, recordSetIdentifier,
238 entity.entity_type, entity.entity_instance_num,
Andrew Jeffery2e248e82023-07-03 17:23:24 +0930239 entity.entity_container_id, &bmc_record_handle);
240 if (rc)
241 {
242 // pldm_pdr_add_fru_record_set() assert()ed on failure
243 throw std::runtime_error(
244 "Failed to add PDR FRU record set");
245 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500246 }
247 auto curSize = table.size();
248 table.resize(curSize + recHeaderSize + tlvs.size());
249 encode_fru_record(table.data(), table.size(), &curSize,
250 recordSetIdentifier, recType, numFRUFields,
251 encType, tlvs.data(), tlvs.size());
252 numRecs++;
253 }
254 }
255}
256
257void FruImpl::getFRUTable(Response& response)
258{
259 auto hdrSize = response.size();
260
261 response.resize(hdrSize + table.size() + sizeof(checksum), 0);
262 std::copy(table.begin(), table.end(), response.begin() + hdrSize);
263
264 // Copy the checksum to response data
265 auto iter = response.begin() + hdrSize + table.size();
266 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
267 iter);
268}
269
John Wang9e82ad12020-06-12 10:53:32 +0800270int FruImpl::getFRURecordByOption(std::vector<uint8_t>& fruData,
271 uint16_t /* fruTableHandle */,
272 uint16_t recordSetIdentifer,
273 uint8_t recordType, uint8_t fieldType)
274{
Manojkiran Eda31a78442021-09-12 15:18:25 +0530275 using sum = uint32_t;
276
John Wang9e82ad12020-06-12 10:53:32 +0800277 // FRU table is built lazily, build if not done.
278 buildFRUTable();
279
280 /* 7 is sizeof(checksum,4) + padBytesMax(3)
281 * We can not know size of the record table got by options in advance, but
282 * it must be less than the source table. So it's safe to use sizeof the
283 * source table + 7 as the buffer length
284 */
285 size_t recordTableSize = table.size() - padBytes + 7;
286 fruData.resize(recordTableSize, 0);
287
Andrew Jeffery663783b2023-07-03 12:58:14 +0930288 int rc = get_fru_record_by_option_check(
289 table.data(), table.size() - padBytes, fruData.data(), &recordTableSize,
290 recordSetIdentifer, recordType, fieldType);
John Wang9e82ad12020-06-12 10:53:32 +0800291
Andrew Jeffery663783b2023-07-03 12:58:14 +0930292 if (rc != PLDM_SUCCESS || recordTableSize == 0)
John Wang9e82ad12020-06-12 10:53:32 +0800293 {
294 return PLDM_FRU_DATA_STRUCTURE_TABLE_UNAVAILABLE;
295 }
296
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500297 auto pads = pldm::utils::getNumPadBytes(recordTableSize);
Manojkiran Eda31a78442021-09-12 15:18:25 +0530298 crc32(fruData.data(), recordTableSize + pads);
John Wang9e82ad12020-06-12 10:53:32 +0800299
300 auto iter = fruData.begin() + recordTableSize + pads;
301 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
302 iter);
303 fruData.resize(recordTableSize + pads + sizeof(sum));
304
305 return PLDM_SUCCESS;
306}
307
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500308namespace fru
309{
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500310Response Handler::getFRURecordTableMetadata(const pldm_msg* request,
311 size_t /*payloadLength*/)
312{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530313 // FRU table is built lazily, build if not done.
314 buildFRUTable();
315
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500316 constexpr uint8_t major = 0x01;
317 constexpr uint8_t minor = 0x00;
318 constexpr uint32_t maxSize = 0xFFFFFFFF;
319
320 Response response(sizeof(pldm_msg_hdr) +
321 PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES,
322 0);
323 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
324
325 auto rc = encode_get_fru_record_table_metadata_resp(
326 request->hdr.instance_id, PLDM_SUCCESS, major, minor, maxSize,
327 impl.size(), impl.numRSI(), impl.numRecords(), impl.checkSum(),
328 responsePtr);
329 if (rc != PLDM_SUCCESS)
330 {
331 return ccOnlyResponse(request, rc);
332 }
333
334 return response;
335}
336
337Response Handler::getFRURecordTable(const pldm_msg* request,
338 size_t payloadLength)
339{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530340 // FRU table is built lazily, build if not done.
341 buildFRUTable();
342
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500343 if (payloadLength != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES)
344 {
345 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
346 }
347
348 Response response(
349 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES, 0);
350 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
351
Patrick Williams6da4f912023-05-10 07:50:53 -0500352 auto rc = encode_get_fru_record_table_resp(request->hdr.instance_id,
353 PLDM_SUCCESS, 0,
354 PLDM_START_AND_END, responsePtr);
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500355 if (rc != PLDM_SUCCESS)
356 {
357 return ccOnlyResponse(request, rc);
358 }
359
360 impl.getFRUTable(response);
361
362 return response;
363}
364
John Wang9e82ad12020-06-12 10:53:32 +0800365Response Handler::getFRURecordByOption(const pldm_msg* request,
366 size_t payloadLength)
367{
368 if (payloadLength != sizeof(pldm_get_fru_record_by_option_req))
369 {
370 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
371 }
372
373 uint32_t retDataTransferHandle{};
374 uint16_t retFruTableHandle{};
375 uint16_t retRecordSetIdentifier{};
376 uint8_t retRecordType{};
377 uint8_t retFieldType{};
378 uint8_t retTransferOpFlag{};
379
380 auto rc = decode_get_fru_record_by_option_req(
381 request, payloadLength, &retDataTransferHandle, &retFruTableHandle,
382 &retRecordSetIdentifier, &retRecordType, &retFieldType,
383 &retTransferOpFlag);
384
385 if (rc != PLDM_SUCCESS)
386 {
387 return ccOnlyResponse(request, rc);
388 }
389
390 std::vector<uint8_t> fruData;
391 rc = impl.getFRURecordByOption(fruData, retFruTableHandle,
392 retRecordSetIdentifier, retRecordType,
393 retFieldType);
394 if (rc != PLDM_SUCCESS)
395 {
396 return ccOnlyResponse(request, rc);
397 }
398
Patrick Williams6da4f912023-05-10 07:50:53 -0500399 auto respPayloadLength = PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES +
400 fruData.size();
John Wang9e82ad12020-06-12 10:53:32 +0800401 Response response(sizeof(pldm_msg_hdr) + respPayloadLength, 0);
402 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
403
404 rc = encode_get_fru_record_by_option_resp(
405 request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END,
406 fruData.data(), fruData.size(), responsePtr, respPayloadLength);
407
408 if (rc != PLDM_SUCCESS)
409 {
410 return ccOnlyResponse(request, rc);
411 }
412
413 return response;
414}
415
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500416} // namespace fru
417
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500418} // namespace responder
419
420} // namespace pldm