1abb87edaSPriyangaRamasamy #include "config.h"
2abb87edaSPriyangaRamasamy 
36c71c9dcSSunny Srivastava #include "common_utility.hpp"
4abb87edaSPriyangaRamasamy #include "defines.hpp"
56c71c9dcSSunny Srivastava #include "ibm_vpd_utils.hpp"
6e12b181bSSunnySrivastava1984 #include "ipz_parser.hpp"
7abb87edaSPriyangaRamasamy #include "keyword_vpd_parser.hpp"
8a00936f8SAlpana Kumari #include "memory_vpd_parser.hpp"
9e12b181bSSunnySrivastava1984 #include "parser_factory.hpp"
10a20be8ecSSunnySrivastava1984 #include "vpd_exceptions.hpp"
11abb87edaSPriyangaRamasamy 
129094d4f6SSunnySrivastava1984 #include <assert.h>
138ea3f6d0SAlpana Kumari #include <ctype.h>
148ea3f6d0SAlpana Kumari 
15abb87edaSPriyangaRamasamy #include <CLI/CLI.hpp>
1688edeb6fSSantosh Puranik #include <algorithm>
177ce68724Salpana07 #include <boost/algorithm/string.hpp>
1865b83601SAlpana Kumari #include <cstdarg>
19abb87edaSPriyangaRamasamy #include <exception>
2083a1d5deSPriyangaRamasamy #include <filesystem>
21abb87edaSPriyangaRamasamy #include <fstream>
222f793048SAlpana Kumari #include <gpiod.hpp>
23abb87edaSPriyangaRamasamy #include <iostream>
24abb87edaSPriyangaRamasamy #include <iterator>
25abb87edaSPriyangaRamasamy #include <nlohmann/json.hpp>
26280197e3SAndrew Geissler #include <phosphor-logging/log.hpp>
277ce68724Salpana07 #include <regex>
28abb87edaSPriyangaRamasamy 
29abb87edaSPriyangaRamasamy using namespace std;
30abb87edaSPriyangaRamasamy using namespace openpower::vpd;
31abb87edaSPriyangaRamasamy using namespace CLI;
32abb87edaSPriyangaRamasamy using namespace vpd::keyword::parser;
3383a1d5deSPriyangaRamasamy using namespace openpower::vpd::constants;
3483a1d5deSPriyangaRamasamy namespace fs = filesystem;
3583a1d5deSPriyangaRamasamy using json = nlohmann::json;
36e12b181bSSunnySrivastava1984 using namespace openpower::vpd::parser::factory;
37945a02d3SSunnySrivastava1984 using namespace openpower::vpd::inventory;
38a00936f8SAlpana Kumari using namespace openpower::vpd::memory::parser;
39e12b181bSSunnySrivastava1984 using namespace openpower::vpd::parser::interface;
40a20be8ecSSunnySrivastava1984 using namespace openpower::vpd::exceptions;
41280197e3SAndrew Geissler using namespace phosphor::logging;
42abb87edaSPriyangaRamasamy 
4365b83601SAlpana Kumari static const deviceTreeMap deviceTreeSystemTypeMap = {
446fb0ef90SJoel Stanley     {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
456fb0ef90SJoel Stanley     {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
466fb0ef90SJoel Stanley     {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
476fb0ef90SJoel Stanley     {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
482fe709f1SAndrew Geissler     {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
492fe709f1SAndrew Geissler     {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}};
5065b83601SAlpana Kumari 
5188edeb6fSSantosh Puranik /**
528589375fSSantosh Puranik  * @brief Returns the power state for chassis0
538589375fSSantosh Puranik  */
548589375fSSantosh Puranik static auto getPowerState()
558589375fSSantosh Puranik {
568589375fSSantosh Puranik     // TODO: How do we handle multiple chassis?
578589375fSSantosh Puranik     string powerState{};
588589375fSSantosh Puranik     auto bus = sdbusplus::bus::new_default();
598589375fSSantosh Puranik     auto properties =
608589375fSSantosh Puranik         bus.new_method_call("xyz.openbmc_project.State.Chassis",
618589375fSSantosh Puranik                             "/xyz/openbmc_project/state/chassis0",
628589375fSSantosh Puranik                             "org.freedesktop.DBus.Properties", "Get");
638589375fSSantosh Puranik     properties.append("xyz.openbmc_project.State.Chassis");
648589375fSSantosh Puranik     properties.append("CurrentPowerState");
658589375fSSantosh Puranik     auto result = bus.call(properties);
668589375fSSantosh Puranik     if (!result.is_method_error())
678589375fSSantosh Puranik     {
688589375fSSantosh Puranik         variant<string> val;
698589375fSSantosh Puranik         result.read(val);
708589375fSSantosh Puranik         if (auto pVal = get_if<string>(&val))
718589375fSSantosh Puranik         {
728589375fSSantosh Puranik             powerState = *pVal;
738589375fSSantosh Puranik         }
748589375fSSantosh Puranik     }
758589375fSSantosh Puranik     cout << "Power state is: " << powerState << endl;
768589375fSSantosh Puranik     return powerState;
778589375fSSantosh Puranik }
788589375fSSantosh Puranik 
798589375fSSantosh Puranik /**
8088edeb6fSSantosh Puranik  * @brief Expands location codes
8188edeb6fSSantosh Puranik  */
8288edeb6fSSantosh Puranik static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
8388edeb6fSSantosh Puranik                                bool isSystemVpd)
8488edeb6fSSantosh Puranik {
8588edeb6fSSantosh Puranik     auto expanded{unexpanded};
8688edeb6fSSantosh Puranik     static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
8788edeb6fSSantosh Puranik     static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
8888edeb6fSSantosh Puranik     static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
8988edeb6fSSantosh Puranik     size_t idx = expanded.find("fcs");
9088edeb6fSSantosh Puranik     try
9188edeb6fSSantosh Puranik     {
9288edeb6fSSantosh Puranik         if (idx != string::npos)
9388edeb6fSSantosh Puranik         {
9488edeb6fSSantosh Puranik             string fc{};
9588edeb6fSSantosh Puranik             string se{};
9688edeb6fSSantosh Puranik             if (isSystemVpd)
9788edeb6fSSantosh Puranik             {
9888edeb6fSSantosh Puranik                 const auto& fcData = vpdMap.at("VCEN").at("FC");
9988edeb6fSSantosh Puranik                 const auto& seData = vpdMap.at("VCEN").at("SE");
10088edeb6fSSantosh Puranik                 fc = string(fcData.data(), fcData.size());
10188edeb6fSSantosh Puranik                 se = string(seData.data(), seData.size());
10288edeb6fSSantosh Puranik             }
10388edeb6fSSantosh Puranik             else
10488edeb6fSSantosh Puranik             {
10588edeb6fSSantosh Puranik                 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
10688edeb6fSSantosh Puranik                 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
10788edeb6fSSantosh Puranik             }
10888edeb6fSSantosh Puranik 
10981671f6dSAlpana Kumari             // TODO: See if ND0 can be placed in the JSON
11081671f6dSAlpana Kumari             expanded.replace(idx, 3, fc.substr(0, 4) + ".ND0." + se);
11188edeb6fSSantosh Puranik         }
11288edeb6fSSantosh Puranik         else
11388edeb6fSSantosh Puranik         {
11488edeb6fSSantosh Puranik             idx = expanded.find("mts");
11588edeb6fSSantosh Puranik             if (idx != string::npos)
11688edeb6fSSantosh Puranik             {
11788edeb6fSSantosh Puranik                 string mt{};
11888edeb6fSSantosh Puranik                 string se{};
11988edeb6fSSantosh Puranik                 if (isSystemVpd)
12088edeb6fSSantosh Puranik                 {
12188edeb6fSSantosh Puranik                     const auto& mtData = vpdMap.at("VSYS").at("TM");
12288edeb6fSSantosh Puranik                     const auto& seData = vpdMap.at("VSYS").at("SE");
12388edeb6fSSantosh Puranik                     mt = string(mtData.data(), mtData.size());
12488edeb6fSSantosh Puranik                     se = string(seData.data(), seData.size());
12588edeb6fSSantosh Puranik                 }
12688edeb6fSSantosh Puranik                 else
12788edeb6fSSantosh Puranik                 {
12888edeb6fSSantosh Puranik                     mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
12988edeb6fSSantosh Puranik                     se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
13088edeb6fSSantosh Puranik                 }
13188edeb6fSSantosh Puranik 
13288edeb6fSSantosh Puranik                 replace(mt.begin(), mt.end(), '-', '.');
13388edeb6fSSantosh Puranik                 expanded.replace(idx, 3, mt + "." + se);
13488edeb6fSSantosh Puranik             }
13588edeb6fSSantosh Puranik         }
13688edeb6fSSantosh Puranik     }
1378e15b93aSPatrick Williams     catch (const exception& e)
13888edeb6fSSantosh Puranik     {
13958e22145SAlpana Kumari         cerr << "Failed to expand location code with exception: " << e.what()
14058e22145SAlpana Kumari              << "\n";
14188edeb6fSSantosh Puranik     }
14288edeb6fSSantosh Puranik     return expanded;
14388edeb6fSSantosh Puranik }
1442f793048SAlpana Kumari 
145abb87edaSPriyangaRamasamy /**
146abb87edaSPriyangaRamasamy  * @brief Populate FRU specific interfaces.
147abb87edaSPriyangaRamasamy  *
148abb87edaSPriyangaRamasamy  * This is a common method which handles both
149abb87edaSPriyangaRamasamy  * ipz and keyword specific interfaces thus,
150abb87edaSPriyangaRamasamy  * reducing the code redundancy.
151abb87edaSPriyangaRamasamy  * @param[in] map - Reference to the innermost keyword-value map.
152abb87edaSPriyangaRamasamy  * @param[in] preIntrStr - Reference to the interface string.
153abb87edaSPriyangaRamasamy  * @param[out] interfaces - Reference to interface map.
154abb87edaSPriyangaRamasamy  */
155abb87edaSPriyangaRamasamy template <typename T>
156abb87edaSPriyangaRamasamy static void populateFruSpecificInterfaces(const T& map,
157abb87edaSPriyangaRamasamy                                           const string& preIntrStr,
158abb87edaSPriyangaRamasamy                                           inventory::InterfaceMap& interfaces)
159abb87edaSPriyangaRamasamy {
160abb87edaSPriyangaRamasamy     inventory::PropertyMap prop;
161abb87edaSPriyangaRamasamy 
162abb87edaSPriyangaRamasamy     for (const auto& kwVal : map)
163abb87edaSPriyangaRamasamy     {
164abb87edaSPriyangaRamasamy         auto kw = kwVal.first;
165abb87edaSPriyangaRamasamy 
166abb87edaSPriyangaRamasamy         if (kw[0] == '#')
167abb87edaSPriyangaRamasamy         {
16858e22145SAlpana Kumari             kw = string("PD_") + kw[1];
169abb87edaSPriyangaRamasamy         }
1708ea3f6d0SAlpana Kumari         else if (isdigit(kw[0]))
1718ea3f6d0SAlpana Kumari         {
17258e22145SAlpana Kumari             kw = string("N_") + kw;
1738ea3f6d0SAlpana Kumari         }
1743ab26a74SAlpana Kumari         if constexpr (is_same<T, KeywordVpdMap>::value)
1753ab26a74SAlpana Kumari         {
1763ab26a74SAlpana Kumari             if (get_if<Binary>(&kwVal.second))
1773ab26a74SAlpana Kumari             {
1783ab26a74SAlpana Kumari                 Binary vec(get_if<Binary>(&kwVal.second)->begin(),
1793ab26a74SAlpana Kumari                            get_if<Binary>(&kwVal.second)->end());
180abb87edaSPriyangaRamasamy                 prop.emplace(move(kw), move(vec));
181abb87edaSPriyangaRamasamy             }
1823ab26a74SAlpana Kumari             else
1833ab26a74SAlpana Kumari             {
1843ab26a74SAlpana Kumari                 if (kw == "MemorySizeInKB")
1853ab26a74SAlpana Kumari                 {
1863ab26a74SAlpana Kumari                     inventory::PropertyMap memProp;
1873ab26a74SAlpana Kumari                     auto memVal = get_if<size_t>(&kwVal.second);
1883ab26a74SAlpana Kumari                     if (memVal)
1893ab26a74SAlpana Kumari                     {
1903ab26a74SAlpana Kumari                         memProp.emplace(move(kw),
1913ab26a74SAlpana Kumari                                         ((*memVal) * CONVERT_MB_TO_KB));
1923ab26a74SAlpana Kumari                         interfaces.emplace(
1933ab26a74SAlpana Kumari                             "xyz.openbmc_project.Inventory.Item.Dimm",
1943ab26a74SAlpana Kumari                             move(memProp));
1953ab26a74SAlpana Kumari                     }
1963ab26a74SAlpana Kumari                     else
1973ab26a74SAlpana Kumari                     {
1983ab26a74SAlpana Kumari                         cerr << "MemorySizeInKB value not found in vpd map\n";
1993ab26a74SAlpana Kumari                     }
2003ab26a74SAlpana Kumari                 }
2013ab26a74SAlpana Kumari             }
2023ab26a74SAlpana Kumari         }
2033ab26a74SAlpana Kumari         else
2043ab26a74SAlpana Kumari         {
2053ab26a74SAlpana Kumari             Binary vec(kwVal.second.begin(), kwVal.second.end());
2063ab26a74SAlpana Kumari             prop.emplace(move(kw), move(vec));
2073ab26a74SAlpana Kumari         }
2083ab26a74SAlpana Kumari     }
209abb87edaSPriyangaRamasamy 
210abb87edaSPriyangaRamasamy     interfaces.emplace(preIntrStr, move(prop));
211abb87edaSPriyangaRamasamy }
212abb87edaSPriyangaRamasamy 
213abb87edaSPriyangaRamasamy /**
214abb87edaSPriyangaRamasamy  * @brief Populate Interfaces.
215abb87edaSPriyangaRamasamy  *
216abb87edaSPriyangaRamasamy  * This method populates common and extra interfaces to dbus.
217abb87edaSPriyangaRamasamy  * @param[in] js - json object
218abb87edaSPriyangaRamasamy  * @param[out] interfaces - Reference to interface map
219abb87edaSPriyangaRamasamy  * @param[in] vpdMap - Reference to the parsed vpd map.
22088edeb6fSSantosh Puranik  * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
221abb87edaSPriyangaRamasamy  */
222abb87edaSPriyangaRamasamy template <typename T>
223abb87edaSPriyangaRamasamy static void populateInterfaces(const nlohmann::json& js,
224abb87edaSPriyangaRamasamy                                inventory::InterfaceMap& interfaces,
22588edeb6fSSantosh Puranik                                const T& vpdMap, bool isSystemVpd)
226abb87edaSPriyangaRamasamy {
227abb87edaSPriyangaRamasamy     for (const auto& ifs : js.items())
228abb87edaSPriyangaRamasamy     {
22988edeb6fSSantosh Puranik         string inf = ifs.key();
230abb87edaSPriyangaRamasamy         inventory::PropertyMap props;
231abb87edaSPriyangaRamasamy 
232abb87edaSPriyangaRamasamy         for (const auto& itr : ifs.value().items())
233abb87edaSPriyangaRamasamy         {
23488edeb6fSSantosh Puranik             const string& busProp = itr.key();
23588edeb6fSSantosh Puranik 
23631970dedSAlpana Kumari             if (itr.value().is_boolean())
23731970dedSAlpana Kumari             {
23888edeb6fSSantosh Puranik                 props.emplace(busProp, itr.value().get<bool>());
23988edeb6fSSantosh Puranik             }
24088edeb6fSSantosh Puranik             else if (itr.value().is_string())
24188edeb6fSSantosh Puranik             {
24258e22145SAlpana Kumari                 if constexpr (is_same<T, Parsed>::value)
24388edeb6fSSantosh Puranik                 {
24488edeb6fSSantosh Puranik                     if (busProp == "LocationCode" &&
245414d5aefSAlpana Kumari                         inf == IBM_LOCATION_CODE_INF)
24688edeb6fSSantosh Puranik                     {
247414d5aefSAlpana Kumari                         // TODO deprecate the com.ibm interface later
24888edeb6fSSantosh Puranik                         auto prop = expandLocationCode(
24988edeb6fSSantosh Puranik                             itr.value().get<string>(), vpdMap, isSystemVpd);
25088edeb6fSSantosh Puranik                         props.emplace(busProp, prop);
251414d5aefSAlpana Kumari                         interfaces.emplace(XYZ_LOCATION_CODE_INF, props);
25269f76024SPriyanga Ramasamy                         interfaces.emplace(IBM_LOCATION_CODE_INF, props);
25388edeb6fSSantosh Puranik                     }
25488edeb6fSSantosh Puranik                     else
25588edeb6fSSantosh Puranik                     {
25688edeb6fSSantosh Puranik                         props.emplace(busProp, itr.value().get<string>());
25788edeb6fSSantosh Puranik                     }
25888edeb6fSSantosh Puranik                 }
25988edeb6fSSantosh Puranik                 else
26088edeb6fSSantosh Puranik                 {
26188edeb6fSSantosh Puranik                     props.emplace(busProp, itr.value().get<string>());
26288edeb6fSSantosh Puranik                 }
26331970dedSAlpana Kumari             }
264ed609affSSantosh Puranik             else if (itr.value().is_array())
265ed609affSSantosh Puranik             {
266ed609affSSantosh Puranik                 try
267ed609affSSantosh Puranik                 {
268ed609affSSantosh Puranik                     props.emplace(busProp, itr.value().get<Binary>());
269ed609affSSantosh Puranik                 }
2708e15b93aSPatrick Williams                 catch (const nlohmann::detail::type_error& e)
271ed609affSSantosh Puranik                 {
272ed609affSSantosh Puranik                     std::cerr << "Type exception: " << e.what() << "\n";
273ed609affSSantosh Puranik                     // Ignore any type errors
274ed609affSSantosh Puranik                 }
275ed609affSSantosh Puranik             }
27631970dedSAlpana Kumari             else if (itr.value().is_object())
27731970dedSAlpana Kumari             {
278abb87edaSPriyangaRamasamy                 const string& rec = itr.value().value("recordName", "");
279abb87edaSPriyangaRamasamy                 const string& kw = itr.value().value("keywordName", "");
280abb87edaSPriyangaRamasamy                 const string& encoding = itr.value().value("encoding", "");
281abb87edaSPriyangaRamasamy 
28258e22145SAlpana Kumari                 if constexpr (is_same<T, Parsed>::value)
283abb87edaSPriyangaRamasamy                 {
28488edeb6fSSantosh Puranik                     if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
28588edeb6fSSantosh Puranik                         vpdMap.at(rec).count(kw))
286abb87edaSPriyangaRamasamy                     {
287abb87edaSPriyangaRamasamy                         auto encoded =
288abb87edaSPriyangaRamasamy                             encodeKeyword(vpdMap.at(rec).at(kw), encoding);
28988edeb6fSSantosh Puranik                         props.emplace(busProp, encoded);
290abb87edaSPriyangaRamasamy                     }
291abb87edaSPriyangaRamasamy                 }
29258e22145SAlpana Kumari                 else if constexpr (is_same<T, KeywordVpdMap>::value)
293abb87edaSPriyangaRamasamy                 {
294abb87edaSPriyangaRamasamy                     if (!kw.empty() && vpdMap.count(kw))
295abb87edaSPriyangaRamasamy                     {
2963ab26a74SAlpana Kumari                         auto kwValue = get_if<Binary>(&vpdMap.at(kw));
2973ab26a74SAlpana Kumari                         auto uintValue = get_if<size_t>(&vpdMap.at(kw));
2983ab26a74SAlpana Kumari 
2993ab26a74SAlpana Kumari                         if (kwValue)
3003ab26a74SAlpana Kumari                         {
301abb87edaSPriyangaRamasamy                             auto prop =
3023ab26a74SAlpana Kumari                                 string((*kwValue).begin(), (*kwValue).end());
3033ab26a74SAlpana Kumari 
304abb87edaSPriyangaRamasamy                             auto encoded = encodeKeyword(prop, encoding);
3053ab26a74SAlpana Kumari 
30688edeb6fSSantosh Puranik                             props.emplace(busProp, encoded);
307abb87edaSPriyangaRamasamy                         }
3083ab26a74SAlpana Kumari                         else if (uintValue)
3093ab26a74SAlpana Kumari                         {
3103ab26a74SAlpana Kumari                             props.emplace(busProp, *uintValue);
3113ab26a74SAlpana Kumari                         }
3123ab26a74SAlpana Kumari                     }
313abb87edaSPriyangaRamasamy                 }
314abb87edaSPriyangaRamasamy             }
315b1e64bb6SMatt Spinler             else if (itr.value().is_number())
316b1e64bb6SMatt Spinler             {
317b1e64bb6SMatt Spinler                 // For now assume the value is a size_t.  In the future it would
318b1e64bb6SMatt Spinler                 // be nice to come up with a way to get the type from the JSON.
319b1e64bb6SMatt Spinler                 props.emplace(busProp, itr.value().get<size_t>());
320b1e64bb6SMatt Spinler             }
32131970dedSAlpana Kumari         }
322abb87edaSPriyangaRamasamy         interfaces.emplace(inf, move(props));
323abb87edaSPriyangaRamasamy     }
324abb87edaSPriyangaRamasamy }
325abb87edaSPriyangaRamasamy 
326*37233785SPriyanga Ramasamy /*API to reset EEPROM pointer to a safe position to avoid VPD corruption.
327*37233785SPriyanga Ramasamy  * Currently do reset only for DIMM VPD.*/
328*37233785SPriyanga Ramasamy static void resetEEPROMPointer(const nlohmann::json& js, const string& file,
329*37233785SPriyanga Ramasamy                                ifstream& vpdFile)
330*37233785SPriyanga Ramasamy {
331*37233785SPriyanga Ramasamy     for (const auto& item : js["frus"][file])
332*37233785SPriyanga Ramasamy     {
333*37233785SPriyanga Ramasamy         if (item.find("extraInterfaces") != item.end())
334*37233785SPriyanga Ramasamy         {
335*37233785SPriyanga Ramasamy             if (item["extraInterfaces"].find(
336*37233785SPriyanga Ramasamy                     "xyz.openbmc_project.Inventory.Item.Dimm") !=
337*37233785SPriyanga Ramasamy                 item["extraInterfaces"].end())
338*37233785SPriyanga Ramasamy             {
339*37233785SPriyanga Ramasamy                 // moves the EEPROM pointer to 2048 'th byte.
340*37233785SPriyanga Ramasamy                 vpdFile.seekg(2047, std::ios::beg);
341*37233785SPriyanga Ramasamy                 // Read that byte and discard - to affirm the move
342*37233785SPriyanga Ramasamy                 // operation.
343*37233785SPriyanga Ramasamy                 char ch;
344*37233785SPriyanga Ramasamy                 vpdFile.read(&ch, sizeof(ch));
345*37233785SPriyanga Ramasamy             }
346*37233785SPriyanga Ramasamy             return;
347*37233785SPriyanga Ramasamy         }
348*37233785SPriyanga Ramasamy     }
349*37233785SPriyanga Ramasamy }
350*37233785SPriyanga Ramasamy 
3512f793048SAlpana Kumari static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
35258e22145SAlpana Kumari {
35358e22145SAlpana Kumari     uint32_t offset = 0;
35458e22145SAlpana Kumari     // check if offset present?
35558e22145SAlpana Kumari     for (const auto& item : js["frus"][file])
35658e22145SAlpana Kumari     {
35758e22145SAlpana Kumari         if (item.find("offset") != item.end())
35858e22145SAlpana Kumari         {
35958e22145SAlpana Kumari             offset = item["offset"];
36058e22145SAlpana Kumari         }
36158e22145SAlpana Kumari     }
36258e22145SAlpana Kumari 
36358e22145SAlpana Kumari     // TODO: Figure out a better way to get max possible VPD size.
3643c2a2b9eSPriyanga Ramasamy     auto maxVPDSize = std::min(std::filesystem::file_size(file),
3653c2a2b9eSPriyanga Ramasamy                                static_cast<uintmax_t>(65504));
3663c2a2b9eSPriyanga Ramasamy 
36758e22145SAlpana Kumari     Binary vpdVector;
3683c2a2b9eSPriyanga Ramasamy     vpdVector.resize(maxVPDSize);
36958e22145SAlpana Kumari     ifstream vpdFile;
37058e22145SAlpana Kumari     vpdFile.open(file, ios::binary);
37158e22145SAlpana Kumari 
37258e22145SAlpana Kumari     vpdFile.seekg(offset, ios_base::cur);
3733c2a2b9eSPriyanga Ramasamy     vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), maxVPDSize);
37458e22145SAlpana Kumari     vpdVector.resize(vpdFile.gcount());
37558e22145SAlpana Kumari 
376*37233785SPriyanga Ramasamy     resetEEPROMPointer(js, file, vpdFile);
377*37233785SPriyanga Ramasamy 
37858e22145SAlpana Kumari     return vpdVector;
37958e22145SAlpana Kumari }
38058e22145SAlpana Kumari 
3812f793048SAlpana Kumari /** This API will be called at the end of VPD collection to perform any post
3822f793048SAlpana Kumari  * actions.
3832f793048SAlpana Kumari  *
3842f793048SAlpana Kumari  * @param[in] json - json object
3852f793048SAlpana Kumari  * @param[in] file - eeprom file path
3862f793048SAlpana Kumari  */
3872f793048SAlpana Kumari static void postFailAction(const nlohmann::json& json, const string& file)
3882f793048SAlpana Kumari {
3892f793048SAlpana Kumari     if ((json["frus"][file].at(0)).find("postActionFail") ==
3902f793048SAlpana Kumari         json["frus"][file].at(0).end())
3912f793048SAlpana Kumari     {
3922f793048SAlpana Kumari         return;
3932f793048SAlpana Kumari     }
3942f793048SAlpana Kumari 
3952f793048SAlpana Kumari     uint8_t pinValue = 0;
3962f793048SAlpana Kumari     string pinName;
3972f793048SAlpana Kumari 
3982f793048SAlpana Kumari     for (const auto& postAction :
3992f793048SAlpana Kumari          (json["frus"][file].at(0))["postActionFail"].items())
4002f793048SAlpana Kumari     {
4012f793048SAlpana Kumari         if (postAction.key() == "pin")
4022f793048SAlpana Kumari         {
4032f793048SAlpana Kumari             pinName = postAction.value();
4042f793048SAlpana Kumari         }
4052f793048SAlpana Kumari         else if (postAction.key() == "value")
4062f793048SAlpana Kumari         {
4072f793048SAlpana Kumari             // Get the value to set
4082f793048SAlpana Kumari             pinValue = postAction.value();
4092f793048SAlpana Kumari         }
4102f793048SAlpana Kumari     }
4112f793048SAlpana Kumari 
4122f793048SAlpana Kumari     cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
4132f793048SAlpana Kumari 
4142f793048SAlpana Kumari     try
4152f793048SAlpana Kumari     {
4162f793048SAlpana Kumari         gpiod::line outputLine = gpiod::find_line(pinName);
4172f793048SAlpana Kumari 
4182f793048SAlpana Kumari         if (!outputLine)
4192f793048SAlpana Kumari         {
4202f793048SAlpana Kumari             cout << "Couldn't find output line:" << pinName
4212f793048SAlpana Kumari                  << " on GPIO. Skipping...\n";
4222f793048SAlpana Kumari 
4232f793048SAlpana Kumari             return;
4242f793048SAlpana Kumari         }
4252f793048SAlpana Kumari         outputLine.request(
4262f793048SAlpana Kumari             {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
4272f793048SAlpana Kumari             pinValue);
4282f793048SAlpana Kumari     }
4298e15b93aSPatrick Williams     catch (const system_error&)
4302f793048SAlpana Kumari     {
4312f793048SAlpana Kumari         cerr << "Failed to set post-action GPIO" << endl;
4322f793048SAlpana Kumari     }
4332f793048SAlpana Kumari }
4342f793048SAlpana Kumari 
4352f793048SAlpana Kumari /** Performs any pre-action needed to get the FRU setup for collection.
4362f793048SAlpana Kumari  *
4372f793048SAlpana Kumari  * @param[in] json - json object
4382f793048SAlpana Kumari  * @param[in] file - eeprom file path
4392f793048SAlpana Kumari  */
4402f793048SAlpana Kumari static void preAction(const nlohmann::json& json, const string& file)
4412f793048SAlpana Kumari {
4422f793048SAlpana Kumari     if ((json["frus"][file].at(0)).find("preAction") ==
4432f793048SAlpana Kumari         json["frus"][file].at(0).end())
4442f793048SAlpana Kumari     {
4452f793048SAlpana Kumari         return;
4462f793048SAlpana Kumari     }
4472f793048SAlpana Kumari 
4482f793048SAlpana Kumari     uint8_t pinValue = 0;
4492f793048SAlpana Kumari     string pinName;
4502f793048SAlpana Kumari 
4512f793048SAlpana Kumari     for (const auto& postAction :
4522f793048SAlpana Kumari          (json["frus"][file].at(0))["preAction"].items())
4532f793048SAlpana Kumari     {
4542f793048SAlpana Kumari         if (postAction.key() == "pin")
4552f793048SAlpana Kumari         {
4562f793048SAlpana Kumari             pinName = postAction.value();
4572f793048SAlpana Kumari         }
4582f793048SAlpana Kumari         else if (postAction.key() == "value")
4592f793048SAlpana Kumari         {
4602f793048SAlpana Kumari             // Get the value to set
4612f793048SAlpana Kumari             pinValue = postAction.value();
4622f793048SAlpana Kumari         }
4632f793048SAlpana Kumari     }
4642f793048SAlpana Kumari 
4652f793048SAlpana Kumari     cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
4662f793048SAlpana Kumari     try
4672f793048SAlpana Kumari     {
4682f793048SAlpana Kumari         gpiod::line outputLine = gpiod::find_line(pinName);
4692f793048SAlpana Kumari 
4702f793048SAlpana Kumari         if (!outputLine)
4712f793048SAlpana Kumari         {
4722f793048SAlpana Kumari             cout << "Couldn't find output line:" << pinName
4732f793048SAlpana Kumari                  << " on GPIO. Skipping...\n";
4742f793048SAlpana Kumari 
4752f793048SAlpana Kumari             return;
4762f793048SAlpana Kumari         }
4772f793048SAlpana Kumari         outputLine.request(
4782f793048SAlpana Kumari             {"FRU pre-action", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
4792f793048SAlpana Kumari             pinValue);
4802f793048SAlpana Kumari     }
4818e15b93aSPatrick Williams     catch (const system_error&)
4822f793048SAlpana Kumari     {
4832f793048SAlpana Kumari         cerr << "Failed to set pre-action GPIO" << endl;
4842f793048SAlpana Kumari         return;
4852f793048SAlpana Kumari     }
4862f793048SAlpana Kumari 
4872f793048SAlpana Kumari     // Now bind the device
4884c3bf5beSAlpana Kumari     string bind = json["frus"][file].at(0).value("devAddress", "");
4892f793048SAlpana Kumari     cout << "Binding device " << bind << endl;
4902f793048SAlpana Kumari     string bindCmd = string("echo \"") + bind +
4912f793048SAlpana Kumari                      string("\" > /sys/bus/i2c/drivers/at24/bind");
4922f793048SAlpana Kumari     cout << bindCmd << endl;
4932f793048SAlpana Kumari     executeCmd(bindCmd);
4942f793048SAlpana Kumari 
4952f793048SAlpana Kumari     // Check if device showed up (test for file)
4962f793048SAlpana Kumari     if (!fs::exists(file))
4972f793048SAlpana Kumari     {
4982f793048SAlpana Kumari         cout << "EEPROM " << file << " does not exist. Take failure action"
4992f793048SAlpana Kumari              << endl;
5002f793048SAlpana Kumari         // If not, then take failure postAction
5012f793048SAlpana Kumari         postFailAction(json, file);
5022f793048SAlpana Kumari     }
5032f793048SAlpana Kumari }
5042f793048SAlpana Kumari 
505abb87edaSPriyangaRamasamy /**
506d3a379a6SSantosh Puranik  * @brief Set certain one time properties in the inventory
507d3a379a6SSantosh Puranik  * Use this function to insert the Functional and Enabled properties into the
508d3a379a6SSantosh Puranik  * inventory map. This function first checks if the object in question already
509d3a379a6SSantosh Puranik  * has these properties hosted on D-Bus, if the property is already there, it is
510d3a379a6SSantosh Puranik  * not modified, hence the name "one time". If the property is not already
511d3a379a6SSantosh Puranik  * present, it will be added to the map with a suitable default value (true for
512d3a379a6SSantosh Puranik  * Functional and false for Enabled)
513d3a379a6SSantosh Puranik  *
514d3a379a6SSantosh Puranik  * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
515d3a379a6SSantosh Puranik  * @param[inout] interfaces - Reference to a map of inventory interfaces to
516d3a379a6SSantosh Puranik  * which the properties will be attached.
517d3a379a6SSantosh Puranik  */
518d3a379a6SSantosh Puranik static void setOneTimeProperties(const std::string& object,
519d3a379a6SSantosh Puranik                                  inventory::InterfaceMap& interfaces)
520d3a379a6SSantosh Puranik {
521d3a379a6SSantosh Puranik     auto bus = sdbusplus::bus::new_default();
522d3a379a6SSantosh Puranik     auto objectPath = INVENTORY_PATH + object;
523d3a379a6SSantosh Puranik     auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
524d3a379a6SSantosh Puranik                                     objectPath.c_str(),
525d3a379a6SSantosh Puranik                                     "org.freedesktop.DBus.Properties", "Get");
526d3a379a6SSantosh Puranik     prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
527d3a379a6SSantosh Puranik     prop.append("Functional");
528d3a379a6SSantosh Puranik     try
529d3a379a6SSantosh Puranik     {
530d3a379a6SSantosh Puranik         auto result = bus.call(prop);
531d3a379a6SSantosh Puranik     }
532d3a379a6SSantosh Puranik     catch (const sdbusplus::exception::SdBusError& e)
533d3a379a6SSantosh Puranik     {
534d3a379a6SSantosh Puranik         // Treat as property unavailable
535d3a379a6SSantosh Puranik         inventory::PropertyMap prop;
536d3a379a6SSantosh Puranik         prop.emplace("Functional", true);
537d3a379a6SSantosh Puranik         interfaces.emplace(
538d3a379a6SSantosh Puranik             "xyz.openbmc_project.State.Decorator.OperationalStatus",
539d3a379a6SSantosh Puranik             move(prop));
540d3a379a6SSantosh Puranik     }
541d3a379a6SSantosh Puranik     prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
542d3a379a6SSantosh Puranik                                objectPath.c_str(),
543d3a379a6SSantosh Puranik                                "org.freedesktop.DBus.Properties", "Get");
544d3a379a6SSantosh Puranik     prop.append("xyz.openbmc_project.Object.Enable");
545d3a379a6SSantosh Puranik     prop.append("Enabled");
546d3a379a6SSantosh Puranik     try
547d3a379a6SSantosh Puranik     {
548d3a379a6SSantosh Puranik         auto result = bus.call(prop);
549d3a379a6SSantosh Puranik     }
550d3a379a6SSantosh Puranik     catch (const sdbusplus::exception::SdBusError& e)
551d3a379a6SSantosh Puranik     {
552d3a379a6SSantosh Puranik         // Treat as property unavailable
553d3a379a6SSantosh Puranik         inventory::PropertyMap prop;
554d3a379a6SSantosh Puranik         prop.emplace("Enabled", false);
555d3a379a6SSantosh Puranik         interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
556d3a379a6SSantosh Puranik     }
557d3a379a6SSantosh Puranik }
558d3a379a6SSantosh Puranik 
559d3a379a6SSantosh Puranik /**
5608e140a1cSPriyangaRamasamy  * @brief Prime the Inventory
5618e140a1cSPriyangaRamasamy  * Prime the inventory by populating only the location code,
5628e140a1cSPriyangaRamasamy  * type interface and the inventory object for the frus
5638e140a1cSPriyangaRamasamy  * which are not system vpd fru.
564abb87edaSPriyangaRamasamy  *
5658e140a1cSPriyangaRamasamy  * @param[in] jsObject - Reference to vpd inventory json object
5668e140a1cSPriyangaRamasamy  * @param[in] vpdMap -  Reference to the parsed vpd map
5678e140a1cSPriyangaRamasamy  *
5688e140a1cSPriyangaRamasamy  * @returns Map of items in extraInterface.
5698e140a1cSPriyangaRamasamy  */
5708e140a1cSPriyangaRamasamy template <typename T>
5718e140a1cSPriyangaRamasamy inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
5728e140a1cSPriyangaRamasamy                                     const T& vpdMap)
5738e140a1cSPriyangaRamasamy {
5748e140a1cSPriyangaRamasamy     inventory::ObjectMap objects;
5758e140a1cSPriyangaRamasamy 
5768e140a1cSPriyangaRamasamy     for (auto& itemFRUS : jsObject["frus"].items())
5778e140a1cSPriyangaRamasamy     {
5782f793048SAlpana Kumari         // Take pre actions
5792f793048SAlpana Kumari         preAction(jsObject, itemFRUS.key());
5808e140a1cSPriyangaRamasamy         for (auto& itemEEPROM : itemFRUS.value())
5818e140a1cSPriyangaRamasamy         {
5828e140a1cSPriyangaRamasamy             inventory::InterfaceMap interfaces;
5838e140a1cSPriyangaRamasamy             inventory::Object object(itemEEPROM.at("inventoryPath"));
5848e140a1cSPriyangaRamasamy 
58550f60bf8SSantosh Puranik             if ((itemFRUS.key() != systemVpdFilePath) &&
58650f60bf8SSantosh Puranik                 !itemEEPROM.value("noprime", false))
5878e140a1cSPriyangaRamasamy             {
588cfd7a75aSAlpana Kumari                 inventory::PropertyMap presProp;
589cfd7a75aSAlpana Kumari                 presProp.emplace("Present", false);
590cfd7a75aSAlpana Kumari                 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
591d3a379a6SSantosh Puranik                                    presProp);
592d3a379a6SSantosh Puranik                 setOneTimeProperties(object, interfaces);
5938e140a1cSPriyangaRamasamy                 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
5948e140a1cSPriyangaRamasamy                 {
5958e140a1cSPriyangaRamasamy                     for (const auto& eI : itemEEPROM["extraInterfaces"].items())
5968e140a1cSPriyangaRamasamy                     {
5978e140a1cSPriyangaRamasamy                         inventory::PropertyMap props;
598414d5aefSAlpana Kumari                         if (eI.key() == IBM_LOCATION_CODE_INF)
5998e140a1cSPriyangaRamasamy                         {
6008e140a1cSPriyangaRamasamy                             if constexpr (std::is_same<T, Parsed>::value)
6018e140a1cSPriyangaRamasamy                             {
6028e140a1cSPriyangaRamasamy                                 for (auto& lC : eI.value().items())
6038e140a1cSPriyangaRamasamy                                 {
6048e140a1cSPriyangaRamasamy                                     auto propVal = expandLocationCode(
6058e140a1cSPriyangaRamasamy                                         lC.value().get<string>(), vpdMap, true);
6068e140a1cSPriyangaRamasamy 
6078e140a1cSPriyangaRamasamy                                     props.emplace(move(lC.key()),
6088e140a1cSPriyangaRamasamy                                                   move(propVal));
609b0f3749dSSantosh Puranik                                     interfaces.emplace(XYZ_LOCATION_CODE_INF,
610b0f3749dSSantosh Puranik                                                        props);
6118e140a1cSPriyangaRamasamy                                     interfaces.emplace(move(eI.key()),
6128e140a1cSPriyangaRamasamy                                                        move(props));
6138e140a1cSPriyangaRamasamy                                 }
6148e140a1cSPriyangaRamasamy                             }
6158e140a1cSPriyangaRamasamy                         }
6168e140a1cSPriyangaRamasamy                         else if (eI.key().find("Inventory.Item.") !=
6178e140a1cSPriyangaRamasamy                                  string::npos)
6188e140a1cSPriyangaRamasamy                         {
6198e140a1cSPriyangaRamasamy                             interfaces.emplace(move(eI.key()), move(props));
6208e140a1cSPriyangaRamasamy                         }
621d3a379a6SSantosh Puranik                         else if (eI.key() ==
622d3a379a6SSantosh Puranik                                  "xyz.openbmc_project.Inventory.Item")
623d3a379a6SSantosh Puranik                         {
624d3a379a6SSantosh Puranik                             for (auto& val : eI.value().items())
625d3a379a6SSantosh Puranik                             {
626d3a379a6SSantosh Puranik                                 if (val.key() == "PrettyName")
627d3a379a6SSantosh Puranik                                 {
628d3a379a6SSantosh Puranik                                     presProp.emplace(val.key(),
629d3a379a6SSantosh Puranik                                                      val.value().get<string>());
630d3a379a6SSantosh Puranik                                 }
631d3a379a6SSantosh Puranik                             }
632d3a379a6SSantosh Puranik                             // Use insert_or_assign here as we may already have
633d3a379a6SSantosh Puranik                             // inserted the present property only earlier in
634d3a379a6SSantosh Puranik                             // this function under this same interface.
635d3a379a6SSantosh Puranik                             interfaces.insert_or_assign(eI.key(),
636d3a379a6SSantosh Puranik                                                         move(presProp));
637d3a379a6SSantosh Puranik                         }
6388e140a1cSPriyangaRamasamy                     }
6398e140a1cSPriyangaRamasamy                 }
6408e140a1cSPriyangaRamasamy                 objects.emplace(move(object), move(interfaces));
6418e140a1cSPriyangaRamasamy             }
6428e140a1cSPriyangaRamasamy         }
6438e140a1cSPriyangaRamasamy     }
6448e140a1cSPriyangaRamasamy     return objects;
6458e140a1cSPriyangaRamasamy }
6468e140a1cSPriyangaRamasamy 
64765b83601SAlpana Kumari /**
64865b83601SAlpana Kumari  * @brief This API executes command to set environment variable
64965b83601SAlpana Kumari  *        And then reboot the system
65065b83601SAlpana Kumari  * @param[in] key   -env key to set new value
65165b83601SAlpana Kumari  * @param[in] value -value to set.
65265b83601SAlpana Kumari  */
65365b83601SAlpana Kumari void setEnvAndReboot(const string& key, const string& value)
65465b83601SAlpana Kumari {
65565b83601SAlpana Kumari     // set env and reboot and break.
65665b83601SAlpana Kumari     executeCmd("/sbin/fw_setenv", key, value);
657280197e3SAndrew Geissler     log<level::INFO>("Rebooting BMC to pick up new device tree");
65865b83601SAlpana Kumari     // make dbus call to reboot
65965b83601SAlpana Kumari     auto bus = sdbusplus::bus::new_default_system();
66065b83601SAlpana Kumari     auto method = bus.new_method_call(
66165b83601SAlpana Kumari         "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
66265b83601SAlpana Kumari         "org.freedesktop.systemd1.Manager", "Reboot");
66365b83601SAlpana Kumari     bus.call_noreply(method);
66465b83601SAlpana Kumari }
66565b83601SAlpana Kumari 
66665b83601SAlpana Kumari /*
66765b83601SAlpana Kumari  * @brief This API checks for env var fitconfig.
66865b83601SAlpana Kumari  *        If not initialised OR updated as per the current system type,
66965b83601SAlpana Kumari  *        update this env var and reboot the system.
67065b83601SAlpana Kumari  *
67165b83601SAlpana Kumari  * @param[in] systemType IM kwd in vpd tells about which system type it is.
67265b83601SAlpana Kumari  * */
67365b83601SAlpana Kumari void setDevTreeEnv(const string& systemType)
67465b83601SAlpana Kumari {
67537e72701SAlpana Kumari     // Init with default dtb
67637e72701SAlpana Kumari     string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
67765b83601SAlpana Kumari 
67865b83601SAlpana Kumari     if (deviceTreeSystemTypeMap.find(systemType) !=
67965b83601SAlpana Kumari         deviceTreeSystemTypeMap.end())
68065b83601SAlpana Kumari     {
68165b83601SAlpana Kumari         newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
68265b83601SAlpana Kumari     }
68337e72701SAlpana Kumari     else
68437e72701SAlpana Kumari     {
68537e72701SAlpana Kumari         // System type not supported
68637e72701SAlpana Kumari         cerr << "This System type not found/supported in dtb table "
68737e72701SAlpana Kumari              << systemType << ". so system will be booted with default dtb.\n";
68837e72701SAlpana Kumari     }
68965b83601SAlpana Kumari 
69065b83601SAlpana Kumari     string readVarValue;
69165b83601SAlpana Kumari     bool envVarFound = false;
69265b83601SAlpana Kumari 
69365b83601SAlpana Kumari     vector<string> output = executeCmd("/sbin/fw_printenv");
69465b83601SAlpana Kumari     for (const auto& entry : output)
69565b83601SAlpana Kumari     {
69665b83601SAlpana Kumari         size_t pos = entry.find("=");
69765b83601SAlpana Kumari         string key = entry.substr(0, pos);
69865b83601SAlpana Kumari         if (key != "fitconfig")
69965b83601SAlpana Kumari         {
70065b83601SAlpana Kumari             continue;
70165b83601SAlpana Kumari         }
70265b83601SAlpana Kumari 
70365b83601SAlpana Kumari         envVarFound = true;
70465b83601SAlpana Kumari         if (pos + 1 < entry.size())
70565b83601SAlpana Kumari         {
70665b83601SAlpana Kumari             readVarValue = entry.substr(pos + 1);
70765b83601SAlpana Kumari             if (readVarValue.find(newDeviceTree) != string::npos)
70865b83601SAlpana Kumari             {
70965b83601SAlpana Kumari                 // fitconfig is Updated. No action needed
71065b83601SAlpana Kumari                 break;
71165b83601SAlpana Kumari             }
71265b83601SAlpana Kumari         }
71365b83601SAlpana Kumari         // set env and reboot and break.
71465b83601SAlpana Kumari         setEnvAndReboot(key, newDeviceTree);
71565b83601SAlpana Kumari         exit(0);
71665b83601SAlpana Kumari     }
71765b83601SAlpana Kumari 
71865b83601SAlpana Kumari     // check If env var Not found
71965b83601SAlpana Kumari     if (!envVarFound)
72065b83601SAlpana Kumari     {
72165b83601SAlpana Kumari         setEnvAndReboot("fitconfig", newDeviceTree);
72265b83601SAlpana Kumari     }
72365b83601SAlpana Kumari }
72465b83601SAlpana Kumari 
7258e140a1cSPriyangaRamasamy /**
7269094d4f6SSunnySrivastava1984  * @brief API to call VPD manager to write VPD to EEPROM.
7279094d4f6SSunnySrivastava1984  * @param[in] Object path.
7289094d4f6SSunnySrivastava1984  * @param[in] record to be updated.
7299094d4f6SSunnySrivastava1984  * @param[in] keyword to be updated.
7309094d4f6SSunnySrivastava1984  * @param[in] keyword data to be updated
7319094d4f6SSunnySrivastava1984  */
7329094d4f6SSunnySrivastava1984 void updateHardware(const string& objectName, const string& recName,
7339094d4f6SSunnySrivastava1984                     const string& kwdName, const Binary& data)
7349094d4f6SSunnySrivastava1984 {
7359094d4f6SSunnySrivastava1984     try
7369094d4f6SSunnySrivastava1984     {
7379094d4f6SSunnySrivastava1984         auto bus = sdbusplus::bus::new_default();
7389094d4f6SSunnySrivastava1984         auto properties =
7399094d4f6SSunnySrivastava1984             bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
7409094d4f6SSunnySrivastava1984         properties.append(
7419094d4f6SSunnySrivastava1984             static_cast<sdbusplus::message::object_path>(objectName));
7429094d4f6SSunnySrivastava1984         properties.append(recName);
7439094d4f6SSunnySrivastava1984         properties.append(kwdName);
7449094d4f6SSunnySrivastava1984         properties.append(data);
7459094d4f6SSunnySrivastava1984         bus.call(properties);
7469094d4f6SSunnySrivastava1984     }
7478be4334fSPatrick Williams     catch (const sdbusplus::exception::exception& e)
7489094d4f6SSunnySrivastava1984     {
7499094d4f6SSunnySrivastava1984         std::string what =
7509094d4f6SSunnySrivastava1984             "VPDManager WriteKeyword api failed for inventory path " +
7519094d4f6SSunnySrivastava1984             objectName;
7529094d4f6SSunnySrivastava1984         what += " record " + recName;
7539094d4f6SSunnySrivastava1984         what += " keyword " + kwdName;
7549094d4f6SSunnySrivastava1984         what += " with bus error = " + std::string(e.what());
7559094d4f6SSunnySrivastava1984 
7569094d4f6SSunnySrivastava1984         // map to hold additional data in case of logging pel
7579094d4f6SSunnySrivastava1984         PelAdditionalData additionalData{};
7589094d4f6SSunnySrivastava1984         additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
7599094d4f6SSunnySrivastava1984         additionalData.emplace("DESCRIPTION", what);
7600746eeebSSunny Srivastava         createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
7619094d4f6SSunnySrivastava1984     }
7629094d4f6SSunnySrivastava1984 }
7639094d4f6SSunnySrivastava1984 
7649094d4f6SSunnySrivastava1984 /**
7659094d4f6SSunnySrivastava1984  * @brief API to check if we need to restore system VPD
7669094d4f6SSunnySrivastava1984  * This functionality is only applicable for IPZ VPD data.
7679094d4f6SSunnySrivastava1984  * @param[in] vpdMap - IPZ vpd map
7689094d4f6SSunnySrivastava1984  * @param[in] objectPath - Object path for the FRU
7699094d4f6SSunnySrivastava1984  * @return EEPROMs with records and keywords updated at standby
7709094d4f6SSunnySrivastava1984  */
7719094d4f6SSunnySrivastava1984 std::vector<RestoredEeproms> restoreSystemVPD(Parsed& vpdMap,
7729094d4f6SSunnySrivastava1984                                               const string& objectPath)
7739094d4f6SSunnySrivastava1984 {
7749094d4f6SSunnySrivastava1984     // the list of keywords for VSYS record is as per the S0 system. Should be
7759094d4f6SSunnySrivastava1984     // updated for another type of systems
7769094d4f6SSunnySrivastava1984     static std::unordered_map<std::string, std::vector<std::string>> svpdKwdMap{
7779094d4f6SSunnySrivastava1984         {"VSYS", {"BR", "TM", "SE", "SU", "RB"}},
7789094d4f6SSunnySrivastava1984         {"VCEN", {"FC", "SE"}},
7799094d4f6SSunnySrivastava1984         {"LXR0", {"LX"}}};
7809094d4f6SSunnySrivastava1984 
7819094d4f6SSunnySrivastava1984     // vector to hold all the EEPROMs updated at standby
7829094d4f6SSunnySrivastava1984     std::vector<RestoredEeproms> updatedEeproms = {};
7839094d4f6SSunnySrivastava1984 
7849094d4f6SSunnySrivastava1984     for (const auto& systemRecKwdPair : svpdKwdMap)
7859094d4f6SSunnySrivastava1984     {
7869094d4f6SSunnySrivastava1984         auto it = vpdMap.find(systemRecKwdPair.first);
7879094d4f6SSunnySrivastava1984 
7889094d4f6SSunnySrivastava1984         // check if record is found in map we got by parser
7899094d4f6SSunnySrivastava1984         if (it != vpdMap.end())
7909094d4f6SSunnySrivastava1984         {
7919094d4f6SSunnySrivastava1984             const auto& kwdListForRecord = systemRecKwdPair.second;
7929094d4f6SSunnySrivastava1984             for (const auto& keyword : kwdListForRecord)
7939094d4f6SSunnySrivastava1984             {
7949094d4f6SSunnySrivastava1984                 DbusPropertyMap& kwdValMap = it->second;
7959094d4f6SSunnySrivastava1984                 auto iterator = kwdValMap.find(keyword);
7969094d4f6SSunnySrivastava1984 
7979094d4f6SSunnySrivastava1984                 if (iterator != kwdValMap.end())
7989094d4f6SSunnySrivastava1984                 {
7999094d4f6SSunnySrivastava1984                     string& kwdValue = iterator->second;
8009094d4f6SSunnySrivastava1984 
8019094d4f6SSunnySrivastava1984                     // check bus data
8029094d4f6SSunnySrivastava1984                     const string& recordName = systemRecKwdPair.first;
8039094d4f6SSunnySrivastava1984                     const string& busValue = readBusProperty(
8049094d4f6SSunnySrivastava1984                         objectPath, ipzVpdInf + recordName, keyword);
8059094d4f6SSunnySrivastava1984 
8069094d4f6SSunnySrivastava1984                     if (busValue.find_first_not_of(' ') != string::npos)
8079094d4f6SSunnySrivastava1984                     {
8089094d4f6SSunnySrivastava1984                         if (kwdValue.find_first_not_of(' ') != string::npos)
8099094d4f6SSunnySrivastava1984                         {
8109094d4f6SSunnySrivastava1984                             // both the data are present, check for mismatch
8119094d4f6SSunnySrivastava1984                             if (busValue != kwdValue)
8129094d4f6SSunnySrivastava1984                             {
8139094d4f6SSunnySrivastava1984                                 string errMsg = "VPD data mismatch on cache "
8149094d4f6SSunnySrivastava1984                                                 "and hardware for record: ";
8159094d4f6SSunnySrivastava1984                                 errMsg += (*it).first;
8169094d4f6SSunnySrivastava1984                                 errMsg += " and keyword: ";
8179094d4f6SSunnySrivastava1984                                 errMsg += keyword;
8189094d4f6SSunnySrivastava1984 
8199094d4f6SSunnySrivastava1984                                 // data mismatch
8209094d4f6SSunnySrivastava1984                                 PelAdditionalData additionalData;
8219094d4f6SSunnySrivastava1984                                 additionalData.emplace("CALLOUT_INVENTORY_PATH",
8229094d4f6SSunnySrivastava1984                                                        objectPath);
8239094d4f6SSunnySrivastava1984 
8249094d4f6SSunnySrivastava1984                                 additionalData.emplace("DESCRIPTION", errMsg);
8259094d4f6SSunnySrivastava1984 
8260746eeebSSunny Srivastava                                 createPEL(additionalData, PelSeverity::WARNING,
8270746eeebSSunny Srivastava                                           errIntfForInvalidVPD);
8289094d4f6SSunnySrivastava1984                             }
8299094d4f6SSunnySrivastava1984                         }
8309094d4f6SSunnySrivastava1984                         else
8319094d4f6SSunnySrivastava1984                         {
8329094d4f6SSunnySrivastava1984                             // implies hardware data is blank
8339094d4f6SSunnySrivastava1984                             // update the map
8349094d4f6SSunnySrivastava1984                             Binary busData(busValue.begin(), busValue.end());
8359094d4f6SSunnySrivastava1984 
8369094d4f6SSunnySrivastava1984                             updatedEeproms.push_back(std::make_tuple(
8379094d4f6SSunnySrivastava1984                                 objectPath, recordName, keyword, busData));
8389094d4f6SSunnySrivastava1984 
8399094d4f6SSunnySrivastava1984                             // update the map as well, so that cache data is not
84090a63b9bSSunny Srivastava                             // updated as blank while populating VPD map on Dbus
84190a63b9bSSunny Srivastava                             // in populateDBus Api
8429094d4f6SSunnySrivastava1984                             kwdValue = busValue;
84390a63b9bSSunny Srivastava                         }
8449094d4f6SSunnySrivastava1984                     }
8459094d4f6SSunnySrivastava1984                     else if (kwdValue.find_first_not_of(' ') == string::npos)
8469094d4f6SSunnySrivastava1984                     {
8479094d4f6SSunnySrivastava1984                         string errMsg = "VPD is blank on both cache and "
8489094d4f6SSunnySrivastava1984                                         "hardware for record: ";
8499094d4f6SSunnySrivastava1984                         errMsg += (*it).first;
8509094d4f6SSunnySrivastava1984                         errMsg += " and keyword: ";
8519094d4f6SSunnySrivastava1984                         errMsg += keyword;
8529094d4f6SSunnySrivastava1984                         errMsg += ". SSR need to update hardware VPD.";
8539094d4f6SSunnySrivastava1984 
8549094d4f6SSunnySrivastava1984                         // both the data are blanks, log PEL
8559094d4f6SSunnySrivastava1984                         PelAdditionalData additionalData;
8569094d4f6SSunnySrivastava1984                         additionalData.emplace("CALLOUT_INVENTORY_PATH",
8579094d4f6SSunnySrivastava1984                                                objectPath);
8589094d4f6SSunnySrivastava1984 
8599094d4f6SSunnySrivastava1984                         additionalData.emplace("DESCRIPTION", errMsg);
8609094d4f6SSunnySrivastava1984 
8619094d4f6SSunnySrivastava1984                         // log PEL TODO: Block IPL
8620746eeebSSunny Srivastava                         createPEL(additionalData, PelSeverity::ERROR,
8630746eeebSSunny Srivastava                                   errIntfForBlankSystemVPD);
8649094d4f6SSunnySrivastava1984                         continue;
8659094d4f6SSunnySrivastava1984                     }
8669094d4f6SSunnySrivastava1984                 }
8679094d4f6SSunnySrivastava1984             }
8689094d4f6SSunnySrivastava1984         }
8699094d4f6SSunnySrivastava1984     }
8709094d4f6SSunnySrivastava1984 
8719094d4f6SSunnySrivastava1984     return updatedEeproms;
8729094d4f6SSunnySrivastava1984 }
8739094d4f6SSunnySrivastava1984 
8749094d4f6SSunnySrivastava1984 /**
8757ce68724Salpana07  * @brief This checks for is this FRU a processor
8767ce68724Salpana07  *        And if yes, then checks for is this primary
8777ce68724Salpana07  *
8787ce68724Salpana07  * @param[in] js- vpd json to get the information about this FRU
8797ce68724Salpana07  * @param[in] filePath- FRU vpd
8807ce68724Salpana07  *
8817ce68724Salpana07  * @return true/false
8827ce68724Salpana07  */
8837ce68724Salpana07 bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
8847ce68724Salpana07 {
8857ce68724Salpana07     bool isProcessor = false;
8867ce68724Salpana07     bool isPrimary = false;
8877ce68724Salpana07 
8887ce68724Salpana07     for (const auto& item : js["frus"][filePath])
8897ce68724Salpana07     {
8907ce68724Salpana07         if (item.find("extraInterfaces") != item.end())
8917ce68724Salpana07         {
8927ce68724Salpana07             for (const auto& eI : item["extraInterfaces"].items())
8937ce68724Salpana07             {
8947ce68724Salpana07                 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
8957ce68724Salpana07                 {
8967ce68724Salpana07                     isProcessor = true;
8977ce68724Salpana07                 }
8987ce68724Salpana07             }
8997ce68724Salpana07         }
9007ce68724Salpana07 
9017ce68724Salpana07         if (isProcessor)
9027ce68724Salpana07         {
9037ce68724Salpana07             string cpuType = item.value("cpuType", "");
9047ce68724Salpana07             if (cpuType == "primary")
9057ce68724Salpana07             {
9067ce68724Salpana07                 isPrimary = true;
9077ce68724Salpana07             }
9087ce68724Salpana07         }
9097ce68724Salpana07     }
9107ce68724Salpana07 
9117ce68724Salpana07     return (isProcessor && isPrimary);
9127ce68724Salpana07 }
9137ce68724Salpana07 
9147ce68724Salpana07 /**
9157ce68724Salpana07  * @brief This finds DIMM vpd in vpd json and enables them by binding the device
9167ce68724Salpana07  *        driver
9177ce68724Salpana07  * @param[in] js- vpd json to iterate through and take action if it is DIMM
9187ce68724Salpana07  */
9197ce68724Salpana07 void doEnableAllDimms(nlohmann::json& js)
9207ce68724Salpana07 {
9217ce68724Salpana07     // iterate over each fru
9227ce68724Salpana07     for (const auto& eachFru : js["frus"].items())
9237ce68724Salpana07     {
9247ce68724Salpana07         // skip the driver binding if eeprom already exists
9257ce68724Salpana07         if (fs::exists(eachFru.key()))
9267ce68724Salpana07         {
9277ce68724Salpana07             continue;
9287ce68724Salpana07         }
9297ce68724Salpana07 
9307ce68724Salpana07         for (const auto& eachInventory : eachFru.value())
9317ce68724Salpana07         {
9327ce68724Salpana07             if (eachInventory.find("extraInterfaces") != eachInventory.end())
9337ce68724Salpana07             {
9347ce68724Salpana07                 for (const auto& eI : eachInventory["extraInterfaces"].items())
9357ce68724Salpana07                 {
9367ce68724Salpana07                     if (eI.key().find("Inventory.Item.Dimm") != string::npos)
9377ce68724Salpana07                     {
9387ce68724Salpana07                         string dimmVpd = eachFru.key();
9397ce68724Salpana07                         // fetch it from
9407ce68724Salpana07                         // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
9417ce68724Salpana07 
9427ce68724Salpana07                         regex matchPatern("([0-9]+-[0-9]{4})");
9437ce68724Salpana07                         smatch matchFound;
9447ce68724Salpana07                         if (regex_search(dimmVpd, matchFound, matchPatern))
9457ce68724Salpana07                         {
9467ce68724Salpana07                             vector<string> i2cReg;
9477ce68724Salpana07                             boost::split(i2cReg, matchFound.str(0),
9487ce68724Salpana07                                          boost::is_any_of("-"));
9497ce68724Salpana07 
9507ce68724Salpana07                             // remove 0s from begining
9517ce68724Salpana07                             const regex pattern("^0+(?!$)");
9527ce68724Salpana07                             for (auto& i : i2cReg)
9537ce68724Salpana07                             {
9547ce68724Salpana07                                 i = regex_replace(i, pattern, "");
9557ce68724Salpana07                             }
9567ce68724Salpana07 
9577ce68724Salpana07                             if (i2cReg.size() == 2)
9587ce68724Salpana07                             {
9597ce68724Salpana07                                 // echo 24c32 0x50 >
9607ce68724Salpana07                                 // /sys/bus/i2c/devices/i2c-16/new_device
9617ce68724Salpana07                                 string cmnd = "echo 24c32 0x" + i2cReg[1] +
9627ce68724Salpana07                                               " > /sys/bus/i2c/devices/i2c-" +
9637ce68724Salpana07                                               i2cReg[0] + "/new_device";
9647ce68724Salpana07 
9657ce68724Salpana07                                 executeCmd(cmnd);
9667ce68724Salpana07                             }
9677ce68724Salpana07                         }
9687ce68724Salpana07                     }
9697ce68724Salpana07                 }
9707ce68724Salpana07             }
9717ce68724Salpana07         }
9727ce68724Salpana07     }
9737ce68724Salpana07 }
9747ce68724Salpana07 
9757ce68724Salpana07 /**
9768e140a1cSPriyangaRamasamy  * @brief Populate Dbus.
977abb87edaSPriyangaRamasamy  * This method invokes all the populateInterface functions
978abb87edaSPriyangaRamasamy  * and notifies PIM about dbus object.
9798e140a1cSPriyangaRamasamy  * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
9808e140a1cSPriyangaRamasamy  * input.
981abb87edaSPriyangaRamasamy  * @param[in] js - Inventory json object
982abb87edaSPriyangaRamasamy  * @param[in] filePath - Path of the vpd file
983abb87edaSPriyangaRamasamy  * @param[in] preIntrStr - Interface string
984abb87edaSPriyangaRamasamy  */
985abb87edaSPriyangaRamasamy template <typename T>
9869094d4f6SSunnySrivastava1984 static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
987abb87edaSPriyangaRamasamy {
988abb87edaSPriyangaRamasamy     inventory::InterfaceMap interfaces;
989abb87edaSPriyangaRamasamy     inventory::ObjectMap objects;
990abb87edaSPriyangaRamasamy     inventory::PropertyMap prop;
9916aa5450eSShantappa Teekappanavar     string ccinFromVpd;
992abb87edaSPriyangaRamasamy 
9939094d4f6SSunnySrivastava1984     // map to hold all the keywords whose value has been changed at standby
9949094d4f6SSunnySrivastava1984     vector<RestoredEeproms> updatedEeproms = {};
9959094d4f6SSunnySrivastava1984 
99650f60bf8SSantosh Puranik     bool isSystemVpd = (filePath == systemVpdFilePath);
99750f60bf8SSantosh Puranik     if constexpr (is_same<T, Parsed>::value)
99850f60bf8SSantosh Puranik     {
9996aa5450eSShantappa Teekappanavar         ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
10006aa5450eSShantappa Teekappanavar         transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
10016aa5450eSShantappa Teekappanavar                   ::toupper);
10026aa5450eSShantappa Teekappanavar 
100350f60bf8SSantosh Puranik         if (isSystemVpd)
100450f60bf8SSantosh Puranik         {
100550f60bf8SSantosh Puranik             std::vector<std::string> interfaces = {motherBoardInterface};
100650f60bf8SSantosh Puranik             // call mapper to check for object path creation
100750f60bf8SSantosh Puranik             MapperResponse subTree =
100850f60bf8SSantosh Puranik                 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
100950f60bf8SSantosh Puranik             string mboardPath =
101050f60bf8SSantosh Puranik                 js["frus"][filePath].at(0).value("inventoryPath", "");
101150f60bf8SSantosh Puranik 
101250f60bf8SSantosh Puranik             // Attempt system VPD restore if we have a motherboard
101350f60bf8SSantosh Puranik             // object in the inventory.
101450f60bf8SSantosh Puranik             if ((subTree.size() != 0) &&
101550f60bf8SSantosh Puranik                 (subTree.find(pimPath + mboardPath) != subTree.end()))
101650f60bf8SSantosh Puranik             {
101750f60bf8SSantosh Puranik                 updatedEeproms = restoreSystemVPD(vpdMap, mboardPath);
101850f60bf8SSantosh Puranik             }
101950f60bf8SSantosh Puranik             else
102050f60bf8SSantosh Puranik             {
102150f60bf8SSantosh Puranik                 log<level::ERR>("No object path found");
102250f60bf8SSantosh Puranik             }
102350f60bf8SSantosh Puranik         }
10247ce68724Salpana07         else
10257ce68724Salpana07         {
10267ce68724Salpana07             // check if it is processor vpd.
10277ce68724Salpana07             auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
10287ce68724Salpana07 
10297ce68724Salpana07             if (isPrimaryCpu)
10307ce68724Salpana07             {
10317ce68724Salpana07                 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
10327ce68724Salpana07 
10337ce68724Salpana07                 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
10347ce68724Salpana07 
10357ce68724Salpana07                 if (chipVersion >= 2)
10367ce68724Salpana07                 {
10377ce68724Salpana07                     doEnableAllDimms(js);
10387ce68724Salpana07                 }
10397ce68724Salpana07             }
10407ce68724Salpana07         }
104150f60bf8SSantosh Puranik     }
104250f60bf8SSantosh Puranik 
104332c687f5SPriyanga Ramasamy     if (isSystemVpd)
104432c687f5SPriyanga Ramasamy     {
104532c687f5SPriyanga Ramasamy         string systemJsonName{};
104632c687f5SPriyanga Ramasamy         if constexpr (is_same<T, Parsed>::value)
104732c687f5SPriyanga Ramasamy         {
104832c687f5SPriyanga Ramasamy             // pick the right system json
104932c687f5SPriyanga Ramasamy             systemJsonName = getSystemsJson(vpdMap);
105032c687f5SPriyanga Ramasamy         }
105132c687f5SPriyanga Ramasamy 
105232c687f5SPriyanga Ramasamy         fs::path target = systemJsonName;
105332c687f5SPriyanga Ramasamy         fs::path link = INVENTORY_JSON_SYM_LINK;
105432c687f5SPriyanga Ramasamy 
105532c687f5SPriyanga Ramasamy         // Create the directory for hosting the symlink
105632c687f5SPriyanga Ramasamy         fs::create_directories(VPD_FILES_PATH);
105732c687f5SPriyanga Ramasamy         // unlink the symlink previously created (if any)
105832c687f5SPriyanga Ramasamy         remove(INVENTORY_JSON_SYM_LINK);
105932c687f5SPriyanga Ramasamy         // create a new symlink based on the system
106032c687f5SPriyanga Ramasamy         fs::create_symlink(target, link);
106132c687f5SPriyanga Ramasamy 
106232c687f5SPriyanga Ramasamy         // Reloading the json
106332c687f5SPriyanga Ramasamy         ifstream inventoryJson(link);
106432c687f5SPriyanga Ramasamy         js = json::parse(inventoryJson);
106532c687f5SPriyanga Ramasamy         inventoryJson.close();
106632c687f5SPriyanga Ramasamy     }
106732c687f5SPriyanga Ramasamy 
1068abb87edaSPriyangaRamasamy     for (const auto& item : js["frus"][filePath])
1069abb87edaSPriyangaRamasamy     {
1070abb87edaSPriyangaRamasamy         const auto& objectPath = item["inventoryPath"];
1071abb87edaSPriyangaRamasamy         sdbusplus::message::object_path object(objectPath);
10729094d4f6SSunnySrivastava1984 
10736aa5450eSShantappa Teekappanavar         vector<string> ccinList;
10746aa5450eSShantappa Teekappanavar         if (item.find("ccin") != item.end())
10756aa5450eSShantappa Teekappanavar         {
10766aa5450eSShantappa Teekappanavar             for (const auto& cc : item["ccin"])
10776aa5450eSShantappa Teekappanavar             {
10786aa5450eSShantappa Teekappanavar                 string ccin = cc;
10796aa5450eSShantappa Teekappanavar                 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
10806aa5450eSShantappa Teekappanavar                 ccinList.push_back(ccin);
10816aa5450eSShantappa Teekappanavar             }
10826aa5450eSShantappa Teekappanavar         }
10836aa5450eSShantappa Teekappanavar 
10846aa5450eSShantappa Teekappanavar         if (!ccinFromVpd.empty() && !ccinList.empty() &&
10856aa5450eSShantappa Teekappanavar             (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
10866aa5450eSShantappa Teekappanavar              ccinList.end()))
10876aa5450eSShantappa Teekappanavar         {
10886aa5450eSShantappa Teekappanavar             continue;
10896aa5450eSShantappa Teekappanavar         }
10906aa5450eSShantappa Teekappanavar 
1091d3a379a6SSantosh Puranik         if (isSystemVpd)
1092d3a379a6SSantosh Puranik         {
1093d3a379a6SSantosh Puranik             // Populate one time properties for the system VPD and its sub-frus.
1094d3a379a6SSantosh Puranik             // For the remaining FRUs, this will get handled as a part of
1095d3a379a6SSantosh Puranik             // priming the inventory.
1096d3a379a6SSantosh Puranik             setOneTimeProperties(objectPath, interfaces);
1097d3a379a6SSantosh Puranik         }
1098d3a379a6SSantosh Puranik 
1099abb87edaSPriyangaRamasamy         // Populate the VPD keywords and the common interfaces only if we
1100abb87edaSPriyangaRamasamy         // are asked to inherit that data from the VPD, else only add the
1101abb87edaSPriyangaRamasamy         // extraInterfaces.
1102abb87edaSPriyangaRamasamy         if (item.value("inherit", true))
1103abb87edaSPriyangaRamasamy         {
110458e22145SAlpana Kumari             if constexpr (is_same<T, Parsed>::value)
1105abb87edaSPriyangaRamasamy             {
11068e140a1cSPriyangaRamasamy                 // Each record in the VPD becomes an interface and all
11078e140a1cSPriyangaRamasamy                 // keyword within the record are properties under that
11088e140a1cSPriyangaRamasamy                 // interface.
1109abb87edaSPriyangaRamasamy                 for (const auto& record : vpdMap)
1110abb87edaSPriyangaRamasamy                 {
1111abb87edaSPriyangaRamasamy                     populateFruSpecificInterfaces(
1112e12b181bSSunnySrivastava1984                         record.second, ipzVpdInf + record.first, interfaces);
1113abb87edaSPriyangaRamasamy                 }
1114abb87edaSPriyangaRamasamy             }
111558e22145SAlpana Kumari             else if constexpr (is_same<T, KeywordVpdMap>::value)
1116abb87edaSPriyangaRamasamy             {
1117e12b181bSSunnySrivastava1984                 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
1118abb87edaSPriyangaRamasamy             }
111988edeb6fSSantosh Puranik             if (js.find("commonInterfaces") != js.end())
112088edeb6fSSantosh Puranik             {
112188edeb6fSSantosh Puranik                 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
112288edeb6fSSantosh Puranik                                    isSystemVpd);
112388edeb6fSSantosh Puranik             }
1124abb87edaSPriyangaRamasamy         }
11250859eb65SSantosh Puranik         else
11260859eb65SSantosh Puranik         {
11270859eb65SSantosh Puranik             // Check if we have been asked to inherit specific record(s)
112858e22145SAlpana Kumari             if constexpr (is_same<T, Parsed>::value)
11290859eb65SSantosh Puranik             {
11300859eb65SSantosh Puranik                 if (item.find("copyRecords") != item.end())
11310859eb65SSantosh Puranik                 {
11320859eb65SSantosh Puranik                     for (const auto& record : item["copyRecords"])
11330859eb65SSantosh Puranik                     {
11340859eb65SSantosh Puranik                         const string& recordName = record;
11350859eb65SSantosh Puranik                         if (vpdMap.find(recordName) != vpdMap.end())
11360859eb65SSantosh Puranik                         {
11370859eb65SSantosh Puranik                             populateFruSpecificInterfaces(
1138e12b181bSSunnySrivastava1984                                 vpdMap.at(recordName), ipzVpdInf + recordName,
11390859eb65SSantosh Puranik                                 interfaces);
11400859eb65SSantosh Puranik                         }
11410859eb65SSantosh Puranik                     }
11420859eb65SSantosh Puranik                 }
11430859eb65SSantosh Puranik             }
11440859eb65SSantosh Puranik         }
114558e22145SAlpana Kumari         if (item.value("inheritEI", true))
114658e22145SAlpana Kumari         {
1147abb87edaSPriyangaRamasamy             // Populate interfaces and properties that are common to every FRU
11488e140a1cSPriyangaRamasamy             // and additional interface that might be defined on a per-FRU
11498e140a1cSPriyangaRamasamy             // basis.
1150abb87edaSPriyangaRamasamy             if (item.find("extraInterfaces") != item.end())
1151abb87edaSPriyangaRamasamy             {
115288edeb6fSSantosh Puranik                 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
115388edeb6fSSantosh Puranik                                    isSystemVpd);
1154abb87edaSPriyangaRamasamy             }
115558e22145SAlpana Kumari         }
1156abb87edaSPriyangaRamasamy         objects.emplace(move(object), move(interfaces));
1157abb87edaSPriyangaRamasamy     }
1158abb87edaSPriyangaRamasamy 
11598e140a1cSPriyangaRamasamy     if (isSystemVpd)
11608e140a1cSPriyangaRamasamy     {
11618e140a1cSPriyangaRamasamy         inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
11628e140a1cSPriyangaRamasamy         objects.insert(primeObject.begin(), primeObject.end());
116365b83601SAlpana Kumari 
11649094d4f6SSunnySrivastava1984         // if system VPD has been restored at standby, update the EEPROM
11659094d4f6SSunnySrivastava1984         for (const auto& item : updatedEeproms)
11669094d4f6SSunnySrivastava1984         {
11679094d4f6SSunnySrivastava1984             updateHardware(get<0>(item), get<1>(item), get<2>(item),
11689094d4f6SSunnySrivastava1984                            get<3>(item));
11699094d4f6SSunnySrivastava1984         }
1170f05effdbSAlpana Kumari 
1171f05effdbSAlpana Kumari         // set the U-boot environment variable for device-tree
1172f05effdbSAlpana Kumari         if constexpr (is_same<T, Parsed>::value)
1173f05effdbSAlpana Kumari         {
1174f05effdbSAlpana Kumari             const string imKeyword = getIM(vpdMap);
1175f05effdbSAlpana Kumari             const string hwKeyword = getHW(vpdMap);
1176f05effdbSAlpana Kumari             string systemType = imKeyword;
1177f05effdbSAlpana Kumari 
1178f05effdbSAlpana Kumari             // check If system has constraint then append HW version to it.
1179f05effdbSAlpana Kumari             ifstream sysJson(SYSTEM_JSON);
1180f05effdbSAlpana Kumari             if (!sysJson)
1181f05effdbSAlpana Kumari             {
1182f05effdbSAlpana Kumari                 throw((VpdJsonException("Failed to access Json path",
1183f05effdbSAlpana Kumari                                         SYSTEM_JSON)));
1184f05effdbSAlpana Kumari             }
1185f05effdbSAlpana Kumari 
1186f05effdbSAlpana Kumari             try
1187f05effdbSAlpana Kumari             {
1188f05effdbSAlpana Kumari                 auto systemJson = json::parse(sysJson);
1189f05effdbSAlpana Kumari                 if (systemJson["system"].find(imKeyword) !=
1190f05effdbSAlpana Kumari                     systemJson["system"].end())
1191f05effdbSAlpana Kumari                 {
1192f05effdbSAlpana Kumari                     if (systemJson["system"][imKeyword].find("constraint") !=
1193f05effdbSAlpana Kumari                         systemJson["system"][imKeyword].end())
1194f05effdbSAlpana Kumari                     {
1195f05effdbSAlpana Kumari                         systemType += "_" + hwKeyword;
1196f05effdbSAlpana Kumari                     }
1197f05effdbSAlpana Kumari                 }
1198d278df11SSantosh Puranik             }
11998e15b93aSPatrick Williams             catch (const json::parse_error& ex)
1200d278df11SSantosh Puranik             {
1201d278df11SSantosh Puranik                 throw((VpdJsonException("System Json parsing failed",
1202d278df11SSantosh Puranik                                         SYSTEM_JSON)));
1203d278df11SSantosh Puranik             }
1204f05effdbSAlpana Kumari 
1205f05effdbSAlpana Kumari             setDevTreeEnv(systemType);
1206f05effdbSAlpana Kumari         }
12078e140a1cSPriyangaRamasamy     }
12088e140a1cSPriyangaRamasamy 
1209abb87edaSPriyangaRamasamy     // Notify PIM
12106c71c9dcSSunny Srivastava     common::utility::callPIM(move(objects));
1211abb87edaSPriyangaRamasamy }
1212abb87edaSPriyangaRamasamy 
1213abb87edaSPriyangaRamasamy int main(int argc, char** argv)
1214abb87edaSPriyangaRamasamy {
1215abb87edaSPriyangaRamasamy     int rc = 0;
1216a20be8ecSSunnySrivastava1984     json js{};
1217c2fe40f8SPriyangaRamasamy     Binary vpdVector{};
1218c2fe40f8SPriyangaRamasamy     string file{};
1219a20be8ecSSunnySrivastava1984     // map to hold additional data in case of logging pel
1220a20be8ecSSunnySrivastava1984     PelAdditionalData additionalData{};
1221a20be8ecSSunnySrivastava1984 
1222a20be8ecSSunnySrivastava1984     // this is needed to hold base fru inventory path in case there is ECC or
1223a20be8ecSSunnySrivastava1984     // vpd exception while parsing the file
1224a20be8ecSSunnySrivastava1984     std::string baseFruInventoryPath = {};
1225abb87edaSPriyangaRamasamy 
12260746eeebSSunny Srivastava     // severity for PEL
12270746eeebSSunny Srivastava     PelSeverity pelSeverity = PelSeverity::WARNING;
12280746eeebSSunny Srivastava 
1229abb87edaSPriyangaRamasamy     try
1230abb87edaSPriyangaRamasamy     {
1231abb87edaSPriyangaRamasamy         App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1232abb87edaSPriyangaRamasamy                 "in DBUS"};
1233abb87edaSPriyangaRamasamy 
1234abb87edaSPriyangaRamasamy         app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
12352f793048SAlpana Kumari             ->required();
1236abb87edaSPriyangaRamasamy 
1237abb87edaSPriyangaRamasamy         CLI11_PARSE(app, argc, argv);
1238abb87edaSPriyangaRamasamy 
12390746eeebSSunny Srivastava         // PEL severity should be ERROR in case of any system VPD failure
12400746eeebSSunny Srivastava         if (file == systemVpdFilePath)
12410746eeebSSunny Srivastava         {
12420746eeebSSunny Srivastava             pelSeverity = PelSeverity::ERROR;
12430746eeebSSunny Srivastava         }
12440746eeebSSunny Srivastava 
12450246a4d7SSantosh Puranik         auto jsonToParse = INVENTORY_JSON_DEFAULT;
12460246a4d7SSantosh Puranik 
12470246a4d7SSantosh Puranik         // If the symlink exists, it means it has been setup for us, switch the
12480246a4d7SSantosh Puranik         // path
12490246a4d7SSantosh Puranik         if (fs::exists(INVENTORY_JSON_SYM_LINK))
12500246a4d7SSantosh Puranik         {
12510246a4d7SSantosh Puranik             jsonToParse = INVENTORY_JSON_SYM_LINK;
12520246a4d7SSantosh Puranik         }
12530246a4d7SSantosh Puranik 
1254abb87edaSPriyangaRamasamy         // Make sure that the file path we get is for a supported EEPROM
12550246a4d7SSantosh Puranik         ifstream inventoryJson(jsonToParse);
1256a20be8ecSSunnySrivastava1984         if (!inventoryJson)
1257a20be8ecSSunnySrivastava1984         {
12580746eeebSSunny Srivastava             throw(VpdJsonException("Failed to access Json path", jsonToParse));
1259a20be8ecSSunnySrivastava1984         }
1260a20be8ecSSunnySrivastava1984 
1261a20be8ecSSunnySrivastava1984         try
1262a20be8ecSSunnySrivastava1984         {
1263a20be8ecSSunnySrivastava1984             js = json::parse(inventoryJson);
1264a20be8ecSSunnySrivastava1984         }
12658e15b93aSPatrick Williams         catch (const json::parse_error& ex)
1266a20be8ecSSunnySrivastava1984         {
12670746eeebSSunny Srivastava             throw(VpdJsonException("Json parsing failed", jsonToParse));
1268a20be8ecSSunnySrivastava1984         }
1269abb87edaSPriyangaRamasamy 
127012e24ff3SSantosh Puranik         // Do we have the mandatory "frus" section?
127112e24ff3SSantosh Puranik         if (js.find("frus") == js.end())
127212e24ff3SSantosh Puranik         {
127312e24ff3SSantosh Puranik             throw(VpdJsonException("FRUs section not found in JSON",
127412e24ff3SSantosh Puranik                                    jsonToParse));
127512e24ff3SSantosh Puranik         }
127612e24ff3SSantosh Puranik 
1277647868edSPriyangaRamasamy         // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1278647868edSPriyangaRamasamy         if (file.find("/ahb:apb") != string::npos)
1279647868edSPriyangaRamasamy         {
1280647868edSPriyangaRamasamy             // Translate udev path to a generic /sys/bus/.. file path.
1281647868edSPriyangaRamasamy             udevToGenericPath(file);
128212e24ff3SSantosh Puranik 
128312e24ff3SSantosh Puranik             if ((js["frus"].find(file) != js["frus"].end()) &&
128450f60bf8SSantosh Puranik                 (file == systemVpdFilePath))
1285647868edSPriyangaRamasamy             {
1286647868edSPriyangaRamasamy                 return 0;
1287647868edSPriyangaRamasamy             }
1288647868edSPriyangaRamasamy         }
1289647868edSPriyangaRamasamy 
1290647868edSPriyangaRamasamy         if (file.empty())
1291647868edSPriyangaRamasamy         {
1292647868edSPriyangaRamasamy             cerr << "The EEPROM path <" << file << "> is not valid.";
1293647868edSPriyangaRamasamy             return 0;
1294647868edSPriyangaRamasamy         }
129512e24ff3SSantosh Puranik         if (js["frus"].find(file) == js["frus"].end())
1296abb87edaSPriyangaRamasamy         {
129788edeb6fSSantosh Puranik             return 0;
1298abb87edaSPriyangaRamasamy         }
1299abb87edaSPriyangaRamasamy 
13002f793048SAlpana Kumari         if (!fs::exists(file))
13012f793048SAlpana Kumari         {
13022f793048SAlpana Kumari             cout << "Device path: " << file
13032f793048SAlpana Kumari                  << " does not exist. Spurious udev event? Exiting." << endl;
13042f793048SAlpana Kumari             return 0;
13052f793048SAlpana Kumari         }
13062f793048SAlpana Kumari 
1307a20be8ecSSunnySrivastava1984         baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
13088589375fSSantosh Puranik         // Check if we can read the VPD file based on the power state
130927a5e95aSSantosh Puranik         // We skip reading VPD when the power is ON in two scenarios:
131027a5e95aSSantosh Puranik         // 1) The eeprom we are trying to read is that of the system VPD
131127a5e95aSSantosh Puranik         // 2) The JSON tells us that the FRU EEPROM cannot be read when
131227a5e95aSSantosh Puranik         // we are powered ON.
131327a5e95aSSantosh Puranik         if (js["frus"][file].at(0).value("powerOffOnly", false) ||
131427a5e95aSSantosh Puranik             (file == systemVpdFilePath))
13158589375fSSantosh Puranik         {
13168589375fSSantosh Puranik             if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
13178589375fSSantosh Puranik                 getPowerState())
13188589375fSSantosh Puranik             {
13198589375fSSantosh Puranik                 cout << "This VPD cannot be read when power is ON" << endl;
13208589375fSSantosh Puranik                 return 0;
13218589375fSSantosh Puranik             }
13228589375fSSantosh Puranik         }
1323a20be8ecSSunnySrivastava1984 
13242f793048SAlpana Kumari         try
13252f793048SAlpana Kumari         {
1326c2fe40f8SPriyangaRamasamy             vpdVector = getVpdDataInVector(js, file);
132733c61c2dSPriyangaRamasamy             ParserInterface* parser = ParserFactory::getParser(vpdVector);
13282f793048SAlpana Kumari             variant<KeywordVpdMap, Store> parseResult;
13292f793048SAlpana Kumari             parseResult = parser->parse();
13309a19554cSSunnySrivastava1984 
1331e12b181bSSunnySrivastava1984             if (auto pVal = get_if<Store>(&parseResult))
1332abb87edaSPriyangaRamasamy             {
1333e12b181bSSunnySrivastava1984                 populateDbus(pVal->getVpdMap(), js, file);
1334e12b181bSSunnySrivastava1984             }
1335e12b181bSSunnySrivastava1984             else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1336abb87edaSPriyangaRamasamy             {
1337e12b181bSSunnySrivastava1984                 populateDbus(*pVal, js, file);
1338abb87edaSPriyangaRamasamy             }
1339abb87edaSPriyangaRamasamy 
1340e12b181bSSunnySrivastava1984             // release the parser object
1341e12b181bSSunnySrivastava1984             ParserFactory::freeParser(parser);
1342abb87edaSPriyangaRamasamy         }
13438e15b93aSPatrick Williams         catch (const exception& e)
13442f793048SAlpana Kumari         {
13452f793048SAlpana Kumari             postFailAction(js, file);
1346a504c3eeSPriyangaRamasamy             throw;
13472f793048SAlpana Kumari         }
13482f793048SAlpana Kumari     }
1349a20be8ecSSunnySrivastava1984     catch (const VpdJsonException& ex)
1350a20be8ecSSunnySrivastava1984     {
1351a20be8ecSSunnySrivastava1984         additionalData.emplace("JSON_PATH", ex.getJsonPath());
1352a20be8ecSSunnySrivastava1984         additionalData.emplace("DESCRIPTION", ex.what());
13530746eeebSSunny Srivastava         createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
1354a20be8ecSSunnySrivastava1984 
1355a20be8ecSSunnySrivastava1984         cerr << ex.what() << "\n";
1356a20be8ecSSunnySrivastava1984         rc = -1;
1357a20be8ecSSunnySrivastava1984     }
1358a20be8ecSSunnySrivastava1984     catch (const VpdEccException& ex)
1359a20be8ecSSunnySrivastava1984     {
1360a20be8ecSSunnySrivastava1984         additionalData.emplace("DESCRIPTION", "ECC check failed");
1361a20be8ecSSunnySrivastava1984         additionalData.emplace("CALLOUT_INVENTORY_PATH",
1362a20be8ecSSunnySrivastava1984                                INVENTORY_PATH + baseFruInventoryPath);
13630746eeebSSunny Srivastava         createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
1364c2fe40f8SPriyangaRamasamy         dumpBadVpd(file, vpdVector);
1365a20be8ecSSunnySrivastava1984         cerr << ex.what() << "\n";
1366a20be8ecSSunnySrivastava1984         rc = -1;
1367a20be8ecSSunnySrivastava1984     }
1368a20be8ecSSunnySrivastava1984     catch (const VpdDataException& ex)
1369a20be8ecSSunnySrivastava1984     {
1370a20be8ecSSunnySrivastava1984         additionalData.emplace("DESCRIPTION", "Invalid VPD data");
1371a20be8ecSSunnySrivastava1984         additionalData.emplace("CALLOUT_INVENTORY_PATH",
1372a20be8ecSSunnySrivastava1984                                INVENTORY_PATH + baseFruInventoryPath);
13730746eeebSSunny Srivastava         createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
1374c2fe40f8SPriyangaRamasamy         dumpBadVpd(file, vpdVector);
1375a20be8ecSSunnySrivastava1984         cerr << ex.what() << "\n";
1376a20be8ecSSunnySrivastava1984         rc = -1;
1377a20be8ecSSunnySrivastava1984     }
13788e15b93aSPatrick Williams     catch (const exception& e)
1379abb87edaSPriyangaRamasamy     {
1380c2fe40f8SPriyangaRamasamy         dumpBadVpd(file, vpdVector);
1381abb87edaSPriyangaRamasamy         cerr << e.what() << "\n";
1382abb87edaSPriyangaRamasamy         rc = -1;
1383abb87edaSPriyangaRamasamy     }
1384abb87edaSPriyangaRamasamy 
1385abb87edaSPriyangaRamasamy     return rc;
1386abb87edaSPriyangaRamasamy }