blob: ef032b453558cd0fb6acaf9db30e792808cb1747 [file] [log] [blame]
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05001#include "fru.hpp"
2
Pavithra Barithaya47180ac2020-10-28 02:12:05 -05003#include "libpldm/entity.h"
George Liu6492f522020-06-16 10:34:05 +08004#include "libpldm/utils.h"
5
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05006#include "common/utils.hpp"
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05007
Manojkiran Edacc5f1582021-09-29 17:03:06 +05308#include <config.h>
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05009#include <systemd/sd-journal.h>
10
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
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050016namespace pldm
17{
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050018namespace responder
19{
Tom Joseph33e9c7e2020-06-11 22:09:52 +053020void FruImpl::buildFRUTable()
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050021{
Tom Joseph33e9c7e2020-06-11 22:09:52 +053022 if (isBuilt)
23 {
24 return;
25 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050026
Tom Josephf0076332020-02-06 10:18:50 +053027 fru_parser::DBusLookupInfo dbusInfo;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050028 // Read the all the inventory D-Bus objects
29 auto& bus = pldm::utils::DBusHandler::getBus();
30 dbus::ObjectValueTree objects;
31
32 try
33 {
Tom Joseph33e9c7e2020-06-11 22:09:52 +053034 dbusInfo = parser.inventoryLookup();
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050035 auto method = bus.new_method_call(
36 std::get<0>(dbusInfo).c_str(), std::get<1>(dbusInfo).c_str(),
37 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
38 auto reply = bus.call(method);
39 reply.read(objects);
40 }
41 catch (const std::exception& e)
42 {
43 std::cerr << "Look up of inventory objects failed and PLDM FRU table "
Brad Bishop0b540732021-08-18 09:42:41 -040044 "creation failed\n";
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050045 return;
46 }
47
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050048 auto itemIntfsLookup = std::get<2>(dbusInfo);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050049
50 for (const auto& object : objects)
51 {
52 const auto& interfaces = object.second;
Sridevi Ramesheefe49b2022-06-27 11:51:02 -050053 bool isPresent = pldm::utils::checkForFruPresence(object.first.str);
54 // Do not create fru record if fru is not present.
55 // Pick up the next available fru.
56 if (!isPresent)
57 {
58 continue;
59 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050060 for (const auto& interface : interfaces)
61 {
62 if (itemIntfsLookup.find(interface.first) != itemIntfsLookup.end())
63 {
64 // An exception will be thrown by getRecordInfo, if the item
65 // D-Bus interface name specified in FRU_Master.json does
66 // not have corresponding config jsons
67 try
68 {
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050069 pldm_entity entity{};
Tom Joseph33e9c7e2020-06-11 22:09:52 +053070 entity.entity_type = parser.getEntityType(interface.first);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050071 pldm_entity_node* parent = nullptr;
72 auto parentObj = pldm::utils::findParent(object.first.str);
73 // To add a FRU to the entity association tree, we need to
74 // determine if the FRU has a parent (D-Bus object). For eg
75 // /system/backplane's parent is /system. /system has no
76 // parent. Some D-Bus pathnames might just be namespaces
77 // (not D-Bus objects), so we need to iterate upwards until
78 // a parent is found, or we reach the root ("/").
79 // Parents are always added first before children in the
80 // entity association tree. We're relying on the fact that
81 // the std::map containing object paths from the
82 // GetManagedObjects call will have a sorted pathname list.
83 do
84 {
85 auto iter = objToEntityNode.find(parentObj);
86 if (iter != objToEntityNode.end())
87 {
88 parent = iter->second;
89 break;
90 }
91 parentObj = pldm::utils::findParent(parentObj);
92 } while (parentObj != "/");
93
94 auto node = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +080095 entityTree, &entity, 0xFFFF, parent,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050096 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
97 objToEntityNode[object.first.str] = node;
98
Tom Joseph33e9c7e2020-06-11 22:09:52 +053099 auto recordInfos = parser.getRecordInfo(interface.first);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500100 populateRecords(interfaces, recordInfos, entity);
George Liuc4ea6a92020-07-14 15:48:44 +0800101
102 associatedEntityMap.emplace(object.first, entity);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500103 break;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500104 }
105 catch (const std::exception& e)
106 {
107 std::cout << "Config JSONs missing for the item "
108 "interface type, interface = "
109 << interface.first << "\n";
110 break;
111 }
112 }
113 }
114 }
115
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530116 pldm_entity_association_pdr_add(entityTree, pdrRepo, false,
117 TERMINUS_HANDLE);
Sampa Misrac073a202021-05-08 10:56:05 -0500118 // save a copy of bmc's entity association tree
119 pldm_entity_association_tree_copy_root(entityTree, bmcEntityTree);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500120
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500121 if (table.size())
122 {
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500123 padBytes = pldm::utils::getNumPadBytes(table.size());
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500124 table.resize(table.size() + padBytes, 0);
125
126 // Calculate the checksum
George Liu077fea22020-04-08 16:47:14 +0800127 checksum = crc32(table.data(), table.size());
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500128 }
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530129 isBuilt = true;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500130}
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500131std::string FruImpl::populatefwVersion()
132{
133 static constexpr auto fwFunctionalObjPath =
134 "/xyz/openbmc_project/software/functional";
135 auto& bus = pldm::utils::DBusHandler::getBus();
136 std::string currentBmcVersion;
137 try
138 {
139 auto method =
140 bus.new_method_call(pldm::utils::mapperService, fwFunctionalObjPath,
141 pldm::utils::dbusProperties, "Get");
142 method.append("xyz.openbmc_project.Association", "endpoints");
143 std::variant<std::vector<std::string>> paths;
144 auto reply = bus.call(method);
145 reply.read(paths);
146 auto fwRunningVersion = std::get<std::vector<std::string>>(paths)[0];
147 constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
148 auto version = pldm::utils::DBusHandler().getDbusPropertyVariant(
149 fwRunningVersion.c_str(), "Version", versionIntf);
150 currentBmcVersion = std::get<std::string>(version);
151 }
152 catch (const std::exception& e)
153 {
154 std::cerr << "failed to make a d-bus call "
155 "Asociation, ERROR= "
156 << e.what() << "\n";
157 return {};
158 }
159 return currentBmcVersion;
160}
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500161void FruImpl::populateRecords(
162 const pldm::responder::dbus::InterfaceMap& interfaces,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500163 const fru_parser::FruRecordInfos& recordInfos, const pldm_entity& entity)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500164{
165 // recordSetIdentifier for the FRU will be set when the first record gets
166 // added for the FRU
167 uint16_t recordSetIdentifier = 0;
168 auto numRecsCount = numRecs;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600169 static uint32_t bmc_record_handle = 0;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500170
171 for (auto const& [recType, encType, fieldInfos] : recordInfos)
172 {
173 std::vector<uint8_t> tlvs;
174 uint8_t numFRUFields = 0;
175 for (auto const& [intf, prop, propType, fieldTypeNum] : fieldInfos)
176 {
177 try
178 {
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500179 pldm::responder::dbus::Value propValue;
Manojkiran Eda2b7c1bf2021-09-09 12:26:00 +0530180
181 // Assuming that 0 container Id is assigned to the System (as
182 // that should be the top most container as per dbus hierarchy)
183 if (entity.entity_container_id == 0 && prop == "Version")
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500184 {
185 propValue = populatefwVersion();
186 }
187 else
188 {
189 propValue = interfaces.at(intf).at(prop);
190 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500191 if (propType == "bytearray")
192 {
193 auto byteArray = std::get<std::vector<uint8_t>>(propValue);
194 if (!byteArray.size())
195 {
196 continue;
197 }
198
199 numFRUFields++;
200 tlvs.emplace_back(fieldTypeNum);
201 tlvs.emplace_back(byteArray.size());
202 std::move(std::begin(byteArray), std::end(byteArray),
203 std::back_inserter(tlvs));
204 }
205 else if (propType == "string")
206 {
207 auto str = std::get<std::string>(propValue);
208 if (!str.size())
209 {
210 continue;
211 }
212
213 numFRUFields++;
214 tlvs.emplace_back(fieldTypeNum);
215 tlvs.emplace_back(str.size());
216 std::move(std::begin(str), std::end(str),
217 std::back_inserter(tlvs));
218 }
219 }
220 catch (const std::out_of_range& e)
221 {
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500222 continue;
223 }
224 }
225
226 if (tlvs.size())
227 {
228 if (numRecs == numRecsCount)
229 {
230 recordSetIdentifier = nextRSI();
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600231 bmc_record_handle = nextRecordHandle();
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500232 pldm_pdr_add_fru_record_set(
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530233 pdrRepo, TERMINUS_HANDLE, recordSetIdentifier,
234 entity.entity_type, entity.entity_instance_num,
235 entity.entity_container_id, bmc_record_handle);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500236 }
237 auto curSize = table.size();
238 table.resize(curSize + recHeaderSize + tlvs.size());
239 encode_fru_record(table.data(), table.size(), &curSize,
240 recordSetIdentifier, recType, numFRUFields,
241 encType, tlvs.data(), tlvs.size());
242 numRecs++;
243 }
244 }
245}
246
247void FruImpl::getFRUTable(Response& response)
248{
249 auto hdrSize = response.size();
250
251 response.resize(hdrSize + table.size() + sizeof(checksum), 0);
252 std::copy(table.begin(), table.end(), response.begin() + hdrSize);
253
254 // Copy the checksum to response data
255 auto iter = response.begin() + hdrSize + table.size();
256 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
257 iter);
258}
259
John Wang9e82ad12020-06-12 10:53:32 +0800260int FruImpl::getFRURecordByOption(std::vector<uint8_t>& fruData,
261 uint16_t /* fruTableHandle */,
262 uint16_t recordSetIdentifer,
263 uint8_t recordType, uint8_t fieldType)
264{
Manojkiran Eda31a78442021-09-12 15:18:25 +0530265 using sum = uint32_t;
266
John Wang9e82ad12020-06-12 10:53:32 +0800267 // FRU table is built lazily, build if not done.
268 buildFRUTable();
269
270 /* 7 is sizeof(checksum,4) + padBytesMax(3)
271 * We can not know size of the record table got by options in advance, but
272 * it must be less than the source table. So it's safe to use sizeof the
273 * source table + 7 as the buffer length
274 */
275 size_t recordTableSize = table.size() - padBytes + 7;
276 fruData.resize(recordTableSize, 0);
277
278 get_fru_record_by_option(table.data(), table.size() - padBytes,
279 fruData.data(), &recordTableSize,
280 recordSetIdentifer, recordType, fieldType);
281
282 if (recordTableSize == 0)
283 {
284 return PLDM_FRU_DATA_STRUCTURE_TABLE_UNAVAILABLE;
285 }
286
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500287 auto pads = pldm::utils::getNumPadBytes(recordTableSize);
Manojkiran Eda31a78442021-09-12 15:18:25 +0530288 crc32(fruData.data(), recordTableSize + pads);
John Wang9e82ad12020-06-12 10:53:32 +0800289
290 auto iter = fruData.begin() + recordTableSize + pads;
291 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
292 iter);
293 fruData.resize(recordTableSize + pads + sizeof(sum));
294
295 return PLDM_SUCCESS;
296}
297
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500298namespace fru
299{
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500300Response Handler::getFRURecordTableMetadata(const pldm_msg* request,
301 size_t /*payloadLength*/)
302{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530303 // FRU table is built lazily, build if not done.
304 buildFRUTable();
305
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500306 constexpr uint8_t major = 0x01;
307 constexpr uint8_t minor = 0x00;
308 constexpr uint32_t maxSize = 0xFFFFFFFF;
309
310 Response response(sizeof(pldm_msg_hdr) +
311 PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES,
312 0);
313 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
314
315 auto rc = encode_get_fru_record_table_metadata_resp(
316 request->hdr.instance_id, PLDM_SUCCESS, major, minor, maxSize,
317 impl.size(), impl.numRSI(), impl.numRecords(), impl.checkSum(),
318 responsePtr);
319 if (rc != PLDM_SUCCESS)
320 {
321 return ccOnlyResponse(request, rc);
322 }
323
324 return response;
325}
326
327Response Handler::getFRURecordTable(const pldm_msg* request,
328 size_t payloadLength)
329{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530330 // FRU table is built lazily, build if not done.
331 buildFRUTable();
332
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500333 if (payloadLength != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES)
334 {
335 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
336 }
337
338 Response response(
339 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES, 0);
340 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
341
342 auto rc =
343 encode_get_fru_record_table_resp(request->hdr.instance_id, PLDM_SUCCESS,
344 0, PLDM_START_AND_END, responsePtr);
345 if (rc != PLDM_SUCCESS)
346 {
347 return ccOnlyResponse(request, rc);
348 }
349
350 impl.getFRUTable(response);
351
352 return response;
353}
354
John Wang9e82ad12020-06-12 10:53:32 +0800355Response Handler::getFRURecordByOption(const pldm_msg* request,
356 size_t payloadLength)
357{
358 if (payloadLength != sizeof(pldm_get_fru_record_by_option_req))
359 {
360 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
361 }
362
363 uint32_t retDataTransferHandle{};
364 uint16_t retFruTableHandle{};
365 uint16_t retRecordSetIdentifier{};
366 uint8_t retRecordType{};
367 uint8_t retFieldType{};
368 uint8_t retTransferOpFlag{};
369
370 auto rc = decode_get_fru_record_by_option_req(
371 request, payloadLength, &retDataTransferHandle, &retFruTableHandle,
372 &retRecordSetIdentifier, &retRecordType, &retFieldType,
373 &retTransferOpFlag);
374
375 if (rc != PLDM_SUCCESS)
376 {
377 return ccOnlyResponse(request, rc);
378 }
379
380 std::vector<uint8_t> fruData;
381 rc = impl.getFRURecordByOption(fruData, retFruTableHandle,
382 retRecordSetIdentifier, retRecordType,
383 retFieldType);
384 if (rc != PLDM_SUCCESS)
385 {
386 return ccOnlyResponse(request, rc);
387 }
388
389 auto respPayloadLength =
390 PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + fruData.size();
391 Response response(sizeof(pldm_msg_hdr) + respPayloadLength, 0);
392 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
393
394 rc = encode_get_fru_record_by_option_resp(
395 request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END,
396 fruData.data(), fruData.size(), responsePtr, respPayloadLength);
397
398 if (rc != PLDM_SUCCESS)
399 {
400 return ccOnlyResponse(request, rc);
401 }
402
403 return response;
404}
405
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500406} // namespace fru
407
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500408} // namespace responder
409
410} // namespace pldm