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