blob: abea750b45a68e2fb76c507e4bb022c65f8714cf [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
Shawn McCarneycdf86de2024-11-26 10:02:14 -06007#include <phosphor-logging/lg2.hpp>
Patrick Williams5670b182023-05-10 07:50:50 -05008
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
Lei YU5e0dcb32019-08-02 18:04:34 +080013namespace utils
14{
15
16namespace // anonymous
17{
18constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
19constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
20constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
21} // namespace
22
Lei YU5f3584d2019-08-27 16:28:53 +080023namespace internal
24{
25template <typename... Ts>
Patrick Williams5670b182023-05-10 07:50:50 -050026std::string concat_string(const Ts&... ts)
Lei YU5f3584d2019-08-27 16:28:53 +080027{
28 std::stringstream s;
29 ((s << ts << " "), ...) << std::endl;
30 return s.str();
31}
32
33// Helper function to run command
34// Returns return code and the stdout
35template <typename... Ts>
Patrick Williams5670b182023-05-10 07:50:50 -050036std::pair<int, std::string> exec(const Ts&... ts)
Lei YU5f3584d2019-08-27 16:28:53 +080037{
38 std::array<char, 512> buffer;
39 std::string cmd = concat_string(ts...);
40 std::stringstream result;
41 int rc;
42 FILE* pipe = popen(cmd.c_str(), "r");
43 if (!pipe)
44 {
45 throw std::runtime_error("popen() failed!");
46 }
47 while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
48 {
49 result << buffer.data();
50 }
51 rc = pclose(pipe);
52 return {rc, result.str()};
53}
54
55} // namespace internal
Lei YUf77189f2019-08-07 14:26:30 +080056const UtilsInterface& getUtils()
57{
58 static Utils utils;
59 return utils;
60}
61
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -060062std::vector<std::string>
63 Utils::getPSUInventoryPaths(sdbusplus::bus_t& bus) const
Lei YU5e0dcb32019-08-02 18:04:34 +080064{
65 std::vector<std::string> paths;
Matt Spinlere183edc2024-07-09 11:25:34 -050066 try
67 {
68 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
69 MAPPER_INTERFACE, "GetSubTreePaths");
70 method.append(PSU_INVENTORY_PATH_BASE);
71 method.append(0); // Depth 0 to search all
72 method.append(std::vector<std::string>({PSU_INVENTORY_IFACE}));
73 auto reply = bus.call(method);
Lei YU5e0dcb32019-08-02 18:04:34 +080074
Matt Spinlere183edc2024-07-09 11:25:34 -050075 reply.read(paths);
76 }
77 catch (const sdbusplus::exception_t&)
78 {
79 // Inventory base path not there yet.
80 }
Lei YU5e0dcb32019-08-02 18:04:34 +080081 return paths;
82}
83
Patrick Williams374fae52022-07-22 19:26:55 -050084std::string Utils::getService(sdbusplus::bus_t& bus, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +080085 const char* interface) const
Lei YUad90ad52019-08-06 11:19:28 +080086{
Lei YUd0bbfa92019-09-11 16:10:54 +080087 auto services = getServices(bus, path, interface);
88 if (services.empty())
89 {
90 return {};
91 }
92 return services[0];
93}
94
Patrick Williamsbab5ed92024-08-16 15:20:54 -040095std::vector<std::string> Utils::getServices(
96 sdbusplus::bus_t& bus, const char* path, const char* interface) const
Lei YUd0bbfa92019-09-11 16:10:54 +080097{
Lei YUad90ad52019-08-06 11:19:28 +080098 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
99 MAPPER_INTERFACE, "GetObject");
100
101 mapper.append(path, std::vector<std::string>({interface}));
102 try
103 {
104 auto mapperResponseMsg = bus.call(mapper);
105
106 std::vector<std::pair<std::string, std::vector<std::string>>>
107 mapperResponse;
108 mapperResponseMsg.read(mapperResponse);
109 if (mapperResponse.empty())
110 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600111 lg2::error("Error reading mapper response");
Lei YUad90ad52019-08-06 11:19:28 +0800112 throw std::runtime_error("Error reading mapper response");
113 }
Lei YUd0bbfa92019-09-11 16:10:54 +0800114 std::vector<std::string> ret;
George Liuaf025bc2024-08-23 14:54:42 +0800115 ret.reserve(mapperResponse.size());
Lei YUd0bbfa92019-09-11 16:10:54 +0800116 for (const auto& i : mapperResponse)
Lei YUad90ad52019-08-06 11:19:28 +0800117 {
Lei YUd0bbfa92019-09-11 16:10:54 +0800118 ret.emplace_back(i.first);
Lei YUad90ad52019-08-06 11:19:28 +0800119 }
Lei YUd0bbfa92019-09-11 16:10:54 +0800120 return ret;
Lei YUad90ad52019-08-06 11:19:28 +0800121 }
Patrick Williams374fae52022-07-22 19:26:55 -0500122 catch (const sdbusplus::exception_t& ex)
Lei YUad90ad52019-08-06 11:19:28 +0800123 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600124 lg2::error("GetObject call failed: path={PATH}, interface={INTERFACE}",
125 "PATH", path, "INTERFACE", interface);
Lei YU2d156252019-10-18 10:02:20 +0800126 throw std::runtime_error("GetObject call failed");
Lei YUad90ad52019-08-06 11:19:28 +0800127 }
128}
129
Lei YUf77189f2019-08-07 14:26:30 +0800130std::string Utils::getVersionId(const std::string& version) const
Lei YUad90ad52019-08-06 11:19:28 +0800131{
132 if (version.empty())
133 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600134 lg2::error("Error version is empty");
Lei YUad90ad52019-08-06 11:19:28 +0800135 return {};
136 }
137
George Liu0b7c7b32022-02-07 16:30:23 +0800138 using EVP_MD_CTX_Ptr =
139 std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>;
140
141 std::array<unsigned char, EVP_MAX_MD_SIZE> digest{};
142 EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), &::EVP_MD_CTX_free);
143
144 EVP_DigestInit(ctx.get(), EVP_sha512());
145 EVP_DigestUpdate(ctx.get(), version.c_str(), strlen(version.c_str()));
146 EVP_DigestFinal(ctx.get(), digest.data(), nullptr);
Lei YUad90ad52019-08-06 11:19:28 +0800147
148 // Only need 8 hex digits.
George Liu0b7c7b32022-02-07 16:30:23 +0800149 char mdString[9];
150 snprintf(mdString, sizeof(mdString), "%02x%02x%02x%02x",
151 (unsigned int)digest[0], (unsigned int)digest[1],
152 (unsigned int)digest[2], (unsigned int)digest[3]);
153
154 return mdString;
Lei YUad90ad52019-08-06 11:19:28 +0800155}
156
Lei YU5f3584d2019-08-27 16:28:53 +0800157std::string Utils::getVersion(const std::string& inventoryPath) const
158{
Shawn McCarney783406e2024-11-17 21:49:37 -0600159 // Invoke vendor-specific tool to get the version string, e.g.
160 // psutils --get-version
Lei YU5f3584d2019-08-27 16:28:53 +0800161 // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0
162 auto [rc, r] = internal::exec(PSU_VERSION_UTIL, inventoryPath);
163 return (rc == 0) ? r : "";
164}
165
Shawn McCarney783406e2024-11-17 21:49:37 -0600166std::string Utils::getModel(const std::string& inventoryPath) const
167{
168 // Invoke vendor-specific tool to get the model string, e.g.
169 // psutils --get-model
170 // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0
171 auto [rc, r] = internal::exec(PSU_MODEL_UTIL, inventoryPath);
172 return (rc == 0) ? r : "";
173}
174
Lei YU65207482019-10-11 16:39:36 +0800175std::string Utils::getLatestVersion(const std::set<std::string>& versions) const
176{
177 if (versions.empty())
178 {
179 return {};
180 }
181 std::stringstream args;
182 for (const auto& s : versions)
183 {
184 args << s << " ";
185 }
186 auto [rc, r] = internal::exec(PSU_VERSION_COMPARE_UTIL, args.str());
187 return (rc == 0) ? r : "";
188}
189
Lei YU4b9ac392019-10-12 11:02:26 +0800190bool Utils::isAssociated(const std::string& psuInventoryPath,
191 const AssociationList& assocs) const
192{
193 return std::find_if(assocs.begin(), assocs.end(),
194 [&psuInventoryPath](const auto& assoc) {
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400195 return psuInventoryPath == std::get<2>(assoc);
196 }) != assocs.end();
Lei YU4b9ac392019-10-12 11:02:26 +0800197}
198
Patrick Williams374fae52022-07-22 19:26:55 -0500199any Utils::getPropertyImpl(sdbusplus::bus_t& bus, const char* service,
Lei YUf77189f2019-08-07 14:26:30 +0800200 const char* path, const char* interface,
201 const char* propertyName) const
202{
203 auto method = bus.new_method_call(service, path,
204 "org.freedesktop.DBus.Properties", "Get");
205 method.append(interface, propertyName);
206 try
207 {
208 PropertyType value{};
209 auto reply = bus.call(method);
210 reply.read(value);
211 return any(value);
212 }
Patrick Williams374fae52022-07-22 19:26:55 -0500213 catch (const sdbusplus::exception_t& ex)
Lei YUf77189f2019-08-07 14:26:30 +0800214 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600215 lg2::error(
216 "GetProperty call failed: path={PATH}, interface={INTERFACE}, "
217 "property={PROPERTY}",
218 "PATH", path, "INTERFACE", interface, "PROPERTY", propertyName);
Lei YUf77189f2019-08-07 14:26:30 +0800219 throw std::runtime_error("GetProperty call failed");
220 }
221}
222
Lei YU5e0dcb32019-08-02 18:04:34 +0800223} // namespace utils