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