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