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