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 | } |
| 653 | auto bus = std::stoi(busnum); |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 654 | if (busBlacklist.find(bus) != busBlacklist.end()) |
| 655 | { |
| 656 | continue; // skip previously failed busses |
| 657 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 658 | |
| 659 | auto file = open(i2cBus.c_str(), O_RDWR); |
| 660 | if (file < 0) |
| 661 | { |
| 662 | std::cerr << "unable to open i2c device " << i2cBus.string() |
| 663 | << "\n"; |
| 664 | continue; |
| 665 | } |
| 666 | unsigned long funcs = 0; |
| 667 | |
| 668 | if (ioctl(file, I2C_FUNCS, &funcs) < 0) |
| 669 | { |
| 670 | std::cerr |
Patrick Venture | 98e0cf3 | 2019-08-02 11:11:03 -0700 | [diff] [blame] | 671 | << "Error: Could not get the adapter functionality matrix bus " |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 672 | << bus << "\n"; |
| 673 | continue; |
| 674 | } |
| 675 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) || |
| 676 | !(I2C_FUNC_SMBUS_READ_I2C_BLOCK)) |
| 677 | { |
| 678 | std::cerr << "Error: Can't use SMBus Receive Byte command bus " |
| 679 | << bus << "\n"; |
| 680 | continue; |
| 681 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 682 | auto& device = busmap[bus]; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 683 | device = std::make_shared<DeviceMap>(); |
| 684 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 685 | // i2cdetect by default uses the range 0x03 to 0x77, as |
| 686 | // this is what we have tested with, use this range. Could be |
| 687 | // changed in future. |
| 688 | if (DEBUG) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 689 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 690 | std::cerr << "Scanning bus " << bus << "\n"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 691 | } |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 692 | |
| 693 | // fd is closed in this function in case the bus locks up |
Patrick Venture | 4f47fe6 | 2019-08-08 16:30:38 -0700 | [diff] [blame] | 694 | getBusFrus(file, 0x03, 0x77, bus, device); |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 695 | |
| 696 | if (DEBUG) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 697 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 698 | std::cerr << "Done scanning bus " << bus << "\n"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 699 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 700 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 701 | } |
| 702 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 703 | // this class allows an async response after all i2c devices are discovered |
| 704 | struct FindDevicesWithCallback |
| 705 | : std::enable_shared_from_this<FindDevicesWithCallback> |
| 706 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 707 | FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 708 | BusMap& busmap, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 709 | std::function<void(void)>&& callback) : |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 710 | _i2cBuses(i2cBuses), |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 711 | _busMap(busmap), _callback(std::move(callback)) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 712 | { |
| 713 | } |
| 714 | ~FindDevicesWithCallback() |
| 715 | { |
| 716 | _callback(); |
| 717 | } |
| 718 | void run() |
| 719 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 720 | FindI2CDevices(_i2cBuses, _busMap); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 721 | } |
| 722 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 723 | const std::vector<fs::path>& _i2cBuses; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 724 | BusMap& _busMap; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 725 | std::function<void(void)> _callback; |
| 726 | }; |
| 727 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 728 | static const std::tm intelEpoch(void) |
| 729 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 730 | std::tm val = {}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 731 | val.tm_year = 1996 - 1900; |
| 732 | return val; |
| 733 | } |
| 734 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 735 | bool formatFru(const std::vector<char>& fruBytes, |
| 736 | boost::container::flat_map<std::string, std::string>& result) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 737 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 738 | static const std::vector<const char*> CHASSIS_FRU_AREAS = { |
Vijay Khemka | 5d5de44 | 2018-11-07 10:51:25 -0800 | [diff] [blame] | 739 | "PART_NUMBER", "SERIAL_NUMBER", "INFO_AM1", "INFO_AM2"}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 740 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 741 | static const std::vector<const char*> BOARD_FRU_AREAS = { |
Vijay Khemka | 5d5de44 | 2018-11-07 10:51:25 -0800 | [diff] [blame] | 742 | "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER", |
| 743 | "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"}; |
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*> PRODUCT_FRU_AREAS = { |
Vijay Khemka | 5d5de44 | 2018-11-07 10:51:25 -0800 | [diff] [blame] | 746 | "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER", |
| 747 | "VERSION", "SERIAL_NUMBER", "ASSET_TAG", |
| 748 | "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 749 | |
James Feist | d068e93 | 2018-09-20 10:53:07 -0700 | [diff] [blame] | 750 | if (fruBytes.size() <= 8) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 751 | { |
| 752 | return false; |
| 753 | } |
| 754 | std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin(); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 755 | result["Common_Format_Version"] = |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 756 | std::to_string(static_cast<int>(*fruAreaOffsetField)); |
| 757 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 758 | const std::vector<const char*>* fieldData; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 759 | |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 760 | for (const std::string& area : FRU_AREAS) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 761 | { |
Patrick Venture | 4b7a745 | 2019-10-28 08:41:02 -0700 | [diff] [blame] | 762 | ++fruAreaOffsetField; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 763 | if (fruAreaOffsetField >= fruBytes.end()) |
| 764 | { |
| 765 | return false; |
| 766 | } |
| 767 | size_t offset = (*fruAreaOffsetField) * 8; |
| 768 | |
| 769 | if (offset > 1) |
| 770 | { |
| 771 | // +2 to skip format and length |
| 772 | std::vector<char>::const_iterator fruBytesIter = |
| 773 | fruBytes.begin() + offset + 2; |
| 774 | |
| 775 | if (fruBytesIter >= fruBytes.end()) |
| 776 | { |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | if (area == "CHASSIS") |
| 781 | { |
| 782 | result["CHASSIS_TYPE"] = |
| 783 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 784 | fruBytesIter += 1; |
| 785 | fieldData = &CHASSIS_FRU_AREAS; |
| 786 | } |
| 787 | else if (area == "BOARD") |
| 788 | { |
| 789 | result["BOARD_LANGUAGE_CODE"] = |
| 790 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 791 | fruBytesIter += 1; |
| 792 | if (fruBytesIter >= fruBytes.end()) |
| 793 | { |
| 794 | return false; |
| 795 | } |
| 796 | |
| 797 | unsigned int minutes = *fruBytesIter | |
| 798 | *(fruBytesIter + 1) << 8 | |
| 799 | *(fruBytesIter + 2) << 16; |
| 800 | std::tm fruTime = intelEpoch(); |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 801 | std::time_t timeValue = std::mktime(&fruTime); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 802 | timeValue += minutes * 60; |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 803 | fruTime = *std::gmtime(&timeValue); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 804 | |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 805 | // Tue Nov 20 23:08:00 2018 |
| 806 | char timeString[32] = {0}; |
| 807 | auto bytes = std::strftime(timeString, sizeof(timeString), |
Patrick Venture | fff050a | 2019-08-13 11:44:30 -0700 | [diff] [blame] | 808 | "%Y-%m-%d - %H:%M:%S", &fruTime); |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 809 | if (bytes == 0) |
| 810 | { |
| 811 | std::cerr << "invalid time string encountered\n"; |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | result["BOARD_MANUFACTURE_DATE"] = std::string(timeString); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 816 | fruBytesIter += 3; |
| 817 | fieldData = &BOARD_FRU_AREAS; |
| 818 | } |
| 819 | else if (area == "PRODUCT") |
| 820 | { |
| 821 | result["PRODUCT_LANGUAGE_CODE"] = |
| 822 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 823 | fruBytesIter += 1; |
| 824 | fieldData = &PRODUCT_FRU_AREAS; |
| 825 | } |
| 826 | else |
| 827 | { |
| 828 | continue; |
| 829 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 830 | for (auto& field : *fieldData) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 831 | { |
| 832 | if (fruBytesIter >= fruBytes.end()) |
| 833 | { |
| 834 | return false; |
| 835 | } |
| 836 | |
Vijay Khemka | 5d5de44 | 2018-11-07 10:51:25 -0800 | [diff] [blame] | 837 | /* Checking for last byte C1 to indicate that no more |
| 838 | * field to be read */ |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 839 | if (static_cast<uint8_t>(*fruBytesIter) == 0xC1) |
Vijay Khemka | 5d5de44 | 2018-11-07 10:51:25 -0800 | [diff] [blame] | 840 | { |
| 841 | break; |
| 842 | } |
| 843 | |
Ed Tanous | 2147e67 | 2019-02-27 13:59:56 -0800 | [diff] [blame] | 844 | size_t length = *fruBytesIter & 0x3f; |
| 845 | fruBytesIter += 1; |
| 846 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 847 | if (fruBytesIter >= fruBytes.end()) |
| 848 | { |
| 849 | return false; |
| 850 | } |
Ed Tanous | 2147e67 | 2019-02-27 13:59:56 -0800 | [diff] [blame] | 851 | std::string value(fruBytesIter, fruBytesIter + length); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 852 | |
Ed Tanous | 2147e67 | 2019-02-27 13:59:56 -0800 | [diff] [blame] | 853 | // Strip non null characters from the end |
| 854 | value.erase(std::find_if(value.rbegin(), value.rend(), |
| 855 | [](char ch) { return ch != 0; }) |
| 856 | .base(), |
| 857 | value.end()); |
| 858 | |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 859 | result[area + "_" + field] = std::move(value); |
Ed Tanous | 2147e67 | 2019-02-27 13:59:56 -0800 | [diff] [blame] | 860 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 861 | fruBytesIter += length; |
| 862 | if (fruBytesIter >= fruBytes.end()) |
| 863 | { |
| 864 | std::cerr << "Warning Fru Length Mismatch:\n "; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 865 | for (auto& c : fruBytes) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 866 | { |
| 867 | std::cerr << c; |
| 868 | } |
| 869 | std::cerr << "\n"; |
| 870 | if (DEBUG) |
| 871 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 872 | for (auto& keyPair : result) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 873 | { |
| 874 | std::cerr << keyPair.first << " : " |
| 875 | << keyPair.second << "\n"; |
| 876 | } |
| 877 | } |
| 878 | return false; |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | return true; |
| 885 | } |
| 886 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 887 | std::vector<uint8_t>& getFruInfo(const uint8_t& bus, const uint8_t& address) |
| 888 | { |
| 889 | auto deviceMap = busMap.find(bus); |
| 890 | if (deviceMap == busMap.end()) |
| 891 | { |
| 892 | throw std::invalid_argument("Invalid Bus."); |
| 893 | } |
| 894 | auto device = deviceMap->second->find(address); |
| 895 | if (device == deviceMap->second->end()) |
| 896 | { |
| 897 | throw std::invalid_argument("Invalid Address."); |
| 898 | } |
| 899 | std::vector<uint8_t>& ret = |
| 900 | reinterpret_cast<std::vector<uint8_t>&>(device->second); |
| 901 | |
| 902 | return ret; |
| 903 | } |
| 904 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 905 | void AddFruObjectToDbus( |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 906 | std::vector<char>& device, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 907 | boost::container::flat_map< |
| 908 | std::pair<size_t, size_t>, |
| 909 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 910 | uint32_t bus, uint32_t address) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 911 | { |
| 912 | boost::container::flat_map<std::string, std::string> formattedFru; |
| 913 | if (!formatFru(device, formattedFru)) |
| 914 | { |
Patrick Venture | b755c83 | 2019-08-07 11:09:14 -0700 | [diff] [blame] | 915 | std::cerr << "failed to format fru for device at bus " << bus |
| 916 | << " address " << address << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 917 | return; |
| 918 | } |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 919 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 920 | auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME"); |
| 921 | std::string productName; |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 922 | // Not found under Board section or an empty string. |
| 923 | if (productNameFind == formattedFru.end() || |
| 924 | productNameFind->second.empty()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 925 | { |
| 926 | productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME"); |
| 927 | } |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 928 | // Found under Product section and not an empty string. |
| 929 | if (productNameFind != formattedFru.end() && |
| 930 | !productNameFind->second.empty()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 931 | { |
| 932 | productName = productNameFind->second; |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 933 | std::regex illegalObject("[^A-Za-z0-9_]"); |
| 934 | productName = std::regex_replace(productName, illegalObject, "_"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 935 | } |
| 936 | else |
| 937 | { |
| 938 | productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT); |
| 939 | UNKNOWN_BUS_OBJECT_COUNT++; |
| 940 | } |
| 941 | |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 942 | productName = "/xyz/openbmc_project/FruDevice/" + productName; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 943 | // avoid duplicates by checking to see if on a mux |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 944 | if (bus > 0) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 945 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 946 | int highest = -1; |
| 947 | bool found = false; |
| 948 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 949 | for (auto const& busIface : dbusInterfaceMap) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 950 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 951 | std::string path = busIface.second->get_object_path(); |
| 952 | if (std::regex_match(path, std::regex(productName + "(_\\d+|)$"))) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 953 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 954 | if (isMuxBus(bus) && address == busIface.first.second && |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 955 | (getFruInfo(static_cast<uint8_t>(busIface.first.first), |
| 956 | static_cast<uint8_t>(busIface.first.second)) == |
| 957 | getFruInfo(static_cast<uint8_t>(bus), |
| 958 | static_cast<uint8_t>(address)))) |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 959 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 960 | // This device is already added to the lower numbered bus, |
| 961 | // do not replicate it. |
| 962 | return; |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 963 | } |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 964 | |
| 965 | // Check if the match named has extra information. |
| 966 | found = true; |
| 967 | std::smatch base_match; |
| 968 | |
| 969 | bool match = std::regex_match( |
| 970 | path, base_match, std::regex(productName + "_(\\d+)$")); |
| 971 | if (match) |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 972 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 973 | if (base_match.size() == 2) |
| 974 | { |
| 975 | std::ssub_match base_sub_match = base_match[1]; |
| 976 | std::string base = base_sub_match.str(); |
| 977 | |
| 978 | int value = std::stoi(base); |
| 979 | highest = (value > highest) ? value : highest; |
| 980 | } |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 981 | } |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 982 | } |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 983 | } // end searching objects |
| 984 | |
| 985 | if (found) |
| 986 | { |
| 987 | // We found something with the same name. If highest was still -1, |
| 988 | // it means this new entry will be _0. |
| 989 | productName += "_"; |
| 990 | productName += std::to_string(++highest); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 991 | } |
| 992 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 993 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 994 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 995 | objServer.add_interface(productName, "xyz.openbmc_project.FruDevice"); |
| 996 | dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface; |
| 997 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 998 | for (auto& property : formattedFru) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 999 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1000 | |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 1001 | std::regex_replace(property.second.begin(), property.second.begin(), |
| 1002 | property.second.end(), NON_ASCII_REGEX, "_"); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1003 | if (property.second.empty()) |
| 1004 | { |
| 1005 | continue; |
| 1006 | } |
| 1007 | std::string key = |
| 1008 | std::regex_replace(property.first, NON_ASCII_REGEX, "_"); |
| 1009 | if (!iface->register_property(key, property.second + '\0')) |
| 1010 | { |
| 1011 | std::cerr << "illegal key: " << key << "\n"; |
| 1012 | } |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 1013 | if (DEBUG) |
| 1014 | { |
| 1015 | std::cout << property.first << ": " << property.second << "\n"; |
| 1016 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1017 | } |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1018 | |
| 1019 | // baseboard will be 0, 0 |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 1020 | iface->register_property("BUS", bus); |
| 1021 | iface->register_property("ADDRESS", address); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1022 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1023 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1026 | static bool readBaseboardFru(std::vector<char>& baseboardFru) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1027 | { |
| 1028 | // try to read baseboard fru from file |
| 1029 | std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary); |
| 1030 | if (baseboardFruFile.good()) |
| 1031 | { |
| 1032 | baseboardFruFile.seekg(0, std::ios_base::end); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1033 | size_t fileSize = static_cast<size_t>(baseboardFruFile.tellg()); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1034 | baseboardFru.resize(fileSize); |
| 1035 | baseboardFruFile.seekg(0, std::ios_base::beg); |
| 1036 | baseboardFruFile.read(baseboardFru.data(), fileSize); |
| 1037 | } |
| 1038 | else |
| 1039 | { |
| 1040 | return false; |
| 1041 | } |
| 1042 | return true; |
| 1043 | } |
| 1044 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1045 | 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] | 1046 | { |
| 1047 | boost::container::flat_map<std::string, std::string> tmp; |
| 1048 | if (fru.size() > MAX_FRU_SIZE) |
| 1049 | { |
| 1050 | std::cerr << "Invalid fru.size() during writeFru\n"; |
| 1051 | return false; |
| 1052 | } |
| 1053 | // verify legal fru by running it through fru parsing logic |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1054 | if (!formatFru(reinterpret_cast<const std::vector<char>&>(fru), tmp)) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1055 | { |
| 1056 | std::cerr << "Invalid fru format during writeFru\n"; |
| 1057 | return false; |
| 1058 | } |
| 1059 | // baseboard fru |
| 1060 | if (bus == 0 && address == 0) |
| 1061 | { |
| 1062 | std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary); |
| 1063 | if (!file.good()) |
| 1064 | { |
| 1065 | std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION |
| 1066 | << "\n"; |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1067 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1068 | return false; |
| 1069 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1070 | file.write(reinterpret_cast<const char*>(fru.data()), fru.size()); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1071 | return file.good(); |
| 1072 | } |
| 1073 | else |
| 1074 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 1075 | if (hasEepromFile(bus, address)) |
| 1076 | { |
| 1077 | auto path = getEepromPath(bus, address); |
| 1078 | int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC); |
| 1079 | if (eeprom < 0) |
| 1080 | { |
| 1081 | std::cerr << "unable to open i2c device " << path << "\n"; |
| 1082 | throw DBusInternalError(); |
| 1083 | return false; |
| 1084 | } |
| 1085 | |
| 1086 | ssize_t writtenBytes = write(eeprom, fru.data(), fru.size()); |
| 1087 | if (writtenBytes < 0) |
| 1088 | { |
| 1089 | std::cerr << "unable to write to i2c device " << path << "\n"; |
| 1090 | close(eeprom); |
| 1091 | throw DBusInternalError(); |
| 1092 | return false; |
| 1093 | } |
| 1094 | |
| 1095 | close(eeprom); |
| 1096 | return true; |
| 1097 | } |
| 1098 | |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1099 | std::string i2cBus = "/dev/i2c-" + std::to_string(bus); |
| 1100 | |
| 1101 | int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC); |
| 1102 | if (file < 0) |
| 1103 | { |
| 1104 | std::cerr << "unable to open i2c device " << i2cBus << "\n"; |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1105 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1106 | return false; |
| 1107 | } |
| 1108 | if (ioctl(file, I2C_SLAVE_FORCE, address) < 0) |
| 1109 | { |
| 1110 | std::cerr << "unable to set device address\n"; |
| 1111 | close(file); |
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 | |
| 1116 | constexpr const size_t RETRY_MAX = 2; |
| 1117 | uint16_t index = 0; |
| 1118 | size_t retries = RETRY_MAX; |
| 1119 | while (index < fru.size()) |
| 1120 | { |
| 1121 | if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) && |
| 1122 | (retries == RETRY_MAX)) |
| 1123 | { |
| 1124 | // The 4K EEPROM only uses the A2 and A1 device address bits |
| 1125 | // with the third bit being a memory page address bit. |
| 1126 | if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0) |
| 1127 | { |
| 1128 | std::cerr << "unable to set device address\n"; |
| 1129 | close(file); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1130 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1131 | return false; |
| 1132 | } |
| 1133 | } |
| 1134 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1135 | if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index), |
| 1136 | fru[index]) < 0) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1137 | { |
| 1138 | if (!retries--) |
| 1139 | { |
| 1140 | std::cerr << "error writing fru: " << strerror(errno) |
| 1141 | << "\n"; |
| 1142 | close(file); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1143 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1144 | return false; |
| 1145 | } |
| 1146 | } |
| 1147 | else |
| 1148 | { |
| 1149 | retries = RETRY_MAX; |
| 1150 | index++; |
| 1151 | } |
| 1152 | // most eeproms require 5-10ms between writes |
| 1153 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 1154 | } |
| 1155 | close(file); |
| 1156 | return true; |
| 1157 | } |
| 1158 | } |
| 1159 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1160 | void rescanBusses( |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1161 | BusMap& busmap, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1162 | boost::container::flat_map< |
| 1163 | std::pair<size_t, size_t>, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1164 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1165 | { |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1166 | static boost::asio::deadline_timer timer(io); |
| 1167 | timer.expires_from_now(boost::posix_time::seconds(1)); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1168 | |
Gunnar Mills | 6f0ae94 | 2018-08-31 12:38:03 -0500 | [diff] [blame] | 1169 | // setup an async wait in case we get flooded with requests |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1170 | timer.async_wait([&](const boost::system::error_code&) { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1171 | auto devDir = fs::path("/dev/"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1172 | std::vector<fs::path> i2cBuses; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1173 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1174 | boost::container::flat_map<size_t, fs::path> busPaths; |
| 1175 | if (!getI2cDevicePaths(devDir, busPaths)) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1176 | { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1177 | std::cerr << "unable to find i2c devices\n"; |
| 1178 | return; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1179 | } |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1180 | |
| 1181 | for (auto busPath : busPaths) |
| 1182 | { |
| 1183 | i2cBuses.emplace_back(busPath.second); |
| 1184 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1185 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1186 | busmap.clear(); |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 1187 | for (auto& [pair, interface] : foundDevices) |
| 1188 | { |
| 1189 | objServer.remove_interface(interface); |
| 1190 | } |
| 1191 | foundDevices.clear(); |
| 1192 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1193 | auto scan = |
| 1194 | std::make_shared<FindDevicesWithCallback>(i2cBuses, busmap, [&]() { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1195 | for (auto& busIface : dbusInterfaceMap) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1196 | { |
| 1197 | objServer.remove_interface(busIface.second); |
| 1198 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1199 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1200 | dbusInterfaceMap.clear(); |
| 1201 | UNKNOWN_BUS_OBJECT_COUNT = 0; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1202 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1203 | // todo, get this from a more sensable place |
| 1204 | std::vector<char> baseboardFru; |
| 1205 | if (readBaseboardFru(baseboardFru)) |
| 1206 | { |
| 1207 | boost::container::flat_map<int, std::vector<char>> |
| 1208 | baseboardDev; |
| 1209 | baseboardDev.emplace(0, baseboardFru); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1210 | busmap[0] = std::make_shared<DeviceMap>(baseboardDev); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1211 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1212 | for (auto& devicemap : busmap) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1213 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1214 | for (auto& device : *devicemap.second) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1215 | { |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1216 | AddFruObjectToDbus(device.second, dbusInterfaceMap, |
| 1217 | devicemap.first, device.first); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | }); |
| 1221 | scan->run(); |
| 1222 | }); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1225 | int main() |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1226 | { |
| 1227 | auto devDir = fs::path("/dev/"); |
James Feist | c9dff1b | 2019-02-13 13:33:13 -0800 | [diff] [blame] | 1228 | auto matchString = std::string(R"(i2c-\d+$)"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1229 | std::vector<fs::path> i2cBuses; |
| 1230 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 1231 | if (!findFiles(devDir, matchString, i2cBuses)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1232 | { |
| 1233 | std::cerr << "unable to find i2c devices\n"; |
| 1234 | return 1; |
| 1235 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1236 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 1237 | // check for and load blacklist with initial buses. |
| 1238 | loadBlacklist(blacklistPath); |
| 1239 | |
Vijay Khemka | 065f6d9 | 2018-12-18 10:37:47 -0800 | [diff] [blame] | 1240 | systemBus->request_name("xyz.openbmc_project.FruDevice"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1241 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1242 | // this is a map with keys of pair(bus number, address) and values of |
| 1243 | // the object on dbus |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1244 | boost::container::flat_map<std::pair<size_t, size_t>, |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1245 | std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 1246 | dbusInterfaceMap; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1247 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1248 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 1249 | objServer.add_interface("/xyz/openbmc_project/FruDevice", |
| 1250 | "xyz.openbmc_project.FruDeviceManager"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1251 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1252 | iface->register_method("ReScan", |
| 1253 | [&]() { rescanBusses(busMap, dbusInterfaceMap); }); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1254 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1255 | iface->register_method("GetRawFru", getFruInfo); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1256 | |
| 1257 | iface->register_method("WriteFru", [&](const uint8_t bus, |
| 1258 | const uint8_t address, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1259 | const std::vector<uint8_t>& data) { |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1260 | if (!writeFru(bus, address, data)) |
| 1261 | { |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1262 | throw std::invalid_argument("Invalid Arguments."); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1263 | return; |
| 1264 | } |
| 1265 | // schedule rescan on success |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1266 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1267 | }); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1268 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1269 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1270 | std::function<void(sdbusplus::message::message & message)> eventHandler = |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1271 | [&](sdbusplus::message::message& message) { |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1272 | std::string objectName; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1273 | boost::container::flat_map< |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1274 | std::string, |
| 1275 | std::variant<std::string, bool, int64_t, uint64_t, double>> |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1276 | values; |
| 1277 | message.read(objectName, values); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1278 | auto findState = values.find("CurrentHostState"); |
| 1279 | bool on = false; |
| 1280 | if (findState != values.end()) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1281 | { |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1282 | on = boost::ends_with(std::get<std::string>(findState->second), |
| 1283 | "Running"); |
| 1284 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1285 | |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1286 | if (on) |
| 1287 | { |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1288 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1289 | } |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1290 | }; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1291 | |
| 1292 | sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1293 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
James Feist | 7bcd3f2 | 2019-03-18 16:04:04 -0700 | [diff] [blame] | 1294 | "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/" |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1295 | "openbmc_project/state/" |
| 1296 | "host0',arg0='xyz.openbmc_project.State.Host'", |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1297 | eventHandler); |
| 1298 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1299 | int fd = inotify_init(); |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 1300 | inotify_add_watch(fd, I2C_DEV_LOCATION, |
| 1301 | IN_CREATE | IN_MOVED_TO | IN_DELETE); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1302 | std::array<char, 4096> readBuffer; |
| 1303 | std::string pendingBuffer; |
| 1304 | // monitor for new i2c devices |
| 1305 | boost::asio::posix::stream_descriptor dirWatch(io, fd); |
| 1306 | std::function<void(const boost::system::error_code, std::size_t)> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1307 | watchI2cBusses = [&](const boost::system::error_code& ec, |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1308 | std::size_t bytes_transferred) { |
| 1309 | if (ec) |
| 1310 | { |
| 1311 | std::cout << "Callback Error " << ec << "\n"; |
| 1312 | return; |
| 1313 | } |
| 1314 | pendingBuffer += std::string(readBuffer.data(), bytes_transferred); |
| 1315 | bool devChange = false; |
| 1316 | while (pendingBuffer.size() > sizeof(inotify_event)) |
| 1317 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1318 | const inotify_event* iEvent = |
| 1319 | reinterpret_cast<const inotify_event*>( |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1320 | pendingBuffer.data()); |
| 1321 | switch (iEvent->mask) |
| 1322 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1323 | case IN_CREATE: |
| 1324 | case IN_MOVED_TO: |
| 1325 | case IN_DELETE: |
| 1326 | if (boost::starts_with(std::string(iEvent->name), |
| 1327 | "i2c")) |
| 1328 | { |
| 1329 | devChange = true; |
| 1330 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len); |
| 1334 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1335 | if (devChange) |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1336 | { |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1337 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1338 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1339 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1340 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), |
| 1341 | watchI2cBusses); |
| 1342 | }; |
| 1343 | |
| 1344 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses); |
Gunnar Mills | b3e42fe | 2018-06-13 15:48:27 -0500 | [diff] [blame] | 1345 | // run the initial scan |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1346 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1347 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1348 | io.run(); |
| 1349 | return 0; |
| 1350 | } |