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