blob: 6586fad4b1d16079e74ba03611e574c187f4a91b [file] [log] [blame]
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05001#pragma once
2
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05003#include "fru_parser.hpp"
Pavithra Barithayaa410c652021-07-22 01:32:47 -05004#include "libpldmresponder/pdr_utils.hpp"
5#include "oem_handler.hpp"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -05006#include "pldmd/handler.hpp"
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05007
George Liuc453e162022-12-21 17:16:23 +08008#include <libpldm/fru.h>
9#include <libpldm/pdr.h>
10
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050011#include <sdbusplus/message.hpp>
George Liu6492f522020-06-16 10:34:05 +080012
13#include <map>
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050014#include <string>
15#include <variant>
16#include <vector>
17
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050018namespace pldm
19{
20
21namespace responder
22{
23
24namespace dbus
25{
26
Unive Tienc40d4a62025-03-12 11:36:07 +080027using Value =
28 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
29 uint64_t, double, std::string, std::vector<uint8_t>,
30 std::vector<uint64_t>, std::vector<std::string>>;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050031using PropertyMap = std::map<Property, Value>;
32using InterfaceMap = std::map<Interface, PropertyMap>;
33using ObjectValueTree = std::map<sdbusplus::message::object_path, InterfaceMap>;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050034using ObjectPath = std::string;
George Liuc4ea6a92020-07-14 15:48:44 +080035using AssociatedEntityMap = std::map<ObjectPath, pldm_entity>;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050036
37} // namespace dbus
38
39/** @class FruImpl
40 *
41 * @brief Builds the PLDM FRU table containing the FRU records
42 */
43class FruImpl
44{
45 public:
46 /* @brief Header size for FRU record, it includes the FRU record set
47 * identifier, FRU record type, Number of FRU fields, Encoding type
48 * of FRU fields
49 */
50 static constexpr size_t recHeaderSize =
51 sizeof(struct pldm_fru_record_data_format) -
52 sizeof(struct pldm_fru_record_tlv);
53
Tom Joseph33e9c7e2020-06-11 22:09:52 +053054 /** @brief Constructor for FruImpl, the configPath is consumed to build the
55 * FruParser object.
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050056 *
57 * @param[in] configPath - path to the directory containing config files
Tom Joseph33e9c7e2020-06-11 22:09:52 +053058 * for PLDM FRU
Manojkiran Eda03b01ca2021-06-29 08:55:09 +053059 * @param[in] fruMasterJsonPath - path to the file containing the FRU D-Bus
60 * Lookup Map
Tom Joseph33e9c7e2020-06-11 22:09:52 +053061 * @param[in] pdrRepo - opaque pointer to PDR repository
62 * @param[in] entityTree - opaque pointer to the entity association tree
Sampa Misrac073a202021-05-08 10:56:05 -050063 * @param[in] bmcEntityTree - opaque pointer to bmc's entity association
64 * tree
Pavithra Barithayaa410c652021-07-22 01:32:47 -050065 * @param[in] oemFruHandler - OEM fru handler
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050066 */
Manojkiran Eda03b01ca2021-06-29 08:55:09 +053067 FruImpl(const std::string& configPath,
68 const std::filesystem::path& fruMasterJsonPath, pldm_pdr* pdrRepo,
Sampa Misrac073a202021-05-08 10:56:05 -050069 pldm_entity_association_tree* entityTree,
George Liua881c172021-06-21 18:28:11 +080070 pldm_entity_association_tree* bmcEntityTree) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -040071 parser(configPath, fruMasterJsonPath), pdrRepo(pdrRepo),
72 entityTree(entityTree), bmcEntityTree(bmcEntityTree)
Tom Joseph33e9c7e2020-06-11 22:09:52 +053073 {}
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050074
Pavithra Barithaya9e6631e2024-01-22 00:46:32 -060075 /** @brief Total length of the FRU table in bytes, this includes the pad
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050076 * bytes and the checksum.
77 *
78 * @return size of the FRU table
79 */
80 uint32_t size() const
81 {
Pavithra Barithaya9e6631e2024-01-22 00:46:32 -060082 return table.size();
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050083 }
84
85 /** @brief The checksum of the contents of the FRU table
86 *
87 * @return checksum
88 */
89 uint32_t checkSum() const
90 {
91 return checksum;
92 }
93
94 /** @brief Number of record set identifiers in the FRU tables
95 *
96 * @return number of record set identifiers
97 */
98 uint16_t numRSI() const
99 {
100 return rsi;
101 }
102
103 /** @brief The number of FRU records in the table
104 *
105 * @return number of FRU records
106 */
107 uint16_t numRecords() const
108 {
109 return numRecs;
110 }
111
112 /** @brief Get the FRU table
113 *
114 * @param[out] - Populate response with the FRU table
115 */
116 void getFRUTable(Response& response);
117
Pavithra Barithaya9e6631e2024-01-22 00:46:32 -0600118 /** @brief Get the Fru Table MetaData
119 *
120 */
121 void getFRURecordTableMetadata();
122
John Wang9e82ad12020-06-12 10:53:32 +0800123 /** @brief Get FRU Record Table By Option
124 * @param[out] response - Populate response with the FRU table got by
125 * options
126 * @param[in] fruTableHandle - The fru table handle
127 * @param[in] recordSetIdentifer - The record set identifier
128 * @param[in] recordType - The record type
129 * @param[in] fieldType - The field type
130 */
131 int getFRURecordByOption(Response& response, uint16_t fruTableHandle,
132 uint16_t recordSetIdentifer, uint8_t recordType,
133 uint8_t fieldType);
134
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530135 /** @brief FRU table is built by processing the D-Bus inventory namespace
136 * based on the config files for FRU. The table is populated based
137 * on the isBuilt flag.
138 */
139 void buildFRUTable();
140
George Liuc4ea6a92020-07-14 15:48:44 +0800141 /** @brief Get std::map associated with the entity
142 * key: object path
143 * value: pldm_entity
144 *
145 * @return std::map<ObjectPath, pldm_entity>
146 */
147 inline const pldm::responder::dbus::AssociatedEntityMap&
148 getAssociateEntityMap() const
149 {
150 return associatedEntityMap;
151 }
152
George Liu5bfb0dc2021-05-01 14:28:41 +0800153 /** @brief Get pldm entity by the object path
154 *
155 * @param[in] intfMaps - D-Bus interfaces and the associated property
156 * values for the FRU
157 *
158 * @return pldm_entity
159 */
Patrick Williams366507c2025-02-03 14:28:01 -0500160 std::optional<pldm_entity> getEntityByObjectPath(
161 const dbus::InterfaceMap& intfMaps);
George Liu5bfb0dc2021-05-01 14:28:41 +0800162
163 /** @brief Update pldm entity to association tree
164 *
165 * @param[in] objects - std::map The object value tree
166 * @param[in] path - Object path
167 *
168 * Ex: Input path =
169 * "/xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0"
170 *
171 * Get the parent class in turn and store it in a temporary vector
172 *
173 * Output tmpObjPaths = {
174 * "/xyz/openbmc_project/inventory/system",
175 * "/xyz/openbmc_project/inventory/system/chassis/",
176 * "/xyz/openbmc_project/inventory/system/chassis/motherboard",
177 * "/xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0"}
178 *
179 */
180 void updateAssociationTree(const dbus::ObjectValueTree& objects,
181 const std::string& path);
182
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500183 /* @brief Method to populate the firmware version ID
184 *
185 * @return firmware version ID
186 */
187 std::string populatefwVersion();
188
Pavithra Barithaya9e6631e2024-01-22 00:46:32 -0600189 /* @brief Method to resize the table
190 *
191 * @return resized table
192 */
193 std::vector<uint8_t> tableResize();
194
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500195 /* @brief set FRU Record Table
196 *
197 * @param[in] fruData - the data of the fru
198 *
199 * @return PLDM completion code
200 */
201 int setFRUTable(const std::vector<uint8_t>& fruData);
202
George Liua881c172021-06-21 18:28:11 +0800203 /* @brief Method to set the oem platform handler in fru handler class
204 *
205 * @param[in] handler - oem fru handler
206 */
207 inline void setOemFruHandler(pldm::responder::oem_fru::Handler* handler)
208 {
209 oemFruHandler = handler;
210 }
211
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500212 private:
213 uint16_t nextRSI()
214 {
215 return ++rsi;
216 }
217
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600218 uint32_t nextRecordHandle()
219 {
220 return ++rh;
221 }
222
223 uint32_t rh = 0;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500224 uint16_t rsi = 0;
225 uint16_t numRecs = 0;
226 uint8_t padBytes = 0;
227 std::vector<uint8_t> table;
228 uint32_t checksum = 0;
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530229 bool isBuilt = false;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500230
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530231 fru_parser::FruParser parser;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500232 pldm_pdr* pdrRepo;
233 pldm_entity_association_tree* entityTree;
Sampa Misrac073a202021-05-08 10:56:05 -0500234 pldm_entity_association_tree* bmcEntityTree;
George Liua881c172021-06-21 18:28:11 +0800235 pldm::responder::oem_fru::Handler* oemFruHandler = nullptr;
Riya Dixit754041d2024-02-20 06:15:49 -0600236 dbus::ObjectValueTree objects;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500237
238 std::map<dbus::ObjectPath, pldm_entity_node*> objToEntityNode{};
239
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500240 /** @brief populateRecord builds the FRU records for an instance of FRU and
241 * updates the FRU table with the FRU records.
242 *
243 * @param[in] interfaces - D-Bus interfaces and the associated property
244 * values for the FRU
245 * @param[in] recordInfos - FRU record info to build the FRU records
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500246 * @param[in/out] entity - PLDM entity corresponding to FRU instance
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500247 */
George Liuc4ea6a92020-07-14 15:48:44 +0800248 void populateRecords(const dbus::InterfaceMap& interfaces,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500249 const fru_parser::FruRecordInfos& recordInfos,
250 const pldm_entity& entity);
George Liuc4ea6a92020-07-14 15:48:44 +0800251
Pavithra Barithaya129dab12025-04-24 16:13:03 +0530252 /** @brief Deletes a FRU record from record set table.
253 * @param[in] rsi - the FRU Record Set Identifier
254 *
255 * @return
256 */
257 void deleteFRURecord(uint16_t rsi);
258
George Liuc4ea6a92020-07-14 15:48:44 +0800259 /** @brief Associate sensor/effecter to FRU entity
260 */
261 dbus::AssociatedEntityMap associatedEntityMap;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500262};
263
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500264namespace fru
265{
266
267class Handler : public CmdHandler
268{
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500269 public:
Manojkiran Eda03b01ca2021-06-29 08:55:09 +0530270 Handler(const std::string& configPath,
271 const std::filesystem::path& fruMasterJsonPath, pldm_pdr* pdrRepo,
Sampa Misrac073a202021-05-08 10:56:05 -0500272 pldm_entity_association_tree* entityTree,
George Liua881c172021-06-21 18:28:11 +0800273 pldm_entity_association_tree* bmcEntityTree) :
274 impl(configPath, fruMasterJsonPath, pdrRepo, entityTree, bmcEntityTree)
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500275 {
Delphine CC Chiud2e48992023-12-05 16:29:51 +0800276 handlers.emplace(
277 PLDM_GET_FRU_RECORD_TABLE_METADATA,
278 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400279 return this->getFRURecordTableMetadata(request, payloadLength);
280 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +0800281 handlers.emplace(
282 PLDM_GET_FRU_RECORD_TABLE,
283 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400284 return this->getFRURecordTable(request, payloadLength);
285 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +0800286 handlers.emplace(
287 PLDM_GET_FRU_RECORD_BY_OPTION,
288 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400289 return this->getFRURecordByOption(request, payloadLength);
290 });
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500291 handlers.emplace(
292 PLDM_SET_FRU_RECORD_TABLE,
293 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400294 return this->setFRURecordTable(request, payloadLength);
295 });
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500296 }
297
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500298 /** @brief Handler for Get FRURecordTableMetadata
299 *
300 * @param[in] request - Request message payload
301 * @param[in] payloadLength - Request payload length
302 *
303 * @return PLDM response message
304 */
305 Response getFRURecordTableMetadata(const pldm_msg* request,
306 size_t payloadLength);
307
308 /** @brief Handler for GetFRURecordTable
309 *
310 * @param[in] request - Request message payload
311 * @param[in] payloadLength - Request payload length
312 *
313 * @return PLDM response message
314 */
315 Response getFRURecordTable(const pldm_msg* request, size_t payloadLength);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500316
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530317 /** @brief Build FRU table is bnot already built
318 *
319 */
320 void buildFRUTable()
321 {
322 impl.buildFRUTable();
323 }
324
George Liuc4ea6a92020-07-14 15:48:44 +0800325 /** @brief Get std::map associated with the entity
326 * key: object path
327 * value: pldm_entity
328 *
329 * @return std::map<ObjectPath, pldm_entity>
330 */
Patrick Williams366507c2025-02-03 14:28:01 -0500331 const pldm::responder::dbus::AssociatedEntityMap& getAssociateEntityMap()
332 const
George Liuc4ea6a92020-07-14 15:48:44 +0800333 {
334 return impl.getAssociateEntityMap();
335 }
336
John Wang9e82ad12020-06-12 10:53:32 +0800337 /** @brief Handler for GetFRURecordByOption
338 *
339 * @param[in] request - Request message payload
340 * @param[in] payloadLength - Request payload length
341 *
342 * @return PLDM response message
343 */
344 Response getFRURecordByOption(const pldm_msg* request,
345 size_t payloadLength);
346
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500347 /** @brief Handler for SetFRURecordTable
348 *
349 * @param[in] request - Request message
350 * @param[in] payloadLength - Request payload length
351 *
352 * @return PLDM response message
353 */
354 Response setFRURecordTable(const pldm_msg* request, size_t payloadLength);
355
George Liua881c172021-06-21 18:28:11 +0800356 /* @brief Method to set the oem platform handler in fru handler class
357 *
358 * @param[in] handler - oem fru handler
359 */
360 void setOemFruHandler(pldm::responder::oem_fru::Handler* handler)
361 {
362 impl.setOemFruHandler(handler);
363 }
364
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500365 using Table = std::vector<uint8_t>;
366
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500367 private:
368 FruImpl impl;
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500369};
370
371} // namespace fru
372
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500373} // namespace responder
374
375} // namespace pldm