Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "utils.hpp" |
| 4 | |
George Liu | 0b7c7b3 | 2022-02-07 16:30:23 +0800 | [diff] [blame] | 5 | #include <openssl/evp.h> |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 6 | |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 8 | |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 9 | #include <algorithm> |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 10 | #include <cerrno> |
| 11 | #include <cstring> |
| 12 | #include <exception> |
| 13 | #include <format> |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 14 | #include <fstream> |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 15 | #include <sstream> |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 16 | #include <stdexcept> |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 17 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 18 | namespace utils |
| 19 | { |
| 20 | |
| 21 | namespace // anonymous |
| 22 | { |
| 23 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 24 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 25 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 26 | } // namespace |
| 27 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 28 | namespace internal |
| 29 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * @brief Concatenate the specified values, separated by spaces, and return |
| 33 | * the resulting string. |
| 34 | * |
| 35 | * @param[in] ts - Parameter pack of values to concatenate |
| 36 | * |
| 37 | * @return Parameter values separated by spaces |
| 38 | */ |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 39 | template <typename... Ts> |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 40 | std::string concat_string(const Ts&... ts) |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 41 | { |
| 42 | std::stringstream s; |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 43 | ((s << ts << " "), ...); |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 44 | return s.str(); |
| 45 | } |
| 46 | |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 47 | /** |
| 48 | * @brief Execute the specified command. |
| 49 | * |
| 50 | * @details Returns a pair containing the exit status and command output. |
| 51 | * Throws an exception if an error occurs. Note that a command that |
| 52 | * returns a non-zero exit status is not considered an error. |
| 53 | * |
| 54 | * @param[in] ts - Parameter pack of command and parameters |
| 55 | * |
| 56 | * @return Exit status and standard output from the command |
| 57 | */ |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 58 | template <typename... Ts> |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 59 | std::pair<int, std::string> exec(const Ts&... ts) |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 60 | { |
| 61 | std::array<char, 512> buffer; |
| 62 | std::string cmd = concat_string(ts...); |
| 63 | std::stringstream result; |
| 64 | int rc; |
| 65 | FILE* pipe = popen(cmd.c_str(), "r"); |
| 66 | if (!pipe) |
| 67 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 68 | throw std::runtime_error{ |
| 69 | std::format("Unable to execute command '{}': popen() failed: {}", |
| 70 | cmd, std::strerror(errno))}; |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 71 | } |
| 72 | while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) |
| 73 | { |
| 74 | result << buffer.data(); |
| 75 | } |
| 76 | rc = pclose(pipe); |
| 77 | return {rc, result.str()}; |
| 78 | } |
| 79 | |
| 80 | } // namespace internal |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 81 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 82 | const UtilsInterface& getUtils() |
| 83 | { |
| 84 | static Utils utils; |
| 85 | return utils; |
| 86 | } |
| 87 | |
Shawn McCarney | d57bd2f | 2024-12-02 18:40:28 -0600 | [diff] [blame] | 88 | std::vector<std::string> |
| 89 | Utils::getPSUInventoryPaths(sdbusplus::bus_t& bus) const |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 90 | { |
| 91 | std::vector<std::string> paths; |
Matt Spinler | e183edc | 2024-07-09 11:25:34 -0500 | [diff] [blame] | 92 | try |
| 93 | { |
| 94 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 95 | MAPPER_INTERFACE, "GetSubTreePaths"); |
| 96 | method.append(PSU_INVENTORY_PATH_BASE); |
| 97 | method.append(0); // Depth 0 to search all |
| 98 | method.append(std::vector<std::string>({PSU_INVENTORY_IFACE})); |
| 99 | auto reply = bus.call(method); |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 100 | |
Matt Spinler | e183edc | 2024-07-09 11:25:34 -0500 | [diff] [blame] | 101 | reply.read(paths); |
| 102 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 103 | catch (const std::exception& e) |
Matt Spinler | e183edc | 2024-07-09 11:25:34 -0500 | [diff] [blame] | 104 | { |
| 105 | // Inventory base path not there yet. |
| 106 | } |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 107 | return paths; |
| 108 | } |
| 109 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 110 | std::string Utils::getService(sdbusplus::bus_t& bus, const char* path, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 111 | const char* interface) const |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 112 | { |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 113 | auto services = getServices(bus, path, interface); |
| 114 | if (services.empty()) |
| 115 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 116 | throw std::runtime_error{std::format( |
| 117 | "No service found for path {}, interface {}", path, interface)}; |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 118 | } |
| 119 | return services[0]; |
| 120 | } |
| 121 | |
Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 122 | std::vector<std::string> Utils::getServices( |
| 123 | sdbusplus::bus_t& bus, const char* path, const char* interface) const |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 124 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 125 | std::vector<std::string> services; |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 126 | try |
| 127 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 128 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 129 | MAPPER_INTERFACE, "GetObject"); |
| 130 | |
| 131 | mapper.append(path, std::vector<std::string>({interface})); |
| 132 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 133 | auto mapperResponseMsg = bus.call(mapper); |
| 134 | |
| 135 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 136 | mapperResponse; |
| 137 | mapperResponseMsg.read(mapperResponse); |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 138 | services.reserve(mapperResponse.size()); |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 139 | for (const auto& i : mapperResponse) |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 140 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 141 | services.emplace_back(i.first); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 142 | } |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 143 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 144 | catch (const std::exception& e) |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 145 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 146 | throw std::runtime_error{ |
| 147 | std::format("Unable to find services for path {}, interface {}: {}", |
| 148 | path, interface, e.what())}; |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 149 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 150 | return services; |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 151 | } |
| 152 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 153 | std::string Utils::getVersionId(const std::string& version) const |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 154 | { |
| 155 | if (version.empty()) |
| 156 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 157 | lg2::error("Error version is empty"); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 158 | return {}; |
| 159 | } |
| 160 | |
George Liu | 0b7c7b3 | 2022-02-07 16:30:23 +0800 | [diff] [blame] | 161 | using EVP_MD_CTX_Ptr = |
| 162 | std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>; |
| 163 | |
| 164 | std::array<unsigned char, EVP_MAX_MD_SIZE> digest{}; |
| 165 | EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), &::EVP_MD_CTX_free); |
| 166 | |
| 167 | EVP_DigestInit(ctx.get(), EVP_sha512()); |
| 168 | EVP_DigestUpdate(ctx.get(), version.c_str(), strlen(version.c_str())); |
| 169 | EVP_DigestFinal(ctx.get(), digest.data(), nullptr); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 170 | |
| 171 | // Only need 8 hex digits. |
George Liu | 0b7c7b3 | 2022-02-07 16:30:23 +0800 | [diff] [blame] | 172 | char mdString[9]; |
| 173 | snprintf(mdString, sizeof(mdString), "%02x%02x%02x%02x", |
| 174 | (unsigned int)digest[0], (unsigned int)digest[1], |
| 175 | (unsigned int)digest[2], (unsigned int)digest[3]); |
| 176 | |
| 177 | return mdString; |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 178 | } |
| 179 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 180 | std::string Utils::getVersion(const std::string& inventoryPath) const |
| 181 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 182 | std::string version; |
| 183 | try |
| 184 | { |
| 185 | // Invoke vendor-specific tool to get the version string, e.g. |
| 186 | // psutils --get-version |
| 187 | // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0 |
| 188 | auto [rc, output] = internal::exec(PSU_VERSION_UTIL, inventoryPath); |
| 189 | if (rc == 0) |
| 190 | { |
| 191 | version = output; |
| 192 | } |
| 193 | } |
| 194 | catch (const std::exception& e) |
| 195 | { |
| 196 | lg2::error("Unable to get firmware version for PSU {PSU}: {ERROR}", |
| 197 | "PSU", inventoryPath, "ERROR", e); |
| 198 | } |
| 199 | return version; |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 200 | } |
| 201 | |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 202 | std::string Utils::getModel(const std::string& inventoryPath) const |
| 203 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 204 | std::string model; |
| 205 | try |
| 206 | { |
| 207 | // Invoke vendor-specific tool to get the model string, e.g. |
| 208 | // psutils --get-model |
| 209 | // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0 |
| 210 | auto [rc, output] = internal::exec(PSU_MODEL_UTIL, inventoryPath); |
| 211 | if (rc == 0) |
| 212 | { |
| 213 | model = output; |
| 214 | } |
| 215 | } |
| 216 | catch (const std::exception& e) |
| 217 | { |
| 218 | lg2::error("Unable to get model for PSU {PSU}: {ERROR}", "PSU", |
| 219 | inventoryPath, "ERROR", e); |
| 220 | } |
| 221 | return model; |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 222 | } |
| 223 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 224 | std::string Utils::getLatestVersion(const std::set<std::string>& versions) const |
| 225 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 226 | std::string latestVersion; |
| 227 | try |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 228 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 229 | if (!versions.empty()) |
| 230 | { |
| 231 | std::stringstream args; |
| 232 | for (const auto& s : versions) |
| 233 | { |
| 234 | args << s << " "; |
| 235 | } |
| 236 | auto [rc, output] = |
| 237 | internal::exec(PSU_VERSION_COMPARE_UTIL, args.str()); |
| 238 | if (rc == 0) |
| 239 | { |
| 240 | latestVersion = output; |
| 241 | } |
| 242 | } |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 243 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 244 | catch (const std::exception& e) |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 245 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 246 | lg2::error("Unable to get latest PSU firmware version: {ERROR}", |
| 247 | "ERROR", e); |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 248 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 249 | return latestVersion; |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 250 | } |
| 251 | |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 252 | bool Utils::isAssociated(const std::string& psuInventoryPath, |
| 253 | const AssociationList& assocs) const |
| 254 | { |
| 255 | return std::find_if(assocs.begin(), assocs.end(), |
| 256 | [&psuInventoryPath](const auto& assoc) { |
Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 257 | return psuInventoryPath == std::get<2>(assoc); |
| 258 | }) != assocs.end(); |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 259 | } |
| 260 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 261 | any Utils::getPropertyImpl(sdbusplus::bus_t& bus, const char* service, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 262 | const char* path, const char* interface, |
| 263 | const char* propertyName) const |
| 264 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 265 | any anyValue{}; |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 266 | try |
| 267 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 268 | auto method = bus.new_method_call( |
| 269 | service, path, "org.freedesktop.DBus.Properties", "Get"); |
| 270 | method.append(interface, propertyName); |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 271 | auto reply = bus.call(method); |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 272 | PropertyType value{}; |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 273 | reply.read(value); |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 274 | anyValue = value; |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 275 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 276 | catch (const std::exception& e) |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 277 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 278 | throw std::runtime_error{std::format( |
| 279 | "Unable to get property {} for path {} and interface {}: {}", |
| 280 | propertyName, path, interface, e.what())}; |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 281 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 282 | return anyValue; |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 283 | } |
| 284 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 285 | } // namespace utils |