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 | */ |
Brad Bishop | 1fb9f3f | 2020-08-28 08:15:13 -0400 | [diff] [blame] | 16 | /// \file FruDevice.cpp |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 17 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame^] | 18 | #include "FruUtils.hpp" |
Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 19 | #include "Utils.hpp" |
| 20 | |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 21 | #include <errno.h> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 22 | #include <fcntl.h> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 23 | #include <sys/inotify.h> |
| 24 | #include <sys/ioctl.h> |
| 25 | |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 26 | #include <boost/algorithm/string/predicate.hpp> |
Brad Bishop | 82c84bb | 2020-08-26 14:12:50 -0400 | [diff] [blame] | 27 | #include <boost/asio/deadline_timer.hpp> |
| 28 | #include <boost/asio/io_service.hpp> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 29 | #include <boost/container/flat_map.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 30 | #include <nlohmann/json.hpp> |
| 31 | #include <sdbusplus/asio/connection.hpp> |
| 32 | #include <sdbusplus/asio/object_server.hpp> |
| 33 | |
| 34 | #include <array> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 35 | #include <chrono> |
| 36 | #include <ctime> |
Patrick Venture | e375400 | 2019-08-06 09:39:12 -0700 | [diff] [blame] | 37 | #include <filesystem> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 38 | #include <fstream> |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 39 | #include <functional> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 40 | #include <future> |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 41 | #include <iomanip> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 42 | #include <iostream> |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 43 | #include <limits> |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 44 | #include <regex> |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 45 | #include <set> |
| 46 | #include <sstream> |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 47 | #include <string> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 48 | #include <thread> |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 49 | #include <utility> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 50 | #include <variant> |
Patrick Venture | c274f2f | 2019-10-26 09:27:23 -0700 | [diff] [blame] | 51 | #include <vector> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 52 | |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 53 | extern "C" |
| 54 | { |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 55 | #include <i2c/smbus.h> |
| 56 | #include <linux/i2c-dev.h> |
| 57 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 58 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 59 | namespace fs = std::filesystem; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 60 | static constexpr bool DEBUG = false; |
| 61 | static size_t UNKNOWN_BUS_OBJECT_COUNT = 0; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 62 | constexpr size_t MAX_FRU_SIZE = 512; |
| 63 | constexpr size_t MAX_EEPROM_PAGE_INDEX = 255; |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 64 | constexpr size_t busTimeoutSeconds = 5; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 65 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 66 | constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json"; |
| 67 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 68 | const static constexpr char* BASEBOARD_FRU_LOCATION = |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 69 | "/etc/fru/baseboard.fru.bin"; |
| 70 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 71 | const static constexpr char* I2C_DEV_LOCATION = "/dev"; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 72 | |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 73 | enum class resCodes |
| 74 | { |
| 75 | resOK, |
| 76 | resWarn, |
| 77 | resErr |
| 78 | }; |
| 79 | |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 80 | static const std::vector<std::string> FRU_AREA_NAMES = { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 81 | "INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"}; |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 82 | const static std::regex NON_ASCII_REGEX("[^\x01-\x7f]"); |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 83 | using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 84 | using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>; |
| 85 | |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 86 | static std::set<size_t> busBlacklist; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 87 | struct FindDevicesWithCallback; |
| 88 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 89 | static BusMap busMap; |
| 90 | |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 91 | static bool powerIsOn = false; |
| 92 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 93 | static boost::container::flat_map< |
| 94 | std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 95 | foundDevices; |
| 96 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 97 | static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses; |
| 98 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 99 | boost::asio::io_service io; |
| 100 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 101 | auto objServer = sdbusplus::asio::object_server(systemBus); |
| 102 | |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 103 | static const std::vector<std::string> CHASSIS_FRU_AREAS = {"PART_NUMBER", |
| 104 | "SERIAL_NUMBER"}; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 105 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 106 | static const std::vector<std::string> BOARD_FRU_AREAS = { |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 107 | "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER", |
| 108 | "FRU_VERSION_ID"}; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 109 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 110 | static const std::vector<std::string> PRODUCT_FRU_AREAS = { |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 111 | "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER", "VERSION", |
| 112 | "SERIAL_NUMBER", "ASSET_TAG", "FRU_VERSION_ID"}; |
| 113 | |
| 114 | static const std::string FRU_CUSTOM_FIELD_NAME = "INFO_AM"; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 115 | |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 116 | static inline const std::string& getFruAreaName(fruAreas area) |
| 117 | { |
| 118 | return FRU_AREA_NAMES[static_cast<unsigned int>(area)]; |
| 119 | } |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 120 | uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter, |
| 121 | std::vector<uint8_t>::const_iterator end); |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 122 | bool updateFRUProperty( |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 123 | const std::string& assetTag, uint32_t bus, uint32_t address, |
| 124 | std::string propertyName, |
| 125 | boost::container::flat_map< |
| 126 | std::pair<size_t, size_t>, |
| 127 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 128 | |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 129 | // Given a bus/address, produce the path in sysfs for an eeprom. |
| 130 | static std::string getEepromPath(size_t bus, size_t address) |
| 131 | { |
| 132 | std::stringstream output; |
| 133 | output << "/sys/bus/i2c/devices/" << bus << "-" << std::right |
| 134 | << std::setfill('0') << std::setw(4) << std::hex << address |
| 135 | << "/eeprom"; |
| 136 | return output.str(); |
| 137 | } |
| 138 | |
| 139 | static bool hasEepromFile(size_t bus, size_t address) |
| 140 | { |
| 141 | auto path = getEepromPath(bus, address); |
| 142 | try |
| 143 | { |
| 144 | return fs::exists(path); |
| 145 | } |
| 146 | catch (...) |
| 147 | { |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 152 | static int64_t readFromEeprom(int flag __attribute__((unused)), int fd, |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 153 | uint16_t address __attribute__((unused)), |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 154 | uint16_t offset, uint8_t len, uint8_t* buf) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 155 | { |
| 156 | auto result = lseek(fd, offset, SEEK_SET); |
| 157 | if (result < 0) |
| 158 | { |
| 159 | std::cerr << "failed to seek\n"; |
| 160 | return -1; |
| 161 | } |
| 162 | |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 163 | return read(fd, buf, len); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 164 | } |
| 165 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 166 | static int busStrToInt(const std::string& busName) |
| 167 | { |
| 168 | auto findBus = busName.rfind("-"); |
| 169 | if (findBus == std::string::npos) |
| 170 | { |
| 171 | return -1; |
| 172 | } |
| 173 | return std::stoi(busName.substr(findBus + 1)); |
| 174 | } |
| 175 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 176 | static int getRootBus(size_t bus) |
| 177 | { |
| 178 | auto ec = std::error_code(); |
| 179 | auto path = std::filesystem::read_symlink( |
| 180 | std::filesystem::path("/sys/bus/i2c/devices/i2c-" + |
| 181 | std::to_string(bus) + "/mux_device"), |
| 182 | ec); |
| 183 | if (ec) |
| 184 | { |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | std::string filename = path.filename(); |
| 189 | auto findBus = filename.find("-"); |
| 190 | if (findBus == std::string::npos) |
| 191 | { |
| 192 | return -1; |
| 193 | } |
| 194 | return std::stoi(filename.substr(0, findBus)); |
| 195 | } |
| 196 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 197 | static bool isMuxBus(size_t bus) |
| 198 | { |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 199 | return is_symlink(std::filesystem::path( |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 200 | "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device")); |
| 201 | } |
| 202 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 203 | static void makeProbeInterface(size_t bus, size_t address) |
| 204 | { |
| 205 | if (isMuxBus(bus)) |
| 206 | { |
| 207 | return; // the mux buses are random, no need to publish |
| 208 | } |
| 209 | auto [it, success] = foundDevices.emplace( |
| 210 | std::make_pair(bus, address), |
| 211 | objServer.add_interface( |
| 212 | "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" + |
| 213 | std::to_string(address), |
| 214 | "xyz.openbmc_project.Inventory.Item.I2CDevice")); |
| 215 | if (!success) |
| 216 | { |
| 217 | return; // already added |
| 218 | } |
| 219 | it->second->register_property("Bus", bus); |
| 220 | it->second->register_property("Address", address); |
| 221 | it->second->initialize(); |
| 222 | } |
| 223 | |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 224 | static int isDevice16Bit(int file) |
| 225 | { |
| 226 | /* Get first byte */ |
| 227 | int byte1 = i2c_smbus_read_byte_data(file, 0); |
| 228 | if (byte1 < 0) |
| 229 | { |
| 230 | return byte1; |
| 231 | } |
| 232 | /* Read 7 more bytes, it will read same first byte in case of |
| 233 | * 8 bit but it will read next byte in case of 16 bit |
| 234 | */ |
| 235 | for (int i = 0; i < 7; i++) |
| 236 | { |
| 237 | int byte2 = i2c_smbus_read_byte_data(file, 0); |
| 238 | if (byte2 < 0) |
| 239 | { |
| 240 | return byte2; |
| 241 | } |
| 242 | if (byte2 != byte1) |
| 243 | { |
| 244 | return 1; |
| 245 | } |
| 246 | } |
| 247 | return 0; |
| 248 | } |
| 249 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 250 | // Issue an I2C transaction to first write to_slave_buf_len bytes,then read |
| 251 | // from_slave_buf_len bytes. |
| 252 | static int i2c_smbus_write_then_read(int file, uint16_t address, |
| 253 | uint8_t* toSlaveBuf, uint8_t toSlaveBufLen, |
| 254 | uint8_t* fromSlaveBuf, |
| 255 | uint8_t fromSlaveBufLen) |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 256 | { |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 257 | if (toSlaveBuf == NULL || toSlaveBufLen == 0 || fromSlaveBuf == NULL || |
| 258 | fromSlaveBufLen == 0) |
| 259 | { |
| 260 | return -1; |
| 261 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 262 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 263 | #define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2 |
| 264 | struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT]; |
| 265 | struct i2c_rdwr_ioctl_data rdwr; |
| 266 | |
| 267 | msgs[0].addr = address; |
| 268 | msgs[0].flags = 0; |
| 269 | msgs[0].len = toSlaveBufLen; |
| 270 | msgs[0].buf = toSlaveBuf; |
| 271 | msgs[1].addr = address; |
| 272 | msgs[1].flags = I2C_M_RD; |
| 273 | msgs[1].len = fromSlaveBufLen; |
| 274 | msgs[1].buf = fromSlaveBuf; |
| 275 | |
| 276 | rdwr.msgs = msgs; |
| 277 | rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT; |
| 278 | |
| 279 | int ret = ioctl(file, I2C_RDWR, &rdwr); |
| 280 | |
| 281 | return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? ret : -1; |
| 282 | } |
| 283 | |
| 284 | static int64_t readBlockData(int flag, int file, uint16_t address, |
| 285 | uint16_t offset, uint8_t len, uint8_t* buf) |
| 286 | { |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 287 | if (flag == 0) |
| 288 | { |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 289 | return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset), |
| 290 | len, buf); |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 293 | offset = htobe16(offset); |
| 294 | return i2c_smbus_write_then_read( |
| 295 | file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len); |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 298 | // TODO: This code is very similar to the non-eeprom version and can be merged |
| 299 | // with some tweaks. |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 300 | static std::vector<uint8_t> processEeprom(int bus, int address) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 301 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 302 | auto path = getEepromPath(bus, address); |
| 303 | |
| 304 | int file = open(path.c_str(), O_RDONLY); |
| 305 | if (file < 0) |
| 306 | { |
| 307 | std::cerr << "Unable to open eeprom file: " << path << "\n"; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 308 | return {}; |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 311 | std::string errorMessage = "eeprom at " + std::to_string(bus) + |
| 312 | " address " + std::to_string(address); |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 313 | std::vector<uint8_t> device = readFRUContents( |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 314 | 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 315 | |
| 316 | close(file); |
| 317 | return device; |
| 318 | } |
| 319 | |
| 320 | std::set<int> findI2CEeproms(int i2cBus, std::shared_ptr<DeviceMap> devices) |
| 321 | { |
| 322 | std::set<int> foundList; |
| 323 | |
| 324 | std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus); |
| 325 | |
| 326 | // For each file listed under the i2c device |
| 327 | // NOTE: This should be faster than just checking for each possible address |
| 328 | // path. |
| 329 | for (const auto& p : fs::directory_iterator(path)) |
| 330 | { |
| 331 | const std::string node = p.path().string(); |
| 332 | std::smatch m; |
| 333 | bool found = |
| 334 | std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)")); |
| 335 | |
| 336 | if (!found) |
| 337 | { |
| 338 | continue; |
| 339 | } |
| 340 | if (m.size() != 2) |
| 341 | { |
| 342 | std::cerr << "regex didn't capture\n"; |
| 343 | continue; |
| 344 | } |
| 345 | |
| 346 | std::ssub_match subMatch = m[1]; |
| 347 | std::string addressString = subMatch.str(); |
| 348 | |
| 349 | std::size_t ignored; |
| 350 | const int hexBase = 16; |
| 351 | int address = std::stoi(addressString, &ignored, hexBase); |
| 352 | |
| 353 | const std::string eeprom = node + "/eeprom"; |
| 354 | |
| 355 | try |
| 356 | { |
| 357 | if (!fs::exists(eeprom)) |
| 358 | { |
| 359 | continue; |
| 360 | } |
| 361 | } |
| 362 | catch (...) |
| 363 | { |
| 364 | continue; |
| 365 | } |
| 366 | |
| 367 | // There is an eeprom file at this address, it may have invalid |
| 368 | // contents, but we found it. |
| 369 | foundList.insert(address); |
| 370 | |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 371 | std::vector<uint8_t> device = processEeprom(i2cBus, address); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 372 | if (!device.empty()) |
| 373 | { |
| 374 | devices->emplace(address, device); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | return foundList; |
| 379 | } |
| 380 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 381 | int getBusFRUs(int file, int first, int last, int bus, |
Patrick Venture | 4f47fe6 | 2019-08-08 16:30:38 -0700 | [diff] [blame] | 382 | std::shared_ptr<DeviceMap> devices) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 383 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 384 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 385 | std::future<int> future = std::async(std::launch::async, [&]() { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 386 | // NOTE: When reading the devices raw on the bus, it can interfere with |
| 387 | // the driver's ability to operate, therefore read eeproms first before |
| 388 | // scanning for devices without drivers. Several experiments were run |
| 389 | // and it was determined that if there were any devices on the bus |
| 390 | // before the eeprom was hit and read, the eeprom driver wouldn't open |
| 391 | // while the bus device was open. An experiment was not performed to see |
| 392 | // if this issue was resolved if the i2c bus device was closed, but |
| 393 | // hexdumps of the eeprom later were successful. |
| 394 | |
| 395 | // Scan for i2c eeproms loaded on this bus. |
| 396 | std::set<int> skipList = findI2CEeproms(bus, devices); |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 397 | std::set<size_t>& failedItems = failedAddresses[bus]; |
| 398 | |
| 399 | std::set<size_t>* rootFailures = nullptr; |
| 400 | int rootBus = getRootBus(bus); |
| 401 | |
| 402 | if (rootBus >= 0) |
| 403 | { |
| 404 | rootFailures = &(failedAddresses[rootBus]); |
| 405 | } |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 406 | |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 407 | constexpr int startSkipSlaveAddr = 0; |
| 408 | constexpr int endSkipSlaveAddr = 12; |
| 409 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 410 | for (int ii = first; ii <= last; ii++) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 411 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 412 | if (skipList.find(ii) != skipList.end()) |
| 413 | { |
| 414 | continue; |
| 415 | } |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 416 | // skipping since no device is present in this range |
| 417 | if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr) |
| 418 | { |
| 419 | continue; |
| 420 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 421 | // Set slave address |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 422 | if (ioctl(file, I2C_SLAVE, ii) < 0) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 423 | { |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 424 | std::cerr << "device at bus " << bus << " address " << ii |
Patrick Venture | 5d8b61d | 2019-08-06 12:36:10 -0700 | [diff] [blame] | 425 | << " busy\n"; |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 426 | continue; |
| 427 | } |
| 428 | // probe |
| 429 | else if (i2c_smbus_read_byte(file) < 0) |
| 430 | { |
| 431 | continue; |
| 432 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 433 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 434 | if (DEBUG) |
| 435 | { |
Patrick Venture | 98e0cf3 | 2019-08-02 11:11:03 -0700 | [diff] [blame] | 436 | std::cout << "something at bus " << bus << " addr " << ii |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 437 | << "\n"; |
| 438 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 439 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 440 | makeProbeInterface(bus, ii); |
| 441 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 442 | if (failedItems.find(ii) != failedItems.end()) |
| 443 | { |
| 444 | // if we failed to read it once, unlikely we can read it later |
| 445 | continue; |
| 446 | } |
| 447 | |
| 448 | if (rootFailures != nullptr) |
| 449 | { |
| 450 | if (rootFailures->find(ii) != rootFailures->end()) |
| 451 | { |
| 452 | continue; |
| 453 | } |
| 454 | } |
| 455 | |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 456 | /* Check for Device type if it is 8 bit or 16 bit */ |
| 457 | int flag = isDevice16Bit(file); |
| 458 | if (flag < 0) |
| 459 | { |
| 460 | std::cerr << "failed to read bus " << bus << " address " << ii |
| 461 | << "\n"; |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 462 | if (powerIsOn) |
| 463 | { |
| 464 | failedItems.insert(ii); |
| 465 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 466 | continue; |
| 467 | } |
| 468 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 469 | std::string errorMessage = |
| 470 | "bus " + std::to_string(bus) + " address " + std::to_string(ii); |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 471 | std::vector<uint8_t> device = |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 472 | readFRUContents(flag, file, static_cast<uint16_t>(ii), |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 473 | readBlockData, errorMessage); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 474 | if (device.empty()) |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 475 | { |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 476 | continue; |
| 477 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 478 | |
Patrick Venture | 786f179 | 2019-08-05 16:33:44 -0700 | [diff] [blame] | 479 | devices->emplace(ii, device); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 480 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 481 | return 1; |
| 482 | }); |
| 483 | std::future_status status = |
| 484 | future.wait_for(std::chrono::seconds(busTimeoutSeconds)); |
| 485 | if (status == std::future_status::timeout) |
| 486 | { |
| 487 | std::cerr << "Error reading bus " << bus << "\n"; |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 488 | if (powerIsOn) |
| 489 | { |
| 490 | busBlacklist.insert(bus); |
| 491 | } |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 492 | close(file); |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 493 | return -1; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 494 | } |
| 495 | |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 496 | close(file); |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 497 | return future.get(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 500 | void loadBlacklist(const char* path) |
| 501 | { |
| 502 | std::ifstream blacklistStream(path); |
| 503 | if (!blacklistStream.good()) |
| 504 | { |
| 505 | // File is optional. |
| 506 | std::cerr << "Cannot open blacklist file.\n\n"; |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | nlohmann::json data = |
| 511 | nlohmann::json::parse(blacklistStream, nullptr, false); |
| 512 | if (data.is_discarded()) |
| 513 | { |
| 514 | std::cerr << "Illegal blacklist file detected, cannot validate JSON, " |
| 515 | "exiting\n"; |
| 516 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // It's expected to have at least one field, "buses" that is an array of the |
| 520 | // buses by integer. Allow for future options to exclude further aspects, |
| 521 | // such as specific addresses or ranges. |
| 522 | if (data.type() != nlohmann::json::value_t::object) |
| 523 | { |
| 524 | std::cerr << "Illegal blacklist, expected to read dictionary\n"; |
| 525 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // If buses field is missing, that's fine. |
| 529 | if (data.count("buses") == 1) |
| 530 | { |
| 531 | // Parse the buses array after a little validation. |
| 532 | auto buses = data.at("buses"); |
| 533 | if (buses.type() != nlohmann::json::value_t::array) |
| 534 | { |
| 535 | // Buses field present but invalid, therefore this is an error. |
| 536 | std::cerr << "Invalid contents for blacklist buses field\n"; |
| 537 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // Catch exception here for type mis-match. |
| 541 | try |
| 542 | { |
| 543 | for (const auto& bus : buses) |
| 544 | { |
| 545 | busBlacklist.insert(bus.get<size_t>()); |
| 546 | } |
| 547 | } |
| 548 | catch (const nlohmann::detail::type_error& e) |
| 549 | { |
| 550 | // Type mis-match is a critical error. |
| 551 | std::cerr << "Invalid bus type: " << e.what() << "\n"; |
| 552 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | return; |
| 557 | } |
| 558 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 559 | static void FindI2CDevices(const std::vector<fs::path>& i2cBuses, |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 560 | BusMap& busmap) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 561 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 562 | for (auto& i2cBus : i2cBuses) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 563 | { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 564 | int bus = busStrToInt(i2cBus); |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 565 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 566 | if (bus < 0) |
| 567 | { |
| 568 | std::cerr << "Cannot translate " << i2cBus << " to int\n"; |
| 569 | continue; |
| 570 | } |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 571 | if (busBlacklist.find(bus) != busBlacklist.end()) |
| 572 | { |
| 573 | continue; // skip previously failed busses |
| 574 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 575 | |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 576 | int rootBus = getRootBus(bus); |
| 577 | if (busBlacklist.find(rootBus) != busBlacklist.end()) |
| 578 | { |
| 579 | continue; |
| 580 | } |
| 581 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 582 | auto file = open(i2cBus.c_str(), O_RDWR); |
| 583 | if (file < 0) |
| 584 | { |
| 585 | std::cerr << "unable to open i2c device " << i2cBus.string() |
| 586 | << "\n"; |
| 587 | continue; |
| 588 | } |
| 589 | unsigned long funcs = 0; |
| 590 | |
| 591 | if (ioctl(file, I2C_FUNCS, &funcs) < 0) |
| 592 | { |
| 593 | std::cerr |
Patrick Venture | 98e0cf3 | 2019-08-02 11:11:03 -0700 | [diff] [blame] | 594 | << "Error: Could not get the adapter functionality matrix bus " |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 595 | << bus << "\n"; |
| 596 | continue; |
| 597 | } |
| 598 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) || |
| 599 | !(I2C_FUNC_SMBUS_READ_I2C_BLOCK)) |
| 600 | { |
| 601 | std::cerr << "Error: Can't use SMBus Receive Byte command bus " |
| 602 | << bus << "\n"; |
| 603 | continue; |
| 604 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 605 | auto& device = busmap[bus]; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 606 | device = std::make_shared<DeviceMap>(); |
| 607 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 608 | // i2cdetect by default uses the range 0x03 to 0x77, as |
| 609 | // this is what we have tested with, use this range. Could be |
| 610 | // changed in future. |
| 611 | if (DEBUG) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 612 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 613 | std::cerr << "Scanning bus " << bus << "\n"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 614 | } |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 615 | |
| 616 | // fd is closed in this function in case the bus locks up |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 617 | getBusFRUs(file, 0x03, 0x77, bus, device); |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 618 | |
| 619 | if (DEBUG) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 620 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 621 | std::cerr << "Done scanning bus " << bus << "\n"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 622 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 623 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 624 | } |
| 625 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 626 | // this class allows an async response after all i2c devices are discovered |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 627 | struct FindDevicesWithCallback : |
| 628 | std::enable_shared_from_this<FindDevicesWithCallback> |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 629 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 630 | FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 631 | BusMap& busmap, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 632 | std::function<void(void)>&& callback) : |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 633 | _i2cBuses(i2cBuses), |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 634 | _busMap(busmap), _callback(std::move(callback)) |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 635 | {} |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 636 | ~FindDevicesWithCallback() |
| 637 | { |
| 638 | _callback(); |
| 639 | } |
| 640 | void run() |
| 641 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 642 | FindI2CDevices(_i2cBuses, _busMap); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 643 | } |
| 644 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 645 | const std::vector<fs::path>& _i2cBuses; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 646 | BusMap& _busMap; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 647 | std::function<void(void)> _callback; |
| 648 | }; |
| 649 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 650 | static const std::tm intelEpoch(void) |
| 651 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 652 | std::tm val = {}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 653 | val.tm_year = 1996 - 1900; |
| 654 | return val; |
| 655 | } |
| 656 | |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 657 | static char sixBitToChar(uint8_t val) |
| 658 | { |
| 659 | return static_cast<char>((val & 0x3f) + ' '); |
| 660 | } |
| 661 | |
| 662 | /* 0xd - 0xf are reserved values, but not fatal; use a placeholder char. */ |
| 663 | static const char bcdHighChars[] = { |
| 664 | ' ', '-', '.', 'X', 'X', 'X', |
| 665 | }; |
| 666 | |
| 667 | static char bcdPlusToChar(uint8_t val) |
| 668 | { |
| 669 | val &= 0xf; |
| 670 | return (val < 10) ? static_cast<char>(val + '0') : bcdHighChars[val - 10]; |
| 671 | } |
| 672 | |
| 673 | enum class DecodeState |
| 674 | { |
| 675 | ok, |
| 676 | end, |
| 677 | err, |
| 678 | }; |
| 679 | |
| 680 | enum FRUDataEncoding |
| 681 | { |
| 682 | binary = 0x0, |
| 683 | bcdPlus = 0x1, |
| 684 | sixBitASCII = 0x2, |
| 685 | languageDependent = 0x3, |
| 686 | }; |
| 687 | |
| 688 | /* Decode FRU data into a std::string, given an input iterator and end. If the |
| 689 | * state returned is fruDataOk, then the resulting string is the decoded FRU |
| 690 | * data. The input iterator is advanced past the data consumed. |
| 691 | * |
| 692 | * On fruDataErr, we have lost synchronisation with the length bytes, so the |
| 693 | * iterator is no longer usable. |
| 694 | */ |
| 695 | static std::pair<DecodeState, std::string> |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 696 | decodeFRUData(std::vector<uint8_t>::const_iterator& iter, |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 697 | const std::vector<uint8_t>::const_iterator& end) |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 698 | { |
| 699 | std::string value; |
| 700 | unsigned int i; |
| 701 | |
| 702 | /* we need at least one byte to decode the type/len header */ |
| 703 | if (iter == end) |
| 704 | { |
| 705 | std::cerr << "Truncated FRU data\n"; |
| 706 | return make_pair(DecodeState::err, value); |
| 707 | } |
| 708 | |
| 709 | uint8_t c = *(iter++); |
| 710 | |
| 711 | /* 0xc1 is the end marker */ |
| 712 | if (c == 0xc1) |
| 713 | { |
| 714 | return make_pair(DecodeState::end, value); |
| 715 | } |
| 716 | |
| 717 | /* decode type/len byte */ |
| 718 | uint8_t type = static_cast<uint8_t>(c >> 6); |
| 719 | uint8_t len = static_cast<uint8_t>(c & 0x3f); |
| 720 | |
| 721 | /* we should have at least len bytes of data available overall */ |
| 722 | if (iter + len > end) |
| 723 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 724 | std::cerr << "FRU data field extends past end of FRU area data\n"; |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 725 | return make_pair(DecodeState::err, value); |
| 726 | } |
| 727 | |
| 728 | switch (type) |
| 729 | { |
| 730 | case FRUDataEncoding::binary: |
Andrei Kartashev | c994c02 | 2020-08-10 18:51:49 +0300 | [diff] [blame] | 731 | { |
| 732 | std::stringstream ss; |
| 733 | ss << std::hex << std::setfill('0'); |
| 734 | for (i = 0; i < len; i++, iter++) |
| 735 | { |
| 736 | uint8_t val = static_cast<uint8_t>(*iter); |
| 737 | ss << std::setw(2) << static_cast<int>(val); |
| 738 | } |
| 739 | value = ss.str(); |
| 740 | break; |
| 741 | } |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 742 | case FRUDataEncoding::languageDependent: |
| 743 | /* For language-code dependent encodings, assume 8-bit ASCII */ |
| 744 | value = std::string(iter, iter + len); |
| 745 | iter += len; |
| 746 | break; |
| 747 | |
| 748 | case FRUDataEncoding::bcdPlus: |
| 749 | value = std::string(); |
| 750 | for (i = 0; i < len; i++, iter++) |
| 751 | { |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 752 | uint8_t val = *iter; |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 753 | value.push_back(bcdPlusToChar(val >> 4)); |
| 754 | value.push_back(bcdPlusToChar(val & 0xf)); |
| 755 | } |
| 756 | break; |
| 757 | |
| 758 | case FRUDataEncoding::sixBitASCII: |
| 759 | { |
Andrei Kartashev | e707de6 | 2020-08-10 18:33:29 +0300 | [diff] [blame] | 760 | unsigned int accum = 0; |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 761 | unsigned int accumBitLen = 0; |
| 762 | value = std::string(); |
| 763 | for (i = 0; i < len; i++, iter++) |
| 764 | { |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 765 | accum |= *iter << accumBitLen; |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 766 | accumBitLen += 8; |
| 767 | while (accumBitLen >= 6) |
| 768 | { |
| 769 | value.push_back(sixBitToChar(accum & 0x3f)); |
| 770 | accum >>= 6; |
| 771 | accumBitLen -= 6; |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | break; |
| 776 | } |
| 777 | |
| 778 | return make_pair(DecodeState::ok, value); |
| 779 | } |
| 780 | |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 781 | static void checkLang(uint8_t lang) |
| 782 | { |
| 783 | // If Lang is not English then the encoding is defined as 2-byte UNICODE, |
| 784 | // but we don't support that. |
| 785 | if (lang && lang != 25) |
| 786 | { |
| 787 | std::cerr << "Warning: language other then English is not " |
| 788 | "supported \n"; |
| 789 | } |
| 790 | } |
| 791 | |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 792 | resCodes formatFRU(const std::vector<uint8_t>& fruBytes, |
| 793 | boost::container::flat_map<std::string, std::string>& result) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 794 | { |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 795 | resCodes ret = resCodes::resOK; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 796 | if (fruBytes.size() <= fruBlockSize) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 797 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 798 | std::cerr << "Error: trying to parse empty FRU \n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 799 | return resCodes::resErr; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 800 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 801 | result["Common_Format_Version"] = |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 802 | std::to_string(static_cast<int>(*fruBytes.begin())); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 803 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 804 | const std::vector<std::string>* fruAreaFieldNames; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 805 | |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 806 | // Don't parse Internal and Multirecord areas |
| 807 | for (fruAreas area = fruAreas::fruAreaChassis; |
| 808 | area <= fruAreas::fruAreaProduct; ++area) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 809 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 810 | |
| 811 | size_t offset = *(fruBytes.begin() + getHeaderAreaFieldOffset(area)); |
| 812 | if (offset == 0) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 813 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 814 | continue; |
| 815 | } |
| 816 | offset *= fruBlockSize; |
| 817 | std::vector<uint8_t>::const_iterator fruBytesIter = |
| 818 | fruBytes.begin() + offset; |
| 819 | if (fruBytesIter + fruBlockSize >= fruBytes.end()) |
| 820 | { |
| 821 | std::cerr << "Not enough data to parse \n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 822 | return resCodes::resErr; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 823 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 824 | // check for format version 1 |
| 825 | if (*fruBytesIter != 0x01) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 826 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 827 | std::cerr << "Unexpected version " << *fruBytesIter << "\n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 828 | return resCodes::resErr; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 829 | } |
| 830 | ++fruBytesIter; |
| 831 | uint8_t fruAreaSize = *fruBytesIter * fruBlockSize; |
| 832 | std::vector<uint8_t>::const_iterator fruBytesIterEndArea = |
| 833 | fruBytes.begin() + offset + fruAreaSize - 1; |
| 834 | ++fruBytesIter; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 835 | |
Andrei Kartashev | 272bafd | 2020-10-09 12:05:32 +0300 | [diff] [blame] | 836 | uint8_t fruComputedChecksum = |
| 837 | calculateChecksum(fruBytes.begin() + offset, fruBytesIterEndArea); |
| 838 | if (fruComputedChecksum != *fruBytesIterEndArea) |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 839 | { |
Andrei Kartashev | 272bafd | 2020-10-09 12:05:32 +0300 | [diff] [blame] | 840 | std::stringstream ss; |
| 841 | ss << std::hex << std::setfill('0'); |
| 842 | ss << "Checksum error in FRU area " << getFruAreaName(area) << "\n"; |
| 843 | ss << "\tComputed checksum: 0x" << std::setw(2) |
| 844 | << static_cast<int>(fruComputedChecksum) << "\n"; |
| 845 | ss << "\tThe read checksum: 0x" << std::setw(2) |
| 846 | << static_cast<int>(*fruBytesIterEndArea) << "\n"; |
| 847 | std::cerr << ss.str(); |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 848 | ret = resCodes::resWarn; |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 849 | } |
| 850 | |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 851 | switch (area) |
| 852 | { |
| 853 | case fruAreas::fruAreaChassis: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 854 | { |
| 855 | result["CHASSIS_TYPE"] = |
| 856 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 857 | fruBytesIter += 1; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 858 | fruAreaFieldNames = &CHASSIS_FRU_AREAS; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 859 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 860 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 861 | case fruAreas::fruAreaBoard: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 862 | { |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 863 | uint8_t lang = *fruBytesIter; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 864 | result["BOARD_LANGUAGE_CODE"] = |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 865 | std::to_string(static_cast<int>(lang)); |
| 866 | checkLang(lang); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 867 | fruBytesIter += 1; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 868 | |
| 869 | unsigned int minutes = *fruBytesIter | |
| 870 | *(fruBytesIter + 1) << 8 | |
| 871 | *(fruBytesIter + 2) << 16; |
| 872 | std::tm fruTime = intelEpoch(); |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 873 | std::time_t timeValue = std::mktime(&fruTime); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 874 | timeValue += minutes * 60; |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 875 | fruTime = *std::gmtime(&timeValue); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 876 | |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 877 | // Tue Nov 20 23:08:00 2018 |
| 878 | char timeString[32] = {0}; |
| 879 | auto bytes = std::strftime(timeString, sizeof(timeString), |
Patrick Venture | fff050a | 2019-08-13 11:44:30 -0700 | [diff] [blame] | 880 | "%Y-%m-%d - %H:%M:%S", &fruTime); |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 881 | if (bytes == 0) |
| 882 | { |
| 883 | std::cerr << "invalid time string encountered\n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 884 | return resCodes::resErr; |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | result["BOARD_MANUFACTURE_DATE"] = std::string(timeString); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 888 | fruBytesIter += 3; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 889 | fruAreaFieldNames = &BOARD_FRU_AREAS; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 890 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 891 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 892 | case fruAreas::fruAreaProduct: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 893 | { |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 894 | uint8_t lang = *fruBytesIter; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 895 | result["PRODUCT_LANGUAGE_CODE"] = |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 896 | std::to_string(static_cast<int>(lang)); |
| 897 | checkLang(lang); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 898 | fruBytesIter += 1; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 899 | fruAreaFieldNames = &PRODUCT_FRU_AREAS; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 900 | break; |
| 901 | } |
| 902 | default: |
| 903 | { |
| 904 | std::cerr << "Internal error: unexpected FRU area index: " |
| 905 | << static_cast<int>(area) << " \n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 906 | return resCodes::resErr; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | size_t fieldIndex = 0; |
| 910 | DecodeState state; |
| 911 | do |
| 912 | { |
| 913 | auto res = decodeFRUData(fruBytesIter, fruBytesIterEndArea); |
| 914 | state = res.first; |
| 915 | std::string value = res.second; |
| 916 | std::string name; |
| 917 | if (fieldIndex < fruAreaFieldNames->size()) |
| 918 | { |
| 919 | name = std::string(getFruAreaName(area)) + "_" + |
| 920 | fruAreaFieldNames->at(fieldIndex); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 921 | } |
| 922 | else |
| 923 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 924 | name = |
| 925 | std::string(getFruAreaName(area)) + "_" + |
| 926 | FRU_CUSTOM_FIELD_NAME + |
| 927 | std::to_string(fieldIndex - fruAreaFieldNames->size() + 1); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 928 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 929 | |
| 930 | if (state == DecodeState::ok) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 931 | { |
Ed Tanous | 2147e67 | 2019-02-27 13:59:56 -0800 | [diff] [blame] | 932 | // Strip non null characters from the end |
| 933 | value.erase(std::find_if(value.rbegin(), value.rend(), |
| 934 | [](char ch) { return ch != 0; }) |
| 935 | .base(), |
| 936 | value.end()); |
| 937 | |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 938 | result[name] = std::move(value); |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 939 | ++fieldIndex; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 940 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 941 | else if (state == DecodeState::err) |
| 942 | { |
| 943 | std::cerr << "Error while parsing " << name << "\n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 944 | ret = resCodes::resWarn; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 945 | // Cancel decoding if failed to parse any of mandatory |
| 946 | // fields |
| 947 | if (fieldIndex < fruAreaFieldNames->size()) |
| 948 | { |
| 949 | std::cerr << "Failed to parse mandatory field \n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 950 | return resCodes::resErr; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 951 | } |
| 952 | } |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 953 | else |
| 954 | { |
| 955 | if (fieldIndex < fruAreaFieldNames->size()) |
| 956 | { |
| 957 | std::cerr << "Mandatory fields absent in FRU area " |
| 958 | << getFruAreaName(area) << " after " << name |
| 959 | << "\n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 960 | ret = resCodes::resWarn; |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 961 | } |
| 962 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 963 | } while (state == DecodeState::ok); |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 964 | for (; fruBytesIter < fruBytesIterEndArea; fruBytesIter++) |
| 965 | { |
| 966 | uint8_t c = *fruBytesIter; |
| 967 | if (c) |
| 968 | { |
| 969 | std::cerr << "Non-zero byte after EndOfFields in FRU area " |
| 970 | << getFruAreaName(area) << "\n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 971 | ret = resCodes::resWarn; |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 972 | break; |
| 973 | } |
| 974 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 975 | } |
| 976 | |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 977 | return ret; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 978 | } |
| 979 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 980 | std::vector<uint8_t>& getFRUInfo(const uint8_t& bus, const uint8_t& address) |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 981 | { |
| 982 | auto deviceMap = busMap.find(bus); |
| 983 | if (deviceMap == busMap.end()) |
| 984 | { |
| 985 | throw std::invalid_argument("Invalid Bus."); |
| 986 | } |
| 987 | auto device = deviceMap->second->find(address); |
| 988 | if (device == deviceMap->second->end()) |
| 989 | { |
| 990 | throw std::invalid_argument("Invalid Address."); |
| 991 | } |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 992 | std::vector<uint8_t>& ret = device->second; |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 993 | |
| 994 | return ret; |
| 995 | } |
| 996 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 997 | void AddFRUObjectToDbus( |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 998 | std::vector<uint8_t>& device, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 999 | boost::container::flat_map< |
| 1000 | std::pair<size_t, size_t>, |
| 1001 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 1002 | uint32_t bus, uint32_t address) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1003 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1004 | boost::container::flat_map<std::string, std::string> formattedFRU; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 1005 | resCodes res = formatFRU(device, formattedFRU); |
| 1006 | if (res == resCodes::resErr) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1007 | { |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 1008 | std::cerr << "failed to parse FRU for device at bus " << bus |
Patrick Venture | b755c83 | 2019-08-07 11:09:14 -0700 | [diff] [blame] | 1009 | << " address " << address << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1010 | return; |
| 1011 | } |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 1012 | else if (res == resCodes::resWarn) |
| 1013 | { |
| 1014 | std::cerr << "there were warnings while parsing FRU for device at bus " |
| 1015 | << bus << " address " << address << "\n"; |
| 1016 | } |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1017 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1018 | auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1019 | std::string productName; |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1020 | // Not found under Board section or an empty string. |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1021 | if (productNameFind == formattedFRU.end() || |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1022 | productNameFind->second.empty()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1023 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1024 | productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1025 | } |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1026 | // Found under Product section and not an empty string. |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1027 | if (productNameFind != formattedFRU.end() && |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1028 | !productNameFind->second.empty()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1029 | { |
| 1030 | productName = productNameFind->second; |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 1031 | std::regex illegalObject("[^A-Za-z0-9_]"); |
| 1032 | productName = std::regex_replace(productName, illegalObject, "_"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1033 | } |
| 1034 | else |
| 1035 | { |
| 1036 | productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT); |
| 1037 | UNKNOWN_BUS_OBJECT_COUNT++; |
| 1038 | } |
| 1039 | |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1040 | productName = "/xyz/openbmc_project/FruDevice/" + productName; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1041 | // avoid duplicates by checking to see if on a mux |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1042 | if (bus > 0) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1043 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1044 | int highest = -1; |
| 1045 | bool found = false; |
| 1046 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1047 | for (auto const& busIface : dbusInterfaceMap) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1048 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1049 | std::string path = busIface.second->get_object_path(); |
| 1050 | if (std::regex_match(path, std::regex(productName + "(_\\d+|)$"))) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1051 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1052 | if (isMuxBus(bus) && address == busIface.first.second && |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1053 | (getFRUInfo(static_cast<uint8_t>(busIface.first.first), |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1054 | static_cast<uint8_t>(busIface.first.second)) == |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1055 | getFRUInfo(static_cast<uint8_t>(bus), |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1056 | static_cast<uint8_t>(address)))) |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1057 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1058 | // This device is already added to the lower numbered bus, |
| 1059 | // do not replicate it. |
| 1060 | return; |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1061 | } |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1062 | |
| 1063 | // Check if the match named has extra information. |
| 1064 | found = true; |
| 1065 | std::smatch base_match; |
| 1066 | |
| 1067 | bool match = std::regex_match( |
| 1068 | path, base_match, std::regex(productName + "_(\\d+)$")); |
| 1069 | if (match) |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1070 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1071 | if (base_match.size() == 2) |
| 1072 | { |
| 1073 | std::ssub_match base_sub_match = base_match[1]; |
| 1074 | std::string base = base_sub_match.str(); |
| 1075 | |
| 1076 | int value = std::stoi(base); |
| 1077 | highest = (value > highest) ? value : highest; |
| 1078 | } |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1079 | } |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1080 | } |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1081 | } // end searching objects |
| 1082 | |
| 1083 | if (found) |
| 1084 | { |
| 1085 | // We found something with the same name. If highest was still -1, |
| 1086 | // it means this new entry will be _0. |
| 1087 | productName += "_"; |
| 1088 | productName += std::to_string(++highest); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1089 | } |
| 1090 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1091 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1092 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 1093 | objServer.add_interface(productName, "xyz.openbmc_project.FruDevice"); |
| 1094 | dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface; |
| 1095 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1096 | for (auto& property : formattedFRU) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1097 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1098 | |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 1099 | std::regex_replace(property.second.begin(), property.second.begin(), |
| 1100 | property.second.end(), NON_ASCII_REGEX, "_"); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1101 | if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG") |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1102 | { |
| 1103 | continue; |
| 1104 | } |
| 1105 | std::string key = |
| 1106 | std::regex_replace(property.first, NON_ASCII_REGEX, "_"); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1107 | |
| 1108 | if (property.first == "PRODUCT_ASSET_TAG") |
| 1109 | { |
| 1110 | std::string propertyName = property.first; |
| 1111 | iface->register_property( |
| 1112 | key, property.second + '\0', |
| 1113 | [bus, address, propertyName, |
| 1114 | &dbusInterfaceMap](const std::string& req, std::string& resp) { |
| 1115 | if (strcmp(req.c_str(), resp.c_str())) |
| 1116 | { |
| 1117 | // call the method which will update |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1118 | if (updateFRUProperty(req, bus, address, propertyName, |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1119 | dbusInterfaceMap)) |
| 1120 | { |
| 1121 | resp = req; |
| 1122 | } |
| 1123 | else |
| 1124 | { |
| 1125 | throw std::invalid_argument( |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1126 | "FRU property update failed."); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | return 1; |
| 1130 | }); |
| 1131 | } |
| 1132 | else if (!iface->register_property(key, property.second + '\0')) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1133 | { |
| 1134 | std::cerr << "illegal key: " << key << "\n"; |
| 1135 | } |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 1136 | if (DEBUG) |
| 1137 | { |
| 1138 | std::cout << property.first << ": " << property.second << "\n"; |
| 1139 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1140 | } |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1141 | |
| 1142 | // baseboard will be 0, 0 |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 1143 | iface->register_property("BUS", bus); |
| 1144 | iface->register_property("ADDRESS", address); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1145 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1146 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1149 | static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1150 | { |
| 1151 | // try to read baseboard fru from file |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1152 | std::ifstream baseboardFRUFile(BASEBOARD_FRU_LOCATION, std::ios::binary); |
| 1153 | if (baseboardFRUFile.good()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1154 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1155 | baseboardFRUFile.seekg(0, std::ios_base::end); |
| 1156 | size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg()); |
| 1157 | baseboardFRU.resize(fileSize); |
| 1158 | baseboardFRUFile.seekg(0, std::ios_base::beg); |
| 1159 | baseboardFRUFile.read(reinterpret_cast<char*>(baseboardFRU.data()), |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1160 | fileSize); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1161 | } |
| 1162 | else |
| 1163 | { |
| 1164 | return false; |
| 1165 | } |
| 1166 | return true; |
| 1167 | } |
| 1168 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1169 | 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] | 1170 | { |
| 1171 | boost::container::flat_map<std::string, std::string> tmp; |
| 1172 | if (fru.size() > MAX_FRU_SIZE) |
| 1173 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1174 | std::cerr << "Invalid fru.size() during writeFRU\n"; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1175 | return false; |
| 1176 | } |
| 1177 | // verify legal fru by running it through fru parsing logic |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 1178 | if (formatFRU(fru, tmp) != resCodes::resOK) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1179 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1180 | std::cerr << "Invalid fru format during writeFRU\n"; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1181 | return false; |
| 1182 | } |
| 1183 | // baseboard fru |
| 1184 | if (bus == 0 && address == 0) |
| 1185 | { |
| 1186 | std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary); |
| 1187 | if (!file.good()) |
| 1188 | { |
| 1189 | std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION |
| 1190 | << "\n"; |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1191 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1192 | return false; |
| 1193 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1194 | file.write(reinterpret_cast<const char*>(fru.data()), fru.size()); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1195 | return file.good(); |
| 1196 | } |
| 1197 | else |
| 1198 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 1199 | if (hasEepromFile(bus, address)) |
| 1200 | { |
| 1201 | auto path = getEepromPath(bus, address); |
| 1202 | int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC); |
| 1203 | if (eeprom < 0) |
| 1204 | { |
| 1205 | std::cerr << "unable to open i2c device " << path << "\n"; |
| 1206 | throw DBusInternalError(); |
| 1207 | return false; |
| 1208 | } |
| 1209 | |
| 1210 | ssize_t writtenBytes = write(eeprom, fru.data(), fru.size()); |
| 1211 | if (writtenBytes < 0) |
| 1212 | { |
| 1213 | std::cerr << "unable to write to i2c device " << path << "\n"; |
| 1214 | close(eeprom); |
| 1215 | throw DBusInternalError(); |
| 1216 | return false; |
| 1217 | } |
| 1218 | |
| 1219 | close(eeprom); |
| 1220 | return true; |
| 1221 | } |
| 1222 | |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1223 | std::string i2cBus = "/dev/i2c-" + std::to_string(bus); |
| 1224 | |
| 1225 | int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC); |
| 1226 | if (file < 0) |
| 1227 | { |
| 1228 | std::cerr << "unable to open i2c device " << i2cBus << "\n"; |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1229 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1230 | return false; |
| 1231 | } |
| 1232 | if (ioctl(file, I2C_SLAVE_FORCE, address) < 0) |
| 1233 | { |
| 1234 | std::cerr << "unable to set device address\n"; |
| 1235 | close(file); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1236 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1237 | return false; |
| 1238 | } |
| 1239 | |
| 1240 | constexpr const size_t RETRY_MAX = 2; |
| 1241 | uint16_t index = 0; |
| 1242 | size_t retries = RETRY_MAX; |
| 1243 | while (index < fru.size()) |
| 1244 | { |
| 1245 | if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) && |
| 1246 | (retries == RETRY_MAX)) |
| 1247 | { |
| 1248 | // The 4K EEPROM only uses the A2 and A1 device address bits |
| 1249 | // with the third bit being a memory page address bit. |
| 1250 | if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0) |
| 1251 | { |
| 1252 | std::cerr << "unable to set device address\n"; |
| 1253 | close(file); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1254 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1255 | return false; |
| 1256 | } |
| 1257 | } |
| 1258 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1259 | if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index), |
| 1260 | fru[index]) < 0) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1261 | { |
| 1262 | if (!retries--) |
| 1263 | { |
| 1264 | std::cerr << "error writing fru: " << strerror(errno) |
| 1265 | << "\n"; |
| 1266 | close(file); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1267 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1268 | return false; |
| 1269 | } |
| 1270 | } |
| 1271 | else |
| 1272 | { |
| 1273 | retries = RETRY_MAX; |
| 1274 | index++; |
| 1275 | } |
| 1276 | // most eeproms require 5-10ms between writes |
| 1277 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 1278 | } |
| 1279 | close(file); |
| 1280 | return true; |
| 1281 | } |
| 1282 | } |
| 1283 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1284 | void rescanOneBus( |
| 1285 | BusMap& busmap, uint8_t busNum, |
| 1286 | boost::container::flat_map< |
| 1287 | std::pair<size_t, size_t>, |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1288 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
| 1289 | bool dbusCall) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1290 | { |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1291 | for (auto& [pair, interface] : foundDevices) |
| 1292 | { |
| 1293 | if (pair.first == static_cast<size_t>(busNum)) |
| 1294 | { |
| 1295 | objServer.remove_interface(interface); |
| 1296 | foundDevices.erase(pair); |
| 1297 | } |
| 1298 | } |
| 1299 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1300 | fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum)); |
| 1301 | if (!fs::exists(busPath)) |
| 1302 | { |
| 1303 | if (dbusCall) |
| 1304 | { |
| 1305 | std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum) |
| 1306 | << "\n"; |
| 1307 | throw std::invalid_argument("Invalid Bus."); |
| 1308 | } |
| 1309 | return; |
| 1310 | } |
| 1311 | |
| 1312 | std::vector<fs::path> i2cBuses; |
| 1313 | i2cBuses.emplace_back(busPath); |
| 1314 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1315 | auto scan = std::make_shared<FindDevicesWithCallback>( |
| 1316 | i2cBuses, busmap, [busNum, &busmap, &dbusInterfaceMap]() { |
| 1317 | for (auto& busIface : dbusInterfaceMap) |
| 1318 | { |
| 1319 | if (busIface.first.first == static_cast<size_t>(busNum)) |
| 1320 | { |
| 1321 | objServer.remove_interface(busIface.second); |
| 1322 | } |
| 1323 | } |
Wludzik, Jozef | c1136b7 | 2020-06-24 11:58:32 +0200 | [diff] [blame] | 1324 | auto found = busmap.find(busNum); |
| 1325 | if (found == busmap.end() || found->second == nullptr) |
| 1326 | { |
| 1327 | return; |
| 1328 | } |
| 1329 | for (auto& device : *(found->second)) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1330 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1331 | AddFRUObjectToDbus(device.second, dbusInterfaceMap, |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1332 | static_cast<uint32_t>(busNum), device.first); |
| 1333 | } |
| 1334 | }); |
| 1335 | scan->run(); |
| 1336 | } |
| 1337 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1338 | void rescanBusses( |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1339 | BusMap& busmap, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1340 | boost::container::flat_map< |
| 1341 | std::pair<size_t, size_t>, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1342 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1343 | { |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1344 | static boost::asio::deadline_timer timer(io); |
| 1345 | timer.expires_from_now(boost::posix_time::seconds(1)); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1346 | |
Gunnar Mills | 6f0ae94 | 2018-08-31 12:38:03 -0500 | [diff] [blame] | 1347 | // setup an async wait in case we get flooded with requests |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1348 | timer.async_wait([&](const boost::system::error_code&) { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1349 | auto devDir = fs::path("/dev/"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1350 | std::vector<fs::path> i2cBuses; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1351 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1352 | boost::container::flat_map<size_t, fs::path> busPaths; |
| 1353 | if (!getI2cDevicePaths(devDir, busPaths)) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1354 | { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1355 | std::cerr << "unable to find i2c devices\n"; |
| 1356 | return; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1357 | } |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1358 | |
| 1359 | for (auto busPath : busPaths) |
| 1360 | { |
| 1361 | i2cBuses.emplace_back(busPath.second); |
| 1362 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1363 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1364 | busmap.clear(); |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 1365 | for (auto& [pair, interface] : foundDevices) |
| 1366 | { |
| 1367 | objServer.remove_interface(interface); |
| 1368 | } |
| 1369 | foundDevices.clear(); |
| 1370 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1371 | auto scan = |
| 1372 | std::make_shared<FindDevicesWithCallback>(i2cBuses, busmap, [&]() { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1373 | for (auto& busIface : dbusInterfaceMap) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1374 | { |
| 1375 | objServer.remove_interface(busIface.second); |
| 1376 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1377 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1378 | dbusInterfaceMap.clear(); |
| 1379 | UNKNOWN_BUS_OBJECT_COUNT = 0; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1380 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1381 | // todo, get this from a more sensable place |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1382 | std::vector<uint8_t> baseboardFRU; |
| 1383 | if (readBaseboardFRU(baseboardFRU)) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1384 | { |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1385 | boost::container::flat_map<int, std::vector<uint8_t>> |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1386 | baseboardDev; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1387 | baseboardDev.emplace(0, baseboardFRU); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1388 | busmap[0] = std::make_shared<DeviceMap>(baseboardDev); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1389 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1390 | for (auto& devicemap : busmap) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1391 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1392 | for (auto& device : *devicemap.second) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1393 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1394 | AddFRUObjectToDbus(device.second, dbusInterfaceMap, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1395 | devicemap.first, device.first); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | }); |
| 1399 | scan->run(); |
| 1400 | }); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1403 | // Calculate new checksum for fru info area |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1404 | uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter, |
| 1405 | std::vector<uint8_t>::const_iterator end) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1406 | { |
| 1407 | constexpr int checksumMod = 256; |
| 1408 | constexpr uint8_t modVal = 0xFF; |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1409 | int sum = std::accumulate(iter, end, 0); |
| 1410 | int checksum = (checksumMod - sum) & modVal; |
| 1411 | return static_cast<uint8_t>(checksum); |
| 1412 | } |
| 1413 | uint8_t calculateChecksum(std::vector<uint8_t>& fruAreaData) |
| 1414 | { |
| 1415 | return calculateChecksum(fruAreaData.begin(), fruAreaData.end()); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | // Update new fru area length & |
| 1419 | // Update checksum at new checksum location |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1420 | // Return the offset of the area checksum byte |
| 1421 | static unsigned int updateFRUAreaLenAndChecksum(std::vector<uint8_t>& fruData, |
| 1422 | size_t fruAreaStart, |
| 1423 | size_t fruAreaEndOfFieldsOffset, |
| 1424 | size_t fruAreaEndOffset) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1425 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1426 | size_t traverseFRUAreaIndex = fruAreaEndOfFieldsOffset - fruAreaStart; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1427 | |
| 1428 | // fill zeros for any remaining unused space |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1429 | std::fill(fruData.begin() + fruAreaEndOfFieldsOffset, |
| 1430 | fruData.begin() + fruAreaEndOffset, 0); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1431 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1432 | size_t mod = traverseFRUAreaIndex % fruBlockSize; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1433 | size_t checksumLoc; |
| 1434 | if (!mod) |
| 1435 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1436 | traverseFRUAreaIndex += (fruBlockSize); |
| 1437 | checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - 1); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1438 | } |
| 1439 | else |
| 1440 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1441 | traverseFRUAreaIndex += (fruBlockSize - mod); |
| 1442 | checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - mod - 1); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1443 | } |
| 1444 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1445 | size_t newFRUAreaLen = (traverseFRUAreaIndex / fruBlockSize) + |
| 1446 | ((traverseFRUAreaIndex % fruBlockSize) != 0); |
| 1447 | size_t fruAreaLengthLoc = fruAreaStart + 1; |
| 1448 | fruData[fruAreaLengthLoc] = static_cast<uint8_t>(newFRUAreaLen); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1449 | |
| 1450 | // Calculate new checksum |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1451 | std::vector<uint8_t> finalFRUData; |
| 1452 | std::copy_n(fruData.begin() + fruAreaStart, checksumLoc - fruAreaStart, |
| 1453 | std::back_inserter(finalFRUData)); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1454 | |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1455 | fruData[checksumLoc] = calculateChecksum(finalFRUData); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1456 | return checksumLoc; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1457 | } |
| 1458 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1459 | static ssize_t getFieldLength(uint8_t fruFieldTypeLenValue) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1460 | { |
| 1461 | constexpr uint8_t typeLenMask = 0x3F; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1462 | constexpr uint8_t endOfFields = 0xC1; |
| 1463 | if (fruFieldTypeLenValue == endOfFields) |
| 1464 | { |
| 1465 | return -1; |
| 1466 | } |
| 1467 | else |
| 1468 | { |
| 1469 | return fruFieldTypeLenValue & typeLenMask; |
| 1470 | } |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | // Details with example of Asset Tag Update |
| 1474 | // To find location of Product Info Area asset tag as per FRU specification |
| 1475 | // 1. Find product Info area starting offset (*8 - as header will be in |
| 1476 | // multiple of 8 bytes). |
| 1477 | // 2. Skip 3 bytes of product info area (like format version, area length, |
| 1478 | // and language code). |
| 1479 | // 3. Traverse manufacturer name, product name, product version, & product |
| 1480 | // serial number, by reading type/length code to reach the Asset Tag. |
| 1481 | // 4. Update the Asset Tag, reposition the product Info area in multiple of |
| 1482 | // 8 bytes. Update the Product area length and checksum. |
| 1483 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1484 | bool updateFRUProperty( |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1485 | const std::string& updatePropertyReq, uint32_t bus, uint32_t address, |
| 1486 | std::string propertyName, |
| 1487 | boost::container::flat_map< |
| 1488 | std::pair<size_t, size_t>, |
| 1489 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap) |
| 1490 | { |
| 1491 | size_t updatePropertyReqLen = updatePropertyReq.length(); |
| 1492 | if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63) |
| 1493 | { |
| 1494 | std::cerr |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1495 | << "FRU field data cannot be of 1 char or more than 63 chars. " |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1496 | "Invalid Length " |
| 1497 | << updatePropertyReqLen << "\n"; |
| 1498 | return false; |
| 1499 | } |
| 1500 | |
| 1501 | std::vector<uint8_t> fruData; |
| 1502 | try |
| 1503 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1504 | fruData = getFRUInfo(static_cast<uint8_t>(bus), |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1505 | static_cast<uint8_t>(address)); |
| 1506 | } |
| 1507 | catch (std::invalid_argument& e) |
| 1508 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1509 | std::cerr << "Failure getting FRU Info" << e.what() << "\n"; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1510 | return false; |
| 1511 | } |
| 1512 | |
| 1513 | if (fruData.empty()) |
| 1514 | { |
| 1515 | return false; |
| 1516 | } |
| 1517 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1518 | const std::vector<std::string>* fruAreaFieldNames; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1519 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1520 | uint8_t fruAreaOffsetFieldValue = 0; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1521 | size_t offset = 0; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1522 | std::string areaName = propertyName.substr(0, propertyName.find("_")); |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1523 | std::string propertyNamePrefix = areaName + "_"; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1524 | auto it = std::find(FRU_AREA_NAMES.begin(), FRU_AREA_NAMES.end(), areaName); |
| 1525 | if (it == FRU_AREA_NAMES.end()) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1526 | { |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1527 | std::cerr << "Can't parse area name for property " << propertyName |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1528 | << " \n"; |
| 1529 | return false; |
| 1530 | } |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1531 | fruAreas fruAreaToUpdate = |
| 1532 | static_cast<fruAreas>(it - FRU_AREA_NAMES.begin()); |
| 1533 | fruAreaOffsetFieldValue = |
| 1534 | fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)]; |
| 1535 | switch (fruAreaToUpdate) |
| 1536 | { |
| 1537 | case fruAreas::fruAreaChassis: |
| 1538 | offset = 3; // chassis part number offset. Skip fixed first 3 bytes |
| 1539 | fruAreaFieldNames = &CHASSIS_FRU_AREAS; |
| 1540 | break; |
| 1541 | case fruAreas::fruAreaBoard: |
| 1542 | offset = 6; // board manufacturer offset. Skip fixed first 6 bytes |
| 1543 | fruAreaFieldNames = &BOARD_FRU_AREAS; |
| 1544 | break; |
| 1545 | case fruAreas::fruAreaProduct: |
| 1546 | // Manufacturer name offset. Skip fixed first 3 product fru bytes |
| 1547 | // i.e. version, area length and language code |
| 1548 | offset = 3; |
| 1549 | fruAreaFieldNames = &PRODUCT_FRU_AREAS; |
| 1550 | break; |
| 1551 | default: |
| 1552 | std::cerr << "Don't know how to handle property " << propertyName |
| 1553 | << " \n"; |
| 1554 | return false; |
| 1555 | } |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1556 | if (fruAreaOffsetFieldValue == 0) |
| 1557 | { |
| 1558 | std::cerr << "FRU Area for " << propertyName << " not present \n"; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1559 | return false; |
| 1560 | } |
| 1561 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1562 | size_t fruAreaStart = fruAreaOffsetFieldValue * fruBlockSize; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1563 | size_t fruAreaSize = fruData[fruAreaStart + 1] * fruBlockSize; |
| 1564 | size_t fruAreaEnd = fruAreaStart + fruAreaSize; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1565 | size_t fruDataIter = fruAreaStart + offset; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1566 | size_t fruUpdateFieldLoc = fruDataIter; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1567 | size_t skipToFRUUpdateField = 0; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1568 | ssize_t fieldLength; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1569 | |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1570 | bool found = false; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1571 | for (auto& field : *fruAreaFieldNames) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1572 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1573 | skipToFRUUpdateField++; |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1574 | if (propertyName == propertyNamePrefix + field) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1575 | { |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1576 | found = true; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1577 | break; |
| 1578 | } |
| 1579 | } |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1580 | if (!found) |
| 1581 | { |
| 1582 | std::size_t pos = propertyName.find(FRU_CUSTOM_FIELD_NAME); |
| 1583 | if (pos == std::string::npos) |
| 1584 | { |
| 1585 | std::cerr << "PropertyName doesn't exist in FRU Area Vectors: " |
| 1586 | << propertyName << "\n"; |
| 1587 | return false; |
| 1588 | } |
| 1589 | std::string fieldNumStr = |
| 1590 | propertyName.substr(pos + FRU_CUSTOM_FIELD_NAME.length()); |
| 1591 | size_t fieldNum = std::stoi(fieldNumStr); |
| 1592 | if (fieldNum == 0) |
| 1593 | { |
| 1594 | std::cerr << "PropertyName not recognized: " << propertyName |
| 1595 | << "\n"; |
| 1596 | return false; |
| 1597 | } |
| 1598 | skipToFRUUpdateField += fieldNum; |
| 1599 | } |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1600 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1601 | for (size_t i = 1; i < skipToFRUUpdateField; i++) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1602 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1603 | fieldLength = getFieldLength(fruData[fruDataIter]); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1604 | if (fieldLength < 0) |
| 1605 | { |
| 1606 | break; |
| 1607 | } |
| 1608 | fruDataIter += 1 + fieldLength; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1609 | } |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1610 | fruUpdateFieldLoc = fruDataIter; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1611 | |
| 1612 | // Push post update fru field bytes to a vector |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1613 | fieldLength = getFieldLength(fruData[fruUpdateFieldLoc]); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1614 | if (fieldLength < 0) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1615 | { |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1616 | std::cerr << "Property " << propertyName << " not present \n"; |
| 1617 | return false; |
| 1618 | } |
| 1619 | fruDataIter += 1 + fieldLength; |
| 1620 | size_t restFRUFieldsLoc = fruDataIter; |
| 1621 | size_t endOfFieldsLoc = 0; |
| 1622 | while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0) |
| 1623 | { |
| 1624 | if (fruDataIter >= (fruAreaStart + fruAreaSize)) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1625 | { |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1626 | fruDataIter = fruAreaStart + fruAreaSize; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1627 | break; |
| 1628 | } |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1629 | fruDataIter += 1 + fieldLength; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1630 | } |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1631 | endOfFieldsLoc = fruDataIter; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1632 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1633 | std::vector<uint8_t> restFRUAreaFieldsData; |
| 1634 | std::copy_n(fruData.begin() + restFRUFieldsLoc, |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1635 | endOfFieldsLoc - restFRUFieldsLoc + 1, |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1636 | std::back_inserter(restFRUAreaFieldsData)); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1637 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1638 | // Push post update fru areas if any |
| 1639 | unsigned int nextFRUAreaLoc = 0; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1640 | for (fruAreas nextFRUArea = fruAreas::fruAreaInternal; |
| 1641 | nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea) |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1642 | { |
| 1643 | unsigned int fruAreaLoc = |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1644 | fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1645 | if ((fruAreaLoc > endOfFieldsLoc) && |
| 1646 | ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc))) |
| 1647 | { |
| 1648 | nextFRUAreaLoc = fruAreaLoc; |
| 1649 | } |
| 1650 | } |
| 1651 | std::vector<uint8_t> restFRUAreasData; |
| 1652 | if (nextFRUAreaLoc) |
| 1653 | { |
| 1654 | std::copy_n(fruData.begin() + nextFRUAreaLoc, |
| 1655 | fruData.size() - nextFRUAreaLoc, |
| 1656 | std::back_inserter(restFRUAreasData)); |
| 1657 | } |
| 1658 | |
| 1659 | // check FRU area size |
| 1660 | size_t fruAreaDataSize = |
| 1661 | ((fruUpdateFieldLoc - fruAreaStart + 1) + restFRUAreaFieldsData.size()); |
| 1662 | size_t fruAreaAvailableSize = fruAreaSize - fruAreaDataSize; |
| 1663 | if ((updatePropertyReqLen + 1) > fruAreaAvailableSize) |
| 1664 | { |
| 1665 | |
| 1666 | #ifdef ENABLE_FRU_AREA_RESIZE |
| 1667 | size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1; |
| 1668 | // round size to 8-byte blocks |
| 1669 | newFRUAreaSize = |
| 1670 | ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize; |
| 1671 | size_t newFRUDataSize = fruData.size() + newFRUAreaSize - fruAreaSize; |
| 1672 | fruData.resize(newFRUDataSize); |
| 1673 | fruAreaSize = newFRUAreaSize; |
| 1674 | fruAreaEnd = fruAreaStart + fruAreaSize; |
| 1675 | #else |
| 1676 | std::cerr << "FRU field length: " << updatePropertyReqLen + 1 |
| 1677 | << " should not be greater than available FRU area size: " |
| 1678 | << fruAreaAvailableSize << "\n"; |
| 1679 | return false; |
| 1680 | #endif // ENABLE_FRU_AREA_RESIZE |
| 1681 | } |
| 1682 | |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1683 | // write new requested property field length and data |
| 1684 | constexpr uint8_t newTypeLenMask = 0xC0; |
| 1685 | fruData[fruUpdateFieldLoc] = |
| 1686 | static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask); |
| 1687 | fruUpdateFieldLoc++; |
| 1688 | std::copy(updatePropertyReq.begin(), updatePropertyReq.end(), |
| 1689 | fruData.begin() + fruUpdateFieldLoc); |
| 1690 | |
| 1691 | // Copy remaining data to main fru area - post updated fru field vector |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1692 | restFRUFieldsLoc = fruUpdateFieldLoc + updatePropertyReqLen; |
| 1693 | size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size(); |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1694 | std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(), |
| 1695 | fruData.begin() + restFRUFieldsLoc); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1696 | |
| 1697 | // Update final fru with new fru area length and checksum |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1698 | unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum( |
| 1699 | fruData, fruAreaStart, fruAreaDataEnd, fruAreaEnd); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1700 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1701 | #ifdef ENABLE_FRU_AREA_RESIZE |
| 1702 | ++nextFRUAreaNewLoc; |
| 1703 | ssize_t nextFRUAreaOffsetDiff = |
| 1704 | (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize; |
| 1705 | // Append rest FRU Areas if size changed and there were other sections after |
| 1706 | // updated one |
| 1707 | if (nextFRUAreaOffsetDiff && nextFRUAreaLoc) |
| 1708 | { |
| 1709 | std::copy(restFRUAreasData.begin(), restFRUAreasData.end(), |
| 1710 | fruData.begin() + nextFRUAreaNewLoc); |
| 1711 | // Update Common Header |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1712 | for (int fruArea = fruAreaInternal; fruArea <= fruAreaMultirecord; |
| 1713 | fruArea++) |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1714 | { |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1715 | unsigned int fruAreaOffsetField = getHeaderAreaFieldOffset(fruArea); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1716 | size_t curFRUAreaOffset = fruData[fruAreaOffsetField]; |
| 1717 | if (curFRUAreaOffset > fruAreaOffsetFieldValue) |
| 1718 | { |
| 1719 | fruData[fruAreaOffsetField] = static_cast<int8_t>( |
| 1720 | curFRUAreaOffset + nextFRUAreaOffsetDiff); |
| 1721 | } |
| 1722 | } |
| 1723 | // Calculate new checksum |
| 1724 | std::vector<uint8_t> headerFRUData; |
| 1725 | std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData)); |
| 1726 | size_t checksumVal = calculateChecksum(headerFRUData); |
| 1727 | fruData[7] = static_cast<uint8_t>(checksumVal); |
| 1728 | // fill zeros if FRU Area size decreased |
| 1729 | if (nextFRUAreaOffsetDiff < 0) |
| 1730 | { |
| 1731 | std::fill(fruData.begin() + nextFRUAreaNewLoc + |
| 1732 | restFRUAreasData.size(), |
| 1733 | fruData.end(), 0); |
| 1734 | } |
| 1735 | } |
| 1736 | #else |
| 1737 | // this is to avoid "unused variable" warning |
| 1738 | (void)nextFRUAreaNewLoc; |
| 1739 | #endif // ENABLE_FRU_AREA_RESIZE |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1740 | if (fruData.empty()) |
| 1741 | { |
| 1742 | return false; |
| 1743 | } |
| 1744 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1745 | if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address), |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1746 | fruData)) |
| 1747 | { |
| 1748 | return false; |
| 1749 | } |
| 1750 | |
| 1751 | // Rescan the bus so that GetRawFru dbus-call fetches updated values |
| 1752 | rescanBusses(busMap, dbusInterfaceMap); |
| 1753 | return true; |
| 1754 | } |
| 1755 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1756 | int main() |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1757 | { |
| 1758 | auto devDir = fs::path("/dev/"); |
James Feist | c9dff1b | 2019-02-13 13:33:13 -0800 | [diff] [blame] | 1759 | auto matchString = std::string(R"(i2c-\d+$)"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1760 | std::vector<fs::path> i2cBuses; |
| 1761 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 1762 | if (!findFiles(devDir, matchString, i2cBuses)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1763 | { |
| 1764 | std::cerr << "unable to find i2c devices\n"; |
| 1765 | return 1; |
| 1766 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1767 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 1768 | // check for and load blacklist with initial buses. |
| 1769 | loadBlacklist(blacklistPath); |
| 1770 | |
Vijay Khemka | 065f6d9 | 2018-12-18 10:37:47 -0800 | [diff] [blame] | 1771 | systemBus->request_name("xyz.openbmc_project.FruDevice"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1772 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1773 | // this is a map with keys of pair(bus number, address) and values of |
| 1774 | // the object on dbus |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1775 | boost::container::flat_map<std::pair<size_t, size_t>, |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1776 | std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 1777 | dbusInterfaceMap; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1778 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1779 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 1780 | objServer.add_interface("/xyz/openbmc_project/FruDevice", |
| 1781 | "xyz.openbmc_project.FruDeviceManager"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1782 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1783 | iface->register_method("ReScan", |
| 1784 | [&]() { rescanBusses(busMap, dbusInterfaceMap); }); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1785 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1786 | iface->register_method("ReScanBus", [&](uint8_t bus) { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1787 | rescanOneBus(busMap, bus, dbusInterfaceMap, true); |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1788 | }); |
| 1789 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1790 | iface->register_method("GetRawFru", getFRUInfo); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1791 | |
| 1792 | iface->register_method("WriteFru", [&](const uint8_t bus, |
| 1793 | const uint8_t address, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1794 | const std::vector<uint8_t>& data) { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1795 | if (!writeFRU(bus, address, data)) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1796 | { |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1797 | throw std::invalid_argument("Invalid Arguments."); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1798 | return; |
| 1799 | } |
| 1800 | // schedule rescan on success |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1801 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1802 | }); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1803 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1804 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1805 | std::function<void(sdbusplus::message::message & message)> eventHandler = |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1806 | [&](sdbusplus::message::message& message) { |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1807 | std::string objectName; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1808 | boost::container::flat_map< |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1809 | std::string, |
| 1810 | std::variant<std::string, bool, int64_t, uint64_t, double>> |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1811 | values; |
| 1812 | message.read(objectName, values); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1813 | auto findState = values.find("CurrentHostState"); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1814 | if (findState != values.end()) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1815 | { |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 1816 | powerIsOn = boost::ends_with( |
| 1817 | std::get<std::string>(findState->second), "Running"); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1818 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1819 | |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 1820 | if (powerIsOn) |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1821 | { |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1822 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1823 | } |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1824 | }; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1825 | |
| 1826 | sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1827 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
James Feist | 7bcd3f2 | 2019-03-18 16:04:04 -0700 | [diff] [blame] | 1828 | "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/" |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1829 | "openbmc_project/state/" |
| 1830 | "host0',arg0='xyz.openbmc_project.State.Host'", |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1831 | eventHandler); |
| 1832 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1833 | int fd = inotify_init(); |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 1834 | inotify_add_watch(fd, I2C_DEV_LOCATION, |
| 1835 | IN_CREATE | IN_MOVED_TO | IN_DELETE); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1836 | std::array<char, 4096> readBuffer; |
| 1837 | std::string pendingBuffer; |
| 1838 | // monitor for new i2c devices |
| 1839 | boost::asio::posix::stream_descriptor dirWatch(io, fd); |
| 1840 | std::function<void(const boost::system::error_code, std::size_t)> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1841 | watchI2cBusses = [&](const boost::system::error_code& ec, |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1842 | std::size_t bytes_transferred) { |
| 1843 | if (ec) |
| 1844 | { |
| 1845 | std::cout << "Callback Error " << ec << "\n"; |
| 1846 | return; |
| 1847 | } |
| 1848 | pendingBuffer += std::string(readBuffer.data(), bytes_transferred); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1849 | while (pendingBuffer.size() > sizeof(inotify_event)) |
| 1850 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1851 | const inotify_event* iEvent = |
| 1852 | reinterpret_cast<const inotify_event*>( |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1853 | pendingBuffer.data()); |
| 1854 | switch (iEvent->mask) |
| 1855 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1856 | case IN_CREATE: |
| 1857 | case IN_MOVED_TO: |
| 1858 | case IN_DELETE: |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1859 | std::string name(iEvent->name); |
| 1860 | if (boost::starts_with(name, "i2c")) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1861 | { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1862 | int bus = busStrToInt(name); |
| 1863 | if (bus < 0) |
| 1864 | { |
| 1865 | std::cerr << "Could not parse bus " << name |
| 1866 | << "\n"; |
| 1867 | continue; |
| 1868 | } |
| 1869 | rescanOneBus(busMap, static_cast<uint8_t>(bus), |
| 1870 | dbusInterfaceMap, false); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1871 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len); |
| 1875 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1876 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1877 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), |
| 1878 | watchI2cBusses); |
| 1879 | }; |
| 1880 | |
| 1881 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses); |
Gunnar Mills | b3e42fe | 2018-06-13 15:48:27 -0500 | [diff] [blame] | 1882 | // run the initial scan |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1883 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1884 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1885 | io.run(); |
| 1886 | return 0; |
| 1887 | } |