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