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