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