blob: 09ed5d31d5c3697de7fa19e61188a1b1e9331889 [file] [log] [blame]
Lei YU5e0dcb32019-08-02 18:04:34 +08001#include "config.h"
2
3#include "utils.hpp"
4
George Liu0b7c7b32022-02-07 16:30:23 +08005#include <openssl/evp.h>
Lei YUad90ad52019-08-06 11:19:28 +08006
Patrick Williams5670b182023-05-10 07:50:50 -05007#include <phosphor-logging/log.hpp>
8
Lei YU4b9ac392019-10-12 11:02:26 +08009#include <algorithm>
Lei YU5e0dcb32019-08-02 18:04:34 +080010#include <fstream>
Lei YU5f3584d2019-08-27 16:28:53 +080011#include <sstream>
Lei YUf77189f2019-08-07 14:26:30 +080012
13using namespace phosphor::logging;
Lei YU5e0dcb32019-08-02 18:04:34 +080014
15namespace utils
16{
17
18namespace // anonymous
19{
20constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
21constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
22constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
23} // namespace
24
Lei YU5f3584d2019-08-27 16:28:53 +080025namespace internal
26{
27template <typename... Ts>
Patrick Williams5670b182023-05-10 07:50:50 -050028std::string concat_string(const Ts&... ts)
Lei YU5f3584d2019-08-27 16:28:53 +080029{
30 std::stringstream s;
31 ((s << ts << " "), ...) << std::endl;
32 return s.str();
33}
34
35// Helper function to run command
36// Returns return code and the stdout
37template <typename... Ts>
Patrick Williams5670b182023-05-10 07:50:50 -050038std::pair<int, std::string> exec(const Ts&... ts)
Lei YU5f3584d2019-08-27 16:28:53 +080039{
40 std::array<char, 512> buffer;
41 std::string cmd = concat_string(ts...);
42 std::stringstream result;
43 int rc;
44 FILE* pipe = popen(cmd.c_str(), "r");
45 if (!pipe)
46 {
47 throw std::runtime_error("popen() failed!");
48 }
49 while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
50 {
51 result << buffer.data();
52 }
53 rc = pclose(pipe);
54 return {rc, result.str()};
55}
56
57} // namespace internal
Lei YUf77189f2019-08-07 14:26:30 +080058const UtilsInterface& getUtils()
59{
60 static Utils utils;
61 return utils;
62}
63
Patrick Williams374fae52022-07-22 19:26:55 -050064std::vector<std::string> Utils::getPSUInventoryPath(sdbusplus::bus_t& bus) const
Lei YU5e0dcb32019-08-02 18:04:34 +080065{
66 std::vector<std::string> paths;
67 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
68 MAPPER_INTERFACE, "GetSubTreePaths");
69 method.append(PSU_INVENTORY_PATH_BASE);
70 method.append(0); // Depth 0 to search all
71 method.append(std::vector<std::string>({PSU_INVENTORY_IFACE}));
72 auto reply = bus.call(method);
73
74 reply.read(paths);
75 return paths;
76}
77
Patrick Williams374fae52022-07-22 19:26:55 -050078std::string Utils::getService(sdbusplus::bus_t& bus, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +080079 const char* interface) const
Lei YUad90ad52019-08-06 11:19:28 +080080{
Lei YUd0bbfa92019-09-11 16:10:54 +080081 auto services = getServices(bus, path, interface);
82 if (services.empty())
83 {
84 return {};
85 }
86 return services[0];
87}
88
Patrick Williams374fae52022-07-22 19:26:55 -050089std::vector<std::string> Utils::getServices(sdbusplus::bus_t& bus,
Lei YUd0bbfa92019-09-11 16:10:54 +080090 const char* path,
91 const char* interface) const
92{
Lei YUad90ad52019-08-06 11:19:28 +080093 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
94 MAPPER_INTERFACE, "GetObject");
95
96 mapper.append(path, std::vector<std::string>({interface}));
97 try
98 {
99 auto mapperResponseMsg = bus.call(mapper);
100
101 std::vector<std::pair<std::string, std::vector<std::string>>>
102 mapperResponse;
103 mapperResponseMsg.read(mapperResponse);
104 if (mapperResponse.empty())
105 {
106 log<level::ERR>("Error reading mapper response");
107 throw std::runtime_error("Error reading mapper response");
108 }
Lei YUd0bbfa92019-09-11 16:10:54 +0800109 std::vector<std::string> ret;
110 for (const auto& i : mapperResponse)
Lei YUad90ad52019-08-06 11:19:28 +0800111 {
Lei YUd0bbfa92019-09-11 16:10:54 +0800112 ret.emplace_back(i.first);
Lei YUad90ad52019-08-06 11:19:28 +0800113 }
Lei YUd0bbfa92019-09-11 16:10:54 +0800114 return ret;
Lei YUad90ad52019-08-06 11:19:28 +0800115 }
Patrick Williams374fae52022-07-22 19:26:55 -0500116 catch (const sdbusplus::exception_t& ex)
Lei YUad90ad52019-08-06 11:19:28 +0800117 {
Lei YU2d156252019-10-18 10:02:20 +0800118 log<level::ERR>("GetObject call failed", entry("PATH=%s", path),
Lei YUad90ad52019-08-06 11:19:28 +0800119 entry("INTERFACE=%s", interface));
Lei YU2d156252019-10-18 10:02:20 +0800120 throw std::runtime_error("GetObject call failed");
Lei YUad90ad52019-08-06 11:19:28 +0800121 }
122}
123
Lei YUf77189f2019-08-07 14:26:30 +0800124std::string Utils::getVersionId(const std::string& version) const
Lei YUad90ad52019-08-06 11:19:28 +0800125{
126 if (version.empty())
127 {
128 log<level::ERR>("Error version is empty");
129 return {};
130 }
131
George Liu0b7c7b32022-02-07 16:30:23 +0800132 using EVP_MD_CTX_Ptr =
133 std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>;
134
135 std::array<unsigned char, EVP_MAX_MD_SIZE> digest{};
136 EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), &::EVP_MD_CTX_free);
137
138 EVP_DigestInit(ctx.get(), EVP_sha512());
139 EVP_DigestUpdate(ctx.get(), version.c_str(), strlen(version.c_str()));
140 EVP_DigestFinal(ctx.get(), digest.data(), nullptr);
Lei YUad90ad52019-08-06 11:19:28 +0800141
142 // Only need 8 hex digits.
George Liu0b7c7b32022-02-07 16:30:23 +0800143 char mdString[9];
144 snprintf(mdString, sizeof(mdString), "%02x%02x%02x%02x",
145 (unsigned int)digest[0], (unsigned int)digest[1],
146 (unsigned int)digest[2], (unsigned int)digest[3]);
147
148 return mdString;
Lei YUad90ad52019-08-06 11:19:28 +0800149}
150
Lei YU5f3584d2019-08-27 16:28:53 +0800151std::string Utils::getVersion(const std::string& inventoryPath) const
152{
153 // Invoke vendor-specify tool to get the version string, e.g.
154 // psutils get-version
155 // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0
156 auto [rc, r] = internal::exec(PSU_VERSION_UTIL, inventoryPath);
157 return (rc == 0) ? r : "";
158}
159
Lei YU65207482019-10-11 16:39:36 +0800160std::string Utils::getLatestVersion(const std::set<std::string>& versions) const
161{
162 if (versions.empty())
163 {
164 return {};
165 }
166 std::stringstream args;
167 for (const auto& s : versions)
168 {
169 args << s << " ";
170 }
171 auto [rc, r] = internal::exec(PSU_VERSION_COMPARE_UTIL, args.str());
172 return (rc == 0) ? r : "";
173}
174
Lei YU4b9ac392019-10-12 11:02:26 +0800175bool Utils::isAssociated(const std::string& psuInventoryPath,
176 const AssociationList& assocs) const
177{
178 return std::find_if(assocs.begin(), assocs.end(),
179 [&psuInventoryPath](const auto& assoc) {
Patrick Williams5670b182023-05-10 07:50:50 -0500180 return psuInventoryPath == std::get<2>(assoc);
181 }) != assocs.end();
Lei YU4b9ac392019-10-12 11:02:26 +0800182}
183
Patrick Williams374fae52022-07-22 19:26:55 -0500184any Utils::getPropertyImpl(sdbusplus::bus_t& bus, const char* service,
Lei YUf77189f2019-08-07 14:26:30 +0800185 const char* path, const char* interface,
186 const char* propertyName) const
187{
188 auto method = bus.new_method_call(service, path,
189 "org.freedesktop.DBus.Properties", "Get");
190 method.append(interface, propertyName);
191 try
192 {
193 PropertyType value{};
194 auto reply = bus.call(method);
195 reply.read(value);
196 return any(value);
197 }
Patrick Williams374fae52022-07-22 19:26:55 -0500198 catch (const sdbusplus::exception_t& ex)
Lei YUf77189f2019-08-07 14:26:30 +0800199 {
200 log<level::ERR>("GetProperty call failed", entry("PATH=%s", path),
201 entry("INTERFACE=%s", interface),
202 entry("PROPERTY=%s", propertyName));
203 throw std::runtime_error("GetProperty call failed");
204 }
205}
206
Lei YU5e0dcb32019-08-02 18:04:34 +0800207} // namespace utils