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 
433c24414fSSunny Srivastava // Map to hold record, kwd pair which can be re-stored at standby.
443c24414fSSunny Srivastava // The list of keywords for VSYS record is as per the S0 system. Should
453c24414fSSunny Srivastava // be updated for another type of systems
463c24414fSSunny Srivastava static const std::unordered_map<std::string, std::vector<std::string>>
4701e6c635SSunny Srivastava     svpdKwdMap{{"VSYS", {"BR", "TM", "SE", "SU", "RB", "WN"}},
483c24414fSSunny Srivastava                {"VCEN", {"FC", "SE"}},
493c24414fSSunny Srivastava                {"LXR0", {"LX"}}};
503c24414fSSunny Srivastava 
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             {
2420d61c588SPriyanga Ramasamy                 if (busProp == "LocationCode" && inf == IBM_LOCATION_CODE_INF)
2430d61c588SPriyanga Ramasamy                 {
2440d61c588SPriyanga Ramasamy                     std::string prop;
24558e22145SAlpana Kumari                     if constexpr (is_same<T, Parsed>::value)
24688edeb6fSSantosh Puranik                     {
247414d5aefSAlpana Kumari                         // TODO deprecate the com.ibm interface later
2480d61c588SPriyanga Ramasamy                         prop = expandLocationCode(itr.value().get<string>(),
2490d61c588SPriyanga Ramasamy                                                   vpdMap, isSystemVpd);
2500d61c588SPriyanga Ramasamy                     }
2510d61c588SPriyanga Ramasamy                     else if constexpr (is_same<T, KeywordVpdMap>::value)
2520d61c588SPriyanga Ramasamy                     {
2530d61c588SPriyanga Ramasamy                         // Send empty Parsed object to expandLocationCode api.
2540d61c588SPriyanga Ramasamy                         prop = expandLocationCode(itr.value().get<string>(),
2550d61c588SPriyanga Ramasamy                                                   Parsed{}, false);
2560d61c588SPriyanga Ramasamy                     }
25788edeb6fSSantosh Puranik                     props.emplace(busProp, prop);
258414d5aefSAlpana Kumari                     interfaces.emplace(XYZ_LOCATION_CODE_INF, props);
25969f76024SPriyanga Ramasamy                     interfaces.emplace(IBM_LOCATION_CODE_INF, props);
26088edeb6fSSantosh Puranik                 }
26188edeb6fSSantosh Puranik                 else
26288edeb6fSSantosh Puranik                 {
26388edeb6fSSantosh Puranik                     props.emplace(busProp, itr.value().get<string>());
26488edeb6fSSantosh Puranik                 }
26588edeb6fSSantosh Puranik             }
266ed609affSSantosh Puranik             else if (itr.value().is_array())
267ed609affSSantosh Puranik             {
268ed609affSSantosh Puranik                 try
269ed609affSSantosh Puranik                 {
270ed609affSSantosh Puranik                     props.emplace(busProp, itr.value().get<Binary>());
271ed609affSSantosh Puranik                 }
2728e15b93aSPatrick Williams                 catch (const nlohmann::detail::type_error& e)
273ed609affSSantosh Puranik                 {
274ed609affSSantosh Puranik                     std::cerr << "Type exception: " << e.what() << "\n";
275ed609affSSantosh Puranik                     // Ignore any type errors
276ed609affSSantosh Puranik                 }
277ed609affSSantosh Puranik             }
27831970dedSAlpana Kumari             else if (itr.value().is_object())
27931970dedSAlpana Kumari             {
280abb87edaSPriyangaRamasamy                 const string& rec = itr.value().value("recordName", "");
281abb87edaSPriyangaRamasamy                 const string& kw = itr.value().value("keywordName", "");
282abb87edaSPriyangaRamasamy                 const string& encoding = itr.value().value("encoding", "");
283abb87edaSPriyangaRamasamy 
28458e22145SAlpana Kumari                 if constexpr (is_same<T, Parsed>::value)
285abb87edaSPriyangaRamasamy                 {
28688edeb6fSSantosh Puranik                     if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
28788edeb6fSSantosh Puranik                         vpdMap.at(rec).count(kw))
288abb87edaSPriyangaRamasamy                     {
289abb87edaSPriyangaRamasamy                         auto encoded =
290abb87edaSPriyangaRamasamy                             encodeKeyword(vpdMap.at(rec).at(kw), encoding);
29188edeb6fSSantosh Puranik                         props.emplace(busProp, encoded);
292abb87edaSPriyangaRamasamy                     }
293abb87edaSPriyangaRamasamy                 }
29458e22145SAlpana Kumari                 else if constexpr (is_same<T, KeywordVpdMap>::value)
295abb87edaSPriyangaRamasamy                 {
296abb87edaSPriyangaRamasamy                     if (!kw.empty() && vpdMap.count(kw))
297abb87edaSPriyangaRamasamy                     {
2983ab26a74SAlpana Kumari                         auto kwValue = get_if<Binary>(&vpdMap.at(kw));
2993ab26a74SAlpana Kumari                         auto uintValue = get_if<size_t>(&vpdMap.at(kw));
3003ab26a74SAlpana Kumari 
3013ab26a74SAlpana Kumari                         if (kwValue)
3023ab26a74SAlpana Kumari                         {
303abb87edaSPriyangaRamasamy                             auto prop =
3043ab26a74SAlpana Kumari                                 string((*kwValue).begin(), (*kwValue).end());
3053ab26a74SAlpana Kumari 
306abb87edaSPriyangaRamasamy                             auto encoded = encodeKeyword(prop, encoding);
3073ab26a74SAlpana Kumari 
30888edeb6fSSantosh Puranik                             props.emplace(busProp, encoded);
309abb87edaSPriyangaRamasamy                         }
3103ab26a74SAlpana Kumari                         else if (uintValue)
3113ab26a74SAlpana Kumari                         {
3123ab26a74SAlpana Kumari                             props.emplace(busProp, *uintValue);
3133ab26a74SAlpana Kumari                         }
3143ab26a74SAlpana Kumari                     }
315abb87edaSPriyangaRamasamy                 }
316abb87edaSPriyangaRamasamy             }
317b1e64bb6SMatt Spinler             else if (itr.value().is_number())
318b1e64bb6SMatt Spinler             {
319b1e64bb6SMatt Spinler                 // For now assume the value is a size_t.  In the future it would
320b1e64bb6SMatt Spinler                 // be nice to come up with a way to get the type from the JSON.
321b1e64bb6SMatt Spinler                 props.emplace(busProp, itr.value().get<size_t>());
322b1e64bb6SMatt Spinler             }
32331970dedSAlpana Kumari         }
324*aa8a893eSPriyanga Ramasamy         insertOrMerge(interfaces, inf, move(props));
325abb87edaSPriyangaRamasamy     }
326abb87edaSPriyangaRamasamy }
327abb87edaSPriyangaRamasamy 
32837233785SPriyanga Ramasamy /*API to reset EEPROM pointer to a safe position to avoid VPD corruption.
32937233785SPriyanga Ramasamy  * Currently do reset only for DIMM VPD.*/
33037233785SPriyanga Ramasamy static void resetEEPROMPointer(const nlohmann::json& js, const string& file,
33137233785SPriyanga Ramasamy                                ifstream& vpdFile)
33237233785SPriyanga Ramasamy {
33337233785SPriyanga Ramasamy     for (const auto& item : js["frus"][file])
33437233785SPriyanga Ramasamy     {
33537233785SPriyanga Ramasamy         if (item.find("extraInterfaces") != item.end())
33637233785SPriyanga Ramasamy         {
33737233785SPriyanga Ramasamy             if (item["extraInterfaces"].find(
33837233785SPriyanga Ramasamy                     "xyz.openbmc_project.Inventory.Item.Dimm") !=
33937233785SPriyanga Ramasamy                 item["extraInterfaces"].end())
34037233785SPriyanga Ramasamy             {
34137233785SPriyanga Ramasamy                 // moves the EEPROM pointer to 2048 'th byte.
34237233785SPriyanga Ramasamy                 vpdFile.seekg(2047, std::ios::beg);
34337233785SPriyanga Ramasamy                 // Read that byte and discard - to affirm the move
34437233785SPriyanga Ramasamy                 // operation.
34537233785SPriyanga Ramasamy                 char ch;
34637233785SPriyanga Ramasamy                 vpdFile.read(&ch, sizeof(ch));
34737233785SPriyanga Ramasamy             }
34837233785SPriyanga Ramasamy             return;
34937233785SPriyanga Ramasamy         }
35037233785SPriyanga Ramasamy     }
35137233785SPriyanga Ramasamy }
35237233785SPriyanga Ramasamy 
3535cb3b1f3Salpana07 /**
3545cb3b1f3Salpana07  * @brief This API checks if this FRU is pcie_devices. If yes then it further
3555cb3b1f3Salpana07  *        checks whether it is PASS1 planar.
3565cb3b1f3Salpana07  */
3575cb3b1f3Salpana07 static bool isThisPcieOnPass1planar(const nlohmann::json& js,
3585cb3b1f3Salpana07                                     const string& file)
3595cb3b1f3Salpana07 {
3605cb3b1f3Salpana07     auto isThisPCIeDev = false;
3615cb3b1f3Salpana07     auto isPASS1 = false;
3625cb3b1f3Salpana07 
3635cb3b1f3Salpana07     // Check if it is a PCIE device
3645cb3b1f3Salpana07     if (js["frus"].find(file) != js["frus"].end())
3655cb3b1f3Salpana07     {
3665cb3b1f3Salpana07         if ((js["frus"][file].find("extraInterfaces") !=
3675cb3b1f3Salpana07              js["frus"][file].end()))
3685cb3b1f3Salpana07         {
3695cb3b1f3Salpana07             if (js["frus"][file]["extraInterfaces"].find(
3705cb3b1f3Salpana07                     "xyz.openbmc_project.Inventory.Item.PCIeDevice") !=
3715cb3b1f3Salpana07                 js["frus"][file]["extraInterfaces"].end())
3725cb3b1f3Salpana07             {
3735cb3b1f3Salpana07                 isThisPCIeDev = true;
3745cb3b1f3Salpana07             }
3755cb3b1f3Salpana07         }
3765cb3b1f3Salpana07     }
3775cb3b1f3Salpana07 
3785cb3b1f3Salpana07     if (isThisPCIeDev)
3795cb3b1f3Salpana07     {
3805cb3b1f3Salpana07         // Collect SystemType to know if it is PASS1 planar.
3815cb3b1f3Salpana07         auto bus = sdbusplus::bus::new_default();
3825cb3b1f3Salpana07         auto properties = bus.new_method_call(
3835cb3b1f3Salpana07             INVENTORY_MANAGER_SERVICE,
3845cb3b1f3Salpana07             "/xyz/openbmc_project/inventory/system/chassis/motherboard",
3855cb3b1f3Salpana07             "org.freedesktop.DBus.Properties", "Get");
3865cb3b1f3Salpana07         properties.append("com.ibm.ipzvpd.VINI");
3875cb3b1f3Salpana07         properties.append("HW");
3885cb3b1f3Salpana07         auto result = bus.call(properties);
3895cb3b1f3Salpana07 
3905cb3b1f3Salpana07         inventory::Value val;
3915cb3b1f3Salpana07         result.read(val);
3925cb3b1f3Salpana07         if (auto pVal = get_if<Binary>(&val))
3935cb3b1f3Salpana07         {
3945cb3b1f3Salpana07             auto hwVersion = *pVal;
3955cb3b1f3Salpana07             if (hwVersion[1] < 2)
3965cb3b1f3Salpana07                 isPASS1 = true;
3975cb3b1f3Salpana07         }
3985cb3b1f3Salpana07     }
3995cb3b1f3Salpana07 
4005cb3b1f3Salpana07     return (isThisPCIeDev && isPASS1);
4015cb3b1f3Salpana07 }
4025cb3b1f3Salpana07 
4032f793048SAlpana Kumari static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
40458e22145SAlpana Kumari {
40558e22145SAlpana Kumari     uint32_t offset = 0;
40658e22145SAlpana Kumari     // check if offset present?
40758e22145SAlpana Kumari     for (const auto& item : js["frus"][file])
40858e22145SAlpana Kumari     {
40958e22145SAlpana Kumari         if (item.find("offset") != item.end())
41058e22145SAlpana Kumari         {
41158e22145SAlpana Kumari             offset = item["offset"];
41258e22145SAlpana Kumari         }
41358e22145SAlpana Kumari     }
41458e22145SAlpana Kumari 
41558e22145SAlpana Kumari     // TODO: Figure out a better way to get max possible VPD size.
4163c2a2b9eSPriyanga Ramasamy     auto maxVPDSize = std::min(std::filesystem::file_size(file),
4173c2a2b9eSPriyanga Ramasamy                                static_cast<uintmax_t>(65504));
4183c2a2b9eSPriyanga Ramasamy 
41958e22145SAlpana Kumari     Binary vpdVector;
4203c2a2b9eSPriyanga Ramasamy     vpdVector.resize(maxVPDSize);
42158e22145SAlpana Kumari     ifstream vpdFile;
42258e22145SAlpana Kumari     vpdFile.open(file, ios::binary);
42358e22145SAlpana Kumari 
42458e22145SAlpana Kumari     vpdFile.seekg(offset, ios_base::cur);
4253c2a2b9eSPriyanga Ramasamy     vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), maxVPDSize);
42658e22145SAlpana Kumari     vpdVector.resize(vpdFile.gcount());
42758e22145SAlpana Kumari 
42837233785SPriyanga Ramasamy     resetEEPROMPointer(js, file, vpdFile);
42937233785SPriyanga Ramasamy 
43058e22145SAlpana Kumari     return vpdVector;
43158e22145SAlpana Kumari }
43258e22145SAlpana Kumari 
4332f793048SAlpana Kumari /** This API will be called at the end of VPD collection to perform any post
4342f793048SAlpana Kumari  * actions.
4352f793048SAlpana Kumari  *
4362f793048SAlpana Kumari  * @param[in] json - json object
4372f793048SAlpana Kumari  * @param[in] file - eeprom file path
4382f793048SAlpana Kumari  */
4392f793048SAlpana Kumari static void postFailAction(const nlohmann::json& json, const string& file)
4402f793048SAlpana Kumari {
4412f793048SAlpana Kumari     if ((json["frus"][file].at(0)).find("postActionFail") ==
4422f793048SAlpana Kumari         json["frus"][file].at(0).end())
4432f793048SAlpana Kumari     {
4442f793048SAlpana Kumari         return;
4452f793048SAlpana Kumari     }
4462f793048SAlpana Kumari 
4472f793048SAlpana Kumari     uint8_t pinValue = 0;
4482f793048SAlpana Kumari     string pinName;
4492f793048SAlpana Kumari 
4502f793048SAlpana Kumari     for (const auto& postAction :
4512f793048SAlpana Kumari          (json["frus"][file].at(0))["postActionFail"].items())
4522f793048SAlpana Kumari     {
4532f793048SAlpana Kumari         if (postAction.key() == "pin")
4542f793048SAlpana Kumari         {
4552f793048SAlpana Kumari             pinName = postAction.value();
4562f793048SAlpana Kumari         }
4572f793048SAlpana Kumari         else if (postAction.key() == "value")
4582f793048SAlpana Kumari         {
4592f793048SAlpana Kumari             // Get the value to set
4602f793048SAlpana Kumari             pinValue = postAction.value();
4612f793048SAlpana Kumari         }
4622f793048SAlpana Kumari     }
4632f793048SAlpana Kumari 
4642f793048SAlpana Kumari     cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
4652f793048SAlpana Kumari 
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             {"Disable line", ::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 post-action GPIO" << endl;
4842f793048SAlpana Kumari     }
4852f793048SAlpana Kumari }
4862f793048SAlpana Kumari 
4872e6c6f73SAlpana Kumari /**
4882e6c6f73SAlpana Kumari  * @brief This sets the appropriate value to presence GPIO based on device
4892e6c6f73SAlpana Kumari  * attached or not on expander GPIO, which enables that FRU's VPD collection.
4902f793048SAlpana Kumari  *
4912f793048SAlpana Kumari  * @param[in] json - json object
4922f793048SAlpana Kumari  * @param[in] file - eeprom file path
4932f793048SAlpana Kumari  */
4942f793048SAlpana Kumari static void preAction(const nlohmann::json& json, const string& file)
4952f793048SAlpana Kumari {
4962e6c6f73SAlpana Kumari     if ((json["frus"][file].at(0)).find("presence") !=
4972f793048SAlpana Kumari         json["frus"][file].at(0).end())
4982f793048SAlpana Kumari     {
4992e6c6f73SAlpana Kumari         if (((json["frus"][file].at(0)["presence"]).find("pin") !=
5002e6c6f73SAlpana Kumari              json["frus"][file].at(0)["presence"].end()) &&
5012e6c6f73SAlpana Kumari             ((json["frus"][file].at(0)["presence"]).find("value") !=
5022e6c6f73SAlpana Kumari              json["frus"][file].at(0)["presence"].end()))
5032e6c6f73SAlpana Kumari         {
5042e14789fSSunny Srivastava             string presPinName = json["frus"][file].at(0)["presence"]["pin"];
5052e14789fSSunny Srivastava             Byte presPinValue = json["frus"][file].at(0)["presence"]["value"];
5062e6c6f73SAlpana Kumari 
5072e6c6f73SAlpana Kumari             try
5082e6c6f73SAlpana Kumari             {
5092e6c6f73SAlpana Kumari                 gpiod::line presenceLine = gpiod::find_line(presPinName);
5102e6c6f73SAlpana Kumari 
5112e6c6f73SAlpana Kumari                 if (!presenceLine)
5122e6c6f73SAlpana Kumari                 {
5132e6c6f73SAlpana Kumari                     cerr << "couldn't find presence line:" << presPinName
5142e6c6f73SAlpana Kumari                          << "\n";
5152f793048SAlpana Kumari                     return;
5162f793048SAlpana Kumari                 }
5172f793048SAlpana Kumari 
5182e6c6f73SAlpana Kumari                 presenceLine.request({"Read the presence line",
5192e6c6f73SAlpana Kumari                                       gpiod::line_request::DIRECTION_INPUT, 0});
5202f793048SAlpana Kumari 
5212e6c6f73SAlpana Kumari                 Byte gpioData = presenceLine.get_value();
5222e6c6f73SAlpana Kumari 
5232e6c6f73SAlpana Kumari                 if (gpioData != presPinValue)
5242f793048SAlpana Kumari                 {
5252e6c6f73SAlpana Kumari                     return;
5262f793048SAlpana Kumari                 }
5272e6c6f73SAlpana Kumari             }
5282e6c6f73SAlpana Kumari             catch (system_error&)
5292f793048SAlpana Kumari             {
5302e6c6f73SAlpana Kumari                 cerr << "Failed to get the presence GPIO for - " << presPinName
5312e6c6f73SAlpana Kumari                      << endl;
5322e6c6f73SAlpana Kumari                 return;
5332e6c6f73SAlpana Kumari             }
5342e6c6f73SAlpana Kumari         }
5352e6c6f73SAlpana Kumari     }
5362e6c6f73SAlpana Kumari 
5372e6c6f73SAlpana Kumari     if ((json["frus"][file].at(0)).find("preAction") !=
5382e6c6f73SAlpana Kumari         json["frus"][file].at(0).end())
5392e6c6f73SAlpana Kumari     {
5402e6c6f73SAlpana Kumari         if (((json["frus"][file].at(0)["preAction"]).find("pin") !=
5412e6c6f73SAlpana Kumari              json["frus"][file].at(0)["preAction"].end()) &&
5422e6c6f73SAlpana Kumari             ((json["frus"][file].at(0)["preAction"]).find("value") !=
5432e6c6f73SAlpana Kumari              json["frus"][file].at(0)["preAction"].end()))
5442e6c6f73SAlpana Kumari         {
5452e6c6f73SAlpana Kumari             string pinName = json["frus"][file].at(0)["preAction"]["pin"];
5462f793048SAlpana Kumari             // Get the value to set
5472e6c6f73SAlpana Kumari             Byte pinValue = json["frus"][file].at(0)["preAction"]["value"];
5482f793048SAlpana Kumari 
5492e6c6f73SAlpana Kumari             cout << "Setting GPIO: " << pinName << " to " << (int)pinValue
5502e6c6f73SAlpana Kumari                  << endl;
5512f793048SAlpana Kumari             try
5522f793048SAlpana Kumari             {
5532f793048SAlpana Kumari                 gpiod::line outputLine = gpiod::find_line(pinName);
5542f793048SAlpana Kumari 
5552f793048SAlpana Kumari                 if (!outputLine)
5562f793048SAlpana Kumari                 {
5572f793048SAlpana Kumari                     cout << "Couldn't find output line:" << pinName
5582f793048SAlpana Kumari                          << " on GPIO. Skipping...\n";
5592f793048SAlpana Kumari 
5602f793048SAlpana Kumari                     return;
5612f793048SAlpana Kumari                 }
5622e6c6f73SAlpana Kumari                 outputLine.request({"FRU pre-action",
5632e6c6f73SAlpana Kumari                                     ::gpiod::line_request::DIRECTION_OUTPUT, 0},
5642f793048SAlpana Kumari                                    pinValue);
5652f793048SAlpana Kumari             }
5662e6c6f73SAlpana Kumari             catch (system_error&)
5672f793048SAlpana Kumari             {
5682e6c6f73SAlpana Kumari                 cerr << "Failed to set pre-action for GPIO - " << pinName
5692e6c6f73SAlpana Kumari                      << endl;
5702f793048SAlpana Kumari                 return;
5712f793048SAlpana Kumari             }
5722e6c6f73SAlpana Kumari         }
5732e6c6f73SAlpana Kumari     }
5742f793048SAlpana Kumari 
5752f793048SAlpana Kumari     // Now bind the device
57664b9f5f1SAlpana Kumari     if (json["frus"][file].at(0).find("devAddress") !=
57764b9f5f1SAlpana Kumari         json["frus"][file].at(0).end())
57864b9f5f1SAlpana Kumari     {
5794c3bf5beSAlpana Kumari         string bind = json["frus"][file].at(0).value("devAddress", "");
5802f793048SAlpana Kumari         cout << "Binding device " << bind << endl;
5812f793048SAlpana Kumari         string bindCmd = string("echo \"") + bind +
5822f793048SAlpana Kumari                          string("\" > /sys/bus/i2c/drivers/at24/bind");
5832f793048SAlpana Kumari         cout << bindCmd << endl;
5842f793048SAlpana Kumari         executeCmd(bindCmd);
58564b9f5f1SAlpana Kumari     }
5862f793048SAlpana Kumari 
5872f793048SAlpana Kumari     // Check if device showed up (test for file)
5882f793048SAlpana Kumari     if (!fs::exists(file))
5892f793048SAlpana Kumari     {
5902f793048SAlpana Kumari         cout << "EEPROM " << file << " does not exist. Take failure action"
5912f793048SAlpana Kumari              << endl;
5922f793048SAlpana Kumari         // If not, then take failure postAction
5932f793048SAlpana Kumari         postFailAction(json, file);
5942f793048SAlpana Kumari     }
5952f793048SAlpana Kumari }
5962f793048SAlpana Kumari 
597abb87edaSPriyangaRamasamy /**
598d3a379a6SSantosh Puranik  * @brief Set certain one time properties in the inventory
599d3a379a6SSantosh Puranik  * Use this function to insert the Functional and Enabled properties into the
600d3a379a6SSantosh Puranik  * inventory map. This function first checks if the object in question already
601d3a379a6SSantosh Puranik  * has these properties hosted on D-Bus, if the property is already there, it is
602d3a379a6SSantosh Puranik  * not modified, hence the name "one time". If the property is not already
603d3a379a6SSantosh Puranik  * present, it will be added to the map with a suitable default value (true for
604d3a379a6SSantosh Puranik  * Functional and false for Enabled)
605d3a379a6SSantosh Puranik  *
606d3a379a6SSantosh Puranik  * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
607d3a379a6SSantosh Puranik  * @param[inout] interfaces - Reference to a map of inventory interfaces to
608d3a379a6SSantosh Puranik  * which the properties will be attached.
609d3a379a6SSantosh Puranik  */
610d3a379a6SSantosh Puranik static void setOneTimeProperties(const std::string& object,
611d3a379a6SSantosh Puranik                                  inventory::InterfaceMap& interfaces)
612d3a379a6SSantosh Puranik {
613d3a379a6SSantosh Puranik     auto bus = sdbusplus::bus::new_default();
614d3a379a6SSantosh Puranik     auto objectPath = INVENTORY_PATH + object;
615d3a379a6SSantosh Puranik     auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
616d3a379a6SSantosh Puranik                                     objectPath.c_str(),
617d3a379a6SSantosh Puranik                                     "org.freedesktop.DBus.Properties", "Get");
618d3a379a6SSantosh Puranik     prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
619d3a379a6SSantosh Puranik     prop.append("Functional");
620d3a379a6SSantosh Puranik     try
621d3a379a6SSantosh Puranik     {
622d3a379a6SSantosh Puranik         auto result = bus.call(prop);
623d3a379a6SSantosh Puranik     }
624d3a379a6SSantosh Puranik     catch (const sdbusplus::exception::SdBusError& e)
625d3a379a6SSantosh Puranik     {
626d3a379a6SSantosh Puranik         // Treat as property unavailable
627d3a379a6SSantosh Puranik         inventory::PropertyMap prop;
628d3a379a6SSantosh Puranik         prop.emplace("Functional", true);
629d3a379a6SSantosh Puranik         interfaces.emplace(
630d3a379a6SSantosh Puranik             "xyz.openbmc_project.State.Decorator.OperationalStatus",
631d3a379a6SSantosh Puranik             move(prop));
632d3a379a6SSantosh Puranik     }
633d3a379a6SSantosh Puranik     prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
634d3a379a6SSantosh Puranik                                objectPath.c_str(),
635d3a379a6SSantosh Puranik                                "org.freedesktop.DBus.Properties", "Get");
636d3a379a6SSantosh Puranik     prop.append("xyz.openbmc_project.Object.Enable");
637d3a379a6SSantosh Puranik     prop.append("Enabled");
638d3a379a6SSantosh Puranik     try
639d3a379a6SSantosh Puranik     {
640d3a379a6SSantosh Puranik         auto result = bus.call(prop);
641d3a379a6SSantosh Puranik     }
642d3a379a6SSantosh Puranik     catch (const sdbusplus::exception::SdBusError& e)
643d3a379a6SSantosh Puranik     {
644d3a379a6SSantosh Puranik         // Treat as property unavailable
645d3a379a6SSantosh Puranik         inventory::PropertyMap prop;
646d3a379a6SSantosh Puranik         prop.emplace("Enabled", false);
647d3a379a6SSantosh Puranik         interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
648d3a379a6SSantosh Puranik     }
649d3a379a6SSantosh Puranik }
650d3a379a6SSantosh Puranik 
651d3a379a6SSantosh Puranik /**
6528e140a1cSPriyangaRamasamy  * @brief Prime the Inventory
6538e140a1cSPriyangaRamasamy  * Prime the inventory by populating only the location code,
6548e140a1cSPriyangaRamasamy  * type interface and the inventory object for the frus
6558e140a1cSPriyangaRamasamy  * which are not system vpd fru.
656abb87edaSPriyangaRamasamy  *
6578e140a1cSPriyangaRamasamy  * @param[in] jsObject - Reference to vpd inventory json object
6588e140a1cSPriyangaRamasamy  * @param[in] vpdMap -  Reference to the parsed vpd map
6598e140a1cSPriyangaRamasamy  *
6608e140a1cSPriyangaRamasamy  * @returns Map of items in extraInterface.
6618e140a1cSPriyangaRamasamy  */
6628e140a1cSPriyangaRamasamy template <typename T>
6638e140a1cSPriyangaRamasamy inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
6648e140a1cSPriyangaRamasamy                                     const T& vpdMap)
6658e140a1cSPriyangaRamasamy {
6668e140a1cSPriyangaRamasamy     inventory::ObjectMap objects;
6678e140a1cSPriyangaRamasamy 
6688e140a1cSPriyangaRamasamy     for (auto& itemFRUS : jsObject["frus"].items())
6698e140a1cSPriyangaRamasamy     {
6708e140a1cSPriyangaRamasamy         for (auto& itemEEPROM : itemFRUS.value())
6718e140a1cSPriyangaRamasamy         {
6722e6c6f73SAlpana Kumari             // Take pre actions if needed
6732e6c6f73SAlpana Kumari             if (itemEEPROM.find("preAction") != itemEEPROM.end())
6742e6c6f73SAlpana Kumari             {
6752e6c6f73SAlpana Kumari                 preAction(jsObject, itemFRUS.key());
6762e6c6f73SAlpana Kumari             }
6772e6c6f73SAlpana Kumari 
6788e140a1cSPriyangaRamasamy             inventory::InterfaceMap interfaces;
6798e140a1cSPriyangaRamasamy             inventory::Object object(itemEEPROM.at("inventoryPath"));
6808e140a1cSPriyangaRamasamy 
68150f60bf8SSantosh Puranik             if ((itemFRUS.key() != systemVpdFilePath) &&
68250f60bf8SSantosh Puranik                 !itemEEPROM.value("noprime", false))
6838e140a1cSPriyangaRamasamy             {
684cfd7a75aSAlpana Kumari                 inventory::PropertyMap presProp;
685cfd7a75aSAlpana Kumari                 presProp.emplace("Present", false);
686cfd7a75aSAlpana Kumari                 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
687d3a379a6SSantosh Puranik                                    presProp);
688d3a379a6SSantosh Puranik                 setOneTimeProperties(object, interfaces);
6898e140a1cSPriyangaRamasamy                 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
6908e140a1cSPriyangaRamasamy                 {
6918e140a1cSPriyangaRamasamy                     for (const auto& eI : itemEEPROM["extraInterfaces"].items())
6928e140a1cSPriyangaRamasamy                     {
6938e140a1cSPriyangaRamasamy                         inventory::PropertyMap props;
694414d5aefSAlpana Kumari                         if (eI.key() == IBM_LOCATION_CODE_INF)
6958e140a1cSPriyangaRamasamy                         {
6968e140a1cSPriyangaRamasamy                             if constexpr (std::is_same<T, Parsed>::value)
6978e140a1cSPriyangaRamasamy                             {
6988e140a1cSPriyangaRamasamy                                 for (auto& lC : eI.value().items())
6998e140a1cSPriyangaRamasamy                                 {
7008e140a1cSPriyangaRamasamy                                     auto propVal = expandLocationCode(
7018e140a1cSPriyangaRamasamy                                         lC.value().get<string>(), vpdMap, true);
7028e140a1cSPriyangaRamasamy 
7038e140a1cSPriyangaRamasamy                                     props.emplace(move(lC.key()),
7048e140a1cSPriyangaRamasamy                                                   move(propVal));
705b0f3749dSSantosh Puranik                                     interfaces.emplace(XYZ_LOCATION_CODE_INF,
706b0f3749dSSantosh Puranik                                                        props);
7078e140a1cSPriyangaRamasamy                                     interfaces.emplace(move(eI.key()),
7088e140a1cSPriyangaRamasamy                                                        move(props));
7098e140a1cSPriyangaRamasamy                                 }
7108e140a1cSPriyangaRamasamy                             }
7118e140a1cSPriyangaRamasamy                         }
7128e140a1cSPriyangaRamasamy                         else if (eI.key().find("Inventory.Item.") !=
7138e140a1cSPriyangaRamasamy                                  string::npos)
7148e140a1cSPriyangaRamasamy                         {
7158e140a1cSPriyangaRamasamy                             interfaces.emplace(move(eI.key()), move(props));
7168e140a1cSPriyangaRamasamy                         }
717d3a379a6SSantosh Puranik                         else if (eI.key() ==
718d3a379a6SSantosh Puranik                                  "xyz.openbmc_project.Inventory.Item")
719d3a379a6SSantosh Puranik                         {
720d3a379a6SSantosh Puranik                             for (auto& val : eI.value().items())
721d3a379a6SSantosh Puranik                             {
722d3a379a6SSantosh Puranik                                 if (val.key() == "PrettyName")
723d3a379a6SSantosh Puranik                                 {
724d3a379a6SSantosh Puranik                                     presProp.emplace(val.key(),
725d3a379a6SSantosh Puranik                                                      val.value().get<string>());
726d3a379a6SSantosh Puranik                                 }
727d3a379a6SSantosh Puranik                             }
728d3a379a6SSantosh Puranik                             // Use insert_or_assign here as we may already have
729d3a379a6SSantosh Puranik                             // inserted the present property only earlier in
730d3a379a6SSantosh Puranik                             // this function under this same interface.
731d3a379a6SSantosh Puranik                             interfaces.insert_or_assign(eI.key(),
732d3a379a6SSantosh Puranik                                                         move(presProp));
733d3a379a6SSantosh Puranik                         }
7348e140a1cSPriyangaRamasamy                     }
7358e140a1cSPriyangaRamasamy                 }
7368e140a1cSPriyangaRamasamy                 objects.emplace(move(object), move(interfaces));
7378e140a1cSPriyangaRamasamy             }
7388e140a1cSPriyangaRamasamy         }
7398e140a1cSPriyangaRamasamy     }
7408e140a1cSPriyangaRamasamy     return objects;
7418e140a1cSPriyangaRamasamy }
7428e140a1cSPriyangaRamasamy 
74365b83601SAlpana Kumari /**
74465b83601SAlpana Kumari  * @brief This API executes command to set environment variable
74565b83601SAlpana Kumari  *        And then reboot the system
74665b83601SAlpana Kumari  * @param[in] key   -env key to set new value
74765b83601SAlpana Kumari  * @param[in] value -value to set.
74865b83601SAlpana Kumari  */
74965b83601SAlpana Kumari void setEnvAndReboot(const string& key, const string& value)
75065b83601SAlpana Kumari {
75165b83601SAlpana Kumari     // set env and reboot and break.
75265b83601SAlpana Kumari     executeCmd("/sbin/fw_setenv", key, value);
753280197e3SAndrew Geissler     log<level::INFO>("Rebooting BMC to pick up new device tree");
75465b83601SAlpana Kumari     // make dbus call to reboot
75565b83601SAlpana Kumari     auto bus = sdbusplus::bus::new_default_system();
75665b83601SAlpana Kumari     auto method = bus.new_method_call(
75765b83601SAlpana Kumari         "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
75865b83601SAlpana Kumari         "org.freedesktop.systemd1.Manager", "Reboot");
75965b83601SAlpana Kumari     bus.call_noreply(method);
76065b83601SAlpana Kumari }
76165b83601SAlpana Kumari 
76265b83601SAlpana Kumari /*
76365b83601SAlpana Kumari  * @brief This API checks for env var fitconfig.
76465b83601SAlpana Kumari  *        If not initialised OR updated as per the current system type,
76565b83601SAlpana Kumari  *        update this env var and reboot the system.
76665b83601SAlpana Kumari  *
76765b83601SAlpana Kumari  * @param[in] systemType IM kwd in vpd tells about which system type it is.
76865b83601SAlpana Kumari  * */
76965b83601SAlpana Kumari void setDevTreeEnv(const string& systemType)
77065b83601SAlpana Kumari {
77137e72701SAlpana Kumari     // Init with default dtb
77237e72701SAlpana Kumari     string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
773e5f177a5SSantosh Puranik     static const deviceTreeMap deviceTreeSystemTypeMap = {
774e5f177a5SSantosh Puranik         {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
775e5f177a5SSantosh Puranik         {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
776e5f177a5SSantosh Puranik         {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
777e5f177a5SSantosh Puranik         {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
778e5f177a5SSantosh Puranik         {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
779e5f177a5SSantosh Puranik         {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}};
78065b83601SAlpana Kumari 
78165b83601SAlpana Kumari     if (deviceTreeSystemTypeMap.find(systemType) !=
78265b83601SAlpana Kumari         deviceTreeSystemTypeMap.end())
78365b83601SAlpana Kumari     {
78465b83601SAlpana Kumari         newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
78565b83601SAlpana Kumari     }
78637e72701SAlpana Kumari     else
78737e72701SAlpana Kumari     {
78837e72701SAlpana Kumari         // System type not supported
789ab1e22c3SAlpana Kumari         string err = "This System type not found/supported in dtb table " +
790ab1e22c3SAlpana Kumari                      systemType +
791ab1e22c3SAlpana Kumari                      ".Please check the HW and IM keywords in the system "
792ab1e22c3SAlpana Kumari                      "VPD.Breaking...";
793ab1e22c3SAlpana Kumari 
794ab1e22c3SAlpana Kumari         // map to hold additional data in case of logging pel
795ab1e22c3SAlpana Kumari         PelAdditionalData additionalData{};
796ab1e22c3SAlpana Kumari         additionalData.emplace("DESCRIPTION", err);
797ab1e22c3SAlpana Kumari         createPEL(additionalData, PelSeverity::WARNING,
798ab1e22c3SAlpana Kumari                   errIntfForInvalidSystemType);
799ab1e22c3SAlpana Kumari         exit(-1);
80037e72701SAlpana Kumari     }
80165b83601SAlpana Kumari 
80265b83601SAlpana Kumari     string readVarValue;
80365b83601SAlpana Kumari     bool envVarFound = false;
80465b83601SAlpana Kumari 
80565b83601SAlpana Kumari     vector<string> output = executeCmd("/sbin/fw_printenv");
80665b83601SAlpana Kumari     for (const auto& entry : output)
80765b83601SAlpana Kumari     {
80865b83601SAlpana Kumari         size_t pos = entry.find("=");
80965b83601SAlpana Kumari         string key = entry.substr(0, pos);
81065b83601SAlpana Kumari         if (key != "fitconfig")
81165b83601SAlpana Kumari         {
81265b83601SAlpana Kumari             continue;
81365b83601SAlpana Kumari         }
81465b83601SAlpana Kumari 
81565b83601SAlpana Kumari         envVarFound = true;
81665b83601SAlpana Kumari         if (pos + 1 < entry.size())
81765b83601SAlpana Kumari         {
81865b83601SAlpana Kumari             readVarValue = entry.substr(pos + 1);
81965b83601SAlpana Kumari             if (readVarValue.find(newDeviceTree) != string::npos)
82065b83601SAlpana Kumari             {
82165b83601SAlpana Kumari                 // fitconfig is Updated. No action needed
82265b83601SAlpana Kumari                 break;
82365b83601SAlpana Kumari             }
82465b83601SAlpana Kumari         }
82565b83601SAlpana Kumari         // set env and reboot and break.
82665b83601SAlpana Kumari         setEnvAndReboot(key, newDeviceTree);
82765b83601SAlpana Kumari         exit(0);
82865b83601SAlpana Kumari     }
82965b83601SAlpana Kumari 
83065b83601SAlpana Kumari     // check If env var Not found
83165b83601SAlpana Kumari     if (!envVarFound)
83265b83601SAlpana Kumari     {
83365b83601SAlpana Kumari         setEnvAndReboot("fitconfig", newDeviceTree);
83465b83601SAlpana Kumari     }
83565b83601SAlpana Kumari }
83665b83601SAlpana Kumari 
8378e140a1cSPriyangaRamasamy /**
8389094d4f6SSunnySrivastava1984  * @brief API to call VPD manager to write VPD to EEPROM.
8399094d4f6SSunnySrivastava1984  * @param[in] Object path.
8409094d4f6SSunnySrivastava1984  * @param[in] record to be updated.
8419094d4f6SSunnySrivastava1984  * @param[in] keyword to be updated.
8429094d4f6SSunnySrivastava1984  * @param[in] keyword data to be updated
8439094d4f6SSunnySrivastava1984  */
8449094d4f6SSunnySrivastava1984 void updateHardware(const string& objectName, const string& recName,
8459094d4f6SSunnySrivastava1984                     const string& kwdName, const Binary& data)
8469094d4f6SSunnySrivastava1984 {
8479094d4f6SSunnySrivastava1984     try
8489094d4f6SSunnySrivastava1984     {
8499094d4f6SSunnySrivastava1984         auto bus = sdbusplus::bus::new_default();
8509094d4f6SSunnySrivastava1984         auto properties =
8519094d4f6SSunnySrivastava1984             bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
8529094d4f6SSunnySrivastava1984         properties.append(
8539094d4f6SSunnySrivastava1984             static_cast<sdbusplus::message::object_path>(objectName));
8549094d4f6SSunnySrivastava1984         properties.append(recName);
8559094d4f6SSunnySrivastava1984         properties.append(kwdName);
8569094d4f6SSunnySrivastava1984         properties.append(data);
8579094d4f6SSunnySrivastava1984         bus.call(properties);
8589094d4f6SSunnySrivastava1984     }
8598be4334fSPatrick Williams     catch (const sdbusplus::exception::exception& e)
8609094d4f6SSunnySrivastava1984     {
8619094d4f6SSunnySrivastava1984         std::string what =
8629094d4f6SSunnySrivastava1984             "VPDManager WriteKeyword api failed for inventory path " +
8639094d4f6SSunnySrivastava1984             objectName;
8649094d4f6SSunnySrivastava1984         what += " record " + recName;
8659094d4f6SSunnySrivastava1984         what += " keyword " + kwdName;
8669094d4f6SSunnySrivastava1984         what += " with bus error = " + std::string(e.what());
8679094d4f6SSunnySrivastava1984 
8689094d4f6SSunnySrivastava1984         // map to hold additional data in case of logging pel
8699094d4f6SSunnySrivastava1984         PelAdditionalData additionalData{};
8709094d4f6SSunnySrivastava1984         additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
8719094d4f6SSunnySrivastava1984         additionalData.emplace("DESCRIPTION", what);
8720746eeebSSunny Srivastava         createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
8739094d4f6SSunnySrivastava1984     }
8749094d4f6SSunnySrivastava1984 }
8759094d4f6SSunnySrivastava1984 
8769094d4f6SSunnySrivastava1984 /**
8773c24414fSSunny Srivastava  * @brief An api to get list of blank system VPD properties.
8783c24414fSSunny Srivastava  * @param[in] vpdMap - IPZ vpd map.
8793c24414fSSunny Srivastava  * @param[in] objectPath - Object path for the FRU.
8803c24414fSSunny Srivastava  * @param[out] blankPropertyList - Properties which are blank in System VPD and
8813c24414fSSunny Srivastava  * needs to be updated as standby.
8823c24414fSSunny Srivastava  */
8833c24414fSSunny Srivastava void getListOfBlankSystemVpd(Parsed& vpdMap, const string& objectPath,
8843c24414fSSunny Srivastava                              std::vector<RestoredEeproms>& blankPropertyList)
8853c24414fSSunny Srivastava {
8863c24414fSSunny Srivastava     for (const auto& systemRecKwdPair : svpdKwdMap)
8873c24414fSSunny Srivastava     {
8883c24414fSSunny Srivastava         auto it = vpdMap.find(systemRecKwdPair.first);
8893c24414fSSunny Srivastava 
8903c24414fSSunny Srivastava         // check if record is found in map we got by parser
8913c24414fSSunny Srivastava         if (it != vpdMap.end())
8923c24414fSSunny Srivastava         {
8933c24414fSSunny Srivastava             const auto& kwdListForRecord = systemRecKwdPair.second;
8943c24414fSSunny Srivastava             for (const auto& keyword : kwdListForRecord)
8953c24414fSSunny Srivastava             {
8963c24414fSSunny Srivastava                 DbusPropertyMap& kwdValMap = it->second;
8973c24414fSSunny Srivastava                 auto iterator = kwdValMap.find(keyword);
8983c24414fSSunny Srivastava 
8993c24414fSSunny Srivastava                 if (iterator != kwdValMap.end())
9003c24414fSSunny Srivastava                 {
9013c24414fSSunny Srivastava                     string& kwdValue = iterator->second;
9023c24414fSSunny Srivastava 
9033c24414fSSunny Srivastava                     // check bus data
9043c24414fSSunny Srivastava                     const string& recordName = systemRecKwdPair.first;
9053c24414fSSunny Srivastava                     const string& busValue = readBusProperty(
9063c24414fSSunny Srivastava                         objectPath, ipzVpdInf + recordName, keyword);
9073c24414fSSunny Srivastava 
9083c24414fSSunny Srivastava                     if (busValue.find_first_not_of(' ') != string::npos)
9093c24414fSSunny Srivastava                     {
9103c24414fSSunny Srivastava                         if (kwdValue.find_first_not_of(' ') == string::npos)
9113c24414fSSunny Srivastava                         {
9123c24414fSSunny Srivastava                             // implies data is blank on EEPROM but not on cache.
9133c24414fSSunny Srivastava                             // So EEPROM vpd update is required.
9143c24414fSSunny Srivastava                             Binary busData(busValue.begin(), busValue.end());
9153c24414fSSunny Srivastava 
9163c24414fSSunny Srivastava                             blankPropertyList.push_back(std::make_tuple(
9173c24414fSSunny Srivastava                                 objectPath, recordName, keyword, busData));
9183c24414fSSunny Srivastava                         }
9193c24414fSSunny Srivastava                     }
9203c24414fSSunny Srivastava                 }
9213c24414fSSunny Srivastava             }
9223c24414fSSunny Srivastava         }
9233c24414fSSunny Srivastava     }
9243c24414fSSunny Srivastava }
9253c24414fSSunny Srivastava 
9263c24414fSSunny Srivastava /**
9279094d4f6SSunnySrivastava1984  * @brief API to check if we need to restore system VPD
9289094d4f6SSunnySrivastava1984  * This functionality is only applicable for IPZ VPD data.
9299094d4f6SSunnySrivastava1984  * @param[in] vpdMap - IPZ vpd map
9309094d4f6SSunnySrivastava1984  * @param[in] objectPath - Object path for the FRU
9319094d4f6SSunnySrivastava1984  */
9323c24414fSSunny Srivastava void restoreSystemVPD(Parsed& vpdMap, const string& objectPath)
9339094d4f6SSunnySrivastava1984 {
9349094d4f6SSunnySrivastava1984     for (const auto& systemRecKwdPair : svpdKwdMap)
9359094d4f6SSunnySrivastava1984     {
9369094d4f6SSunnySrivastava1984         auto it = vpdMap.find(systemRecKwdPair.first);
9379094d4f6SSunnySrivastava1984 
9389094d4f6SSunnySrivastava1984         // check if record is found in map we got by parser
9399094d4f6SSunnySrivastava1984         if (it != vpdMap.end())
9409094d4f6SSunnySrivastava1984         {
9419094d4f6SSunnySrivastava1984             const auto& kwdListForRecord = systemRecKwdPair.second;
9429094d4f6SSunnySrivastava1984             for (const auto& keyword : kwdListForRecord)
9439094d4f6SSunnySrivastava1984             {
9449094d4f6SSunnySrivastava1984                 DbusPropertyMap& kwdValMap = it->second;
9459094d4f6SSunnySrivastava1984                 auto iterator = kwdValMap.find(keyword);
9469094d4f6SSunnySrivastava1984 
9479094d4f6SSunnySrivastava1984                 if (iterator != kwdValMap.end())
9489094d4f6SSunnySrivastava1984                 {
9499094d4f6SSunnySrivastava1984                     string& kwdValue = iterator->second;
9509094d4f6SSunnySrivastava1984 
9519094d4f6SSunnySrivastava1984                     // check bus data
9529094d4f6SSunnySrivastava1984                     const string& recordName = systemRecKwdPair.first;
9539094d4f6SSunnySrivastava1984                     const string& busValue = readBusProperty(
9549094d4f6SSunnySrivastava1984                         objectPath, ipzVpdInf + recordName, keyword);
9559094d4f6SSunnySrivastava1984 
9569094d4f6SSunnySrivastava1984                     if (busValue.find_first_not_of(' ') != string::npos)
9579094d4f6SSunnySrivastava1984                     {
9589094d4f6SSunnySrivastava1984                         if (kwdValue.find_first_not_of(' ') != string::npos)
9599094d4f6SSunnySrivastava1984                         {
9609094d4f6SSunnySrivastava1984                             // both the data are present, check for mismatch
9619094d4f6SSunnySrivastava1984                             if (busValue != kwdValue)
9629094d4f6SSunnySrivastava1984                             {
9639094d4f6SSunnySrivastava1984                                 string errMsg = "VPD data mismatch on cache "
9649094d4f6SSunnySrivastava1984                                                 "and hardware for record: ";
9659094d4f6SSunnySrivastava1984                                 errMsg += (*it).first;
9669094d4f6SSunnySrivastava1984                                 errMsg += " and keyword: ";
9679094d4f6SSunnySrivastava1984                                 errMsg += keyword;
9689094d4f6SSunnySrivastava1984 
9699094d4f6SSunnySrivastava1984                                 // data mismatch
9709094d4f6SSunnySrivastava1984                                 PelAdditionalData additionalData;
9719094d4f6SSunnySrivastava1984                                 additionalData.emplace("CALLOUT_INVENTORY_PATH",
9729094d4f6SSunnySrivastava1984                                                        objectPath);
9739094d4f6SSunnySrivastava1984 
9749094d4f6SSunnySrivastava1984                                 additionalData.emplace("DESCRIPTION", errMsg);
9759094d4f6SSunnySrivastava1984 
9760746eeebSSunny Srivastava                                 createPEL(additionalData, PelSeverity::WARNING,
9770746eeebSSunny Srivastava                                           errIntfForInvalidVPD);
9789094d4f6SSunnySrivastava1984                             }
9799094d4f6SSunnySrivastava1984                         }
9809094d4f6SSunnySrivastava1984                         else
9819094d4f6SSunnySrivastava1984                         {
9829094d4f6SSunnySrivastava1984                             // implies hardware data is blank
9839094d4f6SSunnySrivastava1984                             // update the map
9849094d4f6SSunnySrivastava1984                             Binary busData(busValue.begin(), busValue.end());
9859094d4f6SSunnySrivastava1984 
9869094d4f6SSunnySrivastava1984                             // update the map as well, so that cache data is not
98790a63b9bSSunny Srivastava                             // updated as blank while populating VPD map on Dbus
98890a63b9bSSunny Srivastava                             // in populateDBus Api
9899094d4f6SSunnySrivastava1984                             kwdValue = busValue;
99090a63b9bSSunny Srivastava                         }
9919094d4f6SSunnySrivastava1984                     }
9929094d4f6SSunnySrivastava1984                     else if (kwdValue.find_first_not_of(' ') == string::npos)
9939094d4f6SSunnySrivastava1984                     {
9949094d4f6SSunnySrivastava1984                         string errMsg = "VPD is blank on both cache and "
9959094d4f6SSunnySrivastava1984                                         "hardware for record: ";
9969094d4f6SSunnySrivastava1984                         errMsg += (*it).first;
9979094d4f6SSunnySrivastava1984                         errMsg += " and keyword: ";
9989094d4f6SSunnySrivastava1984                         errMsg += keyword;
9999094d4f6SSunnySrivastava1984                         errMsg += ". SSR need to update hardware VPD.";
10009094d4f6SSunnySrivastava1984 
10019094d4f6SSunnySrivastava1984                         // both the data are blanks, log PEL
10029094d4f6SSunnySrivastava1984                         PelAdditionalData additionalData;
10039094d4f6SSunnySrivastava1984                         additionalData.emplace("CALLOUT_INVENTORY_PATH",
10049094d4f6SSunnySrivastava1984                                                objectPath);
10059094d4f6SSunnySrivastava1984 
10069094d4f6SSunnySrivastava1984                         additionalData.emplace("DESCRIPTION", errMsg);
10079094d4f6SSunnySrivastava1984 
10089094d4f6SSunnySrivastava1984                         // log PEL TODO: Block IPL
10090746eeebSSunny Srivastava                         createPEL(additionalData, PelSeverity::ERROR,
10100746eeebSSunny Srivastava                                   errIntfForBlankSystemVPD);
10119094d4f6SSunnySrivastava1984                         continue;
10129094d4f6SSunnySrivastava1984                     }
10139094d4f6SSunnySrivastava1984                 }
10149094d4f6SSunnySrivastava1984             }
10159094d4f6SSunnySrivastava1984         }
10169094d4f6SSunnySrivastava1984     }
10179094d4f6SSunnySrivastava1984 }
10189094d4f6SSunnySrivastava1984 
10199094d4f6SSunnySrivastava1984 /**
10207ce68724Salpana07  * @brief This checks for is this FRU a processor
10217ce68724Salpana07  *        And if yes, then checks for is this primary
10227ce68724Salpana07  *
10237ce68724Salpana07  * @param[in] js- vpd json to get the information about this FRU
10247ce68724Salpana07  * @param[in] filePath- FRU vpd
10257ce68724Salpana07  *
10267ce68724Salpana07  * @return true/false
10277ce68724Salpana07  */
10287ce68724Salpana07 bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
10297ce68724Salpana07 {
10307ce68724Salpana07     bool isProcessor = false;
10317ce68724Salpana07     bool isPrimary = false;
10327ce68724Salpana07 
10337ce68724Salpana07     for (const auto& item : js["frus"][filePath])
10347ce68724Salpana07     {
10357ce68724Salpana07         if (item.find("extraInterfaces") != item.end())
10367ce68724Salpana07         {
10377ce68724Salpana07             for (const auto& eI : item["extraInterfaces"].items())
10387ce68724Salpana07             {
10397ce68724Salpana07                 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
10407ce68724Salpana07                 {
10417ce68724Salpana07                     isProcessor = true;
10427ce68724Salpana07                 }
10437ce68724Salpana07             }
10447ce68724Salpana07         }
10457ce68724Salpana07 
10467ce68724Salpana07         if (isProcessor)
10477ce68724Salpana07         {
10487ce68724Salpana07             string cpuType = item.value("cpuType", "");
10497ce68724Salpana07             if (cpuType == "primary")
10507ce68724Salpana07             {
10517ce68724Salpana07                 isPrimary = true;
10527ce68724Salpana07             }
10537ce68724Salpana07         }
10547ce68724Salpana07     }
10557ce68724Salpana07 
10567ce68724Salpana07     return (isProcessor && isPrimary);
10577ce68724Salpana07 }
10587ce68724Salpana07 
10597ce68724Salpana07 /**
10607ce68724Salpana07  * @brief This finds DIMM vpd in vpd json and enables them by binding the device
10617ce68724Salpana07  *        driver
10627ce68724Salpana07  * @param[in] js- vpd json to iterate through and take action if it is DIMM
10637ce68724Salpana07  */
10647ce68724Salpana07 void doEnableAllDimms(nlohmann::json& js)
10657ce68724Salpana07 {
10667ce68724Salpana07     // iterate over each fru
10677ce68724Salpana07     for (const auto& eachFru : js["frus"].items())
10687ce68724Salpana07     {
10697ce68724Salpana07         // skip the driver binding if eeprom already exists
10707ce68724Salpana07         if (fs::exists(eachFru.key()))
10717ce68724Salpana07         {
10727ce68724Salpana07             continue;
10737ce68724Salpana07         }
10747ce68724Salpana07 
10757ce68724Salpana07         for (const auto& eachInventory : eachFru.value())
10767ce68724Salpana07         {
10777ce68724Salpana07             if (eachInventory.find("extraInterfaces") != eachInventory.end())
10787ce68724Salpana07             {
10797ce68724Salpana07                 for (const auto& eI : eachInventory["extraInterfaces"].items())
10807ce68724Salpana07                 {
10817ce68724Salpana07                     if (eI.key().find("Inventory.Item.Dimm") != string::npos)
10827ce68724Salpana07                     {
10837ce68724Salpana07                         string dimmVpd = eachFru.key();
10847ce68724Salpana07                         // fetch it from
10857ce68724Salpana07                         // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
10867ce68724Salpana07 
10877ce68724Salpana07                         regex matchPatern("([0-9]+-[0-9]{4})");
10887ce68724Salpana07                         smatch matchFound;
10897ce68724Salpana07                         if (regex_search(dimmVpd, matchFound, matchPatern))
10907ce68724Salpana07                         {
10917ce68724Salpana07                             vector<string> i2cReg;
10927ce68724Salpana07                             boost::split(i2cReg, matchFound.str(0),
10937ce68724Salpana07                                          boost::is_any_of("-"));
10947ce68724Salpana07 
10957ce68724Salpana07                             // remove 0s from begining
10967ce68724Salpana07                             const regex pattern("^0+(?!$)");
10977ce68724Salpana07                             for (auto& i : i2cReg)
10987ce68724Salpana07                             {
10997ce68724Salpana07                                 i = regex_replace(i, pattern, "");
11007ce68724Salpana07                             }
11017ce68724Salpana07 
11027ce68724Salpana07                             if (i2cReg.size() == 2)
11037ce68724Salpana07                             {
11047ce68724Salpana07                                 // echo 24c32 0x50 >
11057ce68724Salpana07                                 // /sys/bus/i2c/devices/i2c-16/new_device
11067ce68724Salpana07                                 string cmnd = "echo 24c32 0x" + i2cReg[1] +
11077ce68724Salpana07                                               " > /sys/bus/i2c/devices/i2c-" +
11087ce68724Salpana07                                               i2cReg[0] + "/new_device";
11097ce68724Salpana07 
11107ce68724Salpana07                                 executeCmd(cmnd);
11117ce68724Salpana07                             }
11127ce68724Salpana07                         }
11137ce68724Salpana07                     }
11147ce68724Salpana07                 }
11157ce68724Salpana07             }
11167ce68724Salpana07         }
11177ce68724Salpana07     }
11187ce68724Salpana07 }
11197ce68724Salpana07 
11207ce68724Salpana07 /**
11218e140a1cSPriyangaRamasamy  * @brief Populate Dbus.
1122abb87edaSPriyangaRamasamy  * This method invokes all the populateInterface functions
1123abb87edaSPriyangaRamasamy  * and notifies PIM about dbus object.
11248e140a1cSPriyangaRamasamy  * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
11258e140a1cSPriyangaRamasamy  * input.
1126abb87edaSPriyangaRamasamy  * @param[in] js - Inventory json object
1127abb87edaSPriyangaRamasamy  * @param[in] filePath - Path of the vpd file
1128abb87edaSPriyangaRamasamy  * @param[in] preIntrStr - Interface string
1129abb87edaSPriyangaRamasamy  */
1130abb87edaSPriyangaRamasamy template <typename T>
11319094d4f6SSunnySrivastava1984 static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
1132abb87edaSPriyangaRamasamy {
1133abb87edaSPriyangaRamasamy     inventory::InterfaceMap interfaces;
1134abb87edaSPriyangaRamasamy     inventory::ObjectMap objects;
1135abb87edaSPriyangaRamasamy     inventory::PropertyMap prop;
11366aa5450eSShantappa Teekappanavar     string ccinFromVpd;
1137abb87edaSPriyangaRamasamy 
113850f60bf8SSantosh Puranik     bool isSystemVpd = (filePath == systemVpdFilePath);
113950f60bf8SSantosh Puranik     if constexpr (is_same<T, Parsed>::value)
114050f60bf8SSantosh Puranik     {
11416aa5450eSShantappa Teekappanavar         ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
11426aa5450eSShantappa Teekappanavar         transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
11436aa5450eSShantappa Teekappanavar                   ::toupper);
11446aa5450eSShantappa Teekappanavar 
114550f60bf8SSantosh Puranik         if (isSystemVpd)
114650f60bf8SSantosh Puranik         {
114750f60bf8SSantosh Puranik             std::vector<std::string> interfaces = {motherBoardInterface};
114850f60bf8SSantosh Puranik             // call mapper to check for object path creation
114950f60bf8SSantosh Puranik             MapperResponse subTree =
115050f60bf8SSantosh Puranik                 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
115150f60bf8SSantosh Puranik             string mboardPath =
115250f60bf8SSantosh Puranik                 js["frus"][filePath].at(0).value("inventoryPath", "");
115350f60bf8SSantosh Puranik 
115450f60bf8SSantosh Puranik             // Attempt system VPD restore if we have a motherboard
115550f60bf8SSantosh Puranik             // object in the inventory.
115650f60bf8SSantosh Puranik             if ((subTree.size() != 0) &&
115750f60bf8SSantosh Puranik                 (subTree.find(pimPath + mboardPath) != subTree.end()))
115850f60bf8SSantosh Puranik             {
11593c24414fSSunny Srivastava                 restoreSystemVPD(vpdMap, mboardPath);
116050f60bf8SSantosh Puranik             }
116150f60bf8SSantosh Puranik             else
116250f60bf8SSantosh Puranik             {
116350f60bf8SSantosh Puranik                 log<level::ERR>("No object path found");
116450f60bf8SSantosh Puranik             }
116550f60bf8SSantosh Puranik         }
11667ce68724Salpana07         else
11677ce68724Salpana07         {
11687ce68724Salpana07             // check if it is processor vpd.
11697ce68724Salpana07             auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
11707ce68724Salpana07 
11717ce68724Salpana07             if (isPrimaryCpu)
11727ce68724Salpana07             {
11737ce68724Salpana07                 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
11747ce68724Salpana07 
11757ce68724Salpana07                 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
11767ce68724Salpana07 
11777ce68724Salpana07                 if (chipVersion >= 2)
11787ce68724Salpana07                 {
11797ce68724Salpana07                     doEnableAllDimms(js);
11807ce68724Salpana07                 }
11817ce68724Salpana07             }
11827ce68724Salpana07         }
118350f60bf8SSantosh Puranik     }
118450f60bf8SSantosh Puranik 
118532c687f5SPriyanga Ramasamy     if (isSystemVpd)
118632c687f5SPriyanga Ramasamy     {
118732c687f5SPriyanga Ramasamy         string systemJsonName{};
118832c687f5SPriyanga Ramasamy         if constexpr (is_same<T, Parsed>::value)
118932c687f5SPriyanga Ramasamy         {
119032c687f5SPriyanga Ramasamy             // pick the right system json
119132c687f5SPriyanga Ramasamy             systemJsonName = getSystemsJson(vpdMap);
119232c687f5SPriyanga Ramasamy         }
119332c687f5SPriyanga Ramasamy 
119432c687f5SPriyanga Ramasamy         fs::path target = systemJsonName;
119532c687f5SPriyanga Ramasamy         fs::path link = INVENTORY_JSON_SYM_LINK;
119632c687f5SPriyanga Ramasamy 
119732c687f5SPriyanga Ramasamy         // Create the directory for hosting the symlink
119832c687f5SPriyanga Ramasamy         fs::create_directories(VPD_FILES_PATH);
119932c687f5SPriyanga Ramasamy         // unlink the symlink previously created (if any)
120032c687f5SPriyanga Ramasamy         remove(INVENTORY_JSON_SYM_LINK);
120132c687f5SPriyanga Ramasamy         // create a new symlink based on the system
120232c687f5SPriyanga Ramasamy         fs::create_symlink(target, link);
120332c687f5SPriyanga Ramasamy 
120432c687f5SPriyanga Ramasamy         // Reloading the json
120532c687f5SPriyanga Ramasamy         ifstream inventoryJson(link);
120632c687f5SPriyanga Ramasamy         js = json::parse(inventoryJson);
120732c687f5SPriyanga Ramasamy         inventoryJson.close();
120832c687f5SPriyanga Ramasamy     }
120932c687f5SPriyanga Ramasamy 
1210abb87edaSPriyangaRamasamy     for (const auto& item : js["frus"][filePath])
1211abb87edaSPriyangaRamasamy     {
1212abb87edaSPriyangaRamasamy         const auto& objectPath = item["inventoryPath"];
1213abb87edaSPriyangaRamasamy         sdbusplus::message::object_path object(objectPath);
12149094d4f6SSunnySrivastava1984 
12156aa5450eSShantappa Teekappanavar         vector<string> ccinList;
12166aa5450eSShantappa Teekappanavar         if (item.find("ccin") != item.end())
12176aa5450eSShantappa Teekappanavar         {
12186aa5450eSShantappa Teekappanavar             for (const auto& cc : item["ccin"])
12196aa5450eSShantappa Teekappanavar             {
12206aa5450eSShantappa Teekappanavar                 string ccin = cc;
12216aa5450eSShantappa Teekappanavar                 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
12226aa5450eSShantappa Teekappanavar                 ccinList.push_back(ccin);
12236aa5450eSShantappa Teekappanavar             }
12246aa5450eSShantappa Teekappanavar         }
12256aa5450eSShantappa Teekappanavar 
12266aa5450eSShantappa Teekappanavar         if (!ccinFromVpd.empty() && !ccinList.empty() &&
12276aa5450eSShantappa Teekappanavar             (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
12286aa5450eSShantappa Teekappanavar              ccinList.end()))
12296aa5450eSShantappa Teekappanavar         {
12306aa5450eSShantappa Teekappanavar             continue;
12316aa5450eSShantappa Teekappanavar         }
12326aa5450eSShantappa Teekappanavar 
1233e3fed701SPriyanga Ramasamy         if ((isSystemVpd) || (item.value("noprime", false)))
1234d3a379a6SSantosh Puranik         {
1235e3fed701SPriyanga Ramasamy 
1236e3fed701SPriyanga Ramasamy             // Populate one time properties for the system VPD and its sub-frus
1237e3fed701SPriyanga Ramasamy             // and for other non-primeable frus.
1238d3a379a6SSantosh Puranik             // For the remaining FRUs, this will get handled as a part of
1239d3a379a6SSantosh Puranik             // priming the inventory.
1240d3a379a6SSantosh Puranik             setOneTimeProperties(objectPath, interfaces);
1241d3a379a6SSantosh Puranik         }
1242d3a379a6SSantosh Puranik 
1243abb87edaSPriyangaRamasamy         // Populate the VPD keywords and the common interfaces only if we
1244abb87edaSPriyangaRamasamy         // are asked to inherit that data from the VPD, else only add the
1245abb87edaSPriyangaRamasamy         // extraInterfaces.
1246abb87edaSPriyangaRamasamy         if (item.value("inherit", true))
1247abb87edaSPriyangaRamasamy         {
124858e22145SAlpana Kumari             if constexpr (is_same<T, Parsed>::value)
1249abb87edaSPriyangaRamasamy             {
12508e140a1cSPriyangaRamasamy                 // Each record in the VPD becomes an interface and all
12518e140a1cSPriyangaRamasamy                 // keyword within the record are properties under that
12528e140a1cSPriyangaRamasamy                 // interface.
1253abb87edaSPriyangaRamasamy                 for (const auto& record : vpdMap)
1254abb87edaSPriyangaRamasamy                 {
1255abb87edaSPriyangaRamasamy                     populateFruSpecificInterfaces(
1256e12b181bSSunnySrivastava1984                         record.second, ipzVpdInf + record.first, interfaces);
1257abb87edaSPriyangaRamasamy                 }
1258abb87edaSPriyangaRamasamy             }
125958e22145SAlpana Kumari             else if constexpr (is_same<T, KeywordVpdMap>::value)
1260abb87edaSPriyangaRamasamy             {
1261e12b181bSSunnySrivastava1984                 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
1262abb87edaSPriyangaRamasamy             }
126388edeb6fSSantosh Puranik             if (js.find("commonInterfaces") != js.end())
126488edeb6fSSantosh Puranik             {
126588edeb6fSSantosh Puranik                 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
126688edeb6fSSantosh Puranik                                    isSystemVpd);
126788edeb6fSSantosh Puranik             }
1268abb87edaSPriyangaRamasamy         }
12690859eb65SSantosh Puranik         else
12700859eb65SSantosh Puranik         {
12710859eb65SSantosh Puranik             // Check if we have been asked to inherit specific record(s)
127258e22145SAlpana Kumari             if constexpr (is_same<T, Parsed>::value)
12730859eb65SSantosh Puranik             {
12740859eb65SSantosh Puranik                 if (item.find("copyRecords") != item.end())
12750859eb65SSantosh Puranik                 {
12760859eb65SSantosh Puranik                     for (const auto& record : item["copyRecords"])
12770859eb65SSantosh Puranik                     {
12780859eb65SSantosh Puranik                         const string& recordName = record;
12790859eb65SSantosh Puranik                         if (vpdMap.find(recordName) != vpdMap.end())
12800859eb65SSantosh Puranik                         {
12810859eb65SSantosh Puranik                             populateFruSpecificInterfaces(
1282e12b181bSSunnySrivastava1984                                 vpdMap.at(recordName), ipzVpdInf + recordName,
12830859eb65SSantosh Puranik                                 interfaces);
12840859eb65SSantosh Puranik                         }
12850859eb65SSantosh Puranik                     }
12860859eb65SSantosh Puranik                 }
12870859eb65SSantosh Puranik             }
12880859eb65SSantosh Puranik         }
1289abb87edaSPriyangaRamasamy         // Populate interfaces and properties that are common to every FRU
12908e140a1cSPriyangaRamasamy         // and additional interface that might be defined on a per-FRU
12918e140a1cSPriyangaRamasamy         // basis.
1292abb87edaSPriyangaRamasamy         if (item.find("extraInterfaces") != item.end())
1293abb87edaSPriyangaRamasamy         {
129488edeb6fSSantosh Puranik             populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
129588edeb6fSSantosh Puranik                                isSystemVpd);
1296abb87edaSPriyangaRamasamy         }
1297*aa8a893eSPriyanga Ramasamy         inventory::PropertyMap presProp;
1298*aa8a893eSPriyanga Ramasamy         presProp.emplace("Present", true);
1299*aa8a893eSPriyanga Ramasamy         insertOrMerge(interfaces, invItemIntf, move(presProp));
1300*aa8a893eSPriyanga Ramasamy 
1301abb87edaSPriyangaRamasamy         objects.emplace(move(object), move(interfaces));
1302abb87edaSPriyangaRamasamy     }
1303abb87edaSPriyangaRamasamy 
13048e140a1cSPriyangaRamasamy     if (isSystemVpd)
13058e140a1cSPriyangaRamasamy     {
13068e140a1cSPriyangaRamasamy         inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
13078e140a1cSPriyangaRamasamy         objects.insert(primeObject.begin(), primeObject.end());
130865b83601SAlpana Kumari 
1309f05effdbSAlpana Kumari         // set the U-boot environment variable for device-tree
1310f05effdbSAlpana Kumari         if constexpr (is_same<T, Parsed>::value)
1311f05effdbSAlpana Kumari         {
1312e5f177a5SSantosh Puranik             setDevTreeEnv(fs::path(getSystemsJson(vpdMap)).filename());
1313f05effdbSAlpana Kumari         }
13148e140a1cSPriyangaRamasamy     }
13158e140a1cSPriyangaRamasamy 
1316abb87edaSPriyangaRamasamy     // Notify PIM
13176c71c9dcSSunny Srivastava     common::utility::callPIM(move(objects));
1318abb87edaSPriyangaRamasamy }
1319abb87edaSPriyangaRamasamy 
1320abb87edaSPriyangaRamasamy int main(int argc, char** argv)
1321abb87edaSPriyangaRamasamy {
1322abb87edaSPriyangaRamasamy     int rc = 0;
1323a20be8ecSSunnySrivastava1984     json js{};
1324c2fe40f8SPriyangaRamasamy     Binary vpdVector{};
1325c2fe40f8SPriyangaRamasamy     string file{};
1326a20be8ecSSunnySrivastava1984     // map to hold additional data in case of logging pel
1327a20be8ecSSunnySrivastava1984     PelAdditionalData additionalData{};
1328a20be8ecSSunnySrivastava1984 
1329a20be8ecSSunnySrivastava1984     // this is needed to hold base fru inventory path in case there is ECC or
1330a20be8ecSSunnySrivastava1984     // vpd exception while parsing the file
1331a20be8ecSSunnySrivastava1984     std::string baseFruInventoryPath = {};
1332abb87edaSPriyangaRamasamy 
13330746eeebSSunny Srivastava     // severity for PEL
13340746eeebSSunny Srivastava     PelSeverity pelSeverity = PelSeverity::WARNING;
13350746eeebSSunny Srivastava 
1336abb87edaSPriyangaRamasamy     try
1337abb87edaSPriyangaRamasamy     {
1338abb87edaSPriyangaRamasamy         App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1339abb87edaSPriyangaRamasamy                 "in DBUS"};
1340abb87edaSPriyangaRamasamy 
1341abb87edaSPriyangaRamasamy         app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
13422f793048SAlpana Kumari             ->required();
1343abb87edaSPriyangaRamasamy 
1344abb87edaSPriyangaRamasamy         CLI11_PARSE(app, argc, argv);
1345abb87edaSPriyangaRamasamy 
13460746eeebSSunny Srivastava         // PEL severity should be ERROR in case of any system VPD failure
13470746eeebSSunny Srivastava         if (file == systemVpdFilePath)
13480746eeebSSunny Srivastava         {
13490746eeebSSunny Srivastava             pelSeverity = PelSeverity::ERROR;
13500746eeebSSunny Srivastava         }
13510746eeebSSunny Srivastava 
13520246a4d7SSantosh Puranik         auto jsonToParse = INVENTORY_JSON_DEFAULT;
13530246a4d7SSantosh Puranik 
13540246a4d7SSantosh Puranik         // If the symlink exists, it means it has been setup for us, switch the
13550246a4d7SSantosh Puranik         // path
13560246a4d7SSantosh Puranik         if (fs::exists(INVENTORY_JSON_SYM_LINK))
13570246a4d7SSantosh Puranik         {
13580246a4d7SSantosh Puranik             jsonToParse = INVENTORY_JSON_SYM_LINK;
13590246a4d7SSantosh Puranik         }
13600246a4d7SSantosh Puranik 
1361abb87edaSPriyangaRamasamy         // Make sure that the file path we get is for a supported EEPROM
13620246a4d7SSantosh Puranik         ifstream inventoryJson(jsonToParse);
1363a20be8ecSSunnySrivastava1984         if (!inventoryJson)
1364a20be8ecSSunnySrivastava1984         {
13650746eeebSSunny Srivastava             throw(VpdJsonException("Failed to access Json path", jsonToParse));
1366a20be8ecSSunnySrivastava1984         }
1367a20be8ecSSunnySrivastava1984 
1368a20be8ecSSunnySrivastava1984         try
1369a20be8ecSSunnySrivastava1984         {
1370a20be8ecSSunnySrivastava1984             js = json::parse(inventoryJson);
1371a20be8ecSSunnySrivastava1984         }
13728e15b93aSPatrick Williams         catch (const json::parse_error& ex)
1373a20be8ecSSunnySrivastava1984         {
13740746eeebSSunny Srivastava             throw(VpdJsonException("Json parsing failed", jsonToParse));
1375a20be8ecSSunnySrivastava1984         }
1376abb87edaSPriyangaRamasamy 
137712e24ff3SSantosh Puranik         // Do we have the mandatory "frus" section?
137812e24ff3SSantosh Puranik         if (js.find("frus") == js.end())
137912e24ff3SSantosh Puranik         {
138012e24ff3SSantosh Puranik             throw(VpdJsonException("FRUs section not found in JSON",
138112e24ff3SSantosh Puranik                                    jsonToParse));
138212e24ff3SSantosh Puranik         }
138312e24ff3SSantosh Puranik 
1384647868edSPriyangaRamasamy         // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1385647868edSPriyangaRamasamy         if (file.find("/ahb:apb") != string::npos)
1386647868edSPriyangaRamasamy         {
1387647868edSPriyangaRamasamy             // Translate udev path to a generic /sys/bus/.. file path.
1388647868edSPriyangaRamasamy             udevToGenericPath(file);
138912e24ff3SSantosh Puranik 
139012e24ff3SSantosh Puranik             if ((js["frus"].find(file) != js["frus"].end()) &&
139150f60bf8SSantosh Puranik                 (file == systemVpdFilePath))
1392647868edSPriyangaRamasamy             {
13933c24414fSSunny Srivastava                 // We need manager service active to process restoring of
13943c24414fSSunny Srivastava                 // system VPD on hardware. So in case any system restore is
13953c24414fSSunny Srivastava                 // required, update hardware in the second trigger of parser
13963c24414fSSunny Srivastava                 // code for system vpd file path.
13973c24414fSSunny Srivastava 
13983c24414fSSunny Srivastava                 std::vector<std::string> interfaces{motherBoardInterface};
13993c24414fSSunny Srivastava 
14003c24414fSSunny Srivastava                 // call mapper to check for object path creation
14013c24414fSSunny Srivastava                 MapperResponse subTree =
14023c24414fSSunny Srivastava                     getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
14033c24414fSSunny Srivastava                 string mboardPath =
14043c24414fSSunny Srivastava                     js["frus"][file].at(0).value("inventoryPath", "");
14053c24414fSSunny Srivastava 
14063c24414fSSunny Srivastava                 // Attempt system VPD restore if we have a motherboard
14073c24414fSSunny Srivastava                 // object in the inventory.
14083c24414fSSunny Srivastava                 if ((subTree.size() != 0) &&
14093c24414fSSunny Srivastava                     (subTree.find(pimPath + mboardPath) != subTree.end()))
14103c24414fSSunny Srivastava                 {
14113c24414fSSunny Srivastava                     vpdVector = getVpdDataInVector(js, file);
14123c24414fSSunny Srivastava                     ParserInterface* parser =
14133c24414fSSunny Srivastava                         ParserFactory::getParser(vpdVector);
14143c24414fSSunny Srivastava                     variant<KeywordVpdMap, Store> parseResult;
14153c24414fSSunny Srivastava                     parseResult = parser->parse();
14163c24414fSSunny Srivastava 
14173c24414fSSunny Srivastava                     if (auto pVal = get_if<Store>(&parseResult))
14183c24414fSSunny Srivastava                     {
14193c24414fSSunny Srivastava                         // map to hold all the keywords whose value is blank and
14203c24414fSSunny Srivastava                         // needs to be updated at standby.
14213c24414fSSunny Srivastava                         vector<RestoredEeproms> blankSystemVpdProperties{};
14223c24414fSSunny Srivastava                         getListOfBlankSystemVpd(pVal->getVpdMap(), mboardPath,
14233c24414fSSunny Srivastava                                                 blankSystemVpdProperties);
14243c24414fSSunny Srivastava 
14253c24414fSSunny Srivastava                         // if system VPD restore is required, update the
14263c24414fSSunny Srivastava                         // EEPROM
14273c24414fSSunny Srivastava                         for (const auto& item : blankSystemVpdProperties)
14283c24414fSSunny Srivastava                         {
14293c24414fSSunny Srivastava                             updateHardware(get<0>(item), get<1>(item),
14303c24414fSSunny Srivastava                                            get<2>(item), get<3>(item));
14313c24414fSSunny Srivastava                         }
14323c24414fSSunny Srivastava                     }
14333c24414fSSunny Srivastava                     else
14343c24414fSSunny Srivastava                     {
14353c24414fSSunny Srivastava                         std::cout << "Not a valid format to restore system VPD"
14363c24414fSSunny Srivastava                                   << std::endl;
14373c24414fSSunny Srivastava                     }
14383c24414fSSunny Srivastava                     // release the parser object
14393c24414fSSunny Srivastava                     ParserFactory::freeParser(parser);
14403c24414fSSunny Srivastava                 }
14413c24414fSSunny Srivastava                 else
14423c24414fSSunny Srivastava                 {
14433c24414fSSunny Srivastava                     log<level::ERR>("No object path found");
14443c24414fSSunny Srivastava                 }
1445647868edSPriyangaRamasamy                 return 0;
1446647868edSPriyangaRamasamy             }
1447647868edSPriyangaRamasamy         }
1448647868edSPriyangaRamasamy 
1449647868edSPriyangaRamasamy         if (file.empty())
1450647868edSPriyangaRamasamy         {
1451647868edSPriyangaRamasamy             cerr << "The EEPROM path <" << file << "> is not valid.";
1452647868edSPriyangaRamasamy             return 0;
1453647868edSPriyangaRamasamy         }
145412e24ff3SSantosh Puranik         if (js["frus"].find(file) == js["frus"].end())
1455abb87edaSPriyangaRamasamy         {
145688edeb6fSSantosh Puranik             return 0;
1457abb87edaSPriyangaRamasamy         }
1458abb87edaSPriyangaRamasamy 
14592f793048SAlpana Kumari         if (!fs::exists(file))
14602f793048SAlpana Kumari         {
14612f793048SAlpana Kumari             cout << "Device path: " << file
14622f793048SAlpana Kumari                  << " does not exist. Spurious udev event? Exiting." << endl;
14632f793048SAlpana Kumari             return 0;
14642f793048SAlpana Kumari         }
14652f793048SAlpana Kumari 
1466a20be8ecSSunnySrivastava1984         baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
14678589375fSSantosh Puranik         // Check if we can read the VPD file based on the power state
146827a5e95aSSantosh Puranik         // We skip reading VPD when the power is ON in two scenarios:
146927a5e95aSSantosh Puranik         // 1) The eeprom we are trying to read is that of the system VPD
147027a5e95aSSantosh Puranik         // 2) The JSON tells us that the FRU EEPROM cannot be read when
147127a5e95aSSantosh Puranik         // we are powered ON.
147227a5e95aSSantosh Puranik         if (js["frus"][file].at(0).value("powerOffOnly", false) ||
147327a5e95aSSantosh Puranik             (file == systemVpdFilePath))
14748589375fSSantosh Puranik         {
14758589375fSSantosh Puranik             if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
14768589375fSSantosh Puranik                 getPowerState())
14778589375fSSantosh Puranik             {
14788589375fSSantosh Puranik                 cout << "This VPD cannot be read when power is ON" << endl;
14798589375fSSantosh Puranik                 return 0;
14808589375fSSantosh Puranik             }
14818589375fSSantosh Puranik         }
1482a20be8ecSSunnySrivastava1984 
14832f793048SAlpana Kumari         try
14842f793048SAlpana Kumari         {
1485c2fe40f8SPriyangaRamasamy             vpdVector = getVpdDataInVector(js, file);
148633c61c2dSPriyangaRamasamy             ParserInterface* parser = ParserFactory::getParser(vpdVector);
14872f793048SAlpana Kumari             variant<KeywordVpdMap, Store> parseResult;
14882f793048SAlpana Kumari             parseResult = parser->parse();
14899a19554cSSunnySrivastava1984 
1490e12b181bSSunnySrivastava1984             if (auto pVal = get_if<Store>(&parseResult))
1491abb87edaSPriyangaRamasamy             {
1492e12b181bSSunnySrivastava1984                 populateDbus(pVal->getVpdMap(), js, file);
1493e12b181bSSunnySrivastava1984             }
1494e12b181bSSunnySrivastava1984             else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1495abb87edaSPriyangaRamasamy             {
1496e12b181bSSunnySrivastava1984                 populateDbus(*pVal, js, file);
1497abb87edaSPriyangaRamasamy             }
1498abb87edaSPriyangaRamasamy 
1499e12b181bSSunnySrivastava1984             // release the parser object
1500e12b181bSSunnySrivastava1984             ParserFactory::freeParser(parser);
1501abb87edaSPriyangaRamasamy         }
15028e15b93aSPatrick Williams         catch (const exception& e)
15032f793048SAlpana Kumari         {
15042f793048SAlpana Kumari             postFailAction(js, file);
1505a504c3eeSPriyangaRamasamy             throw;
15062f793048SAlpana Kumari         }
15072f793048SAlpana Kumari     }
1508a20be8ecSSunnySrivastava1984     catch (const VpdJsonException& ex)
1509a20be8ecSSunnySrivastava1984     {
1510a20be8ecSSunnySrivastava1984         additionalData.emplace("JSON_PATH", ex.getJsonPath());
1511a20be8ecSSunnySrivastava1984         additionalData.emplace("DESCRIPTION", ex.what());
15120746eeebSSunny Srivastava         createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
1513a20be8ecSSunnySrivastava1984 
1514a20be8ecSSunnySrivastava1984         cerr << ex.what() << "\n";
1515a20be8ecSSunnySrivastava1984         rc = -1;
1516a20be8ecSSunnySrivastava1984     }
1517a20be8ecSSunnySrivastava1984     catch (const VpdEccException& ex)
1518a20be8ecSSunnySrivastava1984     {
1519a20be8ecSSunnySrivastava1984         additionalData.emplace("DESCRIPTION", "ECC check failed");
1520a20be8ecSSunnySrivastava1984         additionalData.emplace("CALLOUT_INVENTORY_PATH",
1521a20be8ecSSunnySrivastava1984                                INVENTORY_PATH + baseFruInventoryPath);
15220746eeebSSunny Srivastava         createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
1523c2fe40f8SPriyangaRamasamy         dumpBadVpd(file, vpdVector);
1524a20be8ecSSunnySrivastava1984         cerr << ex.what() << "\n";
1525a20be8ecSSunnySrivastava1984         rc = -1;
1526a20be8ecSSunnySrivastava1984     }
1527a20be8ecSSunnySrivastava1984     catch (const VpdDataException& ex)
1528a20be8ecSSunnySrivastava1984     {
15295cb3b1f3Salpana07         if (isThisPcieOnPass1planar(js, file))
15305cb3b1f3Salpana07         {
15315cb3b1f3Salpana07             cout << "Pcie_device  [" << file
15325cb3b1f3Salpana07                  << "]'s VPD is not valid on PASS1 planar.Ignoring.\n";
15335cb3b1f3Salpana07             rc = 0;
15345cb3b1f3Salpana07         }
15355cb3b1f3Salpana07         else
15365cb3b1f3Salpana07         {
15375cb3b1f3Salpana07             string errorMsg =
15385cb3b1f3Salpana07                 "VPD file is either empty or invalid. Parser failed for [";
15395cb3b1f3Salpana07             errorMsg += file;
15405cb3b1f3Salpana07             errorMsg += "], with error = " + std::string(ex.what());
15415cb3b1f3Salpana07 
15425cb3b1f3Salpana07             additionalData.emplace("DESCRIPTION", errorMsg);
1543a20be8ecSSunnySrivastava1984             additionalData.emplace("CALLOUT_INVENTORY_PATH",
1544a20be8ecSSunnySrivastava1984                                    INVENTORY_PATH + baseFruInventoryPath);
15450746eeebSSunny Srivastava             createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
15465cb3b1f3Salpana07 
1547a20be8ecSSunnySrivastava1984             rc = -1;
1548a20be8ecSSunnySrivastava1984         }
15495cb3b1f3Salpana07     }
15508e15b93aSPatrick Williams     catch (const exception& e)
1551abb87edaSPriyangaRamasamy     {
1552c2fe40f8SPriyangaRamasamy         dumpBadVpd(file, vpdVector);
1553abb87edaSPriyangaRamasamy         cerr << e.what() << "\n";
1554abb87edaSPriyangaRamasamy         rc = -1;
1555abb87edaSPriyangaRamasamy     }
1556abb87edaSPriyangaRamasamy 
1557abb87edaSPriyangaRamasamy     return rc;
1558abb87edaSPriyangaRamasamy }
1559