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