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 | */ |
| 16 | |
Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 17 | #include "Utils.hpp" |
| 18 | |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 19 | #include <errno.h> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 20 | #include <fcntl.h> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 21 | #include <sys/inotify.h> |
| 22 | #include <sys/ioctl.h> |
| 23 | |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 24 | #include <boost/algorithm/string/predicate.hpp> |
Brad Bishop | 82c84bb | 2020-08-26 14:12:50 -0400 | [diff] [blame^] | 25 | #include <boost/asio/deadline_timer.hpp> |
| 26 | #include <boost/asio/io_service.hpp> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 27 | #include <boost/container/flat_map.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 28 | #include <nlohmann/json.hpp> |
| 29 | #include <sdbusplus/asio/connection.hpp> |
| 30 | #include <sdbusplus/asio/object_server.hpp> |
| 31 | |
| 32 | #include <array> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 33 | #include <chrono> |
| 34 | #include <ctime> |
Patrick Venture | e375400 | 2019-08-06 09:39:12 -0700 | [diff] [blame] | 35 | #include <filesystem> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 36 | #include <fstream> |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 37 | #include <functional> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 38 | #include <future> |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 39 | #include <iomanip> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 40 | #include <iostream> |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 41 | #include <limits> |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 42 | #include <regex> |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 43 | #include <set> |
| 44 | #include <sstream> |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 45 | #include <string> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 46 | #include <thread> |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 47 | #include <utility> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 48 | #include <variant> |
Patrick Venture | c274f2f | 2019-10-26 09:27:23 -0700 | [diff] [blame] | 49 | #include <vector> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 50 | |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 51 | extern "C" |
| 52 | { |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 53 | #include <i2c/smbus.h> |
| 54 | #include <linux/i2c-dev.h> |
| 55 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 56 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 57 | namespace fs = std::filesystem; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 58 | static constexpr bool DEBUG = false; |
| 59 | static size_t UNKNOWN_BUS_OBJECT_COUNT = 0; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 60 | constexpr size_t MAX_FRU_SIZE = 512; |
| 61 | constexpr size_t MAX_EEPROM_PAGE_INDEX = 255; |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 62 | constexpr size_t busTimeoutSeconds = 5; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 63 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 64 | constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json"; |
| 65 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 66 | const static constexpr char* BASEBOARD_FRU_LOCATION = |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 67 | "/etc/fru/baseboard.fru.bin"; |
| 68 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 69 | const static constexpr char* I2C_DEV_LOCATION = "/dev"; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 70 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 71 | static constexpr std::array<const char*, 5> FRU_AREAS = { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 72 | "INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"}; |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 73 | const static std::regex NON_ASCII_REGEX("[^\x01-\x7f]"); |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 74 | using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 75 | using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>; |
| 76 | |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 77 | static std::set<size_t> busBlacklist; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 78 | struct FindDevicesWithCallback; |
| 79 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 80 | static BusMap busMap; |
| 81 | |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 82 | static bool powerIsOn = false; |
| 83 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 84 | static boost::container::flat_map< |
| 85 | std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 86 | foundDevices; |
| 87 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 88 | static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses; |
| 89 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 90 | boost::asio::io_service io; |
| 91 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 92 | auto objServer = sdbusplus::asio::object_server(systemBus); |
| 93 | |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 94 | static const std::vector<const char*> CHASSIS_FRU_AREAS = { |
| 95 | "PART_NUMBER", "SERIAL_NUMBER", "INFO_AM1", "INFO_AM2"}; |
| 96 | |
| 97 | static const std::vector<const char*> BOARD_FRU_AREAS = { |
| 98 | "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER", |
| 99 | "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"}; |
| 100 | |
| 101 | static const std::vector<const char*> PRODUCT_FRU_AREAS = { |
| 102 | "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER", "VERSION", "SERIAL_NUMBER", |
| 103 | "ASSET_TAG", "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"}; |
| 104 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 105 | bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 106 | bool updateFruProperty( |
| 107 | const std::string& assetTag, uint32_t bus, uint32_t address, |
| 108 | std::string propertyName, |
| 109 | boost::container::flat_map< |
| 110 | std::pair<size_t, size_t>, |
| 111 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 112 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 113 | using ReadBlockFunc = |
| 114 | std::function<int64_t(int flag, int file, uint16_t address, uint16_t offset, |
| 115 | uint8_t length, uint8_t* outBuf)>; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 116 | |
| 117 | // Read and validate FRU contents, given the flag required for raw i2c, the open |
| 118 | // file handle, a read method, and a helper string for failures. |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 119 | std::vector<uint8_t> readFruContents(int flag, int file, uint16_t address, |
| 120 | ReadBlockFunc readBlock, |
| 121 | const std::string& errorHelp) |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 122 | { |
| 123 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
| 124 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 125 | if (readBlock(flag, file, address, 0x0, 0x8, blockData.data()) < 0) |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 126 | { |
| 127 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 128 | return {}; |
| 129 | } |
| 130 | |
| 131 | // check the header checksum |
| 132 | if (!validateHeader(blockData)) |
| 133 | { |
| 134 | if (DEBUG) |
| 135 | { |
| 136 | std::cerr << "Illegal header " << errorHelp << "\n"; |
| 137 | } |
| 138 | |
| 139 | return {}; |
| 140 | } |
| 141 | |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 142 | std::vector<uint8_t> device; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 143 | device.insert(device.end(), blockData.begin(), blockData.begin() + 8); |
| 144 | |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 145 | bool hasMultiRecords = false; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 146 | int fruLength = 0; |
| 147 | for (size_t jj = 1; jj <= FRU_AREAS.size(); jj++) |
| 148 | { |
Patrick Venture | f2ad76b | 2019-10-30 08:49:27 -0700 | [diff] [blame] | 149 | // Offset value can be 255. |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 150 | int areaOffset = device[jj]; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 151 | if (areaOffset == 0) |
| 152 | { |
| 153 | continue; |
| 154 | } |
| 155 | |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 156 | // MultiRecords are different. jj is not tracking section, it's walking |
| 157 | // the common header. |
| 158 | if (std::string(FRU_AREAS[jj - 1]) == std::string("MULTIRECORD")) |
| 159 | { |
| 160 | hasMultiRecords = true; |
| 161 | break; |
| 162 | } |
| 163 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 164 | areaOffset *= 8; |
| 165 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 166 | if (readBlock(flag, file, address, static_cast<uint16_t>(areaOffset), |
| 167 | 0x2, blockData.data()) < 0) |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 168 | { |
| 169 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 170 | return {}; |
| 171 | } |
| 172 | |
Patrick Venture | f2ad76b | 2019-10-30 08:49:27 -0700 | [diff] [blame] | 173 | // Ignore data type (blockData is already unsigned). |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 174 | int length = blockData[1] * 8; |
| 175 | areaOffset += length; |
| 176 | fruLength = (areaOffset > fruLength) ? areaOffset : fruLength; |
| 177 | } |
| 178 | |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 179 | if (hasMultiRecords) |
| 180 | { |
| 181 | // device[area count] is the index to the last area because the 0th |
| 182 | // entry is not an offset in the common header. |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 183 | int areaOffset = device[FRU_AREAS.size()]; |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 184 | areaOffset *= 8; |
| 185 | |
| 186 | // the multi-area record header is 5 bytes long. |
| 187 | constexpr int multiRecordHeaderSize = 5; |
| 188 | constexpr int multiRecordEndOfList = 0x80; |
| 189 | |
| 190 | // Sanity hard-limit to 64KB. |
| 191 | while (areaOffset < std::numeric_limits<uint16_t>::max()) |
| 192 | { |
| 193 | // In multi-area, the area offset points to the 0th record, each |
| 194 | // record has 3 bytes of the header we care about. |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 195 | if (readBlock(flag, file, address, |
| 196 | static_cast<uint16_t>(areaOffset), 0x3, |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 197 | blockData.data()) < 0) |
| 198 | { |
| 199 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 200 | return {}; |
| 201 | } |
| 202 | |
| 203 | // Ok, let's check the record length, which is in bytes (unsigned, |
| 204 | // up to 255, so blockData should hold uint8_t not char) |
| 205 | int recordLength = blockData[2]; |
| 206 | areaOffset += (recordLength + multiRecordHeaderSize); |
| 207 | fruLength = (areaOffset > fruLength) ? areaOffset : fruLength; |
| 208 | |
| 209 | // If this is the end of the list bail. |
| 210 | if ((blockData[1] & multiRecordEndOfList)) |
| 211 | { |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 217 | // You already copied these first 8 bytes (the ipmi fru header size) |
| 218 | fruLength -= 8; |
| 219 | |
| 220 | int readOffset = 8; |
| 221 | |
| 222 | while (fruLength > 0) |
| 223 | { |
| 224 | int requestLength = std::min(I2C_SMBUS_BLOCK_MAX, fruLength); |
| 225 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 226 | if (readBlock(flag, file, address, static_cast<uint16_t>(readOffset), |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 227 | static_cast<uint8_t>(requestLength), |
| 228 | blockData.data()) < 0) |
| 229 | { |
| 230 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 231 | return {}; |
| 232 | } |
| 233 | |
| 234 | device.insert(device.end(), blockData.begin(), |
| 235 | blockData.begin() + requestLength); |
| 236 | |
| 237 | readOffset += requestLength; |
| 238 | fruLength -= requestLength; |
| 239 | } |
| 240 | |
| 241 | return device; |
| 242 | } |
| 243 | |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 244 | // Given a bus/address, produce the path in sysfs for an eeprom. |
| 245 | static std::string getEepromPath(size_t bus, size_t address) |
| 246 | { |
| 247 | std::stringstream output; |
| 248 | output << "/sys/bus/i2c/devices/" << bus << "-" << std::right |
| 249 | << std::setfill('0') << std::setw(4) << std::hex << address |
| 250 | << "/eeprom"; |
| 251 | return output.str(); |
| 252 | } |
| 253 | |
| 254 | static bool hasEepromFile(size_t bus, size_t address) |
| 255 | { |
| 256 | auto path = getEepromPath(bus, address); |
| 257 | try |
| 258 | { |
| 259 | return fs::exists(path); |
| 260 | } |
| 261 | catch (...) |
| 262 | { |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 267 | static int64_t readFromEeprom(int flag __attribute__((unused)), int fd, |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 268 | uint16_t address __attribute__((unused)), |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 269 | uint16_t offset, uint8_t len, uint8_t* buf) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 270 | { |
| 271 | auto result = lseek(fd, offset, SEEK_SET); |
| 272 | if (result < 0) |
| 273 | { |
| 274 | std::cerr << "failed to seek\n"; |
| 275 | return -1; |
| 276 | } |
| 277 | |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 278 | return read(fd, buf, len); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 279 | } |
| 280 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 281 | static int busStrToInt(const std::string& busName) |
| 282 | { |
| 283 | auto findBus = busName.rfind("-"); |
| 284 | if (findBus == std::string::npos) |
| 285 | { |
| 286 | return -1; |
| 287 | } |
| 288 | return std::stoi(busName.substr(findBus + 1)); |
| 289 | } |
| 290 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 291 | static int getRootBus(size_t bus) |
| 292 | { |
| 293 | auto ec = std::error_code(); |
| 294 | auto path = std::filesystem::read_symlink( |
| 295 | std::filesystem::path("/sys/bus/i2c/devices/i2c-" + |
| 296 | std::to_string(bus) + "/mux_device"), |
| 297 | ec); |
| 298 | if (ec) |
| 299 | { |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | std::string filename = path.filename(); |
| 304 | auto findBus = filename.find("-"); |
| 305 | if (findBus == std::string::npos) |
| 306 | { |
| 307 | return -1; |
| 308 | } |
| 309 | return std::stoi(filename.substr(0, findBus)); |
| 310 | } |
| 311 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 312 | static bool isMuxBus(size_t bus) |
| 313 | { |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 314 | return is_symlink(std::filesystem::path( |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 315 | "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device")); |
| 316 | } |
| 317 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 318 | static void makeProbeInterface(size_t bus, size_t address) |
| 319 | { |
| 320 | if (isMuxBus(bus)) |
| 321 | { |
| 322 | return; // the mux buses are random, no need to publish |
| 323 | } |
| 324 | auto [it, success] = foundDevices.emplace( |
| 325 | std::make_pair(bus, address), |
| 326 | objServer.add_interface( |
| 327 | "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" + |
| 328 | std::to_string(address), |
| 329 | "xyz.openbmc_project.Inventory.Item.I2CDevice")); |
| 330 | if (!success) |
| 331 | { |
| 332 | return; // already added |
| 333 | } |
| 334 | it->second->register_property("Bus", bus); |
| 335 | it->second->register_property("Address", address); |
| 336 | it->second->initialize(); |
| 337 | } |
| 338 | |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 339 | static int isDevice16Bit(int file) |
| 340 | { |
| 341 | /* Get first byte */ |
| 342 | int byte1 = i2c_smbus_read_byte_data(file, 0); |
| 343 | if (byte1 < 0) |
| 344 | { |
| 345 | return byte1; |
| 346 | } |
| 347 | /* Read 7 more bytes, it will read same first byte in case of |
| 348 | * 8 bit but it will read next byte in case of 16 bit |
| 349 | */ |
| 350 | for (int i = 0; i < 7; i++) |
| 351 | { |
| 352 | int byte2 = i2c_smbus_read_byte_data(file, 0); |
| 353 | if (byte2 < 0) |
| 354 | { |
| 355 | return byte2; |
| 356 | } |
| 357 | if (byte2 != byte1) |
| 358 | { |
| 359 | return 1; |
| 360 | } |
| 361 | } |
| 362 | return 0; |
| 363 | } |
| 364 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 365 | // Issue an I2C transaction to first write to_slave_buf_len bytes,then read |
| 366 | // from_slave_buf_len bytes. |
| 367 | static int i2c_smbus_write_then_read(int file, uint16_t address, |
| 368 | uint8_t* toSlaveBuf, uint8_t toSlaveBufLen, |
| 369 | uint8_t* fromSlaveBuf, |
| 370 | uint8_t fromSlaveBufLen) |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 371 | { |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 372 | if (toSlaveBuf == NULL || toSlaveBufLen == 0 || fromSlaveBuf == NULL || |
| 373 | fromSlaveBufLen == 0) |
| 374 | { |
| 375 | return -1; |
| 376 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 377 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 378 | #define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2 |
| 379 | struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT]; |
| 380 | struct i2c_rdwr_ioctl_data rdwr; |
| 381 | |
| 382 | msgs[0].addr = address; |
| 383 | msgs[0].flags = 0; |
| 384 | msgs[0].len = toSlaveBufLen; |
| 385 | msgs[0].buf = toSlaveBuf; |
| 386 | msgs[1].addr = address; |
| 387 | msgs[1].flags = I2C_M_RD; |
| 388 | msgs[1].len = fromSlaveBufLen; |
| 389 | msgs[1].buf = fromSlaveBuf; |
| 390 | |
| 391 | rdwr.msgs = msgs; |
| 392 | rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT; |
| 393 | |
| 394 | int ret = ioctl(file, I2C_RDWR, &rdwr); |
| 395 | |
| 396 | return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? ret : -1; |
| 397 | } |
| 398 | |
| 399 | static int64_t readBlockData(int flag, int file, uint16_t address, |
| 400 | uint16_t offset, uint8_t len, uint8_t* buf) |
| 401 | { |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 402 | if (flag == 0) |
| 403 | { |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 404 | return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset), |
| 405 | len, buf); |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 408 | offset = htobe16(offset); |
| 409 | return i2c_smbus_write_then_read( |
| 410 | file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len); |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 411 | } |
| 412 | |
James Feist | 24bae7a | 2019-04-03 09:50:56 -0700 | [diff] [blame] | 413 | bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData) |
| 414 | { |
| 415 | // ipmi spec format version number is currently at 1, verify it |
| 416 | if (blockData[0] != 0x1) |
| 417 | { |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | // verify pad is set to 0 |
| 422 | if (blockData[6] != 0x0) |
| 423 | { |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | // verify offsets are 0, or don't point to another offset |
| 428 | std::set<uint8_t> foundOffsets; |
| 429 | for (int ii = 1; ii < 6; ii++) |
| 430 | { |
| 431 | if (blockData[ii] == 0) |
| 432 | { |
| 433 | continue; |
| 434 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 435 | auto inserted = foundOffsets.insert(blockData[ii]); |
| 436 | if (!inserted.second) |
James Feist | 24bae7a | 2019-04-03 09:50:56 -0700 | [diff] [blame] | 437 | { |
| 438 | return false; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // validate checksum |
| 443 | size_t sum = 0; |
| 444 | for (int jj = 0; jj < 7; jj++) |
| 445 | { |
| 446 | sum += blockData[jj]; |
| 447 | } |
| 448 | sum = (256 - sum) & 0xFF; |
| 449 | |
| 450 | if (sum != blockData[7]) |
| 451 | { |
| 452 | return false; |
| 453 | } |
| 454 | return true; |
| 455 | } |
| 456 | |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 457 | // TODO: This code is very similar to the non-eeprom version and can be merged |
| 458 | // with some tweaks. |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 459 | static std::vector<uint8_t> processEeprom(int bus, int address) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 460 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 461 | auto path = getEepromPath(bus, address); |
| 462 | |
| 463 | int file = open(path.c_str(), O_RDONLY); |
| 464 | if (file < 0) |
| 465 | { |
| 466 | std::cerr << "Unable to open eeprom file: " << path << "\n"; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 467 | return {}; |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 470 | std::string errorMessage = "eeprom at " + std::to_string(bus) + |
| 471 | " address " + std::to_string(address); |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 472 | std::vector<uint8_t> device = readFruContents( |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 473 | 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 474 | |
| 475 | close(file); |
| 476 | return device; |
| 477 | } |
| 478 | |
| 479 | std::set<int> findI2CEeproms(int i2cBus, std::shared_ptr<DeviceMap> devices) |
| 480 | { |
| 481 | std::set<int> foundList; |
| 482 | |
| 483 | std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus); |
| 484 | |
| 485 | // For each file listed under the i2c device |
| 486 | // NOTE: This should be faster than just checking for each possible address |
| 487 | // path. |
| 488 | for (const auto& p : fs::directory_iterator(path)) |
| 489 | { |
| 490 | const std::string node = p.path().string(); |
| 491 | std::smatch m; |
| 492 | bool found = |
| 493 | std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)")); |
| 494 | |
| 495 | if (!found) |
| 496 | { |
| 497 | continue; |
| 498 | } |
| 499 | if (m.size() != 2) |
| 500 | { |
| 501 | std::cerr << "regex didn't capture\n"; |
| 502 | continue; |
| 503 | } |
| 504 | |
| 505 | std::ssub_match subMatch = m[1]; |
| 506 | std::string addressString = subMatch.str(); |
| 507 | |
| 508 | std::size_t ignored; |
| 509 | const int hexBase = 16; |
| 510 | int address = std::stoi(addressString, &ignored, hexBase); |
| 511 | |
| 512 | const std::string eeprom = node + "/eeprom"; |
| 513 | |
| 514 | try |
| 515 | { |
| 516 | if (!fs::exists(eeprom)) |
| 517 | { |
| 518 | continue; |
| 519 | } |
| 520 | } |
| 521 | catch (...) |
| 522 | { |
| 523 | continue; |
| 524 | } |
| 525 | |
| 526 | // There is an eeprom file at this address, it may have invalid |
| 527 | // contents, but we found it. |
| 528 | foundList.insert(address); |
| 529 | |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 530 | std::vector<uint8_t> device = processEeprom(i2cBus, address); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 531 | if (!device.empty()) |
| 532 | { |
| 533 | devices->emplace(address, device); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | return foundList; |
| 538 | } |
| 539 | |
Patrick Venture | 4f47fe6 | 2019-08-08 16:30:38 -0700 | [diff] [blame] | 540 | int getBusFrus(int file, int first, int last, int bus, |
| 541 | std::shared_ptr<DeviceMap> devices) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 542 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 543 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 544 | std::future<int> future = std::async(std::launch::async, [&]() { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 545 | // NOTE: When reading the devices raw on the bus, it can interfere with |
| 546 | // the driver's ability to operate, therefore read eeproms first before |
| 547 | // scanning for devices without drivers. Several experiments were run |
| 548 | // and it was determined that if there were any devices on the bus |
| 549 | // before the eeprom was hit and read, the eeprom driver wouldn't open |
| 550 | // while the bus device was open. An experiment was not performed to see |
| 551 | // if this issue was resolved if the i2c bus device was closed, but |
| 552 | // hexdumps of the eeprom later were successful. |
| 553 | |
| 554 | // Scan for i2c eeproms loaded on this bus. |
| 555 | std::set<int> skipList = findI2CEeproms(bus, devices); |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 556 | std::set<size_t>& failedItems = failedAddresses[bus]; |
| 557 | |
| 558 | std::set<size_t>* rootFailures = nullptr; |
| 559 | int rootBus = getRootBus(bus); |
| 560 | |
| 561 | if (rootBus >= 0) |
| 562 | { |
| 563 | rootFailures = &(failedAddresses[rootBus]); |
| 564 | } |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 565 | |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 566 | constexpr int startSkipSlaveAddr = 0; |
| 567 | constexpr int endSkipSlaveAddr = 12; |
| 568 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 569 | for (int ii = first; ii <= last; ii++) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 570 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 571 | if (skipList.find(ii) != skipList.end()) |
| 572 | { |
| 573 | continue; |
| 574 | } |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 575 | // skipping since no device is present in this range |
| 576 | if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr) |
| 577 | { |
| 578 | continue; |
| 579 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 580 | // Set slave address |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 581 | if (ioctl(file, I2C_SLAVE, ii) < 0) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 582 | { |
Patrick Venture | 98e0cf3 | 2019-08-02 11:11:03 -0700 | [diff] [blame] | 583 | std::cerr << "device at bus " << bus << " register " << ii |
Patrick Venture | 5d8b61d | 2019-08-06 12:36:10 -0700 | [diff] [blame] | 584 | << " busy\n"; |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 585 | continue; |
| 586 | } |
| 587 | // probe |
| 588 | else if (i2c_smbus_read_byte(file) < 0) |
| 589 | { |
| 590 | continue; |
| 591 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 592 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 593 | if (DEBUG) |
| 594 | { |
Patrick Venture | 98e0cf3 | 2019-08-02 11:11:03 -0700 | [diff] [blame] | 595 | std::cout << "something at bus " << bus << " addr " << ii |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 596 | << "\n"; |
| 597 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 598 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 599 | makeProbeInterface(bus, ii); |
| 600 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 601 | if (failedItems.find(ii) != failedItems.end()) |
| 602 | { |
| 603 | // if we failed to read it once, unlikely we can read it later |
| 604 | continue; |
| 605 | } |
| 606 | |
| 607 | if (rootFailures != nullptr) |
| 608 | { |
| 609 | if (rootFailures->find(ii) != rootFailures->end()) |
| 610 | { |
| 611 | continue; |
| 612 | } |
| 613 | } |
| 614 | |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 615 | /* Check for Device type if it is 8 bit or 16 bit */ |
| 616 | int flag = isDevice16Bit(file); |
| 617 | if (flag < 0) |
| 618 | { |
| 619 | std::cerr << "failed to read bus " << bus << " address " << ii |
| 620 | << "\n"; |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 621 | if (powerIsOn) |
| 622 | { |
| 623 | failedItems.insert(ii); |
| 624 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 625 | continue; |
| 626 | } |
| 627 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 628 | std::string errorMessage = |
| 629 | "bus " + std::to_string(bus) + " address " + std::to_string(ii); |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 630 | std::vector<uint8_t> device = |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 631 | readFruContents(flag, file, static_cast<uint16_t>(ii), |
| 632 | readBlockData, errorMessage); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 633 | if (device.empty()) |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 634 | { |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 635 | continue; |
| 636 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 637 | |
Patrick Venture | 786f179 | 2019-08-05 16:33:44 -0700 | [diff] [blame] | 638 | devices->emplace(ii, device); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 639 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 640 | return 1; |
| 641 | }); |
| 642 | std::future_status status = |
| 643 | future.wait_for(std::chrono::seconds(busTimeoutSeconds)); |
| 644 | if (status == std::future_status::timeout) |
| 645 | { |
| 646 | std::cerr << "Error reading bus " << bus << "\n"; |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 647 | if (powerIsOn) |
| 648 | { |
| 649 | busBlacklist.insert(bus); |
| 650 | } |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 651 | close(file); |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 652 | return -1; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 653 | } |
| 654 | |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 655 | close(file); |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 656 | return future.get(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 659 | void loadBlacklist(const char* path) |
| 660 | { |
| 661 | std::ifstream blacklistStream(path); |
| 662 | if (!blacklistStream.good()) |
| 663 | { |
| 664 | // File is optional. |
| 665 | std::cerr << "Cannot open blacklist file.\n\n"; |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | nlohmann::json data = |
| 670 | nlohmann::json::parse(blacklistStream, nullptr, false); |
| 671 | if (data.is_discarded()) |
| 672 | { |
| 673 | std::cerr << "Illegal blacklist file detected, cannot validate JSON, " |
| 674 | "exiting\n"; |
| 675 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | // It's expected to have at least one field, "buses" that is an array of the |
| 679 | // buses by integer. Allow for future options to exclude further aspects, |
| 680 | // such as specific addresses or ranges. |
| 681 | if (data.type() != nlohmann::json::value_t::object) |
| 682 | { |
| 683 | std::cerr << "Illegal blacklist, expected to read dictionary\n"; |
| 684 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | // If buses field is missing, that's fine. |
| 688 | if (data.count("buses") == 1) |
| 689 | { |
| 690 | // Parse the buses array after a little validation. |
| 691 | auto buses = data.at("buses"); |
| 692 | if (buses.type() != nlohmann::json::value_t::array) |
| 693 | { |
| 694 | // Buses field present but invalid, therefore this is an error. |
| 695 | std::cerr << "Invalid contents for blacklist buses field\n"; |
| 696 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | // Catch exception here for type mis-match. |
| 700 | try |
| 701 | { |
| 702 | for (const auto& bus : buses) |
| 703 | { |
| 704 | busBlacklist.insert(bus.get<size_t>()); |
| 705 | } |
| 706 | } |
| 707 | catch (const nlohmann::detail::type_error& e) |
| 708 | { |
| 709 | // Type mis-match is a critical error. |
| 710 | std::cerr << "Invalid bus type: " << e.what() << "\n"; |
| 711 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
| 715 | return; |
| 716 | } |
| 717 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 718 | static void FindI2CDevices(const std::vector<fs::path>& i2cBuses, |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 719 | BusMap& busmap) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 720 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 721 | for (auto& i2cBus : i2cBuses) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 722 | { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 723 | int bus = busStrToInt(i2cBus); |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 724 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 725 | if (bus < 0) |
| 726 | { |
| 727 | std::cerr << "Cannot translate " << i2cBus << " to int\n"; |
| 728 | continue; |
| 729 | } |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 730 | if (busBlacklist.find(bus) != busBlacklist.end()) |
| 731 | { |
| 732 | continue; // skip previously failed busses |
| 733 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 734 | |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 735 | int rootBus = getRootBus(bus); |
| 736 | if (busBlacklist.find(rootBus) != busBlacklist.end()) |
| 737 | { |
| 738 | continue; |
| 739 | } |
| 740 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 741 | auto file = open(i2cBus.c_str(), O_RDWR); |
| 742 | if (file < 0) |
| 743 | { |
| 744 | std::cerr << "unable to open i2c device " << i2cBus.string() |
| 745 | << "\n"; |
| 746 | continue; |
| 747 | } |
| 748 | unsigned long funcs = 0; |
| 749 | |
| 750 | if (ioctl(file, I2C_FUNCS, &funcs) < 0) |
| 751 | { |
| 752 | std::cerr |
Patrick Venture | 98e0cf3 | 2019-08-02 11:11:03 -0700 | [diff] [blame] | 753 | << "Error: Could not get the adapter functionality matrix bus " |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 754 | << bus << "\n"; |
| 755 | continue; |
| 756 | } |
| 757 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) || |
| 758 | !(I2C_FUNC_SMBUS_READ_I2C_BLOCK)) |
| 759 | { |
| 760 | std::cerr << "Error: Can't use SMBus Receive Byte command bus " |
| 761 | << bus << "\n"; |
| 762 | continue; |
| 763 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 764 | auto& device = busmap[bus]; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 765 | device = std::make_shared<DeviceMap>(); |
| 766 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 767 | // i2cdetect by default uses the range 0x03 to 0x77, as |
| 768 | // this is what we have tested with, use this range. Could be |
| 769 | // changed in future. |
| 770 | if (DEBUG) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 771 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 772 | std::cerr << "Scanning bus " << bus << "\n"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 773 | } |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 774 | |
| 775 | // fd is closed in this function in case the bus locks up |
Patrick Venture | 4f47fe6 | 2019-08-08 16:30:38 -0700 | [diff] [blame] | 776 | getBusFrus(file, 0x03, 0x77, bus, device); |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 777 | |
| 778 | if (DEBUG) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 779 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 780 | std::cerr << "Done scanning bus " << bus << "\n"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 781 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 782 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 783 | } |
| 784 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 785 | // this class allows an async response after all i2c devices are discovered |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 786 | struct FindDevicesWithCallback : |
| 787 | std::enable_shared_from_this<FindDevicesWithCallback> |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 788 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 789 | FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 790 | BusMap& busmap, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 791 | std::function<void(void)>&& callback) : |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 792 | _i2cBuses(i2cBuses), |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 793 | _busMap(busmap), _callback(std::move(callback)) |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 794 | {} |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 795 | ~FindDevicesWithCallback() |
| 796 | { |
| 797 | _callback(); |
| 798 | } |
| 799 | void run() |
| 800 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 801 | FindI2CDevices(_i2cBuses, _busMap); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 802 | } |
| 803 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 804 | const std::vector<fs::path>& _i2cBuses; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 805 | BusMap& _busMap; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 806 | std::function<void(void)> _callback; |
| 807 | }; |
| 808 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 809 | static const std::tm intelEpoch(void) |
| 810 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 811 | std::tm val = {}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 812 | val.tm_year = 1996 - 1900; |
| 813 | return val; |
| 814 | } |
| 815 | |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 816 | static char sixBitToChar(uint8_t val) |
| 817 | { |
| 818 | return static_cast<char>((val & 0x3f) + ' '); |
| 819 | } |
| 820 | |
| 821 | /* 0xd - 0xf are reserved values, but not fatal; use a placeholder char. */ |
| 822 | static const char bcdHighChars[] = { |
| 823 | ' ', '-', '.', 'X', 'X', 'X', |
| 824 | }; |
| 825 | |
| 826 | static char bcdPlusToChar(uint8_t val) |
| 827 | { |
| 828 | val &= 0xf; |
| 829 | return (val < 10) ? static_cast<char>(val + '0') : bcdHighChars[val - 10]; |
| 830 | } |
| 831 | |
| 832 | enum class DecodeState |
| 833 | { |
| 834 | ok, |
| 835 | end, |
| 836 | err, |
| 837 | }; |
| 838 | |
| 839 | enum FRUDataEncoding |
| 840 | { |
| 841 | binary = 0x0, |
| 842 | bcdPlus = 0x1, |
| 843 | sixBitASCII = 0x2, |
| 844 | languageDependent = 0x3, |
| 845 | }; |
| 846 | |
| 847 | /* Decode FRU data into a std::string, given an input iterator and end. If the |
| 848 | * state returned is fruDataOk, then the resulting string is the decoded FRU |
| 849 | * data. The input iterator is advanced past the data consumed. |
| 850 | * |
| 851 | * On fruDataErr, we have lost synchronisation with the length bytes, so the |
| 852 | * iterator is no longer usable. |
| 853 | */ |
| 854 | static std::pair<DecodeState, std::string> |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 855 | decodeFruData(std::vector<uint8_t>::const_iterator& iter, |
| 856 | const std::vector<uint8_t>::const_iterator& end) |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 857 | { |
| 858 | std::string value; |
| 859 | unsigned int i; |
| 860 | |
| 861 | /* we need at least one byte to decode the type/len header */ |
| 862 | if (iter == end) |
| 863 | { |
| 864 | std::cerr << "Truncated FRU data\n"; |
| 865 | return make_pair(DecodeState::err, value); |
| 866 | } |
| 867 | |
| 868 | uint8_t c = *(iter++); |
| 869 | |
| 870 | /* 0xc1 is the end marker */ |
| 871 | if (c == 0xc1) |
| 872 | { |
| 873 | return make_pair(DecodeState::end, value); |
| 874 | } |
| 875 | |
| 876 | /* decode type/len byte */ |
| 877 | uint8_t type = static_cast<uint8_t>(c >> 6); |
| 878 | uint8_t len = static_cast<uint8_t>(c & 0x3f); |
| 879 | |
| 880 | /* we should have at least len bytes of data available overall */ |
| 881 | if (iter + len > end) |
| 882 | { |
| 883 | std::cerr << "FRU data field extends past end of FRU data\n"; |
| 884 | return make_pair(DecodeState::err, value); |
| 885 | } |
| 886 | |
| 887 | switch (type) |
| 888 | { |
| 889 | case FRUDataEncoding::binary: |
| 890 | case FRUDataEncoding::languageDependent: |
| 891 | /* For language-code dependent encodings, assume 8-bit ASCII */ |
| 892 | value = std::string(iter, iter + len); |
| 893 | iter += len; |
| 894 | break; |
| 895 | |
| 896 | case FRUDataEncoding::bcdPlus: |
| 897 | value = std::string(); |
| 898 | for (i = 0; i < len; i++, iter++) |
| 899 | { |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 900 | uint8_t val = *iter; |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 901 | value.push_back(bcdPlusToChar(val >> 4)); |
| 902 | value.push_back(bcdPlusToChar(val & 0xf)); |
| 903 | } |
| 904 | break; |
| 905 | |
| 906 | case FRUDataEncoding::sixBitASCII: |
| 907 | { |
Andrei Kartashev | e707de6 | 2020-08-10 18:33:29 +0300 | [diff] [blame] | 908 | unsigned int accum = 0; |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 909 | unsigned int accumBitLen = 0; |
| 910 | value = std::string(); |
| 911 | for (i = 0; i < len; i++, iter++) |
| 912 | { |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 913 | accum |= *iter << accumBitLen; |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 914 | accumBitLen += 8; |
| 915 | while (accumBitLen >= 6) |
| 916 | { |
| 917 | value.push_back(sixBitToChar(accum & 0x3f)); |
| 918 | accum >>= 6; |
| 919 | accumBitLen -= 6; |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | break; |
| 924 | } |
| 925 | |
| 926 | return make_pair(DecodeState::ok, value); |
| 927 | } |
| 928 | |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 929 | bool formatFru(const std::vector<uint8_t>& fruBytes, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 930 | boost::container::flat_map<std::string, std::string>& result) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 931 | { |
James Feist | d068e93 | 2018-09-20 10:53:07 -0700 | [diff] [blame] | 932 | if (fruBytes.size() <= 8) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 933 | { |
| 934 | return false; |
| 935 | } |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 936 | std::vector<uint8_t>::const_iterator fruAreaOffsetField = fruBytes.begin(); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 937 | result["Common_Format_Version"] = |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 938 | std::to_string(static_cast<int>(*fruAreaOffsetField)); |
| 939 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 940 | const std::vector<const char*>* fieldData; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 941 | |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 942 | for (const std::string& area : FRU_AREAS) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 943 | { |
Patrick Venture | 4b7a745 | 2019-10-28 08:41:02 -0700 | [diff] [blame] | 944 | ++fruAreaOffsetField; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 945 | if (fruAreaOffsetField >= fruBytes.end()) |
| 946 | { |
| 947 | return false; |
| 948 | } |
| 949 | size_t offset = (*fruAreaOffsetField) * 8; |
| 950 | |
| 951 | if (offset > 1) |
| 952 | { |
| 953 | // +2 to skip format and length |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 954 | std::vector<uint8_t>::const_iterator fruBytesIter = |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 955 | fruBytes.begin() + offset + 2; |
| 956 | |
| 957 | if (fruBytesIter >= fruBytes.end()) |
| 958 | { |
| 959 | return false; |
| 960 | } |
| 961 | |
| 962 | if (area == "CHASSIS") |
| 963 | { |
| 964 | result["CHASSIS_TYPE"] = |
| 965 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 966 | fruBytesIter += 1; |
| 967 | fieldData = &CHASSIS_FRU_AREAS; |
| 968 | } |
| 969 | else if (area == "BOARD") |
| 970 | { |
| 971 | result["BOARD_LANGUAGE_CODE"] = |
| 972 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 973 | fruBytesIter += 1; |
| 974 | if (fruBytesIter >= fruBytes.end()) |
| 975 | { |
| 976 | return false; |
| 977 | } |
| 978 | |
| 979 | unsigned int minutes = *fruBytesIter | |
| 980 | *(fruBytesIter + 1) << 8 | |
| 981 | *(fruBytesIter + 2) << 16; |
| 982 | std::tm fruTime = intelEpoch(); |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 983 | std::time_t timeValue = std::mktime(&fruTime); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 984 | timeValue += minutes * 60; |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 985 | fruTime = *std::gmtime(&timeValue); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 986 | |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 987 | // Tue Nov 20 23:08:00 2018 |
| 988 | char timeString[32] = {0}; |
| 989 | auto bytes = std::strftime(timeString, sizeof(timeString), |
Patrick Venture | fff050a | 2019-08-13 11:44:30 -0700 | [diff] [blame] | 990 | "%Y-%m-%d - %H:%M:%S", &fruTime); |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 991 | if (bytes == 0) |
| 992 | { |
| 993 | std::cerr << "invalid time string encountered\n"; |
| 994 | return false; |
| 995 | } |
| 996 | |
| 997 | result["BOARD_MANUFACTURE_DATE"] = std::string(timeString); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 998 | fruBytesIter += 3; |
| 999 | fieldData = &BOARD_FRU_AREAS; |
| 1000 | } |
| 1001 | else if (area == "PRODUCT") |
| 1002 | { |
| 1003 | result["PRODUCT_LANGUAGE_CODE"] = |
| 1004 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 1005 | fruBytesIter += 1; |
| 1006 | fieldData = &PRODUCT_FRU_AREAS; |
| 1007 | } |
| 1008 | else |
| 1009 | { |
| 1010 | continue; |
| 1011 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1012 | for (auto& field : *fieldData) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1013 | { |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 1014 | auto res = decodeFruData(fruBytesIter, fruBytes.end()); |
| 1015 | std::string name = area + "_" + field; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1016 | |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 1017 | if (res.first == DecodeState::end) |
Vijay Khemka | 5d5de44 | 2018-11-07 10:51:25 -0800 | [diff] [blame] | 1018 | { |
| 1019 | break; |
| 1020 | } |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 1021 | else if (res.first == DecodeState::err) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1022 | { |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 1023 | std::cerr << "Error while parsing " << name << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1024 | return false; |
| 1025 | } |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 1026 | |
| 1027 | std::string value = res.second; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1028 | |
Ed Tanous | 2147e67 | 2019-02-27 13:59:56 -0800 | [diff] [blame] | 1029 | // Strip non null characters from the end |
| 1030 | value.erase(std::find_if(value.rbegin(), value.rend(), |
| 1031 | [](char ch) { return ch != 0; }) |
| 1032 | .base(), |
| 1033 | value.end()); |
| 1034 | |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 1035 | result[name] = std::move(value); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | return true; |
| 1041 | } |
| 1042 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1043 | std::vector<uint8_t>& getFruInfo(const uint8_t& bus, const uint8_t& address) |
| 1044 | { |
| 1045 | auto deviceMap = busMap.find(bus); |
| 1046 | if (deviceMap == busMap.end()) |
| 1047 | { |
| 1048 | throw std::invalid_argument("Invalid Bus."); |
| 1049 | } |
| 1050 | auto device = deviceMap->second->find(address); |
| 1051 | if (device == deviceMap->second->end()) |
| 1052 | { |
| 1053 | throw std::invalid_argument("Invalid Address."); |
| 1054 | } |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1055 | std::vector<uint8_t>& ret = device->second; |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1056 | |
| 1057 | return ret; |
| 1058 | } |
| 1059 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1060 | void AddFruObjectToDbus( |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1061 | std::vector<uint8_t>& device, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1062 | boost::container::flat_map< |
| 1063 | std::pair<size_t, size_t>, |
| 1064 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 1065 | uint32_t bus, uint32_t address) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1066 | { |
| 1067 | boost::container::flat_map<std::string, std::string> formattedFru; |
| 1068 | if (!formatFru(device, formattedFru)) |
| 1069 | { |
Patrick Venture | b755c83 | 2019-08-07 11:09:14 -0700 | [diff] [blame] | 1070 | std::cerr << "failed to format fru for device at bus " << bus |
| 1071 | << " address " << address << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1072 | return; |
| 1073 | } |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1074 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1075 | auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME"); |
| 1076 | std::string productName; |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1077 | // Not found under Board section or an empty string. |
| 1078 | if (productNameFind == formattedFru.end() || |
| 1079 | productNameFind->second.empty()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1080 | { |
| 1081 | productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME"); |
| 1082 | } |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1083 | // Found under Product section and not an empty string. |
| 1084 | if (productNameFind != formattedFru.end() && |
| 1085 | !productNameFind->second.empty()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1086 | { |
| 1087 | productName = productNameFind->second; |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 1088 | std::regex illegalObject("[^A-Za-z0-9_]"); |
| 1089 | productName = std::regex_replace(productName, illegalObject, "_"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1090 | } |
| 1091 | else |
| 1092 | { |
| 1093 | productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT); |
| 1094 | UNKNOWN_BUS_OBJECT_COUNT++; |
| 1095 | } |
| 1096 | |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1097 | productName = "/xyz/openbmc_project/FruDevice/" + productName; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1098 | // avoid duplicates by checking to see if on a mux |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1099 | if (bus > 0) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1100 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1101 | int highest = -1; |
| 1102 | bool found = false; |
| 1103 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1104 | for (auto const& busIface : dbusInterfaceMap) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1105 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1106 | std::string path = busIface.second->get_object_path(); |
| 1107 | if (std::regex_match(path, std::regex(productName + "(_\\d+|)$"))) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1108 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1109 | if (isMuxBus(bus) && address == busIface.first.second && |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1110 | (getFruInfo(static_cast<uint8_t>(busIface.first.first), |
| 1111 | static_cast<uint8_t>(busIface.first.second)) == |
| 1112 | getFruInfo(static_cast<uint8_t>(bus), |
| 1113 | static_cast<uint8_t>(address)))) |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1114 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1115 | // This device is already added to the lower numbered bus, |
| 1116 | // do not replicate it. |
| 1117 | return; |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1118 | } |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1119 | |
| 1120 | // Check if the match named has extra information. |
| 1121 | found = true; |
| 1122 | std::smatch base_match; |
| 1123 | |
| 1124 | bool match = std::regex_match( |
| 1125 | path, base_match, std::regex(productName + "_(\\d+)$")); |
| 1126 | if (match) |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1127 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1128 | if (base_match.size() == 2) |
| 1129 | { |
| 1130 | std::ssub_match base_sub_match = base_match[1]; |
| 1131 | std::string base = base_sub_match.str(); |
| 1132 | |
| 1133 | int value = std::stoi(base); |
| 1134 | highest = (value > highest) ? value : highest; |
| 1135 | } |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1136 | } |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1137 | } |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1138 | } // end searching objects |
| 1139 | |
| 1140 | if (found) |
| 1141 | { |
| 1142 | // We found something with the same name. If highest was still -1, |
| 1143 | // it means this new entry will be _0. |
| 1144 | productName += "_"; |
| 1145 | productName += std::to_string(++highest); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1146 | } |
| 1147 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1148 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1149 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 1150 | objServer.add_interface(productName, "xyz.openbmc_project.FruDevice"); |
| 1151 | dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface; |
| 1152 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1153 | for (auto& property : formattedFru) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1154 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1155 | |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 1156 | std::regex_replace(property.second.begin(), property.second.begin(), |
| 1157 | property.second.end(), NON_ASCII_REGEX, "_"); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1158 | if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG") |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1159 | { |
| 1160 | continue; |
| 1161 | } |
| 1162 | std::string key = |
| 1163 | std::regex_replace(property.first, NON_ASCII_REGEX, "_"); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1164 | |
| 1165 | if (property.first == "PRODUCT_ASSET_TAG") |
| 1166 | { |
| 1167 | std::string propertyName = property.first; |
| 1168 | iface->register_property( |
| 1169 | key, property.second + '\0', |
| 1170 | [bus, address, propertyName, |
| 1171 | &dbusInterfaceMap](const std::string& req, std::string& resp) { |
| 1172 | if (strcmp(req.c_str(), resp.c_str())) |
| 1173 | { |
| 1174 | // call the method which will update |
| 1175 | if (updateFruProperty(req, bus, address, propertyName, |
| 1176 | dbusInterfaceMap)) |
| 1177 | { |
| 1178 | resp = req; |
| 1179 | } |
| 1180 | else |
| 1181 | { |
| 1182 | throw std::invalid_argument( |
| 1183 | "Fru property update failed."); |
| 1184 | } |
| 1185 | } |
| 1186 | return 1; |
| 1187 | }); |
| 1188 | } |
| 1189 | else if (!iface->register_property(key, property.second + '\0')) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1190 | { |
| 1191 | std::cerr << "illegal key: " << key << "\n"; |
| 1192 | } |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 1193 | if (DEBUG) |
| 1194 | { |
| 1195 | std::cout << property.first << ": " << property.second << "\n"; |
| 1196 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1197 | } |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1198 | |
| 1199 | // baseboard will be 0, 0 |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 1200 | iface->register_property("BUS", bus); |
| 1201 | iface->register_property("ADDRESS", address); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1202 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1203 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1206 | static bool readBaseboardFru(std::vector<uint8_t>& baseboardFru) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1207 | { |
| 1208 | // try to read baseboard fru from file |
| 1209 | std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary); |
| 1210 | if (baseboardFruFile.good()) |
| 1211 | { |
| 1212 | baseboardFruFile.seekg(0, std::ios_base::end); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1213 | size_t fileSize = static_cast<size_t>(baseboardFruFile.tellg()); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1214 | baseboardFru.resize(fileSize); |
| 1215 | baseboardFruFile.seekg(0, std::ios_base::beg); |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1216 | baseboardFruFile.read(reinterpret_cast<char*>(baseboardFru.data()), |
| 1217 | fileSize); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1218 | } |
| 1219 | else |
| 1220 | { |
| 1221 | return false; |
| 1222 | } |
| 1223 | return true; |
| 1224 | } |
| 1225 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1226 | 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] | 1227 | { |
| 1228 | boost::container::flat_map<std::string, std::string> tmp; |
| 1229 | if (fru.size() > MAX_FRU_SIZE) |
| 1230 | { |
| 1231 | std::cerr << "Invalid fru.size() during writeFru\n"; |
| 1232 | return false; |
| 1233 | } |
| 1234 | // verify legal fru by running it through fru parsing logic |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1235 | if (!formatFru(fru, tmp)) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1236 | { |
| 1237 | std::cerr << "Invalid fru format during writeFru\n"; |
| 1238 | return false; |
| 1239 | } |
| 1240 | // baseboard fru |
| 1241 | if (bus == 0 && address == 0) |
| 1242 | { |
| 1243 | std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary); |
| 1244 | if (!file.good()) |
| 1245 | { |
| 1246 | std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION |
| 1247 | << "\n"; |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1248 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1249 | return false; |
| 1250 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1251 | file.write(reinterpret_cast<const char*>(fru.data()), fru.size()); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1252 | return file.good(); |
| 1253 | } |
| 1254 | else |
| 1255 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 1256 | if (hasEepromFile(bus, address)) |
| 1257 | { |
| 1258 | auto path = getEepromPath(bus, address); |
| 1259 | int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC); |
| 1260 | if (eeprom < 0) |
| 1261 | { |
| 1262 | std::cerr << "unable to open i2c device " << path << "\n"; |
| 1263 | throw DBusInternalError(); |
| 1264 | return false; |
| 1265 | } |
| 1266 | |
| 1267 | ssize_t writtenBytes = write(eeprom, fru.data(), fru.size()); |
| 1268 | if (writtenBytes < 0) |
| 1269 | { |
| 1270 | std::cerr << "unable to write to i2c device " << path << "\n"; |
| 1271 | close(eeprom); |
| 1272 | throw DBusInternalError(); |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
| 1276 | close(eeprom); |
| 1277 | return true; |
| 1278 | } |
| 1279 | |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1280 | std::string i2cBus = "/dev/i2c-" + std::to_string(bus); |
| 1281 | |
| 1282 | int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC); |
| 1283 | if (file < 0) |
| 1284 | { |
| 1285 | std::cerr << "unable to open i2c device " << i2cBus << "\n"; |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1286 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1287 | return false; |
| 1288 | } |
| 1289 | if (ioctl(file, I2C_SLAVE_FORCE, address) < 0) |
| 1290 | { |
| 1291 | std::cerr << "unable to set device address\n"; |
| 1292 | close(file); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1293 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1294 | return false; |
| 1295 | } |
| 1296 | |
| 1297 | constexpr const size_t RETRY_MAX = 2; |
| 1298 | uint16_t index = 0; |
| 1299 | size_t retries = RETRY_MAX; |
| 1300 | while (index < fru.size()) |
| 1301 | { |
| 1302 | if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) && |
| 1303 | (retries == RETRY_MAX)) |
| 1304 | { |
| 1305 | // The 4K EEPROM only uses the A2 and A1 device address bits |
| 1306 | // with the third bit being a memory page address bit. |
| 1307 | if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0) |
| 1308 | { |
| 1309 | std::cerr << "unable to set device address\n"; |
| 1310 | close(file); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1311 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1312 | return false; |
| 1313 | } |
| 1314 | } |
| 1315 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1316 | if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index), |
| 1317 | fru[index]) < 0) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1318 | { |
| 1319 | if (!retries--) |
| 1320 | { |
| 1321 | std::cerr << "error writing fru: " << strerror(errno) |
| 1322 | << "\n"; |
| 1323 | close(file); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1324 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1325 | return false; |
| 1326 | } |
| 1327 | } |
| 1328 | else |
| 1329 | { |
| 1330 | retries = RETRY_MAX; |
| 1331 | index++; |
| 1332 | } |
| 1333 | // most eeproms require 5-10ms between writes |
| 1334 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 1335 | } |
| 1336 | close(file); |
| 1337 | return true; |
| 1338 | } |
| 1339 | } |
| 1340 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1341 | void rescanOneBus( |
| 1342 | BusMap& busmap, uint8_t busNum, |
| 1343 | boost::container::flat_map< |
| 1344 | std::pair<size_t, size_t>, |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1345 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
| 1346 | bool dbusCall) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1347 | { |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1348 | for (auto& [pair, interface] : foundDevices) |
| 1349 | { |
| 1350 | if (pair.first == static_cast<size_t>(busNum)) |
| 1351 | { |
| 1352 | objServer.remove_interface(interface); |
| 1353 | foundDevices.erase(pair); |
| 1354 | } |
| 1355 | } |
| 1356 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1357 | fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum)); |
| 1358 | if (!fs::exists(busPath)) |
| 1359 | { |
| 1360 | if (dbusCall) |
| 1361 | { |
| 1362 | std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum) |
| 1363 | << "\n"; |
| 1364 | throw std::invalid_argument("Invalid Bus."); |
| 1365 | } |
| 1366 | return; |
| 1367 | } |
| 1368 | |
| 1369 | std::vector<fs::path> i2cBuses; |
| 1370 | i2cBuses.emplace_back(busPath); |
| 1371 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1372 | auto scan = std::make_shared<FindDevicesWithCallback>( |
| 1373 | i2cBuses, busmap, [busNum, &busmap, &dbusInterfaceMap]() { |
| 1374 | for (auto& busIface : dbusInterfaceMap) |
| 1375 | { |
| 1376 | if (busIface.first.first == static_cast<size_t>(busNum)) |
| 1377 | { |
| 1378 | objServer.remove_interface(busIface.second); |
| 1379 | } |
| 1380 | } |
Wludzik, Jozef | c1136b7 | 2020-06-24 11:58:32 +0200 | [diff] [blame] | 1381 | auto found = busmap.find(busNum); |
| 1382 | if (found == busmap.end() || found->second == nullptr) |
| 1383 | { |
| 1384 | return; |
| 1385 | } |
| 1386 | for (auto& device : *(found->second)) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1387 | { |
| 1388 | AddFruObjectToDbus(device.second, dbusInterfaceMap, |
| 1389 | static_cast<uint32_t>(busNum), device.first); |
| 1390 | } |
| 1391 | }); |
| 1392 | scan->run(); |
| 1393 | } |
| 1394 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1395 | void rescanBusses( |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1396 | BusMap& busmap, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1397 | boost::container::flat_map< |
| 1398 | std::pair<size_t, size_t>, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1399 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1400 | { |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1401 | static boost::asio::deadline_timer timer(io); |
| 1402 | timer.expires_from_now(boost::posix_time::seconds(1)); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1403 | |
Gunnar Mills | 6f0ae94 | 2018-08-31 12:38:03 -0500 | [diff] [blame] | 1404 | // setup an async wait in case we get flooded with requests |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1405 | timer.async_wait([&](const boost::system::error_code&) { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1406 | auto devDir = fs::path("/dev/"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1407 | std::vector<fs::path> i2cBuses; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1408 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1409 | boost::container::flat_map<size_t, fs::path> busPaths; |
| 1410 | if (!getI2cDevicePaths(devDir, busPaths)) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1411 | { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1412 | std::cerr << "unable to find i2c devices\n"; |
| 1413 | return; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1414 | } |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1415 | |
| 1416 | for (auto busPath : busPaths) |
| 1417 | { |
| 1418 | i2cBuses.emplace_back(busPath.second); |
| 1419 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1420 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1421 | busmap.clear(); |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 1422 | for (auto& [pair, interface] : foundDevices) |
| 1423 | { |
| 1424 | objServer.remove_interface(interface); |
| 1425 | } |
| 1426 | foundDevices.clear(); |
| 1427 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1428 | auto scan = |
| 1429 | std::make_shared<FindDevicesWithCallback>(i2cBuses, busmap, [&]() { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1430 | for (auto& busIface : dbusInterfaceMap) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1431 | { |
| 1432 | objServer.remove_interface(busIface.second); |
| 1433 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1434 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1435 | dbusInterfaceMap.clear(); |
| 1436 | UNKNOWN_BUS_OBJECT_COUNT = 0; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1437 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1438 | // todo, get this from a more sensable place |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1439 | std::vector<uint8_t> baseboardFru; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1440 | if (readBaseboardFru(baseboardFru)) |
| 1441 | { |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1442 | boost::container::flat_map<int, std::vector<uint8_t>> |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1443 | baseboardDev; |
| 1444 | baseboardDev.emplace(0, baseboardFru); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1445 | busmap[0] = std::make_shared<DeviceMap>(baseboardDev); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1446 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1447 | for (auto& devicemap : busmap) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1448 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1449 | for (auto& device : *devicemap.second) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1450 | { |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1451 | AddFruObjectToDbus(device.second, dbusInterfaceMap, |
| 1452 | devicemap.first, device.first); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | }); |
| 1456 | scan->run(); |
| 1457 | }); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1458 | } |
| 1459 | |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1460 | // Calculate new checksum for fru info area |
| 1461 | size_t calculateChecksum(std::vector<uint8_t>& fruAreaData) |
| 1462 | { |
| 1463 | constexpr int checksumMod = 256; |
| 1464 | constexpr uint8_t modVal = 0xFF; |
| 1465 | int sum = std::accumulate(fruAreaData.begin(), fruAreaData.end(), 0); |
| 1466 | return (checksumMod - sum) & modVal; |
| 1467 | } |
| 1468 | |
| 1469 | // Update new fru area length & |
| 1470 | // Update checksum at new checksum location |
| 1471 | static void updateFruAreaLenAndChecksum(std::vector<uint8_t>& fruData, |
| 1472 | size_t fruAreaStartOffset, |
| 1473 | size_t fruAreaNoMoreFieldOffset) |
| 1474 | { |
| 1475 | constexpr size_t fruAreaMultipleBytes = 8; |
| 1476 | size_t traverseFruAreaIndex = fruAreaNoMoreFieldOffset - fruAreaStartOffset; |
| 1477 | |
| 1478 | // fill zeros for any remaining unused space |
| 1479 | std::fill(fruData.begin() + fruAreaNoMoreFieldOffset, fruData.end(), 0); |
| 1480 | |
| 1481 | size_t mod = traverseFruAreaIndex % fruAreaMultipleBytes; |
| 1482 | size_t checksumLoc; |
| 1483 | if (!mod) |
| 1484 | { |
| 1485 | traverseFruAreaIndex += (fruAreaMultipleBytes); |
| 1486 | checksumLoc = fruAreaNoMoreFieldOffset + (fruAreaMultipleBytes - 1); |
| 1487 | } |
| 1488 | else |
| 1489 | { |
| 1490 | traverseFruAreaIndex += (fruAreaMultipleBytes - mod); |
| 1491 | checksumLoc = |
| 1492 | fruAreaNoMoreFieldOffset + (fruAreaMultipleBytes - mod - 1); |
| 1493 | } |
| 1494 | |
| 1495 | size_t newFruAreaLen = (traverseFruAreaIndex / fruAreaMultipleBytes) + |
| 1496 | ((traverseFruAreaIndex % fruAreaMultipleBytes) != 0); |
| 1497 | size_t fruAreaLengthLoc = fruAreaStartOffset + 1; |
| 1498 | fruData[fruAreaLengthLoc] = static_cast<uint8_t>(newFruAreaLen); |
| 1499 | |
| 1500 | // Calculate new checksum |
| 1501 | std::vector<uint8_t> finalFruData; |
| 1502 | std::copy_n(fruData.begin() + fruAreaStartOffset, |
| 1503 | checksumLoc - fruAreaStartOffset, |
| 1504 | std::back_inserter(finalFruData)); |
| 1505 | |
| 1506 | size_t checksumVal = calculateChecksum(finalFruData); |
| 1507 | |
| 1508 | fruData[checksumLoc] = static_cast<uint8_t>(checksumVal); |
| 1509 | } |
| 1510 | |
| 1511 | static size_t getTypeLength(uint8_t fruFieldTypeLenValue) |
| 1512 | { |
| 1513 | constexpr uint8_t typeLenMask = 0x3F; |
| 1514 | return fruFieldTypeLenValue & typeLenMask; |
| 1515 | } |
| 1516 | |
| 1517 | // Details with example of Asset Tag Update |
| 1518 | // To find location of Product Info Area asset tag as per FRU specification |
| 1519 | // 1. Find product Info area starting offset (*8 - as header will be in |
| 1520 | // multiple of 8 bytes). |
| 1521 | // 2. Skip 3 bytes of product info area (like format version, area length, |
| 1522 | // and language code). |
| 1523 | // 3. Traverse manufacturer name, product name, product version, & product |
| 1524 | // serial number, by reading type/length code to reach the Asset Tag. |
| 1525 | // 4. Update the Asset Tag, reposition the product Info area in multiple of |
| 1526 | // 8 bytes. Update the Product area length and checksum. |
| 1527 | |
| 1528 | bool updateFruProperty( |
| 1529 | const std::string& updatePropertyReq, uint32_t bus, uint32_t address, |
| 1530 | std::string propertyName, |
| 1531 | boost::container::flat_map< |
| 1532 | std::pair<size_t, size_t>, |
| 1533 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap) |
| 1534 | { |
| 1535 | size_t updatePropertyReqLen = updatePropertyReq.length(); |
| 1536 | if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63) |
| 1537 | { |
| 1538 | std::cerr |
| 1539 | << "Fru field data cannot be of 1 char or more than 63 chars. " |
| 1540 | "Invalid Length " |
| 1541 | << updatePropertyReqLen << "\n"; |
| 1542 | return false; |
| 1543 | } |
| 1544 | |
| 1545 | std::vector<uint8_t> fruData; |
| 1546 | try |
| 1547 | { |
| 1548 | fruData = getFruInfo(static_cast<uint8_t>(bus), |
| 1549 | static_cast<uint8_t>(address)); |
| 1550 | } |
| 1551 | catch (std::invalid_argument& e) |
| 1552 | { |
| 1553 | std::cerr << "Failure getting Fru Info" << e.what() << "\n"; |
| 1554 | return false; |
| 1555 | } |
| 1556 | |
| 1557 | if (fruData.empty()) |
| 1558 | { |
| 1559 | return false; |
| 1560 | } |
| 1561 | |
| 1562 | const std::vector<const char*>* fieldData; |
| 1563 | |
| 1564 | constexpr size_t commonHeaderMultipleBytes = 8; // in multiple of 8 bytes |
| 1565 | uint8_t fruAreaOffsetFieldValue = 1; |
| 1566 | size_t offset = 0; |
| 1567 | |
| 1568 | if (propertyName.find("CHASSIS") == 0) |
| 1569 | { |
| 1570 | constexpr size_t fruAreaOffsetField = 2; |
| 1571 | fruAreaOffsetFieldValue = fruData[fruAreaOffsetField]; |
| 1572 | |
| 1573 | offset = 3; // chassis part number offset. Skip fixed first 3 bytes |
| 1574 | |
| 1575 | fieldData = &CHASSIS_FRU_AREAS; |
| 1576 | } |
| 1577 | else if (propertyName.find("BOARD") == 0) |
| 1578 | { |
| 1579 | constexpr size_t fruAreaOffsetField = 3; |
| 1580 | fruAreaOffsetFieldValue = fruData[fruAreaOffsetField]; |
| 1581 | |
| 1582 | offset = 6; // board manufacturer offset. Skip fixed first 6 bytes |
| 1583 | |
| 1584 | fieldData = &BOARD_FRU_AREAS; |
| 1585 | } |
| 1586 | else if (propertyName.find("PRODUCT") == 0) |
| 1587 | { |
| 1588 | constexpr size_t fruAreaOffsetField = 4; |
| 1589 | fruAreaOffsetFieldValue = fruData[fruAreaOffsetField]; |
| 1590 | |
| 1591 | offset = 3; |
| 1592 | // Manufacturer name offset. Skip fixed first 3 product fru bytes i.e. |
| 1593 | // version, area length and language code |
| 1594 | |
| 1595 | fieldData = &PRODUCT_FRU_AREAS; |
| 1596 | } |
| 1597 | else |
| 1598 | { |
| 1599 | std::cerr << "ProperyName doesn't exist in Fru Area Vector \n"; |
| 1600 | return false; |
| 1601 | } |
| 1602 | |
| 1603 | size_t fruAreaStartOffset = |
| 1604 | fruAreaOffsetFieldValue * commonHeaderMultipleBytes; |
| 1605 | size_t fruDataIter = fruAreaStartOffset + offset; |
| 1606 | size_t fruUpdateFieldLoc = 0; |
| 1607 | size_t typeLength; |
| 1608 | size_t skipToFruUpdateField = 0; |
| 1609 | |
| 1610 | for (auto& field : *fieldData) |
| 1611 | { |
| 1612 | skipToFruUpdateField++; |
| 1613 | if (propertyName.find(field) != std::string::npos) |
| 1614 | { |
| 1615 | break; |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | for (size_t i = 1; i < skipToFruUpdateField; i++) |
| 1620 | { |
| 1621 | typeLength = getTypeLength(fruData[fruDataIter]); |
| 1622 | fruUpdateFieldLoc = fruDataIter + typeLength; |
| 1623 | fruDataIter = ++fruUpdateFieldLoc; |
| 1624 | } |
| 1625 | |
| 1626 | // Push post update fru field bytes to a vector |
| 1627 | typeLength = getTypeLength(fruData[fruUpdateFieldLoc]); |
| 1628 | size_t postFruUpdateFieldLoc = fruUpdateFieldLoc + typeLength; |
| 1629 | postFruUpdateFieldLoc++; |
| 1630 | skipToFruUpdateField++; |
| 1631 | fruDataIter = postFruUpdateFieldLoc; |
| 1632 | size_t endLengthLoc = 0; |
| 1633 | size_t fruFieldLoc = 0; |
| 1634 | constexpr uint8_t endLength = 0xC1; |
| 1635 | for (size_t i = fruDataIter; i < fruData.size(); i++) |
| 1636 | { |
| 1637 | typeLength = getTypeLength(fruData[fruDataIter]); |
| 1638 | fruFieldLoc = fruDataIter + typeLength; |
| 1639 | fruDataIter = ++fruFieldLoc; |
| 1640 | if (fruData[fruDataIter] == endLength) |
| 1641 | { |
| 1642 | endLengthLoc = fruDataIter; |
| 1643 | break; |
| 1644 | } |
| 1645 | } |
| 1646 | endLengthLoc++; |
| 1647 | |
| 1648 | std::vector<uint8_t> postUpdatedFruData; |
| 1649 | std::copy_n(fruData.begin() + postFruUpdateFieldLoc, |
| 1650 | endLengthLoc - postFruUpdateFieldLoc, |
| 1651 | std::back_inserter(postUpdatedFruData)); |
| 1652 | |
| 1653 | // write new requested property field length and data |
| 1654 | constexpr uint8_t newTypeLenMask = 0xC0; |
| 1655 | fruData[fruUpdateFieldLoc] = |
| 1656 | static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask); |
| 1657 | fruUpdateFieldLoc++; |
| 1658 | std::copy(updatePropertyReq.begin(), updatePropertyReq.end(), |
| 1659 | fruData.begin() + fruUpdateFieldLoc); |
| 1660 | |
| 1661 | // Copy remaining data to main fru area - post updated fru field vector |
| 1662 | postFruUpdateFieldLoc = fruUpdateFieldLoc + updatePropertyReqLen; |
| 1663 | size_t fruAreaEndOffset = postFruUpdateFieldLoc + postUpdatedFruData.size(); |
| 1664 | if (fruAreaEndOffset > fruData.size()) |
| 1665 | { |
| 1666 | std::cerr << "Fru area offset: " << fruAreaEndOffset |
| 1667 | << "should not be greater than Fru data size: " |
| 1668 | << fruData.size() << std::endl; |
| 1669 | throw std::invalid_argument( |
| 1670 | "Fru area offset should not be greater than Fru data size"); |
| 1671 | } |
| 1672 | std::copy(postUpdatedFruData.begin(), postUpdatedFruData.end(), |
| 1673 | fruData.begin() + postFruUpdateFieldLoc); |
| 1674 | |
| 1675 | // Update final fru with new fru area length and checksum |
| 1676 | updateFruAreaLenAndChecksum(fruData, fruAreaStartOffset, fruAreaEndOffset); |
| 1677 | |
| 1678 | if (fruData.empty()) |
| 1679 | { |
| 1680 | return false; |
| 1681 | } |
| 1682 | |
| 1683 | if (!writeFru(static_cast<uint8_t>(bus), static_cast<uint8_t>(address), |
| 1684 | fruData)) |
| 1685 | { |
| 1686 | return false; |
| 1687 | } |
| 1688 | |
| 1689 | // Rescan the bus so that GetRawFru dbus-call fetches updated values |
| 1690 | rescanBusses(busMap, dbusInterfaceMap); |
| 1691 | return true; |
| 1692 | } |
| 1693 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1694 | int main() |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1695 | { |
| 1696 | auto devDir = fs::path("/dev/"); |
James Feist | c9dff1b | 2019-02-13 13:33:13 -0800 | [diff] [blame] | 1697 | auto matchString = std::string(R"(i2c-\d+$)"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1698 | std::vector<fs::path> i2cBuses; |
| 1699 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 1700 | if (!findFiles(devDir, matchString, i2cBuses)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1701 | { |
| 1702 | std::cerr << "unable to find i2c devices\n"; |
| 1703 | return 1; |
| 1704 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1705 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 1706 | // check for and load blacklist with initial buses. |
| 1707 | loadBlacklist(blacklistPath); |
| 1708 | |
Vijay Khemka | 065f6d9 | 2018-12-18 10:37:47 -0800 | [diff] [blame] | 1709 | systemBus->request_name("xyz.openbmc_project.FruDevice"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1710 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1711 | // this is a map with keys of pair(bus number, address) and values of |
| 1712 | // the object on dbus |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1713 | boost::container::flat_map<std::pair<size_t, size_t>, |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1714 | std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 1715 | dbusInterfaceMap; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1716 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1717 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 1718 | objServer.add_interface("/xyz/openbmc_project/FruDevice", |
| 1719 | "xyz.openbmc_project.FruDeviceManager"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1720 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1721 | iface->register_method("ReScan", |
| 1722 | [&]() { rescanBusses(busMap, dbusInterfaceMap); }); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1723 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1724 | iface->register_method("ReScanBus", [&](uint8_t bus) { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1725 | rescanOneBus(busMap, bus, dbusInterfaceMap, true); |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1726 | }); |
| 1727 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1728 | iface->register_method("GetRawFru", getFruInfo); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1729 | |
| 1730 | iface->register_method("WriteFru", [&](const uint8_t bus, |
| 1731 | const uint8_t address, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1732 | const std::vector<uint8_t>& data) { |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1733 | if (!writeFru(bus, address, data)) |
| 1734 | { |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1735 | throw std::invalid_argument("Invalid Arguments."); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1736 | return; |
| 1737 | } |
| 1738 | // schedule rescan on success |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1739 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1740 | }); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1741 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1742 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1743 | std::function<void(sdbusplus::message::message & message)> eventHandler = |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1744 | [&](sdbusplus::message::message& message) { |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1745 | std::string objectName; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1746 | boost::container::flat_map< |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1747 | std::string, |
| 1748 | std::variant<std::string, bool, int64_t, uint64_t, double>> |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1749 | values; |
| 1750 | message.read(objectName, values); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1751 | auto findState = values.find("CurrentHostState"); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1752 | if (findState != values.end()) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1753 | { |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 1754 | powerIsOn = boost::ends_with( |
| 1755 | std::get<std::string>(findState->second), "Running"); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1756 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1757 | |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 1758 | if (powerIsOn) |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1759 | { |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1760 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1761 | } |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1762 | }; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1763 | |
| 1764 | sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1765 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
James Feist | 7bcd3f2 | 2019-03-18 16:04:04 -0700 | [diff] [blame] | 1766 | "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/" |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1767 | "openbmc_project/state/" |
| 1768 | "host0',arg0='xyz.openbmc_project.State.Host'", |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1769 | eventHandler); |
| 1770 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1771 | int fd = inotify_init(); |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 1772 | inotify_add_watch(fd, I2C_DEV_LOCATION, |
| 1773 | IN_CREATE | IN_MOVED_TO | IN_DELETE); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1774 | std::array<char, 4096> readBuffer; |
| 1775 | std::string pendingBuffer; |
| 1776 | // monitor for new i2c devices |
| 1777 | boost::asio::posix::stream_descriptor dirWatch(io, fd); |
| 1778 | std::function<void(const boost::system::error_code, std::size_t)> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1779 | watchI2cBusses = [&](const boost::system::error_code& ec, |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1780 | std::size_t bytes_transferred) { |
| 1781 | if (ec) |
| 1782 | { |
| 1783 | std::cout << "Callback Error " << ec << "\n"; |
| 1784 | return; |
| 1785 | } |
| 1786 | pendingBuffer += std::string(readBuffer.data(), bytes_transferred); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1787 | while (pendingBuffer.size() > sizeof(inotify_event)) |
| 1788 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1789 | const inotify_event* iEvent = |
| 1790 | reinterpret_cast<const inotify_event*>( |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1791 | pendingBuffer.data()); |
| 1792 | switch (iEvent->mask) |
| 1793 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1794 | case IN_CREATE: |
| 1795 | case IN_MOVED_TO: |
| 1796 | case IN_DELETE: |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1797 | std::string name(iEvent->name); |
| 1798 | if (boost::starts_with(name, "i2c")) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1799 | { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1800 | int bus = busStrToInt(name); |
| 1801 | if (bus < 0) |
| 1802 | { |
| 1803 | std::cerr << "Could not parse bus " << name |
| 1804 | << "\n"; |
| 1805 | continue; |
| 1806 | } |
| 1807 | rescanOneBus(busMap, static_cast<uint8_t>(bus), |
| 1808 | dbusInterfaceMap, false); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1809 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len); |
| 1813 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1814 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1815 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), |
| 1816 | watchI2cBusses); |
| 1817 | }; |
| 1818 | |
| 1819 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses); |
Gunnar Mills | b3e42fe | 2018-06-13 15:48:27 -0500 | [diff] [blame] | 1820 | // run the initial scan |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1821 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1822 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1823 | io.run(); |
| 1824 | return 0; |
| 1825 | } |