blob: 50c4b904b68b6665d2b2453a7d2d6d9c8163969e [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
13#include <phosphor-logging/elog-errors.hpp>
Sunny Srivastava28abd6e2021-07-28 02:58:28 -050014#include <xyz/openbmc_project/Common/error.hpp>
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060015
Patrick Williamsc78d8872023-05-10 07:50:56 -050016#include <filesystem>
17
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060018using namespace openpower::vpd::constants;
SunnySrivastava19841356d7e2020-04-24 04:29:35 -050019using namespace openpower::vpd::inventory;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060020using namespace openpower::vpd::manager::editor;
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -050021using namespace openpower::vpd::manager::reader;
SunnySrivastava19849a195542020-09-07 06:04:50 -050022using namespace std;
23using namespace openpower::vpd::parser;
Santosh Puranik6b2b5372022-06-02 20:49:02 +053024using namespace openpower::vpd::parser::factory;
25using namespace openpower::vpd::ipz::parser;
SunnySrivastava19849a195542020-09-07 06:04:50 -050026using namespace openpower::vpd::exceptions;
27using namespace phosphor::logging;
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060028
29namespace openpower
30{
31namespace vpd
32{
33namespace manager
34{
Sunny Srivastava523af2e2022-02-14 07:30:10 -060035Manager::Manager(std::shared_ptr<boost::asio::io_context>& ioCon,
36 std::shared_ptr<sdbusplus::asio::dbus_interface>& iFace,
37 std::shared_ptr<sdbusplus::asio::connection>& conn) :
38 ioContext(ioCon),
39 interface(iFace), conn(conn)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060040{
Sunny Srivastava523af2e2022-02-14 07:30:10 -060041 interface->register_method(
42 "WriteKeyword",
43 [this](const sdbusplus::message::object_path& path,
44 const std::string& recordName, const std::string& keyword,
45 const Binary& value) {
Patrick Williamsc78d8872023-05-10 07:50:56 -050046 this->writeKeyword(path, recordName, keyword, value);
Sunny Srivastava523af2e2022-02-14 07:30:10 -060047 });
48
49 interface->register_method(
50 "GetFRUsByUnexpandedLocationCode",
51 [this](const std::string& locationCode,
52 const uint16_t nodeNumber) -> inventory::ListOfPaths {
53 return this->getFRUsByUnexpandedLocationCode(locationCode,
54 nodeNumber);
55 });
56
57 interface->register_method(
58 "GetFRUsByExpandedLocationCode",
59 [this](const std::string& locationCode) -> inventory::ListOfPaths {
60 return this->getFRUsByExpandedLocationCode(locationCode);
61 });
62
63 interface->register_method(
64 "GetExpandedLocationCode",
65 [this](const std::string& locationCode,
66 const uint16_t nodeNumber) -> std::string {
67 return this->getExpandedLocationCode(locationCode, nodeNumber);
68 });
69
70 interface->register_method("PerformVPDRecollection",
71 [this]() { this->performVPDRecollection(); });
72
Sunny Srivastava28abd6e2021-07-28 02:58:28 -050073 interface->register_method(
74 "deleteFRUVPD", [this](const sdbusplus::message::object_path& path) {
75 this->deleteFRUVPD(path);
76 });
77
Sunny Srivastava6a1bd392021-06-02 04:39:24 -050078 interface->register_method(
79 "CollectFRUVPD", [this](const sdbusplus::message::object_path& path) {
80 this->collectFRUVPD(path);
81 });
82
Sunny Srivastavafdf9ff22022-06-15 11:15:54 -050083 sd_bus_default(&sdBus);
Sunny Srivastava523af2e2022-02-14 07:30:10 -060084 initManager();
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060085}
86
Sunny Srivastava523af2e2022-02-14 07:30:10 -060087void Manager::initManager()
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060088{
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -060089 try
90 {
91 processJSON();
Santosh Puranik6b2b5372022-06-02 20:49:02 +053092 restoreSystemVpd();
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -050093 listenHostState();
Santosh Puranikb62f6052022-04-06 18:37:54 +053094 listenAssetTag();
Alpana Kumarib17dd3b2020-10-01 00:18:10 -050095
Santosh Puranikf2d3b532022-04-19 06:44:07 -050096 // Create an instance of the BIOS handler
Sunny Srivastava523af2e2022-02-14 07:30:10 -060097 biosHandler = std::make_shared<BiosHandler>(conn, *this);
Santosh Puranikf2d3b532022-04-19 06:44:07 -050098
Sunny Srivastava523af2e2022-02-14 07:30:10 -060099 // instantiate gpioMonitor class
100 gpioMon = std::make_shared<GpioMonitor>(jsonFile, ioContext);
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600101 }
102 catch (const std::exception& e)
103 {
104 std::cerr << e.what() << "\n";
105 }
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600106}
107
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530108/**
109 * @brief An api to get list of blank system VPD properties.
110 * @param[in] vpdMap - IPZ vpd map.
111 * @param[in] objectPath - Object path for the FRU.
112 * @param[out] blankPropertyList - Properties which are blank in System VPD and
113 * needs to be updated as standby.
114 */
115static void
116 getListOfBlankSystemVpd(Parsed& vpdMap, const string& objectPath,
117 std::vector<RestoredEeproms>& blankPropertyList)
118{
119 for (const auto& systemRecKwdPair : svpdKwdMap)
120 {
121 auto it = vpdMap.find(systemRecKwdPair.first);
122
123 // check if record is found in map we got by parser
124 if (it != vpdMap.end())
125 {
126 const auto& kwdListForRecord = systemRecKwdPair.second;
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600127 for (const auto& keywordInfo : kwdListForRecord)
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530128 {
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600129 const auto& keyword = get<0>(keywordInfo);
130
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530131 DbusPropertyMap& kwdValMap = it->second;
132 auto iterator = kwdValMap.find(keyword);
133
134 if (iterator != kwdValMap.end())
135 {
136 string& kwdValue = iterator->second;
137
138 // check bus data
139 const string& recordName = systemRecKwdPair.first;
140 const string& busValue = readBusProperty(
141 objectPath, ipzVpdInf + recordName, keyword);
142
Priyanga Ramasamy834c0782023-02-14 12:22:39 -0600143 const auto& defaultValue = get<1>(keywordInfo);
144
145 if (Binary(busValue.begin(), busValue.end()) !=
146 defaultValue)
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530147 {
Priyanga Ramasamy834c0782023-02-14 12:22:39 -0600148 if (Binary(kwdValue.begin(), kwdValue.end()) ==
149 defaultValue)
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530150 {
151 // implies data is blank on EEPROM but not on cache.
152 // So EEPROM vpd update is required.
153 Binary busData(busValue.begin(), busValue.end());
154
155 blankPropertyList.push_back(std::make_tuple(
156 objectPath, recordName, keyword, busData));
157 }
158 }
159 }
160 }
161 }
162 }
163}
164
165void Manager::restoreSystemVpd()
166{
167 std::cout << "Attempting system VPD restore" << std::endl;
168 ParserInterface* parser = nullptr;
169 try
170 {
171 auto vpdVector = getVpdDataInVector(jsonFile, systemVpdFilePath);
girik18bb9852022-11-16 05:48:13 -0600172 uint32_t vpdStartOffset = 0;
Sunny Srivastavaf31a91b2022-06-09 08:11:29 -0500173 const auto& inventoryPath =
174 jsonFile["frus"][systemVpdFilePath][0]["inventoryPath"]
175 .get_ref<const nlohmann::json::string_t&>();
176
girik18bb9852022-11-16 05:48:13 -0600177 parser = ParserFactory::getParser(vpdVector, (pimPath + inventoryPath),
178 systemVpdFilePath, vpdStartOffset);
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530179 auto parseResult = parser->parse();
180
181 if (auto pVal = std::get_if<Store>(&parseResult))
182 {
183 // map to hold all the keywords whose value is blank and
184 // needs to be updated at standby.
185 std::vector<RestoredEeproms> blankSystemVpdProperties{};
186 getListOfBlankSystemVpd(pVal->getVpdMap(), SYSTEM_OBJECT,
187 blankSystemVpdProperties);
188
189 // if system VPD restore is required, update the
190 // EEPROM
191 for (const auto& item : blankSystemVpdProperties)
192 {
193 std::cout << "Restoring keyword: " << std::get<2>(item)
194 << std::endl;
195 writeKeyword(std::get<0>(item), std::get<1>(item),
196 std::get<2>(item), std::get<3>(item));
197 }
198 }
199 else
200 {
201 std::cerr << "Not a valid format to restore system VPD"
202 << std::endl;
203 }
204 }
205 catch (const std::exception& e)
206 {
207 std::cerr << "Failed to restore system VPD due to exception: "
208 << e.what() << std::endl;
209 }
210 // release the parser object
211 ParserFactory::freeParser(parser);
212}
213
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500214void Manager::listenHostState()
215{
Patrick Williams2eb01762022-07-22 19:26:56 -0500216 static std::shared_ptr<sdbusplus::bus::match_t> hostState =
217 std::make_shared<sdbusplus::bus::match_t>(
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600218 *conn,
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500219 sdbusplus::bus::match::rules::propertiesChanged(
220 "/xyz/openbmc_project/state/host0",
221 "xyz.openbmc_project.State.Host"),
Patrick Williams2eb01762022-07-22 19:26:56 -0500222 [this](sdbusplus::message_t& msg) { hostStateCallBack(msg); });
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500223}
224
Sunny Srivastavafdf9ff22022-06-15 11:15:54 -0500225void Manager::checkEssentialFrus()
226{
227 for (const auto& invPath : essentialFrus)
228 {
229 const auto res = readBusProperty(invPath, invItemIntf, "Present");
230
231 // implies the essential FRU is missing. Log PEL.
232 if (res == "false")
233 {
234 auto rc = sd_bus_call_method_async(
235 sdBus, NULL, loggerService, loggerObjectPath,
236 loggerCreateInterface, "Create", NULL, NULL, "ssa{ss}",
237 errIntfForEssentialFru,
238 "xyz.openbmc_project.Logging.Entry.Level.Warning", 2,
239 "DESCRIPTION", "Essential fru missing from the system.",
240 "CALLOUT_INVENTORY_PATH", (pimPath + invPath).c_str());
241
242 if (rc < 0)
243 {
244 log<level::ERR>("Error calling sd_bus_call_method_async",
245 entry("RC=%d", rc),
246 entry("MSG=%s", strerror(-rc)));
247 }
248 }
249 }
250}
251
Patrick Williams7a975f02022-12-07 03:19:53 -0600252void Manager::hostStateCallBack(sdbusplus::message_t& msg)
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500253{
254 if (msg.is_method_error())
255 {
256 std::cerr << "Error in reading signal " << std::endl;
257 }
258
259 Path object;
260 PropertyMap propMap;
261 msg.read(object, propMap);
262 const auto itr = propMap.find("CurrentHostState");
263 if (itr != propMap.end())
264 {
265 if (auto hostState = std::get_if<std::string>(&(itr->second)))
266 {
267 // implies system is moving from standby to power on state
268 if (*hostState == "xyz.openbmc_project.State.Host.HostState."
269 "TransitioningToRunning")
270 {
Sunny Srivastavafdf9ff22022-06-15 11:15:54 -0500271 // detect if essential frus are present in the system.
272 checkEssentialFrus();
273
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500274 // check and perfrom recollection for FRUs replaceable at
275 // standby.
276 performVPDRecollection();
277 return;
278 }
279 }
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500280 }
281}
282
Santosh Puranikb62f6052022-04-06 18:37:54 +0530283void Manager::listenAssetTag()
284{
Patrick Williams2eb01762022-07-22 19:26:56 -0500285 static std::shared_ptr<sdbusplus::bus::match_t> assetMatcher =
286 std::make_shared<sdbusplus::bus::match_t>(
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600287 *conn,
Santosh Puranikb62f6052022-04-06 18:37:54 +0530288 sdbusplus::bus::match::rules::propertiesChanged(
289 "/xyz/openbmc_project/inventory/system",
290 "xyz.openbmc_project.Inventory.Decorator.AssetTag"),
Patrick Williams2eb01762022-07-22 19:26:56 -0500291 [this](sdbusplus::message_t& msg) { assetTagCallback(msg); });
Santosh Puranikb62f6052022-04-06 18:37:54 +0530292}
293
Patrick Williams2eb01762022-07-22 19:26:56 -0500294void Manager::assetTagCallback(sdbusplus::message_t& msg)
Santosh Puranikb62f6052022-04-06 18:37:54 +0530295{
296 if (msg.is_method_error())
297 {
298 std::cerr << "Error in reading signal " << std::endl;
299 }
300
301 Path object;
302 PropertyMap propMap;
303 msg.read(object, propMap);
304 const auto itr = propMap.find("AssetTag");
305 if (itr != propMap.end())
306 {
307 if (auto assetTag = std::get_if<std::string>(&(itr->second)))
308 {
309 // Call Notify to persist the AssetTag
310 inventory::ObjectMap objectMap = {
311 {std::string{"/system"},
312 {{"xyz.openbmc_project.Inventory.Decorator.AssetTag",
313 {{"AssetTag", *assetTag}}}}}};
314
315 common::utility::callPIM(std::move(objectMap));
316 }
317 else
318 {
319 std::cerr << "Failed to read asset tag" << std::endl;
320 }
321 }
322}
323
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600324void Manager::processJSON()
325{
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530326 std::ifstream json(INVENTORY_JSON_SYM_LINK, std::ios::binary);
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600327
328 if (!json)
329 {
330 throw std::runtime_error("json file not found");
331 }
332
333 jsonFile = nlohmann::json::parse(json);
334 if (jsonFile.find("frus") == jsonFile.end())
335 {
336 throw std::runtime_error("frus group not found in json");
337 }
338
339 const nlohmann::json& groupFRUS =
340 jsonFile["frus"].get_ref<const nlohmann::json::object_t&>();
341 for (const auto& itemFRUS : groupFRUS.items())
342 {
343 const std::vector<nlohmann::json>& groupEEPROM =
344 itemFRUS.value().get_ref<const nlohmann::json::array_t&>();
345 for (const auto& itemEEPROM : groupEEPROM)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600346 {
SunnySrivastava198443306542020-04-01 02:50:20 -0500347 bool isMotherboard = false;
Santosh Puranika0b23912022-02-10 13:37:09 +0530348 std::string redundantPath;
349
SunnySrivastava198443306542020-04-01 02:50:20 -0500350 if (itemEEPROM["extraInterfaces"].find(
351 "xyz.openbmc_project.Inventory.Item.Board.Motherboard") !=
352 itemEEPROM["extraInterfaces"].end())
353 {
354 isMotherboard = true;
355 }
Santosh Puranika0b23912022-02-10 13:37:09 +0530356 if (itemEEPROM.find("redundantEeprom") != itemEEPROM.end())
357 {
358 redundantPath = itemEEPROM["redundantEeprom"]
359 .get_ref<const nlohmann::json::string_t&>();
360 }
361 frus.emplace(
362 itemEEPROM["inventoryPath"]
363 .get_ref<const nlohmann::json::string_t&>(),
364 std::make_tuple(itemFRUS.key(), redundantPath, isMotherboard));
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500365
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000366 if (itemEEPROM["extraInterfaces"].find(IBM_LOCATION_CODE_INF) !=
Alpana Kumari920408d2020-05-14 00:07:03 -0500367 itemEEPROM["extraInterfaces"].end())
368 {
369 fruLocationCode.emplace(
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000370 itemEEPROM["extraInterfaces"][IBM_LOCATION_CODE_INF]
Alpana Kumari920408d2020-05-14 00:07:03 -0500371 ["LocationCode"]
372 .get_ref<const nlohmann::json::string_t&>(),
373 itemEEPROM["inventoryPath"]
374 .get_ref<const nlohmann::json::string_t&>());
375 }
SunnySrivastava19849a195542020-09-07 06:04:50 -0500376
Sunny Srivastavaecb5c7d2021-09-02 07:20:24 -0500377 if (itemEEPROM.value("replaceableAtStandby", false))
SunnySrivastava19849a195542020-09-07 06:04:50 -0500378 {
379 replaceableFrus.emplace_back(itemFRUS.key());
380 }
Sunny Srivastavafdf9ff22022-06-15 11:15:54 -0500381
382 if (itemEEPROM.value("essentialFru", false))
383 {
384 essentialFrus.emplace_back(itemEEPROM["inventoryPath"]);
385 }
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600386 }
387 }
388}
389
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600390void Manager::writeKeyword(const sdbusplus::message::object_path& path,
391 const std::string& recordName,
392 const std::string& keyword, const Binary& value)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600393{
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600394 try
395 {
Santosh Puranik8c796812021-12-01 19:17:56 +0530396 std::string objPath{path};
397 // Strip any inventory prefix in path
398 if (objPath.find(INVENTORY_PATH) == 0)
399 {
400 objPath = objPath.substr(sizeof(INVENTORY_PATH) - 1);
401 }
402
403 if (frus.find(objPath) == frus.end())
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600404 {
405 throw std::runtime_error("Inventory path not found");
406 }
407
Santosh Puranika0b23912022-02-10 13:37:09 +0530408 inventory::Path vpdFilePath = std::get<0>(frus.find(objPath)->second);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600409
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500410 // instantiate editor class to update the data
Santosh Puranik8c796812021-12-01 19:17:56 +0530411 EditorImpl edit(vpdFilePath, jsonFile, recordName, keyword, objPath);
Santosh Puranika0b23912022-02-10 13:37:09 +0530412
413 uint32_t offset = 0;
414 // Setup offset, if any
415 for (const auto& item : jsonFile["frus"][vpdFilePath])
416 {
417 if (item.find("offset") != item.end())
418 {
419 offset = item["offset"];
420 break;
421 }
422 }
423
424 edit.updateKeyword(value, offset, true);
425
426 // If we have a redundant EEPROM to update, then update just the EEPROM,
427 // not the cache since that is already done when we updated the primary
428 if (!std::get<1>(frus.find(objPath)->second).empty())
429 {
430 EditorImpl edit(std::get<1>(frus.find(objPath)->second), jsonFile,
Sunny Srivastavaf31a91b2022-06-09 08:11:29 -0500431 recordName, keyword, objPath);
Santosh Puranika0b23912022-02-10 13:37:09 +0530432 edit.updateKeyword(value, offset, false);
433 }
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600434
SunnySrivastava198443306542020-04-01 02:50:20 -0500435 // if it is a motehrboard FRU need to check for location expansion
Santosh Puranika0b23912022-02-10 13:37:09 +0530436 if (std::get<2>(frus.find(objPath)->second))
SunnySrivastava198443306542020-04-01 02:50:20 -0500437 {
438 if (recordName == "VCEN" && (keyword == "FC" || keyword == "SE"))
439 {
440 edit.expandLocationCode("fcs");
441 }
442 else if (recordName == "VSYS" &&
443 (keyword == "TM" || keyword == "SE"))
444 {
445 edit.expandLocationCode("mts");
446 }
447 }
448
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500449 return;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600450 }
451 catch (const std::exception& e)
452 {
453 std::cerr << e.what() << std::endl;
454 }
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600455}
456
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500457ListOfPaths
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600458 Manager::getFRUsByUnexpandedLocationCode(const LocationCode& locationCode,
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500459 const NodeNumber nodeNumber)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600460{
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500461 ReaderImpl read;
462 return read.getFrusAtLocation(locationCode, nodeNumber, fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600463}
464
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500465ListOfPaths
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600466 Manager::getFRUsByExpandedLocationCode(const LocationCode& locationCode)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600467{
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500468 ReaderImpl read;
469 return read.getFRUsByExpandedLocationCode(locationCode, fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600470}
471
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600472LocationCode Manager::getExpandedLocationCode(const LocationCode& locationCode,
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500473 const NodeNumber nodeNumber)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600474{
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500475 ReaderImpl read;
476 return read.getExpandedLocationCode(locationCode, nodeNumber,
477 fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600478}
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600479
SunnySrivastava19849a195542020-09-07 06:04:50 -0500480void Manager::performVPDRecollection()
481{
482 // get list of FRUs replaceable at standby
483 for (const auto& item : replaceableFrus)
484 {
485 const vector<nlohmann::json>& groupEEPROM = jsonFile["frus"][item];
486 const nlohmann::json& singleFru = groupEEPROM[0];
487
488 const string& inventoryPath =
489 singleFru["inventoryPath"]
490 .get_ref<const nlohmann::json::string_t&>();
491
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530492 bool prePostActionRequired = false;
493
494 if ((jsonFile["frus"][item].at(0)).find("preAction") !=
495 jsonFile["frus"][item].at(0).end())
496 {
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500497 try
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530498 {
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500499 if (!executePreAction(jsonFile, item))
500 {
501 // if the FRU has preAction defined then its execution
502 // should pass to ensure bind/unbind of data.
503 // preAction execution failed. should not call
504 // bind/unbind.
505 log<level::ERR>(
506 "Pre-Action execution failed for the FRU",
507 entry("ERROR=%s",
508 ("Inventory path: " + inventoryPath).c_str()));
509 continue;
510 }
511 }
512 catch (const GpioException& e)
513 {
514 log<level::ERR>(e.what());
515 PelAdditionalData additionalData{};
516 additionalData.emplace("DESCRIPTION", e.what());
517 createPEL(additionalData, PelSeverity::WARNING,
518 errIntfForGpioError, sdBus);
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530519 continue;
520 }
521 prePostActionRequired = true;
522 }
523
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500524 // unbind, bind the driver to trigger parser.
525 triggerVpdCollection(singleFru, inventoryPath);
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530526
527 // this check is added to avoid file system expensive call in case not
528 // required.
529 if (prePostActionRequired)
530 {
531 // Check if device showed up (test for file)
532 if (!filesystem::exists(item))
533 {
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500534 try
535 {
536 // If not, then take failure postAction
537 executePostFailAction(jsonFile, item);
538 }
539 catch (const GpioException& e)
540 {
541 PelAdditionalData additionalData{};
542 additionalData.emplace("DESCRIPTION", e.what());
543 createPEL(additionalData, PelSeverity::WARNING,
544 errIntfForGpioError, sdBus);
545 }
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530546 }
Alpana Kumari41d498c2021-09-16 00:29:12 -0500547 else
548 {
549 // bind the LED driver
550 string chipAddr = singleFru.value("pcaChipAddress", "");
551 cout << "performVPDRecollection: Executing driver binding for "
552 "chip "
553 "address - "
554 << chipAddr << endl;
555
556 executeCmd(createBindUnbindDriverCmnd(chipAddr, "i2c",
557 "leds-pca955x", "/bind"));
558 }
Santosh Puranikd40e42d2022-03-23 13:58:06 +0530559 }
SunnySrivastava19849a195542020-09-07 06:04:50 -0500560 }
561}
562
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500563void Manager::collectFRUVPD(const sdbusplus::message::object_path& path)
564{
Sunny Srivastava5ef6ccc2022-05-30 01:35:13 -0500565 std::cout << "Manager called to collect vpd for fru: " << std::string{path}
566 << std::endl;
567
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500568 using InvalidArgument =
569 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
570 using Argument = xyz::openbmc_project::Common::InvalidArgument;
571
Santosh Puranikbc599472022-12-19 20:17:10 +0530572 std::string objPath{path};
573
574 // Strip any inventory prefix in path
575 if (objPath.find(INVENTORY_PATH) == 0)
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500576 {
Santosh Puranikbc599472022-12-19 20:17:10 +0530577 objPath = objPath.substr(sizeof(INVENTORY_PATH) - 1);
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500578 }
579
Santosh Puranikbc599472022-12-19 20:17:10 +0530580 // if path not found in Json.
581 if (frus.find(objPath) == frus.end())
582 {
583 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Object Path"),
584 Argument::ARGUMENT_VALUE(objPath.c_str()));
585 }
586
587 inventory::Path vpdFilePath = std::get<0>(frus.find(objPath)->second);
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500588
589 const std::vector<nlohmann::json>& groupEEPROM =
590 jsonFile["frus"][vpdFilePath].get_ref<const nlohmann::json::array_t&>();
591
Sunny Srivastavaab2304d2023-01-10 23:30:05 -0600592 nlohmann::json singleFru{};
593 for (const auto& item : groupEEPROM)
594 {
595 if (item["inventoryPath"] == objPath)
596 {
597 // this is the inventory we are looking for
598 singleFru = item;
599 break;
600 }
601 }
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500602
603 // check if the device qualifies for CM.
604 if (singleFru.value("concurrentlyMaintainable", false))
605 {
606 bool prePostActionRequired = false;
607
608 if ((jsonFile["frus"][vpdFilePath].at(0)).find("preAction") !=
609 jsonFile["frus"][vpdFilePath].at(0).end())
610 {
611 if (!executePreAction(jsonFile, vpdFilePath))
612 {
613 // if the FRU has preAction defined then its execution should
614 // pass to ensure bind/unbind of data.
615 // preAction execution failed. should not call bind/unbind.
616 log<level::ERR>("Pre-Action execution failed for the FRU");
617 return;
618 }
619
620 prePostActionRequired = true;
621 }
622
623 // unbind, bind the driver to trigger parser.
Santosh Puranikbc599472022-12-19 20:17:10 +0530624 triggerVpdCollection(singleFru, objPath);
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500625
626 // this check is added to avoid file system expensive call in case not
627 // required.
628 if (prePostActionRequired)
629 {
630 // Check if device showed up (test for file)
631 if (!filesystem::exists(vpdFilePath))
632 {
Santosh Puranikbc599472022-12-19 20:17:10 +0530633 try
634 {
635 // If not, then take failure postAction
636 executePostFailAction(jsonFile, vpdFilePath);
637 }
638 catch (const GpioException& e)
639 {
640 PelAdditionalData additionalData{};
641 additionalData.emplace("DESCRIPTION", e.what());
642 createPEL(additionalData, PelSeverity::WARNING,
643 errIntfForGpioError, sdBus);
644 }
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500645 }
Alpana Kumari41d498c2021-09-16 00:29:12 -0500646 else
647 {
648 // bind the LED driver
649 string chipAddr = jsonFile["frus"][vpdFilePath].at(0).value(
650 "pcaChipAddress", "");
651 cout << "Executing driver binding for chip address - "
652 << chipAddr << endl;
653
654 executeCmd(createBindUnbindDriverCmnd(chipAddr, "i2c",
655 "leds-pca955x", "/bind"));
656 }
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500657 }
658 return;
659 }
660 else
661 {
Santosh Puranikbc599472022-12-19 20:17:10 +0530662 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Object Path"),
663 Argument::ARGUMENT_VALUE(objPath.c_str()));
Sunny Srivastava6a1bd392021-06-02 04:39:24 -0500664 }
665}
666
667void Manager::triggerVpdCollection(const nlohmann::json& singleFru,
668 const std::string& path)
669{
670 if ((singleFru.find("devAddress") == singleFru.end()) ||
671 (singleFru.find("driverType") == singleFru.end()) ||
672 (singleFru.find("busType") == singleFru.end()))
673 {
674 // The FRUs is marked for collection but missing mandatory
675 // fields for collection. Log error and return.
676 log<level::ERR>(
677 "Collection Failed as mandatory field missing in Json",
678 entry("ERROR=%s", ("Recollection failed for " + (path)).c_str()));
679
680 return;
681 }
682
683 string deviceAddress = singleFru["devAddress"];
684 const string& driverType = singleFru["driverType"];
685 const string& busType = singleFru["busType"];
686
687 // devTreeStatus flag is present in json as false to mention
688 // that the EEPROM is not mentioned in device tree. If this flag
689 // is absent consider the value to be true, i.e EEPROM is
690 // mentioned in device tree
691 if (!singleFru.value("devTreeStatus", true))
692 {
693 auto pos = deviceAddress.find('-');
694 if (pos != string::npos)
695 {
696 string busNum = deviceAddress.substr(0, pos);
697 deviceAddress = "0x" + deviceAddress.substr(pos + 1, string::npos);
698
699 string deleteDevice = "echo" + deviceAddress + " > /sys/bus/" +
700 busType + "/devices/" + busType + "-" +
701 busNum + "/delete_device";
702 executeCmd(deleteDevice);
703
704 string addDevice = "echo" + driverType + " " + deviceAddress +
705 " > /sys/bus/" + busType + "/devices/" +
706 busType + "-" + busNum + "/new_device";
707 executeCmd(addDevice);
708 }
709 else
710 {
711 const string& inventoryPath =
712 singleFru["inventoryPath"]
713 .get_ref<const nlohmann::json::string_t&>();
714
715 log<level::ERR>(
716 "Wrong format of device address in Json",
717 entry("ERROR=%s",
718 ("Recollection failed for " + inventoryPath).c_str()));
719 }
720 }
721 else
722 {
723 executeCmd(createBindUnbindDriverCmnd(deviceAddress, busType,
724 driverType, "/unbind"));
725 executeCmd(createBindUnbindDriverCmnd(deviceAddress, busType,
726 driverType, "/bind"));
727 }
728}
729
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500730void Manager::deleteFRUVPD(const sdbusplus::message::object_path& path)
731{
Sunny Srivastava5ef6ccc2022-05-30 01:35:13 -0500732 std::cout << "Manager called to delete vpd for fru: " << std::string{path}
733 << std::endl;
734
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500735 using InvalidArgument =
736 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
737 using Argument = xyz::openbmc_project::Common::InvalidArgument;
738
Santosh Puranikbc599472022-12-19 20:17:10 +0530739 std::string objPath{path};
740
741 // Strip any inventory prefix in path
742 if (objPath.find(INVENTORY_PATH) == 0)
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500743 {
Santosh Puranikbc599472022-12-19 20:17:10 +0530744 objPath = objPath.substr(sizeof(INVENTORY_PATH) - 1);
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500745 }
746
Santosh Puranikbc599472022-12-19 20:17:10 +0530747 // if path not found in Json.
748 if (frus.find(objPath) == frus.end())
749 {
750 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Object Path"),
751 Argument::ARGUMENT_VALUE(objPath.c_str()));
752 }
753
754 inventory::Path& vpdFilePath = std::get<0>(frus.find(objPath)->second);
Alpana Kumari41d498c2021-09-16 00:29:12 -0500755
756 string chipAddress =
757 jsonFile["frus"][vpdFilePath].at(0).value("pcaChipAddress", "");
758
759 // Unbind the LED driver for this FRU
760 cout << "Unbinding device- " << chipAddress << endl;
761 executeCmd(createBindUnbindDriverCmnd(chipAddress, "i2c", "leds-pca955x",
762 "/unbind"));
763
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500764 // if the FRU is not present then log error
Santosh Puranikbc599472022-12-19 20:17:10 +0530765 if (readBusProperty(objPath, "xyz.openbmc_project.Inventory.Item",
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500766 "Present") == "false")
767 {
Santosh Puranikbc599472022-12-19 20:17:10 +0530768 elog<InvalidArgument>(Argument::ARGUMENT_NAME("FRU not preset"),
769 Argument::ARGUMENT_VALUE(objPath.c_str()));
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500770 }
771 else
772 {
Santosh Puranikbc599472022-12-19 20:17:10 +0530773 // Set present property of FRU as false as it has been removed.
774 // CC data for FRU is also removed as
775 // a) FRU is not there so CC does not make sense.
776 // b) Sensors dependent on Panel uses CC data.
777 inventory::InterfaceMap interfaces{
778 {"xyz.openbmc_project.Inventory.Item", {{"Present", false}}},
779 {"com.ibm.ipzvpd.VINI", {{"CC", Binary{}}}}};
780
781 inventory::ObjectMap objectMap;
782 objectMap.emplace(objPath, move(interfaces));
Sunny Srivastava28abd6e2021-07-28 02:58:28 -0500783
784 common::utility::callPIM(move(objectMap));
785 }
786}
787
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600788} // namespace manager
789} // namespace vpd
Patrick Williamsc78d8872023-05-10 07:50:56 -0500790} // namespace openpower