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 | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 22 | #include <sys/inotify.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 25 | #include <boost/algorithm/string/predicate.hpp> |
Brad Bishop | 82c84bb | 2020-08-26 14:12:50 -0400 | [diff] [blame] | 26 | #include <boost/asio/deadline_timer.hpp> |
| 27 | #include <boost/asio/io_service.hpp> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 28 | #include <boost/container/flat_map.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 29 | #include <nlohmann/json.hpp> |
| 30 | #include <sdbusplus/asio/connection.hpp> |
| 31 | #include <sdbusplus/asio/object_server.hpp> |
| 32 | |
| 33 | #include <array> |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 34 | #include <cerrno> |
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; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 60 | static constexpr bool debug = false; |
| 61 | static size_t unknownBusObjectCount = 0; |
| 62 | constexpr size_t maxFruSize = 512; |
| 63 | constexpr size_t maxEepromPageIndex = 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 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 68 | const static constexpr char* baseboardFruLocation = |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 69 | "/etc/fru/baseboard.fru.bin"; |
| 70 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 71 | const static constexpr char* i2CDevLocation = "/dev"; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 72 | |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 73 | using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 74 | using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>; |
| 75 | |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 76 | static std::set<size_t> busBlacklist; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 77 | struct FindDevicesWithCallback; |
| 78 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 79 | static BusMap busMap; |
| 80 | |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 81 | static bool powerIsOn = false; |
| 82 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 83 | static boost::container::flat_map< |
| 84 | std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 85 | foundDevices; |
| 86 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 87 | static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses; |
| 88 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 89 | boost::asio::io_service io; |
| 90 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 91 | auto objServer = sdbusplus::asio::object_server(systemBus); |
| 92 | |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 93 | uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter, |
| 94 | std::vector<uint8_t>::const_iterator end); |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 95 | bool updateFRUProperty( |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 96 | const std::string& assetTag, uint32_t bus, uint32_t address, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 97 | const std::string& propertyName, |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 98 | boost::container::flat_map< |
| 99 | std::pair<size_t, size_t>, |
| 100 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 101 | |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 102 | // Given a bus/address, produce the path in sysfs for an eeprom. |
| 103 | static std::string getEepromPath(size_t bus, size_t address) |
| 104 | { |
| 105 | std::stringstream output; |
| 106 | output << "/sys/bus/i2c/devices/" << bus << "-" << std::right |
| 107 | << std::setfill('0') << std::setw(4) << std::hex << address |
| 108 | << "/eeprom"; |
| 109 | return output.str(); |
| 110 | } |
| 111 | |
| 112 | static bool hasEepromFile(size_t bus, size_t address) |
| 113 | { |
| 114 | auto path = getEepromPath(bus, address); |
| 115 | try |
| 116 | { |
| 117 | return fs::exists(path); |
| 118 | } |
| 119 | catch (...) |
| 120 | { |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 125 | static int64_t readFromEeprom(int flag __attribute__((unused)), int fd, |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 126 | uint16_t address __attribute__((unused)), |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 127 | uint16_t offset, uint8_t len, uint8_t* buf) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 128 | { |
| 129 | auto result = lseek(fd, offset, SEEK_SET); |
| 130 | if (result < 0) |
| 131 | { |
| 132 | std::cerr << "failed to seek\n"; |
| 133 | return -1; |
| 134 | } |
| 135 | |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 136 | return read(fd, buf, len); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 137 | } |
| 138 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 139 | static int busStrToInt(const std::string& busName) |
| 140 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 141 | auto findBus = busName.rfind('-'); |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 142 | if (findBus == std::string::npos) |
| 143 | { |
| 144 | return -1; |
| 145 | } |
| 146 | return std::stoi(busName.substr(findBus + 1)); |
| 147 | } |
| 148 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 149 | static int getRootBus(size_t bus) |
| 150 | { |
| 151 | auto ec = std::error_code(); |
| 152 | auto path = std::filesystem::read_symlink( |
| 153 | std::filesystem::path("/sys/bus/i2c/devices/i2c-" + |
| 154 | std::to_string(bus) + "/mux_device"), |
| 155 | ec); |
| 156 | if (ec) |
| 157 | { |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | std::string filename = path.filename(); |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 162 | auto findBus = filename.find('-'); |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 163 | if (findBus == std::string::npos) |
| 164 | { |
| 165 | return -1; |
| 166 | } |
| 167 | return std::stoi(filename.substr(0, findBus)); |
| 168 | } |
| 169 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 170 | static bool isMuxBus(size_t bus) |
| 171 | { |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 172 | return is_symlink(std::filesystem::path( |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 173 | "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device")); |
| 174 | } |
| 175 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 176 | static void makeProbeInterface(size_t bus, size_t address) |
| 177 | { |
| 178 | if (isMuxBus(bus)) |
| 179 | { |
| 180 | return; // the mux buses are random, no need to publish |
| 181 | } |
| 182 | auto [it, success] = foundDevices.emplace( |
| 183 | std::make_pair(bus, address), |
| 184 | objServer.add_interface( |
| 185 | "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" + |
| 186 | std::to_string(address), |
| 187 | "xyz.openbmc_project.Inventory.Item.I2CDevice")); |
| 188 | if (!success) |
| 189 | { |
| 190 | return; // already added |
| 191 | } |
| 192 | it->second->register_property("Bus", bus); |
| 193 | it->second->register_property("Address", address); |
| 194 | it->second->initialize(); |
| 195 | } |
| 196 | |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 197 | static int isDevice16Bit(int file) |
| 198 | { |
| 199 | /* Get first byte */ |
| 200 | int byte1 = i2c_smbus_read_byte_data(file, 0); |
| 201 | if (byte1 < 0) |
| 202 | { |
| 203 | return byte1; |
| 204 | } |
| 205 | /* Read 7 more bytes, it will read same first byte in case of |
| 206 | * 8 bit but it will read next byte in case of 16 bit |
| 207 | */ |
| 208 | for (int i = 0; i < 7; i++) |
| 209 | { |
| 210 | int byte2 = i2c_smbus_read_byte_data(file, 0); |
| 211 | if (byte2 < 0) |
| 212 | { |
| 213 | return byte2; |
| 214 | } |
| 215 | if (byte2 != byte1) |
| 216 | { |
| 217 | return 1; |
| 218 | } |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 223 | // Issue an I2C transaction to first write to_slave_buf_len bytes,then read |
| 224 | // from_slave_buf_len bytes. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 225 | static int i2cSmbusWriteThenRead(int file, uint16_t address, |
| 226 | uint8_t* toSlaveBuf, uint8_t toSlaveBufLen, |
| 227 | uint8_t* fromSlaveBuf, uint8_t fromSlaveBufLen) |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 228 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 229 | if (toSlaveBuf == nullptr || toSlaveBufLen == 0 || |
| 230 | fromSlaveBuf == nullptr || fromSlaveBufLen == 0) |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 231 | { |
| 232 | return -1; |
| 233 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 234 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 235 | #define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2 |
| 236 | struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT]; |
| 237 | struct i2c_rdwr_ioctl_data rdwr; |
| 238 | |
| 239 | msgs[0].addr = address; |
| 240 | msgs[0].flags = 0; |
| 241 | msgs[0].len = toSlaveBufLen; |
| 242 | msgs[0].buf = toSlaveBuf; |
| 243 | msgs[1].addr = address; |
| 244 | msgs[1].flags = I2C_M_RD; |
| 245 | msgs[1].len = fromSlaveBufLen; |
| 246 | msgs[1].buf = fromSlaveBuf; |
| 247 | |
| 248 | rdwr.msgs = msgs; |
| 249 | rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT; |
| 250 | |
| 251 | int ret = ioctl(file, I2C_RDWR, &rdwr); |
| 252 | |
| 253 | return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? ret : -1; |
| 254 | } |
| 255 | |
| 256 | static int64_t readBlockData(int flag, int file, uint16_t address, |
| 257 | uint16_t offset, uint8_t len, uint8_t* buf) |
| 258 | { |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 259 | if (flag == 0) |
| 260 | { |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 261 | return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset), |
| 262 | len, buf); |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 265 | offset = htobe16(offset); |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 266 | return i2cSmbusWriteThenRead( |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 267 | file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len); |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 270 | // TODO: This code is very similar to the non-eeprom version and can be merged |
| 271 | // with some tweaks. |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 272 | static std::vector<uint8_t> processEeprom(int bus, int address) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 273 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 274 | auto path = getEepromPath(bus, address); |
| 275 | |
| 276 | int file = open(path.c_str(), O_RDONLY); |
| 277 | if (file < 0) |
| 278 | { |
| 279 | std::cerr << "Unable to open eeprom file: " << path << "\n"; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 280 | return {}; |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 283 | std::string errorMessage = "eeprom at " + std::to_string(bus) + |
| 284 | " address " + std::to_string(address); |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 285 | std::vector<uint8_t> device = readFRUContents( |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 286 | 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 287 | |
| 288 | close(file); |
| 289 | return device; |
| 290 | } |
| 291 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 292 | std::set<int> findI2CEeproms(int i2cBus, |
| 293 | const std::shared_ptr<DeviceMap>& devices) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 294 | { |
| 295 | std::set<int> foundList; |
| 296 | |
| 297 | std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus); |
| 298 | |
| 299 | // For each file listed under the i2c device |
| 300 | // NOTE: This should be faster than just checking for each possible address |
| 301 | // path. |
| 302 | for (const auto& p : fs::directory_iterator(path)) |
| 303 | { |
| 304 | const std::string node = p.path().string(); |
| 305 | std::smatch m; |
| 306 | bool found = |
| 307 | std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)")); |
| 308 | |
| 309 | if (!found) |
| 310 | { |
| 311 | continue; |
| 312 | } |
| 313 | if (m.size() != 2) |
| 314 | { |
| 315 | std::cerr << "regex didn't capture\n"; |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | std::ssub_match subMatch = m[1]; |
| 320 | std::string addressString = subMatch.str(); |
| 321 | |
| 322 | std::size_t ignored; |
| 323 | const int hexBase = 16; |
| 324 | int address = std::stoi(addressString, &ignored, hexBase); |
| 325 | |
| 326 | const std::string eeprom = node + "/eeprom"; |
| 327 | |
| 328 | try |
| 329 | { |
| 330 | if (!fs::exists(eeprom)) |
| 331 | { |
| 332 | continue; |
| 333 | } |
| 334 | } |
| 335 | catch (...) |
| 336 | { |
| 337 | continue; |
| 338 | } |
| 339 | |
| 340 | // There is an eeprom file at this address, it may have invalid |
| 341 | // contents, but we found it. |
| 342 | foundList.insert(address); |
| 343 | |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 344 | std::vector<uint8_t> device = processEeprom(i2cBus, address); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 345 | if (!device.empty()) |
| 346 | { |
| 347 | devices->emplace(address, device); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | return foundList; |
| 352 | } |
| 353 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 354 | int getBusFRUs(int file, int first, int last, int bus, |
Patrick Venture | 4f47fe6 | 2019-08-08 16:30:38 -0700 | [diff] [blame] | 355 | std::shared_ptr<DeviceMap> devices) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 356 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 357 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 358 | std::future<int> future = std::async(std::launch::async, [&]() { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 359 | // NOTE: When reading the devices raw on the bus, it can interfere with |
| 360 | // the driver's ability to operate, therefore read eeproms first before |
| 361 | // scanning for devices without drivers. Several experiments were run |
| 362 | // and it was determined that if there were any devices on the bus |
| 363 | // before the eeprom was hit and read, the eeprom driver wouldn't open |
| 364 | // while the bus device was open. An experiment was not performed to see |
| 365 | // if this issue was resolved if the i2c bus device was closed, but |
| 366 | // hexdumps of the eeprom later were successful. |
| 367 | |
| 368 | // Scan for i2c eeproms loaded on this bus. |
| 369 | std::set<int> skipList = findI2CEeproms(bus, devices); |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 370 | std::set<size_t>& failedItems = failedAddresses[bus]; |
| 371 | |
| 372 | std::set<size_t>* rootFailures = nullptr; |
| 373 | int rootBus = getRootBus(bus); |
| 374 | |
| 375 | if (rootBus >= 0) |
| 376 | { |
| 377 | rootFailures = &(failedAddresses[rootBus]); |
| 378 | } |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 379 | |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 380 | constexpr int startSkipSlaveAddr = 0; |
| 381 | constexpr int endSkipSlaveAddr = 12; |
| 382 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 383 | for (int ii = first; ii <= last; ii++) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 384 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 385 | if (skipList.find(ii) != skipList.end()) |
| 386 | { |
| 387 | continue; |
| 388 | } |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 389 | // skipping since no device is present in this range |
| 390 | if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr) |
| 391 | { |
| 392 | continue; |
| 393 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 394 | // Set slave address |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 395 | if (ioctl(file, I2C_SLAVE, ii) < 0) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 396 | { |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 397 | std::cerr << "device at bus " << bus << " address " << ii |
Patrick Venture | 5d8b61d | 2019-08-06 12:36:10 -0700 | [diff] [blame] | 398 | << " busy\n"; |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 399 | continue; |
| 400 | } |
| 401 | // probe |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 402 | if (i2c_smbus_read_byte(file) < 0) |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 403 | { |
| 404 | continue; |
| 405 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 406 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 407 | if (debug) |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 408 | { |
Patrick Venture | 98e0cf3 | 2019-08-02 11:11:03 -0700 | [diff] [blame] | 409 | std::cout << "something at bus " << bus << " addr " << ii |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 410 | << "\n"; |
| 411 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 412 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 413 | makeProbeInterface(bus, ii); |
| 414 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 415 | if (failedItems.find(ii) != failedItems.end()) |
| 416 | { |
| 417 | // if we failed to read it once, unlikely we can read it later |
| 418 | continue; |
| 419 | } |
| 420 | |
| 421 | if (rootFailures != nullptr) |
| 422 | { |
| 423 | if (rootFailures->find(ii) != rootFailures->end()) |
| 424 | { |
| 425 | continue; |
| 426 | } |
| 427 | } |
| 428 | |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 429 | /* Check for Device type if it is 8 bit or 16 bit */ |
| 430 | int flag = isDevice16Bit(file); |
| 431 | if (flag < 0) |
| 432 | { |
| 433 | std::cerr << "failed to read bus " << bus << " address " << ii |
| 434 | << "\n"; |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 435 | if (powerIsOn) |
| 436 | { |
| 437 | failedItems.insert(ii); |
| 438 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 439 | continue; |
| 440 | } |
| 441 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 442 | std::string errorMessage = |
| 443 | "bus " + std::to_string(bus) + " address " + std::to_string(ii); |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 444 | std::vector<uint8_t> device = |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 445 | readFRUContents(flag, file, static_cast<uint16_t>(ii), |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 446 | readBlockData, errorMessage); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 447 | if (device.empty()) |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 448 | { |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 449 | continue; |
| 450 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 451 | |
Patrick Venture | 786f179 | 2019-08-05 16:33:44 -0700 | [diff] [blame] | 452 | devices->emplace(ii, device); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 453 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 454 | return 1; |
| 455 | }); |
| 456 | std::future_status status = |
| 457 | future.wait_for(std::chrono::seconds(busTimeoutSeconds)); |
| 458 | if (status == std::future_status::timeout) |
| 459 | { |
| 460 | std::cerr << "Error reading bus " << bus << "\n"; |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 461 | if (powerIsOn) |
| 462 | { |
| 463 | busBlacklist.insert(bus); |
| 464 | } |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 465 | close(file); |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 466 | return -1; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 467 | } |
| 468 | |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 469 | close(file); |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 470 | return future.get(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 473 | void loadBlacklist(const char* path) |
| 474 | { |
| 475 | std::ifstream blacklistStream(path); |
| 476 | if (!blacklistStream.good()) |
| 477 | { |
| 478 | // File is optional. |
| 479 | std::cerr << "Cannot open blacklist file.\n\n"; |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | nlohmann::json data = |
| 484 | nlohmann::json::parse(blacklistStream, nullptr, false); |
| 485 | if (data.is_discarded()) |
| 486 | { |
| 487 | std::cerr << "Illegal blacklist file detected, cannot validate JSON, " |
| 488 | "exiting\n"; |
| 489 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // It's expected to have at least one field, "buses" that is an array of the |
| 493 | // buses by integer. Allow for future options to exclude further aspects, |
| 494 | // such as specific addresses or ranges. |
| 495 | if (data.type() != nlohmann::json::value_t::object) |
| 496 | { |
| 497 | std::cerr << "Illegal blacklist, expected to read dictionary\n"; |
| 498 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // If buses field is missing, that's fine. |
| 502 | if (data.count("buses") == 1) |
| 503 | { |
| 504 | // Parse the buses array after a little validation. |
| 505 | auto buses = data.at("buses"); |
| 506 | if (buses.type() != nlohmann::json::value_t::array) |
| 507 | { |
| 508 | // Buses field present but invalid, therefore this is an error. |
| 509 | std::cerr << "Invalid contents for blacklist buses field\n"; |
| 510 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | // Catch exception here for type mis-match. |
| 514 | try |
| 515 | { |
| 516 | for (const auto& bus : buses) |
| 517 | { |
| 518 | busBlacklist.insert(bus.get<size_t>()); |
| 519 | } |
| 520 | } |
| 521 | catch (const nlohmann::detail::type_error& e) |
| 522 | { |
| 523 | // Type mis-match is a critical error. |
| 524 | std::cerr << "Invalid bus type: " << e.what() << "\n"; |
| 525 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | return; |
| 530 | } |
| 531 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 532 | static void findI2CDevices(const std::vector<fs::path>& i2cBuses, |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 533 | BusMap& busmap) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 534 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 535 | for (auto& i2cBus : i2cBuses) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 536 | { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 537 | int bus = busStrToInt(i2cBus); |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 538 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 539 | if (bus < 0) |
| 540 | { |
| 541 | std::cerr << "Cannot translate " << i2cBus << " to int\n"; |
| 542 | continue; |
| 543 | } |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 544 | if (busBlacklist.find(bus) != busBlacklist.end()) |
| 545 | { |
| 546 | continue; // skip previously failed busses |
| 547 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 548 | |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 549 | int rootBus = getRootBus(bus); |
| 550 | if (busBlacklist.find(rootBus) != busBlacklist.end()) |
| 551 | { |
| 552 | continue; |
| 553 | } |
| 554 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 555 | auto file = open(i2cBus.c_str(), O_RDWR); |
| 556 | if (file < 0) |
| 557 | { |
| 558 | std::cerr << "unable to open i2c device " << i2cBus.string() |
| 559 | << "\n"; |
| 560 | continue; |
| 561 | } |
| 562 | unsigned long funcs = 0; |
| 563 | |
| 564 | if (ioctl(file, I2C_FUNCS, &funcs) < 0) |
| 565 | { |
| 566 | std::cerr |
Patrick Venture | 98e0cf3 | 2019-08-02 11:11:03 -0700 | [diff] [blame] | 567 | << "Error: Could not get the adapter functionality matrix bus " |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 568 | << bus << "\n"; |
Zhikui Ren | c02d8cb | 2021-06-28 16:56:08 -0700 | [diff] [blame] | 569 | close(file); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 570 | continue; |
| 571 | } |
| 572 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) || |
| 573 | !(I2C_FUNC_SMBUS_READ_I2C_BLOCK)) |
| 574 | { |
| 575 | std::cerr << "Error: Can't use SMBus Receive Byte command bus " |
| 576 | << bus << "\n"; |
| 577 | continue; |
| 578 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 579 | auto& device = busmap[bus]; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 580 | device = std::make_shared<DeviceMap>(); |
| 581 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 582 | // i2cdetect by default uses the range 0x03 to 0x77, as |
| 583 | // this is what we have tested with, use this range. Could be |
| 584 | // changed in future. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 585 | if (debug) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 586 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 587 | std::cerr << "Scanning bus " << bus << "\n"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 588 | } |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 589 | |
| 590 | // fd is closed in this function in case the bus locks up |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 591 | getBusFRUs(file, 0x03, 0x77, bus, device); |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 592 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 593 | if (debug) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 594 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 595 | std::cerr << "Done scanning bus " << bus << "\n"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 596 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 597 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 598 | } |
| 599 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 600 | // this class allows an async response after all i2c devices are discovered |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 601 | struct FindDevicesWithCallback : |
| 602 | std::enable_shared_from_this<FindDevicesWithCallback> |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 603 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 604 | FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 605 | BusMap& busmap, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 606 | std::function<void(void)>&& callback) : |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 607 | _i2cBuses(i2cBuses), |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 608 | _busMap(busmap), _callback(std::move(callback)) |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 609 | {} |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 610 | ~FindDevicesWithCallback() |
| 611 | { |
| 612 | _callback(); |
| 613 | } |
| 614 | void run() |
| 615 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 616 | findI2CDevices(_i2cBuses, _busMap); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 617 | } |
| 618 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 619 | const std::vector<fs::path>& _i2cBuses; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 620 | BusMap& _busMap; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 621 | std::function<void(void)> _callback; |
| 622 | }; |
| 623 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 624 | 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] | 625 | { |
| 626 | auto deviceMap = busMap.find(bus); |
| 627 | if (deviceMap == busMap.end()) |
| 628 | { |
| 629 | throw std::invalid_argument("Invalid Bus."); |
| 630 | } |
| 631 | auto device = deviceMap->second->find(address); |
| 632 | if (device == deviceMap->second->end()) |
| 633 | { |
| 634 | throw std::invalid_argument("Invalid Address."); |
| 635 | } |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 636 | std::vector<uint8_t>& ret = device->second; |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 637 | |
| 638 | return ret; |
| 639 | } |
| 640 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 641 | void addFruObjectToDbus( |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 642 | std::vector<uint8_t>& device, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 643 | boost::container::flat_map< |
| 644 | std::pair<size_t, size_t>, |
| 645 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 646 | uint32_t bus, uint32_t address) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 647 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 648 | boost::container::flat_map<std::string, std::string> formattedFRU; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 649 | resCodes res = formatFRU(device, formattedFRU); |
| 650 | if (res == resCodes::resErr) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 651 | { |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 652 | std::cerr << "failed to parse FRU for device at bus " << bus |
Patrick Venture | b755c83 | 2019-08-07 11:09:14 -0700 | [diff] [blame] | 653 | << " address " << address << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 654 | return; |
| 655 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 656 | if (res == resCodes::resWarn) |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 657 | { |
| 658 | std::cerr << "there were warnings while parsing FRU for device at bus " |
| 659 | << bus << " address " << address << "\n"; |
| 660 | } |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 661 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 662 | auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 663 | std::string productName; |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 664 | // Not found under Board section or an empty string. |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 665 | if (productNameFind == formattedFRU.end() || |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 666 | productNameFind->second.empty()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 667 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 668 | productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 669 | } |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 670 | // Found under Product section and not an empty string. |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 671 | if (productNameFind != formattedFRU.end() && |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 672 | !productNameFind->second.empty()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 673 | { |
| 674 | productName = productNameFind->second; |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 675 | std::regex illegalObject("[^A-Za-z0-9_]"); |
| 676 | productName = std::regex_replace(productName, illegalObject, "_"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 677 | } |
| 678 | else |
| 679 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 680 | productName = "UNKNOWN" + std::to_string(unknownBusObjectCount); |
| 681 | unknownBusObjectCount++; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 682 | } |
| 683 | |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 684 | productName = "/xyz/openbmc_project/FruDevice/" + productName; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 685 | // avoid duplicates by checking to see if on a mux |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 686 | if (bus > 0) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 687 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 688 | int highest = -1; |
| 689 | bool found = false; |
| 690 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 691 | for (auto const& busIface : dbusInterfaceMap) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 692 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 693 | std::string path = busIface.second->get_object_path(); |
| 694 | if (std::regex_match(path, std::regex(productName + "(_\\d+|)$"))) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 695 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 696 | if (isMuxBus(bus) && address == busIface.first.second && |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 697 | (getFRUInfo(static_cast<uint8_t>(busIface.first.first), |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 698 | static_cast<uint8_t>(busIface.first.second)) == |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 699 | getFRUInfo(static_cast<uint8_t>(bus), |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 700 | static_cast<uint8_t>(address)))) |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 701 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 702 | // This device is already added to the lower numbered bus, |
| 703 | // do not replicate it. |
| 704 | return; |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 705 | } |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 706 | |
| 707 | // Check if the match named has extra information. |
| 708 | found = true; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 709 | std::smatch baseMatch; |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 710 | |
| 711 | bool match = std::regex_match( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 712 | path, baseMatch, std::regex(productName + "_(\\d+)$")); |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 713 | if (match) |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 714 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 715 | if (baseMatch.size() == 2) |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 716 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 717 | std::ssub_match baseSubMatch = baseMatch[1]; |
| 718 | std::string base = baseSubMatch.str(); |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 719 | |
| 720 | int value = std::stoi(base); |
| 721 | highest = (value > highest) ? value : highest; |
| 722 | } |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 723 | } |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 724 | } |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 725 | } // end searching objects |
| 726 | |
| 727 | if (found) |
| 728 | { |
| 729 | // We found something with the same name. If highest was still -1, |
| 730 | // it means this new entry will be _0. |
| 731 | productName += "_"; |
| 732 | productName += std::to_string(++highest); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 733 | } |
| 734 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 735 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 736 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 737 | objServer.add_interface(productName, "xyz.openbmc_project.FruDevice"); |
| 738 | dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface; |
| 739 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 740 | for (auto& property : formattedFRU) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 741 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 742 | |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 743 | std::regex_replace(property.second.begin(), property.second.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 744 | property.second.end(), nonAsciiRegex, "_"); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 745 | if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG") |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 746 | { |
| 747 | continue; |
| 748 | } |
| 749 | std::string key = |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 750 | std::regex_replace(property.first, nonAsciiRegex, "_"); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 751 | |
| 752 | if (property.first == "PRODUCT_ASSET_TAG") |
| 753 | { |
| 754 | std::string propertyName = property.first; |
| 755 | iface->register_property( |
| 756 | key, property.second + '\0', |
| 757 | [bus, address, propertyName, |
| 758 | &dbusInterfaceMap](const std::string& req, std::string& resp) { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 759 | if (strcmp(req.c_str(), resp.c_str()) != 0) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 760 | { |
| 761 | // call the method which will update |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 762 | if (updateFRUProperty(req, bus, address, propertyName, |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 763 | dbusInterfaceMap)) |
| 764 | { |
| 765 | resp = req; |
| 766 | } |
| 767 | else |
| 768 | { |
| 769 | throw std::invalid_argument( |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 770 | "FRU property update failed."); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | return 1; |
| 774 | }); |
| 775 | } |
| 776 | else if (!iface->register_property(key, property.second + '\0')) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 777 | { |
| 778 | std::cerr << "illegal key: " << key << "\n"; |
| 779 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 780 | if (debug) |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 781 | { |
| 782 | std::cout << property.first << ": " << property.second << "\n"; |
| 783 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 784 | } |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 785 | |
| 786 | // baseboard will be 0, 0 |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 787 | iface->register_property("BUS", bus); |
| 788 | iface->register_property("ADDRESS", address); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 789 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 790 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 793 | static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 794 | { |
| 795 | // try to read baseboard fru from file |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 796 | std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary); |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 797 | if (baseboardFRUFile.good()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 798 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 799 | baseboardFRUFile.seekg(0, std::ios_base::end); |
| 800 | size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg()); |
| 801 | baseboardFRU.resize(fileSize); |
| 802 | baseboardFRUFile.seekg(0, std::ios_base::beg); |
| 803 | baseboardFRUFile.read(reinterpret_cast<char*>(baseboardFRU.data()), |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 804 | fileSize); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 805 | } |
| 806 | else |
| 807 | { |
| 808 | return false; |
| 809 | } |
| 810 | return true; |
| 811 | } |
| 812 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 813 | 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] | 814 | { |
| 815 | boost::container::flat_map<std::string, std::string> tmp; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 816 | if (fru.size() > maxFruSize) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 817 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 818 | std::cerr << "Invalid fru.size() during writeFRU\n"; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 819 | return false; |
| 820 | } |
| 821 | // verify legal fru by running it through fru parsing logic |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame] | 822 | if (formatFRU(fru, tmp) != resCodes::resOK) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 823 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 824 | std::cerr << "Invalid fru format during writeFRU\n"; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 825 | return false; |
| 826 | } |
| 827 | // baseboard fru |
| 828 | if (bus == 0 && address == 0) |
| 829 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 830 | std::ofstream file(baseboardFruLocation, std::ios_base::binary); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 831 | if (!file.good()) |
| 832 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 833 | std::cerr << "Error opening file " << baseboardFruLocation << "\n"; |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 834 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 835 | return false; |
| 836 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 837 | file.write(reinterpret_cast<const char*>(fru.data()), fru.size()); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 838 | return file.good(); |
| 839 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 840 | |
| 841 | if (hasEepromFile(bus, address)) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 842 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 843 | auto path = getEepromPath(bus, address); |
| 844 | int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC); |
| 845 | if (eeprom < 0) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 846 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 847 | std::cerr << "unable to open i2c device " << path << "\n"; |
| 848 | throw DBusInternalError(); |
| 849 | return false; |
| 850 | } |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 851 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 852 | ssize_t writtenBytes = write(eeprom, fru.data(), fru.size()); |
| 853 | if (writtenBytes < 0) |
| 854 | { |
| 855 | std::cerr << "unable to write to i2c device " << path << "\n"; |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 856 | close(eeprom); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 857 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 858 | return false; |
| 859 | } |
| 860 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 861 | close(eeprom); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 862 | return true; |
| 863 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 864 | |
| 865 | std::string i2cBus = "/dev/i2c-" + std::to_string(bus); |
| 866 | |
| 867 | int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC); |
| 868 | if (file < 0) |
| 869 | { |
| 870 | std::cerr << "unable to open i2c device " << i2cBus << "\n"; |
| 871 | throw DBusInternalError(); |
| 872 | return false; |
| 873 | } |
| 874 | if (ioctl(file, I2C_SLAVE_FORCE, address) < 0) |
| 875 | { |
| 876 | std::cerr << "unable to set device address\n"; |
| 877 | close(file); |
| 878 | throw DBusInternalError(); |
| 879 | return false; |
| 880 | } |
| 881 | |
| 882 | constexpr const size_t retryMax = 2; |
| 883 | uint16_t index = 0; |
| 884 | size_t retries = retryMax; |
| 885 | while (index < fru.size()) |
| 886 | { |
| 887 | if ((index && ((index % (maxEepromPageIndex + 1)) == 0)) && |
| 888 | (retries == retryMax)) |
| 889 | { |
| 890 | // The 4K EEPROM only uses the A2 and A1 device address bits |
| 891 | // with the third bit being a memory page address bit. |
| 892 | if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0) |
| 893 | { |
| 894 | std::cerr << "unable to set device address\n"; |
| 895 | close(file); |
| 896 | throw DBusInternalError(); |
| 897 | return false; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index), |
| 902 | fru[index]) < 0) |
| 903 | { |
| 904 | if (!retries--) |
| 905 | { |
| 906 | std::cerr << "error writing fru: " << strerror(errno) << "\n"; |
| 907 | close(file); |
| 908 | throw DBusInternalError(); |
| 909 | return false; |
| 910 | } |
| 911 | } |
| 912 | else |
| 913 | { |
| 914 | retries = retryMax; |
| 915 | index++; |
| 916 | } |
| 917 | // most eeproms require 5-10ms between writes |
| 918 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 919 | } |
| 920 | close(file); |
| 921 | return true; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 924 | void rescanOneBus( |
| 925 | BusMap& busmap, uint8_t busNum, |
| 926 | boost::container::flat_map< |
| 927 | std::pair<size_t, size_t>, |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 928 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
| 929 | bool dbusCall) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 930 | { |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 931 | for (auto& [pair, interface] : foundDevices) |
| 932 | { |
| 933 | if (pair.first == static_cast<size_t>(busNum)) |
| 934 | { |
| 935 | objServer.remove_interface(interface); |
| 936 | foundDevices.erase(pair); |
| 937 | } |
| 938 | } |
| 939 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 940 | fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum)); |
| 941 | if (!fs::exists(busPath)) |
| 942 | { |
| 943 | if (dbusCall) |
| 944 | { |
| 945 | std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum) |
| 946 | << "\n"; |
| 947 | throw std::invalid_argument("Invalid Bus."); |
| 948 | } |
| 949 | return; |
| 950 | } |
| 951 | |
| 952 | std::vector<fs::path> i2cBuses; |
| 953 | i2cBuses.emplace_back(busPath); |
| 954 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 955 | auto scan = std::make_shared<FindDevicesWithCallback>( |
| 956 | i2cBuses, busmap, [busNum, &busmap, &dbusInterfaceMap]() { |
| 957 | for (auto& busIface : dbusInterfaceMap) |
| 958 | { |
| 959 | if (busIface.first.first == static_cast<size_t>(busNum)) |
| 960 | { |
| 961 | objServer.remove_interface(busIface.second); |
| 962 | } |
| 963 | } |
Wludzik, Jozef | c1136b7 | 2020-06-24 11:58:32 +0200 | [diff] [blame] | 964 | auto found = busmap.find(busNum); |
| 965 | if (found == busmap.end() || found->second == nullptr) |
| 966 | { |
| 967 | return; |
| 968 | } |
| 969 | for (auto& device : *(found->second)) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 970 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 971 | addFruObjectToDbus(device.second, dbusInterfaceMap, |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 972 | static_cast<uint32_t>(busNum), device.first); |
| 973 | } |
| 974 | }); |
| 975 | scan->run(); |
| 976 | } |
| 977 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 978 | void rescanBusses( |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 979 | BusMap& busmap, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 980 | boost::container::flat_map< |
| 981 | std::pair<size_t, size_t>, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 982 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 983 | { |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 984 | static boost::asio::deadline_timer timer(io); |
| 985 | timer.expires_from_now(boost::posix_time::seconds(1)); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 986 | |
Gunnar Mills | 6f0ae94 | 2018-08-31 12:38:03 -0500 | [diff] [blame] | 987 | // setup an async wait in case we get flooded with requests |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 988 | timer.async_wait([&](const boost::system::error_code&) { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 989 | auto devDir = fs::path("/dev/"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 990 | std::vector<fs::path> i2cBuses; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 991 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 992 | boost::container::flat_map<size_t, fs::path> busPaths; |
| 993 | if (!getI2cDevicePaths(devDir, busPaths)) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 994 | { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 995 | std::cerr << "unable to find i2c devices\n"; |
| 996 | return; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 997 | } |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 998 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 999 | for (const auto& busPath : busPaths) |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1000 | { |
| 1001 | i2cBuses.emplace_back(busPath.second); |
| 1002 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1003 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1004 | busmap.clear(); |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 1005 | for (auto& [pair, interface] : foundDevices) |
| 1006 | { |
| 1007 | objServer.remove_interface(interface); |
| 1008 | } |
| 1009 | foundDevices.clear(); |
| 1010 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1011 | auto scan = |
| 1012 | std::make_shared<FindDevicesWithCallback>(i2cBuses, busmap, [&]() { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1013 | for (auto& busIface : dbusInterfaceMap) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1014 | { |
| 1015 | objServer.remove_interface(busIface.second); |
| 1016 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1017 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1018 | dbusInterfaceMap.clear(); |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1019 | unknownBusObjectCount = 0; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1020 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1021 | // todo, get this from a more sensable place |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1022 | std::vector<uint8_t> baseboardFRU; |
| 1023 | if (readBaseboardFRU(baseboardFRU)) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1024 | { |
Helen Huang | f69fe90 | 2021-02-19 16:18:22 +0800 | [diff] [blame] | 1025 | // If no device on i2c bus 0, the insertion will happen. |
| 1026 | auto bus0 = |
| 1027 | busmap.try_emplace(0, std::make_shared<DeviceMap>()); |
| 1028 | bus0.first->second->emplace(0, baseboardFRU); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1029 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1030 | for (auto& devicemap : busmap) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1031 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1032 | for (auto& device : *devicemap.second) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1033 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1034 | addFruObjectToDbus(device.second, dbusInterfaceMap, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1035 | devicemap.first, device.first); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | }); |
| 1039 | scan->run(); |
| 1040 | }); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1043 | // Details with example of Asset Tag Update |
| 1044 | // To find location of Product Info Area asset tag as per FRU specification |
| 1045 | // 1. Find product Info area starting offset (*8 - as header will be in |
| 1046 | // multiple of 8 bytes). |
| 1047 | // 2. Skip 3 bytes of product info area (like format version, area length, |
| 1048 | // and language code). |
| 1049 | // 3. Traverse manufacturer name, product name, product version, & product |
| 1050 | // serial number, by reading type/length code to reach the Asset Tag. |
| 1051 | // 4. Update the Asset Tag, reposition the product Info area in multiple of |
| 1052 | // 8 bytes. Update the Product area length and checksum. |
| 1053 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1054 | bool updateFRUProperty( |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1055 | const std::string& updatePropertyReq, uint32_t bus, uint32_t address, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1056 | const std::string& propertyName, |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1057 | boost::container::flat_map< |
| 1058 | std::pair<size_t, size_t>, |
| 1059 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap) |
| 1060 | { |
| 1061 | size_t updatePropertyReqLen = updatePropertyReq.length(); |
| 1062 | if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63) |
| 1063 | { |
| 1064 | std::cerr |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1065 | << "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] | 1066 | "Invalid Length " |
| 1067 | << updatePropertyReqLen << "\n"; |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | std::vector<uint8_t> fruData; |
| 1072 | try |
| 1073 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1074 | fruData = getFRUInfo(static_cast<uint8_t>(bus), |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1075 | static_cast<uint8_t>(address)); |
| 1076 | } |
| 1077 | catch (std::invalid_argument& e) |
| 1078 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1079 | std::cerr << "Failure getting FRU Info" << e.what() << "\n"; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1080 | return false; |
| 1081 | } |
| 1082 | |
| 1083 | if (fruData.empty()) |
| 1084 | { |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1088 | const std::vector<std::string>* fruAreaFieldNames; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1089 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1090 | uint8_t fruAreaOffsetFieldValue = 0; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1091 | size_t offset = 0; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1092 | std::string areaName = propertyName.substr(0, propertyName.find('_')); |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1093 | std::string propertyNamePrefix = areaName + "_"; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1094 | auto it = std::find(fruAreaNames.begin(), fruAreaNames.end(), areaName); |
| 1095 | if (it == fruAreaNames.end()) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1096 | { |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1097 | std::cerr << "Can't parse area name for property " << propertyName |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1098 | << " \n"; |
| 1099 | return false; |
| 1100 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1101 | fruAreas fruAreaToUpdate = static_cast<fruAreas>(it - fruAreaNames.begin()); |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1102 | fruAreaOffsetFieldValue = |
| 1103 | fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)]; |
| 1104 | switch (fruAreaToUpdate) |
| 1105 | { |
| 1106 | case fruAreas::fruAreaChassis: |
| 1107 | offset = 3; // chassis part number offset. Skip fixed first 3 bytes |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1108 | fruAreaFieldNames = &chassisFruAreas; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1109 | break; |
| 1110 | case fruAreas::fruAreaBoard: |
| 1111 | offset = 6; // board manufacturer offset. Skip fixed first 6 bytes |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1112 | fruAreaFieldNames = &boardFruAreas; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1113 | break; |
| 1114 | case fruAreas::fruAreaProduct: |
| 1115 | // Manufacturer name offset. Skip fixed first 3 product fru bytes |
| 1116 | // i.e. version, area length and language code |
| 1117 | offset = 3; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1118 | fruAreaFieldNames = &productFruAreas; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1119 | break; |
| 1120 | default: |
| 1121 | std::cerr << "Don't know how to handle property " << propertyName |
| 1122 | << " \n"; |
| 1123 | return false; |
| 1124 | } |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1125 | if (fruAreaOffsetFieldValue == 0) |
| 1126 | { |
| 1127 | std::cerr << "FRU Area for " << propertyName << " not present \n"; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1128 | return false; |
| 1129 | } |
| 1130 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1131 | size_t fruAreaStart = fruAreaOffsetFieldValue * fruBlockSize; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1132 | size_t fruAreaSize = fruData[fruAreaStart + 1] * fruBlockSize; |
| 1133 | size_t fruAreaEnd = fruAreaStart + fruAreaSize; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1134 | size_t fruDataIter = fruAreaStart + offset; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1135 | size_t skipToFRUUpdateField = 0; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1136 | ssize_t fieldLength; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1137 | |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1138 | bool found = false; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1139 | for (auto& field : *fruAreaFieldNames) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1140 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1141 | skipToFRUUpdateField++; |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1142 | if (propertyName == propertyNamePrefix + field) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1143 | { |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1144 | found = true; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1145 | break; |
| 1146 | } |
| 1147 | } |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1148 | if (!found) |
| 1149 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1150 | std::size_t pos = propertyName.find(fruCustomFieldName); |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1151 | if (pos == std::string::npos) |
| 1152 | { |
| 1153 | std::cerr << "PropertyName doesn't exist in FRU Area Vectors: " |
| 1154 | << propertyName << "\n"; |
| 1155 | return false; |
| 1156 | } |
| 1157 | std::string fieldNumStr = |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1158 | propertyName.substr(pos + fruCustomFieldName.length()); |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1159 | size_t fieldNum = std::stoi(fieldNumStr); |
| 1160 | if (fieldNum == 0) |
| 1161 | { |
| 1162 | std::cerr << "PropertyName not recognized: " << propertyName |
| 1163 | << "\n"; |
| 1164 | return false; |
| 1165 | } |
| 1166 | skipToFRUUpdateField += fieldNum; |
| 1167 | } |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1168 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1169 | for (size_t i = 1; i < skipToFRUUpdateField; i++) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1170 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1171 | fieldLength = getFieldLength(fruData[fruDataIter]); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1172 | if (fieldLength < 0) |
| 1173 | { |
| 1174 | break; |
| 1175 | } |
| 1176 | fruDataIter += 1 + fieldLength; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1177 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1178 | size_t fruUpdateFieldLoc = fruDataIter; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1179 | |
| 1180 | // Push post update fru field bytes to a vector |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1181 | fieldLength = getFieldLength(fruData[fruUpdateFieldLoc]); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1182 | if (fieldLength < 0) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1183 | { |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1184 | std::cerr << "Property " << propertyName << " not present \n"; |
| 1185 | return false; |
| 1186 | } |
| 1187 | fruDataIter += 1 + fieldLength; |
| 1188 | size_t restFRUFieldsLoc = fruDataIter; |
| 1189 | size_t endOfFieldsLoc = 0; |
| 1190 | while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0) |
| 1191 | { |
| 1192 | if (fruDataIter >= (fruAreaStart + fruAreaSize)) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1193 | { |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1194 | fruDataIter = fruAreaStart + fruAreaSize; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1195 | break; |
| 1196 | } |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1197 | fruDataIter += 1 + fieldLength; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1198 | } |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1199 | endOfFieldsLoc = fruDataIter; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1200 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1201 | std::vector<uint8_t> restFRUAreaFieldsData; |
| 1202 | std::copy_n(fruData.begin() + restFRUFieldsLoc, |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1203 | endOfFieldsLoc - restFRUFieldsLoc + 1, |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1204 | std::back_inserter(restFRUAreaFieldsData)); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1205 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1206 | // Push post update fru areas if any |
| 1207 | unsigned int nextFRUAreaLoc = 0; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1208 | for (fruAreas nextFRUArea = fruAreas::fruAreaInternal; |
| 1209 | nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea) |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1210 | { |
| 1211 | unsigned int fruAreaLoc = |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1212 | fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1213 | if ((fruAreaLoc > endOfFieldsLoc) && |
| 1214 | ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc))) |
| 1215 | { |
| 1216 | nextFRUAreaLoc = fruAreaLoc; |
| 1217 | } |
| 1218 | } |
| 1219 | std::vector<uint8_t> restFRUAreasData; |
| 1220 | if (nextFRUAreaLoc) |
| 1221 | { |
| 1222 | std::copy_n(fruData.begin() + nextFRUAreaLoc, |
| 1223 | fruData.size() - nextFRUAreaLoc, |
| 1224 | std::back_inserter(restFRUAreasData)); |
| 1225 | } |
| 1226 | |
| 1227 | // check FRU area size |
| 1228 | size_t fruAreaDataSize = |
| 1229 | ((fruUpdateFieldLoc - fruAreaStart + 1) + restFRUAreaFieldsData.size()); |
| 1230 | size_t fruAreaAvailableSize = fruAreaSize - fruAreaDataSize; |
| 1231 | if ((updatePropertyReqLen + 1) > fruAreaAvailableSize) |
| 1232 | { |
| 1233 | |
| 1234 | #ifdef ENABLE_FRU_AREA_RESIZE |
| 1235 | size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1; |
| 1236 | // round size to 8-byte blocks |
| 1237 | newFRUAreaSize = |
| 1238 | ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize; |
| 1239 | size_t newFRUDataSize = fruData.size() + newFRUAreaSize - fruAreaSize; |
| 1240 | fruData.resize(newFRUDataSize); |
| 1241 | fruAreaSize = newFRUAreaSize; |
| 1242 | fruAreaEnd = fruAreaStart + fruAreaSize; |
| 1243 | #else |
| 1244 | std::cerr << "FRU field length: " << updatePropertyReqLen + 1 |
| 1245 | << " should not be greater than available FRU area size: " |
| 1246 | << fruAreaAvailableSize << "\n"; |
| 1247 | return false; |
| 1248 | #endif // ENABLE_FRU_AREA_RESIZE |
| 1249 | } |
| 1250 | |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1251 | // write new requested property field length and data |
| 1252 | constexpr uint8_t newTypeLenMask = 0xC0; |
| 1253 | fruData[fruUpdateFieldLoc] = |
| 1254 | static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask); |
| 1255 | fruUpdateFieldLoc++; |
| 1256 | std::copy(updatePropertyReq.begin(), updatePropertyReq.end(), |
| 1257 | fruData.begin() + fruUpdateFieldLoc); |
| 1258 | |
| 1259 | // Copy remaining data to main fru area - post updated fru field vector |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1260 | restFRUFieldsLoc = fruUpdateFieldLoc + updatePropertyReqLen; |
| 1261 | size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size(); |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1262 | std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(), |
| 1263 | fruData.begin() + restFRUFieldsLoc); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1264 | |
| 1265 | // Update final fru with new fru area length and checksum |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1266 | unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum( |
| 1267 | fruData, fruAreaStart, fruAreaDataEnd, fruAreaEnd); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1268 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1269 | #ifdef ENABLE_FRU_AREA_RESIZE |
| 1270 | ++nextFRUAreaNewLoc; |
| 1271 | ssize_t nextFRUAreaOffsetDiff = |
| 1272 | (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize; |
| 1273 | // Append rest FRU Areas if size changed and there were other sections after |
| 1274 | // updated one |
| 1275 | if (nextFRUAreaOffsetDiff && nextFRUAreaLoc) |
| 1276 | { |
| 1277 | std::copy(restFRUAreasData.begin(), restFRUAreasData.end(), |
| 1278 | fruData.begin() + nextFRUAreaNewLoc); |
| 1279 | // Update Common Header |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1280 | for (int fruArea = fruAreaInternal; fruArea <= fruAreaMultirecord; |
| 1281 | fruArea++) |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1282 | { |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1283 | unsigned int fruAreaOffsetField = getHeaderAreaFieldOffset(fruArea); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1284 | size_t curFRUAreaOffset = fruData[fruAreaOffsetField]; |
| 1285 | if (curFRUAreaOffset > fruAreaOffsetFieldValue) |
| 1286 | { |
| 1287 | fruData[fruAreaOffsetField] = static_cast<int8_t>( |
| 1288 | curFRUAreaOffset + nextFRUAreaOffsetDiff); |
| 1289 | } |
| 1290 | } |
| 1291 | // Calculate new checksum |
| 1292 | std::vector<uint8_t> headerFRUData; |
| 1293 | std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData)); |
| 1294 | size_t checksumVal = calculateChecksum(headerFRUData); |
| 1295 | fruData[7] = static_cast<uint8_t>(checksumVal); |
| 1296 | // fill zeros if FRU Area size decreased |
| 1297 | if (nextFRUAreaOffsetDiff < 0) |
| 1298 | { |
| 1299 | std::fill(fruData.begin() + nextFRUAreaNewLoc + |
| 1300 | restFRUAreasData.size(), |
| 1301 | fruData.end(), 0); |
| 1302 | } |
| 1303 | } |
| 1304 | #else |
| 1305 | // this is to avoid "unused variable" warning |
| 1306 | (void)nextFRUAreaNewLoc; |
| 1307 | #endif // ENABLE_FRU_AREA_RESIZE |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1308 | if (fruData.empty()) |
| 1309 | { |
| 1310 | return false; |
| 1311 | } |
| 1312 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1313 | if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address), |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1314 | fruData)) |
| 1315 | { |
| 1316 | return false; |
| 1317 | } |
| 1318 | |
| 1319 | // Rescan the bus so that GetRawFru dbus-call fetches updated values |
| 1320 | rescanBusses(busMap, dbusInterfaceMap); |
| 1321 | return true; |
| 1322 | } |
| 1323 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1324 | int main() |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1325 | { |
| 1326 | auto devDir = fs::path("/dev/"); |
James Feist | c9dff1b | 2019-02-13 13:33:13 -0800 | [diff] [blame] | 1327 | auto matchString = std::string(R"(i2c-\d+$)"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1328 | std::vector<fs::path> i2cBuses; |
| 1329 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 1330 | if (!findFiles(devDir, matchString, i2cBuses)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1331 | { |
| 1332 | std::cerr << "unable to find i2c devices\n"; |
| 1333 | return 1; |
| 1334 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1335 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 1336 | // check for and load blacklist with initial buses. |
| 1337 | loadBlacklist(blacklistPath); |
| 1338 | |
Vijay Khemka | 065f6d9 | 2018-12-18 10:37:47 -0800 | [diff] [blame] | 1339 | systemBus->request_name("xyz.openbmc_project.FruDevice"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1340 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1341 | // this is a map with keys of pair(bus number, address) and values of |
| 1342 | // the object on dbus |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1343 | boost::container::flat_map<std::pair<size_t, size_t>, |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1344 | std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 1345 | dbusInterfaceMap; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1346 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1347 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 1348 | objServer.add_interface("/xyz/openbmc_project/FruDevice", |
| 1349 | "xyz.openbmc_project.FruDeviceManager"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1350 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1351 | iface->register_method("ReScan", |
| 1352 | [&]() { rescanBusses(busMap, dbusInterfaceMap); }); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1353 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1354 | iface->register_method("ReScanBus", [&](uint8_t bus) { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1355 | rescanOneBus(busMap, bus, dbusInterfaceMap, true); |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1356 | }); |
| 1357 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1358 | iface->register_method("GetRawFru", getFRUInfo); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1359 | |
| 1360 | iface->register_method("WriteFru", [&](const uint8_t bus, |
| 1361 | const uint8_t address, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1362 | const std::vector<uint8_t>& data) { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1363 | if (!writeFRU(bus, address, data)) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1364 | { |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1365 | throw std::invalid_argument("Invalid Arguments."); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1366 | return; |
| 1367 | } |
| 1368 | // schedule rescan on success |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1369 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1370 | }); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1371 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1372 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1373 | std::function<void(sdbusplus::message::message & message)> eventHandler = |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1374 | [&](sdbusplus::message::message& message) { |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1375 | std::string objectName; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1376 | boost::container::flat_map< |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1377 | std::string, |
| 1378 | std::variant<std::string, bool, int64_t, uint64_t, double>> |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1379 | values; |
| 1380 | message.read(objectName, values); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1381 | auto findState = values.find("CurrentHostState"); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1382 | if (findState != values.end()) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1383 | { |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 1384 | powerIsOn = boost::ends_with( |
| 1385 | std::get<std::string>(findState->second), "Running"); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1386 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1387 | |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 1388 | if (powerIsOn) |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1389 | { |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1390 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1391 | } |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1392 | }; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1393 | |
| 1394 | sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1395 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
James Feist | 7bcd3f2 | 2019-03-18 16:04:04 -0700 | [diff] [blame] | 1396 | "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/" |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 1397 | "openbmc_project/state/" |
| 1398 | "host0',arg0='xyz.openbmc_project.State.Host'", |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1399 | eventHandler); |
| 1400 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1401 | int fd = inotify_init(); |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1402 | inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1403 | std::array<char, 4096> readBuffer; |
| 1404 | std::string pendingBuffer; |
| 1405 | // monitor for new i2c devices |
| 1406 | boost::asio::posix::stream_descriptor dirWatch(io, fd); |
| 1407 | std::function<void(const boost::system::error_code, std::size_t)> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1408 | watchI2cBusses = [&](const boost::system::error_code& ec, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1409 | std::size_t bytesTransferred) { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1410 | if (ec) |
| 1411 | { |
| 1412 | std::cout << "Callback Error " << ec << "\n"; |
| 1413 | return; |
| 1414 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1415 | pendingBuffer += std::string(readBuffer.data(), bytesTransferred); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1416 | while (pendingBuffer.size() > sizeof(inotify_event)) |
| 1417 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1418 | const inotify_event* iEvent = |
| 1419 | reinterpret_cast<const inotify_event*>( |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1420 | pendingBuffer.data()); |
| 1421 | switch (iEvent->mask) |
| 1422 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1423 | case IN_CREATE: |
| 1424 | case IN_MOVED_TO: |
| 1425 | case IN_DELETE: |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1426 | std::string name(iEvent->name); |
| 1427 | if (boost::starts_with(name, "i2c")) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1428 | { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1429 | int bus = busStrToInt(name); |
| 1430 | if (bus < 0) |
| 1431 | { |
| 1432 | std::cerr << "Could not parse bus " << name |
| 1433 | << "\n"; |
| 1434 | continue; |
| 1435 | } |
| 1436 | rescanOneBus(busMap, static_cast<uint8_t>(bus), |
| 1437 | dbusInterfaceMap, false); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1438 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len); |
| 1442 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1443 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1444 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), |
| 1445 | watchI2cBusses); |
| 1446 | }; |
| 1447 | |
| 1448 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses); |
Gunnar Mills | b3e42fe | 2018-06-13 15:48:27 -0500 | [diff] [blame] | 1449 | // run the initial scan |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1450 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1451 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1452 | io.run(); |
| 1453 | return 0; |
| 1454 | } |