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         }
324aa8a893eSPriyanga 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 
433*735dee9bSAlpana Kumari /** Performs any pre-action needed to get the FRU setup for collection.
4342f793048SAlpana Kumari  *
4352f793048SAlpana Kumari  * @param[in] json - json object
4362f793048SAlpana Kumari  * @param[in] file - eeprom file path
4372f793048SAlpana Kumari  */
4382f793048SAlpana Kumari static void preAction(const nlohmann::json& json, const string& file)
4392f793048SAlpana Kumari {
440*735dee9bSAlpana Kumari     if ((json["frus"][file].at(0)).find("preAction") ==
4412f793048SAlpana Kumari         json["frus"][file].at(0).end())
4422f793048SAlpana Kumari     {
4432f793048SAlpana Kumari         return;
4442f793048SAlpana Kumari     }
4452f793048SAlpana Kumari 
446*735dee9bSAlpana Kumari     if (executePreAction(json, file))
4472f793048SAlpana Kumari     {
44864b9f5f1SAlpana Kumari         if (json["frus"][file].at(0).find("devAddress") !=
44964b9f5f1SAlpana Kumari             json["frus"][file].at(0).end())
45064b9f5f1SAlpana Kumari         {
451*735dee9bSAlpana Kumari             // Now bind the device
4524c3bf5beSAlpana Kumari             string bind = json["frus"][file].at(0).value("devAddress", "");
4532f793048SAlpana Kumari             cout << "Binding device " << bind << endl;
4542f793048SAlpana Kumari             string bindCmd = string("echo \"") + bind +
4552f793048SAlpana Kumari                              string("\" > /sys/bus/i2c/drivers/at24/bind");
4562f793048SAlpana Kumari             cout << bindCmd << endl;
4572f793048SAlpana Kumari             executeCmd(bindCmd);
45864b9f5f1SAlpana Kumari         }
4592f793048SAlpana Kumari 
4602f793048SAlpana Kumari         // Check if device showed up (test for file)
4612f793048SAlpana Kumari         if (!fs::exists(file))
4622f793048SAlpana Kumari         {
4632f793048SAlpana Kumari             cout << "EEPROM " << file << " does not exist. Take failure action"
4642f793048SAlpana Kumari                  << endl;
4652f793048SAlpana Kumari             // If not, then take failure postAction
466*735dee9bSAlpana Kumari             executePostFailAction(json, file);
467*735dee9bSAlpana Kumari         }
4682f793048SAlpana Kumari     }
4692f793048SAlpana Kumari }
4702f793048SAlpana Kumari 
471abb87edaSPriyangaRamasamy /**
472d3a379a6SSantosh Puranik  * @brief Set certain one time properties in the inventory
473d3a379a6SSantosh Puranik  * Use this function to insert the Functional and Enabled properties into the
474d3a379a6SSantosh Puranik  * inventory map. This function first checks if the object in question already
475d3a379a6SSantosh Puranik  * has these properties hosted on D-Bus, if the property is already there, it is
476d3a379a6SSantosh Puranik  * not modified, hence the name "one time". If the property is not already
477d3a379a6SSantosh Puranik  * present, it will be added to the map with a suitable default value (true for
478d3a379a6SSantosh Puranik  * Functional and false for Enabled)
479d3a379a6SSantosh Puranik  *
480d3a379a6SSantosh Puranik  * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
481d3a379a6SSantosh Puranik  * @param[inout] interfaces - Reference to a map of inventory interfaces to
482d3a379a6SSantosh Puranik  * which the properties will be attached.
483d3a379a6SSantosh Puranik  */
484d3a379a6SSantosh Puranik static void setOneTimeProperties(const std::string& object,
485d3a379a6SSantosh Puranik                                  inventory::InterfaceMap& interfaces)
486d3a379a6SSantosh Puranik {
487d3a379a6SSantosh Puranik     auto bus = sdbusplus::bus::new_default();
488d3a379a6SSantosh Puranik     auto objectPath = INVENTORY_PATH + object;
489d3a379a6SSantosh Puranik     auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
490d3a379a6SSantosh Puranik                                     objectPath.c_str(),
491d3a379a6SSantosh Puranik                                     "org.freedesktop.DBus.Properties", "Get");
492d3a379a6SSantosh Puranik     prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
493d3a379a6SSantosh Puranik     prop.append("Functional");
494d3a379a6SSantosh Puranik     try
495d3a379a6SSantosh Puranik     {
496d3a379a6SSantosh Puranik         auto result = bus.call(prop);
497d3a379a6SSantosh Puranik     }
498d3a379a6SSantosh Puranik     catch (const sdbusplus::exception::SdBusError& e)
499d3a379a6SSantosh Puranik     {
500d3a379a6SSantosh Puranik         // Treat as property unavailable
501d3a379a6SSantosh Puranik         inventory::PropertyMap prop;
502d3a379a6SSantosh Puranik         prop.emplace("Functional", true);
503d3a379a6SSantosh Puranik         interfaces.emplace(
504d3a379a6SSantosh Puranik             "xyz.openbmc_project.State.Decorator.OperationalStatus",
505d3a379a6SSantosh Puranik             move(prop));
506d3a379a6SSantosh Puranik     }
507d3a379a6SSantosh Puranik     prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
508d3a379a6SSantosh Puranik                                objectPath.c_str(),
509d3a379a6SSantosh Puranik                                "org.freedesktop.DBus.Properties", "Get");
510d3a379a6SSantosh Puranik     prop.append("xyz.openbmc_project.Object.Enable");
511d3a379a6SSantosh Puranik     prop.append("Enabled");
512d3a379a6SSantosh Puranik     try
513d3a379a6SSantosh Puranik     {
514d3a379a6SSantosh Puranik         auto result = bus.call(prop);
515d3a379a6SSantosh Puranik     }
516d3a379a6SSantosh Puranik     catch (const sdbusplus::exception::SdBusError& e)
517d3a379a6SSantosh Puranik     {
518d3a379a6SSantosh Puranik         // Treat as property unavailable
519d3a379a6SSantosh Puranik         inventory::PropertyMap prop;
520d3a379a6SSantosh Puranik         prop.emplace("Enabled", false);
521d3a379a6SSantosh Puranik         interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
522d3a379a6SSantosh Puranik     }
523d3a379a6SSantosh Puranik }
524d3a379a6SSantosh Puranik 
525d3a379a6SSantosh Puranik /**
5268e140a1cSPriyangaRamasamy  * @brief Prime the Inventory
5278e140a1cSPriyangaRamasamy  * Prime the inventory by populating only the location code,
5288e140a1cSPriyangaRamasamy  * type interface and the inventory object for the frus
5298e140a1cSPriyangaRamasamy  * which are not system vpd fru.
530abb87edaSPriyangaRamasamy  *
5318e140a1cSPriyangaRamasamy  * @param[in] jsObject - Reference to vpd inventory json object
5328e140a1cSPriyangaRamasamy  * @param[in] vpdMap -  Reference to the parsed vpd map
5338e140a1cSPriyangaRamasamy  *
5348e140a1cSPriyangaRamasamy  * @returns Map of items in extraInterface.
5358e140a1cSPriyangaRamasamy  */
5368e140a1cSPriyangaRamasamy template <typename T>
5378e140a1cSPriyangaRamasamy inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
5388e140a1cSPriyangaRamasamy                                     const T& vpdMap)
5398e140a1cSPriyangaRamasamy {
5408e140a1cSPriyangaRamasamy     inventory::ObjectMap objects;
5418e140a1cSPriyangaRamasamy 
5428e140a1cSPriyangaRamasamy     for (auto& itemFRUS : jsObject["frus"].items())
5438e140a1cSPriyangaRamasamy     {
5448e140a1cSPriyangaRamasamy         for (auto& itemEEPROM : itemFRUS.value())
5458e140a1cSPriyangaRamasamy         {
5462e6c6f73SAlpana Kumari             // Take pre actions if needed
5472e6c6f73SAlpana Kumari             if (itemEEPROM.find("preAction") != itemEEPROM.end())
5482e6c6f73SAlpana Kumari             {
5492e6c6f73SAlpana Kumari                 preAction(jsObject, itemFRUS.key());
5502e6c6f73SAlpana Kumari             }
5512e6c6f73SAlpana Kumari 
5528e140a1cSPriyangaRamasamy             inventory::InterfaceMap interfaces;
5538e140a1cSPriyangaRamasamy             inventory::Object object(itemEEPROM.at("inventoryPath"));
5548e140a1cSPriyangaRamasamy 
55550f60bf8SSantosh Puranik             if ((itemFRUS.key() != systemVpdFilePath) &&
55650f60bf8SSantosh Puranik                 !itemEEPROM.value("noprime", false))
5578e140a1cSPriyangaRamasamy             {
558cfd7a75aSAlpana Kumari                 inventory::PropertyMap presProp;
559cfd7a75aSAlpana Kumari                 presProp.emplace("Present", false);
560cfd7a75aSAlpana Kumari                 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
561d3a379a6SSantosh Puranik                                    presProp);
562d3a379a6SSantosh Puranik                 setOneTimeProperties(object, interfaces);
5638e140a1cSPriyangaRamasamy                 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
5648e140a1cSPriyangaRamasamy                 {
5658e140a1cSPriyangaRamasamy                     for (const auto& eI : itemEEPROM["extraInterfaces"].items())
5668e140a1cSPriyangaRamasamy                     {
5678e140a1cSPriyangaRamasamy                         inventory::PropertyMap props;
568414d5aefSAlpana Kumari                         if (eI.key() == IBM_LOCATION_CODE_INF)
5698e140a1cSPriyangaRamasamy                         {
5708e140a1cSPriyangaRamasamy                             if constexpr (std::is_same<T, Parsed>::value)
5718e140a1cSPriyangaRamasamy                             {
5728e140a1cSPriyangaRamasamy                                 for (auto& lC : eI.value().items())
5738e140a1cSPriyangaRamasamy                                 {
5748e140a1cSPriyangaRamasamy                                     auto propVal = expandLocationCode(
5758e140a1cSPriyangaRamasamy                                         lC.value().get<string>(), vpdMap, true);
5768e140a1cSPriyangaRamasamy 
5778e140a1cSPriyangaRamasamy                                     props.emplace(move(lC.key()),
5788e140a1cSPriyangaRamasamy                                                   move(propVal));
579b0f3749dSSantosh Puranik                                     interfaces.emplace(XYZ_LOCATION_CODE_INF,
580b0f3749dSSantosh Puranik                                                        props);
5818e140a1cSPriyangaRamasamy                                     interfaces.emplace(move(eI.key()),
5828e140a1cSPriyangaRamasamy                                                        move(props));
5838e140a1cSPriyangaRamasamy                                 }
5848e140a1cSPriyangaRamasamy                             }
5858e140a1cSPriyangaRamasamy                         }
5868e140a1cSPriyangaRamasamy                         else if (eI.key().find("Inventory.Item.") !=
5878e140a1cSPriyangaRamasamy                                  string::npos)
5888e140a1cSPriyangaRamasamy                         {
5898e140a1cSPriyangaRamasamy                             interfaces.emplace(move(eI.key()), move(props));
5908e140a1cSPriyangaRamasamy                         }
591d3a379a6SSantosh Puranik                         else if (eI.key() ==
592d3a379a6SSantosh Puranik                                  "xyz.openbmc_project.Inventory.Item")
593d3a379a6SSantosh Puranik                         {
594d3a379a6SSantosh Puranik                             for (auto& val : eI.value().items())
595d3a379a6SSantosh Puranik                             {
596d3a379a6SSantosh Puranik                                 if (val.key() == "PrettyName")
597d3a379a6SSantosh Puranik                                 {
598d3a379a6SSantosh Puranik                                     presProp.emplace(val.key(),
599d3a379a6SSantosh Puranik                                                      val.value().get<string>());
600d3a379a6SSantosh Puranik                                 }
601d3a379a6SSantosh Puranik                             }
602d3a379a6SSantosh Puranik                             // Use insert_or_assign here as we may already have
603d3a379a6SSantosh Puranik                             // inserted the present property only earlier in
604d3a379a6SSantosh Puranik                             // this function under this same interface.
605d3a379a6SSantosh Puranik                             interfaces.insert_or_assign(eI.key(),
606d3a379a6SSantosh Puranik                                                         move(presProp));
607d3a379a6SSantosh Puranik                         }
6088e140a1cSPriyangaRamasamy                     }
6098e140a1cSPriyangaRamasamy                 }
6108e140a1cSPriyangaRamasamy                 objects.emplace(move(object), move(interfaces));
6118e140a1cSPriyangaRamasamy             }
6128e140a1cSPriyangaRamasamy         }
6138e140a1cSPriyangaRamasamy     }
6148e140a1cSPriyangaRamasamy     return objects;
6158e140a1cSPriyangaRamasamy }
6168e140a1cSPriyangaRamasamy 
61765b83601SAlpana Kumari /**
61865b83601SAlpana Kumari  * @brief This API executes command to set environment variable
61965b83601SAlpana Kumari  *        And then reboot the system
62065b83601SAlpana Kumari  * @param[in] key   -env key to set new value
62165b83601SAlpana Kumari  * @param[in] value -value to set.
62265b83601SAlpana Kumari  */
62365b83601SAlpana Kumari void setEnvAndReboot(const string& key, const string& value)
62465b83601SAlpana Kumari {
62565b83601SAlpana Kumari     // set env and reboot and break.
62665b83601SAlpana Kumari     executeCmd("/sbin/fw_setenv", key, value);
627280197e3SAndrew Geissler     log<level::INFO>("Rebooting BMC to pick up new device tree");
62865b83601SAlpana Kumari     // make dbus call to reboot
62965b83601SAlpana Kumari     auto bus = sdbusplus::bus::new_default_system();
63065b83601SAlpana Kumari     auto method = bus.new_method_call(
63165b83601SAlpana Kumari         "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
63265b83601SAlpana Kumari         "org.freedesktop.systemd1.Manager", "Reboot");
63365b83601SAlpana Kumari     bus.call_noreply(method);
63465b83601SAlpana Kumari }
63565b83601SAlpana Kumari 
63665b83601SAlpana Kumari /*
63765b83601SAlpana Kumari  * @brief This API checks for env var fitconfig.
63865b83601SAlpana Kumari  *        If not initialised OR updated as per the current system type,
63965b83601SAlpana Kumari  *        update this env var and reboot the system.
64065b83601SAlpana Kumari  *
64165b83601SAlpana Kumari  * @param[in] systemType IM kwd in vpd tells about which system type it is.
64265b83601SAlpana Kumari  * */
64365b83601SAlpana Kumari void setDevTreeEnv(const string& systemType)
64465b83601SAlpana Kumari {
64537e72701SAlpana Kumari     // Init with default dtb
64637e72701SAlpana Kumari     string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
647e5f177a5SSantosh Puranik     static const deviceTreeMap deviceTreeSystemTypeMap = {
648e5f177a5SSantosh Puranik         {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
649e5f177a5SSantosh Puranik         {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
650e5f177a5SSantosh Puranik         {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
651e5f177a5SSantosh Puranik         {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
652e5f177a5SSantosh Puranik         {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
653e5f177a5SSantosh Puranik         {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}};
65465b83601SAlpana Kumari 
65565b83601SAlpana Kumari     if (deviceTreeSystemTypeMap.find(systemType) !=
65665b83601SAlpana Kumari         deviceTreeSystemTypeMap.end())
65765b83601SAlpana Kumari     {
65865b83601SAlpana Kumari         newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
65965b83601SAlpana Kumari     }
66037e72701SAlpana Kumari     else
66137e72701SAlpana Kumari     {
66237e72701SAlpana Kumari         // System type not supported
663ab1e22c3SAlpana Kumari         string err = "This System type not found/supported in dtb table " +
664ab1e22c3SAlpana Kumari                      systemType +
665ab1e22c3SAlpana Kumari                      ".Please check the HW and IM keywords in the system "
666ab1e22c3SAlpana Kumari                      "VPD.Breaking...";
667ab1e22c3SAlpana Kumari 
668ab1e22c3SAlpana Kumari         // map to hold additional data in case of logging pel
669ab1e22c3SAlpana Kumari         PelAdditionalData additionalData{};
670ab1e22c3SAlpana Kumari         additionalData.emplace("DESCRIPTION", err);
671ab1e22c3SAlpana Kumari         createPEL(additionalData, PelSeverity::WARNING,
672ab1e22c3SAlpana Kumari                   errIntfForInvalidSystemType);
673ab1e22c3SAlpana Kumari         exit(-1);
67437e72701SAlpana Kumari     }
67565b83601SAlpana Kumari 
67665b83601SAlpana Kumari     string readVarValue;
67765b83601SAlpana Kumari     bool envVarFound = false;
67865b83601SAlpana Kumari 
67965b83601SAlpana Kumari     vector<string> output = executeCmd("/sbin/fw_printenv");
68065b83601SAlpana Kumari     for (const auto& entry : output)
68165b83601SAlpana Kumari     {
68265b83601SAlpana Kumari         size_t pos = entry.find("=");
68365b83601SAlpana Kumari         string key = entry.substr(0, pos);
68465b83601SAlpana Kumari         if (key != "fitconfig")
68565b83601SAlpana Kumari         {
68665b83601SAlpana Kumari             continue;
68765b83601SAlpana Kumari         }
68865b83601SAlpana Kumari 
68965b83601SAlpana Kumari         envVarFound = true;
69065b83601SAlpana Kumari         if (pos + 1 < entry.size())
69165b83601SAlpana Kumari         {
69265b83601SAlpana Kumari             readVarValue = entry.substr(pos + 1);
69365b83601SAlpana Kumari             if (readVarValue.find(newDeviceTree) != string::npos)
69465b83601SAlpana Kumari             {
69565b83601SAlpana Kumari                 // fitconfig is Updated. No action needed
69665b83601SAlpana Kumari                 break;
69765b83601SAlpana Kumari             }
69865b83601SAlpana Kumari         }
69965b83601SAlpana Kumari         // set env and reboot and break.
70065b83601SAlpana Kumari         setEnvAndReboot(key, newDeviceTree);
70165b83601SAlpana Kumari         exit(0);
70265b83601SAlpana Kumari     }
70365b83601SAlpana Kumari 
70465b83601SAlpana Kumari     // check If env var Not found
70565b83601SAlpana Kumari     if (!envVarFound)
70665b83601SAlpana Kumari     {
70765b83601SAlpana Kumari         setEnvAndReboot("fitconfig", newDeviceTree);
70865b83601SAlpana Kumari     }
70965b83601SAlpana Kumari }
71065b83601SAlpana Kumari 
7118e140a1cSPriyangaRamasamy /**
7129094d4f6SSunnySrivastava1984  * @brief API to call VPD manager to write VPD to EEPROM.
7139094d4f6SSunnySrivastava1984  * @param[in] Object path.
7149094d4f6SSunnySrivastava1984  * @param[in] record to be updated.
7159094d4f6SSunnySrivastava1984  * @param[in] keyword to be updated.
7169094d4f6SSunnySrivastava1984  * @param[in] keyword data to be updated
7179094d4f6SSunnySrivastava1984  */
7189094d4f6SSunnySrivastava1984 void updateHardware(const string& objectName, const string& recName,
7199094d4f6SSunnySrivastava1984                     const string& kwdName, const Binary& data)
7209094d4f6SSunnySrivastava1984 {
7219094d4f6SSunnySrivastava1984     try
7229094d4f6SSunnySrivastava1984     {
7239094d4f6SSunnySrivastava1984         auto bus = sdbusplus::bus::new_default();
7249094d4f6SSunnySrivastava1984         auto properties =
7259094d4f6SSunnySrivastava1984             bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
7269094d4f6SSunnySrivastava1984         properties.append(
7279094d4f6SSunnySrivastava1984             static_cast<sdbusplus::message::object_path>(objectName));
7289094d4f6SSunnySrivastava1984         properties.append(recName);
7299094d4f6SSunnySrivastava1984         properties.append(kwdName);
7309094d4f6SSunnySrivastava1984         properties.append(data);
7319094d4f6SSunnySrivastava1984         bus.call(properties);
7329094d4f6SSunnySrivastava1984     }
7338be4334fSPatrick Williams     catch (const sdbusplus::exception::exception& e)
7349094d4f6SSunnySrivastava1984     {
7359094d4f6SSunnySrivastava1984         std::string what =
7369094d4f6SSunnySrivastava1984             "VPDManager WriteKeyword api failed for inventory path " +
7379094d4f6SSunnySrivastava1984             objectName;
7389094d4f6SSunnySrivastava1984         what += " record " + recName;
7399094d4f6SSunnySrivastava1984         what += " keyword " + kwdName;
7409094d4f6SSunnySrivastava1984         what += " with bus error = " + std::string(e.what());
7419094d4f6SSunnySrivastava1984 
7429094d4f6SSunnySrivastava1984         // map to hold additional data in case of logging pel
7439094d4f6SSunnySrivastava1984         PelAdditionalData additionalData{};
7449094d4f6SSunnySrivastava1984         additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
7459094d4f6SSunnySrivastava1984         additionalData.emplace("DESCRIPTION", what);
7460746eeebSSunny Srivastava         createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
7479094d4f6SSunnySrivastava1984     }
7489094d4f6SSunnySrivastava1984 }
7499094d4f6SSunnySrivastava1984 
7509094d4f6SSunnySrivastava1984 /**
7513c24414fSSunny Srivastava  * @brief An api to get list of blank system VPD properties.
7523c24414fSSunny Srivastava  * @param[in] vpdMap - IPZ vpd map.
7533c24414fSSunny Srivastava  * @param[in] objectPath - Object path for the FRU.
7543c24414fSSunny Srivastava  * @param[out] blankPropertyList - Properties which are blank in System VPD and
7553c24414fSSunny Srivastava  * needs to be updated as standby.
7563c24414fSSunny Srivastava  */
7573c24414fSSunny Srivastava void getListOfBlankSystemVpd(Parsed& vpdMap, const string& objectPath,
7583c24414fSSunny Srivastava                              std::vector<RestoredEeproms>& blankPropertyList)
7593c24414fSSunny Srivastava {
7603c24414fSSunny Srivastava     for (const auto& systemRecKwdPair : svpdKwdMap)
7613c24414fSSunny Srivastava     {
7623c24414fSSunny Srivastava         auto it = vpdMap.find(systemRecKwdPair.first);
7633c24414fSSunny Srivastava 
7643c24414fSSunny Srivastava         // check if record is found in map we got by parser
7653c24414fSSunny Srivastava         if (it != vpdMap.end())
7663c24414fSSunny Srivastava         {
7673c24414fSSunny Srivastava             const auto& kwdListForRecord = systemRecKwdPair.second;
7683c24414fSSunny Srivastava             for (const auto& keyword : kwdListForRecord)
7693c24414fSSunny Srivastava             {
7703c24414fSSunny Srivastava                 DbusPropertyMap& kwdValMap = it->second;
7713c24414fSSunny Srivastava                 auto iterator = kwdValMap.find(keyword);
7723c24414fSSunny Srivastava 
7733c24414fSSunny Srivastava                 if (iterator != kwdValMap.end())
7743c24414fSSunny Srivastava                 {
7753c24414fSSunny Srivastava                     string& kwdValue = iterator->second;
7763c24414fSSunny Srivastava 
7773c24414fSSunny Srivastava                     // check bus data
7783c24414fSSunny Srivastava                     const string& recordName = systemRecKwdPair.first;
7793c24414fSSunny Srivastava                     const string& busValue = readBusProperty(
7803c24414fSSunny Srivastava                         objectPath, ipzVpdInf + recordName, keyword);
7813c24414fSSunny Srivastava 
7823c24414fSSunny Srivastava                     if (busValue.find_first_not_of(' ') != string::npos)
7833c24414fSSunny Srivastava                     {
7843c24414fSSunny Srivastava                         if (kwdValue.find_first_not_of(' ') == string::npos)
7853c24414fSSunny Srivastava                         {
7863c24414fSSunny Srivastava                             // implies data is blank on EEPROM but not on cache.
7873c24414fSSunny Srivastava                             // So EEPROM vpd update is required.
7883c24414fSSunny Srivastava                             Binary busData(busValue.begin(), busValue.end());
7893c24414fSSunny Srivastava 
7903c24414fSSunny Srivastava                             blankPropertyList.push_back(std::make_tuple(
7913c24414fSSunny Srivastava                                 objectPath, recordName, keyword, busData));
7923c24414fSSunny Srivastava                         }
7933c24414fSSunny Srivastava                     }
7943c24414fSSunny Srivastava                 }
7953c24414fSSunny Srivastava             }
7963c24414fSSunny Srivastava         }
7973c24414fSSunny Srivastava     }
7983c24414fSSunny Srivastava }
7993c24414fSSunny Srivastava 
8003c24414fSSunny Srivastava /**
8019094d4f6SSunnySrivastava1984  * @brief API to check if we need to restore system VPD
8029094d4f6SSunnySrivastava1984  * This functionality is only applicable for IPZ VPD data.
8039094d4f6SSunnySrivastava1984  * @param[in] vpdMap - IPZ vpd map
8049094d4f6SSunnySrivastava1984  * @param[in] objectPath - Object path for the FRU
8059094d4f6SSunnySrivastava1984  */
8063c24414fSSunny Srivastava void restoreSystemVPD(Parsed& vpdMap, const string& objectPath)
8079094d4f6SSunnySrivastava1984 {
8089094d4f6SSunnySrivastava1984     for (const auto& systemRecKwdPair : svpdKwdMap)
8099094d4f6SSunnySrivastava1984     {
8109094d4f6SSunnySrivastava1984         auto it = vpdMap.find(systemRecKwdPair.first);
8119094d4f6SSunnySrivastava1984 
8129094d4f6SSunnySrivastava1984         // check if record is found in map we got by parser
8139094d4f6SSunnySrivastava1984         if (it != vpdMap.end())
8149094d4f6SSunnySrivastava1984         {
8159094d4f6SSunnySrivastava1984             const auto& kwdListForRecord = systemRecKwdPair.second;
8169094d4f6SSunnySrivastava1984             for (const auto& keyword : kwdListForRecord)
8179094d4f6SSunnySrivastava1984             {
8189094d4f6SSunnySrivastava1984                 DbusPropertyMap& kwdValMap = it->second;
8199094d4f6SSunnySrivastava1984                 auto iterator = kwdValMap.find(keyword);
8209094d4f6SSunnySrivastava1984 
8219094d4f6SSunnySrivastava1984                 if (iterator != kwdValMap.end())
8229094d4f6SSunnySrivastava1984                 {
8239094d4f6SSunnySrivastava1984                     string& kwdValue = iterator->second;
8249094d4f6SSunnySrivastava1984 
8259094d4f6SSunnySrivastava1984                     // check bus data
8269094d4f6SSunnySrivastava1984                     const string& recordName = systemRecKwdPair.first;
8279094d4f6SSunnySrivastava1984                     const string& busValue = readBusProperty(
8289094d4f6SSunnySrivastava1984                         objectPath, ipzVpdInf + recordName, keyword);
8299094d4f6SSunnySrivastava1984 
8309094d4f6SSunnySrivastava1984                     if (busValue.find_first_not_of(' ') != string::npos)
8319094d4f6SSunnySrivastava1984                     {
8329094d4f6SSunnySrivastava1984                         if (kwdValue.find_first_not_of(' ') != string::npos)
8339094d4f6SSunnySrivastava1984                         {
8349094d4f6SSunnySrivastava1984                             // both the data are present, check for mismatch
8359094d4f6SSunnySrivastava1984                             if (busValue != kwdValue)
8369094d4f6SSunnySrivastava1984                             {
8379094d4f6SSunnySrivastava1984                                 string errMsg = "VPD data mismatch on cache "
8389094d4f6SSunnySrivastava1984                                                 "and hardware for record: ";
8399094d4f6SSunnySrivastava1984                                 errMsg += (*it).first;
8409094d4f6SSunnySrivastava1984                                 errMsg += " and keyword: ";
8419094d4f6SSunnySrivastava1984                                 errMsg += keyword;
8429094d4f6SSunnySrivastava1984 
8439094d4f6SSunnySrivastava1984                                 // data mismatch
8449094d4f6SSunnySrivastava1984                                 PelAdditionalData additionalData;
8459094d4f6SSunnySrivastava1984                                 additionalData.emplace("CALLOUT_INVENTORY_PATH",
8469094d4f6SSunnySrivastava1984                                                        objectPath);
8479094d4f6SSunnySrivastava1984 
8489094d4f6SSunnySrivastava1984                                 additionalData.emplace("DESCRIPTION", errMsg);
8499094d4f6SSunnySrivastava1984 
8500746eeebSSunny Srivastava                                 createPEL(additionalData, PelSeverity::WARNING,
8510746eeebSSunny Srivastava                                           errIntfForInvalidVPD);
8529094d4f6SSunnySrivastava1984                             }
8539094d4f6SSunnySrivastava1984                         }
8549094d4f6SSunnySrivastava1984                         else
8559094d4f6SSunnySrivastava1984                         {
8569094d4f6SSunnySrivastava1984                             // implies hardware data is blank
8579094d4f6SSunnySrivastava1984                             // update the map
8589094d4f6SSunnySrivastava1984                             Binary busData(busValue.begin(), busValue.end());
8599094d4f6SSunnySrivastava1984 
8609094d4f6SSunnySrivastava1984                             // update the map as well, so that cache data is not
86190a63b9bSSunny Srivastava                             // updated as blank while populating VPD map on Dbus
86290a63b9bSSunny Srivastava                             // in populateDBus Api
8639094d4f6SSunnySrivastava1984                             kwdValue = busValue;
86490a63b9bSSunny Srivastava                         }
8659094d4f6SSunnySrivastava1984                     }
8669094d4f6SSunnySrivastava1984                     else if (kwdValue.find_first_not_of(' ') == string::npos)
8679094d4f6SSunnySrivastava1984                     {
8689094d4f6SSunnySrivastava1984                         string errMsg = "VPD is blank on both cache and "
8699094d4f6SSunnySrivastava1984                                         "hardware for record: ";
8709094d4f6SSunnySrivastava1984                         errMsg += (*it).first;
8719094d4f6SSunnySrivastava1984                         errMsg += " and keyword: ";
8729094d4f6SSunnySrivastava1984                         errMsg += keyword;
8739094d4f6SSunnySrivastava1984                         errMsg += ". SSR need to update hardware VPD.";
8749094d4f6SSunnySrivastava1984 
8759094d4f6SSunnySrivastava1984                         // both the data are blanks, log PEL
8769094d4f6SSunnySrivastava1984                         PelAdditionalData additionalData;
8779094d4f6SSunnySrivastava1984                         additionalData.emplace("CALLOUT_INVENTORY_PATH",
8789094d4f6SSunnySrivastava1984                                                objectPath);
8799094d4f6SSunnySrivastava1984 
8809094d4f6SSunnySrivastava1984                         additionalData.emplace("DESCRIPTION", errMsg);
8819094d4f6SSunnySrivastava1984 
8829094d4f6SSunnySrivastava1984                         // log PEL TODO: Block IPL
8830746eeebSSunny Srivastava                         createPEL(additionalData, PelSeverity::ERROR,
8840746eeebSSunny Srivastava                                   errIntfForBlankSystemVPD);
8859094d4f6SSunnySrivastava1984                         continue;
8869094d4f6SSunnySrivastava1984                     }
8879094d4f6SSunnySrivastava1984                 }
8889094d4f6SSunnySrivastava1984             }
8899094d4f6SSunnySrivastava1984         }
8909094d4f6SSunnySrivastava1984     }
8919094d4f6SSunnySrivastava1984 }
8929094d4f6SSunnySrivastava1984 
8939094d4f6SSunnySrivastava1984 /**
8947ce68724Salpana07  * @brief This checks for is this FRU a processor
8957ce68724Salpana07  *        And if yes, then checks for is this primary
8967ce68724Salpana07  *
8977ce68724Salpana07  * @param[in] js- vpd json to get the information about this FRU
8987ce68724Salpana07  * @param[in] filePath- FRU vpd
8997ce68724Salpana07  *
9007ce68724Salpana07  * @return true/false
9017ce68724Salpana07  */
9027ce68724Salpana07 bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
9037ce68724Salpana07 {
9047ce68724Salpana07     bool isProcessor = false;
9057ce68724Salpana07     bool isPrimary = false;
9067ce68724Salpana07 
9077ce68724Salpana07     for (const auto& item : js["frus"][filePath])
9087ce68724Salpana07     {
9097ce68724Salpana07         if (item.find("extraInterfaces") != item.end())
9107ce68724Salpana07         {
9117ce68724Salpana07             for (const auto& eI : item["extraInterfaces"].items())
9127ce68724Salpana07             {
9137ce68724Salpana07                 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
9147ce68724Salpana07                 {
9157ce68724Salpana07                     isProcessor = true;
9167ce68724Salpana07                 }
9177ce68724Salpana07             }
9187ce68724Salpana07         }
9197ce68724Salpana07 
9207ce68724Salpana07         if (isProcessor)
9217ce68724Salpana07         {
9227ce68724Salpana07             string cpuType = item.value("cpuType", "");
9237ce68724Salpana07             if (cpuType == "primary")
9247ce68724Salpana07             {
9257ce68724Salpana07                 isPrimary = true;
9267ce68724Salpana07             }
9277ce68724Salpana07         }
9287ce68724Salpana07     }
9297ce68724Salpana07 
9307ce68724Salpana07     return (isProcessor && isPrimary);
9317ce68724Salpana07 }
9327ce68724Salpana07 
9337ce68724Salpana07 /**
9347ce68724Salpana07  * @brief This finds DIMM vpd in vpd json and enables them by binding the device
9357ce68724Salpana07  *        driver
9367ce68724Salpana07  * @param[in] js- vpd json to iterate through and take action if it is DIMM
9377ce68724Salpana07  */
9387ce68724Salpana07 void doEnableAllDimms(nlohmann::json& js)
9397ce68724Salpana07 {
9407ce68724Salpana07     // iterate over each fru
9417ce68724Salpana07     for (const auto& eachFru : js["frus"].items())
9427ce68724Salpana07     {
9437ce68724Salpana07         // skip the driver binding if eeprom already exists
9447ce68724Salpana07         if (fs::exists(eachFru.key()))
9457ce68724Salpana07         {
9467ce68724Salpana07             continue;
9477ce68724Salpana07         }
9487ce68724Salpana07 
9497ce68724Salpana07         for (const auto& eachInventory : eachFru.value())
9507ce68724Salpana07         {
9517ce68724Salpana07             if (eachInventory.find("extraInterfaces") != eachInventory.end())
9527ce68724Salpana07             {
9537ce68724Salpana07                 for (const auto& eI : eachInventory["extraInterfaces"].items())
9547ce68724Salpana07                 {
9557ce68724Salpana07                     if (eI.key().find("Inventory.Item.Dimm") != string::npos)
9567ce68724Salpana07                     {
9577ce68724Salpana07                         string dimmVpd = eachFru.key();
9587ce68724Salpana07                         // fetch it from
9597ce68724Salpana07                         // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
9607ce68724Salpana07 
9617ce68724Salpana07                         regex matchPatern("([0-9]+-[0-9]{4})");
9627ce68724Salpana07                         smatch matchFound;
9637ce68724Salpana07                         if (regex_search(dimmVpd, matchFound, matchPatern))
9647ce68724Salpana07                         {
9657ce68724Salpana07                             vector<string> i2cReg;
9667ce68724Salpana07                             boost::split(i2cReg, matchFound.str(0),
9677ce68724Salpana07                                          boost::is_any_of("-"));
9687ce68724Salpana07 
9697ce68724Salpana07                             // remove 0s from begining
9707ce68724Salpana07                             const regex pattern("^0+(?!$)");
9717ce68724Salpana07                             for (auto& i : i2cReg)
9727ce68724Salpana07                             {
9737ce68724Salpana07                                 i = regex_replace(i, pattern, "");
9747ce68724Salpana07                             }
9757ce68724Salpana07 
9767ce68724Salpana07                             if (i2cReg.size() == 2)
9777ce68724Salpana07                             {
9787ce68724Salpana07                                 // echo 24c32 0x50 >
9797ce68724Salpana07                                 // /sys/bus/i2c/devices/i2c-16/new_device
9807ce68724Salpana07                                 string cmnd = "echo 24c32 0x" + i2cReg[1] +
9817ce68724Salpana07                                               " > /sys/bus/i2c/devices/i2c-" +
9827ce68724Salpana07                                               i2cReg[0] + "/new_device";
9837ce68724Salpana07 
9847ce68724Salpana07                                 executeCmd(cmnd);
9857ce68724Salpana07                             }
9867ce68724Salpana07                         }
9877ce68724Salpana07                     }
9887ce68724Salpana07                 }
9897ce68724Salpana07             }
9907ce68724Salpana07         }
9917ce68724Salpana07     }
9927ce68724Salpana07 }
9937ce68724Salpana07 
9947ce68724Salpana07 /**
9958e140a1cSPriyangaRamasamy  * @brief Populate Dbus.
996abb87edaSPriyangaRamasamy  * This method invokes all the populateInterface functions
997abb87edaSPriyangaRamasamy  * and notifies PIM about dbus object.
9988e140a1cSPriyangaRamasamy  * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
9998e140a1cSPriyangaRamasamy  * input.
1000abb87edaSPriyangaRamasamy  * @param[in] js - Inventory json object
1001abb87edaSPriyangaRamasamy  * @param[in] filePath - Path of the vpd file
1002abb87edaSPriyangaRamasamy  * @param[in] preIntrStr - Interface string
1003abb87edaSPriyangaRamasamy  */
1004abb87edaSPriyangaRamasamy template <typename T>
10059094d4f6SSunnySrivastava1984 static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
1006abb87edaSPriyangaRamasamy {
1007abb87edaSPriyangaRamasamy     inventory::InterfaceMap interfaces;
1008abb87edaSPriyangaRamasamy     inventory::ObjectMap objects;
1009abb87edaSPriyangaRamasamy     inventory::PropertyMap prop;
10106aa5450eSShantappa Teekappanavar     string ccinFromVpd;
1011abb87edaSPriyangaRamasamy 
101250f60bf8SSantosh Puranik     bool isSystemVpd = (filePath == systemVpdFilePath);
101350f60bf8SSantosh Puranik     if constexpr (is_same<T, Parsed>::value)
101450f60bf8SSantosh Puranik     {
10156aa5450eSShantappa Teekappanavar         ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
10166aa5450eSShantappa Teekappanavar         transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
10176aa5450eSShantappa Teekappanavar                   ::toupper);
10186aa5450eSShantappa Teekappanavar 
101950f60bf8SSantosh Puranik         if (isSystemVpd)
102050f60bf8SSantosh Puranik         {
102150f60bf8SSantosh Puranik             std::vector<std::string> interfaces = {motherBoardInterface};
102250f60bf8SSantosh Puranik             // call mapper to check for object path creation
102350f60bf8SSantosh Puranik             MapperResponse subTree =
102450f60bf8SSantosh Puranik                 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
102550f60bf8SSantosh Puranik             string mboardPath =
102650f60bf8SSantosh Puranik                 js["frus"][filePath].at(0).value("inventoryPath", "");
102750f60bf8SSantosh Puranik 
102850f60bf8SSantosh Puranik             // Attempt system VPD restore if we have a motherboard
102950f60bf8SSantosh Puranik             // object in the inventory.
103050f60bf8SSantosh Puranik             if ((subTree.size() != 0) &&
103150f60bf8SSantosh Puranik                 (subTree.find(pimPath + mboardPath) != subTree.end()))
103250f60bf8SSantosh Puranik             {
10333c24414fSSunny Srivastava                 restoreSystemVPD(vpdMap, mboardPath);
103450f60bf8SSantosh Puranik             }
103550f60bf8SSantosh Puranik             else
103650f60bf8SSantosh Puranik             {
103750f60bf8SSantosh Puranik                 log<level::ERR>("No object path found");
103850f60bf8SSantosh Puranik             }
103950f60bf8SSantosh Puranik         }
10407ce68724Salpana07         else
10417ce68724Salpana07         {
10427ce68724Salpana07             // check if it is processor vpd.
10437ce68724Salpana07             auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
10447ce68724Salpana07 
10457ce68724Salpana07             if (isPrimaryCpu)
10467ce68724Salpana07             {
10477ce68724Salpana07                 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
10487ce68724Salpana07 
10497ce68724Salpana07                 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
10507ce68724Salpana07 
10517ce68724Salpana07                 if (chipVersion >= 2)
10527ce68724Salpana07                 {
10537ce68724Salpana07                     doEnableAllDimms(js);
10547ce68724Salpana07                 }
10557ce68724Salpana07             }
10567ce68724Salpana07         }
105750f60bf8SSantosh Puranik     }
105850f60bf8SSantosh Puranik 
105932c687f5SPriyanga Ramasamy     if (isSystemVpd)
106032c687f5SPriyanga Ramasamy     {
106132c687f5SPriyanga Ramasamy         string systemJsonName{};
106232c687f5SPriyanga Ramasamy         if constexpr (is_same<T, Parsed>::value)
106332c687f5SPriyanga Ramasamy         {
106432c687f5SPriyanga Ramasamy             // pick the right system json
106532c687f5SPriyanga Ramasamy             systemJsonName = getSystemsJson(vpdMap);
106632c687f5SPriyanga Ramasamy         }
106732c687f5SPriyanga Ramasamy 
106832c687f5SPriyanga Ramasamy         fs::path target = systemJsonName;
106932c687f5SPriyanga Ramasamy         fs::path link = INVENTORY_JSON_SYM_LINK;
107032c687f5SPriyanga Ramasamy 
107132c687f5SPriyanga Ramasamy         // Create the directory for hosting the symlink
107232c687f5SPriyanga Ramasamy         fs::create_directories(VPD_FILES_PATH);
107332c687f5SPriyanga Ramasamy         // unlink the symlink previously created (if any)
107432c687f5SPriyanga Ramasamy         remove(INVENTORY_JSON_SYM_LINK);
107532c687f5SPriyanga Ramasamy         // create a new symlink based on the system
107632c687f5SPriyanga Ramasamy         fs::create_symlink(target, link);
107732c687f5SPriyanga Ramasamy 
107832c687f5SPriyanga Ramasamy         // Reloading the json
107932c687f5SPriyanga Ramasamy         ifstream inventoryJson(link);
108032c687f5SPriyanga Ramasamy         js = json::parse(inventoryJson);
108132c687f5SPriyanga Ramasamy         inventoryJson.close();
108232c687f5SPriyanga Ramasamy     }
108332c687f5SPriyanga Ramasamy 
1084abb87edaSPriyangaRamasamy     for (const auto& item : js["frus"][filePath])
1085abb87edaSPriyangaRamasamy     {
1086abb87edaSPriyangaRamasamy         const auto& objectPath = item["inventoryPath"];
1087abb87edaSPriyangaRamasamy         sdbusplus::message::object_path object(objectPath);
10889094d4f6SSunnySrivastava1984 
10896aa5450eSShantappa Teekappanavar         vector<string> ccinList;
10906aa5450eSShantappa Teekappanavar         if (item.find("ccin") != item.end())
10916aa5450eSShantappa Teekappanavar         {
10926aa5450eSShantappa Teekappanavar             for (const auto& cc : item["ccin"])
10936aa5450eSShantappa Teekappanavar             {
10946aa5450eSShantappa Teekappanavar                 string ccin = cc;
10956aa5450eSShantappa Teekappanavar                 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
10966aa5450eSShantappa Teekappanavar                 ccinList.push_back(ccin);
10976aa5450eSShantappa Teekappanavar             }
10986aa5450eSShantappa Teekappanavar         }
10996aa5450eSShantappa Teekappanavar 
11006aa5450eSShantappa Teekappanavar         if (!ccinFromVpd.empty() && !ccinList.empty() &&
11016aa5450eSShantappa Teekappanavar             (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
11026aa5450eSShantappa Teekappanavar              ccinList.end()))
11036aa5450eSShantappa Teekappanavar         {
11046aa5450eSShantappa Teekappanavar             continue;
11056aa5450eSShantappa Teekappanavar         }
11066aa5450eSShantappa Teekappanavar 
1107e3fed701SPriyanga Ramasamy         if ((isSystemVpd) || (item.value("noprime", false)))
1108d3a379a6SSantosh Puranik         {
1109e3fed701SPriyanga Ramasamy 
1110e3fed701SPriyanga Ramasamy             // Populate one time properties for the system VPD and its sub-frus
1111e3fed701SPriyanga Ramasamy             // and for other non-primeable frus.
1112d3a379a6SSantosh Puranik             // For the remaining FRUs, this will get handled as a part of
1113d3a379a6SSantosh Puranik             // priming the inventory.
1114d3a379a6SSantosh Puranik             setOneTimeProperties(objectPath, interfaces);
1115d3a379a6SSantosh Puranik         }
1116d3a379a6SSantosh Puranik 
1117abb87edaSPriyangaRamasamy         // Populate the VPD keywords and the common interfaces only if we
1118abb87edaSPriyangaRamasamy         // are asked to inherit that data from the VPD, else only add the
1119abb87edaSPriyangaRamasamy         // extraInterfaces.
1120abb87edaSPriyangaRamasamy         if (item.value("inherit", true))
1121abb87edaSPriyangaRamasamy         {
112258e22145SAlpana Kumari             if constexpr (is_same<T, Parsed>::value)
1123abb87edaSPriyangaRamasamy             {
11248e140a1cSPriyangaRamasamy                 // Each record in the VPD becomes an interface and all
11258e140a1cSPriyangaRamasamy                 // keyword within the record are properties under that
11268e140a1cSPriyangaRamasamy                 // interface.
1127abb87edaSPriyangaRamasamy                 for (const auto& record : vpdMap)
1128abb87edaSPriyangaRamasamy                 {
1129abb87edaSPriyangaRamasamy                     populateFruSpecificInterfaces(
1130e12b181bSSunnySrivastava1984                         record.second, ipzVpdInf + record.first, interfaces);
1131abb87edaSPriyangaRamasamy                 }
1132abb87edaSPriyangaRamasamy             }
113358e22145SAlpana Kumari             else if constexpr (is_same<T, KeywordVpdMap>::value)
1134abb87edaSPriyangaRamasamy             {
1135e12b181bSSunnySrivastava1984                 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
1136abb87edaSPriyangaRamasamy             }
113788edeb6fSSantosh Puranik             if (js.find("commonInterfaces") != js.end())
113888edeb6fSSantosh Puranik             {
113988edeb6fSSantosh Puranik                 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
114088edeb6fSSantosh Puranik                                    isSystemVpd);
114188edeb6fSSantosh Puranik             }
1142abb87edaSPriyangaRamasamy         }
11430859eb65SSantosh Puranik         else
11440859eb65SSantosh Puranik         {
11450859eb65SSantosh Puranik             // Check if we have been asked to inherit specific record(s)
114658e22145SAlpana Kumari             if constexpr (is_same<T, Parsed>::value)
11470859eb65SSantosh Puranik             {
11480859eb65SSantosh Puranik                 if (item.find("copyRecords") != item.end())
11490859eb65SSantosh Puranik                 {
11500859eb65SSantosh Puranik                     for (const auto& record : item["copyRecords"])
11510859eb65SSantosh Puranik                     {
11520859eb65SSantosh Puranik                         const string& recordName = record;
11530859eb65SSantosh Puranik                         if (vpdMap.find(recordName) != vpdMap.end())
11540859eb65SSantosh Puranik                         {
11550859eb65SSantosh Puranik                             populateFruSpecificInterfaces(
1156e12b181bSSunnySrivastava1984                                 vpdMap.at(recordName), ipzVpdInf + recordName,
11570859eb65SSantosh Puranik                                 interfaces);
11580859eb65SSantosh Puranik                         }
11590859eb65SSantosh Puranik                     }
11600859eb65SSantosh Puranik                 }
11610859eb65SSantosh Puranik             }
11620859eb65SSantosh Puranik         }
1163abb87edaSPriyangaRamasamy         // Populate interfaces and properties that are common to every FRU
11648e140a1cSPriyangaRamasamy         // and additional interface that might be defined on a per-FRU
11658e140a1cSPriyangaRamasamy         // basis.
1166abb87edaSPriyangaRamasamy         if (item.find("extraInterfaces") != item.end())
1167abb87edaSPriyangaRamasamy         {
116888edeb6fSSantosh Puranik             populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
116988edeb6fSSantosh Puranik                                isSystemVpd);
1170abb87edaSPriyangaRamasamy         }
1171aa8a893eSPriyanga Ramasamy         inventory::PropertyMap presProp;
1172aa8a893eSPriyanga Ramasamy         presProp.emplace("Present", true);
1173aa8a893eSPriyanga Ramasamy         insertOrMerge(interfaces, invItemIntf, move(presProp));
1174aa8a893eSPriyanga Ramasamy 
1175abb87edaSPriyangaRamasamy         objects.emplace(move(object), move(interfaces));
1176abb87edaSPriyangaRamasamy     }
1177abb87edaSPriyangaRamasamy 
11788e140a1cSPriyangaRamasamy     if (isSystemVpd)
11798e140a1cSPriyangaRamasamy     {
11808e140a1cSPriyangaRamasamy         inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
11818e140a1cSPriyangaRamasamy         objects.insert(primeObject.begin(), primeObject.end());
118265b83601SAlpana Kumari 
1183f05effdbSAlpana Kumari         // set the U-boot environment variable for device-tree
1184f05effdbSAlpana Kumari         if constexpr (is_same<T, Parsed>::value)
1185f05effdbSAlpana Kumari         {
1186e5f177a5SSantosh Puranik             setDevTreeEnv(fs::path(getSystemsJson(vpdMap)).filename());
1187f05effdbSAlpana Kumari         }
11888e140a1cSPriyangaRamasamy     }
11898e140a1cSPriyangaRamasamy 
1190abb87edaSPriyangaRamasamy     // Notify PIM
11916c71c9dcSSunny Srivastava     common::utility::callPIM(move(objects));
1192abb87edaSPriyangaRamasamy }
1193abb87edaSPriyangaRamasamy 
1194abb87edaSPriyangaRamasamy int main(int argc, char** argv)
1195abb87edaSPriyangaRamasamy {
1196abb87edaSPriyangaRamasamy     int rc = 0;
1197a20be8ecSSunnySrivastava1984     json js{};
1198c2fe40f8SPriyangaRamasamy     Binary vpdVector{};
1199c2fe40f8SPriyangaRamasamy     string file{};
1200a20be8ecSSunnySrivastava1984     // map to hold additional data in case of logging pel
1201a20be8ecSSunnySrivastava1984     PelAdditionalData additionalData{};
1202a20be8ecSSunnySrivastava1984 
1203a20be8ecSSunnySrivastava1984     // this is needed to hold base fru inventory path in case there is ECC or
1204a20be8ecSSunnySrivastava1984     // vpd exception while parsing the file
1205a20be8ecSSunnySrivastava1984     std::string baseFruInventoryPath = {};
1206abb87edaSPriyangaRamasamy 
12070746eeebSSunny Srivastava     // severity for PEL
12080746eeebSSunny Srivastava     PelSeverity pelSeverity = PelSeverity::WARNING;
12090746eeebSSunny Srivastava 
1210abb87edaSPriyangaRamasamy     try
1211abb87edaSPriyangaRamasamy     {
1212abb87edaSPriyangaRamasamy         App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1213abb87edaSPriyangaRamasamy                 "in DBUS"};
1214abb87edaSPriyangaRamasamy 
1215abb87edaSPriyangaRamasamy         app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
12162f793048SAlpana Kumari             ->required();
1217abb87edaSPriyangaRamasamy 
1218abb87edaSPriyangaRamasamy         CLI11_PARSE(app, argc, argv);
1219abb87edaSPriyangaRamasamy 
12200746eeebSSunny Srivastava         // PEL severity should be ERROR in case of any system VPD failure
12210746eeebSSunny Srivastava         if (file == systemVpdFilePath)
12220746eeebSSunny Srivastava         {
12230746eeebSSunny Srivastava             pelSeverity = PelSeverity::ERROR;
12240746eeebSSunny Srivastava         }
12250746eeebSSunny Srivastava 
12260246a4d7SSantosh Puranik         auto jsonToParse = INVENTORY_JSON_DEFAULT;
12270246a4d7SSantosh Puranik 
12280246a4d7SSantosh Puranik         // If the symlink exists, it means it has been setup for us, switch the
12290246a4d7SSantosh Puranik         // path
12300246a4d7SSantosh Puranik         if (fs::exists(INVENTORY_JSON_SYM_LINK))
12310246a4d7SSantosh Puranik         {
12320246a4d7SSantosh Puranik             jsonToParse = INVENTORY_JSON_SYM_LINK;
12330246a4d7SSantosh Puranik         }
12340246a4d7SSantosh Puranik 
1235abb87edaSPriyangaRamasamy         // Make sure that the file path we get is for a supported EEPROM
12360246a4d7SSantosh Puranik         ifstream inventoryJson(jsonToParse);
1237a20be8ecSSunnySrivastava1984         if (!inventoryJson)
1238a20be8ecSSunnySrivastava1984         {
12390746eeebSSunny Srivastava             throw(VpdJsonException("Failed to access Json path", jsonToParse));
1240a20be8ecSSunnySrivastava1984         }
1241a20be8ecSSunnySrivastava1984 
1242a20be8ecSSunnySrivastava1984         try
1243a20be8ecSSunnySrivastava1984         {
1244a20be8ecSSunnySrivastava1984             js = json::parse(inventoryJson);
1245a20be8ecSSunnySrivastava1984         }
12468e15b93aSPatrick Williams         catch (const json::parse_error& ex)
1247a20be8ecSSunnySrivastava1984         {
12480746eeebSSunny Srivastava             throw(VpdJsonException("Json parsing failed", jsonToParse));
1249a20be8ecSSunnySrivastava1984         }
1250abb87edaSPriyangaRamasamy 
125112e24ff3SSantosh Puranik         // Do we have the mandatory "frus" section?
125212e24ff3SSantosh Puranik         if (js.find("frus") == js.end())
125312e24ff3SSantosh Puranik         {
125412e24ff3SSantosh Puranik             throw(VpdJsonException("FRUs section not found in JSON",
125512e24ff3SSantosh Puranik                                    jsonToParse));
125612e24ff3SSantosh Puranik         }
125712e24ff3SSantosh Puranik 
1258647868edSPriyangaRamasamy         // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1259647868edSPriyangaRamasamy         if (file.find("/ahb:apb") != string::npos)
1260647868edSPriyangaRamasamy         {
1261647868edSPriyangaRamasamy             // Translate udev path to a generic /sys/bus/.. file path.
1262647868edSPriyangaRamasamy             udevToGenericPath(file);
126312e24ff3SSantosh Puranik 
126412e24ff3SSantosh Puranik             if ((js["frus"].find(file) != js["frus"].end()) &&
126550f60bf8SSantosh Puranik                 (file == systemVpdFilePath))
1266647868edSPriyangaRamasamy             {
12673c24414fSSunny Srivastava                 // We need manager service active to process restoring of
12683c24414fSSunny Srivastava                 // system VPD on hardware. So in case any system restore is
12693c24414fSSunny Srivastava                 // required, update hardware in the second trigger of parser
12703c24414fSSunny Srivastava                 // code for system vpd file path.
12713c24414fSSunny Srivastava 
12723c24414fSSunny Srivastava                 std::vector<std::string> interfaces{motherBoardInterface};
12733c24414fSSunny Srivastava 
12743c24414fSSunny Srivastava                 // call mapper to check for object path creation
12753c24414fSSunny Srivastava                 MapperResponse subTree =
12763c24414fSSunny Srivastava                     getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
12773c24414fSSunny Srivastava                 string mboardPath =
12783c24414fSSunny Srivastava                     js["frus"][file].at(0).value("inventoryPath", "");
12793c24414fSSunny Srivastava 
12803c24414fSSunny Srivastava                 // Attempt system VPD restore if we have a motherboard
12813c24414fSSunny Srivastava                 // object in the inventory.
12823c24414fSSunny Srivastava                 if ((subTree.size() != 0) &&
12833c24414fSSunny Srivastava                     (subTree.find(pimPath + mboardPath) != subTree.end()))
12843c24414fSSunny Srivastava                 {
12853c24414fSSunny Srivastava                     vpdVector = getVpdDataInVector(js, file);
12863c24414fSSunny Srivastava                     ParserInterface* parser =
12873c24414fSSunny Srivastava                         ParserFactory::getParser(vpdVector);
12883c24414fSSunny Srivastava                     variant<KeywordVpdMap, Store> parseResult;
12893c24414fSSunny Srivastava                     parseResult = parser->parse();
12903c24414fSSunny Srivastava 
12913c24414fSSunny Srivastava                     if (auto pVal = get_if<Store>(&parseResult))
12923c24414fSSunny Srivastava                     {
12933c24414fSSunny Srivastava                         // map to hold all the keywords whose value is blank and
12943c24414fSSunny Srivastava                         // needs to be updated at standby.
12953c24414fSSunny Srivastava                         vector<RestoredEeproms> blankSystemVpdProperties{};
12963c24414fSSunny Srivastava                         getListOfBlankSystemVpd(pVal->getVpdMap(), mboardPath,
12973c24414fSSunny Srivastava                                                 blankSystemVpdProperties);
12983c24414fSSunny Srivastava 
12993c24414fSSunny Srivastava                         // if system VPD restore is required, update the
13003c24414fSSunny Srivastava                         // EEPROM
13013c24414fSSunny Srivastava                         for (const auto& item : blankSystemVpdProperties)
13023c24414fSSunny Srivastava                         {
13033c24414fSSunny Srivastava                             updateHardware(get<0>(item), get<1>(item),
13043c24414fSSunny Srivastava                                            get<2>(item), get<3>(item));
13053c24414fSSunny Srivastava                         }
13063c24414fSSunny Srivastava                     }
13073c24414fSSunny Srivastava                     else
13083c24414fSSunny Srivastava                     {
13093c24414fSSunny Srivastava                         std::cout << "Not a valid format to restore system VPD"
13103c24414fSSunny Srivastava                                   << std::endl;
13113c24414fSSunny Srivastava                     }
13123c24414fSSunny Srivastava                     // release the parser object
13133c24414fSSunny Srivastava                     ParserFactory::freeParser(parser);
13143c24414fSSunny Srivastava                 }
13153c24414fSSunny Srivastava                 else
13163c24414fSSunny Srivastava                 {
13173c24414fSSunny Srivastava                     log<level::ERR>("No object path found");
13183c24414fSSunny Srivastava                 }
1319647868edSPriyangaRamasamy                 return 0;
1320647868edSPriyangaRamasamy             }
1321647868edSPriyangaRamasamy         }
1322647868edSPriyangaRamasamy 
1323647868edSPriyangaRamasamy         if (file.empty())
1324647868edSPriyangaRamasamy         {
1325647868edSPriyangaRamasamy             cerr << "The EEPROM path <" << file << "> is not valid.";
1326647868edSPriyangaRamasamy             return 0;
1327647868edSPriyangaRamasamy         }
132812e24ff3SSantosh Puranik         if (js["frus"].find(file) == js["frus"].end())
1329abb87edaSPriyangaRamasamy         {
133088edeb6fSSantosh Puranik             return 0;
1331abb87edaSPriyangaRamasamy         }
1332abb87edaSPriyangaRamasamy 
13332f793048SAlpana Kumari         if (!fs::exists(file))
13342f793048SAlpana Kumari         {
13352f793048SAlpana Kumari             cout << "Device path: " << file
13362f793048SAlpana Kumari                  << " does not exist. Spurious udev event? Exiting." << endl;
13372f793048SAlpana Kumari             return 0;
13382f793048SAlpana Kumari         }
13392f793048SAlpana Kumari 
1340a20be8ecSSunnySrivastava1984         baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
13418589375fSSantosh Puranik         // Check if we can read the VPD file based on the power state
134227a5e95aSSantosh Puranik         // We skip reading VPD when the power is ON in two scenarios:
134327a5e95aSSantosh Puranik         // 1) The eeprom we are trying to read is that of the system VPD
134427a5e95aSSantosh Puranik         // 2) The JSON tells us that the FRU EEPROM cannot be read when
134527a5e95aSSantosh Puranik         // we are powered ON.
134627a5e95aSSantosh Puranik         if (js["frus"][file].at(0).value("powerOffOnly", false) ||
134727a5e95aSSantosh Puranik             (file == systemVpdFilePath))
13488589375fSSantosh Puranik         {
13498589375fSSantosh Puranik             if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
13508589375fSSantosh Puranik                 getPowerState())
13518589375fSSantosh Puranik             {
13528589375fSSantosh Puranik                 cout << "This VPD cannot be read when power is ON" << endl;
13538589375fSSantosh Puranik                 return 0;
13548589375fSSantosh Puranik             }
13558589375fSSantosh Puranik         }
1356a20be8ecSSunnySrivastava1984 
13572f793048SAlpana Kumari         try
13582f793048SAlpana Kumari         {
1359c2fe40f8SPriyangaRamasamy             vpdVector = getVpdDataInVector(js, file);
136033c61c2dSPriyangaRamasamy             ParserInterface* parser = ParserFactory::getParser(vpdVector);
13612f793048SAlpana Kumari             variant<KeywordVpdMap, Store> parseResult;
13622f793048SAlpana Kumari             parseResult = parser->parse();
13639a19554cSSunnySrivastava1984 
1364e12b181bSSunnySrivastava1984             if (auto pVal = get_if<Store>(&parseResult))
1365abb87edaSPriyangaRamasamy             {
1366e12b181bSSunnySrivastava1984                 populateDbus(pVal->getVpdMap(), js, file);
1367e12b181bSSunnySrivastava1984             }
1368e12b181bSSunnySrivastava1984             else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1369abb87edaSPriyangaRamasamy             {
1370e12b181bSSunnySrivastava1984                 populateDbus(*pVal, js, file);
1371abb87edaSPriyangaRamasamy             }
1372abb87edaSPriyangaRamasamy 
1373e12b181bSSunnySrivastava1984             // release the parser object
1374e12b181bSSunnySrivastava1984             ParserFactory::freeParser(parser);
1375abb87edaSPriyangaRamasamy         }
13768e15b93aSPatrick Williams         catch (const exception& e)
13772f793048SAlpana Kumari         {
1378*735dee9bSAlpana Kumari             executePostFailAction(js, file);
1379a504c3eeSPriyangaRamasamy             throw;
13802f793048SAlpana Kumari         }
13812f793048SAlpana Kumari     }
1382a20be8ecSSunnySrivastava1984     catch (const VpdJsonException& ex)
1383a20be8ecSSunnySrivastava1984     {
1384a20be8ecSSunnySrivastava1984         additionalData.emplace("JSON_PATH", ex.getJsonPath());
1385a20be8ecSSunnySrivastava1984         additionalData.emplace("DESCRIPTION", ex.what());
13860746eeebSSunny Srivastava         createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
1387a20be8ecSSunnySrivastava1984 
1388a20be8ecSSunnySrivastava1984         cerr << ex.what() << "\n";
1389a20be8ecSSunnySrivastava1984         rc = -1;
1390a20be8ecSSunnySrivastava1984     }
1391a20be8ecSSunnySrivastava1984     catch (const VpdEccException& ex)
1392a20be8ecSSunnySrivastava1984     {
1393a20be8ecSSunnySrivastava1984         additionalData.emplace("DESCRIPTION", "ECC check failed");
1394a20be8ecSSunnySrivastava1984         additionalData.emplace("CALLOUT_INVENTORY_PATH",
1395a20be8ecSSunnySrivastava1984                                INVENTORY_PATH + baseFruInventoryPath);
13960746eeebSSunny Srivastava         createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
1397c2fe40f8SPriyangaRamasamy         dumpBadVpd(file, vpdVector);
1398a20be8ecSSunnySrivastava1984         cerr << ex.what() << "\n";
1399a20be8ecSSunnySrivastava1984         rc = -1;
1400a20be8ecSSunnySrivastava1984     }
1401a20be8ecSSunnySrivastava1984     catch (const VpdDataException& ex)
1402a20be8ecSSunnySrivastava1984     {
14035cb3b1f3Salpana07         if (isThisPcieOnPass1planar(js, file))
14045cb3b1f3Salpana07         {
14055cb3b1f3Salpana07             cout << "Pcie_device  [" << file
14065cb3b1f3Salpana07                  << "]'s VPD is not valid on PASS1 planar.Ignoring.\n";
14075cb3b1f3Salpana07             rc = 0;
14085cb3b1f3Salpana07         }
14095cb3b1f3Salpana07         else
14105cb3b1f3Salpana07         {
14115cb3b1f3Salpana07             string errorMsg =
14125cb3b1f3Salpana07                 "VPD file is either empty or invalid. Parser failed for [";
14135cb3b1f3Salpana07             errorMsg += file;
14145cb3b1f3Salpana07             errorMsg += "], with error = " + std::string(ex.what());
14155cb3b1f3Salpana07 
14165cb3b1f3Salpana07             additionalData.emplace("DESCRIPTION", errorMsg);
1417a20be8ecSSunnySrivastava1984             additionalData.emplace("CALLOUT_INVENTORY_PATH",
1418a20be8ecSSunnySrivastava1984                                    INVENTORY_PATH + baseFruInventoryPath);
14190746eeebSSunny Srivastava             createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
14205cb3b1f3Salpana07 
1421a20be8ecSSunnySrivastava1984             rc = -1;
1422a20be8ecSSunnySrivastava1984         }
14235cb3b1f3Salpana07     }
14248e15b93aSPatrick Williams     catch (const exception& e)
1425abb87edaSPriyangaRamasamy     {
1426c2fe40f8SPriyangaRamasamy         dumpBadVpd(file, vpdVector);
1427abb87edaSPriyangaRamasamy         cerr << e.what() << "\n";
1428abb87edaSPriyangaRamasamy         rc = -1;
1429abb87edaSPriyangaRamasamy     }
1430abb87edaSPriyangaRamasamy 
1431abb87edaSPriyangaRamasamy     return rc;
1432abb87edaSPriyangaRamasamy }