Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "config.h" |
| 17 | |
| 18 | #include "version.hpp" |
| 19 | |
| 20 | #include "pmbus.hpp" |
| 21 | #include "utility.hpp" |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 22 | #include "utils.hpp" |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 23 | |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 24 | #include <nlohmann/json.hpp> |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 25 | #include <phosphor-logging/lg2.hpp> |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 26 | |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 27 | #include <exception> |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 28 | #include <tuple> |
| 29 | |
| 30 | using json = nlohmann::json; |
| 31 | |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 32 | using namespace utils; |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 33 | using namespace phosphor::power::util; |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 34 | |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 35 | namespace version |
| 36 | { |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 37 | |
| 38 | namespace internal |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 39 | { |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 40 | |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 41 | // PsuInfo contains the device path, PMBus access type, and sysfs file name |
| 42 | using PsuVersionInfo = |
| 43 | std::tuple<std::string, phosphor::pmbus::Type, std::string>; |
| 44 | |
| 45 | /** |
| 46 | * @brief Get PSU version information |
| 47 | * |
| 48 | * @param[in] psuInventoryPath - The PSU inventory path. |
| 49 | * |
| 50 | * @return tuple - device path, PMBus access type, and sysfs file name |
| 51 | */ |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 52 | PsuVersionInfo getVersionInfo(const std::string& psuInventoryPath) |
| 53 | { |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 54 | auto data = loadJSONFromFile(PSU_JSON_PATH); |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 55 | |
| 56 | if (data == nullptr) |
| 57 | { |
| 58 | return {}; |
| 59 | } |
| 60 | |
| 61 | auto devices = data.find("psuDevices"); |
| 62 | if (devices == data.end()) |
| 63 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 64 | lg2::warning("Unable to find psuDevices"); |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 65 | return {}; |
| 66 | } |
| 67 | auto devicePath = devices->find(psuInventoryPath); |
| 68 | if (devicePath == devices->end()) |
| 69 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 70 | lg2::warning("Unable to find path for PSU PATH={PATH}", "PATH", |
| 71 | psuInventoryPath); |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 72 | return {}; |
| 73 | } |
| 74 | |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 75 | auto type = getPMBusAccessType(data); |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 76 | |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 77 | std::string fileName; |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 78 | for (const auto& fru : data["fruConfigs"]) |
| 79 | { |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 80 | if (fru.contains("propertyName") && |
| 81 | (fru["propertyName"] == "Version") && fru.contains("fileName")) |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 82 | { |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 83 | fileName = fru["fileName"]; |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | } |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 87 | if (fileName.empty()) |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 88 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 89 | lg2::warning("Unable to find Version file"); |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 90 | return {}; |
| 91 | } |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 92 | return std::make_tuple(*devicePath, type, fileName); |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 93 | } |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 94 | |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 95 | /** |
| 96 | * @brief Get the PSU version from sysfs. |
| 97 | * |
| 98 | * Obtain PSU information from the PSU JSON file. |
| 99 | * |
| 100 | * Throws an exception if an error occurs. |
| 101 | * |
| 102 | * @param[in] psuInventoryPath - PSU D-Bus inventory path |
| 103 | * |
| 104 | * @return PSU version, or "" if none found |
| 105 | */ |
| 106 | std::string getVersionJson(const std::string& psuInventoryPath) |
| 107 | { |
| 108 | // Get PSU device path, PMBus access type, and sysfs file name from JSON |
| 109 | const auto [devicePath, type, fileName] = getVersionInfo(psuInventoryPath); |
| 110 | |
| 111 | // Read version from sysfs file |
| 112 | std::string version; |
| 113 | if (!devicePath.empty() && !fileName.empty()) |
| 114 | { |
| 115 | phosphor::pmbus::PMBus pmbus(devicePath); |
| 116 | version = pmbus.readString(fileName, type); |
| 117 | } |
| 118 | return version; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @brief Get the PSU version from sysfs. |
| 123 | * |
| 124 | * Obtain PSU information from D-Bus. |
| 125 | * |
| 126 | * Throws an exception if an error occurs. |
| 127 | * |
| 128 | * @param[in] bus - D-Bus connection |
| 129 | * @param[in] psuInventoryPath - PSU D-Bus inventory path |
| 130 | * |
| 131 | * @return PSU version, or "" if none found |
| 132 | */ |
| 133 | std::string getVersionDbus(sdbusplus::bus_t& bus, |
| 134 | const std::string& psuInventoryPath) |
| 135 | { |
| 136 | // Get PSU I2C bus/address and create PMBus interface |
| 137 | const auto [i2cbus, i2caddr] = getPsuI2c(bus, psuInventoryPath); |
| 138 | auto pmbus = getPmbusIntf(i2cbus, i2caddr); |
| 139 | |
| 140 | // Read version from sysfs file |
| 141 | std::string name = "fw_version"; |
| 142 | auto type = phosphor::pmbus::Type::HwmonDeviceDebug; |
| 143 | std::string version = pmbus->readString(name, type); |
| 144 | return version; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @brief Get firmware latest version |
| 149 | * |
| 150 | * @param[in] versions - String of versions |
| 151 | * |
| 152 | * @return version - latest firmware level |
| 153 | */ |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 154 | std::string getLatestDefault(const std::vector<std::string>& versions) |
| 155 | { |
| 156 | std::string latest; |
| 157 | for (const auto& version : versions) |
| 158 | { |
| 159 | if (latest < version) |
| 160 | { |
| 161 | latest = version; |
| 162 | } |
| 163 | } |
| 164 | return latest; |
| 165 | } |
| 166 | |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 167 | } // namespace internal |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 168 | |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 169 | std::string getVersion(sdbusplus::bus_t& bus, |
| 170 | const std::string& psuInventoryPath) |
| 171 | { |
Shawn McCarney | 37c2612 | 2024-10-24 13:58:31 -0500 | [diff] [blame] | 172 | std::string version; |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 173 | try |
| 174 | { |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 175 | if (usePsuJsonFile()) |
| 176 | { |
| 177 | // Obtain PSU information from JSON file |
| 178 | version = internal::getVersionJson(psuInventoryPath); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | // Obtain PSU information from D-Bus |
| 183 | version = internal::getVersionDbus(bus, psuInventoryPath); |
| 184 | } |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 185 | } |
| 186 | catch (const std::exception& e) |
| 187 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 188 | lg2::error("Error in getVersion: {ERROR}", "ERROR", e); |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 189 | } |
Shawn McCarney | 37c2612 | 2024-10-24 13:58:31 -0500 | [diff] [blame] | 190 | return version; |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 191 | } |
| 192 | |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 193 | std::string getLatest(const std::vector<std::string>& versions) |
| 194 | { |
| 195 | // TODO: when multiple PSU/Machines are supported, add configuration options |
| 196 | // to implement machine-specific logic. |
| 197 | // For now IBM AC Servers and Inspur FP5280G2 are supported. |
| 198 | // |
| 199 | // IBM AC servers' PSU version has two types: |
| 200 | // * XXXXYYYYZZZZ: XXXX is the primary version |
| 201 | // YYYY is the secondary version |
| 202 | // ZZZZ is the communication version |
| 203 | // |
| 204 | // * XXXXYYYY: XXXX is the primary version |
| 205 | // YYYY is the seconday version |
| 206 | // |
| 207 | // Inspur FP5280G2 PSU version is human readable text and a larger string |
| 208 | // means a newer version. |
| 209 | // |
| 210 | // So just compare by strings is OK for these cases |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 211 | return internal::getLatestDefault(versions); |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 212 | } |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 213 | |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 214 | } // namespace version |