blob: f4311b8ecbe31a19cbf9ff9d00e7e41ce4755883 [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
Manojkiran Edacc5f1582021-09-29 17:03:06 +05305#include <config.h>
George Liuc453e162022-12-21 17:16:23 +08006#include <libpldm/entity.h>
7#include <libpldm/utils.h>
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05008#include <systemd/sd-journal.h>
9
Riya Dixit49cfb132023-03-02 04:26:53 -060010#include <phosphor-logging/lg2.hpp>
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050011#include <sdbusplus/bus.hpp>
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050012
George Liu6492f522020-06-16 10:34:05 +080013#include <iostream>
14#include <set>
George Liu077fea22020-04-08 16:47:14 +080015
Riya Dixit49cfb132023-03-02 04:26:53 -060016PHOSPHOR_LOG2_USING;
17
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050018namespace pldm
19{
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050020namespace responder
21{
Tom Joseph33e9c7e2020-06-11 22:09:52 +053022void FruImpl::buildFRUTable()
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050023{
Tom Joseph33e9c7e2020-06-11 22:09:52 +053024 if (isBuilt)
25 {
26 return;
27 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050028
Tom Josephf0076332020-02-06 10:18:50 +053029 fru_parser::DBusLookupInfo dbusInfo;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050030 // Read the all the inventory D-Bus objects
31 auto& bus = pldm::utils::DBusHandler::getBus();
32 dbus::ObjectValueTree objects;
33
34 try
35 {
Tom Joseph33e9c7e2020-06-11 22:09:52 +053036 dbusInfo = parser.inventoryLookup();
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050037 auto method = bus.new_method_call(
38 std::get<0>(dbusInfo).c_str(), std::get<1>(dbusInfo).c_str(),
39 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -050040 auto reply = bus.call(
41 method,
42 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050043 reply.read(objects);
44 }
45 catch (const std::exception& e)
46 {
Riya Dixit49cfb132023-03-02 04:26:53 -060047 error(
48 "Look up of inventory objects failed and PLDM FRU table creation failed");
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050049 return;
50 }
51
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050052 auto itemIntfsLookup = std::get<2>(dbusInfo);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050053
54 for (const auto& object : objects)
55 {
56 const auto& interfaces = object.second;
Sridevi Ramesheefe49b2022-06-27 11:51:02 -050057 bool isPresent = pldm::utils::checkForFruPresence(object.first.str);
58 // Do not create fru record if fru is not present.
59 // Pick up the next available fru.
60 if (!isPresent)
61 {
62 continue;
63 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050064 for (const auto& interface : interfaces)
65 {
66 if (itemIntfsLookup.find(interface.first) != itemIntfsLookup.end())
67 {
68 // An exception will be thrown by getRecordInfo, if the item
69 // D-Bus interface name specified in FRU_Master.json does
70 // not have corresponding config jsons
71 try
72 {
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050073 pldm_entity entity{};
Tom Joseph33e9c7e2020-06-11 22:09:52 +053074 entity.entity_type = parser.getEntityType(interface.first);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050075 pldm_entity_node* parent = nullptr;
76 auto parentObj = pldm::utils::findParent(object.first.str);
77 // To add a FRU to the entity association tree, we need to
78 // determine if the FRU has a parent (D-Bus object). For eg
79 // /system/backplane's parent is /system. /system has no
80 // parent. Some D-Bus pathnames might just be namespaces
81 // (not D-Bus objects), so we need to iterate upwards until
82 // a parent is found, or we reach the root ("/").
83 // Parents are always added first before children in the
84 // entity association tree. We're relying on the fact that
85 // the std::map containing object paths from the
86 // GetManagedObjects call will have a sorted pathname list.
87 do
88 {
89 auto iter = objToEntityNode.find(parentObj);
90 if (iter != objToEntityNode.end())
91 {
92 parent = iter->second;
93 break;
94 }
95 parentObj = pldm::utils::findParent(parentObj);
96 } while (parentObj != "/");
97
98 auto node = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +080099 entityTree, &entity, 0xFFFF, parent,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500100 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
101 objToEntityNode[object.first.str] = node;
102
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530103 auto recordInfos = parser.getRecordInfo(interface.first);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500104 populateRecords(interfaces, recordInfos, entity);
George Liuc4ea6a92020-07-14 15:48:44 +0800105
106 associatedEntityMap.emplace(object.first, entity);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500107 break;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500108 }
109 catch (const std::exception& e)
110 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600111 info(
112 "Config JSONs missing for the item interface type, interface = {INTF}",
113 "INTF", interface.first);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500114 break;
115 }
116 }
117 }
118 }
119
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530120 pldm_entity_association_pdr_add(entityTree, pdrRepo, false,
121 TERMINUS_HANDLE);
Sampa Misrac073a202021-05-08 10:56:05 -0500122 // save a copy of bmc's entity association tree
123 pldm_entity_association_tree_copy_root(entityTree, bmcEntityTree);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500124
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500125 if (table.size())
126 {
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500127 padBytes = pldm::utils::getNumPadBytes(table.size());
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500128 table.resize(table.size() + padBytes, 0);
129
130 // Calculate the checksum
George Liu077fea22020-04-08 16:47:14 +0800131 checksum = crc32(table.data(), table.size());
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500132 }
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530133 isBuilt = true;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500134}
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500135std::string FruImpl::populatefwVersion()
136{
137 static constexpr auto fwFunctionalObjPath =
138 "/xyz/openbmc_project/software/functional";
139 auto& bus = pldm::utils::DBusHandler::getBus();
140 std::string currentBmcVersion;
141 try
142 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500143 auto method = bus.new_method_call(pldm::utils::mapperService,
144 fwFunctionalObjPath,
145 pldm::utils::dbusProperties, "Get");
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500146 method.append("xyz.openbmc_project.Association", "endpoints");
147 std::variant<std::vector<std::string>> paths;
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -0500148 auto reply = bus.call(
149 method,
150 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500151 reply.read(paths);
152 auto fwRunningVersion = std::get<std::vector<std::string>>(paths)[0];
153 constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
154 auto version = pldm::utils::DBusHandler().getDbusPropertyVariant(
155 fwRunningVersion.c_str(), "Version", versionIntf);
156 currentBmcVersion = std::get<std::string>(version);
157 }
158 catch (const std::exception& e)
159 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600160 error("failed to make a d-bus call Asociation, ERROR= {ERR_EXCEP}",
161 "ERR_EXCEP", e.what());
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500162 return {};
163 }
164 return currentBmcVersion;
165}
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500166void FruImpl::populateRecords(
167 const pldm::responder::dbus::InterfaceMap& interfaces,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500168 const fru_parser::FruRecordInfos& recordInfos, const pldm_entity& entity)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500169{
170 // recordSetIdentifier for the FRU will be set when the first record gets
171 // added for the FRU
172 uint16_t recordSetIdentifier = 0;
173 auto numRecsCount = numRecs;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600174 static uint32_t bmc_record_handle = 0;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500175
Patrick Williams6da4f912023-05-10 07:50:53 -0500176 for (const auto& [recType, encType, fieldInfos] : recordInfos)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500177 {
178 std::vector<uint8_t> tlvs;
179 uint8_t numFRUFields = 0;
Patrick Williams6da4f912023-05-10 07:50:53 -0500180 for (const auto& [intf, prop, propType, fieldTypeNum] : fieldInfos)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500181 {
182 try
183 {
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500184 pldm::responder::dbus::Value propValue;
Manojkiran Eda2b7c1bf2021-09-09 12:26:00 +0530185
186 // Assuming that 0 container Id is assigned to the System (as
187 // that should be the top most container as per dbus hierarchy)
188 if (entity.entity_container_id == 0 && prop == "Version")
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500189 {
190 propValue = populatefwVersion();
191 }
192 else
193 {
194 propValue = interfaces.at(intf).at(prop);
195 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500196 if (propType == "bytearray")
197 {
198 auto byteArray = std::get<std::vector<uint8_t>>(propValue);
199 if (!byteArray.size())
200 {
201 continue;
202 }
203
204 numFRUFields++;
205 tlvs.emplace_back(fieldTypeNum);
206 tlvs.emplace_back(byteArray.size());
207 std::move(std::begin(byteArray), std::end(byteArray),
208 std::back_inserter(tlvs));
209 }
210 else if (propType == "string")
211 {
212 auto str = std::get<std::string>(propValue);
213 if (!str.size())
214 {
215 continue;
216 }
217
218 numFRUFields++;
219 tlvs.emplace_back(fieldTypeNum);
220 tlvs.emplace_back(str.size());
221 std::move(std::begin(str), std::end(str),
222 std::back_inserter(tlvs));
223 }
224 }
225 catch (const std::out_of_range& e)
226 {
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500227 continue;
228 }
229 }
230
231 if (tlvs.size())
232 {
233 if (numRecs == numRecsCount)
234 {
235 recordSetIdentifier = nextRSI();
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600236 bmc_record_handle = nextRecordHandle();
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500237 pldm_pdr_add_fru_record_set(
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530238 pdrRepo, TERMINUS_HANDLE, recordSetIdentifier,
239 entity.entity_type, entity.entity_instance_num,
240 entity.entity_container_id, bmc_record_handle);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500241 }
242 auto curSize = table.size();
243 table.resize(curSize + recHeaderSize + tlvs.size());
244 encode_fru_record(table.data(), table.size(), &curSize,
245 recordSetIdentifier, recType, numFRUFields,
246 encType, tlvs.data(), tlvs.size());
247 numRecs++;
248 }
249 }
250}
251
252void FruImpl::getFRUTable(Response& response)
253{
254 auto hdrSize = response.size();
255
256 response.resize(hdrSize + table.size() + sizeof(checksum), 0);
257 std::copy(table.begin(), table.end(), response.begin() + hdrSize);
258
259 // Copy the checksum to response data
260 auto iter = response.begin() + hdrSize + table.size();
261 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
262 iter);
263}
264
John Wang9e82ad12020-06-12 10:53:32 +0800265int FruImpl::getFRURecordByOption(std::vector<uint8_t>& fruData,
266 uint16_t /* fruTableHandle */,
267 uint16_t recordSetIdentifer,
268 uint8_t recordType, uint8_t fieldType)
269{
Manojkiran Eda31a78442021-09-12 15:18:25 +0530270 using sum = uint32_t;
271
John Wang9e82ad12020-06-12 10:53:32 +0800272 // FRU table is built lazily, build if not done.
273 buildFRUTable();
274
275 /* 7 is sizeof(checksum,4) + padBytesMax(3)
276 * We can not know size of the record table got by options in advance, but
277 * it must be less than the source table. So it's safe to use sizeof the
278 * source table + 7 as the buffer length
279 */
280 size_t recordTableSize = table.size() - padBytes + 7;
281 fruData.resize(recordTableSize, 0);
282
283 get_fru_record_by_option(table.data(), table.size() - padBytes,
284 fruData.data(), &recordTableSize,
285 recordSetIdentifer, recordType, fieldType);
286
287 if (recordTableSize == 0)
288 {
289 return PLDM_FRU_DATA_STRUCTURE_TABLE_UNAVAILABLE;
290 }
291
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500292 auto pads = pldm::utils::getNumPadBytes(recordTableSize);
Manojkiran Eda31a78442021-09-12 15:18:25 +0530293 crc32(fruData.data(), recordTableSize + pads);
John Wang9e82ad12020-06-12 10:53:32 +0800294
295 auto iter = fruData.begin() + recordTableSize + pads;
296 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
297 iter);
298 fruData.resize(recordTableSize + pads + sizeof(sum));
299
300 return PLDM_SUCCESS;
301}
302
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500303namespace fru
304{
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500305Response Handler::getFRURecordTableMetadata(const pldm_msg* request,
306 size_t /*payloadLength*/)
307{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530308 // FRU table is built lazily, build if not done.
309 buildFRUTable();
310
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500311 constexpr uint8_t major = 0x01;
312 constexpr uint8_t minor = 0x00;
313 constexpr uint32_t maxSize = 0xFFFFFFFF;
314
315 Response response(sizeof(pldm_msg_hdr) +
316 PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES,
317 0);
318 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
319
320 auto rc = encode_get_fru_record_table_metadata_resp(
321 request->hdr.instance_id, PLDM_SUCCESS, major, minor, maxSize,
322 impl.size(), impl.numRSI(), impl.numRecords(), impl.checkSum(),
323 responsePtr);
324 if (rc != PLDM_SUCCESS)
325 {
326 return ccOnlyResponse(request, rc);
327 }
328
329 return response;
330}
331
332Response Handler::getFRURecordTable(const pldm_msg* request,
333 size_t payloadLength)
334{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530335 // FRU table is built lazily, build if not done.
336 buildFRUTable();
337
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500338 if (payloadLength != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES)
339 {
340 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
341 }
342
343 Response response(
344 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES, 0);
345 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
346
Patrick Williams6da4f912023-05-10 07:50:53 -0500347 auto rc = encode_get_fru_record_table_resp(request->hdr.instance_id,
348 PLDM_SUCCESS, 0,
349 PLDM_START_AND_END, responsePtr);
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500350 if (rc != PLDM_SUCCESS)
351 {
352 return ccOnlyResponse(request, rc);
353 }
354
355 impl.getFRUTable(response);
356
357 return response;
358}
359
John Wang9e82ad12020-06-12 10:53:32 +0800360Response Handler::getFRURecordByOption(const pldm_msg* request,
361 size_t payloadLength)
362{
363 if (payloadLength != sizeof(pldm_get_fru_record_by_option_req))
364 {
365 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
366 }
367
368 uint32_t retDataTransferHandle{};
369 uint16_t retFruTableHandle{};
370 uint16_t retRecordSetIdentifier{};
371 uint8_t retRecordType{};
372 uint8_t retFieldType{};
373 uint8_t retTransferOpFlag{};
374
375 auto rc = decode_get_fru_record_by_option_req(
376 request, payloadLength, &retDataTransferHandle, &retFruTableHandle,
377 &retRecordSetIdentifier, &retRecordType, &retFieldType,
378 &retTransferOpFlag);
379
380 if (rc != PLDM_SUCCESS)
381 {
382 return ccOnlyResponse(request, rc);
383 }
384
385 std::vector<uint8_t> fruData;
386 rc = impl.getFRURecordByOption(fruData, retFruTableHandle,
387 retRecordSetIdentifier, retRecordType,
388 retFieldType);
389 if (rc != PLDM_SUCCESS)
390 {
391 return ccOnlyResponse(request, rc);
392 }
393
Patrick Williams6da4f912023-05-10 07:50:53 -0500394 auto respPayloadLength = PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES +
395 fruData.size();
John Wang9e82ad12020-06-12 10:53:32 +0800396 Response response(sizeof(pldm_msg_hdr) + respPayloadLength, 0);
397 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
398
399 rc = encode_get_fru_record_by_option_resp(
400 request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END,
401 fruData.data(), fruData.size(), responsePtr, respPayloadLength);
402
403 if (rc != PLDM_SUCCESS)
404 {
405 return ccOnlyResponse(request, rc);
406 }
407
408 return response;
409}
410
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500411} // namespace fru
412
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500413} // namespace responder
414
415} // namespace pldm