blob: 599b63fbc86b43d6ed146cb2350299c9937ff31d [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
2 * Copyright © 2019 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matt Spinlercad9c2b2019-12-02 15:42:01 -060016
Matt Spinlerc8705e22019-09-11 12:36:07 -050017#include "data_interface.hpp"
18
Matt Spinlerf61f2922020-06-23 11:32:49 -050019#include "util.hpp"
20
Matt Spinlera167a7d2023-06-30 15:14:25 -050021#include <phosphor-logging/lg2.hpp>
Matt Spinler5b423652023-05-04 13:08:44 -050022#include <xyz/openbmc_project/State/BMC/server.hpp>
Matt Spinlerf10068d2020-12-02 10:44:08 -060023#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
Matt Spinlera7d9d962019-11-06 15:01:25 -060024
Arya K Padmanafba3162024-08-30 07:41:32 -050025#include <filesystem>
26
Arya K Padmand8ae6182024-07-19 06:25:10 -050027#ifdef PEL_ENABLE_PHAL
Arya K Padmanafba3162024-08-30 07:41:32 -050028#include <libekb.H>
29#include <libpdbg.h>
Arya K Padmand8ae6182024-07-19 06:25:10 -050030#include <libphal.H>
31#endif
32
Matt Spinler35a405b2022-03-02 11:42:42 -060033// Use a timeout of 10s for D-Bus calls so if there are
34// timeouts the callers of the PEL creation method won't
35// also timeout.
36constexpr auto dbusTimeout = 10000000;
37
Matt Spinlerc8705e22019-09-11 12:36:07 -050038namespace openpower
39{
40namespace pels
41{
42
43namespace service_name
44{
45constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper";
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -050046constexpr auto vpdManager = "com.ibm.VPD.Manager";
Matt Spinler34a904c2020-08-05 14:53:28 -050047constexpr auto ledGroupManager = "xyz.openbmc_project.LED.GroupManager";
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -050048constexpr auto hwIsolation = "org.open_power.HardwareIsolation";
Matt Spinler744d8512022-06-08 08:25:47 -050049constexpr auto biosConfigMgr = "xyz.openbmc_project.BIOSConfigManager";
Vijay Lobo875b6c72021-10-20 17:38:56 -050050constexpr auto bootRawProgress = "xyz.openbmc_project.State.Boot.Raw";
Matt Spinler52ee3a42023-07-27 14:54:48 -050051constexpr auto pldm = "xyz.openbmc_project.PLDM";
Matt Spinler412f50e2023-11-14 12:49:52 -060052constexpr auto inventoryManager = "xyz.openbmc_project.Inventory.Manager";
Matt Spinlerbbc11ef2024-01-11 16:26:51 -060053constexpr auto entityManager = "xyz.openbmc_project.EntityManager";
Arya K Padmanafba3162024-08-30 07:41:32 -050054constexpr auto systemd = "org.freedesktop.systemd1";
Matt Spinlerc8705e22019-09-11 12:36:07 -050055} // namespace service_name
56
57namespace object_path
58{
59constexpr auto objectMapper = "/xyz/openbmc_project/object_mapper";
60constexpr auto systemInv = "/xyz/openbmc_project/inventory/system";
Vijay Lobo81b4dca2021-04-29 00:04:00 -050061constexpr auto motherBoardInv =
62 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
Matt Spinlerb3d488f2020-02-21 15:30:46 -060063constexpr auto baseInv = "/xyz/openbmc_project/inventory";
Matt Spinler4aa23a12020-02-03 15:05:09 -060064constexpr auto bmcState = "/xyz/openbmc_project/state/bmc0";
65constexpr auto chassisState = "/xyz/openbmc_project/state/chassis0";
Matt Spinlera7d9d962019-11-06 15:01:25 -060066constexpr auto hostState = "/xyz/openbmc_project/state/host0";
Matt Spinler9cf3cfd2020-02-03 14:41:55 -060067constexpr auto enableHostPELs =
68 "/xyz/openbmc_project/logging/send_event_logs_to_host";
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -050069constexpr auto vpdManager = "/com/ibm/VPD/Manager";
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -050070constexpr auto logSetting = "/xyz/openbmc_project/logging/settings";
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -050071constexpr auto hwIsolation = "/xyz/openbmc_project/hardware_isolation";
Matt Spinler744d8512022-06-08 08:25:47 -050072constexpr auto biosConfigMgr = "/xyz/openbmc_project/bios_config/manager";
Vijay Lobo875b6c72021-10-20 17:38:56 -050073constexpr auto bootRawProgress = "/xyz/openbmc_project/state/boot/raw0";
Arya K Padmanafba3162024-08-30 07:41:32 -050074constexpr auto systemd = "/org/freedesktop/systemd1";
Matt Spinlerc8705e22019-09-11 12:36:07 -050075} // namespace object_path
76
77namespace interface
78{
79constexpr auto dbusProperty = "org.freedesktop.DBus.Properties";
80constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper";
81constexpr auto invAsset = "xyz.openbmc_project.Inventory.Decorator.Asset";
Matt Spinlerf10068d2020-12-02 10:44:08 -060082constexpr auto bootProgress = "xyz.openbmc_project.State.Boot.Progress";
Matt Spinler9cf3cfd2020-02-03 14:41:55 -060083constexpr auto enable = "xyz.openbmc_project.Object.Enable";
Matt Spinler4aa23a12020-02-03 15:05:09 -060084constexpr auto bmcState = "xyz.openbmc_project.State.BMC";
85constexpr auto chassisState = "xyz.openbmc_project.State.Chassis";
86constexpr auto hostState = "xyz.openbmc_project.State.Host";
Matt Spinlerb3d488f2020-02-21 15:30:46 -060087constexpr auto viniRecordVPD = "com.ibm.ipzvpd.VINI";
Ben Tynere32b7e72021-05-18 12:38:40 -050088constexpr auto vsbpRecordVPD = "com.ibm.ipzvpd.VSBP";
Matt Spinlercc8b1112021-12-15 09:30:35 -060089constexpr auto locCode = "xyz.openbmc_project.Inventory.Decorator.LocationCode";
Matt Spinler1ab66962020-10-29 13:21:44 -050090constexpr auto compatible =
Matt Spinlerbbc11ef2024-01-11 16:26:51 -060091 "xyz.openbmc_project.Inventory.Decorator.Compatible";
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -050092constexpr auto vpdManager = "com.ibm.VPD.Manager";
Matt Spinler34a904c2020-08-05 14:53:28 -050093constexpr auto ledGroup = "xyz.openbmc_project.Led.Group";
Matt Spinler993168d2021-04-07 16:05:03 -050094constexpr auto operationalStatus =
95 "xyz.openbmc_project.State.Decorator.OperationalStatus";
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -050096constexpr auto logSetting = "xyz.openbmc_project.Logging.Settings";
Sumit Kumar027bf282022-01-24 11:25:19 -060097constexpr auto associationDef = "xyz.openbmc_project.Association.Definitions";
Sumit Kumar027bf282022-01-24 11:25:19 -060098constexpr auto hwIsolationEntry = "xyz.openbmc_project.HardwareIsolation.Entry";
99constexpr auto association = "xyz.openbmc_project.Association";
Matt Spinler744d8512022-06-08 08:25:47 -0500100constexpr auto biosConfigMgr = "xyz.openbmc_project.BIOSConfig.Manager";
Vijay Lobo875b6c72021-10-20 17:38:56 -0500101constexpr auto bootRawProgress = "xyz.openbmc_project.State.Boot.Raw";
Matt Spinler5b423652023-05-04 13:08:44 -0500102constexpr auto invItem = "xyz.openbmc_project.Inventory.Item";
103constexpr auto invFan = "xyz.openbmc_project.Inventory.Item.Fan";
104constexpr auto invPowerSupply =
105 "xyz.openbmc_project.Inventory.Item.PowerSupply";
Matt Spinler412f50e2023-11-14 12:49:52 -0600106constexpr auto inventoryManager = "xyz.openbmc_project.Inventory.Manager";
Arya K Padmanafba3162024-08-30 07:41:32 -0500107constexpr auto systemdMgr = "org.freedesktop.systemd1.Manager";
Matt Spinlerc8705e22019-09-11 12:36:07 -0500108} // namespace interface
109
Willy Tu6ddbf692023-09-05 10:54:16 -0700110using namespace sdbusplus::server::xyz::openbmc_project::state::boot;
111using namespace sdbusplus::server::xyz::openbmc_project::state;
Matt Spinler5b423652023-05-04 13:08:44 -0500112namespace match_rules = sdbusplus::bus::match::rules;
113
114const DBusInterfaceList hotplugInterfaces{interface::invFan,
115 interface::invPowerSupply};
Arya K Padmanafba3162024-08-30 07:41:32 -0500116static constexpr auto PDBG_DTB_PATH =
117 "/var/lib/phosphor-software-manager/hostfw/running/DEVTREE";
Matt Spinlera7d9d962019-11-06 15:01:25 -0600118
Matt Spinler0d92b522021-06-16 13:28:17 -0600119std::pair<std::string, std::string>
120 DataInterfaceBase::extractConnectorFromLocCode(
121 const std::string& locationCode)
122{
123 auto base = locationCode;
124 std::string connector{};
125
126 auto pos = base.find("-T");
127 if (pos != std::string::npos)
128 {
129 connector = base.substr(pos);
130 base = base.substr(0, pos);
131 }
132
133 return {base, connector};
134}
135
Arya K Padmanafba3162024-08-30 07:41:32 -0500136DataInterface::DataInterface(sdbusplus::bus_t& bus) :
137 _bus(bus), _systemdSlot(nullptr)
Matt Spinlerc8705e22019-09-11 12:36:07 -0500138{
Matt Spinlercad9c2b2019-12-02 15:42:01 -0600139 readBMCFWVersion();
140 readServerFWVersion();
Matt Spinler677381b2020-01-23 10:04:29 -0600141 readBMCFWVersionID();
Matt Spinler2a28c932020-02-03 14:23:40 -0600142
Matt Spinlerf10068d2020-12-02 10:44:08 -0600143 // Watch the BootProgress property
Matt Spinler2a28c932020-02-03 14:23:40 -0600144 _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>(
Matt Spinlerf10068d2020-12-02 10:44:08 -0600145 bus, object_path::hostState, interface::bootProgress, "BootProgress",
146 *this, [this](const auto& value) {
Patrick Williams075c7922024-08-16 15:19:49 -0400147 this->_bootState = std::get<std::string>(value);
148 auto status = Progress::convertProgressStagesFromString(
149 std::get<std::string>(value));
Matt Spinler2a28c932020-02-03 14:23:40 -0600150
Patrick Williams075c7922024-08-16 15:19:49 -0400151 if ((status == Progress::ProgressStages::SystemInitComplete) ||
152 (status == Progress::ProgressStages::OSRunning))
153 {
154 setHostUp(true);
155 }
156 else
157 {
158 setHostUp(false);
159 }
160 }));
Matt Spinler9cf3cfd2020-02-03 14:41:55 -0600161
162 // Watch the host PEL enable property
163 _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>(
164 bus, object_path::enableHostPELs, interface::enable, "Enabled", *this,
165 [this](const auto& value) {
Patrick Williams075c7922024-08-16 15:19:49 -0400166 if (std::get<bool>(value) != this->_sendPELsToHost)
167 {
168 lg2::info("The send PELs to host setting changed to {VAL}",
169 "VAL", std::get<bool>(value));
170 }
171 this->_sendPELsToHost = std::get<bool>(value);
172 }));
Matt Spinler4aa23a12020-02-03 15:05:09 -0600173
174 // Watch the BMCState property
175 _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>(
176 bus, object_path::bmcState, interface::bmcState, "CurrentBMCState",
177 *this, [this](const auto& value) {
Patrick Williams075c7922024-08-16 15:19:49 -0400178 const auto& state = std::get<std::string>(value);
179 this->_bmcState = state;
Matt Spinler5b423652023-05-04 13:08:44 -0500180
Patrick Williams075c7922024-08-16 15:19:49 -0400181 // Wait for BMC ready to start watching for
182 // plugs so things calm down first.
183 if (BMC::convertBMCStateFromString(state) == BMC::BMCState::Ready)
184 {
185 startFruPlugWatch();
186 }
187 }));
Matt Spinler4aa23a12020-02-03 15:05:09 -0600188
189 // Watch the chassis current and requested power state properties
190 _properties.emplace_back(std::make_unique<InterfaceWatcher<DataInterface>>(
191 bus, object_path::chassisState, interface::chassisState, *this,
192 [this](const auto& properties) {
Patrick Williams075c7922024-08-16 15:19:49 -0400193 auto state = properties.find("CurrentPowerState");
194 if (state != properties.end())
195 {
196 this->_chassisState = std::get<std::string>(state->second);
197 }
Matt Spinler4aa23a12020-02-03 15:05:09 -0600198
Patrick Williams075c7922024-08-16 15:19:49 -0400199 auto trans = properties.find("RequestedPowerTransition");
200 if (trans != properties.end())
201 {
202 this->_chassisTransition = std::get<std::string>(trans->second);
203 }
204 }));
Matt Spinler4aa23a12020-02-03 15:05:09 -0600205
206 // Watch the CurrentHostState property
207 _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>(
208 bus, object_path::hostState, interface::hostState, "CurrentHostState",
209 *this, [this](const auto& value) {
Patrick Williams075c7922024-08-16 15:19:49 -0400210 this->_hostState = std::get<std::string>(value);
211 }));
Matt Spinler744d8512022-06-08 08:25:47 -0500212
213 // Watch the BaseBIOSTable property for the hmc managed attribute
214 _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>(
215 bus, object_path::biosConfigMgr, interface::biosConfigMgr,
216 "BaseBIOSTable", service_name::biosConfigMgr, *this,
217 [this](const auto& value) {
Patrick Williams075c7922024-08-16 15:19:49 -0400218 const auto& attributes = std::get<BiosAttributes>(value);
Matt Spinler744d8512022-06-08 08:25:47 -0500219
Patrick Williams075c7922024-08-16 15:19:49 -0400220 auto it = attributes.find("pvm_hmc_managed");
221 if (it != attributes.end())
Matt Spinler744d8512022-06-08 08:25:47 -0500222 {
Patrick Williams075c7922024-08-16 15:19:49 -0400223 const auto& currentValVariant = std::get<5>(it->second);
224 auto currentVal = std::get_if<std::string>(&currentValVariant);
225 if (currentVal)
226 {
227 this->_hmcManaged =
228 (*currentVal == "Enabled") ? true : false;
229 }
Matt Spinler744d8512022-06-08 08:25:47 -0500230 }
Patrick Williams075c7922024-08-16 15:19:49 -0400231 }));
Arya K Padmanafba3162024-08-30 07:41:32 -0500232
Arya K Padmanafba3162024-08-30 07:41:32 -0500233 if (isPHALDevTreeExist())
234 {
Arya K Padman0b758fb2024-09-06 11:59:45 -0500235#ifdef PEL_ENABLE_PHAL
Arya K Padmanafba3162024-08-30 07:41:32 -0500236 initPHAL();
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500237 try
238 {
239 libguard::libguard_init(false);
240 }
241 catch (libguard::exception::GuardException& e)
242 {
243 lg2::error("Exception to init the guard {ERROR}", "ERROR",
244 e.what());
245 }
Arya K Padman0b758fb2024-09-06 11:59:45 -0500246#endif
Arya K Padmanafba3162024-08-30 07:41:32 -0500247 }
248 else
249 {
250 // Watch the "openpower-update-bios-attr-table" service to init
251 // PHAL libraries
252 subscribeToSystemdSignals();
253 }
Matt Spinlerc8705e22019-09-11 12:36:07 -0500254}
255
Patrick Williams075c7922024-08-16 15:19:49 -0400256DBusPropertyMap DataInterface::getAllProperties(
257 const std::string& service, const std::string& objectPath,
258 const std::string& interface) const
Matt Spinlerc8705e22019-09-11 12:36:07 -0500259{
260 DBusPropertyMap properties;
261
262 auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
263 interface::dbusProperty, "GetAll");
264 method.append(interface);
Matt Spinler35a405b2022-03-02 11:42:42 -0600265 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlerc8705e22019-09-11 12:36:07 -0500266
267 reply.read(properties);
268
269 return properties;
270}
271
Patrick Williams075c7922024-08-16 15:19:49 -0400272void DataInterface::getProperty(
273 const std::string& service, const std::string& objectPath,
274 const std::string& interface, const std::string& property,
275 DBusValue& value) const
Matt Spinlera7d9d962019-11-06 15:01:25 -0600276{
Matt Spinlera7d9d962019-11-06 15:01:25 -0600277 auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
278 interface::dbusProperty, "Get");
279 method.append(interface, property);
Matt Spinler35a405b2022-03-02 11:42:42 -0600280 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlera7d9d962019-11-06 15:01:25 -0600281
282 reply.read(value);
283}
284
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600285DBusPathList DataInterface::getPaths(const DBusInterfaceList& interfaces) const
286{
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600287 auto method = _bus.new_method_call(
288 service_name::objectMapper, object_path::objectMapper,
289 interface::objectMapper, "GetSubTreePaths");
290
291 method.append(std::string{"/"}, 0, interfaces);
292
Matt Spinler35a405b2022-03-02 11:42:42 -0600293 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600294
295 DBusPathList paths;
296 reply.read(paths);
297
298 return paths;
299}
300
Matt Spinlerc8705e22019-09-11 12:36:07 -0500301DBusService DataInterface::getService(const std::string& objectPath,
Matt Spinlerb3f51862019-12-09 13:55:10 -0600302 const std::string& interface) const
Matt Spinlerc8705e22019-09-11 12:36:07 -0500303{
304 auto method = _bus.new_method_call(service_name::objectMapper,
305 object_path::objectMapper,
306 interface::objectMapper, "GetObject");
307
308 method.append(objectPath, std::vector<std::string>({interface}));
309
Matt Spinler35a405b2022-03-02 11:42:42 -0600310 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlerc8705e22019-09-11 12:36:07 -0500311
312 std::map<DBusService, DBusInterfaceList> response;
313 reply.read(response);
314
315 if (!response.empty())
316 {
317 return response.begin()->first;
318 }
319
320 return std::string{};
321}
Matt Spinlera7d9d962019-11-06 15:01:25 -0600322
Matt Spinler677381b2020-01-23 10:04:29 -0600323void DataInterface::readBMCFWVersion()
324{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500325 _bmcFWVersion =
326 phosphor::logging::util::getOSReleaseValue("VERSION").value_or("");
Matt Spinlercad9c2b2019-12-02 15:42:01 -0600327}
328
329void DataInterface::readServerFWVersion()
330{
Sumit Kumarcad16202021-05-13 04:06:15 -0500331 auto value =
332 phosphor::logging::util::getOSReleaseValue("VERSION_ID").value_or("");
333 if ((value != "") && (value.find_last_of(')') != std::string::npos))
334 {
335 std::size_t pos = value.find_first_of('(') + 1;
336 _serverFWVersion = value.substr(pos, value.find_last_of(')') - pos);
337 }
Matt Spinlercad9c2b2019-12-02 15:42:01 -0600338}
339
Matt Spinler677381b2020-01-23 10:04:29 -0600340void DataInterface::readBMCFWVersionID()
341{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500342 _bmcFWVersionID =
343 phosphor::logging::util::getOSReleaseValue("VERSION_ID").value_or("");
Matt Spinler677381b2020-01-23 10:04:29 -0600344}
345
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500346std::string DataInterface::getMachineTypeModel() const
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600347{
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500348 std::string model;
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600349 try
350 {
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500351 auto service = getService(object_path::systemInv, interface::invAsset);
352 if (!service.empty())
353 {
354 DBusValue value;
355 getProperty(service, object_path::systemInv, interface::invAsset,
356 "Model", value);
357
358 model = std::get<std::string>(value);
359 }
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600360 }
361 catch (const std::exception& e)
362 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500363 lg2::warning("Failed reading Model property from "
364 "interface: {IFACE} exception: {ERROR}",
365 "IFACE", interface::invAsset, "ERROR", e);
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600366 }
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500367
368 return model;
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600369}
370
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500371std::string DataInterface::getMachineSerialNumber() const
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600372{
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500373 std::string sn;
374 try
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600375 {
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500376 auto service = getService(object_path::systemInv, interface::invAsset);
377 if (!service.empty())
378 {
379 DBusValue value;
380 getProperty(service, object_path::systemInv, interface::invAsset,
381 "SerialNumber", value);
382
383 sn = std::get<std::string>(value);
384 }
385 }
386 catch (const std::exception& e)
387 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500388 lg2::warning("Failed reading SerialNumber property from "
389 "interface: {IFACE} exception: {ERROR}",
390 "IFACE", interface::invAsset, "ERROR", e);
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600391 }
392
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500393 return sn;
394}
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600395
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500396std::string DataInterface::getMotherboardCCIN() const
397{
398 std::string ccin;
399
400 try
401 {
Patrick Williams075c7922024-08-16 15:19:49 -0400402 auto service =
403 getService(object_path::motherBoardInv, interface::viniRecordVPD);
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500404 if (!service.empty())
405 {
406 DBusValue value;
407 getProperty(service, object_path::motherBoardInv,
408 interface::viniRecordVPD, "CC", value);
409
410 auto cc = std::get<std::vector<uint8_t>>(value);
411 ccin = std::string{cc.begin(), cc.end()};
412 }
413 }
414 catch (const std::exception& e)
415 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500416 lg2::warning("Failed reading Motherboard CCIN property from "
417 "interface: {IFACE} exception: {ERROR}",
418 "IFACE", interface::viniRecordVPD, "ERROR", e);
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500419 }
420
421 return ccin;
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600422}
423
Ben Tynere32b7e72021-05-18 12:38:40 -0500424std::vector<uint8_t> DataInterface::getSystemIMKeyword() const
425{
426 std::vector<uint8_t> systemIM;
427
428 try
429 {
Patrick Williams075c7922024-08-16 15:19:49 -0400430 auto service =
431 getService(object_path::motherBoardInv, interface::vsbpRecordVPD);
Ben Tynere32b7e72021-05-18 12:38:40 -0500432 if (!service.empty())
433 {
434 DBusValue value;
435 getProperty(service, object_path::motherBoardInv,
436 interface::vsbpRecordVPD, "IM", value);
437
438 systemIM = std::get<std::vector<uint8_t>>(value);
439 }
440 }
441 catch (const std::exception& e)
442 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500443 lg2::warning("Failed reading System IM property from "
444 "interface: {IFACE} exception: {ERROR}",
445 "IFACE", interface::vsbpRecordVPD, "ERROR", e);
Ben Tynere32b7e72021-05-18 12:38:40 -0500446 }
447
448 return systemIM;
449}
450
Patrick Williams075c7922024-08-16 15:19:49 -0400451void DataInterface::getHWCalloutFields(
452 const std::string& inventoryPath, std::string& fruPartNumber,
453 std::string& ccin, std::string& serialNumber) const
Matt Spinler60c4e792020-03-13 13:45:36 -0500454{
455 // For now, attempt to get all of the properties directly on the path
456 // passed in. In the future, may need to make use of an algorithm
457 // to figure out which inventory objects actually hold these
458 // interfaces in the case of non FRUs, or possibly another service
459 // will provide this info. Any missing interfaces will result
460 // in exceptions being thrown.
461
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500462 auto service = getService(inventoryPath, interface::viniRecordVPD);
Matt Spinler60c4e792020-03-13 13:45:36 -0500463
Patrick Williams075c7922024-08-16 15:19:49 -0400464 auto properties =
465 getAllProperties(service, inventoryPath, interface::viniRecordVPD);
Matt Spinler60c4e792020-03-13 13:45:36 -0500466
467 auto value = std::get<std::vector<uint8_t>>(properties["FN"]);
468 fruPartNumber = std::string{value.begin(), value.end()};
469
470 value = std::get<std::vector<uint8_t>>(properties["CC"]);
471 ccin = std::string{value.begin(), value.end()};
472
473 value = std::get<std::vector<uint8_t>>(properties["SN"]);
474 serialNumber = std::string{value.begin(), value.end()};
475}
476
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500477std::string
478 DataInterface::getLocationCode(const std::string& inventoryPath) const
479{
480 auto service = getService(inventoryPath, interface::locCode);
481
482 DBusValue locCode;
483 getProperty(service, inventoryPath, interface::locCode, "LocationCode",
484 locCode);
485
486 return std::get<std::string>(locCode);
487}
488
Matt Spinler5fb24c12020-06-04 11:21:33 -0500489std::string
490 DataInterface::addLocationCodePrefix(const std::string& locationCode)
491{
492 static const std::string locationCodePrefix{"Ufcs-"};
493
Matt Spinler0e4d72e2020-08-05 12:36:53 -0500494 // Technically there are 2 location code prefixes, Ufcs and Umts, so
495 // if it already starts with a U then don't need to do anything.
496 if (locationCode.front() != 'U')
Matt Spinler5fb24c12020-06-04 11:21:33 -0500497 {
498 return locationCodePrefix + locationCode;
499 }
500
501 return locationCode;
502}
503
504std::string DataInterface::expandLocationCode(const std::string& locationCode,
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500505 uint16_t /*node*/) const
Matt Spinler5fb24c12020-06-04 11:21:33 -0500506{
Matt Spinler0d92b522021-06-16 13:28:17 -0600507 // Location codes for connectors are the location code of the FRU they are
508 // on, plus a '-Tx' segment. Remove this last segment before expanding it
509 // and then add it back in afterwards. This way, the connector doesn't have
510 // to be in the model just so that it can be expanded.
511 auto [baseLoc, connectorLoc] = extractConnectorFromLocCode(locationCode);
512
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500513 auto method =
514 _bus.new_method_call(service_name::vpdManager, object_path::vpdManager,
515 interface::vpdManager, "GetExpandedLocationCode");
516
Matt Spinler0d92b522021-06-16 13:28:17 -0600517 method.append(addLocationCodePrefix(baseLoc), static_cast<uint16_t>(0));
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500518
Matt Spinler35a405b2022-03-02 11:42:42 -0600519 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500520
521 std::string expandedLocationCode;
522 reply.read(expandedLocationCode);
523
Matt Spinler0d92b522021-06-16 13:28:17 -0600524 if (!connectorLoc.empty())
525 {
526 expandedLocationCode += connectorLoc;
527 }
528
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500529 return expandedLocationCode;
Matt Spinler5fb24c12020-06-04 11:21:33 -0500530}
531
Patrick Williams075c7922024-08-16 15:19:49 -0400532std::vector<std::string> DataInterface::getInventoryFromLocCode(
533 const std::string& locationCode, uint16_t node, bool expanded) const
Matt Spinler5fb24c12020-06-04 11:21:33 -0500534{
Matt Spinler2f9225a2020-08-05 12:58:49 -0500535 std::string methodName = expanded ? "GetFRUsByExpandedLocationCode"
536 : "GetFRUsByUnexpandedLocationCode";
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500537
Matt Spinler0d92b522021-06-16 13:28:17 -0600538 // Remove the connector segment, if present, so that this method call
539 // returns an inventory path that getHWCalloutFields() can be used with.
540 // (The serial number, etc, aren't stored on the connector in the
541 // inventory, and may not even be modeled.)
542 auto [baseLoc, connectorLoc] = extractConnectorFromLocCode(locationCode);
543
Matt Spinler2f9225a2020-08-05 12:58:49 -0500544 auto method =
545 _bus.new_method_call(service_name::vpdManager, object_path::vpdManager,
546 interface::vpdManager, methodName.c_str());
547
548 if (expanded)
549 {
Matt Spinler0d92b522021-06-16 13:28:17 -0600550 method.append(baseLoc);
Matt Spinler2f9225a2020-08-05 12:58:49 -0500551 }
552 else
553 {
Matt Spinler0d92b522021-06-16 13:28:17 -0600554 method.append(addLocationCodePrefix(baseLoc), node);
Matt Spinler2f9225a2020-08-05 12:58:49 -0500555 }
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500556
Matt Spinler35a405b2022-03-02 11:42:42 -0600557 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500558
559 std::vector<sdbusplus::message::object_path> entries;
560 reply.read(entries);
561
Matt Spinlerbad056b2023-01-25 14:16:57 -0600562 std::vector<std::string> paths;
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500563
Matt Spinlerbad056b2023-01-25 14:16:57 -0600564 // Note: The D-Bus method will fail if nothing found.
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500565 std::for_each(entries.begin(), entries.end(),
Matt Spinlerbad056b2023-01-25 14:16:57 -0600566 [&paths](const auto& path) { paths.push_back(path); });
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500567
Matt Spinlerbad056b2023-01-25 14:16:57 -0600568 return paths;
Matt Spinler5fb24c12020-06-04 11:21:33 -0500569}
570
Matt Spinler34a904c2020-08-05 14:53:28 -0500571void DataInterface::assertLEDGroup(const std::string& ledGroup,
572 bool value) const
573{
574 DBusValue variant = value;
Patrick Williams075c7922024-08-16 15:19:49 -0400575 auto method =
576 _bus.new_method_call(service_name::ledGroupManager, ledGroup.c_str(),
577 interface::dbusProperty, "Set");
Matt Spinler34a904c2020-08-05 14:53:28 -0500578 method.append(interface::ledGroup, "Asserted", variant);
Matt Spinler35a405b2022-03-02 11:42:42 -0600579 _bus.call(method, dbusTimeout);
Matt Spinler34a904c2020-08-05 14:53:28 -0500580}
581
Matt Spinler993168d2021-04-07 16:05:03 -0500582void DataInterface::setFunctional(const std::string& objectPath,
583 bool value) const
584{
Matt Spinler412f50e2023-11-14 12:49:52 -0600585 DBusPropertyMap prop{{"Functional", value}};
586 DBusInterfaceMap iface{{interface::operationalStatus, prop}};
Matt Spinler993168d2021-04-07 16:05:03 -0500587
Matt Spinler412f50e2023-11-14 12:49:52 -0600588 // PIM takes a relative path like /system/chassis so remove
589 // /xyz/openbmc_project/inventory if present.
590 std::string path{objectPath};
591 if (path.starts_with(object_path::baseInv))
592 {
593 path = objectPath.substr(strlen(object_path::baseInv));
594 }
595 DBusObjectMap object{{path, iface}};
Matt Spinler993168d2021-04-07 16:05:03 -0500596
Matt Spinler412f50e2023-11-14 12:49:52 -0600597 auto method = _bus.new_method_call(service_name::inventoryManager,
598 object_path::baseInv,
599 interface::inventoryManager, "Notify");
600 method.append(std::move(object));
Matt Spinler35a405b2022-03-02 11:42:42 -0600601 _bus.call(method, dbusTimeout);
Matt Spinler993168d2021-04-07 16:05:03 -0500602}
603
Sumit Kumar76198a22021-07-15 05:59:57 -0500604using AssociationTuple = std::tuple<std::string, std::string, std::string>;
605using AssociationsProperty = std::vector<AssociationTuple>;
606
607void DataInterface::setCriticalAssociation(const std::string& objectPath) const
608{
609 DBusValue getAssociationValue;
610
Sumit Kumar027bf282022-01-24 11:25:19 -0600611 auto service = getService(objectPath, interface::associationDef);
Sumit Kumar76198a22021-07-15 05:59:57 -0500612
Sumit Kumar027bf282022-01-24 11:25:19 -0600613 getProperty(service, objectPath, interface::associationDef, "Associations",
Sumit Kumar76198a22021-07-15 05:59:57 -0500614 getAssociationValue);
615
616 auto association = std::get<AssociationsProperty>(getAssociationValue);
617
618 AssociationTuple critAssociation{
619 "health_rollup", "critical",
620 "/xyz/openbmc_project/inventory/system/chassis"};
621
622 if (std::find(association.begin(), association.end(), critAssociation) ==
623 association.end())
624 {
625 association.push_back(critAssociation);
626 DBusValue setAssociationValue = association;
627
628 auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
629 interface::dbusProperty, "Set");
630
Sumit Kumar027bf282022-01-24 11:25:19 -0600631 method.append(interface::associationDef, "Associations",
Sumit Kumar76198a22021-07-15 05:59:57 -0500632 setAssociationValue);
Matt Spinler35a405b2022-03-02 11:42:42 -0600633 _bus.call(method, dbusTimeout);
Sumit Kumar76198a22021-07-15 05:59:57 -0500634 }
635}
636
Matt Spinler1ab66962020-10-29 13:21:44 -0500637std::vector<std::string> DataInterface::getSystemNames() const
638{
639 DBusSubTree subtree;
640 DBusValue names;
641
642 auto method = _bus.new_method_call(service_name::objectMapper,
643 object_path::objectMapper,
644 interface::objectMapper, "GetSubTree");
645 method.append(std::string{"/"}, 0,
646 std::vector<std::string>{interface::compatible});
Matt Spinler35a405b2022-03-02 11:42:42 -0600647 auto reply = _bus.call(method, dbusTimeout);
Matt Spinler1ab66962020-10-29 13:21:44 -0500648
649 reply.read(subtree);
650 if (subtree.empty())
651 {
652 throw std::runtime_error("Compatible interface not on D-Bus");
653 }
654
Matt Spinlerbbc11ef2024-01-11 16:26:51 -0600655 for (const auto& [path, interfaceMap] : subtree)
656 {
657 auto iface = interfaceMap.find(service_name::entityManager);
658 if (iface == interfaceMap.end())
659 {
660 continue;
661 }
Matt Spinler1ab66962020-10-29 13:21:44 -0500662
Matt Spinlerbbc11ef2024-01-11 16:26:51 -0600663 getProperty(iface->first, path, interface::compatible, "Names", names);
Matt Spinler1ab66962020-10-29 13:21:44 -0500664
Matt Spinlerbbc11ef2024-01-11 16:26:51 -0600665 return std::get<std::vector<std::string>>(names);
666 }
667
668 throw std::runtime_error("EM Compatible interface not on D-Bus");
Matt Spinler1ab66962020-10-29 13:21:44 -0500669}
670
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500671bool DataInterface::getQuiesceOnError() const
672{
673 bool ret = false;
674
675 try
676 {
Patrick Williams075c7922024-08-16 15:19:49 -0400677 auto service =
678 getService(object_path::logSetting, interface::logSetting);
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500679 if (!service.empty())
680 {
681 DBusValue value;
682 getProperty(service, object_path::logSetting, interface::logSetting,
683 "QuiesceOnHwError", value);
684
685 ret = std::get<bool>(value);
686 }
687 }
688 catch (const std::exception& e)
689 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500690 lg2::warning("Failed reading QuiesceOnHwError property from "
691 "interface: {IFACE} exception: {ERROR}",
692 "IFACE", interface::logSetting, "ERROR", e);
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500693 }
694
695 return ret;
696}
697
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500698#ifdef PEL_ENABLE_PHAL
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -0500699void DataInterface::createGuardRecord(const std::vector<uint8_t>& binPath,
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500700 GardType eGardType, uint32_t plid) const
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -0500701{
702 try
703 {
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500704 libguard::create(binPath, plid, eGardType);
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -0500705 }
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500706 catch (libguard::exception::GuardException& e)
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -0500707 {
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500708 lg2::error("Exception to create the guard {ERROR}", "ERROR", e.what());
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -0500709 }
710}
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500711#endif
Sumit Kumar3e274432021-09-14 06:37:56 -0500712
713void DataInterface::createProgressSRC(
714 const uint64_t& priSRC, const std::vector<uint8_t>& srcStruct) const
715{
716 DBusValue variant = std::make_tuple(priSRC, srcStruct);
717
718 auto method = _bus.new_method_call(service_name::bootRawProgress,
Vijay Lobo875b6c72021-10-20 17:38:56 -0500719 object_path::bootRawProgress,
Sumit Kumar3e274432021-09-14 06:37:56 -0500720 interface::dbusProperty, "Set");
721
722 method.append(interface::bootRawProgress, "Value", variant);
723
Matt Spinler35a405b2022-03-02 11:42:42 -0600724 _bus.call(method, dbusTimeout);
Sumit Kumar3e274432021-09-14 06:37:56 -0500725}
Sumit Kumar027bf282022-01-24 11:25:19 -0600726
727std::vector<uint32_t> DataInterface::getLogIDWithHwIsolation() const
728{
729 std::vector<std::string> association = {"xyz.openbmc_project.Association"};
730 std::string hwErrorLog = "/isolated_hw_errorlog";
731 std::string errorLog = "/error_log";
732 DBusPathList paths;
733 std::vector<uint32_t> ids;
734
735 // Get all latest mapper associations
736 paths = getPaths(association);
737 for (auto& path : paths)
738 {
739 // Look for object path with hardware isolation entry if any
740 size_t pos = path.find(hwErrorLog);
741 if (pos != std::string::npos)
742 {
743 // Get the object path
744 std::string ph = path;
745 ph.erase(pos, hwErrorLog.length());
746 auto service = getService(ph, interface::hwIsolationEntry);
747 if (!service.empty())
748 {
749 bool status;
750 DBusValue value;
751
752 // Read the Resolved property from object path
753 getProperty(service, ph, interface::hwIsolationEntry,
754 "Resolved", value);
755
756 status = std::get<bool>(value);
757
758 // If the entry isn't resolved
759 if (!status)
760 {
Patrick Williams075c7922024-08-16 15:19:49 -0400761 auto assocService =
762 getService(path, interface::association);
Matt Spinler45796e82022-07-01 11:25:27 -0500763 if (!assocService.empty())
Sumit Kumar027bf282022-01-24 11:25:19 -0600764 {
Matt Spinler45796e82022-07-01 11:25:27 -0500765 DBusValue endpoints;
Sumit Kumar027bf282022-01-24 11:25:19 -0600766
767 // Read Endpoints property
Matt Spinler45796e82022-07-01 11:25:27 -0500768 getProperty(assocService, path, interface::association,
769 "endpoints", endpoints);
Sumit Kumar027bf282022-01-24 11:25:19 -0600770
771 auto logPath =
Matt Spinler45796e82022-07-01 11:25:27 -0500772 std::get<std::vector<std::string>>(endpoints);
Sumit Kumar027bf282022-01-24 11:25:19 -0600773 if (!logPath.empty())
774 {
775 // Get OpenBMC event log Id
776 uint32_t id = stoi(logPath[0].substr(
777 logPath[0].find_last_of('/') + 1));
778 ids.push_back(id);
779 }
780 }
781 }
782 }
783 }
784
785 // Look for object path with error_log entry if any
786 pos = path.find(errorLog);
787 if (pos != std::string::npos)
788 {
789 auto service = getService(path, interface::association);
790 if (!service.empty())
791 {
792 DBusValue value;
793
794 // Read Endpoints property
795 getProperty(service, path, interface::association, "endpoints",
796 value);
797
798 auto logPath = std::get<std::vector<std::string>>(value);
799 if (!logPath.empty())
800 {
801 // Get OpenBMC event log Id
802 uint32_t id = stoi(
803 logPath[0].substr(logPath[0].find_last_of('/') + 1));
804 ids.push_back(id);
805 }
806 }
807 }
808 }
809
810 if (ids.size() > 1)
811 {
812 // remove duplicates to have only unique ids
813 std::sort(ids.begin(), ids.end());
814 ids.erase(std::unique(ids.begin(), ids.end()), ids.end());
815 }
816 return ids;
817}
Vijay Lobo875b6c72021-10-20 17:38:56 -0500818
819std::vector<uint8_t> DataInterface::getRawProgressSRC(void) const
820{
821 using RawProgressProperty = std::tuple<uint64_t, std::vector<uint8_t>>;
822
823 DBusValue value;
824 getProperty(service_name::bootRawProgress, object_path::bootRawProgress,
825 interface::bootRawProgress, "Value", value);
826
827 const auto& rawProgress = std::get<RawProgressProperty>(value);
828 return std::get<1>(rawProgress);
829}
830
Arya K Padmand8ae6182024-07-19 06:25:10 -0500831std::optional<std::vector<uint8_t>>
832 DataInterface::getDIProperty(const std::string& locationCode) const
833{
834 std::vector<uint8_t> viniDI;
835
836 try
837 {
838 // Note : The hardcoded value 0 should be changed when comes to
839 // multinode system.
840 auto objectPath = getInventoryFromLocCode(locationCode, 0, true);
841
842 DBusValue value;
843 getProperty(service_name::inventoryManager, objectPath[0],
844 interface::viniRecordVPD, "DI", value);
845
846 viniDI = std::get<std::vector<uint8_t>>(value);
847 }
848 catch (const std::exception& e)
849 {
850 lg2::warning(
851 "Failed reading DI property for the location code : {LOC_CODE} from "
852 "interface: {IFACE} exception: {ERROR}",
853 "LOC_CODE", locationCode, "IFACE", interface::viniRecordVPD,
854 "ERROR", e);
855 return std::nullopt;
856 }
857
858 return viniDI;
859}
860
861std::optional<bool>
862 DataInterfaceBase::isDIMMLocCode(const std::string& locCode) const
863{
864 if (_locationCache.contains(locCode))
865 {
866 return _locationCache.at(locCode);
867 }
868 else
869 {
870 return std::nullopt;
871 }
872}
873
874void DataInterfaceBase::addDIMMLocCode(const std::string& locCode,
875 bool isFRUDIMM)
876{
877 _locationCache.insert({locCode, isFRUDIMM});
878}
879
Arya K Padmanced8ed72024-09-02 05:18:07 -0500880bool DataInterfaceBase::isDIMM(const std::string& locCode)
Arya K Padmand8ae6182024-07-19 06:25:10 -0500881{
882 auto isDIMMType = isDIMMLocCode(locCode);
883 if (isDIMMType.has_value())
884 {
885 return isDIMMType.value();
886 }
887#ifndef PEL_ENABLE_PHAL
Arya K Padmanced8ed72024-09-02 05:18:07 -0500888 return false;
Arya K Padmand8ae6182024-07-19 06:25:10 -0500889#else
890 else
891 {
892 // Invoke pHAL API inorder to fetch the FRU Type
893 auto fruType = openpower::phal::pdbg::getFRUType(locCode);
Arya K Padmanced8ed72024-09-02 05:18:07 -0500894 bool isDIMMFRU{false};
Arya K Padmand8ae6182024-07-19 06:25:10 -0500895 if (fruType.has_value())
896 {
Arya K Padmand8ae6182024-07-19 06:25:10 -0500897 if (fruType.value() == ENUM_ATTR_TYPE_DIMM)
898 {
899 isDIMMFRU = true;
900 }
901 addDIMMLocCode(locCode, isDIMMFRU);
Arya K Padmand8ae6182024-07-19 06:25:10 -0500902 }
Arya K Padmanced8ed72024-09-02 05:18:07 -0500903 return isDIMMFRU;
Arya K Padmand8ae6182024-07-19 06:25:10 -0500904 }
905#endif
906}
907
harsh-agarwal1d763db32024-09-03 09:18:50 -0500908DBusPathList DataInterface::getAssociatedPaths(
909 const DBusPath& associatedPath, const DBusPath& subtree, int32_t depth,
910 const DBusInterfaceList& interfaces) const
911{
912 DBusPathList paths;
913 try
914 {
915 auto method = _bus.new_method_call(
916 service_name::objectMapper, object_path::objectMapper,
917 interface::objectMapper, "GetAssociatedSubTreePaths");
918 method.append(sdbusplus::message::object_path(associatedPath),
919 sdbusplus::message::object_path(subtree), depth,
920 interfaces);
921
922 auto reply = _bus.call(method, dbusTimeout);
923 reply.read(paths);
924 }
925 catch (const std::exception& e)
926 {
927 std::string ifaces(
928 std::ranges::fold_left_first(
929 interfaces,
930 [](std::string ifaces, const std::string& iface) {
931 return ifaces + ", " + iface;
932 })
933 .value_or(""));
934
935 lg2::error("Failed getting associated paths: {ERROR}. "
936 "AssociatedPath: {ASSOIC_PATH} Subtree: {SUBTREE} "
937 "Interfaces: {IFACES}",
938 "ERROR", e, "ASSOIC_PATH", associatedPath, "SUBTREE",
939 subtree, "IFACES", ifaces);
940 }
941 return paths;
942}
943
Matt Spinler5b423652023-05-04 13:08:44 -0500944void DataInterface::startFruPlugWatch()
945{
946 // Add a watch on inventory InterfacesAdded and then find all
947 // existing hotpluggable interfaces and add propertiesChanged
948 // watches on them.
949
950 _invIaMatch = std::make_unique<sdbusplus::bus::match_t>(
951 _bus, match_rules::interfacesAdded(object_path::baseInv),
952 std::bind(&DataInterface::inventoryIfaceAdded, this,
953 std::placeholders::_1));
954 try
955 {
956 auto paths = getPaths(hotplugInterfaces);
957
958 _invPresentMatches.clear();
959
960 std::for_each(paths.begin(), paths.end(),
961 [this](const auto& path) { addHotplugWatch(path); });
962 }
963 catch (const sdbusplus::exception_t& e)
964 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500965 lg2::warning("Failed getting FRU paths to watch: {ERROR}", "ERROR", e);
Matt Spinler5b423652023-05-04 13:08:44 -0500966 }
967}
968
969void DataInterface::addHotplugWatch(const std::string& path)
970{
971 if (!_invPresentMatches.contains(path))
972 {
973 _invPresentMatches.emplace(
974 path,
975 std::make_unique<sdbusplus::bus::match_t>(
976 _bus, match_rules::propertiesChanged(path, interface::invItem),
977 std::bind(&DataInterface::presenceChanged, this,
978 std::placeholders::_1)));
979 }
980}
981
982void DataInterface::inventoryIfaceAdded(sdbusplus::message_t& msg)
983{
984 sdbusplus::message::object_path path;
985 DBusInterfaceMap interfaces;
986
987 msg.read(path, interfaces);
988
989 // Check if any of the new interfaces are for hot pluggable FRUs.
990 if (std::find_if(interfaces.begin(), interfaces.end(),
991 [](const auto& interfacePair) {
Patrick Williams075c7922024-08-16 15:19:49 -0400992 return std::find(hotplugInterfaces.begin(),
993 hotplugInterfaces.end(),
994 interfacePair.first) !=
995 hotplugInterfaces.end();
996 }) == interfaces.end())
Matt Spinler5b423652023-05-04 13:08:44 -0500997 {
998 return;
999 }
1000
1001 addHotplugWatch(path.str);
1002
1003 // If an Inventory.Item interface was also added, check presence now.
1004
1005 // Notes:
1006 // * This assumes the Inv.Item and Inv.Fan/PS are added together which
1007 // is currently the case.
1008 // * If the code ever switches to something without a Present
1009 // property, then the IA signal itself would probably indicate presence.
1010
1011 auto itemIt = interfaces.find(interface::invItem);
1012 if (itemIt != interfaces.end())
1013 {
1014 notifyPresenceSubsribers(path.str, itemIt->second);
1015 }
1016}
1017
1018void DataInterface::presenceChanged(sdbusplus::message_t& msg)
1019{
1020 DBusInterface interface;
1021 DBusPropertyMap properties;
1022
1023 msg.read(interface, properties);
1024 if (interface != interface::invItem)
1025 {
1026 return;
1027 }
1028
1029 std::string path = msg.get_path();
1030 notifyPresenceSubsribers(path, properties);
1031}
1032
1033void DataInterface::notifyPresenceSubsribers(const std::string& path,
1034 const DBusPropertyMap& properties)
1035{
Matt Spinler5ee36052023-05-30 14:20:56 -05001036 auto prop = properties.find("Present");
1037 if ((prop == properties.end()) || (!std::get<bool>(prop->second)))
1038 {
1039 return;
1040 }
1041
1042 std::string locCode;
1043
Matt Spinler5b423652023-05-04 13:08:44 -05001044 try
1045 {
Matt Spinler52ee3a42023-07-27 14:54:48 -05001046 auto service = getService(path, interface::locCode);
1047
1048 // If the hotplugged FRU is hosted by PLDM, then it is
1049 // in an IO expansion drawer and we don't care about it.
1050 if (service == service_name::pldm)
1051 {
1052 return;
1053 }
1054
Matt Spinler5ee36052023-05-30 14:20:56 -05001055 locCode = getLocationCode(path);
Matt Spinler5b423652023-05-04 13:08:44 -05001056 }
Matt Spinler5ee36052023-05-30 14:20:56 -05001057 catch (const sdbusplus::exception_t& e)
Matt Spinler5b423652023-05-04 13:08:44 -05001058 {
Matt Spinlera167a7d2023-06-30 15:14:25 -05001059 lg2::debug("Could not get location code for {PATH}: {ERROR}", "PATH",
1060 path, "ERROR", e);
Matt Spinler5ee36052023-05-30 14:20:56 -05001061 return;
Matt Spinler5b423652023-05-04 13:08:44 -05001062 }
Matt Spinler5ee36052023-05-30 14:20:56 -05001063
Matt Spinlera167a7d2023-06-30 15:14:25 -05001064 lg2::debug("Detected FRU {PATH} ({LOC}) present ", "PATH", path, "LOC",
1065 locCode);
Matt Spinler5ee36052023-05-30 14:20:56 -05001066
1067 // Tell the subscribers.
1068 setFruPresent(locCode);
Matt Spinler5b423652023-05-04 13:08:44 -05001069}
Arya K Padmanafba3162024-08-30 07:41:32 -05001070
Arya K Padmanafba3162024-08-30 07:41:32 -05001071bool DataInterface::isPHALDevTreeExist() const
1072{
1073 try
1074 {
1075 if (std::filesystem::exists(PDBG_DTB_PATH))
1076 {
1077 return true;
1078 }
1079 }
1080 catch (const std::exception& e)
1081 {
1082 lg2::error("Failed to check device tree {PHAL_DEVTREE_PATH} existence, "
1083 "{ERROR}",
1084 "PHAL_DEVTREE_PATH", PDBG_DTB_PATH, "ERROR", e);
1085 }
1086 return false;
1087}
1088
Arya K Padman0b758fb2024-09-06 11:59:45 -05001089#ifdef PEL_ENABLE_PHAL
Arya K Padmanafba3162024-08-30 07:41:32 -05001090void DataInterface::initPHAL()
1091{
1092 if (setenv("PDBG_DTB", PDBG_DTB_PATH, 1))
1093 {
1094 // Log message and continue,
1095 // This is to help continue creating PEL in raw format.
1096 lg2::error("Failed to set PDBG_DTB: ({ERRNO})", "ERRNO",
1097 strerror(errno));
1098 }
1099
1100 if (!pdbg_targets_init(NULL))
1101 {
1102 lg2::error("pdbg_targets_init failed");
1103 return;
1104 }
1105
1106 if (libekb_init())
1107 {
1108 lg2::error("libekb_init failed, skipping ffdc processing");
1109 return;
1110 }
1111}
Arya K Padman0b758fb2024-09-06 11:59:45 -05001112#endif
Arya K Padmanafba3162024-08-30 07:41:32 -05001113
1114void DataInterface::subscribeToSystemdSignals()
1115{
1116 try
1117 {
1118 auto method =
1119 _bus.new_method_call(service_name::systemd, object_path::systemd,
1120 interface::systemdMgr, "Subscribe");
1121 _systemdSlot = method.call_async([this](sdbusplus::message_t&& msg) {
1122 // Initializing with nullptr to indicate that it is not subscribed
1123 // to any signal.
1124 this->_systemdSlot = sdbusplus::slot_t(nullptr);
1125 if (msg.is_method_error())
1126 {
1127 auto* error = msg.get_error();
1128 lg2::error("Failed to subscribe JobRemoved systemd signal, "
Arya K Padmanea001072024-09-19 05:00:50 -05001129 "errorName: {ERR_NAME}, errorMsg: {ERR_MSG} ",
Arya K Padman0b758fb2024-09-06 11:59:45 -05001130 "ERR_NAME", error->name, "ERR_MSG", error->message);
Arya K Padmanafba3162024-08-30 07:41:32 -05001131 return;
1132 }
1133
1134 namespace sdbusRule = sdbusplus::bus::match::rules;
1135 this->_systemdMatch =
1136 std::make_unique<decltype(this->_systemdMatch)::element_type>(
1137 this->_bus,
1138 sdbusRule::type::signal() +
1139 sdbusRule::member("JobRemoved") +
1140 sdbusRule::path(object_path::systemd) +
1141 sdbusRule::interface(interface::systemdMgr),
1142 [this](sdbusplus::message_t& msg) {
1143 uint32_t jobID;
1144 sdbusplus::message::object_path jobObjPath;
1145 std::string jobUnitName, jobUnitResult;
1146
1147 msg.read(jobID, jobObjPath, jobUnitName, jobUnitResult);
1148 if ((jobUnitName ==
1149 "openpower-update-bios-attr-table.service") &&
1150 (jobUnitResult == "done"))
1151 {
Arya K Padmanea001072024-09-19 05:00:50 -05001152#ifdef PEL_ENABLE_PHAL
1153 this->initPHAL();
1154#endif
1155 // Invoke unsubscribe method to stop monitoring for
1156 // JobRemoved signals.
Arya K Padmanafba3162024-08-30 07:41:32 -05001157 this->unsubscribeFromSystemdSignals();
1158 }
1159 });
1160 });
1161 }
1162 catch (const sdbusplus::exception_t& e)
1163 {
1164 lg2::error(
1165 "Exception occured while handling JobRemoved systemd signal, "
1166 "exception: {ERROR}",
1167 "ERROR", e);
1168 }
1169}
1170
1171void DataInterface::unsubscribeFromSystemdSignals()
1172{
1173 try
1174 {
1175 auto method =
1176 _bus.new_method_call(service_name::systemd, object_path::systemd,
1177 interface::systemdMgr, "Unsubscribe");
1178 _systemdSlot = method.call_async([this](sdbusplus::message_t&& msg) {
1179 // Unsubscribing the _systemdSlot from the subscribed signal
1180 this->_systemdSlot = sdbusplus::slot_t(nullptr);
1181 if (msg.is_method_error())
1182 {
1183 auto* error = msg.get_error();
1184 lg2::error(
1185 "Failed to unsubscribe from JobRemoved systemd signal, "
Arya K Padmanea001072024-09-19 05:00:50 -05001186 "errorName: {ERR_NAME}, errorMsg: {ERR_MSG} ",
Arya K Padman0b758fb2024-09-06 11:59:45 -05001187 "ERR_NAME", error->name, "ERR_MSG", error->message);
Arya K Padmanafba3162024-08-30 07:41:32 -05001188 return;
1189 }
Arya K Padmanea001072024-09-19 05:00:50 -05001190 // Reset _systemdMatch to avoid reception of further JobRemoved
1191 // signals
1192 this->_systemdMatch.reset();
Arya K Padmanafba3162024-08-30 07:41:32 -05001193 });
1194 }
1195 catch (const sdbusplus::exception_t& e)
1196 {
1197 lg2::error(
1198 "Exception occured while unsubscribing from JobRemoved systemd signal, "
1199 "exception: {ERROR}",
1200 "ERROR", e);
1201 }
1202}
Arya K Padmanafba3162024-08-30 07:41:32 -05001203
Matt Spinlerc8705e22019-09-11 12:36:07 -05001204} // namespace pels
1205} // namespace openpower