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