| 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 | |
| Patrick Williams | 638b84a | 2025-02-01 08:22:34 -0500 | [diff] [blame] | 88 | std::vector<std::string> Utils::getPSUInventoryPaths( |
| 89 | 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 | |
| Patrick Williams | eab6742 | 2025-11-05 00:19:49 -0500 | [diff] [blame^] | 135 | auto mapperResponse = mapperResponseMsg.unpack< |
| 136 | std::vector<std::pair<std::string, std::vector<std::string>>>>(); |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 137 | services.reserve(mapperResponse.size()); |
| Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 138 | for (const auto& i : mapperResponse) |
| Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 139 | { |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 140 | services.emplace_back(i.first); |
| Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 141 | } |
| Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 142 | } |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 143 | catch (const std::exception& e) |
| Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 144 | { |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 145 | throw std::runtime_error{ |
| 146 | std::format("Unable to find services for path {}, interface {}: {}", |
| 147 | path, interface, e.what())}; |
| Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 148 | } |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 149 | return services; |
| Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 150 | } |
| 151 | |
| Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 152 | std::string Utils::getVersionId(const std::string& version) const |
| Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 153 | { |
| 154 | if (version.empty()) |
| 155 | { |
| Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 156 | lg2::error("Error version is empty"); |
| Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 157 | return {}; |
| 158 | } |
| 159 | |
| George Liu | 0b7c7b3 | 2022-02-07 16:30:23 +0800 | [diff] [blame] | 160 | using EVP_MD_CTX_Ptr = |
| 161 | std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>; |
| 162 | |
| 163 | std::array<unsigned char, EVP_MAX_MD_SIZE> digest{}; |
| 164 | EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), &::EVP_MD_CTX_free); |
| 165 | |
| 166 | EVP_DigestInit(ctx.get(), EVP_sha512()); |
| 167 | EVP_DigestUpdate(ctx.get(), version.c_str(), strlen(version.c_str())); |
| 168 | EVP_DigestFinal(ctx.get(), digest.data(), nullptr); |
| Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 169 | |
| 170 | // Only need 8 hex digits. |
| George Liu | 0b7c7b3 | 2022-02-07 16:30:23 +0800 | [diff] [blame] | 171 | char mdString[9]; |
| 172 | snprintf(mdString, sizeof(mdString), "%02x%02x%02x%02x", |
| 173 | (unsigned int)digest[0], (unsigned int)digest[1], |
| 174 | (unsigned int)digest[2], (unsigned int)digest[3]); |
| 175 | |
| 176 | return mdString; |
| Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 177 | } |
| 178 | |
| Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 179 | std::string Utils::getVersion(const std::string& inventoryPath) const |
| 180 | { |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 181 | std::string version; |
| 182 | try |
| 183 | { |
| 184 | // Invoke vendor-specific tool to get the version string, e.g. |
| 185 | // psutils --get-version |
| 186 | // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0 |
| 187 | auto [rc, output] = internal::exec(PSU_VERSION_UTIL, inventoryPath); |
| 188 | if (rc == 0) |
| 189 | { |
| 190 | version = output; |
| 191 | } |
| 192 | } |
| 193 | catch (const std::exception& e) |
| 194 | { |
| 195 | lg2::error("Unable to get firmware version for PSU {PSU}: {ERROR}", |
| 196 | "PSU", inventoryPath, "ERROR", e); |
| 197 | } |
| 198 | return version; |
| Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 199 | } |
| 200 | |
| Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 201 | std::string Utils::getModel(const std::string& inventoryPath) const |
| 202 | { |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 203 | std::string model; |
| 204 | try |
| 205 | { |
| 206 | // Invoke vendor-specific tool to get the model string, e.g. |
| 207 | // psutils --get-model |
| 208 | // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0 |
| 209 | auto [rc, output] = internal::exec(PSU_MODEL_UTIL, inventoryPath); |
| 210 | if (rc == 0) |
| 211 | { |
| 212 | model = output; |
| 213 | } |
| 214 | } |
| 215 | catch (const std::exception& e) |
| 216 | { |
| 217 | lg2::error("Unable to get model for PSU {PSU}: {ERROR}", "PSU", |
| 218 | inventoryPath, "ERROR", e); |
| 219 | } |
| 220 | return model; |
| Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 221 | } |
| 222 | |
| Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 223 | std::string Utils::getLatestVersion(const std::set<std::string>& versions) const |
| 224 | { |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 225 | std::string latestVersion; |
| 226 | try |
| Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 227 | { |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 228 | if (!versions.empty()) |
| 229 | { |
| 230 | std::stringstream args; |
| 231 | for (const auto& s : versions) |
| 232 | { |
| 233 | args << s << " "; |
| 234 | } |
| 235 | auto [rc, output] = |
| 236 | internal::exec(PSU_VERSION_COMPARE_UTIL, args.str()); |
| 237 | if (rc == 0) |
| 238 | { |
| 239 | latestVersion = output; |
| 240 | } |
| 241 | } |
| Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 242 | } |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 243 | catch (const std::exception& e) |
| Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 244 | { |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 245 | lg2::error("Unable to get latest PSU firmware version: {ERROR}", |
| 246 | "ERROR", e); |
| Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 247 | } |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 248 | return latestVersion; |
| Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 249 | } |
| 250 | |
| Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 251 | bool Utils::isAssociated(const std::string& psuInventoryPath, |
| 252 | const AssociationList& assocs) const |
| 253 | { |
| 254 | return std::find_if(assocs.begin(), assocs.end(), |
| 255 | [&psuInventoryPath](const auto& assoc) { |
| Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 256 | return psuInventoryPath == std::get<2>(assoc); |
| 257 | }) != assocs.end(); |
| Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 258 | } |
| 259 | |
| Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 260 | any Utils::getPropertyImpl(sdbusplus::bus_t& bus, const char* service, |
| Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 261 | const char* path, const char* interface, |
| 262 | const char* propertyName) const |
| 263 | { |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 264 | any anyValue{}; |
| Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 265 | try |
| 266 | { |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 267 | auto method = bus.new_method_call( |
| 268 | service, path, "org.freedesktop.DBus.Properties", "Get"); |
| 269 | method.append(interface, propertyName); |
| Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 270 | auto reply = bus.call(method); |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 271 | PropertyType value{}; |
| Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 272 | reply.read(value); |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 273 | anyValue = value; |
| Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 274 | } |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 275 | catch (const std::exception& e) |
| Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 276 | { |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 277 | throw std::runtime_error{std::format( |
| 278 | "Unable to get property {} for path {} and interface {}: {}", |
| 279 | propertyName, path, interface, e.what())}; |
| Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 280 | } |
| Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 281 | return anyValue; |
| Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 282 | } |
| 283 | |
| Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 284 | } // namespace utils |