xref: /openbmc/fb-ipmi-oem/src/commandutils.cpp (revision b2ae88b4147022841172add9be986362f0034136)
1*b2ae88b4Scchoux /*
2*b2ae88b4Scchoux  * Copyright (c)  2023-present Facebook.
3*b2ae88b4Scchoux  *
4*b2ae88b4Scchoux  * Licensed under the Apache License, Version 2.0 (the "License");
5*b2ae88b4Scchoux  * you may not use this file except in compliance with the License.
6*b2ae88b4Scchoux  * You may obtain a copy of the License at
7*b2ae88b4Scchoux  *
8*b2ae88b4Scchoux  *      http://www.apache.org/licenses/LICENSE-2.0
9*b2ae88b4Scchoux  *
10*b2ae88b4Scchoux  * Unless required by applicable law or agreed to in writing, software
11*b2ae88b4Scchoux  * distributed under the License is distributed on an "AS IS" BASIS,
12*b2ae88b4Scchoux  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*b2ae88b4Scchoux  * See the License for the specific language governing permissions and
14*b2ae88b4Scchoux  * limitations under the License.
15*b2ae88b4Scchoux  */
16*b2ae88b4Scchoux 
17*b2ae88b4Scchoux #include <commandutils.hpp>
18*b2ae88b4Scchoux #include <ipmid/utils.hpp>
19*b2ae88b4Scchoux #include <phosphor-logging/log.hpp>
20*b2ae88b4Scchoux 
21*b2ae88b4Scchoux std::optional<std::pair<uint8_t, uint8_t>> getMbFruDevice(void)
22*b2ae88b4Scchoux {
23*b2ae88b4Scchoux     static std::optional<std::pair<uint8_t, uint8_t>> device = std::nullopt;
24*b2ae88b4Scchoux 
25*b2ae88b4Scchoux     if (device)
26*b2ae88b4Scchoux     {
27*b2ae88b4Scchoux         return device;
28*b2ae88b4Scchoux     }
29*b2ae88b4Scchoux 
30*b2ae88b4Scchoux     sdbusplus::bus_t dbus(ipmid_get_sd_bus_connection());
31*b2ae88b4Scchoux     auto mapperCall = dbus.new_method_call("xyz.openbmc_project.ObjectMapper",
32*b2ae88b4Scchoux                                            "/xyz/openbmc_project/object_mapper",
33*b2ae88b4Scchoux                                            "xyz.openbmc_project.ObjectMapper",
34*b2ae88b4Scchoux                                            "GetSubTreePaths");
35*b2ae88b4Scchoux     static constexpr int32_t depth = 0;
36*b2ae88b4Scchoux     static constexpr auto iface = "xyz.openbmc_project.Configuration.EEPROM";
37*b2ae88b4Scchoux     static constexpr auto entityManager = "xyz.openbmc_project.EntityManager";
38*b2ae88b4Scchoux     static constexpr std::array<const char*, 1> interface = {iface};
39*b2ae88b4Scchoux     mapperCall.append("/xyz/openbmc_project/inventory/", depth, interface);
40*b2ae88b4Scchoux 
41*b2ae88b4Scchoux     std::vector<std::string> paths;
42*b2ae88b4Scchoux     try
43*b2ae88b4Scchoux     {
44*b2ae88b4Scchoux         auto resp = dbus.call(mapperCall);
45*b2ae88b4Scchoux         resp.read(paths);
46*b2ae88b4Scchoux     }
47*b2ae88b4Scchoux     catch (const sdbusplus::exception_t& e)
48*b2ae88b4Scchoux     {
49*b2ae88b4Scchoux         phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
50*b2ae88b4Scchoux         return std::nullopt;
51*b2ae88b4Scchoux     }
52*b2ae88b4Scchoux 
53*b2ae88b4Scchoux     const std::string suffix = "/MB_FRU";
54*b2ae88b4Scchoux     for (const auto& path : paths)
55*b2ae88b4Scchoux     {
56*b2ae88b4Scchoux         if (path.ends_with(suffix))
57*b2ae88b4Scchoux         {
58*b2ae88b4Scchoux             uint8_t fruBus = std::get<uint64_t>(
59*b2ae88b4Scchoux                 ipmi::getDbusProperty(dbus, entityManager, path, iface, "Bus"));
60*b2ae88b4Scchoux             uint8_t fruAddr = std::get<uint64_t>(ipmi::getDbusProperty(
61*b2ae88b4Scchoux                 dbus, entityManager, path, iface, "Address"));
62*b2ae88b4Scchoux             device = std::make_pair(fruBus, fruAddr);
63*b2ae88b4Scchoux             break;
64*b2ae88b4Scchoux         }
65*b2ae88b4Scchoux     }
66*b2ae88b4Scchoux 
67*b2ae88b4Scchoux     return device;
68*b2ae88b4Scchoux }
69