Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 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 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 16 | #include "pmbus.hpp" |
| 17 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 18 | #include <phosphor-logging/elog-errors.hpp> |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 19 | #include <phosphor-logging/elog.hpp> |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 20 | #include <phosphor-logging/lg2.hpp> |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 21 | #include <xyz/openbmc_project/Common/Device/error.hpp> |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 22 | #include <xyz/openbmc_project/Common/error.hpp> |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 23 | |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 24 | #include <filesystem> |
| 25 | #include <fstream> |
| 26 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 27 | namespace phosphor |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 28 | { |
| 29 | namespace pmbus |
| 30 | { |
| 31 | |
| 32 | using namespace phosphor::logging; |
| 33 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 34 | using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error; |
Brandon Wyman | 9c7897c | 2019-03-28 17:42:34 -0500 | [diff] [blame] | 35 | namespace fs = std::filesystem; |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 36 | |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 37 | /** |
| 38 | * @brief Helper to close a file handle |
| 39 | */ |
| 40 | struct FileCloser |
| 41 | { |
| 42 | void operator()(FILE* fp) const |
| 43 | { |
| 44 | fclose(fp); |
| 45 | } |
| 46 | }; |
| 47 | |
Matt Spinler | af7321e | 2025-09-16 13:01:21 -0500 | [diff] [blame] | 48 | /** |
| 49 | * @brief Returns the canonical path if it exists |
| 50 | * otherwise returns the path passed in. |
| 51 | * |
| 52 | * @param[in] path - The path to check |
| 53 | * |
| 54 | * @return fs::path - Either the canonical or original path |
| 55 | */ |
| 56 | static fs::path tryCanonical(const fs::path& path) |
| 57 | { |
| 58 | std::error_code ec; |
| 59 | auto canonical = fs::canonical(path, ec); |
| 60 | |
| 61 | if (ec) |
| 62 | { |
| 63 | lg2::error("Could not get canonical path from {PATH}", "PATH", path); |
| 64 | return path; |
| 65 | } |
| 66 | |
| 67 | return canonical; |
| 68 | } |
| 69 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 70 | std::string PMBus::insertPageNum(const std::string& templateName, size_t page) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 71 | { |
| 72 | auto name = templateName; |
| 73 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 74 | // insert the page where the P was |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 75 | auto pos = name.find('P'); |
| 76 | if (pos != std::string::npos) |
| 77 | { |
| 78 | name.replace(pos, 1, std::to_string(page)); |
| 79 | } |
| 80 | |
| 81 | return name; |
| 82 | } |
| 83 | |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 84 | fs::path PMBus::getPath(Type type) |
| 85 | { |
| 86 | switch (type) |
| 87 | { |
| 88 | default: |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 89 | /* fall through */ |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 90 | case Type::Base: |
| 91 | return basePath; |
| 92 | break; |
| 93 | case Type::Hwmon: |
| 94 | return basePath / "hwmon" / hwmonDir; |
| 95 | break; |
| 96 | case Type::Debug: |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 97 | return debugPath / "pmbus" / hwmonDir; |
| 98 | break; |
| 99 | case Type::DeviceDebug: |
Matt Spinler | 4dc4678 | 2018-01-04 14:29:16 -0600 | [diff] [blame] | 100 | { |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 101 | auto dir = driverName + "." + std::to_string(instance); |
| 102 | return debugPath / dir; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 103 | break; |
Matt Spinler | 4dc4678 | 2018-01-04 14:29:16 -0600 | [diff] [blame] | 104 | } |
| 105 | case Type::HwmonDeviceDebug: |
| 106 | return debugPath / "pmbus" / hwmonDir / getDeviceName(); |
| 107 | break; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Matt Spinler | ba05348 | 2018-01-04 14:26:05 -0600 | [diff] [blame] | 111 | std::string PMBus::getDeviceName() |
| 112 | { |
| 113 | std::string name; |
| 114 | std::ifstream file; |
| 115 | auto path = basePath / "name"; |
| 116 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 117 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Matt Spinler | ba05348 | 2018-01-04 14:26:05 -0600 | [diff] [blame] | 118 | std::ifstream::eofbit); |
| 119 | try |
| 120 | { |
| 121 | file.open(path); |
| 122 | file >> name; |
| 123 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 124 | catch (const std::exception& e) |
Matt Spinler | ba05348 | 2018-01-04 14:26:05 -0600 | [diff] [blame] | 125 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 126 | lg2::error("Unable to read PMBus device name PATH={PATH}", "PATH", |
| 127 | path); |
Matt Spinler | ba05348 | 2018-01-04 14:26:05 -0600 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return name; |
| 131 | } |
| 132 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 133 | bool PMBus::readBitInPage(const std::string& name, size_t page, Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 134 | { |
| 135 | auto pagedBit = insertPageNum(name, page); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 136 | return readBit(pagedBit, type); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 139 | bool PMBus::readBit(const std::string& name, Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 140 | { |
| 141 | unsigned long int value = 0; |
| 142 | std::ifstream file; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 143 | fs::path path = getPath(type); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 144 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 145 | path /= name; |
| 146 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 147 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 148 | std::ifstream::eofbit); |
| 149 | |
| 150 | try |
| 151 | { |
Jayanth Othayoth | 757ad6a | 2024-12-15 21:45:03 -0600 | [diff] [blame] | 152 | char* err = nullptr; |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 153 | std::string val(1, '\0'); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 154 | |
| 155 | file.open(path); |
| 156 | file.read(&val[0], 1); |
| 157 | |
| 158 | value = strtoul(val.c_str(), &err, 10); |
| 159 | |
| 160 | if (*err) |
| 161 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 162 | lg2::error( |
| 163 | "Invalid character in sysfs file FILE={FILE} CONTENTS={CONTENTS}", |
| 164 | "FILE", path, "CONTENTS", val); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 165 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 166 | // Catch below and handle as a read failure |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 167 | elog<InternalFailure>(); |
| 168 | } |
| 169 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 170 | catch (const std::exception& e) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 171 | { |
| 172 | auto rc = errno; |
| 173 | |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 174 | lg2::error( |
| 175 | "Failed to read sysfs file errno={ERRNO} FILENAME={FILENAME}", |
| 176 | "ERRNO", rc, "FILENAME", path); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 177 | |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 178 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
| 179 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 180 | elog<ReadFailure>( |
| 181 | metadata::CALLOUT_ERRNO(rc), |
Matt Spinler | af7321e | 2025-09-16 13:01:21 -0500 | [diff] [blame] | 182 | metadata::CALLOUT_DEVICE_PATH(tryCanonical(basePath).c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | return value != 0; |
| 186 | } |
| 187 | |
Brandon Wyman | 3b7b38b | 2017-09-25 16:43:45 -0500 | [diff] [blame] | 188 | bool PMBus::exists(const std::string& name, Type type) |
| 189 | { |
| 190 | auto path = getPath(type); |
| 191 | path /= name; |
| 192 | return fs::exists(path); |
| 193 | } |
| 194 | |
Brandon Wyman | 32453e9 | 2021-12-15 19:00:14 +0000 | [diff] [blame] | 195 | uint64_t PMBus::read(const std::string& name, Type type, bool errTrace) |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 196 | { |
| 197 | uint64_t data = 0; |
| 198 | std::ifstream file; |
| 199 | auto path = getPath(type); |
| 200 | path /= name; |
| 201 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 202 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 203 | std::ifstream::eofbit); |
| 204 | |
| 205 | try |
| 206 | { |
| 207 | file.open(path); |
| 208 | file >> std::hex >> data; |
| 209 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 210 | catch (const std::exception& e) |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 211 | { |
| 212 | auto rc = errno; |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 213 | |
Brandon Wyman | 32453e9 | 2021-12-15 19:00:14 +0000 | [diff] [blame] | 214 | if (errTrace) |
| 215 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 216 | lg2::error( |
| 217 | "Failed to read sysfs file errno={ERRNO} FILENAME={FILENAME}", |
| 218 | "ERRNO", rc, "FILENAME", path); |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 219 | |
Brandon Wyman | 32453e9 | 2021-12-15 19:00:14 +0000 | [diff] [blame] | 220 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
| 221 | |
| 222 | elog<ReadFailure>( |
| 223 | metadata::CALLOUT_ERRNO(rc), |
Matt Spinler | af7321e | 2025-09-16 13:01:21 -0500 | [diff] [blame] | 224 | metadata::CALLOUT_DEVICE_PATH(tryCanonical(basePath).c_str())); |
Brandon Wyman | 32453e9 | 2021-12-15 19:00:14 +0000 | [diff] [blame] | 225 | } |
| 226 | else |
| 227 | { |
| 228 | throw ReadFailure(); |
| 229 | } |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | return data; |
| 233 | } |
| 234 | |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 235 | std::string PMBus::readString(const std::string& name, Type type) |
| 236 | { |
| 237 | std::string data; |
| 238 | std::ifstream file; |
| 239 | auto path = getPath(type); |
| 240 | path /= name; |
| 241 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 242 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 243 | std::ifstream::eofbit); |
| 244 | |
| 245 | try |
| 246 | { |
| 247 | file.open(path); |
| 248 | file >> data; |
| 249 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 250 | catch (const std::exception& e) |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 251 | { |
| 252 | auto rc = errno; |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 253 | lg2::error( |
| 254 | "Failed to read sysfs file errno={ERRNO} FILENAME={FILENAME}", |
| 255 | "ERRNO", rc, "FILENAME", path); |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 256 | |
| 257 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
| 258 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 259 | elog<ReadFailure>( |
| 260 | metadata::CALLOUT_ERRNO(rc), |
Matt Spinler | af7321e | 2025-09-16 13:01:21 -0500 | [diff] [blame] | 261 | metadata::CALLOUT_DEVICE_PATH(tryCanonical(basePath).c_str())); |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | return data; |
| 265 | } |
| 266 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 267 | std::vector<uint8_t> PMBus::readBinary(const std::string& name, Type type, |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 268 | size_t length) |
| 269 | { |
| 270 | auto path = getPath(type) / name; |
| 271 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 272 | // Use C style IO because it's easier to handle telling the difference |
| 273 | // between hitting EOF or getting an actual error. |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 274 | std::unique_ptr<FILE, FileCloser> file{fopen(path.c_str(), "rb")}; |
| 275 | |
| 276 | if (file) |
| 277 | { |
| 278 | std::vector<uint8_t> data(length, 0); |
| 279 | |
Patrick Williams | f540219 | 2024-08-16 15:20:53 -0400 | [diff] [blame] | 280 | auto bytes = |
| 281 | fread(data.data(), sizeof(decltype(data[0])), length, file.get()); |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 282 | |
| 283 | if (bytes != length) |
| 284 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 285 | // If hit EOF, just return the amount of data that was read. |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 286 | if (feof(file.get())) |
| 287 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 288 | data.erase(data.begin() + bytes, data.end()); |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 289 | } |
| 290 | else if (ferror(file.get())) |
| 291 | { |
| 292 | auto rc = errno; |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 293 | lg2::error( |
| 294 | "Failed to read sysfs file errno={ERRNO} FILENAME={FILENAME}", |
| 295 | "ERRNO", rc, "FILENAME", path); |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 296 | using metadata = |
| 297 | xyz::openbmc_project::Common::Device::ReadFailure; |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 298 | |
| 299 | elog<ReadFailure>(metadata::CALLOUT_ERRNO(rc), |
| 300 | metadata::CALLOUT_DEVICE_PATH( |
Matt Spinler | af7321e | 2025-09-16 13:01:21 -0500 | [diff] [blame] | 301 | tryCanonical(basePath).c_str())); |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | return data; |
| 305 | } |
| 306 | |
| 307 | return std::vector<uint8_t>{}; |
| 308 | } |
| 309 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 310 | void PMBus::write(const std::string& name, int value, Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 311 | { |
| 312 | std::ofstream file; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 313 | fs::path path = getPath(type); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 314 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 315 | path /= name; |
| 316 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 317 | file.exceptions(std::ofstream::failbit | std::ofstream::badbit | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 318 | std::ofstream::eofbit); |
| 319 | |
| 320 | try |
| 321 | { |
| 322 | file.open(path); |
| 323 | file << value; |
| 324 | } |
| 325 | catch (const std::exception& e) |
| 326 | { |
| 327 | auto rc = errno; |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 328 | lg2::error( |
| 329 | "Failed to write sysfs file errno={ERRNO} FILENAME={FILENAME}", |
| 330 | "ERRNO", rc, "FILENAME", path); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 331 | |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 332 | using metadata = xyz::openbmc_project::Common::Device::WriteFailure; |
| 333 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 334 | elog<WriteFailure>( |
| 335 | metadata::CALLOUT_ERRNO(rc), |
Matt Spinler | af7321e | 2025-09-16 13:01:21 -0500 | [diff] [blame] | 336 | metadata::CALLOUT_DEVICE_PATH(tryCanonical(basePath).c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 340 | void PMBus::writeBinary(const std::string& name, std::vector<uint8_t> data, |
| 341 | Type type) |
| 342 | { |
| 343 | std::ofstream file; |
| 344 | fs::path path = getPath(type); |
| 345 | |
| 346 | path /= name; |
| 347 | |
| 348 | file.exceptions(std::ofstream::failbit | std::ofstream::badbit | |
| 349 | std::ofstream::eofbit); |
| 350 | |
| 351 | try |
| 352 | { |
| 353 | // I need to specify binary mode when I construct the ofstream |
| 354 | file.open(path, std::ios::out | std::ios_base::binary); |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 355 | lg2::debug("Write data to sysfs file FILENAME={FILENAME}", "FILENAME", |
| 356 | path); |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 357 | file.write(reinterpret_cast<const char*>(&data[0]), data.size()); |
| 358 | } |
| 359 | catch (const std::exception& e) |
| 360 | { |
| 361 | auto rc = errno; |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 362 | lg2::error( |
| 363 | "Failed to write binary data to sysfs file errno={ERRNO} FILENAME={FILENAME}", |
| 364 | "ERRNO", rc, "FILENAME", path); |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 365 | |
| 366 | using metadata = xyz::openbmc_project::Common::Device::WriteFailure; |
| 367 | |
| 368 | elog<WriteFailure>( |
| 369 | metadata::CALLOUT_ERRNO(rc), |
Matt Spinler | af7321e | 2025-09-16 13:01:21 -0500 | [diff] [blame] | 370 | metadata::CALLOUT_DEVICE_PATH(tryCanonical(basePath).c_str())); |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 374 | void PMBus::findHwmonDir() |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 375 | { |
| 376 | fs::path path{basePath}; |
| 377 | path /= "hwmon"; |
| 378 | |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 379 | // Make sure the directory exists, otherwise for things that can be |
| 380 | // dynamically present or not present an exception will be thrown if the |
| 381 | // hwmon directory is not there, resulting in a program termination. |
| 382 | if (fs::is_directory(path)) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 383 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 384 | // look for <basePath>/hwmon/hwmonN/ |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 385 | for (auto& f : fs::directory_iterator(path)) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 386 | { |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 387 | if ((f.path().filename().string().find("hwmon") != |
| 388 | std::string::npos) && |
| 389 | (fs::is_directory(f.path()))) |
| 390 | { |
| 391 | hwmonDir = f.path().filename(); |
| 392 | break; |
| 393 | } |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 397 | // Don't really want to crash here, just log it |
| 398 | // and let accesses fail later |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 399 | if (hwmonDir.empty()) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 400 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 401 | lg2::info("Unable to find hwmon directory in device base path " |
| 402 | "DEVICE_PATH={DEVICE_PATH}", |
| 403 | "DEVICE_PATH", basePath); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 404 | } |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 405 | } |
| 406 | |
Patrick Williams | 92261f8 | 2025-02-01 08:22:34 -0500 | [diff] [blame] | 407 | std::unique_ptr<PMBusBase> PMBus::createPMBus(std::uint8_t bus, |
| 408 | const std::string& address) |
Brandon Wyman | 8d19577 | 2020-01-27 15:03:51 -0600 | [diff] [blame] | 409 | { |
Patrick Williams | f540219 | 2024-08-16 15:20:53 -0400 | [diff] [blame] | 410 | const std::string physpath = { |
| 411 | "/sys/bus/i2c/devices/" + std::to_string(bus) + "-" + address}; |
Brandon Wyman | 8d19577 | 2020-01-27 15:03:51 -0600 | [diff] [blame] | 412 | auto interface = std::make_unique<PMBus>(physpath); |
| 413 | |
| 414 | return interface; |
| 415 | } |
| 416 | |
| 417 | std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus, |
| 418 | const std::string& address) |
| 419 | { |
| 420 | return PMBus::createPMBus(bus, address); |
| 421 | } |
| 422 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 423 | } // namespace pmbus |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 424 | } // namespace phosphor |