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