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