blob: d5d280c52088e46658f06f2c9792807ce10c1d4c [file] [log] [blame]
SunnySrivastava198443306542020-04-01 02:50:20 -05001#include "config.h"
2
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05003#include "ibm_vpd_utils.hpp"
Patrick Venturec83c4dc2018-11-01 16:29:18 -07004
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05005#include "common_utility.hpp"
SunnySrivastava1984d076da82020-03-05 05:33:35 -06006#include "defines.hpp"
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +05307#include "vpd_exceptions.hpp"
SunnySrivastava1984d076da82020-03-05 05:33:35 -06008
Alpana Kumari6bd095f2022-02-23 10:20:20 -06009#include <boost/algorithm/string.hpp>
Alpana Kumari735dee92022-03-25 01:24:40 -050010#include <gpiod.hpp>
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -050011#include <nlohmann/json.hpp>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050012#include <phosphor-logging/elog-errors.hpp>
Patrick Venturec83c4dc2018-11-01 16:29:18 -070013#include <phosphor-logging/log.hpp>
14#include <sdbusplus/server.hpp>
Patrick Williamsc78d8872023-05-10 07:50:56 -050015#include <xyz/openbmc_project/Common/error.hpp>
16
17#include <filesystem>
18#include <fstream>
19#include <iomanip>
20#include <regex>
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053021#include <sstream>
22#include <vector>
Deepak Kodihalli76794492017-02-16 23:48:18 -060023
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053024using json = nlohmann::json;
25
Deepak Kodihalli76794492017-02-16 23:48:18 -060026namespace openpower
27{
28namespace vpd
29{
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050030using namespace openpower::vpd::constants;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050031using namespace inventory;
32using namespace phosphor::logging;
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050033using namespace sdbusplus::xyz::openbmc_project::Common::Error;
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053034using namespace record;
35using namespace openpower::vpd::exceptions;
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -050036using namespace common::utility;
Sunny Srivastava0746eee2021-03-22 13:36:54 -050037using Severity = openpower::vpd::constants::PelSeverity;
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -060038namespace fs = std::filesystem;
Sunny Srivastava0746eee2021-03-22 13:36:54 -050039
40// mapping of severity enum to severity interface
41static std::unordered_map<Severity, std::string> sevMap = {
42 {Severity::INFORMATIONAL,
43 "xyz.openbmc_project.Logging.Entry.Level.Informational"},
44 {Severity::DEBUG, "xyz.openbmc_project.Logging.Entry.Level.Debug"},
45 {Severity::NOTICE, "xyz.openbmc_project.Logging.Entry.Level.Notice"},
46 {Severity::WARNING, "xyz.openbmc_project.Logging.Entry.Level.Warning"},
47 {Severity::CRITICAL, "xyz.openbmc_project.Logging.Entry.Level.Critical"},
48 {Severity::EMERGENCY, "xyz.openbmc_project.Logging.Entry.Level.Emergency"},
49 {Severity::ERROR, "xyz.openbmc_project.Logging.Entry.Level.Error"},
50 {Severity::ALERT, "xyz.openbmc_project.Logging.Entry.Level.Alert"}};
51
Deepak Kodihalli76794492017-02-16 23:48:18 -060052namespace inventory
53{
54
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050055MapperResponse
56 getObjectSubtreeForInterfaces(const std::string& root, const int32_t depth,
57 const std::vector<std::string>& interfaces)
58{
59 auto bus = sdbusplus::bus::new_default();
60 auto mapperCall = bus.new_method_call(mapperDestination, mapperObjectPath,
61 mapperInterface, "GetSubTree");
62 mapperCall.append(root);
63 mapperCall.append(depth);
64 mapperCall.append(interfaces);
65
66 MapperResponse result = {};
67
68 try
69 {
70 auto response = bus.call(mapperCall);
71
72 response.read(result);
73 }
Patrick Williams2eb01762022-07-22 19:26:56 -050074 catch (const sdbusplus::exception_t& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050075 {
76 log<level::ERR>("Error in mapper GetSubTree",
77 entry("ERROR=%s", e.what()));
78 }
79
80 return result;
81}
82
Deepak Kodihalli76794492017-02-16 23:48:18 -060083} // namespace inventory
84
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060085LE2ByteData readUInt16LE(Binary::const_iterator iterator)
86{
87 LE2ByteData lowByte = *iterator;
88 LE2ByteData highByte = *(iterator + 1);
89 lowByte |= (highByte << 8);
90 return lowByte;
91}
92
SunnySrivastava1984d076da82020-03-05 05:33:35 -060093/** @brief Encodes a keyword for D-Bus.
94 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -050095std::string encodeKeyword(const std::string& kw, const std::string& encoding)
SunnySrivastava1984d076da82020-03-05 05:33:35 -060096{
97 if (encoding == "MAC")
98 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -050099 std::string res{};
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600100 size_t first = kw[0];
101 res += toHex(first >> 4);
102 res += toHex(first & 0x0f);
103 for (size_t i = 1; i < kw.size(); ++i)
104 {
105 res += ":";
106 res += toHex(kw[i] >> 4);
107 res += toHex(kw[i] & 0x0f);
108 }
109 return res;
110 }
111 else if (encoding == "DATE")
112 {
113 // Date, represent as
114 // <year>-<month>-<day> <hour>:<min>
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500115 std::string res{};
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600116 static constexpr uint8_t skipPrefix = 3;
117
118 auto strItr = kw.begin();
119 advance(strItr, skipPrefix);
120 for_each(strItr, kw.end(), [&res](size_t c) { res += c; });
121
122 res.insert(BD_YEAR_END, 1, '-');
123 res.insert(BD_MONTH_END, 1, '-');
124 res.insert(BD_DAY_END, 1, ' ');
125 res.insert(BD_HOUR_END, 1, ':');
126
127 return res;
128 }
129 else // default to string encoding
130 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500131 return std::string(kw.begin(), kw.end());
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600132 }
133}
SunnySrivastava198443306542020-04-01 02:50:20 -0500134
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500135std::string readBusProperty(const std::string& obj, const std::string& inf,
136 const std::string& prop)
SunnySrivastava198443306542020-04-01 02:50:20 -0500137{
138 std::string propVal{};
139 std::string object = INVENTORY_PATH + obj;
140 auto bus = sdbusplus::bus::new_default();
141 auto properties = bus.new_method_call(
142 "xyz.openbmc_project.Inventory.Manager", object.c_str(),
143 "org.freedesktop.DBus.Properties", "Get");
144 properties.append(inf);
145 properties.append(prop);
146 auto result = bus.call(properties);
147 if (!result.is_method_error())
148 {
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500149 inventory::Value val;
SunnySrivastava198443306542020-04-01 02:50:20 -0500150 result.read(val);
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500151 if (auto pVal = std::get_if<Binary>(&val))
SunnySrivastava198443306542020-04-01 02:50:20 -0500152 {
153 propVal.assign(reinterpret_cast<const char*>(pVal->data()),
154 pVal->size());
155 }
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500156 else if (auto pVal = std::get_if<std::string>(&val))
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500157 {
158 propVal.assign(pVal->data(), pVal->size());
159 }
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500160 else if (auto pVal = get_if<bool>(&val))
161 {
162 if (*pVal == false)
163 {
164 propVal = "false";
165 }
166 else
167 {
168 propVal = "true";
169 }
170 }
SunnySrivastava198443306542020-04-01 02:50:20 -0500171 }
172 return propVal;
173}
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500174
175void createPEL(const std::map<std::string, std::string>& additionalData,
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500176 const Severity& sev, const std::string& errIntf, sd_bus* sdBus)
177{
178 // This pointer will be NULL in case the call is made from ibm-read-vpd. In
179 // that case a sync call will do.
180 if (sdBus == nullptr)
181 {
182 createSyncPEL(additionalData, sev, errIntf);
183 }
184 else
185 {
186 std::string errDescription{};
187 auto pos = additionalData.find("DESCRIPTION");
188 if (pos != additionalData.end())
189 {
190 errDescription = pos->second;
191 }
192 else
193 {
194 errDescription = "Description field missing in additional data";
195 }
196
197 std::string pelSeverity =
198 "xyz.openbmc_project.Logging.Entry.Level.Error";
199 auto itr = sevMap.find(sev);
200 if (itr != sevMap.end())
201 {
202 pelSeverity = itr->second;
203 }
204
205 // Implies this is a call from Manager. Hence we need to make an async
206 // call to avoid deadlock with Phosphor-logging.
207 auto rc = sd_bus_call_method_async(
208 sdBus, NULL, loggerService, loggerObjectPath, loggerCreateInterface,
209 "Create", NULL, NULL, "ssa{ss}", errIntf.c_str(),
210 pelSeverity.c_str(), 1, "DESCRIPTION", errDescription.c_str());
211
212 if (rc < 0)
213 {
214 log<level::ERR>("Error calling sd_bus_call_method_async",
215 entry("RC=%d", rc), entry("MSG=%s", strerror(-rc)));
216 }
217 }
218}
219
220void createSyncPEL(const std::map<std::string, std::string>& additionalData,
221 const Severity& sev, const std::string& errIntf)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500222{
223 try
224 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500225 std::string pelSeverity =
226 "xyz.openbmc_project.Logging.Entry.Level.Error";
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500227 auto bus = sdbusplus::bus::new_default();
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500228 auto service = getService(bus, loggerObjectPath, loggerCreateInterface);
229 auto method = bus.new_method_call(service.c_str(), loggerObjectPath,
230 loggerCreateInterface, "Create");
231
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500232 auto itr = sevMap.find(sev);
233 if (itr != sevMap.end())
234 {
235 pelSeverity = itr->second;
236 }
237
238 method.append(errIntf, pelSeverity, additionalData);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500239 auto resp = bus.call(method);
240 }
Patrick Williams2eb01762022-07-22 19:26:56 -0500241 catch (const sdbusplus::exception_t& e)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500242 {
Sunny Srivastava5ef6ccc2022-05-30 01:35:13 -0500243 std::cerr << "Dbus call to phosphor-logging Create failed. Reason:"
244 << e.what();
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500245 }
246}
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530247
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500248inventory::VPDfilepath getVpdFilePath(const std::string& jsonFile,
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530249 const std::string& ObjPath)
250{
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500251 std::ifstream inventoryJson(jsonFile);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530252 const auto& jsonObject = json::parse(inventoryJson);
253 inventory::VPDfilepath filePath{};
254
255 if (jsonObject.find("frus") == jsonObject.end())
256 {
257 throw(VpdJsonException(
258 "Invalid JSON structure - frus{} object not found in ", jsonFile));
259 }
260
261 const nlohmann::json& groupFRUS =
262 jsonObject["frus"].get_ref<const nlohmann::json::object_t&>();
263 for (const auto& itemFRUS : groupFRUS.items())
264 {
265 const std::vector<nlohmann::json>& groupEEPROM =
266 itemFRUS.value().get_ref<const nlohmann::json::array_t&>();
267 for (const auto& itemEEPROM : groupEEPROM)
268 {
269 if (itemEEPROM["inventoryPath"]
270 .get_ref<const nlohmann::json::string_t&>() == ObjPath)
271 {
272 filePath = itemFRUS.key();
273 return filePath;
274 }
275 }
276 }
277
278 return filePath;
279}
280
281bool isPathInJson(const std::string& eepromPath)
282{
283 bool present = false;
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500284 std::ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530285
286 try
287 {
288 auto js = json::parse(inventoryJson);
289 if (js.find("frus") == js.end())
290 {
291 throw(VpdJsonException(
292 "Invalid JSON structure - frus{} object not found in ",
293 INVENTORY_JSON_SYM_LINK));
294 }
295 json fruJson = js["frus"];
296
297 if (fruJson.find(eepromPath) != fruJson.end())
298 {
299 present = true;
300 }
301 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500302 catch (const json::parse_error& ex)
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530303 {
304 throw(VpdJsonException("Json Parsing failed", INVENTORY_JSON_SYM_LINK));
305 }
306 return present;
307}
308
309bool isRecKwInDbusJson(const std::string& recordName,
310 const std::string& keyword)
311{
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500312 std::ifstream propertyJson(DBUS_PROP_JSON);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530313 json dbusProperty;
314 bool present = false;
315
316 if (propertyJson.is_open())
317 {
318 try
319 {
320 auto dbusPropertyJson = json::parse(propertyJson);
321 if (dbusPropertyJson.find("dbusProperties") ==
322 dbusPropertyJson.end())
323 {
324 throw(VpdJsonException("dbusProperties{} object not found in "
325 "DbusProperties json : ",
326 DBUS_PROP_JSON));
327 }
328
329 dbusProperty = dbusPropertyJson["dbusProperties"];
330 if (dbusProperty.contains(recordName))
331 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500332 const std::vector<std::string>& kwdsToPublish =
333 dbusProperty[recordName];
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530334 if (find(kwdsToPublish.begin(), kwdsToPublish.end(), keyword) !=
335 kwdsToPublish.end()) // present
336 {
337 present = true;
338 }
339 }
340 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500341 catch (const json::parse_error& ex)
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530342 {
343 throw(VpdJsonException("Json Parsing failed", DBUS_PROP_JSON));
344 }
345 }
346 else
347 {
348 // If dbus properties json is not available, we assume the given
349 // record-keyword is part of dbus-properties json. So setting the bool
350 // variable to true.
351 present = true;
352 }
353 return present;
354}
355
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500356vpdType vpdTypeCheck(const Binary& vpdVector)
357{
358 // Read first 3 Bytes to check the 11S bar code format
359 std::string is11SFormat = "";
360 for (uint8_t i = 0; i < FORMAT_11S_LEN; i++)
361 {
362 is11SFormat += vpdVector[MEMORY_VPD_DATA_START + i];
363 }
364
365 if (vpdVector[IPZ_DATA_START] == KW_VAL_PAIR_START_TAG)
366 {
367 // IPZ VPD FORMAT
368 return vpdType::IPZ_VPD;
369 }
370 else if (vpdVector[KW_VPD_DATA_START] == KW_VPD_START_TAG)
371 {
372 // KEYWORD VPD FORMAT
373 return vpdType::KEYWORD_VPD;
374 }
jinuthomas6555e7e2023-02-14 21:48:00 -0600375 else if (((vpdVector[SPD_BYTE_3] & SPD_BYTE_BIT_0_3_MASK) ==
376 SPD_MODULE_TYPE_DDIMM) &&
jinuthomas0abbb9c2023-05-05 01:37:07 -0500377 (is11SFormat.compare(MEMORY_VPD_START_TAG) == 0))
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500378 {
jinuthomas6555e7e2023-02-14 21:48:00 -0600379 // DDIMM Memory VPD format
380 if ((vpdVector[SPD_BYTE_2] & SPD_BYTE_MASK) == SPD_DRAM_TYPE_DDR5)
381 {
382 return vpdType::DDR5_DDIMM_MEMORY_VPD;
383 }
384 else if ((vpdVector[SPD_BYTE_2] & SPD_BYTE_MASK) == SPD_DRAM_TYPE_DDR4)
385 {
386 return vpdType::DDR4_DDIMM_MEMORY_VPD;
387 }
388 }
389 else if ((vpdVector[SPD_BYTE_2] & SPD_BYTE_MASK) == SPD_DRAM_TYPE_DDR5)
390 {
391 // ISDIMM Memory VPD format
392 return vpdType::DDR5_ISDIMM_MEMORY_VPD;
393 }
394 else if ((vpdVector[SPD_BYTE_2] & SPD_BYTE_MASK) == SPD_DRAM_TYPE_DDR4)
395 {
396 // ISDIMM Memory VPD format
397 return vpdType::DDR4_ISDIMM_MEMORY_VPD;
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500398 }
399
400 // INVALID VPD FORMAT
401 return vpdType::INVALID_VPD_FORMAT;
402}
403
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500404const std::string getIM(const Parsed& vpdMap)
Alpana Kumarif05effd2021-04-07 07:32:53 -0500405{
406 Binary imVal;
407 auto property = vpdMap.find("VSBP");
408 if (property != vpdMap.end())
409 {
410 auto kw = (property->second).find("IM");
411 if (kw != (property->second).end())
412 {
413 copy(kw->second.begin(), kw->second.end(), back_inserter(imVal));
414 }
415 }
416
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500417 std::ostringstream oss;
Alpana Kumarif05effd2021-04-07 07:32:53 -0500418 for (auto& i : imVal)
419 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500420 oss << std::setw(2) << std::setfill('0') << std::hex
421 << static_cast<int>(i);
Alpana Kumarif05effd2021-04-07 07:32:53 -0500422 }
423
424 return oss.str();
425}
426
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500427const std::string getHW(const Parsed& vpdMap)
Alpana Kumarif05effd2021-04-07 07:32:53 -0500428{
429 Binary hwVal;
430 auto prop = vpdMap.find("VINI");
431 if (prop != vpdMap.end())
432 {
433 auto kw = (prop->second).find("HW");
434 if (kw != (prop->second).end())
435 {
436 copy(kw->second.begin(), kw->second.end(), back_inserter(hwVal));
437 }
438 }
439
Alpana Kumari88d2ae82021-11-10 03:23:31 -0600440 // The planar pass only comes from the LSB of the HW keyword,
441 // where as the MSB is used for other purposes such as signifying clock
442 // termination.
443 hwVal[0] = 0x00;
444
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500445 std::ostringstream hwString;
Alpana Kumarif05effd2021-04-07 07:32:53 -0500446 for (auto& i : hwVal)
447 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500448 hwString << std::setw(2) << std::setfill('0') << std::hex
449 << static_cast<int>(i);
Alpana Kumarif05effd2021-04-07 07:32:53 -0500450 }
451
452 return hwString.str();
453}
454
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500455std::string getSystemsJson(const Parsed& vpdMap)
Alpana Kumarif05effd2021-04-07 07:32:53 -0500456{
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500457 std::string jsonPath = "/usr/share/vpd/";
458 std::string jsonName{};
Alpana Kumarif05effd2021-04-07 07:32:53 -0500459
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500460 std::ifstream systemJson(SYSTEM_JSON);
Alpana Kumarif05effd2021-04-07 07:32:53 -0500461 if (!systemJson)
462 {
463 throw((VpdJsonException("Failed to access Json path", SYSTEM_JSON)));
464 }
465
466 try
467 {
468 auto js = json::parse(systemJson);
469
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500470 std::string hwKeyword = getHW(vpdMap);
471 const std::string imKeyword = getIM(vpdMap);
Alpana Kumarif05effd2021-04-07 07:32:53 -0500472
Alpana Kumari1b026112022-03-02 23:41:38 -0600473 transform(hwKeyword.begin(), hwKeyword.end(), hwKeyword.begin(),
474 ::toupper);
475
Alpana Kumarif05effd2021-04-07 07:32:53 -0500476 if (js.find("system") == js.end())
477 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500478 throw std::runtime_error("Invalid systems Json");
Alpana Kumarif05effd2021-04-07 07:32:53 -0500479 }
480
481 if (js["system"].find(imKeyword) == js["system"].end())
482 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500483 throw std::runtime_error(
Alpana Kumarif05effd2021-04-07 07:32:53 -0500484 "Invalid system. This system type is not present "
485 "in the systemsJson. IM: " +
486 imKeyword);
487 }
488
489 if ((js["system"][imKeyword].find("constraint") !=
490 js["system"][imKeyword].end()) &&
Alpana Kumari1b026112022-03-02 23:41:38 -0600491 js["system"][imKeyword]["constraint"].find("HW") !=
492 js["system"][imKeyword]["constraint"].end())
Alpana Kumarif05effd2021-04-07 07:32:53 -0500493 {
Alpana Kumari1b026112022-03-02 23:41:38 -0600494 // collect hw versions from json, and check hwKeyword is part of it
495 // if hwKeyword is found there then load respective json
496 // otherwise load default one.
497 for (const auto& hwVersion :
498 js["system"][imKeyword]["constraint"]["HW"])
499 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500500 std::string hw = hwVersion;
Alpana Kumari1b026112022-03-02 23:41:38 -0600501 transform(hw.begin(), hw.end(), hw.begin(), ::toupper);
502
503 if (hw == hwKeyword)
504 {
505 jsonName = js["system"][imKeyword]["constraint"]["json"];
506 break;
507 }
508 }
509
510 if (jsonName.empty() && js["system"][imKeyword].find("default") !=
511 js["system"][imKeyword].end())
512 {
513 jsonName = js["system"][imKeyword]["default"];
514 }
Alpana Kumarif05effd2021-04-07 07:32:53 -0500515 }
516 else if (js["system"][imKeyword].find("default") !=
517 js["system"][imKeyword].end())
518 {
519 jsonName = js["system"][imKeyword]["default"];
520 }
521 else
522 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500523 throw std::runtime_error(
Alpana Kumarif05effd2021-04-07 07:32:53 -0500524 "Bad System json. Neither constraint nor default found");
525 }
526
527 jsonPath += jsonName;
528 }
529
Patrick Williams8e15b932021-10-06 13:04:22 -0500530 catch (const json::parse_error& ex)
Alpana Kumarif05effd2021-04-07 07:32:53 -0500531 {
532 throw(VpdJsonException("Json Parsing failed", SYSTEM_JSON));
533 }
534 return jsonPath;
535}
536
jinuthomasf457a3e2023-04-13 12:22:48 -0500537void udevToGenericPath(std::string& file, const std::string& driver)
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530538{
539 // Sample udevEvent i2c path :
540 // "/sys/devices/platform/ahb/ahb:apb/ahb:apb:bus@1e78a000/1e78a480.i2c-bus/i2c-8/8-0051/8-00510/nvmem"
541 // find if the path contains the word i2c in it.
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500542 if (file.find("i2c") != std::string::npos)
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530543 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500544 std::string i2cBusAddr{};
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530545
546 // Every udev i2c path should have the common pattern
547 // "i2c-bus_number/bus_number-vpd_address". Search for
548 // "bus_number-vpd_address".
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500549 std::regex i2cPattern("((i2c)-[0-9]+\\/)([0-9]+-[0-9]{4})");
550 std::smatch match;
551 if (std::regex_search(file, match, i2cPattern))
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530552 {
553 i2cBusAddr = match.str(3);
554 }
555 else
556 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500557 std::cerr << "The given udev path < " << file
558 << " > doesn't match the required pattern. Skipping VPD "
559 "collection."
560 << std::endl;
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530561 exit(EXIT_SUCCESS);
562 }
563 // Forming the generic file path
jinuthomasf457a3e2023-04-13 12:22:48 -0500564 file = i2cPathPrefix + driver + "/" + i2cBusAddr + "/eeprom";
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530565 }
566 // Sample udevEvent spi path :
567 // "/sys/devices/platform/ahb/ahb:apb/1e79b000.fsi/fsi-master/fsi0/slave@00:00/00:00:00:04/spi_master/spi2/spi2.0/spi2.00/nvmem"
568 // find if the path contains the word spi in it.
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500569 else if (file.find("spi") != std::string::npos)
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530570 {
571 // Every udev spi path will have common pattern "spi<Digit>/", which
572 // describes the spi bus number at which the fru is connected; Followed
573 // by a slash following the vpd address of the fru. Taking the above
574 // input as a common key, we try to search for the pattern "spi<Digit>/"
575 // using regular expression.
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500576 std::regex spiPattern("((spi)[0-9]+)(\\/)");
577 std::string spiBus{};
578 std::smatch match;
579 if (std::regex_search(file, match, spiPattern))
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530580 {
581 spiBus = match.str(1);
582 }
583 else
584 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500585 std::cerr << "The given udev path < " << file
586 << " > doesn't match the required pattern. Skipping VPD "
587 "collection."
588 << std::endl;
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530589 exit(EXIT_SUCCESS);
590 }
591 // Forming the generic path
jinuthomasf457a3e2023-04-13 12:22:48 -0500592 file = spiPathPrefix + driver + "/" + spiBus + ".0/eeprom";
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530593 }
594 else
595 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500596 std::cerr << "\n The given EEPROM path < " << file
597 << " > is not valid. It's neither I2C nor "
598 "SPI path. Skipping VPD collection.."
599 << std::endl;
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530600 exit(EXIT_SUCCESS);
601 }
602}
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500603std::string getBadVpdName(const std::string& file)
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600604{
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500605 std::string badVpd = BAD_VPD_DIR;
606 if (file.find("i2c") != std::string::npos)
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600607 {
608 badVpd += "i2c-";
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500609 std::regex i2cPattern("(at24/)([0-9]+-[0-9]+)\\/");
610 std::smatch match;
611 if (std::regex_search(file, match, i2cPattern))
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600612 {
613 badVpd += match.str(2);
614 }
615 }
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500616 else if (file.find("spi") != std::string::npos)
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600617 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500618 std::regex spiPattern("((spi)[0-9]+)(.0)");
619 std::smatch match;
620 if (std::regex_search(file, match, spiPattern))
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600621 {
622 badVpd += match.str(1);
623 }
624 }
625 return badVpd;
626}
627
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500628void dumpBadVpd(const std::string& file, const Binary& vpdVector)
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600629{
630 fs::path badVpdDir = BAD_VPD_DIR;
631 fs::create_directory(badVpdDir);
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500632 std::string badVpdPath = getBadVpdName(file);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600633 if (fs::exists(badVpdPath))
634 {
635 std::error_code ec;
636 fs::remove(badVpdPath, ec);
637 if (ec) // error code
638 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500639 std::string error = "Error removing the existing broken vpd in ";
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600640 error += badVpdPath;
641 error += ". Error code : ";
642 error += ec.value();
643 error += ". Error message : ";
644 error += ec.message();
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500645 throw std::runtime_error(error);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600646 }
647 }
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500648 std::ofstream badVpdFileStream(badVpdPath, std::ofstream::binary);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600649 if (!badVpdFileStream)
650 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500651 throw std::runtime_error(
652 "Failed to open bad vpd file path in /tmp/bad-vpd. "
653 "Unable to dump the broken/bad vpd file.");
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600654 }
655 badVpdFileStream.write(reinterpret_cast<const char*>(vpdVector.data()),
656 vpdVector.size());
657}
alpana077ce68722021-07-25 13:23:59 -0500658
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500659const std::string getKwVal(const Parsed& vpdMap, const std::string& rec,
660 const std::string& kwd)
alpana077ce68722021-07-25 13:23:59 -0500661{
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500662 std::string kwVal{};
alpana077ce68722021-07-25 13:23:59 -0500663
664 auto findRec = vpdMap.find(rec);
665
666 // check if record is found in map we got by parser
667 if (findRec != vpdMap.end())
668 {
669 auto findKwd = findRec->second.find(kwd);
670
671 if (findKwd != findRec->second.end())
672 {
673 kwVal = findKwd->second;
674 }
Sunny Srivastava37992a62023-07-11 05:18:41 -0500675 else
676 {
677 std::cout << "Keyword not found" << std::endl;
678 }
679 }
680 else
681 {
682 std::cerr << "Record not found" << std::endl;
alpana077ce68722021-07-25 13:23:59 -0500683 }
684
685 return kwVal;
686}
687
GiridhariKrishnan63639102023-03-02 05:55:47 -0600688std::string hexString(const std::variant<Binary, std::string>& kw)
Priyanga Ramasamyc9ecf8e2021-10-08 02:28:52 -0500689{
GiridhariKrishnan63639102023-03-02 05:55:47 -0600690 std::string hexString;
691 std::visit(
692 [&hexString](auto&& kw) {
Patrick Williamsc78d8872023-05-10 07:50:56 -0500693 std::stringstream ss;
694 std::string hexRep = "0x";
695 ss << hexRep;
696 for (auto& kwVal : kw)
697 {
698 ss << std::setfill('0') << std::setw(2) << std::hex
699 << static_cast<int>(kwVal);
700 }
701 hexString = ss.str();
GiridhariKrishnan63639102023-03-02 05:55:47 -0600702 },
703 kw);
704 return hexString;
Priyanga Ramasamyc9ecf8e2021-10-08 02:28:52 -0500705}
706
GiridhariKrishnan63639102023-03-02 05:55:47 -0600707std::string getPrintableValue(const std::variant<Binary, std::string>& kwVal)
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500708{
GiridhariKrishnan63639102023-03-02 05:55:47 -0600709 std::string kwString{};
710 std::visit(
711 [&kwString](auto&& kwVal) {
Patrick Williamsc78d8872023-05-10 07:50:56 -0500712 const auto it =
713 std::find_if(kwVal.begin(), kwVal.end(),
714 [](const auto& kw) { return !isprint(kw); });
715 if (it != kwVal.end())
716 {
girik390fccb2023-05-02 07:42:47 -0500717 kwString = hexString(kwVal);
Patrick Williamsc78d8872023-05-10 07:50:56 -0500718 }
719 else
720 {
721 kwString = std::string(kwVal.begin(), kwVal.end());
722 }
GiridhariKrishnan63639102023-03-02 05:55:47 -0600723 },
724 kwVal);
725 return kwString;
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500726}
727
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500728void executePostFailAction(const nlohmann::json& json, const std::string& file)
Alpana Kumari735dee92022-03-25 01:24:40 -0500729{
730 if ((json["frus"][file].at(0)).find("postActionFail") ==
731 json["frus"][file].at(0).end())
732 {
733 return;
734 }
735
736 uint8_t pinValue = 0;
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500737 std::string pinName;
Alpana Kumari735dee92022-03-25 01:24:40 -0500738
739 for (const auto& postAction :
740 (json["frus"][file].at(0))["postActionFail"].items())
741 {
742 if (postAction.key() == "pin")
743 {
744 pinName = postAction.value();
745 }
746 else if (postAction.key() == "value")
747 {
748 // Get the value to set
749 pinValue = postAction.value();
750 }
751 }
752
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500753 std::cout << "Setting GPIO: " << pinName << " to " << (int)pinValue
754 << std::endl;
Alpana Kumari735dee92022-03-25 01:24:40 -0500755
756 try
757 {
758 gpiod::line outputLine = gpiod::find_line(pinName);
759
760 if (!outputLine)
761 {
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500762 throw GpioException(
Alpana Kumari6bd095f2022-02-23 10:20:20 -0600763 "Couldn't find output line for the GPIO. Skipping "
764 "this GPIO action.");
Alpana Kumari735dee92022-03-25 01:24:40 -0500765 }
766 outputLine.request(
767 {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
768 pinValue);
769 }
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500770 catch (const std::exception& e)
Alpana Kumari735dee92022-03-25 01:24:40 -0500771 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500772 std::string i2cBusAddr;
773 std::string errMsg = e.what();
Alpana Kumari6bd095f2022-02-23 10:20:20 -0600774 errMsg += "\nGPIO: " + pinName;
775
776 if ((json["frus"][file].at(0)["postActionFail"].find(
777 "gpioI2CAddress")) !=
778 json["frus"][file].at(0)["postActionFail"].end())
779 {
780 i2cBusAddr =
781 json["frus"][file].at(0)["postActionFail"]["gpioI2CAddress"];
Sunny Srivastavafdf9ff22022-06-15 11:15:54 -0500782 errMsg += " i2cBusAddress: " + i2cBusAddr;
Alpana Kumari6bd095f2022-02-23 10:20:20 -0600783 }
784
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500785 throw GpioException(e.what());
Alpana Kumari735dee92022-03-25 01:24:40 -0500786 }
Alpana Kumari6bd095f2022-02-23 10:20:20 -0600787
788 return;
Alpana Kumari735dee92022-03-25 01:24:40 -0500789}
790
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500791std::optional<bool> isPresent(const nlohmann::json& json,
792 const std::string& file)
Santosh Puranik53b38ed2022-04-10 23:15:22 +0530793
Alpana Kumari735dee92022-03-25 01:24:40 -0500794{
795 if ((json["frus"][file].at(0)).find("presence") !=
796 json["frus"][file].at(0).end())
797 {
798 if (((json["frus"][file].at(0)["presence"]).find("pin") !=
799 json["frus"][file].at(0)["presence"].end()) &&
800 ((json["frus"][file].at(0)["presence"]).find("value") !=
801 json["frus"][file].at(0)["presence"].end()))
802 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500803 std::string presPinName =
804 json["frus"][file].at(0)["presence"]["pin"];
Alpana Kumari735dee92022-03-25 01:24:40 -0500805 Byte presPinValue = json["frus"][file].at(0)["presence"]["value"];
806
807 try
808 {
809 gpiod::line presenceLine = gpiod::find_line(presPinName);
810
811 if (!presenceLine)
812 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500813 std::cerr << "Couldn't find the presence line for - "
814 << presPinName << std::endl;
Alpana Kumari40d1c192022-03-09 21:16:02 -0600815
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500816 throw GpioException(
Alpana Kumari6bd095f2022-02-23 10:20:20 -0600817 "Couldn't find the presence line for the "
818 "GPIO. Skipping this GPIO action.");
Alpana Kumari735dee92022-03-25 01:24:40 -0500819 }
820
821 presenceLine.request({"Read the presence line",
822 gpiod::line_request::DIRECTION_INPUT, 0});
823
824 Byte gpioData = presenceLine.get_value();
825
Santosh Puranik53b38ed2022-04-10 23:15:22 +0530826 return (gpioData == presPinValue);
Alpana Kumari735dee92022-03-25 01:24:40 -0500827 }
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500828 catch (const std::exception& e)
Alpana Kumari735dee92022-03-25 01:24:40 -0500829 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500830 std::string i2cBusAddr;
831 std::string errMsg = e.what();
Alpana Kumari6bd095f2022-02-23 10:20:20 -0600832 errMsg += " GPIO : " + presPinName;
833
834 if ((json["frus"][file].at(0)["presence"])
835 .find("gpioI2CAddress") !=
836 json["frus"][file].at(0)["presence"].end())
837 {
838 i2cBusAddr =
839 json["frus"][file].at(0)["presence"]["gpioI2CAddress"];
Sunny Srivastavafdf9ff22022-06-15 11:15:54 -0500840 errMsg += " i2cBusAddress: " + i2cBusAddr;
Alpana Kumari6bd095f2022-02-23 10:20:20 -0600841 }
842
Alpana Kumari40d1c192022-03-09 21:16:02 -0600843 // Take failure postAction
844 executePostFailAction(json, file);
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500845 throw GpioException(errMsg);
Alpana Kumari735dee92022-03-25 01:24:40 -0500846 }
847 }
Alpana Kumari40d1c192022-03-09 21:16:02 -0600848 else
849 {
850 // missing required informations
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500851 std::cerr
852 << "VPD inventory JSON missing basic informations of presence "
853 "for this FRU : ["
854 << file << "]. Executing executePostFailAction." << std::endl;
Alpana Kumari40d1c192022-03-09 21:16:02 -0600855
856 // Take failure postAction
857 executePostFailAction(json, file);
858
859 return false;
860 }
Alpana Kumari735dee92022-03-25 01:24:40 -0500861 }
Santosh Puranik53b38ed2022-04-10 23:15:22 +0530862 return std::optional<bool>{};
863}
864
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500865bool executePreAction(const nlohmann::json& json, const std::string& file)
Santosh Puranik53b38ed2022-04-10 23:15:22 +0530866{
867 auto present = isPresent(json, file);
868 if (present && !present.value())
869 {
870 executePostFailAction(json, file);
871 return false;
872 }
Alpana Kumari735dee92022-03-25 01:24:40 -0500873
874 if ((json["frus"][file].at(0)).find("preAction") !=
875 json["frus"][file].at(0).end())
876 {
877 if (((json["frus"][file].at(0)["preAction"]).find("pin") !=
878 json["frus"][file].at(0)["preAction"].end()) &&
879 ((json["frus"][file].at(0)["preAction"]).find("value") !=
880 json["frus"][file].at(0)["preAction"].end()))
881 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500882 std::string pinName = json["frus"][file].at(0)["preAction"]["pin"];
Alpana Kumari735dee92022-03-25 01:24:40 -0500883 // Get the value to set
884 Byte pinValue = json["frus"][file].at(0)["preAction"]["value"];
885
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500886 std::cout << "Setting GPIO: " << pinName << " to " << (int)pinValue
887 << std::endl;
Alpana Kumari735dee92022-03-25 01:24:40 -0500888 try
889 {
890 gpiod::line outputLine = gpiod::find_line(pinName);
891
892 if (!outputLine)
893 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500894 std::cerr << "Couldn't find the line for output pin - "
895 << pinName << std::endl;
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500896 throw GpioException(
Alpana Kumari6bd095f2022-02-23 10:20:20 -0600897 "Couldn't find output line for the GPIO. "
898 "Skipping this GPIO action.");
Alpana Kumari735dee92022-03-25 01:24:40 -0500899 }
900 outputLine.request({"FRU pre-action",
901 ::gpiod::line_request::DIRECTION_OUTPUT, 0},
902 pinValue);
903 }
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500904 catch (const std::exception& e)
Alpana Kumari735dee92022-03-25 01:24:40 -0500905 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500906 std::string i2cBusAddr;
907 std::string errMsg = e.what();
Alpana Kumari6bd095f2022-02-23 10:20:20 -0600908 errMsg += " GPIO : " + pinName;
909
910 if ((json["frus"][file].at(0)["preAction"])
911 .find("gpioI2CAddress") !=
912 json["frus"][file].at(0)["preAction"].end())
913 {
914 i2cBusAddr =
915 json["frus"][file].at(0)["preAction"]["gpioI2CAddress"];
Sunny Srivastavafdf9ff22022-06-15 11:15:54 -0500916 errMsg += " i2cBusAddress: " + i2cBusAddr;
Alpana Kumari6bd095f2022-02-23 10:20:20 -0600917 }
918
Alpana Kumari40d1c192022-03-09 21:16:02 -0600919 // Take failure postAction
920 executePostFailAction(json, file);
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500921 throw GpioException(errMsg);
Alpana Kumari735dee92022-03-25 01:24:40 -0500922 }
923 }
Alpana Kumari40d1c192022-03-09 21:16:02 -0600924 else
925 {
926 // missing required informations
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500927 std::cerr
Alpana Kumari40d1c192022-03-09 21:16:02 -0600928 << "VPD inventory JSON missing basic informations of preAction "
929 "for this FRU : ["
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500930 << file << "]. Executing executePostFailAction." << std::endl;
Alpana Kumari40d1c192022-03-09 21:16:02 -0600931
932 // Take failure postAction
933 executePostFailAction(json, file);
Alpana Kumari40d1c192022-03-09 21:16:02 -0600934 return false;
935 }
Alpana Kumari735dee92022-03-25 01:24:40 -0500936 }
937 return true;
938}
939
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600940void insertOrMerge(inventory::InterfaceMap& map,
941 const inventory::Interface& interface,
942 inventory::PropertyMap&& property)
943{
944 if (map.find(interface) != map.end())
945 {
946 auto& prop = map.at(interface);
947 prop.insert(property.begin(), property.end());
948 }
949 else
950 {
951 map.emplace(interface, property);
952 }
953}
Santosh Puranikf2d3b532022-04-19 06:44:07 -0500954
955BIOSAttrValueType readBIOSAttribute(const std::string& attrName)
956{
957 std::tuple<std::string, BIOSAttrValueType, BIOSAttrValueType> attrVal;
958 auto bus = sdbusplus::bus::new_default();
959 auto method = bus.new_method_call(
960 "xyz.openbmc_project.BIOSConfigManager",
961 "/xyz/openbmc_project/bios_config/manager",
962 "xyz.openbmc_project.BIOSConfig.Manager", "GetAttribute");
963 method.append(attrName);
964 try
965 {
966 auto result = bus.call(method);
967 result.read(std::get<0>(attrVal), std::get<1>(attrVal),
968 std::get<2>(attrVal));
969 }
970 catch (const sdbusplus::exception::SdBusError& e)
971 {
972 std::cerr << "Failed to read BIOS Attribute: " << attrName << std::endl;
973 std::cerr << e.what() << std::endl;
974 }
975 return std::get<1>(attrVal);
976}
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500977
978std::string getPowerState()
979{
980 // TODO: How do we handle multiple chassis?
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500981 std::string powerState{};
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500982 auto bus = sdbusplus::bus::new_default();
Patrick Williamsc78d8872023-05-10 07:50:56 -0500983 auto properties = bus.new_method_call("xyz.openbmc_project.State.Chassis",
984 "/xyz/openbmc_project/state/chassis0",
985 "org.freedesktop.DBus.Properties",
986 "Get");
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500987 properties.append("xyz.openbmc_project.State.Chassis");
988 properties.append("CurrentPowerState");
989 auto result = bus.call(properties);
990 if (!result.is_method_error())
991 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500992 std::variant<std::string> val;
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500993 result.read(val);
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500994 if (auto pVal = std::get_if<std::string>(&val))
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500995 {
996 powerState = *pVal;
997 }
998 }
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500999 std::cout << "Power state is: " << powerState << std::endl;
Priyanga Ramasamy335873f2022-05-18 01:31:54 -05001000 return powerState;
1001}
Santosh Puranik6b2b5372022-06-02 20:49:02 +05301002
1003Binary getVpdDataInVector(const nlohmann::json& js, const std::string& file)
1004{
1005 uint32_t offset = 0;
1006 // check if offset present?
1007 for (const auto& item : js["frus"][file])
1008 {
1009 if (item.find("offset") != item.end())
1010 {
1011 offset = item["offset"];
1012 }
1013 }
1014
1015 // TODO: Figure out a better way to get max possible VPD size.
1016 auto maxVPDSize = std::min(std::filesystem::file_size(file),
1017 static_cast<uintmax_t>(65504));
1018
1019 Binary vpdVector;
1020 vpdVector.resize(maxVPDSize);
Priyanga Ramasamye0084322022-09-27 06:28:33 -05001021 std::ifstream vpdFile;
1022 vpdFile.open(file, std::ios::binary);
Santosh Puranik6b2b5372022-06-02 20:49:02 +05301023
Priyanga Ramasamye0084322022-09-27 06:28:33 -05001024 vpdFile.seekg(offset, std::ios_base::cur);
Santosh Puranik6b2b5372022-06-02 20:49:02 +05301025 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), maxVPDSize);
1026 vpdVector.resize(vpdFile.gcount());
1027
1028 // Make sure we reset the EEPROM pointer to a "safe" location if it was DIMM
1029 // SPD that we just read.
1030 for (const auto& item : js["frus"][file])
1031 {
1032 if (item.find("extraInterfaces") != item.end())
1033 {
1034 if (item["extraInterfaces"].find(
1035 "xyz.openbmc_project.Inventory.Item.Dimm") !=
1036 item["extraInterfaces"].end())
1037 {
1038 // moves the EEPROM pointer to 2048 'th byte.
1039 vpdFile.seekg(2047, std::ios::beg);
1040 // Read that byte and discard - to affirm the move
1041 // operation.
1042 char ch;
1043 vpdFile.read(&ch, sizeof(ch));
1044 break;
1045 }
1046 }
1047 }
1048
1049 return vpdVector;
1050}
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -06001051
1052std::string getDbusNameForThisKw(const std::string& keyword)
1053{
1054 if (keyword[0] == constants::POUND_KW)
1055 {
1056 return (std::string(constants::POUND_KW_PREFIX) + keyword[1]);
1057 }
1058 else if (isdigit(keyword[0]))
1059 {
1060 return (std::string(constants::NUMERIC_KW_PREFIX) + keyword);
1061 }
1062 return keyword;
1063}
1064
Patrick Venturec83c4dc2018-11-01 16:29:18 -07001065} // namespace vpd
Patrick Williamsc78d8872023-05-10 07:50:56 -05001066} // namespace openpower