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