power-utils: Retrieve Firmware Version from sysfs
Added support to retrieve firmware version from sysfs. This required the
following code additions and modifications:
1 - Locate the target PSU object and retrieve 'i2cBus' and 'i2cAddress'.
2 - Obtain PMBus interface access.
3 - Read the firmware version from sysfs.
Tested:
The new code was loaded onto a system, and the firmware version was
successfully read from sysfs. Verified getVersion using psu.json file
did not change.
Change-Id: I791788f45e4d682578efbed33e3832a833644dad
Signed-off-by: Faisal Awada <faisal@us.ibm.com>
diff --git a/tools/power-utils/main.cpp b/tools/power-utils/main.cpp
index ed63071..eb81db4 100644
--- a/tools/power-utils/main.cpp
+++ b/tools/power-utils/main.cpp
@@ -13,13 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+#include "config.h"
+
#include "updater.hpp"
#include "version.hpp"
#include <CLI/CLI.hpp>
#include <phosphor-logging/log.hpp>
+#include <sdbusplus/bus.hpp>
#include <cassert>
+#include <filesystem>
using namespace phosphor::logging;
@@ -47,9 +51,18 @@
std::string ret;
+ bool useJsonFile = version::utils::checkFileExists(PSU_JSON_PATH);
+ auto bus = sdbusplus::bus::new_default();
if (!psuPath.empty())
{
- ret = version::getVersion(psuPath);
+ if (!useJsonFile)
+ {
+ ret = version::getVersion(bus, psuPath);
+ }
+ else
+ {
+ ret = version::getVersion(psuPath);
+ }
}
if (!versions.empty())
{
diff --git a/tools/power-utils/version.cpp b/tools/power-utils/version.cpp
index 93b9a82..1987342 100644
--- a/tools/power-utils/version.cpp
+++ b/tools/power-utils/version.cpp
@@ -21,19 +21,30 @@
#include "utility.hpp"
#include <phosphor-logging/log.hpp>
+#include <xyz/openbmc_project/Common/Device/error.hpp>
+#include <exception>
+#include <iostream>
+#include <regex>
+#include <stdexcept>
#include <tuple>
using json = nlohmann::json;
using namespace phosphor::logging;
-// PsuInfo contains the device path, pmbus read type, and the version string
-using PsuVersionInfo =
- std::tuple<std::string, phosphor::pmbus::Type, std::string>;
+using namespace phosphor::power::util;
+using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
+namespace version
+{
namespace utils
{
+constexpr auto IBMCFFPSInterface =
+ "xyz.openbmc_project.Configuration.IBMCFFPSConnector";
+constexpr auto i2cBusProp = "I2CBus";
+constexpr auto i2cAddressProp = "I2CAddress";
+
PsuVersionInfo getVersionInfo(const std::string& psuInventoryPath)
{
auto data = phosphor::power::util::loadJSONFromFile(PSU_JSON_PATH);
@@ -90,10 +101,134 @@
return latest;
}
-} // namespace utils
-
-namespace version
+PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus, const std::string& psuInventoryPath)
{
+ auto depth = 0;
+ auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth);
+ if (objects.empty())
+ {
+ throw std::runtime_error("Supported Configuration Not Found");
+ }
+
+ std::optional<std::uint64_t> i2cbus;
+ std::optional<std::uint64_t> i2caddr;
+
+ // GET a map of objects back.
+ // Each object will have a path, a service, and an interface.
+ for (const auto& [path, services] : objects)
+ {
+ auto service = services.begin()->first;
+
+ if (path.empty() || service.empty())
+ {
+ continue;
+ }
+
+ // Match the PSU identifier in the path with the passed PSU inventory
+ // path. Compare the last character of both paths to find the PSU bus
+ // and address. example: PSU path:
+ // /xyz/openbmc_project/inventory/system/board/Nisqually_Backplane/Power_Supply_Slot_0
+ // PSU inventory path:
+ // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0
+ if (path.back() == psuInventoryPath.back())
+ {
+ // Retrieve i2cBus and i2cAddress from array of properties.
+ auto properties =
+ getAllProperties(bus, path, IBMCFFPSInterface, service);
+ for (const auto& property : properties)
+ {
+ try
+ {
+ if (property.first == i2cBusProp)
+ {
+ i2cbus = std::get<uint64_t>(properties.at(i2cBusProp));
+ }
+ else if (property.first == i2cAddressProp)
+ {
+ i2caddr =
+ std::get<uint64_t>(properties.at(i2cAddressProp));
+ }
+ }
+ catch (const std::exception& e)
+ {
+ log<level::WARNING>(
+ std::format("Error reading property {}: {}",
+ property.first, e.what())
+ .c_str());
+ }
+ }
+
+ if (i2cbus.has_value() && i2caddr.has_value())
+ {
+ break;
+ }
+ }
+ }
+
+ if (!i2cbus.has_value() || !i2caddr.has_value())
+ {
+ throw std::runtime_error("Failed to get I2C bus or address");
+ }
+
+ return std::make_tuple(*i2cbus, *i2caddr);
+}
+
+std::unique_ptr<phosphor::pmbus::PMBusBase>
+ getPmbusIntf(std::uint64_t i2cBus, std::uint64_t i2cAddr)
+{
+ std::stringstream ss;
+ ss << std::hex << std::setw(4) << std::setfill('0') << i2cAddr;
+ return phosphor::pmbus::createPMBus(i2cBus, ss.str());
+}
+
+std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf,
+ const std::string& vpdName,
+ const phosphor::pmbus::Type& type,
+ const std::size_t& vpdSize)
+{
+ std::string vpdValue;
+ const std::regex illegalVPDRegex =
+ std::regex("[^[:alnum:]]", std::regex::basic);
+
+ try
+ {
+ vpdValue = pmbusIntf.readString(vpdName, type);
+ }
+ catch (const ReadFailure& e)
+ {
+ // Ignore the read failure, let pmbus code indicate failure.
+ }
+
+ if (vpdValue.size() != vpdSize)
+ {
+ log<level::INFO>(
+ std::format(" {} resize needed. size: {}", vpdName, vpdValue.size())
+ .c_str());
+ vpdValue.resize(vpdSize, ' ');
+ }
+
+ // Replace any illegal values with space(s).
+ std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
+ illegalVPDRegex, " ");
+
+ return vpdValue;
+}
+
+bool checkFileExists(const std::string& filePath)
+{
+ try
+ {
+ return std::filesystem::exists(filePath);
+ }
+ catch (const std::exception& e)
+ {
+ log<level::ERR>(std::format("Unable to check for existence of {}: {}",
+ filePath, e.what())
+ .c_str());
+ }
+ return false;
+}
+} // namespace utils
std::string getVersion(const std::string& psuInventoryPath)
{
@@ -101,7 +236,7 @@
utils::getVersionInfo(psuInventoryPath);
if (devicePath.empty() || versionStr.empty())
{
- return {};
+ return "";
}
std::string version;
try
@@ -116,6 +251,36 @@
return version;
}
+std::string getVersion(sdbusplus::bus_t& bus,
+ const std::string& psuInventoryPath)
+{
+ try
+ {
+ constexpr auto FW_VERSION = "fw_version";
+ using namespace phosphor::pmbus;
+ const auto IBMCFFPS_FW_VERSION_SIZE = 12;
+ const auto& [i2cbus, i2caddr] = utils::getPsuI2c(bus, psuInventoryPath);
+
+ auto pmbusIntf = utils::getPmbusIntf(i2cbus, i2caddr);
+
+ if (!pmbusIntf)
+ {
+ log<level::WARNING>("Unable to get pointer PMBus Interface");
+ return "";
+ }
+
+ std::string fwVersion =
+ utils::readVPDValue(*pmbusIntf, FW_VERSION, Type::HwmonDeviceDebug,
+ IBMCFFPS_FW_VERSION_SIZE);
+ return fwVersion;
+ }
+ catch (const std::exception& e)
+ {
+ log<level::ERR>(std::format("Error: {}", e.what()).c_str());
+ return "";
+ }
+}
+
std::string getLatest(const std::vector<std::string>& versions)
{
// TODO: when multiple PSU/Machines are supported, add configuration options
@@ -136,5 +301,4 @@
// So just compare by strings is OK for these cases
return utils::getLatestDefault(versions);
}
-
} // namespace version
diff --git a/tools/power-utils/version.hpp b/tools/power-utils/version.hpp
index c10071a..534d113 100644
--- a/tools/power-utils/version.hpp
+++ b/tools/power-utils/version.hpp
@@ -15,14 +15,109 @@
*/
#pragma once
+#include "pmbus.hpp"
+
+#include <sdbusplus/bus.hpp>
+
+#include <memory>
#include <string>
+#include <tuple>
#include <vector>
namespace version
{
+namespace utils
+{
+// PsuInfo contains the device path, pmbus read type, and the version string
+using PsuVersionInfo =
+ std::tuple<std::string, phosphor::pmbus::Type, std::string>;
+
+// PsuI2cInfo contains the device i2c bus and i2c address
+using PsuI2cInfo = std::tuple<std::uint64_t, std::uint64_t>;
/**
- * Get the software version of the PSU
+ * @brief Get PSU version information
+ *
+ * @param[in] psuInventoryPath - The PSU inventory path.
+ *
+ * @return tuple - device path, pmbus read type and PSU version
+ */
+PsuVersionInfo getVersionInfo(const std::string& psuInventoryPath);
+
+/**
+ * @brief Get firmware latest version
+ *
+ * @param[in] versions - String of versions
+ *
+ * @return version - latest firmware level
+ */
+std::string getLatestDefault(const std::vector<std::string>& versions);
+
+/**
+ * @brief Get i2c bus and address
+ *
+ * @param[in] bus - Systemd bus connection
+ * @param[in] psuInventoryPath - The PSU inventory path.
+ *
+ * @return tuple - i2cBus and i2cAddr.
+ */
+PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus,
+ const std::string& psuInventoryPath);
+
+/**
+ * @brief Get PMBus interface pointer
+ *
+ * @param[in] i2cBus - PSU i2c bus
+ * @param[in] i2cAddr - PSU i2c address
+ *
+ * @return Pointer to PSU PMBus interface
+ */
+std::unique_ptr<phosphor::pmbus::PMBusBase>
+ getPmbusIntf(std::uint64_t i2cBus, std::uint64_t i2cAddr);
+
+/**
+ * @brief Reads a VPD value from PMBus, corrects size, and contents.
+ *
+ * If the VPD data read is not the passed in size, resize and fill with
+ * spaces. If the data contains a non-alphanumeric value, replace any of
+ * those values with spaces.
+ *
+ * @param[in] pmbusIntf - PMBus Interface.
+ * @param[in] vpdName - The name of the sysfs "file" to read data from.
+ * @param[in] type - The HWMON file type to read from.
+ * @param[in] vpdSize - The expected size of the data for this VPD/property
+ *
+ * @return A string containing the VPD data read, resized if necessary
+ */
+std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf,
+ const std::string& vpdName,
+ const phosphor::pmbus::Type& type,
+ const std::size_t& vpdSize);
+
+/**
+ * @brief Check for file existence
+ *
+ * @param[in] filePath - File path
+ *
+ * @return bool
+ */
+bool checkFileExists(const std::string& filePath);
+
+} // namespace utils
+
+/**
+ * Get the software version of the PSU using sysfs
+ *
+ * @param[in] bus - Systemd bus connection
+ * @param[in] psuInventoryPath - The inventory path of the PSU
+ *
+ * @return The version of the PSU
+ */
+std::string getVersion(sdbusplus::bus_t& bus,
+ const std::string& psuInventoryPath);
+
+/**
+ * Get the software version of the PSU using psu.json
*
* @param[in] psuInventoryPath - The inventory path of the PSU
*