James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel 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 | */ |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 16 | /// \file fru_device.cpp |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 17 | |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 18 | #include "fru_utils.hpp" |
| 19 | #include "utils.hpp" |
Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 20 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 22 | #include <sys/inotify.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 25 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 34c3e01 | 2023-03-06 13:42:00 -0800 | [diff] [blame] | 26 | #include <boost/asio/io_context.hpp> |
Ed Tanous | aa497ed | 2023-02-28 13:43:41 -0800 | [diff] [blame] | 27 | #include <boost/asio/steady_timer.hpp> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 28 | #include <boost/container/flat_map.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 29 | #include <nlohmann/json.hpp> |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 30 | #include <phosphor-logging/lg2.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 31 | #include <sdbusplus/asio/connection.hpp> |
| 32 | #include <sdbusplus/asio/object_server.hpp> |
| 33 | |
| 34 | #include <array> |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 35 | #include <cerrno> |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 36 | #include <charconv> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 37 | #include <chrono> |
| 38 | #include <ctime> |
Patrick Venture | e375400 | 2019-08-06 09:39:12 -0700 | [diff] [blame] | 39 | #include <filesystem> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 40 | #include <fstream> |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 41 | #include <functional> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 42 | #include <future> |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 43 | #include <iomanip> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 44 | #include <iostream> |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 45 | #include <limits> |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 46 | #include <map> |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 47 | #include <optional> |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 48 | #include <regex> |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 49 | #include <set> |
| 50 | #include <sstream> |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 51 | #include <string> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 52 | #include <thread> |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 53 | #include <utility> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 54 | #include <variant> |
Patrick Venture | c274f2f | 2019-10-26 09:27:23 -0700 | [diff] [blame] | 55 | #include <vector> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 56 | |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 57 | extern "C" |
| 58 | { |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 59 | #include <i2c/smbus.h> |
| 60 | #include <linux/i2c-dev.h> |
| 61 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 62 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 63 | namespace fs = std::filesystem; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 64 | constexpr size_t maxFruSize = 512; |
| 65 | constexpr size_t maxEepromPageIndex = 255; |
ankita prasad | 7329a22 | 2023-07-07 09:18:10 +0000 | [diff] [blame] | 66 | constexpr size_t busTimeoutSeconds = 10; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 67 | |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 68 | constexpr const char* blocklistPath = PACKAGE_DIR "blacklist.json"; |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 69 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 70 | const static constexpr char* baseboardFruLocation = |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 71 | "/etc/fru/baseboard.fru.bin"; |
| 72 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 73 | const static constexpr char* i2CDevLocation = "/dev"; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 74 | |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 75 | // TODO Refactor these to not be globals |
| 76 | // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 77 | static boost::container::flat_map<size_t, std::optional<std::set<size_t>>> |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 78 | busBlocklist; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 79 | struct FindDevicesWithCallback; |
| 80 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 81 | static boost::container::flat_map< |
| 82 | std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 83 | foundDevices; |
| 84 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 85 | static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses; |
AKSHAY RAVEENDRAN K | 4d4df5b | 2022-11-14 11:16:29 +0000 | [diff] [blame] | 86 | static boost::container::flat_map<size_t, std::set<size_t>> fruAddresses; |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 87 | |
Ed Tanous | 34c3e01 | 2023-03-06 13:42:00 -0800 | [diff] [blame] | 88 | boost::asio::io_context io; |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 89 | // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 90 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 91 | bool updateFRUProperty( |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 92 | const std::string& updatePropertyReq, uint32_t bus, uint32_t address, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 93 | const std::string& propertyName, |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 94 | boost::container::flat_map< |
| 95 | std::pair<size_t, size_t>, |
Kumar Thangavel | 41a9051 | 2021-11-24 15:44:20 +0530 | [diff] [blame] | 96 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
Jonathan Doman | 2418a61 | 2022-02-14 18:20:58 -0800 | [diff] [blame] | 97 | size_t& unknownBusObjectCount, const bool& powerIsOn, |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 98 | sdbusplus::asio::object_server& objServer, |
| 99 | std::shared_ptr<sdbusplus::asio::connection>& systemBus); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 100 | |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 101 | // Given a bus/address, produce the path in sysfs for an eeprom. |
| 102 | static std::string getEepromPath(size_t bus, size_t address) |
| 103 | { |
| 104 | std::stringstream output; |
| 105 | output << "/sys/bus/i2c/devices/" << bus << "-" << std::right |
| 106 | << std::setfill('0') << std::setw(4) << std::hex << address |
| 107 | << "/eeprom"; |
| 108 | return output.str(); |
| 109 | } |
| 110 | |
| 111 | static bool hasEepromFile(size_t bus, size_t address) |
| 112 | { |
| 113 | auto path = getEepromPath(bus, address); |
| 114 | try |
| 115 | { |
| 116 | return fs::exists(path); |
| 117 | } |
| 118 | catch (...) |
| 119 | { |
| 120 | return false; |
| 121 | } |
| 122 | } |
| 123 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 124 | static int64_t readFromEeprom(int fd, off_t offset, size_t len, uint8_t* buf) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 125 | { |
| 126 | auto result = lseek(fd, offset, SEEK_SET); |
| 127 | if (result < 0) |
| 128 | { |
| 129 | std::cerr << "failed to seek\n"; |
| 130 | return -1; |
| 131 | } |
| 132 | |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 133 | return read(fd, buf, len); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 136 | static int busStrToInt(const std::string_view busName) |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 137 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 138 | auto findBus = busName.rfind('-'); |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 139 | if (findBus == std::string::npos) |
| 140 | { |
| 141 | return -1; |
| 142 | } |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 143 | std::string_view num = busName.substr(findBus + 1); |
| 144 | int val = 0; |
| 145 | std::from_chars(num.data(), num.data() + num.size(), val); |
| 146 | return val; |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 147 | } |
| 148 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 149 | static int getRootBus(size_t bus) |
| 150 | { |
| 151 | auto ec = std::error_code(); |
| 152 | auto path = std::filesystem::read_symlink( |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 153 | std::filesystem::path( |
| 154 | "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"), |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 155 | ec); |
| 156 | if (ec) |
| 157 | { |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | std::string filename = path.filename(); |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 162 | auto findBus = filename.find('-'); |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 163 | if (findBus == std::string::npos) |
| 164 | { |
| 165 | return -1; |
| 166 | } |
| 167 | return std::stoi(filename.substr(0, findBus)); |
| 168 | } |
| 169 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 170 | static bool isMuxBus(size_t bus) |
| 171 | { |
Zhikui Ren | 8af5d3c | 2023-08-09 20:35:35 -0700 | [diff] [blame] | 172 | auto ec = std::error_code(); |
| 173 | auto isSymlink = |
| 174 | is_symlink(std::filesystem::path("/sys/bus/i2c/devices/i2c-" + |
| 175 | std::to_string(bus) + "/mux_device"), |
| 176 | ec); |
| 177 | return (!ec && isSymlink); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 180 | static void makeProbeInterface(size_t bus, size_t address, |
| 181 | sdbusplus::asio::object_server& objServer) |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 182 | { |
| 183 | if (isMuxBus(bus)) |
| 184 | { |
| 185 | return; // the mux buses are random, no need to publish |
| 186 | } |
| 187 | auto [it, success] = foundDevices.emplace( |
| 188 | std::make_pair(bus, address), |
| 189 | objServer.add_interface( |
| 190 | "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" + |
| 191 | std::to_string(address), |
| 192 | "xyz.openbmc_project.Inventory.Item.I2CDevice")); |
| 193 | if (!success) |
| 194 | { |
| 195 | return; // already added |
| 196 | } |
| 197 | it->second->register_property("Bus", bus); |
| 198 | it->second->register_property("Address", address); |
| 199 | it->second->initialize(); |
| 200 | } |
| 201 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 202 | static std::optional<bool> isDevice16Bit(int file) |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 203 | { |
Jae Hyun Yoo | f805baf | 2022-04-01 16:22:28 -0700 | [diff] [blame] | 204 | // Set the higher data word address bits to 0. It's safe on 8-bit addressing |
| 205 | // EEPROMs because it doesn't write any actual data. |
| 206 | int ret = i2c_smbus_write_byte(file, 0); |
| 207 | if (ret < 0) |
| 208 | { |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 209 | return std::nullopt; |
Jae Hyun Yoo | f805baf | 2022-04-01 16:22:28 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 212 | /* Get first byte */ |
| 213 | int byte1 = i2c_smbus_read_byte_data(file, 0); |
| 214 | if (byte1 < 0) |
| 215 | { |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 216 | return std::nullopt; |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 217 | } |
| 218 | /* Read 7 more bytes, it will read same first byte in case of |
| 219 | * 8 bit but it will read next byte in case of 16 bit |
| 220 | */ |
| 221 | for (int i = 0; i < 7; i++) |
| 222 | { |
| 223 | int byte2 = i2c_smbus_read_byte_data(file, 0); |
| 224 | if (byte2 < 0) |
| 225 | { |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 226 | return std::nullopt; |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 227 | } |
| 228 | if (byte2 != byte1) |
| 229 | { |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 230 | return true; |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 233 | return false; |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 236 | // Issue an I2C transaction to first write to_target_buf_len bytes,then read |
| 237 | // from_target_buf_len bytes. |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 238 | static int i2cSmbusWriteThenRead( |
| 239 | int file, uint16_t address, uint8_t* toTargetBuf, uint8_t toTargetBufLen, |
| 240 | uint8_t* fromTargetBuf, uint8_t fromTargetBufLen) |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 241 | { |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 242 | if (toTargetBuf == nullptr || toTargetBufLen == 0 || |
| 243 | fromTargetBuf == nullptr || fromTargetBufLen == 0) |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 244 | { |
| 245 | return -1; |
| 246 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 247 | |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 248 | constexpr size_t smbusWriteThenReadMsgCount = 2; |
| 249 | std::array<struct i2c_msg, smbusWriteThenReadMsgCount> msgs{}; |
| 250 | struct i2c_rdwr_ioctl_data rdwr |
| 251 | {}; |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 252 | |
| 253 | msgs[0].addr = address; |
| 254 | msgs[0].flags = 0; |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 255 | msgs[0].len = toTargetBufLen; |
| 256 | msgs[0].buf = toTargetBuf; |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 257 | msgs[1].addr = address; |
| 258 | msgs[1].flags = I2C_M_RD; |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 259 | msgs[1].len = fromTargetBufLen; |
| 260 | msgs[1].buf = fromTargetBuf; |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 261 | |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 262 | rdwr.msgs = msgs.data(); |
| 263 | rdwr.nmsgs = msgs.size(); |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 264 | |
| 265 | int ret = ioctl(file, I2C_RDWR, &rdwr); |
| 266 | |
Jason M. Bills | f6e4c1f | 2022-08-19 12:23:51 -0700 | [diff] [blame] | 267 | return (ret == static_cast<int>(msgs.size())) ? msgs[1].len : -1; |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 270 | static int64_t readData(bool is16bit, bool isBytewise, int file, |
| 271 | uint16_t address, off_t offset, size_t len, |
| 272 | uint8_t* buf) |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 273 | { |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 274 | if (!is16bit) |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 275 | { |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 276 | if (!isBytewise) |
| 277 | { |
| 278 | return i2c_smbus_read_i2c_block_data( |
| 279 | file, static_cast<uint8_t>(offset), len, buf); |
| 280 | } |
| 281 | |
| 282 | std::span<uint8_t> bufspan{buf, len}; |
| 283 | for (size_t i = 0; i < len; i++) |
| 284 | { |
| 285 | int byte = i2c_smbus_read_byte_data( |
| 286 | file, static_cast<uint8_t>(offset + i)); |
| 287 | if (byte < 0) |
| 288 | { |
| 289 | return static_cast<int64_t>(byte); |
| 290 | } |
| 291 | bufspan[i] = static_cast<uint8_t>(byte); |
| 292 | } |
| 293 | return static_cast<int64_t>(len); |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 296 | offset = htobe16(offset); |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 297 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 298 | uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset); |
| 299 | return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len); |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 302 | // TODO: This code is very similar to the non-eeprom version and can be merged |
| 303 | // with some tweaks. |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 304 | static std::vector<uint8_t> processEeprom(int bus, int address) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 305 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 306 | auto path = getEepromPath(bus, address); |
| 307 | |
| 308 | int file = open(path.c_str(), O_RDONLY); |
| 309 | if (file < 0) |
| 310 | { |
| 311 | std::cerr << "Unable to open eeprom file: " << path << "\n"; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 312 | return {}; |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 315 | std::string errorMessage = "eeprom at " + std::to_string(bus) + |
| 316 | " address " + std::to_string(address); |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 317 | auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) { |
| 318 | return readFromEeprom(file, offset, length, outbuf); |
| 319 | }; |
| 320 | FRUReader reader(std::move(readFunc)); |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 321 | std::pair<std::vector<uint8_t>, bool> pair = |
| 322 | readFRUContents(reader, errorMessage); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 323 | |
| 324 | close(file); |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 325 | return pair.first; |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 328 | std::set<size_t> findI2CEeproms(int i2cBus, |
| 329 | const std::shared_ptr<DeviceMap>& devices) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 330 | { |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 331 | std::set<size_t> foundList; |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 332 | |
| 333 | std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus); |
| 334 | |
| 335 | // For each file listed under the i2c device |
| 336 | // NOTE: This should be faster than just checking for each possible address |
| 337 | // path. |
Zhikui Ren | 8af5d3c | 2023-08-09 20:35:35 -0700 | [diff] [blame] | 338 | auto ec = std::error_code(); |
| 339 | for (const auto& p : fs::directory_iterator(path, ec)) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 340 | { |
Zhikui Ren | 8af5d3c | 2023-08-09 20:35:35 -0700 | [diff] [blame] | 341 | if (ec) |
| 342 | { |
| 343 | std::cerr << "directory_iterator err " << ec.message() << "\n"; |
| 344 | break; |
| 345 | } |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 346 | const std::string node = p.path().string(); |
| 347 | std::smatch m; |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 348 | bool found = |
| 349 | std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)")); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 350 | |
| 351 | if (!found) |
| 352 | { |
| 353 | continue; |
| 354 | } |
| 355 | if (m.size() != 2) |
| 356 | { |
| 357 | std::cerr << "regex didn't capture\n"; |
| 358 | continue; |
| 359 | } |
| 360 | |
| 361 | std::ssub_match subMatch = m[1]; |
| 362 | std::string addressString = subMatch.str(); |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 363 | std::string_view addressStringView(addressString); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 364 | |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 365 | size_t address = 0; |
| 366 | std::from_chars(addressStringView.begin(), addressStringView.end(), |
| 367 | address, 16); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 368 | |
| 369 | const std::string eeprom = node + "/eeprom"; |
| 370 | |
| 371 | try |
| 372 | { |
| 373 | if (!fs::exists(eeprom)) |
| 374 | { |
| 375 | continue; |
| 376 | } |
| 377 | } |
| 378 | catch (...) |
| 379 | { |
| 380 | continue; |
| 381 | } |
| 382 | |
| 383 | // There is an eeprom file at this address, it may have invalid |
| 384 | // contents, but we found it. |
| 385 | foundList.insert(address); |
| 386 | |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 387 | std::vector<uint8_t> device = processEeprom(i2cBus, address); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 388 | if (!device.empty()) |
| 389 | { |
| 390 | devices->emplace(address, device); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return foundList; |
| 395 | } |
| 396 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 397 | int getBusFRUs(int file, int first, int last, int bus, |
Jonathan Doman | 2418a61 | 2022-02-14 18:20:58 -0800 | [diff] [blame] | 398 | std::shared_ptr<DeviceMap> devices, const bool& powerIsOn, |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 399 | sdbusplus::asio::object_server& objServer) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 400 | { |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 401 | std::future<int> future = std::async(std::launch::async, [&]() { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 402 | // NOTE: When reading the devices raw on the bus, it can interfere with |
| 403 | // the driver's ability to operate, therefore read eeproms first before |
| 404 | // scanning for devices without drivers. Several experiments were run |
| 405 | // and it was determined that if there were any devices on the bus |
| 406 | // before the eeprom was hit and read, the eeprom driver wouldn't open |
| 407 | // while the bus device was open. An experiment was not performed to see |
| 408 | // if this issue was resolved if the i2c bus device was closed, but |
| 409 | // hexdumps of the eeprom later were successful. |
| 410 | |
| 411 | // Scan for i2c eeproms loaded on this bus. |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 412 | std::set<size_t> skipList = findI2CEeproms(bus, devices); |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 413 | std::set<size_t>& failedItems = failedAddresses[bus]; |
AKSHAY RAVEENDRAN K | 4d4df5b | 2022-11-14 11:16:29 +0000 | [diff] [blame] | 414 | std::set<size_t>& foundItems = fruAddresses[bus]; |
| 415 | foundItems.clear(); |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 416 | |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 417 | auto busFind = busBlocklist.find(bus); |
| 418 | if (busFind != busBlocklist.end()) |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 419 | { |
| 420 | if (busFind->second != std::nullopt) |
| 421 | { |
| 422 | for (const auto& address : *(busFind->second)) |
| 423 | { |
| 424 | skipList.insert(address); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 429 | std::set<size_t>* rootFailures = nullptr; |
| 430 | int rootBus = getRootBus(bus); |
| 431 | |
| 432 | if (rootBus >= 0) |
| 433 | { |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 434 | auto rootBusFind = busBlocklist.find(rootBus); |
| 435 | if (rootBusFind != busBlocklist.end()) |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 436 | { |
| 437 | if (rootBusFind->second != std::nullopt) |
| 438 | { |
| 439 | for (const auto& rootAddress : *(rootBusFind->second)) |
| 440 | { |
| 441 | skipList.insert(rootAddress); |
| 442 | } |
| 443 | } |
| 444 | } |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 445 | rootFailures = &(failedAddresses[rootBus]); |
AKSHAY RAVEENDRAN K | 4d4df5b | 2022-11-14 11:16:29 +0000 | [diff] [blame] | 446 | foundItems = fruAddresses[rootBus]; |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 447 | } |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 448 | |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 449 | constexpr int startSkipTargetAddr = 0; |
| 450 | constexpr int endSkipTargetAddr = 12; |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 451 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 452 | for (int ii = first; ii <= last; ii++) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 453 | { |
AKSHAY RAVEENDRAN K | 4d4df5b | 2022-11-14 11:16:29 +0000 | [diff] [blame] | 454 | if (foundItems.find(ii) != foundItems.end()) |
| 455 | { |
| 456 | continue; |
| 457 | } |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 458 | if (skipList.find(ii) != skipList.end()) |
| 459 | { |
| 460 | continue; |
| 461 | } |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 462 | // skipping since no device is present in this range |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 463 | if (ii >= startSkipTargetAddr && ii <= endSkipTargetAddr) |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 464 | { |
| 465 | continue; |
| 466 | } |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 467 | // Set target address |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 468 | if (ioctl(file, I2C_SLAVE, ii) < 0) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 469 | { |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 470 | std::cerr << "device at bus " << bus << " address " << ii |
Patrick Venture | 5d8b61d | 2019-08-06 12:36:10 -0700 | [diff] [blame] | 471 | << " busy\n"; |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 472 | continue; |
| 473 | } |
| 474 | // probe |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 475 | if (i2c_smbus_read_byte(file) < 0) |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 476 | { |
| 477 | continue; |
| 478 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 479 | |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 480 | lg2::debug("something at bus {BUS}, addr {ADDR}", "BUS", bus, |
| 481 | "ADDR", ii); |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 482 | |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 483 | makeProbeInterface(bus, ii, objServer); |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 484 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 485 | if (failedItems.find(ii) != failedItems.end()) |
| 486 | { |
| 487 | // if we failed to read it once, unlikely we can read it later |
| 488 | continue; |
| 489 | } |
| 490 | |
| 491 | if (rootFailures != nullptr) |
| 492 | { |
| 493 | if (rootFailures->find(ii) != rootFailures->end()) |
| 494 | { |
| 495 | continue; |
| 496 | } |
| 497 | } |
| 498 | |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 499 | /* Check for Device type if it is 8 bit or 16 bit */ |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 500 | std::optional<bool> is16Bit = isDevice16Bit(file); |
| 501 | if (!is16Bit.has_value()) |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 502 | { |
| 503 | std::cerr << "failed to read bus " << bus << " address " << ii |
| 504 | << "\n"; |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 505 | if (powerIsOn) |
| 506 | { |
| 507 | failedItems.insert(ii); |
| 508 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 509 | continue; |
| 510 | } |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 511 | bool is16BitBool{*is16Bit}; |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 512 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 513 | auto readFunc = [is16BitBool, file, |
| 514 | ii](off_t offset, size_t length, uint8_t* outbuf) { |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 515 | return readData(is16BitBool, false, file, ii, offset, length, |
| 516 | outbuf); |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 517 | }; |
| 518 | FRUReader reader(std::move(readFunc)); |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 519 | std::string errorMessage = |
| 520 | "bus " + std::to_string(bus) + " address " + std::to_string(ii); |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 521 | std::pair<std::vector<uint8_t>, bool> pair = |
| 522 | readFRUContents(reader, errorMessage); |
| 523 | const bool foundHeader = pair.second; |
| 524 | |
| 525 | if (!foundHeader && !is16BitBool) |
| 526 | { |
| 527 | // certain FRU eeproms require bytewise reading. |
| 528 | // otherwise garbage is read. e.g. SuperMicro PWS 920P-SQ |
| 529 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 530 | auto readFunc = |
| 531 | [is16BitBool, file, |
| 532 | ii](off_t offset, size_t length, uint8_t* outbuf) { |
| 533 | return readData(is16BitBool, true, file, ii, offset, |
| 534 | length, outbuf); |
| 535 | }; |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 536 | FRUReader readerBytewise(std::move(readFunc)); |
| 537 | pair = readFRUContents(readerBytewise, errorMessage); |
| 538 | } |
| 539 | |
| 540 | if (pair.first.empty()) |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 541 | { |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 542 | continue; |
| 543 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 544 | |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 545 | devices->emplace(ii, pair.first); |
AKSHAY RAVEENDRAN K | 4d4df5b | 2022-11-14 11:16:29 +0000 | [diff] [blame] | 546 | fruAddresses[bus].insert(ii); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 547 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 548 | return 1; |
| 549 | }); |
| 550 | std::future_status status = |
| 551 | future.wait_for(std::chrono::seconds(busTimeoutSeconds)); |
| 552 | if (status == std::future_status::timeout) |
| 553 | { |
| 554 | std::cerr << "Error reading bus " << bus << "\n"; |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 555 | if (powerIsOn) |
| 556 | { |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 557 | busBlocklist[bus] = std::nullopt; |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 558 | } |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 559 | close(file); |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 560 | return -1; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 561 | } |
| 562 | |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 563 | close(file); |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 564 | return future.get(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 567 | void loadBlocklist(const char* path) |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 568 | { |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 569 | std::ifstream blocklistStream(path); |
| 570 | if (!blocklistStream.good()) |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 571 | { |
| 572 | // File is optional. |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 573 | std::cerr << "Cannot open blocklist file.\n\n"; |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 577 | nlohmann::json data = |
| 578 | nlohmann::json::parse(blocklistStream, nullptr, false); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 579 | if (data.is_discarded()) |
| 580 | { |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 581 | std::cerr << "Illegal blocklist file detected, cannot validate JSON, " |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 582 | "exiting\n"; |
| 583 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | // It's expected to have at least one field, "buses" that is an array of the |
| 587 | // buses by integer. Allow for future options to exclude further aspects, |
| 588 | // such as specific addresses or ranges. |
| 589 | if (data.type() != nlohmann::json::value_t::object) |
| 590 | { |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 591 | std::cerr << "Illegal blocklist, expected to read dictionary\n"; |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 592 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // If buses field is missing, that's fine. |
| 596 | if (data.count("buses") == 1) |
| 597 | { |
| 598 | // Parse the buses array after a little validation. |
| 599 | auto buses = data.at("buses"); |
| 600 | if (buses.type() != nlohmann::json::value_t::array) |
| 601 | { |
| 602 | // Buses field present but invalid, therefore this is an error. |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 603 | std::cerr << "Invalid contents for blocklist buses field\n"; |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 604 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | // Catch exception here for type mis-match. |
| 608 | try |
| 609 | { |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 610 | for (const auto& busIterator : buses) |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 611 | { |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 612 | // If bus and addresses field are missing, that's fine. |
| 613 | if (busIterator.contains("bus") && |
| 614 | busIterator.contains("addresses")) |
| 615 | { |
| 616 | auto busData = busIterator.at("bus"); |
| 617 | auto bus = busData.get<size_t>(); |
| 618 | |
| 619 | auto addressData = busIterator.at("addresses"); |
| 620 | auto addresses = |
| 621 | addressData.get<std::set<std::string_view>>(); |
| 622 | |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 623 | auto& block = busBlocklist[bus].emplace(); |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 624 | for (const auto& address : addresses) |
| 625 | { |
| 626 | size_t addressInt = 0; |
| 627 | std::from_chars(address.begin() + 2, address.end(), |
| 628 | addressInt, 16); |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 629 | block.insert(addressInt); |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | else |
| 633 | { |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 634 | busBlocklist[busIterator.get<size_t>()] = std::nullopt; |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 635 | } |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | catch (const nlohmann::detail::type_error& e) |
| 639 | { |
| 640 | // Type mis-match is a critical error. |
| 641 | std::cerr << "Invalid bus type: " << e.what() << "\n"; |
| 642 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 643 | } |
| 644 | } |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 647 | static void findI2CDevices(const std::vector<fs::path>& i2cBuses, |
Jonathan Doman | 2418a61 | 2022-02-14 18:20:58 -0800 | [diff] [blame] | 648 | BusMap& busmap, const bool& powerIsOn, |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 649 | sdbusplus::asio::object_server& objServer) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 650 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 651 | for (const auto& i2cBus : i2cBuses) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 652 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 653 | int bus = busStrToInt(i2cBus.string()); |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 654 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 655 | if (bus < 0) |
| 656 | { |
| 657 | std::cerr << "Cannot translate " << i2cBus << " to int\n"; |
| 658 | continue; |
| 659 | } |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 660 | auto busFind = busBlocklist.find(bus); |
| 661 | if (busFind != busBlocklist.end()) |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 662 | { |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 663 | if (busFind->second == std::nullopt) |
| 664 | { |
| 665 | continue; // Skip blocked busses. |
| 666 | } |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 667 | } |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 668 | int rootBus = getRootBus(bus); |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 669 | auto rootBusFind = busBlocklist.find(rootBus); |
| 670 | if (rootBusFind != busBlocklist.end()) |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 671 | { |
Bonnie Lo | bfda2c4 | 2022-10-26 16:50:54 +0800 | [diff] [blame] | 672 | if (rootBusFind->second == std::nullopt) |
| 673 | { |
| 674 | continue; |
| 675 | } |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 676 | } |
| 677 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 678 | auto file = open(i2cBus.c_str(), O_RDWR); |
| 679 | if (file < 0) |
| 680 | { |
| 681 | std::cerr << "unable to open i2c device " << i2cBus.string() |
| 682 | << "\n"; |
| 683 | continue; |
| 684 | } |
| 685 | unsigned long funcs = 0; |
| 686 | |
| 687 | if (ioctl(file, I2C_FUNCS, &funcs) < 0) |
| 688 | { |
| 689 | std::cerr |
Patrick Venture | 98e0cf3 | 2019-08-02 11:11:03 -0700 | [diff] [blame] | 690 | << "Error: Could not get the adapter functionality matrix bus " |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 691 | << bus << "\n"; |
Zhikui Ren | c02d8cb | 2021-06-28 16:56:08 -0700 | [diff] [blame] | 692 | close(file); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 693 | continue; |
| 694 | } |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 695 | if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) || |
| 696 | ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 697 | { |
| 698 | std::cerr << "Error: Can't use SMBus Receive Byte command bus " |
| 699 | << bus << "\n"; |
| 700 | continue; |
| 701 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 702 | auto& device = busmap[bus]; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 703 | device = std::make_shared<DeviceMap>(); |
| 704 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 705 | // i2cdetect by default uses the range 0x03 to 0x77, as |
| 706 | // this is what we have tested with, use this range. Could be |
| 707 | // changed in future. |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 708 | lg2::debug("Scanning bus {BUS}", "BUS", bus); |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 709 | |
| 710 | // fd is closed in this function in case the bus locks up |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 711 | getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer); |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 712 | |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 713 | lg2::debug("Done scanning bus {BUS}", "BUS", bus); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 714 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 715 | } |
| 716 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 717 | // this class allows an async response after all i2c devices are discovered |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 718 | struct FindDevicesWithCallback : |
| 719 | std::enable_shared_from_this<FindDevicesWithCallback> |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 720 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 721 | FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses, |
Jonathan Doman | 2418a61 | 2022-02-14 18:20:58 -0800 | [diff] [blame] | 722 | BusMap& busmap, const bool& powerIsOn, |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 723 | sdbusplus::asio::object_server& objServer, |
Delphine CC Chiu | a3ca14a | 2024-03-27 17:02:24 +0800 | [diff] [blame] | 724 | std::function<void()>&& callback) : |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 725 | _i2cBuses(i2cBuses), _busMap(busmap), _powerIsOn(powerIsOn), |
| 726 | _objServer(objServer), _callback(std::move(callback)) |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 727 | {} |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 728 | ~FindDevicesWithCallback() |
| 729 | { |
| 730 | _callback(); |
| 731 | } |
| 732 | void run() |
| 733 | { |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 734 | findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 735 | } |
| 736 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 737 | const std::vector<fs::path>& _i2cBuses; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 738 | BusMap& _busMap; |
Jonathan Doman | 2418a61 | 2022-02-14 18:20:58 -0800 | [diff] [blame] | 739 | const bool& _powerIsOn; |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 740 | sdbusplus::asio::object_server& _objServer; |
Delphine CC Chiu | a3ca14a | 2024-03-27 17:02:24 +0800 | [diff] [blame] | 741 | std::function<void()> _callback; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 742 | }; |
| 743 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 744 | void addFruObjectToDbus( |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 745 | std::vector<uint8_t>& device, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 746 | boost::container::flat_map< |
| 747 | std::pair<size_t, size_t>, |
| 748 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
Kumar Thangavel | 1e8e71f | 2021-12-01 19:39:11 +0530 | [diff] [blame] | 749 | uint32_t bus, uint32_t address, size_t& unknownBusObjectCount, |
Jonathan Doman | 2418a61 | 2022-02-14 18:20:58 -0800 | [diff] [blame] | 750 | const bool& powerIsOn, sdbusplus::asio::object_server& objServer, |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 751 | std::shared_ptr<sdbusplus::asio::connection>& systemBus) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 752 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 753 | boost::container::flat_map<std::string, std::string> formattedFRU; |
Kumar Thangavel | 9f2162a | 2022-08-10 18:05:20 +0530 | [diff] [blame] | 754 | |
| 755 | std::optional<std::string> optionalProductName = getProductName( |
| 756 | device, formattedFRU, bus, address, unknownBusObjectCount); |
| 757 | if (!optionalProductName) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 758 | { |
Kumar Thangavel | 9f2162a | 2022-08-10 18:05:20 +0530 | [diff] [blame] | 759 | std::cerr << "getProductName failed. product name is empty.\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 760 | return; |
| 761 | } |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 762 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 763 | std::string productName = |
| 764 | "/xyz/openbmc_project/FruDevice/" + optionalProductName.value(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 765 | |
Kumar Thangavel | d79d025 | 2022-08-24 14:26:01 +0530 | [diff] [blame] | 766 | std::optional<int> index = findIndexForFRU(dbusInterfaceMap, productName); |
| 767 | if (index.has_value()) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 768 | { |
Kumar Thangavel | d79d025 | 2022-08-24 14:26:01 +0530 | [diff] [blame] | 769 | productName += "_"; |
| 770 | productName += std::to_string(++(*index)); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 771 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 772 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 773 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 774 | objServer.add_interface(productName, "xyz.openbmc_project.FruDevice"); |
| 775 | dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface; |
| 776 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 777 | for (auto& property : formattedFRU) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 778 | { |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 779 | std::regex_replace(property.second.begin(), property.second.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 780 | property.second.end(), nonAsciiRegex, "_"); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 781 | if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG") |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 782 | { |
| 783 | continue; |
| 784 | } |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 785 | std::string key = |
| 786 | std::regex_replace(property.first, nonAsciiRegex, "_"); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 787 | |
| 788 | if (property.first == "PRODUCT_ASSET_TAG") |
| 789 | { |
| 790 | std::string propertyName = property.first; |
| 791 | iface->register_property( |
| 792 | key, property.second + '\0', |
Kumar Thangavel | 41a9051 | 2021-11-24 15:44:20 +0530 | [diff] [blame] | 793 | [bus, address, propertyName, &dbusInterfaceMap, |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 794 | &unknownBusObjectCount, &powerIsOn, &objServer, |
| 795 | &systemBus](const std::string& req, std::string& resp) { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 796 | if (strcmp(req.c_str(), resp.c_str()) != 0) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 797 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 798 | // call the method which will update |
| 799 | if (updateFRUProperty(req, bus, address, propertyName, |
| 800 | dbusInterfaceMap, |
| 801 | unknownBusObjectCount, powerIsOn, |
| 802 | objServer, systemBus)) |
| 803 | { |
| 804 | resp = req; |
| 805 | } |
| 806 | else |
| 807 | { |
| 808 | throw std::invalid_argument( |
| 809 | "FRU property update failed."); |
| 810 | } |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 811 | } |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 812 | return 1; |
| 813 | }); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 814 | } |
| 815 | else if (!iface->register_property(key, property.second + '\0')) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 816 | { |
| 817 | std::cerr << "illegal key: " << key << "\n"; |
| 818 | } |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 819 | lg2::debug("parsed FRU property: {FIRST}: {SECOND}", "FIRST", |
| 820 | property.first, "SECOND", property.second); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 821 | } |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 822 | |
| 823 | // baseboard will be 0, 0 |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 824 | iface->register_property("BUS", bus); |
| 825 | iface->register_property("ADDRESS", address); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 826 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 827 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 828 | } |
| 829 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 830 | static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 831 | { |
| 832 | // try to read baseboard fru from file |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 833 | std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary); |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 834 | if (baseboardFRUFile.good()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 835 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 836 | baseboardFRUFile.seekg(0, std::ios_base::end); |
| 837 | size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg()); |
| 838 | baseboardFRU.resize(fileSize); |
| 839 | baseboardFRUFile.seekg(0, std::ios_base::beg); |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 840 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 841 | char* charOffset = reinterpret_cast<char*>(baseboardFRU.data()); |
| 842 | baseboardFRUFile.read(charOffset, fileSize); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 843 | } |
| 844 | else |
| 845 | { |
| 846 | return false; |
| 847 | } |
| 848 | return true; |
| 849 | } |
| 850 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 851 | bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 852 | { |
| 853 | boost::container::flat_map<std::string, std::string> tmp; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 854 | if (fru.size() > maxFruSize) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 855 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 856 | std::cerr << "Invalid fru.size() during writeFRU\n"; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 857 | return false; |
| 858 | } |
| 859 | // verify legal fru by running it through fru parsing logic |
Michael Shen | 0961b11 | 2022-02-22 11:06:33 +0800 | [diff] [blame] | 860 | if (formatIPMIFRU(fru, tmp) != resCodes::resOK) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 861 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 862 | std::cerr << "Invalid fru format during writeFRU\n"; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 863 | return false; |
| 864 | } |
| 865 | // baseboard fru |
| 866 | if (bus == 0 && address == 0) |
| 867 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 868 | std::ofstream file(baseboardFruLocation, std::ios_base::binary); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 869 | if (!file.good()) |
| 870 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 871 | std::cerr << "Error opening file " << baseboardFruLocation << "\n"; |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 872 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 873 | return false; |
| 874 | } |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 875 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 876 | const char* charOffset = reinterpret_cast<const char*>(fru.data()); |
| 877 | file.write(charOffset, fru.size()); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 878 | return file.good(); |
| 879 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 880 | |
| 881 | if (hasEepromFile(bus, address)) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 882 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 883 | auto path = getEepromPath(bus, address); |
| 884 | int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC); |
| 885 | if (eeprom < 0) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 886 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 887 | std::cerr << "unable to open i2c device " << path << "\n"; |
| 888 | throw DBusInternalError(); |
| 889 | return false; |
| 890 | } |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 891 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 892 | ssize_t writtenBytes = write(eeprom, fru.data(), fru.size()); |
| 893 | if (writtenBytes < 0) |
| 894 | { |
| 895 | std::cerr << "unable to write to i2c device " << path << "\n"; |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 896 | close(eeprom); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 897 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 898 | return false; |
| 899 | } |
| 900 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 901 | close(eeprom); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 902 | return true; |
| 903 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 904 | |
| 905 | std::string i2cBus = "/dev/i2c-" + std::to_string(bus); |
| 906 | |
| 907 | int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC); |
| 908 | if (file < 0) |
| 909 | { |
| 910 | std::cerr << "unable to open i2c device " << i2cBus << "\n"; |
| 911 | throw DBusInternalError(); |
| 912 | return false; |
| 913 | } |
| 914 | if (ioctl(file, I2C_SLAVE_FORCE, address) < 0) |
| 915 | { |
| 916 | std::cerr << "unable to set device address\n"; |
| 917 | close(file); |
| 918 | throw DBusInternalError(); |
| 919 | return false; |
| 920 | } |
| 921 | |
| 922 | constexpr const size_t retryMax = 2; |
| 923 | uint16_t index = 0; |
| 924 | size_t retries = retryMax; |
| 925 | while (index < fru.size()) |
| 926 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 927 | if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) && |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 928 | (retries == retryMax)) |
| 929 | { |
| 930 | // The 4K EEPROM only uses the A2 and A1 device address bits |
| 931 | // with the third bit being a memory page address bit. |
| 932 | if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0) |
| 933 | { |
| 934 | std::cerr << "unable to set device address\n"; |
| 935 | close(file); |
| 936 | throw DBusInternalError(); |
| 937 | return false; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index), |
| 942 | fru[index]) < 0) |
| 943 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 944 | if ((retries--) == 0U) |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 945 | { |
| 946 | std::cerr << "error writing fru: " << strerror(errno) << "\n"; |
| 947 | close(file); |
| 948 | throw DBusInternalError(); |
| 949 | return false; |
| 950 | } |
| 951 | } |
| 952 | else |
| 953 | { |
| 954 | retries = retryMax; |
| 955 | index++; |
| 956 | } |
| 957 | // most eeproms require 5-10ms between writes |
| 958 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 959 | } |
| 960 | close(file); |
| 961 | return true; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 964 | void rescanOneBus( |
krishnar4 | 213ee21 | 2022-11-11 15:39:30 +0530 | [diff] [blame] | 965 | BusMap& busmap, uint16_t busNum, |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 966 | boost::container::flat_map< |
| 967 | std::pair<size_t, size_t>, |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 968 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
Jonathan Doman | 2418a61 | 2022-02-14 18:20:58 -0800 | [diff] [blame] | 969 | bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn, |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 970 | sdbusplus::asio::object_server& objServer, |
| 971 | std::shared_ptr<sdbusplus::asio::connection>& systemBus) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 972 | { |
Yong Li | 6fdfac0 | 2023-10-30 11:21:30 +0800 | [diff] [blame] | 973 | for (auto device = foundDevices.begin(); device != foundDevices.end();) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 974 | { |
Yong Li | 6fdfac0 | 2023-10-30 11:21:30 +0800 | [diff] [blame] | 975 | if (device->first.first == static_cast<size_t>(busNum)) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 976 | { |
Yong Li | 6fdfac0 | 2023-10-30 11:21:30 +0800 | [diff] [blame] | 977 | objServer.remove_interface(device->second); |
| 978 | device = foundDevices.erase(device); |
| 979 | } |
| 980 | else |
| 981 | { |
| 982 | device++; |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 986 | fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum)); |
| 987 | if (!fs::exists(busPath)) |
| 988 | { |
| 989 | if (dbusCall) |
| 990 | { |
| 991 | std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum) |
| 992 | << "\n"; |
| 993 | throw std::invalid_argument("Invalid Bus."); |
| 994 | } |
| 995 | return; |
| 996 | } |
| 997 | |
| 998 | std::vector<fs::path> i2cBuses; |
| 999 | i2cBuses.emplace_back(busPath); |
| 1000 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1001 | auto scan = std::make_shared<FindDevicesWithCallback>( |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 1002 | i2cBuses, busmap, powerIsOn, objServer, |
| 1003 | [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn, |
| 1004 | &objServer, &systemBus]() { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1005 | for (auto busIface = dbusInterfaceMap.begin(); |
| 1006 | busIface != dbusInterfaceMap.end();) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1007 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1008 | if (busIface->first.first == static_cast<size_t>(busNum)) |
| 1009 | { |
| 1010 | objServer.remove_interface(busIface->second); |
| 1011 | busIface = dbusInterfaceMap.erase(busIface); |
| 1012 | } |
| 1013 | else |
| 1014 | { |
| 1015 | busIface++; |
| 1016 | } |
Yong Li | 6fdfac0 | 2023-10-30 11:21:30 +0800 | [diff] [blame] | 1017 | } |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1018 | auto found = busmap.find(busNum); |
| 1019 | if (found == busmap.end() || found->second == nullptr) |
Yong Li | 6fdfac0 | 2023-10-30 11:21:30 +0800 | [diff] [blame] | 1020 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1021 | return; |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1022 | } |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1023 | for (auto& device : *(found->second)) |
| 1024 | { |
| 1025 | addFruObjectToDbus(device.second, dbusInterfaceMap, |
| 1026 | static_cast<uint32_t>(busNum), device.first, |
| 1027 | unknownBusObjectCount, powerIsOn, objServer, |
| 1028 | systemBus); |
| 1029 | } |
| 1030 | }); |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1031 | scan->run(); |
| 1032 | } |
| 1033 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1034 | void rescanBusses( |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1035 | BusMap& busmap, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1036 | boost::container::flat_map< |
| 1037 | std::pair<size_t, size_t>, |
Kumar Thangavel | 41a9051 | 2021-11-24 15:44:20 +0530 | [diff] [blame] | 1038 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
Jonathan Doman | 2418a61 | 2022-02-14 18:20:58 -0800 | [diff] [blame] | 1039 | size_t& unknownBusObjectCount, const bool& powerIsOn, |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 1040 | sdbusplus::asio::object_server& objServer, |
| 1041 | std::shared_ptr<sdbusplus::asio::connection>& systemBus) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1042 | { |
Ed Tanous | aa497ed | 2023-02-28 13:43:41 -0800 | [diff] [blame] | 1043 | static boost::asio::steady_timer timer(io); |
| 1044 | timer.expires_from_now(std::chrono::seconds(1)); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1045 | |
Gunnar Mills | 6f0ae94 | 2018-08-31 12:38:03 -0500 | [diff] [blame] | 1046 | // setup an async wait in case we get flooded with requests |
Jian Zhang | 6e7cebf | 2022-11-29 18:30:21 +0800 | [diff] [blame] | 1047 | timer.async_wait([&](const boost::system::error_code& ec) { |
| 1048 | if (ec == boost::asio::error::operation_aborted) |
| 1049 | { |
| 1050 | return; |
| 1051 | } |
| 1052 | |
| 1053 | if (ec) |
| 1054 | { |
| 1055 | std::cerr << "Error in timer: " << ec.message() << "\n"; |
| 1056 | return; |
| 1057 | } |
| 1058 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1059 | auto devDir = fs::path("/dev/"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1060 | std::vector<fs::path> i2cBuses; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1061 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1062 | boost::container::flat_map<size_t, fs::path> busPaths; |
| 1063 | if (!getI2cDevicePaths(devDir, busPaths)) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1064 | { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1065 | std::cerr << "unable to find i2c devices\n"; |
| 1066 | return; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1067 | } |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1068 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1069 | for (const auto& busPath : busPaths) |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1070 | { |
| 1071 | i2cBuses.emplace_back(busPath.second); |
| 1072 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1073 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1074 | busmap.clear(); |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 1075 | for (auto& [pair, interface] : foundDevices) |
| 1076 | { |
| 1077 | objServer.remove_interface(interface); |
| 1078 | } |
| 1079 | foundDevices.clear(); |
| 1080 | |
Kumar Thangavel | 1e8e71f | 2021-12-01 19:39:11 +0530 | [diff] [blame] | 1081 | auto scan = std::make_shared<FindDevicesWithCallback>( |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 1082 | i2cBuses, busmap, powerIsOn, objServer, [&]() { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1083 | for (auto& busIface : dbusInterfaceMap) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1084 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1085 | objServer.remove_interface(busIface.second); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1086 | } |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1087 | |
| 1088 | dbusInterfaceMap.clear(); |
| 1089 | unknownBusObjectCount = 0; |
| 1090 | |
| 1091 | // todo, get this from a more sensable place |
| 1092 | std::vector<uint8_t> baseboardFRU; |
| 1093 | if (readBaseboardFRU(baseboardFRU)) |
| 1094 | { |
| 1095 | // If no device on i2c bus 0, the insertion will happen. |
| 1096 | auto bus0 = |
| 1097 | busmap.try_emplace(0, std::make_shared<DeviceMap>()); |
| 1098 | bus0.first->second->emplace(0, baseboardFRU); |
| 1099 | } |
| 1100 | for (auto& devicemap : busmap) |
| 1101 | { |
| 1102 | for (auto& device : *devicemap.second) |
| 1103 | { |
| 1104 | addFruObjectToDbus(device.second, dbusInterfaceMap, |
| 1105 | devicemap.first, device.first, |
| 1106 | unknownBusObjectCount, powerIsOn, |
| 1107 | objServer, systemBus); |
| 1108 | } |
| 1109 | } |
| 1110 | }); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1111 | scan->run(); |
| 1112 | }); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1115 | // Details with example of Asset Tag Update |
| 1116 | // To find location of Product Info Area asset tag as per FRU specification |
| 1117 | // 1. Find product Info area starting offset (*8 - as header will be in |
| 1118 | // multiple of 8 bytes). |
| 1119 | // 2. Skip 3 bytes of product info area (like format version, area length, |
| 1120 | // and language code). |
| 1121 | // 3. Traverse manufacturer name, product name, product version, & product |
| 1122 | // serial number, by reading type/length code to reach the Asset Tag. |
| 1123 | // 4. Update the Asset Tag, reposition the product Info area in multiple of |
| 1124 | // 8 bytes. Update the Product area length and checksum. |
| 1125 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1126 | bool updateFRUProperty( |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1127 | const std::string& updatePropertyReq, uint32_t bus, uint32_t address, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1128 | const std::string& propertyName, |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1129 | boost::container::flat_map< |
| 1130 | std::pair<size_t, size_t>, |
Kumar Thangavel | 41a9051 | 2021-11-24 15:44:20 +0530 | [diff] [blame] | 1131 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
Jonathan Doman | 2418a61 | 2022-02-14 18:20:58 -0800 | [diff] [blame] | 1132 | size_t& unknownBusObjectCount, const bool& powerIsOn, |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 1133 | sdbusplus::asio::object_server& objServer, |
| 1134 | std::shared_ptr<sdbusplus::asio::connection>& systemBus) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1135 | { |
| 1136 | size_t updatePropertyReqLen = updatePropertyReq.length(); |
| 1137 | if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63) |
| 1138 | { |
| 1139 | std::cerr |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1140 | << "FRU field data cannot be of 1 char or more than 63 chars. " |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1141 | "Invalid Length " |
| 1142 | << updatePropertyReqLen << "\n"; |
| 1143 | return false; |
| 1144 | } |
| 1145 | |
| 1146 | std::vector<uint8_t> fruData; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1147 | |
Kumar Thangavel | 9d6f590 | 2022-08-26 16:52:14 +0530 | [diff] [blame] | 1148 | if (!getFruData(fruData, bus, address)) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1149 | { |
Kumar Thangavel | 9d6f590 | 2022-08-26 16:52:14 +0530 | [diff] [blame] | 1150 | std::cerr << "Failure getting FRU Data \n"; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1151 | return false; |
| 1152 | } |
| 1153 | |
AKSHAY RAVEENDRAN K | 4d4df5b | 2022-11-14 11:16:29 +0000 | [diff] [blame] | 1154 | struct FruArea fruAreaParams |
| 1155 | {}; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1156 | |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 1157 | if (!findFruAreaLocationAndField(fruData, propertyName, fruAreaParams)) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1158 | { |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 1159 | std::cerr << "findFruAreaLocationAndField failed \n"; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1160 | return false; |
| 1161 | } |
| 1162 | |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 1163 | std::vector<uint8_t> restFRUAreaFieldsData; |
| 1164 | if (!copyRestFRUArea(fruData, propertyName, fruAreaParams, |
| 1165 | restFRUAreaFieldsData)) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1166 | { |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 1167 | std::cerr << "copyRestFRUArea failed \n"; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1168 | return false; |
| 1169 | } |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1170 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1171 | // Push post update fru areas if any |
| 1172 | unsigned int nextFRUAreaLoc = 0; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1173 | for (fruAreas nextFRUArea = fruAreas::fruAreaInternal; |
| 1174 | nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea) |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1175 | { |
| 1176 | unsigned int fruAreaLoc = |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1177 | fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize; |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 1178 | if ((fruAreaLoc > fruAreaParams.restFieldsEnd) && |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1179 | ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc))) |
| 1180 | { |
| 1181 | nextFRUAreaLoc = fruAreaLoc; |
| 1182 | } |
| 1183 | } |
| 1184 | std::vector<uint8_t> restFRUAreasData; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 1185 | if (nextFRUAreaLoc != 0U) |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1186 | { |
| 1187 | std::copy_n(fruData.begin() + nextFRUAreaLoc, |
| 1188 | fruData.size() - nextFRUAreaLoc, |
| 1189 | std::back_inserter(restFRUAreasData)); |
| 1190 | } |
| 1191 | |
| 1192 | // check FRU area size |
| 1193 | size_t fruAreaDataSize = |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 1194 | ((fruAreaParams.updateFieldLoc - fruAreaParams.start + 1) + |
| 1195 | restFRUAreaFieldsData.size()); |
| 1196 | size_t fruAreaAvailableSize = fruAreaParams.size - fruAreaDataSize; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1197 | if ((updatePropertyReqLen + 1) > fruAreaAvailableSize) |
| 1198 | { |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1199 | #ifdef ENABLE_FRU_AREA_RESIZE |
| 1200 | size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1; |
| 1201 | // round size to 8-byte blocks |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1202 | newFRUAreaSize = |
| 1203 | ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize; |
| 1204 | size_t newFRUDataSize = |
| 1205 | fruData.size() + newFRUAreaSize - fruAreaParams.size; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1206 | fruData.resize(newFRUDataSize); |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 1207 | fruAreaParams.size = newFRUAreaSize; |
| 1208 | fruAreaParams.end = fruAreaParams.start + fruAreaParams.size; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1209 | #else |
| 1210 | std::cerr << "FRU field length: " << updatePropertyReqLen + 1 |
| 1211 | << " should not be greater than available FRU area size: " |
| 1212 | << fruAreaAvailableSize << "\n"; |
| 1213 | return false; |
| 1214 | #endif // ENABLE_FRU_AREA_RESIZE |
| 1215 | } |
| 1216 | |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1217 | // write new requested property field length and data |
| 1218 | constexpr uint8_t newTypeLenMask = 0xC0; |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 1219 | fruData[fruAreaParams.updateFieldLoc] = |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1220 | static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask); |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 1221 | fruAreaParams.updateFieldLoc++; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1222 | std::copy(updatePropertyReq.begin(), updatePropertyReq.end(), |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 1223 | fruData.begin() + fruAreaParams.updateFieldLoc); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1224 | |
| 1225 | // Copy remaining data to main fru area - post updated fru field vector |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1226 | fruAreaParams.restFieldsLoc = |
| 1227 | fruAreaParams.updateFieldLoc + updatePropertyReqLen; |
| 1228 | size_t fruAreaDataEnd = |
| 1229 | fruAreaParams.restFieldsLoc + restFRUAreaFieldsData.size(); |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 1230 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1231 | std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(), |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 1232 | fruData.begin() + fruAreaParams.restFieldsLoc); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1233 | |
| 1234 | // Update final fru with new fru area length and checksum |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1235 | unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum( |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 1236 | fruData, fruAreaParams.start, fruAreaDataEnd, fruAreaParams.end); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1237 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1238 | #ifdef ENABLE_FRU_AREA_RESIZE |
| 1239 | ++nextFRUAreaNewLoc; |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1240 | ssize_t nextFRUAreaOffsetDiff = |
| 1241 | (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1242 | // Append rest FRU Areas if size changed and there were other sections after |
| 1243 | // updated one |
| 1244 | if (nextFRUAreaOffsetDiff && nextFRUAreaLoc) |
| 1245 | { |
| 1246 | std::copy(restFRUAreasData.begin(), restFRUAreasData.end(), |
| 1247 | fruData.begin() + nextFRUAreaNewLoc); |
| 1248 | // Update Common Header |
Ed Tanous | 6e22c87 | 2023-02-16 15:19:28 -0800 | [diff] [blame] | 1249 | for (fruAreas nextFRUArea = fruAreas::fruAreaInternal; |
| 1250 | nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea) |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1251 | { |
Ed Tanous | 6e22c87 | 2023-02-16 15:19:28 -0800 | [diff] [blame] | 1252 | unsigned int fruAreaOffsetField = |
| 1253 | getHeaderAreaFieldOffset(nextFRUArea); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1254 | size_t curFRUAreaOffset = fruData[fruAreaOffsetField]; |
Ed Tanous | 6e22c87 | 2023-02-16 15:19:28 -0800 | [diff] [blame] | 1255 | if (curFRUAreaOffset > fruAreaParams.end) |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1256 | { |
| 1257 | fruData[fruAreaOffsetField] = static_cast<int8_t>( |
| 1258 | curFRUAreaOffset + nextFRUAreaOffsetDiff); |
| 1259 | } |
| 1260 | } |
| 1261 | // Calculate new checksum |
| 1262 | std::vector<uint8_t> headerFRUData; |
| 1263 | std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData)); |
| 1264 | size_t checksumVal = calculateChecksum(headerFRUData); |
| 1265 | fruData[7] = static_cast<uint8_t>(checksumVal); |
| 1266 | // fill zeros if FRU Area size decreased |
| 1267 | if (nextFRUAreaOffsetDiff < 0) |
| 1268 | { |
| 1269 | std::fill(fruData.begin() + nextFRUAreaNewLoc + |
| 1270 | restFRUAreasData.size(), |
| 1271 | fruData.end(), 0); |
| 1272 | } |
| 1273 | } |
| 1274 | #else |
| 1275 | // this is to avoid "unused variable" warning |
| 1276 | (void)nextFRUAreaNewLoc; |
| 1277 | #endif // ENABLE_FRU_AREA_RESIZE |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1278 | if (fruData.empty()) |
| 1279 | { |
| 1280 | return false; |
| 1281 | } |
| 1282 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1283 | if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address), |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1284 | fruData)) |
| 1285 | { |
| 1286 | return false; |
| 1287 | } |
| 1288 | |
| 1289 | // Rescan the bus so that GetRawFru dbus-call fetches updated values |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 1290 | rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn, |
| 1291 | objServer, systemBus); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1292 | return true; |
| 1293 | } |
| 1294 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1295 | int main() |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1296 | { |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 1297 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 1298 | sdbusplus::asio::object_server objServer(systemBus); |
| 1299 | |
Kumar Thangavel | 41a9051 | 2021-11-24 15:44:20 +0530 | [diff] [blame] | 1300 | static size_t unknownBusObjectCount = 0; |
Kumar Thangavel | 1e8e71f | 2021-12-01 19:39:11 +0530 | [diff] [blame] | 1301 | static bool powerIsOn = false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1302 | auto devDir = fs::path("/dev/"); |
James Feist | c9dff1b | 2019-02-13 13:33:13 -0800 | [diff] [blame] | 1303 | auto matchString = std::string(R"(i2c-\d+$)"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1304 | std::vector<fs::path> i2cBuses; |
| 1305 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 1306 | if (!findFiles(devDir, matchString, i2cBuses)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1307 | { |
| 1308 | std::cerr << "unable to find i2c devices\n"; |
| 1309 | return 1; |
| 1310 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1311 | |
Matt Simmering | 2447c24 | 2023-11-09 14:18:39 -0800 | [diff] [blame] | 1312 | // check for and load blocklist with initial buses. |
| 1313 | loadBlocklist(blocklistPath); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 1314 | |
Vijay Khemka | 065f6d9 | 2018-12-18 10:37:47 -0800 | [diff] [blame] | 1315 | systemBus->request_name("xyz.openbmc_project.FruDevice"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1316 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1317 | // this is a map with keys of pair(bus number, address) and values of |
| 1318 | // the object on dbus |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1319 | boost::container::flat_map<std::pair<size_t, size_t>, |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1320 | std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 1321 | dbusInterfaceMap; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1322 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1323 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 1324 | objServer.add_interface("/xyz/openbmc_project/FruDevice", |
| 1325 | "xyz.openbmc_project.FruDeviceManager"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1326 | |
Kumar Thangavel | 41a9051 | 2021-11-24 15:44:20 +0530 | [diff] [blame] | 1327 | iface->register_method("ReScan", [&]() { |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 1328 | rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn, |
| 1329 | objServer, systemBus); |
Kumar Thangavel | 41a9051 | 2021-11-24 15:44:20 +0530 | [diff] [blame] | 1330 | }); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1331 | |
krishnar4 | 213ee21 | 2022-11-11 15:39:30 +0530 | [diff] [blame] | 1332 | iface->register_method("ReScanBus", [&](uint16_t bus) { |
Kumar Thangavel | 1e8e71f | 2021-12-01 19:39:11 +0530 | [diff] [blame] | 1333 | rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount, |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 1334 | powerIsOn, objServer, systemBus); |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1335 | }); |
| 1336 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1337 | iface->register_method("GetRawFru", getFRUInfo); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1338 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1339 | iface->register_method( |
| 1340 | "WriteFru", [&](const uint16_t bus, const uint8_t address, |
| 1341 | const std::vector<uint8_t>& data) { |
| 1342 | if (!writeFRU(bus, address, data)) |
| 1343 | { |
| 1344 | throw std::invalid_argument("Invalid Arguments."); |
| 1345 | return; |
| 1346 | } |
| 1347 | // schedule rescan on success |
| 1348 | rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, |
| 1349 | powerIsOn, objServer, systemBus); |
| 1350 | }); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1351 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1352 | |
Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 1353 | std::function<void(sdbusplus::message_t & message)> eventHandler = |
| 1354 | [&](sdbusplus::message_t& message) { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1355 | std::string objectName; |
| 1356 | boost::container::flat_map< |
| 1357 | std::string, |
| 1358 | std::variant<std::string, bool, int64_t, uint64_t, double>> |
| 1359 | values; |
| 1360 | message.read(objectName, values); |
| 1361 | auto findState = values.find("CurrentHostState"); |
| 1362 | if (findState != values.end()) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1363 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1364 | if (std::get<std::string>(findState->second) == |
| 1365 | "xyz.openbmc_project.State.Host.HostState.Running") |
| 1366 | { |
| 1367 | powerIsOn = true; |
| 1368 | } |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1369 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1370 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1371 | if (powerIsOn) |
| 1372 | { |
| 1373 | rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, |
| 1374 | powerIsOn, objServer, systemBus); |
| 1375 | } |
| 1376 | }; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1377 | |
Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 1378 | sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t( |
| 1379 | static_cast<sdbusplus::bus_t&>(*systemBus), |
James Feist | 7bcd3f2 | 2019-03-18 16:04:04 -0700 | [diff] [blame] | 1380 | "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/" |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1381 | "openbmc_project/state/" |
| 1382 | "host0',arg0='xyz.openbmc_project.State.Host'", |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1383 | eventHandler); |
| 1384 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1385 | int fd = inotify_init(); |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1386 | inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE); |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 1387 | std::array<char, 4096> readBuffer{}; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1388 | // monitor for new i2c devices |
| 1389 | boost::asio::posix::stream_descriptor dirWatch(io, fd); |
| 1390 | std::function<void(const boost::system::error_code, std::size_t)> |
Patrick Williams | b9dd7f8 | 2023-10-20 11:20:11 -0500 | [diff] [blame] | 1391 | watchI2cBusses = [&](const boost::system::error_code& ec, |
| 1392 | std::size_t bytesTransferred) { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1393 | if (ec) |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1394 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1395 | std::cout << "Callback Error " << ec << "\n"; |
| 1396 | return; |
| 1397 | } |
| 1398 | size_t index = 0; |
| 1399 | while ((index + sizeof(inotify_event)) <= bytesTransferred) |
| 1400 | { |
| 1401 | const char* p = &readBuffer[index]; |
| 1402 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 1403 | const auto* iEvent = reinterpret_cast<const inotify_event*>(p); |
| 1404 | switch (iEvent->mask) |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 1405 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1406 | case IN_CREATE: |
| 1407 | case IN_MOVED_TO: |
| 1408 | case IN_DELETE: |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1409 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1410 | std::string_view name(&iEvent->name[0], iEvent->len); |
| 1411 | if (boost::starts_with(name, "i2c")) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1412 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1413 | int bus = busStrToInt(name); |
| 1414 | if (bus < 0) |
| 1415 | { |
| 1416 | std::cerr |
| 1417 | << "Could not parse bus " << name << "\n"; |
| 1418 | continue; |
| 1419 | } |
| 1420 | int rootBus = getRootBus(bus); |
| 1421 | if (rootBus >= 0) |
| 1422 | { |
| 1423 | rescanOneBus(busMap, |
| 1424 | static_cast<uint16_t>(rootBus), |
| 1425 | dbusInterfaceMap, false, |
| 1426 | unknownBusObjectCount, powerIsOn, |
| 1427 | objServer, systemBus); |
| 1428 | } |
| 1429 | rescanOneBus(busMap, static_cast<uint16_t>(bus), |
Kumar Thangavel | 41a9051 | 2021-11-24 15:44:20 +0530 | [diff] [blame] | 1430 | dbusInterfaceMap, false, |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 1431 | unknownBusObjectCount, powerIsOn, |
| 1432 | objServer, systemBus); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1433 | } |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1434 | } |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 1435 | break; |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1436 | default: |
| 1437 | break; |
| 1438 | } |
| 1439 | index += sizeof(inotify_event) + iEvent->len; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1440 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1441 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 1442 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), |
| 1443 | watchI2cBusses); |
| 1444 | }; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1445 | |
| 1446 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses); |
Gunnar Mills | b3e42fe | 2018-06-13 15:48:27 -0500 | [diff] [blame] | 1447 | // run the initial scan |
Kumar Thangavel | 7f43ea8 | 2021-12-07 17:20:16 +0530 | [diff] [blame] | 1448 | rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn, |
| 1449 | objServer, systemBus); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1450 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1451 | io.run(); |
| 1452 | return 0; |
| 1453 | } |