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