1 /**
2 * Copyright © 2024 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 "utils.hpp"
19
20 #include "utility.hpp"
21
22 #include <phosphor-logging/lg2.hpp>
23 #include <xyz/openbmc_project/Common/Device/error.hpp>
24
25 #include <cassert>
26 #include <exception>
27 #include <filesystem>
28 #include <iomanip>
29 #include <ios>
30 #include <iostream>
31 #include <regex>
32 #include <sstream>
33 #include <stdexcept>
34
35 using namespace phosphor::power::util;
36 using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
37 namespace fs = std::filesystem;
38
39 namespace utils
40 {
41
42 constexpr auto IBMCFFPSInterface =
43 "xyz.openbmc_project.Configuration.IBMCFFPSConnector";
44 constexpr auto i2cBusProp = "I2CBus";
45 constexpr auto i2cAddressProp = "I2CAddress";
46
getPsuI2c(sdbusplus::bus_t & bus,const std::string & psuInventoryPath)47 PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus, const std::string& psuInventoryPath)
48 {
49 auto depth = 0;
50 auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth);
51 if (objects.empty())
52 {
53 throw std::runtime_error("Supported Configuration Not Found");
54 }
55
56 std::optional<std::uint64_t> i2cbus;
57 std::optional<std::uint64_t> i2caddr;
58
59 // GET a map of objects back.
60 // Each object will have a path, a service, and an interface.
61 for (const auto& [path, services] : objects)
62 {
63 auto service = services.begin()->first;
64
65 if (path.empty() || service.empty())
66 {
67 continue;
68 }
69
70 // Match the PSU identifier in the path with the passed PSU inventory
71 // path. Compare the last character of both paths to find the PSU bus
72 // and address. example: PSU path:
73 // /xyz/openbmc_project/inventory/system/board/Nisqually_Backplane/Power_Supply_Slot_0
74 // PSU inventory path:
75 // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0
76 if (path.back() == psuInventoryPath.back())
77 {
78 // Retrieve i2cBus and i2cAddress from array of properties.
79 auto properties =
80 getAllProperties(bus, path, IBMCFFPSInterface, service);
81 for (const auto& property : properties)
82 {
83 try
84 {
85 if (property.first == i2cBusProp)
86 {
87 i2cbus = std::get<uint64_t>(properties.at(i2cBusProp));
88 }
89 else if (property.first == i2cAddressProp)
90 {
91 i2caddr =
92 std::get<uint64_t>(properties.at(i2cAddressProp));
93 }
94 }
95 catch (const std::exception& e)
96 {
97 lg2::warning("Error reading property {PROPERTY}: {ERROR}",
98 "PROPERTY", property.first, "ERROR", e);
99 }
100 }
101
102 if (i2cbus.has_value() && i2caddr.has_value())
103 {
104 break;
105 }
106 }
107 }
108
109 if (!i2cbus.has_value() || !i2caddr.has_value())
110 {
111 throw std::runtime_error("Failed to get I2C bus or address");
112 }
113
114 return std::make_tuple(*i2cbus, *i2caddr);
115 }
116
getPmbusIntf(std::uint64_t i2cBus,std::uint64_t i2cAddr)117 std::unique_ptr<phosphor::pmbus::PMBusBase> getPmbusIntf(std::uint64_t i2cBus,
118 std::uint64_t i2cAddr)
119 {
120 std::stringstream ss;
121 ss << std::hex << std::setw(4) << std::setfill('0') << i2cAddr;
122 return phosphor::pmbus::createPMBus(i2cBus, ss.str());
123 }
124
readVPDValue(phosphor::pmbus::PMBusBase & pmbusIntf,const std::string & vpdName,const phosphor::pmbus::Type & type,const std::size_t & vpdSize)125 std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf,
126 const std::string& vpdName,
127 const phosphor::pmbus::Type& type,
128 const std::size_t& vpdSize)
129 {
130 std::string vpdValue;
131 const std::regex illegalVPDRegex =
132 std::regex("[^[:alnum:]]", std::regex::basic);
133
134 try
135 {
136 vpdValue = pmbusIntf.readString(vpdName, type);
137 }
138 catch (const ReadFailure& e)
139 {
140 // Ignore the read failure, let pmbus code indicate failure.
141 }
142
143 if (vpdValue.size() != vpdSize)
144 {
145 lg2::info(" {VPDNAME} resize needed. size: {SIZE}", "VPDNAME", vpdName,
146 "SIZE", vpdValue.size());
147 vpdValue.resize(vpdSize, ' ');
148 }
149
150 // Replace any illegal values with space(s).
151 std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
152 illegalVPDRegex, " ");
153
154 return vpdValue;
155 }
156
checkFileExists(const std::string & filePath)157 bool checkFileExists(const std::string& filePath)
158 {
159 try
160 {
161 return std::filesystem::exists(filePath);
162 }
163 catch (const std::exception& e)
164 {
165 lg2::error("Unable to check for existence of {FILEPATH}: {ERROR}",
166 "FILEPATH", filePath, "ERROR", e);
167 }
168 return false;
169 }
170
getDeviceName(std::string devPath)171 std::string getDeviceName(std::string devPath)
172 {
173 if (devPath.empty())
174 {
175 return devPath;
176 }
177 if (devPath.back() == '/')
178 {
179 devPath.pop_back();
180 }
181 return fs::path(devPath).stem().string();
182 }
183
getDevicePath(sdbusplus::bus_t & bus,const std::string & psuInventoryPath)184 std::string getDevicePath(sdbusplus::bus_t& bus,
185 const std::string& psuInventoryPath)
186 {
187 try
188 {
189 if (usePsuJsonFile())
190 {
191 auto data = loadJSONFromFile(PSU_JSON_PATH);
192 if (data == nullptr)
193 {
194 return {};
195 }
196 auto devicePath = data["psuDevices"][psuInventoryPath];
197 if (devicePath.empty())
198 {
199 lg2::warning("Unable to find psu devices or path");
200 }
201 return devicePath;
202 }
203 else
204 {
205 const auto [i2cbus, i2caddr] = getPsuI2c(bus, psuInventoryPath);
206 const auto DevicePath = "/sys/bus/i2c/devices/";
207 std::ostringstream ss;
208 ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
209 std::string addrStr = ss.str();
210 std::string busStr = std::to_string(i2cbus);
211 std::string devPath = DevicePath + busStr + "-" + addrStr;
212 return devPath;
213 }
214 }
215 catch (const std::exception& e)
216 {
217 lg2::error("Error in getDevicePath: {ERROR}", "ERROR", e);
218 return {};
219 }
220 catch (...)
221 {
222 lg2::error("Unknown error occurred in getDevicePath");
223 return {};
224 }
225 }
226
parseDeviceName(const std::string & devName)227 std::pair<uint8_t, uint8_t> parseDeviceName(const std::string& devName)
228 {
229 // Get I2C bus and device address, e.g. 3-0068
230 // is parsed to bus 3, device address 0x68
231 auto pos = devName.find('-');
232 assert(pos != std::string::npos);
233 uint8_t busId = std::stoi(devName.substr(0, pos));
234 uint8_t devAddr = std::stoi(devName.substr(pos + 1), nullptr, 16);
235 return {busId, devAddr};
236 }
237
usePsuJsonFile()238 bool usePsuJsonFile()
239 {
240 return checkFileExists(PSU_JSON_PATH);
241 }
242
243 } // namespace utils
244