blob: 418c831a75fe1ac1952284642cd228c0c6fba3df [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
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500112 pldm_entity_association_pdr_add(entityTree, pdrRepo, false);
Sampa Misrac073a202021-05-08 10:56:05 -0500113 // save a copy of bmc's entity association tree
114 pldm_entity_association_tree_copy_root(entityTree, bmcEntityTree);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500115
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500116 if (table.size())
117 {
118 padBytes = utils::getNumPadBytes(table.size());
119 table.resize(table.size() + padBytes, 0);
120
121 // Calculate the checksum
George Liu077fea22020-04-08 16:47:14 +0800122 checksum = crc32(table.data(), table.size());
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500123 }
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530124 isBuilt = true;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500125}
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500126std::string FruImpl::populatefwVersion()
127{
128 static constexpr auto fwFunctionalObjPath =
129 "/xyz/openbmc_project/software/functional";
130 auto& bus = pldm::utils::DBusHandler::getBus();
131 std::string currentBmcVersion;
132 try
133 {
134 auto method =
135 bus.new_method_call(pldm::utils::mapperService, fwFunctionalObjPath,
136 pldm::utils::dbusProperties, "Get");
137 method.append("xyz.openbmc_project.Association", "endpoints");
138 std::variant<std::vector<std::string>> paths;
139 auto reply = bus.call(method);
140 reply.read(paths);
141 auto fwRunningVersion = std::get<std::vector<std::string>>(paths)[0];
142 constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
143 auto version = pldm::utils::DBusHandler().getDbusPropertyVariant(
144 fwRunningVersion.c_str(), "Version", versionIntf);
145 currentBmcVersion = std::get<std::string>(version);
146 }
147 catch (const std::exception& e)
148 {
149 std::cerr << "failed to make a d-bus call "
150 "Asociation, ERROR= "
151 << e.what() << "\n";
152 return {};
153 }
154 return currentBmcVersion;
155}
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500156void FruImpl::populateRecords(
157 const pldm::responder::dbus::InterfaceMap& interfaces,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500158 const fru_parser::FruRecordInfos& recordInfos, const pldm_entity& entity)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500159{
160 // recordSetIdentifier for the FRU will be set when the first record gets
161 // added for the FRU
162 uint16_t recordSetIdentifier = 0;
163 auto numRecsCount = numRecs;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600164 static uint32_t bmc_record_handle = 0;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500165
166 for (auto const& [recType, encType, fieldInfos] : recordInfos)
167 {
168 std::vector<uint8_t> tlvs;
169 uint8_t numFRUFields = 0;
170 for (auto const& [intf, prop, propType, fieldTypeNum] : fieldInfos)
171 {
172 try
173 {
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500174 pldm::responder::dbus::Value propValue;
Manojkiran Eda2b7c1bf2021-09-09 12:26:00 +0530175
176 // Assuming that 0 container Id is assigned to the System (as
177 // that should be the top most container as per dbus hierarchy)
178 if (entity.entity_container_id == 0 && prop == "Version")
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500179 {
180 propValue = populatefwVersion();
181 }
182 else
183 {
184 propValue = interfaces.at(intf).at(prop);
185 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500186 if (propType == "bytearray")
187 {
188 auto byteArray = std::get<std::vector<uint8_t>>(propValue);
189 if (!byteArray.size())
190 {
191 continue;
192 }
193
194 numFRUFields++;
195 tlvs.emplace_back(fieldTypeNum);
196 tlvs.emplace_back(byteArray.size());
197 std::move(std::begin(byteArray), std::end(byteArray),
198 std::back_inserter(tlvs));
199 }
200 else if (propType == "string")
201 {
202 auto str = std::get<std::string>(propValue);
203 if (!str.size())
204 {
205 continue;
206 }
207
208 numFRUFields++;
209 tlvs.emplace_back(fieldTypeNum);
210 tlvs.emplace_back(str.size());
211 std::move(std::begin(str), std::end(str),
212 std::back_inserter(tlvs));
213 }
214 }
215 catch (const std::out_of_range& e)
216 {
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500217 continue;
218 }
219 }
220
221 if (tlvs.size())
222 {
223 if (numRecs == numRecsCount)
224 {
225 recordSetIdentifier = nextRSI();
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600226 bmc_record_handle = nextRecordHandle();
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500227 pldm_pdr_add_fru_record_set(
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530228 pdrRepo, TERMINUS_HANDLE, recordSetIdentifier,
229 entity.entity_type, entity.entity_instance_num,
230 entity.entity_container_id, bmc_record_handle);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500231 }
232 auto curSize = table.size();
233 table.resize(curSize + recHeaderSize + tlvs.size());
234 encode_fru_record(table.data(), table.size(), &curSize,
235 recordSetIdentifier, recType, numFRUFields,
236 encType, tlvs.data(), tlvs.size());
237 numRecs++;
238 }
239 }
240}
241
242void FruImpl::getFRUTable(Response& response)
243{
244 auto hdrSize = response.size();
245
246 response.resize(hdrSize + table.size() + sizeof(checksum), 0);
247 std::copy(table.begin(), table.end(), response.begin() + hdrSize);
248
249 // Copy the checksum to response data
250 auto iter = response.begin() + hdrSize + table.size();
251 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
252 iter);
253}
254
John Wang9e82ad12020-06-12 10:53:32 +0800255int FruImpl::getFRURecordByOption(std::vector<uint8_t>& fruData,
256 uint16_t /* fruTableHandle */,
257 uint16_t recordSetIdentifer,
258 uint8_t recordType, uint8_t fieldType)
259{
Manojkiran Eda31a78442021-09-12 15:18:25 +0530260 using sum = uint32_t;
261
John Wang9e82ad12020-06-12 10:53:32 +0800262 // FRU table is built lazily, build if not done.
263 buildFRUTable();
264
265 /* 7 is sizeof(checksum,4) + padBytesMax(3)
266 * We can not know size of the record table got by options in advance, but
267 * it must be less than the source table. So it's safe to use sizeof the
268 * source table + 7 as the buffer length
269 */
270 size_t recordTableSize = table.size() - padBytes + 7;
271 fruData.resize(recordTableSize, 0);
272
273 get_fru_record_by_option(table.data(), table.size() - padBytes,
274 fruData.data(), &recordTableSize,
275 recordSetIdentifer, recordType, fieldType);
276
277 if (recordTableSize == 0)
278 {
279 return PLDM_FRU_DATA_STRUCTURE_TABLE_UNAVAILABLE;
280 }
281
282 auto pads = utils::getNumPadBytes(recordTableSize);
Manojkiran Eda31a78442021-09-12 15:18:25 +0530283 crc32(fruData.data(), recordTableSize + pads);
John Wang9e82ad12020-06-12 10:53:32 +0800284
285 auto iter = fruData.begin() + recordTableSize + pads;
286 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
287 iter);
288 fruData.resize(recordTableSize + pads + sizeof(sum));
289
290 return PLDM_SUCCESS;
291}
292
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500293namespace fru
294{
295
296Response Handler::getFRURecordTableMetadata(const pldm_msg* request,
297 size_t /*payloadLength*/)
298{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530299 // FRU table is built lazily, build if not done.
300 buildFRUTable();
301
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500302 constexpr uint8_t major = 0x01;
303 constexpr uint8_t minor = 0x00;
304 constexpr uint32_t maxSize = 0xFFFFFFFF;
305
306 Response response(sizeof(pldm_msg_hdr) +
307 PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES,
308 0);
309 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
310
311 auto rc = encode_get_fru_record_table_metadata_resp(
312 request->hdr.instance_id, PLDM_SUCCESS, major, minor, maxSize,
313 impl.size(), impl.numRSI(), impl.numRecords(), impl.checkSum(),
314 responsePtr);
315 if (rc != PLDM_SUCCESS)
316 {
317 return ccOnlyResponse(request, rc);
318 }
319
320 return response;
321}
322
323Response Handler::getFRURecordTable(const pldm_msg* request,
324 size_t payloadLength)
325{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530326 // FRU table is built lazily, build if not done.
327 buildFRUTable();
328
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500329 if (payloadLength != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES)
330 {
331 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
332 }
333
334 Response response(
335 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES, 0);
336 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
337
338 auto rc =
339 encode_get_fru_record_table_resp(request->hdr.instance_id, PLDM_SUCCESS,
340 0, PLDM_START_AND_END, responsePtr);
341 if (rc != PLDM_SUCCESS)
342 {
343 return ccOnlyResponse(request, rc);
344 }
345
346 impl.getFRUTable(response);
347
348 return response;
349}
350
John Wang9e82ad12020-06-12 10:53:32 +0800351Response Handler::getFRURecordByOption(const pldm_msg* request,
352 size_t payloadLength)
353{
354 if (payloadLength != sizeof(pldm_get_fru_record_by_option_req))
355 {
356 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
357 }
358
359 uint32_t retDataTransferHandle{};
360 uint16_t retFruTableHandle{};
361 uint16_t retRecordSetIdentifier{};
362 uint8_t retRecordType{};
363 uint8_t retFieldType{};
364 uint8_t retTransferOpFlag{};
365
366 auto rc = decode_get_fru_record_by_option_req(
367 request, payloadLength, &retDataTransferHandle, &retFruTableHandle,
368 &retRecordSetIdentifier, &retRecordType, &retFieldType,
369 &retTransferOpFlag);
370
371 if (rc != PLDM_SUCCESS)
372 {
373 return ccOnlyResponse(request, rc);
374 }
375
376 std::vector<uint8_t> fruData;
377 rc = impl.getFRURecordByOption(fruData, retFruTableHandle,
378 retRecordSetIdentifier, retRecordType,
379 retFieldType);
380 if (rc != PLDM_SUCCESS)
381 {
382 return ccOnlyResponse(request, rc);
383 }
384
385 auto respPayloadLength =
386 PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + fruData.size();
387 Response response(sizeof(pldm_msg_hdr) + respPayloadLength, 0);
388 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
389
390 rc = encode_get_fru_record_by_option_resp(
391 request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END,
392 fruData.data(), fruData.size(), responsePtr, respPayloadLength);
393
394 if (rc != PLDM_SUCCESS)
395 {
396 return ccOnlyResponse(request, rc);
397 }
398
399 return response;
400}
401
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500402} // namespace fru
403
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500404} // namespace responder
405
406} // namespace pldm