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