blob: 1c04a127d603377fa7cd53657bbbe36e04ad478e [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
8#include <systemd/sd-journal.h>
9
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
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050015namespace pldm
16{
17
18namespace responder
19{
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
24 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 "
46 "creation failed";
47 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(
91 entityTree, &entity, parent,
92 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);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500113
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500114 if (table.size())
115 {
116 padBytes = utils::getNumPadBytes(table.size());
117 table.resize(table.size() + padBytes, 0);
118
119 // Calculate the checksum
George Liu077fea22020-04-08 16:47:14 +0800120 checksum = crc32(table.data(), table.size());
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500121 }
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530122 isBuilt = true;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500123}
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500124std::string FruImpl::populatefwVersion()
125{
126 static constexpr auto fwFunctionalObjPath =
127 "/xyz/openbmc_project/software/functional";
128 auto& bus = pldm::utils::DBusHandler::getBus();
129 std::string currentBmcVersion;
130 try
131 {
132 auto method =
133 bus.new_method_call(pldm::utils::mapperService, fwFunctionalObjPath,
134 pldm::utils::dbusProperties, "Get");
135 method.append("xyz.openbmc_project.Association", "endpoints");
136 std::variant<std::vector<std::string>> paths;
137 auto reply = bus.call(method);
138 reply.read(paths);
139 auto fwRunningVersion = std::get<std::vector<std::string>>(paths)[0];
140 constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
141 auto version = pldm::utils::DBusHandler().getDbusPropertyVariant(
142 fwRunningVersion.c_str(), "Version", versionIntf);
143 currentBmcVersion = std::get<std::string>(version);
144 }
145 catch (const std::exception& e)
146 {
147 std::cerr << "failed to make a d-bus call "
148 "Asociation, ERROR= "
149 << e.what() << "\n";
150 return {};
151 }
152 return currentBmcVersion;
153}
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500154void FruImpl::populateRecords(
155 const pldm::responder::dbus::InterfaceMap& interfaces,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500156 const fru_parser::FruRecordInfos& recordInfos, const pldm_entity& entity)
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500157{
158 // recordSetIdentifier for the FRU will be set when the first record gets
159 // added for the FRU
160 uint16_t recordSetIdentifier = 0;
161 auto numRecsCount = numRecs;
162
163 for (auto const& [recType, encType, fieldInfos] : recordInfos)
164 {
165 std::vector<uint8_t> tlvs;
166 uint8_t numFRUFields = 0;
167 for (auto const& [intf, prop, propType, fieldTypeNum] : fieldInfos)
168 {
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500169
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500170 try
171 {
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500172 pldm::responder::dbus::Value propValue;
173 if (entity.entity_type == PLDM_ENTITY_SYSTEM_LOGICAL &&
174 prop == "Version")
175 {
176 propValue = populatefwVersion();
177 }
178 else
179 {
180 propValue = interfaces.at(intf).at(prop);
181 }
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500182 if (propType == "bytearray")
183 {
184 auto byteArray = std::get<std::vector<uint8_t>>(propValue);
185 if (!byteArray.size())
186 {
187 continue;
188 }
189
190 numFRUFields++;
191 tlvs.emplace_back(fieldTypeNum);
192 tlvs.emplace_back(byteArray.size());
193 std::move(std::begin(byteArray), std::end(byteArray),
194 std::back_inserter(tlvs));
195 }
196 else if (propType == "string")
197 {
198 auto str = std::get<std::string>(propValue);
199 if (!str.size())
200 {
201 continue;
202 }
203
204 numFRUFields++;
205 tlvs.emplace_back(fieldTypeNum);
206 tlvs.emplace_back(str.size());
207 std::move(std::begin(str), std::end(str),
208 std::back_inserter(tlvs));
209 }
210 }
211 catch (const std::out_of_range& e)
212 {
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500213 continue;
214 }
215 }
216
217 if (tlvs.size())
218 {
219 if (numRecs == numRecsCount)
220 {
221 recordSetIdentifier = nextRSI();
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500222 pldm_pdr_add_fru_record_set(
223 pdrRepo, 0, recordSetIdentifier, entity.entity_type,
224 entity.entity_instance_num, entity.entity_container_id);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500225 }
226 auto curSize = table.size();
227 table.resize(curSize + recHeaderSize + tlvs.size());
228 encode_fru_record(table.data(), table.size(), &curSize,
229 recordSetIdentifier, recType, numFRUFields,
230 encType, tlvs.data(), tlvs.size());
231 numRecs++;
232 }
233 }
234}
235
236void FruImpl::getFRUTable(Response& response)
237{
238 auto hdrSize = response.size();
239
240 response.resize(hdrSize + table.size() + sizeof(checksum), 0);
241 std::copy(table.begin(), table.end(), response.begin() + hdrSize);
242
243 // Copy the checksum to response data
244 auto iter = response.begin() + hdrSize + table.size();
245 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
246 iter);
247}
248
John Wang9e82ad12020-06-12 10:53:32 +0800249int FruImpl::getFRURecordByOption(std::vector<uint8_t>& fruData,
250 uint16_t /* fruTableHandle */,
251 uint16_t recordSetIdentifer,
252 uint8_t recordType, uint8_t fieldType)
253{
254 // FRU table is built lazily, build if not done.
255 buildFRUTable();
256
257 /* 7 is sizeof(checksum,4) + padBytesMax(3)
258 * We can not know size of the record table got by options in advance, but
259 * it must be less than the source table. So it's safe to use sizeof the
260 * source table + 7 as the buffer length
261 */
262 size_t recordTableSize = table.size() - padBytes + 7;
263 fruData.resize(recordTableSize, 0);
264
265 get_fru_record_by_option(table.data(), table.size() - padBytes,
266 fruData.data(), &recordTableSize,
267 recordSetIdentifer, recordType, fieldType);
268
269 if (recordTableSize == 0)
270 {
271 return PLDM_FRU_DATA_STRUCTURE_TABLE_UNAVAILABLE;
272 }
273
274 auto pads = utils::getNumPadBytes(recordTableSize);
275 auto sum = crc32(fruData.data(), recordTableSize + pads);
276
277 auto iter = fruData.begin() + recordTableSize + pads;
278 std::copy_n(reinterpret_cast<const uint8_t*>(&checksum), sizeof(checksum),
279 iter);
280 fruData.resize(recordTableSize + pads + sizeof(sum));
281
282 return PLDM_SUCCESS;
283}
284
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500285namespace fru
286{
287
288Response Handler::getFRURecordTableMetadata(const pldm_msg* request,
289 size_t /*payloadLength*/)
290{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530291 // FRU table is built lazily, build if not done.
292 buildFRUTable();
293
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500294 constexpr uint8_t major = 0x01;
295 constexpr uint8_t minor = 0x00;
296 constexpr uint32_t maxSize = 0xFFFFFFFF;
297
298 Response response(sizeof(pldm_msg_hdr) +
299 PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES,
300 0);
301 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
302
303 auto rc = encode_get_fru_record_table_metadata_resp(
304 request->hdr.instance_id, PLDM_SUCCESS, major, minor, maxSize,
305 impl.size(), impl.numRSI(), impl.numRecords(), impl.checkSum(),
306 responsePtr);
307 if (rc != PLDM_SUCCESS)
308 {
309 return ccOnlyResponse(request, rc);
310 }
311
312 return response;
313}
314
315Response Handler::getFRURecordTable(const pldm_msg* request,
316 size_t payloadLength)
317{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530318 // FRU table is built lazily, build if not done.
319 buildFRUTable();
320
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500321 if (payloadLength != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES)
322 {
323 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
324 }
325
326 Response response(
327 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES, 0);
328 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
329
330 auto rc =
331 encode_get_fru_record_table_resp(request->hdr.instance_id, PLDM_SUCCESS,
332 0, PLDM_START_AND_END, responsePtr);
333 if (rc != PLDM_SUCCESS)
334 {
335 return ccOnlyResponse(request, rc);
336 }
337
338 impl.getFRUTable(response);
339
340 return response;
341}
342
John Wang9e82ad12020-06-12 10:53:32 +0800343Response Handler::getFRURecordByOption(const pldm_msg* request,
344 size_t payloadLength)
345{
346 if (payloadLength != sizeof(pldm_get_fru_record_by_option_req))
347 {
348 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
349 }
350
351 uint32_t retDataTransferHandle{};
352 uint16_t retFruTableHandle{};
353 uint16_t retRecordSetIdentifier{};
354 uint8_t retRecordType{};
355 uint8_t retFieldType{};
356 uint8_t retTransferOpFlag{};
357
358 auto rc = decode_get_fru_record_by_option_req(
359 request, payloadLength, &retDataTransferHandle, &retFruTableHandle,
360 &retRecordSetIdentifier, &retRecordType, &retFieldType,
361 &retTransferOpFlag);
362
363 if (rc != PLDM_SUCCESS)
364 {
365 return ccOnlyResponse(request, rc);
366 }
367
368 std::vector<uint8_t> fruData;
369 rc = impl.getFRURecordByOption(fruData, retFruTableHandle,
370 retRecordSetIdentifier, retRecordType,
371 retFieldType);
372 if (rc != PLDM_SUCCESS)
373 {
374 return ccOnlyResponse(request, rc);
375 }
376
377 auto respPayloadLength =
378 PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + fruData.size();
379 Response response(sizeof(pldm_msg_hdr) + respPayloadLength, 0);
380 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
381
382 rc = encode_get_fru_record_by_option_resp(
383 request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END,
384 fruData.data(), fruData.size(), responsePtr, respPayloadLength);
385
386 if (rc != PLDM_SUCCESS)
387 {
388 return ccOnlyResponse(request, rc);
389 }
390
391 return response;
392}
393
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500394} // namespace fru
395
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500396} // namespace responder
397
398} // namespace pldm