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> |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 10 | #include <fstream> |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 11 | #include <sstream> |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 12 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 13 | namespace utils |
| 14 | { |
| 15 | |
| 16 | namespace // anonymous |
| 17 | { |
| 18 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 19 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 20 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 21 | } // namespace |
| 22 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 23 | namespace internal |
| 24 | { |
| 25 | template <typename... Ts> |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 26 | std::string concat_string(const Ts&... ts) |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 27 | { |
| 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 |
| 35 | template <typename... Ts> |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 36 | std::pair<int, std::string> exec(const Ts&... ts) |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 37 | { |
| 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 YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 56 | const UtilsInterface& getUtils() |
| 57 | { |
| 58 | static Utils utils; |
| 59 | return utils; |
| 60 | } |
| 61 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 62 | std::vector<std::string> Utils::getPSUInventoryPath(sdbusplus::bus_t& bus) const |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 63 | { |
| 64 | std::vector<std::string> paths; |
Matt Spinler | e183edc | 2024-07-09 11:25:34 -0500 | [diff] [blame] | 65 | try |
| 66 | { |
| 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); |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 73 | |
Matt Spinler | e183edc | 2024-07-09 11:25:34 -0500 | [diff] [blame] | 74 | reply.read(paths); |
| 75 | } |
| 76 | catch (const sdbusplus::exception_t&) |
| 77 | { |
| 78 | // Inventory base path not there yet. |
| 79 | } |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 80 | return paths; |
| 81 | } |
| 82 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 83 | std::string Utils::getService(sdbusplus::bus_t& bus, const char* path, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 84 | const char* interface) const |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 85 | { |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 86 | auto services = getServices(bus, path, interface); |
| 87 | if (services.empty()) |
| 88 | { |
| 89 | return {}; |
| 90 | } |
| 91 | return services[0]; |
| 92 | } |
| 93 | |
Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 94 | std::vector<std::string> Utils::getServices( |
| 95 | sdbusplus::bus_t& bus, const char* path, const char* interface) const |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 96 | { |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 97 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 98 | MAPPER_INTERFACE, "GetObject"); |
| 99 | |
| 100 | mapper.append(path, std::vector<std::string>({interface})); |
| 101 | try |
| 102 | { |
| 103 | auto mapperResponseMsg = bus.call(mapper); |
| 104 | |
| 105 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 106 | mapperResponse; |
| 107 | mapperResponseMsg.read(mapperResponse); |
| 108 | if (mapperResponse.empty()) |
| 109 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 110 | lg2::error("Error reading mapper response"); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 111 | throw std::runtime_error("Error reading mapper response"); |
| 112 | } |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 113 | std::vector<std::string> ret; |
George Liu | af025bc | 2024-08-23 14:54:42 +0800 | [diff] [blame] | 114 | ret.reserve(mapperResponse.size()); |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 115 | for (const auto& i : mapperResponse) |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 116 | { |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 117 | ret.emplace_back(i.first); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 118 | } |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 119 | return ret; |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 120 | } |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 121 | catch (const sdbusplus::exception_t& ex) |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 122 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 123 | lg2::error("GetObject call failed: path={PATH}, interface={INTERFACE}", |
| 124 | "PATH", path, "INTERFACE", interface); |
Lei YU | 2d15625 | 2019-10-18 10:02:20 +0800 | [diff] [blame] | 125 | throw std::runtime_error("GetObject call failed"); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 129 | std::string Utils::getVersionId(const std::string& version) const |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 130 | { |
| 131 | if (version.empty()) |
| 132 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 133 | lg2::error("Error version is empty"); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 134 | return {}; |
| 135 | } |
| 136 | |
George Liu | 0b7c7b3 | 2022-02-07 16:30:23 +0800 | [diff] [blame] | 137 | using EVP_MD_CTX_Ptr = |
| 138 | std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>; |
| 139 | |
| 140 | std::array<unsigned char, EVP_MAX_MD_SIZE> digest{}; |
| 141 | EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), &::EVP_MD_CTX_free); |
| 142 | |
| 143 | EVP_DigestInit(ctx.get(), EVP_sha512()); |
| 144 | EVP_DigestUpdate(ctx.get(), version.c_str(), strlen(version.c_str())); |
| 145 | EVP_DigestFinal(ctx.get(), digest.data(), nullptr); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 146 | |
| 147 | // Only need 8 hex digits. |
George Liu | 0b7c7b3 | 2022-02-07 16:30:23 +0800 | [diff] [blame] | 148 | char mdString[9]; |
| 149 | snprintf(mdString, sizeof(mdString), "%02x%02x%02x%02x", |
| 150 | (unsigned int)digest[0], (unsigned int)digest[1], |
| 151 | (unsigned int)digest[2], (unsigned int)digest[3]); |
| 152 | |
| 153 | return mdString; |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 154 | } |
| 155 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 156 | std::string Utils::getVersion(const std::string& inventoryPath) const |
| 157 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 158 | // Invoke vendor-specific tool to get the version string, e.g. |
| 159 | // psutils --get-version |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 160 | // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0 |
| 161 | auto [rc, r] = internal::exec(PSU_VERSION_UTIL, inventoryPath); |
| 162 | return (rc == 0) ? r : ""; |
| 163 | } |
| 164 | |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 165 | std::string Utils::getModel(const std::string& inventoryPath) const |
| 166 | { |
| 167 | // Invoke vendor-specific tool to get the model string, e.g. |
| 168 | // psutils --get-model |
| 169 | // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0 |
| 170 | auto [rc, r] = internal::exec(PSU_MODEL_UTIL, inventoryPath); |
| 171 | return (rc == 0) ? r : ""; |
| 172 | } |
| 173 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 174 | std::string Utils::getLatestVersion(const std::set<std::string>& versions) const |
| 175 | { |
| 176 | if (versions.empty()) |
| 177 | { |
| 178 | return {}; |
| 179 | } |
| 180 | std::stringstream args; |
| 181 | for (const auto& s : versions) |
| 182 | { |
| 183 | args << s << " "; |
| 184 | } |
| 185 | auto [rc, r] = internal::exec(PSU_VERSION_COMPARE_UTIL, args.str()); |
| 186 | return (rc == 0) ? r : ""; |
| 187 | } |
| 188 | |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 189 | bool Utils::isAssociated(const std::string& psuInventoryPath, |
| 190 | const AssociationList& assocs) const |
| 191 | { |
| 192 | return std::find_if(assocs.begin(), assocs.end(), |
| 193 | [&psuInventoryPath](const auto& assoc) { |
Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 194 | return psuInventoryPath == std::get<2>(assoc); |
| 195 | }) != assocs.end(); |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 196 | } |
| 197 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 198 | any Utils::getPropertyImpl(sdbusplus::bus_t& bus, const char* service, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 199 | const char* path, const char* interface, |
| 200 | const char* propertyName) const |
| 201 | { |
| 202 | auto method = bus.new_method_call(service, path, |
| 203 | "org.freedesktop.DBus.Properties", "Get"); |
| 204 | method.append(interface, propertyName); |
| 205 | try |
| 206 | { |
| 207 | PropertyType value{}; |
| 208 | auto reply = bus.call(method); |
| 209 | reply.read(value); |
| 210 | return any(value); |
| 211 | } |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 212 | catch (const sdbusplus::exception_t& ex) |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 213 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 214 | lg2::error( |
| 215 | "GetProperty call failed: path={PATH}, interface={INTERFACE}, " |
| 216 | "property={PROPERTY}", |
| 217 | "PATH", path, "INTERFACE", interface, "PROPERTY", propertyName); |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 218 | throw std::runtime_error("GetProperty call failed"); |
| 219 | } |
| 220 | } |
| 221 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 222 | } // namespace utils |