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