blob: afb563747e70098955d27ffa82928dfa137c1d3e [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{
18
19namespace responder
20{
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");
40 auto reply = bus.call(method);
41 reply.read(objects);
42 }
43 catch (const std::exception& e)
44 {
45 std::cerr << "Look up of inventory objects failed and PLDM FRU table "
Brad Bishop0b540732021-08-18 09:42:41 -040046 "creation failed\n";
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050047 return;
48 }
49
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050050 auto itemIntfsLookup = std::get<2>(dbusInfo);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050051
52 for (const auto& object : objects)
53 {
54 const auto& interfaces = object.second;
55
56 for (const auto& interface : interfaces)
57 {
58 if (itemIntfsLookup.find(interface.first) != itemIntfsLookup.end())
59 {
60 // An exception will be thrown by getRecordInfo, if the item
61 // D-Bus interface name specified in FRU_Master.json does
62 // not have corresponding config jsons
63 try
64 {
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050065 pldm_entity entity{};
Tom Joseph33e9c7e2020-06-11 22:09:52 +053066 entity.entity_type = parser.getEntityType(interface.first);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050067 pldm_entity_node* parent = nullptr;
68 auto parentObj = pldm::utils::findParent(object.first.str);
69 // To add a FRU to the entity association tree, we need to
70 // determine if the FRU has a parent (D-Bus object). For eg
71 // /system/backplane's parent is /system. /system has no
72 // parent. Some D-Bus pathnames might just be namespaces
73 // (not D-Bus objects), so we need to iterate upwards until
74 // a parent is found, or we reach the root ("/").
75 // Parents are always added first before children in the
76 // entity association tree. We're relying on the fact that
77 // the std::map containing object paths from the
78 // GetManagedObjects call will have a sorted pathname list.
79 do
80 {
81 auto iter = objToEntityNode.find(parentObj);
82 if (iter != objToEntityNode.end())
83 {
84 parent = iter->second;
85 break;
86 }
87 parentObj = pldm::utils::findParent(parentObj);
88 } while (parentObj != "/");
89
90 auto node = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +080091 entityTree, &entity, 0xFFFF, parent,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050092 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
93 objToEntityNode[object.first.str] = node;
94
Tom Joseph33e9c7e2020-06-11 22:09:52 +053095 auto recordInfos = parser.getRecordInfo(interface.first);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050096 populateRecords(interfaces, recordInfos, entity);
George Liuc4ea6a92020-07-14 15:48:44 +080097
98 associatedEntityMap.emplace(object.first, entity);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050099 break;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500100 }
101 catch (const std::exception& e)
102 {
103 std::cout << "Config JSONs missing for the item "
104 "interface type, interface = "
105 << interface.first << "\n";
106 break;
107 }
108 }
109 }
110 }
111
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530112 pldm_entity_association_pdr_add(entityTree, pdrRepo, false,
113 TERMINUS_HANDLE);
Sampa Misrac073a202021-05-08 10:56:05 -0500114 // save a copy of bmc's entity association tree
115 pldm_entity_association_tree_copy_root(entityTree, bmcEntityTree);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500116
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500117 if (table.size())
118 {
119 padBytes = utils::getNumPadBytes(table.size());
120 table.resize(table.size() + padBytes, 0);
121
122 // Calculate the checksum
George Liu077fea22020-04-08 16:47:14 +0800123 checksum = crc32(table.data(), table.size());
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500124 }
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530125 isBuilt = true;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500126}
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500127std::string FruImpl::populatefwVersion()
128{
129 static constexpr auto fwFunctionalObjPath =
130 "/xyz/openbmc_project/software/functional";
131 auto& bus = pldm::utils::DBusHandler::getBus();
132 std::string currentBmcVersion;
133 try
134 {
135 auto method =
136 bus.new_method_call(pldm::utils::mapperService, fwFunctionalObjPath,
137 pldm::utils::dbusProperties, "Get");
138 method.append("xyz.openbmc_project.Association", "endpoints");
139 std::variant<std::vector<std::string>> paths;
140 auto reply = bus.call(method);
141 reply.read(paths);
142 auto fwRunningVersion = std::get<std::vector<std::string>>(paths)[0];
143 constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
144 auto version = pldm::utils::DBusHandler().getDbusPropertyVariant(
145 fwRunningVersion.c_str(), "Version", versionIntf);
146 currentBmcVersion = std::get<std::string>(version);
147 }
148 catch (const std::exception& e)
149 {
150 std::cerr << "failed to make a d-bus call "
151 "Asociation, ERROR= "
152 << e.what() << "\n";
153 return {};
154 }
155 return currentBmcVersion;
156}
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500157void FruImpl::populateRecords(
158 const pldm::responder::dbus::InterfaceMap& interfaces,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500159 const fru_parser::FruRecordInfos& recordInfos, const pldm_entity& entity)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500160{
161 // recordSetIdentifier for the FRU will be set when the first record gets
162 // added for the FRU
163 uint16_t recordSetIdentifier = 0;
164 auto numRecsCount = numRecs;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600165 static uint32_t bmc_record_handle = 0;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500166
167 for (auto const& [recType, encType, fieldInfos] : recordInfos)
168 {
169 std::vector<uint8_t> tlvs;
170 uint8_t numFRUFields = 0;
171 for (auto const& [intf, prop, propType, fieldTypeNum] : fieldInfos)
172 {
173 try
174 {
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500175 pldm::responder::dbus::Value propValue;
Manojkiran Eda2b7c1bf2021-09-09 12:26:00 +0530176
177 // Assuming that 0 container Id is assigned to the System (as
178 // that should be the top most container as per dbus hierarchy)
179 if (entity.entity_container_id == 0 && prop == "Version")
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500180 {
181 propValue = populatefwVersion();
182 }
183 else
184 {
185 propValue = interfaces.at(intf).at(prop);
186 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500187 if (propType == "bytearray")
188 {
189 auto byteArray = std::get<std::vector<uint8_t>>(propValue);
190 if (!byteArray.size())
191 {
192 continue;
193 }
194
195 numFRUFields++;
196 tlvs.emplace_back(fieldTypeNum);
197 tlvs.emplace_back(byteArray.size());
198 std::move(std::begin(byteArray), std::end(byteArray),
199 std::back_inserter(tlvs));
200 }
201 else if (propType == "string")
202 {
203 auto str = std::get<std::string>(propValue);
204 if (!str.size())
205 {
206 continue;
207 }
208
209 numFRUFields++;
210 tlvs.emplace_back(fieldTypeNum);
211 tlvs.emplace_back(str.size());
212 std::move(std::begin(str), std::end(str),
213 std::back_inserter(tlvs));
214 }
215 }
216 catch (const std::out_of_range& e)
217 {
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500218 continue;
219 }
220 }
221
222 if (tlvs.size())
223 {
224 if (numRecs == numRecsCount)
225 {
226 recordSetIdentifier = nextRSI();
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600227 bmc_record_handle = nextRecordHandle();
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500228 pldm_pdr_add_fru_record_set(
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530229 pdrRepo, TERMINUS_HANDLE, recordSetIdentifier,
230 entity.entity_type, entity.entity_instance_num,
231 entity.entity_container_id, bmc_record_handle);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500232 }
233 auto curSize = table.size();
234 table.resize(curSize + recHeaderSize + tlvs.size());
235 encode_fru_record(table.data(), table.size(), &curSize,
236 recordSetIdentifier, recType, numFRUFields,
237 encType, tlvs.data(), tlvs.size());
238 numRecs++;
239 }
240 }
241}
242
243void FruImpl::getFRUTable(Response& response)
244{
245 auto hdrSize = response.size();
246
247 response.resize(hdrSize + table.size() + sizeof(checksum), 0);
248 std::copy(table.begin(), table.end(), response.begin() + hdrSize);
249
250 // Copy the checksum to response data
251 auto iter = response.begin() + hdrSize + table.size();
252 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
253 iter);
254}
255
John Wang9e82ad12020-06-12 10:53:32 +0800256int FruImpl::getFRURecordByOption(std::vector<uint8_t>& fruData,
257 uint16_t /* fruTableHandle */,
258 uint16_t recordSetIdentifer,
259 uint8_t recordType, uint8_t fieldType)
260{
Manojkiran Eda31a78442021-09-12 15:18:25 +0530261 using sum = uint32_t;
262
John Wang9e82ad12020-06-12 10:53:32 +0800263 // FRU table is built lazily, build if not done.
264 buildFRUTable();
265
266 /* 7 is sizeof(checksum,4) + padBytesMax(3)
267 * We can not know size of the record table got by options in advance, but
268 * it must be less than the source table. So it's safe to use sizeof the
269 * source table + 7 as the buffer length
270 */
271 size_t recordTableSize = table.size() - padBytes + 7;
272 fruData.resize(recordTableSize, 0);
273
274 get_fru_record_by_option(table.data(), table.size() - padBytes,
275 fruData.data(), &recordTableSize,
276 recordSetIdentifer, recordType, fieldType);
277
278 if (recordTableSize == 0)
279 {
280 return PLDM_FRU_DATA_STRUCTURE_TABLE_UNAVAILABLE;
281 }
282
283 auto pads = utils::getNumPadBytes(recordTableSize);
Manojkiran Eda31a78442021-09-12 15:18:25 +0530284 crc32(fruData.data(), recordTableSize + pads);
John Wang9e82ad12020-06-12 10:53:32 +0800285
286 auto iter = fruData.begin() + recordTableSize + pads;
287 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
288 iter);
289 fruData.resize(recordTableSize + pads + sizeof(sum));
290
291 return PLDM_SUCCESS;
292}
293
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500294namespace fru
295{
296
297Response Handler::getFRURecordTableMetadata(const pldm_msg* request,
298 size_t /*payloadLength*/)
299{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530300 // FRU table is built lazily, build if not done.
301 buildFRUTable();
302
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500303 constexpr uint8_t major = 0x01;
304 constexpr uint8_t minor = 0x00;
305 constexpr uint32_t maxSize = 0xFFFFFFFF;
306
307 Response response(sizeof(pldm_msg_hdr) +
308 PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES,
309 0);
310 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
311
312 auto rc = encode_get_fru_record_table_metadata_resp(
313 request->hdr.instance_id, PLDM_SUCCESS, major, minor, maxSize,
314 impl.size(), impl.numRSI(), impl.numRecords(), impl.checkSum(),
315 responsePtr);
316 if (rc != PLDM_SUCCESS)
317 {
318 return ccOnlyResponse(request, rc);
319 }
320
321 return response;
322}
323
324Response Handler::getFRURecordTable(const pldm_msg* request,
325 size_t payloadLength)
326{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530327 // FRU table is built lazily, build if not done.
328 buildFRUTable();
329
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500330 if (payloadLength != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES)
331 {
332 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
333 }
334
335 Response response(
336 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES, 0);
337 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
338
339 auto rc =
340 encode_get_fru_record_table_resp(request->hdr.instance_id, PLDM_SUCCESS,
341 0, PLDM_START_AND_END, responsePtr);
342 if (rc != PLDM_SUCCESS)
343 {
344 return ccOnlyResponse(request, rc);
345 }
346
347 impl.getFRUTable(response);
348
349 return response;
350}
351
John Wang9e82ad12020-06-12 10:53:32 +0800352Response Handler::getFRURecordByOption(const pldm_msg* request,
353 size_t payloadLength)
354{
355 if (payloadLength != sizeof(pldm_get_fru_record_by_option_req))
356 {
357 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
358 }
359
360 uint32_t retDataTransferHandle{};
361 uint16_t retFruTableHandle{};
362 uint16_t retRecordSetIdentifier{};
363 uint8_t retRecordType{};
364 uint8_t retFieldType{};
365 uint8_t retTransferOpFlag{};
366
367 auto rc = decode_get_fru_record_by_option_req(
368 request, payloadLength, &retDataTransferHandle, &retFruTableHandle,
369 &retRecordSetIdentifier, &retRecordType, &retFieldType,
370 &retTransferOpFlag);
371
372 if (rc != PLDM_SUCCESS)
373 {
374 return ccOnlyResponse(request, rc);
375 }
376
377 std::vector<uint8_t> fruData;
378 rc = impl.getFRURecordByOption(fruData, retFruTableHandle,
379 retRecordSetIdentifier, retRecordType,
380 retFieldType);
381 if (rc != PLDM_SUCCESS)
382 {
383 return ccOnlyResponse(request, rc);
384 }
385
386 auto respPayloadLength =
387 PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + fruData.size();
388 Response response(sizeof(pldm_msg_hdr) + respPayloadLength, 0);
389 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
390
391 rc = encode_get_fru_record_by_option_resp(
392 request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END,
393 fruData.data(), fruData.size(), responsePtr, respPayloadLength);
394
395 if (rc != PLDM_SUCCESS)
396 {
397 return ccOnlyResponse(request, rc);
398 }
399
400 return response;
401}
402
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500403} // namespace fru
404
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500405} // namespace responder
406
407} // namespace pldm