blob: a8167a5f0a8979d45d027898943db0ad62db1940 [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();
Arya K Padman0b758fb2024-09-06 11:59:45 -0500237#endif
Arya K Padmanafba3162024-08-30 07:41:32 -0500238 }
239 else
240 {
241 // Watch the "openpower-update-bios-attr-table" service to init
242 // PHAL libraries
243 subscribeToSystemdSignals();
244 }
Matt Spinlerc8705e22019-09-11 12:36:07 -0500245}
246
Patrick Williams075c7922024-08-16 15:19:49 -0400247DBusPropertyMap DataInterface::getAllProperties(
248 const std::string& service, const std::string& objectPath,
249 const std::string& interface) const
Matt Spinlerc8705e22019-09-11 12:36:07 -0500250{
251 DBusPropertyMap properties;
252
253 auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
254 interface::dbusProperty, "GetAll");
255 method.append(interface);
Matt Spinler35a405b2022-03-02 11:42:42 -0600256 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlerc8705e22019-09-11 12:36:07 -0500257
258 reply.read(properties);
259
260 return properties;
261}
262
Patrick Williams075c7922024-08-16 15:19:49 -0400263void DataInterface::getProperty(
264 const std::string& service, const std::string& objectPath,
265 const std::string& interface, const std::string& property,
266 DBusValue& value) const
Matt Spinlera7d9d962019-11-06 15:01:25 -0600267{
Matt Spinlera7d9d962019-11-06 15:01:25 -0600268 auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
269 interface::dbusProperty, "Get");
270 method.append(interface, property);
Matt Spinler35a405b2022-03-02 11:42:42 -0600271 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlera7d9d962019-11-06 15:01:25 -0600272
273 reply.read(value);
274}
275
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600276DBusPathList DataInterface::getPaths(const DBusInterfaceList& interfaces) const
277{
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600278 auto method = _bus.new_method_call(
279 service_name::objectMapper, object_path::objectMapper,
280 interface::objectMapper, "GetSubTreePaths");
281
282 method.append(std::string{"/"}, 0, interfaces);
283
Matt Spinler35a405b2022-03-02 11:42:42 -0600284 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600285
286 DBusPathList paths;
287 reply.read(paths);
288
289 return paths;
290}
291
Matt Spinlerc8705e22019-09-11 12:36:07 -0500292DBusService DataInterface::getService(const std::string& objectPath,
Matt Spinlerb3f51862019-12-09 13:55:10 -0600293 const std::string& interface) const
Matt Spinlerc8705e22019-09-11 12:36:07 -0500294{
295 auto method = _bus.new_method_call(service_name::objectMapper,
296 object_path::objectMapper,
297 interface::objectMapper, "GetObject");
298
299 method.append(objectPath, std::vector<std::string>({interface}));
300
Matt Spinler35a405b2022-03-02 11:42:42 -0600301 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlerc8705e22019-09-11 12:36:07 -0500302
303 std::map<DBusService, DBusInterfaceList> response;
304 reply.read(response);
305
306 if (!response.empty())
307 {
308 return response.begin()->first;
309 }
310
311 return std::string{};
312}
Matt Spinlera7d9d962019-11-06 15:01:25 -0600313
Matt Spinler677381b2020-01-23 10:04:29 -0600314void DataInterface::readBMCFWVersion()
315{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500316 _bmcFWVersion =
317 phosphor::logging::util::getOSReleaseValue("VERSION").value_or("");
Matt Spinlercad9c2b2019-12-02 15:42:01 -0600318}
319
320void DataInterface::readServerFWVersion()
321{
Sumit Kumarcad16202021-05-13 04:06:15 -0500322 auto value =
323 phosphor::logging::util::getOSReleaseValue("VERSION_ID").value_or("");
324 if ((value != "") && (value.find_last_of(')') != std::string::npos))
325 {
326 std::size_t pos = value.find_first_of('(') + 1;
327 _serverFWVersion = value.substr(pos, value.find_last_of(')') - pos);
328 }
Matt Spinlercad9c2b2019-12-02 15:42:01 -0600329}
330
Matt Spinler677381b2020-01-23 10:04:29 -0600331void DataInterface::readBMCFWVersionID()
332{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500333 _bmcFWVersionID =
334 phosphor::logging::util::getOSReleaseValue("VERSION_ID").value_or("");
Matt Spinler677381b2020-01-23 10:04:29 -0600335}
336
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500337std::string DataInterface::getMachineTypeModel() const
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600338{
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500339 std::string model;
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600340 try
341 {
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500342 auto service = getService(object_path::systemInv, interface::invAsset);
343 if (!service.empty())
344 {
345 DBusValue value;
346 getProperty(service, object_path::systemInv, interface::invAsset,
347 "Model", value);
348
349 model = std::get<std::string>(value);
350 }
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600351 }
352 catch (const std::exception& e)
353 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500354 lg2::warning("Failed reading Model property from "
355 "interface: {IFACE} exception: {ERROR}",
356 "IFACE", interface::invAsset, "ERROR", e);
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600357 }
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500358
359 return model;
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600360}
361
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500362std::string DataInterface::getMachineSerialNumber() const
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600363{
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500364 std::string sn;
365 try
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600366 {
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500367 auto service = getService(object_path::systemInv, interface::invAsset);
368 if (!service.empty())
369 {
370 DBusValue value;
371 getProperty(service, object_path::systemInv, interface::invAsset,
372 "SerialNumber", value);
373
374 sn = std::get<std::string>(value);
375 }
376 }
377 catch (const std::exception& e)
378 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500379 lg2::warning("Failed reading SerialNumber property from "
380 "interface: {IFACE} exception: {ERROR}",
381 "IFACE", interface::invAsset, "ERROR", e);
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600382 }
383
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500384 return sn;
385}
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600386
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500387std::string DataInterface::getMotherboardCCIN() const
388{
389 std::string ccin;
390
391 try
392 {
Patrick Williams075c7922024-08-16 15:19:49 -0400393 auto service =
394 getService(object_path::motherBoardInv, interface::viniRecordVPD);
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500395 if (!service.empty())
396 {
397 DBusValue value;
398 getProperty(service, object_path::motherBoardInv,
399 interface::viniRecordVPD, "CC", value);
400
401 auto cc = std::get<std::vector<uint8_t>>(value);
402 ccin = std::string{cc.begin(), cc.end()};
403 }
404 }
405 catch (const std::exception& e)
406 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500407 lg2::warning("Failed reading Motherboard CCIN property from "
408 "interface: {IFACE} exception: {ERROR}",
409 "IFACE", interface::viniRecordVPD, "ERROR", e);
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500410 }
411
412 return ccin;
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600413}
414
Ben Tynere32b7e72021-05-18 12:38:40 -0500415std::vector<uint8_t> DataInterface::getSystemIMKeyword() const
416{
417 std::vector<uint8_t> systemIM;
418
419 try
420 {
Patrick Williams075c7922024-08-16 15:19:49 -0400421 auto service =
422 getService(object_path::motherBoardInv, interface::vsbpRecordVPD);
Ben Tynere32b7e72021-05-18 12:38:40 -0500423 if (!service.empty())
424 {
425 DBusValue value;
426 getProperty(service, object_path::motherBoardInv,
427 interface::vsbpRecordVPD, "IM", value);
428
429 systemIM = std::get<std::vector<uint8_t>>(value);
430 }
431 }
432 catch (const std::exception& e)
433 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500434 lg2::warning("Failed reading System IM property from "
435 "interface: {IFACE} exception: {ERROR}",
436 "IFACE", interface::vsbpRecordVPD, "ERROR", e);
Ben Tynere32b7e72021-05-18 12:38:40 -0500437 }
438
439 return systemIM;
440}
441
Patrick Williams075c7922024-08-16 15:19:49 -0400442void DataInterface::getHWCalloutFields(
443 const std::string& inventoryPath, std::string& fruPartNumber,
444 std::string& ccin, std::string& serialNumber) const
Matt Spinler60c4e792020-03-13 13:45:36 -0500445{
446 // For now, attempt to get all of the properties directly on the path
447 // passed in. In the future, may need to make use of an algorithm
448 // to figure out which inventory objects actually hold these
449 // interfaces in the case of non FRUs, or possibly another service
450 // will provide this info. Any missing interfaces will result
451 // in exceptions being thrown.
452
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500453 auto service = getService(inventoryPath, interface::viniRecordVPD);
Matt Spinler60c4e792020-03-13 13:45:36 -0500454
Patrick Williams075c7922024-08-16 15:19:49 -0400455 auto properties =
456 getAllProperties(service, inventoryPath, interface::viniRecordVPD);
Matt Spinler60c4e792020-03-13 13:45:36 -0500457
458 auto value = std::get<std::vector<uint8_t>>(properties["FN"]);
459 fruPartNumber = std::string{value.begin(), value.end()};
460
461 value = std::get<std::vector<uint8_t>>(properties["CC"]);
462 ccin = std::string{value.begin(), value.end()};
463
464 value = std::get<std::vector<uint8_t>>(properties["SN"]);
465 serialNumber = std::string{value.begin(), value.end()};
466}
467
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500468std::string
469 DataInterface::getLocationCode(const std::string& inventoryPath) const
470{
471 auto service = getService(inventoryPath, interface::locCode);
472
473 DBusValue locCode;
474 getProperty(service, inventoryPath, interface::locCode, "LocationCode",
475 locCode);
476
477 return std::get<std::string>(locCode);
478}
479
Matt Spinler5fb24c12020-06-04 11:21:33 -0500480std::string
481 DataInterface::addLocationCodePrefix(const std::string& locationCode)
482{
483 static const std::string locationCodePrefix{"Ufcs-"};
484
Matt Spinler0e4d72e2020-08-05 12:36:53 -0500485 // Technically there are 2 location code prefixes, Ufcs and Umts, so
486 // if it already starts with a U then don't need to do anything.
487 if (locationCode.front() != 'U')
Matt Spinler5fb24c12020-06-04 11:21:33 -0500488 {
489 return locationCodePrefix + locationCode;
490 }
491
492 return locationCode;
493}
494
495std::string DataInterface::expandLocationCode(const std::string& locationCode,
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500496 uint16_t /*node*/) const
Matt Spinler5fb24c12020-06-04 11:21:33 -0500497{
Matt Spinler0d92b522021-06-16 13:28:17 -0600498 // Location codes for connectors are the location code of the FRU they are
499 // on, plus a '-Tx' segment. Remove this last segment before expanding it
500 // and then add it back in afterwards. This way, the connector doesn't have
501 // to be in the model just so that it can be expanded.
502 auto [baseLoc, connectorLoc] = extractConnectorFromLocCode(locationCode);
503
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500504 auto method =
505 _bus.new_method_call(service_name::vpdManager, object_path::vpdManager,
506 interface::vpdManager, "GetExpandedLocationCode");
507
Matt Spinler0d92b522021-06-16 13:28:17 -0600508 method.append(addLocationCodePrefix(baseLoc), static_cast<uint16_t>(0));
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500509
Matt Spinler35a405b2022-03-02 11:42:42 -0600510 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500511
512 std::string expandedLocationCode;
513 reply.read(expandedLocationCode);
514
Matt Spinler0d92b522021-06-16 13:28:17 -0600515 if (!connectorLoc.empty())
516 {
517 expandedLocationCode += connectorLoc;
518 }
519
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500520 return expandedLocationCode;
Matt Spinler5fb24c12020-06-04 11:21:33 -0500521}
522
Patrick Williams075c7922024-08-16 15:19:49 -0400523std::vector<std::string> DataInterface::getInventoryFromLocCode(
524 const std::string& locationCode, uint16_t node, bool expanded) const
Matt Spinler5fb24c12020-06-04 11:21:33 -0500525{
Matt Spinler2f9225a2020-08-05 12:58:49 -0500526 std::string methodName = expanded ? "GetFRUsByExpandedLocationCode"
527 : "GetFRUsByUnexpandedLocationCode";
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500528
Matt Spinler0d92b522021-06-16 13:28:17 -0600529 // Remove the connector segment, if present, so that this method call
530 // returns an inventory path that getHWCalloutFields() can be used with.
531 // (The serial number, etc, aren't stored on the connector in the
532 // inventory, and may not even be modeled.)
533 auto [baseLoc, connectorLoc] = extractConnectorFromLocCode(locationCode);
534
Matt Spinler2f9225a2020-08-05 12:58:49 -0500535 auto method =
536 _bus.new_method_call(service_name::vpdManager, object_path::vpdManager,
537 interface::vpdManager, methodName.c_str());
538
539 if (expanded)
540 {
Matt Spinler0d92b522021-06-16 13:28:17 -0600541 method.append(baseLoc);
Matt Spinler2f9225a2020-08-05 12:58:49 -0500542 }
543 else
544 {
Matt Spinler0d92b522021-06-16 13:28:17 -0600545 method.append(addLocationCodePrefix(baseLoc), node);
Matt Spinler2f9225a2020-08-05 12:58:49 -0500546 }
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500547
Matt Spinler35a405b2022-03-02 11:42:42 -0600548 auto reply = _bus.call(method, dbusTimeout);
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500549
550 std::vector<sdbusplus::message::object_path> entries;
551 reply.read(entries);
552
Matt Spinlerbad056b2023-01-25 14:16:57 -0600553 std::vector<std::string> paths;
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500554
Matt Spinlerbad056b2023-01-25 14:16:57 -0600555 // Note: The D-Bus method will fail if nothing found.
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500556 std::for_each(entries.begin(), entries.end(),
Matt Spinlerbad056b2023-01-25 14:16:57 -0600557 [&paths](const auto& path) { paths.push_back(path); });
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500558
Matt Spinlerbad056b2023-01-25 14:16:57 -0600559 return paths;
Matt Spinler5fb24c12020-06-04 11:21:33 -0500560}
561
Matt Spinler34a904c2020-08-05 14:53:28 -0500562void DataInterface::assertLEDGroup(const std::string& ledGroup,
563 bool value) const
564{
565 DBusValue variant = value;
Patrick Williams075c7922024-08-16 15:19:49 -0400566 auto method =
567 _bus.new_method_call(service_name::ledGroupManager, ledGroup.c_str(),
568 interface::dbusProperty, "Set");
Matt Spinler34a904c2020-08-05 14:53:28 -0500569 method.append(interface::ledGroup, "Asserted", variant);
Matt Spinler35a405b2022-03-02 11:42:42 -0600570 _bus.call(method, dbusTimeout);
Matt Spinler34a904c2020-08-05 14:53:28 -0500571}
572
Matt Spinler993168d2021-04-07 16:05:03 -0500573void DataInterface::setFunctional(const std::string& objectPath,
574 bool value) const
575{
Matt Spinler412f50e2023-11-14 12:49:52 -0600576 DBusPropertyMap prop{{"Functional", value}};
577 DBusInterfaceMap iface{{interface::operationalStatus, prop}};
Matt Spinler993168d2021-04-07 16:05:03 -0500578
Matt Spinler412f50e2023-11-14 12:49:52 -0600579 // PIM takes a relative path like /system/chassis so remove
580 // /xyz/openbmc_project/inventory if present.
581 std::string path{objectPath};
582 if (path.starts_with(object_path::baseInv))
583 {
584 path = objectPath.substr(strlen(object_path::baseInv));
585 }
586 DBusObjectMap object{{path, iface}};
Matt Spinler993168d2021-04-07 16:05:03 -0500587
Matt Spinler412f50e2023-11-14 12:49:52 -0600588 auto method = _bus.new_method_call(service_name::inventoryManager,
589 object_path::baseInv,
590 interface::inventoryManager, "Notify");
591 method.append(std::move(object));
Matt Spinler35a405b2022-03-02 11:42:42 -0600592 _bus.call(method, dbusTimeout);
Matt Spinler993168d2021-04-07 16:05:03 -0500593}
594
Sumit Kumar76198a22021-07-15 05:59:57 -0500595using AssociationTuple = std::tuple<std::string, std::string, std::string>;
596using AssociationsProperty = std::vector<AssociationTuple>;
597
598void DataInterface::setCriticalAssociation(const std::string& objectPath) const
599{
600 DBusValue getAssociationValue;
601
Sumit Kumar027bf282022-01-24 11:25:19 -0600602 auto service = getService(objectPath, interface::associationDef);
Sumit Kumar76198a22021-07-15 05:59:57 -0500603
Sumit Kumar027bf282022-01-24 11:25:19 -0600604 getProperty(service, objectPath, interface::associationDef, "Associations",
Sumit Kumar76198a22021-07-15 05:59:57 -0500605 getAssociationValue);
606
607 auto association = std::get<AssociationsProperty>(getAssociationValue);
608
609 AssociationTuple critAssociation{
610 "health_rollup", "critical",
611 "/xyz/openbmc_project/inventory/system/chassis"};
612
613 if (std::find(association.begin(), association.end(), critAssociation) ==
614 association.end())
615 {
616 association.push_back(critAssociation);
617 DBusValue setAssociationValue = association;
618
619 auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
620 interface::dbusProperty, "Set");
621
Sumit Kumar027bf282022-01-24 11:25:19 -0600622 method.append(interface::associationDef, "Associations",
Sumit Kumar76198a22021-07-15 05:59:57 -0500623 setAssociationValue);
Matt Spinler35a405b2022-03-02 11:42:42 -0600624 _bus.call(method, dbusTimeout);
Sumit Kumar76198a22021-07-15 05:59:57 -0500625 }
626}
627
Matt Spinler1ab66962020-10-29 13:21:44 -0500628std::vector<std::string> DataInterface::getSystemNames() const
629{
630 DBusSubTree subtree;
631 DBusValue names;
632
633 auto method = _bus.new_method_call(service_name::objectMapper,
634 object_path::objectMapper,
635 interface::objectMapper, "GetSubTree");
636 method.append(std::string{"/"}, 0,
637 std::vector<std::string>{interface::compatible});
Matt Spinler35a405b2022-03-02 11:42:42 -0600638 auto reply = _bus.call(method, dbusTimeout);
Matt Spinler1ab66962020-10-29 13:21:44 -0500639
640 reply.read(subtree);
641 if (subtree.empty())
642 {
643 throw std::runtime_error("Compatible interface not on D-Bus");
644 }
645
Matt Spinlerbbc11ef2024-01-11 16:26:51 -0600646 for (const auto& [path, interfaceMap] : subtree)
647 {
648 auto iface = interfaceMap.find(service_name::entityManager);
649 if (iface == interfaceMap.end())
650 {
651 continue;
652 }
Matt Spinler1ab66962020-10-29 13:21:44 -0500653
Matt Spinlerbbc11ef2024-01-11 16:26:51 -0600654 getProperty(iface->first, path, interface::compatible, "Names", names);
Matt Spinler1ab66962020-10-29 13:21:44 -0500655
Matt Spinlerbbc11ef2024-01-11 16:26:51 -0600656 return std::get<std::vector<std::string>>(names);
657 }
658
659 throw std::runtime_error("EM Compatible interface not on D-Bus");
Matt Spinler1ab66962020-10-29 13:21:44 -0500660}
661
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500662bool DataInterface::getQuiesceOnError() const
663{
664 bool ret = false;
665
666 try
667 {
Patrick Williams075c7922024-08-16 15:19:49 -0400668 auto service =
669 getService(object_path::logSetting, interface::logSetting);
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500670 if (!service.empty())
671 {
672 DBusValue value;
673 getProperty(service, object_path::logSetting, interface::logSetting,
674 "QuiesceOnHwError", value);
675
676 ret = std::get<bool>(value);
677 }
678 }
679 catch (const std::exception& e)
680 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500681 lg2::warning("Failed reading QuiesceOnHwError property from "
682 "interface: {IFACE} exception: {ERROR}",
683 "IFACE", interface::logSetting, "ERROR", e);
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500684 }
685
686 return ret;
687}
688
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500689#ifdef PEL_ENABLE_PHAL
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -0500690void DataInterface::createGuardRecord(const std::vector<uint8_t>& binPath,
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500691 GardType eGardType, uint32_t plid) const
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -0500692{
693 try
694 {
Deepa Karthikeyanc6396da2024-11-14 04:50:25 -0600695 libguard::libguard_init(false);
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500696 libguard::create(binPath, plid, eGardType);
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -0500697 }
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500698 catch (libguard::exception::GuardException& e)
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -0500699 {
Deepa Karthikeyanc6396da2024-11-14 04:50:25 -0600700 lg2::error("Exception in libguard {ERROR}", "ERROR", e);
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -0500701 }
702}
Deepa Karthikeyanff35be32024-10-15 09:10:49 -0500703#endif
Sumit Kumar3e274432021-09-14 06:37:56 -0500704
705void DataInterface::createProgressSRC(
706 const uint64_t& priSRC, const std::vector<uint8_t>& srcStruct) const
707{
708 DBusValue variant = std::make_tuple(priSRC, srcStruct);
709
710 auto method = _bus.new_method_call(service_name::bootRawProgress,
Vijay Lobo875b6c72021-10-20 17:38:56 -0500711 object_path::bootRawProgress,
Sumit Kumar3e274432021-09-14 06:37:56 -0500712 interface::dbusProperty, "Set");
713
714 method.append(interface::bootRawProgress, "Value", variant);
715
Matt Spinler35a405b2022-03-02 11:42:42 -0600716 _bus.call(method, dbusTimeout);
Sumit Kumar3e274432021-09-14 06:37:56 -0500717}
Sumit Kumar027bf282022-01-24 11:25:19 -0600718
719std::vector<uint32_t> DataInterface::getLogIDWithHwIsolation() const
720{
721 std::vector<std::string> association = {"xyz.openbmc_project.Association"};
722 std::string hwErrorLog = "/isolated_hw_errorlog";
723 std::string errorLog = "/error_log";
724 DBusPathList paths;
725 std::vector<uint32_t> ids;
726
727 // Get all latest mapper associations
728 paths = getPaths(association);
729 for (auto& path : paths)
730 {
731 // Look for object path with hardware isolation entry if any
732 size_t pos = path.find(hwErrorLog);
733 if (pos != std::string::npos)
734 {
735 // Get the object path
736 std::string ph = path;
737 ph.erase(pos, hwErrorLog.length());
738 auto service = getService(ph, interface::hwIsolationEntry);
739 if (!service.empty())
740 {
741 bool status;
742 DBusValue value;
743
744 // Read the Resolved property from object path
745 getProperty(service, ph, interface::hwIsolationEntry,
746 "Resolved", value);
747
748 status = std::get<bool>(value);
749
750 // If the entry isn't resolved
751 if (!status)
752 {
Patrick Williams075c7922024-08-16 15:19:49 -0400753 auto assocService =
754 getService(path, interface::association);
Matt Spinler45796e82022-07-01 11:25:27 -0500755 if (!assocService.empty())
Sumit Kumar027bf282022-01-24 11:25:19 -0600756 {
Matt Spinler45796e82022-07-01 11:25:27 -0500757 DBusValue endpoints;
Sumit Kumar027bf282022-01-24 11:25:19 -0600758
759 // Read Endpoints property
Matt Spinler45796e82022-07-01 11:25:27 -0500760 getProperty(assocService, path, interface::association,
761 "endpoints", endpoints);
Sumit Kumar027bf282022-01-24 11:25:19 -0600762
763 auto logPath =
Matt Spinler45796e82022-07-01 11:25:27 -0500764 std::get<std::vector<std::string>>(endpoints);
Sumit Kumar027bf282022-01-24 11:25:19 -0600765 if (!logPath.empty())
766 {
767 // Get OpenBMC event log Id
768 uint32_t id = stoi(logPath[0].substr(
769 logPath[0].find_last_of('/') + 1));
770 ids.push_back(id);
771 }
772 }
773 }
774 }
775 }
776
777 // Look for object path with error_log entry if any
778 pos = path.find(errorLog);
779 if (pos != std::string::npos)
780 {
781 auto service = getService(path, interface::association);
782 if (!service.empty())
783 {
784 DBusValue value;
785
786 // Read Endpoints property
787 getProperty(service, path, interface::association, "endpoints",
788 value);
789
790 auto logPath = std::get<std::vector<std::string>>(value);
791 if (!logPath.empty())
792 {
793 // Get OpenBMC event log Id
794 uint32_t id = stoi(
795 logPath[0].substr(logPath[0].find_last_of('/') + 1));
796 ids.push_back(id);
797 }
798 }
799 }
800 }
801
802 if (ids.size() > 1)
803 {
804 // remove duplicates to have only unique ids
805 std::sort(ids.begin(), ids.end());
806 ids.erase(std::unique(ids.begin(), ids.end()), ids.end());
807 }
808 return ids;
809}
Vijay Lobo875b6c72021-10-20 17:38:56 -0500810
811std::vector<uint8_t> DataInterface::getRawProgressSRC(void) const
812{
813 using RawProgressProperty = std::tuple<uint64_t, std::vector<uint8_t>>;
814
815 DBusValue value;
816 getProperty(service_name::bootRawProgress, object_path::bootRawProgress,
817 interface::bootRawProgress, "Value", value);
818
819 const auto& rawProgress = std::get<RawProgressProperty>(value);
820 return std::get<1>(rawProgress);
821}
822
Arya K Padmand8ae6182024-07-19 06:25:10 -0500823std::optional<std::vector<uint8_t>>
824 DataInterface::getDIProperty(const std::string& locationCode) const
825{
826 std::vector<uint8_t> viniDI;
827
828 try
829 {
830 // Note : The hardcoded value 0 should be changed when comes to
831 // multinode system.
832 auto objectPath = getInventoryFromLocCode(locationCode, 0, true);
833
834 DBusValue value;
835 getProperty(service_name::inventoryManager, objectPath[0],
836 interface::viniRecordVPD, "DI", value);
837
838 viniDI = std::get<std::vector<uint8_t>>(value);
839 }
840 catch (const std::exception& e)
841 {
842 lg2::warning(
843 "Failed reading DI property for the location code : {LOC_CODE} from "
844 "interface: {IFACE} exception: {ERROR}",
845 "LOC_CODE", locationCode, "IFACE", interface::viniRecordVPD,
846 "ERROR", e);
847 return std::nullopt;
848 }
849
850 return viniDI;
851}
852
853std::optional<bool>
854 DataInterfaceBase::isDIMMLocCode(const std::string& locCode) const
855{
856 if (_locationCache.contains(locCode))
857 {
858 return _locationCache.at(locCode);
859 }
860 else
861 {
862 return std::nullopt;
863 }
864}
865
866void DataInterfaceBase::addDIMMLocCode(const std::string& locCode,
867 bool isFRUDIMM)
868{
869 _locationCache.insert({locCode, isFRUDIMM});
870}
871
Arya K Padmanced8ed72024-09-02 05:18:07 -0500872bool DataInterfaceBase::isDIMM(const std::string& locCode)
Arya K Padmand8ae6182024-07-19 06:25:10 -0500873{
874 auto isDIMMType = isDIMMLocCode(locCode);
875 if (isDIMMType.has_value())
876 {
877 return isDIMMType.value();
878 }
879#ifndef PEL_ENABLE_PHAL
Arya K Padmanced8ed72024-09-02 05:18:07 -0500880 return false;
Arya K Padmand8ae6182024-07-19 06:25:10 -0500881#else
882 else
883 {
884 // Invoke pHAL API inorder to fetch the FRU Type
885 auto fruType = openpower::phal::pdbg::getFRUType(locCode);
Arya K Padmanced8ed72024-09-02 05:18:07 -0500886 bool isDIMMFRU{false};
Arya K Padmand8ae6182024-07-19 06:25:10 -0500887 if (fruType.has_value())
888 {
Arya K Padmand8ae6182024-07-19 06:25:10 -0500889 if (fruType.value() == ENUM_ATTR_TYPE_DIMM)
890 {
891 isDIMMFRU = true;
892 }
893 addDIMMLocCode(locCode, isDIMMFRU);
Arya K Padmand8ae6182024-07-19 06:25:10 -0500894 }
Arya K Padmanced8ed72024-09-02 05:18:07 -0500895 return isDIMMFRU;
Arya K Padmand8ae6182024-07-19 06:25:10 -0500896 }
897#endif
898}
899
harsh-agarwal1d763db32024-09-03 09:18:50 -0500900DBusPathList DataInterface::getAssociatedPaths(
901 const DBusPath& associatedPath, const DBusPath& subtree, int32_t depth,
902 const DBusInterfaceList& interfaces) const
903{
904 DBusPathList paths;
905 try
906 {
907 auto method = _bus.new_method_call(
908 service_name::objectMapper, object_path::objectMapper,
909 interface::objectMapper, "GetAssociatedSubTreePaths");
910 method.append(sdbusplus::message::object_path(associatedPath),
911 sdbusplus::message::object_path(subtree), depth,
912 interfaces);
913
914 auto reply = _bus.call(method, dbusTimeout);
915 reply.read(paths);
916 }
917 catch (const std::exception& e)
918 {
919 std::string ifaces(
920 std::ranges::fold_left_first(
921 interfaces,
922 [](std::string ifaces, const std::string& iface) {
923 return ifaces + ", " + iface;
924 })
925 .value_or(""));
926
927 lg2::error("Failed getting associated paths: {ERROR}. "
928 "AssociatedPath: {ASSOIC_PATH} Subtree: {SUBTREE} "
929 "Interfaces: {IFACES}",
930 "ERROR", e, "ASSOIC_PATH", associatedPath, "SUBTREE",
931 subtree, "IFACES", ifaces);
932 }
933 return paths;
934}
935
Matt Spinler5b423652023-05-04 13:08:44 -0500936void DataInterface::startFruPlugWatch()
937{
938 // Add a watch on inventory InterfacesAdded and then find all
939 // existing hotpluggable interfaces and add propertiesChanged
940 // watches on them.
941
942 _invIaMatch = std::make_unique<sdbusplus::bus::match_t>(
943 _bus, match_rules::interfacesAdded(object_path::baseInv),
944 std::bind(&DataInterface::inventoryIfaceAdded, this,
945 std::placeholders::_1));
946 try
947 {
948 auto paths = getPaths(hotplugInterfaces);
949
950 _invPresentMatches.clear();
951
952 std::for_each(paths.begin(), paths.end(),
953 [this](const auto& path) { addHotplugWatch(path); });
954 }
955 catch (const sdbusplus::exception_t& e)
956 {
Matt Spinlera167a7d2023-06-30 15:14:25 -0500957 lg2::warning("Failed getting FRU paths to watch: {ERROR}", "ERROR", e);
Matt Spinler5b423652023-05-04 13:08:44 -0500958 }
959}
960
961void DataInterface::addHotplugWatch(const std::string& path)
962{
963 if (!_invPresentMatches.contains(path))
964 {
965 _invPresentMatches.emplace(
966 path,
967 std::make_unique<sdbusplus::bus::match_t>(
968 _bus, match_rules::propertiesChanged(path, interface::invItem),
969 std::bind(&DataInterface::presenceChanged, this,
970 std::placeholders::_1)));
971 }
972}
973
974void DataInterface::inventoryIfaceAdded(sdbusplus::message_t& msg)
975{
976 sdbusplus::message::object_path path;
977 DBusInterfaceMap interfaces;
978
979 msg.read(path, interfaces);
980
981 // Check if any of the new interfaces are for hot pluggable FRUs.
982 if (std::find_if(interfaces.begin(), interfaces.end(),
983 [](const auto& interfacePair) {
Patrick Williams075c7922024-08-16 15:19:49 -0400984 return std::find(hotplugInterfaces.begin(),
985 hotplugInterfaces.end(),
986 interfacePair.first) !=
987 hotplugInterfaces.end();
988 }) == interfaces.end())
Matt Spinler5b423652023-05-04 13:08:44 -0500989 {
990 return;
991 }
992
993 addHotplugWatch(path.str);
994
995 // If an Inventory.Item interface was also added, check presence now.
996
997 // Notes:
998 // * This assumes the Inv.Item and Inv.Fan/PS are added together which
999 // is currently the case.
1000 // * If the code ever switches to something without a Present
1001 // property, then the IA signal itself would probably indicate presence.
1002
1003 auto itemIt = interfaces.find(interface::invItem);
1004 if (itemIt != interfaces.end())
1005 {
1006 notifyPresenceSubsribers(path.str, itemIt->second);
1007 }
1008}
1009
1010void DataInterface::presenceChanged(sdbusplus::message_t& msg)
1011{
1012 DBusInterface interface;
1013 DBusPropertyMap properties;
1014
1015 msg.read(interface, properties);
1016 if (interface != interface::invItem)
1017 {
1018 return;
1019 }
1020
1021 std::string path = msg.get_path();
1022 notifyPresenceSubsribers(path, properties);
1023}
1024
1025void DataInterface::notifyPresenceSubsribers(const std::string& path,
1026 const DBusPropertyMap& properties)
1027{
Matt Spinler5ee36052023-05-30 14:20:56 -05001028 auto prop = properties.find("Present");
1029 if ((prop == properties.end()) || (!std::get<bool>(prop->second)))
1030 {
1031 return;
1032 }
1033
1034 std::string locCode;
1035
Matt Spinler5b423652023-05-04 13:08:44 -05001036 try
1037 {
Matt Spinler52ee3a42023-07-27 14:54:48 -05001038 auto service = getService(path, interface::locCode);
1039
1040 // If the hotplugged FRU is hosted by PLDM, then it is
1041 // in an IO expansion drawer and we don't care about it.
1042 if (service == service_name::pldm)
1043 {
1044 return;
1045 }
1046
Matt Spinler5ee36052023-05-30 14:20:56 -05001047 locCode = getLocationCode(path);
Matt Spinler5b423652023-05-04 13:08:44 -05001048 }
Matt Spinler5ee36052023-05-30 14:20:56 -05001049 catch (const sdbusplus::exception_t& e)
Matt Spinler5b423652023-05-04 13:08:44 -05001050 {
Matt Spinlera167a7d2023-06-30 15:14:25 -05001051 lg2::debug("Could not get location code for {PATH}: {ERROR}", "PATH",
1052 path, "ERROR", e);
Matt Spinler5ee36052023-05-30 14:20:56 -05001053 return;
Matt Spinler5b423652023-05-04 13:08:44 -05001054 }
Matt Spinler5ee36052023-05-30 14:20:56 -05001055
Matt Spinlera167a7d2023-06-30 15:14:25 -05001056 lg2::debug("Detected FRU {PATH} ({LOC}) present ", "PATH", path, "LOC",
1057 locCode);
Matt Spinler5ee36052023-05-30 14:20:56 -05001058
1059 // Tell the subscribers.
1060 setFruPresent(locCode);
Matt Spinler5b423652023-05-04 13:08:44 -05001061}
Arya K Padmanafba3162024-08-30 07:41:32 -05001062
Arya K Padmanafba3162024-08-30 07:41:32 -05001063bool DataInterface::isPHALDevTreeExist() const
1064{
1065 try
1066 {
1067 if (std::filesystem::exists(PDBG_DTB_PATH))
1068 {
1069 return true;
1070 }
1071 }
1072 catch (const std::exception& e)
1073 {
1074 lg2::error("Failed to check device tree {PHAL_DEVTREE_PATH} existence, "
1075 "{ERROR}",
1076 "PHAL_DEVTREE_PATH", PDBG_DTB_PATH, "ERROR", e);
1077 }
1078 return false;
1079}
1080
Arya K Padman0b758fb2024-09-06 11:59:45 -05001081#ifdef PEL_ENABLE_PHAL
Arya K Padmanafba3162024-08-30 07:41:32 -05001082void DataInterface::initPHAL()
1083{
1084 if (setenv("PDBG_DTB", PDBG_DTB_PATH, 1))
1085 {
1086 // Log message and continue,
1087 // This is to help continue creating PEL in raw format.
1088 lg2::error("Failed to set PDBG_DTB: ({ERRNO})", "ERRNO",
1089 strerror(errno));
1090 }
1091
1092 if (!pdbg_targets_init(NULL))
1093 {
1094 lg2::error("pdbg_targets_init failed");
1095 return;
1096 }
1097
1098 if (libekb_init())
1099 {
1100 lg2::error("libekb_init failed, skipping ffdc processing");
1101 return;
1102 }
1103}
Arya K Padman0b758fb2024-09-06 11:59:45 -05001104#endif
Arya K Padmanafba3162024-08-30 07:41:32 -05001105
1106void DataInterface::subscribeToSystemdSignals()
1107{
1108 try
1109 {
1110 auto method =
1111 _bus.new_method_call(service_name::systemd, object_path::systemd,
1112 interface::systemdMgr, "Subscribe");
1113 _systemdSlot = method.call_async([this](sdbusplus::message_t&& msg) {
1114 // Initializing with nullptr to indicate that it is not subscribed
1115 // to any signal.
1116 this->_systemdSlot = sdbusplus::slot_t(nullptr);
1117 if (msg.is_method_error())
1118 {
1119 auto* error = msg.get_error();
1120 lg2::error("Failed to subscribe JobRemoved systemd signal, "
Arya K Padmanea001072024-09-19 05:00:50 -05001121 "errorName: {ERR_NAME}, errorMsg: {ERR_MSG} ",
Arya K Padman0b758fb2024-09-06 11:59:45 -05001122 "ERR_NAME", error->name, "ERR_MSG", error->message);
Arya K Padmanafba3162024-08-30 07:41:32 -05001123 return;
1124 }
1125
1126 namespace sdbusRule = sdbusplus::bus::match::rules;
1127 this->_systemdMatch =
1128 std::make_unique<decltype(this->_systemdMatch)::element_type>(
1129 this->_bus,
1130 sdbusRule::type::signal() +
1131 sdbusRule::member("JobRemoved") +
1132 sdbusRule::path(object_path::systemd) +
1133 sdbusRule::interface(interface::systemdMgr),
1134 [this](sdbusplus::message_t& msg) {
1135 uint32_t jobID;
1136 sdbusplus::message::object_path jobObjPath;
1137 std::string jobUnitName, jobUnitResult;
1138
1139 msg.read(jobID, jobObjPath, jobUnitName, jobUnitResult);
1140 if ((jobUnitName ==
1141 "openpower-update-bios-attr-table.service") &&
1142 (jobUnitResult == "done"))
1143 {
Arya K Padmanea001072024-09-19 05:00:50 -05001144#ifdef PEL_ENABLE_PHAL
1145 this->initPHAL();
1146#endif
1147 // Invoke unsubscribe method to stop monitoring for
1148 // JobRemoved signals.
Arya K Padmanafba3162024-08-30 07:41:32 -05001149 this->unsubscribeFromSystemdSignals();
1150 }
1151 });
1152 });
1153 }
1154 catch (const sdbusplus::exception_t& e)
1155 {
1156 lg2::error(
1157 "Exception occured while handling JobRemoved systemd signal, "
1158 "exception: {ERROR}",
1159 "ERROR", e);
1160 }
1161}
1162
1163void DataInterface::unsubscribeFromSystemdSignals()
1164{
1165 try
1166 {
1167 auto method =
1168 _bus.new_method_call(service_name::systemd, object_path::systemd,
1169 interface::systemdMgr, "Unsubscribe");
1170 _systemdSlot = method.call_async([this](sdbusplus::message_t&& msg) {
1171 // Unsubscribing the _systemdSlot from the subscribed signal
1172 this->_systemdSlot = sdbusplus::slot_t(nullptr);
1173 if (msg.is_method_error())
1174 {
1175 auto* error = msg.get_error();
1176 lg2::error(
1177 "Failed to unsubscribe from JobRemoved systemd signal, "
Arya K Padmanea001072024-09-19 05:00:50 -05001178 "errorName: {ERR_NAME}, errorMsg: {ERR_MSG} ",
Arya K Padman0b758fb2024-09-06 11:59:45 -05001179 "ERR_NAME", error->name, "ERR_MSG", error->message);
Arya K Padmanafba3162024-08-30 07:41:32 -05001180 return;
1181 }
Arya K Padmanea001072024-09-19 05:00:50 -05001182 // Reset _systemdMatch to avoid reception of further JobRemoved
1183 // signals
1184 this->_systemdMatch.reset();
Arya K Padmanafba3162024-08-30 07:41:32 -05001185 });
1186 }
1187 catch (const sdbusplus::exception_t& e)
1188 {
1189 lg2::error(
1190 "Exception occured while unsubscribing from JobRemoved systemd signal, "
1191 "exception: {ERROR}",
1192 "ERROR", e);
1193 }
1194}
Arya K Padmanafba3162024-08-30 07:41:32 -05001195
Matt Spinlerc8705e22019-09-11 12:36:07 -05001196} // namespace pels
1197} // namespace openpower