blob: 767ea0bb3788822979ffbc833fc85da71ac4c373 [file] [log] [blame]
SunnySrivastava1984b59fd092020-02-03 09:58:56 -06001#include "config.h"
2
3#include "manager.hpp"
4
Santosh Puranikbf78ed82022-04-20 13:17:04 +05305#include "common_utility.hpp"
SunnySrivastava1984d076da82020-03-05 05:33:35 -06006#include "editor_impl.hpp"
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05007#include "ibm_vpd_utils.hpp"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05008#include "ipz_parser.hpp"
Santosh Puranik6b2b5372022-06-02 20:49:02 +05309#include "parser_factory.hpp"
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -050010#include "reader_impl.hpp"
SunnySrivastava19849a195542020-09-07 06:04:50 -050011#include "vpd_exceptions.hpp"
12
Sunny Srivastava4788e1b2024-03-19 02:11:07 -050013#include <unistd.h>
14
SunnySrivastava19849a195542020-09-07 06:04:50 -050015#include <phosphor-logging/elog-errors.hpp>
Sunny Srivastava28abd6e2021-07-28 02:58:28 -050016#include <xyz/openbmc_project/Common/error.hpp>
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060017
Patrick Williamsc78d8872023-05-10 07:50:56 -050018#include <filesystem>
19
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060020using namespace openpower::vpd::constants;
SunnySrivastava19841356d7e2020-04-24 04:29:35 -050021using namespace openpower::vpd::inventory;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060022using namespace openpower::vpd::manager::editor;
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -050023using namespace openpower::vpd::manager::reader;
SunnySrivastava19849a195542020-09-07 06:04:50 -050024using namespace std;
25using namespace openpower::vpd::parser;
Santosh Puranik6b2b5372022-06-02 20:49:02 +053026using namespace openpower::vpd::parser::factory;
27using namespace openpower::vpd::ipz::parser;
SunnySrivastava19849a195542020-09-07 06:04:50 -050028using namespace openpower::vpd::exceptions;
29using namespace phosphor::logging;
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060030
31namespace openpower
32{
33namespace vpd
34{
35namespace manager
36{
Sunny Srivastavac6e7ea92023-11-03 21:10:43 +053037
Sunny Srivastava523af2e2022-02-14 07:30:10 -060038Manager::Manager(std::shared_ptr<boost::asio::io_context>& ioCon,
39 std::shared_ptr<sdbusplus::asio::dbus_interface>& iFace,
40 std::shared_ptr<sdbusplus::asio::connection>& conn) :
41 ioContext(ioCon),
42 interface(iFace), conn(conn)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060043{
Sunny Srivastava523af2e2022-02-14 07:30:10 -060044 interface->register_method(
45 "WriteKeyword",
46 [this](const sdbusplus::message::object_path& path,
47 const std::string& recordName, const std::string& keyword,
48 const Binary& value) {
Patrick Williamsc78d8872023-05-10 07:50:56 -050049 this->writeKeyword(path, recordName, keyword, value);
Patrick Williamsb7b352a2023-10-20 11:19:19 -050050 });
Sunny Srivastava523af2e2022-02-14 07:30:10 -060051
52 interface->register_method(
53 "GetFRUsByUnexpandedLocationCode",
54 [this](const std::string& locationCode,
55 const uint16_t nodeNumber) -> inventory::ListOfPaths {
Patrick Williamsb7b352a2023-10-20 11:19:19 -050056 return this->getFRUsByUnexpandedLocationCode(locationCode, nodeNumber);
57 });
Sunny Srivastava523af2e2022-02-14 07:30:10 -060058
59 interface->register_method(
60 "GetFRUsByExpandedLocationCode",
61 [this](const std::string& locationCode) -> inventory::ListOfPaths {
Patrick Williamsb7b352a2023-10-20 11:19:19 -050062 return this->getFRUsByExpandedLocationCode(locationCode);
63 });
Sunny Srivastava523af2e2022-02-14 07:30:10 -060064
65 interface->register_method(
66 "GetExpandedLocationCode",
67 [this](const std::string& locationCode,
68 const uint16_t nodeNumber) -> std::string {
Patrick Williamsb7b352a2023-10-20 11:19:19 -050069 return this->getExpandedLocationCode(locationCode, nodeNumber);
70 });
Sunny Srivastava523af2e2022-02-14 07:30:10 -060071
72 interface->register_method("PerformVPDRecollection",
73 [this]() { this->performVPDRecollection(); });
74
Sunny Srivastava28abd6e2021-07-28 02:58:28 -050075 interface->register_method(
76 "deleteFRUVPD", [this](const sdbusplus::message::object_path& path) {
Patrick Williamsb7b352a2023-10-20 11:19:19 -050077 this->deleteFRUVPD(path);
78 });
Sunny Srivastava28abd6e2021-07-28 02:58:28 -050079
Sunny Srivastava6a1bd392021-06-02 04:39:24 -050080 interface->register_method(
81 "CollectFRUVPD", [this](const sdbusplus::message::object_path& path) {
Patrick Williamsb7b352a2023-10-20 11:19:19 -050082 this->collectFRUVPD(path);
83 });
Sunny Srivastava6a1bd392021-06-02 04:39:24 -050084
Sunny Srivastavafdf9ff22022-06-15 11:15:54 -050085 sd_bus_default(&sdBus);
Sunny Srivastava523af2e2022-02-14 07:30:10 -060086 initManager();
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060087}
88
Sunny Srivastava523af2e2022-02-14 07:30:10 -060089void Manager::initManager()
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060090{
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -060091 try
92 {
93 processJSON();
Santosh Puranik6b2b5372022-06-02 20:49:02 +053094 restoreSystemVpd();
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -050095 listenHostState();
Santosh Puranikb62f6052022-04-06 18:37:54 +053096 listenAssetTag();
Alpana Kumarib17dd3b2020-10-01 00:18:10 -050097
Santosh Puranikf2d3b532022-04-19 06:44:07 -050098 // Create an instance of the BIOS handler
Sunny Srivastava523af2e2022-02-14 07:30:10 -060099 biosHandler = std::make_shared<BiosHandler>(conn, *this);
Santosh Puranikf2d3b532022-04-19 06:44:07 -0500100
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600101 // instantiate gpioMonitor class
102 gpioMon = std::make_shared<GpioMonitor>(jsonFile, ioContext);
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600103 }
104 catch (const std::exception& e)
105 {
106 std::cerr << e.what() << "\n";
107 }
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600108}
109
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530110/**
111 * @brief An api to get list of blank system VPD properties.
112 * @param[in] vpdMap - IPZ vpd map.
113 * @param[in] objectPath - Object path for the FRU.
114 * @param[out] blankPropertyList - Properties which are blank in System VPD and
115 * needs to be updated as standby.
116 */
117static void
118 getListOfBlankSystemVpd(Parsed& vpdMap, const string& objectPath,
119 std::vector<RestoredEeproms>& blankPropertyList)
120{
121 for (const auto& systemRecKwdPair : svpdKwdMap)
122 {
123 auto it = vpdMap.find(systemRecKwdPair.first);
124
125 // check if record is found in map we got by parser
126 if (it != vpdMap.end())
127 {
128 const auto& kwdListForRecord = systemRecKwdPair.second;
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600129 for (const auto& keywordInfo : kwdListForRecord)
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530130 {
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600131 const auto& keyword = get<0>(keywordInfo);
132
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530133 DbusPropertyMap& kwdValMap = it->second;
134 auto iterator = kwdValMap.find(keyword);
135
136 if (iterator != kwdValMap.end())
137 {
138 string& kwdValue = iterator->second;
139
140 // check bus data
141 const string& recordName = systemRecKwdPair.first;
142 const string& busValue = readBusProperty(
143 objectPath, ipzVpdInf + recordName, keyword);
144
Priyanga Ramasamy834c0782023-02-14 12:22:39 -0600145 const auto& defaultValue = get<1>(keywordInfo);
146
147 if (Binary(busValue.begin(), busValue.end()) !=
148 defaultValue)
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530149 {
Priyanga Ramasamy834c0782023-02-14 12:22:39 -0600150 if (Binary(kwdValue.begin(), kwdValue.end()) ==
151 defaultValue)
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530152 {
153 // implies data is blank on EEPROM but not on cache.
154 // So EEPROM vpd update is required.
155 Binary busData(busValue.begin(), busValue.end());
156
157 blankPropertyList.push_back(std::make_tuple(
158 objectPath, recordName, keyword, busData));
159 }
160 }
161 }
162 }
163 }
164 }
165}
166
167void Manager::restoreSystemVpd()
168{
169 std::cout << "Attempting system VPD restore" << std::endl;
170 ParserInterface* parser = nullptr;
171 try
172 {
173 auto vpdVector = getVpdDataInVector(jsonFile, systemVpdFilePath);
girik18bb9852022-11-16 05:48:13 -0600174 uint32_t vpdStartOffset = 0;
Sunny Srivastavaf31a91b2022-06-09 08:11:29 -0500175 const auto& inventoryPath =
176 jsonFile["frus"][systemVpdFilePath][0]["inventoryPath"]
177 .get_ref<const nlohmann::json::string_t&>();
178
girik18bb9852022-11-16 05:48:13 -0600179 parser = ParserFactory::getParser(vpdVector, (pimPath + inventoryPath),
180 systemVpdFilePath, vpdStartOffset);
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530181 auto parseResult = parser->parse();
182
183 if (auto pVal = std::get_if<Store>(&parseResult))
184 {
185 // map to hold all the keywords whose value is blank and
186 // needs to be updated at standby.
187 std::vector<RestoredEeproms> blankSystemVpdProperties{};
188 getListOfBlankSystemVpd(pVal->getVpdMap(), SYSTEM_OBJECT,
189 blankSystemVpdProperties);
190
191 // if system VPD restore is required, update the
192 // EEPROM
193 for (const auto& item : blankSystemVpdProperties)
194 {
195 std::cout << "Restoring keyword: " << std::get<2>(item)
196 << std::endl;
197 writeKeyword(std::get<0>(item), std::get<1>(item),
198 std::get<2>(item), std::get<3>(item));
199 }
200 }
201 else
202 {
203 std::cerr << "Not a valid format to restore system VPD"
204 << std::endl;
205 }
206 }
207 catch (const std::exception& e)
208 {
209 std::cerr << "Failed to restore system VPD due to exception: "
210 << e.what() << std::endl;
211 }
212 // release the parser object
213 ParserFactory::freeParser(parser);
214}
215
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500216void Manager::listenHostState()
217{
Patrick Williams2eb01762022-07-22 19:26:56 -0500218 static std::shared_ptr<sdbusplus::bus::match_t> hostState =
219 std::make_shared<sdbusplus::bus::match_t>(
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600220 *conn,
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500221 sdbusplus::bus::match::rules::propertiesChanged(
222 "/xyz/openbmc_project/state/host0",
223 "xyz.openbmc_project.State.Host"),
Patrick Williams2eb01762022-07-22 19:26:56 -0500224 [this](sdbusplus::message_t& msg) { hostStateCallBack(msg); });
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500225}
226
Sunny Srivastavafdf9ff22022-06-15 11:15:54 -0500227void Manager::checkEssentialFrus()
228{
229 for (const auto& invPath : essentialFrus)
230 {
231 const auto res = readBusProperty(invPath, invItemIntf, "Present");
232
233 // implies the essential FRU is missing. Log PEL.
234 if (res == "false")
235 {
236 auto rc = sd_bus_call_method_async(
237 sdBus, NULL, loggerService, loggerObjectPath,
238 loggerCreateInterface, "Create", NULL, NULL, "ssa{ss}",
239 errIntfForEssentialFru,
240 "xyz.openbmc_project.Logging.Entry.Level.Warning", 2,
241 "DESCRIPTION", "Essential fru missing from the system.",
242 "CALLOUT_INVENTORY_PATH", (pimPath + invPath).c_str());
243
244 if (rc < 0)
245 {
246 log<level::ERR>("Error calling sd_bus_call_method_async",
247 entry("RC=%d", rc),
248 entry("MSG=%s", strerror(-rc)));
249 }
250 }
251 }
252}
253
Patrick Williams7a975f02022-12-07 03:19:53 -0600254void Manager::hostStateCallBack(sdbusplus::message_t& msg)
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500255{
256 if (msg.is_method_error())
257 {
258 std::cerr << "Error in reading signal " << std::endl;
259 }
260
261 Path object;
262 PropertyMap propMap;
263 msg.read(object, propMap);
264 const auto itr = propMap.find("CurrentHostState");
265 if (itr != propMap.end())
266 {
267 if (auto hostState = std::get_if<std::string>(&(itr->second)))
268 {
269 // implies system is moving from standby to power on state
270 if (*hostState == "xyz.openbmc_project.State.Host.HostState."
271 "TransitioningToRunning")
272 {
Sunny Srivastavafdf9ff22022-06-15 11:15:54 -0500273 // detect if essential frus are present in the system.
274 checkEssentialFrus();
275
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500276 // check and perfrom recollection for FRUs replaceable at
277 // standby.
278 performVPDRecollection();
279 return;
280 }
281 }
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500282 }
283}
284
Santosh Puranikb62f6052022-04-06 18:37:54 +0530285void Manager::listenAssetTag()
286{
Patrick Williams2eb01762022-07-22 19:26:56 -0500287 static std::shared_ptr<sdbusplus::bus::match_t> assetMatcher =
288 std::make_shared<sdbusplus::bus::match_t>(
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600289 *conn,
Santosh Puranikb62f6052022-04-06 18:37:54 +0530290 sdbusplus::bus::match::rules::propertiesChanged(
291 "/xyz/openbmc_project/inventory/system",
292 "xyz.openbmc_project.Inventory.Decorator.AssetTag"),
Patrick Williams2eb01762022-07-22 19:26:56 -0500293 [this](sdbusplus::message_t& msg) { assetTagCallback(msg); });
Santosh Puranikb62f6052022-04-06 18:37:54 +0530294}
295
Patrick Williams2eb01762022-07-22 19:26:56 -0500296void Manager::assetTagCallback(sdbusplus::message_t& msg)
Santosh Puranikb62f6052022-04-06 18:37:54 +0530297{
298 if (msg.is_method_error())
299 {
300 std::cerr << "Error in reading signal " << std::endl;
301 }
302
303 Path object;
304 PropertyMap propMap;
305 msg.read(object, propMap);
306 const auto itr = propMap.find("AssetTag");
307 if (itr != propMap.end())
308 {
309 if (auto assetTag = std::get_if<std::string>(&(itr->second)))
310 {
311 // Call Notify to persist the AssetTag
312 inventory::ObjectMap objectMap = {
313 {std::string{"/system"},
314 {{"xyz.openbmc_project.Inventory.Decorator.AssetTag",
315 {{"AssetTag", *assetTag}}}}}};
316
317 common::utility::callPIM(std::move(objectMap));
318 }
319 else
320 {
321 std::cerr << "Failed to read asset tag" << std::endl;
322 }
323 }
324}
325
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600326void Manager::processJSON()
327{
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530328 std::ifstream json(INVENTORY_JSON_SYM_LINK, std::ios::binary);
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600329
330 if (!json)
331 {
332 throw std::runtime_error("json file not found");
333 }
334
335 jsonFile = nlohmann::json::parse(json);
336 if (jsonFile.find("frus") == jsonFile.end())
337 {
338 throw std::runtime_error("frus group not found in json");
339 }
340
341 const nlohmann::json& groupFRUS =
342 jsonFile["frus"].get_ref<const nlohmann::json::object_t&>();
343 for (const auto& itemFRUS : groupFRUS.items())
344 {
345 const std::vector<nlohmann::json>& groupEEPROM =
346 itemFRUS.value().get_ref<const nlohmann::json::array_t&>();
347 for (const auto& itemEEPROM : groupEEPROM)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600348 {
SunnySrivastava198443306542020-04-01 02:50:20 -0500349 bool isMotherboard = false;
Santosh Puranika0b23912022-02-10 13:37:09 +0530350 std::string redundantPath;
351
SunnySrivastava198443306542020-04-01 02:50:20 -0500352 if (itemEEPROM["extraInterfaces"].find(
353 "xyz.openbmc_project.Inventory.Item.Board.Motherboard") !=
354 itemEEPROM["extraInterfaces"].end())
355 {
356 isMotherboard = true;
357 }
Santosh Puranika0b23912022-02-10 13:37:09 +0530358 if (itemEEPROM.find("redundantEeprom") != itemEEPROM.end())
359 {
360 redundantPath = itemEEPROM["redundantEeprom"]
361 .get_ref<const nlohmann::json::string_t&>();
362 }
363 frus.emplace(
364 itemEEPROM["inventoryPath"]
365 .get_ref<const nlohmann::json::string_t&>(),
366 std::make_tuple(itemFRUS.key(), redundantPath, isMotherboard));
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500367
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000368 if (itemEEPROM["extraInterfaces"].find(IBM_LOCATION_CODE_INF) !=
Alpana Kumari920408d2020-05-14 00:07:03 -0500369 itemEEPROM["extraInterfaces"].end())
370 {
371 fruLocationCode.emplace(
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000372 itemEEPROM["extraInterfaces"][IBM_LOCATION_CODE_INF]
Alpana Kumari920408d2020-05-14 00:07:03 -0500373 ["LocationCode"]
374 .get_ref<const nlohmann::json::string_t&>(),
375 itemEEPROM["inventoryPath"]
376 .get_ref<const nlohmann::json::string_t&>());
377 }
SunnySrivastava19849a195542020-09-07 06:04:50 -0500378
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500379 if (itemEEPROM.value("replaceableAtStandby", false))
SunnySrivastava19849a195542020-09-07 06:04:50 -0500380 {
381 replaceableFrus.emplace_back(itemFRUS.key());
382 }
Sunny Srivastavafdf9ff22022-06-15 11:15:54 -0500383
384 if (itemEEPROM.value("essentialFru", false))
385 {
386 essentialFrus.emplace_back(itemEEPROM["inventoryPath"]);
387 }
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600388 }
389 }
390}
391
Sunny Srivastava83770862023-10-31 12:57:20 -0500392void Manager::updateSystemVPDBackUpFRU(const std::string& recordName,
393 const std::string& keyword,
394 const Binary& value)
395{
396 const std::string& systemVpdBackupPath =
397 jsonFile["frus"][systemVpdFilePath].at(0).value("systemVpdBackupPath",
398 "");
399
400 if (!systemVpdBackupPath.empty() &&
401 jsonFile["frus"][systemVpdBackupPath].at(0).contains("inventoryPath"))
402 {
403 std::string systemVpdBackupInvPath =
404 jsonFile["frus"][systemVpdBackupPath][0]["inventoryPath"]
405 .get_ref<const nlohmann::json::string_t&>();
406
407 const auto& itr = svpdKwdMap.find(recordName);
408 if (itr != svpdKwdMap.end())
409 {
410 auto systemKwdInfoList = itr->second;
411 const auto& itrToKwd = find_if(systemKwdInfoList.begin(),
412 systemKwdInfoList.end(),
413 [&keyword](const auto& kwdInfo) {
414 return (keyword == std::get<0>(kwdInfo));
415 });
416
417 if (itrToKwd != systemKwdInfoList.end())
418 {
419 EditorImpl edit(systemVpdBackupPath, jsonFile,
420 std::get<4>(*itrToKwd), std::get<5>(*itrToKwd),
421 systemVpdBackupInvPath);
422
423 // Setup offset, if any
424 uint32_t offset = 0;
425 if (jsonFile["frus"][systemVpdBackupPath].at(0).contains(
426 "offset"))
427 {
428 offset =
429 jsonFile["frus"][systemVpdBackupPath].at(0).contains(
430 "offset");
431 }
432
433 edit.updateKeyword(value, offset, true);
434 }
435 }
436 }
437 else
438 {
439 if (systemVpdBackupPath.empty())
440 {
441 throw std::runtime_error(
442 "Invalid entry for systemVpdBackupPath in JSON");
443 }
444 else
445 {
446 throw std::runtime_error(
447 "Inventory path missing for systemVpdBackupPath");
448 }
449 }
450}
451
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600452void Manager::writeKeyword(const sdbusplus::message::object_path& path,
453 const std::string& recordName,
454 const std::string& keyword, const Binary& value)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600455{
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600456 try
457 {
Santosh Puranik8c796812021-12-01 19:17:56 +0530458 std::string objPath{path};
459 // Strip any inventory prefix in path
460 if (objPath.find(INVENTORY_PATH) == 0)
461 {
462 objPath = objPath.substr(sizeof(INVENTORY_PATH) - 1);
463 }
464
465 if (frus.find(objPath) == frus.end())
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600466 {
467 throw std::runtime_error("Inventory path not found");
468 }
469
Santosh Puranika0b23912022-02-10 13:37:09 +0530470 inventory::Path vpdFilePath = std::get<0>(frus.find(objPath)->second);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600471
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500472 // instantiate editor class to update the data
Santosh Puranik8c796812021-12-01 19:17:56 +0530473 EditorImpl edit(vpdFilePath, jsonFile, recordName, keyword, objPath);
Santosh Puranika0b23912022-02-10 13:37:09 +0530474
475 uint32_t offset = 0;
476 // Setup offset, if any
477 for (const auto& item : jsonFile["frus"][vpdFilePath])
478 {
479 if (item.find("offset") != item.end())
480 {
481 offset = item["offset"];
482 break;
483 }
484 }
485
486 edit.updateKeyword(value, offset, true);
487
Sunny Srivastava83770862023-10-31 12:57:20 -0500488 // If system VPD is being updated and system VPD is marked for back up
489 // on another FRU, update data on back up as well.
490 if (objPath == sdbusplus::message::object_path{SYSTEM_OBJECT} &&
491 jsonFile["frus"][systemVpdFilePath].at(0).contains(
492 "systemVpdBackupPath"))
493 {
494 updateSystemVPDBackUpFRU(recordName, keyword, value);
495 }
496
Santosh Puranika0b23912022-02-10 13:37:09 +0530497 // If we have a redundant EEPROM to update, then update just the EEPROM,
498 // not the cache since that is already done when we updated the primary
499 if (!std::get<1>(frus.find(objPath)->second).empty())
500 {
501 EditorImpl edit(std::get<1>(frus.find(objPath)->second), jsonFile,
Sunny Srivastavaf31a91b2022-06-09 08:11:29 -0500502 recordName, keyword, objPath);
Santosh Puranika0b23912022-02-10 13:37:09 +0530503 edit.updateKeyword(value, offset, false);
504 }
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600505
SunnySrivastava198443306542020-04-01 02:50:20 -0500506 // if it is a motehrboard FRU need to check for location expansion
Santosh Puranika0b23912022-02-10 13:37:09 +0530507 if (std::get<2>(frus.find(objPath)->second))
SunnySrivastava198443306542020-04-01 02:50:20 -0500508 {
509 if (recordName == "VCEN" && (keyword == "FC" || keyword == "SE"))
510 {
511 edit.expandLocationCode("fcs");
512 }
513 else if (recordName == "VSYS" &&
514 (keyword == "TM" || keyword == "SE"))
515 {
516 edit.expandLocationCode("mts");
517 }
518 }
519
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500520 return;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600521 }
522 catch (const std::exception& e)
523 {
524 std::cerr << e.what() << std::endl;
525 }
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600526}
527
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500528ListOfPaths
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600529 Manager::getFRUsByUnexpandedLocationCode(const LocationCode& locationCode,
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500530 const NodeNumber nodeNumber)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600531{
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500532 ReaderImpl read;
533 return read.getFrusAtLocation(locationCode, nodeNumber, fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600534}
535
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500536ListOfPaths
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600537 Manager::getFRUsByExpandedLocationCode(const LocationCode& locationCode)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600538{
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500539 ReaderImpl read;
540 return read.getFRUsByExpandedLocationCode(locationCode, fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600541}
542
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600543LocationCode Manager::getExpandedLocationCode(const LocationCode& locationCode,
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500544 const NodeNumber nodeNumber)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600545{
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500546 ReaderImpl read;
547 return read.getExpandedLocationCode(locationCode, nodeNumber,
548 fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600549}
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600550
SunnySrivastava19849a195542020-09-07 06:04:50 -0500551void Manager::performVPDRecollection()
552{
553 // get list of FRUs replaceable at standby
554 for (const auto& item : replaceableFrus)
555 {
556 const vector<nlohmann::json>& groupEEPROM = jsonFile["frus"][item];
557 const nlohmann::json& singleFru = groupEEPROM[0];
558
559 const string& inventoryPath =
560 singleFru["inventoryPath"]
561 .get_ref<const nlohmann::json::string_t&>();
562
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530563 bool prePostActionRequired = false;
564
565 if ((jsonFile["frus"][item].at(0)).find("preAction") !=
566 jsonFile["frus"][item].at(0).end())
567 {
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500568 try
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530569 {
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500570 if (!executePreAction(jsonFile, item))
571 {
572 // if the FRU has preAction defined then its execution
573 // should pass to ensure bind/unbind of data.
574 // preAction execution failed. should not call
575 // bind/unbind.
576 log<level::ERR>(
577 "Pre-Action execution failed for the FRU",
578 entry("ERROR=%s",
579 ("Inventory path: " + inventoryPath).c_str()));
580 continue;
581 }
582 }
583 catch (const GpioException& e)
584 {
585 log<level::ERR>(e.what());
586 PelAdditionalData additionalData{};
587 additionalData.emplace("DESCRIPTION", e.what());
588 createPEL(additionalData, PelSeverity::WARNING,
589 errIntfForGpioError, sdBus);
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530590 continue;
591 }
592 prePostActionRequired = true;
593 }
594
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500595 // unbind, bind the driver to trigger parser.
596 triggerVpdCollection(singleFru, inventoryPath);
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530597
598 // this check is added to avoid file system expensive call in case not
599 // required.
600 if (prePostActionRequired)
601 {
Sunny Srivastava4788e1b2024-03-19 02:11:07 -0500602 // The sleep of 1sec is sliced up in 10 retries of 10 miliseconds
603 // each.
604 for (auto retryCounter = VALUE_0; retryCounter <= VALUE_10;
605 retryCounter++)
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530606 {
Sunny Srivastava4788e1b2024-03-19 02:11:07 -0500607 // sleep for 10 milisecond
608 if (usleep(VALUE_100000) != VALUE_0)
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500609 {
Sunny Srivastava4788e1b2024-03-19 02:11:07 -0500610 std::cout << "Sleep failed before accessing the file"
611 << std::endl;
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500612 }
Alpana Kumari41d498c2021-09-16 00:29:12 -0500613
Sunny Srivastava4788e1b2024-03-19 02:11:07 -0500614 // Check if file showed up
615 if (!filesystem::exists(item))
616 {
617 // Do we need to retry?
618 if (retryCounter < VALUE_10)
619 {
620 continue;
621 }
622
623 try
624 {
625 // If not, then take failure postAction
626 executePostFailAction(jsonFile, item);
627 }
628 catch (const GpioException& e)
629 {
630 PelAdditionalData additionalData{};
631 additionalData.emplace("DESCRIPTION", e.what());
632 createPEL(additionalData, PelSeverity::WARNING,
633 errIntfForGpioError, sdBus);
634 }
635 }
636 else
637 {
638 // bind the LED driver
639 string chipAddr = singleFru.value("pcaChipAddress", "");
640 cout
641 << "performVPDRecollection: Executing driver binding for "
642 "chip "
643 "address - "
644 << chipAddr << endl;
645
646 executeCmd(createBindUnbindDriverCmnd(
647 chipAddr, "i2c", "leds-pca955x", "/bind"));
648
649 // File has been found, kill the retry loop.
650 break;
651 }
Alpana Kumari41d498c2021-09-16 00:29:12 -0500652 }
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530653 }
SunnySrivastava19849a195542020-09-07 06:04:50 -0500654 }
655}
656
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500657void Manager::collectFRUVPD(const sdbusplus::message::object_path& path)
658{
Sunny Srivastava5ef6ccc2022-05-30 01:35:13 -0500659 std::cout << "Manager called to collect vpd for fru: " << std::string{path}
660 << std::endl;
661
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500662 using InvalidArgument =
663 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
664 using Argument = xyz::openbmc_project::Common::InvalidArgument;
665
Santosh Puranikbc599472022-12-19 20:17:10 +0530666 std::string objPath{path};
667
668 // Strip any inventory prefix in path
669 if (objPath.find(INVENTORY_PATH) == 0)
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500670 {
Santosh Puranikbc599472022-12-19 20:17:10 +0530671 objPath = objPath.substr(sizeof(INVENTORY_PATH) - 1);
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500672 }
673
Santosh Puranikbc599472022-12-19 20:17:10 +0530674 // if path not found in Json.
675 if (frus.find(objPath) == frus.end())
676 {
677 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Object Path"),
678 Argument::ARGUMENT_VALUE(objPath.c_str()));
679 }
680
681 inventory::Path vpdFilePath = std::get<0>(frus.find(objPath)->second);
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500682
683 const std::vector<nlohmann::json>& groupEEPROM =
684 jsonFile["frus"][vpdFilePath].get_ref<const nlohmann::json::array_t&>();
685
Sunny Srivastavaab2304d2023-01-10 23:30:05 -0600686 nlohmann::json singleFru{};
687 for (const auto& item : groupEEPROM)
688 {
689 if (item["inventoryPath"] == objPath)
690 {
691 // this is the inventory we are looking for
692 singleFru = item;
693 break;
694 }
695 }
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500696
697 // check if the device qualifies for CM.
698 if (singleFru.value("concurrentlyMaintainable", false))
699 {
700 bool prePostActionRequired = false;
701
702 if ((jsonFile["frus"][vpdFilePath].at(0)).find("preAction") !=
703 jsonFile["frus"][vpdFilePath].at(0).end())
704 {
705 if (!executePreAction(jsonFile, vpdFilePath))
706 {
707 // if the FRU has preAction defined then its execution should
708 // pass to ensure bind/unbind of data.
709 // preAction execution failed. should not call bind/unbind.
710 log<level::ERR>("Pre-Action execution failed for the FRU");
711 return;
712 }
713
714 prePostActionRequired = true;
715 }
716
717 // unbind, bind the driver to trigger parser.
Santosh Puranikbc599472022-12-19 20:17:10 +0530718 triggerVpdCollection(singleFru, objPath);
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500719
720 // this check is added to avoid file system expensive call in case not
721 // required.
722 if (prePostActionRequired)
723 {
724 // Check if device showed up (test for file)
725 if (!filesystem::exists(vpdFilePath))
726 {
Santosh Puranikbc599472022-12-19 20:17:10 +0530727 try
728 {
729 // If not, then take failure postAction
730 executePostFailAction(jsonFile, vpdFilePath);
731 }
732 catch (const GpioException& e)
733 {
734 PelAdditionalData additionalData{};
735 additionalData.emplace("DESCRIPTION", e.what());
736 createPEL(additionalData, PelSeverity::WARNING,
737 errIntfForGpioError, sdBus);
738 }
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500739 }
Alpana Kumari41d498c2021-09-16 00:29:12 -0500740 else
741 {
742 // bind the LED driver
743 string chipAddr = jsonFile["frus"][vpdFilePath].at(0).value(
744 "pcaChipAddress", "");
745 cout << "Executing driver binding for chip address - "
746 << chipAddr << endl;
747
748 executeCmd(createBindUnbindDriverCmnd(chipAddr, "i2c",
749 "leds-pca955x", "/bind"));
750 }
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500751 }
752 return;
753 }
754 else
755 {
Santosh Puranikbc599472022-12-19 20:17:10 +0530756 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Object Path"),
757 Argument::ARGUMENT_VALUE(objPath.c_str()));
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500758 }
759}
760
761void Manager::triggerVpdCollection(const nlohmann::json& singleFru,
762 const std::string& path)
763{
764 if ((singleFru.find("devAddress") == singleFru.end()) ||
765 (singleFru.find("driverType") == singleFru.end()) ||
766 (singleFru.find("busType") == singleFru.end()))
767 {
768 // The FRUs is marked for collection but missing mandatory
769 // fields for collection. Log error and return.
770 log<level::ERR>(
771 "Collection Failed as mandatory field missing in Json",
772 entry("ERROR=%s", ("Recollection failed for " + (path)).c_str()));
773
774 return;
775 }
776
777 string deviceAddress = singleFru["devAddress"];
778 const string& driverType = singleFru["driverType"];
779 const string& busType = singleFru["busType"];
780
781 // devTreeStatus flag is present in json as false to mention
782 // that the EEPROM is not mentioned in device tree. If this flag
783 // is absent consider the value to be true, i.e EEPROM is
784 // mentioned in device tree
785 if (!singleFru.value("devTreeStatus", true))
786 {
787 auto pos = deviceAddress.find('-');
788 if (pos != string::npos)
789 {
790 string busNum = deviceAddress.substr(0, pos);
791 deviceAddress = "0x" + deviceAddress.substr(pos + 1, string::npos);
792
793 string deleteDevice = "echo" + deviceAddress + " > /sys/bus/" +
794 busType + "/devices/" + busType + "-" +
795 busNum + "/delete_device";
796 executeCmd(deleteDevice);
797
798 string addDevice = "echo" + driverType + " " + deviceAddress +
799 " > /sys/bus/" + busType + "/devices/" +
800 busType + "-" + busNum + "/new_device";
801 executeCmd(addDevice);
802 }
803 else
804 {
805 const string& inventoryPath =
806 singleFru["inventoryPath"]
807 .get_ref<const nlohmann::json::string_t&>();
808
809 log<level::ERR>(
810 "Wrong format of device address in Json",
811 entry("ERROR=%s",
812 ("Recollection failed for " + inventoryPath).c_str()));
813 }
814 }
815 else
816 {
817 executeCmd(createBindUnbindDriverCmnd(deviceAddress, busType,
818 driverType, "/unbind"));
819 executeCmd(createBindUnbindDriverCmnd(deviceAddress, busType,
820 driverType, "/bind"));
821 }
822}
823
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500824void Manager::deleteFRUVPD(const sdbusplus::message::object_path& path)
825{
Sunny Srivastava5ef6ccc2022-05-30 01:35:13 -0500826 std::cout << "Manager called to delete vpd for fru: " << std::string{path}
827 << std::endl;
828
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500829 using InvalidArgument =
830 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
831 using Argument = xyz::openbmc_project::Common::InvalidArgument;
832
Santosh Puranikbc599472022-12-19 20:17:10 +0530833 std::string objPath{path};
834
835 // Strip any inventory prefix in path
836 if (objPath.find(INVENTORY_PATH) == 0)
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500837 {
Santosh Puranikbc599472022-12-19 20:17:10 +0530838 objPath = objPath.substr(sizeof(INVENTORY_PATH) - 1);
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500839 }
840
Santosh Puranikbc599472022-12-19 20:17:10 +0530841 // if path not found in Json.
842 if (frus.find(objPath) == frus.end())
843 {
844 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Object Path"),
845 Argument::ARGUMENT_VALUE(objPath.c_str()));
846 }
847
848 inventory::Path& vpdFilePath = std::get<0>(frus.find(objPath)->second);
Alpana Kumari41d498c2021-09-16 00:29:12 -0500849
850 string chipAddress =
851 jsonFile["frus"][vpdFilePath].at(0).value("pcaChipAddress", "");
852
853 // Unbind the LED driver for this FRU
854 cout << "Unbinding device- " << chipAddress << endl;
855 executeCmd(createBindUnbindDriverCmnd(chipAddress, "i2c", "leds-pca955x",
856 "/unbind"));
857
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500858 // if the FRU is not present then log error
Santosh Puranikbc599472022-12-19 20:17:10 +0530859 if (readBusProperty(objPath, "xyz.openbmc_project.Inventory.Item",
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500860 "Present") == "false")
861 {
Santosh Puranikbc599472022-12-19 20:17:10 +0530862 elog<InvalidArgument>(Argument::ARGUMENT_NAME("FRU not preset"),
863 Argument::ARGUMENT_VALUE(objPath.c_str()));
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500864 }
865 else
866 {
Sunny Srivastavac6e7ea92023-11-03 21:10:43 +0530867 inventory::InterfaceMap interfacesPropMap;
868 clearVpdOnRemoval(INVENTORY_PATH + objPath, interfacesPropMap);
Santosh Puranikbc599472022-12-19 20:17:10 +0530869
870 inventory::ObjectMap objectMap;
Sunny Srivastavac6e7ea92023-11-03 21:10:43 +0530871 objectMap.emplace(objPath, move(interfacesPropMap));
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500872
873 common::utility::callPIM(move(objectMap));
874 }
875}
876
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600877} // namespace manager
878} // namespace vpd
Patrick Williamsc78d8872023-05-10 07:50:56 -0500879} // namespace openpower