blob: a42b63f0e0ba4ab64c2106113d7f1e41467b9648 [file] [log] [blame]
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001#pragma once
2
3#include "constants.hpp"
4#include "types.hpp"
5
6#include <nlohmann/json.hpp>
7
8#include <mutex>
9#include <optional>
10#include <semaphore>
11#include <tuple>
12
13namespace vpd
14{
15/**
16 * @brief A class to process and publish VPD data.
17 *
18 * The class works on VPD and is mainly responsible for following tasks:
19 * 1) Select appropriate device tree and JSON. Reboot if required.
20 * 2) Get desired parser using parser factory.
21 * 3) Calling respective parser class to get parsed VPD.
22 * 4) Arranging VPD data under required interfaces.
23 * 5) Calling PIM to publish VPD.
24 *
25 * The class may also implement helper functions required for VPD handling.
26 */
27class Worker
28{
29 public:
30 /**
31 * List of deleted functions.
32 */
33 Worker(const Worker&);
34 Worker& operator=(const Worker&);
35 Worker(Worker&&) = delete;
36
37 /**
38 * @brief Constructor.
39 *
40 * In case the processing is not JSON based, no argument needs to be passed.
41 * Constructor will also, based on symlink pick the correct JSON and
42 * initialize the parsed JSON variable.
43 *
44 * @param[in] pathToConfigJSON - Path to the config JSON, if applicable.
Sunny Srivastava765cf7b2025-02-04 05:24:11 -060045 * @param[in] i_maxThreadCount - Maximum thread while collecting FRUs VPD.
Souvik Roy31548842025-10-22 07:03:12 +000046 * @param[in] i_vpdCollectionMode - Mode in which VPD collection should take
47 * place.
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -050048 *
49 * Note: Throws std::exception in case of construction failure. Caller needs
50 * to handle to detect successful object creation.
51 */
Sunny Srivastava765cf7b2025-02-04 05:24:11 -060052 Worker(std::string pathToConfigJson = std::string(),
Souvik Roy31548842025-10-22 07:03:12 +000053 uint8_t i_maxThreadCount = constants::MAX_THREADS,
54 types::VpdCollectionMode i_vpdCollectionMode =
55 types::VpdCollectionMode::DEFAULT_MODE);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -050056
57 /**
58 * @brief Destructor
59 */
60 ~Worker() = default;
61
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -050062 /**
63 * @brief An API to check if system VPD is already published.
64 *
65 * @return Status, true if system is already collected else false.
66 */
67 bool isSystemVPDOnDBus() const;
68
69 /**
70 * @brief API to process all FRUs presnt in config JSON file.
71 *
72 * This API based on config JSON passed/selected for the system, will
73 * trigger parser for all the FRUs and publish it on DBus.
74 *
75 * Note: Config JSON file path should be passed to worker class constructor
76 * to make use of this API.
77 *
78 */
79 void collectFrusFromJson();
80
81 /**
82 * @brief API to parse VPD data
83 *
84 * @param[in] i_vpdFilePath - Path to the VPD file.
85 */
86 types::VPDMapVariant parseVpdFile(const std::string& i_vpdFilePath);
87
88 /**
89 * @brief An API to populate DBus interfaces for a FRU.
90 *
91 * Note: Call this API to populate D-Bus. Also caller should handle empty
92 * objectInterfaceMap.
93 *
94 * @param[in] parsedVpdMap - Parsed VPD as a map.
95 * @param[out] objectInterfaceMap - Object and its interfaces map.
96 * @param[in] vpdFilePath - EEPROM path of FRU.
97 */
98 void populateDbus(const types::VPDMapVariant& parsedVpdMap,
99 types::ObjectMap& objectInterfaceMap,
100 const std::string& vpdFilePath);
101
102 /**
103 * @brief An API to delete FRU VPD over DBus.
104 *
105 * @param[in] i_dbusObjPath - Dbus object path of the FRU.
106 *
107 * @throw std::runtime_error if given input path is empty.
108 */
109 void deleteFruVpd(const std::string& i_dbusObjPath);
110
111 /**
112 * @brief API to get status of VPD collection process.
113 *
114 * @return - True when done, false otherwise.
115 */
116 inline bool isAllFruCollectionDone() const
117 {
118 return m_isAllFruCollected;
119 }
120
121 /**
122 * @brief API to get system config JSON object
123 *
124 * @return System config JSON object.
125 */
126 inline nlohmann::json getSysCfgJsonObj() const
127 {
128 return m_parsedJson;
129 }
130
131 /**
132 * @brief API to get active thread count.
133 *
134 * Each FRU is collected in a separate thread. This API gives the active
135 * thread collecting FRU's VPD at any given time.
136 *
137 * @return Count of active threads.
138 */
139 size_t getActiveThreadCount() const
140 {
141 return m_activeCollectionThreadCount;
142 }
143
Souvik Roy1f4c8f82025-01-23 00:37:43 -0600144 /**
145 * @brief API to get list of EEPROMs for which thread creation failed.
146 *
147 * This API returns reference to list of EEPROM paths for which VPD
148 * collection thread creation has failed. Manager needs to process this list
149 * of EEPROMs and take appropriate action.
150 *
151 * @return reference to list of EEPROM paths for which VPD collection thread
152 * creation has failed
153 */
154 inline std::forward_list<std::string>& getFailedEepromPaths() noexcept
155 {
156 return m_failedEepromPaths;
157 }
158
Sunny Srivastava380efbb2025-04-25 10:28:30 +0530159 /**
Souvik Roy31548842025-10-22 07:03:12 +0000160 * @brief API to get VPD collection mode
161 *
162 * @return VPD collection mode enum value
163 */
164 inline types::VpdCollectionMode getVpdCollectionMode() const
165 {
166 return m_vpdCollectionMode;
167 }
168
169 /**
Sunny Srivastava380efbb2025-04-25 10:28:30 +0530170 * @brief Collect single FRU VPD
171 * API can be used to perform VPD collection for the given FRU, only if the
172 * current state of the system matches with the state at which the FRU is
173 * allowed for VPD recollection.
174 *
175 * @param[in] i_dbusObjPath - D-bus object path
176 */
177 void collectSingleFruVpd(
178 const sdbusplus::message::object_path& i_dbusObjPath);
179
180 /**
181 * @brief Perform VPD recollection
182 * This api will trigger parser to perform VPD recollection for FRUs that
183 * can be replaced at standby.
184 */
185 void performVpdRecollection();
186
Sunny Srivastava78a50422025-04-25 11:17:56 +0530187 /**
Sunny Srivastava78a50422025-04-25 11:17:56 +0530188 * @brief An API to set appropriate device tree and JSON.
189 *
190 * This API based on system chooses corresponding device tree and JSON.
191 * If device tree change is required, it updates the "fitconfig" and reboots
192 * the system. Else it is NOOP.
193 *
194 * @throw std::exception
195 */
196 void setDeviceTreeAndJson();
197
Anupama B R4c65fcd2025-09-01 08:09:00 -0500198 /**
199 * @brief API to set CollectionStatus property.
200 *
201 * This API updates the CollectionStatus property of the given FRU with the
202 * given value.
203 *
204 * @param[in] i_vpdPath - EEPROM or inventory path.
205 * @param[in] i_value - Value to be set.
206 */
207 void setCollectionStatusProperty(const std::string& i_fruPath,
208 const std::string& i_value) const noexcept;
209
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500210 private:
211 /**
212 * @brief An API to parse and publish a FRU VPD over D-Bus.
213 *
214 * Note: This API will handle all the exceptions internally and will only
215 * return status of parsing and publishing of VPD over D-Bus.
216 *
217 * @param[in] i_vpdFilePath - Path of file containing VPD.
218 * @return Tuple of status and file path. Status, true if successfull else
219 * false.
220 */
Patrick Williams43fedab2025-02-03 14:28:05 -0500221 std::tuple<bool, std::string> parseAndPublishVPD(
222 const std::string& i_vpdFilePath);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500223
224 /**
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500225 * @brief API to select system specific JSON.
226 *
227 * The API based on the IM value of VPD, will select appropriate JSON for
228 * the system. In case no system is found corresponding to the extracted IM
229 * value, error will be logged.
230 *
231 * @param[out] systemJson - System JSON name.
232 * @param[in] parsedVpdMap - Parsed VPD map.
233 */
234 void getSystemJson(std::string& systemJson,
235 const types::VPDMapVariant& parsedVpdMap);
236
237 /**
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500238 * @brief An API to parse given VPD file path.
239 *
Sunny Srivastava043955d2025-01-21 18:04:49 +0530240 * @throw std::exception
241 *
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500242 * @param[in] vpdFilePath - EEPROM file path.
243 * @param[out] parsedVpd - Parsed VPD as a map.
244 */
245 void fillVPDMap(const std::string& vpdFilePath,
246 types::VPDMapVariant& parsedVpd);
247
248 /**
249 * @brief An API to parse and publish system VPD on D-Bus.
250 *
251 * Note: Throws exception in case of invalid VPD format.
252 *
253 * @param[in] parsedVpdMap - Parsed VPD as a map.
254 */
255 void publishSystemVPD(const types::VPDMapVariant& parsedVpdMap);
256
257 /**
258 * @brief An API to process extrainterfaces w.r.t a FRU.
259 *
260 * @param[in] singleFru - JSON block for a single FRU.
261 * @param[out] interfaces - Map to hold interface along with its properties.
262 * @param[in] parsedVpdMap - Parsed VPD as a map.
263 */
264 void processExtraInterfaces(const nlohmann::json& singleFru,
265 types::InterfaceMap& interfaces,
266 const types::VPDMapVariant& parsedVpdMap);
267
268 /**
269 * @brief An API to process embedded and synthesized FRUs.
270 *
271 * @param[in] singleFru - FRU to be processed.
272 * @param[out] interfaces - Map to hold interface along with its properties.
273 */
274 void processEmbeddedAndSynthesizedFrus(const nlohmann::json& singleFru,
275 types::InterfaceMap& interfaces);
276
277 /**
278 * @brief An API to read process FRU based in CCIN.
279 *
280 * For some FRUs VPD can be processed only if the FRU has some specific
281 * value for CCIN. In case the value is not from that set, VPD for those
282 * FRUs can't be processed.
283 *
284 * @param[in] singleFru - Fru whose CCIN value needs to be matched.
285 * @param[in] parsedVpdMap - Parsed VPD map.
286 */
287 bool processFruWithCCIN(const nlohmann::json& singleFru,
288 const types::VPDMapVariant& parsedVpdMap);
289
290 /**
291 * @brief API to process json's inherit flag.
292 *
293 * Inherit flag denotes that some property in the child FRU needs to be
294 * inherited from parent FRU.
295 *
296 * @param[in] parsedVpdMap - Parsed VPD as a map.
297 * @param[out] interfaces - Map to hold interface along with its properties.
298 */
299 void processInheritFlag(const types::VPDMapVariant& parsedVpdMap,
300 types::InterfaceMap& interfaces);
301
302 /**
303 * @brief API to process json's "copyRecord" flag.
304 *
305 * copyRecord flag denotes if some record data needs to be copies in the
306 * given FRU.
307 *
308 * @param[in] singleFru - FRU being processed.
309 * @param[in] parsedVpdMap - Parsed VPD as a map.
310 * @param[out] interfaces - Map to hold interface along with its properties.
311 */
312 void processCopyRecordFlag(const nlohmann::json& singleFru,
313 const types::VPDMapVariant& parsedVpdMap,
314 types::InterfaceMap& interfaces);
315
316 /**
317 * @brief An API to populate IPZ VPD property map.
318 *
319 * @param[out] interfacePropMap - Map of interface and properties under it.
320 * @param[in] keyordValueMap - Keyword value map of IPZ VPD.
321 * @param[in] interfaceName - Name of the interface.
322 */
323 void populateIPZVPDpropertyMap(types::InterfaceMap& interfacePropMap,
324 const types::IPZKwdValueMap& keyordValueMap,
325 const std::string& interfaceName);
326
327 /**
328 * @brief An API to populate Kwd VPD property map.
329 *
330 * @param[in] keyordValueMap - Keyword value map of Kwd VPD.
331 * @param[out] interfaceMap - interface and property,value under it.
332 */
333 void populateKwdVPDpropertyMap(const types::KeywordVpdMap& keyordVPDMap,
334 types::InterfaceMap& interfaceMap);
335
336 /**
337 * @brief API to populate all required interface for a FRU.
338 *
339 * @param[in] interfaceJson - JSON containing interfaces to be populated.
340 * @param[out] interfaceMap - Map to hold populated interfaces.
341 * @param[in] parsedVpdMap - Parsed VPD as a map.
342 */
343 void populateInterfaces(const nlohmann::json& interfaceJson,
344 types::InterfaceMap& interfaceMap,
345 const types::VPDMapVariant& parsedVpdMap);
346
347 /**
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500348 * @brief Check if the given CPU is an IO only chip.
349 *
350 * The CPU is termed as IO, whose all of the cores are bad and can never be
351 * used. Those CPU chips can be used for IO purpose like connecting PCIe
352 * devices etc., The CPU whose every cores are bad, can be identified from
353 * the CP00 record's PG keyword, only if all of the 8 EQs' value equals
354 * 0xE7F9FF. (1EQ has 4 cores grouped together by sharing its cache memory.)
355 *
356 * @param [in] pgKeyword - PG Keyword of CPU.
357 * @return true if the given cpu is an IO, false otherwise.
358 */
359 bool isCPUIOGoodOnly(const std::string& pgKeyword);
360
361 /**
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500362 * @brief API to process preAction(base_action) defined in config JSON.
363 *
364 * @note sequence of tags under any given flag of preAction is EXTREMELY
365 * important to ensure proper processing. The API will process all the
366 * nested items under the base action sequentially. Also if any of the tag
367 * processing fails, the code will not process remaining tags under the
368 * flag.
369 * ******** sample format **************
370 * fru EEPROM path: {
371 * base_action: {
372 * flag1: {
373 * tag1: {
374 * },
375 * tag2: {
376 * }
377 * }
378 * flag2: {
379 * tags: {
380 * }
381 * }
382 * }
383 * }
384 * *************************************
385 *
386 * @param[in] i_vpdFilePath - Path to the EEPROM file.
387 * @param[in] i_flagToProcess - To identify which flag(s) needs to be
388 * processed under PreAction tag of config JSON.
Sunny Srivastava4f053df2025-09-03 02:27:37 -0500389 * @param[out] o_errCode - To set error code in case of error.
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500390 * @return Execution status.
391 */
392 bool processPreAction(const std::string& i_vpdFilePath,
Sunny Srivastava4f053df2025-09-03 02:27:37 -0500393 const std::string& i_flagToProcess,
394 uint16_t& o_errCode);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500395
396 /**
397 * @brief API to process postAction(base_action) defined in config JSON.
398 *
399 * @note Sequence of tags under any given flag of postAction is EXTREMELY
400 * important to ensure proper processing. The API will process all the
401 * nested items under the base action sequentially. Also if any of the tag
402 * processing fails, the code will not process remaining tags under the
403 * flag.
404 * ******** sample format **************
405 * fru EEPROM path: {
406 * base_action: {
407 * flag1: {
408 * tag1: {
409 * },
410 * tag2: {
411 * }
412 * }
413 * flag2: {
414 * tags: {
415 * }
416 * }
417 * }
418 * }
419 * *************************************
420 * Also, if post action is required to be processed only for FRUs with
421 * certain CCIN then CCIN list can be provided under flag.
422 *
423 * @param[in] i_vpdFruPath - Path to the EEPROM file.
424 * @param[in] i_flagToProcess - To identify which flag(s) needs to be
425 * processed under postAction tag of config JSON.
426 * @param[in] i_parsedVpd - Optional Parsed VPD map. If CCIN match is
427 * required.
428 * @return Execution status.
429 */
430 bool processPostAction(
431 const std::string& i_vpdFruPath, const std::string& i_flagToProcess,
432 const std::optional<types::VPDMapVariant> i_parsedVpd = std::nullopt);
433
434 /**
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500435 * @brief An API to perform backup or restore of VPD.
436 *
437 * @param[in,out] io_srcVpdMap - Source VPD map.
438 */
439 void performBackupAndRestore(types::VPDMapVariant& io_srcVpdMap);
440
441 /**
442 * @brief API to update "Functional" property.
443 *
444 * The API sets the default value for "Functional" property once if the
445 * property is not yet populated over DBus. As the property value is not
446 * controlled by the VPD-Collection process, if it is found already
447 * populated, the functions skips re-populating the property so that already
448 * existing value can be retained.
449 *
450 * @param[in] i_inventoryObjPath - Inventory path as read from config JSON.
451 * @param[in] io_interfaces - Map to hold all the interfaces for the FRU.
452 */
453 void processFunctionalProperty(const std::string& i_inventoryObjPath,
454 types::InterfaceMap& io_interfaces);
455
456 /**
457 * @brief API to update "enabled" property.
458 *
459 * The API sets the default value for "enabled" property once if the
460 * property is not yet populated over DBus. As the property value is not
461 * controlled by the VPD-Collection process, if it is found already
462 * populated, the functions skips re-populating the property so that already
463 * existing value can be retained.
464 *
465 * @param[in] i_inventoryObjPath - Inventory path as read from config JSON.
466 * @param[in] io_interfaces - Map to hold all the interfaces for the FRU.
467 */
468 void processEnabledProperty(const std::string& i_inventoryObjPath,
469 types::InterfaceMap& io_interfaces);
470
471 /**
472 * @brief API to form asset tag string for the system.
473 *
474 * @param[in] i_parsedVpdMap - Parsed VPD map.
475 *
476 * @throw std::runtime_error
477 *
478 * @return - Formed asset tag string.
479 */
Patrick Williams43fedab2025-02-03 14:28:05 -0500480 std::string createAssetTagString(
481 const types::VPDMapVariant& i_parsedVpdMap);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500482
483 /**
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500484 * @brief API to set symbolic link for system config JSON.
485 *
486 * Once correct device tree is set, symbolic link to the correct sytsem
487 * config JSON is set to be used in subsequent BMC boot.
488 *
489 * @param[in] i_systemJson - system config JSON.
490 */
491 void setJsonSymbolicLink(const std::string& i_systemJson);
492
Sunny Srivastavad159bb42025-01-09 11:13:50 +0530493 /**
494 * @brief API to set present property.
495 *
Souvik Roy6a9553c2025-02-07 01:16:32 -0600496 * This API updates the present property of the given FRU with the given
497 * value. Note: It is the responsibility of the caller to determine whether
498 * the present property for the FRU should be updated or not.
499 *
Sunny Srivastavad159bb42025-01-09 11:13:50 +0530500 * @param[in] i_vpdPath - EEPROM or inventory path.
501 * @param[in] i_value - value to be set.
502 */
503 void setPresentProperty(const std::string& i_fruPath, const bool& i_value);
504
Sunny Srivastava61611752025-02-04 00:29:33 -0600505 /**
506 * @brief API to check if the path needs to be skipped for collection.
507 *
508 * Some FRUs, under some given scenarios should not be collected and
509 * skipped.
510 *
Sunny Srivastava765cf7b2025-02-04 05:24:11 -0600511 * @param[in] i_vpdFilePath - EEPROM path.
512 *
Sunny Srivastava61611752025-02-04 00:29:33 -0600513 * @return True - if path is empty or should be skipped, false otherwise.
514 */
515 bool skipPathForCollection(const std::string& i_vpdFilePath);
516
Souvik Roy6a9553c2025-02-07 01:16:32 -0600517 /**
518 * @brief API to check if present property should be handled for given FRU.
519 *
520 * vpd-manager should update present property for a FRU if and only if it's
521 * not synthesized and vpd-manager handles present property for the FRU.
522 * This API assumes "handlePresence" tag is a subset of "synthesized" tag.
523 *
524 * @param[in] i_fru - JSON block for a single FRU.
525 *
526 * @return true if present property should be handled, false otherwise.
527 */
528 inline bool isPresentPropertyHandlingRequired(
529 const nlohmann::json& i_fru) const noexcept
530 {
531 // TODO: revisit this to see if this logic can be optimized.
532 return !i_fru.value("synthesized", false) &&
533 i_fru.value("handlePresence", true);
534 }
535
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500536 // Parsed JSON file.
537 nlohmann::json m_parsedJson{};
538
539 // Hold if symlink is present or not.
540 bool m_isSymlinkPresent = false;
541
542 // Path to config JSON if applicable.
543 std::string& m_configJsonPath;
544
545 // Keeps track of active thread(s) doing VPD collection.
546 size_t m_activeCollectionThreadCount = 0;
547
548 // Holds status, if VPD collection has been done or not.
549 // Note: This variable does not give information about successfull or failed
550 // collection. It just states, if the VPD collection process is over or not.
551 bool m_isAllFruCollected = false;
552
553 // To distinguish the factory reset path.
554 bool m_isFactoryResetDone = false;
555
556 // Mutex to guard critical resource m_activeCollectionThreadCount.
557 std::mutex m_mutex;
558
559 // Counting semaphore to limit the number of threads.
Sunny Srivastava765cf7b2025-02-04 05:24:11 -0600560 std::counting_semaphore<constants::MAX_THREADS> m_semaphore;
Souvik Roy1f4c8f82025-01-23 00:37:43 -0600561
562 // List of EEPROM paths for which VPD collection thread creation has failed.
563 std::forward_list<std::string> m_failedEepromPaths;
Souvik Roy31548842025-10-22 07:03:12 +0000564
565 // VPD collection mode
566 types::VpdCollectionMode m_vpdCollectionMode{
567 types::VpdCollectionMode::DEFAULT_MODE};
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500568};
569} // namespace vpd