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